FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg4videodec.c
Date: 2025-07-28 20:30:09
Exec Total Coverage
Lines: 1432 2421 59.1%
Functions: 44 53 83.0%
Branches: 788 1581 49.8%

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