diff mpeg12.c @ 1177:fea03d2c4946 libavcodec

simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture more direct use of the new mb_type stuff instead of codec specific stuff runtime mb_type debug output h264/h263 variants/mpeg1/2/4 error concealment /resilience for mpeg1/2 various minor optimizations
author michaelni
date Thu, 10 Apr 2003 13:18:38 +0000
parents 0e3c0c4a7b3d
children 4c2339f3863a
line wrap: on
line diff
--- a/mpeg12.c	Thu Apr 10 08:47:08 2003 +0000
+++ b/mpeg12.c	Thu Apr 10 13:18:38 2003 +0000
@@ -29,11 +29,6 @@
 
 #include "mpeg12data.h"
 
-#if 1
-#define PRINT_QP(a, b) {}
-#else
-#define PRINT_QP(a, b) printf(a, b)
-#endif
 
 /* Start codes. */
 #define SEQ_END_CODE		0x000001b7
@@ -833,10 +828,10 @@
                  &mbPatTable[0][1], 2, 1,
                  &mbPatTable[0][0], 2, 1);
         
-        init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 32, 
+        init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7, 
                  &table_mb_ptype[0][1], 2, 1,
                  &table_mb_ptype[0][0], 2, 1);
-        init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 32, 
+        init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11, 
                  &table_mb_btype[0][1], 2, 1,
                  &table_mb_btype[0][0], 2, 1);
         init_rl(&rl_mpeg1);
@@ -887,6 +882,11 @@
     assert(s->mb_skiped==0);
 
     if (s->mb_skip_run-- != 0) {
+        if(s->pict_type == I_TYPE){
+            fprintf(stderr, "skiped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
+            return -1;
+        }
+    
         /* skip mb */
         s->mb_intra = 0;
         for(i=0;i<6;i++)
@@ -899,6 +899,7 @@
             s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
             s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
             s->mb_skiped = 1;
+            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
         } else {
             /* if B type, reuse previous vectors and directions */
             s->mv[0][0][0] = s->last_mv[0][0][0];
@@ -906,6 +907,10 @@
             s->mv[1][0][0] = s->last_mv[1][0][0];
             s->mv[1][0][1] = s->last_mv[1][0][1];
 
+            s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= 
+                s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1] | MB_TYPE_SKIP;
+//            assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
+
             if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0) 
                 s->mb_skiped = 1;
         }
@@ -919,9 +924,9 @@
         if (get_bits1(&s->gb) == 0) {
             if (get_bits1(&s->gb) == 0)
                 return -1;
-            mb_type = MB_QUANT | MB_INTRA;
+            mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
         } else {
-            mb_type = MB_INTRA;
+            mb_type = MB_TYPE_INTRA;
         }
         break;
     case P_TYPE:
@@ -930,6 +935,7 @@
             fprintf(stderr, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
             return -1;
         }
+        mb_type = ptype2mb_type[ mb_type ];
         break;
     case B_TYPE:
         mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
@@ -937,216 +943,236 @@
             fprintf(stderr, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
             return -1;
         }
+        mb_type = btype2mb_type[ mb_type ];
         break;
     }
     dprintf("mb_type=%x\n", mb_type);
-    motion_type = 0; /* avoid warning */
-    if (mb_type & (MB_FOR|MB_BACK)) {
-        /* get additionnal motion vector type */
-        if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct) 
-            motion_type = MT_FRAME;
-        else
-            motion_type = get_bits(&s->gb, 2);
-    }
-    /* compute dct type */
-    if (s->picture_structure == PICT_FRAME && 
-        !s->frame_pred_frame_dct &&
-        (mb_type & (MB_PAT | MB_INTRA))) {
-        s->interlaced_dct = get_bits1(&s->gb);
-#ifdef DEBUG
-        if (s->interlaced_dct)
-            printf("interlaced_dct\n");
-#endif
-    } else {
-        s->interlaced_dct = 0; /* frame based */
-    }
+//    motion_type = 0; /* avoid warning */
+    if (IS_INTRA(mb_type)) {
+        /* compute dct type */
+        if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
+            !s->frame_pred_frame_dct) {
+            s->interlaced_dct = get_bits1(&s->gb);
+        }
 
-    if (mb_type & MB_QUANT) {
-        s->qscale = get_qscale(s);
-    }
-    if (mb_type & MB_INTRA) {
+        if (IS_QUANT(mb_type))
+            s->qscale = get_qscale(s);
+        
         if (s->concealment_motion_vectors) {
             /* just parse them */
             if (s->picture_structure != PICT_FRAME) 
                 skip_bits1(&s->gb); /* field select */
             mpeg_decode_motion(s, s->mpeg_f_code[0][0], 0);
             mpeg_decode_motion(s, s->mpeg_f_code[0][1], 0);
+            skip_bits1(&s->gb); /* marker */
         }
         s->mb_intra = 1;
-        cbp = 0x3f;
         memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
-    } else {
-        s->mb_intra = 0;
-        cbp = 0;
-    }
-    /* special case of implicit zero motion vector */
-    if (s->pict_type == P_TYPE && !(mb_type & MB_FOR)) {
-        s->mv_dir = MV_DIR_FORWARD;
-        s->mv_type = MV_TYPE_16X16;
-        s->last_mv[0][0][0] = 0;
-        s->last_mv[0][0][1] = 0;
-        s->last_mv[0][1][0] = 0;
-        s->last_mv[0][1][1] = 0;
-        s->mv[0][0][0] = 0;
-        s->mv[0][0][1] = 0;
-    } else if (mb_type & (MB_FOR | MB_BACK)) {
-        /* motion vectors */
-        s->mv_dir = 0;
-        for(i=0;i<2;i++) {
-            if (mb_type & (MB_FOR >> i)) {
-                s->mv_dir |= (MV_DIR_FORWARD >> i);
-                dprintf("motion_type=%d\n", motion_type);
-                switch(motion_type) {
-                case MT_FRAME: /* or MT_16X8 */
-                    if (s->picture_structure == PICT_FRAME) {
-                        /* MT_FRAME */
-                        s->mv_type = MV_TYPE_16X16;
-                        for(k=0;k<2;k++) {
-                            val = mpeg_decode_motion(s, s->mpeg_f_code[i][k], 
-                                                     s->last_mv[i][0][k]);
-                            s->last_mv[i][0][k] = val;
-                            s->last_mv[i][1][k] = val;
-                            /* full_pel: only for mpeg1 */
-                            if (s->full_pel[i])
-                                val = val << 1;
-                            s->mv[i][0][k] = val;
-                            dprintf("mv%d: %d\n", k, val);
-                        }
-                    } else {
-                        /* MT_16X8 */
-                        s->mv_type = MV_TYPE_16X8;
-                        for(j=0;j<2;j++) {
-                            s->field_select[i][j] = get_bits1(&s->gb);
-                            for(k=0;k<2;k++) {
-                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
-                                                         s->last_mv[i][j][k]);
-                                s->last_mv[i][j][k] = val;
-                                s->mv[i][j][k] = val;
-                            }
-                        }
-                    }
-                    break;
-                case MT_FIELD:
-                    s->mv_type = MV_TYPE_FIELD;
-                    if (s->picture_structure == PICT_FRAME) {
-                        for(j=0;j<2;j++) {
-                            s->field_select[i][j] = get_bits1(&s->gb);
-                            val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
-                                                     s->last_mv[i][j][0]);
-                            s->last_mv[i][j][0] = val;
-                            s->mv[i][j][0] = val;
-                            dprintf("fmx=%d\n", val);
-                            val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
-                                                     s->last_mv[i][j][1] >> 1);
-                            s->last_mv[i][j][1] = val << 1;
-                            s->mv[i][j][1] = val;
-                            dprintf("fmy=%d\n", val);
-                        }
-                    } else {
-                        s->field_select[i][0] = get_bits1(&s->gb);
-                        for(k=0;k<2;k++) {
-                            val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
-                                                     s->last_mv[i][0][k]);
-                            s->last_mv[i][0][k] = val;
-                            s->last_mv[i][1][k] = val;
-                            s->mv[i][0][k] = val;
-                        }
-                    }
-                    break;
-                case MT_DMV:
-                    {
-                        int dmx, dmy, mx, my, m;
 
-                        mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0], 
-                                                s->last_mv[i][0][0]);
-                        s->last_mv[i][0][0] = mx;
-                        s->last_mv[i][1][0] = mx;
-                        dmx = get_dmv(s);
-                        my = mpeg_decode_motion(s, s->mpeg_f_code[i][1], 
-                                                s->last_mv[i][0][1] >> 1);
-                        dmy = get_dmv(s);
-                        s->mv_type = MV_TYPE_DMV;
-                        /* XXX: totally broken */
-                        if (s->picture_structure == PICT_FRAME) {
-                            s->last_mv[i][0][1] = my << 1;
-                            s->last_mv[i][1][1] = my << 1;
-
-                            m = s->top_field_first ? 1 : 3;
-                            /* top -> top pred */
-                            s->mv[i][0][0] = mx; 
-                            s->mv[i][0][1] = my << 1;
-                            s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
-                            s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
-                            m = 4 - m;
-                            s->mv[i][2][0] = mx;
-                            s->mv[i][2][1] = my << 1;
-                            s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
-                            s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
-                        } else {
-                            s->last_mv[i][0][1] = my;
-                            s->last_mv[i][1][1] = my;
-                            s->mv[i][0][0] = mx;
-                            s->mv[i][0][1] = my;
-                            s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx;
-                            s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1 
-                                /* + 2 * cur_field */;
-                        }
-                    }
-                    break;
-                }
-            }
-        }
-    }
-
-    if ((mb_type & MB_INTRA) && s->concealment_motion_vectors) {
-        skip_bits1(&s->gb); /* marker */
-    }
-    
-    if (mb_type & MB_PAT) {
-        cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
-        if (cbp < 0){
-            fprintf(stderr, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
-            return -1;
-        }
-        cbp++;
-    }
-    dprintf("cbp=%x\n", cbp);
-
-    if (s->mpeg2) {
-        if (s->mb_intra) {
+        if (s->mpeg2) {
             for(i=0;i<6;i++) {
                 if (mpeg2_decode_block_intra(s, block[i], i) < 0)
                     return -1;
             }
         } else {
             for(i=0;i<6;i++) {
-                if (cbp & 32) {
-                    if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
-                        return -1;
-                } else {
-                    s->block_last_index[i] = -1;
-                }
-                cbp+=cbp;
+                if (mpeg1_decode_block_intra(s, block[i], i) < 0)
+                    return -1;
             }
         }
     } else {
-        if (s->mb_intra) {
-            for(i=0;i<6;i++) {
-                if (mpeg1_decode_block_intra(s, block[i], i) < 0)
-                    return -1;
+        if (mb_type & MB_TYPE_ZERO_MV){
+            assert(mb_type & MB_TYPE_PAT);
+
+            /* compute dct type */
+            if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
+                !s->frame_pred_frame_dct) {
+                s->interlaced_dct = get_bits1(&s->gb);
+            }
+
+            if (IS_QUANT(mb_type))
+                s->qscale = get_qscale(s);
+
+            s->mv_dir = MV_DIR_FORWARD;
+            s->mv_type = MV_TYPE_16X16;
+            s->last_mv[0][0][0] = 0;
+            s->last_mv[0][0][1] = 0;
+            s->last_mv[0][1][0] = 0;
+            s->last_mv[0][1][1] = 0;
+            s->mv[0][0][0] = 0;
+            s->mv[0][0][1] = 0;
+        }else{
+            assert(mb_type & MB_TYPE_L0L1);
+//FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
+            /* get additionnal motion vector type */
+            if (s->frame_pred_frame_dct) 
+                motion_type = MT_FRAME;
+            else{
+                motion_type = get_bits(&s->gb, 2);
+            }
+
+            /* compute dct type */
+            if (s->picture_structure == PICT_FRAME && //FIXME add a interlaced_dct coded var?
+                !s->frame_pred_frame_dct && IS_PAT(mb_type)) {
+                s->interlaced_dct = get_bits1(&s->gb);
+            }
+
+            if (IS_QUANT(mb_type))
+                s->qscale = get_qscale(s);
+
+            /* motion vectors */
+            s->mv_dir = 0;
+            for(i=0;i<2;i++) {
+                if (USES_LIST(mb_type, i)) {
+                    s->mv_dir |= (MV_DIR_FORWARD >> i);
+                    dprintf("motion_type=%d\n", motion_type);
+                    switch(motion_type) {
+                    case MT_FRAME: /* or MT_16X8 */
+                        if (s->picture_structure == PICT_FRAME) {
+                            /* MT_FRAME */
+                            mb_type |= MB_TYPE_16x16; 
+                            s->mv_type = MV_TYPE_16X16;
+                            s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] = 
+                                mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
+                            s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] = 
+                                mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
+                            /* full_pel: only for mpeg1 */
+                            if (s->full_pel[i]){
+                                s->mv[i][0][0] <<= 1;
+                                s->mv[i][0][1] <<= 1;
+                            }
+                        } else {
+                            /* MT_16X8 */
+                            mb_type |= MB_TYPE_16x8; 
+                            s->mv_type = MV_TYPE_16X8;
+                            for(j=0;j<2;j++) {
+                                s->field_select[i][j] = get_bits1(&s->gb);
+                                for(k=0;k<2;k++) {
+                                    val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
+                                                             s->last_mv[i][j][k]);
+                                    s->last_mv[i][j][k] = val;
+                                    s->mv[i][j][k] = val;
+                                }
+                            }
+                        }
+                        break;
+                    case MT_FIELD:
+                        s->mv_type = MV_TYPE_FIELD;
+                        if (s->picture_structure == PICT_FRAME) {
+                            mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED; 
+                            for(j=0;j<2;j++) {
+                                s->field_select[i][j] = get_bits1(&s->gb);
+                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
+                                                         s->last_mv[i][j][0]);
+                                s->last_mv[i][j][0] = val;
+                                s->mv[i][j][0] = val;
+                                dprintf("fmx=%d\n", val);
+                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
+                                                         s->last_mv[i][j][1] >> 1);
+                                s->last_mv[i][j][1] = val << 1;
+                                s->mv[i][j][1] = val;
+                                dprintf("fmy=%d\n", val);
+                            }
+                        } else {
+                            mb_type |= MB_TYPE_16x16; 
+                            s->field_select[i][0] = get_bits1(&s->gb);
+                            for(k=0;k<2;k++) {
+                                val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
+                                                         s->last_mv[i][0][k]);
+                                s->last_mv[i][0][k] = val;
+                                s->last_mv[i][1][k] = val;
+                                s->mv[i][0][k] = val;
+                            }
+                        }
+                        break;
+                    case MT_DMV:
+                        {
+                            int dmx, dmy, mx, my, m;
+
+                            mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0], 
+                                                    s->last_mv[i][0][0]);
+                            s->last_mv[i][0][0] = mx;
+                            s->last_mv[i][1][0] = mx;
+                            dmx = get_dmv(s);
+                            my = mpeg_decode_motion(s, s->mpeg_f_code[i][1], 
+                                                    s->last_mv[i][0][1] >> 1);
+                            dmy = get_dmv(s);
+                            s->mv_type = MV_TYPE_DMV;
+                            /* XXX: totally broken */
+                            if (s->picture_structure == PICT_FRAME) {
+                                mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED; 
+
+                                s->last_mv[i][0][1] = my << 1;
+                                s->last_mv[i][1][1] = my << 1;
+
+                                m = s->top_field_first ? 1 : 3;
+                                /* top -> top pred */
+                                s->mv[i][0][0] = mx; 
+                                s->mv[i][0][1] = my << 1;
+                                s->mv[i][1][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
+                                s->mv[i][1][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
+                                m = 4 - m;
+                                s->mv[i][2][0] = mx;
+                                s->mv[i][2][1] = my << 1;
+                                s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
+                                s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
+                            } else {
+                                mb_type |= MB_TYPE_16x16;
+
+                                s->last_mv[i][0][1] = my;
+                                s->last_mv[i][1][1] = my;
+                                s->mv[i][0][0] = mx;
+                                s->mv[i][0][1] = my;
+                                s->mv[i][1][0] = ((mx + (mx > 0)) >> 1) + dmx;
+                                s->mv[i][1][1] = ((my + (my > 0)) >> 1) + dmy - 1 
+                                    /* + 2 * cur_field */;
+                            }
+                        }
+                        break;
+                    }
+                }
+            }
+        }
+        
+        s->mb_intra = 0;
+
+        if (IS_PAT(mb_type)) {
+            cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
+            if (cbp < 0){
+                fprintf(stderr, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
+                return -1;
+            }
+            cbp++;
+
+            if (s->mpeg2) {
+                for(i=0;i<6;i++) {
+                    if (cbp & 32) {
+                        if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
+                            return -1;
+                    } else {
+                        s->block_last_index[i] = -1;
+                    }
+                    cbp+=cbp;
+                }
+            } else {
+                for(i=0;i<6;i++) {
+                    if (cbp & 32) {
+                        if (mpeg1_decode_block_inter(s, block[i], i) < 0)
+                            return -1;
+                    } else {
+                        s->block_last_index[i] = -1;
+                    }
+                    cbp+=cbp;
+                }
             }
         }else{
-            for(i=0;i<6;i++) {
-                if (cbp & 32) {
-                    if (mpeg1_decode_block_inter(s, block[i], i) < 0)
-                        return -1;
-                } else {
-                    s->block_last_index[i] = -1;
-                }
-                cbp+=cbp;
-            }
+            for(i=0;i<6;i++)
+                s->block_last_index[i] = -1;
         }
     }
+
+    s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
+
     return 0;
 }
 
@@ -1156,12 +1182,13 @@
     int code, sign, val, m, l, shift;
 
     code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
+    if (code == 0) {
+        return pred;
+    }
     if (code < 0) {
         return 0xffff;
     }
-    if (code == 0) {
-        return pred;
-    }
+
     sign = get_bits1(&s->gb);
     shift = fcode - 1;
     val = (code - 1) << shift;
@@ -1173,7 +1200,7 @@
     val += pred;
     
     /* modulo decoding */
-    l = (1 << shift) * 16;
+    l = 1 << (shift+4);
     m = 2 * l;
     if (val < -l) {
         val += m;
@@ -1713,7 +1740,7 @@
         s->first_field=0;
     else{
         s->first_field ^= 1;
-        memset(s->mbskip_table, 0, s->mb_width*s->mb_height);
+        memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
     }
     
     if(s->alternate_scan){
@@ -1766,6 +1793,7 @@
     }
 }
 
+#define DECODE_SLICE_MB_ADDR_ERROR -3 //we faild decoding the mb_x/y info
 #define DECODE_SLICE_FATAL_ERROR -2
 #define DECODE_SLICE_ERROR -1
 #define DECODE_SLICE_OK 0
@@ -1791,16 +1819,20 @@
     start_code = (start_code - 1) & 0xff;
     if (start_code >= s->mb_height){
         fprintf(stderr, "slice below image (%d >= %d)\n", start_code, s->mb_height);
-        return DECODE_SLICE_ERROR;
+        return DECODE_SLICE_MB_ADDR_ERROR;
     }
     
     ff_mpeg1_clean_buffers(s);
+    s->interlaced_dct = 0;
         
     /* start frame decoding */
     if (s->first_slice) {
       if(s->first_field || s->picture_structure==PICT_FRAME){
         if(MPV_frame_start(s, avctx) < 0)
             return DECODE_SLICE_FATAL_ERROR;
+
+        ff_er_frame_start(s);
+
         /* first check if we must repeat the frame */
         s->current_picture.repeat_pict = 0;
 
@@ -1839,16 +1871,24 @@
     init_get_bits(&s->gb, buf, buf_size*8);
 
     s->qscale = get_qscale(s);
+    if(s->qscale == 0){
+        fprintf(stderr, "qscale == 0\n");
+        return DECODE_SLICE_MB_ADDR_ERROR;
+    }
+    
     /* extra slice info */
     while (get_bits1(&s->gb) != 0) {
         skip_bits(&s->gb, 8);
     }
+    
+    s->mb_x=0;
 
-    s->mb_x=0;
     for(;;) {
         int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
-        if (code < 0)
-            return -1; /* error = end of slice, but empty slice is bad or?*/
+        if (code < 0){
+            fprintf(stderr, "first mb_incr damaged\n");
+            return DECODE_SLICE_MB_ADDR_ERROR;
+        }
         if (code >= 33) {
             if (code == 33) {
                 s->mb_x += 33;
@@ -1860,16 +1900,42 @@
         }
     }
     
-    s->mb_y = start_code;
+    s->resync_mb_x= s->mb_x;
+    s->resync_mb_y= s->mb_y = start_code;
     s->mb_skip_run= 0;
 
     for(;;) {
 	s->dsp.clear_blocks(s->block[0]);
-        
+
         ret = mpeg_decode_mb(s, s->block);
+
         dprintf("ret=%d\n", ret);
         if (ret < 0)
             return -1;
+            
+        if(s->motion_val && s->pict_type != B_TYPE){ //note motion_val is normally NULL unless we want to extract the MVs
+            const int wrap = s->block_wrap[0];
+            const int xy = s->mb_x*2 + 1 + (s->mb_y*2 +1)*wrap;
+            int motion_x, motion_y;
+
+            if (s->mb_intra || s->mv_type == MV_TYPE_16X16) {
+                motion_x = s->mv[0][0][0];
+                motion_y = s->mv[0][0][1];
+            } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
+                int i;
+                motion_x = s->mv[0][0][0] + s->mv[0][1][0];
+                motion_y = s->mv[0][0][1] + s->mv[0][1][1];
+                motion_x = (motion_x>>1) | (motion_x&1);
+            }
+            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;
+        }
         
         MPV_decode_mb(s, s->block);
 
@@ -1884,9 +1950,7 @@
 
             s->mb_x = 0;
             s->mb_y++;
-            PRINT_QP("%s", "\n");
         }
-        PRINT_QP("%2d", s->qscale);
 
         /* skip mb handling */
         if (s->mb_skip_run == -1) {
@@ -1894,8 +1958,14 @@
             s->mb_skip_run = 0;
             for(;;) {
                 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
-                if (code < 0)
+                if (code < 0){
+                    align_get_bits(&s->gb);
+                    if(s->mb_skip_run != 0 || show_bits(&s->gb, 24) != 1){
+                        fprintf(stderr, "slice end missmatch\n");
+                        return -1;
+                    }
                     goto eos; /* error = end of slice */
+                }
                 if (code >= 33) {
                     if (code == 33) {
                         s->mb_skip_run += 33;
@@ -1913,8 +1983,11 @@
         }
     }
 eos: //end of slice
+//printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
+    ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
+
+    emms_c();
     
-    emms_c();
 //intf("%d %d %d %d\n", s->mb_y, s->mb_height, s->pict_type, s->picture_number);
     /* end of slice reached */
     if (s->mb_y<<field_pic == s->mb_height && !s->first_field) {
@@ -1922,11 +1995,13 @@
 
         if(s->mpeg2)
             s->qscale >>=1;
+        ff_er_frame_end(s);
 
         MPV_frame_end(s);
 
         if (s->pict_type == B_TYPE || s->low_delay) {
             *pict= *(AVFrame*)&s->current_picture;
+            ff_print_debug_info(s, s->current_picture_ptr);
         } else {
             s->picture_number++;
             /* latency of 1 frame for I and P frames */
@@ -1935,6 +2010,7 @@
                 return DECODE_SLICE_OK;
             } else {
                 *pict= *(AVFrame*)&s->last_picture;
+                 ff_print_debug_info(s, s->last_picture_ptr);
             }
         }
         return DECODE_SLICE_EOP;
@@ -2194,7 +2270,10 @@
                         if (ret == DECODE_SLICE_EOP) {
                             *data_size = sizeof(AVPicture);
                             goto the_end;
-                        }else if(ret<0){
+                        }else if(ret < 0){
+                            if(ret == DECODE_SLICE_ERROR)
+                                ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
+                                
                             fprintf(stderr,"Error while decoding slice\n");
 			    if(ret==DECODE_SLICE_FATAL_ERROR) return -1;
                         }