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