3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
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.
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.
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
23 #define UNCHECKED_BITSTREAM_READER 1
25 #include "libavutil/internal.h"
26 #include "libavutil/opt.h"
27 #include "error_resilience.h"
30 #include "mpegutils.h"
31 #include "mpegvideo.h"
32 #include "mpegvideodata.h"
33 #include "mpeg4video.h"
39 /* The defines below define the number of bits that are read at once for
40 * reading vlc values. Changing these may improve speed and data cache needs
41 * be aware though that decreasing them may need the number of stages that is
42 * passed to get_vlc* to be increased. */
43 #define SPRITE_TRAJ_VLC_BITS 6
45 #define MB_TYPE_B_VLC_BITS 4
47 static VLC dc_lum, dc_chrom;
48 static VLC sprite_trajectory;
49 static VLC mb_type_b_vlc;
51 static const int mb_type_b_map[4] = {
52 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
53 MB_TYPE_L0L1 | MB_TYPE_16x16,
54 MB_TYPE_L1 | MB_TYPE_16x16,
55 MB_TYPE_L0 | MB_TYPE_16x16,
60 * @param n block index (0-3 are luma, 4-5 are chroma)
61 * @param dir the ac prediction direction
63 void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
66 int16_t *ac_val, *ac_val1;
67 int8_t *const qscale_table = s->current_picture.qscale_table;
70 ac_val = &s->ac_val[0][0][0] + s->block_index[n] * 16;
74 const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
78 if (s->mb_x == 0 || s->qscale == qscale_table[xy] ||
81 for (i = 1; i < 8; i++)
82 block[s->idsp.idct_permutation[i << 3]] += ac_val[i];
84 /* different qscale, we must rescale */
85 for (i = 1; i < 8; i++)
86 block[s->idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
89 const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
91 ac_val -= 16 * s->block_wrap[n];
93 if (s->mb_y == 0 || s->qscale == qscale_table[xy] ||
96 for (i = 1; i < 8; i++)
97 block[s->idsp.idct_permutation[i]] += ac_val[i + 8];
99 /* different qscale, we must rescale */
100 for (i = 1; i < 8; i++)
101 block[s->idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
106 for (i = 1; i < 8; i++)
107 ac_val1[i] = block[s->idsp.idct_permutation[i << 3]];
110 for (i = 1; i < 8; i++)
111 ac_val1[8 + i] = block[s->idsp.idct_permutation[i]];
115 * check if the next stuff is a resync marker or the end.
118 static inline int mpeg4_is_resync(Mpeg4DecContext *ctx)
120 MpegEncContext *s = &ctx->m;
121 int bits_count = get_bits_count(&s->gb);
122 int v = show_bits(&s->gb, 16);
124 if (s->workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker)
128 if (s->pict_type == AV_PICTURE_TYPE_B ||
129 (v >> (8 - s->pict_type) != 1) || s->partitioned_frame)
131 skip_bits(&s->gb, 8 + s->pict_type);
132 bits_count += 8 + s->pict_type;
133 v = show_bits(&s->gb, 16);
136 if (bits_count + 8 >= s->gb.size_in_bits) {
138 v |= 0x7F >> (7 - (bits_count & 7));
143 if (v == ff_mpeg4_resync_prefix[bits_count & 7]) {
145 int mb_num_bits = av_log2(s->mb_num - 1) + 1;
146 GetBitContext gb = s->gb;
148 skip_bits(&s->gb, 1);
149 align_get_bits(&s->gb);
151 for (len = 0; len < 32; len++)
152 if (get_bits1(&s->gb))
155 mb_num = get_bits(&s->gb, mb_num_bits);
156 if (!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
161 if (len >= ff_mpeg4_get_video_packet_prefix_length(s))
168 static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
170 MpegEncContext *s = &ctx->m;
171 int a = 2 << s->sprite_warping_accuracy;
172 int rho = 3 - s->sprite_warping_accuracy;
178 int min_ab, i, w2, h2, w3, h3;
179 int sprite_ref[4][2];
180 int virtual_ref[2][2];
181 int64_t sprite_offset[2][2];
182 int64_t sprite_delta[2][2];
184 // only true for rectangle shapes
185 const int vop_ref[4][2] = { { 0, 0 }, { s->width, 0 },
186 { 0, s->height }, { s->width, s->height } };
187 int d[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
189 if (w <= 0 || h <= 0)
190 return AVERROR_INVALIDDATA;
192 for (i = 0; i < ctx->num_sprite_warping_points; i++) {
196 length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
198 x = get_xbits(gb, length);
200 if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
201 check_marker(s->avctx, gb, "before sprite_trajectory");
203 length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
205 y = get_xbits(gb, length);
207 check_marker(s->avctx, gb, "after sprite_trajectory");
208 ctx->sprite_traj[i][0] = d[i][0] = x;
209 ctx->sprite_traj[i][1] = d[i][1] = y;
212 ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0;
214 while ((1 << alpha) < w)
216 while ((1 << beta) < h)
217 beta++; /* typo in the MPEG-4 std for the definition of w' and h' */
221 // Note, the 4th point isn't used for GMC
222 if (ctx->divx_version == 500 && ctx->divx_build == 413) {
223 sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
224 sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
225 sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
226 sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1];
227 sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0];
228 sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1];
230 sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]);
231 sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]);
232 sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]);
233 sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]);
234 sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]);
235 sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]);
237 /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
238 * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
240 /* This is mostly identical to the MPEG-4 std (and is totally unreadable
241 * because of that...). Perhaps it should be reordered to be more readable.
242 * The idea behind this virtual_ref mess is to be able to use shifts later
243 * per pixel instead of divides so the distance between points is converted
244 * from w&h based to w2&h2 based which are of the 2^x form. */
245 virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) +
246 ROUNDED_DIV(((w - w2) *
247 (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
248 w2 * (r * sprite_ref[1][0] - 16LL * vop_ref[1][0])), w);
249 virtual_ref[0][1] = 16 * vop_ref[0][1] +
250 ROUNDED_DIV(((w - w2) *
251 (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
252 w2 * (r * sprite_ref[1][1] - 16LL * vop_ref[1][1])), w);
253 virtual_ref[1][0] = 16 * vop_ref[0][0] +
254 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
255 h2 * (r * sprite_ref[2][0] - 16LL * vop_ref[2][0])), h);
256 virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) +
257 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
258 h2 * (r * sprite_ref[2][1] - 16LL * vop_ref[2][1])), h);
260 switch (ctx->num_sprite_warping_points) {
262 sprite_offset[0][0] =
263 sprite_offset[0][1] =
264 sprite_offset[1][0] =
265 sprite_offset[1][1] = 0;
266 sprite_delta[0][0] = a;
268 sprite_delta[1][0] = 0;
269 sprite_delta[1][1] = a;
270 ctx->sprite_shift[0] =
271 ctx->sprite_shift[1] = 0;
274 sprite_offset[0][0] = sprite_ref[0][0] - a * vop_ref[0][0];
275 sprite_offset[0][1] = sprite_ref[0][1] - a * vop_ref[0][1];
276 sprite_offset[1][0] = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) -
277 a * (vop_ref[0][0] / 2);
278 sprite_offset[1][1] = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) -
279 a * (vop_ref[0][1] / 2);
280 sprite_delta[0][0] = a;
282 sprite_delta[1][0] = 0;
283 sprite_delta[1][1] = a;
284 ctx->sprite_shift[0] =
285 ctx->sprite_shift[1] = 0;
288 sprite_offset[0][0] = ((int64_t) sprite_ref[0][0] * (1 << alpha + rho)) +
289 ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
290 ((int64_t) -vop_ref[0][0]) +
291 ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
292 ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
293 sprite_offset[0][1] = ((int64_t) sprite_ref[0][1] * (1 << alpha + rho)) +
294 ((int64_t) -r * sprite_ref[0][1] + virtual_ref[0][1]) *
295 ((int64_t) -vop_ref[0][0]) +
296 ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
297 ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
298 sprite_offset[1][0] = (((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
299 ((int64_t)-2 * vop_ref[0][0] + 1) +
300 ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
301 ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
302 (int64_t) sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1)));
303 sprite_offset[1][1] = (((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) *
304 ((int64_t)-2 * vop_ref[0][0] + 1) +
305 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
306 ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
307 (int64_t) sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1)));
308 sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
309 sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]);
310 sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
311 sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
313 ctx->sprite_shift[0] = alpha + rho;
314 ctx->sprite_shift[1] = alpha + rho + 2;
317 min_ab = FFMIN(alpha, beta);
320 sprite_offset[0][0] = ((int64_t)sprite_ref[0][0] * (1 << (alpha + beta + rho - min_ab))) +
321 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-vop_ref[0][0]) +
322 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-vop_ref[0][1]) +
323 ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
324 sprite_offset[0][1] = ((int64_t)sprite_ref[0][1] * (1 << (alpha + beta + rho - min_ab))) +
325 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-vop_ref[0][0]) +
326 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-vop_ref[0][1]) +
327 ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
328 sprite_offset[1][0] = ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-2 * vop_ref[0][0] + 1) +
329 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-2 * vop_ref[0][1] + 1) +
330 (int64_t)2 * w2 * h3 * r * sprite_ref[0][0] - 16 * w2 * h3 +
331 ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
332 sprite_offset[1][1] = ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-2 * vop_ref[0][0] + 1) +
333 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-2 * vop_ref[0][1] + 1) +
334 (int64_t)2 * w2 * h3 * r * sprite_ref[0][1] - 16 * w2 * h3 +
335 ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
336 sprite_delta[0][0] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[0][0]) * h3;
337 sprite_delta[0][1] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[1][0]) * w3;
338 sprite_delta[1][0] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[0][1]) * h3;
339 sprite_delta[1][1] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[1][1]) * w3;
341 ctx->sprite_shift[0] = alpha + beta + rho - min_ab;
342 ctx->sprite_shift[1] = alpha + beta + rho - min_ab + 2;
345 /* try to simplify the situation */
346 if (sprite_delta[0][0] == a << ctx->sprite_shift[0] &&
347 sprite_delta[0][1] == 0 &&
348 sprite_delta[1][0] == 0 &&
349 sprite_delta[1][1] == a << ctx->sprite_shift[0]) {
350 sprite_offset[0][0] >>= ctx->sprite_shift[0];
351 sprite_offset[0][1] >>= ctx->sprite_shift[0];
352 sprite_offset[1][0] >>= ctx->sprite_shift[1];
353 sprite_offset[1][1] >>= ctx->sprite_shift[1];
354 sprite_delta[0][0] = a;
355 sprite_delta[0][1] = 0;
356 sprite_delta[1][0] = 0;
357 sprite_delta[1][1] = a;
358 ctx->sprite_shift[0] = 0;
359 ctx->sprite_shift[1] = 0;
360 s->real_sprite_warping_points = 1;
362 int shift_y = 16 - ctx->sprite_shift[0];
363 int shift_c = 16 - ctx->sprite_shift[1];
365 for (i = 0; i < 2; i++) {
366 if (shift_c < 0 || shift_y < 0 ||
367 FFABS( sprite_offset[0][i]) >= INT_MAX >> shift_y ||
368 FFABS( sprite_offset[1][i]) >= INT_MAX >> shift_c ||
369 FFABS( sprite_delta[0][i]) >= INT_MAX >> shift_y ||
370 FFABS( sprite_delta[1][i]) >= INT_MAX >> shift_y
372 avpriv_request_sample(s->avctx, "Too large sprite shift, delta or offset");
377 for (i = 0; i < 2; i++) {
378 sprite_offset[0][i] *= 1 << shift_y;
379 sprite_offset[1][i] *= 1 << shift_c;
380 sprite_delta[0][i] *= 1 << shift_y;
381 sprite_delta[1][i] *= 1 << shift_y;
382 ctx->sprite_shift[i] = 16;
385 for (i = 0; i < 2; i++) {
387 sprite_delta[i][0] - a * (1LL<<16),
388 sprite_delta[i][1] - a * (1LL<<16)
391 if (llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
392 llabs(sprite_offset[0][i] + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
393 llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL) + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
394 llabs(sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
395 llabs(sprite_delta[i][1] * (w+16LL)) >= INT_MAX ||
396 llabs(sd[0]) >= INT_MAX ||
397 llabs(sd[1]) >= INT_MAX ||
398 llabs(sprite_offset[0][i] + sd[0] * (w+16LL)) >= INT_MAX ||
399 llabs(sprite_offset[0][i] + sd[1] * (h+16LL)) >= INT_MAX ||
400 llabs(sprite_offset[0][i] + sd[0] * (w+16LL) + sd[1] * (h+16LL)) >= INT_MAX
402 avpriv_request_sample(s->avctx, "Overflow on sprite points");
406 s->real_sprite_warping_points = ctx->num_sprite_warping_points;
409 for (i = 0; i < 4; i++) {
410 s->sprite_offset[i&1][i>>1] = sprite_offset[i&1][i>>1];
411 s->sprite_delta [i&1][i>>1] = sprite_delta [i&1][i>>1];
416 memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
417 memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
418 return AVERROR_PATCHWELCOME;
421 static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb) {
422 MpegEncContext *s = &ctx->m;
423 int len = FFMIN(ctx->time_increment_bits + 3, 15);
428 check_marker(s->avctx, gb, "after new_pred");
434 * Decode the next video packet.
435 * @return <0 if something went wrong
437 int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
439 MpegEncContext *s = &ctx->m;
441 int mb_num_bits = av_log2(s->mb_num - 1) + 1;
442 int header_extension = 0, mb_num, len;
444 /* is there enough space left for a video packet + header */
445 if (get_bits_count(&s->gb) > s->gb.size_in_bits - 20)
448 for (len = 0; len < 32; len++)
449 if (get_bits1(&s->gb))
452 if (len != ff_mpeg4_get_video_packet_prefix_length(s)) {
453 av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
457 if (ctx->shape != RECT_SHAPE) {
458 header_extension = get_bits1(&s->gb);
459 // FIXME more stuff here
462 mb_num = get_bits(&s->gb, mb_num_bits);
463 if (mb_num >= s->mb_num) {
464 av_log(s->avctx, AV_LOG_ERROR,
465 "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
469 s->mb_x = mb_num % s->mb_width;
470 s->mb_y = mb_num / s->mb_width;
472 if (ctx->shape != BIN_ONLY_SHAPE) {
473 int qscale = get_bits(&s->gb, s->quant_precision);
475 s->chroma_qscale = s->qscale = qscale;
478 if (ctx->shape == RECT_SHAPE)
479 header_extension = get_bits1(&s->gb);
481 if (header_extension) {
484 while (get_bits1(&s->gb) != 0)
487 check_marker(s->avctx, &s->gb, "before time_increment in video packed header");
488 skip_bits(&s->gb, ctx->time_increment_bits); /* time_increment */
489 check_marker(s->avctx, &s->gb, "before vop_coding_type in video packed header");
491 skip_bits(&s->gb, 2); /* vop coding type */
492 // FIXME not rect stuff here
494 if (ctx->shape != BIN_ONLY_SHAPE) {
495 skip_bits(&s->gb, 3); /* intra dc vlc threshold */
496 // FIXME don't just ignore everything
497 if (s->pict_type == AV_PICTURE_TYPE_S &&
498 ctx->vol_sprite_usage == GMC_SPRITE) {
499 if (mpeg4_decode_sprite_trajectory(ctx, &s->gb) < 0)
500 return AVERROR_INVALIDDATA;
501 av_log(s->avctx, AV_LOG_ERROR, "untested\n");
504 // FIXME reduced res stuff here
506 if (s->pict_type != AV_PICTURE_TYPE_I) {
507 int f_code = get_bits(&s->gb, 3); /* fcode_for */
509 av_log(s->avctx, AV_LOG_ERROR,
510 "Error, video packet header damaged (f_code=0)\n");
512 if (s->pict_type == AV_PICTURE_TYPE_B) {
513 int b_code = get_bits(&s->gb, 3);
515 av_log(s->avctx, AV_LOG_ERROR,
516 "Error, video packet header damaged (b_code=0)\n");
521 decode_new_pred(ctx, &s->gb);
527 * Get the average motion vector for a GMC MB.
528 * @param n either 0 for the x component or 1 for y
529 * @return the average MV for a GMC MB
531 static inline int get_amv(Mpeg4DecContext *ctx, int n)
533 MpegEncContext *s = &ctx->m;
534 int x, y, mb_v, sum, dx, dy, shift;
535 int len = 1 << (s->f_code + 4);
536 const int a = s->sprite_warping_accuracy;
538 if (s->workaround_bugs & FF_BUG_AMV)
539 len >>= s->quarter_sample;
541 if (s->real_sprite_warping_points == 1) {
542 if (ctx->divx_version == 500 && ctx->divx_build == 413)
543 sum = s->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
545 sum = RSHIFT(s->sprite_offset[0][n] * (1 << s->quarter_sample), a);
547 dx = s->sprite_delta[n][0];
548 dy = s->sprite_delta[n][1];
549 shift = ctx->sprite_shift[0];
551 dy -= 1 << (shift + a + 1);
553 dx -= 1 << (shift + a + 1);
554 mb_v = s->sprite_offset[0][n] + dx * s->mb_x * 16 + dy * s->mb_y * 16;
557 for (y = 0; y < 16; y++) {
562 for (x = 0; x < 16; x++) {
567 sum = RSHIFT(sum, a + 8 - s->quarter_sample);
579 * Decode the dc value.
580 * @param n block index (0-3 are luma, 4-5 are chroma)
581 * @param dir_ptr the prediction direction will be stored here
582 * @return the quantized dc
584 static inline int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
589 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
591 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
593 if (code < 0 || code > 9 /* && s->nbit < 9 */) {
594 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
603 level = 2 * get_bits1(&s->gb) - 1;
605 if (get_bits1(&s->gb))
606 level = get_bits(&s->gb, code - 1) + (1 << (code - 1));
608 level = -get_bits(&s->gb, code - 1) - (1 << (code - 1));
611 level = get_xbits(&s->gb, code);
615 if (get_bits1(&s->gb) == 0) { /* marker */
616 if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)) {
617 av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
624 return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
628 * Decode first partition.
629 * @return number of MBs decoded or <0 if an error occurred
631 static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
633 MpegEncContext *s = &ctx->m;
635 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
637 /* decode first partition */
638 s->first_slice_line = 1;
639 for (; s->mb_y < s->mb_height; s->mb_y++) {
640 ff_init_block_index(s);
641 for (; s->mb_x < s->mb_width; s->mb_x++) {
642 const int xy = s->mb_x + s->mb_y * s->mb_stride;
647 ff_update_block_index(s);
648 if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
649 s->first_slice_line = 0;
651 if (s->pict_type == AV_PICTURE_TYPE_I) {
655 if (show_bits_long(&s->gb, 19) == DC_MARKER)
658 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
660 av_log(s->avctx, AV_LOG_ERROR,
661 "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
666 s->cbp_table[xy] = cbpc & 3;
667 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
671 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
673 s->current_picture.qscale_table[xy] = s->qscale;
675 s->mbintra_table[xy] = 1;
676 for (i = 0; i < 6; i++) {
678 int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
680 av_log(s->avctx, AV_LOG_ERROR,
681 "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
688 s->pred_dir_table[xy] = dir;
689 } else { /* P/S_TYPE */
690 int mx, my, pred_x, pred_y, bits;
691 int16_t *const mot_val = s->current_picture.motion_val[0][s->block_index[0]];
692 const int stride = s->b8_stride * 2;
695 bits = show_bits(&s->gb, 17);
696 if (bits == MOTION_MARKER)
700 if (bits & 0x10000) {
702 if (s->pict_type == AV_PICTURE_TYPE_S &&
703 ctx->vol_sprite_usage == GMC_SPRITE) {
704 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
708 mx = get_amv(ctx, 0);
709 my = get_amv(ctx, 1);
711 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
718 mot_val[0 + stride] =
719 mot_val[2 + stride] = mx;
722 mot_val[1 + stride] =
723 mot_val[3 + stride] = my;
725 if (s->mbintra_table[xy])
726 ff_clean_intra_table_entries(s);
730 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
732 av_log(s->avctx, AV_LOG_ERROR,
733 "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
739 s->cbp_table[xy] = cbpc & (8 + 3); // 8 is dquant
741 s->mb_intra = ((cbpc & 4) != 0);
744 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
745 s->mbintra_table[xy] = 1;
748 mot_val[0 + stride] =
749 mot_val[2 + stride] = 0;
752 mot_val[1 + stride] =
753 mot_val[3 + stride] = 0;
755 if (s->mbintra_table[xy])
756 ff_clean_intra_table_entries(s);
758 if (s->pict_type == AV_PICTURE_TYPE_S &&
759 ctx->vol_sprite_usage == GMC_SPRITE &&
761 s->mcsel = get_bits1(&s->gb);
765 if ((cbpc & 16) == 0) {
766 /* 16x16 motion prediction */
768 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
770 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
774 my = ff_h263_decode_motion(s, pred_y, s->f_code);
777 s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
780 mx = get_amv(ctx, 0);
781 my = get_amv(ctx, 1);
782 s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
789 mot_val[0 + stride] =
790 mot_val[2 + stride] = mx;
793 mot_val[1 + stride] =
794 mot_val[3 + stride] = my;
797 s->current_picture.mb_type[xy] = MB_TYPE_8x8 |
799 for (i = 0; i < 4; i++) {
800 int16_t *mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
801 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
805 my = ff_h263_decode_motion(s, pred_y, s->f_code);
822 * decode second partition.
823 * @return <0 if an error occurred
825 static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
828 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
830 s->mb_x = s->resync_mb_x;
831 s->first_slice_line = 1;
832 for (s->mb_y = s->resync_mb_y; mb_num < mb_count; s->mb_y++) {
833 ff_init_block_index(s);
834 for (; mb_num < mb_count && s->mb_x < s->mb_width; s->mb_x++) {
835 const int xy = s->mb_x + s->mb_y * s->mb_stride;
838 ff_update_block_index(s);
839 if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
840 s->first_slice_line = 0;
842 if (s->pict_type == AV_PICTURE_TYPE_I) {
843 int ac_pred = get_bits1(&s->gb);
844 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
846 av_log(s->avctx, AV_LOG_ERROR,
847 "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
851 s->cbp_table[xy] |= cbpy << 2;
852 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
853 } else { /* P || S_TYPE */
854 if (IS_INTRA(s->current_picture.mb_type[xy])) {
857 int ac_pred = get_bits1(&s->gb);
858 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
861 av_log(s->avctx, AV_LOG_ERROR,
862 "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
866 if (s->cbp_table[xy] & 8)
867 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
868 s->current_picture.qscale_table[xy] = s->qscale;
870 for (i = 0; i < 6; i++) {
872 int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
874 av_log(s->avctx, AV_LOG_ERROR,
875 "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
882 s->cbp_table[xy] &= 3; // remove dquant
883 s->cbp_table[xy] |= cbpy << 2;
884 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
885 s->pred_dir_table[xy] = dir;
886 } else if (IS_SKIP(s->current_picture.mb_type[xy])) {
887 s->current_picture.qscale_table[xy] = s->qscale;
888 s->cbp_table[xy] = 0;
890 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
893 av_log(s->avctx, AV_LOG_ERROR,
894 "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
898 if (s->cbp_table[xy] & 8)
899 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
900 s->current_picture.qscale_table[xy] = s->qscale;
902 s->cbp_table[xy] &= 3; // remove dquant
903 s->cbp_table[xy] |= (cbpy ^ 0xf) << 2;
907 if (mb_num >= mb_count)
915 * Decode the first and second partition.
916 * @return <0 if error (and sets error type in the error_status_table)
918 int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx)
920 MpegEncContext *s = &ctx->m;
922 const int part_a_error = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR;
923 const int part_a_end = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END | ER_MV_END) : ER_MV_END;
925 mb_num = mpeg4_decode_partition_a(ctx);
927 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
928 s->mb_x, s->mb_y, part_a_error);
932 if (s->resync_mb_x + s->resync_mb_y * s->mb_width + mb_num > s->mb_num) {
933 av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
934 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
935 s->mb_x, s->mb_y, part_a_error);
939 s->mb_num_left = mb_num;
941 if (s->pict_type == AV_PICTURE_TYPE_I) {
942 while (show_bits(&s->gb, 9) == 1)
943 skip_bits(&s->gb, 9);
944 if (get_bits_long(&s->gb, 19) != DC_MARKER) {
945 av_log(s->avctx, AV_LOG_ERROR,
946 "marker missing after first I partition at %d %d\n",
951 while (show_bits(&s->gb, 10) == 1)
952 skip_bits(&s->gb, 10);
953 if (get_bits(&s->gb, 17) != MOTION_MARKER) {
954 av_log(s->avctx, AV_LOG_ERROR,
955 "marker missing after first P partition at %d %d\n",
960 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
961 s->mb_x - 1, s->mb_y, part_a_end);
963 if (mpeg4_decode_partition_b(s, mb_num) < 0) {
964 if (s->pict_type == AV_PICTURE_TYPE_P)
965 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
966 s->mb_x, s->mb_y, ER_DC_ERROR);
969 if (s->pict_type == AV_PICTURE_TYPE_P)
970 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
971 s->mb_x - 1, s->mb_y, ER_DC_END);
979 * @return <0 if an error occurred
981 static inline int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block,
982 int n, int coded, int intra, int rvlc)
984 MpegEncContext *s = &ctx->m;
985 int level, i, last, run, qmul, qadd;
986 int av_uninit(dc_pred_dir);
989 const uint8_t *scan_table;
991 // Note intra & rvlc should be optimized away if this is inlined
994 if (ctx->use_intra_dc_vlc) {
996 if (s->partitioned_frame) {
997 level = s->dc_val[0][s->block_index[n]];
999 level = FASTDIV((level + (s->y_dc_scale >> 1)), s->y_dc_scale);
1001 level = FASTDIV((level + (s->c_dc_scale >> 1)), s->c_dc_scale);
1002 dc_pred_dir = (s->pred_dir_table[s->mb_x + s->mb_y * s->mb_stride] << n) & 32;
1004 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
1012 ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
1018 rl = &ff_rvlc_rl_intra;
1019 rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
1021 rl = &ff_mpeg4_rl_intra;
1022 rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
1025 if (dc_pred_dir == 0)
1026 scan_table = s->intra_v_scantable.permutated; /* left */
1028 scan_table = s->intra_h_scantable.permutated; /* top */
1030 scan_table = s->intra_scantable.permutated;
1037 s->block_last_index[n] = i;
1041 rl = &ff_rvlc_rl_inter;
1043 rl = &ff_h263_rl_inter;
1045 scan_table = s->intra_scantable.permutated;
1047 if (s->mpeg_quant) {
1051 rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
1053 rl_vlc = ff_h263_rl_inter.rl_vlc[0];
1055 qmul = s->qscale << 1;
1056 qadd = (s->qscale - 1) | 1;
1058 rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
1060 rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
1064 OPEN_READER(re, &s->gb);
1066 UPDATE_CACHE(re, &s->gb);
1067 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1071 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1072 av_log(s->avctx, AV_LOG_ERROR,
1073 "1. marker bit missing in rvlc esc\n");
1076 SKIP_CACHE(re, &s->gb, 1);
1078 last = SHOW_UBITS(re, &s->gb, 1);
1079 SKIP_CACHE(re, &s->gb, 1);
1080 run = SHOW_UBITS(re, &s->gb, 6);
1081 SKIP_COUNTER(re, &s->gb, 1 + 1 + 6);
1082 UPDATE_CACHE(re, &s->gb);
1084 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1085 av_log(s->avctx, AV_LOG_ERROR,
1086 "2. marker bit missing in rvlc esc\n");
1089 SKIP_CACHE(re, &s->gb, 1);
1091 level = SHOW_UBITS(re, &s->gb, 11);
1092 SKIP_CACHE(re, &s->gb, 11);
1094 if (SHOW_UBITS(re, &s->gb, 5) != 0x10) {
1095 av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
1098 SKIP_CACHE(re, &s->gb, 5);
1100 level = level * qmul + qadd;
1101 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1102 SKIP_COUNTER(re, &s->gb, 1 + 11 + 5 + 1);
1109 cache = GET_CACHE(re, &s->gb);
1112 cache ^= 0xC0000000;
1114 if (cache & 0x80000000) {
1115 if (cache & 0x40000000) {
1117 SKIP_CACHE(re, &s->gb, 2);
1118 last = SHOW_UBITS(re, &s->gb, 1);
1119 SKIP_CACHE(re, &s->gb, 1);
1120 run = SHOW_UBITS(re, &s->gb, 6);
1121 SKIP_COUNTER(re, &s->gb, 2 + 1 + 6);
1122 UPDATE_CACHE(re, &s->gb);
1125 level = SHOW_SBITS(re, &s->gb, 12);
1126 LAST_SKIP_BITS(re, &s->gb, 12);
1128 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1129 av_log(s->avctx, AV_LOG_ERROR,
1130 "1. marker bit missing in 3. esc\n");
1131 if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
1134 SKIP_CACHE(re, &s->gb, 1);
1136 level = SHOW_SBITS(re, &s->gb, 12);
1137 SKIP_CACHE(re, &s->gb, 12);
1139 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1140 av_log(s->avctx, AV_LOG_ERROR,
1141 "2. marker bit missing in 3. esc\n");
1142 if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
1146 SKIP_COUNTER(re, &s->gb, 1 + 12 + 1);
1150 if (s->error_recognition >= FF_ER_COMPLIANT) {
1151 const int abs_level= FFABS(level);
1152 if (abs_level<=MAX_LEVEL && run<=MAX_RUN) {
1153 const int run1= run - rl->max_run[last][abs_level] - 1;
1154 if (abs_level <= rl->max_level[last][run]) {
1155 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1158 if (s->error_recognition > FF_ER_COMPLIANT) {
1159 if (abs_level <= rl->max_level[last][run]*2) {
1160 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1163 if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) {
1164 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1172 level = level * qmul + qadd;
1174 level = level * qmul - qadd;
1176 if ((unsigned)(level + 2048) > 4095) {
1177 if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)) {
1178 if (level > 2560 || level < -2560) {
1179 av_log(s->avctx, AV_LOG_ERROR,
1180 "|level| overflow in 3. esc, qp=%d\n",
1185 level = level < 0 ? -2048 : 2047;
1193 SKIP_BITS(re, &s->gb, 2);
1194 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1195 i += run + rl->max_run[run >> 7][level / qmul] + 1; // FIXME opt indexing
1196 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1197 LAST_SKIP_BITS(re, &s->gb, 1);
1201 SKIP_BITS(re, &s->gb, 1);
1202 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1204 level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; // FIXME opt indexing
1205 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1206 LAST_SKIP_BITS(re, &s->gb, 1);
1211 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1212 LAST_SKIP_BITS(re, &s->gb, 1);
1214 ff_tlog(s->avctx, "dct[%d][%d] = %- 4d end?:%d\n", scan_table[i&63]&7, scan_table[i&63] >> 3, level, i>62);
1218 av_log(s->avctx, AV_LOG_ERROR,
1219 "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1223 block[scan_table[i]] = level;
1227 block[scan_table[i]] = level;
1229 CLOSE_READER(re, &s->gb);
1234 if (!ctx->use_intra_dc_vlc) {
1235 block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
1237 i -= i >> 31; // if (i == -1) i = 0;
1240 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1242 i = 63; // FIXME not optimal
1244 s->block_last_index[n] = i;
1249 * decode partition C of one MB.
1250 * @return <0 if an error occurred
1252 static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
1254 Mpeg4DecContext *ctx = (Mpeg4DecContext *)s;
1256 const int xy = s->mb_x + s->mb_y * s->mb_stride;
1258 mb_type = s->current_picture.mb_type[xy];
1259 cbp = s->cbp_table[xy];
1261 ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1263 if (s->current_picture.qscale_table[xy] != s->qscale)
1264 ff_set_qscale(s, s->current_picture.qscale_table[xy]);
1266 if (s->pict_type == AV_PICTURE_TYPE_P ||
1267 s->pict_type == AV_PICTURE_TYPE_S) {
1269 for (i = 0; i < 4; i++) {
1270 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
1271 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
1273 s->mb_intra = IS_INTRA(mb_type);
1275 if (IS_SKIP(mb_type)) {
1277 for (i = 0; i < 6; i++)
1278 s->block_last_index[i] = -1;
1279 s->mv_dir = MV_DIR_FORWARD;
1280 s->mv_type = MV_TYPE_16X16;
1281 if (s->pict_type == AV_PICTURE_TYPE_S
1282 && ctx->vol_sprite_usage == GMC_SPRITE) {
1289 } else if (s->mb_intra) {
1290 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1291 } else if (!s->mb_intra) {
1292 // s->mcsel = 0; // FIXME do we need to init that?
1294 s->mv_dir = MV_DIR_FORWARD;
1295 if (IS_8X8(mb_type)) {
1296 s->mv_type = MV_TYPE_8X8;
1298 s->mv_type = MV_TYPE_16X16;
1301 } else { /* I-Frame */
1303 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1306 if (!IS_SKIP(mb_type)) {
1308 s->bdsp.clear_blocks(s->block[0]);
1309 /* decode each block */
1310 for (i = 0; i < 6; i++) {
1311 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, s->mb_intra, ctx->rvlc) < 0) {
1312 av_log(s->avctx, AV_LOG_ERROR,
1313 "texture corrupted at %d %d %d\n",
1314 s->mb_x, s->mb_y, s->mb_intra);
1321 /* per-MB end of slice check */
1322 if (--s->mb_num_left <= 0) {
1323 if (mpeg4_is_resync(ctx))
1328 if (mpeg4_is_resync(ctx)) {
1329 const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
1330 if (s->cbp_table[xy + delta])
1337 static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
1339 Mpeg4DecContext *ctx = (Mpeg4DecContext *)s;
1340 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1342 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1343 const int xy = s->mb_x + s->mb_y * s->mb_stride;
1345 av_assert2(s->h263_pred);
1347 if (s->pict_type == AV_PICTURE_TYPE_P ||
1348 s->pict_type == AV_PICTURE_TYPE_S) {
1350 if (get_bits1(&s->gb)) {
1353 for (i = 0; i < 6; i++)
1354 s->block_last_index[i] = -1;
1355 s->mv_dir = MV_DIR_FORWARD;
1356 s->mv_type = MV_TYPE_16X16;
1357 if (s->pict_type == AV_PICTURE_TYPE_S &&
1358 ctx->vol_sprite_usage == GMC_SPRITE) {
1359 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1364 s->mv[0][0][0] = get_amv(ctx, 0);
1365 s->mv[0][0][1] = get_amv(ctx, 1);
1368 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1378 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
1380 av_log(s->avctx, AV_LOG_ERROR,
1381 "mcbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1384 } while (cbpc == 20);
1386 s->bdsp.clear_blocks(s->block[0]);
1388 s->mb_intra = ((cbpc & 4) != 0);
1392 if (s->pict_type == AV_PICTURE_TYPE_S &&
1393 ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
1394 s->mcsel = get_bits1(&s->gb);
1397 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
1399 av_log(s->avctx, AV_LOG_ERROR,
1400 "P cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1401 return AVERROR_INVALIDDATA;
1404 cbp = (cbpc & 3) | (cbpy << 2);
1406 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1407 if ((!s->progressive_sequence) &&
1408 (cbp || (s->workaround_bugs & FF_BUG_XVID_ILACE)))
1409 s->interlaced_dct = get_bits1(&s->gb);
1411 s->mv_dir = MV_DIR_FORWARD;
1412 if ((cbpc & 16) == 0) {
1414 s->current_picture.mb_type[xy] = MB_TYPE_GMC |
1417 /* 16x16 global motion prediction */
1418 s->mv_type = MV_TYPE_16X16;
1419 mx = get_amv(ctx, 0);
1420 my = get_amv(ctx, 1);
1421 s->mv[0][0][0] = mx;
1422 s->mv[0][0][1] = my;
1423 } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
1424 s->current_picture.mb_type[xy] = MB_TYPE_16x8 |
1427 /* 16x8 field motion prediction */
1428 s->mv_type = MV_TYPE_FIELD;
1430 s->field_select[0][0] = get_bits1(&s->gb);
1431 s->field_select[0][1] = get_bits1(&s->gb);
1433 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1435 for (i = 0; i < 2; i++) {
1436 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1440 my = ff_h263_decode_motion(s, pred_y / 2, s->f_code);
1444 s->mv[0][i][0] = mx;
1445 s->mv[0][i][1] = my;
1448 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1449 /* 16x16 motion prediction */
1450 s->mv_type = MV_TYPE_16X16;
1451 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1452 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1457 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1461 s->mv[0][0][0] = mx;
1462 s->mv[0][0][1] = my;
1465 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
1466 s->mv_type = MV_TYPE_8X8;
1467 for (i = 0; i < 4; i++) {
1468 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1469 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1473 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1476 s->mv[0][i][0] = mx;
1477 s->mv[0][i][1] = my;
1482 } else if (s->pict_type == AV_PICTURE_TYPE_B) {
1483 int modb1; // first bit of modb
1484 int modb2; // second bit of modb
1487 s->mb_intra = 0; // B-frames never contain intra blocks
1488 s->mcsel = 0; // ... true gmc blocks
1491 for (i = 0; i < 2; i++) {
1492 s->last_mv[i][0][0] =
1493 s->last_mv[i][0][1] =
1494 s->last_mv[i][1][0] =
1495 s->last_mv[i][1][1] = 0;
1498 ff_thread_await_progress(&s->next_picture_ptr->tf, s->mb_y, 0);
1501 /* if we skipped it in the future P-frame than skip it now too */
1502 s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
1504 if (s->mb_skipped) {
1506 for (i = 0; i < 6; i++)
1507 s->block_last_index[i] = -1;
1509 s->mv_dir = MV_DIR_FORWARD;
1510 s->mv_type = MV_TYPE_16X16;
1515 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1521 modb1 = get_bits1(&s->gb);
1523 // like MB_TYPE_B_DIRECT but no vectors coded
1524 mb_type = MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1;
1527 modb2 = get_bits1(&s->gb);
1528 mb_type = get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
1530 av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
1533 mb_type = mb_type_b_map[mb_type];
1537 s->bdsp.clear_blocks(s->block[0]);
1538 cbp = get_bits(&s->gb, 6);
1541 if ((!IS_DIRECT(mb_type)) && cbp) {
1542 if (get_bits1(&s->gb))
1543 ff_set_qscale(s, s->qscale + get_bits1(&s->gb) * 4 - 2);
1546 if (!s->progressive_sequence) {
1548 s->interlaced_dct = get_bits1(&s->gb);
1550 if (!IS_DIRECT(mb_type) && get_bits1(&s->gb)) {
1551 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1552 mb_type &= ~MB_TYPE_16x16;
1554 if (USES_LIST(mb_type, 0)) {
1555 s->field_select[0][0] = get_bits1(&s->gb);
1556 s->field_select[0][1] = get_bits1(&s->gb);
1558 if (USES_LIST(mb_type, 1)) {
1559 s->field_select[1][0] = get_bits1(&s->gb);
1560 s->field_select[1][1] = get_bits1(&s->gb);
1566 if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
1567 s->mv_type = MV_TYPE_16X16;
1569 if (USES_LIST(mb_type, 0)) {
1570 s->mv_dir = MV_DIR_FORWARD;
1572 mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1573 my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1574 s->last_mv[0][1][0] =
1575 s->last_mv[0][0][0] =
1576 s->mv[0][0][0] = mx;
1577 s->last_mv[0][1][1] =
1578 s->last_mv[0][0][1] =
1579 s->mv[0][0][1] = my;
1582 if (USES_LIST(mb_type, 1)) {
1583 s->mv_dir |= MV_DIR_BACKWARD;
1585 mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1586 my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1587 s->last_mv[1][1][0] =
1588 s->last_mv[1][0][0] =
1589 s->mv[1][0][0] = mx;
1590 s->last_mv[1][1][1] =
1591 s->last_mv[1][0][1] =
1592 s->mv[1][0][1] = my;
1594 } else if (!IS_DIRECT(mb_type)) {
1595 s->mv_type = MV_TYPE_FIELD;
1597 if (USES_LIST(mb_type, 0)) {
1598 s->mv_dir = MV_DIR_FORWARD;
1600 for (i = 0; i < 2; i++) {
1601 mx = ff_h263_decode_motion(s, s->last_mv[0][i][0], s->f_code);
1602 my = ff_h263_decode_motion(s, s->last_mv[0][i][1] / 2, s->f_code);
1603 s->last_mv[0][i][0] =
1604 s->mv[0][i][0] = mx;
1605 s->last_mv[0][i][1] = (s->mv[0][i][1] = my) * 2;
1609 if (USES_LIST(mb_type, 1)) {
1610 s->mv_dir |= MV_DIR_BACKWARD;
1612 for (i = 0; i < 2; i++) {
1613 mx = ff_h263_decode_motion(s, s->last_mv[1][i][0], s->b_code);
1614 my = ff_h263_decode_motion(s, s->last_mv[1][i][1] / 2, s->b_code);
1615 s->last_mv[1][i][0] =
1616 s->mv[1][i][0] = mx;
1617 s->last_mv[1][i][1] = (s->mv[1][i][1] = my) * 2;
1623 if (IS_DIRECT(mb_type)) {
1624 if (IS_SKIP(mb_type)) {
1628 mx = ff_h263_decode_motion(s, 0, 1);
1629 my = ff_h263_decode_motion(s, 0, 1);
1632 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1633 mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
1635 s->current_picture.mb_type[xy] = mb_type;
1636 } else { /* I-Frame */
1638 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
1640 av_log(s->avctx, AV_LOG_ERROR,
1641 "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1644 } while (cbpc == 8);
1650 s->ac_pred = get_bits1(&s->gb);
1652 s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
1654 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
1656 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1658 av_log(s->avctx, AV_LOG_ERROR,
1659 "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1662 cbp = (cbpc & 3) | (cbpy << 2);
1664 ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1667 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1669 if (!s->progressive_sequence)
1670 s->interlaced_dct = get_bits1(&s->gb);
1672 s->bdsp.clear_blocks(s->block[0]);
1673 /* decode each block */
1674 for (i = 0; i < 6; i++) {
1675 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 1, 0) < 0)
1682 /* decode each block */
1683 for (i = 0; i < 6; i++) {
1684 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 0, 0) < 0)
1690 /* per-MB end of slice check */
1691 if (s->codec_id == AV_CODEC_ID_MPEG4) {
1692 int next = mpeg4_is_resync(ctx);
1694 if (s->mb_x + s->mb_y*s->mb_width + 1 > next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
1696 } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
1699 if (s->pict_type == AV_PICTURE_TYPE_B) {
1700 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
1701 ff_thread_await_progress(&s->next_picture_ptr->tf,
1702 (s->mb_x + delta >= s->mb_width)
1703 ? FFMIN(s->mb_y + 1, s->mb_height - 1)
1705 if (s->next_picture.mbskip_table[xy + delta])
1716 static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
1718 int hours, minutes, seconds;
1720 if (!show_bits(gb, 23)) {
1721 av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
1725 hours = get_bits(gb, 5);
1726 minutes = get_bits(gb, 6);
1727 check_marker(s->avctx, gb, "in gop_header");
1728 seconds = get_bits(gb, 6);
1730 s->time_base = seconds + 60*(minutes + 60*hours);
1738 static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb)
1741 s->avctx->profile = get_bits(gb, 4);
1742 s->avctx->level = get_bits(gb, 4);
1744 // for Simple profile, level 0
1745 if (s->avctx->profile == 0 && s->avctx->level == 8) {
1746 s->avctx->level = 0;
1752 static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
1754 MpegEncContext *s = &ctx->m;
1755 int width, height, vo_ver_id;
1758 skip_bits(gb, 1); /* random access */
1759 s->vo_type = get_bits(gb, 8);
1760 if (get_bits1(gb) != 0) { /* is_ol_id */
1761 vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
1762 skip_bits(gb, 3); /* vo_priority */
1766 s->aspect_ratio_info = get_bits(gb, 4);
1767 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
1768 s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
1769 s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
1771 s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
1774 if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
1775 int chroma_format = get_bits(gb, 2);
1776 if (chroma_format != CHROMA_420)
1777 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
1779 s->low_delay = get_bits1(gb);
1780 if (get_bits1(gb)) { /* vbv parameters */
1781 get_bits(gb, 15); /* first_half_bitrate */
1782 check_marker(s->avctx, gb, "after first_half_bitrate");
1783 get_bits(gb, 15); /* latter_half_bitrate */
1784 check_marker(s->avctx, gb, "after latter_half_bitrate");
1785 get_bits(gb, 15); /* first_half_vbv_buffer_size */
1786 check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
1787 get_bits(gb, 3); /* latter_half_vbv_buffer_size */
1788 get_bits(gb, 11); /* first_half_vbv_occupancy */
1789 check_marker(s->avctx, gb, "after first_half_vbv_occupancy");
1790 get_bits(gb, 15); /* latter_half_vbv_occupancy */
1791 check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
1794 /* is setting low delay flag only once the smartest thing to do?
1795 * low delay detection will not be overridden. */
1796 if (s->picture_number == 0) {
1797 switch(s->vo_type) {
1798 case SIMPLE_VO_TYPE:
1799 case ADV_SIMPLE_VO_TYPE:
1808 ctx->shape = get_bits(gb, 2); /* vol shape */
1809 if (ctx->shape != RECT_SHAPE)
1810 av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
1811 if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
1812 av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
1813 skip_bits(gb, 4); /* video_object_layer_shape_extension */
1816 check_marker(s->avctx, gb, "before time_increment_resolution");
1818 s->avctx->framerate.num = get_bits(gb, 16);
1819 if (!s->avctx->framerate.num) {
1820 av_log(s->avctx, AV_LOG_ERROR, "framerate==0\n");
1821 return AVERROR_INVALIDDATA;
1824 ctx->time_increment_bits = av_log2(s->avctx->framerate.num - 1) + 1;
1825 if (ctx->time_increment_bits < 1)
1826 ctx->time_increment_bits = 1;
1828 check_marker(s->avctx, gb, "before fixed_vop_rate");
1830 if (get_bits1(gb) != 0) /* fixed_vop_rate */
1831 s->avctx->framerate.den = get_bits(gb, ctx->time_increment_bits);
1833 s->avctx->framerate.den = 1;
1835 s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
1839 if (ctx->shape != BIN_ONLY_SHAPE) {
1840 if (ctx->shape == RECT_SHAPE) {
1841 check_marker(s->avctx, gb, "before width");
1842 width = get_bits(gb, 13);
1843 check_marker(s->avctx, gb, "before height");
1844 height = get_bits(gb, 13);
1845 check_marker(s->avctx, gb, "after height");
1846 if (width && height && /* they should be non zero but who knows */
1847 !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
1848 if (s->width && s->height &&
1849 (s->width != width || s->height != height))
1850 s->context_reinit = 1;
1856 s->progressive_sequence =
1857 s->progressive_frame = get_bits1(gb) ^ 1;
1858 s->interlaced_dct = 0;
1859 if (!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
1860 av_log(s->avctx, AV_LOG_INFO, /* OBMC Disable */
1861 "MPEG-4 OBMC not supported (very likely buggy encoder)\n");
1863 ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
1865 ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
1867 if (ctx->vol_sprite_usage == STATIC_SPRITE)
1868 av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
1869 if (ctx->vol_sprite_usage == STATIC_SPRITE ||
1870 ctx->vol_sprite_usage == GMC_SPRITE) {
1871 if (ctx->vol_sprite_usage == STATIC_SPRITE) {
1872 skip_bits(gb, 13); // sprite_width
1873 check_marker(s->avctx, gb, "after sprite_width");
1874 skip_bits(gb, 13); // sprite_height
1875 check_marker(s->avctx, gb, "after sprite_height");
1876 skip_bits(gb, 13); // sprite_left
1877 check_marker(s->avctx, gb, "after sprite_left");
1878 skip_bits(gb, 13); // sprite_top
1879 check_marker(s->avctx, gb, "after sprite_top");
1881 ctx->num_sprite_warping_points = get_bits(gb, 6);
1882 if (ctx->num_sprite_warping_points > 3) {
1883 av_log(s->avctx, AV_LOG_ERROR,
1884 "%d sprite_warping_points\n",
1885 ctx->num_sprite_warping_points);
1886 ctx->num_sprite_warping_points = 0;
1887 return AVERROR_INVALIDDATA;
1889 s->sprite_warping_accuracy = get_bits(gb, 2);
1890 ctx->sprite_brightness_change = get_bits1(gb);
1891 if (ctx->vol_sprite_usage == STATIC_SPRITE)
1892 skip_bits1(gb); // low_latency_sprite
1894 // FIXME sadct disable bit if verid!=1 && shape not rect
1896 if (get_bits1(gb) == 1) { /* not_8_bit */
1897 s->quant_precision = get_bits(gb, 4); /* quant_precision */
1898 if (get_bits(gb, 4) != 8) /* bits_per_pixel */
1899 av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
1900 if (s->quant_precision != 5)
1901 av_log(s->avctx, AV_LOG_ERROR,
1902 "quant precision %d\n", s->quant_precision);
1903 if (s->quant_precision<3 || s->quant_precision>9) {
1904 s->quant_precision = 5;
1907 s->quant_precision = 5;
1910 // FIXME a bunch of grayscale shape things
1912 if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
1915 /* load default matrixes */
1916 for (i = 0; i < 64; i++) {
1917 int j = s->idsp.idct_permutation[i];
1918 v = ff_mpeg4_default_intra_matrix[i];
1919 s->intra_matrix[j] = v;
1920 s->chroma_intra_matrix[j] = v;
1922 v = ff_mpeg4_default_non_intra_matrix[i];
1923 s->inter_matrix[j] = v;
1924 s->chroma_inter_matrix[j] = v;
1927 /* load custom intra matrix */
1928 if (get_bits1(gb)) {
1930 for (i = 0; i < 64; i++) {
1932 if (get_bits_left(gb) < 8) {
1933 av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
1934 return AVERROR_INVALIDDATA;
1936 v = get_bits(gb, 8);
1941 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1942 s->intra_matrix[j] = last;
1943 s->chroma_intra_matrix[j] = last;
1946 /* replicate last value */
1947 for (; i < 64; i++) {
1948 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1949 s->intra_matrix[j] = last;
1950 s->chroma_intra_matrix[j] = last;
1954 /* load custom non intra matrix */
1955 if (get_bits1(gb)) {
1957 for (i = 0; i < 64; i++) {
1959 if (get_bits_left(gb) < 8) {
1960 av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
1961 return AVERROR_INVALIDDATA;
1963 v = get_bits(gb, 8);
1968 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1969 s->inter_matrix[j] = v;
1970 s->chroma_inter_matrix[j] = v;
1973 /* replicate last value */
1974 for (; i < 64; i++) {
1975 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1976 s->inter_matrix[j] = last;
1977 s->chroma_inter_matrix[j] = last;
1981 // FIXME a bunch of grayscale shape things
1985 s->quarter_sample = get_bits1(gb);
1987 s->quarter_sample = 0;
1989 if (get_bits_left(gb) < 4) {
1990 av_log(s->avctx, AV_LOG_ERROR, "VOL Header truncated\n");
1991 return AVERROR_INVALIDDATA;
1994 if (!get_bits1(gb)) {
1995 int pos = get_bits_count(gb);
1996 int estimation_method = get_bits(gb, 2);
1997 if (estimation_method < 2) {
1998 if (!get_bits1(gb)) {
1999 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */
2000 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */
2001 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */
2002 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */
2003 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */
2004 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upsampling */
2006 if (!get_bits1(gb)) {
2007 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */
2008 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */
2009 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */
2010 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */
2012 if (!check_marker(s->avctx, gb, "in complexity estimation part 1")) {
2013 skip_bits_long(gb, pos - get_bits_count(gb));
2016 if (!get_bits1(gb)) {
2017 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */
2018 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */
2019 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */
2020 ctx->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */
2022 if (!get_bits1(gb)) {
2023 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */
2024 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */
2025 ctx->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */
2026 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */
2027 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */
2028 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */
2030 if (!check_marker(s->avctx, gb, "in complexity estimation part 2")) {
2031 skip_bits_long(gb, pos - get_bits_count(gb));
2034 if (estimation_method == 1) {
2035 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */
2036 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */
2039 av_log(s->avctx, AV_LOG_ERROR,
2040 "Invalid Complexity estimation method %d\n",
2045 ctx->cplx_estimation_trash_i =
2046 ctx->cplx_estimation_trash_p =
2047 ctx->cplx_estimation_trash_b = 0;
2050 ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
2052 s->data_partitioning = get_bits1(gb);
2053 if (s->data_partitioning)
2054 ctx->rvlc = get_bits1(gb);
2056 if (vo_ver_id != 1) {
2057 ctx->new_pred = get_bits1(gb);
2058 if (ctx->new_pred) {
2059 av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
2060 skip_bits(gb, 2); /* requested upstream message type */
2061 skip_bits1(gb); /* newpred segment type */
2063 if (get_bits1(gb)) // reduced_res_vop
2064 av_log(s->avctx, AV_LOG_ERROR,
2065 "reduced resolution VOP not supported\n");
2070 ctx->scalability = get_bits1(gb);
2072 if (ctx->scalability) {
2073 GetBitContext bak = *gb;
2074 int h_sampling_factor_n;
2075 int h_sampling_factor_m;
2076 int v_sampling_factor_n;
2077 int v_sampling_factor_m;
2079 skip_bits1(gb); // hierarchy_type
2080 skip_bits(gb, 4); /* ref_layer_id */
2081 skip_bits1(gb); /* ref_layer_sampling_dir */
2082 h_sampling_factor_n = get_bits(gb, 5);
2083 h_sampling_factor_m = get_bits(gb, 5);
2084 v_sampling_factor_n = get_bits(gb, 5);
2085 v_sampling_factor_m = get_bits(gb, 5);
2086 ctx->enhancement_type = get_bits1(gb);
2088 if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
2089 v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
2090 /* illegal scalability header (VERY broken encoder),
2091 * trying to workaround */
2092 ctx->scalability = 0;
2095 av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
2097 // bin shape stuff FIXME
2101 if (s->avctx->debug&FF_DEBUG_PICT_INFO) {
2102 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",
2103 s->avctx->framerate.den, s->avctx->framerate.num,
2104 ctx->time_increment_bits,
2106 s->progressive_sequence,
2108 ctx->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
2109 s->data_partitioning ? "partition " : "", ctx->rvlc ? "rvlc " : ""
2117 * Decode the user data stuff in the header.
2118 * Also initializes divx/xvid/lavc_version/build.
2120 static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
2122 MpegEncContext *s = &ctx->m;
2126 int ver = 0, build = 0, ver2 = 0, ver3 = 0;
2129 for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
2130 if (show_bits(gb, 23) == 0)
2132 buf[i] = get_bits(gb, 8);
2136 /* divx detection */
2137 e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
2139 e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
2141 ctx->divx_version = ver;
2142 ctx->divx_build = build;
2143 s->divx_packed = e == 3 && last == 'p';
2146 /* libavcodec detection */
2147 e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
2149 e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
2151 e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
2153 build = (ver << 16) + (ver2 << 8) + ver3;
2156 if (strcmp(buf, "ffmpeg") == 0)
2157 ctx->lavc_build = 4600;
2160 ctx->lavc_build = build;
2162 /* Xvid detection */
2163 e = sscanf(buf, "XviD%d", &build);
2165 ctx->xvid_build = build;
2170 int ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
2172 Mpeg4DecContext *ctx = avctx->priv_data;
2173 MpegEncContext *s = &ctx->m;
2175 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
2176 if (s->codec_tag == AV_RL32("XVID") ||
2177 s->codec_tag == AV_RL32("XVIX") ||
2178 s->codec_tag == AV_RL32("RMP4") ||
2179 s->codec_tag == AV_RL32("ZMP4") ||
2180 s->codec_tag == AV_RL32("SIPP"))
2181 ctx->xvid_build = 0;
2184 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
2185 if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
2186 ctx->vol_control_parameters == 0)
2187 ctx->divx_version = 400; // divx 4
2189 if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
2191 ctx->divx_build = -1;
2194 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
2195 if (s->codec_tag == AV_RL32("XVIX"))
2196 s->workaround_bugs |= FF_BUG_XVID_ILACE;
2198 if (s->codec_tag == AV_RL32("UMP4"))
2199 s->workaround_bugs |= FF_BUG_UMP4;
2201 if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
2202 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2204 if (ctx->divx_version > 502 && ctx->divx_build < 1814)
2205 s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
2207 if (ctx->xvid_build <= 3U)
2208 s->padding_bug_score = 256 * 256 * 256 * 64;
2210 if (ctx->xvid_build <= 1U)
2211 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2213 if (ctx->xvid_build <= 12U)
2214 s->workaround_bugs |= FF_BUG_EDGE;
2216 if (ctx->xvid_build <= 32U)
2217 s->workaround_bugs |= FF_BUG_DC_CLIP;
2219 #define SET_QPEL_FUNC(postfix1, postfix2) \
2220 s->qdsp.put_ ## postfix1 = ff_put_ ## postfix2; \
2221 s->qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
2222 s->qdsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
2224 if (ctx->lavc_build < 4653U)
2225 s->workaround_bugs |= FF_BUG_STD_QPEL;
2227 if (ctx->lavc_build < 4655U)
2228 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2230 if (ctx->lavc_build < 4670U)
2231 s->workaround_bugs |= FF_BUG_EDGE;
2233 if (ctx->lavc_build <= 4712U)
2234 s->workaround_bugs |= FF_BUG_DC_CLIP;
2236 if ((ctx->lavc_build&0xFF) >= 100) {
2237 if (ctx->lavc_build > 3621476 && ctx->lavc_build < 3752552 &&
2238 (ctx->lavc_build < 3752037 || ctx->lavc_build > 3752191) // 3.2.1+
2240 s->workaround_bugs |= FF_BUG_IEDGE;
2243 if (ctx->divx_version >= 0)
2244 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2245 if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
2246 s->padding_bug_score = 256 * 256 * 256 * 64;
2248 if (ctx->divx_version < 500U)
2249 s->workaround_bugs |= FF_BUG_EDGE;
2251 if (ctx->divx_version >= 0)
2252 s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
2255 if (s->workaround_bugs & FF_BUG_STD_QPEL) {
2256 SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
2257 SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
2258 SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
2259 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
2260 SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
2261 SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
2263 SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
2264 SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
2265 SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
2266 SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
2267 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
2268 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
2271 if (avctx->debug & FF_DEBUG_BUGS)
2272 av_log(s->avctx, AV_LOG_DEBUG,
2273 "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
2274 s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
2275 ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
2277 if (CONFIG_MPEG4_DECODER && ctx->xvid_build >= 0 &&
2278 s->codec_id == AV_CODEC_ID_MPEG4 &&
2279 avctx->idct_algo == FF_IDCT_AUTO) {
2280 avctx->idct_algo = FF_IDCT_XVID;
2281 ff_mpv_idct_init(s);
2288 static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2290 MpegEncContext *s = &ctx->m;
2291 int time_incr, time_increment;
2295 s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
2296 if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
2297 ctx->vol_control_parameters == 0 && !(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)) {
2298 av_log(s->avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n");
2302 s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
2303 if (s->partitioned_frame)
2304 s->decode_mb = mpeg4_decode_partitioned_mb;
2306 s->decode_mb = mpeg4_decode_mb;
2309 while (get_bits1(gb) != 0)
2312 check_marker(s->avctx, gb, "before time_increment");
2314 if (ctx->time_increment_bits == 0 ||
2315 !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
2316 av_log(s->avctx, AV_LOG_WARNING,
2317 "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);
2319 for (ctx->time_increment_bits = 1;
2320 ctx->time_increment_bits < 16;
2321 ctx->time_increment_bits++) {
2322 if (s->pict_type == AV_PICTURE_TYPE_P ||
2323 (s->pict_type == AV_PICTURE_TYPE_S &&
2324 ctx->vol_sprite_usage == GMC_SPRITE)) {
2325 if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
2327 } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
2331 av_log(s->avctx, AV_LOG_WARNING,
2332 "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits);
2333 if (s->avctx->framerate.num && 4*s->avctx->framerate.num < 1<<ctx->time_increment_bits) {
2334 s->avctx->framerate.num = 1<<ctx->time_increment_bits;
2335 s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
2340 time_increment = get_bits1(gb); // FIXME investigate further
2342 time_increment = get_bits(gb, ctx->time_increment_bits);
2344 if (s->pict_type != AV_PICTURE_TYPE_B) {
2345 s->last_time_base = s->time_base;
2346 s->time_base += time_incr;
2347 s->time = s->time_base * (int64_t)s->avctx->framerate.num + time_increment;
2348 if (s->workaround_bugs & FF_BUG_UMP4) {
2349 if (s->time < s->last_non_b_time) {
2350 /* header is not mpeg-4-compatible, broken encoder,
2351 * trying to workaround */
2353 s->time += s->avctx->framerate.num;
2356 s->pp_time = s->time - s->last_non_b_time;
2357 s->last_non_b_time = s->time;
2359 s->time = (s->last_time_base + time_incr) * (int64_t)s->avctx->framerate.num + time_increment;
2360 s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
2361 if (s->pp_time <= s->pb_time ||
2362 s->pp_time <= s->pp_time - s->pb_time ||
2364 /* messed up order, maybe after seeking? skipping current B-frame */
2365 return FRAME_SKIPPED;
2367 ff_mpeg4_init_direct_mv(s);
2369 if (ctx->t_frame == 0)
2370 ctx->t_frame = s->pb_time;
2371 if (ctx->t_frame == 0)
2372 ctx->t_frame = 1; // 1/0 protection
2373 s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
2374 ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2375 s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
2376 ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2377 if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1) {
2378 s->pb_field_time = 2;
2379 s->pp_field_time = 4;
2380 if (!s->progressive_sequence)
2381 return FRAME_SKIPPED;
2385 if (s->avctx->framerate.den)
2386 pts = ROUNDED_DIV(s->time, s->avctx->framerate.den);
2388 pts = AV_NOPTS_VALUE;
2389 ff_dlog(s->avctx, "MPEG4 PTS: %"PRId64"\n", pts);
2391 check_marker(s->avctx, gb, "before vop_coded");
2394 if (get_bits1(gb) != 1) {
2395 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2396 av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
2397 return FRAME_SKIPPED;
2400 decode_new_pred(ctx, gb);
2402 if (ctx->shape != BIN_ONLY_SHAPE &&
2403 (s->pict_type == AV_PICTURE_TYPE_P ||
2404 (s->pict_type == AV_PICTURE_TYPE_S &&
2405 ctx->vol_sprite_usage == GMC_SPRITE))) {
2406 /* rounding type for motion estimation */
2407 s->no_rounding = get_bits1(gb);
2411 // FIXME reduced res stuff
2413 if (ctx->shape != RECT_SHAPE) {
2414 if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
2415 skip_bits(gb, 13); /* width */
2416 check_marker(s->avctx, gb, "after width");
2417 skip_bits(gb, 13); /* height */
2418 check_marker(s->avctx, gb, "after height");
2419 skip_bits(gb, 13); /* hor_spat_ref */
2420 check_marker(s->avctx, gb, "after hor_spat_ref");
2421 skip_bits(gb, 13); /* ver_spat_ref */
2423 skip_bits1(gb); /* change_CR_disable */
2425 if (get_bits1(gb) != 0)
2426 skip_bits(gb, 8); /* constant_alpha_value */
2429 // FIXME complexity estimation stuff
2431 if (ctx->shape != BIN_ONLY_SHAPE) {
2432 skip_bits_long(gb, ctx->cplx_estimation_trash_i);
2433 if (s->pict_type != AV_PICTURE_TYPE_I)
2434 skip_bits_long(gb, ctx->cplx_estimation_trash_p);
2435 if (s->pict_type == AV_PICTURE_TYPE_B)
2436 skip_bits_long(gb, ctx->cplx_estimation_trash_b);
2438 if (get_bits_left(gb) < 3) {
2439 av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
2440 return AVERROR_INVALIDDATA;
2442 ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
2443 if (!s->progressive_sequence) {
2444 s->top_field_first = get_bits1(gb);
2445 s->alternate_scan = get_bits1(gb);
2447 s->alternate_scan = 0;
2450 if (s->alternate_scan) {
2451 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
2452 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
2453 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
2454 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2456 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
2457 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
2458 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
2459 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2462 if (s->pict_type == AV_PICTURE_TYPE_S) {
2463 if((ctx->vol_sprite_usage == STATIC_SPRITE ||
2464 ctx->vol_sprite_usage == GMC_SPRITE)) {
2465 if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
2466 return AVERROR_INVALIDDATA;
2467 if (ctx->sprite_brightness_change)
2468 av_log(s->avctx, AV_LOG_ERROR,
2469 "sprite_brightness_change not supported\n");
2470 if (ctx->vol_sprite_usage == STATIC_SPRITE)
2471 av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
2473 memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
2474 memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
2478 if (ctx->shape != BIN_ONLY_SHAPE) {
2479 s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision);
2480 if (s->qscale == 0) {
2481 av_log(s->avctx, AV_LOG_ERROR,
2482 "Error, header damaged or not MPEG-4 header (qscale=0)\n");
2483 return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
2486 if (s->pict_type != AV_PICTURE_TYPE_I) {
2487 s->f_code = get_bits(gb, 3); /* fcode_for */
2488 if (s->f_code == 0) {
2489 av_log(s->avctx, AV_LOG_ERROR,
2490 "Error, header damaged or not MPEG-4 header (f_code=0)\n");
2492 return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
2497 if (s->pict_type == AV_PICTURE_TYPE_B) {
2498 s->b_code = get_bits(gb, 3);
2499 if (s->b_code == 0) {
2500 av_log(s->avctx, AV_LOG_ERROR,
2501 "Error, header damaged or not MPEG4 header (b_code=0)\n");
2503 return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly
2508 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2509 av_log(s->avctx, AV_LOG_DEBUG,
2510 "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",
2511 s->qscale, s->f_code, s->b_code,
2512 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")),
2513 gb->size_in_bits,s->progressive_sequence, s->alternate_scan,
2514 s->top_field_first, s->quarter_sample ? "q" : "h",
2515 s->data_partitioning, ctx->resync_marker,
2516 ctx->num_sprite_warping_points, s->sprite_warping_accuracy,
2517 1 - s->no_rounding, s->vo_type,
2518 ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
2519 ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
2520 ctx->cplx_estimation_trash_b,
2526 if (!ctx->scalability) {
2527 if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
2528 skip_bits1(gb); // vop shape coding type
2530 if (ctx->enhancement_type) {
2531 int load_backward_shape = get_bits1(gb);
2532 if (load_backward_shape)
2533 av_log(s->avctx, AV_LOG_ERROR,
2534 "load backward shape isn't supported\n");
2536 skip_bits(gb, 2); // ref_select_code
2539 /* detect buggy encoders which don't set the low_delay flag
2540 * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames
2541 * easily (although it's buggy too) */
2542 if (s->vo_type == 0 && ctx->vol_control_parameters == 0 &&
2543 ctx->divx_version == -1 && s->picture_number == 0) {
2544 av_log(s->avctx, AV_LOG_WARNING,
2545 "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
2549 s->picture_number++; // better than pic number==0 always ;)
2551 // FIXME add short header support
2552 s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
2553 s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
2555 if (s->workaround_bugs & FF_BUG_EDGE) {
2556 s->h_edge_pos = s->width;
2557 s->v_edge_pos = s->height;
2563 * Decode MPEG-4 headers.
2564 * @return <0 if no VOP found (or a damaged one)
2565 * FRAME_SKIPPED if a not coded VOP is found
2566 * 0 if a VOP is found
2568 int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2570 MpegEncContext *s = &ctx->m;
2571 unsigned startcode, v;
2575 /* search next start code */
2578 if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
2580 if (get_bits(gb, 8) == 0xF0)
2586 if (get_bits_count(gb) >= gb->size_in_bits) {
2587 if (gb->size_in_bits == 8 &&
2588 (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
2589 av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
2590 return FRAME_SKIPPED; // divx bug
2592 return -1; // end of stream
2595 /* use the bits after the test */
2596 v = get_bits(gb, 8);
2597 startcode = ((startcode << 8) | v) & 0xffffffff;
2599 if ((startcode & 0xFFFFFF00) != 0x100)
2600 continue; // no startcode
2602 if (s->avctx->debug & FF_DEBUG_STARTCODE) {
2603 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
2604 if (startcode <= 0x11F)
2605 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
2606 else if (startcode <= 0x12F)
2607 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
2608 else if (startcode <= 0x13F)
2609 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2610 else if (startcode <= 0x15F)
2611 av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
2612 else if (startcode <= 0x1AF)
2613 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2614 else if (startcode == 0x1B0)
2615 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
2616 else if (startcode == 0x1B1)
2617 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
2618 else if (startcode == 0x1B2)
2619 av_log(s->avctx, AV_LOG_DEBUG, "User Data");
2620 else if (startcode == 0x1B3)
2621 av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
2622 else if (startcode == 0x1B4)
2623 av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
2624 else if (startcode == 0x1B5)
2625 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
2626 else if (startcode == 0x1B6)
2627 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
2628 else if (startcode == 0x1B7)
2629 av_log(s->avctx, AV_LOG_DEBUG, "slice start");
2630 else if (startcode == 0x1B8)
2631 av_log(s->avctx, AV_LOG_DEBUG, "extension start");
2632 else if (startcode == 0x1B9)
2633 av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
2634 else if (startcode == 0x1BA)
2635 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
2636 else if (startcode == 0x1BB)
2637 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
2638 else if (startcode == 0x1BC)
2639 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
2640 else if (startcode == 0x1BD)
2641 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
2642 else if (startcode == 0x1BE)
2643 av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
2644 else if (startcode == 0x1BF)
2645 av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
2646 else if (startcode == 0x1C0)
2647 av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
2648 else if (startcode == 0x1C1)
2649 av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
2650 else if (startcode == 0x1C2)
2651 av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
2652 else if (startcode == 0x1C3)
2653 av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
2654 else if (startcode <= 0x1C5)
2655 av_log(s->avctx, AV_LOG_DEBUG, "reserved");
2656 else if (startcode <= 0x1FF)
2657 av_log(s->avctx, AV_LOG_DEBUG, "System start");
2658 av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
2661 if (startcode >= 0x120 && startcode <= 0x12F) {
2663 av_log(s->avctx, AV_LOG_ERROR, "Multiple VOL headers");
2664 return AVERROR_INVALIDDATA;
2667 if ((ret = decode_vol_header(ctx, gb)) < 0)
2669 } else if (startcode == USER_DATA_STARTCODE) {
2670 decode_user_data(ctx, gb);
2671 } else if (startcode == GOP_STARTCODE) {
2672 mpeg4_decode_gop_header(s, gb);
2673 } else if (startcode == VOS_STARTCODE) {
2674 mpeg4_decode_profile_level(s, gb);
2675 } else if (startcode == VOP_STARTCODE) {
2684 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
2686 s->avctx->has_b_frames = !s->low_delay;
2688 return decode_vop_header(ctx, gb);
2691 av_cold void ff_mpeg4videodec_static_init(void) {
2692 static int done = 0;
2695 ff_rl_init(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
2696 ff_rl_init(&ff_rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
2697 ff_rl_init(&ff_rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
2698 INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
2699 INIT_VLC_RL(ff_rvlc_rl_inter, 1072);
2700 INIT_VLC_RL(ff_rvlc_rl_intra, 1072);
2701 INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
2702 &ff_mpeg4_DCtab_lum[0][1], 2, 1,
2703 &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
2704 INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
2705 &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
2706 &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
2707 INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
2708 &ff_sprite_trajectory_tab[0][1], 4, 2,
2709 &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
2710 INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
2711 &ff_mb_type_b_tab[0][1], 2, 1,
2712 &ff_mb_type_b_tab[0][0], 2, 1, 16);
2717 int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
2719 Mpeg4DecContext *ctx = avctx->priv_data;
2720 MpegEncContext *s = &ctx->m;
2722 /* divx 5.01+ bitstream reorder stuff */
2723 /* Since this clobbers the input buffer and hwaccel codecs still need the
2724 * data during hwaccel->end_frame we should not do this any earlier */
2725 if (s->divx_packed) {
2726 int current_pos = s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb) >> 3);
2727 int startcode_found = 0;
2729 if (buf_size - current_pos > 7) {
2732 for (i = current_pos; i < buf_size - 4; i++)
2737 buf[i + 3] == 0xB6) {
2738 startcode_found = !(buf[i + 4] & 0x40);
2743 if (startcode_found) {
2744 if (!ctx->showed_packed_warning) {
2745 av_log(s->avctx, AV_LOG_INFO, "Video uses a non-standard and "
2746 "wasteful way to store B-frames ('packed B-frames'). "
2747 "Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.\n");
2748 ctx->showed_packed_warning = 1;
2750 av_fast_padded_malloc(&s->bitstream_buffer,
2751 &s->allocated_bitstream_buffer_size,
2752 buf_size - current_pos);
2753 if (!s->bitstream_buffer) {
2754 s->bitstream_buffer_size = 0;
2755 return AVERROR(ENOMEM);
2757 memcpy(s->bitstream_buffer, buf + current_pos,
2758 buf_size - current_pos);
2759 s->bitstream_buffer_size = buf_size - current_pos;
2767 static int mpeg4_update_thread_context(AVCodecContext *dst,
2768 const AVCodecContext *src)
2770 Mpeg4DecContext *s = dst->priv_data;
2771 const Mpeg4DecContext *s1 = src->priv_data;
2772 int init = s->m.context_initialized;
2774 int ret = ff_mpeg_update_thread_context(dst, src);
2779 memcpy(((uint8_t*)s) + sizeof(MpegEncContext), ((uint8_t*)s1) + sizeof(MpegEncContext), sizeof(Mpeg4DecContext) - sizeof(MpegEncContext));
2781 if (CONFIG_MPEG4_DECODER && !init && s1->xvid_build >= 0)
2782 ff_xvid_idct_init(&s->m.idsp, dst);
2788 static av_cold int decode_init(AVCodecContext *avctx)
2790 Mpeg4DecContext *ctx = avctx->priv_data;
2791 MpegEncContext *s = &ctx->m;
2797 ctx->lavc_build = -1;
2799 if ((ret = ff_h263_decode_init(avctx)) < 0)
2802 ff_mpeg4videodec_static_init();
2805 s->low_delay = 0; /* default, might be overridden in the vol header during header parsing */
2806 s->decode_mb = mpeg4_decode_mb;
2807 ctx->time_increment_bits = 4; /* default value for broken headers */
2809 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
2810 avctx->internal->allocate_progress = 1;
2815 static const AVOption mpeg4_options[] = {
2816 {"quarter_sample", "1/4 subpel MC", offsetof(MpegEncContext, quarter_sample), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
2817 {"divx_packed", "divx style packed b frames", offsetof(MpegEncContext, divx_packed), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
2821 static const AVClass mpeg4_class = {
2822 "MPEG4 Video Decoder",
2823 av_default_item_name,
2825 LIBAVUTIL_VERSION_INT,
2828 AVCodec ff_mpeg4_decoder = {
2830 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
2831 .type = AVMEDIA_TYPE_VIDEO,
2832 .id = AV_CODEC_ID_MPEG4,
2833 .priv_data_size = sizeof(Mpeg4DecContext),
2834 .init = decode_init,
2835 .close = ff_h263_decode_end,
2836 .decode = ff_h263_decode_frame,
2837 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2838 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
2839 AV_CODEC_CAP_FRAME_THREADS,
2840 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2841 .flush = ff_mpeg_flush,
2843 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
2844 .profiles = NULL_IF_CONFIG_SMALL(ff_mpeg4_video_profiles),
2845 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg4_update_thread_context),
2846 .priv_class = &mpeg4_class,
2850 #if CONFIG_MPEG4_VDPAU_DECODER && FF_API_VDPAU
2851 static const AVClass mpeg4_vdpau_class = {
2852 "MPEG4 Video VDPAU Decoder",
2853 av_default_item_name,
2855 LIBAVUTIL_VERSION_INT,
2858 AVCodec ff_mpeg4_vdpau_decoder = {
2859 .name = "mpeg4_vdpau",
2860 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 (VDPAU)"),
2861 .type = AVMEDIA_TYPE_VIDEO,
2862 .id = AV_CODEC_ID_MPEG4,
2863 .priv_data_size = sizeof(Mpeg4DecContext),
2864 .init = decode_init,
2865 .close = ff_h263_decode_end,
2866 .decode = ff_h263_decode_frame,
2867 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
2868 AV_CODEC_CAP_HWACCEL_VDPAU,
2869 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_MPEG4,
2871 .priv_class = &mpeg4_vdpau_class,