]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/h263.c
move motion_val & mb_type to AVFrame patch by (Wolfgang Hesseler <qv at multimediawar...
[ffmpeg] / libavcodec / h263.c
index a3cf187654d4e6c9b7d11f5359e716a5d7fd704b..330d572f51ea403a37818d3c4f3d7435aad5b7fd 100644 (file)
@@ -49,6 +49,8 @@
 #define SPRITE_TRAJ_VLC_BITS 6
 #define MB_TYPE_B_VLC_BITS 4
 #define TEX_VLC_BITS 9
+#define H263_MBTYPE_B_VLC_BITS 6
+#define CBPC_B_VLC_BITS 3
 
 #ifdef CONFIG_ENCODERS
 static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
@@ -185,7 +187,7 @@ void ff_flv_encode_picture_header(MpegEncContext * s, int picture_number)
 
       if(s->h263_aic){
         s->y_dc_scale_table= 
-          s->c_dc_scale_table= h263_aic_dc_scale_table;
+          s->c_dc_scale_table= ff_aic_dc_scale_table;
       }else{
         s->y_dc_scale_table=
           s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
@@ -200,8 +202,6 @@ void h263_encode_picture_header(MpegEncContext * s, int picture_number)
 
     /* Update the pointer to last GOB */
     s->ptr_lastgob = pbBufPtr(&s->pb);
-    s->gob_number = 0;
-
     put_bits(&s->pb, 22, 0x20); /* PSC */
     put_bits(&s->pb, 8, (((int64_t)s->picture_number * 30 * s->avctx->frame_rate_base) / 
                          s->avctx->frame_rate) & 0xff);
@@ -237,17 +237,16 @@ void h263_encode_picture_header(MpegEncContext * s, int picture_number)
             put_bits(&s->pb, 3, format);
             
         put_bits(&s->pb,1,0); /* Custom PCF: off */
-        s->umvplus = s->unrestricted_mv;
-        put_bits(&s->pb, 1, s->umvplus); /* Unrestricted Motion Vector */
+        put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */
         put_bits(&s->pb,1,0); /* SAC: off */
         put_bits(&s->pb,1,s->obmc); /* Advanced Prediction Mode */
         put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
-        put_bits(&s->pb,1,0); /* Deblocking Filter: off */
-        put_bits(&s->pb,1,0); /* Slice Structured: off */
+        put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */
+        put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */
         put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
         put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
-        put_bits(&s->pb,1,0); /* Alternative Inter VLC: off */
-        put_bits(&s->pb,1,0); /* Modified Quantization: off */
+        put_bits(&s->pb,1,s->alt_inter_vlc); /* Alternative Inter VLC */
+        put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */
         put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
         put_bits(&s->pb,3,0); /* Reserved */
                
@@ -279,7 +278,10 @@ void h263_encode_picture_header(MpegEncContext * s, int picture_number)
         /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
         if (s->umvplus)
 //            put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
+//FIXME check actual requested range
             put_bits(&s->pb,2,1); /* unlimited */
+        if(s->h263_slice_structured)
+            put_bits(&s->pb,2,0); /* no weird submodes */
 
         put_bits(&s->pb, 5, s->qscale);
     }
@@ -288,7 +290,7 @@ void h263_encode_picture_header(MpegEncContext * s, int picture_number)
 
     if(s->h263_aic){
          s->y_dc_scale_table= 
-         s->c_dc_scale_table= h263_aic_dc_scale_table;
+         s->c_dc_scale_table= ff_aic_dc_scale_table;
     }else{
         s->y_dc_scale_table=
         s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
@@ -298,22 +300,27 @@ void h263_encode_picture_header(MpegEncContext * s, int picture_number)
 /**
  * Encodes a group of blocks header.
  */
-int h263_encode_gob_header(MpegEncContext * s, int mb_line)
+void h263_encode_gob_header(MpegEncContext * s, int mb_line)
 {
-           align_put_bits(&s->pb);
-           flush_put_bits(&s->pb);
-           /* Call the RTP callback to send the last GOB */
-           if (s->rtp_callback) {
-               int pdif = pbBufPtr(&s->pb) - s->ptr_lastgob;
-               s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number);
-           }
-           put_bits(&s->pb, 17, 1); /* GBSC */
-           s->gob_number = mb_line / s->gob_index;
-           put_bits(&s->pb, 5, s->gob_number); /* GN */
-           put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
-           put_bits(&s->pb, 5, s->qscale); /* GQUANT */
-           //fprintf(stderr,"\nGOB: %2d size: %d", s->gob_number - 1, pdif);
-    return 0;
+    put_bits(&s->pb, 17, 1); /* GBSC */
+
+    if(s->h263_slice_structured){
+        put_bits(&s->pb, 1, 1);
+
+        ff_h263_encode_mba(s);
+
+        if(s->mb_num > 1583)
+            put_bits(&s->pb, 1, 1);
+        put_bits(&s->pb, 5, s->qscale); /* GQUANT */
+        put_bits(&s->pb, 1, 1);
+        put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
+    }else{
+        int gob_number= mb_line / s->gob_index;
+
+        put_bits(&s->pb, 5, gob_number); /* GN */
+        put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
+        put_bits(&s->pb, 5, s->qscale); /* GQUANT */
+    }
 }
 
 static inline int get_block_rate(MpegEncContext * s, DCTELEM block[64], int block_last_index, uint8_t scantable[64]){
@@ -519,12 +526,12 @@ int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my){
         s->mv_type = MV_TYPE_8X8;
         for(i=0; i<4; i++){
             xy= s->block_index[i];
-            s->mv[0][i][0] = s->motion_val[xy][0]*time_pb/time_pp + mx;
-            s->mv[0][i][1] = s->motion_val[xy][1]*time_pb/time_pp + my;
-            s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->motion_val[xy][0]
-                                : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp;
-            s->mv[1][i][1] = my ? s->mv[0][i][1] - s->motion_val[xy][1] 
-                                : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp;
+            s->mv[0][i][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp + mx;
+            s->mv[0][i][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp + my;
+            s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->next_picture.motion_val[0][xy][0]
+                                : s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
+            s->mv[1][i][1] = my ? s->mv[0][i][1] - s->next_picture.motion_val[0][xy][1] 
+                                : s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
         }
         return MB_TYPE_DIRECT2 | MB_TYPE_8x8 | MB_TYPE_L0L1;
     } else if(IS_INTERLACED(colocated_mb_type)){
@@ -546,12 +553,12 @@ int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my){
         }
         return MB_TYPE_DIRECT2 | MB_TYPE_16x8 | MB_TYPE_L0L1 | MB_TYPE_INTERLACED;
     }else{
-        s->mv[0][0][0] = s->mv[0][1][0] = s->mv[0][2][0] = s->mv[0][3][0] = s->motion_val[xy][0]*time_pb/time_pp + mx;
-        s->mv[0][0][1] = s->mv[0][1][1] = s->mv[0][2][1] = s->mv[0][3][1] = s->motion_val[xy][1]*time_pb/time_pp + my;
-        s->mv[1][0][0] = s->mv[1][1][0] = s->mv[1][2][0] = s->mv[1][3][0] = mx ? s->mv[0][0][0] - s->motion_val[xy][0]
-                            : s->motion_val[xy][0]*(time_pb - time_pp)/time_pp;
-        s->mv[1][0][1] = s->mv[1][1][1] = s->mv[1][2][1] = s->mv[1][3][1] = my ? s->mv[0][0][1] - s->motion_val[xy][1] 
-                            : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp;
+        s->mv[0][0][0] = s->mv[0][1][0] = s->mv[0][2][0] = s->mv[0][3][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp + mx;
+        s->mv[0][0][1] = s->mv[0][1][1] = s->mv[0][2][1] = s->mv[0][3][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp + my;
+        s->mv[1][0][0] = s->mv[1][1][0] = s->mv[1][2][0] = s->mv[1][3][0] = mx ? s->mv[0][0][0] - s->next_picture.motion_val[0][xy][0]
+                            : s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
+        s->mv[1][0][1] = s->mv[1][1][1] = s->mv[1][2][1] = s->mv[1][3][1] = my ? s->mv[0][0][1] - s->next_picture.motion_val[0][xy][1] 
+                            : s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
         if((s->avctx->workaround_bugs & FF_BUG_DIRECT_BLOCKSIZE) || !s->quarter_sample)
             s->mv_type= MV_TYPE_16X16;
         else
@@ -589,14 +596,14 @@ void ff_h263_update_motion_val(MpegEncContext * s){
         }
         
         /* no update if 8X8 because it has been done during parsing */
-        s->motion_val[xy][0] = motion_x;
-        s->motion_val[xy][1] = motion_y;
-        s->motion_val[xy + 1][0] = motion_x;
-        s->motion_val[xy + 1][1] = motion_y;
-        s->motion_val[xy + wrap][0] = motion_x;
-        s->motion_val[xy + wrap][1] = motion_y;
-        s->motion_val[xy + 1 + wrap][0] = motion_x;
-        s->motion_val[xy + 1 + wrap][1] = motion_y;
+        s->current_picture.motion_val[0][xy][0] = motion_x;
+        s->current_picture.motion_val[0][xy][1] = motion_y;
+        s->current_picture.motion_val[0][xy + 1][0] = motion_x;
+        s->current_picture.motion_val[0][xy + 1][1] = motion_y;
+        s->current_picture.motion_val[0][xy + wrap][0] = motion_x;
+        s->current_picture.motion_val[0][xy + wrap][1] = motion_y;
+        s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_x;
+        s->current_picture.motion_val[0][xy + 1 + wrap][1] = motion_y;
     }
 
     if(s->encoding){ //FIXME encoding MUST be cleaned up
@@ -723,7 +730,6 @@ void mpeg4_encode_mb(MpegEncContext * s,
     
     //    printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
     if (!s->mb_intra) {
-        /* compute cbp */
         int i, cbp;
         
         if(s->pict_type==B_TYPE){
@@ -945,8 +951,8 @@ void mpeg4_encode_mb(MpegEncContext * s,
                     /* motion vectors: 8x8 mode*/
                     h263_pred_motion(s, i, &pred_x, &pred_y);
 
-                    h263_encode_motion(s, s->motion_val[ s->block_index[i] ][0] - pred_x, s->f_code);
-                    h263_encode_motion(s, s->motion_val[ s->block_index[i] ][1] - pred_y, s->f_code);
+                    h263_encode_motion(s, s->current_picture.motion_val[0][ s->block_index[i] ][0] - pred_x, s->f_code);
+                    h263_encode_motion(s, s->current_picture.motion_val[0][ s->block_index[i] ][1] - pred_y, s->f_code);
                 }
             }
 
@@ -1071,9 +1077,10 @@ void h263_encode_mb(MpegEncContext * s,
         put_bits(&s->pb, 1, 0);        /* mb coded */
         
         cbpc = cbp & 3;
-        if(s->dquant) cbpc+= 8;
         cbpy = cbp >> 2;
-        cbpy ^= 0xf;
+        if(s->alt_inter_vlc==0 || cbpc!=3)
+            cbpy ^= 0xF;
+        if(s->dquant) cbpc+= 8;
         if(s->mv_type==MV_TYPE_16X16){
             put_bits(&s->pb,
                     inter_MCBPC_bits[cbpc],
@@ -1091,8 +1098,8 @@ void h263_encode_mb(MpegEncContext * s,
             h263_pred_motion(s, 0, &pred_x, &pred_y);
             
             if (!s->umvplus) {  
-                h263_encode_motion(s, motion_x - pred_x, s->f_code);
-                h263_encode_motion(s, motion_y - pred_y, s->f_code);
+                h263_encode_motion(s, motion_x - pred_x, 1);
+                h263_encode_motion(s, motion_y - pred_y, 1);
             }
             else {
                 h263p_encode_umotion(s, motion_x - pred_x);
@@ -1117,11 +1124,11 @@ void h263_encode_mb(MpegEncContext * s,
                 /* motion vectors: 8x8 mode*/
                 h263_pred_motion(s, i, &pred_x, &pred_y);
 
-                motion_x= s->motion_val[ s->block_index[i] ][0];
-                motion_y= s->motion_val[ s->block_index[i] ][1];
+                motion_x= s->current_picture.motion_val[0][ s->block_index[i] ][0];
+                motion_y= s->current_picture.motion_val[0][ s->block_index[i] ][1];
                 if (!s->umvplus) {  
-                    h263_encode_motion(s, motion_x - pred_x, s->f_code);
-                    h263_encode_motion(s, motion_y - pred_y, s->f_code);
+                    h263_encode_motion(s, motion_x - pred_x, 1);
+                    h263_encode_motion(s, motion_y - pred_y, 1);
                 }
                 else {
                     h263p_encode_umotion(s, motion_x - pred_x);
@@ -1137,33 +1144,40 @@ void h263_encode_mb(MpegEncContext * s,
             s->mv_bits+= get_bits_diff(s);
         }
     } else {
-        int li = s->h263_aic ? 0 : 1;
+        assert(s->mb_intra);
         
         cbp = 0;
-        for(i=0; i<6; i++) {
+        if (s->h263_aic) {
             /* Predict DC */
-            if (s->h263_aic && s->mb_intra) {
+            for(i=0; i<6; i++) {
                 int16_t level = block[i][0];
-            
+                int scale;
+                
+                if(i<4) scale= s->y_dc_scale;
+                else    scale= s->c_dc_scale;
+
                 pred_dc = h263_pred_dc(s, i, &dc_ptr[i]);
                 level -= pred_dc;
                 /* Quant */
-                if (level < 0)
-                    level = (level + (s->qscale >> 1))/(s->y_dc_scale);
+                if (level >= 0)
+                    level = (level + (scale>>1))/scale;
                 else
-                    level = (level - (s->qscale >> 1))/(s->y_dc_scale);
+                    level = (level - (scale>>1))/scale;
                     
                 /* AIC can change CBP */
                 if (level == 0 && s->block_last_index[i] == 0)
                     s->block_last_index[i] = -1;
-                else if (level < -127)
-                    level = -127;
-                else if (level > 127)
-                    level = 127;
-                
+
+                if(!s->modified_quant){
+                    if (level < -127)
+                        level = -127;
+                    else if (level > 127)
+                        level = 127;
+                }
+
                 block[i][0] = level;
                 /* Reconstruction */ 
-                rec_intradc[i] = (s->y_dc_scale*level) + pred_dc;
+                rec_intradc[i] = scale*level + pred_dc;
                 /* Oddify */
                 rec_intradc[i] |= 1;
                 //if ((rec_intradc[i] % 2) == 0)
@@ -1176,10 +1190,15 @@ void h263_encode_mb(MpegEncContext * s,
                                 
                 /* Update AC/DC tables */
                 *dc_ptr[i] = rec_intradc[i];
+                if (s->block_last_index[i] >= 0)
+                    cbp |= 1 << (5 - i);
+            }
+        }else{
+            for(i=0; i<6; i++) {
+                /* compute cbp */
+                if (s->block_last_index[i] >= 1)
+                    cbp |= 1 << (5 - i);
             }
-            /* compute cbp */
-            if (s->block_last_index[i] >= li)
-                cbp |= 1 << (5 - i);
         }
 
         cbpc = cbp & 3;
@@ -1232,6 +1251,93 @@ void h263_encode_mb(MpegEncContext * s,
 }
 #endif
 
+void ff_h263_loop_filter(MpegEncContext * s){
+    int qp_c;
+    const int linesize  = s->linesize;
+    const int uvlinesize= s->uvlinesize;
+    const int xy = s->mb_y * s->mb_stride + s->mb_x;
+    uint8_t *dest_y = s->dest[0];
+    uint8_t *dest_cb= s->dest[1];
+    uint8_t *dest_cr= s->dest[2];
+    
+//    if(s->pict_type==B_TYPE && !s->readable) return;
+
+    /*
+       Diag Top
+       Left Center
+    */
+    if(!IS_SKIP(s->current_picture.mb_type[xy])){
+        qp_c= s->qscale;
+        s->dsp.h263_v_loop_filter(dest_y+8*linesize  , linesize, qp_c);
+        s->dsp.h263_v_loop_filter(dest_y+8*linesize+8, linesize, qp_c);
+    }else
+        qp_c= 0;
+
+    if(s->mb_y){
+        int qp_dt, qp_t, qp_tc;
+
+        if(IS_SKIP(s->current_picture.mb_type[xy-s->mb_stride]))
+            qp_t=0;
+        else 
+            qp_t= s->current_picture.qscale_table[xy-s->mb_stride];
+
+        if(qp_c) 
+            qp_tc= qp_c;
+        else
+            qp_tc= qp_t;
+            
+        if(qp_tc){
+            const int chroma_qp= s->chroma_qscale_table[qp_tc];
+            s->dsp.h263_v_loop_filter(dest_y  ,   linesize, qp_tc);
+            s->dsp.h263_v_loop_filter(dest_y+8,   linesize, qp_tc);
+        
+            s->dsp.h263_v_loop_filter(dest_cb , uvlinesize, chroma_qp);
+            s->dsp.h263_v_loop_filter(dest_cr , uvlinesize, chroma_qp);
+        }
+        
+        if(qp_t)
+            s->dsp.h263_h_loop_filter(dest_y-8*linesize+8  ,   linesize, qp_t);
+        
+        if(s->mb_x){
+            if(qp_t || IS_SKIP(s->current_picture.mb_type[xy-1-s->mb_stride]))
+                qp_dt= qp_t;
+            else
+                qp_dt= s->current_picture.qscale_table[xy-1-s->mb_stride];
+            
+            if(qp_dt){
+                const int chroma_qp= s->chroma_qscale_table[qp_dt];
+                s->dsp.h263_h_loop_filter(dest_y -8*linesize  ,   linesize, qp_dt);
+                s->dsp.h263_h_loop_filter(dest_cb-8*uvlinesize, uvlinesize, chroma_qp);
+                s->dsp.h263_h_loop_filter(dest_cb-8*uvlinesize, uvlinesize, chroma_qp);
+            }
+        }
+    }
+
+    if(qp_c){
+        s->dsp.h263_h_loop_filter(dest_y +8,   linesize, qp_c);
+        if(s->mb_y + 1 == s->mb_height)
+            s->dsp.h263_h_loop_filter(dest_y+8*linesize+8,   linesize, qp_c);
+    }
+    
+    if(s->mb_x){
+        int qp_lc;
+        if(qp_c || IS_SKIP(s->current_picture.mb_type[xy-1]))
+            qp_lc= qp_c;
+        else
+            qp_lc= s->current_picture.qscale_table[xy-1];
+        
+        if(qp_lc){
+            s->dsp.h263_h_loop_filter(dest_y,   linesize, qp_lc);
+            if(s->mb_y + 1 == s->mb_height){
+                const int chroma_qp= s->chroma_qscale_table[qp_lc];
+                s->dsp.h263_h_loop_filter(dest_y +8*  linesize,   linesize, qp_lc);
+                s->dsp.h263_h_loop_filter(dest_cb             , uvlinesize, chroma_qp);
+                s->dsp.h263_h_loop_filter(dest_cr             , uvlinesize, chroma_qp);
+            }
+        }
+    }
+}
+
 static int h263_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr)
 {
     int x, y, wrap, a, c, pred_dc, scale;
@@ -1285,7 +1391,7 @@ static void h263_pred_acdc(MpegEncContext * s, DCTELEM *block, int n)
     /* find prediction */
     if (n < 4) {
         x = 2 * s->mb_x + 1 + (n & 1);
-        y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
+        y = 2 * s->mb_y + 1 + (n>> 1);
         wrap = s->mb_width * 2 + 2;
         dc_val = s->dc_val[0];
         ac_val = s->ac_val[0][0];
@@ -1309,10 +1415,13 @@ static void h263_pred_acdc(MpegEncContext * s, DCTELEM *block, int n)
     c = dc_val[(x) + (y - 1) * wrap];
     
     /* No prediction outside GOB boundary */
-    if (s->first_slice_line && ((n < 2) || (n > 3)))
-        c = 1024;
-    pred_dc = 1024;
+    if(s->first_slice_line && n!=3){
+        if(n!=2) c= 1024;
+        if(n!=1 && s->mb_x == s->resync_mb_x) a= 1024;
+    }
+    
     if (s->ac_pred) {
+        pred_dc = 1024;
         if (s->h263_aic_dir) {
             /* left prediction */
             if (a != 1024) {
@@ -1347,8 +1456,8 @@ static void h263_pred_acdc(MpegEncContext * s, DCTELEM *block, int n)
     
     if (block[0] < 0)
         block[0] = 0;
-    else if (!(block[0] & 1))
-        block[0]++;
+    else 
+        block[0] |= 1;
     
     /* Update AC/DC tables */
     dc_val[(x) + (y) * wrap] = block[0];
@@ -1371,9 +1480,9 @@ int16_t *h263_pred_motion(MpegEncContext * s, int block,
     wrap = s->block_wrap[0];
     xy = s->block_index[block];
 
-    mot_val = s->motion_val[xy];
+    mot_val = s->current_picture.motion_val[0][xy];
 
-    A = s->motion_val[xy - 1];
+    A = s->current_picture.motion_val[0][xy - 1];
     /* special case for first (slice) line */
     if (s->first_slice_line && block<3) {
         // we cant just change some MVs to simulate that as we need them for the B frames (and ME)
@@ -1381,8 +1490,8 @@ int16_t *h263_pred_motion(MpegEncContext * s, int block,
         if(block==0){ //most common case
             if(s->mb_x  == s->resync_mb_x){ //rare
                 *px= *py = 0;
-            }else if(s->mb_x + 1 == s->resync_mb_x){ //rare
-                C = s->motion_val[xy + off[block] - wrap];
+            }else if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
+                C = s->current_picture.motion_val[0][xy + off[block] - wrap];
                 if(s->mb_x==0){
                     *px = C[0];
                     *py = C[1];
@@ -1395,8 +1504,8 @@ int16_t *h263_pred_motion(MpegEncContext * s, int block,
                 *py = A[1];
             }
         }else if(block==1){
-            if(s->mb_x + 1 == s->resync_mb_x){ //rare
-                C = s->motion_val[xy + off[block] - wrap];
+            if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
+                C = s->current_picture.motion_val[0][xy + off[block] - wrap];
                 *px = mid_pred(A[0], 0, C[0]);
                 *py = mid_pred(A[1], 0, C[1]);
             }else{
@@ -1404,8 +1513,8 @@ int16_t *h263_pred_motion(MpegEncContext * s, int block,
                 *py = A[1];
             }
         }else{ /* block==2*/
-            B = s->motion_val[xy - wrap];
-            C = s->motion_val[xy + off[block] - wrap];
+            B = s->current_picture.motion_val[0][xy - wrap];
+            C = s->current_picture.motion_val[0][xy + off[block] - wrap];
             if(s->mb_x == s->resync_mb_x) //rare
                 A[0]=A[1]=0;
     
@@ -1413,14 +1522,75 @@ int16_t *h263_pred_motion(MpegEncContext * s, int block,
             *py = mid_pred(A[1], B[1], C[1]);
         }
     } else {
-        B = s->motion_val[xy - wrap];
-        C = s->motion_val[xy + off[block] - wrap];
+        B = s->current_picture.motion_val[0][xy - wrap];
+        C = s->current_picture.motion_val[0][xy + off[block] - wrap];
         *px = mid_pred(A[0], B[0], C[0]);
         *py = mid_pred(A[1], B[1], C[1]);
     }
     return mot_val;
 }
 
+// identical to above but with s->current_picture->motion_val, the above one will be removed, and this renamed to it
+int16_t *h263_pred_motion2(MpegEncContext * s, int block, int dir,
+                        int *px, int *py)
+{
+    int xy, wrap;
+    int16_t *A, *B, *C, (*mot_val)[2];
+    static const int off[4]= {2, 1, 1, -1};
+
+    wrap = s->b8_stride;
+    xy = s->mb_x + s->mb_y * wrap;
+
+    mot_val = s->current_picture.motion_val[0][dir] + xy;
+
+    A = mot_val[ - 1];
+    /* special case for first (slice) line */
+    if (s->first_slice_line && block<3) {
+        // we cant just change some MVs to simulate that as we need them for the B frames (and ME)
+        // and if we ever support non rectangular objects than we need to do a few ifs here anyway :(
+        if(block==0){ //most common case
+            if(s->mb_x  == s->resync_mb_x){ //rare
+                *px= *py = 0;
+            }else if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
+                C = mot_val[off[block] - wrap];
+                if(s->mb_x==0){
+                    *px = C[0];
+                    *py = C[1];
+                }else{
+                    *px = mid_pred(A[0], 0, C[0]);
+                    *py = mid_pred(A[1], 0, C[1]);
+                }
+            }else{
+                *px = A[0];
+                *py = A[1];
+            }
+        }else if(block==1){
+            if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
+                C = mot_val[off[block] - wrap];
+                *px = mid_pred(A[0], 0, C[0]);
+                *py = mid_pred(A[1], 0, C[1]);
+            }else{
+                *px = A[0];
+                *py = A[1];
+            }
+        }else{ /* block==2*/
+            B = mot_val[ - wrap];
+            C = mot_val[off[block] - wrap];
+            if(s->mb_x == s->resync_mb_x) //rare
+                A[0]=A[1]=0;
+    
+            *px = mid_pred(A[0], B[0], C[0]);
+            *py = mid_pred(A[1], B[1], C[1]);
+        }
+    } else {
+        B = mot_val[ - wrap];
+        C = mot_val[off[block] - wrap];
+        *px = mid_pred(A[0], B[0], C[0]);
+        *py = mid_pred(A[1], B[1], C[1]);
+    }
+    return *mot_val;
+}
+
 #ifdef CONFIG_ENCODERS
 static void h263_encode_motion(MpegEncContext * s, int val, int f_code)
 {
@@ -1747,9 +1917,17 @@ void h263_encode_init(MpegEncContext *s)
         
         break;
     case CODEC_ID_H263P:
-        s->fcode_tab= umv_fcode_tab;
-        s->min_qcoeff= -127;
-        s->max_qcoeff=  127;
+        if(s->umvplus)
+            s->fcode_tab= umv_fcode_tab;
+        else
+            s->fcode_tab= fcode_tab;
+        if(s->modified_quant){
+            s->min_qcoeff= -2047;
+            s->max_qcoeff=  2047;
+        }else{
+            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 
     case CODEC_ID_FLV1:
@@ -1804,6 +1982,44 @@ static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
         i = 0;
         if (s->h263_aic && s->mb_intra)
             rl = &rl_intra_aic;
+            
+        if(s->alt_inter_vlc && !s->mb_intra){
+            int aic_vlc_bits=0;
+            int inter_vlc_bits=0;
+            int wrong_pos=-1;
+            int aic_code;
+            
+            last_index = s->block_last_index[n];
+            last_non_zero = i - 1;
+            for (; i <= last_index; i++) {
+                j = s->intra_scantable.permutated[i];
+                level = block[j];
+                if (level) {
+                    run = i - last_non_zero - 1;
+                    last = (i == last_index);
+                    
+                    if(level<0) level= -level;
+                
+                    code = get_rl_index(rl, last, run, level);
+                    aic_code = get_rl_index(&rl_intra_aic, last, run, level);
+                    inter_vlc_bits += rl->table_vlc[code][1]+1;
+                    aic_vlc_bits   += rl_intra_aic.table_vlc[aic_code][1]+1;
+
+                    if (code == rl->n) {
+                        inter_vlc_bits += 1+6+8-1;
+                    }                
+                    if (aic_code == rl_intra_aic.n) {
+                        aic_vlc_bits += 1+6+8-1;
+                        wrong_pos += run + 1;
+                    }else
+                        wrong_pos += wrong_run[aic_code];
+                    last_non_zero = i;
+                }    
+            }
+            i = 0;
+            if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
+                rl = &rl_intra_aic;
+        }
     }
    
     /* AC coefs */
@@ -2091,7 +2307,7 @@ void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
     }
     put_bits(&s->pb, 3, 0);    /* intra dc VLC threshold */
     if(!s->progressive_sequence){
-         put_bits(&s->pb, 1, s->top_field_first);
+         put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
          put_bits(&s->pb, 1, s->alternate_scan);
     }
     //FIXME sprite stuff
@@ -2111,19 +2327,20 @@ void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
 #endif //CONFIG_ENCODERS
 
 /**
- * change qscale by given dquant and update qscale dependant variables.
+ * set qscale and update qscale dependant variables.
  */
-static void change_qscale(MpegEncContext * s, int dquant)
+void ff_set_qscale(MpegEncContext * s, int qscale)
 {
-    s->qscale += dquant;
-
-    if (s->qscale < 1)
-        s->qscale = 1;
-    else if (s->qscale > 31)
-        s->qscale = 31;
+    if (qscale < 1)
+        qscale = 1;
+    else if (qscale > 31)
+        qscale = 31;
+        
+    s->qscale = qscale;
+    s->chroma_qscale= s->chroma_qscale_table[qscale];
 
-    s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
-    s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
+    s->y_dc_scale= s->y_dc_scale_table[ qscale ];
+    s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
 }
 
 /**
@@ -2477,6 +2694,8 @@ static VLC mv_vlc;
 static VLC dc_lum, dc_chrom;
 static VLC sprite_trajectory;
 static VLC mb_type_b_vlc;
+static VLC h263_mbtype_b_vlc;
+static VLC cbpc_b_vlc;
 
 void init_vlc_rl(RLTable *rl)
 {
@@ -2569,6 +2788,12 @@ void h263_decode_init_vlc(MpegEncContext *s)
         init_vlc(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
                  &mb_type_b_tab[0][1], 2, 1,
                  &mb_type_b_tab[0][0], 2, 1);
+        init_vlc(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
+                 &h263_mbtype_b_tab[0][1], 2, 1,
+                 &h263_mbtype_b_tab[0][0], 2, 1);
+        init_vlc(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
+                 &cbpc_b_tab[0][1], 2, 1,
+                 &cbpc_b_tab[0][0], 2, 1);
     }
 }
 
@@ -2584,13 +2809,38 @@ int ff_h263_get_gob_height(MpegEncContext *s){
         return 4;
 }
 
+int ff_h263_decode_mba(MpegEncContext *s)
+{
+    int i, mb_pos;
+
+    for(i=0; i<6; i++){
+        if(s->mb_num < ff_mba_max[i]) break;
+    }
+    mb_pos= get_bits(&s->gb, ff_mba_length[i]);
+    s->mb_x= mb_pos % s->mb_width;
+    s->mb_y= mb_pos / s->mb_width;
+
+    return mb_pos;
+}
+
+void ff_h263_encode_mba(MpegEncContext *s)
+{
+    int i, mb_pos;
+
+    for(i=0; i<6; i++){
+        if(s->mb_num < ff_mba_max[i]) break;
+    }
+    mb_pos= s->mb_x + s->mb_width*s->mb_y;
+    put_bits(&s->pb, ff_mba_length[i], mb_pos);
+}
+
 /**
- * decodes the group of blocks header.
+ * decodes the group of blocks header or slice header.
  * @return <0 if an error occured
  */
 static int h263_decode_gob_header(MpegEncContext *s)
 {
-    unsigned int val, gfid;
+    unsigned int val, gfid, gob_number;
     int left;
     
     /* Check for GOB Start Code */
@@ -2608,21 +2858,34 @@ static int h263_decode_gob_header(MpegEncContext *s)
     if(left<=13) 
         return -1;
 
-#ifdef DEBUG
-    fprintf(stderr,"\nGOB Start Code at MB %d\n", (s->mb_y * s->mb_width) + s->mb_x);
-#endif
-    s->gob_number = get_bits(&s->gb, 5); /* GN */
-    gfid = get_bits(&s->gb, 2); /* GFID */
-    s->qscale = get_bits(&s->gb, 5); /* GQUANT */
-    if(s->qscale==0) 
-        return -1;
-    s->mb_x= 0;
-    s->mb_y= s->gob_index* s->gob_number;
+    if(s->h263_slice_structured){
+        if(get_bits1(&s->gb)==0)
+            return -1;
+
+        ff_h263_decode_mba(s);
+
+        if(s->mb_num > 1583)
+            if(get_bits1(&s->gb)==0)
+                return -1;
+        
+        s->qscale = get_bits(&s->gb, 5); /* SQUANT */
+        if(get_bits1(&s->gb)==0)
+            return -1;
+        gfid = get_bits(&s->gb, 2); /* GFID */
+    }else{
+        gob_number = get_bits(&s->gb, 5); /* GN */
+        s->mb_x= 0;
+        s->mb_y= s->gob_index* gob_number;
+        gfid = get_bits(&s->gb, 2); /* GFID */
+        s->qscale = get_bits(&s->gb, 5); /* GQUANT */
+    }
+        
     if(s->mb_y >= s->mb_height) 
         return -1;
-#ifdef DEBUG
-    fprintf(stderr, "\nGN: %u GFID: %u Quant: %u\n", s->gob_number, gfid, s->qscale);
-#endif
+
+    if(s->qscale==0) 
+        return -1;
+
     return 0;
 }
 
@@ -2688,7 +2951,6 @@ void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
 {
     int mb_num_bits= av_log2(s->mb_num - 1) + 1;
 
-    ff_mpeg4_stuffing(&s->pb);
     put_bits(&s->pb, ff_mpeg4_get_video_packet_prefix_length(s), 0);
     put_bits(&s->pb, 1, 1);
     
@@ -2779,7 +3041,7 @@ static int mpeg4_decode_video_packet_header(MpegEncContext *s)
     if(s->shape != BIN_ONLY_SHAPE){
         int qscale= get_bits(&s->gb, s->quant_precision); 
         if(qscale)
-            s->qscale= qscale;
+            s->chroma_qscale=s->qscale= qscale;
     }
 
     if(s->shape == RECT_SHAPE){
@@ -2991,7 +3253,7 @@ static int mpeg4_decode_partition_a(MpegEncContext *s){
                 s->mb_intra = 1;
 
                 if(cbpc & 4) {
-                    change_qscale(s, quant_tab[get_bits(&s->gb, 2)]);
+                    ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
                 }
                 s->current_picture.qscale_table[xy]= s->qscale;
 
@@ -3009,7 +3271,7 @@ static int mpeg4_decode_partition_a(MpegEncContext *s){
                 s->pred_dir_table[xy]= dir;
             }else{ /* P/S_TYPE */
                 int mx, my, pred_x, pred_y, bits;
-                int16_t * const mot_val= s->motion_val[s->block_index[0]];
+                int16_t * const mot_val= s->current_picture.motion_val[0][s->block_index[0]];
                 const int stride= s->block_wrap[0]*2;
 
 //              do{ //FIXME
@@ -3154,7 +3416,7 @@ static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count){
                     }
                     
                     if(s->cbp_table[xy] & 8) {
-                        change_qscale(s, quant_tab[get_bits(&s->gb, 2)]);
+                        ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
                     }
                     s->current_picture.qscale_table[xy]= s->qscale;
 
@@ -3184,7 +3446,7 @@ static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count){
                     }
                     
                     if(s->cbp_table[xy] & 8) {
-                        change_qscale(s, quant_tab[get_bits(&s->gb, 2)]);
+                        ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
                     }
                     s->current_picture.qscale_table[xy]= s->qscale;
 
@@ -3261,16 +3523,14 @@ static int mpeg4_decode_partitioned_mb(MpegEncContext *s, DCTELEM block[6][64])
     cbp = s->cbp_table[xy];
 
     if(s->current_picture.qscale_table[xy] != s->qscale){
-        s->qscale= s->current_picture.qscale_table[xy];
-        s->y_dc_scale= s->y_dc_scale_table[ s->qscale ];
-        s->c_dc_scale= s->c_dc_scale_table[ s->qscale ];
+        ff_set_qscale(s, s->current_picture.qscale_table[xy] );
     }
     
     if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) {
         int i;
         for(i=0; i<4; i++){
-            s->mv[0][i][0] = s->motion_val[ s->block_index[i] ][0];
-            s->mv[0][i][1] = s->motion_val[ s->block_index[i] ][1];
+            s->mv[0][i][0] = s->current_picture.motion_val[0][ s->block_index[i] ][0];
+            s->mv[0][i][1] = s->current_picture.motion_val[0][ s->block_index[i] ][1];
         }
         s->mb_intra = IS_INTRA(mb_type);
 
@@ -3356,7 +3616,7 @@ static void preview_obmc(MpegEncContext *s){
     do{
         if (get_bits1(&s->gb)) {
             /* skip mb */
-            mot_val = s->motion_val[ s->block_index[0] ];
+            mot_val = s->current_picture.motion_val[0][ s->block_index[0] ];
             mot_val[0       ]= mot_val[2       ]= 
             mot_val[0+stride]= mot_val[2+stride]= 0;
             mot_val[1       ]= mot_val[3       ]=
@@ -3373,7 +3633,11 @@ static void preview_obmc(MpegEncContext *s){
     }else{
         get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
         if (cbpc & 8) {
-            skip_bits(&s->gb, 2);
+            if(s->modified_quant){
+                if(get_bits1(&s->gb)) skip_bits(&s->gb, 1);
+                else                  skip_bits(&s->gb, 5);
+            }else
+                skip_bits(&s->gb, 2);
         }
         
         if ((cbpc & 16) == 0) {
@@ -3383,12 +3647,12 @@ static void preview_obmc(MpegEncContext *s){
                 if (s->umvplus)
                    mx = h263p_decode_umotion(s, pred_x);
                 else
-                   mx = h263_decode_motion(s, pred_x, s->f_code);
+                   mx = h263_decode_motion(s, pred_x, 1);
             
                 if (s->umvplus)
                    my = h263p_decode_umotion(s, pred_y);
                 else
-                   my = h263_decode_motion(s, pred_y, s->f_code);
+                   my = h263_decode_motion(s, pred_y, 1);
             
                 mot_val[0       ]= mot_val[2       ]= 
                 mot_val[0+stride]= mot_val[2+stride]= mx;
@@ -3401,12 +3665,12 @@ static void preview_obmc(MpegEncContext *s){
                 if (s->umvplus)
                   mx = h263p_decode_umotion(s, pred_x);
                 else
-                  mx = h263_decode_motion(s, pred_x, s->f_code);
+                  mx = h263_decode_motion(s, pred_x, 1);
                 
                 if (s->umvplus)
                   my = h263p_decode_umotion(s, pred_y);
                 else    
-                  my = h263_decode_motion(s, pred_y, s->f_code);
+                  my = h263_decode_motion(s, 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;
@@ -3425,46 +3689,306 @@ end:
     s->gb= gb;
 }
 
+static void h263_decode_dquant(MpegEncContext *s){
+    static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
+
+    if(s->modified_quant){
+        if(get_bits1(&s->gb))
+            s->qscale= modified_quant_tab[get_bits1(&s->gb)][ s->qscale ];
+        else
+            s->qscale= get_bits(&s->gb, 5);
+    }else
+        s->qscale += quant_tab[get_bits(&s->gb, 2)];
+    ff_set_qscale(s, s->qscale);
+}
+
 int ff_h263_decode_mb(MpegEncContext *s,
                       DCTELEM block[6][64])
 {
     int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
     int16_t *mot_val;
-    static int8_t quant_tab[4] = { -1, -2, 1, 2 };
     const int xy= s->mb_x + s->mb_y * s->mb_stride;
     
-    if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) {
-      do{
-        if (get_bits1(&s->gb)) {
-            /* skip mb */
-            s->mb_intra = 0;
-            for(i=0;i<6;i++)
-                s->block_last_index[i] = -1;
-            s->mv_dir = MV_DIR_FORWARD;
-            s->mv_type = MV_TYPE_16X16;
-            if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
-                s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0;
-                s->mcsel=1;
-                s->mv[0][0][0]= get_amv(s, 0);
-                s->mv[0][0][1]= get_amv(s, 1);
-
-                s->mb_skiped = 0;
-            }else{
+    assert(!s->h263_pred);
+    
+    if (s->pict_type == P_TYPE) {
+        do{
+            if (get_bits1(&s->gb)) {
+                /* skip mb */
+                s->mb_intra = 0;
+                for(i=0;i<6;i++)
+                    s->block_last_index[i] = -1;
+                s->mv_dir = MV_DIR_FORWARD;
+                s->mv_type = MV_TYPE_16X16;
                 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
-                s->mcsel=0;
                 s->mv[0][0][0] = 0;
                 s->mv[0][0][1] = 0;
-                s->mb_skiped = !s->obmc;
+                s->mb_skiped = !(s->obmc | s->loop_filter);
+                goto end;
             }
-            goto end;
+            cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
+            //fprintf(stderr, "\tCBPC: %d", cbpc);
+            if (cbpc < 0){
+                av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
+                return -1;
+            }
+        }while(cbpc == 20);
+        
+        dquant = cbpc & 8;
+        s->mb_intra = ((cbpc & 4) != 0);
+        if (s->mb_intra) goto intra;
+        
+        cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
+        
+        if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
+            cbpy ^= 0xF;
+        
+        cbp = (cbpc & 3) | (cbpy << 2);
+        if (dquant) {
+            h263_decode_dquant(s);
         }
-        cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
-        //fprintf(stderr, "\tCBPC: %d", cbpc);
-        if (cbpc < 0){
-            av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
+        
+        s->mv_dir = MV_DIR_FORWARD;
+        if ((cbpc & 16) == 0) {
+            s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0; 
+            /* 16x16 motion prediction */
+            s->mv_type = MV_TYPE_16X16;
+            h263_pred_motion(s, 0, &pred_x, &pred_y);
+            if (s->umvplus)
+               mx = h263p_decode_umotion(s, pred_x);
+            else
+               mx = h263_decode_motion(s, pred_x, 1);
+            
+            if (mx >= 0xffff)
+                return -1;
+            
+            if (s->umvplus)
+               my = h263p_decode_umotion(s, pred_y);
+            else
+               my = h263_decode_motion(s, pred_y, 1);
+            
+            if (my >= 0xffff)
+                return -1;
+            s->mv[0][0][0] = mx;
+            s->mv[0][0][1] = my;
+
+            if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
+               skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */                   
+        } else {
+            s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0; 
+            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)
+                  mx = h263p_decode_umotion(s, pred_x);
+                else
+                  mx = h263_decode_motion(s, pred_x, 1);
+                if (mx >= 0xffff)
+                    return -1;
+                
+                if (s->umvplus)
+                  my = h263p_decode_umotion(s, pred_y);
+                else    
+                  my = h263_decode_motion(s, pred_y, 1);
+                if (my >= 0xffff)
+                    return -1;
+                s->mv[0][i][0] = mx;
+                s->mv[0][i][1] = my;
+                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;
+            }
+        }
+
+        if(s->obmc){
+            if(s->pict_type == P_TYPE && s->mb_x+1<s->mb_width)
+                preview_obmc(s);
+        }
+    } else if(s->pict_type==B_TYPE) {
+        int mb_type;
+        const int stride= s->b8_stride;
+        int16_t *mot_val0 = s->current_picture.motion_val[0][ s->mb_x + s->mb_y*stride ];
+        int16_t *mot_val1 = s->current_picture.motion_val[1][ s->mb_x + s->mb_y*stride ];
+//        const int mv_xy= s->mb_x + 1 + s->mb_y * s->mb_stride;
+
+        //FIXME ugly 
+        mot_val0[0       ]= mot_val0[2       ]= mot_val0[0+stride]= mot_val0[2+stride]= 0;
+        mot_val0[1       ]= mot_val0[3       ]= mot_val0[1+stride]= mot_val0[3+stride]= 0;
+        mot_val1[0       ]= mot_val1[2       ]= mot_val1[0+stride]= mot_val1[2+stride]= 0;
+        mot_val1[1       ]= mot_val1[3       ]= mot_val1[1+stride]= mot_val1[3+stride]= 0;
+
+        do{
+            mb_type= get_vlc2(&s->gb, h263_mbtype_b_vlc.table, H263_MBTYPE_B_VLC_BITS, 2);
+            if (mb_type < 0){
+                av_log(s->avctx, AV_LOG_ERROR, "b mb_type damaged at %d %d\n", s->mb_x, s->mb_y);
+                return -1;
+            }
+
+            mb_type= h263_mb_type_b_map[ mb_type ];
+        }while(!mb_type);
+
+        s->mb_intra = IS_INTRA(mb_type);
+        if(HAS_CBP(mb_type)){
+            cbpc = get_vlc2(&s->gb, cbpc_b_vlc.table, CBPC_B_VLC_BITS, 1);
+            if(s->mb_intra){
+                dquant = IS_QUANT(mb_type);
+                goto intra;
+            }
+
+            cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
+
+            if (cbpy < 0){
+                av_log(s->avctx, AV_LOG_ERROR, "b cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
+                return -1;
+            }
+        
+            if(s->alt_inter_vlc==0 || (cbpc & 3)!=3)
+                cbpy ^= 0xF;
+        
+            cbp = (cbpc & 3) | (cbpy << 2);
+        }else
+            cbp=0;
+            
+        assert(!s->mb_intra);
+
+        if(IS_QUANT(mb_type)){
+            h263_decode_dquant(s);
+        }
+
+        if(IS_DIRECT(mb_type)){
+            s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
+            mb_type |= ff_mpeg4_set_direct_mv(s, 0, 0);
+        }else{
+            s->mv_dir = 0;
+            s->mv_type= MV_TYPE_16X16;
+//FIXME UMV
+
+            if(USES_LIST(mb_type, 0)){
+                int16_t *mot_val= h263_pred_motion2(s, 0, 0, &mx, &my);
+                s->mv_dir = MV_DIR_FORWARD;
+
+                mx = h263_decode_motion(s, mx, 1);
+                my = h263_decode_motion(s, my, 1);
+                s->mv[0][0][0] = mx;
+                s->mv[0][0][1] = my;
+                mot_val[0       ]= mot_val[2       ]= mot_val[0+stride]= mot_val[2+stride]= mx;
+                mot_val[1       ]= mot_val[3       ]= mot_val[1+stride]= mot_val[3+stride]= my;
+            }
+    
+            if(USES_LIST(mb_type, 1)){
+                int16_t *mot_val= h263_pred_motion2(s, 0, 1, &mx, &my);
+                s->mv_dir |= MV_DIR_BACKWARD;
+
+                mx = h263_decode_motion(s, mx, 1);
+                my = h263_decode_motion(s, my, 1);
+                s->mv[1][0][0] = mx;
+                s->mv[1][0][1] = my;
+                mot_val[0       ]= mot_val[2       ]= mot_val[0+stride]= mot_val[2+stride]= mx;
+                mot_val[1       ]= mot_val[3       ]= mot_val[1+stride]= mot_val[3+stride]= my;
+            }
+        }
+          
+        s->current_picture.mb_type[xy]= mb_type;
+    } else { /* I-Frame */
+        do{
+            cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
+            if (cbpc < 0){
+                av_log(s->avctx, AV_LOG_ERROR, "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
+                return -1;
+            }
+        }while(cbpc == 8);
+
+        dquant = cbpc & 4;
+        s->mb_intra = 1;
+intra:
+        s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
+        if (s->h263_aic) {
+            s->ac_pred = get_bits1(&s->gb);
+            if(s->ac_pred){
+                s->current_picture.mb_type[xy]= MB_TYPE_INTRA | MB_TYPE_ACPRED;
+            
+                s->h263_aic_dir = get_bits1(&s->gb);
+            }
+        }else
+            s->ac_pred = 0;
+        
+        cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
+        if(cbpy<0){
+            av_log(s->avctx, AV_LOG_ERROR, "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
+            return -1;
+        }
+        cbp = (cbpc & 3) | (cbpy << 2);
+        if (dquant) {
+            h263_decode_dquant(s);
+        }
+    }
+
+    /* decode each block */
+    for (i = 0; i < 6; i++) {
+        if (h263_decode_block(s, block[i], i, cbp&32) < 0)
             return -1;
+        cbp+=cbp;
+    }
+end:
+
+        /* per-MB end of slice check */
+    {
+        int v= show_bits(&s->gb, 16);
+    
+        if(get_bits_count(&s->gb) + 16 > s->gb.size_in_bits){
+            v>>= get_bits_count(&s->gb) + 16 - s->gb.size_in_bits;
         }
-      }while(cbpc == 20);
+
+        if(v==0)
+            return SLICE_END;
+    }
+
+    return SLICE_OK;     
+}
+
+int ff_mpeg4_decode_mb(MpegEncContext *s,
+                      DCTELEM block[6][64])
+{
+    int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
+    int16_t *mot_val;
+    static int8_t quant_tab[4] = { -1, -2, 1, 2 };
+    const int xy= s->mb_x + s->mb_y * s->mb_stride;
+    
+    assert(s->h263_pred);
+    
+    if (s->pict_type == P_TYPE || s->pict_type==S_TYPE) {
+        do{
+            if (get_bits1(&s->gb)) {
+                /* skip mb */
+                s->mb_intra = 0;
+                for(i=0;i<6;i++)
+                    s->block_last_index[i] = -1;
+                s->mv_dir = MV_DIR_FORWARD;
+                s->mv_type = MV_TYPE_16X16;
+                if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
+                    s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_GMC | MB_TYPE_16x16 | MB_TYPE_L0;
+                    s->mcsel=1;
+                    s->mv[0][0][0]= get_amv(s, 0);
+                    s->mv[0][0][1]= get_amv(s, 1);
+
+                    s->mb_skiped = 0;
+                }else{
+                    s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
+                    s->mcsel=0;
+                    s->mv[0][0][0] = 0;
+                    s->mv[0][0][1] = 0;
+                    s->mb_skiped = 1;
+                }
+                goto end;
+            }
+            cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
+            //fprintf(stderr, "\tCBPC: %d", cbpc);
+            if (cbpc < 0){
+                av_log(s->avctx, AV_LOG_ERROR, "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
+                return -1;
+            }
+        }while(cbpc == 20);
         
         dquant = cbpc & 8;
         s->mb_intra = ((cbpc & 4) != 0);
@@ -3473,10 +3997,11 @@ int ff_h263_decode_mb(MpegEncContext *s,
         if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
             s->mcsel= get_bits1(&s->gb);
         else s->mcsel= 0;
-        cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
-        cbp = (cbpc & 3) | ((cbpy ^ 0xf) << 2);
+        cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
+        
+        cbp = (cbpc & 3) | (cbpy << 2);
         if (dquant) {
-            change_qscale(s, quant_tab[get_bits(&s->gb, 2)]);
+            ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
         }
         if((!s->progressive_sequence) && (cbp || (s->workaround_bugs&FF_BUG_XVID_ILACE)))
             s->interlaced_dct= get_bits1(&s->gb);
@@ -3518,49 +4043,32 @@ int ff_h263_decode_mb(MpegEncContext *s,
                 /* 16x16 motion prediction */
                 s->mv_type = MV_TYPE_16X16;
                 h263_pred_motion(s, 0, &pred_x, &pred_y);
-                if (s->umvplus)
-                   mx = h263p_decode_umotion(s, pred_x);
-                else
-                   mx = h263_decode_motion(s, pred_x, s->f_code);
+                mx = h263_decode_motion(s, pred_x, s->f_code);
             
                 if (mx >= 0xffff)
                     return -1;
             
-                if (s->umvplus)
-                   my = h263p_decode_umotion(s, pred_y);
-                else
-                   my = h263_decode_motion(s, pred_y, s->f_code);
+                my = h263_decode_motion(s, pred_y, s->f_code);
             
                 if (my >= 0xffff)
                     return -1;
                 s->mv[0][0][0] = mx;
                 s->mv[0][0][1] = my;
-
-                if (s->umvplus && (mx - pred_x) == 1 && (my - pred_y) == 1)
-                   skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */                   
             }
         } else {
             s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0; 
             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)
-                  mx = h263p_decode_umotion(s, pred_x);
-                else
-                  mx = h263_decode_motion(s, pred_x, s->f_code);
+                mx = h263_decode_motion(s, pred_x, s->f_code);
                 if (mx >= 0xffff)
                     return -1;
                 
-                if (s->umvplus)
-                  my = h263p_decode_umotion(s, pred_y);
-                else    
-                  my = h263_decode_motion(s, pred_y, s->f_code);
+                my = h263_decode_motion(s, pred_y, s->f_code);
                 if (my >= 0xffff)
                     return -1;
                 s->mv[0][i][0] = mx;
                 s->mv[0][i][1] = my;
-                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;
             }
@@ -3617,7 +4125,7 @@ int ff_h263_decode_mb(MpegEncContext *s,
 
             if ((!IS_DIRECT(mb_type)) && cbp) {
                 if(get_bits1(&s->gb)){
-                    change_qscale(s, get_bits1(&s->gb)*4 - 2);
+                    ff_set_qscale(s, s->qscale + get_bits1(&s->gb)*4 - 2);
                 }
             }
 
@@ -3712,17 +4220,11 @@ int ff_h263_decode_mb(MpegEncContext *s,
         dquant = cbpc & 4;
         s->mb_intra = 1;
 intra:
-        s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
-        if (s->h263_pred || s->h263_aic) {
-            s->ac_pred = get_bits1(&s->gb);
-            if(s->ac_pred){
-                s->current_picture.mb_type[xy]= MB_TYPE_INTRA | MB_TYPE_ACPRED;
-            
-                if (s->h263_aic)
-                    s->h263_aic_dir = get_bits1(&s->gb);
-            }
-        }else
-            s->ac_pred = 0;
+        s->ac_pred = get_bits1(&s->gb);
+        if(s->ac_pred)
+            s->current_picture.mb_type[xy]= MB_TYPE_INTRA | MB_TYPE_ACPRED;
+        else
+            s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
         
         cbpy = get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1);
         if(cbpy<0){
@@ -3731,48 +4233,28 @@ intra:
         }
         cbp = (cbpc & 3) | (cbpy << 2);
         if (dquant) {
-            change_qscale(s, quant_tab[get_bits(&s->gb, 2)]);
+            ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
         }
         
         if(!s->progressive_sequence)
             s->interlaced_dct= get_bits1(&s->gb);
 
         /* decode each block */
-        if (s->h263_pred) {
-            for (i = 0; i < 6; i++) {
-                if (mpeg4_decode_block(s, block[i], i, cbp&32, 1, 0) < 0)
-                    return -1;
-                cbp+=cbp;
-            }
-        } else {
-            for (i = 0; i < 6; i++) {
-                if (h263_decode_block(s, block[i], i, cbp&32) < 0)
-                    return -1;
-                cbp+=cbp;
-            }
+        for (i = 0; i < 6; i++) {
+            if (mpeg4_decode_block(s, block[i], i, cbp&32, 1, 0) < 0)
+                return -1;
+            cbp+=cbp;
         }
         goto end;
     }
 
     /* decode each block */
-    if (s->h263_pred) {
-        for (i = 0; i < 6; i++) {
-            if (mpeg4_decode_block(s, block[i], i, cbp&32, 0, 0) < 0)
-                return -1;
-            cbp+=cbp;
-        }
-    } else {
-        for (i = 0; i < 6; i++) {
-            if (h263_decode_block(s, block[i], i, cbp&32) < 0)
-                return -1;
-            cbp+=cbp;
-        }
+    for (i = 0; i < 6; i++) {
+        if (mpeg4_decode_block(s, block[i], i, cbp&32, 0, 0) < 0)
+            return -1;
+        cbp+=cbp;
     }
 end:
-    if(s->obmc){
-        if(s->pict_type == P_TYPE && s->mb_x+1<s->mb_width)
-            preview_obmc(s);
-    }
 
         /* per-MB end of slice check */
     if(s->codec_id==CODEC_ID_MPEG4){
@@ -3782,15 +4264,6 @@ end:
                 return SLICE_OK;
             return SLICE_END;
         }
-    }else{
-        int v= show_bits(&s->gb, 16);
-    
-        if(get_bits_count(&s->gb) + 16 > s->gb.size_in_bits){
-            v>>= get_bits_count(&s->gb) + 16 - s->gb.size_in_bits;
-        }
-
-        if(v==0)
-            return SLICE_END;
     }
 
     return SLICE_OK;     
@@ -3865,6 +4338,7 @@ static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
     int code, level, i, j, last, run;
     RLTable *rl = &rl_inter;
     const uint8_t *scan_table;
+    GetBitContext gb= s->gb;
 
     scan_table = s->intra_scantable.permutated;
     if (s->h263_aic && s->mb_intra) {
@@ -3878,7 +4352,7 @@ static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
         }
     } else if (s->mb_intra) {
         /* DC coef */
-        if(s->h263_rv10){
+        if(s->codec_id == CODEC_ID_RV10){
           if (s->rv10_version == 3 && s->pict_type == I_TYPE) {
             int component, diff;
             component = (n <= 3 ? 0 : n - 4 + 1);
@@ -3916,7 +4390,7 @@ static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
         s->block_last_index[n] = i - 1;
         return 0;
     }
-
+retry:
     for(;;) {
         code = get_vlc2(&s->gb, rl->vlc.table, TEX_VLC_BITS, 2);
         if (code < 0){
@@ -3939,7 +4413,7 @@ static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
                 run = get_bits(&s->gb, 6);
                 level = (int8_t)get_bits(&s->gb, 8);
                 if(level == -128){
-                    if (s->h263_rv10) {
+                    if (s->codec_id == CODEC_ID_RV10) {
                         /* XXX: should patch encoder too */
                         level = get_sbits(&s->gb, 12);
                     }else{
@@ -3957,6 +4431,14 @@ static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
         }
         i += run;
         if (i >= 64){
+            if(s->alt_inter_vlc && rl == &rl_inter && !s->mb_intra){
+                //looks like a hack but no, its the way its supposed to work ...
+                rl = &rl_intra_aic;
+                i = 0;
+                s->gb= gb;
+                memset(block, 0, sizeof(DCTELEM)*64);
+                goto retry;
+            }
             av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
             return -1;
         }
@@ -4338,9 +4820,6 @@ int h263_decode_picture_header(MpegEncContext *s)
     skip_bits1(&s->gb);        /* camera  off */
     skip_bits1(&s->gb);        /* freeze picture release off */
 
-    /* Reset GOB number */
-    s->gob_number = 0;
-        
     format = get_bits(&s->gb, 3);
     /*
         0    forbidden
@@ -4372,7 +4851,7 @@ int h263_decode_picture_header(MpegEncContext *s)
             av_log(s->avctx, AV_LOG_ERROR, "H263 PB frame not supported\n");
             return -1; /* not PB frame */
         }
-        s->qscale = get_bits(&s->gb, 5);
+        s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
         skip_bits1(&s->gb);    /* Continuous Presence Multipoint mode: off */
 
         s->width = width;
@@ -4391,29 +4870,25 @@ int h263_decode_picture_header(MpegEncContext *s)
             dprintf("ufep=1, format: %d\n", format);
             skip_bits(&s->gb,1); /* Custom PCF */
             s->umvplus = get_bits(&s->gb, 1); /* Unrestricted Motion Vector */
-            skip_bits1(&s->gb); /* Syntax-based Arithmetic Coding (SAC) */
-            s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
-            s->unrestricted_mv = s->umvplus || s->obmc;
-            s->h263_aic = get_bits1(&s->gb); /* Advanced Intra Coding (AIC) */
-           
-            if (get_bits1(&s->gb) != 0) {
-                av_log(s->avctx, AV_LOG_ERROR, "Deblocking Filter not supported\n");
-            }
             if (get_bits1(&s->gb) != 0) {
-                av_log(s->avctx, AV_LOG_ERROR, "Slice Structured not supported\n");
+                av_log(s->avctx, AV_LOG_ERROR, "Syntax-based Arithmetic Coding (SAC) not supported\n");
             }
+            s->obmc= get_bits1(&s->gb); /* Advanced prediction mode */
+            s->h263_aic = get_bits1(&s->gb); /* Advanced Intra Coding (AIC) */
+            s->loop_filter= get_bits1(&s->gb);
+            s->unrestricted_mv = s->umvplus || s->obmc || s->loop_filter;
+            
+            s->h263_slice_structured= get_bits1(&s->gb);
             if (get_bits1(&s->gb) != 0) {
                 av_log(s->avctx, AV_LOG_ERROR, "Reference Picture Selection not supported\n");
             }
             if (get_bits1(&s->gb) != 0) {
                 av_log(s->avctx, AV_LOG_ERROR, "Independent Segment Decoding not supported\n");
             }
-            if (get_bits1(&s->gb) != 0) {
-                av_log(s->avctx, AV_LOG_ERROR, "Alternative Inter VLC not supported\n");
-            }
-            if (get_bits1(&s->gb) != 0) {
-                av_log(s->avctx, AV_LOG_ERROR, "Modified Quantization not supported\n");
-            }
+            s->alt_inter_vlc= get_bits1(&s->gb);
+            s->modified_quant= get_bits1(&s->gb);
+            if(s->modified_quant)
+                s->chroma_qscale_table= ff_h263_chroma_qscale_table;
             
             skip_bits(&s->gb, 1); /* Prevent start code emulation */
 
@@ -4424,12 +4899,15 @@ int h263_decode_picture_header(MpegEncContext *s)
         }
             
         /* MPPTYPE */
-        s->pict_type = get_bits(&s->gb, 3) + I_TYPE;
-       if (s->pict_type == 8 && s->avctx->codec_tag == ff_get_fourcc("ZYGO"))
-           s->pict_type = I_TYPE;
-        if (s->pict_type != I_TYPE &&
-            s->pict_type != P_TYPE)
+        s->pict_type = get_bits(&s->gb, 3);
+        switch(s->pict_type){
+        case 0: s->pict_type= I_TYPE;break;
+        case 1: s->pict_type= P_TYPE;break;
+        case 3: s->pict_type= B_TYPE;break;
+        case 7: s->pict_type= I_TYPE;break; //ZYGO
+        default:
             return -1;
+        }
         skip_bits(&s->gb, 2);
         s->no_rounding = get_bits1(&s->gb);
         skip_bits(&s->gb, 4);
@@ -4472,6 +4950,14 @@ int h263_decode_picture_header(MpegEncContext *s)
                 if(get_bits1(&s->gb)==0) /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
                     skip_bits1(&s->gb); 
             }
+            if(s->h263_slice_structured){
+                if (get_bits1(&s->gb) != 0) {
+                    av_log(s->avctx, AV_LOG_ERROR, "rectangular slices not supported\n");
+                }
+                if (get_bits1(&s->gb) != 0) {
+                    av_log(s->avctx, AV_LOG_ERROR, "unordered slices not supported\n");
+                }
+            }
         }
             
         s->qscale = get_bits(&s->gb, 5);
@@ -4484,21 +4970,25 @@ int h263_decode_picture_header(MpegEncContext *s)
     
     if(s->h263_aic){
          s->y_dc_scale_table= 
-         s->c_dc_scale_table= h263_aic_dc_scale_table;
+         s->c_dc_scale_table= ff_aic_dc_scale_table;
     }else{
         s->y_dc_scale_table=
         s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
     }
 
      if(s->avctx->debug&FF_DEBUG_PICT_INFO){
-         av_log(s->avctx, AV_LOG_DEBUG, "qp:%d %c size:%d rnd:%d%s%s%s%s%s\n", 
+         av_log(s->avctx, AV_LOG_DEBUG, "qp:%d %c size:%d rnd:%d%s%s%s%s%s%s%s%s%s\n", 
          s->qscale, av_get_pict_type_char(s->pict_type),
          s->gb.size_in_bits, 1-s->no_rounding,
          s->obmc ? " AP" : "",
          s->umvplus ? " UMV" : "",
          s->h263_long_vectors ? " LONG" : "",
          s->h263_plus ? " +" : "",
-         s->h263_aic ? " AIC" : ""
+         s->h263_aic ? " AIC" : "",
+         s->alt_inter_vlc ? " AIV" : "",
+         s->modified_quant ? " MQ" : "",
+         s->loop_filter ? " LOOP" : "",
+         s->h263_slice_structured ? " SS" : ""
          ); 
      }
 #if 1
@@ -4827,9 +5317,10 @@ static int decode_vol_header(MpegEncContext *s, GetBitContext *gb){
             }
         }
         
-        s->progressive_sequence= get_bits1(gb)^1;
+        s->progressive_sequence= 
+        s->progressive_frame= get_bits1(gb)^1;
         if(!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO)) 
-            av_log(s->avctx, AV_LOG_ERROR, "OBMC not supported (very likely buggy encoder)\n");   /* OBMC Disable */
+            av_log(s->avctx, AV_LOG_INFO, "MPEG4 OBMC not supported (very likely buggy encoder)\n");   /* OBMC Disable */
         if (vo_ver_id == 1) {
             s->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
         } else {
@@ -5058,7 +5549,7 @@ static int decode_vop_header(MpegEncContext *s, GetBitContext *gb){
     if(s->partitioned_frame)
         s->decode_mb= mpeg4_decode_partitioned_mb;
     else
-        s->decode_mb= ff_h263_decode_mb;
+        s->decode_mb= ff_mpeg4_decode_mb;
 
     if(s->time_increment_resolution==0){
         s->time_increment_resolution=1;
@@ -5185,7 +5676,7 @@ static int decode_vop_header(MpegEncContext *s, GetBitContext *gb){
      }
 
      if (s->shape != BIN_ONLY_SHAPE) {
-         s->qscale = get_bits(gb, s->quant_precision);
+         s->chroma_qscale= s->qscale = get_bits(gb, s->quant_precision);
          if(s->qscale==0){
              av_log(s->avctx, AV_LOG_ERROR, "Error, header damaged or not MPEG4 header (qscale=0)\n");
              return -1; // makes no sense to continue, as there is nothing left from the image then
@@ -5379,7 +5870,7 @@ int intel_h263_decode_picture_header(MpegEncContext *s)
     /* skip unknown header garbage */
     skip_bits(&s->gb, 41);
 
-    s->qscale = get_bits(&s->gb, 5);
+    s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
     skip_bits1(&s->gb);        /* Continuous Presence Multipoint mode: off */
 
     /* PEI */
@@ -5453,7 +5944,7 @@ int flv_h263_decode_picture_header(MpegEncContext *s)
     if (s->pict_type > P_TYPE)
         s->pict_type = P_TYPE;
     skip_bits1(&s->gb);        /* deblocking flag */
-    s->qscale = get_bits(&s->gb, 5);
+    s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
 
     s->h263_plus = 0;