FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg4videodec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 1324 2292 57.8%
Functions: 35 43 81.4%
Branches: 741 1507 49.2%

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