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/opt.h"
26 #include "error_resilience.h"
28 #include "mpegvideo.h"
29 #include "mpeg4video.h"
33 /* The defines below define the number of bits that are read at once for
34 * reading vlc values. Changing these may improve speed and data cache needs
35 * be aware though that decreasing them may need the number of stages that is
36 * passed to get_vlc* to be increased. */
37 #define SPRITE_TRAJ_VLC_BITS 6
39 #define MB_TYPE_B_VLC_BITS 4
41 static VLC dc_lum, dc_chrom;
42 static VLC sprite_trajectory;
43 static VLC mb_type_b_vlc;
45 static const int mb_type_b_map[4] = {
46 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
47 MB_TYPE_L0L1 | MB_TYPE_16x16,
48 MB_TYPE_L1 | MB_TYPE_16x16,
49 MB_TYPE_L0 | MB_TYPE_16x16,
54 * @param n block index (0-3 are luma, 4-5 are chroma)
55 * @param dir the ac prediction direction
57 void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
60 int16_t *ac_val, *ac_val1;
61 int8_t *const qscale_table = s->current_picture.qscale_table;
64 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
68 const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
72 if (s->mb_x == 0 || s->qscale == qscale_table[xy] ||
75 for (i = 1; i < 8; i++)
76 block[s->dsp.idct_permutation[i << 3]] += ac_val[i];
78 /* different qscale, we must rescale */
79 for (i = 1; i < 8; i++)
80 block[s->dsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
83 const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
85 ac_val -= 16 * s->block_wrap[n];
87 if (s->mb_y == 0 || s->qscale == qscale_table[xy] ||
90 for (i = 1; i < 8; i++)
91 block[s->dsp.idct_permutation[i]] += ac_val[i + 8];
93 /* different qscale, we must rescale */
94 for (i = 1; i < 8; i++)
95 block[s->dsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
100 for (i = 1; i < 8; i++)
101 ac_val1[i] = block[s->dsp.idct_permutation[i << 3]];
104 for (i = 1; i < 8; i++)
105 ac_val1[8 + i] = block[s->dsp.idct_permutation[i]];
109 * check if the next stuff is a resync marker or the end.
112 static inline int mpeg4_is_resync(Mpeg4DecContext *ctx)
114 MpegEncContext *s = &ctx->m;
115 int bits_count = get_bits_count(&s->gb);
116 int v = show_bits(&s->gb, 16);
118 if (s->workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker)
122 if (s->pict_type == AV_PICTURE_TYPE_B ||
123 (v >> (8 - s->pict_type) != 1) || s->partitioned_frame)
125 skip_bits(&s->gb, 8 + s->pict_type);
126 bits_count += 8 + s->pict_type;
127 v = show_bits(&s->gb, 16);
130 if (bits_count + 8 >= s->gb.size_in_bits) {
132 v |= 0x7F >> (7 - (bits_count & 7));
137 if (v == ff_mpeg4_resync_prefix[bits_count & 7]) {
139 int mb_num_bits = av_log2(s->mb_num - 1) + 1;
140 GetBitContext gb = s->gb;
142 skip_bits(&s->gb, 1);
143 align_get_bits(&s->gb);
145 for (len = 0; len < 32; len++)
146 if (get_bits1(&s->gb))
149 mb_num = get_bits(&s->gb, mb_num_bits);
150 if (!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
155 if (len >= ff_mpeg4_get_video_packet_prefix_length(s))
162 static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
164 MpegEncContext *s = &ctx->m;
165 int a = 2 << s->sprite_warping_accuracy;
166 int rho = 3 - s->sprite_warping_accuracy;
172 int min_ab, i, w2, h2, w3, h3;
173 int sprite_ref[4][2];
174 int virtual_ref[2][2];
176 // only true for rectangle shapes
177 const int vop_ref[4][2] = { { 0, 0 }, { s->width, 0 },
178 { 0, s->height }, { s->width, s->height } };
179 int d[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
181 if (w <= 0 || h <= 0)
182 return AVERROR_INVALIDDATA;
184 for (i = 0; i < ctx->num_sprite_warping_points; i++) {
188 length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
190 x = get_xbits(gb, length);
192 if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
193 skip_bits1(gb); /* marker bit */
195 length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
197 y = get_xbits(gb, length);
199 skip_bits1(gb); /* marker bit */
200 ctx->sprite_traj[i][0] = d[i][0] = x;
201 ctx->sprite_traj[i][1] = d[i][1] = y;
204 ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0;
206 while ((1 << alpha) < w)
208 while ((1 << beta) < h)
209 beta++; /* typo in the mpeg4 std for the definition of w' and h' */
213 // Note, the 4th point isn't used for GMC
214 if (ctx->divx_version == 500 && ctx->divx_build == 413) {
215 sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
216 sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
217 sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
218 sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1];
219 sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0];
220 sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1];
222 sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]);
223 sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]);
224 sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]);
225 sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]);
226 sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]);
227 sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]);
229 /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
230 * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
232 /* this is mostly identical to the mpeg4 std (and is totally unreadable
233 * because of that...). Perhaps it should be reordered to be more readable.
234 * The idea behind this virtual_ref mess is to be able to use shifts later
235 * per pixel instead of divides so the distance between points is converted
236 * from w&h based to w2&h2 based which are of the 2^x form. */
237 virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) +
238 ROUNDED_DIV(((w - w2) *
239 (r * sprite_ref[0][0] - 16 * vop_ref[0][0]) +
240 w2 * (r * sprite_ref[1][0] - 16 * vop_ref[1][0])), w);
241 virtual_ref[0][1] = 16 * vop_ref[0][1] +
242 ROUNDED_DIV(((w - w2) *
243 (r * sprite_ref[0][1] - 16 * vop_ref[0][1]) +
244 w2 * (r * sprite_ref[1][1] - 16 * vop_ref[1][1])), w);
245 virtual_ref[1][0] = 16 * vop_ref[0][0] +
246 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16 * vop_ref[0][0]) +
247 h2 * (r * sprite_ref[2][0] - 16 * vop_ref[2][0])), h);
248 virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) +
249 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16 * vop_ref[0][1]) +
250 h2 * (r * sprite_ref[2][1] - 16 * vop_ref[2][1])), h);
252 switch (ctx->num_sprite_warping_points) {
254 s->sprite_offset[0][0] =
255 s->sprite_offset[0][1] =
256 s->sprite_offset[1][0] =
257 s->sprite_offset[1][1] = 0;
258 s->sprite_delta[0][0] = a;
259 s->sprite_delta[0][1] =
260 s->sprite_delta[1][0] = 0;
261 s->sprite_delta[1][1] = a;
262 ctx->sprite_shift[0] =
263 ctx->sprite_shift[1] = 0;
266 s->sprite_offset[0][0] = sprite_ref[0][0] - a * vop_ref[0][0];
267 s->sprite_offset[0][1] = sprite_ref[0][1] - a * vop_ref[0][1];
268 s->sprite_offset[1][0] = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) -
269 a * (vop_ref[0][0] / 2);
270 s->sprite_offset[1][1] = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) -
271 a * (vop_ref[0][1] / 2);
272 s->sprite_delta[0][0] = a;
273 s->sprite_delta[0][1] =
274 s->sprite_delta[1][0] = 0;
275 s->sprite_delta[1][1] = a;
276 ctx->sprite_shift[0] =
277 ctx->sprite_shift[1] = 0;
280 s->sprite_offset[0][0] = (sprite_ref[0][0] << (alpha + rho)) +
281 (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
283 (r * sprite_ref[0][1] - virtual_ref[0][1]) *
284 (-vop_ref[0][1]) + (1 << (alpha + rho - 1));
285 s->sprite_offset[0][1] = (sprite_ref[0][1] << (alpha + rho)) +
286 (-r * sprite_ref[0][1] + virtual_ref[0][1]) *
288 (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
289 (-vop_ref[0][1]) + (1 << (alpha + rho - 1));
290 s->sprite_offset[1][0] = ((-r * sprite_ref[0][0] + virtual_ref[0][0]) *
291 (-2 * vop_ref[0][0] + 1) +
292 (r * sprite_ref[0][1] - virtual_ref[0][1]) *
293 (-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
294 sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1)));
295 s->sprite_offset[1][1] = ((-r * sprite_ref[0][1] + virtual_ref[0][1]) *
296 (-2 * vop_ref[0][0] + 1) +
297 (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
298 (-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
299 sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1)));
300 s->sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
301 s->sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]);
302 s->sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
303 s->sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
305 ctx->sprite_shift[0] = alpha + rho;
306 ctx->sprite_shift[1] = alpha + rho + 2;
309 min_ab = FFMIN(alpha, beta);
312 s->sprite_offset[0][0] = (sprite_ref[0][0] << (alpha + beta + rho - min_ab)) +
313 (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
314 h3 * (-vop_ref[0][0]) +
315 (-r * sprite_ref[0][0] + virtual_ref[1][0]) *
316 w3 * (-vop_ref[0][1]) +
317 (1 << (alpha + beta + rho - min_ab - 1));
318 s->sprite_offset[0][1] = (sprite_ref[0][1] << (alpha + beta + rho - min_ab)) +
319 (-r * sprite_ref[0][1] + virtual_ref[0][1]) *
320 h3 * (-vop_ref[0][0]) +
321 (-r * sprite_ref[0][1] + virtual_ref[1][1]) *
322 w3 * (-vop_ref[0][1]) +
323 (1 << (alpha + beta + rho - min_ab - 1));
324 s->sprite_offset[1][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
325 h3 * (-2 * vop_ref[0][0] + 1) +
326 (-r * sprite_ref[0][0] + virtual_ref[1][0]) *
327 w3 * (-2 * vop_ref[0][1] + 1) + 2 * w2 * h3 *
328 r * sprite_ref[0][0] - 16 * w2 * h3 +
329 (1 << (alpha + beta + rho - min_ab + 1));
330 s->sprite_offset[1][1] = (-r * sprite_ref[0][1] + virtual_ref[0][1]) *
331 h3 * (-2 * vop_ref[0][0] + 1) +
332 (-r * sprite_ref[0][1] + virtual_ref[1][1]) *
333 w3 * (-2 * vop_ref[0][1] + 1) + 2 * w2 * h3 *
334 r * sprite_ref[0][1] - 16 * w2 * h3 +
335 (1 << (alpha + beta + rho - min_ab + 1));
336 s->sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3;
337 s->sprite_delta[0][1] = (-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3;
338 s->sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3;
339 s->sprite_delta[1][1] = (-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3;
341 ctx->sprite_shift[0] = alpha + beta + rho - min_ab;
342 ctx->sprite_shift[1] = alpha + beta + rho - min_ab + 2;
345 /* try to simplify the situation */
346 if (s->sprite_delta[0][0] == a << ctx->sprite_shift[0] &&
347 s->sprite_delta[0][1] == 0 &&
348 s->sprite_delta[1][0] == 0 &&
349 s->sprite_delta[1][1] == a << ctx->sprite_shift[0]) {
350 s->sprite_offset[0][0] >>= ctx->sprite_shift[0];
351 s->sprite_offset[0][1] >>= ctx->sprite_shift[0];
352 s->sprite_offset[1][0] >>= ctx->sprite_shift[1];
353 s->sprite_offset[1][1] >>= ctx->sprite_shift[1];
354 s->sprite_delta[0][0] = a;
355 s->sprite_delta[0][1] = 0;
356 s->sprite_delta[1][0] = 0;
357 s->sprite_delta[1][1] = a;
358 ctx->sprite_shift[0] = 0;
359 ctx->sprite_shift[1] = 0;
360 s->real_sprite_warping_points = 1;
362 int shift_y = 16 - ctx->sprite_shift[0];
363 int shift_c = 16 - ctx->sprite_shift[1];
364 for (i = 0; i < 2; i++) {
365 s->sprite_offset[0][i] <<= shift_y;
366 s->sprite_offset[1][i] <<= shift_c;
367 s->sprite_delta[0][i] <<= shift_y;
368 s->sprite_delta[1][i] <<= shift_y;
369 ctx->sprite_shift[i] = 16;
371 s->real_sprite_warping_points = ctx->num_sprite_warping_points;
377 static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb) {
378 int len = FFMIN(ctx->time_increment_bits + 3, 15);
383 check_marker(gb, "after new_pred");
389 * Decode the next video packet.
390 * @return <0 if something went wrong
392 int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
394 MpegEncContext *s = &ctx->m;
396 int mb_num_bits = av_log2(s->mb_num - 1) + 1;
397 int header_extension = 0, mb_num, len;
399 /* is there enough space left for a video packet + header */
400 if (get_bits_count(&s->gb) > s->gb.size_in_bits - 20)
403 for (len = 0; len < 32; len++)
404 if (get_bits1(&s->gb))
407 if (len != ff_mpeg4_get_video_packet_prefix_length(s)) {
408 av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
412 if (ctx->shape != RECT_SHAPE) {
413 header_extension = get_bits1(&s->gb);
414 // FIXME more stuff here
417 mb_num = get_bits(&s->gb, mb_num_bits);
418 if (mb_num >= s->mb_num) {
419 av_log(s->avctx, AV_LOG_ERROR,
420 "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
424 s->mb_x = mb_num % s->mb_width;
425 s->mb_y = mb_num / s->mb_width;
427 if (ctx->shape != BIN_ONLY_SHAPE) {
428 int qscale = get_bits(&s->gb, s->quant_precision);
430 s->chroma_qscale = s->qscale = qscale;
433 if (ctx->shape == RECT_SHAPE)
434 header_extension = get_bits1(&s->gb);
436 if (header_extension) {
439 while (get_bits1(&s->gb) != 0)
442 check_marker(&s->gb, "before time_increment in video packed header");
443 skip_bits(&s->gb, ctx->time_increment_bits); /* time_increment */
444 check_marker(&s->gb, "before vop_coding_type in video packed header");
446 skip_bits(&s->gb, 2); /* vop coding type */
447 // FIXME not rect stuff here
449 if (ctx->shape != BIN_ONLY_SHAPE) {
450 skip_bits(&s->gb, 3); /* intra dc vlc threshold */
451 // FIXME don't just ignore everything
452 if (s->pict_type == AV_PICTURE_TYPE_S &&
453 ctx->vol_sprite_usage == GMC_SPRITE) {
454 if (mpeg4_decode_sprite_trajectory(ctx, &s->gb) < 0)
455 return AVERROR_INVALIDDATA;
456 av_log(s->avctx, AV_LOG_ERROR, "untested\n");
459 // FIXME reduced res stuff here
461 if (s->pict_type != AV_PICTURE_TYPE_I) {
462 int f_code = get_bits(&s->gb, 3); /* fcode_for */
464 av_log(s->avctx, AV_LOG_ERROR,
465 "Error, video packet header damaged (f_code=0)\n");
467 if (s->pict_type == AV_PICTURE_TYPE_B) {
468 int b_code = get_bits(&s->gb, 3);
470 av_log(s->avctx, AV_LOG_ERROR,
471 "Error, video packet header damaged (b_code=0)\n");
476 decode_new_pred(ctx, &s->gb);
482 * Get the average motion vector for a GMC MB.
483 * @param n either 0 for the x component or 1 for y
484 * @return the average MV for a GMC MB
486 static inline int get_amv(Mpeg4DecContext *ctx, int n)
488 MpegEncContext *s = &ctx->m;
489 int x, y, mb_v, sum, dx, dy, shift;
490 int len = 1 << (s->f_code + 4);
491 const int a = s->sprite_warping_accuracy;
493 if (s->workaround_bugs & FF_BUG_AMV)
494 len >>= s->quarter_sample;
496 if (s->real_sprite_warping_points == 1) {
497 if (ctx->divx_version == 500 && ctx->divx_build == 413)
498 sum = s->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
500 sum = RSHIFT(s->sprite_offset[0][n] << s->quarter_sample, a);
502 dx = s->sprite_delta[n][0];
503 dy = s->sprite_delta[n][1];
504 shift = ctx->sprite_shift[0];
506 dy -= 1 << (shift + a + 1);
508 dx -= 1 << (shift + a + 1);
509 mb_v = s->sprite_offset[0][n] + dx * s->mb_x * 16 + dy * s->mb_y * 16;
512 for (y = 0; y < 16; y++) {
517 for (x = 0; x < 16; x++) {
522 sum = RSHIFT(sum, a + 8 - s->quarter_sample);
534 * Decode the dc value.
535 * @param n block index (0-3 are luma, 4-5 are chroma)
536 * @param dir_ptr the prediction direction will be stored here
537 * @return the quantized dc
539 static inline int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
544 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
546 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
548 if (code < 0 || code > 9 /* && s->nbit < 9 */) {
549 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
558 level = 2 * get_bits1(&s->gb) - 1;
560 if (get_bits1(&s->gb))
561 level = get_bits(&s->gb, code - 1) + (1 << (code - 1));
563 level = -get_bits(&s->gb, code - 1) - (1 << (code - 1));
566 level = get_xbits(&s->gb, code);
570 if (get_bits1(&s->gb) == 0) { /* marker */
571 if (s->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)) {
572 av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
579 return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
583 * Decode first partition.
584 * @return number of MBs decoded or <0 if an error occurred
586 static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
588 MpegEncContext *s = &ctx->m;
590 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
592 /* decode first partition */
593 s->first_slice_line = 1;
594 for (; s->mb_y < s->mb_height; s->mb_y++) {
595 ff_init_block_index(s);
596 for (; s->mb_x < s->mb_width; s->mb_x++) {
597 const int xy = s->mb_x + s->mb_y * s->mb_stride;
602 ff_update_block_index(s);
603 if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
604 s->first_slice_line = 0;
606 if (s->pict_type == AV_PICTURE_TYPE_I) {
610 if (show_bits_long(&s->gb, 19) == DC_MARKER)
613 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
615 av_log(s->avctx, AV_LOG_ERROR,
616 "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
621 s->cbp_table[xy] = cbpc & 3;
622 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
626 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
628 s->current_picture.qscale_table[xy] = s->qscale;
630 s->mbintra_table[xy] = 1;
631 for (i = 0; i < 6; i++) {
633 int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
635 av_log(s->avctx, AV_LOG_ERROR,
636 "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
643 s->pred_dir_table[xy] = dir;
644 } else { /* P/S_TYPE */
645 int mx, my, pred_x, pred_y, bits;
646 int16_t *const mot_val = s->current_picture.motion_val[0][s->block_index[0]];
647 const int stride = s->b8_stride * 2;
650 bits = show_bits(&s->gb, 17);
651 if (bits == MOTION_MARKER)
655 if (bits & 0x10000) {
657 if (s->pict_type == AV_PICTURE_TYPE_S &&
658 ctx->vol_sprite_usage == GMC_SPRITE) {
659 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
663 mx = get_amv(ctx, 0);
664 my = get_amv(ctx, 1);
666 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
673 mot_val[0 + stride] =
674 mot_val[2 + stride] = mx;
677 mot_val[1 + stride] =
678 mot_val[3 + stride] = my;
680 if (s->mbintra_table[xy])
681 ff_clean_intra_table_entries(s);
685 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
687 av_log(s->avctx, AV_LOG_ERROR,
688 "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
694 s->cbp_table[xy] = cbpc & (8 + 3); // 8 is dquant
696 s->mb_intra = ((cbpc & 4) != 0);
699 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
700 s->mbintra_table[xy] = 1;
703 mot_val[0 + stride] =
704 mot_val[2 + stride] = 0;
707 mot_val[1 + stride] =
708 mot_val[3 + stride] = 0;
710 if (s->mbintra_table[xy])
711 ff_clean_intra_table_entries(s);
713 if (s->pict_type == AV_PICTURE_TYPE_S &&
714 ctx->vol_sprite_usage == GMC_SPRITE &&
716 s->mcsel = get_bits1(&s->gb);
720 if ((cbpc & 16) == 0) {
721 /* 16x16 motion prediction */
723 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
725 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
729 my = ff_h263_decode_motion(s, pred_y, s->f_code);
732 s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
735 mx = get_amv(ctx, 0);
736 my = get_amv(ctx, 1);
737 s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
744 mot_val[0 + stride] =
745 mot_val[2 + stride] = mx;
748 mot_val[1 + stride] =
749 mot_val[3 + stride] = my;
752 s->current_picture.mb_type[xy] = MB_TYPE_8x8 |
754 for (i = 0; i < 4; i++) {
755 int16_t *mot_val = ff_h263_pred_motion(s, i, 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);
777 * decode second partition.
778 * @return <0 if an error occurred
780 static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
783 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
785 s->mb_x = s->resync_mb_x;
786 s->first_slice_line = 1;
787 for (s->mb_y = s->resync_mb_y; mb_num < mb_count; s->mb_y++) {
788 ff_init_block_index(s);
789 for (; mb_num < mb_count && s->mb_x < s->mb_width; s->mb_x++) {
790 const int xy = s->mb_x + s->mb_y * s->mb_stride;
793 ff_update_block_index(s);
794 if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
795 s->first_slice_line = 0;
797 if (s->pict_type == AV_PICTURE_TYPE_I) {
798 int ac_pred = get_bits1(&s->gb);
799 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
801 av_log(s->avctx, AV_LOG_ERROR,
802 "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
806 s->cbp_table[xy] |= cbpy << 2;
807 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
808 } else { /* P || S_TYPE */
809 if (IS_INTRA(s->current_picture.mb_type[xy])) {
812 int ac_pred = get_bits1(&s->gb);
813 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
816 av_log(s->avctx, AV_LOG_ERROR,
817 "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
821 if (s->cbp_table[xy] & 8)
822 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
823 s->current_picture.qscale_table[xy] = s->qscale;
825 for (i = 0; i < 6; i++) {
827 int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
829 av_log(s->avctx, AV_LOG_ERROR,
830 "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
837 s->cbp_table[xy] &= 3; // remove dquant
838 s->cbp_table[xy] |= cbpy << 2;
839 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
840 s->pred_dir_table[xy] = dir;
841 } else if (IS_SKIP(s->current_picture.mb_type[xy])) {
842 s->current_picture.qscale_table[xy] = s->qscale;
843 s->cbp_table[xy] = 0;
845 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
848 av_log(s->avctx, AV_LOG_ERROR,
849 "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
853 if (s->cbp_table[xy] & 8)
854 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
855 s->current_picture.qscale_table[xy] = s->qscale;
857 s->cbp_table[xy] &= 3; // remove dquant
858 s->cbp_table[xy] |= (cbpy ^ 0xf) << 2;
862 if (mb_num >= mb_count)
870 * Decode the first and second partition.
871 * @return <0 if error (and sets error type in the error_status_table)
873 int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx)
875 MpegEncContext *s = &ctx->m;
877 const int part_a_error = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR;
878 const int part_a_end = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END | ER_MV_END) : ER_MV_END;
880 mb_num = mpeg4_decode_partition_a(ctx);
882 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
883 s->mb_x, s->mb_y, part_a_error);
887 if (s->resync_mb_x + s->resync_mb_y * s->mb_width + mb_num > s->mb_num) {
888 av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
889 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
890 s->mb_x, s->mb_y, part_a_error);
894 s->mb_num_left = mb_num;
896 if (s->pict_type == AV_PICTURE_TYPE_I) {
897 while (show_bits(&s->gb, 9) == 1)
898 skip_bits(&s->gb, 9);
899 if (get_bits_long(&s->gb, 19) != DC_MARKER) {
900 av_log(s->avctx, AV_LOG_ERROR,
901 "marker missing after first I partition at %d %d\n",
906 while (show_bits(&s->gb, 10) == 1)
907 skip_bits(&s->gb, 10);
908 if (get_bits(&s->gb, 17) != MOTION_MARKER) {
909 av_log(s->avctx, AV_LOG_ERROR,
910 "marker missing after first P partition at %d %d\n",
915 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
916 s->mb_x - 1, s->mb_y, part_a_end);
918 if (mpeg4_decode_partition_b(s, mb_num) < 0) {
919 if (s->pict_type == AV_PICTURE_TYPE_P)
920 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
921 s->mb_x, s->mb_y, ER_DC_ERROR);
924 if (s->pict_type == AV_PICTURE_TYPE_P)
925 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
926 s->mb_x - 1, s->mb_y, ER_DC_END);
934 * @return <0 if an error occurred
936 static inline int mpeg4_decode_block(MpegEncContext *s, int16_t *block,
937 int n, int coded, int intra, int rvlc)
939 int level, i, last, run, qmul, qadd;
940 int av_uninit(dc_pred_dir);
943 const uint8_t *scan_table;
945 // Note intra & rvlc should be optimized away if this is inlined
948 if (s->use_intra_dc_vlc) {
950 if (s->partitioned_frame) {
951 level = s->dc_val[0][s->block_index[n]];
953 level = FASTDIV((level + (s->y_dc_scale >> 1)), s->y_dc_scale);
955 level = FASTDIV((level + (s->c_dc_scale >> 1)), s->c_dc_scale);
956 dc_pred_dir = (s->pred_dir_table[s->mb_x + s->mb_y * s->mb_stride] << n) & 32;
958 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
966 ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
972 rl = &ff_rvlc_rl_intra;
973 rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
975 rl = &ff_mpeg4_rl_intra;
976 rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
979 if (dc_pred_dir == 0)
980 scan_table = s->intra_v_scantable.permutated; /* left */
982 scan_table = s->intra_h_scantable.permutated; /* top */
984 scan_table = s->intra_scantable.permutated;
991 s->block_last_index[n] = i;
995 rl = &ff_rvlc_rl_inter;
997 rl = &ff_h263_rl_inter;
999 scan_table = s->intra_scantable.permutated;
1001 if (s->mpeg_quant) {
1005 rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
1007 rl_vlc = ff_h263_rl_inter.rl_vlc[0];
1009 qmul = s->qscale << 1;
1010 qadd = (s->qscale - 1) | 1;
1012 rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
1014 rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
1018 OPEN_READER(re, &s->gb);
1020 UPDATE_CACHE(re, &s->gb);
1021 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1025 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1026 av_log(s->avctx, AV_LOG_ERROR,
1027 "1. marker bit missing in rvlc esc\n");
1030 SKIP_CACHE(re, &s->gb, 1);
1032 last = SHOW_UBITS(re, &s->gb, 1);
1033 SKIP_CACHE(re, &s->gb, 1);
1034 run = SHOW_UBITS(re, &s->gb, 6);
1035 SKIP_COUNTER(re, &s->gb, 1 + 1 + 6);
1036 UPDATE_CACHE(re, &s->gb);
1038 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1039 av_log(s->avctx, AV_LOG_ERROR,
1040 "2. marker bit missing in rvlc esc\n");
1043 SKIP_CACHE(re, &s->gb, 1);
1045 level = SHOW_UBITS(re, &s->gb, 11);
1046 SKIP_CACHE(re, &s->gb, 11);
1048 if (SHOW_UBITS(re, &s->gb, 5) != 0x10) {
1049 av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
1052 SKIP_CACHE(re, &s->gb, 5);
1054 level = level * qmul + qadd;
1055 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1056 SKIP_COUNTER(re, &s->gb, 1 + 11 + 5 + 1);
1063 cache = GET_CACHE(re, &s->gb);
1066 cache ^= 0xC0000000;
1068 if (cache & 0x80000000) {
1069 if (cache & 0x40000000) {
1071 SKIP_CACHE(re, &s->gb, 2);
1072 last = SHOW_UBITS(re, &s->gb, 1);
1073 SKIP_CACHE(re, &s->gb, 1);
1074 run = SHOW_UBITS(re, &s->gb, 6);
1075 SKIP_COUNTER(re, &s->gb, 2 + 1 + 6);
1076 UPDATE_CACHE(re, &s->gb);
1079 level = SHOW_SBITS(re, &s->gb, 12);
1080 LAST_SKIP_BITS(re, &s->gb, 12);
1082 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1083 av_log(s->avctx, AV_LOG_ERROR,
1084 "1. marker bit missing in 3. esc\n");
1087 SKIP_CACHE(re, &s->gb, 1);
1089 level = SHOW_SBITS(re, &s->gb, 12);
1090 SKIP_CACHE(re, &s->gb, 12);
1092 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1093 av_log(s->avctx, AV_LOG_ERROR,
1094 "2. marker bit missing in 3. esc\n");
1098 SKIP_COUNTER(re, &s->gb, 1 + 12 + 1);
1102 if (s->error_recognition >= FF_ER_COMPLIANT) {
1103 const int abs_level= FFABS(level);
1104 if (abs_level<=MAX_LEVEL && run<=MAX_RUN) {
1105 const int run1= run - rl->max_run[last][abs_level] - 1;
1106 if (abs_level <= rl->max_level[last][run]) {
1107 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1110 if (s->error_recognition > FF_ER_COMPLIANT) {
1111 if (abs_level <= rl->max_level[last][run]*2) {
1112 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1115 if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) {
1116 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1124 level = level * qmul + qadd;
1126 level = level * qmul - qadd;
1128 if ((unsigned)(level + 2048) > 4095) {
1129 if (s->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)) {
1130 if (level > 2560 || level < -2560) {
1131 av_log(s->avctx, AV_LOG_ERROR,
1132 "|level| overflow in 3. esc, qp=%d\n",
1137 level = level < 0 ? -2048 : 2047;
1145 SKIP_BITS(re, &s->gb, 2);
1146 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1147 i += run + rl->max_run[run >> 7][level / qmul] + 1; // FIXME opt indexing
1148 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1149 LAST_SKIP_BITS(re, &s->gb, 1);
1153 SKIP_BITS(re, &s->gb, 1);
1154 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1156 level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; // FIXME opt indexing
1157 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1158 LAST_SKIP_BITS(re, &s->gb, 1);
1163 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1164 LAST_SKIP_BITS(re, &s->gb, 1);
1169 av_log(s->avctx, AV_LOG_ERROR,
1170 "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1174 block[scan_table[i]] = level;
1178 block[scan_table[i]] = level;
1180 CLOSE_READER(re, &s->gb);
1185 if (!s->use_intra_dc_vlc) {
1186 block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
1188 i -= i >> 31; // if (i == -1) i = 0;
1191 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1193 i = 63; // FIXME not optimal
1195 s->block_last_index[n] = i;
1200 * decode partition C of one MB.
1201 * @return <0 if an error occurred
1203 static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
1205 Mpeg4DecContext *ctx = (Mpeg4DecContext *)s;
1207 const int xy = s->mb_x + s->mb_y * s->mb_stride;
1209 mb_type = s->current_picture.mb_type[xy];
1210 cbp = s->cbp_table[xy];
1212 s->use_intra_dc_vlc = s->qscale < s->intra_dc_threshold;
1214 if (s->current_picture.qscale_table[xy] != s->qscale)
1215 ff_set_qscale(s, s->current_picture.qscale_table[xy]);
1217 if (s->pict_type == AV_PICTURE_TYPE_P ||
1218 s->pict_type == AV_PICTURE_TYPE_S) {
1220 for (i = 0; i < 4; i++) {
1221 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
1222 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
1224 s->mb_intra = IS_INTRA(mb_type);
1226 if (IS_SKIP(mb_type)) {
1228 for (i = 0; i < 6; i++)
1229 s->block_last_index[i] = -1;
1230 s->mv_dir = MV_DIR_FORWARD;
1231 s->mv_type = MV_TYPE_16X16;
1232 if (s->pict_type == AV_PICTURE_TYPE_S
1233 && ctx->vol_sprite_usage == GMC_SPRITE) {
1240 } else if (s->mb_intra) {
1241 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1242 } else if (!s->mb_intra) {
1243 // s->mcsel = 0; // FIXME do we need to init that?
1245 s->mv_dir = MV_DIR_FORWARD;
1246 if (IS_8X8(mb_type)) {
1247 s->mv_type = MV_TYPE_8X8;
1249 s->mv_type = MV_TYPE_16X16;
1252 } else { /* I-Frame */
1254 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1257 if (!IS_SKIP(mb_type)) {
1259 s->dsp.clear_blocks(s->block[0]);
1260 /* decode each block */
1261 for (i = 0; i < 6; i++) {
1262 if (mpeg4_decode_block(s, block[i], i, cbp & 32, s->mb_intra, ctx->rvlc) < 0) {
1263 av_log(s->avctx, AV_LOG_ERROR,
1264 "texture corrupted at %d %d %d\n",
1265 s->mb_x, s->mb_y, s->mb_intra);
1272 /* per-MB end of slice check */
1273 if (--s->mb_num_left <= 0) {
1274 if (mpeg4_is_resync(ctx))
1279 if (mpeg4_is_resync(ctx)) {
1280 const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
1281 if (s->cbp_table[xy + delta])
1288 static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
1290 Mpeg4DecContext *ctx = (Mpeg4DecContext *)s;
1291 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1293 static int8_t quant_tab[4] = { -1, -2, 1, 2 };
1294 const int xy = s->mb_x + s->mb_y * s->mb_stride;
1296 av_assert2(s->h263_pred);
1298 if (s->pict_type == AV_PICTURE_TYPE_P ||
1299 s->pict_type == AV_PICTURE_TYPE_S) {
1301 if (get_bits1(&s->gb)) {
1304 for (i = 0; i < 6; i++)
1305 s->block_last_index[i] = -1;
1306 s->mv_dir = MV_DIR_FORWARD;
1307 s->mv_type = MV_TYPE_16X16;
1308 if (s->pict_type == AV_PICTURE_TYPE_S &&
1309 ctx->vol_sprite_usage == GMC_SPRITE) {
1310 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1315 s->mv[0][0][0] = get_amv(ctx, 0);
1316 s->mv[0][0][1] = get_amv(ctx, 1);
1319 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1329 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
1331 av_log(s->avctx, AV_LOG_ERROR,
1332 "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1335 } while (cbpc == 20);
1337 s->dsp.clear_blocks(s->block[0]);
1339 s->mb_intra = ((cbpc & 4) != 0);
1343 if (s->pict_type == AV_PICTURE_TYPE_S &&
1344 ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
1345 s->mcsel = get_bits1(&s->gb);
1348 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
1350 cbp = (cbpc & 3) | (cbpy << 2);
1352 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1353 if ((!s->progressive_sequence) &&
1354 (cbp || (s->workaround_bugs & FF_BUG_XVID_ILACE)))
1355 s->interlaced_dct = get_bits1(&s->gb);
1357 s->mv_dir = MV_DIR_FORWARD;
1358 if ((cbpc & 16) == 0) {
1360 s->current_picture.mb_type[xy] = MB_TYPE_GMC |
1363 /* 16x16 global motion prediction */
1364 s->mv_type = MV_TYPE_16X16;
1365 mx = get_amv(ctx, 0);
1366 my = get_amv(ctx, 1);
1367 s->mv[0][0][0] = mx;
1368 s->mv[0][0][1] = my;
1369 } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
1370 s->current_picture.mb_type[xy] = MB_TYPE_16x8 |
1373 /* 16x8 field motion prediction */
1374 s->mv_type = MV_TYPE_FIELD;
1376 s->field_select[0][0] = get_bits1(&s->gb);
1377 s->field_select[0][1] = get_bits1(&s->gb);
1379 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1381 for (i = 0; i < 2; i++) {
1382 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1386 my = ff_h263_decode_motion(s, pred_y / 2, s->f_code);
1390 s->mv[0][i][0] = mx;
1391 s->mv[0][i][1] = my;
1394 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1395 /* 16x16 motion prediction */
1396 s->mv_type = MV_TYPE_16X16;
1397 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1398 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1403 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1407 s->mv[0][0][0] = mx;
1408 s->mv[0][0][1] = my;
1411 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
1412 s->mv_type = MV_TYPE_8X8;
1413 for (i = 0; i < 4; i++) {
1414 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1415 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1419 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1422 s->mv[0][i][0] = mx;
1423 s->mv[0][i][1] = my;
1428 } else if (s->pict_type == AV_PICTURE_TYPE_B) {
1429 int modb1; // first bit of modb
1430 int modb2; // second bit of modb
1433 s->mb_intra = 0; // B-frames never contain intra blocks
1434 s->mcsel = 0; // ... true gmc blocks
1437 for (i = 0; i < 2; i++) {
1438 s->last_mv[i][0][0] =
1439 s->last_mv[i][0][1] =
1440 s->last_mv[i][1][0] =
1441 s->last_mv[i][1][1] = 0;
1444 ff_thread_await_progress(&s->next_picture_ptr->tf, s->mb_y, 0);
1447 /* if we skipped it in the future P Frame than skip it now too */
1448 s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
1450 if (s->mb_skipped) {
1452 for (i = 0; i < 6; i++)
1453 s->block_last_index[i] = -1;
1455 s->mv_dir = MV_DIR_FORWARD;
1456 s->mv_type = MV_TYPE_16X16;
1461 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1467 modb1 = get_bits1(&s->gb);
1469 // like MB_TYPE_B_DIRECT but no vectors coded
1470 mb_type = MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1;
1473 modb2 = get_bits1(&s->gb);
1474 mb_type = get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
1476 av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
1479 mb_type = mb_type_b_map[mb_type];
1483 s->dsp.clear_blocks(s->block[0]);
1484 cbp = get_bits(&s->gb, 6);
1487 if ((!IS_DIRECT(mb_type)) && cbp) {
1488 if (get_bits1(&s->gb))
1489 ff_set_qscale(s, s->qscale + get_bits1(&s->gb) * 4 - 2);
1492 if (!s->progressive_sequence) {
1494 s->interlaced_dct = get_bits1(&s->gb);
1496 if (!IS_DIRECT(mb_type) && get_bits1(&s->gb)) {
1497 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1498 mb_type &= ~MB_TYPE_16x16;
1500 if (USES_LIST(mb_type, 0)) {
1501 s->field_select[0][0] = get_bits1(&s->gb);
1502 s->field_select[0][1] = get_bits1(&s->gb);
1504 if (USES_LIST(mb_type, 1)) {
1505 s->field_select[1][0] = get_bits1(&s->gb);
1506 s->field_select[1][1] = get_bits1(&s->gb);
1512 if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
1513 s->mv_type = MV_TYPE_16X16;
1515 if (USES_LIST(mb_type, 0)) {
1516 s->mv_dir = MV_DIR_FORWARD;
1518 mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1519 my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1520 s->last_mv[0][1][0] =
1521 s->last_mv[0][0][0] =
1522 s->mv[0][0][0] = mx;
1523 s->last_mv[0][1][1] =
1524 s->last_mv[0][0][1] =
1525 s->mv[0][0][1] = my;
1528 if (USES_LIST(mb_type, 1)) {
1529 s->mv_dir |= MV_DIR_BACKWARD;
1531 mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1532 my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1533 s->last_mv[1][1][0] =
1534 s->last_mv[1][0][0] =
1535 s->mv[1][0][0] = mx;
1536 s->last_mv[1][1][1] =
1537 s->last_mv[1][0][1] =
1538 s->mv[1][0][1] = my;
1540 } else if (!IS_DIRECT(mb_type)) {
1541 s->mv_type = MV_TYPE_FIELD;
1543 if (USES_LIST(mb_type, 0)) {
1544 s->mv_dir = MV_DIR_FORWARD;
1546 for (i = 0; i < 2; i++) {
1547 mx = ff_h263_decode_motion(s, s->last_mv[0][i][0], s->f_code);
1548 my = ff_h263_decode_motion(s, s->last_mv[0][i][1] / 2, s->f_code);
1549 s->last_mv[0][i][0] =
1550 s->mv[0][i][0] = mx;
1551 s->last_mv[0][i][1] = (s->mv[0][i][1] = my) * 2;
1555 if (USES_LIST(mb_type, 1)) {
1556 s->mv_dir |= MV_DIR_BACKWARD;
1558 for (i = 0; i < 2; i++) {
1559 mx = ff_h263_decode_motion(s, s->last_mv[1][i][0], s->b_code);
1560 my = ff_h263_decode_motion(s, s->last_mv[1][i][1] / 2, s->b_code);
1561 s->last_mv[1][i][0] =
1562 s->mv[1][i][0] = mx;
1563 s->last_mv[1][i][1] = (s->mv[1][i][1] = my) * 2;
1569 if (IS_DIRECT(mb_type)) {
1570 if (IS_SKIP(mb_type)) {
1574 mx = ff_h263_decode_motion(s, 0, 1);
1575 my = ff_h263_decode_motion(s, 0, 1);
1578 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1579 mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
1581 s->current_picture.mb_type[xy] = mb_type;
1582 } else { /* I-Frame */
1584 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
1586 av_log(s->avctx, AV_LOG_ERROR,
1587 "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1590 } while (cbpc == 8);
1596 s->ac_pred = get_bits1(&s->gb);
1598 s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
1600 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
1602 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1604 av_log(s->avctx, AV_LOG_ERROR,
1605 "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1608 cbp = (cbpc & 3) | (cbpy << 2);
1610 s->use_intra_dc_vlc = s->qscale < s->intra_dc_threshold;
1613 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1615 if (!s->progressive_sequence)
1616 s->interlaced_dct = get_bits1(&s->gb);
1618 s->dsp.clear_blocks(s->block[0]);
1619 /* decode each block */
1620 for (i = 0; i < 6; i++) {
1621 if (mpeg4_decode_block(s, block[i], i, cbp & 32, 1, 0) < 0)
1628 /* decode each block */
1629 for (i = 0; i < 6; i++) {
1630 if (mpeg4_decode_block(s, block[i], i, cbp & 32, 0, 0) < 0)
1636 /* per-MB end of slice check */
1637 if (s->codec_id == AV_CODEC_ID_MPEG4) {
1638 int next = mpeg4_is_resync(ctx);
1640 if (s->mb_x + s->mb_y*s->mb_width + 1 > next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
1642 } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
1645 if (s->pict_type == AV_PICTURE_TYPE_B) {
1646 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
1647 ff_thread_await_progress(&s->next_picture_ptr->tf,
1648 (s->mb_x + delta >= s->mb_width)
1649 ? FFMIN(s->mb_y + 1, s->mb_height - 1)
1651 if (s->next_picture.mbskip_table[xy + delta])
1662 static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
1664 int hours, minutes, seconds;
1666 if (!show_bits(gb, 23)) {
1667 av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
1671 hours = get_bits(gb, 5);
1672 minutes = get_bits(gb, 6);
1674 seconds = get_bits(gb, 6);
1676 s->time_base = seconds + 60*(minutes + 60*hours);
1684 static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb)
1687 s->avctx->profile = get_bits(gb, 4);
1688 s->avctx->level = get_bits(gb, 4);
1690 // for Simple profile, level 0
1691 if (s->avctx->profile == 0 && s->avctx->level == 8) {
1692 s->avctx->level = 0;
1698 static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
1700 MpegEncContext *s = &ctx->m;
1701 int width, height, vo_ver_id;
1704 skip_bits(gb, 1); /* random access */
1705 s->vo_type = get_bits(gb, 8);
1706 if (get_bits1(gb) != 0) { /* is_ol_id */
1707 vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
1708 skip_bits(gb, 3); /* vo_priority */
1712 s->aspect_ratio_info = get_bits(gb, 4);
1713 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
1714 s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
1715 s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
1717 s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
1720 if ((s->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
1721 int chroma_format = get_bits(gb, 2);
1722 if (chroma_format != CHROMA_420)
1723 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
1725 s->low_delay = get_bits1(gb);
1726 if (get_bits1(gb)) { /* vbv parameters */
1727 get_bits(gb, 15); /* first_half_bitrate */
1728 skip_bits1(gb); /* marker */
1729 get_bits(gb, 15); /* latter_half_bitrate */
1730 skip_bits1(gb); /* marker */
1731 get_bits(gb, 15); /* first_half_vbv_buffer_size */
1732 skip_bits1(gb); /* marker */
1733 get_bits(gb, 3); /* latter_half_vbv_buffer_size */
1734 get_bits(gb, 11); /* first_half_vbv_occupancy */
1735 skip_bits1(gb); /* marker */
1736 get_bits(gb, 15); /* latter_half_vbv_occupancy */
1737 skip_bits1(gb); /* marker */
1740 /* is setting low delay flag only once the smartest thing to do?
1741 * low delay detection won't be overriden. */
1742 if (s->picture_number == 0)
1746 ctx->shape = get_bits(gb, 2); /* vol shape */
1747 if (ctx->shape != RECT_SHAPE)
1748 av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
1749 if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
1750 av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
1751 skip_bits(gb, 4); /* video_object_layer_shape_extension */
1754 check_marker(gb, "before time_increment_resolution");
1756 s->avctx->time_base.den = get_bits(gb, 16);
1757 if (!s->avctx->time_base.den) {
1758 av_log(s->avctx, AV_LOG_ERROR, "time_base.den==0\n");
1759 s->avctx->time_base.num = 0;
1763 ctx->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
1764 if (ctx->time_increment_bits < 1)
1765 ctx->time_increment_bits = 1;
1767 check_marker(gb, "before fixed_vop_rate");
1769 if (get_bits1(gb) != 0) /* fixed_vop_rate */
1770 s->avctx->time_base.num = get_bits(gb, ctx->time_increment_bits);
1772 s->avctx->time_base.num = 1;
1776 if (ctx->shape != BIN_ONLY_SHAPE) {
1777 if (ctx->shape == RECT_SHAPE) {
1778 check_marker(gb, "before width");
1779 width = get_bits(gb, 13);
1780 check_marker(gb, "before height");
1781 height = get_bits(gb, 13);
1782 check_marker(gb, "after height");
1783 if (width && height && /* they should be non zero but who knows */
1784 !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
1785 if (s->width && s->height &&
1786 (s->width != width || s->height != height))
1787 s->context_reinit = 1;
1793 s->progressive_sequence =
1794 s->progressive_frame = get_bits1(gb) ^ 1;
1795 s->interlaced_dct = 0;
1796 if (!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
1797 av_log(s->avctx, AV_LOG_INFO, /* OBMC Disable */
1798 "MPEG4 OBMC not supported (very likely buggy encoder)\n");
1800 ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
1802 ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
1804 if (ctx->vol_sprite_usage == STATIC_SPRITE)
1805 av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
1806 if (ctx->vol_sprite_usage == STATIC_SPRITE ||
1807 ctx->vol_sprite_usage == GMC_SPRITE) {
1808 if (ctx->vol_sprite_usage == STATIC_SPRITE) {
1809 skip_bits(gb, 13); // sprite_width
1810 skip_bits1(gb); /* marker */
1811 skip_bits(gb, 13); // sprite_height
1812 skip_bits1(gb); /* marker */
1813 skip_bits(gb, 13); // sprite_left
1814 skip_bits1(gb); /* marker */
1815 skip_bits(gb, 13); // sprite_top
1816 skip_bits1(gb); /* marker */
1818 ctx->num_sprite_warping_points = get_bits(gb, 6);
1819 if (ctx->num_sprite_warping_points > 3) {
1820 av_log(s->avctx, AV_LOG_ERROR,
1821 "%d sprite_warping_points\n",
1822 ctx->num_sprite_warping_points);
1823 ctx->num_sprite_warping_points = 0;
1826 s->sprite_warping_accuracy = get_bits(gb, 2);
1827 ctx->sprite_brightness_change = get_bits1(gb);
1828 if (ctx->vol_sprite_usage == STATIC_SPRITE)
1829 skip_bits1(gb); // low_latency_sprite
1831 // FIXME sadct disable bit if verid!=1 && shape not rect
1833 if (get_bits1(gb) == 1) { /* not_8_bit */
1834 s->quant_precision = get_bits(gb, 4); /* quant_precision */
1835 if (get_bits(gb, 4) != 8) /* bits_per_pixel */
1836 av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
1837 if (s->quant_precision != 5)
1838 av_log(s->avctx, AV_LOG_ERROR,
1839 "quant precision %d\n", s->quant_precision);
1840 if (s->quant_precision<3 || s->quant_precision>9) {
1841 s->quant_precision = 5;
1844 s->quant_precision = 5;
1847 // FIXME a bunch of grayscale shape things
1849 if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
1852 /* load default matrixes */
1853 for (i = 0; i < 64; i++) {
1854 int j = s->dsp.idct_permutation[i];
1855 v = ff_mpeg4_default_intra_matrix[i];
1856 s->intra_matrix[j] = v;
1857 s->chroma_intra_matrix[j] = v;
1859 v = ff_mpeg4_default_non_intra_matrix[i];
1860 s->inter_matrix[j] = v;
1861 s->chroma_inter_matrix[j] = v;
1864 /* load custom intra matrix */
1865 if (get_bits1(gb)) {
1867 for (i = 0; i < 64; i++) {
1869 v = get_bits(gb, 8);
1874 j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1875 s->intra_matrix[j] = last;
1876 s->chroma_intra_matrix[j] = last;
1879 /* replicate last value */
1880 for (; i < 64; i++) {
1881 int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1882 s->intra_matrix[j] = last;
1883 s->chroma_intra_matrix[j] = last;
1887 /* load custom non intra matrix */
1888 if (get_bits1(gb)) {
1890 for (i = 0; i < 64; i++) {
1892 v = get_bits(gb, 8);
1897 j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1898 s->inter_matrix[j] = v;
1899 s->chroma_inter_matrix[j] = v;
1902 /* replicate last value */
1903 for (; i < 64; i++) {
1904 int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1905 s->inter_matrix[j] = last;
1906 s->chroma_inter_matrix[j] = last;
1910 // FIXME a bunch of grayscale shape things
1914 s->quarter_sample = get_bits1(gb);
1916 s->quarter_sample = 0;
1918 if (!get_bits1(gb)) {
1919 int pos = get_bits_count(gb);
1920 int estimation_method = get_bits(gb, 2);
1921 if (estimation_method < 2) {
1922 if (!get_bits1(gb)) {
1923 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */
1924 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */
1925 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */
1926 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */
1927 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */
1928 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upampling */
1930 if (!get_bits1(gb)) {
1931 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */
1932 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */
1933 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */
1934 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */
1936 if (!check_marker(gb, "in complexity estimation part 1")) {
1937 skip_bits_long(gb, pos - get_bits_count(gb));
1940 if (!get_bits1(gb)) {
1941 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */
1942 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */
1943 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */
1944 ctx->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */
1946 if (!get_bits1(gb)) {
1947 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */
1948 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */
1949 ctx->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */
1950 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */
1951 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */
1952 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */
1954 if (!check_marker(gb, "in complexity estimation part 2")) {
1955 skip_bits_long(gb, pos - get_bits_count(gb));
1958 if (estimation_method == 1) {
1959 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */
1960 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */
1963 av_log(s->avctx, AV_LOG_ERROR,
1964 "Invalid Complexity estimation method %d\n",
1969 ctx->cplx_estimation_trash_i =
1970 ctx->cplx_estimation_trash_p =
1971 ctx->cplx_estimation_trash_b = 0;
1974 ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
1976 s->data_partitioning = get_bits1(gb);
1977 if (s->data_partitioning)
1978 ctx->rvlc = get_bits1(gb);
1980 if (vo_ver_id != 1) {
1981 ctx->new_pred = get_bits1(gb);
1982 if (ctx->new_pred) {
1983 av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
1984 skip_bits(gb, 2); /* requested upstream message type */
1985 skip_bits1(gb); /* newpred segment type */
1987 if (get_bits1(gb)) // reduced_res_vop
1988 av_log(s->avctx, AV_LOG_ERROR,
1989 "reduced resolution VOP not supported\n");
1994 ctx->scalability = get_bits1(gb);
1996 if (ctx->scalability) {
1997 GetBitContext bak = *gb;
1998 int h_sampling_factor_n;
1999 int h_sampling_factor_m;
2000 int v_sampling_factor_n;
2001 int v_sampling_factor_m;
2003 skip_bits1(gb); // hierarchy_type
2004 skip_bits(gb, 4); /* ref_layer_id */
2005 skip_bits1(gb); /* ref_layer_sampling_dir */
2006 h_sampling_factor_n = get_bits(gb, 5);
2007 h_sampling_factor_m = get_bits(gb, 5);
2008 v_sampling_factor_n = get_bits(gb, 5);
2009 v_sampling_factor_m = get_bits(gb, 5);
2010 ctx->enhancement_type = get_bits1(gb);
2012 if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
2013 v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
2014 /* illegal scalability header (VERY broken encoder),
2015 * trying to workaround */
2016 ctx->scalability = 0;
2019 av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
2021 // bin shape stuff FIXME
2025 if (s->avctx->debug&FF_DEBUG_PICT_INFO) {
2026 av_log(s->avctx, AV_LOG_DEBUG, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d, %s%s%s%s\n",
2027 s->avctx->time_base.num, s->avctx->time_base.den,
2028 ctx->time_increment_bits,
2030 s->progressive_sequence,
2031 ctx->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
2032 s->data_partitioning ? "partition " : "", ctx->rvlc ? "rvlc " : ""
2040 * Decode the user data stuff in the header.
2041 * Also initializes divx/xvid/lavc_version/build.
2043 static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
2045 MpegEncContext *s = &ctx->m;
2049 int ver = 0, build = 0, ver2 = 0, ver3 = 0;
2052 for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
2053 if (show_bits(gb, 23) == 0)
2055 buf[i] = get_bits(gb, 8);
2059 /* divx detection */
2060 e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
2062 e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
2064 ctx->divx_version = ver;
2065 ctx->divx_build = build;
2066 s->divx_packed = e == 3 && last == 'p';
2067 if (s->divx_packed && !s->showed_packed_warning) {
2068 av_log(s->avctx, AV_LOG_INFO, "Video uses a non-standard and "
2069 "wasteful way to store B-frames ('packed B-frames'). "
2070 "Consider using a tool like VirtualDub or avidemux to fix it.\n");
2071 s->showed_packed_warning = 1;
2075 /* libavcodec detection */
2076 e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
2078 e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
2080 e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
2082 build = (ver << 16) + (ver2 << 8) + ver3;
2085 if (strcmp(buf, "ffmpeg") == 0)
2086 ctx->lavc_build = 4600;
2089 ctx->lavc_build = build;
2091 /* Xvid detection */
2092 e = sscanf(buf, "XviD%d", &build);
2094 ctx->xvid_build = build;
2099 int ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
2101 Mpeg4DecContext *ctx = avctx->priv_data;
2102 MpegEncContext *s = &ctx->m;
2104 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
2105 if (s->stream_codec_tag == AV_RL32("XVID") ||
2106 s->codec_tag == AV_RL32("XVID") ||
2107 s->codec_tag == AV_RL32("XVIX") ||
2108 s->codec_tag == AV_RL32("RMP4") ||
2109 s->codec_tag == AV_RL32("ZMP4") ||
2110 s->codec_tag == AV_RL32("SIPP"))
2111 ctx->xvid_build = 0;
2114 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
2115 if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
2116 s->vol_control_parameters == 0)
2117 ctx->divx_version = 400; // divx 4
2119 if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
2121 ctx->divx_build = -1;
2124 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
2125 if (s->codec_tag == AV_RL32("XVIX"))
2126 s->workaround_bugs |= FF_BUG_XVID_ILACE;
2128 if (s->codec_tag == AV_RL32("UMP4"))
2129 s->workaround_bugs |= FF_BUG_UMP4;
2131 if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
2132 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2134 if (ctx->divx_version > 502 && ctx->divx_build < 1814)
2135 s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
2137 if (ctx->xvid_build <= 3U)
2138 s->padding_bug_score = 256 * 256 * 256 * 64;
2140 if (ctx->xvid_build <= 1U)
2141 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2143 if (ctx->xvid_build <= 12U)
2144 s->workaround_bugs |= FF_BUG_EDGE;
2146 if (ctx->xvid_build <= 32U)
2147 s->workaround_bugs |= FF_BUG_DC_CLIP;
2149 #define SET_QPEL_FUNC(postfix1, postfix2) \
2150 s->dsp.put_ ## postfix1 = ff_put_ ## postfix2; \
2151 s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
2152 s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
2154 if (ctx->lavc_build < 4653U)
2155 s->workaround_bugs |= FF_BUG_STD_QPEL;
2157 if (ctx->lavc_build < 4655U)
2158 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2160 if (ctx->lavc_build < 4670U)
2161 s->workaround_bugs |= FF_BUG_EDGE;
2163 if (ctx->lavc_build <= 4712U)
2164 s->workaround_bugs |= FF_BUG_DC_CLIP;
2166 if (ctx->divx_version >= 0)
2167 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2168 if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
2169 s->padding_bug_score = 256 * 256 * 256 * 64;
2171 if (ctx->divx_version < 500U)
2172 s->workaround_bugs |= FF_BUG_EDGE;
2174 if (ctx->divx_version >= 0)
2175 s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
2178 if (s->workaround_bugs & FF_BUG_STD_QPEL) {
2179 SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
2180 SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
2181 SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
2182 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
2183 SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
2184 SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
2186 SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
2187 SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
2188 SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
2189 SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
2190 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
2191 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
2194 if (avctx->debug & FF_DEBUG_BUGS)
2195 av_log(s->avctx, AV_LOG_DEBUG,
2196 "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
2197 s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
2198 ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
2201 if (s->codec_id == AV_CODEC_ID_MPEG4 && ctx->xvid_build >= 0 &&
2202 avctx->idct_algo == FF_IDCT_AUTO &&
2203 (av_get_cpu_flags() & AV_CPU_FLAG_MMX)) {
2204 avctx->idct_algo = FF_IDCT_XVIDMMX;
2205 ff_dct_common_init(s);
2212 static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2214 MpegEncContext *s = &ctx->m;
2215 int time_incr, time_increment;
2218 s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
2219 if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
2220 s->vol_control_parameters == 0 && !(s->flags & CODEC_FLAG_LOW_DELAY)) {
2221 av_log(s->avctx, AV_LOG_ERROR, "low_delay flag incorrectly, clearing it\n");
2225 s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
2226 if (s->partitioned_frame)
2227 s->decode_mb = mpeg4_decode_partitioned_mb;
2229 s->decode_mb = mpeg4_decode_mb;
2232 while (get_bits1(gb) != 0)
2235 check_marker(gb, "before time_increment");
2237 if (ctx->time_increment_bits == 0 ||
2238 !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
2239 av_log(s->avctx, AV_LOG_ERROR,
2240 "hmm, seems the headers are not complete, trying to guess time_increment_bits\n");
2242 for (ctx->time_increment_bits = 1;
2243 ctx->time_increment_bits < 16;
2244 ctx->time_increment_bits++) {
2245 if (s->pict_type == AV_PICTURE_TYPE_P ||
2246 (s->pict_type == AV_PICTURE_TYPE_S &&
2247 ctx->vol_sprite_usage == GMC_SPRITE)) {
2248 if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
2250 } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
2254 av_log(s->avctx, AV_LOG_ERROR,
2255 "my guess is %d bits ;)\n", ctx->time_increment_bits);
2256 if (s->avctx->time_base.den && 4*s->avctx->time_base.den < 1<<ctx->time_increment_bits) {
2257 s->avctx->time_base.den = 1<<ctx->time_increment_bits;
2262 time_increment = get_bits1(gb); // FIXME investigate further
2264 time_increment = get_bits(gb, ctx->time_increment_bits);
2266 if (s->pict_type != AV_PICTURE_TYPE_B) {
2267 s->last_time_base = s->time_base;
2268 s->time_base += time_incr;
2269 s->time = s->time_base * s->avctx->time_base.den + time_increment;
2270 if (s->workaround_bugs & FF_BUG_UMP4) {
2271 if (s->time < s->last_non_b_time) {
2272 /* header is not mpeg-4-compatible, broken encoder,
2273 * trying to workaround */
2275 s->time += s->avctx->time_base.den;
2278 s->pp_time = s->time - s->last_non_b_time;
2279 s->last_non_b_time = s->time;
2281 s->time = (s->last_time_base + time_incr) * s->avctx->time_base.den + time_increment;
2282 s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
2283 if (s->pp_time <= s->pb_time ||
2284 s->pp_time <= s->pp_time - s->pb_time ||
2286 /* messed up order, maybe after seeking? skipping current b-frame */
2287 return FRAME_SKIPPED;
2289 ff_mpeg4_init_direct_mv(s);
2291 if (ctx->t_frame == 0)
2292 ctx->t_frame = s->pb_time;
2293 if (ctx->t_frame == 0)
2294 ctx->t_frame = 1; // 1/0 protection
2295 s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
2296 ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2297 s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
2298 ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2299 if (!s->progressive_sequence) {
2300 if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1)
2301 return FRAME_SKIPPED;
2305 if (s->avctx->time_base.num)
2306 pts = ROUNDED_DIV(s->time, s->avctx->time_base.num);
2308 pts = AV_NOPTS_VALUE;
2309 if (s->avctx->debug&FF_DEBUG_PTS)
2310 av_log(s->avctx, AV_LOG_DEBUG, "MPEG4 PTS: %"PRId64"\n",
2313 check_marker(gb, "before vop_coded");
2316 if (get_bits1(gb) != 1) {
2317 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2318 av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
2319 return FRAME_SKIPPED;
2322 decode_new_pred(ctx, gb);
2324 if (ctx->shape != BIN_ONLY_SHAPE &&
2325 (s->pict_type == AV_PICTURE_TYPE_P ||
2326 (s->pict_type == AV_PICTURE_TYPE_S &&
2327 ctx->vol_sprite_usage == GMC_SPRITE))) {
2328 /* rounding type for motion estimation */
2329 s->no_rounding = get_bits1(gb);
2333 // FIXME reduced res stuff
2335 if (ctx->shape != RECT_SHAPE) {
2336 if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
2337 skip_bits(gb, 13); /* width */
2338 skip_bits1(gb); /* marker */
2339 skip_bits(gb, 13); /* height */
2340 skip_bits1(gb); /* marker */
2341 skip_bits(gb, 13); /* hor_spat_ref */
2342 skip_bits1(gb); /* marker */
2343 skip_bits(gb, 13); /* ver_spat_ref */
2345 skip_bits1(gb); /* change_CR_disable */
2347 if (get_bits1(gb) != 0)
2348 skip_bits(gb, 8); /* constant_alpha_value */
2351 // FIXME complexity estimation stuff
2353 if (ctx->shape != BIN_ONLY_SHAPE) {
2354 skip_bits_long(gb, ctx->cplx_estimation_trash_i);
2355 if (s->pict_type != AV_PICTURE_TYPE_I)
2356 skip_bits_long(gb, ctx->cplx_estimation_trash_p);
2357 if (s->pict_type == AV_PICTURE_TYPE_B)
2358 skip_bits_long(gb, ctx->cplx_estimation_trash_b);
2360 if (get_bits_left(gb) < 3) {
2361 av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
2364 s->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
2365 if (!s->progressive_sequence) {
2366 s->top_field_first = get_bits1(gb);
2367 s->alternate_scan = get_bits1(gb);
2369 s->alternate_scan = 0;
2372 if (s->alternate_scan) {
2373 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
2374 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
2375 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
2376 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2378 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
2379 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
2380 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
2381 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2384 if (s->pict_type == AV_PICTURE_TYPE_S &&
2385 (ctx->vol_sprite_usage == STATIC_SPRITE ||
2386 ctx->vol_sprite_usage == GMC_SPRITE)) {
2387 if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
2388 return AVERROR_INVALIDDATA;
2389 if (ctx->sprite_brightness_change)
2390 av_log(s->avctx, AV_LOG_ERROR,
2391 "sprite_brightness_change not supported\n");
2392 if (ctx->vol_sprite_usage == STATIC_SPRITE)
2393 av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
2396 if (ctx->shape != BIN_ONLY_SHAPE) {
2397 s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision);
2398 if (s->qscale == 0) {
2399 av_log(s->avctx, AV_LOG_ERROR,
2400 "Error, header damaged or not MPEG4 header (qscale=0)\n");
2401 return -1; // makes no sense to continue, as there is nothing left from the image then
2404 if (s->pict_type != AV_PICTURE_TYPE_I) {
2405 s->f_code = get_bits(gb, 3); /* fcode_for */
2406 if (s->f_code == 0) {
2407 av_log(s->avctx, AV_LOG_ERROR,
2408 "Error, header damaged or not MPEG4 header (f_code=0)\n");
2410 return -1; // makes no sense to continue, as there is nothing left from the image then
2415 if (s->pict_type == AV_PICTURE_TYPE_B) {
2416 s->b_code = get_bits(gb, 3);
2417 if (s->b_code == 0) {
2418 av_log(s->avctx, AV_LOG_ERROR,
2419 "Error, header damaged or not MPEG4 header (b_code=0)\n");
2421 return -1; // makes no sense to continue, as the MV decoding will break very quickly
2426 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2427 av_log(s->avctx, AV_LOG_DEBUG,
2428 "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",
2429 s->qscale, s->f_code, s->b_code,
2430 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")),
2431 gb->size_in_bits,s->progressive_sequence, s->alternate_scan,
2432 s->top_field_first, s->quarter_sample ? "q" : "h",
2433 s->data_partitioning, ctx->resync_marker,
2434 ctx->num_sprite_warping_points, s->sprite_warping_accuracy,
2435 1 - s->no_rounding, s->vo_type,
2436 s->vol_control_parameters ? " VOLC" : " ", s->intra_dc_threshold,
2437 ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
2438 ctx->cplx_estimation_trash_b,
2444 if (!ctx->scalability) {
2445 if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
2446 skip_bits1(gb); // vop shape coding type
2448 if (ctx->enhancement_type) {
2449 int load_backward_shape = get_bits1(gb);
2450 if (load_backward_shape)
2451 av_log(s->avctx, AV_LOG_ERROR,
2452 "load backward shape isn't supported\n");
2454 skip_bits(gb, 2); // ref_select_code
2457 /* detect buggy encoders which don't set the low_delay flag
2458 * (divx4/xvid/opendivx). Note we cannot detect divx5 without b-frames
2459 * easily (although it's buggy too) */
2460 if (s->vo_type == 0 && s->vol_control_parameters == 0 &&
2461 ctx->divx_version == -1 && s->picture_number == 0) {
2462 av_log(s->avctx, AV_LOG_WARNING,
2463 "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
2467 s->picture_number++; // better than pic number==0 always ;)
2469 // FIXME add short header support
2470 s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
2471 s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
2473 if (s->workaround_bugs & FF_BUG_EDGE) {
2474 s->h_edge_pos = s->width;
2475 s->v_edge_pos = s->height;
2481 * Decode mpeg4 headers.
2482 * @return <0 if no VOP found (or a damaged one)
2483 * FRAME_SKIPPED if a not coded VOP is found
2484 * 0 if a VOP is found
2486 int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2488 MpegEncContext *s = &ctx->m;
2489 unsigned startcode, v;
2491 /* search next start code */
2494 if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
2496 if (get_bits(gb, 8) == 0xF0)
2502 if (get_bits_count(gb) >= gb->size_in_bits) {
2503 if (gb->size_in_bits == 8 &&
2504 (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
2505 av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
2506 return FRAME_SKIPPED; // divx bug
2508 return -1; // end of stream
2511 /* use the bits after the test */
2512 v = get_bits(gb, 8);
2513 startcode = ((startcode << 8) | v) & 0xffffffff;
2515 if ((startcode & 0xFFFFFF00) != 0x100)
2516 continue; // no startcode
2518 if (s->avctx->debug & FF_DEBUG_STARTCODE) {
2519 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
2520 if (startcode <= 0x11F)
2521 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
2522 else if (startcode <= 0x12F)
2523 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
2524 else if (startcode <= 0x13F)
2525 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2526 else if (startcode <= 0x15F)
2527 av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
2528 else if (startcode <= 0x1AF)
2529 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2530 else if (startcode == 0x1B0)
2531 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
2532 else if (startcode == 0x1B1)
2533 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
2534 else if (startcode == 0x1B2)
2535 av_log(s->avctx, AV_LOG_DEBUG, "User Data");
2536 else if (startcode == 0x1B3)
2537 av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
2538 else if (startcode == 0x1B4)
2539 av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
2540 else if (startcode == 0x1B5)
2541 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
2542 else if (startcode == 0x1B6)
2543 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
2544 else if (startcode == 0x1B7)
2545 av_log(s->avctx, AV_LOG_DEBUG, "slice start");
2546 else if (startcode == 0x1B8)
2547 av_log(s->avctx, AV_LOG_DEBUG, "extension start");
2548 else if (startcode == 0x1B9)
2549 av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
2550 else if (startcode == 0x1BA)
2551 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
2552 else if (startcode == 0x1BB)
2553 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
2554 else if (startcode == 0x1BC)
2555 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
2556 else if (startcode == 0x1BD)
2557 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
2558 else if (startcode == 0x1BE)
2559 av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
2560 else if (startcode == 0x1BF)
2561 av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
2562 else if (startcode == 0x1C0)
2563 av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
2564 else if (startcode == 0x1C1)
2565 av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
2566 else if (startcode == 0x1C2)
2567 av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
2568 else if (startcode == 0x1C3)
2569 av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
2570 else if (startcode <= 0x1C5)
2571 av_log(s->avctx, AV_LOG_DEBUG, "reserved");
2572 else if (startcode <= 0x1FF)
2573 av_log(s->avctx, AV_LOG_DEBUG, "System start");
2574 av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
2577 if (startcode >= 0x120 && startcode <= 0x12F) {
2578 if (decode_vol_header(ctx, gb) < 0)
2580 } else if (startcode == USER_DATA_STARTCODE) {
2581 decode_user_data(ctx, gb);
2582 } else if (startcode == GOP_STARTCODE) {
2583 mpeg4_decode_gop_header(s, gb);
2584 } else if (startcode == VOS_STARTCODE) {
2585 mpeg4_decode_profile_level(s, gb);
2586 } else if (startcode == VOP_STARTCODE) {
2595 if (s->flags & CODEC_FLAG_LOW_DELAY)
2597 s->avctx->has_b_frames = !s->low_delay;
2599 return decode_vop_header(ctx, gb);
2602 av_cold void ff_mpeg4videodec_static_init(void) {
2603 static int done = 0;
2606 ff_init_rl(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
2607 ff_init_rl(&ff_rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
2608 ff_init_rl(&ff_rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
2609 INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
2610 INIT_VLC_RL(ff_rvlc_rl_inter, 1072);
2611 INIT_VLC_RL(ff_rvlc_rl_intra, 1072);
2612 INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
2613 &ff_mpeg4_DCtab_lum[0][1], 2, 1,
2614 &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
2615 INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
2616 &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
2617 &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
2618 INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
2619 &ff_sprite_trajectory_tab[0][1], 4, 2,
2620 &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
2621 INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
2622 &ff_mb_type_b_tab[0][1], 2, 1,
2623 &ff_mb_type_b_tab[0][0], 2, 1, 16);
2628 int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
2630 Mpeg4DecContext *ctx = avctx->priv_data;
2631 MpegEncContext *s = &ctx->m;
2633 /* divx 5.01+ bitstream reorder stuff */
2634 /* Since this clobbers the input buffer and hwaccel codecs still need the
2635 * data during hwaccel->end_frame we should not do this any earlier */
2636 if (s->divx_packed) {
2637 int current_pos = s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb) >> 3);
2638 int startcode_found = 0;
2640 if (buf_size - current_pos > 7) {
2643 for (i = current_pos; i < buf_size - 4; i++)
2648 buf[i + 3] == 0xB6) {
2649 startcode_found = !(buf[i + 4] & 0x40);
2654 if (startcode_found) {
2655 av_fast_malloc(&s->bitstream_buffer,
2656 &s->allocated_bitstream_buffer_size,
2657 buf_size - current_pos +
2658 FF_INPUT_BUFFER_PADDING_SIZE);
2659 if (!s->bitstream_buffer)
2660 return AVERROR(ENOMEM);
2661 memcpy(s->bitstream_buffer, buf + current_pos,
2662 buf_size - current_pos);
2663 s->bitstream_buffer_size = buf_size - current_pos;
2670 static int mpeg4_update_thread_context(AVCodecContext *dst,
2671 const AVCodecContext *src)
2673 Mpeg4DecContext *s = dst->priv_data;
2674 const Mpeg4DecContext *s1 = src->priv_data;
2676 int ret = ff_mpeg_update_thread_context(dst, src);
2681 memcpy(((uint8_t*)s) + sizeof(MpegEncContext), ((uint8_t*)s1) + sizeof(MpegEncContext), sizeof(Mpeg4DecContext) - sizeof(MpegEncContext));
2686 static av_cold int decode_init(AVCodecContext *avctx)
2688 Mpeg4DecContext *ctx = avctx->priv_data;
2689 MpegEncContext *s = &ctx->m;
2695 ctx->lavc_build = -1;
2697 if ((ret = ff_h263_decode_init(avctx)) < 0)
2700 ff_mpeg4videodec_static_init();
2703 s->low_delay = 0; /* default, might be overridden in the vol header during header parsing */
2704 s->decode_mb = mpeg4_decode_mb;
2705 ctx->time_increment_bits = 4; /* default value for broken headers */
2707 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
2708 avctx->internal->allocate_progress = 1;
2713 static const AVProfile mpeg4_video_profiles[] = {
2714 { FF_PROFILE_MPEG4_SIMPLE, "Simple Profile" },
2715 { FF_PROFILE_MPEG4_SIMPLE_SCALABLE, "Simple Scalable Profile" },
2716 { FF_PROFILE_MPEG4_CORE, "Core Profile" },
2717 { FF_PROFILE_MPEG4_MAIN, "Main Profile" },
2718 { FF_PROFILE_MPEG4_N_BIT, "N-bit Profile" },
2719 { FF_PROFILE_MPEG4_SCALABLE_TEXTURE, "Scalable Texture Profile" },
2720 { FF_PROFILE_MPEG4_SIMPLE_FACE_ANIMATION, "Simple Face Animation Profile" },
2721 { FF_PROFILE_MPEG4_BASIC_ANIMATED_TEXTURE, "Basic Animated Texture Profile" },
2722 { FF_PROFILE_MPEG4_HYBRID, "Hybrid Profile" },
2723 { FF_PROFILE_MPEG4_ADVANCED_REAL_TIME, "Advanced Real Time Simple Profile" },
2724 { FF_PROFILE_MPEG4_CORE_SCALABLE, "Code Scalable Profile" },
2725 { FF_PROFILE_MPEG4_ADVANCED_CODING, "Advanced Coding Profile" },
2726 { FF_PROFILE_MPEG4_ADVANCED_CORE, "Advanced Core Profile" },
2727 { FF_PROFILE_MPEG4_ADVANCED_SCALABLE_TEXTURE, "Advanced Scalable Texture Profile" },
2728 { FF_PROFILE_MPEG4_SIMPLE_STUDIO, "Simple Studio Profile" },
2729 { FF_PROFILE_MPEG4_ADVANCED_SIMPLE, "Advanced Simple Profile" },
2730 { FF_PROFILE_UNKNOWN },
2733 static const AVOption mpeg4_options[] = {
2734 {"quarter_sample", "1/4 subpel MC", offsetof(MpegEncContext, quarter_sample), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
2735 {"divx_packed", "divx style packed b frames", offsetof(MpegEncContext, divx_packed), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 0},
2739 static const AVClass mpeg4_class = {
2740 "MPEG4 Video Decoder",
2741 av_default_item_name,
2743 LIBAVUTIL_VERSION_INT,
2746 static const AVClass mpeg4_vdpau_class = {
2747 "MPEG4 Video VDPAU Decoder",
2748 av_default_item_name,
2750 LIBAVUTIL_VERSION_INT,
2753 AVCodec ff_mpeg4_decoder = {
2755 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
2756 .type = AVMEDIA_TYPE_VIDEO,
2757 .id = AV_CODEC_ID_MPEG4,
2758 .priv_data_size = sizeof(Mpeg4DecContext),
2759 .init = decode_init,
2760 .close = ff_h263_decode_end,
2761 .decode = ff_h263_decode_frame,
2762 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2763 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2764 CODEC_CAP_FRAME_THREADS,
2765 .flush = ff_mpeg_flush,
2767 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
2768 .profiles = NULL_IF_CONFIG_SMALL(mpeg4_video_profiles),
2769 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg4_update_thread_context),
2770 .priv_class = &mpeg4_class,
2774 #if CONFIG_MPEG4_VDPAU_DECODER
2775 AVCodec ff_mpeg4_vdpau_decoder = {
2776 .name = "mpeg4_vdpau",
2777 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 (VDPAU)"),
2778 .type = AVMEDIA_TYPE_VIDEO,
2779 .id = AV_CODEC_ID_MPEG4,
2780 .priv_data_size = sizeof(MpegEncContext),
2781 .init = decode_init,
2782 .close = ff_h263_decode_end,
2783 .decode = ff_h263_decode_frame,
2784 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2785 CODEC_CAP_HWACCEL_VDPAU,
2786 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_MPEG4,
2788 .priv_class = &mpeg4_vdpau_class,