3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #define UNCHECKED_BITSTREAM_READER 1
25 #include "libavutil/internal.h"
26 #include "libavutil/opt.h"
27 #include "error_resilience.h"
30 #include "mpegutils.h"
31 #include "mpegvideo.h"
32 #include "mpegvideodata.h"
33 #include "mpeg4video.h"
39 /* The defines below define the number of bits that are read at once for
40 * reading vlc values. Changing these may improve speed and data cache needs
41 * be aware though that decreasing them may need the number of stages that is
42 * passed to get_vlc* to be increased. */
43 #define SPRITE_TRAJ_VLC_BITS 6
45 #define MB_TYPE_B_VLC_BITS 4
47 static VLC dc_lum, dc_chrom;
48 static VLC sprite_trajectory;
49 static VLC mb_type_b_vlc;
51 static const int mb_type_b_map[4] = {
52 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
53 MB_TYPE_L0L1 | MB_TYPE_16x16,
54 MB_TYPE_L1 | MB_TYPE_16x16,
55 MB_TYPE_L0 | MB_TYPE_16x16,
60 * @param n block index (0-3 are luma, 4-5 are chroma)
61 * @param dir the ac prediction direction
63 void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
66 int16_t *ac_val, *ac_val1;
67 int8_t *const qscale_table = s->current_picture.qscale_table;
70 ac_val = &s->ac_val[0][0][0] + s->block_index[n] * 16;
74 const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
78 if (s->mb_x == 0 || s->qscale == qscale_table[xy] ||
81 for (i = 1; i < 8; i++)
82 block[s->idsp.idct_permutation[i << 3]] += ac_val[i];
84 /* different qscale, we must rescale */
85 for (i = 1; i < 8; i++)
86 block[s->idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
89 const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
91 ac_val -= 16 * s->block_wrap[n];
93 if (s->mb_y == 0 || s->qscale == qscale_table[xy] ||
96 for (i = 1; i < 8; i++)
97 block[s->idsp.idct_permutation[i]] += ac_val[i + 8];
99 /* different qscale, we must rescale */
100 for (i = 1; i < 8; i++)
101 block[s->idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
106 for (i = 1; i < 8; i++)
107 ac_val1[i] = block[s->idsp.idct_permutation[i << 3]];
110 for (i = 1; i < 8; i++)
111 ac_val1[8 + i] = block[s->idsp.idct_permutation[i]];
115 * check if the next stuff is a resync marker or the end.
118 static inline int mpeg4_is_resync(Mpeg4DecContext *ctx)
120 MpegEncContext *s = &ctx->m;
121 int bits_count = get_bits_count(&s->gb);
122 int v = show_bits(&s->gb, 16);
124 if (s->workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker)
128 if (s->pict_type == AV_PICTURE_TYPE_B ||
129 (v >> (8 - s->pict_type) != 1) || s->partitioned_frame)
131 skip_bits(&s->gb, 8 + s->pict_type);
132 bits_count += 8 + s->pict_type;
133 v = show_bits(&s->gb, 16);
136 if (bits_count + 8 >= s->gb.size_in_bits) {
138 v |= 0x7F >> (7 - (bits_count & 7));
143 if (v == ff_mpeg4_resync_prefix[bits_count & 7]) {
145 int mb_num_bits = av_log2(s->mb_num - 1) + 1;
146 GetBitContext gb = s->gb;
148 skip_bits(&s->gb, 1);
149 align_get_bits(&s->gb);
151 for (len = 0; len < 32; len++)
152 if (get_bits1(&s->gb))
155 mb_num = get_bits(&s->gb, mb_num_bits);
156 if (!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
161 if (len >= ff_mpeg4_get_video_packet_prefix_length(s))
168 static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
170 MpegEncContext *s = &ctx->m;
171 int a = 2 << s->sprite_warping_accuracy;
172 int rho = 3 - s->sprite_warping_accuracy;
178 int min_ab, i, w2, h2, w3, h3;
179 int sprite_ref[4][2];
180 int virtual_ref[2][2];
182 // only true for rectangle shapes
183 const int vop_ref[4][2] = { { 0, 0 }, { s->width, 0 },
184 { 0, s->height }, { s->width, s->height } };
185 int d[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
187 if (w <= 0 || h <= 0)
188 return AVERROR_INVALIDDATA;
190 for (i = 0; i < ctx->num_sprite_warping_points; i++) {
194 length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
196 x = get_xbits(gb, length);
198 if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
199 check_marker(s->avctx, gb, "before sprite_trajectory");
201 length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
203 y = get_xbits(gb, length);
205 check_marker(s->avctx, gb, "after sprite_trajectory");
206 ctx->sprite_traj[i][0] = d[i][0] = x;
207 ctx->sprite_traj[i][1] = d[i][1] = y;
210 ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0;
212 while ((1 << alpha) < w)
214 while ((1 << beta) < h)
215 beta++; /* typo in the MPEG-4 std for the definition of w' and h' */
219 // Note, the 4th point isn't used for GMC
220 if (ctx->divx_version == 500 && ctx->divx_build == 413) {
221 sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
222 sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
223 sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
224 sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1];
225 sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0];
226 sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1];
228 sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]);
229 sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]);
230 sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]);
231 sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]);
232 sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]);
233 sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]);
235 /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
236 * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
238 /* This is mostly identical to the MPEG-4 std (and is totally unreadable
239 * because of that...). Perhaps it should be reordered to be more readable.
240 * The idea behind this virtual_ref mess is to be able to use shifts later
241 * per pixel instead of divides so the distance between points is converted
242 * from w&h based to w2&h2 based which are of the 2^x form. */
243 virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) +
244 ROUNDED_DIV(((w - w2) *
245 (r * sprite_ref[0][0] - 16 * vop_ref[0][0]) +
246 w2 * (r * sprite_ref[1][0] - 16 * vop_ref[1][0])), w);
247 virtual_ref[0][1] = 16 * vop_ref[0][1] +
248 ROUNDED_DIV(((w - w2) *
249 (r * sprite_ref[0][1] - 16 * vop_ref[0][1]) +
250 w2 * (r * sprite_ref[1][1] - 16 * vop_ref[1][1])), w);
251 virtual_ref[1][0] = 16 * vop_ref[0][0] +
252 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16 * vop_ref[0][0]) +
253 h2 * (r * sprite_ref[2][0] - 16 * vop_ref[2][0])), h);
254 virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) +
255 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16 * vop_ref[0][1]) +
256 h2 * (r * sprite_ref[2][1] - 16 * vop_ref[2][1])), h);
258 switch (ctx->num_sprite_warping_points) {
260 s->sprite_offset[0][0] =
261 s->sprite_offset[0][1] =
262 s->sprite_offset[1][0] =
263 s->sprite_offset[1][1] = 0;
264 s->sprite_delta[0][0] = a;
265 s->sprite_delta[0][1] =
266 s->sprite_delta[1][0] = 0;
267 s->sprite_delta[1][1] = a;
268 ctx->sprite_shift[0] =
269 ctx->sprite_shift[1] = 0;
272 s->sprite_offset[0][0] = sprite_ref[0][0] - a * vop_ref[0][0];
273 s->sprite_offset[0][1] = sprite_ref[0][1] - a * vop_ref[0][1];
274 s->sprite_offset[1][0] = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) -
275 a * (vop_ref[0][0] / 2);
276 s->sprite_offset[1][1] = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) -
277 a * (vop_ref[0][1] / 2);
278 s->sprite_delta[0][0] = a;
279 s->sprite_delta[0][1] =
280 s->sprite_delta[1][0] = 0;
281 s->sprite_delta[1][1] = a;
282 ctx->sprite_shift[0] =
283 ctx->sprite_shift[1] = 0;
286 s->sprite_offset[0][0] = (sprite_ref[0][0] * (1 << alpha + rho)) +
287 (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
289 (r * sprite_ref[0][1] - virtual_ref[0][1]) *
290 (-vop_ref[0][1]) + (1 << (alpha + rho - 1));
291 s->sprite_offset[0][1] = (sprite_ref[0][1] * (1 << alpha + rho)) +
292 (-r * sprite_ref[0][1] + virtual_ref[0][1]) *
294 (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
295 (-vop_ref[0][1]) + (1 << (alpha + rho - 1));
296 s->sprite_offset[1][0] = ((-r * sprite_ref[0][0] + virtual_ref[0][0]) *
297 (-2 * vop_ref[0][0] + 1) +
298 (r * sprite_ref[0][1] - virtual_ref[0][1]) *
299 (-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
300 sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1)));
301 s->sprite_offset[1][1] = ((-r * sprite_ref[0][1] + virtual_ref[0][1]) *
302 (-2 * vop_ref[0][0] + 1) +
303 (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
304 (-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
305 sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1)));
306 s->sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
307 s->sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]);
308 s->sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
309 s->sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
311 ctx->sprite_shift[0] = alpha + rho;
312 ctx->sprite_shift[1] = alpha + rho + 2;
315 min_ab = FFMIN(alpha, beta);
318 s->sprite_offset[0][0] = (sprite_ref[0][0] * (1<<(alpha + beta + rho - min_ab))) +
319 (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
320 h3 * (-vop_ref[0][0]) +
321 (-r * sprite_ref[0][0] + virtual_ref[1][0]) *
322 w3 * (-vop_ref[0][1]) +
323 (1 << (alpha + beta + rho - min_ab - 1));
324 s->sprite_offset[0][1] = (sprite_ref[0][1] * (1 << (alpha + beta + rho - min_ab))) +
325 (-r * sprite_ref[0][1] + virtual_ref[0][1]) *
326 h3 * (-vop_ref[0][0]) +
327 (-r * sprite_ref[0][1] + virtual_ref[1][1]) *
328 w3 * (-vop_ref[0][1]) +
329 (1 << (alpha + beta + rho - min_ab - 1));
330 s->sprite_offset[1][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
331 h3 * (-2 * vop_ref[0][0] + 1) +
332 (-r * sprite_ref[0][0] + virtual_ref[1][0]) *
333 w3 * (-2 * vop_ref[0][1] + 1) + 2 * w2 * h3 *
334 r * sprite_ref[0][0] - 16 * w2 * h3 +
335 (1 << (alpha + beta + rho - min_ab + 1));
336 s->sprite_offset[1][1] = (-r * sprite_ref[0][1] + virtual_ref[0][1]) *
337 h3 * (-2 * vop_ref[0][0] + 1) +
338 (-r * sprite_ref[0][1] + virtual_ref[1][1]) *
339 w3 * (-2 * vop_ref[0][1] + 1) + 2 * w2 * h3 *
340 r * sprite_ref[0][1] - 16 * w2 * h3 +
341 (1 << (alpha + beta + rho - min_ab + 1));
342 s->sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3;
343 s->sprite_delta[0][1] = (-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3;
344 s->sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3;
345 s->sprite_delta[1][1] = (-r * 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 (s->sprite_delta[0][0] == a << ctx->sprite_shift[0] &&
353 s->sprite_delta[0][1] == 0 &&
354 s->sprite_delta[1][0] == 0 &&
355 s->sprite_delta[1][1] == a << ctx->sprite_shift[0]) {
356 s->sprite_offset[0][0] >>= ctx->sprite_shift[0];
357 s->sprite_offset[0][1] >>= ctx->sprite_shift[0];
358 s->sprite_offset[1][0] >>= ctx->sprite_shift[1];
359 s->sprite_offset[1][1] >>= ctx->sprite_shift[1];
360 s->sprite_delta[0][0] = a;
361 s->sprite_delta[0][1] = 0;
362 s->sprite_delta[1][0] = 0;
363 s->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 if (shift_c < 0 || shift_y < 0 ||
372 FFABS(s->sprite_offset[0][0]) >= INT_MAX >> shift_y ||
373 FFABS(s->sprite_offset[1][0]) >= INT_MAX >> shift_c ||
374 FFABS(s->sprite_offset[0][1]) >= INT_MAX >> shift_y ||
375 FFABS(s->sprite_offset[1][1]) >= INT_MAX >> shift_c
377 avpriv_request_sample(s->avctx, "Too large sprite shift or offset");
381 for (i = 0; i < 2; i++) {
382 s->sprite_offset[0][i] *= 1 << shift_y;
383 s->sprite_offset[1][i] *= 1 << shift_c;
384 s->sprite_delta[0][i] *= 1 << shift_y;
385 s->sprite_delta[1][i] *= 1 << shift_y;
386 ctx->sprite_shift[i] = 16;
389 for (i = 0; i < 2; i++) {
390 if (llabs(s->sprite_offset[0][i] + s->sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
391 llabs(s->sprite_offset[0][i] + s->sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
392 llabs(s->sprite_offset[0][i] + s->sprite_delta[i][0] * (w+16LL) + s->sprite_delta[i][1] * (h+16LL)) >= INT_MAX) {
393 avpriv_request_sample(s->avctx, "Overflow on sprite points");
397 s->real_sprite_warping_points = ctx->num_sprite_warping_points;
402 memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
403 memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
404 return AVERROR_PATCHWELCOME;
407 static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb) {
408 MpegEncContext *s = &ctx->m;
409 int len = FFMIN(ctx->time_increment_bits + 3, 15);
414 check_marker(s->avctx, gb, "after new_pred");
420 * Decode the next video packet.
421 * @return <0 if something went wrong
423 int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
425 MpegEncContext *s = &ctx->m;
427 int mb_num_bits = av_log2(s->mb_num - 1) + 1;
428 int header_extension = 0, mb_num, len;
430 /* is there enough space left for a video packet + header */
431 if (get_bits_count(&s->gb) > s->gb.size_in_bits - 20)
434 for (len = 0; len < 32; len++)
435 if (get_bits1(&s->gb))
438 if (len != ff_mpeg4_get_video_packet_prefix_length(s)) {
439 av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
443 if (ctx->shape != RECT_SHAPE) {
444 header_extension = get_bits1(&s->gb);
445 // FIXME more stuff here
448 mb_num = get_bits(&s->gb, mb_num_bits);
449 if (mb_num >= s->mb_num) {
450 av_log(s->avctx, AV_LOG_ERROR,
451 "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
455 s->mb_x = mb_num % s->mb_width;
456 s->mb_y = mb_num / s->mb_width;
458 if (ctx->shape != BIN_ONLY_SHAPE) {
459 int qscale = get_bits(&s->gb, s->quant_precision);
461 s->chroma_qscale = s->qscale = qscale;
464 if (ctx->shape == RECT_SHAPE)
465 header_extension = get_bits1(&s->gb);
467 if (header_extension) {
470 while (get_bits1(&s->gb) != 0)
473 check_marker(s->avctx, &s->gb, "before time_increment in video packed header");
474 skip_bits(&s->gb, ctx->time_increment_bits); /* time_increment */
475 check_marker(s->avctx, &s->gb, "before vop_coding_type in video packed header");
477 skip_bits(&s->gb, 2); /* vop coding type */
478 // FIXME not rect stuff here
480 if (ctx->shape != BIN_ONLY_SHAPE) {
481 skip_bits(&s->gb, 3); /* intra dc vlc threshold */
482 // FIXME don't just ignore everything
483 if (s->pict_type == AV_PICTURE_TYPE_S &&
484 ctx->vol_sprite_usage == GMC_SPRITE) {
485 if (mpeg4_decode_sprite_trajectory(ctx, &s->gb) < 0)
486 return AVERROR_INVALIDDATA;
487 av_log(s->avctx, AV_LOG_ERROR, "untested\n");
490 // FIXME reduced res stuff here
492 if (s->pict_type != AV_PICTURE_TYPE_I) {
493 int f_code = get_bits(&s->gb, 3); /* fcode_for */
495 av_log(s->avctx, AV_LOG_ERROR,
496 "Error, video packet header damaged (f_code=0)\n");
498 if (s->pict_type == AV_PICTURE_TYPE_B) {
499 int b_code = get_bits(&s->gb, 3);
501 av_log(s->avctx, AV_LOG_ERROR,
502 "Error, video packet header damaged (b_code=0)\n");
507 decode_new_pred(ctx, &s->gb);
513 * Get the average motion vector for a GMC MB.
514 * @param n either 0 for the x component or 1 for y
515 * @return the average MV for a GMC MB
517 static inline int get_amv(Mpeg4DecContext *ctx, int n)
519 MpegEncContext *s = &ctx->m;
520 int x, y, mb_v, sum, dx, dy, shift;
521 int len = 1 << (s->f_code + 4);
522 const int a = s->sprite_warping_accuracy;
524 if (s->workaround_bugs & FF_BUG_AMV)
525 len >>= s->quarter_sample;
527 if (s->real_sprite_warping_points == 1) {
528 if (ctx->divx_version == 500 && ctx->divx_build == 413)
529 sum = s->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
531 sum = RSHIFT(s->sprite_offset[0][n] * (1 << s->quarter_sample), a);
533 dx = s->sprite_delta[n][0];
534 dy = s->sprite_delta[n][1];
535 shift = ctx->sprite_shift[0];
537 dy -= 1 << (shift + a + 1);
539 dx -= 1 << (shift + a + 1);
540 mb_v = s->sprite_offset[0][n] + dx * s->mb_x * 16 + dy * s->mb_y * 16;
543 for (y = 0; y < 16; y++) {
548 for (x = 0; x < 16; x++) {
553 sum = RSHIFT(sum, a + 8 - s->quarter_sample);
565 * Decode the dc value.
566 * @param n block index (0-3 are luma, 4-5 are chroma)
567 * @param dir_ptr the prediction direction will be stored here
568 * @return the quantized dc
570 static inline int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
575 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
577 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
579 if (code < 0 || code > 9 /* && s->nbit < 9 */) {
580 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
589 level = 2 * get_bits1(&s->gb) - 1;
591 if (get_bits1(&s->gb))
592 level = get_bits(&s->gb, code - 1) + (1 << (code - 1));
594 level = -get_bits(&s->gb, code - 1) - (1 << (code - 1));
597 level = get_xbits(&s->gb, code);
601 if (get_bits1(&s->gb) == 0) { /* marker */
602 if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)) {
603 av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
610 return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
614 * Decode first partition.
615 * @return number of MBs decoded or <0 if an error occurred
617 static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
619 MpegEncContext *s = &ctx->m;
621 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
623 /* decode first partition */
624 s->first_slice_line = 1;
625 for (; s->mb_y < s->mb_height; s->mb_y++) {
626 ff_init_block_index(s);
627 for (; s->mb_x < s->mb_width; s->mb_x++) {
628 const int xy = s->mb_x + s->mb_y * s->mb_stride;
633 ff_update_block_index(s);
634 if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
635 s->first_slice_line = 0;
637 if (s->pict_type == AV_PICTURE_TYPE_I) {
641 if (show_bits_long(&s->gb, 19) == DC_MARKER)
644 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
646 av_log(s->avctx, AV_LOG_ERROR,
647 "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
652 s->cbp_table[xy] = cbpc & 3;
653 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
657 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
659 s->current_picture.qscale_table[xy] = s->qscale;
661 s->mbintra_table[xy] = 1;
662 for (i = 0; i < 6; i++) {
664 int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
666 av_log(s->avctx, AV_LOG_ERROR,
667 "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
674 s->pred_dir_table[xy] = dir;
675 } else { /* P/S_TYPE */
676 int mx, my, pred_x, pred_y, bits;
677 int16_t *const mot_val = s->current_picture.motion_val[0][s->block_index[0]];
678 const int stride = s->b8_stride * 2;
681 bits = show_bits(&s->gb, 17);
682 if (bits == MOTION_MARKER)
686 if (bits & 0x10000) {
688 if (s->pict_type == AV_PICTURE_TYPE_S &&
689 ctx->vol_sprite_usage == GMC_SPRITE) {
690 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
694 mx = get_amv(ctx, 0);
695 my = get_amv(ctx, 1);
697 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
704 mot_val[0 + stride] =
705 mot_val[2 + stride] = mx;
708 mot_val[1 + stride] =
709 mot_val[3 + stride] = my;
711 if (s->mbintra_table[xy])
712 ff_clean_intra_table_entries(s);
716 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
718 av_log(s->avctx, AV_LOG_ERROR,
719 "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
725 s->cbp_table[xy] = cbpc & (8 + 3); // 8 is dquant
727 s->mb_intra = ((cbpc & 4) != 0);
730 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
731 s->mbintra_table[xy] = 1;
734 mot_val[0 + stride] =
735 mot_val[2 + stride] = 0;
738 mot_val[1 + stride] =
739 mot_val[3 + stride] = 0;
741 if (s->mbintra_table[xy])
742 ff_clean_intra_table_entries(s);
744 if (s->pict_type == AV_PICTURE_TYPE_S &&
745 ctx->vol_sprite_usage == GMC_SPRITE &&
747 s->mcsel = get_bits1(&s->gb);
751 if ((cbpc & 16) == 0) {
752 /* 16x16 motion prediction */
754 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
756 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
760 my = ff_h263_decode_motion(s, pred_y, s->f_code);
763 s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
766 mx = get_amv(ctx, 0);
767 my = get_amv(ctx, 1);
768 s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
775 mot_val[0 + stride] =
776 mot_val[2 + stride] = mx;
779 mot_val[1 + stride] =
780 mot_val[3 + stride] = my;
783 s->current_picture.mb_type[xy] = MB_TYPE_8x8 |
785 for (i = 0; i < 4; i++) {
786 int16_t *mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
787 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
791 my = ff_h263_decode_motion(s, pred_y, s->f_code);
808 * decode second partition.
809 * @return <0 if an error occurred
811 static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
814 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
816 s->mb_x = s->resync_mb_x;
817 s->first_slice_line = 1;
818 for (s->mb_y = s->resync_mb_y; mb_num < mb_count; s->mb_y++) {
819 ff_init_block_index(s);
820 for (; mb_num < mb_count && s->mb_x < s->mb_width; s->mb_x++) {
821 const int xy = s->mb_x + s->mb_y * s->mb_stride;
824 ff_update_block_index(s);
825 if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
826 s->first_slice_line = 0;
828 if (s->pict_type == AV_PICTURE_TYPE_I) {
829 int ac_pred = get_bits1(&s->gb);
830 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
832 av_log(s->avctx, AV_LOG_ERROR,
833 "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
837 s->cbp_table[xy] |= cbpy << 2;
838 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
839 } else { /* P || S_TYPE */
840 if (IS_INTRA(s->current_picture.mb_type[xy])) {
843 int ac_pred = get_bits1(&s->gb);
844 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
847 av_log(s->avctx, AV_LOG_ERROR,
848 "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
852 if (s->cbp_table[xy] & 8)
853 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
854 s->current_picture.qscale_table[xy] = s->qscale;
856 for (i = 0; i < 6; i++) {
858 int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
860 av_log(s->avctx, AV_LOG_ERROR,
861 "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
868 s->cbp_table[xy] &= 3; // remove dquant
869 s->cbp_table[xy] |= cbpy << 2;
870 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
871 s->pred_dir_table[xy] = dir;
872 } else if (IS_SKIP(s->current_picture.mb_type[xy])) {
873 s->current_picture.qscale_table[xy] = s->qscale;
874 s->cbp_table[xy] = 0;
876 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
879 av_log(s->avctx, AV_LOG_ERROR,
880 "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
884 if (s->cbp_table[xy] & 8)
885 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
886 s->current_picture.qscale_table[xy] = s->qscale;
888 s->cbp_table[xy] &= 3; // remove dquant
889 s->cbp_table[xy] |= (cbpy ^ 0xf) << 2;
893 if (mb_num >= mb_count)
901 * Decode the first and second partition.
902 * @return <0 if error (and sets error type in the error_status_table)
904 int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx)
906 MpegEncContext *s = &ctx->m;
908 const int part_a_error = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR;
909 const int part_a_end = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END | ER_MV_END) : ER_MV_END;
911 mb_num = mpeg4_decode_partition_a(ctx);
913 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
914 s->mb_x, s->mb_y, part_a_error);
918 if (s->resync_mb_x + s->resync_mb_y * s->mb_width + mb_num > s->mb_num) {
919 av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
920 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
921 s->mb_x, s->mb_y, part_a_error);
925 s->mb_num_left = mb_num;
927 if (s->pict_type == AV_PICTURE_TYPE_I) {
928 while (show_bits(&s->gb, 9) == 1)
929 skip_bits(&s->gb, 9);
930 if (get_bits_long(&s->gb, 19) != DC_MARKER) {
931 av_log(s->avctx, AV_LOG_ERROR,
932 "marker missing after first I partition at %d %d\n",
937 while (show_bits(&s->gb, 10) == 1)
938 skip_bits(&s->gb, 10);
939 if (get_bits(&s->gb, 17) != MOTION_MARKER) {
940 av_log(s->avctx, AV_LOG_ERROR,
941 "marker missing after first P partition at %d %d\n",
946 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
947 s->mb_x - 1, s->mb_y, part_a_end);
949 if (mpeg4_decode_partition_b(s, mb_num) < 0) {
950 if (s->pict_type == AV_PICTURE_TYPE_P)
951 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
952 s->mb_x, s->mb_y, ER_DC_ERROR);
955 if (s->pict_type == AV_PICTURE_TYPE_P)
956 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
957 s->mb_x - 1, s->mb_y, ER_DC_END);
965 * @return <0 if an error occurred
967 static inline int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block,
968 int n, int coded, int intra, int rvlc)
970 MpegEncContext *s = &ctx->m;
971 int level, i, last, run, qmul, qadd;
972 int av_uninit(dc_pred_dir);
975 const uint8_t *scan_table;
977 // Note intra & rvlc should be optimized away if this is inlined
980 if (ctx->use_intra_dc_vlc) {
982 if (s->partitioned_frame) {
983 level = s->dc_val[0][s->block_index[n]];
985 level = FASTDIV((level + (s->y_dc_scale >> 1)), s->y_dc_scale);
987 level = FASTDIV((level + (s->c_dc_scale >> 1)), s->c_dc_scale);
988 dc_pred_dir = (s->pred_dir_table[s->mb_x + s->mb_y * s->mb_stride] << n) & 32;
990 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
998 ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
1004 rl = &ff_rvlc_rl_intra;
1005 rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
1007 rl = &ff_mpeg4_rl_intra;
1008 rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
1011 if (dc_pred_dir == 0)
1012 scan_table = s->intra_v_scantable.permutated; /* left */
1014 scan_table = s->intra_h_scantable.permutated; /* top */
1016 scan_table = s->intra_scantable.permutated;
1023 s->block_last_index[n] = i;
1027 rl = &ff_rvlc_rl_inter;
1029 rl = &ff_h263_rl_inter;
1031 scan_table = s->intra_scantable.permutated;
1033 if (s->mpeg_quant) {
1037 rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
1039 rl_vlc = ff_h263_rl_inter.rl_vlc[0];
1041 qmul = s->qscale << 1;
1042 qadd = (s->qscale - 1) | 1;
1044 rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
1046 rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
1050 OPEN_READER(re, &s->gb);
1052 UPDATE_CACHE(re, &s->gb);
1053 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1057 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1058 av_log(s->avctx, AV_LOG_ERROR,
1059 "1. marker bit missing in rvlc esc\n");
1062 SKIP_CACHE(re, &s->gb, 1);
1064 last = SHOW_UBITS(re, &s->gb, 1);
1065 SKIP_CACHE(re, &s->gb, 1);
1066 run = SHOW_UBITS(re, &s->gb, 6);
1067 SKIP_COUNTER(re, &s->gb, 1 + 1 + 6);
1068 UPDATE_CACHE(re, &s->gb);
1070 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1071 av_log(s->avctx, AV_LOG_ERROR,
1072 "2. marker bit missing in rvlc esc\n");
1075 SKIP_CACHE(re, &s->gb, 1);
1077 level = SHOW_UBITS(re, &s->gb, 11);
1078 SKIP_CACHE(re, &s->gb, 11);
1080 if (SHOW_UBITS(re, &s->gb, 5) != 0x10) {
1081 av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
1084 SKIP_CACHE(re, &s->gb, 5);
1086 level = level * qmul + qadd;
1087 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1088 SKIP_COUNTER(re, &s->gb, 1 + 11 + 5 + 1);
1095 cache = GET_CACHE(re, &s->gb);
1098 cache ^= 0xC0000000;
1100 if (cache & 0x80000000) {
1101 if (cache & 0x40000000) {
1103 SKIP_CACHE(re, &s->gb, 2);
1104 last = SHOW_UBITS(re, &s->gb, 1);
1105 SKIP_CACHE(re, &s->gb, 1);
1106 run = SHOW_UBITS(re, &s->gb, 6);
1107 SKIP_COUNTER(re, &s->gb, 2 + 1 + 6);
1108 UPDATE_CACHE(re, &s->gb);
1111 level = SHOW_SBITS(re, &s->gb, 12);
1112 LAST_SKIP_BITS(re, &s->gb, 12);
1114 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1115 av_log(s->avctx, AV_LOG_ERROR,
1116 "1. marker bit missing in 3. esc\n");
1117 if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
1120 SKIP_CACHE(re, &s->gb, 1);
1122 level = SHOW_SBITS(re, &s->gb, 12);
1123 SKIP_CACHE(re, &s->gb, 12);
1125 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1126 av_log(s->avctx, AV_LOG_ERROR,
1127 "2. marker bit missing in 3. esc\n");
1128 if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
1132 SKIP_COUNTER(re, &s->gb, 1 + 12 + 1);
1136 if (s->error_recognition >= FF_ER_COMPLIANT) {
1137 const int abs_level= FFABS(level);
1138 if (abs_level<=MAX_LEVEL && run<=MAX_RUN) {
1139 const int run1= run - rl->max_run[last][abs_level] - 1;
1140 if (abs_level <= rl->max_level[last][run]) {
1141 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1144 if (s->error_recognition > FF_ER_COMPLIANT) {
1145 if (abs_level <= rl->max_level[last][run]*2) {
1146 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1149 if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) {
1150 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1158 level = level * qmul + qadd;
1160 level = level * qmul - qadd;
1162 if ((unsigned)(level + 2048) > 4095) {
1163 if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)) {
1164 if (level > 2560 || level < -2560) {
1165 av_log(s->avctx, AV_LOG_ERROR,
1166 "|level| overflow in 3. esc, qp=%d\n",
1171 level = level < 0 ? -2048 : 2047;
1179 SKIP_BITS(re, &s->gb, 2);
1180 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1181 i += run + rl->max_run[run >> 7][level / qmul] + 1; // FIXME opt indexing
1182 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1183 LAST_SKIP_BITS(re, &s->gb, 1);
1187 SKIP_BITS(re, &s->gb, 1);
1188 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1190 level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; // FIXME opt indexing
1191 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1192 LAST_SKIP_BITS(re, &s->gb, 1);
1197 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1198 LAST_SKIP_BITS(re, &s->gb, 1);
1200 ff_tlog(s->avctx, "dct[%d][%d] = %- 4d end?:%d\n", scan_table[i&63]&7, scan_table[i&63] >> 3, level, i>62);
1204 av_log(s->avctx, AV_LOG_ERROR,
1205 "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1209 block[scan_table[i]] = level;
1213 block[scan_table[i]] = level;
1215 CLOSE_READER(re, &s->gb);
1220 if (!ctx->use_intra_dc_vlc) {
1221 block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
1223 i -= i >> 31; // if (i == -1) i = 0;
1226 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1228 i = 63; // FIXME not optimal
1230 s->block_last_index[n] = i;
1235 * decode partition C of one MB.
1236 * @return <0 if an error occurred
1238 static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
1240 Mpeg4DecContext *ctx = (Mpeg4DecContext *)s;
1242 const int xy = s->mb_x + s->mb_y * s->mb_stride;
1244 mb_type = s->current_picture.mb_type[xy];
1245 cbp = s->cbp_table[xy];
1247 ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1249 if (s->current_picture.qscale_table[xy] != s->qscale)
1250 ff_set_qscale(s, s->current_picture.qscale_table[xy]);
1252 if (s->pict_type == AV_PICTURE_TYPE_P ||
1253 s->pict_type == AV_PICTURE_TYPE_S) {
1255 for (i = 0; i < 4; i++) {
1256 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
1257 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
1259 s->mb_intra = IS_INTRA(mb_type);
1261 if (IS_SKIP(mb_type)) {
1263 for (i = 0; i < 6; i++)
1264 s->block_last_index[i] = -1;
1265 s->mv_dir = MV_DIR_FORWARD;
1266 s->mv_type = MV_TYPE_16X16;
1267 if (s->pict_type == AV_PICTURE_TYPE_S
1268 && ctx->vol_sprite_usage == GMC_SPRITE) {
1275 } else if (s->mb_intra) {
1276 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1277 } else if (!s->mb_intra) {
1278 // s->mcsel = 0; // FIXME do we need to init that?
1280 s->mv_dir = MV_DIR_FORWARD;
1281 if (IS_8X8(mb_type)) {
1282 s->mv_type = MV_TYPE_8X8;
1284 s->mv_type = MV_TYPE_16X16;
1287 } else { /* I-Frame */
1289 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1292 if (!IS_SKIP(mb_type)) {
1294 s->bdsp.clear_blocks(s->block[0]);
1295 /* decode each block */
1296 for (i = 0; i < 6; i++) {
1297 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, s->mb_intra, ctx->rvlc) < 0) {
1298 av_log(s->avctx, AV_LOG_ERROR,
1299 "texture corrupted at %d %d %d\n",
1300 s->mb_x, s->mb_y, s->mb_intra);
1307 /* per-MB end of slice check */
1308 if (--s->mb_num_left <= 0) {
1309 if (mpeg4_is_resync(ctx))
1314 if (mpeg4_is_resync(ctx)) {
1315 const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
1316 if (s->cbp_table[xy + delta])
1323 static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
1325 Mpeg4DecContext *ctx = (Mpeg4DecContext *)s;
1326 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1328 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1329 const int xy = s->mb_x + s->mb_y * s->mb_stride;
1331 av_assert2(s->h263_pred);
1333 if (s->pict_type == AV_PICTURE_TYPE_P ||
1334 s->pict_type == AV_PICTURE_TYPE_S) {
1336 if (get_bits1(&s->gb)) {
1339 for (i = 0; i < 6; i++)
1340 s->block_last_index[i] = -1;
1341 s->mv_dir = MV_DIR_FORWARD;
1342 s->mv_type = MV_TYPE_16X16;
1343 if (s->pict_type == AV_PICTURE_TYPE_S &&
1344 ctx->vol_sprite_usage == GMC_SPRITE) {
1345 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1350 s->mv[0][0][0] = get_amv(ctx, 0);
1351 s->mv[0][0][1] = get_amv(ctx, 1);
1354 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1364 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
1366 av_log(s->avctx, AV_LOG_ERROR,
1367 "mcbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1370 } while (cbpc == 20);
1372 s->bdsp.clear_blocks(s->block[0]);
1374 s->mb_intra = ((cbpc & 4) != 0);
1378 if (s->pict_type == AV_PICTURE_TYPE_S &&
1379 ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
1380 s->mcsel = get_bits1(&s->gb);
1383 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
1385 av_log(s->avctx, AV_LOG_ERROR,
1386 "P cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1387 return AVERROR_INVALIDDATA;
1390 cbp = (cbpc & 3) | (cbpy << 2);
1392 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1393 if ((!s->progressive_sequence) &&
1394 (cbp || (s->workaround_bugs & FF_BUG_XVID_ILACE)))
1395 s->interlaced_dct = get_bits1(&s->gb);
1397 s->mv_dir = MV_DIR_FORWARD;
1398 if ((cbpc & 16) == 0) {
1400 s->current_picture.mb_type[xy] = MB_TYPE_GMC |
1403 /* 16x16 global motion prediction */
1404 s->mv_type = MV_TYPE_16X16;
1405 mx = get_amv(ctx, 0);
1406 my = get_amv(ctx, 1);
1407 s->mv[0][0][0] = mx;
1408 s->mv[0][0][1] = my;
1409 } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
1410 s->current_picture.mb_type[xy] = MB_TYPE_16x8 |
1413 /* 16x8 field motion prediction */
1414 s->mv_type = MV_TYPE_FIELD;
1416 s->field_select[0][0] = get_bits1(&s->gb);
1417 s->field_select[0][1] = get_bits1(&s->gb);
1419 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1421 for (i = 0; i < 2; i++) {
1422 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1426 my = ff_h263_decode_motion(s, pred_y / 2, s->f_code);
1430 s->mv[0][i][0] = mx;
1431 s->mv[0][i][1] = my;
1434 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1435 /* 16x16 motion prediction */
1436 s->mv_type = MV_TYPE_16X16;
1437 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1438 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1443 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1447 s->mv[0][0][0] = mx;
1448 s->mv[0][0][1] = my;
1451 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
1452 s->mv_type = MV_TYPE_8X8;
1453 for (i = 0; i < 4; i++) {
1454 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1455 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1459 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1462 s->mv[0][i][0] = mx;
1463 s->mv[0][i][1] = my;
1468 } else if (s->pict_type == AV_PICTURE_TYPE_B) {
1469 int modb1; // first bit of modb
1470 int modb2; // second bit of modb
1473 s->mb_intra = 0; // B-frames never contain intra blocks
1474 s->mcsel = 0; // ... true gmc blocks
1477 for (i = 0; i < 2; i++) {
1478 s->last_mv[i][0][0] =
1479 s->last_mv[i][0][1] =
1480 s->last_mv[i][1][0] =
1481 s->last_mv[i][1][1] = 0;
1484 ff_thread_await_progress(&s->next_picture_ptr->tf, s->mb_y, 0);
1487 /* if we skipped it in the future P-frame than skip it now too */
1488 s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
1490 if (s->mb_skipped) {
1492 for (i = 0; i < 6; i++)
1493 s->block_last_index[i] = -1;
1495 s->mv_dir = MV_DIR_FORWARD;
1496 s->mv_type = MV_TYPE_16X16;
1501 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1507 modb1 = get_bits1(&s->gb);
1509 // like MB_TYPE_B_DIRECT but no vectors coded
1510 mb_type = MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1;
1513 modb2 = get_bits1(&s->gb);
1514 mb_type = get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
1516 av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
1519 mb_type = mb_type_b_map[mb_type];
1523 s->bdsp.clear_blocks(s->block[0]);
1524 cbp = get_bits(&s->gb, 6);
1527 if ((!IS_DIRECT(mb_type)) && cbp) {
1528 if (get_bits1(&s->gb))
1529 ff_set_qscale(s, s->qscale + get_bits1(&s->gb) * 4 - 2);
1532 if (!s->progressive_sequence) {
1534 s->interlaced_dct = get_bits1(&s->gb);
1536 if (!IS_DIRECT(mb_type) && get_bits1(&s->gb)) {
1537 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1538 mb_type &= ~MB_TYPE_16x16;
1540 if (USES_LIST(mb_type, 0)) {
1541 s->field_select[0][0] = get_bits1(&s->gb);
1542 s->field_select[0][1] = get_bits1(&s->gb);
1544 if (USES_LIST(mb_type, 1)) {
1545 s->field_select[1][0] = get_bits1(&s->gb);
1546 s->field_select[1][1] = get_bits1(&s->gb);
1552 if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
1553 s->mv_type = MV_TYPE_16X16;
1555 if (USES_LIST(mb_type, 0)) {
1556 s->mv_dir = MV_DIR_FORWARD;
1558 mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1559 my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1560 s->last_mv[0][1][0] =
1561 s->last_mv[0][0][0] =
1562 s->mv[0][0][0] = mx;
1563 s->last_mv[0][1][1] =
1564 s->last_mv[0][0][1] =
1565 s->mv[0][0][1] = my;
1568 if (USES_LIST(mb_type, 1)) {
1569 s->mv_dir |= MV_DIR_BACKWARD;
1571 mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1572 my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1573 s->last_mv[1][1][0] =
1574 s->last_mv[1][0][0] =
1575 s->mv[1][0][0] = mx;
1576 s->last_mv[1][1][1] =
1577 s->last_mv[1][0][1] =
1578 s->mv[1][0][1] = my;
1580 } else if (!IS_DIRECT(mb_type)) {
1581 s->mv_type = MV_TYPE_FIELD;
1583 if (USES_LIST(mb_type, 0)) {
1584 s->mv_dir = MV_DIR_FORWARD;
1586 for (i = 0; i < 2; i++) {
1587 mx = ff_h263_decode_motion(s, s->last_mv[0][i][0], s->f_code);
1588 my = ff_h263_decode_motion(s, s->last_mv[0][i][1] / 2, s->f_code);
1589 s->last_mv[0][i][0] =
1590 s->mv[0][i][0] = mx;
1591 s->last_mv[0][i][1] = (s->mv[0][i][1] = my) * 2;
1595 if (USES_LIST(mb_type, 1)) {
1596 s->mv_dir |= MV_DIR_BACKWARD;
1598 for (i = 0; i < 2; i++) {
1599 mx = ff_h263_decode_motion(s, s->last_mv[1][i][0], s->b_code);
1600 my = ff_h263_decode_motion(s, s->last_mv[1][i][1] / 2, s->b_code);
1601 s->last_mv[1][i][0] =
1602 s->mv[1][i][0] = mx;
1603 s->last_mv[1][i][1] = (s->mv[1][i][1] = my) * 2;
1609 if (IS_DIRECT(mb_type)) {
1610 if (IS_SKIP(mb_type)) {
1614 mx = ff_h263_decode_motion(s, 0, 1);
1615 my = ff_h263_decode_motion(s, 0, 1);
1618 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1619 mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
1621 s->current_picture.mb_type[xy] = mb_type;
1622 } else { /* I-Frame */
1624 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
1626 av_log(s->avctx, AV_LOG_ERROR,
1627 "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1630 } while (cbpc == 8);
1636 s->ac_pred = get_bits1(&s->gb);
1638 s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
1640 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
1642 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1644 av_log(s->avctx, AV_LOG_ERROR,
1645 "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1648 cbp = (cbpc & 3) | (cbpy << 2);
1650 ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1653 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1655 if (!s->progressive_sequence)
1656 s->interlaced_dct = get_bits1(&s->gb);
1658 s->bdsp.clear_blocks(s->block[0]);
1659 /* decode each block */
1660 for (i = 0; i < 6; i++) {
1661 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 1, 0) < 0)
1668 /* decode each block */
1669 for (i = 0; i < 6; i++) {
1670 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 0, 0) < 0)
1676 /* per-MB end of slice check */
1677 if (s->codec_id == AV_CODEC_ID_MPEG4) {
1678 int next = mpeg4_is_resync(ctx);
1680 if (s->mb_x + s->mb_y*s->mb_width + 1 > next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
1682 } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
1685 if (s->pict_type == AV_PICTURE_TYPE_B) {
1686 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
1687 ff_thread_await_progress(&s->next_picture_ptr->tf,
1688 (s->mb_x + delta >= s->mb_width)
1689 ? FFMIN(s->mb_y + 1, s->mb_height - 1)
1691 if (s->next_picture.mbskip_table[xy + delta])
1702 static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
1704 int hours, minutes, seconds;
1706 if (!show_bits(gb, 23)) {
1707 av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
1711 hours = get_bits(gb, 5);
1712 minutes = get_bits(gb, 6);
1713 check_marker(s->avctx, gb, "in gop_header");
1714 seconds = get_bits(gb, 6);
1716 s->time_base = seconds + 60*(minutes + 60*hours);
1724 static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb)
1727 s->avctx->profile = get_bits(gb, 4);
1728 s->avctx->level = get_bits(gb, 4);
1730 // for Simple profile, level 0
1731 if (s->avctx->profile == 0 && s->avctx->level == 8) {
1732 s->avctx->level = 0;
1738 static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
1740 MpegEncContext *s = &ctx->m;
1741 int width, height, vo_ver_id;
1744 skip_bits(gb, 1); /* random access */
1745 s->vo_type = get_bits(gb, 8);
1746 if (get_bits1(gb) != 0) { /* is_ol_id */
1747 vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
1748 skip_bits(gb, 3); /* vo_priority */
1752 s->aspect_ratio_info = get_bits(gb, 4);
1753 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
1754 s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
1755 s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
1757 s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
1760 if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
1761 int chroma_format = get_bits(gb, 2);
1762 if (chroma_format != CHROMA_420)
1763 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
1765 s->low_delay = get_bits1(gb);
1766 if (get_bits1(gb)) { /* vbv parameters */
1767 get_bits(gb, 15); /* first_half_bitrate */
1768 check_marker(s->avctx, gb, "after first_half_bitrate");
1769 get_bits(gb, 15); /* latter_half_bitrate */
1770 check_marker(s->avctx, gb, "after latter_half_bitrate");
1771 get_bits(gb, 15); /* first_half_vbv_buffer_size */
1772 check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
1773 get_bits(gb, 3); /* latter_half_vbv_buffer_size */
1774 get_bits(gb, 11); /* first_half_vbv_occupancy */
1775 check_marker(s->avctx, gb, "after first_half_vbv_occupancy");
1776 get_bits(gb, 15); /* latter_half_vbv_occupancy */
1777 check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
1780 /* is setting low delay flag only once the smartest thing to do?
1781 * low delay detection will not be overridden. */
1782 if (s->picture_number == 0) {
1783 switch(s->vo_type) {
1784 case SIMPLE_VO_TYPE:
1785 case ADV_SIMPLE_VO_TYPE:
1794 ctx->shape = get_bits(gb, 2); /* vol shape */
1795 if (ctx->shape != RECT_SHAPE)
1796 av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
1797 if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
1798 av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
1799 skip_bits(gb, 4); /* video_object_layer_shape_extension */
1802 check_marker(s->avctx, gb, "before time_increment_resolution");
1804 s->avctx->framerate.num = get_bits(gb, 16);
1805 if (!s->avctx->framerate.num) {
1806 av_log(s->avctx, AV_LOG_ERROR, "framerate==0\n");
1807 return AVERROR_INVALIDDATA;
1810 ctx->time_increment_bits = av_log2(s->avctx->framerate.num - 1) + 1;
1811 if (ctx->time_increment_bits < 1)
1812 ctx->time_increment_bits = 1;
1814 check_marker(s->avctx, gb, "before fixed_vop_rate");
1816 if (get_bits1(gb) != 0) /* fixed_vop_rate */
1817 s->avctx->framerate.den = get_bits(gb, ctx->time_increment_bits);
1819 s->avctx->framerate.den = 1;
1821 s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
1825 if (ctx->shape != BIN_ONLY_SHAPE) {
1826 if (ctx->shape == RECT_SHAPE) {
1827 check_marker(s->avctx, gb, "before width");
1828 width = get_bits(gb, 13);
1829 check_marker(s->avctx, gb, "before height");
1830 height = get_bits(gb, 13);
1831 check_marker(s->avctx, gb, "after height");
1832 if (width && height && /* they should be non zero but who knows */
1833 !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
1834 if (s->width && s->height &&
1835 (s->width != width || s->height != height))
1836 s->context_reinit = 1;
1842 s->progressive_sequence =
1843 s->progressive_frame = get_bits1(gb) ^ 1;
1844 s->interlaced_dct = 0;
1845 if (!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
1846 av_log(s->avctx, AV_LOG_INFO, /* OBMC Disable */
1847 "MPEG-4 OBMC not supported (very likely buggy encoder)\n");
1849 ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
1851 ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
1853 if (ctx->vol_sprite_usage == STATIC_SPRITE)
1854 av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
1855 if (ctx->vol_sprite_usage == STATIC_SPRITE ||
1856 ctx->vol_sprite_usage == GMC_SPRITE) {
1857 if (ctx->vol_sprite_usage == STATIC_SPRITE) {
1858 skip_bits(gb, 13); // sprite_width
1859 check_marker(s->avctx, gb, "after sprite_width");
1860 skip_bits(gb, 13); // sprite_height
1861 check_marker(s->avctx, gb, "after sprite_height");
1862 skip_bits(gb, 13); // sprite_left
1863 check_marker(s->avctx, gb, "after sprite_left");
1864 skip_bits(gb, 13); // sprite_top
1865 check_marker(s->avctx, gb, "after sprite_top");
1867 ctx->num_sprite_warping_points = get_bits(gb, 6);
1868 if (ctx->num_sprite_warping_points > 3) {
1869 av_log(s->avctx, AV_LOG_ERROR,
1870 "%d sprite_warping_points\n",
1871 ctx->num_sprite_warping_points);
1872 ctx->num_sprite_warping_points = 0;
1873 return AVERROR_INVALIDDATA;
1875 s->sprite_warping_accuracy = get_bits(gb, 2);
1876 ctx->sprite_brightness_change = get_bits1(gb);
1877 if (ctx->vol_sprite_usage == STATIC_SPRITE)
1878 skip_bits1(gb); // low_latency_sprite
1880 // FIXME sadct disable bit if verid!=1 && shape not rect
1882 if (get_bits1(gb) == 1) { /* not_8_bit */
1883 s->quant_precision = get_bits(gb, 4); /* quant_precision */
1884 if (get_bits(gb, 4) != 8) /* bits_per_pixel */
1885 av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
1886 if (s->quant_precision != 5)
1887 av_log(s->avctx, AV_LOG_ERROR,
1888 "quant precision %d\n", s->quant_precision);
1889 if (s->quant_precision<3 || s->quant_precision>9) {
1890 s->quant_precision = 5;
1893 s->quant_precision = 5;
1896 // FIXME a bunch of grayscale shape things
1898 if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
1901 /* load default matrixes */
1902 for (i = 0; i < 64; i++) {
1903 int j = s->idsp.idct_permutation[i];
1904 v = ff_mpeg4_default_intra_matrix[i];
1905 s->intra_matrix[j] = v;
1906 s->chroma_intra_matrix[j] = v;
1908 v = ff_mpeg4_default_non_intra_matrix[i];
1909 s->inter_matrix[j] = v;
1910 s->chroma_inter_matrix[j] = v;
1913 /* load custom intra matrix */
1914 if (get_bits1(gb)) {
1916 for (i = 0; i < 64; i++) {
1918 if (get_bits_left(gb) < 8) {
1919 av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
1920 return AVERROR_INVALIDDATA;
1922 v = get_bits(gb, 8);
1927 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1928 s->intra_matrix[j] = last;
1929 s->chroma_intra_matrix[j] = last;
1932 /* replicate last value */
1933 for (; i < 64; i++) {
1934 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1935 s->intra_matrix[j] = last;
1936 s->chroma_intra_matrix[j] = last;
1940 /* load custom non intra matrix */
1941 if (get_bits1(gb)) {
1943 for (i = 0; i < 64; i++) {
1945 if (get_bits_left(gb) < 8) {
1946 av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
1947 return AVERROR_INVALIDDATA;
1949 v = get_bits(gb, 8);
1954 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1955 s->inter_matrix[j] = v;
1956 s->chroma_inter_matrix[j] = v;
1959 /* replicate last value */
1960 for (; i < 64; i++) {
1961 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1962 s->inter_matrix[j] = last;
1963 s->chroma_inter_matrix[j] = last;
1967 // FIXME a bunch of grayscale shape things
1971 s->quarter_sample = get_bits1(gb);
1973 s->quarter_sample = 0;
1975 if (get_bits_left(gb) < 4) {
1976 av_log(s->avctx, AV_LOG_ERROR, "VOL Header truncated\n");
1977 return AVERROR_INVALIDDATA;
1980 if (!get_bits1(gb)) {
1981 int pos = get_bits_count(gb);
1982 int estimation_method = get_bits(gb, 2);
1983 if (estimation_method < 2) {
1984 if (!get_bits1(gb)) {
1985 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */
1986 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */
1987 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */
1988 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */
1989 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */
1990 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upsampling */
1992 if (!get_bits1(gb)) {
1993 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */
1994 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */
1995 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */
1996 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */
1998 if (!check_marker(s->avctx, gb, "in complexity estimation part 1")) {
1999 skip_bits_long(gb, pos - get_bits_count(gb));
2002 if (!get_bits1(gb)) {
2003 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */
2004 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */
2005 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */
2006 ctx->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */
2008 if (!get_bits1(gb)) {
2009 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */
2010 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */
2011 ctx->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */
2012 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */
2013 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */
2014 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */
2016 if (!check_marker(s->avctx, gb, "in complexity estimation part 2")) {
2017 skip_bits_long(gb, pos - get_bits_count(gb));
2020 if (estimation_method == 1) {
2021 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */
2022 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */
2025 av_log(s->avctx, AV_LOG_ERROR,
2026 "Invalid Complexity estimation method %d\n",
2031 ctx->cplx_estimation_trash_i =
2032 ctx->cplx_estimation_trash_p =
2033 ctx->cplx_estimation_trash_b = 0;
2036 ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
2038 s->data_partitioning = get_bits1(gb);
2039 if (s->data_partitioning)
2040 ctx->rvlc = get_bits1(gb);
2042 if (vo_ver_id != 1) {
2043 ctx->new_pred = get_bits1(gb);
2044 if (ctx->new_pred) {
2045 av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
2046 skip_bits(gb, 2); /* requested upstream message type */
2047 skip_bits1(gb); /* newpred segment type */
2049 if (get_bits1(gb)) // reduced_res_vop
2050 av_log(s->avctx, AV_LOG_ERROR,
2051 "reduced resolution VOP not supported\n");
2056 ctx->scalability = get_bits1(gb);
2058 if (ctx->scalability) {
2059 GetBitContext bak = *gb;
2060 int h_sampling_factor_n;
2061 int h_sampling_factor_m;
2062 int v_sampling_factor_n;
2063 int v_sampling_factor_m;
2065 skip_bits1(gb); // hierarchy_type
2066 skip_bits(gb, 4); /* ref_layer_id */
2067 skip_bits1(gb); /* ref_layer_sampling_dir */
2068 h_sampling_factor_n = get_bits(gb, 5);
2069 h_sampling_factor_m = get_bits(gb, 5);
2070 v_sampling_factor_n = get_bits(gb, 5);
2071 v_sampling_factor_m = get_bits(gb, 5);
2072 ctx->enhancement_type = get_bits1(gb);
2074 if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
2075 v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
2076 /* illegal scalability header (VERY broken encoder),
2077 * trying to workaround */
2078 ctx->scalability = 0;
2081 av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
2083 // bin shape stuff FIXME
2087 if (s->avctx->debug&FF_DEBUG_PICT_INFO) {
2088 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",
2089 s->avctx->framerate.den, s->avctx->framerate.num,
2090 ctx->time_increment_bits,
2092 s->progressive_sequence,
2094 ctx->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
2095 s->data_partitioning ? "partition " : "", ctx->rvlc ? "rvlc " : ""
2103 * Decode the user data stuff in the header.
2104 * Also initializes divx/xvid/lavc_version/build.
2106 static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
2108 MpegEncContext *s = &ctx->m;
2112 int ver = 0, build = 0, ver2 = 0, ver3 = 0;
2115 for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
2116 if (show_bits(gb, 23) == 0)
2118 buf[i] = get_bits(gb, 8);
2122 /* divx detection */
2123 e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
2125 e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
2127 ctx->divx_version = ver;
2128 ctx->divx_build = build;
2129 s->divx_packed = e == 3 && last == 'p';
2132 /* libavcodec detection */
2133 e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
2135 e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
2137 e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
2139 build = (ver << 16) + (ver2 << 8) + ver3;
2142 if (strcmp(buf, "ffmpeg") == 0)
2143 ctx->lavc_build = 4600;
2146 ctx->lavc_build = build;
2148 /* Xvid detection */
2149 e = sscanf(buf, "XviD%d", &build);
2151 ctx->xvid_build = build;
2156 int ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
2158 Mpeg4DecContext *ctx = avctx->priv_data;
2159 MpegEncContext *s = &ctx->m;
2161 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
2162 if (s->codec_tag == AV_RL32("XVID") ||
2163 s->codec_tag == AV_RL32("XVIX") ||
2164 s->codec_tag == AV_RL32("RMP4") ||
2165 s->codec_tag == AV_RL32("ZMP4") ||
2166 s->codec_tag == AV_RL32("SIPP"))
2167 ctx->xvid_build = 0;
2170 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
2171 if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
2172 ctx->vol_control_parameters == 0)
2173 ctx->divx_version = 400; // divx 4
2175 if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
2177 ctx->divx_build = -1;
2180 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
2181 if (s->codec_tag == AV_RL32("XVIX"))
2182 s->workaround_bugs |= FF_BUG_XVID_ILACE;
2184 if (s->codec_tag == AV_RL32("UMP4"))
2185 s->workaround_bugs |= FF_BUG_UMP4;
2187 if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
2188 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2190 if (ctx->divx_version > 502 && ctx->divx_build < 1814)
2191 s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
2193 if (ctx->xvid_build <= 3U)
2194 s->padding_bug_score = 256 * 256 * 256 * 64;
2196 if (ctx->xvid_build <= 1U)
2197 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2199 if (ctx->xvid_build <= 12U)
2200 s->workaround_bugs |= FF_BUG_EDGE;
2202 if (ctx->xvid_build <= 32U)
2203 s->workaround_bugs |= FF_BUG_DC_CLIP;
2205 #define SET_QPEL_FUNC(postfix1, postfix2) \
2206 s->qdsp.put_ ## postfix1 = ff_put_ ## postfix2; \
2207 s->qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
2208 s->qdsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
2210 if (ctx->lavc_build < 4653U)
2211 s->workaround_bugs |= FF_BUG_STD_QPEL;
2213 if (ctx->lavc_build < 4655U)
2214 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2216 if (ctx->lavc_build < 4670U)
2217 s->workaround_bugs |= FF_BUG_EDGE;
2219 if (ctx->lavc_build <= 4712U)
2220 s->workaround_bugs |= FF_BUG_DC_CLIP;
2222 if ((ctx->lavc_build&0xFF) >= 100) {
2223 if (ctx->lavc_build > 3621476 && ctx->lavc_build < 3752552 &&
2224 (ctx->lavc_build < 3752037 || ctx->lavc_build > 3752191) // 3.2.1+
2226 s->workaround_bugs |= FF_BUG_IEDGE;
2229 if (ctx->divx_version >= 0)
2230 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2231 if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
2232 s->padding_bug_score = 256 * 256 * 256 * 64;
2234 if (ctx->divx_version < 500U)
2235 s->workaround_bugs |= FF_BUG_EDGE;
2237 if (ctx->divx_version >= 0)
2238 s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
2241 if (s->workaround_bugs & FF_BUG_STD_QPEL) {
2242 SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
2243 SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
2244 SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
2245 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
2246 SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
2247 SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
2249 SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
2250 SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
2251 SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
2252 SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
2253 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
2254 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
2257 if (avctx->debug & FF_DEBUG_BUGS)
2258 av_log(s->avctx, AV_LOG_DEBUG,
2259 "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
2260 s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
2261 ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
2263 if (CONFIG_MPEG4_DECODER && ctx->xvid_build >= 0 &&
2264 s->codec_id == AV_CODEC_ID_MPEG4 &&
2265 avctx->idct_algo == FF_IDCT_AUTO) {
2266 avctx->idct_algo = FF_IDCT_XVID;
2267 ff_mpv_idct_init(s);
2274 static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2276 MpegEncContext *s = &ctx->m;
2277 int time_incr, time_increment;
2280 s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
2281 if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
2282 ctx->vol_control_parameters == 0 && !(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)) {
2283 av_log(s->avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n");
2287 s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
2288 if (s->partitioned_frame)
2289 s->decode_mb = mpeg4_decode_partitioned_mb;
2291 s->decode_mb = mpeg4_decode_mb;
2294 while (get_bits1(gb) != 0)
2297 check_marker(s->avctx, gb, "before time_increment");
2299 if (ctx->time_increment_bits == 0 ||
2300 !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
2301 av_log(s->avctx, AV_LOG_WARNING,
2302 "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);
2304 for (ctx->time_increment_bits = 1;
2305 ctx->time_increment_bits < 16;
2306 ctx->time_increment_bits++) {
2307 if (s->pict_type == AV_PICTURE_TYPE_P ||
2308 (s->pict_type == AV_PICTURE_TYPE_S &&
2309 ctx->vol_sprite_usage == GMC_SPRITE)) {
2310 if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
2312 } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
2316 av_log(s->avctx, AV_LOG_WARNING,
2317 "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits);
2318 if (s->avctx->framerate.num && 4*s->avctx->framerate.num < 1<<ctx->time_increment_bits) {
2319 s->avctx->framerate.num = 1<<ctx->time_increment_bits;
2320 s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
2325 time_increment = get_bits1(gb); // FIXME investigate further
2327 time_increment = get_bits(gb, ctx->time_increment_bits);
2329 if (s->pict_type != AV_PICTURE_TYPE_B) {
2330 s->last_time_base = s->time_base;
2331 s->time_base += time_incr;
2332 s->time = s->time_base * s->avctx->framerate.num + time_increment;
2333 if (s->workaround_bugs & FF_BUG_UMP4) {
2334 if (s->time < s->last_non_b_time) {
2335 /* header is not mpeg-4-compatible, broken encoder,
2336 * trying to workaround */
2338 s->time += s->avctx->framerate.num;
2341 s->pp_time = s->time - s->last_non_b_time;
2342 s->last_non_b_time = s->time;
2344 s->time = (s->last_time_base + time_incr) * s->avctx->framerate.num + time_increment;
2345 s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
2346 if (s->pp_time <= s->pb_time ||
2347 s->pp_time <= s->pp_time - s->pb_time ||
2349 /* messed up order, maybe after seeking? skipping current B-frame */
2350 return FRAME_SKIPPED;
2352 ff_mpeg4_init_direct_mv(s);
2354 if (ctx->t_frame == 0)
2355 ctx->t_frame = s->pb_time;
2356 if (ctx->t_frame == 0)
2357 ctx->t_frame = 1; // 1/0 protection
2358 s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
2359 ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2360 s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
2361 ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2362 if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1) {
2363 s->pb_field_time = 2;
2364 s->pp_field_time = 4;
2365 if (!s->progressive_sequence)
2366 return FRAME_SKIPPED;
2370 if (s->avctx->framerate.den)
2371 pts = ROUNDED_DIV(s->time, s->avctx->framerate.den);
2373 pts = AV_NOPTS_VALUE;
2374 ff_dlog(s->avctx, "MPEG4 PTS: %"PRId64"\n", pts);
2376 check_marker(s->avctx, gb, "before vop_coded");
2379 if (get_bits1(gb) != 1) {
2380 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2381 av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
2382 return FRAME_SKIPPED;
2385 decode_new_pred(ctx, gb);
2387 if (ctx->shape != BIN_ONLY_SHAPE &&
2388 (s->pict_type == AV_PICTURE_TYPE_P ||
2389 (s->pict_type == AV_PICTURE_TYPE_S &&
2390 ctx->vol_sprite_usage == GMC_SPRITE))) {
2391 /* rounding type for motion estimation */
2392 s->no_rounding = get_bits1(gb);
2396 // FIXME reduced res stuff
2398 if (ctx->shape != RECT_SHAPE) {
2399 if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
2400 skip_bits(gb, 13); /* width */
2401 check_marker(s->avctx, gb, "after width");
2402 skip_bits(gb, 13); /* height */
2403 check_marker(s->avctx, gb, "after height");
2404 skip_bits(gb, 13); /* hor_spat_ref */
2405 check_marker(s->avctx, gb, "after hor_spat_ref");
2406 skip_bits(gb, 13); /* ver_spat_ref */
2408 skip_bits1(gb); /* change_CR_disable */
2410 if (get_bits1(gb) != 0)
2411 skip_bits(gb, 8); /* constant_alpha_value */
2414 // FIXME complexity estimation stuff
2416 if (ctx->shape != BIN_ONLY_SHAPE) {
2417 skip_bits_long(gb, ctx->cplx_estimation_trash_i);
2418 if (s->pict_type != AV_PICTURE_TYPE_I)
2419 skip_bits_long(gb, ctx->cplx_estimation_trash_p);
2420 if (s->pict_type == AV_PICTURE_TYPE_B)
2421 skip_bits_long(gb, ctx->cplx_estimation_trash_b);
2423 if (get_bits_left(gb) < 3) {
2424 av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
2425 return AVERROR_INVALIDDATA;
2427 ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
2428 if (!s->progressive_sequence) {
2429 s->top_field_first = get_bits1(gb);
2430 s->alternate_scan = get_bits1(gb);
2432 s->alternate_scan = 0;
2435 if (s->alternate_scan) {
2436 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
2437 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
2438 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
2439 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2441 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
2442 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
2443 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
2444 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2447 if (s->pict_type == AV_PICTURE_TYPE_S &&
2448 (ctx->vol_sprite_usage == STATIC_SPRITE ||
2449 ctx->vol_sprite_usage == GMC_SPRITE)) {
2450 if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
2451 return AVERROR_INVALIDDATA;
2452 if (ctx->sprite_brightness_change)
2453 av_log(s->avctx, AV_LOG_ERROR,
2454 "sprite_brightness_change not supported\n");
2455 if (ctx->vol_sprite_usage == STATIC_SPRITE)
2456 av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
2459 if (ctx->shape != BIN_ONLY_SHAPE) {
2460 s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision);
2461 if (s->qscale == 0) {
2462 av_log(s->avctx, AV_LOG_ERROR,
2463 "Error, header damaged or not MPEG-4 header (qscale=0)\n");
2464 return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
2467 if (s->pict_type != AV_PICTURE_TYPE_I) {
2468 s->f_code = get_bits(gb, 3); /* fcode_for */
2469 if (s->f_code == 0) {
2470 av_log(s->avctx, AV_LOG_ERROR,
2471 "Error, header damaged or not MPEG-4 header (f_code=0)\n");
2473 return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
2478 if (s->pict_type == AV_PICTURE_TYPE_B) {
2479 s->b_code = get_bits(gb, 3);
2480 if (s->b_code == 0) {
2481 av_log(s->avctx, AV_LOG_ERROR,
2482 "Error, header damaged or not MPEG4 header (b_code=0)\n");
2484 return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly
2489 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2490 av_log(s->avctx, AV_LOG_DEBUG,
2491 "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",
2492 s->qscale, s->f_code, s->b_code,
2493 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")),
2494 gb->size_in_bits,s->progressive_sequence, s->alternate_scan,
2495 s->top_field_first, s->quarter_sample ? "q" : "h",
2496 s->data_partitioning, ctx->resync_marker,
2497 ctx->num_sprite_warping_points, s->sprite_warping_accuracy,
2498 1 - s->no_rounding, s->vo_type,
2499 ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
2500 ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
2501 ctx->cplx_estimation_trash_b,
2507 if (!ctx->scalability) {
2508 if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
2509 skip_bits1(gb); // vop shape coding type
2511 if (ctx->enhancement_type) {
2512 int load_backward_shape = get_bits1(gb);
2513 if (load_backward_shape)
2514 av_log(s->avctx, AV_LOG_ERROR,
2515 "load backward shape isn't supported\n");
2517 skip_bits(gb, 2); // ref_select_code
2520 /* detect buggy encoders which don't set the low_delay flag
2521 * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames
2522 * easily (although it's buggy too) */
2523 if (s->vo_type == 0 && ctx->vol_control_parameters == 0 &&
2524 ctx->divx_version == -1 && s->picture_number == 0) {
2525 av_log(s->avctx, AV_LOG_WARNING,
2526 "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
2530 s->picture_number++; // better than pic number==0 always ;)
2532 // FIXME add short header support
2533 s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
2534 s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
2536 if (s->workaround_bugs & FF_BUG_EDGE) {
2537 s->h_edge_pos = s->width;
2538 s->v_edge_pos = s->height;
2544 * Decode MPEG-4 headers.
2545 * @return <0 if no VOP found (or a damaged one)
2546 * FRAME_SKIPPED if a not coded VOP is found
2547 * 0 if a VOP is found
2549 int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2551 MpegEncContext *s = &ctx->m;
2552 unsigned startcode, v;
2555 /* search next start code */
2558 if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
2560 if (get_bits(gb, 8) == 0xF0)
2566 if (get_bits_count(gb) >= gb->size_in_bits) {
2567 if (gb->size_in_bits == 8 &&
2568 (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
2569 av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
2570 return FRAME_SKIPPED; // divx bug
2572 return -1; // end of stream
2575 /* use the bits after the test */
2576 v = get_bits(gb, 8);
2577 startcode = ((startcode << 8) | v) & 0xffffffff;
2579 if ((startcode & 0xFFFFFF00) != 0x100)
2580 continue; // no startcode
2582 if (s->avctx->debug & FF_DEBUG_STARTCODE) {
2583 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
2584 if (startcode <= 0x11F)
2585 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
2586 else if (startcode <= 0x12F)
2587 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
2588 else if (startcode <= 0x13F)
2589 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2590 else if (startcode <= 0x15F)
2591 av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
2592 else if (startcode <= 0x1AF)
2593 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2594 else if (startcode == 0x1B0)
2595 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
2596 else if (startcode == 0x1B1)
2597 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
2598 else if (startcode == 0x1B2)
2599 av_log(s->avctx, AV_LOG_DEBUG, "User Data");
2600 else if (startcode == 0x1B3)
2601 av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
2602 else if (startcode == 0x1B4)
2603 av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
2604 else if (startcode == 0x1B5)
2605 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
2606 else if (startcode == 0x1B6)
2607 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
2608 else if (startcode == 0x1B7)
2609 av_log(s->avctx, AV_LOG_DEBUG, "slice start");
2610 else if (startcode == 0x1B8)
2611 av_log(s->avctx, AV_LOG_DEBUG, "extension start");
2612 else if (startcode == 0x1B9)
2613 av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
2614 else if (startcode == 0x1BA)
2615 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
2616 else if (startcode == 0x1BB)
2617 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
2618 else if (startcode == 0x1BC)
2619 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
2620 else if (startcode == 0x1BD)
2621 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
2622 else if (startcode == 0x1BE)
2623 av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
2624 else if (startcode == 0x1BF)
2625 av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
2626 else if (startcode == 0x1C0)
2627 av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
2628 else if (startcode == 0x1C1)
2629 av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
2630 else if (startcode == 0x1C2)
2631 av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
2632 else if (startcode == 0x1C3)
2633 av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
2634 else if (startcode <= 0x1C5)
2635 av_log(s->avctx, AV_LOG_DEBUG, "reserved");
2636 else if (startcode <= 0x1FF)
2637 av_log(s->avctx, AV_LOG_DEBUG, "System start");
2638 av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
2641 if (startcode >= 0x120 && startcode <= 0x12F) {
2642 if ((ret = decode_vol_header(ctx, gb)) < 0)
2644 } else if (startcode == USER_DATA_STARTCODE) {
2645 decode_user_data(ctx, gb);
2646 } else if (startcode == GOP_STARTCODE) {
2647 mpeg4_decode_gop_header(s, gb);
2648 } else if (startcode == VOS_STARTCODE) {
2649 mpeg4_decode_profile_level(s, gb);
2650 } else if (startcode == VOP_STARTCODE) {
2659 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
2661 s->avctx->has_b_frames = !s->low_delay;
2663 return decode_vop_header(ctx, gb);
2666 av_cold void ff_mpeg4videodec_static_init(void) {
2667 static int done = 0;
2670 ff_rl_init(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
2671 ff_rl_init(&ff_rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
2672 ff_rl_init(&ff_rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
2673 INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
2674 INIT_VLC_RL(ff_rvlc_rl_inter, 1072);
2675 INIT_VLC_RL(ff_rvlc_rl_intra, 1072);
2676 INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
2677 &ff_mpeg4_DCtab_lum[0][1], 2, 1,
2678 &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
2679 INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
2680 &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
2681 &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
2682 INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
2683 &ff_sprite_trajectory_tab[0][1], 4, 2,
2684 &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
2685 INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
2686 &ff_mb_type_b_tab[0][1], 2, 1,
2687 &ff_mb_type_b_tab[0][0], 2, 1, 16);
2692 int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
2694 Mpeg4DecContext *ctx = avctx->priv_data;
2695 MpegEncContext *s = &ctx->m;
2697 /* divx 5.01+ bitstream reorder stuff */
2698 /* Since this clobbers the input buffer and hwaccel codecs still need the
2699 * data during hwaccel->end_frame we should not do this any earlier */
2700 if (s->divx_packed) {
2701 int current_pos = s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb) >> 3);
2702 int startcode_found = 0;
2704 if (buf_size - current_pos > 7) {
2707 for (i = current_pos; i < buf_size - 4; i++)
2712 buf[i + 3] == 0xB6) {
2713 startcode_found = !(buf[i + 4] & 0x40);
2718 if (startcode_found) {
2719 if (!ctx->showed_packed_warning) {
2720 av_log(s->avctx, AV_LOG_INFO, "Video uses a non-standard and "
2721 "wasteful way to store B-frames ('packed B-frames'). "
2722 "Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.\n");
2723 ctx->showed_packed_warning = 1;
2725 av_fast_padded_malloc(&s->bitstream_buffer,
2726 &s->allocated_bitstream_buffer_size,
2727 buf_size - current_pos);
2728 if (!s->bitstream_buffer) {
2729 s->bitstream_buffer_size = 0;
2730 return AVERROR(ENOMEM);
2732 memcpy(s->bitstream_buffer, buf + current_pos,
2733 buf_size - current_pos);
2734 s->bitstream_buffer_size = buf_size - current_pos;
2742 static int mpeg4_update_thread_context(AVCodecContext *dst,
2743 const AVCodecContext *src)
2745 Mpeg4DecContext *s = dst->priv_data;
2746 const Mpeg4DecContext *s1 = src->priv_data;
2747 int init = s->m.context_initialized;
2749 int ret = ff_mpeg_update_thread_context(dst, src);
2754 memcpy(((uint8_t*)s) + sizeof(MpegEncContext), ((uint8_t*)s1) + sizeof(MpegEncContext), sizeof(Mpeg4DecContext) - sizeof(MpegEncContext));
2756 if (CONFIG_MPEG4_DECODER && !init && s1->xvid_build >= 0)
2757 ff_xvid_idct_init(&s->m.idsp, dst);
2763 static av_cold int decode_init(AVCodecContext *avctx)
2765 Mpeg4DecContext *ctx = avctx->priv_data;
2766 MpegEncContext *s = &ctx->m;
2772 ctx->lavc_build = -1;
2774 if ((ret = ff_h263_decode_init(avctx)) < 0)
2777 ff_mpeg4videodec_static_init();
2780 s->low_delay = 0; /* default, might be overridden in the vol header during header parsing */
2781 s->decode_mb = mpeg4_decode_mb;
2782 ctx->time_increment_bits = 4; /* default value for broken headers */
2784 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
2785 avctx->internal->allocate_progress = 1;
2790 static const AVOption mpeg4_options[] = {
2791 {"quarter_sample", "1/4 subpel MC", offsetof(MpegEncContext, quarter_sample), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
2792 {"divx_packed", "divx style packed b frames", offsetof(MpegEncContext, divx_packed), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
2796 static const AVClass mpeg4_class = {
2797 "MPEG4 Video Decoder",
2798 av_default_item_name,
2800 LIBAVUTIL_VERSION_INT,
2803 AVCodec ff_mpeg4_decoder = {
2805 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
2806 .type = AVMEDIA_TYPE_VIDEO,
2807 .id = AV_CODEC_ID_MPEG4,
2808 .priv_data_size = sizeof(Mpeg4DecContext),
2809 .init = decode_init,
2810 .close = ff_h263_decode_end,
2811 .decode = ff_h263_decode_frame,
2812 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2813 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
2814 AV_CODEC_CAP_FRAME_THREADS,
2815 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2816 .flush = ff_mpeg_flush,
2818 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
2819 .profiles = NULL_IF_CONFIG_SMALL(ff_mpeg4_video_profiles),
2820 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg4_update_thread_context),
2821 .priv_class = &mpeg4_class,
2825 #if CONFIG_MPEG4_VDPAU_DECODER && FF_API_VDPAU
2826 static const AVClass mpeg4_vdpau_class = {
2827 "MPEG4 Video VDPAU Decoder",
2828 av_default_item_name,
2830 LIBAVUTIL_VERSION_INT,
2833 AVCodec ff_mpeg4_vdpau_decoder = {
2834 .name = "mpeg4_vdpau",
2835 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 (VDPAU)"),
2836 .type = AVMEDIA_TYPE_VIDEO,
2837 .id = AV_CODEC_ID_MPEG4,
2838 .priv_data_size = sizeof(Mpeg4DecContext),
2839 .init = decode_init,
2840 .close = ff_h263_decode_end,
2841 .decode = ff_h263_decode_frame,
2842 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
2843 AV_CODEC_CAP_HWACCEL_VDPAU,
2844 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_MPEG4,
2846 .priv_class = &mpeg4_vdpau_class,