Coverage Report

Created: 2026-01-16 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/mpeg4videodec.c
Line
Count
Source
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 "config_components.h"
26
27
#include "libavutil/avassert.h"
28
#include "libavutil/internal.h"
29
#include "libavutil/opt.h"
30
#include "libavutil/thread.h"
31
#include "codec_internal.h"
32
#include "error_resilience.h"
33
#include "hwconfig.h"
34
#include "idctdsp.h"
35
#include "mpegutils.h"
36
#include "mpegvideo.h"
37
#include "mpegvideodata.h"
38
#include "mpegvideodec.h"
39
#include "mpegvideo_unquantize.h"
40
#include "mpeg4video.h"
41
#include "mpeg4videodata.h"
42
#include "mpeg4videodec.h"
43
#include "mpeg4videodefs.h"
44
#include "h263.h"
45
#include "h263data.h"
46
#include "h263dec.h"
47
#include "internal.h"
48
#include "profiles.h"
49
#include "qpeldsp.h"
50
#include "threadprogress.h"
51
#include "unary.h"
52
53
#if 0 //3IV1 is quite rare and it slows things down a tiny bit
54
#define IS_3IV1 (s->codec_tag == AV_RL32("3IV1"))
55
#else
56
1.57M
#define IS_3IV1 0
57
#endif
58
59
/* The defines below define the number of bits that are read at once for
60
 * reading vlc values. Changing these may improve speed and data cache needs
61
 * be aware though that decreasing them may need the number of stages that is
62
 * passed to get_vlc* to be increased. */
63
165k
#define SPRITE_TRAJ_VLC_BITS 6
64
261k
#define DC_VLC_BITS 9
65
31.5k
#define MB_TYPE_B_VLC_BITS 4
66
783k
#define STUDIO_INTRA_BITS 9
67
68
static VLCElem dc_lum[512], dc_chrom[512];
69
static VLCElem sprite_trajectory[128];
70
static VLCElem mb_type_b_vlc[16];
71
static const VLCElem *studio_intra_tab[12];
72
static VLCElem studio_luma_dc[528];
73
static VLCElem studio_chroma_dc[528];
74
75
static const uint8_t mpeg4_block_count[4] = { 0, 6, 8, 12 };
76
77
static const int16_t mb_type_b_map[4] = {
78
    MB_TYPE_DIRECT2     | MB_TYPE_BIDIR_MV,
79
    MB_TYPE_BIDIR_MV    | MB_TYPE_16x16,
80
    MB_TYPE_BACKWARD_MV | MB_TYPE_16x16,
81
    MB_TYPE_FORWARD_MV  | MB_TYPE_16x16,
82
};
83
84
static inline Mpeg4DecContext *h263_to_mpeg4(H263DecContext *h)
85
2.61M
{
86
2.61M
    av_assert2(h->c.codec_id == AV_CODEC_ID_MPEG4 && h->c.avctx->priv_data == h);
87
2.61M
    return (Mpeg4DecContext*)h;
88
2.61M
}
89
90
static void gmc1_motion(MpegEncContext *s, const Mpeg4DecContext *ctx,
91
                        uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
92
                        uint8_t *const *ref_picture)
93
126k
{
94
126k
    const uint8_t *ptr;
95
126k
    int src_x, src_y, motion_x, motion_y;
96
126k
    ptrdiff_t offset, linesize, uvlinesize;
97
126k
    int emu = 0;
98
99
126k
    motion_x   = ctx->sprite_offset[0][0];
100
126k
    motion_y   = ctx->sprite_offset[0][1];
101
126k
    src_x      = s->mb_x * 16 + (motion_x >> (ctx->sprite_warping_accuracy + 1));
102
126k
    src_y      = s->mb_y * 16 + (motion_y >> (ctx->sprite_warping_accuracy + 1));
103
126k
    motion_x *= 1 << (3 - ctx->sprite_warping_accuracy);
104
126k
    motion_y *= 1 << (3 - ctx->sprite_warping_accuracy);
105
126k
    src_x      = av_clip(src_x, -16, s->width);
106
126k
    if (src_x == s->width)
107
1.64k
        motion_x = 0;
108
126k
    src_y = av_clip(src_y, -16, s->height);
109
126k
    if (src_y == s->height)
110
5.03k
        motion_y = 0;
111
112
126k
    linesize   = s->linesize;
113
126k
    uvlinesize = s->uvlinesize;
114
115
126k
    ptr = ref_picture[0] + src_y * linesize + src_x;
116
117
126k
    if ((unsigned)src_x >= FFMAX(s->h_edge_pos - 17, 0) ||
118
89.6k
        (unsigned)src_y >= FFMAX(s->v_edge_pos - 17, 0)) {
119
52.5k
        s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
120
52.5k
                                 linesize, linesize,
121
52.5k
                                 17, 17,
122
52.5k
                                 src_x, src_y,
123
52.5k
                                 s->h_edge_pos, s->v_edge_pos);
124
52.5k
        ptr = s->sc.edge_emu_buffer;
125
52.5k
    }
126
127
126k
    if ((motion_x | motion_y) & 7) {
128
0
        ctx->mdsp.gmc1(dest_y, ptr, linesize, 16,
129
0
                       motion_x & 15, motion_y & 15, 128 - s->no_rounding);
130
0
        ctx->mdsp.gmc1(dest_y + 8, ptr + 8, linesize, 16,
131
0
                       motion_x & 15, motion_y & 15, 128 - s->no_rounding);
132
126k
    } else {
133
126k
        int dxy;
134
135
126k
        dxy = ((motion_x >> 3) & 1) | ((motion_y >> 2) & 2);
136
126k
        if (s->no_rounding) {
137
28.2k
            s->hdsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
138
98.4k
        } else {
139
98.4k
            s->hdsp.put_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
140
98.4k
        }
141
126k
    }
142
143
126k
    if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY)
144
0
        return;
145
146
126k
    motion_x   = ctx->sprite_offset[1][0];
147
126k
    motion_y   = ctx->sprite_offset[1][1];
148
126k
    src_x      = s->mb_x * 8 + (motion_x >> (ctx->sprite_warping_accuracy + 1));
149
126k
    src_y      = s->mb_y * 8 + (motion_y >> (ctx->sprite_warping_accuracy + 1));
150
126k
    motion_x  *= 1 << (3 - ctx->sprite_warping_accuracy);
151
126k
    motion_y  *= 1 << (3 - ctx->sprite_warping_accuracy);
152
126k
    src_x      = av_clip(src_x, -8, s->width >> 1);
153
126k
    if (src_x == s->width >> 1)
154
2.01k
        motion_x = 0;
155
126k
    src_y = av_clip(src_y, -8, s->height >> 1);
156
126k
    if (src_y == s->height >> 1)
157
6.11k
        motion_y = 0;
158
159
126k
    offset = (src_y * uvlinesize) + src_x;
160
126k
    ptr    = ref_picture[1] + offset;
161
126k
    if ((unsigned)src_x >= FFMAX((s->h_edge_pos >> 1) - 9, 0) ||
162
89.4k
        (unsigned)src_y >= FFMAX((s->v_edge_pos >> 1) - 9, 0)) {
163
52.7k
        s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
164
52.7k
                                 uvlinesize, uvlinesize,
165
52.7k
                                 9, 9,
166
52.7k
                                 src_x, src_y,
167
52.7k
                                 s->h_edge_pos >> 1, s->v_edge_pos >> 1);
168
52.7k
        ptr = s->sc.edge_emu_buffer;
169
52.7k
        emu = 1;
170
52.7k
    }
171
126k
    ctx->mdsp.gmc1(dest_cb, ptr, uvlinesize, 8,
172
126k
                   motion_x & 15, motion_y & 15, 128 - s->no_rounding);
173
174
126k
    ptr = ref_picture[2] + offset;
175
126k
    if (emu) {
176
52.7k
        s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
177
52.7k
                                 uvlinesize, uvlinesize,
178
52.7k
                                 9, 9,
179
52.7k
                                 src_x, src_y,
180
52.7k
                                 s->h_edge_pos >> 1, s->v_edge_pos >> 1);
181
52.7k
        ptr = s->sc.edge_emu_buffer;
182
52.7k
    }
183
126k
    ctx->mdsp.gmc1(dest_cr, ptr, uvlinesize, 8,
184
126k
                   motion_x & 15, motion_y & 15, 128 - s->no_rounding);
185
126k
}
186
187
static void gmc_motion(MpegEncContext *s, const Mpeg4DecContext *ctx,
188
                       uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
189
                       uint8_t *const *ref_picture)
190
182k
{
191
182k
    const uint8_t *ptr;
192
182k
    int linesize, uvlinesize;
193
182k
    const int a = ctx->sprite_warping_accuracy;
194
182k
    int ox, oy;
195
196
182k
    linesize   = s->linesize;
197
182k
    uvlinesize = s->uvlinesize;
198
199
182k
    ptr = ref_picture[0];
200
201
182k
    ox = ctx->sprite_offset[0][0] + ctx->sprite_delta[0][0] * s->mb_x * 16 +
202
182k
         ctx->sprite_delta[0][1] * s->mb_y * 16;
203
182k
    oy = ctx->sprite_offset[0][1] + ctx->sprite_delta[1][0] * s->mb_x * 16 +
204
182k
         ctx->sprite_delta[1][1] * s->mb_y * 16;
205
206
182k
    ctx->mdsp.gmc(dest_y, ptr, linesize, 16,
207
182k
                  ox, oy,
208
182k
                  ctx->sprite_delta[0][0], ctx->sprite_delta[0][1],
209
182k
                  ctx->sprite_delta[1][0], ctx->sprite_delta[1][1],
210
182k
                  a + 1, (1 << (2 * a + 1)) - s->no_rounding,
211
182k
                  s->h_edge_pos, s->v_edge_pos);
212
182k
    ctx->mdsp.gmc(dest_y + 8, ptr, linesize, 16,
213
182k
                  ox + ctx->sprite_delta[0][0] * 8,
214
182k
                  oy + ctx->sprite_delta[1][0] * 8,
215
182k
                  ctx->sprite_delta[0][0], ctx->sprite_delta[0][1],
216
182k
                  ctx->sprite_delta[1][0], ctx->sprite_delta[1][1],
217
182k
                  a + 1, (1 << (2 * a + 1)) - s->no_rounding,
218
182k
                  s->h_edge_pos, s->v_edge_pos);
219
220
182k
    if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY)
221
0
        return;
222
223
182k
    ox = ctx->sprite_offset[1][0] + ctx->sprite_delta[0][0] * s->mb_x * 8 +
224
182k
         ctx->sprite_delta[0][1] * s->mb_y * 8;
225
182k
    oy = ctx->sprite_offset[1][1] + ctx->sprite_delta[1][0] * s->mb_x * 8 +
226
182k
         ctx->sprite_delta[1][1] * s->mb_y * 8;
227
228
182k
    ptr = ref_picture[1];
229
182k
    ctx->mdsp.gmc(dest_cb, ptr, uvlinesize, 8,
230
182k
                  ox, oy,
231
182k
                  ctx->sprite_delta[0][0], ctx->sprite_delta[0][1],
232
182k
                  ctx->sprite_delta[1][0], ctx->sprite_delta[1][1],
233
182k
                  a + 1, (1 << (2 * a + 1)) - s->no_rounding,
234
182k
                  (s->h_edge_pos + 1) >> 1, (s->v_edge_pos + 1) >> 1);
235
236
182k
    ptr = ref_picture[2];
237
182k
    ctx->mdsp.gmc(dest_cr, ptr, uvlinesize, 8,
238
182k
                  ox, oy,
239
182k
                  ctx->sprite_delta[0][0], ctx->sprite_delta[0][1],
240
182k
                  ctx->sprite_delta[1][0], ctx->sprite_delta[1][1],
241
182k
                  a + 1, (1 << (2 * a + 1)) - s->no_rounding,
242
182k
                  (s->h_edge_pos + 1) >> 1, (s->v_edge_pos + 1) >> 1);
243
182k
}
244
245
void ff_mpeg4_mcsel_motion(MpegEncContext *s,
246
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
247
                           uint8_t *const *ref_picture)
248
308k
{
249
308k
    const Mpeg4DecContext *const ctx = (Mpeg4DecContext*)s;
250
251
308k
    if (ctx->real_sprite_warping_points == 1) {
252
126k
        gmc1_motion(s, ctx, dest_y, dest_cb, dest_cr,
253
126k
                    ref_picture);
254
182k
    } else {
255
182k
        gmc_motion(s, ctx, dest_y, dest_cb, dest_cr,
256
182k
                    ref_picture);
257
182k
    }
258
308k
}
259
260
void ff_mpeg4_decode_studio(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb,
261
                            uint8_t *dest_cr, int block_size, int uvlinesize,
262
                            int dct_linesize, int dct_offset)
263
8.30k
{
264
8.30k
    Mpeg4DecContext *const ctx = (Mpeg4DecContext*)s;
265
8.30k
    const int act_block_size = block_size * 2;
266
267
8.30k
    if (ctx->dpcm_direction == 0) {
268
5.86k
        s->idsp.idct_put(dest_y,                               dct_linesize, (int16_t*)ctx->block32[0]);
269
5.86k
        s->idsp.idct_put(dest_y              + act_block_size, dct_linesize, (int16_t*)ctx->block32[1]);
270
5.86k
        s->idsp.idct_put(dest_y + dct_offset,                  dct_linesize, (int16_t*)ctx->block32[2]);
271
5.86k
        s->idsp.idct_put(dest_y + dct_offset + act_block_size, dct_linesize, (int16_t*)ctx->block32[3]);
272
273
5.86k
        dct_linesize = uvlinesize << s->interlaced_dct;
274
5.86k
        dct_offset   = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
275
276
5.86k
        s->idsp.idct_put(dest_cb,              dct_linesize, (int16_t*)ctx->block32[4]);
277
5.86k
        s->idsp.idct_put(dest_cr,              dct_linesize, (int16_t*)ctx->block32[5]);
278
5.86k
        s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, (int16_t*)ctx->block32[6]);
279
5.86k
        s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, (int16_t*)ctx->block32[7]);
280
5.86k
        if (!s->chroma_x_shift){ //Chroma444
281
4.94k
            s->idsp.idct_put(dest_cb + act_block_size,              dct_linesize, (int16_t*)ctx->block32[8]);
282
4.94k
            s->idsp.idct_put(dest_cr + act_block_size,              dct_linesize, (int16_t*)ctx->block32[9]);
283
4.94k
            s->idsp.idct_put(dest_cb + act_block_size + dct_offset, dct_linesize, (int16_t*)ctx->block32[10]);
284
4.94k
            s->idsp.idct_put(dest_cr + act_block_size + dct_offset, dct_linesize, (int16_t*)ctx->block32[11]);
285
4.94k
        }
286
5.86k
    } else if (ctx->dpcm_direction == 1) {
287
1.18k
        uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr};
288
1.18k
        int linesize[3] = {dct_linesize, uvlinesize, uvlinesize};
289
4.74k
        for (int i = 0; i < 3; i++) {
290
3.55k
            const uint16_t *src = ctx->dpcm_macroblock[i];
291
3.55k
            int vsub = i ? s->chroma_y_shift : 0;
292
3.55k
            int hsub = i ? s->chroma_x_shift : 0;
293
3.55k
            int lowres = s->avctx->lowres;
294
3.55k
            int step = 1 << lowres;
295
28.6k
            for (int h = 0; h < (16 >> (vsub + lowres)); h++){
296
247k
                for (int w = 0, idx = 0; w < (16 >> (hsub + lowres)); w++, idx += step)
297
222k
                    dest_pcm[i][w] = src[idx];
298
25.0k
                dest_pcm[i] += linesize[i] / 2;
299
25.0k
                src         += (16 >> hsub) * step;
300
25.0k
            }
301
3.55k
        }
302
1.25k
    } else {
303
1.25k
        uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr};
304
1.25k
        int linesize[3] = {dct_linesize, uvlinesize, uvlinesize};
305
1.25k
        av_assert2(ctx->dpcm_direction == -1);
306
5.02k
        for (int i = 0; i < 3; i++) {
307
3.76k
            const uint16_t *src = ctx->dpcm_macroblock[i];
308
3.76k
            int vsub = i ? s->chroma_y_shift : 0;
309
3.76k
            int hsub = i ? s->chroma_x_shift : 0;
310
3.76k
            int lowres = s->avctx->lowres;
311
3.76k
            int step = 1 << lowres;
312
3.76k
            dest_pcm[i] += (linesize[i] / 2) * ((16 >> vsub + lowres) - 1);
313
43.2k
            for (int h = (16 >> (vsub + lowres)) - 1; h >= 0; h--){
314
606k
                for (int w = (16 >> (hsub + lowres)) - 1, idx = 0; w >= 0; w--, idx += step)
315
566k
                    dest_pcm[i][w] = src[idx];
316
39.5k
                src += step * (16 >> hsub);
317
39.5k
                dest_pcm[i] -= linesize[i] / 2;
318
39.5k
            }
319
3.76k
        }
320
1.25k
    }
321
8.30k
}
322
323
/**
324
 * Predict the ac.
325
 * @param n block index (0-3 are luma, 4-5 are chroma)
326
 * @param dir the ac prediction direction
327
 */
328
void ff_mpeg4_pred_ac(H263DecContext *const h, int16_t *block, int n, int dir)
329
29.3M
{
330
29.3M
    int i;
331
29.3M
    int16_t *ac_val, *ac_val1;
332
29.3M
    int8_t *const qscale_table = h->c.cur_pic.qscale_table;
333
334
    /* find prediction */
335
29.3M
    ac_val  = &h->c.ac_val[0][0] + h->c.block_index[n] * 16;
336
29.3M
    ac_val1 = ac_val;
337
29.3M
    if (h->c.ac_pred) {
338
11.3M
        if (dir == 0) {
339
7.28M
            const int xy = h->c.mb_x - 1 + h->c.mb_y * h->c.mb_stride;
340
            /* left prediction */
341
7.28M
            ac_val -= 16;
342
343
7.28M
            if (h->c.mb_x == 0 || h->c.qscale == qscale_table[xy] ||
344
7.25M
                n == 1 || n == 3) {
345
                /* same qscale */
346
58.0M
                for (i = 1; i < 8; i++)
347
50.7M
                    block[h->c.idsp.idct_permutation[i << 3]] += ac_val[i];
348
7.25M
            } else {
349
                /* different qscale, we must rescale */
350
280k
                for (i = 1; i < 8; i++)
351
245k
                    block[h->c.idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], h->c.qscale);
352
35.0k
            }
353
7.28M
        } else {
354
4.08M
            const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride - h->c.mb_stride;
355
            /* top prediction */
356
4.08M
            ac_val -= 16 * h->c.block_wrap[n];
357
358
4.08M
            if (h->c.mb_y == 0 || h->c.qscale == qscale_table[xy] ||
359
4.07M
                n == 2 || n == 3) {
360
                /* same qscale */
361
32.6M
                for (i = 1; i < 8; i++)
362
28.5M
                    block[h->c.idsp.idct_permutation[i]] += ac_val[i + 8];
363
4.07M
            } else {
364
                /* different qscale, we must rescale */
365
96.1k
                for (i = 1; i < 8; i++)
366
84.0k
                    block[h->c.idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], h->c.qscale);
367
12.0k
            }
368
4.08M
        }
369
11.3M
    }
370
    /* left copy */
371
234M
    for (i = 1; i < 8; i++)
372
205M
        ac_val1[i] = block[h->c.idsp.idct_permutation[i << 3]];
373
374
    /* top copy */
375
234M
    for (i = 1; i < 8; i++)
376
205M
        ac_val1[8 + i] = block[h->c.idsp.idct_permutation[i]];
377
29.3M
}
378
379
/**
380
 * check if the next stuff is a resync marker or the end.
381
 * @return 0 if not
382
 */
383
static inline int mpeg4_is_resync(Mpeg4DecContext *ctx)
384
1.18M
{
385
1.18M
    H263DecContext *const h = &ctx->h;
386
1.18M
    int bits_count = get_bits_count(&h->gb);
387
1.18M
    int v          = show_bits(&h->gb, 16);
388
389
1.18M
    if (h->c.workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker)
390
61.5k
        return 0;
391
392
1.12M
    while (v <= 0xFF) {
393
30.9k
        if (h->c.pict_type == AV_PICTURE_TYPE_B ||
394
27.5k
            (v >> (8 - h->c.pict_type) != 1) || h->partitioned_frame)
395
29.7k
            break;
396
1.28k
        skip_bits(&h->gb, 8 + h->c.pict_type);
397
1.28k
        bits_count += 8 + h->c.pict_type;
398
1.28k
        v = show_bits(&h->gb, 16);
399
1.28k
    }
400
401
1.12M
    if (bits_count + 8 >= h->gb.size_in_bits) {
402
17.1k
        v >>= 8;
403
17.1k
        v  |= 0x7F >> (7 - (bits_count & 7));
404
405
17.1k
        if (v == 0x7F)
406
1.32k
            return h->c.mb_num;
407
1.10M
    } else {
408
1.10M
        static const uint16_t mpeg4_resync_prefix[8] = {
409
1.10M
            0x7F00, 0x7E00, 0x7C00, 0x7800, 0x7000, 0x6000, 0x4000, 0x0000
410
1.10M
        };
411
412
1.10M
        if (v == mpeg4_resync_prefix[bits_count & 7]) {
413
11.8k
            int len, mb_num;
414
11.8k
            int mb_num_bits = av_log2(h->c.mb_num - 1) + 1;
415
11.8k
            GetBitContext gb = h->gb;
416
417
11.8k
            skip_bits(&h->gb, 1);
418
11.8k
            align_get_bits(&h->gb);
419
420
286k
            for (len = 0; len < 32; len++)
421
283k
                if (get_bits1(&h->gb))
422
8.65k
                    break;
423
424
11.8k
            mb_num = get_bits(&h->gb, mb_num_bits);
425
11.8k
            if (!mb_num || mb_num > h->c.mb_num || get_bits_count(&h->gb) + 6 > h->gb.size_in_bits)
426
3.11k
                mb_num= -1;
427
428
11.8k
            h->gb = gb;
429
430
11.8k
            if (len >= ff_mpeg4_get_video_packet_prefix_length(h->c.pict_type, ctx->f_code, ctx->b_code))
431
10.4k
                return mb_num;
432
11.8k
        }
433
1.10M
    }
434
1.10M
    return 0;
435
1.12M
}
436
437
static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
438
43.1k
{
439
43.1k
    MpegEncContext *s = &ctx->h.c;
440
43.1k
    int a     = 2 << ctx->sprite_warping_accuracy;
441
43.1k
    int rho   = 3  - ctx->sprite_warping_accuracy;
442
43.1k
    int r     = 16 / a;
443
43.1k
    int alpha = 1;
444
43.1k
    int beta  = 0;
445
43.1k
    int w     = s->width;
446
43.1k
    int h     = s->height;
447
43.1k
    int min_ab, i, w2, h2, w3, h3;
448
43.1k
    int sprite_ref[4][2];
449
43.1k
    int virtual_ref[2][2];
450
43.1k
    int64_t sprite_offset[2][2];
451
43.1k
    int64_t sprite_delta[2][2];
452
453
    // only true for rectangle shapes
454
43.1k
    const int vop_ref[4][2] = { { 0, 0 },         { s->width, 0 },
455
43.1k
                                { 0, s->height }, { s->width, s->height } };
456
43.1k
    int d[4][2]             = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
457
458
43.1k
    if (w <= 0 || h <= 0)
459
312
        return AVERROR_INVALIDDATA;
460
461
125k
    for (i = 0; i < ctx->num_sprite_warping_points; i++) {
462
82.5k
        int length;
463
82.5k
        int x = 0, y = 0;
464
465
82.5k
        length = get_vlc2(gb, sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 2);
466
82.5k
        if (length > 0)
467
45.3k
            x = get_xbits(gb, length);
468
469
82.5k
        if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
470
82.5k
            check_marker(s->avctx, gb, "before sprite_trajectory");
471
472
82.5k
        length = get_vlc2(gb, sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 2);
473
82.5k
        if (length > 0)
474
48.0k
            y = get_xbits(gb, length);
475
476
82.5k
        check_marker(s->avctx, gb, "after sprite_trajectory");
477
82.5k
        ctx->sprite_traj[i][0] = d[i][0] = x;
478
82.5k
        ctx->sprite_traj[i][1] = d[i][1] = y;
479
82.5k
    }
480
131k
    for (; i < 4; i++)
481
88.8k
        ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0;
482
483
307k
    while ((1 << alpha) < w)
484
264k
        alpha++;
485
374k
    while ((1 << beta) < h)
486
331k
        beta++;  /* typo in the MPEG-4 std for the definition of w' and h' */
487
42.8k
    w2 = 1 << alpha;
488
42.8k
    h2 = 1 << beta;
489
490
    // Note, the 4th point isn't used for GMC
491
42.8k
    if (ctx->divx_version == 500 && ctx->divx_build == 413) {
492
0
        sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
493
0
        sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
494
0
        sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
495
0
        sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1];
496
0
        sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0];
497
0
        sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1];
498
42.8k
    } else {
499
42.8k
        sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]);
500
42.8k
        sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]);
501
42.8k
        sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]);
502
42.8k
        sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]);
503
42.8k
        sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]);
504
42.8k
        sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]);
505
42.8k
    }
506
    /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
507
     * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
508
509
    /* This is mostly identical to the MPEG-4 std (and is totally unreadable
510
     * because of that...). Perhaps it should be reordered to be more readable.
511
     * The idea behind this virtual_ref mess is to be able to use shifts later
512
     * per pixel instead of divides so the distance between points is converted
513
     * from w&h based to w2&h2 based which are of the 2^x form. */
514
42.8k
    virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) +
515
42.8k
                         ROUNDED_DIV(((w - w2) *
516
42.8k
                                           (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
517
42.8k
                                      w2 * (r * sprite_ref[1][0] - 16LL * vop_ref[1][0])), w);
518
42.8k
    virtual_ref[0][1] = 16 * vop_ref[0][1] +
519
42.8k
                        ROUNDED_DIV(((w - w2) *
520
42.8k
                                          (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
521
42.8k
                                     w2 * (r * sprite_ref[1][1] - 16LL * vop_ref[1][1])), w);
522
42.8k
    virtual_ref[1][0] = 16 * vop_ref[0][0] +
523
42.8k
                        ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
524
42.8k
                                           h2 * (r * sprite_ref[2][0] - 16LL * vop_ref[2][0])), h);
525
42.8k
    virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) +
526
42.8k
                        ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
527
42.8k
                                           h2 * (r * sprite_ref[2][1] - 16LL * vop_ref[2][1])), h);
528
529
42.8k
    switch (ctx->num_sprite_warping_points) {
530
8.95k
    case 0:
531
8.95k
        sprite_offset[0][0]    =
532
8.95k
        sprite_offset[0][1]    =
533
8.95k
        sprite_offset[1][0]    =
534
8.95k
        sprite_offset[1][1]    = 0;
535
8.95k
        sprite_delta[0][0]     = a;
536
8.95k
        sprite_delta[0][1]     =
537
8.95k
        sprite_delta[1][0]     = 0;
538
8.95k
        sprite_delta[1][1]     = a;
539
8.95k
        ctx->sprite_shift[0]   =
540
8.95k
        ctx->sprite_shift[1]   = 0;
541
8.95k
        break;
542
2.86k
    case 1:     // GMC only
543
2.86k
        sprite_offset[0][0]    = sprite_ref[0][0] - a * vop_ref[0][0];
544
2.86k
        sprite_offset[0][1]    = sprite_ref[0][1] - a * vop_ref[0][1];
545
2.86k
        sprite_offset[1][0]    = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) -
546
2.86k
                                 a * (vop_ref[0][0] / 2);
547
2.86k
        sprite_offset[1][1]    = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) -
548
2.86k
                                 a * (vop_ref[0][1] / 2);
549
2.86k
        sprite_delta[0][0]     = a;
550
2.86k
        sprite_delta[0][1]     =
551
2.86k
        sprite_delta[1][0]     = 0;
552
2.86k
        sprite_delta[1][1]     = a;
553
2.86k
        ctx->sprite_shift[0]   =
554
2.86k
        ctx->sprite_shift[1]   = 0;
555
2.86k
        break;
556
13.4k
    case 2:
557
13.4k
        sprite_offset[0][0]    = ((int64_t)      sprite_ref[0][0] * (1 << alpha + rho)) +
558
13.4k
                                 ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
559
13.4k
                                 ((int64_t)        -vop_ref[0][0]) +
560
13.4k
                                 ((int64_t)  r * sprite_ref[0][1] - virtual_ref[0][1]) *
561
13.4k
                                 ((int64_t)        -vop_ref[0][1]) + (1 << (alpha + rho - 1));
562
13.4k
        sprite_offset[0][1]    = ((int64_t)      sprite_ref[0][1] * (1 << alpha + rho)) +
563
13.4k
                                 ((int64_t) -r * sprite_ref[0][1] + virtual_ref[0][1]) *
564
13.4k
                                 ((int64_t)        -vop_ref[0][0]) +
565
13.4k
                                 ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
566
13.4k
                                 ((int64_t)        -vop_ref[0][1]) + (1 << (alpha + rho - 1));
567
13.4k
        sprite_offset[1][0]    = (((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
568
13.4k
                                  ((int64_t)-2 *    vop_ref[0][0] + 1) +
569
13.4k
                                  ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
570
13.4k
                                  ((int64_t)-2 *    vop_ref[0][1] + 1) + 2 * w2 * r *
571
13.4k
                                   (int64_t)     sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1)));
572
13.4k
        sprite_offset[1][1]    = (((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) *
573
13.4k
                                  ((int64_t)-2 *    vop_ref[0][0] + 1) +
574
13.4k
                                  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
575
13.4k
                                  ((int64_t)-2 *    vop_ref[0][1] + 1) + 2 * w2 * r *
576
13.4k
                                  (int64_t)      sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1)));
577
13.4k
        sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
578
13.4k
        sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]);
579
13.4k
        sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
580
13.4k
        sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
581
582
13.4k
        ctx->sprite_shift[0]  = alpha + rho;
583
13.4k
        ctx->sprite_shift[1]  = alpha + rho + 2;
584
13.4k
        break;
585
17.6k
    case 3:
586
17.6k
        min_ab = FFMIN(alpha, beta);
587
17.6k
        w3     = w2 >> min_ab;
588
17.6k
        h3     = h2 >> min_ab;
589
17.6k
        sprite_offset[0][0]    = ((int64_t)sprite_ref[0][0] * (1 << (alpha + beta + rho - min_ab))) +
590
17.6k
                                 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-vop_ref[0][0]) +
591
17.6k
                                 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-vop_ref[0][1]) +
592
17.6k
                                 ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
593
17.6k
        sprite_offset[0][1]    = ((int64_t)sprite_ref[0][1] * (1 << (alpha + beta + rho - min_ab))) +
594
17.6k
                                 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-vop_ref[0][0]) +
595
17.6k
                                 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-vop_ref[0][1]) +
596
17.6k
                                 ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
597
17.6k
        sprite_offset[1][0]    = ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-2 * vop_ref[0][0] + 1) +
598
17.6k
                                 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-2 * vop_ref[0][1] + 1) +
599
17.6k
                                  (int64_t)2 * w2 * h3 * r * sprite_ref[0][0] - 16 * w2 * h3 +
600
17.6k
                                 ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
601
17.6k
        sprite_offset[1][1]    = ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-2 * vop_ref[0][0] + 1) +
602
17.6k
                                 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-2 * vop_ref[0][1] + 1) +
603
17.6k
                                  (int64_t)2 * w2 * h3 * r * sprite_ref[0][1] - 16 * w2 * h3 +
604
17.6k
                                 ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
605
17.6k
        sprite_delta[0][0] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[0][0]) * h3;
606
17.6k
        sprite_delta[0][1] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[1][0]) * w3;
607
17.6k
        sprite_delta[1][0] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[0][1]) * h3;
608
17.6k
        sprite_delta[1][1] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[1][1]) * w3;
609
610
17.6k
        ctx->sprite_shift[0]  = alpha + beta + rho - min_ab;
611
17.6k
        ctx->sprite_shift[1]  = alpha + beta + rho - min_ab + 2;
612
17.6k
        break;
613
0
    default:
614
0
        av_unreachable("num_sprite_warping_points outside of 0..3 results in an error"
615
42.8k
                       "in which num_sprite_warping_points is reset to zero");
616
42.8k
    }
617
    /* try to simplify the situation */
618
42.8k
    if (sprite_delta[0][0] == a << ctx->sprite_shift[0] &&
619
26.5k
        sprite_delta[0][1] == 0 &&
620
20.7k
        sprite_delta[1][0] == 0 &&
621
18.0k
        sprite_delta[1][1] == a << ctx->sprite_shift[0]) {
622
17.2k
        sprite_offset[0][0] >>= ctx->sprite_shift[0];
623
17.2k
        sprite_offset[0][1] >>= ctx->sprite_shift[0];
624
17.2k
        sprite_offset[1][0] >>= ctx->sprite_shift[1];
625
17.2k
        sprite_offset[1][1] >>= ctx->sprite_shift[1];
626
17.2k
        sprite_delta[0][0] = a;
627
17.2k
        sprite_delta[0][1] = 0;
628
17.2k
        sprite_delta[1][0] = 0;
629
17.2k
        sprite_delta[1][1] = a;
630
17.2k
        ctx->sprite_shift[0] = 0;
631
17.2k
        ctx->sprite_shift[1] = 0;
632
17.2k
        ctx->real_sprite_warping_points = 1;
633
25.5k
    } else {
634
25.5k
        int shift_y = 16 - ctx->sprite_shift[0];
635
25.5k
        int shift_c = 16 - ctx->sprite_shift[1];
636
637
72.8k
        for (i = 0; i < 2; i++) {
638
49.8k
            if (shift_c < 0 || shift_y < 0 ||
639
49.1k
                FFABS(  sprite_offset[0][i]) >= INT_MAX >> shift_y  ||
640
48.6k
                FFABS(  sprite_offset[1][i]) >= INT_MAX >> shift_c  ||
641
48.4k
                FFABS(   sprite_delta[0][i]) >= INT_MAX >> shift_y  ||
642
47.5k
                FFABS(   sprite_delta[1][i]) >= INT_MAX >> shift_y
643
49.8k
            ) {
644
2.58k
                avpriv_request_sample(s->avctx, "Too large sprite shift, delta or offset");
645
2.58k
                goto overflow;
646
2.58k
            }
647
49.8k
        }
648
649
68.8k
        for (i = 0; i < 2; i++) {
650
45.9k
            sprite_offset[0][i]    *= 1 << shift_y;
651
45.9k
            sprite_offset[1][i]    *= 1 << shift_c;
652
45.9k
            sprite_delta[0][i]     *= 1 << shift_y;
653
45.9k
            sprite_delta[1][i]     *= 1 << shift_y;
654
45.9k
            ctx->sprite_shift[i]     = 16;
655
656
45.9k
        }
657
60.7k
        for (i = 0; i < 2; i++) {
658
42.8k
            int64_t sd[2] = {
659
42.8k
                sprite_delta[i][0] - a * (1LL<<16),
660
42.8k
                sprite_delta[i][1] - a * (1LL<<16)
661
42.8k
            };
662
663
42.8k
            if (llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
664
41.3k
                llabs(sprite_offset[0][i] + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
665
40.7k
                llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL) + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
666
40.4k
                llabs(sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
667
40.2k
                llabs(sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
668
40.0k
                llabs(sd[0]) >= INT_MAX ||
669
40.0k
                llabs(sd[1]) >= INT_MAX ||
670
40.0k
                llabs(sprite_offset[0][i] + sd[0] * (w+16LL)) >= INT_MAX ||
671
39.5k
                llabs(sprite_offset[0][i] + sd[1] * (h+16LL)) >= INT_MAX ||
672
38.3k
                llabs(sprite_offset[0][i] + sd[0] * (w+16LL) + sd[1] * (h+16LL)) >= INT_MAX
673
42.8k
            ) {
674
5.13k
                avpriv_request_sample(s->avctx, "Overflow on sprite points");
675
5.13k
                goto overflow;
676
5.13k
            }
677
42.8k
        }
678
17.8k
        ctx->real_sprite_warping_points = ctx->num_sprite_warping_points;
679
17.8k
    }
680
681
175k
    for (i = 0; i < 4; i++) {
682
140k
        ctx->sprite_offset[i&1][i>>1] = sprite_offset[i&1][i>>1];
683
140k
        ctx->sprite_delta [i&1][i>>1] = sprite_delta [i&1][i>>1];
684
140k
    }
685
686
35.1k
    return 0;
687
7.72k
overflow:
688
7.72k
    memset(ctx->sprite_offset, 0, sizeof(ctx->sprite_offset));
689
7.72k
    memset(ctx->sprite_delta,  0, sizeof(ctx->sprite_delta));
690
7.72k
    return AVERROR_PATCHWELCOME;
691
42.8k
}
692
693
78.4k
static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb) {
694
78.4k
    int len = FFMIN(ctx->time_increment_bits + 3, 15);
695
696
78.4k
    get_bits(gb, len);
697
78.4k
    if (get_bits1(gb))
698
34.0k
        get_bits(gb, len);
699
78.4k
    check_marker(ctx->h.c.avctx, gb, "after new_pred");
700
701
78.4k
    return 0;
702
78.4k
}
703
704
/**
705
 * Decode the next video packet.
706
 * @return <0 if something went wrong
707
 */
708
int ff_mpeg4_decode_video_packet_header(H263DecContext *const h)
709
687k
{
710
687k
    Mpeg4DecContext *const ctx = h263_to_mpeg4(h);
711
712
687k
    int mb_num_bits      = av_log2(h->c.mb_num - 1) + 1;
713
687k
    int header_extension = 0, mb_num, len;
714
715
    /* is there enough space left for a video packet + header */
716
687k
    if (get_bits_count(&h->gb) > h->gb.size_in_bits - 20)
717
11.1k
        return AVERROR_INVALIDDATA;
718
719
18.1M
    for (len = 0; len < 32; len++)
720
17.8M
        if (get_bits1(&h->gb))
721
359k
            break;
722
723
675k
    if (len != ff_mpeg4_get_video_packet_prefix_length(h->c.pict_type, ctx->f_code, ctx->b_code)) {
724
566k
        av_log(h->c.avctx, AV_LOG_ERROR, "marker does not match f_code\n");
725
566k
        return AVERROR_INVALIDDATA;
726
566k
    }
727
728
109k
    if (ctx->shape != RECT_SHAPE) {
729
4.93k
        header_extension = get_bits1(&h->gb);
730
        // FIXME more stuff here
731
4.93k
    }
732
733
109k
    mb_num = get_bits(&h->gb, mb_num_bits);
734
109k
    if (mb_num >= h->c.mb_num || !mb_num) {
735
19.7k
        av_log(h->c.avctx, AV_LOG_ERROR,
736
19.7k
               "illegal mb_num in video packet (%d %d) \n", mb_num, h->c.mb_num);
737
19.7k
        return AVERROR_INVALIDDATA;
738
19.7k
    }
739
740
90.0k
    h->c.mb_x = mb_num % h->c.mb_width;
741
90.0k
    h->c.mb_y = mb_num / h->c.mb_width;
742
743
90.0k
    if (ctx->shape != BIN_ONLY_SHAPE) {
744
87.7k
        int qscale = get_bits(&h->gb, ctx->quant_precision);
745
87.7k
        if (qscale)
746
73.1k
            h->c.chroma_qscale = h->c.qscale = qscale;
747
87.7k
    }
748
749
90.0k
    if (ctx->shape == RECT_SHAPE)
750
86.6k
        header_extension = get_bits1(&h->gb);
751
752
90.0k
    if (header_extension) {
753
240k
        while (get_bits1(&h->gb) != 0)
754
192k
            ;
755
756
47.9k
        check_marker(h->c.avctx, &h->gb, "before time_increment in video packed header");
757
47.9k
        skip_bits(&h->gb, ctx->time_increment_bits);      /* time_increment */
758
47.9k
        check_marker(h->c.avctx, &h->gb, "before vop_coding_type in video packed header");
759
760
47.9k
        skip_bits(&h->gb, 2); /* vop coding type */
761
        // FIXME not rect stuff here
762
763
47.9k
        if (ctx->shape != BIN_ONLY_SHAPE) {
764
46.6k
            skip_bits(&h->gb, 3); /* intra dc vlc threshold */
765
            // FIXME don't just ignore everything
766
46.6k
            if (h->c.pict_type == AV_PICTURE_TYPE_S &&
767
28.7k
                ctx->vol_sprite_usage == GMC_SPRITE) {
768
20.6k
                if (mpeg4_decode_sprite_trajectory(ctx, &h->gb) < 0)
769
5.16k
                    return AVERROR_INVALIDDATA;
770
15.4k
                av_log(h->c.avctx, AV_LOG_ERROR, "untested\n");
771
15.4k
            }
772
773
            // FIXME reduced res stuff here
774
775
41.4k
            if (h->c.pict_type != AV_PICTURE_TYPE_I) {
776
29.6k
                int f_code = get_bits(&h->gb, 3);       /* fcode_for */
777
29.6k
                if (f_code == 0)
778
9.57k
                    av_log(h->c.avctx, AV_LOG_ERROR,
779
9.57k
                           "Error, video packet header damaged (f_code=0)\n");
780
29.6k
            }
781
41.4k
            if (h->c.pict_type == AV_PICTURE_TYPE_B) {
782
651
                int b_code = get_bits(&h->gb, 3);
783
651
                if (b_code == 0)
784
296
                    av_log(h->c.avctx, AV_LOG_ERROR,
785
296
                           "Error, video packet header damaged (b_code=0)\n");
786
651
            }
787
41.4k
        }
788
47.9k
    }
789
84.8k
    if (ctx->new_pred)
790
19.8k
        decode_new_pred(ctx, &h->gb);
791
792
84.8k
    return 0;
793
90.0k
}
794
795
static void reset_studio_dc_predictors(Mpeg4DecContext *const ctx)
796
60.6k
{
797
60.6k
    H263DecContext *const h = &ctx->h;
798
    /* Reset DC Predictors */
799
60.6k
    h->last_dc[0] =
800
60.6k
    h->last_dc[1] =
801
60.6k
    h->last_dc[2] = 1 << (h->c.avctx->bits_per_raw_sample + ctx->dct_precision + h->c.intra_dc_precision - 1);
802
60.6k
}
803
804
/**
805
 * Decode the next video packet.
806
 * @return <0 if something went wrong
807
 */
808
int ff_mpeg4_decode_studio_slice_header(H263DecContext *const h)
809
21.7k
{
810
21.7k
    Mpeg4DecContext *const ctx = h263_to_mpeg4(h);
811
21.7k
    GetBitContext *gb = &h->gb;
812
21.7k
    unsigned vlc_len;
813
21.7k
    uint16_t mb_num;
814
815
21.7k
    if (get_bits_left(gb) >= 32 && get_bits_long(gb, 32) == SLICE_STARTCODE) {
816
18.8k
        vlc_len = av_log2(h->c.mb_width * h->c.mb_height) + 1;
817
18.8k
        mb_num = get_bits(gb, vlc_len);
818
819
18.8k
        if (mb_num >= h->c.mb_num)
820
501
            return AVERROR_INVALIDDATA;
821
822
18.2k
        h->c.mb_x = mb_num % h->c.mb_width;
823
18.2k
        h->c.mb_y = mb_num / h->c.mb_width;
824
825
18.2k
        if (ctx->shape != BIN_ONLY_SHAPE)
826
18.0k
            h->c.qscale = mpeg_get_qscale(&h->gb, h->c.q_scale_type);
827
828
18.2k
        if (get_bits1(gb)) {  /* slice_extension_flag */
829
7.07k
            skip_bits1(gb);   /* intra_slice */
830
7.07k
            skip_bits1(gb);   /* slice_VOP_id_enable */
831
7.07k
            skip_bits(gb, 6); /* slice_VOP_id */
832
15.3k
            while (get_bits1(gb)) /* extra_bit_slice */
833
8.31k
                skip_bits(gb, 8); /* extra_information_slice */
834
7.07k
        }
835
836
18.2k
        reset_studio_dc_predictors(ctx);
837
18.2k
    }
838
2.93k
    else {
839
2.93k
        return AVERROR_INVALIDDATA;
840
2.93k
    }
841
842
18.2k
    return 0;
843
21.7k
}
844
845
/**
846
 * Get the average motion vector for a GMC MB.
847
 * @param n either 0 for the x component or 1 for y
848
 * @return the average MV for a GMC MB
849
 */
850
static inline int get_amv(Mpeg4DecContext *ctx, int n)
851
1.67M
{
852
1.67M
    MPVContext *const s = &ctx->h.c;
853
1.67M
    int x, y, mb_v, sum, dx, dy, shift;
854
1.67M
    int len     = 1 << (ctx->f_code + 4);
855
1.67M
    const int a = ctx->sprite_warping_accuracy;
856
857
1.67M
    if (s->workaround_bugs & FF_BUG_AMV)
858
586k
        len >>= s->quarter_sample;
859
860
1.67M
    if (ctx->real_sprite_warping_points == 1) {
861
411k
        if (ctx->divx_version == 500 && ctx->divx_build == 413 && a >= s->quarter_sample)
862
0
            sum = ctx->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
863
411k
        else
864
411k
            sum = RSHIFT(ctx->sprite_offset[0][n] * (1 << s->quarter_sample), a);
865
1.26M
    } else {
866
1.26M
        dx    = ctx->sprite_delta[n][0];
867
1.26M
        dy    = ctx->sprite_delta[n][1];
868
1.26M
        shift = ctx->sprite_shift[0];
869
1.26M
        if (n)
870
630k
            dy -= 1 << (shift + a + 1);
871
630k
        else
872
630k
            dx -= 1 << (shift + a + 1);
873
1.26M
        mb_v = ctx->sprite_offset[0][n] + dx * s->mb_x * 16U + dy * s->mb_y * 16U;
874
875
1.26M
        sum = 0;
876
21.4M
        for (y = 0; y < 16; y++) {
877
20.1M
            int v;
878
879
20.1M
            v = mb_v + (unsigned)dy * y;
880
            // FIXME optimize
881
342M
            for (x = 0; x < 16; x++) {
882
322M
                sum += v >> shift;
883
322M
                v   += dx;
884
322M
            }
885
20.1M
        }
886
1.26M
        sum = RSHIFT(sum, a + 8 - s->quarter_sample);
887
1.26M
    }
888
889
1.67M
    if (sum < -len)
890
239k
        sum = -len;
891
1.43M
    else if (sum >= len)
892
424k
        sum = len - 1;
893
894
1.67M
    return sum;
895
1.67M
}
896
897
/**
898
 * Predict the dc.
899
 * @param n block index (0-3 are luma, 4-5 are chroma)
900
 * @param dir_ptr pointer to an integer where the prediction direction will be stored
901
 */
902
static inline int mpeg4_pred_dc(MpegEncContext *s, int n, int *dir_ptr)
903
645k
{
904
645k
    const int16_t *const dc_val = s->dc_val + s->block_index[n];
905
645k
    const int wrap = s->block_wrap[n];
906
645k
    int pred;
907
908
    /* find prediction */
909
910
    /* B C
911
     * A X
912
     */
913
645k
    int a = dc_val[-1];
914
645k
    int b = dc_val[-1 - wrap];
915
645k
    int c = dc_val[-wrap];
916
917
    /* outside slice handling (we can't do that by memset as we need the
918
     * dc for error resilience) */
919
645k
    if (s->first_slice_line && n != 3) {
920
369k
        if (n != 2)
921
296k
            b = c = 1024;
922
369k
        if (n != 1 && s->mb_x == s->resync_mb_x)
923
92.2k
            b = a = 1024;
924
369k
    }
925
645k
    if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1) {
926
29.5k
        if (n == 0 || n == 4 || n == 5)
927
14.6k
            b = 1024;
928
29.5k
    }
929
930
645k
    if (abs(a - b) < abs(b - c)) {
931
145k
        pred     = c;
932
145k
        *dir_ptr = 1; /* top */
933
499k
    } else {
934
499k
        pred     = a;
935
499k
        *dir_ptr = 0; /* left */
936
499k
    }
937
645k
    return pred;
938
645k
}
939
940
static inline int mpeg4_get_level_dc(MpegEncContext *s, int n, int pred, int level)
941
621k
{
942
621k
    int scale = n < 4 ? s->y_dc_scale : s->c_dc_scale;
943
621k
    int ret;
944
945
621k
    if (IS_3IV1)
946
0
        scale = 8;
947
948
    /* we assume pred is positive */
949
621k
    pred = FASTDIV((pred + (scale >> 1)), scale);
950
951
621k
    level += pred;
952
621k
    ret    = level;
953
621k
    level *= scale;
954
621k
    if (level & (~2047)) {
955
97.8k
        if (s->avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) {
956
5.03k
            if (level < 0) {
957
1.47k
                av_log(s->avctx, AV_LOG_ERROR,
958
1.47k
                       "dc<0 at %dx%d\n", s->mb_x, s->mb_y);
959
1.47k
                return AVERROR_INVALIDDATA;
960
1.47k
            }
961
3.55k
            if (level > 2048 + scale) {
962
2.39k
                av_log(s->avctx, AV_LOG_ERROR,
963
2.39k
                       "dc overflow at %dx%d\n", s->mb_x, s->mb_y);
964
2.39k
                return AVERROR_INVALIDDATA;
965
2.39k
            }
966
3.55k
        }
967
93.9k
        if (level < 0)
968
3.57k
            level = 0;
969
90.4k
        else if (!(s->workaround_bugs & FF_BUG_DC_CLIP))
970
27.6k
            level = 2047;
971
93.9k
    }
972
617k
    s->dc_val[s->block_index[n]] = level;
973
974
617k
    return ret;
975
621k
}
976
977
/**
978
 * Decode the dc value.
979
 * @param n block index (0-3 are luma, 4-5 are chroma)
980
 * @param dir_ptr the prediction direction will be stored here
981
 * @return the quantized dc
982
 */
983
static inline int mpeg4_decode_dc(H263DecContext *const h, int n, int *dir_ptr)
984
261k
{
985
261k
    int level, code, pred;
986
987
261k
    if (n < 4)
988
187k
        code = get_vlc2(&h->gb, dc_lum, DC_VLC_BITS, 1);
989
73.4k
    else
990
73.4k
        code = get_vlc2(&h->gb, dc_chrom, DC_VLC_BITS, 1);
991
992
261k
    if (code < 0) {
993
9.73k
        av_log(h->c.avctx, AV_LOG_ERROR, "illegal dc vlc\n");
994
9.73k
        return AVERROR_INVALIDDATA;
995
9.73k
    }
996
997
251k
    if (code == 0) {
998
51.4k
        level = 0;
999
200k
    } else {
1000
200k
        if (IS_3IV1) {
1001
0
            if (code == 1)
1002
0
                level = 2 * get_bits1(&h->gb) - 1;
1003
0
            else {
1004
0
                if (get_bits1(&h->gb))
1005
0
                    level = get_bits(&h->gb, code - 1) + (1 << (code - 1));
1006
0
                else
1007
0
                    level = -get_bits(&h->gb, code - 1) - (1 << (code - 1));
1008
0
            }
1009
200k
        } else {
1010
200k
            level = get_xbits(&h->gb, code);
1011
200k
        }
1012
1013
200k
        if (code > 8) {
1014
2.61k
            if (get_bits1(&h->gb) == 0) { /* marker */
1015
1.51k
                if (h->c.avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)) {
1016
244
                    av_log(h->c.avctx, AV_LOG_ERROR, "dc marker bit missing\n");
1017
244
                    return AVERROR_INVALIDDATA;
1018
244
                }
1019
1.51k
            }
1020
2.61k
        }
1021
200k
    }
1022
1023
251k
    pred = mpeg4_pred_dc(&h->c, n, dir_ptr);
1024
251k
    return mpeg4_get_level_dc(&h->c, n, pred, level);
1025
251k
}
1026
1027
/**
1028
 * Decode first partition.
1029
 * @return number of MBs decoded or <0 if an error occurred
1030
 */
1031
static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
1032
54.2k
{
1033
54.2k
    H263DecContext *const h = &ctx->h;
1034
54.2k
    int mb_num = 0;
1035
54.2k
    static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1036
1037
    /* decode first partition */
1038
54.2k
    h->c.first_slice_line = 1;
1039
249k
    for (; h->c.mb_y < h->c.mb_height; h->c.mb_y++) {
1040
243k
        ff_init_block_index(&h->c);
1041
1.21M
        for (; h->c.mb_x < h->c.mb_width; h->c.mb_x++) {
1042
1.01M
            const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride;
1043
1.01M
            int cbpc;
1044
1.01M
            int dir = 0;
1045
1046
1.01M
            mb_num++;
1047
1.01M
            ff_update_block_index(&h->c, 8, h->c.avctx->lowres, 1);
1048
1.01M
            if (h->c.mb_x == h->c.resync_mb_x && h->c.mb_y == h->c.resync_mb_y + 1)
1049
19.9k
                h->c.first_slice_line = 0;
1050
1051
1.01M
            if (h->c.pict_type == AV_PICTURE_TYPE_I) {
1052
38.2k
                int i;
1053
1054
39.2k
                do {
1055
39.2k
                    if (show_bits(&h->gb, 19) == DC_MARKER)
1056
949
                        return mb_num - 1;
1057
1058
38.2k
                    cbpc = get_vlc2(&h->gb, ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 2);
1059
38.2k
                    if (cbpc < 0) {
1060
4.42k
                        av_log(h->c.avctx, AV_LOG_ERROR,
1061
4.42k
                               "mcbpc corrupted at %d %d\n", h->c.mb_x, h->c.mb_y);
1062
4.42k
                        return AVERROR_INVALIDDATA;
1063
4.42k
                    }
1064
38.2k
                } while (cbpc == 8);
1065
1066
32.8k
                h->c.cbp_table[xy]       = cbpc & 3;
1067
32.8k
                h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA;
1068
32.8k
                h->c.mb_intra            = 1;
1069
1070
32.8k
                if (cbpc & 4)
1071
5.37k
                    ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]);
1072
1073
32.8k
                h->c.cur_pic.qscale_table[xy] = h->c.qscale;
1074
1075
32.8k
                h->c.mbintra_table[xy] = 1;
1076
192k
                for (i = 0; i < 6; i++) {
1077
169k
                    int dc_pred_dir;
1078
169k
                    int dc = mpeg4_decode_dc(h, i, &dc_pred_dir);
1079
169k
                    if (dc < 0) {
1080
9.15k
                        av_log(h->c.avctx, AV_LOG_ERROR,
1081
9.15k
                               "DC corrupted at %d %d\n", h->c.mb_x, h->c.mb_y);
1082
9.15k
                        return dc;
1083
9.15k
                    }
1084
160k
                    dir <<= 1;
1085
160k
                    if (dc_pred_dir)
1086
36.1k
                        dir |= 1;
1087
160k
                }
1088
23.6k
                h->c.pred_dir_table[xy] = dir;
1089
977k
            } else { /* P/S_TYPE */
1090
977k
                int mx, my, pred_x, pred_y, bits;
1091
977k
                int16_t *const mot_val = h->c.cur_pic.motion_val[0][h->c.block_index[0]];
1092
977k
                const int stride       = h->c.b8_stride * 2;
1093
1094
978k
try_again:
1095
978k
                bits = show_bits(&h->gb, 17);
1096
978k
                if (bits == MOTION_MARKER)
1097
7.82k
                    return mb_num - 1;
1098
1099
971k
                skip_bits1(&h->gb);
1100
971k
                if (bits & 0x10000) {
1101
                    /* skip mb */
1102
701k
                    if (h->c.pict_type == AV_PICTURE_TYPE_S &&
1103
528k
                        ctx->vol_sprite_usage == GMC_SPRITE) {
1104
345k
                        h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP  |
1105
345k
                                                         MB_TYPE_16x16 |
1106
345k
                                                         MB_TYPE_GMC   |
1107
345k
                                                         MB_TYPE_FORWARD_MV;
1108
345k
                        mx = get_amv(ctx, 0);
1109
345k
                        my = get_amv(ctx, 1);
1110
355k
                    } else {
1111
355k
                        h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP  |
1112
355k
                                                         MB_TYPE_16x16 |
1113
355k
                                                         MB_TYPE_FORWARD_MV;
1114
355k
                        mx = my = 0;
1115
355k
                    }
1116
701k
                    mot_val[0]          =
1117
701k
                    mot_val[2]          =
1118
701k
                    mot_val[0 + stride] =
1119
701k
                    mot_val[2 + stride] = mx;
1120
701k
                    mot_val[1]          =
1121
701k
                    mot_val[3]          =
1122
701k
                    mot_val[1 + stride] =
1123
701k
                    mot_val[3 + stride] = my;
1124
1125
701k
                    ff_h263_clean_intra_table_entries(&h->c, xy);
1126
701k
                    continue;
1127
701k
                }
1128
1129
269k
                cbpc = get_vlc2(&h->gb, ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 2);
1130
269k
                if (cbpc < 0) {
1131
12.9k
                    av_log(h->c.avctx, AV_LOG_ERROR,
1132
12.9k
                           "mcbpc corrupted at %d %d\n", h->c.mb_x, h->c.mb_y);
1133
12.9k
                    return AVERROR_INVALIDDATA;
1134
12.9k
                }
1135
256k
                if (cbpc == 20)
1136
1.46k
                    goto try_again;
1137
1138
255k
                h->c.cbp_table[xy] = cbpc & (8 + 3);  // 8 is dquant
1139
1140
255k
                h->c.mb_intra = ((cbpc & 4) != 0);
1141
1142
255k
                if (h->c.mb_intra) {
1143
33.3k
                    h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA;
1144
33.3k
                    h->c.mbintra_table[xy] = 1;
1145
33.3k
                    mot_val[0]          =
1146
33.3k
                    mot_val[2]          =
1147
33.3k
                    mot_val[0 + stride] =
1148
33.3k
                    mot_val[2 + stride] = 0;
1149
33.3k
                    mot_val[1]          =
1150
33.3k
                    mot_val[3]          =
1151
33.3k
                    mot_val[1 + stride] =
1152
33.3k
                    mot_val[3 + stride] = 0;
1153
222k
                } else {
1154
222k
                    ff_h263_clean_intra_table_entries(&h->c, xy);
1155
1156
222k
                    if (h->c.pict_type == AV_PICTURE_TYPE_S &&
1157
163k
                        ctx->vol_sprite_usage == GMC_SPRITE &&
1158
107k
                        (cbpc & 16) == 0)
1159
85.3k
                        h->c.mcsel = get_bits1(&h->gb);
1160
136k
                    else
1161
136k
                        h->c.mcsel = 0;
1162
1163
222k
                    if ((cbpc & 16) == 0) {
1164
                        /* 16x16 motion prediction */
1165
1166
175k
                        ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y);
1167
175k
                        if (!h->c.mcsel) {
1168
130k
                            mx = ff_h263_decode_motion(h, pred_x, ctx->f_code);
1169
130k
                            if (mx >= 0xffff)
1170
1.84k
                                return AVERROR_INVALIDDATA;
1171
1172
128k
                            my = ff_h263_decode_motion(h, pred_y, ctx->f_code);
1173
128k
                            if (my >= 0xffff)
1174
1.88k
                                return AVERROR_INVALIDDATA;
1175
126k
                            h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 |
1176
126k
                                                             MB_TYPE_FORWARD_MV;
1177
126k
                        } else {
1178
44.4k
                            mx = get_amv(ctx, 0);
1179
44.4k
                            my = get_amv(ctx, 1);
1180
44.4k
                            h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 |
1181
44.4k
                                                             MB_TYPE_GMC   |
1182
44.4k
                                                             MB_TYPE_FORWARD_MV;
1183
44.4k
                        }
1184
1185
171k
                        mot_val[0]          =
1186
171k
                        mot_val[2]          =
1187
171k
                        mot_val[0 + stride] =
1188
171k
                        mot_val[2 + stride] = mx;
1189
171k
                        mot_val[1]          =
1190
171k
                        mot_val[3]          =
1191
171k
                        mot_val[1 + stride] =
1192
171k
                        mot_val[3 + stride] = my;
1193
171k
                    } else {
1194
46.9k
                        int i;
1195
46.9k
                        h->c.cur_pic.mb_type[xy] = MB_TYPE_8x8 |
1196
46.9k
                                                         MB_TYPE_FORWARD_MV;
1197
208k
                        for (i = 0; i < 4; i++) {
1198
170k
                            int16_t *mot_val = ff_h263_pred_motion(&h->c, i, 0, &pred_x, &pred_y);
1199
170k
                            mx = ff_h263_decode_motion(h, pred_x, ctx->f_code);
1200
170k
                            if (mx >= 0xffff)
1201
4.46k
                                return AVERROR_INVALIDDATA;
1202
1203
166k
                            my = ff_h263_decode_motion(h, pred_y, ctx->f_code);
1204
166k
                            if (my >= 0xffff)
1205
5.03k
                                return AVERROR_INVALIDDATA;
1206
161k
                            mot_val[0] = mx;
1207
161k
                            mot_val[1] = my;
1208
161k
                        }
1209
46.9k
                    }
1210
222k
                }
1211
255k
            }
1212
1.01M
        }
1213
194k
        h->c.mb_x = 0;
1214
194k
    }
1215
1216
5.73k
    return mb_num;
1217
54.2k
}
1218
1219
/**
1220
 * decode second partition.
1221
 * @return <0 if an error occurred
1222
 */
1223
static int mpeg4_decode_partition_b(H263DecContext *const h, int mb_count)
1224
8.45k
{
1225
8.45k
    int mb_num = 0;
1226
8.45k
    static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1227
1228
8.45k
    h->c.mb_x = h->c.resync_mb_x;
1229
8.45k
    h->c.first_slice_line = 1;
1230
46.4k
    for (h->c.mb_y = h->c.resync_mb_y; mb_num < mb_count; h->c.mb_y++) {
1231
46.4k
        ff_init_block_index(&h->c);
1232
180k
        for (; mb_num < mb_count && h->c.mb_x < h->c.mb_width; h->c.mb_x++) {
1233
135k
            const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride;
1234
1235
135k
            mb_num++;
1236
135k
            ff_update_block_index(&h->c, 8, h->c.avctx->lowres, 1);
1237
135k
            if (h->c.mb_x == h->c.resync_mb_x && h->c.mb_y == h->c.resync_mb_y + 1)
1238
2.93k
                h->c.first_slice_line = 0;
1239
1240
135k
            if (h->c.pict_type == AV_PICTURE_TYPE_I) {
1241
2.67k
                int ac_pred = get_bits1(&h->gb);
1242
2.67k
                int cbpy    = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
1243
2.67k
                if (cbpy < 0) {
1244
237
                    av_log(h->c.avctx, AV_LOG_ERROR,
1245
237
                           "cbpy corrupted at %d %d\n", h->c.mb_x, h->c.mb_y);
1246
237
                    return AVERROR_INVALIDDATA;
1247
237
                }
1248
1249
2.43k
                h->c.cbp_table[xy]       |= cbpy << 2;
1250
2.43k
                h->c.cur_pic.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
1251
133k
            } else { /* P || S_TYPE */
1252
133k
                if (IS_INTRA(h->c.cur_pic.mb_type[xy])) {
1253
3.62k
                    int i;
1254
3.62k
                    int dir     = 0;
1255
3.62k
                    int ac_pred = get_bits1(&h->gb);
1256
3.62k
                    int cbpy    = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
1257
1258
3.62k
                    if (cbpy < 0) {
1259
403
                        av_log(h->c.avctx, AV_LOG_ERROR,
1260
403
                               "I cbpy corrupted at %d %d\n", h->c.mb_x, h->c.mb_y);
1261
403
                        return AVERROR_INVALIDDATA;
1262
403
                    }
1263
1264
3.22k
                    if (h->c.cbp_table[xy] & 8)
1265
788
                        ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]);
1266
3.22k
                    h->c.cur_pic.qscale_table[xy] = h->c.qscale;
1267
1268
19.4k
                    for (i = 0; i < 6; i++) {
1269
16.9k
                        int dc_pred_dir;
1270
16.9k
                        int dc = mpeg4_decode_dc(h, i, &dc_pred_dir);
1271
16.9k
                        if (dc < 0) {
1272
729
                            av_log(h->c.avctx, AV_LOG_ERROR,
1273
729
                                   "DC corrupted at %d %d\n", h->c.mb_x, h->c.mb_y);
1274
729
                            return dc;
1275
729
                        }
1276
16.2k
                        dir <<= 1;
1277
16.2k
                        if (dc_pred_dir)
1278
4.05k
                            dir |= 1;
1279
16.2k
                    }
1280
2.49k
                    h->c.cbp_table[xy]       &= 3;  // remove dquant
1281
2.49k
                    h->c.cbp_table[xy]       |= cbpy << 2;
1282
2.49k
                    h->c.cur_pic.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
1283
2.49k
                    h->c.pred_dir_table[xy]   = dir;
1284
129k
                } else if (IS_SKIP(h->c.cur_pic.mb_type[xy])) {
1285
107k
                    h->c.cur_pic.qscale_table[xy] = h->c.qscale;
1286
107k
                    h->c.cbp_table[xy]            = 0;
1287
107k
                } else {
1288
22.1k
                    int cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
1289
1290
22.1k
                    if (cbpy < 0) {
1291
664
                        av_log(h->c.avctx, AV_LOG_ERROR,
1292
664
                               "P cbpy corrupted at %d %d\n", h->c.mb_x, h->c.mb_y);
1293
664
                        return AVERROR_INVALIDDATA;
1294
664
                    }
1295
1296
21.4k
                    if (h->c.cbp_table[xy] & 8)
1297
5.23k
                        ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]);
1298
21.4k
                    h->c.cur_pic.qscale_table[xy] = h->c.qscale;
1299
1300
21.4k
                    h->c.cbp_table[xy] &= 3;  // remove dquant
1301
21.4k
                    h->c.cbp_table[xy] |= (cbpy ^ 0xf) << 2;
1302
21.4k
                }
1303
133k
            }
1304
135k
        }
1305
44.3k
        if (mb_num >= mb_count)
1306
6.41k
            return 0;
1307
37.9k
        h->c.mb_x = 0;
1308
37.9k
    }
1309
0
    return 0;
1310
8.45k
}
1311
1312
/**
1313
 * Decode the first and second partition.
1314
 * @return <0 if error (and sets error type in the error_status_table)
1315
 */
1316
int ff_mpeg4_decode_partitions(H263DecContext *const h)
1317
54.2k
{
1318
54.2k
    Mpeg4DecContext *const ctx = h263_to_mpeg4(h);
1319
54.2k
    int mb_num;
1320
54.2k
    int ret;
1321
54.2k
    const int part_a_error = h->c.pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR;
1322
54.2k
    const int part_a_end   = h->c.pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END   | ER_MV_END)   : ER_MV_END;
1323
1324
54.2k
    mb_num = mpeg4_decode_partition_a(ctx);
1325
54.2k
    if (mb_num <= 0) {
1326
40.1k
        ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
1327
40.1k
                        h->c.mb_x, h->c.mb_y, part_a_error);
1328
40.1k
        return mb_num ? mb_num : AVERROR_INVALIDDATA;
1329
40.1k
    }
1330
1331
14.1k
    if (h->c.resync_mb_x + h->c.resync_mb_y * h->c.mb_width + mb_num > h->c.mb_num) {
1332
0
        av_log(h->c.avctx, AV_LOG_ERROR, "slice below monitor ...\n");
1333
0
        ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
1334
0
                        h->c.mb_x, h->c.mb_y, part_a_error);
1335
0
        return AVERROR_INVALIDDATA;
1336
0
    }
1337
1338
14.1k
    h->mb_num_left = mb_num;
1339
1340
14.1k
    if (h->c.pict_type == AV_PICTURE_TYPE_I) {
1341
2.15k
        while (show_bits(&h->gb, 9) == 1)
1342
588
            skip_bits(&h->gb, 9);
1343
1.56k
        if (get_bits(&h->gb, 19) != DC_MARKER) {
1344
854
            av_log(h->c.avctx, AV_LOG_ERROR,
1345
854
                   "marker missing after first I partition at %d %d\n",
1346
854
                   h->c.mb_x, h->c.mb_y);
1347
854
            return AVERROR_INVALIDDATA;
1348
854
        }
1349
12.5k
    } else {
1350
13.0k
        while (show_bits(&h->gb, 10) == 1)
1351
485
            skip_bits(&h->gb, 10);
1352
12.5k
        if (get_bits(&h->gb, 17) != MOTION_MARKER) {
1353
4.79k
            av_log(h->c.avctx, AV_LOG_ERROR,
1354
4.79k
                   "marker missing after first P partition at %d %d\n",
1355
4.79k
                   h->c.mb_x, h->c.mb_y);
1356
4.79k
            return AVERROR_INVALIDDATA;
1357
4.79k
        }
1358
12.5k
    }
1359
8.45k
    ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
1360
8.45k
                    h->c.mb_x - 1, h->c.mb_y, part_a_end);
1361
1362
8.45k
    ret = mpeg4_decode_partition_b(h, mb_num);
1363
8.45k
    if (ret < 0) {
1364
2.03k
        if (h->c.pict_type == AV_PICTURE_TYPE_P)
1365
871
            ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
1366
871
                            h->c.mb_x, h->c.mb_y, ER_DC_ERROR);
1367
2.03k
        return ret;
1368
6.41k
    } else {
1369
6.41k
        if (h->c.pict_type == AV_PICTURE_TYPE_P)
1370
1.85k
            ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
1371
1.85k
                            h->c.mb_x - 1, h->c.mb_y, ER_DC_END);
1372
6.41k
    }
1373
1374
6.41k
    return 0;
1375
8.45k
}
1376
1377
/**
1378
 * Decode a block.
1379
 * @return <0 if an error occurred
1380
 */
1381
static inline int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block,
1382
                                     int n, int coded, int intra,
1383
                                     int use_intra_dc_vlc, int rvlc)
1384
2.23M
{
1385
2.23M
    H263DecContext *const h = &ctx->h;
1386
2.23M
    int level, i, last, run, qmul, qadd, pred;
1387
2.23M
    int av_uninit(dc_pred_dir);
1388
2.23M
    const RLTable *rl;
1389
2.23M
    const RL_VLC_ELEM *rl_vlc;
1390
2.23M
    const uint8_t *scan_table;
1391
1392
    // Note intra & rvlc should be optimized away if this is inlined
1393
1394
2.23M
    if (intra) {
1395
        // FIXME add short header support
1396
473k
        if (use_intra_dc_vlc) {
1397
            /* DC coef */
1398
79.5k
            if (h->partitioned_frame) {
1399
4.30k
                level = h->c.dc_val[h->c.block_index[n]];
1400
4.30k
                if (n < 4)
1401
3.14k
                    level = FASTDIV((level + (h->c.y_dc_scale >> 1)), h->c.y_dc_scale);
1402
1.16k
                else
1403
1.16k
                    level = FASTDIV((level + (h->c.c_dc_scale >> 1)), h->c.c_dc_scale);
1404
4.30k
                dc_pred_dir = (h->c.pred_dir_table[h->c.mb_x + h->c.mb_y * h->c.mb_stride] << n) & 32;
1405
75.2k
            } else {
1406
75.2k
                level = mpeg4_decode_dc(h, n, &dc_pred_dir);
1407
75.2k
                if (level < 0)
1408
5.09k
                    return level;
1409
75.2k
            }
1410
74.4k
            block[0] = level;
1411
74.4k
            i        = 0;
1412
394k
        } else {
1413
394k
            i = -1;
1414
394k
            pred = mpeg4_pred_dc(&h->c, n, &dc_pred_dir);
1415
394k
        }
1416
468k
        if (!coded)
1417
260k
            goto not_coded;
1418
1419
208k
        if (rvlc) {
1420
680
            rl     = &ff_rvlc_rl_intra;
1421
680
            rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
1422
207k
        } else {
1423
207k
            rl     = &ff_mpeg4_rl_intra;
1424
207k
            rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
1425
207k
        }
1426
208k
        if (h->c.ac_pred) {
1427
149k
            if (dc_pred_dir == 0)
1428
105k
                scan_table = h->permutated_intra_v_scantable;  /* left */
1429
43.3k
            else
1430
43.3k
                scan_table = h->permutated_intra_h_scantable;  /* top */
1431
149k
        } else {
1432
59.4k
            scan_table = h->c.intra_scantable.permutated;
1433
59.4k
        }
1434
208k
        qmul = 1;
1435
208k
        qadd = 0;
1436
1.76M
    } else {
1437
1.76M
        i = -1;
1438
1.76M
        if (!coded) {
1439
1.51M
            h->c.block_last_index[n] = i;
1440
1.51M
            return 0;
1441
1.51M
        }
1442
254k
        if (rvlc)
1443
5.12k
            rl = &ff_rvlc_rl_inter;
1444
249k
        else
1445
249k
            rl = &ff_h263_rl_inter;
1446
1447
254k
        scan_table = h->c.intra_scantable.permutated;
1448
1449
254k
        if (ctx->mpeg_quant) {
1450
135k
            qmul = 1;
1451
135k
            qadd = 0;
1452
135k
            if (rvlc)
1453
956
                rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
1454
134k
            else
1455
134k
                rl_vlc = ff_h263_rl_inter.rl_vlc[0];
1456
135k
        } else {
1457
118k
            qmul = h->c.qscale << 1;
1458
118k
            qadd = (h->c.qscale - 1) | 1;
1459
118k
            if (rvlc)
1460
4.16k
                rl_vlc = ff_rvlc_rl_inter.rl_vlc[h->c.qscale];
1461
114k
            else
1462
114k
                rl_vlc = ff_h263_rl_inter.rl_vlc[h->c.qscale];
1463
118k
        }
1464
254k
    }
1465
462k
    {
1466
462k
        OPEN_READER(re, &h->gb);
1467
2.18M
        for (;;) {
1468
2.18M
            UPDATE_CACHE(re, &h->gb);
1469
2.18M
            GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1470
2.18M
            if (level == 0) {
1471
                /* escape */
1472
14.7k
                if (rvlc) {
1473
2.13k
                    if (SHOW_UBITS(re, &h->gb, 1) == 0) {
1474
794
                        av_log(h->c.avctx, AV_LOG_ERROR,
1475
794
                               "1. marker bit missing in rvlc esc\n");
1476
794
                        return AVERROR_INVALIDDATA;
1477
794
                    }
1478
1.34k
                    SKIP_CACHE(re, &h->gb, 1);
1479
1480
1.34k
                    last = SHOW_UBITS(re, &h->gb, 1);
1481
1.34k
                    SKIP_CACHE(re, &h->gb, 1);
1482
1.34k
                    run = SHOW_UBITS(re, &h->gb, 6);
1483
1.34k
                    SKIP_COUNTER(re, &h->gb, 1 + 1 + 6);
1484
1.34k
                    UPDATE_CACHE(re, &h->gb);
1485
1486
1.34k
                    if (SHOW_UBITS(re, &h->gb, 1) == 0) {
1487
368
                        av_log(h->c.avctx, AV_LOG_ERROR,
1488
368
                               "2. marker bit missing in rvlc esc\n");
1489
368
                        return AVERROR_INVALIDDATA;
1490
368
                    }
1491
973
                    SKIP_CACHE(re, &h->gb, 1);
1492
1493
973
                    level = SHOW_UBITS(re, &h->gb, 11);
1494
973
                    SKIP_CACHE(re, &h->gb, 11);
1495
1496
973
                    if (SHOW_UBITS(re, &h->gb, 5) != 0x10) {
1497
483
                        av_log(h->c.avctx, AV_LOG_ERROR, "reverse esc missing\n");
1498
483
                        return AVERROR_INVALIDDATA;
1499
483
                    }
1500
490
                    SKIP_CACHE(re, &h->gb, 5);
1501
1502
490
                    level = level * qmul + qadd;
1503
490
                    level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1);
1504
490
                    SKIP_COUNTER(re, &h->gb, 1 + 11 + 5 + 1);
1505
1506
490
                    i += run + 1;
1507
490
                    if (last)
1508
280
                        i += 192;
1509
12.5k
                } else {
1510
12.5k
                    int cache;
1511
12.5k
                    cache = GET_CACHE(re, &h->gb);
1512
1513
12.5k
                    if (IS_3IV1)
1514
0
                        cache ^= 0xC0000000;
1515
1516
12.5k
                    if (cache & 0x80000000) {
1517
10.9k
                        if (cache & 0x40000000) {
1518
                            /* third escape */
1519
9.49k
                            SKIP_CACHE(re, &h->gb, 2);
1520
9.49k
                            last = SHOW_UBITS(re, &h->gb, 1);
1521
9.49k
                            SKIP_CACHE(re, &h->gb, 1);
1522
9.49k
                            run = SHOW_UBITS(re, &h->gb, 6);
1523
9.49k
                            SKIP_COUNTER(re, &h->gb, 2 + 1 + 6);
1524
9.49k
                            UPDATE_CACHE(re, &h->gb);
1525
1526
9.49k
                            if (IS_3IV1) {
1527
0
                                level = SHOW_SBITS(re, &h->gb, 12);
1528
0
                                LAST_SKIP_BITS(re, &h->gb, 12);
1529
9.49k
                            } else {
1530
9.49k
                                if (SHOW_UBITS(re, &h->gb, 1) == 0) {
1531
849
                                    av_log(h->c.avctx, AV_LOG_ERROR,
1532
849
                                           "1. marker bit missing in 3. esc\n");
1533
849
                                    if (!(h->c.avctx->err_recognition & AV_EF_IGNORE_ERR) || get_bits_left(&h->gb) <= 0)
1534
849
                                        return AVERROR_INVALIDDATA;
1535
849
                                }
1536
8.64k
                                SKIP_CACHE(re, &h->gb, 1);
1537
1538
8.64k
                                level = SHOW_SBITS(re, &h->gb, 12);
1539
8.64k
                                SKIP_CACHE(re, &h->gb, 12);
1540
1541
8.64k
                                if (SHOW_UBITS(re, &h->gb, 1) == 0) {
1542
493
                                    av_log(h->c.avctx, AV_LOG_ERROR,
1543
493
                                           "2. marker bit missing in 3. esc\n");
1544
493
                                    if (!(h->c.avctx->err_recognition & AV_EF_IGNORE_ERR) || get_bits_left(&h->gb) <= 0)
1545
493
                                        return AVERROR_INVALIDDATA;
1546
493
                                }
1547
1548
8.15k
                                SKIP_COUNTER(re, &h->gb, 1 + 12 + 1);
1549
8.15k
                            }
1550
1551
#if 0
1552
                            if (h->c.error_recognition >= FF_ER_COMPLIANT) {
1553
                                const int abs_level= FFABS(level);
1554
                                if (abs_level<=MAX_LEVEL && run<=MAX_RUN) {
1555
                                    const int run1= run - rl->max_run[last][abs_level] - 1;
1556
                                    if (abs_level <= rl->max_level[last][run]) {
1557
                                        av_log(h->c.avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1558
                                        return AVERROR_INVALIDDATA;
1559
                                    }
1560
                                    if (h->c.error_recognition > FF_ER_COMPLIANT) {
1561
                                        if (abs_level <= rl->max_level[last][run]*2) {
1562
                                            av_log(h->c.avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1563
                                            return AVERROR_INVALIDDATA;
1564
                                        }
1565
                                        if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) {
1566
                                            av_log(h->c.avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1567
                                            return AVERROR_INVALIDDATA;
1568
                                        }
1569
                                    }
1570
                                }
1571
                            }
1572
#endif
1573
8.15k
                            if (level > 0)
1574
6.26k
                                level = level * qmul + qadd;
1575
1.89k
                            else
1576
1.89k
                                level = level * qmul - qadd;
1577
1578
8.15k
                            if ((unsigned)(level + 2048) > 4095) {
1579
4.18k
                                if (h->c.avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)) {
1580
635
                                    if (level > 2560 || level < -2560) {
1581
433
                                        av_log(h->c.avctx, AV_LOG_ERROR,
1582
433
                                               "|level| overflow in 3. esc, qp=%d\n",
1583
433
                                               h->c.qscale);
1584
433
                                        return AVERROR_INVALIDDATA;
1585
433
                                    }
1586
635
                                }
1587
3.75k
                                level = level < 0 ? -2048 : 2047;
1588
3.75k
                            }
1589
1590
7.71k
                            i += run + 1;
1591
7.71k
                            if (last)
1592
5.12k
                                i += 192;
1593
7.71k
                        } else {
1594
                            /* second escape */
1595
1.41k
                            SKIP_BITS(re, &h->gb, 2);
1596
1.41k
                            GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1597
1.41k
                            i    += run + rl->max_run[run >> 7][level / qmul] + 1;  // FIXME opt indexing
1598
1.41k
                            level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1);
1599
1.41k
                            LAST_SKIP_BITS(re, &h->gb, 1);
1600
1.41k
                        }
1601
10.9k
                    } else {
1602
                        /* first escape */
1603
1.67k
                        SKIP_BITS(re, &h->gb, 1);
1604
1.67k
                        GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1605
1.67k
                        i    += run;
1606
1.67k
                        level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul;  // FIXME opt indexing
1607
1.67k
                        level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1);
1608
1.67k
                        LAST_SKIP_BITS(re, &h->gb, 1);
1609
1.67k
                    }
1610
12.5k
                }
1611
2.17M
            } else {
1612
2.17M
                i    += run;
1613
2.17M
                level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1);
1614
2.17M
                LAST_SKIP_BITS(re, &h->gb, 1);
1615
2.17M
            }
1616
2.18M
            ff_tlog(h->c.avctx, "dct[%d][%d] = %- 4d end?:%d\n", scan_table[i&63]&7, scan_table[i&63] >> 3, level, i>62);
1617
2.18M
            if (i > 62) {
1618
459k
                i -= 192;
1619
459k
                if (i & (~63)) {
1620
115k
                    av_log(h->c.avctx, AV_LOG_ERROR,
1621
115k
                           "ac-tex damaged at %d %d\n", h->c.mb_x, h->c.mb_y);
1622
115k
                    return AVERROR_INVALIDDATA;
1623
115k
                }
1624
1625
343k
                block[scan_table[i]] = level;
1626
343k
                break;
1627
459k
            }
1628
1629
1.72M
            block[scan_table[i]] = level;
1630
1.72M
        }
1631
343k
        CLOSE_READER(re, &h->gb);
1632
343k
    }
1633
1634
603k
not_coded:
1635
603k
    if (intra) {
1636
437k
        if (!use_intra_dc_vlc) {
1637
369k
            block[0] = mpeg4_get_level_dc(&h->c, n, pred, block[0]);
1638
1639
369k
            i -= i >> 31;  // if (i == -1) i = 0;
1640
369k
        }
1641
1642
437k
        ff_mpeg4_pred_ac(h, block, n, dc_pred_dir);
1643
437k
        if (h->c.ac_pred)
1644
262k
            i = 63;  // FIXME not optimal
1645
437k
    }
1646
603k
    h->c.block_last_index[n] = i;
1647
603k
    return 0;
1648
343k
}
1649
1650
/**
1651
 * decode partition C of one MB.
1652
 * @return <0 if an error occurred
1653
 */
1654
static int mpeg4_decode_partitioned_mb(H263DecContext *const h)
1655
52.1k
{
1656
52.1k
    Mpeg4DecContext *const ctx = h263_to_mpeg4(h);
1657
52.1k
    const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride;
1658
1659
52.1k
    const int mb_type = h->c.cur_pic.mb_type[xy];
1660
52.1k
    int           cbp = h->c.cbp_table[xy];
1661
1662
52.1k
    const int use_intra_dc_vlc = h->c.qscale < ctx->intra_dc_threshold;
1663
1664
52.1k
    if (h->c.cur_pic.qscale_table[xy] != h->c.qscale)
1665
1.84k
        ff_set_qscale(&h->c, h->c.cur_pic.qscale_table[xy]);
1666
1667
52.1k
    if (h->c.pict_type == AV_PICTURE_TYPE_P ||
1668
51.3k
        h->c.pict_type == AV_PICTURE_TYPE_S) {
1669
51.3k
        int i;
1670
256k
        for (i = 0; i < 4; i++) {
1671
205k
            h->c.mv[0][i][0] = h->c.cur_pic.motion_val[0][h->c.block_index[i]][0];
1672
205k
            h->c.mv[0][i][1] = h->c.cur_pic.motion_val[0][h->c.block_index[i]][1];
1673
205k
        }
1674
51.3k
        h->c.mb_intra = IS_INTRA(mb_type);
1675
1676
51.3k
        if (IS_SKIP(mb_type)) {
1677
            /* skip mb */
1678
295k
            for (i = 0; i < 6; i++)
1679
252k
                h->c.block_last_index[i] = -1;
1680
42.1k
            h->c.mv_dir  = MV_DIR_FORWARD;
1681
42.1k
            h->c.mv_type = MV_TYPE_16X16;
1682
42.1k
            if (h->c.pict_type == AV_PICTURE_TYPE_S
1683
26.5k
                && ctx->vol_sprite_usage == GMC_SPRITE) {
1684
5.27k
                h->c.mcsel      = 1;
1685
5.27k
                h->c.mb_skipped = 0;
1686
5.27k
                h->c.cur_pic.mbskip_table[xy] = 0;
1687
36.8k
            } else {
1688
36.8k
                h->c.mcsel      = 0;
1689
36.8k
                h->c.mb_skipped = 1;
1690
36.8k
                h->c.cur_pic.mbskip_table[xy] = 1;
1691
36.8k
            }
1692
42.1k
        } else if (h->c.mb_intra) {
1693
739
            h->c.ac_pred = IS_ACPRED(h->c.cur_pic.mb_type[xy]);
1694
8.44k
        } else if (!h->c.mb_intra) {
1695
            // h->c.mcsel = 0;  // FIXME do we need to init that?
1696
1697
8.44k
            h->c.mv_dir = MV_DIR_FORWARD;
1698
8.44k
            if (IS_8X8(mb_type)) {
1699
1.37k
                h->c.mv_type = MV_TYPE_8X8;
1700
7.07k
            } else {
1701
7.07k
                h->c.mv_type = MV_TYPE_16X16;
1702
7.07k
            }
1703
8.44k
        }
1704
51.3k
    } else { /* I-Frame */
1705
762
        h->c.mb_intra = 1;
1706
762
        h->c.ac_pred  = IS_ACPRED(h->c.cur_pic.mb_type[xy]);
1707
762
    }
1708
1709
52.1k
    if (!IS_SKIP(mb_type)) {
1710
9.95k
        int i;
1711
9.95k
        h->c.bdsp.clear_blocks(h->block[0]);
1712
        /* decode each block */
1713
49.0k
        for (i = 0; i < 6; i++) {
1714
43.8k
            if (mpeg4_decode_block(ctx, h->block[i], i, cbp & 32, h->c.mb_intra,
1715
43.8k
                                   use_intra_dc_vlc, ctx->rvlc) < 0) {
1716
4.82k
                av_log(h->c.avctx, AV_LOG_ERROR,
1717
4.82k
                       "texture corrupted at %d %d %d\n",
1718
4.82k
                       h->c.mb_x, h->c.mb_y, h->c.mb_intra);
1719
4.82k
                return AVERROR_INVALIDDATA;
1720
4.82k
            }
1721
39.0k
            cbp += cbp;
1722
39.0k
        }
1723
9.95k
    }
1724
1725
    /* per-MB end of slice check */
1726
47.2k
    if (--h->mb_num_left <= 0) {
1727
1.11k
        if (mpeg4_is_resync(ctx))
1728
240
            return SLICE_END;
1729
873
        else
1730
873
            return SLICE_NOEND;
1731
46.1k
    } else {
1732
46.1k
        if (mpeg4_is_resync(ctx)) {
1733
5.35k
            const int delta = h->c.mb_x + 1 == h->c.mb_width ? 2 : 1;
1734
5.35k
            if (h->c.cbp_table[xy + delta])
1735
478
                return SLICE_END;
1736
5.35k
        }
1737
45.6k
        return SLICE_OK;
1738
46.1k
    }
1739
47.2k
}
1740
1741
static int mpeg4_decode_mb(H263DecContext *const h)
1742
1.29M
{
1743
1.29M
    Mpeg4DecContext *const ctx = h263_to_mpeg4(h);
1744
1.29M
    int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1745
1.29M
    static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1746
1.29M
    const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride;
1747
1.29M
    int next;
1748
1749
1.29M
    av_assert2(h->c.h263_pred);
1750
1751
1.29M
    if (h->c.pict_type == AV_PICTURE_TYPE_P ||
1752
1.17M
        h->c.pict_type == AV_PICTURE_TYPE_S) {
1753
1.07M
        do {
1754
1.07M
            if (get_bits1(&h->gb)) {
1755
                /* skip mb */
1756
808k
                h->c.mb_intra = 0;
1757
5.66M
                for (i = 0; i < 6; i++)
1758
4.85M
                    h->c.block_last_index[i] = -1;
1759
808k
                h->c.mv_dir  = MV_DIR_FORWARD;
1760
808k
                h->c.mv_type = MV_TYPE_16X16;
1761
808k
                if (h->c.pict_type == AV_PICTURE_TYPE_S &&
1762
726k
                    ctx->vol_sprite_usage == GMC_SPRITE) {
1763
410k
                    h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP  |
1764
410k
                                                     MB_TYPE_GMC   |
1765
410k
                                                     MB_TYPE_16x16 |
1766
410k
                                             MB_TYPE_FORWARD_MV;
1767
410k
                    h->c.mcsel       = 1;
1768
410k
                    h->c.mv[0][0][0] = get_amv(ctx, 0);
1769
410k
                    h->c.mv[0][0][1] = get_amv(ctx, 1);
1770
410k
                    h->c.cur_pic.mbskip_table[xy] = 0;
1771
410k
                    h->c.mb_skipped  = 0;
1772
410k
                } else {
1773
397k
                    h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 |
1774
397k
                                             MB_TYPE_FORWARD_MV;
1775
397k
                    h->c.mcsel       = 0;
1776
397k
                    h->c.mv[0][0][0] = 0;
1777
397k
                    h->c.mv[0][0][1] = 0;
1778
397k
                    h->c.cur_pic.mbskip_table[xy] = 1;
1779
397k
                    h->c.mb_skipped  = 1;
1780
397k
                }
1781
808k
                goto end;
1782
808k
            }
1783
267k
            cbpc = get_vlc2(&h->gb, ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 2);
1784
267k
            if (cbpc < 0) {
1785
12.0k
                av_log(h->c.avctx, AV_LOG_ERROR,
1786
12.0k
                       "mcbpc damaged at %d %d\n", h->c.mb_x, h->c.mb_y);
1787
12.0k
                return AVERROR_INVALIDDATA;
1788
12.0k
            }
1789
267k
        } while (cbpc == 20);
1790
1791
254k
        dquant      = cbpc & 8;
1792
254k
        h->c.mb_intra = ((cbpc & 4) != 0);
1793
254k
        if (h->c.mb_intra)
1794
23.4k
            goto intra;
1795
230k
        h->c.bdsp.clear_blocks(h->block[0]);
1796
1797
230k
        if (h->c.pict_type == AV_PICTURE_TYPE_S &&
1798
197k
            ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
1799
54.5k
            h->c.mcsel = get_bits1(&h->gb);
1800
176k
        else
1801
176k
            h->c.mcsel = 0;
1802
230k
        cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1) ^ 0x0F;
1803
230k
        if (cbpy < 0) {
1804
7.09k
            av_log(h->c.avctx, AV_LOG_ERROR,
1805
7.09k
                   "P cbpy damaged at %d %d\n", h->c.mb_x, h->c.mb_y);
1806
7.09k
            return AVERROR_INVALIDDATA;
1807
7.09k
        }
1808
1809
223k
        cbp = (cbpc & 3) | (cbpy << 2);
1810
223k
        if (dquant)
1811
18.8k
            ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]);
1812
223k
        if ((!h->c.progressive_sequence) &&
1813
150k
            (cbp || (h->c.workaround_bugs & FF_BUG_XVID_ILACE)))
1814
132k
            h->c.interlaced_dct = get_bits1(&h->gb);
1815
1816
223k
        h->c.mv_dir = MV_DIR_FORWARD;
1817
223k
        if ((cbpc & 16) == 0) {
1818
190k
            if (h->c.mcsel) {
1819
35.6k
                h->c.cur_pic.mb_type[xy] = MB_TYPE_GMC | MB_TYPE_16x16 |
1820
35.6k
                                         MB_TYPE_FORWARD_MV;
1821
                /* 16x16 global motion prediction */
1822
35.6k
                h->c.mv_type   = MV_TYPE_16X16;
1823
35.6k
                mx             = get_amv(ctx, 0);
1824
35.6k
                my             = get_amv(ctx, 1);
1825
35.6k
                h->c.mv[0][0][0] = mx;
1826
35.6k
                h->c.mv[0][0][1] = my;
1827
155k
            } else if ((!h->c.progressive_sequence) && get_bits1(&h->gb)) {
1828
33.3k
                h->c.cur_pic.mb_type[xy] = MB_TYPE_16x8 | MB_TYPE_FORWARD_MV |
1829
33.3k
                                                 MB_TYPE_INTERLACED;
1830
                /* 16x8 field motion prediction */
1831
33.3k
                h->c.mv_type = MV_TYPE_FIELD;
1832
1833
33.3k
                h->c.field_select[0][0] = get_bits1(&h->gb);
1834
33.3k
                h->c.field_select[0][1] = get_bits1(&h->gb);
1835
1836
33.3k
                ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y);
1837
1838
95.2k
                for (i = 0; i < 2; i++) {
1839
64.6k
                    mx = ff_h263_decode_motion(h, pred_x, ctx->f_code);
1840
64.6k
                    if (mx >= 0xffff)
1841
1.33k
                        return AVERROR_INVALIDDATA;
1842
1843
63.2k
                    my = ff_h263_decode_motion(h, pred_y / 2, ctx->f_code);
1844
63.2k
                    if (my >= 0xffff)
1845
1.30k
                        return AVERROR_INVALIDDATA;
1846
1847
61.9k
                    h->c.mv[0][i][0] = mx;
1848
61.9k
                    h->c.mv[0][i][1] = my;
1849
61.9k
                }
1850
121k
            } else {
1851
121k
                h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
1852
                /* 16x16 motion prediction */
1853
121k
                h->c.mv_type             = MV_TYPE_16X16;
1854
121k
                ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y);
1855
121k
                mx = ff_h263_decode_motion(h, pred_x, ctx->f_code);
1856
1857
121k
                if (mx >= 0xffff)
1858
1.37k
                    return AVERROR_INVALIDDATA;
1859
1860
120k
                my = ff_h263_decode_motion(h, pred_y, ctx->f_code);
1861
1862
120k
                if (my >= 0xffff)
1863
1.13k
                    return AVERROR_INVALIDDATA;
1864
119k
                h->c.mv[0][0][0] = mx;
1865
119k
                h->c.mv[0][0][1] = my;
1866
119k
            }
1867
190k
        } else {
1868
32.9k
            h->c.cur_pic.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_FORWARD_MV;
1869
32.9k
            h->c.mv_type             = MV_TYPE_8X8;
1870
153k
            for (i = 0; i < 4; i++) {
1871
125k
                int16_t *mot_val = ff_h263_pred_motion(&h->c, i, 0, &pred_x, &pred_y);
1872
125k
                mx      = ff_h263_decode_motion(h, pred_x, ctx->f_code);
1873
125k
                if (mx >= 0xffff)
1874
3.37k
                    return AVERROR_INVALIDDATA;
1875
1876
121k
                my = ff_h263_decode_motion(h, pred_y, ctx->f_code);
1877
121k
                if (my >= 0xffff)
1878
1.46k
                    return AVERROR_INVALIDDATA;
1879
120k
                h->c.mv[0][i][0] = mx;
1880
120k
                h->c.mv[0][i][1] = my;
1881
120k
                mot_val[0]     = mx;
1882
120k
                mot_val[1]     = my;
1883
120k
            }
1884
32.9k
        }
1885
223k
    } else if (h->c.pict_type == AV_PICTURE_TYPE_B) {
1886
138k
        int modb1;   // first bit of modb
1887
138k
        int modb2;   // second bit of modb
1888
138k
        int mb_type;
1889
1890
138k
        h->c.mb_intra = 0;  // B-frames never contain intra blocks
1891
138k
        h->c.mcsel    = 0;  //      ...               true gmc blocks
1892
1893
138k
        if (h->c.mb_x == 0) {
1894
143k
            for (i = 0; i < 2; i++) {
1895
95.9k
                h->c.last_mv[i][0][0] =
1896
95.9k
                h->c.last_mv[i][0][1] =
1897
95.9k
                h->c.last_mv[i][1][0] =
1898
95.9k
                h->c.last_mv[i][1][1] = 0;
1899
95.9k
            }
1900
1901
47.9k
            ff_thread_progress_await(&h->c.next_pic.ptr->progress, h->c.mb_y);
1902
47.9k
        }
1903
1904
        /* if we skipped it in the future P-frame than skip it now too */
1905
138k
        h->c.mb_skipped = h->c.next_pic.mbskip_table[h->c.mb_y * h->c.mb_stride + h->c.mb_x];  // Note, skiptab=0 if last was GMC
1906
1907
138k
        if (h->c.mb_skipped) {
1908
            /* skip mb */
1909
63.4k
            for (i = 0; i < 6; i++)
1910
54.3k
                h->c.block_last_index[i] = -1;
1911
1912
9.05k
            h->c.mv_dir      = MV_DIR_FORWARD;
1913
9.05k
            h->c.mv_type     = MV_TYPE_16X16;
1914
9.05k
            h->c.mv[0][0][0] =
1915
9.05k
            h->c.mv[0][0][1] =
1916
9.05k
            h->c.mv[1][0][0] =
1917
9.05k
            h->c.mv[1][0][1] = 0;
1918
9.05k
            h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP  |
1919
9.05k
                                             MB_TYPE_16x16 |
1920
9.05k
                                             MB_TYPE_FORWARD_MV;
1921
9.05k
            goto end;
1922
9.05k
        }
1923
1924
129k
        modb1 = get_bits1(&h->gb);
1925
129k
        if (modb1) {
1926
            // like MB_TYPE_B_DIRECT but no vectors coded
1927
97.6k
            mb_type = MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_BIDIR_MV;
1928
97.6k
            cbp     = 0;
1929
97.6k
        } else {
1930
31.5k
            modb2   = get_bits1(&h->gb);
1931
31.5k
            mb_type = get_vlc2(&h->gb, mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 1);
1932
31.5k
            if (mb_type < 0) {
1933
1.26k
                av_log(h->c.avctx, AV_LOG_ERROR, "illegal MB_type\n");
1934
1.26k
                return AVERROR_INVALIDDATA;
1935
1.26k
            }
1936
30.2k
            if (modb2) {
1937
16.9k
                cbp = 0;
1938
16.9k
            } else {
1939
13.3k
                h->c.bdsp.clear_blocks(h->block[0]);
1940
13.3k
                cbp = get_bits(&h->gb, 6);
1941
13.3k
            }
1942
1943
30.2k
            if ((!IS_DIRECT(mb_type)) && cbp) {
1944
5.53k
                if (get_bits1(&h->gb))
1945
887
                    ff_set_qscale(&h->c, h->c.qscale + get_bits1(&h->gb) * 4 - 2);
1946
5.53k
            }
1947
1948
30.2k
            if (!h->c.progressive_sequence) {
1949
6.31k
                if (cbp)
1950
2.66k
                    h->c.interlaced_dct = get_bits1(&h->gb);
1951
1952
6.31k
                if (!IS_DIRECT(mb_type) && get_bits1(&h->gb)) {
1953
1.83k
                    mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1954
1.83k
                    mb_type &= ~MB_TYPE_16x16;
1955
1956
1.83k
                    if (HAS_FORWARD_MV(mb_type)) {
1957
1.41k
                        h->c.field_select[0][0] = get_bits1(&h->gb);
1958
1.41k
                        h->c.field_select[0][1] = get_bits1(&h->gb);
1959
1.41k
                    }
1960
1.83k
                    if (HAS_BACKWARD_MV(mb_type)) {
1961
1.57k
                        h->c.field_select[1][0] = get_bits1(&h->gb);
1962
1.57k
                        h->c.field_select[1][1] = get_bits1(&h->gb);
1963
1.57k
                    }
1964
1.83k
                }
1965
6.31k
            }
1966
1967
30.2k
            h->c.mv_dir = 0;
1968
30.2k
            if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
1969
9.81k
                h->c.mv_type = MV_TYPE_16X16;
1970
1971
9.81k
                if (HAS_FORWARD_MV(mb_type)) {
1972
7.51k
                    h->c.mv_dir = MV_DIR_FORWARD;
1973
1974
7.51k
                    mx = ff_h263_decode_motion(h, h->c.last_mv[0][0][0], ctx->f_code);
1975
7.51k
                    my = ff_h263_decode_motion(h, h->c.last_mv[0][0][1], ctx->f_code);
1976
7.51k
                    h->c.last_mv[0][1][0] =
1977
7.51k
                    h->c.last_mv[0][0][0] =
1978
7.51k
                    h->c.mv[0][0][0]      = mx;
1979
7.51k
                    h->c.last_mv[0][1][1] =
1980
7.51k
                    h->c.last_mv[0][0][1] =
1981
7.51k
                    h->c.mv[0][0][1]      = my;
1982
7.51k
                }
1983
1984
9.81k
                if (HAS_BACKWARD_MV(mb_type)) {
1985
8.59k
                    h->c.mv_dir |= MV_DIR_BACKWARD;
1986
1987
8.59k
                    mx = ff_h263_decode_motion(h, h->c.last_mv[1][0][0], ctx->b_code);
1988
8.59k
                    my = ff_h263_decode_motion(h, h->c.last_mv[1][0][1], ctx->b_code);
1989
8.59k
                    h->c.last_mv[1][1][0] =
1990
8.59k
                    h->c.last_mv[1][0][0] =
1991
8.59k
                    h->c.mv[1][0][0]      = mx;
1992
8.59k
                    h->c.last_mv[1][1][1] =
1993
8.59k
                    h->c.last_mv[1][0][1] =
1994
8.59k
                    h->c.mv[1][0][1]      = my;
1995
8.59k
                }
1996
20.4k
            } else if (!IS_DIRECT(mb_type)) {
1997
1.83k
                h->c.mv_type = MV_TYPE_FIELD;
1998
1999
1.83k
                if (HAS_FORWARD_MV(mb_type)) {
2000
1.41k
                    h->c.mv_dir = MV_DIR_FORWARD;
2001
2002
4.24k
                    for (i = 0; i < 2; i++) {
2003
2.83k
                        mx = ff_h263_decode_motion(h, h->c.last_mv[0][i][0], ctx->f_code);
2004
2.83k
                        my = ff_h263_decode_motion(h, h->c.last_mv[0][i][1] / 2, ctx->f_code);
2005
2.83k
                        h->c.last_mv[0][i][0] =
2006
2.83k
                        h->c.mv[0][i][0]      = mx;
2007
2.83k
                        h->c.last_mv[0][i][1] = (h->c.mv[0][i][1] = my) * 2;
2008
2.83k
                    }
2009
1.41k
                }
2010
2011
1.83k
                if (HAS_BACKWARD_MV(mb_type)) {
2012
1.57k
                    h->c.mv_dir |= MV_DIR_BACKWARD;
2013
2014
4.73k
                    for (i = 0; i < 2; i++) {
2015
3.15k
                        mx = ff_h263_decode_motion(h, h->c.last_mv[1][i][0], ctx->b_code);
2016
3.15k
                        my = ff_h263_decode_motion(h, h->c.last_mv[1][i][1] / 2, ctx->b_code);
2017
3.15k
                        h->c.last_mv[1][i][0] =
2018
3.15k
                        h->c.mv[1][i][0]      = mx;
2019
3.15k
                        h->c.last_mv[1][i][1] = (h->c.mv[1][i][1] = my) * 2;
2020
3.15k
                    }
2021
1.57k
                }
2022
1.83k
            }
2023
30.2k
        }
2024
2025
127k
        if (IS_DIRECT(mb_type)) {
2026
116k
            if (IS_SKIP(mb_type)) {
2027
97.6k
                mx =
2028
97.6k
                my = 0;
2029
97.6k
            } else {
2030
18.6k
                mx = ff_h263_decode_motion(h, 0, 1);
2031
18.6k
                my = ff_h263_decode_motion(h, 0, 1);
2032
18.6k
            }
2033
2034
116k
            h->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2035
116k
            mb_type  |= ff_mpeg4_set_direct_mv(&h->c, mx, my);
2036
116k
        }
2037
127k
        h->c.cur_pic.mb_type[xy] = mb_type;
2038
127k
    } else { /* I-Frame */
2039
83.3k
        int use_intra_dc_vlc;
2040
2041
84.1k
        do {
2042
84.1k
            cbpc = get_vlc2(&h->gb, ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 2);
2043
84.1k
            if (cbpc < 0) {
2044
7.16k
                av_log(h->c.avctx, AV_LOG_ERROR,
2045
7.16k
                       "I cbpc damaged at %d %d\n", h->c.mb_x, h->c.mb_y);
2046
7.16k
                return AVERROR_INVALIDDATA;
2047
7.16k
            }
2048
84.1k
        } while (cbpc == 8);
2049
2050
76.2k
        dquant = cbpc & 4;
2051
76.2k
        h->c.mb_intra = 1;
2052
2053
99.6k
intra:
2054
99.6k
        h->c.ac_pred = get_bits1(&h->gb);
2055
99.6k
        if (h->c.ac_pred)
2056
59.1k
            h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
2057
40.5k
        else
2058
40.5k
            h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA;
2059
2060
99.6k
        cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
2061
99.6k
        if (cbpy < 0) {
2062
4.01k
            av_log(h->c.avctx, AV_LOG_ERROR,
2063
4.01k
                   "I cbpy damaged at %d %d\n", h->c.mb_x, h->c.mb_y);
2064
4.01k
            return AVERROR_INVALIDDATA;
2065
4.01k
        }
2066
95.6k
        cbp = (cbpc & 3) | (cbpy << 2);
2067
2068
95.6k
        use_intra_dc_vlc = h->c.qscale < ctx->intra_dc_threshold;
2069
2070
95.6k
        if (dquant)
2071
33.0k
            ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]);
2072
2073
95.6k
        if (!h->c.progressive_sequence)
2074
48.8k
            h->c.interlaced_dct = get_bits1(&h->gb);
2075
2076
95.6k
        h->c.bdsp.clear_blocks(h->block[0]);
2077
        /* decode each block */
2078
527k
        for (i = 0; i < 6; i++) {
2079
467k
            if (mpeg4_decode_block(ctx, h->block[i], i, cbp & 32,
2080
467k
                                   1, use_intra_dc_vlc, 0) < 0)
2081
35.7k
                return AVERROR_INVALIDDATA;
2082
432k
            cbp += cbp;
2083
432k
        }
2084
59.9k
        goto end;
2085
95.6k
    }
2086
2087
    /* decode each block */
2088
1.98M
    for (i = 0; i < 6; i++) {
2089
1.72M
        if (mpeg4_decode_block(ctx, h->block[i], i, cbp & 32, 0, 0, 0) < 0)
2090
83.5k
            return AVERROR_INVALIDDATA;
2091
1.64M
        cbp += cbp;
2092
1.64M
    }
2093
2094
1.13M
end:
2095
    /* per-MB end of slice check */
2096
1.13M
    next = mpeg4_is_resync(ctx);
2097
1.13M
    if (next) {
2098
6.19k
        if (h->c.mb_x + h->c.mb_y*h->c.mb_width + 1 >  next && (h->c.avctx->err_recognition & AV_EF_AGGRESSIVE)) {
2099
408
            return AVERROR_INVALIDDATA;
2100
5.78k
        } else if (h->c.mb_x + h->c.mb_y*h->c.mb_width + 1 >= next)
2101
1.93k
            return SLICE_END;
2102
2103
3.85k
        if (h->c.pict_type == AV_PICTURE_TYPE_B) {
2104
1.34k
            const int delta = h->c.mb_x + 1 == h->c.mb_width ? 2 : 1;
2105
1.34k
            ff_thread_progress_await(&h->c.next_pic.ptr->progress,
2106
1.34k
                                        (h->c.mb_x + delta >= h->c.mb_width)
2107
1.34k
                                        ? FFMIN(h->c.mb_y + 1, h->c.mb_height - 1)
2108
1.34k
                                        : h->c.mb_y);
2109
1.34k
            if (h->c.next_pic.mbskip_table[xy + delta])
2110
844
                return SLICE_OK;
2111
1.34k
        }
2112
2113
3.01k
        return SLICE_END;
2114
3.85k
    }
2115
2116
1.12M
    return SLICE_OK;
2117
1.13M
}
2118
2119
/* As per spec, studio start code search isn't the same as the old type of start code */
2120
static void next_start_code_studio(GetBitContext *gb)
2121
222k
{
2122
222k
    align_get_bits(gb);
2123
2124
7.65M
    while (get_bits_left(gb) >= 24 && show_bits(gb, 24) != 0x1) {
2125
7.42M
        get_bits(gb, 8);
2126
7.42M
    }
2127
222k
}
2128
2129
/* additional_code, vlc index */
2130
static const uint8_t ac_state_tab[22][2] =
2131
{
2132
    {0, 0},
2133
    {0, 1},
2134
    {1, 1},
2135
    {2, 1},
2136
    {3, 1},
2137
    {4, 1},
2138
    {5, 1},
2139
    {1, 2},
2140
    {2, 2},
2141
    {3, 2},
2142
    {4, 2},
2143
    {5, 2},
2144
    {6, 2},
2145
    {1, 3},
2146
    {2, 4},
2147
    {3, 5},
2148
    {4, 6},
2149
    {5, 7},
2150
    {6, 8},
2151
    {7, 9},
2152
    {8, 10},
2153
    {0, 11}
2154
};
2155
2156
static int mpeg4_decode_studio_block(Mpeg4DecContext *const ctx, int32_t block[64], int n)
2157
94.4k
{
2158
94.4k
    H263DecContext *const h = &ctx->h;
2159
2160
94.4k
    int cc, dct_dc_size, dct_diff, code, j, idx = 1, group = 0, run = 0,
2161
94.4k
        additional_code_len, sign, mismatch;
2162
94.4k
    const VLCElem *cur_vlc = studio_intra_tab[0];
2163
94.4k
    const uint8_t *const scantable = h->c.intra_scantable.permutated;
2164
94.4k
    const uint16_t *quant_matrix;
2165
94.4k
    uint32_t flc;
2166
94.4k
    const int min = -1 *  (1 << (h->c.avctx->bits_per_raw_sample + 6));
2167
94.4k
    const int max =      ((1 << (h->c.avctx->bits_per_raw_sample + 6)) - 1);
2168
94.4k
    int shift = 3 - ctx->dct_precision;
2169
2170
94.4k
    mismatch = 1;
2171
2172
94.4k
    memset(block, 0, 64 * sizeof(int32_t));
2173
2174
94.4k
    if (n < 4) {
2175
40.6k
        cc = 0;
2176
40.6k
        dct_dc_size = get_vlc2(&h->gb, studio_luma_dc, STUDIO_INTRA_BITS, 2);
2177
40.6k
        quant_matrix = h->c.intra_matrix;
2178
53.8k
    } else {
2179
53.8k
        cc = (n & 1) + 1;
2180
53.8k
        if (ctx->rgb)
2181
25.4k
            dct_dc_size = get_vlc2(&h->gb, studio_luma_dc, STUDIO_INTRA_BITS, 2);
2182
28.3k
        else
2183
28.3k
            dct_dc_size = get_vlc2(&h->gb, studio_chroma_dc, STUDIO_INTRA_BITS, 2);
2184
53.8k
        quant_matrix = h->c.chroma_intra_matrix;
2185
53.8k
    }
2186
2187
94.4k
    if (dct_dc_size == 0) {
2188
4.00k
        dct_diff = 0;
2189
90.4k
    } else {
2190
90.4k
        dct_diff = get_xbits(&h->gb, dct_dc_size);
2191
2192
90.4k
        if (dct_dc_size > 8) {
2193
7.78k
            if(!check_marker(h->c.avctx, &h->gb, "dct_dc_size > 8"))
2194
409
                return AVERROR_INVALIDDATA;
2195
7.78k
        }
2196
2197
90.4k
    }
2198
2199
94.0k
    h->last_dc[cc] += dct_diff;
2200
2201
94.0k
    if (ctx->mpeg_quant)
2202
26.5k
        block[0] = h->last_dc[cc] * (8 >> h->c.intra_dc_precision);
2203
67.4k
    else
2204
67.4k
        block[0] = h->last_dc[cc] * (8 >> h->c.intra_dc_precision) * (8 >> ctx->dct_precision);
2205
    /* TODO: support mpeg_quant for AC coefficients */
2206
2207
94.0k
    block[0] = av_clip(block[0], min, max);
2208
94.0k
    mismatch ^= block[0];
2209
2210
    /* AC Coefficients */
2211
688k
    while (1) {
2212
688k
        group = get_vlc2(&h->gb, cur_vlc, STUDIO_INTRA_BITS, 2);
2213
2214
688k
        if (group < 0) {
2215
6.33k
            av_log(h->c.avctx, AV_LOG_ERROR, "illegal ac coefficient group vlc\n");
2216
6.33k
            return AVERROR_INVALIDDATA;
2217
6.33k
        }
2218
2219
682k
        additional_code_len = ac_state_tab[group][0];
2220
682k
        cur_vlc = studio_intra_tab[ac_state_tab[group][1]];
2221
2222
682k
        if (group == 0) {
2223
            /* End of Block */
2224
85.2k
            break;
2225
597k
        } else if (group >= 1 && group <= 6) {
2226
            /* Zero run length (Table B.47) */
2227
20.4k
            run = 1 << additional_code_len;
2228
20.4k
            if (additional_code_len)
2229
5.87k
                run += get_bits(&h->gb, additional_code_len);
2230
20.4k
            idx += run;
2231
20.4k
            continue;
2232
576k
        } else if (group >= 7 && group <= 12) {
2233
            /* Zero run length and +/-1 level (Table B.48) */
2234
135k
            code = get_bits(&h->gb, additional_code_len);
2235
135k
            sign = code & 1;
2236
135k
            code >>= 1;
2237
135k
            run = (1 << (additional_code_len - 1)) + code;
2238
135k
            idx += run;
2239
135k
            if (idx > 63)
2240
994
                return AVERROR_INVALIDDATA;
2241
134k
            j = scantable[idx++];
2242
134k
            block[j] = sign ? 1 : -1;
2243
441k
        } else if (group >= 13 && group <= 20) {
2244
            /* Level value (Table B.49) */
2245
438k
            if (idx > 63)
2246
1.18k
                return AVERROR_INVALIDDATA;
2247
437k
            j = scantable[idx++];
2248
437k
            block[j] = get_xbits(&h->gb, additional_code_len);
2249
437k
        } else if (group == 21) {
2250
            /* Escape */
2251
3.00k
            if (idx > 63)
2252
313
                return AVERROR_INVALIDDATA;
2253
2.69k
            j = scantable[idx++];
2254
2.69k
            additional_code_len = h->c.avctx->bits_per_raw_sample + ctx->dct_precision + 4;
2255
2.69k
            flc = get_bits(&h->gb, additional_code_len);
2256
2.69k
            if (flc >> (additional_code_len-1))
2257
555
                block[j] = -1 * (( flc ^ ((1 << additional_code_len) -1)) + 1);
2258
2.14k
            else
2259
2.14k
                block[j] = flc;
2260
2.69k
        }
2261
574k
        block[j] = ((block[j] * quant_matrix[j] * h->c.qscale) * (1 << shift)) / 16;
2262
574k
        block[j] = av_clip(block[j], min, max);
2263
574k
        mismatch ^= block[j];
2264
574k
    }
2265
2266
85.2k
    block[63] ^= mismatch & 1;
2267
2268
85.2k
    return 0;
2269
94.0k
}
2270
2271
static int mpeg4_decode_dpcm_macroblock(Mpeg4DecContext *const ctx,
2272
                                        int16_t macroblock[256], int n)
2273
16.9k
{
2274
16.9k
    H263DecContext *const h = &ctx->h;
2275
16.9k
    int j, w, height, idx = 0;
2276
16.9k
    int block_mean, rice_parameter, rice_prefix_code, rice_suffix_code,
2277
16.9k
        dpcm_residual, left, top, topleft, min_left_top, max_left_top, p, p2, output;
2278
16.9k
    height = 16 >> (n ? h->c.chroma_y_shift : 0);
2279
16.9k
    w = 16 >> (n ? h->c.chroma_x_shift : 0);
2280
2281
16.9k
    block_mean = get_bits(&h->gb, h->c.avctx->bits_per_raw_sample);
2282
16.9k
    if (block_mean == 0){
2283
1.91k
        av_log(h->c.avctx, AV_LOG_ERROR, "Forbidden block_mean\n");
2284
1.91k
        return AVERROR_INVALIDDATA;
2285
1.91k
    }
2286
15.0k
    h->last_dc[n] = block_mean * (1 << (ctx->dct_precision + h->c.intra_dc_precision));
2287
2288
15.0k
    rice_parameter = get_bits(&h->gb, 4);
2289
15.0k
    if (rice_parameter == 0) {
2290
698
        av_log(h->c.avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n");
2291
698
        return AVERROR_INVALIDDATA;
2292
698
    }
2293
2294
14.3k
    if (rice_parameter == 15)
2295
10.4k
        rice_parameter = 0;
2296
2297
14.3k
    if (rice_parameter > 11) {
2298
1.17k
        av_log(h->c.avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n");
2299
1.17k
        return AVERROR_INVALIDDATA;
2300
1.17k
    }
2301
2302
169k
    for (int i = 0; i < height; i++) {
2303
159k
        output = 1 << (h->c.avctx->bits_per_raw_sample - 1);
2304
159k
        top = 1 << (h->c.avctx->bits_per_raw_sample - 1);
2305
2306
2.62M
        for (j = 0; j < w; j++) {
2307
2.47M
            left = output;
2308
2.47M
            topleft = top;
2309
2310
2.47M
            rice_prefix_code = get_unary(&h->gb, 1, 12);
2311
2312
            /* Escape */
2313
2.47M
            if (rice_prefix_code == 11)
2314
1.81k
                dpcm_residual = get_bits(&h->gb, h->c.avctx->bits_per_raw_sample);
2315
2.46M
            else {
2316
2.46M
                if (rice_prefix_code == 12) {
2317
4.05k
                    av_log(h->c.avctx, AV_LOG_ERROR, "Forbidden rice_prefix_code\n");
2318
4.05k
                    return AVERROR_INVALIDDATA;
2319
4.05k
                }
2320
2.46M
                rice_suffix_code = get_bitsz(&h->gb, rice_parameter);
2321
2.46M
                dpcm_residual = (rice_prefix_code << rice_parameter) + rice_suffix_code;
2322
2.46M
            }
2323
2324
            /* Map to a signed residual */
2325
2.46M
            if (dpcm_residual & 1)
2326
243k
                dpcm_residual = (-1 * dpcm_residual) >> 1;
2327
2.22M
            else
2328
2.22M
                dpcm_residual = (dpcm_residual >> 1);
2329
2330
2.46M
            if (i != 0)
2331
2.27M
                top = macroblock[idx-w];
2332
2333
2.46M
            p = left + top - topleft;
2334
2.46M
            min_left_top = FFMIN(left, top);
2335
2.46M
            if (p < min_left_top)
2336
79.5k
                p = min_left_top;
2337
2338
2.46M
            max_left_top = FFMAX(left, top);
2339
2.46M
            if (p > max_left_top)
2340
72.0k
                p = max_left_top;
2341
2342
2.46M
            p2 = (FFMIN(min_left_top, topleft) + FFMAX(max_left_top, topleft)) >> 1;
2343
2.46M
            if (p2 == p)
2344
1.88M
                p2 = block_mean;
2345
2346
2.46M
            if (p2 > p)
2347
1.61M
                dpcm_residual *= -1;
2348
2349
2.46M
            macroblock[idx++] = output = (dpcm_residual + p) & ((1 << h->c.avctx->bits_per_raw_sample) - 1);
2350
2.46M
        }
2351
159k
    }
2352
2353
9.12k
    return 0;
2354
13.1k
}
2355
2356
static int mpeg4_decode_studio_mb(H263DecContext *const h)
2357
25.3k
{
2358
25.3k
    Mpeg4DecContext *const ctx = h263_to_mpeg4(h);
2359
25.3k
    int i;
2360
2361
25.3k
    ctx->dpcm_direction = 0;
2362
2363
    /* StudioMacroblock */
2364
    /* Assumes I-VOP */
2365
25.3k
    h->c.mb_intra = 1;
2366
25.3k
    if (get_bits1(&h->gb)) { /* compression_mode */
2367
        /* DCT */
2368
        /* macroblock_type, 1 or 2-bit VLC */
2369
15.1k
        if (!get_bits1(&h->gb)) {
2370
10.1k
            skip_bits1(&h->gb);
2371
10.1k
            h->c.qscale = mpeg_get_qscale(&h->gb, h->c.q_scale_type);
2372
10.1k
        }
2373
2374
100k
        for (i = 0; i < mpeg4_block_count[h->c.chroma_format]; i++) {
2375
94.4k
            if (mpeg4_decode_studio_block(ctx, ctx->block32[i], i) < 0)
2376
9.24k
                return AVERROR_INVALIDDATA;
2377
94.4k
        }
2378
15.1k
    } else {
2379
        /* DPCM */
2380
10.2k
        check_marker(h->c.avctx, &h->gb, "DPCM block start");
2381
10.2k
        ctx->dpcm_direction = get_bits1(&h->gb) ? -1 : 1;
2382
19.4k
        for (i = 0; i < 3; i++) {
2383
16.9k
            if (mpeg4_decode_dpcm_macroblock(ctx, ctx->dpcm_macroblock[i], i) < 0)
2384
7.83k
                return AVERROR_INVALIDDATA;
2385
16.9k
        }
2386
10.2k
    }
2387
2388
8.30k
    if (get_bits_left(&h->gb) >= 24 && show_bits(&h->gb, 23) == 0) {
2389
808
        next_start_code_studio(&h->gb);
2390
808
        return SLICE_END;
2391
808
    }
2392
2393
    //vcon-stp9L1.bits (first frame)
2394
7.49k
    if (get_bits_left(&h->gb) == 0)
2395
206
        return SLICE_END;
2396
2397
    //vcon-stp2L1.bits, vcon-stp3L1.bits, vcon-stp6L1.bits, vcon-stp7L1.bits, vcon-stp8L1.bits, vcon-stp10L1.bits (first frame)
2398
7.29k
    if (get_bits_left(&h->gb) < 8U && show_bits(&h->gb, get_bits_left(&h->gb)) == 0)
2399
197
        return SLICE_END;
2400
2401
7.09k
    return SLICE_OK;
2402
7.29k
}
2403
2404
static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
2405
72.3k
{
2406
72.3k
    int hours, minutes, seconds;
2407
2408
72.3k
    if (!show_bits(gb, 23)) {
2409
58.2k
        av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
2410
58.2k
        return AVERROR_INVALIDDATA;
2411
58.2k
    }
2412
2413
14.1k
    hours   = get_bits(gb, 5);
2414
14.1k
    minutes = get_bits(gb, 6);
2415
14.1k
    check_marker(s->avctx, gb, "in gop_header");
2416
14.1k
    seconds = get_bits(gb, 6);
2417
2418
14.1k
    s->time_base = seconds + 60*(minutes + 60*hours);
2419
2420
14.1k
    skip_bits1(gb);
2421
14.1k
    skip_bits1(gb);
2422
2423
14.1k
    return 0;
2424
72.3k
}
2425
2426
static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb, int *profile, int *level)
2427
10.1k
{
2428
2429
10.1k
    *profile = get_bits(gb, 4);
2430
10.1k
    *level   = get_bits(gb, 4);
2431
2432
    // for Simple profile, level 0
2433
10.1k
    if (*profile == 0 && *level == 8) {
2434
483
        *level = 0;
2435
483
    }
2436
2437
10.1k
    return 0;
2438
10.1k
}
2439
2440
static int mpeg4_decode_visual_object(MpegEncContext *s, GetBitContext *gb)
2441
14.5k
{
2442
14.5k
    int visual_object_type;
2443
14.5k
    int is_visual_object_identifier = get_bits1(gb);
2444
2445
14.5k
    if (is_visual_object_identifier) {
2446
6.95k
        skip_bits(gb, 4+3);
2447
6.95k
    }
2448
14.5k
    visual_object_type = get_bits(gb, 4);
2449
2450
14.5k
    if (visual_object_type == VOT_VIDEO_ID ||
2451
11.3k
        visual_object_type == VOT_STILL_TEXTURE_ID) {
2452
7.03k
        int video_signal_type = get_bits1(gb);
2453
7.03k
        if (video_signal_type) {
2454
4.74k
            int video_range, color_description;
2455
4.74k
            skip_bits(gb, 3); // video_format
2456
4.74k
            video_range = get_bits1(gb);
2457
4.74k
            color_description = get_bits1(gb);
2458
2459
4.74k
            s->avctx->color_range = video_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
2460
2461
4.74k
            if (color_description) {
2462
3.27k
                s->avctx->color_primaries = get_bits(gb, 8);
2463
3.27k
                s->avctx->color_trc       = get_bits(gb, 8);
2464
3.27k
                s->avctx->colorspace      = get_bits(gb, 8);
2465
3.27k
            }
2466
4.74k
        }
2467
7.03k
    }
2468
2469
14.5k
    return 0;
2470
14.5k
}
2471
2472
static void mpeg4_load_default_matrices(MpegEncContext *s)
2473
217k
{
2474
217k
    int i, v;
2475
2476
    /* load default matrices */
2477
14.1M
    for (i = 0; i < 64; i++) {
2478
13.9M
        int j = s->idsp.idct_permutation[i];
2479
13.9M
        v = ff_mpeg4_default_intra_matrix[i];
2480
13.9M
        s->intra_matrix[j]        = v;
2481
13.9M
        s->chroma_intra_matrix[j] = v;
2482
2483
13.9M
        v = ff_mpeg4_default_non_intra_matrix[i];
2484
13.9M
        s->inter_matrix[j]        = v;
2485
13.9M
        s->chroma_inter_matrix[j] = v;
2486
13.9M
    }
2487
217k
}
2488
2489
static int read_quant_matrix_ext(MpegEncContext *s, GetBitContext *gb)
2490
18.2k
{
2491
18.2k
    int i, j, v;
2492
2493
18.2k
    if (get_bits1(gb)) {
2494
14.1k
        if (get_bits_left(gb) < 64*8)
2495
5.26k
            return AVERROR_INVALIDDATA;
2496
        /* intra_quantiser_matrix */
2497
574k
        for (i = 0; i < 64; i++) {
2498
565k
            v = get_bits(gb, 8);
2499
565k
            j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2500
565k
            s->intra_matrix[j]        = v;
2501
565k
            s->chroma_intra_matrix[j] = v;
2502
565k
        }
2503
8.84k
    }
2504
2505
12.9k
    if (get_bits1(gb)) {
2506
8.58k
        if (get_bits_left(gb) < 64*8)
2507
1.98k
            return AVERROR_INVALIDDATA;
2508
        /* non_intra_quantiser_matrix */
2509
429k
        for (i = 0; i < 64; i++) {
2510
422k
            get_bits(gb, 8);
2511
422k
        }
2512
6.60k
    }
2513
2514
11.0k
    if (get_bits1(gb)) {
2515
6.47k
        if (get_bits_left(gb) < 64*8)
2516
1.15k
            return AVERROR_INVALIDDATA;
2517
        /* chroma_intra_quantiser_matrix */
2518
346k
        for (i = 0; i < 64; i++) {
2519
340k
            v = get_bits(gb, 8);
2520
340k
            j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2521
340k
            s->chroma_intra_matrix[j] = v;
2522
340k
        }
2523
5.32k
    }
2524
2525
9.84k
    if (get_bits1(gb)) {
2526
5.91k
        if (get_bits_left(gb) < 64*8)
2527
1.14k
            return AVERROR_INVALIDDATA;
2528
        /* chroma_non_intra_quantiser_matrix */
2529
310k
        for (i = 0; i < 64; i++) {
2530
305k
            get_bits(gb, 8);
2531
305k
        }
2532
4.77k
    }
2533
2534
8.70k
    next_start_code_studio(gb);
2535
8.70k
    return 0;
2536
9.84k
}
2537
2538
static void extension_and_user_data(MpegEncContext *s, GetBitContext *gb, int id)
2539
213k
{
2540
213k
    uint32_t startcode;
2541
213k
    uint8_t extension_type;
2542
2543
213k
    startcode = show_bits_long(gb, 32);
2544
213k
    if (startcode == USER_DATA_STARTCODE || startcode == EXT_STARTCODE) {
2545
2546
21.2k
        if ((id == 2 || id == 4) && startcode == EXT_STARTCODE) {
2547
19.5k
            skip_bits_long(gb, 32);
2548
19.5k
            extension_type = get_bits(gb, 4);
2549
19.5k
            if (extension_type == QUANT_MATRIX_EXT_ID)
2550
18.2k
                read_quant_matrix_ext(s, gb);
2551
19.5k
        }
2552
21.2k
    }
2553
213k
}
2554
2555
static int decode_studio_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2556
33.3k
{
2557
33.3k
    MPVContext *const s = &ctx->h.c;
2558
33.3k
    int width, height, aspect_ratio_info;
2559
33.3k
    int bits_per_raw_sample;
2560
33.3k
    int rgb, chroma_format;
2561
2562
    // random_accessible_vol and video_object_type_indication have already
2563
    // been read by the caller decode_vol_header()
2564
33.3k
    skip_bits(gb, 4); /* video_object_layer_verid */
2565
33.3k
    ctx->shape = get_bits(gb, 2); /* video_object_layer_shape */
2566
33.3k
    skip_bits(gb, 4); /* video_object_layer_shape_extension */
2567
33.3k
    skip_bits1(gb); /* progressive_sequence */
2568
33.3k
    if (ctx->shape != RECT_SHAPE) {
2569
1.45k
        avpriv_request_sample(s->avctx, "MPEG-4 Studio profile non rectangular shape");
2570
1.45k
        return AVERROR_PATCHWELCOME;
2571
1.45k
    }
2572
31.8k
    if (ctx->shape != BIN_ONLY_SHAPE) {
2573
31.8k
        rgb = get_bits1(gb); /* rgb_components */
2574
31.8k
        chroma_format = get_bits(gb, 2); /* chroma_format */
2575
31.8k
        if (!chroma_format || chroma_format == CHROMA_420 || (rgb && chroma_format == CHROMA_422)) {
2576
1.68k
            av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
2577
1.68k
            return AVERROR_INVALIDDATA;
2578
1.68k
        }
2579
2580
30.1k
        bits_per_raw_sample = get_bits(gb, 4); /* bit_depth */
2581
30.1k
        if (bits_per_raw_sample == 10) {
2582
28.3k
            if (rgb) {
2583
22.9k
                s->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
2584
22.9k
            } else {
2585
5.39k
                s->avctx->pix_fmt = chroma_format == CHROMA_422 ? AV_PIX_FMT_YUV422P10 : AV_PIX_FMT_YUV444P10;
2586
5.39k
            }
2587
28.3k
        } else {
2588
1.78k
            avpriv_request_sample(s->avctx, "MPEG-4 Studio profile bit-depth %u", bits_per_raw_sample);
2589
1.78k
            return AVERROR_PATCHWELCOME;
2590
1.78k
        }
2591
28.3k
        if (rgb != ctx->rgb || s->chroma_format != chroma_format)
2592
4.24k
            s->context_reinit = 1;
2593
28.3k
        s->avctx->bits_per_raw_sample = bits_per_raw_sample;
2594
28.3k
        ctx->rgb = rgb;
2595
28.3k
        s->chroma_format = chroma_format;
2596
28.3k
    }
2597
28.3k
    if (ctx->shape == RECT_SHAPE) {
2598
28.3k
        check_marker(s->avctx, gb, "before video_object_layer_width");
2599
28.3k
        width  = get_bits(gb, 14); /* video_object_layer_width */
2600
28.3k
        check_marker(s->avctx, gb, "before video_object_layer_height");
2601
28.3k
        height = get_bits(gb, 14); /* video_object_layer_height */
2602
28.3k
        check_marker(s->avctx, gb, "after video_object_layer_height");
2603
2604
        /* Do the same check as non-studio profile */
2605
28.3k
        if (width && height) {
2606
24.3k
            if (s->width && s->height &&
2607
22.0k
                (s->width != width || s->height != height))
2608
6.99k
                s->context_reinit = 1;
2609
24.3k
            s->width  = width;
2610
24.3k
            s->height = height;
2611
24.3k
        }
2612
28.3k
    }
2613
28.3k
    aspect_ratio_info = get_bits(gb, 4);
2614
28.3k
    if (aspect_ratio_info == FF_ASPECT_EXTENDED) {
2615
1.91k
        s->avctx->sample_aspect_ratio.num = get_bits(gb, 8);  // par_width
2616
1.91k
        s->avctx->sample_aspect_ratio.den = get_bits(gb, 8);  // par_height
2617
26.4k
    } else {
2618
26.4k
        s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[aspect_ratio_info];
2619
26.4k
    }
2620
28.3k
    skip_bits(gb, 4); /* frame_rate_code */
2621
28.3k
    skip_bits(gb, 15); /* first_half_bit_rate */
2622
28.3k
    check_marker(s->avctx, gb, "after first_half_bit_rate");
2623
28.3k
    skip_bits(gb, 15); /* latter_half_bit_rate */
2624
28.3k
    check_marker(s->avctx, gb, "after latter_half_bit_rate");
2625
28.3k
    skip_bits(gb, 15); /* first_half_vbv_buffer_size */
2626
28.3k
    check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2627
28.3k
    skip_bits(gb, 3); /* latter_half_vbv_buffer_size */
2628
28.3k
    skip_bits(gb, 11); /* first_half_vbv_buffer_size */
2629
28.3k
    check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2630
28.3k
    skip_bits(gb, 15); /* latter_half_vbv_occupancy */
2631
28.3k
    check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
2632
28.3k
    s->low_delay  = get_bits1(gb);
2633
28.3k
    ctx->mpeg_quant = get_bits1(gb); /* mpeg2_stream */
2634
2635
28.3k
    next_start_code_studio(gb);
2636
28.3k
    extension_and_user_data(s, gb, 2);
2637
2638
28.3k
    return 0;
2639
31.8k
}
2640
2641
static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2642
180k
{
2643
180k
    H263DecContext *const h = &ctx->h;
2644
180k
    int width, height, vo_ver_id, aspect_ratio_info;
2645
2646
    /* vol header */
2647
180k
    skip_bits(gb, 1);                   /* random access */
2648
180k
    ctx->vo_type = get_bits(gb, 8);
2649
2650
    /* If we are in studio profile (per vo_type), check if its all consistent
2651
     * and if so continue pass control to decode_studio_vol_header().
2652
     * elIf something is inconsistent, error out
2653
     * else continue with (non studio) vol header decpoding.
2654
     */
2655
180k
    if (ctx->vo_type == CORE_STUDIO_VO_TYPE ||
2656
175k
        ctx->vo_type == SIMPLE_STUDIO_VO_TYPE) {
2657
37.0k
        if (h->c.avctx->profile != AV_PROFILE_UNKNOWN && h->c.avctx->profile != AV_PROFILE_MPEG4_SIMPLE_STUDIO)
2658
3.75k
            return AVERROR_INVALIDDATA;
2659
33.3k
        h->c.studio_profile = 1;
2660
33.3k
        h->c.avctx->profile = AV_PROFILE_MPEG4_SIMPLE_STUDIO;
2661
33.3k
        return decode_studio_vol_header(ctx, gb);
2662
143k
    } else if (h->c.studio_profile) {
2663
11.5k
        return AVERROR_PATCHWELCOME;
2664
11.5k
    }
2665
2666
132k
    if (get_bits1(gb) != 0) {           /* is_ol_id */
2667
73.0k
        vo_ver_id = get_bits(gb, 4);    /* vo_ver_id */
2668
73.0k
        skip_bits(gb, 3);               /* vo_priority */
2669
73.0k
    } else {
2670
59.2k
        vo_ver_id = 1;
2671
59.2k
    }
2672
132k
    aspect_ratio_info = get_bits(gb, 4);
2673
132k
    if (aspect_ratio_info == FF_ASPECT_EXTENDED) {
2674
7.14k
        h->c.avctx->sample_aspect_ratio.num = get_bits(gb, 8);  // par_width
2675
7.14k
        h->c.avctx->sample_aspect_ratio.den = get_bits(gb, 8);  // par_height
2676
125k
    } else {
2677
125k
        h->c.avctx->sample_aspect_ratio = ff_h263_pixel_aspect[aspect_ratio_info];
2678
125k
    }
2679
2680
132k
    if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
2681
52.8k
        int chroma_format = get_bits(gb, 2);
2682
52.8k
        if (chroma_format != CHROMA_420)
2683
34.3k
            av_log(h->c.avctx, AV_LOG_ERROR, "illegal chroma format\n");
2684
2685
52.8k
        h->c.low_delay = get_bits1(gb);
2686
52.8k
        if (get_bits1(gb)) {    /* vbv parameters */
2687
26.3k
            get_bits(gb, 15);   /* first_half_bitrate */
2688
26.3k
            check_marker(h->c.avctx, gb, "after first_half_bitrate");
2689
26.3k
            get_bits(gb, 15);   /* latter_half_bitrate */
2690
26.3k
            check_marker(h->c.avctx, gb, "after latter_half_bitrate");
2691
26.3k
            get_bits(gb, 15);   /* first_half_vbv_buffer_size */
2692
26.3k
            check_marker(h->c.avctx, gb, "after first_half_vbv_buffer_size");
2693
26.3k
            get_bits(gb, 3);    /* latter_half_vbv_buffer_size */
2694
26.3k
            get_bits(gb, 11);   /* first_half_vbv_occupancy */
2695
26.3k
            check_marker(h->c.avctx, gb, "after first_half_vbv_occupancy");
2696
26.3k
            get_bits(gb, 15);   /* latter_half_vbv_occupancy */
2697
26.3k
            check_marker(h->c.avctx, gb, "after latter_half_vbv_occupancy");
2698
26.3k
        }
2699
79.4k
    } else {
2700
        /* is setting low delay flag only once the smartest thing to do?
2701
         * low delay detection will not be overridden. */
2702
79.4k
        if (h->picture_number == 0) {
2703
12.4k
            switch (ctx->vo_type) {
2704
1.12k
            case SIMPLE_VO_TYPE:
2705
1.35k
            case ADV_SIMPLE_VO_TYPE:
2706
1.35k
                h->c.low_delay = 1;
2707
1.35k
                break;
2708
11.1k
            default:
2709
11.1k
                h->c.low_delay = 0;
2710
12.4k
            }
2711
12.4k
        }
2712
79.4k
    }
2713
2714
132k
    ctx->shape = get_bits(gb, 2); /* vol shape */
2715
132k
    if (ctx->shape != RECT_SHAPE)
2716
31.9k
        av_log(h->c.avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
2717
132k
    if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
2718
8.74k
        av_log(h->c.avctx, AV_LOG_ERROR, "Gray shape not supported\n");
2719
8.74k
        skip_bits(gb, 4);  /* video_object_layer_shape_extension */
2720
8.74k
    }
2721
2722
132k
    check_marker(h->c.avctx, gb, "before time_increment_resolution");
2723
2724
132k
    h->c.avctx->framerate.num = get_bits(gb, 16);
2725
132k
    if (!h->c.avctx->framerate.num) {
2726
4.42k
        av_log(h->c.avctx, AV_LOG_ERROR, "framerate==0\n");
2727
4.42k
        return AVERROR_INVALIDDATA;
2728
4.42k
    }
2729
2730
127k
    ctx->time_increment_bits = av_log2(h->c.avctx->framerate.num - 1) + 1;
2731
127k
    if (ctx->time_increment_bits < 1)
2732
0
        ctx->time_increment_bits = 1;
2733
2734
127k
    check_marker(h->c.avctx, gb, "before fixed_vop_rate");
2735
2736
127k
    if (get_bits1(gb) != 0)     /* fixed_vop_rate  */
2737
59.2k
        h->c.avctx->framerate.den = get_bits(gb, ctx->time_increment_bits);
2738
68.5k
    else
2739
68.5k
        h->c.avctx->framerate.den = 1;
2740
2741
127k
    ctx->t_frame = 0;
2742
2743
127k
    if (ctx->shape != BIN_ONLY_SHAPE) {
2744
119k
        if (ctx->shape == RECT_SHAPE) {
2745
96.5k
            check_marker(h->c.avctx, gb, "before width");
2746
96.5k
            width = get_bits(gb, 13);
2747
96.5k
            check_marker(h->c.avctx, gb, "before height");
2748
96.5k
            height = get_bits(gb, 13);
2749
96.5k
            check_marker(h->c.avctx, gb, "after height");
2750
96.5k
            if (width && height &&  /* they should be non zero but who knows */
2751
79.1k
                !(h->c.width && h->c.codec_tag == AV_RL32("MP4S"))) {
2752
78.9k
                if (h->c.width && h->c.height &&
2753
66.4k
                    (h->c.width != width || h->c.height != height))
2754
45.1k
                    h->c.context_reinit = 1;
2755
78.9k
                h->c.width  = width;
2756
78.9k
                h->c.height = height;
2757
78.9k
            }
2758
96.5k
        }
2759
2760
119k
        h->c.progressive_sequence  =
2761
119k
        h->c.progressive_frame     = get_bits1(gb) ^ 1;
2762
119k
        h->c.interlaced_dct        = 0;
2763
119k
        if (!get_bits1(gb) && (h->c.avctx->debug & FF_DEBUG_PICT_INFO))
2764
0
            av_log(h->c.avctx, AV_LOG_INFO,           /* OBMC Disable */
2765
0
                   "MPEG-4 OBMC not supported (very likely buggy encoder)\n");
2766
119k
        if (vo_ver_id == 1)
2767
54.3k
            ctx->vol_sprite_usage = get_bits1(gb);    /* vol_sprite_usage */
2768
64.7k
        else
2769
64.7k
            ctx->vol_sprite_usage = get_bits(gb, 2);  /* vol_sprite_usage */
2770
2771
119k
        if (ctx->vol_sprite_usage == STATIC_SPRITE)
2772
16.7k
            av_log(h->c.avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
2773
119k
        if (ctx->vol_sprite_usage == STATIC_SPRITE ||
2774
102k
            ctx->vol_sprite_usage == GMC_SPRITE) {
2775
29.2k
            if (ctx->vol_sprite_usage == STATIC_SPRITE) {
2776
16.7k
                skip_bits(gb, 13); // sprite_width
2777
16.7k
                check_marker(h->c.avctx, gb, "after sprite_width");
2778
16.7k
                skip_bits(gb, 13); // sprite_height
2779
16.7k
                check_marker(h->c.avctx, gb, "after sprite_height");
2780
16.7k
                skip_bits(gb, 13); // sprite_left
2781
16.7k
                check_marker(h->c.avctx, gb, "after sprite_left");
2782
16.7k
                skip_bits(gb, 13); // sprite_top
2783
16.7k
                check_marker(h->c.avctx, gb, "after sprite_top");
2784
16.7k
            }
2785
29.2k
            ctx->num_sprite_warping_points = get_bits(gb, 6);
2786
29.2k
            if (ctx->num_sprite_warping_points > 3) {
2787
11.4k
                av_log(h->c.avctx, AV_LOG_ERROR,
2788
11.4k
                       "%d sprite_warping_points\n",
2789
11.4k
                       ctx->num_sprite_warping_points);
2790
11.4k
                ctx->num_sprite_warping_points = 0;
2791
11.4k
                return AVERROR_INVALIDDATA;
2792
11.4k
            }
2793
17.8k
            ctx->sprite_warping_accuracy  = get_bits(gb, 2);
2794
17.8k
            ctx->sprite_brightness_change = get_bits1(gb);
2795
17.8k
            if (ctx->vol_sprite_usage == STATIC_SPRITE)
2796
6.31k
                skip_bits1(gb); // low_latency_sprite
2797
17.8k
        }
2798
        // FIXME sadct disable bit if verid!=1 && shape not rect
2799
2800
107k
        if (get_bits1(gb) == 1) {                   /* not_8_bit */
2801
34.9k
            ctx->quant_precision = get_bits(gb, 4); /* quant_precision */
2802
34.9k
            if (get_bits(gb, 4) != 8)               /* bits_per_pixel */
2803
34.0k
                av_log(h->c.avctx, AV_LOG_ERROR, "N-bit not supported\n");
2804
34.9k
            if (ctx->quant_precision != 5)
2805
33.1k
                av_log(h->c.avctx, AV_LOG_ERROR,
2806
33.1k
                       "quant precision %d\n", ctx->quant_precision);
2807
34.9k
            if (ctx->quant_precision < 3 || ctx->quant_precision > 9)
2808
27.1k
                ctx->quant_precision = 5;
2809
72.6k
        } else {
2810
72.6k
            ctx->quant_precision = 5;
2811
72.6k
        }
2812
2813
        // FIXME a bunch of grayscale shape things
2814
2815
107k
        if ((ctx->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
2816
36.6k
            int i, v;
2817
2818
36.6k
            mpeg4_load_default_matrices(&h->c);
2819
2820
            /* load custom intra matrix */
2821
36.6k
            if (get_bits1(gb)) {
2822
21.8k
                int last = 0;
2823
183k
                for (i = 0; i < 64; i++) {
2824
182k
                    int j;
2825
182k
                    if (get_bits_left(gb) < 8) {
2826
7.45k
                        av_log(h->c.avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2827
7.45k
                        return AVERROR_INVALIDDATA;
2828
7.45k
                    }
2829
175k
                    v = get_bits(gb, 8);
2830
175k
                    if (v == 0)
2831
13.5k
                        break;
2832
2833
161k
                    last = v;
2834
161k
                    j = h->c.idsp.idct_permutation[ff_zigzag_direct[i]];
2835
161k
                    h->c.intra_matrix[j]        = last;
2836
161k
                }
2837
2838
                /* replicate last value */
2839
777k
                for (; i < 64; i++) {
2840
763k
                    int j = h->c.idsp.idct_permutation[ff_zigzag_direct[i]];
2841
763k
                    h->c.intra_matrix[j]        = last;
2842
763k
                }
2843
14.4k
            }
2844
2845
            /* load custom non intra matrix */
2846
29.2k
            if (get_bits1(gb)) {
2847
11.9k
                int last = 0;
2848
123k
                for (i = 0; i < 64; i++) {
2849
123k
                    int j;
2850
123k
                    if (get_bits_left(gb) < 8) {
2851
436
                        av_log(h->c.avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2852
436
                        return AVERROR_INVALIDDATA;
2853
436
                    }
2854
122k
                    v = get_bits(gb, 8);
2855
122k
                    if (v == 0)
2856
10.8k
                        break;
2857
2858
112k
                    last = v;
2859
112k
                    j = h->c.idsp.idct_permutation[ff_zigzag_direct[i]];
2860
112k
                    h->c.inter_matrix[j]        = v;
2861
112k
                }
2862
2863
                /* replicate last value */
2864
636k
                for (; i < 64; i++) {
2865
625k
                    int j = h->c.idsp.idct_permutation[ff_zigzag_direct[i]];
2866
625k
                    h->c.inter_matrix[j]        = last;
2867
625k
                }
2868
11.4k
            }
2869
2870
            // FIXME a bunch of grayscale shape things
2871
29.2k
        }
2872
2873
99.7k
        if (vo_ver_id != 1)
2874
54.9k
            h->c.quarter_sample = get_bits1(gb);
2875
44.8k
        else
2876
44.8k
            h->c.quarter_sample = 0;
2877
2878
99.7k
        if (get_bits_left(gb) < 4) {
2879
5.56k
            av_log(h->c.avctx, AV_LOG_ERROR, "VOL Header truncated\n");
2880
5.56k
            return AVERROR_INVALIDDATA;
2881
5.56k
        }
2882
2883
94.1k
        if (!get_bits1(gb)) {
2884
76.3k
            int pos               = get_bits_count(gb);
2885
76.3k
            int estimation_method = get_bits(gb, 2);
2886
76.3k
            if (estimation_method < 2) {
2887
57.9k
                if (!get_bits1(gb)) {
2888
49.8k
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* opaque */
2889
49.8k
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* transparent */
2890
49.8k
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* intra_cae */
2891
49.8k
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* inter_cae */
2892
49.8k
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* no_update */
2893
49.8k
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* upsampling */
2894
49.8k
                }
2895
57.9k
                if (!get_bits1(gb)) {
2896
39.5k
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* intra_blocks */
2897
39.5k
                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* inter_blocks */
2898
39.5k
                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* inter4v_blocks */
2899
39.5k
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* not coded blocks */
2900
39.5k
                }
2901
57.9k
                if (!check_marker(h->c.avctx, gb, "in complexity estimation part 1")) {
2902
35.6k
                    skip_bits_long(gb, pos - get_bits_count(gb));
2903
35.6k
                    goto no_cplx_est;
2904
35.6k
                }
2905
22.2k
                if (!get_bits1(gb)) {
2906
9.43k
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* dct_coeffs */
2907
9.43k
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* dct_lines */
2908
9.43k
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* vlc_syms */
2909
9.43k
                    ctx->cplx_estimation_trash_i += 4 * get_bits1(gb);  /* vlc_bits */
2910
9.43k
                }
2911
22.2k
                if (!get_bits1(gb)) {
2912
7.15k
                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* apm */
2913
7.15k
                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* npm */
2914
7.15k
                    ctx->cplx_estimation_trash_b += 8 * get_bits1(gb);  /* interpolate_mc_q */
2915
7.15k
                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* forwback_mc_q */
2916
7.15k
                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* halfpel2 */
2917
7.15k
                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* halfpel4 */
2918
7.15k
                }
2919
22.2k
                if (!check_marker(h->c.avctx, gb, "in complexity estimation part 2")) {
2920
9.24k
                    skip_bits_long(gb, pos - get_bits_count(gb));
2921
9.24k
                    goto no_cplx_est;
2922
9.24k
                }
2923
13.0k
                if (estimation_method == 1) {
2924
7.33k
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* sadct */
2925
7.33k
                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* qpel */
2926
7.33k
                }
2927
13.0k
            } else
2928
18.3k
                av_log(h->c.avctx, AV_LOG_ERROR,
2929
18.3k
                       "Invalid Complexity estimation method %d\n",
2930
18.3k
                       estimation_method);
2931
76.3k
        } else {
2932
2933
62.7k
no_cplx_est:
2934
62.7k
            ctx->cplx_estimation_trash_i =
2935
62.7k
            ctx->cplx_estimation_trash_p =
2936
62.7k
            ctx->cplx_estimation_trash_b = 0;
2937
62.7k
        }
2938
2939
94.1k
        ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
2940
2941
94.1k
        h->data_partitioning = get_bits1(gb);
2942
94.1k
        if (h->data_partitioning)
2943
28.0k
            ctx->rvlc = get_bits1(gb);
2944
2945
94.1k
        if (vo_ver_id != 1) {
2946
53.1k
            ctx->new_pred = get_bits1(gb);
2947
53.1k
            if (ctx->new_pred) {
2948
14.6k
                av_log(h->c.avctx, AV_LOG_ERROR, "new pred not supported\n");
2949
14.6k
                skip_bits(gb, 2); /* requested upstream message type */
2950
14.6k
                skip_bits1(gb);   /* newpred segment type */
2951
14.6k
            }
2952
53.1k
            if (get_bits1(gb)) // reduced_res_vop
2953
14.4k
                av_log(h->c.avctx, AV_LOG_ERROR,
2954
14.4k
                       "reduced resolution VOP not supported\n");
2955
53.1k
        } else {
2956
41.0k
            ctx->new_pred = 0;
2957
41.0k
        }
2958
2959
94.1k
        ctx->scalability = get_bits1(gb);
2960
2961
94.1k
        if (ctx->scalability) {
2962
30.2k
            GetBitContext bak = *gb;
2963
30.2k
            int h_sampling_factor_n;
2964
30.2k
            int h_sampling_factor_m;
2965
30.2k
            int v_sampling_factor_n;
2966
30.2k
            int v_sampling_factor_m;
2967
2968
30.2k
            skip_bits1(gb);    // hierarchy_type
2969
30.2k
            skip_bits(gb, 4);  /* ref_layer_id */
2970
30.2k
            skip_bits1(gb);    /* ref_layer_sampling_dir */
2971
30.2k
            h_sampling_factor_n = get_bits(gb, 5);
2972
30.2k
            h_sampling_factor_m = get_bits(gb, 5);
2973
30.2k
            v_sampling_factor_n = get_bits(gb, 5);
2974
30.2k
            v_sampling_factor_m = get_bits(gb, 5);
2975
30.2k
            ctx->enhancement_type = get_bits1(gb);
2976
2977
30.2k
            if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
2978
23.1k
                v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
2979
                /* illegal scalability header (VERY broken encoder),
2980
                 * trying to workaround */
2981
23.1k
                ctx->scalability = 0;
2982
23.1k
                *gb            = bak;
2983
23.1k
            } else
2984
7.09k
                av_log(h->c.avctx, AV_LOG_ERROR, "scalability not supported\n");
2985
2986
            // bin shape stuff FIXME
2987
30.2k
        }
2988
94.1k
    }
2989
2990
102k
    if (h->c.avctx->debug&FF_DEBUG_PICT_INFO) {
2991
0
        av_log(h->c.avctx, AV_LOG_DEBUG, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d, low_delay:%d  %s%s%s%s\n",
2992
0
               h->c.avctx->framerate.den, h->c.avctx->framerate.num,
2993
0
               ctx->time_increment_bits,
2994
0
               ctx->quant_precision,
2995
0
               h->c.progressive_sequence,
2996
0
               h->c.low_delay,
2997
0
               ctx->scalability ? "scalability " :"" ,
2998
0
               h->c.quarter_sample ? "qpel " : "",
2999
0
               h->data_partitioning ? "partition " : "",
3000
0
               ctx->rvlc ? "rvlc " : ""
3001
0
        );
3002
0
    }
3003
3004
102k
    return 0;
3005
127k
}
3006
3007
/**
3008
 * Decode the user data stuff in the header.
3009
 * Also initializes divx/xvid/lavc_version/build.
3010
 */
3011
static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
3012
25.7k
{
3013
25.7k
    H263DecContext *const h = &ctx->h;
3014
25.7k
    char buf[256];
3015
25.7k
    int i;
3016
25.7k
    int e;
3017
25.7k
    int ver = 0, build = 0, ver2 = 0, ver3 = 0;
3018
25.7k
    char last;
3019
3020
687k
    for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
3021
687k
        if (show_bits(gb, 23) == 0)
3022
25.0k
            break;
3023
662k
        buf[i] = get_bits(gb, 8);
3024
662k
    }
3025
25.7k
    buf[i] = 0;
3026
3027
    /* divx detection */
3028
25.7k
    e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
3029
25.7k
    if (e < 2)
3030
25.7k
        e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
3031
25.7k
    if (e >= 2) {
3032
3.11k
        ctx->divx_version = ver;
3033
3.11k
        ctx->divx_build   = build;
3034
3.11k
        h->divx_packed  = e == 3 && last == 'p';
3035
3.11k
    }
3036
3037
    /* libavcodec detection */
3038
25.7k
    e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
3039
25.7k
    if (e != 4)
3040
25.7k
        e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
3041
25.7k
    if (e != 4) {
3042
25.7k
        e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
3043
25.7k
        if (e > 1) {
3044
1.75k
            if (ver > 0xFFU || ver2 > 0xFFU || ver3 > 0xFFU) {
3045
955
                av_log(h->c.avctx, AV_LOG_WARNING,
3046
955
                     "Unknown Lavc version string encountered, %d.%d.%d; "
3047
955
                     "clamping sub-version values to 8-bits.\n",
3048
955
                     ver, ver2, ver3);
3049
955
            }
3050
1.75k
            build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF);
3051
1.75k
        }
3052
25.7k
    }
3053
25.7k
    if (e != 4) {
3054
24.8k
        if (strcmp(buf, "ffmpeg") == 0)
3055
2.30k
            ctx->lavc_build = 4600;
3056
24.8k
    }
3057
25.7k
    if (e == 4)
3058
917
        ctx->lavc_build = build;
3059
3060
    /* Xvid detection */
3061
25.7k
    e = sscanf(buf, "XviD%d", &build);
3062
25.7k
    if (e == 1)
3063
6.33k
        ctx->xvid_build = build;
3064
3065
25.7k
    return 0;
3066
25.7k
}
3067
3068
static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb,
3069
                             int parse_only)
3070
732k
{
3071
732k
    H263DecContext *const h = &ctx->h;
3072
732k
    int time_incr, time_increment;
3073
732k
    int64_t pts;
3074
3075
732k
    h->c.mcsel     = 0;
3076
732k
    h->c.pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I;        /* pict type: I = 0 , P = 1 */
3077
732k
    if (h->c.pict_type == AV_PICTURE_TYPE_B && h->c.low_delay &&
3078
12.1k
        ctx->vol_control_parameters == 0 && !(h->c.avctx->flags & AV_CODEC_FLAG_LOW_DELAY)) {
3079
2.67k
        av_log(h->c.avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n");
3080
2.67k
        h->c.low_delay = 0;
3081
2.67k
    }
3082
3083
732k
    h->partitioned_frame = h->data_partitioning && h->c.pict_type != AV_PICTURE_TYPE_B;
3084
732k
    if (h->partitioned_frame)
3085
117k
        h->decode_mb = mpeg4_decode_partitioned_mb;
3086
615k
    else
3087
615k
        h->decode_mb = mpeg4_decode_mb;
3088
3089
732k
    time_incr = 0;
3090
1.83M
    while (get_bits1(gb) != 0)
3091
1.10M
        time_incr++;
3092
3093
732k
    check_marker(h->c.avctx, gb, "before time_increment");
3094
3095
732k
    if (ctx->time_increment_bits == 0 ||
3096
731k
        !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
3097
216k
        av_log(h->c.avctx, AV_LOG_WARNING,
3098
216k
               "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);
3099
3100
216k
        for (ctx->time_increment_bits = 1;
3101
2.88M
             ctx->time_increment_bits < 16;
3102
2.73M
             ctx->time_increment_bits++) {
3103
2.73M
            if (h->c.pict_type == AV_PICTURE_TYPE_P ||
3104
2.55M
                (h->c.pict_type == AV_PICTURE_TYPE_S &&
3105
752k
                 ctx->vol_sprite_usage == GMC_SPRITE)) {
3106
349k
                if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
3107
12.3k
                    break;
3108
2.38M
            } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
3109
55.2k
                break;
3110
2.73M
        }
3111
3112
216k
        av_log(h->c.avctx, AV_LOG_WARNING,
3113
216k
               "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits);
3114
216k
    }
3115
3116
732k
    if (IS_3IV1)
3117
0
        time_increment = get_bits1(gb);        // FIXME investigate further
3118
732k
    else
3119
732k
        time_increment = get_bits(gb, ctx->time_increment_bits);
3120
3121
732k
    if (h->c.pict_type != AV_PICTURE_TYPE_B) {
3122
649k
        h->c.last_time_base = h->c.time_base;
3123
649k
        h->c.time_base     += time_incr;
3124
649k
        h->c.time = h->c.time_base * (int64_t)h->c.avctx->framerate.num + time_increment;
3125
649k
        if (h->c.workaround_bugs & FF_BUG_UMP4) {
3126
82.2k
            if (h->c.time < h->c.last_non_b_time) {
3127
                /* header is not mpeg-4-compatible, broken encoder,
3128
                 * trying to workaround */
3129
8.15k
                h->c.time_base++;
3130
8.15k
                h->c.time += h->c.avctx->framerate.num;
3131
8.15k
            }
3132
82.2k
        }
3133
649k
        h->c.pp_time         = h->c.time - h->c.last_non_b_time;
3134
649k
        h->c.last_non_b_time = h->c.time;
3135
649k
    } else {
3136
83.0k
        h->c.time    = (h->c.last_time_base + time_incr) * (int64_t)h->c.avctx->framerate.num + time_increment;
3137
83.0k
        h->c.pb_time = h->c.pp_time - (h->c.last_non_b_time - h->c.time);
3138
83.0k
        if (h->c.pp_time <= h->c.pb_time ||
3139
43.4k
            h->c.pp_time <= h->c.pp_time - h->c.pb_time ||
3140
42.7k
            h->c.pp_time <= 0) {
3141
            /* messed up order, maybe after seeking? skipping current B-frame */
3142
42.7k
            return FRAME_SKIPPED;
3143
42.7k
        }
3144
40.2k
        ff_mpeg4_init_direct_mv(&h->c);
3145
3146
40.2k
        if (ctx->t_frame == 0)
3147
14.0k
            ctx->t_frame = h->c.pb_time;
3148
40.2k
        if (ctx->t_frame == 0)
3149
0
            ctx->t_frame = 1;  // 1/0 protection
3150
40.2k
        h->c.pp_field_time = (ROUNDED_DIV(h->c.last_non_b_time, ctx->t_frame) -
3151
40.2k
                              ROUNDED_DIV(h->c.last_non_b_time - h->c.pp_time, ctx->t_frame)) * 2;
3152
40.2k
        h->c.pb_field_time = (ROUNDED_DIV(h->c.time, ctx->t_frame) -
3153
40.2k
                              ROUNDED_DIV(h->c.last_non_b_time - h->c.pp_time, ctx->t_frame)) * 2;
3154
40.2k
        if (h->c.pp_field_time <= h->c.pb_field_time || h->c.pb_field_time <= 1) {
3155
31.1k
            h->c.pb_field_time = 2;
3156
31.1k
            h->c.pp_field_time = 4;
3157
31.1k
            if (!h->c.progressive_sequence)
3158
12.2k
                return FRAME_SKIPPED;
3159
31.1k
        }
3160
40.2k
    }
3161
3162
677k
    if (h->c.avctx->framerate.den)
3163
663k
        pts = ROUNDED_DIV(h->c.time, h->c.avctx->framerate.den);
3164
14.6k
    else
3165
14.6k
        pts = AV_NOPTS_VALUE;
3166
677k
    ff_dlog(h->c.avctx, "MPEG4 PTS: %"PRId64"\n", pts);
3167
3168
677k
    check_marker(h->c.avctx, gb, "before vop_coded");
3169
3170
    /* vop coded */
3171
677k
    if (get_bits1(gb) != 1) {
3172
124k
        if (h->c.avctx->debug & FF_DEBUG_PICT_INFO)
3173
0
            av_log(h->c.avctx, AV_LOG_ERROR, "vop not coded\n");
3174
124k
        h->skipped_last_frame = 1;
3175
124k
        return FRAME_SKIPPED;
3176
124k
    }
3177
553k
    if (ctx->new_pred)
3178
58.5k
        decode_new_pred(ctx, gb);
3179
3180
553k
    if (ctx->shape != BIN_ONLY_SHAPE &&
3181
531k
                    (h->c.pict_type == AV_PICTURE_TYPE_P ||
3182
451k
                     (h->c.pict_type == AV_PICTURE_TYPE_S &&
3183
300k
                      ctx->vol_sprite_usage == GMC_SPRITE))) {
3184
        /* rounding type for motion estimation */
3185
113k
        h->c.no_rounding = get_bits1(gb);
3186
439k
    } else {
3187
439k
        h->c.no_rounding = 0;
3188
439k
    }
3189
    // FIXME reduced res stuff
3190
3191
553k
    if (ctx->shape != RECT_SHAPE) {
3192
115k
        if (ctx->vol_sprite_usage != 1 || h->c.pict_type != AV_PICTURE_TYPE_I) {
3193
107k
            skip_bits(gb, 13);  /* width */
3194
107k
            check_marker(h->c.avctx, gb, "after width");
3195
107k
            skip_bits(gb, 13);  /* height */
3196
107k
            check_marker(h->c.avctx, gb, "after height");
3197
107k
            skip_bits(gb, 13);  /* hor_spat_ref */
3198
107k
            check_marker(h->c.avctx, gb, "after hor_spat_ref");
3199
107k
            skip_bits(gb, 13);  /* ver_spat_ref */
3200
107k
        }
3201
115k
        skip_bits1(gb);         /* change_CR_disable */
3202
3203
115k
        if (get_bits1(gb) != 0)
3204
30.6k
            skip_bits(gb, 8);   /* constant_alpha_value */
3205
115k
    }
3206
3207
    // FIXME complexity estimation stuff
3208
3209
553k
    if (ctx->shape != BIN_ONLY_SHAPE) {
3210
531k
        skip_bits_long(gb, ctx->cplx_estimation_trash_i);
3211
531k
        if (h->c.pict_type != AV_PICTURE_TYPE_I)
3212
399k
            skip_bits_long(gb, ctx->cplx_estimation_trash_p);
3213
531k
        if (h->c.pict_type == AV_PICTURE_TYPE_B)
3214
20.0k
            skip_bits_long(gb, ctx->cplx_estimation_trash_b);
3215
3216
531k
        if (get_bits_left(gb) < 3) {
3217
86.0k
            av_log(h->c.avctx, AV_LOG_ERROR, "Header truncated\n");
3218
86.0k
            return AVERROR_INVALIDDATA;
3219
86.0k
        }
3220
445k
        ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
3221
445k
        if (!h->c.progressive_sequence) {
3222
260k
            h->c.top_field_first = get_bits1(gb);
3223
260k
            h->c.alternate_scan  = get_bits1(gb);
3224
260k
        } else
3225
184k
            h->c.alternate_scan = 0;
3226
445k
    }
3227
    /* Skip at this point when only parsing since the remaining
3228
     * data is not useful for a parser and requires the
3229
     * sprite_trajectory VLC to be initialized. */
3230
467k
    if (parse_only)
3231
275k
        goto end;
3232
3233
191k
    if (h->c.alternate_scan) {
3234
33.4k
        ff_init_scantable(h->c.idsp.idct_permutation, &h->c.intra_scantable,   ff_alternate_vertical_scan);
3235
33.4k
        ff_permute_scantable(h->permutated_intra_h_scantable, ff_alternate_vertical_scan,
3236
33.4k
                             h->c.idsp.idct_permutation);
3237
158k
    } else {
3238
158k
        ff_init_scantable(h->c.idsp.idct_permutation, &h->c.intra_scantable,   ff_zigzag_direct);
3239
158k
        ff_permute_scantable(h->permutated_intra_h_scantable, ff_alternate_horizontal_scan,
3240
158k
                             h->c.idsp.idct_permutation);
3241
158k
    }
3242
191k
    ff_permute_scantable(h->permutated_intra_v_scantable, ff_alternate_vertical_scan,
3243
191k
                         h->c.idsp.idct_permutation);
3244
3245
191k
    if (h->c.pict_type == AV_PICTURE_TYPE_S) {
3246
134k
        if((ctx->vol_sprite_usage == STATIC_SPRITE ||
3247
128k
            ctx->vol_sprite_usage == GMC_SPRITE)) {
3248
22.5k
            if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
3249
2.86k
                return AVERROR_INVALIDDATA;
3250
19.6k
            if (ctx->sprite_brightness_change)
3251
7.10k
                av_log(h->c.avctx, AV_LOG_ERROR,
3252
7.10k
                    "sprite_brightness_change not supported\n");
3253
19.6k
            if (ctx->vol_sprite_usage == STATIC_SPRITE)
3254
4.87k
                av_log(h->c.avctx, AV_LOG_ERROR, "static sprite not supported\n");
3255
111k
        } else {
3256
111k
            memset(ctx->sprite_offset, 0, sizeof(ctx->sprite_offset));
3257
111k
            memset(ctx->sprite_delta,  0, sizeof(ctx->sprite_delta));
3258
111k
        }
3259
134k
    }
3260
3261
188k
    ctx->f_code = 1;
3262
188k
    ctx->b_code = 1;
3263
188k
    if (ctx->shape != BIN_ONLY_SHAPE) {
3264
182k
        h->c.chroma_qscale = h->c.qscale = get_bits(gb, ctx->quant_precision);
3265
182k
        if (h->c.qscale == 0) {
3266
5.28k
            av_log(h->c.avctx, AV_LOG_ERROR,
3267
5.28k
                   "Error, header damaged or not MPEG-4 header (qscale=0)\n");
3268
5.28k
            return AVERROR_INVALIDDATA;  // makes no sense to continue, as there is nothing left from the image then
3269
5.28k
        }
3270
3271
177k
        if (h->c.pict_type != AV_PICTURE_TYPE_I) {
3272
158k
            ctx->f_code = get_bits(gb, 3);        /* fcode_for */
3273
158k
            if (ctx->f_code == 0) {
3274
4.85k
                av_log(h->c.avctx, AV_LOG_ERROR,
3275
4.85k
                       "Error, header damaged or not MPEG-4 header (f_code=0)\n");
3276
4.85k
                ctx->f_code = 1;
3277
4.85k
                return AVERROR_INVALIDDATA;  // makes no sense to continue, as there is nothing left from the image then
3278
4.85k
            }
3279
158k
        }
3280
3281
172k
        if (h->c.pict_type == AV_PICTURE_TYPE_B) {
3282
4.44k
            ctx->b_code = get_bits(gb, 3);
3283
4.44k
            if (ctx->b_code == 0) {
3284
260
                av_log(h->c.avctx, AV_LOG_ERROR,
3285
260
                       "Error, header damaged or not MPEG4 header (b_code=0)\n");
3286
260
                ctx->b_code=1;
3287
260
                return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly
3288
260
            }
3289
4.44k
        }
3290
3291
172k
        if (h->c.avctx->debug & FF_DEBUG_PICT_INFO) {
3292
0
            av_log(h->c.avctx, AV_LOG_DEBUG,
3293
0
                   "qp:%d fc:%d,%d %c size:%d pro:%d alt:%d top:%d %cpel part:%d resync:%d w:%d a:%d rnd:%d vot:%d%s dc:%d ce:%d/%d/%d time:%"PRId64" tincr:%d\n",
3294
0
                   h->c.qscale, ctx->f_code, ctx->b_code,
3295
0
                   h->c.pict_type == AV_PICTURE_TYPE_I ? 'I' : (h->c.pict_type == AV_PICTURE_TYPE_P ? 'P' : (h->c.pict_type == AV_PICTURE_TYPE_B ? 'B' : 'S')),
3296
0
                   gb->size_in_bits,h->c.progressive_sequence, h->c.alternate_scan,
3297
0
                   h->c.top_field_first, h->c.quarter_sample ? 'q' : 'h',
3298
0
                   h->data_partitioning, ctx->resync_marker,
3299
0
                   ctx->num_sprite_warping_points, ctx->sprite_warping_accuracy,
3300
0
                   1 - h->c.no_rounding, ctx->vo_type,
3301
0
                   ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
3302
0
                   ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
3303
0
                   ctx->cplx_estimation_trash_b,
3304
0
                   h->c.time,
3305
0
                   time_increment
3306
0
                  );
3307
0
        }
3308
3309
172k
        if (!ctx->scalability) {
3310
162k
            if (ctx->shape != RECT_SHAPE && h->c.pict_type != AV_PICTURE_TYPE_I)
3311
1.08k
                skip_bits1(gb);  // vop shape coding type
3312
162k
        } else {
3313
9.43k
            if (ctx->enhancement_type) {
3314
4.67k
                int load_backward_shape = get_bits1(gb);
3315
4.67k
                if (load_backward_shape)
3316
2.00k
                    av_log(h->c.avctx, AV_LOG_ERROR,
3317
2.00k
                           "load backward shape isn't supported\n");
3318
4.67k
            }
3319
9.43k
            skip_bits(gb, 2);  // ref_select_code
3320
9.43k
        }
3321
172k
    }
3322
3323
178k
    h->c.dct_unquantize_intra = ctx->mpeg_quant ? ctx->dct_unquantize_mpeg2_intra
3324
178k
                                                : ctx->dct_unquantize_h263_intra;
3325
    // The following tells ff_mpv_reconstruct_mb() to unquantize iff mpeg_quant
3326
178k
    h->c.dct_unquantize_inter = ctx->mpeg_quant ? ctx->dct_unquantize_mpeg2_inter : NULL;
3327
3328
453k
end:
3329
    /* detect buggy encoders which don't set the low_delay flag
3330
     * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames
3331
     * easily (although it's buggy too) */
3332
453k
    if (ctx->vo_type == 0 && ctx->vol_control_parameters == 0 &&
3333
33.3k
        ctx->divx_version == -1 && h->picture_number == 0) {
3334
957
        av_log(h->c.avctx, AV_LOG_WARNING,
3335
957
               "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
3336
957
        h->c.low_delay = 1;
3337
957
    }
3338
3339
453k
    h->picture_number++;  // better than pic number==0 always ;)
3340
3341
453k
    if (h->c.workaround_bugs & FF_BUG_EDGE) {
3342
95.1k
        h->c.h_edge_pos = h->c.width;
3343
95.1k
        h->c.v_edge_pos = h->c.height;
3344
95.1k
    }
3345
453k
    return 0;
3346
178k
}
3347
3348
static void decode_smpte_tc(Mpeg4DecContext *ctx, GetBitContext *gb)
3349
180k
{
3350
180k
    AVCodecContext *const avctx = ctx->h.c.avctx;
3351
3352
180k
    skip_bits(gb, 16); /* Time_code[63..48] */
3353
180k
    check_marker(avctx, gb, "after Time_code[63..48]");
3354
180k
    skip_bits(gb, 16); /* Time_code[47..32] */
3355
180k
    check_marker(avctx, gb, "after Time_code[47..32]");
3356
180k
    skip_bits(gb, 16); /* Time_code[31..16] */
3357
180k
    check_marker(avctx, gb, "after Time_code[31..16]");
3358
180k
    skip_bits(gb, 16); /* Time_code[15..0] */
3359
180k
    check_marker(avctx, gb, "after Time_code[15..0]");
3360
180k
    skip_bits(gb, 4); /* reserved_bits */
3361
180k
}
3362
3363
/**
3364
 * Decode the next studio vop header.
3365
 * @return <0 if something went wrong
3366
 */
3367
static int decode_studio_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
3368
220k
{
3369
220k
    H263DecContext *const h = &ctx->h;
3370
3371
220k
    if (get_bits_left(gb) <= 32)
3372
40.0k
        return 0;
3373
3374
180k
    h->partitioned_frame = 0;
3375
180k
    h->c.interlaced_dct = 0;
3376
180k
    h->decode_mb = mpeg4_decode_studio_mb;
3377
3378
180k
    decode_smpte_tc(ctx, gb);
3379
3380
180k
    skip_bits(gb, 10); /* temporal_reference */
3381
180k
    skip_bits(gb, 2); /* vop_structure */
3382
180k
    h->c.pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* vop_coding_type */
3383
180k
    if (get_bits1(gb)) { /* vop_coded */
3384
55.8k
        skip_bits1(gb); /* top_field_first */
3385
55.8k
        skip_bits1(gb); /* repeat_first_field */
3386
55.8k
        h->c.progressive_frame = get_bits1(gb) ^ 1; /* progressive_frame */
3387
55.8k
    }
3388
3389
180k
    if (h->c.pict_type == AV_PICTURE_TYPE_I) {
3390
114k
        if (get_bits1(gb))
3391
42.3k
            reset_studio_dc_predictors(ctx);
3392
114k
    }
3393
3394
180k
    if (ctx->shape != BIN_ONLY_SHAPE) {
3395
179k
        h->c.alternate_scan       = get_bits1(gb);
3396
179k
        h->c.frame_pred_frame_dct = get_bits1(gb);
3397
179k
        ctx->dct_precision        = get_bits(gb, 2);
3398
179k
        h->c.intra_dc_precision   = get_bits(gb, 2);
3399
179k
        h->c.q_scale_type         = get_bits1(gb);
3400
179k
    }
3401
3402
180k
    ff_init_scantable(h->c.idsp.idct_permutation, &h->c.intra_scantable,
3403
180k
                      h->c.alternate_scan ? ff_alternate_vertical_scan : ff_zigzag_direct);
3404
3405
180k
    mpeg4_load_default_matrices(&h->c);
3406
3407
180k
    next_start_code_studio(gb);
3408
180k
    extension_and_user_data(&h->c, gb, 4);
3409
3410
180k
    return 0;
3411
220k
}
3412
3413
static int decode_studiovisualobject(Mpeg4DecContext *ctx, GetBitContext *gb)
3414
5.38k
{
3415
5.38k
    int visual_object_type;
3416
3417
5.38k
    skip_bits(gb, 4); /* visual_object_verid */
3418
5.38k
    visual_object_type = get_bits(gb, 4);
3419
5.38k
    if (visual_object_type != VOT_VIDEO_ID) {
3420
2.28k
        avpriv_request_sample(ctx->h.c.avctx, "VO type %u", visual_object_type);
3421
2.28k
        return AVERROR_PATCHWELCOME;
3422
2.28k
    }
3423
3424
3.09k
    next_start_code_studio(gb);
3425
3.09k
    extension_and_user_data(&ctx->h.c, gb, 1);
3426
3427
3.09k
    return 0;
3428
5.38k
}
3429
3430
/**
3431
 * Decode MPEG-4 headers.
3432
 *
3433
 * @param  header If set the absence of a VOP is not treated as error; otherwise, it is treated as such.
3434
 * @param  parse_only If set, things only relevant to a decoder may be skipped;
3435
 *                    furthermore, the VLC tables may be uninitialized.
3436
 * @return <0 if an error occurred
3437
 *         FRAME_SKIPPED if a not coded VOP is found
3438
 *         0 else
3439
 */
3440
int ff_mpeg4_parse_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb,
3441
                                  int header, int parse_only)
3442
1.23M
{
3443
1.23M
    MPVContext *const s = &ctx->h.c;
3444
1.23M
    unsigned startcode, v;
3445
1.23M
    int ret;
3446
1.23M
    int vol = 0;
3447
3448
    /* search next start code */
3449
1.23M
    align_get_bits(gb);
3450
3451
    // If we have not switched to studio profile than we also did not switch bps
3452
    // that means something else (like a previous instance) outside set bps which
3453
    // would be inconsistent with the correct state, thus reset it
3454
1.23M
    if (!s->studio_profile && s->avctx->bits_per_raw_sample != 8)
3455
964k
        s->avctx->bits_per_raw_sample = 0;
3456
3457
1.23M
    if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
3458
0
        skip_bits(gb, 24);
3459
0
        if (get_bits(gb, 8) == 0xF0)
3460
0
            goto end;
3461
0
    }
3462
3463
1.23M
    startcode = 0xff;
3464
81.6M
    for (;;) {
3465
81.6M
        if (get_bits_count(gb) >= gb->size_in_bits) {
3466
214k
            if (gb->size_in_bits == 8 &&
3467
155k
                (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
3468
58.3k
                av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
3469
58.3k
                return FRAME_SKIPPED;  // divx bug
3470
155k
            } else if (header && get_bits_count(gb) == gb->size_in_bits) {
3471
4.45k
                return 0; // ordinary return value for parsing of extradata
3472
4.45k
            } else
3473
151k
                return AVERROR_INVALIDDATA;  // end of stream
3474
214k
        }
3475
3476
        /* use the bits after the test */
3477
81.4M
        v = get_bits(gb, 8);
3478
81.4M
        startcode = ((startcode << 8) | v) & 0xffffffff;
3479
3480
81.4M
        if ((startcode & 0xFFFFFF00) != 0x100)
3481
79.6M
            continue;  // no startcode
3482
3483
1.80M
        if (s->avctx->debug & FF_DEBUG_STARTCODE) {
3484
0
            const char *name;
3485
0
            if (startcode <= 0x11F)
3486
0
                name = "Video Object Start";
3487
0
            else if (startcode <= 0x12F)
3488
0
                name = "Video Object Layer Start";
3489
0
            else if (startcode <= 0x13F)
3490
0
                name = "Reserved";
3491
0
            else if (startcode <= 0x15F)
3492
0
                name = "FGS bp start";
3493
0
            else if (startcode <= 0x1AF)
3494
0
                name = "Reserved";
3495
0
            else if (startcode == 0x1B0)
3496
0
                name = "Visual Object Seq Start";
3497
0
            else if (startcode == 0x1B1)
3498
0
                name = "Visual Object Seq End";
3499
0
            else if (startcode == 0x1B2)
3500
0
                name = "User Data";
3501
0
            else if (startcode == 0x1B3)
3502
0
                name = "Group of VOP start";
3503
0
            else if (startcode == 0x1B4)
3504
0
                name = "Video Session Error";
3505
0
            else if (startcode == 0x1B5)
3506
0
                name = "Visual Object Start";
3507
0
            else if (startcode == 0x1B6)
3508
0
                name = "Video Object Plane start";
3509
0
            else if (startcode == 0x1B7)
3510
0
                name = "slice start";
3511
0
            else if (startcode == 0x1B8)
3512
0
                name = "extension start";
3513
0
            else if (startcode == 0x1B9)
3514
0
                name = "fgs start";
3515
0
            else if (startcode == 0x1BA)
3516
0
                name = "FBA Object start";
3517
0
            else if (startcode == 0x1BB)
3518
0
                name = "FBA Object Plane start";
3519
0
            else if (startcode == 0x1BC)
3520
0
                name = "Mesh Object start";
3521
0
            else if (startcode == 0x1BD)
3522
0
                name = "Mesh Object Plane start";
3523
0
            else if (startcode == 0x1BE)
3524
0
                name = "Still Texture Object start";
3525
0
            else if (startcode == 0x1BF)
3526
0
                name = "Texture Spatial Layer start";
3527
0
            else if (startcode == 0x1C0)
3528
0
                name = "Texture SNR Layer start";
3529
0
            else if (startcode == 0x1C1)
3530
0
                name = "Texture Tile start";
3531
0
            else if (startcode == 0x1C2)
3532
0
                name = "Texture Shape Layer start";
3533
0
            else if (startcode == 0x1C3)
3534
0
                name = "stuffing start";
3535
0
            else if (startcode <= 0x1C5)
3536
0
                name = "Reserved";
3537
0
            else if (startcode <= 0x1FF)
3538
0
                name = "System start";
3539
0
            else
3540
0
                av_unreachable("Unexpected startcode");
3541
0
            av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X %s at %d\n",
3542
0
                   startcode, name, get_bits_count(gb));
3543
0
        }
3544
3545
1.80M
        if (startcode >= 0x120 && startcode <= 0x12F) {
3546
211k
            if (vol) {
3547
30.9k
                av_log(s->avctx, AV_LOG_WARNING, "Ignoring multiple VOL headers\n");
3548
30.9k
                continue;
3549
30.9k
            }
3550
180k
            vol++;
3551
180k
            if ((ret = decode_vol_header(ctx, gb)) < 0)
3552
49.5k
                return ret;
3553
1.59M
        } else if (startcode == USER_DATA_STARTCODE) {
3554
25.7k
            decode_user_data(ctx, gb);
3555
1.56M
        } else if (startcode == GOP_STARTCODE) {
3556
72.3k
            mpeg4_decode_gop_header(s, gb);
3557
1.49M
        } else if (startcode == VOS_STARTCODE) {
3558
10.1k
            int profile, level;
3559
10.1k
            mpeg4_decode_profile_level(s, gb, &profile, &level);
3560
10.1k
            if (profile == AV_PROFILE_MPEG4_SIMPLE_STUDIO &&
3561
2.56k
                (level > 0 && level < 9)) {
3562
921
                s->studio_profile = 1;
3563
921
                next_start_code_studio(gb);
3564
921
                extension_and_user_data(s, gb, 0);
3565
9.25k
            } else if (s->studio_profile) {
3566
1.63k
                avpriv_request_sample(s->avctx, "Mix of studio and non studio profile");
3567
1.63k
                return AVERROR_PATCHWELCOME;
3568
1.63k
            }
3569
8.54k
            s->avctx->profile = profile;
3570
8.54k
            s->avctx->level   = level;
3571
1.48M
        } else if (startcode == VISUAL_OBJ_STARTCODE) {
3572
19.9k
            if (s->studio_profile) {
3573
5.38k
                if ((ret = decode_studiovisualobject(ctx, gb)) < 0)
3574
2.28k
                    return ret;
3575
5.38k
            } else
3576
14.5k
                mpeg4_decode_visual_object(s, gb);
3577
1.46M
        } else if (startcode == VOP_STARTCODE) {
3578
967k
            break;
3579
967k
        }
3580
3581
751k
        align_get_bits(gb);
3582
751k
        startcode = 0xff;
3583
751k
    }
3584
3585
967k
end:
3586
967k
    if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
3587
0
        s->low_delay = 1;
3588
3589
967k
    if (s->studio_profile) {
3590
235k
        if (!s->avctx->bits_per_raw_sample) {
3591
14.4k
            av_log(s->avctx, AV_LOG_ERROR, "Missing VOL header\n");
3592
14.4k
            return AVERROR_INVALIDDATA;
3593
14.4k
        }
3594
220k
        return decode_studio_vop_header(ctx, gb);
3595
235k
    } else
3596
732k
        return decode_vop_header(ctx, gb, parse_only);
3597
967k
}
3598
3599
#if CONFIG_MPEG4_DECODER
3600
static av_cold void permute_quant_matrix(uint16_t matrix[64],
3601
                                         const uint8_t new_perm[64],
3602
                                         const uint8_t old_perm[64])
3603
462
{
3604
462
    uint16_t tmp[64];
3605
3606
462
    memcpy(tmp, matrix, sizeof(tmp));
3607
30.0k
    for (int i = 0; i < 64; ++i)
3608
29.5k
        matrix[new_perm[i]] = tmp[old_perm[i]];
3609
462
}
3610
3611
static av_cold void switch_to_xvid_idct(AVCodecContext *const avctx,
3612
                                        H263DecContext *const h)
3613
231
{
3614
231
    uint8_t old_permutation[64];
3615
3616
231
    memcpy(old_permutation, h->c.idsp.idct_permutation, sizeof(old_permutation));
3617
3618
231
    avctx->idct_algo = FF_IDCT_XVID;
3619
231
    ff_mpv_idct_init(&h->c);
3620
231
    ff_permute_scantable(h->permutated_intra_h_scantable,
3621
231
                         h->c.alternate_scan ? ff_alternate_vertical_scan : ff_alternate_horizontal_scan,
3622
231
                         h->c.idsp.idct_permutation);
3623
231
    ff_permute_scantable(h->permutated_intra_v_scantable, ff_alternate_vertical_scan,
3624
231
                         h->c.idsp.idct_permutation);
3625
3626
    // Normal (i.e. non-studio) MPEG-4 does not use the chroma matrices.
3627
231
    permute_quant_matrix(h->c.inter_matrix, h->c.idsp.idct_permutation, old_permutation);
3628
231
    permute_quant_matrix(h->c.intra_matrix, h->c.idsp.idct_permutation, old_permutation);
3629
231
}
3630
3631
void ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
3632
179k
{
3633
179k
    Mpeg4DecContext *ctx = avctx->priv_data;
3634
179k
    H263DecContext *const h = &ctx->h;
3635
3636
179k
    if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
3637
157k
        if (h->c.codec_tag == AV_RL32("XVID") ||
3638
157k
            h->c.codec_tag == AV_RL32("XVIX") ||
3639
157k
            h->c.codec_tag == AV_RL32("RMP4") ||
3640
157k
            h->c.codec_tag == AV_RL32("ZMP4") ||
3641
156k
            h->c.codec_tag == AV_RL32("SIPP"))
3642
1.37k
            ctx->xvid_build = 0;
3643
157k
    }
3644
3645
179k
    if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
3646
156k
        if (h->c.codec_tag == AV_RL32("DIVX") && ctx->vo_type == 0 &&
3647
424
            ctx->vol_control_parameters == 0)
3648
226
            ctx->divx_version = 400;  // divx 4
3649
3650
179k
    if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
3651
218
        ctx->divx_version =
3652
218
        ctx->divx_build   = -1;
3653
218
    }
3654
3655
179k
    if (h->c.workaround_bugs & FF_BUG_AUTODETECT) {
3656
125k
        if (h->c.codec_tag == AV_RL32("XVIX"))
3657
1.94k
            h->c.workaround_bugs |= FF_BUG_XVID_ILACE;
3658
3659
125k
        if (h->c.codec_tag == AV_RL32("UMP4"))
3660
196
            h->c.workaround_bugs |= FF_BUG_UMP4;
3661
3662
125k
        if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
3663
1.30k
            h->c.workaround_bugs |= FF_BUG_QPEL_CHROMA;
3664
3665
125k
        if (ctx->divx_version > 502 && ctx->divx_build < 1814)
3666
300
            h->c.workaround_bugs |= FF_BUG_QPEL_CHROMA2;
3667
3668
125k
        if (ctx->xvid_build <= 3U)
3669
2.44k
            h->padding_bug_score = 256 * 256 * 256 * 64;
3670
3671
125k
        if (ctx->xvid_build <= 1U)
3672
2.42k
            h->c.workaround_bugs |= FF_BUG_QPEL_CHROMA;
3673
3674
125k
        if (ctx->xvid_build <= 12U)
3675
2.45k
            h->c.workaround_bugs |= FF_BUG_EDGE;
3676
3677
125k
        if (ctx->xvid_build <= 32U)
3678
2.47k
            h->c.workaround_bugs |= FF_BUG_DC_CLIP;
3679
3680
125k
#define SET_QPEL_FUNC(postfix1, postfix2)                           \
3681
345k
    h->c.qdsp.put_        ## postfix1 = ff_put_        ## postfix2; \
3682
345k
    h->c.qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
3683
345k
    h->c.qdsp.avg_        ## postfix1 = ff_avg_        ## postfix2;
3684
3685
125k
        if (ctx->lavc_build < 4653U)
3686
3.61k
            h->c.workaround_bugs |= FF_BUG_STD_QPEL;
3687
3688
125k
        if (ctx->lavc_build < 4655U)
3689
3.61k
            h->c.workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
3690
3691
125k
        if (ctx->lavc_build < 4670U)
3692
3.61k
            h->c.workaround_bugs |= FF_BUG_EDGE;
3693
3694
125k
        if (ctx->lavc_build <= 4712U)
3695
3.61k
            h->c.workaround_bugs |= FF_BUG_DC_CLIP;
3696
3697
125k
        if ((ctx->lavc_build&0xFF) >= 100) {
3698
125k
            if (ctx->lavc_build > 3621476 && ctx->lavc_build < 3752552 &&
3699
723
               (ctx->lavc_build < 3752037 || ctx->lavc_build > 3752191) // 3.2.1+
3700
125k
            )
3701
489
                h->c.workaround_bugs |= FF_BUG_IEDGE;
3702
125k
        }
3703
3704
125k
        if (ctx->divx_version >= 0)
3705
3.67k
            h->c.workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
3706
125k
        if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
3707
0
            h->padding_bug_score = 256 * 256 * 256 * 64;
3708
3709
125k
        if (ctx->divx_version < 500U)
3710
2.03k
            h->c.workaround_bugs |= FF_BUG_EDGE;
3711
3712
125k
        if (ctx->divx_version >= 0)
3713
3.67k
            h->c.workaround_bugs |= FF_BUG_HPEL_CHROMA;
3714
125k
    }
3715
3716
179k
    if (h->c.workaround_bugs & FF_BUG_STD_QPEL) {
3717
28.7k
        SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
3718
28.7k
        SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
3719
28.7k
        SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
3720
28.7k
        SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
3721
28.7k
        SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
3722
28.7k
        SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
3723
3724
28.7k
        SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
3725
28.7k
        SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
3726
28.7k
        SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
3727
28.7k
        SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
3728
28.7k
        SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
3729
28.7k
        SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
3730
28.7k
    }
3731
3732
179k
    if (avctx->debug & FF_DEBUG_BUGS)
3733
0
        av_log(h->c.avctx, AV_LOG_DEBUG,
3734
0
               "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
3735
0
               h->c.workaround_bugs, ctx->lavc_build, ctx->xvid_build,
3736
0
               ctx->divx_version, ctx->divx_build, h->divx_packed ? "p" : "");
3737
3738
179k
    if (ctx->xvid_build >= 0 &&
3739
5.13k
        avctx->idct_algo == FF_IDCT_AUTO && !h->c.studio_profile) {
3740
231
        switch_to_xvid_idct(avctx, h);
3741
231
    }
3742
179k
}
3743
3744
static int mpeg4_decode_picture_header(H263DecContext *const h)
3745
473k
{
3746
473k
    Mpeg4DecContext *const ctx = h263_to_mpeg4(h);
3747
3748
473k
    h->skipped_last_frame = 0;
3749
3750
473k
    if (ctx->bitstream_buffer) {
3751
1.97k
        int buf_size = get_bits_left(&h->gb) / 8U;
3752
1.97k
        int bitstream_buffer_size = ctx->bitstream_buffer->size;
3753
1.97k
        const uint8_t *buf = h->gb.buffer;
3754
3755
1.97k
        if (h->divx_packed) {
3756
66.8k
            for (int i = 0; i < buf_size - 3; i++) {
3757
66.1k
                if (buf[i] == 0 && buf[i+1] == 0 && buf[i+2] == 1) {
3758
1.23k
                    if (buf[i+3] == 0xB0) {
3759
82
                        av_log(h->c.avctx, AV_LOG_WARNING, "Discarding excessive bitstream in packed xvid\n");
3760
82
                        bitstream_buffer_size = 0;
3761
82
                    }
3762
1.23k
                    break;
3763
1.23k
                }
3764
66.1k
            }
3765
1.89k
        }
3766
1.97k
        ctx->bitstream_buffer->size = 0;
3767
1.97k
        if (bitstream_buffer_size && (h->divx_packed || buf_size <= MAX_NVOP_SIZE)) {// divx 5.01+/xvid frame reorder
3768
1.19k
            int ret = init_get_bits8(&h->gb, ctx->bitstream_buffer->data,
3769
1.19k
                                     bitstream_buffer_size);
3770
1.19k
            if (ret < 0)
3771
0
                return ret;
3772
1.19k
        } else
3773
780
            av_buffer_unref(&ctx->bitstream_buffer);
3774
1.97k
    }
3775
3776
473k
    return ff_mpeg4_parse_picture_header(ctx, &h->gb, 0, 0);
3777
473k
}
3778
3779
int ff_mpeg4_frame_end(AVCodecContext *avctx, const AVPacket *pkt)
3780
152k
{
3781
152k
    Mpeg4DecContext *ctx = avctx->priv_data;
3782
152k
    H263DecContext *const h = &ctx->h;
3783
152k
    int ret;
3784
3785
152k
    av_assert1(!ctx->bitstream_buffer || !ctx->bitstream_buffer->size);
3786
3787
    /* divx 5.01+ bitstream reorder stuff */
3788
152k
    if (h->divx_packed) {
3789
5.42k
        int current_pos     = ctx->bitstream_buffer && h->gb.buffer == ctx->bitstream_buffer->data ? 0 : (get_bits_count(&h->gb) >> 3);
3790
5.42k
        int startcode_found = 0;
3791
5.42k
        uint8_t *buf = pkt->data;
3792
5.42k
        int buf_size = pkt->size;
3793
3794
5.42k
        if (buf_size - current_pos > 7) {
3795
3796
2.73k
            int i;
3797
233k
            for (i = current_pos; i < buf_size - 4; i++)
3798
3799
232k
                if (buf[i]     == 0 &&
3800
49.8k
                    buf[i + 1] == 0 &&
3801
31.5k
                    buf[i + 2] == 1 &&
3802
3.99k
                    buf[i + 3] == 0xB6) {
3803
1.71k
                    startcode_found = !(buf[i + 4] & 0x40);
3804
1.71k
                    break;
3805
1.71k
                }
3806
2.73k
        }
3807
3808
5.42k
        if (startcode_found) {
3809
1.43k
            if (!ctx->showed_packed_warning) {
3810
92
                av_log(h->c.avctx, AV_LOG_INFO, "Video uses a non-standard and "
3811
92
                       "wasteful way to store B-frames ('packed B-frames'). "
3812
92
                       "Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.\n");
3813
92
                ctx->showed_packed_warning = 1;
3814
92
            }
3815
1.43k
            ret = av_buffer_replace(&ctx->bitstream_buffer, pkt->buf);
3816
1.43k
            if (ret < 0)
3817
0
                return ret;
3818
3819
1.43k
            ctx->bitstream_buffer->data = buf + current_pos;
3820
1.43k
            ctx->bitstream_buffer->size = buf_size - current_pos;
3821
1.43k
        }
3822
5.42k
    }
3823
3824
152k
    return 0;
3825
152k
}
3826
3827
#if HAVE_THREADS
3828
static av_cold void clear_context(MpegEncContext *s)
3829
0
{
3830
0
    memset(&s->buffer_pools, 0, sizeof(s->buffer_pools));
3831
0
    memset(&s->next_pic, 0, sizeof(s->next_pic));
3832
0
    memset(&s->last_pic, 0, sizeof(s->last_pic));
3833
0
    memset(&s->cur_pic,  0, sizeof(s->cur_pic));
3834
3835
0
    memset(s->thread_context, 0, sizeof(s->thread_context));
3836
3837
0
    s->ac_val_base = NULL;
3838
0
    s->ac_val = NULL;
3839
0
    memset(&s->sc, 0, sizeof(s->sc));
3840
3841
0
    s->p_field_mv_table_base = NULL;
3842
0
    for (int i = 0; i < 2; i++)
3843
0
        for (int j = 0; j < 2; j++)
3844
0
            s->p_field_mv_table[i][j] = NULL;
3845
3846
0
    s->dc_val_base = NULL;
3847
0
    s->coded_block_base = NULL;
3848
0
    s->mbintra_table = NULL;
3849
0
    s->cbp_table = NULL;
3850
0
    s->pred_dir_table = NULL;
3851
3852
0
    s->mbskip_table = NULL;
3853
3854
0
    s->er.error_status_table = NULL;
3855
0
    s->er.er_temp_buffer = NULL;
3856
0
    s->mb_index2xy = NULL;
3857
3858
0
    s->context_initialized   = 0;
3859
0
    s->context_reinit        = 0;
3860
0
}
3861
3862
static av_cold int update_mpvctx(MpegEncContext *s, const MpegEncContext *s1)
3863
0
{
3864
0
    AVCodecContext *avctx = s->avctx;
3865
    // FIXME the following leads to a data race; instead copy only
3866
    // the necessary fields.
3867
0
    memcpy(s, s1, sizeof(*s));
3868
0
    clear_context(s);
3869
3870
0
    s->avctx = avctx;
3871
3872
0
    if (s1->context_initialized) {
3873
0
        int err = ff_mpv_common_init(s);
3874
0
        if (err < 0)
3875
0
            return err;
3876
0
    }
3877
0
    return 0;
3878
0
}
3879
3880
static int mpeg4_update_thread_context(AVCodecContext *dst,
3881
                                       const AVCodecContext *src)
3882
0
{
3883
0
    Mpeg4DecContext *s = dst->priv_data;
3884
0
    const Mpeg4DecContext *s1 = src->priv_data;
3885
0
    int init = s->h.c.context_initialized;
3886
0
    int ret;
3887
3888
0
    if (!init) {
3889
0
        ret = update_mpvctx(&s->h.c, &s1->h.c);
3890
0
        if (ret < 0)
3891
0
            return ret;
3892
0
    }
3893
3894
0
    ret = ff_mpeg_update_thread_context(dst, src);
3895
0
    if (ret < 0)
3896
0
        return ret;
3897
3898
    // copy all the necessary fields explicitly
3899
0
    s->time_increment_bits       = s1->time_increment_bits;
3900
0
    s->shape                     = s1->shape;
3901
0
    s->vol_sprite_usage          = s1->vol_sprite_usage;
3902
0
    s->sprite_brightness_change  = s1->sprite_brightness_change;
3903
0
    s->sprite_warping_accuracy   = s1->sprite_warping_accuracy;
3904
0
    s->num_sprite_warping_points = s1->num_sprite_warping_points;
3905
0
    s->h.data_partitioning       = s1->h.data_partitioning;
3906
0
    s->mpeg_quant                = s1->mpeg_quant;
3907
0
    s->rvlc                      = s1->rvlc;
3908
0
    s->resync_marker             = s1->resync_marker;
3909
0
    s->t_frame                   = s1->t_frame;
3910
0
    s->new_pred                  = s1->new_pred;
3911
0
    s->enhancement_type          = s1->enhancement_type;
3912
0
    s->scalability               = s1->scalability;
3913
0
    s->intra_dc_threshold        = s1->intra_dc_threshold;
3914
0
    s->h.divx_packed             = s1->h.divx_packed;
3915
0
    s->divx_version              = s1->divx_version;
3916
0
    s->divx_build                = s1->divx_build;
3917
0
    s->xvid_build                = s1->xvid_build;
3918
0
    s->lavc_build                = s1->lavc_build;
3919
0
    s->vo_type                   = s1->vo_type;
3920
0
    s->showed_packed_warning     = s1->showed_packed_warning;
3921
0
    s->vol_control_parameters    = s1->vol_control_parameters;
3922
0
    s->cplx_estimation_trash_i   = s1->cplx_estimation_trash_i;
3923
0
    s->cplx_estimation_trash_p   = s1->cplx_estimation_trash_p;
3924
0
    s->cplx_estimation_trash_b   = s1->cplx_estimation_trash_b;
3925
0
    s->rgb                       = s1->rgb;
3926
3927
0
    s->h.skipped_last_frame      = s1->h.skipped_last_frame;
3928
0
    s->h.padding_bug_score       = s1->h.padding_bug_score; // FIXME: racy
3929
3930
0
    s->h.picture_number          = s1->h.picture_number;
3931
3932
0
    memcpy(s->sprite_shift, s1->sprite_shift, sizeof(s1->sprite_shift));
3933
0
    memcpy(s->sprite_traj,  s1->sprite_traj,  sizeof(s1->sprite_traj));
3934
3935
0
    return av_buffer_replace(&s->bitstream_buffer, s1->bitstream_buffer);
3936
0
}
3937
3938
static int mpeg4_update_thread_context_for_user(AVCodecContext *dst,
3939
                                                const AVCodecContext *src)
3940
0
{
3941
0
    H263DecContext *const h = dst->priv_data;
3942
0
    const H263DecContext *const h1 = src->priv_data;
3943
3944
0
    h->c.quarter_sample = h1->c.quarter_sample;
3945
0
    h->divx_packed      = h1->divx_packed;
3946
3947
0
    return 0;
3948
0
}
3949
#endif
3950
3951
static av_cold void mpeg4_init_static(void)
3952
1
{
3953
1
    static VLCElem vlc_buf[6498];
3954
1
    VLCInitState state = VLC_INIT_STATE(vlc_buf);
3955
3956
1
    VLC_INIT_STATIC_TABLE_FROM_LENGTHS(studio_luma_dc, STUDIO_INTRA_BITS, 19,
3957
1
                                       &ff_mpeg4_studio_dc_luma[0][1], 2,
3958
1
                                       &ff_mpeg4_studio_dc_luma[0][0], 2, 1,
3959
1
                                       0, 0);
3960
3961
1
    VLC_INIT_STATIC_TABLE_FROM_LENGTHS(studio_chroma_dc, STUDIO_INTRA_BITS, 19,
3962
1
                                       &ff_mpeg4_studio_dc_chroma[0][1], 2,
3963
1
                                       &ff_mpeg4_studio_dc_chroma[0][0], 2, 1,
3964
1
                                       0, 0);
3965
3966
13
    for (unsigned i = 0; i < 12; i++) {
3967
12
        studio_intra_tab[i] =
3968
12
            ff_vlc_init_tables_from_lengths(&state, STUDIO_INTRA_BITS, 24,
3969
12
                                            &ff_mpeg4_studio_intra[i][0][1], 2,
3970
12
                                            &ff_mpeg4_studio_intra[i][0][0], 2, 1,
3971
12
                                            0, 0);
3972
12
    }
3973
3974
1
    static uint8_t mpeg4_rl_intra_table[2][2 * MAX_RUN + MAX_LEVEL + 3];
3975
1
    ff_rl_init(&ff_mpeg4_rl_intra, mpeg4_rl_intra_table);
3976
1
    ff_h263_init_rl_inter();
3977
3978
1
    INIT_FIRST_VLC_RL(ff_mpeg4_rl_intra, 554);
3979
1
    VLC_INIT_RL(ff_rvlc_rl_inter, 1072);
3980
1
    INIT_FIRST_VLC_RL(ff_rvlc_rl_intra, 1072);
3981
1
    VLC_INIT_STATIC_TABLE(dc_lum, DC_VLC_BITS, 10 /* 13 */,
3982
1
                          &ff_mpeg4_DCtab_lum[0][1], 2, 1,
3983
1
                          &ff_mpeg4_DCtab_lum[0][0], 2, 1, 0);
3984
1
    VLC_INIT_STATIC_TABLE(dc_chrom, DC_VLC_BITS, 10 /* 13 */,
3985
1
                          &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
3986
1
                          &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 0);
3987
1
    VLC_INIT_STATIC_TABLE_FROM_LENGTHS(sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
3988
1
                                       ff_sprite_trajectory_lens, 1,
3989
1
                                       NULL, 0, 0, 0, 0);
3990
1
    VLC_INIT_STATIC_SPARSE_TABLE(mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
3991
1
                                 &ff_mb_type_b_tab[0][1], 2, 1,
3992
1
                                 &ff_mb_type_b_tab[0][0], 2, 1,
3993
1
                                 mb_type_b_map, 2, 2, 0);
3994
1
}
3995
3996
static av_cold int decode_init(AVCodecContext *avctx)
3997
11.3k
{
3998
11.3k
    static AVOnce init_static_once = AV_ONCE_INIT;
3999
11.3k
    Mpeg4DecContext *ctx = avctx->priv_data;
4000
11.3k
    H263DecContext *const h = &ctx->h;
4001
11.3k
    MPVUnquantDSPContext unquant_dsp_ctx;
4002
11.3k
    int ret;
4003
4004
11.3k
    ctx->divx_version =
4005
11.3k
    ctx->divx_build   =
4006
11.3k
    ctx->xvid_build   =
4007
11.3k
    ctx->lavc_build   = -1;
4008
4009
11.3k
    if ((ret = ff_h263_decode_init(avctx)) < 0)
4010
0
        return ret;
4011
4012
11.3k
    ff_mpv_unquantize_init(&unquant_dsp_ctx,
4013
11.3k
                           avctx->flags & AV_CODEC_FLAG_BITEXACT, 0);
4014
4015
11.3k
    ctx->dct_unquantize_h263_intra  = unquant_dsp_ctx.dct_unquantize_h263_intra;
4016
11.3k
    ctx->dct_unquantize_mpeg2_intra = unquant_dsp_ctx.dct_unquantize_mpeg2_intra;
4017
    // dct_unquantize_inter is only used with MPEG-2 quantizers,
4018
    // so that is all we keep.
4019
11.3k
    ctx->dct_unquantize_mpeg2_inter = unquant_dsp_ctx.dct_unquantize_mpeg2_inter;
4020
4021
11.3k
    h->c.y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
4022
11.3k
    h->c.c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
4023
4024
11.3k
    h->c.h263_pred = 1;
4025
11.3k
    h->c.low_delay = 0; /* default, might be overridden in the vol header during header parsing */
4026
11.3k
    h->decode_header = mpeg4_decode_picture_header;
4027
11.3k
    h->decode_mb   = mpeg4_decode_mb;
4028
11.3k
    ctx->time_increment_bits = 4; /* default value for broken headers */
4029
11.3k
    ctx->quant_precision     = 5;
4030
4031
11.3k
    avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
4032
4033
11.3k
    ff_qpeldsp_init(&h->c.qdsp);
4034
11.3k
    ff_mpeg4videodsp_init(&ctx->mdsp);
4035
4036
11.3k
    ff_thread_once(&init_static_once, mpeg4_init_static);
4037
4038
    /* Must be after initializing the MPEG-4 static tables */
4039
11.3k
    if (avctx->extradata_size && !avctx->internal->is_copy) {
4040
187
        GetBitContext gb;
4041
4042
187
        if (init_get_bits8(&gb, avctx->extradata, avctx->extradata_size) >= 0)
4043
187
            ff_mpeg4_parse_picture_header(ctx, &gb, 1, 0);
4044
187
    }
4045
4046
11.3k
    return 0;
4047
11.3k
}
4048
4049
static av_cold void mpeg4_flush(AVCodecContext *avctx)
4050
144k
{
4051
144k
    Mpeg4DecContext *const ctx = avctx->priv_data;
4052
4053
144k
    av_buffer_unref(&ctx->bitstream_buffer);
4054
144k
    ff_mpeg_flush(avctx);
4055
144k
}
4056
4057
static av_cold int mpeg4_close(AVCodecContext *avctx)
4058
11.3k
{
4059
11.3k
    Mpeg4DecContext *const ctx = avctx->priv_data;
4060
4061
11.3k
    av_buffer_unref(&ctx->bitstream_buffer);
4062
4063
11.3k
    return ff_mpv_decode_close(avctx);
4064
11.3k
}
4065
4066
#define OFFSET(x) offsetof(H263DecContext, x)
4067
#define FLAGS AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY
4068
static const AVOption mpeg4_options[] = {
4069
    {"quarter_sample", "1/4 subpel MC", OFFSET(c.quarter_sample), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS},
4070
    {"divx_packed", "divx style packed b frames", OFFSET(divx_packed), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS},
4071
    {NULL}
4072
};
4073
4074
static const AVClass mpeg4_class = {
4075
    .class_name = "MPEG4 Video Decoder",
4076
    .item_name  = av_default_item_name,
4077
    .option     = mpeg4_options,
4078
    .version    = LIBAVUTIL_VERSION_INT,
4079
};
4080
4081
const FFCodec ff_mpeg4_decoder = {
4082
    .p.name                = "mpeg4",
4083
    CODEC_LONG_NAME("MPEG-4 part 2"),
4084
    .p.type                = AVMEDIA_TYPE_VIDEO,
4085
    .p.id                  = AV_CODEC_ID_MPEG4,
4086
    .priv_data_size        = sizeof(Mpeg4DecContext),
4087
    .init                  = decode_init,
4088
    FF_CODEC_DECODE_CB(ff_h263_decode_frame),
4089
    .close                 = mpeg4_close,
4090
    .p.capabilities        = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
4091
                             AV_CODEC_CAP_DELAY | AV_CODEC_CAP_FRAME_THREADS,
4092
    .caps_internal         = FF_CODEC_CAP_INIT_CLEANUP |
4093
                             FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
4094
    .flush                 = mpeg4_flush,
4095
    .p.max_lowres          = 3,
4096
    .p.profiles            = NULL_IF_CONFIG_SMALL(ff_mpeg4_video_profiles),
4097
    UPDATE_THREAD_CONTEXT(mpeg4_update_thread_context),
4098
    UPDATE_THREAD_CONTEXT_FOR_USER(mpeg4_update_thread_context_for_user),
4099
    .p.priv_class = &mpeg4_class,
4100
    .hw_configs            = (const AVCodecHWConfigInternal *const []) {
4101
#if CONFIG_MPEG4_NVDEC_HWACCEL
4102
                               HWACCEL_NVDEC(mpeg4),
4103
#endif
4104
#if CONFIG_MPEG4_VAAPI_HWACCEL
4105
                               HWACCEL_VAAPI(mpeg4),
4106
#endif
4107
#if CONFIG_MPEG4_VDPAU_HWACCEL
4108
                               HWACCEL_VDPAU(mpeg4),
4109
#endif
4110
#if CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
4111
                               HWACCEL_VIDEOTOOLBOX(mpeg4),
4112
#endif
4113
                               NULL
4114
                           },
4115
};
4116
#endif /* CONFIG_MPEG4_DECODER */