GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/mpeg4videodec.c Lines: 1016 2146 47.3 %
Date: 2021-04-20 04:37:23 Branches: 610 1449 42.1 %

Line Branch Exec 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 "libavutil/internal.h"
26
#include "libavutil/opt.h"
27
#include "libavutil/pixdesc.h"
28
#include "libavutil/thread.h"
29
#include "error_resilience.h"
30
#include "hwconfig.h"
31
#include "idctdsp.h"
32
#include "internal.h"
33
#include "mpegutils.h"
34
#include "mpegvideo.h"
35
#include "mpegvideodata.h"
36
#include "mpeg4video.h"
37
#include "h263.h"
38
#include "profiles.h"
39
#include "thread.h"
40
#include "xvididct.h"
41
#include "unary.h"
42
43
/* The defines below define the number of bits that are read at once for
44
 * reading vlc values. Changing these may improve speed and data cache needs
45
 * be aware though that decreasing them may need the number of stages that is
46
 * passed to get_vlc* to be increased. */
47
#define SPRITE_TRAJ_VLC_BITS 6
48
#define DC_VLC_BITS 9
49
#define MB_TYPE_B_VLC_BITS 4
50
#define STUDIO_INTRA_BITS 9
51
52
static VLC dc_lum, dc_chrom;
53
static VLC sprite_trajectory;
54
static VLC mb_type_b_vlc;
55
static VLC studio_intra_tab[12];
56
static VLC studio_luma_dc;
57
static VLC studio_chroma_dc;
58
59
static const int mb_type_b_map[4] = {
60
    MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
61
    MB_TYPE_L0L1    | MB_TYPE_16x16,
62
    MB_TYPE_L1      | MB_TYPE_16x16,
63
    MB_TYPE_L0      | MB_TYPE_16x16,
64
};
65
66
/**
67
 * Predict the ac.
68
 * @param n block index (0-3 are luma, 4-5 are chroma)
69
 * @param dir the ac prediction direction
70
 */
71
1299666
void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
72
{
73
    int i;
74
    int16_t *ac_val, *ac_val1;
75
1299666
    int8_t *const qscale_table = s->current_picture.qscale_table;
76
77
    /* find prediction */
78
1299666
    ac_val  = &s->ac_val[0][0][0] + s->block_index[n] * 16;
79
1299666
    ac_val1 = ac_val;
80
1299666
    if (s->ac_pred) {
81
30084
        if (dir == 0) {
82
20713
            const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
83
            /* left prediction */
84
20713
            ac_val -= 16;
85
86

20713
            if (s->mb_x == 0 || s->qscale == qscale_table[xy] ||
87
                n == 1 || n == 3) {
88
                /* same qscale */
89
165704
                for (i = 1; i < 8; i++)
90
144991
                    block[s->idsp.idct_permutation[i << 3]] += ac_val[i];
91
            } else {
92
                /* different qscale, we must rescale */
93
                for (i = 1; i < 8; i++)
94
                    block[s->idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
95
            }
96
        } else {
97
9371
            const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
98
            /* top prediction */
99
9371
            ac_val -= 16 * s->block_wrap[n];
100
101

9371
            if (s->mb_y == 0 || s->qscale == qscale_table[xy] ||
102
2
                n == 2 || n == 3) {
103
                /* same qscale */
104
74968
                for (i = 1; i < 8; i++)
105
65597
                    block[s->idsp.idct_permutation[i]] += ac_val[i + 8];
106
            } else {
107
                /* different qscale, we must rescale */
108
                for (i = 1; i < 8; i++)
109
                    block[s->idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
110
            }
111
        }
112
    }
113
    /* left copy */
114
10397328
    for (i = 1; i < 8; i++)
115
9097662
        ac_val1[i] = block[s->idsp.idct_permutation[i << 3]];
116
117
    /* top copy */
118
10397328
    for (i = 1; i < 8; i++)
119
9097662
        ac_val1[8 + i] = block[s->idsp.idct_permutation[i]];
120
1299666
}
121
122
/**
123
 * check if the next stuff is a resync marker or the end.
124
 * @return 0 if not
125
 */
126
1427120
static inline int mpeg4_is_resync(Mpeg4DecContext *ctx)
127
{
128
1427120
    MpegEncContext *s = &ctx->m;
129
1427120
    int bits_count = get_bits_count(&s->gb);
130
1427120
    int v          = show_bits(&s->gb, 16);
131
132

1427120
    if (s->workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker)
133
        return 0;
134
135
1427120
    while (v <= 0xFF) {
136
3299
        if (s->pict_type == AV_PICTURE_TYPE_B ||
137

2300
            (v >> (8 - s->pict_type) != 1) || s->partitioned_frame)
138
            break;
139
        skip_bits(&s->gb, 8 + s->pict_type);
140
        bits_count += 8 + s->pict_type;
141
        v = show_bits(&s->gb, 16);
142
    }
143
144
1427120
    if (bits_count + 8 >= s->gb.size_in_bits) {
145
11669
        v >>= 8;
146
11669
        v  |= 0x7F >> (7 - (bits_count & 7));
147
148
11669
        if (v == 0x7F)
149
11352
            return s->mb_num;
150
    } else {
151
1415451
        if (v == ff_mpeg4_resync_prefix[bits_count & 7]) {
152
            int len, mb_num;
153
4098
            int mb_num_bits = av_log2(s->mb_num - 1) + 1;
154
4098
            GetBitContext gb = s->gb;
155
156
4098
            skip_bits(&s->gb, 1);
157
4098
            align_get_bits(&s->gb);
158
159
72133
            for (len = 0; len < 32; len++)
160
72133
                if (get_bits1(&s->gb))
161
4098
                    break;
162
163
4098
            mb_num = get_bits(&s->gb, mb_num_bits);
164

4098
            if (!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
165
                mb_num= -1;
166
167
4098
            s->gb = gb;
168
169
4098
            if (len >= ff_mpeg4_get_video_packet_prefix_length(s))
170
4096
                return mb_num;
171
        }
172
    }
173
1411672
    return 0;
174
}
175
176
static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
177
{
178
    MpegEncContext *s = &ctx->m;
179
    int a     = 2 << s->sprite_warping_accuracy;
180
    int rho   = 3  - s->sprite_warping_accuracy;
181
    int r     = 16 / a;
182
    int alpha = 1;
183
    int beta  = 0;
184
    int w     = s->width;
185
    int h     = s->height;
186
    int min_ab, i, w2, h2, w3, h3;
187
    int sprite_ref[4][2];
188
    int virtual_ref[2][2];
189
    int64_t sprite_offset[2][2];
190
    int64_t sprite_delta[2][2];
191
192
    // only true for rectangle shapes
193
    const int vop_ref[4][2] = { { 0, 0 },         { s->width, 0 },
194
                                { 0, s->height }, { s->width, s->height } };
195
    int d[4][2]             = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
196
197
    if (w <= 0 || h <= 0)
198
        return AVERROR_INVALIDDATA;
199
200
    for (i = 0; i < ctx->num_sprite_warping_points; i++) {
201
        int length;
202
        int x = 0, y = 0;
203
204
        length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 2);
205
        if (length > 0)
206
            x = get_xbits(gb, length);
207
208
        if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
209
            check_marker(s->avctx, gb, "before sprite_trajectory");
210
211
        length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 2);
212
        if (length > 0)
213
            y = get_xbits(gb, length);
214
215
        check_marker(s->avctx, gb, "after sprite_trajectory");
216
        ctx->sprite_traj[i][0] = d[i][0] = x;
217
        ctx->sprite_traj[i][1] = d[i][1] = y;
218
    }
219
    for (; i < 4; i++)
220
        ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0;
221
222
    while ((1 << alpha) < w)
223
        alpha++;
224
    while ((1 << beta) < h)
225
        beta++;  /* typo in the MPEG-4 std for the definition of w' and h' */
226
    w2 = 1 << alpha;
227
    h2 = 1 << beta;
228
229
    // Note, the 4th point isn't used for GMC
230
    if (ctx->divx_version == 500 && ctx->divx_build == 413) {
231
        sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
232
        sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
233
        sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
234
        sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1];
235
        sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0];
236
        sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1];
237
    } else {
238
        sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]);
239
        sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]);
240
        sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]);
241
        sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]);
242
        sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]);
243
        sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]);
244
    }
245
    /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
246
     * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
247
248
    /* This is mostly identical to the MPEG-4 std (and is totally unreadable
249
     * because of that...). Perhaps it should be reordered to be more readable.
250
     * The idea behind this virtual_ref mess is to be able to use shifts later
251
     * per pixel instead of divides so the distance between points is converted
252
     * from w&h based to w2&h2 based which are of the 2^x form. */
253
    virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) +
254
                         ROUNDED_DIV(((w - w2) *
255
                                           (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
256
                                      w2 * (r * sprite_ref[1][0] - 16LL * vop_ref[1][0])), w);
257
    virtual_ref[0][1] = 16 * vop_ref[0][1] +
258
                        ROUNDED_DIV(((w - w2) *
259
                                          (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
260
                                     w2 * (r * sprite_ref[1][1] - 16LL * vop_ref[1][1])), w);
261
    virtual_ref[1][0] = 16 * vop_ref[0][0] +
262
                        ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
263
                                           h2 * (r * sprite_ref[2][0] - 16LL * vop_ref[2][0])), h);
264
    virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) +
265
                        ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
266
                                           h2 * (r * sprite_ref[2][1] - 16LL * vop_ref[2][1])), h);
267
268
    switch (ctx->num_sprite_warping_points) {
269
    case 0:
270
        sprite_offset[0][0]    =
271
        sprite_offset[0][1]    =
272
        sprite_offset[1][0]    =
273
        sprite_offset[1][1]    = 0;
274
        sprite_delta[0][0]     = a;
275
        sprite_delta[0][1]     =
276
        sprite_delta[1][0]     = 0;
277
        sprite_delta[1][1]     = a;
278
        ctx->sprite_shift[0]   =
279
        ctx->sprite_shift[1]   = 0;
280
        break;
281
    case 1:     // GMC only
282
        sprite_offset[0][0]    = sprite_ref[0][0] - a * vop_ref[0][0];
283
        sprite_offset[0][1]    = sprite_ref[0][1] - a * vop_ref[0][1];
284
        sprite_offset[1][0]    = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) -
285
                                 a * (vop_ref[0][0] / 2);
286
        sprite_offset[1][1]    = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) -
287
                                 a * (vop_ref[0][1] / 2);
288
        sprite_delta[0][0]     = a;
289
        sprite_delta[0][1]     =
290
        sprite_delta[1][0]     = 0;
291
        sprite_delta[1][1]     = a;
292
        ctx->sprite_shift[0]   =
293
        ctx->sprite_shift[1]   = 0;
294
        break;
295
    case 2:
296
        sprite_offset[0][0]    = ((int64_t)      sprite_ref[0][0] * (1 << alpha + rho)) +
297
                                 ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
298
                                 ((int64_t)        -vop_ref[0][0]) +
299
                                 ((int64_t)  r * sprite_ref[0][1] - virtual_ref[0][1]) *
300
                                 ((int64_t)        -vop_ref[0][1]) + (1 << (alpha + rho - 1));
301
        sprite_offset[0][1]    = ((int64_t)      sprite_ref[0][1] * (1 << alpha + rho)) +
302
                                 ((int64_t) -r * sprite_ref[0][1] + virtual_ref[0][1]) *
303
                                 ((int64_t)        -vop_ref[0][0]) +
304
                                 ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
305
                                 ((int64_t)        -vop_ref[0][1]) + (1 << (alpha + rho - 1));
306
        sprite_offset[1][0]    = (((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
307
                                  ((int64_t)-2 *    vop_ref[0][0] + 1) +
308
                                  ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
309
                                  ((int64_t)-2 *    vop_ref[0][1] + 1) + 2 * w2 * r *
310
                                   (int64_t)     sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1)));
311
        sprite_offset[1][1]    = (((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) *
312
                                  ((int64_t)-2 *    vop_ref[0][0] + 1) +
313
                                  ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
314
                                  ((int64_t)-2 *    vop_ref[0][1] + 1) + 2 * w2 * r *
315
                                  (int64_t)      sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1)));
316
        sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
317
        sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]);
318
        sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
319
        sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
320
321
        ctx->sprite_shift[0]  = alpha + rho;
322
        ctx->sprite_shift[1]  = alpha + rho + 2;
323
        break;
324
    case 3:
325
        min_ab = FFMIN(alpha, beta);
326
        w3     = w2 >> min_ab;
327
        h3     = h2 >> min_ab;
328
        sprite_offset[0][0]    = ((int64_t)sprite_ref[0][0] * (1 << (alpha + beta + rho - min_ab))) +
329
                                 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-vop_ref[0][0]) +
330
                                 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-vop_ref[0][1]) +
331
                                 ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
332
        sprite_offset[0][1]    = ((int64_t)sprite_ref[0][1] * (1 << (alpha + beta + rho - min_ab))) +
333
                                 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-vop_ref[0][0]) +
334
                                 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-vop_ref[0][1]) +
335
                                 ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
336
        sprite_offset[1][0]    = ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-2 * vop_ref[0][0] + 1) +
337
                                 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-2 * vop_ref[0][1] + 1) +
338
                                  (int64_t)2 * w2 * h3 * r * sprite_ref[0][0] - 16 * w2 * h3 +
339
                                 ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
340
        sprite_offset[1][1]    = ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-2 * vop_ref[0][0] + 1) +
341
                                 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-2 * vop_ref[0][1] + 1) +
342
                                  (int64_t)2 * w2 * h3 * r * sprite_ref[0][1] - 16 * w2 * h3 +
343
                                 ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
344
        sprite_delta[0][0] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[0][0]) * h3;
345
        sprite_delta[0][1] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[1][0]) * w3;
346
        sprite_delta[1][0] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[0][1]) * h3;
347
        sprite_delta[1][1] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[1][1]) * w3;
348
349
        ctx->sprite_shift[0]  = alpha + beta + rho - min_ab;
350
        ctx->sprite_shift[1]  = alpha + beta + rho - min_ab + 2;
351
        break;
352
    }
353
    /* try to simplify the situation */
354
    if (sprite_delta[0][0] == a << ctx->sprite_shift[0] &&
355
        sprite_delta[0][1] == 0 &&
356
        sprite_delta[1][0] == 0 &&
357
        sprite_delta[1][1] == a << ctx->sprite_shift[0]) {
358
        sprite_offset[0][0] >>= ctx->sprite_shift[0];
359
        sprite_offset[0][1] >>= ctx->sprite_shift[0];
360
        sprite_offset[1][0] >>= ctx->sprite_shift[1];
361
        sprite_offset[1][1] >>= ctx->sprite_shift[1];
362
        sprite_delta[0][0] = a;
363
        sprite_delta[0][1] = 0;
364
        sprite_delta[1][0] = 0;
365
        sprite_delta[1][1] = a;
366
        ctx->sprite_shift[0] = 0;
367
        ctx->sprite_shift[1] = 0;
368
        s->real_sprite_warping_points = 1;
369
    } else {
370
        int shift_y = 16 - ctx->sprite_shift[0];
371
        int shift_c = 16 - ctx->sprite_shift[1];
372
373
        for (i = 0; i < 2; i++) {
374
            if (shift_c < 0 || shift_y < 0 ||
375
                FFABS(  sprite_offset[0][i]) >= INT_MAX >> shift_y  ||
376
                FFABS(  sprite_offset[1][i]) >= INT_MAX >> shift_c  ||
377
                FFABS(   sprite_delta[0][i]) >= INT_MAX >> shift_y  ||
378
                FFABS(   sprite_delta[1][i]) >= INT_MAX >> shift_y
379
            ) {
380
                avpriv_request_sample(s->avctx, "Too large sprite shift, delta or offset");
381
                goto overflow;
382
            }
383
        }
384
385
        for (i = 0; i < 2; i++) {
386
            sprite_offset[0][i]    *= 1 << shift_y;
387
            sprite_offset[1][i]    *= 1 << shift_c;
388
            sprite_delta[0][i]     *= 1 << shift_y;
389
            sprite_delta[1][i]     *= 1 << shift_y;
390
            ctx->sprite_shift[i]     = 16;
391
392
        }
393
        for (i = 0; i < 2; i++) {
394
            int64_t sd[2] = {
395
                sprite_delta[i][0] - a * (1LL<<16),
396
                sprite_delta[i][1] - a * (1LL<<16)
397
            };
398
399
            if (llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
400
                llabs(sprite_offset[0][i] + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
401
                llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL) + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
402
                llabs(sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
403
                llabs(sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
404
                llabs(sd[0]) >= INT_MAX ||
405
                llabs(sd[1]) >= INT_MAX ||
406
                llabs(sprite_offset[0][i] + sd[0] * (w+16LL)) >= INT_MAX ||
407
                llabs(sprite_offset[0][i] + sd[1] * (h+16LL)) >= INT_MAX ||
408
                llabs(sprite_offset[0][i] + sd[0] * (w+16LL) + sd[1] * (h+16LL)) >= INT_MAX
409
            ) {
410
                avpriv_request_sample(s->avctx, "Overflow on sprite points");
411
                goto overflow;
412
            }
413
        }
414
        s->real_sprite_warping_points = ctx->num_sprite_warping_points;
415
    }
416
417
    for (i = 0; i < 4; i++) {
418
        s->sprite_offset[i&1][i>>1] = sprite_offset[i&1][i>>1];
419
        s->sprite_delta [i&1][i>>1] = sprite_delta [i&1][i>>1];
420
    }
421
422
    return 0;
423
overflow:
424
    memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
425
    memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
426
    return AVERROR_PATCHWELCOME;
427
}
428
429
static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb) {
430
    MpegEncContext *s = &ctx->m;
431
    int len = FFMIN(ctx->time_increment_bits + 3, 15);
432
433
    get_bits(gb, len);
434
    if (get_bits1(gb))
435
        get_bits(gb, len);
436
    check_marker(s->avctx, gb, "after new_pred");
437
438
    return 0;
439
}
440
441
/**
442
 * Decode the next video packet.
443
 * @return <0 if something went wrong
444
 */
445
3867
int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
446
{
447
3867
    MpegEncContext *s = &ctx->m;
448
449
3867
    int mb_num_bits      = av_log2(s->mb_num - 1) + 1;
450
3867
    int header_extension = 0, mb_num, len;
451
452
    /* is there enough space left for a video packet + header */
453
3867
    if (get_bits_count(&s->gb) > s->gb.size_in_bits - 20)
454
        return AVERROR_INVALIDDATA;
455
456
68178
    for (len = 0; len < 32; len++)
457
68178
        if (get_bits1(&s->gb))
458
3867
            break;
459
460
3867
    if (len != ff_mpeg4_get_video_packet_prefix_length(s)) {
461
        av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
462
        return AVERROR_INVALIDDATA;
463
    }
464
465
3867
    if (ctx->shape != RECT_SHAPE) {
466
        header_extension = get_bits1(&s->gb);
467
        // FIXME more stuff here
468
    }
469
470
3867
    mb_num = get_bits(&s->gb, mb_num_bits);
471

3867
    if (mb_num >= s->mb_num || !mb_num) {
472
        av_log(s->avctx, AV_LOG_ERROR,
473
               "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
474
        return AVERROR_INVALIDDATA;
475
    }
476
477
3867
    s->mb_x = mb_num % s->mb_width;
478
3867
    s->mb_y = mb_num / s->mb_width;
479
480
3867
    if (ctx->shape != BIN_ONLY_SHAPE) {
481
3867
        int qscale = get_bits(&s->gb, s->quant_precision);
482
3867
        if (qscale)
483
3867
            s->chroma_qscale = s->qscale = qscale;
484
    }
485
486
3867
    if (ctx->shape == RECT_SHAPE)
487
3867
        header_extension = get_bits1(&s->gb);
488
489
3867
    if (header_extension) {
490
        int time_incr = 0;
491
492
        while (get_bits1(&s->gb) != 0)
493
            time_incr++;
494
495
        check_marker(s->avctx, &s->gb, "before time_increment in video packed header");
496
        skip_bits(&s->gb, ctx->time_increment_bits);      /* time_increment */
497
        check_marker(s->avctx, &s->gb, "before vop_coding_type in video packed header");
498
499
        skip_bits(&s->gb, 2); /* vop coding type */
500
        // FIXME not rect stuff here
501
502
        if (ctx->shape != BIN_ONLY_SHAPE) {
503
            skip_bits(&s->gb, 3); /* intra dc vlc threshold */
504
            // FIXME don't just ignore everything
505
            if (s->pict_type == AV_PICTURE_TYPE_S &&
506
                ctx->vol_sprite_usage == GMC_SPRITE) {
507
                if (mpeg4_decode_sprite_trajectory(ctx, &s->gb) < 0)
508
                    return AVERROR_INVALIDDATA;
509
                av_log(s->avctx, AV_LOG_ERROR, "untested\n");
510
            }
511
512
            // FIXME reduced res stuff here
513
514
            if (s->pict_type != AV_PICTURE_TYPE_I) {
515
                int f_code = get_bits(&s->gb, 3);       /* fcode_for */
516
                if (f_code == 0)
517
                    av_log(s->avctx, AV_LOG_ERROR,
518
                           "Error, video packet header damaged (f_code=0)\n");
519
            }
520
            if (s->pict_type == AV_PICTURE_TYPE_B) {
521
                int b_code = get_bits(&s->gb, 3);
522
                if (b_code == 0)
523
                    av_log(s->avctx, AV_LOG_ERROR,
524
                           "Error, video packet header damaged (b_code=0)\n");
525
            }
526
        }
527
    }
528
3867
    if (ctx->new_pred)
529
        decode_new_pred(ctx, &s->gb);
530
531
3867
    return 0;
532
}
533
534
static void reset_studio_dc_predictors(MpegEncContext *s)
535
{
536
    /* Reset DC Predictors */
537
    s->last_dc[0] =
538
    s->last_dc[1] =
539
    s->last_dc[2] = 1 << (s->avctx->bits_per_raw_sample + s->dct_precision + s->intra_dc_precision - 1);
540
}
541
542
/**
543
 * Decode the next video packet.
544
 * @return <0 if something went wrong
545
 */
546
int ff_mpeg4_decode_studio_slice_header(Mpeg4DecContext *ctx)
547
{
548
    MpegEncContext *s = &ctx->m;
549
    GetBitContext *gb = &s->gb;
550
    unsigned vlc_len;
551
    uint16_t mb_num;
552
553
    if (get_bits_left(gb) >= 32 && get_bits_long(gb, 32) == SLICE_START_CODE) {
554
        vlc_len = av_log2(s->mb_width * s->mb_height) + 1;
555
        mb_num = get_bits(gb, vlc_len);
556
557
        if (mb_num >= s->mb_num)
558
            return AVERROR_INVALIDDATA;
559
560
        s->mb_x = mb_num % s->mb_width;
561
        s->mb_y = mb_num / s->mb_width;
562
563
        if (ctx->shape != BIN_ONLY_SHAPE)
564
            s->qscale = mpeg_get_qscale(s);
565
566
        if (get_bits1(gb)) {  /* slice_extension_flag */
567
            skip_bits1(gb);   /* intra_slice */
568
            skip_bits1(gb);   /* slice_VOP_id_enable */
569
            skip_bits(gb, 6); /* slice_VOP_id */
570
            while (get_bits1(gb)) /* extra_bit_slice */
571
                skip_bits(gb, 8); /* extra_information_slice */
572
        }
573
574
        reset_studio_dc_predictors(s);
575
    }
576
    else {
577
        return AVERROR_INVALIDDATA;
578
    }
579
580
    return 0;
581
}
582
583
/**
584
 * Get the average motion vector for a GMC MB.
585
 * @param n either 0 for the x component or 1 for y
586
 * @return the average MV for a GMC MB
587
 */
588
static inline int get_amv(Mpeg4DecContext *ctx, int n)
589
{
590
    MpegEncContext *s = &ctx->m;
591
    int x, y, mb_v, sum, dx, dy, shift;
592
    int len     = 1 << (s->f_code + 4);
593
    const int a = s->sprite_warping_accuracy;
594
595
    if (s->workaround_bugs & FF_BUG_AMV)
596
        len >>= s->quarter_sample;
597
598
    if (s->real_sprite_warping_points == 1) {
599
        if (ctx->divx_version == 500 && ctx->divx_build == 413 && a >= s->quarter_sample)
600
            sum = s->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
601
        else
602
            sum = RSHIFT(s->sprite_offset[0][n] * (1 << s->quarter_sample), a);
603
    } else {
604
        dx    = s->sprite_delta[n][0];
605
        dy    = s->sprite_delta[n][1];
606
        shift = ctx->sprite_shift[0];
607
        if (n)
608
            dy -= 1 << (shift + a + 1);
609
        else
610
            dx -= 1 << (shift + a + 1);
611
        mb_v = s->sprite_offset[0][n] + dx * s->mb_x * 16U + dy * s->mb_y * 16U;
612
613
        sum = 0;
614
        for (y = 0; y < 16; y++) {
615
            int v;
616
617
            v = mb_v + dy * y;
618
            // FIXME optimize
619
            for (x = 0; x < 16; x++) {
620
                sum += v >> shift;
621
                v   += dx;
622
            }
623
        }
624
        sum = RSHIFT(sum, a + 8 - s->quarter_sample);
625
    }
626
627
    if (sum < -len)
628
        sum = -len;
629
    else if (sum >= len)
630
        sum = len - 1;
631
632
    return sum;
633
}
634
635
/**
636
 * Decode the dc value.
637
 * @param n block index (0-3 are luma, 4-5 are chroma)
638
 * @param dir_ptr the prediction direction will be stored here
639
 * @return the quantized dc
640
 */
641
1068954
static inline int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
642
{
643
    int level, code;
644
645
1068954
    if (n < 4)
646
712636
        code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
647
    else
648
356318
        code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
649
650

1068954
    if (code < 0 || code > 9 /* && s->nbit < 9 */) {
651
        av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
652
        return AVERROR_INVALIDDATA;
653
    }
654
655
1068954
    if (code == 0) {
656
156549
        level = 0;
657
    } else {
658
        if (IS_3IV1) {
659
            if (code == 1)
660
                level = 2 * get_bits1(&s->gb) - 1;
661
            else {
662
                if (get_bits1(&s->gb))
663
                    level = get_bits(&s->gb, code - 1) + (1 << (code - 1));
664
                else
665
                    level = -get_bits(&s->gb, code - 1) - (1 << (code - 1));
666
            }
667
        } else {
668
912405
            level = get_xbits(&s->gb, code);
669
        }
670
671
912405
        if (code > 8) {
672
            if (get_bits1(&s->gb) == 0) { /* marker */
673
                if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)) {
674
                    av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
675
                    return AVERROR_INVALIDDATA;
676
                }
677
            }
678
        }
679
    }
680
681
1068954
    return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
682
}
683
684
/**
685
 * Decode first partition.
686
 * @return number of MBs decoded or <0 if an error occurred
687
 */
688
1728
static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
689
{
690
1728
    MpegEncContext *s = &ctx->m;
691
1728
    int mb_num = 0;
692
    static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
693
694
    /* decode first partition */
695
1728
    s->first_slice_line = 1;
696
8311
    for (; s->mb_y < s->mb_height; s->mb_y++) {
697
7839
        ff_init_block_index(s);
698
145903
        for (; s->mb_x < s->mb_width; s->mb_x++) {
699
139320
            const int xy = s->mb_x + s->mb_y * s->mb_stride;
700
            int cbpc;
701
139320
            int dir = 0;
702
703
139320
            mb_num++;
704
139320
            ff_update_block_index(s);
705

139320
            if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
706
1611
                s->first_slice_line = 0;
707
708
139320
            if (s->pict_type == AV_PICTURE_TYPE_I) {
709
                int i;
710
711
                do {
712
17762
                    if (show_bits(&s->gb, 19) == DC_MARKER)
713
439
                        return mb_num - 1;
714
715
17323
                    cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
716
17323
                    if (cbpc < 0) {
717
                        av_log(s->avctx, AV_LOG_ERROR,
718
                               "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
719
                        return AVERROR_INVALIDDATA;
720
                    }
721
17323
                } while (cbpc == 8);
722
723
17323
                s->cbp_table[xy]               = cbpc & 3;
724
17323
                s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
725
17323
                s->mb_intra                    = 1;
726
727
17323
                if (cbpc & 4)
728
                    ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
729
730
17323
                s->current_picture.qscale_table[xy] = s->qscale;
731
732
17323
                s->mbintra_table[xy] = 1;
733
121261
                for (i = 0; i < 6; i++) {
734
                    int dc_pred_dir;
735
103938
                    int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
736
103938
                    if (dc < 0) {
737
                        av_log(s->avctx, AV_LOG_ERROR,
738
                               "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
739
                        return dc;
740
                    }
741
103938
                    dir <<= 1;
742
103938
                    if (dc_pred_dir)
743
30118
                        dir |= 1;
744
                }
745
17323
                s->pred_dir_table[xy] = dir;
746
            } else { /* P/S_TYPE */
747
                int mx, my, pred_x, pred_y, bits;
748
121558
                int16_t *const mot_val = s->current_picture.motion_val[0][s->block_index[0]];
749
121558
                const int stride       = s->b8_stride * 2;
750
751
121558
try_again:
752
121558
                bits = show_bits(&s->gb, 17);
753
121558
                if (bits == MOTION_MARKER)
754
817
                    return mb_num - 1;
755
756
120741
                skip_bits1(&s->gb);
757
120741
                if (bits & 0x10000) {
758
                    /* skip mb */
759
997
                    if (s->pict_type == AV_PICTURE_TYPE_S &&
760
                        ctx->vol_sprite_usage == GMC_SPRITE) {
761
                        s->current_picture.mb_type[xy] = MB_TYPE_SKIP  |
762
                                                         MB_TYPE_16x16 |
763
                                                         MB_TYPE_GMC   |
764
                                                         MB_TYPE_L0;
765
                        mx = get_amv(ctx, 0);
766
                        my = get_amv(ctx, 1);
767
                    } else {
768
997
                        s->current_picture.mb_type[xy] = MB_TYPE_SKIP  |
769
                                                         MB_TYPE_16x16 |
770
                                                         MB_TYPE_L0;
771
997
                        mx = my = 0;
772
                    }
773
997
                    mot_val[0]          =
774
997
                    mot_val[2]          =
775
997
                    mot_val[0 + stride] =
776
997
                    mot_val[2 + stride] = mx;
777
997
                    mot_val[1]          =
778
997
                    mot_val[3]          =
779
997
                    mot_val[1 + stride] =
780
997
                    mot_val[3 + stride] = my;
781
782
997
                    if (s->mbintra_table[xy])
783
224
                        ff_clean_intra_table_entries(s);
784
997
                    continue;
785
                }
786
787
119744
                cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
788
119744
                if (cbpc < 0) {
789
                    av_log(s->avctx, AV_LOG_ERROR,
790
                           "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
791
                    return AVERROR_INVALIDDATA;
792
                }
793
119744
                if (cbpc == 20)
794
                    goto try_again;
795
796
119744
                s->cbp_table[xy] = cbpc & (8 + 3);  // 8 is dquant
797
798
119744
                s->mb_intra = ((cbpc & 4) != 0);
799
800
119744
                if (s->mb_intra) {
801
6287
                    s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
802
6287
                    s->mbintra_table[xy] = 1;
803
6287
                    mot_val[0]          =
804
6287
                    mot_val[2]          =
805
6287
                    mot_val[0 + stride] =
806
6287
                    mot_val[2 + stride] = 0;
807
6287
                    mot_val[1]          =
808
6287
                    mot_val[3]          =
809
6287
                    mot_val[1 + stride] =
810
6287
                    mot_val[3 + stride] = 0;
811
                } else {
812
113457
                    if (s->mbintra_table[xy])
813
19851
                        ff_clean_intra_table_entries(s);
814
815
113457
                    if (s->pict_type == AV_PICTURE_TYPE_S &&
816
                        ctx->vol_sprite_usage == GMC_SPRITE &&
817
                        (cbpc & 16) == 0)
818
                        s->mcsel = get_bits1(&s->gb);
819
                    else
820
113457
                        s->mcsel = 0;
821
822
113457
                    if ((cbpc & 16) == 0) {
823
                        /* 16x16 motion prediction */
824
825
81626
                        ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
826
81626
                        if (!s->mcsel) {
827
81626
                            mx = ff_h263_decode_motion(s, pred_x, s->f_code);
828
81626
                            if (mx >= 0xffff)
829
                                return AVERROR_INVALIDDATA;
830
831
81626
                            my = ff_h263_decode_motion(s, pred_y, s->f_code);
832
81626
                            if (my >= 0xffff)
833
                                return AVERROR_INVALIDDATA;
834
81626
                            s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
835
                                                             MB_TYPE_L0;
836
                        } else {
837
                            mx = get_amv(ctx, 0);
838
                            my = get_amv(ctx, 1);
839
                            s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
840
                                                             MB_TYPE_GMC   |
841
                                                             MB_TYPE_L0;
842
                        }
843
844
81626
                        mot_val[0]          =
845
81626
                        mot_val[2]          =
846
81626
                        mot_val[0 + stride] =
847
81626
                        mot_val[2 + stride] = mx;
848
81626
                        mot_val[1]          =
849
81626
                        mot_val[3]          =
850
81626
                        mot_val[1 + stride] =
851
81626
                        mot_val[3 + stride] = my;
852
                    } else {
853
                        int i;
854
31831
                        s->current_picture.mb_type[xy] = MB_TYPE_8x8 |
855
                                                         MB_TYPE_L0;
856
159155
                        for (i = 0; i < 4; i++) {
857
127324
                            int16_t *mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
858
127324
                            mx = ff_h263_decode_motion(s, pred_x, s->f_code);
859
127324
                            if (mx >= 0xffff)
860
                                return AVERROR_INVALIDDATA;
861
862
127324
                            my = ff_h263_decode_motion(s, pred_y, s->f_code);
863
127324
                            if (my >= 0xffff)
864
                                return AVERROR_INVALIDDATA;
865
127324
                            mot_val[0] = mx;
866
127324
                            mot_val[1] = my;
867
                        }
868
                    }
869
                }
870
            }
871
        }
872
6583
        s->mb_x = 0;
873
    }
874
875
472
    return mb_num;
876
}
877
878
/**
879
 * decode second partition.
880
 * @return <0 if an error occurred
881
 */
882
1728
static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
883
{
884
1728
    int mb_num = 0;
885
    static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
886
887
1728
    s->mb_x = s->resync_mb_x;
888
1728
    s->first_slice_line = 1;
889
7728
    for (s->mb_y = s->resync_mb_y; mb_num < mb_count; s->mb_y++) {
890
7728
        ff_init_block_index(s);
891

145792
        for (; mb_num < mb_count && s->mb_x < s->mb_width; s->mb_x++) {
892
138064
            const int xy = s->mb_x + s->mb_y * s->mb_stride;
893
894
138064
            mb_num++;
895
138064
            ff_update_block_index(s);
896

138064
            if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
897
1611
                s->first_slice_line = 0;
898
899
138064
            if (s->pict_type == AV_PICTURE_TYPE_I) {
900
17323
                int ac_pred = get_bits1(&s->gb);
901
17323
                int cbpy    = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
902
17323
                if (cbpy < 0) {
903
                    av_log(s->avctx, AV_LOG_ERROR,
904
                           "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
905
                    return AVERROR_INVALIDDATA;
906
                }
907
908
17323
                s->cbp_table[xy]               |= cbpy << 2;
909
17323
                s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
910
            } else { /* P || S_TYPE */
911
120741
                if (IS_INTRA(s->current_picture.mb_type[xy])) {
912
                    int i;
913
6287
                    int dir     = 0;
914
6287
                    int ac_pred = get_bits1(&s->gb);
915
6287
                    int cbpy    = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
916
917
6287
                    if (cbpy < 0) {
918
                        av_log(s->avctx, AV_LOG_ERROR,
919
                               "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
920
                        return AVERROR_INVALIDDATA;
921
                    }
922
923
6287
                    if (s->cbp_table[xy] & 8)
924
                        ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
925
6287
                    s->current_picture.qscale_table[xy] = s->qscale;
926
927
44009
                    for (i = 0; i < 6; i++) {
928
                        int dc_pred_dir;
929
37722
                        int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
930
37722
                        if (dc < 0) {
931
                            av_log(s->avctx, AV_LOG_ERROR,
932
                                   "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
933
                            return dc;
934
                        }
935
37722
                        dir <<= 1;
936
37722
                        if (dc_pred_dir)
937
11780
                            dir |= 1;
938
                    }
939
6287
                    s->cbp_table[xy]               &= 3;  // remove dquant
940
6287
                    s->cbp_table[xy]               |= cbpy << 2;
941
6287
                    s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
942
6287
                    s->pred_dir_table[xy]           = dir;
943
114454
                } else if (IS_SKIP(s->current_picture.mb_type[xy])) {
944
997
                    s->current_picture.qscale_table[xy] = s->qscale;
945
997
                    s->cbp_table[xy]                    = 0;
946
                } else {
947
113457
                    int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
948
949
113457
                    if (cbpy < 0) {
950
                        av_log(s->avctx, AV_LOG_ERROR,
951
                               "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
952
                        return AVERROR_INVALIDDATA;
953
                    }
954
955
113457
                    if (s->cbp_table[xy] & 8)
956
                        ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
957
113457
                    s->current_picture.qscale_table[xy] = s->qscale;
958
959
113457
                    s->cbp_table[xy] &= 3;  // remove dquant
960
113457
                    s->cbp_table[xy] |= (cbpy ^ 0xf) << 2;
961
                }
962
            }
963
        }
964
7728
        if (mb_num >= mb_count)
965
1728
            return 0;
966
6000
        s->mb_x = 0;
967
    }
968
    return 0;
969
}
970
971
/**
972
 * Decode the first and second partition.
973
 * @return <0 if error (and sets error type in the error_status_table)
974
 */
975
1728
int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx)
976
{
977
1728
    MpegEncContext *s = &ctx->m;
978
    int mb_num;
979
    int ret;
980
1728
    const int part_a_error = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR;
981
1728
    const int part_a_end   = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END   | ER_MV_END)   : ER_MV_END;
982
983
1728
    mb_num = mpeg4_decode_partition_a(ctx);
984
1728
    if (mb_num <= 0) {
985
        ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
986
                        s->mb_x, s->mb_y, part_a_error);
987
        return mb_num ? mb_num : AVERROR_INVALIDDATA;
988
    }
989
990
1728
    if (s->resync_mb_x + s->resync_mb_y * s->mb_width + mb_num > s->mb_num) {
991
        av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
992
        ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
993
                        s->mb_x, s->mb_y, part_a_error);
994
        return AVERROR_INVALIDDATA;
995
    }
996
997
1728
    s->mb_num_left = mb_num;
998
999
1728
    if (s->pict_type == AV_PICTURE_TYPE_I) {
1000
499
        while (show_bits(&s->gb, 9) == 1)
1001
            skip_bits(&s->gb, 9);
1002
499
        if (get_bits(&s->gb, 19) != DC_MARKER) {
1003
            av_log(s->avctx, AV_LOG_ERROR,
1004
                   "marker missing after first I partition at %d %d\n",
1005
                   s->mb_x, s->mb_y);
1006
            return AVERROR_INVALIDDATA;
1007
        }
1008
    } else {
1009
1229
        while (show_bits(&s->gb, 10) == 1)
1010
            skip_bits(&s->gb, 10);
1011
1229
        if (get_bits(&s->gb, 17) != MOTION_MARKER) {
1012
            av_log(s->avctx, AV_LOG_ERROR,
1013
                   "marker missing after first P partition at %d %d\n",
1014
                   s->mb_x, s->mb_y);
1015
            return AVERROR_INVALIDDATA;
1016
        }
1017
    }
1018
1728
    ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1019
1728
                    s->mb_x - 1, s->mb_y, part_a_end);
1020
1021
1728
    ret = mpeg4_decode_partition_b(s, mb_num);
1022
1728
    if (ret < 0) {
1023
        if (s->pict_type == AV_PICTURE_TYPE_P)
1024
            ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1025
                            s->mb_x, s->mb_y, ER_DC_ERROR);
1026
        return ret;
1027
    } else {
1028
1728
        if (s->pict_type == AV_PICTURE_TYPE_P)
1029
1229
            ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1030
1229
                            s->mb_x - 1, s->mb_y, ER_DC_END);
1031
    }
1032
1033
1728
    return 0;
1034
}
1035
1036
/**
1037
 * Decode a block.
1038
 * @return <0 if an error occurred
1039
 */
1040
7873800
static inline int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block,
1041
                                     int n, int coded, int intra, int rvlc)
1042
{
1043
7873800
    MpegEncContext *s = &ctx->m;
1044
    int level, i, last, run, qmul, qadd;
1045
7873800
    int av_uninit(dc_pred_dir);
1046
    RLTable *rl;
1047
    RL_VLC_ELEM *rl_vlc;
1048
    const uint8_t *scan_table;
1049
1050
    // Note intra & rvlc should be optimized away if this is inlined
1051
1052
7873800
    if (intra) {
1053
1068954
        if (ctx->use_intra_dc_vlc) {
1054
            /* DC coef */
1055
1068954
            if (s->partitioned_frame) {
1056
141660
                level = s->dc_val[0][s->block_index[n]];
1057
141660
                if (n < 4)
1058
94440
                    level = FASTDIV((level + (s->y_dc_scale >> 1)), s->y_dc_scale);
1059
                else
1060
47220
                    level = FASTDIV((level + (s->c_dc_scale >> 1)), s->c_dc_scale);
1061
141660
                dc_pred_dir = (s->pred_dir_table[s->mb_x + s->mb_y * s->mb_stride] << n) & 32;
1062
            } else {
1063
927294
                level = mpeg4_decode_dc(s, n, &dc_pred_dir);
1064
927294
                if (level < 0)
1065
                    return level;
1066
            }
1067
1068954
            block[0] = level;
1068
1068954
            i        = 0;
1069
        } else {
1070
            i = -1;
1071
            ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
1072
        }
1073
1068954
        if (!coded)
1074
215012
            goto not_coded;
1075
1076
853942
        if (rvlc) {
1077
            rl     = &ff_rvlc_rl_intra;
1078
            rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
1079
        } else {
1080
853942
            rl     = &ff_mpeg4_rl_intra;
1081
853942
            rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
1082
        }
1083
853942
        if (s->ac_pred) {
1084
22081
            if (dc_pred_dir == 0)
1085
15515
                scan_table = s->intra_v_scantable.permutated;  /* left */
1086
            else
1087
6566
                scan_table = s->intra_h_scantable.permutated;  /* top */
1088
        } else {
1089
831861
            scan_table = s->intra_scantable.permutated;
1090
        }
1091
853942
        qmul = 1;
1092
853942
        qadd = 0;
1093
    } else {
1094
6804846
        i = -1;
1095
6804846
        if (!coded) {
1096
4621462
            s->block_last_index[n] = i;
1097
4621462
            return 0;
1098
        }
1099
2183384
        if (rvlc)
1100
            rl = &ff_rvlc_rl_inter;
1101
        else
1102
2183384
            rl = &ff_h263_rl_inter;
1103
1104
2183384
        scan_table = s->intra_scantable.permutated;
1105
1106
2183384
        if (s->mpeg_quant) {
1107
11882
            qmul = 1;
1108
11882
            qadd = 0;
1109
11882
            if (rvlc)
1110
                rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
1111
            else
1112
11882
                rl_vlc = ff_h263_rl_inter.rl_vlc[0];
1113
        } else {
1114
2171502
            qmul = s->qscale << 1;
1115
2171502
            qadd = (s->qscale - 1) | 1;
1116
2171502
            if (rvlc)
1117
                rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
1118
            else
1119
2171502
                rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
1120
        }
1121
    }
1122
    {
1123
3037326
        OPEN_READER(re, &s->gb);
1124
        for (;;) {
1125
26282206
            UPDATE_CACHE(re, &s->gb);
1126
26282206
            GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1127
26282206
            if (level == 0) {
1128
                /* escape */
1129
770420
                if (rvlc) {
1130
                    if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1131
                        av_log(s->avctx, AV_LOG_ERROR,
1132
                               "1. marker bit missing in rvlc esc\n");
1133
                        return AVERROR_INVALIDDATA;
1134
                    }
1135
                    SKIP_CACHE(re, &s->gb, 1);
1136
1137
                    last = SHOW_UBITS(re, &s->gb, 1);
1138
                    SKIP_CACHE(re, &s->gb, 1);
1139
                    run = SHOW_UBITS(re, &s->gb, 6);
1140
                    SKIP_COUNTER(re, &s->gb, 1 + 1 + 6);
1141
                    UPDATE_CACHE(re, &s->gb);
1142
1143
                    if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1144
                        av_log(s->avctx, AV_LOG_ERROR,
1145
                               "2. marker bit missing in rvlc esc\n");
1146
                        return AVERROR_INVALIDDATA;
1147
                    }
1148
                    SKIP_CACHE(re, &s->gb, 1);
1149
1150
                    level = SHOW_UBITS(re, &s->gb, 11);
1151
                    SKIP_CACHE(re, &s->gb, 11);
1152
1153
                    if (SHOW_UBITS(re, &s->gb, 5) != 0x10) {
1154
                        av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
1155
                        return AVERROR_INVALIDDATA;
1156
                    }
1157
                    SKIP_CACHE(re, &s->gb, 5);
1158
1159
                    level = level * qmul + qadd;
1160
                    level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1161
                    SKIP_COUNTER(re, &s->gb, 1 + 11 + 5 + 1);
1162
1163
                    i += run + 1;
1164
                    if (last)
1165
                        i += 192;
1166
                } else {
1167
                    int cache;
1168
770420
                    cache = GET_CACHE(re, &s->gb);
1169
1170
                    if (IS_3IV1)
1171
                        cache ^= 0xC0000000;
1172
1173
770420
                    if (cache & 0x80000000) {
1174
353524
                        if (cache & 0x40000000) {
1175
                            /* third escape */
1176
137781
                            SKIP_CACHE(re, &s->gb, 2);
1177
137781
                            last = SHOW_UBITS(re, &s->gb, 1);
1178
137781
                            SKIP_CACHE(re, &s->gb, 1);
1179
137781
                            run = SHOW_UBITS(re, &s->gb, 6);
1180
137781
                            SKIP_COUNTER(re, &s->gb, 2 + 1 + 6);
1181
137781
                            UPDATE_CACHE(re, &s->gb);
1182
1183
                            if (IS_3IV1) {
1184
                                level = SHOW_SBITS(re, &s->gb, 12);
1185
                                LAST_SKIP_BITS(re, &s->gb, 12);
1186
                            } else {
1187
137781
                                if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1188
                                    av_log(s->avctx, AV_LOG_ERROR,
1189
                                           "1. marker bit missing in 3. esc\n");
1190
                                    if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
1191
                                        return AVERROR_INVALIDDATA;
1192
                                }
1193
137781
                                SKIP_CACHE(re, &s->gb, 1);
1194
1195
137781
                                level = SHOW_SBITS(re, &s->gb, 12);
1196
137781
                                SKIP_CACHE(re, &s->gb, 12);
1197
1198
137781
                                if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1199
                                    av_log(s->avctx, AV_LOG_ERROR,
1200
                                           "2. marker bit missing in 3. esc\n");
1201
                                    if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
1202
                                        return AVERROR_INVALIDDATA;
1203
                                }
1204
1205
137781
                                SKIP_COUNTER(re, &s->gb, 1 + 12 + 1);
1206
                            }
1207
1208
#if 0
1209
                            if (s->error_recognition >= FF_ER_COMPLIANT) {
1210
                                const int abs_level= FFABS(level);
1211
                                if (abs_level<=MAX_LEVEL && run<=MAX_RUN) {
1212
                                    const int run1= run - rl->max_run[last][abs_level] - 1;
1213
                                    if (abs_level <= rl->max_level[last][run]) {
1214
                                        av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1215
                                        return AVERROR_INVALIDDATA;
1216
                                    }
1217
                                    if (s->error_recognition > FF_ER_COMPLIANT) {
1218
                                        if (abs_level <= rl->max_level[last][run]*2) {
1219
                                            av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1220
                                            return AVERROR_INVALIDDATA;
1221
                                        }
1222
                                        if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) {
1223
                                            av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1224
                                            return AVERROR_INVALIDDATA;
1225
                                        }
1226
                                    }
1227
                                }
1228
                            }
1229
#endif
1230
137781
                            if (level > 0)
1231
69038
                                level = level * qmul + qadd;
1232
                            else
1233
68743
                                level = level * qmul - qadd;
1234
1235
137781
                            if ((unsigned)(level + 2048) > 4095) {
1236
                                if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)) {
1237
                                    if (level > 2560 || level < -2560) {
1238
                                        av_log(s->avctx, AV_LOG_ERROR,
1239
                                               "|level| overflow in 3. esc, qp=%d\n",
1240
                                               s->qscale);
1241
                                        return AVERROR_INVALIDDATA;
1242
                                    }
1243
                                }
1244
                                level = level < 0 ? -2048 : 2047;
1245
                            }
1246
1247
137781
                            i += run + 1;
1248
137781
                            if (last)
1249
44545
                                i += 192;
1250
                        } else {
1251
                            /* second escape */
1252
215743
                            SKIP_BITS(re, &s->gb, 2);
1253
215743
                            GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1254
215743
                            i    += run + rl->max_run[run >> 7][level / qmul] + 1;  // FIXME opt indexing
1255
215743
                            level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1256
215743
                            LAST_SKIP_BITS(re, &s->gb, 1);
1257
                        }
1258
                    } else {
1259
                        /* first escape */
1260
416896
                        SKIP_BITS(re, &s->gb, 1);
1261
416896
                        GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1262
416896
                        i    += run;
1263
416896
                        level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul;  // FIXME opt indexing
1264
416896
                        level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1265
416896
                        LAST_SKIP_BITS(re, &s->gb, 1);
1266
                    }
1267
                }
1268
            } else {
1269
25511786
                i    += run;
1270
25511786
                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1271
25511786
                LAST_SKIP_BITS(re, &s->gb, 1);
1272
            }
1273
            ff_tlog(s->avctx, "dct[%d][%d] = %- 4d end?:%d\n", scan_table[i&63]&7, scan_table[i&63] >> 3, level, i>62);
1274
26282206
            if (i > 62) {
1275
3037326
                i -= 192;
1276
3037326
                if (i & (~63)) {
1277
                    av_log(s->avctx, AV_LOG_ERROR,
1278
                           "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1279
                    return AVERROR_INVALIDDATA;
1280
                }
1281
1282
3037326
                block[scan_table[i]] = level;
1283
3037326
                break;
1284
            }
1285
1286
23244880
            block[scan_table[i]] = level;
1287
        }
1288
3037326
        CLOSE_READER(re, &s->gb);
1289
    }
1290
1291
3252338
not_coded:
1292
3252338
    if (intra) {
1293
1068954
        if (!ctx->use_intra_dc_vlc) {
1294
            block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
1295
1296
            i -= i >> 31;  // if (i == -1) i = 0;
1297
        }
1298
1299
1068954
        ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1300
1068954
        if (s->ac_pred)
1301
25890
            i = 63;  // FIXME not optimal
1302
    }
1303
3252338
    s->block_last_index[n] = i;
1304
3252338
    return 0;
1305
}
1306
1307
/**
1308
 * decode partition C of one MB.
1309
 * @return <0 if an error occurred
1310
 */
1311
138064
static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
1312
{
1313
138064
    Mpeg4DecContext *ctx = s->avctx->priv_data;
1314
    int cbp, mb_type;
1315
138064
    const int xy = s->mb_x + s->mb_y * s->mb_stride;
1316
1317
    av_assert2(s == (void*)ctx);
1318
1319
138064
    mb_type = s->current_picture.mb_type[xy];
1320
138064
    cbp     = s->cbp_table[xy];
1321
1322
138064
    ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1323
1324
138064
    if (s->current_picture.qscale_table[xy] != s->qscale)
1325
        ff_set_qscale(s, s->current_picture.qscale_table[xy]);
1326
1327
138064
    if (s->pict_type == AV_PICTURE_TYPE_P ||
1328
138064
        s->pict_type == AV_PICTURE_TYPE_S) {
1329
        int i;
1330
603705
        for (i = 0; i < 4; i++) {
1331
482964
            s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
1332
482964
            s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
1333
        }
1334
120741
        s->mb_intra = IS_INTRA(mb_type);
1335
1336
120741
        if (IS_SKIP(mb_type)) {
1337
            /* skip mb */
1338
6979
            for (i = 0; i < 6; i++)
1339
5982
                s->block_last_index[i] = -1;
1340
997
            s->mv_dir  = MV_DIR_FORWARD;
1341
997
            s->mv_type = MV_TYPE_16X16;
1342
997
            if (s->pict_type == AV_PICTURE_TYPE_S
1343
                && ctx->vol_sprite_usage == GMC_SPRITE) {
1344
                s->mcsel      = 1;
1345
                s->mb_skipped = 0;
1346
            } else {
1347
997
                s->mcsel      = 0;
1348
997
                s->mb_skipped = 1;
1349
            }
1350
119744
        } else if (s->mb_intra) {
1351
6287
            s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1352
113457
        } else if (!s->mb_intra) {
1353
            // s->mcsel = 0;  // FIXME do we need to init that?
1354
1355
113457
            s->mv_dir = MV_DIR_FORWARD;
1356
113457
            if (IS_8X8(mb_type)) {
1357
31831
                s->mv_type = MV_TYPE_8X8;
1358
            } else {
1359
81626
                s->mv_type = MV_TYPE_16X16;
1360
            }
1361
        }
1362
    } else { /* I-Frame */
1363
17323
        s->mb_intra = 1;
1364
17323
        s->ac_pred  = IS_ACPRED(s->current_picture.mb_type[xy]);
1365
    }
1366
1367
138064
    if (!IS_SKIP(mb_type)) {
1368
        int i;
1369
137067
        s->bdsp.clear_blocks(s->block[0]);
1370
        /* decode each block */
1371
959469
        for (i = 0; i < 6; i++) {
1372
822402
            if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, s->mb_intra, ctx->rvlc) < 0) {
1373
                av_log(s->avctx, AV_LOG_ERROR,
1374
                       "texture corrupted at %d %d %d\n",
1375
                       s->mb_x, s->mb_y, s->mb_intra);
1376
                return AVERROR_INVALIDDATA;
1377
            }
1378
822402
            cbp += cbp;
1379
        }
1380
    }
1381
1382
    /* per-MB end of slice check */
1383
138064
    if (--s->mb_num_left <= 0) {
1384
1728
        if (mpeg4_is_resync(ctx))
1385
1728
            return SLICE_END;
1386
        else
1387
            return SLICE_NOEND;
1388
    } else {
1389
136336
        if (mpeg4_is_resync(ctx)) {
1390
308
            const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
1391
308
            if (s->cbp_table[xy + delta])
1392
                return SLICE_END;
1393
        }
1394
136336
        return SLICE_OK;
1395
    }
1396
}
1397
1398
1289056
static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
1399
{
1400
1289056
    Mpeg4DecContext *ctx = s->avctx->priv_data;
1401
    int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1402
    int16_t *mot_val;
1403
    static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1404
1289056
    const int xy = s->mb_x + s->mb_y * s->mb_stride;
1405
1406
    av_assert2(s ==  (void*)ctx);
1407
    av_assert2(s->h263_pred);
1408
1409
1289056
    if (s->pict_type == AV_PICTURE_TYPE_P ||
1410
367659
        s->pict_type == AV_PICTURE_TYPE_S) {
1411
        do {
1412
921397
            if (get_bits1(&s->gb)) {
1413
                /* skip mb */
1414
80953
                s->mb_intra = 0;
1415
566671
                for (i = 0; i < 6; i++)
1416
485718
                    s->block_last_index[i] = -1;
1417
80953
                s->mv_dir  = MV_DIR_FORWARD;
1418
80953
                s->mv_type = MV_TYPE_16X16;
1419
80953
                if (s->pict_type == AV_PICTURE_TYPE_S &&
1420
                    ctx->vol_sprite_usage == GMC_SPRITE) {
1421
                    s->current_picture.mb_type[xy] = MB_TYPE_SKIP  |
1422
                                                     MB_TYPE_GMC   |
1423
                                                     MB_TYPE_16x16 |
1424
                                                     MB_TYPE_L0;
1425
                    s->mcsel       = 1;
1426
                    s->mv[0][0][0] = get_amv(ctx, 0);
1427
                    s->mv[0][0][1] = get_amv(ctx, 1);
1428
                    s->mb_skipped  = 0;
1429
                } else {
1430
80953
                    s->current_picture.mb_type[xy] = MB_TYPE_SKIP  |
1431
                                                     MB_TYPE_16x16 |
1432
                                                     MB_TYPE_L0;
1433
80953
                    s->mcsel       = 0;
1434
80953
                    s->mv[0][0][0] = 0;
1435
80953
                    s->mv[0][0][1] = 0;
1436
80953
                    s->mb_skipped  = 1;
1437
                }
1438
80953
                goto end;
1439
            }
1440
840444
            cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
1441
840444
            if (cbpc < 0) {
1442
                av_log(s->avctx, AV_LOG_ERROR,
1443
                       "mcbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1444
                return AVERROR_INVALIDDATA;
1445
            }
1446
840444
        } while (cbpc == 20);
1447
1448
840444
        s->bdsp.clear_blocks(s->block[0]);
1449
840444
        dquant      = cbpc & 8;
1450
840444
        s->mb_intra = ((cbpc & 4) != 0);
1451
840444
        if (s->mb_intra)
1452
27278
            goto intra;
1453
1454
813166
        if (s->pict_type == AV_PICTURE_TYPE_S &&
1455
            ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
1456
            s->mcsel = get_bits1(&s->gb);
1457
        else
1458
813166
            s->mcsel = 0;
1459
813166
        cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
1460
813166
        if (cbpy < 0) {
1461
            av_log(s->avctx, AV_LOG_ERROR,
1462
                   "P cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1463
            return AVERROR_INVALIDDATA;
1464
        }
1465
1466
813166
        cbp = (cbpc & 3) | (cbpy << 2);
1467
813166
        if (dquant)
1468
9237
            ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1469

813166
        if ((!s->progressive_sequence) &&
1470
            (cbp || (s->workaround_bugs & FF_BUG_XVID_ILACE)))
1471
            s->interlaced_dct = get_bits1(&s->gb);
1472
1473
813166
        s->mv_dir = MV_DIR_FORWARD;
1474
813166
        if ((cbpc & 16) == 0) {
1475
721166
            if (s->mcsel) {
1476
                s->current_picture.mb_type[xy] = MB_TYPE_GMC   |
1477
                                                 MB_TYPE_16x16 |
1478
                                                 MB_TYPE_L0;
1479
                /* 16x16 global motion prediction */
1480
                s->mv_type     = MV_TYPE_16X16;
1481
                mx             = get_amv(ctx, 0);
1482
                my             = get_amv(ctx, 1);
1483
                s->mv[0][0][0] = mx;
1484
                s->mv[0][0][1] = my;
1485

721166
            } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
1486
                s->current_picture.mb_type[xy] = MB_TYPE_16x8 |
1487
                                                 MB_TYPE_L0   |
1488
                                                 MB_TYPE_INTERLACED;
1489
                /* 16x8 field motion prediction */
1490
                s->mv_type = MV_TYPE_FIELD;
1491
1492
                s->field_select[0][0] = get_bits1(&s->gb);
1493
                s->field_select[0][1] = get_bits1(&s->gb);
1494
1495
                ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1496
1497
                for (i = 0; i < 2; i++) {
1498
                    mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1499
                    if (mx >= 0xffff)
1500
                        return AVERROR_INVALIDDATA;
1501
1502
                    my = ff_h263_decode_motion(s, pred_y / 2, s->f_code);
1503
                    if (my >= 0xffff)
1504
                        return AVERROR_INVALIDDATA;
1505
1506
                    s->mv[0][i][0] = mx;
1507
                    s->mv[0][i][1] = my;
1508
                }
1509
            } else {
1510
721166
                s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1511
                /* 16x16 motion prediction */
1512
721166
                s->mv_type = MV_TYPE_16X16;
1513
721166
                ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1514
721166
                mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1515
1516
721166
                if (mx >= 0xffff)
1517
                    return AVERROR_INVALIDDATA;
1518
1519
721166
                my = ff_h263_decode_motion(s, pred_y, s->f_code);
1520
1521
721166
                if (my >= 0xffff)
1522
                    return AVERROR_INVALIDDATA;
1523
721166
                s->mv[0][0][0] = mx;
1524
721166
                s->mv[0][0][1] = my;
1525
            }
1526
        } else {
1527
92000
            s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
1528
92000
            s->mv_type                     = MV_TYPE_8X8;
1529
460000
            for (i = 0; i < 4; i++) {
1530
368000
                mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1531
368000
                mx      = ff_h263_decode_motion(s, pred_x, s->f_code);
1532
368000
                if (mx >= 0xffff)
1533
                    return AVERROR_INVALIDDATA;
1534
1535
368000
                my = ff_h263_decode_motion(s, pred_y, s->f_code);
1536
368000
                if (my >= 0xffff)
1537
                    return AVERROR_INVALIDDATA;
1538
368000
                s->mv[0][i][0] = mx;
1539
368000
                s->mv[0][i][1] = my;
1540
368000
                mot_val[0]     = mx;
1541
368000
                mot_val[1]     = my;
1542
            }
1543
        }
1544
367659
    } else if (s->pict_type == AV_PICTURE_TYPE_B) {
1545
        int modb1;   // first bit of modb
1546
        int modb2;   // second bit of modb
1547
        int mb_type;
1548
1549
240388
        s->mb_intra = 0;  // B-frames never contain intra blocks
1550
240388
        s->mcsel    = 0;  //      ...               true gmc blocks
1551
1552
240388
        if (s->mb_x == 0) {
1553
31062
            for (i = 0; i < 2; i++) {
1554
20708
                s->last_mv[i][0][0] =
1555
20708
                s->last_mv[i][0][1] =
1556
20708
                s->last_mv[i][1][0] =
1557
20708
                s->last_mv[i][1][1] = 0;
1558
            }
1559
1560
10354
            ff_thread_await_progress(&s->next_picture_ptr->tf, s->mb_y, 0);
1561
        }
1562
1563
        /* if we skipped it in the future P-frame than skip it now too */
1564
240388
        s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x];  // Note, skiptab=0 if last was GMC
1565
1566
240388
        if (s->mb_skipped) {
1567
            /* skip mb */
1568
230090
            for (i = 0; i < 6; i++)
1569
197220
                s->block_last_index[i] = -1;
1570
1571
32870
            s->mv_dir      = MV_DIR_FORWARD;
1572
32870
            s->mv_type     = MV_TYPE_16X16;
1573
32870
            s->mv[0][0][0] =
1574
32870
            s->mv[0][0][1] =
1575
32870
            s->mv[1][0][0] =
1576
32870
            s->mv[1][0][1] = 0;
1577
32870
            s->current_picture.mb_type[xy] = MB_TYPE_SKIP  |
1578
                                             MB_TYPE_16x16 |
1579
                                             MB_TYPE_L0;
1580
32870
            goto end;
1581
        }
1582
1583
207518
        modb1 = get_bits1(&s->gb);
1584
207518
        if (modb1) {
1585
            // like MB_TYPE_B_DIRECT but no vectors coded
1586
44804
            mb_type = MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1;
1587
44804
            cbp     = 0;
1588
        } else {
1589
162714
            modb2   = get_bits1(&s->gb);
1590
162714
            mb_type = get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
1591
162714
            if (mb_type < 0) {
1592
                av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
1593
                return AVERROR_INVALIDDATA;
1594
            }
1595
162714
            mb_type = mb_type_b_map[mb_type];
1596
162714
            if (modb2) {
1597
67183
                cbp = 0;
1598
            } else {
1599
95531
                s->bdsp.clear_blocks(s->block[0]);
1600
95531
                cbp = get_bits(&s->gb, 6);
1601
            }
1602
1603

162714
            if ((!IS_DIRECT(mb_type)) && cbp) {
1604
69310
                if (get_bits1(&s->gb))
1605
6992
                    ff_set_qscale(s, s->qscale + get_bits1(&s->gb) * 4 - 2);
1606
            }
1607
1608
162714
            if (!s->progressive_sequence) {
1609
                if (cbp)
1610
                    s->interlaced_dct = get_bits1(&s->gb);
1611
1612
                if (!IS_DIRECT(mb_type) && get_bits1(&s->gb)) {
1613
                    mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1614
                    mb_type &= ~MB_TYPE_16x16;
1615
1616
                    if (USES_LIST(mb_type, 0)) {
1617
                        s->field_select[0][0] = get_bits1(&s->gb);
1618
                        s->field_select[0][1] = get_bits1(&s->gb);
1619
                    }
1620
                    if (USES_LIST(mb_type, 1)) {
1621
                        s->field_select[1][0] = get_bits1(&s->gb);
1622
                        s->field_select[1][1] = get_bits1(&s->gb);
1623
                    }
1624
                }
1625
            }
1626
1627
162714
            s->mv_dir = 0;
1628
162714
            if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
1629
120541
                s->mv_type = MV_TYPE_16X16;
1630
1631
120541
                if (USES_LIST(mb_type, 0)) {
1632
82655
                    s->mv_dir = MV_DIR_FORWARD;
1633
1634
82655
                    mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1635
82655
                    my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1636
82655
                    s->last_mv[0][1][0] =
1637
82655
                    s->last_mv[0][0][0] =
1638
82655
                    s->mv[0][0][0]      = mx;
1639
82655
                    s->last_mv[0][1][1] =
1640
82655
                    s->last_mv[0][0][1] =
1641
82655
                    s->mv[0][0][1]      = my;
1642
                }
1643
1644
120541
                if (USES_LIST(mb_type, 1)) {
1645
94596
                    s->mv_dir |= MV_DIR_BACKWARD;
1646
1647
94596
                    mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1648
94596
                    my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1649
94596
                    s->last_mv[1][1][0] =
1650
94596
                    s->last_mv[1][0][0] =
1651
94596
                    s->mv[1][0][0]      = mx;
1652
94596
                    s->last_mv[1][1][1] =
1653
94596
                    s->last_mv[1][0][1] =
1654
94596
                    s->mv[1][0][1]      = my;
1655
                }
1656
42173
            } else if (!IS_DIRECT(mb_type)) {
1657
                s->mv_type = MV_TYPE_FIELD;
1658
1659
                if (USES_LIST(mb_type, 0)) {
1660
                    s->mv_dir = MV_DIR_FORWARD;
1661
1662
                    for (i = 0; i < 2; i++) {
1663
                        mx = ff_h263_decode_motion(s, s->last_mv[0][i][0], s->f_code);
1664
                        my = ff_h263_decode_motion(s, s->last_mv[0][i][1] / 2, s->f_code);
1665
                        s->last_mv[0][i][0] =
1666
                        s->mv[0][i][0]      = mx;
1667
                        s->last_mv[0][i][1] = (s->mv[0][i][1] = my) * 2;
1668
                    }
1669
                }
1670
1671
                if (USES_LIST(mb_type, 1)) {
1672
                    s->mv_dir |= MV_DIR_BACKWARD;
1673
1674
                    for (i = 0; i < 2; i++) {
1675
                        mx = ff_h263_decode_motion(s, s->last_mv[1][i][0], s->b_code);
1676
                        my = ff_h263_decode_motion(s, s->last_mv[1][i][1] / 2, s->b_code);
1677
                        s->last_mv[1][i][0] =
1678
                        s->mv[1][i][0]      = mx;
1679
                        s->last_mv[1][i][1] = (s->mv[1][i][1] = my) * 2;
1680
                    }
1681
                }
1682
            }
1683
        }
1684
1685
207518
        if (IS_DIRECT(mb_type)) {
1686
86977
            if (IS_SKIP(mb_type)) {
1687
44804
                mx =
1688
44804
                my = 0;
1689
            } else {
1690
42173
                mx = ff_h263_decode_motion(s, 0, 1);
1691
42173
                my = ff_h263_decode_motion(s, 0, 1);
1692
            }
1693
1694
86977
            s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1695
86977
            mb_type  |= ff_mpeg4_set_direct_mv(s, mx, my);
1696
        }
1697
207518
        s->current_picture.mb_type[xy] = mb_type;
1698
    } else { /* I-Frame */
1699
        do {
1700
127271
            cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
1701
127271
            if (cbpc < 0) {
1702
                av_log(s->avctx, AV_LOG_ERROR,
1703
                       "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1704
                return AVERROR_INVALIDDATA;
1705
            }
1706
127271
        } while (cbpc == 8);
1707
1708
127271
        dquant = cbpc & 4;
1709
127271
        s->mb_intra = 1;
1710
1711
154549
intra:
1712
154549
        s->ac_pred = get_bits1(&s->gb);
1713
154549
        if (s->ac_pred)
1714
552
            s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
1715
        else
1716
153997
            s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
1717
1718
154549
        cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1719
154549
        if (cbpy < 0) {
1720
            av_log(s->avctx, AV_LOG_ERROR,
1721
                   "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1722
            return AVERROR_INVALIDDATA;
1723
        }
1724
154549
        cbp = (cbpc & 3) | (cbpy << 2);
1725
1726
154549
        ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1727
1728
154549
        if (dquant)
1729
7107
            ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1730
1731
154549
        if (!s->progressive_sequence)
1732
            s->interlaced_dct = get_bits1(&s->gb);
1733
1734
154549
        s->bdsp.clear_blocks(s->block[0]);
1735
        /* decode each block */
1736
1081843
        for (i = 0; i < 6; i++) {
1737
927294
            if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 1, 0) < 0)
1738
                return AVERROR_INVALIDDATA;
1739
927294
            cbp += cbp;
1740
        }
1741
154549
        goto end;
1742
    }
1743
1744
    /* decode each block */
1745
7144788
    for (i = 0; i < 6; i++) {
1746
6124104
        if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 0, 0) < 0)
1747
            return AVERROR_INVALIDDATA;
1748
6124104
        cbp += cbp;
1749
    }
1750
1751
1020684
end:
1752
    /* per-MB end of slice check */
1753
1289056
    if (s->codec_id == AV_CODEC_ID_MPEG4) {
1754
1289056
        int next = mpeg4_is_resync(ctx);
1755
1289056
        if (next) {
1756

13412
            if        (s->mb_x + s->mb_y*s->mb_width + 1 >  next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
1757
                return AVERROR_INVALIDDATA;
1758
13412
            } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
1759
5468
                return SLICE_END;
1760
1761
7944
            if (s->pict_type == AV_PICTURE_TYPE_B) {
1762
7944
                const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
1763
7944
                ff_thread_await_progress(&s->next_picture_ptr->tf,
1764
7944
                                         (s->mb_x + delta >= s->mb_width)
1765
168
                                         ? FFMIN(s->mb_y + 1, s->mb_height - 1)
1766
                                         : s->mb_y, 0);
1767
7944
                if (s->next_picture.mbskip_table[xy + delta])
1768
7944
                    return SLICE_OK;
1769
            }
1770
1771
            return SLICE_END;
1772
        }
1773
    }
1774
1775
1275644
    return SLICE_OK;
1776
}
1777
1778
/* As per spec, studio start code search isn't the same as the old type of start code */
1779
static void next_start_code_studio(GetBitContext *gb)
1780
{
1781
    align_get_bits(gb);
1782
1783
    while (get_bits_left(gb) >= 24 && show_bits(gb, 24) != 0x1) {
1784
        get_bits(gb, 8);
1785
    }
1786
}
1787
1788
/* additional_code, vlc index */
1789
static const uint8_t ac_state_tab[22][2] =
1790
{
1791
    {0, 0},
1792
    {0, 1},
1793
    {1, 1},
1794
    {2, 1},
1795
    {3, 1},
1796
    {4, 1},
1797
    {5, 1},
1798
    {1, 2},
1799
    {2, 2},
1800
    {3, 2},
1801
    {4, 2},
1802
    {5, 2},
1803
    {6, 2},
1804
    {1, 3},
1805
    {2, 4},
1806
    {3, 5},
1807
    {4, 6},
1808
    {5, 7},
1809
    {6, 8},
1810
    {7, 9},
1811
    {8, 10},
1812
    {0, 11}
1813
};
1814
1815
static int mpeg4_decode_studio_block(MpegEncContext *s, int32_t block[64], int n)
1816
{
1817
    Mpeg4DecContext *ctx = s->avctx->priv_data;
1818
1819
    int cc, dct_dc_size, dct_diff, code, j, idx = 1, group = 0, run = 0,
1820
        additional_code_len, sign, mismatch;
1821
    const VLC *cur_vlc = &studio_intra_tab[0];
1822
    uint8_t *const scantable = s->intra_scantable.permutated;
1823
    const uint16_t *quant_matrix;
1824
    uint32_t flc;
1825
    const int min = -1 *  (1 << (s->avctx->bits_per_raw_sample + 6));
1826
    const int max =      ((1 << (s->avctx->bits_per_raw_sample + 6)) - 1);
1827
    int shift =  3 - s->dct_precision;
1828
1829
    mismatch = 1;
1830
1831
    memset(block, 0, 64 * sizeof(int32_t));
1832
1833
    if (n < 4) {
1834
        cc = 0;
1835
        dct_dc_size = get_vlc2(&s->gb, studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
1836
        quant_matrix = s->intra_matrix;
1837
    } else {
1838
        cc = (n & 1) + 1;
1839
        if (ctx->rgb)
1840
            dct_dc_size = get_vlc2(&s->gb, studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
1841
        else
1842
            dct_dc_size = get_vlc2(&s->gb, studio_chroma_dc.table, STUDIO_INTRA_BITS, 2);
1843
        quant_matrix = s->chroma_intra_matrix;
1844
    }
1845
1846
    if (dct_dc_size == 0) {
1847
        dct_diff = 0;
1848
    } else {
1849
        dct_diff = get_xbits(&s->gb, dct_dc_size);
1850
1851
        if (dct_dc_size > 8) {
1852
            if(!check_marker(s->avctx, &s->gb, "dct_dc_size > 8"))
1853
                return AVERROR_INVALIDDATA;
1854
        }
1855
1856
    }
1857
1858
    s->last_dc[cc] += dct_diff;
1859
1860
    if (s->mpeg_quant)
1861
        block[0] = s->last_dc[cc] * (8 >> s->intra_dc_precision);
1862
    else
1863
        block[0] = s->last_dc[cc] * (8 >> s->intra_dc_precision) * (8 >> s->dct_precision);
1864
    /* TODO: support mpeg_quant for AC coefficients */
1865
1866
    block[0] = av_clip(block[0], min, max);
1867
    mismatch ^= block[0];
1868
1869
    /* AC Coefficients */
1870
    while (1) {
1871
        group = get_vlc2(&s->gb, cur_vlc->table, STUDIO_INTRA_BITS, 2);
1872
1873
        if (group < 0) {
1874
            av_log(s->avctx, AV_LOG_ERROR, "illegal ac coefficient group vlc\n");
1875
            return AVERROR_INVALIDDATA;
1876
        }
1877
1878
        additional_code_len = ac_state_tab[group][0];
1879
        cur_vlc = &studio_intra_tab[ac_state_tab[group][1]];
1880
1881
        if (group == 0) {
1882
            /* End of Block */
1883
            break;
1884
        } else if (group >= 1 && group <= 6) {
1885
            /* Zero run length (Table B.47) */
1886
            run = 1 << additional_code_len;
1887
            if (additional_code_len)
1888
                run += get_bits(&s->gb, additional_code_len);
1889
            idx += run;
1890
            continue;
1891
        } else if (group >= 7 && group <= 12) {
1892
            /* Zero run length and +/-1 level (Table B.48) */
1893
            code = get_bits(&s->gb, additional_code_len);
1894
            sign = code & 1;
1895
            code >>= 1;
1896
            run = (1 << (additional_code_len - 1)) + code;
1897
            idx += run;
1898
            if (idx > 63)
1899
                return AVERROR_INVALIDDATA;
1900
            j = scantable[idx++];
1901
            block[j] = sign ? 1 : -1;
1902
        } else if (group >= 13 && group <= 20) {
1903
            /* Level value (Table B.49) */
1904
            if (idx > 63)
1905
                return AVERROR_INVALIDDATA;
1906
            j = scantable[idx++];
1907
            block[j] = get_xbits(&s->gb, additional_code_len);
1908
        } else if (group == 21) {
1909
            /* Escape */
1910
            if (idx > 63)
1911
                return AVERROR_INVALIDDATA;
1912
            j = scantable[idx++];
1913
            additional_code_len = s->avctx->bits_per_raw_sample + s->dct_precision + 4;
1914
            flc = get_bits(&s->gb, additional_code_len);
1915
            if (flc >> (additional_code_len-1))
1916
                block[j] = -1 * (( flc ^ ((1 << additional_code_len) -1)) + 1);
1917
            else
1918
                block[j] = flc;
1919
        }
1920
        block[j] = ((block[j] * quant_matrix[j] * s->qscale) * (1 << shift)) / 16;
1921
        block[j] = av_clip(block[j], min, max);
1922
        mismatch ^= block[j];
1923
    }
1924
1925
    block[63] ^= mismatch & 1;
1926
1927
    return 0;
1928
}
1929
1930
static int mpeg4_decode_dpcm_macroblock(MpegEncContext *s, int16_t macroblock[256], int n)
1931
{
1932
    int i, j, w, h, idx = 0;
1933
    int block_mean, rice_parameter, rice_prefix_code, rice_suffix_code,
1934
        dpcm_residual, left, top, topleft, min_left_top, max_left_top, p, p2, output;
1935
    h = 16 >> (n ? s->chroma_y_shift : 0);
1936
    w = 16 >> (n ? s->chroma_x_shift : 0);
1937
1938
    block_mean = get_bits(&s->gb, s->avctx->bits_per_raw_sample);
1939
    if (block_mean == 0){
1940
        av_log(s->avctx, AV_LOG_ERROR, "Forbidden block_mean\n");
1941
        return AVERROR_INVALIDDATA;
1942
    }
1943
    s->last_dc[n] = block_mean * (1 << (s->dct_precision + s->intra_dc_precision));
1944
1945
    rice_parameter = get_bits(&s->gb, 4);
1946
    if (rice_parameter == 0) {
1947
        av_log(s->avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n");
1948
        return AVERROR_INVALIDDATA;
1949
    }
1950
1951
    if (rice_parameter == 15)
1952
        rice_parameter = 0;
1953
1954
    if (rice_parameter > 11) {
1955
        av_log(s->avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n");
1956
        return AVERROR_INVALIDDATA;
1957
    }
1958
1959
    for (i = 0; i < h; i++) {
1960
        output = 1 << (s->avctx->bits_per_raw_sample - 1);
1961
        top = 1 << (s->avctx->bits_per_raw_sample - 1);
1962
1963
        for (j = 0; j < w; j++) {
1964
            left = output;
1965
            topleft = top;
1966
1967
            rice_prefix_code = get_unary(&s->gb, 1, 12);
1968
1969
            /* Escape */
1970
            if (rice_prefix_code == 11)
1971
                dpcm_residual = get_bits(&s->gb, s->avctx->bits_per_raw_sample);
1972
            else {
1973
                if (rice_prefix_code == 12) {
1974
                    av_log(s->avctx, AV_LOG_ERROR, "Forbidden rice_prefix_code\n");
1975
                    return AVERROR_INVALIDDATA;
1976
                }
1977
                rice_suffix_code = get_bitsz(&s->gb, rice_parameter);
1978
                dpcm_residual = (rice_prefix_code << rice_parameter) + rice_suffix_code;
1979
            }
1980
1981
            /* Map to a signed residual */
1982
            if (dpcm_residual & 1)
1983
                dpcm_residual = (-1 * dpcm_residual) >> 1;
1984
            else
1985
                dpcm_residual = (dpcm_residual >> 1);
1986
1987
            if (i != 0)
1988
                top = macroblock[idx-w];
1989
1990
            p = left + top - topleft;
1991
            min_left_top = FFMIN(left, top);
1992
            if (p < min_left_top)
1993
                p = min_left_top;
1994
1995
            max_left_top = FFMAX(left, top);
1996
            if (p > max_left_top)
1997
                p = max_left_top;
1998
1999
            p2 = (FFMIN(min_left_top, topleft) + FFMAX(max_left_top, topleft)) >> 1;
2000
            if (p2 == p)
2001
                p2 = block_mean;
2002
2003
            if (p2 > p)
2004
                dpcm_residual *= -1;
2005
2006
            macroblock[idx++] = output = (dpcm_residual + p) & ((1 << s->avctx->bits_per_raw_sample) - 1);
2007
        }
2008
    }
2009
2010
    return 0;
2011
}
2012
2013
static int mpeg4_decode_studio_mb(MpegEncContext *s, int16_t block_[12][64])
2014
{
2015
    int i;
2016
2017
    s->dpcm_direction = 0;
2018
2019
    /* StudioMacroblock */
2020
    /* Assumes I-VOP */
2021
    s->mb_intra = 1;
2022
    if (get_bits1(&s->gb)) { /* compression_mode */
2023
        /* DCT */
2024
        /* macroblock_type, 1 or 2-bit VLC */
2025
        if (!get_bits1(&s->gb)) {
2026
            skip_bits1(&s->gb);
2027
            s->qscale = mpeg_get_qscale(s);
2028
        }
2029
2030
        for (i = 0; i < mpeg4_block_count[s->chroma_format]; i++) {
2031
            if (mpeg4_decode_studio_block(s, (*s->block32)[i], i) < 0)
2032
                return AVERROR_INVALIDDATA;
2033
        }
2034
    } else {
2035
        /* DPCM */
2036
        check_marker(s->avctx, &s->gb, "DPCM block start");
2037
        s->dpcm_direction = get_bits1(&s->gb) ? -1 : 1;
2038
        for (i = 0; i < 3; i++) {
2039
            if (mpeg4_decode_dpcm_macroblock(s, (*s->dpcm_macroblock)[i], i) < 0)
2040
                return AVERROR_INVALIDDATA;
2041
        }
2042
    }
2043
2044
    if (get_bits_left(&s->gb) >= 24 && show_bits(&s->gb, 23) == 0) {
2045
        next_start_code_studio(&s->gb);
2046
        return SLICE_END;
2047
    }
2048
2049
    //vcon-stp9L1.bits (first frame)
2050
    if (get_bits_left(&s->gb) == 0)
2051
        return SLICE_END;
2052
2053
    //vcon-stp2L1.bits, vcon-stp3L1.bits, vcon-stp6L1.bits, vcon-stp7L1.bits, vcon-stp8L1.bits, vcon-stp10L1.bits (first frame)
2054
    if (get_bits_left(&s->gb) < 8U && show_bits(&s->gb, get_bits_left(&s->gb)) == 0)
2055
        return SLICE_END;
2056
2057
    return SLICE_OK;
2058
}
2059
2060
955
static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
2061
{
2062
    int hours, minutes, seconds;
2063
2064
955
    if (!show_bits(gb, 23)) {
2065
        av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
2066
        return AVERROR_INVALIDDATA;
2067
    }
2068
2069
955
    hours   = get_bits(gb, 5);
2070
955
    minutes = get_bits(gb, 6);
2071
955
    check_marker(s->avctx, gb, "in gop_header");
2072
955
    seconds = get_bits(gb, 6);
2073
2074
955
    s->time_base = seconds + 60*(minutes + 60*hours);
2075
2076
955
    skip_bits1(gb);
2077
955
    skip_bits1(gb);
2078
2079
955
    return 0;
2080
}
2081
2082
1218
static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb, int *profile, int *level)
2083
{
2084
2085
1218
    *profile = get_bits(gb, 4);
2086
1218
    *level   = get_bits(gb, 4);
2087
2088
    // for Simple profile, level 0
2089

1218
    if (*profile == 0 && *level == 8) {
2090
        *level = 0;
2091
    }
2092
2093
1218
    return 0;
2094
}
2095
2096
1218
static int mpeg4_decode_visual_object(MpegEncContext *s, GetBitContext *gb)
2097
{
2098
    int visual_object_type;
2099
1218
    int is_visual_object_identifier = get_bits1(gb);
2100
2101
1218
    if (is_visual_object_identifier) {
2102
1184
        skip_bits(gb, 4+3);
2103
    }
2104
1218
    visual_object_type = get_bits(gb, 4);
2105
2106

1218
    if (visual_object_type == VOT_VIDEO_ID ||
2107
        visual_object_type == VOT_STILL_TEXTURE_ID) {
2108
1218
        int video_signal_type = get_bits1(gb);
2109
1218
        if (video_signal_type) {
2110
            int video_range, color_description;
2111
            skip_bits(gb, 3); // video_format
2112
            video_range = get_bits1(gb);
2113
            color_description = get_bits1(gb);
2114
2115
            s->avctx->color_range = video_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
2116
2117
            if (color_description) {
2118
                s->avctx->color_primaries = get_bits(gb, 8);
2119
                s->avctx->color_trc       = get_bits(gb, 8);
2120
                s->avctx->colorspace      = get_bits(gb, 8);
2121
            }
2122
        }
2123
    }
2124
2125
1218
    return 0;
2126
}
2127
2128
23
static void mpeg4_load_default_matrices(MpegEncContext *s)
2129
{
2130
    int i, v;
2131
2132
    /* load default matrices */
2133
1495
    for (i = 0; i < 64; i++) {
2134
1472
        int j = s->idsp.idct_permutation[i];
2135
1472
        v = ff_mpeg4_default_intra_matrix[i];
2136
1472
        s->intra_matrix[j]        = v;
2137
1472
        s->chroma_intra_matrix[j] = v;
2138
2139
1472
        v = ff_mpeg4_default_non_intra_matrix[i];
2140
1472
        s->inter_matrix[j]        = v;
2141
1472
        s->chroma_inter_matrix[j] = v;
2142
    }
2143
23
}
2144
2145
static int read_quant_matrix_ext(MpegEncContext *s, GetBitContext *gb)
2146
{
2147
    int i, j, v;
2148
2149
    if (get_bits1(gb)) {
2150
        if (get_bits_left(gb) < 64*8)
2151
            return AVERROR_INVALIDDATA;
2152
        /* intra_quantiser_matrix */
2153
        for (i = 0; i < 64; i++) {
2154
            v = get_bits(gb, 8);
2155
            j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2156
            s->intra_matrix[j]        = v;
2157
            s->chroma_intra_matrix[j] = v;
2158
        }
2159
    }
2160
2161
    if (get_bits1(gb)) {
2162
        if (get_bits_left(gb) < 64*8)
2163
            return AVERROR_INVALIDDATA;
2164
        /* non_intra_quantiser_matrix */
2165
        for (i = 0; i < 64; i++) {
2166
            get_bits(gb, 8);
2167
        }
2168
    }
2169
2170
    if (get_bits1(gb)) {
2171
        if (get_bits_left(gb) < 64*8)
2172
            return AVERROR_INVALIDDATA;
2173
        /* chroma_intra_quantiser_matrix */
2174
        for (i = 0; i < 64; i++) {
2175
            v = get_bits(gb, 8);
2176
            j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2177
            s->chroma_intra_matrix[j] = v;
2178
        }
2179
    }
2180
2181
    if (get_bits1(gb)) {
2182
        if (get_bits_left(gb) < 64*8)
2183
            return AVERROR_INVALIDDATA;
2184
        /* chroma_non_intra_quantiser_matrix */
2185
        for (i = 0; i < 64; i++) {
2186
            get_bits(gb, 8);
2187
        }
2188
    }
2189
2190
    next_start_code_studio(gb);
2191
    return 0;
2192
}
2193
2194
static void extension_and_user_data(MpegEncContext *s, GetBitContext *gb, int id)
2195
{
2196
    uint32_t startcode;
2197
    uint8_t extension_type;
2198
2199
    startcode = show_bits_long(gb, 32);
2200
    if (startcode == USER_DATA_STARTCODE || startcode == EXT_STARTCODE) {
2201
2202
        if ((id == 2 || id == 4) && startcode == EXT_STARTCODE) {
2203
            skip_bits_long(gb, 32);
2204
            extension_type = get_bits(gb, 4);
2205
            if (extension_type == QUANT_MATRIX_EXT_ID)
2206
                read_quant_matrix_ext(s, gb);
2207
        }
2208
    }
2209
}
2210
2211
static int decode_studio_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2212
{
2213
    MpegEncContext *s = &ctx->m;
2214
    int width, height;
2215
    int bits_per_raw_sample;
2216
    int rgb, chroma_format;
2217
2218
    // random_accessible_vol and video_object_type_indication have already
2219
    // been read by the caller decode_vol_header()
2220
    skip_bits(gb, 4); /* video_object_layer_verid */
2221
    ctx->shape = get_bits(gb, 2); /* video_object_layer_shape */
2222
    skip_bits(gb, 4); /* video_object_layer_shape_extension */
2223
    skip_bits1(gb); /* progressive_sequence */
2224
    if (ctx->shape != RECT_SHAPE) {
2225
        avpriv_request_sample(s->avctx, "MPEG-4 Studio profile non rectangular shape");
2226
        return AVERROR_PATCHWELCOME;
2227
    }
2228
    if (ctx->shape != BIN_ONLY_SHAPE) {
2229
        rgb = get_bits1(gb); /* rgb_components */
2230
        chroma_format = get_bits(gb, 2); /* chroma_format */
2231
        if (!chroma_format || chroma_format == CHROMA_420 || (rgb && chroma_format == CHROMA_422)) {
2232
            av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
2233
            return AVERROR_INVALIDDATA;
2234
        }
2235
2236
        bits_per_raw_sample = get_bits(gb, 4); /* bit_depth */
2237
        if (bits_per_raw_sample == 10) {
2238
            if (rgb) {
2239
                s->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
2240
            } else {
2241
                s->avctx->pix_fmt = chroma_format == CHROMA_422 ? AV_PIX_FMT_YUV422P10 : AV_PIX_FMT_YUV444P10;
2242
            }
2243
        } else {
2244
            avpriv_request_sample(s->avctx, "MPEG-4 Studio profile bit-depth %u", bits_per_raw_sample);
2245
            return AVERROR_PATCHWELCOME;
2246
        }
2247
        if (rgb != ctx->rgb || s->chroma_format != chroma_format)
2248
            s->context_reinit = 1;
2249
        s->avctx->bits_per_raw_sample = bits_per_raw_sample;
2250
        ctx->rgb = rgb;
2251
        s->chroma_format = chroma_format;
2252
    }
2253
    if (ctx->shape == RECT_SHAPE) {
2254
        check_marker(s->avctx, gb, "before video_object_layer_width");
2255
        width  = get_bits(gb, 14); /* video_object_layer_width */
2256
        check_marker(s->avctx, gb, "before video_object_layer_height");
2257
        height = get_bits(gb, 14); /* video_object_layer_height */
2258
        check_marker(s->avctx, gb, "after video_object_layer_height");
2259
2260
        /* Do the same check as non-studio profile */
2261
        if (width && height) {
2262
            if (s->width && s->height &&
2263
                (s->width != width || s->height != height))
2264
                s->context_reinit = 1;
2265
            s->width  = width;
2266
            s->height = height;
2267
        }
2268
    }
2269
    s->aspect_ratio_info = get_bits(gb, 4);
2270
    if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
2271
        s->avctx->sample_aspect_ratio.num = get_bits(gb, 8);  // par_width
2272
        s->avctx->sample_aspect_ratio.den = get_bits(gb, 8);  // par_height
2273
    } else {
2274
        s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
2275
    }
2276
    skip_bits(gb, 4); /* frame_rate_code */
2277
    skip_bits(gb, 15); /* first_half_bit_rate */
2278
    check_marker(s->avctx, gb, "after first_half_bit_rate");
2279
    skip_bits(gb, 15); /* latter_half_bit_rate */
2280
    check_marker(s->avctx, gb, "after latter_half_bit_rate");
2281
    skip_bits(gb, 15); /* first_half_vbv_buffer_size */
2282
    check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2283
    skip_bits(gb, 3); /* latter_half_vbv_buffer_size */
2284
    skip_bits(gb, 11); /* first_half_vbv_buffer_size */
2285
    check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2286
    skip_bits(gb, 15); /* latter_half_vbv_occupancy */
2287
    check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
2288
    s->low_delay  = get_bits1(gb);
2289
    s->mpeg_quant = get_bits1(gb); /* mpeg2_stream */
2290
2291
    next_start_code_studio(gb);
2292
    extension_and_user_data(s, gb, 2);
2293
2294
    return 0;
2295
}
2296
2297
1250
static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2298
{
2299
1250
    MpegEncContext *s = &ctx->m;
2300
    int width, height, vo_ver_id;
2301
2302
    /* vol header */
2303
1250
    skip_bits(gb, 1);                   /* random access */
2304
1250
    s->vo_type = get_bits(gb, 8);
2305
2306
    /* If we are in studio profile (per vo_type), check if its all consistent
2307
     * and if so continue pass control to decode_studio_vol_header().
2308
     * elIf something is inconsistent, error out
2309
     * else continue with (non studio) vol header decpoding.
2310
     */
2311
1250
    if (s->vo_type == CORE_STUDIO_VO_TYPE ||
2312
1250
        s->vo_type == SIMPLE_STUDIO_VO_TYPE) {
2313
        if (s->avctx->profile != FF_PROFILE_UNKNOWN && s->avctx->profile != FF_PROFILE_MPEG4_SIMPLE_STUDIO)
2314
            return AVERROR_INVALIDDATA;
2315
        s->studio_profile = 1;
2316
        s->avctx->profile = FF_PROFILE_MPEG4_SIMPLE_STUDIO;
2317
        return decode_studio_vol_header(ctx, gb);
2318
1250
    } else if (s->studio_profile) {
2319
        return AVERROR_PATCHWELCOME;
2320
    }
2321
2322
1250
    if (get_bits1(gb) != 0) {           /* is_ol_id */
2323
1214
        vo_ver_id = get_bits(gb, 4);    /* vo_ver_id */
2324
1214
        skip_bits(gb, 3);               /* vo_priority */
2325
    } else {
2326
36
        vo_ver_id = 1;
2327
    }
2328
1250
    s->aspect_ratio_info = get_bits(gb, 4);
2329
1250
    if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
2330
1
        s->avctx->sample_aspect_ratio.num = get_bits(gb, 8);  // par_width
2331
1
        s->avctx->sample_aspect_ratio.den = get_bits(gb, 8);  // par_height
2332
    } else {
2333
1249
        s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
2334
    }
2335
2336
1250
    if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
2337
1210
        int chroma_format = get_bits(gb, 2);
2338
1210
        if (chroma_format != CHROMA_420)
2339
            av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
2340
2341
1210
        s->low_delay = get_bits1(gb);
2342
1210
        if (get_bits1(gb)) {    /* vbv parameters */
2343
            get_bits(gb, 15);   /* first_half_bitrate */
2344
            check_marker(s->avctx, gb, "after first_half_bitrate");
2345
            get_bits(gb, 15);   /* latter_half_bitrate */
2346
            check_marker(s->avctx, gb, "after latter_half_bitrate");
2347
            get_bits(gb, 15);   /* first_half_vbv_buffer_size */
2348
            check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2349
            get_bits(gb, 3);    /* latter_half_vbv_buffer_size */
2350
            get_bits(gb, 11);   /* first_half_vbv_occupancy */
2351
            check_marker(s->avctx, gb, "after first_half_vbv_occupancy");
2352
            get_bits(gb, 15);   /* latter_half_vbv_occupancy */
2353
            check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
2354
        }
2355
    } else {
2356
        /* is setting low delay flag only once the smartest thing to do?
2357
         * low delay detection will not be overridden. */
2358
40
        if (s->picture_number == 0) {
2359
11
            switch(s->vo_type) {
2360
6
            case SIMPLE_VO_TYPE:
2361
            case ADV_SIMPLE_VO_TYPE:
2362
6
                s->low_delay = 1;
2363
6
                break;
2364
5
            default:
2365
5
                s->low_delay = 0;
2366
            }
2367
29
        }
2368
    }
2369
2370
1250
    ctx->shape = get_bits(gb, 2); /* vol shape */
2371
1250
    if (ctx->shape != RECT_SHAPE)
2372
        av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
2373

1250
    if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
2374
        av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
2375
        skip_bits(gb, 4);  /* video_object_layer_shape_extension */
2376
    }
2377
2378
1250
    check_marker(s->avctx, gb, "before time_increment_resolution");
2379
2380
1250
    s->avctx->framerate.num = get_bits(gb, 16);
2381
1250
    if (!s->avctx->framerate.num) {
2382
        av_log(s->avctx, AV_LOG_ERROR, "framerate==0\n");
2383
        return AVERROR_INVALIDDATA;
2384
    }
2385
2386
1250
    ctx->time_increment_bits = av_log2(s->avctx->framerate.num - 1) + 1;
2387
1250
    if (ctx->time_increment_bits < 1)
2388
        ctx->time_increment_bits = 1;
2389
2390
1250
    check_marker(s->avctx, gb, "before fixed_vop_rate");
2391
2392
1250
    if (get_bits1(gb) != 0)     /* fixed_vop_rate  */
2393
26
        s->avctx->framerate.den = get_bits(gb, ctx->time_increment_bits);
2394
    else
2395
1224
        s->avctx->framerate.den = 1;
2396
2397
1250
    s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
2398
2399
1250
    ctx->t_frame = 0;
2400
2401
1250
    if (ctx->shape != BIN_ONLY_SHAPE) {
2402
1250
        if (ctx->shape == RECT_SHAPE) {
2403
1250
            check_marker(s->avctx, gb, "before width");
2404
1250
            width = get_bits(gb, 13);
2405
1250
            check_marker(s->avctx, gb, "before height");
2406
1250
            height = get_bits(gb, 13);
2407
1250
            check_marker(s->avctx, gb, "after height");
2408

1250
            if (width && height &&  /* they should be non zero but who knows */
2409

1250
                !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
2410

1250
                if (s->width && s->height &&
2411

914
                    (s->width != width || s->height != height))
2412
20
                    s->context_reinit = 1;
2413
1250
                s->width  = width;
2414
1250
                s->height = height;
2415
            }
2416
        }
2417
2418
1250
        s->progressive_sequence  =
2419
1250
        s->progressive_frame     = get_bits1(gb) ^ 1;
2420
1250
        s->interlaced_dct        = 0;
2421

1250
        if (!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
2422
            av_log(s->avctx, AV_LOG_INFO,           /* OBMC Disable */
2423
                   "MPEG-4 OBMC not supported (very likely buggy encoder)\n");
2424
1250
        if (vo_ver_id == 1)
2425
745
            ctx->vol_sprite_usage = get_bits1(gb);    /* vol_sprite_usage */
2426
        else
2427
505
            ctx->vol_sprite_usage = get_bits(gb, 2);  /* vol_sprite_usage */
2428
2429
1250
        if (ctx->vol_sprite_usage == STATIC_SPRITE)
2430
            av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
2431
1250
        if (ctx->vol_sprite_usage == STATIC_SPRITE ||
2432
1250
            ctx->vol_sprite_usage == GMC_SPRITE) {
2433
            if (ctx->vol_sprite_usage == STATIC_SPRITE) {
2434
                skip_bits(gb, 13); // sprite_width
2435
                check_marker(s->avctx, gb, "after sprite_width");
2436
                skip_bits(gb, 13); // sprite_height
2437
                check_marker(s->avctx, gb, "after sprite_height");
2438
                skip_bits(gb, 13); // sprite_left
2439
                check_marker(s->avctx, gb, "after sprite_left");
2440
                skip_bits(gb, 13); // sprite_top
2441
                check_marker(s->avctx, gb, "after sprite_top");
2442
            }
2443
            ctx->num_sprite_warping_points = get_bits(gb, 6);
2444
            if (ctx->num_sprite_warping_points > 3) {
2445
                av_log(s->avctx, AV_LOG_ERROR,
2446
                       "%d sprite_warping_points\n",
2447
                       ctx->num_sprite_warping_points);
2448
                ctx->num_sprite_warping_points = 0;
2449
                return AVERROR_INVALIDDATA;
2450
            }
2451
            s->sprite_warping_accuracy  = get_bits(gb, 2);
2452
            ctx->sprite_brightness_change = get_bits1(gb);
2453
            if (ctx->vol_sprite_usage == STATIC_SPRITE)
2454
                skip_bits1(gb); // low_latency_sprite
2455
        }
2456
        // FIXME sadct disable bit if verid!=1 && shape not rect
2457
2458
1250
        if (get_bits1(gb) == 1) {                   /* not_8_bit */
2459
            s->quant_precision = get_bits(gb, 4);   /* quant_precision */
2460
            if (get_bits(gb, 4) != 8)               /* bits_per_pixel */
2461
                av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
2462
            if (s->quant_precision != 5)
2463
                av_log(s->avctx, AV_LOG_ERROR,
2464
                       "quant precision %d\n", s->quant_precision);
2465
            if (s->quant_precision<3 || s->quant_precision>9) {
2466
                s->quant_precision = 5;
2467
            }
2468
        } else {
2469
1250
            s->quant_precision = 5;
2470
        }
2471
2472
        // FIXME a bunch of grayscale shape things
2473
2474
1250
        if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
2475
            int i, v;
2476
2477
23
            mpeg4_load_default_matrices(s);
2478
2479
            /* load custom intra matrix */
2480
23
            if (get_bits1(gb)) {
2481
12
                int last = 0;
2482
360
                for (i = 0; i < 64; i++) {
2483
                    int j;
2484
360
                    if (get_bits_left(gb) < 8) {
2485
                        av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2486
                        return AVERROR_INVALIDDATA;
2487
                    }
2488
360
                    v = get_bits(gb, 8);
2489
360
                    if (v == 0)
2490
12
                        break;
2491
2492
348
                    last = v;
2493
348
                    j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2494
348
                    s->intra_matrix[j]        = last;
2495
348
                    s->chroma_intra_matrix[j] = last;
2496
                }
2497
2498
                /* replicate last value */
2499
432
                for (; i < 64; i++) {
2500
420
                    int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2501
420
                    s->intra_matrix[j]        = last;
2502
420
                    s->chroma_intra_matrix[j] = last;
2503
                }
2504
            }
2505
2506
            /* load custom non intra matrix */
2507
23
            if (get_bits1(gb)) {
2508
12
                int last = 0;
2509
360
                for (i = 0; i < 64; i++) {
2510
                    int j;
2511
360
                    if (get_bits_left(gb) < 8) {
2512
                        av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2513
                        return AVERROR_INVALIDDATA;
2514
                    }
2515
360
                    v = get_bits(gb, 8);
2516
360
                    if (v == 0)
2517
12
                        break;
2518
2519
348
                    last = v;
2520
348
                    j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2521
348
                    s->inter_matrix[j]        = v;
2522
348
                    s->chroma_inter_matrix[j] = v;
2523
                }
2524
2525
                /* replicate last value */
2526
432
                for (; i < 64; i++) {
2527
420
                    int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2528
420
                    s->inter_matrix[j]        = last;
2529
420
                    s->chroma_inter_matrix[j] = last;
2530
                }
2531
            }
2532
2533
            // FIXME a bunch of grayscale shape things
2534
        }
2535
2536
1250
        if (vo_ver_id != 1)
2537
505
            s->quarter_sample = get_bits1(gb);
2538
        else
2539
745
            s->quarter_sample = 0;
2540
2541
1250
        if (get_bits_left(gb) < 4) {
2542
            av_log(s->avctx, AV_LOG_ERROR, "VOL Header truncated\n");
2543
            return AVERROR_INVALIDDATA;
2544
        }
2545
2546
1250
        if (!get_bits1(gb)) {
2547
            int pos               = get_bits_count(gb);
2548
            int estimation_method = get_bits(gb, 2);
2549
            if (estimation_method < 2) {
2550
                if (!get_bits1(gb)) {
2551
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* opaque */
2552
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* transparent */
2553
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* intra_cae */
2554
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* inter_cae */
2555
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* no_update */
2556
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* upsampling */
2557
                }
2558
                if (!get_bits1(gb)) {
2559
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* intra_blocks */
2560
                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* inter_blocks */
2561
                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* inter4v_blocks */
2562
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* not coded blocks */
2563
                }
2564
                if (!check_marker(s->avctx, gb, "in complexity estimation part 1")) {
2565
                    skip_bits_long(gb, pos - get_bits_count(gb));
2566
                    goto no_cplx_est;
2567
                }
2568
                if (!get_bits1(gb)) {
2569
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* dct_coeffs */
2570
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* dct_lines */
2571
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* vlc_syms */
2572
                    ctx->cplx_estimation_trash_i += 4 * get_bits1(gb);  /* vlc_bits */
2573
                }
2574
                if (!get_bits1(gb)) {
2575
                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* apm */
2576
                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* npm */
2577
                    ctx->cplx_estimation_trash_b += 8 * get_bits1(gb);  /* interpolate_mc_q */
2578
                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* forwback_mc_q */
2579
                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* halfpel2 */
2580
                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* halfpel4 */
2581
                }
2582
                if (!check_marker(s->avctx, gb, "in complexity estimation part 2")) {
2583
                    skip_bits_long(gb, pos - get_bits_count(gb));
2584
                    goto no_cplx_est;
2585
                }
2586
                if (estimation_method == 1) {
2587
                    ctx->cplx_estimation_trash_i += 8 * get_bits1(gb);  /* sadct */
2588
                    ctx->cplx_estimation_trash_p += 8 * get_bits1(gb);  /* qpel */
2589
                }
2590
            } else
2591
                av_log(s->avctx, AV_LOG_ERROR,
2592
                       "Invalid Complexity estimation method %d\n",
2593
                       estimation_method);
2594
        } else {
2595
2596
1250
no_cplx_est:
2597
1250
            ctx->cplx_estimation_trash_i =
2598
1250
            ctx->cplx_estimation_trash_p =
2599
1250
            ctx->cplx_estimation_trash_b = 0;
2600
        }
2601
2602
1250
        ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
2603
2604
1250
        s->data_partitioning = get_bits1(gb);
2605
1250
        if (s->data_partitioning)
2606
288
            ctx->rvlc = get_bits1(gb);
2607
2608
1250
        if (vo_ver_id != 1) {
2609
505
            ctx->new_pred = get_bits1(gb);
2610
505
            if (ctx->new_pred) {
2611
                av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
2612
                skip_bits(gb, 2); /* requested upstream message type */
2613
                skip_bits1(gb);   /* newpred segment type */
2614
            }
2615
505
            if (get_bits1(gb)) // reduced_res_vop
2616
                av_log(s->avctx, AV_LOG_ERROR,
2617
                       "reduced resolution VOP not supported\n");
2618
        } else {
2619
745
            ctx->new_pred = 0;
2620
        }
2621
2622
1250
        ctx->scalability = get_bits1(gb);
2623
2624
1250
        if (ctx->scalability) {
2625
            GetBitContext bak = *gb;
2626
            int h_sampling_factor_n;
2627
            int h_sampling_factor_m;
2628
            int v_sampling_factor_n;
2629
            int v_sampling_factor_m;
2630
2631
            skip_bits1(gb);    // hierarchy_type
2632
            skip_bits(gb, 4);  /* ref_layer_id */
2633
            skip_bits1(gb);    /* ref_layer_sampling_dir */
2634
            h_sampling_factor_n = get_bits(gb, 5);
2635
            h_sampling_factor_m = get_bits(gb, 5);
2636
            v_sampling_factor_n = get_bits(gb, 5);
2637
            v_sampling_factor_m = get_bits(gb, 5);
2638
            ctx->enhancement_type = get_bits1(gb);
2639
2640
            if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
2641
                v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
2642
                /* illegal scalability header (VERY broken encoder),
2643
                 * trying to workaround */
2644
                ctx->scalability = 0;
2645
                *gb            = bak;
2646
            } else
2647
                av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
2648
2649
            // bin shape stuff FIXME
2650
        }
2651
    }
2652
2653
1250
    if (s->avctx->debug&FF_DEBUG_PICT_INFO) {
2654
        av_log(s->avctx, AV_LOG_DEBUG, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d, low_delay:%d  %s%s%s%s\n",
2655
               s->avctx->framerate.den, s->avctx->framerate.num,
2656
               ctx->time_increment_bits,
2657
               s->quant_precision,
2658
               s->progressive_sequence,
2659
               s->low_delay,
2660
               ctx->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
2661
               s->data_partitioning ? "partition " : "", ctx->rvlc ? "rvlc " : ""
2662
        );
2663
    }
2664
2665
1250
    return 0;
2666
}
2667
2668
/**
2669
 * Decode the user data stuff in the header.
2670
 * Also initializes divx/xvid/lavc_version/build.
2671
 */
2672
76
static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
2673
{
2674
76
    MpegEncContext *s = &ctx->m;
2675
    char buf[256];
2676
    int i;
2677
    int e;
2678
76
    int ver = 0, build = 0, ver2 = 0, ver3 = 0;
2679
    char last;
2680
2681

747
    for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
2682
730
        if (show_bits(gb, 23) == 0)
2683
59
            break;
2684
671
        buf[i] = get_bits(gb, 8);
2685
    }
2686
76
    buf[i] = 0;
2687
2688
    /* divx detection */
2689
76
    e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
2690
76
    if (e < 2)
2691
76
        e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
2692
76
    if (e >= 2) {
2693
11
        ctx->divx_version = ver;
2694
11
        ctx->divx_build   = build;
2695

11
        s->divx_packed  = e == 3 && last == 'p';
2696
    }
2697
2698
    /* libavcodec detection */
2699
76
    e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
2700
76
    if (e != 4)
2701
76
        e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
2702
76
    if (e != 4) {
2703
76
        e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
2704
76
        if (e > 1) {
2705

11
            if (ver > 0xFFU || ver2 > 0xFFU || ver3 > 0xFFU) {
2706
                av_log(s->avctx, AV_LOG_WARNING,
2707
                     "Unknown Lavc version string encountered, %d.%d.%d; "
2708
                     "clamping sub-version values to 8-bits.\n",
2709
                     ver, ver2, ver3);
2710
            }
2711
11
            build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF);
2712
        }
2713
    }
2714
76
    if (e != 4) {
2715
65
        if (strcmp(buf, "ffmpeg") == 0)
2716
            ctx->lavc_build = 4600;
2717
    }
2718
76
    if (e == 4)
2719
11
        ctx->lavc_build = build;
2720
2721
    /* Xvid detection */
2722
76
    e = sscanf(buf, "XviD%d", &build);
2723
76
    if (e == 1)
2724
26
        ctx->xvid_build = build;
2725
2726
76
    return 0;
2727
}
2728
2729
3474
int ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
2730
{
2731
3474
    Mpeg4DecContext *ctx = avctx->priv_data;
2732
3474
    MpegEncContext *s = &ctx->m;
2733
2734

3474
    if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
2735
3323
        if (s->codec_tag        == AV_RL32("XVID") ||
2736
3323
            s->codec_tag        == AV_RL32("XVIX") ||
2737
3323
            s->codec_tag        == AV_RL32("RMP4") ||
2738
3323
            s->codec_tag        == AV_RL32("ZMP4") ||
2739
3323
            s->codec_tag        == AV_RL32("SIPP"))
2740
            ctx->xvid_build = 0;
2741
    }
2742
2743

3474
    if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
2744

3323
        if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
2745
            ctx->vol_control_parameters == 0)
2746
            ctx->divx_version = 400;  // divx 4
2747
2748

3474
    if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
2749
4
        ctx->divx_version =
2750
4
        ctx->divx_build   = -1;
2751
    }
2752
2753
3474
    if (s->workaround_bugs & FF_BUG_AUTODETECT) {
2754
3474
        if (s->codec_tag == AV_RL32("XVIX"))
2755
            s->workaround_bugs |= FF_BUG_XVID_ILACE;
2756
2757
3474
        if (s->codec_tag == AV_RL32("UMP4"))
2758
            s->workaround_bugs |= FF_BUG_UMP4;
2759
2760

3474
        if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
2761
            s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2762
2763

3474
        if (ctx->divx_version > 502 && ctx->divx_build < 1814)
2764
            s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
2765
2766
3474
        if (ctx->xvid_build <= 3U)
2767
            s->padding_bug_score = 256 * 256 * 256 * 64;
2768
2769
3474
        if (ctx->xvid_build <= 1U)
2770
            s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2771
2772
3474
        if (ctx->xvid_build <= 12U)
2773
            s->workaround_bugs |= FF_BUG_EDGE;
2774
2775
3474
        if (ctx->xvid_build <= 32U)
2776
            s->workaround_bugs |= FF_BUG_DC_CLIP;
2777
2778
#define SET_QPEL_FUNC(postfix1, postfix2)                           \
2779
    s->qdsp.put_        ## postfix1 = ff_put_        ## postfix2;   \
2780
    s->qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;   \
2781
    s->qdsp.avg_        ## postfix1 = ff_avg_        ## postfix2;
2782
2783
3474
        if (ctx->lavc_build < 4653U)
2784
            s->workaround_bugs |= FF_BUG_STD_QPEL;
2785
2786
3474
        if (ctx->lavc_build < 4655U)
2787
            s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2788
2789
3474
        if (ctx->lavc_build < 4670U)
2790
            s->workaround_bugs |= FF_BUG_EDGE;
2791
2792
3474
        if (ctx->lavc_build <= 4712U)
2793
            s->workaround_bugs |= FF_BUG_DC_CLIP;
2794
2795
3474
        if ((ctx->lavc_build&0xFF) >= 100) {
2796

3474
            if (ctx->lavc_build > 3621476 && ctx->lavc_build < 3752552 &&
2797
               (ctx->lavc_build < 3752037 || ctx->lavc_build > 3752191) // 3.2.1+
2798
            )
2799
                s->workaround_bugs |= FF_BUG_IEDGE;
2800
        }
2801
2802
3474
        if (ctx->divx_version >= 0)
2803
            s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2804

3474
        if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
2805
            s->padding_bug_score = 256 * 256 * 256 * 64;
2806
2807
3474
        if (ctx->divx_version < 500U)
2808
            s->workaround_bugs |= FF_BUG_EDGE;
2809
2810
3474
        if (ctx->divx_version >= 0)
2811
            s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
2812
    }
2813
2814
3474
    if (s->workaround_bugs & FF_BUG_STD_QPEL) {
2815
        SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
2816
        SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
2817
        SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
2818
        SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
2819
        SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
2820
        SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
2821
2822
        SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
2823
        SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
2824
        SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
2825
        SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
2826
        SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
2827
        SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
2828
    }
2829
2830
3474
    if (avctx->debug & FF_DEBUG_BUGS)
2831
        av_log(s->avctx, AV_LOG_DEBUG,
2832
               "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
2833
               s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
2834
               ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
2835
2836
3474
    if (CONFIG_MPEG4_DECODER && ctx->xvid_build >= 0 &&
2837
64
        s->codec_id == AV_CODEC_ID_MPEG4 &&
2838
64
        avctx->idct_algo == FF_IDCT_AUTO) {
2839
4
        avctx->idct_algo = FF_IDCT_XVID;
2840
4
        ff_mpv_idct_init(s);
2841
4
        return 1;
2842
    }
2843
2844
3470
    return 0;
2845
}
2846
2847
6950
static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2848
{
2849
6950
    MpegEncContext *s = &ctx->m;
2850
    int time_incr, time_increment;
2851
    int64_t pts;
2852
2853
6950
    s->mcsel       = 0;
2854
6950
    s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I;        /* pict type: I = 0 , P = 1 */
2855

6950
    if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
2856
        ctx->vol_control_parameters == 0 && !(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)) {
2857
        av_log(s->avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n");
2858
        s->low_delay = 0;
2859
    }
2860
2861

6950
    s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
2862
6950
    if (s->partitioned_frame)
2863
1028
        s->decode_mb = mpeg4_decode_partitioned_mb;
2864
    else
2865
5922
        s->decode_mb = mpeg4_decode_mb;
2866
2867
6950
    time_incr = 0;
2868
7199
    while (get_bits1(gb) != 0)
2869
249
        time_incr++;
2870
2871
6950
    check_marker(s->avctx, gb, "before time_increment");
2872
2873
6950
    if (ctx->time_increment_bits == 0 ||
2874
6948
        !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
2875
10
        av_log(s->avctx, AV_LOG_WARNING,
2876
               "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);
2877
2878
10
        for (ctx->time_increment_bits = 1;
2879
110
             ctx->time_increment_bits < 16;
2880
100
             ctx->time_increment_bits++) {
2881
110
            if (s->pict_type == AV_PICTURE_TYPE_P ||
2882
110
                (s->pict_type == AV_PICTURE_TYPE_S &&
2883
                 ctx->vol_sprite_usage == GMC_SPRITE)) {
2884
                if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
2885
                    break;
2886
110
            } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
2887
10
                break;
2888
        }
2889
2890
10
        av_log(s->avctx, AV_LOG_WARNING,
2891
               "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits);
2892

10
        if (s->avctx->framerate.num && 4*s->avctx->framerate.num < 1<<ctx->time_increment_bits) {
2893
6
            s->avctx->framerate.num = 1<<ctx->time_increment_bits;
2894
6
            s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
2895
        }
2896
    }
2897
2898
    if (IS_3IV1)
2899
        time_increment = get_bits1(gb);        // FIXME investigate further
2900
    else
2901
6950
        time_increment = get_bits(gb, ctx->time_increment_bits);
2902
2903
6950
    if (s->pict_type != AV_PICTURE_TYPE_B) {
2904
5585
        s->last_time_base = s->time_base;
2905
5585
        s->time_base     += time_incr;
2906
5585
        s->time = s->time_base * (int64_t)s->avctx->framerate.num + time_increment;
2907
5585
        if (s->workaround_bugs & FF_BUG_UMP4) {
2908
            if (s->time < s->last_non_b_time) {
2909
                /* header is not mpeg-4-compatible, broken encoder,
2910
                 * trying to workaround */
2911
                s->time_base++;
2912
                s->time += s->avctx->framerate.num;
2913
            }
2914
        }
2915
5585
        s->pp_time         = s->time - s->last_non_b_time;
2916
5585
        s->last_non_b_time = s->time;
2917
    } else {
2918
1365
        s->time    = (s->last_time_base + time_incr) * (int64_t)s->avctx->framerate.num + time_increment;
2919
1365
        s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
2920
1365
        if (s->pp_time <= s->pb_time ||
2921
1365
            s->pp_time <= s->pp_time - s->pb_time ||
2922
1365
            s->pp_time <= 0) {
2923
            /* messed up order, maybe after seeking? skipping current B-frame */
2924
            return FRAME_SKIPPED;
2925
        }
2926
1365
        ff_mpeg4_init_direct_mv(s);
2927
2928
1365
        if (ctx->t_frame == 0)
2929
215
            ctx->t_frame = s->pb_time;
2930
1365
        if (ctx->t_frame == 0)
2931
            ctx->t_frame = 1;  // 1/0 protection
2932
1365
        s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
2933
1365
                            ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2934
1365
        s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
2935
1365
                            ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2936

1365
        if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1) {
2937
2
            s->pb_field_time = 2;
2938
2
            s->pp_field_time = 4;
2939
2
            if (!s->progressive_sequence)
2940
                return FRAME_SKIPPED;
2941
        }
2942
    }
2943
2944
6950
    if (s->avctx->framerate.den)
2945
6950
        pts = ROUNDED_DIV(s->time, s->avctx->framerate.den);
2946
    else
2947
        pts = AV_NOPTS_VALUE;
2948
    ff_dlog(s->avctx, "MPEG4 PTS: %"PRId64"\n", pts);
2949
2950
6950
    check_marker(s->avctx, gb, "before vop_coded");
2951
2952
    /* vop coded */
2953
6950
    if (get_bits1(gb) != 1) {
2954
8
        if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2955
            av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
2956
8
        return FRAME_SKIPPED;
2957
    }
2958
6942
    if (ctx->new_pred)
2959
        decode_new_pred(ctx, gb);
2960
2961
6942
    if (ctx->shape != BIN_ONLY_SHAPE &&
2962
6942
                    (s->pict_type == AV_PICTURE_TYPE_P ||
2963
2379
                     (s->pict_type == AV_PICTURE_TYPE_S &&
2964
                      ctx->vol_sprite_usage == GMC_SPRITE))) {
2965
        /* rounding type for motion estimation */
2966
4563
        s->no_rounding = get_bits1(gb);
2967
    } else {
2968
2379
        s->no_rounding = 0;
2969
    }
2970
    // FIXME reduced res stuff
2971
2972
6942
    if (ctx->shape != RECT_SHAPE) {
2973
        if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
2974
            skip_bits(gb, 13);  /* width */
2975
            check_marker(s->avctx, gb, "after width");
2976
            skip_bits(gb, 13);  /* height */
2977
            check_marker(s->avctx, gb, "after height");
2978
            skip_bits(gb, 13);  /* hor_spat_ref */
2979
            check_marker(s->avctx, gb, "after hor_spat_ref");
2980
            skip_bits(gb, 13);  /* ver_spat_ref */
2981
        }
2982
        skip_bits1(gb);         /* change_CR_disable */
2983
2984
        if (get_bits1(gb) != 0)
2985
            skip_bits(gb, 8);   /* constant_alpha_value */
2986
    }
2987
2988
    // FIXME complexity estimation stuff
2989
2990
6942
    if (ctx->shape != BIN_ONLY_SHAPE) {
2991
6942
        skip_bits_long(gb, ctx->cplx_estimation_trash_i);
2992
6942
        if (s->pict_type != AV_PICTURE_TYPE_I)
2993
5928
            skip_bits_long(gb, ctx->cplx_estimation_trash_p);
2994
6942
        if (s->pict_type == AV_PICTURE_TYPE_B)
2995
1365
            skip_bits_long(gb, ctx->cplx_estimation_trash_b);
2996
2997
6942
        if (get_bits_left(gb) < 3) {
2998
            av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
2999
            return AVERROR_INVALIDDATA;
3000
        }
3001
6942
        ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
3002
6942
        if (!s->progressive_sequence) {
3003
50
            s->top_field_first = get_bits1(gb);
3004
50
            s->alternate_scan  = get_bits1(gb);
3005
        } else
3006
6892
            s->alternate_scan = 0;
3007
    }
3008
3009
6942
    if (s->alternate_scan) {
3010
6
        ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable,   ff_alternate_vertical_scan);
3011
6
        ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable,   ff_alternate_vertical_scan);
3012
6
        ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
3013
6
        ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
3014
    } else {
3015
6936
        ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable,   ff_zigzag_direct);
3016
6936
        ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable,   ff_zigzag_direct);
3017
6936
        ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
3018
6936
        ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
3019
    }
3020
3021
6942
    if (s->pict_type == AV_PICTURE_TYPE_S) {
3022
        if((ctx->vol_sprite_usage == STATIC_SPRITE ||
3023
            ctx->vol_sprite_usage == GMC_SPRITE)) {
3024
            if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
3025
                return AVERROR_INVALIDDATA;
3026
            if (ctx->sprite_brightness_change)
3027
                av_log(s->avctx, AV_LOG_ERROR,
3028
                    "sprite_brightness_change not supported\n");
3029
            if (ctx->vol_sprite_usage == STATIC_SPRITE)
3030
                av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
3031
        } else {
3032
            memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
3033
            memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
3034
        }
3035
    }
3036
3037
6942
    if (ctx->shape != BIN_ONLY_SHAPE) {
3038
6942
        s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision);
3039
6942
        if (s->qscale == 0) {
3040
            av_log(s->avctx, AV_LOG_ERROR,
3041
                   "Error, header damaged or not MPEG-4 header (qscale=0)\n");
3042
            return AVERROR_INVALIDDATA;  // makes no sense to continue, as there is nothing left from the image then
3043
        }
3044
3045
6942
        if (s->pict_type != AV_PICTURE_TYPE_I) {
3046
5928
            s->f_code = get_bits(gb, 3);        /* fcode_for */
3047
5928
            if (s->f_code == 0) {
3048
4
                av_log(s->avctx, AV_LOG_ERROR,
3049
                       "Error, header damaged or not MPEG-4 header (f_code=0)\n");
3050
4
                s->f_code = 1;
3051
4
                return AVERROR_INVALIDDATA;  // makes no sense to continue, as there is nothing left from the image then
3052
            }
3053
        } else
3054
1014
            s->f_code = 1;
3055
3056
6938
        if (s->pict_type == AV_PICTURE_TYPE_B) {
3057
1365
            s->b_code = get_bits(gb, 3);
3058
1365
            if (s->b_code == 0) {
3059
                av_log(s->avctx, AV_LOG_ERROR,
3060
                       "Error, header damaged or not MPEG4 header (b_code=0)\n");
3061
                s->b_code=1;
3062
                return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly
3063
            }
3064
        } else
3065
5573
            s->b_code = 1;
3066
3067
6938
        if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
3068
            av_log(s->avctx, AV_LOG_DEBUG,
3069
                   "qp:%d fc:%d,%d %s size:%d pro:%d alt:%d top:%d %spel part:%d resync:%d w:%d a:%d rnd:%d vot:%d%s dc:%d ce:%d/%d/%d time:%"PRId64" tincr:%d\n",
3070
                   s->qscale, s->f_code, s->b_code,
3071
                   s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
3072
                   gb->size_in_bits,s->progressive_sequence, s->alternate_scan,
3073
                   s->top_field_first, s->quarter_sample ? "q" : "h",
3074
                   s->data_partitioning, ctx->resync_marker,
3075
                   ctx->num_sprite_warping_points, s->sprite_warping_accuracy,
3076
                   1 - s->no_rounding, s->vo_type,
3077
                   ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
3078
                   ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
3079
                   ctx->cplx_estimation_trash_b,
3080
                   s->time,
3081
                   time_increment
3082
                  );
3083
        }
3084
3085
6938
        if (!ctx->scalability) {
3086

6938
            if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
3087
                skip_bits1(gb);  // vop shape coding type
3088
        } else {
3089
            if (ctx->enhancement_type) {
3090
                int load_backward_shape = get_bits1(gb);
3091
                if (load_backward_shape)
3092
                    av_log(s->avctx, AV_LOG_ERROR,
3093
                           "load backward shape isn't supported\n");
3094
            }
3095
            skip_bits(gb, 2);  // ref_select_code
3096
        }
3097
    }
3098
    /* detect buggy encoders which don't set the low_delay flag
3099
     * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames
3100
     * easily (although it's buggy too) */
3101

6938
    if (s->vo_type == 0 && ctx->vol_control_parameters == 0 &&
3102

48
        ctx->divx_version == -1 && s->picture_number == 0) {
3103
2
        av_log(s->avctx, AV_LOG_WARNING,
3104
               "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
3105
2
        s->low_delay = 1;
3106
    }
3107
3108
6938
    s->picture_number++;  // better than pic number==0 always ;)
3109
3110
    // FIXME add short header support
3111
6938
    s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
3112
6938
    s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
3113
3114
6938
    if (s->workaround_bugs & FF_BUG_EDGE) {
3115
        s->h_edge_pos = s->width;
3116
        s->v_edge_pos = s->height;
3117
    }
3118
6938
    return 0;
3119
}
3120
3121
static void decode_smpte_tc(Mpeg4DecContext *ctx, GetBitContext *gb)
3122
{
3123
    MpegEncContext *s = &ctx->m;
3124
3125
    skip_bits(gb, 16); /* Time_code[63..48] */
3126
    check_marker(s->avctx, gb, "after Time_code[63..48]");
3127
    skip_bits(gb, 16); /* Time_code[47..32] */
3128
    check_marker(s->avctx, gb, "after Time_code[47..32]");
3129
    skip_bits(gb, 16); /* Time_code[31..16] */
3130
    check_marker(s->avctx, gb, "after Time_code[31..16]");
3131
    skip_bits(gb, 16); /* Time_code[15..0] */
3132
    check_marker(s->avctx, gb, "after Time_code[15..0]");
3133
    skip_bits(gb, 4); /* reserved_bits */
3134
}
3135
3136
/**
3137
 * Decode the next studio vop header.
3138
 * @return <0 if something went wrong
3139
 */
3140
static int decode_studio_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
3141
{
3142
    MpegEncContext *s = &ctx->m;
3143
3144
    if (get_bits_left(gb) <= 32)
3145
        return 0;
3146
3147
    s->partitioned_frame = 0;
3148
    s->interlaced_dct = 0;
3149
    s->decode_mb = mpeg4_decode_studio_mb;
3150
3151
    decode_smpte_tc(ctx, gb);
3152
3153
    skip_bits(gb, 10); /* temporal_reference */
3154
    skip_bits(gb, 2); /* vop_structure */
3155
    s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* vop_coding_type */
3156
    if (get_bits1(gb)) { /* vop_coded */
3157
        skip_bits1(gb); /* top_field_first */
3158
        skip_bits1(gb); /* repeat_first_field */
3159
        s->progressive_frame = get_bits1(gb) ^ 1; /* progressive_frame */
3160
    }
3161
3162
    if (s->pict_type == AV_PICTURE_TYPE_I) {
3163
        if (get_bits1(gb))
3164
            reset_studio_dc_predictors(s);
3165
    }
3166
3167
    if (ctx->shape != BIN_ONLY_SHAPE) {
3168
        s->alternate_scan = get_bits1(gb);
3169
        s->frame_pred_frame_dct = get_bits1(gb);
3170
        s->dct_precision = get_bits(gb, 2);
3171
        s->intra_dc_precision = get_bits(gb, 2);
3172
        s->q_scale_type = get_bits1(gb);
3173
    }
3174
3175
    if (s->alternate_scan) {
3176
        ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable,   ff_alternate_vertical_scan);
3177
        ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable,   ff_alternate_vertical_scan);
3178
        ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
3179
        ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
3180
    } else {
3181
        ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable,   ff_zigzag_direct);
3182
        ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable,   ff_zigzag_direct);
3183
        ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
3184
        ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
3185
    }
3186
3187
    mpeg4_load_default_matrices(s);
3188
3189
    next_start_code_studio(gb);
3190
    extension_and_user_data(s, gb, 4);
3191
3192
    return 0;
3193
}
3194
3195
static int decode_studiovisualobject(Mpeg4DecContext *ctx, GetBitContext *gb)
3196
{
3197
    MpegEncContext *s = &ctx->m;
3198
    int visual_object_type;
3199
3200
    skip_bits(gb, 4); /* visual_object_verid */
3201
    visual_object_type = get_bits(gb, 4);
3202
    if (visual_object_type != VOT_VIDEO_ID) {
3203
        avpriv_request_sample(s->avctx, "VO type %u", visual_object_type);
3204
        return AVERROR_PATCHWELCOME;
3205
    }
3206
3207
    next_start_code_studio(gb);
3208
    extension_and_user_data(s, gb, 1);
3209
3210
    return 0;
3211
}
3212
3213
/**
3214
 * Decode MPEG-4 headers.
3215
 *
3216
 * @param  header If set the absence of a VOP is not treated as error; otherwise, it is treated as such.
3217
 * @return <0 if an error occurred
3218
 *         FRAME_SKIPPED if a not coded VOP is found
3219
 *         0 else
3220
 */
3221
7444
int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb, int header)
3222
{
3223
7444
    MpegEncContext *s = &ctx->m;
3224
    unsigned startcode, v;
3225
    int ret;
3226
7444
    int vol = 0;
3227
3228
    /* search next start code */
3229
7444
    align_get_bits(gb);
3230
3231
    // If we have not switched to studio profile than we also did not switch bps
3232
    // that means something else (like a previous instance) outside set bps which
3233
    // would be inconsistant with the currect state, thus reset it
3234

7444
    if (!s->studio_profile && s->avctx->bits_per_raw_sample != 8)
3235
7444
        s->avctx->bits_per_raw_sample = 0;
3236
3237

7444
    if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
3238
        skip_bits(gb, 24);
3239
        if (get_bits(gb, 8) == 0xF0)
3240
            goto end;
3241
    }
3242
3243
7444
    startcode = 0xff;
3244
    for (;;) {
3245
52166
        if (get_bits_count(gb) >= gb->size_in_bits) {
3246
494
            if (gb->size_in_bits == 8 &&
3247

494
                (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
3248
                av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
3249
                return FRAME_SKIPPED;  // divx bug
3250

494
            } else if (header && get_bits_count(gb) == gb->size_in_bits) {
3251
403
                return 0; // ordinary return value for parsing of extradata
3252
            } else
3253
91
                return AVERROR_INVALIDDATA;  // end of stream
3254
        }
3255
3256
        /* use the bits after the test */
3257
51672
        v = get_bits(gb, 8);
3258
51672
        startcode = ((startcode << 8) | v) & 0xffffffff;
3259
3260
51672
        if ((startcode & 0xFFFFFF00) != 0x100)
3261
38755
            continue;  // no startcode
3262
3263
12917
        if (s->avctx->debug & FF_DEBUG_STARTCODE) {
3264
            av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
3265
            if (startcode <= 0x11F)
3266
                av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
3267
            else if (startcode <= 0x12F)
3268
                av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
3269
            else if (startcode <= 0x13F)
3270
                av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
3271
            else if (startcode <= 0x15F)
3272
                av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
3273
            else if (startcode <= 0x1AF)
3274
                av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
3275
            else if (startcode == 0x1B0)
3276
                av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
3277
            else if (startcode == 0x1B1)
3278
                av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
3279
            else if (startcode == 0x1B2)
3280
                av_log(s->avctx, AV_LOG_DEBUG, "User Data");
3281
            else if (startcode == 0x1B3)
3282
                av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
3283
            else if (startcode == 0x1B4)
3284
                av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
3285
            else if (startcode == 0x1B5)
3286
                av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
3287
            else if (startcode == 0x1B6)
3288
                av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
3289
            else if (startcode == 0x1B7)
3290
                av_log(s->avctx, AV_LOG_DEBUG, "slice start");
3291
            else if (startcode == 0x1B8)
3292
                av_log(s->avctx, AV_LOG_DEBUG, "extension start");
3293
            else if (startcode == 0x1B9)
3294
                av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
3295
            else if (startcode == 0x1BA)
3296
                av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
3297
            else if (startcode == 0x1BB)
3298
                av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
3299
            else if (startcode == 0x1BC)
3300
                av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
3301
            else if (startcode == 0x1BD)
3302
                av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
3303
            else if (startcode == 0x1BE)
3304
                av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
3305
            else if (startcode == 0x1BF)
3306
                av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
3307
            else if (startcode == 0x1C0)
3308
                av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
3309
            else if (startcode == 0x1C1)
3310
                av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
3311
            else if (startcode == 0x1C2)
3312
                av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
3313
            else if (startcode == 0x1C3)
3314
                av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
3315
            else if (startcode <= 0x1C5)
3316
                av_log(s->avctx, AV_LOG_DEBUG, "reserved");
3317
            else if (startcode <= 0x1FF)
3318
                av_log(s->avctx, AV_LOG_DEBUG, "System start");
3319
            av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
3320
        }
3321
3322

12917
        if (startcode >= 0x120 && startcode <= 0x12F) {
3323
1250
            if (vol) {
3324
                av_log(s->avctx, AV_LOG_WARNING, "Ignoring multiple VOL headers\n");
3325
                continue;
3326
            }
3327
1250
            vol++;
3328
1250
            if ((ret = decode_vol_header(ctx, gb)) < 0)
3329
                return ret;
3330
11667
        } else if (startcode == USER_DATA_STARTCODE) {
3331
76
            decode_user_data(ctx, gb);
3332
11591
        } else if (startcode == GOP_STARTCODE) {
3333
955
            mpeg4_decode_gop_header(s, gb);
3334
10636
        } else if (startcode == VOS_STARTCODE) {
3335
            int profile, level;
3336
1218
            mpeg4_decode_profile_level(s, gb, &profile, &level);
3337
1218
            if (profile == FF_PROFILE_MPEG4_SIMPLE_STUDIO &&
3338
                (level > 0 && level < 9)) {
3339
                s->studio_profile = 1;
3340
                next_start_code_studio(gb);
3341
                extension_and_user_data(s, gb, 0);
3342
1218
            } else if (s->studio_profile) {
3343
                avpriv_request_sample(s->avctx, "Mix of studio and non studio profile");
3344
                return AVERROR_PATCHWELCOME;
3345
            }
3346
1218
            s->avctx->profile = profile;
3347
1218
            s->avctx->level   = level;
3348
9418
        } else if (startcode == VISUAL_OBJ_STARTCODE) {
3349
1218
            if (s->studio_profile) {
3350
                if ((ret = decode_studiovisualobject(ctx, gb)) < 0)
3351
                    return ret;
3352
            } else
3353
1218
                mpeg4_decode_visual_object(s, gb);
3354
8200
        } else if (startcode == VOP_STARTCODE) {
3355
6950
            break;
3356
        }
3357
3358
5967
        align_get_bits(gb);
3359
5967
        startcode = 0xff;
3360
    }
3361
3362
6950
end:
3363
6950
    if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
3364
        s->low_delay = 1;
3365
6950
    s->avctx->has_b_frames = !s->low_delay;
3366
3367
6950
    if (s->studio_profile) {
3368
        if (!s->avctx->bits_per_raw_sample) {
3369
            av_log(s->avctx, AV_LOG_ERROR, "Missing VOL header\n");
3370
            return AVERROR_INVALIDDATA;
3371
        }
3372
        return decode_studio_vop_header(ctx, gb);
3373
    } else
3374
6950
        return decode_vop_header(ctx, gb);
3375
}
3376
3377
426
av_cold void ff_mpeg4videodec_static_init(void) {
3378
    static int done = 0;
3379
3380
426
    if (!done) {
3381
97
        ff_rl_init(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
3382
97
        ff_rl_init(&ff_rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
3383
97
        ff_rl_init(&ff_rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
3384
97
        INIT_FIRST_VLC_RL(ff_mpeg4_rl_intra, 554);
3385

3201
        INIT_VLC_RL(ff_rvlc_rl_inter, 1072);
3386
97
        INIT_FIRST_VLC_RL(ff_rvlc_rl_intra, 1072);
3387
97
        INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
3388
                        &ff_mpeg4_DCtab_lum[0][1], 2, 1,
3389
                        &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
3390
97
        INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
3391
                        &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
3392
                        &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
3393
97
        INIT_VLC_STATIC_FROM_LENGTHS(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
3394
                                     ff_sprite_trajectory_lens, 1,
3395
                                     NULL, 0, 0, 0, 0, 128);
3396
97
        INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
3397
                        &ff_mb_type_b_tab[0][1], 2, 1,
3398
                        &ff_mb_type_b_tab[0][0], 2, 1, 16);
3399
97
        done = 1;
3400
    }
3401
426
}
3402
3403
3329
int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
3404
{
3405
3329
    Mpeg4DecContext *ctx = avctx->priv_data;
3406
3329
    MpegEncContext    *s = &ctx->m;
3407
3408
    /* divx 5.01+ bitstream reorder stuff */