FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12dec.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 1168 1622 72.0%
Functions: 30 34 88.2%
Branches: 672 1131 59.4%

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