FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg4videodec.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 1359 2347 57.9%
Functions: 38 47 80.9%
Branches: 758 1547 49.0%

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