FFmpeg
Loading...
Searching...
No Matches
mpeg4videodec.c
Go to the documentation of this file.
1/*
2 * MPEG-4 decoder
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23#define UNCHECKED_BITSTREAM_READER 1
24
25#include "config_components.h"
26
27#include "libavutil/avassert.h"
28#include "libavutil/internal.h"
29#include "libavutil/opt.h"
30#include "libavutil/thread.h"
31#include "codec_internal.h"
32#include "error_resilience.h"
33#include "hwconfig.h"
34#include "idctdsp.h"
35#include "mpegutils.h"
36#include "mpegvideo.h"
37#include "mpegvideodata.h"
38#include "mpegvideodec.h"
40#include "mpeg4video.h"
41#include "mpeg4videodata.h"
42#include "mpeg4videodec.h"
43#include "mpeg4videodefs.h"
44#include "h263.h"
45#include "h263data.h"
46#include "h263dec.h"
47#include "internal.h"
48#include "profiles.h"
49#include "qpeldsp.h"
50#include "threadprogress.h"
51#include "unary.h"
52
53#if 0 //3IV1 is quite rare and it slows things down a tiny bit
54#define IS_3IV1 (s->codec_tag == AV_RL32("3IV1"))
55#else
56#define IS_3IV1 0
57#endif
58
59/* The defines below define the number of bits that are read at once for
60 * reading vlc values. Changing these may improve speed and data cache needs
61 * be aware though that decreasing them may need the number of stages that is
62 * passed to get_vlc* to be increased. */
63#define SPRITE_TRAJ_VLC_BITS 6
64#define DC_VLC_BITS 9
65#define MB_TYPE_B_VLC_BITS 4
66#define STUDIO_INTRA_BITS 9
67
68static VLCElem dc_lum[512], dc_chrom[512];
71static const VLCElem *studio_intra_tab[12];
74
75static const uint8_t mpeg4_block_count[4] = { 0, 6, 8, 12 };
76
83
85{
86 av_assert2(h->c.codec_id == AV_CODEC_ID_MPEG4 && h->c.avctx->priv_data == h);
87 return (Mpeg4DecContext*)h;
88}
89
91 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
92 uint8_t *const *ref_picture)
93{
94 const uint8_t *ptr;
95 int src_x, src_y, motion_x, motion_y;
96 ptrdiff_t offset, linesize, uvlinesize;
97 int emu = 0;
98
99 motion_x = ctx->sprite_offset[0][0];
100 motion_y = ctx->sprite_offset[0][1];
101 src_x = s->mb_x * 16 + (motion_x >> (ctx->sprite_warping_accuracy + 1));
102 src_y = s->mb_y * 16 + (motion_y >> (ctx->sprite_warping_accuracy + 1));
103 motion_x *= 1 << (3 - ctx->sprite_warping_accuracy);
104 motion_y *= 1 << (3 - ctx->sprite_warping_accuracy);
105 src_x = av_clip(src_x, -16, s->width);
106 if (src_x == s->width)
107 motion_x = 0;
108 src_y = av_clip(src_y, -16, s->height);
109 if (src_y == s->height)
110 motion_y = 0;
111
112 linesize = s->linesize;
113 uvlinesize = s->uvlinesize;
114
115 ptr = ref_picture[0] + src_y * linesize + src_x;
116
117 if ((unsigned)src_x >= FFMAX(s->h_edge_pos - 17, 0) ||
118 (unsigned)src_y >= FFMAX(s->v_edge_pos - 17, 0)) {
119 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
120 linesize, linesize,
121 17, 17,
122 src_x, src_y,
123 s->h_edge_pos, s->v_edge_pos);
124 ptr = s->sc.edge_emu_buffer;
125 }
126
127 if ((motion_x | motion_y) & 7) {
128 ctx->mdsp.gmc1(dest_y, ptr, linesize, 16,
129 motion_x & 15, motion_y & 15, 128 - s->no_rounding);
130 ctx->mdsp.gmc1(dest_y + 8, ptr + 8, linesize, 16,
131 motion_x & 15, motion_y & 15, 128 - s->no_rounding);
132 } else {
133 int dxy;
134
135 dxy = ((motion_x >> 3) & 1) | ((motion_y >> 2) & 2);
136 if (s->no_rounding) {
137 s->hdsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
138 } else {
139 s->hdsp.put_pixels_tab[0][dxy](dest_y, ptr, linesize, 16);
140 }
141 }
142
143 if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY)
144 return;
145
146 motion_x = ctx->sprite_offset[1][0];
147 motion_y = ctx->sprite_offset[1][1];
148 src_x = s->mb_x * 8 + (motion_x >> (ctx->sprite_warping_accuracy + 1));
149 src_y = s->mb_y * 8 + (motion_y >> (ctx->sprite_warping_accuracy + 1));
150 motion_x *= 1 << (3 - ctx->sprite_warping_accuracy);
151 motion_y *= 1 << (3 - ctx->sprite_warping_accuracy);
152 src_x = av_clip(src_x, -8, s->width >> 1);
153 if (src_x == s->width >> 1)
154 motion_x = 0;
155 src_y = av_clip(src_y, -8, s->height >> 1);
156 if (src_y == s->height >> 1)
157 motion_y = 0;
158
159 offset = (src_y * uvlinesize) + src_x;
160 ptr = ref_picture[1] + offset;
161 if ((unsigned)src_x >= FFMAX((s->h_edge_pos >> 1) - 9, 0) ||
162 (unsigned)src_y >= FFMAX((s->v_edge_pos >> 1) - 9, 0)) {
163 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
164 uvlinesize, uvlinesize,
165 9, 9,
166 src_x, src_y,
167 s->h_edge_pos >> 1, s->v_edge_pos >> 1);
168 ptr = s->sc.edge_emu_buffer;
169 emu = 1;
170 }
171 ctx->mdsp.gmc1(dest_cb, ptr, uvlinesize, 8,
172 motion_x & 15, motion_y & 15, 128 - s->no_rounding);
173
174 ptr = ref_picture[2] + offset;
175 if (emu) {
176 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
177 uvlinesize, uvlinesize,
178 9, 9,
179 src_x, src_y,
180 s->h_edge_pos >> 1, s->v_edge_pos >> 1);
181 ptr = s->sc.edge_emu_buffer;
182 }
183 ctx->mdsp.gmc1(dest_cr, ptr, uvlinesize, 8,
184 motion_x & 15, motion_y & 15, 128 - s->no_rounding);
185}
186
188 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
189 uint8_t *const *ref_picture)
190{
191 const uint8_t *ptr;
192 int linesize, uvlinesize;
193 const int a = ctx->sprite_warping_accuracy;
194 int ox, oy;
195
196 linesize = s->linesize;
197 uvlinesize = s->uvlinesize;
198
199 ptr = ref_picture[0];
200
201 ox = ctx->sprite_offset[0][0] + ctx->sprite_delta[0][0] * s->mb_x * 16 +
202 ctx->sprite_delta[0][1] * s->mb_y * 16;
203 oy = ctx->sprite_offset[0][1] + ctx->sprite_delta[1][0] * s->mb_x * 16 +
204 ctx->sprite_delta[1][1] * s->mb_y * 16;
205
206 ctx->mdsp.gmc(dest_y, ptr, linesize, 16,
207 ox, oy,
208 ctx->sprite_delta[0][0], ctx->sprite_delta[0][1],
209 ctx->sprite_delta[1][0], ctx->sprite_delta[1][1],
210 a + 1, (1 << (2 * a + 1)) - s->no_rounding,
211 s->h_edge_pos, s->v_edge_pos);
212 ctx->mdsp.gmc(dest_y + 8, ptr, linesize, 16,
213 ox + ctx->sprite_delta[0][0] * 8,
214 oy + ctx->sprite_delta[1][0] * 8,
215 ctx->sprite_delta[0][0], ctx->sprite_delta[0][1],
216 ctx->sprite_delta[1][0], ctx->sprite_delta[1][1],
217 a + 1, (1 << (2 * a + 1)) - s->no_rounding,
218 s->h_edge_pos, s->v_edge_pos);
219
220 if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY)
221 return;
222
223 ox = ctx->sprite_offset[1][0] + ctx->sprite_delta[0][0] * s->mb_x * 8 +
224 ctx->sprite_delta[0][1] * s->mb_y * 8;
225 oy = ctx->sprite_offset[1][1] + ctx->sprite_delta[1][0] * s->mb_x * 8 +
226 ctx->sprite_delta[1][1] * s->mb_y * 8;
227
228 ptr = ref_picture[1];
229 ctx->mdsp.gmc(dest_cb, ptr, uvlinesize, 8,
230 ox, oy,
231 ctx->sprite_delta[0][0], ctx->sprite_delta[0][1],
232 ctx->sprite_delta[1][0], ctx->sprite_delta[1][1],
233 a + 1, (1 << (2 * a + 1)) - s->no_rounding,
234 (s->h_edge_pos + 1) >> 1, (s->v_edge_pos + 1) >> 1);
235
236 ptr = ref_picture[2];
237 ctx->mdsp.gmc(dest_cr, ptr, uvlinesize, 8,
238 ox, oy,
239 ctx->sprite_delta[0][0], ctx->sprite_delta[0][1],
240 ctx->sprite_delta[1][0], ctx->sprite_delta[1][1],
241 a + 1, (1 << (2 * a + 1)) - s->no_rounding,
242 (s->h_edge_pos + 1) >> 1, (s->v_edge_pos + 1) >> 1);
243}
244
246 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
247 uint8_t *const *ref_picture)
248{
249 const Mpeg4DecContext *const ctx = (Mpeg4DecContext*)s;
250
251 if (ctx->real_sprite_warping_points == 1) {
252 gmc1_motion(s, ctx, dest_y, dest_cb, dest_cr,
253 ref_picture);
254 } else {
255 gmc_motion(s, ctx, dest_y, dest_cb, dest_cr,
256 ref_picture);
257 }
258}
259
260void ff_mpeg4_decode_studio(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb,
261 uint8_t *dest_cr, int block_size, int uvlinesize,
262 int dct_linesize, int dct_offset)
263{
265 const int act_block_size = block_size * 2;
266
267 if (ctx->dpcm_direction == 0) {
268 s->idsp.idct_put(dest_y, dct_linesize, (int16_t*)ctx->block32[0]);
269 s->idsp.idct_put(dest_y + act_block_size, dct_linesize, (int16_t*)ctx->block32[1]);
270 s->idsp.idct_put(dest_y + dct_offset, dct_linesize, (int16_t*)ctx->block32[2]);
271 s->idsp.idct_put(dest_y + dct_offset + act_block_size, dct_linesize, (int16_t*)ctx->block32[3]);
272
273 dct_linesize = uvlinesize << s->interlaced_dct;
274 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
275
276 s->idsp.idct_put(dest_cb, dct_linesize, (int16_t*)ctx->block32[4]);
277 s->idsp.idct_put(dest_cr, dct_linesize, (int16_t*)ctx->block32[5]);
278 s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, (int16_t*)ctx->block32[6]);
279 s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, (int16_t*)ctx->block32[7]);
280 if (!s->chroma_x_shift){ //Chroma444
281 s->idsp.idct_put(dest_cb + act_block_size, dct_linesize, (int16_t*)ctx->block32[8]);
282 s->idsp.idct_put(dest_cr + act_block_size, dct_linesize, (int16_t*)ctx->block32[9]);
283 s->idsp.idct_put(dest_cb + act_block_size + dct_offset, dct_linesize, (int16_t*)ctx->block32[10]);
284 s->idsp.idct_put(dest_cr + act_block_size + dct_offset, dct_linesize, (int16_t*)ctx->block32[11]);
285 }
286 } else if (ctx->dpcm_direction == 1) {
287 uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr};
288 int linesize[3] = {dct_linesize, uvlinesize, uvlinesize};
289 for (int i = 0; i < 3; i++) {
290 const uint16_t *src = ctx->dpcm_macroblock[i];
291 int vsub = i ? s->chroma_y_shift : 0;
292 int hsub = i ? s->chroma_x_shift : 0;
293 int lowres = s->avctx->lowres;
294 int step = 1 << lowres;
295 for (int h = 0; h < (16 >> (vsub + lowres)); h++){
296 for (int w = 0, idx = 0; w < (16 >> (hsub + lowres)); w++, idx += step)
297 dest_pcm[i][w] = src[idx];
298 dest_pcm[i] += linesize[i] / 2;
299 src += (16 >> hsub) * step;
300 }
301 }
302 } else {
303 uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr};
304 int linesize[3] = {dct_linesize, uvlinesize, uvlinesize};
305 av_assert2(ctx->dpcm_direction == -1);
306 for (int i = 0; i < 3; i++) {
307 const uint16_t *src = ctx->dpcm_macroblock[i];
308 int vsub = i ? s->chroma_y_shift : 0;
309 int hsub = i ? s->chroma_x_shift : 0;
310 int lowres = s->avctx->lowres;
311 int step = 1 << lowres;
312 dest_pcm[i] += (linesize[i] / 2) * ((16 >> vsub + lowres) - 1);
313 for (int h = (16 >> (vsub + lowres)) - 1; h >= 0; h--){
314 for (int w = (16 >> (hsub + lowres)) - 1, idx = 0; w >= 0; w--, idx += step)
315 dest_pcm[i][w] = src[idx];
316 src += step * (16 >> hsub);
317 dest_pcm[i] -= linesize[i] / 2;
318 }
319 }
320 }
321}
322
323/**
324 * Predict the ac.
325 * @param n block index (0-3 are luma, 4-5 are chroma)
326 * @param dir the ac prediction direction
327 */
328void ff_mpeg4_pred_ac(H263DecContext *const h, int16_t *block, int n, int dir)
329{
330 int i;
331 int16_t *ac_val, *ac_val1;
332 int8_t *const qscale_table = h->c.cur_pic.qscale_table;
333
334 /* find prediction */
335 ac_val = &h->c.ac_val[0][0] + h->c.block_index[n] * 16;
336 ac_val1 = ac_val;
337 if (h->c.ac_pred) {
338 if (dir == 0) {
339 const int xy = h->c.mb_x - 1 + h->c.mb_y * h->c.mb_stride;
340 /* left prediction */
341 ac_val -= 16;
342
343 if (h->c.mb_x == 0 || h->c.qscale == qscale_table[xy] ||
344 n == 1 || n == 3) {
345 /* same qscale */
346 for (i = 1; i < 8; i++)
347 block[h->c.idsp.idct_permutation[i << 3]] += ac_val[i];
348 } else {
349 /* different qscale, we must rescale */
350 for (i = 1; i < 8; i++)
351 block[h->c.idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], h->c.qscale);
352 }
353 } else {
354 const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride - h->c.mb_stride;
355 /* top prediction */
356 ac_val -= 16 * h->c.block_wrap[n];
357
358 if (h->c.mb_y == 0 || h->c.qscale == qscale_table[xy] ||
359 n == 2 || n == 3) {
360 /* same qscale */
361 for (i = 1; i < 8; i++)
362 block[h->c.idsp.idct_permutation[i]] += ac_val[i + 8];
363 } else {
364 /* different qscale, we must rescale */
365 for (i = 1; i < 8; i++)
366 block[h->c.idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], h->c.qscale);
367 }
368 }
369 }
370 /* left copy */
371 for (i = 1; i < 8; i++)
372 ac_val1[i] = block[h->c.idsp.idct_permutation[i << 3]];
373
374 /* top copy */
375 for (i = 1; i < 8; i++)
376 ac_val1[8 + i] = block[h->c.idsp.idct_permutation[i]];
377}
378
379/**
380 * check if the next stuff is a resync marker or the end.
381 * @return 0 if not
382 */
384{
385 H263DecContext *const h = &ctx->h;
386 int bits_count = get_bits_count(&h->gb);
387 int v = show_bits(&h->gb, 16);
388
389 if (h->c.workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker)
390 return 0;
391
392 while (v <= 0xFF) {
393 if (h->c.pict_type == AV_PICTURE_TYPE_B ||
394 (v >> (8 - h->c.pict_type) != 1) || h->partitioned_frame)
395 break;
396 skip_bits(&h->gb, 8 + h->c.pict_type);
397 bits_count += 8 + h->c.pict_type;
398 v = show_bits(&h->gb, 16);
399 }
400
401 if (bits_count + 8 >= h->gb.size_in_bits) {
402 v >>= 8;
403 v |= 0x7F >> (7 - (bits_count & 7));
404
405 if (v == 0x7F)
406 return h->c.mb_num;
407 } else {
408 static const uint16_t mpeg4_resync_prefix[8] = {
409 0x7F00, 0x7E00, 0x7C00, 0x7800, 0x7000, 0x6000, 0x4000, 0x0000
410 };
411
412 if (v == mpeg4_resync_prefix[bits_count & 7]) {
413 int len, mb_num;
414 int mb_num_bits = av_log2(h->c.mb_num - 1) + 1;
415 GetBitContext gb = h->gb;
416
417 skip_bits(&h->gb, 1);
418 align_get_bits(&h->gb);
419
420 for (len = 0; len < 32; len++)
421 if (get_bits1(&h->gb))
422 break;
423
424 mb_num = get_bits(&h->gb, mb_num_bits);
425 if (!mb_num || mb_num > h->c.mb_num || get_bits_count(&h->gb) + 6 > h->gb.size_in_bits)
426 mb_num= -1;
427
428 h->gb = gb;
429
430 if (len >= ff_mpeg4_get_video_packet_prefix_length(h->c.pict_type, ctx->f_code, ctx->b_code))
431 return mb_num;
432 }
433 }
434 return 0;
435}
436
438{
439 MpegEncContext *s = &ctx->h.c;
440 int a = 2 << ctx->sprite_warping_accuracy;
441 int rho = 3 - ctx->sprite_warping_accuracy;
442 int r = 16 / a;
443 int alpha = 1;
444 int beta = 0;
445 int w = s->width;
446 int h = s->height;
447 int min_ab, i, w2, h2, w3, h3;
448 int sprite_ref[4][2];
449 int virtual_ref[2][2];
450 int64_t sprite_offset[2][2];
451 int64_t sprite_delta[2][2];
452
453 // only true for rectangle shapes
454 const int vop_ref[4][2] = { { 0, 0 }, { s->width, 0 },
455 { 0, s->height }, { s->width, s->height } };
456 int d[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
457
458 if (w <= 0 || h <= 0)
459 return AVERROR_INVALIDDATA;
460
461 for (i = 0; i < ctx->num_sprite_warping_points; i++) {
462 int length;
463 int x = 0, y = 0;
464
466 if (length > 0)
467 x = get_xbits(gb, length);
468
469 if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
470 check_marker(s->avctx, gb, "before sprite_trajectory");
471
473 if (length > 0)
474 y = get_xbits(gb, length);
475
476 check_marker(s->avctx, gb, "after sprite_trajectory");
477 ctx->sprite_traj[i][0] = d[i][0] = x;
478 ctx->sprite_traj[i][1] = d[i][1] = y;
479 }
480 for (; i < 4; i++)
481 ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0;
482
483 while ((1 << alpha) < w)
484 alpha++;
485 while ((1 << beta) < h)
486 beta++; /* typo in the MPEG-4 std for the definition of w' and h' */
487 w2 = 1 << alpha;
488 h2 = 1 << beta;
489
490 // Note, the 4th point isn't used for GMC
491 if (ctx->divx_version == 500 && ctx->divx_build == 413) {
492 sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
493 sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
494 sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
495 sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1];
496 sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0];
497 sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1];
498 } else {
499 sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]);
500 sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]);
501 sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]);
502 sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]);
503 sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]);
504 sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]);
505 }
506 /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
507 * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
508
509 /* This is mostly identical to the MPEG-4 std (and is totally unreadable
510 * because of that...). Perhaps it should be reordered to be more readable.
511 * The idea behind this virtual_ref mess is to be able to use shifts later
512 * per pixel instead of divides so the distance between points is converted
513 * from w&h based to w2&h2 based which are of the 2^x form. */
514 virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) +
515 ROUNDED_DIV(((w - w2) *
516 (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
517 w2 * (r * sprite_ref[1][0] - 16LL * vop_ref[1][0])), w);
518 virtual_ref[0][1] = 16 * vop_ref[0][1] +
519 ROUNDED_DIV(((w - w2) *
520 (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
521 w2 * (r * sprite_ref[1][1] - 16LL * vop_ref[1][1])), w);
522 virtual_ref[1][0] = 16 * vop_ref[0][0] +
523 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
524 h2 * (r * sprite_ref[2][0] - 16LL * vop_ref[2][0])), h);
525 virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) +
526 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
527 h2 * (r * sprite_ref[2][1] - 16LL * vop_ref[2][1])), h);
528
529 switch (ctx->num_sprite_warping_points) {
530 case 0:
531 sprite_offset[0][0] =
532 sprite_offset[0][1] =
533 sprite_offset[1][0] =
534 sprite_offset[1][1] = 0;
535 sprite_delta[0][0] = a;
536 sprite_delta[0][1] =
537 sprite_delta[1][0] = 0;
538 sprite_delta[1][1] = a;
539 ctx->sprite_shift[0] =
540 ctx->sprite_shift[1] = 0;
541 break;
542 case 1: // GMC only
543 sprite_offset[0][0] = sprite_ref[0][0] - a * vop_ref[0][0];
544 sprite_offset[0][1] = sprite_ref[0][1] - a * vop_ref[0][1];
545 sprite_offset[1][0] = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) -
546 a * (vop_ref[0][0] / 2);
547 sprite_offset[1][1] = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) -
548 a * (vop_ref[0][1] / 2);
549 sprite_delta[0][0] = a;
550 sprite_delta[0][1] =
551 sprite_delta[1][0] = 0;
552 sprite_delta[1][1] = a;
553 ctx->sprite_shift[0] =
554 ctx->sprite_shift[1] = 0;
555 break;
556 case 2:
557 sprite_offset[0][0] = ((int64_t) sprite_ref[0][0] * (1 << alpha + rho)) +
558 ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
559 ((int64_t) -vop_ref[0][0]) +
560 ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
561 ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
562 sprite_offset[0][1] = ((int64_t) sprite_ref[0][1] * (1 << alpha + rho)) +
563 ((int64_t) -r * sprite_ref[0][1] + virtual_ref[0][1]) *
564 ((int64_t) -vop_ref[0][0]) +
565 ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
566 ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
567 sprite_offset[1][0] = (((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
568 ((int64_t)-2 * vop_ref[0][0] + 1) +
569 ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
570 ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
571 (int64_t) sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1)));
572 sprite_offset[1][1] = (((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) *
573 ((int64_t)-2 * vop_ref[0][0] + 1) +
574 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
575 ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
576 (int64_t) sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1)));
577 sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
578 sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]);
579 sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
580 sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
581
582 ctx->sprite_shift[0] = alpha + rho;
583 ctx->sprite_shift[1] = alpha + rho + 2;
584 break;
585 case 3:
586 min_ab = FFMIN(alpha, beta);
587 w3 = w2 >> min_ab;
588 h3 = h2 >> min_ab;
589 sprite_offset[0][0] = ((int64_t)sprite_ref[0][0] * (1 << (alpha + beta + rho - min_ab))) +
590 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-vop_ref[0][0]) +
591 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-vop_ref[0][1]) +
592 ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
593 sprite_offset[0][1] = ((int64_t)sprite_ref[0][1] * (1 << (alpha + beta + rho - min_ab))) +
594 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-vop_ref[0][0]) +
595 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-vop_ref[0][1]) +
596 ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
597 sprite_offset[1][0] = ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-2 * vop_ref[0][0] + 1) +
598 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-2 * vop_ref[0][1] + 1) +
599 (int64_t)2 * w2 * h3 * r * sprite_ref[0][0] - 16 * w2 * h3 +
600 ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
601 sprite_offset[1][1] = ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-2 * vop_ref[0][0] + 1) +
602 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-2 * vop_ref[0][1] + 1) +
603 (int64_t)2 * w2 * h3 * r * sprite_ref[0][1] - 16 * w2 * h3 +
604 ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
605 sprite_delta[0][0] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[0][0]) * h3;
606 sprite_delta[0][1] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[1][0]) * w3;
607 sprite_delta[1][0] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[0][1]) * h3;
608 sprite_delta[1][1] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[1][1]) * w3;
609
610 ctx->sprite_shift[0] = alpha + beta + rho - min_ab;
611 ctx->sprite_shift[1] = alpha + beta + rho - min_ab + 2;
612 break;
613 default:
614 av_unreachable("num_sprite_warping_points outside of 0..3 results in an error"
615 "in which num_sprite_warping_points is reset to zero");
616 }
617 /* try to simplify the situation */
618 if (sprite_delta[0][0] == a << ctx->sprite_shift[0] &&
619 sprite_delta[0][1] == 0 &&
620 sprite_delta[1][0] == 0 &&
621 sprite_delta[1][1] == a << ctx->sprite_shift[0]) {
622 sprite_offset[0][0] >>= ctx->sprite_shift[0];
623 sprite_offset[0][1] >>= ctx->sprite_shift[0];
624 sprite_offset[1][0] >>= ctx->sprite_shift[1];
625 sprite_offset[1][1] >>= ctx->sprite_shift[1];
626 sprite_delta[0][0] = a;
627 sprite_delta[0][1] = 0;
628 sprite_delta[1][0] = 0;
629 sprite_delta[1][1] = a;
630 ctx->sprite_shift[0] = 0;
631 ctx->sprite_shift[1] = 0;
632 ctx->real_sprite_warping_points = 1;
633 } else {
634 int shift_y = 16 - ctx->sprite_shift[0];
635 int shift_c = 16 - ctx->sprite_shift[1];
636
637 for (i = 0; i < 2; i++) {
638 if (shift_c < 0 || shift_y < 0 ||
639 FFABS( sprite_offset[0][i]) >= INT_MAX >> shift_y ||
640 FFABS( sprite_offset[1][i]) >= INT_MAX >> shift_c ||
641 FFABS( sprite_delta[0][i]) >= INT_MAX >> shift_y ||
642 FFABS( sprite_delta[1][i]) >= INT_MAX >> shift_y
643 ) {
644 avpriv_request_sample(s->avctx, "Too large sprite shift, delta or offset");
645 goto overflow;
646 }
647 }
648
649 for (i = 0; i < 2; i++) {
650 sprite_offset[0][i] *= 1 << shift_y;
651 sprite_offset[1][i] *= 1 << shift_c;
652 sprite_delta[0][i] *= 1 << shift_y;
653 sprite_delta[1][i] *= 1 << shift_y;
654 ctx->sprite_shift[i] = 16;
655
656 }
657 for (i = 0; i < 2; i++) {
658 int64_t sd[2] = {
659 sprite_delta[i][0] - a * (1LL<<16),
660 sprite_delta[i][1] - a * (1LL<<16)
661 };
662
663 if (llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
664 llabs(sprite_offset[0][i] + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
665 llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL) + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
666 llabs(sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
667 llabs(sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
668 llabs(sd[0]) >= INT_MAX ||
669 llabs(sd[1]) >= INT_MAX ||
670 llabs(sprite_offset[0][i] + sd[0] * (w+16LL)) >= INT_MAX ||
671 llabs(sprite_offset[0][i] + sd[1] * (h+16LL)) >= INT_MAX ||
672 llabs(sprite_offset[0][i] + sd[0] * (w+16LL) + sd[1] * (h+16LL)) >= INT_MAX
673 ) {
674 avpriv_request_sample(s->avctx, "Overflow on sprite points");
675 goto overflow;
676 }
677 }
678 ctx->real_sprite_warping_points = ctx->num_sprite_warping_points;
679 }
680
681 for (i = 0; i < 4; i++) {
682 ctx->sprite_offset[i&1][i>>1] = sprite_offset[i&1][i>>1];
683 ctx->sprite_delta [i&1][i>>1] = sprite_delta [i&1][i>>1];
684 }
685
686 return 0;
687overflow:
688 memset(ctx->sprite_offset, 0, sizeof(ctx->sprite_offset));
689 memset(ctx->sprite_delta, 0, sizeof(ctx->sprite_delta));
691}
692
694 int len = FFMIN(ctx->time_increment_bits + 3, 15);
695
696 get_bits(gb, len);
697 if (get_bits1(gb))
698 get_bits(gb, len);
699 check_marker(ctx->h.c.avctx, gb, "after new_pred");
700
701 return 0;
702}
703
704/**
705 * Decode the next video packet.
706 * @return <0 if something went wrong
707 */
709{
711
712 int mb_num_bits = av_log2(h->c.mb_num - 1) + 1;
713 int header_extension = 0, mb_num, len;
714
715 /* is there enough space left for a video packet + header */
716 if (get_bits_count(&h->gb) > h->gb.size_in_bits - 20)
717 return AVERROR_INVALIDDATA;
718
719 for (len = 0; len < 32; len++)
720 if (get_bits1(&h->gb))
721 break;
722
723 if (len != ff_mpeg4_get_video_packet_prefix_length(h->c.pict_type, ctx->f_code, ctx->b_code)) {
724 av_log(h->c.avctx, AV_LOG_ERROR, "marker does not match f_code\n");
725 return AVERROR_INVALIDDATA;
726 }
727
728 if (ctx->shape != RECT_SHAPE) {
729 header_extension = get_bits1(&h->gb);
730 // FIXME more stuff here
731 }
732
733 mb_num = get_bits(&h->gb, mb_num_bits);
734 if (mb_num >= h->c.mb_num || !mb_num) {
735 av_log(h->c.avctx, AV_LOG_ERROR,
736 "illegal mb_num in video packet (%d %d) \n", mb_num, h->c.mb_num);
737 return AVERROR_INVALIDDATA;
738 }
739
740 h->c.mb_x = mb_num % h->c.mb_width;
741 h->c.mb_y = mb_num / h->c.mb_width;
742
743 if (ctx->shape != BIN_ONLY_SHAPE) {
744 int qscale = get_bits(&h->gb, ctx->quant_precision);
745 if (qscale)
746 h->c.chroma_qscale = h->c.qscale = qscale;
747 }
748
749 if (ctx->shape == RECT_SHAPE)
750 header_extension = get_bits1(&h->gb);
751
752 if (header_extension) {
753 while (get_bits1(&h->gb) != 0)
754 ;
755
756 check_marker(h->c.avctx, &h->gb, "before time_increment in video packed header");
757 skip_bits(&h->gb, ctx->time_increment_bits); /* time_increment */
758 check_marker(h->c.avctx, &h->gb, "before vop_coding_type in video packed header");
759
760 skip_bits(&h->gb, 2); /* vop coding type */
761 // FIXME not rect stuff here
762
763 if (ctx->shape != BIN_ONLY_SHAPE) {
764 skip_bits(&h->gb, 3); /* intra dc vlc threshold */
765 // FIXME don't just ignore everything
766 if (h->c.pict_type == AV_PICTURE_TYPE_S &&
767 ctx->vol_sprite_usage == GMC_SPRITE) {
768 if (mpeg4_decode_sprite_trajectory(ctx, &h->gb) < 0)
769 return AVERROR_INVALIDDATA;
770 av_log(h->c.avctx, AV_LOG_ERROR, "untested\n");
771 }
772
773 // FIXME reduced res stuff here
774
775 if (h->c.pict_type != AV_PICTURE_TYPE_I) {
776 int f_code = get_bits(&h->gb, 3); /* fcode_for */
777 if (f_code == 0)
778 av_log(h->c.avctx, AV_LOG_ERROR,
779 "Error, video packet header damaged (f_code=0)\n");
780 }
781 if (h->c.pict_type == AV_PICTURE_TYPE_B) {
782 int b_code = get_bits(&h->gb, 3);
783 if (b_code == 0)
784 av_log(h->c.avctx, AV_LOG_ERROR,
785 "Error, video packet header damaged (b_code=0)\n");
786 }
787 }
788 }
789 if (ctx->new_pred)
790 decode_new_pred(ctx, &h->gb);
791
792 return 0;
793}
794
796{
797 H263DecContext *const h = &ctx->h;
798 /* Reset DC Predictors */
799 h->last_dc[0] =
800 h->last_dc[1] =
801 h->last_dc[2] = 1 << (h->c.avctx->bits_per_raw_sample + ctx->dct_precision + h->c.intra_dc_precision - 1);
802}
803
804/**
805 * Decode the next video packet.
806 * @return <0 if something went wrong
807 */
809{
811 GetBitContext *gb = &h->gb;
812 unsigned vlc_len;
813 uint16_t mb_num;
814
815 if (get_bits_left(gb) >= 32 && get_bits_long(gb, 32) == SLICE_STARTCODE) {
816 vlc_len = av_log2(h->c.mb_width * h->c.mb_height) + 1;
817 mb_num = get_bits(gb, vlc_len);
818
819 if (mb_num >= h->c.mb_num)
820 return AVERROR_INVALIDDATA;
821
822 h->c.mb_x = mb_num % h->c.mb_width;
823 h->c.mb_y = mb_num / h->c.mb_width;
824
825 if (ctx->shape != BIN_ONLY_SHAPE)
826 h->c.qscale = mpeg_get_qscale(&h->gb, h->c.q_scale_type);
827
828 if (get_bits1(gb)) { /* slice_extension_flag */
829 skip_bits1(gb); /* intra_slice */
830 skip_bits1(gb); /* slice_VOP_id_enable */
831 skip_bits(gb, 6); /* slice_VOP_id */
832 while (get_bits1(gb)) /* extra_bit_slice */
833 skip_bits(gb, 8); /* extra_information_slice */
834 }
835
837 }
838 else {
839 return AVERROR_INVALIDDATA;
840 }
841
842 return 0;
843}
844
845/**
846 * Get the average motion vector for a GMC MB.
847 * @param n either 0 for the x component or 1 for y
848 * @return the average MV for a GMC MB
849 */
850static inline int get_amv(Mpeg4DecContext *ctx, int n)
851{
852 MPVContext *const s = &ctx->h.c;
853 int x, y, mb_v, sum, dx, dy, shift;
854 int len = 1 << (ctx->f_code + 4);
855 const int a = ctx->sprite_warping_accuracy;
856
857 if (s->workaround_bugs & FF_BUG_AMV)
858 len >>= s->quarter_sample;
859
860 if (ctx->real_sprite_warping_points == 1) {
861 if (ctx->divx_version == 500 && ctx->divx_build == 413 && a >= s->quarter_sample)
862 sum = ctx->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
863 else
864 sum = RSHIFT(ctx->sprite_offset[0][n] * (1 << s->quarter_sample), a);
865 } else {
866 dx = ctx->sprite_delta[n][0];
867 dy = ctx->sprite_delta[n][1];
868 shift = ctx->sprite_shift[0];
869 if (n)
870 dy -= 1 << (shift + a + 1);
871 else
872 dx -= 1 << (shift + a + 1);
873 mb_v = ctx->sprite_offset[0][n] + dx * s->mb_x * 16U + dy * s->mb_y * 16U;
874
875 sum = 0;
876 for (y = 0; y < 16; y++) {
877 int v;
878
879 v = mb_v + (unsigned)dy * y;
880 // FIXME optimize
881 for (x = 0; x < 16; x++) {
882 sum += v >> shift;
883 v += dx;
884 }
885 }
886 sum = RSHIFT(sum, a + 8 - s->quarter_sample);
887 }
888
889 if (sum < -len)
890 sum = -len;
891 else if (sum >= len)
892 sum = len - 1;
893
894 return sum;
895}
896
897/**
898 * Predict the dc.
899 * @param n block index (0-3 are luma, 4-5 are chroma)
900 * @param dir_ptr pointer to an integer where the prediction direction will be stored
901 */
902static inline int mpeg4_pred_dc(MpegEncContext *s, int n, int *dir_ptr)
903{
904 const int16_t *const dc_val = s->dc_val + s->block_index[n];
905 const int wrap = s->block_wrap[n];
906 int pred;
907
908 /* find prediction */
909
910 /* B C
911 * A X
912 */
913 int a = dc_val[-1];
914 int b = dc_val[-1 - wrap];
915 int c = dc_val[-wrap];
916
917 /* outside slice handling (we can't do that by memset as we need the
918 * dc for error resilience) */
919 if (s->first_slice_line && n != 3) {
920 if (n != 2)
921 b = c = 1024;
922 if (n != 1 && s->mb_x == s->resync_mb_x)
923 b = a = 1024;
924 }
925 if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1) {
926 if (n == 0 || n == 4 || n == 5)
927 b = 1024;
928 }
929
930 if (abs(a - b) < abs(b - c)) {
931 pred = c;
932 *dir_ptr = 1; /* top */
933 } else {
934 pred = a;
935 *dir_ptr = 0; /* left */
936 }
937 return pred;
938}
939
940static inline int mpeg4_get_level_dc(MpegEncContext *s, int n, int pred, int level)
941{
942 int scale = n < 4 ? s->y_dc_scale : s->c_dc_scale;
943 int ret;
944
945 if (IS_3IV1)
946 scale = 8;
947
948 /* we assume pred is positive */
949 pred = FASTDIV((pred + (scale >> 1)), scale);
950
951 level += pred;
952 ret = level;
953 level *= scale;
954 if (level & (~2047)) {
955 if (s->avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) {
956 if (level < 0) {
957 av_log(s->avctx, AV_LOG_ERROR,
958 "dc<0 at %dx%d\n", s->mb_x, s->mb_y);
959 return AVERROR_INVALIDDATA;
960 }
961 if (level > 2048 + scale) {
962 av_log(s->avctx, AV_LOG_ERROR,
963 "dc overflow at %dx%d\n", s->mb_x, s->mb_y);
964 return AVERROR_INVALIDDATA;
965 }
966 }
967 if (level < 0)
968 level = 0;
969 else if (!(s->workaround_bugs & FF_BUG_DC_CLIP))
970 level = 2047;
971 }
972 s->dc_val[s->block_index[n]] = level;
973
974 return ret;
975}
976
977/**
978 * Decode the dc value.
979 * @param n block index (0-3 are luma, 4-5 are chroma)
980 * @param dir_ptr the prediction direction will be stored here
981 * @return the quantized dc
982 */
983static inline int mpeg4_decode_dc(H263DecContext *const h, int n, int *dir_ptr)
984{
985 int level, code, pred;
986
987 if (n < 4)
988 code = get_vlc2(&h->gb, dc_lum, DC_VLC_BITS, 1);
989 else
990 code = get_vlc2(&h->gb, dc_chrom, DC_VLC_BITS, 1);
991
992 if (code < 0) {
993 av_log(h->c.avctx, AV_LOG_ERROR, "illegal dc vlc\n");
994 return AVERROR_INVALIDDATA;
995 }
996
997 if (code == 0) {
998 level = 0;
999 } else {
1000 if (IS_3IV1) {
1001 if (code == 1)
1002 level = 2 * get_bits1(&h->gb) - 1;
1003 else {
1004 if (get_bits1(&h->gb))
1005 level = get_bits(&h->gb, code - 1) + (1 << (code - 1));
1006 else
1007 level = -get_bits(&h->gb, code - 1) - (1 << (code - 1));
1008 }
1009 } else {
1010 level = get_xbits(&h->gb, code);
1011 }
1012
1013 if (code > 8) {
1014 if (get_bits1(&h->gb) == 0) { /* marker */
1015 if (h->c.avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)) {
1016 av_log(h->c.avctx, AV_LOG_ERROR, "dc marker bit missing\n");
1017 return AVERROR_INVALIDDATA;
1018 }
1019 }
1020 }
1021 }
1022
1023 pred = mpeg4_pred_dc(&h->c, n, dir_ptr);
1024 return mpeg4_get_level_dc(&h->c, n, pred, level);
1025}
1026
1027/**
1028 * Decode first partition.
1029 * @return number of MBs decoded or <0 if an error occurred
1030 */
1032{
1033 H263DecContext *const h = &ctx->h;
1034 int mb_num = 0;
1035 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1036
1037 /* decode first partition */
1038 h->c.first_slice_line = 1;
1039 for (; h->c.mb_y < h->c.mb_height; h->c.mb_y++) {
1041 for (; h->c.mb_x < h->c.mb_width; h->c.mb_x++) {
1042 const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride;
1043 int cbpc;
1044 int dir = 0;
1045
1046 mb_num++;
1047 ff_update_block_index(&h->c, 8, h->c.avctx->lowres, 1);
1048 if (h->c.mb_x == h->c.resync_mb_x && h->c.mb_y == h->c.resync_mb_y + 1)
1049 h->c.first_slice_line = 0;
1050
1051 if (h->c.pict_type == AV_PICTURE_TYPE_I) {
1052 int i;
1053
1054 do {
1055 if (show_bits(&h->gb, 19) == DC_MARKER)
1056 return mb_num - 1;
1057
1059 if (cbpc < 0) {
1060 av_log(h->c.avctx, AV_LOG_ERROR,
1061 "mcbpc corrupted at %d %d\n", h->c.mb_x, h->c.mb_y);
1062 return AVERROR_INVALIDDATA;
1063 }
1064 } while (cbpc == 8);
1065
1066 h->c.cbp_table[xy] = cbpc & 3;
1067 h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA;
1068 h->c.mb_intra = 1;
1069
1070 if (cbpc & 4)
1071 ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]);
1072
1073 h->c.cur_pic.qscale_table[xy] = h->c.qscale;
1074
1075 h->c.mbintra_table[xy] = 1;
1076 for (i = 0; i < 6; i++) {
1077 int dc_pred_dir;
1078 int dc = mpeg4_decode_dc(h, i, &dc_pred_dir);
1079 if (dc < 0) {
1080 av_log(h->c.avctx, AV_LOG_ERROR,
1081 "DC corrupted at %d %d\n", h->c.mb_x, h->c.mb_y);
1082 return dc;
1083 }
1084 dir <<= 1;
1085 if (dc_pred_dir)
1086 dir |= 1;
1087 }
1088 h->c.pred_dir_table[xy] = dir;
1089 } else { /* P/S_TYPE */
1090 int mx, my, pred_x, pred_y, bits;
1091 int16_t *const mot_val = h->c.cur_pic.motion_val[0][h->c.block_index[0]];
1092 const int stride = h->c.b8_stride * 2;
1093
1094try_again:
1095 bits = show_bits(&h->gb, 17);
1096 if (bits == MOTION_MARKER)
1097 return mb_num - 1;
1098
1099 skip_bits1(&h->gb);
1100 if (bits & 0x10000) {
1101 /* skip mb */
1102 if (h->c.pict_type == AV_PICTURE_TYPE_S &&
1103 ctx->vol_sprite_usage == GMC_SPRITE) {
1104 h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP |
1106 MB_TYPE_GMC |
1108 mx = get_amv(ctx, 0);
1109 my = get_amv(ctx, 1);
1110 } else {
1111 h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP |
1114 mx = my = 0;
1115 }
1116 mot_val[0] =
1117 mot_val[2] =
1118 mot_val[0 + stride] =
1119 mot_val[2 + stride] = mx;
1120 mot_val[1] =
1121 mot_val[3] =
1122 mot_val[1 + stride] =
1123 mot_val[3 + stride] = my;
1124
1126 continue;
1127 }
1128
1130 if (cbpc < 0) {
1131 av_log(h->c.avctx, AV_LOG_ERROR,
1132 "mcbpc corrupted at %d %d\n", h->c.mb_x, h->c.mb_y);
1133 return AVERROR_INVALIDDATA;
1134 }
1135 if (cbpc == 20)
1136 goto try_again;
1137
1138 h->c.cbp_table[xy] = cbpc & (8 + 3); // 8 is dquant
1139
1140 h->c.mb_intra = ((cbpc & 4) != 0);
1141
1142 if (h->c.mb_intra) {
1143 h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA;
1144 h->c.mbintra_table[xy] = 1;
1145 mot_val[0] =
1146 mot_val[2] =
1147 mot_val[0 + stride] =
1148 mot_val[2 + stride] = 0;
1149 mot_val[1] =
1150 mot_val[3] =
1151 mot_val[1 + stride] =
1152 mot_val[3 + stride] = 0;
1153 } else {
1155
1156 if (h->c.pict_type == AV_PICTURE_TYPE_S &&
1157 ctx->vol_sprite_usage == GMC_SPRITE &&
1158 (cbpc & 16) == 0)
1159 h->c.mcsel = get_bits1(&h->gb);
1160 else
1161 h->c.mcsel = 0;
1162
1163 if ((cbpc & 16) == 0) {
1164 /* 16x16 motion prediction */
1165
1166 ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y);
1167 if (!h->c.mcsel) {
1168 mx = ff_h263_decode_motion(h, pred_x, ctx->f_code);
1169 if (mx >= 0xffff)
1170 return AVERROR_INVALIDDATA;
1171
1172 my = ff_h263_decode_motion(h, pred_y, ctx->f_code);
1173 if (my >= 0xffff)
1174 return AVERROR_INVALIDDATA;
1175 h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 |
1177 } else {
1178 mx = get_amv(ctx, 0);
1179 my = get_amv(ctx, 1);
1180 h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 |
1181 MB_TYPE_GMC |
1183 }
1184
1185 mot_val[0] =
1186 mot_val[2] =
1187 mot_val[0 + stride] =
1188 mot_val[2 + stride] = mx;
1189 mot_val[1] =
1190 mot_val[3] =
1191 mot_val[1 + stride] =
1192 mot_val[3 + stride] = my;
1193 } else {
1194 int i;
1195 h->c.cur_pic.mb_type[xy] = MB_TYPE_8x8 |
1197 for (i = 0; i < 4; i++) {
1198 int16_t *mot_val = ff_h263_pred_motion(&h->c, i, 0, &pred_x, &pred_y);
1199 mx = ff_h263_decode_motion(h, pred_x, ctx->f_code);
1200 if (mx >= 0xffff)
1201 return AVERROR_INVALIDDATA;
1202
1203 my = ff_h263_decode_motion(h, pred_y, ctx->f_code);
1204 if (my >= 0xffff)
1205 return AVERROR_INVALIDDATA;
1206 mot_val[0] = mx;
1207 mot_val[1] = my;
1208 }
1209 }
1210 }
1211 }
1212 }
1213 h->c.mb_x = 0;
1214 }
1215
1216 return mb_num;
1217}
1218
1219/**
1220 * decode second partition.
1221 * @return <0 if an error occurred
1222 */
1223static int mpeg4_decode_partition_b(H263DecContext *const h, int mb_count)
1224{
1225 int mb_num = 0;
1226 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1227
1228 h->c.mb_x = h->c.resync_mb_x;
1229 h->c.first_slice_line = 1;
1230 for (h->c.mb_y = h->c.resync_mb_y; mb_num < mb_count; h->c.mb_y++) {
1232 for (; mb_num < mb_count && h->c.mb_x < h->c.mb_width; h->c.mb_x++) {
1233 const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride;
1234
1235 mb_num++;
1236 ff_update_block_index(&h->c, 8, h->c.avctx->lowres, 1);
1237 if (h->c.mb_x == h->c.resync_mb_x && h->c.mb_y == h->c.resync_mb_y + 1)
1238 h->c.first_slice_line = 0;
1239
1240 if (h->c.pict_type == AV_PICTURE_TYPE_I) {
1241 int ac_pred = get_bits1(&h->gb);
1242 int cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
1243 if (cbpy < 0) {
1244 av_log(h->c.avctx, AV_LOG_ERROR,
1245 "cbpy corrupted at %d %d\n", h->c.mb_x, h->c.mb_y);
1246 return AVERROR_INVALIDDATA;
1247 }
1248
1249 h->c.cbp_table[xy] |= cbpy << 2;
1250 h->c.cur_pic.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
1251 } else { /* P || S_TYPE */
1252 if (IS_INTRA(h->c.cur_pic.mb_type[xy])) {
1253 int i;
1254 int dir = 0;
1255 int ac_pred = get_bits1(&h->gb);
1256 int cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
1257
1258 if (cbpy < 0) {
1259 av_log(h->c.avctx, AV_LOG_ERROR,
1260 "I cbpy corrupted at %d %d\n", h->c.mb_x, h->c.mb_y);
1261 return AVERROR_INVALIDDATA;
1262 }
1263
1264 if (h->c.cbp_table[xy] & 8)
1265 ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]);
1266 h->c.cur_pic.qscale_table[xy] = h->c.qscale;
1267
1268 for (i = 0; i < 6; i++) {
1269 int dc_pred_dir;
1270 int dc = mpeg4_decode_dc(h, i, &dc_pred_dir);
1271 if (dc < 0) {
1272 av_log(h->c.avctx, AV_LOG_ERROR,
1273 "DC corrupted at %d %d\n", h->c.mb_x, h->c.mb_y);
1274 return dc;
1275 }
1276 dir <<= 1;
1277 if (dc_pred_dir)
1278 dir |= 1;
1279 }
1280 h->c.cbp_table[xy] &= 3; // remove dquant
1281 h->c.cbp_table[xy] |= cbpy << 2;
1282 h->c.cur_pic.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
1283 h->c.pred_dir_table[xy] = dir;
1284 } else if (IS_SKIP(h->c.cur_pic.mb_type[xy])) {
1285 h->c.cur_pic.qscale_table[xy] = h->c.qscale;
1286 h->c.cbp_table[xy] = 0;
1287 } else {
1288 int cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
1289
1290 if (cbpy < 0) {
1291 av_log(h->c.avctx, AV_LOG_ERROR,
1292 "P cbpy corrupted at %d %d\n", h->c.mb_x, h->c.mb_y);
1293 return AVERROR_INVALIDDATA;
1294 }
1295
1296 if (h->c.cbp_table[xy] & 8)
1297 ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]);
1298 h->c.cur_pic.qscale_table[xy] = h->c.qscale;
1299
1300 h->c.cbp_table[xy] &= 3; // remove dquant
1301 h->c.cbp_table[xy] |= (cbpy ^ 0xf) << 2;
1302 }
1303 }
1304 }
1305 if (mb_num >= mb_count)
1306 return 0;
1307 h->c.mb_x = 0;
1308 }
1309 return 0;
1310}
1311
1312/**
1313 * Decode the first and second partition.
1314 * @return <0 if error (and sets error type in the error_status_table)
1315 */
1317{
1319 int mb_num;
1320 int ret;
1321 const int part_a_error = h->c.pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR;
1322 const int part_a_end = h->c.pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END | ER_MV_END) : ER_MV_END;
1323
1324 mb_num = mpeg4_decode_partition_a(ctx);
1325 if (mb_num <= 0) {
1326 ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
1327 h->c.mb_x, h->c.mb_y, part_a_error);
1328 return mb_num ? mb_num : AVERROR_INVALIDDATA;
1329 }
1330
1331 if (h->c.resync_mb_x + h->c.resync_mb_y * h->c.mb_width + mb_num > h->c.mb_num) {
1332 av_log(h->c.avctx, AV_LOG_ERROR, "slice below monitor ...\n");
1333 ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
1334 h->c.mb_x, h->c.mb_y, part_a_error);
1335 return AVERROR_INVALIDDATA;
1336 }
1337
1338 h->mb_num_left = mb_num;
1339
1340 if (h->c.pict_type == AV_PICTURE_TYPE_I) {
1341 while (show_bits(&h->gb, 9) == 1)
1342 skip_bits(&h->gb, 9);
1343 if (get_bits(&h->gb, 19) != DC_MARKER) {
1344 av_log(h->c.avctx, AV_LOG_ERROR,
1345 "marker missing after first I partition at %d %d\n",
1346 h->c.mb_x, h->c.mb_y);
1347 return AVERROR_INVALIDDATA;
1348 }
1349 } else {
1350 while (show_bits(&h->gb, 10) == 1)
1351 skip_bits(&h->gb, 10);
1352 if (get_bits(&h->gb, 17) != MOTION_MARKER) {
1353 av_log(h->c.avctx, AV_LOG_ERROR,
1354 "marker missing after first P partition at %d %d\n",
1355 h->c.mb_x, h->c.mb_y);
1356 return AVERROR_INVALIDDATA;
1357 }
1358 }
1359 ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
1360 h->c.mb_x - 1, h->c.mb_y, part_a_end);
1361
1362 ret = mpeg4_decode_partition_b(h, mb_num);
1363 if (ret < 0) {
1364 if (h->c.pict_type == AV_PICTURE_TYPE_P)
1365 ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
1366 h->c.mb_x, h->c.mb_y, ER_DC_ERROR);
1367 return ret;
1368 } else {
1369 if (h->c.pict_type == AV_PICTURE_TYPE_P)
1370 ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
1371 h->c.mb_x - 1, h->c.mb_y, ER_DC_END);
1372 }
1373
1374 return 0;
1375}
1376
1377/**
1378 * Decode a block.
1379 * @return <0 if an error occurred
1380 */
1381static inline int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block,
1382 int n, int coded, int intra,
1383 int use_intra_dc_vlc, int rvlc)
1384{
1385 H263DecContext *const h = &ctx->h;
1386 int level, i, last, run, qmul, qadd, pred;
1387 int av_uninit(dc_pred_dir);
1388 const RLTable *rl;
1389 const RL_VLC_ELEM *rl_vlc;
1390 const uint8_t *scan_table;
1391
1392 // Note intra & rvlc should be optimized away if this is inlined
1393
1394 if (intra) {
1395 // FIXME add short header support
1396 if (use_intra_dc_vlc) {
1397 /* DC coef */
1398 if (h->partitioned_frame) {
1399 level = h->c.dc_val[h->c.block_index[n]];
1400 if (n < 4)
1401 level = FASTDIV((level + (h->c.y_dc_scale >> 1)), h->c.y_dc_scale);
1402 else
1403 level = FASTDIV((level + (h->c.c_dc_scale >> 1)), h->c.c_dc_scale);
1404 dc_pred_dir = (h->c.pred_dir_table[h->c.mb_x + h->c.mb_y * h->c.mb_stride] << n) & 32;
1405 } else {
1406 level = mpeg4_decode_dc(h, n, &dc_pred_dir);
1407 if (level < 0)
1408 return level;
1409 }
1410 block[0] = level;
1411 i = 0;
1412 } else {
1413 i = -1;
1414 pred = mpeg4_pred_dc(&h->c, n, &dc_pred_dir);
1415 }
1416 if (!coded)
1417 goto not_coded;
1418
1419 if (rvlc) {
1420 rl = &ff_rvlc_rl_intra;
1421 rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
1422 } else {
1423 rl = &ff_mpeg4_rl_intra;
1424 rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
1425 }
1426 if (h->c.ac_pred) {
1427 if (dc_pred_dir == 0)
1428 scan_table = h->permutated_intra_v_scantable; /* left */
1429 else
1430 scan_table = h->permutated_intra_h_scantable; /* top */
1431 } else {
1432 scan_table = h->c.intra_scantable.permutated;
1433 }
1434 qmul = 1;
1435 qadd = 0;
1436 } else {
1437 i = -1;
1438 if (!coded) {
1439 h->c.block_last_index[n] = i;
1440 return 0;
1441 }
1442 if (rvlc)
1443 rl = &ff_rvlc_rl_inter;
1444 else
1445 rl = &ff_h263_rl_inter;
1446
1447 scan_table = h->c.intra_scantable.permutated;
1448
1449 if (ctx->mpeg_quant) {
1450 qmul = 1;
1451 qadd = 0;
1452 if (rvlc)
1453 rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
1454 else
1455 rl_vlc = ff_h263_rl_inter.rl_vlc[0];
1456 } else {
1457 qmul = h->c.qscale << 1;
1458 qadd = (h->c.qscale - 1) | 1;
1459 if (rvlc)
1460 rl_vlc = ff_rvlc_rl_inter.rl_vlc[h->c.qscale];
1461 else
1462 rl_vlc = ff_h263_rl_inter.rl_vlc[h->c.qscale];
1463 }
1464 }
1465 {
1466 OPEN_READER(re, &h->gb);
1467 for (;;) {
1468 UPDATE_CACHE(re, &h->gb);
1469 GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1470 if (level == 0) {
1471 /* escape */
1472 if (rvlc) {
1473 if (SHOW_UBITS(re, &h->gb, 1) == 0) {
1474 av_log(h->c.avctx, AV_LOG_ERROR,
1475 "1. marker bit missing in rvlc esc\n");
1476 return AVERROR_INVALIDDATA;
1477 }
1478 SKIP_CACHE(re, &h->gb, 1);
1479
1480 last = SHOW_UBITS(re, &h->gb, 1);
1481 SKIP_CACHE(re, &h->gb, 1);
1482 run = SHOW_UBITS(re, &h->gb, 6);
1483 SKIP_COUNTER(re, &h->gb, 1 + 1 + 6);
1484 UPDATE_CACHE(re, &h->gb);
1485
1486 if (SHOW_UBITS(re, &h->gb, 1) == 0) {
1487 av_log(h->c.avctx, AV_LOG_ERROR,
1488 "2. marker bit missing in rvlc esc\n");
1489 return AVERROR_INVALIDDATA;
1490 }
1491 SKIP_CACHE(re, &h->gb, 1);
1492
1493 level = SHOW_UBITS(re, &h->gb, 11);
1494 SKIP_CACHE(re, &h->gb, 11);
1495
1496 if (SHOW_UBITS(re, &h->gb, 5) != 0x10) {
1497 av_log(h->c.avctx, AV_LOG_ERROR, "reverse esc missing\n");
1498 return AVERROR_INVALIDDATA;
1499 }
1500 SKIP_CACHE(re, &h->gb, 5);
1501
1502 level = level * qmul + qadd;
1503 level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1);
1504 SKIP_COUNTER(re, &h->gb, 1 + 11 + 5 + 1);
1505
1506 i += run + 1;
1507 if (last)
1508 i += 192;
1509 } else {
1510 int cache;
1511 cache = GET_CACHE(re, &h->gb);
1512
1513 if (IS_3IV1)
1514 cache ^= 0xC0000000;
1515
1516 if (cache & 0x80000000) {
1517 if (cache & 0x40000000) {
1518 /* third escape */
1519 SKIP_CACHE(re, &h->gb, 2);
1520 last = SHOW_UBITS(re, &h->gb, 1);
1521 SKIP_CACHE(re, &h->gb, 1);
1522 run = SHOW_UBITS(re, &h->gb, 6);
1523 SKIP_COUNTER(re, &h->gb, 2 + 1 + 6);
1524 UPDATE_CACHE(re, &h->gb);
1525
1526 if (IS_3IV1) {
1527 level = SHOW_SBITS(re, &h->gb, 12);
1528 LAST_SKIP_BITS(re, &h->gb, 12);
1529 } else {
1530 if (SHOW_UBITS(re, &h->gb, 1) == 0) {
1531 av_log(h->c.avctx, AV_LOG_ERROR,
1532 "1. marker bit missing in 3. esc\n");
1533 if (!(h->c.avctx->err_recognition & AV_EF_IGNORE_ERR) || get_bits_left(&h->gb) <= 0)
1534 return AVERROR_INVALIDDATA;
1535 }
1536 SKIP_CACHE(re, &h->gb, 1);
1537
1538 level = SHOW_SBITS(re, &h->gb, 12);
1539 SKIP_CACHE(re, &h->gb, 12);
1540
1541 if (SHOW_UBITS(re, &h->gb, 1) == 0) {
1542 av_log(h->c.avctx, AV_LOG_ERROR,
1543 "2. marker bit missing in 3. esc\n");
1544 if (!(h->c.avctx->err_recognition & AV_EF_IGNORE_ERR) || get_bits_left(&h->gb) <= 0)
1545 return AVERROR_INVALIDDATA;
1546 }
1547
1548 SKIP_COUNTER(re, &h->gb, 1 + 12 + 1);
1549 }
1550
1551#if 0
1552 if (h->c.error_recognition >= FF_ER_COMPLIANT) {
1553 const int abs_level= FFABS(level);
1554 if (abs_level<=MAX_LEVEL && run<=MAX_RUN) {
1555 const int run1= run - rl->max_run[last][abs_level] - 1;
1556 if (abs_level <= rl->max_level[last][run]) {
1557 av_log(h->c.avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1558 return AVERROR_INVALIDDATA;
1559 }
1560 if (h->c.error_recognition > FF_ER_COMPLIANT) {
1561 if (abs_level <= rl->max_level[last][run]*2) {
1562 av_log(h->c.avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1563 return AVERROR_INVALIDDATA;
1564 }
1565 if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) {
1566 av_log(h->c.avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1567 return AVERROR_INVALIDDATA;
1568 }
1569 }
1570 }
1571 }
1572#endif
1573 if (level > 0)
1574 level = level * qmul + qadd;
1575 else
1576 level = level * qmul - qadd;
1577
1578 if ((unsigned)(level + 2048) > 4095) {
1579 if (h->c.avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)) {
1580 if (level > 2560 || level < -2560) {
1581 av_log(h->c.avctx, AV_LOG_ERROR,
1582 "|level| overflow in 3. esc, qp=%d\n",
1583 h->c.qscale);
1584 return AVERROR_INVALIDDATA;
1585 }
1586 }
1587 level = level < 0 ? -2048 : 2047;
1588 }
1589
1590 i += run + 1;
1591 if (last)
1592 i += 192;
1593 } else {
1594 /* second escape */
1595 SKIP_BITS(re, &h->gb, 2);
1596 GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1597 i += run + rl->max_run[run >> 7][level / qmul] + 1; // FIXME opt indexing
1598 level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1);
1599 LAST_SKIP_BITS(re, &h->gb, 1);
1600 }
1601 } else {
1602 /* first escape */
1603 SKIP_BITS(re, &h->gb, 1);
1604 GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1605 i += run;
1606 level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; // FIXME opt indexing
1607 level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1);
1608 LAST_SKIP_BITS(re, &h->gb, 1);
1609 }
1610 }
1611 } else {
1612 i += run;
1613 level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1);
1614 LAST_SKIP_BITS(re, &h->gb, 1);
1615 }
1616 ff_tlog(h->c.avctx, "dct[%d][%d] = %- 4d end?:%d\n", scan_table[i&63]&7, scan_table[i&63] >> 3, level, i>62);
1617 if (i > 62) {
1618 i -= 192;
1619 if (i & (~63)) {
1620 av_log(h->c.avctx, AV_LOG_ERROR,
1621 "ac-tex damaged at %d %d\n", h->c.mb_x, h->c.mb_y);
1622 return AVERROR_INVALIDDATA;
1623 }
1624
1625 block[scan_table[i]] = level;
1626 break;
1627 }
1628
1629 block[scan_table[i]] = level;
1630 }
1631 CLOSE_READER(re, &h->gb);
1632 }
1633
1634not_coded:
1635 if (intra) {
1636 if (!use_intra_dc_vlc) {
1637 block[0] = mpeg4_get_level_dc(&h->c, n, pred, block[0]);
1638
1639 i -= i >> 31; // if (i == -1) i = 0;
1640 }
1641
1642 ff_mpeg4_pred_ac(h, block, n, dc_pred_dir);
1643 if (h->c.ac_pred)
1644 i = 63; // FIXME not optimal
1645 }
1646 h->c.block_last_index[n] = i;
1647 return 0;
1648}
1649
1650/**
1651 * decode partition C of one MB.
1652 * @return <0 if an error occurred
1653 */
1655{
1657 const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride;
1658
1659 const int mb_type = h->c.cur_pic.mb_type[xy];
1660 int cbp = h->c.cbp_table[xy];
1661
1662 const int use_intra_dc_vlc = h->c.qscale < ctx->intra_dc_threshold;
1663
1664 if (h->c.cur_pic.qscale_table[xy] != h->c.qscale)
1665 ff_set_qscale(&h->c, h->c.cur_pic.qscale_table[xy]);
1666
1667 if (h->c.pict_type == AV_PICTURE_TYPE_P ||
1668 h->c.pict_type == AV_PICTURE_TYPE_S) {
1669 int i;
1670 for (i = 0; i < 4; i++) {
1671 h->c.mv[0][i][0] = h->c.cur_pic.motion_val[0][h->c.block_index[i]][0];
1672 h->c.mv[0][i][1] = h->c.cur_pic.motion_val[0][h->c.block_index[i]][1];
1673 }
1674 h->c.mb_intra = IS_INTRA(mb_type);
1675
1676 if (IS_SKIP(mb_type)) {
1677 /* skip mb */
1678 for (i = 0; i < 6; i++)
1679 h->c.block_last_index[i] = -1;
1680 h->c.mv_dir = MV_DIR_FORWARD;
1681 h->c.mv_type = MV_TYPE_16X16;
1682 if (h->c.pict_type == AV_PICTURE_TYPE_S
1683 && ctx->vol_sprite_usage == GMC_SPRITE) {
1684 h->c.mcsel = 1;
1685 h->c.mb_skipped = 0;
1686 h->c.cur_pic.mbskip_table[xy] = 0;
1687 } else {
1688 h->c.mcsel = 0;
1689 h->c.mb_skipped = 1;
1690 h->c.cur_pic.mbskip_table[xy] = 1;
1691 }
1692 } else if (h->c.mb_intra) {
1693 h->c.ac_pred = IS_ACPRED(h->c.cur_pic.mb_type[xy]);
1694 } else if (!h->c.mb_intra) {
1695 // h->c.mcsel = 0; // FIXME do we need to init that?
1696
1697 h->c.mv_dir = MV_DIR_FORWARD;
1698 if (IS_8X8(mb_type)) {
1699 h->c.mv_type = MV_TYPE_8X8;
1700 } else {
1701 h->c.mv_type = MV_TYPE_16X16;
1702 }
1703 }
1704 } else { /* I-Frame */
1705 h->c.mb_intra = 1;
1706 h->c.ac_pred = IS_ACPRED(h->c.cur_pic.mb_type[xy]);
1707 }
1708
1709 if (!IS_SKIP(mb_type)) {
1710 int i;
1711 h->c.bdsp.clear_blocks(h->block[0]);
1712 /* decode each block */
1713 for (i = 0; i < 6; i++) {
1714 if (mpeg4_decode_block(ctx, h->block[i], i, cbp & 32, h->c.mb_intra,
1715 use_intra_dc_vlc, ctx->rvlc) < 0) {
1716 av_log(h->c.avctx, AV_LOG_ERROR,
1717 "texture corrupted at %d %d %d\n",
1718 h->c.mb_x, h->c.mb_y, h->c.mb_intra);
1719 return AVERROR_INVALIDDATA;
1720 }
1721 cbp += cbp;
1722 }
1723 }
1724
1725 /* per-MB end of slice check */
1726 if (--h->mb_num_left <= 0) {
1727 if (mpeg4_is_resync(ctx))
1728 return SLICE_END;
1729 else
1730 return SLICE_NOEND;
1731 } else {
1732 if (mpeg4_is_resync(ctx)) {
1733 const int delta = h->c.mb_x + 1 == h->c.mb_width ? 2 : 1;
1734 if (h->c.cbp_table[xy + delta])
1735 return SLICE_END;
1736 }
1737 return SLICE_OK;
1738 }
1739}
1740
1742{
1744 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1745 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1746 const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride;
1747 int next;
1748
1749 av_assert2(h->c.h263_pred);
1750
1751 if (h->c.pict_type == AV_PICTURE_TYPE_P ||
1752 h->c.pict_type == AV_PICTURE_TYPE_S) {
1753 do {
1754 if (get_bits1(&h->gb)) {
1755 /* skip mb */
1756 h->c.mb_intra = 0;
1757 for (i = 0; i < 6; i++)
1758 h->c.block_last_index[i] = -1;
1759 h->c.mv_dir = MV_DIR_FORWARD;
1760 h->c.mv_type = MV_TYPE_16X16;
1761 if (h->c.pict_type == AV_PICTURE_TYPE_S &&
1762 ctx->vol_sprite_usage == GMC_SPRITE) {
1763 h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP |
1764 MB_TYPE_GMC |
1767 h->c.mcsel = 1;
1768 h->c.mv[0][0][0] = get_amv(ctx, 0);
1769 h->c.mv[0][0][1] = get_amv(ctx, 1);
1770 h->c.cur_pic.mbskip_table[xy] = 0;
1771 h->c.mb_skipped = 0;
1772 } else {
1773 h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 |
1775 h->c.mcsel = 0;
1776 h->c.mv[0][0][0] = 0;
1777 h->c.mv[0][0][1] = 0;
1778 h->c.cur_pic.mbskip_table[xy] = 1;
1779 h->c.mb_skipped = 1;
1780 }
1781 goto end;
1782 }
1784 if (cbpc < 0) {
1785 av_log(h->c.avctx, AV_LOG_ERROR,
1786 "mcbpc damaged at %d %d\n", h->c.mb_x, h->c.mb_y);
1787 return AVERROR_INVALIDDATA;
1788 }
1789 } while (cbpc == 20);
1790
1791 dquant = cbpc & 8;
1792 h->c.mb_intra = ((cbpc & 4) != 0);
1793 if (h->c.mb_intra)
1794 goto intra;
1795 h->c.bdsp.clear_blocks(h->block[0]);
1796
1797 if (h->c.pict_type == AV_PICTURE_TYPE_S &&
1798 ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
1799 h->c.mcsel = get_bits1(&h->gb);
1800 else
1801 h->c.mcsel = 0;
1802 cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1) ^ 0x0F;
1803 if (cbpy < 0) {
1804 av_log(h->c.avctx, AV_LOG_ERROR,
1805 "P cbpy damaged at %d %d\n", h->c.mb_x, h->c.mb_y);
1806 return AVERROR_INVALIDDATA;
1807 }
1808
1809 cbp = (cbpc & 3) | (cbpy << 2);
1810 if (dquant)
1811 ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]);
1812 if ((!h->c.progressive_sequence) &&
1813 (cbp || (h->c.workaround_bugs & FF_BUG_XVID_ILACE)))
1814 h->c.interlaced_dct = get_bits1(&h->gb);
1815
1816 h->c.mv_dir = MV_DIR_FORWARD;
1817 if ((cbpc & 16) == 0) {
1818 if (h->c.mcsel) {
1819 h->c.cur_pic.mb_type[xy] = MB_TYPE_GMC | MB_TYPE_16x16 |
1821 /* 16x16 global motion prediction */
1822 h->c.mv_type = MV_TYPE_16X16;
1823 mx = get_amv(ctx, 0);
1824 my = get_amv(ctx, 1);
1825 h->c.mv[0][0][0] = mx;
1826 h->c.mv[0][0][1] = my;
1827 } else if ((!h->c.progressive_sequence) && get_bits1(&h->gb)) {
1828 h->c.cur_pic.mb_type[xy] = MB_TYPE_16x8 | MB_TYPE_FORWARD_MV |
1830 /* 16x8 field motion prediction */
1831 h->c.mv_type = MV_TYPE_FIELD;
1832
1833 h->c.field_select[0][0] = get_bits1(&h->gb);
1834 h->c.field_select[0][1] = get_bits1(&h->gb);
1835
1836 ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y);
1837
1838 for (i = 0; i < 2; i++) {
1839 mx = ff_h263_decode_motion(h, pred_x, ctx->f_code);
1840 if (mx >= 0xffff)
1841 return AVERROR_INVALIDDATA;
1842
1843 my = ff_h263_decode_motion(h, pred_y / 2, ctx->f_code);
1844 if (my >= 0xffff)
1845 return AVERROR_INVALIDDATA;
1846
1847 h->c.mv[0][i][0] = mx;
1848 h->c.mv[0][i][1] = my;
1849 }
1850 } else {
1851 h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
1852 /* 16x16 motion prediction */
1853 h->c.mv_type = MV_TYPE_16X16;
1854 ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y);
1855 mx = ff_h263_decode_motion(h, pred_x, ctx->f_code);
1856
1857 if (mx >= 0xffff)
1858 return AVERROR_INVALIDDATA;
1859
1860 my = ff_h263_decode_motion(h, pred_y, ctx->f_code);
1861
1862 if (my >= 0xffff)
1863 return AVERROR_INVALIDDATA;
1864 h->c.mv[0][0][0] = mx;
1865 h->c.mv[0][0][1] = my;
1866 }
1867 } else {
1868 h->c.cur_pic.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_FORWARD_MV;
1869 h->c.mv_type = MV_TYPE_8X8;
1870 for (i = 0; i < 4; i++) {
1871 int16_t *mot_val = ff_h263_pred_motion(&h->c, i, 0, &pred_x, &pred_y);
1872 mx = ff_h263_decode_motion(h, pred_x, ctx->f_code);
1873 if (mx >= 0xffff)
1874 return AVERROR_INVALIDDATA;
1875
1876 my = ff_h263_decode_motion(h, pred_y, ctx->f_code);
1877 if (my >= 0xffff)
1878 return AVERROR_INVALIDDATA;
1879 h->c.mv[0][i][0] = mx;
1880 h->c.mv[0][i][1] = my;
1881 mot_val[0] = mx;
1882 mot_val[1] = my;
1883 }
1884 }
1885 } else if (h->c.pict_type == AV_PICTURE_TYPE_B) {
1886 int modb1; // first bit of modb
1887 int modb2; // second bit of modb
1888 int mb_type;
1889
1890 h->c.mb_intra = 0; // B-frames never contain intra blocks
1891 h->c.mcsel = 0; // ... true gmc blocks
1892
1893 if (h->c.mb_x == 0) {
1894 for (i = 0; i < 2; i++) {
1895 h->c.last_mv[i][0][0] =
1896 h->c.last_mv[i][0][1] =
1897 h->c.last_mv[i][1][0] =
1898 h->c.last_mv[i][1][1] = 0;
1899 }
1900
1901 ff_thread_progress_await(&h->c.next_pic.ptr->progress, h->c.mb_y);
1902 }
1903
1904 /* if we skipped it in the future P-frame than skip it now too */
1905 h->c.mb_skipped = h->c.next_pic.mbskip_table[h->c.mb_y * h->c.mb_stride + h->c.mb_x]; // Note, skiptab=0 if last was GMC
1906
1907 if (h->c.mb_skipped) {
1908 /* skip mb */
1909 for (i = 0; i < 6; i++)
1910 h->c.block_last_index[i] = -1;
1911
1912 h->c.mv_dir = MV_DIR_FORWARD;
1913 h->c.mv_type = MV_TYPE_16X16;
1914 h->c.mv[0][0][0] =
1915 h->c.mv[0][0][1] =
1916 h->c.mv[1][0][0] =
1917 h->c.mv[1][0][1] = 0;
1918 h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP |
1921 goto end;
1922 }
1923
1924 modb1 = get_bits1(&h->gb);
1925 if (modb1) {
1926 // like MB_TYPE_B_DIRECT but no vectors coded
1928 cbp = 0;
1929 } else {
1930 modb2 = get_bits1(&h->gb);
1931 mb_type = get_vlc2(&h->gb, mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 1);
1932 if (mb_type < 0) {
1933 av_log(h->c.avctx, AV_LOG_ERROR, "illegal MB_type\n");
1934 return AVERROR_INVALIDDATA;
1935 }
1936 if (modb2) {
1937 cbp = 0;
1938 } else {
1939 h->c.bdsp.clear_blocks(h->block[0]);
1940 cbp = get_bits(&h->gb, 6);
1941 }
1942
1943 if ((!IS_DIRECT(mb_type)) && cbp) {
1944 if (get_bits1(&h->gb))
1945 ff_set_qscale(&h->c, h->c.qscale + get_bits1(&h->gb) * 4 - 2);
1946 }
1947
1948 if (!h->c.progressive_sequence) {
1949 if (cbp)
1950 h->c.interlaced_dct = get_bits1(&h->gb);
1951
1952 if (!IS_DIRECT(mb_type) && get_bits1(&h->gb)) {
1953 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1954 mb_type &= ~MB_TYPE_16x16;
1955
1956 if (HAS_FORWARD_MV(mb_type)) {
1957 h->c.field_select[0][0] = get_bits1(&h->gb);
1958 h->c.field_select[0][1] = get_bits1(&h->gb);
1959 }
1960 if (HAS_BACKWARD_MV(mb_type)) {
1961 h->c.field_select[1][0] = get_bits1(&h->gb);
1962 h->c.field_select[1][1] = get_bits1(&h->gb);
1963 }
1964 }
1965 }
1966
1967 h->c.mv_dir = 0;
1968 if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
1969 h->c.mv_type = MV_TYPE_16X16;
1970
1971 if (HAS_FORWARD_MV(mb_type)) {
1972 h->c.mv_dir = MV_DIR_FORWARD;
1973
1974 mx = ff_h263_decode_motion(h, h->c.last_mv[0][0][0], ctx->f_code);
1975 my = ff_h263_decode_motion(h, h->c.last_mv[0][0][1], ctx->f_code);
1976 h->c.last_mv[0][1][0] =
1977 h->c.last_mv[0][0][0] =
1978 h->c.mv[0][0][0] = mx;
1979 h->c.last_mv[0][1][1] =
1980 h->c.last_mv[0][0][1] =
1981 h->c.mv[0][0][1] = my;
1982 }
1983
1984 if (HAS_BACKWARD_MV(mb_type)) {
1985 h->c.mv_dir |= MV_DIR_BACKWARD;
1986
1987 mx = ff_h263_decode_motion(h, h->c.last_mv[1][0][0], ctx->b_code);
1988 my = ff_h263_decode_motion(h, h->c.last_mv[1][0][1], ctx->b_code);
1989 h->c.last_mv[1][1][0] =
1990 h->c.last_mv[1][0][0] =
1991 h->c.mv[1][0][0] = mx;
1992 h->c.last_mv[1][1][1] =
1993 h->c.last_mv[1][0][1] =
1994 h->c.mv[1][0][1] = my;
1995 }
1996 } else if (!IS_DIRECT(mb_type)) {
1997 h->c.mv_type = MV_TYPE_FIELD;
1998
1999 if (HAS_FORWARD_MV(mb_type)) {
2000 h->c.mv_dir = MV_DIR_FORWARD;
2001
2002 for (i = 0; i < 2; i++) {
2003 mx = ff_h263_decode_motion(h, h->c.last_mv[0][i][0], ctx->f_code);
2004 my = ff_h263_decode_motion(h, h->c.last_mv[0][i][1] / 2, ctx->f_code);
2005 h->c.last_mv[0][i][0] =
2006 h->c.mv[0][i][0] = mx;
2007 h->c.last_mv[0][i][1] = (h->c.mv[0][i][1] = my) * 2;
2008 }
2009 }
2010
2011 if (HAS_BACKWARD_MV(mb_type)) {
2012 h->c.mv_dir |= MV_DIR_BACKWARD;
2013
2014 for (i = 0; i < 2; i++) {
2015 mx = ff_h263_decode_motion(h, h->c.last_mv[1][i][0], ctx->b_code);
2016 my = ff_h263_decode_motion(h, h->c.last_mv[1][i][1] / 2, ctx->b_code);
2017 h->c.last_mv[1][i][0] =
2018 h->c.mv[1][i][0] = mx;
2019 h->c.last_mv[1][i][1] = (h->c.mv[1][i][1] = my) * 2;
2020 }
2021 }
2022 }
2023 }
2024
2025 if (IS_DIRECT(mb_type)) {
2026 if (IS_SKIP(mb_type)) {
2027 mx =
2028 my = 0;
2029 } else {
2030 mx = ff_h263_decode_motion(h, 0, 1);
2031 my = ff_h263_decode_motion(h, 0, 1);
2032 }
2033
2034 h->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2035 mb_type |= ff_mpeg4_set_direct_mv(&h->c, mx, my);
2036 }
2037 h->c.cur_pic.mb_type[xy] = mb_type;
2038 } else { /* I-Frame */
2039 int use_intra_dc_vlc;
2040
2041 do {
2043 if (cbpc < 0) {
2044 av_log(h->c.avctx, AV_LOG_ERROR,
2045 "I cbpc damaged at %d %d\n", h->c.mb_x, h->c.mb_y);
2046 return AVERROR_INVALIDDATA;
2047 }
2048 } while (cbpc == 8);
2049
2050 dquant = cbpc & 4;
2051 h->c.mb_intra = 1;
2052
2053intra:
2054 h->c.ac_pred = get_bits1(&h->gb);
2055 if (h->c.ac_pred)
2056 h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
2057 else
2058 h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA;
2059
2060 cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
2061 if (cbpy < 0) {
2062 av_log(h->c.avctx, AV_LOG_ERROR,
2063 "I cbpy damaged at %d %d\n", h->c.mb_x, h->c.mb_y);
2064 return AVERROR_INVALIDDATA;
2065 }
2066 cbp = (cbpc & 3) | (cbpy << 2);
2067
2068 use_intra_dc_vlc = h->c.qscale < ctx->intra_dc_threshold;
2069
2070 if (dquant)
2071 ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]);
2072
2073 if (!h->c.progressive_sequence)
2074 h->c.interlaced_dct = get_bits1(&h->gb);
2075
2076 h->c.bdsp.clear_blocks(h->block[0]);
2077 /* decode each block */
2078 for (i = 0; i < 6; i++) {
2079 if (mpeg4_decode_block(ctx, h->block[i], i, cbp & 32,
2080 1, use_intra_dc_vlc, 0) < 0)
2081 return AVERROR_INVALIDDATA;
2082 cbp += cbp;
2083 }
2084 goto end;
2085 }
2086
2087 /* decode each block */
2088 for (i = 0; i < 6; i++) {
2089 if (mpeg4_decode_block(ctx, h->block[i], i, cbp & 32, 0, 0, 0) < 0)
2090 return AVERROR_INVALIDDATA;
2091 cbp += cbp;
2092 }
2093
2094end:
2095 /* per-MB end of slice check */
2096 next = mpeg4_is_resync(ctx);
2097 if (next) {
2098 if (h->c.mb_x + h->c.mb_y*h->c.mb_width + 1 > next && (h->c.avctx->err_recognition & AV_EF_AGGRESSIVE)) {
2099 return AVERROR_INVALIDDATA;
2100 } else if (h->c.mb_x + h->c.mb_y*h->c.mb_width + 1 >= next)
2101 return SLICE_END;
2102
2103 if (h->c.pict_type == AV_PICTURE_TYPE_B) {
2104 const int delta = h->c.mb_x + 1 == h->c.mb_width ? 2 : 1;
2105 ff_thread_progress_await(&h->c.next_pic.ptr->progress,
2106 (h->c.mb_x + delta >= h->c.mb_width)
2107 ? FFMIN(h->c.mb_y + 1, h->c.mb_height - 1)
2108 : h->c.mb_y);
2109 if (h->c.next_pic.mbskip_table[xy + delta])
2110 return SLICE_OK;
2111 }
2112
2113 return SLICE_END;
2114 }
2115
2116 return SLICE_OK;
2117}
2118
2119/* As per spec, studio start code search isn't the same as the old type of start code */
2121{
2122 align_get_bits(gb);
2123
2124 while (get_bits_left(gb) >= 24 && show_bits(gb, 24) != 0x1) {
2125 get_bits(gb, 8);
2126 }
2127}
2128
2129/* additional_code, vlc index */
2130static const uint8_t ac_state_tab[22][2] =
2131{
2132 {0, 0},
2133 {0, 1},
2134 {1, 1},
2135 {2, 1},
2136 {3, 1},
2137 {4, 1},
2138 {5, 1},
2139 {1, 2},
2140 {2, 2},
2141 {3, 2},
2142 {4, 2},
2143 {5, 2},
2144 {6, 2},
2145 {1, 3},
2146 {2, 4},
2147 {3, 5},
2148 {4, 6},
2149 {5, 7},
2150 {6, 8},
2151 {7, 9},
2152 {8, 10},
2153 {0, 11}
2154};
2155
2157{
2158 H263DecContext *const h = &ctx->h;
2159
2160 int cc, dct_dc_size, dct_diff, code, j, idx = 1, group = 0, run = 0,
2161 additional_code_len, sign, mismatch;
2162 const VLCElem *cur_vlc = studio_intra_tab[0];
2163 const uint8_t *const scantable = h->c.intra_scantable.permutated;
2164 const uint16_t *quant_matrix;
2165 uint32_t flc;
2166 const int min = -1 * (1 << (h->c.avctx->bits_per_raw_sample + 6));
2167 const int max = ((1 << (h->c.avctx->bits_per_raw_sample + 6)) - 1);
2168 int shift = 3 - ctx->dct_precision;
2169
2170 mismatch = 1;
2171
2172 memset(block, 0, 64 * sizeof(int32_t));
2173
2174 if (n < 4) {
2175 cc = 0;
2176 dct_dc_size = get_vlc2(&h->gb, studio_luma_dc, STUDIO_INTRA_BITS, 2);
2177 quant_matrix = h->c.intra_matrix;
2178 } else {
2179 cc = (n & 1) + 1;
2180 if (ctx->rgb)
2181 dct_dc_size = get_vlc2(&h->gb, studio_luma_dc, STUDIO_INTRA_BITS, 2);
2182 else
2183 dct_dc_size = get_vlc2(&h->gb, studio_chroma_dc, STUDIO_INTRA_BITS, 2);
2184 quant_matrix = h->c.chroma_intra_matrix;
2185 }
2186
2187 if (dct_dc_size == 0) {
2188 dct_diff = 0;
2189 } else {
2190 dct_diff = get_xbits(&h->gb, dct_dc_size);
2191
2192 if (dct_dc_size > 8) {
2193 if(!check_marker(h->c.avctx, &h->gb, "dct_dc_size > 8"))
2194 return AVERROR_INVALIDDATA;
2195 }
2196
2197 }
2198
2199 h->last_dc[cc] += dct_diff;
2200
2201 if (ctx->mpeg_quant)
2202 block[0] = h->last_dc[cc] * (8 >> h->c.intra_dc_precision);
2203 else
2204 block[0] = h->last_dc[cc] * (8 >> h->c.intra_dc_precision) * (8 >> ctx->dct_precision);
2205 /* TODO: support mpeg_quant for AC coefficients */
2206
2207 block[0] = av_clip(block[0], min, max);
2208 mismatch ^= block[0];
2209
2210 /* AC Coefficients */
2211 while (1) {
2212 group = get_vlc2(&h->gb, cur_vlc, STUDIO_INTRA_BITS, 2);
2213
2214 if (group < 0) {
2215 av_log(h->c.avctx, AV_LOG_ERROR, "illegal ac coefficient group vlc\n");
2216 return AVERROR_INVALIDDATA;
2217 }
2218
2219 additional_code_len = ac_state_tab[group][0];
2220 cur_vlc = studio_intra_tab[ac_state_tab[group][1]];
2221
2222 if (group == 0) {
2223 /* End of Block */
2224 break;
2225 } else if (group >= 1 && group <= 6) {
2226 /* Zero run length (Table B.47) */
2227 run = 1 << additional_code_len;
2228 if (additional_code_len)
2229 run += get_bits(&h->gb, additional_code_len);
2230 idx += run;
2231 continue;
2232 } else if (group >= 7 && group <= 12) {
2233 /* Zero run length and +/-1 level (Table B.48) */
2234 code = get_bits(&h->gb, additional_code_len);
2235 sign = code & 1;
2236 code >>= 1;
2237 run = (1 << (additional_code_len - 1)) + code;
2238 idx += run;
2239 if (idx > 63)
2240 return AVERROR_INVALIDDATA;
2241 j = scantable[idx++];
2242 block[j] = sign ? 1 : -1;
2243 } else if (group >= 13 && group <= 20) {
2244 /* Level value (Table B.49) */
2245 if (idx > 63)
2246 return AVERROR_INVALIDDATA;
2247 j = scantable[idx++];
2248 block[j] = get_xbits(&h->gb, additional_code_len);
2249 } else if (group == 21) {
2250 /* Escape */
2251 if (idx > 63)
2252 return AVERROR_INVALIDDATA;
2253 j = scantable[idx++];
2254 additional_code_len = h->c.avctx->bits_per_raw_sample + ctx->dct_precision + 4;
2255 flc = get_bits(&h->gb, additional_code_len);
2256 if (flc >> (additional_code_len-1))
2257 block[j] = -1 * (( flc ^ ((1 << additional_code_len) -1)) + 1);
2258 else
2259 block[j] = flc;
2260 }
2261 block[j] = ((block[j] * quant_matrix[j] * h->c.qscale) * (1 << shift)) / 16;
2262 block[j] = av_clip(block[j], min, max);
2263 mismatch ^= block[j];
2264 }
2265
2266 block[63] ^= mismatch & 1;
2267
2268 return 0;
2269}
2270
2272 int16_t macroblock[256], int n)
2273{
2274 H263DecContext *const h = &ctx->h;
2275 int j, w, height, idx = 0;
2276 int block_mean, rice_parameter, rice_prefix_code, rice_suffix_code,
2277 dpcm_residual, left, top, topleft, min_left_top, max_left_top, p, p2, output;
2278 height = 16 >> (n ? h->c.chroma_y_shift : 0);
2279 w = 16 >> (n ? h->c.chroma_x_shift : 0);
2280
2281 block_mean = get_bits(&h->gb, h->c.avctx->bits_per_raw_sample);
2282 if (block_mean == 0){
2283 av_log(h->c.avctx, AV_LOG_ERROR, "Forbidden block_mean\n");
2284 return AVERROR_INVALIDDATA;
2285 }
2286 h->last_dc[n] = block_mean * (1 << (ctx->dct_precision + h->c.intra_dc_precision));
2287
2288 rice_parameter = get_bits(&h->gb, 4);
2289 if (rice_parameter == 0) {
2290 av_log(h->c.avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n");
2291 return AVERROR_INVALIDDATA;
2292 }
2293
2294 if (rice_parameter == 15)
2295 rice_parameter = 0;
2296
2297 if (rice_parameter > 11) {
2298 av_log(h->c.avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n");
2299 return AVERROR_INVALIDDATA;
2300 }
2301
2302 for (int i = 0; i < height; i++) {
2303 output = 1 << (h->c.avctx->bits_per_raw_sample - 1);
2304 top = 1 << (h->c.avctx->bits_per_raw_sample - 1);
2305
2306 for (j = 0; j < w; j++) {
2307 left = output;
2308 topleft = top;
2309
2310 rice_prefix_code = get_unary(&h->gb, 1, 12);
2311
2312 /* Escape */
2313 if (rice_prefix_code == 11)
2314 dpcm_residual = get_bits(&h->gb, h->c.avctx->bits_per_raw_sample);
2315 else {
2316 if (rice_prefix_code == 12) {
2317 av_log(h->c.avctx, AV_LOG_ERROR, "Forbidden rice_prefix_code\n");
2318 return AVERROR_INVALIDDATA;
2319 }
2320 rice_suffix_code = get_bitsz(&h->gb, rice_parameter);
2321 dpcm_residual = (rice_prefix_code << rice_parameter) + rice_suffix_code;
2322 }
2323
2324 /* Map to a signed residual */
2325 if (dpcm_residual & 1)
2326 dpcm_residual = (-1 * dpcm_residual) >> 1;
2327 else
2328 dpcm_residual = (dpcm_residual >> 1);
2329
2330 if (i != 0)
2331 top = macroblock[idx-w];
2332
2333 p = left + top - topleft;
2334 min_left_top = FFMIN(left, top);
2335 if (p < min_left_top)
2336 p = min_left_top;
2337
2338 max_left_top = FFMAX(left, top);
2339 if (p > max_left_top)
2340 p = max_left_top;
2341
2342 p2 = (FFMIN(min_left_top, topleft) + FFMAX(max_left_top, topleft)) >> 1;
2343 if (p2 == p)
2344 p2 = block_mean;
2345
2346 if (p2 > p)
2347 dpcm_residual *= -1;
2348
2349 macroblock[idx++] = output = (dpcm_residual + p) & ((1 << h->c.avctx->bits_per_raw_sample) - 1);
2350 }
2351 }
2352
2353 return 0;
2354}
2355
2357{
2359 int i;
2360
2361 ctx->dpcm_direction = 0;
2362
2363 /* StudioMacroblock */
2364 /* Assumes I-VOP */
2365 h->c.mb_intra = 1;
2366 if (get_bits1(&h->gb)) { /* compression_mode */
2367 /* DCT */
2368 /* macroblock_type, 1 or 2-bit VLC */
2369 if (!get_bits1(&h->gb)) {
2370 skip_bits1(&h->gb);
2371 h->c.qscale = mpeg_get_qscale(&h->gb, h->c.q_scale_type);
2372 }
2373
2374 for (i = 0; i < mpeg4_block_count[h->c.chroma_format]; i++) {
2375 if (mpeg4_decode_studio_block(ctx, ctx->block32[i], i) < 0)
2376 return AVERROR_INVALIDDATA;
2377 }
2378 } else {
2379 /* DPCM */
2380 check_marker(h->c.avctx, &h->gb, "DPCM block start");
2381 ctx->dpcm_direction = get_bits1(&h->gb) ? -1 : 1;
2382 for (i = 0; i < 3; i++) {
2383 if (mpeg4_decode_dpcm_macroblock(ctx, ctx->dpcm_macroblock[i], i) < 0)
2384 return AVERROR_INVALIDDATA;
2385 }
2386 }
2387
2388 if (get_bits_left(&h->gb) >= 24 && show_bits(&h->gb, 23) == 0) {
2390 return SLICE_END;
2391 }
2392
2393 //vcon-stp9L1.bits (first frame)
2394 if (get_bits_left(&h->gb) == 0)
2395 return SLICE_END;
2396
2397 //vcon-stp2L1.bits, vcon-stp3L1.bits, vcon-stp6L1.bits, vcon-stp7L1.bits, vcon-stp8L1.bits, vcon-stp10L1.bits (first frame)
2398 if (get_bits_left(&h->gb) < 8U && show_bits(&h->gb, get_bits_left(&h->gb)) == 0)
2399 return SLICE_END;
2400
2401 return SLICE_OK;
2402}
2403
2405{
2406 int hours, minutes, seconds;
2407
2408 if (!show_bits(gb, 23)) {
2409 av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
2410 return AVERROR_INVALIDDATA;
2411 }
2412
2413 hours = get_bits(gb, 5);
2414 minutes = get_bits(gb, 6);
2415 check_marker(s->avctx, gb, "in gop_header");
2416 seconds = get_bits(gb, 6);
2417
2418 s->time_base = seconds + 60*(minutes + 60*hours);
2419
2420 skip_bits1(gb);
2421 skip_bits1(gb);
2422
2423 return 0;
2424}
2425
2427{
2428
2429 *profile = get_bits(gb, 4);
2430 *level = get_bits(gb, 4);
2431
2432 // for Simple profile, level 0
2433 if (*profile == 0 && *level == 8) {
2434 *level = 0;
2435 }
2436
2437 return 0;
2438}
2439
2441{
2442 int visual_object_type;
2443 int is_visual_object_identifier = get_bits1(gb);
2444
2445 if (is_visual_object_identifier) {
2446 skip_bits(gb, 4+3);
2447 }
2448 visual_object_type = get_bits(gb, 4);
2449
2450 if (visual_object_type == VOT_VIDEO_ID ||
2451 visual_object_type == VOT_STILL_TEXTURE_ID) {
2452 int video_signal_type = get_bits1(gb);
2453 if (video_signal_type) {
2454 int video_range, color_description;
2455 skip_bits(gb, 3); // video_format
2456 video_range = get_bits1(gb);
2457 color_description = get_bits1(gb);
2458
2459 s->avctx->color_range = video_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
2460
2461 if (color_description) {
2462 s->avctx->color_primaries = get_bits(gb, 8);
2463 s->avctx->color_trc = get_bits(gb, 8);
2464 s->avctx->colorspace = get_bits(gb, 8);
2465 }
2466 }
2467 }
2468
2469 return 0;
2470}
2471
2473{
2474 int i, v;
2475
2476 /* load default matrices */
2477 for (i = 0; i < 64; i++) {
2478 int j = s->idsp.idct_permutation[i];
2480 s->intra_matrix[j] = v;
2481 s->chroma_intra_matrix[j] = v;
2482
2484 s->inter_matrix[j] = v;
2485 s->chroma_inter_matrix[j] = v;
2486 }
2487}
2488
2490{
2491 int i, j, v;
2492
2493 if (get_bits1(gb)) {
2494 if (get_bits_left(gb) < 64*8)
2495 return AVERROR_INVALIDDATA;
2496 /* intra_quantiser_matrix */
2497 for (i = 0; i < 64; i++) {
2498 v = get_bits(gb, 8);
2499 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2500 s->intra_matrix[j] = v;
2501 s->chroma_intra_matrix[j] = v;
2502 }
2503 }
2504
2505 if (get_bits1(gb)) {
2506 if (get_bits_left(gb) < 64*8)
2507 return AVERROR_INVALIDDATA;
2508 /* non_intra_quantiser_matrix */
2509 for (i = 0; i < 64; i++) {
2510 get_bits(gb, 8);
2511 }
2512 }
2513
2514 if (get_bits1(gb)) {
2515 if (get_bits_left(gb) < 64*8)
2516 return AVERROR_INVALIDDATA;
2517 /* chroma_intra_quantiser_matrix */
2518 for (i = 0; i < 64; i++) {
2519 v = get_bits(gb, 8);
2520 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2521 s->chroma_intra_matrix[j] = v;
2522 }
2523 }
2524
2525 if (get_bits1(gb)) {
2526 if (get_bits_left(gb) < 64*8)
2527 return AVERROR_INVALIDDATA;
2528 /* chroma_non_intra_quantiser_matrix */
2529 for (i = 0; i < 64; i++) {
2530 get_bits(gb, 8);
2531 }
2532 }
2533
2535 return 0;
2536}
2537
2539{
2540 uint32_t startcode;
2541 uint8_t extension_type;
2542
2543 startcode = show_bits_long(gb, 32);
2544 if (startcode == USER_DATA_STARTCODE || startcode == EXT_STARTCODE) {
2545
2546 if ((id == 2 || id == 4) && startcode == EXT_STARTCODE) {
2547 skip_bits_long(gb, 32);
2548 extension_type = get_bits(gb, 4);
2549 if (extension_type == QUANT_MATRIX_EXT_ID)
2551 }
2552 }
2553}
2554
2556{
2557 MPVContext *const s = &ctx->h.c;
2558 int width, height, aspect_ratio_info;
2559 int bits_per_raw_sample;
2560 int rgb, chroma_format;
2561
2562 // random_accessible_vol and video_object_type_indication have already
2563 // been read by the caller decode_vol_header()
2564 skip_bits(gb, 4); /* video_object_layer_verid */
2565 ctx->shape = get_bits(gb, 2); /* video_object_layer_shape */
2566 skip_bits(gb, 4); /* video_object_layer_shape_extension */
2567 skip_bits1(gb); /* progressive_sequence */
2568 if (ctx->shape != RECT_SHAPE) {
2569 avpriv_request_sample(s->avctx, "MPEG-4 Studio profile non rectangular shape");
2570 return AVERROR_PATCHWELCOME;
2571 }
2572 if (ctx->shape != BIN_ONLY_SHAPE) {
2573 rgb = get_bits1(gb); /* rgb_components */
2574 chroma_format = get_bits(gb, 2); /* chroma_format */
2575 if (!chroma_format || chroma_format == CHROMA_420 || (rgb && chroma_format == CHROMA_422)) {
2576 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
2577 return AVERROR_INVALIDDATA;
2578 }
2579
2580 bits_per_raw_sample = get_bits(gb, 4); /* bit_depth */
2581 if (bits_per_raw_sample == 10) {
2582 if (rgb) {
2583 s->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
2584 } else {
2585 s->avctx->pix_fmt = chroma_format == CHROMA_422 ? AV_PIX_FMT_YUV422P10 : AV_PIX_FMT_YUV444P10;
2586 }
2587 } else {
2588 avpriv_request_sample(s->avctx, "MPEG-4 Studio profile bit-depth %u", bits_per_raw_sample);
2589 return AVERROR_PATCHWELCOME;
2590 }
2591 if (rgb != ctx->rgb || s->chroma_format != chroma_format)
2592 s->context_reinit = 1;
2593 s->avctx->bits_per_raw_sample = bits_per_raw_sample;
2594 ctx->rgb = rgb;
2595 s->chroma_format = chroma_format;
2596 }
2597 if (ctx->shape == RECT_SHAPE) {
2598 check_marker(s->avctx, gb, "before video_object_layer_width");
2599 width = get_bits(gb, 14); /* video_object_layer_width */
2600 check_marker(s->avctx, gb, "before video_object_layer_height");
2601 height = get_bits(gb, 14); /* video_object_layer_height */
2602 check_marker(s->avctx, gb, "after video_object_layer_height");
2603
2604 /* Do the same check as non-studio profile */
2605 if (width && height) {
2606 if (s->width && s->height &&
2607 (s->width != width || s->height != height))
2608 s->context_reinit = 1;
2609 s->width = width;
2610 s->height = height;
2611 }
2612 }
2613 aspect_ratio_info = get_bits(gb, 4);
2614 if (aspect_ratio_info == FF_ASPECT_EXTENDED) {
2615 s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
2616 s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
2617 } else {
2618 s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[aspect_ratio_info];
2619 }
2620 skip_bits(gb, 4); /* frame_rate_code */
2621 skip_bits(gb, 15); /* first_half_bit_rate */
2622 check_marker(s->avctx, gb, "after first_half_bit_rate");
2623 skip_bits(gb, 15); /* latter_half_bit_rate */
2624 check_marker(s->avctx, gb, "after latter_half_bit_rate");
2625 skip_bits(gb, 15); /* first_half_vbv_buffer_size */
2626 check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2627 skip_bits(gb, 3); /* latter_half_vbv_buffer_size */
2628 skip_bits(gb, 11); /* first_half_vbv_buffer_size */
2629 check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2630 skip_bits(gb, 15); /* latter_half_vbv_occupancy */
2631 check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
2632 s->low_delay = get_bits1(gb);
2633 ctx->mpeg_quant = get_bits1(gb); /* mpeg2_stream */
2634
2636 extension_and_user_data(s, gb, 2);
2637
2638 return 0;
2639}
2640
2642{
2643 H263DecContext *const h = &ctx->h;
2644 int width, height, vo_ver_id, aspect_ratio_info;
2645
2646 /* vol header */
2647 skip_bits(gb, 1); /* random access */
2648 ctx->vo_type = get_bits(gb, 8);
2649
2650 /* If we are in studio profile (per vo_type), check if its all consistent
2651 * and if so continue pass control to decode_studio_vol_header().
2652 * elIf something is inconsistent, error out
2653 * else continue with (non studio) vol header decpoding.
2654 */
2655 if (ctx->vo_type == CORE_STUDIO_VO_TYPE ||
2656 ctx->vo_type == SIMPLE_STUDIO_VO_TYPE) {
2657 if (h->c.avctx->profile != AV_PROFILE_UNKNOWN && h->c.avctx->profile != AV_PROFILE_MPEG4_SIMPLE_STUDIO)
2658 return AVERROR_INVALIDDATA;
2659 h->c.studio_profile = 1;
2660 h->c.avctx->profile = AV_PROFILE_MPEG4_SIMPLE_STUDIO;
2661 return decode_studio_vol_header(ctx, gb);
2662 } else if (h->c.studio_profile) {
2663 return AVERROR_PATCHWELCOME;
2664 }
2665
2666 if (get_bits1(gb) != 0) { /* is_ol_id */
2667 vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
2668 skip_bits(gb, 3); /* vo_priority */
2669 } else {
2670 vo_ver_id = 1;
2671 }
2672 aspect_ratio_info = get_bits(gb, 4);
2673 if (aspect_ratio_info == FF_ASPECT_EXTENDED) {
2674 h->c.avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
2675 h->c.avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
2676 } else {
2677 h->c.avctx->sample_aspect_ratio = ff_h263_pixel_aspect[aspect_ratio_info];
2678 }
2679
2680 if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
2681 int chroma_format = get_bits(gb, 2);
2682 if (chroma_format != CHROMA_420)
2683 av_log(h->c.avctx, AV_LOG_ERROR, "illegal chroma format\n");
2684
2685 h->c.low_delay = get_bits1(gb);
2686 if (get_bits1(gb)) { /* vbv parameters */
2687 get_bits(gb, 15); /* first_half_bitrate */
2688 check_marker(h->c.avctx, gb, "after first_half_bitrate");
2689 get_bits(gb, 15); /* latter_half_bitrate */
2690 check_marker(h->c.avctx, gb, "after latter_half_bitrate");
2691 get_bits(gb, 15); /* first_half_vbv_buffer_size */
2692 check_marker(h->c.avctx, gb, "after first_half_vbv_buffer_size");
2693 get_bits(gb, 3); /* latter_half_vbv_buffer_size */
2694 get_bits(gb, 11); /* first_half_vbv_occupancy */
2695 check_marker(h->c.avctx, gb, "after first_half_vbv_occupancy");
2696 get_bits(gb, 15); /* latter_half_vbv_occupancy */
2697 check_marker(h->c.avctx, gb, "after latter_half_vbv_occupancy");
2698 }
2699 } else {
2700 /* is setting low delay flag only once the smartest thing to do?
2701 * low delay detection will not be overridden. */
2702 if (h->picture_number == 0) {
2703 switch (ctx->vo_type) {
2704 case SIMPLE_VO_TYPE:
2705 case ADV_SIMPLE_VO_TYPE:
2706 h->c.low_delay = 1;
2707 break;
2708 default:
2709 h->c.low_delay = 0;
2710 }
2711 }
2712 }
2713
2714 ctx->shape = get_bits(gb, 2); /* vol shape */
2715 if (ctx->shape != RECT_SHAPE)
2716 av_log(h->c.avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
2717 if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
2718 av_log(h->c.avctx, AV_LOG_ERROR, "Gray shape not supported\n");
2719 skip_bits(gb, 4); /* video_object_layer_shape_extension */
2720 }
2721
2722 check_marker(h->c.avctx, gb, "before time_increment_resolution");
2723
2724 h->c.avctx->framerate.num = get_bits(gb, 16);
2725 if (!h->c.avctx->framerate.num) {
2726 av_log(h->c.avctx, AV_LOG_ERROR, "framerate==0\n");
2727 return AVERROR_INVALIDDATA;
2728 }
2729
2730 ctx->time_increment_bits = av_log2(h->c.avctx->framerate.num - 1) + 1;
2731 if (ctx->time_increment_bits < 1)
2732 ctx->time_increment_bits = 1;
2733
2734 check_marker(h->c.avctx, gb, "before fixed_vop_rate");
2735
2736 if (get_bits1(gb) != 0) /* fixed_vop_rate */
2737 h->c.avctx->framerate.den = get_bits(gb, ctx->time_increment_bits);
2738 else
2739 h->c.avctx->framerate.den = 1;
2740
2741 ctx->t_frame = 0;
2742
2743 if (ctx->shape != BIN_ONLY_SHAPE) {
2744 if (ctx->shape == RECT_SHAPE) {
2745 check_marker(h->c.avctx, gb, "before width");
2746 width = get_bits(gb, 13);
2747 check_marker(h->c.avctx, gb, "before height");
2748 height = get_bits(gb, 13);
2749 check_marker(h->c.avctx, gb, "after height");
2750 if (width && height && /* they should be non zero but who knows */
2751 !(h->c.width && h->c.codec_tag == AV_RL32("MP4S"))) {
2752 if (h->c.width && h->c.height &&
2753 (h->c.width != width || h->c.height != height))
2754 h->c.context_reinit = 1;
2755 h->c.width = width;
2756 h->c.height = height;
2757 }
2758 }
2759
2760 h->c.progressive_sequence =
2761 h->c.progressive_frame = get_bits1(gb) ^ 1;
2762 h->c.interlaced_dct = 0;
2763 if (!get_bits1(gb) && (h->c.avctx->debug & FF_DEBUG_PICT_INFO))
2764 av_log(h->c.avctx, AV_LOG_INFO, /* OBMC Disable */
2765 "MPEG-4 OBMC not supported (very likely buggy encoder)\n");
2766 if (vo_ver_id == 1)
2767 ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
2768 else
2769 ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
2770
2771 if (ctx->vol_sprite_usage == STATIC_SPRITE)
2772 av_log(h->c.avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
2773 if (ctx->vol_sprite_usage == STATIC_SPRITE ||
2774 ctx->vol_sprite_usage == GMC_SPRITE) {
2775 if (ctx->vol_sprite_usage == STATIC_SPRITE) {
2776 skip_bits(gb, 13); // sprite_width
2777 check_marker(h->c.avctx, gb, "after sprite_width");
2778 skip_bits(gb, 13); // sprite_height
2779 check_marker(h->c.avctx, gb, "after sprite_height");
2780 skip_bits(gb, 13); // sprite_left
2781 check_marker(h->c.avctx, gb, "after sprite_left");
2782 skip_bits(gb, 13); // sprite_top
2783 check_marker(h->c.avctx, gb, "after sprite_top");
2784 }
2785 ctx->num_sprite_warping_points = get_bits(gb, 6);
2786 if (ctx->num_sprite_warping_points > 3) {
2787 av_log(h->c.avctx, AV_LOG_ERROR,
2788 "%d sprite_warping_points\n",
2789 ctx->num_sprite_warping_points);
2790 ctx->num_sprite_warping_points = 0;
2791 return AVERROR_INVALIDDATA;
2792 }
2793 ctx->sprite_warping_accuracy = get_bits(gb, 2);
2794 ctx->sprite_brightness_change = get_bits1(gb);
2795 if (ctx->vol_sprite_usage == STATIC_SPRITE)
2796 skip_bits1(gb); // low_latency_sprite
2797 }
2798 // FIXME sadct disable bit if verid!=1 && shape not rect
2799
2800 if (get_bits1(gb) == 1) { /* not_8_bit */
2801 ctx->quant_precision = get_bits(gb, 4); /* quant_precision */
2802 if (get_bits(gb, 4) != 8) /* bits_per_pixel */
2803 av_log(h->c.avctx, AV_LOG_ERROR, "N-bit not supported\n");
2804 if (ctx->quant_precision != 5)
2805 av_log(h->c.avctx, AV_LOG_ERROR,
2806 "quant precision %d\n", ctx->quant_precision);
2807 if (ctx->quant_precision < 3 || ctx->quant_precision > 9)
2808 ctx->quant_precision = 5;
2809 } else {
2810 ctx->quant_precision = 5;
2811 }
2812
2813 // FIXME a bunch of grayscale shape things
2814
2815 if ((ctx->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
2816 int i, v;
2817
2819
2820 /* load custom intra matrix */
2821 if (get_bits1(gb)) {
2822 int last = 0;
2823 for (i = 0; i < 64; i++) {
2824 int j;
2825 if (get_bits_left(gb) < 8) {
2826 av_log(h->c.avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2827 return AVERROR_INVALIDDATA;
2828 }
2829 v = get_bits(gb, 8);
2830 if (v == 0)
2831 break;
2832
2833 last = v;
2834 j = h->c.idsp.idct_permutation[ff_zigzag_direct[i]];
2835 h->c.intra_matrix[j] = last;
2836 }
2837
2838 /* replicate last value */
2839 for (; i < 64; i++) {
2840 int j = h->c.idsp.idct_permutation[ff_zigzag_direct[i]];
2841 h->c.intra_matrix[j] = last;
2842 }
2843 }
2844
2845 /* load custom non intra matrix */
2846 if (get_bits1(gb)) {
2847 int last = 0;
2848 for (i = 0; i < 64; i++) {
2849 int j;
2850 if (get_bits_left(gb) < 8) {
2851 av_log(h->c.avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2852 return AVERROR_INVALIDDATA;
2853 }
2854 v = get_bits(gb, 8);
2855 if (v == 0)
2856 break;
2857
2858 last = v;
2859 j = h->c.idsp.idct_permutation[ff_zigzag_direct[i]];
2860 h->c.inter_matrix[j] = v;
2861 }
2862
2863 /* replicate last value */
2864 for (; i < 64; i++) {
2865 int j = h->c.idsp.idct_permutation[ff_zigzag_direct[i]];
2866 h->c.inter_matrix[j] = last;
2867 }
2868 }
2869
2870 // FIXME a bunch of grayscale shape things
2871 }
2872
2873 if (vo_ver_id != 1)
2874 h->c.quarter_sample = get_bits1(gb);
2875 else
2876 h->c.quarter_sample = 0;
2877
2878 if (get_bits_left(gb) < 4) {
2879 av_log(h->c.avctx, AV_LOG_ERROR, "VOL Header truncated\n");
2880 return AVERROR_INVALIDDATA;
2881 }
2882
2883 if (!get_bits1(gb)) {
2884 int pos = get_bits_count(gb);
2885 int estimation_method = get_bits(gb, 2);
2886 if (estimation_method < 2) {
2887 if (!get_bits1(gb)) {
2888 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */
2889 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */
2890 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */
2891 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */
2892 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */
2893 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upsampling */
2894 }
2895 if (!get_bits1(gb)) {
2896 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */
2897 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */
2898 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */
2899 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */
2900 }
2901 if (!check_marker(h->c.avctx, gb, "in complexity estimation part 1")) {
2903 goto no_cplx_est;
2904 }
2905 if (!get_bits1(gb)) {
2906 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */
2907 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */
2908 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */
2909 ctx->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */
2910 }
2911 if (!get_bits1(gb)) {
2912 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */
2913 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */
2914 ctx->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */
2915 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */
2916 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */
2917 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */
2918 }
2919 if (!check_marker(h->c.avctx, gb, "in complexity estimation part 2")) {
2921 goto no_cplx_est;
2922 }
2923 if (estimation_method == 1) {
2924 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */
2925 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */
2926 }
2927 } else
2928 av_log(h->c.avctx, AV_LOG_ERROR,
2929 "Invalid Complexity estimation method %d\n",
2930 estimation_method);
2931 } else {
2932
2933no_cplx_est:
2934 ctx->cplx_estimation_trash_i =
2935 ctx->cplx_estimation_trash_p =
2936 ctx->cplx_estimation_trash_b = 0;
2937 }
2938
2939 ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
2940
2941 h->data_partitioning = get_bits1(gb);
2942 if (h->data_partitioning)
2943 ctx->rvlc = get_bits1(gb);
2944
2945 if (vo_ver_id != 1) {
2946 ctx->new_pred = get_bits1(gb);
2947 if (ctx->new_pred) {
2948 av_log(h->c.avctx, AV_LOG_ERROR, "new pred not supported\n");
2949 skip_bits(gb, 2); /* requested upstream message type */
2950 skip_bits1(gb); /* newpred segment type */
2951 }
2952 if (get_bits1(gb)) // reduced_res_vop
2953 av_log(h->c.avctx, AV_LOG_ERROR,
2954 "reduced resolution VOP not supported\n");
2955 } else {
2956 ctx->new_pred = 0;
2957 }
2958
2959 ctx->scalability = get_bits1(gb);
2960
2961 if (ctx->scalability) {
2962 GetBitContext bak = *gb;
2963 int h_sampling_factor_n;
2964 int h_sampling_factor_m;
2965 int v_sampling_factor_n;
2966 int v_sampling_factor_m;
2967
2968 skip_bits1(gb); // hierarchy_type
2969 skip_bits(gb, 4); /* ref_layer_id */
2970 skip_bits1(gb); /* ref_layer_sampling_dir */
2971 h_sampling_factor_n = get_bits(gb, 5);
2972 h_sampling_factor_m = get_bits(gb, 5);
2973 v_sampling_factor_n = get_bits(gb, 5);
2974 v_sampling_factor_m = get_bits(gb, 5);
2975 ctx->enhancement_type = get_bits1(gb);
2976
2977 if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
2978 v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
2979 /* illegal scalability header (VERY broken encoder),
2980 * trying to workaround */
2981 ctx->scalability = 0;
2982 *gb = bak;
2983 } else
2984 av_log(h->c.avctx, AV_LOG_ERROR, "scalability not supported\n");
2985
2986 // bin shape stuff FIXME
2987 }
2988 }
2989
2990 if (h->c.avctx->debug&FF_DEBUG_PICT_INFO) {
2991 av_log(h->c.avctx, AV_LOG_DEBUG, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d, low_delay:%d %s%s%s%s\n",
2992 h->c.avctx->framerate.den, h->c.avctx->framerate.num,
2993 ctx->time_increment_bits,
2994 ctx->quant_precision,
2995 h->c.progressive_sequence,
2996 h->c.low_delay,
2997 ctx->scalability ? "scalability " :"" ,
2998 h->c.quarter_sample ? "qpel " : "",
2999 h->data_partitioning ? "partition " : "",
3000 ctx->rvlc ? "rvlc " : ""
3001 );
3002 }
3003
3004 return 0;
3005}
3006
3007/**
3008 * Decode the user data stuff in the header.
3009 * Also initializes divx/xvid/lavc_version/build.
3010 */
3012{
3013 H263DecContext *const h = &ctx->h;
3014 char buf[256];
3015 int i;
3016 int e;
3017 int ver = 0, build = 0, ver2 = 0, ver3 = 0;
3018 char last;
3019
3020 for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
3021 if (show_bits(gb, 23) == 0)
3022 break;
3023 buf[i] = get_bits(gb, 8);
3024 }
3025 buf[i] = 0;
3026
3027 /* divx detection */
3028 e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
3029 if (e < 2)
3030 e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
3031 if (e >= 2) {
3032 ctx->divx_version = ver;
3033 ctx->divx_build = build;
3034 h->divx_packed = e == 3 && last == 'p';
3035 }
3036
3037 /* libavcodec detection */
3038 e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
3039 if (e != 4)
3040 e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
3041 if (e != 4) {
3042 e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
3043 if (e > 1) {
3044 if (ver > 0xFFU || ver2 > 0xFFU || ver3 > 0xFFU) {
3045 av_log(h->c.avctx, AV_LOG_WARNING,
3046 "Unknown Lavc version string encountered, %d.%d.%d; "
3047 "clamping sub-version values to 8-bits.\n",
3048 ver, ver2, ver3);
3049 }
3050 build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF);
3051 }
3052 }
3053 if (e != 4) {
3054 if (strcmp(buf, "ffmpeg") == 0)
3055 ctx->lavc_build = 4600;
3056 }
3057 if (e == 4)
3058 ctx->lavc_build = build;
3059
3060 /* Xvid detection */
3061 e = sscanf(buf, "XviD%d", &build);
3062 if (e == 1)
3063 ctx->xvid_build = build;
3064
3065 return 0;
3066}
3067
3069 int parse_only)
3070{
3071 H263DecContext *const h = &ctx->h;
3072 int time_incr, time_increment;
3073 int64_t pts;
3074
3075 h->c.mcsel = 0;
3076 h->c.pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
3077 if (h->c.pict_type == AV_PICTURE_TYPE_B && h->c.low_delay &&
3078 ctx->vol_control_parameters == 0 && !(h->c.avctx->flags & AV_CODEC_FLAG_LOW_DELAY)) {
3079 av_log(h->c.avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n");
3080 h->c.low_delay = 0;
3081 }
3082
3083 h->partitioned_frame = h->data_partitioning && h->c.pict_type != AV_PICTURE_TYPE_B;
3084 if (h->partitioned_frame)
3085 h->decode_mb = mpeg4_decode_partitioned_mb;
3086 else
3087 h->decode_mb = mpeg4_decode_mb;
3088
3089 time_incr = 0;
3090 while (get_bits1(gb) != 0)
3091 time_incr++;
3092
3093 check_marker(h->c.avctx, gb, "before time_increment");
3094
3095 if (ctx->time_increment_bits == 0 ||
3096 !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
3097 av_log(h->c.avctx, AV_LOG_WARNING,
3098 "time_increment_bits %d is invalid in relation to the current bitstream, this is likely caused by a missing VOL header\n", ctx->time_increment_bits);
3099
3100 for (ctx->time_increment_bits = 1;
3101 ctx->time_increment_bits < 16;
3102 ctx->time_increment_bits++) {
3103 if (h->c.pict_type == AV_PICTURE_TYPE_P ||
3104 (h->c.pict_type == AV_PICTURE_TYPE_S &&
3105 ctx->vol_sprite_usage == GMC_SPRITE)) {
3106 if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
3107 break;
3108 } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18)
3109 break;
3110 }
3111
3112 av_log(h->c.avctx, AV_LOG_WARNING,
3113 "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits);
3114 }
3115
3116 if (IS_3IV1)
3117 time_increment = get_bits1(gb); // FIXME investigate further
3118 else
3119 time_increment = get_bits(gb, ctx->time_increment_bits);
3120
3121 if (h->c.pict_type != AV_PICTURE_TYPE_B) {
3122 h->c.last_time_base = h->c.time_base;
3123 h->c.time_base += time_incr;
3124 h->c.time = h->c.time_base * (int64_t)h->c.avctx->framerate.num + time_increment;
3125 if (h->c.workaround_bugs & FF_BUG_UMP4) {
3126 if (h->c.time < h->c.last_non_b_time) {
3127 /* header is not mpeg-4-compatible, broken encoder,
3128 * trying to workaround */
3129 h->c.time_base++;
3130 h->c.time += h->c.avctx->framerate.num;
3131 }
3132 }
3133 h->c.pp_time = h->c.time - h->c.last_non_b_time;
3134 h->c.last_non_b_time = h->c.time;
3135 } else {
3136 h->c.time = (h->c.last_time_base + time_incr) * (int64_t)h->c.avctx->framerate.num + time_increment;
3137 h->c.pb_time = h->c.pp_time - (h->c.last_non_b_time - h->c.time);
3138 if (h->c.pp_time <= h->c.pb_time ||
3139 h->c.pp_time <= h->c.pp_time - h->c.pb_time ||
3140 h->c.pp_time <= 0) {
3141 /* messed up order, maybe after seeking? skipping current B-frame */
3142 return FRAME_SKIPPED;
3143 }
3145
3146 if (ctx->t_frame == 0)
3147 ctx->t_frame = h->c.pb_time;
3148 if (ctx->t_frame == 0)
3149 ctx->t_frame = 1; // 1/0 protection
3150 h->c.pp_field_time = (ROUNDED_DIV(h->c.last_non_b_time, ctx->t_frame) -
3151 ROUNDED_DIV(h->c.last_non_b_time - h->c.pp_time, ctx->t_frame)) * 2;
3152 h->c.pb_field_time = (ROUNDED_DIV(h->c.time, ctx->t_frame) -
3153 ROUNDED_DIV(h->c.last_non_b_time - h->c.pp_time, ctx->t_frame)) * 2;
3154 if (h->c.pp_field_time <= h->c.pb_field_time || h->c.pb_field_time <= 1) {
3155 h->c.pb_field_time = 2;
3156 h->c.pp_field_time = 4;
3157 if (!h->c.progressive_sequence)
3158 return FRAME_SKIPPED;
3159 }
3160 }
3161
3162 if (h->c.avctx->framerate.den)
3163 pts = ROUNDED_DIV(h->c.time, h->c.avctx->framerate.den);
3164 else
3166 ff_dlog(h->c.avctx, "MPEG4 PTS: %"PRId64"\n", pts);
3167
3168 check_marker(h->c.avctx, gb, "before vop_coded");
3169
3170 /* vop coded */
3171 if (get_bits1(gb) != 1) {
3172 if (h->c.avctx->debug & FF_DEBUG_PICT_INFO)
3173 av_log(h->c.avctx, AV_LOG_ERROR, "vop not coded\n");
3174 h->skipped_last_frame = 1;
3175 return FRAME_SKIPPED;
3176 }
3177 if (ctx->new_pred)
3178 decode_new_pred(ctx, gb);
3179
3180 if (ctx->shape != BIN_ONLY_SHAPE &&
3181 (h->c.pict_type == AV_PICTURE_TYPE_P ||
3182 (h->c.pict_type == AV_PICTURE_TYPE_S &&
3183 ctx->vol_sprite_usage == GMC_SPRITE))) {
3184 /* rounding type for motion estimation */
3185 h->c.no_rounding = get_bits1(gb);
3186 } else {
3187 h->c.no_rounding = 0;
3188 }
3189 // FIXME reduced res stuff
3190
3191 if (ctx->shape != RECT_SHAPE) {
3192 if (ctx->vol_sprite_usage != 1 || h->c.pict_type != AV_PICTURE_TYPE_I) {
3193 skip_bits(gb, 13); /* width */
3194 check_marker(h->c.avctx, gb, "after width");
3195 skip_bits(gb, 13); /* height */
3196 check_marker(h->c.avctx, gb, "after height");
3197 skip_bits(gb, 13); /* hor_spat_ref */
3198 check_marker(h->c.avctx, gb, "after hor_spat_ref");
3199 skip_bits(gb, 13); /* ver_spat_ref */
3200 }
3201 skip_bits1(gb); /* change_CR_disable */
3202
3203 if (get_bits1(gb) != 0)
3204 skip_bits(gb, 8); /* constant_alpha_value */
3205 }
3206
3207 // FIXME complexity estimation stuff
3208
3209 if (ctx->shape != BIN_ONLY_SHAPE) {
3210 skip_bits_long(gb, ctx->cplx_estimation_trash_i);
3211 if (h->c.pict_type != AV_PICTURE_TYPE_I)
3212 skip_bits_long(gb, ctx->cplx_estimation_trash_p);
3213 if (h->c.pict_type == AV_PICTURE_TYPE_B)
3214 skip_bits_long(gb, ctx->cplx_estimation_trash_b);
3215
3216 if (get_bits_left(gb) < 3) {
3217 av_log(h->c.avctx, AV_LOG_ERROR, "Header truncated\n");
3218 return AVERROR_INVALIDDATA;
3219 }
3220 ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
3221 if (!h->c.progressive_sequence) {
3222 h->c.top_field_first = get_bits1(gb);
3223 h->c.alternate_scan = get_bits1(gb);
3224 } else
3225 h->c.alternate_scan = 0;
3226 }
3227 /* Skip at this point when only parsing since the remaining
3228 * data is not useful for a parser and requires the
3229 * sprite_trajectory VLC to be initialized. */
3230 if (parse_only)
3231 goto end;
3232
3233 if (h->c.alternate_scan) {
3234 ff_init_scantable(h->c.idsp.idct_permutation, &h->c.intra_scantable, ff_alternate_vertical_scan);
3235 ff_permute_scantable(h->permutated_intra_h_scantable, ff_alternate_vertical_scan,
3236 h->c.idsp.idct_permutation);
3237 } else {
3238 ff_init_scantable(h->c.idsp.idct_permutation, &h->c.intra_scantable, ff_zigzag_direct);
3239 ff_permute_scantable(h->permutated_intra_h_scantable, ff_alternate_horizontal_scan,
3240 h->c.idsp.idct_permutation);
3241 }
3242 ff_permute_scantable(h->permutated_intra_v_scantable, ff_alternate_vertical_scan,
3243 h->c.idsp.idct_permutation);
3244
3245 if (h->c.pict_type == AV_PICTURE_TYPE_S) {
3246 if((ctx->vol_sprite_usage == STATIC_SPRITE ||
3247 ctx->vol_sprite_usage == GMC_SPRITE)) {
3249 return AVERROR_INVALIDDATA;
3250 if (ctx->sprite_brightness_change)
3251 av_log(h->c.avctx, AV_LOG_ERROR,
3252 "sprite_brightness_change not supported\n");
3253 if (ctx->vol_sprite_usage == STATIC_SPRITE)
3254 av_log(h->c.avctx, AV_LOG_ERROR, "static sprite not supported\n");
3255 } else {
3256 memset(ctx->sprite_offset, 0, sizeof(ctx->sprite_offset));
3257 memset(ctx->sprite_delta, 0, sizeof(ctx->sprite_delta));
3258 }
3259 }
3260
3261 ctx->f_code = 1;
3262 ctx->b_code = 1;
3263 if (ctx->shape != BIN_ONLY_SHAPE) {
3264 h->c.chroma_qscale = h->c.qscale = get_bits(gb, ctx->quant_precision);
3265 if (h->c.qscale == 0) {
3266 av_log(h->c.avctx, AV_LOG_ERROR,
3267 "Error, header damaged or not MPEG-4 header (qscale=0)\n");
3268 return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
3269 }
3270
3271 if (h->c.pict_type != AV_PICTURE_TYPE_I) {
3272 ctx->f_code = get_bits(gb, 3); /* fcode_for */
3273 if (ctx->f_code == 0) {
3274 av_log(h->c.avctx, AV_LOG_ERROR,
3275 "Error, header damaged or not MPEG-4 header (f_code=0)\n");
3276 ctx->f_code = 1;
3277 return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
3278 }
3279 }
3280
3281 if (h->c.pict_type == AV_PICTURE_TYPE_B) {
3282 ctx->b_code = get_bits(gb, 3);
3283 if (ctx->b_code == 0) {
3284 av_log(h->c.avctx, AV_LOG_ERROR,
3285 "Error, header damaged or not MPEG4 header (b_code=0)\n");
3286 ctx->b_code=1;
3287 return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly
3288 }
3289 }
3290
3291 if (h->c.avctx->debug & FF_DEBUG_PICT_INFO) {
3292 av_log(h->c.avctx, AV_LOG_DEBUG,
3293 "qp:%d fc:%d,%d %c size:%d pro:%d alt:%d top:%d %cpel part:%d resync:%d w:%d a:%d rnd:%d vot:%d%s dc:%d ce:%d/%d/%d time:%"PRId64" tincr:%d\n",
3294 h->c.qscale, ctx->f_code, ctx->b_code,
3295 h->c.pict_type == AV_PICTURE_TYPE_I ? 'I' : (h->c.pict_type == AV_PICTURE_TYPE_P ? 'P' : (h->c.pict_type == AV_PICTURE_TYPE_B ? 'B' : 'S')),
3296 gb->size_in_bits,h->c.progressive_sequence, h->c.alternate_scan,
3297 h->c.top_field_first, h->c.quarter_sample ? 'q' : 'h',
3298 h->data_partitioning, ctx->resync_marker,
3299 ctx->num_sprite_warping_points, ctx->sprite_warping_accuracy,
3300 1 - h->c.no_rounding, ctx->vo_type,
3301 ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
3302 ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
3303 ctx->cplx_estimation_trash_b,
3304 h->c.time,
3305 time_increment
3306 );
3307 }
3308
3309 if (!ctx->scalability) {
3310 if (ctx->shape != RECT_SHAPE && h->c.pict_type != AV_PICTURE_TYPE_I)
3311 skip_bits1(gb); // vop shape coding type
3312 } else {
3313 if (ctx->enhancement_type) {
3314 int load_backward_shape = get_bits1(gb);
3315 if (load_backward_shape)
3316 av_log(h->c.avctx, AV_LOG_ERROR,
3317 "load backward shape isn't supported\n");
3318 }
3319 skip_bits(gb, 2); // ref_select_code
3320 }
3321 }
3322
3323 h->c.dct_unquantize_intra = ctx->mpeg_quant ? ctx->dct_unquantize_mpeg2_intra
3324 : ctx->dct_unquantize_h263_intra;
3325 // The following tells ff_mpv_reconstruct_mb() to unquantize iff mpeg_quant
3326 h->c.dct_unquantize_inter = ctx->mpeg_quant ? ctx->dct_unquantize_mpeg2_inter : NULL;
3327
3328end:
3329 /* detect buggy encoders which don't set the low_delay flag
3330 * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames
3331 * easily (although it's buggy too) */
3332 if (ctx->vo_type == 0 && ctx->vol_control_parameters == 0 &&
3333 ctx->divx_version == -1 && h->picture_number == 0) {
3334 av_log(h->c.avctx, AV_LOG_WARNING,
3335 "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
3336 h->c.low_delay = 1;
3337 }
3338
3339 h->picture_number++; // better than pic number==0 always ;)
3340
3341 if (h->c.workaround_bugs & FF_BUG_EDGE) {
3342 h->c.h_edge_pos = h->c.width;
3343 h->c.v_edge_pos = h->c.height;
3344 }
3345 return 0;
3346}
3347
3349{
3350 AVCodecContext *const avctx = ctx->h.c.avctx;
3351
3352 skip_bits(gb, 16); /* Time_code[63..48] */
3353 check_marker(avctx, gb, "after Time_code[63..48]");
3354 skip_bits(gb, 16); /* Time_code[47..32] */
3355 check_marker(avctx, gb, "after Time_code[47..32]");
3356 skip_bits(gb, 16); /* Time_code[31..16] */
3357 check_marker(avctx, gb, "after Time_code[31..16]");
3358 skip_bits(gb, 16); /* Time_code[15..0] */
3359 check_marker(avctx, gb, "after Time_code[15..0]");
3360 skip_bits(gb, 4); /* reserved_bits */
3361}
3362
3363/**
3364 * Decode the next studio vop header.
3365 * @return <0 if something went wrong
3366 */
3368{
3369 H263DecContext *const h = &ctx->h;
3370
3371 if (get_bits_left(gb) <= 32)
3372 return 0;
3373
3374 h->partitioned_frame = 0;
3375 h->c.interlaced_dct = 0;
3376 h->decode_mb = mpeg4_decode_studio_mb;
3377
3378 decode_smpte_tc(ctx, gb);
3379
3380 skip_bits(gb, 10); /* temporal_reference */
3381 skip_bits(gb, 2); /* vop_structure */
3382 h->c.pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* vop_coding_type */
3383 if (get_bits1(gb)) { /* vop_coded */
3384 skip_bits1(gb); /* top_field_first */
3385 skip_bits1(gb); /* repeat_first_field */
3386 h->c.progressive_frame = get_bits1(gb) ^ 1; /* progressive_frame */
3387 }
3388
3389 if (h->c.pict_type == AV_PICTURE_TYPE_I) {
3390 if (get_bits1(gb))
3392 }
3393
3394 if (ctx->shape != BIN_ONLY_SHAPE) {
3395 h->c.alternate_scan = get_bits1(gb);
3396 h->c.frame_pred_frame_dct = get_bits1(gb);
3397 ctx->dct_precision = get_bits(gb, 2);
3398 h->c.intra_dc_precision = get_bits(gb, 2);
3399 h->c.q_scale_type = get_bits1(gb);
3400 }
3401
3402 ff_init_scantable(h->c.idsp.idct_permutation, &h->c.intra_scantable,
3403 h->c.alternate_scan ? ff_alternate_vertical_scan : ff_zigzag_direct);
3404
3406
3408 extension_and_user_data(&h->c, gb, 4);
3409
3410 return 0;
3411}
3412
3414{
3415 int visual_object_type;
3416
3417 skip_bits(gb, 4); /* visual_object_verid */
3418 visual_object_type = get_bits(gb, 4);
3419 if (visual_object_type != VOT_VIDEO_ID) {
3420 avpriv_request_sample(ctx->h.c.avctx, "VO type %u", visual_object_type);
3421 return AVERROR_PATCHWELCOME;
3422 }
3423
3425 extension_and_user_data(&ctx->h.c, gb, 1);
3426
3427 return 0;
3428}
3429
3430/**
3431 * Decode MPEG-4 headers.
3432 *
3433 * @param header If set the absence of a VOP is not treated as error; otherwise, it is treated as such.
3434 * @param parse_only If set, things only relevant to a decoder may be skipped;
3435 * furthermore, the VLC tables may be uninitialized.
3436 * @return <0 if an error occurred
3437 * FRAME_SKIPPED if a not coded VOP is found
3438 * 0 else
3439 */
3441 int header, int parse_only)
3442{
3443 MPVContext *const s = &ctx->h.c;
3444 unsigned startcode, v;
3445 int ret;
3446 int vol = 0;
3447
3448 /* search next start code */
3449 align_get_bits(gb);
3450
3451 // If we have not switched to studio profile than we also did not switch bps
3452 // that means something else (like a previous instance) outside set bps which
3453 // would be inconsistent with the correct state, thus reset it
3454 if (!s->studio_profile && s->avctx->bits_per_raw_sample != 8)
3455 s->avctx->bits_per_raw_sample = 0;
3456
3457 if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
3458 skip_bits(gb, 24);
3459 if (get_bits(gb, 8) == 0xF0)
3460 goto end;
3461 }
3462
3463 startcode = 0xff;
3464 for (;;) {
3465 if (get_bits_count(gb) >= gb->size_in_bits) {
3466 if (gb->size_in_bits == 8 &&
3467 (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
3468 av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
3469 return FRAME_SKIPPED; // divx bug
3470 } else if (header && get_bits_count(gb) == gb->size_in_bits) {
3471 return 0; // ordinary return value for parsing of extradata
3472 } else
3473 return AVERROR_INVALIDDATA; // end of stream
3474 }
3475
3476 /* use the bits after the test */
3477 v = get_bits(gb, 8);
3478 startcode = ((startcode << 8) | v) & 0xffffffff;
3479
3480 if ((startcode & 0xFFFFFF00) != 0x100)
3481 continue; // no startcode
3482
3483 if (s->avctx->debug & FF_DEBUG_STARTCODE) {
3484 const char *name;
3485 if (startcode <= 0x11F)
3486 name = "Video Object Start";
3487 else if (startcode <= 0x12F)
3488 name = "Video Object Layer Start";
3489 else if (startcode <= 0x13F)
3490 name = "Reserved";
3491 else if (startcode <= 0x15F)
3492 name = "FGS bp start";
3493 else if (startcode <= 0x1AF)
3494 name = "Reserved";
3495 else if (startcode == 0x1B0)
3496 name = "Visual Object Seq Start";
3497 else if (startcode == 0x1B1)
3498 name = "Visual Object Seq End";
3499 else if (startcode == 0x1B2)
3500 name = "User Data";
3501 else if (startcode == 0x1B3)
3502 name = "Group of VOP start";
3503 else if (startcode == 0x1B4)
3504 name = "Video Session Error";
3505 else if (startcode == 0x1B5)
3506 name = "Visual Object Start";
3507 else if (startcode == 0x1B6)
3508 name = "Video Object Plane start";
3509 else if (startcode == 0x1B7)
3510 name = "slice start";
3511 else if (startcode == 0x1B8)
3512 name = "extension start";
3513 else if (startcode == 0x1B9)
3514 name = "fgs start";
3515 else if (startcode == 0x1BA)
3516 name = "FBA Object start";
3517 else if (startcode == 0x1BB)
3518 name = "FBA Object Plane start";
3519 else if (startcode == 0x1BC)
3520 name = "Mesh Object start";
3521 else if (startcode == 0x1BD)
3522 name = "Mesh Object Plane start";
3523 else if (startcode == 0x1BE)
3524 name = "Still Texture Object start";
3525 else if (startcode == 0x1BF)
3526 name = "Texture Spatial Layer start";
3527 else if (startcode == 0x1C0)
3528 name = "Texture SNR Layer start";
3529 else if (startcode == 0x1C1)
3530 name = "Texture Tile start";
3531 else if (startcode == 0x1C2)
3532 name = "Texture Shape Layer start";
3533 else if (startcode == 0x1C3)
3534 name = "stuffing start";
3535 else if (startcode <= 0x1C5)
3536 name = "Reserved";
3537 else if (startcode <= 0x1FF)
3538 name = "System start";
3539 else
3540 av_unreachable("Unexpected startcode");
3541 av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X %s at %d\n",
3542 startcode, name, get_bits_count(gb));
3543 }
3544
3545 if (startcode >= 0x120 && startcode <= 0x12F) {
3546 if (vol) {
3547 av_log(s->avctx, AV_LOG_WARNING, "Ignoring multiple VOL headers\n");
3548 continue;
3549 }
3550 vol++;
3551 if ((ret = decode_vol_header(ctx, gb)) < 0)
3552 return ret;
3553 } else if (startcode == USER_DATA_STARTCODE) {
3554 decode_user_data(ctx, gb);
3555 } else if (startcode == GOP_STARTCODE) {
3557 } else if (startcode == VOS_STARTCODE) {
3558 int profile, level;
3561 (level > 0 && level < 9)) {
3562 s->studio_profile = 1;
3564 extension_and_user_data(s, gb, 0);
3565 } else if (s->studio_profile) {
3566 avpriv_request_sample(s->avctx, "Mix of studio and non studio profile");
3567 return AVERROR_PATCHWELCOME;
3568 }
3569 s->avctx->profile = profile;
3570 s->avctx->level = level;
3571 } else if (startcode == VISUAL_OBJ_STARTCODE) {
3572 if (s->studio_profile) {
3573 if ((ret = decode_studiovisualobject(ctx, gb)) < 0)
3574 return ret;
3575 } else
3577 } else if (startcode == VOP_STARTCODE) {
3578 break;
3579 }
3580
3581 align_get_bits(gb);
3582 startcode = 0xff;
3583 }
3584
3585end:
3586 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
3587 s->low_delay = 1;
3588
3589 if (s->studio_profile) {
3590 if (!s->avctx->bits_per_raw_sample) {
3591 av_log(s->avctx, AV_LOG_ERROR, "Missing VOL header\n");
3592 return AVERROR_INVALIDDATA;
3593 }
3594 return decode_studio_vop_header(ctx, gb);
3595 } else
3596 return decode_vop_header(ctx, gb, parse_only);
3597}
3598
3599#if CONFIG_MPEG4_DECODER
3600static av_cold void permute_quant_matrix(uint16_t matrix[64],
3601 const uint8_t new_perm[64],
3602 const uint8_t old_perm[64])
3603{
3604 uint16_t tmp[64];
3605
3606 memcpy(tmp, matrix, sizeof(tmp));
3607 for (int i = 0; i < 64; ++i)
3608 matrix[new_perm[i]] = tmp[old_perm[i]];
3609}
3610
3611static av_cold void switch_to_xvid_idct(AVCodecContext *const avctx,
3612 H263DecContext *const h)
3613{
3614 uint8_t old_permutation[64];
3615
3616 memcpy(old_permutation, h->c.idsp.idct_permutation, sizeof(old_permutation));
3617
3618 avctx->idct_algo = FF_IDCT_XVID;
3619 ff_mpv_idct_init(&h->c);
3620 ff_permute_scantable(h->permutated_intra_h_scantable,
3622 h->c.idsp.idct_permutation);
3623 ff_permute_scantable(h->permutated_intra_v_scantable, ff_alternate_vertical_scan,
3624 h->c.idsp.idct_permutation);
3625
3626 // Normal (i.e. non-studio) MPEG-4 does not use the chroma matrices.
3627 permute_quant_matrix(h->c.inter_matrix, h->c.idsp.idct_permutation, old_permutation);
3628 permute_quant_matrix(h->c.intra_matrix, h->c.idsp.idct_permutation, old_permutation);
3629}
3630
3632{
3633 Mpeg4DecContext *ctx = avctx->priv_data;
3634 H263DecContext *const h = &ctx->h;
3635
3636 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
3637 if (h->c.codec_tag == AV_RL32("XVID") ||
3638 h->c.codec_tag == AV_RL32("XVIX") ||
3639 h->c.codec_tag == AV_RL32("RMP4") ||
3640 h->c.codec_tag == AV_RL32("ZMP4") ||
3641 h->c.codec_tag == AV_RL32("SIPP"))
3642 ctx->xvid_build = 0;
3643 }
3644
3645 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
3646 if (h->c.codec_tag == AV_RL32("DIVX") && ctx->vo_type == 0 &&
3647 ctx->vol_control_parameters == 0)
3648 ctx->divx_version = 400; // divx 4
3649
3650 if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
3651 ctx->divx_version =
3652 ctx->divx_build = -1;
3653 }
3654
3655 if (h->c.workaround_bugs & FF_BUG_AUTODETECT) {
3656 if (h->c.codec_tag == AV_RL32("XVIX"))
3657 h->c.workaround_bugs |= FF_BUG_XVID_ILACE;
3658
3659 if (h->c.codec_tag == AV_RL32("UMP4"))
3660 h->c.workaround_bugs |= FF_BUG_UMP4;
3661
3662 if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
3663 h->c.workaround_bugs |= FF_BUG_QPEL_CHROMA;
3664
3665 if (ctx->divx_version > 502 && ctx->divx_build < 1814)
3666 h->c.workaround_bugs |= FF_BUG_QPEL_CHROMA2;
3667
3668 if (ctx->xvid_build <= 3U)
3669 h->padding_bug_score = 256 * 256 * 256 * 64;
3670
3671 if (ctx->xvid_build <= 1U)
3672 h->c.workaround_bugs |= FF_BUG_QPEL_CHROMA;
3673
3674 if (ctx->xvid_build <= 12U)
3675 h->c.workaround_bugs |= FF_BUG_EDGE;
3676
3677 if (ctx->xvid_build <= 32U)
3678 h->c.workaround_bugs |= FF_BUG_DC_CLIP;
3679
3680#define SET_QPEL_FUNC(postfix1, postfix2) \
3681 h->c.qdsp.put_ ## postfix1 = ff_put_ ## postfix2; \
3682 h->c.qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
3683 h->c.qdsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
3684
3685 if (ctx->lavc_build < 4653U)
3686 h->c.workaround_bugs |= FF_BUG_STD_QPEL;
3687
3688 if (ctx->lavc_build < 4655U)
3689 h->c.workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
3690
3691 if (ctx->lavc_build < 4670U)
3692 h->c.workaround_bugs |= FF_BUG_EDGE;
3693
3694 if (ctx->lavc_build <= 4712U)
3695 h->c.workaround_bugs |= FF_BUG_DC_CLIP;
3696
3697 if ((ctx->lavc_build&0xFF) >= 100) {
3698 if (ctx->lavc_build > 3621476 && ctx->lavc_build < 3752552 &&
3699 (ctx->lavc_build < 3752037 || ctx->lavc_build > 3752191) // 3.2.1+
3700 )
3701 h->c.workaround_bugs |= FF_BUG_IEDGE;
3702 }
3703
3704 if (ctx->divx_version >= 0)
3705 h->c.workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
3706 if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
3707 h->padding_bug_score = 256 * 256 * 256 * 64;
3708
3709 if (ctx->divx_version < 500U)
3710 h->c.workaround_bugs |= FF_BUG_EDGE;
3711
3712 if (ctx->divx_version >= 0)
3713 h->c.workaround_bugs |= FF_BUG_HPEL_CHROMA;
3714 }
3715
3716 if (h->c.workaround_bugs & FF_BUG_STD_QPEL) {
3717 SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
3718 SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
3719 SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
3720 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
3721 SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
3722 SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
3723
3724 SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
3725 SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
3726 SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
3727 SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
3728 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
3729 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
3730 }
3731
3732 if (avctx->debug & FF_DEBUG_BUGS)
3733 av_log(h->c.avctx, AV_LOG_DEBUG,
3734 "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
3735 h->c.workaround_bugs, ctx->lavc_build, ctx->xvid_build,
3736 ctx->divx_version, ctx->divx_build, h->divx_packed ? "p" : "");
3737
3738 if (ctx->xvid_build >= 0 &&
3739 avctx->idct_algo == FF_IDCT_AUTO && !h->c.studio_profile) {
3740 switch_to_xvid_idct(avctx, h);
3741 }
3742}
3743
3744static int mpeg4_decode_picture_header(H263DecContext *const h)
3745{
3747
3748 h->skipped_last_frame = 0;
3749
3750 if (ctx->bitstream_buffer) {
3751 int buf_size = get_bits_left(&h->gb) / 8U;
3752 int bitstream_buffer_size = ctx->bitstream_buffer->size;
3753 const uint8_t *buf = h->gb.buffer;
3754
3755 if (h->divx_packed) {
3756 for (int i = 0; i < buf_size - 3; i++) {
3757 if (buf[i] == 0 && buf[i+1] == 0 && buf[i+2] == 1) {
3758 if (buf[i+3] == 0xB0) {
3759 av_log(h->c.avctx, AV_LOG_WARNING, "Discarding excessive bitstream in packed xvid\n");
3760 bitstream_buffer_size = 0;
3761 }
3762 break;
3763 }
3764 }
3765 }
3766 ctx->bitstream_buffer->size = 0;
3767 if (bitstream_buffer_size && (h->divx_packed || buf_size <= MAX_NVOP_SIZE)) {// divx 5.01+/xvid frame reorder
3768 int ret = init_get_bits8(&h->gb, ctx->bitstream_buffer->data,
3769 bitstream_buffer_size);
3770 if (ret < 0)
3771 return ret;
3772 } else
3773 av_buffer_unref(&ctx->bitstream_buffer);
3774 }
3775
3776 return ff_mpeg4_parse_picture_header(ctx, &h->gb, 0, 0);
3777}
3778
3780{
3781 Mpeg4DecContext *ctx = avctx->priv_data;
3782 H263DecContext *const h = &ctx->h;
3783 int ret;
3784
3785 av_assert1(!ctx->bitstream_buffer || !ctx->bitstream_buffer->size);
3786
3787 /* divx 5.01+ bitstream reorder stuff */
3788 if (h->divx_packed) {
3789 int current_pos = ctx->bitstream_buffer && h->gb.buffer == ctx->bitstream_buffer->data ? 0 : (get_bits_count(&h->gb) >> 3);
3790 int startcode_found = 0;
3791 uint8_t *buf = pkt->data;
3792 int buf_size = pkt->size;
3793
3794 if (buf_size - current_pos > 7) {
3795
3796 int i;
3797 for (i = current_pos; i < buf_size - 4; i++)
3798
3799 if (buf[i] == 0 &&
3800 buf[i + 1] == 0 &&
3801 buf[i + 2] == 1 &&
3802 buf[i + 3] == 0xB6) {
3803 startcode_found = !(buf[i + 4] & 0x40);
3804 break;
3805 }
3806 }
3807
3808 if (startcode_found) {
3809 if (!ctx->showed_packed_warning) {
3810 av_log(h->c.avctx, AV_LOG_INFO, "Video uses a non-standard and "
3811 "wasteful way to store B-frames ('packed B-frames'). "
3812 "Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.\n");
3813 ctx->showed_packed_warning = 1;
3814 }
3815 ret = av_buffer_replace(&ctx->bitstream_buffer, pkt->buf);
3816 if (ret < 0)
3817 return ret;
3818
3819 ctx->bitstream_buffer->data = buf + current_pos;
3820 ctx->bitstream_buffer->size = buf_size - current_pos;
3821 }
3822 }
3823
3824 return 0;
3825}
3826
3827#if HAVE_THREADS
3829{
3830 memset(&s->buffer_pools, 0, sizeof(s->buffer_pools));
3831 memset(&s->next_pic, 0, sizeof(s->next_pic));
3832 memset(&s->last_pic, 0, sizeof(s->last_pic));
3833 memset(&s->cur_pic, 0, sizeof(s->cur_pic));
3834
3835 memset(s->thread_context, 0, sizeof(s->thread_context));
3836
3837 s->ac_val_base = NULL;
3838 s->ac_val = NULL;
3839 memset(&s->sc, 0, sizeof(s->sc));
3840
3841 s->p_field_mv_table_base = NULL;
3842 for (int i = 0; i < 2; i++)
3843 for (int j = 0; j < 2; j++)
3844 s->p_field_mv_table[i][j] = NULL;
3845
3846 s->dc_val_base = NULL;
3847 s->coded_block_base = NULL;
3848 s->mbintra_table = NULL;
3849 s->cbp_table = NULL;
3850 s->pred_dir_table = NULL;
3851
3852 s->mbskip_table = NULL;
3853
3854 s->er.error_status_table = NULL;
3855 s->er.er_temp_buffer = NULL;
3856 s->mb_index2xy = NULL;
3857
3858 s->context_initialized = 0;
3859 s->context_reinit = 0;
3860}
3861
3862static av_cold int update_mpvctx(MpegEncContext *s, const MpegEncContext *s1)
3863{
3864 AVCodecContext *avctx = s->avctx;
3865 // FIXME the following leads to a data race; instead copy only
3866 // the necessary fields.
3867 memcpy(s, s1, sizeof(*s));
3869
3870 s->avctx = avctx;
3871
3872 if (s1->context_initialized) {
3873 int err = ff_mpv_common_init(s);
3874 if (err < 0)
3875 return err;
3876 }
3877 return 0;
3878}
3879
3880static int mpeg4_update_thread_context(AVCodecContext *dst,
3881 const AVCodecContext *src)
3882{
3883 Mpeg4DecContext *s = dst->priv_data;
3884 const Mpeg4DecContext *s1 = src->priv_data;
3885 int init = s->h.c.context_initialized;
3886 int ret;
3887
3888 if (!init) {
3889 ret = update_mpvctx(&s->h.c, &s1->h.c);
3890 if (ret < 0)
3891 return ret;
3892 }
3893
3895 if (ret < 0)
3896 return ret;
3897
3898 // copy all the necessary fields explicitly
3899 s->time_increment_bits = s1->time_increment_bits;
3900 s->shape = s1->shape;
3901 s->vol_sprite_usage = s1->vol_sprite_usage;
3902 s->sprite_brightness_change = s1->sprite_brightness_change;
3903 s->sprite_warping_accuracy = s1->sprite_warping_accuracy;
3904 s->num_sprite_warping_points = s1->num_sprite_warping_points;
3905 s->h.data_partitioning = s1->h.data_partitioning;
3906 s->mpeg_quant = s1->mpeg_quant;
3907 s->rvlc = s1->rvlc;
3908 s->resync_marker = s1->resync_marker;
3909 s->t_frame = s1->t_frame;
3910 s->new_pred = s1->new_pred;
3911 s->enhancement_type = s1->enhancement_type;
3912 s->scalability = s1->scalability;
3913 s->intra_dc_threshold = s1->intra_dc_threshold;
3914 s->h.divx_packed = s1->h.divx_packed;
3915 s->divx_version = s1->divx_version;
3916 s->divx_build = s1->divx_build;
3917 s->xvid_build = s1->xvid_build;
3918 s->lavc_build = s1->lavc_build;
3919 s->vo_type = s1->vo_type;
3920 s->showed_packed_warning = s1->showed_packed_warning;
3921 s->vol_control_parameters = s1->vol_control_parameters;
3922 s->cplx_estimation_trash_i = s1->cplx_estimation_trash_i;
3923 s->cplx_estimation_trash_p = s1->cplx_estimation_trash_p;
3924 s->cplx_estimation_trash_b = s1->cplx_estimation_trash_b;
3925 s->rgb = s1->rgb;
3926 s->h.c.studio_profile = s1->h.c.studio_profile;
3927
3928 s->h.skipped_last_frame = s1->h.skipped_last_frame;
3929 s->h.padding_bug_score = s1->h.padding_bug_score; // FIXME: racy
3930
3931 s->h.picture_number = s1->h.picture_number;
3932
3933 memcpy(s->sprite_shift, s1->sprite_shift, sizeof(s1->sprite_shift));
3934 memcpy(s->sprite_traj, s1->sprite_traj, sizeof(s1->sprite_traj));
3935
3936 return av_buffer_replace(&s->bitstream_buffer, s1->bitstream_buffer);
3937}
3938
3939static int mpeg4_update_thread_context_for_user(AVCodecContext *dst,
3940 const AVCodecContext *src)
3941{
3942 H263DecContext *const h = dst->priv_data;
3943 const H263DecContext *const h1 = src->priv_data;
3944
3945 h->c.quarter_sample = h1->c.quarter_sample;
3946 h->divx_packed = h1->divx_packed;
3947
3948 return 0;
3949}
3950#endif
3951
3952static av_cold void mpeg4_init_static(void)
3953{
3954 static VLCElem vlc_buf[6498];
3956
3958 &ff_mpeg4_studio_dc_luma[0][1], 2,
3959 &ff_mpeg4_studio_dc_luma[0][0], 2, 1,
3960 0, 0);
3961
3963 &ff_mpeg4_studio_dc_chroma[0][1], 2,
3964 &ff_mpeg4_studio_dc_chroma[0][0], 2, 1,
3965 0, 0);
3966
3967 for (unsigned i = 0; i < 12; i++) {
3970 &ff_mpeg4_studio_intra[i][0][1], 2,
3971 &ff_mpeg4_studio_intra[i][0][0], 2, 1,
3972 0, 0);
3973 }
3974
3975 static uint8_t mpeg4_rl_intra_table[2][2 * MAX_RUN + MAX_LEVEL + 3];
3976 ff_rl_init(&ff_mpeg4_rl_intra, mpeg4_rl_intra_table);
3978
3983 &ff_mpeg4_DCtab_lum[0][1], 2, 1,
3984 &ff_mpeg4_DCtab_lum[0][0], 2, 1, 0);
3986 &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
3987 &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 0);
3990 NULL, 0, 0, 0, 0);
3992 &ff_mb_type_b_tab[0][1], 2, 1,
3993 &ff_mb_type_b_tab[0][0], 2, 1,
3994 mb_type_b_map, 2, 2, 0);
3995}
3996
3997static av_cold int decode_init(AVCodecContext *avctx)
3998{
3999 static AVOnce init_static_once = AV_ONCE_INIT;
4000 Mpeg4DecContext *ctx = avctx->priv_data;
4001 H263DecContext *const h = &ctx->h;
4002 MPVUnquantDSPContext unquant_dsp_ctx;
4003 int ret;
4004
4005 ctx->divx_version =
4006 ctx->divx_build =
4007 ctx->xvid_build =
4008 ctx->lavc_build = -1;
4009
4010 if ((ret = ff_h263_decode_init(avctx)) < 0)
4011 return ret;
4012
4013 ff_mpv_unquantize_init(&unquant_dsp_ctx,
4014 avctx->flags & AV_CODEC_FLAG_BITEXACT, 0);
4015
4016 ctx->dct_unquantize_h263_intra = unquant_dsp_ctx.dct_unquantize_h263_intra;
4017 ctx->dct_unquantize_mpeg2_intra = unquant_dsp_ctx.dct_unquantize_mpeg2_intra;
4018 // dct_unquantize_inter is only used with MPEG-2 quantizers,
4019 // so that is all we keep.
4020 ctx->dct_unquantize_mpeg2_inter = unquant_dsp_ctx.dct_unquantize_mpeg2_inter;
4021
4022 h->c.y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
4023 h->c.c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
4024
4025 h->c.h263_pred = 1;
4026 h->c.low_delay = 0; /* default, might be overridden in the vol header during header parsing */
4027 h->decode_header = mpeg4_decode_picture_header;
4028 h->decode_mb = mpeg4_decode_mb;
4029 ctx->time_increment_bits = 4; /* default value for broken headers */
4030 ctx->quant_precision = 5;
4031
4033
4034 ff_qpeldsp_init(&h->c.qdsp);
4035 ff_mpeg4videodsp_init(&ctx->mdsp);
4036
4037 ff_thread_once(&init_static_once, mpeg4_init_static);
4038
4039 /* Must be after initializing the MPEG-4 static tables */
4040 if (avctx->extradata_size && !avctx->internal->is_copy) {
4041 GetBitContext gb;
4042
4043 if (init_get_bits8(&gb, avctx->extradata, avctx->extradata_size) >= 0)
4045 }
4046
4047 return 0;
4048}
4049
4050static av_cold void mpeg4_flush(AVCodecContext *avctx)
4051{
4052 Mpeg4DecContext *const ctx = avctx->priv_data;
4053
4054 av_buffer_unref(&ctx->bitstream_buffer);
4055 ff_mpeg_flush(avctx);
4056}
4057
4058static av_cold int mpeg4_close(AVCodecContext *avctx)
4059{
4060 Mpeg4DecContext *const ctx = avctx->priv_data;
4061
4062 av_buffer_unref(&ctx->bitstream_buffer);
4063
4064 return ff_mpv_decode_close(avctx);
4065}
4066
4067#define OFFSET(x) offsetof(H263DecContext, x)
4068#define FLAGS AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY
4069static const AVOption mpeg4_options[] = {
4070 {"quarter_sample", "1/4 subpel MC", OFFSET(c.quarter_sample), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS},
4071 {"divx_packed", "divx style packed b frames", OFFSET(divx_packed), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS},
4072 {NULL}
4073};
4074
4075static const AVClass mpeg4_class = {
4076 .class_name = "MPEG4 Video Decoder",
4077 .item_name = av_default_item_name,
4078 .option = mpeg4_options,
4079 .version = LIBAVUTIL_VERSION_INT,
4080};
4081
4082const FFCodec ff_mpeg4_decoder = {
4083 .p.name = "mpeg4",
4084 CODEC_LONG_NAME("MPEG-4 part 2"),
4085 .p.type = AVMEDIA_TYPE_VIDEO,
4086 .p.id = AV_CODEC_ID_MPEG4,
4087 .priv_data_size = sizeof(Mpeg4DecContext),
4088 .init = decode_init,
4090 .close = mpeg4_close,
4093 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
4095 .flush = mpeg4_flush,
4096 .p.max_lowres = 3,
4098 UPDATE_THREAD_CONTEXT(mpeg4_update_thread_context),
4099 UPDATE_THREAD_CONTEXT_FOR_USER(mpeg4_update_thread_context_for_user),
4100 .p.priv_class = &mpeg4_class,
4101 .hw_configs = (const AVCodecHWConfigInternal *const []) {
4102#if CONFIG_MPEG4_NVDEC_HWACCEL
4103 HWACCEL_NVDEC(mpeg4),
4104#endif
4105#if CONFIG_MPEG4_NVDEC_CUARRAY_HWACCEL
4106 HWACCEL_NVDEC_CUARRAY(mpeg4),
4107#endif
4108#if CONFIG_MPEG4_VAAPI_HWACCEL
4109 HWACCEL_VAAPI(mpeg4),
4110#endif
4111#if CONFIG_MPEG4_VDPAU_HWACCEL
4112 HWACCEL_VDPAU(mpeg4),
4113#endif
4114#if CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
4115 HWACCEL_VIDEOTOOLBOX(mpeg4),
4116#endif
4117 NULL
4118 },
4119};
4120#endif /* CONFIG_MPEG4_DECODER */
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t my
Definition dsp.h:57
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t mx
Definition dsp.h:57
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
#define wrap(func)
Definition neontest.h:65
const FFCodec ff_mpeg4_decoder
static AVFormatContext * ctx
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition avassert.h:109
#define FF_BUG_EDGE
Definition avcodec.h:1355
#define FF_DEBUG_STARTCODE
Definition avcodec.h:1400
#define FF_BUG_XVID_ILACE
Definition avcodec.h:1347
#define FF_BUG_DC_CLIP
Definition avcodec.h:1357
#define FF_BUG_HPEL_CHROMA
Definition avcodec.h:1356
#define FF_BUG_QPEL_CHROMA2
Definition avcodec.h:1353
#define FF_BUG_DIRECT_BLOCKSIZE
Definition avcodec.h:1354
#define FF_DEBUG_PICT_INFO
Definition avcodec.h:1393
#define FF_BUG_AMV
Definition avcodec.h:1350
#define FF_BUG_UMP4
Definition avcodec.h:1348
#define FF_IDCT_AUTO
Definition avcodec.h:1545
#define FF_BUG_AUTODETECT
autodetection
Definition avcodec.h:1346
#define FF_DEBUG_BUGS
Definition avcodec.h:1403
#define FF_IDCT_XVID
Definition avcodec.h:1552
#define FF_BUG_IEDGE
Definition avcodec.h:1360
#define FF_BUG_STD_QPEL
Definition avcodec.h:1352
#define FF_BUG_NO_PADDING
Definition avcodec.h:1349
#define FF_BUG_QPEL_CHROMA
Definition avcodec.h:1351
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define UPDATE_THREAD_CONTEXT(func)
#define UPDATE_THREAD_CONTEXT_FOR_USER(func)
#define FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM
The decoder extracts and fills its parameters even if the frame is skipped due to the skip_frame sett...
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define av_clip
Definition common.h:100
#define ROUNDED_DIV(a, b)
Definition common.h:58
#define RSHIFT(a, b)
Definition common.h:56
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define abs(x)
#define min(a, b)
#define max(a, b)
static int16_t block[64]
Definition dct.c:125
#define AV_EF_BITSTREAM
detect bitstream specification deviations
Definition defs.h:49
#define AV_PROFILE_UNKNOWN
Definition defs.h:65
#define AV_PROFILE_MPEG4_SIMPLE_STUDIO
Definition defs.h:145
#define AV_EF_COMPLIANT
consider all spec non compliances as errors
Definition defs.h:55
#define AV_EF_IGNORE_ERR
ignore errors and continue
Definition defs.h:53
#define AV_EF_AGGRESSIVE
consider things that a sane encoder/muxer should not do as an error
Definition defs.h:56
static AVPacket * pkt
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define TEX_VLC_BITS
Definition dvdec.c:146
void ff_er_add_slice(ERContext *s, int startx, int starty, int endx, int endy, int status)
Add a slice.
#define ER_DC_ERROR
#define ER_MV_ERROR
#define ER_MV_END
#define ER_DC_END
static struct @346255127015250356166251341105367306144006377143 state
static const uint8_t bits[8]
Definition fastaudio.c:100
#define OFFSET(x)
static int lowres
Definition ffplay.c:333
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition get_bits.h:498
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition get_bits.h:424
#define GET_CACHE(name, gb)
Definition get_bits.h:251
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition get_bits.h:645
#define SKIP_CACHE(name, gb, num)
Definition get_bits.h:216
#define CLOSE_READER(name, gb)
Definition get_bits.h:189
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
#define SHOW_UBITS(name, gb, num)
Definition get_bits.h:247
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
#define SHOW_SBITS(name, gb, num)
Definition get_bits.h:248
#define OPEN_READER(name, gb)
Definition get_bits.h:182
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
#define SKIP_BITS(name, gb, num)
Definition get_bits.h:229
#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth, need_update)
Definition get_bits.h:602
#define UPDATE_CACHE(name, gb)
Definition get_bits.h:213
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
#define LAST_SKIP_BITS(name, gb, num)
Definition get_bits.h:235
static const uint8_t * align_get_bits(GetBitContext *s)
Definition get_bits.h:560
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static void skip_bits1(GetBitContext *s)
Definition get_bits.h:416
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define SKIP_COUNTER(name, gb, num)
Definition get_bits.h:223
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition get_bits.h:373
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
Definition get_bits.h:353
static int get_xbits(GetBitContext *s, int n)
Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
Definition get_bits.h:294
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AV_CODEC_CAP_DRAW_HORIZ_BAND
Decoder can use draw_horiz_band callback.
Definition codec.h:41
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition avcodec.h:302
#define AV_CODEC_FLAG_LOW_DELAY
Force low delay.
Definition avcodec.h:314
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:139
int av_buffer_replace(AVBufferRef **pdst, const AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition buffer.c:233
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
@ AV_PICTURE_TYPE_S
S(GMC)-VOP MPEG-4.
Definition avutil.h:281
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition avutil.h:280
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
Definition h263.c:182
static void ff_h263_clean_intra_table_entries(MpegEncContext *s, int xy)
Definition h263.h:47
#define FF_ASPECT_EXTENDED
Definition h263.h:26
void ff_h263_init_rl_inter(void)
RLTable ff_h263_rl_inter
Definition h263data.c:159
const AVRational ff_h263_pixel_aspect[16]
Definition h263data.c:273
H.263 tables.
VLCElem ff_h263_inter_MCBPC_vlc[]
Definition ituh263dec.c:103
int ff_h263_decode_motion(H263DecContext *const h, int pred, int f_code)
Definition ituh263dec.c:279
#define CBPY_VLC_BITS
Definition h263dec.h:35
#define SLICE_NOEND
no end marker or error found but mb count exceeded
Definition h263dec.h:96
VLCElem ff_h263_cbpy_vlc[]
Definition ituh263dec.c:104
#define INTRA_MCBPC_VLC_BITS
Definition h263dec.h:33
#define FRAME_SKIPPED
Frame is not coded.
Definition h263dec.h:90
VLCElem ff_h263_intra_MCBPC_vlc[]
Definition ituh263dec.c:102
#define INTER_MCBPC_VLC_BITS
Definition h263dec.h:34
int a
#define HWACCEL_NVDEC_CUARRAY(codec)
Definition hwconfig.h:70
#define HWACCEL_VDPAU(codec)
Definition hwconfig.h:74
#define HWACCEL_NVDEC(codec)
Definition hwconfig.h:68
#define HWACCEL_VAAPI(codec)
Definition hwconfig.h:72
#define HWACCEL_VIDEOTOOLBOX(codec)
Definition hwconfig.h:76
static const int16_t alpha[]
Definition ilbcdata.h:55
#define r
Definition input.c:42
#define b
Definition input.c:43
#define av_log2
Definition intmath.h:84
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
#define DC_VLC_BITS
Definition intrax8.c:41
#define AV_RL32(p)
unsigned offset
Definition libaomenc.c:763
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static int shift(int a, int b)
Definition bonk.c:261
#define SLICE_END
end marker found
Definition h261dec.c:42
#define SLICE_OK
Definition h261dec.c:40
av_cold int ff_h263_decode_init(AVCodecContext *avctx)
Definition h263dec.c:94
int ff_h263_decode_frame(AVCodecContext *avctx, AVFrame *pict, int *got_frame, AVPacket *avpkt)
Definition h263dec.c:443
av_cold void ff_permute_scantable(uint8_t dst[64], const uint8_t src[64], const uint8_t permutation[64])
Definition idctdsp.c:30
common internal api header.
av_cold void ff_mpeg4videodsp_init(Mpeg4VideoDSPContext *c)
av_cold void ff_qpeldsp_init(QpelDSPContext *c)
Definition qpeldsp.c:784
#define av_uninit(x)
Definition attributes.h:187
#define av_cold
Definition attributes.h:117
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define AVOnce
Definition thread.h:202
static int ff_thread_once(char *control, void(*routine)(void))
Definition thread.h:205
#define AV_ONCE_INIT
Definition thread.h:203
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
const uint8_t ff_zigzag_direct[64]
Definition mathtables.c:137
#define FASTDIV(a, b)
Definition mathops.h:216
static const VLCElem * rl_vlc[2]
Definition mobiclip.c:279
const uint8_t ff_sprite_trajectory_lens[15]
Definition mpeg4data.h:325
const uint8_t ff_mpeg4_DCtab_lum[13][2]
Definition mpeg4data.h:34
const uint8_t ff_mpeg4_studio_intra[12][24][2]
Definition mpeg4data.h:384
RLTable ff_mpeg4_rl_intra
Definition mpeg4data.h:108
const uint8_t ff_mb_type_b_tab[4][2]
Definition mpeg4data.h:329
const int16_t ff_mpeg4_default_intra_matrix[64]
Definition mpeg4data.h:334
const uint8_t ff_mpeg4_c_dc_scale_table[32]
Definition mpeg4data.h:360
const uint8_t ff_mpeg4_y_dc_scale_table[32]
Definition mpeg4data.h:356
const uint8_t ff_mpeg4_DCtab_chrom[13][2]
Definition mpeg4data.h:40
const uint8_t ff_mpeg4_studio_dc_chroma[19][2]
Definition mpeg4data.h:377
const int16_t ff_mpeg4_default_non_intra_matrix[64]
Definition mpeg4data.h:345
RLTable ff_rvlc_rl_intra
Definition mpeg4data.h:317
RLTable ff_rvlc_rl_inter
Definition mpeg4data.h:213
const uint8_t ff_mpeg4_studio_dc_luma[19][2]
Definition mpeg4data.h:370
const uint8_t ff_mpeg4_dc_threshold[8]
Definition mpeg4data.h:365
void ff_mpeg4_init_direct_mv(MpegEncContext *s)
Definition mpeg4video.c:73
int ff_mpeg4_get_video_packet_prefix_length(enum AVPictureType pict_type, int f_code, int b_code)
Definition mpeg4video.c:28
int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my)
Definition mpeg4video.c:119
static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb, int *profile, int *level)
static int mpeg4_decode_studio_mb(H263DecContext *const h)
static VLCElem sprite_trajectory[128]
static VLCElem dc_lum[512]
static VLCElem mb_type_b_vlc[16]
static int mpeg4_decode_dc(H263DecContext *const h, int n, int *dir_ptr)
Decode the dc value.
void ff_mpeg4_decode_studio(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int block_size, int uvlinesize, int dct_linesize, int dct_offset)
#define STUDIO_INTRA_BITS
static const int16_t mb_type_b_map[4]
#define IS_3IV1
static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb)
static int read_quant_matrix_ext(MpegEncContext *s, GetBitContext *gb)
static void decode_smpte_tc(Mpeg4DecContext *ctx, GetBitContext *gb)
static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
Decode first partition.
static int decode_studio_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
Decode the next studio vop header.
static int mpeg4_decode_partitioned_mb(H263DecContext *const h)
decode partition C of one MB.
static const uint8_t ac_state_tab[22][2]
static Mpeg4DecContext * h263_to_mpeg4(H263DecContext *h)
int ff_mpeg4_decode_partitions(H263DecContext *const h)
Decode the first and second partition.
#define MB_TYPE_B_VLC_BITS
int ff_mpeg4_parse_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb, int header, int parse_only)
Decode MPEG-4 headers.
static int mpeg4_decode_visual_object(MpegEncContext *s, GetBitContext *gb)
static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
static void extension_and_user_data(MpegEncContext *s, GetBitContext *gb, int id)
int ff_mpeg4_decode_video_packet_header(H263DecContext *const h)
Decode the next video packet.
static int mpeg4_decode_mb(H263DecContext *const h)
static void next_start_code_studio(GetBitContext *gb)
static VLCElem studio_luma_dc[528]
static VLCElem studio_chroma_dc[528]
static int mpeg4_pred_dc(MpegEncContext *s, int n, int *dir_ptr)
Predict the dc.
static void gmc1_motion(MpegEncContext *s, const Mpeg4DecContext *ctx, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, uint8_t *const *ref_picture)
static const uint8_t mpeg4_block_count[4]
#define SPRITE_TRAJ_VLC_BITS
static int mpeg4_decode_dpcm_macroblock(Mpeg4DecContext *const ctx, int16_t macroblock[256], int n)
static int decode_studio_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb, int parse_only)
void ff_mpeg4_pred_ac(H263DecContext *const h, int16_t *block, int n, int dir)
Predict the ac.
static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
static int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block, int n, int coded, int intra, int use_intra_dc_vlc, int rvlc)
Decode a block.
static void mpeg4_load_default_matrices(MpegEncContext *s)
static void gmc_motion(MpegEncContext *s, const Mpeg4DecContext *ctx, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, uint8_t *const *ref_picture)
int ff_mpeg4_decode_studio_slice_header(H263DecContext *const h)
Decode the next video packet.
void ff_mpeg4_mcsel_motion(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, uint8_t *const *ref_picture)
static int decode_studiovisualobject(Mpeg4DecContext *ctx, GetBitContext *gb)
static const VLCElem * studio_intra_tab[12]
static int mpeg4_decode_studio_block(Mpeg4DecContext *const ctx, int32_t block[64], int n)
static int mpeg4_decode_partition_b(H263DecContext *const h, int mb_count)
decode second partition.
static VLCElem dc_chrom[512]
static int mpeg4_get_level_dc(MpegEncContext *s, int n, int pred, int level)
static void reset_studio_dc_predictors(Mpeg4DecContext *const ctx)
static int mpeg4_is_resync(Mpeg4DecContext *ctx)
check if the next stuff is a resync marker or the end.
static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
Decode the user data stuff in the header.
static int get_amv(Mpeg4DecContext *ctx, int n)
Get the average motion vector for a GMC MB.
int ff_mpeg4_frame_end(AVCodecContext *avctx, const AVPacket *pkt)
void ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
#define QUANT_MATRIX_EXT_ID
#define ADV_SIMPLE_VO_TYPE
#define VOT_STILL_TEXTURE_ID
#define GRAY_SHAPE
#define USER_DATA_STARTCODE
#define VOT_VIDEO_ID
#define STATIC_SPRITE
#define SIMPLE_STUDIO_VO_TYPE
#define EXT_STARTCODE
#define GMC_SPRITE
#define SIMPLE_VO_TYPE
#define DC_MARKER
#define MOTION_MARKER
#define RECT_SHAPE
#define VOS_STARTCODE
#define CORE_STUDIO_VO_TYPE
#define SLICE_STARTCODE
#define MAX_NVOP_SIZE
#define VOP_STARTCODE
#define GOP_STARTCODE
#define BIN_ONLY_SHAPE
#define VISUAL_OBJ_STARTCODE
#define MB_TYPE_8x8
Definition mpegutils.h:44
#define MB_TYPE_GMC
Definition mpegutils.h:60
#define IS_ACPRED(a)
Definition mpegutils.h:84
#define MB_TYPE_BACKWARD_MV
Definition mpegutils.h:50
#define HAS_BACKWARD_MV(a)
Definition mpegutils.h:89
#define IS_DIRECT(a)
Definition mpegutils.h:78
#define MB_TYPE_SKIP
Definition mpegutils.h:61
#define MB_TYPE_INTRA
Definition mpegutils.h:64
#define MB_TYPE_DIRECT2
Definition mpegutils.h:46
#define MB_TYPE_16x8
Definition mpegutils.h:42
#define MB_TYPE_BIDIR_MV
Definition mpegutils.h:51
#define HAS_FORWARD_MV(a)
Definition mpegutils.h:88
#define IS_8X8(a)
Definition mpegutils.h:83
#define MB_TYPE_INTERLACED
Definition mpegutils.h:45
#define MB_TYPE_16x16
Definition mpegutils.h:41
#define MB_TYPE_ACPRED
Definition mpegutils.h:62
#define MB_TYPE_FORWARD_MV
Definition mpegutils.h:49
#define IS_SKIP(a)
Definition mpegutils.h:75
av_cold int ff_mpv_common_init(MpegEncContext *s)
init common structure for both encoder and decoder.
Definition mpegvideo.c:359
av_cold void ff_mpv_idct_init(MpegEncContext *s)
Definition mpegvideo.c:81
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
Definition mpegvideo.c:505
void ff_init_block_index(MpegEncContext *s)
Definition mpegvideo.c:472
mpegvideo header.
#define MV_DIR_BACKWARD
Definition mpegvideo.h:169
static void ff_update_block_index(MpegEncContext *s, int bits_per_raw_sample, int lowres, int chroma_x_shift)
Definition mpegvideo.h:335
#define MV_DIR_FORWARD
Definition mpegvideo.h:168
#define MV_TYPE_FIELD
2 vectors, one per field
Definition mpegvideo.h:175
#define MV_TYPE_8X8
4 vectors (H.263, MPEG-4 4MV)
Definition mpegvideo.h:173
void ff_init_scantable(const uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
#define CHROMA_420
Definition mpegvideo.h:264
#define MV_DIRECT
bidirectional mode where the difference equals the MV of the last P/S/I-Frame (MPEG-4)
Definition mpegvideo.h:170
#define MV_TYPE_16X16
1 vector for the whole mb
Definition mpegvideo.h:172
#define CHROMA_422
Definition mpegvideo.h:265
av_cold void ff_mpeg_flush(AVCodecContext *avctx)
av_cold int ff_mpv_decode_close(AVCodecContext *avctx)
int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
update_thread_context for mpegvideo-based decoders.
#define ff_mpv_unquantize_init(s, bitexact, q_scale_type)
const uint8_t ff_alternate_horizontal_scan[64]
const uint8_t ff_alternate_vertical_scan[64]
mpegvideo decoder header.
static int mpeg_get_qscale(GetBitContext *const gb, int q_scale_type)
static int check_marker(void *logctx, GetBitContext *s, const char *msg)
int profile
Definition mxfenc.c:2299
AVOptions.
@ AVCHROMA_LOC_LEFT
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition pixfmt.h:804
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:783
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:564
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
#define IS_INTRA(x, y)
const AVProfile ff_mpeg4_video_profiles[]
Definition profiles.c:127
quarterpel DSP functions
#define SET_QPEL_FUNC(OP, X, Y, SIZE, CPU, PREFIX)
const char * name
Definition qsvenc.c:142
av_cold void ff_rl_init(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Initialize index_run, max_level and max_run from n, last, table_vlc, table_run and table_level.
Definition rl.c:43
#define VLC_INIT_RL(rl, static_size)
Definition rl.h:83
#define MAX_LEVEL
Definition rl.h:36
#define MAX_RUN
Definition rl.h:35
#define INIT_FIRST_VLC_RL(rl, static_size)
Definition rl.h:93
static const uint8_t header[24]
Definition sdr2.c:68
static const float pred[4]
Definition siprdata.h:259
const uint8_t * code
Definition spdifenc.c:433
unsigned int pos
Definition spdifenc.c:431
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
int debug
debug
Definition avcodec.h:1392
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition avcodec.h:1544
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition avcodec.h:688
int extradata_size
Definition avcodec.h:527
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
void * priv_data
Definition avcodec.h:470
int is_copy
When using frame-threaded decoding, this field is set for the first worker thread (e....
Definition internal.h:54
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
MPVContext c
Definition h263dec.h:44
int divx_packed
divx specific, used to workaround (many) bugs in divx5
Definition h263dec.h:75
int picture_number
Definition h263dec.h:50
int padding_bug_score
used to detect the VERY common padding bug in MPEG-4
Definition h263dec.h:73
int data_partitioning
data partitioning flag from header
Definition h263dec.h:76
int skipped_last_frame
Definition h263dec.h:74
void(* dct_unquantize_mpeg2_intra)(const MPVContext *s, int16_t *block, int n, int qscale)
void(* dct_unquantize_mpeg2_inter)(const MPVContext *s, int16_t *block, int n, int qscale)
void(* dct_unquantize_h263_intra)(const MPVContext *s, int16_t *block, int n, int qscale)
int intra_dc_threshold
QP above which the ac VLC should be used for intra dc.
int time_increment_bits
number of bits to represent the fractional part of time
int num_sprite_warping_points
int showed_packed_warning
flag for having shown the warning about invalid Divx B-frames
int sprite_shift[2]
sprite shift [isChroma]
H263DecContext h
int vol_control_parameters
does the stream contain the low_delay flag, used to work around buggy encoders.
uint16_t sprite_traj[4][2]
sprite trajectory points
int t_frame
time distance of first I -> B, used for interlaced B-frames
AVBufferRef * bitstream_buffer
Divx 5.01 puts several frames in a single one, this is used to reorder them.
int resync_marker
could this stream contain resync markers
MpegEncContext.
Definition mpegvideo.h:67
int context_initialized
Definition mpegvideo.h:95
RLTable.
Definition rl.h:39
int8_t * max_run[2]
encoding & decoding
Definition rl.h:47
int8_t * max_level[2]
encoding & decoding
Definition rl.h:46
Definition vlc.h:32
For static VLCs, the number of bits can often be hardcoded at each get_vlc2() callsite.
Definition vlc.h:220
Definition rpzaenc.c:60
uint8_t run
Definition svq3.c:207
uint8_t level
Definition svq3.c:208
#define stride
static void clear_context(SwrContext *s)
Definition swresample.c:111
#define ff_dlog(a,...)
#define avpriv_request_sample(...)
#define ff_tlog(a,...)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
void ff_thread_progress_await(const ThreadProgress *pro_c, int n)
This function is a no-op in no-op mode; otherwise it waits until other threads have reached a certain...
static int64_t pts
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition unary.h:46
static const AVOption mpeg4_options[]
static void hsub(htype *dst, const htype *src, int bins)
Definition vf_median.c:74
av_cold const VLCElem * ff_vlc_init_tables_from_lengths(VLCInitState *state, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags)
Definition vlc.c:366
#define VLC_INIT_STATIC_TABLE(vlc_table, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, flags)
Definition vlc.h:278
#define VLC_INIT_STATE(_table)
Definition vlc.h:225
#define VLC_INIT_STATIC_TABLE_FROM_LENGTHS(vlc_table, nb_bits, nb_codes, lens, lens_wrap, syms, syms_wrap, syms_size, offset, flags)
Definition vlc.h:288
#define VLC_INIT_STATIC_SPARSE_TABLE(vlc_table, nb_bits, nb_codes, bits, bits_wrap, bits_size, codes, codes_wrap, codes_size, symbols, symbols_wrap, symbols_size, flags)
Definition vlc.h:266
VLCElem RL_VLC_ELEM
Definition vlc.h:48
float delta
int len
static double c[64]