]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mpeg12.c
fraps: check overread per sample instead of per line
[ffmpeg] / libavcodec / mpeg12.c
index 970d1d57034b3c44699abd1c05303b85d9f31e95..3bc857dc5f59b321ea00604e34bcff356d371ab7 100644 (file)
 #define MB_PTYPE_VLC_BITS 6
 #define MB_BTYPE_VLC_BITS 6
 
-static inline int mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
-static inline int mpeg1_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
-static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
-static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
-static inline int mpeg2_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
-static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
-static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
-static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
-static void exchange_uv(MpegEncContext *s);
+static VLC mv_vlc;
 
-uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
+/* as H.263, but only 17 codes */
+static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
+{
+    int code, sign, val, shift;
 
+    code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
+    if (code == 0) {
+        return pred;
+    }
+    if (code < 0) {
+        return 0xffff;
+    }
 
-#define INIT_2D_VLC_RL(rl, static_size)\
-{\
-    static RL_VLC_ELEM rl_vlc_table[static_size];\
-    INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
-                    &rl.table_vlc[0][1], 4, 2,\
-                    &rl.table_vlc[0][0], 4, 2, static_size);\
-\
-    rl.rl_vlc[0] = rl_vlc_table;\
-    init_2d_vlc_rl(&rl);\
+    sign  = get_bits1(&s->gb);
+    shift = fcode - 1;
+    val   = code;
+    if (shift) {
+        val  = (val - 1) << shift;
+        val |= get_bits(&s->gb, shift);
+        val++;
+    }
+    if (sign)
+        val = -val;
+    val += pred;
+
+    /* modulo decoding */
+    return sign_extend(val, 5 + shift);
 }
 
-static void init_2d_vlc_rl(RLTable *rl)
+static inline int mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
 {
-    int i;
+    int level, dc, diff, i, j, run;
+    int component;
+    RLTable *rl = &ff_rl_mpeg1;
+    uint8_t * const scantable    = s->intra_scantable.permutated;
+    const uint16_t *quant_matrix = s->intra_matrix;
+    const int qscale             = s->qscale;
 
-    for (i = 0; i < rl->vlc.table_size; i++) {
-        int code = rl->vlc.table[i][0];
-        int len  = rl->vlc.table[i][1];
-        int level, run;
+    /* DC coefficient */
+    component = (n <= 3 ? 0 : n - 4 + 1);
+    diff = decode_dc(&s->gb, component);
+    if (diff >= 0xffff)
+        return -1;
+    dc  = s->last_dc[component];
+    dc += diff;
+    s->last_dc[component] = dc;
+    block[0] = dc * quant_matrix[0];
+    av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
+    i = 0;
+    {
+        OPEN_READER(re, &s->gb);
+        /* now quantify & encode AC coefficients */
+        for (;;) {
+            UPDATE_CACHE(re, &s->gb);
+            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
 
-        if (len == 0) { // illegal code
-            run   = 65;
-            level = MAX_LEVEL;
-        } else if (len<0) { //more bits needed
-            run   = 0;
-            level = code;
-        } else {
-            if (code == rl->n) { //esc
-                run   = 65;
-                level = 0;
-            } else if (code == rl->n+1) { //eob
-                run   = 0;
-                level = 127;
+            if (level == 127) {
+                break;
+            } else if (level != 0) {
+                i += run;
+                j = scantable[i];
+                level = (level * qscale * quant_matrix[j]) >> 4;
+                level = (level - 1) | 1;
+                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+                LAST_SKIP_BITS(re, &s->gb, 1);
             } else {
-                run   = rl->table_run  [code] + 1;
-                level = rl->table_level[code];
+                /* escape */
+                run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
+                UPDATE_CACHE(re, &s->gb);
+                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
+                if (level == -128) {
+                    level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
+                } else if (level == 0) {
+                    level = SHOW_UBITS(re, &s->gb, 8)      ; LAST_SKIP_BITS(re, &s->gb, 8);
+                }
+                i += run;
+                j = scantable[i];
+                if (level < 0) {
+                    level = -level;
+                    level = (level * qscale * quant_matrix[j]) >> 4;
+                    level = (level - 1) | 1;
+                    level = -level;
+                } else {
+                    level = (level * qscale * quant_matrix[j]) >> 4;
+                    level = (level - 1) | 1;
+                }
+            }
+            if (i > 63) {
+                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
+                return -1;
             }
+
+            block[j] = level;
         }
-        rl->rl_vlc[0][i].len   = len;
-        rl->rl_vlc[0][i].level = level;
-        rl->rl_vlc[0][i].run   = run;
+        CLOSE_READER(re, &s->gb);
     }
+    s->block_last_index[n] = i;
+   return 0;
 }
 
-void ff_mpeg12_common_init(MpegEncContext *s)
+int ff_mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
 {
-
-    s->y_dc_scale_table =
-    s->c_dc_scale_table = ff_mpeg2_dc_scale_table[s->intra_dc_precision];
-
+    return mpeg1_decode_block_intra(s, block, n);
 }
 
-void ff_mpeg1_clean_buffers(MpegEncContext *s)
+static inline int mpeg1_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
 {
-    s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
-    s->last_dc[1] = s->last_dc[0];
-    s->last_dc[2] = s->last_dc[0];
-    memset(s->last_mv, 0, sizeof(s->last_mv));
-}
-
+    int level, i, j, run;
+    RLTable *rl = &ff_rl_mpeg1;
+    uint8_t * const scantable    = s->intra_scantable.permutated;
+    const uint16_t *quant_matrix = s->inter_matrix;
+    const int qscale             = s->qscale;
 
-/******************************************/
-/* decoding */
+    {
+        OPEN_READER(re, &s->gb);
+        i = -1;
+        // special case for first coefficient, no need to add second VLC table
+        UPDATE_CACHE(re, &s->gb);
+        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
+            level = (3 * qscale * quant_matrix[0]) >> 5;
+            level = (level - 1) | 1;
+            if (GET_CACHE(re, &s->gb) & 0x40000000)
+                level = -level;
+            block[0] = level;
+            i++;
+            SKIP_BITS(re, &s->gb, 2);
+            if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
+                goto end;
+        }
+        /* now quantify & encode AC coefficients */
+        for (;;) {
+            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
 
-VLC ff_dc_lum_vlc;
-VLC ff_dc_chroma_vlc;
+            if (level != 0) {
+                i += run;
+                j = scantable[i];
+                level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
+                level = (level - 1) | 1;
+                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+                SKIP_BITS(re, &s->gb, 1);
+            } else {
+                /* escape */
+                run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
+                UPDATE_CACHE(re, &s->gb);
+                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
+                if (level == -128) {
+                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
+                } else if (level == 0) {
+                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
+                }
+                i += run;
+                j = scantable[i];
+                if (level < 0) {
+                    level = -level;
+                    level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
+                    level = (level - 1) | 1;
+                    level = -level;
+                } else {
+                    level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
+                    level = (level - 1) | 1;
+                }
+            }
+            if (i > 63) {
+                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
+                return -1;
+            }
 
-static VLC mv_vlc;
-static VLC mbincr_vlc;
-static VLC mb_ptype_vlc;
-static VLC mb_btype_vlc;
-static VLC mb_pat_vlc;
+            block[j] = level;
+            if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
+                break;
+            UPDATE_CACHE(re, &s->gb);
+        }
+end:
+        LAST_SKIP_BITS(re, &s->gb, 2);
+        CLOSE_READER(re, &s->gb);
+    }
+    s->block_last_index[n] = i;
+    return 0;
+}
 
-av_cold void ff_mpeg12_init_vlcs(void)
+static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
 {
-    static int done = 0;
+    int level, i, j, run;
+    RLTable *rl = &ff_rl_mpeg1;
+    uint8_t * const scantable = s->intra_scantable.permutated;
+    const int qscale          = s->qscale;
 
-    if (!done) {
-        done = 1;
+    {
+        OPEN_READER(re, &s->gb);
+        i = -1;
+        // special case for first coefficient, no need to add second VLC table
+        UPDATE_CACHE(re, &s->gb);
+        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
+            level = (3 * qscale) >> 1;
+            level = (level - 1) | 1;
+            if (GET_CACHE(re, &s->gb) & 0x40000000)
+                level = -level;
+            block[0] = level;
+            i++;
+            SKIP_BITS(re, &s->gb, 2);
+            if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
+                goto end;
+        }
 
-        INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
-                        ff_mpeg12_vlc_dc_lum_bits, 1, 1,
-                        ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
-        INIT_VLC_STATIC(&ff_dc_chroma_vlc,  DC_VLC_BITS, 12,
-                        ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
-                        ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
-        INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
-                        &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
-                        &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
-        INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
-                        &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
-                        &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
-        INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
-                        &ff_mpeg12_mbPatTable[0][1], 2, 1,
-                        &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
+        /* now quantify & encode AC coefficients */
+        for (;;) {
+            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
 
-        INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
-                        &table_mb_ptype[0][1], 2, 1,
-                        &table_mb_ptype[0][0], 2, 1, 64);
-        INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
-                        &table_mb_btype[0][1], 2, 1,
-                        &table_mb_btype[0][0], 2, 1, 64);
-        init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
-        init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
+            if (level != 0) {
+                i += run;
+                j = scantable[i];
+                level = ((level * 2 + 1) * qscale) >> 1;
+                level = (level - 1) | 1;
+                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+                SKIP_BITS(re, &s->gb, 1);
+            } else {
+                /* escape */
+                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
+                UPDATE_CACHE(re, &s->gb);
+                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
+                if (level == -128) {
+                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
+                } else if (level == 0) {
+                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
+                }
+                i += run;
+                j = scantable[i];
+                if (level < 0) {
+                    level = -level;
+                    level = ((level * 2 + 1) * qscale) >> 1;
+                    level = (level - 1) | 1;
+                    level = -level;
+                } else {
+                    level = ((level * 2 + 1) * qscale) >> 1;
+                    level = (level - 1) | 1;
+                }
+            }
 
-        INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
-        INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
+            block[j] = level;
+            if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
+                break;
+            UPDATE_CACHE(re, &s->gb);
+        }
+end:
+        LAST_SKIP_BITS(re, &s->gb, 2);
+        CLOSE_READER(re, &s->gb);
     }
+    s->block_last_index[n] = i;
+    return 0;
 }
 
-static inline int get_dmv(MpegEncContext *s)
-{
-    if (get_bits1(&s->gb))
-        return 1 - (get_bits1(&s->gb) << 1);
-    else
-        return 0;
-}
 
-static inline int get_qscale(MpegEncContext *s)
+static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n)
 {
-    int qscale = get_bits(&s->gb, 5);
-    if (s->q_scale_type) {
-        return non_linear_qscale[qscale];
-    } else {
-        return qscale << 1;
-    }
-}
+    int level, i, j, run;
+    RLTable *rl = &ff_rl_mpeg1;
+    uint8_t * const scantable = s->intra_scantable.permutated;
+    const uint16_t *quant_matrix;
+    const int qscale = s->qscale;
+    int mismatch;
 
-/* motion type (for MPEG-2) */
-#define MT_FIELD 1
-#define MT_FRAME 2
-#define MT_16X8  2
-#define MT_DMV   3
+    mismatch = 1;
 
-static int mpeg_decode_mb(MpegEncContext *s, DCTELEM block[12][64])
-{
-    int i, j, k, cbp, val, mb_type, motion_type;
-    const int mb_block_count = 4 + (1 << s->chroma_format);
+    {
+        OPEN_READER(re, &s->gb);
+        i = -1;
+        if (n < 4)
+            quant_matrix = s->inter_matrix;
+        else
+            quant_matrix = s->chroma_inter_matrix;
 
-    av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
+        // special case for first coefficient, no need to add second VLC table
+        UPDATE_CACHE(re, &s->gb);
+        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
+            level= (3 * qscale * quant_matrix[0]) >> 5;
+            if (GET_CACHE(re, &s->gb) & 0x40000000)
+                level = -level;
+            block[0]  = level;
+            mismatch ^= level;
+            i++;
+            SKIP_BITS(re, &s->gb, 2);
+            if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
+                goto end;
+        }
 
-    assert(s->mb_skipped == 0);
+        /* now quantify & encode AC coefficients */
+        for (;;) {
+            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
 
-    if (s->mb_skip_run-- != 0) {
-        if (s->pict_type == AV_PICTURE_TYPE_P) {
-            s->mb_skipped = 1;
-            s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
-        } else {
-            int mb_type;
+            if (level != 0) {
+                i += run;
+                j = scantable[i];
+                level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
+                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+                SKIP_BITS(re, &s->gb, 1);
+            } else {
+                /* escape */
+                run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
+                UPDATE_CACHE(re, &s->gb);
+                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
 
-            if (s->mb_x)
-                mb_type = s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
-            else
-                mb_type = s->current_picture.f.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1]; // FIXME not sure if this is allowed in MPEG at all
-            if (IS_INTRA(mb_type))
+                i += run;
+                j = scantable[i];
+                if (level < 0) {
+                    level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
+                    level = -level;
+                } else {
+                    level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
+                }
+            }
+            if (i > 63) {
+                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
                 return -1;
+            }
 
-            s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride] =
-                mb_type | MB_TYPE_SKIP;
-//            assert(s->current_picture.f.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_skipped = 1;
+            mismatch ^= level;
+            block[j]  = level;
+            if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
+                break;
+            UPDATE_CACHE(re, &s->gb);
         }
-
-        return 0;
+end:
+        LAST_SKIP_BITS(re, &s->gb, 2);
+        CLOSE_READER(re, &s->gb);
     }
+    block[63] ^= (mismatch & 1);
 
-    switch (s->pict_type) {
-    default:
-    case AV_PICTURE_TYPE_I:
-        if (get_bits1(&s->gb) == 0) {
-            if (get_bits1(&s->gb) == 0) {
-                av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
-                return -1;
-            }
-            mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
-        } else {
-            mb_type = MB_TYPE_INTRA;
-        }
-        break;
-    case AV_PICTURE_TYPE_P:
-        mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
-        if (mb_type < 0) {
-            av_log(s->avctx, AV_LOG_ERROR, "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 AV_PICTURE_TYPE_B:
-        mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
-        if (mb_type < 0) {
-            av_log(s->avctx, AV_LOG_ERROR, "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;
-    }
-    av_dlog(s->avctx, "mb_type=%x\n", mb_type);
-//    motion_type = 0; /* avoid warning */
-    if (IS_INTRA(mb_type)) {
-        s->dsp.clear_blocks(s->block[0]);
+    s->block_last_index[n] = i;
+    return 0;
+}
 
-        if (!s->chroma_y_shift) {
-            s->dsp.clear_blocks(s->block[6]);
-        }
+static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
+                                                    DCTELEM *block, int n)
+{
+    int level, i, j, run;
+    RLTable *rl = &ff_rl_mpeg1;
+    uint8_t * const scantable = s->intra_scantable.permutated;
+    const int qscale          = s->qscale;
+    OPEN_READER(re, &s->gb);
+    i = -1;
 
-        /* compute DCT type */
-        if (s->picture_structure == PICT_FRAME && // FIXME add an interlaced_dct coded var?
-            !s->frame_pred_frame_dct) {
-            s->interlaced_dct = get_bits1(&s->gb);
-        }
+    // special case for first coefficient, no need to add second VLC table
+    UPDATE_CACHE(re, &s->gb);
+    if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
+        level = (3 * qscale) >> 1;
+        if (GET_CACHE(re, &s->gb) & 0x40000000)
+            level = -level;
+        block[0] = level;
+        i++;
+        SKIP_BITS(re, &s->gb, 2);
+        if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
+            goto end;
+    }
 
-        if (IS_QUANT(mb_type))
-            s->qscale = get_qscale(s);
+    /* now quantify & encode AC coefficients */
+    for (;;) {
+        GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
 
-        if (s->concealment_motion_vectors) {
-            /* just parse them */
-            if (s->picture_structure != PICT_FRAME)
-                skip_bits1(&s->gb); /* field select */
+        if (level != 0) {
+            i += run;
+            j  = scantable[i];
+            level = ((level * 2 + 1) * qscale) >> 1;
+            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+            SKIP_BITS(re, &s->gb, 1);
+        } else {
+            /* escape */
+            run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
+            UPDATE_CACHE(re, &s->gb);
+            level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
 
-            s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
-                mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
-            s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
-                mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
+            i += run;
+            j  = scantable[i];
+            if (level < 0) {
+                level = ((-level * 2 + 1) * qscale) >> 1;
+                level = -level;
+            } else {
+                level = ((level * 2 + 1) * qscale) >> 1;
+            }
+        }
 
-            skip_bits1(&s->gb); /* marker */
-        } else
-            memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
-        s->mb_intra = 1;
-        // if 1, we memcpy blocks in xvmcvideo
-        if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
-            ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
-            if (s->swap_uv) {
-                exchange_uv(s);
-            }
-        }
+        block[j] = level;
+        if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
+            break;
+        UPDATE_CACHE(re, &s->gb);
+    }
+end:
+    LAST_SKIP_BITS(re, &s->gb, 2);
+    CLOSE_READER(re, &s->gb);
+    s->block_last_index[n] = i;
+    return 0;
+}
 
-        if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
-            if (s->flags2 & CODEC_FLAG2_FAST) {
-                for (i = 0; i < 6; i++) {
-                    mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
-                }
+
+static inline int mpeg2_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
+{
+    int level, dc, diff, i, j, run;
+    int component;
+    RLTable *rl;
+    uint8_t * const scantable = s->intra_scantable.permutated;
+    const uint16_t *quant_matrix;
+    const int qscale = s->qscale;
+    int mismatch;
+
+    /* DC coefficient */
+    if (n < 4) {
+        quant_matrix = s->intra_matrix;
+        component = 0;
+    } else {
+        quant_matrix = s->chroma_intra_matrix;
+        component = (n & 1) + 1;
+    }
+    diff = decode_dc(&s->gb, component);
+    if (diff >= 0xffff)
+        return -1;
+    dc  = s->last_dc[component];
+    dc += diff;
+    s->last_dc[component] = dc;
+    block[0] = dc << (3 - s->intra_dc_precision);
+    av_dlog(s->avctx, "dc=%d\n", block[0]);
+    mismatch = block[0] ^ 1;
+    i = 0;
+    if (s->intra_vlc_format)
+        rl = &ff_rl_mpeg2;
+    else
+        rl = &ff_rl_mpeg1;
+
+    {
+        OPEN_READER(re, &s->gb);
+        /* now quantify & encode AC coefficients */
+        for (;;) {
+            UPDATE_CACHE(re, &s->gb);
+            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
+
+            if (level == 127) {
+                break;
+            } else if (level != 0) {
+                i += run;
+                j  = scantable[i];
+                level = (level * qscale * quant_matrix[j]) >> 4;
+                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+                LAST_SKIP_BITS(re, &s->gb, 1);
             } else {
-                for (i = 0; i < mb_block_count; i++) {
-                    if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
-                        return -1;
+                /* escape */
+                run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
+                UPDATE_CACHE(re, &s->gb);
+                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
+                i += run;
+                j  = scantable[i];
+                if (level < 0) {
+                    level = (-level * qscale * quant_matrix[j]) >> 4;
+                    level = -level;
+                } else {
+                    level = (level * qscale * quant_matrix[j]) >> 4;
                 }
             }
-        } else {
-            for (i = 0; i < 6; i++) {
-                if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
-                    return -1;
+            if (i > 63) {
+                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
+                return -1;
             }
+
+            mismatch ^= level;
+            block[j]  = level;
         }
+        CLOSE_READER(re, &s->gb);
+    }
+    block[63] ^= mismatch & 1;
+
+    s->block_last_index[n] = i;
+    return 0;
+}
+
+static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
+{
+    int level, dc, diff, j, run;
+    int component;
+    RLTable *rl;
+    uint8_t * scantable = s->intra_scantable.permutated;
+    const uint16_t *quant_matrix;
+    const int qscale = s->qscale;
+
+    /* DC coefficient */
+    if (n < 4) {
+        quant_matrix = s->intra_matrix;
+        component = 0;
     } else {
-        if (mb_type & MB_TYPE_ZERO_MV) {
-            assert(mb_type & MB_TYPE_CBP);
+        quant_matrix = s->chroma_intra_matrix;
+        component = (n & 1) + 1;
+    }
+    diff = decode_dc(&s->gb, component);
+    if (diff >= 0xffff)
+        return -1;
+    dc = s->last_dc[component];
+    dc += diff;
+    s->last_dc[component] = dc;
+    block[0] = dc << (3 - s->intra_dc_precision);
+    if (s->intra_vlc_format)
+        rl = &ff_rl_mpeg2;
+    else
+        rl = &ff_rl_mpeg1;
 
-            s->mv_dir = MV_DIR_FORWARD;
-            if (s->picture_structure == PICT_FRAME) {
-                if (!s->frame_pred_frame_dct)
-                    s->interlaced_dct = get_bits1(&s->gb);
-                s->mv_type = MV_TYPE_16X16;
+    {
+        OPEN_READER(re, &s->gb);
+        /* now quantify & encode AC coefficients */
+        for (;;) {
+            UPDATE_CACHE(re, &s->gb);
+            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
+
+            if (level == 127) {
+                break;
+            } else if (level != 0) {
+                scantable += run;
+                j = *scantable;
+                level = (level * qscale * quant_matrix[j]) >> 4;
+                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+                LAST_SKIP_BITS(re, &s->gb, 1);
             } else {
-                s->mv_type = MV_TYPE_FIELD;
-                mb_type |= MB_TYPE_INTERLACED;
-                s->field_select[0][0] = s->picture_structure - 1;
+                /* escape */
+                run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
+                UPDATE_CACHE(re, &s->gb);
+                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
+                scantable += run;
+                j = *scantable;
+                if (level < 0) {
+                    level = (-level * qscale * quant_matrix[j]) >> 4;
+                    level = -level;
+                } else {
+                    level = (level * qscale * quant_matrix[j]) >> 4;
+                }
             }
 
-            if (IS_QUANT(mb_type))
-                s->qscale = get_qscale(s);
+            block[j] = level;
+        }
+        CLOSE_READER(re, &s->gb);
+    }
 
-            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;
+    s->block_last_index[n] = scantable - s->intra_scantable.permutated;
+    return 0;
+}
+
+uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
+
+#define INIT_2D_VLC_RL(rl, static_size)\
+{\
+    static RL_VLC_ELEM rl_vlc_table[static_size];\
+    INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
+                    &rl.table_vlc[0][1], 4, 2,\
+                    &rl.table_vlc[0][0], 4, 2, static_size);\
+\
+    rl.rl_vlc[0] = rl_vlc_table;\
+    init_2d_vlc_rl(&rl);\
+}
+
+static void init_2d_vlc_rl(RLTable *rl)
+{
+    int i;
+
+    for (i = 0; i < rl->vlc.table_size; i++) {
+        int code = rl->vlc.table[i][0];
+        int len  = rl->vlc.table[i][1];
+        int level, run;
+
+        if (len == 0) { // illegal code
+            run   = 65;
+            level = MAX_LEVEL;
+        } else if (len<0) { //more bits needed
+            run   = 0;
+            level = code;
         } else {
-            assert(mb_type & MB_TYPE_L0L1);
-            // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
-            /* get additional motion vector type */
-            if (s->frame_pred_frame_dct)
-                motion_type = MT_FRAME;
-            else {
-                motion_type = get_bits(&s->gb, 2);
-                if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
-                    s->interlaced_dct = get_bits1(&s->gb);
+            if (code == rl->n) { //esc
+                run   = 65;
+                level = 0;
+            } else if (code == rl->n+1) { //eob
+                run   = 0;
+                level = 127;
+            } else {
+                run   = rl->table_run  [code] + 1;
+                level = rl->table_level[code];
             }
+        }
+        rl->rl_vlc[0][i].len   = len;
+        rl->rl_vlc[0][i].level = level;
+        rl->rl_vlc[0][i].run   = run;
+    }
+}
 
-            if (IS_QUANT(mb_type))
-                s->qscale = get_qscale(s);
-
-            /* motion vectors */
-            s->mv_dir = (mb_type >> 13) & 3;
-            av_dlog(s->avctx, "motion_type=%d\n", motion_type);
-            switch (motion_type) {
-            case MT_FRAME: /* or MT_16X8 */
-                if (s->picture_structure == PICT_FRAME) {
-                    mb_type |= MB_TYPE_16x16;
-                    s->mv_type = MV_TYPE_16X16;
-                    for (i = 0; i < 2; i++) {
-                        if (USES_LIST(mb_type, i)) {
-                            /* MT_FRAME */
-                            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 MPEG-1 */
-                            if (s->full_pel[i]) {
-                                s->mv[i][0][0] <<= 1;
-                                s->mv[i][0][1] <<= 1;
-                            }
-                        }
-                    }
-                } else {
-                    mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
-                    s->mv_type = MV_TYPE_16X8;
-                    for (i = 0; i < 2; i++) {
-                        if (USES_LIST(mb_type, i)) {
-                            /* MT_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:
-                if(s->progressive_sequence){
-                    av_log(s->avctx, AV_LOG_ERROR, "MT_FIELD in progressive_sequence\n");
-                    return -1;
-                }
-                s->mv_type = MV_TYPE_FIELD;
-                if (s->picture_structure == PICT_FRAME) {
-                    mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
-                    for (i = 0; i < 2; i++) {
-                        if (USES_LIST(mb_type, i)) {
-                            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;
-                                av_dlog(s->avctx, "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;
-                                av_dlog(s->avctx, "fmy=%d\n", val);
-                            }
-                        }
-                    }
-                } else {
-                    mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
-                    for (i = 0; i < 2; i++) {
-                        if (USES_LIST(mb_type, i)) {
-                            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:
-                if(s->progressive_sequence){
-                    av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
-                    return -1;
-                }
-                s->mv_type = MV_TYPE_DMV;
-                for (i = 0; i < 2; i++) {
-                    if (USES_LIST(mb_type, i)) {
-                        int dmx, dmy, mx, my, m;
-                        const int my_shift = s->picture_structure == PICT_FRAME;
-
-                        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] >> my_shift);
-                        dmy = get_dmv(s);
+void ff_mpeg12_common_init(MpegEncContext *s)
+{
 
+    s->y_dc_scale_table =
+    s->c_dc_scale_table = ff_mpeg2_dc_scale_table[s->intra_dc_precision];
 
-                        s->last_mv[i][0][1] = my << my_shift;
-                        s->last_mv[i][1][1] = my << my_shift;
+}
 
-                        s->mv[i][0][0] = mx;
-                        s->mv[i][0][1] = my;
-                        s->mv[i][1][0] = mx; // not used
-                        s->mv[i][1][1] = my; // not used
+void ff_mpeg1_clean_buffers(MpegEncContext *s)
+{
+    s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
+    s->last_dc[1] = s->last_dc[0];
+    s->last_dc[2] = s->last_dc[0];
+    memset(s->last_mv, 0, sizeof(s->last_mv));
+}
 
-                        if (s->picture_structure == PICT_FRAME) {
-                            mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
 
-                            // m = 1 + 2 * s->top_field_first;
-                            m = s->top_field_first ? 1 : 3;
+/******************************************/
+/* decoding */
 
-                            /* top -> top pred */
-                            s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
-                            s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
-                            m = 4 - m;
-                            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;
+VLC ff_dc_lum_vlc;
+VLC ff_dc_chroma_vlc;
 
-                            s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
-                            s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
-                            if (s->picture_structure == PICT_TOP_FIELD)
-                                s->mv[i][2][1]--;
-                            else
-                                s->mv[i][2][1]++;
-                        }
-                    }
-                }
-                break;
-            default:
-                av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
-                return -1;
-            }
-        }
+static VLC mbincr_vlc;
+static VLC mb_ptype_vlc;
+static VLC mb_btype_vlc;
+static VLC mb_pat_vlc;
 
-        s->mb_intra = 0;
-        if (HAS_CBP(mb_type)) {
-            s->dsp.clear_blocks(s->block[0]);
+av_cold void ff_mpeg12_init_vlcs(void)
+{
+    static int done = 0;
 
-            cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
-            if (mb_block_count > 6) {
-                 cbp <<= mb_block_count - 6;
-                 cbp  |= get_bits(&s->gb, mb_block_count - 6);
-                 s->dsp.clear_blocks(s->block[6]);
-            }
-            if (cbp <= 0) {
-                av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
-                return -1;
-            }
+    if (!done) {
+        done = 1;
 
-            //if 1, we memcpy blocks in xvmcvideo
-            if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
-                ff_xvmc_pack_pblocks(s, cbp);
-                if (s->swap_uv) {
-                    exchange_uv(s);
-                }
-            }
+        INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
+                        ff_mpeg12_vlc_dc_lum_bits, 1, 1,
+                        ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
+        INIT_VLC_STATIC(&ff_dc_chroma_vlc,  DC_VLC_BITS, 12,
+                        ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
+                        ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
+        INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
+                        &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
+                        &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
+        INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
+                        &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
+                        &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
+        INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
+                        &ff_mpeg12_mbPatTable[0][1], 2, 1,
+                        &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
 
-            if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
-                if (s->flags2 & CODEC_FLAG2_FAST) {
-                    for (i = 0; i < 6; i++) {
-                        if (cbp & 32) {
-                            mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
-                        } else {
-                            s->block_last_index[i] = -1;
-                        }
-                        cbp += cbp;
-                    }
-                } else {
-                    cbp <<= 12-mb_block_count;
+        INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
+                        &table_mb_ptype[0][1], 2, 1,
+                        &table_mb_ptype[0][0], 2, 1, 64);
+        INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
+                        &table_mb_btype[0][1], 2, 1,
+                        &table_mb_btype[0][0], 2, 1, 64);
+        init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
+        init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
 
-                    for (i = 0; i < mb_block_count; i++) {
-                        if (cbp & (1 << 11)) {
-                            if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
-                                return -1;
-                        } else {
-                            s->block_last_index[i] = -1;
-                        }
-                        cbp += cbp;
-                    }
-                }
-            } else {
-                if (s->flags2 & CODEC_FLAG2_FAST) {
-                    for (i = 0; i < 6; i++) {
-                        if (cbp & 32) {
-                            mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
-                        } 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, *s->pblocks[i], i) < 0)
-                                return -1;
-                        } else {
-                            s->block_last_index[i] = -1;
-                        }
-                        cbp += cbp;
-                    }
-                }
-            }
-        } else {
-            for (i = 0; i < 12; i++)
-                s->block_last_index[i] = -1;
-        }
+        INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
+        INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
     }
-
-    s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
-
-    return 0;
 }
 
-/* as H.263, but only 17 codes */
-static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
+static inline int get_dmv(MpegEncContext *s)
 {
-    int code, sign, val, l, shift;
+    if (get_bits1(&s->gb))
+        return 1 - (get_bits1(&s->gb) << 1);
+    else
+        return 0;
+}
 
-    code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
-    if (code == 0) {
-        return pred;
-    }
-    if (code < 0) {
-        return 0xffff;
+static inline int get_qscale(MpegEncContext *s)
+{
+    int qscale = get_bits(&s->gb, 5);
+    if (s->q_scale_type) {
+        return non_linear_qscale[qscale];
+    } else {
+        return qscale << 1;
     }
+}
 
-    sign  = get_bits1(&s->gb);
-    shift = fcode - 1;
-    val   = code;
-    if (shift) {
-        val  = (val - 1) << shift;
-        val |= get_bits(&s->gb, shift);
-        val++;
-    }
-    if (sign)
-        val = -val;
-    val += pred;
+static void exchange_uv(MpegEncContext *s)
+{
+    DCTELEM (*tmp)[64];
 
-    /* modulo decoding */
-    l   = INT_BIT - 5 - shift;
-    val = (val << l) >> l;
-    return val;
+    tmp           = s->pblocks[4];
+    s->pblocks[4] = s->pblocks[5];
+    s->pblocks[5] = tmp;
 }
 
-static inline int mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
-{
-    int level, dc, diff, i, j, run;
-    int component;
-    RLTable *rl = &ff_rl_mpeg1;
-    uint8_t * const scantable    = s->intra_scantable.permutated;
-    const uint16_t *quant_matrix = s->intra_matrix;
-    const int qscale             = s->qscale;
+/* motion type (for MPEG-2) */
+#define MT_FIELD 1
+#define MT_FRAME 2
+#define MT_16X8  2
+#define MT_DMV   3
 
-    /* DC coefficient */
-    component = (n <= 3 ? 0 : n - 4 + 1);
-    diff = decode_dc(&s->gb, component);
-    if (diff >= 0xffff)
-        return -1;
-    dc  = s->last_dc[component];
-    dc += diff;
-    s->last_dc[component] = dc;
-    block[0] = dc * quant_matrix[0];
-    av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
-    i = 0;
-    {
-        OPEN_READER(re, &s->gb);
-        /* now quantify & encode AC coefficients */
-        for (;;) {
-            UPDATE_CACHE(re, &s->gb);
-            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
+static int mpeg_decode_mb(MpegEncContext *s, DCTELEM block[12][64])
+{
+    int i, j, k, cbp, val, mb_type, motion_type;
+    const int mb_block_count = 4 + (1 << s->chroma_format);
 
-            if (level == 127) {
-                break;
-            } else if (level != 0) {
-                i += run;
-                j = scantable[i];
-                level = (level * qscale * quant_matrix[j]) >> 4;
-                level = (level - 1) | 1;
-                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
-                LAST_SKIP_BITS(re, &s->gb, 1);
-            } else {
-                /* escape */
-                run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
-                UPDATE_CACHE(re, &s->gb);
-                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
-                if (level == -128) {
-                    level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
-                } else if (level == 0) {
-                    level = SHOW_UBITS(re, &s->gb, 8)      ; LAST_SKIP_BITS(re, &s->gb, 8);
-                }
-                i += run;
-                j = scantable[i];
-                if (level < 0) {
-                    level = -level;
-                    level = (level * qscale * quant_matrix[j]) >> 4;
-                    level = (level - 1) | 1;
-                    level = -level;
-                } else {
-                    level = (level * qscale * quant_matrix[j]) >> 4;
-                    level = (level - 1) | 1;
-                }
-            }
-            if (i > 63) {
-                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
-                return -1;
-            }
+    av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
 
-            block[j] = level;
-        }
-        CLOSE_READER(re, &s->gb);
-    }
-    s->block_last_index[n] = i;
-   return 0;
-}
+    assert(s->mb_skipped == 0);
 
-int ff_mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
-{
-    return mpeg1_decode_block_intra(s, block, n);
-}
+    if (s->mb_skip_run-- != 0) {
+        if (s->pict_type == AV_PICTURE_TYPE_P) {
+            s->mb_skipped = 1;
+            s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
+        } else {
+            int mb_type;
 
-static inline int mpeg1_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
-{
-    int level, i, j, run;
-    RLTable *rl = &ff_rl_mpeg1;
-    uint8_t * const scantable    = s->intra_scantable.permutated;
-    const uint16_t *quant_matrix = s->inter_matrix;
-    const int qscale             = s->qscale;
+            if (s->mb_x)
+                mb_type = s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
+            else
+                mb_type = s->current_picture.f.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1]; // FIXME not sure if this is allowed in MPEG at all
+            if (IS_INTRA(mb_type))
+                return -1;
+            s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride] =
+                mb_type | MB_TYPE_SKIP;
+//            assert(s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1] & (MB_TYPE_16x16 | MB_TYPE_16x8));
 
-    {
-        OPEN_READER(re, &s->gb);
-        i = -1;
-        // special case for first coefficient, no need to add second VLC table
-        UPDATE_CACHE(re, &s->gb);
-        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
-            level = (3 * qscale * quant_matrix[0]) >> 5;
-            level = (level - 1) | 1;
-            if (GET_CACHE(re, &s->gb) & 0x40000000)
-                level = -level;
-            block[0] = level;
-            i++;
-            SKIP_BITS(re, &s->gb, 2);
-            if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
-                goto end;
+            if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
+                s->mb_skipped = 1;
         }
-        /* now quantify & encode AC coefficients */
-        for (;;) {
-            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
 
-            if (level != 0) {
-                i += run;
-                j = scantable[i];
-                level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
-                level = (level - 1) | 1;
-                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
-                SKIP_BITS(re, &s->gb, 1);
-            } else {
-                /* escape */
-                run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
-                UPDATE_CACHE(re, &s->gb);
-                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
-                if (level == -128) {
-                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
-                } else if (level == 0) {
-                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
-                }
-                i += run;
-                j = scantable[i];
-                if (level < 0) {
-                    level = -level;
-                    level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
-                    level = (level - 1) | 1;
-                    level = -level;
-                } else {
-                    level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
-                    level = (level - 1) | 1;
-                }
-            }
-            if (i > 63) {
-                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
+        return 0;
+    }
+
+    switch (s->pict_type) {
+    default:
+    case AV_PICTURE_TYPE_I:
+        if (get_bits1(&s->gb) == 0) {
+            if (get_bits1(&s->gb) == 0) {
+                av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
                 return -1;
             }
-
-            block[j] = level;
-            if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
-                break;
-            UPDATE_CACHE(re, &s->gb);
+            mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
+        } else {
+            mb_type = MB_TYPE_INTRA;
         }
-end:
-        LAST_SKIP_BITS(re, &s->gb, 2);
-        CLOSE_READER(re, &s->gb);
+        break;
+    case AV_PICTURE_TYPE_P:
+        mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
+        if (mb_type < 0) {
+            av_log(s->avctx, AV_LOG_ERROR, "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 AV_PICTURE_TYPE_B:
+        mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
+        if (mb_type < 0) {
+            av_log(s->avctx, AV_LOG_ERROR, "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;
     }
-    s->block_last_index[n] = i;
-    return 0;
-}
+    av_dlog(s->avctx, "mb_type=%x\n", mb_type);
+//    motion_type = 0; /* avoid warning */
+    if (IS_INTRA(mb_type)) {
+        s->dsp.clear_blocks(s->block[0]);
 
-static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
-{
-    int level, i, j, run;
-    RLTable *rl = &ff_rl_mpeg1;
-    uint8_t * const scantable = s->intra_scantable.permutated;
-    const int qscale          = s->qscale;
+        if (!s->chroma_y_shift) {
+            s->dsp.clear_blocks(s->block[6]);
+        }
 
-    {
-        OPEN_READER(re, &s->gb);
-        i = -1;
-        // special case for first coefficient, no need to add second VLC table
-        UPDATE_CACHE(re, &s->gb);
-        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
-            level = (3 * qscale) >> 1;
-            level = (level - 1) | 1;
-            if (GET_CACHE(re, &s->gb) & 0x40000000)
-                level = -level;
-            block[0] = level;
-            i++;
-            SKIP_BITS(re, &s->gb, 2);
-            if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
-                goto end;
+        /* compute DCT type */
+        if (s->picture_structure == PICT_FRAME && // FIXME add an interlaced_dct coded var?
+            !s->frame_pred_frame_dct) {
+            s->interlaced_dct = get_bits1(&s->gb);
         }
 
-        /* now quantify & encode AC coefficients */
-        for (;;) {
-            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
-
-            if (level != 0) {
-                i += run;
-                j = scantable[i];
-                level = ((level * 2 + 1) * qscale) >> 1;
-                level = (level - 1) | 1;
-                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
-                SKIP_BITS(re, &s->gb, 1);
-            } else {
-                /* escape */
-                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
-                UPDATE_CACHE(re, &s->gb);
-                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
-                if (level == -128) {
-                    level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
-                } else if (level == 0) {
-                    level = SHOW_UBITS(re, &s->gb, 8)      ; SKIP_BITS(re, &s->gb, 8);
-                }
-                i += run;
-                j = scantable[i];
-                if (level < 0) {
-                    level = -level;
-                    level = ((level * 2 + 1) * qscale) >> 1;
-                    level = (level - 1) | 1;
-                    level = -level;
-                } else {
-                    level = ((level * 2 + 1) * qscale) >> 1;
-                    level = (level - 1) | 1;
-                }
-            }
-
-            block[j] = level;
-            if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
-                break;
-            UPDATE_CACHE(re, &s->gb);
-        }
-end:
-        LAST_SKIP_BITS(re, &s->gb, 2);
-        CLOSE_READER(re, &s->gb);
-    }
-    s->block_last_index[n] = i;
-    return 0;
-}
-
-
-static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n)
-{
-    int level, i, j, run;
-    RLTable *rl = &ff_rl_mpeg1;
-    uint8_t * const scantable = s->intra_scantable.permutated;
-    const uint16_t *quant_matrix;
-    const int qscale = s->qscale;
-    int mismatch;
+        if (IS_QUANT(mb_type))
+            s->qscale = get_qscale(s);
 
-    mismatch = 1;
+        if (s->concealment_motion_vectors) {
+            /* just parse them */
+            if (s->picture_structure != PICT_FRAME)
+                skip_bits1(&s->gb); /* field select */
 
-    {
-        OPEN_READER(re, &s->gb);
-        i = -1;
-        if (n < 4)
-            quant_matrix = s->inter_matrix;
-        else
-            quant_matrix = s->chroma_inter_matrix;
+            s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
+                mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
+            s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
+                mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
 
-        // special case for first coefficient, no need to add second VLC table
-        UPDATE_CACHE(re, &s->gb);
-        if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
-            level= (3 * qscale * quant_matrix[0]) >> 5;
-            if (GET_CACHE(re, &s->gb) & 0x40000000)
-                level = -level;
-            block[0]  = level;
-            mismatch ^= level;
-            i++;
-            SKIP_BITS(re, &s->gb, 2);
-            if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
-                goto end;
+            skip_bits1(&s->gb); /* marker */
+        } else
+            memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
+        s->mb_intra = 1;
+        // if 1, we memcpy blocks in xvmcvideo
+        if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
+            ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
+            if (s->swap_uv) {
+                exchange_uv(s);
+            }
         }
 
-        /* now quantify & encode AC coefficients */
-        for (;;) {
-            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
-
-            if (level != 0) {
-                i += run;
-                j = scantable[i];
-                level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
-                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
-                SKIP_BITS(re, &s->gb, 1);
+        if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
+            if (s->flags2 & CODEC_FLAG2_FAST) {
+                for (i = 0; i < 6; i++) {
+                    mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
+                }
             } else {
-                /* escape */
-                run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
-                UPDATE_CACHE(re, &s->gb);
-                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
-
-                i += run;
-                j = scantable[i];
-                if (level < 0) {
-                    level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
-                    level = -level;
-                } else {
-                    level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
+                for (i = 0; i < mb_block_count; i++) {
+                    if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
+                        return -1;
                 }
             }
-            if (i > 63) {
-                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
-                return -1;
+        } else {
+            for (i = 0; i < 6; i++) {
+                if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
+                    return -1;
             }
-
-            mismatch ^= level;
-            block[j]  = level;
-            if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
-                break;
-            UPDATE_CACHE(re, &s->gb);
         }
-end:
-        LAST_SKIP_BITS(re, &s->gb, 2);
-        CLOSE_READER(re, &s->gb);
-    }
-    block[63] ^= (mismatch & 1);
+    } else {
+        if (mb_type & MB_TYPE_ZERO_MV) {
+            assert(mb_type & MB_TYPE_CBP);
 
-    s->block_last_index[n] = i;
-    return 0;
-}
+            s->mv_dir = MV_DIR_FORWARD;
+            if (s->picture_structure == PICT_FRAME) {
+                if (!s->frame_pred_frame_dct)
+                    s->interlaced_dct = get_bits1(&s->gb);
+                s->mv_type = MV_TYPE_16X16;
+            } else {
+                s->mv_type = MV_TYPE_FIELD;
+                mb_type |= MB_TYPE_INTERLACED;
+                s->field_select[0][0] = s->picture_structure - 1;
+            }
 
-static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
-                                                    DCTELEM *block, int n)
-{
-    int level, i, j, run;
-    RLTable *rl = &ff_rl_mpeg1;
-    uint8_t * const scantable = s->intra_scantable.permutated;
-    const int qscale          = s->qscale;
-    OPEN_READER(re, &s->gb);
-    i = -1;
+            if (IS_QUANT(mb_type))
+                s->qscale = get_qscale(s);
 
-    // special case for first coefficient, no need to add second VLC table
-    UPDATE_CACHE(re, &s->gb);
-    if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
-        level = (3 * qscale) >> 1;
-        if (GET_CACHE(re, &s->gb) & 0x40000000)
-            level = -level;
-        block[0] = level;
-        i++;
-        SKIP_BITS(re, &s->gb, 2);
-        if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
-            goto end;
-    }
+            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 additional motion vector type */
+            if (s->frame_pred_frame_dct)
+                motion_type = MT_FRAME;
+            else {
+                motion_type = get_bits(&s->gb, 2);
+                if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
+                    s->interlaced_dct = get_bits1(&s->gb);
+            }
 
-    /* now quantify & encode AC coefficients */
-    for (;;) {
-        GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
+            if (IS_QUANT(mb_type))
+                s->qscale = get_qscale(s);
 
-        if (level != 0) {
-            i += run;
-            j  = scantable[i];
-            level = ((level * 2 + 1) * qscale) >> 1;
-            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
-            SKIP_BITS(re, &s->gb, 1);
-        } else {
-            /* escape */
-            run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
-            UPDATE_CACHE(re, &s->gb);
-            level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
+            /* motion vectors */
+            s->mv_dir = (mb_type >> 13) & 3;
+            av_dlog(s->avctx, "motion_type=%d\n", motion_type);
+            switch (motion_type) {
+            case MT_FRAME: /* or MT_16X8 */
+                if (s->picture_structure == PICT_FRAME) {
+                    mb_type |= MB_TYPE_16x16;
+                    s->mv_type = MV_TYPE_16X16;
+                    for (i = 0; i < 2; i++) {
+                        if (USES_LIST(mb_type, i)) {
+                            /* MT_FRAME */
+                            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 MPEG-1 */
+                            if (s->full_pel[i]) {
+                                s->mv[i][0][0] <<= 1;
+                                s->mv[i][0][1] <<= 1;
+                            }
+                        }
+                    }
+                } else {
+                    mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
+                    s->mv_type = MV_TYPE_16X8;
+                    for (i = 0; i < 2; i++) {
+                        if (USES_LIST(mb_type, i)) {
+                            /* MT_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 (i = 0; i < 2; i++) {
+                        if (USES_LIST(mb_type, i)) {
+                            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;
+                                av_dlog(s->avctx, "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;
+                                av_dlog(s->avctx, "fmy=%d\n", val);
+                            }
+                        }
+                    }
+                } else {
+                    av_assert0(!s->progressive_sequence);
+                    mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
+                    for (i = 0; i < 2; i++) {
+                        if (USES_LIST(mb_type, i)) {
+                            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:
+                if(s->progressive_sequence){
+                    av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
+                    return -1;
+                }
+                s->mv_type = MV_TYPE_DMV;
+                for (i = 0; i < 2; i++) {
+                    if (USES_LIST(mb_type, i)) {
+                        int dmx, dmy, mx, my, m;
+                        const int my_shift = s->picture_structure == PICT_FRAME;
 
-            i += run;
-            j  = scantable[i];
-            if (level < 0) {
-                level = ((-level * 2 + 1) * qscale) >> 1;
-                level = -level;
-            } else {
-                level = ((level * 2 + 1) * qscale) >> 1;
-            }
-        }
+                        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] >> my_shift);
+                        dmy = get_dmv(s);
 
-        block[j] = level;
-        if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
-            break;
-        UPDATE_CACHE(re, &s->gb);
-    }
-end:
-    LAST_SKIP_BITS(re, &s->gb, 2);
-    CLOSE_READER(re, &s->gb);
-    s->block_last_index[n] = i;
-    return 0;
-}
 
+                        s->last_mv[i][0][1] = my << my_shift;
+                        s->last_mv[i][1][1] = my << my_shift;
 
-static inline int mpeg2_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
-{
-    int level, dc, diff, i, j, run;
-    int component;
-    RLTable *rl;
-    uint8_t * const scantable = s->intra_scantable.permutated;
-    const uint16_t *quant_matrix;
-    const int qscale = s->qscale;
-    int mismatch;
+                        s->mv[i][0][0] = mx;
+                        s->mv[i][0][1] = my;
+                        s->mv[i][1][0] = mx; // not used
+                        s->mv[i][1][1] = my; // not used
 
-    /* DC coefficient */
-    if (n < 4) {
-        quant_matrix = s->intra_matrix;
-        component = 0;
-    } else {
-        quant_matrix = s->chroma_intra_matrix;
-        component = (n & 1) + 1;
-    }
-    diff = decode_dc(&s->gb, component);
-    if (diff >= 0xffff)
-        return -1;
-    dc  = s->last_dc[component];
-    dc += diff;
-    s->last_dc[component] = dc;
-    block[0] = dc << (3 - s->intra_dc_precision);
-    av_dlog(s->avctx, "dc=%d\n", block[0]);
-    mismatch = block[0] ^ 1;
-    i = 0;
-    if (s->intra_vlc_format)
-        rl = &ff_rl_mpeg2;
-    else
-        rl = &ff_rl_mpeg1;
+                        if (s->picture_structure == PICT_FRAME) {
+                            mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
 
-    {
-        OPEN_READER(re, &s->gb);
-        /* now quantify & encode AC coefficients */
-        for (;;) {
-            UPDATE_CACHE(re, &s->gb);
-            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
+                            // m = 1 + 2 * s->top_field_first;
+                            m = s->top_field_first ? 1 : 3;
 
-            if (level == 127) {
-                break;
-            } else if (level != 0) {
-                i += run;
-                j  = scantable[i];
-                level = (level * qscale * quant_matrix[j]) >> 4;
-                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
-                LAST_SKIP_BITS(re, &s->gb, 1);
-            } else {
-                /* escape */
-                run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
-                UPDATE_CACHE(re, &s->gb);
-                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
-                i += run;
-                j  = scantable[i];
-                if (level < 0) {
-                    level = (-level * qscale * quant_matrix[j]) >> 4;
-                    level = -level;
-                } else {
-                    level = (level * qscale * quant_matrix[j]) >> 4;
+                            /* top -> top pred */
+                            s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
+                            s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
+                            m = 4 - m;
+                            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->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
+                            s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
+                            if (s->picture_structure == PICT_TOP_FIELD)
+                                s->mv[i][2][1]--;
+                            else
+                                s->mv[i][2][1]++;
+                        }
+                    }
                 }
-            }
-            if (i > 63) {
-                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
+                break;
+            default:
+                av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
                 return -1;
             }
-
-            mismatch ^= level;
-            block[j]  = level;
         }
-        CLOSE_READER(re, &s->gb);
-    }
-    block[63] ^= mismatch & 1;
 
-    s->block_last_index[n] = i;
-    return 0;
-}
+        s->mb_intra = 0;
+        if (HAS_CBP(mb_type)) {
+            s->dsp.clear_blocks(s->block[0]);
 
-static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
-{
-    int level, dc, diff, j, run;
-    int component;
-    RLTable *rl;
-    uint8_t * scantable = s->intra_scantable.permutated;
-    const uint16_t *quant_matrix;
-    const int qscale = s->qscale;
+            cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
+            if (mb_block_count > 6) {
+                 cbp <<= mb_block_count - 6;
+                 cbp  |= get_bits(&s->gb, mb_block_count - 6);
+                 s->dsp.clear_blocks(s->block[6]);
+            }
+            if (cbp <= 0) {
+                av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
+                return -1;
+            }
 
-    /* DC coefficient */
-    if (n < 4) {
-        quant_matrix = s->intra_matrix;
-        component = 0;
-    } else {
-        quant_matrix = s->chroma_intra_matrix;
-        component = (n & 1) + 1;
-    }
-    diff = decode_dc(&s->gb, component);
-    if (diff >= 0xffff)
-        return -1;
-    dc = s->last_dc[component];
-    dc += diff;
-    s->last_dc[component] = dc;
-    block[0] = dc << (3 - s->intra_dc_precision);
-    if (s->intra_vlc_format)
-        rl = &ff_rl_mpeg2;
-    else
-        rl = &ff_rl_mpeg1;
+            //if 1, we memcpy blocks in xvmcvideo
+            if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
+                ff_xvmc_pack_pblocks(s, cbp);
+                if (s->swap_uv) {
+                    exchange_uv(s);
+                }
+            }
 
-    {
-        OPEN_READER(re, &s->gb);
-        /* now quantify & encode AC coefficients */
-        for (;;) {
-            UPDATE_CACHE(re, &s->gb);
-            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
+            if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
+                if (s->flags2 & CODEC_FLAG2_FAST) {
+                    for (i = 0; i < 6; i++) {
+                        if (cbp & 32) {
+                            mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
+                        } else {
+                            s->block_last_index[i] = -1;
+                        }
+                        cbp += cbp;
+                    }
+                } else {
+                    cbp <<= 12-mb_block_count;
 
-            if (level == 127) {
-                break;
-            } else if (level != 0) {
-                scantable += run;
-                j = *scantable;
-                level = (level * qscale * quant_matrix[j]) >> 4;
-                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
-                LAST_SKIP_BITS(re, &s->gb, 1);
+                    for (i = 0; i < mb_block_count; i++) {
+                        if (cbp & (1 << 11)) {
+                            if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
+                                return -1;
+                        } else {
+                            s->block_last_index[i] = -1;
+                        }
+                        cbp += cbp;
+                    }
+                }
             } else {
-                /* escape */
-                run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
-                UPDATE_CACHE(re, &s->gb);
-                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
-                scantable += run;
-                j = *scantable;
-                if (level < 0) {
-                    level = (-level * qscale * quant_matrix[j]) >> 4;
-                    level = -level;
+                if (s->flags2 & CODEC_FLAG2_FAST) {
+                    for (i = 0; i < 6; i++) {
+                        if (cbp & 32) {
+                            mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
+                        } else {
+                            s->block_last_index[i] = -1;
+                        }
+                        cbp += cbp;
+                    }
                 } else {
-                    level = (level * qscale * quant_matrix[j]) >> 4;
+                    for (i = 0; i < 6; i++) {
+                        if (cbp & 32) {
+                            if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
+                                return -1;
+                        } else {
+                            s->block_last_index[i] = -1;
+                        }
+                        cbp += cbp;
+                    }
                 }
             }
-
-            block[j] = level;
+        } else {
+            for (i = 0; i < 12; i++)
+                s->block_last_index[i] = -1;
         }
-        CLOSE_READER(re, &s->gb);
     }
 
-    s->block_last_index[n] = scantable - s->intra_scantable.permutated;
+    s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
+
     return 0;
 }
 
-typedef struct Mpeg1Context {
-    MpegEncContext mpeg_enc_ctx;
-    int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
-    int repeat_field; /* true if we must repeat the field */
-    AVPanScan pan_scan;              /**< some temporary storage for the panscan */
-    int slice_count;
-    int swap_uv;//indicate VCR2
-    int save_aspect_info;
-    int save_width, save_height, save_progressive_seq;
-    AVRational frame_rate_ext;       ///< MPEG-2 specific framerate modificator
-    int sync;                        ///< Did we reach a sync point like a GOP/SEQ/KEYFrame?
-} Mpeg1Context;
-
 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
 {
     Mpeg1Context *s = avctx->priv_data;
@@ -1178,7 +1158,7 @@ static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCode
     if (!ctx->mpeg_enc_ctx_allocated)
         memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
 
-    if (!(s->pict_type == FF_B_TYPE || s->low_delay))
+    if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
         s->picture_number++;
 
     return 0;
@@ -1296,8 +1276,8 @@ static int mpeg_decode_postinit(AVCodecContext *avctx)
         assert((avctx->sub_id == 1) == (avctx->codec_id == CODEC_ID_MPEG1VIDEO));
         if (avctx->codec_id == CODEC_ID_MPEG1VIDEO) {
             //MPEG-1 fps
-            avctx->time_base.den = ff_frame_rate_tab[s->frame_rate_index].num;
-            avctx->time_base.num = ff_frame_rate_tab[s->frame_rate_index].den;
+            avctx->time_base.den = avpriv_frame_rate_tab[s->frame_rate_index].num;
+            avctx->time_base.num = avpriv_frame_rate_tab[s->frame_rate_index].den;
             //MPEG-1 aspect
             avctx->sample_aspect_ratio = av_d2q(1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
             avctx->ticks_per_frame=1;
@@ -1305,8 +1285,8 @@ static int mpeg_decode_postinit(AVCodecContext *avctx)
         //MPEG-2 fps
             av_reduce(&s->avctx->time_base.den,
                       &s->avctx->time_base.num,
-                      ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
-                      ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
+                      avpriv_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
+                      avpriv_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
                       1 << 30);
             avctx->ticks_per_frame = 2;
             //MPEG-2 aspect
@@ -1384,7 +1364,7 @@ static int mpeg1_decode_picture(AVCodecContext *avctx,
     if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
         s->full_pel[0] = get_bits1(&s->gb);
         f_code = get_bits(&s->gb, 3);
-        if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
+        if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
             return -1;
         s->mpeg_f_code[0][0] = f_code;
         s->mpeg_f_code[0][1] = f_code;
@@ -1392,7 +1372,7 @@ static int mpeg1_decode_picture(AVCodecContext *avctx,
     if (s->pict_type == AV_PICTURE_TYPE_B) {
         s->full_pel[1] = get_bits1(&s->gb);
         f_code = get_bits(&s->gb, 3);
-        if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
+        if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
             return -1;
         s->mpeg_f_code[1][0] = f_code;
         s->mpeg_f_code[1][1] = f_code;
@@ -1610,15 +1590,6 @@ static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
     av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
 }
 
-static void exchange_uv(MpegEncContext *s)
-{
-    DCTELEM (*tmp)[64];
-
-    tmp           = s->pblocks[4];
-    s->pblocks[4] = s->pblocks[5];
-    s->pblocks[5] = tmp;
-}
-
 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
 {
     AVCodecContext *avctx = s->avctx;
@@ -1646,7 +1617,7 @@ static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
 
         *s->current_picture_ptr->f.pan_scan = s1->pan_scan;
 
-        if (HAVE_PTHREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
+        if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
             ff_thread_finish_setup(avctx);
     } else { // second field
         int i;
@@ -1686,10 +1657,9 @@ static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
  * @return DECODE_SLICE_ERROR if the slice is damaged<br>
  *         DECODE_SLICE_OK if this slice is ok<br>
  */
-static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
+static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
                              const uint8_t **buf, int buf_size)
 {
-    MpegEncContext *s     = &s1->mpeg_enc_ctx;
     AVCodecContext *avctx = s->avctx;
     const int lowres      = s->avctx->lowres;
     const int field_pic   = s->picture_structure != PICT_FRAME;
@@ -1747,7 +1717,7 @@ static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
     if (avctx->hwaccel) {
         const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
         int start_code = -1;
-        buf_end = ff_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
+        buf_end = avpriv_mpv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
         if (buf_end < *buf + buf_size)
             buf_end -= 4;
         s->mb_y = mb_y;
@@ -1834,7 +1804,7 @@ static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
                              && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
 
                 if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
-                    || (avctx->error_recognition >= FF_ER_AGGRESSIVE && left > 8)) {
+                    || ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
                     av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
                     return -1;
                 } else
@@ -1921,12 +1891,12 @@ static int slice_decode_thread(AVCodecContext *c, void *arg)
         uint32_t start_code;
         int ret;
 
-        ret = mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
+        ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
         emms_c();
 //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
 //ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, s->start_mb_y, s->end_mb_y, s->error_count);
         if (ret < 0) {
-            if (c->error_recognition >= FF_ER_EXPLODE)
+            if (c->err_recognition & AV_EF_EXPLODE)
                 return ret;
             if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
                 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, AC_ERROR | DC_ERROR | MV_ERROR);
@@ -1938,7 +1908,7 @@ static int slice_decode_thread(AVCodecContext *c, void *arg)
             return 0;
 
         start_code = -1;
-        buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
+        buf = avpriv_mpv_find_start_code(buf, s->gb.buffer_end, &start_code);
         mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
         if (s->picture_structure == PICT_BOTTOM_FIELD)
             mb_y++;
@@ -1968,7 +1938,7 @@ static int slice_end(AVCodecContext *avctx, AVFrame *pict)
         ff_xvmc_field_end(s);
 
     /* end of slice reached */
-    if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field) {
+    if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field && !s->first_slice) {
         /* end of image */
 
         s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_MPEG2;
@@ -2014,7 +1984,7 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx,
     s->aspect_ratio_info = get_bits(&s->gb, 4);
     if (s->aspect_ratio_info == 0) {
         av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
-        if (avctx->error_recognition >= FF_ER_COMPLIANT)
+        if (avctx->err_recognition & AV_EF_BITSTREAM)
             return -1;
     }
     s->frame_rate_index = get_bits(&s->gb, 4);
@@ -2133,8 +2103,22 @@ static int vcr2_init_sequence(AVCodecContext *avctx)
 static void mpeg_decode_user_data(AVCodecContext *avctx,
                                   const uint8_t *p, int buf_size)
 {
+    Mpeg1Context *s = avctx->priv_data;
     const uint8_t *buf_end = p + buf_size;
 
+    if(buf_size > 29){
+        int i;
+        for(i=0; i<20; i++)
+            if(!memcmp(p+i, "\0TMPGEXS\0", 9)){
+                s->tmpgexs= 1;
+            }
+
+/*        for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
+            av_log(0,0, "%c", p[i]);
+        }
+            av_log(0,0, "\n");*/
+    }
+
     /* we parse the DTG active format information */
     if (buf_end - p >= 5 &&
         p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
@@ -2158,14 +2142,14 @@ static void mpeg_decode_gop(AVCodecContext *avctx,
     Mpeg1Context *s1  = avctx->priv_data;
     MpegEncContext *s = &s1->mpeg_enc_ctx;
 
+    int drop_frame_flag;
     int time_code_hours, time_code_minutes;
     int time_code_seconds, time_code_pictures;
     int broken_link;
 
     init_get_bits(&s->gb, buf, buf_size*8);
 
-    skip_bits1(&s->gb); /* drop_frame_flag */
-
+    drop_frame_flag   = get_bits(&s->gb, 1);
     time_code_hours   = get_bits(&s->gb, 5);
     time_code_minutes = get_bits(&s->gb, 6);
     skip_bits1(&s->gb); // marker bit
@@ -2179,8 +2163,9 @@ static void mpeg_decode_gop(AVCodecContext *avctx,
     broken_link = get_bits1(&s->gb);
 
     if (s->avctx->debug & FF_DEBUG_PICT_INFO)
-        av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
+        av_log(s->avctx, AV_LOG_DEBUG, "GOP (%02d:%02d:%02d%c%02d) closed_gop=%d broken_link=%d\n",
                time_code_hours, time_code_minutes, time_code_seconds,
+               drop_frame_flag ? ';' : ':',
                time_code_pictures, s->closed_gop, broken_link);
 }
 /**
@@ -2217,7 +2202,7 @@ int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size,
             }
             state++;
         } else {
-            i = ff_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
+            i = avpriv_mpv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
             if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
                 i++;
                 pc->frame_start_found = 4;
@@ -2287,7 +2272,7 @@ static int mpeg_decode_frame(AVCodecContext *avctx,
 
     if (avctx->extradata && !avctx->frame_number) {
         int ret = decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
-        if (ret < 0 && avctx->error_recognition >= FF_ER_EXPLODE)
+        if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
             return ret;
     }
 
@@ -2308,7 +2293,7 @@ static int decode_chunks(AVCodecContext *avctx,
     for (;;) {
         /* find next start code */
         uint32_t start_code = -1;
-        buf_ptr = ff_find_start_code(buf_ptr, buf_end, &start_code);
+        buf_ptr = avpriv_mpv_find_start_code(buf_ptr, buf_end, &start_code);
         if (start_code > 0x1ff) {
             if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {
                 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
@@ -2323,6 +2308,7 @@ static int decode_chunks(AVCodecContext *avctx,
                 if (CONFIG_VDPAU && uses_vdpau(avctx))
                     ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
 
+
                 if (slice_end(avctx, picture)) {
                     if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
                         *data_size = sizeof(AVPicture);
@@ -2343,15 +2329,20 @@ static int decode_chunks(AVCodecContext *avctx,
         case SEQ_START_CODE:
             if (last_code == 0) {
                 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
-                s->sync=1;
+                if(buf != avctx->extradata)
+                    s->sync=1;
             } else {
                 av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
-                if (avctx->error_recognition >= FF_ER_EXPLODE)
+                if (avctx->err_recognition & AV_EF_EXPLODE)
                     return AVERROR_INVALIDDATA;
             }
             break;
 
         case PICTURE_START_CODE:
+            if(s->tmpgexs){
+                s2->intra_dc_precision= 3;
+                s2->intra_matrix[0]= 1;
+            }
             if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {
                 int i;
 
@@ -2376,7 +2367,7 @@ static int decode_chunks(AVCodecContext *avctx,
                 last_code = PICTURE_START_CODE;
             } else {
                 av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
-                if (avctx->error_recognition >= FF_ER_EXPLODE)
+                if (avctx->err_recognition & AV_EF_EXPLODE)
                     return AVERROR_INVALIDDATA;
             }
             break;
@@ -2389,7 +2380,7 @@ static int decode_chunks(AVCodecContext *avctx,
                 mpeg_decode_sequence_extension(s);
                 } else {
                     av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
-                    if (avctx->error_recognition >= FF_ER_EXPLODE)
+                    if (avctx->err_recognition & AV_EF_EXPLODE)
                         return AVERROR_INVALIDDATA;
                 }
                 break;
@@ -2407,7 +2398,7 @@ static int decode_chunks(AVCodecContext *avctx,
                     mpeg_decode_picture_coding_extension(s);
                 } else {
                     av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
-                    if (avctx->error_recognition >= FF_ER_EXPLODE)
+                    if (avctx->err_recognition & AV_EF_EXPLODE)
                         return AVERROR_INVALIDDATA;
                 }
                 break;
@@ -2423,7 +2414,7 @@ static int decode_chunks(AVCodecContext *avctx,
                 s->sync=1;
             } else {
                 av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
-                if (avctx->error_recognition >= FF_ER_EXPLODE)
+                if (avctx->err_recognition & AV_EF_EXPLODE)
                     return AVERROR_INVALIDDATA;
             }
             break;
@@ -2449,7 +2440,7 @@ static int decode_chunks(AVCodecContext *avctx,
                             break;
                     }
                 }
-                if (s2->pict_type == AV_PICTURE_TYPE_I)
+                if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->flags2 & CODEC_FLAG2_SHOW_ALL))
                     s->sync=1;
                 if (s2->next_picture_ptr == NULL) {
                 /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
@@ -2470,7 +2461,7 @@ static int decode_chunks(AVCodecContext *avctx,
 
                 if (!s2->pict_type) {
                     av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
-                    if (avctx->error_recognition >= FF_ER_EXPLODE)
+                    if (avctx->err_recognition & AV_EF_EXPLODE)
                         return AVERROR_INVALIDDATA;
                     break;
                 }
@@ -2507,11 +2498,11 @@ static int decode_chunks(AVCodecContext *avctx,
                     }
                     buf_ptr += 2; // FIXME add minimum number of bytes per slice
                 } else {
-                    ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
+                    ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
                     emms_c();
 
                     if (ret < 0) {
-                        if (avctx->error_recognition >= FF_ER_EXPLODE)
+                        if (avctx->err_recognition & AV_EF_EXPLODE)
                             return ret;
                         if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
                             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);