3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "error_resilience.h"
26 #include "mpegutils.h"
27 #include "mpegvideo.h"
28 #include "mpegvideodata.h"
29 #include "mpeg4video.h"
35 /* The defines below define the number of bits that are read at once for
36 * reading vlc values. Changing these may improve speed and data cache needs
37 * be aware though that decreasing them may need the number of stages that is
38 * passed to get_vlc* to be increased. */
39 #define SPRITE_TRAJ_VLC_BITS 6
41 #define MB_TYPE_B_VLC_BITS 4
43 static VLC dc_lum, dc_chrom;
44 static VLC sprite_trajectory;
45 static VLC mb_type_b_vlc;
47 static const int mb_type_b_map[4] = {
48 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
49 MB_TYPE_L0L1 | MB_TYPE_16x16,
50 MB_TYPE_L1 | MB_TYPE_16x16,
51 MB_TYPE_L0 | MB_TYPE_16x16,
56 * @param n block index (0-3 are luma, 4-5 are chroma)
57 * @param dir the ac prediction direction
59 void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
62 int16_t *ac_val, *ac_val1;
63 int8_t *const qscale_table = s->current_picture.qscale_table;
66 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
70 const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
74 if (s->mb_x == 0 || s->qscale == qscale_table[xy] ||
77 for (i = 1; i < 8; i++)
78 block[s->idsp.idct_permutation[i << 3]] += ac_val[i];
80 /* different qscale, we must rescale */
81 for (i = 1; i < 8; i++)
82 block[s->idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
85 const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
87 ac_val -= 16 * s->block_wrap[n];
89 if (s->mb_y == 0 || s->qscale == qscale_table[xy] ||
92 for (i = 1; i < 8; i++)
93 block[s->idsp.idct_permutation[i]] += ac_val[i + 8];
95 /* different qscale, we must rescale */
96 for (i = 1; i < 8; i++)
97 block[s->idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
102 for (i = 1; i < 8; i++)
103 ac_val1[i] = block[s->idsp.idct_permutation[i << 3]];
106 for (i = 1; i < 8; i++)
107 ac_val1[8 + i] = block[s->idsp.idct_permutation[i]];
111 * check if the next stuff is a resync marker or the end.
114 static inline int mpeg4_is_resync(MpegEncContext *s)
116 int bits_count = get_bits_count(&s->gb);
117 int v = show_bits(&s->gb, 16);
119 if (s->workaround_bugs & FF_BUG_NO_PADDING)
123 if (s->pict_type == AV_PICTURE_TYPE_B ||
124 (v >> (8 - s->pict_type) != 1) || s->partitioned_frame)
126 skip_bits(&s->gb, 8 + s->pict_type);
127 bits_count += 8 + s->pict_type;
128 v = show_bits(&s->gb, 16);
131 if (bits_count + 8 >= s->gb.size_in_bits) {
133 v |= 0x7F >> (7 - (bits_count & 7));
138 if (v == ff_mpeg4_resync_prefix[bits_count & 7]) {
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))
151 if (len >= ff_mpeg4_get_video_packet_prefix_length(s))
158 static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
160 MpegEncContext *s = &ctx->m;
161 int a = 2 << s->sprite_warping_accuracy;
162 int rho = 3 - s->sprite_warping_accuracy;
168 int min_ab, i, w2, h2, w3, h3;
169 int sprite_ref[4][2];
170 int virtual_ref[2][2];
172 // only true for rectangle shapes
173 const int vop_ref[4][2] = { { 0, 0 }, { s->width, 0 },
174 { 0, s->height }, { s->width, s->height } };
175 int d[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
177 if (w <= 0 || h <= 0)
178 return AVERROR_INVALIDDATA;
180 for (i = 0; i < ctx->num_sprite_warping_points; i++) {
184 length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
186 x = get_xbits(gb, length);
188 if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
189 skip_bits1(gb); /* marker bit */
191 length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 3);
193 y = get_xbits(gb, length);
195 skip_bits1(gb); /* marker bit */
196 ctx->sprite_traj[i][0] = d[i][0] = x;
197 ctx->sprite_traj[i][1] = d[i][1] = y;
200 ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0;
202 while ((1 << alpha) < w)
204 while ((1 << beta) < h)
205 beta++; /* typo in the MPEG-4 std for the definition of w' and h' */
209 // Note, the 4th point isn't used for GMC
210 if (ctx->divx_version == 500 && ctx->divx_build == 413) {
211 sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
212 sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
213 sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
214 sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1];
215 sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0];
216 sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1];
218 sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]);
219 sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]);
220 sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]);
221 sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]);
222 sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]);
223 sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]);
225 /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
226 * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
228 /* This is mostly identical to the MPEG-4 std (and is totally unreadable
229 * because of that...). Perhaps it should be reordered to be more readable.
230 * The idea behind this virtual_ref mess is to be able to use shifts later
231 * per pixel instead of divides so the distance between points is converted
232 * from w&h based to w2&h2 based which are of the 2^x form. */
233 virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) +
234 ROUNDED_DIV(((w - w2) *
235 (r * sprite_ref[0][0] - 16 * vop_ref[0][0]) +
236 w2 * (r * sprite_ref[1][0] - 16 * vop_ref[1][0])), w);
237 virtual_ref[0][1] = 16 * vop_ref[0][1] +
238 ROUNDED_DIV(((w - w2) *
239 (r * sprite_ref[0][1] - 16 * vop_ref[0][1]) +
240 w2 * (r * sprite_ref[1][1] - 16 * vop_ref[1][1])), w);
241 virtual_ref[1][0] = 16 * vop_ref[0][0] +
242 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16 * vop_ref[0][0]) +
243 h2 * (r * sprite_ref[2][0] - 16 * vop_ref[2][0])), h);
244 virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) +
245 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16 * vop_ref[0][1]) +
246 h2 * (r * sprite_ref[2][1] - 16 * vop_ref[2][1])), h);
248 switch (ctx->num_sprite_warping_points) {
250 s->sprite_offset[0][0] =
251 s->sprite_offset[0][1] =
252 s->sprite_offset[1][0] =
253 s->sprite_offset[1][1] = 0;
254 s->sprite_delta[0][0] = a;
255 s->sprite_delta[0][1] =
256 s->sprite_delta[1][0] = 0;
257 s->sprite_delta[1][1] = a;
258 ctx->sprite_shift[0] =
259 ctx->sprite_shift[1] = 0;
262 s->sprite_offset[0][0] = sprite_ref[0][0] - a * vop_ref[0][0];
263 s->sprite_offset[0][1] = sprite_ref[0][1] - a * vop_ref[0][1];
264 s->sprite_offset[1][0] = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) -
265 a * (vop_ref[0][0] / 2);
266 s->sprite_offset[1][1] = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) -
267 a * (vop_ref[0][1] / 2);
268 s->sprite_delta[0][0] = a;
269 s->sprite_delta[0][1] =
270 s->sprite_delta[1][0] = 0;
271 s->sprite_delta[1][1] = a;
272 ctx->sprite_shift[0] =
273 ctx->sprite_shift[1] = 0;
276 s->sprite_offset[0][0] = (sprite_ref[0][0] << (alpha + rho)) +
277 (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
279 (r * sprite_ref[0][1] - virtual_ref[0][1]) *
280 (-vop_ref[0][1]) + (1 << (alpha + rho - 1));
281 s->sprite_offset[0][1] = (sprite_ref[0][1] << (alpha + rho)) +
282 (-r * sprite_ref[0][1] + virtual_ref[0][1]) *
284 (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
285 (-vop_ref[0][1]) + (1 << (alpha + rho - 1));
286 s->sprite_offset[1][0] = ((-r * sprite_ref[0][0] + virtual_ref[0][0]) *
287 (-2 * vop_ref[0][0] + 1) +
288 (r * sprite_ref[0][1] - virtual_ref[0][1]) *
289 (-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
290 sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1)));
291 s->sprite_offset[1][1] = ((-r * sprite_ref[0][1] + virtual_ref[0][1]) *
292 (-2 * vop_ref[0][0] + 1) +
293 (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
294 (-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
295 sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1)));
296 s->sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
297 s->sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]);
298 s->sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
299 s->sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
301 ctx->sprite_shift[0] = alpha + rho;
302 ctx->sprite_shift[1] = alpha + rho + 2;
305 min_ab = FFMIN(alpha, beta);
308 s->sprite_offset[0][0] = (sprite_ref[0][0] << (alpha + beta + rho - min_ab)) +
309 (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
310 h3 * (-vop_ref[0][0]) +
311 (-r * sprite_ref[0][0] + virtual_ref[1][0]) *
312 w3 * (-vop_ref[0][1]) +
313 (1 << (alpha + beta + rho - min_ab - 1));
314 s->sprite_offset[0][1] = (sprite_ref[0][1] << (alpha + beta + rho - min_ab)) +
315 (-r * sprite_ref[0][1] + virtual_ref[0][1]) *
316 h3 * (-vop_ref[0][0]) +
317 (-r * sprite_ref[0][1] + virtual_ref[1][1]) *
318 w3 * (-vop_ref[0][1]) +
319 (1 << (alpha + beta + rho - min_ab - 1));
320 s->sprite_offset[1][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]) *
321 h3 * (-2 * vop_ref[0][0] + 1) +
322 (-r * sprite_ref[0][0] + virtual_ref[1][0]) *
323 w3 * (-2 * vop_ref[0][1] + 1) + 2 * w2 * h3 *
324 r * sprite_ref[0][0] - 16 * w2 * h3 +
325 (1 << (alpha + beta + rho - min_ab + 1));
326 s->sprite_offset[1][1] = (-r * sprite_ref[0][1] + virtual_ref[0][1]) *
327 h3 * (-2 * vop_ref[0][0] + 1) +
328 (-r * sprite_ref[0][1] + virtual_ref[1][1]) *
329 w3 * (-2 * vop_ref[0][1] + 1) + 2 * w2 * h3 *
330 r * sprite_ref[0][1] - 16 * w2 * h3 +
331 (1 << (alpha + beta + rho - min_ab + 1));
332 s->sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3;
333 s->sprite_delta[0][1] = (-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3;
334 s->sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3;
335 s->sprite_delta[1][1] = (-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3;
337 ctx->sprite_shift[0] = alpha + beta + rho - min_ab;
338 ctx->sprite_shift[1] = alpha + beta + rho - min_ab + 2;
341 /* try to simplify the situation */
342 if (s->sprite_delta[0][0] == a << ctx->sprite_shift[0] &&
343 s->sprite_delta[0][1] == 0 &&
344 s->sprite_delta[1][0] == 0 &&
345 s->sprite_delta[1][1] == a << ctx->sprite_shift[0]) {
346 s->sprite_offset[0][0] >>= ctx->sprite_shift[0];
347 s->sprite_offset[0][1] >>= ctx->sprite_shift[0];
348 s->sprite_offset[1][0] >>= ctx->sprite_shift[1];
349 s->sprite_offset[1][1] >>= ctx->sprite_shift[1];
350 s->sprite_delta[0][0] = a;
351 s->sprite_delta[0][1] = 0;
352 s->sprite_delta[1][0] = 0;
353 s->sprite_delta[1][1] = a;
354 ctx->sprite_shift[0] = 0;
355 ctx->sprite_shift[1] = 0;
356 s->real_sprite_warping_points = 1;
358 int shift_y = 16 - ctx->sprite_shift[0];
359 int shift_c = 16 - ctx->sprite_shift[1];
360 for (i = 0; i < 2; i++) {
361 s->sprite_offset[0][i] <<= shift_y;
362 s->sprite_offset[1][i] <<= shift_c;
363 s->sprite_delta[0][i] <<= shift_y;
364 s->sprite_delta[1][i] <<= shift_y;
365 ctx->sprite_shift[i] = 16;
367 s->real_sprite_warping_points = ctx->num_sprite_warping_points;
374 * Decode the next video packet.
375 * @return <0 if something went wrong
377 int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
379 MpegEncContext *s = &ctx->m;
381 int mb_num_bits = av_log2(s->mb_num - 1) + 1;
382 int header_extension = 0, mb_num, len;
384 /* is there enough space left for a video packet + header */
385 if (get_bits_count(&s->gb) > s->gb.size_in_bits - 20)
388 for (len = 0; len < 32; len++)
389 if (get_bits1(&s->gb))
392 if (len != ff_mpeg4_get_video_packet_prefix_length(s)) {
393 av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
397 if (ctx->shape != RECT_SHAPE) {
398 header_extension = get_bits1(&s->gb);
399 // FIXME more stuff here
402 mb_num = get_bits(&s->gb, mb_num_bits);
403 if (mb_num >= s->mb_num) {
404 av_log(s->avctx, AV_LOG_ERROR,
405 "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
408 if (s->pict_type == AV_PICTURE_TYPE_B) {
409 int mb_x = 0, mb_y = 0;
411 while (s->next_picture.mbskip_table[s->mb_index2xy[mb_num]]) {
413 ff_thread_await_progress(&s->next_picture_ptr->tf, mb_y++, 0);
415 if (++mb_x == s->mb_width)
418 if (mb_num >= s->mb_num)
419 return -1; // slice contains just skipped MBs (already decoded)
422 s->mb_x = mb_num % s->mb_width;
423 s->mb_y = mb_num / s->mb_width;
425 if (ctx->shape != BIN_ONLY_SHAPE) {
426 int qscale = get_bits(&s->gb, s->quant_precision);
428 s->chroma_qscale = s->qscale = qscale;
431 if (ctx->shape == RECT_SHAPE)
432 header_extension = get_bits1(&s->gb);
434 if (header_extension) {
437 while (get_bits1(&s->gb) != 0)
440 check_marker(&s->gb, "before time_increment in video packed header");
441 skip_bits(&s->gb, ctx->time_increment_bits); /* time_increment */
442 check_marker(&s->gb, "before vop_coding_type in video packed header");
444 skip_bits(&s->gb, 2); /* vop coding type */
445 // FIXME not rect stuff here
447 if (ctx->shape != BIN_ONLY_SHAPE) {
448 skip_bits(&s->gb, 3); /* intra dc vlc threshold */
449 // FIXME don't just ignore everything
450 if (s->pict_type == AV_PICTURE_TYPE_S &&
451 ctx->vol_sprite_usage == GMC_SPRITE) {
452 if (mpeg4_decode_sprite_trajectory(ctx, &s->gb) < 0)
453 return AVERROR_INVALIDDATA;
454 av_log(s->avctx, AV_LOG_ERROR, "untested\n");
457 // FIXME reduced res stuff here
459 if (s->pict_type != AV_PICTURE_TYPE_I) {
460 int f_code = get_bits(&s->gb, 3); /* fcode_for */
462 av_log(s->avctx, AV_LOG_ERROR,
463 "Error, video packet header damaged (f_code=0)\n");
465 if (s->pict_type == AV_PICTURE_TYPE_B) {
466 int b_code = get_bits(&s->gb, 3);
468 av_log(s->avctx, AV_LOG_ERROR,
469 "Error, video packet header damaged (b_code=0)\n");
473 // FIXME new-pred stuff
479 * Get the average motion vector for a GMC MB.
480 * @param n either 0 for the x component or 1 for y
481 * @return the average MV for a GMC MB
483 static inline int get_amv(Mpeg4DecContext *ctx, int n)
485 MpegEncContext *s = &ctx->m;
486 int x, y, mb_v, sum, dx, dy, shift;
487 int len = 1 << (s->f_code + 4);
488 const int a = s->sprite_warping_accuracy;
490 if (s->workaround_bugs & FF_BUG_AMV)
491 len >>= s->quarter_sample;
493 if (s->real_sprite_warping_points == 1) {
494 if (ctx->divx_version == 500 && ctx->divx_build == 413)
495 sum = s->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
497 sum = RSHIFT(s->sprite_offset[0][n] << s->quarter_sample, a);
499 dx = s->sprite_delta[n][0];
500 dy = s->sprite_delta[n][1];
501 shift = ctx->sprite_shift[0];
503 dy -= 1 << (shift + a + 1);
505 dx -= 1 << (shift + a + 1);
506 mb_v = s->sprite_offset[0][n] + dx * s->mb_x * 16 + dy * s->mb_y * 16;
509 for (y = 0; y < 16; y++) {
514 for (x = 0; x < 16; x++) {
519 sum = RSHIFT(sum, a + 8 - s->quarter_sample);
531 * Decode the dc value.
532 * @param n block index (0-3 are luma, 4-5 are chroma)
533 * @param dir_ptr the prediction direction will be stored here
534 * @return the quantized dc
536 static inline int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
541 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
543 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
545 if (code < 0 || code > 9 /* && s->nbit < 9 */) {
546 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
555 level = 2 * get_bits1(&s->gb) - 1;
557 if (get_bits1(&s->gb))
558 level = get_bits(&s->gb, code - 1) + (1 << (code - 1));
560 level = -get_bits(&s->gb, code - 1) - (1 << (code - 1));
563 level = get_xbits(&s->gb, code);
567 if (get_bits1(&s->gb) == 0) { /* marker */
568 if (s->avctx->err_recognition & AV_EF_BITSTREAM) {
569 av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
576 return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
580 * Decode first partition.
581 * @return number of MBs decoded or <0 if an error occurred
583 static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
585 MpegEncContext *s = &ctx->m;
587 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
589 /* decode first partition */
590 s->first_slice_line = 1;
591 for (; s->mb_y < s->mb_height; s->mb_y++) {
592 ff_init_block_index(s);
593 for (; s->mb_x < s->mb_width; s->mb_x++) {
594 const int xy = s->mb_x + s->mb_y * s->mb_stride;
599 ff_update_block_index(s);
600 if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
601 s->first_slice_line = 0;
603 if (s->pict_type == AV_PICTURE_TYPE_I) {
607 if (show_bits_long(&s->gb, 19) == DC_MARKER)
610 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
612 av_log(s->avctx, AV_LOG_ERROR,
613 "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
618 s->cbp_table[xy] = cbpc & 3;
619 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
623 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
625 s->current_picture.qscale_table[xy] = s->qscale;
627 s->mbintra_table[xy] = 1;
628 for (i = 0; i < 6; i++) {
630 int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
632 av_log(s->avctx, AV_LOG_ERROR,
633 "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
640 s->pred_dir_table[xy] = dir;
641 } else { /* P/S_TYPE */
642 int mx, my, pred_x, pred_y, bits;
643 int16_t *const mot_val = s->current_picture.motion_val[0][s->block_index[0]];
644 const int stride = s->b8_stride * 2;
647 bits = show_bits(&s->gb, 17);
648 if (bits == MOTION_MARKER)
652 if (bits & 0x10000) {
654 if (s->pict_type == AV_PICTURE_TYPE_S &&
655 ctx->vol_sprite_usage == GMC_SPRITE) {
656 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
660 mx = get_amv(ctx, 0);
661 my = get_amv(ctx, 1);
663 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
670 mot_val[0 + stride] =
671 mot_val[2 + stride] = mx;
674 mot_val[1 + stride] =
675 mot_val[3 + stride] = my;
677 if (s->mbintra_table[xy])
678 ff_clean_intra_table_entries(s);
682 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
684 av_log(s->avctx, AV_LOG_ERROR,
685 "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
691 s->cbp_table[xy] = cbpc & (8 + 3); // 8 is dquant
693 s->mb_intra = ((cbpc & 4) != 0);
696 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
697 s->mbintra_table[xy] = 1;
700 mot_val[0 + stride] =
701 mot_val[2 + stride] = 0;
704 mot_val[1 + stride] =
705 mot_val[3 + stride] = 0;
707 if (s->mbintra_table[xy])
708 ff_clean_intra_table_entries(s);
710 if (s->pict_type == AV_PICTURE_TYPE_S &&
711 ctx->vol_sprite_usage == GMC_SPRITE &&
713 s->mcsel = get_bits1(&s->gb);
717 if ((cbpc & 16) == 0) {
718 /* 16x16 motion prediction */
720 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
722 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
726 my = ff_h263_decode_motion(s, pred_y, s->f_code);
729 s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
732 mx = get_amv(ctx, 0);
733 my = get_amv(ctx, 1);
734 s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
741 mot_val[0 + stride] =
742 mot_val[2 + stride] = mx;
745 mot_val[1 + stride] =
746 mot_val[3 + stride] = my;
749 s->current_picture.mb_type[xy] = MB_TYPE_8x8 |
751 for (i = 0; i < 4; i++) {
752 int16_t *mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
753 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
757 my = ff_h263_decode_motion(s, pred_y, s->f_code);
774 * decode second partition.
775 * @return <0 if an error occurred
777 static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
780 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
782 s->mb_x = s->resync_mb_x;
783 s->first_slice_line = 1;
784 for (s->mb_y = s->resync_mb_y; mb_num < mb_count; s->mb_y++) {
785 ff_init_block_index(s);
786 for (; mb_num < mb_count && s->mb_x < s->mb_width; s->mb_x++) {
787 const int xy = s->mb_x + s->mb_y * s->mb_stride;
790 ff_update_block_index(s);
791 if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1)
792 s->first_slice_line = 0;
794 if (s->pict_type == AV_PICTURE_TYPE_I) {
795 int ac_pred = get_bits1(&s->gb);
796 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
798 av_log(s->avctx, AV_LOG_ERROR,
799 "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
803 s->cbp_table[xy] |= cbpy << 2;
804 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
805 } else { /* P || S_TYPE */
806 if (IS_INTRA(s->current_picture.mb_type[xy])) {
809 int ac_pred = get_bits1(&s->gb);
810 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
813 av_log(s->avctx, AV_LOG_ERROR,
814 "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
818 if (s->cbp_table[xy] & 8)
819 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
820 s->current_picture.qscale_table[xy] = s->qscale;
822 for (i = 0; i < 6; i++) {
824 int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
826 av_log(s->avctx, AV_LOG_ERROR,
827 "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
834 s->cbp_table[xy] &= 3; // remove dquant
835 s->cbp_table[xy] |= cbpy << 2;
836 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
837 s->pred_dir_table[xy] = dir;
838 } else if (IS_SKIP(s->current_picture.mb_type[xy])) {
839 s->current_picture.qscale_table[xy] = s->qscale;
840 s->cbp_table[xy] = 0;
842 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
845 av_log(s->avctx, AV_LOG_ERROR,
846 "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
850 if (s->cbp_table[xy] & 8)
851 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
852 s->current_picture.qscale_table[xy] = s->qscale;
854 s->cbp_table[xy] &= 3; // remove dquant
855 s->cbp_table[xy] |= (cbpy ^ 0xf) << 2;
859 if (mb_num >= mb_count)
867 * Decode the first and second partition.
868 * @return <0 if error (and sets error type in the error_status_table)
870 int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx)
872 MpegEncContext *s = &ctx->m;
874 const int part_a_error = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR;
875 const int part_a_end = s->pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END | ER_MV_END) : ER_MV_END;
877 mb_num = mpeg4_decode_partition_a(ctx);
879 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
880 s->mb_x, s->mb_y, part_a_error);
884 if (s->resync_mb_x + s->resync_mb_y * s->mb_width + mb_num > s->mb_num) {
885 av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
886 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
887 s->mb_x, s->mb_y, part_a_error);
891 s->mb_num_left = mb_num;
893 if (s->pict_type == AV_PICTURE_TYPE_I) {
894 while (show_bits(&s->gb, 9) == 1)
895 skip_bits(&s->gb, 9);
896 if (get_bits_long(&s->gb, 19) != DC_MARKER) {
897 av_log(s->avctx, AV_LOG_ERROR,
898 "marker missing after first I partition at %d %d\n",
903 while (show_bits(&s->gb, 10) == 1)
904 skip_bits(&s->gb, 10);
905 if (get_bits(&s->gb, 17) != MOTION_MARKER) {
906 av_log(s->avctx, AV_LOG_ERROR,
907 "marker missing after first P partition at %d %d\n",
912 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
913 s->mb_x - 1, s->mb_y, part_a_end);
915 if (mpeg4_decode_partition_b(s, mb_num) < 0) {
916 if (s->pict_type == AV_PICTURE_TYPE_P)
917 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
918 s->mb_x, s->mb_y, ER_DC_ERROR);
921 if (s->pict_type == AV_PICTURE_TYPE_P)
922 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
923 s->mb_x - 1, s->mb_y, ER_DC_END);
931 * @return <0 if an error occurred
933 static inline int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block,
934 int n, int coded, int intra, int rvlc)
936 MpegEncContext *s = &ctx->m;
937 int level, i, last, run, qmul, qadd, dc_pred_dir;
940 const uint8_t *scan_table;
942 // Note intra & rvlc should be optimized away if this is inlined
945 if (ctx->use_intra_dc_vlc) {
947 if (s->partitioned_frame) {
948 level = s->dc_val[0][s->block_index[n]];
950 level = FASTDIV((level + (s->y_dc_scale >> 1)), s->y_dc_scale);
952 level = FASTDIV((level + (s->c_dc_scale >> 1)), s->c_dc_scale);
953 dc_pred_dir = (s->pred_dir_table[s->mb_x + s->mb_y * s->mb_stride] << n) & 32;
955 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
963 ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
969 rl = &ff_rvlc_rl_intra;
970 rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
972 rl = &ff_mpeg4_rl_intra;
973 rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
976 if (dc_pred_dir == 0)
977 scan_table = s->intra_v_scantable.permutated; /* left */
979 scan_table = s->intra_h_scantable.permutated; /* top */
981 scan_table = s->intra_scantable.permutated;
988 s->block_last_index[n] = i;
992 rl = &ff_rvlc_rl_inter;
994 rl = &ff_h263_rl_inter;
996 scan_table = s->intra_scantable.permutated;
1002 rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
1004 rl_vlc = ff_h263_rl_inter.rl_vlc[0];
1006 qmul = s->qscale << 1;
1007 qadd = (s->qscale - 1) | 1;
1009 rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
1011 rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
1015 OPEN_READER(re, &s->gb);
1017 UPDATE_CACHE(re, &s->gb);
1018 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1022 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1023 av_log(s->avctx, AV_LOG_ERROR,
1024 "1. marker bit missing in rvlc esc\n");
1027 SKIP_CACHE(re, &s->gb, 1);
1029 last = SHOW_UBITS(re, &s->gb, 1);
1030 SKIP_CACHE(re, &s->gb, 1);
1031 run = SHOW_UBITS(re, &s->gb, 6);
1032 SKIP_COUNTER(re, &s->gb, 1 + 1 + 6);
1033 UPDATE_CACHE(re, &s->gb);
1035 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1036 av_log(s->avctx, AV_LOG_ERROR,
1037 "2. marker bit missing in rvlc esc\n");
1040 SKIP_CACHE(re, &s->gb, 1);
1042 level = SHOW_UBITS(re, &s->gb, 11);
1043 SKIP_CACHE(re, &s->gb, 11);
1045 if (SHOW_UBITS(re, &s->gb, 5) != 0x10) {
1046 av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
1049 SKIP_CACHE(re, &s->gb, 5);
1051 level = level * qmul + qadd;
1052 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1053 SKIP_COUNTER(re, &s->gb, 1 + 11 + 5 + 1);
1060 cache = GET_CACHE(re, &s->gb);
1063 cache ^= 0xC0000000;
1065 if (cache & 0x80000000) {
1066 if (cache & 0x40000000) {
1068 SKIP_CACHE(re, &s->gb, 2);
1069 last = SHOW_UBITS(re, &s->gb, 1);
1070 SKIP_CACHE(re, &s->gb, 1);
1071 run = SHOW_UBITS(re, &s->gb, 6);
1072 SKIP_COUNTER(re, &s->gb, 2 + 1 + 6);
1073 UPDATE_CACHE(re, &s->gb);
1076 level = SHOW_SBITS(re, &s->gb, 12);
1077 LAST_SKIP_BITS(re, &s->gb, 12);
1079 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1080 av_log(s->avctx, AV_LOG_ERROR,
1081 "1. marker bit missing in 3. esc\n");
1084 SKIP_CACHE(re, &s->gb, 1);
1086 level = SHOW_SBITS(re, &s->gb, 12);
1087 SKIP_CACHE(re, &s->gb, 12);
1089 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1090 av_log(s->avctx, AV_LOG_ERROR,
1091 "2. marker bit missing in 3. esc\n");
1095 SKIP_COUNTER(re, &s->gb, 1 + 12 + 1);
1099 level = level * qmul + qadd;
1101 level = level * qmul - qadd;
1103 if ((unsigned)(level + 2048) > 4095) {
1104 if (s->avctx->err_recognition & AV_EF_BITSTREAM) {
1105 if (level > 2560 || level < -2560) {
1106 av_log(s->avctx, AV_LOG_ERROR,
1107 "|level| overflow in 3. esc, qp=%d\n",
1112 level = level < 0 ? -2048 : 2047;
1120 SKIP_BITS(re, &s->gb, 2);
1121 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1122 i += run + rl->max_run[run >> 7][level / qmul] + 1; // FIXME opt indexing
1123 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1124 LAST_SKIP_BITS(re, &s->gb, 1);
1128 SKIP_BITS(re, &s->gb, 1);
1129 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1131 level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; // FIXME opt indexing
1132 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1133 LAST_SKIP_BITS(re, &s->gb, 1);
1138 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1139 LAST_SKIP_BITS(re, &s->gb, 1);
1144 av_log(s->avctx, AV_LOG_ERROR,
1145 "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1149 block[scan_table[i]] = level;
1153 block[scan_table[i]] = level;
1155 CLOSE_READER(re, &s->gb);
1160 if (!ctx->use_intra_dc_vlc) {
1161 block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
1163 i -= i >> 31; // if (i == -1) i = 0;
1166 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1168 i = 63; // FIXME not optimal
1170 s->block_last_index[n] = i;
1175 * decode partition C of one MB.
1176 * @return <0 if an error occurred
1178 static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
1180 Mpeg4DecContext *ctx = (Mpeg4DecContext *)s;
1182 const int xy = s->mb_x + s->mb_y * s->mb_stride;
1184 mb_type = s->current_picture.mb_type[xy];
1185 cbp = s->cbp_table[xy];
1187 ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1189 if (s->current_picture.qscale_table[xy] != s->qscale)
1190 ff_set_qscale(s, s->current_picture.qscale_table[xy]);
1192 if (s->pict_type == AV_PICTURE_TYPE_P ||
1193 s->pict_type == AV_PICTURE_TYPE_S) {
1195 for (i = 0; i < 4; i++) {
1196 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
1197 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
1199 s->mb_intra = IS_INTRA(mb_type);
1201 if (IS_SKIP(mb_type)) {
1203 for (i = 0; i < 6; i++)
1204 s->block_last_index[i] = -1;
1205 s->mv_dir = MV_DIR_FORWARD;
1206 s->mv_type = MV_TYPE_16X16;
1207 if (s->pict_type == AV_PICTURE_TYPE_S
1208 && ctx->vol_sprite_usage == GMC_SPRITE) {
1215 } else if (s->mb_intra) {
1216 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1217 } else if (!s->mb_intra) {
1218 // s->mcsel = 0; // FIXME do we need to init that?
1220 s->mv_dir = MV_DIR_FORWARD;
1221 if (IS_8X8(mb_type)) {
1222 s->mv_type = MV_TYPE_8X8;
1224 s->mv_type = MV_TYPE_16X16;
1227 } else { /* I-Frame */
1229 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1232 if (!IS_SKIP(mb_type)) {
1234 s->bdsp.clear_blocks(s->block[0]);
1235 /* decode each block */
1236 for (i = 0; i < 6; i++) {
1237 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, s->mb_intra, ctx->rvlc) < 0) {
1238 av_log(s->avctx, AV_LOG_ERROR,
1239 "texture corrupted at %d %d %d\n",
1240 s->mb_x, s->mb_y, s->mb_intra);
1247 /* per-MB end of slice check */
1248 if (--s->mb_num_left <= 0) {
1249 if (mpeg4_is_resync(s))
1254 if (mpeg4_is_resync(s)) {
1255 const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
1256 if (s->cbp_table[xy + delta])
1263 static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
1265 Mpeg4DecContext *ctx = (Mpeg4DecContext *)s;
1266 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1268 static int8_t quant_tab[4] = { -1, -2, 1, 2 };
1269 const int xy = s->mb_x + s->mb_y * s->mb_stride;
1271 assert(s->h263_pred);
1273 if (s->pict_type == AV_PICTURE_TYPE_P ||
1274 s->pict_type == AV_PICTURE_TYPE_S) {
1276 if (get_bits1(&s->gb)) {
1279 for (i = 0; i < 6; i++)
1280 s->block_last_index[i] = -1;
1281 s->mv_dir = MV_DIR_FORWARD;
1282 s->mv_type = MV_TYPE_16X16;
1283 if (s->pict_type == AV_PICTURE_TYPE_S &&
1284 ctx->vol_sprite_usage == GMC_SPRITE) {
1285 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1290 s->mv[0][0][0] = get_amv(ctx, 0);
1291 s->mv[0][0][1] = get_amv(ctx, 1);
1294 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1304 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
1306 av_log(s->avctx, AV_LOG_ERROR,
1307 "cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1310 } while (cbpc == 20);
1312 s->bdsp.clear_blocks(s->block[0]);
1314 s->mb_intra = ((cbpc & 4) != 0);
1318 if (s->pict_type == AV_PICTURE_TYPE_S &&
1319 ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
1320 s->mcsel = get_bits1(&s->gb);
1323 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
1325 cbp = (cbpc & 3) | (cbpy << 2);
1327 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1328 if ((!s->progressive_sequence) &&
1329 (cbp || (s->workaround_bugs & FF_BUG_XVID_ILACE)))
1330 s->interlaced_dct = get_bits1(&s->gb);
1332 s->mv_dir = MV_DIR_FORWARD;
1333 if ((cbpc & 16) == 0) {
1335 s->current_picture.mb_type[xy] = MB_TYPE_GMC |
1338 /* 16x16 global motion prediction */
1339 s->mv_type = MV_TYPE_16X16;
1340 mx = get_amv(ctx, 0);
1341 my = get_amv(ctx, 1);
1342 s->mv[0][0][0] = mx;
1343 s->mv[0][0][1] = my;
1344 } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
1345 s->current_picture.mb_type[xy] = MB_TYPE_16x8 |
1348 /* 16x8 field motion prediction */
1349 s->mv_type = MV_TYPE_FIELD;
1351 s->field_select[0][0] = get_bits1(&s->gb);
1352 s->field_select[0][1] = get_bits1(&s->gb);
1354 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1356 for (i = 0; i < 2; i++) {
1357 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1361 my = ff_h263_decode_motion(s, pred_y / 2, s->f_code);
1365 s->mv[0][i][0] = mx;
1366 s->mv[0][i][1] = my;
1369 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1370 /* 16x16 motion prediction */
1371 s->mv_type = MV_TYPE_16X16;
1372 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1373 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1378 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1382 s->mv[0][0][0] = mx;
1383 s->mv[0][0][1] = my;
1386 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
1387 s->mv_type = MV_TYPE_8X8;
1388 for (i = 0; i < 4; i++) {
1389 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1390 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1394 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1397 s->mv[0][i][0] = mx;
1398 s->mv[0][i][1] = my;
1403 } else if (s->pict_type == AV_PICTURE_TYPE_B) {
1404 int modb1; // first bit of modb
1405 int modb2; // second bit of modb
1408 s->mb_intra = 0; // B-frames never contain intra blocks
1409 s->mcsel = 0; // ... true gmc blocks
1412 for (i = 0; i < 2; i++) {
1413 s->last_mv[i][0][0] =
1414 s->last_mv[i][0][1] =
1415 s->last_mv[i][1][0] =
1416 s->last_mv[i][1][1] = 0;
1419 ff_thread_await_progress(&s->next_picture_ptr->tf, s->mb_y, 0);
1422 /* if we skipped it in the future P-frame than skip it now too */
1423 s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
1425 if (s->mb_skipped) {
1427 for (i = 0; i < 6; i++)
1428 s->block_last_index[i] = -1;
1430 s->mv_dir = MV_DIR_FORWARD;
1431 s->mv_type = MV_TYPE_16X16;
1436 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1442 modb1 = get_bits1(&s->gb);
1444 // like MB_TYPE_B_DIRECT but no vectors coded
1445 mb_type = MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1;
1448 modb2 = get_bits1(&s->gb);
1449 mb_type = get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
1451 av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
1454 mb_type = mb_type_b_map[mb_type];
1458 s->bdsp.clear_blocks(s->block[0]);
1459 cbp = get_bits(&s->gb, 6);
1462 if ((!IS_DIRECT(mb_type)) && cbp) {
1463 if (get_bits1(&s->gb))
1464 ff_set_qscale(s, s->qscale + get_bits1(&s->gb) * 4 - 2);
1467 if (!s->progressive_sequence) {
1469 s->interlaced_dct = get_bits1(&s->gb);
1471 if (!IS_DIRECT(mb_type) && get_bits1(&s->gb)) {
1472 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1473 mb_type &= ~MB_TYPE_16x16;
1475 if (USES_LIST(mb_type, 0)) {
1476 s->field_select[0][0] = get_bits1(&s->gb);
1477 s->field_select[0][1] = get_bits1(&s->gb);
1479 if (USES_LIST(mb_type, 1)) {
1480 s->field_select[1][0] = get_bits1(&s->gb);
1481 s->field_select[1][1] = get_bits1(&s->gb);
1487 if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
1488 s->mv_type = MV_TYPE_16X16;
1490 if (USES_LIST(mb_type, 0)) {
1491 s->mv_dir = MV_DIR_FORWARD;
1493 mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1494 my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1495 s->last_mv[0][1][0] =
1496 s->last_mv[0][0][0] =
1497 s->mv[0][0][0] = mx;
1498 s->last_mv[0][1][1] =
1499 s->last_mv[0][0][1] =
1500 s->mv[0][0][1] = my;
1503 if (USES_LIST(mb_type, 1)) {
1504 s->mv_dir |= MV_DIR_BACKWARD;
1506 mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1507 my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1508 s->last_mv[1][1][0] =
1509 s->last_mv[1][0][0] =
1510 s->mv[1][0][0] = mx;
1511 s->last_mv[1][1][1] =
1512 s->last_mv[1][0][1] =
1513 s->mv[1][0][1] = my;
1515 } else if (!IS_DIRECT(mb_type)) {
1516 s->mv_type = MV_TYPE_FIELD;
1518 if (USES_LIST(mb_type, 0)) {
1519 s->mv_dir = MV_DIR_FORWARD;
1521 for (i = 0; i < 2; i++) {
1522 mx = ff_h263_decode_motion(s, s->last_mv[0][i][0], s->f_code);
1523 my = ff_h263_decode_motion(s, s->last_mv[0][i][1] / 2, s->f_code);
1524 s->last_mv[0][i][0] =
1525 s->mv[0][i][0] = mx;
1526 s->last_mv[0][i][1] = (s->mv[0][i][1] = my) * 2;
1530 if (USES_LIST(mb_type, 1)) {
1531 s->mv_dir |= MV_DIR_BACKWARD;
1533 for (i = 0; i < 2; i++) {
1534 mx = ff_h263_decode_motion(s, s->last_mv[1][i][0], s->b_code);
1535 my = ff_h263_decode_motion(s, s->last_mv[1][i][1] / 2, s->b_code);
1536 s->last_mv[1][i][0] =
1537 s->mv[1][i][0] = mx;
1538 s->last_mv[1][i][1] = (s->mv[1][i][1] = my) * 2;
1544 if (IS_DIRECT(mb_type)) {
1545 if (IS_SKIP(mb_type)) {
1549 mx = ff_h263_decode_motion(s, 0, 1);
1550 my = ff_h263_decode_motion(s, 0, 1);
1553 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1554 mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
1556 s->current_picture.mb_type[xy] = mb_type;
1557 } else { /* I-Frame */
1559 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
1561 av_log(s->avctx, AV_LOG_ERROR,
1562 "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1565 } while (cbpc == 8);
1571 s->ac_pred = get_bits1(&s->gb);
1573 s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
1575 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
1577 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1579 av_log(s->avctx, AV_LOG_ERROR,
1580 "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1583 cbp = (cbpc & 3) | (cbpy << 2);
1585 ctx->use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1588 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1590 if (!s->progressive_sequence)
1591 s->interlaced_dct = get_bits1(&s->gb);
1593 s->bdsp.clear_blocks(s->block[0]);
1594 /* decode each block */
1595 for (i = 0; i < 6; i++) {
1596 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 1, 0) < 0)
1603 /* decode each block */
1604 for (i = 0; i < 6; i++) {
1605 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 0, 0) < 0)
1611 /* per-MB end of slice check */
1612 if (s->codec_id == AV_CODEC_ID_MPEG4) {
1613 if (mpeg4_is_resync(s)) {
1614 const int delta = s->mb_x + 1 == s->mb_width ? 2 : 1;
1616 if (s->pict_type == AV_PICTURE_TYPE_B &&
1617 s->next_picture.mbskip_table[xy + delta]) {
1618 ff_thread_await_progress(&s->next_picture_ptr->tf,
1619 (s->mb_x + delta >= s->mb_width)
1620 ? FFMIN(s->mb_y + 1, s->mb_height - 1)
1624 if (s->pict_type == AV_PICTURE_TYPE_B &&
1625 s->next_picture.mbskip_table[xy + delta])
1634 static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
1636 int hours, minutes, seconds;
1637 unsigned time_code = show_bits(gb, 18);
1639 if (time_code & 0x40) { /* marker_bit */
1640 hours = time_code >> 13;
1641 minutes = time_code >> 7 & 0x3f;
1642 seconds = time_code & 0x3f;
1643 s->time_base = seconds + 60 * (minutes + 60 * hours);
1644 skip_bits(gb, 20); /* time_code, closed_gov, broken_link */
1646 av_log(s->avctx, AV_LOG_WARNING, "GOP header missing marker_bit\n");
1652 static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb)
1654 int profile_and_level_indication;
1656 profile_and_level_indication = get_bits(gb, 8);
1658 s->avctx->profile = (profile_and_level_indication & 0xf0) >> 4;
1659 s->avctx->level = (profile_and_level_indication & 0x0f);
1661 // for Simple profile, level 0
1662 if (s->avctx->profile == 0 && s->avctx->level == 8) {
1663 s->avctx->level = 0;
1669 static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
1671 MpegEncContext *s = &ctx->m;
1672 int width, height, vo_ver_id;
1675 skip_bits(gb, 1); /* random access */
1676 s->vo_type = get_bits(gb, 8);
1677 if (get_bits1(gb) != 0) { /* is_ol_id */
1678 vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
1679 skip_bits(gb, 3); /* vo_priority */
1683 s->aspect_ratio_info = get_bits(gb, 4);
1684 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
1685 s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
1686 s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
1688 s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[s->aspect_ratio_info];
1691 if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
1692 int chroma_format = get_bits(gb, 2);
1693 if (chroma_format != CHROMA_420)
1694 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
1696 s->low_delay = get_bits1(gb);
1697 if (get_bits1(gb)) { /* vbv parameters */
1698 get_bits(gb, 15); /* first_half_bitrate */
1699 skip_bits1(gb); /* marker */
1700 get_bits(gb, 15); /* latter_half_bitrate */
1701 skip_bits1(gb); /* marker */
1702 get_bits(gb, 15); /* first_half_vbv_buffer_size */
1703 skip_bits1(gb); /* marker */
1704 get_bits(gb, 3); /* latter_half_vbv_buffer_size */
1705 get_bits(gb, 11); /* first_half_vbv_occupancy */
1706 skip_bits1(gb); /* marker */
1707 get_bits(gb, 15); /* latter_half_vbv_occupancy */
1708 skip_bits1(gb); /* marker */
1711 /* is setting low delay flag only once the smartest thing to do?
1712 * low delay detection will not be overridden. */
1713 if (s->picture_number == 0)
1717 ctx->shape = get_bits(gb, 2); /* vol shape */
1718 if (ctx->shape != RECT_SHAPE)
1719 av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
1720 if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
1721 av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
1722 skip_bits(gb, 4); /* video_object_layer_shape_extension */
1725 check_marker(gb, "before time_increment_resolution");
1727 s->avctx->framerate.num = get_bits(gb, 16);
1728 if (!s->avctx->framerate.num) {
1729 av_log(s->avctx, AV_LOG_ERROR, "framerate==0\n");
1733 ctx->time_increment_bits = av_log2(s->avctx->framerate.num - 1) + 1;
1734 if (ctx->time_increment_bits < 1)
1735 ctx->time_increment_bits = 1;
1737 check_marker(gb, "before fixed_vop_rate");
1739 if (get_bits1(gb) != 0) /* fixed_vop_rate */
1740 s->avctx->framerate.den = get_bits(gb, ctx->time_increment_bits);
1742 s->avctx->framerate.den = 1;
1746 if (ctx->shape != BIN_ONLY_SHAPE) {
1747 if (ctx->shape == RECT_SHAPE) {
1748 skip_bits1(gb); /* marker */
1749 width = get_bits(gb, 13);
1750 skip_bits1(gb); /* marker */
1751 height = get_bits(gb, 13);
1752 skip_bits1(gb); /* marker */
1753 if (width && height && /* they should be non zero but who knows */
1754 !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
1755 if (s->width && s->height &&
1756 (s->width != width || s->height != height))
1757 s->context_reinit = 1;
1763 s->progressive_sequence =
1764 s->progressive_frame = get_bits1(gb) ^ 1;
1765 s->interlaced_dct = 0;
1766 if (!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
1767 av_log(s->avctx, AV_LOG_INFO, /* OBMC Disable */
1768 "MPEG-4 OBMC not supported (very likely buggy encoder)\n");
1770 ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
1772 ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
1774 if (ctx->vol_sprite_usage == STATIC_SPRITE)
1775 av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
1776 if (ctx->vol_sprite_usage == STATIC_SPRITE ||
1777 ctx->vol_sprite_usage == GMC_SPRITE) {
1778 if (ctx->vol_sprite_usage == STATIC_SPRITE) {
1779 skip_bits(gb, 13); // sprite_width
1780 skip_bits1(gb); /* marker */
1781 skip_bits(gb, 13); // sprite_height
1782 skip_bits1(gb); /* marker */
1783 skip_bits(gb, 13); // sprite_left
1784 skip_bits1(gb); /* marker */
1785 skip_bits(gb, 13); // sprite_top
1786 skip_bits1(gb); /* marker */
1788 ctx->num_sprite_warping_points = get_bits(gb, 6);
1789 if (ctx->num_sprite_warping_points > 3) {
1790 av_log(s->avctx, AV_LOG_ERROR,
1791 "%d sprite_warping_points\n",
1792 ctx->num_sprite_warping_points);
1793 ctx->num_sprite_warping_points = 0;
1796 s->sprite_warping_accuracy = get_bits(gb, 2);
1797 ctx->sprite_brightness_change = get_bits1(gb);
1798 if (ctx->vol_sprite_usage == STATIC_SPRITE)
1799 skip_bits1(gb); // low_latency_sprite
1801 // FIXME sadct disable bit if verid!=1 && shape not rect
1803 if (get_bits1(gb) == 1) { /* not_8_bit */
1804 s->quant_precision = get_bits(gb, 4); /* quant_precision */
1805 if (get_bits(gb, 4) != 8) /* bits_per_pixel */
1806 av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
1807 if (s->quant_precision != 5)
1808 av_log(s->avctx, AV_LOG_ERROR,
1809 "quant precision %d\n", s->quant_precision);
1811 s->quant_precision = 5;
1814 // FIXME a bunch of grayscale shape things
1816 if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
1819 /* load default matrixes */
1820 for (i = 0; i < 64; i++) {
1821 int j = s->idsp.idct_permutation[i];
1822 v = ff_mpeg4_default_intra_matrix[i];
1823 s->intra_matrix[j] = v;
1824 s->chroma_intra_matrix[j] = v;
1826 v = ff_mpeg4_default_non_intra_matrix[i];
1827 s->inter_matrix[j] = v;
1828 s->chroma_inter_matrix[j] = v;
1831 /* load custom intra matrix */
1832 if (get_bits1(gb)) {
1834 for (i = 0; i < 64; i++) {
1836 v = get_bits(gb, 8);
1841 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1842 s->intra_matrix[j] = last;
1843 s->chroma_intra_matrix[j] = last;
1846 /* replicate last value */
1847 for (; i < 64; i++) {
1848 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1849 s->intra_matrix[j] = last;
1850 s->chroma_intra_matrix[j] = last;
1854 /* load custom non intra matrix */
1855 if (get_bits1(gb)) {
1857 for (i = 0; i < 64; i++) {
1859 v = get_bits(gb, 8);
1864 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1865 s->inter_matrix[j] = v;
1866 s->chroma_inter_matrix[j] = v;
1869 /* replicate last value */
1870 for (; i < 64; i++) {
1871 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1872 s->inter_matrix[j] = last;
1873 s->chroma_inter_matrix[j] = last;
1877 // FIXME a bunch of grayscale shape things
1881 s->quarter_sample = get_bits1(gb);
1883 s->quarter_sample = 0;
1885 if (!get_bits1(gb)) {
1886 int pos = get_bits_count(gb);
1887 int estimation_method = get_bits(gb, 2);
1888 if (estimation_method < 2) {
1889 if (!get_bits1(gb)) {
1890 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */
1891 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */
1892 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */
1893 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */
1894 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */
1895 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upsampling */
1897 if (!get_bits1(gb)) {
1898 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */
1899 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */
1900 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */
1901 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */
1903 if (!check_marker(gb, "in complexity estimation part 1")) {
1904 skip_bits_long(gb, pos - get_bits_count(gb));
1907 if (!get_bits1(gb)) {
1908 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */
1909 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */
1910 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */
1911 ctx->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */
1913 if (!get_bits1(gb)) {
1914 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */
1915 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */
1916 ctx->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */
1917 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */
1918 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */
1919 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */
1921 if (!check_marker(gb, "in complexity estimation part 2")) {
1922 skip_bits_long(gb, pos - get_bits_count(gb));
1925 if (estimation_method == 1) {
1926 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */
1927 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */
1930 av_log(s->avctx, AV_LOG_ERROR,
1931 "Invalid Complexity estimation method %d\n",
1936 ctx->cplx_estimation_trash_i =
1937 ctx->cplx_estimation_trash_p =
1938 ctx->cplx_estimation_trash_b = 0;
1941 ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
1943 s->data_partitioning = get_bits1(gb);
1944 if (s->data_partitioning)
1945 ctx->rvlc = get_bits1(gb);
1947 if (vo_ver_id != 1) {
1948 ctx->new_pred = get_bits1(gb);
1949 if (ctx->new_pred) {
1950 av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
1951 skip_bits(gb, 2); /* requested upstream message type */
1952 skip_bits1(gb); /* newpred segment type */
1954 if (get_bits1(gb)) // reduced_res_vop
1955 av_log(s->avctx, AV_LOG_ERROR,
1956 "reduced resolution VOP not supported\n");
1961 ctx->scalability = get_bits1(gb);
1963 if (ctx->scalability) {
1964 GetBitContext bak = *gb;
1965 int h_sampling_factor_n;
1966 int h_sampling_factor_m;
1967 int v_sampling_factor_n;
1968 int v_sampling_factor_m;
1970 skip_bits1(gb); // hierarchy_type
1971 skip_bits(gb, 4); /* ref_layer_id */
1972 skip_bits1(gb); /* ref_layer_sampling_dir */
1973 h_sampling_factor_n = get_bits(gb, 5);
1974 h_sampling_factor_m = get_bits(gb, 5);
1975 v_sampling_factor_n = get_bits(gb, 5);
1976 v_sampling_factor_m = get_bits(gb, 5);
1977 ctx->enhancement_type = get_bits1(gb);
1979 if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
1980 v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
1981 /* illegal scalability header (VERY broken encoder),
1982 * trying to workaround */
1983 ctx->scalability = 0;
1986 av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
1988 // bin shape stuff FIXME
1996 * Decode the user data stuff in the header.
1997 * Also initializes divx/xvid/lavc_version/build.
1999 static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
2001 MpegEncContext *s = &ctx->m;
2005 int ver = 0, build = 0, ver2 = 0, ver3 = 0;
2008 for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
2009 if (show_bits(gb, 23) == 0)
2011 buf[i] = get_bits(gb, 8);
2015 /* divx detection */
2016 e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
2018 e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
2020 ctx->divx_version = ver;
2021 ctx->divx_build = build;
2022 s->divx_packed = e == 3 && last == 'p';
2023 if (s->divx_packed && !ctx->showed_packed_warning) {
2024 av_log(s->avctx, AV_LOG_WARNING,
2025 "Invalid and inefficient vfw-avi packed B-frames detected\n");
2026 ctx->showed_packed_warning = 1;
2030 /* libavcodec detection */
2031 e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
2033 e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
2035 e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
2037 build = (ver << 16) + (ver2 << 8) + ver3;
2040 if (strcmp(buf, "ffmpeg") == 0)
2041 ctx->lavc_build = 4600;
2044 ctx->lavc_build = build;
2046 /* Xvid detection */
2047 e = sscanf(buf, "XviD%d", &build);
2049 ctx->xvid_build = build;
2051 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
2052 if (s->codec_tag == AV_RL32("XVID") ||
2053 s->codec_tag == AV_RL32("XVIX") ||
2054 s->codec_tag == AV_RL32("RMP4") ||
2055 s->codec_tag == AV_RL32("ZMP4") ||
2056 s->codec_tag == AV_RL32("SIPP"))
2057 ctx->xvid_build = 0;
2060 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
2061 if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
2062 ctx->vol_control_parameters == 0)
2063 ctx->divx_version = 400; // divx 4
2065 if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
2067 ctx->divx_build = -1;
2070 if (CONFIG_MPEG4_DECODER && ctx->xvid_build >= 0)
2071 ff_xvid_idct_init(&s->idsp, s->avctx);
2076 static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2078 MpegEncContext *s = &ctx->m;
2079 int time_incr, time_increment;
2081 s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
2082 if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
2083 ctx->vol_control_parameters == 0 && !(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)) {
2084 av_log(s->avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n");
2088 s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
2089 if (s->partitioned_frame)
2090 s->decode_mb = mpeg4_decode_partitioned_mb;
2092 s->decode_mb = mpeg4_decode_mb;
2095 while (get_bits1(gb) != 0)
2098 check_marker(gb, "before time_increment");
2100 if (ctx->time_increment_bits == 0 ||
2101 !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
2102 /* Headers seem incomplete; try to guess time_increment_bits. */
2103 for (ctx->time_increment_bits = 1;
2104 ctx->time_increment_bits < 16;
2105 ctx->time_increment_bits++) {
2106 if (s->pict_type == AV_PICTURE_TYPE_P ||
2107 (s->pict_type == AV_PICTURE_TYPE_S &&
2108 ctx->vol_sprite_usage == GMC_SPRITE)) {
2109 if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
2111 } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
2117 time_increment = get_bits1(gb); // FIXME investigate further
2119 time_increment = get_bits(gb, ctx->time_increment_bits);
2121 if (s->pict_type != AV_PICTURE_TYPE_B) {
2122 s->last_time_base = s->time_base;
2123 s->time_base += time_incr;
2124 s->time = s->time_base * s->avctx->framerate.num + time_increment;
2125 if (s->workaround_bugs & FF_BUG_UMP4) {
2126 if (s->time < s->last_non_b_time) {
2127 /* header is not mpeg-4-compatible, broken encoder,
2128 * trying to workaround */
2130 s->time += s->avctx->framerate.num;
2133 s->pp_time = s->time - s->last_non_b_time;
2134 s->last_non_b_time = s->time;
2136 s->time = (s->last_time_base + time_incr) * s->avctx->framerate.num + time_increment;
2137 s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
2138 if (s->pp_time <= s->pb_time ||
2139 s->pp_time <= s->pp_time - s->pb_time ||
2141 /* messed up order, maybe after seeking? skipping current B-frame */
2142 return FRAME_SKIPPED;
2144 ff_mpeg4_init_direct_mv(s);
2146 if (ctx->t_frame == 0)
2147 ctx->t_frame = s->pb_time;
2148 if (ctx->t_frame == 0)
2149 ctx->t_frame = 1; // 1/0 protection
2150 s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
2151 ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2152 s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
2153 ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
2154 if (!s->progressive_sequence) {
2155 if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1)
2156 return FRAME_SKIPPED;
2160 check_marker(gb, "before vop_coded");
2163 if (get_bits1(gb) != 1) {
2164 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2165 av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
2166 return FRAME_SKIPPED;
2168 if (ctx->shape != BIN_ONLY_SHAPE &&
2169 (s->pict_type == AV_PICTURE_TYPE_P ||
2170 (s->pict_type == AV_PICTURE_TYPE_S &&
2171 ctx->vol_sprite_usage == GMC_SPRITE))) {
2172 /* rounding type for motion estimation */
2173 s->no_rounding = get_bits1(gb);
2177 // FIXME reduced res stuff
2179 if (ctx->shape != RECT_SHAPE) {
2180 if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
2181 skip_bits(gb, 13); /* width */
2182 skip_bits1(gb); /* marker */
2183 skip_bits(gb, 13); /* height */
2184 skip_bits1(gb); /* marker */
2185 skip_bits(gb, 13); /* hor_spat_ref */
2186 skip_bits1(gb); /* marker */
2187 skip_bits(gb, 13); /* ver_spat_ref */
2189 skip_bits1(gb); /* change_CR_disable */
2191 if (get_bits1(gb) != 0)
2192 skip_bits(gb, 8); /* constant_alpha_value */
2195 // FIXME complexity estimation stuff
2197 if (ctx->shape != BIN_ONLY_SHAPE) {
2198 skip_bits_long(gb, ctx->cplx_estimation_trash_i);
2199 if (s->pict_type != AV_PICTURE_TYPE_I)
2200 skip_bits_long(gb, ctx->cplx_estimation_trash_p);
2201 if (s->pict_type == AV_PICTURE_TYPE_B)
2202 skip_bits_long(gb, ctx->cplx_estimation_trash_b);
2204 ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
2205 if (!s->progressive_sequence) {
2206 s->top_field_first = get_bits1(gb);
2207 s->alternate_scan = get_bits1(gb);
2209 s->alternate_scan = 0;
2212 if (s->alternate_scan) {
2213 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
2214 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
2215 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
2216 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2218 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
2219 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
2220 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
2221 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
2224 if (s->pict_type == AV_PICTURE_TYPE_S &&
2225 (ctx->vol_sprite_usage == STATIC_SPRITE ||
2226 ctx->vol_sprite_usage == GMC_SPRITE)) {
2227 if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
2228 return AVERROR_INVALIDDATA;
2229 if (ctx->sprite_brightness_change)
2230 av_log(s->avctx, AV_LOG_ERROR,
2231 "sprite_brightness_change not supported\n");
2232 if (ctx->vol_sprite_usage == STATIC_SPRITE)
2233 av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
2236 if (ctx->shape != BIN_ONLY_SHAPE) {
2237 s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision);
2238 if (s->qscale == 0) {
2239 av_log(s->avctx, AV_LOG_ERROR,
2240 "Error, header damaged or not MPEG-4 header (qscale=0)\n");
2241 return -1; // makes no sense to continue, as there is nothing left from the image then
2244 if (s->pict_type != AV_PICTURE_TYPE_I) {
2245 s->f_code = get_bits(gb, 3); /* fcode_for */
2246 if (s->f_code == 0) {
2247 av_log(s->avctx, AV_LOG_ERROR,
2248 "Error, header damaged or not MPEG-4 header (f_code=0)\n");
2249 return -1; // makes no sense to continue, as there is nothing left from the image then
2254 if (s->pict_type == AV_PICTURE_TYPE_B) {
2255 s->b_code = get_bits(gb, 3);
2259 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2260 av_log(s->avctx, AV_LOG_DEBUG,
2261 "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\n",
2262 s->qscale, s->f_code, s->b_code,
2263 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")),
2264 gb->size_in_bits, s->progressive_sequence, s->alternate_scan,
2265 s->top_field_first, s->quarter_sample ? "q" : "h",
2266 s->data_partitioning, ctx->resync_marker,
2267 ctx->num_sprite_warping_points, s->sprite_warping_accuracy,
2268 1 - s->no_rounding, s->vo_type,
2269 ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
2270 ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
2271 ctx->cplx_estimation_trash_b);
2274 if (!ctx->scalability) {
2275 if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
2276 skip_bits1(gb); // vop shape coding type
2278 if (ctx->enhancement_type) {
2279 int load_backward_shape = get_bits1(gb);
2280 if (load_backward_shape)
2281 av_log(s->avctx, AV_LOG_ERROR,
2282 "load backward shape isn't supported\n");
2284 skip_bits(gb, 2); // ref_select_code
2287 /* detect buggy encoders which don't set the low_delay flag
2288 * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames
2289 * easily (although it's buggy too) */
2290 if (s->vo_type == 0 && ctx->vol_control_parameters == 0 &&
2291 ctx->divx_version == -1 && s->picture_number == 0) {
2292 av_log(s->avctx, AV_LOG_WARNING,
2293 "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
2297 s->picture_number++; // better than pic number==0 always ;)
2299 // FIXME add short header support
2300 s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
2301 s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
2303 if (s->workaround_bugs & FF_BUG_EDGE) {
2304 s->h_edge_pos = s->width;
2305 s->v_edge_pos = s->height;
2311 * Decode MPEG-4 headers.
2312 * @return <0 if no VOP found (or a damaged one)
2313 * FRAME_SKIPPED if a not coded VOP is found
2314 * 0 if a VOP is found
2316 int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2318 MpegEncContext *s = &ctx->m;
2319 unsigned startcode, v;
2321 /* search next start code */
2324 if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
2326 if (get_bits(gb, 8) == 0xF0)
2332 if (get_bits_count(gb) >= gb->size_in_bits) {
2333 if (gb->size_in_bits == 8 &&
2334 (ctx->divx_version >= 0 || ctx->xvid_build >= 0)) {
2335 av_log(s->avctx, AV_LOG_WARNING, "frame skip %d\n", gb->size_in_bits);
2336 return FRAME_SKIPPED; // divx bug
2338 return -1; // end of stream
2341 /* use the bits after the test */
2342 v = get_bits(gb, 8);
2343 startcode = ((startcode << 8) | v) & 0xffffffff;
2345 if ((startcode & 0xFFFFFF00) != 0x100)
2346 continue; // no startcode
2348 if (s->avctx->debug & FF_DEBUG_STARTCODE) {
2349 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X ", startcode);
2350 if (startcode <= 0x11F)
2351 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Start");
2352 else if (startcode <= 0x12F)
2353 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Layer Start");
2354 else if (startcode <= 0x13F)
2355 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2356 else if (startcode <= 0x15F)
2357 av_log(s->avctx, AV_LOG_DEBUG, "FGS bp start");
2358 else if (startcode <= 0x1AF)
2359 av_log(s->avctx, AV_LOG_DEBUG, "Reserved");
2360 else if (startcode == 0x1B0)
2361 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq Start");
2362 else if (startcode == 0x1B1)
2363 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Seq End");
2364 else if (startcode == 0x1B2)
2365 av_log(s->avctx, AV_LOG_DEBUG, "User Data");
2366 else if (startcode == 0x1B3)
2367 av_log(s->avctx, AV_LOG_DEBUG, "Group of VOP start");
2368 else if (startcode == 0x1B4)
2369 av_log(s->avctx, AV_LOG_DEBUG, "Video Session Error");
2370 else if (startcode == 0x1B5)
2371 av_log(s->avctx, AV_LOG_DEBUG, "Visual Object Start");
2372 else if (startcode == 0x1B6)
2373 av_log(s->avctx, AV_LOG_DEBUG, "Video Object Plane start");
2374 else if (startcode == 0x1B7)
2375 av_log(s->avctx, AV_LOG_DEBUG, "slice start");
2376 else if (startcode == 0x1B8)
2377 av_log(s->avctx, AV_LOG_DEBUG, "extension start");
2378 else if (startcode == 0x1B9)
2379 av_log(s->avctx, AV_LOG_DEBUG, "fgs start");
2380 else if (startcode == 0x1BA)
2381 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object start");
2382 else if (startcode == 0x1BB)
2383 av_log(s->avctx, AV_LOG_DEBUG, "FBA Object Plane start");
2384 else if (startcode == 0x1BC)
2385 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object start");
2386 else if (startcode == 0x1BD)
2387 av_log(s->avctx, AV_LOG_DEBUG, "Mesh Object Plane start");
2388 else if (startcode == 0x1BE)
2389 av_log(s->avctx, AV_LOG_DEBUG, "Still Texture Object start");
2390 else if (startcode == 0x1BF)
2391 av_log(s->avctx, AV_LOG_DEBUG, "Texture Spatial Layer start");
2392 else if (startcode == 0x1C0)
2393 av_log(s->avctx, AV_LOG_DEBUG, "Texture SNR Layer start");
2394 else if (startcode == 0x1C1)
2395 av_log(s->avctx, AV_LOG_DEBUG, "Texture Tile start");
2396 else if (startcode == 0x1C2)
2397 av_log(s->avctx, AV_LOG_DEBUG, "Texture Shape Layer start");
2398 else if (startcode == 0x1C3)
2399 av_log(s->avctx, AV_LOG_DEBUG, "stuffing start");
2400 else if (startcode <= 0x1C5)
2401 av_log(s->avctx, AV_LOG_DEBUG, "reserved");
2402 else if (startcode <= 0x1FF)
2403 av_log(s->avctx, AV_LOG_DEBUG, "System start");
2404 av_log(s->avctx, AV_LOG_DEBUG, " at %d\n", get_bits_count(gb));
2407 if (startcode >= 0x120 && startcode <= 0x12F) {
2408 if (decode_vol_header(ctx, gb) < 0)
2410 } else if (startcode == USER_DATA_STARTCODE) {
2411 decode_user_data(ctx, gb);
2412 } else if (startcode == GOP_STARTCODE) {
2413 mpeg4_decode_gop_header(s, gb);
2414 } else if (startcode == VOS_STARTCODE) {
2415 mpeg4_decode_profile_level(s, gb);
2416 } else if (startcode == VOP_STARTCODE) {
2425 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
2427 s->avctx->has_b_frames = !s->low_delay;
2429 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
2430 if (s->codec_tag == AV_RL32("XVIX"))
2431 s->workaround_bugs |= FF_BUG_XVID_ILACE;
2433 if (s->codec_tag == AV_RL32("UMP4"))
2434 s->workaround_bugs |= FF_BUG_UMP4;
2436 if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
2437 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2439 if (ctx->divx_version > 502 && ctx->divx_build < 1814)
2440 s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
2442 if (ctx->xvid_build <= 3U)
2443 s->padding_bug_score = 256 * 256 * 256 * 64;
2445 if (ctx->xvid_build <= 1U)
2446 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2448 if (ctx->xvid_build <= 12U)
2449 s->workaround_bugs |= FF_BUG_EDGE;
2451 if (ctx->xvid_build <= 32U)
2452 s->workaround_bugs |= FF_BUG_DC_CLIP;
2454 if (ctx->lavc_build < 4653U)
2455 s->workaround_bugs |= FF_BUG_STD_QPEL;
2457 if (ctx->lavc_build < 4655U)
2458 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2460 if (ctx->lavc_build < 4670U)
2461 s->workaround_bugs |= FF_BUG_EDGE;
2463 if (ctx->lavc_build <= 4712U)
2464 s->workaround_bugs |= FF_BUG_DC_CLIP;
2466 if (ctx->divx_version >= 0)
2467 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2469 if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
2470 s->padding_bug_score = 256 * 256 * 256 * 64;
2472 if (ctx->divx_version < 500U)
2473 s->workaround_bugs |= FF_BUG_EDGE;
2475 if (ctx->divx_version >= 0)
2476 s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
2480 if (s->avctx->debug & FF_DEBUG_BUGS)
2481 av_log(s->avctx, AV_LOG_DEBUG,
2482 "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
2483 s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
2484 ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
2486 return decode_vop_header(ctx, gb);
2489 int ff_mpeg4_frame_end(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
2491 Mpeg4DecContext *ctx = avctx->priv_data;
2492 MpegEncContext *s = &ctx->m;
2494 /* divx 5.01+ bitstream reorder stuff */
2495 if (s->divx_packed) {
2496 int current_pos = get_bits_count(&s->gb) >> 3;
2497 int startcode_found = 0;
2499 if (buf_size - current_pos > 5) {
2501 for (i = current_pos; i < buf_size - 3; i++)
2505 buf[i + 3] == 0xB6) {
2506 startcode_found = 1;
2510 if (s->gb.buffer == s->bitstream_buffer && buf_size > 7 &&
2511 ctx->xvid_build >= 0) { // xvid style
2512 startcode_found = 1;
2516 if (startcode_found) {
2517 av_fast_malloc(&s->bitstream_buffer,
2518 &s->allocated_bitstream_buffer_size,
2519 buf_size - current_pos +
2520 AV_INPUT_BUFFER_PADDING_SIZE);
2521 if (!s->bitstream_buffer)
2522 return AVERROR(ENOMEM);
2523 memcpy(s->bitstream_buffer, buf + current_pos,
2524 buf_size - current_pos);
2525 s->bitstream_buffer_size = buf_size - current_pos;
2532 static int mpeg4_update_thread_context(AVCodecContext *dst,
2533 const AVCodecContext *src)
2535 Mpeg4DecContext *s = dst->priv_data;
2536 const Mpeg4DecContext *s1 = src->priv_data;
2537 int init = s->m.context_initialized;
2539 int ret = ff_mpeg_update_thread_context(dst, src);
2544 if (CONFIG_MPEG4_DECODER && !init && s1->xvid_build >= 0)
2545 ff_xvid_idct_init(&s->m.idsp, dst);
2547 s->shape = s1->shape;
2548 s->time_increment_bits = s1->time_increment_bits;
2549 s->xvid_build = s1->xvid_build;
2554 static av_cold int decode_init(AVCodecContext *avctx)
2556 Mpeg4DecContext *ctx = avctx->priv_data;
2557 MpegEncContext *s = &ctx->m;
2559 static int done = 0;
2564 ctx->lavc_build = -1;
2566 if ((ret = ff_h263_decode_init(avctx)) < 0)
2572 ff_rl_init(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
2573 ff_rl_init(&ff_rvlc_rl_inter, ff_mpeg4_static_rl_table_store[1]);
2574 ff_rl_init(&ff_rvlc_rl_intra, ff_mpeg4_static_rl_table_store[2]);
2575 INIT_VLC_RL(ff_mpeg4_rl_intra, 554);
2576 INIT_VLC_RL(ff_rvlc_rl_inter, 1072);
2577 INIT_VLC_RL(ff_rvlc_rl_intra, 1072);
2578 INIT_VLC_STATIC(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
2579 &ff_mpeg4_DCtab_lum[0][1], 2, 1,
2580 &ff_mpeg4_DCtab_lum[0][0], 2, 1, 512);
2581 INIT_VLC_STATIC(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
2582 &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
2583 &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 512);
2584 INIT_VLC_STATIC(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
2585 &ff_sprite_trajectory_tab[0][1], 4, 2,
2586 &ff_sprite_trajectory_tab[0][0], 4, 2, 128);
2587 INIT_VLC_STATIC(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
2588 &ff_mb_type_b_tab[0][1], 2, 1,
2589 &ff_mb_type_b_tab[0][0], 2, 1, 16);
2593 s->low_delay = 0; /* default, might be overridden in the vol header during header parsing */
2594 s->decode_mb = mpeg4_decode_mb;
2595 ctx->time_increment_bits = 4; /* default value for broken headers */
2597 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
2598 avctx->internal->allocate_progress = 1;
2603 AVCodec ff_mpeg4_decoder = {
2605 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
2606 .type = AVMEDIA_TYPE_VIDEO,
2607 .id = AV_CODEC_ID_MPEG4,
2608 .priv_data_size = sizeof(Mpeg4DecContext),
2609 .init = decode_init,
2610 .close = ff_h263_decode_end,
2611 .decode = ff_h263_decode_frame,
2612 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2613 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
2614 AV_CODEC_CAP_FRAME_THREADS,
2615 .flush = ff_mpeg_flush,
2616 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
2617 .profiles = NULL_IF_CONFIG_SMALL(ff_mpeg4_video_profiles),
2618 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg4_update_thread_context),