FFmpeg coverage


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