]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mpeg4videodec.c
Merge commit '58a868968df445068a143f327ced03b6a02baf0d'
[ffmpeg] / libavcodec / mpeg4videodec.c
index 38d3d2af57d32267ded23a957223cc4f662dcd29..34ec2b3e61e1f9021dcfd454baff3ec442d4af95 100644 (file)
@@ -3,23 +3,26 @@
  * Copyright (c) 2000,2001 Fabrice Bellard
  * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
  *
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#define UNCHECKED_BITSTREAM_READER 1
+
+#include "libavutil/opt.h"
 #include "error_resilience.h"
 #include "internal.h"
 #include "mpegvideo.h"
@@ -106,12 +109,13 @@ void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
  * check if the next stuff is a resync marker or the end.
  * @return 0 if not
  */
-static inline int mpeg4_is_resync(MpegEncContext *s)
+static inline int mpeg4_is_resync(Mpeg4DecContext *ctx)
 {
+    MpegEncContext *s = &ctx->m;
     int bits_count = get_bits_count(&s->gb);
     int v          = show_bits(&s->gb, 16);
 
-    if (s->workaround_bugs & FF_BUG_NO_PADDING)
+    if (s->workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker)
         return 0;
 
     while (v <= 0xFF) {
@@ -128,10 +132,11 @@ static inline int mpeg4_is_resync(MpegEncContext *s)
         v  |= 0x7F >> (7 - (bits_count & 7));
 
         if (v == 0x7F)
-            return 1;
+            return s->mb_num;
     } else {
         if (v == ff_mpeg4_resync_prefix[bits_count & 7]) {
-            int len;
+            int len, mb_num;
+            int mb_num_bits = av_log2(s->mb_num - 1) + 1;
             GetBitContext gb = s->gb;
 
             skip_bits(&s->gb, 1);
@@ -141,17 +146,22 @@ static inline int mpeg4_is_resync(MpegEncContext *s)
                 if (get_bits1(&s->gb))
                     break;
 
+            mb_num = get_bits(&s->gb, mb_num_bits);
+            if (!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
+                mb_num= -1;
+
             s->gb = gb;
 
             if (len >= ff_mpeg4_get_video_packet_prefix_length(s))
-                return 1;
+                return mb_num;
         }
     }
     return 0;
 }
 
-static int mpeg4_decode_sprite_trajectory(MpegEncContext *s, GetBitContext *gb)
+static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
 {
+    MpegEncContext *s = &ctx->m;
     int a     = 2 << s->sprite_warping_accuracy;
     int rho   = 3  - s->sprite_warping_accuracy;
     int r     = 16 / a;
@@ -171,7 +181,7 @@ static int mpeg4_decode_sprite_trajectory(MpegEncContext *s, GetBitContext *gb)
     if (w <= 0 || h <= 0)
         return AVERROR_INVALIDDATA;
 
-    for (i = 0; i < s->num_sprite_warping_points; i++) {
+    for (i = 0; i < ctx->num_sprite_warping_points; i++) {
         int length;
         int x = 0, y = 0;
 
@@ -179,7 +189,7 @@ static int mpeg4_decode_sprite_trajectory(MpegEncContext *s, GetBitContext *gb)
         if (length)
             x = get_xbits(gb, length);
 
-        if (!(s->divx_version == 500 && s->divx_build == 413))
+        if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
             skip_bits1(gb);     /* marker bit */
 
         length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
@@ -187,11 +197,11 @@ static int mpeg4_decode_sprite_trajectory(MpegEncContext *s, GetBitContext *gb)
             y = get_xbits(gb, length);
 
         skip_bits1(gb);         /* marker bit */
-        s->sprite_traj[i][0] = d[i][0] = x;
-        s->sprite_traj[i][1] = d[i][1] = y;
+        ctx->sprite_traj[i][0] = d[i][0] = x;
+        ctx->sprite_traj[i][1] = d[i][1] = y;
     }
     for (; i < 4; i++)
-        s->sprite_traj[i][0] = s->sprite_traj[i][1] = 0;
+        ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0;
 
     while ((1 << alpha) < w)
         alpha++;
@@ -201,7 +211,7 @@ static int mpeg4_decode_sprite_trajectory(MpegEncContext *s, GetBitContext *gb)
     h2 = 1 << beta;
 
     // Note, the 4th point isn't used for GMC
-    if (s->divx_version == 500 && s->divx_build == 413) {
+    if (ctx->divx_version == 500 && ctx->divx_build == 413) {
         sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
         sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
         sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
@@ -239,7 +249,7 @@ static int mpeg4_decode_sprite_trajectory(MpegEncContext *s, GetBitContext *gb)
                         ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16 * vop_ref[0][1]) +
                                      h2 * (r * sprite_ref[2][1] - 16 * vop_ref[2][1])), h);
 
-    switch (s->num_sprite_warping_points) {
+    switch (ctx->num_sprite_warping_points) {
     case 0:
         s->sprite_offset[0][0] =
         s->sprite_offset[0][1] =
@@ -249,8 +259,8 @@ static int mpeg4_decode_sprite_trajectory(MpegEncContext *s, GetBitContext *gb)
         s->sprite_delta[0][1]  =
         s->sprite_delta[1][0]  = 0;
         s->sprite_delta[1][1]  = a;
-        s->sprite_shift[0]     =
-        s->sprite_shift[1]     = 0;
+        ctx->sprite_shift[0]   =
+        ctx->sprite_shift[1]   = 0;
         break;
     case 1:     // GMC only
         s->sprite_offset[0][0] = sprite_ref[0][0] - a * vop_ref[0][0];
@@ -263,8 +273,8 @@ static int mpeg4_decode_sprite_trajectory(MpegEncContext *s, GetBitContext *gb)
         s->sprite_delta[0][1]  =
         s->sprite_delta[1][0]  = 0;
         s->sprite_delta[1][1]  = a;
-        s->sprite_shift[0]     =
-        s->sprite_shift[1]     = 0;
+        ctx->sprite_shift[0]   =
+        ctx->sprite_shift[1]   = 0;
         break;
     case 2:
         s->sprite_offset[0][0] = (sprite_ref[0][0] << (alpha + rho)) +
@@ -292,8 +302,8 @@ static int mpeg4_decode_sprite_trajectory(MpegEncContext *s, GetBitContext *gb)
         s->sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
         s->sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
 
-        s->sprite_shift[0] = alpha + rho;
-        s->sprite_shift[1] = alpha + rho + 2;
+        ctx->sprite_shift[0]  = alpha + rho;
+        ctx->sprite_shift[1]  = alpha + rho + 2;
         break;
     case 3:
         min_ab = FFMIN(alpha, beta);
@@ -328,42 +338,53 @@ static int mpeg4_decode_sprite_trajectory(MpegEncContext *s, GetBitContext *gb)
         s->sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3;
         s->sprite_delta[1][1] = (-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3;
 
-        s->sprite_shift[0] = alpha + beta + rho - min_ab;
-        s->sprite_shift[1] = alpha + beta + rho - min_ab + 2;
+        ctx->sprite_shift[0]  = alpha + beta + rho - min_ab;
+        ctx->sprite_shift[1]  = alpha + beta + rho - min_ab + 2;
         break;
     }
     /* try to simplify the situation */
-    if (s->sprite_delta[0][0] == a << s->sprite_shift[0] &&
+    if (s->sprite_delta[0][0] == a << ctx->sprite_shift[0] &&
         s->sprite_delta[0][1] == 0 &&
         s->sprite_delta[1][0] == 0 &&
-        s->sprite_delta[1][1] == a << s->sprite_shift[0]) {
-        s->sprite_offset[0][0] >>= s->sprite_shift[0];
-        s->sprite_offset[0][1] >>= s->sprite_shift[0];
-        s->sprite_offset[1][0] >>= s->sprite_shift[1];
-        s->sprite_offset[1][1] >>= s->sprite_shift[1];
+        s->sprite_delta[1][1] == a << ctx->sprite_shift[0]) {
+        s->sprite_offset[0][0] >>= ctx->sprite_shift[0];
+        s->sprite_offset[0][1] >>= ctx->sprite_shift[0];
+        s->sprite_offset[1][0] >>= ctx->sprite_shift[1];
+        s->sprite_offset[1][1] >>= ctx->sprite_shift[1];
         s->sprite_delta[0][0] = a;
         s->sprite_delta[0][1] = 0;
         s->sprite_delta[1][0] = 0;
         s->sprite_delta[1][1] = a;
-        s->sprite_shift[0] = 0;
-        s->sprite_shift[1] = 0;
+        ctx->sprite_shift[0] = 0;
+        ctx->sprite_shift[1] = 0;
         s->real_sprite_warping_points = 1;
     } else {
-        int shift_y = 16 - s->sprite_shift[0];
-        int shift_c = 16 - s->sprite_shift[1];
+        int shift_y = 16 - ctx->sprite_shift[0];
+        int shift_c = 16 - ctx->sprite_shift[1];
         for (i = 0; i < 2; i++) {
             s->sprite_offset[0][i] <<= shift_y;
             s->sprite_offset[1][i] <<= shift_c;
             s->sprite_delta[0][i]  <<= shift_y;
             s->sprite_delta[1][i]  <<= shift_y;
-            s->sprite_shift[i]       = 16;
+            ctx->sprite_shift[i]     = 16;
         }
-        s->real_sprite_warping_points = s->num_sprite_warping_points;
+        s->real_sprite_warping_points = ctx->num_sprite_warping_points;
     }
 
     return 0;
 }
 
+static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb) {
+    int len = FFMIN(ctx->time_increment_bits + 3, 15);
+
+    get_bits(gb, len);
+    if (get_bits1(gb))
+        get_bits(gb, len);
+    check_marker(gb, "after new_pred");
+
+    return 0;
+}
+
 /**
  * Decode the next video packet.
  * @return <0 if something went wrong
@@ -399,19 +420,6 @@ int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
                "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
         return -1;
     }
-    if (s->pict_type == AV_PICTURE_TYPE_B) {
-        int mb_x = 0, mb_y = 0;
-
-        while (s->next_picture.mbskip_table[s->mb_index2xy[mb_num]]) {
-            if (!mb_x)
-                ff_thread_await_progress(&s->next_picture_ptr->tf, mb_y++, 0);
-            mb_num++;
-            if (++mb_x == s->mb_width)
-                mb_x = 0;
-        }
-        if (mb_num >= s->mb_num)
-            return -1;  // slice contains just skipped MBs (already decoded)
-    }
 
     s->mb_x = mb_num % s->mb_width;
     s->mb_y = mb_num / s->mb_width;
@@ -442,8 +450,8 @@ int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
             skip_bits(&s->gb, 3); /* intra dc vlc threshold */
             // FIXME don't just ignore everything
             if (s->pict_type == AV_PICTURE_TYPE_S &&
-                s->vol_sprite_usage == GMC_SPRITE) {
-                if (mpeg4_decode_sprite_trajectory(s, &s->gb) < 0)
+                ctx->vol_sprite_usage == GMC_SPRITE) {
+                if (mpeg4_decode_sprite_trajectory(ctx, &s->gb) < 0)
                     return AVERROR_INVALIDDATA;
                 av_log(s->avctx, AV_LOG_ERROR, "untested\n");
             }
@@ -464,7 +472,8 @@ int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
             }
         }
     }
-    // FIXME new-pred stuff
+    if (ctx->new_pred)
+        decode_new_pred(ctx, &s->gb);
 
     return 0;
 }
@@ -474,8 +483,9 @@ int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
  * @param n either 0 for the x component or 1 for y
  * @return the average MV for a GMC MB
  */
-static inline int get_amv(MpegEncContext *s, int n)
+static inline int get_amv(Mpeg4DecContext *ctx, int n)
 {
+    MpegEncContext *s = &ctx->m;
     int x, y, mb_v, sum, dx, dy, shift;
     int len     = 1 << (s->f_code + 4);
     const int a = s->sprite_warping_accuracy;
@@ -484,14 +494,14 @@ static inline int get_amv(MpegEncContext *s, int n)
         len >>= s->quarter_sample;
 
     if (s->real_sprite_warping_points == 1) {
-        if (s->divx_version == 500 && s->divx_build == 413)
+        if (ctx->divx_version == 500 && ctx->divx_build == 413)
             sum = s->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
         else
             sum = RSHIFT(s->sprite_offset[0][n] << s->quarter_sample, a);
     } else {
         dx    = s->sprite_delta[n][0];
         dy    = s->sprite_delta[n][1];
-        shift = s->sprite_shift[0];
+        shift = ctx->sprite_shift[0];
         if (n)
             dy -= 1 << (shift + a + 1);
         else
@@ -558,7 +568,7 @@ static inline int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
 
         if (code > 8) {
             if (get_bits1(&s->gb) == 0) { /* marker */
-                if (s->err_recognition & AV_EF_BITSTREAM) {
+                if (s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)) {
                     av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
                     return -1;
                 }
@@ -573,8 +583,9 @@ static inline int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
  * Decode first partition.
  * @return number of MBs decoded or <0 if an error occurred
  */
-static int mpeg4_decode_partition_a(MpegEncContext *s)
+static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
 {
+    MpegEncContext *s = &ctx->m;
     int mb_num = 0;
     static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
 
@@ -644,13 +655,13 @@ try_again:
                 if (bits & 0x10000) {
                     /* skip mb */
                     if (s->pict_type == AV_PICTURE_TYPE_S &&
-                        s->vol_sprite_usage == GMC_SPRITE) {
+                        ctx->vol_sprite_usage == GMC_SPRITE) {
                         s->current_picture.mb_type[xy] = MB_TYPE_SKIP  |
                                                          MB_TYPE_16x16 |
                                                          MB_TYPE_GMC   |
                                                          MB_TYPE_L0;
-                        mx = get_amv(s, 0);
-                        my = get_amv(s, 1);
+                        mx = get_amv(ctx, 0);
+                        my = get_amv(ctx, 1);
                     } else {
                         s->current_picture.mb_type[xy] = MB_TYPE_SKIP  |
                                                          MB_TYPE_16x16 |
@@ -700,7 +711,7 @@ try_again:
                         ff_clean_intra_table_entries(s);
 
                     if (s->pict_type == AV_PICTURE_TYPE_S &&
-                        s->vol_sprite_usage == GMC_SPRITE &&
+                        ctx->vol_sprite_usage == GMC_SPRITE &&
                         (cbpc & 16) == 0)
                         s->mcsel = get_bits1(&s->gb);
                     else
@@ -721,8 +732,8 @@ try_again:
                             s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
                                                              MB_TYPE_L0;
                         } else {
-                            mx = get_amv(s, 0);
-                            my = get_amv(s, 1);
+                            mx = get_amv(ctx, 0);
+                            my = get_amv(ctx, 1);
                             s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
                                                              MB_TYPE_GMC   |
                                                              MB_TYPE_L0;
@@ -859,13 +870,14 @@ static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
  * Decode the first and second partition.
  * @return <0 if error (and sets error type in the error_status_table)
  */
-int ff_mpeg4_decode_partitions(MpegEncContext *s)
+int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx)
 {
+    MpegEncContext *s = &ctx->m;
     int mb_num;
     const int part_a_error = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR;
     const int part_a_end   = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END   | ER_MV_END)   : ER_MV_END;
 
-    mb_num = mpeg4_decode_partition_a(s);
+    mb_num = mpeg4_decode_partition_a(ctx);
     if (mb_num < 0) {
         ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
                         s->mb_x, s->mb_y, part_a_error);
@@ -921,10 +933,12 @@ int ff_mpeg4_decode_partitions(MpegEncContext *s)
  * Decode a block.
  * @return <0 if an error occurred
  */
-static inline int mpeg4_decode_block(MpegEncContext *s, int16_t *block,
+static inline int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block,
                                      int n, int coded, int intra, int rvlc)
 {
-    int level, i, last, run, qmul, qadd, dc_pred_dir;
+    MpegEncContext *s = &ctx->m;
+    int level, i, last, run, qmul, qadd;
+    int av_uninit(dc_pred_dir);
     RLTable *rl;
     RL_VLC_ELEM *rl_vlc;
     const uint8_t *scan_table;
@@ -932,7 +946,7 @@ static inline int mpeg4_decode_block(MpegEncContext *s, int16_t *block,
     // Note intra & rvlc should be optimized away if this is inlined
 
     if (intra) {
-        if (s->use_intra_dc_vlc) {
+        if (ctx->use_intra_dc_vlc) {
             /* DC coef */
             if (s->partitioned_frame) {
                 level = s->dc_val[0][s->block_index[n]];
@@ -1085,13 +1099,35 @@ static inline int mpeg4_decode_block(MpegEncContext *s, int16_t *block,
                                 SKIP_COUNTER(re, &s->gb, 1 + 12 + 1);
                             }
 
+#if 0
+                            if (s->error_recognition >= FF_ER_COMPLIANT) {
+                                const int abs_level= FFABS(level);
+                                if (abs_level<=MAX_LEVEL && run<=MAX_RUN) {
+                                    const int run1= run - rl->max_run[last][abs_level] - 1;
+                                    if (abs_level <= rl->max_level[last][run]) {
+                                        av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
+                                        return -1;
+                                    }
+                                    if (s->error_recognition > FF_ER_COMPLIANT) {
+                                        if (abs_level <= rl->max_level[last][run]*2) {
+                                            av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
+                                            return -1;
+                                        }
+                                        if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) {
+                                            av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
+                                            return -1;
+                                        }
+                                    }
+                                }
+                            }
+#endif
                             if (level > 0)
                                 level = level * qmul + qadd;
                             else
                                 level = level * qmul - qadd;
 
                             if ((unsigned)(level + 2048) > 4095) {
-                                if (s->err_recognition & AV_EF_BITSTREAM) {
+                                if (s->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)) {
                                     if (level > 2560 || level < -2560) {
                                         av_log(s->avctx, AV_LOG_ERROR,
                                                "|level| overflow in 3. esc, qp=%d\n",
@@ -1147,7 +1183,7 @@ static inline int mpeg4_decode_block(MpegEncContext *s, int16_t *block,
 
 not_coded:
     if (intra) {
-        if (!s->use_intra_dc_vlc) {
+        if (!ctx->use_intra_dc_vlc) {
             block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
 
             i -= i >> 31;  // if (i == -1) i = 0;
@@ -1167,13 +1203,14 @@ not_coded:
  */
 static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
 {
+    Mpeg4DecContext *ctx = (Mpeg4DecContext *)s;
     int cbp, mb_type;
     const int xy = s->mb_x + s->mb_y * s->mb_stride;
 
     mb_type = s->current_picture.mb_type[xy];
     cbp     = s->cbp_table[xy];
 
-    s->use_intra_dc_vlc = s->qscale < s->intra_dc_threshold;
+    ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
 
     if (s->current_picture.qscale_table[xy] != s->qscale)
         ff_set_qscale(s, s->current_picture.qscale_table[xy]);
@@ -1194,7 +1231,7 @@ static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
             s->mv_dir  = MV_DIR_FORWARD;
             s->mv_type = MV_TYPE_16X16;
             if (s->pict_type == AV_PICTURE_TYPE_S
-                && s->vol_sprite_usage == GMC_SPRITE) {
+                && ctx->vol_sprite_usage == GMC_SPRITE) {
                 s->mcsel      = 1;
                 s->mb_skipped = 0;
             } else {
@@ -1223,7 +1260,7 @@ static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
         s->dsp.clear_blocks(s->block[0]);
         /* decode each block */
         for (i = 0; i < 6; i++) {
-            if (mpeg4_decode_block(s, block[i], i, cbp & 32, s->mb_intra, s->rvlc) < 0) {
+            if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, s->mb_intra, ctx->rvlc) < 0) {
                 av_log(s->avctx, AV_LOG_ERROR,
                        "texture corrupted at %d %d %d\n",
                        s->mb_x, s->mb_y, s->mb_intra);
@@ -1235,12 +1272,12 @@ static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
 
     /* per-MB end of slice check */
     if (--s->mb_num_left <= 0) {
-        if (mpeg4_is_resync(s))
+        if (mpeg4_is_resync(ctx))
             return SLICE_END;
         else
             return SLICE_NOEND;
     } else {
-        if (mpeg4_is_resync(s)) {
+        if (mpeg4_is_resync(ctx)) {
             const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
             if (s->cbp_table[xy + delta])
                 return SLICE_END;
@@ -1251,12 +1288,13 @@ static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
 
 static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
 {
+    Mpeg4DecContext *ctx = (Mpeg4DecContext *)s;
     int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
     int16_t *mot_val;
     static int8_t quant_tab[4] = { -1, -2, 1, 2 };
     const int xy = s->mb_x + s->mb_y * s->mb_stride;
 
-    assert(s->h263_pred);
+    av_assert2(s->h263_pred);
 
     if (s->pict_type == AV_PICTURE_TYPE_P ||
         s->pict_type == AV_PICTURE_TYPE_S) {
@@ -1269,14 +1307,14 @@ static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
                 s->mv_dir  = MV_DIR_FORWARD;
                 s->mv_type = MV_TYPE_16X16;
                 if (s->pict_type == AV_PICTURE_TYPE_S &&
-                    s->vol_sprite_usage == GMC_SPRITE) {
+                    ctx->vol_sprite_usage == GMC_SPRITE) {
                     s->current_picture.mb_type[xy] = MB_TYPE_SKIP  |
                                                      MB_TYPE_GMC   |
                                                      MB_TYPE_16x16 |
                                                      MB_TYPE_L0;
                     s->mcsel       = 1;
-                    s->mv[0][0][0] = get_amv(s, 0);
-                    s->mv[0][0][1] = get_amv(s, 1);
+                    s->mv[0][0][0] = get_amv(ctx, 0);
+                    s->mv[0][0][1] = get_amv(ctx, 1);
                     s->mb_skipped  = 0;
                 } else {
                     s->current_picture.mb_type[xy] = MB_TYPE_SKIP  |
@@ -1304,7 +1342,7 @@ static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
             goto intra;
 
         if (s->pict_type == AV_PICTURE_TYPE_S &&
-            s->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
+            ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
             s->mcsel = get_bits1(&s->gb);
         else
             s->mcsel = 0;
@@ -1325,8 +1363,8 @@ static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
                                                  MB_TYPE_L0;
                 /* 16x16 global motion prediction */
                 s->mv_type     = MV_TYPE_16X16;
-                mx             = get_amv(s, 0);
-                my             = get_amv(s, 1);
+                mx             = get_amv(ctx, 0);
+                my             = get_amv(ctx, 1);
                 s->mv[0][0][0] = mx;
                 s->mv[0][0][1] = my;
             } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
@@ -1570,7 +1608,7 @@ intra:
         }
         cbp = (cbpc & 3) | (cbpy << 2);
 
-        s->use_intra_dc_vlc = s->qscale < s->intra_dc_threshold;
+        ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
 
         if (dquant)
             ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
@@ -1581,7 +1619,7 @@ intra:
         s->dsp.clear_blocks(s->block[0]);
         /* decode each block */
         for (i = 0; i < 6; i++) {
-            if (mpeg4_decode_block(s, block[i], i, cbp & 32, 1, 0) < 0)
+            if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 1, 0) < 0)
                 return -1;
             cbp += cbp;
         }
@@ -1590,7 +1628,7 @@ intra:
 
     /* decode each block */
     for (i = 0; i < 6; i++) {
-        if (mpeg4_decode_block(s, block[i], i, cbp & 32, 0, 0) < 0)
+        if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 0, 0) < 0)
             return -1;
         cbp += cbp;
     }
@@ -1598,20 +1636,23 @@ intra:
 end:
     /* per-MB end of slice check */
     if (s->codec_id == AV_CODEC_ID_MPEG4) {
-        if (mpeg4_is_resync(s)) {
-            const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
+        int next = mpeg4_is_resync(ctx);
+        if (next) {
+            if        (s->mb_x + s->mb_y*s->mb_width + 1 >  next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
+                return -1;
+            } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
+                return SLICE_END;
 
-            if (s->pict_type == AV_PICTURE_TYPE_B &&
-                s->next_picture.mbskip_table[xy + delta]) {
+            if (s->pict_type == AV_PICTURE_TYPE_B) {
+                const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
                 ff_thread_await_progress(&s->next_picture_ptr->tf,
                                          (s->mb_x + delta >= s->mb_width)
                                          ? FFMIN(s->mb_y + 1, s->mb_height - 1)
                                          : s->mb_y, 0);
+                if (s->next_picture.mbskip_table[xy + delta])
+                    return SLICE_OK;
             }
 
-            if (s->pict_type == AV_PICTURE_TYPE_B &&
-                s->next_picture.mbskip_table[xy + delta])
-                return SLICE_OK;
             return SLICE_END;
         }
     }
@@ -1622,29 +1663,30 @@ end:
 static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
 {
     int hours, minutes, seconds;
-    unsigned time_code = show_bits(gb, 18);
-
-    if (time_code & 0x40) {     /* marker_bit */
-        hours   = time_code >> 13;
-        minutes = time_code >> 7 & 0x3f;
-        seconds = time_code & 0x3f;
-        s->time_base = seconds + 60 * (minutes + 60 * hours);
-        skip_bits(gb, 20);      /* time_code, closed_gov, broken_link */
-    } else {
-        av_log(s->avctx, AV_LOG_WARNING, "GOP header missing marker_bit\n");
+
+    if (!show_bits(gb, 23)) {
+        av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
+        return -1;
     }
 
+    hours   = get_bits(gb, 5);
+    minutes = get_bits(gb, 6);
+    skip_bits1(gb);
+    seconds = get_bits(gb, 6);
+
+    s->time_base = seconds + 60*(minutes + 60*hours);
+
+    skip_bits1(gb);
+    skip_bits1(gb);
+
     return 0;
 }
 
 static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb)
 {
-    int profile_and_level_indication;
 
-    profile_and_level_indication = get_bits(gb, 8);
-
-    s->avctx->profile = (profile_and_level_indication & 0xf0) >> 4;
-    s->avctx->level   = (profile_and_level_indication & 0x0f);
+    s->avctx->profile = get_bits(gb, 4);
+    s->avctx->level   = get_bits(gb, 4);
 
     // for Simple profile, level 0
     if (s->avctx->profile == 0 && s->avctx->level == 8) {
@@ -1730,15 +1772,15 @@ static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
     else
         s->avctx->time_base.num = 1;
 
-    s->t_frame = 0;
+    ctx->t_frame = 0;
 
     if (ctx->shape != BIN_ONLY_SHAPE) {
         if (ctx->shape == RECT_SHAPE) {
-            skip_bits1(gb);   /* marker */
+            check_marker(gb, "before width");
             width = get_bits(gb, 13);
-            skip_bits1(gb);   /* marker */
+            check_marker(gb, "before height");
             height = get_bits(gb, 13);
-            skip_bits1(gb);   /* marker */
+            check_marker(gb, "after height");
             if (width && height &&  /* they should be non zero but who knows */
                 !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
                 if (s->width && s->height &&
@@ -1756,36 +1798,36 @@ static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
             av_log(s->avctx, AV_LOG_INFO,           /* OBMC Disable */
                    "MPEG4 OBMC not supported (very likely buggy encoder)\n");
         if (vo_ver_id == 1)
-            s->vol_sprite_usage = get_bits1(gb);    /* vol_sprite_usage */
+            ctx->vol_sprite_usage = get_bits1(gb);    /* vol_sprite_usage */
         else
-            s->vol_sprite_usage = get_bits(gb, 2);  /* vol_sprite_usage */
+            ctx->vol_sprite_usage = get_bits(gb, 2);  /* vol_sprite_usage */
 
-        if (s->vol_sprite_usage == STATIC_SPRITE)
+        if (ctx->vol_sprite_usage == STATIC_SPRITE)
             av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
-        if (s->vol_sprite_usage == STATIC_SPRITE ||
-            s->vol_sprite_usage == GMC_SPRITE) {
-            if (s->vol_sprite_usage == STATIC_SPRITE) {
-                s->sprite_width = get_bits(gb, 13);
+        if (ctx->vol_sprite_usage == STATIC_SPRITE ||
+            ctx->vol_sprite_usage == GMC_SPRITE) {
+            if (ctx->vol_sprite_usage == STATIC_SPRITE) {
+                skip_bits(gb, 13); // sprite_width
                 skip_bits1(gb); /* marker */
-                s->sprite_height = get_bits(gb, 13);
+                skip_bits(gb, 13); // sprite_height
                 skip_bits1(gb); /* marker */
-                s->sprite_left = get_bits(gb, 13);
+                skip_bits(gb, 13); // sprite_left
                 skip_bits1(gb); /* marker */
-                s->sprite_top = get_bits(gb, 13);
+                skip_bits(gb, 13); // sprite_top
                 skip_bits1(gb); /* marker */
             }
-            s->num_sprite_warping_points = get_bits(gb, 6);
-            if (s->num_sprite_warping_points > 3) {
+            ctx->num_sprite_warping_points = get_bits(gb, 6);
+            if (ctx->num_sprite_warping_points > 3) {
                 av_log(s->avctx, AV_LOG_ERROR,
                        "%d sprite_warping_points\n",
-                       s->num_sprite_warping_points);
-                s->num_sprite_warping_points = 0;
+                       ctx->num_sprite_warping_points);
+                ctx->num_sprite_warping_points = 0;
                 return -1;
             }
             s->sprite_warping_accuracy  = get_bits(gb, 2);
-            s->sprite_brightness_change = get_bits1(gb);
-            if (s->vol_sprite_usage == STATIC_SPRITE)
-                s->low_latency_sprite = get_bits1(gb);
+            ctx->sprite_brightness_change = get_bits1(gb);
+            if (ctx->vol_sprite_usage == STATIC_SPRITE)
+                skip_bits1(gb); // low_latency_sprite
         }
         // FIXME sadct disable bit if verid!=1 && shape not rect
 
@@ -1796,6 +1838,9 @@ static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
             if (s->quant_precision != 5)
                 av_log(s->avctx, AV_LOG_ERROR,
                        "quant precision %d\n", s->quant_precision);
+            if (s->quant_precision<3 || s->quant_precision>9) {
+                s->quant_precision = 5;
+            }
         } else {
             s->quant_precision = 5;
         }
@@ -1876,44 +1921,44 @@ static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
             int estimation_method = get_bits(gb, 2);
             if (estimation_method < 2) {
                 if (!get_bits1(gb)) {
-                    s->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* opaque */
-                    s->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* transparent */
-                    s->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* intra_cae */
-                    s->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* inter_cae */
-                    s->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* no_update */
-                    s->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* upampling */
+                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* opaque */
+                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* transparent */
+                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* intra_cae */
+                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* inter_cae */
+                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* no_update */
+                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* upampling */
                 }
                 if (!get_bits1(gb)) {
-                    s->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* intra_blocks */
-                    s->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* inter_blocks */
-                    s->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* inter4v_blocks */
-                    s->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* not coded blocks */
+                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* intra_blocks */
+                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* inter_blocks */
+                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* inter4v_blocks */
+                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* not coded blocks */
                 }
                 if (!check_marker(gb, "in complexity estimation part 1")) {
                     skip_bits_long(gb, pos - get_bits_count(gb));
                     goto no_cplx_est;
                 }
                 if (!get_bits1(gb)) {
-                    s->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* dct_coeffs */
-                    s->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* dct_lines */
-                    s->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* vlc_syms */
-                    s->cplx_estimation_trash_i += 4 * get_bits1(gb);  /* vlc_bits */
+                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* dct_coeffs */
+                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* dct_lines */
+                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* vlc_syms */
+                    ctx->cplx_estimation_trash_i += 4 * get_bits1(gb);  /* vlc_bits */
                 }
                 if (!get_bits1(gb)) {
-                    s->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* apm */
-                    s->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* npm */
-                    s->cplx_estimation_trash_b += 8 * get_bits1(gb);  /* interpolate_mc_q */
-                    s->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* forwback_mc_q */
-                    s->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* halfpel2 */
-                    s->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* halfpel4 */
+                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* apm */
+                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* npm */
+                    ctx->cplx_estimation_trash_b += 8 * get_bits1(gb);  /* interpolate_mc_q */
+                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* forwback_mc_q */
+                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* halfpel2 */
+                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* halfpel4 */
                 }
                 if (!check_marker(gb, "in complexity estimation part 2")) {
                     skip_bits_long(gb, pos - get_bits_count(gb));
                     goto no_cplx_est;
                 }
                 if (estimation_method == 1) {
-                    s->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* sadct */
-                    s->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* qpel */
+                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* sadct */
+                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* qpel */
                 }
             } else
                 av_log(s->avctx, AV_LOG_ERROR,
@@ -1922,56 +1967,54 @@ static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
         } else {
 
 no_cplx_est:
-            s->cplx_estimation_trash_i =
-            s->cplx_estimation_trash_p =
-            s->cplx_estimation_trash_b = 0;
+            ctx->cplx_estimation_trash_i =
+            ctx->cplx_estimation_trash_p =
+            ctx->cplx_estimation_trash_b = 0;
         }
 
-        s->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
+        ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
 
         s->data_partitioning = get_bits1(gb);
         if (s->data_partitioning)
-            s->rvlc = get_bits1(gb);
+            ctx->rvlc = get_bits1(gb);
 
         if (vo_ver_id != 1) {
-            s->new_pred = get_bits1(gb);
-            if (s->new_pred) {
+            ctx->new_pred = get_bits1(gb);
+            if (ctx->new_pred) {
                 av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
                 skip_bits(gb, 2); /* requested upstream message type */
                 skip_bits1(gb);   /* newpred segment type */
             }
-            s->reduced_res_vop = get_bits1(gb);
-            if (s->reduced_res_vop)
+            if (get_bits1(gb)) // reduced_res_vop
                 av_log(s->avctx, AV_LOG_ERROR,
                        "reduced resolution VOP not supported\n");
         } else {
-            s->new_pred        = 0;
-            s->reduced_res_vop = 0;
+            ctx->new_pred = 0;
         }
 
-        s->scalability = get_bits1(gb);
+        ctx->scalability = get_bits1(gb);
 
-        if (s->scalability) {
+        if (ctx->scalability) {
             GetBitContext bak = *gb;
             int h_sampling_factor_n;
             int h_sampling_factor_m;
             int v_sampling_factor_n;
             int v_sampling_factor_m;
 
-            s->hierachy_type = get_bits1(gb);
+            skip_bits1(gb);    // hierarchy_type
             skip_bits(gb, 4);  /* ref_layer_id */
             skip_bits1(gb);    /* ref_layer_sampling_dir */
             h_sampling_factor_n = get_bits(gb, 5);
             h_sampling_factor_m = get_bits(gb, 5);
             v_sampling_factor_n = get_bits(gb, 5);
             v_sampling_factor_m = get_bits(gb, 5);
-            s->enhancement_type = get_bits1(gb);
+            ctx->enhancement_type = get_bits1(gb);
 
             if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
                 v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
                 /* illegal scalability header (VERY broken encoder),
                  * trying to workaround */
-                s->scalability = 0;
+                ctx->scalability = 0;
                 *gb            = bak;
             } else
                 av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
@@ -1980,6 +2023,17 @@ no_cplx_est:
         }
     }
 
+    if (s->avctx->debug&FF_DEBUG_PICT_INFO) {
+        av_log(s->avctx, AV_LOG_DEBUG, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d,  %s%s%s%s\n",
+               s->avctx->time_base.num, s->avctx->time_base.den,
+               ctx->time_increment_bits,
+               s->quant_precision,
+               s->progressive_sequence,
+               ctx->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
+               s->data_partitioning ? "partition " : "", ctx->rvlc ? "rvlc " : ""
+        );
+    }
+
     return 0;
 }
 
@@ -1987,8 +2041,9 @@ no_cplx_est:
  * Decode the user data stuff in the header.
  * Also initializes divx/xvid/lavc_version/build.
  */
-static int decode_user_data(MpegEncContext *s, GetBitContext *gb)
+static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
 {
+    MpegEncContext *s = &ctx->m;
     char buf[256];
     int i;
     int e;
@@ -2007,13 +2062,14 @@ static int decode_user_data(MpegEncContext *s, GetBitContext *gb)
     if (e < 2)
         e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
     if (e >= 2) {
-        s->divx_version = ver;
-        s->divx_build   = build;
+        ctx->divx_version = ver;
+        ctx->divx_build   = build;
         s->divx_packed  = e == 3 && last == 'p';
-        if (s->divx_packed && !s->showed_packed_warning) {
-            av_log(s->avctx, AV_LOG_WARNING,
-                   "Invalid and inefficient vfw-avi packed B frames detected\n");
-            s->showed_packed_warning = 1;
+        if (s->divx_packed && !ctx->showed_packed_warning) {
+            av_log(s->avctx, AV_LOG_INFO, "Video uses a non-standard and "
+                   "wasteful way to store B-frames ('packed B-frames'). "
+                   "Consider using a tool like VirtualDub or avidemux to fix it.\n");
+            ctx->showed_packed_warning = 1;
         }
     }
 
@@ -2028,23 +2084,137 @@ static int decode_user_data(MpegEncContext *s, GetBitContext *gb)
     }
     if (e != 4) {
         if (strcmp(buf, "ffmpeg") == 0)
-            s->lavc_build = 4600;
+            ctx->lavc_build = 4600;
     }
     if (e == 4)
-        s->lavc_build = build;
+        ctx->lavc_build = build;
 
     /* Xvid detection */
     e = sscanf(buf, "XviD%d", &build);
     if (e == 1)
-        s->xvid_build = build;
+        ctx->xvid_build = build;
 
     return 0;
 }
 
+int ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
+{
+    Mpeg4DecContext *ctx = avctx->priv_data;
+    MpegEncContext *s = &ctx->m;
+
+    if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
+        if (s->stream_codec_tag == AV_RL32("XVID") ||
+            s->codec_tag        == AV_RL32("XVID") ||
+            s->codec_tag        == AV_RL32("XVIX") ||
+            s->codec_tag        == AV_RL32("RMP4") ||
+            s->codec_tag        == AV_RL32("ZMP4") ||
+            s->codec_tag        == AV_RL32("SIPP"))
+            ctx->xvid_build = 0;
+    }
+
+    if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
+        if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
+            s->vol_control_parameters == 0)
+            ctx->divx_version = 400;  // divx 4
+
+    if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
+        ctx->divx_version =
+        ctx->divx_build   = -1;
+    }
+
+    if (s->workaround_bugs & FF_BUG_AUTODETECT) {
+        if (s->codec_tag == AV_RL32("XVIX"))
+            s->workaround_bugs |= FF_BUG_XVID_ILACE;
+
+        if (s->codec_tag == AV_RL32("UMP4"))
+            s->workaround_bugs |= FF_BUG_UMP4;
+
+        if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
+            s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
+
+        if (ctx->divx_version > 502 && ctx->divx_build < 1814)
+            s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
+
+        if (ctx->xvid_build <= 3U)
+            s->padding_bug_score = 256 * 256 * 256 * 64;
+
+        if (ctx->xvid_build <= 1U)
+            s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
+
+        if (ctx->xvid_build <= 12U)
+            s->workaround_bugs |= FF_BUG_EDGE;
+
+        if (ctx->xvid_build <= 32U)
+            s->workaround_bugs |= FF_BUG_DC_CLIP;
+
+#define SET_QPEL_FUNC(postfix1, postfix2)                           \
+    s->dsp.put_        ## postfix1 = ff_put_        ## postfix2;    \
+    s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;    \
+    s->dsp.avg_        ## postfix1 = ff_avg_        ## postfix2;
+
+        if (ctx->lavc_build < 4653U)
+            s->workaround_bugs |= FF_BUG_STD_QPEL;
+
+        if (ctx->lavc_build < 4655U)
+            s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
+
+        if (ctx->lavc_build < 4670U)
+            s->workaround_bugs |= FF_BUG_EDGE;
+
+        if (ctx->lavc_build <= 4712U)
+            s->workaround_bugs |= FF_BUG_DC_CLIP;
+
+        if (ctx->divx_version >= 0)
+            s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
+        if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
+            s->padding_bug_score = 256 * 256 * 256 * 64;
+
+        if (ctx->divx_version < 500U)
+            s->workaround_bugs |= FF_BUG_EDGE;
+
+        if (ctx->divx_version >= 0)
+            s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
+    }
+
+    if (s->workaround_bugs & FF_BUG_STD_QPEL) {
+        SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
+        SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
+        SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
+        SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
+        SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
+        SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
+
+        SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
+        SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
+        SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
+        SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
+        SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
+        SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
+    }
+
+    if (avctx->debug & FF_DEBUG_BUGS)
+        av_log(s->avctx, AV_LOG_DEBUG,
+               "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
+               s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
+               ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
+
+#if HAVE_MMX
+    if (s->codec_id == AV_CODEC_ID_MPEG4 && ctx->xvid_build >= 0 &&
+        avctx->idct_algo == FF_IDCT_AUTO &&
+        (av_get_cpu_flags() & AV_CPU_FLAG_MMX)) {
+        avctx->idct_algo = FF_IDCT_XVIDMMX;
+        ff_dct_common_init(s);
+        return 1;
+    }
+#endif
+    return 0;
+}
+
 static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
 {
     MpegEncContext *s = &ctx->m;
     int time_incr, time_increment;
+    int64_t pts;
 
     s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I;        /* pict type: I = 0 , P = 1 */
     if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
@@ -2075,7 +2245,7 @@ static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
              ctx->time_increment_bits++) {
             if (s->pict_type == AV_PICTURE_TYPE_P ||
                 (s->pict_type == AV_PICTURE_TYPE_S &&
-                 s->vol_sprite_usage == GMC_SPRITE)) {
+                 ctx->vol_sprite_usage == GMC_SPRITE)) {
                 if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
                     break;
             } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
@@ -2084,6 +2254,9 @@ static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
 
         av_log(s->avctx, AV_LOG_ERROR,
                "my guess is %d bits ;)\n", ctx->time_increment_bits);
+        if (s->avctx->time_base.den && 4*s->avctx->time_base.den < 1<<ctx->time_increment_bits) {
+            s->avctx->time_base.den = 1<<ctx->time_increment_bits;
+        }
     }
 
     if (IS_3IV1)
@@ -2116,20 +2289,28 @@ static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
         }
         ff_mpeg4_init_direct_mv(s);
 
-        if (s->t_frame == 0)
-            s->t_frame = s->pb_time;
-        if (s->t_frame == 0)
-            s->t_frame = 1;  // 1/0 protection
-        s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, s->t_frame) -
-                            ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame)) * 2;
-        s->pb_field_time = (ROUNDED_DIV(s->time, s->t_frame) -
-                            ROUNDED_DIV(s->last_non_b_time - s->pp_time, s->t_frame)) * 2;
+        if (ctx->t_frame == 0)
+            ctx->t_frame = s->pb_time;
+        if (ctx->t_frame == 0)
+            ctx->t_frame = 1;  // 1/0 protection
+        s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
+                            ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
+        s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
+                            ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
         if (!s->progressive_sequence) {
             if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1)
                 return FRAME_SKIPPED;
         }
     }
 
+    if (s->avctx->time_base.num)
+        pts = ROUNDED_DIV(s->time, s->avctx->time_base.num);
+    else
+        pts = AV_NOPTS_VALUE;
+    if (s->avctx->debug&FF_DEBUG_PTS)
+        av_log(s->avctx, AV_LOG_DEBUG, "MPEG4 PTS: %"PRId64"\n",
+               pts);
+
     check_marker(gb, "before vop_coded");
 
     /* vop coded */
@@ -2138,10 +2319,13 @@ static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
             av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
         return FRAME_SKIPPED;
     }
+    if (ctx->new_pred)
+        decode_new_pred(ctx, gb);
+
     if (ctx->shape != BIN_ONLY_SHAPE &&
                     (s->pict_type == AV_PICTURE_TYPE_P ||
                      (s->pict_type == AV_PICTURE_TYPE_S &&
-                      s->vol_sprite_usage == GMC_SPRITE))) {
+                      ctx->vol_sprite_usage == GMC_SPRITE))) {
         /* rounding type for motion estimation */
         s->no_rounding = get_bits1(gb);
     } else {
@@ -2150,7 +2334,7 @@ static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
     // FIXME reduced res stuff
 
     if (ctx->shape != RECT_SHAPE) {
-        if (s->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
+        if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
             skip_bits(gb, 13);  /* width */
             skip_bits1(gb);     /* marker */
             skip_bits(gb, 13);  /* height */
@@ -2168,13 +2352,17 @@ static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
     // FIXME complexity estimation stuff
 
     if (ctx->shape != BIN_ONLY_SHAPE) {
-        skip_bits_long(gb, s->cplx_estimation_trash_i);
+        skip_bits_long(gb, ctx->cplx_estimation_trash_i);
         if (s->pict_type != AV_PICTURE_TYPE_I)
-            skip_bits_long(gb, s->cplx_estimation_trash_p);
+            skip_bits_long(gb, ctx->cplx_estimation_trash_p);
         if (s->pict_type == AV_PICTURE_TYPE_B)
-            skip_bits_long(gb, s->cplx_estimation_trash_b);
+            skip_bits_long(gb, ctx->cplx_estimation_trash_b);
 
-        s->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
+        if (get_bits_left(gb) < 3) {
+            av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
+            return -1;
+        }
+        ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
         if (!s->progressive_sequence) {
             s->top_field_first = get_bits1(gb);
             s->alternate_scan  = get_bits1(gb);
@@ -2195,14 +2383,14 @@ static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
     }
 
     if (s->pict_type == AV_PICTURE_TYPE_S &&
-        (s->vol_sprite_usage == STATIC_SPRITE ||
-         s->vol_sprite_usage == GMC_SPRITE)) {
-        if (mpeg4_decode_sprite_trajectory(s, gb) < 0)
+        (ctx->vol_sprite_usage == STATIC_SPRITE ||
+         ctx->vol_sprite_usage == GMC_SPRITE)) {
+        if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
             return AVERROR_INVALIDDATA;
-        if (s->sprite_brightness_change)
+        if (ctx->sprite_brightness_change)
             av_log(s->avctx, AV_LOG_ERROR,
                    "sprite_brightness_change not supported\n");
-        if (s->vol_sprite_usage == STATIC_SPRITE)
+        if (ctx->vol_sprite_usage == STATIC_SPRITE)
             av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
     }
 
@@ -2219,6 +2407,7 @@ static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
             if (s->f_code == 0) {
                 av_log(s->avctx, AV_LOG_ERROR,
                        "Error, header damaged or not MPEG4 header (f_code=0)\n");
+                s->f_code = 1;
                 return -1;  // makes no sense to continue, as there is nothing left from the image then
             }
         } else
@@ -2226,29 +2415,38 @@ static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
 
         if (s->pict_type == AV_PICTURE_TYPE_B) {
             s->b_code = get_bits(gb, 3);
+            if (s->b_code == 0) {
+                av_log(s->avctx, AV_LOG_ERROR,
+                       "Error, header damaged or not MPEG4 header (b_code=0)\n");
+                s->b_code=1;
+                return -1; // makes no sense to continue, as the MV decoding will break very quickly
+            }
         } else
             s->b_code = 1;
 
         if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
             av_log(s->avctx, AV_LOG_DEBUG,
-                   "qp:%d fc:%d,%d %s size:%d pro:%d alt:%d top:%d %spel part:%d resync:%d w:%d a:%d rnd:%d vot:%d%s dc:%d ce:%d/%d/%d\n",
+                   "qp:%d fc:%d,%d %s size:%d pro:%d alt:%d top:%d %spel part:%d resync:%d w:%d a:%d rnd:%d vot:%d%s dc:%d ce:%d/%d/%d time:%"PRId64" tincr:%d\n",
                    s->qscale, s->f_code, s->b_code,
                    s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
-                   gb->size_in_bits, s->progressive_sequence, s->alternate_scan,
+                   gb->size_in_bits,s->progressive_sequence, s->alternate_scan,
                    s->top_field_first, s->quarter_sample ? "q" : "h",
-                   s->data_partitioning, s->resync_marker,
-                   s->num_sprite_warping_points, s->sprite_warping_accuracy,
+                   s->data_partitioning, ctx->resync_marker,
+                   ctx->num_sprite_warping_points, s->sprite_warping_accuracy,
                    1 - s->no_rounding, s->vo_type,
-                   s->vol_control_parameters ? " VOLC" : " ", s->intra_dc_threshold,
-                   s->cplx_estimation_trash_i, s->cplx_estimation_trash_p,
-                   s->cplx_estimation_trash_b);
+                   s->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
+                   ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
+                   ctx->cplx_estimation_trash_b,
+                   s->time,
+                   time_increment
+                  );
         }
 
-        if (!s->scalability) {
+        if (!ctx->scalability) {
             if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
                 skip_bits1(gb);  // vop shape coding type
         } else {
-            if (s->enhancement_type) {
+            if (ctx->enhancement_type) {
                 int load_backward_shape = get_bits1(gb);
                 if (load_backward_shape)
                     av_log(s->avctx, AV_LOG_ERROR,
@@ -2261,7 +2459,7 @@ static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
      * (divx4/xvid/opendivx). Note we cannot detect divx5 without b-frames
      * easily (although it's buggy too) */
     if (s->vo_type == 0 && s->vol_control_parameters == 0 &&
-        s->divx_version == -1 && s->picture_number == 0) {
+        ctx->divx_version == -1 && s->picture_number == 0) {
         av_log(s->avctx, AV_LOG_WARNING,
                "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
         s->low_delay = 1;
@@ -2304,8 +2502,8 @@ int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
     for (;;) {
         if (get_bits_count(gb) >= gb->size_in_bits) {
             if (gb->size_in_bits == 8 &&
-                (s->divx_version >= 0 || s->xvid_build >= 0)) {
-                av_log(s->avctx, AV_LOG_WARNING, "frame skip %d\n", gb->size_in_bits);
+                (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
+                av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
                 return FRAME_SKIPPED;  // divx bug
             } else
                 return -1;  // end of stream
@@ -2381,7 +2579,7 @@ int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
             if (decode_vol_header(ctx, gb) < 0)
                 return -1;
         } else if (startcode == USER_DATA_STARTCODE) {
-            decode_user_data(s, gb);
+            decode_user_data(ctx, gb);
         } else if (startcode == GOP_STARTCODE) {
             mpeg4_decode_gop_header(s, gb);
         } else if (startcode == VOS_STARTCODE) {
@@ -2402,6 +2600,74 @@ end:
     return decode_vop_header(ctx, gb);
 }
 
+av_cold void ff_mpeg4videodec_static_init(void) {
+    static int done = 0;
+
+    if (!done) {
+        ff_init_rl(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
+        ff_init_rl(&ff_rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
+        ff_init_rl(&ff_rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
+        INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
+        INIT_VLC_RL(ff_rvlc_rl_inter, 1072);
+        INIT_VLC_RL(ff_rvlc_rl_intra, 1072);
+        INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
+                        &ff_mpeg4_DCtab_lum[0][1], 2, 1,
+                        &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
+        INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
+                        &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
+                        &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
+        INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
+                        &ff_sprite_trajectory_tab[0][1], 4, 2,
+                        &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
+        INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
+                        &ff_mb_type_b_tab[0][1], 2, 1,
+                        &ff_mb_type_b_tab[0][0], 2, 1, 16);
+        done = 1;
+    }
+}
+
+int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
+{
+    Mpeg4DecContext *ctx = avctx->priv_data;
+    MpegEncContext    *s = &ctx->m;
+
+    /* divx 5.01+ bitstream reorder stuff */
+    /* Since this clobbers the input buffer and hwaccel codecs still need the
+     * data during hwaccel->end_frame we should not do this any earlier */
+    if (s->divx_packed) {
+        int current_pos     = s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb) >> 3);
+        int startcode_found = 0;
+
+        if (buf_size - current_pos > 7) {
+
+            int i;
+            for (i = current_pos; i < buf_size - 4; i++)
+
+                if (buf[i]     == 0 &&
+                    buf[i + 1] == 0 &&
+                    buf[i + 2] == 1 &&
+                    buf[i + 3] == 0xB6) {
+                    startcode_found = !(buf[i + 4] & 0x40);
+                    break;
+                }
+        }
+
+        if (startcode_found) {
+            av_fast_malloc(&s->bitstream_buffer,
+                           &s->allocated_bitstream_buffer_size,
+                           buf_size - current_pos +
+                           FF_INPUT_BUFFER_PADDING_SIZE);
+            if (!s->bitstream_buffer)
+                return AVERROR(ENOMEM);
+            memcpy(s->bitstream_buffer, buf + current_pos,
+                   buf_size - current_pos);
+            s->bitstream_buffer_size = buf_size - current_pos;
+        }
+    }
+
+    return 0;
+}
+
 static int mpeg4_update_thread_context(AVCodecContext *dst,
                                        const AVCodecContext *src)
 {
@@ -2413,8 +2679,7 @@ static int mpeg4_update_thread_context(AVCodecContext *dst,
     if (ret < 0)
         return ret;
 
-    s->shape               = s1->shape;
-    s->time_increment_bits = s1->time_increment_bits;
+    memcpy(((uint8_t*)s) + sizeof(MpegEncContext), ((uint8_t*)s1) + sizeof(MpegEncContext), sizeof(Mpeg4DecContext) - sizeof(MpegEncContext));
 
     return 0;
 }
@@ -2424,41 +2689,19 @@ static av_cold int decode_init(AVCodecContext *avctx)
     Mpeg4DecContext *ctx = avctx->priv_data;
     MpegEncContext *s = &ctx->m;
     int ret;
-    static int done = 0;
 
-    s->divx_version =
-    s->divx_build   =
-    s->xvid_build   =
-    s->lavc_build   = -1;
+    ctx->divx_version =
+    ctx->divx_build   =
+    ctx->xvid_build   =
+    ctx->lavc_build   = -1;
 
     if ((ret = ff_h263_decode_init(avctx)) < 0)
         return ret;
 
-    if (!done) {
-        done = 1;
-
-        ff_init_rl(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
-        ff_init_rl(&ff_rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
-        ff_init_rl(&ff_rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
-        INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
-        INIT_VLC_RL(ff_rvlc_rl_inter, 1072);
-        INIT_VLC_RL(ff_rvlc_rl_intra, 1072);
-        INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
-                        &ff_mpeg4_DCtab_lum[0][1], 2, 1,
-                        &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
-        INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
-                        &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
-                        &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
-        INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
-                        &ff_sprite_trajectory_tab[0][1], 4, 2,
-                        &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
-        INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
-                        &ff_mb_type_b_tab[0][1], 2, 1,
-                        &ff_mb_type_b_tab[0][0], 2, 1, 16);
-    }
+    ff_mpeg4videodec_static_init();
 
     s->h263_pred = 1;
-    s->low_delay = 0; /* default, might be overriden in the vol header during header parsing */
+    s->low_delay = 0; /* default, might be overridden in the vol header during header parsing */
     s->decode_mb = mpeg4_decode_mb;
     ctx->time_increment_bits = 4; /* default value for broken headers */
 
@@ -2485,6 +2728,27 @@ static const AVProfile mpeg4_video_profiles[] = {
     { FF_PROFILE_MPEG4_ADVANCED_SCALABLE_TEXTURE, "Advanced Scalable Texture Profile" },
     { FF_PROFILE_MPEG4_SIMPLE_STUDIO,             "Simple Studio Profile" },
     { FF_PROFILE_MPEG4_ADVANCED_SIMPLE,           "Advanced Simple Profile" },
+    { FF_PROFILE_UNKNOWN },
+};
+
+static const AVOption mpeg4_options[] = {
+    {"quarter_sample", "1/4 subpel MC", offsetof(MpegEncContext, quarter_sample), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
+    {"divx_packed", "divx style packed b frames", offsetof(MpegEncContext, divx_packed), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
+    {NULL}
+};
+
+static const AVClass mpeg4_class = {
+    "MPEG4 Video Decoder",
+    av_default_item_name,
+    mpeg4_options,
+    LIBAVUTIL_VERSION_INT,
+};
+
+static const AVClass mpeg4_vdpau_class = {
+    "MPEG4 Video VDPAU Decoder",
+    av_default_item_name,
+    mpeg4_options,
+    LIBAVUTIL_VERSION_INT,
 };
 
 AVCodec ff_mpeg4_decoder = {
@@ -2500,7 +2764,28 @@ AVCodec ff_mpeg4_decoder = {
                              CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
                              CODEC_CAP_FRAME_THREADS,
     .flush                 = ff_mpeg_flush,
+    .max_lowres            = 3,
     .pix_fmts              = ff_h263_hwaccel_pixfmt_list_420,
     .profiles              = NULL_IF_CONFIG_SMALL(mpeg4_video_profiles),
     .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg4_update_thread_context),
+    .priv_class = &mpeg4_class,
+};
+
+
+#if CONFIG_MPEG4_VDPAU_DECODER
+AVCodec ff_mpeg4_vdpau_decoder = {
+    .name           = "mpeg4_vdpau",
+    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 (VDPAU)"),
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_MPEG4,
+    .priv_data_size = sizeof(MpegEncContext),
+    .init           = decode_init,
+    .close          = ff_h263_decode_end,
+    .decode         = ff_h263_decode_frame,
+    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
+                      CODEC_CAP_HWACCEL_VDPAU,
+    .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_MPEG4,
+                                                  AV_PIX_FMT_NONE },
+    .priv_class     = &mpeg4_vdpau_class,
 };
+#endif