LCOV - code coverage report
Current view: top level - libavcodec - mpeg4videodec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 974 1721 56.6 %
Date: 2017-12-15 11:05:35 Functions: 20 24 83.3 %

          Line data    Source code
       1             : /*
       2             :  * MPEG-4 decoder
       3             :  * Copyright (c) 2000,2001 Fabrice Bellard
       4             :  * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
       5             :  *
       6             :  * This file is part of FFmpeg.
       7             :  *
       8             :  * FFmpeg is free software; you can redistribute it and/or
       9             :  * modify it under the terms of the GNU Lesser General Public
      10             :  * License as published by the Free Software Foundation; either
      11             :  * version 2.1 of the License, or (at your option) any later version.
      12             :  *
      13             :  * FFmpeg is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :  * Lesser General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU Lesser General Public
      19             :  * License along with FFmpeg; if not, write to the Free Software
      20             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      21             :  */
      22             : 
      23             : #define UNCHECKED_BITSTREAM_READER 1
      24             : 
      25             : #include "libavutil/internal.h"
      26             : #include "libavutil/opt.h"
      27             : #include "error_resilience.h"
      28             : #include "hwaccel.h"
      29             : #include "idctdsp.h"
      30             : #include "internal.h"
      31             : #include "mpegutils.h"
      32             : #include "mpegvideo.h"
      33             : #include "mpegvideodata.h"
      34             : #include "mpeg4video.h"
      35             : #include "h263.h"
      36             : #include "profiles.h"
      37             : #include "thread.h"
      38             : #include "xvididct.h"
      39             : 
      40             : /* The defines below define the number of bits that are read at once for
      41             :  * reading vlc values. Changing these may improve speed and data cache needs
      42             :  * be aware though that decreasing them may need the number of stages that is
      43             :  * passed to get_vlc* to be increased. */
      44             : #define SPRITE_TRAJ_VLC_BITS 6
      45             : #define DC_VLC_BITS 9
      46             : #define MB_TYPE_B_VLC_BITS 4
      47             : 
      48             : static VLC dc_lum, dc_chrom;
      49             : static VLC sprite_trajectory;
      50             : static VLC mb_type_b_vlc;
      51             : 
      52             : static const int mb_type_b_map[4] = {
      53             :     MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
      54             :     MB_TYPE_L0L1    | MB_TYPE_16x16,
      55             :     MB_TYPE_L1      | MB_TYPE_16x16,
      56             :     MB_TYPE_L0      | MB_TYPE_16x16,
      57             : };
      58             : 
      59             : /**
      60             :  * Predict the ac.
      61             :  * @param n block index (0-3 are luma, 4-5 are chroma)
      62             :  * @param dir the ac prediction direction
      63             :  */
      64     1288164 : void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
      65             : {
      66             :     int i;
      67             :     int16_t *ac_val, *ac_val1;
      68     1288164 :     int8_t *const qscale_table = s->current_picture.qscale_table;
      69             : 
      70             :     /* find prediction */
      71     1288164 :     ac_val  = &s->ac_val[0][0][0] + s->block_index[n] * 16;
      72     1288164 :     ac_val1 = ac_val;
      73     1288164 :     if (s->ac_pred) {
      74       30084 :         if (dir == 0) {
      75       20713 :             const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
      76             :             /* left prediction */
      77       20713 :             ac_val -= 16;
      78             : 
      79       41426 :             if (s->mb_x == 0 || s->qscale == qscale_table[xy] ||
      80           0 :                 n == 1 || n == 3) {
      81             :                 /* same qscale */
      82      165704 :                 for (i = 1; i < 8; i++)
      83      144991 :                     block[s->idsp.idct_permutation[i << 3]] += ac_val[i];
      84             :             } else {
      85             :                 /* different qscale, we must rescale */
      86           0 :                 for (i = 1; i < 8; i++)
      87           0 :                     block[s->idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
      88             :             }
      89             :         } else {
      90        9371 :             const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
      91             :             /* top prediction */
      92        9371 :             ac_val -= 16 * s->block_wrap[n];
      93             : 
      94       18742 :             if (s->mb_y == 0 || s->qscale == qscale_table[xy] ||
      95           2 :                 n == 2 || n == 3) {
      96             :                 /* same qscale */
      97       74968 :                 for (i = 1; i < 8; i++)
      98       65597 :                     block[s->idsp.idct_permutation[i]] += ac_val[i + 8];
      99             :             } else {
     100             :                 /* different qscale, we must rescale */
     101           0 :                 for (i = 1; i < 8; i++)
     102           0 :                     block[s->idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
     103             :             }
     104             :         }
     105             :     }
     106             :     /* left copy */
     107    10305312 :     for (i = 1; i < 8; i++)
     108     9017148 :         ac_val1[i] = block[s->idsp.idct_permutation[i << 3]];
     109             : 
     110             :     /* top copy */
     111    10305312 :     for (i = 1; i < 8; i++)
     112     9017148 :         ac_val1[8 + i] = block[s->idsp.idct_permutation[i]];
     113     1288164 : }
     114             : 
     115             : /**
     116             :  * check if the next stuff is a resync marker or the end.
     117             :  * @return 0 if not
     118             :  */
     119     1419992 : static inline int mpeg4_is_resync(Mpeg4DecContext *ctx)
     120             : {
     121     1419992 :     MpegEncContext *s = &ctx->m;
     122     1419992 :     int bits_count = get_bits_count(&s->gb);
     123     1419992 :     int v          = show_bits(&s->gb, 16);
     124             : 
     125     1419992 :     if (s->workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker)
     126           0 :         return 0;
     127             : 
     128     2839984 :     while (v <= 0xFF) {
     129        5041 :         if (s->pict_type == AV_PICTURE_TYPE_B ||
     130        2197 :             (v >> (8 - s->pict_type) != 1) || s->partitioned_frame)
     131             :             break;
     132           0 :         skip_bits(&s->gb, 8 + s->pict_type);
     133           0 :         bits_count += 8 + s->pict_type;
     134           0 :         v = show_bits(&s->gb, 16);
     135             :     }
     136             : 
     137     1419992 :     if (bits_count + 8 >= s->gb.size_in_bits) {
     138       11651 :         v >>= 8;
     139       11651 :         v  |= 0x7F >> (7 - (bits_count & 7));
     140             : 
     141       11651 :         if (v == 0x7F)
     142       11334 :             return s->mb_num;
     143             :     } else {
     144     1408341 :         if (v == ff_mpeg4_resync_prefix[bits_count & 7]) {
     145             :             int len, mb_num;
     146        4098 :             int mb_num_bits = av_log2(s->mb_num - 1) + 1;
     147        4098 :             GetBitContext gb = s->gb;
     148             : 
     149        4098 :             skip_bits(&s->gb, 1);
     150        4098 :             align_get_bits(&s->gb);
     151             : 
     152       72133 :             for (len = 0; len < 32; len++)
     153       72133 :                 if (get_bits1(&s->gb))
     154        4098 :                     break;
     155             : 
     156        4098 :             mb_num = get_bits(&s->gb, mb_num_bits);
     157        4098 :             if (!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
     158           0 :                 mb_num= -1;
     159             : 
     160        4098 :             s->gb = gb;
     161             : 
     162        4098 :             if (len >= ff_mpeg4_get_video_packet_prefix_length(s))
     163        4096 :                 return mb_num;
     164             :         }
     165             :     }
     166     1404562 :     return 0;
     167             : }
     168             : 
     169           0 : static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
     170             : {
     171           0 :     MpegEncContext *s = &ctx->m;
     172           0 :     int a     = 2 << s->sprite_warping_accuracy;
     173           0 :     int rho   = 3  - s->sprite_warping_accuracy;
     174           0 :     int r     = 16 / a;
     175           0 :     int alpha = 1;
     176           0 :     int beta  = 0;
     177           0 :     int w     = s->width;
     178           0 :     int h     = s->height;
     179             :     int min_ab, i, w2, h2, w3, h3;
     180             :     int sprite_ref[4][2];
     181             :     int virtual_ref[2][2];
     182             :     int64_t sprite_offset[2][2];
     183             :     int64_t sprite_delta[2][2];
     184             : 
     185             :     // only true for rectangle shapes
     186           0 :     const int vop_ref[4][2] = { { 0, 0 },         { s->width, 0 },
     187           0 :                                 { 0, s->height }, { s->width, s->height } };
     188           0 :     int d[4][2]             = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
     189             : 
     190           0 :     if (w <= 0 || h <= 0)
     191           0 :         return AVERROR_INVALIDDATA;
     192             : 
     193             :     /* the decoder was not properly initialized and we cannot continue */
     194           0 :     if (sprite_trajectory.table == NULL)
     195           0 :         return AVERROR_INVALIDDATA;
     196             : 
     197           0 :     for (i = 0; i < ctx->num_sprite_warping_points; i++) {
     198             :         int length;
     199           0 :         int x = 0, y = 0;
     200             : 
     201           0 :         length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
     202           0 :         if (length > 0)
     203           0 :             x = get_xbits(gb, length);
     204             : 
     205           0 :         if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
     206           0 :             check_marker(s->avctx, gb, "before sprite_trajectory");
     207             : 
     208           0 :         length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
     209           0 :         if (length > 0)
     210           0 :             y = get_xbits(gb, length);
     211             : 
     212           0 :         check_marker(s->avctx, gb, "after sprite_trajectory");
     213           0 :         ctx->sprite_traj[i][0] = d[i][0] = x;
     214           0 :         ctx->sprite_traj[i][1] = d[i][1] = y;
     215             :     }
     216           0 :     for (; i < 4; i++)
     217           0 :         ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0;
     218             : 
     219           0 :     while ((1 << alpha) < w)
     220           0 :         alpha++;
     221           0 :     while ((1 << beta) < h)
     222           0 :         beta++;  /* typo in the MPEG-4 std for the definition of w' and h' */
     223           0 :     w2 = 1 << alpha;
     224           0 :     h2 = 1 << beta;
     225             : 
     226             :     // Note, the 4th point isn't used for GMC
     227           0 :     if (ctx->divx_version == 500 && ctx->divx_build == 413) {
     228           0 :         sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
     229           0 :         sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
     230           0 :         sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
     231           0 :         sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1];
     232           0 :         sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0];
     233           0 :         sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1];
     234             :     } else {
     235           0 :         sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]);
     236           0 :         sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]);
     237           0 :         sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]);
     238           0 :         sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]);
     239           0 :         sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]);
     240           0 :         sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]);
     241             :     }
     242             :     /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
     243             :      * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
     244             : 
     245             :     /* This is mostly identical to the MPEG-4 std (and is totally unreadable
     246             :      * because of that...). Perhaps it should be reordered to be more readable.
     247             :      * The idea behind this virtual_ref mess is to be able to use shifts later
     248             :      * per pixel instead of divides so the distance between points is converted
     249             :      * from w&h based to w2&h2 based which are of the 2^x form. */
     250           0 :     virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) +
     251           0 :                          ROUNDED_DIV(((w - w2) *
     252             :                                            (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
     253             :                                       w2 * (r * sprite_ref[1][0] - 16LL * vop_ref[1][0])), w);
     254           0 :     virtual_ref[0][1] = 16 * vop_ref[0][1] +
     255           0 :                         ROUNDED_DIV(((w - w2) *
     256             :                                           (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
     257             :                                      w2 * (r * sprite_ref[1][1] - 16LL * vop_ref[1][1])), w);
     258           0 :     virtual_ref[1][0] = 16 * vop_ref[0][0] +
     259           0 :                         ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
     260             :                                            h2 * (r * sprite_ref[2][0] - 16LL * vop_ref[2][0])), h);
     261           0 :     virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) +
     262           0 :                         ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
     263             :                                            h2 * (r * sprite_ref[2][1] - 16LL * vop_ref[2][1])), h);
     264             : 
     265           0 :     switch (ctx->num_sprite_warping_points) {
     266           0 :     case 0:
     267           0 :         sprite_offset[0][0]    =
     268           0 :         sprite_offset[0][1]    =
     269           0 :         sprite_offset[1][0]    =
     270           0 :         sprite_offset[1][1]    = 0;
     271           0 :         sprite_delta[0][0]     = a;
     272           0 :         sprite_delta[0][1]     =
     273           0 :         sprite_delta[1][0]     = 0;
     274           0 :         sprite_delta[1][1]     = a;
     275           0 :         ctx->sprite_shift[0]   =
     276           0 :         ctx->sprite_shift[1]   = 0;
     277           0 :         break;
     278           0 :     case 1:     // GMC only
     279           0 :         sprite_offset[0][0]    = sprite_ref[0][0] - a * vop_ref[0][0];
     280           0 :         sprite_offset[0][1]    = sprite_ref[0][1] - a * vop_ref[0][1];
     281           0 :         sprite_offset[1][0]    = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) -
     282           0 :                                  a * (vop_ref[0][0] / 2);
     283           0 :         sprite_offset[1][1]    = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) -
     284           0 :                                  a * (vop_ref[0][1] / 2);
     285           0 :         sprite_delta[0][0]     = a;
     286           0 :         sprite_delta[0][1]     =
     287           0 :         sprite_delta[1][0]     = 0;
     288           0 :         sprite_delta[1][1]     = a;
     289           0 :         ctx->sprite_shift[0]   =
     290           0 :         ctx->sprite_shift[1]   = 0;
     291           0 :         break;
     292           0 :     case 2:
     293           0 :         sprite_offset[0][0]    = ((int64_t)      sprite_ref[0][0] * (1 << alpha + rho)) +
     294           0 :                                  ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
     295           0 :                                  ((int64_t)        -vop_ref[0][0]) +
     296           0 :                                  ((int64_t)  r * sprite_ref[0][1] - virtual_ref[0][1]) *
     297           0 :                                  ((int64_t)        -vop_ref[0][1]) + (1 << (alpha + rho - 1));
     298           0 :         sprite_offset[0][1]    = ((int64_t)      sprite_ref[0][1] * (1 << alpha + rho)) +
     299           0 :                                  ((int64_t) -r * sprite_ref[0][1] + virtual_ref[0][1]) *
     300           0 :                                  ((int64_t)        -vop_ref[0][0]) +
     301           0 :                                  ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
     302           0 :                                  ((int64_t)        -vop_ref[0][1]) + (1 << (alpha + rho - 1));
     303           0 :         sprite_offset[1][0]    = (((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
     304           0 :                                   ((int64_t)-2 *    vop_ref[0][0] + 1) +
     305           0 :                                   ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
     306           0 :                                   ((int64_t)-2 *    vop_ref[0][1] + 1) + 2 * w2 * r *
     307           0 :                                    (int64_t)     sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1)));
     308           0 :         sprite_offset[1][1]    = (((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) *
     309           0 :                                   ((int64_t)-2 *    vop_ref[0][0] + 1) +
     310           0 :                                   ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
     311           0 :                                   ((int64_t)-2 *    vop_ref[0][1] + 1) + 2 * w2 * r *
     312           0 :                                   (int64_t)      sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1)));
     313           0 :         sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
     314           0 :         sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]);
     315           0 :         sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
     316           0 :         sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
     317             : 
     318           0 :         ctx->sprite_shift[0]  = alpha + rho;
     319           0 :         ctx->sprite_shift[1]  = alpha + rho + 2;
     320           0 :         break;
     321           0 :     case 3:
     322           0 :         min_ab = FFMIN(alpha, beta);
     323           0 :         w3     = w2 >> min_ab;
     324           0 :         h3     = h2 >> min_ab;
     325           0 :         sprite_offset[0][0]    = ((int64_t)sprite_ref[0][0] * (1 << (alpha + beta + rho - min_ab))) +
     326           0 :                                  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-vop_ref[0][0]) +
     327           0 :                                  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-vop_ref[0][1]) +
     328           0 :                                  ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
     329           0 :         sprite_offset[0][1]    = ((int64_t)sprite_ref[0][1] * (1 << (alpha + beta + rho - min_ab))) +
     330           0 :                                  ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-vop_ref[0][0]) +
     331           0 :                                  ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-vop_ref[0][1]) +
     332           0 :                                  ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
     333           0 :         sprite_offset[1][0]    = ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-2 * vop_ref[0][0] + 1) +
     334           0 :                                  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-2 * vop_ref[0][1] + 1) +
     335           0 :                                   (int64_t)2 * w2 * h3 * r * sprite_ref[0][0] - 16 * w2 * h3 +
     336           0 :                                  ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
     337           0 :         sprite_offset[1][1]    = ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-2 * vop_ref[0][0] + 1) +
     338           0 :                                  ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-2 * vop_ref[0][1] + 1) +
     339           0 :                                   (int64_t)2 * w2 * h3 * r * sprite_ref[0][1] - 16 * w2 * h3 +
     340           0 :                                  ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
     341           0 :         sprite_delta[0][0] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[0][0]) * h3;
     342           0 :         sprite_delta[0][1] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[1][0]) * w3;
     343           0 :         sprite_delta[1][0] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[0][1]) * h3;
     344           0 :         sprite_delta[1][1] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[1][1]) * w3;
     345             : 
     346           0 :         ctx->sprite_shift[0]  = alpha + beta + rho - min_ab;
     347           0 :         ctx->sprite_shift[1]  = alpha + beta + rho - min_ab + 2;
     348           0 :         break;
     349             :     }
     350             :     /* try to simplify the situation */
     351           0 :     if (sprite_delta[0][0] == a << ctx->sprite_shift[0] &&
     352           0 :         sprite_delta[0][1] == 0 &&
     353           0 :         sprite_delta[1][0] == 0 &&
     354           0 :         sprite_delta[1][1] == a << ctx->sprite_shift[0]) {
     355           0 :         sprite_offset[0][0] >>= ctx->sprite_shift[0];
     356           0 :         sprite_offset[0][1] >>= ctx->sprite_shift[0];
     357           0 :         sprite_offset[1][0] >>= ctx->sprite_shift[1];
     358           0 :         sprite_offset[1][1] >>= ctx->sprite_shift[1];
     359           0 :         sprite_delta[0][0] = a;
     360           0 :         sprite_delta[0][1] = 0;
     361           0 :         sprite_delta[1][0] = 0;
     362           0 :         sprite_delta[1][1] = a;
     363           0 :         ctx->sprite_shift[0] = 0;
     364           0 :         ctx->sprite_shift[1] = 0;
     365           0 :         s->real_sprite_warping_points = 1;
     366             :     } else {
     367           0 :         int shift_y = 16 - ctx->sprite_shift[0];
     368           0 :         int shift_c = 16 - ctx->sprite_shift[1];
     369             : 
     370           0 :         for (i = 0; i < 2; i++) {
     371           0 :             if (shift_c < 0 || shift_y < 0 ||
     372           0 :                 FFABS(  sprite_offset[0][i]) >= INT_MAX >> shift_y  ||
     373           0 :                 FFABS(  sprite_offset[1][i]) >= INT_MAX >> shift_c  ||
     374           0 :                 FFABS(   sprite_delta[0][i]) >= INT_MAX >> shift_y  ||
     375           0 :                 FFABS(   sprite_delta[1][i]) >= INT_MAX >> shift_y
     376             :             ) {
     377           0 :                 avpriv_request_sample(s->avctx, "Too large sprite shift, delta or offset");
     378           0 :                 goto overflow;
     379             :             }
     380             :         }
     381             : 
     382           0 :         for (i = 0; i < 2; i++) {
     383           0 :             sprite_offset[0][i]    *= 1 << shift_y;
     384           0 :             sprite_offset[1][i]    *= 1 << shift_c;
     385           0 :             sprite_delta[0][i]     *= 1 << shift_y;
     386           0 :             sprite_delta[1][i]     *= 1 << shift_y;
     387           0 :             ctx->sprite_shift[i]     = 16;
     388             : 
     389             :         }
     390           0 :         for (i = 0; i < 2; i++) {
     391           0 :             int64_t sd[2] = {
     392           0 :                 sprite_delta[i][0] - a * (1LL<<16),
     393           0 :                 sprite_delta[i][1] - a * (1LL<<16)
     394             :             };
     395             : 
     396           0 :             if (llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
     397           0 :                 llabs(sprite_offset[0][i] + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
     398           0 :                 llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL) + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
     399           0 :                 llabs(sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
     400           0 :                 llabs(sprite_delta[i][1] * (w+16LL)) >= INT_MAX ||
     401           0 :                 llabs(sd[0]) >= INT_MAX ||
     402           0 :                 llabs(sd[1]) >= INT_MAX ||
     403           0 :                 llabs(sprite_offset[0][i] + sd[0] * (w+16LL)) >= INT_MAX ||
     404           0 :                 llabs(sprite_offset[0][i] + sd[1] * (h+16LL)) >= INT_MAX ||
     405           0 :                 llabs(sprite_offset[0][i] + sd[0] * (w+16LL) + sd[1] * (h+16LL)) >= INT_MAX
     406             :             ) {
     407           0 :                 avpriv_request_sample(s->avctx, "Overflow on sprite points");
     408           0 :                 goto overflow;
     409             :             }
     410             :         }
     411           0 :         s->real_sprite_warping_points = ctx->num_sprite_warping_points;
     412             :     }
     413             : 
     414           0 :     for (i = 0; i < 4; i++) {
     415           0 :         s->sprite_offset[i&1][i>>1] = sprite_offset[i&1][i>>1];
     416           0 :         s->sprite_delta [i&1][i>>1] = sprite_delta [i&1][i>>1];
     417             :     }
     418             : 
     419           0 :     return 0;
     420           0 : overflow:
     421           0 :     memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
     422           0 :     memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
     423           0 :     return AVERROR_PATCHWELCOME;
     424             : }
     425             : 
     426           0 : static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb) {
     427           0 :     MpegEncContext *s = &ctx->m;
     428           0 :     int len = FFMIN(ctx->time_increment_bits + 3, 15);
     429             : 
     430           0 :     get_bits(gb, len);
     431           0 :     if (get_bits1(gb))
     432           0 :         get_bits(gb, len);
     433           0 :     check_marker(s->avctx, gb, "after new_pred");
     434             : 
     435           0 :     return 0;
     436             : }
     437             : 
     438             : /**
     439             :  * Decode the next video packet.
     440             :  * @return <0 if something went wrong
     441             :  */
     442        3867 : int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
     443             : {
     444        3867 :     MpegEncContext *s = &ctx->m;
     445             : 
     446        3867 :     int mb_num_bits      = av_log2(s->mb_num - 1) + 1;
     447        3867 :     int header_extension = 0, mb_num, len;
     448             : 
     449             :     /* is there enough space left for a video packet + header */
     450        3867 :     if (get_bits_count(&s->gb) > s->gb.size_in_bits - 20)
     451           0 :         return -1;
     452             : 
     453       68178 :     for (len = 0; len < 32; len++)
     454       68178 :         if (get_bits1(&s->gb))
     455        3867 :             break;
     456             : 
     457        3867 :     if (len != ff_mpeg4_get_video_packet_prefix_length(s)) {
     458           0 :         av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
     459           0 :         return -1;
     460             :     }
     461             : 
     462        3867 :     if (ctx->shape != RECT_SHAPE) {
     463           0 :         header_extension = get_bits1(&s->gb);
     464             :         // FIXME more stuff here
     465             :     }
     466             : 
     467        3867 :     mb_num = get_bits(&s->gb, mb_num_bits);
     468        3867 :     if (mb_num >= s->mb_num) {
     469           0 :         av_log(s->avctx, AV_LOG_ERROR,
     470             :                "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
     471           0 :         return -1;
     472             :     }
     473             : 
     474        3867 :     s->mb_x = mb_num % s->mb_width;
     475        3867 :     s->mb_y = mb_num / s->mb_width;
     476             : 
     477        3867 :     if (ctx->shape != BIN_ONLY_SHAPE) {
     478        3867 :         int qscale = get_bits(&s->gb, s->quant_precision);
     479        3867 :         if (qscale)
     480        3867 :             s->chroma_qscale = s->qscale = qscale;
     481             :     }
     482             : 
     483        3867 :     if (ctx->shape == RECT_SHAPE)
     484        3867 :         header_extension = get_bits1(&s->gb);
     485             : 
     486        3867 :     if (header_extension) {
     487           0 :         int time_incr = 0;
     488             : 
     489           0 :         while (get_bits1(&s->gb) != 0)
     490           0 :             time_incr++;
     491             : 
     492           0 :         check_marker(s->avctx, &s->gb, "before time_increment in video packed header");
     493           0 :         skip_bits(&s->gb, ctx->time_increment_bits);      /* time_increment */
     494           0 :         check_marker(s->avctx, &s->gb, "before vop_coding_type in video packed header");
     495             : 
     496           0 :         skip_bits(&s->gb, 2); /* vop coding type */
     497             :         // FIXME not rect stuff here
     498             : 
     499           0 :         if (ctx->shape != BIN_ONLY_SHAPE) {
     500           0 :             skip_bits(&s->gb, 3); /* intra dc vlc threshold */
     501             :             // FIXME don't just ignore everything
     502           0 :             if (s->pict_type == AV_PICTURE_TYPE_S &&
     503           0 :                 ctx->vol_sprite_usage == GMC_SPRITE) {
     504           0 :                 if (mpeg4_decode_sprite_trajectory(ctx, &s->gb) < 0)
     505           0 :                     return AVERROR_INVALIDDATA;
     506           0 :                 av_log(s->avctx, AV_LOG_ERROR, "untested\n");
     507             :             }
     508             : 
     509             :             // FIXME reduced res stuff here
     510             : 
     511           0 :             if (s->pict_type != AV_PICTURE_TYPE_I) {
     512           0 :                 int f_code = get_bits(&s->gb, 3);       /* fcode_for */
     513           0 :                 if (f_code == 0)
     514           0 :                     av_log(s->avctx, AV_LOG_ERROR,
     515             :                            "Error, video packet header damaged (f_code=0)\n");
     516             :             }
     517           0 :             if (s->pict_type == AV_PICTURE_TYPE_B) {
     518           0 :                 int b_code = get_bits(&s->gb, 3);
     519           0 :                 if (b_code == 0)
     520           0 :                     av_log(s->avctx, AV_LOG_ERROR,
     521             :                            "Error, video packet header damaged (b_code=0)\n");
     522             :             }
     523             :         }
     524             :     }
     525        3867 :     if (ctx->new_pred)
     526           0 :         decode_new_pred(ctx, &s->gb);
     527             : 
     528        3867 :     return 0;
     529             : }
     530             : 
     531             : /**
     532             :  * Get the average motion vector for a GMC MB.
     533             :  * @param n either 0 for the x component or 1 for y
     534             :  * @return the average MV for a GMC MB
     535             :  */
     536           0 : static inline int get_amv(Mpeg4DecContext *ctx, int n)
     537             : {
     538           0 :     MpegEncContext *s = &ctx->m;
     539             :     int x, y, mb_v, sum, dx, dy, shift;
     540           0 :     int len     = 1 << (s->f_code + 4);
     541           0 :     const int a = s->sprite_warping_accuracy;
     542             : 
     543           0 :     if (s->workaround_bugs & FF_BUG_AMV)
     544           0 :         len >>= s->quarter_sample;
     545             : 
     546           0 :     if (s->real_sprite_warping_points == 1) {
     547           0 :         if (ctx->divx_version == 500 && ctx->divx_build == 413)
     548           0 :             sum = s->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
     549             :         else
     550           0 :             sum = RSHIFT(s->sprite_offset[0][n] * (1 << s->quarter_sample), a);
     551             :     } else {
     552           0 :         dx    = s->sprite_delta[n][0];
     553           0 :         dy    = s->sprite_delta[n][1];
     554           0 :         shift = ctx->sprite_shift[0];
     555           0 :         if (n)
     556           0 :             dy -= 1 << (shift + a + 1);
     557             :         else
     558           0 :             dx -= 1 << (shift + a + 1);
     559           0 :         mb_v = s->sprite_offset[0][n] + dx * s->mb_x * 16 + dy * s->mb_y * 16;
     560             : 
     561           0 :         sum = 0;
     562           0 :         for (y = 0; y < 16; y++) {
     563             :             int v;
     564             : 
     565           0 :             v = mb_v + dy * y;
     566             :             // FIXME optimize
     567           0 :             for (x = 0; x < 16; x++) {
     568           0 :                 sum += v >> shift;
     569           0 :                 v   += dx;
     570             :             }
     571             :         }
     572           0 :         sum = RSHIFT(sum, a + 8 - s->quarter_sample);
     573             :     }
     574             : 
     575           0 :     if (sum < -len)
     576           0 :         sum = -len;
     577           0 :     else if (sum >= len)
     578           0 :         sum = len - 1;
     579             : 
     580           0 :     return sum;
     581             : }
     582             : 
     583             : /**
     584             :  * Decode the dc value.
     585             :  * @param n block index (0-3 are luma, 4-5 are chroma)
     586             :  * @param dir_ptr the prediction direction will be stored here
     587             :  * @return the quantized dc
     588             :  */
     589     1057452 : static inline int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
     590             : {
     591             :     int level, code;
     592             : 
     593     1057452 :     if (n < 4)
     594      704968 :         code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
     595             :     else
     596      352484 :         code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
     597             : 
     598     1057452 :     if (code < 0 || code > 9 /* && s->nbit < 9 */) {
     599           0 :         av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
     600           0 :         return -1;
     601             :     }
     602             : 
     603     1057452 :     if (code == 0) {
     604      155928 :         level = 0;
     605             :     } else {
     606             :         if (IS_3IV1) {
     607             :             if (code == 1)
     608             :                 level = 2 * get_bits1(&s->gb) - 1;
     609             :             else {
     610             :                 if (get_bits1(&s->gb))
     611             :                     level = get_bits(&s->gb, code - 1) + (1 << (code - 1));
     612             :                 else
     613             :                     level = -get_bits(&s->gb, code - 1) - (1 << (code - 1));
     614             :             }
     615             :         } else {
     616      901524 :             level = get_xbits(&s->gb, code);
     617             :         }
     618             : 
     619      901524 :         if (code > 8) {
     620           0 :             if (get_bits1(&s->gb) == 0) { /* marker */
     621           0 :                 if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)) {
     622           0 :                     av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
     623           0 :                     return -1;
     624             :                 }
     625             :             }
     626             :         }
     627             :     }
     628             : 
     629     1057452 :     return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
     630             : }
     631             : 
     632             : /**
     633             :  * Decode first partition.
     634             :  * @return number of MBs decoded or <0 if an error occurred
     635             :  */
     636        1728 : static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
     637             : {
     638        1728 :     MpegEncContext *s = &ctx->m;
     639        1728 :     int mb_num = 0;
     640             :     static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
     641             : 
     642             :     /* decode first partition */
     643        1728 :     s->first_slice_line = 1;
     644        8311 :     for (; s->mb_y < s->mb_height; s->mb_y++) {
     645        7839 :         ff_init_block_index(s);
     646      145903 :         for (; s->mb_x < s->mb_width; s->mb_x++) {
     647      139320 :             const int xy = s->mb_x + s->mb_y * s->mb_stride;
     648             :             int cbpc;
     649      139320 :             int dir = 0;
     650             : 
     651      139320 :             mb_num++;
     652      139320 :             ff_update_block_index(s);
     653      139320 :             if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
     654        1611 :                 s->first_slice_line = 0;
     655             : 
     656      139320 :             if (s->pict_type == AV_PICTURE_TYPE_I) {
     657             :                 int i;
     658             : 
     659             :                 do {
     660       17762 :                     if (show_bits_long(&s->gb, 19) == DC_MARKER)
     661         439 :                         return mb_num - 1;
     662             : 
     663       17323 :                     cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
     664       17323 :                     if (cbpc < 0) {
     665           0 :                         av_log(s->avctx, AV_LOG_ERROR,
     666             :                                "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
     667           0 :                         return -1;
     668             :                     }
     669       17323 :                 } while (cbpc == 8);
     670             : 
     671       17323 :                 s->cbp_table[xy]               = cbpc & 3;
     672       17323 :                 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
     673       17323 :                 s->mb_intra                    = 1;
     674             : 
     675       17323 :                 if (cbpc & 4)
     676           0 :                     ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
     677             : 
     678       17323 :                 s->current_picture.qscale_table[xy] = s->qscale;
     679             : 
     680       17323 :                 s->mbintra_table[xy] = 1;
     681      121261 :                 for (i = 0; i < 6; i++) {
     682             :                     int dc_pred_dir;
     683      103938 :                     int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
     684      103938 :                     if (dc < 0) {
     685           0 :                         av_log(s->avctx, AV_LOG_ERROR,
     686             :                                "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
     687           0 :                         return -1;
     688             :                     }
     689      103938 :                     dir <<= 1;
     690      103938 :                     if (dc_pred_dir)
     691       30118 :                         dir |= 1;
     692             :                 }
     693       17323 :                 s->pred_dir_table[xy] = dir;
     694             :             } else { /* P/S_TYPE */
     695             :                 int mx, my, pred_x, pred_y, bits;
     696      121558 :                 int16_t *const mot_val = s->current_picture.motion_val[0][s->block_index[0]];
     697      121558 :                 const int stride       = s->b8_stride * 2;
     698             : 
     699      121558 : try_again:
     700      121558 :                 bits = show_bits(&s->gb, 17);
     701      121558 :                 if (bits == MOTION_MARKER)
     702        1634 :                     return mb_num - 1;
     703             : 
     704      120741 :                 skip_bits1(&s->gb);
     705      120741 :                 if (bits & 0x10000) {
     706             :                     /* skip mb */
     707         997 :                     if (s->pict_type == AV_PICTURE_TYPE_S &&
     708           0 :                         ctx->vol_sprite_usage == GMC_SPRITE) {
     709           0 :                         s->current_picture.mb_type[xy] = MB_TYPE_SKIP  |
     710             :                                                          MB_TYPE_16x16 |
     711             :                                                          MB_TYPE_GMC   |
     712             :                                                          MB_TYPE_L0;
     713           0 :                         mx = get_amv(ctx, 0);
     714           0 :                         my = get_amv(ctx, 1);
     715             :                     } else {
     716         997 :                         s->current_picture.mb_type[xy] = MB_TYPE_SKIP  |
     717             :                                                          MB_TYPE_16x16 |
     718             :                                                          MB_TYPE_L0;
     719         997 :                         mx = my = 0;
     720             :                     }
     721         997 :                     mot_val[0]          =
     722        1994 :                     mot_val[2]          =
     723        2991 :                     mot_val[0 + stride] =
     724        1994 :                     mot_val[2 + stride] = mx;
     725        1994 :                     mot_val[1]          =
     726        2991 :                     mot_val[3]          =
     727        2991 :                     mot_val[1 + stride] =
     728        1994 :                     mot_val[3 + stride] = my;
     729             : 
     730         997 :                     if (s->mbintra_table[xy])
     731         224 :                         ff_clean_intra_table_entries(s);
     732         997 :                     continue;
     733             :                 }
     734             : 
     735      119744 :                 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
     736      119744 :                 if (cbpc < 0) {
     737           0 :                     av_log(s->avctx, AV_LOG_ERROR,
     738             :                            "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
     739           0 :                     return -1;
     740             :                 }
     741      119744 :                 if (cbpc == 20)
     742           0 :                     goto try_again;
     743             : 
     744      119744 :                 s->cbp_table[xy] = cbpc & (8 + 3);  // 8 is dquant
     745             : 
     746      119744 :                 s->mb_intra = ((cbpc & 4) != 0);
     747             : 
     748      119744 :                 if (s->mb_intra) {
     749        6287 :                     s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
     750        6287 :                     s->mbintra_table[xy] = 1;
     751        6287 :                     mot_val[0]          =
     752       12574 :                     mot_val[2]          =
     753       18861 :                     mot_val[0 + stride] =
     754       12574 :                     mot_val[2 + stride] = 0;
     755       12574 :                     mot_val[1]          =
     756       18861 :                     mot_val[3]          =
     757       18861 :                     mot_val[1 + stride] =
     758       12574 :                     mot_val[3 + stride] = 0;
     759             :                 } else {
     760      113457 :                     if (s->mbintra_table[xy])
     761       19851 :                         ff_clean_intra_table_entries(s);
     762             : 
     763      113457 :                     if (s->pict_type == AV_PICTURE_TYPE_S &&
     764           0 :                         ctx->vol_sprite_usage == GMC_SPRITE &&
     765           0 :                         (cbpc & 16) == 0)
     766           0 :                         s->mcsel = get_bits1(&s->gb);
     767             :                     else
     768      113457 :                         s->mcsel = 0;
     769             : 
     770      113457 :                     if ((cbpc & 16) == 0) {
     771             :                         /* 16x16 motion prediction */
     772             : 
     773       81626 :                         ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
     774       81626 :                         if (!s->mcsel) {
     775       81626 :                             mx = ff_h263_decode_motion(s, pred_x, s->f_code);
     776       81626 :                             if (mx >= 0xffff)
     777           0 :                                 return -1;
     778             : 
     779       81626 :                             my = ff_h263_decode_motion(s, pred_y, s->f_code);
     780       81626 :                             if (my >= 0xffff)
     781           0 :                                 return -1;
     782       81626 :                             s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
     783             :                                                              MB_TYPE_L0;
     784             :                         } else {
     785           0 :                             mx = get_amv(ctx, 0);
     786           0 :                             my = get_amv(ctx, 1);
     787           0 :                             s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
     788             :                                                              MB_TYPE_GMC   |
     789             :                                                              MB_TYPE_L0;
     790             :                         }
     791             : 
     792       81626 :                         mot_val[0]          =
     793      163252 :                         mot_val[2]          =
     794      244878 :                         mot_val[0 + stride] =
     795      163252 :                         mot_val[2 + stride] = mx;
     796      163252 :                         mot_val[1]          =
     797      244878 :                         mot_val[3]          =
     798      244878 :                         mot_val[1 + stride] =
     799      163252 :                         mot_val[3 + stride] = my;
     800             :                     } else {
     801             :                         int i;
     802       31831 :                         s->current_picture.mb_type[xy] = MB_TYPE_8x8 |
     803             :                                                          MB_TYPE_L0;
     804      159155 :                         for (i = 0; i < 4; i++) {
     805      127324 :                             int16_t *mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
     806      127324 :                             mx = ff_h263_decode_motion(s, pred_x, s->f_code);
     807      127324 :                             if (mx >= 0xffff)
     808           0 :                                 return -1;
     809             : 
     810      127324 :                             my = ff_h263_decode_motion(s, pred_y, s->f_code);
     811      127324 :                             if (my >= 0xffff)
     812           0 :                                 return -1;
     813      127324 :                             mot_val[0] = mx;
     814      127324 :                             mot_val[1] = my;
     815             :                         }
     816             :                     }
     817             :                 }
     818             :             }
     819             :         }
     820        6583 :         s->mb_x = 0;
     821             :     }
     822             : 
     823         472 :     return mb_num;
     824             : }
     825             : 
     826             : /**
     827             :  * decode second partition.
     828             :  * @return <0 if an error occurred
     829             :  */
     830        1728 : static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
     831             : {
     832        1728 :     int mb_num = 0;
     833             :     static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
     834             : 
     835        1728 :     s->mb_x = s->resync_mb_x;
     836        1728 :     s->first_slice_line = 1;
     837        7728 :     for (s->mb_y = s->resync_mb_y; mb_num < mb_count; s->mb_y++) {
     838        7728 :         ff_init_block_index(s);
     839      145792 :         for (; mb_num < mb_count && s->mb_x < s->mb_width; s->mb_x++) {
     840      138064 :             const int xy = s->mb_x + s->mb_y * s->mb_stride;
     841             : 
     842      138064 :             mb_num++;
     843      138064 :             ff_update_block_index(s);
     844      138064 :             if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
     845        1611 :                 s->first_slice_line = 0;
     846             : 
     847      138064 :             if (s->pict_type == AV_PICTURE_TYPE_I) {
     848       17323 :                 int ac_pred = get_bits1(&s->gb);
     849       17323 :                 int cbpy    = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
     850       17323 :                 if (cbpy < 0) {
     851           0 :                     av_log(s->avctx, AV_LOG_ERROR,
     852             :                            "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
     853           0 :                     return -1;
     854             :                 }
     855             : 
     856       17323 :                 s->cbp_table[xy]               |= cbpy << 2;
     857       17323 :                 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
     858             :             } else { /* P || S_TYPE */
     859      120741 :                 if (IS_INTRA(s->current_picture.mb_type[xy])) {
     860             :                     int i;
     861        6287 :                     int dir     = 0;
     862        6287 :                     int ac_pred = get_bits1(&s->gb);
     863        6287 :                     int cbpy    = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
     864             : 
     865        6287 :                     if (cbpy < 0) {
     866           0 :                         av_log(s->avctx, AV_LOG_ERROR,
     867             :                                "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
     868           0 :                         return -1;
     869             :                     }
     870             : 
     871        6287 :                     if (s->cbp_table[xy] & 8)
     872           0 :                         ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
     873        6287 :                     s->current_picture.qscale_table[xy] = s->qscale;
     874             : 
     875       44009 :                     for (i = 0; i < 6; i++) {
     876             :                         int dc_pred_dir;
     877       37722 :                         int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
     878       37722 :                         if (dc < 0) {
     879           0 :                             av_log(s->avctx, AV_LOG_ERROR,
     880             :                                    "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
     881           0 :                             return -1;
     882             :                         }
     883       37722 :                         dir <<= 1;
     884       37722 :                         if (dc_pred_dir)
     885       11780 :                             dir |= 1;
     886             :                     }
     887        6287 :                     s->cbp_table[xy]               &= 3;  // remove dquant
     888        6287 :                     s->cbp_table[xy]               |= cbpy << 2;
     889        6287 :                     s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
     890        6287 :                     s->pred_dir_table[xy]           = dir;
     891      114454 :                 } else if (IS_SKIP(s->current_picture.mb_type[xy])) {
     892         997 :                     s->current_picture.qscale_table[xy] = s->qscale;
     893         997 :                     s->cbp_table[xy]                    = 0;
     894             :                 } else {
     895      113457 :                     int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
     896             : 
     897      113457 :                     if (cbpy < 0) {
     898           0 :                         av_log(s->avctx, AV_LOG_ERROR,
     899             :                                "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
     900           0 :                         return -1;
     901             :                     }
     902             : 
     903      113457 :                     if (s->cbp_table[xy] & 8)
     904           0 :                         ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
     905      113457 :                     s->current_picture.qscale_table[xy] = s->qscale;
     906             : 
     907      113457 :                     s->cbp_table[xy] &= 3;  // remove dquant
     908      113457 :                     s->cbp_table[xy] |= (cbpy ^ 0xf) << 2;
     909             :                 }
     910             :             }
     911             :         }
     912        7728 :         if (mb_num >= mb_count)
     913        1728 :             return 0;
     914        6000 :         s->mb_x = 0;
     915             :     }
     916           0 :     return 0;
     917             : }
     918             : 
     919             : /**
     920             :  * Decode the first and second partition.
     921             :  * @return <0 if error (and sets error type in the error_status_table)
     922             :  */
     923        1728 : int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx)
     924             : {
     925        1728 :     MpegEncContext *s = &ctx->m;
     926             :     int mb_num;
     927        1728 :     const int part_a_error = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR;
     928        1728 :     const int part_a_end   = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END   | ER_MV_END)   : ER_MV_END;
     929             : 
     930        1728 :     mb_num = mpeg4_decode_partition_a(ctx);
     931        1728 :     if (mb_num <= 0) {
     932           0 :         ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
     933             :                         s->mb_x, s->mb_y, part_a_error);
     934           0 :         return -1;
     935             :     }
     936             : 
     937        1728 :     if (s->resync_mb_x + s->resync_mb_y * s->mb_width + mb_num > s->mb_num) {
     938           0 :         av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
     939           0 :         ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
     940             :                         s->mb_x, s->mb_y, part_a_error);
     941           0 :         return -1;
     942             :     }
     943             : 
     944        1728 :     s->mb_num_left = mb_num;
     945             : 
     946        1728 :     if (s->pict_type == AV_PICTURE_TYPE_I) {
     947         998 :         while (show_bits(&s->gb, 9) == 1)
     948           0 :             skip_bits(&s->gb, 9);
     949         499 :         if (get_bits_long(&s->gb, 19) != DC_MARKER) {
     950           0 :             av_log(s->avctx, AV_LOG_ERROR,
     951             :                    "marker missing after first I partition at %d %d\n",
     952             :                    s->mb_x, s->mb_y);
     953           0 :             return -1;
     954             :         }
     955             :     } else {
     956        2458 :         while (show_bits(&s->gb, 10) == 1)
     957           0 :             skip_bits(&s->gb, 10);
     958        1229 :         if (get_bits(&s->gb, 17) != MOTION_MARKER) {
     959           0 :             av_log(s->avctx, AV_LOG_ERROR,
     960             :                    "marker missing after first P partition at %d %d\n",
     961             :                    s->mb_x, s->mb_y);
     962           0 :             return -1;
     963             :         }
     964             :     }
     965        3456 :     ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
     966        1728 :                     s->mb_x - 1, s->mb_y, part_a_end);
     967             : 
     968        1728 :     if (mpeg4_decode_partition_b(s, mb_num) < 0) {
     969           0 :         if (s->pict_type == AV_PICTURE_TYPE_P)
     970           0 :             ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
     971             :                             s->mb_x, s->mb_y, ER_DC_ERROR);
     972           0 :         return -1;
     973             :     } else {
     974        1728 :         if (s->pict_type == AV_PICTURE_TYPE_P)
     975        2458 :             ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
     976        1229 :                             s->mb_x - 1, s->mb_y, ER_DC_END);
     977             :     }
     978             : 
     979        1728 :     return 0;
     980             : }
     981             : 
     982             : /**
     983             :  * Decode a block.
     984             :  * @return <0 if an error occurred
     985             :  */
     986     7831032 : static inline int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block,
     987             :                                      int n, int coded, int intra, int rvlc)
     988             : {
     989     7831032 :     MpegEncContext *s = &ctx->m;
     990             :     int level, i, last, run, qmul, qadd;
     991     7831032 :     int av_uninit(dc_pred_dir);
     992             :     RLTable *rl;
     993             :     RL_VLC_ELEM *rl_vlc;
     994             :     const uint8_t *scan_table;
     995             : 
     996             :     // Note intra & rvlc should be optimized away if this is inlined
     997             : 
     998     7831032 :     if (intra) {
     999     1057452 :         if (ctx->use_intra_dc_vlc) {
    1000             :             /* DC coef */
    1001     1057452 :             if (s->partitioned_frame) {
    1002      141660 :                 level = s->dc_val[0][s->block_index[n]];
    1003      141660 :                 if (n < 4)
    1004       94440 :                     level = FASTDIV((level + (s->y_dc_scale >> 1)), s->y_dc_scale);
    1005             :                 else
    1006       47220 :                     level = FASTDIV((level + (s->c_dc_scale >> 1)), s->c_dc_scale);
    1007      141660 :                 dc_pred_dir = (s->pred_dir_table[s->mb_x + s->mb_y * s->mb_stride] << n) & 32;
    1008             :             } else {
    1009      915792 :                 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
    1010      915792 :                 if (level < 0)
    1011           0 :                     return -1;
    1012             :             }
    1013     1057452 :             block[0] = level;
    1014     1057452 :             i        = 0;
    1015             :         } else {
    1016           0 :             i = -1;
    1017           0 :             ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
    1018             :         }
    1019     1057452 :         if (!coded)
    1020      214865 :             goto not_coded;
    1021             : 
    1022      842587 :         if (rvlc) {
    1023           0 :             rl     = &ff_rvlc_rl_intra;
    1024           0 :             rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
    1025             :         } else {
    1026      842587 :             rl     = &ff_mpeg4_rl_intra;
    1027      842587 :             rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
    1028             :         }
    1029      842587 :         if (s->ac_pred) {
    1030       22081 :             if (dc_pred_dir == 0)
    1031       15515 :                 scan_table = s->intra_v_scantable.permutated;  /* left */
    1032             :             else
    1033        6566 :                 scan_table = s->intra_h_scantable.permutated;  /* top */
    1034             :         } else {
    1035      820506 :             scan_table = s->intra_scantable.permutated;
    1036             :         }
    1037      842587 :         qmul = 1;
    1038      842587 :         qadd = 0;
    1039             :     } else {
    1040     6773580 :         i = -1;
    1041     6773580 :         if (!coded) {
    1042     4617682 :             s->block_last_index[n] = i;
    1043     4617682 :             return 0;
    1044             :         }
    1045     2155898 :         if (rvlc)
    1046           0 :             rl = &ff_rvlc_rl_inter;
    1047             :         else
    1048     2155898 :             rl = &ff_h263_rl_inter;
    1049             : 
    1050     2155898 :         scan_table = s->intra_scantable.permutated;
    1051             : 
    1052     2155898 :         if (s->mpeg_quant) {
    1053       11882 :             qmul = 1;
    1054       11882 :             qadd = 0;
    1055       11882 :             if (rvlc)
    1056           0 :                 rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
    1057             :             else
    1058       11882 :                 rl_vlc = ff_h263_rl_inter.rl_vlc[0];
    1059             :         } else {
    1060     2144016 :             qmul = s->qscale << 1;
    1061     2144016 :             qadd = (s->qscale - 1) | 1;
    1062     2144016 :             if (rvlc)
    1063           0 :                 rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
    1064             :             else
    1065     2144016 :                 rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
    1066             :         }
    1067             :     }
    1068             :     {
    1069     2998485 :         OPEN_READER(re, &s->gb);
    1070             :         for (;;) {
    1071    47774759 :             UPDATE_CACHE(re, &s->gb);
    1072    25386622 :             GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
    1073    25386622 :             if (level == 0) {
    1074             :                 /* escape */
    1075      740972 :                 if (rvlc) {
    1076           0 :                     if (SHOW_UBITS(re, &s->gb, 1) == 0) {
    1077           0 :                         av_log(s->avctx, AV_LOG_ERROR,
    1078             :                                "1. marker bit missing in rvlc esc\n");
    1079           0 :                         return -1;
    1080             :                     }
    1081           0 :                     SKIP_CACHE(re, &s->gb, 1);
    1082             : 
    1083           0 :                     last = SHOW_UBITS(re, &s->gb, 1);
    1084           0 :                     SKIP_CACHE(re, &s->gb, 1);
    1085           0 :                     run = SHOW_UBITS(re, &s->gb, 6);
    1086           0 :                     SKIP_COUNTER(re, &s->gb, 1 + 1 + 6);
    1087           0 :                     UPDATE_CACHE(re, &s->gb);
    1088             : 
    1089           0 :                     if (SHOW_UBITS(re, &s->gb, 1) == 0) {
    1090           0 :                         av_log(s->avctx, AV_LOG_ERROR,
    1091             :                                "2. marker bit missing in rvlc esc\n");
    1092           0 :                         return -1;
    1093             :                     }
    1094           0 :                     SKIP_CACHE(re, &s->gb, 1);
    1095             : 
    1096           0 :                     level = SHOW_UBITS(re, &s->gb, 11);
    1097           0 :                     SKIP_CACHE(re, &s->gb, 11);
    1098             : 
    1099           0 :                     if (SHOW_UBITS(re, &s->gb, 5) != 0x10) {
    1100           0 :                         av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
    1101           0 :                         return -1;
    1102             :                     }
    1103           0 :                     SKIP_CACHE(re, &s->gb, 5);
    1104             : 
    1105           0 :                     level = level * qmul + qadd;
    1106           0 :                     level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
    1107           0 :                     SKIP_COUNTER(re, &s->gb, 1 + 11 + 5 + 1);
    1108             : 
    1109           0 :                     i += run + 1;
    1110           0 :                     if (last)
    1111           0 :                         i += 192;
    1112             :                 } else {
    1113             :                     int cache;
    1114      740972 :                     cache = GET_CACHE(re, &s->gb);
    1115             : 
    1116             :                     if (IS_3IV1)
    1117             :                         cache ^= 0xC0000000;
    1118             : 
    1119      740972 :                     if (cache & 0x80000000) {
    1120      346312 :                         if (cache & 0x40000000) {
    1121             :                             /* third escape */
    1122      131775 :                             SKIP_CACHE(re, &s->gb, 2);
    1123      131775 :                             last = SHOW_UBITS(re, &s->gb, 1);
    1124      131775 :                             SKIP_CACHE(re, &s->gb, 1);
    1125      131775 :                             run = SHOW_UBITS(re, &s->gb, 6);
    1126      131775 :                             SKIP_COUNTER(re, &s->gb, 2 + 1 + 6);
    1127      131775 :                             UPDATE_CACHE(re, &s->gb);
    1128             : 
    1129             :                             if (IS_3IV1) {
    1130             :                                 level = SHOW_SBITS(re, &s->gb, 12);
    1131             :                                 LAST_SKIP_BITS(re, &s->gb, 12);
    1132             :                             } else {
    1133      131775 :                                 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
    1134           0 :                                     av_log(s->avctx, AV_LOG_ERROR,
    1135             :                                            "1. marker bit missing in 3. esc\n");
    1136           0 :                                     if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
    1137           0 :                                         return -1;
    1138             :                                 }
    1139      131775 :                                 SKIP_CACHE(re, &s->gb, 1);
    1140             : 
    1141      131775 :                                 level = SHOW_SBITS(re, &s->gb, 12);
    1142      131775 :                                 SKIP_CACHE(re, &s->gb, 12);
    1143             : 
    1144      131775 :                                 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
    1145           0 :                                     av_log(s->avctx, AV_LOG_ERROR,
    1146             :                                            "2. marker bit missing in 3. esc\n");
    1147           0 :                                     if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
    1148           0 :                                         return -1;
    1149             :                                 }
    1150             : 
    1151      131775 :                                 SKIP_COUNTER(re, &s->gb, 1 + 12 + 1);
    1152             :                             }
    1153             : 
    1154             : #if 0
    1155             :                             if (s->error_recognition >= FF_ER_COMPLIANT) {
    1156             :                                 const int abs_level= FFABS(level);
    1157             :                                 if (abs_level<=MAX_LEVEL && run<=MAX_RUN) {
    1158             :                                     const int run1= run - rl->max_run[last][abs_level] - 1;
    1159             :                                     if (abs_level <= rl->max_level[last][run]) {
    1160             :                                         av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
    1161             :                                         return -1;
    1162             :                                     }
    1163             :                                     if (s->error_recognition > FF_ER_COMPLIANT) {
    1164             :                                         if (abs_level <= rl->max_level[last][run]*2) {
    1165             :                                             av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
    1166             :                                             return -1;
    1167             :                                         }
    1168             :                                         if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) {
    1169             :                                             av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
    1170             :                                             return -1;
    1171             :                                         }
    1172             :                                     }
    1173             :                                 }
    1174             :                             }
    1175             : #endif
    1176      131775 :                             if (level > 0)
    1177       66110 :                                 level = level * qmul + qadd;
    1178             :                             else
    1179       65665 :                                 level = level * qmul - qadd;
    1180             : 
    1181      131775 :                             if ((unsigned)(level + 2048) > 4095) {
    1182           0 :                                 if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)) {
    1183           0 :                                     if (level > 2560 || level < -2560) {
    1184           0 :                                         av_log(s->avctx, AV_LOG_ERROR,
    1185             :                                                "|level| overflow in 3. esc, qp=%d\n",
    1186             :                                                s->qscale);
    1187           0 :                                         return -1;
    1188             :                                     }
    1189             :                                 }
    1190           0 :                                 level = level < 0 ? -2048 : 2047;
    1191             :                             }
    1192             : 
    1193      131775 :                             i += run + 1;
    1194      131775 :                             if (last)
    1195       43135 :                                 i += 192;
    1196             :                         } else {
    1197             :                             /* second escape */
    1198      214537 :                             SKIP_BITS(re, &s->gb, 2);
    1199      214537 :                             GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
    1200      214537 :                             i    += run + rl->max_run[run >> 7][level / qmul] + 1;  // FIXME opt indexing
    1201      214537 :                             level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
    1202      214537 :                             LAST_SKIP_BITS(re, &s->gb, 1);
    1203             :                         }
    1204             :                     } else {
    1205             :                         /* first escape */
    1206      394660 :                         SKIP_BITS(re, &s->gb, 1);
    1207      394660 :                         GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
    1208      394660 :                         i    += run;
    1209      394660 :                         level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul;  // FIXME opt indexing
    1210      394660 :                         level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
    1211      394660 :                         LAST_SKIP_BITS(re, &s->gb, 1);
    1212             :                     }
    1213             :                 }
    1214             :             } else {
    1215    24645650 :                 i    += run;
    1216    24645650 :                 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
    1217    24645650 :                 LAST_SKIP_BITS(re, &s->gb, 1);
    1218             :             }
    1219             :             ff_tlog(s->avctx, "dct[%d][%d] = %- 4d end?:%d\n", scan_table[i&63]&7, scan_table[i&63] >> 3, level, i>62);
    1220    25386622 :             if (i > 62) {
    1221     2998485 :                 i -= 192;
    1222     2998485 :                 if (i & (~63)) {
    1223           0 :                     av_log(s->avctx, AV_LOG_ERROR,
    1224             :                            "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
    1225           0 :                     return -1;
    1226             :                 }
    1227             : 
    1228     2998485 :                 block[scan_table[i]] = level;
    1229     2998485 :                 break;
    1230             :             }
    1231             : 
    1232    22388137 :             block[scan_table[i]] = level;
    1233             :         }
    1234     2998485 :         CLOSE_READER(re, &s->gb);
    1235             :     }
    1236             : 
    1237     3213350 : not_coded:
    1238     3213350 :     if (intra) {
    1239     1057452 :         if (!ctx->use_intra_dc_vlc) {
    1240           0 :             block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
    1241             : 
    1242           0 :             i -= i >> 31;  // if (i == -1) i = 0;
    1243             :         }
    1244             : 
    1245     1057452 :         ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
    1246     1057452 :         if (s->ac_pred)
    1247       25890 :             i = 63;  // FIXME not optimal
    1248             :     }
    1249     3213350 :     s->block_last_index[n] = i;
    1250     3213350 :     return 0;
    1251             : }
    1252             : 
    1253             : /**
    1254             :  * decode partition C of one MB.
    1255             :  * @return <0 if an error occurred
    1256             :  */
    1257      138064 : static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
    1258             : {
    1259      138064 :     Mpeg4DecContext *ctx = (Mpeg4DecContext *)s;
    1260             :     int cbp, mb_type;
    1261      138064 :     const int xy = s->mb_x + s->mb_y * s->mb_stride;
    1262             : 
    1263      138064 :     mb_type = s->current_picture.mb_type[xy];
    1264      138064 :     cbp     = s->cbp_table[xy];
    1265             : 
    1266      138064 :     ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
    1267             : 
    1268      138064 :     if (s->current_picture.qscale_table[xy] != s->qscale)
    1269           0 :         ff_set_qscale(s, s->current_picture.qscale_table[xy]);
    1270             : 
    1271      155387 :     if (s->pict_type == AV_PICTURE_TYPE_P ||
    1272      138064 :         s->pict_type == AV_PICTURE_TYPE_S) {
    1273             :         int i;
    1274      603705 :         for (i = 0; i < 4; i++) {
    1275      482964 :             s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
    1276      482964 :             s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
    1277             :         }
    1278      120741 :         s->mb_intra = IS_INTRA(mb_type);
    1279             : 
    1280      120741 :         if (IS_SKIP(mb_type)) {
    1281             :             /* skip mb */
    1282        6979 :             for (i = 0; i < 6; i++)
    1283        5982 :                 s->block_last_index[i] = -1;
    1284         997 :             s->mv_dir  = MV_DIR_FORWARD;
    1285         997 :             s->mv_type = MV_TYPE_16X16;
    1286         997 :             if (s->pict_type == AV_PICTURE_TYPE_S
    1287           0 :                 && ctx->vol_sprite_usage == GMC_SPRITE) {
    1288           0 :                 s->mcsel      = 1;
    1289           0 :                 s->mb_skipped = 0;
    1290             :             } else {
    1291         997 :                 s->mcsel      = 0;
    1292         997 :                 s->mb_skipped = 1;
    1293             :             }
    1294      119744 :         } else if (s->mb_intra) {
    1295        6287 :             s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
    1296      113457 :         } else if (!s->mb_intra) {
    1297             :             // s->mcsel = 0;  // FIXME do we need to init that?
    1298             : 
    1299      113457 :             s->mv_dir = MV_DIR_FORWARD;
    1300      113457 :             if (IS_8X8(mb_type)) {
    1301       31831 :                 s->mv_type = MV_TYPE_8X8;
    1302             :             } else {
    1303       81626 :                 s->mv_type = MV_TYPE_16X16;
    1304             :             }
    1305             :         }
    1306             :     } else { /* I-Frame */
    1307       17323 :         s->mb_intra = 1;
    1308       17323 :         s->ac_pred  = IS_ACPRED(s->current_picture.mb_type[xy]);
    1309             :     }
    1310             : 
    1311      138064 :     if (!IS_SKIP(mb_type)) {
    1312             :         int i;
    1313      137067 :         s->bdsp.clear_blocks(s->block[0]);
    1314             :         /* decode each block */
    1315      959469 :         for (i = 0; i < 6; i++) {
    1316      822402 :             if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, s->mb_intra, ctx->rvlc) < 0) {
    1317           0 :                 av_log(s->avctx, AV_LOG_ERROR,
    1318             :                        "texture corrupted at %d %d %d\n",
    1319             :                        s->mb_x, s->mb_y, s->mb_intra);
    1320           0 :                 return -1;
    1321             :             }
    1322      822402 :             cbp += cbp;
    1323             :         }
    1324             :     }
    1325             : 
    1326             :     /* per-MB end of slice check */
    1327      138064 :     if (--s->mb_num_left <= 0) {
    1328        1728 :         if (mpeg4_is_resync(ctx))
    1329        1728 :             return SLICE_END;
    1330             :         else
    1331           0 :             return SLICE_NOEND;
    1332             :     } else {
    1333      136336 :         if (mpeg4_is_resync(ctx)) {
    1334         308 :             const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
    1335         308 :             if (s->cbp_table[xy + delta])
    1336           0 :                 return SLICE_END;
    1337             :         }
    1338      136336 :         return SLICE_OK;
    1339             :     }
    1340             : }
    1341             : 
    1342     1281928 : static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
    1343             : {
    1344     1281928 :     Mpeg4DecContext *ctx = (Mpeg4DecContext *)s;
    1345             :     int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
    1346             :     int16_t *mot_val;
    1347             :     static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
    1348     1281928 :     const int xy = s->mb_x + s->mb_y * s->mb_stride;
    1349             : 
    1350             :     av_assert2(s->h263_pred);
    1351             : 
    1352     1644835 :     if (s->pict_type == AV_PICTURE_TYPE_P ||
    1353      362907 :         s->pict_type == AV_PICTURE_TYPE_S) {
    1354             :         do {
    1355      919021 :             if (get_bits1(&s->gb)) {
    1356             :                 /* skip mb */
    1357       80953 :                 s->mb_intra = 0;
    1358      566671 :                 for (i = 0; i < 6; i++)
    1359      485718 :                     s->block_last_index[i] = -1;
    1360       80953 :                 s->mv_dir  = MV_DIR_FORWARD;
    1361       80953 :                 s->mv_type = MV_TYPE_16X16;
    1362       80953 :                 if (s->pict_type == AV_PICTURE_TYPE_S &&
    1363           0 :                     ctx->vol_sprite_usage == GMC_SPRITE) {
    1364           0 :                     s->current_picture.mb_type[xy] = MB_TYPE_SKIP  |
    1365             :                                                      MB_TYPE_GMC   |
    1366             :                                                      MB_TYPE_16x16 |
    1367             :                                                      MB_TYPE_L0;
    1368           0 :                     s->mcsel       = 1;
    1369           0 :                     s->mv[0][0][0] = get_amv(ctx, 0);
    1370           0 :                     s->mv[0][0][1] = get_amv(ctx, 1);
    1371           0 :                     s->mb_skipped  = 0;
    1372             :                 } else {
    1373       80953 :                     s->current_picture.mb_type[xy] = MB_TYPE_SKIP  |
    1374             :                                                      MB_TYPE_16x16 |
    1375             :                                                      MB_TYPE_L0;
    1376       80953 :                     s->mcsel       = 0;
    1377       80953 :                     s->mv[0][0][0] = 0;
    1378       80953 :                     s->mv[0][0][1] = 0;
    1379       80953 :                     s->mb_skipped  = 1;
    1380             :                 }
    1381       80953 :                 goto end;
    1382             :             }
    1383      838068 :             cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
    1384      838068 :             if (cbpc < 0) {
    1385           0 :                 av_log(s->avctx, AV_LOG_ERROR,
    1386             :                        "mcbpc damaged at %d %d\n", s->mb_x, s->mb_y);
    1387           0 :                 return -1;
    1388             :             }
    1389      838068 :         } while (cbpc == 20);
    1390             : 
    1391      838068 :         s->bdsp.clear_blocks(s->block[0]);
    1392      838068 :         dquant      = cbpc & 8;
    1393      838068 :         s->mb_intra = ((cbpc & 4) != 0);
    1394      838068 :         if (s->mb_intra)
    1395       26549 :             goto intra;
    1396             : 
    1397      811519 :         if (s->pict_type == AV_PICTURE_TYPE_S &&
    1398           0 :             ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
    1399           0 :             s->mcsel = get_bits1(&s->gb);
    1400             :         else
    1401      811519 :             s->mcsel = 0;
    1402      811519 :         cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
    1403      811519 :         if (cbpy < 0) {
    1404           0 :             av_log(s->avctx, AV_LOG_ERROR,
    1405             :                    "P cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
    1406           0 :             return AVERROR_INVALIDDATA;
    1407             :         }
    1408             : 
    1409      811519 :         cbp = (cbpc & 3) | (cbpy << 2);
    1410      811519 :         if (dquant)
    1411        8916 :             ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
    1412      811519 :         if ((!s->progressive_sequence) &&
    1413           0 :             (cbp || (s->workaround_bugs & FF_BUG_XVID_ILACE)))
    1414           0 :             s->interlaced_dct = get_bits1(&s->gb);
    1415             : 
    1416      811519 :         s->mv_dir = MV_DIR_FORWARD;
    1417     1623038 :         if ((cbpc & 16) == 0) {
    1418      720392 :             if (s->mcsel) {
    1419           0 :                 s->current_picture.mb_type[xy] = MB_TYPE_GMC   |
    1420             :                                                  MB_TYPE_16x16 |
    1421             :                                                  MB_TYPE_L0;
    1422             :                 /* 16x16 global motion prediction */
    1423           0 :                 s->mv_type     = MV_TYPE_16X16;
    1424           0 :                 mx             = get_amv(ctx, 0);
    1425           0 :                 my             = get_amv(ctx, 1);
    1426           0 :                 s->mv[0][0][0] = mx;
    1427           0 :                 s->mv[0][0][1] = my;
    1428      720392 :             } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
    1429           0 :                 s->current_picture.mb_type[xy] = MB_TYPE_16x8 |
    1430             :                                                  MB_TYPE_L0   |
    1431             :                                                  MB_TYPE_INTERLACED;
    1432             :                 /* 16x8 field motion prediction */
    1433           0 :                 s->mv_type = MV_TYPE_FIELD;
    1434             : 
    1435           0 :                 s->field_select[0][0] = get_bits1(&s->gb);
    1436           0 :                 s->field_select[0][1] = get_bits1(&s->gb);
    1437             : 
    1438           0 :                 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
    1439             : 
    1440           0 :                 for (i = 0; i < 2; i++) {
    1441           0 :                     mx = ff_h263_decode_motion(s, pred_x, s->f_code);
    1442           0 :                     if (mx >= 0xffff)
    1443           0 :                         return -1;
    1444             : 
    1445           0 :                     my = ff_h263_decode_motion(s, pred_y / 2, s->f_code);
    1446           0 :                     if (my >= 0xffff)
    1447           0 :                         return -1;
    1448             : 
    1449           0 :                     s->mv[0][i][0] = mx;
    1450           0 :                     s->mv[0][i][1] = my;
    1451             :                 }
    1452             :             } else {
    1453      720392 :                 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
    1454             :                 /* 16x16 motion prediction */
    1455      720392 :                 s->mv_type = MV_TYPE_16X16;
    1456      720392 :                 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
    1457      720392 :                 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
    1458             : 
    1459      720392 :                 if (mx >= 0xffff)
    1460           0 :                     return -1;
    1461             : 
    1462      720392 :                 my = ff_h263_decode_motion(s, pred_y, s->f_code);
    1463             : 
    1464      720392 :                 if (my >= 0xffff)
    1465           0 :                     return -1;
    1466      720392 :                 s->mv[0][0][0] = mx;
    1467      720392 :                 s->mv[0][0][1] = my;
    1468             :             }
    1469             :         } else {
    1470       91127 :             s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
    1471       91127 :             s->mv_type                     = MV_TYPE_8X8;
    1472      455635 :             for (i = 0; i < 4; i++) {
    1473      364508 :                 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
    1474      364508 :                 mx      = ff_h263_decode_motion(s, pred_x, s->f_code);
    1475      364508 :                 if (mx >= 0xffff)
    1476           0 :                     return -1;
    1477             : 
    1478      364508 :                 my = ff_h263_decode_motion(s, pred_y, s->f_code);
    1479      364508 :                 if (my >= 0xffff)
    1480           0 :                     return -1;
    1481      364508 :                 s->mv[0][i][0] = mx;
    1482      364508 :                 s->mv[0][i][1] = my;
    1483      364508 :                 mot_val[0]     = mx;
    1484      364508 :                 mot_val[1]     = my;
    1485             :             }
    1486             :         }
    1487      362907 :     } else if (s->pict_type == AV_PICTURE_TYPE_B) {
    1488             :         int modb1;   // first bit of modb
    1489             :         int modb2;   // second bit of modb
    1490             :         int mb_type;
    1491             : 
    1492      236824 :         s->mb_intra = 0;  // B-frames never contain intra blocks
    1493      236824 :         s->mcsel    = 0;  //      ...               true gmc blocks
    1494             : 
    1495      236824 :         if (s->mb_x == 0) {
    1496       30576 :             for (i = 0; i < 2; i++) {
    1497       20384 :                 s->last_mv[i][0][0] =
    1498       20384 :                 s->last_mv[i][0][1] =
    1499       20384 :                 s->last_mv[i][1][0] =
    1500       20384 :                 s->last_mv[i][1][1] = 0;
    1501             :             }
    1502             : 
    1503       10192 :             ff_thread_await_progress(&s->next_picture_ptr->tf, s->mb_y, 0);
    1504             :         }
    1505             : 
    1506             :         /* if we skipped it in the future P-frame than skip it now too */
    1507      236824 :         s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x];  // Note, skiptab=0 if last was GMC
    1508             : 
    1509      236824 :         if (s->mb_skipped) {
    1510             :             /* skip mb */
    1511      230090 :             for (i = 0; i < 6; i++)
    1512      197220 :                 s->block_last_index[i] = -1;
    1513             : 
    1514       32870 :             s->mv_dir      = MV_DIR_FORWARD;
    1515       32870 :             s->mv_type     = MV_TYPE_16X16;
    1516       32870 :             s->mv[0][0][0] =
    1517       32870 :             s->mv[0][0][1] =
    1518       32870 :             s->mv[1][0][0] =
    1519       32870 :             s->mv[1][0][1] = 0;
    1520       32870 :             s->current_picture.mb_type[xy] = MB_TYPE_SKIP  |
    1521             :                                              MB_TYPE_16x16 |
    1522             :                                              MB_TYPE_L0;
    1523       32870 :             goto end;
    1524             :         }
    1525             : 
    1526      203954 :         modb1 = get_bits1(&s->gb);
    1527      203954 :         if (modb1) {
    1528             :             // like MB_TYPE_B_DIRECT but no vectors coded
    1529       44804 :             mb_type = MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1;
    1530       44804 :             cbp     = 0;
    1531             :         } else {
    1532      159150 :             modb2   = get_bits1(&s->gb);
    1533      159150 :             mb_type = get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
    1534      159150 :             if (mb_type < 0) {
    1535           0 :                 av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
    1536           0 :                 return -1;
    1537             :             }
    1538      159150 :             mb_type = mb_type_b_map[mb_type];
    1539      159150 :             if (modb2) {
    1540       67177 :                 cbp = 0;
    1541             :             } else {
    1542       91973 :                 s->bdsp.clear_blocks(s->block[0]);
    1543       91973 :                 cbp = get_bits(&s->gb, 6);
    1544             :             }
    1545             : 
    1546      159150 :             if ((!IS_DIRECT(mb_type)) && cbp) {
    1547       66247 :                 if (get_bits1(&s->gb))
    1548        6257 :                     ff_set_qscale(s, s->qscale + get_bits1(&s->gb) * 4 - 2);
    1549             :             }
    1550             : 
    1551      159150 :             if (!s->progressive_sequence) {
    1552           0 :                 if (cbp)
    1553           0 :                     s->interlaced_dct = get_bits1(&s->gb);
    1554             : 
    1555           0 :                 if (!IS_DIRECT(mb_type) && get_bits1(&s->gb)) {
    1556           0 :                     mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
    1557           0 :                     mb_type &= ~MB_TYPE_16x16;
    1558             : 
    1559           0 :                     if (USES_LIST(mb_type, 0)) {
    1560           0 :                         s->field_select[0][0] = get_bits1(&s->gb);
    1561           0 :                         s->field_select[0][1] = get_bits1(&s->gb);
    1562             :                     }
    1563           0 :                     if (USES_LIST(mb_type, 1)) {
    1564           0 :                         s->field_select[1][0] = get_bits1(&s->gb);
    1565           0 :                         s->field_select[1][1] = get_bits1(&s->gb);
    1566             :                     }
    1567             :                 }
    1568             :             }
    1569             : 
    1570      159150 :             s->mv_dir = 0;
    1571      159150 :             if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
    1572      117472 :                 s->mv_type = MV_TYPE_16X16;
    1573             : 
    1574      117472 :                 if (USES_LIST(mb_type, 0)) {
    1575       81428 :                     s->mv_dir = MV_DIR_FORWARD;
    1576             : 
    1577       81428 :                     mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
    1578       81428 :                     my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
    1579       81428 :                     s->last_mv[0][1][0] =
    1580       81428 :                     s->last_mv[0][0][0] =
    1581       81428 :                     s->mv[0][0][0]      = mx;
    1582       81428 :                     s->last_mv[0][1][1] =
    1583       81428 :                     s->last_mv[0][0][1] =
    1584       81428 :                     s->mv[0][0][1]      = my;
    1585             :                 }
    1586             : 
    1587      117472 :                 if (USES_LIST(mb_type, 1)) {
    1588       92202 :                     s->mv_dir |= MV_DIR_BACKWARD;
    1589             : 
    1590       92202 :                     mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
    1591       92202 :                     my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
    1592       92202 :                     s->last_mv[1][1][0] =
    1593       92202 :                     s->last_mv[1][0][0] =
    1594       92202 :                     s->mv[1][0][0]      = mx;
    1595       92202 :                     s->last_mv[1][1][1] =
    1596       92202 :                     s->last_mv[1][0][1] =
    1597       92202 :                     s->mv[1][0][1]      = my;
    1598             :                 }
    1599       41678 :             } else if (!IS_DIRECT(mb_type)) {
    1600           0 :                 s->mv_type = MV_TYPE_FIELD;
    1601             : 
    1602           0 :                 if (USES_LIST(mb_type, 0)) {
    1603           0 :                     s->mv_dir = MV_DIR_FORWARD;
    1604             : 
    1605           0 :                     for (i = 0; i < 2; i++) {
    1606           0 :                         mx = ff_h263_decode_motion(s, s->last_mv[0][i][0], s->f_code);
    1607           0 :                         my = ff_h263_decode_motion(s, s->last_mv[0][i][1] / 2, s->f_code);
    1608           0 :                         s->last_mv[0][i][0] =
    1609           0 :                         s->mv[0][i][0]      = mx;
    1610           0 :                         s->last_mv[0][i][1] = (s->mv[0][i][1] = my) * 2;
    1611             :                     }
    1612             :                 }
    1613             : 
    1614           0 :                 if (USES_LIST(mb_type, 1)) {
    1615           0 :                     s->mv_dir |= MV_DIR_BACKWARD;
    1616             : 
    1617           0 :                     for (i = 0; i < 2; i++) {
    1618           0 :                         mx = ff_h263_decode_motion(s, s->last_mv[1][i][0], s->b_code);
    1619           0 :                         my = ff_h263_decode_motion(s, s->last_mv[1][i][1] / 2, s->b_code);
    1620           0 :                         s->last_mv[1][i][0] =
    1621           0 :                         s->mv[1][i][0]      = mx;
    1622           0 :                         s->last_mv[1][i][1] = (s->mv[1][i][1] = my) * 2;
    1623             :                     }
    1624             :                 }
    1625             :             }
    1626             :         }
    1627             : 
    1628      203954 :         if (IS_DIRECT(mb_type)) {
    1629       86482 :             if (IS_SKIP(mb_type)) {
    1630       44804 :                 mx =
    1631       44804 :                 my = 0;
    1632             :             } else {
    1633       41678 :                 mx = ff_h263_decode_motion(s, 0, 1);
    1634       41678 :                 my = ff_h263_decode_motion(s, 0, 1);
    1635             :             }
    1636             : 
    1637       86482 :             s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
    1638       86482 :             mb_type  |= ff_mpeg4_set_direct_mv(s, mx, my);
    1639             :         }
    1640      203954 :         s->current_picture.mb_type[xy] = mb_type;
    1641             :     } else { /* I-Frame */
    1642             :         do {
    1643      126083 :             cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
    1644      126083 :             if (cbpc < 0) {
    1645           0 :                 av_log(s->avctx, AV_LOG_ERROR,
    1646             :                        "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
    1647           0 :                 return -1;
    1648             :             }
    1649      126083 :         } while (cbpc == 8);
    1650             : 
    1651      126083 :         dquant = cbpc & 4;
    1652      126083 :         s->mb_intra = 1;
    1653             : 
    1654      152632 : intra:
    1655      152632 :         s->ac_pred = get_bits1(&s->gb);
    1656      152632 :         if (s->ac_pred)
    1657         552 :             s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
    1658             :         else
    1659      152080 :             s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
    1660             : 
    1661      152632 :         cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
    1662      152632 :         if (cbpy < 0) {
    1663           0 :             av_log(s->avctx, AV_LOG_ERROR,
    1664             :                    "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
    1665           0 :             return -1;
    1666             :         }
    1667      152632 :         cbp = (cbpc & 3) | (cbpy << 2);
    1668             : 
    1669      152632 :         ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
    1670             : 
    1671      152632 :         if (dquant)
    1672        6288 :             ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
    1673             : 
    1674      152632 :         if (!s->progressive_sequence)
    1675           0 :             s->interlaced_dct = get_bits1(&s->gb);
    1676             : 
    1677      152632 :         s->bdsp.clear_blocks(s->block[0]);
    1678             :         /* decode each block */
    1679     1068424 :         for (i = 0; i < 6; i++) {
    1680      915792 :             if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 1, 0) < 0)
    1681           0 :                 return -1;
    1682      915792 :             cbp += cbp;
    1683             :         }
    1684      152632 :         goto end;
    1685             :     }
    1686             : 
    1687             :     /* decode each block */
    1688     7108311 :     for (i = 0; i < 6; i++) {
    1689     6092838 :         if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 0, 0) < 0)
    1690           0 :             return -1;
    1691     6092838 :         cbp += cbp;
    1692             :     }
    1693             : 
    1694     1015473 : end:
    1695             :     /* per-MB end of slice check */
    1696     1281928 :     if (s->codec_id == AV_CODEC_ID_MPEG4) {
    1697     1281928 :         int next = mpeg4_is_resync(ctx);
    1698     1281928 :         if (next) {
    1699       13394 :             if        (s->mb_x + s->mb_y*s->mb_width + 1 >  next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
    1700           0 :                 return -1;
    1701       13394 :             } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
    1702        5450 :                 return SLICE_END;
    1703             : 
    1704        7944 :             if (s->pict_type == AV_PICTURE_TYPE_B) {
    1705        7944 :                 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
    1706        8112 :                 ff_thread_await_progress(&s->next_picture_ptr->tf,
    1707        7944 :                                          (s->mb_x + delta >= s->mb_width)
    1708         168 :                                          ? FFMIN(s->mb_y + 1, s->mb_height - 1)
    1709             :                                          : s->mb_y, 0);
    1710        7944 :                 if (s->next_picture.mbskip_table[xy + delta])
    1711        7944 :                     return SLICE_OK;
    1712             :             }
    1713             : 
    1714           0 :             return SLICE_END;
    1715             :         }
    1716             :     }
    1717             : 
    1718     1268534 :     return SLICE_OK;
    1719             : }
    1720             : 
    1721         945 : static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
    1722             : {
    1723             :     int hours, minutes, seconds;
    1724             : 
    1725         945 :     if (!show_bits(gb, 23)) {
    1726           0 :         av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
    1727           0 :         return -1;
    1728             :     }
    1729             : 
    1730         945 :     hours   = get_bits(gb, 5);
    1731         945 :     minutes = get_bits(gb, 6);
    1732         945 :     check_marker(s->avctx, gb, "in gop_header");
    1733         945 :     seconds = get_bits(gb, 6);
    1734             : 
    1735         945 :     s->time_base = seconds + 60*(minutes + 60*hours);
    1736             : 
    1737         945 :     skip_bits1(gb);
    1738         945 :     skip_bits1(gb);
    1739             : 
    1740         945 :     return 0;
    1741             : }
    1742             : 
    1743        1202 : static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb)
    1744             : {
    1745             : 
    1746        1202 :     s->avctx->profile = get_bits(gb, 4);
    1747        1202 :     s->avctx->level   = get_bits(gb, 4);
    1748             : 
    1749             :     // for Simple profile, level 0
    1750        1202 :     if (s->avctx->profile == 0 && s->avctx->level == 8) {
    1751           0 :         s->avctx->level = 0;
    1752             :     }
    1753             : 
    1754        1202 :     return 0;
    1755             : }
    1756             : 
    1757        1234 : static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
    1758             : {
    1759        1234 :     MpegEncContext *s = &ctx->m;
    1760             :     int width, height, vo_ver_id;
    1761             : 
    1762             :     /* vol header */
    1763        1234 :     skip_bits(gb, 1);                   /* random access */
    1764        1234 :     s->vo_type = get_bits(gb, 8);
    1765        1234 :     if (get_bits1(gb) != 0) {           /* is_ol_id */
    1766        1198 :         vo_ver_id = get_bits(gb, 4);    /* vo_ver_id */
    1767        1198 :         skip_bits(gb, 3);               /* vo_priority */
    1768             :     } else {
    1769          36 :         vo_ver_id = 1;
    1770             :     }
    1771        1234 :     s->aspect_ratio_info = get_bits(gb, 4);
    1772        1234 :     if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
    1773           0 :         s->avctx->sample_aspect_ratio.num = get_bits(gb, 8);  // par_width
    1774           0 :         s->avctx->sample_aspect_ratio.den = get_bits(gb, 8);  // par_height
    1775             :     } else {
    1776        1234 :         s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
    1777             :     }
    1778             : 
    1779        1234 :     if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
    1780        1194 :         int chroma_format = get_bits(gb, 2);
    1781        1194 :         if (chroma_format != CHROMA_420)
    1782           0 :             av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
    1783             : 
    1784        1194 :         s->low_delay = get_bits1(gb);
    1785        1194 :         if (get_bits1(gb)) {    /* vbv parameters */
    1786           0 :             get_bits(gb, 15);   /* first_half_bitrate */
    1787           0 :             check_marker(s->avctx, gb, "after first_half_bitrate");
    1788           0 :             get_bits(gb, 15);   /* latter_half_bitrate */
    1789           0 :             check_marker(s->avctx, gb, "after latter_half_bitrate");
    1790           0 :             get_bits(gb, 15);   /* first_half_vbv_buffer_size */
    1791           0 :             check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
    1792           0 :             get_bits(gb, 3);    /* latter_half_vbv_buffer_size */
    1793           0 :             get_bits(gb, 11);   /* first_half_vbv_occupancy */
    1794           0 :             check_marker(s->avctx, gb, "after first_half_vbv_occupancy");
    1795           0 :             get_bits(gb, 15);   /* latter_half_vbv_occupancy */
    1796           0 :             check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
    1797             :         }
    1798             :     } else {
    1799             :         /* is setting low delay flag only once the smartest thing to do?
    1800             :          * low delay detection will not be overridden. */
    1801          40 :         if (s->picture_number == 0) {
    1802          11 :             switch(s->vo_type) {
    1803           6 :             case SIMPLE_VO_TYPE:
    1804             :             case ADV_SIMPLE_VO_TYPE:
    1805           6 :                 s->low_delay = 1;
    1806           6 :                 break;
    1807           5 :             default:
    1808           5 :                 s->low_delay = 0;
    1809             :             }
    1810          29 :         }
    1811             :     }
    1812             : 
    1813        1234 :     ctx->shape = get_bits(gb, 2); /* vol shape */
    1814        1234 :     if (ctx->shape != RECT_SHAPE)
    1815           0 :         av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
    1816        1234 :     if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
    1817           0 :         av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
    1818           0 :         skip_bits(gb, 4);  /* video_object_layer_shape_extension */
    1819             :     }
    1820             : 
    1821        1234 :     check_marker(s->avctx, gb, "before time_increment_resolution");
    1822             : 
    1823        1234 :     s->avctx->framerate.num = get_bits(gb, 16);
    1824        1234 :     if (!s->avctx->framerate.num) {
    1825           0 :         av_log(s->avctx, AV_LOG_ERROR, "framerate==0\n");
    1826           0 :         return AVERROR_INVALIDDATA;
    1827             :     }
    1828             : 
    1829        1234 :     ctx->time_increment_bits = av_log2(s->avctx->framerate.num - 1) + 1;
    1830        1234 :     if (ctx->time_increment_bits < 1)
    1831           0 :         ctx->time_increment_bits = 1;
    1832             : 
    1833        1234 :     check_marker(s->avctx, gb, "before fixed_vop_rate");
    1834             : 
    1835        1234 :     if (get_bits1(gb) != 0)     /* fixed_vop_rate  */
    1836          26 :         s->avctx->framerate.den = get_bits(gb, ctx->time_increment_bits);
    1837             :     else
    1838        1208 :         s->avctx->framerate.den = 1;
    1839             : 
    1840        1234 :     s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
    1841             : 
    1842        1234 :     ctx->t_frame = 0;
    1843             : 
    1844        1234 :     if (ctx->shape != BIN_ONLY_SHAPE) {
    1845        1234 :         if (ctx->shape == RECT_SHAPE) {
    1846        1234 :             check_marker(s->avctx, gb, "before width");
    1847        1234 :             width = get_bits(gb, 13);
    1848        1234 :             check_marker(s->avctx, gb, "before height");
    1849        1234 :             height = get_bits(gb, 13);
    1850        1234 :             check_marker(s->avctx, gb, "after height");
    1851        2468 :             if (width && height &&  /* they should be non zero but who knows */
    1852        2136 :                 !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
    1853        2136 :                 if (s->width && s->height &&
    1854        1784 :                     (s->width != width || s->height != height))
    1855          20 :                     s->context_reinit = 1;
    1856        1234 :                 s->width  = width;
    1857        1234 :                 s->height = height;
    1858             :             }
    1859             :         }
    1860             : 
    1861        1234 :         s->progressive_sequence  =
    1862        1234 :         s->progressive_frame     = get_bits1(gb) ^ 1;
    1863        1234 :         s->interlaced_dct        = 0;
    1864        1234 :         if (!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
    1865           0 :             av_log(s->avctx, AV_LOG_INFO,           /* OBMC Disable */
    1866             :                    "MPEG-4 OBMC not supported (very likely buggy encoder)\n");
    1867        1234 :         if (vo_ver_id == 1)
    1868         744 :             ctx->vol_sprite_usage = get_bits1(gb);    /* vol_sprite_usage */
    1869             :         else
    1870         490 :             ctx->vol_sprite_usage = get_bits(gb, 2);  /* vol_sprite_usage */
    1871             : 
    1872        1234 :         if (ctx->vol_sprite_usage == STATIC_SPRITE)
    1873           0 :             av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
    1874        2468 :         if (ctx->vol_sprite_usage == STATIC_SPRITE ||
    1875        1234 :             ctx->vol_sprite_usage == GMC_SPRITE) {
    1876           0 :             if (ctx->vol_sprite_usage == STATIC_SPRITE) {
    1877           0 :                 skip_bits(gb, 13); // sprite_width
    1878           0 :                 check_marker(s->avctx, gb, "after sprite_width");
    1879           0 :                 skip_bits(gb, 13); // sprite_height
    1880           0 :                 check_marker(s->avctx, gb, "after sprite_height");
    1881           0 :                 skip_bits(gb, 13); // sprite_left
    1882           0 :                 check_marker(s->avctx, gb, "after sprite_left");
    1883           0 :                 skip_bits(gb, 13); // sprite_top
    1884           0 :                 check_marker(s->avctx, gb, "after sprite_top");
    1885             :             }
    1886           0 :             ctx->num_sprite_warping_points = get_bits(gb, 6);
    1887           0 :             if (ctx->num_sprite_warping_points > 3) {
    1888           0 :                 av_log(s->avctx, AV_LOG_ERROR,
    1889             :                        "%d sprite_warping_points\n",
    1890             :                        ctx->num_sprite_warping_points);
    1891           0 :                 ctx->num_sprite_warping_points = 0;
    1892           0 :                 return AVERROR_INVALIDDATA;
    1893             :             }
    1894           0 :             s->sprite_warping_accuracy  = get_bits(gb, 2);
    1895           0 :             ctx->sprite_brightness_change = get_bits1(gb);
    1896           0 :             if (ctx->vol_sprite_usage == STATIC_SPRITE)
    1897           0 :                 skip_bits1(gb); // low_latency_sprite
    1898             :         }
    1899             :         // FIXME sadct disable bit if verid!=1 && shape not rect
    1900             : 
    1901        1234 :         if (get_bits1(gb) == 1) {                   /* not_8_bit */
    1902           0 :             s->quant_precision = get_bits(gb, 4);   /* quant_precision */
    1903           0 :             if (get_bits(gb, 4) != 8)               /* bits_per_pixel */
    1904           0 :                 av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
    1905           0 :             if (s->quant_precision != 5)
    1906           0 :                 av_log(s->avctx, AV_LOG_ERROR,
    1907             :                        "quant precision %d\n", s->quant_precision);
    1908           0 :             if (s->quant_precision<3 || s->quant_precision>9) {
    1909           0 :                 s->quant_precision = 5;
    1910             :             }
    1911             :         } else {
    1912        1234 :             s->quant_precision = 5;
    1913             :         }
    1914             : 
    1915             :         // FIXME a bunch of grayscale shape things
    1916             : 
    1917        1234 :         if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
    1918             :             int i, v;
    1919             : 
    1920             :             /* load default matrixes */
    1921        1495 :             for (i = 0; i < 64; i++) {
    1922        1472 :                 int j = s->idsp.idct_permutation[i];
    1923        1472 :                 v = ff_mpeg4_default_intra_matrix[i];
    1924        1472 :                 s->intra_matrix[j]        = v;
    1925        1472 :                 s->chroma_intra_matrix[j] = v;
    1926             : 
    1927        1472 :                 v = ff_mpeg4_default_non_intra_matrix[i];
    1928        1472 :                 s->inter_matrix[j]        = v;
    1929        1472 :                 s->chroma_inter_matrix[j] = v;
    1930             :             }
    1931             : 
    1932             :             /* load custom intra matrix */
    1933          23 :             if (get_bits1(gb)) {
    1934          12 :                 int last = 0;
    1935         360 :                 for (i = 0; i < 64; i++) {
    1936             :                     int j;
    1937         360 :                     if (get_bits_left(gb) < 8) {
    1938           0 :                         av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
    1939           0 :                         return AVERROR_INVALIDDATA;
    1940             :                     }
    1941         360 :                     v = get_bits(gb, 8);
    1942         360 :                     if (v == 0)
    1943          12 :                         break;
    1944             : 
    1945         348 :                     last = v;
    1946         348 :                     j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
    1947         348 :                     s->intra_matrix[j]        = last;
    1948         348 :                     s->chroma_intra_matrix[j] = last;
    1949             :                 }
    1950             : 
    1951             :                 /* replicate last value */
    1952         432 :                 for (; i < 64; i++) {
    1953         420 :                     int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
    1954         420 :                     s->intra_matrix[j]        = last;
    1955         420 :                     s->chroma_intra_matrix[j] = last;
    1956             :                 }
    1957             :             }
    1958             : 
    1959             :             /* load custom non intra matrix */
    1960          23 :             if (get_bits1(gb)) {
    1961          12 :                 int last = 0;
    1962         360 :                 for (i = 0; i < 64; i++) {
    1963             :                     int j;
    1964         360 :                     if (get_bits_left(gb) < 8) {
    1965           0 :                         av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
    1966           0 :                         return AVERROR_INVALIDDATA;
    1967             :                     }
    1968         360 :                     v = get_bits(gb, 8);
    1969         360 :                     if (v == 0)
    1970          12 :                         break;
    1971             : 
    1972         348 :                     last = v;
    1973         348 :                     j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
    1974         348 :                     s->inter_matrix[j]        = v;
    1975         348 :                     s->chroma_inter_matrix[j] = v;
    1976             :                 }
    1977             : 
    1978             :                 /* replicate last value */
    1979         432 :                 for (; i < 64; i++) {
    1980         420 :                     int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
    1981         420 :                     s->inter_matrix[j]        = last;
    1982         420 :                     s->chroma_inter_matrix[j] = last;
    1983             :                 }
    1984             :             }
    1985             : 
    1986             :             // FIXME a bunch of grayscale shape things
    1987             :         }
    1988             : 
    1989        1234 :         if (vo_ver_id != 1)
    1990         490 :             s->quarter_sample = get_bits1(gb);
    1991             :         else
    1992         744 :             s->quarter_sample = 0;
    1993             : 
    1994        1234 :         if (get_bits_left(gb) < 4) {
    1995           0 :             av_log(s->avctx, AV_LOG_ERROR, "VOL Header truncated\n");
    1996           0 :             return AVERROR_INVALIDDATA;
    1997             :         }
    1998             : 
    1999        1234 :         if (!get_bits1(gb)) {
    2000           0 :             int pos               = get_bits_count(gb);
    2001           0 :             int estimation_method = get_bits(gb, 2);
    2002           0 :             if (estimation_method < 2) {
    2003           0 :                 if (!get_bits1(gb)) {
    2004           0 :                     ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* opaque */
    2005           0 :                     ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* transparent */
    2006           0 :                     ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* intra_cae */
    2007           0 :                     ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* inter_cae */
    2008           0 :                     ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* no_update */
    2009           0 :                     ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* upsampling */
    2010             :                 }
    2011           0 :                 if (!get_bits1(gb)) {
    2012           0 :                     ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* intra_blocks */
    2013           0 :                     ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* inter_blocks */
    2014           0 :                     ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* inter4v_blocks */
    2015           0 :                     ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* not coded blocks */
    2016             :                 }
    2017           0 :                 if (!check_marker(s->avctx, gb, "in complexity estimation part 1")) {
    2018           0 :                     skip_bits_long(gb, pos - get_bits_count(gb));
    2019           0 :                     goto no_cplx_est;
    2020             :                 }
    2021           0 :                 if (!get_bits1(gb)) {
    2022           0 :                     ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* dct_coeffs */
    2023           0 :                     ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* dct_lines */
    2024           0 :                     ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* vlc_syms */
    2025           0 :                     ctx->cplx_estimation_trash_i += 4 * get_bits1(gb);  /* vlc_bits */
    2026             :                 }
    2027           0 :                 if (!get_bits1(gb)) {
    2028           0 :                     ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* apm */
    2029           0 :                     ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* npm */
    2030           0 :                     ctx->cplx_estimation_trash_b += 8 * get_bits1(gb);  /* interpolate_mc_q */
    2031           0 :                     ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* forwback_mc_q */
    2032           0 :                     ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* halfpel2 */
    2033           0 :                     ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* halfpel4 */
    2034             :                 }
    2035           0 :                 if (!check_marker(s->avctx, gb, "in complexity estimation part 2")) {
    2036           0 :                     skip_bits_long(gb, pos - get_bits_count(gb));
    2037           0 :                     goto no_cplx_est;
    2038             :                 }
    2039           0 :                 if (estimation_method == 1) {
    2040           0 :                     ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* sadct */
    2041           0 :                     ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* qpel */
    2042             :                 }
    2043             :             } else
    2044           0 :                 av_log(s->avctx, AV_LOG_ERROR,
    2045             :                        "Invalid Complexity estimation method %d\n",
    2046             :                        estimation_method);
    2047             :         } else {
    2048             : 
    2049        1234 : no_cplx_est:
    2050        1234 :             ctx->cplx_estimation_trash_i =
    2051        1234 :             ctx->cplx_estimation_trash_p =
    2052        1234 :             ctx->cplx_estimation_trash_b = 0;
    2053             :         }
    2054             : 
    2055        1234 :         ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
    2056             : 
    2057        1234 :         s->data_partitioning = get_bits1(gb);
    2058        1234 :         if (s->data_partitioning)
    2059         288 :             ctx->rvlc = get_bits1(gb);
    2060             : 
    2061        1234 :         if (vo_ver_id != 1) {
    2062         490 :             ctx->new_pred = get_bits1(gb);
    2063         490 :             if (ctx->new_pred) {
    2064           0 :                 av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
    2065           0 :                 skip_bits(gb, 2); /* requested upstream message type */
    2066           0 :                 skip_bits1(gb);   /* newpred segment type */
    2067             :             }
    2068         490 :             if (get_bits1(gb)) // reduced_res_vop
    2069           0 :                 av_log(s->avctx, AV_LOG_ERROR,
    2070             :                        "reduced resolution VOP not supported\n");
    2071             :         } else {
    2072         744 :             ctx->new_pred = 0;
    2073             :         }
    2074             : 
    2075        1234 :         ctx->scalability = get_bits1(gb);
    2076             : 
    2077        1234 :         if (ctx->scalability) {
    2078           0 :             GetBitContext bak = *gb;
    2079             :             int h_sampling_factor_n;
    2080             :             int h_sampling_factor_m;
    2081             :             int v_sampling_factor_n;
    2082             :             int v_sampling_factor_m;
    2083             : 
    2084           0 :             skip_bits1(gb);    // hierarchy_type
    2085           0 :             skip_bits(gb, 4);  /* ref_layer_id */
    2086           0 :             skip_bits1(gb);    /* ref_layer_sampling_dir */
    2087           0 :             h_sampling_factor_n = get_bits(gb, 5);
    2088           0 :             h_sampling_factor_m = get_bits(gb, 5);
    2089           0 :             v_sampling_factor_n = get_bits(gb, 5);
    2090           0 :             v_sampling_factor_m = get_bits(gb, 5);
    2091           0 :             ctx->enhancement_type = get_bits1(gb);
    2092             : 
    2093           0 :             if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
    2094           0 :                 v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
    2095             :                 /* illegal scalability header (VERY broken encoder),
    2096             :                  * trying to workaround */
    2097           0 :                 ctx->scalability = 0;
    2098           0 :                 *gb            = bak;
    2099             :             } else
    2100           0 :                 av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
    2101             : 
    2102             :             // bin shape stuff FIXME
    2103             :         }
    2104             :     }
    2105             : 
    2106        1234 :     if (s->avctx->debug&FF_DEBUG_PICT_INFO) {
    2107           0 :         av_log(s->avctx, AV_LOG_DEBUG, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d, low_delay:%d  %s%s%s%s\n",
    2108           0 :                s->avctx->framerate.den, s->avctx->framerate.num,
    2109             :                ctx->time_increment_bits,
    2110             :                s->quant_precision,
    2111             :                s->progressive_sequence,
    2112             :                s->low_delay,
    2113           0 :                ctx->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
    2114           0 :                s->data_partitioning ? "partition " : "", ctx->rvlc ? "rvlc " : ""
    2115             :         );
    2116             :     }
    2117             : 
    2118        1234 :     return 0;
    2119             : }
    2120             : 
    2121             : /**
    2122             :  * Decode the user data stuff in the header.
    2123             :  * Also initializes divx/xvid/lavc_version/build.
    2124             :  */
    2125          75 : static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
    2126             : {
    2127          75 :     MpegEncContext *s = &ctx->m;
    2128             :     char buf[256];
    2129             :     int i;
    2130             :     int e;
    2131          75 :     int ver = 0, build = 0, ver2 = 0, ver3 = 0;
    2132             :     char last;
    2133             : 
    2134         732 :     for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
    2135         716 :         if (show_bits(gb, 23) == 0)
    2136          59 :             break;
    2137         657 :         buf[i] = get_bits(gb, 8);
    2138             :     }
    2139          75 :     buf[i] = 0;
    2140             : 
    2141             :     /* divx detection */
    2142          75 :     e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
    2143          75 :     if (e < 2)
    2144          75 :         e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
    2145          75 :     if (e >= 2) {
    2146          11 :         ctx->divx_version = ver;
    2147          11 :         ctx->divx_build   = build;
    2148          11 :         s->divx_packed  = e == 3 && last == 'p';
    2149             :     }
    2150             : 
    2151             :     /* libavcodec detection */
    2152          75 :     e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
    2153          75 :     if (e != 4)
    2154          75 :         e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
    2155          75 :     if (e != 4) {
    2156          75 :         e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
    2157          75 :         if (e > 1) {
    2158          10 :             if (ver > 0xFFU || ver2 > 0xFFU || ver3 > 0xFFU) {
    2159           0 :                 av_log(s->avctx, AV_LOG_WARNING,
    2160             :                      "Unknown Lavc version string encountered, %d.%d.%d; "
    2161             :                      "clamping sub-version values to 8-bits.\n",
    2162             :                      ver, ver2, ver3);
    2163             :             }
    2164          10 :             build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF);
    2165             :         }
    2166             :     }
    2167          75 :     if (e != 4) {
    2168          65 :         if (strcmp(buf, "ffmpeg") == 0)
    2169           0 :             ctx->lavc_build = 4600;
    2170             :     }
    2171          75 :     if (e == 4)
    2172          10 :         ctx->lavc_build = build;
    2173             : 
    2174             :     /* Xvid detection */
    2175          75 :     e = sscanf(buf, "XviD%d", &build);
    2176          75 :     if (e == 1)
    2177          26 :         ctx->xvid_build = build;
    2178             : 
    2179          75 :     return 0;
    2180             : }
    2181             : 
    2182        3452 : int ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
    2183             : {
    2184        3452 :     Mpeg4DecContext *ctx = avctx->priv_data;
    2185        3452 :     MpegEncContext *s = &ctx->m;
    2186             : 
    2187        3452 :     if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
    2188        6604 :         if (s->codec_tag        == AV_RL32("XVID") ||
    2189        6604 :             s->codec_tag        == AV_RL32("XVIX") ||
    2190        6604 :             s->codec_tag        == AV_RL32("RMP4") ||
    2191        6604 :             s->codec_tag        == AV_RL32("ZMP4") ||
    2192        3302 :             s->codec_tag        == AV_RL32("SIPP"))
    2193           0 :             ctx->xvid_build = 0;
    2194             :     }
    2195             : 
    2196        3452 :     if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
    2197        3302 :         if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
    2198           0 :             ctx->vol_control_parameters == 0)
    2199           0 :             ctx->divx_version = 400;  // divx 4
    2200             : 
    2201        3452 :     if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
    2202           4 :         ctx->divx_version =
    2203           4 :         ctx->divx_build   = -1;
    2204             :     }
    2205             : 
    2206        3452 :     if (s->workaround_bugs & FF_BUG_AUTODETECT) {
    2207        3452 :         if (s->codec_tag == AV_RL32("XVIX"))
    2208           0 :             s->workaround_bugs |= FF_BUG_XVID_ILACE;
    2209             : 
    2210        3452 :         if (s->codec_tag == AV_RL32("UMP4"))
    2211           0 :             s->workaround_bugs |= FF_BUG_UMP4;
    2212             : 
    2213        3452 :         if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
    2214           0 :             s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
    2215             : 
    2216        3452 :         if (ctx->divx_version > 502 && ctx->divx_build < 1814)
    2217           0 :             s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
    2218             : 
    2219        3452 :         if (ctx->xvid_build <= 3U)
    2220           0 :             s->padding_bug_score = 256 * 256 * 256 * 64;
    2221             : 
    2222        3452 :         if (ctx->xvid_build <= 1U)
    2223           0 :             s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
    2224             : 
    2225        3452 :         if (ctx->xvid_build <= 12U)
    2226           0 :             s->workaround_bugs |= FF_BUG_EDGE;
    2227             : 
    2228        3452 :         if (ctx->xvid_build <= 32U)
    2229           0 :             s->workaround_bugs |= FF_BUG_DC_CLIP;
    2230             : 
    2231             : #define SET_QPEL_FUNC(postfix1, postfix2)                           \
    2232             :     s->qdsp.put_        ## postfix1 = ff_put_        ## postfix2;   \
    2233             :     s->qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;   \
    2234             :     s->qdsp.avg_        ## postfix1 = ff_avg_        ## postfix2;
    2235             : 
    2236        3452 :         if (ctx->lavc_build < 4653U)
    2237           0 :             s->workaround_bugs |= FF_BUG_STD_QPEL;
    2238             : 
    2239        3452 :         if (ctx->lavc_build < 4655U)
    2240           0 :             s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
    2241             : 
    2242        3452 :         if (ctx->lavc_build < 4670U)
    2243           0 :             s->workaround_bugs |= FF_BUG_EDGE;
    2244             : 
    2245        3452 :         if (ctx->lavc_build <= 4712U)
    2246           0 :             s->workaround_bugs |= FF_BUG_DC_CLIP;
    2247             : 
    2248        3452 :         if ((ctx->lavc_build&0xFF) >= 100) {
    2249        3452 :             if (ctx->lavc_build > 3621476 && ctx->lavc_build < 3752552 &&
    2250           0 :                (ctx->lavc_build < 3752037 || ctx->lavc_build > 3752191) // 3.2.1+
    2251             :             )
    2252           0 :                 s->workaround_bugs |= FF_BUG_IEDGE;
    2253             :         }
    2254             : 
    2255        3452 :         if (ctx->divx_version >= 0)
    2256           0 :             s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
    2257        3452 :         if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
    2258           0 :             s->padding_bug_score = 256 * 256 * 256 * 64;
    2259             : 
    2260        3452 :         if (ctx->divx_version < 500U)
    2261           0 :             s->workaround_bugs |= FF_BUG_EDGE;
    2262             : 
    2263        3452 :         if (ctx->divx_version >= 0)
    2264           0 :             s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
    2265             :     }
    2266             : 
    2267        3452 :     if (s->workaround_bugs & FF_BUG_STD_QPEL) {
    2268           0 :         SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
    2269           0 :         SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
    2270           0 :         SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
    2271           0 :         SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
    2272           0 :         SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
    2273           0 :         SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
    2274             : 
    2275           0 :         SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
    2276           0 :         SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
    2277           0 :         SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
    2278           0 :         SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
    2279           0 :         SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
    2280           0 :         SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
    2281             :     }
    2282             : 
    2283        3452 :     if (avctx->debug & FF_DEBUG_BUGS)
    2284           0 :         av_log(s->avctx, AV_LOG_DEBUG,
    2285             :                "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
    2286             :                s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
    2287           0 :                ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
    2288             : 
    2289        3516 :     if (CONFIG_MPEG4_DECODER && ctx->xvid_build >= 0 &&
    2290         128 :         s->codec_id == AV_CODEC_ID_MPEG4 &&
    2291          64 :         avctx->idct_algo == FF_IDCT_AUTO) {
    2292           4 :         avctx->idct_algo = FF_IDCT_XVID;
    2293           4 :         ff_mpv_idct_init(s);
    2294           4 :         return 1;
    2295             :     }
    2296             : 
    2297        3448 :     return 0;
    2298             : }
    2299             : 
    2300        6910 : static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
    2301             : {
    2302        6910 :     MpegEncContext *s = &ctx->m;
    2303             :     int time_incr, time_increment;
    2304             :     int64_t pts;
    2305             : 
    2306        6910 :     s->mcsel       = 0;
    2307        6910 :     s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I;        /* pict type: I = 0 , P = 1 */
    2308        6910 :     if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
    2309           0 :         ctx->vol_control_parameters == 0 && !(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)) {
    2310           0 :         av_log(s->avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n");
    2311           0 :         s->low_delay = 0;
    2312             :     }
    2313             : 
    2314        6910 :     s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
    2315        6910 :     if (s->partitioned_frame)
    2316        1028 :         s->decode_mb = mpeg4_decode_partitioned_mb;
    2317             :     else
    2318        5882 :         s->decode_mb = mpeg4_decode_mb;
    2319             : 
    2320        6910 :     time_incr = 0;
    2321       14069 :     while (get_bits1(gb) != 0)
    2322         249 :         time_incr++;
    2323             : 
    2324        6910 :     check_marker(s->avctx, gb, "before time_increment");
    2325             : 
    2326       13818 :     if (ctx->time_increment_bits == 0 ||
    2327        6908 :         !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
    2328          10 :         av_log(s->avctx, AV_LOG_WARNING,
    2329             :                "time_increment_bits %d is invalid in relation to the current bitstream, this is likely caused by a missing VOL header\n", ctx->time_increment_bits);
    2330             : 
    2331         120 :         for (ctx->time_increment_bits = 1;
    2332         110 :              ctx->time_increment_bits < 16;
    2333         100 :              ctx->time_increment_bits++) {
    2334         220 :             if (s->pict_type == AV_PICTURE_TYPE_P ||
    2335         110 :                 (s->pict_type == AV_PICTURE_TYPE_S &&
    2336           0 :                  ctx->vol_sprite_usage == GMC_SPRITE)) {
    2337           0 :                 if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
    2338           0 :                     break;
    2339         110 :             } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
    2340          10 :                 break;
    2341             :         }
    2342             : 
    2343          10 :         av_log(s->avctx, AV_LOG_WARNING,
    2344             :                "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits);
    2345          10 :         if (s->avctx->framerate.num && 4*s->avctx->framerate.num < 1<<ctx->time_increment_bits) {
    2346           6 :             s->avctx->framerate.num = 1<<ctx->time_increment_bits;
    2347           6 :             s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
    2348             :         }
    2349             :     }
    2350             : 
    2351             :     if (IS_3IV1)
    2352             :         time_increment = get_bits1(gb);        // FIXME investigate further
    2353             :     else
    2354        6910 :         time_increment = get_bits(gb, ctx->time_increment_bits);
    2355             : 
    2356        6910 :     if (s->pict_type != AV_PICTURE_TYPE_B) {
    2357        5563 :         s->last_time_base = s->time_base;
    2358        5563 :         s->time_base     += time_incr;
    2359        5563 :         s->time = s->time_base * (int64_t)s->avctx->framerate.num + time_increment;
    2360        5563 :         if (s->workaround_bugs & FF_BUG_UMP4) {
    2361           0 :             if (s->time < s->last_non_b_time) {
    2362             :                 /* header is not mpeg-4-compatible, broken encoder,
    2363             :                  * trying to workaround */
    2364           0 :                 s->time_base++;
    2365           0 :                 s->time += s->avctx->framerate.num;
    2366             :             }
    2367             :         }
    2368        5563 :         s->pp_time         = s->time - s->last_non_b_time;
    2369        5563 :         s->last_non_b_time = s->time;
    2370             :     } else {
    2371        1347 :         s->time    = (s->last_time_base + time_incr) * (int64_t)s->avctx->framerate.num + time_increment;
    2372        1347 :         s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
    2373        2694 :         if (s->pp_time <= s->pb_time ||
    2374        2694 :             s->pp_time <= s->pp_time - s->pb_time ||
    2375        1347 :             s->pp_time <= 0) {
    2376             :             /* messed up order, maybe after seeking? skipping current B-frame */
    2377           0 :             return FRAME_SKIPPED;
    2378             :         }
    2379        1347 :         ff_mpeg4_init_direct_mv(s);
    2380             : 
    2381        1347 :         if (ctx->t_frame == 0)
    2382         209 :             ctx->t_frame = s->pb_time;
    2383        1347 :         if (ctx->t_frame == 0)
    2384           0 :             ctx->t_frame = 1;  // 1/0 protection
    2385        2694 :         s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
    2386        1347 :                             ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
    2387        2694 :         s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
    2388        1347 :                             ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
    2389        1347 :         if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1) {
    2390           2 :             s->pb_field_time = 2;
    2391           2 :             s->pp_field_time = 4;
    2392           2 :             if (!s->progressive_sequence)
    2393           0 :                 return FRAME_SKIPPED;
    2394             :         }
    2395             :     }
    2396             : 
    2397        6910 :     if (s->avctx->framerate.den)
    2398        6910 :         pts = ROUNDED_DIV(s->time, s->avctx->framerate.den);
    2399             :     else
    2400           0 :         pts = AV_NOPTS_VALUE;
    2401             :     ff_dlog(s->avctx, "MPEG4 PTS: %"PRId64"\n", pts);
    2402             : 
    2403        6910 :     check_marker(s->avctx, gb, "before vop_coded");
    2404             : 
    2405             :     /* vop coded */
    2406        6910 :     if (get_bits1(gb) != 1) {
    2407           8 :         if (s->avctx->debug & FF_DEBUG_PICT_INFO)
    2408           0 :             av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
    2409           8 :         return FRAME_SKIPPED;
    2410             :     }
    2411        6902 :     if (ctx->new_pred)
    2412           0 :         decode_new_pred(ctx, gb);
    2413             : 
    2414       13804 :     if (ctx->shape != BIN_ONLY_SHAPE &&
    2415        9253 :                     (s->pict_type == AV_PICTURE_TYPE_P ||
    2416        2351 :                      (s->pict_type == AV_PICTURE_TYPE_S &&
    2417           0 :                       ctx->vol_sprite_usage == GMC_SPRITE))) {
    2418             :         /* rounding type for motion estimation */
    2419        4551 :         s->no_rounding = get_bits1(gb);
    2420             :     } else {
    2421        2351 :         s->no_rounding = 0;
    2422             :     }
    2423             :     // FIXME reduced res stuff
    2424             : 
    2425        6902 :     if (ctx->shape != RECT_SHAPE) {
    2426           0 :         if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
    2427           0 :             skip_bits(gb, 13);  /* width */
    2428           0 :             check_marker(s->avctx, gb, "after width");
    2429           0 :             skip_bits(gb, 13);  /* height */
    2430           0 :             check_marker(s->avctx, gb, "after height");
    2431           0 :             skip_bits(gb, 13);  /* hor_spat_ref */
    2432           0 :             check_marker(s->avctx, gb, "after hor_spat_ref");
    2433           0 :             skip_bits(gb, 13);  /* ver_spat_ref */
    2434             :         }
    2435           0 :         skip_bits1(gb);         /* change_CR_disable */
    2436             : 
    2437           0 :         if (get_bits1(gb) != 0)
    2438           0 :             skip_bits(gb, 8);   /* constant_alpha_value */
    2439             :     }
    2440             : 
    2441             :     // FIXME complexity estimation stuff
    2442             : 
    2443        6902 :     if (ctx->shape != BIN_ONLY_SHAPE) {
    2444        6902 :         skip_bits_long(gb, ctx->cplx_estimation_trash_i);
    2445        6902 :         if (s->pict_type != AV_PICTURE_TYPE_I)
    2446        5898 :             skip_bits_long(gb, ctx->cplx_estimation_trash_p);
    2447        6902 :         if (s->pict_type == AV_PICTURE_TYPE_B)
    2448        1347 :             skip_bits_long(gb, ctx->cplx_estimation_trash_b);
    2449             : 
    2450        6902 :         if (get_bits_left(gb) < 3) {
    2451           0 :             av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
    2452           0 :             return AVERROR_INVALIDDATA;
    2453             :         }
    2454        6902 :         ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
    2455        6902 :         if (!s->progressive_sequence) {
    2456          50 :             s->top_field_first = get_bits1(gb);
    2457          50 :             s->alternate_scan  = get_bits1(gb);
    2458             :         } else
    2459        6852 :             s->alternate_scan = 0;
    2460             :     }
    2461             : 
    2462        6902 :     if (s->alternate_scan) {
    2463           6 :         ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable,   ff_alternate_vertical_scan);
    2464           6 :         ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable,   ff_alternate_vertical_scan);
    2465           6 :         ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
    2466           6 :         ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
    2467             :     } else {
    2468        6896 :         ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable,   ff_zigzag_direct);
    2469        6896 :         ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable,   ff_zigzag_direct);
    2470        6896 :         ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
    2471        6896 :         ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
    2472             :     }
    2473             : 
    2474        6902 :     if (s->pict_type == AV_PICTURE_TYPE_S) {
    2475           0 :         if((ctx->vol_sprite_usage == STATIC_SPRITE ||
    2476           0 :             ctx->vol_sprite_usage == GMC_SPRITE)) {
    2477           0 :             if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
    2478           0 :                 return AVERROR_INVALIDDATA;
    2479           0 :             if (ctx->sprite_brightness_change)
    2480           0 :                 av_log(s->avctx, AV_LOG_ERROR,
    2481             :                     "sprite_brightness_change not supported\n");
    2482           0 :             if (ctx->vol_sprite_usage == STATIC_SPRITE)
    2483           0 :                 av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
    2484             :         } else {
    2485           0 :             memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
    2486           0 :             memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
    2487             :         }
    2488             :     }
    2489             : 
    2490        6902 :     if (ctx->shape != BIN_ONLY_SHAPE) {
    2491        6902 :         s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision);
    2492        6902 :         if (s->qscale == 0) {
    2493           0 :             av_log(s->avctx, AV_LOG_ERROR,
    2494             :                    "Error, header damaged or not MPEG-4 header (qscale=0)\n");
    2495           0 :             return AVERROR_INVALIDDATA;  // makes no sense to continue, as there is nothing left from the image then
    2496             :         }
    2497             : 
    2498        6902 :         if (s->pict_type != AV_PICTURE_TYPE_I) {
    2499        5898 :             s->f_code = get_bits(gb, 3);        /* fcode_for */
    2500        5898 :             if (s->f_code == 0) {
    2501           4 :                 av_log(s->avctx, AV_LOG_ERROR,
    2502             :                        "Error, header damaged or not MPEG-4 header (f_code=0)\n");
    2503           4 :                 s->f_code = 1;
    2504           4 :                 return AVERROR_INVALIDDATA;  // makes no sense to continue, as there is nothing left from the image then
    2505             :             }
    2506             :         } else
    2507        1004 :             s->f_code = 1;
    2508             : 
    2509        6898 :         if (s->pict_type == AV_PICTURE_TYPE_B) {
    2510        1347 :             s->b_code = get_bits(gb, 3);
    2511        1347 :             if (s->b_code == 0) {
    2512           0 :                 av_log(s->avctx, AV_LOG_ERROR,
    2513             :                        "Error, header damaged or not MPEG4 header (b_code=0)\n");
    2514           0 :                 s->b_code=1;
    2515           0 :                 return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly
    2516             :             }
    2517             :         } else
    2518        5551 :             s->b_code = 1;
    2519             : 
    2520        6898 :         if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
    2521           0 :             av_log(s->avctx, AV_LOG_DEBUG,
    2522             :                    "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",
    2523             :                    s->qscale, s->f_code, s->b_code,
    2524           0 :                    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")),
    2525             :                    gb->size_in_bits,s->progressive_sequence, s->alternate_scan,
    2526           0 :                    s->top_field_first, s->quarter_sample ? "q" : "h",
    2527             :                    s->data_partitioning, ctx->resync_marker,
    2528             :                    ctx->num_sprite_warping_points, s->sprite_warping_accuracy,
    2529           0 :                    1 - s->no_rounding, s->vo_type,
    2530           0 :                    ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
    2531             :                    ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
    2532             :                    ctx->cplx_estimation_trash_b,
    2533             :                    s->time,
    2534             :                    time_increment
    2535             :                   );
    2536             :         }
    2537             : 
    2538        6898 :         if (!ctx->scalability) {
    2539        6898 :             if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
    2540           0 :                 skip_bits1(gb);  // vop shape coding type
    2541             :         } else {
    2542           0 :             if (ctx->enhancement_type) {
    2543           0 :                 int load_backward_shape = get_bits1(gb);
    2544           0 :                 if (load_backward_shape)
    2545           0 :                     av_log(s->avctx, AV_LOG_ERROR,
    2546             :                            "load backward shape isn't supported\n");
    2547             :             }
    2548           0 :             skip_bits(gb, 2);  // ref_select_code
    2549             :         }
    2550             :     }
    2551             :     /* detect buggy encoders which don't set the low_delay flag
    2552             :      * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames
    2553             :      * easily (although it's buggy too) */
    2554        6946 :     if (s->vo_type == 0 && ctx->vol_control_parameters == 0 &&
    2555          50 :         ctx->divx_version == -1 && s->picture_number == 0) {
    2556           2 :         av_log(s->avctx, AV_LOG_WARNING,
    2557             :                "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
    2558           2 :         s->low_delay = 1;
    2559             :     }
    2560             : 
    2561        6898 :     s->picture_number++;  // better than pic number==0 always ;)
    2562             : 
    2563             :     // FIXME add short header support
    2564        6898 :     s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
    2565        6898 :     s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
    2566             : 
    2567        6898 :     if (s->workaround_bugs & FF_BUG_EDGE) {
    2568           0 :         s->h_edge_pos = s->width;
    2569           0 :         s->v_edge_pos = s->height;
    2570             :     }
    2571        6898 :     return 0;
    2572             : }
    2573             : 
    2574             : /**
    2575             :  * Decode MPEG-4 headers.
    2576             :  * @return <0 if no VOP found (or a damaged one)
    2577             :  *         FRAME_SKIPPED if a not coded VOP is found
    2578             :  *         0 if a VOP is found
    2579             :  */
    2580        7397 : int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
    2581             : {
    2582        7397 :     MpegEncContext *s = &ctx->m;
    2583             :     unsigned startcode, v;
    2584             :     int ret;
    2585        7397 :     int vol = 0;
    2586             : 
    2587             :     /* search next start code */
    2588        7397 :     align_get_bits(gb);
    2589             : 
    2590        7397 :     if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
    2591           0 :         skip_bits(gb, 24);
    2592           0 :         if (get_bits(gb, 8) == 0xF0)
    2593           0 :             goto end;
    2594             :     }
    2595             : 
    2596        7397 :     startcode = 0xff;
    2597             :     for (;;) {
    2598      100741 :         if (get_bits_count(gb) >= gb->size_in_bits) {
    2599         487 :             if (gb->size_in_bits == 8 &&
    2600           0 :                 (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
    2601           0 :                 av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
    2602           0 :                 return FRAME_SKIPPED;  // divx bug
    2603             :             } else
    2604         487 :                 return -1;  // end of stream
    2605             :         }
    2606             : 
    2607             :         /* use the bits after the test */
    2608       53582 :         v = get_bits(gb, 8);
    2609       53582 :         startcode = ((startcode << 8) | v) & 0xffffffff;
    2610             : 
    2611       53582 :         if ((startcode & 0xFFFFFF00) != 0x100)
    2612       40780 :             continue;  // no startcode
    2613             : 
    2614       12802 :         if (s->avctx->debug & FF_DEBUG_STARTCODE) {
    2615           0 :             av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
    2616           0 :             if (startcode <= 0x11F)
    2617           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
    2618           0 :             else if (startcode <= 0x12F)
    2619           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
    2620           0 :             else if (startcode <= 0x13F)
    2621           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
    2622           0 :             else if (startcode <= 0x15F)
    2623           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
    2624           0 :             else if (startcode <= 0x1AF)
    2625           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
    2626           0 :             else if (startcode == 0x1B0)
    2627           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
    2628           0 :             else if (startcode == 0x1B1)
    2629           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
    2630           0 :             else if (startcode == 0x1B2)
    2631           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "User Data");
    2632           0 :             else if (startcode == 0x1B3)
    2633           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
    2634           0 :             else if (startcode == 0x1B4)
    2635           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
    2636           0 :             else if (startcode == 0x1B5)
    2637           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
    2638           0 :             else if (startcode == 0x1B6)
    2639           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
    2640           0 :             else if (startcode == 0x1B7)
    2641           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "slice start");
    2642           0 :             else if (startcode == 0x1B8)
    2643           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "extension start");
    2644           0 :             else if (startcode == 0x1B9)
    2645           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
    2646           0 :             else if (startcode == 0x1BA)
    2647           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
    2648           0 :             else if (startcode == 0x1BB)
    2649           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
    2650           0 :             else if (startcode == 0x1BC)
    2651           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
    2652           0 :             else if (startcode == 0x1BD)
    2653           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
    2654           0 :             else if (startcode == 0x1BE)
    2655           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
    2656           0 :             else if (startcode == 0x1BF)
    2657           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
    2658           0 :             else if (startcode == 0x1C0)
    2659           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
    2660           0 :             else if (startcode == 0x1C1)
    2661           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
    2662           0 :             else if (startcode == 0x1C2)
    2663           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
    2664           0 :             else if (startcode == 0x1C3)
    2665           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
    2666           0 :             else if (startcode <= 0x1C5)
    2667           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "reserved");
    2668           0 :             else if (startcode <= 0x1FF)
    2669           0 :                 av_log(s->avctx, AV_LOG_DEBUG, "System start");
    2670           0 :             av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
    2671             :         }
    2672             : 
    2673       12802 :         if (startcode >= 0x120 && startcode <= 0x12F) {
    2674        1234 :             if (vol) {
    2675           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Multiple VOL headers");
    2676           0 :                 return AVERROR_INVALIDDATA;
    2677             :             }
    2678        1234 :             vol++;
    2679        2468 :             if ((ret = decode_vol_header(ctx, gb)) < 0)
    2680           0 :                 return ret;
    2681       11568 :         } else if (startcode == USER_DATA_STARTCODE) {
    2682          75 :             decode_user_data(ctx, gb);
    2683       11493 :         } else if (startcode == GOP_STARTCODE) {
    2684         945 :             mpeg4_decode_gop_header(s, gb);
    2685       10548 :         } else if (startcode == VOS_STARTCODE) {
    2686        1202 :             mpeg4_decode_profile_level(s, gb);
    2687        9346 :         } else if (startcode == VOP_STARTCODE) {
    2688        6910 :             break;
    2689             :         }
    2690             : 
    2691        5892 :         align_get_bits(gb);
    2692        5892 :         startcode = 0xff;
    2693             :     }
    2694             : 
    2695        6910 : end:
    2696        6910 :     if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
    2697           0 :         s->low_delay = 1;
    2698        6910 :     s->avctx->has_b_frames = !s->low_delay;
    2699             : 
    2700        6910 :     return decode_vop_header(ctx, gb);
    2701             : }
    2702             : 
    2703         493 : av_cold void ff_mpeg4videodec_static_init(void) {
    2704             :     static int done = 0;
    2705             : 
    2706         493 :     if (!done) {
    2707          93 :         ff_rl_init(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
    2708          93 :         ff_rl_init(&ff_rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
    2709          93 :         ff_rl_init(&ff_rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
    2710          93 :         INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
    2711          93 :         INIT_VLC_RL(ff_rvlc_rl_inter, 1072);
    2712          93 :         INIT_VLC_RL(ff_rvlc_rl_intra, 1072);
    2713          93 :         INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
    2714             :                         &ff_mpeg4_DCtab_lum[0][1], 2, 1,
    2715             :                         &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
    2716          93 :         INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
    2717             :                         &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
    2718             :                         &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
    2719          93 :         INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
    2720             :                         &ff_sprite_trajectory_tab[0][1], 4, 2,
    2721             :                         &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
    2722          93 :         INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
    2723             :                         &ff_mb_type_b_tab[0][1], 2, 1,
    2724             :                         &ff_mb_type_b_tab[0][0], 2, 1, 16);
    2725          93 :         done = 1;
    2726             :     }
    2727         493 : }
    2728             : 
    2729        3311 : int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
    2730             : {
    2731        3311 :     Mpeg4DecContext *ctx = avctx->priv_data;
    2732        3311 :     MpegEncContext    *s = &ctx->m;
    2733             : 
    2734             :     /* divx 5.01+ bitstream reorder stuff */
    2735             :     /* Since this clobbers the input buffer and hwaccel codecs still need the
    2736             :      * data during hwaccel->end_frame we should not do this any earlier */
    2737        3311 :     if (s->divx_packed) {
    2738          15 :         int current_pos     = s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb) >> 3);
    2739          15 :         int startcode_found = 0;
    2740             : 
    2741          15 :         if (buf_size - current_pos > 7) {
    2742             : 
    2743             :             int i;
    2744          13 :             for (i = current_pos; i < buf_size - 4; i++)
    2745             : 
    2746          21 :                 if (buf[i]     == 0 &&
    2747          16 :                     buf[i + 1] == 0 &&
    2748          16 :                     buf[i + 2] == 1 &&
    2749           8 :                     buf[i + 3] == 0xB6) {
    2750           8 :                     startcode_found = !(buf[i + 4] & 0x40);
    2751           8 :                     break;
    2752             :                 }
    2753             :         }
    2754             : 
    2755          15 :         if (startcode_found) {
    2756           8 :             if (!ctx->showed_packed_warning) {
    2757           1 :                 av_log(s->avctx, AV_LOG_INFO, "Video uses a non-standard and "
    2758             :                        "wasteful way to store B-frames ('packed B-frames'). "
    2759             :                        "Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.\n");
    2760           1 :                 ctx->showed_packed_warning = 1;
    2761             :             }
    2762           8 :             av_fast_padded_malloc(&s->bitstream_buffer,
    2763             :                            &s->allocated_bitstream_buffer_size,
    2764           8 :                            buf_size - current_pos);
    2765           8 :             if (!s->bitstream_buffer) {
    2766           0 :                 s->bitstream_buffer_size = 0;
    2767           0 :                 return AVERROR(ENOMEM);
    2768             :             }
    2769           8 :             memcpy(s->bitstream_buffer, buf + current_pos,
    2770           8 :                    buf_size - current_pos);
    2771           8 :             s->bitstream_buffer_size = buf_size - current_pos;
    2772             :         }
    2773             :     }
    2774             : 
    2775        3311 :     return 0;
    2776             : }
    2777             : 
    2778             : #if HAVE_THREADS
    2779           0 : static int mpeg4_update_thread_context(AVCodecContext *dst,
    2780             :                                        const AVCodecContext *src)
    2781             : {
    2782           0 :     Mpeg4DecContext *s = dst->priv_data;
    2783           0 :     const Mpeg4DecContext *s1 = src->priv_data;
    2784           0 :     int init = s->m.context_initialized;
    2785             : 
    2786           0 :     int ret = ff_mpeg_update_thread_context(dst, src);
    2787             : 
    2788           0 :     if (ret < 0)
    2789           0 :         return ret;
    2790             : 
    2791           0 :     memcpy(((uint8_t*)s) + sizeof(MpegEncContext), ((uint8_t*)s1) + sizeof(MpegEncContext), sizeof(Mpeg4DecContext) - sizeof(MpegEncContext));
    2792             : 
    2793           0 :     if (CONFIG_MPEG4_DECODER && !init && s1->xvid_build >= 0)
    2794           0 :         ff_xvid_idct_init(&s->m.idsp, dst);
    2795             : 
    2796           0 :     return 0;
    2797             : }
    2798             : #endif
    2799             : 
    2800         157 : static av_cold int decode_init(AVCodecContext *avctx)
    2801             : {
    2802         157 :     Mpeg4DecContext *ctx = avctx->priv_data;
    2803         157 :     MpegEncContext *s = &ctx->m;
    2804             :     int ret;
    2805             : 
    2806         157 :     ctx->divx_version =
    2807         157 :     ctx->divx_build   =
    2808         157 :     ctx->xvid_build   =
    2809         157 :     ctx->lavc_build   = -1;
    2810             : 
    2811         157 :     if ((ret = ff_h263_decode_init(avctx)) < 0)
    2812           0 :         return ret;
    2813             : 
    2814         157 :     ff_mpeg4videodec_static_init();
    2815             : 
    2816         157 :     s->h263_pred = 1;
    2817         157 :     s->low_delay = 0; /* default, might be overridden in the vol header during header parsing */
    2818         157 :     s->decode_mb = mpeg4_decode_mb;
    2819         157 :     ctx->time_increment_bits = 4; /* default value for broken headers */
    2820             : 
    2821         157 :     avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
    2822         157 :     avctx->internal->allocate_progress = 1;
    2823             : 
    2824         157 :     return 0;
    2825             : }
    2826             : 
    2827             : static const AVOption mpeg4_options[] = {
    2828             :     {"quarter_sample", "1/4 subpel MC", offsetof(MpegEncContext, quarter_sample), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
    2829             :     {"divx_packed", "divx style packed b frames", offsetof(MpegEncContext, divx_packed), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
    2830             :     {NULL}
    2831             : };
    2832             : 
    2833             : static const AVClass mpeg4_class = {
    2834             :     .class_name = "MPEG4 Video Decoder",
    2835             :     .item_name  = av_default_item_name,
    2836             :     .option     = mpeg4_options,
    2837             :     .version    = LIBAVUTIL_VERSION_INT,
    2838             : };
    2839             : 
    2840             : AVCodec ff_mpeg4_decoder = {
    2841             :     .name                  = "mpeg4",
    2842             :     .long_name             = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
    2843             :     .type                  = AVMEDIA_TYPE_VIDEO,
    2844             :     .id                    = AV_CODEC_ID_MPEG4,
    2845             :     .priv_data_size        = sizeof(Mpeg4DecContext),
    2846             :     .init                  = decode_init,
    2847             :     .close                 = ff_h263_decode_end,
    2848             :     .decode                = ff_h263_decode_frame,
    2849             :     .capabilities          = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
    2850             :                              AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
    2851             :                              AV_CODEC_CAP_FRAME_THREADS,
    2852             :     .caps_internal         = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
    2853             :     .flush                 = ff_mpeg_flush,
    2854             :     .max_lowres            = 3,
    2855             :     .pix_fmts              = ff_h263_hwaccel_pixfmt_list_420,
    2856             :     .profiles              = NULL_IF_CONFIG_SMALL(ff_mpeg4_video_profiles),
    2857             :     .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg4_update_thread_context),
    2858             :     .priv_class = &mpeg4_class,
    2859             :     .hw_configs            = (const AVCodecHWConfigInternal*[]) {
    2860             : #if CONFIG_MPEG4_NVDEC_HWACCEL
    2861             :                                HWACCEL_NVDEC(mpeg4),
    2862             : #endif
    2863             : #if CONFIG_MPEG4_VAAPI_HWACCEL
    2864             :                                HWACCEL_VAAPI(mpeg4),
    2865             : #endif
    2866             : #if CONFIG_MPEG4_VDPAU_HWACCEL
    2867             :                                HWACCEL_VDPAU(mpeg4),
    2868             : #endif
    2869             : #if CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
    2870             :                                HWACCEL_VIDEOTOOLBOX(mpeg4),
    2871             : #endif
    2872             :                                NULL
    2873             :                            },
    2874             : };

Generated by: LCOV version 1.13