../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
1176 / 1622
72.5%
Branches:
678 / 1135
59.7%
Functions:
30 / 33
90.9%

libavcodec/mpeg12dec.c
Line Branch Exec Source
1 /*
2 * MPEG-1/2 decoder
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2013 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 /**
24 * @file
25 * MPEG-1/2 decoder
26 */
27
28 #include "config_components.h"
29
30 #define UNCHECKED_BITSTREAM_READER 1
31 #include <inttypes.h>
32 #include <stdatomic.h>
33
34 #include "libavutil/attributes.h"
35 #include "libavutil/imgutils.h"
36 #include "libavutil/internal.h"
37 #include "libavutil/mem_internal.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavutil/reverse.h"
40 #include "libavutil/stereo3d.h"
41 #include "libavutil/timecode.h"
42
43 #include "avcodec.h"
44 #include "codec_internal.h"
45 #include "decode.h"
46 #include "error_resilience.h"
47 #include "get_bits.h"
48 #include "hwaccel_internal.h"
49 #include "hwconfig.h"
50 #include "idctdsp.h"
51 #include "mpeg_er.h"
52 #include "mpeg12.h"
53 #include "mpeg12data.h"
54 #include "mpeg12dec.h"
55 #include "mpegutils.h"
56 #include "mpegvideo.h"
57 #include "mpegvideodata.h"
58 #include "mpegvideodec.h"
59 #include "profiles.h"
60 #include "startcode.h"
61
62 #define A53_MAX_CC_COUNT 2000
63
64 enum Mpeg2ClosedCaptionsFormat {
65 CC_FORMAT_AUTO,
66 CC_FORMAT_A53_PART4,
67 CC_FORMAT_SCTE20,
68 CC_FORMAT_DVD,
69 CC_FORMAT_DISH
70 };
71
72 typedef struct Mpeg12SliceContext {
73 MPVContext c;
74 GetBitContext gb;
75
76 int last_dc[3]; ///< last DC values
77
78 DECLARE_ALIGNED_32(int16_t, block)[12][64];
79 } Mpeg12SliceContext;
80
81 typedef struct Mpeg1Context {
82 Mpeg12SliceContext slice;
83 AVPanScan pan_scan; /* some temporary storage for the panscan */
84 enum AVStereo3DType stereo3d_type;
85 int has_stereo3d;
86 AVBufferRef *a53_buf_ref;
87 enum Mpeg2ClosedCaptionsFormat cc_format;
88 uint8_t afd;
89 int has_afd;
90 int slice_count;
91 unsigned aspect_ratio_info;
92 int save_progressive_seq, save_chroma_format;
93 AVRational frame_rate_ext; /* MPEG-2 specific framerate modificator */
94 unsigned frame_rate_index;
95 int sync; /* Did we reach a sync point like a GOP/SEQ/KEYFrame? */
96 int closed_gop;
97 int tmpgexs;
98 int first_slice;
99 int extradata_decoded;
100 int vbv_delay;
101 int64_t bit_rate;
102 int64_t timecode_frame_start; /*< GOP timecode frame start number, in non drop frame format */
103 } Mpeg1Context;
104
105 /* as H.263, but only 17 codes */
106 5942498 static int mpeg_decode_motion(Mpeg12SliceContext *const s, int fcode, int pred)
107 {
108 int code, sign, val, shift;
109
110 5942498 code = get_vlc2(&s->gb, ff_mv_vlc, MV_VLC_BITS, 2);
111
2/2
✓ Branch 3 → 4 taken 2411939 times.
✓ Branch 3 → 5 taken 3530559 times.
5942498 if (code == 0)
112 2411939 return pred;
113
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 3530559 times.
3530559 if (code < 0)
114 ✗ return 0xffff;
115
116 3530559 sign = get_bits1(&s->gb);
117 3530559 shift = fcode - 1;
118 3530559 val = code;
119
2/2
✓ Branch 8 → 9 taken 3192043 times.
✓ Branch 8 → 11 taken 338516 times.
3530559 if (shift) {
120 3192043 val = (val - 1) << shift;
121 3192043 val |= get_bits(&s->gb, shift);
122 3192043 val++;
123 }
124
2/2
✓ Branch 11 → 12 taken 1823106 times.
✓ Branch 11 → 13 taken 1707453 times.
3530559 if (sign)
125 1823106 val = -val;
126 3530559 val += pred;
127
128 /* modulo decoding */
129 3530559 return sign_extend(val, 5 + shift);
130 }
131
132 #define MAX_INDEX (64 - 1)
133 #define check_scantable_index(ctx, x) \
134 do { \
135 if ((x) > MAX_INDEX) { \
136 av_log(ctx->c.avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
137 ctx->c.mb_x, ctx->c.mb_y); \
138 return AVERROR_INVALIDDATA; \
139 } \
140 } while (0)
141
142 403182 static inline int mpeg1_decode_block_inter(Mpeg12SliceContext *const s,
143 int16_t *block, int n)
144 {
145 int level, i, j, run;
146 403182 const uint8_t *const scantable = s->c.intra_scantable.permutated;
147 403182 const uint16_t *quant_matrix = s->c.inter_matrix;
148 403182 const int qscale = s->c.qscale;
149
150 {
151 403182 OPEN_READER(re, &s->gb);
152 403182 i = -1;
153 // special case for first coefficient, no need to add second VLC table
154 403182 UPDATE_CACHE(re, &s->gb);
155
2/2
✓ Branch 2 → 3 taken 301257 times.
✓ Branch 2 → 4 taken 101925 times.
403182 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
156 101925 level = (3 * qscale * quant_matrix[0]) >> 5;
157 101925 level = (level - 1) | 1;
158
2/2
✓ Branch 4 → 5 taken 57008 times.
✓ Branch 4 → 6 taken 44917 times.
101925 if (GET_CACHE(re, &s->gb) & 0x40000000)
159 57008 level = -level;
160 101925 block[0] = level;
161 101925 i++;
162 101925 SKIP_BITS(re, &s->gb, 2);
163
2/2
✓ Branch 6 → 3 taken 67516 times.
✓ Branch 6 → 7 taken 34409 times.
101925 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
164 34409 goto end;
165 }
166 /* now quantify & encode AC coefficients */
167 for (;;) {
168
2/2
✓ Branch 9 → 10 taken 335978 times.
✓ Branch 9 → 12 taken 2584676 times.
2920654 GET_RL_VLC(level, run, re, &s->gb, ff_mpeg1_rl_vlc,
169 TEX_VLC_BITS, 2, 0);
170
171
2/2
✓ Branch 12 → 13 taken 2841520 times.
✓ Branch 12 → 18 taken 79134 times.
2920654 if (level != 0) {
172 2841520 i += run;
173
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 2841520 times.
2841520 if (i > MAX_INDEX)
174 ✗ break;
175 2841520 j = scantable[i];
176 2841520 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
177 2841520 level = (level - 1) | 1;
178 2841520 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
179 2841520 SHOW_SBITS(re, &s->gb, 1);
180 2841520 SKIP_BITS(re, &s->gb, 1);
181 } else {
182 /* escape */
183 79134 run = SHOW_UBITS(re, &s->gb, 6) + 1;
184 79134 LAST_SKIP_BITS(re, &s->gb, 6);
185 79134 UPDATE_CACHE(re, &s->gb);
186 79134 level = SHOW_SBITS(re, &s->gb, 8);
187 79134 SKIP_BITS(re, &s->gb, 8);
188
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 23 taken 79134 times.
79134 if (level == -128) {
189 ✗ level = SHOW_UBITS(re, &s->gb, 8) - 256;
190 ✗ SKIP_BITS(re, &s->gb, 8);
191
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 26 taken 79134 times.
79134 } else if (level == 0) {
192 ✗ level = SHOW_UBITS(re, &s->gb, 8);
193 ✗ SKIP_BITS(re, &s->gb, 8);
194 }
195 79134 i += run;
196
1/2
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 28 taken 79134 times.
79134 if (i > MAX_INDEX)
197 ✗ break;
198 79134 j = scantable[i];
199
2/2
✓ Branch 28 → 29 taken 38931 times.
✓ Branch 28 → 30 taken 40203 times.
79134 if (level < 0) {
200 38931 level = -level;
201 38931 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
202 38931 level = (level - 1) | 1;
203 38931 level = -level;
204 } else {
205 40203 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
206 40203 level = (level - 1) | 1;
207 }
208 }
209
210 2920654 block[j] = level;
211
2/2
✓ Branch 31 → 32 taken 368773 times.
✓ Branch 31 → 33 taken 2551881 times.
2920654 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
212 368773 break;
213 2551881 UPDATE_CACHE(re, &s->gb);
214 }
215 403182 end:
216 403182 LAST_SKIP_BITS(re, &s->gb, 2);
217 403182 CLOSE_READER(re, &s->gb);
218 }
219
220
1/2
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 37 taken 403182 times.
403182 check_scantable_index(s, i);
221
222 403182 s->c.block_last_index[n] = i;
223 403182 return 0;
224 }
225
226 5494255 static inline int mpeg2_decode_block_non_intra(Mpeg12SliceContext *const s,
227 int16_t *block, int n)
228 {
229 int level, i, j, run;
230 5494255 const uint8_t *const scantable = s->c.intra_scantable.permutated;
231 const uint16_t *quant_matrix;
232 5494255 const int qscale = s->c.qscale;
233 int mismatch;
234
235 5494255 mismatch = 1;
236
237 {
238 5494255 OPEN_READER(re, &s->gb);
239 5494255 i = -1;
240
2/2
✓ Branch 2 → 3 taken 4142457 times.
✓ Branch 2 → 4 taken 1351798 times.
5494255 if (n < 4)
241 4142457 quant_matrix = s->c.inter_matrix;
242 else
243 1351798 quant_matrix = s->c.chroma_inter_matrix;
244
245 // Special case for first coefficient, no need to add second VLC table.
246 5494255 UPDATE_CACHE(re, &s->gb);
247
2/2
✓ Branch 5 → 6 taken 3459201 times.
✓ Branch 5 → 7 taken 2035054 times.
5494255 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
248 2035054 level = (3 * qscale * quant_matrix[0]) >> 5;
249
2/2
✓ Branch 7 → 8 taken 1299324 times.
✓ Branch 7 → 9 taken 735730 times.
2035054 if (GET_CACHE(re, &s->gb) & 0x40000000)
250 1299324 level = -level;
251 2035054 block[0] = level;
252 2035054 mismatch ^= level;
253 2035054 i++;
254 2035054 SKIP_BITS(re, &s->gb, 2);
255
2/2
✓ Branch 9 → 6 taken 1049837 times.
✓ Branch 9 → 10 taken 985217 times.
2035054 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
256 985217 goto end;
257 }
258
259 /* now quantify & encode AC coefficients */
260 for (;;) {
261
2/2
✓ Branch 12 → 13 taken 1687536 times.
✓ Branch 12 → 15 taken 17171984 times.
18859520 GET_RL_VLC(level, run, re, &s->gb, ff_mpeg1_rl_vlc,
262 TEX_VLC_BITS, 2, 0);
263
264
2/2
✓ Branch 15 → 16 taken 18586167 times.
✓ Branch 15 → 21 taken 273353 times.
18859520 if (level != 0) {
265 18586167 i += run;
266
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 18586167 times.
18586167 if (i > MAX_INDEX)
267 ✗ break;
268 18586167 j = scantable[i];
269 18586167 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
270 18586167 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
271 18586167 SHOW_SBITS(re, &s->gb, 1);
272 18586167 SKIP_BITS(re, &s->gb, 1);
273 } else {
274 /* escape */
275 273353 run = SHOW_UBITS(re, &s->gb, 6) + 1;
276 273353 LAST_SKIP_BITS(re, &s->gb, 6);
277 273353 UPDATE_CACHE(re, &s->gb);
278 273353 level = SHOW_SBITS(re, &s->gb, 12);
279 273353 SKIP_BITS(re, &s->gb, 12);
280
281 273353 i += run;
282
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 273353 times.
273353 if (i > MAX_INDEX)
283 ✗ break;
284 273353 j = scantable[i];
285
2/2
✓ Branch 25 → 26 taken 131929 times.
✓ Branch 25 → 27 taken 141424 times.
273353 if (level < 0) {
286 131929 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
287 131929 level = -level;
288 } else {
289 141424 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
290 }
291 }
292
293 18859520 mismatch ^= level;
294 18859520 block[j] = level;
295
2/2
✓ Branch 28 → 29 taken 4509038 times.
✓ Branch 28 → 30 taken 14350482 times.
18859520 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
296 4509038 break;
297 14350482 UPDATE_CACHE(re, &s->gb);
298 }
299 5494255 end:
300 5494255 LAST_SKIP_BITS(re, &s->gb, 2);
301 5494255 CLOSE_READER(re, &s->gb);
302 }
303 5494255 block[63] ^= (mismatch & 1);
304
305
1/2
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 34 taken 5494255 times.
5494255 check_scantable_index(s, i);
306
307 5494255 s->c.block_last_index[n] = i;
308 5494255 return 0;
309 }
310
311 1982606 static inline int mpeg2_decode_block_intra(Mpeg12SliceContext *const s,
312 int16_t *block, int n)
313 {
314 int level, dc, diff, i, j, run;
315 int component;
316 const RL_VLC_ELEM *rl_vlc;
317 1982606 const uint8_t *const scantable = s->c.intra_scantable.permutated;
318 const uint16_t *quant_matrix;
319 1982606 const int qscale = s->c.qscale;
320 int mismatch;
321
322 /* DC coefficient */
323
2/2
✓ Branch 2 → 3 taken 1249566 times.
✓ Branch 2 → 4 taken 733040 times.
1982606 if (n < 4) {
324 1249566 quant_matrix = s->c.intra_matrix;
325 1249566 component = 0;
326 } else {
327 733040 quant_matrix = s->c.chroma_intra_matrix;
328 733040 component = (n & 1) + 1;
329 }
330 1982606 diff = decode_dc(&s->gb, component);
331 1982606 dc = s->last_dc[component];
332 1982606 dc += diff;
333 1982606 s->last_dc[component] = dc;
334 1982606 block[0] = dc * (1 << (3 - s->c.intra_dc_precision));
335 ff_tlog(s->c.avctx, "dc=%d\n", block[0]);
336 1982606 mismatch = block[0] ^ 1;
337 1982606 i = 0;
338
2/2
✓ Branch 6 → 7 taken 1400114 times.
✓ Branch 6 → 8 taken 582492 times.
1982606 if (s->c.intra_vlc_format)
339 1400114 rl_vlc = ff_mpeg2_rl_vlc;
340 else
341 582492 rl_vlc = ff_mpeg1_rl_vlc;
342
343 {
344 1982606 OPEN_READER(re, &s->gb);
345 /* now quantify & encode AC coefficients */
346 for (;;) {
347 14678712 UPDATE_CACHE(re, &s->gb);
348
2/2
✓ Branch 11 → 12 taken 768618 times.
✓ Branch 11 → 14 taken 13910094 times.
14678712 GET_RL_VLC(level, run, re, &s->gb, rl_vlc,
349 TEX_VLC_BITS, 2, 0);
350
351
2/2
✓ Branch 14 → 15 taken 1982595 times.
✓ Branch 14 → 16 taken 12696117 times.
14678712 if (level == 127) {
352 1982595 break;
353
2/2
✓ Branch 16 → 17 taken 12553923 times.
✓ Branch 16 → 22 taken 142194 times.
12696117 } else if (level != 0) {
354 12553923 i += run;
355
2/2
✓ Branch 17 → 18 taken 11 times.
✓ Branch 17 → 19 taken 12553912 times.
12553923 if (i > MAX_INDEX)
356 11 break;
357 12553912 j = scantable[i];
358 12553912 level = (level * qscale * quant_matrix[j]) >> 4;
359 12553912 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
360 12553912 SHOW_SBITS(re, &s->gb, 1);
361 12553912 LAST_SKIP_BITS(re, &s->gb, 1);
362 } else {
363 /* escape */
364 142194 run = SHOW_UBITS(re, &s->gb, 6) + 1;
365 142194 SKIP_BITS(re, &s->gb, 6);
366 142194 level = SHOW_SBITS(re, &s->gb, 12);
367 142194 LAST_SKIP_BITS(re, &s->gb, 12);
368 142194 i += run;
369
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 142194 times.
142194 if (i > MAX_INDEX)
370 ✗ break;
371 142194 j = scantable[i];
372
2/2
✓ Branch 26 → 27 taken 64511 times.
✓ Branch 26 → 28 taken 77683 times.
142194 if (level < 0) {
373 64511 level = (-level * qscale * quant_matrix[j]) >> 4;
374 64511 level = -level;
375 } else {
376 77683 level = (level * qscale * quant_matrix[j]) >> 4;
377 }
378 }
379
380 12696106 mismatch ^= level;
381 12696106 block[j] = level;
382 }
383 1982606 CLOSE_READER(re, &s->gb);
384 }
385 1982606 block[63] ^= mismatch & 1;
386
387
2/2
✓ Branch 30 → 31 taken 11 times.
✓ Branch 30 → 33 taken 1982595 times.
1982606 check_scantable_index(s, i);
388
389 1982595 return 0;
390 }
391
392 46398 static inline int get_dmv(Mpeg12SliceContext *const s)
393 {
394
2/2
✓ Branch 3 → 4 taken 43253 times.
✓ Branch 3 → 6 taken 3145 times.
46398 if (get_bits1(&s->gb))
395 43253 return 1 - (get_bits1(&s->gb) << 1);
396 else
397 3145 return 0;
398 }
399
400 /* motion type (for MPEG-2) */
401 #define MT_FIELD 1
402 #define MT_FRAME 2
403 #define MT_16X8 2
404 #define MT_DMV 3
405
406 2915126 static int mpeg_decode_mb(Mpeg12SliceContext *const s, int *mb_skip_run)
407 {
408 int i, j, k, cbp, val, mb_type, motion_type;
409 2915126 const int mb_block_count = 4 + (1 << s->c.chroma_format);
410 int ret;
411
412 ff_tlog(s->c.avctx, "decode_mb: x=%d y=%d\n", s->c.mb_x, s->c.mb_y);
413
414 av_assert2(s->c.mb_skipped == 0);
415
416
2/2
✓ Branch 2 → 3 taken 266145 times.
✓ Branch 2 → 14 taken 2648981 times.
2915126 if ((*mb_skip_run)-- != 0) {
417
2/2
✓ Branch 3 → 4 taken 80299 times.
✓ Branch 3 → 5 taken 185846 times.
266145 if (s->c.pict_type == AV_PICTURE_TYPE_P) {
418 80299 s->c.mb_skipped = 1;
419 80299 s->c.cur_pic.mb_type[s->c.mb_x + s->c.mb_y * s->c.mb_stride] =
420 MB_TYPE_SKIP | MB_TYPE_FORWARD_MV | MB_TYPE_16x16;
421 } else {
422 int mb_type;
423
424
1/2
✓ Branch 5 → 6 taken 185846 times.
✗ Branch 5 → 7 not taken.
185846 if (s->c.mb_x)
425 185846 mb_type = s->c.cur_pic.mb_type[s->c.mb_x + s->c.mb_y * s->c.mb_stride - 1];
426 else
427 // FIXME not sure if this is allowed in MPEG at all
428 ✗ mb_type = s->c.cur_pic.mb_type[s->c.mb_width + (s->c.mb_y - 1) * s->c.mb_stride - 1];
429
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 11 taken 185846 times.
185846 if (IS_INTRA(mb_type)) {
430 ✗ av_log(s->c.avctx, AV_LOG_ERROR, "skip with previntra\n");
431 ✗ return AVERROR_INVALIDDATA;
432 }
433 185846 s->c.cur_pic.mb_type[s->c.mb_x + s->c.mb_y * s->c.mb_stride] =
434 185846 mb_type | MB_TYPE_SKIP;
435
436
2/2
✓ Branch 11 → 12 taken 138129 times.
✓ Branch 11 → 13 taken 47717 times.
185846 if ((s->c.mv[0][0][0] | s->c.mv[0][0][1] | s->c.mv[1][0][0] | s->c.mv[1][0][1]) == 0)
437 138129 s->c.mb_skipped = 1;
438 }
439
440 266145 return 0;
441 }
442
443
3/3
✓ Branch 14 → 15 taken 249588 times.
✓ Branch 14 → 24 taken 1438999 times.
✓ Branch 14 → 29 taken 960394 times.
2648981 switch (s->c.pict_type) {
444 249588 default:
445 case AV_PICTURE_TYPE_I:
446
2/2
✓ Branch 16 → 17 taken 20793 times.
✓ Branch 16 → 22 taken 228795 times.
249588 if (get_bits1(&s->gb) == 0) {
447
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 21 taken 20793 times.
20793 if (get_bits1(&s->gb) == 0) {
448 ✗ av_log(s->c.avctx, AV_LOG_ERROR,
449 "Invalid mb type in I-frame at %d %d\n",
450 s->c.mb_x, s->c.mb_y);
451 ✗ return AVERROR_INVALIDDATA;
452 }
453 20793 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
454 } else {
455 228795 mb_type = MB_TYPE_INTRA;
456 }
457 249588 break;
458 1438999 case AV_PICTURE_TYPE_P:
459 1438999 mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc, MB_PTYPE_VLC_BITS, 1);
460
1/2
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 28 taken 1438999 times.
1438999 if (mb_type < 0) {
461 ✗ av_log(s->c.avctx, AV_LOG_ERROR,
462 "Invalid mb type in P-frame at %d %d\n", s->c.mb_x, s->c.mb_y);
463 ✗ return AVERROR_INVALIDDATA;
464 }
465 1438999 break;
466 960394 case AV_PICTURE_TYPE_B:
467 960394 mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc, MB_BTYPE_VLC_BITS, 1);
468
1/2
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 33 taken 960394 times.
960394 if (mb_type < 0) {
469 ✗ av_log(s->c.avctx, AV_LOG_ERROR,
470 "Invalid mb type in B-frame at %d %d\n", s->c.mb_x, s->c.mb_y);
471 ✗ return AVERROR_INVALIDDATA;
472 }
473 960394 break;
474 }
475 ff_tlog(s->c.avctx, "mb_type=%x\n", mb_type);
476 // motion_type = 0; /* avoid warning */
477
2/2
✓ Branch 34 → 35 taken 333401 times.
✓ Branch 34 → 66 taken 2315580 times.
2648981 if (IS_INTRA(mb_type)) {
478 333401 s->c.bdsp.clear_blocks(s->block[0]);
479
480
2/2
✓ Branch 36 → 37 taken 54134 times.
✓ Branch 36 → 38 taken 279267 times.
333401 if (!s->c.chroma_y_shift)
481 54134 s->c.bdsp.clear_blocks(s->block[6]);
482
483 /* compute DCT type */
484 // FIXME: add an interlaced_dct coded var?
485
2/2
✓ Branch 38 → 39 taken 271878 times.
✓ Branch 38 → 42 taken 61523 times.
333401 if (s->c.picture_structure == PICT_FRAME &&
486
2/2
✓ Branch 39 → 40 taken 128660 times.
✓ Branch 39 → 42 taken 143218 times.
271878 !s->c.frame_pred_frame_dct)
487 128660 s->c.interlaced_dct = get_bits1(&s->gb);
488
489
2/2
✓ Branch 42 → 43 taken 29497 times.
✓ Branch 42 → 45 taken 303904 times.
333401 if (IS_QUANT(mb_type))
490 29497 s->c.qscale = mpeg_get_qscale(&s->gb, s->c.q_scale_type);
491
492
2/2
✓ Branch 45 → 46 taken 1356 times.
✓ Branch 45 → 51 taken 332045 times.
333401 if (s->c.concealment_motion_vectors) {
493 /* just parse them */
494
2/2
✓ Branch 46 → 47 taken 1350 times.
✓ Branch 46 → 48 taken 6 times.
1356 if (s->c.picture_structure != PICT_FRAME)
495 1350 skip_bits1(&s->gb); /* field select */
496
497 1356 s->c.mv[0][0][0] =
498 1356 s->c.last_mv[0][0][0] =
499 1356 s->c.last_mv[0][1][0] = mpeg_decode_motion(s, s->c.mpeg_f_code[0][0],
500 s->c.last_mv[0][0][0]);
501 1356 s->c.mv[0][0][1] =
502 1356 s->c.last_mv[0][0][1] =
503 1356 s->c.last_mv[0][1][1] = mpeg_decode_motion(s, s->c.mpeg_f_code[0][1],
504 s->c.last_mv[0][0][1]);
505
506 1356 check_marker(s->c.avctx, &s->gb, "after concealment_motion_vectors");
507 } else {
508 /* reset mv prediction */
509 332045 memset(s->c.last_mv, 0, sizeof(s->c.last_mv));
510 }
511 333401 s->c.mb_intra = 1;
512
513
2/2
✓ Branch 52 → 53 taken 312397 times.
✓ Branch 52 → 59 taken 21004 times.
333401 if (s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO) {
514
2/2
✓ Branch 58 → 54 taken 1982606 times.
✓ Branch 58 → 187 taken 312386 times.
2294992 for (i = 0; i < mb_block_count; i++)
515
2/2
✓ Branch 55 → 56 taken 11 times.
✓ Branch 55 → 57 taken 1982595 times.
1982606 if ((ret = mpeg2_decode_block_intra(s, s->block[i], i)) < 0)
516 11 return ret;
517 } else {
518
2/2
✓ Branch 65 → 60 taken 126024 times.
✓ Branch 65 → 187 taken 21004 times.
147028 for (i = 0; i < 6; i++) {
519 126024 ret = ff_mpeg1_decode_block_intra(&s->gb,
520 126024 s->c.intra_matrix,
521 126024 s->c.intra_scantable.permutated,
522 126024 s->last_dc, s->block[i],
523 i, s->c.qscale);
524
1/2
✗ Branch 61 → 62 not taken.
✓ Branch 61 → 64 taken 126024 times.
126024 if (ret < 0) {
525 ✗ av_log(s->c.avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n",
526 s->c.mb_x, s->c.mb_y);
527 ✗ return ret;
528 }
529 }
530 }
531 } else {
532
2/2
✓ Branch 66 → 67 taken 243867 times.
✓ Branch 66 → 78 taken 2071713 times.
2315580 if (mb_type & MB_TYPE_ZERO_MV) {
533 av_assert2(mb_type & MB_TYPE_CBP);
534
535 243867 s->c.mv_dir = MV_DIR_FORWARD;
536
2/2
✓ Branch 67 → 68 taken 111841 times.
✓ Branch 67 → 73 taken 132026 times.
243867 if (s->c.picture_structure == PICT_FRAME) {
537
1/2
✓ Branch 68 → 69 taken 111841 times.
✗ Branch 68 → 72 not taken.
111841 if (s->c.picture_structure == PICT_FRAME
538
2/2
✓ Branch 69 → 70 taken 77256 times.
✓ Branch 69 → 72 taken 34585 times.
111841 && !s->c.frame_pred_frame_dct)
539 77256 s->c.interlaced_dct = get_bits1(&s->gb);
540 111841 s->c.mv_type = MV_TYPE_16X16;
541 } else {
542 132026 s->c.mv_type = MV_TYPE_FIELD;
543 132026 mb_type |= MB_TYPE_INTERLACED;
544 132026 s->c.field_select[0][0] = s->c.picture_structure - 1;
545 }
546
547
2/2
✓ Branch 74 → 75 taken 63703 times.
✓ Branch 74 → 77 taken 180164 times.
243867 if (IS_QUANT(mb_type))
548 63703 s->c.qscale = mpeg_get_qscale(&s->gb, s->c.q_scale_type);
549
550 243867 s->c.last_mv[0][0][0] = 0;
551 243867 s->c.last_mv[0][0][1] = 0;
552 243867 s->c.last_mv[0][1][0] = 0;
553 243867 s->c.last_mv[0][1][1] = 0;
554 243867 s->c.mv[0][0][0] = 0;
555 243867 s->c.mv[0][0][1] = 0;
556 } else {
557 av_assert2(mb_type & MB_TYPE_BIDIR_MV);
558 // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
559 /* get additional motion vector type */
560
4/4
✓ Branch 78 → 79 taken 1538791 times.
✓ Branch 78 → 81 taken 532922 times.
✓ Branch 79 → 80 taken 700841 times.
✓ Branch 79 → 81 taken 837950 times.
2071713 if (s->c.picture_structure == PICT_FRAME && s->c.frame_pred_frame_dct) {
561 700841 motion_type = MT_FRAME;
562 } else {
563 1370872 motion_type = get_bits(&s->gb, 2);
564
4/4
✓ Branch 82 → 83 taken 837950 times.
✓ Branch 82 → 86 taken 532922 times.
✓ Branch 83 → 84 taken 601670 times.
✓ Branch 83 → 86 taken 236280 times.
1370872 if (s->c.picture_structure == PICT_FRAME && HAS_CBP(mb_type))
565 601670 s->c.interlaced_dct = get_bits1(&s->gb);
566 }
567
568
2/2
✓ Branch 86 → 87 taken 212446 times.
✓ Branch 86 → 89 taken 1859267 times.
2071713 if (IS_QUANT(mb_type))
569 212446 s->c.qscale = mpeg_get_qscale(&s->gb, s->c.q_scale_type);
570
571 /* motion vectors */
572 2071713 s->c.mv_dir = MB_TYPE_MV_2_MV_DIR(mb_type);
573 ff_tlog(s->c.avctx, "motion_type=%d\n", motion_type);
574
3/4
✓ Branch 89 → 90 taken 1751229 times.
✓ Branch 89 → 112 taken 297285 times.
✓ Branch 89 → 136 taken 23199 times.
✗ Branch 89 → 156 not taken.
2071713 switch (motion_type) {
575 1751229 case MT_FRAME: /* or MT_16X8 */
576
2/2
✓ Branch 90 → 91 taken 1440900 times.
✓ Branch 90 → 99 taken 310329 times.
1751229 if (s->c.picture_structure == PICT_FRAME) {
577 1440900 mb_type |= MB_TYPE_16x16;
578 1440900 s->c.mv_type = MV_TYPE_16X16;
579
2/2
✓ Branch 98 → 92 taken 2881800 times.
✓ Branch 98 → 111 taken 1440900 times.
4322700 for (i = 0; i < 2; i++) {
580
2/2
✓ Branch 92 → 93 taken 1878164 times.
✓ Branch 92 → 97 taken 1003636 times.
2881800 if (HAS_MV(mb_type, i)) {
581 /* MT_FRAME */
582 1878164 s->c.mv[i][0][0] =
583 1878164 s->c.last_mv[i][0][0] =
584 1878164 s->c.last_mv[i][1][0] =
585 1878164 mpeg_decode_motion(s, s->c.mpeg_f_code[i][0],
586 s->c.last_mv[i][0][0]);
587 1878164 s->c.mv[i][0][1] =
588 1878164 s->c.last_mv[i][0][1] =
589 1878164 s->c.last_mv[i][1][1] =
590 1878164 mpeg_decode_motion(s, s->c.mpeg_f_code[i][1],
591 s->c.last_mv[i][0][1]);
592 /* full_pel: only for MPEG-1 */
593
1/2
✗ Branch 95 → 96 not taken.
✓ Branch 95 → 97 taken 1878164 times.
1878164 if (s->c.full_pel[i]) {
594 ✗ s->c.mv[i][0][0] *= 2;
595 ✗ s->c.mv[i][0][1] *= 2;
596 }
597 }
598 }
599 } else {
600 310329 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
601 310329 s->c.mv_type = MV_TYPE_16X8;
602
2/2
✓ Branch 110 → 100 taken 620658 times.
✓ Branch 110 → 111 taken 310329 times.
930987 for (i = 0; i < 2; i++) {
603
2/2
✓ Branch 100 → 101 taken 311691 times.
✓ Branch 100 → 109 taken 308967 times.
620658 if (HAS_MV(mb_type, i)) {
604 /* MT_16X8 */
605
2/2
✓ Branch 108 → 102 taken 623382 times.
✓ Branch 108 → 109 taken 311691 times.
935073 for (j = 0; j < 2; j++) {
606 623382 s->c.field_select[i][j] = get_bits1(&s->gb);
607
2/2
✓ Branch 106 → 104 taken 1246764 times.
✓ Branch 106 → 107 taken 623382 times.
1870146 for (k = 0; k < 2; k++) {
608 1246764 val = mpeg_decode_motion(s, s->c.mpeg_f_code[i][k],
609 s->c.last_mv[i][j][k]);
610 1246764 s->c.last_mv[i][j][k] = val;
611 1246764 s->c.mv[i][j][k] = val;
612 }
613 }
614 }
615 }
616 }
617 1751229 break;
618 297285 case MT_FIELD:
619 297285 s->c.mv_type = MV_TYPE_FIELD;
620
2/2
✓ Branch 112 → 113 taken 97891 times.
✓ Branch 112 → 123 taken 199394 times.
297285 if (s->c.picture_structure == PICT_FRAME) {
621 97891 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
622
2/2
✓ Branch 122 → 114 taken 195782 times.
✓ Branch 122 → 135 taken 97891 times.
293673 for (i = 0; i < 2; i++) {
623
2/2
✓ Branch 114 → 115 taken 120540 times.
✓ Branch 114 → 121 taken 75242 times.
195782 if (HAS_MV(mb_type, i)) {
624
2/2
✓ Branch 120 → 116 taken 241080 times.
✓ Branch 120 → 121 taken 120540 times.
361620 for (j = 0; j < 2; j++) {
625 241080 s->c.field_select[i][j] = get_bits1(&s->gb);
626 241080 val = mpeg_decode_motion(s, s->c.mpeg_f_code[i][0],
627 s->c.last_mv[i][j][0]);
628 241080 s->c.last_mv[i][j][0] = val;
629 241080 s->c.mv[i][j][0] = val;
630 ff_tlog(s->c.avctx, "fmx=%d\n", val);
631 241080 val = mpeg_decode_motion(s, s->c.mpeg_f_code[i][1],
632 241080 s->c.last_mv[i][j][1] >> 1);
633 241080 s->c.last_mv[i][j][1] = 2 * val;
634 241080 s->c.mv[i][j][1] = val;
635 ff_tlog(s->c.avctx, "fmy=%d\n", val);
636 }
637 }
638 }
639 } else {
640
1/2
✗ Branch 123 → 124 not taken.
✓ Branch 123 → 126 taken 199394 times.
199394 av_assert0(!s->c.progressive_sequence);
641 199394 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
642
2/2
✓ Branch 134 → 127 taken 398788 times.
✓ Branch 134 → 135 taken 199394 times.
598182 for (i = 0; i < 2; i++) {
643
2/2
✓ Branch 127 → 128 taken 204068 times.
✓ Branch 127 → 133 taken 194720 times.
398788 if (HAS_MV(mb_type, i)) {
644 204068 s->c.field_select[i][0] = get_bits1(&s->gb);
645
2/2
✓ Branch 132 → 130 taken 408136 times.
✓ Branch 132 → 133 taken 204068 times.
612204 for (k = 0; k < 2; k++) {
646 408136 val = mpeg_decode_motion(s, s->c.mpeg_f_code[i][k],
647 s->c.last_mv[i][0][k]);
648 408136 s->c.last_mv[i][0][k] = val;
649 408136 s->c.last_mv[i][1][k] = val;
650 408136 s->c.mv[i][0][k] = val;
651 }
652 }
653 }
654 }
655 297285 break;
656 23199 case MT_DMV:
657
1/2
✗ Branch 136 → 137 not taken.
✓ Branch 136 → 139 taken 23199 times.
23199 if (s->c.progressive_sequence){
658 ✗ av_log(s->c.avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
659 ✗ return AVERROR_INVALIDDATA;
660 }
661 23199 s->c.mv_type = MV_TYPE_DMV;
662
2/2
✓ Branch 154 → 140 taken 46398 times.
✓ Branch 154 → 155 taken 23199 times.
69597 for (i = 0; i < 2; i++) {
663
2/2
✓ Branch 140 → 141 taken 23199 times.
✓ Branch 140 → 153 taken 23199 times.
46398 if (HAS_MV(mb_type, i)) {
664 int dmx, dmy, mx, my, m;
665 23199 const int my_shift = s->c.picture_structure == PICT_FRAME;
666
667 23199 mx = mpeg_decode_motion(s, s->c.mpeg_f_code[i][0],
668 s->c.last_mv[i][0][0]);
669 23199 s->c.last_mv[i][0][0] = mx;
670 23199 s->c.last_mv[i][1][0] = mx;
671 23199 dmx = get_dmv(s);
672 23199 my = mpeg_decode_motion(s, s->c.mpeg_f_code[i][1],
673 23199 s->c.last_mv[i][0][1] >> my_shift);
674 23199 dmy = get_dmv(s);
675
676
677 23199 s->c.last_mv[i][0][1] = my * (1 << my_shift);
678 23199 s->c.last_mv[i][1][1] = my * (1 << my_shift);
679
680 23199 s->c.mv[i][0][0] = mx;
681 23199 s->c.mv[i][0][1] = my;
682 23199 s->c.mv[i][1][0] = mx; // not used
683 23199 s->c.mv[i][1][1] = my; // not used
684
685
1/2
✗ Branch 145 → 146 not taken.
✓ Branch 145 → 150 taken 23199 times.
23199 if (s->c.picture_structure == PICT_FRAME) {
686 ✗ mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
687
688 // m = 1 + 2 * s->c.top_field_first;
689
0/2
✗ Branch 146 → 147 not taken.
✗ Branch 146 → 148 not taken.
✗ m = s->c.top_field_first ? 1 : 3;
690
691 /* top -> top pred */
692 ✗ s->c.mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
693 ✗ s->c.mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
694 ✗ m = 4 - m;
695 ✗ s->c.mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
696 ✗ s->c.mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
697 } else {
698 23199 mb_type |= MB_TYPE_16x16;
699
700 23199 s->c.mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
701 23199 s->c.mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
702
2/2
✓ Branch 150 → 151 taken 19870 times.
✓ Branch 150 → 152 taken 3329 times.
23199 if (s->c.picture_structure == PICT_TOP_FIELD)
703 19870 s->c.mv[i][2][1]--;
704 else
705 3329 s->c.mv[i][2][1]++;
706 }
707 }
708 }
709 23199 break;
710 ✗ default:
711 ✗ av_log(s->c.avctx, AV_LOG_ERROR,
712 "00 motion_type at %d %d\n", s->c.mb_x, s->c.mb_y);
713 ✗ return AVERROR_INVALIDDATA;
714 }
715 }
716
717 2315580 s->c.mb_intra = 0;
718 2315580 s->last_dc[0] = s->last_dc[1] = s->last_dc[2] = 128 << s->c.intra_dc_precision;
719
2/2
✓ Branch 158 → 159 taken 1842050 times.
✓ Branch 158 → 184 taken 473530 times.
2315580 if (HAS_CBP(mb_type)) {
720 1842050 s->c.bdsp.clear_blocks(s->block[0]);
721
722 1842050 cbp = get_vlc2(&s->gb, ff_mb_pat_vlc, MB_PAT_VLC_BITS, 1);
723
2/2
✓ Branch 161 → 162 taken 43711 times.
✓ Branch 161 → 164 taken 1798339 times.
1842050 if (mb_block_count > 6) {
724 43711 cbp *= 1 << mb_block_count - 6;
725 43711 cbp |= get_bits(&s->gb, mb_block_count - 6);
726 43711 s->c.bdsp.clear_blocks(s->block[6]);
727 }
728
1/2
✗ Branch 164 → 165 not taken.
✓ Branch 164 → 167 taken 1842050 times.
1842050 if (cbp <= 0) {
729 ✗ av_log(s->c.avctx, AV_LOG_ERROR,
730 "invalid cbp %d at %d %d\n", cbp, s->c.mb_x, s->c.mb_y);
731 ✗ return AVERROR_INVALIDDATA;
732 }
733
734
2/2
✓ Branch 167 → 168 taken 1730314 times.
✓ Branch 167 → 176 taken 111736 times.
1842050 if (s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO) {
735 1730314 cbp <<= 12 - mb_block_count;
736
737
2/2
✓ Branch 175 → 169 taken 10469306 times.
✓ Branch 175 → 187 taken 1730314 times.
12199620 for (i = 0; i < mb_block_count; i++) {
738
2/2
✓ Branch 169 → 170 taken 5494255 times.
✓ Branch 169 → 173 taken 4975051 times.
10469306 if (cbp & (1 << 11)) {
739
1/2
✗ Branch 171 → 172 not taken.
✓ Branch 171 → 174 taken 5494255 times.
5494255 if ((ret = mpeg2_decode_block_non_intra(s, s->block[i], i)) < 0)
740 ✗ return ret;
741 } else {
742 4975051 s->c.block_last_index[i] = -1;
743 }
744 10469306 cbp += cbp;
745 }
746 } else {
747
2/2
✓ Branch 183 → 177 taken 670416 times.
✓ Branch 183 → 187 taken 111736 times.
782152 for (i = 0; i < 6; i++) {
748
2/2
✓ Branch 177 → 178 taken 403182 times.
✓ Branch 177 → 181 taken 267234 times.
670416 if (cbp & 32) {
749
1/2
✗ Branch 179 → 180 not taken.
✓ Branch 179 → 182 taken 403182 times.
403182 if ((ret = mpeg1_decode_block_inter(s, s->block[i], i)) < 0)
750 ✗ return ret;
751 } else {
752 267234 s->c.block_last_index[i] = -1;
753 }
754 670416 cbp += cbp;
755 }
756 }
757 } else {
758
2/2
✓ Branch 186 → 185 taken 5682360 times.
✓ Branch 186 → 187 taken 473530 times.
6155890 for (i = 0; i < 12; i++)
759 5682360 s->c.block_last_index[i] = -1;
760 }
761 }
762
763 2648970 s->c.cur_pic.mb_type[s->c.mb_x + s->c.mb_y * s->c.mb_stride] = mb_type;
764
765 2648970 return 0;
766 }
767
768 345 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
769 {
770 345 Mpeg1Context *s = avctx->priv_data;
771 345 MPVContext *const s2 = &s->slice.c;
772 int ret;
773
774 345 s2->slice_ctx_size = sizeof(s->slice);
775 345 s2->out_format = FMT_MPEG1;
776
777
2/2
✓ Branch 2 → 3 taken 343 times.
✓ Branch 2 → 5 taken 2 times.
345 if ( avctx->codec_tag != AV_RL32("VCR2")
778
1/2
✓ Branch 3 → 4 taken 343 times.
✗ Branch 3 → 5 not taken.
343 && avctx->codec_tag != AV_RL32("BW10"))
779 343 avctx->coded_width = avctx->coded_height = 0; // do not trust dimensions from input
780 345 ret = ff_mpv_decode_init(s2, avctx);
781
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 345 times.
345 if (ret < 0)
782 ✗ return ret;
783
784 345 ff_mpeg12_init_vlcs();
785
786 345 s2->chroma_format = CHROMA_420;
787 345 avctx->color_range = AVCOL_RANGE_MPEG;
788 345 return 0;
789 }
790
791 static const enum AVPixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
792 #if CONFIG_MPEG1_NVDEC_HWACCEL
793 AV_PIX_FMT_CUDA,
794 #endif
795 #if CONFIG_MPEG1_NVDEC_CUARRAY_HWACCEL
796 AV_PIX_FMT_CUARRAY,
797 #endif
798 #if CONFIG_MPEG1_VDPAU_HWACCEL
799 AV_PIX_FMT_VDPAU,
800 #endif
801 AV_PIX_FMT_YUV420P,
802 AV_PIX_FMT_NONE
803 };
804
805 static const enum AVPixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
806 #if CONFIG_MPEG2_NVDEC_HWACCEL
807 AV_PIX_FMT_CUDA,
808 #endif
809 #if CONFIG_MPEG2_NVDEC_CUARRAY_HWACCEL
810 AV_PIX_FMT_CUARRAY,
811 #endif
812 #if CONFIG_MPEG2_VDPAU_HWACCEL
813 AV_PIX_FMT_VDPAU,
814 #endif
815 #if CONFIG_MPEG2_DXVA2_HWACCEL
816 AV_PIX_FMT_DXVA2_VLD,
817 #endif
818 #if CONFIG_MPEG2_D3D11VA_HWACCEL
819 AV_PIX_FMT_D3D11VA_VLD,
820 AV_PIX_FMT_D3D11,
821 #endif
822 #if CONFIG_MPEG2_D3D12VA_HWACCEL
823 AV_PIX_FMT_D3D12,
824 #endif
825 #if CONFIG_MPEG2_VAAPI_HWACCEL
826 AV_PIX_FMT_VAAPI,
827 #endif
828 #if CONFIG_MPEG2_VIDEOTOOLBOX_HWACCEL
829 AV_PIX_FMT_VIDEOTOOLBOX,
830 #endif
831 AV_PIX_FMT_YUV420P,
832 AV_PIX_FMT_NONE
833 };
834
835 static const enum AVPixelFormat mpeg12_pixfmt_list_422[] = {
836 AV_PIX_FMT_YUV422P,
837 AV_PIX_FMT_NONE
838 };
839
840 static const enum AVPixelFormat mpeg12_pixfmt_list_444[] = {
841 AV_PIX_FMT_YUV444P,
842 AV_PIX_FMT_NONE
843 };
844
845 301 static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
846 {
847 301 Mpeg1Context *s1 = avctx->priv_data;
848 301 MPVContext *const s = &s1->slice.c;
849 const enum AVPixelFormat *pix_fmts;
850
851 if (CONFIG_GRAY && (avctx->flags & AV_CODEC_FLAG_GRAY))
852 return AV_PIX_FMT_GRAY8;
853
854
2/2
✓ Branch 2 → 3 taken 240 times.
✓ Branch 2 → 6 taken 61 times.
301 if (s->chroma_format < CHROMA_422)
855 240 pix_fmts = avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO ?
856
2/2
✓ Branch 3 → 4 taken 25 times.
✓ Branch 3 → 5 taken 215 times.
240 mpeg1_hwaccel_pixfmt_list_420 :
857 mpeg2_hwaccel_pixfmt_list_420;
858
1/2
✓ Branch 6 → 7 taken 61 times.
✗ Branch 6 → 8 not taken.
61 else if (s->chroma_format == CHROMA_422)
859 61 pix_fmts = mpeg12_pixfmt_list_422;
860 else
861 ✗ pix_fmts = mpeg12_pixfmt_list_444;
862
863 301 return ff_get_format(avctx, pix_fmts);
864 }
865
866 /* Call this function when we know all parameters.
867 * It may be called in different places for MPEG-1 and MPEG-2. */
868 5371 static int mpeg_decode_postinit(AVCodecContext *avctx)
869 {
870 5371 Mpeg1Context *s1 = avctx->priv_data;
871 5371 MPVContext *const s = &s1->slice.c;
872 int ret;
873
874
2/2
✓ Branch 2 → 3 taken 494 times.
✓ Branch 2 → 4 taken 4877 times.
5371 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
875 // MPEG-1 aspect
876 494 AVRational aspect_inv = av_d2q(ff_mpeg1_aspect[s1->aspect_ratio_info], 255);
877 494 avctx->sample_aspect_ratio = (AVRational) { aspect_inv.den, aspect_inv.num };
878 } else { // MPEG-2
879 // MPEG-2 aspect
880
2/2
✓ Branch 4 → 5 taken 2811 times.
✓ Branch 4 → 14 taken 2066 times.
4877 if (s1->aspect_ratio_info > 1) {
881 AVRational dar =
882 2811 av_mul_q(av_div_q(ff_mpeg2_aspect[s1->aspect_ratio_info],
883 2811 (AVRational) { s1->pan_scan.width,
884 2811 s1->pan_scan.height }),
885 2811 (AVRational) { s->width, s->height });
886
887 /* We ignore the spec here and guess a bit as reality does not
888 * match the spec, see for example res_change_ffmpeg_aspect.ts
889 * and sequence-display-aspect.mpg.
890 * issue1613, 621, 562 */
891
5/6
✓ Branch 5 → 6 taken 591 times.
✓ Branch 5 → 11 taken 2220 times.
✓ Branch 6 → 7 taken 591 times.
✗ Branch 6 → 11 not taken.
✓ Branch 8 → 9 taken 31 times.
✓ Branch 8 → 12 taken 560 times.
3402 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
892
2/2
✓ Branch 10 → 11 taken 26 times.
✓ Branch 10 → 12 taken 5 times.
622 (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
893 31 av_cmp_q(dar, (AVRational) { 16, 9 }))) {
894 2246 s->avctx->sample_aspect_ratio =
895 2246 av_div_q(ff_mpeg2_aspect[s1->aspect_ratio_info],
896 2246 (AVRational) { s->width, s->height });
897 } else {
898 565 s->avctx->sample_aspect_ratio =
899 565 av_div_q(ff_mpeg2_aspect[s1->aspect_ratio_info],
900 565 (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
901 // issue1613 4/3 16/9 -> 16/9
902 // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
903 // widescreen-issue562.mpg 4/3 16/9 -> 16/9
904 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
905 ff_dlog(avctx, "aspect A %d/%d\n",
906 ff_mpeg2_aspect[s1->aspect_ratio_info].num,
907 ff_mpeg2_aspect[s1->aspect_ratio_info].den);
908 ff_dlog(avctx, "aspect B %d/%d\n", s->avctx->sample_aspect_ratio.num,
909 s->avctx->sample_aspect_ratio.den);
910 }
911 } else {
912 2066 s->avctx->sample_aspect_ratio =
913 2066 ff_mpeg2_aspect[s1->aspect_ratio_info];
914 }
915 } // MPEG-2
916
917
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 19 taken 5371 times.
5371 if (av_image_check_sar(s->width, s->height,
918 avctx->sample_aspect_ratio) < 0) {
919 ✗ av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
920 avctx->sample_aspect_ratio.num,
921 avctx->sample_aspect_ratio.den);
922 ✗ avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
923 }
924
925
2/2
✓ Branch 19 → 20 taken 5072 times.
✓ Branch 19 → 25 taken 299 times.
5371 if (!s->context_initialized ||
926
1/2
✓ Branch 20 → 21 taken 5072 times.
✗ Branch 20 → 25 not taken.
5072 avctx->coded_width != s->width ||
927
1/2
✓ Branch 21 → 22 taken 5072 times.
✗ Branch 21 → 25 not taken.
5072 avctx->coded_height != s->height ||
928
1/2
✓ Branch 22 → 23 taken 5072 times.
✗ Branch 22 → 25 not taken.
5072 s1->save_chroma_format != s->chroma_format ||
929
1/4
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 56 taken 5072 times.
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 56 not taken.
5072 (s1->save_progressive_seq != s->progressive_sequence && FFALIGN(s->height, 16) != FFALIGN(s->height, 32)) ||
930 0) {
931
1/2
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 299 times.
299 if (s->context_initialized)
932 ✗ ff_mpv_common_end(s);
933
934 299 ret = ff_set_dimensions(avctx, s->width, s->height);
935
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 299 times.
299 if (ret < 0)
936 ✗ return ret;
937
938
3/4
✓ Branch 30 → 31 taken 274 times.
✓ Branch 30 → 34 taken 25 times.
✓ Branch 31 → 32 taken 274 times.
✗ Branch 31 → 34 not taken.
299 if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && s1->bit_rate &&
939
2/2
✓ Branch 32 → 33 taken 107 times.
✓ Branch 32 → 34 taken 167 times.
274 (s1->bit_rate != 0x3FFFF*400)) {
940 107 avctx->rc_max_rate = s1->bit_rate;
941
3/4
✓ Branch 34 → 35 taken 25 times.
✓ Branch 34 → 39 taken 167 times.
✓ Branch 35 → 36 taken 25 times.
✗ Branch 35 → 39 not taken.
192 } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && s1->bit_rate &&
942
2/4
✓ Branch 36 → 37 taken 25 times.
✗ Branch 36 → 38 not taken.
✓ Branch 37 → 38 taken 25 times.
✗ Branch 37 → 39 not taken.
25 (s1->bit_rate != 0x3FFFF*400 || s1->vbv_delay != 0xFFFF)) {
943 25 avctx->bit_rate = s1->bit_rate;
944 }
945 299 s1->save_progressive_seq = s->progressive_sequence;
946 299 s1->save_chroma_format = s->chroma_format;
947
948 /* low_delay may be forced, in this case we will have B-frames
949 * that behave like P-frames. */
950 299 avctx->has_b_frames = !s->low_delay;
951
952
2/2
✓ Branch 39 → 40 taken 25 times.
✓ Branch 39 → 41 taken 274 times.
299 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
953 // MPEG-1 fps
954 25 avctx->framerate = ff_mpeg12_frame_rate_tab[s1->frame_rate_index];
955 25 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
956 } else { // MPEG-2
957 // MPEG-2 fps
958 274 av_reduce(&s->avctx->framerate.num,
959 274 &s->avctx->framerate.den,
960 274 ff_mpeg12_frame_rate_tab[s1->frame_rate_index].num * s1->frame_rate_ext.num,
961 274 ff_mpeg12_frame_rate_tab[s1->frame_rate_index].den * s1->frame_rate_ext.den,
962 1 << 30);
963
964
2/3
✓ Branch 42 → 43 taken 213 times.
✓ Branch 42 → 44 taken 61 times.
✗ Branch 42 → 45 not taken.
274 switch (s->chroma_format) {
965 213 case CHROMA_420: avctx->chroma_sample_location = AVCHROMA_LOC_LEFT; break;
966 61 case CHROMA_422:
967 61 case CHROMA_444: avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT; break;
968 ✗ default: av_assert0(0);
969 }
970 } // MPEG-2
971
972 299 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
973
974
1/2
✗ Branch 49 → 50 not taken.
✓ Branch 49 → 51 taken 299 times.
299 if ((ret = ff_mpv_common_init(s)) < 0)
975 ✗ return ret;
976
1/2
✓ Branch 51 → 52 taken 299 times.
✗ Branch 51 → 56 not taken.
299 if (!s->avctx->lowres)
977
2/2
✓ Branch 55 → 53 taken 347 times.
✓ Branch 55 → 56 taken 299 times.
646 for (int i = 0; i < s->slice_context_count; i++)
978 347 ff_mpv_framesize_disable(&s->thread_context[i]->sc);
979 }
980 5371 return 0;
981 }
982
983 5371 static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
984 int buf_size)
985 {
986 5371 Mpeg1Context *s1 = avctx->priv_data;
987 5371 MPVContext *const s = &s1->slice.c;
988 5371 GetBitContext gb0, *const gb = &gb0;
989 int ref, f_code, vbv_delay, ret;
990
991 5371 ret = init_get_bits8(gb, buf, buf_size);
992
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 5371 times.
5371 if (ret < 0)
993 ✗ return ret;
994
995 5371 ref = get_bits(gb, 10); /* temporal ref */
996 5371 s->pict_type = get_bits(gb, 3);
997
2/4
✓ Branch 7 → 8 taken 5371 times.
✗ Branch 7 → 9 not taken.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 5371 times.
5371 if (s->pict_type == 0 || s->pict_type > 3)
998 ✗ return AVERROR_INVALIDDATA;
999
1000 5371 vbv_delay = get_bits(gb, 16);
1001 5371 s1->vbv_delay = vbv_delay;
1002
2/2
✓ Branch 11 → 12 taken 2511 times.
✓ Branch 11 → 13 taken 2860 times.
5371 if (s->pict_type == AV_PICTURE_TYPE_P ||
1003
2/2
✓ Branch 12 → 13 taken 1754 times.
✓ Branch 12 → 19 taken 757 times.
2511 s->pict_type == AV_PICTURE_TYPE_B) {
1004 4614 s->full_pel[0] = get_bits1(gb);
1005 4614 f_code = get_bits(gb, 3);
1006
1/4
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 4614 times.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
4614 if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1007 ✗ return AVERROR_INVALIDDATA;
1008 4614 f_code += !f_code;
1009 4614 s->mpeg_f_code[0][0] = f_code;
1010 4614 s->mpeg_f_code[0][1] = f_code;
1011 }
1012
2/2
✓ Branch 19 → 20 taken 1754 times.
✓ Branch 19 → 26 taken 3617 times.
5371 if (s->pict_type == AV_PICTURE_TYPE_B) {
1013 1754 s->full_pel[1] = get_bits1(gb);
1014 1754 f_code = get_bits(gb, 3);
1015
1/4
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 25 taken 1754 times.
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
1754 if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1016 ✗ return AVERROR_INVALIDDATA;
1017 1754 f_code += !f_code;
1018 1754 s->mpeg_f_code[1][0] = f_code;
1019 1754 s->mpeg_f_code[1][1] = f_code;
1020 }
1021
1022
1/2
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 28 taken 5371 times.
5371 if (avctx->debug & FF_DEBUG_PICT_INFO)
1023 ✗ av_log(avctx, AV_LOG_DEBUG,
1024 ✗ "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1025
1026 5371 return 0;
1027 }
1028
1029 803 static void mpeg_decode_sequence_extension(Mpeg1Context *const s1,
1030 GetBitContext *const gb)
1031 {
1032 803 MPVContext *const s = &s1->slice.c;
1033 int horiz_size_ext, vert_size_ext;
1034 int bit_rate_ext;
1035
1036 803 skip_bits(gb, 1); /* profile and level esc*/
1037 803 s->avctx->profile = get_bits(gb, 3);
1038 803 s->avctx->level = get_bits(gb, 4);
1039 803 s->progressive_sequence = get_bits1(gb); /* progressive_sequence */
1040 803 s->chroma_format = get_bits(gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1041
1042
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 803 times.
803 if (!s->chroma_format) {
1043 ✗ s->chroma_format = CHROMA_420;
1044 ✗ av_log(s->avctx, AV_LOG_WARNING, "Chroma format invalid\n");
1045 }
1046
1047 803 horiz_size_ext = get_bits(gb, 2);
1048 803 vert_size_ext = get_bits(gb, 2);
1049 803 s->width |= (horiz_size_ext << 12);
1050 803 s->height |= (vert_size_ext << 12);
1051 803 bit_rate_ext = get_bits(gb, 12); /* XXX: handle it */
1052 803 s1->bit_rate += (bit_rate_ext << 18) * 400LL;
1053 803 check_marker(s->avctx, gb, "after bit rate extension");
1054 803 s->avctx->rc_buffer_size += get_bits(gb, 8) * 1024 * 16 << 10;
1055
1056 803 s->low_delay = get_bits1(gb);
1057
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 803 times.
803 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
1058 ✗ s->low_delay = 1;
1059
1060 803 s1->frame_rate_ext.num = get_bits(gb, 2) + 1;
1061 803 s1->frame_rate_ext.den = get_bits(gb, 5) + 1;
1062
1063 ff_dlog(s->avctx, "sequence extension\n");
1064 803 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1065
1066
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 803 times.
803 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1067 ✗ av_log(s->avctx, AV_LOG_DEBUG,
1068 "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%"PRId64"\n",
1069 ✗ s->avctx->profile, s->avctx->level, s->progressive_sequence, s->chroma_format,
1070 ✗ s->avctx->rc_buffer_size, s1->bit_rate);
1071 803 }
1072
1073 151 static void mpeg_decode_sequence_display_extension(Mpeg1Context *const s1,
1074 GetBitContext *const gb)
1075 {
1076 151 MPVContext *const s = &s1->slice.c;
1077 int color_description, w, h;
1078
1079 151 skip_bits(gb, 3); /* video format */
1080 151 color_description = get_bits1(gb);
1081
2/2
✓ Branch 4 → 5 taken 89 times.
✓ Branch 4 → 22 taken 62 times.
151 if (color_description) {
1082 89 s->avctx->color_primaries = get_bits(gb, 8);
1083 89 s->avctx->color_trc = get_bits(gb, 8);
1084 89 s->avctx->colorspace = get_bits(gb, 8);
1085
1086 // Reserved and unassigned values carry no colour information
1087
1/2
✓ Branch 8 → 9 taken 89 times.
✗ Branch 8 → 12 not taken.
89 if (s->avctx->color_primaries == AVCOL_PRI_RESERVED0 ||
1088
3/4
✓ Branch 9 → 10 taken 86 times.
✓ Branch 9 → 12 taken 3 times.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 86 times.
175 s->avctx->color_primaries == AVCOL_PRI_RESERVED ||
1089 86 !av_color_primaries_name(s->avctx->color_primaries))
1090 3 s->avctx->color_primaries = AVCOL_PRI_UNSPECIFIED;
1091
1/2
✓ Branch 13 → 14 taken 89 times.
✗ Branch 13 → 17 not taken.
89 if (s->avctx->color_trc == AVCOL_TRC_RESERVED0 ||
1092
3/4
✓ Branch 14 → 15 taken 86 times.
✓ Branch 14 → 17 taken 3 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 86 times.
175 s->avctx->color_trc == AVCOL_TRC_RESERVED ||
1093 86 !av_color_transfer_name(s->avctx->color_trc))
1094 3 s->avctx->color_trc = AVCOL_TRC_UNSPECIFIED;
1095
3/4
✓ Branch 18 → 19 taken 86 times.
✓ Branch 18 → 21 taken 3 times.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 86 times.
175 if (s->avctx->colorspace == AVCOL_SPC_RESERVED ||
1096 86 !av_color_space_name(s->avctx->colorspace))
1097 3 s->avctx->colorspace = AVCOL_SPC_UNSPECIFIED;
1098 }
1099 151 w = get_bits(gb, 14);
1100 151 skip_bits(gb, 1); // marker
1101 151 h = get_bits(gb, 14);
1102 // remaining 3 bits are zero padding
1103
1104 151 s1->pan_scan.width = 16 * w;
1105 151 s1->pan_scan.height = 16 * h;
1106
1107
1/2
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 151 times.
151 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1108 ✗ av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1109 151 }
1110
1111 31 static void mpeg_decode_picture_display_extension(Mpeg1Context *const s1,
1112 GetBitContext *const gb)
1113 {
1114 31 MPVContext *const s = &s1->slice.c;
1115 int i, nofco;
1116
1117 31 nofco = 1;
1118
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 6 taken 31 times.
31 if (s->progressive_sequence) {
1119
0/2
✗ Branch 3 → 4 not taken.
✗ Branch 3 → 9 not taken.
✗ if (s->repeat_first_field) {
1120 ✗ nofco++;
1121
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 9 not taken.
✗ if (s->top_field_first)
1122 ✗ nofco++;
1123 }
1124 } else {
1125
2/2
✓ Branch 6 → 7 taken 13 times.
✓ Branch 6 → 9 taken 18 times.
31 if (s->picture_structure == PICT_FRAME) {
1126 13 nofco++;
1127
2/2
✓ Branch 7 → 8 taken 6 times.
✓ Branch 7 → 9 taken 7 times.
13 if (s->repeat_first_field)
1128 6 nofco++;
1129 }
1130 }
1131
2/2
✓ Branch 15 → 10 taken 50 times.
✓ Branch 15 → 16 taken 31 times.
81 for (i = 0; i < nofco; i++) {
1132 50 s1->pan_scan.position[i][0] = get_sbits(gb, 16);
1133 50 skip_bits(gb, 1); // marker
1134 50 s1->pan_scan.position[i][1] = get_sbits(gb, 16);
1135 50 skip_bits(gb, 1); // marker
1136 }
1137
1138
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 31 times.
31 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1139 ✗ av_log(s->avctx, AV_LOG_DEBUG,
1140 "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
1141 ✗ s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1142 ✗ s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1143 ✗ s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1144 31 }
1145
1146 259 static int load_matrix(MPVContext *const s, GetBitContext *const gb,
1147 uint16_t matrix0[64], uint16_t matrix1[64], int intra)
1148 {
1149 int i;
1150
1151
2/2
✓ Branch 15 → 3 taken 16576 times.
✓ Branch 15 → 16 taken 259 times.
16835 for (i = 0; i < 64; i++) {
1152 16576 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1153 16576 int v = get_bits(gb, 8);
1154
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 7 taken 16576 times.
16576 if (v == 0) {
1155 ✗ av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1156 ✗ return AVERROR_INVALIDDATA;
1157 }
1158
5/6
✓ Branch 7 → 8 taken 5184 times.
✓ Branch 7 → 12 taken 11392 times.
✓ Branch 8 → 9 taken 81 times.
✓ Branch 8 → 12 taken 5103 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 81 times.
16576 if (intra && i == 0 && v != 8) {
1159 ✗ av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v);
1160 ✗ v = 8; // needed by pink.mpg / issue1046
1161 }
1162 16576 matrix0[j] = v;
1163
2/2
✓ Branch 12 → 13 taken 15936 times.
✓ Branch 12 → 14 taken 640 times.
16576 if (matrix1)
1164 15936 matrix1[j] = v;
1165 }
1166 259 return 0;
1167 }
1168
1169 1053 static void mpeg_decode_quant_matrix_extension(MPVContext *const s,
1170 GetBitContext *const gb)
1171 {
1172 ff_dlog(s->avctx, "matrix extension\n");
1173
1174
2/2
✓ Branch 3 → 4 taken 34 times.
✓ Branch 3 → 5 taken 1019 times.
1053 if (get_bits1(gb))
1175 34 load_matrix(s, gb, s->chroma_intra_matrix, s->intra_matrix, 1);
1176
2/2
✓ Branch 6 → 7 taken 77 times.
✓ Branch 6 → 8 taken 976 times.
1053 if (get_bits1(gb))
1177 77 load_matrix(s, gb, s->chroma_inter_matrix, s->inter_matrix, 0);
1178
2/2
✓ Branch 9 → 10 taken 9 times.
✓ Branch 9 → 11 taken 1044 times.
1053 if (get_bits1(gb))
1179 9 load_matrix(s, gb, s->chroma_intra_matrix, NULL, 1);
1180
2/2
✓ Branch 12 → 13 taken 1 time.
✓ Branch 12 → 14 taken 1052 times.
1053 if (get_bits1(gb))
1181 1 load_matrix(s, gb, s->chroma_inter_matrix, NULL, 0);
1182 1053 }
1183
1184 4877 static int mpeg_decode_picture_coding_extension(Mpeg1Context *const s1,
1185 GetBitContext *const gb)
1186 {
1187 4877 MPVContext *const s = &s1->slice.c;
1188
1189 4877 s->full_pel[0] = s->full_pel[1] = 0;
1190 4877 s->mpeg_f_code[0][0] = get_bits(gb, 4);
1191 4877 s->mpeg_f_code[0][1] = get_bits(gb, 4);
1192 4877 s->mpeg_f_code[1][0] = get_bits(gb, 4);
1193 4877 s->mpeg_f_code[1][1] = get_bits(gb, 4);
1194 4877 s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0];
1195 4877 s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1];
1196 4877 s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0];
1197 4877 s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1];
1198
1/4
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 20 taken 4877 times.
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 20 not taken.
4877 if (!s->pict_type && s->context_initialized) {
1199 ✗ av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code\n");
1200
0/2
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 11 not taken.
✗ if (s->avctx->err_recognition & AV_EF_EXPLODE)
1201 ✗ return AVERROR_INVALIDDATA;
1202 ✗ av_log(s->avctx, AV_LOG_WARNING, "Guessing pict_type from mpeg_f_code\n");
1203
0/4
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 19 not taken.
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 19 not taken.
✗ if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1204
0/4
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 17 not taken.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 17 not taken.
✗ if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1205 ✗ s->pict_type = AV_PICTURE_TYPE_I;
1206 else
1207 ✗ s->pict_type = AV_PICTURE_TYPE_P;
1208 } else
1209 ✗ s->pict_type = AV_PICTURE_TYPE_B;
1210 }
1211
1212 4877 s->intra_dc_precision = get_bits(gb, 2);
1213 4877 s->picture_structure = get_bits(gb, 2);
1214 4877 s->top_field_first = get_bits1(gb);
1215 4877 s->frame_pred_frame_dct = get_bits1(gb);
1216 4877 s->concealment_motion_vectors = get_bits1(gb);
1217 4877 s->q_scale_type = get_bits1(gb);
1218 4877 s->intra_vlc_format = get_bits1(gb);
1219 4877 s->alternate_scan = get_bits1(gb);
1220 4877 s->repeat_first_field = get_bits1(gb);
1221 4877 s->chroma_420_type = get_bits1(gb);
1222 4877 s->progressive_frame = get_bits1(gb);
1223
1224 // We only initialize intra_scantable.permutated, as this is all we use.
1225 4877 ff_permute_scantable(s->intra_scantable.permutated,
1226 4877 s->alternate_scan ? ff_alternate_vertical_scan : ff_zigzag_direct,
1227
2/2
✓ Branch 31 → 32 taken 1767 times.
✓ Branch 31 → 33 taken 3110 times.
4877 s->idsp.idct_permutation);
1228
1229 /* composite display not parsed */
1230 ff_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1231 ff_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1232 ff_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1233 ff_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1234 ff_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1235 ff_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1236 ff_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1237 ff_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1238 ff_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1239
1240 4877 return 0;
1241 }
1242
1243 4868 static int mpeg_field_start(Mpeg1Context *s1, const uint8_t *buf, int buf_size)
1244 {
1245 4868 MPVContext *const s = &s1->slice.c;
1246 4868 AVCodecContext *avctx = s->avctx;
1247 4868 int second_field = 0;
1248 int ret;
1249
1250
1/2
✓ Branch 2 → 3 taken 4868 times.
✗ Branch 2 → 5 not taken.
4868 if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {
1251
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 4868 times.
4868 if (s->mb_width * s->mb_height * 11LL / (33 * 2 * 8) > buf_size)
1252 ✗ return AVERROR_INVALIDDATA;
1253 }
1254
1255 /* start frame decoding */
1256
4/4
✓ Branch 5 → 6 taken 4130 times.
✓ Branch 5 → 7 taken 738 times.
✓ Branch 6 → 7 taken 3392 times.
✓ Branch 6 → 53 taken 738 times.
8998 if (s->first_field || s->picture_structure == PICT_FRAME) {
1257 AVFrameSideData *pan_scan;
1258
1259
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 4130 times.
4130 if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
1260 ✗ return ret;
1261
1262
2/2
✓ Branch 10 → 11 taken 738 times.
✓ Branch 10 → 22 taken 3392 times.
4130 if (s->picture_structure != PICT_FRAME) {
1263 738 s->cur_pic.ptr->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST *
1264
2/2
✓ Branch 11 → 12 taken 2 times.
✓ Branch 11 → 13 taken 736 times.
738 (s->picture_structure == PICT_TOP_FIELD);
1265
1266
2/2
✓ Branch 21 → 15 taken 2214 times.
✓ Branch 21 → 22 taken 738 times.
2952 for (int i = 0; i < 3; i++) {
1267
2/2
✓ Branch 15 → 16 taken 2208 times.
✓ Branch 15 → 20 taken 6 times.
2214 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1268
1/2
✓ Branch 16 → 17 taken 2208 times.
✗ Branch 16 → 18 not taken.
2208 s->cur_pic.data[i] = FF_PTR_ADD(s->cur_pic.data[i],
1269 s->cur_pic.linesize[i]);
1270 }
1271 2214 s->cur_pic.linesize[i] *= 2;
1272 }
1273 }
1274
1275 4130 ff_mpeg_er_frame_start(s);
1276
1277 /* first check if we must repeat the frame */
1278 4130 s->cur_pic.ptr->f->repeat_pict = 0;
1279
2/2
✓ Branch 23 → 24 taken 6 times.
✓ Branch 23 → 30 taken 4124 times.
4130 if (s->repeat_first_field) {
1280
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 28 taken 6 times.
6 if (s->progressive_sequence) {
1281
0/2
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 27 not taken.
✗ if (s->top_field_first)
1282 ✗ s->cur_pic.ptr->f->repeat_pict = 4;
1283 else
1284 ✗ s->cur_pic.ptr->f->repeat_pict = 2;
1285
1/2
✓ Branch 28 → 29 taken 6 times.
✗ Branch 28 → 30 not taken.
6 } else if (s->progressive_frame) {
1286 6 s->cur_pic.ptr->f->repeat_pict = 1;
1287 }
1288 }
1289
1290 4130 ret = ff_frame_new_side_data(s->avctx, s->cur_pic.ptr->f,
1291 AV_FRAME_DATA_PANSCAN, sizeof(s1->pan_scan),
1292 &pan_scan);
1293
1/2
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 33 taken 4130 times.
4130 if (ret < 0)
1294 ✗ return ret;
1295
1/2
✓ Branch 33 → 34 taken 4130 times.
✗ Branch 33 → 35 not taken.
4130 if (pan_scan)
1296 4130 memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
1297
1298
2/2
✓ Branch 35 → 36 taken 944 times.
✓ Branch 35 → 39 taken 3186 times.
4130 if (s1->a53_buf_ref) {
1299 944 ret = ff_frame_new_side_data_from_buf(
1300 944 s->avctx, s->cur_pic.ptr->f, AV_FRAME_DATA_A53_CC,
1301 &s1->a53_buf_ref);
1302
1/2
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 39 taken 944 times.
944 if (ret < 0)
1303 ✗ return ret;
1304 }
1305
1306
1/2
✗ Branch 39 → 40 not taken.
✓ Branch 39 → 44 taken 4130 times.
4130 if (s1->has_stereo3d) {
1307 ✗ AVStereo3D *stereo = av_stereo3d_create_side_data(s->cur_pic.ptr->f);
1308
0/2
✗ Branch 41 → 42 not taken.
✗ Branch 41 → 43 not taken.
✗ if (!stereo)
1309 ✗ return AVERROR(ENOMEM);
1310
1311 ✗ stereo->type = s1->stereo3d_type;
1312 ✗ s1->has_stereo3d = 0;
1313 }
1314
1315
2/2
✓ Branch 44 → 45 taken 404 times.
✓ Branch 44 → 51 taken 3726 times.
4130 if (s1->has_afd) {
1316 AVFrameSideData *sd;
1317 404 ret = ff_frame_new_side_data(s->avctx, s->cur_pic.ptr->f,
1318 AV_FRAME_DATA_AFD, 1, &sd);
1319
1/2
✗ Branch 46 → 47 not taken.
✓ Branch 46 → 48 taken 404 times.
404 if (ret < 0)
1320 ✗ return ret;
1321
1/2
✓ Branch 48 → 49 taken 404 times.
✗ Branch 48 → 50 not taken.
404 if (sd)
1322 404 *sd->data = s1->afd;
1323 404 s1->has_afd = 0;
1324 }
1325 } else { // second field
1326 738 second_field = 1;
1327
1/2
✗ Branch 53 → 54 not taken.
✓ Branch 53 → 56 taken 738 times.
738 if (!s->cur_pic.ptr) {
1328 ✗ av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1329 ✗ return AVERROR_INVALIDDATA;
1330 }
1331
1332
1/2
✗ Branch 56 → 57 not taken.
✓ Branch 56 → 62 taken 738 times.
738 if (s->avctx->hwaccel) {
1333
0/2
✗ Branch 59 → 60 not taken.
✗ Branch 59 → 62 not taken.
✗ if ((ret = FF_HW_SIMPLE_CALL(s->avctx, end_frame)) < 0) {
1334 ✗ av_log(avctx, AV_LOG_ERROR,
1335 "hardware accelerator failed to decode first field\n");
1336 ✗ return ret;
1337 }
1338 }
1339 738 ret = ff_mpv_alloc_dummy_frames(s);
1340
1/2
✗ Branch 63 → 64 not taken.
✓ Branch 63 → 65 taken 738 times.
738 if (ret < 0)
1341 ✗ return ret;
1342
1343
2/2
✓ Branch 69 → 66 taken 2214 times.
✓ Branch 69 → 70 taken 738 times.
2952 for (int i = 0; i < 3; i++) {
1344 2214 s->cur_pic.data[i] = s->cur_pic.ptr->f->data[i];
1345
2/2
✓ Branch 66 → 67 taken 6 times.
✓ Branch 66 → 68 taken 2208 times.
2214 if (s->picture_structure == PICT_BOTTOM_FIELD)
1346 6 s->cur_pic.data[i] +=
1347 6 s->cur_pic.ptr->f->linesize[i];
1348 }
1349 }
1350
1351
1/2
✗ Branch 70 → 71 not taken.
✓ Branch 70 → 75 taken 4868 times.
4868 if (avctx->hwaccel) {
1352
0/2
✗ Branch 73 → 74 not taken.
✗ Branch 73 → 78 not taken.
✗ if ((ret = FF_HW_CALL(avctx, start_frame, NULL, buf, buf_size)) < 0)
1353 ✗ return ret;
1354
2/2
✓ Branch 75 → 76 taken 157 times.
✓ Branch 75 → 78 taken 4711 times.
4868 } else if (s->codec_tag == MKTAG('V', 'C', 'R', '2')) {
1355 // Exchange UV
1356 157 FFSWAP(uint8_t*, s->cur_pic.data[1], s->cur_pic.data[2]);
1357 157 FFSWAP(ptrdiff_t, s->cur_pic.linesize[1], s->cur_pic.linesize[2]);
1358
1/2
✓ Branch 76 → 77 taken 157 times.
✗ Branch 76 → 78 not taken.
157 if (!second_field) {
1359 157 FFSWAP(uint8_t*, s->next_pic.data[1], s->next_pic.data[2]);
1360 157 FFSWAP(ptrdiff_t, s->next_pic.linesize[1], s->next_pic.linesize[2]);
1361 157 FFSWAP(uint8_t*, s->last_pic.data[1], s->last_pic.data[2]);
1362 157 FFSWAP(ptrdiff_t, s->last_pic.linesize[1], s->last_pic.linesize[2]);
1363 }
1364 }
1365
1366 4868 return 0;
1367 }
1368
1369 #define DECODE_SLICE_ERROR -1
1370 #define DECODE_SLICE_OK 0
1371
1372 /**
1373 * Decode a slice.
1374 * Mpeg12SliceContext.c.mb_y must be set to the MB row from the startcode.
1375 * @return DECODE_SLICE_ERROR if the slice is damaged,
1376 * DECODE_SLICE_OK if this slice is OK
1377 */
1378 135519 static int mpeg_decode_slice(Mpeg12SliceContext *const s, int mb_y,
1379 const uint8_t **buf, int buf_size)
1380 {
1381 135519 AVCodecContext *avctx = s->c.avctx;
1382 135519 const int lowres = s->c.avctx->lowres;
1383 135519 const int field_pic = s->c.picture_structure != PICT_FRAME;
1384 int ret;
1385
1386 135519 s->c.resync_mb_x =
1387 135519 s->c.resync_mb_y = -1;
1388
1389
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 135519 times.
135519 av_assert0(mb_y < s->c.mb_height);
1390
1391 135519 ret = init_get_bits8(&s->gb, *buf, buf_size);
1392
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 135519 times.
135519 if (ret < 0)
1393 ✗ return ret;
1394
1395
3/4
✓ Branch 8 → 9 taken 128246 times.
✓ Branch 8 → 11 taken 7273 times.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 128246 times.
135519 if (s->c.codec_id != AV_CODEC_ID_MPEG1VIDEO && s->c.mb_height > 2800/16)
1396 ✗ skip_bits(&s->gb, 3);
1397
1398 135519 s->c.interlaced_dct = 0;
1399
1400 135519 s->c.qscale = mpeg_get_qscale(&s->gb, s->c.q_scale_type);
1401
1402
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 15 taken 135519 times.
135519 if (s->c.qscale == 0) {
1403 ✗ av_log(s->c.avctx, AV_LOG_ERROR, "qscale == 0\n");
1404 ✗ return AVERROR_INVALIDDATA;
1405 }
1406
1407 /* extra slice info */
1408
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 135519 times.
135519 if (skip_1stop_8data_bits(&s->gb) < 0)
1409 ✗ return AVERROR_INVALIDDATA;
1410
1411 135519 s->c.mb_x = 0;
1412
1413
3/4
✓ Branch 18 → 19 taken 5669 times.
✓ Branch 18 → 21 taken 129850 times.
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 5669 times.
135519 if (mb_y == 0 && s->c.codec_tag == AV_RL32("SLIF")) {
1414 ✗ skip_bits1(&s->gb);
1415 } else {
1416
1/2
✓ Branch 33 → 22 taken 148877 times.
✗ Branch 33 → 34 not taken.
284396 while (get_bits_left(&s->gb) > 0) {
1417 148877 int code = get_vlc2(&s->gb, ff_mbincr_vlc,
1418 MBINCR_VLC_BITS, 2);
1419
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 26 taken 148877 times.
148877 if (code < 0) {
1420 ✗ av_log(s->c.avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1421 ✗ return AVERROR_INVALIDDATA;
1422 }
1423
2/2
✓ Branch 26 → 27 taken 13358 times.
✓ Branch 26 → 30 taken 135519 times.
148877 if (code >= 33) {
1424
1/2
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 29 taken 13358 times.
13358 if (code == 33)
1425 13358 s->c.mb_x += 33;
1426 /* otherwise, stuffing, nothing to do */
1427 } else {
1428 135519 s->c.mb_x += code;
1429 135519 break;
1430 }
1431 }
1432 }
1433
1434
1/2
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 37 taken 135519 times.
135519 if (s->c.mb_x >= (unsigned) s->c.mb_width) {
1435 ✗ av_log(s->c.avctx, AV_LOG_ERROR, "initial skip overflow\n");
1436 ✗ return AVERROR_INVALIDDATA;
1437 }
1438
1439
1/2
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 47 taken 135519 times.
135519 if (avctx->hwaccel) {
1440 ✗ const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1441 ✗ int start_code = -1;
1442 ✗ buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1443
0/2
✗ Branch 39 → 40 not taken.
✗ Branch 39 → 41 not taken.
✗ if (buf_end < *buf + buf_size)
1444 ✗ buf_end -= 4;
1445 ✗ s->c.mb_y = mb_y;
1446
0/2
✗ Branch 43 → 44 not taken.
✗ Branch 43 → 45 not taken.
✗ if (FF_HW_CALL(avctx, decode_slice, buf_start, buf_end - buf_start) < 0)
1447 ✗ return DECODE_SLICE_ERROR;
1448 ✗ *buf = buf_end;
1449 ✗ return DECODE_SLICE_OK;
1450 }
1451
1452 135519 s->c.resync_mb_x = s->c.mb_x;
1453 135519 s->c.resync_mb_y = s->c.mb_y = mb_y;
1454 135519 ff_init_block_index(&s->c);
1455
1456
8/8
✓ Branch 48 → 49 taken 5669 times.
✓ Branch 48 → 76 taken 129850 times.
✓ Branch 49 → 50 taken 4130 times.
✓ Branch 49 → 76 taken 1539 times.
✓ Branch 50 → 51 taken 4128 times.
✓ Branch 50 → 52 taken 2 times.
✓ Branch 51 → 52 taken 3392 times.
✓ Branch 51 → 76 taken 736 times.
135519 if (s->c.mb_y == 0 && s->c.mb_x == 0 && (s->c.first_field || s->c.picture_structure == PICT_FRAME)) {
1457
1/2
✗ Branch 52 → 53 not taken.
✓ Branch 52 → 76 taken 3394 times.
3394 if (s->c.avctx->debug & FF_DEBUG_PICT_INFO) {
1458 ✗ av_log(s->c.avctx, AV_LOG_DEBUG,
1459 "qp:%d fc:%2d%2d%2d%2d %c %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
1460 s->c.qscale,
1461 s->c.mpeg_f_code[0][0], s->c.mpeg_f_code[0][1],
1462 s->c.mpeg_f_code[1][0], s->c.mpeg_f_code[1][1],
1463
0/2
✗ Branch 68 → 69 not taken.
✗ Branch 68 → 74 not taken.
✗ s->c.pict_type == AV_PICTURE_TYPE_I ? 'I' :
1464
0/2
✗ Branch 69 → 70 not taken.
✗ Branch 69 → 73 not taken.
✗ (s->c.pict_type == AV_PICTURE_TYPE_P ? 'P' :
1465
0/2
✗ Branch 70 → 71 not taken.
✗ Branch 70 → 72 not taken.
✗ (s->c.pict_type == AV_PICTURE_TYPE_B ? 'B' : 'S')),
1466
0/2
✗ Branch 65 → 66 not taken.
✗ Branch 65 → 67 not taken.
✗ s->c.progressive_sequence ? "ps" : "",
1467
0/2
✗ Branch 62 → 63 not taken.
✗ Branch 62 → 64 not taken.
✗ s->c.progressive_frame ? "pf" : "",
1468
0/2
✗ Branch 59 → 60 not taken.
✗ Branch 59 → 61 not taken.
✗ s->c.alternate_scan ? "alt" : "",
1469
0/2
✗ Branch 56 → 57 not taken.
✗ Branch 56 → 58 not taken.
✗ s->c.top_field_first ? "top" : "",
1470 s->c.intra_dc_precision, s->c.picture_structure,
1471 s->c.frame_pred_frame_dct, s->c.concealment_motion_vectors,
1472 s->c.q_scale_type, s->c.intra_vlc_format,
1473
0/2
✗ Branch 53 → 54 not taken.
✗ Branch 53 → 55 not taken.
✗ s->c.repeat_first_field, s->c.chroma_420_type ? "420" : "");
1474 }
1475 }
1476
1477 135519 s->last_dc[0] = 128 << s->c.intra_dc_precision;
1478 135519 s->last_dc[1] = s->last_dc[0];
1479 135519 s->last_dc[2] = s->last_dc[0];
1480 135519 memset(s->c.last_mv, 0, sizeof(s->c.last_mv));
1481
1482 135519 for (int mb_skip_run = 0;;) {
1483 2915126 ret = mpeg_decode_mb(s, &mb_skip_run);
1484
2/2
✓ Branch 78 → 79 taken 11 times.
✓ Branch 78 → 80 taken 2915115 times.
2915126 if (ret < 0)
1485 11 return ret;
1486
1487 // Note motion_val is normally NULL unless we want to extract the MVs.
1488
1/2
✗ Branch 80 → 81 not taken.
✓ Branch 80 → 96 taken 2915115 times.
2915115 if (s->c.cur_pic.motion_val[0]) {
1489 ✗ const int wrap = s->c.b8_stride;
1490 ✗ int xy = s->c.mb_x * 2 + s->c.mb_y * 2 * wrap;
1491 ✗ int b8_xy = 4 * (s->c.mb_x + s->c.mb_y * s->c.mb_stride);
1492 int motion_x, motion_y, dir, i;
1493
1494
0/2
✗ Branch 95 → 82 not taken.
✗ Branch 95 → 96 not taken.
✗ for (i = 0; i < 2; i++) {
1495
0/2
✗ Branch 93 → 83 not taken.
✗ Branch 93 → 94 not taken.
✗ for (dir = 0; dir < 2; dir++) {
1496
0/4
✗ Branch 83 → 84 not taken.
✗ Branch 83 → 86 not taken.
✗ Branch 84 → 85 not taken.
✗ Branch 84 → 87 not taken.
✗ if (s->c.mb_intra ||
1497
0/2
✗ Branch 85 → 86 not taken.
✗ Branch 85 → 87 not taken.
✗ (dir == 1 && s->c.pict_type != AV_PICTURE_TYPE_B)) {
1498 ✗ motion_x = motion_y = 0;
1499
0/2
✗ Branch 87 → 88 not taken.
✗ Branch 87 → 90 not taken.
✗ } else if (s->c.mv_type == MV_TYPE_16X16 ||
1500
0/4
✗ Branch 88 → 89 not taken.
✗ Branch 88 → 91 not taken.
✗ Branch 89 → 90 not taken.
✗ Branch 89 → 91 not taken.
✗ (s->c.mv_type == MV_TYPE_FIELD && field_pic)) {
1501 ✗ motion_x = s->c.mv[dir][0][0];
1502 ✗ motion_y = s->c.mv[dir][0][1];
1503 } else { /* if ((s->c.mv_type == MV_TYPE_FIELD) || (s->c.mv_type == MV_TYPE_16X8)) */
1504 ✗ motion_x = s->c.mv[dir][i][0];
1505 ✗ motion_y = s->c.mv[dir][i][1];
1506 }
1507
1508 ✗ s->c.cur_pic.motion_val[dir][xy][0] = motion_x;
1509 ✗ s->c.cur_pic.motion_val[dir][xy][1] = motion_y;
1510 ✗ s->c.cur_pic.motion_val[dir][xy + 1][0] = motion_x;
1511 ✗ s->c.cur_pic.motion_val[dir][xy + 1][1] = motion_y;
1512 ✗ s->c.cur_pic.ref_index [dir][b8_xy] =
1513 ✗ s->c.cur_pic.ref_index [dir][b8_xy + 1] = s->c.field_select[dir][i];
1514 av_assert2(s->c.field_select[dir][i] == 0 ||
1515 s->c.field_select[dir][i] == 1);
1516 }
1517 ✗ xy += wrap;
1518 ✗ b8_xy += 2;
1519 }
1520 }
1521
1522 2915115 s->c.dest[0] += 16 >> lowres;
1523 2915115 s->c.dest[1] +=(16 >> lowres) >> s->c.chroma_x_shift;
1524 2915115 s->c.dest[2] +=(16 >> lowres) >> s->c.chroma_x_shift;
1525
1526 2915115 ff_mpv_reconstruct_mb(&s->c, s->block);
1527
1528
2/2
✓ Branch 97 → 98 taken 86287 times.
✓ Branch 97 → 148 taken 2828828 times.
2915115 if (++s->c.mb_x >= s->c.mb_width) {
1529 86287 const int mb_size = 16 >> s->c.avctx->lowres;
1530 int left;
1531
1532 86287 ff_mpeg_draw_horiz_band(&s->c, mb_size * (s->c.mb_y >> field_pic), mb_size);
1533
1534 86287 s->c.mb_x = 0;
1535 86287 s->c.mb_y += 1 << field_pic;
1536
1537
2/2
✓ Branch 99 → 100 taken 4857 times.
✓ Branch 99 → 137 taken 81430 times.
86287 if (s->c.mb_y >= s->c.mb_height) {
1538 4857 int left = get_bits_left(&s->gb);
1539 9975 int is_d10 = s->c.chroma_format == CHROMA_422 &&
1540
2/2
✓ Branch 102 → 103 taken 49 times.
✓ Branch 102 → 110 taken 212 times.
261 s->c.pict_type == AV_PICTURE_TYPE_I &&
1541
2/4
✓ Branch 103 → 104 taken 49 times.
✗ Branch 103 → 110 not taken.
✓ Branch 104 → 105 taken 49 times.
✗ Branch 104 → 110 not taken.
49 avctx->profile == 0 && avctx->level == 5 &&
1542
2/2
✓ Branch 105 → 106 taken 25 times.
✓ Branch 105 → 110 taken 24 times.
49 s->c.intra_dc_precision == 2 &&
1543
4/6
✓ Branch 101 → 102 taken 261 times.
✓ Branch 101 → 110 taken 4596 times.
✓ Branch 106 → 107 taken 25 times.
✗ Branch 106 → 110 not taken.
✓ Branch 107 → 108 taken 25 times.
✗ Branch 107 → 110 not taken.
5143 s->c.q_scale_type == 1 && s->c.alternate_scan == 0 &&
1544
1/2
✓ Branch 108 → 109 taken 25 times.
✗ Branch 108 → 110 not taken.
25 s->c.progressive_frame == 0
1545 /* vbv_delay == 0xBBB || 0xE10 */;
1546
1547
4/4
✓ Branch 111 → 112 taken 1508 times.
✓ Branch 111 → 124 taken 3349 times.
✓ Branch 112 → 113 taken 1483 times.
✓ Branch 112 → 124 taken 25 times.
4857 if (left >= 32 && !is_d10) {
1548 1483 GetBitContext gb = s->gb;
1549 1483 align_get_bits(&gb);
1550
1/2
✗ Branch 115 → 116 not taken.
✓ Branch 115 → 118 taken 1483 times.
1483 if (show_bits(&gb, 24) == 0x060E2B) {
1551 ✗ av_log(avctx, AV_LOG_DEBUG, "Invalid MXF data found in video stream\n");
1552 ✗ is_d10 = 1;
1553 }
1554
3/4
✓ Branch 118 → 119 taken 1482 times.
✓ Branch 118 → 123 taken 1 time.
✗ Branch 120 → 121 not taken.
✓ Branch 120 → 123 taken 1482 times.
1483 if (left > 32 && show_bits_long(&gb, 32) == 0x201) {
1555 ✗ av_log(avctx, AV_LOG_DEBUG, "skipping m704 alpha (unsupported)\n");
1556 ✗ goto eos;
1557 }
1558 }
1559
1560
3/4
✓ Branch 124 → 125 taken 4857 times.
✗ Branch 124 → 131 not taken.
✓ Branch 125 → 126 taken 4546 times.
✓ Branch 125 → 129 taken 311 times.
4857 if (left < 0 ||
1561
1/4
✗ Branch 127 → 128 not taken.
✓ Branch 127 → 129 taken 4546 times.
✗ Branch 128 → 129 not taken.
✗ Branch 128 → 131 not taken.
4546 (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
1562
1/4
✗ Branch 129 → 130 not taken.
✓ Branch 129 → 136 taken 4857 times.
✗ Branch 130 → 131 not taken.
✗ Branch 130 → 136 not taken.
4857 ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) {
1563
0/2
✗ Branch 131 → 132 not taken.
✗ Branch 131 → 133 not taken.
✗ av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X at %d %d\n",
1564 ✗ left, left>0 ? show_bits(&s->gb, FFMIN(left, 23)) : 0, s->c.mb_x, s->c.mb_y);
1565 ✗ return AVERROR_INVALIDDATA;
1566 } else
1567 4857 goto eos;
1568 }
1569 // There are some files out there which are missing the last slice
1570 // in cases where the slice is completely outside the visible
1571 // area, we detect this here instead of running into the end expecting
1572 // more data
1573 81430 left = get_bits_left(&s->gb);
1574
2/2
✓ Branch 138 → 139 taken 250 times.
✓ Branch 138 → 147 taken 81180 times.
81430 if (s->c.mb_y >= ((s->c.height + 15) >> 4) &&
1575
2/4
✓ Branch 139 → 140 taken 250 times.
✗ Branch 139 → 147 not taken.
✗ Branch 140 → 141 not taken.
✓ Branch 140 → 147 taken 250 times.
250 !s->c.progressive_sequence &&
1576
0/2
✗ Branch 141 → 142 not taken.
✗ Branch 141 → 147 not taken.
✗ left <= 25 &&
1577 ✗ left >= 0 &&
1578
0/4
✗ Branch 142 → 143 not taken.
✗ Branch 142 → 147 not taken.
✗ Branch 143 → 144 not taken.
✗ Branch 143 → 146 not taken.
✗ mb_skip_run == -1 &&
1579
0/2
✗ Branch 145 → 146 not taken.
✗ Branch 145 → 147 not taken.
✗ (!left || show_bits(&s->gb, left) == 0))
1580 ✗ goto eos;
1581
1582 81430 ff_init_block_index(&s->c);
1583 }
1584
1585 /* skip mb handling */
1586
2/2
✓ Branch 148 → 149 taken 2644113 times.
✓ Branch 148 → 179 taken 266145 times.
2910258 if (mb_skip_run == -1) {
1587 /* read increment again */
1588 2644113 mb_skip_run = 0;
1589 2459 for (;;) {
1590 2646572 int code = get_vlc2(&s->gb, ff_mbincr_vlc,
1591 MBINCR_VLC_BITS, 2);
1592
1/2
✗ Branch 151 → 152 not taken.
✓ Branch 151 → 154 taken 2646572 times.
2646572 if (code < 0) {
1593 ✗ av_log(s->c.avctx, AV_LOG_ERROR, "mb incr damaged\n");
1594 ✗ return AVERROR_INVALIDDATA;
1595 }
1596
2/2
✓ Branch 154 → 155 taken 133110 times.
✓ Branch 154 → 164 taken 2513462 times.
2646572 if (code >= 33) {
1597
2/2
✓ Branch 155 → 156 taken 2459 times.
✓ Branch 155 → 157 taken 130651 times.
133110 if (code == 33) {
1598 2459 mb_skip_run += 33;
1599
1/2
✓ Branch 157 → 158 taken 130651 times.
✗ Branch 157 → 165 not taken.
130651 } else if (code == 35) {
1600
2/4
✓ Branch 158 → 159 taken 130651 times.
✗ Branch 158 → 161 not taken.
✗ Branch 160 → 161 not taken.
✓ Branch 160 → 163 taken 130651 times.
130651 if (mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1601 ✗ av_log(s->c.avctx, AV_LOG_ERROR, "slice mismatch\n");
1602 ✗ return AVERROR_INVALIDDATA;
1603 }
1604 130651 goto eos; /* end of slice */
1605 }
1606 /* otherwise, stuffing, nothing to do */
1607 } else {
1608 2513462 mb_skip_run += code;
1609 2513462 break;
1610 }
1611 }
1612
2/2
✓ Branch 166 → 167 taken 87891 times.
✓ Branch 166 → 179 taken 2425571 times.
2513462 if (mb_skip_run) {
1613 int i;
1614
1/2
✗ Branch 167 → 168 not taken.
✓ Branch 167 → 170 taken 87891 times.
87891 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
1615 ✗ av_log(s->c.avctx, AV_LOG_ERROR,
1616 "skipped MB in I-frame at %d %d\n", s->c.mb_x, s->c.mb_y);
1617 ✗ return AVERROR_INVALIDDATA;
1618 }
1619
1620 /* skip mb */
1621 87891 s->c.mb_intra = 0;
1622
2/2
✓ Branch 172 → 171 taken 1054692 times.
✓ Branch 172 → 173 taken 87891 times.
1142583 for (i = 0; i < 12; i++)
1623 1054692 s->c.block_last_index[i] = -1;
1624 87891 s->last_dc[0] = s->last_dc[1] = s->last_dc[2] = 128 << s->c.intra_dc_precision;
1625
2/2
✓ Branch 173 → 174 taken 67754 times.
✓ Branch 173 → 175 taken 20137 times.
87891 if (s->c.picture_structure == PICT_FRAME)
1626 67754 s->c.mv_type = MV_TYPE_16X16;
1627 else
1628 20137 s->c.mv_type = MV_TYPE_FIELD;
1629
2/2
✓ Branch 176 → 177 taken 28471 times.
✓ Branch 176 → 178 taken 59420 times.
87891 if (s->c.pict_type == AV_PICTURE_TYPE_P) {
1630 /* if P type, zero motion vector is implied */
1631 28471 s->c.mv_dir = MV_DIR_FORWARD;
1632 28471 s->c.mv[0][0][0] = s->c.mv[0][0][1] = 0;
1633 28471 s->c.last_mv[0][0][0] = s->c.last_mv[0][0][1] = 0;
1634 28471 s->c.last_mv[0][1][0] = s->c.last_mv[0][1][1] = 0;
1635 28471 s->c.field_select[0][0] = (s->c.picture_structure - 1) & 1;
1636 } else {
1637 /* if B type, reuse previous vectors and directions */
1638 59420 s->c.mv[0][0][0] = s->c.last_mv[0][0][0];
1639 59420 s->c.mv[0][0][1] = s->c.last_mv[0][0][1];
1640 59420 s->c.mv[1][0][0] = s->c.last_mv[1][0][0];
1641 59420 s->c.mv[1][0][1] = s->c.last_mv[1][0][1];
1642 59420 s->c.field_select[0][0] = (s->c.picture_structure - 1) & 1;
1643 59420 s->c.field_select[1][0] = (s->c.picture_structure - 1) & 1;
1644 }
1645 }
1646 }
1647 }
1648 135508 eos: // end of slice
1649
1/2
✗ Branch 183 → 184 not taken.
✓ Branch 183 → 187 taken 135508 times.
135508 if (get_bits_left(&s->gb) < 0) {
1650 ✗ av_log(s->c.avctx, AV_LOG_ERROR, "overread %d\n", -get_bits_left(&s->gb));
1651 ✗ return AVERROR_INVALIDDATA;
1652 }
1653 135508 *buf += (get_bits_count(&s->gb) - 1) / 8;
1654 ff_dlog(s->c.avctx, "Slice start:%d %d end:%d %d\n", s->c.resync_mb_x, s->c.resync_mb_y, s->c.mb_x, s->c.mb_y);
1655 135508 return 0;
1656 }
1657
1658 8496 static int slice_decode_thread(AVCodecContext *c, void *arg)
1659 {
1660 8496 Mpeg12SliceContext *const s = *(void **) arg;
1661 8496 const uint8_t *buf = s->gb.buffer;
1662 8496 const uint8_t *end = buf + get_bits_bytesize(&s->gb, 0);
1663 8496 int mb_y = s->c.start_mb_y;
1664 8496 const int field_pic = s->c.picture_structure != PICT_FRAME;
1665
1666 8496 s->c.er.error_count = (3 * (s->c.end_mb_y - s->c.start_mb_y) * s->c.mb_width) >> field_pic;
1667
1668 19824 for (;;) {
1669 uint32_t start_code;
1670 int ret;
1671
1672 28320 ret = mpeg_decode_slice(s, mb_y, &buf, end - buf);
1673 ff_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1674 ret, s->c.resync_mb_x, s->c.resync_mb_y, s->c.mb_x, s->c.mb_y,
1675 s->c.start_mb_y, s->c.end_mb_y, s->c.er.error_count);
1676
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 11 taken 28320 times.
28320 if (ret < 0) {
1677
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (c->err_recognition & AV_EF_EXPLODE)
1678 8496 return ret;
1679
0/4
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 12 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 9 → 12 not taken.
✗ if (s->c.resync_mb_x >= 0 && s->c.resync_mb_y >= 0)
1680 ✗ ff_er_add_slice(&s->c.er, s->c.resync_mb_x, s->c.resync_mb_y,
1681 s->c.mb_x, s->c.mb_y,
1682 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
1683 } else {
1684 28320 ff_er_add_slice(&s->c.er, s->c.resync_mb_x, s->c.resync_mb_y,
1685 28320 s->c.mb_x - 1, s->c.mb_y,
1686 ER_AC_END | ER_DC_END | ER_MV_END);
1687 }
1688
1689
2/2
✓ Branch 12 → 13 taken 8496 times.
✓ Branch 12 → 14 taken 19824 times.
28320 if (s->c.mb_y == s->c.end_mb_y)
1690 8496 return 0;
1691
1692 19824 start_code = -1;
1693 19824 buf = avpriv_find_start_code(buf, end, &start_code);
1694
2/4
✓ Branch 15 → 16 taken 19824 times.
✗ Branch 15 → 17 not taken.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 19824 times.
19824 if (start_code < SLICE_MIN_START_CODE || start_code > SLICE_MAX_START_CODE)
1695 ✗ return AVERROR_INVALIDDATA;
1696 19824 mb_y = start_code - SLICE_MIN_START_CODE;
1697
2/4
✓ Branch 18 → 19 taken 19824 times.
✗ Branch 18 → 21 not taken.
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 19824 times.
19824 if (s->c.codec_id != AV_CODEC_ID_MPEG1VIDEO && s->c.mb_height > 2800/16)
1698 ✗ mb_y += (*buf&0xE0)<<2;
1699 19824 mb_y <<= field_pic;
1700
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 19824 times.
19824 if (s->c.picture_structure == PICT_BOTTOM_FIELD)
1701 ✗ mb_y++;
1702
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 19824 times.
19824 if (mb_y >= s->c.end_mb_y)
1703 ✗ return AVERROR_INVALIDDATA;
1704 }
1705 }
1706
1707 /**
1708 * Handle slice ends.
1709 * @return 1 if it seems to be the last slice
1710 */
1711 4459 static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
1712 {
1713 4459 Mpeg1Context *s1 = avctx->priv_data;
1714 4459 MPVContext *const s = &s1->slice.c;
1715
1716
3/4
✓ Branch 2 → 3 taken 4131 times.
✓ Branch 2 → 4 taken 328 times.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 4131 times.
4459 if (!s->context_initialized || !s->cur_pic.ptr)
1717 328 return 0;
1718
1719
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 11 taken 4131 times.
4131 if (s->avctx->hwaccel) {
1720 ✗ int ret = FF_HW_SIMPLE_CALL(s->avctx, end_frame);
1721
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 11 not taken.
✗ if (ret < 0) {
1722 ✗ av_log(avctx, AV_LOG_ERROR,
1723 "hardware accelerator failed to decode picture\n");
1724 ✗ return ret;
1725 }
1726 }
1727
1728 /* end of slice reached */
1729
3/4
✓ Branch 11 → 12 taken 4130 times.
✓ Branch 11 → 32 taken 1 time.
✓ Branch 12 → 13 taken 4130 times.
✗ Branch 12 → 32 not taken.
4131 if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field && !s1->first_slice) {
1730 /* end of image */
1731
1732 4130 ff_er_frame_end(&s->er, NULL);
1733
1734 4130 ff_mpv_frame_end(s);
1735
1736
4/4
✓ Branch 15 → 16 taken 2578 times.
✓ Branch 15 → 17 taken 1552 times.
✓ Branch 16 → 17 taken 561 times.
✓ Branch 16 → 23 taken 2017 times.
4130 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
1737 2113 int ret = av_frame_ref(pict, s->cur_pic.ptr->f);
1738
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 2113 times.
2113 if (ret < 0)
1739 ✗ return ret;
1740 2113 ff_print_debug_info(s, s->cur_pic.ptr, pict);
1741 2113 ff_mpv_export_qp_table(s, pict, s->cur_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG2);
1742 2113 *got_output = 1;
1743 } else {
1744 /* latency of 1 frame for I- and P-frames */
1745
4/4
✓ Branch 23 → 24 taken 1956 times.
✓ Branch 23 → 31 taken 61 times.
✓ Branch 24 → 25 taken 1953 times.
✓ Branch 24 → 31 taken 3 times.
2017 if (s->last_pic.ptr && !s->last_pic.ptr->dummy) {
1746 1953 int ret = av_frame_ref(pict, s->last_pic.ptr->f);
1747
1/2
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 28 taken 1953 times.
1953 if (ret < 0)
1748 ✗ return ret;
1749 1953 ff_print_debug_info(s, s->last_pic.ptr, pict);
1750 1953 ff_mpv_export_qp_table(s, pict, s->last_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG2);
1751 1953 *got_output = 1;
1752 }
1753 }
1754
1755 4130 return 1;
1756 } else {
1757 1 return 0;
1758 }
1759 }
1760
1761 895 static int mpeg1_decode_sequence(AVCodecContext *avctx,
1762 const uint8_t *buf, int buf_size)
1763 {
1764 895 Mpeg1Context *s1 = avctx->priv_data;
1765 895 MPVContext *const s = &s1->slice.c;
1766 895 GetBitContext gb0, *const gb = &gb0;
1767 int width, height;
1768 int i, v, j;
1769
1770 895 int ret = init_get_bits8(gb, buf, buf_size);
1771
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 895 times.
895 if (ret < 0)
1772 ✗ return ret;
1773
1774 895 width = get_bits(gb, 12);
1775 895 height = get_bits(gb, 12);
1776
2/4
✓ Branch 7 → 8 taken 895 times.
✗ Branch 7 → 9 not taken.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 12 taken 895 times.
895 if (width == 0 || height == 0) {
1777 ✗ av_log(avctx, AV_LOG_WARNING,
1778 "Invalid horizontal or vertical size value.\n");
1779
0/2
✗ Branch 10 → 11 not taken.
✗ Branch 10 → 12 not taken.
✗ if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
1780 ✗ return AVERROR_INVALIDDATA;
1781 }
1782 895 s1->aspect_ratio_info = get_bits(gb, 4);
1783
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 17 taken 895 times.
895 if (s1->aspect_ratio_info == 0) {
1784 ✗ av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
1785
0/2
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 17 not taken.
✗ if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
1786 ✗ return AVERROR_INVALIDDATA;
1787 }
1788 895 s1->frame_rate_index = get_bits(gb, 4);
1789
2/4
✓ Branch 18 → 19 taken 895 times.
✗ Branch 18 → 20 not taken.
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 22 taken 895 times.
895 if (s1->frame_rate_index == 0 || s1->frame_rate_index > 13) {
1790 ✗ av_log(avctx, AV_LOG_WARNING,
1791 "frame_rate_index %d is invalid\n", s1->frame_rate_index);
1792 ✗ s1->frame_rate_index = 1;
1793 }
1794 895 s1->bit_rate = get_bits(gb, 18) * 400;
1795
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 895 times.
895 if (check_marker(s->avctx, gb, "in sequence header") == 0) {
1796 ✗ return AVERROR_INVALIDDATA;
1797 }
1798
1799 895 s->avctx->rc_buffer_size = get_bits(gb, 10) * 1024 * 16;
1800 895 skip_bits(gb, 1);
1801
1802 /* get matrix */
1803
2/2
✓ Branch 29 → 30 taken 38 times.
✓ Branch 29 → 31 taken 857 times.
895 if (get_bits1(gb)) {
1804 38 load_matrix(s, gb, s->chroma_intra_matrix, s->intra_matrix, 1);
1805 } else {
1806
2/2
✓ Branch 33 → 32 taken 54848 times.
✓ Branch 33 → 34 taken 857 times.
55705 for (i = 0; i < 64; i++) {
1807 54848 j = s->idsp.idct_permutation[i];
1808 54848 v = ff_mpeg1_default_intra_matrix[i];
1809 54848 s->intra_matrix[j] = v;
1810 54848 s->chroma_intra_matrix[j] = v;
1811 }
1812 }
1813
2/2
✓ Branch 35 → 36 taken 100 times.
✓ Branch 35 → 37 taken 795 times.
895 if (get_bits1(gb)) {
1814 100 load_matrix(s, gb, s->chroma_inter_matrix, s->inter_matrix, 0);
1815 } else {
1816
2/2
✓ Branch 39 → 38 taken 50880 times.
✓ Branch 39 → 40 taken 795 times.
51675 for (i = 0; i < 64; i++) {
1817 50880 int j = s->idsp.idct_permutation[i];
1818 50880 v = ff_mpeg1_default_non_intra_matrix[i];
1819 50880 s->inter_matrix[j] = v;
1820 50880 s->chroma_inter_matrix[j] = v;
1821 }
1822 }
1823
1824
1/2
✗ Branch 41 → 42 not taken.
✓ Branch 41 → 44 taken 895 times.
895 if (show_bits(gb, 23) != 0) {
1825 ✗ av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
1826 ✗ return AVERROR_INVALIDDATA;
1827 }
1828
1829 895 s->width = width;
1830 895 s->height = height;
1831
1832 /* We set MPEG-2 parameters so that it emulates MPEG-1. */
1833 895 s->progressive_sequence = 1;
1834 895 s->progressive_frame = 1;
1835 895 s->picture_structure = PICT_FRAME;
1836 895 s->first_field = 0;
1837 895 s->frame_pred_frame_dct = 1;
1838 895 s->chroma_format = CHROMA_420;
1839 895 s->codec_id =
1840 895 s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
1841
1/2
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 46 taken 895 times.
895 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
1842 ✗ s->low_delay = 1;
1843
1844
1/2
✗ Branch 46 → 47 not taken.
✓ Branch 46 → 48 taken 895 times.
895 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1845 ✗ av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%"PRId64", aspect_ratio_info: %d \n",
1846 ✗ s->avctx->rc_buffer_size, s1->bit_rate, s1->aspect_ratio_info);
1847
1848 895 return 0;
1849 }
1850
1851 2 static int vcr2_init_sequence(AVCodecContext *avctx)
1852 {
1853 2 Mpeg1Context *s1 = avctx->priv_data;
1854 2 MPVContext *const s = &s1->slice.c;
1855 int i, v, ret;
1856
1857 /* start new MPEG-1 context decoding */
1858
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2 times.
2 if (s->context_initialized)
1859 ✗ ff_mpv_common_end(s);
1860
1861 2 s->width = avctx->coded_width;
1862 2 s->height = avctx->coded_height;
1863 2 avctx->has_b_frames = 0; // true?
1864 2 s->low_delay = 1;
1865
1866 2 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1867
1868
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 2 times.
2 if ((ret = ff_mpv_common_init(s)) < 0)
1869 ✗ return ret;
1870
1/2
✓ Branch 8 → 9 taken 2 times.
✗ Branch 8 → 13 not taken.
2 if (!s->avctx->lowres)
1871
2/2
✓ Branch 12 → 10 taken 2 times.
✓ Branch 12 → 13 taken 2 times.
4 for (int i = 0; i < s->slice_context_count; i++)
1872 2 ff_mpv_framesize_disable(&s->thread_context[i]->sc);
1873
1874
2/2
✓ Branch 15 → 14 taken 128 times.
✓ Branch 15 → 16 taken 2 times.
130 for (i = 0; i < 64; i++) {
1875 128 int j = s->idsp.idct_permutation[i];
1876 128 v = ff_mpeg1_default_intra_matrix[i];
1877 128 s->intra_matrix[j] = v;
1878 128 s->chroma_intra_matrix[j] = v;
1879
1880 128 v = ff_mpeg1_default_non_intra_matrix[i];
1881 128 s->inter_matrix[j] = v;
1882 128 s->chroma_inter_matrix[j] = v;
1883 }
1884
1885 2 s->progressive_sequence = 1;
1886 2 s->progressive_frame = 1;
1887 2 s->picture_structure = PICT_FRAME;
1888 2 s->first_field = 0;
1889 2 s->frame_pred_frame_dct = 1;
1890 2 s->chroma_format = CHROMA_420;
1891
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 2 times.
2 if (s->codec_tag == AV_RL32("BW10")) {
1892 ✗ s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
1893 } else {
1894 2 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1895 }
1896 2 s1->save_progressive_seq = s->progressive_sequence;
1897 2 s1->save_chroma_format = s->chroma_format;
1898 2 return 0;
1899 }
1900
1901 963 static void mpeg_set_cc_format(AVCodecContext *avctx, enum Mpeg2ClosedCaptionsFormat format,
1902 const char *label)
1903 {
1904 963 Mpeg1Context *s1 = avctx->priv_data;
1905
1906 av_assert2(format != CC_FORMAT_AUTO);
1907
1908
2/2
✓ Branch 2 → 3 taken 13 times.
✓ Branch 2 → 4 taken 950 times.
963 if (!s1->cc_format) {
1909 13 s1->cc_format = format;
1910
1911 13 av_log(avctx, AV_LOG_DEBUG, "CC: first seen substream is %s format\n", label);
1912 }
1913 963 }
1914
1915 1039 static int mpeg_decode_a53_cc(AVCodecContext *avctx,
1916 const uint8_t *p, int buf_size)
1917 {
1918 1039 Mpeg1Context *s1 = avctx->priv_data;
1919
1920
5/6
✓ Branch 2 → 3 taken 950 times.
✓ Branch 2 → 4 taken 89 times.
✓ Branch 3 → 4 taken 544 times.
✓ Branch 3 → 23 taken 406 times.
✓ Branch 4 → 5 taken 633 times.
✗ Branch 4 → 23 not taken.
1039 if ((!s1->cc_format || s1->cc_format == CC_FORMAT_A53_PART4) &&
1921 633 buf_size >= 6 &&
1922
5/8
✓ Branch 5 → 6 taken 553 times.
✓ Branch 5 → 23 taken 80 times.
✓ Branch 6 → 7 taken 553 times.
✗ Branch 6 → 23 not taken.
✓ Branch 7 → 8 taken 553 times.
✗ Branch 7 → 23 not taken.
✓ Branch 8 → 9 taken 553 times.
✗ Branch 8 → 23 not taken.
633 p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
1923
2/4
✓ Branch 9 → 10 taken 553 times.
✗ Branch 9 → 23 not taken.
✓ Branch 10 → 11 taken 553 times.
✗ Branch 10 → 23 not taken.
553 p[4] == 3 && (p[5] & 0x40)) {
1924 /* extract A53 Part 4 CC data */
1925 553 int cc_count = p[5] & 0x1f;
1926
2/4
✓ Branch 11 → 12 taken 553 times.
✗ Branch 11 → 22 not taken.
✓ Branch 12 → 13 taken 553 times.
✗ Branch 12 → 22 not taken.
553 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
1927
2/2
✓ Branch 13 → 14 taken 8 times.
✓ Branch 13 → 15 taken 545 times.
553 int old_size = s1->a53_buf_ref ? s1->a53_buf_ref->size : 0;
1928 553 const uint64_t new_size = (old_size + cc_count
1929 553 * UINT64_C(3));
1930 int ret;
1931
1932
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 553 times.
553 if (new_size > 3*A53_MAX_CC_COUNT)
1933 ✗ return AVERROR(EINVAL);
1934
1935 553 ret = av_buffer_realloc(&s1->a53_buf_ref, new_size);
1936
1/2
✓ Branch 19 → 20 taken 553 times.
✗ Branch 19 → 21 not taken.
553 if (ret >= 0)
1937 553 memcpy(s1->a53_buf_ref->data + old_size, p + 7, cc_count * UINT64_C(3));
1938
1939 553 mpeg_set_cc_format(avctx, CC_FORMAT_A53_PART4, "A/53 Part 4");
1940 }
1941 553 return 1;
1942
4/6
✓ Branch 23 → 24 taken 406 times.
✓ Branch 23 → 25 taken 80 times.
✓ Branch 24 → 25 taken 406 times.
✗ Branch 24 → 59 not taken.
✓ Branch 25 → 26 taken 486 times.
✗ Branch 25 → 59 not taken.
486 } else if ((!s1->cc_format || s1->cc_format == CC_FORMAT_SCTE20) &&
1943 486 buf_size >= 2 &&
1944
3/4
✓ Branch 26 → 27 taken 410 times.
✓ Branch 26 → 59 taken 76 times.
✓ Branch 27 → 28 taken 410 times.
✗ Branch 27 → 59 not taken.
486 p[0] == 0x03 && (p[1]&0x7f) == 0x01) {
1945 /* extract SCTE-20 CC data */
1946 GetBitContext gb;
1947 410 int cc_count = 0;
1948 int i, ret;
1949
1950 410 ret = init_get_bits8(&gb, p + 2, buf_size - 2);
1951
1/2
✗ Branch 29 → 30 not taken.
✓ Branch 29 → 31 taken 410 times.
410 if (ret < 0)
1952 ✗ return ret;
1953 410 cc_count = get_bits(&gb, 5);
1954
1/2
✓ Branch 32 → 33 taken 410 times.
✗ Branch 32 → 57 not taken.
410 if (cc_count > 0) {
1955
2/2
✓ Branch 33 → 34 taken 4 times.
✓ Branch 33 → 35 taken 406 times.
410 int old_size = s1->a53_buf_ref ? s1->a53_buf_ref->size : 0;
1956 410 const uint64_t new_size = (old_size + cc_count
1957 410 * UINT64_C(3));
1958
1/2
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 38 taken 410 times.
410 if (new_size > 3*A53_MAX_CC_COUNT)
1959 ✗ return AVERROR(EINVAL);
1960
1961 410 ret = av_buffer_realloc(&s1->a53_buf_ref, new_size);
1962
1/2
✓ Branch 39 → 40 taken 410 times.
✗ Branch 39 → 56 not taken.
410 if (ret >= 0) {
1963 uint8_t field, cc1, cc2;
1964 410 uint8_t *cap = s1->a53_buf_ref->data + old_size;
1965
1966 410 memset(cap, 0, cc_count * 3);
1967
3/4
✓ Branch 53 → 54 taken 820 times.
✓ Branch 53 → 56 taken 410 times.
✓ Branch 55 → 41 taken 820 times.
✗ Branch 55 → 56 not taken.
1230 for (i = 0; i < cc_count && get_bits_left(&gb) >= 26; i++) {
1968 820 skip_bits(&gb, 2); // priority
1969 820 field = get_bits(&gb, 2);
1970 820 skip_bits(&gb, 5); // line_offset
1971 820 cc1 = get_bits(&gb, 8);
1972 820 cc2 = get_bits(&gb, 8);
1973 820 skip_bits(&gb, 1); // marker
1974
1975
1/2
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 820 times.
820 if (!field) { // forbidden
1976 ✗ cap[0] = cap[1] = cap[2] = 0x00;
1977 } else {
1978 820 field = (field == 2 ? 1 : 0);
1979
1/2
✗ Branch 49 → 50 not taken.
✓ Branch 49 → 51 taken 820 times.
820 if (!s1->slice.c.top_field_first) field = !field;
1980 820 cap[0] = 0x04 | field;
1981 820 cap[1] = ff_reverse[cc1];
1982 820 cap[2] = ff_reverse[cc2];
1983 }
1984 820 cap += 3;
1985 }
1986 }
1987
1988 410 mpeg_set_cc_format(avctx, CC_FORMAT_SCTE20, "SCTE-20");
1989 }
1990 410 return 1;
1991
2/6
✗ Branch 59 → 60 not taken.
✓ Branch 59 → 61 taken 76 times.
✗ Branch 60 → 61 not taken.
✗ Branch 60 → 91 not taken.
✓ Branch 61 → 62 taken 76 times.
✗ Branch 61 → 91 not taken.
76 } else if ((!s1->cc_format || s1->cc_format == CC_FORMAT_DVD) &&
1992 76 buf_size >= 11 &&
1993
1/8
✗ Branch 62 → 63 not taken.
✓ Branch 62 → 91 taken 76 times.
✗ Branch 63 → 64 not taken.
✗ Branch 63 → 91 not taken.
✗ Branch 64 → 65 not taken.
✗ Branch 64 → 91 not taken.
✗ Branch 65 → 66 not taken.
✗ Branch 65 → 91 not taken.
76 p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
1994 /* extract DVD CC data
1995 *
1996 * uint32_t user_data_start_code 0x000001B2 (big endian)
1997 * uint16_t user_identifier 0x4343 "CC"
1998 * uint8_t user_data_type_code 0x01
1999 * uint8_t caption_block_size 0xF8
2000 * uint8_t
2001 * bit 7 caption_odd_field_first 1=odd field (CC1/CC2) first 0=even field (CC3/CC4) first
2002 * bit 6 caption_filler 0
2003 * bit 5:1 caption_block_count number of caption blocks (pairs of caption words = frames). Most DVDs use 15 per start of GOP.
2004 * bit 0 caption_extra_field_added 1=one additional caption word
2005 *
2006 * struct caption_field_block {
2007 * uint8_t
2008 * bit 7:1 caption_filler 0x7F (all 1s)
2009 * bit 0 caption_field_odd 1=odd field (this is CC1/CC2) 0=even field (this is CC3/CC4)
2010 * uint8_t caption_first_byte
2011 * uint8_t caption_second_byte
2012 * } caption_block[(caption_block_count * 2) + caption_extra_field_added];
2013 *
2014 * Some DVDs encode caption data for both fields with caption_field_odd=1. The only way to decode the fields
2015 * correctly is to start on the field indicated by caption_odd_field_first and count between odd/even fields.
2016 * Don't assume that the first caption word is the odd field. There do exist MPEG files in the wild that start
2017 * on the even field. There also exist DVDs in the wild that encode an odd field count and the
2018 * caption_extra_field_added/caption_odd_field_first bits change per packet to allow that. */
2019 ✗ int cc_count = 0;
2020 int i, ret;
2021 // There is a caption count field in the data, but it is often
2022 // incorrect. So count the number of captions present.
2023
0/4
✗ Branch 68 → 69 not taken.
✗ Branch 68 → 70 not taken.
✗ Branch 69 → 67 not taken.
✗ Branch 69 → 70 not taken.
✗ for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2024 ✗ cc_count++;
2025 // Transform the DVD format into A53 Part 4 format
2026
0/2
✗ Branch 70 → 71 not taken.
✗ Branch 70 → 90 not taken.
✗ if (cc_count > 0) {
2027
0/2
✗ Branch 71 → 72 not taken.
✗ Branch 71 → 73 not taken.
✗ int old_size = s1->a53_buf_ref ? s1->a53_buf_ref->size : 0;
2028 ✗ const uint64_t new_size = (old_size + cc_count
2029 ✗ * UINT64_C(6));
2030
0/2
✗ Branch 74 → 75 not taken.
✗ Branch 74 → 76 not taken.
✗ if (new_size > 3*A53_MAX_CC_COUNT)
2031 ✗ return AVERROR(EINVAL);
2032
2033 ✗ ret = av_buffer_realloc(&s1->a53_buf_ref, new_size);
2034
0/2
✗ Branch 77 → 78 not taken.
✗ Branch 77 → 89 not taken.
✗ if (ret >= 0) {
2035 ✗ uint8_t field1 = !!(p[4] & 0x80);
2036 ✗ uint8_t *cap = s1->a53_buf_ref->data + old_size;
2037 ✗ p += 5;
2038
0/2
✗ Branch 88 → 79 not taken.
✗ Branch 88 → 89 not taken.
✗ for (i = 0; i < cc_count; i++) {
2039
0/4
✗ Branch 79 → 80 not taken.
✗ Branch 79 → 82 not taken.
✗ Branch 80 → 81 not taken.
✗ Branch 80 → 82 not taken.
✗ cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2040 ✗ cap[1] = p[1];
2041 ✗ cap[2] = p[2];
2042
0/4
✗ Branch 83 → 84 not taken.
✗ Branch 83 → 86 not taken.
✗ Branch 84 → 85 not taken.
✗ Branch 84 → 86 not taken.
✗ cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2043 ✗ cap[4] = p[4];
2044 ✗ cap[5] = p[5];
2045 ✗ cap += 6;
2046 ✗ p += 6;
2047 }
2048 }
2049
2050 ✗ mpeg_set_cc_format(avctx, CC_FORMAT_DVD, "DVD");
2051 }
2052 ✗ return 1;
2053
2/6
✗ Branch 91 → 92 not taken.
✓ Branch 91 → 93 taken 76 times.
✗ Branch 92 → 93 not taken.
✗ Branch 92 → 121 not taken.
✓ Branch 93 → 94 taken 76 times.
✗ Branch 93 → 121 not taken.
76 } else if ((!s1->cc_format || s1->cc_format == CC_FORMAT_DISH) &&
2054 76 buf_size >= 12 &&
2055
1/4
✗ Branch 94 → 95 not taken.
✓ Branch 94 → 121 taken 76 times.
✗ Branch 95 → 96 not taken.
✗ Branch 95 → 121 not taken.
76 p[0] == 0x05 && p[1] == 0x02) {
2056 /* extract Dish Network CC data */
2057 ✗ const uint8_t cc_header = 0xf8 | 0x04 /* valid */ | 0x00 /* line 21 field 1 */;
2058 ✗ uint8_t cc_data[4] = {0};
2059 ✗ int cc_count = 0;
2060 ✗ uint8_t cc_type = p[7];
2061 ✗ p += 8;
2062 ✗ buf_size -= 8;
2063
2064
0/4
✗ Branch 96 → 97 not taken.
✗ Branch 96 → 99 not taken.
✗ Branch 97 → 98 not taken.
✗ Branch 97 → 99 not taken.
✗ if (cc_type == 0x05 && buf_size >= 7) {
2065 ✗ cc_type = p[6];
2066 ✗ p += 7;
2067 ✗ buf_size -= 7;
2068 }
2069
2070
0/4
✗ Branch 99 → 100 not taken.
✗ Branch 99 → 105 not taken.
✗ Branch 100 → 101 not taken.
✗ Branch 100 → 105 not taken.
✗ if (cc_type == 0x02 && buf_size >= 4) { /* 2-byte caption, can be repeated */
2071 ✗ cc_count = 1;
2072 ✗ cc_data[0] = p[1];
2073 ✗ cc_data[1] = p[2];
2074 ✗ cc_type = p[3];
2075
2076 /* Only repeat characters when the next type flag
2077 * is 0x04 and the characters are repeatable (i.e., less than
2078 * 32 with the parity stripped).
2079 */
2080
0/4
✗ Branch 101 → 102 not taken.
✗ Branch 101 → 104 not taken.
✗ Branch 102 → 103 not taken.
✗ Branch 102 → 104 not taken.
✗ if (cc_type == 0x04 && (cc_data[0] & 0x7f) < 32) {
2081 ✗ cc_count = 2;
2082 ✗ cc_data[2] = cc_data[0];
2083 ✗ cc_data[3] = cc_data[1];
2084 }
2085
0/4
✗ Branch 105 → 106 not taken.
✗ Branch 105 → 108 not taken.
✗ Branch 106 → 107 not taken.
✗ Branch 106 → 108 not taken.
✗ } else if (cc_type == 0x04 && buf_size >= 5) { /* 4-byte caption, not repeated */
2086 ✗ cc_count = 2;
2087 ✗ cc_data[0] = p[1];
2088 ✗ cc_data[1] = p[2];
2089 ✗ cc_data[2] = p[3];
2090 ✗ cc_data[3] = p[4];
2091 }
2092
2093
0/2
✗ Branch 108 → 109 not taken.
✗ Branch 108 → 119 not taken.
✗ if (cc_count > 0) {
2094 int ret;
2095
0/2
✗ Branch 109 → 110 not taken.
✗ Branch 109 → 111 not taken.
✗ int old_size = s1->a53_buf_ref ? s1->a53_buf_ref->size : 0;
2096 ✗ const uint64_t new_size = (old_size + cc_count * UINT64_C(3));
2097
0/2
✗ Branch 112 → 113 not taken.
✗ Branch 112 → 114 not taken.
✗ if (new_size > 3 * A53_MAX_CC_COUNT)
2098 ✗ return AVERROR(EINVAL);
2099
2100 ✗ ret = av_buffer_realloc(&s1->a53_buf_ref, new_size);
2101
0/2
✗ Branch 115 → 116 not taken.
✗ Branch 115 → 118 not taken.
✗ if (ret >= 0) {
2102 ✗ uint8_t *cap = s1->a53_buf_ref->data + old_size;
2103 ✗ cap[0] = cc_header;
2104 ✗ cap[1] = cc_data[0];
2105 ✗ cap[2] = cc_data[1];
2106
0/2
✗ Branch 116 → 117 not taken.
✗ Branch 116 → 118 not taken.
✗ if (cc_count == 2) {
2107 ✗ cap[3] = cc_header;
2108 ✗ cap[4] = cc_data[2];
2109 ✗ cap[5] = cc_data[3];
2110 }
2111 }
2112
2113 ✗ mpeg_set_cc_format(avctx, CC_FORMAT_DISH, "Dish Network");
2114 }
2115 ✗ return 1;
2116 }
2117 76 return 0;
2118 }
2119
2120 1451 static void mpeg_decode_user_data(AVCodecContext *avctx,
2121 const uint8_t *p, int buf_size)
2122 {
2123 1451 const uint8_t *buf_end = p + buf_size;
2124 1451 Mpeg1Context *s1 = avctx->priv_data;
2125
2126 #if 0
2127 int i;
2128 for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
2129 av_log(avctx, AV_LOG_ERROR, "%c", p[i]);
2130 }
2131 av_log(avctx, AV_LOG_ERROR, "\n");
2132 #endif
2133
2134
1/2
✓ Branch 2 → 3 taken 1451 times.
✗ Branch 2 → 8 not taken.
1451 if (buf_size > 29){
2135 int i;
2136
2/2
✓ Branch 7 → 4 taken 29020 times.
✓ Branch 7 → 8 taken 1451 times.
30471 for(i=0; i<20; i++)
2137
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 29020 times.
29020 if (!memcmp(p+i, "\0TMPGEXS\0", 9)){
2138 ✗ s1->tmpgexs = 1;
2139 }
2140 }
2141 /* we parse the DTG active format information */
2142
1/2
✓ Branch 8 → 9 taken 1451 times.
✗ Branch 8 → 20 not taken.
1451 if (buf_end - p >= 5 &&
2143
5/8
✓ Branch 9 → 10 taken 412 times.
✓ Branch 9 → 20 taken 1039 times.
✓ Branch 10 → 11 taken 412 times.
✗ Branch 10 → 20 not taken.
✓ Branch 11 → 12 taken 412 times.
✗ Branch 11 → 20 not taken.
✓ Branch 12 → 13 taken 412 times.
✗ Branch 12 → 20 not taken.
1863 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2144 412 int flags = p[4];
2145 412 p += 5;
2146
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 412 times.
412 if (flags & 0x80) {
2147 /* skip event id */
2148 ✗ p += 2;
2149 }
2150
1/2
✓ Branch 15 → 16 taken 412 times.
✗ Branch 15 → 19 not taken.
412 if (flags & 0x40) {
2151
1/2
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 412 times.
412 if (buf_end - p < 1)
2152 ✗ return;
2153 412 s1->has_afd = 1;
2154 412 s1->afd = p[0] & 0x0f;
2155 }
2156
1/2
✓ Branch 20 → 21 taken 1039 times.
✗ Branch 20 → 36 not taken.
1039 } else if (buf_end - p >= 6 &&
2157
1/8
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 36 taken 1039 times.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 36 not taken.
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 36 not taken.
✗ Branch 24 → 25 not taken.
✗ Branch 24 → 36 not taken.
1039 p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
2158
0/2
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 36 not taken.
✗ p[4] == 0x03) { // S3D_video_format_length
2159 // the 0x7F mask ignores the reserved_bit value
2160 ✗ const uint8_t S3D_video_format_type = p[5] & 0x7F;
2161
2162
0/4
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 30 not taken.
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 30 not taken.
✗ if (S3D_video_format_type == 0x03 ||
2163
0/2
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 30 not taken.
✗ S3D_video_format_type == 0x04 ||
2164
0/2
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 35 not taken.
✗ S3D_video_format_type == 0x08 ||
2165 S3D_video_format_type == 0x23) {
2166
2167 ✗ s1->has_stereo3d = 1;
2168
2169
0/5
✗ Branch 30 → 31 not taken.
✗ Branch 30 → 32 not taken.
✗ Branch 30 → 33 not taken.
✗ Branch 30 → 34 not taken.
✗ Branch 30 → 35 not taken.
✗ switch (S3D_video_format_type) {
2170 ✗ case 0x03:
2171 ✗ s1->stereo3d_type = AV_STEREO3D_SIDEBYSIDE;
2172 ✗ break;
2173 ✗ case 0x04:
2174 ✗ s1->stereo3d_type = AV_STEREO3D_TOPBOTTOM;
2175 ✗ break;
2176 ✗ case 0x08:
2177 ✗ s1->stereo3d_type = AV_STEREO3D_2D;
2178 ✗ break;
2179 ✗ case 0x23:
2180 ✗ s1->stereo3d_type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2181 ✗ break;
2182 }
2183 }
2184
2/2
✓ Branch 37 → 38 taken 963 times.
✓ Branch 37 → 39 taken 76 times.
1039 } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
2185 963 return;
2186 }
2187 }
2188
2189 606 static int mpeg_decode_gop(AVCodecContext *avctx,
2190 const uint8_t *buf, int buf_size)
2191 {
2192 606 Mpeg1Context *s1 = avctx->priv_data;
2193 606 MPVContext *const s = &s1->slice.c;
2194 606 GetBitContext gb0, *const gb = &gb0;
2195 int broken_link;
2196 int64_t tc;
2197
2198 606 int ret = init_get_bits8(gb, buf, buf_size);
2199
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 606 times.
606 if (ret < 0)
2200 ✗ return ret;
2201
2202 606 tc = s1->timecode_frame_start = get_bits(gb, 25);
2203
2204 606 s1->closed_gop = get_bits1(gb);
2205 /* broken_link indicates that after editing the
2206 * reference frames of the first B-Frames after GOP I-Frame
2207 * are missing (open gop) */
2208 606 broken_link = get_bits1(gb);
2209
2210
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 12 taken 606 times.
606 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2211 char tcbuf[AV_TIMECODE_STR_SIZE];
2212 ✗ av_timecode_make_mpeg_tc_string(tcbuf, tc);
2213 ✗ av_log(s->avctx, AV_LOG_DEBUG,
2214 "GOP (%s) closed_gop=%d broken_link=%d\n",
2215 tcbuf, s1->closed_gop, broken_link);
2216 }
2217
2218 606 return 0;
2219 }
2220
2221 4459 static void mpeg12_execute_slice_threads(AVCodecContext *avctx,
2222 Mpeg1Context *const s)
2223 {
2224
2/2
✓ Branch 2 → 3 taken 950 times.
✓ Branch 2 → 11 taken 3509 times.
4459 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
2225
1/2
✓ Branch 3 → 4 taken 950 times.
✗ Branch 3 → 11 not taken.
950 !avctx->hwaccel) {
2226 950 MPVContext *const s2 = &s->slice.c;
2227 950 int error_count = 0;
2228
2229 950 avctx->execute(avctx, slice_decode_thread,
2230 950 s2->mpeg12_contexts, NULL,
2231 s->slice_count, sizeof(s2->mpeg12_contexts[0]));
2232
2233
2/2
✓ Branch 9 → 6 taken 8496 times.
✓ Branch 9 → 10 taken 950 times.
9446 for (int i = 0; i < s->slice_count; i++) {
2234 8496 MpegEncContext *const slice = s2->thread_context[i];
2235 8496 int slice_err = atomic_load_explicit(&slice->er.error_count,
2236 memory_order_relaxed);
2237 // error_count can get set to INT_MAX on serious errors.
2238 // So use saturated addition.
2239
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 8496 times.
8496 if ((unsigned)slice_err > INT_MAX - error_count) {
2240 ✗ error_count = INT_MAX;
2241 ✗ break;
2242 }
2243 8496 error_count += slice_err;
2244 }
2245 950 atomic_store_explicit(&s2->er.error_count, error_count,
2246 memory_order_relaxed);
2247 }
2248 4459 }
2249
2250 5005 static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
2251 int *got_output, const uint8_t *buf, int buf_size)
2252 {
2253 5005 Mpeg1Context *s = avctx->priv_data;
2254 5005 MPVContext *const s2 = &s->slice.c;
2255 5005 const uint8_t *buf_ptr = buf;
2256 5005 const uint8_t *buf_end = buf + buf_size;
2257 int ret, input_size;
2258 5005 int last_code = 0, skip_frame = 0;
2259 5005 int picture_start_code_seen = 0;
2260
2261 239474 for (;;) {
2262 /* find next start code */
2263 244479 uint32_t start_code = -1;
2264 244479 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
2265
2/2
✓ Branch 4 → 5 taken 4826 times.
✓ Branch 4 → 14 taken 239653 times.
244479 if (start_code > 0x1ff) {
2266
2/2
✓ Branch 5 → 6 taken 4459 times.
✓ Branch 5 → 10 taken 367 times.
4826 if (!skip_frame) {
2267 4459 mpeg12_execute_slice_threads(avctx, s);
2268
2269 4459 ret = slice_end(avctx, picture, got_output);
2270
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 4459 times.
4459 if (ret < 0)
2271 5005 return ret;
2272 }
2273 4826 s2->pict_type = 0;
2274
2275
1/4
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 13 taken 4826 times.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
4826 if (avctx->err_recognition & AV_EF_EXPLODE && s2->er.error_count)
2276 ✗ return AVERROR_INVALIDDATA;
2277
2278 4826 return FFMAX(0, buf_ptr - buf);
2279 }
2280
2281 239653 input_size = buf_end - buf_ptr;
2282
2283
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 239653 times.
239653 if (avctx->debug & FF_DEBUG_STARTCODE)
2284 ✗ av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %td left %d\n",
2285 start_code, buf_ptr - buf, input_size);
2286
2287 /* prepare data for next start code */
2288
6/6
✓ Branch 16 → 17 taken 895 times.
✓ Branch 16 → 25 taken 5550 times.
✓ Branch 16 → 58 taken 6915 times.
✓ Branch 16 → 86 taken 1451 times.
✓ Branch 16 → 88 taken 606 times.
✓ Branch 16 → 97 taken 224236 times.
239653 switch (start_code) {
2289 895 case SEQ_START_CODE:
2290
1/2
✓ Branch 17 → 18 taken 895 times.
✗ Branch 17 → 21 not taken.
895 if (last_code == 0) {
2291 895 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2292
2/2
✓ Branch 19 → 20 taken 597 times.
✓ Branch 19 → 24 taken 298 times.
895 if (buf != avctx->extradata)
2293 597 s->sync = 1;
2294 } else {
2295 ✗ av_log(avctx, AV_LOG_ERROR,
2296 "ignoring SEQ_START_CODE after %X\n", last_code);
2297
0/2
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
✗ if (avctx->err_recognition & AV_EF_EXPLODE)
2298 ✗ return AVERROR_INVALIDDATA;
2299 }
2300 895 break;
2301
2302 5550 case PICTURE_START_CODE:
2303
3/4
✓ Branch 25 → 26 taken 873 times.
✓ Branch 25 → 29 taken 4677 times.
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 29 taken 873 times.
5550 if (picture_start_code_seen && s2->picture_structure == PICT_FRAME) {
2304 /* If it's a frame picture, there can't be more than one picture header.
2305 Yet, it does happen and we need to handle it. */
2306 ✗ av_log(avctx, AV_LOG_WARNING, "ignoring extra picture following a frame-picture\n");
2307 ✗ break;
2308 }
2309 5550 picture_start_code_seen = 1;
2310
2311
1/4
✗ Branch 29 → 30 not taken.
✓ Branch 29 → 33 taken 5550 times.
✗ Branch 30 → 31 not taken.
✗ Branch 30 → 33 not taken.
5550 if (buf == avctx->extradata && avctx->codec_tag == AV_RL32("AVmp")) {
2312 ✗ av_log(avctx, AV_LOG_WARNING, "ignoring picture start code in AVmp extradata\n");
2313 ✗ break;
2314 }
2315
1/2
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 5550 times.
5550 if (buf == avctx->extradata)
2316 ✗ return AVERROR_INVALIDDATA;
2317
2318
3/4
✓ Branch 35 → 36 taken 5371 times.
✓ Branch 35 → 37 taken 179 times.
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 39 taken 5371 times.
5550 if (s2->width <= 0 || s2->height <= 0) {
2319 179 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
2320 s2->width, s2->height);
2321 179 return AVERROR_INVALIDDATA;
2322 }
2323
2324
1/2
✗ Branch 39 → 40 not taken.
✓ Branch 39 → 41 taken 5371 times.
5371 if (s->tmpgexs){
2325 ✗ s2->intra_dc_precision= 3;
2326 ✗ s2->intra_matrix[0]= 1;
2327 }
2328
1/2
✗ Branch 41 → 42 not taken.
✓ Branch 41 → 44 taken 5371 times.
5371 if (s->slice_count) {
2329 ✗ mpeg12_execute_slice_threads(avctx, s);
2330 ✗ s->slice_count = 0;
2331 }
2332
3/4
✓ Branch 44 → 45 taken 873 times.
✓ Branch 44 → 46 taken 4498 times.
✓ Branch 45 → 46 taken 873 times.
✗ Branch 45 → 54 not taken.
5371 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2333 5371 ret = mpeg_decode_postinit(avctx);
2334
1/2
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 50 taken 5371 times.
5371 if (ret < 0) {
2335 ✗ av_log(avctx, AV_LOG_ERROR,
2336 "mpeg_decode_postinit() failure\n");
2337 ✗ return ret;
2338 }
2339
2340 /* We have a complete image: we try to decompress it. */
2341
1/2
✗ Branch 51 → 52 not taken.
✓ Branch 51 → 53 taken 5371 times.
5371 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2342 ✗ s2->pict_type = 0;
2343 5371 s->first_slice = 1;
2344 5371 last_code = PICTURE_START_CODE;
2345 } else {
2346 ✗ av_log(avctx, AV_LOG_ERROR,
2347 "ignoring pic after %X\n", last_code);
2348
0/2
✗ Branch 55 → 56 not taken.
✗ Branch 55 → 57 not taken.
✗ if (avctx->err_recognition & AV_EF_EXPLODE)
2349 ✗ return AVERROR_INVALIDDATA;
2350 }
2351 5371 break;
2352 6915 case EXT_START_CODE: {
2353 6915 GetBitContext gb0, *const gb = &gb0;
2354
2355 6915 ret = init_get_bits8(gb, buf_ptr, input_size);
2356
1/2
✗ Branch 59 → 60 not taken.
✓ Branch 59 → 61 taken 6915 times.
6915 if (ret < 0)
2357 ✗ return ret;
2358
2359
5/6
✓ Branch 62 → 63 taken 803 times.
✓ Branch 62 → 69 taken 151 times.
✓ Branch 62 → 71 taken 1053 times.
✓ Branch 62 → 73 taken 31 times.
✓ Branch 62 → 75 taken 4877 times.
✗ Branch 62 → 83 not taken.
6915 switch (get_bits(gb, 4)) {
2360 803 case 0x1:
2361
1/2
✓ Branch 63 → 64 taken 803 times.
✗ Branch 63 → 65 not taken.
803 if (last_code == 0) {
2362 803 mpeg_decode_sequence_extension(s, gb);
2363 } else {
2364 ✗ av_log(avctx, AV_LOG_ERROR,
2365 "ignoring seq ext after %X\n", last_code);
2366
0/2
✗ Branch 66 → 67 not taken.
✗ Branch 66 → 68 not taken.
✗ if (avctx->err_recognition & AV_EF_EXPLODE)
2367 ✗ return AVERROR_INVALIDDATA;
2368 }
2369 803 break;
2370 151 case 0x2:
2371 151 mpeg_decode_sequence_display_extension(s, gb);
2372 151 break;
2373 1053 case 0x3:
2374 1053 mpeg_decode_quant_matrix_extension(s2, gb);
2375 1053 break;
2376 31 case 0x7:
2377 31 mpeg_decode_picture_display_extension(s, gb);
2378 31 break;
2379 4877 case 0x8:
2380
1/2
✓ Branch 75 → 76 taken 4877 times.
✗ Branch 75 → 79 not taken.
4877 if (last_code == PICTURE_START_CODE) {
2381 4877 int ret = mpeg_decode_picture_coding_extension(s, gb);
2382
1/2
✗ Branch 77 → 78 not taken.
✓ Branch 77 → 82 taken 4877 times.
4877 if (ret < 0)
2383 ✗ return ret;
2384 } else {
2385 ✗ av_log(avctx, AV_LOG_ERROR,
2386 "ignoring pic cod ext after %X\n", last_code);
2387
0/2
✗ Branch 80 → 81 not taken.
✗ Branch 80 → 82 not taken.
✗ if (avctx->err_recognition & AV_EF_EXPLODE)
2388 ✗ return AVERROR_INVALIDDATA;
2389 }
2390 4877 break;
2391 }
2392 6915 break;
2393 }
2394 1451 case USER_START_CODE:
2395 1451 mpeg_decode_user_data(avctx, buf_ptr, input_size);
2396 1451 break;
2397 606 case GOP_START_CODE:
2398
1/2
✓ Branch 88 → 89 taken 606 times.
✗ Branch 88 → 93 not taken.
606 if (last_code == 0) {
2399 606 s2->first_field = 0;
2400 606 ret = mpeg_decode_gop(avctx, buf_ptr, input_size);
2401
1/2
✗ Branch 90 → 91 not taken.
✓ Branch 90 → 92 taken 606 times.
606 if (ret < 0)
2402 ✗ return ret;
2403 606 s->sync = 1;
2404 } else {
2405 ✗ av_log(avctx, AV_LOG_ERROR,
2406 "ignoring GOP_START_CODE after %X\n", last_code);
2407
0/2
✗ Branch 94 → 95 not taken.
✗ Branch 94 → 96 not taken.
✗ if (avctx->err_recognition & AV_EF_EXPLODE)
2408 ✗ return AVERROR_INVALIDDATA;
2409 }
2410 606 break;
2411 224236 default:
2412
2/2
✓ Branch 97 → 98 taken 223313 times.
✓ Branch 97 → 114 taken 923 times.
224236 if (start_code >= SLICE_MIN_START_CODE &&
2413
4/4
✓ Branch 98 → 99 taken 223308 times.
✓ Branch 98 → 114 taken 5 times.
✓ Branch 99 → 100 taken 5371 times.
✓ Branch 99 → 114 taken 217937 times.
223313 start_code <= SLICE_MAX_START_CODE && last_code == PICTURE_START_CODE) {
2414
3/4
✓ Branch 100 → 101 taken 1529 times.
✓ Branch 100 → 103 taken 3842 times.
✗ Branch 101 → 102 not taken.
✓ Branch 101 → 103 taken 1529 times.
5371 if (s2->progressive_sequence && !s2->progressive_frame) {
2415 ✗ s2->progressive_frame = 1;
2416 ✗ av_log(s2->avctx, AV_LOG_ERROR,
2417 "interlaced frame in progressive sequence, ignoring\n");
2418 }
2419
2420
1/2
✓ Branch 103 → 104 taken 5371 times.
✗ Branch 103 → 106 not taken.
5371 if (s2->picture_structure == 0 ||
2421
3/4
✓ Branch 104 → 105 taken 1536 times.
✓ Branch 104 → 108 taken 3835 times.
✗ Branch 105 → 106 not taken.
✓ Branch 105 → 108 taken 1536 times.
5371 (s2->progressive_frame && s2->picture_structure != PICT_FRAME)) {
2422 ✗ av_log(s2->avctx, AV_LOG_ERROR,
2423 "picture_structure %d invalid, ignoring\n",
2424 s2->picture_structure);
2425 ✗ s2->picture_structure = PICT_FRAME;
2426 }
2427
2428
3/4
✓ Branch 108 → 109 taken 1529 times.
✓ Branch 108 → 111 taken 3842 times.
✗ Branch 109 → 110 not taken.
✓ Branch 109 → 111 taken 1529 times.
5371 if (s2->progressive_sequence && !s2->frame_pred_frame_dct)
2429 ✗ av_log(s2->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
2430
2431
2/2
✓ Branch 111 → 112 taken 3623 times.
✓ Branch 111 → 113 taken 1748 times.
5371 if (s2->picture_structure == PICT_FRAME) {
2432 3623 s2->first_field = 0;
2433 3623 s2->v_edge_pos = 16 * s2->mb_height;
2434 } else {
2435 1748 s2->first_field ^= 1;
2436 1748 s2->v_edge_pos = 8 * s2->mb_height;
2437 1748 memset(s2->mbskip_table, 0, s2->mb_stride * s2->mb_height);
2438 }
2439 }
2440
2/2
✓ Branch 114 → 115 taken 223313 times.
✓ Branch 114 → 184 taken 923 times.
224236 if (start_code >= SLICE_MIN_START_CODE &&
2441
4/4
✓ Branch 115 → 116 taken 223308 times.
✓ Branch 115 → 184 taken 5 times.
✓ Branch 116 → 117 taken 223128 times.
✓ Branch 116 → 184 taken 180 times.
223313 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2442 223128 const int field_pic = s2->picture_structure != PICT_FRAME;
2443 223128 int mb_y = start_code - SLICE_MIN_START_CODE;
2444 223128 last_code = SLICE_MIN_START_CODE;
2445
3/4
✓ Branch 117 → 118 taken 215540 times.
✓ Branch 117 → 120 taken 7588 times.
✗ Branch 118 → 119 not taken.
✓ Branch 118 → 120 taken 215540 times.
223128 if (s2->codec_id != AV_CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
2446 ✗ mb_y += (*buf_ptr&0xE0)<<2;
2447
2448 223128 mb_y <<= field_pic;
2449
2/2
✓ Branch 120 → 121 taken 14357 times.
✓ Branch 120 → 122 taken 208771 times.
223128 if (s2->picture_structure == PICT_BOTTOM_FIELD)
2450 14357 mb_y++;
2451
2452
1/2
✗ Branch 122 → 123 not taken.
✓ Branch 122 → 125 taken 223128 times.
223128 if (buf_end - buf_ptr < 2) {
2453 ✗ av_log(s2->avctx, AV_LOG_ERROR, "slice too small\n");
2454 ✗ return AVERROR_INVALIDDATA;
2455 }
2456
2457
1/2
✗ Branch 125 → 126 not taken.
✓ Branch 125 → 128 taken 223128 times.
223128 if (mb_y >= s2->mb_height) {
2458 ✗ av_log(s2->avctx, AV_LOG_ERROR,
2459 "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2460 ✗ return AVERROR_INVALIDDATA;
2461 }
2462
2463
2/2
✓ Branch 128 → 129 taken 92580 times.
✓ Branch 128 → 133 taken 130548 times.
223128 if (!s2->last_pic.ptr) {
2464 /* Skip B-frames if we do not have reference frames and
2465 * GOP is not closed. */
2466
2/2
✓ Branch 129 → 130 taken 792 times.
✓ Branch 129 → 133 taken 91788 times.
92580 if (s2->pict_type == AV_PICTURE_TYPE_B) {
2467
1/2
✓ Branch 130 → 131 taken 792 times.
✗ Branch 130 → 133 not taken.
792 if (!s->closed_gop) {
2468 792 skip_frame = 1;
2469 792 av_log(s2->avctx, AV_LOG_DEBUG,
2470 "Skipping B slice due to open GOP\n");
2471 792 break;
2472 }
2473 }
2474 }
2475
3/4
✓ Branch 133 → 134 taken 88103 times.
✓ Branch 133 → 135 taken 134233 times.
✗ Branch 134 → 135 not taken.
✓ Branch 134 → 136 taken 88103 times.
222336 if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL))
2476 134233 s->sync = 1;
2477
2/2
✓ Branch 136 → 137 taken 86896 times.
✓ Branch 136 → 141 taken 135440 times.
222336 if (!s2->next_pic.ptr) {
2478 /* Skip P-frames if we do not have a reference frame or
2479 * we have an invalid header. */
2480
4/4
✓ Branch 137 → 138 taken 5286 times.
✓ Branch 137 → 141 taken 81610 times.
✓ Branch 138 → 139 taken 4356 times.
✓ Branch 138 → 141 taken 930 times.
86896 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
2481 4356 skip_frame = 1;
2482 4356 av_log(s2->avctx, AV_LOG_DEBUG,
2483 "Skipping P slice due to !sync\n");
2484 4356 break;
2485 }
2486 }
2487
2/2
✓ Branch 141 → 142 taken 82461 times.
✓ Branch 141 → 143 taken 135519 times.
217980 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
2488
1/2
✓ Branch 142 → 143 taken 82461 times.
✗ Branch 142 → 146 not taken.
82461 s2->pict_type == AV_PICTURE_TYPE_B) ||
2489
2/2
✓ Branch 143 → 144 taken 82461 times.
✓ Branch 143 → 145 taken 135519 times.
217980 (avctx->skip_frame >= AVDISCARD_NONKEY &&
2490
2/2
✓ Branch 144 → 145 taken 81531 times.
✓ Branch 144 → 146 taken 930 times.
82461 s2->pict_type != AV_PICTURE_TYPE_I) ||
2491
2/2
✓ Branch 145 → 146 taken 81531 times.
✓ Branch 145 → 147 taken 135519 times.
217050 avctx->skip_frame >= AVDISCARD_ALL) {
2492 82461 skip_frame = 1;
2493 82461 break;
2494 }
2495
2496
1/2
✗ Branch 147 → 148 not taken.
✓ Branch 147 → 149 taken 135519 times.
135519 if (!s2->context_initialized)
2497 ✗ break;
2498
2499
2/2
✓ Branch 149 → 150 taken 128246 times.
✓ Branch 149 → 152 taken 7273 times.
135519 if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2500
1/2
✓ Branch 150 → 151 taken 128246 times.
✗ Branch 150 → 185 not taken.
128246 if (mb_y < avctx->skip_top ||
2501
1/2
✓ Branch 151 → 152 taken 128246 times.
✗ Branch 151 → 185 not taken.
128246 mb_y >= s2->mb_height - avctx->skip_bottom)
2502 break;
2503 }
2504
2505
1/2
✗ Branch 152 → 153 not taken.
✓ Branch 152 → 157 taken 135519 times.
135519 if (!s2->pict_type) {
2506 ✗ av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2507
0/2
✗ Branch 154 → 155 not taken.
✗ Branch 154 → 156 not taken.
✗ if (avctx->err_recognition & AV_EF_EXPLODE)
2508 ✗ return AVERROR_INVALIDDATA;
2509 ✗ break;
2510 }
2511
2512
2/2
✓ Branch 157 → 158 taken 4868 times.
✓ Branch 157 → 161 taken 130651 times.
135519 if (s->first_slice) {
2513 4868 skip_frame = 0;
2514 4868 s->first_slice = 0;
2515
1/2
✗ Branch 159 → 160 not taken.
✓ Branch 159 → 161 taken 4868 times.
4868 if ((ret = mpeg_field_start(s, buf, buf_size)) < 0)
2516 ✗ return ret;
2517 }
2518
1/2
✗ Branch 161 → 162 not taken.
✓ Branch 161 → 164 taken 135519 times.
135519 if (!s2->cur_pic.ptr) {
2519 ✗ av_log(avctx, AV_LOG_ERROR,
2520 "current_picture not initialized\n");
2521 ✗ return AVERROR_INVALIDDATA;
2522 }
2523
2524 135519 if (HAVE_THREADS &&
2525
2/2
✓ Branch 164 → 165 taken 28320 times.
✓ Branch 164 → 176 taken 107199 times.
135519 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2526
1/2
✓ Branch 165 → 166 taken 28320 times.
✗ Branch 165 → 176 not taken.
28320 !avctx->hwaccel) {
2527 28320 int threshold = (s2->mb_height * s->slice_count +
2528 28320 s2->slice_context_count / 2) /
2529 28320 s2->slice_context_count;
2530
2/2
✓ Branch 166 → 167 taken 8496 times.
✓ Branch 166 → 175 taken 19824 times.
28320 if (threshold <= mb_y) {
2531 8496 Mpeg12SliceContext *const thread_context = s2->mpeg12_contexts[s->slice_count];
2532
2533 8496 thread_context->c.start_mb_y = mb_y;
2534 8496 thread_context->c.end_mb_y = s2->mb_height;
2535
2/2
✓ Branch 167 → 168 taken 7552 times.
✓ Branch 167 → 171 taken 944 times.
8496 if (s->slice_count) {
2536 7552 s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
2537 7552 ret = ff_update_duplicate_context(&thread_context->c, s2);
2538
1/2
✗ Branch 169 → 170 not taken.
✓ Branch 169 → 171 taken 7552 times.
7552 if (ret < 0)
2539 ✗ return ret;
2540 }
2541 8496 ret = init_get_bits8(&thread_context->gb, buf_ptr, input_size);
2542
1/2
✗ Branch 172 → 173 not taken.
✓ Branch 172 → 174 taken 8496 times.
8496 if (ret < 0)
2543 ✗ return ret;
2544 8496 s->slice_count++;
2545 }
2546 28320 buf_ptr += 2; // FIXME add minimum number of bytes per slice
2547 } else {
2548 107199 ret = mpeg_decode_slice(&s->slice, mb_y, &buf_ptr, input_size);
2549
2550
2/2
✓ Branch 177 → 178 taken 11 times.
✓ Branch 177 → 183 taken 107188 times.
107199 if (ret < 0) {
2551
1/2
✗ Branch 178 → 179 not taken.
✓ Branch 178 → 180 taken 11 times.
11 if (avctx->err_recognition & AV_EF_EXPLODE)
2552 ✗ return ret;
2553
2/4
✓ Branch 180 → 181 taken 11 times.
✗ Branch 180 → 184 not taken.
✓ Branch 181 → 182 taken 11 times.
✗ Branch 181 → 184 not taken.
11 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2554 11 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2555 s2->resync_mb_y, s2->mb_x, s2->mb_y,
2556 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
2557 } else {
2558 107188 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2559 107188 s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
2560 ER_AC_END | ER_DC_END | ER_MV_END);
2561 }
2562 }
2563 }
2564 136627 break;
2565 }
2566 }
2567 }
2568
2569 4853 static int mpeg_decode_frame(AVCodecContext *avctx, AVFrame *picture,
2570 int *got_output, AVPacket *avpkt)
2571 {
2572 4853 const uint8_t *buf = avpkt->data;
2573 int ret;
2574 4853 int buf_size = avpkt->size;
2575 4853 Mpeg1Context *s = avctx->priv_data;
2576 4853 MPVContext *const s2 = &s->slice.c;
2577
2578
3/6
✓ Branch 2 → 3 taken 4707 times.
✓ Branch 2 → 5 taken 146 times.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 13 taken 4707 times.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 13 not taken.
4853 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2579 /* special case for last picture */
2580
4/4
✓ Branch 5 → 6 taken 133 times.
✓ Branch 5 → 12 taken 13 times.
✓ Branch 6 → 7 taken 63 times.
✓ Branch 6 → 12 taken 70 times.
146 if (s2->low_delay == 0 && s2->next_pic.ptr) {
2581 63 int ret = av_frame_ref(picture, s2->next_pic.ptr->f);
2582
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 63 times.
63 if (ret < 0)
2583 ✗ return ret;
2584
2585 63 ff_mpv_unref_picture(&s2->next_pic);
2586
2587 63 *got_output = 1;
2588 }
2589 146 return buf_size;
2590 }
2591
2592
2/2
✓ Branch 13 → 14 taken 510 times.
✓ Branch 13 → 17 taken 4197 times.
4707 if (!s2->context_initialized &&
2593
3/4
✓ Branch 14 → 15 taken 508 times.
✓ Branch 14 → 16 taken 2 times.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 508 times.
510 (s2->codec_tag == AV_RL32("VCR2") || s2->codec_tag == AV_RL32("BW10")))
2594 2 vcr2_init_sequence(avctx);
2595
2596 4707 s->slice_count = 0;
2597
2598
4/4
✓ Branch 17 → 18 taken 4304 times.
✓ Branch 17 → 28 taken 403 times.
✓ Branch 18 → 19 taken 298 times.
✓ Branch 18 → 28 taken 4006 times.
4707 if (avctx->extradata && !s->extradata_decoded) {
2599 298 ret = decode_chunks(avctx, picture, got_output,
2600 298 avctx->extradata, avctx->extradata_size);
2601
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 24 taken 298 times.
298 if (*got_output) {
2602 ✗ av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
2603 ✗ av_frame_unref(picture);
2604 ✗ *got_output = 0;
2605 }
2606 298 s->extradata_decoded = 1;
2607
1/4
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 28 taken 298 times.
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 28 not taken.
298 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) {
2608 ✗ ff_mpv_unref_picture(&s2->cur_pic);
2609 ✗ return ret;
2610 }
2611 }
2612
2613 4707 ret = decode_chunks(avctx, picture, got_output, buf, buf_size);
2614
4/4
✓ Branch 29 → 30 taken 4528 times.
✓ Branch 29 → 31 taken 179 times.
✓ Branch 30 → 31 taken 4066 times.
✓ Branch 30 → 40 taken 462 times.
4707 if (ret<0 || *got_output) {
2615 4245 ff_mpv_unref_picture(&s2->cur_pic);
2616
2617
4/4
✓ Branch 32 → 33 taken 561 times.
✓ Branch 32 → 40 taken 3684 times.
✓ Branch 33 → 34 taken 382 times.
✓ Branch 33 → 40 taken 179 times.
4245 if (s->timecode_frame_start != -1 && *got_output) {
2618 char tcbuf[AV_TIMECODE_STR_SIZE];
2619 382 AVFrameSideData *tcside = av_frame_new_side_data(picture,
2620 AV_FRAME_DATA_GOP_TIMECODE,
2621 sizeof(int64_t));
2622
1/2
✗ Branch 35 → 36 not taken.
✓ Branch 35 → 37 taken 382 times.
382 if (!tcside)
2623 ✗ return AVERROR(ENOMEM);
2624 382 memcpy(tcside->data, &s->timecode_frame_start, sizeof(int64_t));
2625
2626 382 av_timecode_make_mpeg_tc_string(tcbuf, s->timecode_frame_start);
2627 382 av_dict_set(&picture->metadata, "timecode", tcbuf, 0);
2628
2629 382 s->timecode_frame_start = -1;
2630 }
2631 }
2632
2633 4707 return ret;
2634 }
2635
2636 ✗ static av_cold void flush(AVCodecContext *avctx)
2637 {
2638 ✗ Mpeg1Context *s = avctx->priv_data;
2639
2640 ✗ s->sync = 0;
2641 ✗ s->closed_gop = 0;
2642
2643 ✗ av_buffer_unref(&s->a53_buf_ref);
2644 ✗ ff_mpeg_flush(avctx);
2645 ✗ }
2646
2647 345 static av_cold int mpeg_decode_end(AVCodecContext *avctx)
2648 {
2649 345 Mpeg1Context *s = avctx->priv_data;
2650
2651 345 av_buffer_unref(&s->a53_buf_ref);
2652 345 return ff_mpv_decode_close(avctx);
2653 }
2654
2655 const FFCodec ff_mpeg1video_decoder = {
2656 .p.name = "mpeg1video",
2657 CODEC_LONG_NAME("MPEG-1 video"),
2658 .p.type = AVMEDIA_TYPE_VIDEO,
2659 .p.id = AV_CODEC_ID_MPEG1VIDEO,
2660 .priv_data_size = sizeof(Mpeg1Context),
2661 .init = mpeg_decode_init,
2662 .close = mpeg_decode_end,
2663 FF_CODEC_DECODE_CB(mpeg_decode_frame),
2664 .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2665 AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
2666 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2667 .flush = flush,
2668 .p.max_lowres = 3,
2669 .hw_configs = (const AVCodecHWConfigInternal *const []) {
2670 #if CONFIG_MPEG1_NVDEC_HWACCEL
2671 HWACCEL_NVDEC(mpeg1),
2672 #endif
2673 #if CONFIG_MPEG1_NVDEC_CUARRAY_HWACCEL
2674 HWACCEL_NVDEC_CUARRAY(mpeg1),
2675 #endif
2676 #if CONFIG_MPEG1_VDPAU_HWACCEL
2677 HWACCEL_VDPAU(mpeg1),
2678 #endif
2679 #if CONFIG_MPEG1_VIDEOTOOLBOX_HWACCEL
2680 HWACCEL_VIDEOTOOLBOX(mpeg1),
2681 #endif
2682 NULL
2683 },
2684 };
2685
2686 #define M2V_OFFSET(x) offsetof(Mpeg1Context, x)
2687 #define M2V_PARAM AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2688
2689 static const AVOption mpeg2video_options[] = {
2690 { "cc_format", "extract a specific Closed Captions format",
2691 M2V_OFFSET(cc_format), AV_OPT_TYPE_INT, { .i64 = CC_FORMAT_AUTO },
2692 CC_FORMAT_AUTO, CC_FORMAT_DISH, M2V_PARAM, .unit = "cc_format" },
2693
2694 { "auto", "pick first seen CC substream", 0, AV_OPT_TYPE_CONST,
2695 { .i64 = CC_FORMAT_AUTO }, .flags = M2V_PARAM, .unit = "cc_format" },
2696 { "a53", "pick A/53 Part 4 CC substream", 0, AV_OPT_TYPE_CONST,
2697 { .i64 = CC_FORMAT_A53_PART4 }, .flags = M2V_PARAM, .unit = "cc_format" },
2698 { "scte20", "pick SCTE-20 CC substream", 0, AV_OPT_TYPE_CONST,
2699 { .i64 = CC_FORMAT_SCTE20 }, .flags = M2V_PARAM, .unit = "cc_format" },
2700 { "dvd", "pick DVD CC substream", 0, AV_OPT_TYPE_CONST,
2701 { .i64 = CC_FORMAT_DVD }, .flags = M2V_PARAM, .unit = "cc_format" },
2702 { "dish", "pick Dish Network CC substream", 0, AV_OPT_TYPE_CONST,
2703 { .i64 = CC_FORMAT_DISH }, .flags = M2V_PARAM, .unit = "cc_format" },
2704 { NULL }
2705 };
2706
2707 static const AVClass mpeg2video_class = {
2708 .class_name = "MPEG-2 video",
2709 .item_name = av_default_item_name,
2710 .option = mpeg2video_options,
2711 .version = LIBAVUTIL_VERSION_INT,
2712 .category = AV_CLASS_CATEGORY_DECODER,
2713 };
2714
2715 const FFCodec ff_mpeg2video_decoder = {
2716 .p.name = "mpeg2video",
2717 CODEC_LONG_NAME("MPEG-2 video"),
2718 .p.type = AVMEDIA_TYPE_VIDEO,
2719 .p.id = AV_CODEC_ID_MPEG2VIDEO,
2720 .p.priv_class = &mpeg2video_class,
2721 .priv_data_size = sizeof(Mpeg1Context),
2722 .init = mpeg_decode_init,
2723 .close = mpeg_decode_end,
2724 FF_CODEC_DECODE_CB(mpeg_decode_frame),
2725 .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2726 AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
2727 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2728 .flush = flush,
2729 .p.max_lowres = 3,
2730 .p.profiles = NULL_IF_CONFIG_SMALL(ff_mpeg2_video_profiles),
2731 .hw_configs = (const AVCodecHWConfigInternal *const []) {
2732 #if CONFIG_MPEG2_DXVA2_HWACCEL
2733 HWACCEL_DXVA2(mpeg2),
2734 #endif
2735 #if CONFIG_MPEG2_D3D11VA_HWACCEL
2736 HWACCEL_D3D11VA(mpeg2),
2737 #endif
2738 #if CONFIG_MPEG2_D3D11VA2_HWACCEL
2739 HWACCEL_D3D11VA2(mpeg2),
2740 #endif
2741 #if CONFIG_MPEG2_D3D12VA_HWACCEL
2742 HWACCEL_D3D12VA(mpeg2),
2743 #endif
2744 #if CONFIG_MPEG2_NVDEC_HWACCEL
2745 HWACCEL_NVDEC(mpeg2),
2746 #endif
2747 #if CONFIG_MPEG2_NVDEC_CUARRAY_HWACCEL
2748 HWACCEL_NVDEC_CUARRAY(mpeg2),
2749 #endif
2750 #if CONFIG_MPEG2_VAAPI_HWACCEL
2751 HWACCEL_VAAPI(mpeg2),
2752 #endif
2753 #if CONFIG_MPEG2_VDPAU_HWACCEL
2754 HWACCEL_VDPAU(mpeg2),
2755 #endif
2756 #if CONFIG_MPEG2_VIDEOTOOLBOX_HWACCEL
2757 HWACCEL_VIDEOTOOLBOX(mpeg2),
2758 #endif
2759 NULL
2760 },
2761 };
2762
2763 //legacy decoder
2764 const FFCodec ff_mpegvideo_decoder = {
2765 .p.name = "mpegvideo",
2766 CODEC_LONG_NAME("MPEG-1 video"),
2767 .p.type = AVMEDIA_TYPE_VIDEO,
2768 .p.id = AV_CODEC_ID_MPEG2VIDEO,
2769 .priv_data_size = sizeof(Mpeg1Context),
2770 .init = mpeg_decode_init,
2771 .close = mpeg_decode_end,
2772 FF_CODEC_DECODE_CB(mpeg_decode_frame),
2773 .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2774 AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
2775 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2776 .flush = flush,
2777 .p.max_lowres = 3,
2778 };
2779
2780 typedef struct IPUContext {
2781 Mpeg12SliceContext m;
2782
2783 int flags;
2784 } IPUContext;
2785
2786 ✗ static int ipu_decode_frame(AVCodecContext *avctx, AVFrame *frame,
2787 int *got_frame, AVPacket *avpkt)
2788 {
2789 ✗ IPUContext *s = avctx->priv_data;
2790 ✗ MPVContext *const m = &s->m.c;
2791 ✗ GetBitContext *const gb = &s->m.gb;
2792 ✗ int16_t (*const block)[64] = s->m.block;
2793 int ret;
2794
2795 // Check for minimal intra MB size (considering mb header, luma & chroma dc VLC, ac EOB VLC)
2796
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (avpkt->size*8LL < (avctx->width+15)/16 * ((avctx->height+15)/16) * (2LL + 3*4 + 2*2 + 2*6))
2797 ✗ return AVERROR_INVALIDDATA;
2798
2799 ✗ ret = ff_get_buffer(avctx, frame, 0);
2800
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 7 not taken.
✗ if (ret < 0)
2801 ✗ return ret;
2802
2803 ✗ ret = init_get_bits8(gb, avpkt->data, avpkt->size);
2804
0/2
✗ Branch 8 → 9 not taken.
✗ Branch 8 → 10 not taken.
✗ if (ret < 0)
2805 ✗ return ret;
2806
2807 ✗ s->flags = get_bits(gb, 8);
2808 ✗ m->intra_dc_precision = s->flags & 3;
2809 ✗ m->q_scale_type = !!(s->flags & 0x40);
2810 ✗ m->intra_vlc_format = !!(s->flags & 0x20);
2811 ✗ m->alternate_scan = !!(s->flags & 0x10);
2812
2813 ✗ ff_permute_scantable(m->intra_scantable.permutated,
2814 ✗ s->flags & 0x10 ? ff_alternate_vertical_scan : ff_zigzag_direct,
2815
0/2
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
✗ m->idsp.idct_permutation);
2816
2817 ✗ s->m.last_dc[0] = s->m.last_dc[1] = s->m.last_dc[2] = 128 << (s->flags & 3);
2818 ✗ m->qscale = 1;
2819
2820
0/2
✗ Branch 51 → 16 not taken.
✗ Branch 51 → 52 not taken.
✗ for (int y = 0; y < avctx->height; y += 16) {
2821 int intraquant;
2822
2823
0/2
✗ Branch 49 → 17 not taken.
✗ Branch 49 → 50 not taken.
✗ for (int x = 0; x < avctx->width; x += 16) {
2824
0/4
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 19 not taken.
✗ Branch 18 → 19 not taken.
✗ Branch 18 → 22 not taken.
✗ if (x || y) {
2825
0/2
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 22 not taken.
✗ if (!get_bits1(gb))
2826 ✗ return AVERROR_INVALIDDATA;
2827 }
2828
0/2
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
✗ if (get_bits1(gb)) {
2829 ✗ intraquant = 0;
2830 } else {
2831
0/2
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 28 not taken.
✗ if (!get_bits1(gb))
2832 ✗ return AVERROR_INVALIDDATA;
2833 ✗ intraquant = 1;
2834 }
2835
2836
0/2
✗ Branch 29 → 30 not taken.
✗ Branch 29 → 31 not taken.
✗ if (s->flags & 4)
2837 ✗ skip_bits1(gb);
2838
2839
0/2
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 34 not taken.
✗ if (intraquant)
2840 ✗ m->qscale = mpeg_get_qscale(gb, m->q_scale_type);
2841
2842 ✗ memset(block, 0, 6 * sizeof(*block));
2843
2844
0/2
✗ Branch 41 → 35 not taken.
✗ Branch 41 → 42 not taken.
✗ for (int n = 0; n < 6; n++) {
2845
0/2
✗ Branch 35 → 36 not taken.
✗ Branch 35 → 37 not taken.
✗ if (s->flags & 0x80) {
2846 ✗ ret = ff_mpeg1_decode_block_intra(gb,
2847 ✗ m->intra_matrix,
2848 ✗ m->intra_scantable.permutated,
2849 ✗ s->m.last_dc, block[n],
2850 n, m->qscale);
2851 } else {
2852 ✗ ret = mpeg2_decode_block_intra(&s->m, block[n], n);
2853 }
2854
2855
0/2
✗ Branch 38 → 39 not taken.
✗ Branch 38 → 40 not taken.
✗ if (ret < 0)
2856 ✗ return ret;
2857 }
2858
2859 ✗ m->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x,
2860 ✗ frame->linesize[0], block[0]);
2861 ✗ m->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x + 8,
2862 ✗ frame->linesize[0], block[1]);
2863 ✗ m->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x,
2864 ✗ frame->linesize[0], block[2]);
2865 ✗ m->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8,
2866 ✗ frame->linesize[0], block[3]);
2867 ✗ m->idsp.idct_put(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
2868 ✗ frame->linesize[1], block[4]);
2869 ✗ m->idsp.idct_put(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
2870 ✗ frame->linesize[2], block[5]);
2871 }
2872 }
2873
2874 ✗ align_get_bits(gb);
2875
0/2
✗ Branch 54 → 55 not taken.
✗ Branch 54 → 56 not taken.
✗ if (get_bits_left(gb) != 32)
2876 ✗ return AVERROR_INVALIDDATA;
2877
2878 ✗ *got_frame = 1;
2879
2880 ✗ return avpkt->size;
2881 }
2882
2883 ✗ static av_cold int ipu_decode_init(AVCodecContext *avctx)
2884 {
2885 ✗ IPUContext *s = avctx->priv_data;
2886 ✗ MPVContext *const m = &s->m.c;
2887
2888 ✗ avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2889 ✗ m->avctx = avctx;
2890
2891 ✗ ff_idctdsp_init(&m->idsp, avctx);
2892 ✗ ff_mpeg12_init_vlcs();
2893
2894
0/2
✗ Branch 6 → 5 not taken.
✗ Branch 6 → 7 not taken.
✗ for (int i = 0; i < 64; i++) {
2895 ✗ int j = m->idsp.idct_permutation[i];
2896 ✗ int v = ff_mpeg1_default_intra_matrix[i];
2897 ✗ m->intra_matrix[j] = v;
2898 ✗ m->chroma_intra_matrix[j] = v;
2899 }
2900
2901 ✗ return 0;
2902 }
2903
2904 const FFCodec ff_ipu_decoder = {
2905 .p.name = "ipu",
2906 CODEC_LONG_NAME("IPU Video"),
2907 .p.type = AVMEDIA_TYPE_VIDEO,
2908 .p.id = AV_CODEC_ID_IPU,
2909 .priv_data_size = sizeof(IPUContext),
2910 .init = ipu_decode_init,
2911 FF_CODEC_DECODE_CB(ipu_decode_frame),
2912 .p.capabilities = AV_CODEC_CAP_DR1,
2913 };
2914