FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg4videodec.c
Date: 2022-07-04 00:18:54
Exec Total Coverage
Lines: 1286 2195 58.6%
Branches: 738 1475 50.0%

Line Branch Exec Source
1 /*
2 * MPEG-4 decoder
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #define UNCHECKED_BITSTREAM_READER 1
24
25 #include "config_components.h"
26
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/thread.h"
30 #include "codec_internal.h"
31 #include "error_resilience.h"
32 #include "hwconfig.h"
33 #include "idctdsp.h"
34 #include "mpegutils.h"
35 #include "mpegvideo.h"
36 #include "mpegvideodata.h"
37 #include "mpegvideodec.h"
38 #include "mpeg4video.h"
39 #include "mpeg4videodata.h"
40 #include "mpeg4videodec.h"
41 #include "h263.h"
42 #include "h263data.h"
43 #include "h263dec.h"
44 #include "profiles.h"
45 #include "threadframe.h"
46 #include "xvididct.h"
47 #include "unary.h"
48
49 /* The defines below define the number of bits that are read at once for
50 * reading vlc values. Changing these may improve speed and data cache needs
51 * be aware though that decreasing them may need the number of stages that is
52 * passed to get_vlc* to be increased. */
53 #define SPRITE_TRAJ_VLC_BITS 6
54 #define DC_VLC_BITS 9
55 #define MB_TYPE_B_VLC_BITS 4
56 #define STUDIO_INTRA_BITS 9
57
58 static VLC dc_lum, dc_chrom;
59 static VLC sprite_trajectory;
60 static VLC mb_type_b_vlc;
61 static VLC studio_intra_tab[12];
62 static VLC studio_luma_dc;
63 static VLC studio_chroma_dc;
64
65 static const uint8_t mpeg4_block_count[4] = { 0, 6, 8, 12 };
66
67 static const int mb_type_b_map[4] = {
68 MB_TYPE_DIRECT2 | MB_TYPE_L0L1,
69 MB_TYPE_L0L1 | MB_TYPE_16x16,
70 MB_TYPE_L1 | MB_TYPE_16x16,
71 MB_TYPE_L0 | MB_TYPE_16x16,
72 };
73
74 1350 void ff_mpeg4_decode_studio(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb,
75 uint8_t *dest_cr, int block_size, int uvlinesize,
76 int dct_linesize, int dct_offset)
77 {
78 1350 Mpeg4DecContext *const ctx = (Mpeg4DecContext*)s;
79 1350 const int act_block_size = block_size * 2;
80
81
2/2
✓ Branch 0 taken 667 times.
✓ Branch 1 taken 683 times.
1350 if (ctx->dpcm_direction == 0) {
82 667 s->idsp.idct_put(dest_y, dct_linesize, (int16_t*)ctx->block32[0]);
83 667 s->idsp.idct_put(dest_y + act_block_size, dct_linesize, (int16_t*)ctx->block32[1]);
84 667 s->idsp.idct_put(dest_y + dct_offset, dct_linesize, (int16_t*)ctx->block32[2]);
85 667 s->idsp.idct_put(dest_y + dct_offset + act_block_size, dct_linesize, (int16_t*)ctx->block32[3]);
86
87 667 dct_linesize = uvlinesize << s->interlaced_dct;
88
1/2
✓ Branch 0 taken 667 times.
✗ Branch 1 not taken.
667 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
89
90 667 s->idsp.idct_put(dest_cb, dct_linesize, (int16_t*)ctx->block32[4]);
91 667 s->idsp.idct_put(dest_cr, dct_linesize, (int16_t*)ctx->block32[5]);
92 667 s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, (int16_t*)ctx->block32[6]);
93 667 s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, (int16_t*)ctx->block32[7]);
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 667 times.
667 if (!s->chroma_x_shift){ //Chroma444
95 s->idsp.idct_put(dest_cb + act_block_size, dct_linesize, (int16_t*)ctx->block32[8]);
96 s->idsp.idct_put(dest_cr + act_block_size, dct_linesize, (int16_t*)ctx->block32[9]);
97 s->idsp.idct_put(dest_cb + act_block_size + dct_offset, dct_linesize, (int16_t*)ctx->block32[10]);
98 s->idsp.idct_put(dest_cr + act_block_size + dct_offset, dct_linesize, (int16_t*)ctx->block32[11]);
99 }
100
1/2
✓ Branch 0 taken 683 times.
✗ Branch 1 not taken.
683 } else if (ctx->dpcm_direction == 1) {
101 683 uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr};
102 683 int linesize[3] = {dct_linesize, uvlinesize, uvlinesize};
103
2/2
✓ Branch 0 taken 2049 times.
✓ Branch 1 taken 683 times.
2732 for (int i = 0; i < 3; i++) {
104 2049 const uint16_t *src = ctx->dpcm_macroblock[i];
105
2/2
✓ Branch 0 taken 1366 times.
✓ Branch 1 taken 683 times.
2049 int vsub = i ? s->chroma_y_shift : 0;
106
2/2
✓ Branch 0 taken 1366 times.
✓ Branch 1 taken 683 times.
2049 int hsub = i ? s->chroma_x_shift : 0;
107 2049 int lowres = s->avctx->lowres;
108 2049 int step = 1 << lowres;
109
2/2
✓ Branch 0 taken 32784 times.
✓ Branch 1 taken 2049 times.
34833 for (int h = 0; h < (16 >> (vsub + lowres)); h++){
110
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)
111 349696 dest_pcm[i][w] = src[idx];
112 32784 dest_pcm[i] += linesize[i] / 2;
113 32784 src += (16 >> hsub) * step;
114 }
115 }
116 } else {
117 uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr};
118 int linesize[3] = {dct_linesize, uvlinesize, uvlinesize};
119 av_assert2(ctx->dpcm_direction == -1);
120 for (int i = 0; i < 3; i++) {
121 const uint16_t *src = ctx->dpcm_macroblock[i];
122 int vsub = i ? s->chroma_y_shift : 0;
123 int hsub = i ? s->chroma_x_shift : 0;
124 int lowres = s->avctx->lowres;
125 int step = 1 << lowres;
126 dest_pcm[i] += (linesize[i] / 2) * ((16 >> vsub) - 1);
127 for (int h = (16 >> (vsub + lowres)) - 1; h >= 0; h--){
128 for (int w = (16 >> (hsub + lowres)) - 1, idx = 0; w >= 0; w--, idx += step)
129 dest_pcm[i][w] = src[idx];
130 src += step * (16 >> hsub);
131 dest_pcm[i] -= linesize[i] / 2;
132 }
133 }
134 }
135 1350 }
136
137 /**
138 * Predict the ac.
139 * @param n block index (0-3 are luma, 4-5 are chroma)
140 * @param dir the ac prediction direction
141 */
142 1299666 void ff_mpeg4_pred_ac(MpegEncContext *s, int16_t *block, int n, int dir)
143 {
144 int i;
145 int16_t *ac_val, *ac_val1;
146 1299666 int8_t *const qscale_table = s->current_picture.qscale_table;
147
148 /* find prediction */
149 1299666 ac_val = &s->ac_val[0][0][0] + s->block_index[n] * 16;
150 1299666 ac_val1 = ac_val;
151
2/2
✓ Branch 0 taken 30084 times.
✓ Branch 1 taken 1269582 times.
1299666 if (s->ac_pred) {
152
2/2
✓ Branch 0 taken 20713 times.
✓ Branch 1 taken 9371 times.
30084 if (dir == 0) {
153 20713 const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
154 /* left prediction */
155 20713 ac_val -= 16;
156
157
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] ||
158 n == 1 || n == 3) {
159 /* same qscale */
160
2/2
✓ Branch 0 taken 144991 times.
✓ Branch 1 taken 20713 times.
165704 for (i = 1; i < 8; i++)
161 144991 block[s->idsp.idct_permutation[i << 3]] += ac_val[i];
162 } else {
163 /* different qscale, we must rescale */
164 for (i = 1; i < 8; i++)
165 block[s->idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
166 }
167 } else {
168 9371 const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
169 /* top prediction */
170 9371 ac_val -= 16 * s->block_wrap[n];
171
172
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] ||
173
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 n == 2 || n == 3) {
174 /* same qscale */
175
2/2
✓ Branch 0 taken 65597 times.
✓ Branch 1 taken 9371 times.
74968 for (i = 1; i < 8; i++)
176 65597 block[s->idsp.idct_permutation[i]] += ac_val[i + 8];
177 } else {
178 /* different qscale, we must rescale */
179 for (i = 1; i < 8; i++)
180 block[s->idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
181 }
182 }
183 }
184 /* left copy */
185
2/2
✓ Branch 0 taken 9097662 times.
✓ Branch 1 taken 1299666 times.
10397328 for (i = 1; i < 8; i++)
186 9097662 ac_val1[i] = block[s->idsp.idct_permutation[i << 3]];
187
188 /* top copy */
189
2/2
✓ Branch 0 taken 9097662 times.
✓ Branch 1 taken 1299666 times.
10397328 for (i = 1; i < 8; i++)
190 9097662 ac_val1[8 + i] = block[s->idsp.idct_permutation[i]];
191 1299666 }
192
193 /**
194 * check if the next stuff is a resync marker or the end.
195 * @return 0 if not
196 */
197 1427120 static inline int mpeg4_is_resync(Mpeg4DecContext *ctx)
198 {
199 1427120 MpegEncContext *s = &ctx->m;
200 1427120 int bits_count = get_bits_count(&s->gb);
201 1427120 int v = show_bits(&s->gb, 16);
202
203
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1427120 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1427120 if (s->workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker)
204 return 0;
205
206
2/2
✓ Branch 0 taken 3299 times.
✓ Branch 1 taken 1423821 times.
1427120 while (v <= 0xFF) {
207
2/2
✓ Branch 0 taken 2300 times.
✓ Branch 1 taken 999 times.
3299 if (s->pict_type == AV_PICTURE_TYPE_B ||
208
3/4
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 2124 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 176 times.
2300 (v >> (8 - s->pict_type) != 1) || s->partitioned_frame)
209 break;
210 skip_bits(&s->gb, 8 + s->pict_type);
211 bits_count += 8 + s->pict_type;
212 v = show_bits(&s->gb, 16);
213 }
214
215
2/2
✓ Branch 0 taken 11669 times.
✓ Branch 1 taken 1415451 times.
1427120 if (bits_count + 8 >= s->gb.size_in_bits) {
216 11669 v >>= 8;
217 11669 v |= 0x7F >> (7 - (bits_count & 7));
218
219
2/2
✓ Branch 0 taken 11352 times.
✓ Branch 1 taken 317 times.
11669 if (v == 0x7F)
220 11352 return s->mb_num;
221 } else {
222
2/2
✓ Branch 0 taken 4098 times.
✓ Branch 1 taken 1411353 times.
1415451 if (v == ff_mpeg4_resync_prefix[bits_count & 7]) {
223 int len, mb_num;
224 4098 int mb_num_bits = av_log2(s->mb_num - 1) + 1;
225 4098 GetBitContext gb = s->gb;
226
227 4098 skip_bits(&s->gb, 1);
228 4098 align_get_bits(&s->gb);
229
230
1/2
✓ Branch 0 taken 72133 times.
✗ Branch 1 not taken.
72133 for (len = 0; len < 32; len++)
231
2/2
✓ Branch 1 taken 4098 times.
✓ Branch 2 taken 68035 times.
72133 if (get_bits1(&s->gb))
232 4098 break;
233
234 4098 mb_num = get_bits(&s->gb, mb_num_bits);
235
3/6
✓ Branch 0 taken 4098 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4098 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 4098 times.
4098 if (!mb_num || mb_num > s->mb_num || get_bits_count(&s->gb)+6 > s->gb.size_in_bits)
236 mb_num= -1;
237
238 4098 s->gb = gb;
239
240
2/2
✓ Branch 1 taken 4096 times.
✓ Branch 2 taken 2 times.
4098 if (len >= ff_mpeg4_get_video_packet_prefix_length(s))
241 4096 return mb_num;
242 }
243 }
244 1411672 return 0;
245 }
246
247 static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb)
248 {
249 MpegEncContext *s = &ctx->m;
250 int a = 2 << s->sprite_warping_accuracy;
251 int rho = 3 - s->sprite_warping_accuracy;
252 int r = 16 / a;
253 int alpha = 1;
254 int beta = 0;
255 int w = s->width;
256 int h = s->height;
257 int min_ab, i, w2, h2, w3, h3;
258 int sprite_ref[4][2];
259 int virtual_ref[2][2];
260 int64_t sprite_offset[2][2];
261 int64_t sprite_delta[2][2];
262
263 // only true for rectangle shapes
264 const int vop_ref[4][2] = { { 0, 0 }, { s->width, 0 },
265 { 0, s->height }, { s->width, s->height } };
266 int d[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } };
267
268 if (w <= 0 || h <= 0)
269 return AVERROR_INVALIDDATA;
270
271 for (i = 0; i < ctx->num_sprite_warping_points; i++) {
272 int length;
273 int x = 0, y = 0;
274
275 length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 2);
276 if (length > 0)
277 x = get_xbits(gb, length);
278
279 if (!(ctx->divx_version == 500 && ctx->divx_build == 413))
280 check_marker(s->avctx, gb, "before sprite_trajectory");
281
282 length = get_vlc2(gb, sprite_trajectory.table, SPRITE_TRAJ_VLC_BITS, 2);
283 if (length > 0)
284 y = get_xbits(gb, length);
285
286 check_marker(s->avctx, gb, "after sprite_trajectory");
287 ctx->sprite_traj[i][0] = d[i][0] = x;
288 ctx->sprite_traj[i][1] = d[i][1] = y;
289 }
290 for (; i < 4; i++)
291 ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0;
292
293 while ((1 << alpha) < w)
294 alpha++;
295 while ((1 << beta) < h)
296 beta++; /* typo in the MPEG-4 std for the definition of w' and h' */
297 w2 = 1 << alpha;
298 h2 = 1 << beta;
299
300 // Note, the 4th point isn't used for GMC
301 if (ctx->divx_version == 500 && ctx->divx_build == 413) {
302 sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0];
303 sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1];
304 sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0];
305 sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1];
306 sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0];
307 sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1];
308 } else {
309 sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]);
310 sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]);
311 sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]);
312 sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]);
313 sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]);
314 sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]);
315 }
316 /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]);
317 * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */
318
319 /* This is mostly identical to the MPEG-4 std (and is totally unreadable
320 * because of that...). Perhaps it should be reordered to be more readable.
321 * The idea behind this virtual_ref mess is to be able to use shifts later
322 * per pixel instead of divides so the distance between points is converted
323 * from w&h based to w2&h2 based which are of the 2^x form. */
324 virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) +
325 ROUNDED_DIV(((w - w2) *
326 (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
327 w2 * (r * sprite_ref[1][0] - 16LL * vop_ref[1][0])), w);
328 virtual_ref[0][1] = 16 * vop_ref[0][1] +
329 ROUNDED_DIV(((w - w2) *
330 (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
331 w2 * (r * sprite_ref[1][1] - 16LL * vop_ref[1][1])), w);
332 virtual_ref[1][0] = 16 * vop_ref[0][0] +
333 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) +
334 h2 * (r * sprite_ref[2][0] - 16LL * vop_ref[2][0])), h);
335 virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) +
336 ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) +
337 h2 * (r * sprite_ref[2][1] - 16LL * vop_ref[2][1])), h);
338
339 switch (ctx->num_sprite_warping_points) {
340 case 0:
341 sprite_offset[0][0] =
342 sprite_offset[0][1] =
343 sprite_offset[1][0] =
344 sprite_offset[1][1] = 0;
345 sprite_delta[0][0] = a;
346 sprite_delta[0][1] =
347 sprite_delta[1][0] = 0;
348 sprite_delta[1][1] = a;
349 ctx->sprite_shift[0] =
350 ctx->sprite_shift[1] = 0;
351 break;
352 case 1: // GMC only
353 sprite_offset[0][0] = sprite_ref[0][0] - a * vop_ref[0][0];
354 sprite_offset[0][1] = sprite_ref[0][1] - a * vop_ref[0][1];
355 sprite_offset[1][0] = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) -
356 a * (vop_ref[0][0] / 2);
357 sprite_offset[1][1] = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) -
358 a * (vop_ref[0][1] / 2);
359 sprite_delta[0][0] = a;
360 sprite_delta[0][1] =
361 sprite_delta[1][0] = 0;
362 sprite_delta[1][1] = a;
363 ctx->sprite_shift[0] =
364 ctx->sprite_shift[1] = 0;
365 break;
366 case 2:
367 sprite_offset[0][0] = ((int64_t) sprite_ref[0][0] * (1 << alpha + rho)) +
368 ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
369 ((int64_t) -vop_ref[0][0]) +
370 ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
371 ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
372 sprite_offset[0][1] = ((int64_t) sprite_ref[0][1] * (1 << alpha + rho)) +
373 ((int64_t) -r * sprite_ref[0][1] + virtual_ref[0][1]) *
374 ((int64_t) -vop_ref[0][0]) +
375 ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) *
376 ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1));
377 sprite_offset[1][0] = (((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
378 ((int64_t)-2 * vop_ref[0][0] + 1) +
379 ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) *
380 ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
381 (int64_t) sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1)));
382 sprite_offset[1][1] = (((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) *
383 ((int64_t)-2 * vop_ref[0][0] + 1) +
384 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) *
385 ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r *
386 (int64_t) sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1)));
387 sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
388 sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]);
389 sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]);
390 sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]);
391
392 ctx->sprite_shift[0] = alpha + rho;
393 ctx->sprite_shift[1] = alpha + rho + 2;
394 break;
395 case 3:
396 min_ab = FFMIN(alpha, beta);
397 w3 = w2 >> min_ab;
398 h3 = h2 >> min_ab;
399 sprite_offset[0][0] = ((int64_t)sprite_ref[0][0] * (1 << (alpha + beta + rho - min_ab))) +
400 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-vop_ref[0][0]) +
401 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-vop_ref[0][1]) +
402 ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
403 sprite_offset[0][1] = ((int64_t)sprite_ref[0][1] * (1 << (alpha + beta + rho - min_ab))) +
404 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-vop_ref[0][0]) +
405 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-vop_ref[0][1]) +
406 ((int64_t)1 << (alpha + beta + rho - min_ab - 1));
407 sprite_offset[1][0] = ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-2 * vop_ref[0][0] + 1) +
408 ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-2 * vop_ref[0][1] + 1) +
409 (int64_t)2 * w2 * h3 * r * sprite_ref[0][0] - 16 * w2 * h3 +
410 ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
411 sprite_offset[1][1] = ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-2 * vop_ref[0][0] + 1) +
412 ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-2 * vop_ref[0][1] + 1) +
413 (int64_t)2 * w2 * h3 * r * sprite_ref[0][1] - 16 * w2 * h3 +
414 ((int64_t)1 << (alpha + beta + rho - min_ab + 1));
415 sprite_delta[0][0] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[0][0]) * h3;
416 sprite_delta[0][1] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[1][0]) * w3;
417 sprite_delta[1][0] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[0][1]) * h3;
418 sprite_delta[1][1] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[1][1]) * w3;
419
420 ctx->sprite_shift[0] = alpha + beta + rho - min_ab;
421 ctx->sprite_shift[1] = alpha + beta + rho - min_ab + 2;
422 break;
423 }
424 /* try to simplify the situation */
425 if (sprite_delta[0][0] == a << ctx->sprite_shift[0] &&
426 sprite_delta[0][1] == 0 &&
427 sprite_delta[1][0] == 0 &&
428 sprite_delta[1][1] == a << ctx->sprite_shift[0]) {
429 sprite_offset[0][0] >>= ctx->sprite_shift[0];
430 sprite_offset[0][1] >>= ctx->sprite_shift[0];
431 sprite_offset[1][0] >>= ctx->sprite_shift[1];
432 sprite_offset[1][1] >>= ctx->sprite_shift[1];
433 sprite_delta[0][0] = a;
434 sprite_delta[0][1] = 0;
435 sprite_delta[1][0] = 0;
436 sprite_delta[1][1] = a;
437 ctx->sprite_shift[0] = 0;
438 ctx->sprite_shift[1] = 0;
439 s->real_sprite_warping_points = 1;
440 } else {
441 int shift_y = 16 - ctx->sprite_shift[0];
442 int shift_c = 16 - ctx->sprite_shift[1];
443
444 for (i = 0; i < 2; i++) {
445 if (shift_c < 0 || shift_y < 0 ||
446 FFABS( sprite_offset[0][i]) >= INT_MAX >> shift_y ||
447 FFABS( sprite_offset[1][i]) >= INT_MAX >> shift_c ||
448 FFABS( sprite_delta[0][i]) >= INT_MAX >> shift_y ||
449 FFABS( sprite_delta[1][i]) >= INT_MAX >> shift_y
450 ) {
451 avpriv_request_sample(s->avctx, "Too large sprite shift, delta or offset");
452 goto overflow;
453 }
454 }
455
456 for (i = 0; i < 2; i++) {
457 sprite_offset[0][i] *= 1 << shift_y;
458 sprite_offset[1][i] *= 1 << shift_c;
459 sprite_delta[0][i] *= 1 << shift_y;
460 sprite_delta[1][i] *= 1 << shift_y;
461 ctx->sprite_shift[i] = 16;
462
463 }
464 for (i = 0; i < 2; i++) {
465 int64_t sd[2] = {
466 sprite_delta[i][0] - a * (1LL<<16),
467 sprite_delta[i][1] - a * (1LL<<16)
468 };
469
470 if (llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
471 llabs(sprite_offset[0][i] + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
472 llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL) + sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
473 llabs(sprite_delta[i][0] * (w+16LL)) >= INT_MAX ||
474 llabs(sprite_delta[i][1] * (h+16LL)) >= INT_MAX ||
475 llabs(sd[0]) >= INT_MAX ||
476 llabs(sd[1]) >= INT_MAX ||
477 llabs(sprite_offset[0][i] + sd[0] * (w+16LL)) >= INT_MAX ||
478 llabs(sprite_offset[0][i] + sd[1] * (h+16LL)) >= INT_MAX ||
479 llabs(sprite_offset[0][i] + sd[0] * (w+16LL) + sd[1] * (h+16LL)) >= INT_MAX
480 ) {
481 avpriv_request_sample(s->avctx, "Overflow on sprite points");
482 goto overflow;
483 }
484 }
485 s->real_sprite_warping_points = ctx->num_sprite_warping_points;
486 }
487
488 for (i = 0; i < 4; i++) {
489 s->sprite_offset[i&1][i>>1] = sprite_offset[i&1][i>>1];
490 s->sprite_delta [i&1][i>>1] = sprite_delta [i&1][i>>1];
491 }
492
493 return 0;
494 overflow:
495 memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
496 memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
497 return AVERROR_PATCHWELCOME;
498 }
499
500 static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb) {
501 MpegEncContext *s = &ctx->m;
502 int len = FFMIN(ctx->time_increment_bits + 3, 15);
503
504 get_bits(gb, len);
505 if (get_bits1(gb))
506 get_bits(gb, len);
507 check_marker(s->avctx, gb, "after new_pred");
508
509 return 0;
510 }
511
512 /**
513 * Decode the next video packet.
514 * @return <0 if something went wrong
515 */
516 3867 int ff_mpeg4_decode_video_packet_header(Mpeg4DecContext *ctx)
517 {
518 3867 MpegEncContext *s = &ctx->m;
519
520 3867 int mb_num_bits = av_log2(s->mb_num - 1) + 1;
521 3867 int header_extension = 0, mb_num, len;
522
523 /* is there enough space left for a video packet + header */
524
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3867 times.
3867 if (get_bits_count(&s->gb) > s->gb.size_in_bits - 20)
525 return AVERROR_INVALIDDATA;
526
527
1/2
✓ Branch 0 taken 68178 times.
✗ Branch 1 not taken.
68178 for (len = 0; len < 32; len++)
528
2/2
✓ Branch 1 taken 3867 times.
✓ Branch 2 taken 64311 times.
68178 if (get_bits1(&s->gb))
529 3867 break;
530
531
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3867 times.
3867 if (len != ff_mpeg4_get_video_packet_prefix_length(s)) {
532 av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
533 return AVERROR_INVALIDDATA;
534 }
535
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3867 times.
3867 if (ctx->shape != RECT_SHAPE) {
537 header_extension = get_bits1(&s->gb);
538 // FIXME more stuff here
539 }
540
541 3867 mb_num = get_bits(&s->gb, mb_num_bits);
542
2/4
✓ Branch 0 taken 3867 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3867 times.
3867 if (mb_num >= s->mb_num || !mb_num) {
543 av_log(s->avctx, AV_LOG_ERROR,
544 "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
545 return AVERROR_INVALIDDATA;
546 }
547
548 3867 s->mb_x = mb_num % s->mb_width;
549 3867 s->mb_y = mb_num / s->mb_width;
550
551
1/2
✓ Branch 0 taken 3867 times.
✗ Branch 1 not taken.
3867 if (ctx->shape != BIN_ONLY_SHAPE) {
552 3867 int qscale = get_bits(&s->gb, s->quant_precision);
553
1/2
✓ Branch 0 taken 3867 times.
✗ Branch 1 not taken.
3867 if (qscale)
554 3867 s->chroma_qscale = s->qscale = qscale;
555 }
556
557
1/2
✓ Branch 0 taken 3867 times.
✗ Branch 1 not taken.
3867 if (ctx->shape == RECT_SHAPE)
558 3867 header_extension = get_bits1(&s->gb);
559
560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3867 times.
3867 if (header_extension) {
561 int time_incr = 0;
562
563 while (get_bits1(&s->gb) != 0)
564 time_incr++;
565
566 check_marker(s->avctx, &s->gb, "before time_increment in video packed header");
567 skip_bits(&s->gb, ctx->time_increment_bits); /* time_increment */
568 check_marker(s->avctx, &s->gb, "before vop_coding_type in video packed header");
569
570 skip_bits(&s->gb, 2); /* vop coding type */
571 // FIXME not rect stuff here
572
573 if (ctx->shape != BIN_ONLY_SHAPE) {
574 skip_bits(&s->gb, 3); /* intra dc vlc threshold */
575 // FIXME don't just ignore everything
576 if (s->pict_type == AV_PICTURE_TYPE_S &&
577 ctx->vol_sprite_usage == GMC_SPRITE) {
578 if (mpeg4_decode_sprite_trajectory(ctx, &s->gb) < 0)
579 return AVERROR_INVALIDDATA;
580 av_log(s->avctx, AV_LOG_ERROR, "untested\n");
581 }
582
583 // FIXME reduced res stuff here
584
585 if (s->pict_type != AV_PICTURE_TYPE_I) {
586 int f_code = get_bits(&s->gb, 3); /* fcode_for */
587 if (f_code == 0)
588 av_log(s->avctx, AV_LOG_ERROR,
589 "Error, video packet header damaged (f_code=0)\n");
590 }
591 if (s->pict_type == AV_PICTURE_TYPE_B) {
592 int b_code = get_bits(&s->gb, 3);
593 if (b_code == 0)
594 av_log(s->avctx, AV_LOG_ERROR,
595 "Error, video packet header damaged (b_code=0)\n");
596 }
597 }
598 }
599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3867 times.
3867 if (ctx->new_pred)
600 decode_new_pred(ctx, &s->gb);
601
602 3867 return 0;
603 }
604
605 30 static void reset_studio_dc_predictors(MpegEncContext *s)
606 {
607 /* Reset DC Predictors */
608 30 s->last_dc[0] =
609 30 s->last_dc[1] =
610 30 s->last_dc[2] = 1 << (s->avctx->bits_per_raw_sample + s->dct_precision + s->intra_dc_precision - 1);
611 30 }
612
613 /**
614 * Decode the next video packet.
615 * @return <0 if something went wrong
616 */
617 30 int ff_mpeg4_decode_studio_slice_header(Mpeg4DecContext *ctx)
618 {
619 30 MpegEncContext *s = &ctx->m;
620 30 GetBitContext *gb = &s->gb;
621 unsigned vlc_len;
622 uint16_t mb_num;
623
624
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) {
625 30 vlc_len = av_log2(s->mb_width * s->mb_height) + 1;
626 30 mb_num = get_bits(gb, vlc_len);
627
628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (mb_num >= s->mb_num)
629 return AVERROR_INVALIDDATA;
630
631 30 s->mb_x = mb_num % s->mb_width;
632 30 s->mb_y = mb_num / s->mb_width;
633
634
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (ctx->shape != BIN_ONLY_SHAPE)
635 30 s->qscale = mpeg_get_qscale(s);
636
637
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if (get_bits1(gb)) { /* slice_extension_flag */
638 skip_bits1(gb); /* intra_slice */
639 skip_bits1(gb); /* slice_VOP_id_enable */
640 skip_bits(gb, 6); /* slice_VOP_id */
641 while (get_bits1(gb)) /* extra_bit_slice */
642 skip_bits(gb, 8); /* extra_information_slice */
643 }
644
645 30 reset_studio_dc_predictors(s);
646 }
647 else {
648 return AVERROR_INVALIDDATA;
649 }
650
651 30 return 0;
652 }
653
654 /**
655 * Get the average motion vector for a GMC MB.
656 * @param n either 0 for the x component or 1 for y
657 * @return the average MV for a GMC MB
658 */
659 static inline int get_amv(Mpeg4DecContext *ctx, int n)
660 {
661 MpegEncContext *s = &ctx->m;
662 int x, y, mb_v, sum, dx, dy, shift;
663 int len = 1 << (s->f_code + 4);
664 const int a = s->sprite_warping_accuracy;
665
666 if (s->workaround_bugs & FF_BUG_AMV)
667 len >>= s->quarter_sample;
668
669 if (s->real_sprite_warping_points == 1) {
670 if (ctx->divx_version == 500 && ctx->divx_build == 413 && a >= s->quarter_sample)
671 sum = s->sprite_offset[0][n] / (1 << (a - s->quarter_sample));
672 else
673 sum = RSHIFT(s->sprite_offset[0][n] * (1 << s->quarter_sample), a);
674 } else {
675 dx = s->sprite_delta[n][0];
676 dy = s->sprite_delta[n][1];
677 shift = ctx->sprite_shift[0];
678 if (n)
679 dy -= 1 << (shift + a + 1);
680 else
681 dx -= 1 << (shift + a + 1);
682 mb_v = s->sprite_offset[0][n] + dx * s->mb_x * 16U + dy * s->mb_y * 16U;
683
684 sum = 0;
685 for (y = 0; y < 16; y++) {
686 int v;
687
688 v = mb_v + dy * y;
689 // FIXME optimize
690 for (x = 0; x < 16; x++) {
691 sum += v >> shift;
692 v += dx;
693 }
694 }
695 sum = RSHIFT(sum, a + 8 - s->quarter_sample);
696 }
697
698 if (sum < -len)
699 sum = -len;
700 else if (sum >= len)
701 sum = len - 1;
702
703 return sum;
704 }
705
706 /**
707 * Decode the dc value.
708 * @param n block index (0-3 are luma, 4-5 are chroma)
709 * @param dir_ptr the prediction direction will be stored here
710 * @return the quantized dc
711 */
712 1068954 static inline int mpeg4_decode_dc(MpegEncContext *s, int n, int *dir_ptr)
713 {
714 int level, code;
715
716
2/2
✓ Branch 0 taken 712636 times.
✓ Branch 1 taken 356318 times.
1068954 if (n < 4)
717 712636 code = get_vlc2(&s->gb, dc_lum.table, DC_VLC_BITS, 1);
718 else
719 356318 code = get_vlc2(&s->gb, dc_chrom.table, DC_VLC_BITS, 1);
720
721
2/4
✓ Branch 0 taken 1068954 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1068954 times.
1068954 if (code < 0 || code > 9 /* && s->nbit < 9 */) {
722 av_log(s->avctx, AV_LOG_ERROR, "illegal dc vlc\n");
723 return AVERROR_INVALIDDATA;
724 }
725
726
2/2
✓ Branch 0 taken 156549 times.
✓ Branch 1 taken 912405 times.
1068954 if (code == 0) {
727 156549 level = 0;
728 } else {
729 if (IS_3IV1) {
730 if (code == 1)
731 level = 2 * get_bits1(&s->gb) - 1;
732 else {
733 if (get_bits1(&s->gb))
734 level = get_bits(&s->gb, code - 1) + (1 << (code - 1));
735 else
736 level = -get_bits(&s->gb, code - 1) - (1 << (code - 1));
737 }
738 } else {
739 912405 level = get_xbits(&s->gb, code);
740 }
741
742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 912405 times.
912405 if (code > 8) {
743 if (get_bits1(&s->gb) == 0) { /* marker */
744 if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)) {
745 av_log(s->avctx, AV_LOG_ERROR, "dc marker bit missing\n");
746 return AVERROR_INVALIDDATA;
747 }
748 }
749 }
750 }
751
752 1068954 return ff_mpeg4_pred_dc(s, n, level, dir_ptr, 0);
753 }
754
755 /**
756 * Decode first partition.
757 * @return number of MBs decoded or <0 if an error occurred
758 */
759 1728 static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx)
760 {
761 1728 MpegEncContext *s = &ctx->m;
762 1728 int mb_num = 0;
763 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
764
765 /* decode first partition */
766 1728 s->first_slice_line = 1;
767
2/2
✓ Branch 0 taken 7839 times.
✓ Branch 1 taken 472 times.
8311 for (; s->mb_y < s->mb_height; s->mb_y++) {
768 7839 ff_init_block_index(s);
769
2/2
✓ Branch 0 taken 139320 times.
✓ Branch 1 taken 6583 times.
145903 for (; s->mb_x < s->mb_width; s->mb_x++) {
770 139320 const int xy = s->mb_x + s->mb_y * s->mb_stride;
771 int cbpc;
772 139320 int dir = 0;
773
774 139320 mb_num++;
775 139320 ff_update_block_index(s);
776
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)
777 1611 s->first_slice_line = 0;
778
779
2/2
✓ Branch 0 taken 17762 times.
✓ Branch 1 taken 121558 times.
139320 if (s->pict_type == AV_PICTURE_TYPE_I) {
780 int i;
781
782 do {
783
2/2
✓ Branch 1 taken 439 times.
✓ Branch 2 taken 17323 times.
17762 if (show_bits(&s->gb, 19) == DC_MARKER)
784 439 return mb_num - 1;
785
786 17323 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17323 times.
17323 if (cbpc < 0) {
788 av_log(s->avctx, AV_LOG_ERROR,
789 "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
790 return AVERROR_INVALIDDATA;
791 }
792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17323 times.
17323 } while (cbpc == 8);
793
794 17323 s->cbp_table[xy] = cbpc & 3;
795 17323 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
796 17323 s->mb_intra = 1;
797
798
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17323 times.
17323 if (cbpc & 4)
799 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
800
801 17323 s->current_picture.qscale_table[xy] = s->qscale;
802
803 17323 s->mbintra_table[xy] = 1;
804
2/2
✓ Branch 0 taken 103938 times.
✓ Branch 1 taken 17323 times.
121261 for (i = 0; i < 6; i++) {
805 int dc_pred_dir;
806 103938 int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
807
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103938 times.
103938 if (dc < 0) {
808 av_log(s->avctx, AV_LOG_ERROR,
809 "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
810 return dc;
811 }
812 103938 dir <<= 1;
813
2/2
✓ Branch 0 taken 30118 times.
✓ Branch 1 taken 73820 times.
103938 if (dc_pred_dir)
814 30118 dir |= 1;
815 }
816 17323 s->pred_dir_table[xy] = dir;
817 } else { /* P/S_TYPE */
818 int mx, my, pred_x, pred_y, bits;
819 121558 int16_t *const mot_val = s->current_picture.motion_val[0][s->block_index[0]];
820 121558 const int stride = s->b8_stride * 2;
821
822 121558 try_again:
823 121558 bits = show_bits(&s->gb, 17);
824
2/2
✓ Branch 0 taken 817 times.
✓ Branch 1 taken 120741 times.
121558 if (bits == MOTION_MARKER)
825 817 return mb_num - 1;
826
827 120741 skip_bits1(&s->gb);
828
2/2
✓ Branch 0 taken 997 times.
✓ Branch 1 taken 119744 times.
120741 if (bits & 0x10000) {
829 /* skip mb */
830
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 997 times.
997 if (s->pict_type == AV_PICTURE_TYPE_S &&
831 ctx->vol_sprite_usage == GMC_SPRITE) {
832 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
833 MB_TYPE_16x16 |
834 MB_TYPE_GMC |
835 MB_TYPE_L0;
836 mx = get_amv(ctx, 0);
837 my = get_amv(ctx, 1);
838 } else {
839 997 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
840 MB_TYPE_16x16 |
841 MB_TYPE_L0;
842 997 mx = my = 0;
843 }
844 997 mot_val[0] =
845 997 mot_val[2] =
846 997 mot_val[0 + stride] =
847 997 mot_val[2 + stride] = mx;
848 997 mot_val[1] =
849 997 mot_val[3] =
850 997 mot_val[1 + stride] =
851 997 mot_val[3 + stride] = my;
852
853
2/2
✓ Branch 0 taken 224 times.
✓ Branch 1 taken 773 times.
997 if (s->mbintra_table[xy])
854 224 ff_clean_intra_table_entries(s);
855 997 continue;
856 }
857
858 119744 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 119744 times.
119744 if (cbpc < 0) {
860 av_log(s->avctx, AV_LOG_ERROR,
861 "mcbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
862 return AVERROR_INVALIDDATA;
863 }
864
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 119744 times.
119744 if (cbpc == 20)
865 goto try_again;
866
867 119744 s->cbp_table[xy] = cbpc & (8 + 3); // 8 is dquant
868
869 119744 s->mb_intra = ((cbpc & 4) != 0);
870
871
2/2
✓ Branch 0 taken 6287 times.
✓ Branch 1 taken 113457 times.
119744 if (s->mb_intra) {
872 6287 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
873 6287 s->mbintra_table[xy] = 1;
874 6287 mot_val[0] =
875 6287 mot_val[2] =
876 6287 mot_val[0 + stride] =
877 6287 mot_val[2 + stride] = 0;
878 6287 mot_val[1] =
879 6287 mot_val[3] =
880 6287 mot_val[1 + stride] =
881 6287 mot_val[3 + stride] = 0;
882 } else {
883
2/2
✓ Branch 0 taken 19851 times.
✓ Branch 1 taken 93606 times.
113457 if (s->mbintra_table[xy])
884 19851 ff_clean_intra_table_entries(s);
885
886
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 113457 times.
113457 if (s->pict_type == AV_PICTURE_TYPE_S &&
887 ctx->vol_sprite_usage == GMC_SPRITE &&
888 (cbpc & 16) == 0)
889 s->mcsel = get_bits1(&s->gb);
890 else
891 113457 s->mcsel = 0;
892
893
2/2
✓ Branch 0 taken 81626 times.
✓ Branch 1 taken 31831 times.
113457 if ((cbpc & 16) == 0) {
894 /* 16x16 motion prediction */
895
896 81626 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
897
1/2
✓ Branch 0 taken 81626 times.
✗ Branch 1 not taken.
81626 if (!s->mcsel) {
898 81626 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81626 times.
81626 if (mx >= 0xffff)
900 return AVERROR_INVALIDDATA;
901
902 81626 my = ff_h263_decode_motion(s, pred_y, s->f_code);
903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81626 times.
81626 if (my >= 0xffff)
904 return AVERROR_INVALIDDATA;
905 81626 s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
906 MB_TYPE_L0;
907 } else {
908 mx = get_amv(ctx, 0);
909 my = get_amv(ctx, 1);
910 s->current_picture.mb_type[xy] = MB_TYPE_16x16 |
911 MB_TYPE_GMC |
912 MB_TYPE_L0;
913 }
914
915 81626 mot_val[0] =
916 81626 mot_val[2] =
917 81626 mot_val[0 + stride] =
918 81626 mot_val[2 + stride] = mx;
919 81626 mot_val[1] =
920 81626 mot_val[3] =
921 81626 mot_val[1 + stride] =
922 81626 mot_val[3 + stride] = my;
923 } else {
924 int i;
925 31831 s->current_picture.mb_type[xy] = MB_TYPE_8x8 |
926 MB_TYPE_L0;
927
2/2
✓ Branch 0 taken 127324 times.
✓ Branch 1 taken 31831 times.
159155 for (i = 0; i < 4; i++) {
928 127324 int16_t *mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
929 127324 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
930
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127324 times.
127324 if (mx >= 0xffff)
931 return AVERROR_INVALIDDATA;
932
933 127324 my = ff_h263_decode_motion(s, pred_y, s->f_code);
934
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127324 times.
127324 if (my >= 0xffff)
935 return AVERROR_INVALIDDATA;
936 127324 mot_val[0] = mx;
937 127324 mot_val[1] = my;
938 }
939 }
940 }
941 }
942 }
943 6583 s->mb_x = 0;
944 }
945
946 472 return mb_num;
947 }
948
949 /**
950 * decode second partition.
951 * @return <0 if an error occurred
952 */
953 1728 static int mpeg4_decode_partition_b(MpegEncContext *s, int mb_count)
954 {
955 1728 int mb_num = 0;
956 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
957
958 1728 s->mb_x = s->resync_mb_x;
959 1728 s->first_slice_line = 1;
960
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++) {
961 7728 ff_init_block_index(s);
962
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++) {
963 138064 const int xy = s->mb_x + s->mb_y * s->mb_stride;
964
965 138064 mb_num++;
966 138064 ff_update_block_index(s);
967
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)
968 1611 s->first_slice_line = 0;
969
970
2/2
✓ Branch 0 taken 17323 times.
✓ Branch 1 taken 120741 times.
138064 if (s->pict_type == AV_PICTURE_TYPE_I) {
971 17323 int ac_pred = get_bits1(&s->gb);
972 17323 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
973
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17323 times.
17323 if (cbpy < 0) {
974 av_log(s->avctx, AV_LOG_ERROR,
975 "cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
976 return AVERROR_INVALIDDATA;
977 }
978
979 17323 s->cbp_table[xy] |= cbpy << 2;
980 17323 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
981 } else { /* P || S_TYPE */
982
2/2
✓ Branch 0 taken 6287 times.
✓ Branch 1 taken 114454 times.
120741 if (IS_INTRA(s->current_picture.mb_type[xy])) {
983 int i;
984 6287 int dir = 0;
985 6287 int ac_pred = get_bits1(&s->gb);
986 6287 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
987
988
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6287 times.
6287 if (cbpy < 0) {
989 av_log(s->avctx, AV_LOG_ERROR,
990 "I cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
991 return AVERROR_INVALIDDATA;
992 }
993
994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6287 times.
6287 if (s->cbp_table[xy] & 8)
995 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
996 6287 s->current_picture.qscale_table[xy] = s->qscale;
997
998
2/2
✓ Branch 0 taken 37722 times.
✓ Branch 1 taken 6287 times.
44009 for (i = 0; i < 6; i++) {
999 int dc_pred_dir;
1000 37722 int dc = mpeg4_decode_dc(s, i, &dc_pred_dir);
1001
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37722 times.
37722 if (dc < 0) {
1002 av_log(s->avctx, AV_LOG_ERROR,
1003 "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
1004 return dc;
1005 }
1006 37722 dir <<= 1;
1007
2/2
✓ Branch 0 taken 11780 times.
✓ Branch 1 taken 25942 times.
37722 if (dc_pred_dir)
1008 11780 dir |= 1;
1009 }
1010 6287 s->cbp_table[xy] &= 3; // remove dquant
1011 6287 s->cbp_table[xy] |= cbpy << 2;
1012 6287 s->current_picture.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED;
1013 6287 s->pred_dir_table[xy] = dir;
1014
2/2
✓ Branch 0 taken 997 times.
✓ Branch 1 taken 113457 times.
114454 } else if (IS_SKIP(s->current_picture.mb_type[xy])) {
1015 997 s->current_picture.qscale_table[xy] = s->qscale;
1016 997 s->cbp_table[xy] = 0;
1017 } else {
1018 113457 int cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1019
1020
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 113457 times.
113457 if (cbpy < 0) {
1021 av_log(s->avctx, AV_LOG_ERROR,
1022 "P cbpy corrupted at %d %d\n", s->mb_x, s->mb_y);
1023 return AVERROR_INVALIDDATA;
1024 }
1025
1026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 113457 times.
113457 if (s->cbp_table[xy] & 8)
1027 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1028 113457 s->current_picture.qscale_table[xy] = s->qscale;
1029
1030 113457 s->cbp_table[xy] &= 3; // remove dquant
1031 113457 s->cbp_table[xy] |= (cbpy ^ 0xf) << 2;
1032 }
1033 }
1034 }
1035
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 6000 times.
7728 if (mb_num >= mb_count)
1036 1728 return 0;
1037 6000 s->mb_x = 0;
1038 }
1039 return 0;
1040 }
1041
1042 /**
1043 * Decode the first and second partition.
1044 * @return <0 if error (and sets error type in the error_status_table)
1045 */
1046 1728 int ff_mpeg4_decode_partitions(Mpeg4DecContext *ctx)
1047 {
1048 1728 MpegEncContext *s = &ctx->m;
1049 int mb_num;
1050 int ret;
1051
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;
1052
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;
1053
1054 1728 mb_num = mpeg4_decode_partition_a(ctx);
1055
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1728 times.
1728 if (mb_num <= 0) {
1056 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1057 s->mb_x, s->mb_y, part_a_error);
1058 return mb_num ? mb_num : AVERROR_INVALIDDATA;
1059 }
1060
1061
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) {
1062 av_log(s->avctx, AV_LOG_ERROR, "slice below monitor ...\n");
1063 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1064 s->mb_x, s->mb_y, part_a_error);
1065 return AVERROR_INVALIDDATA;
1066 }
1067
1068 1728 s->mb_num_left = mb_num;
1069
1070
2/2
✓ Branch 0 taken 499 times.
✓ Branch 1 taken 1229 times.
1728 if (s->pict_type == AV_PICTURE_TYPE_I) {
1071
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 499 times.
499 while (show_bits(&s->gb, 9) == 1)
1072 skip_bits(&s->gb, 9);
1073
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 499 times.
499 if (get_bits(&s->gb, 19) != DC_MARKER) {
1074 av_log(s->avctx, AV_LOG_ERROR,
1075 "marker missing after first I partition at %d %d\n",
1076 s->mb_x, s->mb_y);
1077 return AVERROR_INVALIDDATA;
1078 }
1079 } else {
1080
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1229 times.
1229 while (show_bits(&s->gb, 10) == 1)
1081 skip_bits(&s->gb, 10);
1082
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1229 times.
1229 if (get_bits(&s->gb, 17) != MOTION_MARKER) {
1083 av_log(s->avctx, AV_LOG_ERROR,
1084 "marker missing after first P partition at %d %d\n",
1085 s->mb_x, s->mb_y);
1086 return AVERROR_INVALIDDATA;
1087 }
1088 }
1089 1728 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1090 1728 s->mb_x - 1, s->mb_y, part_a_end);
1091
1092 1728 ret = mpeg4_decode_partition_b(s, mb_num);
1093
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1728 times.
1728 if (ret < 0) {
1094 if (s->pict_type == AV_PICTURE_TYPE_P)
1095 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1096 s->mb_x, s->mb_y, ER_DC_ERROR);
1097 return ret;
1098 } else {
1099
2/2
✓ Branch 0 taken 1229 times.
✓ Branch 1 taken 499 times.
1728 if (s->pict_type == AV_PICTURE_TYPE_P)
1100 1229 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1101 1229 s->mb_x - 1, s->mb_y, ER_DC_END);
1102 }
1103
1104 1728 return 0;
1105 }
1106
1107 /**
1108 * Decode a block.
1109 * @return <0 if an error occurred
1110 */
1111 7873800 static inline int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block,
1112 int n, int coded, int intra,
1113 int use_intra_dc_vlc, int rvlc)
1114 {
1115 7873800 MpegEncContext *s = &ctx->m;
1116 int level, i, last, run, qmul, qadd;
1117 7873800 int av_uninit(dc_pred_dir);
1118 RLTable *rl;
1119 RL_VLC_ELEM *rl_vlc;
1120 const uint8_t *scan_table;
1121
1122 // Note intra & rvlc should be optimized away if this is inlined
1123
1124
2/2
✓ Branch 0 taken 1068954 times.
✓ Branch 1 taken 6804846 times.
7873800 if (intra) {
1125
1/2
✓ Branch 0 taken 1068954 times.
✗ Branch 1 not taken.
1068954 if (use_intra_dc_vlc) {
1126 /* DC coef */
1127
2/2
✓ Branch 0 taken 141660 times.
✓ Branch 1 taken 927294 times.
1068954 if (s->partitioned_frame) {
1128 141660 level = s->dc_val[0][s->block_index[n]];
1129
2/2
✓ Branch 0 taken 94440 times.
✓ Branch 1 taken 47220 times.
141660 if (n < 4)
1130 94440 level = FASTDIV((level + (s->y_dc_scale >> 1)), s->y_dc_scale);
1131 else
1132 47220 level = FASTDIV((level + (s->c_dc_scale >> 1)), s->c_dc_scale);
1133 141660 dc_pred_dir = (s->pred_dir_table[s->mb_x + s->mb_y * s->mb_stride] << n) & 32;
1134 } else {
1135 927294 level = mpeg4_decode_dc(s, n, &dc_pred_dir);
1136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 927294 times.
927294 if (level < 0)
1137 return level;
1138 }
1139 1068954 block[0] = level;
1140 1068954 i = 0;
1141 } else {
1142 i = -1;
1143 ff_mpeg4_pred_dc(s, n, 0, &dc_pred_dir, 0);
1144 }
1145
2/2
✓ Branch 0 taken 215012 times.
✓ Branch 1 taken 853942 times.
1068954 if (!coded)
1146 215012 goto not_coded;
1147
1148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 853942 times.
853942 if (rvlc) {
1149 rl = &ff_rvlc_rl_intra;
1150 rl_vlc = ff_rvlc_rl_intra.rl_vlc[0];
1151 } else {
1152 853942 rl = &ff_mpeg4_rl_intra;
1153 853942 rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0];
1154 }
1155
2/2
✓ Branch 0 taken 22081 times.
✓ Branch 1 taken 831861 times.
853942 if (s->ac_pred) {
1156
2/2
✓ Branch 0 taken 15515 times.
✓ Branch 1 taken 6566 times.
22081 if (dc_pred_dir == 0)
1157 15515 scan_table = s->intra_v_scantable.permutated; /* left */
1158 else
1159 6566 scan_table = s->intra_h_scantable.permutated; /* top */
1160 } else {
1161 831861 scan_table = s->intra_scantable.permutated;
1162 }
1163 853942 qmul = 1;
1164 853942 qadd = 0;
1165 } else {
1166 6804846 i = -1;
1167
2/2
✓ Branch 0 taken 4621462 times.
✓ Branch 1 taken 2183384 times.
6804846 if (!coded) {
1168 4621462 s->block_last_index[n] = i;
1169 4621462 return 0;
1170 }
1171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2183384 times.
2183384 if (rvlc)
1172 rl = &ff_rvlc_rl_inter;
1173 else
1174 2183384 rl = &ff_h263_rl_inter;
1175
1176 2183384 scan_table = s->intra_scantable.permutated;
1177
1178
2/2
✓ Branch 0 taken 11882 times.
✓ Branch 1 taken 2171502 times.
2183384 if (s->mpeg_quant) {
1179 11882 qmul = 1;
1180 11882 qadd = 0;
1181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11882 times.
11882 if (rvlc)
1182 rl_vlc = ff_rvlc_rl_inter.rl_vlc[0];
1183 else
1184 11882 rl_vlc = ff_h263_rl_inter.rl_vlc[0];
1185 } else {
1186 2171502 qmul = s->qscale << 1;
1187 2171502 qadd = (s->qscale - 1) | 1;
1188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2171502 times.
2171502 if (rvlc)
1189 rl_vlc = ff_rvlc_rl_inter.rl_vlc[s->qscale];
1190 else
1191 2171502 rl_vlc = ff_h263_rl_inter.rl_vlc[s->qscale];
1192 }
1193 }
1194 {
1195 3037326 OPEN_READER(re, &s->gb);
1196 for (;;) {
1197 26282206 UPDATE_CACHE(re, &s->gb);
1198
2/2
✓ Branch 1 taken 1121528 times.
✓ Branch 2 taken 25160678 times.
26282206 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 0);
1199
2/2
✓ Branch 0 taken 770420 times.
✓ Branch 1 taken 25511786 times.
26282206 if (level == 0) {
1200 /* escape */
1201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 770420 times.
770420 if (rvlc) {
1202 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1203 av_log(s->avctx, AV_LOG_ERROR,
1204 "1. marker bit missing in rvlc esc\n");
1205 return AVERROR_INVALIDDATA;
1206 }
1207 SKIP_CACHE(re, &s->gb, 1);
1208
1209 last = SHOW_UBITS(re, &s->gb, 1);
1210 SKIP_CACHE(re, &s->gb, 1);
1211 run = SHOW_UBITS(re, &s->gb, 6);
1212 SKIP_COUNTER(re, &s->gb, 1 + 1 + 6);
1213 UPDATE_CACHE(re, &s->gb);
1214
1215 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1216 av_log(s->avctx, AV_LOG_ERROR,
1217 "2. marker bit missing in rvlc esc\n");
1218 return AVERROR_INVALIDDATA;
1219 }
1220 SKIP_CACHE(re, &s->gb, 1);
1221
1222 level = SHOW_UBITS(re, &s->gb, 11);
1223 SKIP_CACHE(re, &s->gb, 11);
1224
1225 if (SHOW_UBITS(re, &s->gb, 5) != 0x10) {
1226 av_log(s->avctx, AV_LOG_ERROR, "reverse esc missing\n");
1227 return AVERROR_INVALIDDATA;
1228 }
1229 SKIP_CACHE(re, &s->gb, 5);
1230
1231 level = level * qmul + qadd;
1232 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1233 SKIP_COUNTER(re, &s->gb, 1 + 11 + 5 + 1);
1234
1235 i += run + 1;
1236 if (last)
1237 i += 192;
1238 } else {
1239 int cache;
1240 770420 cache = GET_CACHE(re, &s->gb);
1241
1242 if (IS_3IV1)
1243 cache ^= 0xC0000000;
1244
1245
2/2
✓ Branch 0 taken 353524 times.
✓ Branch 1 taken 416896 times.
770420 if (cache & 0x80000000) {
1246
2/2
✓ Branch 0 taken 137781 times.
✓ Branch 1 taken 215743 times.
353524 if (cache & 0x40000000) {
1247 /* third escape */
1248 137781 SKIP_CACHE(re, &s->gb, 2);
1249 137781 last = SHOW_UBITS(re, &s->gb, 1);
1250 137781 SKIP_CACHE(re, &s->gb, 1);
1251 137781 run = SHOW_UBITS(re, &s->gb, 6);
1252 137781 SKIP_COUNTER(re, &s->gb, 2 + 1 + 6);
1253 137781 UPDATE_CACHE(re, &s->gb);
1254
1255 if (IS_3IV1) {
1256 level = SHOW_SBITS(re, &s->gb, 12);
1257 LAST_SKIP_BITS(re, &s->gb, 12);
1258 } else {
1259
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 137781 times.
137781 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1260 av_log(s->avctx, AV_LOG_ERROR,
1261 "1. marker bit missing in 3. esc\n");
1262 if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
1263 return AVERROR_INVALIDDATA;
1264 }
1265 137781 SKIP_CACHE(re, &s->gb, 1);
1266
1267 137781 level = SHOW_SBITS(re, &s->gb, 12);
1268 137781 SKIP_CACHE(re, &s->gb, 12);
1269
1270
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 137781 times.
137781 if (SHOW_UBITS(re, &s->gb, 1) == 0) {
1271 av_log(s->avctx, AV_LOG_ERROR,
1272 "2. marker bit missing in 3. esc\n");
1273 if (!(s->avctx->err_recognition & AV_EF_IGNORE_ERR))
1274 return AVERROR_INVALIDDATA;
1275 }
1276
1277 137781 SKIP_COUNTER(re, &s->gb, 1 + 12 + 1);
1278 }
1279
1280 #if 0
1281 if (s->error_recognition >= FF_ER_COMPLIANT) {
1282 const int abs_level= FFABS(level);
1283 if (abs_level<=MAX_LEVEL && run<=MAX_RUN) {
1284 const int run1= run - rl->max_run[last][abs_level] - 1;
1285 if (abs_level <= rl->max_level[last][run]) {
1286 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n");
1287 return AVERROR_INVALIDDATA;
1288 }
1289 if (s->error_recognition > FF_ER_COMPLIANT) {
1290 if (abs_level <= rl->max_level[last][run]*2) {
1291 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n");
1292 return AVERROR_INVALIDDATA;
1293 }
1294 if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) {
1295 av_log(s->avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n");
1296 return AVERROR_INVALIDDATA;
1297 }
1298 }
1299 }
1300 }
1301 #endif
1302
2/2
✓ Branch 0 taken 69038 times.
✓ Branch 1 taken 68743 times.
137781 if (level > 0)
1303 69038 level = level * qmul + qadd;
1304 else
1305 68743 level = level * qmul - qadd;
1306
1307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137781 times.
137781 if ((unsigned)(level + 2048) > 4095) {
1308 if (s->avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)) {
1309 if (level > 2560 || level < -2560) {
1310 av_log(s->avctx, AV_LOG_ERROR,
1311 "|level| overflow in 3. esc, qp=%d\n",
1312 s->qscale);
1313 return AVERROR_INVALIDDATA;
1314 }
1315 }
1316 level = level < 0 ? -2048 : 2047;
1317 }
1318
1319 137781 i += run + 1;
1320
2/2
✓ Branch 0 taken 44545 times.
✓ Branch 1 taken 93236 times.
137781 if (last)
1321 44545 i += 192;
1322 } else {
1323 /* second escape */
1324 215743 SKIP_BITS(re, &s->gb, 2);
1325
2/2
✓ Branch 1 taken 23515 times.
✓ Branch 2 taken 192228 times.
215743 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1326 215743 i += run + rl->max_run[run >> 7][level / qmul] + 1; // FIXME opt indexing
1327 215743 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1328 215743 LAST_SKIP_BITS(re, &s->gb, 1);
1329 }
1330 } else {
1331 /* first escape */
1332 416896 SKIP_BITS(re, &s->gb, 1);
1333
2/2
✓ Branch 1 taken 67722 times.
✓ Branch 2 taken 349174 times.
416896 GET_RL_VLC(level, run, re, &s->gb, rl_vlc, TEX_VLC_BITS, 2, 1);
1334 416896 i += run;
1335 416896 level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; // FIXME opt indexing
1336 416896 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1337 416896 LAST_SKIP_BITS(re, &s->gb, 1);
1338 }
1339 }
1340 } else {
1341 25511786 i += run;
1342 25511786 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1343 25511786 LAST_SKIP_BITS(re, &s->gb, 1);
1344 }
1345 ff_tlog(s->avctx, "dct[%d][%d] = %- 4d end?:%d\n", scan_table[i&63]&7, scan_table[i&63] >> 3, level, i>62);
1346
2/2
✓ Branch 0 taken 3037326 times.
✓ Branch 1 taken 23244880 times.
26282206 if (i > 62) {
1347 3037326 i -= 192;
1348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3037326 times.
3037326 if (i & (~63)) {
1349 av_log(s->avctx, AV_LOG_ERROR,
1350 "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1351 return AVERROR_INVALIDDATA;
1352 }
1353
1354 3037326 block[scan_table[i]] = level;
1355 3037326 break;
1356 }
1357
1358 23244880 block[scan_table[i]] = level;
1359 }
1360 3037326 CLOSE_READER(re, &s->gb);
1361 }
1362
1363 3252338 not_coded:
1364
2/2
✓ Branch 0 taken 1068954 times.
✓ Branch 1 taken 2183384 times.
3252338 if (intra) {
1365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1068954 times.
1068954 if (!use_intra_dc_vlc) {
1366 block[0] = ff_mpeg4_pred_dc(s, n, block[0], &dc_pred_dir, 0);
1367
1368 i -= i >> 31; // if (i == -1) i = 0;
1369 }
1370
1371 1068954 ff_mpeg4_pred_ac(s, block, n, dc_pred_dir);
1372
2/2
✓ Branch 0 taken 25890 times.
✓ Branch 1 taken 1043064 times.
1068954 if (s->ac_pred)
1373 25890 i = 63; // FIXME not optimal
1374 }
1375 3252338 s->block_last_index[n] = i;
1376 3252338 return 0;
1377 }
1378
1379 /**
1380 * decode partition C of one MB.
1381 * @return <0 if an error occurred
1382 */
1383 138064 static int mpeg4_decode_partitioned_mb(MpegEncContext *s, int16_t block[6][64])
1384 {
1385 138064 Mpeg4DecContext *ctx = s->avctx->priv_data;
1386 int cbp, mb_type, use_intra_dc_vlc;
1387 138064 const int xy = s->mb_x + s->mb_y * s->mb_stride;
1388
1389 av_assert2(s == (void*)ctx);
1390
1391 138064 mb_type = s->current_picture.mb_type[xy];
1392 138064 cbp = s->cbp_table[xy];
1393
1394 138064 use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1395
1396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 138064 times.
138064 if (s->current_picture.qscale_table[xy] != s->qscale)
1397 ff_set_qscale(s, s->current_picture.qscale_table[xy]);
1398
1399
2/2
✓ Branch 0 taken 17323 times.
✓ Branch 1 taken 120741 times.
138064 if (s->pict_type == AV_PICTURE_TYPE_P ||
1400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17323 times.
138064 s->pict_type == AV_PICTURE_TYPE_S) {
1401 int i;
1402
2/2
✓ Branch 0 taken 482964 times.
✓ Branch 1 taken 120741 times.
603705 for (i = 0; i < 4; i++) {
1403 482964 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
1404 482964 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
1405 }
1406 120741 s->mb_intra = IS_INTRA(mb_type);
1407
1408
2/2
✓ Branch 0 taken 997 times.
✓ Branch 1 taken 119744 times.
120741 if (IS_SKIP(mb_type)) {
1409 /* skip mb */
1410
2/2
✓ Branch 0 taken 5982 times.
✓ Branch 1 taken 997 times.
6979 for (i = 0; i < 6; i++)
1411 5982 s->block_last_index[i] = -1;
1412 997 s->mv_dir = MV_DIR_FORWARD;
1413 997 s->mv_type = MV_TYPE_16X16;
1414
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 997 times.
997 if (s->pict_type == AV_PICTURE_TYPE_S
1415 && ctx->vol_sprite_usage == GMC_SPRITE) {
1416 s->mcsel = 1;
1417 s->mb_skipped = 0;
1418 } else {
1419 997 s->mcsel = 0;
1420 997 s->mb_skipped = 1;
1421 }
1422
2/2
✓ Branch 0 taken 6287 times.
✓ Branch 1 taken 113457 times.
119744 } else if (s->mb_intra) {
1423 6287 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1424
1/2
✓ Branch 0 taken 113457 times.
✗ Branch 1 not taken.
113457 } else if (!s->mb_intra) {
1425 // s->mcsel = 0; // FIXME do we need to init that?
1426
1427 113457 s->mv_dir = MV_DIR_FORWARD;
1428
2/2
✓ Branch 0 taken 31831 times.
✓ Branch 1 taken 81626 times.
113457 if (IS_8X8(mb_type)) {
1429 31831 s->mv_type = MV_TYPE_8X8;
1430 } else {
1431 81626 s->mv_type = MV_TYPE_16X16;
1432 }
1433 }
1434 } else { /* I-Frame */
1435 17323 s->mb_intra = 1;
1436 17323 s->ac_pred = IS_ACPRED(s->current_picture.mb_type[xy]);
1437 }
1438
1439
2/2
✓ Branch 0 taken 137067 times.
✓ Branch 1 taken 997 times.
138064 if (!IS_SKIP(mb_type)) {
1440 int i;
1441 137067 s->bdsp.clear_blocks(s->block[0]);
1442 /* decode each block */
1443
2/2
✓ Branch 0 taken 822402 times.
✓ Branch 1 taken 137067 times.
959469 for (i = 0; i < 6; i++) {
1444
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,
1445 use_intra_dc_vlc, ctx->rvlc) < 0) {
1446 av_log(s->avctx, AV_LOG_ERROR,
1447 "texture corrupted at %d %d %d\n",
1448 s->mb_x, s->mb_y, s->mb_intra);
1449 return AVERROR_INVALIDDATA;
1450 }
1451 822402 cbp += cbp;
1452 }
1453 }
1454
1455 /* per-MB end of slice check */
1456
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 136336 times.
138064 if (--s->mb_num_left <= 0) {
1457
1/2
✓ Branch 1 taken 1728 times.
✗ Branch 2 not taken.
1728 if (mpeg4_is_resync(ctx))
1458 1728 return SLICE_END;
1459 else
1460 return SLICE_NOEND;
1461 } else {
1462
2/2
✓ Branch 1 taken 308 times.
✓ Branch 2 taken 136028 times.
136336 if (mpeg4_is_resync(ctx)) {
1463
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;
1464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (s->cbp_table[xy + delta])
1465 return SLICE_END;
1466 }
1467 136336 return SLICE_OK;
1468 }
1469 }
1470
1471 1289056 static int mpeg4_decode_mb(MpegEncContext *s, int16_t block[6][64])
1472 {
1473 1289056 Mpeg4DecContext *ctx = s->avctx->priv_data;
1474 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1475 int16_t *mot_val;
1476 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
1477 1289056 const int xy = s->mb_x + s->mb_y * s->mb_stride;
1478
1479 av_assert2(s == (void*)ctx);
1480 av_assert2(s->h263_pred);
1481
1482
2/2
✓ Branch 0 taken 367659 times.
✓ Branch 1 taken 921397 times.
1289056 if (s->pict_type == AV_PICTURE_TYPE_P ||
1483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 367659 times.
367659 s->pict_type == AV_PICTURE_TYPE_S) {
1484 do {
1485
2/2
✓ Branch 1 taken 80953 times.
✓ Branch 2 taken 840444 times.
921397 if (get_bits1(&s->gb)) {
1486 /* skip mb */
1487 80953 s->mb_intra = 0;
1488
2/2
✓ Branch 0 taken 485718 times.
✓ Branch 1 taken 80953 times.
566671 for (i = 0; i < 6; i++)
1489 485718 s->block_last_index[i] = -1;
1490 80953 s->mv_dir = MV_DIR_FORWARD;
1491 80953 s->mv_type = MV_TYPE_16X16;
1492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80953 times.
80953 if (s->pict_type == AV_PICTURE_TYPE_S &&
1493 ctx->vol_sprite_usage == GMC_SPRITE) {
1494 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1495 MB_TYPE_GMC |
1496 MB_TYPE_16x16 |
1497 MB_TYPE_L0;
1498 s->mcsel = 1;
1499 s->mv[0][0][0] = get_amv(ctx, 0);
1500 s->mv[0][0][1] = get_amv(ctx, 1);
1501 s->mb_skipped = 0;
1502 } else {
1503 80953 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1504 MB_TYPE_16x16 |
1505 MB_TYPE_L0;
1506 80953 s->mcsel = 0;
1507 80953 s->mv[0][0][0] = 0;
1508 80953 s->mv[0][0][1] = 0;
1509 80953 s->mb_skipped = 1;
1510 }
1511 80953 goto end;
1512 }
1513 840444 cbpc = get_vlc2(&s->gb, ff_h263_inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
1514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 840444 times.
840444 if (cbpc < 0) {
1515 av_log(s->avctx, AV_LOG_ERROR,
1516 "mcbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1517 return AVERROR_INVALIDDATA;
1518 }
1519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 840444 times.
840444 } while (cbpc == 20);
1520
1521 840444 s->bdsp.clear_blocks(s->block[0]);
1522 840444 dquant = cbpc & 8;
1523 840444 s->mb_intra = ((cbpc & 4) != 0);
1524
2/2
✓ Branch 0 taken 27278 times.
✓ Branch 1 taken 813166 times.
840444 if (s->mb_intra)
1525 27278 goto intra;
1526
1527
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 813166 times.
813166 if (s->pict_type == AV_PICTURE_TYPE_S &&
1528 ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0)
1529 s->mcsel = get_bits1(&s->gb);
1530 else
1531 813166 s->mcsel = 0;
1532 813166 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1) ^ 0x0F;
1533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 813166 times.
813166 if (cbpy < 0) {
1534 av_log(s->avctx, AV_LOG_ERROR,
1535 "P cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1536 return AVERROR_INVALIDDATA;
1537 }
1538
1539 813166 cbp = (cbpc & 3) | (cbpy << 2);
1540
2/2
✓ Branch 0 taken 9237 times.
✓ Branch 1 taken 803929 times.
813166 if (dquant)
1541 9237 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1542
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 813166 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
813166 if ((!s->progressive_sequence) &&
1543 (cbp || (s->workaround_bugs & FF_BUG_XVID_ILACE)))
1544 s->interlaced_dct = get_bits1(&s->gb);
1545
1546 813166 s->mv_dir = MV_DIR_FORWARD;
1547
2/2
✓ Branch 0 taken 721166 times.
✓ Branch 1 taken 92000 times.
813166 if ((cbpc & 16) == 0) {
1548
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 721166 times.
721166 if (s->mcsel) {
1549 s->current_picture.mb_type[xy] = MB_TYPE_GMC |
1550 MB_TYPE_16x16 |
1551 MB_TYPE_L0;
1552 /* 16x16 global motion prediction */
1553 s->mv_type = MV_TYPE_16X16;
1554 mx = get_amv(ctx, 0);
1555 my = get_amv(ctx, 1);
1556 s->mv[0][0][0] = mx;
1557 s->mv[0][0][1] = my;
1558
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 721166 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
721166 } else if ((!s->progressive_sequence) && get_bits1(&s->gb)) {
1559 s->current_picture.mb_type[xy] = MB_TYPE_16x8 |
1560 MB_TYPE_L0 |
1561 MB_TYPE_INTERLACED;
1562 /* 16x8 field motion prediction */
1563 s->mv_type = MV_TYPE_FIELD;
1564
1565 s->field_select[0][0] = get_bits1(&s->gb);
1566 s->field_select[0][1] = get_bits1(&s->gb);
1567
1568 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1569
1570 for (i = 0; i < 2; i++) {
1571 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1572 if (mx >= 0xffff)
1573 return AVERROR_INVALIDDATA;
1574
1575 my = ff_h263_decode_motion(s, pred_y / 2, s->f_code);
1576 if (my >= 0xffff)
1577 return AVERROR_INVALIDDATA;
1578
1579 s->mv[0][i][0] = mx;
1580 s->mv[0][i][1] = my;
1581 }
1582 } else {
1583 721166 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
1584 /* 16x16 motion prediction */
1585 721166 s->mv_type = MV_TYPE_16X16;
1586 721166 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1587 721166 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1588
1589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 721166 times.
721166 if (mx >= 0xffff)
1590 return AVERROR_INVALIDDATA;
1591
1592 721166 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1593
1594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 721166 times.
721166 if (my >= 0xffff)
1595 return AVERROR_INVALIDDATA;
1596 721166 s->mv[0][0][0] = mx;
1597 721166 s->mv[0][0][1] = my;
1598 }
1599 } else {
1600 92000 s->current_picture.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_L0;
1601 92000 s->mv_type = MV_TYPE_8X8;
1602
2/2
✓ Branch 0 taken 368000 times.
✓ Branch 1 taken 92000 times.
460000 for (i = 0; i < 4; i++) {
1603 368000 mot_val = ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1604 368000 mx = ff_h263_decode_motion(s, pred_x, s->f_code);
1605
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 368000 times.
368000 if (mx >= 0xffff)
1606 return AVERROR_INVALIDDATA;
1607
1608 368000 my = ff_h263_decode_motion(s, pred_y, s->f_code);
1609
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 368000 times.
368000 if (my >= 0xffff)
1610 return AVERROR_INVALIDDATA;
1611 368000 s->mv[0][i][0] = mx;
1612 368000 s->mv[0][i][1] = my;
1613 368000 mot_val[0] = mx;
1614 368000 mot_val[1] = my;
1615 }
1616 }
1617
2/2
✓ Branch 0 taken 240388 times.
✓ Branch 1 taken 127271 times.
367659 } else if (s->pict_type == AV_PICTURE_TYPE_B) {
1618 int modb1; // first bit of modb
1619 int modb2; // second bit of modb
1620 int mb_type;
1621
1622 240388 s->mb_intra = 0; // B-frames never contain intra blocks
1623 240388 s->mcsel = 0; // ... true gmc blocks
1624
1625
2/2
✓ Branch 0 taken 10354 times.
✓ Branch 1 taken 230034 times.
240388 if (s->mb_x == 0) {
1626
2/2
✓ Branch 0 taken 20708 times.
✓ Branch 1 taken 10354 times.
31062 for (i = 0; i < 2; i++) {
1627 20708 s->last_mv[i][0][0] =
1628 20708 s->last_mv[i][0][1] =
1629 20708 s->last_mv[i][1][0] =
1630 20708 s->last_mv[i][1][1] = 0;
1631 }
1632
1633 10354 ff_thread_await_progress(&s->next_picture_ptr->tf, s->mb_y, 0);
1634 }
1635
1636 /* if we skipped it in the future P-frame than skip it now too */
1637 240388 s->mb_skipped = s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]; // Note, skiptab=0 if last was GMC
1638
1639
2/2
✓ Branch 0 taken 32870 times.
✓ Branch 1 taken 207518 times.
240388 if (s->mb_skipped) {
1640 /* skip mb */
1641
2/2
✓ Branch 0 taken 197220 times.
✓ Branch 1 taken 32870 times.
230090 for (i = 0; i < 6; i++)
1642 197220 s->block_last_index[i] = -1;
1643
1644 32870 s->mv_dir = MV_DIR_FORWARD;
1645 32870 s->mv_type = MV_TYPE_16X16;
1646 32870 s->mv[0][0][0] =
1647 32870 s->mv[0][0][1] =
1648 32870 s->mv[1][0][0] =
1649 32870 s->mv[1][0][1] = 0;
1650 32870 s->current_picture.mb_type[xy] = MB_TYPE_SKIP |
1651 MB_TYPE_16x16 |
1652 MB_TYPE_L0;
1653 32870 goto end;
1654 }
1655
1656 207518 modb1 = get_bits1(&s->gb);
1657
2/2
✓ Branch 0 taken 44804 times.
✓ Branch 1 taken 162714 times.
207518 if (modb1) {
1658 // like MB_TYPE_B_DIRECT but no vectors coded
1659 44804 mb_type = MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_L0L1;
1660 44804 cbp = 0;
1661 } else {
1662 162714 modb2 = get_bits1(&s->gb);
1663 162714 mb_type = get_vlc2(&s->gb, mb_type_b_vlc.table, MB_TYPE_B_VLC_BITS, 1);
1664
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 162714 times.
162714 if (mb_type < 0) {
1665 av_log(s->avctx, AV_LOG_ERROR, "illegal MB_type\n");
1666 return AVERROR_INVALIDDATA;
1667 }
1668 162714 mb_type = mb_type_b_map[mb_type];
1669
2/2
✓ Branch 0 taken 67183 times.
✓ Branch 1 taken 95531 times.
162714 if (modb2) {
1670 67183 cbp = 0;
1671 } else {
1672 95531 s->bdsp.clear_blocks(s->block[0]);
1673 95531 cbp = get_bits(&s->gb, 6);
1674 }
1675
1676
4/4
✓ Branch 0 taken 120541 times.
✓ Branch 1 taken 42173 times.
✓ Branch 2 taken 69310 times.
✓ Branch 3 taken 51231 times.
162714 if ((!IS_DIRECT(mb_type)) && cbp) {
1677
2/2
✓ Branch 1 taken 6992 times.
✓ Branch 2 taken 62318 times.
69310 if (get_bits1(&s->gb))
1678 6992 ff_set_qscale(s, s->qscale + get_bits1(&s->gb) * 4 - 2);
1679 }
1680
1681
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 162714 times.
162714 if (!s->progressive_sequence) {
1682 if (cbp)
1683 s->interlaced_dct = get_bits1(&s->gb);
1684
1685 if (!IS_DIRECT(mb_type) && get_bits1(&s->gb)) {
1686 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
1687 mb_type &= ~MB_TYPE_16x16;
1688
1689 if (USES_LIST(mb_type, 0)) {
1690 s->field_select[0][0] = get_bits1(&s->gb);
1691 s->field_select[0][1] = get_bits1(&s->gb);
1692 }
1693 if (USES_LIST(mb_type, 1)) {
1694 s->field_select[1][0] = get_bits1(&s->gb);
1695 s->field_select[1][1] = get_bits1(&s->gb);
1696 }
1697 }
1698 }
1699
1700 162714 s->mv_dir = 0;
1701
2/2
✓ Branch 0 taken 120541 times.
✓ Branch 1 taken 42173 times.
162714 if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) {
1702 120541 s->mv_type = MV_TYPE_16X16;
1703
1704
2/2
✓ Branch 0 taken 82655 times.
✓ Branch 1 taken 37886 times.
120541 if (USES_LIST(mb_type, 0)) {
1705 82655 s->mv_dir = MV_DIR_FORWARD;
1706
1707 82655 mx = ff_h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
1708 82655 my = ff_h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
1709 82655 s->last_mv[0][1][0] =
1710 82655 s->last_mv[0][0][0] =
1711 82655 s->mv[0][0][0] = mx;
1712 82655 s->last_mv[0][1][1] =
1713 82655 s->last_mv[0][0][1] =
1714 82655 s->mv[0][0][1] = my;
1715 }
1716
1717
2/2
✓ Branch 0 taken 94596 times.
✓ Branch 1 taken 25945 times.
120541 if (USES_LIST(mb_type, 1)) {
1718 94596 s->mv_dir |= MV_DIR_BACKWARD;
1719
1720 94596 mx = ff_h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
1721 94596 my = ff_h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
1722 94596 s->last_mv[1][1][0] =
1723 94596 s->last_mv[1][0][0] =
1724 94596 s->mv[1][0][0] = mx;
1725 94596 s->last_mv[1][1][1] =
1726 94596 s->last_mv[1][0][1] =
1727 94596 s->mv[1][0][1] = my;
1728 }
1729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42173 times.
42173 } else if (!IS_DIRECT(mb_type)) {
1730 s->mv_type = MV_TYPE_FIELD;
1731
1732 if (USES_LIST(mb_type, 0)) {
1733 s->mv_dir = MV_DIR_FORWARD;
1734
1735 for (i = 0; i < 2; i++) {
1736 mx = ff_h263_decode_motion(s, s->last_mv[0][i][0], s->f_code);
1737 my = ff_h263_decode_motion(s, s->last_mv[0][i][1] / 2, s->f_code);
1738 s->last_mv[0][i][0] =
1739 s->mv[0][i][0] = mx;
1740 s->last_mv[0][i][1] = (s->mv[0][i][1] = my) * 2;
1741 }
1742 }
1743
1744 if (USES_LIST(mb_type, 1)) {
1745 s->mv_dir |= MV_DIR_BACKWARD;
1746
1747 for (i = 0; i < 2; i++) {
1748 mx = ff_h263_decode_motion(s, s->last_mv[1][i][0], s->b_code);
1749 my = ff_h263_decode_motion(s, s->last_mv[1][i][1] / 2, s->b_code);
1750 s->last_mv[1][i][0] =
1751 s->mv[1][i][0] = mx;
1752 s->last_mv[1][i][1] = (s->mv[1][i][1] = my) * 2;
1753 }
1754 }
1755 }
1756 }
1757
1758
2/2
✓ Branch 0 taken 86977 times.
✓ Branch 1 taken 120541 times.
207518 if (IS_DIRECT(mb_type)) {
1759
2/2
✓ Branch 0 taken 44804 times.
✓ Branch 1 taken 42173 times.
86977 if (IS_SKIP(mb_type)) {
1760 44804 mx =
1761 44804 my = 0;
1762 } else {
1763 42173 mx = ff_h263_decode_motion(s, 0, 1);
1764 42173 my = ff_h263_decode_motion(s, 0, 1);
1765 }
1766
1767 86977 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
1768 86977 mb_type |= ff_mpeg4_set_direct_mv(s, mx, my);
1769 }
1770 207518 s->current_picture.mb_type[xy] = mb_type;
1771 } else { /* I-Frame */
1772 int use_intra_dc_vlc;
1773
1774 do {
1775 127271 cbpc = get_vlc2(&s->gb, ff_h263_intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
1776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127271 times.
127271 if (cbpc < 0) {
1777 av_log(s->avctx, AV_LOG_ERROR,
1778 "I cbpc damaged at %d %d\n", s->mb_x, s->mb_y);
1779 return AVERROR_INVALIDDATA;
1780 }
1781
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127271 times.
127271 } while (cbpc == 8);
1782
1783 127271 dquant = cbpc & 4;
1784 127271 s->mb_intra = 1;
1785
1786 154549 intra:
1787 154549 s->ac_pred = get_bits1(&s->gb);
1788
2/2
✓ Branch 0 taken 552 times.
✓ Branch 1 taken 153997 times.
154549 if (s->ac_pred)
1789 552 s->current_picture.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED;
1790 else
1791 153997 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
1792
1793 154549 cbpy = get_vlc2(&s->gb, ff_h263_cbpy_vlc.table, CBPY_VLC_BITS, 1);
1794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 154549 times.
154549 if (cbpy < 0) {
1795 av_log(s->avctx, AV_LOG_ERROR,
1796 "I cbpy damaged at %d %d\n", s->mb_x, s->mb_y);
1797 return AVERROR_INVALIDDATA;
1798 }
1799 154549 cbp = (cbpc & 3) | (cbpy << 2);
1800
1801 154549 use_intra_dc_vlc = s->qscale < ctx->intra_dc_threshold;
1802
1803
2/2
✓ Branch 0 taken 7107 times.
✓ Branch 1 taken 147442 times.
154549 if (dquant)
1804 7107 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
1805
1806
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 154549 times.
154549 if (!s->progressive_sequence)
1807 s->interlaced_dct = get_bits1(&s->gb);
1808
1809 154549 s->bdsp.clear_blocks(s->block[0]);
1810 /* decode each block */
1811
2/2
✓ Branch 0 taken 927294 times.
✓ Branch 1 taken 154549 times.
1081843 for (i = 0; i < 6; i++) {
1812
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 927294 times.
927294 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32,
1813 1, use_intra_dc_vlc, 0) < 0)
1814 return AVERROR_INVALIDDATA;
1815 927294 cbp += cbp;
1816 }
1817 154549 goto end;
1818 }
1819
1820 /* decode each block */
1821
2/2
✓ Branch 0 taken 6124104 times.
✓ Branch 1 taken 1020684 times.
7144788 for (i = 0; i < 6; i++) {
1822
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6124104 times.
6124104 if (mpeg4_decode_block(ctx, block[i], i, cbp & 32, 0, 0, 0) < 0)
1823 return AVERROR_INVALIDDATA;
1824 6124104 cbp += cbp;
1825 }
1826
1827 1020684 end:
1828 /* per-MB end of slice check */
1829
1/2
✓ Branch 0 taken 1289056 times.
✗ Branch 1 not taken.
1289056 if (s->codec_id == AV_CODEC_ID_MPEG4) {
1830 1289056 int next = mpeg4_is_resync(ctx);
1831
2/2
✓ Branch 0 taken 13412 times.
✓ Branch 1 taken 1275644 times.
1289056 if (next) {
1832
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 13407 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
13412 if (s->mb_x + s->mb_y*s->mb_width + 1 > next && (s->avctx->err_recognition & AV_EF_AGGRESSIVE)) {
1833 return AVERROR_INVALIDDATA;
1834
2/2
✓ Branch 0 taken 5468 times.
✓ Branch 1 taken 7944 times.
13412 } else if (s->mb_x + s->mb_y*s->mb_width + 1 >= next)
1835 5468 return SLICE_END;
1836
1837
1/2
✓ Branch 0 taken 7944 times.
✗ Branch 1 not taken.
7944 if (s->pict_type == AV_PICTURE_TYPE_B) {
1838
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;
1839 7944 ff_thread_await_progress(&s->next_picture_ptr->tf,
1840
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 7776 times.
7944 (s->mb_x + delta >= s->mb_width)
1841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 ? FFMIN(s->mb_y + 1, s->mb_height - 1)
1842 : s->mb_y, 0);
1843
1/2
✓ Branch 0 taken 7944 times.
✗ Branch 1 not taken.
7944 if (s->next_picture.mbskip_table[xy + delta])
1844 7944 return SLICE_OK;
1845 }
1846
1847 return SLICE_END;
1848 }
1849 }
1850
1851 1275644 return SLICE_OK;
1852 }
1853
1854 /* As per spec, studio start code search isn't the same as the old type of start code */
1855 34 static void next_start_code_studio(GetBitContext *gb)
1856 {
1857 34 align_get_bits(gb);
1858
1859
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) {
1860 get_bits(gb, 8);
1861 }
1862 34 }
1863
1864 /* additional_code, vlc index */
1865 static const uint8_t ac_state_tab[22][2] =
1866 {
1867 {0, 0},
1868 {0, 1},
1869 {1, 1},
1870 {2, 1},
1871 {3, 1},
1872 {4, 1},
1873 {5, 1},
1874 {1, 2},
1875 {2, 2},
1876 {3, 2},
1877 {4, 2},
1878 {5, 2},
1879 {6, 2},
1880 {1, 3},
1881 {2, 4},
1882 {3, 5},
1883 {4, 6},
1884 {5, 7},
1885 {6, 8},
1886 {7, 9},
1887 {8, 10},
1888 {0, 11}
1889 };
1890
1891 5336 static int mpeg4_decode_studio_block(MpegEncContext *s, int32_t block[64], int n)
1892 {
1893 5336 Mpeg4DecContext *ctx = s->avctx->priv_data;
1894
1895 5336 int cc, dct_dc_size, dct_diff, code, j, idx = 1, group = 0, run = 0,
1896 additional_code_len, sign, mismatch;
1897 5336 const VLC *cur_vlc = &studio_intra_tab[0];
1898 5336 uint8_t *const scantable = s->intra_scantable.permutated;
1899 const uint16_t *quant_matrix;
1900 uint32_t flc;
1901 5336 const int min = -1 * (1 << (s->avctx->bits_per_raw_sample + 6));
1902 5336 const int max = ((1 << (s->avctx->bits_per_raw_sample + 6)) - 1);
1903 5336 int shift = 3 - s->dct_precision;
1904
1905 5336 mismatch = 1;
1906
1907 5336 memset(block, 0, 64 * sizeof(int32_t));
1908
1909
2/2
✓ Branch 0 taken 2668 times.
✓ Branch 1 taken 2668 times.
5336 if (n < 4) {
1910 2668 cc = 0;
1911 2668 dct_dc_size = get_vlc2(&s->gb, studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
1912 2668 quant_matrix = s->intra_matrix;
1913 } else {
1914 2668 cc = (n & 1) + 1;
1915
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2668 times.
2668 if (ctx->rgb)
1916 dct_dc_size = get_vlc2(&s->gb, studio_luma_dc.table, STUDIO_INTRA_BITS, 2);
1917 else
1918 2668 dct_dc_size = get_vlc2(&s->gb, studio_chroma_dc.table, STUDIO_INTRA_BITS, 2);
1919 2668 quant_matrix = s->chroma_intra_matrix;
1920 }
1921
1922
2/2
✓ Branch 0 taken 242 times.
✓ Branch 1 taken 5094 times.
5336 if (dct_dc_size == 0) {
1923 242 dct_diff = 0;
1924 } else {
1925 5094 dct_diff = get_xbits(&s->gb, dct_dc_size);
1926
1927
2/2
✓ Branch 0 taken 227 times.
✓ Branch 1 taken 4867 times.
5094 if (dct_dc_size > 8) {
1928
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 227 times.
227 if(!check_marker(s->avctx, &s->gb, "dct_dc_size > 8"))
1929 return AVERROR_INVALIDDATA;
1930 }
1931
1932 }
1933
1934 5336 s->last_dc[cc] += dct_diff;
1935
1936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5336 times.
5336 if (s->mpeg_quant)
1937 block[0] = s->last_dc[cc] * (8 >> s->intra_dc_precision);
1938 else
1939 5336 block[0] = s->last_dc[cc] * (8 >> s->intra_dc_precision) * (8 >> s->dct_precision);
1940 /* TODO: support mpeg_quant for AC coefficients */
1941
1942 5336 block[0] = av_clip(block[0], min, max);
1943 5336 mismatch ^= block[0];
1944
1945 /* AC Coefficients */
1946 while (1) {
1947 33424 group = get_vlc2(&s->gb, cur_vlc->table, STUDIO_INTRA_BITS, 2);
1948
1949
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33424 times.
33424 if (group < 0) {
1950 av_log(s->avctx, AV_LOG_ERROR, "illegal ac coefficient group vlc\n");
1951 return AVERROR_INVALIDDATA;
1952 }
1953
1954 33424 additional_code_len = ac_state_tab[group][0];
1955 33424 cur_vlc = &studio_intra_tab[ac_state_tab[group][1]];
1956
1957
2/2
✓ Branch 0 taken 5336 times.
✓ Branch 1 taken 28088 times.
33424 if (group == 0) {
1958 /* End of Block */
1959 5336 break;
1960
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) {
1961 /* Zero run length (Table B.47) */
1962 2347 run = 1 << additional_code_len;
1963
2/2
✓ Branch 0 taken 927 times.
✓ Branch 1 taken 1420 times.
2347 if (additional_code_len)
1964 927 run += get_bits(&s->gb, additional_code_len);
1965 2347 idx += run;
1966 2347 continue;
1967
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) {
1968 /* Zero run length and +/-1 level (Table B.48) */
1969 6045 code = get_bits(&s->gb, additional_code_len);
1970 6045 sign = code & 1;
1971 6045 code >>= 1;
1972 6045 run = (1 << (additional_code_len - 1)) + code;
1973 6045 idx += run;
1974
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6045 times.
6045 if (idx > 63)
1975 return AVERROR_INVALIDDATA;
1976 6045 j = scantable[idx++];
1977
2/2
✓ Branch 0 taken 3183 times.
✓ Branch 1 taken 2862 times.
6045 block[j] = sign ? 1 : -1;
1978
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) {
1979 /* Level value (Table B.49) */
1980
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19696 times.
19696 if (idx > 63)
1981 return AVERROR_INVALIDDATA;
1982 19696 j = scantable[idx++];
1983 19696 block[j] = get_xbits(&s->gb, additional_code_len);
1984 } else if (group == 21) {
1985 /* Escape */
1986 if (idx > 63)
1987 return AVERROR_INVALIDDATA;
1988 j = scantable[idx++];
1989 additional_code_len = s->avctx->bits_per_raw_sample + s->dct_precision + 4;
1990 flc = get_bits(&s->gb, additional_code_len);
1991 if (flc >> (additional_code_len-1))
1992 block[j] = -1 * (( flc ^ ((1 << additional_code_len) -1)) + 1);
1993 else
1994 block[j] = flc;
1995 }
1996 25741 block[j] = ((block[j] * quant_matrix[j] * s->qscale) * (1 << shift)) / 16;
1997 25741 block[j] = av_clip(block[j], min, max);
1998 25741 mismatch ^= block[j];
1999 }
2000
2001 5336 block[63] ^= mismatch & 1;
2002
2003 5336 return 0;
2004 }
2005
2006 2049 static int mpeg4_decode_dpcm_macroblock(MpegEncContext *s, int16_t macroblock[256], int n)
2007 {
2008 2049 int i, j, w, h, idx = 0;
2009 int block_mean, rice_parameter, rice_prefix_code, rice_suffix_code,
2010 dpcm_residual, left, top, topleft, min_left_top, max_left_top, p, p2, output;
2011
2/2
✓ Branch 0 taken 1366 times.
✓ Branch 1 taken 683 times.
2049 h = 16 >> (n ? s->chroma_y_shift : 0);
2012
2/2
✓ Branch 0 taken 1366 times.
✓ Branch 1 taken 683 times.
2049 w = 16 >> (n ? s->chroma_x_shift : 0);
2013
2014 2049 block_mean = get_bits(&s->gb, s->avctx->bits_per_raw_sample);
2015
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2049 times.
2049 if (block_mean == 0){
2016 av_log(s->avctx, AV_LOG_ERROR, "Forbidden block_mean\n");
2017 return AVERROR_INVALIDDATA;
2018 }
2019 2049 s->last_dc[n] = block_mean * (1 << (s->dct_precision + s->intra_dc_precision));
2020
2021 2049 rice_parameter = get_bits(&s->gb, 4);
2022
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2049 times.
2049 if (rice_parameter == 0) {
2023 av_log(s->avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n");
2024 return AVERROR_INVALIDDATA;
2025 }
2026
2027
2/2
✓ Branch 0 taken 329 times.
✓ Branch 1 taken 1720 times.
2049 if (rice_parameter == 15)
2028 329 rice_parameter = 0;
2029
2030
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2049 times.
2049 if (rice_parameter > 11) {
2031 av_log(s->avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n");
2032 return AVERROR_INVALIDDATA;
2033 }
2034
2035
2/2
✓ Branch 0 taken 32784 times.
✓ Branch 1 taken 2049 times.
34833 for (i = 0; i < h; i++) {
2036 32784 output = 1 << (s->avctx->bits_per_raw_sample - 1);
2037 32784 top = 1 << (s->avctx->bits_per_raw_sample - 1);
2038
2039
2/2
✓ Branch 0 taken 349696 times.
✓ Branch 1 taken 32784 times.
382480 for (j = 0; j < w; j++) {
2040 349696 left = output;
2041 349696 topleft = top;
2042
2043 349696 rice_prefix_code = get_unary(&s->gb, 1, 12);
2044
2045 /* Escape */
2046
2/2
✓ Branch 0 taken 3815 times.
✓ Branch 1 taken 345881 times.
349696 if (rice_prefix_code == 11)
2047 3815 dpcm_residual = get_bits(&s->gb, s->avctx->bits_per_raw_sample);
2048 else {
2049
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 345881 times.
345881 if (rice_prefix_code == 12) {
2050 av_log(s->avctx, AV_LOG_ERROR, "Forbidden rice_prefix_code\n");
2051 return AVERROR_INVALIDDATA;
2052 }
2053 345881 rice_suffix_code = get_bitsz(&s->gb, rice_parameter);
2054 345881 dpcm_residual = (rice_prefix_code << rice_parameter) + rice_suffix_code;
2055 }
2056
2057 /* Map to a signed residual */
2058
2/2
✓ Branch 0 taken 94792 times.
✓ Branch 1 taken 254904 times.
349696 if (dpcm_residual & 1)
2059 94792 dpcm_residual = (-1 * dpcm_residual) >> 1;
2060 else
2061 254904 dpcm_residual = (dpcm_residual >> 1);
2062
2063
2/2
✓ Branch 0 taken 327840 times.
✓ Branch 1 taken 21856 times.
349696 if (i != 0)
2064 327840 top = macroblock[idx-w];
2065
2066 349696 p = left + top - topleft;
2067 349696 min_left_top = FFMIN(left, top);
2068
2/2
✓ Branch 0 taken 44503 times.
✓ Branch 1 taken 305193 times.
349696 if (p < min_left_top)
2069 44503 p = min_left_top;
2070
2071 349696 max_left_top = FFMAX(left, top);
2072
2/2
✓ Branch 0 taken 42683 times.
✓ Branch 1 taken 307013 times.
349696 if (p > max_left_top)
2073 42683 p = max_left_top;
2074
2075 349696 p2 = (FFMIN(min_left_top, topleft) + FFMAX(max_left_top, topleft)) >> 1;
2076
2/2
✓ Branch 0 taken 67194 times.
✓ Branch 1 taken 282502 times.
349696 if (p2 == p)
2077 67194 p2 = block_mean;
2078
2079
2/2
✓ Branch 0 taken 174738 times.
✓ Branch 1 taken 174958 times.
349696 if (p2 > p)
2080 174738 dpcm_residual *= -1;
2081
2082 349696 macroblock[idx++] = output = (dpcm_residual + p) & ((1 << s->avctx->bits_per_raw_sample) - 1);
2083 }
2084 }
2085
2086 2049 return 0;
2087 }
2088
2089 1350 static int mpeg4_decode_studio_mb(MpegEncContext *s, int16_t block_[12][64])
2090 {
2091 1350 Mpeg4DecContext *const ctx = (Mpeg4DecContext*)s;
2092 int i;
2093
2094 1350 ctx->dpcm_direction = 0;
2095
2096 /* StudioMacroblock */
2097 /* Assumes I-VOP */
2098 1350 s->mb_intra = 1;
2099
2/2
✓ Branch 1 taken 667 times.
✓ Branch 2 taken 683 times.
1350 if (get_bits1(&s->gb)) { /* compression_mode */
2100 /* DCT */
2101 /* macroblock_type, 1 or 2-bit VLC */
2102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 667 times.
667 if (!get_bits1(&s->gb)) {
2103 skip_bits1(&s->gb);
2104 s->qscale = mpeg_get_qscale(s);
2105 }
2106
2107
2/2
✓ Branch 0 taken 5336 times.
✓ Branch 1 taken 667 times.
6003 for (i = 0; i < mpeg4_block_count[s->chroma_format]; i++) {
2108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5336 times.
5336 if (mpeg4_decode_studio_block(s, ctx->block32[i], i) < 0)
2109 return AVERROR_INVALIDDATA;
2110 }
2111 } else {
2112 /* DPCM */
2113 683 check_marker(s->avctx, &s->gb, "DPCM block start");
2114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 683 times.
683 ctx->dpcm_direction = get_bits1(&s->gb) ? -1 : 1;
2115
2/2
✓ Branch 0 taken 2049 times.
✓ Branch 1 taken 683 times.
2732 for (i = 0; i < 3; i++) {
2116
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2049 times.
2049 if (mpeg4_decode_dpcm_macroblock(s, ctx->dpcm_macroblock[i], i) < 0)
2117 return AVERROR_INVALIDDATA;
2118 }
2119 }
2120
2121
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) {
2122 29 next_start_code_studio(&s->gb);
2123 29 return SLICE_END;
2124 }
2125
2126 //vcon-stp9L1.bits (first frame)
2127
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1321 times.
1321 if (get_bits_left(&s->gb) == 0)
2128 return SLICE_END;
2129
2130 //vcon-stp2L1.bits, vcon-stp3L1.bits, vcon-stp6L1.bits, vcon-stp7L1.bits, vcon-stp8L1.bits, vcon-stp10L1.bits (first frame)
2131
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)
2132 1 return SLICE_END;
2133
2134 1320 return SLICE_OK;
2135 }
2136
2137 959 static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb)
2138 {
2139 int hours, minutes, seconds;
2140
2141
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 959 times.
959 if (!show_bits(gb, 23)) {
2142 av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n");
2143 return AVERROR_INVALIDDATA;
2144 }
2145
2146 959 hours = get_bits(gb, 5);
2147 959 minutes = get_bits(gb, 6);
2148 959 check_marker(s->avctx, gb, "in gop_header");
2149 959 seconds = get_bits(gb, 6);
2150
2151 959 s->time_base = seconds + 60*(minutes + 60*hours);
2152
2153 959 skip_bits1(gb);
2154 959 skip_bits1(gb);
2155
2156 959 return 0;
2157 }
2158
2159 1220 static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb, int *profile, int *level)
2160 {
2161
2162 1220 *profile = get_bits(gb, 4);
2163 1220 *level = get_bits(gb, 4);
2164
2165 // for Simple profile, level 0
2166
3/4
✓ Branch 0 taken 695 times.
✓ Branch 1 taken 525 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 695 times.
1220 if (*profile == 0 && *level == 8) {
2167 *level = 0;
2168 }
2169
2170 1220 return 0;
2171 }
2172
2173 1220 static int mpeg4_decode_visual_object(MpegEncContext *s, GetBitContext *gb)
2174 {
2175 int visual_object_type;
2176 1220 int is_visual_object_identifier = get_bits1(gb);
2177
2178
2/2
✓ Branch 0 taken 1186 times.
✓ Branch 1 taken 34 times.
1220 if (is_visual_object_identifier) {
2179 1186 skip_bits(gb, 4+3);
2180 }
2181 1220 visual_object_type = get_bits(gb, 4);
2182
2183
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1220 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1220 if (visual_object_type == VOT_VIDEO_ID ||
2184 visual_object_type == VOT_STILL_TEXTURE_ID) {
2185 1220 int video_signal_type = get_bits1(gb);
2186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1220 times.
1220 if (video_signal_type) {
2187 int video_range, color_description;
2188 skip_bits(gb, 3); // video_format
2189 video_range = get_bits1(gb);
2190 color_description = get_bits1(gb);
2191
2192 s->avctx->color_range = video_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
2193
2194 if (color_description) {
2195 s->avctx->color_primaries = get_bits(gb, 8);
2196 s->avctx->color_trc = get_bits(gb, 8);
2197 s->avctx->colorspace = get_bits(gb, 8);
2198 }
2199 }
2200 }
2201
2202 1220 return 0;
2203 }
2204
2205 25 static void mpeg4_load_default_matrices(MpegEncContext *s)
2206 {
2207 int i, v;
2208
2209 /* load default matrices */
2210
2/2
✓ Branch 0 taken 1600 times.
✓ Branch 1 taken 25 times.
1625 for (i = 0; i < 64; i++) {
2211 1600 int j = s->idsp.idct_permutation[i];
2212 1600 v = ff_mpeg4_default_intra_matrix[i];
2213 1600 s->intra_matrix[j] = v;
2214 1600 s->chroma_intra_matrix[j] = v;
2215
2216 1600 v = ff_mpeg4_default_non_intra_matrix[i];
2217 1600 s->inter_matrix[j] = v;
2218 1600 s->chroma_inter_matrix[j] = v;
2219 }
2220 25 }
2221
2222 static int read_quant_matrix_ext(MpegEncContext *s, GetBitContext *gb)
2223 {
2224 int i, j, v;
2225
2226 if (get_bits1(gb)) {
2227 if (get_bits_left(gb) < 64*8)
2228 return AVERROR_INVALIDDATA;
2229 /* intra_quantiser_matrix */
2230 for (i = 0; i < 64; i++) {
2231 v = get_bits(gb, 8);
2232 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2233 s->intra_matrix[j] = v;
2234 s->chroma_intra_matrix[j] = v;
2235 }
2236 }
2237
2238 if (get_bits1(gb)) {
2239 if (get_bits_left(gb) < 64*8)
2240 return AVERROR_INVALIDDATA;
2241 /* non_intra_quantiser_matrix */
2242 for (i = 0; i < 64; i++) {
2243 get_bits(gb, 8);
2244 }
2245 }
2246
2247 if (get_bits1(gb)) {
2248 if (get_bits_left(gb) < 64*8)
2249 return AVERROR_INVALIDDATA;
2250 /* chroma_intra_quantiser_matrix */
2251 for (i = 0; i < 64; i++) {
2252 v = get_bits(gb, 8);
2253 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2254 s->chroma_intra_matrix[j] = v;
2255 }
2256 }
2257
2258 if (get_bits1(gb)) {
2259 if (get_bits_left(gb) < 64*8)
2260 return AVERROR_INVALIDDATA;
2261 /* chroma_non_intra_quantiser_matrix */
2262 for (i = 0; i < 64; i++) {
2263 get_bits(gb, 8);
2264 }
2265 }
2266
2267 next_start_code_studio(gb);
2268 return 0;
2269 }
2270
2271 5 static void extension_and_user_data(MpegEncContext *s, GetBitContext *gb, int id)
2272 {
2273 uint32_t startcode;
2274 uint8_t extension_type;
2275
2276 5 startcode = show_bits_long(gb, 32);
2277
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) {
2278
2279 if ((id == 2 || id == 4) && startcode == EXT_STARTCODE) {
2280 skip_bits_long(gb, 32);
2281 extension_type = get_bits(gb, 4);
2282 if (extension_type == QUANT_MATRIX_EXT_ID)
2283 read_quant_matrix_ext(s, gb);
2284 }
2285 }
2286 5 }
2287
2288 3 static int decode_studio_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2289 {
2290 3 MpegEncContext *s = &ctx->m;
2291 int width, height, aspect_ratio_info;
2292 int bits_per_raw_sample;
2293 int rgb, chroma_format;
2294
2295 // random_accessible_vol and video_object_type_indication have already
2296 // been read by the caller decode_vol_header()
2297 3 skip_bits(gb, 4); /* video_object_layer_verid */
2298 3 ctx->shape = get_bits(gb, 2); /* video_object_layer_shape */
2299 3 skip_bits(gb, 4); /* video_object_layer_shape_extension */
2300 3 skip_bits1(gb); /* progressive_sequence */
2301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ctx->shape != RECT_SHAPE) {
2302 avpriv_request_sample(s->avctx, "MPEG-4 Studio profile non rectangular shape");
2303 return AVERROR_PATCHWELCOME;
2304 }
2305
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (ctx->shape != BIN_ONLY_SHAPE) {
2306 3 rgb = get_bits1(gb); /* rgb_components */
2307 3 chroma_format = get_bits(gb, 2); /* chroma_format */
2308
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)) {
2309 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
2310 return AVERROR_INVALIDDATA;
2311 }
2312
2313 3 bits_per_raw_sample = get_bits(gb, 4); /* bit_depth */
2314
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (bits_per_raw_sample == 10) {
2315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (rgb) {
2316 s->avctx->pix_fmt = AV_PIX_FMT_GBRP10;
2317 } else {
2318
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;
2319 }
2320 } else {
2321 avpriv_request_sample(s->avctx, "MPEG-4 Studio profile bit-depth %u", bits_per_raw_sample);
2322 return AVERROR_PATCHWELCOME;
2323 }
2324
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)
2325 2 s->context_reinit = 1;
2326 3 s->avctx->bits_per_raw_sample = bits_per_raw_sample;
2327 3 ctx->rgb = rgb;
2328 3 s->chroma_format = chroma_format;
2329 }
2330
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (ctx->shape == RECT_SHAPE) {
2331 3 check_marker(s->avctx, gb, "before video_object_layer_width");
2332 3 width = get_bits(gb, 14); /* video_object_layer_width */
2333 3 check_marker(s->avctx, gb, "before video_object_layer_height");
2334 3 height = get_bits(gb, 14); /* video_object_layer_height */
2335 3 check_marker(s->avctx, gb, "after video_object_layer_height");
2336
2337 /* Do the same check as non-studio profile */
2338
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (width && height) {
2339
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 &&
2340
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))
2341 s->context_reinit = 1;
2342 3 s->width = width;
2343 3 s->height = height;
2344 }
2345 }
2346 3 aspect_ratio_info = get_bits(gb, 4);
2347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (aspect_ratio_info == FF_ASPECT_EXTENDED) {
2348 s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
2349 s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
2350 } else {
2351 3 s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[aspect_ratio_info];
2352 }
2353 3 skip_bits(gb, 4); /* frame_rate_code */
2354 3 skip_bits(gb, 15); /* first_half_bit_rate */
2355 3 check_marker(s->avctx, gb, "after first_half_bit_rate");
2356 3 skip_bits(gb, 15); /* latter_half_bit_rate */
2357 3 check_marker(s->avctx, gb, "after latter_half_bit_rate");
2358 3 skip_bits(gb, 15); /* first_half_vbv_buffer_size */
2359 3 check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2360 3 skip_bits(gb, 3); /* latter_half_vbv_buffer_size */
2361 3 skip_bits(gb, 11); /* first_half_vbv_buffer_size */
2362 3 check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2363 3 skip_bits(gb, 15); /* latter_half_vbv_occupancy */
2364 3 check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
2365 3 s->low_delay = get_bits1(gb);
2366 3 s->mpeg_quant = get_bits1(gb); /* mpeg2_stream */
2367
2368 3 next_start_code_studio(gb);
2369 3 extension_and_user_data(s, gb, 2);
2370
2371 3 return 0;
2372 }
2373
2374 1255 static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb)
2375 {
2376 1255 MpegEncContext *s = &ctx->m;
2377 int width, height, vo_ver_id, aspect_ratio_info;
2378
2379 /* vol header */
2380 1255 skip_bits(gb, 1); /* random access */
2381 1255 ctx->vo_type = get_bits(gb, 8);
2382
2383 /* If we are in studio profile (per vo_type), check if its all consistent
2384 * and if so continue pass control to decode_studio_vol_header().
2385 * elIf something is inconsistent, error out
2386 * else continue with (non studio) vol header decpoding.
2387 */
2388
2/2
✓ Branch 0 taken 1252 times.
✓ Branch 1 taken 3 times.
1255 if (ctx->vo_type == CORE_STUDIO_VO_TYPE ||
2389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1252 times.
1252 ctx->vo_type == SIMPLE_STUDIO_VO_TYPE) {
2390
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 != FF_PROFILE_UNKNOWN && s->avctx->profile != FF_PROFILE_MPEG4_SIMPLE_STUDIO)
2391 return AVERROR_INVALIDDATA;
2392 3 s->studio_profile = 1;
2393 3 s->avctx->profile = FF_PROFILE_MPEG4_SIMPLE_STUDIO;
2394 3 return decode_studio_vol_header(ctx, gb);
2395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1252 times.
1252 } else if (s->studio_profile) {
2396 return AVERROR_PATCHWELCOME;
2397 }
2398
2399
2/2
✓ Branch 1 taken 1216 times.
✓ Branch 2 taken 36 times.
1252 if (get_bits1(gb) != 0) { /* is_ol_id */
2400 1216 vo_ver_id = get_bits(gb, 4); /* vo_ver_id */
2401 1216 skip_bits(gb, 3); /* vo_priority */
2402 } else {
2403 36 vo_ver_id = 1;
2404 }
2405 1252 aspect_ratio_info = get_bits(gb, 4);
2406
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1251 times.
1252 if (aspect_ratio_info == FF_ASPECT_EXTENDED) {
2407 1 s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width
2408 1 s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height
2409 } else {
2410 1251 s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[aspect_ratio_info];
2411 }
2412
2413
2/2
✓ Branch 1 taken 1212 times.
✓ Branch 2 taken 40 times.
1252 if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */
2414 1212 int chroma_format = get_bits(gb, 2);
2415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1212 times.
1212 if (chroma_format != CHROMA_420)
2416 av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n");
2417
2418 1212 s->low_delay = get_bits1(gb);
2419
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1212 times.
1212 if (get_bits1(gb)) { /* vbv parameters */
2420 get_bits(gb, 15); /* first_half_bitrate */
2421 check_marker(s->avctx, gb, "after first_half_bitrate");
2422 get_bits(gb, 15); /* latter_half_bitrate */
2423 check_marker(s->avctx, gb, "after latter_half_bitrate");
2424 get_bits(gb, 15); /* first_half_vbv_buffer_size */
2425 check_marker(s->avctx, gb, "after first_half_vbv_buffer_size");
2426 get_bits(gb, 3); /* latter_half_vbv_buffer_size */
2427 get_bits(gb, 11); /* first_half_vbv_occupancy */
2428 check_marker(s->avctx, gb, "after first_half_vbv_occupancy");
2429 get_bits(gb, 15); /* latter_half_vbv_occupancy */
2430 check_marker(s->avctx, gb, "after latter_half_vbv_occupancy");
2431 }
2432 } else {
2433 /* is setting low delay flag only once the smartest thing to do?
2434 * low delay detection will not be overridden. */
2435
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 29 times.
40 if (s->picture_number == 0) {
2436
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5 times.
11 switch (ctx->vo_type) {
2437 6 case SIMPLE_VO_TYPE:
2438 case ADV_SIMPLE_VO_TYPE:
2439 6 s->low_delay = 1;
2440 6 break;
2441 5 default:
2442 5 s->low_delay = 0;
2443 }
2444 }
2445 }
2446
2447 1252 ctx->shape = get_bits(gb, 2); /* vol shape */
2448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1252 times.
1252 if (ctx->shape != RECT_SHAPE)
2449 av_log(s->avctx, AV_LOG_ERROR, "only rectangular vol supported\n");
2450
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1252 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1252 if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) {
2451 av_log(s->avctx, AV_LOG_ERROR, "Gray shape not supported\n");
2452 skip_bits(gb, 4); /* video_object_layer_shape_extension */
2453 }
2454
2455 1252 check_marker(s->avctx, gb, "before time_increment_resolution");
2456
2457 1252 s->avctx->framerate.num = get_bits(gb, 16);
2458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1252 times.
1252 if (!s->avctx->framerate.num) {
2459 av_log(s->avctx, AV_LOG_ERROR, "framerate==0\n");
2460 return AVERROR_INVALIDDATA;
2461 }
2462
2463 1252 ctx->time_increment_bits = av_log2(s->avctx->framerate.num - 1) + 1;
2464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1252 times.
1252 if (ctx->time_increment_bits < 1)
2465 ctx->time_increment_bits = 1;
2466
2467 1252 check_marker(s->avctx, gb, "before fixed_vop_rate");
2468
2469
2/2
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 1226 times.
1252 if (get_bits1(gb) != 0) /* fixed_vop_rate */
2470 26 s->avctx->framerate.den = get_bits(gb, ctx->time_increment_bits);
2471 else
2472 1226 s->avctx->framerate.den = 1;
2473
2474 1252 s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
2475
2476 1252 ctx->t_frame = 0;
2477
2478
1/2
✓ Branch 0 taken 1252 times.
✗ Branch 1 not taken.
1252 if (ctx->shape != BIN_ONLY_SHAPE) {
2479
1/2
✓ Branch 0 taken 1252 times.
✗ Branch 1 not taken.
1252 if (ctx->shape == RECT_SHAPE) {
2480 1252 check_marker(s->avctx, gb, "before width");
2481 1252 width = get_bits(gb, 13);
2482 1252 check_marker(s->avctx, gb, "before height");
2483 1252 height = get_bits(gb, 13);
2484 1252 check_marker(s->avctx, gb, "after height");
2485
2/4
✓ Branch 0 taken 1252 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1252 times.
✗ Branch 3 not taken.
1252 if (width && height && /* they should be non zero but who knows */
2486
3/4
✓ Branch 0 taken 916 times.
✓ Branch 1 taken 336 times.
✓ Branch 2 taken 916 times.
✗ Branch 3 not taken.
1252 !(s->width && s->codec_tag == AV_RL32("MP4S"))) {
2487
3/4
✓ Branch 0 taken 916 times.
✓ Branch 1 taken 336 times.
✓ Branch 2 taken 916 times.
✗ Branch 3 not taken.
1252 if (s->width && s->height &&
2488
3/4
✓ Branch 0 taken 896 times.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 896 times.
916 (s->width != width || s->height != height))
2489 20 s->context_reinit = 1;
2490 1252 s->width = width;
2491 1252 s->height = height;
2492 }
2493 }
2494
2495 1252 s->progressive_sequence =
2496 1252 s->progressive_frame = get_bits1(gb) ^ 1;
2497 1252 s->interlaced_dct = 0;
2498
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 1252 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1252 if (!get_bits1(gb) && (s->avctx->debug & FF_DEBUG_PICT_INFO))
2499 av_log(s->avctx, AV_LOG_INFO, /* OBMC Disable */
2500 "MPEG-4 OBMC not supported (very likely buggy encoder)\n");
2501
2/2
✓ Branch 0 taken 747 times.
✓ Branch 1 taken 505 times.
1252 if (vo_ver_id == 1)
2502 747 ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */
2503 else
2504 505 ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */
2505
2506
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1252 times.
1252 if (ctx->vol_sprite_usage == STATIC_SPRITE)
2507 av_log(s->avctx, AV_LOG_ERROR, "Static Sprites not supported\n");
2508
1/2
✓ Branch 0 taken 1252 times.
✗ Branch 1 not taken.
1252 if (ctx->vol_sprite_usage == STATIC_SPRITE ||
2509
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1252 times.
1252 ctx->vol_sprite_usage == GMC_SPRITE) {
2510 if (ctx->vol_sprite_usage == STATIC_SPRITE) {
2511 skip_bits(gb, 13); // sprite_width
2512 check_marker(s->avctx, gb, "after sprite_width");
2513 skip_bits(gb, 13); // sprite_height
2514 check_marker(s->avctx, gb, "after sprite_height");
2515 skip_bits(gb, 13); // sprite_left
2516 check_marker(s->avctx, gb, "after sprite_left");
2517 skip_bits(gb, 13); // sprite_top
2518 check_marker(s->avctx, gb, "after sprite_top");
2519 }
2520 ctx->num_sprite_warping_points = get_bits(gb, 6);
2521 if (ctx->num_sprite_warping_points > 3) {
2522 av_log(s->avctx, AV_LOG_ERROR,
2523 "%d sprite_warping_points\n",
2524 ctx->num_sprite_warping_points);
2525 ctx->num_sprite_warping_points = 0;
2526 return AVERROR_INVALIDDATA;
2527 }
2528 s->sprite_warping_accuracy = get_bits(gb, 2);
2529 ctx->sprite_brightness_change = get_bits1(gb);
2530 if (ctx->vol_sprite_usage == STATIC_SPRITE)
2531 skip_bits1(gb); // low_latency_sprite
2532 }
2533 // FIXME sadct disable bit if verid!=1 && shape not rect
2534
2535
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1252 times.
1252 if (get_bits1(gb) == 1) { /* not_8_bit */
2536 s->quant_precision = get_bits(gb, 4); /* quant_precision */
2537 if (get_bits(gb, 4) != 8) /* bits_per_pixel */
2538 av_log(s->avctx, AV_LOG_ERROR, "N-bit not supported\n");
2539 if (s->quant_precision != 5)
2540 av_log(s->avctx, AV_LOG_ERROR,
2541 "quant precision %d\n", s->quant_precision);
2542 if (s->quant_precision<3 || s->quant_precision>9) {
2543 s->quant_precision = 5;
2544 }
2545 } else {
2546 1252 s->quant_precision = 5;
2547 }
2548
2549 // FIXME a bunch of grayscale shape things
2550
2551
2/2
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 1229 times.
1252 if ((s->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */
2552 int i, v;
2553
2554 23 mpeg4_load_default_matrices(s);
2555
2556 /* load custom intra matrix */
2557
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 11 times.
23 if (get_bits1(gb)) {
2558 12 int last = 0;
2559
1/2
✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
360 for (i = 0; i < 64; i++) {
2560 int j;
2561
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 360 times.
360 if (get_bits_left(gb) < 8) {
2562 av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2563 return AVERROR_INVALIDDATA;
2564 }
2565 360 v = get_bits(gb, 8);
2566
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 348 times.
360 if (v == 0)
2567 12 break;
2568
2569 348 last = v;
2570 348 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2571 348 s->intra_matrix[j] = last;
2572 348 s->chroma_intra_matrix[j] = last;
2573 }
2574
2575 /* replicate last value */
2576
2/2
✓ Branch 0 taken 420 times.
✓ Branch 1 taken 12 times.
432 for (; i < 64; i++) {
2577 420 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2578 420 s->intra_matrix[j] = last;
2579 420 s->chroma_intra_matrix[j] = last;
2580 }
2581 }
2582
2583 /* load custom non intra matrix */
2584
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 11 times.
23 if (get_bits1(gb)) {
2585 12 int last = 0;
2586
1/2
✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
360 for (i = 0; i < 64; i++) {
2587 int j;
2588
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 360 times.
360 if (get_bits_left(gb) < 8) {
2589 av_log(s->avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n");
2590 return AVERROR_INVALIDDATA;
2591 }
2592 360 v = get_bits(gb, 8);
2593
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 348 times.
360 if (v == 0)
2594 12 break;
2595
2596 348 last = v;
2597 348 j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2598 348 s->inter_matrix[j] = v;
2599 348 s->chroma_inter_matrix[j] = v;
2600 }
2601
2602 /* replicate last value */
2603
2/2
✓ Branch 0 taken 420 times.
✓ Branch 1 taken 12 times.
432 for (; i < 64; i++) {
2604 420 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
2605 420 s->inter_matrix[j] = last;
2606 420 s->chroma_inter_matrix[j] = last;
2607 }
2608 }
2609
2610 // FIXME a bunch of grayscale shape things
2611 }
2612
2613
2/2
✓ Branch 0 taken 505 times.
✓ Branch 1 taken 747 times.
1252 if (vo_ver_id != 1)
2614 505 s->quarter_sample = get_bits1(gb);
2615 else
2616 747 s->quarter_sample = 0;
2617
2618
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1252 times.
1252 if (get_bits_left(gb) < 4) {
2619 av_log(s->avctx, AV_LOG_ERROR, "VOL Header truncated\n");
2620 return AVERROR_INVALIDDATA;
2621 }
2622
2623
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1252 times.
1252 if (!get_bits1(gb)) {
2624 int pos = get_bits_count(gb);
2625 int estimation_method = get_bits(gb, 2);
2626 if (estimation_method < 2) {
2627 if (!get_bits1(gb)) {
2628 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */
2629 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */
2630 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */
2631 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */
2632 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */
2633 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upsampling */
2634 }
2635 if (!get_bits1(gb)) {
2636 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */
2637 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */
2638 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */
2639 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */
2640 }
2641 if (!check_marker(s->avctx, gb, "in complexity estimation part 1")) {
2642 skip_bits_long(gb, pos - get_bits_count(gb));
2643 goto no_cplx_est;
2644 }
2645 if (!get_bits1(gb)) {
2646 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */
2647 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */
2648 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */
2649 ctx->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */
2650 }
2651 if (!get_bits1(gb)) {
2652 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */
2653 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */
2654 ctx->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */
2655 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */
2656 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */
2657 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */
2658 }
2659 if (!check_marker(s->avctx, gb, "in complexity estimation part 2")) {
2660 skip_bits_long(gb, pos - get_bits_count(gb));
2661 goto no_cplx_est;
2662 }
2663 if (estimation_method == 1) {
2664 ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */
2665 ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */
2666 }
2667 } else
2668 av_log(s->avctx, AV_LOG_ERROR,
2669 "Invalid Complexity estimation method %d\n",
2670 estimation_method);
2671 } else {
2672
2673 1252 no_cplx_est:
2674 1252 ctx->cplx_estimation_trash_i =
2675 1252 ctx->cplx_estimation_trash_p =
2676 1252 ctx->cplx_estimation_trash_b = 0;
2677 }
2678
2679 1252 ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */
2680
2681 1252 s->data_partitioning = get_bits1(gb);
2682
2/2
✓ Branch 0 taken 288 times.
✓ Branch 1 taken 964 times.
1252 if (s->data_partitioning)
2683 288 ctx->rvlc = get_bits1(gb);
2684
2685
2/2
✓ Branch 0 taken 505 times.
✓ Branch 1 taken 747 times.
1252 if (vo_ver_id != 1) {
2686 505 ctx->new_pred = get_bits1(gb);
2687
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 505 times.
505 if (ctx->new_pred) {
2688 av_log(s->avctx, AV_LOG_ERROR, "new pred not supported\n");
2689 skip_bits(gb, 2); /* requested upstream message type */
2690 skip_bits1(gb); /* newpred segment type */
2691 }
2692
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 505 times.
505 if (get_bits1(gb)) // reduced_res_vop
2693 av_log(s->avctx, AV_LOG_ERROR,
2694 "reduced resolution VOP not supported\n");
2695 } else {
2696 747 ctx->new_pred = 0;
2697 }
2698
2699 1252 ctx->scalability = get_bits1(gb);
2700
2701
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1252 times.
1252 if (ctx->scalability) {
2702 GetBitContext bak = *gb;
2703 int h_sampling_factor_n;
2704 int h_sampling_factor_m;
2705 int v_sampling_factor_n;
2706 int v_sampling_factor_m;
2707
2708 skip_bits1(gb); // hierarchy_type
2709 skip_bits(gb, 4); /* ref_layer_id */
2710 skip_bits1(gb); /* ref_layer_sampling_dir */
2711 h_sampling_factor_n = get_bits(gb, 5);
2712 h_sampling_factor_m = get_bits(gb, 5);
2713 v_sampling_factor_n = get_bits(gb, 5);
2714 v_sampling_factor_m = get_bits(gb, 5);
2715 ctx->enhancement_type = get_bits1(gb);
2716
2717 if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 ||
2718 v_sampling_factor_n == 0 || v_sampling_factor_m == 0) {
2719 /* illegal scalability header (VERY broken encoder),
2720 * trying to workaround */
2721 ctx->scalability = 0;
2722 *gb = bak;
2723 } else
2724 av_log(s->avctx, AV_LOG_ERROR, "scalability not supported\n");
2725
2726 // bin shape stuff FIXME
2727 }
2728 }
2729
2730
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1252 times.
1252 if (s->avctx->debug&FF_DEBUG_PICT_INFO) {
2731 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",
2732 s->avctx->framerate.den, s->avctx->framerate.num,
2733 ctx->time_increment_bits,
2734 s->quant_precision,
2735 s->progressive_sequence,
2736 s->low_delay,
2737 ctx->scalability ? "scalability " :"" , s->quarter_sample ? "qpel " : "",
2738 s->data_partitioning ? "partition " : "", ctx->rvlc ? "rvlc " : ""
2739 );
2740 }
2741
2742 1252 return 0;
2743 }
2744
2745 /**
2746 * Decode the user data stuff in the header.
2747 * Also initializes divx/xvid/lavc_version/build.
2748 */
2749 76 static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb)
2750 {
2751 76 MpegEncContext *s = &ctx->m;
2752 char buf[256];
2753 int i;
2754 int e;
2755 76 int ver = 0, build = 0, ver2 = 0, ver3 = 0;
2756 char last;
2757
2758
3/4
✓ Branch 0 taken 747 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 730 times.
✓ Branch 4 taken 17 times.
747 for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) {
2759
2/2
✓ Branch 1 taken 59 times.
✓ Branch 2 taken 671 times.
730 if (show_bits(gb, 23) == 0)
2760 59 break;
2761 671 buf[i] = get_bits(gb, 8);
2762 }
2763 76 buf[i] = 0;
2764
2765 /* divx detection */
2766 76 e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last);
2767
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 if (e < 2)
2768 76 e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last);
2769
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 65 times.
76 if (e >= 2) {
2770 11 ctx->divx_version = ver;
2771 11 ctx->divx_build = build;
2772
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';
2773 }
2774
2775 /* libavcodec detection */
2776 76 e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3;
2777
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 if (e != 4)
2778 76 e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build);
2779
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 if (e != 4) {
2780 76 e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1;
2781
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 65 times.
76 if (e > 1) {
2782
3/6
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 11 times.
11 if (ver > 0xFFU || ver2 > 0xFFU || ver3 > 0xFFU) {
2783 av_log(s->avctx, AV_LOG_WARNING,
2784 "Unknown Lavc version string encountered, %d.%d.%d; "
2785 "clamping sub-version values to 8-bits.\n",
2786 ver, ver2, ver3);
2787 }
2788 11 build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF);
2789 }
2790 }
2791
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 11 times.
76 if (e != 4) {
2792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (strcmp(buf, "ffmpeg") == 0)
2793 ctx->lavc_build = 4600;
2794 }
2795
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 65 times.
76 if (e == 4)
2796 11 ctx->lavc_build = build;
2797
2798 /* Xvid detection */
2799 76 e = sscanf(buf, "XviD%d", &build);
2800
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 50 times.
76 if (e == 1)
2801 26 ctx->xvid_build = build;
2802
2803 76 return 0;
2804 }
2805
2806 3477 int ff_mpeg4_workaround_bugs(AVCodecContext *avctx)
2807 {
2808 3477 Mpeg4DecContext *ctx = avctx->priv_data;
2809 3477 MpegEncContext *s = &ctx->m;
2810
2811
5/6
✓ Branch 0 taken 3413 times.
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 3413 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3326 times.
✓ Branch 5 taken 87 times.
3477 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) {
2812
1/2
✓ Branch 0 taken 3326 times.
✗ Branch 1 not taken.
3326 if (s->codec_tag == AV_RL32("XVID") ||
2813
1/2
✓ Branch 0 taken 3326 times.
✗ Branch 1 not taken.
3326 s->codec_tag == AV_RL32("XVIX") ||
2814
1/2
✓ Branch 0 taken 3326 times.
✗ Branch 1 not taken.
3326 s->codec_tag == AV_RL32("RMP4") ||
2815
1/2
✓ Branch 0 taken 3326 times.
✗ Branch 1 not taken.
3326 s->codec_tag == AV_RL32("ZMP4") ||
2816
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3326 times.
3326 s->codec_tag == AV_RL32("SIPP"))
2817 ctx->xvid_build = 0;
2818 }
2819
2820
5/6
✓ Branch 0 taken 3413 times.
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 3413 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3326 times.
✓ Branch 5 taken 87 times.
3477 if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1)
2821
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3326 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3326 if (s->codec_tag == AV_RL32("DIVX") && ctx->vo_type == 0 &&
2822 ctx->vol_control_parameters == 0)
2823 ctx->divx_version = 400; // divx 4
2824
2825
4/4
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 3413 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 60 times.
3477 if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) {
2826 4 ctx->divx_version =
2827 4 ctx->divx_build = -1;
2828 }
2829
2830
1/2
✓ Branch 0 taken 3477 times.
✗ Branch 1 not taken.
3477 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
2831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
3477 if (s->codec_tag == AV_RL32("XVIX"))
2832 s->workaround_bugs |= FF_BUG_XVID_ILACE;
2833
2834
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
3477 if (s->codec_tag == AV_RL32("UMP4"))
2835 s->workaround_bugs |= FF_BUG_UMP4;
2836
2837
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3477 if (ctx->divx_version >= 500 && ctx->divx_build < 1814)
2838 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2839
2840
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3477 if (ctx->divx_version > 502 && ctx->divx_build < 1814)
2841 s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
2842
2843
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
3477 if (ctx->xvid_build <= 3U)
2844 s->padding_bug_score = 256 * 256 * 256 * 64;
2845
2846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
3477 if (ctx->xvid_build <= 1U)
2847 s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
2848
2849
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
3477 if (ctx->xvid_build <= 12U)
2850 s->workaround_bugs |= FF_BUG_EDGE;
2851
2852
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
3477 if (ctx->xvid_build <= 32U)
2853 s->workaround_bugs |= FF_BUG_DC_CLIP;
2854
2855 #define SET_QPEL_FUNC(postfix1, postfix2) \
2856 s->qdsp.put_ ## postfix1 = ff_put_ ## postfix2; \
2857 s->qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
2858 s->qdsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
2859
2860
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
3477 if (ctx->lavc_build < 4653U)
2861 s->workaround_bugs |= FF_BUG_STD_QPEL;
2862
2863
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
3477 if (ctx->lavc_build < 4655U)
2864 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2865
2866
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
3477 if (ctx->lavc_build < 4670U)
2867 s->workaround_bugs |= FF_BUG_EDGE;
2868
2869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
3477 if (ctx->lavc_build <= 4712U)
2870 s->workaround_bugs |= FF_BUG_DC_CLIP;
2871
2872
1/2
✓ Branch 0 taken 3477 times.
✗ Branch 1 not taken.
3477 if ((ctx->lavc_build&0xFF) >= 100) {
2873
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3476 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
3477 if (ctx->lavc_build > 3621476 && ctx->lavc_build < 3752552 &&
2874 (ctx->lavc_build < 3752037 || ctx->lavc_build > 3752191) // 3.2.1+
2875 )
2876 s->workaround_bugs |= FF_BUG_IEDGE;
2877 }
2878
2879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
3477 if (ctx->divx_version >= 0)
2880 s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
2881
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3477 if (ctx->divx_version == 501 && ctx->divx_build == 20020416)
2882 s->padding_bug_score = 256 * 256 * 256 * 64;
2883
2884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
3477 if (ctx->divx_version < 500U)
2885 s->workaround_bugs |= FF_BUG_EDGE;
2886
2887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
3477 if (ctx->divx_version >= 0)
2888 s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
2889 }
2890
2891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
3477 if (s->workaround_bugs & FF_BUG_STD_QPEL) {
2892 SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
2893 SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
2894 SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
2895 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
2896 SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
2897 SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
2898
2899 SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
2900 SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
2901 SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
2902 SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
2903 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
2904 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
2905 }
2906
2907
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3477 times.
3477 if (avctx->debug & FF_DEBUG_BUGS)
2908 av_log(s->avctx, AV_LOG_DEBUG,
2909 "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
2910 s->workaround_bugs, ctx->lavc_build, ctx->xvid_build,
2911 ctx->divx_version, ctx->divx_build, s->divx_packed ? "p" : "");
2912
2913
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 3413 times.
3477 if (CONFIG_MPEG4_DECODER && ctx->xvid_build >= 0 &&
2914
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 s->codec_id == AV_CODEC_ID_MPEG4 &&
2915
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 60 times.
64 avctx->idct_algo == FF_IDCT_AUTO) {
2916 4 avctx->idct_algo = FF_IDCT_XVID;
2917 4 ff_mpv_idct_init(s);
2918 4 return 1;
2919 }
2920
2921 3473 return 0;
2922 }
2923
2924 6952 static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb,
2925 int parse_only)
2926 {
2927 6952 MpegEncContext *s = &ctx->m;
2928 int time_incr, time_increment;
2929 int64_t pts;
2930
2931 6952 s->mcsel = 0;
2932 6952 s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */
2933
3/4
✓ Branch 0 taken 1365 times.
✓ Branch 1 taken 5587 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1365 times.
6952 if (s->pict_type == AV_PICTURE_TYPE_B && s->low_delay &&
2934 ctx->vol_control_parameters == 0 && !(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)) {
2935 av_log(s->avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n");
2936 s->low_delay = 0;
2937 }
2938
2939
4/4
✓ Branch 0 taken 1284 times.
✓ Branch 1 taken 5668 times.
✓ Branch 2 taken 1028 times.
✓ Branch 3 taken 256 times.
6952 s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
2940
2/2
✓ Branch 0 taken 1028 times.
✓ Branch 1 taken 5924 times.
6952 if (s->partitioned_frame)
2941 1028 s->decode_mb = mpeg4_decode_partitioned_mb;
2942 else
2943 5924 s->decode_mb = mpeg4_decode_mb;
2944
2945 6952 time_incr = 0;
2946
2/2
✓ Branch 1 taken 249 times.
✓ Branch 2 taken 6952 times.
7201 while (get_bits1(gb) != 0)
2947 249 time_incr++;
2948
2949 6952 check_marker(s->avctx, gb, "before time_increment");
2950
2951
2/2
✓ Branch 0 taken 6950 times.
✓ Branch 1 taken 2 times.
6952 if (ctx->time_increment_bits == 0 ||
2952
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 6942 times.
6950 !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) {
2953 10 av_log(s->avctx, AV_LOG_WARNING,
2954 "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);
2955
2956 10 for (ctx->time_increment_bits = 1;
2957
1/2
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
110 ctx->time_increment_bits < 16;
2958 100 ctx->time_increment_bits++) {
2959
1/2
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
110 if (s->pict_type == AV_PICTURE_TYPE_P ||
2960
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 (s->pict_type == AV_PICTURE_TYPE_S &&
2961 ctx->vol_sprite_usage == GMC_SPRITE)) {
2962 if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30)
2963 break;
2964
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)
2965 10 break;
2966 }
2967
2968 10 av_log(s->avctx, AV_LOG_WARNING,
2969 "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits);
2970
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
10 if (s->avctx->framerate.num && 4*s->avctx->framerate.num < 1<<ctx->time_increment_bits) {
2971 6 s->avctx->framerate.num = 1<<ctx->time_increment_bits;
2972 6 s->avctx->time_base = av_inv_q(av_mul_q(s->avctx->framerate, (AVRational){s->avctx->ticks_per_frame, 1}));
2973 }
2974 }
2975
2976 if (IS_3IV1)
2977 time_increment = get_bits1(gb); // FIXME investigate further
2978 else
2979 6952 time_increment = get_bits(gb, ctx->time_increment_bits);
2980
2981
2/2
✓ Branch 0 taken 5587 times.
✓ Branch 1 taken 1365 times.
6952 if (s->pict_type != AV_PICTURE_TYPE_B) {
2982 5587 s->last_time_base = s->time_base;
2983 5587 s->time_base += time_incr;
2984 5587 s->time = s->time_base * (int64_t)s->avctx->framerate.num + time_increment;
2985
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5587 times.
5587 if (s->workaround_bugs & FF_BUG_UMP4) {
2986 if (s->time < s->last_non_b_time) {
2987 /* header is not mpeg-4-compatible, broken encoder,
2988 * trying to workaround */
2989 s->time_base++;
2990 s->time += s->avctx->framerate.num;
2991 }
2992 }
2993 5587 s->pp_time = s->time - s->last_non_b_time;
2994 5587 s->last_non_b_time = s->time;
2995 } else {
2996 1365 s->time = (s->last_time_base + time_incr) * (int64_t)s->avctx->framerate.num + time_increment;
2997 1365 s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
2998
1/2
✓ Branch 0 taken 1365 times.
✗ Branch 1 not taken.
1365 if (s->pp_time <= s->pb_time ||
2999
1/2
✓ Branch 0 taken 1365 times.
✗ Branch 1 not taken.
1365 s->pp_time <= s->pp_time - s->pb_time ||
3000
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1365 times.
1365 s->pp_time <= 0) {
3001 /* messed up order, maybe after seeking? skipping current B-frame */
3002 return FRAME_SKIPPED;
3003 }
3004 1365 ff_mpeg4_init_direct_mv(s);
3005
3006
2/2
✓ Branch 0 taken 215 times.
✓ Branch 1 taken 1150 times.
1365 if (ctx->t_frame == 0)
3007 215 ctx->t_frame = s->pb_time;
3008
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1365 times.
1365 if (ctx->t_frame == 0)
3009 ctx->t_frame = 1; // 1/0 protection
3010
1/2
✓ Branch 0 taken 1365 times.
✗ Branch 1 not taken.
1365 s->pp_field_time = (ROUNDED_DIV(s->last_non_b_time, ctx->t_frame) -
3011
1/2
✓ Branch 0 taken 1365 times.
✗ Branch 1 not taken.
1365 ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
3012
1/2
✓ Branch 0 taken 1365 times.
✗ Branch 1 not taken.
1365 s->pb_field_time = (ROUNDED_DIV(s->time, ctx->t_frame) -
3013
1/2
✓ Branch 0 taken 1365 times.
✗ Branch 1 not taken.
1365 ROUNDED_DIV(s->last_non_b_time - s->pp_time, ctx->t_frame)) * 2;
3014
3/4
✓ Branch 0 taken 1363 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1363 times.
1365 if (s->pp_field_time <= s->pb_field_time || s->pb_field_time <= 1) {
3015 2 s->pb_field_time = 2;
3016 2 s->pp_field_time = 4;
3017
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->progressive_sequence)
3018 return FRAME_SKIPPED;
3019 }
3020 }
3021
3022
1/2
✓ Branch 0 taken 6952 times.
✗ Branch 1 not taken.
6952 if (s->avctx->framerate.den)
3023
1/2
✓ Branch 0 taken 6952 times.
✗ Branch 1 not taken.
6952 pts = ROUNDED_DIV(s->time, s->avctx->framerate.den);
3024 else
3025 pts = AV_NOPTS_VALUE;
3026 ff_dlog(s->avctx, "MPEG4 PTS: %"PRId64"\n", pts);
3027
3028 6952 check_marker(s->avctx, gb, "before vop_coded");
3029
3030 /* vop coded */
3031
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 6944 times.
6952 if (get_bits1(gb) != 1) {
3032
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
3033 av_log(s->avctx, AV_LOG_ERROR, "vop not coded\n");
3034 8 return FRAME_SKIPPED;
3035 }
3036
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6944 times.
6944 if (ctx->new_pred)
3037 decode_new_pred(ctx, gb);
3038
3039
1/2
✓ Branch 0 taken 6944 times.
✗ Branch 1 not taken.
6944 if (ctx->shape != BIN_ONLY_SHAPE &&
3040
2/2
✓ Branch 0 taken 2381 times.
✓ Branch 1 taken 4563 times.
6944 (s->pict_type == AV_PICTURE_TYPE_P ||
3041
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2381 times.
2381 (s->pict_type == AV_PICTURE_TYPE_S &&
3042 ctx->vol_sprite_usage == GMC_SPRITE))) {
3043 /* rounding type for motion estimation */
3044 4563 s->no_rounding = get_bits1(gb);
3045 } else {
3046 2381 s->no_rounding = 0;
3047 }
3048 // FIXME reduced res stuff
3049
3050
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6944 times.
6944 if (ctx->shape != RECT_SHAPE) {
3051 if (ctx->vol_sprite_usage != 1 || s->pict_type != AV_PICTURE_TYPE_I) {
3052 skip_bits(gb, 13); /* width */
3053 check_marker(s->avctx, gb, "after width");
3054 skip_bits(gb, 13); /* height */
3055 check_marker(s->avctx, gb, "after height");
3056 skip_bits(gb, 13); /* hor_spat_ref */
3057 check_marker(s->avctx, gb, "after hor_spat_ref");
3058 skip_bits(gb, 13); /* ver_spat_ref */
3059 }
3060 skip_bits1(gb); /* change_CR_disable */
3061
3062 if (get_bits1(gb) != 0)
3063 skip_bits(gb, 8); /* constant_alpha_value */
3064 }
3065
3066 // FIXME complexity estimation stuff
3067
3068
1/2
✓ Branch 0 taken 6944 times.
✗ Branch 1 not taken.
6944 if (ctx->shape != BIN_ONLY_SHAPE) {
3069 6944 skip_bits_long(gb, ctx->cplx_estimation_trash_i);
3070
2/2
✓ Branch 0 taken 5928 times.
✓ Branch 1 taken 1016 times.
6944 if (s->pict_type != AV_PICTURE_TYPE_I)
3071 5928 skip_bits_long(gb, ctx->cplx_estimation_trash_p);
3072
2/2
✓ Branch 0 taken 1365 times.
✓ Branch 1 taken 5579 times.
6944 if (s->pict_type == AV_PICTURE_TYPE_B)
3073 1365 skip_bits_long(gb, ctx->cplx_estimation_trash_b);
3074
3075
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6944 times.
6944 if (get_bits_left(gb) < 3) {
3076 av_log(s->avctx, AV_LOG_ERROR, "Header truncated\n");
3077 return AVERROR_INVALIDDATA;
3078 }
3079 6944 ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)];
3080
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 6894 times.
6944 if (!s->progressive_sequence) {
3081 50 s->top_field_first = get_bits1(gb);
3082 50 s->alternate_scan = get_bits1(gb);
3083 } else
3084 6894 s->alternate_scan = 0;
3085 }
3086
3087
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6938 times.
6944 if (s->alternate_scan) {
3088 6 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
3089 6 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
3090 6 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
3091 6 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
3092 } else {
3093 6938 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
3094 6938 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
3095 6938 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
3096 6938 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
3097 }
3098
3099 /* Skip at this point when only parsing since the remaining
3100 * data is not useful for a parser and requires the
3101 * sprite_trajectory VLC to be initialized. */
3102
2/2
✓ Branch 0 taken 3468 times.
✓ Branch 1 taken 3476 times.
6944 if (parse_only)
3103 3468 goto end;
3104
3105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3476 times.
3476 if (s->pict_type == AV_PICTURE_TYPE_S) {
3106 if((ctx->vol_sprite_usage == STATIC_SPRITE ||
3107 ctx->vol_sprite_usage == GMC_SPRITE)) {
3108 if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0)
3109 return AVERROR_INVALIDDATA;
3110 if (ctx->sprite_brightness_change)
3111 av_log(s->avctx, AV_LOG_ERROR,
3112 "sprite_brightness_change not supported\n");
3113 if (ctx->vol_sprite_usage == STATIC_SPRITE)
3114 av_log(s->avctx, AV_LOG_ERROR, "static sprite not supported\n");
3115 } else {
3116 memset(s->sprite_offset, 0, sizeof(s->sprite_offset));
3117 memset(s->sprite_delta, 0, sizeof(s->sprite_delta));
3118 }
3119 }
3120
3121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3476 times.
3476 if (ctx->shape != BIN_ONLY_SHAPE) {
3122 3476 s->chroma_qscale = s->qscale = get_bits(gb, s->quant_precision);
3123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3476 times.
3476 if (s->qscale == 0) {
3124 av_log(s->avctx, AV_LOG_ERROR,
3125 "Error, header damaged or not MPEG-4 header (qscale=0)\n");
3126 return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
3127 }
3128
3129
2/2
✓ Branch 0 taken 3026 times.
✓ Branch 1 taken 450 times.
3476 if (s->pict_type != AV_PICTURE_TYPE_I) {
3130 3026 s->f_code = get_bits(gb, 3); /* fcode_for */
3131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3026 times.
3026 if (s->f_code == 0) {
3132 av_log(s->avctx, AV_LOG_ERROR,
3133 "Error, header damaged or not MPEG-4 header (f_code=0)\n");
3134 s->f_code = 1;
3135 return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then
3136 }
3137 } else
3138 450 s->f_code = 1;
3139
3140
2/2
✓ Branch 0 taken 683 times.
✓ Branch 1 taken 2793 times.
3476 if (s->pict_type == AV_PICTURE_TYPE_B) {
3141 683 s->b_code = get_bits(gb, 3);
3142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 683 times.
683 if (s->b_code == 0) {
3143 av_log(s->avctx, AV_LOG_ERROR,
3144 "Error, header damaged or not MPEG4 header (b_code=0)\n");
3145 s->b_code=1;
3146 return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly
3147 }
3148 } else
3149 2793 s->b_code = 1;
3150
3151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3476 times.
3476 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
3152 av_log(s->avctx, AV_LOG_DEBUG,
3153 "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",
3154 s->qscale, s->f_code, s->b_code,
3155 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')),
3156 gb->size_in_bits,s->progressive_sequence, s->alternate_scan,
3157 s->top_field_first, s->quarter_sample ? 'q' : 'h',
3158 s->data_partitioning, ctx->resync_marker,
3159 ctx->num_sprite_warping_points, s->sprite_warping_accuracy,
3160 1 - s->no_rounding, ctx->vo_type,
3161 ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold,
3162 ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p,
3163 ctx->cplx_estimation_trash_b,
3164 s->time,
3165 time_increment
3166 );
3167 }
3168
3169
1/2
✓ Branch 0 taken 3476 times.
✗ Branch 1 not taken.
3476 if (!ctx->scalability) {
3170
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3476 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3476 if (ctx->shape != RECT_SHAPE && s->pict_type != AV_PICTURE_TYPE_I)
3171 skip_bits1(gb); // vop shape coding type
3172 } else {
3173 if (ctx->enhancement_type) {
3174 int load_backward_shape = get_bits1(gb);
3175 if (load_backward_shape)
3176 av_log(s->avctx, AV_LOG_ERROR,
3177 "load backward shape isn't supported\n");
3178 }
3179 skip_bits(gb, 2); // ref_select_code
3180 }
3181 }
3182
3183 end:
3184 /* detect buggy encoders which don't set the low_delay flag
3185 * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames
3186 * easily (although it's buggy too) */
3187
3/4
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 6892 times.
✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
6944 if (ctx->vo_type == 0 && ctx->vol_control_parameters == 0 &&
3188
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) {
3189 2 av_log(s->avctx, AV_LOG_WARNING,
3190 "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n");
3191 2 s->low_delay = 1;
3192 }
3193
3194 6944 s->picture_number++; // better than pic number==0 always ;)
3195
3196 // FIXME add short header support
3197 6944 s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table;
3198 6944 s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table;
3199
3200
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6944 times.
6944 if (s->workaround_bugs & FF_BUG_EDGE) {
3201 s->h_edge_pos = s->width;
3202 s->v_edge_pos = s->height;
3203 }
3204 6944 return 0;
3205 }
3206
3207 2 static void decode_smpte_tc(Mpeg4DecContext *ctx, GetBitContext *gb)
3208 {
3209 2 MpegEncContext *s = &ctx->m;
3210
3211 2 skip_bits(gb, 16); /* Time_code[63..48] */
3212 2 check_marker(s->avctx, gb, "after Time_code[63..48]");
3213 2 skip_bits(gb, 16); /* Time_code[47..32] */
3214 2 check_marker(s->avctx, gb, "after Time_code[47..32]");
3215 2 skip_bits(gb, 16); /* Time_code[31..16] */
3216 2 check_marker(s->avctx, gb, "after Time_code[31..16]");
3217 2 skip_bits(gb, 16); /* Time_code[15..0] */
3218 2 check_marker(s->avctx, gb, "after Time_code[15..0]");
3219 2 skip_bits(gb, 4); /* reserved_bits */
3220 2 }
3221
3222 /**
3223 * Decode the next studio vop header.
3224 * @return <0 if something went wrong
3225 */
3226 2 static int decode_studio_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb)
3227 {
3228 2 MpegEncContext *s = &ctx->m;
3229
3230
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (get_bits_left(gb) <= 32)
3231 return 0;
3232
3233 2 s->partitioned_frame = 0;
3234 2 s->interlaced_dct = 0;
3235 2 s->decode_mb = mpeg4_decode_studio_mb;
3236
3237 2 decode_smpte_tc(ctx, gb);
3238
3239 2 skip_bits(gb, 10); /* temporal_reference */
3240 2 skip_bits(gb, 2); /* vop_structure */
3241 2 s->pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* vop_coding_type */
3242
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (get_bits1(gb)) { /* vop_coded */
3243 2 skip_bits1(gb); /* top_field_first */
3244 2 skip_bits1(gb); /* repeat_first_field */
3245 2 s->progressive_frame = get_bits1(gb) ^ 1; /* progressive_frame */
3246 }
3247
3248
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (s->pict_type == AV_PICTURE_TYPE_I) {
3249
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (get_bits1(gb))
3250 reset_studio_dc_predictors(s);
3251 }
3252
3253
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ctx->shape != BIN_ONLY_SHAPE) {
3254 2 s->alternate_scan = get_bits1(gb);
3255 2 s->frame_pred_frame_dct = get_bits1(gb);
3256 2 s->dct_precision = get_bits(gb, 2);
3257 2 s->intra_dc_precision = get_bits(gb, 2);
3258 2 s->q_scale_type = get_bits1(gb);
3259 }
3260
3261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->alternate_scan) {
3262 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
3263 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
3264 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_vertical_scan);
3265 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
3266 } else {
3267 2 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
3268 2 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
3269 2 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
3270 2 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
3271 }
3272
3273 2 mpeg4_load_default_matrices(s);
3274
3275 2 next_start_code_studio(gb);
3276 2 extension_and_user_data(s, gb, 4);
3277
3278 2 return 0;
3279 }
3280
3281 static int decode_studiovisualobject(Mpeg4DecContext *ctx, GetBitContext *gb)
3282 {
3283 MpegEncContext *s = &ctx->m;
3284 int visual_object_type;
3285
3286 skip_bits(gb, 4); /* visual_object_verid */
3287 visual_object_type = get_bits(gb, 4);
3288 if (visual_object_type != VOT_VIDEO_ID) {
3289 avpriv_request_sample(s->avctx, "VO type %u", visual_object_type);
3290 return AVERROR_PATCHWELCOME;
3291 }
3292
3293 next_start_code_studio(gb);
3294 extension_and_user_data(s, gb, 1);
3295
3296 return 0;
3297 }
3298
3299 /**
3300 * Decode MPEG-4 headers.
3301 *
3302 * @param header If set the absence of a VOP is not treated as error; otherwise, it is treated as such.
3303 * @param parse_only If set, things only relevant to a decoder may be skipped;
3304 * furthermore, the VLC tables may be uninitialized.
3305 * @return <0 if an error occurred
3306 * FRAME_SKIPPED if a not coded VOP is found
3307 * 0 else
3308 */
3309 7452 int ff_mpeg4_decode_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb,
3310 int header, int parse_only)
3311 {
3312 7452 MpegEncContext *s = &ctx->m;
3313 unsigned startcode, v;
3314 int ret;
3315 7452 int vol = 0;
3316
3317 /* search next start code */
3318 7452 align_get_bits(gb);
3319
3320 // If we have not switched to studio profile than we also did not switch bps
3321 // that means something else (like a previous instance) outside set bps which
3322 // would be inconsistant with the currect state, thus reset it
3323
3/4
✓ Branch 0 taken 7450 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 7450 times.
✗ Branch 3 not taken.
7452 if (!s->studio_profile && s->avctx->bits_per_raw_sample != 8)
3324 7450 s->avctx->bits_per_raw_sample = 0;
3325
3326
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7452 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
7452 if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) {
3327 skip_bits(gb, 24);
3328 if (get_bits(gb, 8) == 0xF0)
3329 goto end;
3330 }
3331
3332 7452 startcode = 0xff;
3333 for (;;) {
3334
2/2
✓ Branch 1 taken 498 times.
✓ Branch 2 taken 51774 times.
52272 if (get_bits_count(gb) >= gb->size_in_bits) {
3335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 498 times.
498 if (gb->size_in_bits == 8 &&
3336
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 498 times.
498 (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) {
3337 av_log(s->av