FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg4videodec.c
Date: 2025-06-01 09:29:47
Exec Total Coverage
Lines: 1410 2399 58.8%
Functions: 42 51 82.4%
Branches: 770 1563 49.3%

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