FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpeg12dec.c
Date: 2022-01-27 16:04:32
Exec Total Coverage
Lines: 1169 1758 66.5%
Branches: 643 1145 56.2%

Line Branch Exec Source
1 /*
2 * MPEG-1/2 decoder
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2013 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * MPEG-1/2 decoder
26 */
27
28 #define UNCHECKED_BITSTREAM_READER 1
29 #include <inttypes.h>
30
31 #include "libavutil/attributes.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/mem_internal.h"
35 #include "libavutil/stereo3d.h"
36 #include "libavutil/timecode.h"
37 #include "libavutil/video_enc_params.h"
38
39 #include "avcodec.h"
40 #include "bytestream.h"
41 #include "error_resilience.h"
42 #include "hwconfig.h"
43 #include "idctdsp.h"
44 #include "internal.h"
45 #include "mpeg_er.h"
46 #include "mpeg12.h"
47 #include "mpeg12data.h"
48 #include "mpegutils.h"
49 #include "mpegvideo.h"
50 #include "mpegvideodata.h"
51 #include "profiles.h"
52 #include "thread.h"
53 #include "xvmc_internal.h"
54
55 #define A53_MAX_CC_COUNT 2000
56
57 typedef struct Mpeg1Context {
58 MpegEncContext mpeg_enc_ctx;
59 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
60 int repeat_field; /* true if we must repeat the field */
61 AVPanScan pan_scan; /* some temporary storage for the panscan */
62 AVStereo3D stereo3d;
63 int has_stereo3d;
64 AVBufferRef *a53_buf_ref;
65 uint8_t afd;
66 int has_afd;
67 int slice_count;
68 AVRational save_aspect;
69 int save_width, save_height, save_progressive_seq;
70 int rc_buffer_size;
71 AVRational frame_rate_ext; /* MPEG-2 specific framerate modificator */
72 int sync; /* Did we reach a sync point like a GOP/SEQ/KEYFrame? */
73 int closed_gop;
74 int tmpgexs;
75 int first_slice;
76 int extradata_decoded;
77 } Mpeg1Context;
78
79 #define MB_TYPE_ZERO_MV 0x20000000
80
81 static const uint32_t ptype2mb_type[7] = {
82 MB_TYPE_INTRA,
83 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
84 MB_TYPE_L0,
85 MB_TYPE_L0 | MB_TYPE_CBP,
86 MB_TYPE_QUANT | MB_TYPE_INTRA,
87 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
88 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
89 };
90
91 static const uint32_t btype2mb_type[11] = {
92 MB_TYPE_INTRA,
93 MB_TYPE_L1,
94 MB_TYPE_L1 | MB_TYPE_CBP,
95 MB_TYPE_L0,
96 MB_TYPE_L0 | MB_TYPE_CBP,
97 MB_TYPE_L0L1,
98 MB_TYPE_L0L1 | MB_TYPE_CBP,
99 MB_TYPE_QUANT | MB_TYPE_INTRA,
100 MB_TYPE_QUANT | MB_TYPE_L1 | MB_TYPE_CBP,
101 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
102 MB_TYPE_QUANT | MB_TYPE_L0L1 | MB_TYPE_CBP,
103 };
104
105 /* as H.263, but only 17 codes */
106 4268676 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
107 {
108 int code, sign, val, shift;
109
110 4268676 code = get_vlc2(&s->gb, ff_mv_vlc.table, MV_VLC_BITS, 2);
111
2/2
✓ Branch 0 taken 1662399 times.
✓ Branch 1 taken 2606277 times.
4268676 if (code == 0)
112 1662399 return pred;
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2606277 times.
2606277 if (code < 0)
114 return 0xffff;
115
116 2606277 sign = get_bits1(&s->gb);
117 2606277 shift = fcode - 1;
118 2606277 val = code;
119
2/2
✓ Branch 0 taken 2266640 times.
✓ Branch 1 taken 339637 times.
2606277 if (shift) {
120 2266640 val = (val - 1) << shift;
121 2266640 val |= get_bits(&s->gb, shift);
122 2266640 val++;
123 }
124
2/2
✓ Branch 0 taken 1343990 times.
✓ Branch 1 taken 1262287 times.
2606277 if (sign)
125 1343990 val = -val;
126 2606277 val += pred;
127
128 /* modulo decoding */
129 2606277 return sign_extend(val, 5 + shift);
130 }
131
132 #define MAX_INDEX (64 - 1)
133 #define check_scantable_index(ctx, x) \
134 do { \
135 if ((x) > MAX_INDEX) { \
136 av_log(ctx->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
137 ctx->mb_x, ctx->mb_y); \
138 return AVERROR_INVALIDDATA; \
139 } \
140 } while (0)
141
142 403118 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
143 int16_t *block, int n)
144 {
145 int level, i, j, run;
146 403118 RLTable *rl = &ff_rl_mpeg1;
147 403118 uint8_t *const scantable = s->intra_scantable.permutated;
148 403118 const uint16_t *quant_matrix = s->inter_matrix;
149 403118 const int qscale = s->qscale;
150
151 {
152 403118 OPEN_READER(re, &s->gb);
153 403118 i = -1;
154 // special case for first coefficient, no need to add second VLC table
155 403118 UPDATE_CACHE(re, &s->gb);
156
2/2
✓ Branch 0 taken 101724 times.
✓ Branch 1 taken 301394 times.
403118 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
157 101724 level = (3 * qscale * quant_matrix[0]) >> 5;
158 101724 level = (level - 1) | 1;
159
2/2
✓ Branch 0 taken 56898 times.
✓ Branch 1 taken 44826 times.
101724 if (GET_CACHE(re, &s->gb) & 0x40000000)
160 56898 level = -level;
161 101724 block[0] = level;
162 101724 i++;
163 101724 SKIP_BITS(re, &s->gb, 2);
164
2/2
✓ Branch 0 taken 34285 times.
✓ Branch 1 taken 67439 times.
101724 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
165 34285 goto end;
166 }
167 /* now quantify & encode AC coefficients */
168 for (;;) {
169
2/2
✓ Branch 1 taken 336221 times.
✓ Branch 2 taken 2585187 times.
2921408 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
170 TEX_VLC_BITS, 2, 0);
171
172
2/2
✓ Branch 0 taken 2842322 times.
✓ Branch 1 taken 79086 times.
2921408 if (level != 0) {
173 2842322 i += run;
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2842322 times.
2842322 if (i > MAX_INDEX)
175 break;
176 2842322 j = scantable[i];
177 2842322 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
178 2842322 level = (level - 1) | 1;
179 2842322 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
180 2842322 SHOW_SBITS(re, &s->gb, 1);
181 2842322 SKIP_BITS(re, &s->gb, 1);
182 } else {
183 /* escape */
184 79086 run = SHOW_UBITS(re, &s->gb, 6) + 1;
185 79086 LAST_SKIP_BITS(re, &s->gb, 6);
186 79086 UPDATE_CACHE(re, &s->gb);
187 79086 level = SHOW_SBITS(re, &s->gb, 8);
188 79086 SKIP_BITS(re, &s->gb, 8);
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79086 times.
79086 if (level == -128) {
190 level = SHOW_UBITS(re, &s->gb, 8) - 256;
191 SKIP_BITS(re, &s->gb, 8);
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79086 times.
79086 } else if (level == 0) {
193 level = SHOW_UBITS(re, &s->gb, 8);
194 SKIP_BITS(re, &s->gb, 8);
195 }
196 79086 i += run;
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79086 times.
79086 if (i > MAX_INDEX)
198 break;
199 79086 j = scantable[i];
200
2/2
✓ Branch 0 taken 38919 times.
✓ Branch 1 taken 40167 times.
79086 if (level < 0) {
201 38919 level = -level;
202 38919 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
203 38919 level = (level - 1) | 1;
204 38919 level = -level;
205 } else {
206 40167 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
207 40167 level = (level - 1) | 1;
208 }
209 }
210
211 2921408 block[j] = level;
212
2/2
✓ Branch 0 taken 368833 times.
✓ Branch 1 taken 2552575 times.
2921408 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
213 368833 break;
214 2552575 UPDATE_CACHE(re, &s->gb);
215 }
216 403118 end:
217 403118 LAST_SKIP_BITS(re, &s->gb, 2);
218 403118 CLOSE_READER(re, &s->gb);
219 }
220
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 403118 times.
403118 check_scantable_index(s, i);
222
223 403118 s->block_last_index[n] = i;
224 403118 return 0;
225 }
226
227 /**
228 * Changing this would eat up any speed benefits it has.
229 * Do not use "fast" flag if you need the code to be robust.
230 */
231 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s,
232 int16_t *block, int n)
233 {
234 int level, i, j, run;
235 RLTable *rl = &ff_rl_mpeg1;
236 uint8_t *const scantable = s->intra_scantable.permutated;
237 const int qscale = s->qscale;
238
239 {
240 OPEN_READER(re, &s->gb);
241 i = -1;
242 // Special case for first coefficient, no need to add second VLC table.
243 UPDATE_CACHE(re, &s->gb);
244 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
245 level = (3 * qscale) >> 1;
246 level = (level - 1) | 1;
247 if (GET_CACHE(re, &s->gb) & 0x40000000)
248 level = -level;
249 block[0] = level;
250 i++;
251 SKIP_BITS(re, &s->gb, 2);
252 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
253 goto end;
254 }
255
256 /* now quantify & encode AC coefficients */
257 for (;;) {
258 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
259 TEX_VLC_BITS, 2, 0);
260
261 if (level != 0) {
262 i += run;
263 if (i > MAX_INDEX)
264 break;
265 j = scantable[i];
266 level = ((level * 2 + 1) * qscale) >> 1;
267 level = (level - 1) | 1;
268 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
269 SHOW_SBITS(re, &s->gb, 1);
270 SKIP_BITS(re, &s->gb, 1);
271 } else {
272 /* escape */
273 run = SHOW_UBITS(re, &s->gb, 6) + 1;
274 LAST_SKIP_BITS(re, &s->gb, 6);
275 UPDATE_CACHE(re, &s->gb);
276 level = SHOW_SBITS(re, &s->gb, 8);
277 SKIP_BITS(re, &s->gb, 8);
278 if (level == -128) {
279 level = SHOW_UBITS(re, &s->gb, 8) - 256;
280 SKIP_BITS(re, &s->gb, 8);
281 } else if (level == 0) {
282 level = SHOW_UBITS(re, &s->gb, 8);
283 SKIP_BITS(re, &s->gb, 8);
284 }
285 i += run;
286 if (i > MAX_INDEX)
287 break;
288 j = scantable[i];
289 if (level < 0) {
290 level = -level;
291 level = ((level * 2 + 1) * qscale) >> 1;
292 level = (level - 1) | 1;
293 level = -level;
294 } else {
295 level = ((level * 2 + 1) * qscale) >> 1;
296 level = (level - 1) | 1;
297 }
298 }
299
300 block[j] = level;
301 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
302 break;
303 UPDATE_CACHE(re, &s->gb);
304 }
305 end:
306 LAST_SKIP_BITS(re, &s->gb, 2);
307 CLOSE_READER(re, &s->gb);
308 }
309
310 check_scantable_index(s, i);
311
312 s->block_last_index[n] = i;
313 return 0;
314 }
315
316 3924107 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
317 int16_t *block, int n)
318 {
319 int level, i, j, run;
320 3924107 RLTable *rl = &ff_rl_mpeg1;
321 3924107 uint8_t *const scantable = s->intra_scantable.permutated;
322 const uint16_t *quant_matrix;
323 3924107 const int qscale = s->qscale;
324 int mismatch;
325
326 3924107 mismatch = 1;
327
328 {
329 3924107 OPEN_READER(re, &s->gb);
330 3924107 i = -1;
331
2/2
✓ Branch 0 taken 2871886 times.
✓ Branch 1 taken 1052221 times.
3924107 if (n < 4)
332 2871886 quant_matrix = s->inter_matrix;
333 else
334 1052221 quant_matrix = s->chroma_inter_matrix;
335
336 // Special case for first coefficient, no need to add second VLC table.
337 3924107 UPDATE_CACHE(re, &s->gb);
338
2/2
✓ Branch 0 taken 1415133 times.
✓ Branch 1 taken 2508974 times.
3924107 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
339 1415133 level = (3 * qscale * quant_matrix[0]) >> 5;
340
2/2
✓ Branch 0 taken 885057 times.
✓ Branch 1 taken 530076 times.
1415133 if (GET_CACHE(re, &s->gb) & 0x40000000)
341 885057 level = -level;
342 1415133 block[0] = level;
343 1415133 mismatch ^= level;
344 1415133 i++;
345 1415133 SKIP_BITS(re, &s->gb, 2);
346
2/2
✓ Branch 0 taken 673112 times.
✓ Branch 1 taken 742021 times.
1415133 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
347 673112 goto end;
348 }
349
350 /* now quantify & encode AC coefficients */
351 for (;;) {
352
2/2
✓ Branch 1 taken 1407741 times.
✓ Branch 2 taken 13970288 times.
15378029 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
353 TEX_VLC_BITS, 2, 0);
354
355
2/2
✓ Branch 0 taken 15126107 times.
✓ Branch 1 taken 251922 times.
15378029 if (level != 0) {
356 15126107 i += run;
357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15126107 times.
15126107 if (i > MAX_INDEX)
358 break;
359 15126107 j = scantable[i];
360 15126107 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
361 15126107 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
362 15126107 SHOW_SBITS(re, &s->gb, 1);
363 15126107 SKIP_BITS(re, &s->gb, 1);
364 } else {
365 /* escape */
366 251922 run = SHOW_UBITS(re, &s->gb, 6) + 1;
367 251922 LAST_SKIP_BITS(re, &s->gb, 6);
368 251922 UPDATE_CACHE(re, &s->gb);
369 251922 level = SHOW_SBITS(re, &s->gb, 12);
370 251922 SKIP_BITS(re, &s->gb, 12);
371
372 251922 i += run;
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 251922 times.
251922 if (i > MAX_INDEX)
374 break;
375 251922 j = scantable[i];
376
2/2
✓ Branch 0 taken 123054 times.
✓ Branch 1 taken 128868 times.
251922 if (level < 0) {
377 123054 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
378 123054 level = -level;
379 } else {
380 128868 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
381 }
382 }
383
384 15378029 mismatch ^= level;
385 15378029 block[j] = level;
386
2/2
✓ Branch 0 taken 3250995 times.
✓ Branch 1 taken 12127034 times.
15378029 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
387 3250995 break;
388 12127034 UPDATE_CACHE(re, &s->gb);
389 }
390 3924107 end:
391 3924107 LAST_SKIP_BITS(re, &s->gb, 2);
392 3924107 CLOSE_READER(re, &s->gb);
393 }
394 3924107 block[63] ^= (mismatch & 1);
395
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3924107 times.
3924107 check_scantable_index(s, i);
397
398 3924107 s->block_last_index[n] = i;
399 3924107 return 0;
400 }
401
402 /**
403 * Changing this would eat up any speed benefits it has.
404 * Do not use "fast" flag if you need the code to be robust.
405 */
406 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
407 int16_t *block, int n)
408 {
409 int level, i, j, run;
410 RLTable *rl = &ff_rl_mpeg1;
411 uint8_t *const scantable = s->intra_scantable.permutated;
412 const int qscale = s->qscale;
413 OPEN_READER(re, &s->gb);
414 i = -1;
415
416 // special case for first coefficient, no need to add second VLC table
417 UPDATE_CACHE(re, &s->gb);
418 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
419 level = (3 * qscale) >> 1;
420 if (GET_CACHE(re, &s->gb) & 0x40000000)
421 level = -level;
422 block[0] = level;
423 i++;
424 SKIP_BITS(re, &s->gb, 2);
425 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
426 goto end;
427 }
428
429 /* now quantify & encode AC coefficients */
430 for (;;) {
431 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
432
433 if (level != 0) {
434 i += run;
435 if (i > MAX_INDEX)
436 break;
437 j = scantable[i];
438 level = ((level * 2 + 1) * qscale) >> 1;
439 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
440 SHOW_SBITS(re, &s->gb, 1);
441 SKIP_BITS(re, &s->gb, 1);
442 } else {
443 /* escape */
444 run = SHOW_UBITS(re, &s->gb, 6) + 1;
445 LAST_SKIP_BITS(re, &s->gb, 6);
446 UPDATE_CACHE(re, &s->gb);
447 level = SHOW_SBITS(re, &s->gb, 12);
448 SKIP_BITS(re, &s->gb, 12);
449
450 i += run;
451 if (i > MAX_INDEX)
452 break;
453 j = scantable[i];
454 if (level < 0) {
455 level = ((-level * 2 + 1) * qscale) >> 1;
456 level = -level;
457 } else {
458 level = ((level * 2 + 1) * qscale) >> 1;
459 }
460 }
461
462 block[j] = level;
463 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF || i > 63)
464 break;
465
466 UPDATE_CACHE(re, &s->gb);
467 }
468 end:
469 LAST_SKIP_BITS(re, &s->gb, 2);
470 CLOSE_READER(re, &s->gb);
471
472 check_scantable_index(s, i);
473
474 s->block_last_index[n] = i;
475 return 0;
476 }
477
478 1654206 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
479 int16_t *block, int n)
480 {
481 int level, dc, diff, i, j, run;
482 int component;
483 RLTable *rl;
484 1654206 uint8_t *const scantable = s->intra_scantable.permutated;
485 const uint16_t *quant_matrix;
486 1654206 const int qscale = s->qscale;
487 int mismatch;
488
489 /* DC coefficient */
490
2/2
✓ Branch 0 taken 1031060 times.
✓ Branch 1 taken 623146 times.
1654206 if (n < 4) {
491 1031060 quant_matrix = s->intra_matrix;
492 1031060 component = 0;
493 } else {
494 623146 quant_matrix = s->chroma_intra_matrix;
495 623146 component = (n & 1) + 1;
496 }
497 1654206 diff = decode_dc(&s->gb, component);
498 1654206 dc = s->last_dc[component];
499 1654206 dc += diff;
500 1654206 s->last_dc[component] = dc;
501 1654206 block[0] = dc * (1 << (3 - s->intra_dc_precision));
502 ff_tlog(s->avctx, "dc=%d\n", block[0]);
503 1654206 mismatch = block[0] ^ 1;
504 1654206 i = 0;
505
2/2
✓ Branch 0 taken 1094802 times.
✓ Branch 1 taken 559404 times.
1654206 if (s->intra_vlc_format)
506 1094802 rl = &ff_rl_mpeg2;
507 else
508 559404 rl = &ff_rl_mpeg1;
509
510 {
511 1654206 OPEN_READER(re, &s->gb);
512 /* now quantify & encode AC coefficients */
513 for (;;) {
514 12867568 UPDATE_CACHE(re, &s->gb);
515
2/2
✓ Branch 1 taken 686026 times.
✓ Branch 2 taken 12181542 times.
12867568 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
516 TEX_VLC_BITS, 2, 0);
517
518
2/2
✓ Branch 0 taken 1654202 times.
✓ Branch 1 taken 11213366 times.
12867568 if (level == 127) {
519 1654202 break;
520
2/2
✓ Branch 0 taken 11091082 times.
✓ Branch 1 taken 122284 times.
11213366 } else if (level != 0) {
521 11091082 i += run;
522
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 11091078 times.
11091082 if (i > MAX_INDEX)
523 4 break;
524 11091078 j = scantable[i];
525 11091078 level = (level * qscale * quant_matrix[j]) >> 4;
526 11091078 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
527 11091078 SHOW_SBITS(re, &s->gb, 1);
528 11091078 LAST_SKIP_BITS(re, &s->gb, 1);
529 } else {
530 /* escape */
531 122284 run = SHOW_UBITS(re, &s->gb, 6) + 1;
532 122284 SKIP_BITS(re, &s->gb, 6);
533 122284 level = SHOW_SBITS(re, &s->gb, 12);
534 122284 LAST_SKIP_BITS(re, &s->gb, 12);
535 122284 i += run;
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 122284 times.
122284 if (i > MAX_INDEX)
537 break;
538 122284 j = scantable[i];
539
2/2
✓ Branch 0 taken 53053 times.
✓ Branch 1 taken 69231 times.
122284 if (level < 0) {
540 53053 level = (-level * qscale * quant_matrix[j]) >> 4;
541 53053 level = -level;
542 } else {
543 69231 level = (level * qscale * quant_matrix[j]) >> 4;
544 }
545 }
546
547 11213362 mismatch ^= level;
548 11213362 block[j] = level;
549 }
550 1654206 CLOSE_READER(re, &s->gb);
551 }
552 1654206 block[63] ^= mismatch & 1;
553
554
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1654202 times.
1654206 check_scantable_index(s, i);
555
556 1654202 s->block_last_index[n] = i;
557 1654202 return 0;
558 }
559
560 /**
561 * Changing this would eat up any speed benefits it has.
562 * Do not use "fast" flag if you need the code to be robust.
563 */
564 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
565 int16_t *block, int n)
566 {
567 int level, dc, diff, i, j, run;
568 int component;
569 RLTable *rl;
570 uint8_t *const scantable = s->intra_scantable.permutated;
571 const uint16_t *quant_matrix;
572 const int qscale = s->qscale;
573
574 /* DC coefficient */
575 if (n < 4) {
576 quant_matrix = s->intra_matrix;
577 component = 0;
578 } else {
579 quant_matrix = s->chroma_intra_matrix;
580 component = (n & 1) + 1;
581 }
582 diff = decode_dc(&s->gb, component);
583 dc = s->last_dc[component];
584 dc += diff;
585 s->last_dc[component] = dc;
586 block[0] = dc * (1 << (3 - s->intra_dc_precision));
587 i = 0;
588 if (s->intra_vlc_format)
589 rl = &ff_rl_mpeg2;
590 else
591 rl = &ff_rl_mpeg1;
592
593 {
594 OPEN_READER(re, &s->gb);
595 /* now quantify & encode AC coefficients */
596 for (;;) {
597 UPDATE_CACHE(re, &s->gb);
598 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
599 TEX_VLC_BITS, 2, 0);
600
601 if (level >= 64 || i > 63) {
602 break;
603 } else if (level != 0) {
604 i += run;
605 j = scantable[i];
606 level = (level * qscale * quant_matrix[j]) >> 4;
607 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
608 SHOW_SBITS(re, &s->gb, 1);
609 LAST_SKIP_BITS(re, &s->gb, 1);
610 } else {
611 /* escape */
612 run = SHOW_UBITS(re, &s->gb, 6) + 1;
613 SKIP_BITS(re, &s->gb, 6);
614 level = SHOW_SBITS(re, &s->gb, 12);
615 LAST_SKIP_BITS(re, &s->gb, 12);
616 i += run;
617 j = scantable[i];
618 if (level < 0) {
619 level = (-level * qscale * quant_matrix[j]) >> 4;
620 level = -level;
621 } else {
622 level = (level * qscale * quant_matrix[j]) >> 4;
623 }
624 }
625
626 block[j] = level;
627 }
628 CLOSE_READER(re, &s->gb);
629 }
630
631 check_scantable_index(s, i);
632
633 s->block_last_index[n] = i;
634 return 0;
635 }
636
637 /******************************************/
638 /* decoding */
639
640 22848 static inline int get_dmv(MpegEncContext *s)
641 {
642
2/2
✓ Branch 1 taken 21321 times.
✓ Branch 2 taken 1527 times.
22848 if (get_bits1(&s->gb))
643 21321 return 1 - (get_bits1(&s->gb) << 1);
644 else
645 1527 return 0;
646 }
647
648 /* motion type (for MPEG-2) */
649 #define MT_FIELD 1
650 #define MT_FRAME 2
651 #define MT_16X8 2
652 #define MT_DMV 3
653
654 2124178 static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
655 {
656 int i, j, k, cbp, val, mb_type, motion_type;
657 2124178 const int mb_block_count = 4 + (1 << s->chroma_format);
658 int ret;
659
660 ff_tlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
661
662 av_assert2(s->mb_skipped == 0);
663
664
2/2
✓ Branch 0 taken 156167 times.
✓ Branch 1 taken 1968011 times.
2124178 if (s->mb_skip_run-- != 0) {
665
2/2
✓ Branch 0 taken 51347 times.
✓ Branch 1 taken 104820 times.
156167 if (s->pict_type == AV_PICTURE_TYPE_P) {
666 51347 s->mb_skipped = 1;
667 51347 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
668 MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
669 } else {
670 int mb_type;
671
672
1/2
✓ Branch 0 taken 104820 times.
✗ Branch 1 not taken.
104820 if (s->mb_x)
673 104820 mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
674 else
675 // FIXME not sure if this is allowed in MPEG at all
676 mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104820 times.
104820 if (IS_INTRA(mb_type)) {
678 av_log(s->avctx, AV_LOG_ERROR, "skip with previntra\n");
679 return AVERROR_INVALIDDATA;
680 }
681 104820 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
682 104820 mb_type | MB_TYPE_SKIP;
683
684
2/2
✓ Branch 0 taken 74214 times.
✓ Branch 1 taken 30606 times.
104820 if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
685 74214 s->mb_skipped = 1;
686 }
687
688 156167 return 0;
689 }
690
691
3/3
✓ Branch 0 taken 210510 times.
✓ Branch 1 taken 1018163 times.
✓ Branch 2 taken 739338 times.
1968011 switch (s->pict_type) {
692 210510 default:
693 case AV_PICTURE_TYPE_I:
694
2/2
✓ Branch 1 taken 14380 times.
✓ Branch 2 taken 196130 times.
210510 if (get_bits1(&s->gb) == 0) {
695
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14380 times.
14380 if (get_bits1(&s->gb) == 0) {
696 av_log(s->avctx, AV_LOG_ERROR,
697 "Invalid mb type in I-frame at %d %d\n",
698 s->mb_x, s->mb_y);
699 return AVERROR_INVALIDDATA;
700 }
701 14380 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
702 } else {
703 196130 mb_type = MB_TYPE_INTRA;
704 }
705 210510 break;
706 1018163 case AV_PICTURE_TYPE_P:
707 1018163 mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
708
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1018163 times.
1018163 if (mb_type < 0) {
709 av_log(s->avctx, AV_LOG_ERROR,
710 "Invalid mb type in P-frame at %d %d\n", s->mb_x, s->mb_y);
711 return AVERROR_INVALIDDATA;
712 }
713 1018163 mb_type = ptype2mb_type[mb_type];
714 1018163 break;
715 739338 case AV_PICTURE_TYPE_B:
716 739338 mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
717
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 739338 times.
739338 if (mb_type < 0) {
718 av_log(s->avctx, AV_LOG_ERROR,
719 "Invalid mb type in B-frame at %d %d\n", s->mb_x, s->mb_y);
720 return AVERROR_INVALIDDATA;
721 }
722 739338 mb_type = btype2mb_type[mb_type];
723 739338 break;
724 }
725 ff_tlog(s->avctx, "mb_type=%x\n", mb_type);
726 // motion_type = 0; /* avoid warning */
727
2/2
✓ Branch 0 taken 278788 times.
✓ Branch 1 taken 1689223 times.
1968011 if (IS_INTRA(mb_type)) {
728 278788 s->bdsp.clear_blocks(s->block[0]);
729
730
2/2
✓ Branch 0 taken 53810 times.
✓ Branch 1 taken 224978 times.
278788 if (!s->chroma_y_shift)
731 53810 s->bdsp.clear_blocks(s->block[6]);
732
733 /* compute DCT type */
734 // FIXME: add an interlaced_dct coded var?
735
2/2
✓ Branch 0 taken 243197 times.
✓ Branch 1 taken 35591 times.
278788 if (s->picture_structure == PICT_FRAME &&
736
2/2
✓ Branch 0 taken 104089 times.
✓ Branch 1 taken 139108 times.
243197 !s->frame_pred_frame_dct)
737 104089 s->interlaced_dct = get_bits1(&s->gb);
738
739
2/2
✓ Branch 0 taken 19501 times.
✓ Branch 1 taken 259287 times.
278788 if (IS_QUANT(mb_type))
740 19501 s->qscale = mpeg_get_qscale(s);
741
742
2/2
✓ Branch 0 taken 678 times.
✓ Branch 1 taken 278110 times.
278788 if (s->concealment_motion_vectors) {
743 /* just parse them */
744
2/2
✓ Branch 0 taken 675 times.
✓ Branch 1 taken 3 times.
678 if (s->picture_structure != PICT_FRAME)
745 675 skip_bits1(&s->gb); /* field select */
746
747 678 s->mv[0][0][0] =
748 678 s->last_mv[0][0][0] =
749 678 s->last_mv[0][1][0] = mpeg_decode_motion(s, s->mpeg_f_code[0][0],
750 s->last_mv[0][0][0]);
751 678 s->mv[0][0][1] =
752 678 s->last_mv[0][0][1] =
753 678 s->last_mv[0][1][1] = mpeg_decode_motion(s, s->mpeg_f_code[0][1],
754 s->last_mv[0][0][1]);
755
756 678 check_marker(s->avctx, &s->gb, "after concealment_motion_vectors");
757 } else {
758 /* reset mv prediction */
759 278110 memset(s->last_mv, 0, sizeof(s->last_mv));
760 }
761 278788 s->mb_intra = 1;
762 // if 1, we memcpy blocks in xvmcvideo
763 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
764 ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
765
766
2/2
✓ Branch 0 taken 257767 times.
✓ Branch 1 taken 21021 times.
278788 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 257767 times.
257767 if (s->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
768 for (i = 0; i < 6; i++)
769 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
770 } else {
771
2/2
✓ Branch 0 taken 1654206 times.
✓ Branch 1 taken 257763 times.
1911969 for (i = 0; i < mb_block_count; i++)
772
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1654202 times.
1654206 if ((ret = mpeg2_decode_block_intra(s, *s->pblocks[i], i)) < 0)
773 4 return ret;
774 }
775 } else {
776
2/2
✓ Branch 0 taken 126126 times.
✓ Branch 1 taken 21021 times.
147147 for (i = 0; i < 6; i++) {
777 126126 ret = ff_mpeg1_decode_block_intra(&s->gb,
778 126126 s->intra_matrix,
779 126126 s->intra_scantable.permutated,
780 126126 s->last_dc, *s->pblocks[i],
781 i, s->qscale);
782
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126126 times.
126126 if (ret < 0) {
783 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n",
784 s->mb_x, s->mb_y);
785 return ret;
786 }
787
788 126126 s->block_last_index[i] = ret;
789 }
790 }
791 } else {
792
2/2
✓ Branch 0 taken 148485 times.
✓ Branch 1 taken 1540738 times.
1689223 if (mb_type & MB_TYPE_ZERO_MV) {
793 av_assert2(mb_type & MB_TYPE_CBP);
794
795 148485 s->mv_dir = MV_DIR_FORWARD;
796
2/2
✓ Branch 0 taken 75956 times.
✓ Branch 1 taken 72529 times.
148485 if (s->picture_structure == PICT_FRAME) {
797
1/2
✓ Branch 0 taken 75956 times.
✗ Branch 1 not taken.
75956 if (s->picture_structure == PICT_FRAME
798
2/2
✓ Branch 0 taken 46050 times.
✓ Branch 1 taken 29906 times.
75956 && !s->frame_pred_frame_dct)
799 46050 s->interlaced_dct = get_bits1(&s->gb);
800 75956 s->mv_type = MV_TYPE_16X16;
801 } else {
802 72529 s->mv_type = MV_TYPE_FIELD;
803 72529 mb_type |= MB_TYPE_INTERLACED;
804 72529 s->field_select[0][0] = s->picture_structure - 1;
805 }
806
807
2/2
✓ Branch 0 taken 31326 times.
✓ Branch 1 taken 117159 times.
148485 if (IS_QUANT(mb_type))
808 31326 s->qscale = mpeg_get_qscale(s);
809
810 148485 s->last_mv[0][0][0] = 0;
811 148485 s->last_mv[0][0][1] = 0;
812 148485 s->last_mv[0][1][0] = 0;
813 148485 s->last_mv[0][1][1] = 0;
814 148485 s->mv[0][0][0] = 0;
815 148485 s->mv[0][0][1] = 0;
816 } else {
817 av_assert2(mb_type & MB_TYPE_L0L1);
818 // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
819 /* get additional motion vector type */
820
4/4
✓ Branch 0 taken 1232826 times.
✓ Branch 1 taken 307912 times.
✓ Branch 2 taken 661748 times.
✓ Branch 3 taken 571078 times.
1540738 if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct) {
821 661748 motion_type = MT_FRAME;
822 } else {
823 878990 motion_type = get_bits(&s->gb, 2);
824
4/4
✓ Branch 0 taken 571078 times.
✓ Branch 1 taken 307912 times.
✓ Branch 2 taken 414187 times.
✓ Branch 3 taken 156891 times.
878990 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
825 414187 s->interlaced_dct = get_bits1(&s->gb);
826 }
827
828
2/2
✓ Branch 0 taken 112590 times.
✓ Branch 1 taken 1428148 times.
1540738 if (IS_QUANT(mb_type))
829 112590 s->qscale = mpeg_get_qscale(s);
830
831 /* motion vectors */
832 1540738 s->mv_dir = (mb_type >> 13) & 3;
833 ff_tlog(s->avctx, "motion_type=%d\n", motion_type);
834
3/4
✓ Branch 0 taken 1316564 times.
✓ Branch 1 taken 212750 times.
✓ Branch 2 taken 11424 times.
✗ Branch 3 not taken.
1540738 switch (motion_type) {
835 1316564 case MT_FRAME: /* or MT_16X8 */
836
2/2
✓ Branch 0 taken 1163356 times.
✓ Branch 1 taken 153208 times.
1316564 if (s->picture_structure == PICT_FRAME) {
837 1163356 mb_type |= MB_TYPE_16x16;
838 1163356 s->mv_type = MV_TYPE_16X16;
839
2/2
✓ Branch 0 taken 2326712 times.
✓ Branch 1 taken 1163356 times.
3490068 for (i = 0; i < 2; i++) {
840
2/2
✓ Branch 0 taken 1494834 times.
✓ Branch 1 taken 831878 times.
2326712 if (USES_LIST(mb_type, i)) {
841 /* MT_FRAME */
842 1494834 s->mv[i][0][0] =
843 1494834 s->last_mv[i][0][0] =
844 1494834 s->last_mv[i][1][0] =
845 1494834 mpeg_decode_motion(s, s->mpeg_f_code[i][0],
846 s->last_mv[i][0][0]);
847 1494834 s->mv[i][0][1] =
848 1494834 s->last_mv[i][0][1] =
849 1494834 s->last_mv[i][1][1] =
850 1494834 mpeg_decode_motion(s, s->mpeg_f_code[i][1],
851 s->last_mv[i][0][1]);
852 /* full_pel: only for MPEG-1 */
853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1494834 times.
1494834 if (s->full_pel[i]) {
854 s->mv[i][0][0] *= 2;
855 s->mv[i][0][1] *= 2;
856 }
857 }
858 }
859 } else {
860 153208 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
861 153208 s->mv_type = MV_TYPE_16X8;
862
2/2
✓ Branch 0 taken 306416 times.
✓ Branch 1 taken 153208 times.
459624 for (i = 0; i < 2; i++) {
863
2/2
✓ Branch 0 taken 153889 times.
✓ Branch 1 taken 152527 times.
306416 if (USES_LIST(mb_type, i)) {
864 /* MT_16X8 */
865
2/2
✓ Branch 0 taken 307778 times.
✓ Branch 1 taken 153889 times.
461667 for (j = 0; j < 2; j++) {
866 307778 s->field_select[i][j] = get_bits1(&s->gb);
867
2/2
✓ Branch 0 taken 615556 times.
✓ Branch 1 taken 307778 times.
923334 for (k = 0; k < 2; k++) {
868 615556 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
869 s->last_mv[i][j][k]);
870 615556 s->last_mv[i][j][k] = val;
871 615556 s->mv[i][j][k] = val;
872 }
873 }
874 }
875 }
876 }
877 1316564 break;
878 212750 case MT_FIELD:
879 212750 s->mv_type = MV_TYPE_FIELD;
880
2/2
✓ Branch 0 taken 69470 times.
✓ Branch 1 taken 143280 times.
212750 if (s->picture_structure == PICT_FRAME) {
881 69470 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
882
2/2
✓ Branch 0 taken 138940 times.
✓ Branch 1 taken 69470 times.
208410 for (i = 0; i < 2; i++) {
883
2/2
✓ Branch 0 taken 85842 times.
✓ Branch 1 taken 53098 times.
138940 if (USES_LIST(mb_type, i)) {
884
2/2
✓ Branch 0 taken 171684 times.
✓ Branch 1 taken 85842 times.
257526 for (j = 0; j < 2; j++) {
885 171684 s->field_select[i][j] = get_bits1(&s->gb);
886 171684 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
887 s->last_mv[i][j][0]);
888 171684 s->last_mv[i][j][0] = val;
889 171684 s->mv[i][j][0] = val;
890 ff_tlog(s->avctx, "fmx=%d\n", val);
891 171684 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
892 171684 s->last_mv[i][j][1] >> 1);
893 171684 s->last_mv[i][j][1] = 2 * val;
894 171684 s->mv[i][j][1] = val;
895 ff_tlog(s->avctx, "fmy=%d\n", val);
896 }
897 }
898 }
899 } else {
900
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 143280 times.
143280 av_assert0(!s->progressive_sequence);
901 143280 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
902
2/2
✓ Branch 0 taken 286560 times.
✓ Branch 1 taken 143280 times.
429840 for (i = 0; i < 2; i++) {
903
2/2
✓ Branch 0 taken 147940 times.
✓ Branch 1 taken 138620 times.
286560 if (USES_LIST(mb_type, i)) {
904 147940 s->field_select[i][0] = get_bits1(&s->gb);
905
2/2
✓ Branch 0 taken 295880 times.
✓ Branch 1 taken 147940 times.
443820 for (k = 0; k < 2; k++) {
906 295880 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
907 s->last_mv[i][0][k]);
908 295880 s->last_mv[i][0][k] = val;
909 295880 s->last_mv[i][1][k] = val;
910 295880 s->mv[i][0][k] = val;
911 }
912 }
913 }
914 }
915 212750 break;
916 11424 case MT_DMV:
917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11424 times.
11424 if (s->progressive_sequence){
918 av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
919 return AVERROR_INVALIDDATA;
920 }
921 11424 s->mv_type = MV_TYPE_DMV;
922
2/2
✓ Branch 0 taken 22848 times.
✓ Branch 1 taken 11424 times.
34272 for (i = 0; i < 2; i++) {
923
2/2
✓ Branch 0 taken 11424 times.
✓ Branch 1 taken 11424 times.
22848 if (USES_LIST(mb_type, i)) {
924 int dmx, dmy, mx, my, m;
925 11424 const int my_shift = s->picture_structure == PICT_FRAME;
926
927 11424 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
928 s->last_mv[i][0][0]);
929 11424 s->last_mv[i][0][0] = mx;
930 11424 s->last_mv[i][1][0] = mx;
931 11424 dmx = get_dmv(s);
932 11424 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
933 11424 s->last_mv[i][0][1] >> my_shift);
934 11424 dmy = get_dmv(s);
935
936
937 11424 s->last_mv[i][0][1] = my * (1 << my_shift);
938 11424 s->last_mv[i][1][1] = my * (1 << my_shift);
939
940 11424 s->mv[i][0][0] = mx;
941 11424 s->mv[i][0][1] = my;
942 11424 s->mv[i][1][0] = mx; // not used
943 11424 s->mv[i][1][1] = my; // not used
944
945
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11424 times.
11424 if (s->picture_structure == PICT_FRAME) {
946 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
947
948 // m = 1 + 2 * s->top_field_first;
949 m = s->top_field_first ? 1 : 3;
950
951 /* top -> top pred */
952 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
953 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
954 m = 4 - m;
955 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
956 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
957 } else {
958 11424 mb_type |= MB_TYPE_16x16;
959
960 11424 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
961 11424 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
962
2/2
✓ Branch 0 taken 9816 times.
✓ Branch 1 taken 1608 times.
11424 if (s->picture_structure == PICT_TOP_FIELD)
963 9816 s->mv[i][2][1]--;
964 else
965 1608 s->mv[i][2][1]++;
966 }
967 }
968 }
969 11424 break;
970 default:
971 av_log(s->avctx, AV_LOG_ERROR,
972 "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
973 return AVERROR_INVALIDDATA;
974 }
975 }
976
977 1689223 s->mb_intra = 0;
978
2/2
✓ Branch 0 taken 1329521 times.
✓ Branch 1 taken 359702 times.
1689223 if (HAS_CBP(mb_type)) {
979 1329521 s->bdsp.clear_blocks(s->block[0]);
980
981 1329521 cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
982
2/2
✓ Branch 0 taken 46345 times.
✓ Branch 1 taken 1283176 times.
1329521 if (mb_block_count > 6) {
983 46345 cbp *= 1 << mb_block_count - 6;
984 46345 cbp |= get_bits(&s->gb, mb_block_count - 6);
985 46345 s->bdsp.clear_blocks(s->block[6]);
986 }
987
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1329521 times.
1329521 if (cbp <= 0) {
988 av_log(s->avctx, AV_LOG_ERROR,
989 "invalid cbp %d at %d %d\n", cbp, s->mb_x, s->mb_y);
990 return AVERROR_INVALIDDATA;
991 }
992
993 // if 1, we memcpy blocks in xvmcvideo
994 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
995 ff_xvmc_pack_pblocks(s, cbp);
996
997
2/2
✓ Branch 0 taken 1217779 times.
✓ Branch 1 taken 111742 times.
1329521 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
998
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1217779 times.
1217779 if (s->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
999 for (i = 0; i < 6; i++) {
1000 if (cbp & 32)
1001 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
1002 else
1003 s->block_last_index[i] = -1;
1004 cbp += cbp;
1005 }
1006 } else {
1007 1217779 cbp <<= 12 - mb_block_count;
1008
1009
2/2
✓ Branch 0 taken 7399364 times.
✓ Branch 1 taken 1217779 times.
8617143 for (i = 0; i < mb_block_count; i++) {
1010
2/2
✓ Branch 0 taken 3924107 times.
✓ Branch 1 taken 3475257 times.
7399364 if (cbp & (1 << 11)) {
1011
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3924107 times.
3924107 if ((ret = mpeg2_decode_block_non_intra(s, *s->pblocks[i], i)) < 0)
1012 return ret;
1013 } else {
1014 3475257 s->block_last_index[i] = -1;
1015 }
1016 7399364 cbp += cbp;
1017 }
1018 }
1019 } else {
1020
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 111742 times.
111742 if (s->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
1021 for (i = 0; i < 6; i++) {
1022 if (cbp & 32)
1023 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
1024 else
1025 s->block_last_index[i] = -1;
1026 cbp += cbp;
1027 }
1028 } else {
1029
2/2
✓ Branch 0 taken 670452 times.
✓ Branch 1 taken 111742 times.
782194 for (i = 0; i < 6; i++) {
1030
2/2
✓ Branch 0 taken 403118 times.
✓ Branch 1 taken 267334 times.
670452 if (cbp & 32) {
1031
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 403118 times.
403118 if ((ret = mpeg1_decode_block_inter(s, *s->pblocks[i], i)) < 0)
1032 return ret;
1033 } else {
1034 267334 s->block_last_index[i] = -1;
1035 }
1036 670452 cbp += cbp;
1037 }
1038 }
1039 }
1040 } else {
1041
2/2
✓ Branch 0 taken 4316424 times.
✓ Branch 1 taken 359702 times.
4676126 for (i = 0; i < 12; i++)
1042 4316424 s->block_last_index[i] = -1;
1043 }
1044 }
1045
1046 1968007 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
1047
1048 1968007 return 0;
1049 }
1050
1051 263 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1052 {
1053 263 Mpeg1Context *s = avctx->priv_data;
1054 263 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1055
1056
2/2
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 2 times.
263 if ( avctx->codec_tag != AV_RL32("VCR2")
1057
1/2
✓ Branch 0 taken 261 times.
✗ Branch 1 not taken.
261 && avctx->codec_tag != AV_RL32("BW10"))
1058 261 avctx->coded_width = avctx->coded_height = 0; // do not trust dimensions from input
1059 263 ff_mpv_decode_init(s2, avctx);
1060
1061 /* we need some permutation to store matrices,
1062 * until the decoder sets the real permutation. */
1063 263 ff_mpv_idct_init(s2);
1064 263 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1065 263 ff_mpeg12_init_vlcs();
1066
1067 263 s2->chroma_format = 1;
1068 263 s->mpeg_enc_ctx_allocated = 0;
1069 263 s->repeat_field = 0;
1070 263 avctx->color_range = AVCOL_RANGE_MPEG;
1071 263 return 0;
1072 }
1073
1074 #if HAVE_THREADS
1075 static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
1076 const AVCodecContext *avctx_from)
1077 {
1078 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1079 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1080 int err;
1081
1082 if (avctx == avctx_from ||
1083 !ctx_from->mpeg_enc_ctx_allocated ||
1084 !s1->context_initialized)
1085 return 0;
1086
1087 err = ff_mpeg_update_thread_context(avctx, avctx_from);
1088 if (err)
1089 return err;
1090
1091 if (!ctx->mpeg_enc_ctx_allocated)
1092 memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
1093
1094 return 0;
1095 }
1096 #endif
1097
1098 960 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1099 const uint8_t *new_perm)
1100 {
1101 uint16_t temp_matrix[64];
1102 int i;
1103
1104 960 memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
1105
1106
2/2
✓ Branch 0 taken 61440 times.
✓ Branch 1 taken 960 times.
62400 for (i = 0; i < 64; i++)
1107 61440 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1108 960 }
1109
1110 static const enum AVPixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
1111 #if CONFIG_MPEG1_NVDEC_HWACCEL
1112 AV_PIX_FMT_CUDA,
1113 #endif
1114 #if CONFIG_MPEG1_XVMC_HWACCEL
1115 AV_PIX_FMT_XVMC,
1116 #endif
1117 #if CONFIG_MPEG1_VDPAU_HWACCEL
1118 AV_PIX_FMT_VDPAU,
1119 #endif
1120 AV_PIX_FMT_YUV420P,
1121 AV_PIX_FMT_NONE
1122 };
1123
1124 static const enum AVPixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
1125 #if CONFIG_MPEG2_NVDEC_HWACCEL
1126 AV_PIX_FMT_CUDA,
1127 #endif
1128 #if CONFIG_MPEG2_XVMC_HWACCEL
1129 AV_PIX_FMT_XVMC,
1130 #endif
1131 #if CONFIG_MPEG2_VDPAU_HWACCEL
1132 AV_PIX_FMT_VDPAU,
1133 #endif
1134 #if CONFIG_MPEG2_DXVA2_HWACCEL
1135 AV_PIX_FMT_DXVA2_VLD,
1136 #endif
1137 #if CONFIG_MPEG2_D3D11VA_HWACCEL
1138 AV_PIX_FMT_D3D11VA_VLD,
1139 AV_PIX_FMT_D3D11,
1140 #endif
1141 #if CONFIG_MPEG2_VAAPI_HWACCEL
1142 AV_PIX_FMT_VAAPI,
1143 #endif
1144 #if CONFIG_MPEG2_VIDEOTOOLBOX_HWACCEL
1145 AV_PIX_FMT_VIDEOTOOLBOX,
1146 #endif
1147 AV_PIX_FMT_YUV420P,
1148 AV_PIX_FMT_NONE
1149 };
1150
1151 static const enum AVPixelFormat mpeg12_pixfmt_list_422[] = {
1152 AV_PIX_FMT_YUV422P,
1153 AV_PIX_FMT_NONE
1154 };
1155
1156 static const enum AVPixelFormat mpeg12_pixfmt_list_444[] = {
1157 AV_PIX_FMT_YUV444P,
1158 AV_PIX_FMT_NONE
1159 };
1160
1161 242 static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
1162 {
1163 242 Mpeg1Context *s1 = avctx->priv_data;
1164 242 MpegEncContext *s = &s1->mpeg_enc_ctx;
1165 const enum AVPixelFormat *pix_fmts;
1166
1167 if (CONFIG_GRAY && (avctx->flags & AV_CODEC_FLAG_GRAY))
1168 return AV_PIX_FMT_GRAY8;
1169
1170
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 55 times.
242 if (s->chroma_format < 2)
1171 187 pix_fmts = avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO ?
1172
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 161 times.
187 mpeg1_hwaccel_pixfmt_list_420 :
1173 mpeg2_hwaccel_pixfmt_list_420;
1174
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 else if (s->chroma_format == 2)
1175 55 pix_fmts = mpeg12_pixfmt_list_422;
1176 else
1177 pix_fmts = mpeg12_pixfmt_list_444;
1178
1179 242 return ff_thread_get_format(avctx, pix_fmts);
1180 }
1181
1182 242 static void setup_hwaccel_for_pixfmt(AVCodecContext *avctx)
1183 {
1184 // until then pix_fmt may be changed right after codec init
1185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 242 times.
242 if (avctx->hwaccel)
1186 if (avctx->idct_algo == FF_IDCT_AUTO)
1187 avctx->idct_algo = FF_IDCT_NONE;
1188
1189
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 242 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
242 if (avctx->hwaccel && avctx->pix_fmt == AV_PIX_FMT_XVMC) {
1190 Mpeg1Context *s1 = avctx->priv_data;
1191 MpegEncContext *s = &s1->mpeg_enc_ctx;
1192
1193 s->pack_pblocks = 1;
1194 }
1195 242 }
1196
1197 /* Call this function when we know all parameters.
1198 * It may be called in different places for MPEG-1 and MPEG-2. */
1199 4414 static int mpeg_decode_postinit(AVCodecContext *avctx)
1200 {
1201 4414 Mpeg1Context *s1 = avctx->priv_data;
1202 4414 MpegEncContext *s = &s1->mpeg_enc_ctx;
1203 uint8_t old_permutation[64];
1204 int ret;
1205
1206
2/2
✓ Branch 0 taken 494 times.
✓ Branch 1 taken 3920 times.
4414 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1207 // MPEG-1 aspect
1208 494 AVRational aspect_inv = av_d2q(ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1209 494 avctx->sample_aspect_ratio = (AVRational) { aspect_inv.den, aspect_inv.num };
1210 } else { // MPEG-2
1211 // MPEG-2 aspect
1212
2/2
✓ Branch 0 taken 1930 times.
✓ Branch 1 taken 1990 times.
3920 if (s->aspect_ratio_info > 1) {
1213 AVRational dar =
1214 1930 av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1215 1930 (AVRational) { s1->pan_scan.width,
1216 1930 s1->pan_scan.height }),
1217 1930 (AVRational) { s->width, s->height });
1218
1219 /* We ignore the spec here and guess a bit as reality does not
1220 * match the spec, see for example res_change_ffmpeg_aspect.ts
1221 * and sequence-display-aspect.mpg.
1222 * issue1613, 621, 562 */
1223
5/6
✓ Branch 0 taken 432 times.
✓ Branch 1 taken 1498 times.
✓ Branch 2 taken 432 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 418 times.
2362 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1224
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
446 (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
1225 14 av_cmp_q(dar, (AVRational) { 16, 9 }))) {
1226 1512 s->avctx->sample_aspect_ratio =
1227 1512 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1228 1512 (AVRational) { s->width, s->height });
1229 } else {
1230 418 s->avctx->sample_aspect_ratio =
1231 418 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1232 418 (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
1233 // issue1613 4/3 16/9 -> 16/9
1234 // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1235 // widescreen-issue562.mpg 4/3 16/9 -> 16/9
1236 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1237 ff_dlog(avctx, "aspect A %d/%d\n",
1238 ff_mpeg2_aspect[s->aspect_ratio_info].num,
1239 ff_mpeg2_aspect[s->aspect_ratio_info].den);
1240 ff_dlog(avctx, "aspect B %d/%d\n", s->avctx->sample_aspect_ratio.num,
1241 s->avctx->sample_aspect_ratio.den);
1242 }
1243 } else {
1244 1990 s->avctx->sample_aspect_ratio =
1245 1990 ff_mpeg2_aspect[s->aspect_ratio_info];
1246 }
1247 } // MPEG-2
1248
1249
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4414 times.
4414 if (av_image_check_sar(s->width, s->height,
1250 avctx->sample_aspect_ratio) < 0) {
1251 av_log(avctx, AV_LOG_WARNING, "ignoring invalid SAR: %u/%u\n",
1252 avctx->sample_aspect_ratio.num,
1253 avctx->sample_aspect_ratio.den);
1254 avctx->sample_aspect_ratio = (AVRational){ 0, 1 };
1255 }
1256
1257
2/2
✓ Branch 0 taken 4176 times.
✓ Branch 1 taken 238 times.
4414 if ((s1->mpeg_enc_ctx_allocated == 0) ||
1258
1/2
✓ Branch 0 taken 4176 times.
✗ Branch 1 not taken.
4176 avctx->coded_width != s->width ||
1259
1/2
✓ Branch 0 taken 4176 times.
✗ Branch 1 not taken.
4176 avctx->coded_height != s->height ||
1260
1/2
✓ Branch 0 taken 4176 times.
✗ Branch 1 not taken.
4176 s1->save_width != s->width ||
1261
3/4
✓ Branch 0 taken 4176 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4174 times.
✓ Branch 3 taken 2 times.
8352 s1->save_height != s->height ||
1262 4176 av_cmp_q(s1->save_aspect, s->avctx->sample_aspect_ratio) ||
1263
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4174 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4174 (s1->save_progressive_seq != s->progressive_sequence && FFALIGN(s->height, 16) != FFALIGN(s->height, 32)) ||
1264 0) {
1265
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 238 times.
240 if (s1->mpeg_enc_ctx_allocated) {
1266 #if FF_API_FLAG_TRUNCATED
1267 2 ParseContext pc = s->parse_context;
1268 2 s->parse_context.buffer = 0;
1269 2 ff_mpv_common_end(s);
1270 2 s->parse_context = pc;
1271 #else
1272 ff_mpv_common_end(s);
1273 #endif
1274 2 s1->mpeg_enc_ctx_allocated = 0;
1275 }
1276
1277 240 ret = ff_set_dimensions(avctx, s->width, s->height);
1278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 240 times.
240 if (ret < 0)
1279 return ret;
1280
1281
4/4
✓ Branch 0 taken 215 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 213 times.
✓ Branch 3 taken 2 times.
240 if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->bit_rate) {
1282 213 avctx->rc_max_rate = s->bit_rate;
1283
3/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
27 } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && s->bit_rate &&
1284
2/4
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
25 (s->bit_rate != 0x3FFFF*400 || s->vbv_delay != 0xFFFF)) {
1285 25 avctx->bit_rate = s->bit_rate;
1286 }
1287 240 s1->save_aspect = s->avctx->sample_aspect_ratio;
1288 240 s1->save_width = s->width;
1289 240 s1->save_height = s->height;
1290 240 s1->save_progressive_seq = s->progressive_sequence;
1291
1292 /* low_delay may be forced, in this case we will have B-frames
1293 * that behave like P-frames. */
1294 240 avctx->has_b_frames = !s->low_delay;
1295
1296
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 215 times.
240 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1297 // MPEG-1 fps
1298 25 avctx->framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
1299 25 avctx->ticks_per_frame = 1;
1300
1301 25 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1302 } else { // MPEG-2
1303 // MPEG-2 fps
1304 215 av_reduce(&s->avctx->framerate.num,
1305 215 &s->avctx->framerate.den,
1306 215 ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
1307 215 ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1308 1 << 30);
1309 215 avctx->ticks_per_frame = 2;
1310
1311
2/3
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 55 times.
✗ Branch 2 not taken.
215 switch (s->chroma_format) {
1312 160 case 1: avctx->chroma_sample_location = AVCHROMA_LOC_LEFT; break;
1313 55 case 2:
1314 55 case 3: avctx->chroma_sample_location = AVCHROMA_LOC_TOPLEFT; break;
1315 default: av_assert0(0);
1316 }
1317 } // MPEG-2
1318
1319 240 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1320 240 setup_hwaccel_for_pixfmt(avctx);
1321
1322 /* Quantization matrices may need reordering
1323 * if DCT permutation is changed. */
1324 240 memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
1325
1326 240 ff_mpv_idct_init(s);
1327
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
240 if ((ret = ff_mpv_common_init(s)) < 0)
1328 return ret;
1329
1330 240 quant_matrix_rebuild(s->intra_matrix, old_permutation, s->idsp.idct_permutation);
1331 240 quant_matrix_rebuild(s->inter_matrix, old_permutation, s->idsp.idct_permutation);
1332 240 quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
1333 240 quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
1334
1335 240 s1->mpeg_enc_ctx_allocated = 1;
1336 }
1337 4414 return 0;
1338 }
1339
1340 4414 static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
1341 int buf_size)
1342 {
1343 4414 Mpeg1Context *s1 = avctx->priv_data;
1344 4414 MpegEncContext *s = &s1->mpeg_enc_ctx;
1345 int ref, f_code, vbv_delay, ret;
1346
1347 4414 ret = init_get_bits8(&s->gb, buf, buf_size);
1348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4414 times.
4414 if (ret < 0)
1349 return ret;
1350
1351 4414 ref = get_bits(&s->gb, 10); /* temporal ref */
1352 4414 s->pict_type = get_bits(&s->gb, 3);
1353
2/4
✓ Branch 0 taken 4414 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4414 times.
4414 if (s->pict_type == 0 || s->pict_type > 3)
1354 return AVERROR_INVALIDDATA;
1355
1356 4414 vbv_delay = get_bits(&s->gb, 16);
1357 4414 s->vbv_delay = vbv_delay;
1358
2/2
✓ Branch 0 taken 2187 times.
✓ Branch 1 taken 2227 times.
4414 if (s->pict_type == AV_PICTURE_TYPE_P ||
1359
2/2
✓ Branch 0 taken 1520 times.
✓ Branch 1 taken 667 times.
2187 s->pict_type == AV_PICTURE_TYPE_B) {
1360 3747 s->full_pel[0] = get_bits1(&s->gb);
1361 3747 f_code = get_bits(&s->gb, 3);
1362
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3747 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3747 if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1363 return AVERROR_INVALIDDATA;
1364 3747 f_code += !f_code;
1365 3747 s->mpeg_f_code[0][0] = f_code;
1366 3747 s->mpeg_f_code[0][1] = f_code;
1367 }
1368
2/2
✓ Branch 0 taken 1520 times.
✓ Branch 1 taken 2894 times.
4414 if (s->pict_type == AV_PICTURE_TYPE_B) {
1369 1520 s->full_pel[1] = get_bits1(&s->gb);
1370 1520 f_code = get_bits(&s->gb, 3);
1371
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1520 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1520 if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1372 return AVERROR_INVALIDDATA;
1373 1520 f_code += !f_code;
1374 1520 s->mpeg_f_code[1][0] = f_code;
1375 1520 s->mpeg_f_code[1][1] = f_code;
1376 }
1377 4414 s->current_picture.f->pict_type = s->pict_type;
1378 4414 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1379
1380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4414 times.
4414 if (avctx->debug & FF_DEBUG_PICT_INFO)
1381 av_log(avctx, AV_LOG_DEBUG,
1382 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1383
1384 4414 s->y_dc_scale = 8;
1385 4414 s->c_dc_scale = 8;
1386 4414 return 0;
1387 }
1388
1389 654 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1390 {
1391 654 MpegEncContext *s = &s1->mpeg_enc_ctx;
1392 int horiz_size_ext, vert_size_ext;
1393 int bit_rate_ext;
1394 AVCPBProperties *cpb_props;
1395
1396 654 skip_bits(&s->gb, 1); /* profile and level esc*/
1397 654 s->avctx->profile = get_bits(&s->gb, 3);
1398 654 s->avctx->level = get_bits(&s->gb, 4);
1399 654 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1400 654 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1401
1402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 654 times.
654 if (!s->chroma_format) {
1403 s->chroma_format = 1;
1404 av_log(s->avctx, AV_LOG_WARNING, "Chroma format invalid\n");
1405 }
1406
1407 654 horiz_size_ext = get_bits(&s->gb, 2);
1408 654 vert_size_ext = get_bits(&s->gb, 2);
1409 654 s->width |= (horiz_size_ext << 12);
1410 654 s->height |= (vert_size_ext << 12);
1411 654 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1412 654 s->bit_rate += (bit_rate_ext << 18) * 400LL;
1413 654 check_marker(s->avctx, &s->gb, "after bit rate extension");
1414 654 s1->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1415
1416 654 s->low_delay = get_bits1(&s->gb);
1417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 654 times.
654 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
1418 s->low_delay = 1;
1419
1420 654 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1421 654 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1422
1423 ff_dlog(s->avctx, "sequence extension\n");
1424 654 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1425
1426
1/2
✓ Branch 1 taken 654 times.
✗ Branch 2 not taken.
654 if (cpb_props = ff_add_cpb_side_data(s->avctx)) {
1427 654 cpb_props->buffer_size = s1->rc_buffer_size;
1428
2/2
✓ Branch 0 taken 239 times.
✓ Branch 1 taken 415 times.
654 if (s->bit_rate != 0x3FFFF*400)
1429 239 cpb_props->max_bitrate = s->bit_rate;
1430 }
1431
1432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 654 times.
654 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1433 av_log(s->avctx, AV_LOG_DEBUG,
1434 "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%"PRId64"\n",
1435 s->avctx->profile, s->avctx->level, s->progressive_sequence, s->chroma_format,
1436 s1->rc_buffer_size, s->bit_rate);
1437 654 }
1438
1439 76 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1440 {
1441 76 MpegEncContext *s = &s1->mpeg_enc_ctx;
1442 int color_description, w, h;
1443
1444 76 skip_bits(&s->gb, 3); /* video format */
1445 76 color_description = get_bits1(&s->gb);
1446
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 45 times.
76 if (color_description) {
1447 31 s->avctx->color_primaries = get_bits(&s->gb, 8);
1448 31 s->avctx->color_trc = get_bits(&s->gb, 8);
1449 31 s->avctx->colorspace = get_bits(&s->gb, 8);
1450 }
1451 76 w = get_bits(&s->gb, 14);
1452 76 skip_bits(&s->gb, 1); // marker
1453 76 h = get_bits(&s->gb, 14);
1454 // remaining 3 bits are zero padding
1455
1456 76 s1->pan_scan.width = 16 * w;
1457 76 s1->pan_scan.height = 16 * h;
1458
1459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1460 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1461 76 }
1462
1463 15 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1464 {
1465 15 MpegEncContext *s = &s1->mpeg_enc_ctx;
1466 int i, nofco;
1467
1468 15 nofco = 1;
1469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (s->progressive_sequence) {
1470 if (s->repeat_first_field) {
1471 nofco++;
1472 if (s->top_field_first)
1473 nofco++;
1474 }
1475 } else {
1476
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 10 times.
15 if (s->picture_structure == PICT_FRAME) {
1477 5 nofco++;
1478
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 if (s->repeat_first_field)
1479 3 nofco++;
1480 }
1481 }
1482
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 15 times.
38 for (i = 0; i < nofco; i++) {
1483 23 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1484 23 skip_bits(&s->gb, 1); // marker
1485 23 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1486 23 skip_bits(&s->gb, 1); // marker
1487 }
1488
1489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1490 av_log(s->avctx, AV_LOG_DEBUG,
1491 "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
1492 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1493 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1494 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1495 15 }
1496
1497 150 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
1498 uint16_t matrix1[64], int intra)
1499 {
1500 int i;
1501
1502
2/2
✓ Branch 0 taken 9600 times.
✓ Branch 1 taken 150 times.
9750 for (i = 0; i < 64; i++) {
1503 9600 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1504 9600 int v = get_bits(&s->gb, 8);
1505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9600 times.
9600 if (v == 0) {
1506 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1507 return AVERROR_INVALIDDATA;
1508 }
1509
5/6
✓ Branch 0 taken 2624 times.
✓ Branch 1 taken 6976 times.
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 2583 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 41 times.
9600 if (intra && i == 0 && v != 8) {
1510 av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v);
1511 v = 8; // needed by pink.mpg / issue1046
1512 }
1513 9600 matrix0[j] = v;
1514
2/2
✓ Branch 0 taken 9216 times.
✓ Branch 1 taken 384 times.
9600 if (matrix1)
1515 9216 matrix1[j] = v;
1516 }
1517 150 return 0;
1518 }
1519
1520 531 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1521 {
1522 ff_dlog(s->avctx, "matrix extension\n");
1523
1524
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 513 times.
531 if (get_bits1(&s->gb))
1525 18 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1526
2/2
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 490 times.
531 if (get_bits1(&s->gb))
1527 41 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1528
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 526 times.
531 if (get_bits1(&s->gb))
1529 5 load_matrix(s, s->chroma_intra_matrix, NULL, 1);
1530
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 530 times.
531 if (get_bits1(&s->gb))
1531 1 load_matrix(s, s->chroma_inter_matrix, NULL, 0);
1532 531 }
1533
1534 3920 static int mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1535 {
1536 3920 MpegEncContext *s = &s1->mpeg_enc_ctx;
1537
1538 3920 s->full_pel[0] = s->full_pel[1] = 0;
1539 3920 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1540 3920 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1541 3920 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1542 3920 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1543 3920 s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0];
1544 3920 s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1];
1545 3920 s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0];
1546 3920 s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1];
1547
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3920 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3920 if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1548 av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code\n");
1549 if (s->avctx->err_recognition & AV_EF_EXPLODE)
1550 return AVERROR_INVALIDDATA;
1551 av_log(s->avctx, AV_LOG_WARNING, "Guessing pict_type from mpeg_f_code\n");
1552 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1553 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1554 s->pict_type = AV_PICTURE_TYPE_I;
1555 else
1556 s->pict_type = AV_PICTURE_TYPE_P;
1557 } else
1558 s->pict_type = AV_PICTURE_TYPE_B;
1559 s->current_picture.f->pict_type = s->pict_type;
1560 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1561 }
1562
1563 3920 s->intra_dc_precision = get_bits(&s->gb, 2);
1564 3920 s->picture_structure = get_bits(&s->gb, 2);
1565 3920 s->top_field_first = get_bits1(&s->gb);
1566 3920 s->frame_pred_frame_dct = get_bits1(&s->gb);
1567 3920 s->concealment_motion_vectors = get_bits1(&s->gb);
1568 3920 s->q_scale_type = get_bits1(&s->gb);
1569 3920 s->intra_vlc_format = get_bits1(&s->gb);
1570 3920 s->alternate_scan = get_bits1(&s->gb);
1571 3920 s->repeat_first_field = get_bits1(&s->gb);
1572 3920 s->chroma_420_type = get_bits1(&s->gb);
1573 3920 s->progressive_frame = get_bits1(&s->gb);
1574
1575
2/2
✓ Branch 0 taken 811 times.
✓ Branch 1 taken 3109 times.
3920 if (s->alternate_scan) {
1576 811 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
1577 811 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
1578 } else {
1579 3109 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
1580 3109 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
1581 }
1582
1583 /* composite display not parsed */
1584 ff_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1585 ff_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1586 ff_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1587 ff_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1588 ff_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1589 ff_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1590 ff_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1591 ff_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1592 ff_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1593
1594 3920 return 0;
1595 }
1596
1597 4080 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1598 {
1599 4080 AVCodecContext *avctx = s->avctx;
1600 4080 Mpeg1Context *s1 = (Mpeg1Context *) s;
1601 int ret;
1602
1603
1/2
✓ Branch 0 taken 4080 times.
✗ Branch 1 not taken.
4080 if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {
1604
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4080 times.
4080 if (s->mb_width * s->mb_height * 11LL / (33 * 2 * 8) > buf_size)
1605 return AVERROR_INVALIDDATA;
1606 }
1607
1608 /* start frame decoding */
1609
4/4
✓ Branch 0 taken 3538 times.
✓ Branch 1 taken 542 times.
✓ Branch 2 taken 2996 times.
✓ Branch 3 taken 542 times.
7618 if (s->first_field || s->picture_structure == PICT_FRAME) {
1610 AVFrameSideData *pan_scan;
1611
1612
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3538 times.
3538 if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
1613 return ret;
1614
1615 3538 ff_mpeg_er_frame_start(s);
1616
1617 /* first check if we must repeat the frame */
1618 3538 s->current_picture_ptr->f->repeat_pict = 0;
1619
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3535 times.
3538 if (s->repeat_first_field) {
1620
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (s->progressive_sequence) {
1621 if (s->top_field_first)
1622 s->current_picture_ptr->f->repeat_pict = 4;
1623 else
1624 s->current_picture_ptr->f->repeat_pict = 2;
1625
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 } else if (s->progressive_frame) {
1626 3 s->current_picture_ptr->f->repeat_pict = 1;
1627 }
1628 }
1629
1630 3538 pan_scan = av_frame_new_side_data(s->current_picture_ptr->f,
1631 AV_FRAME_DATA_PANSCAN,
1632 sizeof(s1->pan_scan));
1633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3538 times.
3538 if (!pan_scan)
1634 return AVERROR(ENOMEM);
1635 3538 memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
1636
1637
2/2
✓ Branch 0 taken 607 times.
✓ Branch 1 taken 2931 times.
3538 if (s1->a53_buf_ref) {
1638 607 AVFrameSideData *sd = av_frame_new_side_data_from_buf(
1639 607 s->current_picture_ptr->f, AV_FRAME_DATA_A53_CC,
1640 s1->a53_buf_ref);
1641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 607 times.
607 if (!sd)
1642 av_buffer_unref(&s1->a53_buf_ref);
1643 607 s1->a53_buf_ref = NULL;
1644 }
1645
1646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3538 times.
3538 if (s1->has_stereo3d) {
1647 AVStereo3D *stereo = av_stereo3d_create_side_data(s->current_picture_ptr->f);
1648 if (!stereo)
1649 return AVERROR(ENOMEM);
1650
1651 *stereo = s1->stereo3d;
1652 s1->has_stereo3d = 0;
1653 }
1654
1655
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 3336 times.
3538 if (s1->has_afd) {
1656 AVFrameSideData *sd =
1657 202 av_frame_new_side_data(s->current_picture_ptr->f,
1658 AV_FRAME_DATA_AFD, 1);
1659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
202 if (!sd)
1660 return AVERROR(ENOMEM);
1661
1662 202 *sd->data = s1->afd;
1663 202 s1->has_afd = 0;
1664 }
1665
1666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3538 times.
3538 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1667 ff_thread_finish_setup(avctx);
1668 } else { // second field
1669 int i;
1670
1671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 542 times.
542 if (!s->current_picture_ptr) {
1672 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1673 return AVERROR_INVALIDDATA;
1674 }
1675
1676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 542 times.
542 if (s->avctx->hwaccel) {
1677 if ((ret = s->avctx->hwaccel->end_frame(s->avctx)) < 0) {
1678 av_log(avctx, AV_LOG_ERROR,
1679 "hardware accelerator failed to decode first field\n");
1680 return ret;
1681 }
1682 }
1683
1684
2/2
✓ Branch 0 taken 2168 times.
✓ Branch 1 taken 542 times.
2710 for (i = 0; i < 4; i++) {
1685 2168 s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
1686
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2164 times.
2168 if (s->picture_structure == PICT_BOTTOM_FIELD)
1687 4 s->current_picture.f->data[i] +=
1688 4 s->current_picture_ptr->f->linesize[i];
1689 }
1690 }
1691
1692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4080 times.
4080 if (avctx->hwaccel) {
1693 if ((ret = avctx->hwaccel->start_frame(avctx, buf, buf_size)) < 0)
1694 return ret;
1695 }
1696
1697 4080 return 0;
1698 }
1699
1700 #define DECODE_SLICE_ERROR -1
1701 #define DECODE_SLICE_OK 0
1702
1703 /**
1704 * Decode a slice.
1705 * MpegEncContext.mb_y must be set to the MB row from the startcode.
1706 * @return DECODE_SLICE_ERROR if the slice is damaged,
1707 * DECODE_SLICE_OK if this slice is OK
1708 */
1709 112865 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1710 const uint8_t **buf, int buf_size)
1711 {
1712 112865 AVCodecContext *avctx = s->avctx;
1713 112865 const int lowres = s->avctx->lowres;
1714 112865 const int field_pic = s->picture_structure != PICT_FRAME;
1715 int ret;
1716
1717 112865 s->resync_mb_x =
1718 112865 s->resync_mb_y = -1;
1719
1720
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112865 times.
112865 av_assert0(mb_y < s->mb_height);
1721
1722 112865 init_get_bits(&s->gb, *buf, buf_size * 8);
1723
3/4
✓ Branch 0 taken 105592 times.
✓ Branch 1 taken 7273 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 105592 times.
112865 if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
1724 skip_bits(&s->gb, 3);
1725
1726 112865 ff_mpeg1_clean_buffers(s);
1727 112865 s->interlaced_dct = 0;
1728
1729 112865 s->qscale = mpeg_get_qscale(s);
1730
1731
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112865 times.
112865 if (s->qscale == 0) {
1732 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1733 return AVERROR_INVALIDDATA;
1734 }
1735
1736 /* extra slice info */
1737
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 112865 times.
112865 if (skip_1stop_8data_bits(&s->gb) < 0)
1738 return AVERROR_INVALIDDATA;
1739
1740 112865 s->mb_x = 0;
1741
1742
3/4
✓ Branch 0 taken 5001 times.
✓ Branch 1 taken 107864 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5001 times.
112865 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1743 skip_bits1(&s->gb);
1744 } else {
1745
1/2
✓ Branch 1 taken 125064 times.
✗ Branch 2 not taken.
125064 while (get_bits_left(&s->gb) > 0) {
1746 125064 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1747 MBINCR_VLC_BITS, 2);
1748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125064 times.
125064 if (code < 0) {
1749 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1750 return AVERROR_INVALIDDATA;
1751 }
1752
2/2
✓ Branch 0 taken 12199 times.
✓ Branch 1 taken 112865 times.
125064 if (code >= 33) {
1753
1/2
✓ Branch 0 taken 12199 times.
✗ Branch 1 not taken.
12199 if (code == 33)
1754 12199 s->mb_x += 33;
1755 /* otherwise, stuffing, nothing to do */
1756 } else {
1757 112865 s->mb_x += code;
1758 112865 break;
1759 }
1760 }
1761 }
1762
1763
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112865 times.
112865 if (s->mb_x >= (unsigned) s->mb_width) {
1764 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1765 return AVERROR_INVALIDDATA;
1766 }
1767
1768
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 112865 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
112865 if (avctx->hwaccel && avctx->hwaccel->decode_slice) {
1769 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1770 int start_code = -1;
1771 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1772 if (buf_end < *buf + buf_size)
1773 buf_end -= 4;
1774 s->mb_y = mb_y;
1775 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1776 return DECODE_SLICE_ERROR;
1777 *buf = buf_end;
1778 return DECODE_SLICE_OK;
1779 }
1780
1781 112865 s->resync_mb_x = s->mb_x;
1782 112865 s->resync_mb_y = s->mb_y = mb_y;
1783 112865 s->mb_skip_run = 0;
1784 112865 ff_init_block_index(s);
1785
1786
8/8
✓ Branch 0 taken 5001 times.
✓ Branch 1 taken 107864 times.
✓ Branch 2 taken 3538 times.
✓ Branch 3 taken 1463 times.
✓ Branch 4 taken 3537 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 2996 times.
✓ Branch 7 taken 541 times.
112865 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2997 times.
2997 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1788 av_log(s->avctx, AV_LOG_DEBUG,
1789 "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",
1790 s->qscale,
1791 s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
1792 s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1793 s->pict_type == AV_PICTURE_TYPE_I ? 'I' :
1794 (s->pict_type == AV_PICTURE_TYPE_P ? 'P' :
1795 (s->pict_type == AV_PICTURE_TYPE_B ? 'B' : 'S')),
1796 s->progressive_sequence ? "ps" : "",
1797 s->progressive_frame ? "pf" : "",
1798 s->alternate_scan ? "alt" : "",
1799 s->top_field_first ? "top" : "",
1800 s->intra_dc_precision, s->picture_structure,
1801 s->frame_pred_frame_dct, s->concealment_motion_vectors,
1802 s->q_scale_type, s->intra_vlc_format,
1803 s->repeat_first_field, s->chroma_420_type ? "420" : "");
1804 }
1805 }
1806
1807 for (;;) {
1808 // If 1, we memcpy blocks in xvmcvideo.
1809 2011313 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
1810 ff_xvmc_init_block(s); // set s->block
1811
1812
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 2124174 times.
2124178 if ((ret = mpeg_decode_mb(s, s->block)) < 0)
1813 4 return ret;
1814
1815 // Note motion_val is normally NULL unless we want to extract the MVs.
1816
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2124174 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2124174 if (s->current_picture.motion_val[0] && !s->encoding) {
1817 const int wrap = s->b8_stride;
1818 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1819 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1820 int motion_x, motion_y, dir, i;
1821
1822 for (i = 0; i < 2; i++) {
1823 for (dir = 0; dir < 2; dir++) {
1824 if (s->mb_intra ||
1825 (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1826 motion_x = motion_y = 0;
1827 } else if (s->mv_type == MV_TYPE_16X16 ||
1828 (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1829 motion_x = s->mv[dir][0][0];
1830 motion_y = s->mv[dir][0][1];
1831 } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
1832 motion_x = s->mv[dir][i][0];
1833 motion_y = s->mv[dir][i][1];
1834 }
1835
1836 s->current_picture.motion_val[dir][xy][0] = motion_x;
1837 s->current_picture.motion_val[dir][xy][1] = motion_y;
1838 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1839 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1840 s->current_picture.ref_index [dir][b8_xy] =
1841 s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1842 av_assert2(s->field_select[dir][i] == 0 ||
1843 s->field_select[dir][i] == 1);
1844 }
1845 xy += wrap;
1846 b8_xy += 2;
1847 }
1848 }
1849
1850 2124174 s->dest[0] += 16 >> lowres;
1851 2124174 s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
1852 2124174 s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
1853
1854 2124174 ff_mpv_reconstruct_mb(s, s->block);
1855
1856
2/2
✓ Branch 0 taken 67944 times.
✓ Branch 1 taken 2056230 times.
2124174 if (++s->mb_x >= s->mb_width) {
1857 67944 const int mb_size = 16 >> s->avctx->lowres;
1858 int left;
1859
1860 67944 ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
1861 67944 ff_mpv_report_decode_progress(s);
1862
1863 67944 s->mb_x = 0;
1864 67944 s->mb_y += 1 << field_pic;
1865
1866
2/2
✓ Branch 0 taken 4076 times.
✓ Branch 1 taken 63868 times.
67944 if (s->mb_y >= s->mb_height) {
1867 4076 int left = get_bits_left(&s->gb);
1868 8413 int is_d10 = s->chroma_format == 2 &&
1869
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 212 times.
261 s->pict_type == AV_PICTURE_TYPE_I &&
1870
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 &&
1871
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 24 times.
49 s->intra_dc_precision == 2 &&
1872
4/6
✓ Branch 0 taken 261 times.
✓ Branch 1 taken 3815 times.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25 times.
✗ Branch 5 not taken.
4362 s->q_scale_type == 1 && s->alternate_scan == 0 &&
1873
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 s->progressive_frame == 0
1874 /* vbv_delay == 0xBBB || 0xE10 */;
1875
1876
4/4
✓ Branch 0 taken 1069 times.
✓ Branch 1 taken 3007 times.
✓ Branch 2 taken 1044 times.
✓ Branch 3 taken 25 times.
4076 if (left >= 32 && !is_d10) {
1877 1044 GetBitContext gb = s->gb;
1878 1044 align_get_bits(&gb);
1879
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1044 times.
1044 if (show_bits(&gb, 24) == 0x060E2B) {
1880 av_log(avctx, AV_LOG_DEBUG, "Invalid MXF data found in video stream\n");
1881 is_d10 = 1;
1882 }
1883
3/4
✓ Branch 0 taken 1043 times.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1043 times.
1044 if (left > 32 && show_bits_long(&gb, 32) == 0x201) {
1884 av_log(avctx, AV_LOG_DEBUG, "skipping m704 alpha (unsupported)\n");
1885 goto eos;
1886 }
1887 }
1888
1889
3/4
✓ Branch 0 taken 4076 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3783 times.
✓ Branch 3 taken 293 times.
4076 if (left < 0 ||
1890
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 3783 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
3783 (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
1891
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4076 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4076 ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) {
1892 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X at %d %d\n",
1893 left, left>0 ? show_bits(&s->gb, FFMIN(left, 23)) : 0, s->mb_x, s->mb_y);
1894 return AVERROR_INVALIDDATA;
1895 } else
1896 4076 goto eos;
1897 }
1898 // There are some files out there which are missing the last slice
1899 // in cases where the slice is completely outside the visible
1900 // area, we detect this here instead of running into the end expecting
1901 // more data
1902 63868 left = get_bits_left(&s->gb);
1903
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 63668 times.
63868 if (s->mb_y >= ((s->height + 15) >> 4) &&
1904
2/4
✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 200 times.
200 !s->progressive_sequence &&
1905 left <= 25 &&
1906 left >= 0 &&
1907 s->mb_skip_run == -1 &&
1908 (!left || show_bits(&s->gb, left) == 0))
1909 goto eos;
1910
1911 63868 ff_init_block_index(s);
1912 }
1913
1914 /* skip mb handling */
1915
2/2
✓ Branch 0 taken 1963931 times.
✓ Branch 1 taken 156167 times.
2120098 if (s->mb_skip_run == -1) {
1916 /* read increment again */
1917 1963931 s->mb_skip_run = 0;
1918 1229 for (;;) {
1919 1965160 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1920 MBINCR_VLC_BITS, 2);
1921
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1965160 times.
1965160 if (code < 0) {
1922 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1923 return AVERROR_INVALIDDATA;
1924 }
1925
2/2
✓ Branch 0 taken 110014 times.
✓ Branch 1 taken 1855146 times.
1965160 if (code >= 33) {
1926
2/2
✓ Branch 0 taken 1229 times.
✓ Branch 1 taken 108785 times.
110014 if (code == 33) {
1927 1229 s->mb_skip_run += 33;
1928
1/2
✓ Branch 0 taken 108785 times.
✗ Branch 1 not taken.
108785 } else if (code == 35) {
1929
2/4
✓ Branch 0 taken 108785 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 108785 times.
108785 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1930 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1931 return AVERROR_INVALIDDATA;
1932 }
1933 108785 goto eos; /* end of slice */
1934 }
1935 /* otherwise, stuffing, nothing to do */
1936 } else {
1937 1855146 s->mb_skip_run += code;
1938 1855146 break;
1939 }
1940 }
1941
2/2
✓ Branch 0 taken 57069 times.
✓ Branch 1 taken 1798077 times.
1855146 if (s->mb_skip_run) {
1942 int i;
1943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57069 times.
57069 if (s->pict_type == AV_PICTURE_TYPE_I) {
1944 av_log(s->avctx, AV_LOG_ERROR,
1945 "skipped MB in I-frame at %d %d\n", s->mb_x, s->mb_y);
1946 return AVERROR_INVALIDDATA;
1947 }
1948
1949 /* skip mb */
1950 57069 s->mb_intra = 0;
1951
2/2
✓ Branch 0 taken 684828 times.
✓ Branch 1 taken 57069 times.
741897 for (i = 0; i < 12; i++)
1952 684828 s->block_last_index[i] = -1;
1953
2/2
✓ Branch 0 taken 39925 times.
✓ Branch 1 taken 17144 times.
57069 if (s->picture_structure == PICT_FRAME)
1954 39925 s->mv_type = MV_TYPE_16X16;
1955 else
1956 17144 s->mv_type = MV_TYPE_FIELD;
1957
2/2
✓ Branch 0 taken 20233 times.
✓ Branch 1 taken 36836 times.
57069 if (s->pict_type == AV_PICTURE_TYPE_P) {
1958 /* if P type, zero motion vector is implied */
1959 20233 s->mv_dir = MV_DIR_FORWARD;
1960 20233 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1961 20233 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1962 20233 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1963 20233 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1964 } else {
1965 /* if B type, reuse previous vectors and directions */
1966 36836 s->mv[0][0][0] = s->last_mv[0][0][0];
1967 36836 s->mv[0][0][1] = s->last_mv[0][0][1];
1968 36836 s->mv[1][0][0] = s->last_mv[1][0][0];
1969 36836 s->mv[1][0][1] = s->last_mv[1][0][1];
1970 36836 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1971 36836 s->field_select[1][0] = (s->picture_structure - 1) & 1;
1972 }
1973 }
1974 }
1975 }
1976 112861 eos: // end of slice
1977
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 112861 times.
112861 if (get_bits_left(&s->gb) < 0) {
1978 av_log(s, AV_LOG_ERROR, "overread %d\n", -get_bits_left(&s->gb));
1979 return AVERROR_INVALIDDATA;
1980 }
1981 112861 *buf += (get_bits_count(&s->gb) - 1) / 8;
1982 ff_dlog(s, "Slice start:%d %d end:%d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1983 112861 return 0;
1984 }
1985
1986 5463 static int slice_decode_thread(AVCodecContext *c, void *arg)
1987 {
1988 5463 MpegEncContext *s = *(void **) arg;
1989 5463 const uint8_t *buf = s->gb.buffer;
1990 5463 int mb_y = s->start_mb_y;
1991 5463 const int field_pic = s->picture_structure != PICT_FRAME;
1992
1993 5463 s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
1994
1995 12747 for (;;) {
1996 uint32_t start_code;
1997 int ret;
1998
1999 18210 ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
2000 18210 emms_c();
2001 ff_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
2002 ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
2003 s->start_mb_y, s->end_mb_y, s->er.error_count);
2004
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18210 times.
18210 if (ret < 0) {
2005 if (c->err_recognition & AV_EF_EXPLODE)
2006 5463 return ret;
2007 if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
2008 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
2009 s->mb_x, s->mb_y,
2010 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
2011 } else {
2012 18210 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
2013 18210 s->mb_x - 1, s->mb_y,
2014 ER_AC_END | ER_DC_END | ER_MV_END);
2015 }
2016
2017
2/2
✓ Branch 0 taken 5463 times.
✓ Branch 1 taken 12747 times.
18210 if (s->mb_y == s->end_mb_y)
2018 5463 return 0;
2019
2020 12747 start_code = -1;
2021 12747 buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
2022
2/4
✓ Branch 0 taken 12747 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12747 times.
12747 if (start_code < SLICE_MIN_START_CODE || start_code > SLICE_MAX_START_CODE)
2023 return AVERROR_INVALIDDATA;
2024 12747 mb_y = start_code - SLICE_MIN_START_CODE;
2025
2/4
✓ Branch 0 taken 12747 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12747 times.
12747 if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
2026 mb_y += (*buf&0xE0)<<2;
2027 12747 mb_y <<= field_pic;
2028
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12747 times.
12747 if (s->picture_structure == PICT_BOTTOM_FIELD)
2029 mb_y++;
2030
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12747 times.
12747 if (mb_y >= s->end_mb_y)
2031 return AVERROR_INVALIDDATA;
2032 }
2033 }
2034
2035 /**
2036 * Handle slice ends.
2037 * @return 1 if it seems to be the last slice
2038 */
2039 3795 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
2040 {
2041 3795 Mpeg1Context *s1 = avctx->priv_data;
2042 3795 MpegEncContext *s = &s1->mpeg_enc_ctx;
2043
2044
3/4
✓ Branch 0 taken 3539 times.
✓ Branch 1 taken 256 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3539 times.
3795 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
2045 256 return 0;
2046
2047
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3539 times.
3539 if (s->avctx->hwaccel) {
2048 int ret = s->avctx->hwaccel->end_frame(s->avctx);
2049 if (ret < 0) {
2050 av_log(avctx, AV_LOG_ERROR,
2051 "hardware accelerator failed to decode picture\n");
2052 return ret;
2053 }
2054 }
2055
2056 /* end of slice reached */
2057
3/4
✓ Branch 0 taken 3538 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3538 times.
✗ Branch 3 not taken.
3539 if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field && !s1->first_slice) {
2058 /* end of image */
2059
2060 3538 ff_er_frame_end(&s->er);
2061
2062 3538 ff_mpv_frame_end(s);
2063
2064
4/4
✓ Branch 0 taken 2215 times.
✓ Branch 1 taken 1323 times.
✓ Branch 2 taken 368 times.
✓ Branch 3 taken 1847 times.
5229 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
2065 1691 int ret = av_frame_ref(pict, s->current_picture_ptr->f);
2066
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1691 times.
1691 if (ret < 0)
2067 return ret;
2068 1691 ff_print_debug_info(s, s->current_picture_ptr, pict);
2069 1691 ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG2);
2070 } else {
2071 /* latency of 1 frame for I- and P-frames */
2072
2/2
✓ Branch 0 taken 1788 times.
✓ Branch 1 taken 59 times.
1847 if (s->last_picture_ptr) {
2073 1788 int ret = av_frame_ref(pict, s->last_picture_ptr->f);
2074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1788 times.
1788 if (ret < 0)
2075 return ret;
2076 1788 ff_print_debug_info(s, s->last_picture_ptr, pict);
2077 1788 ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG2);
2078 }
2079 }
2080
2081 3538 return 1;
2082 } else {
2083 1 return 0;
2084 }
2085 }
2086
2087 746 static int mpeg1_decode_sequence(AVCodecContext *avctx,
2088 const uint8_t *buf, int buf_size)
2089 {
2090 746 Mpeg1Context *s1 = avctx->priv_data;
2091 746 MpegEncContext *s = &s1->mpeg_enc_ctx;
2092 int width, height;
2093 int i, v, j;
2094
2095 746 init_get_bits(&s->gb, buf, buf_size * 8);
2096
2097 746 width = get_bits(&s->gb, 12);
2098 746 height = get_bits(&s->gb, 12);
2099
2/4
✓ Branch 0 taken 746 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 746 times.
746 if (width == 0 || height == 0) {
2100 av_log(avctx, AV_LOG_WARNING,
2101 "Invalid horizontal or vertical size value.\n");
2102 if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
2103 return AVERROR_INVALIDDATA;
2104 }
2105 746 s->aspect_ratio_info = get_bits(&s->gb, 4);
2106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 746 times.
746 if (s->aspect_ratio_info == 0) {
2107 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
2108 if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
2109 return AVERROR_INVALIDDATA;
2110 }
2111 746 s->frame_rate_index = get_bits(&s->gb, 4);
2112
2/4
✓ Branch 0 taken 746 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 746 times.
746 if (s->frame_rate_index == 0 || s->frame_rate_index > 13) {
2113 av_log(avctx, AV_LOG_WARNING,
2114 "frame_rate_index %d is invalid\n", s->frame_rate_index);
2115 s->frame_rate_index = 1;
2116 }
2117 746 s->bit_rate = get_bits(&s->gb, 18) * 400LL;
2118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 746 times.
746 if (check_marker(s->avctx, &s->gb, "in sequence header") == 0) {
2119 return AVERROR_INVALIDDATA;
2120 }
2121
2122 746 s1->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
2123 746 skip_bits(&s->gb, 1);
2124
2125 /* get matrix */
2126
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 728 times.
746 if (get_bits1(&s->gb)) {
2127 18 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
2128 } else {
2129
2/2
✓ Branch 0 taken 46592 times.
✓ Branch 1 taken 728 times.
47320 for (i = 0; i < 64; i++) {
2130 46592 j = s->idsp.idct_permutation[i];
2131 46592 v = ff_mpeg1_default_intra_matrix[i];
2132 46592 s->intra_matrix[j] = v;
2133 46592 s->chroma_intra_matrix[j] = v;
2134 }
2135 }
2136
2/2
✓ Branch 1 taken 67 times.
✓ Branch 2 taken 679 times.
746 if (get_bits1(&s->gb)) {
2137 67 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
2138 } else {
2139
2/2
✓ Branch 0 taken 43456 times.
✓ Branch 1 taken 679 times.
44135 for (i = 0; i < 64; i++) {
2140 43456 int j = s->idsp.idct_permutation[i];
2141 43456 v = ff_mpeg1_default_non_intra_matrix[i];
2142 43456 s->inter_matrix[j] = v;
2143 43456 s->chroma_inter_matrix[j] = v;
2144 }
2145 }
2146
2147
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 746 times.
746 if (show_bits(&s->gb, 23) != 0) {
2148 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2149 return AVERROR_INVALIDDATA;
2150 }
2151
2152 746 s->width = width;
2153 746 s->height = height;
2154
2155 /* We set MPEG-2 parameters so that it emulates MPEG-1. */
2156 746 s->progressive_sequence = 1;
2157 746 s->progressive_frame = 1;
2158 746 s->picture_structure = PICT_FRAME;
2159 746 s->first_field = 0;
2160 746 s->frame_pred_frame_dct = 1;
2161 746 s->chroma_format = 1;
2162 746 s->codec_id =
2163 746 s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
2164 746 s->out_format = FMT_MPEG1;
2165 746 s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
2166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 746 times.
746 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
2167 s->low_delay = 1;
2168
2169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 746 times.
746 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2170 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%"PRId64", aspect_ratio_info: %d \n",
2171 s1->rc_buffer_size, s->bit_rate, s->aspect_ratio_info);
2172
2173 746 return 0;
2174 }
2175
2176 2 static int vcr2_init_sequence(AVCodecContext *avctx)
2177 {
2178 2 Mpeg1Context *s1 = avctx->priv_data;
2179 2 MpegEncContext *s = &s1->mpeg_enc_ctx;
2180 int i, v, ret;
2181
2182 /* start new MPEG-1 context decoding */
2183 2 s->out_format = FMT_MPEG1;
2184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s1->mpeg_enc_ctx_allocated) {
2185 ff_mpv_common_end(s);
2186 s1->mpeg_enc_ctx_allocated = 0;
2187 }
2188 2 s->width = avctx->coded_width;
2189 2 s->height = avctx->coded_height;
2190 2 avctx->has_b_frames = 0; // true?
2191 2 s->low_delay = 1;
2192
2193 2 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2194 2 setup_hwaccel_for_pixfmt(avctx);
2195
2196 2 ff_mpv_idct_init(s);
2197
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = ff_mpv_common_init(s)) < 0)
2198 return ret;
2199 2 s1->mpeg_enc_ctx_allocated = 1;
2200
2201
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 2 times.
130 for (i = 0; i < 64; i++) {
2202 128 int j = s->idsp.idct_permutation[i];
2203 128 v = ff_mpeg1_default_intra_matrix[i];
2204 128 s->intra_matrix[j] = v;
2205 128 s->chroma_intra_matrix[j] = v;
2206
2207 128 v = ff_mpeg1_default_non_intra_matrix[i];
2208 128 s->inter_matrix[j] = v;
2209 128 s->chroma_inter_matrix[j] = v;
2210 }
2211
2212 2 s->progressive_sequence = 1;
2213 2 s->progressive_frame = 1;
2214 2 s->picture_structure = PICT_FRAME;
2215 2 s->first_field = 0;
2216 2 s->frame_pred_frame_dct = 1;
2217 2 s->chroma_format = 1;
2218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->codec_tag == AV_RL32("BW10")) {
2219 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
2220 } else {
2221 2 s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
2222 2 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
2223 }
2224 2 s1->save_width = s->width;
2225 2 s1->save_height = s->height;
2226 2 s1->save_progressive_seq = s->progressive_sequence;
2227 2 return 0;
2228 }
2229
2230 659 static int mpeg_decode_a53_cc(AVCodecContext *avctx,
2231 const uint8_t *p, int buf_size)
2232 {
2233 659 Mpeg1Context *s1 = avctx->priv_data;
2234
2235
1/2
✓ Branch 0 taken 659 times.
✗ Branch 1 not taken.
659 if (buf_size >= 6 &&
2236
5/8
✓ Branch 0 taken 415 times.
✓ Branch 1 taken 244 times.
✓ Branch 2 taken 415 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 415 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 415 times.
✗ Branch 7 not taken.
659 p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
2237
2/4
✓ Branch 0 taken 415 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 415 times.
✗ Branch 3 not taken.
415 p[4] == 3 && (p[5] & 0x40)) {
2238 /* extract A53 Part 4 CC data */
2239 415 int cc_count = p[5] & 0x1f;
2240
2/4
✓ Branch 0 taken 415 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 415 times.
✗ Branch 3 not taken.
415 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2241
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 409 times.
415 int old_size = s1->a53_buf_ref ? s1->a53_buf_ref->size : 0;
2242 415 const uint64_t new_size = (old_size + cc_count
2243 415 * UINT64_C(3));
2244 int ret;
2245
2246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 415 times.
415 if (new_size > 3*A53_MAX_CC_COUNT)
2247 return AVERROR(EINVAL);
2248
2249 415 ret = av_buffer_realloc(&s1->a53_buf_ref, new_size);
2250
1/2
✓ Branch 0 taken 415 times.
✗ Branch 1 not taken.
415 if (ret >= 0)
2251 415 memcpy(s1->a53_buf_ref->data + old_size, p + 7, cc_count * UINT64_C(3));
2252
2253 415 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
2254 }
2255 415 return 1;
2256
1/2
✓ Branch 0 taken 244 times.
✗ Branch 1 not taken.
244 } else if (buf_size >= 2 &&
2257
3/4
✓ Branch 0 taken 205 times.
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 205 times.
✗ Branch 3 not taken.
244 p[0] == 0x03 && (p[1]&0x7f) == 0x01) {
2258 /* extract SCTE-20 CC data */
2259 GetBitContext gb;
2260 205 int cc_count = 0;
2261 int i, ret;
2262
2263 205 init_get_bits8(&gb, p + 2, buf_size - 2);
2264 205 cc_count = get_bits(&gb, 5);
2265
1/2
✓ Branch 0 taken 205 times.
✗ Branch 1 not taken.
205 if (cc_count > 0) {
2266
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;
2267 205 const uint64_t new_size = (old_size + cc_count
2268 205 * UINT64_C(3));
2269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 205 times.
205 if (new_size > 3*A53_MAX_CC_COUNT)
2270 return AVERROR(EINVAL);
2271
2272 205 ret = av_buffer_realloc(&s1->a53_buf_ref, new_size);
2273
1/2
✓ Branch 0 taken 205 times.
✗ Branch 1 not taken.
205 if (ret >= 0) {
2274 uint8_t field, cc1, cc2;
2275 205 uint8_t *cap = s1->a53_buf_ref->data;
2276
2277 205 memset(s1->a53_buf_ref->data + old_size, 0, cc_count * 3);
2278
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++) {
2279 410 skip_bits(&gb, 2); // priority
2280 410 field = get_bits(&gb, 2);
2281 410 skip_bits(&gb, 5); // line_offset
2282 410 cc1 = get_bits(&gb, 8);
2283 410 cc2 = get_bits(&gb, 8);
2284 410 skip_bits(&gb, 1); // marker
2285
2286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410 times.
410 if (!field) { // forbidden
2287 cap[0] = cap[1] = cap[2] = 0x00;
2288 } else {
2289 410 field = (field == 2 ? 1 : 0);
2290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410 times.
410 if (!s1->mpeg_enc_ctx.top_field_first) field = !field;
2291 410 cap[0] = 0x04 | field;
2292 410 cap[1] = ff_reverse[cc1];
2293 410 cap[2] = ff_reverse[cc2];
2294 }
2295 410 cap += 3;
2296 }
2297 }
2298 205 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
2299 }
2300 205 return 1;
2301
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 } else if (buf_size >= 11 &&
2302
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 39 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.
39 p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
2303 /* extract DVD CC data
2304 *
2305 * uint32_t user_data_start_code 0x000001B2 (big endian)
2306 * uint16_t user_identifier 0x4343 "CC"
2307 * uint8_t user_data_type_code 0x01
2308 * uint8_t caption_block_size 0xF8
2309 * uint8_t
2310 * bit 7 caption_odd_field_first 1=odd field (CC1/CC2) first 0=even field (CC3/CC4) first
2311 * bit 6 caption_filler 0
2312 * bit 5:1 caption_block_count number of caption blocks (pairs of caption words = frames). Most DVDs use 15 per start of GOP.
2313 * bit 0 caption_extra_field_added 1=one additional caption word
2314 *
2315 * struct caption_field_block {
2316 * uint8_t
2317 * bit 7:1 caption_filler 0x7F (all 1s)
2318 * bit 0 caption_field_odd 1=odd field (this is CC1/CC2) 0=even field (this is CC3/CC4)
2319 * uint8_t caption_first_byte
2320 * uint8_t caption_second_byte
2321 * } caption_block[(caption_block_count * 2) + caption_extra_field_added];
2322 *
2323 * Some DVDs encode caption data for both fields with caption_field_odd=1. The only way to decode the fields
2324 * correctly is to start on the field indicated by caption_odd_field_first and count between odd/even fields.
2325 * Don't assume that the first caption word is the odd field. There do exist MPEG files in the wild that start
2326 * on the even field. There also exist DVDs in the wild that encode an odd field count and the
2327 * caption_extra_field_added/caption_odd_field_first bits change per packet to allow that. */
2328 int cc_count = 0;
2329 int i, ret;
2330 // There is a caption count field in the data, but it is often
2331 // incorrect. So count the number of captions present.
2332 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2333 cc_count++;
2334 // Transform the DVD format into A53 Part 4 format
2335 if (cc_count > 0) {
2336 int old_size = s1->a53_buf_ref ? s1->a53_buf_ref->size : 0;
2337 const uint64_t new_size = (old_size + cc_count
2338 * UINT64_C(6));
2339 if (new_size > 3*A53_MAX_CC_COUNT)
2340 return AVERROR(EINVAL);
2341
2342 ret = av_buffer_realloc(&s1->a53_buf_ref, new_size);
2343 if (ret >= 0) {
2344 uint8_t field1 = !!(p[4] & 0x80);
2345 uint8_t *cap = s1->a53_buf_ref->data;
2346 p += 5;
2347 for (i = 0; i < cc_count; i++) {
2348 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2349 cap[1] = p[1];
2350 cap[2] = p[2];
2351 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2352 cap[4] = p[4];
2353 cap[5] = p[5];
2354 cap += 6;
2355 p += 6;
2356 }
2357 }
2358 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
2359 }
2360 return 1;
2361 }
2362 39 return 0;
2363 }
2364
2365 866 static void mpeg_decode_user_data(AVCodecContext *avctx,
2366 const uint8_t *p, int buf_size)
2367 {
2368 866 Mpeg1Context *s = avctx->priv_data;
2369 866 const uint8_t *buf_end = p + buf_size;
2370 866 Mpeg1Context *s1 = avctx->priv_data;
2371
2372 #if 0
2373 int i;
2374 for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
2375 av_log(avctx, AV_LOG_ERROR, "%c", p[i]);
2376 }
2377 av_log(avctx, AV_LOG_ERROR, "\n");
2378 #endif
2379
2380
1/2
✓ Branch 0 taken 866 times.
✗ Branch 1 not taken.
866 if (buf_size > 29){
2381 int i;
2382
2/2
✓ Branch 0 taken 17320 times.
✓ Branch 1 taken 866 times.
18186 for(i=0; i<20; i++)
2383
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17320 times.
17320 if (!memcmp(p+i, "\0TMPGEXS\0", 9)){
2384 s->tmpgexs= 1;
2385 }
2386 }
2387 /* we parse the DTG active format information */
2388
1/2
✓ Branch 0 taken 866 times.
✗ Branch 1 not taken.
866 if (buf_end - p >= 5 &&
2389
5/8
✓ Branch 0 taken 207 times.
✓ Branch 1 taken 659 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.
1073 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2390 207 int flags = p[4];
2391 207 p += 5;
2392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 207 times.
207 if (flags & 0x80) {
2393 /* skip event id */
2394 p += 2;
2395 }
2396
1/2
✓ Branch 0 taken 207 times.
✗ Branch 1 not taken.
207 if (flags & 0x40) {
2397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 207 times.
207 if (buf_end - p < 1)
2398 return;
2399 207 s1->has_afd = 1;
2400 207 s1->afd = p[0] & 0x0f;
2401 }
2402
1/2
✓ Branch 0 taken 659 times.
✗ Branch 1 not taken.
659 } else if (buf_end - p >= 6 &&
2403
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 659 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.
659 p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
2404 p[4] == 0x03) { // S3D_video_format_length
2405 // the 0x7F mask ignores the reserved_bit value
2406 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2407
2408 if (S3D_video_format_type == 0x03 ||
2409 S3D_video_format_type == 0x04 ||
2410 S3D_video_format_type == 0x08 ||
2411 S3D_video_format_type == 0x23) {
2412
2413 s1->has_stereo3d = 1;
2414
2415 switch (S3D_video_format_type) {
2416 case 0x03:
2417 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
2418 break;
2419 case 0x04:
2420 s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
2421 break;
2422 case 0x08:
2423 s1->stereo3d.type = AV_STEREO3D_2D;
2424 break;
2425 case 0x23:
2426 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2427 break;
2428 }
2429 }
2430
2/2
✓ Branch 1 taken 620 times.
✓ Branch 2 taken 39 times.
659 } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
2431 620 return;
2432 }
2433 }
2434
2435 517 static void mpeg_decode_gop(AVCodecContext *avctx,
2436 const uint8_t *buf, int buf_size)
2437 {
2438 517 Mpeg1Context *s1 = avctx->priv_data;
2439 517 MpegEncContext *s = &s1->mpeg_enc_ctx;
2440 int broken_link;
2441 int64_t tc;
2442
2443 517 init_get_bits(&s->gb, buf, buf_size * 8);
2444
2445 517 tc = s-> timecode_frame_start = get_bits(&s->gb, 25);
2446
2447 517 s1->closed_gop = get_bits1(&s->gb);
2448 /* broken_link indicates that after editing the
2449 * reference frames of the first B-Frames after GOP I-Frame
2450 * are missing (open gop) */
2451 517 broken_link = get_bits1(&s->gb);
2452
2453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 517 times.
517 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2454 char tcbuf[AV_TIMECODE_STR_SIZE];
2455 av_timecode_make_mpeg_tc_string(tcbuf, tc);
2456 av_log(s->avctx, AV_LOG_DEBUG,
2457 "GOP (%s) closed_gop=%d broken_link=%d\n",
2458 tcbuf, s1->closed_gop, broken_link);
2459 }
2460 517 }
2461
2462 4172 static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
2463 int *got_output, const uint8_t *buf, int buf_size)
2464 {
2465 4172 Mpeg1Context *s = avctx->priv_data;
2466 4172 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2467 4172 const uint8_t *buf_ptr = buf;
2468 4172 const uint8_t *buf_end = buf + buf_size;
2469 int ret, input_size;
2470 4172 int last_code = 0, skip_frame = 0;
2471 4172 int picture_start_code_seen = 0;
2472
2473 198372 for (;;) {
2474 /* find next start code */
2475 202544 uint32_t start_code = -1;
2476 202544 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
2477
2/2
✓ Branch 0 taken 4054 times.
✓ Branch 1 taken 198490 times.
202544 if (start_code > 0x1ff) {
2478
2/2
✓ Branch 0 taken 3795 times.
✓ Branch 1 taken 259 times.
4054 if (!skip_frame) {
2479 3795 if (HAVE_THREADS &&
2480
2/2
✓ Branch 0 taken 611 times.
✓ Branch 1 taken 3184 times.
3795 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2481
1/2
✓ Branch 0 taken 611 times.
✗ Branch 1 not taken.
611 !avctx->hwaccel) {
2482 int i;
2483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 611 times.
611 av_assert0(avctx->thread_count > 1);
2484
2485 611 avctx->execute(avctx, slice_decode_thread,
2486 611 &s2->thread_context[0], NULL,
2487 s->slice_count, sizeof(void *));
2488
2/2
✓ Branch 0 taken 5463 times.
✓ Branch 1 taken 611 times.
6074 for (i = 0; i < s->slice_count; i++)
2489 5463 s2->er.error_count += s2->thread_context[i]->er.error_count;
2490 }
2491
2492 3795 ret = slice_end(avctx, picture);
2493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3795 times.
3795 if (ret < 0)
2494 4172 return ret;
2495
2/2
✓ Branch 0 taken 3538 times.
✓ Branch 1 taken 257 times.
3795 else if (ret) {
2496 // FIXME: merge with the stuff in mpeg_decode_slice
2497
5/6
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 3470 times.
✓ Branch 2 taken 59 times.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 59 times.
3538 if (s2->last_picture_ptr || s2->low_delay || s2->pict_type == AV_PICTURE_TYPE_B)
2498 3479 *got_output = 1;
2499 }
2500 }
2501 4054 s2->pict_type = 0;
2502
2503
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4054 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4054 if (avctx->err_recognition & AV_EF_EXPLODE && s2->er.error_count)
2504 return AVERROR_INVALIDDATA;
2505
2506 #if FF_API_FLAG_TRUNCATED
2507 4054 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2508 #else
2509 return FFMAX(0, buf_ptr - buf);
2510 #endif
2511 }
2512
2513 198490 input_size = buf_end - buf_ptr;
2514
2515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 198490 times.
198490 if (avctx->debug & FF_DEBUG_STARTCODE)
2516 av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %"PTRDIFF_SPECIFIER" left %d\n",
2517 start_code, buf_ptr - buf, input_size);
2518
2519 /* prepare data for next start code */
2520
6/6
✓ Branch 0 taken 746 times.
✓ Branch 1 taken 4532 times.
✓ Branch 2 taken 5196 times.
✓ Branch 3 taken 866 times.
✓ Branch 4 taken 517 times.
✓ Branch 5 taken 186633 times.
198490 switch (start_code) {
2521 746 case SEQ_START_CODE:
2522
1/2
✓ Branch 0 taken 746 times.
✗ Branch 1 not taken.
746 if (last_code == 0) {
2523 746 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2524
2/2
✓ Branch 0 taken 508 times.
✓ Branch 1 taken 238 times.
746 if (buf != avctx->extradata)
2525 508 s->sync = 1;
2526 } else {
2527 av_log(avctx, AV_LOG_ERROR,
2528 "ignoring SEQ_START_CODE after %X\n", last_code);
2529 if (avctx->err_recognition & AV_EF_EXPLODE)
2530 return AVERROR_INVALIDDATA;
2531 }
2532 746 break;
2533
2534 4532 case PICTURE_START_CODE:
2535
3/4
✓ Branch 0 taken 616 times.
✓ Branch 1 taken 3916 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 616 times.
4532 if (picture_start_code_seen && s2->picture_structure == PICT_FRAME) {
2536 /* If it's a frame picture, there can't be more than one picture header.
2537 Yet, it does happen and we need to handle it. */
2538 av_log(avctx, AV_LOG_WARNING, "ignoring extra picture following a frame-picture\n");
2539 break;
2540 }
2541 4532 picture_start_code_seen = 1;
2542
2543
3/4
✓ Branch 0 taken 4414 times.
✓ Branch 1 taken 118 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4414 times.
4532 if (s2->width <= 0 || s2->height <= 0) {
2544 118 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
2545 s2->width, s2->height);
2546 118 return AVERROR_INVALIDDATA;
2547 }
2548
2549
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4414 times.
4414 if (s->tmpgexs){
2550 s2->intra_dc_precision= 3;
2551 s2->intra_matrix[0]= 1;
2552 }
2553
2/2
✓ Branch 0 taken 615 times.
✓ Branch 1 taken 3799 times.
4414 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
2554
2/4
✓ Branch 0 taken 615 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 615 times.
615 !avctx->hwaccel && s->slice_count) {
2555 int i;
2556
2557 avctx->execute(avctx, slice_decode_thread,
2558 s2->thread_context, NULL,
2559 s->slice_count, sizeof(void *));
2560 for (i = 0; i < s->slice_count; i++)
2561 s2->er.error_count += s2->thread_context[i]->er.error_count;
2562 s->slice_count = 0;
2563 }
2564
3/4
✓ Branch 0 taken 616 times.
✓ Branch 1 taken 3798 times.
✓ Branch 2 taken 616 times.
✗ Branch 3 not taken.
4414 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2565 4414 ret = mpeg_decode_postinit(avctx);
2566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4414 times.
4414 if (ret < 0) {
2567 av_log(avctx, AV_LOG_ERROR,
2568 "mpeg_decode_postinit() failure\n");
2569 return ret;
2570 }
2571
2572 /* We have a complete image: we try to decompress it. */
2573
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4414 times.
4414 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2574 s2->pict_type = 0;
2575 4414 s->first_slice = 1;
2576 4414 last_code = PICTURE_START_CODE;
2577 } else {
2578 av_log(avctx, AV_LOG_ERROR,
2579 "ignoring pic after %X\n", last_code);
2580 if (avctx->err_recognition & AV_EF_EXPLODE)
2581 return AVERROR_INVALIDDATA;
2582 }
2583 4414 break;
2584 5196 case EXT_START_CODE:
2585 5196 init_get_bits(&s2->gb, buf_ptr, input_size * 8);
2586
2587 5196 switch (get_bits(&s2->gb, 4)) {
2588 654 case 0x1:
2589
1/2
✓ Branch 0 taken 654 times.
✗ Branch 1 not taken.
654 if (last_code == 0) {
2590 654 mpeg_decode_sequence_extension(s);
2591 } else {
2592 av_log(avctx, AV_LOG_ERROR,
2593 "ignoring seq ext after %X\n", last_code);
2594 if (avctx->err_recognition & AV_EF_EXPLODE)
2595 return AVERROR_INVALIDDATA;
2596 }
2597 654 break;
2598 76 case 0x2:
2599 76 mpeg_decode_sequence_display_extension(s);
2600 76 break;
2601 531 case 0x3:
2602 531 mpeg_decode_quant_matrix_extension(s2);
2603 531 break;
2604 15 case 0x7:
2605 15 mpeg_decode_picture_display_extension(s);
2606 15 break;
2607 3920 case 0x8:
2608
1/2
✓ Branch 0 taken 3920 times.
✗ Branch 1 not taken.
3920 if (last_code == PICTURE_START_CODE) {
2609 3920 int ret = mpeg_decode_picture_coding_extension(s);
2610
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3920 times.
3920 if (ret < 0)
2611 return ret;
2612 } else {
2613 av_log(avctx, AV_LOG_ERROR,
2614 "ignoring pic cod ext after %X\n", last_code);
2615 if (avctx->err_recognition & AV_EF_EXPLODE)
2616 return AVERROR_INVALIDDATA;
2617 }
2618 3920 break;
2619 }
2620 5196 break;
2621 866 case USER_START_CODE:
2622 866 mpeg_decode_user_data(avctx, buf_ptr, input_size);
2623 866 break;
2624 517 case GOP_START_CODE:
2625
1/2
✓ Branch 0 taken 517 times.
✗ Branch 1 not taken.
517 if (last_code == 0) {
2626 517 s2->first_field = 0;
2627 517 mpeg_decode_gop(avctx, buf_ptr, input_size);
2628 517 s->sync = 1;
2629 } else {
2630 av_log(avctx, AV_LOG_ERROR,
2631 "ignoring GOP_START_CODE after %X\n", last_code);
2632 if (avctx->err_recognition & AV_EF_EXPLODE)
2633 return AVERROR_INVALIDDATA;
2634 }
2635 517 break;
2636 186633 default:
2637
2/2
✓ Branch 0 taken 185992 times.
✓ Branch 1 taken 641 times.
186633 if (start_code >= SLICE_MIN_START_CODE &&
2638
4/4
✓ Branch 0 taken 185988 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4414 times.
✓ Branch 3 taken 181574 times.
185992 start_code <= SLICE_MAX_START_CODE && last_code == PICTURE_START_CODE) {
2639
3/4
✓ Branch 0 taken 1650 times.
✓ Branch 1 taken 2764 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1650 times.
4414 if (s2->progressive_sequence && !s2->progressive_frame) {
2640 s2->progressive_frame = 1;
2641 av_log(s2->avctx, AV_LOG_ERROR,
2642 "interlaced frame in progressive sequence, ignoring\n");
2643 }
2644
2645
1/2
✓ Branch 0 taken 4414 times.
✗ Branch 1 not taken.
4414 if (s2->picture_structure == 0 ||
2646
3/4
✓ Branch 0 taken 1654 times.
✓ Branch 1 taken 2760 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1654 times.
4414 (s2->progressive_frame && s2->picture_structure != PICT_FRAME)) {
2647 av_log(s2->avctx, AV_LOG_ERROR,
2648 "picture_structure %d invalid, ignoring\n",
2649 s2->picture_structure);
2650 s2->picture_structure = PICT_FRAME;
2651 }
2652
2653
3/4
✓ Branch 0 taken 1650 times.
✓ Branch 1 taken 2764 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1650 times.
4414 if (s2->progressive_sequence && !s2->frame_pred_frame_dct)
2654 av_log(s2->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
2655
2656
2/2
✓ Branch 0 taken 3180 times.
✓ Branch 1 taken 1234 times.
4414 if (s2->picture_structure == PICT_FRAME) {
2657 3180 s2->first_field = 0;
2658 3180 s2->v_edge_pos = 16 * s2->mb_height;
2659 } else {
2660 1234 s2->first_field ^= 1;
2661 1234 s2->v_edge_pos = 8 * s2->mb_height;
2662 1234 memset(s2->mbskip_table, 0, s2->mb_stride * s2->mb_height);
2663 }
2664 }
2665
2/2
✓ Branch 0 taken 185992 times.
✓ Branch 1 taken 641 times.
186633 if (start_code >= SLICE_MIN_START_CODE &&
2666
4/4
✓ Branch 0 taken 185988 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 185868 times.
✓ Branch 3 taken 120 times.
185992 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2667 185868 const int field_pic = s2->picture_structure != PICT_FRAME;
2668 185868 int mb_y = start_code - SLICE_MIN_START_CODE;
2669 185868 last_code = SLICE_MIN_START_CODE;
2670
3/4
✓ Branch 0 taken 178280 times.
✓ Branch 1 taken 7588 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 178280 times.
185868 if (s2->codec_id != AV_CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
2671 mb_y += (*buf_ptr&0xE0)<<2;
2672
2673 185868 mb_y <<= field_pic;
2674
2/2
✓ Branch 0 taken 8919 times.
✓ Branch 1 taken 176949 times.
185868 if (s2->picture_structure == PICT_BOTTOM_FIELD)
2675 8919 mb_y++;
2676
2677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185868 times.
185868 if (buf_end - buf_ptr < 2) {
2678 av_log(s2->avctx, AV_LOG_ERROR, "slice too small\n");
2679 return AVERROR_INVALIDDATA;
2680 }
2681
2682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185868 times.
185868 if (mb_y >= s2->mb_height) {
2683 av_log(s2->avctx, AV_LOG_ERROR,
2684 "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2685 return AVERROR_INVALIDDATA;
2686 }
2687
2688
2/2
✓ Branch 0 taken 76578 times.
✓ Branch 1 taken 109290 times.
185868 if (!s2->last_picture_ptr) {
2689 /* Skip B-frames if we do not have reference frames and
2690 * GOP is not closed. */
2691
2/2
✓ Branch 0 taken 672 times.
✓ Branch 1 taken 75906 times.
76578 if (s2->pict_type == AV_PICTURE_TYPE_B) {
2692
1/2
✓ Branch 0 taken 672 times.
✗ Branch 1 not taken.
672 if (!s->closed_gop) {
2693 672 skip_frame = 1;
2694 672 av_log(s2->avctx, AV_LOG_DEBUG,
2695 "Skipping B slice due to open GOP\n");
2696 672 break;
2697 }
2698 }
2699 }
2700
3/4
✓ Branch 0 taken 65676 times.
✓ Branch 1 taken 119520 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 65676 times.
185196 if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL))
2701 119520 s->sync = 1;
2702
2/2
✓ Branch 0 taken 72399 times.
✓ Branch 1 taken 112797 times.
185196 if (!s2->next_picture_ptr) {
2703 /* Skip P-frames if we do not have a reference frame or
2704 * we have an invalid header. */
2705
4/4
✓ Branch 0 taken 3011 times.
✓ Branch 1 taken 69388 times.
✓ Branch 2 taken 2412 times.
✓ Branch 3 taken 599 times.
72399 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
2706 2412 skip_frame = 1;
2707 2412 av_log(s2->avctx, AV_LOG_DEBUG,
2708 "Skipping P slice due to !sync\n");
2709 2412 break;
2710 }
2711 }
2712
2/2
✓ Branch 0 taken 69919 times.
✓ Branch 1 taken 112865 times.
182784 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
2713
1/2
✓ Branch 0 taken 69919 times.
✗ Branch 1 not taken.
69919 s2->pict_type == AV_PICTURE_TYPE_B) ||
2714
2/2
✓ Branch 0 taken 69919 times.
✓ Branch 1 taken 112865 times.
182784 (avctx->skip_frame >= AVDISCARD_NONKEY &&
2715
2/2
✓ Branch 0 taken 69320 times.
✓ Branch 1 taken 599 times.
69919 s2->pict_type != AV_PICTURE_TYPE_I) ||
2716
2/2
✓ Branch 0 taken 69320 times.
✓ Branch 1 taken 112865 times.
182185 avctx->skip_frame >= AVDISCARD_ALL) {
2717 69919 skip_frame = 1;
2718 69919 break;
2719 }
2720
2721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112865 times.
112865 if (!s->mpeg_enc_ctx_allocated)
2722 break;
2723
2724
2/2
✓ Branch 0 taken 105592 times.
✓ Branch 1 taken 7273 times.
112865 if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2725
1/2
✓ Branch 0 taken 105592 times.
✗ Branch 1 not taken.
105592 if (mb_y < avctx->skip_top ||
2726
1/2
✓ Branch 0 taken 105592 times.
✗ Branch 1 not taken.
105592 mb_y >= s2->mb_height - avctx->skip_bottom)
2727 break;
2728 }
2729
2730
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112865 times.
112865 if (!s2->pict_type) {
2731 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2732 if (avctx->err_recognition & AV_EF_EXPLODE)
2733 return AVERROR_INVALIDDATA;
2734 break;
2735 }
2736
2737
2/2
✓ Branch 0 taken 4080 times.
✓ Branch 1 taken 108785 times.
112865 if (s->first_slice) {
2738 4080 skip_frame = 0;
2739 4080 s->first_slice = 0;
2740
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4080 times.
4080 if ((ret = mpeg_field_start(s2, buf, buf_size)) < 0)
2741 return ret;
2742 }
2743
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112865 times.
112865 if (!s2->current_picture_ptr) {
2744 av_log(avctx, AV_LOG_ERROR,
2745 "current_picture not initialized\n");
2746 return AVERROR_INVALIDDATA;
2747 }
2748
2749 112865 if (HAVE_THREADS &&
2750
2/2
✓ Branch 0 taken 18210 times.
✓ Branch 1 taken 94655 times.
112865 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2751
1/2
✓ Branch 0 taken 18210 times.
✗ Branch 1 not taken.
18210 !avctx->hwaccel) {
2752 18210 int threshold = (s2->mb_height * s->slice_count +
2753 18210 s2->slice_context_count / 2) /
2754 18210 s2->slice_context_count;
2755
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18210 times.
18210 av_assert0(avctx->thread_count > 1);
2756
2/2
✓ Branch 0 taken 5463 times.
✓ Branch 1 taken 12747 times.
18210 if (threshold <= mb_y) {
2757 5463 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2758
2759 5463 thread_context->start_mb_y = mb_y;
2760 5463 thread_context->end_mb_y = s2->mb_height;
2761
2/2
✓ Branch 0 taken 4856 times.
✓ Branch 1 taken 607 times.
5463 if (s->slice_count) {
2762 4856 s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
2763 4856 ret = ff_update_duplicate_context(thread_context, s2);
2764
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4856 times.
4856 if (ret < 0)
2765 return ret;
2766 }
2767 5463 init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
2768 5463 s->slice_count++;
2769 }
2770 18210 buf_ptr += 2; // FIXME add minimum number of bytes per slice
2771 } else {
2772 94655 ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2773 94655 emms_c();
2774
2775
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 94651 times.
94655 if (ret < 0) {
2776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (avctx->err_recognition & AV_EF_EXPLODE)
2777 return ret;
2778
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2779 4 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2780 s2->resync_mb_y, s2->mb_x, s2->mb_y,
2781 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
2782 } else {
2783 94651 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2784 94651 s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
2785 ER_AC_END | ER_DC_END | ER_MV_END);
2786 }
2787 }
2788 }
2789 113630 break;
2790 }
2791 }
2792 }
2793
2794 4065 static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
2795 int *got_output, AVPacket *avpkt)
2796 {
2797 4065 const uint8_t *buf = avpkt->data;
2798 int ret;
2799 4065 int buf_size = avpkt->size;
2800 4065 Mpeg1Context *s = avctx->priv_data;
2801 4065 AVFrame *picture = data;
2802 4065 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2803
2804
3/6
✓ Branch 0 taken 3934 times.
✓ Branch 1 taken 131 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3934 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4065 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2805 /* special case for last picture */
2806
4/4
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 59 times.
✓ Branch 3 taken 63 times.
131 if (s2->low_delay == 0 && s2->next_picture_ptr) {
2807 59 int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
2808
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if (ret < 0)
2809 return ret;
2810
2811 59 s2->next_picture_ptr = NULL;
2812
2813 59 *got_output = 1;
2814 }
2815 131 return buf_size;
2816 }
2817
2818 #if FF_API_FLAG_TRUNCATED
2819
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3934 times.
3934 if (s2->avctx->flags & AV_CODEC_FLAG_TRUNCATED) {
2820 int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
2821 buf_size, NULL);
2822
2823 if (ff_combine_frame(&s2->parse_context, next,
2824 (const uint8_t **) &buf, &buf_size) < 0)
2825 return buf_size;
2826 }
2827 #endif
2828
2829 3934 s2->codec_tag = ff_toupper4(avctx->codec_tag);
2830
4/4
✓ Branch 0 taken 376 times.
✓ Branch 1 taken 3558 times.
✓ Branch 2 taken 374 times.
✓ Branch 3 taken 2 times.
3934 if (s->mpeg_enc_ctx_allocated == 0 && ( s2->codec_tag == AV_RL32("VCR2")
2831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 374 times.
374 || s2->codec_tag == AV_RL32("BW10")
2832 ))
2833 2 vcr2_init_sequence(avctx);
2834
2835 3934 s->slice_count = 0;
2836
2837
4/4
✓ Branch 0 taken 3648 times.
✓ Branch 1 taken 286 times.
✓ Branch 2 taken 238 times.
✓ Branch 3 taken 3410 times.
3934 if (avctx->extradata && !s->extradata_decoded) {
2838 238 ret = decode_chunks(avctx, picture, got_output,
2839 238 avctx->extradata, avctx->extradata_size);
2840
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 238 times.
238 if (*got_output) {
2841 av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
2842 av_frame_unref(picture);
2843 *got_output = 0;
2844 }
2845 238 s->extradata_decoded = 1;
2846
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 238 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
238 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) {
2847 s2->current_picture_ptr = NULL;
2848 return ret;
2849 }
2850 }
2851
2852 3934 ret = decode_chunks(avctx, picture, got_output, buf, buf_size);
2853
4/4
✓ Branch 0 taken 3816 times.
✓ Branch 1 taken 118 times.
✓ Branch 2 taken 3479 times.
✓ Branch 3 taken 337 times.
3934 if (ret<0 || *got_output) {
2854 3597 s2->current_picture_ptr = NULL;
2855
2856
4/4
✓ Branch 0 taken 462 times.
✓ Branch 1 taken 3135 times.
✓ Branch 2 taken 344 times.
✓ Branch 3 taken 118 times.
3597 if (s2->timecode_frame_start != -1 && *got_output) {
2857 char tcbuf[AV_TIMECODE_STR_SIZE];
2858 344 AVFrameSideData *tcside = av_frame_new_side_data(picture,
2859 AV_FRAME_DATA_GOP_TIMECODE,
2860 sizeof(int64_t));
2861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 344 times.
344 if (!tcside)
2862 return AVERROR(ENOMEM);
2863 344 memcpy(tcside->data, &s2->timecode_frame_start, sizeof(int64_t));
2864
2865 344 av_timecode_make_mpeg_tc_string(tcbuf, s2->timecode_frame_start);
2866 344 av_dict_set(&picture->metadata, "timecode", tcbuf, 0);
2867
2868 344 s2->timecode_frame_start = -1;
2869 }
2870 }
2871
2872 3934 return ret;
2873 }
2874
2875 static void flush(AVCodecContext *avctx)
2876 {
2877 Mpeg1Context *s = avctx->priv_data;
2878
2879 s->sync = 0;
2880 s->closed_gop = 0;
2881
2882 ff_mpeg_flush(avctx);
2883 }
2884
2885 263 static av_cold int mpeg_decode_end(AVCodecContext *avctx)
2886 {
2887 263 Mpeg1Context *s = avctx->priv_data;
2888
2889
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 23 times.
263 if (s->mpeg_enc_ctx_allocated)
2890 240 ff_mpv_common_end(&s->mpeg_enc_ctx);
2891 263 av_buffer_unref(&s->a53_buf_ref);
2892 263 return 0;
2893 }
2894
2895 const AVCodec ff_mpeg1video_decoder = {
2896 .name = "mpeg1video",
2897 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2898 .type = AVMEDIA_TYPE_VIDEO,
2899 .id = AV_CODEC_ID_MPEG1VIDEO,
2900 .priv_data_size = sizeof(Mpeg1Context),
2901 .init = mpeg_decode_init,
2902 .close = mpeg_decode_end,
2903 .decode = mpeg_decode_frame,
2904 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2905 #if FF_API_FLAG_TRUNCATED
2906 AV_CODEC_CAP_TRUNCATED |
2907 #endif
2908 AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
2909 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
2910 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2911 .flush = flush,
2912 .max_lowres = 3,
2913 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context),
2914 .hw_configs = (const AVCodecHWConfigInternal *const []) {
2915 #if CONFIG_MPEG1_NVDEC_HWACCEL
2916 HWACCEL_NVDEC(mpeg1),
2917 #endif
2918 #if CONFIG_MPEG1_VDPAU_HWACCEL
2919 HWACCEL_VDPAU(mpeg1),
2920 #endif
2921 #if CONFIG_MPEG1_VIDEOTOOLBOX_HWACCEL
2922 HWACCEL_VIDEOTOOLBOX(mpeg1),
2923 #endif
2924 #if CONFIG_MPEG1_XVMC_HWACCEL
2925 HWACCEL_XVMC(mpeg1),
2926 #endif
2927 NULL
2928 },
2929 };
2930
2931 const AVCodec ff_mpeg2video_decoder = {
2932 .name = "mpeg2video",
2933 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2934 .type = AVMEDIA_TYPE_VIDEO,
2935 .id = AV_CODEC_ID_MPEG2VIDEO,
2936 .priv_data_size = sizeof(Mpeg1Context),
2937 .init = mpeg_decode_init,
2938 .close = mpeg_decode_end,
2939 .decode = mpeg_decode_frame,
2940 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2941 #if FF_API_FLAG_TRUNCATED
2942 AV_CODEC_CAP_TRUNCATED |
2943 #endif
2944 AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
2945 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
2946 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2947 .flush = flush,
2948 .max_lowres = 3,
2949 .profiles = NULL_IF_CONFIG_SMALL(ff_mpeg2_video_profiles),
2950 .hw_configs = (const AVCodecHWConfigInternal *const []) {
2951 #if CONFIG_MPEG2_DXVA2_HWACCEL
2952 HWACCEL_DXVA2(mpeg2),
2953 #endif
2954 #if CONFIG_MPEG2_D3D11VA_HWACCEL
2955 HWACCEL_D3D11VA(mpeg2),
2956 #endif
2957 #if CONFIG_MPEG2_D3D11VA2_HWACCEL
2958 HWACCEL_D3D11VA2(mpeg2),
2959 #endif
2960 #if CONFIG_MPEG2_NVDEC_HWACCEL
2961 HWACCEL_NVDEC(mpeg2),
2962 #endif
2963 #if CONFIG_MPEG2_VAAPI_HWACCEL
2964 HWACCEL_VAAPI(mpeg2),
2965 #endif
2966 #if CONFIG_MPEG2_VDPAU_HWACCEL
2967 HWACCEL_VDPAU(mpeg2),
2968 #endif
2969 #if CONFIG_MPEG2_VIDEOTOOLBOX_HWACCEL
2970 HWACCEL_VIDEOTOOLBOX(mpeg2),
2971 #endif
2972 #if CONFIG_MPEG2_XVMC_HWACCEL
2973 HWACCEL_XVMC(mpeg2),
2974 #endif
2975 NULL
2976 },
2977 };
2978
2979 //legacy decoder
2980 const AVCodec ff_mpegvideo_decoder = {
2981 .name = "mpegvideo",
2982 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2983 .type = AVMEDIA_TYPE_VIDEO,
2984 .id = AV_CODEC_ID_MPEG2VIDEO,
2985 .priv_data_size = sizeof(Mpeg1Context),
2986 .init = mpeg_decode_init,
2987 .close = mpeg_decode_end,
2988 .decode = mpeg_decode_frame,
2989 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2990 #if FF_API_FLAG_TRUNCATED
2991 AV_CODEC_CAP_TRUNCATED |
2992 #endif
2993 AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
2994 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
2995 FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
2996 .flush = flush,
2997 .max_lowres = 3,
2998 };
2999
3000 typedef struct IPUContext {
3001 MpegEncContext m;
3002
3003 int flags;
3004 DECLARE_ALIGNED(32, int16_t, block)[6][64];
3005 } IPUContext;
3006
3007 static int ipu_decode_frame(AVCodecContext *avctx, void *data,
3008 int *got_frame, AVPacket *avpkt)
3009 {
3010 IPUContext *s = avctx->priv_data;
3011 MpegEncContext *m = &s->m;
3012 GetBitContext *gb = &m->gb;
3013 AVFrame * const frame = data;
3014 int ret;
3015
3016 ret = ff_get_buffer(avctx, frame, 0);
3017 if (ret < 0)
3018 return ret;
3019
3020 ret = init_get_bits8(gb, avpkt->data, avpkt->size);
3021 if (ret < 0)
3022 return ret;
3023
3024 s->flags = get_bits(gb, 8);
3025 m->intra_dc_precision = s->flags & 3;
3026 m->q_scale_type = !!(s->flags & 0x40);
3027 m->intra_vlc_format = !!(s->flags & 0x20);
3028 m->alternate_scan = !!(s->flags & 0x10);
3029
3030 if (s->flags & 0x10) {
3031 ff_init_scantable(m->idsp.idct_permutation, &m->inter_scantable, ff_alternate_vertical_scan);
3032 ff_init_scantable(m->idsp.idct_permutation, &m->intra_scantable, ff_alternate_vertical_scan);
3033 } else {
3034 ff_init_scantable(m->idsp.idct_permutation, &m->inter_scantable, ff_zigzag_direct);
3035 ff_init_scantable(m->idsp.idct_permutation, &m->intra_scantable, ff_zigzag_direct);
3036 }
3037
3038 m->last_dc[0] = m->last_dc[1] = m->last_dc[2] = 1 << (7 + (s->flags & 3));
3039 m->qscale = 1;
3040
3041 for (int y = 0; y < avctx->height; y += 16) {
3042 int intraquant;
3043
3044 for (int x = 0; x < avctx->width; x += 16) {
3045 if (x || y) {
3046 if (!get_bits1(gb))
3047 return AVERROR_INVALIDDATA;
3048 }
3049 if (get_bits1(gb)) {
3050 intraquant = 0;
3051 } else {
3052 if (!get_bits1(gb))
3053 return AVERROR_INVALIDDATA;
3054 intraquant = 1;
3055 }
3056
3057 if (s->flags & 4)
3058 skip_bits1(gb);
3059
3060 if (intraquant)
3061 m->qscale = mpeg_get_qscale(m);
3062
3063 memset(s->block, 0, sizeof(s->block));
3064
3065 for (int n = 0; n < 6; n++) {
3066 if (s->flags & 0x80) {
3067 ret = ff_mpeg1_decode_block_intra(&m->gb,
3068 m->intra_matrix,
3069 m->intra_scantable.permutated,
3070 m->last_dc, s->block[n],
3071 n, m->qscale);
3072 if (ret >= 0)
3073 m->block_last_index[n] = ret;
3074 } else {
3075 ret = mpeg2_decode_block_intra(m, s->block[n], n);
3076 }
3077
3078 if (ret < 0)
3079 return ret;
3080 }
3081
3082 m->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x,
3083 frame->linesize[0], s->block[0]);
3084 m->idsp.idct_put(frame->data[0] + y * frame->linesize[0] + x + 8,
3085 frame->linesize[0], s->block[1]);
3086 m->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x,
3087 frame->linesize[0], s->block[2]);
3088 m->idsp.idct_put(frame->data[0] + (y + 8) * frame->linesize[0] + x + 8,
3089 frame->linesize[0], s->block[3]);
3090 m->idsp.idct_put(frame->data[1] + (y >> 1) * frame->linesize[1] + (x >> 1),
3091 frame->linesize[1], s->block[4]);
3092 m->idsp.idct_put(frame->data[2] + (y >> 1) * frame->linesize[2] + (x >> 1),
3093 frame->linesize[2], s->block[5]);
3094 }
3095 }
3096
3097 align_get_bits(gb);
3098 if (get_bits_left(gb) != 32)
3099 return AVERROR_INVALIDDATA;
3100
3101 frame->pict_type = AV_PICTURE_TYPE_I;
3102 frame->key_frame = 1;
3103 *got_frame = 1;
3104
3105 return avpkt->size;
3106 }
3107
3108 static av_cold int ipu_decode_init(AVCodecContext *avctx)
3109 {
3110 IPUContext *s = avctx->priv_data;
3111 MpegEncContext *m = &s->m;
3112
3113 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
3114
3115 ff_mpv_decode_init(m, avctx);
3116 ff_mpv_idct_init(m);
3117 ff_mpeg12_common_init(m);
3118 ff_mpeg12_init_vlcs();
3119
3120 for (int i = 0; i < 64; i++) {
3121 int j = m->idsp.idct_permutation[i];
3122 int v = ff_mpeg1_default_intra_matrix[i];
3123 m->intra_matrix[j] = v;
3124 m->chroma_intra_matrix[j] = v;
3125 }
3126
3127 for (int i = 0; i < 64; i++) {
3128 int j = m->idsp.idct_permutation[i];
3129 int v = ff_mpeg1_default_non_intra_matrix[i];
3130 m->inter_matrix[j] = v;
3131 m->chroma_inter_matrix[j] = v;
3132 }
3133
3134 return 0;
3135 }
3136
3137 static av_cold int ipu_decode_end(AVCodecContext *avctx)
3138 {
3139 IPUContext *s = avctx->priv_data;
3140
3141 ff_mpv_common_end(&s->m);
3142
3143 return 0;
3144 }
3145
3146 const AVCodec ff_ipu_decoder = {
3147 .name = "ipu",
3148 .long_name = NULL_IF_CONFIG_SMALL("IPU Video"),
3149 .type = AVMEDIA_TYPE_VIDEO,
3150 .id = AV_CODEC_ID_IPU,
3151 .priv_data_size = sizeof(IPUContext),
3152 .init = ipu_decode_init,
3153 .decode = ipu_decode_frame,
3154 .close = ipu_decode_end,
3155 .capabilities = AV_CODEC_CAP_DR1,
3156 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
3157 };
3158