FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12dec.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 1167 1624 71.9%
Functions: 29 33 87.9%
Branches: 676 1139 59.4%

Line Branch Exec Source
1 /*
2 * MPEG-1/2 decoder
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2013 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * MPEG-1/2 decoder
26 */
27
28 #include "config_components.h"
29
30 #define UNCHECKED_BITSTREAM_READER 1
31 #include <inttypes.h>
32 #include <stdatomic.h>
33
34 #include "libavutil/attributes.h"
35 #include "libavutil/emms.h"
36 #include "libavutil/imgutils.h"
37 #include "libavutil/internal.h"
38 #include "libavutil/mem_internal.h"
39 #include "libavutil/reverse.h"
40 #include "libavutil/stereo3d.h"
41 #include "libavutil/timecode.h"
42
43 #include "avcodec.h"
44 #include "codec_internal.h"
45 #include "decode.h"
46 #include "error_resilience.h"
47 #include "hwaccel_internal.h"
48 #include "hwconfig.h"
49 #include "idctdsp.h"
50 #include "mpeg_er.h"
51 #include "mpeg12.h"
52 #include "mpeg12codecs.h"
53 #include "mpeg12data.h"
54 #include "mpeg12dec.h"
55 #include "mpegutils.h"
56 #include "mpegvideo.h"
57 #include "mpegvideodata.h"
58 #include "mpegvideodec.h"
59 #include "profiles.h"
60 #include "startcode.h"
61 #include "thread.h"
62
63 #define A53_MAX_CC_COUNT 2000
64
65 enum Mpeg2ClosedCaptionsFormat {
66 CC_FORMAT_AUTO,
67 CC_FORMAT_A53_PART4,
68 CC_FORMAT_SCTE20,
69 CC_FORMAT_DVD,
70 CC_FORMAT_DISH
71 };
72
73 typedef struct Mpeg1Context {
74 MpegEncContext mpeg_enc_ctx;
75 AVPanScan pan_scan; /* some temporary storage for the panscan */
76 enum AVStereo3DType stereo3d_type;
77 int has_stereo3d;
78 AVBufferRef *a53_buf_ref;
79 enum Mpeg2ClosedCaptionsFormat cc_format;
80 uint8_t afd;
81 int has_afd;
82 int slice_count;
83 unsigned aspect_ratio_info;
84 AVRational save_aspect;
85 int save_width, save_height, save_progressive_seq;
86 AVRational frame_rate_ext; /* MPEG-2 specific framerate modificator */
87 unsigned frame_rate_index;
88 int sync; /* Did we reach a sync point like a GOP/SEQ/KEYFrame? */
89 int closed_gop;
90 int tmpgexs;
91 int first_slice;
92 int extradata_decoded;
93 int vbv_delay;
94 int64_t 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 5381430 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
99 {
100 int code, sign, val, shift;
101
102 5381430 code = get_vlc2(&s->gb, ff_mv_vlc, MV_VLC_BITS, 2);
103
2/2
✓ Branch 0 taken 2001098 times.
✓ Branch 1 taken 3380332 times.
5381430 if (code == 0)
104 2001098 return pred;
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3380332 times.
3380332 if (code < 0)
106 return 0xffff;
107
108 3380332 sign = get_bits1(&s->gb);
109 3380332 shift = fcode - 1;
110 3380332 val = code;
111
2/2
✓ Branch 0 taken 3040672 times.
✓ Branch 1 taken 339660 times.
3380332 if (shift) {
112 3040672 val = (val - 1) << shift;
113 3040672 val |= get_bits(&s->gb, shift);
114 3040672 val++;
115 }
116
2/2
✓ Branch 0 taken 1747538 times.
✓ Branch 1 taken 1632794 times.
3380332 if (sign)
117 1747538 val = -val;
118 3380332 val += pred;
119
120 /* modulo decoding */
121 3380332 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 5111209 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
219 int16_t *block, int n)
220 {
221 int level, i, j, run;
222 5111209 const uint8_t *const scantable = s->intra_scantable.permutated;
223 const uint16_t *quant_matrix;
224 5111209 const int qscale = s->qscale;
225 int mismatch;
226
227 5111209 mismatch = 1;
228
229 {
230 5111209 OPEN_READER(re, &s->gb);
231 5111209 i = -1;
232
2/2
✓ Branch 0 taken 3823465 times.
✓ Branch 1 taken 1287744 times.
5111209 if (n < 4)
233 3823465 quant_matrix = s->inter_matrix;
234 else
235 1287744 quant_matrix = s->chroma_inter_matrix;
236
237 // Special case for first coefficient, no need to add second VLC table.
238 5111209 UPDATE_CACHE(re, &s->gb);
239
2/2
✓ Branch 0 taken 3241888 times.
✓ Branch 1 taken 1869321 times.
5111209 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
240 1869321 level = (3 * qscale * quant_matrix[0]) >> 5;
241
2/2
✓ Branch 0 taken 1181473 times.
✓ Branch 1 taken 687848 times.
1869321 if (GET_CACHE(re, &s->gb) & 0x40000000)
242 1181473 level = -level;
243 1869321 block[0] = level;
244 1869321 mismatch ^= level;
245 1869321 i++;
246 1869321 SKIP_BITS(re, &s->gb, 2);
247
2/2
✓ Branch 0 taken 979209 times.
✓ Branch 1 taken 890112 times.
1869321 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
248 890112 goto end;
249 }
250
251 /* now quantify & encode AC coefficients */
252 for (;;) {
253
2/2
✓ Branch 1 taken 1627031 times.
✓ Branch 2 taken 16613621 times.
18240652 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 17969009 times.
✓ Branch 1 taken 271643 times.
18240652 if (level != 0) {
257 17969009 i += run;
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17969009 times.
17969009 if (i > MAX_INDEX)
259 break;
260 17969009 j = scantable[i];
261 17969009 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
262 17969009 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
263 17969009 SHOW_SBITS(re, &s->gb, 1);
264 17969009 SKIP_BITS(re, &s->gb, 1);
265 } else {
266 /* escape */
267 271643 run = SHOW_UBITS(re, &s->gb, 6) + 1;
268 271643 LAST_SKIP_BITS(re, &s->gb, 6);
269 271643 UPDATE_CACHE(re, &s->gb);
270 271643 level = SHOW_SBITS(re, &s->gb, 12);
271 271643 SKIP_BITS(re, &s->gb, 12);
272
273 271643 i += run;
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 271643 times.
271643 if (i > MAX_INDEX)
275 break;
276 271643 j = scantable[i];
277
2/2
✓ Branch 0 taken 131735 times.
✓ Branch 1 taken 139908 times.
271643 if (level < 0) {
278 131735 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
279 131735 level = -level;
280 } else {
281 139908 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
282 }
283 }
284
285 18240652 mismatch ^= level;
286 18240652 block[j] = level;
287
2/2
✓ Branch 0 taken 4221097 times.
✓ Branch 1 taken 14019555 times.
18240652 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
288 4221097 break;
289 14019555 UPDATE_CACHE(re, &s->gb);
290 }
291 5111209 end:
292 5111209 LAST_SKIP_BITS(re, &s->gb, 2);
293 5111209 CLOSE_READER(re, &s->gb);
294 }
295 5111209 block[63] ^= (mismatch & 1);
296
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5111209 times.
5111209 check_scantable_index(s, i);
298
299 5111209 s->block_last_index[n] = i;
300 5111209 return 0;
301 }
302
303 1912082 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 1912082 const uint8_t *const scantable = s->intra_scantable.permutated;
310 const uint16_t *quant_matrix;
311 1912082 const int qscale = s->qscale;
312 int mismatch;
313
314 /* DC coefficient */
315
2/2
✓ Branch 0 taken 1202516 times.
✓ Branch 1 taken 709566 times.
1912082 if (n < 4) {
316 1202516 quant_matrix = s->intra_matrix;
317 1202516 component = 0;
318 } else {
319 709566 quant_matrix = s->chroma_intra_matrix;
320 709566 component = (n & 1) + 1;
321 }
322 1912082 diff = decode_dc(&s->gb, component);
323 1912082 dc = s->last_dc[component];
324 1912082 dc += diff;
325 1912082 s->last_dc[component] = dc;
326 1912082 block[0] = dc * (1 << (3 - s->intra_dc_precision));
327 ff_tlog(s->avctx, "dc=%d\n", block[0]);
328 1912082 mismatch = block[0] ^ 1;
329 1912082 i = 0;
330
2/2
✓ Branch 0 taken 1329536 times.
✓ Branch 1 taken 582546 times.
1912082 if (s->intra_vlc_format)
331 1329536 rl_vlc = ff_mpeg2_rl_vlc;
332 else
333 582546 rl_vlc = ff_mpeg1_rl_vlc;
334
335 {
336 1912082 OPEN_READER(re, &s->gb);
337 /* now quantify & encode AC coefficients */
338 for (;;) {
339 26547098 UPDATE_CACHE(re, &s->gb);
340
2/2
✓ Branch 1 taken 735097 times.
✓ Branch 2 taken 13494493 times.
14229590 GET_RL_VLC(level, run, re, &s->gb, rl_vlc,
341 TEX_VLC_BITS, 2, 0);
342
343
2/2
✓ Branch 0 taken 1912072 times.
✓ Branch 1 taken 12317518 times.
14229590 if (level == 127) {
344 1912072 break;
345
2/2
✓ Branch 0 taken 12183049 times.
✓ Branch 1 taken 134469 times.
12317518 } else if (level != 0) {
346 12183049 i += run;
347
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 12183039 times.
12183049 if (i > MAX_INDEX)
348 10 break;
349 12183039 j = scantable[i];
350 12183039 level = (level * qscale * quant_matrix[j]) >> 4;
351 12183039 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
352 12183039 SHOW_SBITS(re, &s->gb, 1);
353 12183039 LAST_SKIP_BITS(re, &s->gb, 1);
354 } else {
355 /* escape */
356 134469 run = SHOW_UBITS(re, &s->gb, 6) + 1;
357 134469 SKIP_BITS(re, &s->gb, 6);
358 134469 level = SHOW_SBITS(re, &s->gb, 12);
359 134469 LAST_SKIP_BITS(re, &s->gb, 12);
360 134469 i += run;
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 134469 times.
134469 if (i > MAX_INDEX)
362 break;
363 134469 j = scantable[i];
364
2/2
✓ Branch 0 taken 59996 times.
✓ Branch 1 taken 74473 times.
134469 if (level < 0) {
365 59996 level = (-level * qscale * quant_matrix[j]) >> 4;
366 59996 level = -level;
367 } else {
368 74473 level = (level * qscale * quant_matrix[j]) >> 4;
369 }
370 }
371
372 12317508 mismatch ^= level;
373 12317508 block[j] = level;
374 }
375 1912082 CLOSE_READER(re, &s->gb);
376 }
377 1912082 block[63] ^= mismatch & 1;
378
379
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1912072 times.
1912082 check_scantable_index(s, i);
380
381 1912072 s->block_last_index[n] = i;
382 1912072 return 0;
383 }
384
385 /******************************************/
386 /* decoding */
387
388 42876 static inline int get_dmv(MpegEncContext *s)
389 {
390
2/2
✓ Branch 1 taken 39969 times.
✓ Branch 2 taken 2907 times.
42876 if (get_bits1(&s->gb))
391 39969 return 1 - (get_bits1(&s->gb) << 1);
392 else
393 2907 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 2602090 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 2602090 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 165428 times.
✓ Branch 1 taken 2436662 times.
2602090 if (s->mb_skip_run-- != 0) {
413
2/2
✓ Branch 0 taken 57361 times.
✓ Branch 1 taken 108067 times.
165428 if (s->pict_type == AV_PICTURE_TYPE_P) {
414 57361 s->mb_skipped = 1;
415 57361 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 108067 times.
✗ Branch 1 not taken.
108067 if (s->mb_x)
421 108067 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 108067 times.
108067 if (IS_INTRA(mb_type)) {
426 av_log(s->avctx, AV_LOG_ERROR, "skip with previntra\n");
427 return AVERROR_INVALIDDATA;
428 }
429 108067 s->cur_pic.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
430 108067 mb_type | MB_TYPE_SKIP;
431
432
2/2
✓ Branch 0 taken 75586 times.
✓ Branch 1 taken 32481 times.
108067 if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
433 75586 s->mb_skipped = 1;
434 }
435
436 165428 return 0;
437 }
438
439
3/3
✓ Branch 0 taken 239208 times.
✓ Branch 1 taken 1336161 times.
✓ Branch 2 taken 861293 times.
2436662 switch (s->pict_type) {
440 239208 default:
441 case AV_PICTURE_TYPE_I:
442
2/2
✓ Branch 1 taken 17897 times.
✓ Branch 2 taken 221311 times.
239208 if (get_bits1(&s->gb) == 0) {
443
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17897 times.
17897 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 17897 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
450 } else {
451 221311 mb_type = MB_TYPE_INTRA;
452 }
453 239208 break;
454 1336161 case AV_PICTURE_TYPE_P:
455 1336161 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 1336161 times.
1336161 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 1336161 break;
462 861293 case AV_PICTURE_TYPE_B:
463 861293 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 861293 times.
861293 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 861293 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 321638 times.
✓ Branch 1 taken 2115024 times.
2436662 if (IS_INTRA(mb_type)) {
474 321638 s->bdsp.clear_blocks(s->block[0]);
475
476
2/2
✓ Branch 0 taken 54159 times.
✓ Branch 1 taken 267479 times.
321638 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 263992 times.
✓ Branch 1 taken 57646 times.
321638 if (s->picture_structure == PICT_FRAME &&
482
2/2
✓ Branch 0 taken 120691 times.
✓ Branch 1 taken 143301 times.
263992 !s->frame_pred_frame_dct)
483 120691 s->interlaced_dct = get_bits1(&s->gb);
484
485
2/2
✓ Branch 0 taken 25984 times.
✓ Branch 1 taken 295654 times.
321638 if (IS_QUANT(mb_type))
486 25984 s->qscale = mpeg_get_qscale(s);
487
488
2/2
✓ Branch 0 taken 1356 times.
✓ Branch 1 taken 320282 times.
321638 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 320282 memset(s->last_mv, 0, sizeof(s->last_mv));
506 }
507 321638 s->mb_intra = 1;
508
509
2/2
✓ Branch 0 taken 300634 times.
✓ Branch 1 taken 21004 times.
321638 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
510
2/2
✓ Branch 0 taken 1912082 times.
✓ Branch 1 taken 300624 times.
2212706 for (i = 0; i < mb_block_count; i++)
511
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1912072 times.
1912082 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 211967 times.
✓ Branch 1 taken 1903057 times.
2115024 if (mb_type & MB_TYPE_ZERO_MV) {
531 av_assert2(mb_type & MB_TYPE_CBP);
532
533 211967 s->mv_dir = MV_DIR_FORWARD;
534
2/2
✓ Branch 0 taken 87905 times.
✓ Branch 1 taken 124062 times.
211967 if (s->picture_structure == PICT_FRAME) {
535
1/2
✓ Branch 0 taken 87905 times.
✗ Branch 1 not taken.
87905 if (s->picture_structure == PICT_FRAME
536
2/2
✓ Branch 0 taken 53337 times.
✓ Branch 1 taken 34568 times.
87905 && !s->frame_pred_frame_dct)
537 53337 s->interlaced_dct = get_bits1(&s->gb);
538 87905 s->mv_type = MV_TYPE_16X16;
539 } else {
540 124062 s->mv_type = MV_TYPE_FIELD;
541 124062 mb_type |= MB_TYPE_INTERLACED;
542 124062 s->field_select[0][0] = s->picture_structure - 1;
543 }
544
545
2/2
✓ Branch 0 taken 51734 times.
✓ Branch 1 taken 160233 times.
211967 if (IS_QUANT(mb_type))
546 51734 s->qscale = mpeg_get_qscale(s);
547
548 211967 s->last_mv[0][0][0] = 0;
549 211967 s->last_mv[0][0][1] = 0;
550 211967 s->last_mv[0][1][0] = 0;
551 211967 s->last_mv[0][1][1] = 0;
552 211967 s->mv[0][0][0] = 0;
553 211967 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 1402745 times.
✓ Branch 1 taken 500312 times.
✓ Branch 2 taken 700747 times.
✓ Branch 3 taken 701998 times.
1903057 if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct) {
559 700747 motion_type = MT_FRAME;
560 } else {
561 1202310 motion_type = get_bits(&s->gb, 2);
562
4/4
✓ Branch 0 taken 701998 times.
✓ Branch 1 taken 500312 times.
✓ Branch 2 taken 515636 times.
✓ Branch 3 taken 186362 times.
1202310 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
563 515636 s->interlaced_dct = get_bits1(&s->gb);
564 }
565
566
2/2
✓ Branch 0 taken 173679 times.
✓ Branch 1 taken 1729378 times.
1903057 if (IS_QUANT(mb_type))
567 173679 s->qscale = mpeg_get_qscale(s);
568
569 /* motion vectors */
570 1903057 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 1619707 times.
✓ Branch 1 taken 261912 times.
✓ Branch 2 taken 21438 times.
✗ Branch 3 not taken.
1903057 switch (motion_type) {
573 1619707 case MT_FRAME: /* or MT_16X8 */
574
2/2
✓ Branch 0 taken 1332186 times.
✓ Branch 1 taken 287521 times.
1619707 if (s->picture_structure == PICT_FRAME) {
575 1332186 mb_type |= MB_TYPE_16x16;
576 1332186 s->mv_type = MV_TYPE_16X16;
577
2/2
✓ Branch 0 taken 2664372 times.
✓ Branch 1 taken 1332186 times.
3996558 for (i = 0; i < 2; i++) {
578
2/2
✓ Branch 0 taken 1716696 times.
✓ Branch 1 taken 947676 times.
2664372 if (HAS_MV(mb_type, i)) {
579 /* MT_FRAME */
580 1716696 s->mv[i][0][0] =
581 1716696 s->last_mv[i][0][0] =
582 1716696 s->last_mv[i][1][0] =
583 1716696 mpeg_decode_motion(s, s->mpeg_f_code[i][0],
584 s->last_mv[i][0][0]);
585 1716696 s->mv[i][0][1] =
586 1716696 s->last_mv[i][0][1] =
587 1716696 s->last_mv[i][1][1] =
588 1716696 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 1716696 times.
1716696 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 287521 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
599 287521 s->mv_type = MV_TYPE_16X8;
600
2/2
✓ Branch 0 taken 575042 times.
✓ Branch 1 taken 287521 times.
862563 for (i = 0; i < 2; i++) {
601
2/2
✓ Branch 0 taken 288883 times.
✓ Branch 1 taken 286159 times.
575042 if (HAS_MV(mb_type, i)) {
602 /* MT_16X8 */
603
2/2
✓ Branch 0 taken 577766 times.
✓ Branch 1 taken 288883 times.
866649 for (j = 0; j < 2; j++) {
604 577766 s->field_select[i][j] = get_bits1(&s->gb);
605
2/2
✓ Branch 0 taken 1155532 times.
✓ Branch 1 taken 577766 times.
1733298 for (k = 0; k < 2; k++) {
606 1155532 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
607 s->last_mv[i][j][k]);
608 1155532 s->last_mv[i][j][k] = val;
609 1155532 s->mv[i][j][k] = val;
610 }
611 }
612 }
613 }
614 }
615 1619707 break;
616 261912 case MT_FIELD:
617 261912 s->mv_type = MV_TYPE_FIELD;
618
2/2
✓ Branch 0 taken 70559 times.
✓ Branch 1 taken 191353 times.
261912 if (s->picture_structure == PICT_FRAME) {
619 70559 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
620
2/2
✓ Branch 0 taken 141118 times.
✓ Branch 1 taken 70559 times.
211677 for (i = 0; i < 2; i++) {
621
2/2
✓ Branch 0 taken 88716 times.
✓ Branch 1 taken 52402 times.
141118 if (HAS_MV(mb_type, i)) {
622
2/2
✓ Branch 0 taken 177432 times.
✓ Branch 1 taken 88716 times.
266148 for (j = 0; j < 2; j++) {
623 177432 s->field_select[i][j] = get_bits1(&s->gb);
624 177432 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
625 s->last_mv[i][j][0]);
626 177432 s->last_mv[i][j][0] = val;
627 177432 s->mv[i][j][0] = val;
628 ff_tlog(s->avctx, "fmx=%d\n", val);
629 177432 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
630 177432 s->last_mv[i][j][1] >> 1);
631 177432 s->last_mv[i][j][1] = 2 * val;
632 177432 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 191353 times.
191353 av_assert0(!s->progressive_sequence);
639 191353 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
640
2/2
✓ Branch 0 taken 382706 times.
✓ Branch 1 taken 191353 times.
574059 for (i = 0; i < 2; i++) {
641
2/2
✓ Branch 0 taken 196027 times.
✓ Branch 1 taken 186679 times.
382706 if (HAS_MV(mb_type, i)) {
642 196027 s->field_select[i][0] = get_bits1(&s->gb);
643
2/2
✓ Branch 0 taken 392054 times.
✓ Branch 1 taken 196027 times.
588081 for (k = 0; k < 2; k++) {
644 392054 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
645 s->last_mv[i][0][k]);
646 392054 s->last_mv[i][0][k] = val;
647 392054 s->last_mv[i][1][k] = val;
648 392054 s->mv[i][0][k] = val;
649 }
650 }
651 }
652 }
653 261912 break;
654 21438 case MT_DMV:
655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21438 times.
21438 if (s->progressive_sequence){
656 av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
657 return AVERROR_INVALIDDATA;
658 }
659 21438 s->mv_type = MV_TYPE_DMV;
660
2/2
✓ Branch 0 taken 42876 times.
✓ Branch 1 taken 21438 times.
64314 for (i = 0; i < 2; i++) {
661
2/2
✓ Branch 0 taken 21438 times.
✓ Branch 1 taken 21438 times.
42876 if (HAS_MV(mb_type, i)) {
662 int dmx, dmy, mx, my, m;
663 21438 const int my_shift = s->picture_structure == PICT_FRAME;
664
665 21438 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
666 s->last_mv[i][0][0]);
667 21438 s->last_mv[i][0][0] = mx;
668 21438 s->last_mv[i][1][0] = mx;
669 21438 dmx = get_dmv(s);
670 21438 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
671 21438 s->last_mv[i][0][1] >> my_shift);
672 21438 dmy = get_dmv(s);
673
674
675 21438 s->last_mv[i][0][1] = my * (1 << my_shift);
676 21438 s->last_mv[i][1][1] = my * (1 << my_shift);
677
678 21438 s->mv[i][0][0] = mx;
679 21438 s->mv[i][0][1] = my;
680 21438 s->mv[i][1][0] = mx; // not used
681 21438 s->mv[i][1][1] = my; // not used
682
683
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21438 times.
21438 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 21438 mb_type |= MB_TYPE_16x16;
697
698 21438 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
699 21438 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
700
2/2
✓ Branch 0 taken 18359 times.
✓ Branch 1 taken 3079 times.
21438 if (s->picture_structure == PICT_TOP_FIELD)
701 18359 s->mv[i][2][1]--;
702 else
703 3079 s->mv[i][2][1]++;
704 }
705 }
706 }
707 21438 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 2115024 s->mb_intra = 0;
716 2115024 s->last_dc[0] = s->last_dc[1] = s->last_dc[2] = 128 << s->intra_dc_precision;
717
2/2
✓ Branch 0 taken 1693930 times.
✓ Branch 1 taken 421094 times.
2115024 if (HAS_CBP(mb_type)) {
718 1693930 s->bdsp.clear_blocks(s->block[0]);
719
720 1693930 cbp = get_vlc2(&s->gb, ff_mb_pat_vlc, MB_PAT_VLC_BITS, 1);
721
2/2
✓ Branch 0 taken 43689 times.
✓ Branch 1 taken 1650241 times.
1693930 if (mb_block_count > 6) {
722 43689 cbp *= 1 << mb_block_count - 6;
723 43689 cbp |= get_bits(&s->gb, mb_block_count - 6);
724 43689 s->bdsp.clear_blocks(s->block[6]);
725 }
726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1693930 times.
1693930 if (cbp <= 0) {
727 av_log(s->avctx, AV_LOG_ERROR,
728 "invalid cbp %d at %d %d\n", cbp, s->mb_x, s->mb_y);
729 return AVERROR_INVALIDDATA;
730 }
731
732
2/2
✓ Branch 0 taken 1582194 times.
✓ Branch 1 taken 111736 times.
1693930 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
733 1582194 cbp <<= 12 - mb_block_count;
734
735
2/2
✓ Branch 0 taken 9580542 times.
✓ Branch 1 taken 1582194 times.
11162736 for (i = 0; i < mb_block_count; i++) {
736
2/2
✓ Branch 0 taken 5111209 times.
✓ Branch 1 taken 4469333 times.
9580542 if (cbp & (1 << 11)) {
737
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5111209 times.
5111209 if ((ret = mpeg2_decode_block_non_intra(s, s->block[i], i)) < 0)
738 return ret;
739 } else {
740 4469333 s->block_last_index[i] = -1;
741 }
742 9580542 cbp += cbp;
743 }
744 } else {
745
2/2
✓ Branch 0 taken 670416 times.
✓ Branch 1 taken 111736 times.
782152 for (i = 0; i < 6; i++) {
746
2/2
✓ Branch 0 taken 403182 times.
✓ Branch 1 taken 267234 times.
670416 if (cbp & 32) {
747
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 403182 times.
403182 if ((ret = mpeg1_decode_block_inter(s, s->block[i], i)) < 0)
748 return ret;
749 } else {
750 267234 s->block_last_index[i] = -1;
751 }
752 670416 cbp += cbp;
753 }
754 }
755 } else {
756
2/2
✓ Branch 0 taken 5053128 times.
✓ Branch 1 taken 421094 times.
5474222 for (i = 0; i < 12; i++)
757 5053128 s->block_last_index[i] = -1;
758 }
759 }
760
761 2436652 s->cur_pic.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
762
763 2436652 return 0;
764 }
765
766 288 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
767 {
768 288 Mpeg1Context *s = avctx->priv_data;
769 288 MpegEncContext *s2 = &s->mpeg_enc_ctx;
770 int ret;
771
772 288 s2->out_format = FMT_MPEG1;
773
774
2/2
✓ Branch 0 taken 286 times.
✓ Branch 1 taken 2 times.
288 if ( avctx->codec_tag != AV_RL32("VCR2")
775
1/2
✓ Branch 0 taken 286 times.
✗ Branch 1 not taken.
286 && avctx->codec_tag != AV_RL32("BW10"))
776 286 avctx->coded_width = avctx->coded_height = 0; // do not trust dimensions from input
777 288 ret = ff_mpv_decode_init(s2, avctx);
778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 288 times.
288 if (ret < 0)
779 return ret;
780
781 288 ff_mpeg12_init_vlcs();
782
783 288 s2->chroma_format = CHROMA_420;
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 < CHROMA_422)
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 == CHROMA_422)
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 5054 static int mpeg_decode_postinit(AVCodecContext *avctx)
882 {
883 5054 Mpeg1Context *s1 = avctx->priv_data;
884 5054 MpegEncContext *s = &s1->mpeg_enc_ctx;
885 int ret;
886
887
2/2
✓ Branch 0 taken 494 times.
✓ Branch 1 taken 4560 times.
5054 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 2520 times.
✓ Branch 1 taken 2040 times.
4560 if (s1->aspect_ratio_info > 1) {
894 AVRational dar =
895 2520 av_mul_q(av_div_q(ff_mpeg2_aspect[s1->aspect_ratio_info],
896 2520 (AVRational) { s1->pan_scan.width,
897 2520 s1->pan_scan.height }),
898 2520 (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 1937 times.
✓ Branch 2 taken 583 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 27 times.
✓ Branch 5 taken 556 times.
3103 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 1963 s->avctx->sample_aspect_ratio =
908 1963 av_div_q(ff_mpeg2_aspect[s1->aspect_ratio_info],
909 1963 (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 5054 times.
5054 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 4795 times.
✓ Branch 1 taken 259 times.
5054 if (!s->context_initialized ||
939
1/2
✓ Branch 0 taken 4795 times.
✗ Branch 1 not taken.
4795 avctx->coded_width != s->width ||
940
1/2
✓ Branch 0 taken 4795 times.
✗ Branch 1 not taken.
4795 avctx->coded_height != s->height ||
941
1/2
✓ Branch 0 taken 4795 times.
✗ Branch 1 not taken.
4795 s1->save_width != s->width ||
942
3/4
✓ Branch 0 taken 4795 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4793 times.
✓ Branch 3 taken 2 times.
9590 s1->save_height != s->height ||
943 4795 av_cmp_q(s1->save_aspect, s->avctx->sample_aspect_ratio) ||
944
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4793 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4793 (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 || s1->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 CHROMA_420: avctx->chroma_sample_location = AVCHROMA_LOC_LEFT; break;
994 61 case CHROMA_422:
995 61 case CHROMA_444: 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
2/2
✓ Branch 0 taken 301 times.
✓ Branch 1 taken 261 times.
562 for (int i = 0; i < s->slice_context_count; i++)
1006 301 ff_mpv_framesize_disable(&s->thread_context[i]->sc);
1007 }
1008 5054 return 0;
1009 }
1010
1011 5054 static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
1012 int buf_size)
1013 {
1014 5054 Mpeg1Context *s1 = avctx->priv_data;
1015 5054 MpegEncContext *s = &s1->mpeg_enc_ctx;
1016 int ref, f_code, vbv_delay, ret;
1017
1018 5054 ret = init_get_bits8(&s->gb, buf, buf_size);
1019
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5054 times.
5054 if (ret < 0)
1020 return ret;
1021
1022 5054 ref = get_bits(&s->gb, 10); /* temporal ref */
1023 5054 s->pict_type = get_bits(&s->gb, 3);
1024
2/4
✓ Branch 0 taken 5054 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5054 times.
5054 if (s->pict_type == 0 || s->pict_type > 3)
1025 return AVERROR_INVALIDDATA;
1026
1027 5054 vbv_delay = get_bits(&s->gb, 16);
1028 5054 s1->vbv_delay = vbv_delay;
1029
2/2
✓ Branch 0 taken 2329 times.
✓ Branch 1 taken 2725 times.
5054 if (s->pict_type == AV_PICTURE_TYPE_P ||
1030
2/2
✓ Branch 0 taken 1618 times.
✓ Branch 1 taken 711 times.
2329 s->pict_type == AV_PICTURE_TYPE_B) {
1031 4343 s->full_pel[0] = get_bits1(&s->gb);
1032 4343 f_code = get_bits(&s->gb, 3);
1033
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4343 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4343 if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1034 return AVERROR_INVALIDDATA;
1035 4343 f_code += !f_code;
1036 4343 s->mpeg_f_code[0][0] = f_code;
1037 4343 s->mpeg_f_code[0][1] = f_code;
1038 }
1039
2/2
✓ Branch 0 taken 1618 times.
✓ Branch 1 taken 3436 times.
5054 if (s->pict_type == AV_PICTURE_TYPE_B) {
1040 1618 s->full_pel[1] = get_bits1(&s->gb);
1041 1618 f_code = get_bits(&s->gb, 3);
1042
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1618 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1618 if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1043 return AVERROR_INVALIDDATA;
1044 1618 f_code += !f_code;
1045 1618 s->mpeg_f_code[1][0] = f_code;
1046 1618 s->mpeg_f_code[1][1] = f_code;
1047 }
1048
1049
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5054 times.
5054 if (avctx->debug & FF_DEBUG_PICT_INFO)
1050 av_log(avctx, AV_LOG_DEBUG,
1051 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1052
1053 5054 return 0;
1054 }
1055
1056 717 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1057 {
1058 717 MpegEncContext *s = &s1->mpeg_enc_ctx;
1059 int horiz_size_ext, vert_size_ext;
1060 int bit_rate_ext;
1061
1062 717 skip_bits(&s->gb, 1); /* profile and level esc*/
1063 717 s->avctx->profile = get_bits(&s->gb, 3);
1064 717 s->avctx->level = get_bits(&s->gb, 4);
1065 717 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1066 717 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1067
1068
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 717 times.
717 if (!s->chroma_format) {
1069 s->chroma_format = CHROMA_420;
1070 av_log(s->avctx, AV_LOG_WARNING, "Chroma format invalid\n");
1071 }
1072
1073 717 horiz_size_ext = get_bits(&s->gb, 2);
1074 717 vert_size_ext = get_bits(&s->gb, 2);
1075 717 s->width |= (horiz_size_ext << 12);
1076 717 s->height |= (vert_size_ext << 12);
1077 717 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1078 717 s->bit_rate += (bit_rate_ext << 18) * 400LL;
1079 717 check_marker(s->avctx, &s->gb, "after bit rate extension");
1080 717 s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1081
1082 717 s->low_delay = get_bits1(&s->gb);
1083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 717 times.
717 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
1084 s->low_delay = 1;
1085
1086 717 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1087 717 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1088
1089 ff_dlog(s->avctx, "sequence extension\n");
1090 717 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1091
1092
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 717 times.
717 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1093 av_log(s->avctx, AV_LOG_DEBUG,
1094 "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%"PRId64"\n",
1095 s->avctx->profile, s->avctx->level, s->progressive_sequence, s->chroma_format,
1096 s->avctx->rc_buffer_size, s->bit_rate);
1097 717 }
1098
1099 95 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1100 {
1101 95 MpegEncContext *s = &s1->mpeg_enc_ctx;
1102 int color_description, w, h;
1103
1104 95 skip_bits(&s->gb, 3); /* video format */
1105 95 color_description = get_bits1(&s->gb);
1106
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 62 times.
95 if (color_description) {
1107 33 s->avctx->color_primaries = get_bits(&s->gb, 8);
1108 33 s->avctx->color_trc = get_bits(&s->gb, 8);
1109 33 s->avctx->colorspace = get_bits(&s->gb, 8);
1110 }
1111 95 w = get_bits(&s->gb, 14);
1112 95 skip_bits(&s->gb, 1); // marker
1113 95 h = get_bits(&s->gb, 14);
1114 // remaining 3 bits are zero padding
1115
1116 95 s1->pan_scan.width = 16 * w;
1117 95 s1->pan_scan.height = 16 * h;
1118
1119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 95 times.
95 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1120 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1121 95 }
1122
1123 27 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1124 {
1125 27 MpegEncContext *s = &s1->mpeg_enc_ctx;
1126 int i, nofco;
1127
1128 27 nofco = 1;
1129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (s->progressive_sequence) {
1130 if (s->repeat_first_field) {
1131 nofco++;
1132 if (s->top_field_first)
1133 nofco++;
1134 }
1135 } else {
1136
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 18 times.
27 if (s->picture_structure == PICT_FRAME) {
1137 9 nofco++;
1138
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 if (s->repeat_first_field)
1139 6 nofco++;
1140 }
1141 }
1142
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 27 times.
69 for (i = 0; i < nofco; i++) {
1143 42 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1144 42 skip_bits(&s->gb, 1); // marker
1145 42 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1146 42 skip_bits(&s->gb, 1); // marker
1147 }
1148
1149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1150 av_log(s->avctx, AV_LOG_DEBUG,
1151 "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
1152 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1153 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1154 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1155 27 }
1156
1157 223 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
1158 uint16_t matrix1[64], int intra)
1159 {
1160 int i;
1161
1162
2/2
✓ Branch 0 taken 14272 times.
✓ Branch 1 taken 223 times.
14495 for (i = 0; i < 64; i++) {
1163 14272 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1164 14272 int v = get_bits(&s->gb, 8);
1165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14272 times.
14272 if (v == 0) {
1166 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1167 return AVERROR_INVALIDDATA;
1168 }
1169
5/6
✓ Branch 0 taken 4160 times.
✓ Branch 1 taken 10112 times.
✓ Branch 2 taken 65 times.
✓ Branch 3 taken 4095 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 65 times.
14272 if (intra && i == 0 && v != 8) {
1170 av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v);
1171 v = 8; // needed by pink.mpg / issue1046
1172 }
1173 14272 matrix0[j] = v;
1174
2/2
✓ Branch 0 taken 13632 times.
✓ Branch 1 taken 640 times.
14272 if (matrix1)
1175 13632 matrix1[j] = v;
1176 }
1177 223 return 0;
1178 }
1179
1180 977 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1181 {
1182 ff_dlog(s->avctx, "matrix extension\n");
1183
1184
2/2
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 943 times.
977 if (get_bits1(&s->gb))
1185 34 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1186
2/2
✓ Branch 1 taken 73 times.
✓ Branch 2 taken 904 times.
977 if (get_bits1(&s->gb))
1187 73 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1188
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 968 times.
977 if (get_bits1(&s->gb))
1189 9 load_matrix(s, s->chroma_intra_matrix, NULL, 1);
1190
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 976 times.
977 if (get_bits1(&s->gb))
1191 1 load_matrix(s, s->chroma_inter_matrix, NULL, 0);
1192 977 }
1193
1194 4560 static int mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1195 {
1196 4560 MpegEncContext *s = &s1->mpeg_enc_ctx;
1197
1198 4560 s->full_pel[0] = s->full_pel[1] = 0;
1199 4560 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1200 4560 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1201 4560 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1202 4560 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1203 4560 s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0];
1204 4560 s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1];
1205 4560 s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0];
1206 4560 s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1];
1207
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4560 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4560 if (!s->pict_type && s->context_initialized) {
1208 av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code\n");
1209 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1210 return AVERROR_INVALIDDATA;
1211 av_log(s->avctx, AV_LOG_WARNING, "Guessing pict_type from mpeg_f_code\n");
1212 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1213 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1214 s->pict_type = AV_PICTURE_TYPE_I;
1215 else
1216 s->pict_type = AV_PICTURE_TYPE_P;
1217 } else
1218 s->pict_type = AV_PICTURE_TYPE_B;
1219 }
1220
1221 4560 s->intra_dc_precision = get_bits(&s->gb, 2);
1222 4560 s->picture_structure = get_bits(&s->gb, 2);
1223 4560 s->top_field_first = get_bits1(&s->gb);
1224 4560 s->frame_pred_frame_dct = get_bits1(&s->gb);
1225 4560 s->concealment_motion_vectors = get_bits1(&s->gb);
1226 4560 s->q_scale_type = get_bits1(&s->gb);
1227 4560 s->intra_vlc_format = get_bits1(&s->gb);
1228 4560 s->alternate_scan = get_bits1(&s->gb);
1229 4560 s->repeat_first_field = get_bits1(&s->gb);
1230 4560 s->chroma_420_type = get_bits1(&s->gb);
1231 4560 s->progressive_frame = get_bits1(&s->gb);
1232
1233 // We only initialize intra_scantable.permutated, as this is all we use.
1234 4560 ff_permute_scantable(s->intra_scantable.permutated,
1235 4560 s->alternate_scan ? ff_alternate_vertical_scan : ff_zigzag_direct,
1236
2/2
✓ Branch 0 taken 1482 times.
✓ Branch 1 taken 3078 times.
4560 s->idsp.idct_permutation);
1237
1238 /* composite display not parsed */
1239 ff_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1240 ff_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1241 ff_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1242 ff_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1243 ff_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1244 ff_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1245 ff_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1246 ff_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1247 ff_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1248
1249 4560 return 0;
1250 }
1251
1252 4609 static int mpeg_field_start(Mpeg1Context *s1, const uint8_t *buf, int buf_size)
1253 {
1254 4609 MpegEncContext *s = &s1->mpeg_enc_ctx;
1255 4609 AVCodecContext *avctx = s->avctx;
1256 4609 int second_field = 0;
1257 int ret;
1258
1259
1/2
✓ Branch 0 taken 4609 times.
✗ Branch 1 not taken.
4609 if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {
1260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4609 times.
4609 if (s->mb_width * s->mb_height * 11LL / (33 * 2 * 8) > buf_size)
1261 return AVERROR_INVALIDDATA;
1262 }
1263
1264 /* start frame decoding */
1265
4/4
✓ Branch 0 taken 3899 times.
✓ Branch 1 taken 710 times.
✓ Branch 2 taken 3189 times.
✓ Branch 3 taken 710 times.
8508 if (s->first_field || s->picture_structure == PICT_FRAME) {
1266 AVFrameSideData *pan_scan;
1267
1268
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3899 times.
3899 if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
1269 return ret;
1270
1271
2/2
✓ Branch 0 taken 710 times.
✓ Branch 1 taken 3189 times.
3899 if (s->picture_structure != PICT_FRAME) {
1272 710 s->cur_pic.ptr->f->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST *
1273
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 708 times.
710 (s->picture_structure == PICT_TOP_FIELD);
1274
1275
2/2
✓ Branch 0 taken 2130 times.
✓ Branch 1 taken 710 times.
2840 for (int i = 0; i < 3; i++) {
1276
2/2
✓ Branch 0 taken 2124 times.
✓ Branch 1 taken 6 times.
2130 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1277
1/2
✓ Branch 0 taken 2124 times.
✗ Branch 1 not taken.
2124 s->cur_pic.data[i] = FF_PTR_ADD(s->cur_pic.data[i],
1278 s->cur_pic.linesize[i]);
1279 }
1280 2130 s->cur_pic.linesize[i] *= 2;
1281 }
1282 }
1283
1284 3899 ff_mpeg_er_frame_start(s);
1285
1286 /* first check if we must repeat the frame */
1287 3899 s->cur_pic.ptr->f->repeat_pict = 0;
1288
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3893 times.
3899 if (s->repeat_first_field) {
1289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (s->progressive_sequence) {
1290 if (s->top_field_first)
1291 s->cur_pic.ptr->f->repeat_pict = 4;
1292 else
1293 s->cur_pic.ptr->f->repeat_pict = 2;
1294
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 } else if (s->progressive_frame) {
1295 6 s->cur_pic.ptr->f->repeat_pict = 1;
1296 }
1297 }
1298
1299 3899 ret = ff_frame_new_side_data(s->avctx, s->cur_pic.ptr->f,
1300 AV_FRAME_DATA_PANSCAN, sizeof(s1->pan_scan),
1301 &pan_scan);
1302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3899 times.
3899 if (ret < 0)
1303 return ret;
1304
1/2
✓ Branch 0 taken 3899 times.
✗ Branch 1 not taken.
3899 if (pan_scan)
1305 3899 memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
1306
1307
2/2
✓ Branch 0 taken 742 times.
✓ Branch 1 taken 3157 times.
3899 if (s1->a53_buf_ref) {
1308 742 ret = ff_frame_new_side_data_from_buf(
1309 742 s->avctx, s->cur_pic.ptr->f, AV_FRAME_DATA_A53_CC,
1310 &s1->a53_buf_ref);
1311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 742 times.
742 if (ret < 0)
1312 return ret;
1313 }
1314
1315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3899 times.
3899 if (s1->has_stereo3d) {
1316 AVStereo3D *stereo = av_stereo3d_create_side_data(s->cur_pic.ptr->f);
1317 if (!stereo)
1318 return AVERROR(ENOMEM);
1319
1320 stereo->type = s1->stereo3d_type;
1321 s1->has_stereo3d = 0;
1322 }
1323
1324
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 3697 times.
3899 if (s1->has_afd) {
1325 AVFrameSideData *sd;
1326 202 ret = ff_frame_new_side_data(s->avctx, s->cur_pic.ptr->f,
1327 AV_FRAME_DATA_AFD, 1, &sd);
1328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
202 if (ret < 0)
1329 return ret;
1330
1/2
✓ Branch 0 taken 202 times.
✗ Branch 1 not taken.
202 if (sd)
1331 202 *sd->data = s1->afd;
1332 202 s1->has_afd = 0;
1333 }
1334
1335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3899 times.
3899 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1336 ff_thread_finish_setup(avctx);
1337 } else { // second field
1338 710 second_field = 1;
1339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 710 times.
710 if (!s->cur_pic.ptr) {
1340 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1341 return AVERROR_INVALIDDATA;
1342 }
1343
1344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 710 times.
710 if (s->avctx->hwaccel) {
1345 if ((ret = FF_HW_SIMPLE_CALL(s->avctx, end_frame)) < 0) {
1346 av_log(avctx, AV_LOG_ERROR,
1347 "hardware accelerator failed to decode first field\n");
1348 return ret;
1349 }
1350 }
1351 710 ret = ff_mpv_alloc_dummy_frames(s);
1352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 710 times.
710 if (ret < 0)
1353 return ret;
1354
1355
2/2
✓ Branch 0 taken 2130 times.
✓ Branch 1 taken 710 times.
2840 for (int i = 0; i < 3; i++) {
1356 2130 s->cur_pic.data[i] = s->cur_pic.ptr->f->data[i];
1357
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2124 times.
2130 if (s->picture_structure == PICT_BOTTOM_FIELD)
1358 6 s->cur_pic.data[i] +=
1359 6 s->cur_pic.ptr->f->linesize[i];
1360 }
1361 }
1362
1363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4609 times.
4609 if (avctx->hwaccel) {
1364 if ((ret = FF_HW_CALL(avctx, start_frame, buf, buf_size)) < 0)
1365 return ret;
1366
2/2
✓ Branch 0 taken 157 times.
✓ Branch 1 taken 4452 times.
4609 } else if (s->codec_tag == MKTAG('V', 'C', 'R', '2')) {
1367 // Exchange UV
1368 157 FFSWAP(uint8_t*, s->cur_pic.data[1], s->cur_pic.data[2]);
1369 157 FFSWAP(ptrdiff_t, s->cur_pic.linesize[1], s->cur_pic.linesize[2]);
1370
1/2
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
157 if (!second_field) {
1371 157 FFSWAP(uint8_t*, s->next_pic.data[1], s->next_pic.data[2]);
1372 157 FFSWAP(ptrdiff_t, s->next_pic.linesize[1], s->next_pic.linesize[2]);
1373 157 FFSWAP(uint8_t*, s->last_pic.data[1], s->last_pic.data[2]);
1374 157 FFSWAP(ptrdiff_t, s->last_pic.linesize[1], s->last_pic.linesize[2]);
1375 }
1376 }
1377
1378 4609 return 0;
1379 }
1380
1381 #define DECODE_SLICE_ERROR -1
1382 #define DECODE_SLICE_OK 0
1383
1384 /**
1385 * Decode a slice.
1386 * MpegEncContext.mb_y must be set to the MB row from the startcode.
1387 * @return DECODE_SLICE_ERROR if the slice is damaged,
1388 * DECODE_SLICE_OK if this slice is OK
1389 */
1390 127107 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1391 const uint8_t **buf, int buf_size)
1392 {
1393 127107 AVCodecContext *avctx = s->avctx;
1394 127107 const int lowres = s->avctx->lowres;
1395 127107 const int field_pic = s->picture_structure != PICT_FRAME;
1396 int ret;
1397
1398 127107 s->resync_mb_x =
1399 127107 s->resync_mb_y = -1;
1400
1401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127107 times.
127107 av_assert0(mb_y < s->mb_height);
1402
1403 127107 ret = init_get_bits8(&s->gb, *buf, buf_size);
1404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127107 times.
127107 if (ret < 0)
1405 return ret;
1406
1407
3/4
✓ Branch 0 taken 119834 times.
✓ Branch 1 taken 7273 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 119834 times.
127107 if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
1408 skip_bits(&s->gb, 3);
1409
1410 127107 ff_mpeg1_clean_buffers(s);
1411 127107 s->interlaced_dct = 0;
1412
1413 127107 s->qscale = mpeg_get_qscale(s);
1414
1415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127107 times.
127107 if (s->qscale == 0) {
1416 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1417 return AVERROR_INVALIDDATA;
1418 }
1419
1420 /* extra slice info */
1421
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 127107 times.
127107 if (skip_1stop_8data_bits(&s->gb) < 0)
1422 return AVERROR_INVALIDDATA;
1423
1424 127107 s->mb_x = 0;
1425
1426
3/4
✓ Branch 0 taken 5394 times.
✓ Branch 1 taken 121713 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5394 times.
127107 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1427 skip_bits1(&s->gb);
1428 } else {
1429
1/2
✓ Branch 1 taken 140105 times.
✗ Branch 2 not taken.
267212 while (get_bits_left(&s->gb) > 0) {
1430 140105 int code = get_vlc2(&s->gb, ff_mbincr_vlc,
1431 MBINCR_VLC_BITS, 2);
1432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140105 times.
140105 if (code < 0) {
1433 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1434 return AVERROR_INVALIDDATA;
1435 }
1436
2/2
✓ Branch 0 taken 12998 times.
✓ Branch 1 taken 127107 times.
140105 if (code >= 33) {
1437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12998 times.
12998 if (code == 33)
1438 12998 s->mb_x += 33;
1439 /* otherwise, stuffing, nothing to do */
1440 } else {
1441 127107 s->mb_x += code;
1442 127107 break;
1443 }
1444 }
1445 }
1446
1447
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127107 times.
127107 if (s->mb_x >= (unsigned) s->mb_width) {
1448 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1449 return AVERROR_INVALIDDATA;
1450 }
1451
1452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127107 times.
127107 if (avctx->hwaccel) {
1453 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1454 int start_code = -1;
1455 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1456 if (buf_end < *buf + buf_size)
1457 buf_end -= 4;
1458 s->mb_y = mb_y;
1459 if (FF_HW_CALL(avctx, decode_slice, buf_start, buf_end - buf_start) < 0)
1460 return DECODE_SLICE_ERROR;
1461 *buf = buf_end;
1462 return DECODE_SLICE_OK;
1463 }
1464
1465 127107 s->resync_mb_x = s->mb_x;
1466 127107 s->resync_mb_y = s->mb_y = mb_y;
1467 127107 s->mb_skip_run = 0;
1468 127107 ff_init_block_index(s);
1469
1470
8/8
✓ Branch 0 taken 5394 times.
✓ Branch 1 taken 121713 times.
✓ Branch 2 taken 3899 times.
✓ Branch 3 taken 1495 times.
✓ Branch 4 taken 3897 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 3189 times.
✓ Branch 7 taken 708 times.
127107 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3191 times.
3191 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1472 av_log(s->avctx, AV_LOG_DEBUG,
1473 "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",
1474 s->qscale,
1475 s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
1476 s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1477 s->pict_type == AV_PICTURE_TYPE_I ? 'I' :
1478 (s->pict_type == AV_PICTURE_TYPE_P ? 'P' :
1479 (s->pict_type == AV_PICTURE_TYPE_B ? 'B' : 'S')),
1480 s->progressive_sequence ? "ps" : "",
1481 s->progressive_frame ? "pf" : "",
1482 s->alternate_scan ? "alt" : "",
1483 s->top_field_first ? "top" : "",
1484 s->intra_dc_precision, s->picture_structure,
1485 s->frame_pred_frame_dct, s->concealment_motion_vectors,
1486 s->q_scale_type, s->intra_vlc_format,
1487 s->repeat_first_field, s->chroma_420_type ? "420" : "");
1488 }
1489 }
1490
1491 for (;;) {
1492
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 2602080 times.
2602090 if ((ret = mpeg_decode_mb(s, s->block)) < 0)
1493 10 return ret;
1494
1495 // Note motion_val is normally NULL unless we want to extract the MVs.
1496
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2602080 times.
2602080 if (s->cur_pic.motion_val[0]) {
1497 const int wrap = s->b8_stride;
1498 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1499 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1500 int motion_x, motion_y, dir, i;
1501
1502 for (i = 0; i < 2; i++) {
1503 for (dir = 0; dir < 2; dir++) {
1504 if (s->mb_intra ||
1505 (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1506 motion_x = motion_y = 0;
1507 } else if (s->mv_type == MV_TYPE_16X16 ||
1508 (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1509 motion_x = s->mv[dir][0][0];
1510 motion_y = s->mv[dir][0][1];
1511 } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
1512 motion_x = s->mv[dir][i][0];
1513 motion_y = s->mv[dir][i][1];
1514 }
1515
1516 s->cur_pic.motion_val[dir][xy][0] = motion_x;
1517 s->cur_pic.motion_val[dir][xy][1] = motion_y;
1518 s->cur_pic.motion_val[dir][xy + 1][0] = motion_x;
1519 s->cur_pic.motion_val[dir][xy + 1][1] = motion_y;
1520 s->cur_pic.ref_index [dir][b8_xy] =
1521 s->cur_pic.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1522 av_assert2(s->field_select[dir][i] == 0 ||
1523 s->field_select[dir][i] == 1);
1524 }
1525 xy += wrap;
1526 b8_xy += 2;
1527 }
1528 }
1529
1530 2602080 s->dest[0] += 16 >> lowres;
1531 2602080 s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
1532 2602080 s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
1533
1534 2602080 ff_mpv_reconstruct_mb(s, s->block);
1535
1536
2/2
✓ Branch 0 taken 79196 times.
✓ Branch 1 taken 2522884 times.
2602080 if (++s->mb_x >= s->mb_width) {
1537 79196 const int mb_size = 16 >> s->avctx->lowres;
1538 int left;
1539
1540 79196 ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
1541 79196 ff_mpv_report_decode_progress(s);
1542
1543 79196 s->mb_x = 0;
1544 79196 s->mb_y += 1 << field_pic;
1545
1546
2/2
✓ Branch 0 taken 4599 times.
✓ Branch 1 taken 74597 times.
79196 if (s->mb_y >= s->mb_height) {
1547 4599 int left = get_bits_left(&s->gb);
1548 9459 int is_d10 = s->chroma_format == CHROMA_422 &&
1549
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 212 times.
261 s->pict_type == AV_PICTURE_TYPE_I &&
1550
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 &&
1551
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 24 times.
49 s->intra_dc_precision == 2 &&
1552
4/6
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 4338 times.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25 times.
✗ Branch 5 not taken.
4885 s->q_scale_type == 1 && s->alternate_scan == 0 &&
1553
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 s->progressive_frame == 0
1554 /* vbv_delay == 0xBBB || 0xE10 */;
1555
1556
4/4
✓ Branch 0 taken 1466 times.
✓ Branch 1 taken 3133 times.
✓ Branch 2 taken 1441 times.
✓ Branch 3 taken 25 times.
4599 if (left >= 32 && !is_d10) {
1557 1441 GetBitContext gb = s->gb;
1558 1441 align_get_bits(&gb);
1559
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1441 times.
1441 if (show_bits(&gb, 24) == 0x060E2B) {
1560 av_log(avctx, AV_LOG_DEBUG, "Invalid MXF data found in video stream\n");
1561 is_d10 = 1;
1562 }
1563
3/4
✓ Branch 0 taken 1440 times.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1440 times.
1441 if (left > 32 && show_bits_long(&gb, 32) == 0x201) {
1564 av_log(avctx, AV_LOG_DEBUG, "skipping m704 alpha (unsupported)\n");
1565 goto eos;
1566 }
1567 }
1568
1569
3/4
✓ Branch 0 taken 4599 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4297 times.
✓ Branch 3 taken 302 times.
4599 if (left < 0 ||
1570
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 4297 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
4297 (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
1571
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4599 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4599 ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) {
1572 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X at %d %d\n",
1573 left, left>0 ? show_bits(&s->gb, FFMIN(left, 23)) : 0, s->mb_x, s->mb_y);
1574 return AVERROR_INVALIDDATA;
1575 } else
1576 4599 goto eos;
1577 }
1578 // There are some files out there which are missing the last slice
1579 // in cases where the slice is completely outside the visible
1580 // area, we detect this here instead of running into the end expecting
1581 // more data
1582 74597 left = get_bits_left(&s->gb);
1583
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 74347 times.
74597 if (s->mb_y >= ((s->height + 15) >> 4) &&
1584
2/4
✓ Branch 0 taken 250 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 250 times.
250 !s->progressive_sequence &&
1585 left <= 25 &&
1586 left >= 0 &&
1587 s->mb_skip_run == -1 &&
1588 (!left || show_bits(&s->gb, left) == 0))
1589 goto eos;
1590
1591 74597 ff_init_block_index(s);
1592 }
1593
1594 /* skip mb handling */
1595
2/2
✓ Branch 0 taken 2432053 times.
✓ Branch 1 taken 165428 times.
2597481 if (s->mb_skip_run == -1) {
1596 /* read increment again */
1597 2432053 s->mb_skip_run = 0;
1598 1230 for (;;) {
1599 2433283 int code = get_vlc2(&s->gb, ff_mbincr_vlc,
1600 MBINCR_VLC_BITS, 2);
1601
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2433283 times.
2433283 if (code < 0) {
1602 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1603 return AVERROR_INVALIDDATA;
1604 }
1605
2/2
✓ Branch 0 taken 123728 times.
✓ Branch 1 taken 2309555 times.
2433283 if (code >= 33) {
1606
2/2
✓ Branch 0 taken 1230 times.
✓ Branch 1 taken 122498 times.
123728 if (code == 33) {
1607 1230 s->mb_skip_run += 33;
1608
1/2
✓ Branch 0 taken 122498 times.
✗ Branch 1 not taken.
122498 } else if (code == 35) {
1609
2/4
✓ Branch 0 taken 122498 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 122498 times.
122498 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1610 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1611 return AVERROR_INVALIDDATA;
1612 }
1613 122498 goto eos; /* end of slice */
1614 }
1615 /* otherwise, stuffing, nothing to do */
1616 } else {
1617 2309555 s->mb_skip_run += code;
1618 2309555 break;
1619 }
1620 }
1621
2/2
✓ Branch 0 taken 61947 times.
✓ Branch 1 taken 2247608 times.
2309555 if (s->mb_skip_run) {
1622 int i;
1623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61947 times.
61947 if (s->pict_type == AV_PICTURE_TYPE_I) {
1624 av_log(s->avctx, AV_LOG_ERROR,
1625 "skipped MB in I-frame at %d %d\n", s->mb_x, s->mb_y);
1626 return AVERROR_INVALIDDATA;
1627 }
1628
1629 /* skip mb */
1630 61947 s->mb_intra = 0;
1631
2/2
✓ Branch 0 taken 743364 times.
✓ Branch 1 taken 61947 times.
805311 for (i = 0; i < 12; i++)
1632 743364 s->block_last_index[i] = -1;
1633 61947 s->last_dc[0] = s->last_dc[1] = s->last_dc[2] = 128 << s->intra_dc_precision;
1634
2/2
✓ Branch 0 taken 42242 times.
✓ Branch 1 taken 19705 times.
61947 if (s->picture_structure == PICT_FRAME)
1635 42242 s->mv_type = MV_TYPE_16X16;
1636 else
1637 19705 s->mv_type = MV_TYPE_FIELD;
1638
2/2
✓ Branch 0 taken 23749 times.
✓ Branch 1 taken 38198 times.
61947 if (s->pict_type == AV_PICTURE_TYPE_P) {
1639 /* if P type, zero motion vector is implied */
1640 23749 s->mv_dir = MV_DIR_FORWARD;
1641 23749 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1642 23749 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1643 23749 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1644 23749 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1645 } else {
1646 /* if B type, reuse previous vectors and directions */
1647 38198 s->mv[0][0][0] = s->last_mv[0][0][0];
1648 38198 s->mv[0][0][1] = s->last_mv[0][0][1];
1649 38198 s->mv[1][0][0] = s->last_mv[1][0][0];
1650 38198 s->mv[1][0][1] = s->last_mv[1][0][1];
1651 38198 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1652 38198 s->field_select[1][0] = (s->picture_structure - 1) & 1;
1653 }
1654 }
1655 }
1656 }
1657 127097 eos: // end of slice
1658
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 127097 times.
127097 if (get_bits_left(&s->gb) < 0) {
1659 av_log(s->avctx, AV_LOG_ERROR, "overread %d\n", -get_bits_left(&s->gb));
1660 return AVERROR_INVALIDDATA;
1661 }
1662 127097 *buf += (get_bits_count(&s->gb) - 1) / 8;
1663 ff_dlog(s->avctx, "Slice start:%d %d end:%d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1664 127097 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 4186 static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
1721 {
1722 4186 Mpeg1Context *s1 = avctx->priv_data;
1723 4186 MpegEncContext *s = &s1->mpeg_enc_ctx;
1724
1725
3/4
✓ Branch 0 taken 3900 times.
✓ Branch 1 taken 286 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3900 times.
4186 if (!s->context_initialized || !s->cur_pic.ptr)
1726 286 return 0;
1727
1728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3900 times.
3900 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 3899 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3899 times.
✗ Branch 3 not taken.
3900 if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field && !s1->first_slice) {
1739 /* end of image */
1740
1741 3899 ff_er_frame_end(&s->er, NULL);
1742
1743 3899 ff_mpv_frame_end(s);
1744
1745
4/4
✓ Branch 0 taken 2481 times.
✓ Branch 1 taken 1418 times.
✓ Branch 2 taken 533 times.
✓ Branch 3 taken 1948 times.
3899 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
1746 1951 int ret = av_frame_ref(pict, s->cur_pic.ptr->f);
1747
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1951 times.
1951 if (ret < 0)
1748 return ret;
1749 1951 ff_print_debug_info(s, s->cur_pic.ptr, pict);
1750 1951 ff_mpv_export_qp_table(s, pict, s->cur_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG2);
1751 1951 *got_output = 1;
1752 } else {
1753 /* latency of 1 frame for I- and P-frames */
1754
4/4
✓ Branch 0 taken 1889 times.
✓ Branch 1 taken 59 times.
✓ Branch 2 taken 1886 times.
✓ Branch 3 taken 3 times.
1948 if (s->last_pic.ptr && !s->last_pic.ptr->dummy) {
1755 1886 int ret = av_frame_ref(pict, s->last_pic.ptr->f);
1756
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1886 times.
1886 if (ret < 0)
1757 return ret;
1758 1886 ff_print_debug_info(s, s->last_pic.ptr, pict);
1759 1886 ff_mpv_export_qp_table(s, pict, s->last_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG2);
1760 1886 *got_output = 1;
1761 }
1762 }
1763
1764 3899 return 1;
1765 } else {
1766 1 return 0;
1767 }
1768 }
1769
1770 809 static int mpeg1_decode_sequence(AVCodecContext *avctx,
1771 const uint8_t *buf, int buf_size)
1772 {
1773 809 Mpeg1Context *s1 = avctx->priv_data;
1774 809 MpegEncContext *s = &s1->mpeg_enc_ctx;
1775 int width, height;
1776 int i, v, j;
1777
1778 809 int ret = init_get_bits8(&s->gb, buf, buf_size);
1779
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 809 times.
809 if (ret < 0)
1780 return ret;
1781
1782 809 width = get_bits(&s->gb, 12);
1783 809 height = get_bits(&s->gb, 12);
1784
2/4
✓ Branch 0 taken 809 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 809 times.
809 if (width == 0 || height == 0) {
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 809 s1->aspect_ratio_info = get_bits(&s->gb, 4);
1791
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 809 times.
809 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 809 s1->frame_rate_index = get_bits(&s->gb, 4);
1797
2/4
✓ Branch 0 taken 809 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 809 times.
809 if (s1->frame_rate_index == 0 || s1->frame_rate_index > 13) {
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 809 s->bit_rate = get_bits(&s->gb, 18) * 400LL;
1803
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 809 times.
809 if (check_marker(s->avctx, &s->gb, "in sequence header") == 0) {
1804 return AVERROR_INVALIDDATA;
1805 }
1806
1807 809 s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
1808 809 skip_bits(&s->gb, 1);
1809
1810 /* get matrix */
1811
2/2
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 787 times.
809 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 50368 times.
✓ Branch 1 taken 787 times.
51155 for (i = 0; i < 64; i++) {
1815 50368 j = s->idsp.idct_permutation[i];
1816 50368 v = ff_mpeg1_default_intra_matrix[i];
1817 50368 s->intra_matrix[j] = v;
1818 50368 s->chroma_intra_matrix[j] = v;
1819 }
1820 }
1821
2/2
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 725 times.
809 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 46400 times.
✓ Branch 1 taken 725 times.
47125 for (i = 0; i < 64; i++) {
1825 46400 int j = s->idsp.idct_permutation[i];
1826 46400 v = ff_mpeg1_default_non_intra_matrix[i];
1827 46400 s->inter_matrix[j] = v;
1828 46400 s->chroma_inter_matrix[j] = v;
1829 }
1830 }
1831
1832
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 809 times.
809 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 809 s->width = width;
1838 809 s->height = height;
1839
1840 /* We set MPEG-2 parameters so that it emulates MPEG-1. */
1841 809 s->progressive_sequence = 1;
1842 809 s->progressive_frame = 1;
1843 809 s->picture_structure = PICT_FRAME;
1844 809 s->first_field = 0;
1845 809 s->frame_pred_frame_dct = 1;
1846 809 s->chroma_format = CHROMA_420;
1847 809 s->codec_id =
1848 809 s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
1849
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 809 times.
809 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 809 times.
809 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 809 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/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int i = 0; i < s->slice_context_count; i++)
1880 2 ff_mpv_framesize_disable(&s->thread_context[i]->sc);
1881
1882
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 2 times.
130 for (i = 0; i < 64; i++) {
1883 128 int j = s->idsp.idct_permutation[i];
1884 128 v = ff_mpeg1_default_intra_matrix[i];
1885 128 s->intra_matrix[j] = v;
1886 128 s->chroma_intra_matrix[j] = v;
1887
1888 128 v = ff_mpeg1_default_non_intra_matrix[i];
1889 128 s->inter_matrix[j] = v;
1890 128 s->chroma_inter_matrix[j] = v;
1891 }
1892
1893 2 s->progressive_sequence = 1;
1894 2 s->progressive_frame = 1;
1895 2 s->picture_structure = PICT_FRAME;
1896 2 s->first_field = 0;
1897 2 s->frame_pred_frame_dct = 1;
1898 2 s->chroma_format = CHROMA_420;
1899
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->codec_tag == AV_RL32("BW10")) {
1900 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
1901 } else {
1902 2 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1903 }
1904 2 s1->save_width = s->width;
1905 2 s1->save_height = s->height;
1906 2 s1->save_progressive_seq = s->progressive_sequence;
1907 2 return 0;
1908 }
1909
1910 758 static void mpeg_set_cc_format(AVCodecContext *avctx, enum Mpeg2ClosedCaptionsFormat format,
1911 const char *label)
1912 {
1913 758 Mpeg1Context *s1 = avctx->priv_data;
1914
1915 av_assert2(format != CC_FORMAT_AUTO);
1916
1917
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 747 times.
758 if (!s1->cc_format) {
1918 11 s1->cc_format = format;
1919
1920 11 av_log(avctx, AV_LOG_DEBUG, "CC: first seen substream is %s format\n", label);
1921 }
1922
1923 #if FF_API_CODEC_PROPS
1924 FF_DISABLE_DEPRECATION_WARNINGS
1925 758 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
1926 FF_ENABLE_DEPRECATION_WARNINGS
1927 #endif
1928 758 }
1929
1930 830 static int mpeg_decode_a53_cc(AVCodecContext *avctx,
1931 const uint8_t *p, int buf_size)
1932 {
1933 830 Mpeg1Context *s1 = avctx->priv_data;
1934
1935
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) &&
1936 627 buf_size >= 6 &&
1937
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' &&
1938
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)) {
1939 /* extract A53 Part 4 CC data */
1940 553 int cc_count = p[5] & 0x1f;
1941
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) {
1942
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;
1943 553 const uint64_t new_size = (old_size + cc_count
1944 553 * UINT64_C(3));
1945 int ret;
1946
1947
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 553 times.
553 if (new_size > 3*A53_MAX_CC_COUNT)
1948 return AVERROR(EINVAL);
1949
1950 553 ret = av_buffer_realloc(&s1->a53_buf_ref, new_size);
1951
1/2
✓ Branch 0 taken 553 times.
✗ Branch 1 not taken.
553 if (ret >= 0)
1952 553 memcpy(s1->a53_buf_ref->data + old_size, p + 7, cc_count * UINT64_C(3));
1953
1954 553 mpeg_set_cc_format(avctx, CC_FORMAT_A53_PART4, "A/53 Part 4");
1955 }
1956 553 return 1;
1957
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) &&
1958 277 buf_size >= 2 &&
1959
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) {
1960 /* extract SCTE-20 CC data */
1961 GetBitContext gb;
1962 205 int cc_count = 0;
1963 int i, ret;
1964
1965 205 ret = init_get_bits8(&gb, p + 2, buf_size - 2);
1966
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 205 times.
205 if (ret < 0)
1967 return ret;
1968 205 cc_count = get_bits(&gb, 5);
1969
1/2
✓ Branch 0 taken 205 times.
✗ Branch 1 not taken.
205 if (cc_count > 0) {
1970
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;
1971 205 const uint64_t new_size = (old_size + cc_count
1972 205 * UINT64_C(3));
1973
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 205 times.
205 if (new_size > 3*A53_MAX_CC_COUNT)
1974 return AVERROR(EINVAL);
1975
1976 205 ret = av_buffer_realloc(&s1->a53_buf_ref, new_size);
1977
1/2
✓ Branch 0 taken 205 times.
✗ Branch 1 not taken.
205 if (ret >= 0) {
1978 uint8_t field, cc1, cc2;
1979 205 uint8_t *cap = s1->a53_buf_ref->data + old_size;
1980
1981 205 memset(cap, 0, cc_count * 3);
1982
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++) {
1983 410 skip_bits(&gb, 2); // priority
1984 410 field = get_bits(&gb, 2);
1985 410 skip_bits(&gb, 5); // line_offset
1986 410 cc1 = get_bits(&gb, 8);
1987 410 cc2 = get_bits(&gb, 8);
1988 410 skip_bits(&gb, 1); // marker
1989
1990
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410 times.
410 if (!field) { // forbidden
1991 cap[0] = cap[1] = cap[2] = 0x00;
1992 } else {
1993 410 field = (field == 2 ? 1 : 0);
1994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410 times.
410 if (!s1->mpeg_enc_ctx.top_field_first) field = !field;
1995 410 cap[0] = 0x04 | field;
1996 410 cap[1] = ff_reverse[cc1];
1997 410 cap[2] = ff_reverse[cc2];
1998 }
1999 410 cap += 3;
2000 }
2001 }
2002
2003 205 mpeg_set_cc_format(avctx, CC_FORMAT_SCTE20, "SCTE-20");
2004 }
2005 205 return 1;
2006
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) &&
2007 72 buf_size >= 11 &&
2008
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) {
2009 /* extract DVD CC data
2010 *
2011 * uint32_t user_data_start_code 0x000001B2 (big endian)
2012 * uint16_t user_identifier 0x4343 "CC"
2013 * uint8_t user_data_type_code 0x01
2014 * uint8_t caption_block_size 0xF8
2015 * uint8_t
2016 * bit 7 caption_odd_field_first 1=odd field (CC1/CC2) first 0=even field (CC3/CC4) first
2017 * bit 6 caption_filler 0
2018 * bit 5:1 caption_block_count number of caption blocks (pairs of caption words = frames). Most DVDs use 15 per start of GOP.
2019 * bit 0 caption_extra_field_added 1=one additional caption word
2020 *
2021 * struct caption_field_block {
2022 * uint8_t
2023 * bit 7:1 caption_filler 0x7F (all 1s)
2024 * bit 0 caption_field_odd 1=odd field (this is CC1/CC2) 0=even field (this is CC3/CC4)
2025 * uint8_t caption_first_byte
2026 * uint8_t caption_second_byte
2027 * } caption_block[(caption_block_count * 2) + caption_extra_field_added];
2028 *
2029 * Some DVDs encode caption data for both fields with caption_field_odd=1. The only way to decode the fields
2030 * correctly is to start on the field indicated by caption_odd_field_first and count between odd/even fields.
2031 * Don't assume that the first caption word is the odd field. There do exist MPEG files in the wild that start
2032 * on the even field. There also exist DVDs in the wild that encode an odd field count and the
2033 * caption_extra_field_added/caption_odd_field_first bits change per packet to allow that. */
2034 int cc_count = 0;
2035 int i, ret;
2036 // There is a caption count field in the data, but it is often
2037 // incorrect. So count the number of captions present.
2038 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2039 cc_count++;
2040 // Transform the DVD format into A53 Part 4 format
2041 if (cc_count > 0) {
2042 int old_size = s1->a53_buf_ref ? s1->a53_buf_ref->size : 0;
2043 const uint64_t new_size = (old_size + cc_count
2044 * UINT64_C(6));
2045 if (new_size > 3*A53_MAX_CC_COUNT)
2046 return AVERROR(EINVAL);
2047
2048 ret = av_buffer_realloc(&s1->a53_buf_ref, new_size);
2049 if (ret >= 0) {
2050 uint8_t field1 = !!(p[4] & 0x80);
2051 uint8_t *cap = s1->a53_buf_ref->data + old_size;
2052 p += 5;
2053 for (i = 0; i < cc_count; i++) {
2054 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2055 cap[1] = p[1];
2056 cap[2] = p[2];
2057 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2058 cap[4] = p[4];
2059 cap[5] = p[5];
2060 cap += 6;
2061 p += 6;
2062 }
2063 }
2064
2065 mpeg_set_cc_format(avctx, CC_FORMAT_DVD, "DVD");
2066 }
2067 return 1;
2068
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 72 times.
✗ Branch 5 not taken.
72 } else if ((!s1->cc_format || s1->cc_format == CC_FORMAT_DISH) &&
2069 72 buf_size >= 12 &&
2070
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) {
2071 /* extract Dish Network CC data */
2072 const uint8_t cc_header = 0xf8 | 0x04 /* valid */ | 0x00 /* line 21 field 1 */;
2073 uint8_t cc_data[4] = {0};
2074 int cc_count = 0;
2075 uint8_t cc_type = p[7];
2076 p += 8;
2077 buf_size -= 8;
2078
2079 if (cc_type == 0x05 && buf_size >= 7) {
2080 cc_type = p[6];
2081 p += 7;
2082 buf_size -= 7;
2083 }
2084
2085 if (cc_type == 0x02 && buf_size >= 4) { /* 2-byte caption, can be repeated */
2086 cc_count = 1;
2087 cc_data[0] = p[1];
2088 cc_data[1] = p[2];
2089 cc_type = p[3];
2090
2091 /* Only repeat characters when the next type flag
2092 * is 0x04 and the characters are repeatable (i.e., less than
2093 * 32 with the parity stripped).
2094 */
2095 if (cc_type == 0x04 && (cc_data[0] & 0x7f) < 32) {
2096 cc_count = 2;
2097 cc_data[2] = cc_data[0];
2098 cc_data[3] = cc_data[1];
2099 }
2100 } else if (cc_type == 0x04 && buf_size >= 5) { /* 4-byte caption, not repeated */
2101 cc_count = 2;
2102 cc_data[0] = p[1];
2103 cc_data[1] = p[2];
2104 cc_data[2] = p[3];
2105 cc_data[3] = p[4];
2106 }
2107
2108 if (cc_count > 0) {
2109 int ret;
2110 int old_size = s1->a53_buf_ref ? s1->a53_buf_ref->size : 0;
2111 const uint64_t new_size = (old_size + cc_count * UINT64_C(3));
2112 if (new_size > 3 * A53_MAX_CC_COUNT)
2113 return AVERROR(EINVAL);
2114
2115 ret = av_buffer_realloc(&s1->a53_buf_ref, new_size);
2116 if (ret >= 0) {
2117 uint8_t *cap = s1->a53_buf_ref->data + old_size;
2118 cap[0] = cc_header;
2119 cap[1] = cc_data[0];
2120 cap[2] = cc_data[1];
2121 if (cc_count == 2) {
2122 cap[3] = cc_header;
2123 cap[4] = cc_data[2];
2124 cap[5] = cc_data[3];
2125 }
2126 }
2127
2128 mpeg_set_cc_format(avctx, CC_FORMAT_DISH, "Dish Network");
2129 }
2130 return 1;
2131 }
2132 72 return 0;
2133 }
2134
2135 1037 static void mpeg_decode_user_data(AVCodecContext *avctx,
2136 const uint8_t *p, int buf_size)
2137 {
2138 1037 Mpeg1Context *s = avctx->priv_data;
2139 1037 const uint8_t *buf_end = p + buf_size;
2140 1037 Mpeg1Context *s1 = avctx->priv_data;
2141
2142 #if 0
2143 int i;
2144 for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
2145 av_log(avctx, AV_LOG_ERROR, "%c", p[i]);
2146 }
2147 av_log(avctx, AV_LOG_ERROR, "\n");
2148 #endif
2149
2150
1/2
✓ Branch 0 taken 1037 times.
✗ Branch 1 not taken.
1037 if (buf_size > 29){
2151 int i;
2152
2/2
✓ Branch 0 taken 20740 times.
✓ Branch 1 taken 1037 times.
21777 for(i=0; i<20; i++)
2153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20740 times.
20740 if (!memcmp(p+i, "\0TMPGEXS\0", 9)){
2154 s->tmpgexs= 1;
2155 }
2156 }
2157 /* we parse the DTG active format information */
2158
1/2
✓ Branch 0 taken 1037 times.
✗ Branch 1 not taken.
1037 if (buf_end - p >= 5 &&
2159
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') {
2160 207 int flags = p[4];
2161 207 p += 5;
2162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 207 times.
207 if (flags & 0x80) {
2163 /* skip event id */
2164 p += 2;
2165 }
2166
1/2
✓ Branch 0 taken 207 times.
✗ Branch 1 not taken.
207 if (flags & 0x40) {
2167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 207 times.
207 if (buf_end - p < 1)
2168 return;
2169 207 s1->has_afd = 1;
2170 207 s1->afd = p[0] & 0x0f;
2171 }
2172
1/2
✓ Branch 0 taken 830 times.
✗ Branch 1 not taken.
830 } else if (buf_end - p >= 6 &&
2173
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' &&
2174 p[4] == 0x03) { // S3D_video_format_length
2175 // the 0x7F mask ignores the reserved_bit value
2176 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2177
2178 if (S3D_video_format_type == 0x03 ||
2179 S3D_video_format_type == 0x04 ||
2180 S3D_video_format_type == 0x08 ||
2181 S3D_video_format_type == 0x23) {
2182
2183 s1->has_stereo3d = 1;
2184
2185 switch (S3D_video_format_type) {
2186 case 0x03:
2187 s1->stereo3d_type = AV_STEREO3D_SIDEBYSIDE;
2188 break;
2189 case 0x04:
2190 s1->stereo3d_type = AV_STEREO3D_TOPBOTTOM;
2191 break;
2192 case 0x08:
2193 s1->stereo3d_type = AV_STEREO3D_2D;
2194 break;
2195 case 0x23:
2196 s1->stereo3d_type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2197 break;
2198 }
2199 }
2200
2/2
✓ Branch 1 taken 758 times.
✓ Branch 2 taken 72 times.
830 } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
2201 758 return;
2202 }
2203 }
2204
2205 560 static int mpeg_decode_gop(AVCodecContext *avctx,
2206 const uint8_t *buf, int buf_size)
2207 {
2208 560 Mpeg1Context *s1 = avctx->priv_data;
2209 560 MpegEncContext *s = &s1->mpeg_enc_ctx;
2210 int broken_link;
2211 int64_t tc;
2212
2213 560 int ret = init_get_bits8(&s->gb, buf, buf_size);
2214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 560 times.
560 if (ret < 0)
2215 return ret;
2216
2217 560 tc = s1->timecode_frame_start = get_bits(&s->gb, 25);
2218
2219 560 s1->closed_gop = get_bits1(&s->gb);
2220 /* broken_link indicates that after editing the
2221 * reference frames of the first B-Frames after GOP I-Frame
2222 * are missing (open gop) */
2223 560 broken_link = get_bits1(&s->gb);
2224
2225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 560 times.
560 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2226 char tcbuf[AV_TIMECODE_STR_SIZE];
2227 av_timecode_make_mpeg_tc_string(tcbuf, tc);
2228 av_log(s->avctx, AV_LOG_DEBUG,
2229 "GOP (%s) closed_gop=%d broken_link=%d\n",
2230 tcbuf, s1->closed_gop, broken_link);
2231 }
2232
2233 560 return 0;
2234 }
2235
2236 4675 static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
2237 int *got_output, const uint8_t *buf, int buf_size)
2238 {
2239 4675 Mpeg1Context *s = avctx->priv_data;
2240 4675 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2241 4675 const uint8_t *buf_ptr = buf;
2242 4675 const uint8_t *buf_end = buf + buf_size;
2243 int ret, input_size;
2244 4675 int last_code = 0, skip_frame = 0;
2245 4675 int picture_start_code_seen = 0;
2246
2247 224269 for (;;) {
2248 /* find next start code */
2249 228944 uint32_t start_code = -1;
2250 228944 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
2251
2/2
✓ Branch 0 taken 4505 times.
✓ Branch 1 taken 224439 times.
228944 if (start_code > 0x1ff) {
2252
2/2
✓ Branch 0 taken 4186 times.
✓ Branch 1 taken 319 times.
4505 if (!skip_frame) {
2253 4186 if (HAVE_THREADS &&
2254
2/2
✓ Branch 0 taken 747 times.
✓ Branch 1 taken 3439 times.
4186 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2255
1/2
✓ Branch 0 taken 747 times.
✗ Branch 1 not taken.
747 !avctx->hwaccel) {
2256 747 int error_count = 0;
2257 int i;
2258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 747 times.
747 av_assert0(avctx->thread_count > 1);
2259
2260 747 avctx->execute(avctx, slice_decode_thread,
2261 747 &s2->thread_context[0], NULL,
2262 s->slice_count, sizeof(void *));
2263
2/2
✓ Branch 0 taken 6678 times.
✓ Branch 1 taken 747 times.
7425 for (i = 0; i < s->slice_count; i++)
2264 6678 error_count += atomic_load_explicit(&s2->thread_context[i]->er.error_count,
2265 memory_order_relaxed);
2266 747 atomic_store_explicit(&s2->er.error_count, error_count,
2267 memory_order_relaxed);
2268 }
2269
2270 4186 ret = slice_end(avctx, picture, got_output);
2271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4186 times.
4186 if (ret < 0)
2272 4675 return ret;
2273 }
2274 4505 s2->pict_type = 0;
2275
2276
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4505 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4505 if (avctx->err_recognition & AV_EF_EXPLODE && s2->er.error_count)
2277 return AVERROR_INVALIDDATA;
2278
2279 4505 return FFMAX(0, buf_ptr - buf);
2280 }
2281
2282 224439 input_size = buf_end - buf_ptr;
2283
2284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 224439 times.
224439 if (avctx->debug & FF_DEBUG_STARTCODE)
2285 av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %"PTRDIFF_SPECIFIER" left %d\n",
2286 start_code, buf_ptr - buf, input_size);
2287
2288 /* prepare data for next start code */
2289
6/6
✓ Branch 0 taken 809 times.
✓ Branch 1 taken 5224 times.
✓ Branch 2 taken 6376 times.
✓ Branch 3 taken 1037 times.
✓ Branch 4 taken 560 times.
✓ Branch 5 taken 210433 times.
224439 switch (start_code) {
2290 809 case SEQ_START_CODE:
2291
1/2
✓ Branch 0 taken 809 times.
✗ Branch 1 not taken.
809 if (last_code == 0) {
2292 809 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2293
2/2
✓ Branch 0 taken 551 times.
✓ Branch 1 taken 258 times.
809 if (buf != avctx->extradata)
2294 551 s->sync = 1;
2295 } else {
2296 av_log(avctx, AV_LOG_ERROR,
2297 "ignoring SEQ_START_CODE after %X\n", last_code);
2298 if (avctx->err_recognition & AV_EF_EXPLODE)
2299 return AVERROR_INVALIDDATA;
2300 }
2301 809 break;
2302
2303 5224 case PICTURE_START_CODE:
2304
3/4
✓ Branch 0 taken 835 times.
✓ Branch 1 taken 4389 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 835 times.
5224 if (picture_start_code_seen && s2->picture_structure == PICT_FRAME) {
2305 /* If it's a frame picture, there can't be more than one picture header.
2306 Yet, it does happen and we need to handle it. */
2307 av_log(avctx, AV_LOG_WARNING, "ignoring extra picture following a frame-picture\n");
2308 break;
2309 }
2310 5224 picture_start_code_seen = 1;
2311
2312
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5224 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5224 if (buf == avctx->extradata && avctx->codec_tag == AV_RL32("AVmp")) {
2313 av_log(avctx, AV_LOG_WARNING, "ignoring picture start code in AVmp extradata\n");
2314 break;
2315 }
2316
2317
3/4
✓ Branch 0 taken 5054 times.
✓ Branch 1 taken 170 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5054 times.
5224 if (s2->width <= 0 || s2->height <= 0) {
2318 170 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
2319 s2->width, s2->height);
2320 170 return AVERROR_INVALIDDATA;
2321 }
2322
2323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5054 times.
5054 if (s->tmpgexs){
2324 s2->intra_dc_precision= 3;
2325 s2->intra_matrix[0]= 1;
2326 }
2327
2/2
✓ Branch 0 taken 752 times.
✓ Branch 1 taken 4302 times.
5054 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
2328
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) {
2329 int error_count = 0;
2330 int i;
2331
2332 avctx->execute(avctx, slice_decode_thread,
2333 s2->thread_context, NULL,
2334 s->slice_count, sizeof(void *));
2335 for (i = 0; i < s->slice_count; i++)
2336 error_count += atomic_load_explicit(&s2->thread_context[i]->er.error_count,
2337 memory_order_relaxed);
2338 atomic_store_explicit(&s2->er.error_count, error_count,
2339 memory_order_relaxed);
2340 s->slice_count = 0;
2341 }
2342
3/4
✓ Branch 0 taken 835 times.
✓ Branch 1 taken 4219 times.
✓ Branch 2 taken 835 times.
✗ Branch 3 not taken.
5054 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2343 5054 ret = mpeg_decode_postinit(avctx);
2344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5054 times.
5054 if (ret < 0) {
2345 av_log(avctx, AV_LOG_ERROR,
2346 "mpeg_decode_postinit() failure\n");
2347 return ret;
2348 }
2349
2350 /* We have a complete image: we try to decompress it. */
2351
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5054 times.
5054 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2352 s2->pict_type = 0;
2353 5054 s->first_slice = 1;
2354 5054 last_code = PICTURE_START_CODE;
2355 } else {
2356 av_log(avctx, AV_LOG_ERROR,
2357 "ignoring pic after %X\n", last_code);
2358 if (avctx->err_recognition & AV_EF_EXPLODE)
2359 return AVERROR_INVALIDDATA;
2360 }
2361 5054 break;
2362 6376 case EXT_START_CODE:
2363 6376 ret = init_get_bits8(&s2->gb, buf_ptr, input_size);
2364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6376 times.
6376 if (ret < 0)
2365 return ret;
2366
2367
5/6
✓ Branch 1 taken 717 times.
✓ Branch 2 taken 95 times.
✓ Branch 3 taken 977 times.
✓ Branch 4 taken 27 times.
✓ Branch 5 taken 4560 times.
✗ Branch 6 not taken.
6376 switch (get_bits(&s2->gb, 4)) {
2368 717 case 0x1:
2369
1/2
✓ Branch 0 taken 717 times.
✗ Branch 1 not taken.
717 if (last_code == 0) {
2370 717 mpeg_decode_sequence_extension(s);
2371 } else {
2372 av_log(avctx, AV_LOG_ERROR,
2373 "ignoring seq ext after %X\n", last_code);
2374 if (avctx->err_recognition & AV_EF_EXPLODE)
2375 return AVERROR_INVALIDDATA;
2376 }
2377 717 break;
2378 95 case 0x2:
2379 95 mpeg_decode_sequence_display_extension(s);
2380 95 break;
2381 977 case 0x3:
2382 977 mpeg_decode_quant_matrix_extension(s2);
2383 977 break;
2384 27 case 0x7:
2385 27 mpeg_decode_picture_display_extension(s);
2386 27 break;
2387 4560 case 0x8:
2388
1/2
✓ Branch 0 taken 4560 times.
✗ Branch 1 not taken.
4560 if (last_code == PICTURE_START_CODE) {
2389 4560 int ret = mpeg_decode_picture_coding_extension(s);
2390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4560 times.
4560 if (ret < 0)
2391 return ret;
2392 } else {
2393 av_log(avctx, AV_LOG_ERROR,
2394 "ignoring pic cod ext after %X\n", last_code);
2395 if (avctx->err_recognition & AV_EF_EXPLODE)
2396 return AVERROR_INVALIDDATA;
2397 }
2398 4560 break;
2399 }
2400 6376 break;
2401 1037 case USER_START_CODE:
2402 1037 mpeg_decode_user_data(avctx, buf_ptr, input_size);
2403 1037 break;
2404 560 case GOP_START_CODE:
2405
1/2
✓ Branch 0 taken 560 times.
✗ Branch 1 not taken.
560 if (last_code == 0) {
2406 560 s2->first_field = 0;
2407 560 ret = mpeg_decode_gop(avctx, buf_ptr, input_size);
2408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 560 times.
560 if (ret < 0)
2409 return ret;
2410 560 s->sync = 1;
2411 } else {
2412 av_log(avctx, AV_LOG_ERROR,
2413 "ignoring GOP_START_CODE after %X\n", last_code);
2414 if (avctx->err_recognition & AV_EF_EXPLODE)
2415 return AVERROR_INVALIDDATA;
2416 }
2417 560 break;
2418 210433 default:
2419
2/2
✓ Branch 0 taken 209531 times.
✓ Branch 1 taken 902 times.
210433 if (start_code >= SLICE_MIN_START_CODE &&
2420
4/4
✓ Branch 0 taken 209526 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5054 times.
✓ Branch 3 taken 204472 times.
209531 start_code <= SLICE_MAX_START_CODE && last_code == PICTURE_START_CODE) {
2421
3/4
✓ Branch 0 taken 1497 times.
✓ Branch 1 taken 3557 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1497 times.
5054 if (s2->progressive_sequence && !s2->progressive_frame) {
2422 s2->progressive_frame = 1;
2423 av_log(s2->avctx, AV_LOG_ERROR,
2424 "interlaced frame in progressive sequence, ignoring\n");
2425 }
2426
2427
1/2
✓ Branch 0 taken 5054 times.
✗ Branch 1 not taken.
5054 if (s2->picture_structure == 0 ||
2428
3/4
✓ Branch 0 taken 1504 times.
✓ Branch 1 taken 3550 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1504 times.
5054 (s2->progressive_frame && s2->picture_structure != PICT_FRAME)) {
2429 av_log(s2->avctx, AV_LOG_ERROR,
2430 "picture_structure %d invalid, ignoring\n",
2431 s2->picture_structure);
2432 s2->picture_structure = PICT_FRAME;
2433 }
2434
2435
3/4
✓ Branch 0 taken 1497 times.
✓ Branch 1 taken 3557 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1497 times.
5054 if (s2->progressive_sequence && !s2->frame_pred_frame_dct)
2436 av_log(s2->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
2437
2438
2/2
✓ Branch 0 taken 3382 times.
✓ Branch 1 taken 1672 times.
5054 if (s2->picture_structure == PICT_FRAME) {
2439 3382 s2->first_field = 0;
2440 3382 s2->v_edge_pos = 16 * s2->mb_height;
2441 } else {
2442 1672 s2->first_field ^= 1;
2443 1672 s2->v_edge_pos = 8 * s2->mb_height;
2444 1672 memset(s2->mbskip_table, 0, s2->mb_stride * s2->mb_height);
2445 }
2446 }
2447
2/2
✓ Branch 0 taken 209531 times.
✓ Branch 1 taken 902 times.
210433 if (start_code >= SLICE_MIN_START_CODE &&
2448
4/4
✓ Branch 0 taken 209526 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 209356 times.
✓ Branch 3 taken 170 times.
209531 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2449 209356 const int field_pic = s2->picture_structure != PICT_FRAME;
2450 209356 int mb_y = start_code - SLICE_MIN_START_CODE;
2451 209356 last_code = SLICE_MIN_START_CODE;
2452
3/4
✓ Branch 0 taken 201768 times.
✓ Branch 1 taken 7588 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 201768 times.
209356 if (s2->codec_id != AV_CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
2453 mb_y += (*buf_ptr&0xE0)<<2;
2454
2455 209356 mb_y <<= field_pic;
2456
2/2
✓ Branch 0 taken 13673 times.
✓ Branch 1 taken 195683 times.
209356 if (s2->picture_structure == PICT_BOTTOM_FIELD)
2457 13673 mb_y++;
2458
2459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 209356 times.
209356 if (buf_end - buf_ptr < 2) {
2460 av_log(s2->avctx, AV_LOG_ERROR, "slice too small\n");
2461 return AVERROR_INVALIDDATA;
2462 }
2463
2464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 209356 times.
209356 if (mb_y >= s2->mb_height) {
2465 av_log(s2->avctx, AV_LOG_ERROR,
2466 "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2467 return AVERROR_INVALIDDATA;
2468 }
2469
2470
2/2
✓ Branch 0 taken 85820 times.
✓ Branch 1 taken 123536 times.
209356 if (!s2->last_pic.ptr) {
2471 /* Skip B-frames if we do not have reference frames and
2472 * GOP is not closed. */
2473
2/2
✓ Branch 0 taken 732 times.
✓ Branch 1 taken 85088 times.
85820 if (s2->pict_type == AV_PICTURE_TYPE_B) {
2474
1/2
✓ Branch 0 taken 732 times.
✗ Branch 1 not taken.
732 if (!s->closed_gop) {
2475 732 skip_frame = 1;
2476 732 av_log(s2->avctx, AV_LOG_DEBUG,
2477 "Skipping B slice due to open GOP\n");
2478 732 break;
2479 }
2480 }
2481 }
2482
3/4
✓ Branch 0 taken 80903 times.
✓ Branch 1 taken 127721 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 80903 times.
208624 if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL))
2483 127721 s->sync = 1;
2484
2/2
✓ Branch 0 taken 81593 times.
✓ Branch 1 taken 127031 times.
208624 if (!s2->next_pic.ptr) {
2485 /* Skip P-frames if we do not have a reference frame or
2486 * we have an invalid header. */
2487
4/4
✓ Branch 0 taken 4944 times.
✓ Branch 1 taken 76649 times.
✓ Branch 2 taken 4032 times.
✓ Branch 3 taken 912 times.
81593 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
2488 4032 skip_frame = 1;
2489 4032 av_log(s2->avctx, AV_LOG_DEBUG,
2490 "Skipping P slice due to !sync\n");
2491 4032 break;
2492 }
2493 }
2494
2/2
✓ Branch 0 taken 77485 times.
✓ Branch 1 taken 127107 times.
204592 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
2495
1/2
✓ Branch 0 taken 77485 times.
✗ Branch 1 not taken.
77485 s2->pict_type == AV_PICTURE_TYPE_B) ||
2496
2/2
✓ Branch 0 taken 77485 times.
✓ Branch 1 taken 127107 times.
204592 (avctx->skip_frame >= AVDISCARD_NONKEY &&
2497
2/2
✓ Branch 0 taken 76573 times.
✓ Branch 1 taken 912 times.
77485 s2->pict_type != AV_PICTURE_TYPE_I) ||
2498
2/2
✓ Branch 0 taken 76573 times.
✓ Branch 1 taken 127107 times.
203680 avctx->skip_frame >= AVDISCARD_ALL) {
2499 77485 skip_frame = 1;
2500 77485 break;
2501 }
2502
2503
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127107 times.
127107 if (!s2->context_initialized)
2504 break;
2505
2506
2/2
✓ Branch 0 taken 119834 times.
✓ Branch 1 taken 7273 times.
127107 if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2507
1/2
✓ Branch 0 taken 119834 times.
✗ Branch 1 not taken.
119834 if (mb_y < avctx->skip_top ||
2508
1/2
✓ Branch 0 taken 119834 times.
✗ Branch 1 not taken.
119834 mb_y >= s2->mb_height - avctx->skip_bottom)
2509 break;
2510 }
2511
2512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127107 times.
127107 if (!s2->pict_type) {
2513 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2514 if (avctx->err_recognition & AV_EF_EXPLODE)
2515 return AVERROR_INVALIDDATA;
2516 break;
2517 }
2518
2519
2/2
✓ Branch 0 taken 4609 times.
✓ Branch 1 taken 122498 times.
127107 if (s->first_slice) {
2520 4609 skip_frame = 0;
2521 4609 s->first_slice = 0;
2522
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4609 times.
4609 if ((ret = mpeg_field_start(s, buf, buf_size)) < 0)
2523 return ret;
2524 }
2525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127107 times.
127107 if (!s2->cur_pic.ptr) {
2526 av_log(avctx, AV_LOG_ERROR,
2527 "current_picture not initialized\n");
2528 return AVERROR_INVALIDDATA;
2529 }
2530
2531 127107 if (HAVE_THREADS &&
2532
2/2
✓ Branch 0 taken 22260 times.
✓ Branch 1 taken 104847 times.
127107 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2533
1/2
✓ Branch 0 taken 22260 times.
✗ Branch 1 not taken.
22260 !avctx->hwaccel) {
2534 22260 int threshold = (s2->mb_height * s->slice_count +
2535 22260 s2->slice_context_count / 2) /
2536 22260 s2->slice_context_count;
2537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22260 times.
22260 av_assert0(avctx->thread_count > 1);
2538
2/2
✓ Branch 0 taken 6678 times.
✓ Branch 1 taken 15582 times.
22260 if (threshold <= mb_y) {
2539 6678 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2540
2541 6678 thread_context->start_mb_y = mb_y;
2542 6678 thread_context->end_mb_y = s2->mb_height;
2543
2/2
✓ Branch 0 taken 5936 times.
✓ Branch 1 taken 742 times.
6678 if (s->slice_count) {
2544 5936 s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
2545 5936 ret = ff_update_duplicate_context(thread_context, s2);
2546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5936 times.
5936 if (ret < 0)
2547 return ret;
2548 }
2549 6678 ret = init_get_bits8(&thread_context->gb, buf_ptr, input_size);
2550
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6678 times.
6678 if (ret < 0)
2551 return ret;
2552 6678 s->slice_count++;
2553 }
2554 22260 buf_ptr += 2; // FIXME add minimum number of bytes per slice
2555 } else {
2556 104847 ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2557 104847 emms_c();
2558
2559
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 104837 times.
104847 if (ret < 0) {
2560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (avctx->err_recognition & AV_EF_EXPLODE)
2561 return ret;
2562
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)
2563 10 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2564 s2->resync_mb_y, s2->mb_x, s2->mb_y,
2565 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
2566 } else {
2567 104837 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2568 104837 s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
2569 ER_AC_END | ER_DC_END | ER_MV_END);
2570 }
2571 }
2572 }
2573 128184 break;
2574 }
2575 }
2576 }
2577
2578 4557 static int mpeg_decode_frame(AVCodecContext *avctx, AVFrame *picture,
2579 int *got_output, AVPacket *avpkt)
2580 {
2581 4557 const uint8_t *buf = avpkt->data;
2582 int ret;
2583 4557 int buf_size = avpkt->size;
2584 4557 Mpeg1Context *s = avctx->priv_data;
2585 4557 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2586
2587
3/6
✓ Branch 0 taken 4417 times.
✓ Branch 1 taken 140 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4417 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4557 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2588 /* special case for last picture */
2589
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) {
2590 61 int ret = av_frame_ref(picture, s2->next_pic.ptr->f);
2591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61 times.
61 if (ret < 0)
2592 return ret;
2593
2594 61 ff_mpv_unref_picture(&s2->next_pic);
2595
2596 61 *got_output = 1;
2597 }
2598 140 return buf_size;
2599 }
2600
2601
2/2
✓ Branch 0 taken 459 times.
✓ Branch 1 taken 3958 times.
4417 if (!s2->context_initialized &&
2602
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")))
2603 2 vcr2_init_sequence(avctx);
2604
2605 4417 s->slice_count = 0;
2606
2607
4/4
✓ Branch 0 taken 4024 times.
✓ Branch 1 taken 393 times.
✓ Branch 2 taken 258 times.
✓ Branch 3 taken 3766 times.
4417 if (avctx->extradata && !s->extradata_decoded) {
2608 258 ret = decode_chunks(avctx, picture, got_output,
2609 258 avctx->extradata, avctx->extradata_size);
2610
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 258 times.
258 if (*got_output) {
2611 av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
2612 av_frame_unref(picture);
2613 *got_output = 0;
2614 }
2615 258 s->extradata_decoded = 1;
2616
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)) {
2617 ff_mpv_unref_picture(&s2->cur_pic);
2618 return ret;
2619 }
2620 }
2621
2622 4417 ret = decode_chunks(avctx, picture, got_output, buf, buf_size);
2623
4/4
✓ Branch 0 taken 4247 times.
✓ Branch 1 taken 170 times.
✓ Branch 2 taken 3837 times.
✓ Branch 3 taken 410 times.
4417 if (ret<0 || *got_output) {
2624 4007 ff_mpv_unref_picture(&s2->cur_pic);
2625
2626
4/4
✓ Branch 0 taken 544 times.
✓ Branch 1 taken 3463 times.
✓ Branch 2 taken 374 times.
✓ Branch 3 taken 170 times.
4007 if (s->timecode_frame_start != -1 && *got_output) {
2627 char tcbuf[AV_TIMECODE_STR_SIZE];
2628 374 AVFrameSideData *tcside = av_frame_new_side_data(picture,
2629 AV_FRAME_DATA_GOP_TIMECODE,
2630 sizeof(int64_t));
2631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 374 times.
374 if (!tcside)
2632 return AVERROR(ENOMEM);
2633 374 memcpy(tcside->data, &s->timecode_frame_start, sizeof(int64_t));
2634
2635 374 av_timecode_make_mpeg_tc_string(tcbuf, s->timecode_frame_start);
2636 374 av_dict_set(&picture->metadata, "timecode", tcbuf, 0);
2637
2638 374 s->timecode_frame_start = -1;
2639 }
2640 }
2641
2642 4417 return ret;
2643 }
2644
2645 static av_cold void flush(AVCodecContext *avctx)
2646 {
2647 Mpeg1Context *s = avctx->priv_data;
2648
2649 s->sync = 0;
2650 s->closed_gop = 0;
2651
2652 av_buffer_unref(&s->a53_buf_ref);
2653 ff_mpeg_flush(avctx);
2654 }
2655
2656 288 static av_cold int mpeg_decode_end(AVCodecContext *avctx)
2657 {
2658 288 Mpeg1Context *s = avctx->priv_data;
2659
2660 288 av_buffer_unref(&s->a53_buf_ref);
2661 288 return ff_mpv_decode_close(avctx);
2662 }
2663
2664 const FFCodec ff_mpeg1video_decoder = {
2665 .p.name = "mpeg1video",
2666 CODEC_LONG_NAME("MPEG-1 video"),
2667 .p.type = AVMEDIA_TYPE_VIDEO,
2668 .p.id = AV_CODEC_ID_MPEG1VIDEO,
2669 .priv_data_size = sizeof(Mpeg1Context),
2670 .init = mpeg_decode_init,
2671 .close = mpeg_decode_end,
2672 FF_CODEC_DECODE_CB(mpeg_decode_frame),
2673 .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2674 AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
2675 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2676 .flush = flush,
2677 .p.max_lowres = 3,
2678 UPDATE_THREAD_CONTEXT(mpeg_decode_update_thread_context),
2679 .hw_configs = (const AVCodecHWConfigInternal *const []) {
2680 #if CONFIG_MPEG1_NVDEC_HWACCEL
2681 HWACCEL_NVDEC(mpeg1),
2682 #endif
2683 #if CONFIG_MPEG1_VDPAU_HWACCEL
2684 HWACCEL_VDPAU(mpeg1),
2685 #endif
2686 #if CONFIG_MPEG1_VIDEOTOOLBOX_HWACCEL
2687 HWACCEL_VIDEOTOOLBOX(mpeg1),
2688 #endif
2689 NULL
2690 },
2691 };
2692
2693 #define M2V_OFFSET(x) offsetof(Mpeg1Context, x)
2694 #define M2V_PARAM AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2695
2696 static const AVOption mpeg2video_options[] = {
2697 { "cc_format", "extract a specific Closed Captions format",
2698 M2V_OFFSET(cc_format), AV_OPT_TYPE_INT, { .i64 = CC_FORMAT_AUTO },
2699 CC_FORMAT_AUTO, CC_FORMAT_DISH, M2V_PARAM, .unit = "cc_format" },
2700
2701 { "auto", "pick first seen CC substream", 0, AV_OPT_TYPE_CONST,
2702 { .i64 = CC_FORMAT_AUTO }, .flags = M2V_PARAM, .unit = "cc_format" },
2703 { "a53", "pick A/53 Part 4 CC substream", 0, AV_OPT_TYPE_CONST,
2704 { .i64 = CC_FORMAT_A53_PART4 }, .flags = M2V_PARAM, .unit = "cc_format" },
2705 { "scte20", "pick SCTE-20 CC substream", 0, AV_OPT_TYPE_CONST,
2706 { .i64 = CC_FORMAT_SCTE20 }, .flags = M2V_PARAM, .unit = "cc_format" },
2707 { "dvd", "pick DVD CC substream", 0, AV_OPT_TYPE_CONST,
2708 { .i64 = CC_FORMAT_DVD }, .flags = M2V_PARAM, .unit = "cc_format" },
2709 { "dish", "pick Dish Network CC substream", 0, AV_OPT_TYPE_CONST,
2710 { .i64 = CC_FORMAT_DISH }, .flags = M2V_PARAM, .unit = "cc_format" },
2711 { NULL }
2712 };
2713
2714 static const AVClass mpeg2video_class = {
2715 .class_name = "MPEG-2 video",
2716 .item_name = av_default_item_name,
2717 .option = mpeg2video_options,
2718 .version = LIBAVUTIL_VERSION_INT,
2719 .category = AV_CLASS_CATEGORY_DECODER,
2720 };
2721
2722 const FFCodec ff_mpeg2video_decoder = {
2723 .p.name = "mpeg2video",
2724 CODEC_LONG_NAME("MPEG-2 video"),
2725 .p.type = AVMEDIA_TYPE_VIDEO,
2726 .p.id = AV_CODEC_ID_MPEG2VIDEO,
2727 .p.priv_class = &mpeg2video_class,
2728 .priv_data_size = sizeof(Mpeg1Context),
2729 .init = mpeg_decode_init,
2730 .close = mpeg_decode_end,
2731 FF_CODEC_DECODE_CB(mpeg_decode_frame),
2732 .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2733 AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
2734 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2735 .flush = flush,
2736 .p.max_lowres = 3,
2737 .p.profiles = NULL_IF_CONFIG_SMALL(ff_mpeg2_video_profiles),
2738 .hw_configs = (const AVCodecHWConfigInternal *const []) {
2739 #if CONFIG_MPEG2_DXVA2_HWACCEL
2740 HWACCEL_DXVA2(mpeg2),
2741 #endif
2742 #if CONFIG_MPEG2_D3D11VA_HWACCEL
2743 HWACCEL_D3D11VA(mpeg2),
2744 #endif
2745 #if CONFIG_MPEG2_D3D11VA2_HWACCEL
2746 HWACCEL_D3D11VA2(mpeg2),
2747 #endif
2748 #if CONFIG_MPEG2_D3D12VA_HWACCEL
2749 HWACCEL_D3D12VA(mpeg2),
2750 #endif
2751 #if CONFIG_MPEG2_NVDEC_HWACCEL
2752 HWACCEL_NVDEC(mpeg2),
2753 #endif
2754 #if CONFIG_MPEG2_VAAPI_HWACCEL
2755 HWACCEL_VAAPI(mpeg2),
2756 #endif
2757 #if CONFIG_MPEG2_VDPAU_HWACCEL
2758 HWACCEL_VDPAU(mpeg2),
2759 #endif
2760 #if CONFIG_MPEG2_VIDEOTOOLBOX_HWACCEL
2761 HWACCEL_VIDEOTOOLBOX(mpeg2),
2762 #endif
2763 NULL
2764 },
2765 };
2766
2767 //legacy decoder
2768 const FFCodec ff_mpegvideo_decoder = {
2769 .p.name = "mpegvideo",
2770 CODEC_LONG_NAME("MPEG-1 video"),
2771 .p.type = AVMEDIA_TYPE_VIDEO,
2772 .p.id = AV_CODEC_ID_MPEG2VIDEO,
2773 .priv_data_size = sizeof(Mpeg1Context),
2774 .init = mpeg_decode_init,
2775 .close = mpeg_decode_end,
2776 FF_CODEC_DECODE_CB(mpeg_decode_frame),
2777 .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2778 AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
2779 .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2780 .flush = flush,
2781 .p.max_lowres = 3,
2782 };
2783
2784 typedef struct IPUContext {
2785 MpegEncContext m;
2786
2787 int flags;
2788 DECLARE_ALIGNED(32, int16_t, block)[6][64];
2789 } IPUContext;
2790
2791 static int ipu_decode_frame(AVCodecContext *avctx, AVFrame *frame,
2792 int *got_frame, AVPacket *avpkt)
2793 {
2794 IPUContext *s = avctx->priv_data;
2795 MpegEncContext *m = &s->m;
2796 GetBitContext *gb = &m->gb;
2797 int ret;
2798
2799 // Check for minimal intra MB size (considering mb header, luma & chroma dc VLC, ac EOB VLC)
2800 if (avpkt->size*8LL < (avctx->width+15)/16 * ((avctx->height+15)/16) * (2LL + 3*4 + 2*2 + 2*6))
2801 return AVERROR_INVALIDDATA;
2802
2803 ret = ff_get_buffer(avctx, frame, 0);
2804 if (ret < 0)
2805 return ret;
2806
2807 ret = init_get_bits8(gb, avpkt->data, avpkt->size);
2808 if (ret < 0)
2809 return ret;
2810
2811 s->flags = get_bits(gb, 8);
2812 m->intra_dc_precision = s->flags & 3;
2813 m->q_scale_type = !!(s->flags & 0x40);
2814 m->intra_vlc_format = !!(s->flags & 0x20);
2815 m->alternate_scan = !!(s->flags & 0x10);
2816
2817 ff_permute_scantable(m->intra_scantable.permutated,
2818 s->flags & 0x10 ? ff_alternate_vertical_scan : ff_zigzag_direct,
2819 m->idsp.idct_permutation);
2820
2821 m->last_dc[0] = m->last_dc[1] = m->last_dc[2] = 1 << (7 + (s->flags & 3));
2822 m->qscale = 1;
2823
2824 for (int y = 0; y < avctx->height; y += 16) {
2825 int intraquant;
2826
2827 for (int x = 0; x < avctx->width; x += 16) {
2828 if (x || y) {
2829 if (!get_bits1(gb))
2830 return AVERROR_INVALIDDATA;
2831 }
2832 if (get_bits1(gb)) {
2833 intraquant = 0;
2834 } else {
2835 if (!get_bits1(gb))
2836 return AVERROR_INVALIDDATA;
2837 intraquant = 1;
2838 }
2839
2840 if (s->flags & 4)
2841 skip_bits1(gb);
2842
2843 if (intraquant)
2844 m->qscale = mpeg_get_qscale(m);
2845
2846 memset(s->block, 0, sizeof(s->block));
2847
2848 for (int n = 0; n < 6; n++) {
2849 if (s->flags & 0x80) {
2850 ret = ff_mpeg1_decode_block_intra(&m->gb,
2851 m->intra_matrix,
2852 m->intra_scantable.permutated,
2853 m->last_dc, s->block[n],
2854 n, m->qscale);
2855 } else {
2856 ret = mpeg2_decode_block_intra(m, s->block[n], n);
2857 }
2858
2859 if (ret < 0)
2860 return ret;
2861 }
2862
2863 m->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x,
2864 frame->linesize[0], s->block[0]);
2865 m->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x + 8,
2866 frame->linesize[0], s->block[1]);
2867 m->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x,
2868 frame->linesize[0], s->block[2]);
2869 m->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8,
2870 frame->linesize[0], s->block[3]);
2871 m->idsp.idct_put(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
2872 frame->linesize[1], s->block[4]);
2873 m->idsp.idct_put(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
2874 frame->linesize[2], s->block[5]);
2875 }
2876 }
2877
2878 align_get_bits(gb);
2879 if (get_bits_left(gb) != 32)
2880 return AVERROR_INVALIDDATA;
2881
2882 *got_frame = 1;
2883
2884 return avpkt->size;
2885 }
2886
2887 static av_cold int ipu_decode_init(AVCodecContext *avctx)
2888 {
2889 IPUContext *s = avctx->priv_data;
2890 MpegEncContext *m = &s->m;
2891
2892 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
2893 m->avctx = avctx;
2894
2895 ff_idctdsp_init(&m->idsp, avctx);
2896 ff_mpeg12_init_vlcs();
2897
2898 for (int i = 0; i < 64; i++) {
2899 int j = m->idsp.idct_permutation[i];
2900 int v = ff_mpeg1_default_intra_matrix[i];
2901 m->intra_matrix[j] = v;
2902 m->chroma_intra_matrix[j] = v;
2903 }
2904
2905 return 0;
2906 }
2907
2908 const FFCodec ff_ipu_decoder = {
2909 .p.name = "ipu",
2910 CODEC_LONG_NAME("IPU Video"),
2911 .p.type = AVMEDIA_TYPE_VIDEO,
2912 .p.id = AV_CODEC_ID_IPU,
2913 .priv_data_size = sizeof(IPUContext),
2914 .init = ipu_decode_init,
2915 FF_CODEC_DECODE_CB(ipu_decode_frame),
2916 .p.capabilities = AV_CODEC_CAP_DR1,
2917 };
2918