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"
29 #include "mpegutils.h"
30 #include "mpegvideo.h"
31 #include "mpeg4video.h"
36 /* The defines below define the number of bits that are read at once for
37 * reading vlc values. Changing these may improve speed and data cache needs
38 * be aware though that decreasing them may need the number of stages that is
39 * passed to get_vlc* to be increased. */
40 #define SPRITE_TRAJ_VLC_BITS 6
42 #define MB_TYPE_B_VLC_BITS 4
44 static VLC dc_lum
, dc_chrom
;
45 static VLC sprite_trajectory
;
46 static VLC mb_type_b_vlc
;
48 static const int mb_type_b_map
[4] = {
49 MB_TYPE_DIRECT2
| MB_TYPE_L0L1
,
50 MB_TYPE_L0L1
| MB_TYPE_16x16
,
51 MB_TYPE_L1
| MB_TYPE_16x16
,
52 MB_TYPE_L0
| MB_TYPE_16x16
,
57 * @param n block index (0-3 are luma, 4-5 are chroma)
58 * @param dir the ac prediction direction
60 void ff_mpeg4_pred_ac(MpegEncContext
*s
, int16_t *block
, int n
, int dir
)
63 int16_t *ac_val
, *ac_val1
;
64 int8_t *const qscale_table
= s
->current_picture
.qscale_table
;
67 ac_val
= s
->ac_val
[0][0] + s
->block_index
[n
] * 16;
71 const int xy
= s
->mb_x
- 1 + s
->mb_y
* s
->mb_stride
;
75 if (s
->mb_x
== 0 || s
->qscale
== qscale_table
[xy
] ||
78 for (i
= 1; i
< 8; i
++)
79 block
[s
->idsp
.idct_permutation
[i
<< 3]] += ac_val
[i
];
81 /* different qscale, we must rescale */
82 for (i
= 1; i
< 8; i
++)
83 block
[s
->idsp
.idct_permutation
[i
<< 3]] += ROUNDED_DIV(ac_val
[i
] * qscale_table
[xy
], s
->qscale
);
86 const int xy
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
- s
->mb_stride
;
88 ac_val
-= 16 * s
->block_wrap
[n
];
90 if (s
->mb_y
== 0 || s
->qscale
== qscale_table
[xy
] ||
93 for (i
= 1; i
< 8; i
++)
94 block
[s
->idsp
.idct_permutation
[i
]] += ac_val
[i
+ 8];
96 /* different qscale, we must rescale */
97 for (i
= 1; i
< 8; i
++)
98 block
[s
->idsp
.idct_permutation
[i
]] += ROUNDED_DIV(ac_val
[i
+ 8] * qscale_table
[xy
], s
->qscale
);
103 for (i
= 1; i
< 8; i
++)
104 ac_val1
[i
] = block
[s
->idsp
.idct_permutation
[i
<< 3]];
107 for (i
= 1; i
< 8; i
++)
108 ac_val1
[8 + i
] = block
[s
->idsp
.idct_permutation
[i
]];
112 * check if the next stuff is a resync marker or the end.
115 static inline int mpeg4_is_resync(Mpeg4DecContext
*ctx
)
117 MpegEncContext
*s
= &ctx
->m
;
118 int bits_count
= get_bits_count(&s
->gb
);
119 int v
= show_bits(&s
->gb
, 16);
121 if (s
->workaround_bugs
& FF_BUG_NO_PADDING
&& !ctx
->resync_marker
)
125 if (s
->pict_type
== AV_PICTURE_TYPE_B
||
126 (v
>> (8 - s
->pict_type
) != 1) || s
->partitioned_frame
)
128 skip_bits(&s
->gb
, 8 + s
->pict_type
);
129 bits_count
+= 8 + s
->pict_type
;
130 v
= show_bits(&s
->gb
, 16);
133 if (bits_count
+ 8 >= s
->gb
.size_in_bits
) {
135 v
|= 0x7F >> (7 - (bits_count
& 7));
140 if (v
== ff_mpeg4_resync_prefix
[bits_count
& 7]) {
142 int mb_num_bits
= av_log2(s
->mb_num
- 1) + 1;
143 GetBitContext gb
= s
->gb
;
145 skip_bits(&s
->gb
, 1);
146 align_get_bits(&s
->gb
);
148 for (len
= 0; len
< 32; len
++)
149 if (get_bits1(&s
->gb
))
152 mb_num
= get_bits(&s
->gb
, mb_num_bits
);
153 if (!mb_num
|| mb_num
> s
->mb_num
|| get_bits_count(&s
->gb
)+6 > s
->gb
.size_in_bits
)
158 if (len
>= ff_mpeg4_get_video_packet_prefix_length(s
))
165 static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext
*ctx
, GetBitContext
*gb
)
167 MpegEncContext
*s
= &ctx
->m
;
168 int a
= 2 << s
->sprite_warping_accuracy
;
169 int rho
= 3 - s
->sprite_warping_accuracy
;
175 int min_ab
, i
, w2
, h2
, w3
, h3
;
176 int sprite_ref
[4][2];
177 int virtual_ref
[2][2];
179 // only true for rectangle shapes
180 const int vop_ref
[4][2] = { { 0, 0 }, { s
->width
, 0 },
181 { 0, s
->height
}, { s
->width
, s
->height
} };
182 int d
[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
184 if (w
<= 0 || h
<= 0)
185 return AVERROR_INVALIDDATA
;
187 for (i
= 0; i
< ctx
->num_sprite_warping_points
; i
++) {
191 length
= get_vlc2(gb
, sprite_trajectory
.table
, SPRITE_TRAJ_VLC_BITS
, 3);
193 x
= get_xbits(gb
, length
);
195 if (!(ctx
->divx_version
== 500 && ctx
->divx_build
== 413))
196 skip_bits1(gb
); /* marker bit */
198 length
= get_vlc2(gb
, sprite_trajectory
.table
, SPRITE_TRAJ_VLC_BITS
, 3);
200 y
= get_xbits(gb
, length
);
202 skip_bits1(gb
); /* marker bit */
203 ctx
->sprite_traj
[i
][0] = d
[i
][0] = x
;
204 ctx
->sprite_traj
[i
][1] = d
[i
][1] = y
;
207 ctx
->sprite_traj
[i
][0] = ctx
->sprite_traj
[i
][1] = 0;
209 while ((1 << alpha
) < w
)
211 while ((1 << beta
) < h
)
212 beta
++; /* typo in the mpeg4 std for the definition of w' and h' */
216 // Note, the 4th point isn't used for GMC
217 if (ctx
->divx_version
== 500 && ctx
->divx_build
== 413) {
218 sprite_ref
[0][0] = a
* vop_ref
[0][0] + d
[0][0];
219 sprite_ref
[0][1] = a
* vop_ref
[0][1] + d
[0][1];
220 sprite_ref
[1][0] = a
* vop_ref
[1][0] + d
[0][0] + d
[1][0];
221 sprite_ref
[1][1] = a
* vop_ref
[1][1] + d
[0][1] + d
[1][1];
222 sprite_ref
[2][0] = a
* vop_ref
[2][0] + d
[0][0] + d
[2][0];
223 sprite_ref
[2][1] = a
* vop_ref
[2][1] + d
[0][1] + d
[2][1];
225 sprite_ref
[0][0] = (a
>> 1) * (2 * vop_ref
[0][0] + d
[0][0]);
226 sprite_ref
[0][1] = (a
>> 1) * (2 * vop_ref
[0][1] + d
[0][1]);
227 sprite_ref
[1][0] = (a
>> 1) * (2 * vop_ref
[1][0] + d
[0][0] + d
[1][0]);
228 sprite_ref
[1][1] = (a
>> 1) * (2 * vop_ref
[1][1] + d
[0][1] + d
[1][1]);
229 sprite_ref
[2][0] = (a
>> 1) * (2 * vop_ref
[2][0] + d
[0][0] + d
[2][0]);
230 sprite_ref
[2][1] = (a
>> 1) * (2 * vop_ref
[2][1] + d
[0][1] + d
[2][1]);
232 /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
233 * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
235 /* this is mostly identical to the mpeg4 std (and is totally unreadable
236 * because of that...). Perhaps it should be reordered to be more readable.
237 * The idea behind this virtual_ref mess is to be able to use shifts later
238 * per pixel instead of divides so the distance between points is converted
239 * from w&h based to w2&h2 based which are of the 2^x form. */
240 virtual_ref
[0][0] = 16 * (vop_ref
[0][0] + w2
) +
241 ROUNDED_DIV(((w
- w2
) *
242 (r
* sprite_ref
[0][0] - 16 * vop_ref
[0][0]) +
243 w2
* (r
* sprite_ref
[1][0] - 16 * vop_ref
[1][0])), w
);
244 virtual_ref
[0][1] = 16 * vop_ref
[0][1] +
245 ROUNDED_DIV(((w
- w2
) *
246 (r
* sprite_ref
[0][1] - 16 * vop_ref
[0][1]) +
247 w2
* (r
* sprite_ref
[1][1] - 16 * vop_ref
[1][1])), w
);
248 virtual_ref
[1][0] = 16 * vop_ref
[0][0] +
249 ROUNDED_DIV(((h
- h2
) * (r
* sprite_ref
[0][0] - 16 * vop_ref
[0][0]) +
250 h2
* (r
* sprite_ref
[2][0] - 16 * vop_ref
[2][0])), h
);
251 virtual_ref
[1][1] = 16 * (vop_ref
[0][1] + h2
) +
252 ROUNDED_DIV(((h
- h2
) * (r
* sprite_ref
[0][1] - 16 * vop_ref
[0][1]) +
253 h2
* (r
* sprite_ref
[2][1] - 16 * vop_ref
[2][1])), h
);
255 switch (ctx
->num_sprite_warping_points
) {
257 s
->sprite_offset
[0][0] =
258 s
->sprite_offset
[0][1] =
259 s
->sprite_offset
[1][0] =
260 s
->sprite_offset
[1][1] = 0;
261 s
->sprite_delta
[0][0] = a
;
262 s
->sprite_delta
[0][1] =
263 s
->sprite_delta
[1][0] = 0;
264 s
->sprite_delta
[1][1] = a
;
265 ctx
->sprite_shift
[0] =
266 ctx
->sprite_shift
[1] = 0;
269 s
->sprite_offset
[0][0] = sprite_ref
[0][0] - a
* vop_ref
[0][0];
270 s
->sprite_offset
[0][1] = sprite_ref
[0][1] - a
* vop_ref
[0][1];
271 s
->sprite_offset
[1][0] = ((sprite_ref
[0][0] >> 1) | (sprite_ref
[0][0] & 1)) -
272 a
* (vop_ref
[0][0] / 2);
273 s
->sprite_offset
[1][1] = ((sprite_ref
[0][1] >> 1) | (sprite_ref
[0][1] & 1)) -
274 a
* (vop_ref
[0][1] / 2);
275 s
->sprite_delta
[0][0] = a
;
276 s
->sprite_delta
[0][1] =
277 s
->sprite_delta
[1][0] = 0;
278 s
->sprite_delta
[1][1] = a
;
279 ctx
->sprite_shift
[0] =
280 ctx
->sprite_shift
[1] = 0;
283 s
->sprite_offset
[0][0] = (sprite_ref
[0][0] << (alpha
+ rho
)) +
284 (-r
* sprite_ref
[0][0] + virtual_ref
[0][0]) *
286 (r
* sprite_ref
[0][1] - virtual_ref
[0][1]) *
287 (-vop_ref
[0][1]) + (1 << (alpha
+ rho
- 1));
288 s
->sprite_offset
[0][1] = (sprite_ref
[0][1] << (alpha
+ rho
)) +
289 (-r
* sprite_ref
[0][1] + virtual_ref
[0][1]) *
291 (-r
* sprite_ref
[0][0] + virtual_ref
[0][0]) *
292 (-vop_ref
[0][1]) + (1 << (alpha
+ rho
- 1));
293 s
->sprite_offset
[1][0] = ((-r
* sprite_ref
[0][0] + virtual_ref
[0][0]) *
294 (-2 * vop_ref
[0][0] + 1) +
295 (r
* sprite_ref
[0][1] - virtual_ref
[0][1]) *
296 (-2 * vop_ref
[0][1] + 1) + 2 * w2
* r
*
297 sprite_ref
[0][0] - 16 * w2
+ (1 << (alpha
+ rho
+ 1)));
298 s
->sprite_offset
[1][1] = ((-r
* sprite_ref
[0][1] + virtual_ref
[0][1]) *
299 (-2 * vop_ref
[0][0] + 1) +
300 (-r
* sprite_ref
[0][0] + virtual_ref
[0][0]) *
301 (-2 * vop_ref
[0][1] + 1) + 2 * w2
* r
*
302 sprite_ref
[0][1] - 16 * w2
+ (1 << (alpha
+ rho
+ 1)));
303 s
->sprite_delta
[0][0] = (-r
* sprite_ref
[0][0] + virtual_ref
[0][0]);
304 s
->sprite_delta
[0][1] = (+r
* sprite_ref
[0][1] - virtual_ref
[0][1]);
305 s
->sprite_delta
[1][0] = (-r
* sprite_ref
[0][1] + virtual_ref
[0][1]);
306 s
->sprite_delta
[1][1] = (-r
* sprite_ref
[0][0] + virtual_ref
[0][0]);
308 ctx
->sprite_shift
[0] = alpha
+ rho
;
309 ctx
->sprite_shift
[1] = alpha
+ rho
+ 2;
312 min_ab
= FFMIN(alpha
, beta
);
315 s
->sprite_offset
[0][0] = (sprite_ref
[0][0] << (alpha
+ beta
+ rho
- min_ab
)) +
316 (-r
* sprite_ref
[0][0] + virtual_ref
[0][0]) *
317 h3
* (-vop_ref
[0][0]) +
318 (-r
* sprite_ref
[0][0] + virtual_ref
[1][0]) *
319 w3
* (-vop_ref
[0][1]) +
320 (1 << (alpha
+ beta
+ rho
- min_ab
- 1));
321 s
->sprite_offset
[0][1] = (sprite_ref
[0][1] << (alpha
+ beta
+ rho
- min_ab
)) +
322 (-r
* sprite_ref
[0][1] + virtual_ref
[0][1]) *
323 h3
* (-vop_ref
[0][0]) +
324 (-r
* sprite_ref
[0][1] + virtual_ref
[1][1]) *
325 w3
* (-vop_ref
[0][1]) +
326 (1 << (alpha
+ beta
+ rho
- min_ab
- 1));
327 s
->sprite_offset
[1][0] = (-r
* sprite_ref
[0][0] + virtual_ref
[0][0]) *
328 h3
* (-2 * vop_ref
[0][0] + 1) +
329 (-r
* sprite_ref
[0][0] + virtual_ref
[1][0]) *
330 w3
* (-2 * vop_ref
[0][1] + 1) + 2 * w2
* h3
*
331 r
* sprite_ref
[0][0] - 16 * w2
* h3
+
332 (1 << (alpha
+ beta
+ rho
- min_ab
+ 1));
333 s
->sprite_offset
[1][1] = (-r
* sprite_ref
[0][1] + virtual_ref
[0][1]) *
334 h3
* (-2 * vop_ref
[0][0] + 1) +
335 (-r
* sprite_ref
[0][1] + virtual_ref
[1][1]) *
336 w3
* (-2 * vop_ref
[0][1] + 1) + 2 * w2
* h3
*
337 r
* sprite_ref
[0][1] - 16 * w2
* h3
+
338 (1 << (alpha
+ beta
+ rho
- min_ab
+ 1));
339 s
->sprite_delta
[0][0] = (-r
* sprite_ref
[0][0] + virtual_ref
[0][0]) * h3
;
340 s
->sprite_delta
[0][1] = (-r
* sprite_ref
[0][0] + virtual_ref
[1][0]) * w3
;
341 s
->sprite_delta
[1][0] = (-r
* sprite_ref
[0][1] + virtual_ref
[0][1]) * h3
;
342 s
->sprite_delta
[1][1] = (-r
* sprite_ref
[0][1] + virtual_ref
[1][1]) * w3
;
344 ctx
->sprite_shift
[0] = alpha
+ beta
+ rho
- min_ab
;
345 ctx
->sprite_shift
[1] = alpha
+ beta
+ rho
- min_ab
+ 2;
348 /* try to simplify the situation */
349 if (s
->sprite_delta
[0][0] == a
<< ctx
->sprite_shift
[0] &&
350 s
->sprite_delta
[0][1] == 0 &&
351 s
->sprite_delta
[1][0] == 0 &&
352 s
->sprite_delta
[1][1] == a
<< ctx
->sprite_shift
[0]) {
353 s
->sprite_offset
[0][0] >>= ctx
->sprite_shift
[0];
354 s
->sprite_offset
[0][1] >>= ctx
->sprite_shift
[0];
355 s
->sprite_offset
[1][0] >>= ctx
->sprite_shift
[1];
356 s
->sprite_offset
[1][1] >>= ctx
->sprite_shift
[1];
357 s
->sprite_delta
[0][0] = a
;
358 s
->sprite_delta
[0][1] = 0;
359 s
->sprite_delta
[1][0] = 0;
360 s
->sprite_delta
[1][1] = a
;
361 ctx
->sprite_shift
[0] = 0;
362 ctx
->sprite_shift
[1] = 0;
363 s
->real_sprite_warping_points
= 1;
365 int shift_y
= 16 - ctx
->sprite_shift
[0];
366 int shift_c
= 16 - ctx
->sprite_shift
[1];
367 for (i
= 0; i
< 2; i
++) {
368 s
->sprite_offset
[0][i
] <<= shift_y
;
369 s
->sprite_offset
[1][i
] <<= shift_c
;
370 s
->sprite_delta
[0][i
] <<= shift_y
;
371 s
->sprite_delta
[1][i
] <<= shift_y
;
372 ctx
->sprite_shift
[i
] = 16;
374 s
->real_sprite_warping_points
= ctx
->num_sprite_warping_points
;
380 static int decode_new_pred(Mpeg4DecContext
*ctx
, GetBitContext
*gb
) {
381 int len
= FFMIN(ctx
->time_increment_bits
+ 3, 15);
386 check_marker(gb
, "after new_pred");
392 * Decode the next video packet.
393 * @return <0 if something went wrong
395 int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext
*ctx
)
397 MpegEncContext
*s
= &ctx
->m
;
399 int mb_num_bits
= av_log2(s
->mb_num
- 1) + 1;
400 int header_extension
= 0, mb_num
, len
;
402 /* is there enough space left for a video packet + header */
403 if (get_bits_count(&s
->gb
) > s
->gb
.size_in_bits
- 20)
406 for (len
= 0; len
< 32; len
++)
407 if (get_bits1(&s
->gb
))
410 if (len
!= ff_mpeg4_get_video_packet_prefix_length(s
)) {
411 av_log(s
->avctx
, AV_LOG_ERROR
, "marker does not match f_code\n");
415 if (ctx
->shape
!= RECT_SHAPE
) {
416 header_extension
= get_bits1(&s
->gb
);
417 // FIXME more stuff here
420 mb_num
= get_bits(&s
->gb
, mb_num_bits
);
421 if (mb_num
>= s
->mb_num
) {
422 av_log(s
->avctx
, AV_LOG_ERROR
,
423 "illegal mb_num in video packet (%d %d) \n", mb_num
, s
->mb_num
);
427 s
->mb_x
= mb_num
% s
->mb_width
;
428 s
->mb_y
= mb_num
/ s
->mb_width
;
430 if (ctx
->shape
!= BIN_ONLY_SHAPE
) {
431 int qscale
= get_bits(&s
->gb
, s
->quant_precision
);
433 s
->chroma_qscale
= s
->qscale
= qscale
;
436 if (ctx
->shape
== RECT_SHAPE
)
437 header_extension
= get_bits1(&s
->gb
);
439 if (header_extension
) {
442 while (get_bits1(&s
->gb
) != 0)
445 check_marker(&s
->gb
, "before time_increment in video packed header");
446 skip_bits(&s
->gb
, ctx
->time_increment_bits
); /* time_increment */
447 check_marker(&s
->gb
, "before vop_coding_type in video packed header");
449 skip_bits(&s
->gb
, 2); /* vop coding type */
450 // FIXME not rect stuff here
452 if (ctx
->shape
!= BIN_ONLY_SHAPE
) {
453 skip_bits(&s
->gb
, 3); /* intra dc vlc threshold */
454 // FIXME don't just ignore everything
455 if (s
->pict_type
== AV_PICTURE_TYPE_S
&&
456 ctx
->vol_sprite_usage
== GMC_SPRITE
) {
457 if (mpeg4_decode_sprite_trajectory(ctx
, &s
->gb
) < 0)
458 return AVERROR_INVALIDDATA
;
459 av_log(s
->avctx
, AV_LOG_ERROR
, "untested\n");
462 // FIXME reduced res stuff here
464 if (s
->pict_type
!= AV_PICTURE_TYPE_I
) {
465 int f_code
= get_bits(&s
->gb
, 3); /* fcode_for */
467 av_log(s
->avctx
, AV_LOG_ERROR
,
468 "Error, video packet header damaged (f_code=0)\n");
470 if (s
->pict_type
== AV_PICTURE_TYPE_B
) {
471 int b_code
= get_bits(&s
->gb
, 3);
473 av_log(s
->avctx
, AV_LOG_ERROR
,
474 "Error, video packet header damaged (b_code=0)\n");
479 decode_new_pred(ctx
, &s
->gb
);
485 * Get the average motion vector for a GMC MB.
486 * @param n either 0 for the x component or 1 for y
487 * @return the average MV for a GMC MB
489 static inline int get_amv(Mpeg4DecContext
*ctx
, int n
)
491 MpegEncContext
*s
= &ctx
->m
;
492 int x
, y
, mb_v
, sum
, dx
, dy
, shift
;
493 int len
= 1 << (s
->f_code
+ 4);
494 const int a
= s
->sprite_warping_accuracy
;
496 if (s
->workaround_bugs
& FF_BUG_AMV
)
497 len
>>= s
->quarter_sample
;
499 if (s
->real_sprite_warping_points
== 1) {
500 if (ctx
->divx_version
== 500 && ctx
->divx_build
== 413)
501 sum
= s
->sprite_offset
[0][n
] / (1 << (a
- s
->quarter_sample
));
503 sum
= RSHIFT(s
->sprite_offset
[0][n
] << s
->quarter_sample
, a
);
505 dx
= s
->sprite_delta
[n
][0];
506 dy
= s
->sprite_delta
[n
][1];
507 shift
= ctx
->sprite_shift
[0];
509 dy
-= 1 << (shift
+ a
+ 1);
511 dx
-= 1 << (shift
+ a
+ 1);
512 mb_v
= s
->sprite_offset
[0][n
] + dx
* s
->mb_x
* 16 + dy
* s
->mb_y
* 16;
515 for (y
= 0; y
< 16; y
++) {
520 for (x
= 0; x
< 16; x
++) {
525 sum
= RSHIFT(sum
, a
+ 8 - s
->quarter_sample
);
537 * Decode the dc value.
538 * @param n block index (0-3 are luma, 4-5 are chroma)
539 * @param dir_ptr the prediction direction will be stored here
540 * @return the quantized dc
542 static inline int mpeg4_decode_dc(MpegEncContext
*s
, int n
, int *dir_ptr
)
547 code
= get_vlc2(&s
->gb
, dc_lum
.table
, DC_VLC_BITS
, 1);
549 code
= get_vlc2(&s
->gb
, dc_chrom
.table
, DC_VLC_BITS
, 1);
551 if (code
< 0 || code
> 9 /* && s->nbit < 9 */) {
552 av_log(s
->avctx
, AV_LOG_ERROR
, "illegal dc vlc\n");
561 level
= 2 * get_bits1(&s
->gb
) - 1;
563 if (get_bits1(&s
->gb
))
564 level
= get_bits(&s
->gb
, code
- 1) + (1 << (code
- 1));
566 level
= -get_bits(&s
->gb
, code
- 1) - (1 << (code
- 1));
569 level
= get_xbits(&s
->gb
, code
);
573 if (get_bits1(&s
->gb
) == 0) { /* marker */
574 if (s
->err_recognition
& (AV_EF_BITSTREAM
|AV_EF_COMPLIANT
)) {
575 av_log(s
->avctx
, AV_LOG_ERROR
, "dc marker bit missing\n");
582 return ff_mpeg4_pred_dc(s
, n
, level
, dir_ptr
, 0);
586 * Decode first partition.
587 * @return number of MBs decoded or <0 if an error occurred
589 static int mpeg4_decode_partition_a(Mpeg4DecContext
*ctx
)
591 MpegEncContext
*s
= &ctx
->m
;
593 static const int8_t quant_tab
[4] = { -1, -2, 1, 2 };
595 /* decode first partition */
596 s
->first_slice_line
= 1;
597 for (; s
->mb_y
< s
->mb_height
; s
->mb_y
++) {
598 ff_init_block_index(s
);
599 for (; s
->mb_x
< s
->mb_width
; s
->mb_x
++) {
600 const int xy
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
605 ff_update_block_index(s
);
606 if (s
->mb_x
== s
->resync_mb_x
&& s
->mb_y
== s
->resync_mb_y
+ 1)
607 s
->first_slice_line
= 0;
609 if (s
->pict_type
== AV_PICTURE_TYPE_I
) {
613 if (show_bits_long(&s
->gb
, 19) == DC_MARKER
)
616 cbpc
= get_vlc2(&s
->gb
, ff_h263_intra_MCBPC_vlc
.table
, INTRA_MCBPC_VLC_BITS
, 2);
618 av_log(s
->avctx
, AV_LOG_ERROR
,
619 "mcbpc corrupted at %d %d\n", s
->mb_x
, s
->mb_y
);
624 s
->cbp_table
[xy
] = cbpc
& 3;
625 s
->current_picture
.mb_type
[xy
] = MB_TYPE_INTRA
;
629 ff_set_qscale(s
, s
->qscale
+ quant_tab
[get_bits(&s
->gb
, 2)]);
631 s
->current_picture
.qscale_table
[xy
] = s
->qscale
;
633 s
->mbintra_table
[xy
] = 1;
634 for (i
= 0; i
< 6; i
++) {
636 int dc
= mpeg4_decode_dc(s
, i
, &dc_pred_dir
);
638 av_log(s
->avctx
, AV_LOG_ERROR
,
639 "DC corrupted at %d %d\n", s
->mb_x
, s
->mb_y
);
646 s
->pred_dir_table
[xy
] = dir
;
647 } else { /* P/S_TYPE */
648 int mx
, my
, pred_x
, pred_y
, bits
;
649 int16_t *const mot_val
= s
->current_picture
.motion_val
[0][s
->block_index
[0]];
650 const int stride
= s
->b8_stride
* 2;
653 bits
= show_bits(&s
->gb
, 17);
654 if (bits
== MOTION_MARKER
)
658 if (bits
& 0x10000) {
660 if (s
->pict_type
== AV_PICTURE_TYPE_S
&&
661 ctx
->vol_sprite_usage
== GMC_SPRITE
) {
662 s
->current_picture
.mb_type
[xy
] = MB_TYPE_SKIP
|
666 mx
= get_amv(ctx
, 0);
667 my
= get_amv(ctx
, 1);
669 s
->current_picture
.mb_type
[xy
] = MB_TYPE_SKIP
|
676 mot_val
[0 + stride
] =
677 mot_val
[2 + stride
] = mx
;
680 mot_val
[1 + stride
] =
681 mot_val
[3 + stride
] = my
;
683 if (s
->mbintra_table
[xy
])
684 ff_clean_intra_table_entries(s
);
688 cbpc
= get_vlc2(&s
->gb
, ff_h263_inter_MCBPC_vlc
.table
, INTER_MCBPC_VLC_BITS
, 2);
690 av_log(s
->avctx
, AV_LOG_ERROR
,
691 "mcbpc corrupted at %d %d\n", s
->mb_x
, s
->mb_y
);
697 s
->cbp_table
[xy
] = cbpc
& (8 + 3); // 8 is dquant
699 s
->mb_intra
= ((cbpc
& 4) != 0);
702 s
->current_picture
.mb_type
[xy
] = MB_TYPE_INTRA
;
703 s
->mbintra_table
[xy
] = 1;
706 mot_val
[0 + stride
] =
707 mot_val
[2 + stride
] = 0;
710 mot_val
[1 + stride
] =
711 mot_val
[3 + stride
] = 0;
713 if (s
->mbintra_table
[xy
])
714 ff_clean_intra_table_entries(s
);
716 if (s
->pict_type
== AV_PICTURE_TYPE_S
&&
717 ctx
->vol_sprite_usage
== GMC_SPRITE
&&
719 s
->mcsel
= get_bits1(&s
->gb
);
723 if ((cbpc
& 16) == 0) {
724 /* 16x16 motion prediction */
726 ff_h263_pred_motion(s
, 0, 0, &pred_x
, &pred_y
);
728 mx
= ff_h263_decode_motion(s
, pred_x
, s
->f_code
);
732 my
= ff_h263_decode_motion(s
, pred_y
, s
->f_code
);
735 s
->current_picture
.mb_type
[xy
] = MB_TYPE_16x16
|
738 mx
= get_amv(ctx
, 0);
739 my
= get_amv(ctx
, 1);
740 s
->current_picture
.mb_type
[xy
] = MB_TYPE_16x16
|
747 mot_val
[0 + stride
] =
748 mot_val
[2 + stride
] = mx
;
751 mot_val
[1 + stride
] =
752 mot_val
[3 + stride
] = my
;
755 s
->current_picture
.mb_type
[xy
] = MB_TYPE_8x8
|
757 for (i
= 0; i
< 4; i
++) {
758 int16_t *mot_val
= ff_h263_pred_motion(s
, i
, 0, &pred_x
, &pred_y
);
759 mx
= ff_h263_decode_motion(s
, pred_x
, s
->f_code
);
763 my
= ff_h263_decode_motion(s
, pred_y
, s
->f_code
);
780 * decode second partition.
781 * @return <0 if an error occurred
783 static int mpeg4_decode_partition_b(MpegEncContext
*s
, int mb_count
)
786 static const int8_t quant_tab
[4] = { -1, -2, 1, 2 };
788 s
->mb_x
= s
->resync_mb_x
;
789 s
->first_slice_line
= 1;
790 for (s
->mb_y
= s
->resync_mb_y
; mb_num
< mb_count
; s
->mb_y
++) {
791 ff_init_block_index(s
);
792 for (; mb_num
< mb_count
&& s
->mb_x
< s
->mb_width
; s
->mb_x
++) {
793 const int xy
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
796 ff_update_block_index(s
);
797 if (s
->mb_x
== s
->resync_mb_x
&& s
->mb_y
== s
->resync_mb_y
+ 1)
798 s
->first_slice_line
= 0;
800 if (s
->pict_type
== AV_PICTURE_TYPE_I
) {
801 int ac_pred
= get_bits1(&s
->gb
);
802 int cbpy
= get_vlc2(&s
->gb
, ff_h263_cbpy_vlc
.table
, CBPY_VLC_BITS
, 1);
804 av_log(s
->avctx
, AV_LOG_ERROR
,
805 "cbpy corrupted at %d %d\n", s
->mb_x
, s
->mb_y
);
809 s
->cbp_table
[xy
] |= cbpy
<< 2;
810 s
->current_picture
.mb_type
[xy
] |= ac_pred
* MB_TYPE_ACPRED
;
811 } else { /* P || S_TYPE */
812 if (IS_INTRA(s
->current_picture
.mb_type
[xy
])) {
815 int ac_pred
= get_bits1(&s
->gb
);
816 int cbpy
= get_vlc2(&s
->gb
, ff_h263_cbpy_vlc
.table
, CBPY_VLC_BITS
, 1);
819 av_log(s
->avctx
, AV_LOG_ERROR
,
820 "I cbpy corrupted at %d %d\n", s
->mb_x
, s
->mb_y
);
824 if (s
->cbp_table
[xy
] & 8)
825 ff_set_qscale(s
, s
->qscale
+ quant_tab
[get_bits(&s
->gb
, 2)]);
826 s
->current_picture
.qscale_table
[xy
] = s
->qscale
;
828 for (i
= 0; i
< 6; i
++) {
830 int dc
= mpeg4_decode_dc(s
, i
, &dc_pred_dir
);
832 av_log(s
->avctx
, AV_LOG_ERROR
,
833 "DC corrupted at %d %d\n", s
->mb_x
, s
->mb_y
);
840 s
->cbp_table
[xy
] &= 3; // remove dquant
841 s
->cbp_table
[xy
] |= cbpy
<< 2;
842 s
->current_picture
.mb_type
[xy
] |= ac_pred
* MB_TYPE_ACPRED
;
843 s
->pred_dir_table
[xy
] = dir
;
844 } else if (IS_SKIP(s
->current_picture
.mb_type
[xy
])) {
845 s
->current_picture
.qscale_table
[xy
] = s
->qscale
;
846 s
->cbp_table
[xy
] = 0;
848 int cbpy
= get_vlc2(&s
->gb
, ff_h263_cbpy_vlc
.table
, CBPY_VLC_BITS
, 1);
851 av_log(s
->avctx
, AV_LOG_ERROR
,
852 "P cbpy corrupted at %d %d\n", s
->mb_x
, s
->mb_y
);
856 if (s
->cbp_table
[xy
] & 8)
857 ff_set_qscale(s
, s
->qscale
+ quant_tab
[get_bits(&s
->gb
, 2)]);
858 s
->current_picture
.qscale_table
[xy
] = s
->qscale
;
860 s
->cbp_table
[xy
] &= 3; // remove dquant
861 s
->cbp_table
[xy
] |= (cbpy
^ 0xf) << 2;
865 if (mb_num
>= mb_count
)
873 * Decode the first and second partition.
874 * @return <0 if error (and sets error type in the error_status_table)
876 int ff_mpeg4_decode_partitions(Mpeg4DecContext
*ctx
)
878 MpegEncContext
*s
= &ctx
->m
;
880 const int part_a_error
= s
->pict_type
== AV_PICTURE_TYPE_I
? (ER_DC_ERROR
| ER_MV_ERROR
) : ER_MV_ERROR
;
881 const int part_a_end
= s
->pict_type
== AV_PICTURE_TYPE_I
? (ER_DC_END
| ER_MV_END
) : ER_MV_END
;
883 mb_num
= mpeg4_decode_partition_a(ctx
);
885 ff_er_add_slice(&s
->er
, s
->resync_mb_x
, s
->resync_mb_y
,
886 s
->mb_x
, s
->mb_y
, part_a_error
);
890 if (s
->resync_mb_x
+ s
->resync_mb_y
* s
->mb_width
+ mb_num
> s
->mb_num
) {
891 av_log(s
->avctx
, AV_LOG_ERROR
, "slice below monitor ...\n");
892 ff_er_add_slice(&s
->er
, s
->resync_mb_x
, s
->resync_mb_y
,
893 s
->mb_x
, s
->mb_y
, part_a_error
);
897 s
->mb_num_left
= mb_num
;
899 if (s
->pict_type
== AV_PICTURE_TYPE_I
) {
900 while (show_bits(&s
->gb
, 9) == 1)
901 skip_bits(&s
->gb
, 9);
902 if (get_bits_long(&s
->gb
, 19) != DC_MARKER
) {
903 av_log(s
->avctx
, AV_LOG_ERROR
,
904 "marker missing after first I partition at %d %d\n",
909 while (show_bits(&s
->gb
, 10) == 1)
910 skip_bits(&s
->gb
, 10);
911 if (get_bits(&s
->gb
, 17) != MOTION_MARKER
) {
912 av_log(s
->avctx
, AV_LOG_ERROR
,
913 "marker missing after first P partition at %d %d\n",
918 ff_er_add_slice(&s
->er
, s
->resync_mb_x
, s
->resync_mb_y
,
919 s
->mb_x
- 1, s
->mb_y
, part_a_end
);
921 if (mpeg4_decode_partition_b(s
, mb_num
) < 0) {
922 if (s
->pict_type
== AV_PICTURE_TYPE_P
)
923 ff_er_add_slice(&s
->er
, s
->resync_mb_x
, s
->resync_mb_y
,
924 s
->mb_x
, s
->mb_y
, ER_DC_ERROR
);
927 if (s
->pict_type
== AV_PICTURE_TYPE_P
)
928 ff_er_add_slice(&s
->er
, s
->resync_mb_x
, s
->resync_mb_y
,
929 s
->mb_x
- 1, s
->mb_y
, ER_DC_END
);
937 * @return <0 if an error occurred
939 static inline int mpeg4_decode_block(Mpeg4DecContext
*ctx
, int16_t *block
,
940 int n
, int coded
, int intra
, int rvlc
)
942 MpegEncContext
*s
= &ctx
->m
;
943 int level
, i
, last
, run
, qmul
, qadd
;
944 int av_uninit(dc_pred_dir
);
947 const uint8_t *scan_table
;
949 // Note intra & rvlc should be optimized away if this is inlined
952 if (ctx
->use_intra_dc_vlc
) {
954 if (s
->partitioned_frame
) {
955 level
= s
->dc_val
[0][s
->block_index
[n
]];
957 level
= FASTDIV((level
+ (s
->y_dc_scale
>> 1)), s
->y_dc_scale
);
959 level
= FASTDIV((level
+ (s
->c_dc_scale
>> 1)), s
->c_dc_scale
);
960 dc_pred_dir
= (s
->pred_dir_table
[s
->mb_x
+ s
->mb_y
* s
->mb_stride
] << n
) & 32;
962 level
= mpeg4_decode_dc(s
, n
, &dc_pred_dir
);
970 ff_mpeg4_pred_dc(s
, n
, 0, &dc_pred_dir
, 0);
976 rl
= &ff_rvlc_rl_intra
;
977 rl_vlc
= ff_rvlc_rl_intra
.rl_vlc
[0];
979 rl
= &ff_mpeg4_rl_intra
;
980 rl_vlc
= ff_mpeg4_rl_intra
.rl_vlc
[0];
983 if (dc_pred_dir
== 0)
984 scan_table
= s
->intra_v_scantable
.permutated
; /* left */
986 scan_table
= s
->intra_h_scantable
.permutated
; /* top */
988 scan_table
= s
->intra_scantable
.permutated
;
995 s
->block_last_index
[n
] = i
;
999 rl
= &ff_rvlc_rl_inter
;
1001 rl
= &ff_h263_rl_inter
;
1003 scan_table
= s
->intra_scantable
.permutated
;
1005 if (s
->mpeg_quant
) {
1009 rl_vlc
= ff_rvlc_rl_inter
.rl_vlc
[0];
1011 rl_vlc
= ff_h263_rl_inter
.rl_vlc
[0];
1013 qmul
= s
->qscale
<< 1;
1014 qadd
= (s
->qscale
- 1) | 1;
1016 rl_vlc
= ff_rvlc_rl_inter
.rl_vlc
[s
->qscale
];
1018 rl_vlc
= ff_h263_rl_inter
.rl_vlc
[s
->qscale
];
1022 OPEN_READER(re
, &s
->gb
);
1024 UPDATE_CACHE(re
, &s
->gb
);
1025 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl_vlc
, TEX_VLC_BITS
, 2, 0);
1029 if (SHOW_UBITS(re
, &s
->gb
, 1) == 0) {
1030 av_log(s
->avctx
, AV_LOG_ERROR
,
1031 "1. marker bit missing in rvlc esc\n");
1034 SKIP_CACHE(re
, &s
->gb
, 1);
1036 last
= SHOW_UBITS(re
, &s
->gb
, 1);
1037 SKIP_CACHE(re
, &s
->gb
, 1);
1038 run
= SHOW_UBITS(re
, &s
->gb
, 6);
1039 SKIP_COUNTER(re
, &s
->gb
, 1 + 1 + 6);
1040 UPDATE_CACHE(re
, &s
->gb
);
1042 if (SHOW_UBITS(re
, &s
->gb
, 1) == 0) {
1043 av_log(s
->avctx
, AV_LOG_ERROR
,
1044 "2. marker bit missing in rvlc esc\n");
1047 SKIP_CACHE(re
, &s
->gb
, 1);
1049 level
= SHOW_UBITS(re
, &s
->gb
, 11);
1050 SKIP_CACHE(re
, &s
->gb
, 11);
1052 if (SHOW_UBITS(re
, &s
->gb
, 5) != 0x10) {
1053 av_log(s
->avctx
, AV_LOG_ERROR
, "reverse esc missing\n");
1056 SKIP_CACHE(re
, &s
->gb
, 5);
1058 level
= level
* qmul
+ qadd
;
1059 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
1060 SKIP_COUNTER(re
, &s
->gb
, 1 + 11 + 5 + 1);
1067 cache
= GET_CACHE(re
, &s
->gb
);
1070 cache
^= 0xC0000000;
1072 if (cache
& 0x80000000) {
1073 if (cache
& 0x40000000) {
1075 SKIP_CACHE(re
, &s
->gb
, 2);
1076 last
= SHOW_UBITS(re
, &s
->gb
, 1);
1077 SKIP_CACHE(re
, &s
->gb
, 1);
1078 run
= SHOW_UBITS(re
, &s
->gb
, 6);
1079 SKIP_COUNTER(re
, &s
->gb
, 2 + 1 + 6);
1080 UPDATE_CACHE(re
, &s
->gb
);
1083 level
= SHOW_SBITS(re
, &s
->gb
, 12);
1084 LAST_SKIP_BITS(re
, &s
->gb
, 12);
1086 if (SHOW_UBITS(re
, &s
->gb
, 1) == 0) {
1087 av_log(s
->avctx
, AV_LOG_ERROR
,
1088 "1. marker bit missing in 3. esc\n");
1089 if (!(s
->err_recognition
& AV_EF_IGNORE_ERR
))
1092 SKIP_CACHE(re
, &s
->gb
, 1);
1094 level
= SHOW_SBITS(re
, &s
->gb
, 12);
1095 SKIP_CACHE(re
, &s
->gb
, 12);
1097 if (SHOW_UBITS(re
, &s
->gb
, 1) == 0) {
1098 av_log(s
->avctx
, AV_LOG_ERROR
,
1099 "2. marker bit missing in 3. esc\n");
1100 if (!(s
->err_recognition
& AV_EF_IGNORE_ERR
))
1104 SKIP_COUNTER(re
, &s
->gb
, 1 + 12 + 1);
1108 if (s
->error_recognition
>= FF_ER_COMPLIANT
) {
1109 const int abs_level
= FFABS(level
);
1110 if (abs_level
<=MAX_LEVEL
&& run
<=MAX_RUN
) {
1111 const int run1
= run
- rl
->max_run
[last
][abs_level
] - 1;
1112 if (abs_level
<= rl
->max_level
[last
][run
]) {
1113 av_log(s
->avctx
, AV_LOG_ERROR
, "illegal 3. esc, vlc encoding possible\n");
1116 if (s
->error_recognition
> FF_ER_COMPLIANT
) {
1117 if (abs_level
<= rl
->max_level
[last
][run
]*2) {
1118 av_log(s
->avctx
, AV_LOG_ERROR
, "illegal 3. esc, esc 1 encoding possible\n");
1121 if (run1
>= 0 && abs_level
<= rl
->max_level
[last
][run1
]) {
1122 av_log(s
->avctx
, AV_LOG_ERROR
, "illegal 3. esc, esc 2 encoding possible\n");
1130 level
= level
* qmul
+ qadd
;
1132 level
= level
* qmul
- qadd
;
1134 if ((unsigned)(level
+ 2048) > 4095) {
1135 if (s
->err_recognition
& (AV_EF_BITSTREAM
|AV_EF_AGGRESSIVE
)) {
1136 if (level
> 2560 || level
< -2560) {
1137 av_log(s
->avctx
, AV_LOG_ERROR
,
1138 "|level| overflow in 3. esc, qp=%d\n",
1143 level
= level
< 0 ? -2048 : 2047;
1151 SKIP_BITS(re
, &s
->gb
, 2);
1152 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl_vlc
, TEX_VLC_BITS
, 2, 1);
1153 i
+= run
+ rl
->max_run
[run
>> 7][level
/ qmul
] + 1; // FIXME opt indexing
1154 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
1155 LAST_SKIP_BITS(re
, &s
->gb
, 1);
1159 SKIP_BITS(re
, &s
->gb
, 1);
1160 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl_vlc
, TEX_VLC_BITS
, 2, 1);
1162 level
= level
+ rl
->max_level
[run
>> 7][(run
- 1) & 63] * qmul
; // FIXME opt indexing
1163 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
1164 LAST_SKIP_BITS(re
, &s
->gb
, 1);
1169 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
1170 LAST_SKIP_BITS(re
, &s
->gb
, 1);
1172 tprintf(s
->avctx
, "dct[%d][%d] = %- 4d end?:%d\n", scan_table
[i
&63]&7, scan_table
[i
&63] >> 3, level
, i
>62);
1176 av_log(s
->avctx
, AV_LOG_ERROR
,
1177 "ac-tex damaged at %d %d\n", s
->mb_x
, s
->mb_y
);
1181 block
[scan_table
[i
]] = level
;
1185 block
[scan_table
[i
]] = level
;
1187 CLOSE_READER(re
, &s
->gb
);
1192 if (!ctx
->use_intra_dc_vlc
) {
1193 block
[0] = ff_mpeg4_pred_dc(s
, n
, block
[0], &dc_pred_dir
, 0);
1195 i
-= i
>> 31; // if (i == -1) i = 0;
1198 ff_mpeg4_pred_ac(s
, block
, n
, dc_pred_dir
);
1200 i
= 63; // FIXME not optimal
1202 s
->block_last_index
[n
] = i
;
1207 * decode partition C of one MB.
1208 * @return <0 if an error occurred
1210 static int mpeg4_decode_partitioned_mb(MpegEncContext
*s
, int16_t block
[6][64])
1212 Mpeg4DecContext
*ctx
= (Mpeg4DecContext
*)s
;
1214 const int xy
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
1216 mb_type
= s
->current_picture
.mb_type
[xy
];
1217 cbp
= s
->cbp_table
[xy
];
1219 ctx
->use_intra_dc_vlc
= s
->qscale
< ctx
->intra_dc_threshold
;
1221 if (s
->current_picture
.qscale_table
[xy
] != s
->qscale
)
1222 ff_set_qscale(s
, s
->current_picture
.qscale_table
[xy
]);
1224 if (s
->pict_type
== AV_PICTURE_TYPE_P
||
1225 s
->pict_type
== AV_PICTURE_TYPE_S
) {
1227 for (i
= 0; i
< 4; i
++) {
1228 s
->mv
[0][i
][0] = s
->current_picture
.motion_val
[0][s
->block_index
[i
]][0];
1229 s
->mv
[0][i
][1] = s
->current_picture
.motion_val
[0][s
->block_index
[i
]][1];
1231 s
->mb_intra
= IS_INTRA(mb_type
);
1233 if (IS_SKIP(mb_type
)) {
1235 for (i
= 0; i
< 6; i
++)
1236 s
->block_last_index
[i
] = -1;
1237 s
->mv_dir
= MV_DIR_FORWARD
;
1238 s
->mv_type
= MV_TYPE_16X16
;
1239 if (s
->pict_type
== AV_PICTURE_TYPE_S
1240 && ctx
->vol_sprite_usage
== GMC_SPRITE
) {
1247 } else if (s
->mb_intra
) {
1248 s
->ac_pred
= IS_ACPRED(s
->current_picture
.mb_type
[xy
]);
1249 } else if (!s
->mb_intra
) {
1250 // s->mcsel = 0; // FIXME do we need to init that?
1252 s
->mv_dir
= MV_DIR_FORWARD
;
1253 if (IS_8X8(mb_type
)) {
1254 s
->mv_type
= MV_TYPE_8X8
;
1256 s
->mv_type
= MV_TYPE_16X16
;
1259 } else { /* I-Frame */
1261 s
->ac_pred
= IS_ACPRED(s
->current_picture
.mb_type
[xy
]);
1264 if (!IS_SKIP(mb_type
)) {
1266 s
->bdsp
.clear_blocks(s
->block
[0]);
1267 /* decode each block */
1268 for (i
= 0; i
< 6; i
++) {
1269 if (mpeg4_decode_block(ctx
, block
[i
], i
, cbp
& 32, s
->mb_intra
, ctx
->rvlc
) < 0) {
1270 av_log(s
->avctx
, AV_LOG_ERROR
,
1271 "texture corrupted at %d %d %d\n",
1272 s
->mb_x
, s
->mb_y
, s
->mb_intra
);
1279 /* per-MB end of slice check */
1280 if (--s
->mb_num_left
<= 0) {
1281 if (mpeg4_is_resync(ctx
))
1286 if (mpeg4_is_resync(ctx
)) {
1287 const int delta
= s
->mb_x
+ 1 == s
->mb_width
? 2 : 1;
1288 if (s
->cbp_table
[xy
+ delta
])
1295 static int mpeg4_decode_mb(MpegEncContext
*s
, int16_t block
[6][64])
1297 Mpeg4DecContext
*ctx
= (Mpeg4DecContext
*)s
;
1298 int cbpc
, cbpy
, i
, cbp
, pred_x
, pred_y
, mx
, my
, dquant
;
1300 static int8_t quant_tab
[4] = { -1, -2, 1, 2 };
1301 const int xy
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
1303 av_assert2(s
->h263_pred
);
1305 if (s
->pict_type
== AV_PICTURE_TYPE_P
||
1306 s
->pict_type
== AV_PICTURE_TYPE_S
) {
1308 if (get_bits1(&s
->gb
)) {
1311 for (i
= 0; i
< 6; i
++)
1312 s
->block_last_index
[i
] = -1;
1313 s
->mv_dir
= MV_DIR_FORWARD
;
1314 s
->mv_type
= MV_TYPE_16X16
;
1315 if (s
->pict_type
== AV_PICTURE_TYPE_S
&&
1316 ctx
->vol_sprite_usage
== GMC_SPRITE
) {
1317 s
->current_picture
.mb_type
[xy
] = MB_TYPE_SKIP
|
1322 s
->mv
[0][0][0] = get_amv(ctx
, 0);
1323 s
->mv
[0][0][1] = get_amv(ctx
, 1);
1326 s
->current_picture
.mb_type
[xy
] = MB_TYPE_SKIP
|
1336 cbpc
= get_vlc2(&s
->gb
, ff_h263_inter_MCBPC_vlc
.table
, INTER_MCBPC_VLC_BITS
, 2);
1338 av_log(s
->avctx
, AV_LOG_ERROR
,
1339 "mcbpc damaged at %d %d\n", s
->mb_x
, s
->mb_y
);
1342 } while (cbpc
== 20);
1344 s
->bdsp
.clear_blocks(s
->block
[0]);
1346 s
->mb_intra
= ((cbpc
& 4) != 0);
1350 if (s
->pict_type
== AV_PICTURE_TYPE_S
&&
1351 ctx
->vol_sprite_usage
== GMC_SPRITE
&& (cbpc
& 16) == 0)
1352 s
->mcsel
= get_bits1(&s
->gb
);
1355 cbpy
= get_vlc2(&s
->gb
, ff_h263_cbpy_vlc
.table
, CBPY_VLC_BITS
, 1) ^ 0x0F;
1357 cbp
= (cbpc
& 3) | (cbpy
<< 2);
1359 ff_set_qscale(s
, s
->qscale
+ quant_tab
[get_bits(&s
->gb
, 2)]);
1360 if ((!s
->progressive_sequence
) &&
1361 (cbp
|| (s
->workaround_bugs
& FF_BUG_XVID_ILACE
)))
1362 s
->interlaced_dct
= get_bits1(&s
->gb
);
1364 s
->mv_dir
= MV_DIR_FORWARD
;
1365 if ((cbpc
& 16) == 0) {
1367 s
->current_picture
.mb_type
[xy
] = MB_TYPE_GMC
|
1370 /* 16x16 global motion prediction */
1371 s
->mv_type
= MV_TYPE_16X16
;
1372 mx
= get_amv(ctx
, 0);
1373 my
= get_amv(ctx
, 1);
1374 s
->mv
[0][0][0] = mx
;
1375 s
->mv
[0][0][1] = my
;
1376 } else if ((!s
->progressive_sequence
) && get_bits1(&s
->gb
)) {
1377 s
->current_picture
.mb_type
[xy
] = MB_TYPE_16x8
|
1380 /* 16x8 field motion prediction */
1381 s
->mv_type
= MV_TYPE_FIELD
;
1383 s
->field_select
[0][0] = get_bits1(&s
->gb
);
1384 s
->field_select
[0][1] = get_bits1(&s
->gb
);
1386 ff_h263_pred_motion(s
, 0, 0, &pred_x
, &pred_y
);
1388 for (i
= 0; i
< 2; i
++) {
1389 mx
= ff_h263_decode_motion(s
, pred_x
, s
->f_code
);
1393 my
= ff_h263_decode_motion(s
, pred_y
/ 2, s
->f_code
);
1397 s
->mv
[0][i
][0] = mx
;
1398 s
->mv
[0][i
][1] = my
;
1401 s
->current_picture
.mb_type
[xy
] = MB_TYPE_16x16
| MB_TYPE_L0
;
1402 /* 16x16 motion prediction */
1403 s
->mv_type
= MV_TYPE_16X16
;
1404 ff_h263_pred_motion(s
, 0, 0, &pred_x
, &pred_y
);
1405 mx
= ff_h263_decode_motion(s
, pred_x
, s
->f_code
);
1410 my
= ff_h263_decode_motion(s
, pred_y
, s
->f_code
);
1414 s
->mv
[0][0][0] = mx
;
1415 s
->mv
[0][0][1] = my
;
1418 s
->current_picture
.mb_type
[xy
] = MB_TYPE_8x8
| MB_TYPE_L0
;
1419 s
->mv_type
= MV_TYPE_8X8
;
1420 for (i
= 0; i
< 4; i
++) {
1421 mot_val
= ff_h263_pred_motion(s
, i
, 0, &pred_x
, &pred_y
);
1422 mx
= ff_h263_decode_motion(s
, pred_x
, s
->f_code
);
1426 my
= ff_h263_decode_motion(s
, pred_y
, s
->f_code
);
1429 s
->mv
[0][i
][0] = mx
;
1430 s
->mv
[0][i
][1] = my
;
1435 } else if (s
->pict_type
== AV_PICTURE_TYPE_B
) {
1436 int modb1
; // first bit of modb
1437 int modb2
; // second bit of modb
1440 s
->mb_intra
= 0; // B-frames never contain intra blocks
1441 s
->mcsel
= 0; // ... true gmc blocks
1444 for (i
= 0; i
< 2; i
++) {
1445 s
->last_mv
[i
][0][0] =
1446 s
->last_mv
[i
][0][1] =
1447 s
->last_mv
[i
][1][0] =
1448 s
->last_mv
[i
][1][1] = 0;
1451 ff_thread_await_progress(&s
->next_picture_ptr
->tf
, s
->mb_y
, 0);
1454 /* if we skipped it in the future P Frame than skip it now too */
1455 s
->mb_skipped
= s
->next_picture
.mbskip_table
[s
->mb_y
* s
->mb_stride
+ s
->mb_x
]; // Note, skiptab=0 if last was GMC
1457 if (s
->mb_skipped
) {
1459 for (i
= 0; i
< 6; i
++)
1460 s
->block_last_index
[i
] = -1;
1462 s
->mv_dir
= MV_DIR_FORWARD
;
1463 s
->mv_type
= MV_TYPE_16X16
;
1468 s
->current_picture
.mb_type
[xy
] = MB_TYPE_SKIP
|
1474 modb1
= get_bits1(&s
->gb
);
1476 // like MB_TYPE_B_DIRECT but no vectors coded
1477 mb_type
= MB_TYPE_DIRECT2
| MB_TYPE_SKIP
| MB_TYPE_L0L1
;
1480 modb2
= get_bits1(&s
->gb
);
1481 mb_type
= get_vlc2(&s
->gb
, mb_type_b_vlc
.table
, MB_TYPE_B_VLC_BITS
, 1);
1483 av_log(s
->avctx
, AV_LOG_ERROR
, "illegal MB_type\n");
1486 mb_type
= mb_type_b_map
[mb_type
];
1490 s
->bdsp
.clear_blocks(s
->block
[0]);
1491 cbp
= get_bits(&s
->gb
, 6);
1494 if ((!IS_DIRECT(mb_type
)) && cbp
) {
1495 if (get_bits1(&s
->gb
))
1496 ff_set_qscale(s
, s
->qscale
+ get_bits1(&s
->gb
) * 4 - 2);
1499 if (!s
->progressive_sequence
) {
1501 s
->interlaced_dct
= get_bits1(&s
->gb
);
1503 if (!IS_DIRECT(mb_type
) && get_bits1(&s
->gb
)) {
1504 mb_type
|= MB_TYPE_16x8
| MB_TYPE_INTERLACED
;
1505 mb_type
&= ~MB_TYPE_16x16
;
1507 if (USES_LIST(mb_type
, 0)) {
1508 s
->field_select
[0][0] = get_bits1(&s
->gb
);
1509 s
->field_select
[0][1] = get_bits1(&s
->gb
);
1511 if (USES_LIST(mb_type
, 1)) {
1512 s
->field_select
[1][0] = get_bits1(&s
->gb
);
1513 s
->field_select
[1][1] = get_bits1(&s
->gb
);
1519 if ((mb_type
& (MB_TYPE_DIRECT2
| MB_TYPE_INTERLACED
)) == 0) {
1520 s
->mv_type
= MV_TYPE_16X16
;
1522 if (USES_LIST(mb_type
, 0)) {
1523 s
->mv_dir
= MV_DIR_FORWARD
;
1525 mx
= ff_h263_decode_motion(s
, s
->last_mv
[0][0][0], s
->f_code
);
1526 my
= ff_h263_decode_motion(s
, s
->last_mv
[0][0][1], s
->f_code
);
1527 s
->last_mv
[0][1][0] =
1528 s
->last_mv
[0][0][0] =
1529 s
->mv
[0][0][0] = mx
;
1530 s
->last_mv
[0][1][1] =
1531 s
->last_mv
[0][0][1] =
1532 s
->mv
[0][0][1] = my
;
1535 if (USES_LIST(mb_type
, 1)) {
1536 s
->mv_dir
|= MV_DIR_BACKWARD
;
1538 mx
= ff_h263_decode_motion(s
, s
->last_mv
[1][0][0], s
->b_code
);
1539 my
= ff_h263_decode_motion(s
, s
->last_mv
[1][0][1], s
->b_code
);
1540 s
->last_mv
[1][1][0] =
1541 s
->last_mv
[1][0][0] =
1542 s
->mv
[1][0][0] = mx
;
1543 s
->last_mv
[1][1][1] =
1544 s
->last_mv
[1][0][1] =
1545 s
->mv
[1][0][1] = my
;
1547 } else if (!IS_DIRECT(mb_type
)) {
1548 s
->mv_type
= MV_TYPE_FIELD
;
1550 if (USES_LIST(mb_type
, 0)) {
1551 s
->mv_dir
= MV_DIR_FORWARD
;
1553 for (i
= 0; i
< 2; i
++) {
1554 mx
= ff_h263_decode_motion(s
, s
->last_mv
[0][i
][0], s
->f_code
);
1555 my
= ff_h263_decode_motion(s
, s
->last_mv
[0][i
][1] / 2, s
->f_code
);
1556 s
->last_mv
[0][i
][0] =
1557 s
->mv
[0][i
][0] = mx
;
1558 s
->last_mv
[0][i
][1] = (s
->mv
[0][i
][1] = my
) * 2;
1562 if (USES_LIST(mb_type
, 1)) {
1563 s
->mv_dir
|= MV_DIR_BACKWARD
;
1565 for (i
= 0; i
< 2; i
++) {
1566 mx
= ff_h263_decode_motion(s
, s
->last_mv
[1][i
][0], s
->b_code
);
1567 my
= ff_h263_decode_motion(s
, s
->last_mv
[1][i
][1] / 2, s
->b_code
);
1568 s
->last_mv
[1][i
][0] =
1569 s
->mv
[1][i
][0] = mx
;
1570 s
->last_mv
[1][i
][1] = (s
->mv
[1][i
][1] = my
) * 2;
1576 if (IS_DIRECT(mb_type
)) {
1577 if (IS_SKIP(mb_type
)) {
1581 mx
= ff_h263_decode_motion(s
, 0, 1);
1582 my
= ff_h263_decode_motion(s
, 0, 1);
1585 s
->mv_dir
= MV_DIR_FORWARD
| MV_DIR_BACKWARD
| MV_DIRECT
;
1586 mb_type
|= ff_mpeg4_set_direct_mv(s
, mx
, my
);
1588 s
->current_picture
.mb_type
[xy
] = mb_type
;
1589 } else { /* I-Frame */
1591 cbpc
= get_vlc2(&s
->gb
, ff_h263_intra_MCBPC_vlc
.table
, INTRA_MCBPC_VLC_BITS
, 2);
1593 av_log(s
->avctx
, AV_LOG_ERROR
,
1594 "I cbpc damaged at %d %d\n", s
->mb_x
, s
->mb_y
);
1597 } while (cbpc
== 8);
1603 s
->ac_pred
= get_bits1(&s
->gb
);
1605 s
->current_picture
.mb_type
[xy
] = MB_TYPE_INTRA
| MB_TYPE_ACPRED
;
1607 s
->current_picture
.mb_type
[xy
] = MB_TYPE_INTRA
;
1609 cbpy
= get_vlc2(&s
->gb
, ff_h263_cbpy_vlc
.table
, CBPY_VLC_BITS
, 1);
1611 av_log(s
->avctx
, AV_LOG_ERROR
,
1612 "I cbpy damaged at %d %d\n", s
->mb_x
, s
->mb_y
);
1615 cbp
= (cbpc
& 3) | (cbpy
<< 2);
1617 ctx
->use_intra_dc_vlc
= s
->qscale
< ctx
->intra_dc_threshold
;
1620 ff_set_qscale(s
, s
->qscale
+ quant_tab
[get_bits(&s
->gb
, 2)]);
1622 if (!s
->progressive_sequence
)
1623 s
->interlaced_dct
= get_bits1(&s
->gb
);
1625 s
->bdsp
.clear_blocks(s
->block
[0]);
1626 /* decode each block */
1627 for (i
= 0; i
< 6; i
++) {
1628 if (mpeg4_decode_block(ctx
, block
[i
], i
, cbp
& 32, 1, 0) < 0)
1635 /* decode each block */
1636 for (i
= 0; i
< 6; i
++) {
1637 if (mpeg4_decode_block(ctx
, block
[i
], i
, cbp
& 32, 0, 0) < 0)
1643 /* per-MB end of slice check */
1644 if (s
->codec_id
== AV_CODEC_ID_MPEG4
) {
1645 int next
= mpeg4_is_resync(ctx
);
1647 if (s
->mb_x
+ s
->mb_y
*s
->mb_width
+ 1 > next
&& (s
->avctx
->err_recognition
& AV_EF_AGGRESSIVE
)) {
1649 } else if (s
->mb_x
+ s
->mb_y
*s
->mb_width
+ 1 >= next
)
1652 if (s
->pict_type
== AV_PICTURE_TYPE_B
) {
1653 const int delta
= s
->mb_x
+ 1 == s
->mb_width
? 2 : 1;
1654 ff_thread_await_progress(&s
->next_picture_ptr
->tf
,
1655 (s
->mb_x
+ delta
>= s
->mb_width
)
1656 ? FFMIN(s
->mb_y
+ 1, s
->mb_height
- 1)
1658 if (s
->next_picture
.mbskip_table
[xy
+ delta
])
1669 static int mpeg4_decode_gop_header(MpegEncContext
*s
, GetBitContext
*gb
)
1671 int hours
, minutes
, seconds
;
1673 if (!show_bits(gb
, 23)) {
1674 av_log(s
->avctx
, AV_LOG_WARNING
, "GOP header invalid\n");
1678 hours
= get_bits(gb
, 5);
1679 minutes
= get_bits(gb
, 6);
1681 seconds
= get_bits(gb
, 6);
1683 s
->time_base
= seconds
+ 60*(minutes
+ 60*hours
);
1691 static int mpeg4_decode_profile_level(MpegEncContext
*s
, GetBitContext
*gb
)
1694 s
->avctx
->profile
= get_bits(gb
, 4);
1695 s
->avctx
->level
= get_bits(gb
, 4);
1697 // for Simple profile, level 0
1698 if (s
->avctx
->profile
== 0 && s
->avctx
->level
== 8) {
1699 s
->avctx
->level
= 0;
1705 static int decode_vol_header(Mpeg4DecContext
*ctx
, GetBitContext
*gb
)
1707 MpegEncContext
*s
= &ctx
->m
;
1708 int width
, height
, vo_ver_id
;
1711 skip_bits(gb
, 1); /* random access */
1712 s
->vo_type
= get_bits(gb
, 8);
1713 if (get_bits1(gb
) != 0) { /* is_ol_id */
1714 vo_ver_id
= get_bits(gb
, 4); /* vo_ver_id */
1715 skip_bits(gb
, 3); /* vo_priority */
1719 s
->aspect_ratio_info
= get_bits(gb
, 4);
1720 if (s
->aspect_ratio_info
== FF_ASPECT_EXTENDED
) {
1721 s
->avctx
->sample_aspect_ratio
.num
= get_bits(gb
, 8); // par_width
1722 s
->avctx
->sample_aspect_ratio
.den
= get_bits(gb
, 8); // par_height
1724 s
->avctx
->sample_aspect_ratio
= ff_h263_pixel_aspect
[s
->aspect_ratio_info
];
1727 if ((ctx
->vol_control_parameters
= get_bits1(gb
))) { /* vol control parameter */
1728 int chroma_format
= get_bits(gb
, 2);
1729 if (chroma_format
!= CHROMA_420
)
1730 av_log(s
->avctx
, AV_LOG_ERROR
, "illegal chroma format\n");
1732 s
->low_delay
= get_bits1(gb
);
1733 if (get_bits1(gb
)) { /* vbv parameters */
1734 get_bits(gb
, 15); /* first_half_bitrate */
1735 skip_bits1(gb
); /* marker */
1736 get_bits(gb
, 15); /* latter_half_bitrate */
1737 skip_bits1(gb
); /* marker */
1738 get_bits(gb
, 15); /* first_half_vbv_buffer_size */
1739 skip_bits1(gb
); /* marker */
1740 get_bits(gb
, 3); /* latter_half_vbv_buffer_size */
1741 get_bits(gb
, 11); /* first_half_vbv_occupancy */
1742 skip_bits1(gb
); /* marker */
1743 get_bits(gb
, 15); /* latter_half_vbv_occupancy */
1744 skip_bits1(gb
); /* marker */
1747 /* is setting low delay flag only once the smartest thing to do?
1748 * low delay detection won't be overridden. */
1749 if (s
->picture_number
== 0)
1753 ctx
->shape
= get_bits(gb
, 2); /* vol shape */
1754 if (ctx
->shape
!= RECT_SHAPE
)
1755 av_log(s
->avctx
, AV_LOG_ERROR
, "only rectangular vol supported\n");
1756 if (ctx
->shape
== GRAY_SHAPE
&& vo_ver_id
!= 1) {
1757 av_log(s
->avctx
, AV_LOG_ERROR
, "Gray shape not supported\n");
1758 skip_bits(gb
, 4); /* video_object_layer_shape_extension */
1761 check_marker(gb
, "before time_increment_resolution");
1763 s
->avctx
->framerate
.num
= get_bits(gb
, 16);
1764 if (!s
->avctx
->framerate
.num
) {
1765 av_log(s
->avctx
, AV_LOG_ERROR
, "framerate==0\n");
1766 return AVERROR_INVALIDDATA
;
1769 ctx
->time_increment_bits
= av_log2(s
->avctx
->framerate
.num
- 1) + 1;
1770 if (ctx
->time_increment_bits
< 1)
1771 ctx
->time_increment_bits
= 1;
1773 check_marker(gb
, "before fixed_vop_rate");
1775 if (get_bits1(gb
) != 0) /* fixed_vop_rate */
1776 s
->avctx
->framerate
.den
= get_bits(gb
, ctx
->time_increment_bits
);
1778 s
->avctx
->framerate
.den
= 1;
1780 s
->avctx
->time_base
= av_inv_q(av_mul_q(s
->avctx
->framerate
, (AVRational
){s
->avctx
->ticks_per_frame
, 1}));
1784 if (ctx
->shape
!= BIN_ONLY_SHAPE
) {
1785 if (ctx
->shape
== RECT_SHAPE
) {
1786 check_marker(gb
, "before width");
1787 width
= get_bits(gb
, 13);
1788 check_marker(gb
, "before height");
1789 height
= get_bits(gb
, 13);
1790 check_marker(gb
, "after height");
1791 if (width
&& height
&& /* they should be non zero but who knows */
1792 !(s
->width
&& s
->codec_tag
== AV_RL32("MP4S"))) {
1793 if (s
->width
&& s
->height
&&
1794 (s
->width
!= width
|| s
->height
!= height
))
1795 s
->context_reinit
= 1;
1801 s
->progressive_sequence
=
1802 s
->progressive_frame
= get_bits1(gb
) ^ 1;
1803 s
->interlaced_dct
= 0;
1804 if (!get_bits1(gb
) && (s
->avctx
->debug
& FF_DEBUG_PICT_INFO
))
1805 av_log(s
->avctx
, AV_LOG_INFO
, /* OBMC Disable */
1806 "MPEG4 OBMC not supported (very likely buggy encoder)\n");
1808 ctx
->vol_sprite_usage
= get_bits1(gb
); /* vol_sprite_usage */
1810 ctx
->vol_sprite_usage
= get_bits(gb
, 2); /* vol_sprite_usage */
1812 if (ctx
->vol_sprite_usage
== STATIC_SPRITE
)
1813 av_log(s
->avctx
, AV_LOG_ERROR
, "Static Sprites not supported\n");
1814 if (ctx
->vol_sprite_usage
== STATIC_SPRITE
||
1815 ctx
->vol_sprite_usage
== GMC_SPRITE
) {
1816 if (ctx
->vol_sprite_usage
== STATIC_SPRITE
) {
1817 skip_bits(gb
, 13); // sprite_width
1818 skip_bits1(gb
); /* marker */
1819 skip_bits(gb
, 13); // sprite_height
1820 skip_bits1(gb
); /* marker */
1821 skip_bits(gb
, 13); // sprite_left
1822 skip_bits1(gb
); /* marker */
1823 skip_bits(gb
, 13); // sprite_top
1824 skip_bits1(gb
); /* marker */
1826 ctx
->num_sprite_warping_points
= get_bits(gb
, 6);
1827 if (ctx
->num_sprite_warping_points
> 3) {
1828 av_log(s
->avctx
, AV_LOG_ERROR
,
1829 "%d sprite_warping_points\n",
1830 ctx
->num_sprite_warping_points
);
1831 ctx
->num_sprite_warping_points
= 0;
1832 return AVERROR_INVALIDDATA
;
1834 s
->sprite_warping_accuracy
= get_bits(gb
, 2);
1835 ctx
->sprite_brightness_change
= get_bits1(gb
);
1836 if (ctx
->vol_sprite_usage
== STATIC_SPRITE
)
1837 skip_bits1(gb
); // low_latency_sprite
1839 // FIXME sadct disable bit if verid!=1 && shape not rect
1841 if (get_bits1(gb
) == 1) { /* not_8_bit */
1842 s
->quant_precision
= get_bits(gb
, 4); /* quant_precision */
1843 if (get_bits(gb
, 4) != 8) /* bits_per_pixel */
1844 av_log(s
->avctx
, AV_LOG_ERROR
, "N-bit not supported\n");
1845 if (s
->quant_precision
!= 5)
1846 av_log(s
->avctx
, AV_LOG_ERROR
,
1847 "quant precision %d\n", s
->quant_precision
);
1848 if (s
->quant_precision
<3 || s
->quant_precision
>9) {
1849 s
->quant_precision
= 5;
1852 s
->quant_precision
= 5;
1855 // FIXME a bunch of grayscale shape things
1857 if ((s
->mpeg_quant
= get_bits1(gb
))) { /* vol_quant_type */
1860 /* load default matrixes */
1861 for (i
= 0; i
< 64; i
++) {
1862 int j
= s
->idsp
.idct_permutation
[i
];
1863 v
= ff_mpeg4_default_intra_matrix
[i
];
1864 s
->intra_matrix
[j
] = v
;
1865 s
->chroma_intra_matrix
[j
] = v
;
1867 v
= ff_mpeg4_default_non_intra_matrix
[i
];
1868 s
->inter_matrix
[j
] = v
;
1869 s
->chroma_inter_matrix
[j
] = v
;
1872 /* load custom intra matrix */
1873 if (get_bits1(gb
)) {
1875 for (i
= 0; i
< 64; i
++) {
1877 v
= get_bits(gb
, 8);
1882 j
= s
->idsp
.idct_permutation
[ff_zigzag_direct
[i
]];
1883 s
->intra_matrix
[j
] = last
;
1884 s
->chroma_intra_matrix
[j
] = last
;
1887 /* replicate last value */
1888 for (; i
< 64; i
++) {
1889 int j
= s
->idsp
.idct_permutation
[ff_zigzag_direct
[i
]];
1890 s
->intra_matrix
[j
] = last
;
1891 s
->chroma_intra_matrix
[j
] = last
;
1895 /* load custom non intra matrix */
1896 if (get_bits1(gb
)) {
1898 for (i
= 0; i
< 64; i
++) {
1900 v
= get_bits(gb
, 8);
1905 j
= s
->idsp
.idct_permutation
[ff_zigzag_direct
[i
]];
1906 s
->inter_matrix
[j
] = v
;
1907 s
->chroma_inter_matrix
[j
] = v
;
1910 /* replicate last value */
1911 for (; i
< 64; i
++) {
1912 int j
= s
->idsp
.idct_permutation
[ff_zigzag_direct
[i
]];
1913 s
->inter_matrix
[j
] = last
;
1914 s
->chroma_inter_matrix
[j
] = last
;
1918 // FIXME a bunch of grayscale shape things
1922 s
->quarter_sample
= get_bits1(gb
);
1924 s
->quarter_sample
= 0;
1926 if (get_bits_left(gb
) < 4) {
1927 av_log(s
->avctx
, AV_LOG_ERROR
, "VOL Header truncated\n");
1928 return AVERROR_INVALIDDATA
;
1931 if (!get_bits1(gb
)) {
1932 int pos
= get_bits_count(gb
);
1933 int estimation_method
= get_bits(gb
, 2);
1934 if (estimation_method
< 2) {
1935 if (!get_bits1(gb
)) {
1936 ctx
->cplx_estimation_trash_i
+= 8 * get_bits1(gb
); /* opaque */
1937 ctx
->cplx_estimation_trash_i
+= 8 * get_bits1(gb
); /* transparent */
1938 ctx
->cplx_estimation_trash_i
+= 8 * get_bits1(gb
); /* intra_cae */
1939 ctx
->cplx_estimation_trash_i
+= 8 * get_bits1(gb
); /* inter_cae */
1940 ctx
->cplx_estimation_trash_i
+= 8 * get_bits1(gb
); /* no_update */
1941 ctx
->cplx_estimation_trash_i
+= 8 * get_bits1(gb
); /* upampling */
1943 if (!get_bits1(gb
)) {
1944 ctx
->cplx_estimation_trash_i
+= 8 * get_bits1(gb
); /* intra_blocks */
1945 ctx
->cplx_estimation_trash_p
+= 8 * get_bits1(gb
); /* inter_blocks */
1946 ctx
->cplx_estimation_trash_p
+= 8 * get_bits1(gb
); /* inter4v_blocks */
1947 ctx
->cplx_estimation_trash_i
+= 8 * get_bits1(gb
); /* not coded blocks */
1949 if (!check_marker(gb
, "in complexity estimation part 1")) {
1950 skip_bits_long(gb
, pos
- get_bits_count(gb
));
1953 if (!get_bits1(gb
)) {
1954 ctx
->cplx_estimation_trash_i
+= 8 * get_bits1(gb
); /* dct_coeffs */
1955 ctx
->cplx_estimation_trash_i
+= 8 * get_bits1(gb
); /* dct_lines */
1956 ctx
->cplx_estimation_trash_i
+= 8 * get_bits1(gb
); /* vlc_syms */
1957 ctx
->cplx_estimation_trash_i
+= 4 * get_bits1(gb
); /* vlc_bits */
1959 if (!get_bits1(gb
)) {
1960 ctx
->cplx_estimation_trash_p
+= 8 * get_bits1(gb
); /* apm */
1961 ctx
->cplx_estimation_trash_p
+= 8 * get_bits1(gb
); /* npm */
1962 ctx
->cplx_estimation_trash_b
+= 8 * get_bits1(gb
); /* interpolate_mc_q */
1963 ctx
->cplx_estimation_trash_p
+= 8 * get_bits1(gb
); /* forwback_mc_q */
1964 ctx
->cplx_estimation_trash_p
+= 8 * get_bits1(gb
); /* halfpel2 */
1965 ctx
->cplx_estimation_trash_p
+= 8 * get_bits1(gb
); /* halfpel4 */
1967 if (!check_marker(gb
, "in complexity estimation part 2")) {
1968 skip_bits_long(gb
, pos
- get_bits_count(gb
));
1971 if (estimation_method
== 1) {
1972 ctx
->cplx_estimation_trash_i
+= 8 * get_bits1(gb
); /* sadct */
1973 ctx
->cplx_estimation_trash_p
+= 8 * get_bits1(gb
); /* qpel */
1976 av_log(s
->avctx
, AV_LOG_ERROR
,
1977 "Invalid Complexity estimation method %d\n",
1982 ctx
->cplx_estimation_trash_i
=
1983 ctx
->cplx_estimation_trash_p
=
1984 ctx
->cplx_estimation_trash_b
= 0;
1987 ctx
->resync_marker
= !get_bits1(gb
); /* resync_marker_disabled */
1989 s
->data_partitioning
= get_bits1(gb
);
1990 if (s
->data_partitioning
)
1991 ctx
->rvlc
= get_bits1(gb
);
1993 if (vo_ver_id
!= 1) {
1994 ctx
->new_pred
= get_bits1(gb
);
1995 if (ctx
->new_pred
) {
1996 av_log(s
->avctx
, AV_LOG_ERROR
, "new pred not supported\n");
1997 skip_bits(gb
, 2); /* requested upstream message type */
1998 skip_bits1(gb
); /* newpred segment type */
2000 if (get_bits1(gb
)) // reduced_res_vop
2001 av_log(s
->avctx
, AV_LOG_ERROR
,
2002 "reduced resolution VOP not supported\n");
2007 ctx
->scalability
= get_bits1(gb
);
2009 if (ctx
->scalability
) {
2010 GetBitContext bak
= *gb
;
2011 int h_sampling_factor_n
;
2012 int h_sampling_factor_m
;
2013 int v_sampling_factor_n
;
2014 int v_sampling_factor_m
;
2016 skip_bits1(gb
); // hierarchy_type
2017 skip_bits(gb
, 4); /* ref_layer_id */
2018 skip_bits1(gb
); /* ref_layer_sampling_dir */
2019 h_sampling_factor_n
= get_bits(gb
, 5);
2020 h_sampling_factor_m
= get_bits(gb
, 5);
2021 v_sampling_factor_n
= get_bits(gb
, 5);
2022 v_sampling_factor_m
= get_bits(gb
, 5);
2023 ctx
->enhancement_type
= get_bits1(gb
);
2025 if (h_sampling_factor_n
== 0 || h_sampling_factor_m
== 0 ||
2026 v_sampling_factor_n
== 0 || v_sampling_factor_m
== 0) {
2027 /* illegal scalability header (VERY broken encoder),
2028 * trying to workaround */
2029 ctx
->scalability
= 0;
2032 av_log(s
->avctx
, AV_LOG_ERROR
, "scalability not supported\n");
2034 // bin shape stuff FIXME
2038 if (s
->avctx
->debug
&FF_DEBUG_PICT_INFO
) {
2039 av_log(s
->avctx
, AV_LOG_DEBUG
, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d, %s%s%s%s\n",
2040 s
->avctx
->framerate
.den
, s
->avctx
->framerate
.num
,
2041 ctx
->time_increment_bits
,
2043 s
->progressive_sequence
,
2044 ctx
->scalability
? "scalability " :"" , s
->quarter_sample
? "qpel " : "",
2045 s
->data_partitioning
? "partition " : "", ctx
->rvlc
? "rvlc " : ""
2053 * Decode the user data stuff in the header.
2054 * Also initializes divx/xvid/lavc_version/build.
2056 static int decode_user_data(Mpeg4DecContext
*ctx
, GetBitContext
*gb
)
2058 MpegEncContext
*s
= &ctx
->m
;
2062 int ver
= 0, build
= 0, ver2
= 0, ver3
= 0;
2065 for (i
= 0; i
< 255 && get_bits_count(gb
) < gb
->size_in_bits
; i
++) {
2066 if (show_bits(gb
, 23) == 0)
2068 buf
[i
] = get_bits(gb
, 8);
2072 /* divx detection */
2073 e
= sscanf(buf
, "DivX%dBuild%d%c", &ver
, &build
, &last
);
2075 e
= sscanf(buf
, "DivX%db%d%c", &ver
, &build
, &last
);
2077 ctx
->divx_version
= ver
;
2078 ctx
->divx_build
= build
;
2079 s
->divx_packed
= e
== 3 && last
== 'p';
2080 if (s
->divx_packed
&& !ctx
->showed_packed_warning
) {
2081 av_log(s
->avctx
, AV_LOG_INFO
, "Video uses a non-standard and "
2082 "wasteful way to store B-frames ('packed B-frames'). "
2083 "Consider using a tool like VirtualDub or avidemux to fix it.\n");
2084 ctx
->showed_packed_warning
= 1;
2088 /* libavcodec detection */
2089 e
= sscanf(buf
, "FFmpe%*[^b]b%d", &build
) + 3;
2091 e
= sscanf(buf
, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver
, &ver2
, &ver3
, &build
);
2093 e
= sscanf(buf
, "Lavc%d.%d.%d", &ver
, &ver2
, &ver3
) + 1;
2095 build
= (ver
<< 16) + (ver2
<< 8) + ver3
;
2098 if (strcmp(buf
, "ffmpeg") == 0)
2099 ctx
->lavc_build
= 4600;
2102 ctx
->lavc_build
= build
;
2104 /* Xvid detection */
2105 e
= sscanf(buf
, "XviD%d", &build
);
2107 ctx
->xvid_build
= build
;
2112 int ff_mpeg4_workaround_bugs(AVCodecContext
*avctx
)
2114 Mpeg4DecContext
*ctx
= avctx
->priv_data
;
2115 MpegEncContext
*s
= &ctx
->m
;
2117 if (ctx
->xvid_build
== -1 && ctx
->divx_version
== -1 && ctx
->lavc_build
== -1) {
2118 if (s
->stream_codec_tag
== AV_RL32("XVID") ||
2119 s
->codec_tag
== AV_RL32("XVID") ||
2120 s
->codec_tag
== AV_RL32("XVIX") ||
2121 s
->codec_tag
== AV_RL32("RMP4") ||
2122 s
->codec_tag
== AV_RL32("ZMP4") ||
2123 s
->codec_tag
== AV_RL32("SIPP"))
2124 ctx
->xvid_build
= 0;
2127 if (ctx
->xvid_build
== -1 && ctx
->divx_version
== -1 && ctx
->lavc_build
== -1)
2128 if (s
->codec_tag
== AV_RL32("DIVX") && s
->vo_type
== 0 &&
2129 ctx
->vol_control_parameters
== 0)
2130 ctx
->divx_version
= 400; // divx 4
2132 if (ctx
->xvid_build
>= 0 && ctx
->divx_version
>= 0) {
2134 ctx
->divx_build
= -1;
2137 if (s
->workaround_bugs
& FF_BUG_AUTODETECT
) {
2138 if (s
->codec_tag
== AV_RL32("XVIX"))
2139 s
->workaround_bugs
|= FF_BUG_XVID_ILACE
;
2141 if (s
->codec_tag
== AV_RL32("UMP4"))
2142 s
->workaround_bugs
|= FF_BUG_UMP4
;
2144 if (ctx
->divx_version
>= 500 && ctx
->divx_build
< 1814)
2145 s
->workaround_bugs
|= FF_BUG_QPEL_CHROMA
;
2147 if (ctx
->divx_version
> 502 && ctx
->divx_build
< 1814)
2148 s
->workaround_bugs
|= FF_BUG_QPEL_CHROMA2
;
2150 if (ctx
->xvid_build
<= 3U)
2151 s
->padding_bug_score
= 256 * 256 * 256 * 64;
2153 if (ctx
->xvid_build
<= 1U)
2154 s
->workaround_bugs
|= FF_BUG_QPEL_CHROMA
;
2156 if (ctx
->xvid_build
<= 12U)
2157 s
->workaround_bugs
|= FF_BUG_EDGE
;
2159 if (ctx
->xvid_build
<= 32U)
2160 s
->workaround_bugs
|= FF_BUG_DC_CLIP
;
2162 #define SET_QPEL_FUNC(postfix1, postfix2) \
2163 s->qdsp.put_ ## postfix1 = ff_put_ ## postfix2; \
2164 s->qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
2165 s->qdsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
2167 if (ctx
->lavc_build
< 4653U)
2168 s
->workaround_bugs
|= FF_BUG_STD_QPEL
;
2170 if (ctx
->lavc_build
< 4655U)
2171 s
->workaround_bugs
|= FF_BUG_DIRECT_BLOCKSIZE
;
2173 if (ctx
->lavc_build
< 4670U)
2174 s
->workaround_bugs
|= FF_BUG_EDGE
;
2176 if (ctx
->lavc_build
<= 4712U)
2177 s
->workaround_bugs
|= FF_BUG_DC_CLIP
;
2179 if (ctx
->divx_version
>= 0)
2180 s
->workaround_bugs
|= FF_BUG_DIRECT_BLOCKSIZE
;
2181 if (ctx
->divx_version
== 501 && ctx
->divx_build
== 20020416)
2182 s
->padding_bug_score
= 256 * 256 * 256 * 64;
2184 if (ctx
->divx_version
< 500U)
2185 s
->workaround_bugs
|= FF_BUG_EDGE
;
2187 if (ctx
->divx_version
>= 0)
2188 s
->workaround_bugs
|= FF_BUG_HPEL_CHROMA
;
2191 if (s
->workaround_bugs
& FF_BUG_STD_QPEL
) {
2192 SET_QPEL_FUNC(qpel_pixels_tab
[0][5], qpel16_mc11_old_c
)
2193 SET_QPEL_FUNC(qpel_pixels_tab
[0][7], qpel16_mc31_old_c
)
2194 SET_QPEL_FUNC(qpel_pixels_tab
[0][9], qpel16_mc12_old_c
)
2195 SET_QPEL_FUNC(qpel_pixels_tab
[0][11], qpel16_mc32_old_c
)
2196 SET_QPEL_FUNC(qpel_pixels_tab
[0][13], qpel16_mc13_old_c
)
2197 SET_QPEL_FUNC(qpel_pixels_tab
[0][15], qpel16_mc33_old_c
)
2199 SET_QPEL_FUNC(qpel_pixels_tab
[1][5], qpel8_mc11_old_c
)
2200 SET_QPEL_FUNC(qpel_pixels_tab
[1][7], qpel8_mc31_old_c
)
2201 SET_QPEL_FUNC(qpel_pixels_tab
[1][9], qpel8_mc12_old_c
)
2202 SET_QPEL_FUNC(qpel_pixels_tab
[1][11], qpel8_mc32_old_c
)
2203 SET_QPEL_FUNC(qpel_pixels_tab
[1][13], qpel8_mc13_old_c
)
2204 SET_QPEL_FUNC(qpel_pixels_tab
[1][15], qpel8_mc33_old_c
)
2207 if (avctx
->debug
& FF_DEBUG_BUGS
)
2208 av_log(s
->avctx
, AV_LOG_DEBUG
,
2209 "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
2210 s
->workaround_bugs
, ctx
->lavc_build
, ctx
->xvid_build
,
2211 ctx
->divx_version
, ctx
->divx_build
, s
->divx_packed
? "p" : "");
2213 if (CONFIG_MPEG4_DECODER
&& ctx
->xvid_build
>= 0 &&
2214 s
->codec_id
== AV_CODEC_ID_MPEG4
&&
2215 avctx
->idct_algo
== FF_IDCT_AUTO
) {
2216 avctx
->idct_algo
= FF_IDCT_XVID
;
2217 ff_mpv_idct_init(s
);
2224 static int decode_vop_header(Mpeg4DecContext
*ctx
, GetBitContext
*gb
)
2226 MpegEncContext
*s
= &ctx
->m
;
2227 int time_incr
, time_increment
;
2230 s
->pict_type
= get_bits(gb
, 2) + AV_PICTURE_TYPE_I
; /* pict type: I = 0 , P = 1 */
2231 if (s
->pict_type
== AV_PICTURE_TYPE_B
&& s
->low_delay
&&
2232 ctx
->vol_control_parameters
== 0 && !(s
->flags
& CODEC_FLAG_LOW_DELAY
)) {
2233 av_log(s
->avctx
, AV_LOG_ERROR
, "low_delay flag set incorrectly, clearing it\n");
2237 s
->partitioned_frame
= s
->data_partitioning
&& s
->pict_type
!= AV_PICTURE_TYPE_B
;
2238 if (s
->partitioned_frame
)
2239 s
->decode_mb
= mpeg4_decode_partitioned_mb
;
2241 s
->decode_mb
= mpeg4_decode_mb
;
2244 while (get_bits1(gb
) != 0)
2247 check_marker(gb
, "before time_increment");
2249 if (ctx
->time_increment_bits
== 0 ||
2250 !(show_bits(gb
, ctx
->time_increment_bits
+ 1) & 1)) {
2251 av_log(s
->avctx
, AV_LOG_ERROR
,
2252 "hmm, seems the headers are not complete, trying to guess time_increment_bits\n");
2254 for (ctx
->time_increment_bits
= 1;
2255 ctx
->time_increment_bits
< 16;
2256 ctx
->time_increment_bits
++) {
2257 if (s
->pict_type
== AV_PICTURE_TYPE_P
||
2258 (s
->pict_type
== AV_PICTURE_TYPE_S
&&
2259 ctx
->vol_sprite_usage
== GMC_SPRITE
)) {
2260 if ((show_bits(gb
, ctx
->time_increment_bits
+ 6) & 0x37) == 0x30)
2262 } else if ((show_bits(gb
, ctx
->time_increment_bits
+ 5) & 0x1F) == 0x18)
2266 av_log(s
->avctx
, AV_LOG_ERROR
,
2267 "my guess is %d bits ;)\n", ctx
->time_increment_bits
);
2268 if (s
->avctx
->framerate
.num
&& 4*s
->avctx
->framerate
.num
< 1<<ctx
->time_increment_bits
) {
2269 s
->avctx
->framerate
.num
= 1<<ctx
->time_increment_bits
;
2270 s
->avctx
->time_base
= av_inv_q(av_mul_q(s
->avctx
->framerate
, (AVRational
){s
->avctx
->ticks_per_frame
, 1}));
2275 time_increment
= get_bits1(gb
); // FIXME investigate further
2277 time_increment
= get_bits(gb
, ctx
->time_increment_bits
);
2279 if (s
->pict_type
!= AV_PICTURE_TYPE_B
) {
2280 s
->last_time_base
= s
->time_base
;
2281 s
->time_base
+= time_incr
;
2282 s
->time
= s
->time_base
* s
->avctx
->framerate
.num
+ time_increment
;
2283 if (s
->workaround_bugs
& FF_BUG_UMP4
) {
2284 if (s
->time
< s
->last_non_b_time
) {
2285 /* header is not mpeg-4-compatible, broken encoder,
2286 * trying to workaround */
2288 s
->time
+= s
->avctx
->framerate
.num
;
2291 s
->pp_time
= s
->time
- s
->last_non_b_time
;
2292 s
->last_non_b_time
= s
->time
;
2294 s
->time
= (s
->last_time_base
+ time_incr
) * s
->avctx
->framerate
.num
+ time_increment
;
2295 s
->pb_time
= s
->pp_time
- (s
->last_non_b_time
- s
->time
);
2296 if (s
->pp_time
<= s
->pb_time
||
2297 s
->pp_time
<= s
->pp_time
- s
->pb_time
||
2299 /* messed up order, maybe after seeking? skipping current b-frame */
2300 return FRAME_SKIPPED
;
2302 ff_mpeg4_init_direct_mv(s
);
2304 if (ctx
->t_frame
== 0)
2305 ctx
->t_frame
= s
->pb_time
;
2306 if (ctx
->t_frame
== 0)
2307 ctx
->t_frame
= 1; // 1/0 protection
2308 s
->pp_field_time
= (ROUNDED_DIV(s
->last_non_b_time
, ctx
->t_frame
) -
2309 ROUNDED_DIV(s
->last_non_b_time
- s
->pp_time
, ctx
->t_frame
)) * 2;
2310 s
->pb_field_time
= (ROUNDED_DIV(s
->time
, ctx
->t_frame
) -
2311 ROUNDED_DIV(s
->last_non_b_time
- s
->pp_time
, ctx
->t_frame
)) * 2;
2312 if (s
->pp_field_time
<= s
->pb_field_time
|| s
->pb_field_time
<= 1) {
2313 s
->pb_field_time
= 2;
2314 s
->pp_field_time
= 4;
2315 if (!s
->progressive_sequence
)
2316 return FRAME_SKIPPED
;
2320 if (s
->avctx
->framerate
.den
)
2321 pts
= ROUNDED_DIV(s
->time
, s
->avctx
->framerate
.den
);
2323 pts
= AV_NOPTS_VALUE
;
2324 if (s
->avctx
->debug
&FF_DEBUG_PTS
)
2325 av_log(s
->avctx
, AV_LOG_DEBUG
, "MPEG4 PTS: %"PRId64
"\n",
2328 check_marker(gb
, "before vop_coded");
2331 if (get_bits1(gb
) != 1) {
2332 if (s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
2333 av_log(s
->avctx
, AV_LOG_ERROR
, "vop not coded\n");
2334 return FRAME_SKIPPED
;
2337 decode_new_pred(ctx
, gb
);
2339 if (ctx
->shape
!= BIN_ONLY_SHAPE
&&
2340 (s
->pict_type
== AV_PICTURE_TYPE_P
||
2341 (s
->pict_type
== AV_PICTURE_TYPE_S
&&
2342 ctx
->vol_sprite_usage
== GMC_SPRITE
))) {
2343 /* rounding type for motion estimation */
2344 s
->no_rounding
= get_bits1(gb
);
2348 // FIXME reduced res stuff
2350 if (ctx
->shape
!= RECT_SHAPE
) {
2351 if (ctx
->vol_sprite_usage
!= 1 || s
->pict_type
!= AV_PICTURE_TYPE_I
) {
2352 skip_bits(gb
, 13); /* width */
2353 skip_bits1(gb
); /* marker */
2354 skip_bits(gb
, 13); /* height */
2355 skip_bits1(gb
); /* marker */
2356 skip_bits(gb
, 13); /* hor_spat_ref */
2357 skip_bits1(gb
); /* marker */
2358 skip_bits(gb
, 13); /* ver_spat_ref */
2360 skip_bits1(gb
); /* change_CR_disable */
2362 if (get_bits1(gb
) != 0)
2363 skip_bits(gb
, 8); /* constant_alpha_value */
2366 // FIXME complexity estimation stuff
2368 if (ctx
->shape
!= BIN_ONLY_SHAPE
) {
2369 skip_bits_long(gb
, ctx
->cplx_estimation_trash_i
);
2370 if (s
->pict_type
!= AV_PICTURE_TYPE_I
)
2371 skip_bits_long(gb
, ctx
->cplx_estimation_trash_p
);
2372 if (s
->pict_type
== AV_PICTURE_TYPE_B
)
2373 skip_bits_long(gb
, ctx
->cplx_estimation_trash_b
);
2375 if (get_bits_left(gb
) < 3) {
2376 av_log(s
->avctx
, AV_LOG_ERROR
, "Header truncated\n");
2377 return AVERROR_INVALIDDATA
;
2379 ctx
->intra_dc_threshold
= ff_mpeg4_dc_threshold
[get_bits(gb
, 3)];
2380 if (!s
->progressive_sequence
) {
2381 s
->top_field_first
= get_bits1(gb
);
2382 s
->alternate_scan
= get_bits1(gb
);
2384 s
->alternate_scan
= 0;
2387 if (s
->alternate_scan
) {
2388 ff_init_scantable(s
->idsp
.idct_permutation
, &s
->inter_scantable
, ff_alternate_vertical_scan
);
2389 ff_init_scantable(s
->idsp
.idct_permutation
, &s
->intra_scantable
, ff_alternate_vertical_scan
);
2390 ff_init_scantable(s
->idsp
.idct_permutation
, &s
->intra_h_scantable
, ff_alternate_vertical_scan
);
2391 ff_init_scantable(s
->idsp
.idct_permutation
, &s
->intra_v_scantable
, ff_alternate_vertical_scan
);
2393 ff_init_scantable(s
->idsp
.idct_permutation
, &s
->inter_scantable
, ff_zigzag_direct
);
2394 ff_init_scantable(s
->idsp
.idct_permutation
, &s
->intra_scantable
, ff_zigzag_direct
);
2395 ff_init_scantable(s
->idsp
.idct_permutation
, &s
->intra_h_scantable
, ff_alternate_horizontal_scan
);
2396 ff_init_scantable(s
->idsp
.idct_permutation
, &s
->intra_v_scantable
, ff_alternate_vertical_scan
);
2399 if (s
->pict_type
== AV_PICTURE_TYPE_S
&&
2400 (ctx
->vol_sprite_usage
== STATIC_SPRITE
||
2401 ctx
->vol_sprite_usage
== GMC_SPRITE
)) {
2402 if (mpeg4_decode_sprite_trajectory(ctx
, gb
) < 0)
2403 return AVERROR_INVALIDDATA
;
2404 if (ctx
->sprite_brightness_change
)
2405 av_log(s
->avctx
, AV_LOG_ERROR
,
2406 "sprite_brightness_change not supported\n");
2407 if (ctx
->vol_sprite_usage
== STATIC_SPRITE
)
2408 av_log(s
->avctx
, AV_LOG_ERROR
, "static sprite not supported\n");
2411 if (ctx
->shape
!= BIN_ONLY_SHAPE
) {
2412 s
->chroma_qscale
= s
->qscale
= get_bits(gb
, s
->quant_precision
);
2413 if (s
->qscale
== 0) {
2414 av_log(s
->avctx
, AV_LOG_ERROR
,
2415 "Error, header damaged or not MPEG4 header (qscale=0)\n");
2416 return AVERROR_INVALIDDATA
; // makes no sense to continue, as there is nothing left from the image then
2419 if (s
->pict_type
!= AV_PICTURE_TYPE_I
) {
2420 s
->f_code
= get_bits(gb
, 3); /* fcode_for */
2421 if (s
->f_code
== 0) {
2422 av_log(s
->avctx
, AV_LOG_ERROR
,
2423 "Error, header damaged or not MPEG4 header (f_code=0)\n");
2425 return AVERROR_INVALIDDATA
; // makes no sense to continue, as there is nothing left from the image then
2430 if (s
->pict_type
== AV_PICTURE_TYPE_B
) {
2431 s
->b_code
= get_bits(gb
, 3);
2432 if (s
->b_code
== 0) {
2433 av_log(s
->avctx
, AV_LOG_ERROR
,
2434 "Error, header damaged or not MPEG4 header (b_code=0)\n");
2436 return AVERROR_INVALIDDATA
; // makes no sense to continue, as the MV decoding will break very quickly
2441 if (s
->avctx
->debug
& FF_DEBUG_PICT_INFO
) {
2442 av_log(s
->avctx
, AV_LOG_DEBUG
,
2443 "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",
2444 s
->qscale
, s
->f_code
, s
->b_code
,
2445 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")),
2446 gb
->size_in_bits
,s
->progressive_sequence
, s
->alternate_scan
,
2447 s
->top_field_first
, s
->quarter_sample
? "q" : "h",
2448 s
->data_partitioning
, ctx
->resync_marker
,
2449 ctx
->num_sprite_warping_points
, s
->sprite_warping_accuracy
,
2450 1 - s
->no_rounding
, s
->vo_type
,
2451 ctx
->vol_control_parameters
? " VOLC" : " ", ctx
->intra_dc_threshold
,
2452 ctx
->cplx_estimation_trash_i
, ctx
->cplx_estimation_trash_p
,
2453 ctx
->cplx_estimation_trash_b
,
2459 if (!ctx
->scalability
) {
2460 if (ctx
->shape
!= RECT_SHAPE
&& s
->pict_type
!= AV_PICTURE_TYPE_I
)
2461 skip_bits1(gb
); // vop shape coding type
2463 if (ctx
->enhancement_type
) {
2464 int load_backward_shape
= get_bits1(gb
);
2465 if (load_backward_shape
)
2466 av_log(s
->avctx
, AV_LOG_ERROR
,
2467 "load backward shape isn't supported\n");
2469 skip_bits(gb
, 2); // ref_select_code
2472 /* detect buggy encoders which don't set the low_delay flag
2473 * (divx4/xvid/opendivx). Note we cannot detect divx5 without b-frames
2474 * easily (although it's buggy too) */
2475 if (s
->vo_type
== 0 && ctx
->vol_control_parameters
== 0 &&
2476 ctx
->divx_version
== -1 && s
->picture_number
== 0) {
2477 av_log(s
->avctx
, AV_LOG_WARNING
,
2478 "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
2482 s
->picture_number
++; // better than pic number==0 always ;)
2484 // FIXME add short header support
2485 s
->y_dc_scale_table
= ff_mpeg4_y_dc_scale_table
;
2486 s
->c_dc_scale_table
= ff_mpeg4_c_dc_scale_table
;
2488 if (s
->workaround_bugs
& FF_BUG_EDGE
) {
2489 s
->h_edge_pos
= s
->width
;
2490 s
->v_edge_pos
= s
->height
;
2496 * Decode mpeg4 headers.
2497 * @return <0 if no VOP found (or a damaged one)
2498 * FRAME_SKIPPED if a not coded VOP is found
2499 * 0 if a VOP is found
2501 int ff_mpeg4_decode_picture_header(Mpeg4DecContext
*ctx
, GetBitContext
*gb
)
2503 MpegEncContext
*s
= &ctx
->m
;
2504 unsigned startcode
, v
;
2507 /* search next start code */
2510 if (s
->codec_tag
== AV_RL32("WV1F") && show_bits(gb
, 24) == 0x575630) {
2512 if (get_bits(gb
, 8) == 0xF0)
2518 if (get_bits_count(gb
) >= gb
->size_in_bits
) {
2519 if (gb
->size_in_bits
== 8 &&
2520 (ctx
->divx_version
>= 0 || ctx
->xvid_build
>= 0) || s
->codec_tag
== AV_RL32("QMP4")) {
2521 av_log(s
->avctx
, AV_LOG_VERBOSE
, "frame skip %d\n", gb
->size_in_bits
);
2522 return FRAME_SKIPPED
; // divx bug
2524 return -1; // end of stream
2527 /* use the bits after the test */
2528 v
= get_bits(gb
, 8);
2529 startcode
= ((startcode
<< 8) | v
) & 0xffffffff;
2531 if ((startcode
& 0xFFFFFF00) != 0x100)
2532 continue; // no startcode
2534 if (s
->avctx
->debug
& FF_DEBUG_STARTCODE
) {
2535 av_log(s
->avctx
, AV_LOG_DEBUG
, "startcode: %3X ", startcode
);
2536 if (startcode
<= 0x11F)
2537 av_log(s
->avctx
, AV_LOG_DEBUG
, "Video Object Start");
2538 else if (startcode
<= 0x12F)
2539 av_log(s
->avctx
, AV_LOG_DEBUG
, "Video Object Layer Start");
2540 else if (startcode
<= 0x13F)
2541 av_log(s
->avctx
, AV_LOG_DEBUG
, "Reserved");
2542 else if (startcode
<= 0x15F)
2543 av_log(s
->avctx
, AV_LOG_DEBUG
, "FGS bp start");
2544 else if (startcode
<= 0x1AF)
2545 av_log(s
->avctx
, AV_LOG_DEBUG
, "Reserved");
2546 else if (startcode
== 0x1B0)
2547 av_log(s
->avctx
, AV_LOG_DEBUG
, "Visual Object Seq Start");
2548 else if (startcode
== 0x1B1)
2549 av_log(s
->avctx
, AV_LOG_DEBUG
, "Visual Object Seq End");
2550 else if (startcode
== 0x1B2)
2551 av_log(s
->avctx
, AV_LOG_DEBUG
, "User Data");
2552 else if (startcode
== 0x1B3)
2553 av_log(s
->avctx
, AV_LOG_DEBUG
, "Group of VOP start");
2554 else if (startcode
== 0x1B4)
2555 av_log(s
->avctx
, AV_LOG_DEBUG
, "Video Session Error");
2556 else if (startcode
== 0x1B5)
2557 av_log(s
->avctx
, AV_LOG_DEBUG
, "Visual Object Start");
2558 else if (startcode
== 0x1B6)
2559 av_log(s
->avctx
, AV_LOG_DEBUG
, "Video Object Plane start");
2560 else if (startcode
== 0x1B7)
2561 av_log(s
->avctx
, AV_LOG_DEBUG
, "slice start");
2562 else if (startcode
== 0x1B8)
2563 av_log(s
->avctx
, AV_LOG_DEBUG
, "extension start");
2564 else if (startcode
== 0x1B9)
2565 av_log(s
->avctx
, AV_LOG_DEBUG
, "fgs start");
2566 else if (startcode
== 0x1BA)
2567 av_log(s
->avctx
, AV_LOG_DEBUG
, "FBA Object start");
2568 else if (startcode
== 0x1BB)
2569 av_log(s
->avctx
, AV_LOG_DEBUG
, "FBA Object Plane start");
2570 else if (startcode
== 0x1BC)
2571 av_log(s
->avctx
, AV_LOG_DEBUG
, "Mesh Object start");
2572 else if (startcode
== 0x1BD)
2573 av_log(s
->avctx
, AV_LOG_DEBUG
, "Mesh Object Plane start");
2574 else if (startcode
== 0x1BE)
2575 av_log(s
->avctx
, AV_LOG_DEBUG
, "Still Texture Object start");
2576 else if (startcode
== 0x1BF)
2577 av_log(s
->avctx
, AV_LOG_DEBUG
, "Texture Spatial Layer start");
2578 else if (startcode
== 0x1C0)
2579 av_log(s
->avctx
, AV_LOG_DEBUG
, "Texture SNR Layer start");
2580 else if (startcode
== 0x1C1)
2581 av_log(s
->avctx
, AV_LOG_DEBUG
, "Texture Tile start");
2582 else if (startcode
== 0x1C2)
2583 av_log(s
->avctx
, AV_LOG_DEBUG
, "Texture Shape Layer start");
2584 else if (startcode
== 0x1C3)
2585 av_log(s
->avctx
, AV_LOG_DEBUG
, "stuffing start");
2586 else if (startcode
<= 0x1C5)
2587 av_log(s
->avctx
, AV_LOG_DEBUG
, "reserved");
2588 else if (startcode
<= 0x1FF)
2589 av_log(s
->avctx
, AV_LOG_DEBUG
, "System start");
2590 av_log(s
->avctx
, AV_LOG_DEBUG
, " at %d\n", get_bits_count(gb
));
2593 if (startcode
>= 0x120 && startcode
<= 0x12F) {
2594 if ((ret
= decode_vol_header(ctx
, gb
)) < 0)
2596 } else if (startcode
== USER_DATA_STARTCODE
) {
2597 decode_user_data(ctx
, gb
);
2598 } else if (startcode
== GOP_STARTCODE
) {
2599 mpeg4_decode_gop_header(s
, gb
);
2600 } else if (startcode
== VOS_STARTCODE
) {
2601 mpeg4_decode_profile_level(s
, gb
);
2602 } else if (startcode
== VOP_STARTCODE
) {
2611 if (s
->flags
& CODEC_FLAG_LOW_DELAY
)
2613 s
->avctx
->has_b_frames
= !s
->low_delay
;
2615 return decode_vop_header(ctx
, gb
);
2618 av_cold
void ff_mpeg4videodec_static_init(void) {
2619 static int done
= 0;
2622 ff_init_rl(&ff_mpeg4_rl_intra
, ff_mpeg4_static_rl_table_store
[0]);
2623 ff_init_rl(&ff_rvlc_rl_inter
, ff_mpeg4_static_rl_table_store
[1]);
2624 ff_init_rl(&ff_rvlc_rl_intra
, ff_mpeg4_static_rl_table_store
[2]);
2625 INIT_VLC_RL(ff_mpeg4_rl_intra
, 554);
2626 INIT_VLC_RL(ff_rvlc_rl_inter
, 1072);
2627 INIT_VLC_RL(ff_rvlc_rl_intra
, 1072);
2628 INIT_VLC_STATIC(&dc_lum
, DC_VLC_BITS
, 10 /* 13 */,
2629 &ff_mpeg4_DCtab_lum
[0][1], 2, 1,
2630 &ff_mpeg4_DCtab_lum
[0][0], 2, 1, 512);
2631 INIT_VLC_STATIC(&dc_chrom
, DC_VLC_BITS
, 10 /* 13 */,
2632 &ff_mpeg4_DCtab_chrom
[0][1], 2, 1,
2633 &ff_mpeg4_DCtab_chrom
[0][0], 2, 1, 512);
2634 INIT_VLC_STATIC(&sprite_trajectory
, SPRITE_TRAJ_VLC_BITS
, 15,
2635 &ff_sprite_trajectory_tab
[0][1], 4, 2,
2636 &ff_sprite_trajectory_tab
[0][0], 4, 2, 128);
2637 INIT_VLC_STATIC(&mb_type_b_vlc
, MB_TYPE_B_VLC_BITS
, 4,
2638 &ff_mb_type_b_tab
[0][1], 2, 1,
2639 &ff_mb_type_b_tab
[0][0], 2, 1, 16);
2644 int ff_mpeg4_frame_end(AVCodecContext
*avctx
, const uint8_t *buf
, int buf_size
)
2646 Mpeg4DecContext
*ctx
= avctx
->priv_data
;
2647 MpegEncContext
*s
= &ctx
->m
;
2649 /* divx 5.01+ bitstream reorder stuff */
2650 /* Since this clobbers the input buffer and hwaccel codecs still need the
2651 * data during hwaccel->end_frame we should not do this any earlier */
2652 if (s
->divx_packed
) {
2653 int current_pos
= s
->gb
.buffer
== s
->bitstream_buffer
? 0 : (get_bits_count(&s
->gb
) >> 3);
2654 int startcode_found
= 0;
2656 if (buf_size
- current_pos
> 7) {
2659 for (i
= current_pos
; i
< buf_size
- 4; i
++)
2664 buf
[i
+ 3] == 0xB6) {
2665 startcode_found
= !(buf
[i
+ 4] & 0x40);
2670 if (startcode_found
) {
2671 av_fast_padded_malloc(&s
->bitstream_buffer
,
2672 &s
->allocated_bitstream_buffer_size
,
2673 buf_size
- current_pos
);
2674 if (!s
->bitstream_buffer
)
2675 return AVERROR(ENOMEM
);
2676 memcpy(s
->bitstream_buffer
, buf
+ current_pos
,
2677 buf_size
- current_pos
);
2678 s
->bitstream_buffer_size
= buf_size
- current_pos
;
2685 static int mpeg4_update_thread_context(AVCodecContext
*dst
,
2686 const AVCodecContext
*src
)
2688 Mpeg4DecContext
*s
= dst
->priv_data
;
2689 const Mpeg4DecContext
*s1
= src
->priv_data
;
2690 int init
= s
->m
.context_initialized
;
2692 int ret
= ff_mpeg_update_thread_context(dst
, src
);
2697 memcpy(((uint8_t*)s
) + sizeof(MpegEncContext
), ((uint8_t*)s1
) + sizeof(MpegEncContext
), sizeof(Mpeg4DecContext
) - sizeof(MpegEncContext
));
2699 if (CONFIG_MPEG4_DECODER
&& !init
&& s1
->xvid_build
>= 0)
2700 ff_xvid_idct_init(&s
->m
.idsp
, dst
);
2705 static av_cold
int decode_init(AVCodecContext
*avctx
)
2707 Mpeg4DecContext
*ctx
= avctx
->priv_data
;
2708 MpegEncContext
*s
= &ctx
->m
;
2714 ctx
->lavc_build
= -1;
2716 if ((ret
= ff_h263_decode_init(avctx
)) < 0)
2719 ff_mpeg4videodec_static_init();
2722 s
->low_delay
= 0; /* default, might be overridden in the vol header during header parsing */
2723 s
->decode_mb
= mpeg4_decode_mb
;
2724 ctx
->time_increment_bits
= 4; /* default value for broken headers */
2726 avctx
->chroma_sample_location
= AVCHROMA_LOC_LEFT
;
2727 avctx
->internal
->allocate_progress
= 1;
2732 static const AVProfile mpeg4_video_profiles
[] = {
2733 { FF_PROFILE_MPEG4_SIMPLE
, "Simple Profile" },
2734 { FF_PROFILE_MPEG4_SIMPLE_SCALABLE
, "Simple Scalable Profile" },
2735 { FF_PROFILE_MPEG4_CORE
, "Core Profile" },
2736 { FF_PROFILE_MPEG4_MAIN
, "Main Profile" },
2737 { FF_PROFILE_MPEG4_N_BIT
, "N-bit Profile" },
2738 { FF_PROFILE_MPEG4_SCALABLE_TEXTURE
, "Scalable Texture Profile" },
2739 { FF_PROFILE_MPEG4_SIMPLE_FACE_ANIMATION
, "Simple Face Animation Profile" },
2740 { FF_PROFILE_MPEG4_BASIC_ANIMATED_TEXTURE
, "Basic Animated Texture Profile" },
2741 { FF_PROFILE_MPEG4_HYBRID
, "Hybrid Profile" },
2742 { FF_PROFILE_MPEG4_ADVANCED_REAL_TIME
, "Advanced Real Time Simple Profile" },
2743 { FF_PROFILE_MPEG4_CORE_SCALABLE
, "Code Scalable Profile" },
2744 { FF_PROFILE_MPEG4_ADVANCED_CODING
, "Advanced Coding Profile" },
2745 { FF_PROFILE_MPEG4_ADVANCED_CORE
, "Advanced Core Profile" },
2746 { FF_PROFILE_MPEG4_ADVANCED_SCALABLE_TEXTURE
, "Advanced Scalable Texture Profile" },
2747 { FF_PROFILE_MPEG4_SIMPLE_STUDIO
, "Simple Studio Profile" },
2748 { FF_PROFILE_MPEG4_ADVANCED_SIMPLE
, "Advanced Simple Profile" },
2749 { FF_PROFILE_UNKNOWN
},
2752 static const AVOption mpeg4_options
[] = {
2753 {"quarter_sample", "1/4 subpel MC", offsetof(MpegEncContext
, quarter_sample
), FF_OPT_TYPE_INT
, {.i64
= 0}, 0, 1, 0},
2754 {"divx_packed", "divx style packed b frames", offsetof(MpegEncContext
, divx_packed
), FF_OPT_TYPE_INT
, {.i64
= 0}, 0, 1, 0},
2758 static const AVClass mpeg4_class
= {
2759 "MPEG4 Video Decoder",
2760 av_default_item_name
,
2762 LIBAVUTIL_VERSION_INT
,
2765 AVCodec ff_mpeg4_decoder
= {
2767 .long_name
= NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
2768 .type
= AVMEDIA_TYPE_VIDEO
,
2769 .id
= AV_CODEC_ID_MPEG4
,
2770 .priv_data_size
= sizeof(Mpeg4DecContext
),
2771 .init
= decode_init
,
2772 .close
= ff_h263_decode_end
,
2773 .decode
= ff_h263_decode_frame
,
2774 .capabilities
= CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
|
2775 CODEC_CAP_TRUNCATED
| CODEC_CAP_DELAY
|
2776 CODEC_CAP_FRAME_THREADS
,
2777 .flush
= ff_mpeg_flush
,
2779 .pix_fmts
= ff_h263_hwaccel_pixfmt_list_420
,
2780 .profiles
= NULL_IF_CONFIG_SMALL(mpeg4_video_profiles
),
2781 .update_thread_context
= ONLY_IF_THREADS_ENABLED(mpeg4_update_thread_context
),
2782 .priv_class
= &mpeg4_class
,
2786 #if CONFIG_MPEG4_VDPAU_DECODER
2787 static const AVClass mpeg4_vdpau_class
= {
2788 "MPEG4 Video VDPAU Decoder",
2789 av_default_item_name
,
2791 LIBAVUTIL_VERSION_INT
,
2794 AVCodec ff_mpeg4_vdpau_decoder
= {
2795 .name
= "mpeg4_vdpau",
2796 .long_name
= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 (VDPAU)"),
2797 .type
= AVMEDIA_TYPE_VIDEO
,
2798 .id
= AV_CODEC_ID_MPEG4
,
2799 .priv_data_size
= sizeof(Mpeg4DecContext
),
2800 .init
= decode_init
,
2801 .close
= ff_h263_decode_end
,
2802 .decode
= ff_h263_decode_frame
,
2803 .capabilities
= CODEC_CAP_DR1
| CODEC_CAP_TRUNCATED
| CODEC_CAP_DELAY
|
2804 CODEC_CAP_HWACCEL_VDPAU
,
2805 .pix_fmts
= (const enum AVPixelFormat
[]) { AV_PIX_FMT_VDPAU_MPEG4
,
2807 .priv_class
= &mpeg4_vdpau_class
,