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,
1115 #if CONFIG_MPEG2_VAAPI_HWACCEL
1118 #if CONFIG_MPEG1_VDPAU_HWACCEL | CONFIG_MPEG2_VDPAU_HWACCEL
1125 static const enum AVPixelFormat mpeg12_pixfmt_list_422[] = {
1130 static const enum AVPixelFormat mpeg12_pixfmt_list_444[] = {
1135 static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
1137 Mpeg1Context *s1 = avctx->priv_data;
1138 MpegEncContext *s = &s1->mpeg_enc_ctx;
1139 const enum AVPixelFormat *pix_fmts;
1141 if (s->chroma_format < 2)
1142 pix_fmts = mpeg12_hwaccel_pixfmt_list_420;
1143 else if (s->chroma_format == 2)
1144 pix_fmts = mpeg12_pixfmt_list_422;
1146 pix_fmts = mpeg12_pixfmt_list_444;
1148 return ff_get_format(avctx, pix_fmts);
1151 /* Call this function when we know all parameters.
1152 * It may be called in different places for MPEG-1 and MPEG-2. */
1153 static int mpeg_decode_postinit(AVCodecContext *avctx)
1155 Mpeg1Context *s1 = avctx->priv_data;
1156 MpegEncContext *s = &s1->mpeg_enc_ctx;
1157 uint8_t old_permutation[64];
1160 if ((s1->mpeg_enc_ctx_allocated == 0) ||
1161 avctx->coded_width != s->width ||
1162 avctx->coded_height != s->height ||
1163 s1->save_width != s->width ||
1164 s1->save_height != s->height ||
1165 s1->save_aspect_info != s->aspect_ratio_info ||
1166 s1->save_progressive_seq != s->progressive_sequence ||
1168 if (s1->mpeg_enc_ctx_allocated) {
1169 ParseContext pc = s->parse_context;
1170 s->parse_context.buffer = 0;
1171 ff_mpv_common_end(s);
1172 s->parse_context = pc;
1175 ret = ff_set_dimensions(avctx, s->width, s->height);
1179 avctx->bit_rate = s->bit_rate;
1180 s1->save_aspect_info = s->aspect_ratio_info;
1181 s1->save_width = s->width;
1182 s1->save_height = s->height;
1183 s1->save_progressive_seq = s->progressive_sequence;
1185 /* low_delay may be forced, in this case we will have B-frames
1186 * that behave like P-frames. */
1187 avctx->has_b_frames = !s->low_delay;
1189 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1191 avctx->framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
1193 avctx->sample_aspect_ratio = av_d2q(1.0 / ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1194 avctx->ticks_per_frame = 1;
1197 av_reduce(&s->avctx->framerate.num,
1198 &s->avctx->framerate.den,
1199 ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num * 2,
1200 ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1202 avctx->ticks_per_frame = 2;
1204 if (s->aspect_ratio_info > 1) {
1206 av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1207 (AVRational) { s1->pan_scan.width,
1208 s1->pan_scan.height }),
1209 (AVRational) { s->width, s->height });
1211 /* We ignore the spec here and guess a bit as reality does not
1212 * match the spec, see for example res_change_ffmpeg_aspect.ts
1213 * and sequence-display-aspect.mpg.
1214 * issue1613, 621, 562 */
1215 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1216 (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
1217 av_cmp_q(dar, (AVRational) { 16, 9 }))) {
1218 s->avctx->sample_aspect_ratio =
1219 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1220 (AVRational) { s->width, s->height });
1222 s->avctx->sample_aspect_ratio =
1223 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1224 (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
1225 // issue1613 4/3 16/9 -> 16/9
1226 // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1227 // widescreen-issue562.mpg 4/3 16/9 -> 16/9
1228 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1229 ff_dlog(avctx, "A %d/%d\n",
1230 ff_mpeg2_aspect[s->aspect_ratio_info].num,
1231 ff_mpeg2_aspect[s->aspect_ratio_info].den);
1232 ff_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
1233 s->avctx->sample_aspect_ratio.den);
1236 s->avctx->sample_aspect_ratio =
1237 ff_mpeg2_aspect[s->aspect_ratio_info];
1241 ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
1243 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1244 // until then pix_fmt may be changed right after codec init
1245 if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
1246 avctx->idct_algo = FF_IDCT_SIMPLE;
1248 /* Quantization matrices may need reordering
1249 * if DCT permutation is changed. */
1250 memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
1252 ff_mpv_idct_init(s);
1253 if ((ret = ff_mpv_common_init(s)) < 0)
1256 quant_matrix_rebuild(s->intra_matrix, old_permutation, s->idsp.idct_permutation);
1257 quant_matrix_rebuild(s->inter_matrix, old_permutation, s->idsp.idct_permutation);
1258 quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
1259 quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
1261 s1->mpeg_enc_ctx_allocated = 1;
1266 static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
1269 Mpeg1Context *s1 = avctx->priv_data;
1270 MpegEncContext *s = &s1->mpeg_enc_ctx;
1271 int ref, f_code, vbv_delay;
1273 init_get_bits(&s->gb, buf, buf_size * 8);
1275 ref = get_bits(&s->gb, 10); /* temporal ref */
1276 s->pict_type = get_bits(&s->gb, 3);
1277 if (s->pict_type == 0 || s->pict_type > 3)
1278 return AVERROR_INVALIDDATA;
1280 vbv_delay = get_bits(&s->gb, 16);
1281 if (s->pict_type == AV_PICTURE_TYPE_P ||
1282 s->pict_type == AV_PICTURE_TYPE_B) {
1283 s->full_pel[0] = get_bits1(&s->gb);
1284 f_code = get_bits(&s->gb, 3);
1285 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1286 return AVERROR_INVALIDDATA;
1287 s->mpeg_f_code[0][0] = f_code;
1288 s->mpeg_f_code[0][1] = f_code;
1290 if (s->pict_type == AV_PICTURE_TYPE_B) {
1291 s->full_pel[1] = get_bits1(&s->gb);
1292 f_code = get_bits(&s->gb, 3);
1293 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1294 return AVERROR_INVALIDDATA;
1295 s->mpeg_f_code[1][0] = f_code;
1296 s->mpeg_f_code[1][1] = f_code;
1298 s->current_picture.f->pict_type = s->pict_type;
1299 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1301 if (avctx->debug & FF_DEBUG_PICT_INFO)
1302 av_log(avctx, AV_LOG_DEBUG,
1303 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1310 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1312 MpegEncContext *s = &s1->mpeg_enc_ctx;
1313 int horiz_size_ext, vert_size_ext;
1316 skip_bits(&s->gb, 1); /* profile and level esc*/
1317 s->avctx->profile = get_bits(&s->gb, 3);
1318 s->avctx->level = get_bits(&s->gb, 4);
1319 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1320 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1321 horiz_size_ext = get_bits(&s->gb, 2);
1322 vert_size_ext = get_bits(&s->gb, 2);
1323 s->width |= (horiz_size_ext << 12);
1324 s->height |= (vert_size_ext << 12);
1326 bit_rate_ext = get_bits(&s->gb, 12) << 18;
1327 if (bit_rate_ext < INT_MAX / 400 &&
1328 bit_rate_ext * 400 < INT_MAX - s->bit_rate) {
1329 s->bit_rate += bit_rate_ext * 400;
1331 av_log(s->avctx, AV_LOG_WARNING, "Invalid bit rate extension value: %d\n",
1332 bit_rate_ext >> 18);
1336 skip_bits1(&s->gb); /* marker */
1337 s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1339 s->low_delay = get_bits1(&s->gb);
1340 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
1343 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1344 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1346 ff_dlog(s->avctx, "sequence extension\n");
1347 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1349 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1350 av_log(s->avctx, AV_LOG_DEBUG,
1351 "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1352 s->avctx->profile, s->avctx->level,
1353 s->avctx->rc_buffer_size, s->bit_rate);
1356 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1358 MpegEncContext *s = &s1->mpeg_enc_ctx;
1359 int color_description, w, h;
1361 skip_bits(&s->gb, 3); /* video format */
1362 color_description = get_bits1(&s->gb);
1363 if (color_description) {
1364 s->avctx->color_primaries = get_bits(&s->gb, 8);
1365 s->avctx->color_trc = get_bits(&s->gb, 8);
1366 s->avctx->colorspace = get_bits(&s->gb, 8);
1368 w = get_bits(&s->gb, 14);
1369 skip_bits(&s->gb, 1); // marker
1370 h = get_bits(&s->gb, 14);
1371 // remaining 3 bits are zero padding
1373 s1->pan_scan.width = 16 * w;
1374 s1->pan_scan.height = 16 * h;
1376 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1377 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1380 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1382 MpegEncContext *s = &s1->mpeg_enc_ctx;
1386 if (s->progressive_sequence) {
1387 if (s->repeat_first_field) {
1389 if (s->top_field_first)
1393 if (s->picture_structure == PICT_FRAME) {
1395 if (s->repeat_first_field)
1399 for (i = 0; i < nofco; i++) {
1400 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1401 skip_bits(&s->gb, 1); // marker
1402 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1403 skip_bits(&s->gb, 1); // marker
1406 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1407 av_log(s->avctx, AV_LOG_DEBUG,
1408 "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
1409 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1410 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1411 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1414 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
1415 uint16_t matrix1[64], int intra)
1419 for (i = 0; i < 64; i++) {
1420 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1421 int v = get_bits(&s->gb, 8);
1423 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1424 return AVERROR_INVALIDDATA;
1426 if (intra && i == 0 && v != 8) {
1427 av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
1428 v = 8; // needed by pink.mpg / issue1046
1437 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1439 ff_dlog(s->avctx, "matrix extension\n");
1441 if (get_bits1(&s->gb))
1442 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1443 if (get_bits1(&s->gb))
1444 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1445 if (get_bits1(&s->gb))
1446 load_matrix(s, s->chroma_intra_matrix, NULL, 1);
1447 if (get_bits1(&s->gb))
1448 load_matrix(s, s->chroma_inter_matrix, NULL, 0);
1451 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1453 MpegEncContext *s = &s1->mpeg_enc_ctx;
1455 s->full_pel[0] = s->full_pel[1] = 0;
1456 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1457 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1458 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1459 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1460 if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1461 av_log(s->avctx, AV_LOG_ERROR,
1462 "Missing picture start code, guessing missing values\n");
1463 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1464 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1465 s->pict_type = AV_PICTURE_TYPE_I;
1467 s->pict_type = AV_PICTURE_TYPE_P;
1469 s->pict_type = AV_PICTURE_TYPE_B;
1470 s->current_picture.f->pict_type = s->pict_type;
1471 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1473 s->intra_dc_precision = get_bits(&s->gb, 2);
1474 s->picture_structure = get_bits(&s->gb, 2);
1475 s->top_field_first = get_bits1(&s->gb);
1476 s->frame_pred_frame_dct = get_bits1(&s->gb);
1477 s->concealment_motion_vectors = get_bits1(&s->gb);
1478 s->q_scale_type = get_bits1(&s->gb);
1479 s->intra_vlc_format = get_bits1(&s->gb);
1480 s->alternate_scan = get_bits1(&s->gb);
1481 s->repeat_first_field = get_bits1(&s->gb);
1482 s->chroma_420_type = get_bits1(&s->gb);
1483 s->progressive_frame = get_bits1(&s->gb);
1485 if (s->progressive_sequence && !s->progressive_frame) {
1486 s->progressive_frame = 1;
1487 av_log(s->avctx, AV_LOG_ERROR,
1488 "interlaced frame in progressive sequence, ignoring\n");
1491 if (s->picture_structure == 0 ||
1492 (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
1493 av_log(s->avctx, AV_LOG_ERROR,
1494 "picture_structure %d invalid, ignoring\n",
1495 s->picture_structure);
1496 s->picture_structure = PICT_FRAME;
1499 if (s->progressive_sequence && !s->frame_pred_frame_dct)
1500 av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
1502 if (s->picture_structure == PICT_FRAME) {
1503 s->v_edge_pos = 16 * s->mb_height;
1505 s->v_edge_pos = 8 * s->mb_height;
1506 memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
1509 if (s->alternate_scan) {
1510 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
1511 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
1513 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
1514 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
1517 /* composite display not parsed */
1518 ff_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1519 ff_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1520 ff_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1521 ff_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1522 ff_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1523 ff_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1524 ff_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1525 ff_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1526 ff_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1529 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1531 AVCodecContext *avctx = s->avctx;
1532 Mpeg1Context *s1 = (Mpeg1Context *) s;
1535 if (s->picture_structure == PICT_FRAME)
1538 s->first_field ^= 1;
1540 /* start frame decoding */
1541 if (s->first_field || s->picture_structure == PICT_FRAME) {
1542 AVFrameSideData *pan_scan;
1544 if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
1547 ff_mpeg_er_frame_start(s);
1549 /* first check if we must repeat the frame */
1550 s->current_picture_ptr->f->repeat_pict = 0;
1551 if (s->repeat_first_field) {
1552 if (s->progressive_sequence) {
1553 if (s->top_field_first)
1554 s->current_picture_ptr->f->repeat_pict = 4;
1556 s->current_picture_ptr->f->repeat_pict = 2;
1557 } else if (s->progressive_frame) {
1558 s->current_picture_ptr->f->repeat_pict = 1;
1562 pan_scan = av_frame_new_side_data(s->current_picture_ptr->f,
1563 AV_FRAME_DATA_PANSCAN,
1564 sizeof(s1->pan_scan));
1566 return AVERROR(ENOMEM);
1567 memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
1569 if (s1->a53_caption) {
1570 AVFrameSideData *sd = av_frame_new_side_data(
1571 s->current_picture_ptr->f, AV_FRAME_DATA_A53_CC,
1572 s1->a53_caption_size);
1574 memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
1575 av_freep(&s1->a53_caption);
1578 if (s1->has_stereo3d) {
1579 AVStereo3D *stereo = av_stereo3d_create_side_data(s->current_picture_ptr->f);
1581 return AVERROR(ENOMEM);
1583 *stereo = s1->stereo3d;
1584 s1->has_stereo3d = 0;
1588 AVFrameSideData *sd =
1589 av_frame_new_side_data(s->current_picture_ptr->f,
1590 AV_FRAME_DATA_AFD, 1);
1592 return AVERROR(ENOMEM);
1594 *sd->data = s1->afd;
1598 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1599 ff_thread_finish_setup(avctx);
1600 } else { // second field
1603 if (!s->current_picture_ptr) {
1604 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1605 return AVERROR_INVALIDDATA;
1608 if (s->avctx->hwaccel &&
1609 (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
1610 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1611 av_log(avctx, AV_LOG_ERROR,
1612 "hardware accelerator failed to decode first field\n");
1615 for (i = 0; i < 4; i++) {
1616 s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
1617 if (s->picture_structure == PICT_BOTTOM_FIELD)
1618 s->current_picture.f->data[i] +=
1619 s->current_picture_ptr->f->linesize[i];
1623 if (avctx->hwaccel) {
1624 if ((ret = avctx->hwaccel->start_frame(avctx, buf, buf_size)) < 0)
1631 #define DECODE_SLICE_ERROR -1
1632 #define DECODE_SLICE_OK 0
1636 * MpegEncContext.mb_y must be set to the MB row from the startcode.
1637 * @return DECODE_SLICE_ERROR if the slice is damaged,
1638 * DECODE_SLICE_OK if this slice is OK
1640 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1641 const uint8_t **buf, int buf_size)
1643 AVCodecContext *avctx = s->avctx;
1644 const int field_pic = s->picture_structure != PICT_FRAME;
1648 s->resync_mb_y = -1;
1650 assert(mb_y < s->mb_height);
1652 init_get_bits(&s->gb, *buf, buf_size * 8);
1654 ff_mpeg1_clean_buffers(s);
1655 s->interlaced_dct = 0;
1657 s->qscale = get_qscale(s);
1659 if (s->qscale == 0) {
1660 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1661 return AVERROR_INVALIDDATA;
1664 /* extra slice info */
1665 while (get_bits1(&s->gb) != 0)
1666 skip_bits(&s->gb, 8);
1670 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1673 while (get_bits_left(&s->gb) > 0) {
1674 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1675 MBINCR_VLC_BITS, 2);
1677 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1678 return AVERROR_INVALIDDATA;
1683 /* otherwise, stuffing, nothing to do */
1691 if (s->mb_x >= (unsigned) s->mb_width) {
1692 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1693 return AVERROR_INVALIDDATA;
1696 if (avctx->hwaccel) {
1697 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1698 int start_code = -1;
1699 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1700 if (buf_end < *buf + buf_size)
1703 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1704 return DECODE_SLICE_ERROR;
1706 return DECODE_SLICE_OK;
1709 s->resync_mb_x = s->mb_x;
1710 s->resync_mb_y = s->mb_y = mb_y;
1712 ff_init_block_index(s);
1714 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1715 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1716 av_log(s->avctx, AV_LOG_DEBUG,
1717 "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",
1719 s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
1720 s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1721 s->pict_type == AV_PICTURE_TYPE_I ? "I" :
1722 (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
1723 (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
1724 s->progressive_sequence ? "ps" : "",
1725 s->progressive_frame ? "pf" : "",
1726 s->alternate_scan ? "alt" : "",
1727 s->top_field_first ? "top" : "",
1728 s->intra_dc_precision, s->picture_structure,
1729 s->frame_pred_frame_dct, s->concealment_motion_vectors,
1730 s->q_scale_type, s->intra_vlc_format,
1731 s->repeat_first_field, s->chroma_420_type ? "420" : "");
1736 if ((ret = mpeg_decode_mb(s, s->block)) < 0)
1739 // Note motion_val is normally NULL unless we want to extract the MVs.
1740 if (s->current_picture.motion_val[0] && !s->encoding) {
1741 const int wrap = s->b8_stride;
1742 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1743 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1744 int motion_x, motion_y, dir, i;
1746 for (i = 0; i < 2; i++) {
1747 for (dir = 0; dir < 2; dir++) {
1749 (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1750 motion_x = motion_y = 0;
1751 } else if (s->mv_type == MV_TYPE_16X16 ||
1752 (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1753 motion_x = s->mv[dir][0][0];
1754 motion_y = s->mv[dir][0][1];
1755 } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
1756 motion_x = s->mv[dir][i][0];
1757 motion_y = s->mv[dir][i][1];
1760 s->current_picture.motion_val[dir][xy][0] = motion_x;
1761 s->current_picture.motion_val[dir][xy][1] = motion_y;
1762 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1763 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1764 s->current_picture.ref_index [dir][b8_xy] =
1765 s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1766 assert(s->field_select[dir][i] == 0 ||
1767 s->field_select[dir][i] == 1);
1775 s->dest[1] += 16 >> s->chroma_x_shift;
1776 s->dest[2] += 16 >> s->chroma_x_shift;
1778 ff_mpv_decode_mb(s, s->block);
1780 if (++s->mb_x >= s->mb_width) {
1781 const int mb_size = 16;
1783 ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
1784 ff_mpv_report_decode_progress(s);
1787 s->mb_y += 1 << field_pic;
1789 if (s->mb_y >= s->mb_height) {
1790 int left = get_bits_left(&s->gb);
1791 int is_d10 = s->chroma_format == 2 &&
1792 s->pict_type == AV_PICTURE_TYPE_I &&
1793 avctx->profile == 0 && avctx->level == 5 &&
1794 s->intra_dc_precision == 2 &&
1795 s->q_scale_type == 1 && s->alternate_scan == 0 &&
1796 s->progressive_frame == 0
1797 /* vbv_delay == 0xBBB || 0xE10 */;
1800 (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
1801 ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
1802 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
1803 left, show_bits(&s->gb, FFMIN(left, 23)));
1804 return AVERROR_INVALIDDATA;
1809 ff_init_block_index(s);
1812 /* skip mb handling */
1813 if (s->mb_skip_run == -1) {
1814 /* read increment again */
1817 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1818 MBINCR_VLC_BITS, 2);
1820 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1821 return AVERROR_INVALIDDATA;
1825 s->mb_skip_run += 33;
1826 } else if (code == 35) {
1827 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1828 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1829 return AVERROR_INVALIDDATA;
1831 goto eos; /* end of slice */
1833 /* otherwise, stuffing, nothing to do */
1835 s->mb_skip_run += code;
1839 if (s->mb_skip_run) {
1841 if (s->pict_type == AV_PICTURE_TYPE_I) {
1842 av_log(s->avctx, AV_LOG_ERROR,
1843 "skipped MB in I-frame at %d %d\n", s->mb_x, s->mb_y);
1844 return AVERROR_INVALIDDATA;
1849 for (i = 0; i < 12; i++)
1850 s->block_last_index[i] = -1;
1851 if (s->picture_structure == PICT_FRAME)
1852 s->mv_type = MV_TYPE_16X16;
1854 s->mv_type = MV_TYPE_FIELD;
1855 if (s->pict_type == AV_PICTURE_TYPE_P) {
1856 /* if P type, zero motion vector is implied */
1857 s->mv_dir = MV_DIR_FORWARD;
1858 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1859 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1860 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1861 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1863 /* if B type, reuse previous vectors and directions */
1864 s->mv[0][0][0] = s->last_mv[0][0][0];
1865 s->mv[0][0][1] = s->last_mv[0][0][1];
1866 s->mv[1][0][0] = s->last_mv[1][0][0];
1867 s->mv[1][0][1] = s->last_mv[1][0][1];
1872 eos: // end of slice
1873 *buf += (get_bits_count(&s->gb) - 1) / 8;
1874 ff_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1878 static int slice_decode_thread(AVCodecContext *c, void *arg)
1880 MpegEncContext *s = *(void **) arg;
1881 const uint8_t *buf = s->gb.buffer;
1882 int mb_y = s->start_mb_y;
1883 const int field_pic = s->picture_structure != PICT_FRAME;
1885 s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
1888 uint32_t start_code;
1891 ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
1893 ff_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1894 ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
1895 s->start_mb_y, s->end_mb_y, s->er.error_count);
1897 if (c->err_recognition & AV_EF_EXPLODE)
1899 if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
1900 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1902 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
1904 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1905 s->mb_x - 1, s->mb_y,
1906 ER_AC_END | ER_DC_END | ER_MV_END);
1909 if (s->mb_y == s->end_mb_y)
1913 buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
1914 mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
1915 if (s->picture_structure == PICT_BOTTOM_FIELD)
1917 if (mb_y < 0 || mb_y >= s->end_mb_y)
1918 return AVERROR_INVALIDDATA;
1923 * Handle slice ends.
1924 * @return 1 if it seems to be the last slice
1926 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
1928 Mpeg1Context *s1 = avctx->priv_data;
1929 MpegEncContext *s = &s1->mpeg_enc_ctx;
1931 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
1934 if (s->avctx->hwaccel) {
1935 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1936 av_log(avctx, AV_LOG_ERROR,
1937 "hardware accelerator failed to decode picture\n");
1940 /* end of slice reached */
1941 if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field) {
1944 ff_er_frame_end(&s->er);
1946 ff_mpv_frame_end(s);
1948 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
1949 int ret = av_frame_ref(pict, s->current_picture_ptr->f);
1952 ff_print_debug_info(s, s->current_picture_ptr);
1954 if (avctx->active_thread_type & FF_THREAD_FRAME)
1955 s->picture_number++;
1956 /* latency of 1 frame for I- and P-frames */
1957 /* XXX: use another variable than picture_number */
1958 if (s->last_picture_ptr) {
1959 int ret = av_frame_ref(pict, s->last_picture_ptr->f);
1962 ff_print_debug_info(s, s->last_picture_ptr);
1972 static int mpeg1_decode_sequence(AVCodecContext *avctx,
1973 const uint8_t *buf, int buf_size)
1975 Mpeg1Context *s1 = avctx->priv_data;
1976 MpegEncContext *s = &s1->mpeg_enc_ctx;
1980 init_get_bits(&s->gb, buf, buf_size * 8);
1982 width = get_bits(&s->gb, 12);
1983 height = get_bits(&s->gb, 12);
1984 if (width == 0 || height == 0) {
1985 av_log(avctx, AV_LOG_WARNING,
1986 "Invalid horizontal or vertical size value.\n");
1987 if (avctx->err_recognition & AV_EF_BITSTREAM)
1988 return AVERROR_INVALIDDATA;
1990 s->aspect_ratio_info = get_bits(&s->gb, 4);
1991 if (s->aspect_ratio_info == 0) {
1992 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
1993 if (avctx->err_recognition & AV_EF_BITSTREAM)
1994 return AVERROR_INVALIDDATA;
1996 s->frame_rate_index = get_bits(&s->gb, 4);
1997 if (s->frame_rate_index == 0 || s->frame_rate_index > 13) {
1998 av_log(avctx, AV_LOG_WARNING,
1999 "frame_rate_index %d is invalid\n", s->frame_rate_index);
2000 return AVERROR_INVALIDDATA;
2002 s->bit_rate = get_bits(&s->gb, 18) * 400;
2003 if (get_bits1(&s->gb) == 0) { /* marker */
2004 av_log(avctx, AV_LOG_ERROR, "Marker in sequence header missing\n");
2005 return AVERROR_INVALIDDATA;
2010 s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
2011 skip_bits(&s->gb, 1);
2014 if (get_bits1(&s->gb)) {
2015 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
2017 for (i = 0; i < 64; i++) {
2018 j = s->idsp.idct_permutation[i];
2019 v = ff_mpeg1_default_intra_matrix[i];
2020 s->intra_matrix[j] = v;
2021 s->chroma_intra_matrix[j] = v;
2024 if (get_bits1(&s->gb)) {
2025 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
2027 for (i = 0; i < 64; i++) {
2028 int j = s->idsp.idct_permutation[i];
2029 v = ff_mpeg1_default_non_intra_matrix[i];
2030 s->inter_matrix[j] = v;
2031 s->chroma_inter_matrix[j] = v;
2035 if (show_bits(&s->gb, 23) != 0) {
2036 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2037 return AVERROR_INVALIDDATA;
2040 /* We set MPEG-2 parameters so that it emulates MPEG-1. */
2041 s->progressive_sequence = 1;
2042 s->progressive_frame = 1;
2043 s->picture_structure = PICT_FRAME;
2044 s->frame_pred_frame_dct = 1;
2045 s->chroma_format = 1;
2047 s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
2048 s->out_format = FMT_MPEG1;
2049 if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
2052 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2053 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2054 s->avctx->rc_buffer_size, s->bit_rate);
2059 static int vcr2_init_sequence(AVCodecContext *avctx)
2061 Mpeg1Context *s1 = avctx->priv_data;
2062 MpegEncContext *s = &s1->mpeg_enc_ctx;
2065 /* start new MPEG-1 context decoding */
2066 s->out_format = FMT_MPEG1;
2067 if (s1->mpeg_enc_ctx_allocated) {
2068 ff_mpv_common_end(s);
2070 s->width = avctx->coded_width;
2071 s->height = avctx->coded_height;
2072 avctx->has_b_frames = 0; // true?
2075 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2077 if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
2078 avctx->idct_algo = FF_IDCT_SIMPLE;
2080 ff_mpv_idct_init(s);
2081 if ((ret = ff_mpv_common_init(s)) < 0)
2083 s1->mpeg_enc_ctx_allocated = 1;
2085 for (i = 0; i < 64; i++) {
2086 int j = s->idsp.idct_permutation[i];
2087 v = ff_mpeg1_default_intra_matrix[i];
2088 s->intra_matrix[j] = v;
2089 s->chroma_intra_matrix[j] = v;
2091 v = ff_mpeg1_default_non_intra_matrix[i];
2092 s->inter_matrix[j] = v;
2093 s->chroma_inter_matrix[j] = v;
2096 s->progressive_sequence = 1;
2097 s->progressive_frame = 1;
2098 s->picture_structure = PICT_FRAME;
2099 s->frame_pred_frame_dct = 1;
2100 s->chroma_format = 1;
2101 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
2102 s1->save_width = s->width;
2103 s1->save_height = s->height;
2104 s1->save_progressive_seq = s->progressive_sequence;
2108 static int mpeg_decode_a53_cc(AVCodecContext *avctx,
2109 const uint8_t *p, int buf_size)
2111 Mpeg1Context *s1 = avctx->priv_data;
2113 if (buf_size >= 6 &&
2114 p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
2115 p[4] == 3 && (p[5] & 0x40)) {
2116 /* extract A53 Part 4 CC data */
2117 int cc_count = p[5] & 0x1f;
2118 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2119 av_freep(&s1->a53_caption);
2120 s1->a53_caption_size = cc_count * 3;
2121 s1->a53_caption = av_malloc(s1->a53_caption_size);
2122 if (s1->a53_caption)
2123 memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
2126 } else if (buf_size >= 11 &&
2127 p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
2128 /* extract DVD CC data */
2131 // There is a caption count field in the data, but it is often
2132 // incorrect. So count the number of captions present.
2133 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2135 // Transform the DVD format into A53 Part 4 format
2137 av_freep(&s1->a53_caption);
2138 s1->a53_caption_size = cc_count * 6;
2139 s1->a53_caption = av_malloc(s1->a53_caption_size);
2140 if (s1->a53_caption) {
2141 uint8_t field1 = !!(p[4] & 0x80);
2142 uint8_t *cap = s1->a53_caption;
2144 for (i = 0; i < cc_count; i++) {
2145 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2148 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2161 static void mpeg_decode_user_data(AVCodecContext *avctx,
2162 const uint8_t *p, int buf_size)
2164 const uint8_t *buf_end = p + buf_size;
2165 Mpeg1Context *s1 = avctx->priv_data;
2167 /* we parse the DTG active format information */
2168 if (buf_end - p >= 5 &&
2169 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2177 if (buf_end - p < 1)
2180 FF_DISABLE_DEPRECATION_WARNINGS
2181 avctx->dtg_active_format = p[0] & 0x0f;
2182 FF_ENABLE_DEPRECATION_WARNINGS
2183 #endif /* FF_API_AFD */
2185 s1->afd = p[0] & 0x0f;
2187 } else if (buf_end - p >= 6 &&
2188 p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
2189 p[4] == 0x03) { // S3D_video_format_length
2190 // the 0x7F mask ignores the reserved_bit value
2191 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2193 if (S3D_video_format_type == 0x03 ||
2194 S3D_video_format_type == 0x04 ||
2195 S3D_video_format_type == 0x08 ||
2196 S3D_video_format_type == 0x23) {
2198 s1->has_stereo3d = 1;
2200 switch (S3D_video_format_type) {
2202 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
2205 s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
2208 s1->stereo3d.type = AV_STEREO3D_2D;
2211 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2215 } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
2220 static void mpeg_decode_gop(AVCodecContext *avctx,
2221 const uint8_t *buf, int buf_size)
2223 Mpeg1Context *s1 = avctx->priv_data;
2224 MpegEncContext *s = &s1->mpeg_enc_ctx;
2226 int time_code_hours, time_code_minutes;
2227 int time_code_seconds, time_code_pictures;
2230 init_get_bits(&s->gb, buf, buf_size * 8);
2232 skip_bits1(&s->gb); /* drop_frame_flag */
2234 time_code_hours = get_bits(&s->gb, 5);
2235 time_code_minutes = get_bits(&s->gb, 6);
2236 skip_bits1(&s->gb); // marker bit
2237 time_code_seconds = get_bits(&s->gb, 6);
2238 time_code_pictures = get_bits(&s->gb, 6);
2240 s1->closed_gop = get_bits1(&s->gb);
2241 /* broken_link indicate that after editing the
2242 * reference frames of the first B-Frames after GOP I-Frame
2243 * are missing (open gop) */
2244 broken_link = get_bits1(&s->gb);
2246 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2247 av_log(s->avctx, AV_LOG_DEBUG,
2248 "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
2249 time_code_hours, time_code_minutes, time_code_seconds,
2250 time_code_pictures, s1->closed_gop, broken_link);
2253 static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
2254 int *got_output, const uint8_t *buf, int buf_size)
2256 Mpeg1Context *s = avctx->priv_data;
2257 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2258 const uint8_t *buf_ptr = buf;
2259 const uint8_t *buf_end = buf + buf_size;
2260 int ret, input_size;
2261 int last_code = 0, skip_frame = 0;
2264 /* find next start code */
2265 uint32_t start_code = -1;
2266 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
2267 if (start_code > 0x1ff) {
2270 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2274 avctx->execute(avctx, slice_decode_thread,
2275 &s2->thread_context[0], NULL,
2276 s->slice_count, sizeof(void *));
2277 for (i = 0; i < s->slice_count; i++)
2278 s2->er.error_count += s2->thread_context[i]->er.error_count;
2281 ret = slice_end(avctx, picture);
2285 // FIXME: merge with the stuff in mpeg_decode_slice
2286 if (s2->last_picture_ptr || s2->low_delay)
2291 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2294 input_size = buf_end - buf_ptr;
2296 if (avctx->debug & FF_DEBUG_STARTCODE)
2297 av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %td left %d\n",
2298 start_code, buf_ptr - buf, input_size);
2300 /* prepare data for next start code */
2301 switch (start_code) {
2302 case SEQ_START_CODE:
2303 if (last_code == 0) {
2304 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2307 av_log(avctx, AV_LOG_ERROR,
2308 "ignoring SEQ_START_CODE after %X\n", last_code);
2309 if (avctx->err_recognition & AV_EF_EXPLODE)
2310 return AVERROR_INVALIDDATA;
2314 case PICTURE_START_CODE:
2315 if (s2->width <= 0 || s2->height <= 0) {
2316 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
2317 s2->width, s2->height);
2318 return AVERROR_INVALIDDATA;
2321 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
2322 !avctx->hwaccel && s->slice_count) {
2325 avctx->execute(avctx, slice_decode_thread,
2326 s2->thread_context, NULL,
2327 s->slice_count, sizeof(void *));
2328 for (i = 0; i < s->slice_count; i++)
2329 s2->er.error_count += s2->thread_context[i]->er.error_count;
2332 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2333 ret = mpeg_decode_postinit(avctx);
2335 av_log(avctx, AV_LOG_ERROR,
2336 "mpeg_decode_postinit() failure\n");
2340 /* We have a complete image: we try to decompress it. */
2341 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2344 last_code = PICTURE_START_CODE;
2346 av_log(avctx, AV_LOG_ERROR,
2347 "ignoring pic after %X\n", last_code);
2348 if (avctx->err_recognition & AV_EF_EXPLODE)
2349 return AVERROR_INVALIDDATA;
2352 case EXT_START_CODE:
2353 init_get_bits(&s2->gb, buf_ptr, input_size * 8);
2355 switch (get_bits(&s2->gb, 4)) {
2357 if (last_code == 0) {
2358 mpeg_decode_sequence_extension(s);
2360 av_log(avctx, AV_LOG_ERROR,
2361 "ignoring seq ext after %X\n", last_code);
2362 if (avctx->err_recognition & AV_EF_EXPLODE)
2363 return AVERROR_INVALIDDATA;
2367 mpeg_decode_sequence_display_extension(s);
2370 mpeg_decode_quant_matrix_extension(s2);
2373 mpeg_decode_picture_display_extension(s);
2376 if (last_code == PICTURE_START_CODE) {
2377 mpeg_decode_picture_coding_extension(s);
2379 av_log(avctx, AV_LOG_ERROR,
2380 "ignoring pic cod ext after %X\n", last_code);
2381 if (avctx->err_recognition & AV_EF_EXPLODE)
2382 return AVERROR_INVALIDDATA;
2387 case USER_START_CODE:
2388 mpeg_decode_user_data(avctx, buf_ptr, input_size);
2390 case GOP_START_CODE:
2391 if (last_code == 0) {
2392 s2->first_field = 0;
2393 mpeg_decode_gop(avctx, buf_ptr, input_size);
2396 av_log(avctx, AV_LOG_ERROR,
2397 "ignoring GOP_START_CODE after %X\n", last_code);
2398 if (avctx->err_recognition & AV_EF_EXPLODE)
2399 return AVERROR_INVALIDDATA;
2403 if (start_code >= SLICE_MIN_START_CODE &&
2404 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2405 const int field_pic = s2->picture_structure != PICT_FRAME;
2406 int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
2407 last_code = SLICE_MIN_START_CODE;
2409 if (s2->picture_structure == PICT_BOTTOM_FIELD)
2412 if (mb_y >= s2->mb_height) {
2413 av_log(s2->avctx, AV_LOG_ERROR,
2414 "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2415 return AVERROR_INVALIDDATA;
2418 if (!s2->last_picture_ptr) {
2419 /* Skip B-frames if we do not have reference frames and
2420 * GOP is not closed. */
2421 if (s2->pict_type == AV_PICTURE_TYPE_B) {
2422 if (!s->closed_gop) {
2428 if (s2->pict_type == AV_PICTURE_TYPE_I)
2430 if (!s2->next_picture_ptr) {
2431 /* Skip P-frames if we do not have a reference frame or
2432 * we have an invalid header. */
2433 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
2438 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
2439 s2->pict_type == AV_PICTURE_TYPE_B) ||
2440 (avctx->skip_frame >= AVDISCARD_NONKEY &&
2441 s2->pict_type != AV_PICTURE_TYPE_I) ||
2442 avctx->skip_frame >= AVDISCARD_ALL) {
2447 if (!s->mpeg_enc_ctx_allocated)
2450 if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2451 if (mb_y < avctx->skip_top ||
2452 mb_y >= s2->mb_height - avctx->skip_bottom)
2456 if (!s2->pict_type) {
2457 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2458 if (avctx->err_recognition & AV_EF_EXPLODE)
2459 return AVERROR_INVALIDDATA;
2463 if (s->first_slice) {
2466 if ((ret = mpeg_field_start(s2, buf, buf_size)) < 0)
2469 if (!s2->current_picture_ptr) {
2470 av_log(avctx, AV_LOG_ERROR,
2471 "current_picture not initialized\n");
2472 return AVERROR_INVALIDDATA;
2476 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2478 int threshold = (s2->mb_height * s->slice_count +
2479 s2->slice_context_count / 2) /
2480 s2->slice_context_count;
2481 if (threshold <= mb_y) {
2482 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2484 thread_context->start_mb_y = mb_y;
2485 thread_context->end_mb_y = s2->mb_height;
2486 if (s->slice_count) {
2487 s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
2488 ret = ff_update_duplicate_context(thread_context, s2);
2492 init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
2495 buf_ptr += 2; // FIXME add minimum number of bytes per slice
2497 ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2501 if (avctx->err_recognition & AV_EF_EXPLODE)
2503 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2504 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2505 s2->resync_mb_y, s2->mb_x, s2->mb_y,
2506 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
2508 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2509 s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
2510 ER_AC_END | ER_DC_END | ER_MV_END);
2519 static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
2520 int *got_output, AVPacket *avpkt)
2522 const uint8_t *buf = avpkt->data;
2523 int buf_size = avpkt->size;
2524 Mpeg1Context *s = avctx->priv_data;
2525 AVFrame *picture = data;
2526 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2527 ff_dlog(avctx, "fill_buffer\n");
2529 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2530 /* special case for last picture */
2531 if (s2->low_delay == 0 && s2->next_picture_ptr) {
2532 int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
2536 s2->next_picture_ptr = NULL;
2543 if (s2->avctx->flags & AV_CODEC_FLAG_TRUNCATED) {
2544 int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
2547 if (ff_combine_frame(&s2->parse_context, next,
2548 (const uint8_t **) &buf, &buf_size) < 0)
2552 if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
2553 vcr2_init_sequence(avctx);
2557 if (avctx->extradata && !s->extradata_decoded) {
2558 int ret = decode_chunks(avctx, picture, got_output,
2559 avctx->extradata, avctx->extradata_size);
2560 s->extradata_decoded = 1;
2561 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
2565 return decode_chunks(avctx, picture, got_output, buf, buf_size);
2568 static void flush(AVCodecContext *avctx)
2570 Mpeg1Context *s = avctx->priv_data;
2575 ff_mpeg_flush(avctx);
2578 static av_cold int mpeg_decode_end(AVCodecContext *avctx)
2580 Mpeg1Context *s = avctx->priv_data;
2582 if (s->mpeg_enc_ctx_allocated)
2583 ff_mpv_common_end(&s->mpeg_enc_ctx);
2584 av_freep(&s->a53_caption);
2588 AVCodec ff_mpeg1video_decoder = {
2589 .name = "mpeg1video",
2590 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2591 .type = AVMEDIA_TYPE_VIDEO,
2592 .id = AV_CODEC_ID_MPEG1VIDEO,
2593 .priv_data_size = sizeof(Mpeg1Context),
2594 .init = mpeg_decode_init,
2595 .close = mpeg_decode_end,
2596 .decode = mpeg_decode_frame,
2597 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2598 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
2599 AV_CODEC_CAP_SLICE_THREADS,
2601 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2604 AVCodec ff_mpeg2video_decoder = {
2605 .name = "mpeg2video",
2606 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2607 .type = AVMEDIA_TYPE_VIDEO,
2608 .id = AV_CODEC_ID_MPEG2VIDEO,
2609 .priv_data_size = sizeof(Mpeg1Context),
2610 .init = mpeg_decode_init,
2611 .close = mpeg_decode_end,
2612 .decode = mpeg_decode_frame,
2613 .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
2614 AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY |
2615 AV_CODEC_CAP_SLICE_THREADS,
2617 .profiles = NULL_IF_CONFIG_SMALL(ff_mpeg2_video_profiles),