FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12dec.c
Date: 2025-01-20 09:27:23
Exec Total Coverage
Lines: 1163 1617 71.9%
Functions: 29 33 87.9%
Branches: 672 1135 59.2%

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