changeset 1089:7e79a58954b1 libavcodec

h263(+) clenaup & bugfixes
author michaelni
date Fri, 28 Feb 2003 13:07:07 +0000
parents bb27c685fc72
children 8216533dd959
files h263.c mpegvideo.c mpegvideo.h
diffstat 3 files changed, 60 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/h263.c	Fri Feb 28 01:59:56 2003 +0000
+++ b/h263.c	Fri Feb 28 13:07:07 2003 +0000
@@ -199,7 +199,7 @@
             put_bits(&s->pb, 3, format);
             
         put_bits(&s->pb,1,0); /* Custom PCF: off */
-        s->umvplus = (s->pict_type == P_TYPE) && s->unrestricted_mv;
+        s->umvplus = s->unrestricted_mv;
         put_bits(&s->pb, 1, s->umvplus); /* Unrestricted Motion Vector */
         put_bits(&s->pb,1,0); /* SAC: off */
         put_bits(&s->pb,1,0); /* Advanced Prediction Mode: off */
@@ -241,7 +241,9 @@
         
         /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
         if (s->umvplus)
-            put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
+//            put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
+            put_bits(&s->pb,2,1); /* unlimited */
+
         put_bits(&s->pb, 5, s->qscale);
     }
 
@@ -1473,12 +1475,12 @@
         break;
     case CODEC_ID_H263P:
         s->fcode_tab= umv_fcode_tab;
-        s->min_qcoeff= -128;
+        s->min_qcoeff= -127;
         s->max_qcoeff=  127;
         break;
         //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later 
     default: //nothing needed default table allready set in mpegvideo.c
-        s->min_qcoeff= -128;
+        s->min_qcoeff= -127;
         s->max_qcoeff=  127;
         s->y_dc_scale_table=
         s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
@@ -1506,26 +1508,26 @@
     rl = &rl_inter;
     if (s->mb_intra && !s->h263_aic) {
         /* DC coef */
-	    level = block[0];
+        level = block[0];
         /* 255 cannot be represented, so we clamp */
         if (level > 254) {
             level = 254;
             block[0] = 254;
         }
         /* 0 cannot be represented also */
-        else if (!level) {
+        else if (level < 1) {
             level = 1;
             block[0] = 1;
         }
-	    if (level == 128)
-	        put_bits(&s->pb, 8, 0xff);
-	    else
-	        put_bits(&s->pb, 8, level & 0xff);
-	    i = 1;
+        if (level == 128)
+            put_bits(&s->pb, 8, 0xff);
+        else
+            put_bits(&s->pb, 8, level & 0xff);
+        i = 1;
     } else {
-	    i = 0;
-	    if (s->h263_aic && s->mb_intra)
-	        rl = &rl_intra_aic;
+        i = 0;
+        if (s->h263_aic && s->mb_intra)
+            rl = &rl_intra_aic;
     }
    
     /* AC coefs */
@@ -1548,7 +1550,16 @@
             if (code == rl->n) {
                 put_bits(&s->pb, 1, last);
                 put_bits(&s->pb, 6, run);
-                put_bits(&s->pb, 8, slevel & 0xff);
+                
+                assert(slevel != 0);
+
+                if(slevel < 128 && slevel > -128) 
+                    put_bits(&s->pb, 8, slevel & 0xff);
+                else{
+                    put_bits(&s->pb, 8, 128);
+                    put_bits(&s->pb, 5, slevel & 0x1f);
+                    put_bits(&s->pb, 6, (slevel>>5)&0x3f);
+                }
             } else {
                 put_bits(&s->pb, 1, sign);
             }
@@ -3188,7 +3199,7 @@
                 /* 16x16 motion prediction */
                 s->mv_type = MV_TYPE_16X16;
                 h263_pred_motion(s, 0, &pred_x, &pred_y);
-                if (s->umvplus_dec)
+                if (s->umvplus)
                    mx = h263p_decode_umotion(s, pred_x);
                 else
                    mx = h263_decode_motion(s, pred_x, s->f_code);
@@ -3196,7 +3207,7 @@
                 if (mx >= 0xffff)
                     return -1;
             
-                if (s->umvplus_dec)
+                if (s->umvplus)
                    my = h263p_decode_umotion(s, pred_y);
                 else
                    my = h263_decode_motion(s, pred_y, s->f_code);
@@ -3206,7 +3217,7 @@
                 s->mv[0][0][0] = mx;
                 s->mv[0][0][1] = my;
 
-                if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
+                if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
                    skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */                   
             }
         } else {
@@ -3214,14 +3225,14 @@
             s->mv_type = MV_TYPE_8X8;
             for(i=0;i<4;i++) {
                 mot_val = h263_pred_motion(s, i, &pred_x, &pred_y);
-                if (s->umvplus_dec)
+                if (s->umvplus)
                   mx = h263p_decode_umotion(s, pred_x);
                 else
                   mx = h263_decode_motion(s, pred_x, s->f_code);
                 if (mx >= 0xffff)
                     return -1;
                 
-                if (s->umvplus_dec)
+                if (s->umvplus)
                   my = h263p_decode_umotion(s, pred_y);
                 else    
                   my = h263_decode_motion(s, pred_y, s->f_code);
@@ -3229,7 +3240,7 @@
                     return -1;
                 s->mv[0][i][0] = mx;
                 s->mv[0][i][1] = my;
-                if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
+                if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
                   skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
                 mot_val[0] = mx;
                 mot_val[1] = my;
@@ -3553,6 +3564,10 @@
             }
         } else {
             level = get_bits(&s->gb, 8);
+            if((level&0x7F) == 0){
+                fprintf("illegal dc at %d %d\n", s->mb_x, s->mb_y);
+                return -1;
+            }
             if (level == 255)
                 level = 128;
         }
@@ -3579,10 +3594,16 @@
             last = get_bits1(&s->gb);
             run = get_bits(&s->gb, 6);
             level = (int8_t)get_bits(&s->gb, 8);
-            if (s->h263_rv10 && level == -128) {
-                /* XXX: should patch encoder too */
-                level = get_bits(&s->gb, 12);
-		level= (level + ((-1)<<11)) ^ ((-1)<<11); //sign extension
+            if(level == -128){
+                if (s->h263_rv10) {
+                    /* XXX: should patch encoder too */
+                    level = get_bits(&s->gb, 12);
+                    level= (level + ((-1)<<11)) ^ ((-1)<<11); //sign extension
+                }else{
+                    level = get_bits(&s->gb, 5);
+                    level += get_bits(&s->gb, 6)<<5;
+                    level= (level + ((-1)<<10)) ^ ((-1)<<10); //sign extension
+                }
             }
         } else {
             run = rl->table_run[code];
@@ -3931,7 +3952,7 @@
             format = get_bits(&s->gb, 3);
             dprintf("ufep=1, format: %d\n", format);
             skip_bits(&s->gb,1); /* Custom PCF */
-            s->umvplus_dec = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */
+            s->umvplus = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */
             skip_bits1(&s->gb); /* Syntax-based Arithmetic Coding (SAC) */
             if (get_bits1(&s->gb) != 0) {
                 s->mv_type = MV_TYPE_8X8; /* Advanced prediction mode */
@@ -4002,8 +4023,9 @@
                 return -1;
             s->width = width;
             s->height = height;
-            if (s->umvplus_dec) {
-                skip_bits1(&s->gb); /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
+            if (s->umvplus) {
+                if(get_bits1(&s->gb)==0) /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
+                    skip_bits1(&s->gb); 
             }
         }
             
--- a/mpegvideo.c	Fri Feb 28 01:59:56 2003 +0000
+++ b/mpegvideo.c	Fri Feb 28 13:07:07 2003 +0000
@@ -656,8 +656,7 @@
         s->h263_aic = 1;
         
         /* These are just to be sure */
-        s->umvplus = 0;
-        s->umvplus_dec = 0;
+        s->umvplus = 1;
         avctx->delay=0;
         s->low_delay=1;
         break;
@@ -2908,17 +2907,14 @@
     
     s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration
     
-    if(s->msmpeg4_version){
-        if(s->pict_type==I_TYPE)
-            s->no_rounding=1;
-        else if(s->flipflop_rounding)
-            s->no_rounding ^= 1;          
-    }else if(s->out_format == FMT_H263){
-        if(s->pict_type==I_TYPE)
-            s->no_rounding=0;
-        else if(s->pict_type!=B_TYPE)
+    if(s->pict_type==I_TYPE){
+        if(s->msmpeg4_version) s->no_rounding=1;
+        else                   s->no_rounding=0;
+    }else if(s->pict_type!=B_TYPE){
+        if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
             s->no_rounding ^= 1;          
     }
+    
     /* Estimate motion for every MB */
     s->mb_intra=0; //for the rate distoration & bit compare functions
     if(s->pict_type != I_TYPE){
@@ -2993,7 +2989,7 @@
 //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
     }
 
-    if(s->codec_id != CODEC_ID_H263P){ //FIXME use umvplus or something
+    if(!s->umvplus){
         if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) {
             s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER);
         
--- a/mpegvideo.h	Fri Feb 28 01:59:56 2003 +0000
+++ b/mpegvideo.h	Fri Feb 28 13:07:07 2003 +0000
@@ -251,7 +251,7 @@
     int last_non_b_pict_type;   /* used for mpeg4 gmc b-frames & ratecontrol */
     int frame_rate_index;
     /* motion compensation */
-    int unrestricted_mv;
+    int unrestricted_mv;          /* mv can point outside of the coded picture */
     int h263_long_vectors; /* use horrible h263v1 long vector mode */
 
     DSPContext dsp;             /* pointers for accelerated dsp fucntions */
@@ -399,8 +399,7 @@
     int gob_index;
         
     /* H.263+ specific */
-    int umvplus;
-    int umvplus_dec;
+    int umvplus;                    /* == H263+ && unrestricted_mv */
     int h263_aic; /* Advanded INTRA Coding (AIC) */
     int h263_aic_dir; /* AIC direction: 0 = left, 1 = top */