FFmpeg coverage


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