FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12dec.c
Date: 2025-06-23 20:06:14
Exec Total Coverage
Lines: 1158 1601 72.3%
Functions: 30 33 90.9%
Branches: 666 1117 59.6%

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