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(Mpeg4DecContext *ctx, int16_t *block,
937 int n, int coded, int intra, int rvlc)
939 MpegEncContext *s = &ctx->m;
940 int level, i, last, run, qmul, qadd;
941 int av_uninit(dc_pred_dir);
944 const uint8_t *scan_table;
946 // Note intra & rvlc should be optimized away if this is inlined
949 if (ctx->use_intra_dc_vlc) {
951 if (s->partitioned_frame) {
952 level = s->dc_val[0][s->block_index[n]];
954 level = FASTDIV((level + (s->y_dc_scale >> 1)), s->y_dc_scale);
956 level = FASTDIV((level + (s->c_dc_scale >> 1)), s->c_dc_scale);
957 dc_pred_dir = (s->pred_dir_table[s->mb_x + s->mb_y * s->mb_stride] << n) & 32;
959 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
967 ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
973 rl = &ff_rvlc_rl_intra;
974 rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
976 rl = &ff_mpeg4_rl_intra;
977 rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
980 if (dc_pred_dir == 0)
981 scan_table = s->intra_v_scantable.permutated; /* left */
983 scan_table = s->intra_h_scantable.permutated; /* top */
985 scan_table = s->intra_scantable.permutated;
992 s->block_last_index[n] = i;
996 rl = &ff_rvlc_rl_inter;
998 rl = &ff_h263_rl_inter;
1000 scan_table = s->intra_scantable.permutated;
1002 if (s->mpeg_quant) {
1006 rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
1008 rl_vlc = ff_h263_rl_inter.rl_vlc[0];
1010 qmul = s->qscale << 1;
1011 qadd = (s->qscale - 1) | 1;
1013 rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
1015 rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
1019 OPEN_READER(re, &s->gb);
1021 UPDATE_CACHE(re, &s->gb);
1022 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1026 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1027 av_log(s->avctx, AV_LOG_ERROR,
1028 "1. marker bit missing in rvlc esc\n");
1031 SKIP_CACHE(re, &s->gb, 1);
1033 last = SHOW_UBITS(re, &s->gb, 1);
1034 SKIP_CACHE(re, &s->gb, 1);
1035 run = SHOW_UBITS(re, &s->gb, 6);
1036 SKIP_COUNTER(re, &s->gb, 1 + 1 + 6);
1037 UPDATE_CACHE(re, &s->gb);
1039 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1040 av_log(s->avctx, AV_LOG_ERROR,
1041 "2. marker bit missing in rvlc esc\n");
1044 SKIP_CACHE(re, &s->gb, 1);
1046 level = SHOW_UBITS(re, &s->gb, 11);
1047 SKIP_CACHE(re, &s->gb, 11);
1049 if (SHOW_UBITS(re, &s->gb, 5) != 0x10) {
1050 av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
1053 SKIP_CACHE(re, &s->gb, 5);
1055 level = level * qmul + qadd;
1056 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1057 SKIP_COUNTER(re, &s->gb, 1 + 11 + 5 + 1);
1064 cache = GET_CACHE(re, &s->gb);
1067 cache ^= 0xC0000000;
1069 if (cache & 0x80000000) {
1070 if (cache & 0x40000000) {
1072 SKIP_CACHE(re, &s->gb, 2);
1073 last = SHOW_UBITS(re, &s->gb, 1);
1074 SKIP_CACHE(re, &s->gb, 1);
1075 run = SHOW_UBITS(re, &s->gb, 6);
1076 SKIP_COUNTER(re, &s->gb, 2 + 1 + 6);
1077 UPDATE_CACHE(re, &s->gb);
1080 level = SHOW_SBITS(re, &s->gb, 12);
1081 LAST_SKIP_BITS(re, &s->gb, 12);
1083 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1084 av_log(s->avctx, AV_LOG_ERROR,
1085 "1. marker bit missing in 3. esc\n");
1088 SKIP_CACHE(re, &s->gb, 1);
1090 level = SHOW_SBITS(re, &s->gb, 12);
1091 SKIP_CACHE(re, &s->gb, 12);
1093 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1094 av_log(s->avctx, AV_LOG_ERROR,
1095 "2. marker bit missing in 3. esc\n");
1099 SKIP_COUNTER(re, &s->gb, 1 + 12 + 1);
1103 if (s->error_recognition >= FF_ER_COMPLIANT) {
1104 const int abs_level= FFABS(level);
1105 if (abs_level<=MAX_LEVEL && run<=MAX_RUN) {
1106 const int run1= run - rl->max_run[last][abs_level] - 1;
1107 if (abs_level <= rl->max_level[last][run]) {
1108 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1111 if (s->error_recognition > FF_ER_COMPLIANT) {
1112 if (abs_level <= rl->max_level[last][run]*2) {
1113 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1116 if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) {
1117 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1125 level = level * qmul + qadd;
1127 level = level * qmul - qadd;
1129 if ((unsigned)(level + 2048) > 4095) {
1130 if (s->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)) {
1131 if (level > 2560 || level < -2560) {
1132 av_log(s->avctx, AV_LOG_ERROR,
1133 "|level| overflow in 3. esc, qp=%d\n",
1138 level = level < 0 ? -2048 : 2047;
1146 SKIP_BITS(re, &s->gb, 2);
1147 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1148 i += run + rl->max_run[run >> 7][level / qmul] + 1; // FIXME opt indexing
1149 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1150 LAST_SKIP_BITS(re, &s->gb, 1);
1154 SKIP_BITS(re, &s->gb, 1);
1155 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1157 level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; // FIXME opt indexing
1158 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1159 LAST_SKIP_BITS(re, &s->gb, 1);
1164 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1165 LAST_SKIP_BITS(re, &s->gb, 1);
1170 av_log(s->avctx, AV_LOG_ERROR,
1171 "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1175 block[scan_table[i]] = level;
1179 block[scan_table[i]] = level;
1181 CLOSE_READER(re, &s->gb);
1186 if (!ctx->use_intra_dc_vlc) {
1187 block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
1189 i -= i >> 31; // if (i == -1) i = 0;
1192 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1194 i = 63; // FIXME not optimal
1196 s->block_last_index[n] = i;
1201 * decode partition C of one MB.
1202 * @return <0 if an error occurred
1204 static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
1206 Mpeg4DecContext *ctx = (Mpeg4DecContext *)s;
1208 const int xy = s->mb_x + s->mb_y * s->mb_stride;
1210 mb_type = s->current_picture.mb_type[xy];
1211 cbp = s->cbp_table[xy];
1213 ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1215 if (s->current_picture.qscale_table[xy] != s->qscale)
1216 ff_set_qscale(s, s->current_picture.qscale_table[xy]);
1218 if (s->pict_type == AV_PICTURE_TYPE_P ||
1219 s->pict_type == AV_PICTURE_TYPE_S) {
1221 for (i = 0; i < 4; i++) {
1222 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
1223 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
1225 s->mb_intra = IS_INTRA(mb_type);
1227 if (IS_SKIP(mb_type)) {
1229 for (i = 0; i < 6; i++)
1230 s->block_last_index[i] = -1;
1231 s->mv_dir = MV_DIR_FORWARD;
1232 s->mv_type = MV_TYPE_16X16;
1233 if (s->pict_type == AV_PICTURE_TYPE_S
1234 && ctx->vol_sprite_usage == GMC_SPRITE) {
1241 } else if (s->mb_intra) {
1242 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1243 } else if (!s->mb_intra) {
1244 // s->mcsel = 0; // FIXME do we need to init that?
1246 s->mv_dir = MV_DIR_FORWARD;
1247 if (IS_8X8(mb_type)) {
1248 s->mv_type = MV_TYPE_8X8;
1250 s->mv_type = MV_TYPE_16X16;
1253 } else { /* I-Frame */
1255 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1258 if (!IS_SKIP(mb_type)) {
1260 s->dsp.clear_blocks(s->block[0]);
1261 /* decode each block */
1262 for (i = 0; i < 6; i++) {
1263 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, s->mb_intra, ctx->rvlc) < 0) {
1264 av_log(s->avctx, AV_LOG_ERROR,
1265 "texture corrupted at %d %d %d\n",
1266 s->mb_x, s->mb_y, s->mb_intra);
1273 /* per-MB end of slice check */
1274 if (--s->mb_num_left <= 0) {
1275 if (mpeg4_is_resync(ctx))
1280 if (mpeg4_is_resync(ctx)) {
1281 const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
1282 if (s->cbp_table[xy + delta])
1289 static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
1291 Mpeg4DecContext *ctx = (Mpeg4DecContext *)s;
1292 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1294 static int8_t quant_tab[4] = { -1, -2, 1, 2 };
1295 const int xy = s->mb_x + s->mb_y * s->mb_stride;
1297 av_assert2(s->h263_pred);
1299 if (s->pict_type == AV_PICTURE_TYPE_P ||
1300 s->pict_type == AV_PICTURE_TYPE_S) {
1302 if (get_bits1(&s->gb)) {
1305 for (i = 0; i < 6; i++)
1306 s->block_last_index[i] = -1;
1307 s->mv_dir = MV_DIR_FORWARD;
1308 s->mv_type = MV_TYPE_16X16;
1309 if (s->pict_type == AV_PICTURE_TYPE_S &&
1310 ctx->vol_sprite_usage == GMC_SPRITE) {
1311 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1316 s->mv[0][0][0] = get_amv(ctx, 0);
1317 s->mv[0][0][1] = get_amv(ctx, 1);
1320 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1330 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
1332 av_log(s->avctx, AV_LOG_ERROR,
1333 "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1336 } while (cbpc == 20);
1338 s->dsp.clear_blocks(s->block[0]);
1340 s->mb_intra = ((cbpc & 4) != 0);
1344 if (s->pict_type == AV_PICTURE_TYPE_S &&
1345 ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
1346 s->mcsel = get_bits1(&s->gb);
1349 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
1351 cbp = (cbpc & 3) | (cbpy << 2);
1353 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1354 if ((!s->progressive_sequence) &&
1355 (cbp || (s->workaround_bugs & FF_BUG_XVID_ILACE)))
1356 s->interlaced_dct = get_bits1(&s->gb);
1358 s->mv_dir = MV_DIR_FORWARD;
1359 if ((cbpc & 16) == 0) {
1361 s->current_picture.mb_type[xy] = MB_TYPE_GMC |
1364 /* 16x16 global motion prediction */
1365 s->mv_type = MV_TYPE_16X16;
1366 mx = get_amv(ctx, 0);
1367 my = get_amv(ctx, 1);
1368 s->mv[0][0][0] = mx;
1369 s->mv[0][0][1] = my;
1370 } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
1371 s->current_picture.mb_type[xy] = MB_TYPE_16x8 |
1374 /* 16x8 field motion prediction */
1375 s->mv_type = MV_TYPE_FIELD;
1377 s->field_select[0][0] = get_bits1(&s->gb);
1378 s->field_select[0][1] = get_bits1(&s->gb);
1380 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1382 for (i = 0; i < 2; i++) {
1383 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1387 my = ff_h263_decode_motion(s, pred_y / 2, s->f_code);
1391 s->mv[0][i][0] = mx;
1392 s->mv[0][i][1] = my;
1395 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1396 /* 16x16 motion prediction */
1397 s->mv_type = MV_TYPE_16X16;
1398 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1399 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1404 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1408 s->mv[0][0][0] = mx;
1409 s->mv[0][0][1] = my;
1412 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
1413 s->mv_type = MV_TYPE_8X8;
1414 for (i = 0; i < 4; i++) {
1415 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1416 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1420 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1423 s->mv[0][i][0] = mx;
1424 s->mv[0][i][1] = my;
1429 } else if (s->pict_type == AV_PICTURE_TYPE_B) {
1430 int modb1; // first bit of modb
1431 int modb2; // second bit of modb
1434 s->mb_intra = 0; // B-frames never contain intra blocks
1435 s->mcsel = 0; // ... true gmc blocks
1438 for (i = 0; i < 2; i++) {
1439 s->last_mv[i][0][0] =
1440 s->last_mv[i][0][1] =
1441 s->last_mv[i][1][0] =
1442 s->last_mv[i][1][1] = 0;
1445 ff_thread_await_progress(&s->next_picture_ptr->tf, s->mb_y, 0);
1448 /* if we skipped it in the future P Frame than skip it now too */
1449 s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
1451 if (s->mb_skipped) {
1453 for (i = 0; i < 6; i++)
1454 s->block_last_index[i] = -1;
1456 s->mv_dir = MV_DIR_FORWARD;
1457 s->mv_type = MV_TYPE_16X16;
1462 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1468 modb1 = get_bits1(&s->gb);
1470 // like MB_TYPE_B_DIRECT but no vectors coded
1471 mb_type = MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1;
1474 modb2 = get_bits1(&s->gb);
1475 mb_type = get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
1477 av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
1480 mb_type = mb_type_b_map[mb_type];
1484 s->dsp.clear_blocks(s->block[0]);
1485 cbp = get_bits(&s->gb, 6);
1488 if ((!IS_DIRECT(mb_type)) && cbp) {
1489 if (get_bits1(&s->gb))
1490 ff_set_qscale(s, s->qscale + get_bits1(&s->gb) * 4 - 2);
1493 if (!s->progressive_sequence) {
1495 s->interlaced_dct = get_bits1(&s->gb);
1497 if (!IS_DIRECT(mb_type) && get_bits1(&s->gb)) {
1498 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1499 mb_type &= ~MB_TYPE_16x16;
1501 if (USES_LIST(mb_type, 0)) {
1502 s->field_select[0][0] = get_bits1(&s->gb);
1503 s->field_select[0][1] = get_bits1(&s->gb);
1505 if (USES_LIST(mb_type, 1)) {
1506 s->field_select[1][0] = get_bits1(&s->gb);
1507 s->field_select[1][1] = get_bits1(&s->gb);
1513 if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
1514 s->mv_type = MV_TYPE_16X16;
1516 if (USES_LIST(mb_type, 0)) {
1517 s->mv_dir = MV_DIR_FORWARD;
1519 mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1520 my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1521 s->last_mv[0][1][0] =
1522 s->last_mv[0][0][0] =
1523 s->mv[0][0][0] = mx;
1524 s->last_mv[0][1][1] =
1525 s->last_mv[0][0][1] =
1526 s->mv[0][0][1] = my;
1529 if (USES_LIST(mb_type, 1)) {
1530 s->mv_dir |= MV_DIR_BACKWARD;
1532 mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1533 my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1534 s->last_mv[1][1][0] =
1535 s->last_mv[1][0][0] =
1536 s->mv[1][0][0] = mx;
1537 s->last_mv[1][1][1] =
1538 s->last_mv[1][0][1] =
1539 s->mv[1][0][1] = my;
1541 } else if (!IS_DIRECT(mb_type)) {
1542 s->mv_type = MV_TYPE_FIELD;
1544 if (USES_LIST(mb_type, 0)) {
1545 s->mv_dir = MV_DIR_FORWARD;
1547 for (i = 0; i < 2; i++) {
1548 mx = ff_h263_decode_motion(s, s->last_mv[0][i][0], s->f_code);
1549 my = ff_h263_decode_motion(s, s->last_mv[0][i][1] / 2, s->f_code);
1550 s->last_mv[0][i][0] =
1551 s->mv[0][i][0] = mx;
1552 s->last_mv[0][i][1] = (s->mv[0][i][1] = my) * 2;
1556 if (USES_LIST(mb_type, 1)) {
1557 s->mv_dir |= MV_DIR_BACKWARD;
1559 for (i = 0; i < 2; i++) {
1560 mx = ff_h263_decode_motion(s, s->last_mv[1][i][0], s->b_code);
1561 my = ff_h263_decode_motion(s, s->last_mv[1][i][1] / 2, s->b_code);
1562 s->last_mv[1][i][0] =
1563 s->mv[1][i][0] = mx;
1564 s->last_mv[1][i][1] = (s->mv[1][i][1] = my) * 2;
1570 if (IS_DIRECT(mb_type)) {
1571 if (IS_SKIP(mb_type)) {
1575 mx = ff_h263_decode_motion(s, 0, 1);
1576 my = ff_h263_decode_motion(s, 0, 1);
1579 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1580 mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
1582 s->current_picture.mb_type[xy] = mb_type;
1583 } else { /* I-Frame */
1585 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
1587 av_log(s->avctx, AV_LOG_ERROR,
1588 "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1591 } while (cbpc == 8);
1597 s->ac_pred = get_bits1(&s->gb);
1599 s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
1601 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
1603 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1605 av_log(s->avctx, AV_LOG_ERROR,
1606 "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1609 cbp = (cbpc & 3) | (cbpy << 2);
1611 ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1614 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1616 if (!s->progressive_sequence)
1617 s->interlaced_dct = get_bits1(&s->gb);
1619 s->dsp.clear_blocks(s->block[0]);
1620 /* decode each block */
1621 for (i = 0; i < 6; i++) {
1622 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 1, 0) < 0)
1629 /* decode each block */
1630 for (i = 0; i < 6; i++) {
1631 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 0, 0) < 0)
1637 /* per-MB end of slice check */
1638 if (s->codec_id == AV_CODEC_ID_MPEG4) {
1639 int next = mpeg4_is_resync(ctx);
1641 if (s->mb_x + s->mb_y*s->mb_width + 1 > next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
1643 } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
1646 if (s->pict_type == AV_PICTURE_TYPE_B) {
1647 const int delta= s->mb_x + 1 == s->mb_width ? 2 : 1;
1648 ff_thread_await_progress(&s->next_picture_ptr->tf,
1649 (s->mb_x + delta >= s->mb_width)
1650 ? FFMIN(s->mb_y + 1, s->mb_height - 1)
1652 if (s->next_picture.mbskip_table[xy + delta])
1663 static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
1665 int hours, minutes, seconds;
1667 if (!show_bits(gb, 23)) {
1668 av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
1672 hours = get_bits(gb, 5);
1673 minutes = get_bits(gb, 6);
1675 seconds = get_bits(gb, 6);
1677 s->time_base = seconds + 60*(minutes + 60*hours);
1685 static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb)
1688 s->avctx->profile = get_bits(gb, 4);
1689 s->avctx->level = get_bits(gb, 4);
1691 // for Simple profile, level 0
1692 if (s->avctx->profile == 0 && s->avctx->level == 8) {
1693 s->avctx->level = 0;
1699 static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
1701 MpegEncContext *s = &ctx->m;
1702 int width, height, vo_ver_id;
1705 skip_bits(gb, 1); /* random access */
1706 s->vo_type = get_bits(gb, 8);
1707 if (get_bits1(gb) != 0) { /* is_ol_id */
1708 vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
1709 skip_bits(gb, 3); /* vo_priority */
1713 s->aspect_ratio_info = get_bits(gb, 4);
1714 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
1715 s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
1716 s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
1718 s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
1721 if ((s->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
1722 int chroma_format = get_bits(gb, 2);
1723 if (chroma_format != CHROMA_420)
1724 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
1726 s->low_delay = get_bits1(gb);
1727 if (get_bits1(gb)) { /* vbv parameters */
1728 get_bits(gb, 15); /* first_half_bitrate */
1729 skip_bits1(gb); /* marker */
1730 get_bits(gb, 15); /* latter_half_bitrate */
1731 skip_bits1(gb); /* marker */
1732 get_bits(gb, 15); /* first_half_vbv_buffer_size */
1733 skip_bits1(gb); /* marker */
1734 get_bits(gb, 3); /* latter_half_vbv_buffer_size */
1735 get_bits(gb, 11); /* first_half_vbv_occupancy */
1736 skip_bits1(gb); /* marker */
1737 get_bits(gb, 15); /* latter_half_vbv_occupancy */
1738 skip_bits1(gb); /* marker */
1741 /* is setting low delay flag only once the smartest thing to do?
1742 * low delay detection won't be overriden. */
1743 if (s->picture_number == 0)
1747 ctx->shape = get_bits(gb, 2); /* vol shape */
1748 if (ctx->shape != RECT_SHAPE)
1749 av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
1750 if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
1751 av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
1752 skip_bits(gb, 4); /* video_object_layer_shape_extension */
1755 check_marker(gb, "before time_increment_resolution");
1757 s->avctx->time_base.den = get_bits(gb, 16);
1758 if (!s->avctx->time_base.den) {
1759 av_log(s->avctx, AV_LOG_ERROR, "time_base.den==0\n");
1760 s->avctx->time_base.num = 0;
1764 ctx->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
1765 if (ctx->time_increment_bits < 1)
1766 ctx->time_increment_bits = 1;
1768 check_marker(gb, "before fixed_vop_rate");
1770 if (get_bits1(gb) != 0) /* fixed_vop_rate */
1771 s->avctx->time_base.num = get_bits(gb, ctx->time_increment_bits);
1773 s->avctx->time_base.num = 1;
1777 if (ctx->shape != BIN_ONLY_SHAPE) {
1778 if (ctx->shape == RECT_SHAPE) {
1779 check_marker(gb, "before width");
1780 width = get_bits(gb, 13);
1781 check_marker(gb, "before height");
1782 height = get_bits(gb, 13);
1783 check_marker(gb, "after height");
1784 if (width && height && /* they should be non zero but who knows */
1785 !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
1786 if (s->width && s->height &&
1787 (s->width != width || s->height != height))
1788 s->context_reinit = 1;
1794 s->progressive_sequence =
1795 s->progressive_frame = get_bits1(gb) ^ 1;
1796 s->interlaced_dct = 0;
1797 if (!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
1798 av_log(s->avctx, AV_LOG_INFO, /* OBMC Disable */
1799 "MPEG4 OBMC not supported (very likely buggy encoder)\n");
1801 ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
1803 ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
1805 if (ctx->vol_sprite_usage == STATIC_SPRITE)
1806 av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
1807 if (ctx->vol_sprite_usage == STATIC_SPRITE ||
1808 ctx->vol_sprite_usage == GMC_SPRITE) {
1809 if (ctx->vol_sprite_usage == STATIC_SPRITE) {
1810 skip_bits(gb, 13); // sprite_width
1811 skip_bits1(gb); /* marker */
1812 skip_bits(gb, 13); // sprite_height
1813 skip_bits1(gb); /* marker */
1814 skip_bits(gb, 13); // sprite_left
1815 skip_bits1(gb); /* marker */
1816 skip_bits(gb, 13); // sprite_top
1817 skip_bits1(gb); /* marker */
1819 ctx->num_sprite_warping_points = get_bits(gb, 6);
1820 if (ctx->num_sprite_warping_points > 3) {
1821 av_log(s->avctx, AV_LOG_ERROR,
1822 "%d sprite_warping_points\n",
1823 ctx->num_sprite_warping_points);
1824 ctx->num_sprite_warping_points = 0;
1827 s->sprite_warping_accuracy = get_bits(gb, 2);
1828 ctx->sprite_brightness_change = get_bits1(gb);
1829 if (ctx->vol_sprite_usage == STATIC_SPRITE)
1830 skip_bits1(gb); // low_latency_sprite
1832 // FIXME sadct disable bit if verid!=1 && shape not rect
1834 if (get_bits1(gb) == 1) { /* not_8_bit */
1835 s->quant_precision = get_bits(gb, 4); /* quant_precision */
1836 if (get_bits(gb, 4) != 8) /* bits_per_pixel */
1837 av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
1838 if (s->quant_precision != 5)
1839 av_log(s->avctx, AV_LOG_ERROR,
1840 "quant precision %d\n", s->quant_precision);
1841 if (s->quant_precision<3 || s->quant_precision>9) {
1842 s->quant_precision = 5;
1845 s->quant_precision = 5;
1848 // FIXME a bunch of grayscale shape things
1850 if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
1853 /* load default matrixes */
1854 for (i = 0; i < 64; i++) {
1855 int j = s->dsp.idct_permutation[i];
1856 v = ff_mpeg4_default_intra_matrix[i];
1857 s->intra_matrix[j] = v;
1858 s->chroma_intra_matrix[j] = v;
1860 v = ff_mpeg4_default_non_intra_matrix[i];
1861 s->inter_matrix[j] = v;
1862 s->chroma_inter_matrix[j] = v;
1865 /* load custom intra matrix */
1866 if (get_bits1(gb)) {
1868 for (i = 0; i < 64; i++) {
1870 v = get_bits(gb, 8);
1875 j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1876 s->intra_matrix[j] = last;
1877 s->chroma_intra_matrix[j] = last;
1880 /* replicate last value */
1881 for (; i < 64; i++) {
1882 int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1883 s->intra_matrix[j] = last;
1884 s->chroma_intra_matrix[j] = last;
1888 /* load custom non intra matrix */
1889 if (get_bits1(gb)) {
1891 for (i = 0; i < 64; i++) {
1893 v = get_bits(gb, 8);
1898 j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1899 s->inter_matrix[j] = v;
1900 s->chroma_inter_matrix[j] = v;
1903 /* replicate last value */
1904 for (; i < 64; i++) {
1905 int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1906 s->inter_matrix[j] = last;
1907 s->chroma_inter_matrix[j] = last;
1911 // FIXME a bunch of grayscale shape things
1915 s->quarter_sample = get_bits1(gb);
1917 s->quarter_sample = 0;
1919 if (!get_bits1(gb)) {
1920 int pos = get_bits_count(gb);
1921 int estimation_method = get_bits(gb, 2);
1922 if (estimation_method < 2) {
1923 if (!get_bits1(gb)) {
1924 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */
1925 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */
1926 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */
1927 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */
1928 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */
1929 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upampling */
1931 if (!get_bits1(gb)) {
1932 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */
1933 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */
1934 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */
1935 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */
1937 if (!check_marker(gb, "in complexity estimation part 1")) {
1938 skip_bits_long(gb, pos - get_bits_count(gb));
1941 if (!get_bits1(gb)) {
1942 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */
1943 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */
1944 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */
1945 ctx->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */
1947 if (!get_bits1(gb)) {
1948 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */
1949 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */
1950 ctx->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */
1951 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */
1952 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */
1953 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */
1955 if (!check_marker(gb, "in complexity estimation part 2")) {
1956 skip_bits_long(gb, pos - get_bits_count(gb));
1959 if (estimation_method == 1) {
1960 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */
1961 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */
1964 av_log(s->avctx, AV_LOG_ERROR,
1965 "Invalid Complexity estimation method %d\n",
1970 ctx->cplx_estimation_trash_i =
1971 ctx->cplx_estimation_trash_p =
1972 ctx->cplx_estimation_trash_b = 0;
1975 ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
1977 s->data_partitioning = get_bits1(gb);
1978 if (s->data_partitioning)
1979 ctx->rvlc = get_bits1(gb);
1981 if (vo_ver_id != 1) {
1982 ctx->new_pred = get_bits1(gb);
1983 if (ctx->new_pred) {
1984 av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
1985 skip_bits(gb, 2); /* requested upstream message type */
1986 skip_bits1(gb); /* newpred segment type */
1988 if (get_bits1(gb)) // reduced_res_vop
1989 av_log(s->avctx, AV_LOG_ERROR,
1990 "reduced resolution VOP not supported\n");
1995 ctx->scalability = get_bits1(gb);
1997 if (ctx->scalability) {
1998 GetBitContext bak = *gb;
1999 int h_sampling_factor_n;
2000 int h_sampling_factor_m;
2001 int v_sampling_factor_n;
2002 int v_sampling_factor_m;
2004 skip_bits1(gb); // hierarchy_type
2005 skip_bits(gb, 4); /* ref_layer_id */
2006 skip_bits1(gb); /* ref_layer_sampling_dir */
2007 h_sampling_factor_n = get_bits(gb, 5);
2008 h_sampling_factor_m = get_bits(gb, 5);
2009 v_sampling_factor_n = get_bits(gb, 5);
2010 v_sampling_factor_m = get_bits(gb, 5);
2011 ctx->enhancement_type = get_bits1(gb);
2013 if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
2014 v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
2015 /* illegal scalability header (VERY broken encoder),
2016 * trying to workaround */
2017 ctx->scalability = 0;
2020 av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
2022 // bin shape stuff FIXME
2026 if (s->avctx->debug&FF_DEBUG_PICT_INFO) {
2027 av_log(s->avctx, AV_LOG_DEBUG, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d, %s%s%s%s\n",
2028 s->avctx->time_base.num, s->avctx->time_base.den,
2029 ctx->time_increment_bits,
2031 s->progressive_sequence,
2032 ctx->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
2033 s->data_partitioning ? "partition " : "", ctx->rvlc ? "rvlc " : ""
2041 * Decode the user data stuff in the header.
2042 * Also initializes divx/xvid/lavc_version/build.
2044 static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
2046 MpegEncContext *s = &ctx->m;
2050 int ver = 0, build = 0, ver2 = 0, ver3 = 0;
2053 for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
2054 if (show_bits(gb, 23) == 0)
2056 buf[i] = get_bits(gb, 8);
2060 /* divx detection */
2061 e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
2063 e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
2065 ctx->divx_version = ver;
2066 ctx->divx_build = build;
2067 s->divx_packed = e == 3 && last == 'p';
2068 if (s->divx_packed && !ctx->showed_packed_warning) {
2069 av_log(s->avctx, AV_LOG_INFO, "Video uses a non-standard and "
2070 "wasteful way to store B-frames ('packed B-frames'). "
2071 "Consider using a tool like VirtualDub or avidemux to fix it.\n");
2072 ctx->showed_packed_warning = 1;
2076 /* libavcodec detection */
2077 e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
2079 e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
2081 e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
2083 build = (ver << 16) + (ver2 << 8) + ver3;
2086 if (strcmp(buf, "ffmpeg") == 0)
2087 ctx->lavc_build = 4600;
2090 ctx->lavc_build = build;
2092 /* Xvid detection */
2093 e = sscanf(buf, "XviD%d", &build);
2095 ctx->xvid_build = build;
2100 int ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
2102 Mpeg4DecContext *ctx = avctx->priv_data;
2103 MpegEncContext *s = &ctx->m;
2105 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
2106 if (s->stream_codec_tag == AV_RL32("XVID") ||
2107 s->codec_tag == AV_RL32("XVID") ||
2108 s->codec_tag == AV_RL32("XVIX") ||
2109 s->codec_tag == AV_RL32("RMP4") ||
2110 s->codec_tag == AV_RL32("ZMP4") ||
2111 s->codec_tag == AV_RL32("SIPP"))
2112 ctx->xvid_build = 0;
2115 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
2116 if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
2117 s->vol_control_parameters == 0)
2118 ctx->divx_version = 400; // divx 4
2120 if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
2122 ctx->divx_build = -1;
2125 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
2126 if (s->codec_tag == AV_RL32("XVIX"))
2127 s->workaround_bugs |= FF_BUG_XVID_ILACE;
2129 if (s->codec_tag == AV_RL32("UMP4"))
2130 s->workaround_bugs |= FF_BUG_UMP4;
2132 if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
2133 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2135 if (ctx->divx_version > 502 && ctx->divx_build < 1814)
2136 s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
2138 if (ctx->xvid_build <= 3U)
2139 s->padding_bug_score = 256 * 256 * 256 * 64;
2141 if (ctx->xvid_build <= 1U)
2142 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2144 if (ctx->xvid_build <= 12U)
2145 s->workaround_bugs |= FF_BUG_EDGE;
2147 if (ctx->xvid_build <= 32U)
2148 s->workaround_bugs |= FF_BUG_DC_CLIP;
2150 #define SET_QPEL_FUNC(postfix1, postfix2) \
2151 s->dsp.put_ ## postfix1 = ff_put_ ## postfix2; \
2152 s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
2153 s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
2155 if (ctx->lavc_build < 4653U)
2156 s->workaround_bugs |= FF_BUG_STD_QPEL;
2158 if (ctx->lavc_build < 4655U)
2159 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2161 if (ctx->lavc_build < 4670U)
2162 s->workaround_bugs |= FF_BUG_EDGE;
2164 if (ctx->lavc_build <= 4712U)
2165 s->workaround_bugs |= FF_BUG_DC_CLIP;
2167 if (ctx->divx_version >= 0)
2168 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2169 if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
2170 s->padding_bug_score = 256 * 256 * 256 * 64;
2172 if (ctx->divx_version < 500U)
2173 s->workaround_bugs |= FF_BUG_EDGE;
2175 if (ctx->divx_version >= 0)
2176 s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
2179 if (s->workaround_bugs & FF_BUG_STD_QPEL) {
2180 SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
2181 SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
2182 SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
2183 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
2184 SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
2185 SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
2187 SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
2188 SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
2189 SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
2190 SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
2191 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
2192 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
2195 if (avctx->debug & FF_DEBUG_BUGS)
2196 av_log(s->avctx, AV_LOG_DEBUG,
2197 "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
2198 s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
2199 ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
2202 if (s->codec_id == AV_CODEC_ID_MPEG4 && ctx->xvid_build >= 0 &&
2203 avctx->idct_algo == FF_IDCT_AUTO &&
2204 (av_get_cpu_flags() & AV_CPU_FLAG_MMX)) {
2205 avctx->idct_algo = FF_IDCT_XVIDMMX;
2206 ff_dct_common_init(s);
2213 static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2215 MpegEncContext *s = &ctx->m;
2216 int time_incr, time_increment;
2219 s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
2220 if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
2221 s->vol_control_parameters == 0 && !(s->flags & CODEC_FLAG_LOW_DELAY)) {
2222 av_log(s->avctx, AV_LOG_ERROR, "low_delay flag incorrectly, clearing it\n");
2226 s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
2227 if (s->partitioned_frame)
2228 s->decode_mb = mpeg4_decode_partitioned_mb;
2230 s->decode_mb = mpeg4_decode_mb;
2233 while (get_bits1(gb) != 0)
2236 check_marker(gb, "before time_increment");
2238 if (ctx->time_increment_bits == 0 ||
2239 !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
2240 av_log(s->avctx, AV_LOG_ERROR,
2241 "hmm, seems the headers are not complete, trying to guess time_increment_bits\n");
2243 for (ctx->time_increment_bits = 1;
2244 ctx->time_increment_bits < 16;
2245 ctx->time_increment_bits++) {
2246 if (s->pict_type == AV_PICTURE_TYPE_P ||
2247 (s->pict_type == AV_PICTURE_TYPE_S &&
2248 ctx->vol_sprite_usage == GMC_SPRITE)) {
2249 if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
2251 } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
2255 av_log(s->avctx, AV_LOG_ERROR,
2256 "my guess is %d bits ;)\n", ctx->time_increment_bits);
2257 if (s->avctx->time_base.den && 4*s->avctx->time_base.den < 1<<ctx->time_increment_bits) {
2258 s->avctx->time_base.den = 1<<ctx->time_increment_bits;
2263 time_increment = get_bits1(gb); // FIXME investigate further
2265 time_increment = get_bits(gb, ctx->time_increment_bits);
2267 if (s->pict_type != AV_PICTURE_TYPE_B) {
2268 s->last_time_base = s->time_base;
2269 s->time_base += time_incr;
2270 s->time = s->time_base * s->avctx->time_base.den + time_increment;
2271 if (s->workaround_bugs & FF_BUG_UMP4) {
2272 if (s->time < s->last_non_b_time) {
2273 /* header is not mpeg-4-compatible, broken encoder,
2274 * trying to workaround */
2276 s->time += s->avctx->time_base.den;
2279 s->pp_time = s->time - s->last_non_b_time;
2280 s->last_non_b_time = s->time;
2282 s->time = (s->last_time_base + time_incr) * s->avctx->time_base.den + time_increment;
2283 s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
2284 if (s->pp_time <= s->pb_time ||
2285 s->pp_time <= s->pp_time - s->pb_time ||
2287 /* messed up order, maybe after seeking? skipping current b-frame */
2288 return FRAME_SKIPPED;
2290 ff_mpeg4_init_direct_mv(s);
2292 if (ctx->t_frame == 0)
2293 ctx->t_frame = s->pb_time;
2294 if (ctx->t_frame == 0)
2295 ctx->t_frame = 1; // 1/0 protection
2296 s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
2297 ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2298 s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
2299 ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2300 if (!s->progressive_sequence) {
2301 if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1)
2302 return FRAME_SKIPPED;
2306 if (s->avctx->time_base.num)
2307 pts = ROUNDED_DIV(s->time, s->avctx->time_base.num);
2309 pts = AV_NOPTS_VALUE;
2310 if (s->avctx->debug&FF_DEBUG_PTS)
2311 av_log(s->avctx, AV_LOG_DEBUG, "MPEG4 PTS: %"PRId64"\n",
2314 check_marker(gb, "before vop_coded");
2317 if (get_bits1(gb) != 1) {
2318 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2319 av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
2320 return FRAME_SKIPPED;
2323 decode_new_pred(ctx, gb);
2325 if (ctx->shape != BIN_ONLY_SHAPE &&
2326 (s->pict_type == AV_PICTURE_TYPE_P ||
2327 (s->pict_type == AV_PICTURE_TYPE_S &&
2328 ctx->vol_sprite_usage == GMC_SPRITE))) {
2329 /* rounding type for motion estimation */
2330 s->no_rounding = get_bits1(gb);
2334 // FIXME reduced res stuff
2336 if (ctx->shape != RECT_SHAPE) {
2337 if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
2338 skip_bits(gb, 13); /* width */
2339 skip_bits1(gb); /* marker */
2340 skip_bits(gb, 13); /* height */
2341 skip_bits1(gb); /* marker */
2342 skip_bits(gb, 13); /* hor_spat_ref */
2343 skip_bits1(gb); /* marker */
2344 skip_bits(gb, 13); /* ver_spat_ref */
2346 skip_bits1(gb); /* change_CR_disable */
2348 if (get_bits1(gb) != 0)
2349 skip_bits(gb, 8); /* constant_alpha_value */
2352 // FIXME complexity estimation stuff
2354 if (ctx->shape != BIN_ONLY_SHAPE) {
2355 skip_bits_long(gb, ctx->cplx_estimation_trash_i);
2356 if (s->pict_type != AV_PICTURE_TYPE_I)
2357 skip_bits_long(gb, ctx->cplx_estimation_trash_p);
2358 if (s->pict_type == AV_PICTURE_TYPE_B)
2359 skip_bits_long(gb, ctx->cplx_estimation_trash_b);
2361 if (get_bits_left(gb) < 3) {
2362 av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
2365 ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
2366 if (!s->progressive_sequence) {
2367 s->top_field_first = get_bits1(gb);
2368 s->alternate_scan = get_bits1(gb);
2370 s->alternate_scan = 0;
2373 if (s->alternate_scan) {
2374 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
2375 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
2376 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
2377 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2379 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
2380 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
2381 ff_init_scantable(s->dsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
2382 ff_init_scantable(s->dsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2385 if (s->pict_type == AV_PICTURE_TYPE_S &&
2386 (ctx->vol_sprite_usage == STATIC_SPRITE ||
2387 ctx->vol_sprite_usage == GMC_SPRITE)) {
2388 if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
2389 return AVERROR_INVALIDDATA;
2390 if (ctx->sprite_brightness_change)
2391 av_log(s->avctx, AV_LOG_ERROR,
2392 "sprite_brightness_change not supported\n");
2393 if (ctx->vol_sprite_usage == STATIC_SPRITE)
2394 av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
2397 if (ctx->shape != BIN_ONLY_SHAPE) {
2398 s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision);
2399 if (s->qscale == 0) {
2400 av_log(s->avctx, AV_LOG_ERROR,
2401 "Error, header damaged or not MPEG4 header (qscale=0)\n");
2402 return -1; // makes no sense to continue, as there is nothing left from the image then
2405 if (s->pict_type != AV_PICTURE_TYPE_I) {
2406 s->f_code = get_bits(gb, 3); /* fcode_for */
2407 if (s->f_code == 0) {
2408 av_log(s->avctx, AV_LOG_ERROR,
2409 "Error, header damaged or not MPEG4 header (f_code=0)\n");
2411 return -1; // makes no sense to continue, as there is nothing left from the image then
2416 if (s->pict_type == AV_PICTURE_TYPE_B) {
2417 s->b_code = get_bits(gb, 3);
2418 if (s->b_code == 0) {
2419 av_log(s->avctx, AV_LOG_ERROR,
2420 "Error, header damaged or not MPEG4 header (b_code=0)\n");
2422 return -1; // makes no sense to continue, as the MV decoding will break very quickly
2427 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2428 av_log(s->avctx, AV_LOG_DEBUG,
2429 "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",
2430 s->qscale, s->f_code, s->b_code,
2431 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")),
2432 gb->size_in_bits,s->progressive_sequence, s->alternate_scan,
2433 s->top_field_first, s->quarter_sample ? "q" : "h",
2434 s->data_partitioning, ctx->resync_marker,
2435 ctx->num_sprite_warping_points, s->sprite_warping_accuracy,
2436 1 - s->no_rounding, s->vo_type,
2437 s->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
2438 ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
2439 ctx->cplx_estimation_trash_b,
2445 if (!ctx->scalability) {
2446 if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
2447 skip_bits1(gb); // vop shape coding type
2449 if (ctx->enhancement_type) {
2450 int load_backward_shape = get_bits1(gb);
2451 if (load_backward_shape)
2452 av_log(s->avctx, AV_LOG_ERROR,
2453 "load backward shape isn't supported\n");
2455 skip_bits(gb, 2); // ref_select_code
2458 /* detect buggy encoders which don't set the low_delay flag
2459 * (divx4/xvid/opendivx). Note we cannot detect divx5 without b-frames
2460 * easily (although it's buggy too) */
2461 if (s->vo_type == 0 && s->vol_control_parameters == 0 &&
2462 ctx->divx_version == -1 && s->picture_number == 0) {
2463 av_log(s->avctx, AV_LOG_WARNING,
2464 "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
2468 s->picture_number++; // better than pic number==0 always ;)
2470 // FIXME add short header support
2471 s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
2472 s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
2474 if (s->workaround_bugs & FF_BUG_EDGE) {
2475 s->h_edge_pos = s->width;
2476 s->v_edge_pos = s->height;
2482 * Decode mpeg4 headers.
2483 * @return <0 if no VOP found (or a damaged one)
2484 * FRAME_SKIPPED if a not coded VOP is found
2485 * 0 if a VOP is found
2487 int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2489 MpegEncContext *s = &ctx->m;
2490 unsigned startcode, v;
2492 /* search next start code */
2495 if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
2497 if (get_bits(gb, 8) == 0xF0)
2503 if (get_bits_count(gb) >= gb->size_in_bits) {
2504 if (gb->size_in_bits == 8 &&
2505 (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
2506 av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
2507 return FRAME_SKIPPED; // divx bug
2509 return -1; // end of stream
2512 /* use the bits after the test */
2513 v = get_bits(gb, 8);
2514 startcode = ((startcode << 8) | v) & 0xffffffff;
2516 if ((startcode & 0xFFFFFF00) != 0x100)
2517 continue; // no startcode
2519 if (s->avctx->debug & FF_DEBUG_STARTCODE) {
2520 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
2521 if (startcode <= 0x11F)
2522 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
2523 else if (startcode <= 0x12F)
2524 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
2525 else if (startcode <= 0x13F)
2526 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2527 else if (startcode <= 0x15F)
2528 av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
2529 else if (startcode <= 0x1AF)
2530 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2531 else if (startcode == 0x1B0)
2532 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
2533 else if (startcode == 0x1B1)
2534 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
2535 else if (startcode == 0x1B2)
2536 av_log(s->avctx, AV_LOG_DEBUG, "User Data");
2537 else if (startcode == 0x1B3)
2538 av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
2539 else if (startcode == 0x1B4)
2540 av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
2541 else if (startcode == 0x1B5)
2542 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
2543 else if (startcode == 0x1B6)
2544 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
2545 else if (startcode == 0x1B7)
2546 av_log(s->avctx, AV_LOG_DEBUG, "slice start");
2547 else if (startcode == 0x1B8)
2548 av_log(s->avctx, AV_LOG_DEBUG, "extension start");
2549 else if (startcode == 0x1B9)
2550 av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
2551 else if (startcode == 0x1BA)
2552 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
2553 else if (startcode == 0x1BB)
2554 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
2555 else if (startcode == 0x1BC)
2556 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
2557 else if (startcode == 0x1BD)
2558 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
2559 else if (startcode == 0x1BE)
2560 av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
2561 else if (startcode == 0x1BF)
2562 av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
2563 else if (startcode == 0x1C0)
2564 av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
2565 else if (startcode == 0x1C1)
2566 av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
2567 else if (startcode == 0x1C2)
2568 av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
2569 else if (startcode == 0x1C3)
2570 av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
2571 else if (startcode <= 0x1C5)
2572 av_log(s->avctx, AV_LOG_DEBUG, "reserved");
2573 else if (startcode <= 0x1FF)
2574 av_log(s->avctx, AV_LOG_DEBUG, "System start");
2575 av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
2578 if (startcode >= 0x120 && startcode <= 0x12F) {
2579 if (decode_vol_header(ctx, gb) < 0)
2581 } else if (startcode == USER_DATA_STARTCODE) {
2582 decode_user_data(ctx, gb);
2583 } else if (startcode == GOP_STARTCODE) {
2584 mpeg4_decode_gop_header(s, gb);
2585 } else if (startcode == VOS_STARTCODE) {
2586 mpeg4_decode_profile_level(s, gb);
2587 } else if (startcode == VOP_STARTCODE) {
2596 if (s->flags & CODEC_FLAG_LOW_DELAY)
2598 s->avctx->has_b_frames = !s->low_delay;
2600 return decode_vop_header(ctx, gb);
2603 av_cold void ff_mpeg4videodec_static_init(void) {
2604 static int done = 0;
2607 ff_init_rl(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
2608 ff_init_rl(&ff_rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
2609 ff_init_rl(&ff_rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
2610 INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
2611 INIT_VLC_RL(ff_rvlc_rl_inter, 1072);
2612 INIT_VLC_RL(ff_rvlc_rl_intra, 1072);
2613 INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
2614 &ff_mpeg4_DCtab_lum[0][1], 2, 1,
2615 &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
2616 INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
2617 &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
2618 &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
2619 INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
2620 &ff_sprite_trajectory_tab[0][1], 4, 2,
2621 &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
2622 INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
2623 &ff_mb_type_b_tab[0][1], 2, 1,
2624 &ff_mb_type_b_tab[0][0], 2, 1, 16);
2629 int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
2631 Mpeg4DecContext *ctx = avctx->priv_data;
2632 MpegEncContext *s = &ctx->m;
2634 /* divx 5.01+ bitstream reorder stuff */
2635 /* Since this clobbers the input buffer and hwaccel codecs still need the
2636 * data during hwaccel->end_frame we should not do this any earlier */
2637 if (s->divx_packed) {
2638 int current_pos = s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb) >> 3);
2639 int startcode_found = 0;
2641 if (buf_size - current_pos > 7) {
2644 for (i = current_pos; i < buf_size - 4; i++)
2649 buf[i + 3] == 0xB6) {
2650 startcode_found = !(buf[i + 4] & 0x40);
2655 if (startcode_found) {
2656 av_fast_padded_malloc(&s->bitstream_buffer,
2657 &s->allocated_bitstream_buffer_size,
2658 buf_size - current_pos);
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,