FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg4videodec.c
Date: 2026-09-25 11:37:27
Exec Total Coverage
Lines: 1436 2426 59.2%
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 1536203 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 1536203 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 1371246 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 1371246 int8_t *const qscale_table = h->c.cur_pic.qscale_table;
333
334 /* find prediction */
335 1371246 ac_val = &h->c.ac_val[0][0] + h->c.block_index[n] * 16;
336 1371246 ac_val1 = ac_val;
337
2/2
✓ Branch 0 taken 30084 times.
✓ Branch 1 taken 1341162 times.
1371246 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 9598722 times.
✓ Branch 1 taken 1371246 times.
10969968 for (i = 1; i < 8; i++)
372 9598722 ac_val1[i] = block[h->c.idsp.idct_permutation[i << 3]];
373
374 /* top copy */
375
2/2
✓ Branch 0 taken 9598722 times.
✓ Branch 1 taken 1371246 times.
10969968 for (i = 1; i < 8; i++)
376 9598722 ac_val1[8 + i] = block[h->c.idsp.idct_permutation[i]];
377 1371246 }
378
379 /**
380 * check if the next stuff is a resync marker or the end.
381 * @return 0 if not
382 */
383 1525604 static inline int mpeg4_is_resync(Mpeg4DecContext *ctx)
384 {
385 1525604 H263DecContext *const h = &ctx->h;
386 1525604 int bits_count = get_bits_count(&h->gb);
387 1525604 int v = show_bits(&h->gb, 16);
388
389
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1525604 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1525604 if (h->c.workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker)
390 ✗ return 0;
391
392
2/2
✓ Branch 0 taken 4383 times.
✓ Branch 1 taken 1521221 times.
1525604 while (v <= 0xFF) {
393
2/2
✓ Branch 0 taken 3397 times.
✓ Branch 1 taken 986 times.
4383 if (h->c.pict_type == AV_PICTURE_TYPE_B ||
394
3/4
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 3221 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 176 times.
3397 (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 11806 times.
✓ Branch 1 taken 1513798 times.
1525604 if (bits_count + 8 >= h->gb.size_in_bits) {
402 11806 v >>= 8;
403 11806 v |= 0x7F >> (7 - (bits_count & 7));
404
405
2/2
✓ Branch 0 taken 11441 times.
✓ Branch 1 taken 365 times.
11806 if (v == 0x7F)
406 11441 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 1509660 times.
1513798 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 1510027 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 H263DecContext *const h = &ctx->h;
798 /* Reset DC Predictors */
799 30 h->last_dc[0] =
800 30 h->last_dc[1] =
801 30 h->last_dc[2] = 1 << (h->c.avctx->bits_per_raw_sample + ctx->dct_precision + h->c.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 1140534 static inline int mpeg4_pred_dc(MpegEncContext *s, int n, int *dir_ptr)
903 {
904 1140534 const int16_t *const dc_val = s->dc_val + s->block_index[n];
905 1140534 const int wrap = s->block_wrap[n];
906 int pred;
907
908 /* find prediction */
909
910 /* B C
911 * A X
912 */
913 1140534 int a = dc_val[-1];
914 1140534 int b = dc_val[-1 - wrap];
915 1140534 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 131454 times.
✓ Branch 1 taken 1009080 times.
✓ Branch 2 taken 109545 times.
✓ Branch 3 taken 21909 times.
1140534 if (s->first_slice_line && n != 3) {
920
2/2
✓ Branch 0 taken 87636 times.
✓ Branch 1 taken 21909 times.
109545 if (n != 2)
921 87636 b = c = 1024;
922
4/4
✓ Branch 0 taken 87636 times.
✓ Branch 1 taken 21909 times.
✓ Branch 2 taken 4372 times.
✓ Branch 3 taken 83264 times.
109545 if (n != 1 && s->mb_x == s->resync_mb_x)
923 4372 b = a = 1024;
924 }
925
4/4
✓ Branch 0 taken 46188 times.
✓ Branch 1 taken 1094346 times.
✓ Branch 2 taken 6276 times.
✓ Branch 3 taken 39912 times.
1140534 if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1) {
926
6/6
✓ Branch 0 taken 5230 times.
✓ Branch 1 taken 1046 times.
✓ Branch 2 taken 4184 times.
✓ Branch 3 taken 1046 times.
✓ Branch 4 taken 1046 times.
✓ Branch 5 taken 3138 times.
6276 if (n == 0 || n == 4 || n == 5)
927 3138 b = 1024;
928 }
929
930
2/2
✓ Branch 0 taken 437890 times.
✓ Branch 1 taken 702644 times.
1140534 if (abs(a - b) < abs(b - c)) {
931 437890 pred = c;
932 437890 *dir_ptr = 1; /* top */
933 } else {
934 702644 pred = a;
935 702644 *dir_ptr = 0; /* left */
936 }
937 1140534 return pred;
938 }
939
940 1140534 static inline int mpeg4_get_level_dc(MpegEncContext *s, int n, int pred, int level)
941 {
942
2/2
✓ Branch 0 taken 760356 times.
✓ Branch 1 taken 380178 times.
1140534 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 1140534 pred = FASTDIV((pred + (scale >> 1)), scale);
950
951 1140534 level += pred;
952 1140534 ret = level;
953 1140534 level *= scale;
954
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1140534 times.
1140534 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 1140534 s->dc_val[s->block_index[n]] = level;
973
974 1140534 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 1140534 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 760356 times.
✓ Branch 1 taken 380178 times.
1140534 if (n < 4)
988 760356 code = get_vlc2(&h->gb, dc_lum, DC_VLC_BITS, 1);
989 else
990 380178 code = get_vlc2(&h->gb, dc_chrom, DC_VLC_BITS, 1);
991
992
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1140534 times.
1140534 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 182332 times.
✓ Branch 1 taken 958202 times.
1140534 if (code == 0) {
998 182332 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 958202 level = get_xbits(&h->gb, code);
1011 }
1012
1013
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 958202 times.
958202 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 1140534 pred = mpeg4_pred_dc(&h->c, n, dir_ptr);
1024 1140534 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 8120472 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 8120472 H263DecContext *const h = &ctx->h;
1386 int level, i, last, run, qmul, qadd, pred;
1387 8120472 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 1140534 times.
✓ Branch 1 taken 6979938 times.
8120472 if (intra) {
1395 // FIXME add short header support
1396
1/2
✓ Branch 0 taken 1140534 times.
✗ Branch 1 not taken.
1140534 if (use_intra_dc_vlc) {
1397 /* DC coef */
1398
2/2
✓ Branch 0 taken 141660 times.
✓ Branch 1 taken 998874 times.
1140534 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 998874 level = mpeg4_decode_dc(h, n, &dc_pred_dir);
1407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 998874 times.
998874 if (level < 0)
1408 ✗ return level;
1409 }
1410 1140534 block[0] = level;
1411 1140534 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 238686 times.
✓ Branch 1 taken 901848 times.
1140534 if (!coded)
1417 238686 goto not_coded;
1418
1419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 901848 times.
901848 if (rvlc) {
1420 ✗ rl = &ff_rvlc_rl_intra;
1421 ✗ rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
1422 } else {
1423 901848 rl = &ff_mpeg4_rl_intra;
1424 901848 rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
1425 }
1426
2/2
✓ Branch 0 taken 22081 times.
✓ Branch 1 taken 879767 times.
901848 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->permutated_intra_v_scantable; /* left */
1429 else
1430 6566 scan_table = h->permutated_intra_h_scantable; /* top */
1431 } else {
1432 879767 scan_table = h->c.intra_scantable.permutated;
1433 }
1434 901848 qmul = 1;
1435 901848 qadd = 0;
1436 } else {
1437 6979938 i = -1;
1438
2/2
✓ Branch 0 taken 4748444 times.
✓ Branch 1 taken 2231494 times.
6979938 if (!coded) {
1439 4748444 h->c.block_last_index[n] = i;
1440 4748444 return 0;
1441 }
1442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2231494 times.
2231494 if (rvlc)
1443 ✗ rl = &ff_rvlc_rl_inter;
1444 else
1445 2231494 rl = &ff_h263_rl_inter;
1446
1447 2231494 scan_table = h->c.intra_scantable.permutated;
1448
1449
2/2
✓ Branch 0 taken 112985 times.
✓ Branch 1 taken 2118509 times.
2231494 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 2118509 qmul = h->c.qscale << 1;
1458 2118509 qadd = (h->c.qscale - 1) | 1;
1459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2118509 times.
2118509 if (rvlc)
1460 ✗ rl_vlc = ff_rvlc_rl_inter.rl_vlc[h->c.qscale];
1461 else
1462 2118509 rl_vlc = ff_h263_rl_inter.rl_vlc[h->c.qscale];
1463 }
1464 }
1465 {
1466 3133342 OPEN_READER(re, &h->gb);
1467 for (;;) {
1468 50784236 UPDATE_CACHE(re, &h->gb);
1469
2/2
✓ Branch 1 taken 1123536 times.
✓ Branch 2 taken 25835253 times.
26958789 GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1470
2/2
✓ Branch 0 taken 774245 times.
✓ Branch 1 taken 26184544 times.
26958789 if (level == 0) {
1471 /* escape */
1472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 774245 times.
774245 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 774245 cache = GET_CACHE(re, &h->gb);
1512
1513 if (IS_3IV1)
1514 cache ^= 0xC0000000;
1515
1516
2/2
✓ Branch 0 taken 359184 times.
✓ Branch 1 taken 415061 times.
774245 if (cache & 0x80000000) {
1517
2/2
✓ Branch 0 taken 139219 times.
✓ Branch 1 taken 219965 times.
359184 if (cache & 0x40000000) {
1518 /* third escape */
1519 139219 SKIP_CACHE(re, &h->gb, 2);
1520 139219 last = SHOW_UBITS(re, &h->gb, 1);
1521 139219 SKIP_CACHE(re, &h->gb, 1);
1522 139219 run = SHOW_UBITS(re, &h->gb, 6);
1523 139219 SKIP_COUNTER(re, &h->gb, 2 + 1 + 6);
1524 139219 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 139219 times.
139219 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 139219 SKIP_CACHE(re, &h->gb, 1);
1537
1538 139219 level = SHOW_SBITS(re, &h->gb, 12);
1539 139219 SKIP_CACHE(re, &h->gb, 12);
1540
1541
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 139219 times.
139219 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 139219 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 69809 times.
✓ Branch 1 taken 69410 times.
139219 if (level > 0)
1574 69809 level = level * qmul + qadd;
1575 else
1576 69410 level = level * qmul - qadd;
1577
1578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 139219 times.
139219 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 139219 i += run + 1;
1591
2/2
✓ Branch 0 taken 44007 times.
✓ Branch 1 taken 95212 times.
139219 if (last)
1592 44007 i += 192;
1593 } else {
1594 /* second escape */
1595 219965 SKIP_BITS(re, &h->gb, 2);
1596
2/2
✓ Branch 1 taken 24415 times.
✓ Branch 2 taken 195550 times.
219965 GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1597 219965 i += run + rl->max_run[run >> 7][level / qmul] + 1; // FIXME opt indexing
1598 219965 level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1);
1599 219965 LAST_SKIP_BITS(re, &h->gb, 1);
1600 }
1601 } else {
1602 /* first escape */
1603 415061 SKIP_BITS(re, &h->gb, 1);
1604
2/2
✓ Branch 1 taken 66854 times.
✓ Branch 2 taken 348207 times.
415061 GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1605 415061 i += run;
1606 415061 level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; // FIXME opt indexing
1607 415061 level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1);
1608 415061 LAST_SKIP_BITS(re, &h->gb, 1);
1609 }
1610 }
1611 } else {
1612 26184544 i += run;
1613 26184544 level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1);
1614 26184544 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 3133342 times.
✓ Branch 1 taken 23825447 times.
26958789 if (i > 62) {
1618 3133342 i -= 192;
1619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3133342 times.
3133342 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 3133342 block[scan_table[i]] = level;
1626 3133342 break;
1627 }
1628
1629 23825447 block[scan_table[i]] = level;
1630 }
1631 3133342 CLOSE_READER(re, &h->gb);
1632 }
1633
1634 3372028 not_coded:
1635
2/2
✓ Branch 0 taken 1140534 times.
✓ Branch 1 taken 2231494 times.
3372028 if (intra) {
1636
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1140534 times.
1140534 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 1140534 ff_mpeg4_pred_ac(h, block, n, dc_pred_dir);
1643
2/2
✓ Branch 0 taken 25890 times.
✓ Branch 1 taken 1114644 times.
1140534 if (h->c.ac_pred)
1644 25890 i = 63; // FIXME not optimal
1645 }
1646 3372028 h->c.block_last_index[n] = i;
1647 3372028 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 1387540 static int mpeg4_decode_mb(H263DecContext *const h)
1742 {
1743 1387540 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 1387540 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 371418 times.
✓ Branch 1 taken 1016122 times.
1387540 if (h->c.pict_type == AV_PICTURE_TYPE_P ||
1752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 371418 times.
371418 h->c.pict_type == AV_PICTURE_TYPE_S) {
1753 do {
1754
2/2
✓ Branch 1 taken 138337 times.
✓ Branch 2 taken 877785 times.
1016122 if (get_bits1(&h->gb)) {
1755 /* skip mb */
1756 138337 h->c.mb_intra = 0;
1757
2/2
✓ Branch 0 taken 830022 times.
✓ Branch 1 taken 138337 times.
968359 for (i = 0; i < 6; i++)
1758 830022 h->c.block_last_index[i] = -1;
1759 138337 h->c.mv_dir = MV_DIR_FORWARD;
1760 138337 h->c.mv_type = MV_TYPE_16X16;
1761
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138337 times.
138337 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 138337 h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 |
1774 MB_TYPE_FORWARD_MV;
1775 138337 h->c.mcsel = 0;
1776 138337 h->c.mv[0][0][0] = 0;
1777 138337 h->c.mv[0][0][1] = 0;
1778 138337 h->c.cur_pic.mbskip_table[xy] = 1;
1779 138337 h->c.mb_skipped = 1;
1780 }
1781 138337 goto end;
1782 }
1783 877785 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 877785 times.
877785 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 877785 times.
877785 } while (cbpc == 20);
1790
1791 877785 dquant = cbpc & 8;
1792 877785 h->c.mb_intra = ((cbpc & 4) != 0);
1793
2/2
✓ Branch 0 taken 29716 times.
✓ Branch 1 taken 848069 times.
877785 if (h->c.mb_intra)
1794 29716 goto intra;
1795 848069 h->c.bdsp.clear_blocks(h->block[0]);
1796
1797
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 848069 times.
848069 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 848069 h->c.mcsel = 0;
1802 848069 cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1) ^ 0x0F;
1803
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 848069 times.
848069 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 848069 cbp = (cbpc & 3) | (cbpy << 2);
1810
2/2
✓ Branch 0 taken 23756 times.
✓ Branch 1 taken 824313 times.
848069 if (dquant)
1811 23756 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 848069 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
848069 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 848069 h->c.mv_dir = MV_DIR_FORWARD;
1817
2/2
✓ Branch 0 taken 750084 times.
✓ Branch 1 taken 97985 times.
848069 if ((cbpc & 16) == 0) {
1818
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 750084 times.
750084 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 750084 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
750084 } 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 750084 h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_FORWARD_MV;
1852 /* 16x16 motion prediction */
1853 750084 h->c.mv_type = MV_TYPE_16X16;
1854 750084 ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y);
1855 750084 mx = ff_h263_decode_motion(h, pred_x, ctx->f_code);
1856
1857
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 750084 times.
750084 if (mx >= 0xffff)
1858 ✗ return AVERROR_INVALIDDATA;
1859
1860 750084 my = ff_h263_decode_motion(h, pred_y, ctx->f_code);
1861
1862
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 750084 times.
750084 if (my >= 0xffff)
1863 ✗ return AVERROR_INVALIDDATA;
1864 750084 h->c.mv[0][0][0] = mx;
1865 750084 h->c.mv[0][0][1] = my;
1866 }
1867 } else {
1868 97985 h->c.cur_pic.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_FORWARD_MV;
1869 97985 h->c.mv_type = MV_TYPE_8X8;
1870
2/2
✓ Branch 0 taken 391940 times.
✓ Branch 1 taken 97985 times.
489925 for (i = 0; i < 4; i++) {
1871 391940 int16_t *mot_val = ff_h263_pred_motion(&h->c, i, 0, &pred_x, &pred_y);
1872 391940 mx = ff_h263_decode_motion(h, pred_x, ctx->f_code);
1873
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 391940 times.
391940 if (mx >= 0xffff)
1874 ✗ return AVERROR_INVALIDDATA;
1875
1876 391940 my = ff_h263_decode_motion(h, pred_y, ctx->f_code);
1877
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 391940 times.
391940 if (my >= 0xffff)
1878 ✗ return AVERROR_INVALIDDATA;
1879 391940 h->c.mv[0][i][0] = mx;
1880 391940 h->c.mv[0][i][1] = my;
1881 391940 mot_val[0] = mx;
1882 391940 mot_val[1] = my;
1883 }
1884 }
1885
2/2
✓ Branch 0 taken 234655 times.
✓ Branch 1 taken 136763 times.
371418 } 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 234655 h->c.mb_intra = 0; // B-frames never contain intra blocks
1891 234655 h->c.mcsel = 0; // ... true gmc blocks
1892
1893
2/2
✓ Branch 0 taken 10039 times.
✓ Branch 1 taken 224616 times.
234655 if (h->c.mb_x == 0) {
1894
2/2
✓ Branch 0 taken 20078 times.
✓ Branch 1 taken 10039 times.
30117 for (i = 0; i < 2; i++) {
1895 20078 h->c.last_mv[i][0][0] =
1896 20078 h->c.last_mv[i][0][1] =
1897 20078 h->c.last_mv[i][1][0] =
1898 20078 h->c.last_mv[i][1][1] = 0;
1899 }
1900
1901 10039 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 234655 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 32858 times.
✓ Branch 1 taken 201797 times.
234655 if (h->c.mb_skipped) {
1908 /* skip mb */
1909
2/2
✓ Branch 0 taken 197148 times.
✓ Branch 1 taken 32858 times.
230006 for (i = 0; i < 6; i++)
1910 197148 h->c.block_last_index[i] = -1;
1911
1912 32858 h->c.mv_dir = MV_DIR_FORWARD;
1913 32858 h->c.mv_type = MV_TYPE_16X16;
1914 32858 h->c.mv[0][0][0] =
1915 32858 h->c.mv[0][0][1] =
1916 32858 h->c.mv[1][0][0] =
1917 32858 h->c.mv[1][0][1] = 0;
1918 32858 h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP |
1919 MB_TYPE_16x16 |
1920 MB_TYPE_FORWARD_MV;
1921 32858 goto end;
1922 }
1923
1924 201797 modb1 = get_bits1(&h->gb);
1925
2/2
✓ Branch 0 taken 44656 times.
✓ Branch 1 taken 157141 times.
201797 if (modb1) {
1926 // like MB_TYPE_B_DIRECT but no vectors coded
1927 44656 mb_type = MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_BIDIR_MV;
1928 44656 cbp = 0;
1929 } else {
1930 157141 modb2 = get_bits1(&h->gb);
1931 157141 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 157141 times.
157141 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 65170 times.
✓ Branch 1 taken 91971 times.
157141 if (modb2) {
1937 65170 cbp = 0;
1938 } else {
1939 91971 h->c.bdsp.clear_blocks(h->block[0]);
1940 91971 cbp = get_bits(&h->gb, 6);
1941 }
1942
1943
4/4
✓ Branch 0 taken 116094 times.
✓ Branch 1 taken 41047 times.
✓ Branch 2 taken 66366 times.
✓ Branch 3 taken 49728 times.
157141 if ((!IS_DIRECT(mb_type)) && cbp) {
1944
2/2
✓ Branch 1 taken 7585 times.
✓ Branch 2 taken 58781 times.
66366 if (get_bits1(&h->gb))
1945 7585 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 157141 times.
157141 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 157141 h->c.mv_dir = 0;
1968
2/2
✓ Branch 0 taken 116094 times.
✓ Branch 1 taken 41047 times.
157141 if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
1969 116094 h->c.mv_type = MV_TYPE_16X16;
1970
1971
2/2
✓ Branch 0 taken 80827 times.
✓ Branch 1 taken 35267 times.
116094 if (HAS_FORWARD_MV(mb_type)) {
1972 80827 h->c.mv_dir = MV_DIR_FORWARD;
1973
1974 80827 mx = ff_h263_decode_motion(h, h->c.last_mv[0][0][0], ctx->f_code);
1975 80827 my = ff_h263_decode_motion(h, h->c.last_mv[0][0][1], ctx->f_code);
1976 80827 h->c.last_mv[0][1][0] =
1977 80827 h->c.last_mv[0][0][0] =
1978 80827 h->c.mv[0][0][0] = mx;
1979 80827 h->c.last_mv[0][1][1] =
1980 80827 h->c.last_mv[0][0][1] =
1981 80827 h->c.mv[0][0][1] = my;
1982 }
1983
1984
2/2
✓ Branch 0 taken 92480 times.
✓ Branch 1 taken 23614 times.
116094 if (HAS_BACKWARD_MV(mb_type)) {
1985 92480 h->c.mv_dir |= MV_DIR_BACKWARD;
1986
1987 92480 mx = ff_h263_decode_motion(h, h->c.last_mv[1][0][0], ctx->b_code);
1988 92480 my = ff_h263_decode_motion(h, h->c.last_mv[1][0][1], ctx->b_code);
1989 92480 h->c.last_mv[1][1][0] =
1990 92480 h->c.last_mv[1][0][0] =
1991 92480 h->c.mv[1][0][0] = mx;
1992 92480 h->c.last_mv[1][1][1] =
1993 92480 h->c.last_mv[1][0][1] =
1994 92480 h->c.mv[1][0][1] = my;
1995 }
1996
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41047 times.
41047 } 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 85703 times.
✓ Branch 1 taken 116094 times.
201797 if (IS_DIRECT(mb_type)) {
2026
2/2
✓ Branch 0 taken 44656 times.
✓ Branch 1 taken 41047 times.
85703 if (IS_SKIP(mb_type)) {
2027 44656 mx =
2028 44656 my = 0;
2029 } else {
2030 41047 mx = ff_h263_decode_motion(h, 0, 1);
2031 41047 my = ff_h263_decode_motion(h, 0, 1);
2032 }
2033
2034 85703 h->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
2035 85703 mb_type |= ff_mpeg4_set_direct_mv(&h->c, mx, my);
2036 }
2037 201797 h->c.cur_pic.mb_type[xy] = mb_type;
2038 } else { /* I-Frame */
2039 int use_intra_dc_vlc;
2040
2041 do {
2042 136763 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 136763 times.
136763 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 136763 times.
136763 } while (cbpc == 8);
2049
2050 136763 dquant = cbpc & 4;
2051 136763 h->c.mb_intra = 1;
2052
2053 166479 intra:
2054 166479 h->c.ac_pred = get_bits1(&h->gb);
2055
2/2
✓ Branch 0 taken 552 times.
✓ Branch 1 taken 165927 times.
166479 if (h->c.ac_pred)
2056 552 h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
2057 else
2058 165927 h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA;
2059
2060 166479 cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1);
2061
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 166479 times.
166479 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 166479 cbp = (cbpc & 3) | (cbpy << 2);
2067
2068 166479 use_intra_dc_vlc = h->c.qscale < ctx->intra_dc_threshold;
2069
2070
2/2
✓ Branch 0 taken 10532 times.
✓ Branch 1 taken 155947 times.
166479 if (dquant)
2071 10532 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 166479 times.
166479 if (!h->c.progressive_sequence)
2074 ✗ h->c.interlaced_dct = get_bits1(&h->gb);
2075
2076 166479 h->c.bdsp.clear_blocks(h->block[0]);
2077 /* decode each block */
2078
2/2
✓ Branch 0 taken 998874 times.
✓ Branch 1 taken 166479 times.
1165353 for (i = 0; i < 6; i++) {
2079
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 998874 times.
998874 if (mpeg4_decode_block(ctx, h->block[i], i, cbp & 32,
2080 1, use_intra_dc_vlc, 0) < 0)
2081 ✗ return AVERROR_INVALIDDATA;
2082 998874 cbp += cbp;
2083 }
2084 166479 goto end;
2085 }
2086
2087 /* decode each block */
2088
2/2
✓ Branch 0 taken 6299196 times.
✓ Branch 1 taken 1049866 times.
7349062 for (i = 0; i < 6; i++) {
2089
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6299196 times.
6299196 if (mpeg4_decode_block(ctx, h->block[i], i, cbp & 32, 0, 0, 0) < 0)
2090 ✗ return AVERROR_INVALIDDATA;
2091 6299196 cbp += cbp;
2092 }
2093
2094 1049866 end:
2095 /* per-MB end of slice check */
2096 1387540 next = mpeg4_is_resync(ctx);
2097
2/2
✓ Branch 0 taken 13541 times.
✓ Branch 1 taken 1373999 times.
1387540 if (next) {
2098
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 13536 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
13541 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 5597 times.
✓ Branch 1 taken 7944 times.
13541 } else if (h->c.mb_x + h->c.mb_y*h->c.mb_width + 1 >= next)
2101 5597 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 1373999 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->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->last_dc[cc] * (8 >> h->c.intra_dc_precision);
2203 else
2204 5336 block[0] = h->last_dc[cc] * (8 >> h->c.intra_dc_precision) * (8 >> ctx->dct_precision);
2205 /* TODO: support mpeg_quant for AC coefficients */
2206
2207 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->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 1006 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 1006 times.
1006 if (!show_bits(gb, 23)) {
2409 ✗ av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
2410 ✗ return AVERROR_INVALIDDATA;
2411 }
2412
2413 1006 hours = get_bits(gb, 5);
2414 1006 minutes = get_bits(gb, 6);
2415 1006 check_marker(s->avctx, gb, "in gop_header");
2416 1006 seconds = get_bits(gb, 6);
2417
2418 1006 s->time_base = seconds + 60*(minutes + 60*hours);
2419
2420 1006 skip_bits1(gb);
2421 1006 skip_bits1(gb);
2422
2423 1006 return 0;
2424 }
2425
2426 1209 static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb, int *profile, int *level)
2427 {
2428
2429 1209 *profile = get_bits(gb, 4);
2430 1209 *level = get_bits(gb, 4);
2431
2432 // for Simple profile, level 0
2433
3/4
✓ Branch 0 taken 705 times.
✓ Branch 1 taken 504 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 705 times.
1209 if (*profile == 0 && *level == 8) {
2434 ✗ *level = 0;
2435 }
2436
2437 1209 return 0;
2438 }
2439
2440 1209 static int mpeg4_decode_visual_object(MpegEncContext *s, GetBitContext *gb)
2441 {
2442 int visual_object_type;
2443 1209 int is_visual_object_identifier = get_bits1(gb);
2444
2445
2/2
✓ Branch 0 taken 1183 times.
✓ Branch 1 taken 26 times.
1209 if (is_visual_object_identifier) {
2446 1183 skip_bits(gb, 4+3);
2447 }
2448 1209 visual_object_type = get_bits(gb, 4);
2449
2450
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1209 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1209 if (visual_object_type == VOT_VIDEO_ID ||
2451 visual_object_type == VOT_STILL_TEXTURE_ID) {
2452 1209 int video_signal_type = get_bits1(gb);
2453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1209 times.
1209 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 1209 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 1242 static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2642 {
2643 1242 H263DecContext *const h = &ctx->h;
2644 int width, height, vo_ver_id, aspect_ratio_info;
2645
2646 /* vol header */
2647 1242 skip_bits(gb, 1); /* random access */
2648 1242 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 1239 times.
✓ Branch 1 taken 3 times.
1242 if (ctx->vo_type == CORE_STUDIO_VO_TYPE ||
2656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
1239 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 1239 times.
1239 } else if (h->c.studio_profile) {
2663 ✗ return AVERROR_PATCHWELCOME;
2664 }
2665
2666
2/2
✓ Branch 1 taken 1211 times.
✓ Branch 2 taken 28 times.
1239 if (get_bits1(gb) != 0) { /* is_ol_id */
2667 1211 vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
2668 1211 skip_bits(gb, 3); /* vo_priority */
2669 } else {
2670 28 vo_ver_id = 1;
2671 }
2672 1239 aspect_ratio_info = get_bits(gb, 4);
2673
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1237 times.
1239 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 1237 h->c.avctx->sample_aspect_ratio = ff_h263_pixel_aspect[aspect_ratio_info];
2678 }
2679
2680
2/2
✓ Branch 1 taken 1202 times.
✓ Branch 2 taken 37 times.
1239 if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
2681 1202 int chroma_format = get_bits(gb, 2);
2682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1202 times.
1202 if (chroma_format != CHROMA_420)
2683 ✗ av_log(h->c.avctx, AV_LOG_ERROR, "illegal chroma format\n");
2684
2685 1202 h->c.low_delay = get_bits1(gb);
2686
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1202 times.
1202 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 1239 ctx->shape = get_bits(gb, 2); /* vol shape */
2715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
1239 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 1239 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1239 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 1239 check_marker(h->c.avctx, gb, "before time_increment_resolution");
2723
2724 1239 h->c.avctx->framerate.num = get_bits(gb, 16);
2725
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
1239 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 1239 ctx->time_increment_bits = av_log2(h->c.avctx->framerate.num - 1) + 1;
2731
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
1239 if (ctx->time_increment_bits < 1)
2732 ✗ ctx->time_increment_bits = 1;
2733
2734 1239 check_marker(h->c.avctx, gb, "before fixed_vop_rate");
2735
2736
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 1220 times.
1239 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 1220 h->c.avctx->framerate.den = 1;
2740
2741 1239 ctx->t_frame = 0;
2742
2743
1/2
✓ Branch 0 taken 1239 times.
✗ Branch 1 not taken.
1239 if (ctx->shape != BIN_ONLY_SHAPE) {
2744
1/2
✓ Branch 0 taken 1239 times.
✗ Branch 1 not taken.
1239 if (ctx->shape == RECT_SHAPE) {
2745 1239 check_marker(h->c.avctx, gb, "before width");
2746 1239 width = get_bits(gb, 13);
2747 1239 check_marker(h->c.avctx, gb, "before height");
2748 1239 height = get_bits(gb, 13);
2749 1239 check_marker(h->c.avctx, gb, "after height");
2750
2/4
✓ Branch 0 taken 1239 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1239 times.
✗ Branch 3 not taken.
1239 if (width && height && /* they should be non zero but who knows */
2751
3/4
✓ Branch 0 taken 881 times.
✓ Branch 1 taken 358 times.
✓ Branch 2 taken 881 times.
✗ Branch 3 not taken.
1239 !(h->c.width && h->c.codec_tag == AV_RL32("MP4S"))) {
2752
3/4
✓ Branch 0 taken 881 times.
✓ Branch 1 taken 358 times.
✓ Branch 2 taken 881 times.
✗ Branch 3 not taken.
1239 if (h->c.width && h->c.height &&
2753
3/4
✓ Branch 0 taken 861 times.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 861 times.
881 (h->c.width != width || h->c.height != height))
2754 20 h->c.context_reinit = 1;
2755 1239 h->c.width = width;
2756 1239 h->c.height = height;
2757 }
2758 }
2759
2760 1239 h->c.progressive_sequence =
2761 1239 h->c.progressive_frame = get_bits1(gb) ^ 1;
2762 1239 h->c.interlaced_dct = 0;
2763
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 1239 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1239 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 751 times.
✓ Branch 1 taken 488 times.
1239 if (vo_ver_id == 1)
2767 751 ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
2768 else
2769 488 ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
2770
2771
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
1239 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 1239 times.
✗ Branch 1 not taken.
1239 if (ctx->vol_sprite_usage == STATIC_SPRITE ||
2774
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
1239 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 1239 times.
1239 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 1239 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 1131 times.
1239 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 488 times.
✓ Branch 1 taken 751 times.
1239 if (vo_ver_id != 1)
2874 488 h->c.quarter_sample = get_bits1(gb);
2875 else
2876 751 h->c.quarter_sample = 0;
2877
2878
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1239 times.
1239 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 1239 times.
1239 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 1239 no_cplx_est:
2934 1239 ctx->cplx_estimation_trash_i =
2935 1239 ctx->cplx_estimation_trash_p =
2936 1239 ctx->cplx_estimation_trash_b = 0;
2937 }
2938
2939 1239 ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
2940
2941 1239 h->data_partitioning = get_bits1(gb);
2942
2/2
✓ Branch 0 taken 273 times.
✓ Branch 1 taken 966 times.
1239 if (h->data_partitioning)
2943 273 ctx->rvlc = get_bits1(gb);
2944
2945
2/2
✓ Branch 0 taken 488 times.
✓ Branch 1 taken 751 times.
1239 if (vo_ver_id != 1) {
2946 488 ctx->new_pred = get_bits1(gb);
2947
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 488 times.
488 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 488 times.
488 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 751 ctx->new_pred = 0;
2957 }
2958
2959 1239 ctx->scalability = get_bits1(gb);
2960
2961
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
1239 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 1239 times.
1239 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 1239 return 0;
3005 }
3006
3007 /**
3008 * Decode the user data stuff in the header.
3009 * Also initializes divx/xvid/lavc_version/build.
3010 */
3011 90 static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
3012 {
3013 90 H263DecContext *const h = &ctx->h;
3014 char buf[256];
3015 int i;
3016 int e;
3017 90 int ver = 0, build = 0, ver2 = 0, ver3 = 0;
3018 char last;
3019
3020
3/4
✓ Branch 0 taken 980 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 944 times.
✓ Branch 4 taken 36 times.
980 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 890 times.
944 if (show_bits(gb, 23) == 0)
3022 54 break;
3023 890 buf[i] = get_bits(gb, 8);
3024 }
3025 90 buf[i] = 0;
3026
3027 /* divx detection */
3028 90 e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
3029
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 if (e < 2)
3030 90 e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
3031
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 80 times.
90 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 90 e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
3039
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 if (e != 4)
3040 90 e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
3041
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 if (e != 4) {
3042 90 e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
3043
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 57 times.
90 if (e > 1) {
3044
3/6
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 33 times.
33 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 33 build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF);
3051 }
3052 }
3053
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 33 times.
90 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 33 times.
✓ Branch 1 taken 57 times.
90 if (e == 4)
3058 33 ctx->lavc_build = build;
3059
3060 /* Xvid detection */
3061 90 e = sscanf(buf, "XviD%d", &build);
3062
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 71 times.
90 if (e == 1)
3063 19 ctx->xvid_build = build;
3064
3065 90 return 0;
3066 }
3067
3068 7137 static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb,
3069 int parse_only)
3070 {
3071 7137 H263DecContext *const h = &ctx->h;
3072 int time_incr, time_increment;
3073 int64_t pts;
3074
3075 7137 h->c.mcsel = 0;
3076 7137 h->c.pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
3077
3/4
✓ Branch 0 taken 1301 times.
✓ Branch 1 taken 5836 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1301 times.
7137 if (h->c.pict_type == AV_PICTURE_TYPE_B && h->c.low_delay &&
3078 ✗ ctx->vol_control_parameters == 0 && !(h->c.avctx->flags & AV_CODEC_FLAG_LOW_DELAY)) {
3079 ✗ av_log(h->c.avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n");
3080 ✗ h->c.low_delay = 0;
3081 }
3082
3083
4/4
✓ Branch 0 taken 1284 times.
✓ Branch 1 taken 5853 times.
✓ Branch 2 taken 1028 times.
✓ Branch 3 taken 256 times.
7137 h->partitioned_frame = h->data_partitioning && h->c.pict_type != AV_PICTURE_TYPE_B;
3084
2/2
✓ Branch 0 taken 1028 times.
✓ Branch 1 taken 6109 times.
7137 if (h->partitioned_frame)
3085 1028 h->decode_mb = mpeg4_decode_partitioned_mb;
3086 else
3087 6109 h->decode_mb = mpeg4_decode_mb;
3088
3089 7137 time_incr = 0;
3090
2/2
✓ Branch 1 taken 245 times.
✓ Branch 2 taken 7137 times.
7382 while (get_bits1(gb) != 0)
3091 245 time_incr++;
3092
3093 7137 check_marker(h->c.avctx, gb, "before time_increment");
3094
3095
2/2
✓ Branch 0 taken 7135 times.
✓ Branch 1 taken 2 times.
7137 if (ctx->time_increment_bits == 0 ||
3096
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 7127 times.
7135 !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
3097 10 av_log(h->c.avctx, AV_LOG_WARNING,
3098 "time_increment_bits %d is invalid in relation to the current bitstream, this is likely caused by a missing VOL header\n", ctx->time_increment_bits);
3099
3100 10 for (ctx->time_increment_bits = 1;
3101
1/2
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
110 ctx->time_increment_bits < 16;
3102 100 ctx->time_increment_bits++) {
3103
1/2
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
110 if (h->c.pict_type == AV_PICTURE_TYPE_P ||
3104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 (h->c.pict_type == AV_PICTURE_TYPE_S &&
3105 ✗ ctx->vol_sprite_usage == GMC_SPRITE)) {
3106 ✗ if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
3107 ✗ break;
3108
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)
3109 10 break;
3110 }
3111
3112 10 av_log(h->c.avctx, AV_LOG_WARNING,
3113 "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits);
3114 }
3115
3116 if (IS_3IV1)
3117 time_increment = get_bits1(gb); // FIXME investigate further
3118 else
3119 7137 time_increment = get_bits(gb, ctx->time_increment_bits);
3120
3121
2/2
✓ Branch 0 taken 5836 times.
✓ Branch 1 taken 1301 times.
7137 if (h->c.pict_type != AV_PICTURE_TYPE_B) {
3122 5836 h->c.last_time_base = h->c.time_base;
3123 5836 h->c.time_base += time_incr;
3124 5836 h->c.time = h->c.time_base * (int64_t)h->c.avctx->framerate.num + time_increment;
3125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5836 times.
5836 if (h->c.workaround_bugs & FF_BUG_UMP4) {
3126 ✗ if (h->c.time < h->c.last_non_b_time) {
3127 /* header is not mpeg-4-compatible, broken encoder,
3128 * trying to workaround */
3129 ✗ h->c.time_base++;
3130 ✗ h->c.time += h->c.avctx->framerate.num;
3131 }
3132 }
3133 5836 h->c.pp_time = h->c.time - h->c.last_non_b_time;
3134 5836 h->c.last_non_b_time = h->c.time;
3135 } else {
3136 1301 h->c.time = (h->c.last_time_base + time_incr) * (int64_t)h->c.avctx->framerate.num + time_increment;
3137 1301 h->c.pb_time = h->c.pp_time - (h->c.last_non_b_time - h->c.time);
3138
1/2
✓ Branch 0 taken 1301 times.
✗ Branch 1 not taken.
1301 if (h->c.pp_time <= h->c.pb_time ||
3139
1/2
✓ Branch 0 taken 1301 times.
✗ Branch 1 not taken.
1301 h->c.pp_time <= h->c.pp_time - h->c.pb_time ||
3140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1301 times.
1301 h->c.pp_time <= 0) {
3141 /* messed up order, maybe after seeking? skipping current B-frame */
3142 ✗ return FRAME_SKIPPED;
3143 }
3144 1301 ff_mpeg4_init_direct_mv(&h->c);
3145
3146
2/2
✓ Branch 0 taken 215 times.
✓ Branch 1 taken 1086 times.
1301 if (ctx->t_frame == 0)
3147 215 ctx->t_frame = h->c.pb_time;
3148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1301 times.
1301 if (ctx->t_frame == 0)
3149 ✗ ctx->t_frame = 1; // 1/0 protection
3150
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) -
3151
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;
3152
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) -
3153
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;
3154
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) {
3155 2 h->c.pb_field_time = 2;
3156 2 h->c.pp_field_time = 4;
3157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!h->c.progressive_sequence)
3158 ✗ return FRAME_SKIPPED;
3159 }
3160 }
3161
3162
1/2
✓ Branch 0 taken 7137 times.
✗ Branch 1 not taken.
7137 if (h->c.avctx->framerate.den)
3163
1/2
✓ Branch 0 taken 7137 times.
✗ Branch 1 not taken.
7137 pts = ROUNDED_DIV(h->c.time, h->c.avctx->framerate.den);
3164 else
3165 ✗ pts = AV_NOPTS_VALUE;
3166 ff_dlog(h->c.avctx, "MPEG4 PTS: %"PRId64"\n", pts);
3167
3168 7137 check_marker(h->c.avctx, gb, "before vop_coded");
3169
3170 /* vop coded */
3171
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 7129 times.
7137 if (get_bits1(gb) != 1) {
3172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (h->c.avctx->debug & FF_DEBUG_PICT_INFO)
3173 ✗ av_log(h->c.avctx, AV_LOG_ERROR, "vop not coded\n");
3174 8 h->skipped_last_frame = 1;
3175 8 return FRAME_SKIPPED;
3176 }
3177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7129 times.
7129 if (ctx->new_pred)
3178 ✗ decode_new_pred(ctx, gb);
3179
3180
1/2
✓ Branch 0 taken 7129 times.
✗ Branch 1 not taken.
7129 if (ctx->shape != BIN_ONLY_SHAPE &&
3181
2/2
✓ Branch 0 taken 2360 times.
✓ Branch 1 taken 4769 times.
7129 (h->c.pict_type == AV_PICTURE_TYPE_P ||
3182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2360 times.
2360 (h->c.pict_type == AV_PICTURE_TYPE_S &&
3183 ✗ ctx->vol_sprite_usage == GMC_SPRITE))) {
3184 /* rounding type for motion estimation */
3185 4769 h->c.no_rounding = get_bits1(gb);
3186 } else {
3187 2360 h->c.no_rounding = 0;
3188 }
3189 // FIXME reduced res stuff
3190
3191
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7129 times.
7129 if (ctx->shape != RECT_SHAPE) {
3192 ✗ if (ctx->vol_sprite_usage != 1 || h->c.pict_type != AV_PICTURE_TYPE_I) {
3193 ✗ skip_bits(gb, 13); /* width */
3194 ✗ check_marker(h->c.avctx, gb, "after width");
3195 ✗ skip_bits(gb, 13); /* height */
3196 ✗ check_marker(h->c.avctx, gb, "after height");
3197 ✗ skip_bits(gb, 13); /* hor_spat_ref */
3198 ✗ check_marker(h->c.avctx, gb, "after hor_spat_ref");
3199 ✗ skip_bits(gb, 13); /* ver_spat_ref */
3200 }
3201 ✗ skip_bits1(gb); /* change_CR_disable */
3202
3203 ✗ if (get_bits1(gb) != 0)
3204 ✗ skip_bits(gb, 8); /* constant_alpha_value */
3205 }
3206
3207 // FIXME complexity estimation stuff
3208
3209
1/2
✓ Branch 0 taken 7129 times.
✗ Branch 1 not taken.
7129 if (ctx->shape != BIN_ONLY_SHAPE) {
3210 7129 skip_bits_long(gb, ctx->cplx_estimation_trash_i);
3211
2/2
✓ Branch 0 taken 6070 times.
✓ Branch 1 taken 1059 times.
7129 if (h->c.pict_type != AV_PICTURE_TYPE_I)
3212 6070 skip_bits_long(gb, ctx->cplx_estimation_trash_p);
3213
2/2
✓ Branch 0 taken 1301 times.
✓ Branch 1 taken 5828 times.
7129 if (h->c.pict_type == AV_PICTURE_TYPE_B)
3214 1301 skip_bits_long(gb, ctx->cplx_estimation_trash_b);
3215
3216
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7129 times.
7129 if (get_bits_left(gb) < 3) {
3217 ✗ av_log(h->c.avctx, AV_LOG_ERROR, "Header truncated\n");
3218 ✗ return AVERROR_INVALIDDATA;
3219 }
3220 7129 ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
3221
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 7079 times.
7129 if (!h->c.progressive_sequence) {
3222 50 h->c.top_field_first = get_bits1(gb);
3223 50 h->c.alternate_scan = get_bits1(gb);
3224 } else
3225 7079 h->c.alternate_scan = 0;
3226 }
3227 /* Skip at this point when only parsing since the remaining
3228 * data is not useful for a parser and requires the
3229 * sprite_trajectory VLC to be initialized. */
3230
2/2
✓ Branch 0 taken 3546 times.
✓ Branch 1 taken 3583 times.
7129 if (parse_only)
3231 3546 goto end;
3232
3233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3583 times.
3583 if (h->c.alternate_scan) {
3234 ✗ ff_init_scantable(h->c.idsp.idct_permutation, &h->c.intra_scantable, ff_alternate_vertical_scan);
3235 ✗ ff_permute_scantable(h->permutated_intra_h_scantable, ff_alternate_vertical_scan,
3236 ✗ h->c.idsp.idct_permutation);
3237 } else {
3238 3583 ff_init_scantable(h->c.idsp.idct_permutation, &h->c.intra_scantable, ff_zigzag_direct);
3239 3583 ff_permute_scantable(h->permutated_intra_h_scantable, ff_alternate_horizontal_scan,
3240 3583 h->c.idsp.idct_permutation);
3241 }
3242 3583 ff_permute_scantable(h->permutated_intra_v_scantable, ff_alternate_vertical_scan,
3243 3583 h->c.idsp.idct_permutation);
3244
3245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3583 times.
3583 if (h->c.pict_type == AV_PICTURE_TYPE_S) {
3246 ✗ if((ctx->vol_sprite_usage == STATIC_SPRITE ||
3247 ✗ ctx->vol_sprite_usage == GMC_SPRITE)) {
3248 ✗ if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
3249 ✗ return AVERROR_INVALIDDATA;
3250 ✗ if (ctx->sprite_brightness_change)
3251 ✗ av_log(h->c.avctx, AV_LOG_ERROR,
3252 "sprite_brightness_change not supported\n");
3253 ✗ if (ctx->vol_sprite_usage == STATIC_SPRITE)
3254 ✗ av_log(h->c.avctx, AV_LOG_ERROR, "static sprite not supported\n");
3255 } else {
3256 ✗ memset(ctx->sprite_offset, 0, sizeof(ctx->sprite_offset));
3257 ✗ memset(ctx->sprite_delta, 0, sizeof(ctx->sprite_delta));
3258 }
3259 }
3260
3261 3583 ctx->f_code = 1;
3262 3583 ctx->b_code = 1;
3263
1/2
✓ Branch 0 taken 3583 times.
✗ Branch 1 not taken.
3583 if (ctx->shape != BIN_ONLY_SHAPE) {
3264 3583 h->c.chroma_qscale = h->c.qscale = get_bits(gb, ctx->quant_precision);
3265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3583 times.
3583 if (h->c.qscale == 0) {
3266 ✗ av_log(h->c.avctx, AV_LOG_ERROR,
3267 "Error, header damaged or not MPEG-4 header (qscale=0)\n");
3268 ✗ return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
3269 }
3270
3271
2/2
✓ Branch 0 taken 3107 times.
✓ Branch 1 taken 476 times.
3583 if (h->c.pict_type != AV_PICTURE_TYPE_I) {
3272 3107 ctx->f_code = get_bits(gb, 3); /* fcode_for */
3273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3107 times.
3107 if (ctx->f_code == 0) {
3274 ✗ av_log(h->c.avctx, AV_LOG_ERROR,
3275 "Error, header damaged or not MPEG-4 header (f_code=0)\n");
3276 ✗ ctx->f_code = 1;
3277 ✗ return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
3278 }
3279 }
3280
3281
2/2
✓ Branch 0 taken 648 times.
✓ Branch 1 taken 2935 times.
3583 if (h->c.pict_type == AV_PICTURE_TYPE_B) {
3282 648 ctx->b_code = get_bits(gb, 3);
3283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 648 times.
648 if (ctx->b_code == 0) {
3284 ✗ av_log(h->c.avctx, AV_LOG_ERROR,
3285 "Error, header damaged or not MPEG4 header (b_code=0)\n");
3286 ✗ ctx->b_code=1;
3287 ✗ return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly
3288 }
3289 }
3290
3291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3583 times.
3583 if (h->c.avctx->debug & FF_DEBUG_PICT_INFO) {
3292 ✗ av_log(h->c.avctx, AV_LOG_DEBUG,
3293 "qp:%d fc:%d,%d %c size:%d pro:%d alt:%d top:%d %cpel part:%d resync:%d w:%d a:%d rnd:%d vot:%d%s dc:%d ce:%d/%d/%d time:%"PRId64" tincr:%d\n",
3294 h->c.qscale, ctx->f_code, ctx->b_code,
3295 ✗ h->c.pict_type == AV_PICTURE_TYPE_I ? 'I' : (h->c.pict_type == AV_PICTURE_TYPE_P ? 'P' : (h->c.pict_type == AV_PICTURE_TYPE_B ? 'B' : 'S')),
3296 gb->size_in_bits,h->c.progressive_sequence, h->c.alternate_scan,
3297 ✗ h->c.top_field_first, h->c.quarter_sample ? 'q' : 'h',
3298 h->data_partitioning, ctx->resync_marker,
3299 ctx->num_sprite_warping_points, ctx->sprite_warping_accuracy,
3300 ✗ 1 - h->c.no_rounding, ctx->vo_type,
3301 ✗ ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
3302 ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
3303 ctx->cplx_estimation_trash_b,
3304 h->c.time,
3305 time_increment
3306 );
3307 }
3308
3309
1/2
✓ Branch 0 taken 3583 times.
✗ Branch 1 not taken.
3583 if (!ctx->scalability) {
3310
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3583 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3583 if (ctx->shape != RECT_SHAPE && h->c.pict_type != AV_PICTURE_TYPE_I)
3311 ✗ skip_bits1(gb); // vop shape coding type
3312 } else {
3313 ✗ if (ctx->enhancement_type) {
3314 ✗ int load_backward_shape = get_bits1(gb);
3315 ✗ if (load_backward_shape)
3316 ✗ av_log(h->c.avctx, AV_LOG_ERROR,
3317 "load backward shape isn't supported\n");
3318 }
3319 ✗ skip_bits(gb, 2); // ref_select_code
3320 }
3321 }
3322
3323 7166 h->c.dct_unquantize_intra = ctx->mpeg_quant ? ctx->dct_unquantize_mpeg2_intra
3324
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 3334 times.
3583 : ctx->dct_unquantize_h263_intra;
3325 // The following tells ff_mpv_reconstruct_mb() to unquantize iff mpeg_quant
3326
2/2
✓ Branch 0 taken 249 times.
✓ Branch 1 taken 3334 times.
3583 h->c.dct_unquantize_inter = ctx->mpeg_quant ? ctx->dct_unquantize_mpeg2_inter : NULL;
3327
3328 7129 end:
3329 /* detect buggy encoders which don't set the low_delay flag
3330 * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames
3331 * easily (although it's buggy too) */
3332
3/4
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 7077 times.
✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
7129 if (ctx->vo_type == 0 && ctx->vol_control_parameters == 0 &&
3333
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) {
3334 2 av_log(h->c.avctx, AV_LOG_WARNING,
3335 "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
3336 2 h->c.low_delay = 1;
3337 }
3338
3339 7129 h->picture_number++; // better than pic number==0 always ;)
3340
3341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7129 times.
7129 if (h->c.workaround_bugs & FF_BUG_EDGE) {
3342 ✗ h->c.h_edge_pos = h->c.width;
3343 ✗ h->c.v_edge_pos = h->c.height;
3344 }
3345 7129 return 0;
3346 }
3347
3348 2 static void decode_smpte_tc(Mpeg4DecContext *ctx, GetBitContext *gb)
3349 {
3350 2 AVCodecContext *const avctx = ctx->h.c.avctx;
3351
3352 2 skip_bits(gb, 16); /* Time_code[63..48] */
3353 2 check_marker(avctx, gb, "after Time_code[63..48]");
3354 2 skip_bits(gb, 16); /* Time_code[47..32] */
3355 2 check_marker(avctx, gb, "after Time_code[47..32]");
3356 2 skip_bits(gb, 16); /* Time_code[31..16] */
3357 2 check_marker(avctx, gb, "after Time_code[31..16]");
3358 2 skip_bits(gb, 16); /* Time_code[15..0] */
3359 2 check_marker(avctx, gb, "after Time_code[15..0]");
3360 2 skip_bits(gb, 4); /* reserved_bits */
3361 2 }
3362
3363 /**
3364 * Decode the next studio vop header.
3365 * @return <0 if something went wrong
3366 */
3367 2 static int decode_studio_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
3368 {
3369 2 H263DecContext *const h = &ctx->h;
3370
3371
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (get_bits_left(gb) <= 32)
3372 ✗ return 0;
3373
3374 2 h->partitioned_frame = 0;
3375 2 h->c.interlaced_dct = 0;
3376 2 h->decode_mb = mpeg4_decode_studio_mb;
3377
3378 2 decode_smpte_tc(ctx, gb);
3379
3380 2 skip_bits(gb, 10); /* temporal_reference */
3381 2 skip_bits(gb, 2); /* vop_structure */
3382 2 h->c.pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* vop_coding_type */
3383
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (get_bits1(gb)) { /* vop_coded */
3384 2 skip_bits1(gb); /* top_field_first */
3385 2 skip_bits1(gb); /* repeat_first_field */
3386 2 h->c.progressive_frame = get_bits1(gb) ^ 1; /* progressive_frame */
3387 }
3388
3389
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (h->c.pict_type == AV_PICTURE_TYPE_I) {
3390
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (get_bits1(gb))
3391 ✗ reset_studio_dc_predictors(ctx);
3392 }
3393
3394
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ctx->shape != BIN_ONLY_SHAPE) {
3395 2 h->c.alternate_scan = get_bits1(gb);
3396 2 h->c.frame_pred_frame_dct = get_bits1(gb);
3397 2 ctx->dct_precision = get_bits(gb, 2);
3398 2 h->c.intra_dc_precision = get_bits(gb, 2);
3399 2 h->c.q_scale_type = get_bits1(gb);
3400 }
3401
3402 2 ff_init_scantable(h->c.idsp.idct_permutation, &h->c.intra_scantable,
3403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 h->c.alternate_scan ? ff_alternate_vertical_scan : ff_zigzag_direct);
3404
3405 2 mpeg4_load_default_matrices(&h->c);
3406
3407 2 next_start_code_studio(gb);
3408 2 extension_and_user_data(&h->c, gb, 4);
3409
3410 2 return 0;
3411 }
3412
3413 ✗ static int decode_studiovisualobject(Mpeg4DecContext *ctx, GetBitContext *gb)
3414 {
3415 int visual_object_type;
3416
3417 ✗ skip_bits(gb, 4); /* visual_object_verid */
3418 ✗ visual_object_type = get_bits(gb, 4);
3419 ✗ if (visual_object_type != VOT_VIDEO_ID) {
3420 ✗ avpriv_request_sample(ctx->h.c.avctx, "VO type %u", visual_object_type);
3421 ✗ return AVERROR_PATCHWELCOME;
3422 }
3423
3424 ✗ next_start_code_studio(gb);
3425 ✗ extension_and_user_data(&ctx->h.c, gb, 1);
3426
3427 ✗ return 0;
3428 }
3429
3430 /**
3431 * Decode MPEG-4 headers.
3432 *
3433 * @param header If set the absence of a VOP is not treated as error; otherwise, it is treated as such.
3434 * @param parse_only If set, things only relevant to a decoder may be skipped;
3435 * furthermore, the VLC tables may be uninitialized.
3436 * @return <0 if an error occurred
3437 * FRAME_SKIPPED if a not coded VOP is found
3438 * 0 else
3439 */
3440 7622 int ff_mpeg4_parse_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb,
3441 int header, int parse_only)
3442 {
3443 7622 MPVContext *const s = &ctx->h.c;
3444 unsigned startcode, v;
3445 int ret;
3446 7622 int vol = 0;
3447
3448 /* search next start code */
3449 7622 align_get_bits(gb);
3450
3451 // If we have not switched to studio profile than we also did not switch bps
3452 // that means something else (like a previous instance) outside set bps which
3453 // would be inconsistent with the correct state, thus reset it
3454
3/4
✓ Branch 0 taken 7620 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 7620 times.
✗ Branch 3 not taken.
7622 if (!s->studio_profile && s->avctx->bits_per_raw_sample != 8)
3455 7620 s->avctx->bits_per_raw_sample = 0;
3456
3457
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7622 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
7622 if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
3458 ✗ skip_bits(gb, 24);
3459 ✗ if (get_bits(gb, 8) == 0xF0)
3460 ✗ goto end;
3461 }
3462
3463 7622 startcode = 0xff;
3464 for (;;) {
3465
2/2
✓ Branch 1 taken 483 times.
✓ Branch 2 taken 52566 times.
53049 if (get_bits_count(gb) >= gb->size_in_bits) {
3466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 483 times.
483 if (gb->size_in_bits == 8 &&
3467
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 483 times.
483 (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
3468 ✗ av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits);
3469 ✗ return FRAME_SKIPPED; // divx bug
3470
3/4
✓ Branch 0 taken 386 times.
✓ Branch 1 taken 97 times.
✓ Branch 3 taken 386 times.
✗ Branch 4 not taken.
483 } else if (header && get_bits_count(gb) == gb->size_in_bits) {
3471 386 return 0; // ordinary return value for parsing of extradata
3472 } else
3473 97 return AVERROR_INVALIDDATA; // end of stream
3474 }
3475
3476 /* use the bits after the test */
3477 52566 v = get_bits(gb, 8);
3478 52566 startcode = ((startcode << 8) | v) & 0xffffffff;
3479
3480
2/2
✓ Branch 0 taken 39429 times.
✓ Branch 1 taken 13137 times.
52566 if ((startcode & 0xFFFFFF00) != 0x100)
3481 39429 continue; // no startcode
3482
3483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13137 times.
13137 if (s->avctx->debug & FF_DEBUG_STARTCODE) {
3484 const char *name;
3485 ✗ if (startcode <= 0x11F)
3486 ✗ name = "Video Object Start";
3487 ✗ else if (startcode <= 0x12F)
3488 ✗ name = "Video Object Layer Start";
3489 ✗ else if (startcode <= 0x13F)
3490 ✗ name = "Reserved";
3491 ✗ else if (startcode <= 0x15F)
3492 ✗ name = "FGS bp start";
3493 ✗ else if (startcode <= 0x1AF)
3494 ✗ name = "Reserved";
3495 ✗ else if (startcode == 0x1B0)
3496 ✗ name = "Visual Object Seq Start";
3497 ✗ else if (startcode == 0x1B1)
3498 ✗ name = "Visual Object Seq End";
3499 ✗ else if (startcode == 0x1B2)
3500 ✗ name = "User Data";
3501 ✗ else if (startcode == 0x1B3)
3502 ✗ name = "Group of VOP start";
3503 ✗ else if (startcode == 0x1B4)
3504 ✗ name = "Video Session Error";
3505 ✗ else if (startcode == 0x1B5)
3506 ✗ name = "Visual Object Start";
3507 ✗ else if (startcode == 0x1B6)
3508 ✗ name = "Video Object Plane start";
3509 ✗ else if (startcode == 0x1B7)
3510 ✗ name = "slice start";
3511 ✗ else if (startcode == 0x1B8)
3512 ✗ name = "extension start";
3513 ✗ else if (startcode == 0x1B9)
3514 ✗ name = "fgs start";
3515 ✗ else if (startcode == 0x1BA)
3516 ✗ name = "FBA Object start";
3517 ✗ else if (startcode == 0x1BB)
3518 ✗ name = "FBA Object Plane start";
3519 ✗ else if (startcode == 0x1BC)
3520 ✗ name = "Mesh Object start";
3521 ✗ else if (startcode == 0x1BD)
3522 ✗ name = "Mesh Object Plane start";
3523 ✗ else if (startcode == 0x1BE)
3524 ✗ name = "Still Texture Object start";
3525 ✗ else if (startcode == 0x1BF)
3526 ✗ name = "Texture Spatial Layer start";
3527 ✗ else if (startcode == 0x1C0)
3528 ✗ name = "Texture SNR Layer start";
3529 ✗ else if (startcode == 0x1C1)
3530 ✗ name = "Texture Tile start";
3531 ✗ else if (startcode == 0x1C2)
3532 ✗ name = "Texture Shape Layer start";
3533 ✗ else if (startcode == 0x1C3)
3534 ✗ name = "stuffing start";
3535 ✗ else if (startcode <= 0x1C5)
3536 ✗ name = "Reserved";
3537 ✗ else if (startcode <= 0x1FF)
3538 ✗ name = "System start";
3539 else
3540 ✗ av_unreachable("Unexpected startcode");
3541 ✗ av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X %s at %d\n",
3542 startcode, name, get_bits_count(gb));
3543 }
3544
3545
4/4
✓ Branch 0 taken 11895 times.
✓ Branch 1 taken 1242 times.
✓ Branch 2 taken 1242 times.
✓ Branch 3 taken 10653 times.
13137 if (startcode >= 0x120 && startcode <= 0x12F) {
3546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1242 times.
1242 if (vol) {
3547 ✗ av_log(s->avctx, AV_LOG_WARNING, "Ignoring multiple VOL headers\n");
3548 ✗ continue;
3549 }
3550 1242 vol++;
3551
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1242 times.
1242 if ((ret = decode_vol_header(ctx, gb)) < 0)
3552 ✗ return ret;
3553
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 11805 times.
11895 } else if (startcode == USER_DATA_STARTCODE) {
3554 90 decode_user_data(ctx, gb);
3555
2/2
✓ Branch 0 taken 1006 times.
✓ Branch 1 taken 10799 times.
11805 } else if (startcode == GOP_STARTCODE) {
3556 1006 mpeg4_decode_gop_header(s, gb);
3557
2/2
✓ Branch 0 taken 1209 times.
✓ Branch 1 taken 9590 times.
10799 } else if (startcode == VOS_STARTCODE) {
3558 int profile, level;
3559 1209 mpeg4_decode_profile_level(s, gb, &profile, &level);
3560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1209 times.
1209 if (profile == AV_PROFILE_MPEG4_SIMPLE_STUDIO &&
3561 ✗ (level > 0 && level < 9)) {
3562 ✗ s->studio_profile = 1;
3563 ✗ next_start_code_studio(gb);
3564 ✗ extension_and_user_data(s, gb, 0);
3565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1209 times.
1209 } else if (s->studio_profile) {
3566 ✗ avpriv_request_sample(s->avctx, "Mix of studio and non studio profile");
3567 ✗ return AVERROR_PATCHWELCOME;
3568 }
3569 1209 s->avctx->profile = profile;
3570 1209 s->avctx->level = level;
3571
2/2
✓ Branch 0 taken 1209 times.
✓ Branch 1 taken 8381 times.
9590 } else if (startcode == VISUAL_OBJ_STARTCODE) {
3572
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1209 times.
1209 if (s->studio_profile) {
3573 ✗ if ((ret = decode_studiovisualobject(ctx, gb)) < 0)
3574 ✗ return ret;
3575 } else
3576 1209 mpeg4_decode_visual_object(s, gb);
3577
2/2
✓ Branch 0 taken 7139 times.
✓ Branch 1 taken 1242 times.
8381 } else if (startcode == VOP_STARTCODE) {
3578 7139 break;
3579 }
3580
3581 5998 align_get_bits(gb);
3582 5998 startcode = 0xff;
3583 }
3584
3585 7139 end:
3586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7139 times.
7139 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
3587 ✗ s->low_delay = 1;
3588
3589
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7137 times.
7139 if (s->studio_profile) {
3590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->avctx->bits_per_raw_sample) {
3591 ✗ av_log(s->avctx, AV_LOG_ERROR, "Missing VOL header\n");
3592 ✗ return AVERROR_INVALIDDATA;
3593 }
3594 2 return decode_studio_vop_header(ctx, gb);
3595 } else
3596 7137 return decode_vop_header(ctx, gb, parse_only);
3597 }
3598
3599 #if CONFIG_MPEG4_DECODER
3600 8 static av_cold void permute_quant_matrix(uint16_t matrix[64],
3601 const uint8_t new_perm[64],
3602 const uint8_t old_perm[64])
3603 {
3604 uint16_t tmp[64];
3605
3606 8 memcpy(tmp, matrix, sizeof(tmp));
3607
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 8 times.
520 for (int i = 0; i < 64; ++i)
3608 512 matrix[new_perm[i]] = tmp[old_perm[i]];
3609 8 }
3610
3611 4 static av_cold void switch_to_xvid_idct(AVCodecContext *const avctx,
3612 H263DecContext *const h)
3613 {
3614 uint8_t old_permutation[64];
3615
3616 4 memcpy(old_permutation, h->c.idsp.idct_permutation, sizeof(old_permutation));
3617
3618 4 avctx->idct_algo = FF_IDCT_XVID;
3619 4 ff_mpv_idct_init(&h->c);
3620 4 ff_permute_scantable(h->permutated_intra_h_scantable,
3621 4 h->c.alternate_scan ? ff_alternate_vertical_scan : ff_alternate_horizontal_scan,
3622
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 h->c.idsp.idct_permutation);
3623 4 ff_permute_scantable(h->permutated_intra_v_scantable, ff_alternate_vertical_scan,
3624 4 h->c.idsp.idct_permutation);
3625
3626 // Normal (i.e. non-studio) MPEG-4 does not use the chroma matrices.
3627 4 permute_quant_matrix(h->c.inter_matrix, h->c.idsp.idct_permutation, old_permutation);
3628 4 permute_quant_matrix(h->c.intra_matrix, h->c.idsp.idct_permutation, old_permutation);
3629 4 }
3630
3631 3584 void ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
3632 {
3633 3584 Mpeg4DecContext *ctx = avctx->priv_data;
3634 3584 H263DecContext *const h = &ctx->h;
3635
3636
5/6
✓ Branch 0 taken 3524 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 3524 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3418 times.
✓ Branch 5 taken 106 times.
3584 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
3637
1/2
✓ Branch 0 taken 3418 times.
✗ Branch 1 not taken.
3418 if (h->c.codec_tag == AV_RL32("XVID") ||
3638
1/2
✓ Branch 0 taken 3418 times.
✗ Branch 1 not taken.
3418 h->c.codec_tag == AV_RL32("XVIX") ||
3639
1/2
✓ Branch 0 taken 3418 times.
✗ Branch 1 not taken.
3418 h->c.codec_tag == AV_RL32("RMP4") ||
3640
1/2
✓ Branch 0 taken 3418 times.
✗ Branch 1 not taken.
3418 h->c.codec_tag == AV_RL32("ZMP4") ||
3641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
3418 h->c.codec_tag == AV_RL32("SIPP"))
3642 ✗ ctx->xvid_build = 0;
3643 }
3644
3645
5/6
✓ Branch 0 taken 3524 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 3524 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3418 times.
✓ Branch 5 taken 106 times.
3584 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
3646
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3418 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3418 if (h->c.codec_tag == AV_RL32("DIVX") && ctx->vo_type == 0 &&
3647 ✗ ctx->vol_control_parameters == 0)
3648 ✗ ctx->divx_version = 400; // divx 4
3649
3650
4/4
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 3524 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 57 times.
3584 if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
3651 3 ctx->divx_version =
3652 3 ctx->divx_build = -1;
3653 }
3654
3655
1/2
✓ Branch 0 taken 3584 times.
✗ Branch 1 not taken.
3584 if (h->c.workaround_bugs & FF_BUG_AUTODETECT) {
3656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
3584 if (h->c.codec_tag == AV_RL32("XVIX"))
3657 ✗ h->c.workaround_bugs |= FF_BUG_XVID_ILACE;
3658
3659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
3584 if (h->c.codec_tag == AV_RL32("UMP4"))
3660 ✗ h->c.workaround_bugs |= FF_BUG_UMP4;
3661
3662
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3584 if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
3663 ✗ h->c.workaround_bugs |= FF_BUG_QPEL_CHROMA;
3664
3665
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3584 if (ctx->divx_version > 502 && ctx->divx_build < 1814)
3666 ✗ h->c.workaround_bugs |= FF_BUG_QPEL_CHROMA2;
3667
3668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
3584 if (ctx->xvid_build <= 3U)
3669 ✗ h->padding_bug_score = 256 * 256 * 256 * 64;
3670
3671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
3584 if (ctx->xvid_build <= 1U)
3672 ✗ h->c.workaround_bugs |= FF_BUG_QPEL_CHROMA;
3673
3674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
3584 if (ctx->xvid_build <= 12U)
3675 ✗ h->c.workaround_bugs |= FF_BUG_EDGE;
3676
3677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
3584 if (ctx->xvid_build <= 32U)
3678 ✗ h->c.workaround_bugs |= FF_BUG_DC_CLIP;
3679
3680 #define SET_QPEL_FUNC(postfix1, postfix2) \
3681 h->c.qdsp.put_ ## postfix1 = ff_put_ ## postfix2; \
3682 h->c.qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
3683 h->c.qdsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
3684
3685
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
3584 if (ctx->lavc_build < 4653U)
3686 ✗ h->c.workaround_bugs |= FF_BUG_STD_QPEL;
3687
3688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
3584 if (ctx->lavc_build < 4655U)
3689 ✗ h->c.workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
3690
3691
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
3584 if (ctx->lavc_build < 4670U)
3692 ✗ h->c.workaround_bugs |= FF_BUG_EDGE;
3693
3694
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
3584 if (ctx->lavc_build <= 4712U)
3695 ✗ h->c.workaround_bugs |= FF_BUG_DC_CLIP;
3696
3697
1/2
✓ Branch 0 taken 3584 times.
✗ Branch 1 not taken.
3584 if ((ctx->lavc_build&0xFF) >= 100) {
3698
3/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 3564 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
3584 if (ctx->lavc_build > 3621476 && ctx->lavc_build < 3752552 &&
3699 ✗ (ctx->lavc_build < 3752037 || ctx->lavc_build > 3752191) // 3.2.1+
3700 )
3701 ✗ h->c.workaround_bugs |= FF_BUG_IEDGE;
3702 }
3703
3704
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
3584 if (ctx->divx_version >= 0)
3705 ✗ h->c.workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
3706
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3584 if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
3707 ✗ h->padding_bug_score = 256 * 256 * 256 * 64;
3708
3709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
3584 if (ctx->divx_version < 500U)
3710 ✗ h->c.workaround_bugs |= FF_BUG_EDGE;
3711
3712
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
3584 if (ctx->divx_version >= 0)
3713 ✗ h->c.workaround_bugs |= FF_BUG_HPEL_CHROMA;
3714 }
3715
3716
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
3584 if (h->c.workaround_bugs & FF_BUG_STD_QPEL) {
3717 ✗ SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
3718 ✗ SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
3719 ✗ SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
3720 ✗ SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
3721 ✗ SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
3722 ✗ SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
3723
3724 ✗ SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
3725 ✗ SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
3726 ✗ SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
3727 ✗ SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
3728 ✗ SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
3729 ✗ SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
3730 }
3731
3732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3584 times.
3584 if (avctx->debug & FF_DEBUG_BUGS)
3733 ✗ av_log(h->c.avctx, AV_LOG_DEBUG,
3734 "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
3735 h->c.workaround_bugs, ctx->lavc_build, ctx->xvid_build,
3736 ✗ ctx->divx_version, ctx->divx_build, h->divx_packed ? "p" : "");
3737
3738
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 3524 times.
3584 if (ctx->xvid_build >= 0 &&
3739
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) {
3740 4 switch_to_xvid_idct(avctx, h);
3741 }
3742 3584 }
3743
3744 3584 static int mpeg4_decode_picture_header(H263DecContext *const h)
3745 {
3746 3584 Mpeg4DecContext *const ctx = h263_to_mpeg4(h);
3747
3748 3584 h->skipped_last_frame = 0;
3749
3750
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3573 times.
3584 if (ctx->bitstream_buffer) {
3751 11 int buf_size = get_bits_left(&h->gb) / 8U;
3752 11 int bitstream_buffer_size = ctx->bitstream_buffer->size;
3753 11 const uint8_t *buf = h->gb.buffer;
3754
3755
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (h->divx_packed) {
3756
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 for (int i = 0; i < buf_size - 3; i++) {
3757
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) {
3758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (buf[i+3] == 0xB0) {
3759 ✗ av_log(h->c.avctx, AV_LOG_WARNING, "Discarding excessive bitstream in packed xvid\n");
3760 ✗ bitstream_buffer_size = 0;
3761 }
3762 11 break;
3763 }
3764 }
3765 }
3766 11 ctx->bitstream_buffer->size = 0;
3767
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
3768 7 int ret = init_get_bits8(&h->gb, ctx->bitstream_buffer->data,
3769 bitstream_buffer_size);
3770
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ret < 0)
3771 ✗ return ret;
3772 } else
3773 4 av_buffer_unref(&ctx->bitstream_buffer);
3774 }
3775
3776 3584 return ff_mpeg4_parse_picture_header(ctx, &h->gb, 0, 0);
3777 }
3778
3779 3419 int ff_mpeg4_frame_end(AVCodecContext *avctx, const AVPacket *pkt)
3780 {
3781 3419 Mpeg4DecContext *ctx = avctx->priv_data;
3782 3419 H263DecContext *const h = &ctx->h;
3783 int ret;
3784
3785 av_assert1(!ctx->bitstream_buffer || !ctx->bitstream_buffer->size);
3786
3787 /* divx 5.01+ bitstream reorder stuff */
3788
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3404 times.
3419 if (h->divx_packed) {
3789
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);
3790 15 int startcode_found = 0;
3791 15 uint8_t *buf = pkt->data;
3792 15 int buf_size = pkt->size;
3793
3794
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7 times.
15 if (buf_size - current_pos > 7) {
3795
3796 int i;
3797
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 for (i = current_pos; i < buf_size - 4; i++)
3798
3799
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 5 times.
13 if (buf[i] == 0 &&
3800
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 buf[i + 1] == 0 &&
3801
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 buf[i + 2] == 1 &&
3802
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 buf[i + 3] == 0xB6) {
3803 8 startcode_found = !(buf[i + 4] & 0x40);
3804 8 break;
3805 }
3806 }
3807
3808
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 7 times.
15 if (startcode_found) {
3809
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 if (!ctx->showed_packed_warning) {
3810 1 av_log(h->c.avctx, AV_LOG_INFO, "Video uses a non-standard and "
3811 "wasteful way to store B-frames ('packed B-frames'). "
3812 "Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.\n");
3813 1 ctx->showed_packed_warning = 1;
3814 }
3815 8 ret = av_buffer_replace(&ctx->bitstream_buffer, pkt->buf);
3816
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0)
3817 ✗ return ret;
3818
3819 8 ctx->bitstream_buffer->data = buf + current_pos;
3820 8 ctx->bitstream_buffer->size = buf_size - current_pos;
3821 }
3822 }
3823
3824 3419 return 0;
3825 }
3826
3827 #if HAVE_THREADS
3828 8 static av_cold void clear_context(MpegEncContext *s)
3829 {
3830 8 memset(&s->buffer_pools, 0, sizeof(s->buffer_pools));
3831 8 memset(&s->next_pic, 0, sizeof(s->next_pic));
3832 8 memset(&s->last_pic, 0, sizeof(s->last_pic));
3833 8 memset(&s->cur_pic, 0, sizeof(s->cur_pic));
3834
3835 8 memset(s->thread_context, 0, sizeof(s->thread_context));
3836
3837 8 s->ac_val_base = NULL;
3838 8 s->ac_val = NULL;
3839 8 memset(&s->sc, 0, sizeof(s->sc));
3840
3841 8 s->p_field_mv_table_base = NULL;
3842
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 8 times.
24 for (int i = 0; i < 2; i++)
3843
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 16 times.
48 for (int j = 0; j < 2; j++)
3844 32 s->p_field_mv_table[i][j] = NULL;
3845
3846 8 s->dc_val_base = NULL;
3847 8 s->coded_block_base = NULL;
3848 8 s->mbintra_table = NULL;
3849 8 s->cbp_table = NULL;
3850 8 s->pred_dir_table = NULL;
3851
3852 8 s->mbskip_table = NULL;
3853
3854 8 s->er.error_status_table = NULL;
3855 8 s->er.er_temp_buffer = NULL;
3856 8 s->mb_index2xy = NULL;
3857
3858 8 s->context_initialized = 0;
3859 8 s->context_reinit = 0;
3860 8 }
3861
3862 8 static av_cold int update_mpvctx(MpegEncContext *s, const MpegEncContext *s1)
3863 {
3864 8 AVCodecContext *avctx = s->avctx;
3865 // FIXME the following leads to a data race; instead copy only
3866 // the necessary fields.
3867 8 memcpy(s, s1, sizeof(*s));
3868 8 clear_context(s);
3869
3870 8 s->avctx = avctx;
3871
3872
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (s1->context_initialized) {
3873 8 int err = ff_mpv_common_init(s);
3874
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (err < 0)
3875 ✗ return err;
3876 }
3877 8 return 0;
3878 }
3879
3880 18 static int mpeg4_update_thread_context(AVCodecContext *dst,
3881 const AVCodecContext *src)
3882 {
3883 18 Mpeg4DecContext *s = dst->priv_data;
3884 18 const Mpeg4DecContext *s1 = src->priv_data;
3885 18 int init = s->h.c.context_initialized;
3886 int ret;
3887
3888
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 10 times.
18 if (!init) {
3889 8 ret = update_mpvctx(&s->h.c, &s1->h.c);
3890
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0)
3891 ✗ return ret;
3892 }
3893
3894 18 ret = ff_mpeg_update_thread_context(dst, src);
3895
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (ret < 0)
3896 ✗ return ret;
3897
3898 // copy all the necessary fields explicitly
3899 18 s->time_increment_bits = s1->time_increment_bits;
3900 18 s->shape = s1->shape;
3901 18 s->vol_sprite_usage = s1->vol_sprite_usage;
3902 18 s->sprite_brightness_change = s1->sprite_brightness_change;
3903 18 s->sprite_warping_accuracy = s1->sprite_warping_accuracy;
3904 18 s->num_sprite_warping_points = s1->num_sprite_warping_points;
3905 18 s->h.data_partitioning = s1->h.data_partitioning;
3906 18 s->mpeg_quant = s1->mpeg_quant;
3907 18 s->rvlc = s1->rvlc;
3908 18 s->resync_marker = s1->resync_marker;
3909 18 s->t_frame = s1->t_frame;
3910 18 s->new_pred = s1->new_pred;
3911 18 s->enhancement_type = s1->enhancement_type;
3912 18 s->scalability = s1->scalability;
3913 18 s->intra_dc_threshold = s1->intra_dc_threshold;
3914 18 s->h.divx_packed = s1->h.divx_packed;
3915 18 s->divx_version = s1->divx_version;
3916 18 s->divx_build = s1->divx_build;
3917 18 s->xvid_build = s1->xvid_build;
3918 18 s->lavc_build = s1->lavc_build;
3919 18 s->vo_type = s1->vo_type;
3920 18 s->showed_packed_warning = s1->showed_packed_warning;
3921 18 s->vol_control_parameters = s1->vol_control_parameters;
3922 18 s->cplx_estimation_trash_i = s1->cplx_estimation_trash_i;
3923 18 s->cplx_estimation_trash_p = s1->cplx_estimation_trash_p;
3924 18 s->cplx_estimation_trash_b = s1->cplx_estimation_trash_b;
3925 18 s->rgb = s1->rgb;
3926 18 s->h.c.studio_profile = s1->h.c.studio_profile;
3927
3928 18 s->h.skipped_last_frame = s1->h.skipped_last_frame;
3929 18 s->h.padding_bug_score = s1->h.padding_bug_score; // FIXME: racy
3930
3931 18 s->h.picture_number = s1->h.picture_number;
3932
3933 18 memcpy(s->sprite_shift, s1->sprite_shift, sizeof(s1->sprite_shift));
3934 18 memcpy(s->sprite_traj, s1->sprite_traj, sizeof(s1->sprite_traj));
3935
3936 18 return av_buffer_replace(&s->bitstream_buffer, s1->bitstream_buffer);
3937 }
3938
3939 12 static int mpeg4_update_thread_context_for_user(AVCodecContext *dst,
3940 const AVCodecContext *src)
3941 {
3942 12 H263DecContext *const h = dst->priv_data;
3943 12 const H263DecContext *const h1 = src->priv_data;
3944
3945 12 h->c.quarter_sample = h1->c.quarter_sample;
3946 12 h->divx_packed = h1->divx_packed;
3947
3948 12 return 0;
3949 }
3950 #endif
3951
3952 118 static av_cold void mpeg4_init_static(void)
3953 {
3954 static VLCElem vlc_buf[6498];
3955 118 VLCInitState state = VLC_INIT_STATE(vlc_buf);
3956
3957 118 VLC_INIT_STATIC_TABLE_FROM_LENGTHS(studio_luma_dc, STUDIO_INTRA_BITS, 19,
3958 &ff_mpeg4_studio_dc_luma[0][1], 2,
3959 &ff_mpeg4_studio_dc_luma[0][0], 2, 1,
3960 0, 0);
3961
3962 118 VLC_INIT_STATIC_TABLE_FROM_LENGTHS(studio_chroma_dc, STUDIO_INTRA_BITS, 19,
3963 &ff_mpeg4_studio_dc_chroma[0][1], 2,
3964 &ff_mpeg4_studio_dc_chroma[0][0], 2, 1,
3965 0, 0);
3966
3967
2/2
✓ Branch 0 taken 1416 times.
✓ Branch 1 taken 118 times.
1534 for (unsigned i = 0; i < 12; i++) {
3968 1416 studio_intra_tab[i] =
3969 1416 ff_vlc_init_tables_from_lengths(&state, STUDIO_INTRA_BITS, 24,
3970 1416 &ff_mpeg4_studio_intra[i][0][1], 2,
3971 1416 &ff_mpeg4_studio_intra[i][0][0], 2, 1,
3972 0, 0);
3973 }
3974
3975 static uint8_t mpeg4_rl_intra_table[2][2 * MAX_RUN + MAX_LEVEL + 3];
3976 118 ff_rl_init(&ff_mpeg4_rl_intra, mpeg4_rl_intra_table);
3977 118 ff_h263_init_rl_inter();
3978
3979 118 INIT_FIRST_VLC_RL(ff_mpeg4_rl_intra, 554);
3980
2/2
✓ Branch 0 taken 3776 times.
✓ Branch 1 taken 118 times.
3894 VLC_INIT_RL(ff_rvlc_rl_inter, 1072);
3981 118 INIT_FIRST_VLC_RL(ff_rvlc_rl_intra, 1072);
3982 118 VLC_INIT_STATIC_TABLE(dc_lum, DC_VLC_BITS, 10 /* 13 */,
3983 &ff_mpeg4_DCtab_lum[0][1], 2, 1,
3984 &ff_mpeg4_DCtab_lum[0][0], 2, 1, 0);
3985 118 VLC_INIT_STATIC_TABLE(dc_chrom, DC_VLC_BITS, 10 /* 13 */,
3986 &ff_mpeg4_DCtab_chrom[0][1], 2, 1,
3987 &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 0);
3988 118 VLC_INIT_STATIC_TABLE_FROM_LENGTHS(sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
3989 ff_sprite_trajectory_lens, 1,
3990 NULL, 0, 0, 0, 0);
3991 118 VLC_INIT_STATIC_SPARSE_TABLE(mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
3992 &ff_mb_type_b_tab[0][1], 2, 1,
3993 &ff_mb_type_b_tab[0][0], 2, 1,
3994 mb_type_b_map, 2, 2, 0);
3995 118 }
3996
3997 212 static av_cold int decode_init(AVCodecContext *avctx)
3998 {
3999 static AVOnce init_static_once = AV_ONCE_INIT;
4000 212 Mpeg4DecContext *ctx = avctx->priv_data;
4001 212 H263DecContext *const h = &ctx->h;
4002 MPVUnquantDSPContext unquant_dsp_ctx;
4003 int ret;
4004
4005 212 ctx->divx_version =
4006 212 ctx->divx_build =
4007 212 ctx->xvid_build =
4008 212 ctx->lavc_build = -1;
4009
4010
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 212 times.
212 if ((ret = ff_h263_decode_init(avctx)) < 0)
4011 ✗ return ret;
4012
4013 212 ff_mpv_unquantize_init(&unquant_dsp_ctx,
4014 avctx->flags & AV_CODEC_FLAG_BITEXACT, 0);
4015
4016 212 ctx->dct_unquantize_h263_intra = unquant_dsp_ctx.dct_unquantize_h263_intra;
4017 212 ctx->dct_unquantize_mpeg2_intra = unquant_dsp_ctx.dct_unquantize_mpeg2_intra;
4018 // dct_unquantize_inter is only used with MPEG-2 quantizers,
4019 // so that is all we keep.
4020 212 ctx->dct_unquantize_mpeg2_inter = unquant_dsp_ctx.dct_unquantize_mpeg2_inter;
4021
4022 212 h->c.y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
4023 212 h->c.c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
4024
4025 212 h->c.h263_pred = 1;
4026 212 h->c.low_delay = 0; /* default, might be overridden in the vol header during header parsing */
4027 212 h->decode_header = mpeg4_decode_picture_header;
4028 212 h->decode_mb = mpeg4_decode_mb;
4029 212 ctx->time_increment_bits = 4; /* default value for broken headers */
4030 212 ctx->quant_precision = 5;
4031
4032 212 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
4033
4034 212 ff_qpeldsp_init(&h->c.qdsp);
4035 212 ff_mpeg4videodsp_init(&ctx->mdsp);
4036
4037 212 ff_thread_once(&init_static_once, mpeg4_init_static);
4038
4039 /* Must be after initializing the MPEG-4 static tables */
4040
4/4
✓ Branch 0 taken 143 times.
✓ Branch 1 taken 69 times.
✓ Branch 2 taken 135 times.
✓ Branch 3 taken 8 times.
212 if (avctx->extradata_size && !avctx->internal->is_copy) {
4041 GetBitContext gb;
4042
4043
1/2
✓ Branch 1 taken 135 times.
✗ Branch 2 not taken.
135 if (init_get_bits8(&gb, avctx->extradata, avctx->extradata_size) >= 0)
4044 135 ff_mpeg4_parse_picture_header(ctx, &gb, 1, 0);
4045 }
4046
4047 212 return 0;
4048 }
4049
4050 ✗ static av_cold void mpeg4_flush(AVCodecContext *avctx)
4051 {
4052 ✗ Mpeg4DecContext *const ctx = avctx->priv_data;
4053
4054 ✗ av_buffer_unref(&ctx->bitstream_buffer);
4055 ✗ ff_mpeg_flush(avctx);
4056 ✗ }
4057
4058 212 static av_cold int mpeg4_close(AVCodecContext *avctx)
4059 {
4060 212 Mpeg4DecContext *const ctx = avctx->priv_data;
4061
4062 212 av_buffer_unref(&ctx->bitstream_buffer);
4063
4064 212 return ff_mpv_decode_close(avctx);
4065 }
4066
4067 #define OFFSET(x) offsetof(H263DecContext, x)
4068 #define FLAGS AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY
4069 static const AVOption mpeg4_options[] = {
4070 {"quarter_sample", "1/4 subpel MC", OFFSET(c.quarter_sample), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS},
4071 {"divx_packed", "divx style packed b frames", OFFSET(divx_packed), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS},
4072 {NULL}
4073 };
4074
4075 static const AVClass mpeg4_class = {
4076 .class_name = "MPEG4 Video Decoder",
4077 .item_name = av_default_item_name,
4078 .option = mpeg4_options,
4079 .version = LIBAVUTIL_VERSION_INT,
4080 };
4081
4082 const FFCodec ff_mpeg4_decoder = {
4083 .p.name = "mpeg4",
4084 CODEC_LONG_NAME("MPEG-4 part 2"),
4085 .p.type = AVMEDIA_TYPE_VIDEO,
4086 .p.id = AV_CODEC_ID_MPEG4,
4087 .priv_data_size = sizeof(Mpeg4DecContext),
4088 .init = decode_init,
4089 FF_CODEC_DECODE_CB(ff_h263_decode_frame),
4090 .close = mpeg4_close,
4091 .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
4092 AV_CODEC_CAP_DELAY | AV_CODEC_CAP_FRAME_THREADS,
4093 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
4094 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
4095 .flush = mpeg4_flush,
4096 .p.max_lowres = 3,
4097 .p.profiles = NULL_IF_CONFIG_SMALL(ff_mpeg4_video_profiles),
4098 UPDATE_THREAD_CONTEXT(mpeg4_update_thread_context),
4099 UPDATE_THREAD_CONTEXT_FOR_USER(mpeg4_update_thread_context_for_user),
4100 .p.priv_class = &mpeg4_class,
4101 .hw_configs = (const AVCodecHWConfigInternal *const []) {
4102 #if CONFIG_MPEG4_NVDEC_HWACCEL
4103 HWACCEL_NVDEC(mpeg4),
4104 #endif
4105 #if CONFIG_MPEG4_NVDEC_CUARRAY_HWACCEL
4106 HWACCEL_NVDEC_CUARRAY(mpeg4),
4107 #endif
4108 #if CONFIG_MPEG4_VAAPI_HWACCEL
4109 HWACCEL_VAAPI(mpeg4),
4110 #endif
4111 #if CONFIG_MPEG4_VDPAU_HWACCEL
4112 HWACCEL_VDPAU(mpeg4),
4113 #endif
4114 #if CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
4115 HWACCEL_VIDEOTOOLBOX(mpeg4),
4116 #endif
4117 NULL
4118 },
4119 };
4120 #endif /* CONFIG_MPEG4_DECODER */
4121