FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg4videodec.c
Date: 2024-04-24 13:31:03
Exec Total Coverage
Lines: 1336 2303 58.0%
Functions: 35 43 81.4%
Branches: 750 1509 49.7%

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