LCOV - code coverage report
Current view: top level - libavcodec - mpeg4videodec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1013 2046 49.5 %
Date: 2018-05-20 11:54:08 Functions: 24 39 61.5 %

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

Generated by: LCOV version 1.13