FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg4videodec.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 1354 2343 57.8%
Functions: 38 47 80.9%
Branches: 756 1545 48.9%

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