3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "libavutil/attributes.h"
31 #include "libavutil/internal.h"
32 #include "libavutil/stereo3d.h"
35 #include "bytestream.h"
36 #include "error_resilience.h"
41 #include "mpeg12data.h"
42 #include "mpegutils.h"
43 #include "mpegvideo.h"
44 #include "mpegvideodata.h"
49 typedef struct Mpeg1Context {
50 MpegEncContext mpeg_enc_ctx;
51 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
52 int repeat_field; /* true if we must repeat the field */
53 AVPanScan pan_scan; /* some temporary storage for the panscan */
62 int save_width, save_height, save_progressive_seq;
63 AVRational frame_rate_ext; /* MPEG-2 specific framerate modificator */
64 int sync; /* Did we reach a sync point like a GOP/SEQ/KEYFrame? */
65 int closed_gop; /* GOP is closed */
67 int extradata_decoded;
70 #define MB_TYPE_ZERO_MV 0x20000000
72 static const uint32_t ptype2mb_type[7] = {
74 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
76 MB_TYPE_L0 | MB_TYPE_CBP,
77 MB_TYPE_QUANT | MB_TYPE_INTRA,
78 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
79 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
82 static const uint32_t btype2mb_type[11] = {
85 MB_TYPE_L1 | MB_TYPE_CBP,
87 MB_TYPE_L0 | MB_TYPE_CBP,
89 MB_TYPE_L0L1 | MB_TYPE_CBP,
90 MB_TYPE_QUANT | MB_TYPE_INTRA,
91 MB_TYPE_QUANT | MB_TYPE_L1 | MB_TYPE_CBP,
92 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
93 MB_TYPE_QUANT | MB_TYPE_L0L1 | MB_TYPE_CBP,
96 static const uint8_t non_linear_qscale[32] = {
97 0, 1, 2, 3, 4, 5, 6, 7,
98 8, 10, 12, 14, 16, 18, 20, 22,
99 24, 28, 32, 36, 40, 44, 48, 52,
100 56, 64, 72, 80, 88, 96, 104, 112,
103 /* as H.263, but only 17 codes */
104 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
106 int code, sign, val, shift;
108 code = get_vlc2(&s->gb, ff_mv_vlc.table, MV_VLC_BITS, 2);
114 sign = get_bits1(&s->gb);
118 val = (val - 1) << shift;
119 val |= get_bits(&s->gb, shift);
126 /* modulo decoding */
127 return sign_extend(val, 5 + shift);
130 #define MAX_INDEX (64 - 1)
131 #define check_scantable_index(ctx, x) \
133 if ((x) > MAX_INDEX) { \
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_inter(MpegEncContext *s,
141 int16_t *block, int n)
143 int level, i, j, run;
144 RLTable *rl = &ff_rl_mpeg1;
145 uint8_t *const scantable = s->intra_scantable.permutated;
146 const uint16_t *quant_matrix = s->inter_matrix;
147 const int qscale = s->qscale;
150 OPEN_READER(re, &s->gb);
152 // special case for first coefficient, no need to add second VLC table
153 UPDATE_CACHE(re, &s->gb);
154 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
155 level = (3 * qscale * quant_matrix[0]) >> 5;
156 level = (level - 1) | 1;
157 if (GET_CACHE(re, &s->gb) & 0x40000000)
161 SKIP_BITS(re, &s->gb, 2);
162 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
165 /* now quantify & encode AC coefficients */
167 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
175 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
176 level = (level - 1) | 1;
177 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
178 SHOW_SBITS(re, &s->gb, 1);
179 SKIP_BITS(re, &s->gb, 1);
182 run = SHOW_UBITS(re, &s->gb, 6) + 1;
183 LAST_SKIP_BITS(re, &s->gb, 6);
184 UPDATE_CACHE(re, &s->gb);
185 level = SHOW_SBITS(re, &s->gb, 8);
186 SKIP_BITS(re, &s->gb, 8);
188 level = SHOW_UBITS(re, &s->gb, 8) - 256;
189 SKIP_BITS(re, &s->gb, 8);
190 } else if (level == 0) {
191 level = SHOW_UBITS(re, &s->gb, 8);
192 SKIP_BITS(re, &s->gb, 8);
200 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
201 level = (level - 1) | 1;
204 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
205 level = (level - 1) | 1;
210 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
212 UPDATE_CACHE(re, &s->gb);
215 LAST_SKIP_BITS(re, &s->gb, 2);
216 CLOSE_READER(re, &s->gb);
219 check_scantable_index(s, i);
221 s->block_last_index[n] = i;
225 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s,
226 int16_t *block, int n)
228 int level, i, j, run;
229 RLTable *rl = &ff_rl_mpeg1;
230 uint8_t *const scantable = s->intra_scantable.permutated;
231 const int qscale = s->qscale;
234 OPEN_READER(re, &s->gb);
236 // Special case for first coefficient, no need to add second VLC table.
237 UPDATE_CACHE(re, &s->gb);
238 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
239 level = (3 * qscale) >> 1;
240 level = (level - 1) | 1;
241 if (GET_CACHE(re, &s->gb) & 0x40000000)
245 SKIP_BITS(re, &s->gb, 2);
246 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
250 /* now quantify & encode AC coefficients */
252 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
260 level = ((level * 2 + 1) * qscale) >> 1;
261 level = (level - 1) | 1;
262 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
263 SHOW_SBITS(re, &s->gb, 1);
264 SKIP_BITS(re, &s->gb, 1);
267 run = SHOW_UBITS(re, &s->gb, 6) + 1;
268 LAST_SKIP_BITS(re, &s->gb, 6);
269 UPDATE_CACHE(re, &s->gb);
270 level = SHOW_SBITS(re, &s->gb, 8);
271 SKIP_BITS(re, &s->gb, 8);
273 level = SHOW_UBITS(re, &s->gb, 8) - 256;
274 SKIP_BITS(re, &s->gb, 8);
275 } else if (level == 0) {
276 level = SHOW_UBITS(re, &s->gb, 8);
277 SKIP_BITS(re, &s->gb, 8);
285 level = ((level * 2 + 1) * qscale) >> 1;
286 level = (level - 1) | 1;
289 level = ((level * 2 + 1) * qscale) >> 1;
290 level = (level - 1) | 1;
295 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
297 UPDATE_CACHE(re, &s->gb);
300 LAST_SKIP_BITS(re, &s->gb, 2);
301 CLOSE_READER(re, &s->gb);
304 check_scantable_index(s, i);
306 s->block_last_index[n] = i;
310 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
311 int16_t *block, int n)
313 int level, i, j, run;
314 RLTable *rl = &ff_rl_mpeg1;
315 uint8_t *const scantable = s->intra_scantable.permutated;
316 const uint16_t *quant_matrix;
317 const int qscale = s->qscale;
323 OPEN_READER(re, &s->gb);
326 quant_matrix = s->inter_matrix;
328 quant_matrix = s->chroma_inter_matrix;
330 // Special case for first coefficient, no need to add second VLC table.
331 UPDATE_CACHE(re, &s->gb);
332 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
333 level = (3 * qscale * quant_matrix[0]) >> 5;
334 if (GET_CACHE(re, &s->gb) & 0x40000000)
339 SKIP_BITS(re, &s->gb, 2);
340 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
344 /* now quantify & encode AC coefficients */
346 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
354 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
355 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
356 SHOW_SBITS(re, &s->gb, 1);
357 SKIP_BITS(re, &s->gb, 1);
360 run = SHOW_UBITS(re, &s->gb, 6) + 1;
361 LAST_SKIP_BITS(re, &s->gb, 6);
362 UPDATE_CACHE(re, &s->gb);
363 level = SHOW_SBITS(re, &s->gb, 12);
364 SKIP_BITS(re, &s->gb, 12);
371 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
374 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
380 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
382 UPDATE_CACHE(re, &s->gb);
385 LAST_SKIP_BITS(re, &s->gb, 2);
386 CLOSE_READER(re, &s->gb);
388 block[63] ^= (mismatch & 1);
390 check_scantable_index(s, i);
392 s->block_last_index[n] = i;
396 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
397 int16_t *block, int n)
399 int level, i, j, run;
400 RLTable *rl = &ff_rl_mpeg1;
401 uint8_t *const scantable = s->intra_scantable.permutated;
402 const int qscale = s->qscale;
403 OPEN_READER(re, &s->gb);
406 // special case for first coefficient, no need to add second VLC table
407 UPDATE_CACHE(re, &s->gb);
408 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
409 level = (3 * qscale) >> 1;
410 if (GET_CACHE(re, &s->gb) & 0x40000000)
414 SKIP_BITS(re, &s->gb, 2);
415 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
419 /* now quantify & encode AC coefficients */
421 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
428 level = ((level * 2 + 1) * qscale) >> 1;
429 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
430 SHOW_SBITS(re, &s->gb, 1);
431 SKIP_BITS(re, &s->gb, 1);
434 run = SHOW_UBITS(re, &s->gb, 6) + 1;
435 LAST_SKIP_BITS(re, &s->gb, 6);
436 UPDATE_CACHE(re, &s->gb);
437 level = SHOW_SBITS(re, &s->gb, 12);
438 SKIP_BITS(re, &s->gb, 12);
445 level = ((-level * 2 + 1) * qscale) >> 1;
448 level = ((level * 2 + 1) * qscale) >> 1;
453 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
455 UPDATE_CACHE(re, &s->gb);
458 LAST_SKIP_BITS(re, &s->gb, 2);
459 CLOSE_READER(re, &s->gb);
461 check_scantable_index(s, i);
463 s->block_last_index[n] = i;
467 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
468 int16_t *block, int n)
470 int level, dc, diff, i, j, run;
473 uint8_t *const scantable = s->intra_scantable.permutated;
474 const uint16_t *quant_matrix;
475 const int qscale = s->qscale;
480 quant_matrix = s->intra_matrix;
483 quant_matrix = s->chroma_intra_matrix;
484 component = (n & 1) + 1;
486 diff = decode_dc(&s->gb, component);
488 return AVERROR_INVALIDDATA;
489 dc = s->last_dc[component];
491 s->last_dc[component] = dc;
492 block[0] = dc << (3 - s->intra_dc_precision);
493 ff_dlog(s->avctx, "dc=%d\n", block[0]);
494 mismatch = block[0] ^ 1;
496 if (s->intra_vlc_format)
502 OPEN_READER(re, &s->gb);
503 /* now quantify & encode AC coefficients */
505 UPDATE_CACHE(re, &s->gb);
506 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
511 } else if (level != 0) {
516 level = (level * qscale * quant_matrix[j]) >> 4;
517 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
518 SHOW_SBITS(re, &s->gb, 1);
519 LAST_SKIP_BITS(re, &s->gb, 1);
522 run = SHOW_UBITS(re, &s->gb, 6) + 1;
523 LAST_SKIP_BITS(re, &s->gb, 6);
524 UPDATE_CACHE(re, &s->gb);
525 level = SHOW_SBITS(re, &s->gb, 12);
526 SKIP_BITS(re, &s->gb, 12);
532 level = (-level * qscale * quant_matrix[j]) >> 4;
535 level = (level * qscale * quant_matrix[j]) >> 4;
542 CLOSE_READER(re, &s->gb);
544 block[63] ^= mismatch & 1;
546 check_scantable_index(s, i);
548 s->block_last_index[n] = i;
552 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
553 int16_t *block, int n)
555 int level, dc, diff, i, j, run;
558 uint8_t *const scantable = s->intra_scantable.permutated;
559 const uint16_t *quant_matrix;
560 const int qscale = s->qscale;
564 quant_matrix = s->intra_matrix;
567 quant_matrix = s->chroma_intra_matrix;
568 component = (n & 1) + 1;
570 diff = decode_dc(&s->gb, component);
572 return AVERROR_INVALIDDATA;
573 dc = s->last_dc[component];
575 s->last_dc[component] = dc;
576 block[0] = dc << (3 - s->intra_dc_precision);
578 if (s->intra_vlc_format)
584 OPEN_READER(re, &s->gb);
585 /* now quantify & encode AC coefficients */
587 UPDATE_CACHE(re, &s->gb);
588 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
593 } else if (level != 0) {
598 level = (level * qscale * quant_matrix[j]) >> 4;
599 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
600 SHOW_SBITS(re, &s->gb, 1);
601 LAST_SKIP_BITS(re, &s->gb, 1);
604 run = SHOW_UBITS(re, &s->gb, 6) + 1;
605 LAST_SKIP_BITS(re, &s->gb, 6);
606 UPDATE_CACHE(re, &s->gb);
607 level = SHOW_SBITS(re, &s->gb, 12);
608 SKIP_BITS(re, &s->gb, 12);
614 level = (-level * qscale * quant_matrix[j]) >> 4;
617 level = (level * qscale * quant_matrix[j]) >> 4;
623 CLOSE_READER(re, &s->gb);
626 check_scantable_index(s, i);
628 s->block_last_index[n] = i;
632 /******************************************/
635 static inline int get_dmv(MpegEncContext *s)
637 if (get_bits1(&s->gb))
638 return 1 - (get_bits1(&s->gb) << 1);
643 static inline int get_qscale(MpegEncContext *s)
645 int qscale = get_bits(&s->gb, 5);
647 return non_linear_qscale[qscale];
652 /* motion type (for MPEG-2) */
658 static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
660 int i, j, k, cbp, val, mb_type, motion_type;
661 const int mb_block_count = 4 + (1 << s->chroma_format);
664 ff_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
666 assert(s->mb_skipped == 0);
668 if (s->mb_skip_run-- != 0) {
669 if (s->pict_type == AV_PICTURE_TYPE_P) {
671 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
672 MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
677 mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
679 // FIXME not sure if this is allowed in MPEG at all
680 mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
681 if (IS_INTRA(mb_type))
682 return AVERROR_INVALIDDATA;
683 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
684 mb_type | MB_TYPE_SKIP;
685 // assert(s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1] & (MB_TYPE_16x16 | MB_TYPE_16x8));
687 if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
694 switch (s->pict_type) {
696 case AV_PICTURE_TYPE_I:
697 if (get_bits1(&s->gb) == 0) {
698 if (get_bits1(&s->gb) == 0) {
699 av_log(s->avctx, AV_LOG_ERROR,
700 "Invalid mb type in I-frame at %d %d\n",
702 return AVERROR_INVALIDDATA;
704 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
706 mb_type = MB_TYPE_INTRA;
709 case AV_PICTURE_TYPE_P:
710 mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
712 av_log(s->avctx, AV_LOG_ERROR,
713 "Invalid mb type in P-frame at %d %d\n", s->mb_x, s->mb_y);
714 return AVERROR_INVALIDDATA;
716 mb_type = ptype2mb_type[mb_type];
718 case AV_PICTURE_TYPE_B:
719 mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
721 av_log(s->avctx, AV_LOG_ERROR,
722 "Invalid mb type in B-frame at %d %d\n", s->mb_x, s->mb_y);
723 return AVERROR_INVALIDDATA;
725 mb_type = btype2mb_type[mb_type];
728 ff_dlog(s->avctx, "mb_type=%x\n", mb_type);
729 // motion_type = 0; /* avoid warning */
730 if (IS_INTRA(mb_type)) {
731 s->bdsp.clear_blocks(s->block[0]);
733 if (!s->chroma_y_shift)
734 s->bdsp.clear_blocks(s->block[6]);
736 /* compute DCT type */
737 // FIXME: add an interlaced_dct coded var?
738 if (s->picture_structure == PICT_FRAME &&
739 !s->frame_pred_frame_dct)
740 s->interlaced_dct = get_bits1(&s->gb);
742 if (IS_QUANT(mb_type))
743 s->qscale = get_qscale(s);
745 if (s->concealment_motion_vectors) {
746 /* just parse them */
747 if (s->picture_structure != PICT_FRAME)
748 skip_bits1(&s->gb); /* field select */
751 s->last_mv[0][0][0] =
752 s->last_mv[0][1][0] = mpeg_decode_motion(s, s->mpeg_f_code[0][0],
753 s->last_mv[0][0][0]);
755 s->last_mv[0][0][1] =
756 s->last_mv[0][1][1] = mpeg_decode_motion(s, s->mpeg_f_code[0][1],
757 s->last_mv[0][0][1]);
759 skip_bits1(&s->gb); /* marker */
761 /* reset mv prediction */
762 memset(s->last_mv, 0, sizeof(s->last_mv));
766 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
767 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);
771 for (i = 0; i < mb_block_count; i++)
772 if ((ret = mpeg2_decode_block_intra(s, *s->pblocks[i], i)) < 0)
776 for (i = 0; i < 6; i++) {
777 ret = ff_mpeg1_decode_block_intra(&s->gb,
779 s->intra_scantable.permutated,
780 s->last_dc, *s->pblocks[i],
783 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n",
788 s->block_last_index[i] = ret;
792 if (mb_type & MB_TYPE_ZERO_MV) {
793 assert(mb_type & MB_TYPE_CBP);
795 s->mv_dir = MV_DIR_FORWARD;
796 if (s->picture_structure == PICT_FRAME) {
797 if (!s->frame_pred_frame_dct)
798 s->interlaced_dct = get_bits1(&s->gb);
799 s->mv_type = MV_TYPE_16X16;
801 s->mv_type = MV_TYPE_FIELD;
802 mb_type |= MB_TYPE_INTERLACED;
803 s->field_select[0][0] = s->picture_structure - 1;
806 if (IS_QUANT(mb_type))
807 s->qscale = get_qscale(s);
809 s->last_mv[0][0][0] = 0;
810 s->last_mv[0][0][1] = 0;
811 s->last_mv[0][1][0] = 0;
812 s->last_mv[0][1][1] = 0;
816 assert(mb_type & MB_TYPE_L0L1);
817 // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
818 /* get additional motion vector type */
819 if (s->frame_pred_frame_dct) {
820 motion_type = MT_FRAME;
822 motion_type = get_bits(&s->gb, 2);
823 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
824 s->interlaced_dct = get_bits1(&s->gb);
827 if (IS_QUANT(mb_type))
828 s->qscale = get_qscale(s);
831 s->mv_dir = (mb_type >> 13) & 3;
832 ff_dlog(s->avctx, "motion_type=%d\n", motion_type);
833 switch (motion_type) {
834 case MT_FRAME: /* or MT_16X8 */
835 if (s->picture_structure == PICT_FRAME) {
836 mb_type |= MB_TYPE_16x16;
837 s->mv_type = MV_TYPE_16X16;
838 for (i = 0; i < 2; i++) {
839 if (USES_LIST(mb_type, i)) {
842 s->last_mv[i][0][0] =
843 s->last_mv[i][1][0] =
844 mpeg_decode_motion(s, s->mpeg_f_code[i][0],
845 s->last_mv[i][0][0]);
847 s->last_mv[i][0][1] =
848 s->last_mv[i][1][1] =
849 mpeg_decode_motion(s, s->mpeg_f_code[i][1],
850 s->last_mv[i][0][1]);
851 /* full_pel: only for MPEG-1 */
852 if (s->full_pel[i]) {
853 s->mv[i][0][0] <<= 1;
854 s->mv[i][0][1] <<= 1;
859 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
860 s->mv_type = MV_TYPE_16X8;
861 for (i = 0; i < 2; i++) {
862 if (USES_LIST(mb_type, i)) {
864 for (j = 0; j < 2; j++) {
865 s->field_select[i][j] = get_bits1(&s->gb);
866 for (k = 0; k < 2; k++) {
867 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
868 s->last_mv[i][j][k]);
869 s->last_mv[i][j][k] = val;
870 s->mv[i][j][k] = val;
878 s->mv_type = MV_TYPE_FIELD;
879 if (s->picture_structure == PICT_FRAME) {
880 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
881 for (i = 0; i < 2; i++) {
882 if (USES_LIST(mb_type, i)) {
883 for (j = 0; j < 2; j++) {
884 s->field_select[i][j] = get_bits1(&s->gb);
885 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
886 s->last_mv[i][j][0]);
887 s->last_mv[i][j][0] = val;
888 s->mv[i][j][0] = val;
889 ff_dlog(s->avctx, "fmx=%d\n", val);
890 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
891 s->last_mv[i][j][1] >> 1);
892 s->last_mv[i][j][1] = val << 1;
893 s->mv[i][j][1] = val;
894 ff_dlog(s->avctx, "fmy=%d\n", val);
899 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
900 for (i = 0; i < 2; i++) {
901 if (USES_LIST(mb_type, i)) {
902 s->field_select[i][0] = get_bits1(&s->gb);
903 for (k = 0; k < 2; k++) {
904 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
905 s->last_mv[i][0][k]);
906 s->last_mv[i][0][k] = val;
907 s->last_mv[i][1][k] = val;
908 s->mv[i][0][k] = val;
915 s->mv_type = MV_TYPE_DMV;
916 for (i = 0; i < 2; i++) {
917 if (USES_LIST(mb_type, i)) {
918 int dmx, dmy, mx, my, m;
919 const int my_shift = s->picture_structure == PICT_FRAME;
921 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
922 s->last_mv[i][0][0]);
923 s->last_mv[i][0][0] = mx;
924 s->last_mv[i][1][0] = mx;
926 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
927 s->last_mv[i][0][1] >> my_shift);
931 s->last_mv[i][0][1] = my << my_shift;
932 s->last_mv[i][1][1] = my << my_shift;
936 s->mv[i][1][0] = mx; // not used
937 s->mv[i][1][1] = my; // not used
939 if (s->picture_structure == PICT_FRAME) {
940 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
942 // m = 1 + 2 * s->top_field_first;
943 m = s->top_field_first ? 1 : 3;
945 /* top -> top pred */
946 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
947 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
949 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
950 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
952 mb_type |= MB_TYPE_16x16;
954 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
955 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
956 if (s->picture_structure == PICT_TOP_FIELD)
965 av_log(s->avctx, AV_LOG_ERROR,
966 "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
967 return AVERROR_INVALIDDATA;
972 if (HAS_CBP(mb_type)) {
973 s->bdsp.clear_blocks(s->block[0]);
975 cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
976 if (mb_block_count > 6) {
977 cbp <<= mb_block_count - 6;
978 cbp |= get_bits(&s->gb, mb_block_count - 6);
979 s->bdsp.clear_blocks(s->block[6]);
982 av_log(s->avctx, AV_LOG_ERROR,
983 "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
984 return AVERROR_INVALIDDATA;
987 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
988 if (s->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
989 for (i = 0; i < 6; i++) {
991 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
993 s->block_last_index[i] = -1;
997 cbp <<= 12 - mb_block_count;
999 for (i = 0; i < mb_block_count; i++) {
1000 if (cbp & (1 << 11)) {
1001 if ((ret = mpeg2_decode_block_non_intra(s, *s->pblocks[i], i)) < 0)
1004 s->block_last_index[i] = -1;
1010 if (s->avctx->flags2 & AV_CODEC_FLAG2_FAST) {
1011 for (i = 0; i < 6; i++) {
1013 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
1015 s->block_last_index[i] = -1;
1019 for (i = 0; i < 6; i++) {
1021 if ((ret = mpeg1_decode_block_inter(s, *s->pblocks[i], i)) < 0)
1024 s->block_last_index[i] = -1;
1031 for (i = 0; i < 12; i++)
1032 s->block_last_index[i] = -1;
1036 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
1041 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1043 Mpeg1Context *s = avctx->priv_data;
1044 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1046 ff_mpv_decode_defaults(s2);
1048 s->mpeg_enc_ctx.avctx = avctx;
1050 /* we need some permutation to store matrices,
1051 * until the decoder sets the real permutation. */
1052 ff_mpv_idct_init(s2);
1053 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1054 ff_mpeg12_init_vlcs();
1056 s->mpeg_enc_ctx_allocated = 0;
1057 s->mpeg_enc_ctx.picture_number = 0;
1058 s->repeat_field = 0;
1059 s->mpeg_enc_ctx.codec_id = avctx->codec->id;
1060 avctx->color_range = AVCOL_RANGE_MPEG;
1061 if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
1062 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1064 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1068 static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
1069 const AVCodecContext *avctx_from)
1071 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1072 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1075 if (avctx == avctx_from ||
1076 !ctx_from->mpeg_enc_ctx_allocated ||
1077 !s1->context_initialized)
1080 err = ff_mpeg_update_thread_context(avctx, avctx_from);
1084 if (!ctx->mpeg_enc_ctx_allocated) {
1085 // copy the whole context after the initial MpegEncContext structure
1086 memcpy(ctx, ctx_from, sizeof(*ctx));
1087 memset(&ctx->mpeg_enc_ctx, 0, sizeof(ctx->mpeg_enc_ctx));
1090 if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
1091 s->picture_number++;
1096 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1097 const uint8_t *new_perm)
1099 uint16_t temp_matrix[64];
1102 memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
1104 for (i = 0; i < 64; i++)
1105 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1108 static const enum AVPixelFormat mpeg12_hwaccel_pixfmt_list_420[] = {
1109 #if CONFIG_MPEG2_DXVA2_HWACCEL
1110 AV_PIX_FMT_DXVA2_VLD,
1112 #if CONFIG_MPEG2_D3D11VA_HWACCEL
1113 AV_PIX_FMT_D3D11VA_VLD,
1116 #if CONFIG_MPEG2_VAAPI_HWACCEL
1119 #if CONFIG_MPEG1_VDPAU_HWACCEL | CONFIG_MPEG2_VDPAU_HWACCEL
1126 static const enum AVPixelFormat mpeg12_pixfmt_list_422[] = {
1131 static const enum AVPixelFormat mpeg12_pixfmt_list_444[] = {
1136 static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
1138 Mpeg1Context *s1 = avctx->priv_data;
1139 MpegEncContext *s = &s1->mpeg_enc_ctx;
1140 const enum AVPixelFormat *pix_fmts;
1142 if (s->chroma_format < 2)
1143 pix_fmts = mpeg12_hwaccel_pixfmt_list_420;
1144 else if (s->chroma_format == 2)
1145 pix_fmts = mpeg12_pixfmt_list_422;
1147 pix_fmts = mpeg12_pixfmt_list_444;
1149 return ff_get_format(avctx, pix_fmts);
1152 /* Call this function when we know all parameters.
1153 * It may be called in different places for MPEG-1 and MPEG-2. */
1154 static int mpeg_decode_postinit(AVCodecContext *avctx)
1156 Mpeg1Context *s1 = avctx->priv_data;
1157 MpegEncContext *s = &s1->mpeg_enc_ctx;
1158 uint8_t old_permutation[64];
1161 if ((s1->mpeg_enc_ctx_allocated == 0) ||
1162 avctx->coded_width != s->width ||
1163 avctx->coded_height != s->height ||
1164 s1->save_width != s->width ||
1165 s1->save_height != s->height ||
1166 s1->save_aspect_info != s->aspect_ratio_info ||
1167 s1->save_progressive_seq != s->progressive_sequence ||
1169 if (s1->mpeg_enc_ctx_allocated) {
1170 ParseContext pc = s->parse_context;
1171 s->parse_context.buffer = 0;
1172 ff_mpv_common_end(s);
1173 s->parse_context = pc;
1176 ret = ff_set_dimensions(avctx, s->width, s->height);
1180 avctx->bit_rate = s->bit_rate;
1181 s1->save_aspect_info = s->aspect_ratio_info;
1182 s1->save_width = s->width;
1183 s1->save_height = s->height;
1184 s1->save_progressive_seq = s->progressive_sequence;
1186 /* low_delay may be forced, in this case we will have B-frames
1187 * that behave like P-frames. */
1188 avctx->has_b_frames = !s->low_delay;
1190 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1192 avctx->framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
1194 avctx->sample_aspect_ratio = av_d2q(1.0 / ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1195 avctx->ticks_per_frame = 1;
1198 av_reduce(&s->avctx->framerate.num,
1199 &s->avctx->framerate.den,
1200 ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num * 2,
1201 ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1203 avctx->ticks_per_frame = 2;
1205 if (s->aspect_ratio_info > 1) {
1207 av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1208 (AVRational) { s1->pan_scan.width,
1209 s1->pan_scan.height }),
1210 (AVRational) { s->width, s->height });
1212 /* We ignore the spec here and guess a bit as reality does not
1213 * match the spec, see for example res_change_ffmpeg_aspect.ts
1214 * and sequence-display-aspect.mpg.
1215 * issue1613, 621, 562 */
1216 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1217 (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
1218 av_cmp_q(dar, (AVRational) { 16, 9 }))) {
1219 s->avctx->sample_aspect_ratio =
1220 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1221 (AVRational) { s->width, s->height });
1223 s->avctx->sample_aspect_ratio =
1224 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1225 (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
1226 // issue1613 4/3 16/9 -> 16/9
1227 // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1228 // widescreen-issue562.mpg 4/3 16/9 -> 16/9
1229 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1230 ff_dlog(avctx, "A %d/%d\n",
1231 ff_mpeg2_aspect[s->aspect_ratio_info].num,
1232 ff_mpeg2_aspect[s->aspect_ratio_info].den);
1233 ff_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
1234 s->avctx->sample_aspect_ratio.den);
1237 s->avctx->sample_aspect_ratio =
1238 ff_mpeg2_aspect[s->aspect_ratio_info];
1242 ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
1244 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1245 // until then pix_fmt may be changed right after codec init
1246 if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
1247 avctx->idct_algo = FF_IDCT_SIMPLE;
1249 /* Quantization matrices may need reordering
1250 * if DCT permutation is changed. */
1251 memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
1253 ff_mpv_idct_init(s);
1254 if ((ret = ff_mpv_common_init(s)) < 0)
1257 quant_matrix_rebuild(s->intra_matrix, old_permutation, s->idsp.idct_permutation);
1258 quant_matrix_rebuild(s->inter_matrix, old_permutation, s->idsp.idct_permutation);
1259 quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
1260 quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
1262 s1->mpeg_enc_ctx_allocated = 1;
1267 static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
1270 Mpeg1Context *s1 = avctx->priv_data;
1271 MpegEncContext *s = &s1->mpeg_enc_ctx;
1272 int ref, f_code, vbv_delay;
1274 init_get_bits(&s->gb, buf, buf_size * 8);
1276 ref = get_bits(&s->gb, 10); /* temporal ref */
1277 s->pict_type = get_bits(&s->gb, 3);
1278 if (s->pict_type == 0 || s->pict_type > 3)
1279 return AVERROR_INVALIDDATA;
1281 vbv_delay = get_bits(&s->gb, 16);
1282 if (s->pict_type == AV_PICTURE_TYPE_P ||
1283 s->pict_type == AV_PICTURE_TYPE_B) {
1284 s->full_pel[0] = get_bits1(&s->gb);
1285 f_code = get_bits(&s->gb, 3);
1286 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1287 return AVERROR_INVALIDDATA;
1288 s->mpeg_f_code[0][0] = f_code;
1289 s->mpeg_f_code[0][1] = f_code;
1291 if (s->pict_type == AV_PICTURE_TYPE_B) {
1292 s->full_pel[1] = get_bits1(&s->gb);
1293 f_code = get_bits(&s->gb, 3);
1294 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1295 return AVERROR_INVALIDDATA;
1296 s->mpeg_f_code[1][0] = f_code;
1297 s->mpeg_f_code[1][1] = f_code;
1299 s->current_picture.f->pict_type = s->pict_type;
1300 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1302 if (avctx->debug & FF_DEBUG_PICT_INFO)
1303 av_log(avctx, AV_LOG_DEBUG,
1304 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1311 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1313 MpegEncContext *s = &s1->mpeg_enc_ctx;
1314 int horiz_size_ext, vert_size_ext;
1317 skip_bits(&s->gb, 1); /* profile and level esc*/
1318 s->avctx->profile = get_bits(&s->gb, 3);
1319 s->avctx->level = get_bits(&s->gb, 4);
1320 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1321 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1322 horiz_size_ext = get_bits(&s->gb, 2);
1323 vert_size_ext = get_bits(&s->gb, 2);
1324 s->width |= (horiz_size_ext << 12);
1325 s->height |= (vert_size_ext << 12);
1327 bit_rate_ext = get_bits(&s->gb, 12) << 18;
1328 if (bit_rate_ext < INT_MAX / 400 &&
1329 bit_rate_ext * 400 < INT_MAX - s->bit_rate) {
1330 s->bit_rate += bit_rate_ext * 400;
1332 av_log(s->avctx, AV_LOG_WARNING, "Invalid bit rate extension value: %d\n",
1333 bit_rate_ext >> 18);
1337 skip_bits1(&s->gb); /* marker */
1338 s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1340 s->low_delay = get_bits1(&s->gb);
1341 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
1344 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1345 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1347 ff_dlog(s->avctx, "sequence extension\n");
1348 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1350 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1351 av_log(s->avctx, AV_LOG_DEBUG,
1352 "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1353 s->avctx->profile, s->avctx->level,
1354 s->avctx->rc_buffer_size, s->bit_rate);
1357 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1359 MpegEncContext *s = &s1->mpeg_enc_ctx;
1360 int color_description, w, h;
1362 skip_bits(&s->gb, 3); /* video format */
1363 color_description = get_bits1(&s->gb);
1364 if (color_description) {
1365 s->avctx->color_primaries = get_bits(&s->gb, 8);
1366 s->avctx->color_trc = get_bits(&s->gb, 8);
1367 s->avctx->colorspace = get_bits(&s->gb, 8);
1369 w = get_bits(&s->gb, 14);
1370 skip_bits(&s->gb, 1); // marker
1371 h = get_bits(&s->gb, 14);
1372 // remaining 3 bits are zero padding
1374 s1->pan_scan.width = 16 * w;
1375 s1->pan_scan.height = 16 * h;
1377 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1378 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1381 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1383 MpegEncContext *s = &s1->mpeg_enc_ctx;
1387 if (s->progressive_sequence) {
1388 if (s->repeat_first_field) {
1390 if (s->top_field_first)
1394 if (s->picture_structure == PICT_FRAME) {
1396 if (s->repeat_first_field)
1400 for (i = 0; i < nofco; i++) {
1401 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1402 skip_bits(&s->gb, 1); // marker
1403 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1404 skip_bits(&s->gb, 1); // marker
1407 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1408 av_log(s->avctx, AV_LOG_DEBUG,
1409 "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
1410 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1411 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1412 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1415 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
1416 uint16_t matrix1[64], int intra)
1420 for (i = 0; i < 64; i++) {
1421 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1422 int v = get_bits(&s->gb, 8);
1424 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1425 return AVERROR_INVALIDDATA;
1427 if (intra && i == 0 && v != 8) {
1428 av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
1429 v = 8; // needed by pink.mpg / issue1046
1438 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1440 ff_dlog(s->avctx, "matrix extension\n");
1442 if (get_bits1(&s->gb))
1443 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1444 if (get_bits1(&s->gb))
1445 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1446 if (get_bits1(&s->gb))
1447 load_matrix(s, s->chroma_intra_matrix, NULL, 1);
1448 if (get_bits1(&s->gb))
1449 load_matrix(s, s->chroma_inter_matrix, NULL, 0);
1452 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1454 MpegEncContext *s = &s1->mpeg_enc_ctx;
1456 s->full_pel[0] = s->full_pel[1] = 0;
1457 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1458 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1459 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1460 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1461 if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1462 av_log(s->avctx, AV_LOG_ERROR,
1463 "Missing picture start code, guessing missing values\n");
1464 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1465 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1466 s->pict_type = AV_PICTURE_TYPE_I;
1468 s->pict_type = AV_PICTURE_TYPE_P;
1470 s->pict_type = AV_PICTURE_TYPE_B;
1471 s->current_picture.f->pict_type = s->pict_type;
1472 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1474 s->intra_dc_precision = get_bits(&s->gb, 2);
1475 s->picture_structure = get_bits(&s->gb, 2);
1476 s->top_field_first = get_bits1(&s->gb);
1477 s->frame_pred_frame_dct = get_bits1(&s->gb);
1478 s->concealment_motion_vectors = get_bits1(&s->gb);
1479 s->q_scale_type = get_bits1(&s->gb);
1480 s->intra_vlc_format = get_bits1(&s->gb);
1481 s->alternate_scan = get_bits1(&s->gb);
1482 s->repeat_first_field = get_bits1(&s->gb);
1483 s->chroma_420_type = get_bits1(&s->gb);
1484 s->progressive_frame = get_bits1(&s->gb);
1486 if (s->progressive_sequence && !s->progressive_frame) {
1487 s->progressive_frame = 1;
1488 av_log(s->avctx, AV_LOG_ERROR,
1489 "interlaced frame in progressive sequence, ignoring\n");
1492 if (s->picture_structure == 0 ||
1493 (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
1494 av_log(s->avctx, AV_LOG_ERROR,
1495 "picture_structure %d invalid, ignoring\n",
1496 s->picture_structure);
1497 s->picture_structure = PICT_FRAME;
1500 if (s->progressive_sequence && !s->frame_pred_frame_dct)
1501 av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
1503 if (s->picture_structure == PICT_FRAME) {
1504 s->v_edge_pos = 16 * s->mb_height;
1506 s->v_edge_pos = 8 * s->mb_height;
1507 memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
1510 if (s->alternate_scan) {
1511 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
1512 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
1514 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
1515 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
1518 /* composite display not parsed */
1519 ff_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1520 ff_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1521 ff_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1522 ff_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1523 ff_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1524 ff_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1525 ff_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1526 ff_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1527 ff_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1530 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1532 AVCodecContext *avctx = s->avctx;
1533 Mpeg1Context *s1 = (Mpeg1Context *) s;
1536 if (s->picture_structure == PICT_FRAME)
1539 s->first_field ^= 1;
1541 /* start frame decoding */
1542 if (s->first_field || s->picture_structure == PICT_FRAME) {
1543 AVFrameSideData *pan_scan;
1545 if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
1548 ff_mpeg_er_frame_start(s);
1550 /* first check if we must repeat the frame */
1551 s->current_picture_ptr->f->repeat_pict = 0;
1552 if (s->repeat_first_field) {
1553 if (s->progressive_sequence) {
1554 if (s->top_field_first)
1555 s->current_picture_ptr->f->repeat_pict = 4;
1557 s->current_picture_ptr->f->repeat_pict = 2;
1558 } else if (s->progressive_frame) {
1559 s->current_picture_ptr->f->repeat_pict = 1;
1563 pan_scan = av_frame_new_side_data(s->current_picture_ptr->f,
1564 AV_FRAME_DATA_PANSCAN,
1565 sizeof(s1->pan_scan));
1567 return AVERROR(ENOMEM);
1568 memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
1570 if (s1->a53_caption) {
1571 AVFrameSideData *sd = av_frame_new_side_data(
1572 s->current_picture_ptr->f, AV_FRAME_DATA_A53_CC,
1573 s1->a53_caption_size);
1575 memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
1576 av_freep(&s1->a53_caption);
1579 if (s1->has_stereo3d) {
1580 AVStereo3D *stereo = av_stereo3d_create_side_data(s->current_picture_ptr->f);
1582 return AVERROR(ENOMEM);
1584 *stereo = s1->stereo3d;
1585 s1->has_stereo3d = 0;
1589 AVFrameSideData *sd =
1590 av_frame_new_side_data(s->current_picture_ptr->f,
1591 AV_FRAME_DATA_AFD, 1);
1593 return AVERROR(ENOMEM);
1595 *sd->data = s1->afd;
1599 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1600 ff_thread_finish_setup(avctx);
1601 } else { // second field
1604 if (!s->current_picture_ptr) {
1605 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1606 return AVERROR_INVALIDDATA;
1609 if (s->avctx->hwaccel &&
1610 (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
1611 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1612 av_log(avctx, AV_LOG_ERROR,
1613 "hardware accelerator failed to decode first field\n");
1616 for (i = 0; i < 4; i++) {
1617 s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
1618 if (s->picture_structure == PICT_BOTTOM_FIELD)
1619 s->current_picture.f->data[i] +=
1620 s->current_picture_ptr->f->linesize[i];
1624 if (avctx->hwaccel) {
1625 if ((ret = avctx->hwaccel->start_frame(avctx, buf, buf_size)) < 0)
1632 #define DECODE_SLICE_ERROR -1
1633 #define DECODE_SLICE_OK 0
1637 * MpegEncContext.mb_y must be set to the MB row from the startcode.
1638 * @return DECODE_SLICE_ERROR if the slice is damaged,
1639 * DECODE_SLICE_OK if this slice is OK
1641 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1642 const uint8_t **buf, int buf_size)
1644 AVCodecContext *avctx = s->avctx;
1645 const int field_pic = s->picture_structure != PICT_FRAME;
1649 s->resync_mb_y = -1;
1651 assert(mb_y < s->mb_height);
1653 init_get_bits(&s->gb, *buf, buf_size * 8);
1655 ff_mpeg1_clean_buffers(s);
1656 s->interlaced_dct = 0;
1658 s->qscale = get_qscale(s);
1660 if (s->qscale == 0) {
1661 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1662 return AVERROR_INVALIDDATA;
1665 /* extra slice info */
1666 while (get_bits1(&s->gb) != 0)
1667 skip_bits(&s->gb, 8);
1671 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1674 while (get_bits_left(&s->gb) > 0) {
1675 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1676 MBINCR_VLC_BITS, 2);
1678 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1679 return AVERROR_INVALIDDATA;
1684 /* otherwise, stuffing, nothing to do */
1692 if (s->mb_x >= (unsigned) s->mb_width) {
1693 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1694 return AVERROR_INVALIDDATA;
1697 if (avctx->hwaccel) {
1698 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1699 int start_code = -1;
1700 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1701 if (buf_end < *buf + buf_size)
1704 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1705 return DECODE_SLICE_ERROR;
1707 return DECODE_SLICE_OK;
1710 s->resync_mb_x = s->mb_x;
1711 s->resync_mb_y = s->mb_y = mb_y;
1713 ff_init_block_index(s);
1715 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1716 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1717 av_log(s->avctx, AV_LOG_DEBUG,
1718 "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",
1720 s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
1721 s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1722 s->pict_type == AV_PICTURE_TYPE_I ? "I" :
1723 (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
1724 (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
1725 s->progressive_sequence ? "ps" : "",
1726 s->progressive_frame ? "pf" : "",
1727 s->alternate_scan ? "alt" : "",
1728 s->top_field_first ? "top" : "",
1729 s->intra_dc_precision, s->picture_structure,
1730 s->frame_pred_frame_dct, s->concealment_motion_vectors,
1731 s->q_scale_type, s->intra_vlc_format,
1732 s->repeat_first_field, s->chroma_420_type ? "420" : "");
1737 if ((ret = mpeg_decode_mb(s, s->block)) < 0)
1740 // Note motion_val is normally NULL unless we want to extract the MVs.
1741 if (s->current_picture.motion_val[0] && !s->encoding) {
1742 const int wrap = s->b8_stride;
1743 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1744 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1745 int motion_x, motion_y, dir, i;
1747 for (i = 0; i < 2; i++) {
1748 for (dir = 0; dir < 2; dir++) {
1750 (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1751 motion_x = motion_y = 0;
1752 } else if (s->mv_type == MV_TYPE_16X16 ||
1753 (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1754 motion_x = s->mv[dir][0][0];
1755 motion_y = s->mv[dir][0][1];
1756 } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
1757 motion_x = s->mv[dir][i][0];
1758 motion_y = s->mv[dir][i][1];
1761 s->current_picture.motion_val[dir][xy][0] = motion_x;
1762 s->current_picture.motion_val[dir][xy][1] = motion_y;
1763 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1764 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1765 s->current_picture.ref_index [dir][b8_xy] =
1766 s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1767 assert(s->field_select[dir][i] == 0 ||
1768 s->field_select[dir][i] == 1);
1776 s->dest[1] += 16 >> s->chroma_x_shift;
1777 s->dest[2] += 16 >> s->chroma_x_shift;
1779 ff_mpv_decode_mb(s, s->block);
1781 if (++s->mb_x >= s->mb_width) {
1782 const int mb_size = 16;
1784 ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
1785 ff_mpv_report_decode_progress(s);
1788 s->mb_y += 1 << field_pic;
1790 if (s->mb_y >= s->mb_height) {
1791 int left = get_bits_left(&s->gb);
1792 int is_d10 = s->chroma_format == 2 &&
1793 s->pict_type == AV_PICTURE_TYPE_I &&
1794 avctx->profile == 0 && avctx->level == 5 &&
1795 s->intra_dc_precision == 2 &&
1796 s->q_scale_type == 1 && s->alternate_scan == 0 &&
1797 s->progressive_frame == 0
1798 /* vbv_delay == 0xBBB || 0xE10 */;
1801 (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
1802 ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
1803 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
1804 left, show_bits(&s->gb, FFMIN(left, 23)));
1805 return AVERROR_INVALIDDATA;
1810 ff_init_block_index(s);
1813 /* skip mb handling */
1814 if (s->mb_skip_run == -1) {
1815 /* read increment again */
1818 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1819 MBINCR_VLC_BITS, 2);
1821 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1822 return AVERROR_INVALIDDATA;
1826 s->mb_skip_run += 33;
1827 } else if (code == 35) {
1828 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1829 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1830 return AVERROR_INVALIDDATA;
1832 goto eos; /* end of slice */
1834 /* otherwise, stuffing, nothing to do */
1836 s->mb_skip_run += code;
1840 if (s->mb_skip_run) {
1842 if (s->pict_type == AV_PICTURE_TYPE_I) {
1843 av_log(s->avctx, AV_LOG_ERROR,
1844 "skipped MB in I-frame at %d %d\n", s->mb_x, s->mb_y);
1845 return AVERROR_INVALIDDATA;
1850 for (i = 0; i < 12; i++)
1851 s->block_last_index[i] = -1;
1852 if (s->picture_structure == PICT_FRAME)
1853 s->mv_type = MV_TYPE_16X16;
1855 s->mv_type = MV_TYPE_FIELD;
1856 if (s->pict_type == AV_PICTURE_TYPE_P) {
1857 /* if P type, zero motion vector is implied */
1858 s->mv_dir = MV_DIR_FORWARD;
1859 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1860 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1861 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1862 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1864 /* if B type, reuse previous vectors and directions */
1865 s->mv[0][0][0] = s->last_mv[0][0][0];
1866 s->mv[0][0][1] = s->last_mv[0][0][1];
1867 s->mv[1][0][0] = s->last_mv[1][0][0];
1868 s->mv[1][0][1] = s->last_mv[1][0][1];
1873 eos: // end of slice
1874 *buf += (get_bits_count(&s->gb) - 1) / 8;
1875 ff_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1879 static int slice_decode_thread(AVCodecContext *c, void *arg)
1881 MpegEncContext *s = *(void **) arg;
1882 const uint8_t *buf = s->gb.buffer;
1883 int mb_y = s->start_mb_y;
1884 const int field_pic = s->picture_structure != PICT_FRAME;
1886 s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
1889 uint32_t start_code;
1892 ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
1894 ff_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1895 ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
1896 s->start_mb_y, s->end_mb_y, s->er.error_count);
1898 if (c->err_recognition & AV_EF_EXPLODE)
1900 if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
1901 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1903 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
1905 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1906 s->mb_x - 1, s->mb_y,
1907 ER_AC_END | ER_DC_END | ER_MV_END);
1910 if (s->mb_y == s->end_mb_y)
1914 buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
1915 mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
1916 if (s->picture_structure == PICT_BOTTOM_FIELD)
1918 if (mb_y < 0 || mb_y >= s->end_mb_y)
1919 return AVERROR_INVALIDDATA;
1924 * Handle slice ends.
1925 * @return 1 if it seems to be the last slice
1927 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
1929 Mpeg1Context *s1 = avctx->priv_data;
1930 MpegEncContext *s = &s1->mpeg_enc_ctx;
1932 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
1935 if (s->avctx->hwaccel) {
1936 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1937 av_log(avctx, AV_LOG_ERROR,
1938 "hardware accelerator failed to decode picture\n");
1941 /* end of slice reached */
1942 if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field) {
1945 ff_er_frame_end(&s->er);
1947 ff_mpv_frame_end(s);
1949 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
1950 int ret = av_frame_ref(pict, s->current_picture_ptr->f);
1953 ff_print_debug_info(s, s->current_picture_ptr);
1955 if (avctx->active_thread_type & FF_THREAD_FRAME)
1956 s->picture_number++;
1957 /* latency of 1 frame for I- and P-frames */
1958 /* XXX: use another variable than picture_number */
1959 if (s->last_picture_ptr) {
1960 int ret = av_frame_ref(pict, s->last_picture_ptr->f);
1963 ff_print_debug_info(s, s->last_picture_ptr);
1973 static int mpeg1_decode_sequence(AVCodecContext *avctx,
1974 const uint8_t *buf, int buf_size)
1976 Mpeg1Context *s1 = avctx->priv_data;
1977 MpegEncContext *s = &s1->mpeg_enc_ctx;
1981 init_get_bits(&s->gb, buf, buf_size * 8);
1983 width = get_bits(&s->gb, 12);
1984 height = get_bits(&s->gb, 12);
1985 if (width == 0 || height == 0) {
1986 av_log(avctx, AV_LOG_WARNING,
1987 "Invalid horizontal or vertical size value.\n");
1988 if (avctx->err_recognition & AV_EF_BITSTREAM)
1989 return AVERROR_INVALIDDATA;
1991 s->aspect_ratio_info = get_bits(&s->gb, 4);
1992 if (s->aspect_ratio_info == 0) {
1993 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
1994 if (avctx->err_recognition & AV_EF_BITSTREAM)
1995 return AVERROR_INVALIDDATA;
1997 s->frame_rate_index = get_bits(&s->gb, 4);
1998 if (s->frame_rate_index == 0 || s->frame_rate_index > 13) {
1999 av_log(avctx, AV_LOG_WARNING,
2000 "frame_rate_index %d is invalid\n", s->frame_rate_index);
2001 return AVERROR_INVALIDDATA;
2003 s->bit_rate = get_bits(&s->gb, 18) * 400;
2004 if (get_bits1(&s->gb) == 0) { /* marker */
2005 av_log(avctx, AV_LOG_ERROR, "Marker in sequence header missing\n");
2006 return AVERROR_INVALIDDATA;
2011 s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
2012 skip_bits(&s->gb, 1);
2015 if (get_bits1(&s->gb)) {
2016 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
2018 for (i = 0; i < 64; i++) {
2019 j = s->idsp.idct_permutation[i];
2020 v = ff_mpeg1_default_intra_matrix[i];
2021 s->intra_matrix[j] = v;
2022 s->chroma_intra_matrix[j] = v;
2025 if (get_bits1(&s->gb)) {
2026 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
2028 for (i = 0; i < 64; i++) {
2029 int j = s->idsp.idct_permutation[i];
2030 v = ff_mpeg1_default_non_intra_matrix[i];
2031 s->inter_matrix[j] = v;
2032 s->chroma_inter_matrix[j] = v;
2036 if (show_bits(&s->gb, 23) != 0) {
2037 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2038 return AVERROR_INVALIDDATA;
2041 /* We set MPEG-2 parameters so that it emulates MPEG-1. */
2042 s->progressive_sequence = 1;
2043 s->progressive_frame = 1;
2044 s->picture_structure = PICT_FRAME;
2045 s->frame_pred_frame_dct = 1;
2046 s->chroma_format = 1;
2048 s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
2049 s->out_format = FMT_MPEG1;
2050 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
2053 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2054 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2055 s->avctx->rc_buffer_size, s->bit_rate);
2060 static int vcr2_init_sequence(AVCodecContext *avctx)
2062 Mpeg1Context *s1 = avctx->priv_data;
2063 MpegEncContext *s = &s1->mpeg_enc_ctx;
2066 /* start new MPEG-1 context decoding */
2067 s->out_format = FMT_MPEG1;
2068 if (s1->mpeg_enc_ctx_allocated) {
2069 ff_mpv_common_end(s);
2071 s->width = avctx->coded_width;
2072 s->height = avctx->coded_height;
2073 avctx->has_b_frames = 0; // true?
2076 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2078 if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
2079 avctx->idct_algo = FF_IDCT_SIMPLE;
2081 ff_mpv_idct_init(s);
2082 if ((ret = ff_mpv_common_init(s)) < 0)
2084 s1->mpeg_enc_ctx_allocated = 1;
2086 for (i = 0; i < 64; i++) {
2087 int j = s->idsp.idct_permutation[i];
2088 v = ff_mpeg1_default_intra_matrix[i];
2089 s->intra_matrix[j] = v;
2090 s->chroma_intra_matrix[j] = v;
2092 v = ff_mpeg1_default_non_intra_matrix[i];
2093 s->inter_matrix[j] = v;
2094 s->chroma_inter_matrix[j] = v;
2097 s->progressive_sequence = 1;
2098 s->progressive_frame = 1;
2099 s->picture_structure = PICT_FRAME;
2100 s->frame_pred_frame_dct = 1;
2101 s->chroma_format = 1;
2102 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
2103 s1->save_width = s->width;
2104 s1->save_height = s->height;
2105 s1->save_progressive_seq = s->progressive_sequence;
2109 static int mpeg_decode_a53_cc(AVCodecContext *avctx,
2110 const uint8_t *p, int buf_size)
2112 Mpeg1Context *s1 = avctx->priv_data;
2114 if (buf_size >= 6 &&
2115 p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
2116 p[4] == 3 && (p[5] & 0x40)) {
2117 /* extract A53 Part 4 CC data */
2118 int cc_count = p[5] & 0x1f;
2119 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2120 av_freep(&s1->a53_caption);
2121 s1->a53_caption_size = cc_count * 3;
2122 s1->a53_caption = av_malloc(s1->a53_caption_size);
2123 if (s1->a53_caption)
2124 memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
2127 } else if (buf_size >= 11 &&
2128 p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
2129 /* extract DVD CC data */
2132 // There is a caption count field in the data, but it is often
2133 // incorrect. So count the number of captions present.
2134 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2136 // Transform the DVD format into A53 Part 4 format
2138 av_freep(&s1->a53_caption);
2139 s1->a53_caption_size = cc_count * 6;
2140 s1->a53_caption = av_malloc(s1->a53_caption_size);
2141 if (s1->a53_caption) {
2142 uint8_t field1 = !!(p[4] & 0x80);
2143 uint8_t *cap = s1->a53_caption;
2145 for (i = 0; i < cc_count; i++) {
2146 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2149 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2162 static void mpeg_decode_user_data(AVCodecContext *avctx,
2163 const uint8_t *p, int buf_size)
2165 const uint8_t *buf_end = p + buf_size;
2166 Mpeg1Context *s1 = avctx->priv_data;
2168 /* we parse the DTG active format information */
2169 if (buf_end - p >= 5 &&
2170 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2178 if (buf_end - p < 1)
2181 s1->afd = p[0] & 0x0f;
2183 } else if (buf_end - p >= 6 &&
2184 p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
2185 p[4] == 0x03) { // S3D_video_format_length
2186 // the 0x7F mask ignores the reserved_bit value
2187 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2189 if (S3D_video_format_type == 0x03 ||
2190 S3D_video_format_type == 0x04 ||
2191 S3D_video_format_type == 0x08 ||
2192 S3D_video_format_type == 0x23) {
2194 s1->has_stereo3d = 1;
2196 switch (S3D_video_format_type) {
2198 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
2201 s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
2204 s1->stereo3d.type = AV_STEREO3D_2D;
2207 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2211 } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
2216 static void mpeg_decode_gop(AVCodecContext *avctx,
2217 const uint8_t *buf, int buf_size)
2219 Mpeg1Context *s1 = avctx->priv_data;
2220 MpegEncContext *s = &s1->mpeg_enc_ctx;
2222 int time_code_hours, time_code_minutes;
2223 int time_code_seconds, time_code_pictures;
2226 init_get_bits(&s->gb, buf, buf_size * 8);
2228 skip_bits1(&s->gb); /* drop_frame_flag */
2230 time_code_hours = get_bits(&s->gb, 5);
2231 time_code_minutes = get_bits(&s->gb, 6);
2232 skip_bits1(&s->gb); // marker bit
2233 time_code_seconds = get_bits(&s->gb, 6);
2234 time_code_pictures = get_bits(&s->gb, 6);
2236 s1->closed_gop = get_bits1(&s->gb);
2237 /* broken_link indicate that after editing the
2238 * reference frames of the first B-Frames after GOP I-Frame
2239 * are missing (open gop) */
2240 broken_link = get_bits1(&s->gb);
2242 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2243 av_log(s->avctx, AV_LOG_DEBUG,
2244 "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
2245 time_code_hours, time_code_minutes, time_code_seconds,
2246 time_code_pictures, s1->closed_gop, broken_link);
2249 static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
2250 int *got_output, const uint8_t *buf, int buf_size)
2252 Mpeg1Context *s = avctx->priv_data;
2253 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2254 const uint8_t *buf_ptr = buf;
2255 const uint8_t *buf_end = buf + buf_size;
2256 int ret, input_size;
2257 int last_code = 0, skip_frame = 0;
2260 /* find next start code */
2261 uint32_t start_code = -1;
2262 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
2263 if (start_code > 0x1ff) {
2266 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2270 avctx->execute(avctx, slice_decode_thread,
2271 &s2->thread_context[0], NULL,
2272 s->slice_count, sizeof(void *));
2273 for (i = 0; i < s->slice_count; i++)
2274 s2->er.error_count += s2->thread_context[i]->er.error_count;
2277 ret = slice_end(avctx, picture);
2281 // FIXME: merge with the stuff in mpeg_decode_slice
2282 if (s2->last_picture_ptr || s2->low_delay)
2287 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2290 input_size = buf_end - buf_ptr;
2292 if (avctx->debug & FF_DEBUG_STARTCODE)
2293 av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %td left %d\n",
2294 start_code, buf_ptr - buf, input_size);
2296 /* prepare data for next start code */
2297 switch (start_code) {
2298 case SEQ_START_CODE:
2299 if (last_code == 0) {
2300 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2303 av_log(avctx, AV_LOG_ERROR,
2304 "ignoring SEQ_START_CODE after %X\n", last_code);
2305 if (avctx->err_recognition & AV_EF_EXPLODE)
2306 return AVERROR_INVALIDDATA;
2310 case PICTURE_START_CODE:
2311 if (s2->width <= 0 || s2->height <= 0) {
2312 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
2313 s2->width, s2->height);
2314 return AVERROR_INVALIDDATA;
2317 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
2318 !avctx->hwaccel && s->slice_count) {
2321 avctx->execute(avctx, slice_decode_thread,
2322 s2->thread_context, NULL,
2323 s->slice_count, sizeof(void *));
2324 for (i = 0; i < s->slice_count; i++)
2325 s2->er.error_count += s2->thread_context[i]->er.error_count;
2328 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2329 ret = mpeg_decode_postinit(avctx);
2331 av_log(avctx, AV_LOG_ERROR,
2332 "mpeg_decode_postinit() failure\n");
2336 /* We have a complete image: we try to decompress it. */
2337 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2340 last_code = PICTURE_START_CODE;
2342 av_log(avctx, AV_LOG_ERROR,
2343 "ignoring pic after %X\n", last_code);
2344 if (avctx->err_recognition & AV_EF_EXPLODE)
2345 return AVERROR_INVALIDDATA;
2348 case EXT_START_CODE:
2349 init_get_bits(&s2->gb, buf_ptr, input_size * 8);
2351 switch (get_bits(&s2->gb, 4)) {
2353 if (last_code == 0) {
2354 mpeg_decode_sequence_extension(s);
2356 av_log(avctx, AV_LOG_ERROR,
2357 "ignoring seq ext after %X\n", last_code);
2358 if (avctx->err_recognition & AV_EF_EXPLODE)
2359 return AVERROR_INVALIDDATA;
2363 mpeg_decode_sequence_display_extension(s);
2366 mpeg_decode_quant_matrix_extension(s2);
2369 mpeg_decode_picture_display_extension(s);
2372 if (last_code == PICTURE_START_CODE) {
2373 mpeg_decode_picture_coding_extension(s);
2375 av_log(avctx, AV_LOG_ERROR,
2376 "ignoring pic cod ext after %X\n", last_code);
2377 if (avctx->err_recognition & AV_EF_EXPLODE)
2378 return AVERROR_INVALIDDATA;
2383 case USER_START_CODE:
2384 mpeg_decode_user_data(avctx, buf_ptr, input_size);
2386 case GOP_START_CODE:
2387 if (last_code == 0) {
2388 s2->first_field = 0;
2389 mpeg_decode_gop(avctx, buf_ptr, input_size);
2392 av_log(avctx, AV_LOG_ERROR,
2393 "ignoring GOP_START_CODE after %X\n", last_code);
2394 if (avctx->err_recognition & AV_EF_EXPLODE)
2395 return AVERROR_INVALIDDATA;
2399 if (start_code >= SLICE_MIN_START_CODE &&
2400 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2401 const int field_pic = s2->picture_structure != PICT_FRAME;
2402 int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
2403 last_code = SLICE_MIN_START_CODE;
2405 if (s2->picture_structure == PICT_BOTTOM_FIELD)
2408 if (mb_y >= s2->mb_height) {
2409 av_log(s2->avctx, AV_LOG_ERROR,
2410 "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2411 return AVERROR_INVALIDDATA;
2414 if (!s2->last_picture_ptr) {
2415 /* Skip B-frames if we do not have reference frames and
2416 * GOP is not closed. */
2417 if (s2->pict_type == AV_PICTURE_TYPE_B) {
2418 if (!s->closed_gop) {
2424 if (s2->pict_type == AV_PICTURE_TYPE_I)
2426 if (!s2->next_picture_ptr) {
2427 /* Skip P-frames if we do not have a reference frame or
2428 * we have an invalid header. */
2429 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
2434 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
2435 s2->pict_type == AV_PICTURE_TYPE_B) ||
2436 (avctx->skip_frame >= AVDISCARD_NONKEY &&
2437 s2->pict_type != AV_PICTURE_TYPE_I) ||
2438 avctx->skip_frame >= AVDISCARD_ALL) {
2443 if (!s->mpeg_enc_ctx_allocated)
2446 if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2447 if (mb_y < avctx->skip_top ||
2448 mb_y >= s2->mb_height - avctx->skip_bottom)
2452 if (!s2->pict_type) {
2453 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2454 if (avctx->err_recognition & AV_EF_EXPLODE)
2455 return AVERROR_INVALIDDATA;
2459 if (s->first_slice) {
2462 if ((ret = mpeg_field_start(s2, buf, buf_size)) < 0)
2465 if (!s2->current_picture_ptr) {
2466 av_log(avctx, AV_LOG_ERROR,
2467 "current_picture not initialized\n");
2468 return AVERROR_INVALIDDATA;
2472 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2474 int threshold = (s2->mb_height * s->slice_count +
2475 s2->slice_context_count / 2) /
2476 s2->slice_context_count;
2477 if (threshold <= mb_y) {
2478 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2480 thread_context->start_mb_y = mb_y;
2481 thread_context->end_mb_y = s2->mb_height;
2482 if (s->slice_count) {
2483 s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
2484 ret = ff_update_duplicate_context(thread_context, s2);
2488 init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
2491 buf_ptr += 2; // FIXME add minimum number of bytes per slice
2493 ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2497 if (avctx->err_recognition & AV_EF_EXPLODE)
2499 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2500 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2501 s2->resync_mb_y, s2->mb_x, s2->mb_y,
2502 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
2504 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2505 s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
2506 ER_AC_END | ER_DC_END | ER_MV_END);
2515 static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
2516 int *got_output, AVPacket *avpkt)
2518 const uint8_t *buf = avpkt->data;
2519 int buf_size = avpkt->size;
2520 Mpeg1Context *s = avctx->priv_data;
2521 AVFrame *picture = data;
2522 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2523 ff_dlog(avctx, "fill_buffer\n");
2525 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2526 /* special case for last picture */
2527 if (s2->low_delay == 0 && s2->next_picture_ptr) {
2528 int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
2532 s2->next_picture_ptr = NULL;
2539 if (s2->avctx->flags & AV_CODEC_FLAG_TRUNCATED) {
2540 int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
2543 if (ff_combine_frame(&s2->parse_context, next,
2544 (const uint8_t **) &buf, &buf_size) < 0)
2548 if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
2549 vcr2_init_sequence(avctx);
2553 if (avctx->extradata && !s->extradata_decoded) {
2554 int ret = decode_chunks(avctx, picture, got_output,
2555 avctx->extradata, avctx->extradata_size);
2556 s->extradata_decoded = 1;
2557 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
2561 return decode_chunks(avctx, picture, got_output, buf, buf_size);
2564 static void flush(AVCodecContext *avctx)
2566 Mpeg1Context *s = avctx->priv_data;
2571 ff_mpeg_flush(avctx);
2574 static av_cold int mpeg_decode_end(AVCodecContext *avctx)
2576 Mpeg1Context *s = avctx->priv_data;
2578 if (s->mpeg_enc_ctx_allocated)
2579 ff_mpv_common_end(&s->mpeg_enc_ctx);
2580 av_freep(&s->a53_caption);
2584 AVCodec ff_mpeg1video_decoder = {
2585 .name = "mpeg1video",
2586 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2587 .type = AVMEDIA_TYPE_VIDEO,
2588 .id = AV_CODEC_ID_MPEG1VIDEO,
2589 .priv_data_size = sizeof(Mpeg1Context),
2590 .init = mpeg_decode_init,
2591 .close = mpeg_decode_end,
2592 .decode = mpeg_decode_frame,
2593 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2594 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
2595 AV_CODEC_CAP_SLICE_THREADS,
2597 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2600 AVCodec ff_mpeg2video_decoder = {
2601 .name = "mpeg2video",
2602 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2603 .type = AVMEDIA_TYPE_VIDEO,
2604 .id = AV_CODEC_ID_MPEG2VIDEO,
2605 .priv_data_size = sizeof(Mpeg1Context),
2606 .init = mpeg_decode_init,
2607 .close = mpeg_decode_end,
2608 .decode = mpeg_decode_frame,
2609 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2610 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
2611 AV_CODEC_CAP_SLICE_THREADS,
2613 .profiles = NULL_IF_CONFIG_SMALL(ff_mpeg2_video_profiles),