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"
46 #include "xvmc_internal.h"
48 typedef struct Mpeg1Context {
49 MpegEncContext mpeg_enc_ctx;
50 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
51 int repeat_field; /* true if we must repeat the field */
52 AVPanScan pan_scan; /* some temporary storage for the panscan */
61 int save_width, save_height, save_progressive_seq;
62 AVRational frame_rate_ext; /* MPEG-2 specific framerate modificator */
63 int sync; /* Did we reach a sync point like a GOP/SEQ/KEYFrame? */
64 int closed_gop; /* GOP is closed */
66 int extradata_decoded;
69 #define MB_TYPE_ZERO_MV 0x20000000
71 static const uint32_t ptype2mb_type[7] = {
73 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
75 MB_TYPE_L0 | MB_TYPE_CBP,
76 MB_TYPE_QUANT | MB_TYPE_INTRA,
77 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
78 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
81 static const uint32_t btype2mb_type[11] = {
84 MB_TYPE_L1 | MB_TYPE_CBP,
86 MB_TYPE_L0 | MB_TYPE_CBP,
88 MB_TYPE_L0L1 | MB_TYPE_CBP,
89 MB_TYPE_QUANT | MB_TYPE_INTRA,
90 MB_TYPE_QUANT | MB_TYPE_L1 | MB_TYPE_CBP,
91 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
92 MB_TYPE_QUANT | MB_TYPE_L0L1 | MB_TYPE_CBP,
95 static const uint8_t non_linear_qscale[32] = {
96 0, 1, 2, 3, 4, 5, 6, 7,
97 8, 10, 12, 14, 16, 18, 20, 22,
98 24, 28, 32, 36, 40, 44, 48, 52,
99 56, 64, 72, 80, 88, 96, 104, 112,
102 /* as H.263, but only 17 codes */
103 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
105 int code, sign, val, shift;
107 code = get_vlc2(&s->gb, ff_mv_vlc.table, MV_VLC_BITS, 2);
113 sign = get_bits1(&s->gb);
117 val = (val - 1) << shift;
118 val |= get_bits(&s->gb, shift);
125 /* modulo decoding */
126 return sign_extend(val, 5 + shift);
129 #define check_scantable_index(ctx, x) \
132 av_log(ctx->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
133 ctx->mb_x, ctx->mb_y); \
134 return AVERROR_INVALIDDATA; \
138 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
139 int16_t *block, int n)
141 int level, dc, diff, i, j, run;
143 RLTable *rl = &ff_rl_mpeg1;
144 uint8_t *const scantable = s->intra_scantable.permutated;
145 const uint16_t *quant_matrix = s->intra_matrix;
146 const int qscale = s->qscale;
149 component = (n <= 3 ? 0 : n - 4 + 1);
150 diff = decode_dc(&s->gb, component);
153 dc = s->last_dc[component];
155 s->last_dc[component] = dc;
156 block[0] = dc * quant_matrix[0];
157 av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
160 OPEN_READER(re, &s->gb);
161 /* now quantify & encode AC coefficients */
163 UPDATE_CACHE(re, &s->gb);
164 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
169 } else if (level != 0) {
171 check_scantable_index(s, i);
173 level = (level * qscale * quant_matrix[j]) >> 4;
174 level = (level - 1) | 1;
175 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
176 SHOW_SBITS(re, &s->gb, 1);
177 LAST_SKIP_BITS(re, &s->gb, 1);
180 run = SHOW_UBITS(re, &s->gb, 6) + 1;
181 LAST_SKIP_BITS(re, &s->gb, 6);
182 UPDATE_CACHE(re, &s->gb);
183 level = SHOW_SBITS(re, &s->gb, 8);
184 SKIP_BITS(re, &s->gb, 8);
186 level = SHOW_UBITS(re, &s->gb, 8) - 256;
187 LAST_SKIP_BITS(re, &s->gb, 8);
188 } else if (level == 0) {
189 level = SHOW_UBITS(re, &s->gb, 8);
190 LAST_SKIP_BITS(re, &s->gb, 8);
193 check_scantable_index(s, i);
197 level = (level * qscale * quant_matrix[j]) >> 4;
198 level = (level - 1) | 1;
201 level = (level * qscale * quant_matrix[j]) >> 4;
202 level = (level - 1) | 1;
208 CLOSE_READER(re, &s->gb);
210 s->block_last_index[n] = i;
214 int ff_mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
216 return mpeg1_decode_block_intra(s, block, n);
219 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
220 int16_t *block, int n)
222 int level, i, j, run;
223 RLTable *rl = &ff_rl_mpeg1;
224 uint8_t *const scantable = s->intra_scantable.permutated;
225 const uint16_t *quant_matrix = s->inter_matrix;
226 const int qscale = s->qscale;
229 OPEN_READER(re, &s->gb);
231 // special case for first coefficient, no need to add second VLC table
232 UPDATE_CACHE(re, &s->gb);
233 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
234 level = (3 * qscale * quant_matrix[0]) >> 5;
235 level = (level - 1) | 1;
236 if (GET_CACHE(re, &s->gb) & 0x40000000)
240 SKIP_BITS(re, &s->gb, 2);
241 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
244 /* now quantify & encode AC coefficients */
246 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
251 check_scantable_index(s, i);
253 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
254 level = (level - 1) | 1;
255 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
256 SHOW_SBITS(re, &s->gb, 1);
257 SKIP_BITS(re, &s->gb, 1);
260 run = SHOW_UBITS(re, &s->gb, 6) + 1;
261 LAST_SKIP_BITS(re, &s->gb, 6);
262 UPDATE_CACHE(re, &s->gb);
263 level = SHOW_SBITS(re, &s->gb, 8);
264 SKIP_BITS(re, &s->gb, 8);
266 level = SHOW_UBITS(re, &s->gb, 8) - 256;
267 SKIP_BITS(re, &s->gb, 8);
268 } else if (level == 0) {
269 level = SHOW_UBITS(re, &s->gb, 8);
270 SKIP_BITS(re, &s->gb, 8);
273 check_scantable_index(s, i);
277 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
278 level = (level - 1) | 1;
281 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
282 level = (level - 1) | 1;
287 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
289 UPDATE_CACHE(re, &s->gb);
292 LAST_SKIP_BITS(re, &s->gb, 2);
293 CLOSE_READER(re, &s->gb);
295 s->block_last_index[n] = i;
299 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s,
300 int16_t *block, int n)
302 int level, i, j, run;
303 RLTable *rl = &ff_rl_mpeg1;
304 uint8_t *const scantable = s->intra_scantable.permutated;
305 const int qscale = s->qscale;
308 OPEN_READER(re, &s->gb);
310 // Special case for first coefficient, no need to add second VLC table.
311 UPDATE_CACHE(re, &s->gb);
312 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
313 level = (3 * qscale) >> 1;
314 level = (level - 1) | 1;
315 if (GET_CACHE(re, &s->gb) & 0x40000000)
319 SKIP_BITS(re, &s->gb, 2);
320 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
324 /* now quantify & encode AC coefficients */
326 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
331 check_scantable_index(s, i);
333 level = ((level * 2 + 1) * qscale) >> 1;
334 level = (level - 1) | 1;
335 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
336 SHOW_SBITS(re, &s->gb, 1);
337 SKIP_BITS(re, &s->gb, 1);
340 run = SHOW_UBITS(re, &s->gb, 6) + 1;
341 LAST_SKIP_BITS(re, &s->gb, 6);
342 UPDATE_CACHE(re, &s->gb);
343 level = SHOW_SBITS(re, &s->gb, 8);
344 SKIP_BITS(re, &s->gb, 8);
346 level = SHOW_UBITS(re, &s->gb, 8) - 256;
347 SKIP_BITS(re, &s->gb, 8);
348 } else if (level == 0) {
349 level = SHOW_UBITS(re, &s->gb, 8);
350 SKIP_BITS(re, &s->gb, 8);
353 check_scantable_index(s, i);
357 level = ((level * 2 + 1) * qscale) >> 1;
358 level = (level - 1) | 1;
361 level = ((level * 2 + 1) * qscale) >> 1;
362 level = (level - 1) | 1;
367 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
369 UPDATE_CACHE(re, &s->gb);
372 LAST_SKIP_BITS(re, &s->gb, 2);
373 CLOSE_READER(re, &s->gb);
375 s->block_last_index[n] = i;
379 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
380 int16_t *block, int n)
382 int level, i, j, run;
383 RLTable *rl = &ff_rl_mpeg1;
384 uint8_t *const scantable = s->intra_scantable.permutated;
385 const uint16_t *quant_matrix;
386 const int qscale = s->qscale;
392 OPEN_READER(re, &s->gb);
395 quant_matrix = s->inter_matrix;
397 quant_matrix = s->chroma_inter_matrix;
399 // Special case for first coefficient, no need to add second VLC table.
400 UPDATE_CACHE(re, &s->gb);
401 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
402 level = (3 * qscale * quant_matrix[0]) >> 5;
403 if (GET_CACHE(re, &s->gb) & 0x40000000)
408 SKIP_BITS(re, &s->gb, 2);
409 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
413 /* now quantify & encode AC coefficients */
415 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
420 check_scantable_index(s, i);
422 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
423 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
424 SHOW_SBITS(re, &s->gb, 1);
425 SKIP_BITS(re, &s->gb, 1);
428 run = SHOW_UBITS(re, &s->gb, 6) + 1;
429 LAST_SKIP_BITS(re, &s->gb, 6);
430 UPDATE_CACHE(re, &s->gb);
431 level = SHOW_SBITS(re, &s->gb, 12);
432 SKIP_BITS(re, &s->gb, 12);
435 check_scantable_index(s, i);
438 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
441 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
447 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
449 UPDATE_CACHE(re, &s->gb);
452 LAST_SKIP_BITS(re, &s->gb, 2);
453 CLOSE_READER(re, &s->gb);
455 block[63] ^= (mismatch & 1);
457 s->block_last_index[n] = i;
461 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
462 int16_t *block, int n)
464 int level, i, j, run;
465 RLTable *rl = &ff_rl_mpeg1;
466 uint8_t *const scantable = s->intra_scantable.permutated;
467 const int qscale = s->qscale;
468 OPEN_READER(re, &s->gb);
471 // special case for first coefficient, no need to add second VLC table
472 UPDATE_CACHE(re, &s->gb);
473 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
474 level = (3 * qscale) >> 1;
475 if (GET_CACHE(re, &s->gb) & 0x40000000)
479 SKIP_BITS(re, &s->gb, 2);
480 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
484 /* now quantify & encode AC coefficients */
486 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
490 check_scantable_index(s, i);
492 level = ((level * 2 + 1) * qscale) >> 1;
493 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
494 SHOW_SBITS(re, &s->gb, 1);
495 SKIP_BITS(re, &s->gb, 1);
498 run = SHOW_UBITS(re, &s->gb, 6) + 1;
499 LAST_SKIP_BITS(re, &s->gb, 6);
500 UPDATE_CACHE(re, &s->gb);
501 level = SHOW_SBITS(re, &s->gb, 12);
502 SKIP_BITS(re, &s->gb, 12);
505 check_scantable_index(s, i);
508 level = ((-level * 2 + 1) * qscale) >> 1;
511 level = ((level * 2 + 1) * qscale) >> 1;
516 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
518 UPDATE_CACHE(re, &s->gb);
521 LAST_SKIP_BITS(re, &s->gb, 2);
522 CLOSE_READER(re, &s->gb);
523 s->block_last_index[n] = i;
527 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
528 int16_t *block, int n)
530 int level, dc, diff, i, j, run;
533 uint8_t *const scantable = s->intra_scantable.permutated;
534 const uint16_t *quant_matrix;
535 const int qscale = s->qscale;
540 quant_matrix = s->intra_matrix;
543 quant_matrix = s->chroma_intra_matrix;
544 component = (n & 1) + 1;
546 diff = decode_dc(&s->gb, component);
549 dc = s->last_dc[component];
551 s->last_dc[component] = dc;
552 block[0] = dc << (3 - s->intra_dc_precision);
553 av_dlog(s->avctx, "dc=%d\n", block[0]);
554 mismatch = block[0] ^ 1;
556 if (s->intra_vlc_format)
562 OPEN_READER(re, &s->gb);
563 /* now quantify & encode AC coefficients */
565 UPDATE_CACHE(re, &s->gb);
566 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
571 } else if (level != 0) {
573 check_scantable_index(s, i);
575 level = (level * qscale * quant_matrix[j]) >> 4;
576 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
577 SHOW_SBITS(re, &s->gb, 1);
578 LAST_SKIP_BITS(re, &s->gb, 1);
581 run = SHOW_UBITS(re, &s->gb, 6) + 1;
582 LAST_SKIP_BITS(re, &s->gb, 6);
583 UPDATE_CACHE(re, &s->gb);
584 level = SHOW_SBITS(re, &s->gb, 12);
585 SKIP_BITS(re, &s->gb, 12);
587 check_scantable_index(s, i);
590 level = (-level * qscale * quant_matrix[j]) >> 4;
593 level = (level * qscale * quant_matrix[j]) >> 4;
600 CLOSE_READER(re, &s->gb);
602 block[63] ^= mismatch & 1;
604 s->block_last_index[n] = i;
608 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
609 int16_t *block, int n)
611 int level, dc, diff, i, j, run;
614 uint8_t *const scantable = s->intra_scantable.permutated;
615 const uint16_t *quant_matrix;
616 const int qscale = s->qscale;
620 quant_matrix = s->intra_matrix;
623 quant_matrix = s->chroma_intra_matrix;
624 component = (n & 1) + 1;
626 diff = decode_dc(&s->gb, component);
629 dc = s->last_dc[component];
631 s->last_dc[component] = dc;
632 block[0] = dc << (3 - s->intra_dc_precision);
634 if (s->intra_vlc_format)
640 OPEN_READER(re, &s->gb);
641 /* now quantify & encode AC coefficients */
643 UPDATE_CACHE(re, &s->gb);
644 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
649 } else if (level != 0) {
651 check_scantable_index(s, i);
653 level = (level * qscale * quant_matrix[j]) >> 4;
654 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
655 SHOW_SBITS(re, &s->gb, 1);
656 LAST_SKIP_BITS(re, &s->gb, 1);
659 run = SHOW_UBITS(re, &s->gb, 6) + 1;
660 LAST_SKIP_BITS(re, &s->gb, 6);
661 UPDATE_CACHE(re, &s->gb);
662 level = SHOW_SBITS(re, &s->gb, 12);
663 SKIP_BITS(re, &s->gb, 12);
665 check_scantable_index(s, i);
668 level = (-level * qscale * quant_matrix[j]) >> 4;
671 level = (level * qscale * quant_matrix[j]) >> 4;
677 CLOSE_READER(re, &s->gb);
680 s->block_last_index[n] = i;
684 /******************************************/
687 static inline int get_dmv(MpegEncContext *s)
689 if (get_bits1(&s->gb))
690 return 1 - (get_bits1(&s->gb) << 1);
695 static inline int get_qscale(MpegEncContext *s)
697 int qscale = get_bits(&s->gb, 5);
699 return non_linear_qscale[qscale];
704 /* motion type (for MPEG-2) */
710 static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
712 int i, j, k, cbp, val, mb_type, motion_type;
713 const int mb_block_count = 4 + (1 << s->chroma_format);
715 av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
717 assert(s->mb_skipped == 0);
719 if (s->mb_skip_run-- != 0) {
720 if (s->pict_type == AV_PICTURE_TYPE_P) {
722 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
723 MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
728 mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
730 // FIXME not sure if this is allowed in MPEG at all
731 mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
732 if (IS_INTRA(mb_type))
734 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
735 mb_type | MB_TYPE_SKIP;
736 // assert(s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1] & (MB_TYPE_16x16 | MB_TYPE_16x8));
738 if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
745 switch (s->pict_type) {
747 case AV_PICTURE_TYPE_I:
748 if (get_bits1(&s->gb) == 0) {
749 if (get_bits1(&s->gb) == 0) {
750 av_log(s->avctx, AV_LOG_ERROR,
751 "invalid mb type in I Frame at %d %d\n",
755 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
757 mb_type = MB_TYPE_INTRA;
760 case AV_PICTURE_TYPE_P:
761 mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
763 av_log(s->avctx, AV_LOG_ERROR,
764 "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
767 mb_type = ptype2mb_type[mb_type];
769 case AV_PICTURE_TYPE_B:
770 mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
772 av_log(s->avctx, AV_LOG_ERROR,
773 "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
776 mb_type = btype2mb_type[mb_type];
779 av_dlog(s->avctx, "mb_type=%x\n", mb_type);
780 // motion_type = 0; /* avoid warning */
781 if (IS_INTRA(mb_type)) {
782 s->bdsp.clear_blocks(s->block[0]);
784 if (!s->chroma_y_shift)
785 s->bdsp.clear_blocks(s->block[6]);
787 /* compute DCT type */
788 // FIXME: add an interlaced_dct coded var?
789 if (s->picture_structure == PICT_FRAME &&
790 !s->frame_pred_frame_dct)
791 s->interlaced_dct = get_bits1(&s->gb);
793 if (IS_QUANT(mb_type))
794 s->qscale = get_qscale(s);
796 if (s->concealment_motion_vectors) {
797 /* just parse them */
798 if (s->picture_structure != PICT_FRAME)
799 skip_bits1(&s->gb); /* field select */
802 s->last_mv[0][0][0] =
803 s->last_mv[0][1][0] = mpeg_decode_motion(s, s->mpeg_f_code[0][0],
804 s->last_mv[0][0][0]);
806 s->last_mv[0][0][1] =
807 s->last_mv[0][1][1] = mpeg_decode_motion(s, s->mpeg_f_code[0][1],
808 s->last_mv[0][0][1]);
810 skip_bits1(&s->gb); /* marker */
812 /* reset mv prediction */
813 memset(s->last_mv, 0, sizeof(s->last_mv));
817 FF_DISABLE_DEPRECATION_WARNINGS
818 // if 1, we memcpy blocks in xvmcvideo
819 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
820 ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
821 FF_ENABLE_DEPRECATION_WARNINGS
822 #endif /* FF_API_XVMC */
824 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
825 if (s->flags2 & CODEC_FLAG2_FAST) {
826 for (i = 0; i < 6; i++)
827 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
829 for (i = 0; i < mb_block_count; i++)
830 if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
834 for (i = 0; i < 6; i++)
835 if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
839 if (mb_type & MB_TYPE_ZERO_MV) {
840 assert(mb_type & MB_TYPE_CBP);
842 s->mv_dir = MV_DIR_FORWARD;
843 if (s->picture_structure == PICT_FRAME) {
844 if (!s->frame_pred_frame_dct)
845 s->interlaced_dct = get_bits1(&s->gb);
846 s->mv_type = MV_TYPE_16X16;
848 s->mv_type = MV_TYPE_FIELD;
849 mb_type |= MB_TYPE_INTERLACED;
850 s->field_select[0][0] = s->picture_structure - 1;
853 if (IS_QUANT(mb_type))
854 s->qscale = get_qscale(s);
856 s->last_mv[0][0][0] = 0;
857 s->last_mv[0][0][1] = 0;
858 s->last_mv[0][1][0] = 0;
859 s->last_mv[0][1][1] = 0;
863 assert(mb_type & MB_TYPE_L0L1);
864 // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
865 /* get additional motion vector type */
866 if (s->frame_pred_frame_dct) {
867 motion_type = MT_FRAME;
869 motion_type = get_bits(&s->gb, 2);
870 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
871 s->interlaced_dct = get_bits1(&s->gb);
874 if (IS_QUANT(mb_type))
875 s->qscale = get_qscale(s);
878 s->mv_dir = (mb_type >> 13) & 3;
879 av_dlog(s->avctx, "motion_type=%d\n", motion_type);
880 switch (motion_type) {
881 case MT_FRAME: /* or MT_16X8 */
882 if (s->picture_structure == PICT_FRAME) {
883 mb_type |= MB_TYPE_16x16;
884 s->mv_type = MV_TYPE_16X16;
885 for (i = 0; i < 2; i++) {
886 if (USES_LIST(mb_type, i)) {
889 s->last_mv[i][0][0] =
890 s->last_mv[i][1][0] =
891 mpeg_decode_motion(s, s->mpeg_f_code[i][0],
892 s->last_mv[i][0][0]);
894 s->last_mv[i][0][1] =
895 s->last_mv[i][1][1] =
896 mpeg_decode_motion(s, s->mpeg_f_code[i][1],
897 s->last_mv[i][0][1]);
898 /* full_pel: only for MPEG-1 */
899 if (s->full_pel[i]) {
900 s->mv[i][0][0] <<= 1;
901 s->mv[i][0][1] <<= 1;
906 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
907 s->mv_type = MV_TYPE_16X8;
908 for (i = 0; i < 2; i++) {
909 if (USES_LIST(mb_type, i)) {
911 for (j = 0; j < 2; j++) {
912 s->field_select[i][j] = get_bits1(&s->gb);
913 for (k = 0; k < 2; k++) {
914 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
915 s->last_mv[i][j][k]);
916 s->last_mv[i][j][k] = val;
917 s->mv[i][j][k] = val;
925 s->mv_type = MV_TYPE_FIELD;
926 if (s->picture_structure == PICT_FRAME) {
927 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
928 for (i = 0; i < 2; i++) {
929 if (USES_LIST(mb_type, i)) {
930 for (j = 0; j < 2; j++) {
931 s->field_select[i][j] = get_bits1(&s->gb);
932 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
933 s->last_mv[i][j][0]);
934 s->last_mv[i][j][0] = val;
935 s->mv[i][j][0] = val;
936 av_dlog(s->avctx, "fmx=%d\n", val);
937 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
938 s->last_mv[i][j][1] >> 1);
939 s->last_mv[i][j][1] = val << 1;
940 s->mv[i][j][1] = val;
941 av_dlog(s->avctx, "fmy=%d\n", val);
946 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
947 for (i = 0; i < 2; i++) {
948 if (USES_LIST(mb_type, i)) {
949 s->field_select[i][0] = get_bits1(&s->gb);
950 for (k = 0; k < 2; k++) {
951 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
952 s->last_mv[i][0][k]);
953 s->last_mv[i][0][k] = val;
954 s->last_mv[i][1][k] = val;
955 s->mv[i][0][k] = val;
962 s->mv_type = MV_TYPE_DMV;
963 for (i = 0; i < 2; i++) {
964 if (USES_LIST(mb_type, i)) {
965 int dmx, dmy, mx, my, m;
966 const int my_shift = s->picture_structure == PICT_FRAME;
968 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
969 s->last_mv[i][0][0]);
970 s->last_mv[i][0][0] = mx;
971 s->last_mv[i][1][0] = mx;
973 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
974 s->last_mv[i][0][1] >> my_shift);
978 s->last_mv[i][0][1] = my << my_shift;
979 s->last_mv[i][1][1] = my << my_shift;
983 s->mv[i][1][0] = mx; // not used
984 s->mv[i][1][1] = my; // not used
986 if (s->picture_structure == PICT_FRAME) {
987 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
989 // m = 1 + 2 * s->top_field_first;
990 m = s->top_field_first ? 1 : 3;
992 /* top -> top pred */
993 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
994 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
996 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
997 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
999 mb_type |= MB_TYPE_16x16;
1001 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1002 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1003 if (s->picture_structure == PICT_TOP_FIELD)
1012 av_log(s->avctx, AV_LOG_ERROR,
1013 "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1019 if (HAS_CBP(mb_type)) {
1020 s->bdsp.clear_blocks(s->block[0]);
1022 cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1023 if (mb_block_count > 6) {
1024 cbp <<= mb_block_count - 6;
1025 cbp |= get_bits(&s->gb, mb_block_count - 6);
1026 s->bdsp.clear_blocks(s->block[6]);
1029 av_log(s->avctx, AV_LOG_ERROR,
1030 "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
1035 FF_DISABLE_DEPRECATION_WARNINGS
1036 // if 1, we memcpy blocks in xvmcvideo
1037 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1038 ff_xvmc_pack_pblocks(s, cbp);
1039 FF_ENABLE_DEPRECATION_WARNINGS
1040 #endif /* FF_API_XVMC */
1042 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1043 if (s->flags2 & CODEC_FLAG2_FAST) {
1044 for (i = 0; i < 6; i++) {
1046 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
1048 s->block_last_index[i] = -1;
1052 cbp <<= 12 - mb_block_count;
1054 for (i = 0; i < mb_block_count; i++) {
1055 if (cbp & (1 << 11)) {
1056 if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
1059 s->block_last_index[i] = -1;
1065 if (s->flags2 & CODEC_FLAG2_FAST) {
1066 for (i = 0; i < 6; i++) {
1068 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
1070 s->block_last_index[i] = -1;
1074 for (i = 0; i < 6; i++) {
1076 if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
1079 s->block_last_index[i] = -1;
1086 for (i = 0; i < 12; i++)
1087 s->block_last_index[i] = -1;
1091 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
1096 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1098 Mpeg1Context *s = avctx->priv_data;
1099 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1101 ff_mpv_decode_defaults(s2);
1103 s->mpeg_enc_ctx.avctx = avctx;
1104 s->mpeg_enc_ctx.flags = avctx->flags;
1105 s->mpeg_enc_ctx.flags2 = avctx->flags2;
1107 /* we need some permutation to store matrices,
1108 * until the decoder sets the real permutation. */
1109 ff_mpv_idct_init(s2);
1110 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1111 ff_mpeg12_init_vlcs();
1113 s->mpeg_enc_ctx_allocated = 0;
1114 s->mpeg_enc_ctx.picture_number = 0;
1115 s->repeat_field = 0;
1116 s->mpeg_enc_ctx.codec_id = avctx->codec->id;
1117 avctx->color_range = AVCOL_RANGE_MPEG;
1118 if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
1119 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1121 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1125 static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
1126 const AVCodecContext *avctx_from)
1128 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1129 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1132 if (avctx == avctx_from ||
1133 !ctx_from->mpeg_enc_ctx_allocated ||
1134 !s1->context_initialized)
1137 err = ff_mpeg_update_thread_context(avctx, avctx_from);
1141 if (!ctx->mpeg_enc_ctx_allocated)
1142 memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
1144 if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
1145 s->picture_number++;
1150 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1151 const uint8_t *new_perm)
1153 uint16_t temp_matrix[64];
1156 memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
1158 for (i = 0; i < 64; i++)
1159 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1163 static const enum AVPixelFormat pixfmt_xvmc_mpg2_420[] = {
1164 AV_PIX_FMT_XVMC_MPEG2_IDCT,
1165 AV_PIX_FMT_XVMC_MPEG2_MC,
1168 #endif /* FF_API_XVMC */
1170 static const enum AVPixelFormat mpeg12_hwaccel_pixfmt_list_420[] = {
1171 #if CONFIG_MPEG2_DXVA2_HWACCEL
1172 AV_PIX_FMT_DXVA2_VLD,
1174 #if CONFIG_MPEG2_VAAPI_HWACCEL
1175 AV_PIX_FMT_VAAPI_VLD,
1177 #if CONFIG_MPEG1_VDPAU_HWACCEL | CONFIG_MPEG2_VDPAU_HWACCEL
1184 static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
1186 Mpeg1Context *s1 = avctx->priv_data;
1187 MpegEncContext *s = &s1->mpeg_enc_ctx;
1190 FF_DISABLE_DEPRECATION_WARNINGS
1191 if (avctx->xvmc_acceleration)
1192 return ff_get_format(avctx, pixfmt_xvmc_mpg2_420);
1193 FF_ENABLE_DEPRECATION_WARNINGS
1194 #endif /* FF_API_XVMC */
1196 if (s->chroma_format < 2)
1197 return ff_get_format(avctx, mpeg12_hwaccel_pixfmt_list_420);
1198 else if (s->chroma_format == 2)
1199 return AV_PIX_FMT_YUV422P;
1201 return AV_PIX_FMT_YUV444P;
1204 /* Call this function when we know all parameters.
1205 * It may be called in different places for MPEG-1 and MPEG-2. */
1206 static int mpeg_decode_postinit(AVCodecContext *avctx)
1208 Mpeg1Context *s1 = avctx->priv_data;
1209 MpegEncContext *s = &s1->mpeg_enc_ctx;
1210 uint8_t old_permutation[64];
1213 if ((s1->mpeg_enc_ctx_allocated == 0) ||
1214 avctx->coded_width != s->width ||
1215 avctx->coded_height != s->height ||
1216 s1->save_width != s->width ||
1217 s1->save_height != s->height ||
1218 s1->save_aspect_info != s->aspect_ratio_info ||
1219 s1->save_progressive_seq != s->progressive_sequence ||
1221 if (s1->mpeg_enc_ctx_allocated) {
1222 ParseContext pc = s->parse_context;
1223 s->parse_context.buffer = 0;
1224 ff_mpv_common_end(s);
1225 s->parse_context = pc;
1228 if ((s->width == 0) || (s->height == 0))
1231 ret = ff_set_dimensions(avctx, s->width, s->height);
1235 avctx->bit_rate = s->bit_rate;
1236 s1->save_aspect_info = s->aspect_ratio_info;
1237 s1->save_width = s->width;
1238 s1->save_height = s->height;
1239 s1->save_progressive_seq = s->progressive_sequence;
1241 /* low_delay may be forced, in this case we will have B-frames
1242 * that behave like P-frames. */
1243 avctx->has_b_frames = !s->low_delay;
1245 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1247 avctx->time_base.den = ff_mpeg12_frame_rate_tab[s->frame_rate_index].num;
1248 avctx->time_base.num = ff_mpeg12_frame_rate_tab[s->frame_rate_index].den;
1250 avctx->sample_aspect_ratio = av_d2q(1.0 / ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1251 avctx->ticks_per_frame = 1;
1254 av_reduce(&s->avctx->time_base.den,
1255 &s->avctx->time_base.num,
1256 ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num * 2,
1257 ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1259 avctx->ticks_per_frame = 2;
1261 if (s->aspect_ratio_info > 1) {
1263 av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1264 (AVRational) { s1->pan_scan.width,
1265 s1->pan_scan.height }),
1266 (AVRational) { s->width, s->height });
1268 /* We ignore the spec here and guess a bit as reality does not
1269 * match the spec, see for example res_change_ffmpeg_aspect.ts
1270 * and sequence-display-aspect.mpg.
1271 * issue1613, 621, 562 */
1272 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1273 (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
1274 av_cmp_q(dar, (AVRational) { 16, 9 }))) {
1275 s->avctx->sample_aspect_ratio =
1276 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1277 (AVRational) { s->width, s->height });
1279 s->avctx->sample_aspect_ratio =
1280 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1281 (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
1282 // issue1613 4/3 16/9 -> 16/9
1283 // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1284 // widescreen-issue562.mpg 4/3 16/9 -> 16/9
1285 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1286 av_dlog(avctx, "A %d/%d\n",
1287 ff_mpeg2_aspect[s->aspect_ratio_info].num,
1288 ff_mpeg2_aspect[s->aspect_ratio_info].den);
1289 av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
1290 s->avctx->sample_aspect_ratio.den);
1293 s->avctx->sample_aspect_ratio =
1294 ff_mpeg2_aspect[s->aspect_ratio_info];
1298 ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
1300 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1301 // until then pix_fmt may be changed right after codec init
1303 if ((avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT ||
1304 avctx->hwaccel) && avctx->idct_algo == FF_IDCT_AUTO)
1306 if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
1307 #endif /* FF_API_XVMC */
1308 avctx->idct_algo = FF_IDCT_SIMPLE;
1310 /* Quantization matrices may need reordering
1311 * if DCT permutation is changed. */
1312 memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
1314 ff_mpv_idct_init(s);
1315 if (ff_mpv_common_init(s) < 0)
1318 quant_matrix_rebuild(s->intra_matrix, old_permutation, s->idsp.idct_permutation);
1319 quant_matrix_rebuild(s->inter_matrix, old_permutation, s->idsp.idct_permutation);
1320 quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
1321 quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
1323 s1->mpeg_enc_ctx_allocated = 1;
1328 static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
1331 Mpeg1Context *s1 = avctx->priv_data;
1332 MpegEncContext *s = &s1->mpeg_enc_ctx;
1333 int ref, f_code, vbv_delay;
1335 init_get_bits(&s->gb, buf, buf_size * 8);
1337 ref = get_bits(&s->gb, 10); /* temporal ref */
1338 s->pict_type = get_bits(&s->gb, 3);
1339 if (s->pict_type == 0 || s->pict_type > 3)
1342 vbv_delay = get_bits(&s->gb, 16);
1343 if (s->pict_type == AV_PICTURE_TYPE_P ||
1344 s->pict_type == AV_PICTURE_TYPE_B) {
1345 s->full_pel[0] = get_bits1(&s->gb);
1346 f_code = get_bits(&s->gb, 3);
1347 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1349 s->mpeg_f_code[0][0] = f_code;
1350 s->mpeg_f_code[0][1] = f_code;
1352 if (s->pict_type == AV_PICTURE_TYPE_B) {
1353 s->full_pel[1] = get_bits1(&s->gb);
1354 f_code = get_bits(&s->gb, 3);
1355 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1357 s->mpeg_f_code[1][0] = f_code;
1358 s->mpeg_f_code[1][1] = f_code;
1360 s->current_picture.f->pict_type = s->pict_type;
1361 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1363 if (avctx->debug & FF_DEBUG_PICT_INFO)
1364 av_log(avctx, AV_LOG_DEBUG,
1365 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1372 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1374 MpegEncContext *s = &s1->mpeg_enc_ctx;
1375 int horiz_size_ext, vert_size_ext;
1378 skip_bits(&s->gb, 1); /* profile and level esc*/
1379 s->avctx->profile = get_bits(&s->gb, 3);
1380 s->avctx->level = get_bits(&s->gb, 4);
1381 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1382 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1383 horiz_size_ext = get_bits(&s->gb, 2);
1384 vert_size_ext = get_bits(&s->gb, 2);
1385 s->width |= (horiz_size_ext << 12);
1386 s->height |= (vert_size_ext << 12);
1387 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1388 s->bit_rate += (bit_rate_ext << 18) * 400;
1389 skip_bits1(&s->gb); /* marker */
1390 s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1392 s->low_delay = get_bits1(&s->gb);
1393 if (s->flags & CODEC_FLAG_LOW_DELAY)
1396 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1397 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1399 av_dlog(s->avctx, "sequence extension\n");
1400 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1402 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1403 av_log(s->avctx, AV_LOG_DEBUG,
1404 "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1405 s->avctx->profile, s->avctx->level,
1406 s->avctx->rc_buffer_size, s->bit_rate);
1409 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1411 MpegEncContext *s = &s1->mpeg_enc_ctx;
1412 int color_description, w, h;
1414 skip_bits(&s->gb, 3); /* video format */
1415 color_description = get_bits1(&s->gb);
1416 if (color_description) {
1417 s->avctx->color_primaries = get_bits(&s->gb, 8);
1418 s->avctx->color_trc = get_bits(&s->gb, 8);
1419 s->avctx->colorspace = get_bits(&s->gb, 8);
1421 w = get_bits(&s->gb, 14);
1422 skip_bits(&s->gb, 1); // marker
1423 h = get_bits(&s->gb, 14);
1424 // remaining 3 bits are zero padding
1426 s1->pan_scan.width = 16 * w;
1427 s1->pan_scan.height = 16 * h;
1429 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1430 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1433 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1435 MpegEncContext *s = &s1->mpeg_enc_ctx;
1439 if (s->progressive_sequence) {
1440 if (s->repeat_first_field) {
1442 if (s->top_field_first)
1446 if (s->picture_structure == PICT_FRAME) {
1448 if (s->repeat_first_field)
1452 for (i = 0; i < nofco; i++) {
1453 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1454 skip_bits(&s->gb, 1); // marker
1455 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1456 skip_bits(&s->gb, 1); // marker
1459 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1460 av_log(s->avctx, AV_LOG_DEBUG,
1461 "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
1462 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1463 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1464 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1467 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
1468 uint16_t matrix1[64], int intra)
1472 for (i = 0; i < 64; i++) {
1473 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1474 int v = get_bits(&s->gb, 8);
1476 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1479 if (intra && i == 0 && v != 8) {
1480 av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
1481 v = 8; // needed by pink.mpg / issue1046
1490 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1492 av_dlog(s->avctx, "matrix extension\n");
1494 if (get_bits1(&s->gb))
1495 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1496 if (get_bits1(&s->gb))
1497 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1498 if (get_bits1(&s->gb))
1499 load_matrix(s, s->chroma_intra_matrix, NULL, 1);
1500 if (get_bits1(&s->gb))
1501 load_matrix(s, s->chroma_inter_matrix, NULL, 0);
1504 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1506 MpegEncContext *s = &s1->mpeg_enc_ctx;
1508 s->full_pel[0] = s->full_pel[1] = 0;
1509 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1510 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1511 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1512 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1513 if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1514 av_log(s->avctx, AV_LOG_ERROR,
1515 "Missing picture start code, guessing missing values\n");
1516 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1517 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1518 s->pict_type = AV_PICTURE_TYPE_I;
1520 s->pict_type = AV_PICTURE_TYPE_P;
1522 s->pict_type = AV_PICTURE_TYPE_B;
1523 s->current_picture.f->pict_type = s->pict_type;
1524 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1526 s->intra_dc_precision = get_bits(&s->gb, 2);
1527 s->picture_structure = get_bits(&s->gb, 2);
1528 s->top_field_first = get_bits1(&s->gb);
1529 s->frame_pred_frame_dct = get_bits1(&s->gb);
1530 s->concealment_motion_vectors = get_bits1(&s->gb);
1531 s->q_scale_type = get_bits1(&s->gb);
1532 s->intra_vlc_format = get_bits1(&s->gb);
1533 s->alternate_scan = get_bits1(&s->gb);
1534 s->repeat_first_field = get_bits1(&s->gb);
1535 s->chroma_420_type = get_bits1(&s->gb);
1536 s->progressive_frame = get_bits1(&s->gb);
1538 if (s->progressive_sequence && !s->progressive_frame) {
1539 s->progressive_frame = 1;
1540 av_log(s->avctx, AV_LOG_ERROR,
1541 "interlaced frame in progressive sequence, ignoring\n");
1544 if (s->picture_structure == 0 ||
1545 (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
1546 av_log(s->avctx, AV_LOG_ERROR,
1547 "picture_structure %d invalid, ignoring\n",
1548 s->picture_structure);
1549 s->picture_structure = PICT_FRAME;
1552 if (s->progressive_sequence && !s->frame_pred_frame_dct)
1553 av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
1555 if (s->picture_structure == PICT_FRAME) {
1557 s->v_edge_pos = 16 * s->mb_height;
1559 s->first_field ^= 1;
1560 s->v_edge_pos = 8 * s->mb_height;
1561 memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
1564 if (s->alternate_scan) {
1565 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
1566 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
1568 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
1569 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
1572 /* composite display not parsed */
1573 av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1574 av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1575 av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1576 av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1577 av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1578 av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1579 av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1580 av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1581 av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1584 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1586 AVCodecContext *avctx = s->avctx;
1587 Mpeg1Context *s1 = (Mpeg1Context *) s;
1589 /* start frame decoding */
1590 if (s->first_field || s->picture_structure == PICT_FRAME) {
1591 AVFrameSideData *pan_scan;
1593 if (ff_mpv_frame_start(s, avctx) < 0)
1596 ff_mpeg_er_frame_start(s);
1598 /* first check if we must repeat the frame */
1599 s->current_picture_ptr->f->repeat_pict = 0;
1600 if (s->repeat_first_field) {
1601 if (s->progressive_sequence) {
1602 if (s->top_field_first)
1603 s->current_picture_ptr->f->repeat_pict = 4;
1605 s->current_picture_ptr->f->repeat_pict = 2;
1606 } else if (s->progressive_frame) {
1607 s->current_picture_ptr->f->repeat_pict = 1;
1611 pan_scan = av_frame_new_side_data(s->current_picture_ptr->f,
1612 AV_FRAME_DATA_PANSCAN,
1613 sizeof(s1->pan_scan));
1615 return AVERROR(ENOMEM);
1616 memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
1618 if (s1->a53_caption) {
1619 AVFrameSideData *sd = av_frame_new_side_data(
1620 s->current_picture_ptr->f, AV_FRAME_DATA_A53_CC,
1621 s1->a53_caption_size);
1623 memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
1624 av_freep(&s1->a53_caption);
1627 if (s1->has_stereo3d) {
1628 AVStereo3D *stereo = av_stereo3d_create_side_data(s->current_picture_ptr->f);
1630 return AVERROR(ENOMEM);
1632 *stereo = s1->stereo3d;
1633 s1->has_stereo3d = 0;
1637 AVFrameSideData *sd =
1638 av_frame_new_side_data(s->current_picture_ptr->f,
1639 AV_FRAME_DATA_AFD, 1);
1641 return AVERROR(ENOMEM);
1643 *sd->data = s1->afd;
1647 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1648 ff_thread_finish_setup(avctx);
1649 } else { // second field
1652 if (!s->current_picture_ptr) {
1653 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1657 if (s->avctx->hwaccel &&
1658 (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
1659 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1660 av_log(avctx, AV_LOG_ERROR,
1661 "hardware accelerator failed to decode first field\n");
1664 for (i = 0; i < 4; i++) {
1665 s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
1666 if (s->picture_structure == PICT_BOTTOM_FIELD)
1667 s->current_picture.f->data[i] +=
1668 s->current_picture_ptr->f->linesize[i];
1672 if (avctx->hwaccel) {
1673 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
1678 FF_DISABLE_DEPRECATION_WARNINGS
1679 // ff_mpv_frame_start will call this function too,
1680 // but we need to call it on every field
1681 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1682 if (ff_xvmc_field_start(s, avctx) < 0)
1684 FF_ENABLE_DEPRECATION_WARNINGS
1685 #endif /* FF_API_XVMC */
1690 #define DECODE_SLICE_ERROR -1
1691 #define DECODE_SLICE_OK 0
1695 * MpegEncContext.mb_y must be set to the MB row from the startcode.
1696 * @return DECODE_SLICE_ERROR if the slice is damaged,
1697 * DECODE_SLICE_OK if this slice is OK
1699 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1700 const uint8_t **buf, int buf_size)
1702 AVCodecContext *avctx = s->avctx;
1703 const int field_pic = s->picture_structure != PICT_FRAME;
1706 s->resync_mb_y = -1;
1708 assert(mb_y < s->mb_height);
1710 init_get_bits(&s->gb, *buf, buf_size * 8);
1712 ff_mpeg1_clean_buffers(s);
1713 s->interlaced_dct = 0;
1715 s->qscale = get_qscale(s);
1717 if (s->qscale == 0) {
1718 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1722 /* extra slice info */
1723 while (get_bits1(&s->gb) != 0)
1724 skip_bits(&s->gb, 8);
1728 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1731 while (get_bits_left(&s->gb) > 0) {
1732 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1733 MBINCR_VLC_BITS, 2);
1735 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1741 /* otherwise, stuffing, nothing to do */
1749 if (s->mb_x >= (unsigned) s->mb_width) {
1750 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1754 if (avctx->hwaccel) {
1755 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1756 int start_code = -1;
1757 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1758 if (buf_end < *buf + buf_size)
1761 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1762 return DECODE_SLICE_ERROR;
1764 return DECODE_SLICE_OK;
1767 s->resync_mb_x = s->mb_x;
1768 s->resync_mb_y = s->mb_y = mb_y;
1770 ff_init_block_index(s);
1772 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1773 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1774 av_log(s->avctx, AV_LOG_DEBUG,
1775 "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",
1777 s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
1778 s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1779 s->pict_type == AV_PICTURE_TYPE_I ? "I" :
1780 (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
1781 (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
1782 s->progressive_sequence ? "ps" : "",
1783 s->progressive_frame ? "pf" : "",
1784 s->alternate_scan ? "alt" : "",
1785 s->top_field_first ? "top" : "",
1786 s->intra_dc_precision, s->picture_structure,
1787 s->frame_pred_frame_dct, s->concealment_motion_vectors,
1788 s->q_scale_type, s->intra_vlc_format,
1789 s->repeat_first_field, s->chroma_420_type ? "420" : "");
1795 FF_DISABLE_DEPRECATION_WARNINGS
1796 // If 1, we memcpy blocks in xvmcvideo.
1797 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1798 ff_xvmc_init_block(s); // set s->block
1799 FF_ENABLE_DEPRECATION_WARNINGS
1800 #endif /* FF_API_XVMC */
1802 if (mpeg_decode_mb(s, s->block) < 0)
1805 // Note motion_val is normally NULL unless we want to extract the MVs.
1806 if (s->current_picture.motion_val[0] && !s->encoding) {
1807 const int wrap = s->b8_stride;
1808 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1809 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1810 int motion_x, motion_y, dir, i;
1812 for (i = 0; i < 2; i++) {
1813 for (dir = 0; dir < 2; dir++) {
1815 (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1816 motion_x = motion_y = 0;
1817 } else if (s->mv_type == MV_TYPE_16X16 ||
1818 (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1819 motion_x = s->mv[dir][0][0];
1820 motion_y = s->mv[dir][0][1];
1821 } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
1822 motion_x = s->mv[dir][i][0];
1823 motion_y = s->mv[dir][i][1];
1826 s->current_picture.motion_val[dir][xy][0] = motion_x;
1827 s->current_picture.motion_val[dir][xy][1] = motion_y;
1828 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1829 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1830 s->current_picture.ref_index [dir][b8_xy] =
1831 s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1832 assert(s->field_select[dir][i] == 0 ||
1833 s->field_select[dir][i] == 1);
1841 s->dest[1] += 16 >> s->chroma_x_shift;
1842 s->dest[2] += 16 >> s->chroma_x_shift;
1844 ff_mpv_decode_mb(s, s->block);
1846 if (++s->mb_x >= s->mb_width) {
1847 const int mb_size = 16;
1849 ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
1850 ff_mpv_report_decode_progress(s);
1853 s->mb_y += 1 << field_pic;
1855 if (s->mb_y >= s->mb_height) {
1856 int left = get_bits_left(&s->gb);
1857 int is_d10 = s->chroma_format == 2 &&
1858 s->pict_type == AV_PICTURE_TYPE_I &&
1859 avctx->profile == 0 && avctx->level == 5 &&
1860 s->intra_dc_precision == 2 &&
1861 s->q_scale_type == 1 && s->alternate_scan == 0 &&
1862 s->progressive_frame == 0
1863 /* vbv_delay == 0xBBB || 0xE10 */;
1866 (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
1867 ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
1868 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
1869 left, show_bits(&s->gb, FFMIN(left, 23)));
1875 ff_init_block_index(s);
1878 /* skip mb handling */
1879 if (s->mb_skip_run == -1) {
1880 /* read increment again */
1883 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1884 MBINCR_VLC_BITS, 2);
1886 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1891 s->mb_skip_run += 33;
1892 } else if (code == 35) {
1893 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1894 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1897 goto eos; /* end of slice */
1899 /* otherwise, stuffing, nothing to do */
1901 s->mb_skip_run += code;
1905 if (s->mb_skip_run) {
1907 if (s->pict_type == AV_PICTURE_TYPE_I) {
1908 av_log(s->avctx, AV_LOG_ERROR,
1909 "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1915 for (i = 0; i < 12; i++)
1916 s->block_last_index[i] = -1;
1917 if (s->picture_structure == PICT_FRAME)
1918 s->mv_type = MV_TYPE_16X16;
1920 s->mv_type = MV_TYPE_FIELD;
1921 if (s->pict_type == AV_PICTURE_TYPE_P) {
1922 /* if P type, zero motion vector is implied */
1923 s->mv_dir = MV_DIR_FORWARD;
1924 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1925 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1926 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1927 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1929 /* if B type, reuse previous vectors and directions */
1930 s->mv[0][0][0] = s->last_mv[0][0][0];
1931 s->mv[0][0][1] = s->last_mv[0][0][1];
1932 s->mv[1][0][0] = s->last_mv[1][0][0];
1933 s->mv[1][0][1] = s->last_mv[1][0][1];
1938 eos: // end of slice
1939 *buf += (get_bits_count(&s->gb) - 1) / 8;
1940 av_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1944 static int slice_decode_thread(AVCodecContext *c, void *arg)
1946 MpegEncContext *s = *(void **) arg;
1947 const uint8_t *buf = s->gb.buffer;
1948 int mb_y = s->start_mb_y;
1949 const int field_pic = s->picture_structure != PICT_FRAME;
1951 s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
1954 uint32_t start_code;
1957 ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
1959 av_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1960 ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
1961 s->start_mb_y, s->end_mb_y, s->er.error_count);
1963 if (c->err_recognition & AV_EF_EXPLODE)
1965 if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
1966 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1968 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
1970 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1971 s->mb_x - 1, s->mb_y,
1972 ER_AC_END | ER_DC_END | ER_MV_END);
1975 if (s->mb_y == s->end_mb_y)
1979 buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
1980 mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
1981 if (s->picture_structure == PICT_BOTTOM_FIELD)
1983 if (mb_y < 0 || mb_y >= s->end_mb_y)
1989 * Handle slice ends.
1990 * @return 1 if it seems to be the last slice
1992 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
1994 Mpeg1Context *s1 = avctx->priv_data;
1995 MpegEncContext *s = &s1->mpeg_enc_ctx;
1997 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
2000 if (s->avctx->hwaccel) {
2001 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
2002 av_log(avctx, AV_LOG_ERROR,
2003 "hardware accelerator failed to decode picture\n");
2007 FF_DISABLE_DEPRECATION_WARNINGS
2008 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
2009 ff_xvmc_field_end(s);
2010 FF_ENABLE_DEPRECATION_WARNINGS
2011 #endif /* FF_API_XVMC */
2013 /* end of slice reached */
2014 if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field) {
2017 ff_er_frame_end(&s->er);
2019 ff_mpv_frame_end(s);
2021 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
2022 int ret = av_frame_ref(pict, s->current_picture_ptr->f);
2025 ff_print_debug_info(s, s->current_picture_ptr);
2027 if (avctx->active_thread_type & FF_THREAD_FRAME)
2028 s->picture_number++;
2029 /* latency of 1 frame for I- and P-frames */
2030 /* XXX: use another variable than picture_number */
2031 if (s->last_picture_ptr != NULL) {
2032 int ret = av_frame_ref(pict, s->last_picture_ptr->f);
2035 ff_print_debug_info(s, s->last_picture_ptr);
2045 static int mpeg1_decode_sequence(AVCodecContext *avctx,
2046 const uint8_t *buf, int buf_size)
2048 Mpeg1Context *s1 = avctx->priv_data;
2049 MpegEncContext *s = &s1->mpeg_enc_ctx;
2053 init_get_bits(&s->gb, buf, buf_size * 8);
2055 width = get_bits(&s->gb, 12);
2056 height = get_bits(&s->gb, 12);
2057 if (width == 0 || height == 0) {
2058 av_log(avctx, AV_LOG_WARNING,
2059 "Invalid horizontal or vertical size value.\n");
2060 if (avctx->err_recognition & AV_EF_BITSTREAM)
2061 return AVERROR_INVALIDDATA;
2063 s->aspect_ratio_info = get_bits(&s->gb, 4);
2064 if (s->aspect_ratio_info == 0) {
2065 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
2066 if (avctx->err_recognition & AV_EF_BITSTREAM)
2069 s->frame_rate_index = get_bits(&s->gb, 4);
2070 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
2072 s->bit_rate = get_bits(&s->gb, 18) * 400;
2073 if (get_bits1(&s->gb) == 0) /* marker */
2078 s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
2079 skip_bits(&s->gb, 1);
2082 if (get_bits1(&s->gb)) {
2083 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
2085 for (i = 0; i < 64; i++) {
2086 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;
2092 if (get_bits1(&s->gb)) {
2093 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
2095 for (i = 0; i < 64; i++) {
2096 int j = s->idsp.idct_permutation[i];
2097 v = ff_mpeg1_default_non_intra_matrix[i];
2098 s->inter_matrix[j] = v;
2099 s->chroma_inter_matrix[j] = v;
2103 if (show_bits(&s->gb, 23) != 0) {
2104 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2108 /* We set MPEG-2 parameters so that it emulates MPEG-1. */
2109 s->progressive_sequence = 1;
2110 s->progressive_frame = 1;
2111 s->picture_structure = PICT_FRAME;
2112 s->frame_pred_frame_dct = 1;
2113 s->chroma_format = 1;
2115 s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
2116 s->out_format = FMT_MPEG1;
2117 if (s->flags & CODEC_FLAG_LOW_DELAY)
2120 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2121 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2122 s->avctx->rc_buffer_size, s->bit_rate);
2127 static int vcr2_init_sequence(AVCodecContext *avctx)
2129 Mpeg1Context *s1 = avctx->priv_data;
2130 MpegEncContext *s = &s1->mpeg_enc_ctx;
2133 /* start new MPEG-1 context decoding */
2134 s->out_format = FMT_MPEG1;
2135 if (s1->mpeg_enc_ctx_allocated) {
2136 ff_mpv_common_end(s);
2138 s->width = avctx->coded_width;
2139 s->height = avctx->coded_height;
2140 avctx->has_b_frames = 0; // true?
2143 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2146 if ((avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel) &&
2147 avctx->idct_algo == FF_IDCT_AUTO)
2149 if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
2150 #endif /* FF_API_XVMC */
2151 avctx->idct_algo = FF_IDCT_SIMPLE;
2153 ff_mpv_idct_init(s);
2154 if (ff_mpv_common_init(s) < 0)
2156 s1->mpeg_enc_ctx_allocated = 1;
2158 for (i = 0; i < 64; i++) {
2159 int j = s->idsp.idct_permutation[i];
2160 v = ff_mpeg1_default_intra_matrix[i];
2161 s->intra_matrix[j] = v;
2162 s->chroma_intra_matrix[j] = v;
2164 v = ff_mpeg1_default_non_intra_matrix[i];
2165 s->inter_matrix[j] = v;
2166 s->chroma_inter_matrix[j] = v;
2169 s->progressive_sequence = 1;
2170 s->progressive_frame = 1;
2171 s->picture_structure = PICT_FRAME;
2172 s->frame_pred_frame_dct = 1;
2173 s->chroma_format = 1;
2174 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
2175 s1->save_width = s->width;
2176 s1->save_height = s->height;
2177 s1->save_progressive_seq = s->progressive_sequence;
2181 static int mpeg_decode_a53_cc(AVCodecContext *avctx,
2182 const uint8_t *p, int buf_size)
2184 Mpeg1Context *s1 = avctx->priv_data;
2186 if (buf_size >= 6 &&
2187 p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
2188 p[4] == 3 && (p[5] & 0x40)) {
2189 /* extract A53 Part 4 CC data */
2190 int cc_count = p[5] & 0x1f;
2191 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2192 av_freep(&s1->a53_caption);
2193 s1->a53_caption_size = cc_count * 3;
2194 s1->a53_caption = av_malloc(s1->a53_caption_size);
2195 if (s1->a53_caption)
2196 memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
2199 } else if (buf_size >= 11 &&
2200 p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
2201 /* extract DVD CC data */
2204 // There is a caption count field in the data, but it is often
2205 // incorect. So count the number of captions present.
2206 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2208 // Transform the DVD format into A53 Part 4 format
2210 av_freep(&s1->a53_caption);
2211 s1->a53_caption_size = cc_count * 6;
2212 s1->a53_caption = av_malloc(s1->a53_caption_size);
2213 if (s1->a53_caption) {
2214 uint8_t field1 = !!(p[4] & 0x80);
2215 uint8_t *cap = s1->a53_caption;
2217 for (i = 0; i < cc_count; i++) {
2218 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2221 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2234 static void mpeg_decode_user_data(AVCodecContext *avctx,
2235 const uint8_t *p, int buf_size)
2237 const uint8_t *buf_end = p + buf_size;
2238 Mpeg1Context *s1 = avctx->priv_data;
2240 /* we parse the DTG active format information */
2241 if (buf_end - p >= 5 &&
2242 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2250 if (buf_end - p < 1)
2253 FF_DISABLE_DEPRECATION_WARNINGS
2254 avctx->dtg_active_format = p[0] & 0x0f;
2255 FF_ENABLE_DEPRECATION_WARNINGS
2256 #endif /* FF_API_AFD */
2258 s1->afd = p[0] & 0x0f;
2260 } else if (buf_end - p >= 6 &&
2261 p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
2262 p[4] == 0x03) { // S3D_video_format_length
2263 // the 0x7F mask ignores the reserved_bit value
2264 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2266 if (S3D_video_format_type == 0x03 ||
2267 S3D_video_format_type == 0x04 ||
2268 S3D_video_format_type == 0x08 ||
2269 S3D_video_format_type == 0x23) {
2271 s1->has_stereo3d = 1;
2273 switch (S3D_video_format_type) {
2275 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
2278 s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
2281 s1->stereo3d.type = AV_STEREO3D_2D;
2284 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2288 } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
2293 static void mpeg_decode_gop(AVCodecContext *avctx,
2294 const uint8_t *buf, int buf_size)
2296 Mpeg1Context *s1 = avctx->priv_data;
2297 MpegEncContext *s = &s1->mpeg_enc_ctx;
2299 int time_code_hours, time_code_minutes;
2300 int time_code_seconds, time_code_pictures;
2303 init_get_bits(&s->gb, buf, buf_size * 8);
2305 skip_bits1(&s->gb); /* drop_frame_flag */
2307 time_code_hours = get_bits(&s->gb, 5);
2308 time_code_minutes = get_bits(&s->gb, 6);
2309 skip_bits1(&s->gb); // marker bit
2310 time_code_seconds = get_bits(&s->gb, 6);
2311 time_code_pictures = get_bits(&s->gb, 6);
2313 s1->closed_gop = get_bits1(&s->gb);
2314 /* broken_link indicate that after editing the
2315 * reference frames of the first B-Frames after GOP I-Frame
2316 * are missing (open gop) */
2317 broken_link = get_bits1(&s->gb);
2319 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2320 av_log(s->avctx, AV_LOG_DEBUG,
2321 "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
2322 time_code_hours, time_code_minutes, time_code_seconds,
2323 time_code_pictures, s1->closed_gop, broken_link);
2326 static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
2327 int *got_output, const uint8_t *buf, int buf_size)
2329 Mpeg1Context *s = avctx->priv_data;
2330 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2331 const uint8_t *buf_ptr = buf;
2332 const uint8_t *buf_end = buf + buf_size;
2333 int ret, input_size;
2334 int last_code = 0, skip_frame = 0;
2337 /* find next start code */
2338 uint32_t start_code = -1;
2339 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
2340 if (start_code > 0x1ff) {
2343 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2347 avctx->execute(avctx, slice_decode_thread,
2348 &s2->thread_context[0], NULL,
2349 s->slice_count, sizeof(void *));
2350 for (i = 0; i < s->slice_count; i++)
2351 s2->er.error_count += s2->thread_context[i]->er.error_count;
2354 ret = slice_end(avctx, picture);
2358 // FIXME: merge with the stuff in mpeg_decode_slice
2359 if (s2->last_picture_ptr || s2->low_delay)
2364 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2367 input_size = buf_end - buf_ptr;
2369 if (avctx->debug & FF_DEBUG_STARTCODE)
2370 av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %td left %d\n",
2371 start_code, buf_ptr - buf, input_size);
2373 /* prepare data for next start code */
2374 switch (start_code) {
2375 case SEQ_START_CODE:
2376 if (last_code == 0) {
2377 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2380 av_log(avctx, AV_LOG_ERROR,
2381 "ignoring SEQ_START_CODE after %X\n", last_code);
2382 if (avctx->err_recognition & AV_EF_EXPLODE)
2383 return AVERROR_INVALIDDATA;
2387 case PICTURE_START_CODE:
2388 if (s2->width <= 0 || s2->height <= 0) {
2389 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
2390 s2->width, s2->height);
2391 return AVERROR_INVALIDDATA;
2394 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
2395 !avctx->hwaccel && s->slice_count) {
2398 avctx->execute(avctx, slice_decode_thread,
2399 s2->thread_context, NULL,
2400 s->slice_count, sizeof(void *));
2401 for (i = 0; i < s->slice_count; i++)
2402 s2->er.error_count += s2->thread_context[i]->er.error_count;
2405 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2406 ret = mpeg_decode_postinit(avctx);
2408 av_log(avctx, AV_LOG_ERROR,
2409 "mpeg_decode_postinit() failure\n");
2413 /* We have a complete image: we try to decompress it. */
2414 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2417 last_code = PICTURE_START_CODE;
2419 av_log(avctx, AV_LOG_ERROR,
2420 "ignoring pic after %X\n", last_code);
2421 if (avctx->err_recognition & AV_EF_EXPLODE)
2422 return AVERROR_INVALIDDATA;
2425 case EXT_START_CODE:
2426 init_get_bits(&s2->gb, buf_ptr, input_size * 8);
2428 switch (get_bits(&s2->gb, 4)) {
2430 if (last_code == 0) {
2431 mpeg_decode_sequence_extension(s);
2433 av_log(avctx, AV_LOG_ERROR,
2434 "ignoring seq ext after %X\n", last_code);
2435 if (avctx->err_recognition & AV_EF_EXPLODE)
2436 return AVERROR_INVALIDDATA;
2440 mpeg_decode_sequence_display_extension(s);
2443 mpeg_decode_quant_matrix_extension(s2);
2446 mpeg_decode_picture_display_extension(s);
2449 if (last_code == PICTURE_START_CODE) {
2450 mpeg_decode_picture_coding_extension(s);
2452 av_log(avctx, AV_LOG_ERROR,
2453 "ignoring pic cod ext after %X\n", last_code);
2454 if (avctx->err_recognition & AV_EF_EXPLODE)
2455 return AVERROR_INVALIDDATA;
2460 case USER_START_CODE:
2461 mpeg_decode_user_data(avctx, buf_ptr, input_size);
2463 case GOP_START_CODE:
2464 if (last_code == 0) {
2465 s2->first_field = 0;
2466 mpeg_decode_gop(avctx, buf_ptr, input_size);
2469 av_log(avctx, AV_LOG_ERROR,
2470 "ignoring GOP_START_CODE after %X\n", last_code);
2471 if (avctx->err_recognition & AV_EF_EXPLODE)
2472 return AVERROR_INVALIDDATA;
2476 if (start_code >= SLICE_MIN_START_CODE &&
2477 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2478 const int field_pic = s2->picture_structure != PICT_FRAME;
2479 int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
2480 last_code = SLICE_MIN_START_CODE;
2482 if (s2->picture_structure == PICT_BOTTOM_FIELD)
2485 if (mb_y >= s2->mb_height) {
2486 av_log(s2->avctx, AV_LOG_ERROR,
2487 "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2491 if (!s2->last_picture_ptr) {
2492 /* Skip B-frames if we do not have reference frames and
2493 * GOP is not closed. */
2494 if (s2->pict_type == AV_PICTURE_TYPE_B) {
2495 if (!s->closed_gop) {
2501 if (s2->pict_type == AV_PICTURE_TYPE_I)
2503 if (!s2->next_picture_ptr) {
2504 /* Skip P-frames if we do not have a reference frame or
2505 * we have an invalid header. */
2506 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
2511 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
2512 s2->pict_type == AV_PICTURE_TYPE_B) ||
2513 (avctx->skip_frame >= AVDISCARD_NONKEY &&
2514 s2->pict_type != AV_PICTURE_TYPE_I) ||
2515 avctx->skip_frame >= AVDISCARD_ALL) {
2520 if (!s->mpeg_enc_ctx_allocated)
2523 if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2524 if (mb_y < avctx->skip_top ||
2525 mb_y >= s2->mb_height - avctx->skip_bottom)
2529 if (!s2->pict_type) {
2530 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2531 if (avctx->err_recognition & AV_EF_EXPLODE)
2532 return AVERROR_INVALIDDATA;
2536 if (s->first_slice) {
2539 if (mpeg_field_start(s2, buf, buf_size) < 0)
2542 if (!s2->current_picture_ptr) {
2543 av_log(avctx, AV_LOG_ERROR,
2544 "current_picture not initialized\n");
2545 return AVERROR_INVALIDDATA;
2549 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2551 int threshold = (s2->mb_height * s->slice_count +
2552 s2->slice_context_count / 2) /
2553 s2->slice_context_count;
2554 if (threshold <= mb_y) {
2555 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2557 thread_context->start_mb_y = mb_y;
2558 thread_context->end_mb_y = s2->mb_height;
2559 if (s->slice_count) {
2560 s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
2561 ret = ff_update_duplicate_context(thread_context, s2);
2565 init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
2568 buf_ptr += 2; // FIXME add minimum number of bytes per slice
2570 ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2574 if (avctx->err_recognition & AV_EF_EXPLODE)
2576 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2577 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2578 s2->resync_mb_y, s2->mb_x, s2->mb_y,
2579 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
2581 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2582 s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
2583 ER_AC_END | ER_DC_END | ER_MV_END);
2592 static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
2593 int *got_output, AVPacket *avpkt)
2595 const uint8_t *buf = avpkt->data;
2596 int buf_size = avpkt->size;
2597 Mpeg1Context *s = avctx->priv_data;
2598 AVFrame *picture = data;
2599 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2600 av_dlog(avctx, "fill_buffer\n");
2602 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2603 /* special case for last picture */
2604 if (s2->low_delay == 0 && s2->next_picture_ptr) {
2605 int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
2609 s2->next_picture_ptr = NULL;
2616 if (s2->flags & CODEC_FLAG_TRUNCATED) {
2617 int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
2620 if (ff_combine_frame(&s2->parse_context, next,
2621 (const uint8_t **) &buf, &buf_size) < 0)
2625 if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
2626 vcr2_init_sequence(avctx);
2630 if (avctx->extradata && !s->extradata_decoded) {
2631 int ret = decode_chunks(avctx, picture, got_output,
2632 avctx->extradata, avctx->extradata_size);
2633 s->extradata_decoded = 1;
2634 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
2638 return decode_chunks(avctx, picture, got_output, buf, buf_size);
2641 static void flush(AVCodecContext *avctx)
2643 Mpeg1Context *s = avctx->priv_data;
2648 ff_mpeg_flush(avctx);
2651 static av_cold int mpeg_decode_end(AVCodecContext *avctx)
2653 Mpeg1Context *s = avctx->priv_data;
2655 if (s->mpeg_enc_ctx_allocated)
2656 ff_mpv_common_end(&s->mpeg_enc_ctx);
2657 av_freep(&s->a53_caption);
2661 static const AVProfile mpeg2_video_profiles[] = {
2662 { FF_PROFILE_MPEG2_422, "4:2:2" },
2663 { FF_PROFILE_MPEG2_HIGH, "High" },
2664 { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2665 { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2666 { FF_PROFILE_MPEG2_MAIN, "Main" },
2667 { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2668 { FF_PROFILE_RESERVED, "Reserved" },
2669 { FF_PROFILE_RESERVED, "Reserved" },
2670 { FF_PROFILE_UNKNOWN },
2673 AVCodec ff_mpeg1video_decoder = {
2674 .name = "mpeg1video",
2675 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2676 .type = AVMEDIA_TYPE_VIDEO,
2677 .id = AV_CODEC_ID_MPEG1VIDEO,
2678 .priv_data_size = sizeof(Mpeg1Context),
2679 .init = mpeg_decode_init,
2680 .close = mpeg_decode_end,
2681 .decode = mpeg_decode_frame,
2682 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2683 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2684 CODEC_CAP_SLICE_THREADS,
2686 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2689 AVCodec ff_mpeg2video_decoder = {
2690 .name = "mpeg2video",
2691 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2692 .type = AVMEDIA_TYPE_VIDEO,
2693 .id = AV_CODEC_ID_MPEG2VIDEO,
2694 .priv_data_size = sizeof(Mpeg1Context),
2695 .init = mpeg_decode_init,
2696 .close = mpeg_decode_end,
2697 .decode = mpeg_decode_frame,
2698 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2699 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2700 CODEC_CAP_SLICE_THREADS,
2702 .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2706 #if CONFIG_MPEG_XVMC_DECODER
2707 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2709 if (avctx->active_thread_type & FF_THREAD_SLICE)
2711 if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2713 if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
2714 av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2716 mpeg_decode_init(avctx);
2718 avctx->pix_fmt = AV_PIX_FMT_XVMC_MPEG2_IDCT;
2719 avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2724 AVCodec ff_mpeg_xvmc_decoder = {
2725 .name = "mpegvideo_xvmc",
2726 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2727 .type = AVMEDIA_TYPE_VIDEO,
2728 .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2729 .priv_data_size = sizeof(Mpeg1Context),
2730 .init = mpeg_mc_decode_init,
2731 .close = mpeg_decode_end,
2732 .decode = mpeg_decode_frame,
2733 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2734 CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2739 #endif /* FF_API_XVMC */