3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2013 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
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.
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.
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
28 #define UNCHECKED_BITSTREAM_READER 1
31 #include "libavutil/attributes.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/stereo3d.h"
36 #include "bytestream.h"
37 #include "error_resilience.h"
42 #include "mpeg12data.h"
43 #include "mpegutils.h"
44 #include "mpegvideo.h"
47 #include "vdpau_internal.h"
48 #include "xvmc_internal.h"
50 typedef struct Mpeg1Context {
51 MpegEncContext mpeg_enc_ctx;
52 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
53 int repeat_field; /* true if we must repeat the field */
54 AVPanScan pan_scan; /* some temporary storage for the panscan */
62 AVRational save_aspect;
63 int save_width, save_height, save_progressive_seq;
64 AVRational frame_rate_ext; /* MPEG-2 specific framerate modificator */
65 int sync; /* Did we reach a sync point like a GOP/SEQ/KEYFrame? */
68 int extradata_decoded;
71 #define MB_TYPE_ZERO_MV 0x20000000
73 static const uint32_t ptype2mb_type[7] = {
75 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
77 MB_TYPE_L0 | MB_TYPE_CBP,
78 MB_TYPE_QUANT | MB_TYPE_INTRA,
79 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
80 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
83 static const uint32_t btype2mb_type[11] = {
86 MB_TYPE_L1 | MB_TYPE_CBP,
88 MB_TYPE_L0 | MB_TYPE_CBP,
90 MB_TYPE_L0L1 | MB_TYPE_CBP,
91 MB_TYPE_QUANT | MB_TYPE_INTRA,
92 MB_TYPE_QUANT | MB_TYPE_L1 | MB_TYPE_CBP,
93 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
94 MB_TYPE_QUANT | MB_TYPE_L0L1 | MB_TYPE_CBP,
97 static const uint8_t non_linear_qscale[32] = {
98 0, 1, 2, 3, 4, 5, 6, 7,
99 8, 10, 12, 14, 16, 18, 20, 22,
100 24, 28, 32, 36, 40, 44, 48, 52,
101 56, 64, 72, 80, 88, 96, 104, 112,
104 /* as H.263, but only 17 codes */
105 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
107 int code, sign, val, shift;
109 code = get_vlc2(&s->gb, ff_mv_vlc.table, MV_VLC_BITS, 2);
115 sign = get_bits1(&s->gb);
119 val = (val - 1) << shift;
120 val |= get_bits(&s->gb, shift);
127 /* modulo decoding */
128 return sign_extend(val, 5 + shift);
131 #define check_scantable_index(ctx, x) \
134 av_log(ctx->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
135 ctx->mb_x, ctx->mb_y); \
136 return AVERROR_INVALIDDATA; \
140 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
141 int16_t *block, int n)
143 int level, dc, diff, i, j, run;
145 RLTable *rl = &ff_rl_mpeg1;
146 uint8_t *const scantable = s->intra_scantable.permutated;
147 const uint16_t *quant_matrix = s->intra_matrix;
148 const int qscale = s->qscale;
151 component = (n <= 3 ? 0 : n - 4 + 1);
152 diff = decode_dc(&s->gb, component);
154 return AVERROR_INVALIDDATA;
155 dc = s->last_dc[component];
157 s->last_dc[component] = dc;
158 block[0] = dc * quant_matrix[0];
159 ff_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
162 OPEN_READER(re, &s->gb);
163 UPDATE_CACHE(re, &s->gb);
164 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
167 /* now quantify & encode AC coefficients */
169 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
174 check_scantable_index(s, i);
176 level = (level * qscale * quant_matrix[j]) >> 4;
177 level = (level - 1) | 1;
178 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
179 SHOW_SBITS(re, &s->gb, 1);
180 SKIP_BITS(re, &s->gb, 1);
183 run = SHOW_UBITS(re, &s->gb, 6) + 1;
184 LAST_SKIP_BITS(re, &s->gb, 6);
185 UPDATE_CACHE(re, &s->gb);
186 level = SHOW_SBITS(re, &s->gb, 8);
187 SKIP_BITS(re, &s->gb, 8);
189 level = SHOW_UBITS(re, &s->gb, 8) - 256;
190 SKIP_BITS(re, &s->gb, 8);
191 } else if (level == 0) {
192 level = SHOW_UBITS(re, &s->gb, 8);
193 SKIP_BITS(re, &s->gb, 8);
196 check_scantable_index(s, i);
200 level = (level * qscale * quant_matrix[j]) >> 4;
201 level = (level - 1) | 1;
204 level = (level * qscale * quant_matrix[j]) >> 4;
205 level = (level - 1) | 1;
210 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
213 UPDATE_CACHE(re, &s->gb);
216 LAST_SKIP_BITS(re, &s->gb, 2);
217 CLOSE_READER(re, &s->gb);
219 s->block_last_index[n] = i;
223 int ff_mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
225 return mpeg1_decode_block_intra(s, block, n);
228 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
229 int16_t *block, int n)
231 int level, i, j, run;
232 RLTable *rl = &ff_rl_mpeg1;
233 uint8_t *const scantable = s->intra_scantable.permutated;
234 const uint16_t *quant_matrix = s->inter_matrix;
235 const int qscale = s->qscale;
238 OPEN_READER(re, &s->gb);
240 // special case for first coefficient, no need to add second VLC table
241 UPDATE_CACHE(re, &s->gb);
242 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
243 level = (3 * qscale * quant_matrix[0]) >> 5;
244 level = (level - 1) | 1;
245 if (GET_CACHE(re, &s->gb) & 0x40000000)
249 SKIP_BITS(re, &s->gb, 2);
250 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
253 /* now quantify & encode AC coefficients */
255 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
260 check_scantable_index(s, i);
262 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
263 level = (level - 1) | 1;
264 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
265 SHOW_SBITS(re, &s->gb, 1);
266 SKIP_BITS(re, &s->gb, 1);
269 run = SHOW_UBITS(re, &s->gb, 6) + 1;
270 LAST_SKIP_BITS(re, &s->gb, 6);
271 UPDATE_CACHE(re, &s->gb);
272 level = SHOW_SBITS(re, &s->gb, 8);
273 SKIP_BITS(re, &s->gb, 8);
275 level = SHOW_UBITS(re, &s->gb, 8) - 256;
276 SKIP_BITS(re, &s->gb, 8);
277 } else if (level == 0) {
278 level = SHOW_UBITS(re, &s->gb, 8);
279 SKIP_BITS(re, &s->gb, 8);
282 check_scantable_index(s, i);
286 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
287 level = (level - 1) | 1;
290 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
291 level = (level - 1) | 1;
296 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
298 UPDATE_CACHE(re, &s->gb);
301 LAST_SKIP_BITS(re, &s->gb, 2);
302 CLOSE_READER(re, &s->gb);
304 s->block_last_index[n] = i;
309 * Note: this function can read out of range and crash for corrupt streams.
310 * Changing this would eat up any speed benefits it has.
311 * Do not use "fast" flag if you need the code to be robust.
313 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s,
314 int16_t *block, int n)
316 int level, i, j, run;
317 RLTable *rl = &ff_rl_mpeg1;
318 uint8_t *const scantable = s->intra_scantable.permutated;
319 const int qscale = s->qscale;
322 OPEN_READER(re, &s->gb);
324 // Special case for first coefficient, no need to add second VLC table.
325 UPDATE_CACHE(re, &s->gb);
326 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
327 level = (3 * qscale) >> 1;
328 level = (level - 1) | 1;
329 if (GET_CACHE(re, &s->gb) & 0x40000000)
333 SKIP_BITS(re, &s->gb, 2);
334 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
338 /* now quantify & encode AC coefficients */
340 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
345 check_scantable_index(s, i);
347 level = ((level * 2 + 1) * qscale) >> 1;
348 level = (level - 1) | 1;
349 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
350 SHOW_SBITS(re, &s->gb, 1);
351 SKIP_BITS(re, &s->gb, 1);
354 run = SHOW_UBITS(re, &s->gb, 6) + 1;
355 LAST_SKIP_BITS(re, &s->gb, 6);
356 UPDATE_CACHE(re, &s->gb);
357 level = SHOW_SBITS(re, &s->gb, 8);
358 SKIP_BITS(re, &s->gb, 8);
360 level = SHOW_UBITS(re, &s->gb, 8) - 256;
361 SKIP_BITS(re, &s->gb, 8);
362 } else if (level == 0) {
363 level = SHOW_UBITS(re, &s->gb, 8);
364 SKIP_BITS(re, &s->gb, 8);
367 check_scantable_index(s, i);
371 level = ((level * 2 + 1) * qscale) >> 1;
372 level = (level - 1) | 1;
375 level = ((level * 2 + 1) * qscale) >> 1;
376 level = (level - 1) | 1;
381 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
383 UPDATE_CACHE(re, &s->gb);
386 LAST_SKIP_BITS(re, &s->gb, 2);
387 CLOSE_READER(re, &s->gb);
389 s->block_last_index[n] = i;
393 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
394 int16_t *block, int n)
396 int level, i, j, run;
397 RLTable *rl = &ff_rl_mpeg1;
398 uint8_t *const scantable = s->intra_scantable.permutated;
399 const uint16_t *quant_matrix;
400 const int qscale = s->qscale;
406 OPEN_READER(re, &s->gb);
409 quant_matrix = s->inter_matrix;
411 quant_matrix = s->chroma_inter_matrix;
413 // Special case for first coefficient, no need to add second VLC table.
414 UPDATE_CACHE(re, &s->gb);
415 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
416 level = (3 * qscale * quant_matrix[0]) >> 5;
417 if (GET_CACHE(re, &s->gb) & 0x40000000)
422 SKIP_BITS(re, &s->gb, 2);
423 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
427 /* now quantify & encode AC coefficients */
429 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
434 check_scantable_index(s, i);
436 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
437 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
438 SHOW_SBITS(re, &s->gb, 1);
439 SKIP_BITS(re, &s->gb, 1);
442 run = SHOW_UBITS(re, &s->gb, 6) + 1;
443 LAST_SKIP_BITS(re, &s->gb, 6);
444 UPDATE_CACHE(re, &s->gb);
445 level = SHOW_SBITS(re, &s->gb, 12);
446 SKIP_BITS(re, &s->gb, 12);
449 check_scantable_index(s, i);
452 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
455 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
461 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
463 UPDATE_CACHE(re, &s->gb);
466 LAST_SKIP_BITS(re, &s->gb, 2);
467 CLOSE_READER(re, &s->gb);
469 block[63] ^= (mismatch & 1);
471 s->block_last_index[n] = i;
476 * Note: this function can read out of range and crash for corrupt streams.
477 * Changing this would eat up any speed benefits it has.
478 * Do not use "fast" flag if you need the code to be robust.
480 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
481 int16_t *block, int n)
483 int level, i, j, run;
484 RLTable *rl = &ff_rl_mpeg1;
485 uint8_t *const scantable = s->intra_scantable.permutated;
486 const int qscale = s->qscale;
487 OPEN_READER(re, &s->gb);
490 // special case for first coefficient, no need to add second VLC table
491 UPDATE_CACHE(re, &s->gb);
492 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
493 level = (3 * qscale) >> 1;
494 if (GET_CACHE(re, &s->gb) & 0x40000000)
498 SKIP_BITS(re, &s->gb, 2);
499 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
503 /* now quantify & encode AC coefficients */
505 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
510 level = ((level * 2 + 1) * qscale) >> 1;
511 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
512 SHOW_SBITS(re, &s->gb, 1);
513 SKIP_BITS(re, &s->gb, 1);
516 run = SHOW_UBITS(re, &s->gb, 6) + 1;
517 LAST_SKIP_BITS(re, &s->gb, 6);
518 UPDATE_CACHE(re, &s->gb);
519 level = SHOW_SBITS(re, &s->gb, 12);
520 SKIP_BITS(re, &s->gb, 12);
525 level = ((-level * 2 + 1) * qscale) >> 1;
528 level = ((level * 2 + 1) * qscale) >> 1;
533 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF || i > 63)
536 UPDATE_CACHE(re, &s->gb);
539 LAST_SKIP_BITS(re, &s->gb, 2);
540 CLOSE_READER(re, &s->gb);
541 s->block_last_index[n] = i;
545 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
546 int16_t *block, int n)
548 int level, dc, diff, i, j, run;
551 uint8_t *const scantable = s->intra_scantable.permutated;
552 const uint16_t *quant_matrix;
553 const int qscale = s->qscale;
558 quant_matrix = s->intra_matrix;
561 quant_matrix = s->chroma_intra_matrix;
562 component = (n & 1) + 1;
564 diff = decode_dc(&s->gb, component);
566 return AVERROR_INVALIDDATA;
567 dc = s->last_dc[component];
569 s->last_dc[component] = dc;
570 block[0] = dc << (3 - s->intra_dc_precision);
571 ff_dlog(s->avctx, "dc=%d\n", block[0]);
572 mismatch = block[0] ^ 1;
574 if (s->intra_vlc_format)
580 OPEN_READER(re, &s->gb);
581 /* now quantify & encode AC coefficients */
583 UPDATE_CACHE(re, &s->gb);
584 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
589 } else if (level != 0) {
591 check_scantable_index(s, i);
593 level = (level * qscale * quant_matrix[j]) >> 4;
594 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
595 SHOW_SBITS(re, &s->gb, 1);
596 LAST_SKIP_BITS(re, &s->gb, 1);
599 run = SHOW_UBITS(re, &s->gb, 6) + 1;
600 LAST_SKIP_BITS(re, &s->gb, 6);
601 UPDATE_CACHE(re, &s->gb);
602 level = SHOW_SBITS(re, &s->gb, 12);
603 SKIP_BITS(re, &s->gb, 12);
605 check_scantable_index(s, i);
608 level = (-level * qscale * quant_matrix[j]) >> 4;
611 level = (level * qscale * quant_matrix[j]) >> 4;
618 CLOSE_READER(re, &s->gb);
620 block[63] ^= mismatch & 1;
622 s->block_last_index[n] = i;
627 * Note: this function can read out of range and crash for corrupt streams.
628 * Changing this would eat up any speed benefits it has.
629 * Do not use "fast" flag if you need the code to be robust.
631 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
632 int16_t *block, int n)
634 int level, dc, diff, i, j, run;
637 uint8_t *const scantable = s->intra_scantable.permutated;
638 const uint16_t *quant_matrix;
639 const int qscale = s->qscale;
643 quant_matrix = s->intra_matrix;
646 quant_matrix = s->chroma_intra_matrix;
647 component = (n & 1) + 1;
649 diff = decode_dc(&s->gb, component);
651 return AVERROR_INVALIDDATA;
652 dc = s->last_dc[component];
654 s->last_dc[component] = dc;
655 block[0] = dc << (3 - s->intra_dc_precision);
657 if (s->intra_vlc_format)
663 OPEN_READER(re, &s->gb);
664 /* now quantify & encode AC coefficients */
666 UPDATE_CACHE(re, &s->gb);
667 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
670 if (level >= 64 || i > 63) {
672 } else if (level != 0) {
675 level = (level * qscale * quant_matrix[j]) >> 4;
676 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
677 SHOW_SBITS(re, &s->gb, 1);
678 LAST_SKIP_BITS(re, &s->gb, 1);
681 run = SHOW_UBITS(re, &s->gb, 6) + 1;
682 LAST_SKIP_BITS(re, &s->gb, 6);
683 UPDATE_CACHE(re, &s->gb);
684 level = SHOW_SBITS(re, &s->gb, 12);
685 SKIP_BITS(re, &s->gb, 12);
689 level = (-level * qscale * quant_matrix[j]) >> 4;
692 level = (level * qscale * quant_matrix[j]) >> 4;
698 CLOSE_READER(re, &s->gb);
701 s->block_last_index[n] = i;
705 /******************************************/
708 static inline int get_dmv(MpegEncContext *s)
710 if (get_bits1(&s->gb))
711 return 1 - (get_bits1(&s->gb) << 1);
716 static inline int get_qscale(MpegEncContext *s)
718 int qscale = get_bits(&s->gb, 5);
720 return non_linear_qscale[qscale];
726 /* motion type (for MPEG-2) */
732 static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
734 int i, j, k, cbp, val, mb_type, motion_type;
735 const int mb_block_count = 4 + (1 << s->chroma_format);
738 ff_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
740 av_assert2(s->mb_skipped == 0);
742 if (s->mb_skip_run-- != 0) {
743 if (s->pict_type == AV_PICTURE_TYPE_P) {
745 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
746 MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
751 mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
753 // FIXME not sure if this is allowed in MPEG at all
754 mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
755 if (IS_INTRA(mb_type)) {
756 av_log(s->avctx, AV_LOG_ERROR, "skip with previntra\n");
757 return AVERROR_INVALIDDATA;
759 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
760 mb_type | MB_TYPE_SKIP;
762 if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
769 switch (s->pict_type) {
771 case AV_PICTURE_TYPE_I:
772 if (get_bits1(&s->gb) == 0) {
773 if (get_bits1(&s->gb) == 0) {
774 av_log(s->avctx, AV_LOG_ERROR,
775 "invalid mb type in I Frame at %d %d\n",
777 return AVERROR_INVALIDDATA;
779 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
781 mb_type = MB_TYPE_INTRA;
784 case AV_PICTURE_TYPE_P:
785 mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
787 av_log(s->avctx, AV_LOG_ERROR,
788 "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
789 return AVERROR_INVALIDDATA;
791 mb_type = ptype2mb_type[mb_type];
793 case AV_PICTURE_TYPE_B:
794 mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
796 av_log(s->avctx, AV_LOG_ERROR,
797 "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
798 return AVERROR_INVALIDDATA;
800 mb_type = btype2mb_type[mb_type];
803 ff_dlog(s->avctx, "mb_type=%x\n", mb_type);
804 // motion_type = 0; /* avoid warning */
805 if (IS_INTRA(mb_type)) {
806 s->bdsp.clear_blocks(s->block[0]);
808 if (!s->chroma_y_shift)
809 s->bdsp.clear_blocks(s->block[6]);
811 /* compute DCT type */
812 // FIXME: add an interlaced_dct coded var?
813 if (s->picture_structure == PICT_FRAME &&
814 !s->frame_pred_frame_dct)
815 s->interlaced_dct = get_bits1(&s->gb);
817 if (IS_QUANT(mb_type))
818 s->qscale = get_qscale(s);
820 if (s->concealment_motion_vectors) {
821 /* just parse them */
822 if (s->picture_structure != PICT_FRAME)
823 skip_bits1(&s->gb); /* field select */
826 s->last_mv[0][0][0] =
827 s->last_mv[0][1][0] = mpeg_decode_motion(s, s->mpeg_f_code[0][0],
828 s->last_mv[0][0][0]);
830 s->last_mv[0][0][1] =
831 s->last_mv[0][1][1] = mpeg_decode_motion(s, s->mpeg_f_code[0][1],
832 s->last_mv[0][0][1]);
834 check_marker(&s->gb, "after concealment_motion_vectors");
836 /* reset mv prediction */
837 memset(s->last_mv, 0, sizeof(s->last_mv));
840 // if 1, we memcpy blocks in xvmcvideo
841 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
842 ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
844 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
845 if (s->flags2 & CODEC_FLAG2_FAST) {
846 for (i = 0; i < 6; i++)
847 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
849 for (i = 0; i < mb_block_count; i++)
850 if ((ret = mpeg2_decode_block_intra(s, *s->pblocks[i], i)) < 0)
854 for (i = 0; i < 6; i++)
855 if ((ret = mpeg1_decode_block_intra(s, *s->pblocks[i], i)) < 0)
859 if (mb_type & MB_TYPE_ZERO_MV) {
860 av_assert2(mb_type & MB_TYPE_CBP);
862 s->mv_dir = MV_DIR_FORWARD;
863 if (s->picture_structure == PICT_FRAME) {
864 if (s->picture_structure == PICT_FRAME
865 && !s->frame_pred_frame_dct)
866 s->interlaced_dct = get_bits1(&s->gb);
867 s->mv_type = MV_TYPE_16X16;
869 s->mv_type = MV_TYPE_FIELD;
870 mb_type |= MB_TYPE_INTERLACED;
871 s->field_select[0][0] = s->picture_structure - 1;
874 if (IS_QUANT(mb_type))
875 s->qscale = get_qscale(s);
877 s->last_mv[0][0][0] = 0;
878 s->last_mv[0][0][1] = 0;
879 s->last_mv[0][1][0] = 0;
880 s->last_mv[0][1][1] = 0;
884 av_assert2(mb_type & MB_TYPE_L0L1);
885 // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
886 /* get additional motion vector type */
887 if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct) {
888 motion_type = MT_FRAME;
890 motion_type = get_bits(&s->gb, 2);
891 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
892 s->interlaced_dct = get_bits1(&s->gb);
895 if (IS_QUANT(mb_type))
896 s->qscale = get_qscale(s);
899 s->mv_dir = (mb_type >> 13) & 3;
900 ff_dlog(s->avctx, "motion_type=%d\n", motion_type);
901 switch (motion_type) {
902 case MT_FRAME: /* or MT_16X8 */
903 if (s->picture_structure == PICT_FRAME) {
904 mb_type |= MB_TYPE_16x16;
905 s->mv_type = MV_TYPE_16X16;
906 for (i = 0; i < 2; i++) {
907 if (USES_LIST(mb_type, i)) {
910 s->last_mv[i][0][0] =
911 s->last_mv[i][1][0] =
912 mpeg_decode_motion(s, s->mpeg_f_code[i][0],
913 s->last_mv[i][0][0]);
915 s->last_mv[i][0][1] =
916 s->last_mv[i][1][1] =
917 mpeg_decode_motion(s, s->mpeg_f_code[i][1],
918 s->last_mv[i][0][1]);
919 /* full_pel: only for MPEG-1 */
920 if (s->full_pel[i]) {
921 s->mv[i][0][0] <<= 1;
922 s->mv[i][0][1] <<= 1;
927 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
928 s->mv_type = MV_TYPE_16X8;
929 for (i = 0; i < 2; i++) {
930 if (USES_LIST(mb_type, i)) {
932 for (j = 0; j < 2; j++) {
933 s->field_select[i][j] = get_bits1(&s->gb);
934 for (k = 0; k < 2; k++) {
935 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
936 s->last_mv[i][j][k]);
937 s->last_mv[i][j][k] = val;
938 s->mv[i][j][k] = val;
946 s->mv_type = MV_TYPE_FIELD;
947 if (s->picture_structure == PICT_FRAME) {
948 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
949 for (i = 0; i < 2; i++) {
950 if (USES_LIST(mb_type, i)) {
951 for (j = 0; j < 2; j++) {
952 s->field_select[i][j] = get_bits1(&s->gb);
953 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
954 s->last_mv[i][j][0]);
955 s->last_mv[i][j][0] = val;
956 s->mv[i][j][0] = val;
957 ff_dlog(s->avctx, "fmx=%d\n", val);
958 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
959 s->last_mv[i][j][1] >> 1);
960 s->last_mv[i][j][1] = 2 * val;
961 s->mv[i][j][1] = val;
962 ff_dlog(s->avctx, "fmy=%d\n", val);
967 av_assert0(!s->progressive_sequence);
968 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
969 for (i = 0; i < 2; i++) {
970 if (USES_LIST(mb_type, i)) {
971 s->field_select[i][0] = get_bits1(&s->gb);
972 for (k = 0; k < 2; k++) {
973 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
974 s->last_mv[i][0][k]);
975 s->last_mv[i][0][k] = val;
976 s->last_mv[i][1][k] = val;
977 s->mv[i][0][k] = val;
984 if (s->progressive_sequence){
985 av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
986 return AVERROR_INVALIDDATA;
988 s->mv_type = MV_TYPE_DMV;
989 for (i = 0; i < 2; i++) {
990 if (USES_LIST(mb_type, i)) {
991 int dmx, dmy, mx, my, m;
992 const int my_shift = s->picture_structure == PICT_FRAME;
994 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
995 s->last_mv[i][0][0]);
996 s->last_mv[i][0][0] = mx;
997 s->last_mv[i][1][0] = mx;
999 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
1000 s->last_mv[i][0][1] >> my_shift);
1004 s->last_mv[i][0][1] = my << my_shift;
1005 s->last_mv[i][1][1] = my << my_shift;
1007 s->mv[i][0][0] = mx;
1008 s->mv[i][0][1] = my;
1009 s->mv[i][1][0] = mx; // not used
1010 s->mv[i][1][1] = my; // not used
1012 if (s->picture_structure == PICT_FRAME) {
1013 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
1015 // m = 1 + 2 * s->top_field_first;
1016 m = s->top_field_first ? 1 : 3;
1018 /* top -> top pred */
1019 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1020 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1022 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1023 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1025 mb_type |= MB_TYPE_16x16;
1027 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1028 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1029 if (s->picture_structure == PICT_TOP_FIELD)
1038 av_log(s->avctx, AV_LOG_ERROR,
1039 "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1040 return AVERROR_INVALIDDATA;
1045 if (HAS_CBP(mb_type)) {
1046 s->bdsp.clear_blocks(s->block[0]);
1048 cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1049 if (mb_block_count > 6) {
1050 cbp <<= mb_block_count - 6;
1051 cbp |= get_bits(&s->gb, mb_block_count - 6);
1052 s->bdsp.clear_blocks(s->block[6]);
1055 av_log(s->avctx, AV_LOG_ERROR,
1056 "invalid cbp %d at %d %d\n", cbp, s->mb_x, s->mb_y);
1057 return AVERROR_INVALIDDATA;
1060 // if 1, we memcpy blocks in xvmcvideo
1061 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
1062 ff_xvmc_pack_pblocks(s, cbp);
1064 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1065 if (s->flags2 & CODEC_FLAG2_FAST) {
1066 for (i = 0; i < 6; i++) {
1068 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
1070 s->block_last_index[i] = -1;
1074 cbp <<= 12 - mb_block_count;
1076 for (i = 0; i < mb_block_count; i++) {
1077 if (cbp & (1 << 11)) {
1078 if ((ret = mpeg2_decode_block_non_intra(s, *s->pblocks[i], i)) < 0)
1081 s->block_last_index[i] = -1;
1087 if (s->flags2 & CODEC_FLAG2_FAST) {
1088 for (i = 0; i < 6; i++) {
1090 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
1092 s->block_last_index[i] = -1;
1096 for (i = 0; i < 6; i++) {
1098 if ((ret = mpeg1_decode_block_inter(s, *s->pblocks[i], i)) < 0)
1101 s->block_last_index[i] = -1;
1108 for (i = 0; i < 12; i++)
1109 s->block_last_index[i] = -1;
1113 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
1118 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1120 Mpeg1Context *s = avctx->priv_data;
1121 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1123 ff_mpv_decode_defaults(s2);
1125 if ( avctx->codec_tag != AV_RL32("VCR2")
1126 && avctx->codec_tag != AV_RL32("BW10"))
1127 avctx->coded_width = avctx->coded_height = 0; // do not trust dimensions from input
1128 ff_mpv_decode_init(s2, avctx);
1130 s->mpeg_enc_ctx.avctx = avctx;
1132 /* we need some permutation to store matrices,
1133 * until the decoder sets the real permutation. */
1134 ff_mpv_idct_init(s2);
1135 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1136 ff_mpeg12_init_vlcs();
1138 s->mpeg_enc_ctx_allocated = 0;
1139 s->mpeg_enc_ctx.picture_number = 0;
1140 s->repeat_field = 0;
1141 s->mpeg_enc_ctx.codec_id = avctx->codec->id;
1142 avctx->color_range = AVCOL_RANGE_MPEG;
1143 if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
1144 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1146 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1150 static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
1151 const AVCodecContext *avctx_from)
1153 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1154 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1157 if (avctx == avctx_from ||
1158 !ctx_from->mpeg_enc_ctx_allocated ||
1159 !s1->context_initialized)
1162 err = ff_mpeg_update_thread_context(avctx, avctx_from);
1166 if (!ctx->mpeg_enc_ctx_allocated)
1167 memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
1169 if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
1170 s->picture_number++;
1175 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1176 const uint8_t *new_perm)
1178 uint16_t temp_matrix[64];
1181 memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
1183 for (i = 0; i < 64; i++)
1184 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1187 static const enum AVPixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
1188 #if CONFIG_MPEG1_XVMC_HWACCEL
1191 #if CONFIG_MPEG1_VDPAU_HWACCEL
1192 AV_PIX_FMT_VDPAU_MPEG1,
1199 static const enum AVPixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
1200 #if CONFIG_MPEG2_XVMC_HWACCEL
1203 #if CONFIG_MPEG2_VDPAU_HWACCEL
1204 AV_PIX_FMT_VDPAU_MPEG2,
1207 #if CONFIG_MPEG2_DXVA2_HWACCEL
1208 AV_PIX_FMT_DXVA2_VLD,
1210 #if CONFIG_MPEG2_VAAPI_HWACCEL
1211 AV_PIX_FMT_VAAPI_VLD,
1217 static const enum AVPixelFormat mpeg12_pixfmt_list_422[] = {
1222 static const enum AVPixelFormat mpeg12_pixfmt_list_444[] = {
1227 static inline int uses_vdpau(AVCodecContext *avctx) {
1228 return avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG2;
1231 static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
1233 Mpeg1Context *s1 = avctx->priv_data;
1234 MpegEncContext *s = &s1->mpeg_enc_ctx;
1235 const enum AVPixelFormat *pix_fmts;
1237 if (s->chroma_format < 2)
1238 pix_fmts = avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO ?
1239 mpeg1_hwaccel_pixfmt_list_420 :
1240 mpeg2_hwaccel_pixfmt_list_420;
1241 else if (s->chroma_format == 2)
1242 pix_fmts = mpeg12_pixfmt_list_422;
1244 pix_fmts = mpeg12_pixfmt_list_444;
1246 return ff_thread_get_format(avctx, pix_fmts);
1249 static void setup_hwaccel_for_pixfmt(AVCodecContext *avctx)
1251 // until then pix_fmt may be changed right after codec init
1252 if (avctx->hwaccel || uses_vdpau(avctx))
1253 if (avctx->idct_algo == FF_IDCT_AUTO)
1254 avctx->idct_algo = FF_IDCT_SIMPLE;
1256 if (avctx->hwaccel && avctx->pix_fmt == AV_PIX_FMT_XVMC) {
1257 Mpeg1Context *s1 = avctx->priv_data;
1258 MpegEncContext *s = &s1->mpeg_enc_ctx;
1260 s->pack_pblocks = 1;
1262 avctx->xvmc_acceleration = 2;
1263 #endif /* FF_API_XVMC */
1267 /* Call this function when we know all parameters.
1268 * It may be called in different places for MPEG-1 and MPEG-2. */
1269 static int mpeg_decode_postinit(AVCodecContext *avctx)
1271 Mpeg1Context *s1 = avctx->priv_data;
1272 MpegEncContext *s = &s1->mpeg_enc_ctx;
1273 uint8_t old_permutation[64];
1276 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1278 avctx->sample_aspect_ratio = av_d2q(1.0 / ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1281 if (s->aspect_ratio_info > 1) {
1283 av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1284 (AVRational) { s1->pan_scan.width,
1285 s1->pan_scan.height }),
1286 (AVRational) { s->width, s->height });
1288 /* We ignore the spec here and guess a bit as reality does not
1289 * match the spec, see for example res_change_ffmpeg_aspect.ts
1290 * and sequence-display-aspect.mpg.
1291 * issue1613, 621, 562 */
1292 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1293 (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
1294 av_cmp_q(dar, (AVRational) { 16, 9 }))) {
1295 s->avctx->sample_aspect_ratio =
1296 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1297 (AVRational) { s->width, s->height });
1299 s->avctx->sample_aspect_ratio =
1300 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1301 (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
1302 // issue1613 4/3 16/9 -> 16/9
1303 // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1304 // widescreen-issue562.mpg 4/3 16/9 -> 16/9
1305 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1306 ff_dlog(avctx, "A %d/%d\n",
1307 ff_mpeg2_aspect[s->aspect_ratio_info].num,
1308 ff_mpeg2_aspect[s->aspect_ratio_info].den);
1309 ff_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
1310 s->avctx->sample_aspect_ratio.den);
1313 s->avctx->sample_aspect_ratio =
1314 ff_mpeg2_aspect[s->aspect_ratio_info];
1318 ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
1320 if ((s1->mpeg_enc_ctx_allocated == 0) ||
1321 avctx->coded_width != s->width ||
1322 avctx->coded_height != s->height ||
1323 s1->save_width != s->width ||
1324 s1->save_height != s->height ||
1325 av_cmp_q(s1->save_aspect, s->avctx->sample_aspect_ratio) ||
1326 (s1->save_progressive_seq != s->progressive_sequence && FFALIGN(s->height, 16) != FFALIGN(s->height, 32)) ||
1328 if (s1->mpeg_enc_ctx_allocated) {
1329 ParseContext pc = s->parse_context;
1330 s->parse_context.buffer = 0;
1331 ff_mpv_common_end(s);
1332 s->parse_context = pc;
1333 s1->mpeg_enc_ctx_allocated = 0;
1336 ret = ff_set_dimensions(avctx, s->width, s->height);
1340 if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->bit_rate) {
1341 avctx->rc_max_rate = s->bit_rate;
1342 } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && s->bit_rate &&
1343 (s->bit_rate != 0x3FFFF*400 || s->vbv_delay != 0xFFFF)) {
1344 avctx->bit_rate = s->bit_rate;
1346 s1->save_aspect = s->avctx->sample_aspect_ratio;
1347 s1->save_width = s->width;
1348 s1->save_height = s->height;
1349 s1->save_progressive_seq = s->progressive_sequence;
1351 /* low_delay may be forced, in this case we will have B-frames
1352 * that behave like P-frames. */
1353 avctx->has_b_frames = !s->low_delay;
1355 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1357 avctx->framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
1358 avctx->ticks_per_frame = 1;
1361 av_reduce(&s->avctx->framerate.num,
1362 &s->avctx->framerate.den,
1363 ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
1364 ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1366 avctx->ticks_per_frame = 2;
1369 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1370 setup_hwaccel_for_pixfmt(avctx);
1372 /* Quantization matrices may need reordering
1373 * if DCT permutation is changed. */
1374 memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
1376 ff_mpv_idct_init(s);
1377 if ((ret = ff_mpv_common_init(s)) < 0)
1380 quant_matrix_rebuild(s->intra_matrix, old_permutation, s->idsp.idct_permutation);
1381 quant_matrix_rebuild(s->inter_matrix, old_permutation, s->idsp.idct_permutation);
1382 quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
1383 quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
1385 s1->mpeg_enc_ctx_allocated = 1;
1390 static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
1393 Mpeg1Context *s1 = avctx->priv_data;
1394 MpegEncContext *s = &s1->mpeg_enc_ctx;
1395 int ref, f_code, vbv_delay;
1397 init_get_bits(&s->gb, buf, buf_size * 8);
1399 ref = get_bits(&s->gb, 10); /* temporal ref */
1400 s->pict_type = get_bits(&s->gb, 3);
1401 if (s->pict_type == 0 || s->pict_type > 3)
1402 return AVERROR_INVALIDDATA;
1404 vbv_delay = get_bits(&s->gb, 16);
1405 s->vbv_delay = vbv_delay;
1406 if (s->pict_type == AV_PICTURE_TYPE_P ||
1407 s->pict_type == AV_PICTURE_TYPE_B) {
1408 s->full_pel[0] = get_bits1(&s->gb);
1409 f_code = get_bits(&s->gb, 3);
1410 if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1411 return AVERROR_INVALIDDATA;
1413 s->mpeg_f_code[0][0] = f_code;
1414 s->mpeg_f_code[0][1] = f_code;
1416 if (s->pict_type == AV_PICTURE_TYPE_B) {
1417 s->full_pel[1] = get_bits1(&s->gb);
1418 f_code = get_bits(&s->gb, 3);
1419 if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1420 return AVERROR_INVALIDDATA;
1422 s->mpeg_f_code[1][0] = f_code;
1423 s->mpeg_f_code[1][1] = f_code;
1425 s->current_picture.f->pict_type = s->pict_type;
1426 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1428 if (avctx->debug & FF_DEBUG_PICT_INFO)
1429 av_log(avctx, AV_LOG_DEBUG,
1430 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1437 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1439 MpegEncContext *s = &s1->mpeg_enc_ctx;
1440 int horiz_size_ext, vert_size_ext;
1443 skip_bits(&s->gb, 1); /* profile and level esc*/
1444 s->avctx->profile = get_bits(&s->gb, 3);
1445 s->avctx->level = get_bits(&s->gb, 4);
1446 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1447 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1448 horiz_size_ext = get_bits(&s->gb, 2);
1449 vert_size_ext = get_bits(&s->gb, 2);
1450 s->width |= (horiz_size_ext << 12);
1451 s->height |= (vert_size_ext << 12);
1452 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1453 s->bit_rate += (bit_rate_ext << 18) * 400;
1454 check_marker(&s->gb, "after bit rate extension");
1455 s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1457 s->low_delay = get_bits1(&s->gb);
1458 if (s->flags & CODEC_FLAG_LOW_DELAY)
1461 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1462 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1464 ff_dlog(s->avctx, "sequence extension\n");
1465 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1467 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1468 av_log(s->avctx, AV_LOG_DEBUG,
1469 "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%d\n",
1470 s->avctx->profile, s->avctx->level, s->progressive_sequence, s->chroma_format,
1471 s->avctx->rc_buffer_size, s->bit_rate);
1474 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1476 MpegEncContext *s = &s1->mpeg_enc_ctx;
1477 int color_description, w, h;
1479 skip_bits(&s->gb, 3); /* video format */
1480 color_description = get_bits1(&s->gb);
1481 if (color_description) {
1482 s->avctx->color_primaries = get_bits(&s->gb, 8);
1483 s->avctx->color_trc = get_bits(&s->gb, 8);
1484 s->avctx->colorspace = get_bits(&s->gb, 8);
1486 w = get_bits(&s->gb, 14);
1487 skip_bits(&s->gb, 1); // marker
1488 h = get_bits(&s->gb, 14);
1489 // remaining 3 bits are zero padding
1491 s1->pan_scan.width = 16 * w;
1492 s1->pan_scan.height = 16 * h;
1494 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1495 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1498 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1500 MpegEncContext *s = &s1->mpeg_enc_ctx;
1504 if (s->progressive_sequence) {
1505 if (s->repeat_first_field) {
1507 if (s->top_field_first)
1511 if (s->picture_structure == PICT_FRAME) {
1513 if (s->repeat_first_field)
1517 for (i = 0; i < nofco; i++) {
1518 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1519 skip_bits(&s->gb, 1); // marker
1520 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1521 skip_bits(&s->gb, 1); // marker
1524 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1525 av_log(s->avctx, AV_LOG_DEBUG,
1526 "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
1527 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1528 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1529 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1532 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
1533 uint16_t matrix1[64], int intra)
1537 for (i = 0; i < 64; i++) {
1538 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1539 int v = get_bits(&s->gb, 8);
1541 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1542 return AVERROR_INVALIDDATA;
1544 if (intra && i == 0 && v != 8) {
1545 av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v);
1546 v = 8; // needed by pink.mpg / issue1046
1555 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1557 ff_dlog(s->avctx, "matrix extension\n");
1559 if (get_bits1(&s->gb))
1560 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1561 if (get_bits1(&s->gb))
1562 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1563 if (get_bits1(&s->gb))
1564 load_matrix(s, s->chroma_intra_matrix, NULL, 1);
1565 if (get_bits1(&s->gb))
1566 load_matrix(s, s->chroma_inter_matrix, NULL, 0);
1569 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1571 MpegEncContext *s = &s1->mpeg_enc_ctx;
1573 s->full_pel[0] = s->full_pel[1] = 0;
1574 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1575 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1576 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1577 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1578 if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1579 av_log(s->avctx, AV_LOG_ERROR,
1580 "Missing picture start code, guessing missing values\n");
1581 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1582 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1583 s->pict_type = AV_PICTURE_TYPE_I;
1585 s->pict_type = AV_PICTURE_TYPE_P;
1587 s->pict_type = AV_PICTURE_TYPE_B;
1588 s->current_picture.f->pict_type = s->pict_type;
1589 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1591 s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0];
1592 s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1];
1593 s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0];
1594 s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1];
1596 s->intra_dc_precision = get_bits(&s->gb, 2);
1597 s->picture_structure = get_bits(&s->gb, 2);
1598 s->top_field_first = get_bits1(&s->gb);
1599 s->frame_pred_frame_dct = get_bits1(&s->gb);
1600 s->concealment_motion_vectors = get_bits1(&s->gb);
1601 s->q_scale_type = get_bits1(&s->gb);
1602 s->intra_vlc_format = get_bits1(&s->gb);
1603 s->alternate_scan = get_bits1(&s->gb);
1604 s->repeat_first_field = get_bits1(&s->gb);
1605 s->chroma_420_type = get_bits1(&s->gb);
1606 s->progressive_frame = get_bits1(&s->gb);
1608 if (s->alternate_scan) {
1609 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
1610 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
1612 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
1613 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
1616 /* composite display not parsed */
1617 ff_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1618 ff_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1619 ff_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1620 ff_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1621 ff_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1622 ff_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1623 ff_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1624 ff_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1625 ff_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1628 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1630 AVCodecContext *avctx = s->avctx;
1631 Mpeg1Context *s1 = (Mpeg1Context *) s;
1634 /* start frame decoding */
1635 if (s->first_field || s->picture_structure == PICT_FRAME) {
1636 AVFrameSideData *pan_scan;
1638 if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
1641 ff_mpeg_er_frame_start(s);
1643 /* first check if we must repeat the frame */
1644 s->current_picture_ptr->f->repeat_pict = 0;
1645 if (s->repeat_first_field) {
1646 if (s->progressive_sequence) {
1647 if (s->top_field_first)
1648 s->current_picture_ptr->f->repeat_pict = 4;
1650 s->current_picture_ptr->f->repeat_pict = 2;
1651 } else if (s->progressive_frame) {
1652 s->current_picture_ptr->f->repeat_pict = 1;
1656 pan_scan = av_frame_new_side_data(s->current_picture_ptr->f,
1657 AV_FRAME_DATA_PANSCAN,
1658 sizeof(s1->pan_scan));
1660 return AVERROR(ENOMEM);
1661 memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
1663 if (s1->a53_caption) {
1664 AVFrameSideData *sd = av_frame_new_side_data(
1665 s->current_picture_ptr->f, AV_FRAME_DATA_A53_CC,
1666 s1->a53_caption_size);
1668 memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
1669 av_freep(&s1->a53_caption);
1672 if (s1->has_stereo3d) {
1673 AVStereo3D *stereo = av_stereo3d_create_side_data(s->current_picture_ptr->f);
1675 return AVERROR(ENOMEM);
1677 *stereo = s1->stereo3d;
1678 s1->has_stereo3d = 0;
1682 AVFrameSideData *sd =
1683 av_frame_new_side_data(s->current_picture_ptr->f,
1684 AV_FRAME_DATA_AFD, 1);
1686 return AVERROR(ENOMEM);
1688 *sd->data = s1->afd;
1692 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1693 ff_thread_finish_setup(avctx);
1694 } else { // second field
1697 if (!s->current_picture_ptr) {
1698 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1699 return AVERROR_INVALIDDATA;
1702 if (s->avctx->hwaccel &&
1703 (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
1704 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1705 av_log(avctx, AV_LOG_ERROR,
1706 "hardware accelerator failed to decode first field\n");
1709 for (i = 0; i < 4; i++) {
1710 s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
1711 if (s->picture_structure == PICT_BOTTOM_FIELD)
1712 s->current_picture.f->data[i] +=
1713 s->current_picture_ptr->f->linesize[i];
1717 if (avctx->hwaccel) {
1718 if ((ret = avctx->hwaccel->start_frame(avctx, buf, buf_size)) < 0)
1725 #define DECODE_SLICE_ERROR -1
1726 #define DECODE_SLICE_OK 0
1730 * MpegEncContext.mb_y must be set to the MB row from the startcode.
1731 * @return DECODE_SLICE_ERROR if the slice is damaged,
1732 * DECODE_SLICE_OK if this slice is OK
1734 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1735 const uint8_t **buf, int buf_size)
1737 AVCodecContext *avctx = s->avctx;
1738 const int lowres = s->avctx->lowres;
1739 const int field_pic = s->picture_structure != PICT_FRAME;
1743 s->resync_mb_y = -1;
1745 av_assert0(mb_y < s->mb_height);
1747 init_get_bits(&s->gb, *buf, buf_size * 8);
1748 if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
1749 skip_bits(&s->gb, 3);
1751 ff_mpeg1_clean_buffers(s);
1752 s->interlaced_dct = 0;
1754 s->qscale = get_qscale(s);
1756 if (s->qscale == 0) {
1757 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1758 return AVERROR_INVALIDDATA;
1761 /* extra slice info */
1762 if (skip_1stop_8data_bits(&s->gb) < 0)
1763 return AVERROR_INVALIDDATA;
1767 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1770 while (get_bits_left(&s->gb) > 0) {
1771 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1772 MBINCR_VLC_BITS, 2);
1774 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1775 return AVERROR_INVALIDDATA;
1780 /* otherwise, stuffing, nothing to do */
1788 if (s->mb_x >= (unsigned) s->mb_width) {
1789 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1790 return AVERROR_INVALIDDATA;
1793 if (avctx->hwaccel && avctx->hwaccel->decode_slice) {
1794 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1795 int start_code = -1;
1796 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1797 if (buf_end < *buf + buf_size)
1800 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1801 return DECODE_SLICE_ERROR;
1803 return DECODE_SLICE_OK;
1806 s->resync_mb_x = s->mb_x;
1807 s->resync_mb_y = s->mb_y = mb_y;
1809 ff_init_block_index(s);
1811 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1812 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1813 av_log(s->avctx, AV_LOG_DEBUG,
1814 "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
1816 s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
1817 s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1818 s->pict_type == AV_PICTURE_TYPE_I ? "I" :
1819 (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
1820 (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
1821 s->progressive_sequence ? "ps" : "",
1822 s->progressive_frame ? "pf" : "",
1823 s->alternate_scan ? "alt" : "",
1824 s->top_field_first ? "top" : "",
1825 s->intra_dc_precision, s->picture_structure,
1826 s->frame_pred_frame_dct, s->concealment_motion_vectors,
1827 s->q_scale_type, s->intra_vlc_format,
1828 s->repeat_first_field, s->chroma_420_type ? "420" : "");
1833 // If 1, we memcpy blocks in xvmcvideo.
1834 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
1835 ff_xvmc_init_block(s); // set s->block
1837 if ((ret = mpeg_decode_mb(s, s->block)) < 0)
1840 // Note motion_val is normally NULL unless we want to extract the MVs.
1841 if (s->current_picture.motion_val[0] && !s->encoding) {
1842 const int wrap = s->b8_stride;
1843 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1844 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1845 int motion_x, motion_y, dir, i;
1847 for (i = 0; i < 2; i++) {
1848 for (dir = 0; dir < 2; dir++) {
1850 (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1851 motion_x = motion_y = 0;
1852 } else if (s->mv_type == MV_TYPE_16X16 ||
1853 (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1854 motion_x = s->mv[dir][0][0];
1855 motion_y = s->mv[dir][0][1];
1856 } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
1857 motion_x = s->mv[dir][i][0];
1858 motion_y = s->mv[dir][i][1];
1861 s->current_picture.motion_val[dir][xy][0] = motion_x;
1862 s->current_picture.motion_val[dir][xy][1] = motion_y;
1863 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1864 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1865 s->current_picture.ref_index [dir][b8_xy] =
1866 s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1867 av_assert2(s->field_select[dir][i] == 0 ||
1868 s->field_select[dir][i] == 1);
1875 s->dest[0] += 16 >> lowres;
1876 s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
1877 s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
1879 ff_mpv_decode_mb(s, s->block);
1881 if (++s->mb_x >= s->mb_width) {
1882 const int mb_size = 16 >> s->avctx->lowres;
1884 ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
1885 ff_mpv_report_decode_progress(s);
1888 s->mb_y += 1 << field_pic;
1890 if (s->mb_y >= s->mb_height) {
1891 int left = get_bits_left(&s->gb);
1892 int is_d10 = s->chroma_format == 2 &&
1893 s->pict_type == AV_PICTURE_TYPE_I &&
1894 avctx->profile == 0 && avctx->level == 5 &&
1895 s->intra_dc_precision == 2 &&
1896 s->q_scale_type == 1 && s->alternate_scan == 0 &&
1897 s->progressive_frame == 0
1898 /* vbv_delay == 0xBBB || 0xE10 */;
1900 if (left >= 32 && !is_d10) {
1901 GetBitContext gb = s->gb;
1902 align_get_bits(&gb);
1903 if (show_bits(&gb, 24) == 0x060E2B) {
1904 av_log(avctx, AV_LOG_DEBUG, "Invalid MXF data found in video stream\n");
1910 (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
1911 ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) {
1912 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
1913 left, show_bits(&s->gb, FFMIN(left, 23)));
1914 return AVERROR_INVALIDDATA;
1918 // There are some files out there which are missing the last slice
1919 // in cases where the slice is completely outside the visible
1920 // area, we detect this here instead of running into the end expecting
1922 if (s->mb_y >= ((s->height + 15) >> 4) &&
1923 s->progressive_frame &&
1924 !s->progressive_sequence &&
1925 get_bits_left(&s->gb) <= 8 &&
1926 get_bits_left(&s->gb) >= 0 &&
1927 s->mb_skip_run == -1 &&
1928 show_bits(&s->gb, 8) == 0)
1931 ff_init_block_index(s);
1934 /* skip mb handling */
1935 if (s->mb_skip_run == -1) {
1936 /* read increment again */
1939 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1940 MBINCR_VLC_BITS, 2);
1942 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1943 return AVERROR_INVALIDDATA;
1947 s->mb_skip_run += 33;
1948 } else if (code == 35) {
1949 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1950 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1951 return AVERROR_INVALIDDATA;
1953 goto eos; /* end of slice */
1955 /* otherwise, stuffing, nothing to do */
1957 s->mb_skip_run += code;
1961 if (s->mb_skip_run) {
1963 if (s->pict_type == AV_PICTURE_TYPE_I) {
1964 av_log(s->avctx, AV_LOG_ERROR,
1965 "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1966 return AVERROR_INVALIDDATA;
1971 for (i = 0; i < 12; i++)
1972 s->block_last_index[i] = -1;
1973 if (s->picture_structure == PICT_FRAME)
1974 s->mv_type = MV_TYPE_16X16;
1976 s->mv_type = MV_TYPE_FIELD;
1977 if (s->pict_type == AV_PICTURE_TYPE_P) {
1978 /* if P type, zero motion vector is implied */
1979 s->mv_dir = MV_DIR_FORWARD;
1980 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1981 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1982 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1983 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1985 /* if B type, reuse previous vectors and directions */
1986 s->mv[0][0][0] = s->last_mv[0][0][0];
1987 s->mv[0][0][1] = s->last_mv[0][0][1];
1988 s->mv[1][0][0] = s->last_mv[1][0][0];
1989 s->mv[1][0][1] = s->last_mv[1][0][1];
1994 eos: // end of slice
1995 if (get_bits_left(&s->gb) < 0) {
1996 av_log(s, AV_LOG_ERROR, "overread %d\n", -get_bits_left(&s->gb));
1997 return AVERROR_INVALIDDATA;
1999 *buf += (get_bits_count(&s->gb) - 1) / 8;
2000 ff_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
2004 static int slice_decode_thread(AVCodecContext *c, void *arg)
2006 MpegEncContext *s = *(void **) arg;
2007 const uint8_t *buf = s->gb.buffer;
2008 int mb_y = s->start_mb_y;
2009 const int field_pic = s->picture_structure != PICT_FRAME;
2011 s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
2014 uint32_t start_code;
2017 ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
2019 ff_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
2020 ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
2021 s->start_mb_y, s->end_mb_y, s->er.error_count);
2023 if (c->err_recognition & AV_EF_EXPLODE)
2025 if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
2026 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
2028 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
2030 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
2031 s->mb_x - 1, s->mb_y,
2032 ER_AC_END | ER_DC_END | ER_MV_END);
2035 if (s->mb_y == s->end_mb_y)
2039 buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
2040 mb_y = start_code - SLICE_MIN_START_CODE;
2041 if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
2042 mb_y += (*buf&0xE0)<<2;
2044 if (s->picture_structure == PICT_BOTTOM_FIELD)
2046 if (mb_y < 0 || mb_y >= s->end_mb_y)
2047 return AVERROR_INVALIDDATA;
2052 * Handle slice ends.
2053 * @return 1 if it seems to be the last slice
2055 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
2057 Mpeg1Context *s1 = avctx->priv_data;
2058 MpegEncContext *s = &s1->mpeg_enc_ctx;
2060 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
2063 if (s->avctx->hwaccel) {
2064 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
2065 av_log(avctx, AV_LOG_ERROR,
2066 "hardware accelerator failed to decode picture\n");
2069 /* end of slice reached */
2070 if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field && !s1->first_slice) {
2073 ff_er_frame_end(&s->er);
2075 ff_mpv_frame_end(s);
2077 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
2078 int ret = av_frame_ref(pict, s->current_picture_ptr->f);
2081 ff_print_debug_info(s, s->current_picture_ptr, pict);
2082 ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG2);
2084 if (avctx->active_thread_type & FF_THREAD_FRAME)
2085 s->picture_number++;
2086 /* latency of 1 frame for I- and P-frames */
2087 /* XXX: use another variable than picture_number */
2088 if (s->last_picture_ptr) {
2089 int ret = av_frame_ref(pict, s->last_picture_ptr->f);
2092 ff_print_debug_info(s, s->last_picture_ptr, pict);
2093 ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG2);
2103 static int mpeg1_decode_sequence(AVCodecContext *avctx,
2104 const uint8_t *buf, int buf_size)
2106 Mpeg1Context *s1 = avctx->priv_data;
2107 MpegEncContext *s = &s1->mpeg_enc_ctx;
2111 init_get_bits(&s->gb, buf, buf_size * 8);
2113 width = get_bits(&s->gb, 12);
2114 height = get_bits(&s->gb, 12);
2115 if (width == 0 || height == 0) {
2116 av_log(avctx, AV_LOG_WARNING,
2117 "Invalid horizontal or vertical size value.\n");
2118 if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
2119 return AVERROR_INVALIDDATA;
2121 s->aspect_ratio_info = get_bits(&s->gb, 4);
2122 if (s->aspect_ratio_info == 0) {
2123 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
2124 if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
2125 return AVERROR_INVALIDDATA;
2127 s->frame_rate_index = get_bits(&s->gb, 4);
2128 if (s->frame_rate_index == 0 || s->frame_rate_index > 13) {
2129 av_log(avctx, AV_LOG_WARNING,
2130 "frame_rate_index %d is invalid\n", s->frame_rate_index);
2131 s->frame_rate_index = 1;
2133 s->bit_rate = get_bits(&s->gb, 18) * 400;
2134 if (check_marker(&s->gb, "in sequence header") == 0) {
2135 return AVERROR_INVALIDDATA;
2140 s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
2141 skip_bits(&s->gb, 1);
2144 if (get_bits1(&s->gb)) {
2145 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
2147 for (i = 0; i < 64; i++) {
2148 j = s->idsp.idct_permutation[i];
2149 v = ff_mpeg1_default_intra_matrix[i];
2150 s->intra_matrix[j] = v;
2151 s->chroma_intra_matrix[j] = v;
2154 if (get_bits1(&s->gb)) {
2155 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
2157 for (i = 0; i < 64; i++) {
2158 int j = s->idsp.idct_permutation[i];
2159 v = ff_mpeg1_default_non_intra_matrix[i];
2160 s->inter_matrix[j] = v;
2161 s->chroma_inter_matrix[j] = v;
2165 if (show_bits(&s->gb, 23) != 0) {
2166 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2167 return AVERROR_INVALIDDATA;
2170 /* We set MPEG-2 parameters so that it emulates MPEG-1. */
2171 s->progressive_sequence = 1;
2172 s->progressive_frame = 1;
2173 s->picture_structure = PICT_FRAME;
2175 s->frame_pred_frame_dct = 1;
2176 s->chroma_format = 1;
2178 s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
2179 s->out_format = FMT_MPEG1;
2180 s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
2181 if (s->flags & CODEC_FLAG_LOW_DELAY)
2184 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2185 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d, aspect_ratio_info: %d \n",
2186 s->avctx->rc_buffer_size, s->bit_rate, s->aspect_ratio_info);
2191 static int vcr2_init_sequence(AVCodecContext *avctx)
2193 Mpeg1Context *s1 = avctx->priv_data;
2194 MpegEncContext *s = &s1->mpeg_enc_ctx;
2197 /* start new MPEG-1 context decoding */
2198 s->out_format = FMT_MPEG1;
2199 if (s1->mpeg_enc_ctx_allocated) {
2200 ff_mpv_common_end(s);
2201 s1->mpeg_enc_ctx_allocated = 0;
2203 s->width = avctx->coded_width;
2204 s->height = avctx->coded_height;
2205 avctx->has_b_frames = 0; // true?
2208 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2209 setup_hwaccel_for_pixfmt(avctx);
2211 ff_mpv_idct_init(s);
2212 if ((ret = ff_mpv_common_init(s)) < 0)
2214 s1->mpeg_enc_ctx_allocated = 1;
2216 for (i = 0; i < 64; i++) {
2217 int j = s->idsp.idct_permutation[i];
2218 v = ff_mpeg1_default_intra_matrix[i];
2219 s->intra_matrix[j] = v;
2220 s->chroma_intra_matrix[j] = v;
2222 v = ff_mpeg1_default_non_intra_matrix[i];
2223 s->inter_matrix[j] = v;
2224 s->chroma_inter_matrix[j] = v;
2227 s->progressive_sequence = 1;
2228 s->progressive_frame = 1;
2229 s->picture_structure = PICT_FRAME;
2231 s->frame_pred_frame_dct = 1;
2232 s->chroma_format = 1;
2233 if (s->codec_tag == AV_RL32("BW10")) {
2234 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
2236 s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
2237 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
2239 s1->save_width = s->width;
2240 s1->save_height = s->height;
2241 s1->save_progressive_seq = s->progressive_sequence;
2245 static int mpeg_decode_a53_cc(AVCodecContext *avctx,
2246 const uint8_t *p, int buf_size)
2248 Mpeg1Context *s1 = avctx->priv_data;
2250 if (buf_size >= 6 &&
2251 p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
2252 p[4] == 3 && (p[5] & 0x40)) {
2253 /* extract A53 Part 4 CC data */
2254 int cc_count = p[5] & 0x1f;
2255 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2256 av_freep(&s1->a53_caption);
2257 s1->a53_caption_size = cc_count * 3;
2258 s1->a53_caption = av_malloc(s1->a53_caption_size);
2259 if (s1->a53_caption)
2260 memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
2263 } else if (buf_size >= 11 &&
2264 p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
2265 /* extract DVD CC data */
2268 // There is a caption count field in the data, but it is often
2269 // incorect. So count the number of captions present.
2270 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2272 // Transform the DVD format into A53 Part 4 format
2274 av_freep(&s1->a53_caption);
2275 s1->a53_caption_size = cc_count * 6;
2276 s1->a53_caption = av_malloc(s1->a53_caption_size);
2277 if (s1->a53_caption) {
2278 uint8_t field1 = !!(p[4] & 0x80);
2279 uint8_t *cap = s1->a53_caption;
2281 for (i = 0; i < cc_count; i++) {
2282 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2285 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2298 static void mpeg_decode_user_data(AVCodecContext *avctx,
2299 const uint8_t *p, int buf_size)
2301 Mpeg1Context *s = avctx->priv_data;
2302 const uint8_t *buf_end = p + buf_size;
2303 Mpeg1Context *s1 = avctx->priv_data;
2307 for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
2308 av_log(avctx, AV_LOG_ERROR, "%c", p[i]);
2310 av_log(avctx, AV_LOG_ERROR, "\n");
2316 if (!memcmp(p+i, "\0TMPGEXS\0", 9)){
2320 /* we parse the DTG active format information */
2321 if (buf_end - p >= 5 &&
2322 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2330 if (buf_end - p < 1)
2333 FF_DISABLE_DEPRECATION_WARNINGS
2334 avctx->dtg_active_format = p[0] & 0x0f;
2335 FF_ENABLE_DEPRECATION_WARNINGS
2336 #endif /* FF_API_AFD */
2338 s1->afd = p[0] & 0x0f;
2340 } else if (buf_end - p >= 6 &&
2341 p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
2342 p[4] == 0x03) { // S3D_video_format_length
2343 // the 0x7F mask ignores the reserved_bit value
2344 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2346 if (S3D_video_format_type == 0x03 ||
2347 S3D_video_format_type == 0x04 ||
2348 S3D_video_format_type == 0x08 ||
2349 S3D_video_format_type == 0x23) {
2351 s1->has_stereo3d = 1;
2353 switch (S3D_video_format_type) {
2355 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
2358 s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
2361 s1->stereo3d.type = AV_STEREO3D_2D;
2364 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2368 } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
2373 static void mpeg_decode_gop(AVCodecContext *avctx,
2374 const uint8_t *buf, int buf_size)
2376 Mpeg1Context *s1 = avctx->priv_data;
2377 MpegEncContext *s = &s1->mpeg_enc_ctx;
2381 init_get_bits(&s->gb, buf, buf_size * 8);
2383 tc = avctx->timecode_frame_start = get_bits(&s->gb, 25);
2385 s->closed_gop = get_bits1(&s->gb);
2386 /* broken_link indicate that after editing the
2387 * reference frames of the first B-Frames after GOP I-Frame
2388 * are missing (open gop) */
2389 broken_link = get_bits1(&s->gb);
2391 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2392 char tcbuf[AV_TIMECODE_STR_SIZE];
2393 av_timecode_make_mpeg_tc_string(tcbuf, tc);
2394 av_log(s->avctx, AV_LOG_DEBUG,
2395 "GOP (%s) closed_gop=%d broken_link=%d\n",
2396 tcbuf, s->closed_gop, broken_link);
2400 static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
2401 int *got_output, const uint8_t *buf, int buf_size)
2403 Mpeg1Context *s = avctx->priv_data;
2404 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2405 const uint8_t *buf_ptr = buf;
2406 const uint8_t *buf_end = buf + buf_size;
2407 int ret, input_size;
2408 int last_code = 0, skip_frame = 0;
2409 int picture_start_code_seen = 0;
2412 /* find next start code */
2413 uint32_t start_code = -1;
2414 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
2415 if (start_code > 0x1ff) {
2418 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2421 av_assert0(avctx->thread_count > 1);
2423 avctx->execute(avctx, slice_decode_thread,
2424 &s2->thread_context[0], NULL,
2425 s->slice_count, sizeof(void *));
2426 for (i = 0; i < s->slice_count; i++)
2427 s2->er.error_count += s2->thread_context[i]->er.error_count;
2430 if ((CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER)
2431 && uses_vdpau(avctx))
2432 ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
2434 ret = slice_end(avctx, picture);
2438 // FIXME: merge with the stuff in mpeg_decode_slice
2439 if (s2->last_picture_ptr || s2->low_delay)
2445 if (avctx->err_recognition & AV_EF_EXPLODE && s2->er.error_count)
2446 return AVERROR_INVALIDDATA;
2448 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2451 input_size = buf_end - buf_ptr;
2453 if (avctx->debug & FF_DEBUG_STARTCODE)
2454 av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %"PTRDIFF_SPECIFIER" left %d\n",
2455 start_code, buf_ptr - buf, input_size);
2457 /* prepare data for next start code */
2458 switch (start_code) {
2459 case SEQ_START_CODE:
2460 if (last_code == 0) {
2461 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2462 if (buf != avctx->extradata)
2465 av_log(avctx, AV_LOG_ERROR,
2466 "ignoring SEQ_START_CODE after %X\n", last_code);
2467 if (avctx->err_recognition & AV_EF_EXPLODE)
2468 return AVERROR_INVALIDDATA;
2472 case PICTURE_START_CODE:
2473 if (picture_start_code_seen && s2->picture_structure == PICT_FRAME) {
2474 /* If it's a frame picture, there can't be more than one picture header.
2475 Yet, it does happen and we need to handle it. */
2476 av_log(avctx, AV_LOG_WARNING, "ignoring extra picture following a frame-picture\n");
2479 picture_start_code_seen = 1;
2481 if (s2->width <= 0 || s2->height <= 0) {
2482 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
2483 s2->width, s2->height);
2484 return AVERROR_INVALIDDATA;
2488 s2->intra_dc_precision= 3;
2489 s2->intra_matrix[0]= 1;
2491 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
2492 !avctx->hwaccel && s->slice_count) {
2495 avctx->execute(avctx, slice_decode_thread,
2496 s2->thread_context, NULL,
2497 s->slice_count, sizeof(void *));
2498 for (i = 0; i < s->slice_count; i++)
2499 s2->er.error_count += s2->thread_context[i]->er.error_count;
2502 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2503 ret = mpeg_decode_postinit(avctx);
2505 av_log(avctx, AV_LOG_ERROR,
2506 "mpeg_decode_postinit() failure\n");
2510 /* We have a complete image: we try to decompress it. */
2511 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2514 last_code = PICTURE_START_CODE;
2516 av_log(avctx, AV_LOG_ERROR,
2517 "ignoring pic after %X\n", last_code);
2518 if (avctx->err_recognition & AV_EF_EXPLODE)
2519 return AVERROR_INVALIDDATA;
2522 case EXT_START_CODE:
2523 init_get_bits(&s2->gb, buf_ptr, input_size * 8);
2525 switch (get_bits(&s2->gb, 4)) {
2527 if (last_code == 0) {
2528 mpeg_decode_sequence_extension(s);
2530 av_log(avctx, AV_LOG_ERROR,
2531 "ignoring seq ext after %X\n", last_code);
2532 if (avctx->err_recognition & AV_EF_EXPLODE)
2533 return AVERROR_INVALIDDATA;
2537 mpeg_decode_sequence_display_extension(s);
2540 mpeg_decode_quant_matrix_extension(s2);
2543 mpeg_decode_picture_display_extension(s);
2546 if (last_code == PICTURE_START_CODE) {
2547 mpeg_decode_picture_coding_extension(s);
2549 av_log(avctx, AV_LOG_ERROR,
2550 "ignoring pic cod ext after %X\n", last_code);
2551 if (avctx->err_recognition & AV_EF_EXPLODE)
2552 return AVERROR_INVALIDDATA;
2557 case USER_START_CODE:
2558 mpeg_decode_user_data(avctx, buf_ptr, input_size);
2560 case GOP_START_CODE:
2561 if (last_code == 0) {
2562 s2->first_field = 0;
2563 mpeg_decode_gop(avctx, buf_ptr, input_size);
2566 av_log(avctx, AV_LOG_ERROR,
2567 "ignoring GOP_START_CODE after %X\n", last_code);
2568 if (avctx->err_recognition & AV_EF_EXPLODE)
2569 return AVERROR_INVALIDDATA;
2573 if (start_code >= SLICE_MIN_START_CODE &&
2574 start_code <= SLICE_MAX_START_CODE && last_code == PICTURE_START_CODE) {
2575 if (s2->progressive_sequence && !s2->progressive_frame) {
2576 s2->progressive_frame = 1;
2577 av_log(s2->avctx, AV_LOG_ERROR,
2578 "interlaced frame in progressive sequence, ignoring\n");
2581 if (s2->picture_structure == 0 ||
2582 (s2->progressive_frame && s2->picture_structure != PICT_FRAME)) {
2583 av_log(s2->avctx, AV_LOG_ERROR,
2584 "picture_structure %d invalid, ignoring\n",
2585 s2->picture_structure);
2586 s2->picture_structure = PICT_FRAME;
2589 if (s2->progressive_sequence && !s2->frame_pred_frame_dct)
2590 av_log(s2->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
2592 if (s2->picture_structure == PICT_FRAME) {
2593 s2->first_field = 0;
2594 s2->v_edge_pos = 16 * s2->mb_height;
2596 s2->first_field ^= 1;
2597 s2->v_edge_pos = 8 * s2->mb_height;
2598 memset(s2->mbskip_table, 0, s2->mb_stride * s2->mb_height);
2601 if (start_code >= SLICE_MIN_START_CODE &&
2602 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2603 const int field_pic = s2->picture_structure != PICT_FRAME;
2604 int mb_y = start_code - SLICE_MIN_START_CODE;
2605 last_code = SLICE_MIN_START_CODE;
2606 if (s2->codec_id != AV_CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
2607 mb_y += (*buf_ptr&0xE0)<<2;
2610 if (s2->picture_structure == PICT_BOTTOM_FIELD)
2613 if (buf_end - buf_ptr < 2) {
2614 av_log(s2->avctx, AV_LOG_ERROR, "slice too small\n");
2615 return AVERROR_INVALIDDATA;
2618 if (mb_y >= s2->mb_height) {
2619 av_log(s2->avctx, AV_LOG_ERROR,
2620 "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2621 return AVERROR_INVALIDDATA;
2624 if (!s2->last_picture_ptr) {
2625 /* Skip B-frames if we do not have reference frames and
2626 * GOP is not closed. */
2627 if (s2->pict_type == AV_PICTURE_TYPE_B) {
2628 if (!s2->closed_gop) {
2634 if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->flags2 & CODEC_FLAG2_SHOW_ALL))
2636 if (!s2->next_picture_ptr) {
2637 /* Skip P-frames if we do not have a reference frame or
2638 * we have an invalid header. */
2639 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
2644 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
2645 s2->pict_type == AV_PICTURE_TYPE_B) ||
2646 (avctx->skip_frame >= AVDISCARD_NONKEY &&
2647 s2->pict_type != AV_PICTURE_TYPE_I) ||
2648 avctx->skip_frame >= AVDISCARD_ALL) {
2653 if (!s->mpeg_enc_ctx_allocated)
2656 if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2657 if (mb_y < avctx->skip_top ||
2658 mb_y >= s2->mb_height - avctx->skip_bottom)
2662 if (!s2->pict_type) {
2663 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2664 if (avctx->err_recognition & AV_EF_EXPLODE)
2665 return AVERROR_INVALIDDATA;
2669 if (s->first_slice) {
2672 if ((ret = mpeg_field_start(s2, buf, buf_size)) < 0)
2675 if (!s2->current_picture_ptr) {
2676 av_log(avctx, AV_LOG_ERROR,
2677 "current_picture not initialized\n");
2678 return AVERROR_INVALIDDATA;
2681 if (uses_vdpau(avctx)) {
2687 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2689 int threshold = (s2->mb_height * s->slice_count +
2690 s2->slice_context_count / 2) /
2691 s2->slice_context_count;
2692 av_assert0(avctx->thread_count > 1);
2693 if (threshold <= mb_y) {
2694 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2696 thread_context->start_mb_y = mb_y;
2697 thread_context->end_mb_y = s2->mb_height;
2698 if (s->slice_count) {
2699 s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
2700 ret = ff_update_duplicate_context(thread_context, s2);
2704 init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
2707 buf_ptr += 2; // FIXME add minimum number of bytes per slice
2709 ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2713 if (avctx->err_recognition & AV_EF_EXPLODE)
2715 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2716 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2717 s2->resync_mb_y, s2->mb_x, s2->mb_y,
2718 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
2720 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2721 s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
2722 ER_AC_END | ER_DC_END | ER_MV_END);
2731 static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
2732 int *got_output, AVPacket *avpkt)
2734 const uint8_t *buf = avpkt->data;
2736 int buf_size = avpkt->size;
2737 Mpeg1Context *s = avctx->priv_data;
2738 AVFrame *picture = data;
2739 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2741 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2742 /* special case for last picture */
2743 if (s2->low_delay == 0 && s2->next_picture_ptr) {
2744 int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
2748 s2->next_picture_ptr = NULL;
2755 if (s2->flags & CODEC_FLAG_TRUNCATED) {
2756 int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
2759 if (ff_combine_frame(&s2->parse_context, next,
2760 (const uint8_t **) &buf, &buf_size) < 0)
2764 s2->codec_tag = avpriv_toupper4(avctx->codec_tag);
2765 if (s->mpeg_enc_ctx_allocated == 0 && ( s2->codec_tag == AV_RL32("VCR2")
2766 || s2->codec_tag == AV_RL32("BW10")
2768 vcr2_init_sequence(avctx);
2772 if (avctx->extradata && !s->extradata_decoded) {
2773 ret = decode_chunks(avctx, picture, got_output,
2774 avctx->extradata, avctx->extradata_size);
2776 av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
2779 s->extradata_decoded = 1;
2780 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) {
2781 s2->current_picture_ptr = NULL;
2786 ret = decode_chunks(avctx, picture, got_output, buf, buf_size);
2787 if (ret<0 || *got_output)
2788 s2->current_picture_ptr = NULL;
2793 static void flush(AVCodecContext *avctx)
2795 Mpeg1Context *s = avctx->priv_data;
2799 ff_mpeg_flush(avctx);
2802 static av_cold int mpeg_decode_end(AVCodecContext *avctx)
2804 Mpeg1Context *s = avctx->priv_data;
2806 if (s->mpeg_enc_ctx_allocated)
2807 ff_mpv_common_end(&s->mpeg_enc_ctx);
2808 av_freep(&s->a53_caption);
2812 static const AVProfile mpeg2_video_profiles[] = {
2813 { FF_PROFILE_MPEG2_422, "4:2:2" },
2814 { FF_PROFILE_MPEG2_HIGH, "High" },
2815 { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2816 { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2817 { FF_PROFILE_MPEG2_MAIN, "Main" },
2818 { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2819 { FF_PROFILE_RESERVED, "Reserved" },
2820 { FF_PROFILE_RESERVED, "Reserved" },
2821 { FF_PROFILE_UNKNOWN },
2824 AVCodec ff_mpeg1video_decoder = {
2825 .name = "mpeg1video",
2826 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2827 .type = AVMEDIA_TYPE_VIDEO,
2828 .id = AV_CODEC_ID_MPEG1VIDEO,
2829 .priv_data_size = sizeof(Mpeg1Context),
2830 .init = mpeg_decode_init,
2831 .close = mpeg_decode_end,
2832 .decode = mpeg_decode_frame,
2833 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2834 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2835 CODEC_CAP_SLICE_THREADS,
2838 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2841 AVCodec ff_mpeg2video_decoder = {
2842 .name = "mpeg2video",
2843 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2844 .type = AVMEDIA_TYPE_VIDEO,
2845 .id = AV_CODEC_ID_MPEG2VIDEO,
2846 .priv_data_size = sizeof(Mpeg1Context),
2847 .init = mpeg_decode_init,
2848 .close = mpeg_decode_end,
2849 .decode = mpeg_decode_frame,
2850 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2851 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2852 CODEC_CAP_SLICE_THREADS,
2855 .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2859 AVCodec ff_mpegvideo_decoder = {
2860 .name = "mpegvideo",
2861 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2862 .type = AVMEDIA_TYPE_VIDEO,
2863 .id = AV_CODEC_ID_MPEG2VIDEO,
2864 .priv_data_size = sizeof(Mpeg1Context),
2865 .init = mpeg_decode_init,
2866 .close = mpeg_decode_end,
2867 .decode = mpeg_decode_frame,
2868 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
2874 #if CONFIG_MPEG_XVMC_DECODER
2875 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2877 if (avctx->active_thread_type & FF_THREAD_SLICE)
2879 if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2881 if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
2882 ff_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2884 mpeg_decode_init(avctx);
2886 avctx->pix_fmt = AV_PIX_FMT_XVMC_MPEG2_IDCT;
2887 avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2892 AVCodec ff_mpeg_xvmc_decoder = {
2893 .name = "mpegvideo_xvmc",
2894 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2895 .type = AVMEDIA_TYPE_VIDEO,
2896 .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2897 .priv_data_size = sizeof(Mpeg1Context),
2898 .init = mpeg_mc_decode_init,
2899 .close = mpeg_decode_end,
2900 .decode = mpeg_decode_frame,
2901 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2902 CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2907 #endif /* FF_API_XVMC */
2909 #if CONFIG_MPEG_VDPAU_DECODER
2910 AVCodec ff_mpeg_vdpau_decoder = {
2911 .name = "mpegvideo_vdpau",
2912 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
2913 .type = AVMEDIA_TYPE_VIDEO,
2914 .id = AV_CODEC_ID_MPEG2VIDEO,
2915 .priv_data_size = sizeof(Mpeg1Context),
2916 .init = mpeg_decode_init,
2917 .close = mpeg_decode_end,
2918 .decode = mpeg_decode_frame,
2919 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
2920 CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2925 #if CONFIG_MPEG1_VDPAU_DECODER
2926 AVCodec ff_mpeg1_vdpau_decoder = {
2927 .name = "mpeg1video_vdpau",
2928 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
2929 .type = AVMEDIA_TYPE_VIDEO,
2930 .id = AV_CODEC_ID_MPEG1VIDEO,
2931 .priv_data_size = sizeof(Mpeg1Context),
2932 .init = mpeg_decode_init,
2933 .close = mpeg_decode_end,
2934 .decode = mpeg_decode_frame,
2935 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
2936 CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,