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"
31 #include "mpegutils.h"
32 #include "mpegvideo.h"
33 #include "mpegvideodata.h"
34 #include "mpeg4video.h"
40 /* The defines below define the number of bits that are read at once for
41 * reading vlc values. Changing these may improve speed and data cache needs
42 * be aware though that decreasing them may need the number of stages that is
43 * passed to get_vlc* to be increased. */
44 #define SPRITE_TRAJ_VLC_BITS 6
46 #define MB_TYPE_B_VLC_BITS 4
47 #define STUDIO_INTRA_BITS 9
49 static VLC dc_lum, dc_chrom;
50 static VLC sprite_trajectory;
51 static VLC mb_type_b_vlc;
53 static const int mb_type_b_map[4] = {
54 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
55 MB_TYPE_L0L1 | MB_TYPE_16x16,
56 MB_TYPE_L1 | MB_TYPE_16x16,
57 MB_TYPE_L0 | MB_TYPE_16x16,
62 * @param n block index (0-3 are luma, 4-5 are chroma)
63 * @param dir the ac prediction direction
65 void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
68 int16_t *ac_val, *ac_val1;
69 int8_t *const qscale_table = s->current_picture.qscale_table;
72 ac_val = &s->ac_val[0][0][0] + s->block_index[n] * 16;
76 const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
80 if (s->mb_x == 0 || s->qscale == qscale_table[xy] ||
83 for (i = 1; i < 8; i++)
84 block[s->idsp.idct_permutation[i << 3]] += ac_val[i];
86 /* different qscale, we must rescale */
87 for (i = 1; i < 8; i++)
88 block[s->idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
91 const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
93 ac_val -= 16 * s->block_wrap[n];
95 if (s->mb_y == 0 || s->qscale == qscale_table[xy] ||
98 for (i = 1; i < 8; i++)
99 block[s->idsp.idct_permutation[i]] += ac_val[i + 8];
101 /* different qscale, we must rescale */
102 for (i = 1; i < 8; i++)
103 block[s->idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
108 for (i = 1; i < 8; i++)
109 ac_val1[i] = block[s->idsp.idct_permutation[i << 3]];
112 for (i = 1; i < 8; i++)
113 ac_val1[8 + i] = block[s->idsp.idct_permutation[i]];
117 * check if the next stuff is a resync marker or the end.
120 static inline int mpeg4_is_resync(Mpeg4DecContext *ctx)
122 MpegEncContext *s = &ctx->m;
123 int bits_count = get_bits_count(&s->gb);
124 int v = show_bits(&s->gb, 16);
126 if (s->workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker)
130 if (s->pict_type == AV_PICTURE_TYPE_B ||
131 (v >> (8 - s->pict_type) != 1) || s->partitioned_frame)
133 skip_bits(&s->gb, 8 + s->pict_type);
134 bits_count += 8 + s->pict_type;
135 v = show_bits(&s->gb, 16);
138 if (bits_count + 8 >= s->gb.size_in_bits) {
140 v |= 0x7F >> (7 - (bits_count & 7));
145 if (v == ff_mpeg4_resync_prefix[bits_count & 7]) {
147 int mb_num_bits = av_log2(s->mb_num - 1) + 1;
148 GetBitContext gb = s->gb;
150 skip_bits(&s->gb, 1);
151 align_get_bits(&s->gb);
153 for (len = 0; len < 32; len++)
154 if (get_bits1(&s->gb))
157 mb_num = get_bits(&s->gb, mb_num_bits);
158 if (!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
163 if (len >= ff_mpeg4_get_video_packet_prefix_length(s))
170 static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
172 MpegEncContext *s = &ctx->m;
173 int a = 2 << s->sprite_warping_accuracy;
174 int rho = 3 - s->sprite_warping_accuracy;
180 int min_ab, i, w2, h2, w3, h3;
181 int sprite_ref[4][2];
182 int virtual_ref[2][2];
183 int64_t sprite_offset[2][2];
184 int64_t sprite_delta[2][2];
186 // only true for rectangle shapes
187 const int vop_ref[4][2] = { { 0, 0 }, { s->width, 0 },
188 { 0, s->height }, { s->width, s->height } };
189 int d[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
191 if (w <= 0 || h <= 0)
192 return AVERROR_INVALIDDATA;
194 /* the decoder was not properly initialized and we cannot continue */
195 if (sprite_trajectory.table == NULL)
196 return AVERROR_INVALIDDATA;
198 for (i = 0; i < ctx->num_sprite_warping_points; i++) {
202 length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
204 x = get_xbits(gb, length);
206 if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
207 check_marker(s->avctx, gb, "before sprite_trajectory");
209 length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
211 y = get_xbits(gb, length);
213 check_marker(s->avctx, gb, "after sprite_trajectory");
214 ctx->sprite_traj[i][0] = d[i][0] = x;
215 ctx->sprite_traj[i][1] = d[i][1] = y;
218 ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0;
220 while ((1 << alpha) < w)
222 while ((1 << beta) < h)
223 beta++; /* typo in the MPEG-4 std for the definition of w' and h' */
227 // Note, the 4th point isn't used for GMC
228 if (ctx->divx_version == 500 && ctx->divx_build == 413) {
229 sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
230 sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
231 sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
232 sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1];
233 sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0];
234 sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1];
236 sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]);
237 sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]);
238 sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]);
239 sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]);
240 sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]);
241 sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]);
243 /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
244 * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
246 /* This is mostly identical to the MPEG-4 std (and is totally unreadable
247 * because of that...). Perhaps it should be reordered to be more readable.
248 * The idea behind this virtual_ref mess is to be able to use shifts later
249 * per pixel instead of divides so the distance between points is converted
250 * from w&h based to w2&h2 based which are of the 2^x form. */
251 virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) +
252 ROUNDED_DIV(((w - w2) *
253 (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
254 w2 * (r * sprite_ref[1][0] - 16LL * vop_ref[1][0])), w);
255 virtual_ref[0][1] = 16 * vop_ref[0][1] +
256 ROUNDED_DIV(((w - w2) *
257 (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
258 w2 * (r * sprite_ref[1][1] - 16LL * vop_ref[1][1])), w);
259 virtual_ref[1][0] = 16 * vop_ref[0][0] +
260 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
261 h2 * (r * sprite_ref[2][0] - 16LL * vop_ref[2][0])), h);
262 virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) +
263 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
264 h2 * (r * sprite_ref[2][1] - 16LL * vop_ref[2][1])), h);
266 switch (ctx->num_sprite_warping_points) {
268 sprite_offset[0][0] =
269 sprite_offset[0][1] =
270 sprite_offset[1][0] =
271 sprite_offset[1][1] = 0;
272 sprite_delta[0][0] = a;
274 sprite_delta[1][0] = 0;
275 sprite_delta[1][1] = a;
276 ctx->sprite_shift[0] =
277 ctx->sprite_shift[1] = 0;
280 sprite_offset[0][0] = sprite_ref[0][0] - a * vop_ref[0][0];
281 sprite_offset[0][1] = sprite_ref[0][1] - a * vop_ref[0][1];
282 sprite_offset[1][0] = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) -
283 a * (vop_ref[0][0] / 2);
284 sprite_offset[1][1] = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) -
285 a * (vop_ref[0][1] / 2);
286 sprite_delta[0][0] = a;
288 sprite_delta[1][0] = 0;
289 sprite_delta[1][1] = a;
290 ctx->sprite_shift[0] =
291 ctx->sprite_shift[1] = 0;
294 sprite_offset[0][0] = ((int64_t) sprite_ref[0][0] * (1 << alpha + rho)) +
295 ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
296 ((int64_t) -vop_ref[0][0]) +
297 ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
298 ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
299 sprite_offset[0][1] = ((int64_t) sprite_ref[0][1] * (1 << alpha + rho)) +
300 ((int64_t) -r * sprite_ref[0][1] + virtual_ref[0][1]) *
301 ((int64_t) -vop_ref[0][0]) +
302 ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
303 ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
304 sprite_offset[1][0] = (((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
305 ((int64_t)-2 * vop_ref[0][0] + 1) +
306 ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
307 ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
308 (int64_t) sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1)));
309 sprite_offset[1][1] = (((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) *
310 ((int64_t)-2 * vop_ref[0][0] + 1) +
311 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
312 ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
313 (int64_t) sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1)));
314 sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
315 sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]);
316 sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
317 sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
319 ctx->sprite_shift[0] = alpha + rho;
320 ctx->sprite_shift[1] = alpha + rho + 2;
323 min_ab = FFMIN(alpha, beta);
326 sprite_offset[0][0] = ((int64_t)sprite_ref[0][0] * (1 << (alpha + beta + rho - min_ab))) +
327 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-vop_ref[0][0]) +
328 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-vop_ref[0][1]) +
329 ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
330 sprite_offset[0][1] = ((int64_t)sprite_ref[0][1] * (1 << (alpha + beta + rho - min_ab))) +
331 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-vop_ref[0][0]) +
332 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-vop_ref[0][1]) +
333 ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
334 sprite_offset[1][0] = ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-2 * vop_ref[0][0] + 1) +
335 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-2 * vop_ref[0][1] + 1) +
336 (int64_t)2 * w2 * h3 * r * sprite_ref[0][0] - 16 * w2 * h3 +
337 ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
338 sprite_offset[1][1] = ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-2 * vop_ref[0][0] + 1) +
339 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-2 * vop_ref[0][1] + 1) +
340 (int64_t)2 * w2 * h3 * r * sprite_ref[0][1] - 16 * w2 * h3 +
341 ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
342 sprite_delta[0][0] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[0][0]) * h3;
343 sprite_delta[0][1] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[1][0]) * w3;
344 sprite_delta[1][0] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[0][1]) * h3;
345 sprite_delta[1][1] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[1][1]) * w3;
347 ctx->sprite_shift[0] = alpha + beta + rho - min_ab;
348 ctx->sprite_shift[1] = alpha + beta + rho - min_ab + 2;
351 /* try to simplify the situation */
352 if (sprite_delta[0][0] == a << ctx->sprite_shift[0] &&
353 sprite_delta[0][1] == 0 &&
354 sprite_delta[1][0] == 0 &&
355 sprite_delta[1][1] == a << ctx->sprite_shift[0]) {
356 sprite_offset[0][0] >>= ctx->sprite_shift[0];
357 sprite_offset[0][1] >>= ctx->sprite_shift[0];
358 sprite_offset[1][0] >>= ctx->sprite_shift[1];
359 sprite_offset[1][1] >>= ctx->sprite_shift[1];
360 sprite_delta[0][0] = a;
361 sprite_delta[0][1] = 0;
362 sprite_delta[1][0] = 0;
363 sprite_delta[1][1] = a;
364 ctx->sprite_shift[0] = 0;
365 ctx->sprite_shift[1] = 0;
366 s->real_sprite_warping_points = 1;
368 int shift_y = 16 - ctx->sprite_shift[0];
369 int shift_c = 16 - ctx->sprite_shift[1];
371 for (i = 0; i < 2; i++) {
372 if (shift_c < 0 || shift_y < 0 ||
373 FFABS( sprite_offset[0][i]) >= INT_MAX >> shift_y ||
374 FFABS( sprite_offset[1][i]) >= INT_MAX >> shift_c ||
375 FFABS( sprite_delta[0][i]) >= INT_MAX >> shift_y ||
376 FFABS( sprite_delta[1][i]) >= INT_MAX >> shift_y
378 avpriv_request_sample(s->avctx, "Too large sprite shift, delta or offset");
383 for (i = 0; i < 2; i++) {
384 sprite_offset[0][i] *= 1 << shift_y;
385 sprite_offset[1][i] *= 1 << shift_c;
386 sprite_delta[0][i] *= 1 << shift_y;
387 sprite_delta[1][i] *= 1 << shift_y;
388 ctx->sprite_shift[i] = 16;
391 for (i = 0; i < 2; i++) {
393 sprite_delta[i][0] - a * (1LL<<16),
394 sprite_delta[i][1] - a * (1LL<<16)
397 if (llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
398 llabs(sprite_offset[0][i] + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
399 llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL) + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
400 llabs(sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
401 llabs(sprite_delta[i][1] * (w+16LL)) >= INT_MAX ||
402 llabs(sd[0]) >= INT_MAX ||
403 llabs(sd[1]) >= INT_MAX ||
404 llabs(sprite_offset[0][i] + sd[0] * (w+16LL)) >= INT_MAX ||
405 llabs(sprite_offset[0][i] + sd[1] * (h+16LL)) >= INT_MAX ||
406 llabs(sprite_offset[0][i] + sd[0] * (w+16LL) + sd[1] * (h+16LL)) >= INT_MAX
408 avpriv_request_sample(s->avctx, "Overflow on sprite points");
412 s->real_sprite_warping_points = ctx->num_sprite_warping_points;
415 for (i = 0; i < 4; i++) {
416 s->sprite_offset[i&1][i>>1] = sprite_offset[i&1][i>>1];
417 s->sprite_delta [i&1][i>>1] = sprite_delta [i&1][i>>1];
422 memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
423 memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
424 return AVERROR_PATCHWELCOME;
427 static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb) {
428 MpegEncContext *s = &ctx->m;
429 int len = FFMIN(ctx->time_increment_bits + 3, 15);
434 check_marker(s->avctx, gb, "after new_pred");
440 * Decode the next video packet.
441 * @return <0 if something went wrong
443 int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
445 MpegEncContext *s = &ctx->m;
447 int mb_num_bits = av_log2(s->mb_num - 1) + 1;
448 int header_extension = 0, mb_num, len;
450 /* is there enough space left for a video packet + header */
451 if (get_bits_count(&s->gb) > s->gb.size_in_bits - 20)
452 return AVERROR_INVALIDDATA;
454 for (len = 0; len < 32; len++)
455 if (get_bits1(&s->gb))
458 if (len != ff_mpeg4_get_video_packet_prefix_length(s)) {
459 av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
460 return AVERROR_INVALIDDATA;
463 if (ctx->shape != RECT_SHAPE) {
464 header_extension = get_bits1(&s->gb);
465 // FIXME more stuff here
468 mb_num = get_bits(&s->gb, mb_num_bits);
469 if (mb_num >= s->mb_num || !mb_num) {
470 av_log(s->avctx, AV_LOG_ERROR,
471 "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
472 return AVERROR_INVALIDDATA;
475 s->mb_x = mb_num % s->mb_width;
476 s->mb_y = mb_num / s->mb_width;
478 if (ctx->shape != BIN_ONLY_SHAPE) {
479 int qscale = get_bits(&s->gb, s->quant_precision);
481 s->chroma_qscale = s->qscale = qscale;
484 if (ctx->shape == RECT_SHAPE)
485 header_extension = get_bits1(&s->gb);
487 if (header_extension) {
490 while (get_bits1(&s->gb) != 0)
493 check_marker(s->avctx, &s->gb, "before time_increment in video packed header");
494 skip_bits(&s->gb, ctx->time_increment_bits); /* time_increment */
495 check_marker(s->avctx, &s->gb, "before vop_coding_type in video packed header");
497 skip_bits(&s->gb, 2); /* vop coding type */
498 // FIXME not rect stuff here
500 if (ctx->shape != BIN_ONLY_SHAPE) {
501 skip_bits(&s->gb, 3); /* intra dc vlc threshold */
502 // FIXME don't just ignore everything
503 if (s->pict_type == AV_PICTURE_TYPE_S &&
504 ctx->vol_sprite_usage == GMC_SPRITE) {
505 if (mpeg4_decode_sprite_trajectory(ctx, &s->gb) < 0)
506 return AVERROR_INVALIDDATA;
507 av_log(s->avctx, AV_LOG_ERROR, "untested\n");
510 // FIXME reduced res stuff here
512 if (s->pict_type != AV_PICTURE_TYPE_I) {
513 int f_code = get_bits(&s->gb, 3); /* fcode_for */
515 av_log(s->avctx, AV_LOG_ERROR,
516 "Error, video packet header damaged (f_code=0)\n");
518 if (s->pict_type == AV_PICTURE_TYPE_B) {
519 int b_code = get_bits(&s->gb, 3);
521 av_log(s->avctx, AV_LOG_ERROR,
522 "Error, video packet header damaged (b_code=0)\n");
527 decode_new_pred(ctx, &s->gb);
532 static void reset_studio_dc_predictors(MpegEncContext *s)
534 /* Reset DC Predictors */
537 s->last_dc[2] = 1 << (s->avctx->bits_per_raw_sample + s->dct_precision + s->intra_dc_precision - 1);
541 * Decode the next video packet.
542 * @return <0 if something went wrong
544 int ff_mpeg4_decode_studio_slice_header(Mpeg4DecContext *ctx)
546 MpegEncContext *s = &ctx->m;
547 GetBitContext *gb = &s->gb;
551 if (get_bits_left(gb) >= 32 && get_bits_long(gb, 32) == SLICE_START_CODE) {
552 vlc_len = av_log2(s->mb_width * s->mb_height) + 1;
553 mb_num = get_bits(gb, vlc_len);
555 if (mb_num >= s->mb_num)
556 return AVERROR_INVALIDDATA;
558 s->mb_x = mb_num % s->mb_width;
559 s->mb_y = mb_num / s->mb_width;
561 if (ctx->shape != BIN_ONLY_SHAPE)
562 s->qscale = mpeg_get_qscale(s);
564 if (get_bits1(gb)) { /* slice_extension_flag */
565 skip_bits1(gb); /* intra_slice */
566 skip_bits1(gb); /* slice_VOP_id_enable */
567 skip_bits(gb, 6); /* slice_VOP_id */
568 while (get_bits1(gb)) /* extra_bit_slice */
569 skip_bits(gb, 8); /* extra_information_slice */
572 reset_studio_dc_predictors(s);
575 return AVERROR_INVALIDDATA;
582 * Get the average motion vector for a GMC MB.
583 * @param n either 0 for the x component or 1 for y
584 * @return the average MV for a GMC MB
586 static inline int get_amv(Mpeg4DecContext *ctx, int n)
588 MpegEncContext *s = &ctx->m;
589 int x, y, mb_v, sum, dx, dy, shift;
590 int len = 1 << (s->f_code + 4);
591 const int a = s->sprite_warping_accuracy;
593 if (s->workaround_bugs & FF_BUG_AMV)
594 len >>= s->quarter_sample;
596 if (s->real_sprite_warping_points == 1) {
597 if (ctx->divx_version == 500 && ctx->divx_build == 413)
598 sum = s->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
600 sum = RSHIFT(s->sprite_offset[0][n] * (1 << s->quarter_sample), a);
602 dx = s->sprite_delta[n][0];
603 dy = s->sprite_delta[n][1];
604 shift = ctx->sprite_shift[0];
606 dy -= 1 << (shift + a + 1);
608 dx -= 1 << (shift + a + 1);
609 mb_v = s->sprite_offset[0][n] + dx * s->mb_x * 16 + dy * s->mb_y * 16;
612 for (y = 0; y < 16; y++) {
617 for (x = 0; x < 16; x++) {
622 sum = RSHIFT(sum, a + 8 - s->quarter_sample);
634 * Decode the dc value.
635 * @param n block index (0-3 are luma, 4-5 are chroma)
636 * @param dir_ptr the prediction direction will be stored here
637 * @return the quantized dc
639 static inline int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
644 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
646 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
648 if (code < 0 || code > 9 /* && s->nbit < 9 */) {
649 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
650 return AVERROR_INVALIDDATA;
658 level = 2 * get_bits1(&s->gb) - 1;
660 if (get_bits1(&s->gb))
661 level = get_bits(&s->gb, code - 1) + (1 << (code - 1));
663 level = -get_bits(&s->gb, code - 1) - (1 << (code - 1));
666 level = get_xbits(&s->gb, code);
670 if (get_bits1(&s->gb) == 0) { /* marker */
671 if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)) {
672 av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
673 return AVERROR_INVALIDDATA;
679 return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
683 * Decode first partition.
684 * @return number of MBs decoded or <0 if an error occurred
686 static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
688 MpegEncContext *s = &ctx->m;
690 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
692 /* decode first partition */
693 s->first_slice_line = 1;
694 for (; s->mb_y < s->mb_height; s->mb_y++) {
695 ff_init_block_index(s);
696 for (; s->mb_x < s->mb_width; s->mb_x++) {
697 const int xy = s->mb_x + s->mb_y * s->mb_stride;
702 ff_update_block_index(s);
703 if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
704 s->first_slice_line = 0;
706 if (s->pict_type == AV_PICTURE_TYPE_I) {
710 if (show_bits_long(&s->gb, 19) == DC_MARKER)
713 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
715 av_log(s->avctx, AV_LOG_ERROR,
716 "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
717 return AVERROR_INVALIDDATA;
721 s->cbp_table[xy] = cbpc & 3;
722 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
726 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
728 s->current_picture.qscale_table[xy] = s->qscale;
730 s->mbintra_table[xy] = 1;
731 for (i = 0; i < 6; i++) {
733 int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
735 av_log(s->avctx, AV_LOG_ERROR,
736 "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
743 s->pred_dir_table[xy] = dir;
744 } else { /* P/S_TYPE */
745 int mx, my, pred_x, pred_y, bits;
746 int16_t *const mot_val = s->current_picture.motion_val[0][s->block_index[0]];
747 const int stride = s->b8_stride * 2;
750 bits = show_bits(&s->gb, 17);
751 if (bits == MOTION_MARKER)
755 if (bits & 0x10000) {
757 if (s->pict_type == AV_PICTURE_TYPE_S &&
758 ctx->vol_sprite_usage == GMC_SPRITE) {
759 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
763 mx = get_amv(ctx, 0);
764 my = get_amv(ctx, 1);
766 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
773 mot_val[0 + stride] =
774 mot_val[2 + stride] = mx;
777 mot_val[1 + stride] =
778 mot_val[3 + stride] = my;
780 if (s->mbintra_table[xy])
781 ff_clean_intra_table_entries(s);
785 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
787 av_log(s->avctx, AV_LOG_ERROR,
788 "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
789 return AVERROR_INVALIDDATA;
794 s->cbp_table[xy] = cbpc & (8 + 3); // 8 is dquant
796 s->mb_intra = ((cbpc & 4) != 0);
799 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
800 s->mbintra_table[xy] = 1;
803 mot_val[0 + stride] =
804 mot_val[2 + stride] = 0;
807 mot_val[1 + stride] =
808 mot_val[3 + stride] = 0;
810 if (s->mbintra_table[xy])
811 ff_clean_intra_table_entries(s);
813 if (s->pict_type == AV_PICTURE_TYPE_S &&
814 ctx->vol_sprite_usage == GMC_SPRITE &&
816 s->mcsel = get_bits1(&s->gb);
820 if ((cbpc & 16) == 0) {
821 /* 16x16 motion prediction */
823 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
825 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
827 return AVERROR_INVALIDDATA;
829 my = ff_h263_decode_motion(s, pred_y, s->f_code);
831 return AVERROR_INVALIDDATA;
832 s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
835 mx = get_amv(ctx, 0);
836 my = get_amv(ctx, 1);
837 s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
844 mot_val[0 + stride] =
845 mot_val[2 + stride] = mx;
848 mot_val[1 + stride] =
849 mot_val[3 + stride] = my;
852 s->current_picture.mb_type[xy] = MB_TYPE_8x8 |
854 for (i = 0; i < 4; i++) {
855 int16_t *mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
856 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
858 return AVERROR_INVALIDDATA;
860 my = ff_h263_decode_motion(s, pred_y, s->f_code);
862 return AVERROR_INVALIDDATA;
877 * decode second partition.
878 * @return <0 if an error occurred
880 static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
883 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
885 s->mb_x = s->resync_mb_x;
886 s->first_slice_line = 1;
887 for (s->mb_y = s->resync_mb_y; mb_num < mb_count; s->mb_y++) {
888 ff_init_block_index(s);
889 for (; mb_num < mb_count && s->mb_x < s->mb_width; s->mb_x++) {
890 const int xy = s->mb_x + s->mb_y * s->mb_stride;
893 ff_update_block_index(s);
894 if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
895 s->first_slice_line = 0;
897 if (s->pict_type == AV_PICTURE_TYPE_I) {
898 int ac_pred = get_bits1(&s->gb);
899 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
901 av_log(s->avctx, AV_LOG_ERROR,
902 "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
903 return AVERROR_INVALIDDATA;
906 s->cbp_table[xy] |= cbpy << 2;
907 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
908 } else { /* P || S_TYPE */
909 if (IS_INTRA(s->current_picture.mb_type[xy])) {
912 int ac_pred = get_bits1(&s->gb);
913 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
916 av_log(s->avctx, AV_LOG_ERROR,
917 "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
918 return AVERROR_INVALIDDATA;
921 if (s->cbp_table[xy] & 8)
922 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
923 s->current_picture.qscale_table[xy] = s->qscale;
925 for (i = 0; i < 6; i++) {
927 int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
929 av_log(s->avctx, AV_LOG_ERROR,
930 "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
937 s->cbp_table[xy] &= 3; // remove dquant
938 s->cbp_table[xy] |= cbpy << 2;
939 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
940 s->pred_dir_table[xy] = dir;
941 } else if (IS_SKIP(s->current_picture.mb_type[xy])) {
942 s->current_picture.qscale_table[xy] = s->qscale;
943 s->cbp_table[xy] = 0;
945 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
948 av_log(s->avctx, AV_LOG_ERROR,
949 "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
950 return AVERROR_INVALIDDATA;
953 if (s->cbp_table[xy] & 8)
954 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
955 s->current_picture.qscale_table[xy] = s->qscale;
957 s->cbp_table[xy] &= 3; // remove dquant
958 s->cbp_table[xy] |= (cbpy ^ 0xf) << 2;
962 if (mb_num >= mb_count)
970 * Decode the first and second partition.
971 * @return <0 if error (and sets error type in the error_status_table)
973 int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx)
975 MpegEncContext *s = &ctx->m;
978 const int part_a_error = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR;
979 const int part_a_end = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END | ER_MV_END) : ER_MV_END;
981 mb_num = mpeg4_decode_partition_a(ctx);
983 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
984 s->mb_x, s->mb_y, part_a_error);
985 return mb_num ? mb_num : AVERROR_INVALIDDATA;
988 if (s->resync_mb_x + s->resync_mb_y * s->mb_width + mb_num > s->mb_num) {
989 av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
990 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
991 s->mb_x, s->mb_y, part_a_error);
992 return AVERROR_INVALIDDATA;
995 s->mb_num_left = mb_num;
997 if (s->pict_type == AV_PICTURE_TYPE_I) {
998 while (show_bits(&s->gb, 9) == 1)
999 skip_bits(&s->gb, 9);
1000 if (get_bits_long(&s->gb, 19) != DC_MARKER) {
1001 av_log(s->avctx, AV_LOG_ERROR,
1002 "marker missing after first I partition at %d %d\n",
1004 return AVERROR_INVALIDDATA;
1007 while (show_bits(&s->gb, 10) == 1)
1008 skip_bits(&s->gb, 10);
1009 if (get_bits(&s->gb, 17) != MOTION_MARKER) {
1010 av_log(s->avctx, AV_LOG_ERROR,
1011 "marker missing after first P partition at %d %d\n",
1013 return AVERROR_INVALIDDATA;
1016 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1017 s->mb_x - 1, s->mb_y, part_a_end);
1019 ret = mpeg4_decode_partition_b(s, mb_num);
1021 if (s->pict_type == AV_PICTURE_TYPE_P)
1022 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1023 s->mb_x, s->mb_y, ER_DC_ERROR);
1026 if (s->pict_type == AV_PICTURE_TYPE_P)
1027 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1028 s->mb_x - 1, s->mb_y, ER_DC_END);
1036 * @return <0 if an error occurred
1038 static inline int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block,
1039 int n, int coded, int intra, int rvlc)
1041 MpegEncContext *s = &ctx->m;
1042 int level, i, last, run, qmul, qadd;
1043 int av_uninit(dc_pred_dir);
1045 RL_VLC_ELEM *rl_vlc;
1046 const uint8_t *scan_table;
1048 // Note intra & rvlc should be optimized away if this is inlined
1051 if (ctx->use_intra_dc_vlc) {
1053 if (s->partitioned_frame) {
1054 level = s->dc_val[0][s->block_index[n]];
1056 level = FASTDIV((level + (s->y_dc_scale >> 1)), s->y_dc_scale);
1058 level = FASTDIV((level + (s->c_dc_scale >> 1)), s->c_dc_scale);
1059 dc_pred_dir = (s->pred_dir_table[s->mb_x + s->mb_y * s->mb_stride] << n) & 32;
1061 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
1069 ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
1075 rl = &ff_rvlc_rl_intra;
1076 rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
1078 rl = &ff_mpeg4_rl_intra;
1079 rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
1082 if (dc_pred_dir == 0)
1083 scan_table = s->intra_v_scantable.permutated; /* left */
1085 scan_table = s->intra_h_scantable.permutated; /* top */
1087 scan_table = s->intra_scantable.permutated;
1094 s->block_last_index[n] = i;
1098 rl = &ff_rvlc_rl_inter;
1100 rl = &ff_h263_rl_inter;
1102 scan_table = s->intra_scantable.permutated;
1104 if (s->mpeg_quant) {
1108 rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
1110 rl_vlc = ff_h263_rl_inter.rl_vlc[0];
1112 qmul = s->qscale << 1;
1113 qadd = (s->qscale - 1) | 1;
1115 rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
1117 rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
1121 OPEN_READER(re, &s->gb);
1123 UPDATE_CACHE(re, &s->gb);
1124 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1128 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1129 av_log(s->avctx, AV_LOG_ERROR,
1130 "1. marker bit missing in rvlc esc\n");
1131 return AVERROR_INVALIDDATA;
1133 SKIP_CACHE(re, &s->gb, 1);
1135 last = SHOW_UBITS(re, &s->gb, 1);
1136 SKIP_CACHE(re, &s->gb, 1);
1137 run = SHOW_UBITS(re, &s->gb, 6);
1138 SKIP_COUNTER(re, &s->gb, 1 + 1 + 6);
1139 UPDATE_CACHE(re, &s->gb);
1141 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1142 av_log(s->avctx, AV_LOG_ERROR,
1143 "2. marker bit missing in rvlc esc\n");
1144 return AVERROR_INVALIDDATA;
1146 SKIP_CACHE(re, &s->gb, 1);
1148 level = SHOW_UBITS(re, &s->gb, 11);
1149 SKIP_CACHE(re, &s->gb, 11);
1151 if (SHOW_UBITS(re, &s->gb, 5) != 0x10) {
1152 av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
1153 return AVERROR_INVALIDDATA;
1155 SKIP_CACHE(re, &s->gb, 5);
1157 level = level * qmul + qadd;
1158 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1159 SKIP_COUNTER(re, &s->gb, 1 + 11 + 5 + 1);
1166 cache = GET_CACHE(re, &s->gb);
1169 cache ^= 0xC0000000;
1171 if (cache & 0x80000000) {
1172 if (cache & 0x40000000) {
1174 SKIP_CACHE(re, &s->gb, 2);
1175 last = SHOW_UBITS(re, &s->gb, 1);
1176 SKIP_CACHE(re, &s->gb, 1);
1177 run = SHOW_UBITS(re, &s->gb, 6);
1178 SKIP_COUNTER(re, &s->gb, 2 + 1 + 6);
1179 UPDATE_CACHE(re, &s->gb);
1182 level = SHOW_SBITS(re, &s->gb, 12);
1183 LAST_SKIP_BITS(re, &s->gb, 12);
1185 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1186 av_log(s->avctx, AV_LOG_ERROR,
1187 "1. marker bit missing in 3. esc\n");
1188 if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
1189 return AVERROR_INVALIDDATA;
1191 SKIP_CACHE(re, &s->gb, 1);
1193 level = SHOW_SBITS(re, &s->gb, 12);
1194 SKIP_CACHE(re, &s->gb, 12);
1196 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1197 av_log(s->avctx, AV_LOG_ERROR,
1198 "2. marker bit missing in 3. esc\n");
1199 if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
1200 return AVERROR_INVALIDDATA;
1203 SKIP_COUNTER(re, &s->gb, 1 + 12 + 1);
1207 if (s->error_recognition >= FF_ER_COMPLIANT) {
1208 const int abs_level= FFABS(level);
1209 if (abs_level<=MAX_LEVEL && run<=MAX_RUN) {
1210 const int run1= run - rl->max_run[last][abs_level] - 1;
1211 if (abs_level <= rl->max_level[last][run]) {
1212 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1213 return AVERROR_INVALIDDATA;
1215 if (s->error_recognition > FF_ER_COMPLIANT) {
1216 if (abs_level <= rl->max_level[last][run]*2) {
1217 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1218 return AVERROR_INVALIDDATA;
1220 if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) {
1221 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1222 return AVERROR_INVALIDDATA;
1229 level = level * qmul + qadd;
1231 level = level * qmul - qadd;
1233 if ((unsigned)(level + 2048) > 4095) {
1234 if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)) {
1235 if (level > 2560 || level < -2560) {
1236 av_log(s->avctx, AV_LOG_ERROR,
1237 "|level| overflow in 3. esc, qp=%d\n",
1239 return AVERROR_INVALIDDATA;
1242 level = level < 0 ? -2048 : 2047;
1250 SKIP_BITS(re, &s->gb, 2);
1251 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1252 i += run + rl->max_run[run >> 7][level / qmul] + 1; // FIXME opt indexing
1253 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1254 LAST_SKIP_BITS(re, &s->gb, 1);
1258 SKIP_BITS(re, &s->gb, 1);
1259 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1261 level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; // FIXME opt indexing
1262 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1263 LAST_SKIP_BITS(re, &s->gb, 1);
1268 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1269 LAST_SKIP_BITS(re, &s->gb, 1);
1271 ff_tlog(s->avctx, "dct[%d][%d] = %- 4d end?:%d\n", scan_table[i&63]&7, scan_table[i&63] >> 3, level, i>62);
1275 av_log(s->avctx, AV_LOG_ERROR,
1276 "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1277 return AVERROR_INVALIDDATA;
1280 block[scan_table[i]] = level;
1284 block[scan_table[i]] = level;
1286 CLOSE_READER(re, &s->gb);
1291 if (!ctx->use_intra_dc_vlc) {
1292 block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
1294 i -= i >> 31; // if (i == -1) i = 0;
1297 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1299 i = 63; // FIXME not optimal
1301 s->block_last_index[n] = i;
1306 * decode partition C of one MB.
1307 * @return <0 if an error occurred
1309 static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
1311 Mpeg4DecContext *ctx = s->avctx->priv_data;
1313 const int xy = s->mb_x + s->mb_y * s->mb_stride;
1315 av_assert2(s == (void*)ctx);
1317 mb_type = s->current_picture.mb_type[xy];
1318 cbp = s->cbp_table[xy];
1320 ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1322 if (s->current_picture.qscale_table[xy] != s->qscale)
1323 ff_set_qscale(s, s->current_picture.qscale_table[xy]);
1325 if (s->pict_type == AV_PICTURE_TYPE_P ||
1326 s->pict_type == AV_PICTURE_TYPE_S) {
1328 for (i = 0; i < 4; i++) {
1329 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
1330 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
1332 s->mb_intra = IS_INTRA(mb_type);
1334 if (IS_SKIP(mb_type)) {
1336 for (i = 0; i < 6; i++)
1337 s->block_last_index[i] = -1;
1338 s->mv_dir = MV_DIR_FORWARD;
1339 s->mv_type = MV_TYPE_16X16;
1340 if (s->pict_type == AV_PICTURE_TYPE_S
1341 && ctx->vol_sprite_usage == GMC_SPRITE) {
1348 } else if (s->mb_intra) {
1349 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1350 } else if (!s->mb_intra) {
1351 // s->mcsel = 0; // FIXME do we need to init that?
1353 s->mv_dir = MV_DIR_FORWARD;
1354 if (IS_8X8(mb_type)) {
1355 s->mv_type = MV_TYPE_8X8;
1357 s->mv_type = MV_TYPE_16X16;
1360 } else { /* I-Frame */
1362 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1365 if (!IS_SKIP(mb_type)) {
1367 s->bdsp.clear_blocks(s->block[0]);
1368 /* decode each block */
1369 for (i = 0; i < 6; i++) {
1370 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, s->mb_intra, ctx->rvlc) < 0) {
1371 av_log(s->avctx, AV_LOG_ERROR,
1372 "texture corrupted at %d %d %d\n",
1373 s->mb_x, s->mb_y, s->mb_intra);
1374 return AVERROR_INVALIDDATA;
1380 /* per-MB end of slice check */
1381 if (--s->mb_num_left <= 0) {
1382 if (mpeg4_is_resync(ctx))
1387 if (mpeg4_is_resync(ctx)) {
1388 const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
1389 if (s->cbp_table[xy + delta])
1396 static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
1398 Mpeg4DecContext *ctx = s->avctx->priv_data;
1399 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1401 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1402 const int xy = s->mb_x + s->mb_y * s->mb_stride;
1404 av_assert2(s == (void*)ctx);
1405 av_assert2(s->h263_pred);
1407 if (s->pict_type == AV_PICTURE_TYPE_P ||
1408 s->pict_type == AV_PICTURE_TYPE_S) {
1410 if (get_bits1(&s->gb)) {
1413 for (i = 0; i < 6; i++)
1414 s->block_last_index[i] = -1;
1415 s->mv_dir = MV_DIR_FORWARD;
1416 s->mv_type = MV_TYPE_16X16;
1417 if (s->pict_type == AV_PICTURE_TYPE_S &&
1418 ctx->vol_sprite_usage == GMC_SPRITE) {
1419 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1424 s->mv[0][0][0] = get_amv(ctx, 0);
1425 s->mv[0][0][1] = get_amv(ctx, 1);
1428 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1438 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
1440 av_log(s->avctx, AV_LOG_ERROR,
1441 "mcbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1442 return AVERROR_INVALIDDATA;
1444 } while (cbpc == 20);
1446 s->bdsp.clear_blocks(s->block[0]);
1448 s->mb_intra = ((cbpc & 4) != 0);
1452 if (s->pict_type == AV_PICTURE_TYPE_S &&
1453 ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
1454 s->mcsel = get_bits1(&s->gb);
1457 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
1459 av_log(s->avctx, AV_LOG_ERROR,
1460 "P cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1461 return AVERROR_INVALIDDATA;
1464 cbp = (cbpc & 3) | (cbpy << 2);
1466 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1467 if ((!s->progressive_sequence) &&
1468 (cbp || (s->workaround_bugs & FF_BUG_XVID_ILACE)))
1469 s->interlaced_dct = get_bits1(&s->gb);
1471 s->mv_dir = MV_DIR_FORWARD;
1472 if ((cbpc & 16) == 0) {
1474 s->current_picture.mb_type[xy] = MB_TYPE_GMC |
1477 /* 16x16 global motion prediction */
1478 s->mv_type = MV_TYPE_16X16;
1479 mx = get_amv(ctx, 0);
1480 my = get_amv(ctx, 1);
1481 s->mv[0][0][0] = mx;
1482 s->mv[0][0][1] = my;
1483 } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
1484 s->current_picture.mb_type[xy] = MB_TYPE_16x8 |
1487 /* 16x8 field motion prediction */
1488 s->mv_type = MV_TYPE_FIELD;
1490 s->field_select[0][0] = get_bits1(&s->gb);
1491 s->field_select[0][1] = get_bits1(&s->gb);
1493 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1495 for (i = 0; i < 2; i++) {
1496 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1498 return AVERROR_INVALIDDATA;
1500 my = ff_h263_decode_motion(s, pred_y / 2, s->f_code);
1502 return AVERROR_INVALIDDATA;
1504 s->mv[0][i][0] = mx;
1505 s->mv[0][i][1] = my;
1508 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1509 /* 16x16 motion prediction */
1510 s->mv_type = MV_TYPE_16X16;
1511 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1512 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1515 return AVERROR_INVALIDDATA;
1517 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1520 return AVERROR_INVALIDDATA;
1521 s->mv[0][0][0] = mx;
1522 s->mv[0][0][1] = my;
1525 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
1526 s->mv_type = MV_TYPE_8X8;
1527 for (i = 0; i < 4; i++) {
1528 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1529 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1531 return AVERROR_INVALIDDATA;
1533 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1535 return AVERROR_INVALIDDATA;
1536 s->mv[0][i][0] = mx;
1537 s->mv[0][i][1] = my;
1542 } else if (s->pict_type == AV_PICTURE_TYPE_B) {
1543 int modb1; // first bit of modb
1544 int modb2; // second bit of modb
1547 s->mb_intra = 0; // B-frames never contain intra blocks
1548 s->mcsel = 0; // ... true gmc blocks
1551 for (i = 0; i < 2; i++) {
1552 s->last_mv[i][0][0] =
1553 s->last_mv[i][0][1] =
1554 s->last_mv[i][1][0] =
1555 s->last_mv[i][1][1] = 0;
1558 ff_thread_await_progress(&s->next_picture_ptr->tf, s->mb_y, 0);
1561 /* if we skipped it in the future P-frame than skip it now too */
1562 s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
1564 if (s->mb_skipped) {
1566 for (i = 0; i < 6; i++)
1567 s->block_last_index[i] = -1;
1569 s->mv_dir = MV_DIR_FORWARD;
1570 s->mv_type = MV_TYPE_16X16;
1575 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1581 modb1 = get_bits1(&s->gb);
1583 // like MB_TYPE_B_DIRECT but no vectors coded
1584 mb_type = MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1;
1587 modb2 = get_bits1(&s->gb);
1588 mb_type = get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
1590 av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
1591 return AVERROR_INVALIDDATA;
1593 mb_type = mb_type_b_map[mb_type];
1597 s->bdsp.clear_blocks(s->block[0]);
1598 cbp = get_bits(&s->gb, 6);
1601 if ((!IS_DIRECT(mb_type)) && cbp) {
1602 if (get_bits1(&s->gb))
1603 ff_set_qscale(s, s->qscale + get_bits1(&s->gb) * 4 - 2);
1606 if (!s->progressive_sequence) {
1608 s->interlaced_dct = get_bits1(&s->gb);
1610 if (!IS_DIRECT(mb_type) && get_bits1(&s->gb)) {
1611 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1612 mb_type &= ~MB_TYPE_16x16;
1614 if (USES_LIST(mb_type, 0)) {
1615 s->field_select[0][0] = get_bits1(&s->gb);
1616 s->field_select[0][1] = get_bits1(&s->gb);
1618 if (USES_LIST(mb_type, 1)) {
1619 s->field_select[1][0] = get_bits1(&s->gb);
1620 s->field_select[1][1] = get_bits1(&s->gb);
1626 if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
1627 s->mv_type = MV_TYPE_16X16;
1629 if (USES_LIST(mb_type, 0)) {
1630 s->mv_dir = MV_DIR_FORWARD;
1632 mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1633 my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1634 s->last_mv[0][1][0] =
1635 s->last_mv[0][0][0] =
1636 s->mv[0][0][0] = mx;
1637 s->last_mv[0][1][1] =
1638 s->last_mv[0][0][1] =
1639 s->mv[0][0][1] = my;
1642 if (USES_LIST(mb_type, 1)) {
1643 s->mv_dir |= MV_DIR_BACKWARD;
1645 mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1646 my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1647 s->last_mv[1][1][0] =
1648 s->last_mv[1][0][0] =
1649 s->mv[1][0][0] = mx;
1650 s->last_mv[1][1][1] =
1651 s->last_mv[1][0][1] =
1652 s->mv[1][0][1] = my;
1654 } else if (!IS_DIRECT(mb_type)) {
1655 s->mv_type = MV_TYPE_FIELD;
1657 if (USES_LIST(mb_type, 0)) {
1658 s->mv_dir = MV_DIR_FORWARD;
1660 for (i = 0; i < 2; i++) {
1661 mx = ff_h263_decode_motion(s, s->last_mv[0][i][0], s->f_code);
1662 my = ff_h263_decode_motion(s, s->last_mv[0][i][1] / 2, s->f_code);
1663 s->last_mv[0][i][0] =
1664 s->mv[0][i][0] = mx;
1665 s->last_mv[0][i][1] = (s->mv[0][i][1] = my) * 2;
1669 if (USES_LIST(mb_type, 1)) {
1670 s->mv_dir |= MV_DIR_BACKWARD;
1672 for (i = 0; i < 2; i++) {
1673 mx = ff_h263_decode_motion(s, s->last_mv[1][i][0], s->b_code);
1674 my = ff_h263_decode_motion(s, s->last_mv[1][i][1] / 2, s->b_code);
1675 s->last_mv[1][i][0] =
1676 s->mv[1][i][0] = mx;
1677 s->last_mv[1][i][1] = (s->mv[1][i][1] = my) * 2;
1683 if (IS_DIRECT(mb_type)) {
1684 if (IS_SKIP(mb_type)) {
1688 mx = ff_h263_decode_motion(s, 0, 1);
1689 my = ff_h263_decode_motion(s, 0, 1);
1692 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1693 mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
1695 s->current_picture.mb_type[xy] = mb_type;
1696 } else { /* I-Frame */
1698 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
1700 av_log(s->avctx, AV_LOG_ERROR,
1701 "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1702 return AVERROR_INVALIDDATA;
1704 } while (cbpc == 8);
1710 s->ac_pred = get_bits1(&s->gb);
1712 s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
1714 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
1716 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1718 av_log(s->avctx, AV_LOG_ERROR,
1719 "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1720 return AVERROR_INVALIDDATA;
1722 cbp = (cbpc & 3) | (cbpy << 2);
1724 ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1727 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1729 if (!s->progressive_sequence)
1730 s->interlaced_dct = get_bits1(&s->gb);
1732 s->bdsp.clear_blocks(s->block[0]);
1733 /* decode each block */
1734 for (i = 0; i < 6; i++) {
1735 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 1, 0) < 0)
1736 return AVERROR_INVALIDDATA;
1742 /* decode each block */
1743 for (i = 0; i < 6; i++) {
1744 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 0, 0) < 0)
1745 return AVERROR_INVALIDDATA;
1750 /* per-MB end of slice check */
1751 if (s->codec_id == AV_CODEC_ID_MPEG4) {
1752 int next = mpeg4_is_resync(ctx);
1754 if (s->mb_x + s->mb_y*s->mb_width + 1 > next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
1755 return AVERROR_INVALIDDATA;
1756 } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
1759 if (s->pict_type == AV_PICTURE_TYPE_B) {
1760 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
1761 ff_thread_await_progress(&s->next_picture_ptr->tf,
1762 (s->mb_x + delta >= s->mb_width)
1763 ? FFMIN(s->mb_y + 1, s->mb_height - 1)
1765 if (s->next_picture.mbskip_table[xy + delta])
1776 /* As per spec, studio start code search isn't the same as the old type of start code */
1777 static void next_start_code_studio(GetBitContext *gb)
1781 while (get_bits_left(gb) >= 24 && show_bits_long(gb, 24) != 0x1) {
1786 /* additional_code, vlc index */
1787 static const uint8_t ac_state_tab[22][2] =
1813 static int mpeg4_decode_studio_block(MpegEncContext *s, int32_t block[64], int n)
1815 Mpeg4DecContext *ctx = s->avctx->priv_data;
1817 int cc, dct_dc_size, dct_diff, code, j, idx = 1, group = 0, run = 0,
1818 additional_code_len, sign, mismatch;
1819 VLC *cur_vlc = &ctx->studio_intra_tab[0];
1820 uint8_t *const scantable = s->intra_scantable.permutated;
1821 const uint16_t *quant_matrix;
1823 const int min = -1 * (1 << (s->avctx->bits_per_raw_sample + 6));
1824 const int max = ((1 << (s->avctx->bits_per_raw_sample + 6)) - 1);
1828 memset(block, 0, 64 * sizeof(int32_t));
1832 dct_dc_size = get_vlc2(&s->gb, ctx->studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
1833 quant_matrix = s->intra_matrix;
1837 dct_dc_size = get_vlc2(&s->gb, ctx->studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
1839 dct_dc_size = get_vlc2(&s->gb, ctx->studio_chroma_dc.table, STUDIO_INTRA_BITS, 2);
1840 quant_matrix = s->chroma_intra_matrix;
1843 if (dct_dc_size < 0) {
1844 av_log(s->avctx, AV_LOG_ERROR, "illegal dct_dc_size vlc\n");
1845 return AVERROR_INVALIDDATA;
1846 } else if (dct_dc_size == 0) {
1849 dct_diff = get_xbits(&s->gb, dct_dc_size);
1851 if (dct_dc_size > 8) {
1852 if(!check_marker(s->avctx, &s->gb, "dct_dc_size > 8"))
1853 return AVERROR_INVALIDDATA;
1858 s->last_dc[cc] += dct_diff;
1861 block[0] = s->last_dc[cc] * (8 >> s->intra_dc_precision);
1863 block[0] = s->last_dc[cc] * (8 >> s->intra_dc_precision) * (8 >> s->dct_precision);
1864 /* TODO: support mpeg_quant for AC coefficients */
1866 block[0] = av_clip(block[0], min, max);
1867 mismatch ^= block[0];
1869 /* AC Coefficients */
1871 group = get_vlc2(&s->gb, cur_vlc->table, STUDIO_INTRA_BITS, 2);
1874 av_log(s->avctx, AV_LOG_ERROR, "illegal ac coefficient group vlc\n");
1875 return AVERROR_INVALIDDATA;
1878 additional_code_len = ac_state_tab[group][0];
1879 cur_vlc = &ctx->studio_intra_tab[ac_state_tab[group][1]];
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);
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);
1896 run = (1 << (additional_code_len - 1)) + code;
1898 j = scantable[idx++];
1899 block[j] = sign ? 1 : -1;
1900 } else if (group >= 13 && group <= 20) {
1901 /* Level value (Table B.49) */
1902 j = scantable[idx++];
1903 block[j] = get_xbits(&s->gb, additional_code_len);
1904 } else if (group == 21) {
1906 j = scantable[idx++];
1907 additional_code_len = s->avctx->bits_per_raw_sample + s->dct_precision + 4;
1908 flc = get_bits(&s->gb, additional_code_len);
1909 if (flc >> (additional_code_len-1))
1910 block[j] = -1 * (( flc ^ ((1 << additional_code_len) -1)) + 1);
1914 block[j] = ((8 * 2 * block[j] * quant_matrix[j] * s->qscale) >> s->dct_precision) / 32;
1915 block[j] = av_clip(block[j], min, max);
1916 mismatch ^= block[j];
1919 block[63] ^= mismatch & 1;
1924 static int mpeg4_decode_studio_mb(MpegEncContext *s, int16_t block_[12][64])
1928 /* StudioMacroblock */
1931 if (get_bits1(&s->gb)) { /* compression_mode */
1933 /* macroblock_type, 1 or 2-bit VLC */
1934 if (!get_bits1(&s->gb)) {
1936 s->qscale = mpeg_get_qscale(s);
1939 for (i = 0; i < mpeg4_block_count[s->chroma_format]; i++) {
1940 if (mpeg4_decode_studio_block(s, (*s->block32)[i], i) < 0)
1941 return AVERROR_INVALIDDATA;
1945 check_marker(s->avctx, &s->gb, "DPCM block start");
1946 avpriv_request_sample(s->avctx, "DPCM encoded block");
1947 next_start_code_studio(&s->gb);
1951 if (get_bits_left(&s->gb) >= 24 && show_bits(&s->gb, 23) == 0) {
1952 next_start_code_studio(&s->gb);
1959 static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
1961 int hours, minutes, seconds;
1963 if (!show_bits(gb, 23)) {
1964 av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
1965 return AVERROR_INVALIDDATA;
1968 hours = get_bits(gb, 5);
1969 minutes = get_bits(gb, 6);
1970 check_marker(s->avctx, gb, "in gop_header");
1971 seconds = get_bits(gb, 6);
1973 s->time_base = seconds + 60*(minutes + 60*hours);
1981 static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb)
1984 s->avctx->profile = get_bits(gb, 4);
1985 s->avctx->level = get_bits(gb, 4);
1987 // for Simple profile, level 0
1988 if (s->avctx->profile == 0 && s->avctx->level == 8) {
1989 s->avctx->level = 0;
1995 static int mpeg4_decode_visual_object(MpegEncContext *s, GetBitContext *gb)
1997 int visual_object_type;
1998 int is_visual_object_identifier = get_bits1(gb);
2000 if (is_visual_object_identifier) {
2003 visual_object_type = get_bits(gb, 4);
2005 if (visual_object_type == VOT_VIDEO_ID ||
2006 visual_object_type == VOT_STILL_TEXTURE_ID) {
2007 int video_signal_type = get_bits1(gb);
2008 if (video_signal_type) {
2009 int video_range, color_description;
2010 skip_bits(gb, 3); // video_format
2011 video_range = get_bits1(gb);
2012 color_description = get_bits1(gb);
2014 s->avctx->color_range = video_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
2016 if (color_description) {
2017 s->avctx->color_primaries = get_bits(gb, 8);
2018 s->avctx->color_trc = get_bits(gb, 8);
2019 s->avctx->colorspace = get_bits(gb, 8);
2027 static void mpeg4_load_default_matrices(MpegEncContext *s)
2031 /* load default matrices */
2032 for (i = 0; i < 64; i++) {
2033 int j = s->idsp.idct_permutation[i];
2034 v = ff_mpeg4_default_intra_matrix[i];
2035 s->intra_matrix[j] = v;
2036 s->chroma_intra_matrix[j] = v;
2038 v = ff_mpeg4_default_non_intra_matrix[i];
2039 s->inter_matrix[j] = v;
2040 s->chroma_inter_matrix[j] = v;
2044 static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2046 MpegEncContext *s = &ctx->m;
2047 int width, height, vo_ver_id;
2050 skip_bits(gb, 1); /* random access */
2051 s->vo_type = get_bits(gb, 8);
2052 if (get_bits1(gb) != 0) { /* is_ol_id */
2053 vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
2054 skip_bits(gb, 3); /* vo_priority */
2058 s->aspect_ratio_info = get_bits(gb, 4);
2059 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
2060 s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
2061 s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
2063 s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
2066 if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
2067 int chroma_format = get_bits(gb, 2);
2068 if (chroma_format != CHROMA_420)
2069 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
2071 s->low_delay = get_bits1(gb);
2072 if (get_bits1(gb)) { /* vbv parameters */
2073 get_bits(gb, 15); /* first_half_bitrate */
2074 check_marker(s->avctx, gb, "after first_half_bitrate");
2075 get_bits(gb, 15); /* latter_half_bitrate */
2076 check_marker(s->avctx, gb, "after latter_half_bitrate");
2077 get_bits(gb, 15); /* first_half_vbv_buffer_size */
2078 check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2079 get_bits(gb, 3); /* latter_half_vbv_buffer_size */
2080 get_bits(gb, 11); /* first_half_vbv_occupancy */
2081 check_marker(s->avctx, gb, "after first_half_vbv_occupancy");
2082 get_bits(gb, 15); /* latter_half_vbv_occupancy */
2083 check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
2086 /* is setting low delay flag only once the smartest thing to do?
2087 * low delay detection will not be overridden. */
2088 if (s->picture_number == 0) {
2089 switch(s->vo_type) {
2090 case SIMPLE_VO_TYPE:
2091 case ADV_SIMPLE_VO_TYPE:
2100 ctx->shape = get_bits(gb, 2); /* vol shape */
2101 if (ctx->shape != RECT_SHAPE)
2102 av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
2103 if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
2104 av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
2105 skip_bits(gb, 4); /* video_object_layer_shape_extension */
2108 check_marker(s->avctx, gb, "before time_increment_resolution");
2110 s->avctx->framerate.num = get_bits(gb, 16);
2111 if (!s->avctx->framerate.num) {
2112 av_log(s->avctx, AV_LOG_ERROR, "framerate==0\n");
2113 return AVERROR_INVALIDDATA;
2116 ctx->time_increment_bits = av_log2(s->avctx->framerate.num - 1) + 1;
2117 if (ctx->time_increment_bits < 1)
2118 ctx->time_increment_bits = 1;
2120 check_marker(s->avctx, gb, "before fixed_vop_rate");
2122 if (get_bits1(gb) != 0) /* fixed_vop_rate */
2123 s->avctx->framerate.den = get_bits(gb, ctx->time_increment_bits);
2125 s->avctx->framerate.den = 1;
2127 s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
2131 if (ctx->shape != BIN_ONLY_SHAPE) {
2132 if (ctx->shape == RECT_SHAPE) {
2133 check_marker(s->avctx, gb, "before width");
2134 width = get_bits(gb, 13);
2135 check_marker(s->avctx, gb, "before height");
2136 height = get_bits(gb, 13);
2137 check_marker(s->avctx, gb, "after height");
2138 if (width && height && /* they should be non zero but who knows */
2139 !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
2140 if (s->width && s->height &&
2141 (s->width != width || s->height != height))
2142 s->context_reinit = 1;
2148 s->progressive_sequence =
2149 s->progressive_frame = get_bits1(gb) ^ 1;
2150 s->interlaced_dct = 0;
2151 if (!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
2152 av_log(s->avctx, AV_LOG_INFO, /* OBMC Disable */
2153 "MPEG-4 OBMC not supported (very likely buggy encoder)\n");
2155 ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
2157 ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
2159 if (ctx->vol_sprite_usage == STATIC_SPRITE)
2160 av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
2161 if (ctx->vol_sprite_usage == STATIC_SPRITE ||
2162 ctx->vol_sprite_usage == GMC_SPRITE) {
2163 if (ctx->vol_sprite_usage == STATIC_SPRITE) {
2164 skip_bits(gb, 13); // sprite_width
2165 check_marker(s->avctx, gb, "after sprite_width");
2166 skip_bits(gb, 13); // sprite_height
2167 check_marker(s->avctx, gb, "after sprite_height");
2168 skip_bits(gb, 13); // sprite_left
2169 check_marker(s->avctx, gb, "after sprite_left");
2170 skip_bits(gb, 13); // sprite_top
2171 check_marker(s->avctx, gb, "after sprite_top");
2173 ctx->num_sprite_warping_points = get_bits(gb, 6);
2174 if (ctx->num_sprite_warping_points > 3) {
2175 av_log(s->avctx, AV_LOG_ERROR,
2176 "%d sprite_warping_points\n",
2177 ctx->num_sprite_warping_points);
2178 ctx->num_sprite_warping_points = 0;
2179 return AVERROR_INVALIDDATA;
2181 s->sprite_warping_accuracy = get_bits(gb, 2);
2182 ctx->sprite_brightness_change = get_bits1(gb);
2183 if (ctx->vol_sprite_usage == STATIC_SPRITE)
2184 skip_bits1(gb); // low_latency_sprite
2186 // FIXME sadct disable bit if verid!=1 && shape not rect
2188 if (get_bits1(gb) == 1) { /* not_8_bit */
2189 s->quant_precision = get_bits(gb, 4); /* quant_precision */
2190 if (get_bits(gb, 4) != 8) /* bits_per_pixel */
2191 av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
2192 if (s->quant_precision != 5)
2193 av_log(s->avctx, AV_LOG_ERROR,
2194 "quant precision %d\n", s->quant_precision);
2195 if (s->quant_precision<3 || s->quant_precision>9) {
2196 s->quant_precision = 5;
2199 s->quant_precision = 5;
2202 // FIXME a bunch of grayscale shape things
2204 if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
2207 mpeg4_load_default_matrices(s);
2209 /* load custom intra matrix */
2210 if (get_bits1(gb)) {
2212 for (i = 0; i < 64; i++) {
2214 if (get_bits_left(gb) < 8) {
2215 av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2216 return AVERROR_INVALIDDATA;
2218 v = get_bits(gb, 8);
2223 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2224 s->intra_matrix[j] = last;
2225 s->chroma_intra_matrix[j] = last;
2228 /* replicate last value */
2229 for (; i < 64; i++) {
2230 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2231 s->intra_matrix[j] = last;
2232 s->chroma_intra_matrix[j] = last;
2236 /* load custom non intra matrix */
2237 if (get_bits1(gb)) {
2239 for (i = 0; i < 64; i++) {
2241 if (get_bits_left(gb) < 8) {
2242 av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2243 return AVERROR_INVALIDDATA;
2245 v = get_bits(gb, 8);
2250 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2251 s->inter_matrix[j] = v;
2252 s->chroma_inter_matrix[j] = v;
2255 /* replicate last value */
2256 for (; i < 64; i++) {
2257 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2258 s->inter_matrix[j] = last;
2259 s->chroma_inter_matrix[j] = last;
2263 // FIXME a bunch of grayscale shape things
2267 s->quarter_sample = get_bits1(gb);
2269 s->quarter_sample = 0;
2271 if (get_bits_left(gb) < 4) {
2272 av_log(s->avctx, AV_LOG_ERROR, "VOL Header truncated\n");
2273 return AVERROR_INVALIDDATA;
2276 if (!get_bits1(gb)) {
2277 int pos = get_bits_count(gb);
2278 int estimation_method = get_bits(gb, 2);
2279 if (estimation_method < 2) {
2280 if (!get_bits1(gb)) {
2281 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */
2282 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */
2283 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */
2284 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */
2285 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */
2286 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upsampling */
2288 if (!get_bits1(gb)) {
2289 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */
2290 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */
2291 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */
2292 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */
2294 if (!check_marker(s->avctx, gb, "in complexity estimation part 1")) {
2295 skip_bits_long(gb, pos - get_bits_count(gb));
2298 if (!get_bits1(gb)) {
2299 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */
2300 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */
2301 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */
2302 ctx->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */
2304 if (!get_bits1(gb)) {
2305 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */
2306 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */
2307 ctx->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */
2308 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */
2309 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */
2310 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */
2312 if (!check_marker(s->avctx, gb, "in complexity estimation part 2")) {
2313 skip_bits_long(gb, pos - get_bits_count(gb));
2316 if (estimation_method == 1) {
2317 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */
2318 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */
2321 av_log(s->avctx, AV_LOG_ERROR,
2322 "Invalid Complexity estimation method %d\n",
2327 ctx->cplx_estimation_trash_i =
2328 ctx->cplx_estimation_trash_p =
2329 ctx->cplx_estimation_trash_b = 0;
2332 ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
2334 s->data_partitioning = get_bits1(gb);
2335 if (s->data_partitioning)
2336 ctx->rvlc = get_bits1(gb);
2338 if (vo_ver_id != 1) {
2339 ctx->new_pred = get_bits1(gb);
2340 if (ctx->new_pred) {
2341 av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
2342 skip_bits(gb, 2); /* requested upstream message type */
2343 skip_bits1(gb); /* newpred segment type */
2345 if (get_bits1(gb)) // reduced_res_vop
2346 av_log(s->avctx, AV_LOG_ERROR,
2347 "reduced resolution VOP not supported\n");
2352 ctx->scalability = get_bits1(gb);
2354 if (ctx->scalability) {
2355 GetBitContext bak = *gb;
2356 int h_sampling_factor_n;
2357 int h_sampling_factor_m;
2358 int v_sampling_factor_n;
2359 int v_sampling_factor_m;
2361 skip_bits1(gb); // hierarchy_type
2362 skip_bits(gb, 4); /* ref_layer_id */
2363 skip_bits1(gb); /* ref_layer_sampling_dir */
2364 h_sampling_factor_n = get_bits(gb, 5);
2365 h_sampling_factor_m = get_bits(gb, 5);
2366 v_sampling_factor_n = get_bits(gb, 5);
2367 v_sampling_factor_m = get_bits(gb, 5);
2368 ctx->enhancement_type = get_bits1(gb);
2370 if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
2371 v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
2372 /* illegal scalability header (VERY broken encoder),
2373 * trying to workaround */
2374 ctx->scalability = 0;
2377 av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
2379 // bin shape stuff FIXME
2383 if (s->avctx->debug&FF_DEBUG_PICT_INFO) {
2384 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",
2385 s->avctx->framerate.den, s->avctx->framerate.num,
2386 ctx->time_increment_bits,
2388 s->progressive_sequence,
2390 ctx->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
2391 s->data_partitioning ? "partition " : "", ctx->rvlc ? "rvlc " : ""
2399 * Decode the user data stuff in the header.
2400 * Also initializes divx/xvid/lavc_version/build.
2402 static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
2404 MpegEncContext *s = &ctx->m;
2408 int ver = 0, build = 0, ver2 = 0, ver3 = 0;
2411 for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
2412 if (show_bits(gb, 23) == 0)
2414 buf[i] = get_bits(gb, 8);
2418 /* divx detection */
2419 e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
2421 e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
2423 ctx->divx_version = ver;
2424 ctx->divx_build = build;
2425 s->divx_packed = e == 3 && last == 'p';
2428 /* libavcodec detection */
2429 e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
2431 e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
2433 e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
2435 if (ver > 0xFFU || ver2 > 0xFFU || ver3 > 0xFFU) {
2436 av_log(s->avctx, AV_LOG_WARNING,
2437 "Unknown Lavc version string encountered, %d.%d.%d; "
2438 "clamping sub-version values to 8-bits.\n",
2441 build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF);
2445 if (strcmp(buf, "ffmpeg") == 0)
2446 ctx->lavc_build = 4600;
2449 ctx->lavc_build = build;
2451 /* Xvid detection */
2452 e = sscanf(buf, "XviD%d", &build);
2454 ctx->xvid_build = build;
2459 int ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
2461 Mpeg4DecContext *ctx = avctx->priv_data;
2462 MpegEncContext *s = &ctx->m;
2464 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
2465 if (s->codec_tag == AV_RL32("XVID") ||
2466 s->codec_tag == AV_RL32("XVIX") ||
2467 s->codec_tag == AV_RL32("RMP4") ||
2468 s->codec_tag == AV_RL32("ZMP4") ||
2469 s->codec_tag == AV_RL32("SIPP"))
2470 ctx->xvid_build = 0;
2473 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
2474 if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
2475 ctx->vol_control_parameters == 0)
2476 ctx->divx_version = 400; // divx 4
2478 if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
2480 ctx->divx_build = -1;
2483 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
2484 if (s->codec_tag == AV_RL32("XVIX"))
2485 s->workaround_bugs |= FF_BUG_XVID_ILACE;
2487 if (s->codec_tag == AV_RL32("UMP4"))
2488 s->workaround_bugs |= FF_BUG_UMP4;
2490 if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
2491 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2493 if (ctx->divx_version > 502 && ctx->divx_build < 1814)
2494 s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
2496 if (ctx->xvid_build <= 3U)
2497 s->padding_bug_score = 256 * 256 * 256 * 64;
2499 if (ctx->xvid_build <= 1U)
2500 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2502 if (ctx->xvid_build <= 12U)
2503 s->workaround_bugs |= FF_BUG_EDGE;
2505 if (ctx->xvid_build <= 32U)
2506 s->workaround_bugs |= FF_BUG_DC_CLIP;
2508 #define SET_QPEL_FUNC(postfix1, postfix2) \
2509 s->qdsp.put_ ## postfix1 = ff_put_ ## postfix2; \
2510 s->qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
2511 s->qdsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
2513 if (ctx->lavc_build < 4653U)
2514 s->workaround_bugs |= FF_BUG_STD_QPEL;
2516 if (ctx->lavc_build < 4655U)
2517 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2519 if (ctx->lavc_build < 4670U)
2520 s->workaround_bugs |= FF_BUG_EDGE;
2522 if (ctx->lavc_build <= 4712U)
2523 s->workaround_bugs |= FF_BUG_DC_CLIP;
2525 if ((ctx->lavc_build&0xFF) >= 100) {
2526 if (ctx->lavc_build > 3621476 && ctx->lavc_build < 3752552 &&
2527 (ctx->lavc_build < 3752037 || ctx->lavc_build > 3752191) // 3.2.1+
2529 s->workaround_bugs |= FF_BUG_IEDGE;
2532 if (ctx->divx_version >= 0)
2533 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2534 if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
2535 s->padding_bug_score = 256 * 256 * 256 * 64;
2537 if (ctx->divx_version < 500U)
2538 s->workaround_bugs |= FF_BUG_EDGE;
2540 if (ctx->divx_version >= 0)
2541 s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
2544 if (s->workaround_bugs & FF_BUG_STD_QPEL) {
2545 SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
2546 SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
2547 SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
2548 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
2549 SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
2550 SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
2552 SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
2553 SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
2554 SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
2555 SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
2556 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
2557 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
2560 if (avctx->debug & FF_DEBUG_BUGS)
2561 av_log(s->avctx, AV_LOG_DEBUG,
2562 "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
2563 s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
2564 ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
2566 if (CONFIG_MPEG4_DECODER && ctx->xvid_build >= 0 &&
2567 s->codec_id == AV_CODEC_ID_MPEG4 &&
2568 avctx->idct_algo == FF_IDCT_AUTO) {
2569 avctx->idct_algo = FF_IDCT_XVID;
2570 ff_mpv_idct_init(s);
2577 static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2579 MpegEncContext *s = &ctx->m;
2580 int time_incr, time_increment;
2584 s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
2585 if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
2586 ctx->vol_control_parameters == 0 && !(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)) {
2587 av_log(s->avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n");
2591 s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
2592 if (s->partitioned_frame)
2593 s->decode_mb = mpeg4_decode_partitioned_mb;
2595 s->decode_mb = mpeg4_decode_mb;
2598 while (get_bits1(gb) != 0)
2601 check_marker(s->avctx, gb, "before time_increment");
2603 if (ctx->time_increment_bits == 0 ||
2604 !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
2605 av_log(s->avctx, AV_LOG_WARNING,
2606 "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);
2608 for (ctx->time_increment_bits = 1;
2609 ctx->time_increment_bits < 16;
2610 ctx->time_increment_bits++) {
2611 if (s->pict_type == AV_PICTURE_TYPE_P ||
2612 (s->pict_type == AV_PICTURE_TYPE_S &&
2613 ctx->vol_sprite_usage == GMC_SPRITE)) {
2614 if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
2616 } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
2620 av_log(s->avctx, AV_LOG_WARNING,
2621 "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits);
2622 if (s->avctx->framerate.num && 4*s->avctx->framerate.num < 1<<ctx->time_increment_bits) {
2623 s->avctx->framerate.num = 1<<ctx->time_increment_bits;
2624 s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
2629 time_increment = get_bits1(gb); // FIXME investigate further
2631 time_increment = get_bits(gb, ctx->time_increment_bits);
2633 if (s->pict_type != AV_PICTURE_TYPE_B) {
2634 s->last_time_base = s->time_base;
2635 s->time_base += time_incr;
2636 s->time = s->time_base * (int64_t)s->avctx->framerate.num + time_increment;
2637 if (s->workaround_bugs & FF_BUG_UMP4) {
2638 if (s->time < s->last_non_b_time) {
2639 /* header is not mpeg-4-compatible, broken encoder,
2640 * trying to workaround */
2642 s->time += s->avctx->framerate.num;
2645 s->pp_time = s->time - s->last_non_b_time;
2646 s->last_non_b_time = s->time;
2648 s->time = (s->last_time_base + time_incr) * (int64_t)s->avctx->framerate.num + time_increment;
2649 s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
2650 if (s->pp_time <= s->pb_time ||
2651 s->pp_time <= s->pp_time - s->pb_time ||
2653 /* messed up order, maybe after seeking? skipping current B-frame */
2654 return FRAME_SKIPPED;
2656 ff_mpeg4_init_direct_mv(s);
2658 if (ctx->t_frame == 0)
2659 ctx->t_frame = s->pb_time;
2660 if (ctx->t_frame == 0)
2661 ctx->t_frame = 1; // 1/0 protection
2662 s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
2663 ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2664 s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
2665 ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2666 if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1) {
2667 s->pb_field_time = 2;
2668 s->pp_field_time = 4;
2669 if (!s->progressive_sequence)
2670 return FRAME_SKIPPED;
2674 if (s->avctx->framerate.den)
2675 pts = ROUNDED_DIV(s->time, s->avctx->framerate.den);
2677 pts = AV_NOPTS_VALUE;
2678 ff_dlog(s->avctx, "MPEG4 PTS: %"PRId64"\n", pts);
2680 check_marker(s->avctx, gb, "before vop_coded");
2683 if (get_bits1(gb) != 1) {
2684 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2685 av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
2686 return FRAME_SKIPPED;
2689 decode_new_pred(ctx, gb);
2691 if (ctx->shape != BIN_ONLY_SHAPE &&
2692 (s->pict_type == AV_PICTURE_TYPE_P ||
2693 (s->pict_type == AV_PICTURE_TYPE_S &&
2694 ctx->vol_sprite_usage == GMC_SPRITE))) {
2695 /* rounding type for motion estimation */
2696 s->no_rounding = get_bits1(gb);
2700 // FIXME reduced res stuff
2702 if (ctx->shape != RECT_SHAPE) {
2703 if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
2704 skip_bits(gb, 13); /* width */
2705 check_marker(s->avctx, gb, "after width");
2706 skip_bits(gb, 13); /* height */
2707 check_marker(s->avctx, gb, "after height");
2708 skip_bits(gb, 13); /* hor_spat_ref */
2709 check_marker(s->avctx, gb, "after hor_spat_ref");
2710 skip_bits(gb, 13); /* ver_spat_ref */
2712 skip_bits1(gb); /* change_CR_disable */
2714 if (get_bits1(gb) != 0)
2715 skip_bits(gb, 8); /* constant_alpha_value */
2718 // FIXME complexity estimation stuff
2720 if (ctx->shape != BIN_ONLY_SHAPE) {
2721 skip_bits_long(gb, ctx->cplx_estimation_trash_i);
2722 if (s->pict_type != AV_PICTURE_TYPE_I)
2723 skip_bits_long(gb, ctx->cplx_estimation_trash_p);
2724 if (s->pict_type == AV_PICTURE_TYPE_B)
2725 skip_bits_long(gb, ctx->cplx_estimation_trash_b);
2727 if (get_bits_left(gb) < 3) {
2728 av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
2729 return AVERROR_INVALIDDATA;
2731 ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
2732 if (!s->progressive_sequence) {
2733 s->top_field_first = get_bits1(gb);
2734 s->alternate_scan = get_bits1(gb);
2736 s->alternate_scan = 0;
2739 if (s->alternate_scan) {
2740 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
2741 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
2742 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
2743 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2745 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
2746 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
2747 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
2748 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2751 if (s->pict_type == AV_PICTURE_TYPE_S) {
2752 if((ctx->vol_sprite_usage == STATIC_SPRITE ||
2753 ctx->vol_sprite_usage == GMC_SPRITE)) {
2754 if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
2755 return AVERROR_INVALIDDATA;
2756 if (ctx->sprite_brightness_change)
2757 av_log(s->avctx, AV_LOG_ERROR,
2758 "sprite_brightness_change not supported\n");
2759 if (ctx->vol_sprite_usage == STATIC_SPRITE)
2760 av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
2762 memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
2763 memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
2767 if (ctx->shape != BIN_ONLY_SHAPE) {
2768 s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision);
2769 if (s->qscale == 0) {
2770 av_log(s->avctx, AV_LOG_ERROR,
2771 "Error, header damaged or not MPEG-4 header (qscale=0)\n");
2772 return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
2775 if (s->pict_type != AV_PICTURE_TYPE_I) {
2776 s->f_code = get_bits(gb, 3); /* fcode_for */
2777 if (s->f_code == 0) {
2778 av_log(s->avctx, AV_LOG_ERROR,
2779 "Error, header damaged or not MPEG-4 header (f_code=0)\n");
2781 return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
2786 if (s->pict_type == AV_PICTURE_TYPE_B) {
2787 s->b_code = get_bits(gb, 3);
2788 if (s->b_code == 0) {
2789 av_log(s->avctx, AV_LOG_ERROR,
2790 "Error, header damaged or not MPEG4 header (b_code=0)\n");
2792 return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly
2797 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2798 av_log(s->avctx, AV_LOG_DEBUG,
2799 "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",
2800 s->qscale, s->f_code, s->b_code,
2801 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")),
2802 gb->size_in_bits,s->progressive_sequence, s->alternate_scan,
2803 s->top_field_first, s->quarter_sample ? "q" : "h",
2804 s->data_partitioning, ctx->resync_marker,
2805 ctx->num_sprite_warping_points, s->sprite_warping_accuracy,
2806 1 - s->no_rounding, s->vo_type,
2807 ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
2808 ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
2809 ctx->cplx_estimation_trash_b,
2815 if (!ctx->scalability) {
2816 if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
2817 skip_bits1(gb); // vop shape coding type
2819 if (ctx->enhancement_type) {
2820 int load_backward_shape = get_bits1(gb);
2821 if (load_backward_shape)
2822 av_log(s->avctx, AV_LOG_ERROR,
2823 "load backward shape isn't supported\n");
2825 skip_bits(gb, 2); // ref_select_code
2828 /* detect buggy encoders which don't set the low_delay flag
2829 * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames
2830 * easily (although it's buggy too) */
2831 if (s->vo_type == 0 && ctx->vol_control_parameters == 0 &&
2832 ctx->divx_version == -1 && s->picture_number == 0) {
2833 av_log(s->avctx, AV_LOG_WARNING,
2834 "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
2838 s->picture_number++; // better than pic number==0 always ;)
2840 // FIXME add short header support
2841 s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
2842 s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
2844 if (s->workaround_bugs & FF_BUG_EDGE) {
2845 s->h_edge_pos = s->width;
2846 s->v_edge_pos = s->height;
2851 static void read_quant_matrix_ext(MpegEncContext *s, GetBitContext *gb)
2855 if (get_bits1(gb)) {
2856 /* intra_quantiser_matrix */
2857 for (i = 0; i < 64; i++) {
2858 v = get_bits(gb, 8);
2859 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2860 s->intra_matrix[j] = v;
2861 s->chroma_intra_matrix[j] = v;
2865 if (get_bits1(gb)) {
2866 /* non_intra_quantiser_matrix */
2867 for (i = 0; i < 64; i++) {
2872 if (get_bits1(gb)) {
2873 /* chroma_intra_quantiser_matrix */
2874 for (i = 0; i < 64; i++) {
2875 v = get_bits(gb, 8);
2876 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2877 s->chroma_intra_matrix[j] = v;
2881 if (get_bits1(gb)) {
2882 /* chroma_non_intra_quantiser_matrix */
2883 for (i = 0; i < 64; i++) {
2888 next_start_code_studio(gb);
2891 static void extension_and_user_data(MpegEncContext *s, GetBitContext *gb, int id)
2894 uint8_t extension_type;
2896 startcode = show_bits_long(gb, 32);
2897 if (startcode == USER_DATA_STARTCODE || startcode == EXT_STARTCODE) {
2899 if ((id == 2 || id == 4) && startcode == EXT_STARTCODE) {
2900 skip_bits_long(gb, 32);
2901 extension_type = get_bits(gb, 4);
2902 if (extension_type == QUANT_MATRIX_EXT_ID)
2903 read_quant_matrix_ext(s, gb);
2908 static void decode_smpte_tc(Mpeg4DecContext *ctx, GetBitContext *gb)
2910 MpegEncContext *s = &ctx->m;
2912 skip_bits(gb, 16); /* Time_code[63..48] */
2913 check_marker(s->avctx, gb, "after Time_code[63..48]");
2914 skip_bits(gb, 16); /* Time_code[47..32] */
2915 check_marker(s->avctx, gb, "after Time_code[47..32]");
2916 skip_bits(gb, 16); /* Time_code[31..16] */
2917 check_marker(s->avctx, gb, "after Time_code[31..16]");
2918 skip_bits(gb, 16); /* Time_code[15..0] */
2919 check_marker(s->avctx, gb, "after Time_code[15..0]");
2920 skip_bits(gb, 4); /* reserved_bits */
2924 * Decode the next studio vop header.
2925 * @return <0 if something went wrong
2927 static int decode_studio_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2929 MpegEncContext *s = &ctx->m;
2931 if (get_bits_left(gb) <= 32)
2934 if (get_bits_long(gb, 32) != VOP_STARTCODE)
2935 return AVERROR_INVALIDDATA;
2937 s->decode_mb = mpeg4_decode_studio_mb;
2939 decode_smpte_tc(ctx, gb);
2941 skip_bits(gb, 10); /* temporal_reference */
2942 skip_bits(gb, 2); /* vop_structure */
2943 s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* vop_coding_type */
2944 if (get_bits1(gb)) { /* vop_coded */
2945 skip_bits1(gb); /* top_field_first */
2946 skip_bits1(gb); /* repeat_first_field */
2947 s->progressive_frame = get_bits1(gb) ^ 1; /* progressive_frame */
2950 if (s->pict_type == AV_PICTURE_TYPE_I) {
2952 reset_studio_dc_predictors(s);
2955 if (ctx->shape != BIN_ONLY_SHAPE) {
2956 s->alternate_scan = get_bits1(gb);
2957 s->frame_pred_frame_dct = get_bits1(gb);
2958 s->dct_precision = get_bits(gb, 2);
2959 s->intra_dc_precision = get_bits(gb, 2);
2960 s->q_scale_type = get_bits1(gb);
2963 if (s->alternate_scan) {
2964 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
2965 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
2966 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
2967 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2969 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
2970 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
2971 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
2972 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2975 mpeg4_load_default_matrices(s);
2977 next_start_code_studio(gb);
2978 extension_and_user_data(s, gb, 4);
2983 static int decode_studiovisualobject(Mpeg4DecContext *ctx, GetBitContext *gb)
2985 MpegEncContext *s = &ctx->m;
2986 int visual_object_type;
2988 skip_bits(gb, 4); /* visual_object_verid */
2989 visual_object_type = get_bits(gb, 4);
2990 if (visual_object_type != VOT_VIDEO_ID) {
2991 avpriv_request_sample(s->avctx, "VO type %u", visual_object_type);
2992 return AVERROR_PATCHWELCOME;
2995 next_start_code_studio(gb);
2996 extension_and_user_data(s, gb, 1);
3001 static int decode_studio_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
3003 MpegEncContext *s = &ctx->m;
3006 skip_bits1(gb); /* random_accessible_vol */
3007 skip_bits(gb, 8); /* video_object_type_indication */
3008 skip_bits(gb, 4); /* video_object_layer_verid */
3009 ctx->shape = get_bits(gb, 2); /* video_object_layer_shape */
3010 skip_bits(gb, 4); /* video_object_layer_shape_extension */
3011 skip_bits1(gb); /* progressive_sequence */
3012 if (ctx->shape != BIN_ONLY_SHAPE) {
3013 ctx->rgb = get_bits1(gb); /* rgb_components */
3014 s->chroma_format = get_bits(gb, 2); /* chroma_format */
3015 if (!s->chroma_format) {
3016 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
3017 return AVERROR_INVALIDDATA;
3020 s->avctx->bits_per_raw_sample = get_bits(gb, 4); /* bit_depth */
3021 if (s->avctx->bits_per_raw_sample == 10) {
3023 s->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
3026 s->avctx->pix_fmt = s->chroma_format == CHROMA_422 ? AV_PIX_FMT_YUV422P10 : AV_PIX_FMT_YUV444P10;
3030 avpriv_request_sample(s->avctx, "MPEG-4 Studio profile bit-depth %u", s->avctx->bits_per_raw_sample);
3031 return AVERROR_PATCHWELCOME;
3034 if (ctx->shape == RECT_SHAPE) {
3035 check_marker(s->avctx, gb, "before video_object_layer_width");
3036 width = get_bits(gb, 14); /* video_object_layer_width */
3037 check_marker(s->avctx, gb, "before video_object_layer_height");
3038 height = get_bits(gb, 14); /* video_object_layer_height */
3039 check_marker(s->avctx, gb, "after video_object_layer_height");
3041 /* Do the same check as non-studio profile */
3042 if (width && height) {
3043 if (s->width && s->height &&
3044 (s->width != width || s->height != height))
3045 s->context_reinit = 1;
3050 s->aspect_ratio_info = get_bits(gb, 4);
3051 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
3052 s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
3053 s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
3055 s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
3057 skip_bits(gb, 4); /* frame_rate_code */
3058 skip_bits(gb, 15); /* first_half_bit_rate */
3059 check_marker(s->avctx, gb, "after first_half_bit_rate");
3060 skip_bits(gb, 15); /* latter_half_bit_rate */
3061 check_marker(s->avctx, gb, "after latter_half_bit_rate");
3062 skip_bits(gb, 15); /* first_half_vbv_buffer_size */
3063 check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
3064 skip_bits(gb, 3); /* latter_half_vbv_buffer_size */
3065 skip_bits(gb, 11); /* first_half_vbv_buffer_size */
3066 check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
3067 skip_bits(gb, 15); /* latter_half_vbv_occupancy */
3068 check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
3069 s->low_delay = get_bits1(gb);
3070 s->mpeg_quant = get_bits1(gb); /* mpeg2_stream */
3072 next_start_code_studio(gb);
3073 extension_and_user_data(s, gb, 2);
3079 * Decode MPEG-4 headers.
3080 * @return <0 if no VOP found (or a damaged one)
3081 * FRAME_SKIPPED if a not coded VOP is found
3082 * 0 if a VOP is found
3084 int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
3086 MpegEncContext *s = &ctx->m;
3087 unsigned startcode, v;
3091 /* search next start code */
3094 if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
3096 if (get_bits(gb, 8) == 0xF0)
3102 if (get_bits_count(gb) >= gb->size_in_bits) {
3103 if (gb->size_in_bits == 8 &&
3104 (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
3105 av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
3106 return FRAME_SKIPPED; // divx bug
3108 return AVERROR_INVALIDDATA; // end of stream
3111 /* use the bits after the test */
3112 v = get_bits(gb, 8);
3113 startcode = ((startcode << 8) | v) & 0xffffffff;
3115 if ((startcode & 0xFFFFFF00) != 0x100)
3116 continue; // no startcode
3118 if (s->avctx->debug & FF_DEBUG_STARTCODE) {
3119 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
3120 if (startcode <= 0x11F)
3121 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
3122 else if (startcode <= 0x12F)
3123 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
3124 else if (startcode <= 0x13F)
3125 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
3126 else if (startcode <= 0x15F)
3127 av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
3128 else if (startcode <= 0x1AF)
3129 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
3130 else if (startcode == 0x1B0)
3131 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
3132 else if (startcode == 0x1B1)
3133 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
3134 else if (startcode == 0x1B2)
3135 av_log(s->avctx, AV_LOG_DEBUG, "User Data");
3136 else if (startcode == 0x1B3)
3137 av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
3138 else if (startcode == 0x1B4)
3139 av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
3140 else if (startcode == 0x1B5)
3141 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
3142 else if (startcode == 0x1B6)
3143 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
3144 else if (startcode == 0x1B7)
3145 av_log(s->avctx, AV_LOG_DEBUG, "slice start");
3146 else if (startcode == 0x1B8)
3147 av_log(s->avctx, AV_LOG_DEBUG, "extension start");
3148 else if (startcode == 0x1B9)
3149 av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
3150 else if (startcode == 0x1BA)
3151 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
3152 else if (startcode == 0x1BB)
3153 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
3154 else if (startcode == 0x1BC)
3155 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
3156 else if (startcode == 0x1BD)
3157 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
3158 else if (startcode == 0x1BE)
3159 av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
3160 else if (startcode == 0x1BF)
3161 av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
3162 else if (startcode == 0x1C0)
3163 av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
3164 else if (startcode == 0x1C1)
3165 av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
3166 else if (startcode == 0x1C2)
3167 av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
3168 else if (startcode == 0x1C3)
3169 av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
3170 else if (startcode <= 0x1C5)
3171 av_log(s->avctx, AV_LOG_DEBUG, "reserved");
3172 else if (startcode <= 0x1FF)
3173 av_log(s->avctx, AV_LOG_DEBUG, "System start");
3174 av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
3177 if (startcode >= 0x120 && startcode <= 0x12F) {
3179 av_log(s->avctx, AV_LOG_WARNING, "Ignoring multiple VOL headers\n");
3183 if (s->studio_profile) {
3184 if ((ret = decode_studio_vol_header(ctx, gb)) < 0)
3188 if ((ret = decode_vol_header(ctx, gb)) < 0)
3191 } else if (startcode == USER_DATA_STARTCODE) {
3192 decode_user_data(ctx, gb);
3193 } else if (startcode == GOP_STARTCODE) {
3194 mpeg4_decode_gop_header(s, gb);
3195 } else if (startcode == VOS_STARTCODE) {
3196 mpeg4_decode_profile_level(s, gb);
3197 if (s->avctx->profile == FF_PROFILE_MPEG4_SIMPLE_STUDIO &&
3198 (s->avctx->level > 0 && s->avctx->level < 9)) {
3199 s->studio_profile = 1;
3200 next_start_code_studio(gb);
3201 extension_and_user_data(s, gb, 0);
3203 } else if (startcode == VISUAL_OBJ_STARTCODE) {
3204 if (s->studio_profile) {
3205 if ((ret = decode_studiovisualobject(ctx, gb)) < 0)
3208 mpeg4_decode_visual_object(s, gb);
3209 } else if (startcode == VOP_STARTCODE) {
3218 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
3220 s->avctx->has_b_frames = !s->low_delay;
3222 if (s->studio_profile)
3223 return decode_studio_vop_header(ctx, gb);
3225 return decode_vop_header(ctx, gb);
3228 av_cold void ff_mpeg4videodec_static_init(void) {
3229 static int done = 0;
3232 ff_rl_init(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
3233 ff_rl_init(&ff_rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
3234 ff_rl_init(&ff_rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
3235 INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
3236 INIT_VLC_RL(ff_rvlc_rl_inter, 1072);
3237 INIT_VLC_RL(ff_rvlc_rl_intra, 1072);
3238 INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
3239 &ff_mpeg4_DCtab_lum[0][1], 2, 1,
3240 &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
3241 INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
3242 &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
3243 &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
3244 INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
3245 &ff_sprite_trajectory_tab[0][1], 4, 2,
3246 &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
3247 INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
3248 &ff_mb_type_b_tab[0][1], 2, 1,
3249 &ff_mb_type_b_tab[0][0], 2, 1, 16);
3254 int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
3256 Mpeg4DecContext *ctx = avctx->priv_data;
3257 MpegEncContext *s = &ctx->m;
3259 /* divx 5.01+ bitstream reorder stuff */
3260 /* Since this clobbers the input buffer and hwaccel codecs still need the
3261 * data during hwaccel->end_frame we should not do this any earlier */
3262 if (s->divx_packed) {
3263 int current_pos = s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb) >> 3);
3264 int startcode_found = 0;
3266 if (buf_size - current_pos > 7) {
3269 for (i = current_pos; i < buf_size - 4; i++)
3274 buf[i + 3] == 0xB6) {
3275 startcode_found = !(buf[i + 4] & 0x40);
3280 if (startcode_found) {
3281 if (!ctx->showed_packed_warning) {
3282 av_log(s->avctx, AV_LOG_INFO, "Video uses a non-standard and "
3283 "wasteful way to store B-frames ('packed B-frames'). "
3284 "Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.\n");
3285 ctx->showed_packed_warning = 1;
3287 av_fast_padded_malloc(&s->bitstream_buffer,
3288 &s->allocated_bitstream_buffer_size,
3289 buf_size - current_pos);
3290 if (!s->bitstream_buffer) {
3291 s->bitstream_buffer_size = 0;
3292 return AVERROR(ENOMEM);
3294 memcpy(s->bitstream_buffer, buf + current_pos,
3295 buf_size - current_pos);
3296 s->bitstream_buffer_size = buf_size - current_pos;
3304 static int mpeg4_update_thread_context(AVCodecContext *dst,
3305 const AVCodecContext *src)
3307 Mpeg4DecContext *s = dst->priv_data;
3308 const Mpeg4DecContext *s1 = src->priv_data;
3309 int init = s->m.context_initialized;
3311 int ret = ff_mpeg_update_thread_context(dst, src);
3316 memcpy(((uint8_t*)s) + sizeof(MpegEncContext), ((uint8_t*)s1) + sizeof(MpegEncContext), sizeof(Mpeg4DecContext) - sizeof(MpegEncContext));
3318 if (CONFIG_MPEG4_DECODER && !init && s1->xvid_build >= 0)
3319 ff_xvid_idct_init(&s->m.idsp, dst);
3325 static av_cold int init_studio_vlcs(Mpeg4DecContext *ctx)
3329 for (i = 0; i < 12; i++) {
3330 ret = init_vlc(&ctx->studio_intra_tab[i], STUDIO_INTRA_BITS, 22,
3331 &ff_mpeg4_studio_intra[i][0][1], 4, 2,
3332 &ff_mpeg4_studio_intra[i][0][0], 4, 2,
3339 ret = init_vlc(&ctx->studio_luma_dc, STUDIO_INTRA_BITS, 19,
3340 &ff_mpeg4_studio_dc_luma[0][1], 4, 2,
3341 &ff_mpeg4_studio_dc_luma[0][0], 4, 2,
3346 ret = init_vlc(&ctx->studio_chroma_dc, STUDIO_INTRA_BITS, 19,
3347 &ff_mpeg4_studio_dc_chroma[0][1], 4, 2,
3348 &ff_mpeg4_studio_dc_chroma[0][0], 4, 2,
3356 static av_cold int decode_init(AVCodecContext *avctx)
3358 Mpeg4DecContext *ctx = avctx->priv_data;
3359 MpegEncContext *s = &ctx->m;
3365 ctx->lavc_build = -1;
3367 if ((ret = ff_h263_decode_init(avctx)) < 0)
3370 ff_mpeg4videodec_static_init();
3371 if ((ret = init_studio_vlcs(ctx)) < 0)
3375 s->low_delay = 0; /* default, might be overridden in the vol header during header parsing */
3376 s->decode_mb = mpeg4_decode_mb;
3377 ctx->time_increment_bits = 4; /* default value for broken headers */
3379 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
3380 avctx->internal->allocate_progress = 1;
3385 static av_cold int decode_end(AVCodecContext *avctx)
3387 Mpeg4DecContext *ctx = avctx->priv_data;
3390 if (!avctx->internal->is_copy) {
3391 for (i = 0; i < 12; i++)
3392 ff_free_vlc(&ctx->studio_intra_tab[i]);
3394 ff_free_vlc(&ctx->studio_luma_dc);
3395 ff_free_vlc(&ctx->studio_chroma_dc);
3398 return ff_h263_decode_end(avctx);
3401 static const AVOption mpeg4_options[] = {
3402 {"quarter_sample", "1/4 subpel MC", offsetof(MpegEncContext, quarter_sample), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
3403 {"divx_packed", "divx style packed b frames", offsetof(MpegEncContext, divx_packed), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
3407 static const AVClass mpeg4_class = {
3408 .class_name = "MPEG4 Video Decoder",
3409 .item_name = av_default_item_name,
3410 .option = mpeg4_options,
3411 .version = LIBAVUTIL_VERSION_INT,
3414 AVCodec ff_mpeg4_decoder = {
3416 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
3417 .type = AVMEDIA_TYPE_VIDEO,
3418 .id = AV_CODEC_ID_MPEG4,
3419 .priv_data_size = sizeof(Mpeg4DecContext),
3420 .init = decode_init,
3421 .close = decode_end,
3422 .decode = ff_h263_decode_frame,
3423 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
3424 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
3425 AV_CODEC_CAP_FRAME_THREADS,
3426 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
3427 .flush = ff_mpeg_flush,
3429 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
3430 .profiles = NULL_IF_CONFIG_SMALL(ff_mpeg4_video_profiles),
3431 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg4_update_thread_context),
3432 .priv_class = &mpeg4_class,
3433 .hw_configs = (const AVCodecHWConfigInternal*[]) {
3434 #if CONFIG_MPEG4_NVDEC_HWACCEL
3435 HWACCEL_NVDEC(mpeg4),
3437 #if CONFIG_MPEG4_VAAPI_HWACCEL
3438 HWACCEL_VAAPI(mpeg4),
3440 #if CONFIG_MPEG4_VDPAU_HWACCEL
3441 HWACCEL_VDPAU(mpeg4),
3443 #if CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
3444 HWACCEL_VIDEOTOOLBOX(mpeg4),