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"
37 #include "error_resilience.h"
40 #include "mpeg12data.h"
41 #include "mpegutils.h"
42 #include "mpegvideo.h"
45 #include "xvmc_internal.h"
47 typedef struct Mpeg1Context {
48 MpegEncContext mpeg_enc_ctx;
49 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
50 int repeat_field; /* true if we must repeat the field */
51 AVPanScan pan_scan; /* some temporary storage for the panscan */
58 int save_width, save_height, save_progressive_seq;
59 AVRational frame_rate_ext; /* MPEG-2 specific framerate modificator */
60 int sync; /* Did we reach a sync point like a GOP/SEQ/KEYFrame? */
61 int closed_gop; /* GOP is closed */
63 int extradata_decoded;
66 #define MB_TYPE_ZERO_MV 0x20000000
68 static const uint32_t ptype2mb_type[7] = {
70 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
72 MB_TYPE_L0 | MB_TYPE_CBP,
73 MB_TYPE_QUANT | MB_TYPE_INTRA,
74 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
75 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
78 static const uint32_t btype2mb_type[11] = {
81 MB_TYPE_L1 | MB_TYPE_CBP,
83 MB_TYPE_L0 | MB_TYPE_CBP,
85 MB_TYPE_L0L1 | MB_TYPE_CBP,
86 MB_TYPE_QUANT | MB_TYPE_INTRA,
87 MB_TYPE_QUANT | MB_TYPE_L1 | MB_TYPE_CBP,
88 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
89 MB_TYPE_QUANT | MB_TYPE_L0L1 | MB_TYPE_CBP,
92 static const uint8_t non_linear_qscale[32] = {
93 0, 1, 2, 3, 4, 5, 6, 7,
94 8, 10, 12, 14, 16, 18, 20, 22,
95 24, 28, 32, 36, 40, 44, 48, 52,
96 56, 64, 72, 80, 88, 96, 104, 112,
99 /* as H.263, but only 17 codes */
100 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
102 int code, sign, val, shift;
104 code = get_vlc2(&s->gb, ff_mv_vlc.table, MV_VLC_BITS, 2);
110 sign = get_bits1(&s->gb);
114 val = (val - 1) << shift;
115 val |= get_bits(&s->gb, shift);
122 /* modulo decoding */
123 return sign_extend(val, 5 + shift);
126 #define check_scantable_index(ctx, x) \
129 av_log(ctx->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
130 ctx->mb_x, ctx->mb_y); \
131 return AVERROR_INVALIDDATA; \
135 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
136 int16_t *block, int n)
138 int level, dc, diff, i, j, run;
140 RLTable *rl = &ff_rl_mpeg1;
141 uint8_t *const scantable = s->intra_scantable.permutated;
142 const uint16_t *quant_matrix = s->intra_matrix;
143 const int qscale = s->qscale;
146 component = (n <= 3 ? 0 : n - 4 + 1);
147 diff = decode_dc(&s->gb, component);
150 dc = s->last_dc[component];
152 s->last_dc[component] = dc;
153 block[0] = dc * quant_matrix[0];
154 av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
157 OPEN_READER(re, &s->gb);
158 /* now quantify & encode AC coefficients */
160 UPDATE_CACHE(re, &s->gb);
161 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
166 } else if (level != 0) {
168 check_scantable_index(s, i);
170 level = (level * qscale * quant_matrix[j]) >> 4;
171 level = (level - 1) | 1;
172 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
173 SHOW_SBITS(re, &s->gb, 1);
174 LAST_SKIP_BITS(re, &s->gb, 1);
177 run = SHOW_UBITS(re, &s->gb, 6) + 1;
178 LAST_SKIP_BITS(re, &s->gb, 6);
179 UPDATE_CACHE(re, &s->gb);
180 level = SHOW_SBITS(re, &s->gb, 8);
181 SKIP_BITS(re, &s->gb, 8);
183 level = SHOW_UBITS(re, &s->gb, 8) - 256;
184 LAST_SKIP_BITS(re, &s->gb, 8);
185 } else if (level == 0) {
186 level = SHOW_UBITS(re, &s->gb, 8);
187 LAST_SKIP_BITS(re, &s->gb, 8);
190 check_scantable_index(s, i);
194 level = (level * qscale * quant_matrix[j]) >> 4;
195 level = (level - 1) | 1;
198 level = (level * qscale * quant_matrix[j]) >> 4;
199 level = (level - 1) | 1;
205 CLOSE_READER(re, &s->gb);
207 s->block_last_index[n] = i;
211 int ff_mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
213 return mpeg1_decode_block_intra(s, block, n);
216 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
217 int16_t *block, int n)
219 int level, i, j, run;
220 RLTable *rl = &ff_rl_mpeg1;
221 uint8_t *const scantable = s->intra_scantable.permutated;
222 const uint16_t *quant_matrix = s->inter_matrix;
223 const int qscale = s->qscale;
226 OPEN_READER(re, &s->gb);
228 // special case for first coefficient, no need to add second VLC table
229 UPDATE_CACHE(re, &s->gb);
230 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
231 level = (3 * qscale * quant_matrix[0]) >> 5;
232 level = (level - 1) | 1;
233 if (GET_CACHE(re, &s->gb) & 0x40000000)
237 SKIP_BITS(re, &s->gb, 2);
238 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
241 /* now quantify & encode AC coefficients */
243 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
248 check_scantable_index(s, i);
250 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
251 level = (level - 1) | 1;
252 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
253 SHOW_SBITS(re, &s->gb, 1);
254 SKIP_BITS(re, &s->gb, 1);
257 run = SHOW_UBITS(re, &s->gb, 6) + 1;
258 LAST_SKIP_BITS(re, &s->gb, 6);
259 UPDATE_CACHE(re, &s->gb);
260 level = SHOW_SBITS(re, &s->gb, 8);
261 SKIP_BITS(re, &s->gb, 8);
263 level = SHOW_UBITS(re, &s->gb, 8) - 256;
264 SKIP_BITS(re, &s->gb, 8);
265 } else if (level == 0) {
266 level = SHOW_UBITS(re, &s->gb, 8);
267 SKIP_BITS(re, &s->gb, 8);
270 check_scantable_index(s, i);
274 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
275 level = (level - 1) | 1;
278 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
279 level = (level - 1) | 1;
284 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
286 UPDATE_CACHE(re, &s->gb);
289 LAST_SKIP_BITS(re, &s->gb, 2);
290 CLOSE_READER(re, &s->gb);
292 s->block_last_index[n] = i;
296 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s,
297 int16_t *block, int n)
299 int level, i, j, run;
300 RLTable *rl = &ff_rl_mpeg1;
301 uint8_t *const scantable = s->intra_scantable.permutated;
302 const int qscale = s->qscale;
305 OPEN_READER(re, &s->gb);
307 // Special case for first coefficient, no need to add second VLC table.
308 UPDATE_CACHE(re, &s->gb);
309 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
310 level = (3 * qscale) >> 1;
311 level = (level - 1) | 1;
312 if (GET_CACHE(re, &s->gb) & 0x40000000)
316 SKIP_BITS(re, &s->gb, 2);
317 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
321 /* now quantify & encode AC coefficients */
323 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
328 check_scantable_index(s, i);
330 level = ((level * 2 + 1) * qscale) >> 1;
331 level = (level - 1) | 1;
332 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
333 SHOW_SBITS(re, &s->gb, 1);
334 SKIP_BITS(re, &s->gb, 1);
337 run = SHOW_UBITS(re, &s->gb, 6) + 1;
338 LAST_SKIP_BITS(re, &s->gb, 6);
339 UPDATE_CACHE(re, &s->gb);
340 level = SHOW_SBITS(re, &s->gb, 8);
341 SKIP_BITS(re, &s->gb, 8);
343 level = SHOW_UBITS(re, &s->gb, 8) - 256;
344 SKIP_BITS(re, &s->gb, 8);
345 } else if (level == 0) {
346 level = SHOW_UBITS(re, &s->gb, 8);
347 SKIP_BITS(re, &s->gb, 8);
350 check_scantable_index(s, i);
354 level = ((level * 2 + 1) * qscale) >> 1;
355 level = (level - 1) | 1;
358 level = ((level * 2 + 1) * qscale) >> 1;
359 level = (level - 1) | 1;
364 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
366 UPDATE_CACHE(re, &s->gb);
369 LAST_SKIP_BITS(re, &s->gb, 2);
370 CLOSE_READER(re, &s->gb);
372 s->block_last_index[n] = i;
376 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
377 int16_t *block, int n)
379 int level, i, j, run;
380 RLTable *rl = &ff_rl_mpeg1;
381 uint8_t *const scantable = s->intra_scantable.permutated;
382 const uint16_t *quant_matrix;
383 const int qscale = s->qscale;
389 OPEN_READER(re, &s->gb);
392 quant_matrix = s->inter_matrix;
394 quant_matrix = s->chroma_inter_matrix;
396 // Special case for first coefficient, no need to add second VLC table.
397 UPDATE_CACHE(re, &s->gb);
398 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
399 level = (3 * qscale * quant_matrix[0]) >> 5;
400 if (GET_CACHE(re, &s->gb) & 0x40000000)
405 SKIP_BITS(re, &s->gb, 2);
406 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
410 /* now quantify & encode AC coefficients */
412 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
417 check_scantable_index(s, i);
419 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
420 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
421 SHOW_SBITS(re, &s->gb, 1);
422 SKIP_BITS(re, &s->gb, 1);
425 run = SHOW_UBITS(re, &s->gb, 6) + 1;
426 LAST_SKIP_BITS(re, &s->gb, 6);
427 UPDATE_CACHE(re, &s->gb);
428 level = SHOW_SBITS(re, &s->gb, 12);
429 SKIP_BITS(re, &s->gb, 12);
432 check_scantable_index(s, i);
435 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
438 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
444 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
446 UPDATE_CACHE(re, &s->gb);
449 LAST_SKIP_BITS(re, &s->gb, 2);
450 CLOSE_READER(re, &s->gb);
452 block[63] ^= (mismatch & 1);
454 s->block_last_index[n] = i;
458 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
459 int16_t *block, int n)
461 int level, i, j, run;
462 RLTable *rl = &ff_rl_mpeg1;
463 uint8_t *const scantable = s->intra_scantable.permutated;
464 const int qscale = s->qscale;
465 OPEN_READER(re, &s->gb);
468 // special case for first coefficient, no need to add second VLC table
469 UPDATE_CACHE(re, &s->gb);
470 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
471 level = (3 * qscale) >> 1;
472 if (GET_CACHE(re, &s->gb) & 0x40000000)
476 SKIP_BITS(re, &s->gb, 2);
477 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
481 /* now quantify & encode AC coefficients */
483 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
487 check_scantable_index(s, i);
489 level = ((level * 2 + 1) * qscale) >> 1;
490 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
491 SHOW_SBITS(re, &s->gb, 1);
492 SKIP_BITS(re, &s->gb, 1);
495 run = SHOW_UBITS(re, &s->gb, 6) + 1;
496 LAST_SKIP_BITS(re, &s->gb, 6);
497 UPDATE_CACHE(re, &s->gb);
498 level = SHOW_SBITS(re, &s->gb, 12);
499 SKIP_BITS(re, &s->gb, 12);
502 check_scantable_index(s, i);
505 level = ((-level * 2 + 1) * qscale) >> 1;
508 level = ((level * 2 + 1) * qscale) >> 1;
513 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
515 UPDATE_CACHE(re, &s->gb);
518 LAST_SKIP_BITS(re, &s->gb, 2);
519 CLOSE_READER(re, &s->gb);
520 s->block_last_index[n] = i;
524 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
525 int16_t *block, int n)
527 int level, dc, diff, i, j, run;
530 uint8_t *const scantable = s->intra_scantable.permutated;
531 const uint16_t *quant_matrix;
532 const int qscale = s->qscale;
537 quant_matrix = s->intra_matrix;
540 quant_matrix = s->chroma_intra_matrix;
541 component = (n & 1) + 1;
543 diff = decode_dc(&s->gb, component);
546 dc = s->last_dc[component];
548 s->last_dc[component] = dc;
549 block[0] = dc << (3 - s->intra_dc_precision);
550 av_dlog(s->avctx, "dc=%d\n", block[0]);
551 mismatch = block[0] ^ 1;
553 if (s->intra_vlc_format)
559 OPEN_READER(re, &s->gb);
560 /* now quantify & encode AC coefficients */
562 UPDATE_CACHE(re, &s->gb);
563 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
568 } else if (level != 0) {
570 check_scantable_index(s, i);
572 level = (level * qscale * quant_matrix[j]) >> 4;
573 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
574 SHOW_SBITS(re, &s->gb, 1);
575 LAST_SKIP_BITS(re, &s->gb, 1);
578 run = SHOW_UBITS(re, &s->gb, 6) + 1;
579 LAST_SKIP_BITS(re, &s->gb, 6);
580 UPDATE_CACHE(re, &s->gb);
581 level = SHOW_SBITS(re, &s->gb, 12);
582 SKIP_BITS(re, &s->gb, 12);
584 check_scantable_index(s, i);
587 level = (-level * qscale * quant_matrix[j]) >> 4;
590 level = (level * qscale * quant_matrix[j]) >> 4;
597 CLOSE_READER(re, &s->gb);
599 block[63] ^= mismatch & 1;
601 s->block_last_index[n] = i;
605 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
606 int16_t *block, int n)
608 int level, dc, diff, i, j, run;
611 uint8_t *const scantable = s->intra_scantable.permutated;
612 const uint16_t *quant_matrix;
613 const int qscale = s->qscale;
617 quant_matrix = s->intra_matrix;
620 quant_matrix = s->chroma_intra_matrix;
621 component = (n & 1) + 1;
623 diff = decode_dc(&s->gb, component);
626 dc = s->last_dc[component];
628 s->last_dc[component] = dc;
629 block[0] = dc << (3 - s->intra_dc_precision);
631 if (s->intra_vlc_format)
637 OPEN_READER(re, &s->gb);
638 /* now quantify & encode AC coefficients */
640 UPDATE_CACHE(re, &s->gb);
641 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
646 } else if (level != 0) {
648 check_scantable_index(s, i);
650 level = (level * qscale * quant_matrix[j]) >> 4;
651 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
652 SHOW_SBITS(re, &s->gb, 1);
653 LAST_SKIP_BITS(re, &s->gb, 1);
656 run = SHOW_UBITS(re, &s->gb, 6) + 1;
657 LAST_SKIP_BITS(re, &s->gb, 6);
658 UPDATE_CACHE(re, &s->gb);
659 level = SHOW_SBITS(re, &s->gb, 12);
660 SKIP_BITS(re, &s->gb, 12);
662 check_scantable_index(s, i);
665 level = (-level * qscale * quant_matrix[j]) >> 4;
668 level = (level * qscale * quant_matrix[j]) >> 4;
674 CLOSE_READER(re, &s->gb);
677 s->block_last_index[n] = i;
681 /******************************************/
684 static inline int get_dmv(MpegEncContext *s)
686 if (get_bits1(&s->gb))
687 return 1 - (get_bits1(&s->gb) << 1);
692 static inline int get_qscale(MpegEncContext *s)
694 int qscale = get_bits(&s->gb, 5);
696 return non_linear_qscale[qscale];
701 /* motion type (for MPEG-2) */
707 static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
709 int i, j, k, cbp, val, mb_type, motion_type;
710 const int mb_block_count = 4 + (1 << s->chroma_format);
712 av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
714 assert(s->mb_skipped == 0);
716 if (s->mb_skip_run-- != 0) {
717 if (s->pict_type == AV_PICTURE_TYPE_P) {
719 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
720 MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
725 mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
727 // FIXME not sure if this is allowed in MPEG at all
728 mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
729 if (IS_INTRA(mb_type))
731 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
732 mb_type | MB_TYPE_SKIP;
733 // assert(s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1] & (MB_TYPE_16x16 | MB_TYPE_16x8));
735 if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
742 switch (s->pict_type) {
744 case AV_PICTURE_TYPE_I:
745 if (get_bits1(&s->gb) == 0) {
746 if (get_bits1(&s->gb) == 0) {
747 av_log(s->avctx, AV_LOG_ERROR,
748 "invalid mb type in I Frame at %d %d\n",
752 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
754 mb_type = MB_TYPE_INTRA;
757 case AV_PICTURE_TYPE_P:
758 mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
760 av_log(s->avctx, AV_LOG_ERROR,
761 "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
764 mb_type = ptype2mb_type[mb_type];
766 case AV_PICTURE_TYPE_B:
767 mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
769 av_log(s->avctx, AV_LOG_ERROR,
770 "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
773 mb_type = btype2mb_type[mb_type];
776 av_dlog(s->avctx, "mb_type=%x\n", mb_type);
777 // motion_type = 0; /* avoid warning */
778 if (IS_INTRA(mb_type)) {
779 s->bdsp.clear_blocks(s->block[0]);
781 if (!s->chroma_y_shift)
782 s->bdsp.clear_blocks(s->block[6]);
784 /* compute DCT type */
785 // FIXME: add an interlaced_dct coded var?
786 if (s->picture_structure == PICT_FRAME &&
787 !s->frame_pred_frame_dct)
788 s->interlaced_dct = get_bits1(&s->gb);
790 if (IS_QUANT(mb_type))
791 s->qscale = get_qscale(s);
793 if (s->concealment_motion_vectors) {
794 /* just parse them */
795 if (s->picture_structure != PICT_FRAME)
796 skip_bits1(&s->gb); /* field select */
799 s->last_mv[0][0][0] =
800 s->last_mv[0][1][0] = mpeg_decode_motion(s, s->mpeg_f_code[0][0],
801 s->last_mv[0][0][0]);
803 s->last_mv[0][0][1] =
804 s->last_mv[0][1][1] = mpeg_decode_motion(s, s->mpeg_f_code[0][1],
805 s->last_mv[0][0][1]);
807 skip_bits1(&s->gb); /* marker */
809 /* reset mv prediction */
810 memset(s->last_mv, 0, sizeof(s->last_mv));
814 FF_DISABLE_DEPRECATION_WARNINGS
815 // if 1, we memcpy blocks in xvmcvideo
816 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
817 ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
818 FF_ENABLE_DEPRECATION_WARNINGS
819 #endif /* FF_API_XVMC */
821 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
822 if (s->flags2 & CODEC_FLAG2_FAST) {
823 for (i = 0; i < 6; i++)
824 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
826 for (i = 0; i < mb_block_count; i++)
827 if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
831 for (i = 0; i < 6; i++)
832 if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
836 if (mb_type & MB_TYPE_ZERO_MV) {
837 assert(mb_type & MB_TYPE_CBP);
839 s->mv_dir = MV_DIR_FORWARD;
840 if (s->picture_structure == PICT_FRAME) {
841 if (!s->frame_pred_frame_dct)
842 s->interlaced_dct = get_bits1(&s->gb);
843 s->mv_type = MV_TYPE_16X16;
845 s->mv_type = MV_TYPE_FIELD;
846 mb_type |= MB_TYPE_INTERLACED;
847 s->field_select[0][0] = s->picture_structure - 1;
850 if (IS_QUANT(mb_type))
851 s->qscale = get_qscale(s);
853 s->last_mv[0][0][0] = 0;
854 s->last_mv[0][0][1] = 0;
855 s->last_mv[0][1][0] = 0;
856 s->last_mv[0][1][1] = 0;
860 assert(mb_type & MB_TYPE_L0L1);
861 // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
862 /* get additional motion vector type */
863 if (s->frame_pred_frame_dct) {
864 motion_type = MT_FRAME;
866 motion_type = get_bits(&s->gb, 2);
867 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
868 s->interlaced_dct = get_bits1(&s->gb);
871 if (IS_QUANT(mb_type))
872 s->qscale = get_qscale(s);
875 s->mv_dir = (mb_type >> 13) & 3;
876 av_dlog(s->avctx, "motion_type=%d\n", motion_type);
877 switch (motion_type) {
878 case MT_FRAME: /* or MT_16X8 */
879 if (s->picture_structure == PICT_FRAME) {
880 mb_type |= MB_TYPE_16x16;
881 s->mv_type = MV_TYPE_16X16;
882 for (i = 0; i < 2; i++) {
883 if (USES_LIST(mb_type, i)) {
886 s->last_mv[i][0][0] =
887 s->last_mv[i][1][0] =
888 mpeg_decode_motion(s, s->mpeg_f_code[i][0],
889 s->last_mv[i][0][0]);
891 s->last_mv[i][0][1] =
892 s->last_mv[i][1][1] =
893 mpeg_decode_motion(s, s->mpeg_f_code[i][1],
894 s->last_mv[i][0][1]);
895 /* full_pel: only for MPEG-1 */
896 if (s->full_pel[i]) {
897 s->mv[i][0][0] <<= 1;
898 s->mv[i][0][1] <<= 1;
903 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
904 s->mv_type = MV_TYPE_16X8;
905 for (i = 0; i < 2; i++) {
906 if (USES_LIST(mb_type, i)) {
908 for (j = 0; j < 2; j++) {
909 s->field_select[i][j] = get_bits1(&s->gb);
910 for (k = 0; k < 2; k++) {
911 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
912 s->last_mv[i][j][k]);
913 s->last_mv[i][j][k] = val;
914 s->mv[i][j][k] = val;
922 s->mv_type = MV_TYPE_FIELD;
923 if (s->picture_structure == PICT_FRAME) {
924 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
925 for (i = 0; i < 2; i++) {
926 if (USES_LIST(mb_type, i)) {
927 for (j = 0; j < 2; j++) {
928 s->field_select[i][j] = get_bits1(&s->gb);
929 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
930 s->last_mv[i][j][0]);
931 s->last_mv[i][j][0] = val;
932 s->mv[i][j][0] = val;
933 av_dlog(s->avctx, "fmx=%d\n", val);
934 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
935 s->last_mv[i][j][1] >> 1);
936 s->last_mv[i][j][1] = val << 1;
937 s->mv[i][j][1] = val;
938 av_dlog(s->avctx, "fmy=%d\n", val);
943 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
944 for (i = 0; i < 2; i++) {
945 if (USES_LIST(mb_type, i)) {
946 s->field_select[i][0] = get_bits1(&s->gb);
947 for (k = 0; k < 2; k++) {
948 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
949 s->last_mv[i][0][k]);
950 s->last_mv[i][0][k] = val;
951 s->last_mv[i][1][k] = val;
952 s->mv[i][0][k] = val;
959 s->mv_type = MV_TYPE_DMV;
960 for (i = 0; i < 2; i++) {
961 if (USES_LIST(mb_type, i)) {
962 int dmx, dmy, mx, my, m;
963 const int my_shift = s->picture_structure == PICT_FRAME;
965 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
966 s->last_mv[i][0][0]);
967 s->last_mv[i][0][0] = mx;
968 s->last_mv[i][1][0] = mx;
970 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
971 s->last_mv[i][0][1] >> my_shift);
975 s->last_mv[i][0][1] = my << my_shift;
976 s->last_mv[i][1][1] = my << my_shift;
980 s->mv[i][1][0] = mx; // not used
981 s->mv[i][1][1] = my; // not used
983 if (s->picture_structure == PICT_FRAME) {
984 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
986 // m = 1 + 2 * s->top_field_first;
987 m = s->top_field_first ? 1 : 3;
989 /* top -> top pred */
990 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
991 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
993 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
994 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
996 mb_type |= MB_TYPE_16x16;
998 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
999 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1000 if (s->picture_structure == PICT_TOP_FIELD)
1009 av_log(s->avctx, AV_LOG_ERROR,
1010 "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1016 if (HAS_CBP(mb_type)) {
1017 s->bdsp.clear_blocks(s->block[0]);
1019 cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1020 if (mb_block_count > 6) {
1021 cbp <<= mb_block_count - 6;
1022 cbp |= get_bits(&s->gb, mb_block_count - 6);
1023 s->bdsp.clear_blocks(s->block[6]);
1026 av_log(s->avctx, AV_LOG_ERROR,
1027 "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
1032 FF_DISABLE_DEPRECATION_WARNINGS
1033 // if 1, we memcpy blocks in xvmcvideo
1034 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1035 ff_xvmc_pack_pblocks(s, cbp);
1036 FF_ENABLE_DEPRECATION_WARNINGS
1037 #endif /* FF_API_XVMC */
1039 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1040 if (s->flags2 & CODEC_FLAG2_FAST) {
1041 for (i = 0; i < 6; i++) {
1043 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
1045 s->block_last_index[i] = -1;
1049 cbp <<= 12 - mb_block_count;
1051 for (i = 0; i < mb_block_count; i++) {
1052 if (cbp & (1 << 11)) {
1053 if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
1056 s->block_last_index[i] = -1;
1062 if (s->flags2 & CODEC_FLAG2_FAST) {
1063 for (i = 0; i < 6; i++) {
1065 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
1067 s->block_last_index[i] = -1;
1071 for (i = 0; i < 6; i++) {
1073 if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
1076 s->block_last_index[i] = -1;
1083 for (i = 0; i < 12; i++)
1084 s->block_last_index[i] = -1;
1088 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
1093 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1095 Mpeg1Context *s = avctx->priv_data;
1096 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1099 /* we need some permutation to store matrices,
1100 * until MPV_common_init() sets the real permutation. */
1101 for (i = 0; i < 64; i++)
1102 s2->dsp.idct_permutation[i] = i;
1104 ff_MPV_decode_defaults(s2);
1106 s->mpeg_enc_ctx.avctx = avctx;
1107 s->mpeg_enc_ctx.flags = avctx->flags;
1108 s->mpeg_enc_ctx.flags2 = avctx->flags2;
1109 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1110 ff_mpeg12_init_vlcs();
1112 s->mpeg_enc_ctx_allocated = 0;
1113 s->mpeg_enc_ctx.picture_number = 0;
1114 s->repeat_field = 0;
1115 s->mpeg_enc_ctx.codec_id = avctx->codec->id;
1116 avctx->color_range = AVCOL_RANGE_MPEG;
1117 if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
1118 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1120 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1124 static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
1125 const AVCodecContext *avctx_from)
1127 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1128 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1131 if (avctx == avctx_from ||
1132 !ctx_from->mpeg_enc_ctx_allocated ||
1133 !s1->context_initialized)
1136 err = ff_mpeg_update_thread_context(avctx, avctx_from);
1140 if (!ctx->mpeg_enc_ctx_allocated)
1141 memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
1143 if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
1144 s->picture_number++;
1149 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1150 const uint8_t *new_perm)
1152 uint16_t temp_matrix[64];
1155 memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
1157 for (i = 0; i < 64; i++)
1158 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1162 static const enum AVPixelFormat pixfmt_xvmc_mpg2_420[] = {
1163 AV_PIX_FMT_XVMC_MPEG2_IDCT,
1164 AV_PIX_FMT_XVMC_MPEG2_MC,
1167 #endif /* FF_API_XVMC */
1169 static const enum AVPixelFormat mpeg12_hwaccel_pixfmt_list_420[] = {
1170 #if CONFIG_MPEG2_DXVA2_HWACCEL
1171 AV_PIX_FMT_DXVA2_VLD,
1173 #if CONFIG_MPEG2_VAAPI_HWACCEL
1174 AV_PIX_FMT_VAAPI_VLD,
1176 #if CONFIG_MPEG1_VDPAU_HWACCEL | CONFIG_MPEG2_VDPAU_HWACCEL
1183 static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
1185 Mpeg1Context *s1 = avctx->priv_data;
1186 MpegEncContext *s = &s1->mpeg_enc_ctx;
1189 FF_DISABLE_DEPRECATION_WARNINGS
1190 if (avctx->xvmc_acceleration)
1191 return ff_get_format(avctx, pixfmt_xvmc_mpg2_420);
1192 FF_ENABLE_DEPRECATION_WARNINGS
1193 #endif /* FF_API_XVMC */
1195 if (s->chroma_format < 2)
1196 return ff_get_format(avctx, mpeg12_hwaccel_pixfmt_list_420);
1197 else if (s->chroma_format == 2)
1198 return AV_PIX_FMT_YUV422P;
1200 return AV_PIX_FMT_YUV444P;
1203 /* Call this function when we know all parameters.
1204 * It may be called in different places for MPEG-1 and MPEG-2. */
1205 static int mpeg_decode_postinit(AVCodecContext *avctx)
1207 Mpeg1Context *s1 = avctx->priv_data;
1208 MpegEncContext *s = &s1->mpeg_enc_ctx;
1209 uint8_t old_permutation[64];
1212 if ((s1->mpeg_enc_ctx_allocated == 0) ||
1213 avctx->coded_width != s->width ||
1214 avctx->coded_height != s->height ||
1215 s1->save_width != s->width ||
1216 s1->save_height != s->height ||
1217 s1->save_aspect_info != s->aspect_ratio_info ||
1218 s1->save_progressive_seq != s->progressive_sequence ||
1220 if (s1->mpeg_enc_ctx_allocated) {
1221 ParseContext pc = s->parse_context;
1222 s->parse_context.buffer = 0;
1223 ff_MPV_common_end(s);
1224 s->parse_context = pc;
1227 if ((s->width == 0) || (s->height == 0))
1230 ret = ff_set_dimensions(avctx, s->width, s->height);
1234 avctx->bit_rate = s->bit_rate;
1235 s1->save_aspect_info = s->aspect_ratio_info;
1236 s1->save_width = s->width;
1237 s1->save_height = s->height;
1238 s1->save_progressive_seq = s->progressive_sequence;
1240 /* low_delay may be forced, in this case we will have B-frames
1241 * that behave like P-frames. */
1242 avctx->has_b_frames = !s->low_delay;
1244 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1246 avctx->time_base.den = ff_mpeg12_frame_rate_tab[s->frame_rate_index].num;
1247 avctx->time_base.num = ff_mpeg12_frame_rate_tab[s->frame_rate_index].den;
1249 avctx->sample_aspect_ratio = av_d2q(1.0 / ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1250 avctx->ticks_per_frame = 1;
1253 av_reduce(&s->avctx->time_base.den,
1254 &s->avctx->time_base.num,
1255 ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num * 2,
1256 ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1258 avctx->ticks_per_frame = 2;
1260 if (s->aspect_ratio_info > 1) {
1262 av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1263 (AVRational) { s1->pan_scan.width,
1264 s1->pan_scan.height }),
1265 (AVRational) { s->width, s->height });
1267 /* We ignore the spec here and guess a bit as reality does not
1268 * match the spec, see for example res_change_ffmpeg_aspect.ts
1269 * and sequence-display-aspect.mpg.
1270 * issue1613, 621, 562 */
1271 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1272 (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
1273 av_cmp_q(dar, (AVRational) { 16, 9 }))) {
1274 s->avctx->sample_aspect_ratio =
1275 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1276 (AVRational) { s->width, s->height });
1278 s->avctx->sample_aspect_ratio =
1279 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1280 (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
1281 // issue1613 4/3 16/9 -> 16/9
1282 // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1283 // widescreen-issue562.mpg 4/3 16/9 -> 16/9
1284 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1285 av_dlog(avctx, "A %d/%d\n",
1286 ff_mpeg2_aspect[s->aspect_ratio_info].num,
1287 ff_mpeg2_aspect[s->aspect_ratio_info].den);
1288 av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
1289 s->avctx->sample_aspect_ratio.den);
1292 s->avctx->sample_aspect_ratio =
1293 ff_mpeg2_aspect[s->aspect_ratio_info];
1297 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1298 // until then pix_fmt may be changed right after codec init
1300 if ((avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT ||
1301 avctx->hwaccel) && avctx->idct_algo == FF_IDCT_AUTO)
1303 if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
1304 #endif /* FF_API_XVMC */
1305 avctx->idct_algo = FF_IDCT_SIMPLE;
1307 /* Quantization matrices may need reordering
1308 * if DCT permutation is changed. */
1309 memcpy(old_permutation, s->dsp.idct_permutation, 64 * sizeof(uint8_t));
1311 if (ff_MPV_common_init(s) < 0)
1314 quant_matrix_rebuild(s->intra_matrix, old_permutation, s->dsp.idct_permutation);
1315 quant_matrix_rebuild(s->inter_matrix, old_permutation, s->dsp.idct_permutation);
1316 quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->dsp.idct_permutation);
1317 quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->dsp.idct_permutation);
1319 s1->mpeg_enc_ctx_allocated = 1;
1324 static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
1327 Mpeg1Context *s1 = avctx->priv_data;
1328 MpegEncContext *s = &s1->mpeg_enc_ctx;
1329 int ref, f_code, vbv_delay;
1331 init_get_bits(&s->gb, buf, buf_size * 8);
1333 ref = get_bits(&s->gb, 10); /* temporal ref */
1334 s->pict_type = get_bits(&s->gb, 3);
1335 if (s->pict_type == 0 || s->pict_type > 3)
1338 vbv_delay = get_bits(&s->gb, 16);
1339 if (s->pict_type == AV_PICTURE_TYPE_P ||
1340 s->pict_type == AV_PICTURE_TYPE_B) {
1341 s->full_pel[0] = get_bits1(&s->gb);
1342 f_code = get_bits(&s->gb, 3);
1343 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1345 s->mpeg_f_code[0][0] = f_code;
1346 s->mpeg_f_code[0][1] = f_code;
1348 if (s->pict_type == AV_PICTURE_TYPE_B) {
1349 s->full_pel[1] = get_bits1(&s->gb);
1350 f_code = get_bits(&s->gb, 3);
1351 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1353 s->mpeg_f_code[1][0] = f_code;
1354 s->mpeg_f_code[1][1] = f_code;
1356 s->current_picture.f->pict_type = s->pict_type;
1357 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1359 if (avctx->debug & FF_DEBUG_PICT_INFO)
1360 av_log(avctx, AV_LOG_DEBUG,
1361 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1368 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1370 MpegEncContext *s = &s1->mpeg_enc_ctx;
1371 int horiz_size_ext, vert_size_ext;
1374 skip_bits(&s->gb, 1); /* profile and level esc*/
1375 s->avctx->profile = get_bits(&s->gb, 3);
1376 s->avctx->level = get_bits(&s->gb, 4);
1377 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1378 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1379 horiz_size_ext = get_bits(&s->gb, 2);
1380 vert_size_ext = get_bits(&s->gb, 2);
1381 s->width |= (horiz_size_ext << 12);
1382 s->height |= (vert_size_ext << 12);
1383 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1384 s->bit_rate += (bit_rate_ext << 18) * 400;
1385 skip_bits1(&s->gb); /* marker */
1386 s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1388 s->low_delay = get_bits1(&s->gb);
1389 if (s->flags & CODEC_FLAG_LOW_DELAY)
1392 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1393 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1395 av_dlog(s->avctx, "sequence extension\n");
1396 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1398 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1399 av_log(s->avctx, AV_LOG_DEBUG,
1400 "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1401 s->avctx->profile, s->avctx->level,
1402 s->avctx->rc_buffer_size, s->bit_rate);
1405 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1407 MpegEncContext *s = &s1->mpeg_enc_ctx;
1408 int color_description, w, h;
1410 skip_bits(&s->gb, 3); /* video format */
1411 color_description = get_bits1(&s->gb);
1412 if (color_description) {
1413 s->avctx->color_primaries = get_bits(&s->gb, 8);
1414 s->avctx->color_trc = get_bits(&s->gb, 8);
1415 s->avctx->colorspace = get_bits(&s->gb, 8);
1417 w = get_bits(&s->gb, 14);
1418 skip_bits(&s->gb, 1); // marker
1419 h = get_bits(&s->gb, 14);
1420 // remaining 3 bits are zero padding
1422 s1->pan_scan.width = 16 * w;
1423 s1->pan_scan.height = 16 * h;
1425 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1426 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1429 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1431 MpegEncContext *s = &s1->mpeg_enc_ctx;
1435 if (s->progressive_sequence) {
1436 if (s->repeat_first_field) {
1438 if (s->top_field_first)
1442 if (s->picture_structure == PICT_FRAME) {
1444 if (s->repeat_first_field)
1448 for (i = 0; i < nofco; i++) {
1449 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1450 skip_bits(&s->gb, 1); // marker
1451 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1452 skip_bits(&s->gb, 1); // marker
1455 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1456 av_log(s->avctx, AV_LOG_DEBUG,
1457 "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
1458 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1459 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1460 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1463 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
1464 uint16_t matrix1[64], int intra)
1468 for (i = 0; i < 64; i++) {
1469 int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1470 int v = get_bits(&s->gb, 8);
1472 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1475 if (intra && i == 0 && v != 8) {
1476 av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
1477 v = 8; // needed by pink.mpg / issue1046
1486 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1488 av_dlog(s->avctx, "matrix extension\n");
1490 if (get_bits1(&s->gb))
1491 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1492 if (get_bits1(&s->gb))
1493 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1494 if (get_bits1(&s->gb))
1495 load_matrix(s, s->chroma_intra_matrix, NULL, 1);
1496 if (get_bits1(&s->gb))
1497 load_matrix(s, s->chroma_inter_matrix, NULL, 0);
1500 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1502 MpegEncContext *s = &s1->mpeg_enc_ctx;
1504 s->full_pel[0] = s->full_pel[1] = 0;
1505 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1506 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1507 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1508 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1509 if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1510 av_log(s->avctx, AV_LOG_ERROR,
1511 "Missing picture start code, guessing missing values\n");
1512 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1513 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1514 s->pict_type = AV_PICTURE_TYPE_I;
1516 s->pict_type = AV_PICTURE_TYPE_P;
1518 s->pict_type = AV_PICTURE_TYPE_B;
1519 s->current_picture.f->pict_type = s->pict_type;
1520 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1522 s->intra_dc_precision = get_bits(&s->gb, 2);
1523 s->picture_structure = get_bits(&s->gb, 2);
1524 s->top_field_first = get_bits1(&s->gb);
1525 s->frame_pred_frame_dct = get_bits1(&s->gb);
1526 s->concealment_motion_vectors = get_bits1(&s->gb);
1527 s->q_scale_type = get_bits1(&s->gb);
1528 s->intra_vlc_format = get_bits1(&s->gb);
1529 s->alternate_scan = get_bits1(&s->gb);
1530 s->repeat_first_field = get_bits1(&s->gb);
1531 s->chroma_420_type = get_bits1(&s->gb);
1532 s->progressive_frame = get_bits1(&s->gb);
1534 if (s->progressive_sequence && !s->progressive_frame) {
1535 s->progressive_frame = 1;
1536 av_log(s->avctx, AV_LOG_ERROR,
1537 "interlaced frame in progressive sequence, ignoring\n");
1540 if (s->picture_structure == 0 ||
1541 (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
1542 av_log(s->avctx, AV_LOG_ERROR,
1543 "picture_structure %d invalid, ignoring\n",
1544 s->picture_structure);
1545 s->picture_structure = PICT_FRAME;
1548 if (s->progressive_sequence && !s->frame_pred_frame_dct)
1549 av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
1551 if (s->picture_structure == PICT_FRAME) {
1553 s->v_edge_pos = 16 * s->mb_height;
1555 s->first_field ^= 1;
1556 s->v_edge_pos = 8 * s->mb_height;
1557 memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
1560 if (s->alternate_scan) {
1561 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
1562 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
1564 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
1565 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
1568 /* composite display not parsed */
1569 av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1570 av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1571 av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1572 av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1573 av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1574 av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1575 av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1576 av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1577 av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1580 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1582 AVCodecContext *avctx = s->avctx;
1583 Mpeg1Context *s1 = (Mpeg1Context *) s;
1585 /* start frame decoding */
1586 if (s->first_field || s->picture_structure == PICT_FRAME) {
1587 AVFrameSideData *pan_scan;
1589 if (ff_MPV_frame_start(s, avctx) < 0)
1592 ff_mpeg_er_frame_start(s);
1594 /* first check if we must repeat the frame */
1595 s->current_picture_ptr->f->repeat_pict = 0;
1596 if (s->repeat_first_field) {
1597 if (s->progressive_sequence) {
1598 if (s->top_field_first)
1599 s->current_picture_ptr->f->repeat_pict = 4;
1601 s->current_picture_ptr->f->repeat_pict = 2;
1602 } else if (s->progressive_frame) {
1603 s->current_picture_ptr->f->repeat_pict = 1;
1607 pan_scan = av_frame_new_side_data(s->current_picture_ptr->f,
1608 AV_FRAME_DATA_PANSCAN,
1609 sizeof(s1->pan_scan));
1611 return AVERROR(ENOMEM);
1612 memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
1614 if (s1->a53_caption) {
1615 AVFrameSideData *sd = av_frame_new_side_data(
1616 s->current_picture_ptr->f, AV_FRAME_DATA_A53_CC,
1617 s1->a53_caption_size);
1619 memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
1620 av_freep(&s1->a53_caption);
1623 if (s1->has_stereo3d) {
1624 AVStereo3D *stereo = av_stereo3d_create_side_data(s->current_picture_ptr->f);
1626 return AVERROR(ENOMEM);
1628 *stereo = s1->stereo3d;
1629 s1->has_stereo3d = 0;
1631 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1632 ff_thread_finish_setup(avctx);
1633 } else { // second field
1636 if (!s->current_picture_ptr) {
1637 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1641 if (s->avctx->hwaccel &&
1642 (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
1643 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1644 av_log(avctx, AV_LOG_ERROR,
1645 "hardware accelerator failed to decode first field\n");
1648 for (i = 0; i < 4; i++) {
1649 s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
1650 if (s->picture_structure == PICT_BOTTOM_FIELD)
1651 s->current_picture.f->data[i] +=
1652 s->current_picture_ptr->f->linesize[i];
1656 if (avctx->hwaccel) {
1657 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
1662 FF_DISABLE_DEPRECATION_WARNINGS
1663 // MPV_frame_start will call this function too,
1664 // but we need to call it on every field
1665 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1666 if (ff_xvmc_field_start(s, avctx) < 0)
1668 FF_ENABLE_DEPRECATION_WARNINGS
1669 #endif /* FF_API_XVMC */
1674 #define DECODE_SLICE_ERROR -1
1675 #define DECODE_SLICE_OK 0
1679 * MpegEncContext.mb_y must be set to the MB row from the startcode.
1680 * @return DECODE_SLICE_ERROR if the slice is damaged,
1681 * DECODE_SLICE_OK if this slice is OK
1683 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1684 const uint8_t **buf, int buf_size)
1686 AVCodecContext *avctx = s->avctx;
1687 const int field_pic = s->picture_structure != PICT_FRAME;
1690 s->resync_mb_y = -1;
1692 assert(mb_y < s->mb_height);
1694 init_get_bits(&s->gb, *buf, buf_size * 8);
1696 ff_mpeg1_clean_buffers(s);
1697 s->interlaced_dct = 0;
1699 s->qscale = get_qscale(s);
1701 if (s->qscale == 0) {
1702 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1706 /* extra slice info */
1707 while (get_bits1(&s->gb) != 0)
1708 skip_bits(&s->gb, 8);
1712 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1715 while (get_bits_left(&s->gb) > 0) {
1716 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1717 MBINCR_VLC_BITS, 2);
1719 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1725 /* otherwise, stuffing, nothing to do */
1733 if (s->mb_x >= (unsigned) s->mb_width) {
1734 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1738 if (avctx->hwaccel) {
1739 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1740 int start_code = -1;
1741 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1742 if (buf_end < *buf + buf_size)
1745 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1746 return DECODE_SLICE_ERROR;
1748 return DECODE_SLICE_OK;
1751 s->resync_mb_x = s->mb_x;
1752 s->resync_mb_y = s->mb_y = mb_y;
1754 ff_init_block_index(s);
1756 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1757 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1758 av_log(s->avctx, AV_LOG_DEBUG,
1759 "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",
1761 s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
1762 s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1763 s->pict_type == AV_PICTURE_TYPE_I ? "I" :
1764 (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
1765 (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
1766 s->progressive_sequence ? "ps" : "",
1767 s->progressive_frame ? "pf" : "",
1768 s->alternate_scan ? "alt" : "",
1769 s->top_field_first ? "top" : "",
1770 s->intra_dc_precision, s->picture_structure,
1771 s->frame_pred_frame_dct, s->concealment_motion_vectors,
1772 s->q_scale_type, s->intra_vlc_format,
1773 s->repeat_first_field, s->chroma_420_type ? "420" : "");
1779 FF_DISABLE_DEPRECATION_WARNINGS
1780 // If 1, we memcpy blocks in xvmcvideo.
1781 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1782 ff_xvmc_init_block(s); // set s->block
1783 FF_ENABLE_DEPRECATION_WARNINGS
1784 #endif /* FF_API_XVMC */
1786 if (mpeg_decode_mb(s, s->block) < 0)
1789 // Note motion_val is normally NULL unless we want to extract the MVs.
1790 if (s->current_picture.motion_val[0] && !s->encoding) {
1791 const int wrap = s->b8_stride;
1792 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1793 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1794 int motion_x, motion_y, dir, i;
1796 for (i = 0; i < 2; i++) {
1797 for (dir = 0; dir < 2; dir++) {
1799 (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1800 motion_x = motion_y = 0;
1801 } else if (s->mv_type == MV_TYPE_16X16 ||
1802 (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1803 motion_x = s->mv[dir][0][0];
1804 motion_y = s->mv[dir][0][1];
1805 } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
1806 motion_x = s->mv[dir][i][0];
1807 motion_y = s->mv[dir][i][1];
1810 s->current_picture.motion_val[dir][xy][0] = motion_x;
1811 s->current_picture.motion_val[dir][xy][1] = motion_y;
1812 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1813 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1814 s->current_picture.ref_index [dir][b8_xy] =
1815 s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1816 assert(s->field_select[dir][i] == 0 ||
1817 s->field_select[dir][i] == 1);
1825 s->dest[1] += 16 >> s->chroma_x_shift;
1826 s->dest[2] += 16 >> s->chroma_x_shift;
1828 ff_MPV_decode_mb(s, s->block);
1830 if (++s->mb_x >= s->mb_width) {
1831 const int mb_size = 16;
1833 ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
1834 ff_MPV_report_decode_progress(s);
1837 s->mb_y += 1 << field_pic;
1839 if (s->mb_y >= s->mb_height) {
1840 int left = get_bits_left(&s->gb);
1841 int is_d10 = s->chroma_format == 2 &&
1842 s->pict_type == AV_PICTURE_TYPE_I &&
1843 avctx->profile == 0 && avctx->level == 5 &&
1844 s->intra_dc_precision == 2 &&
1845 s->q_scale_type == 1 && s->alternate_scan == 0 &&
1846 s->progressive_frame == 0
1847 /* vbv_delay == 0xBBB || 0xE10 */;
1850 (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
1851 ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
1852 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
1853 left, show_bits(&s->gb, FFMIN(left, 23)));
1859 ff_init_block_index(s);
1862 /* skip mb handling */
1863 if (s->mb_skip_run == -1) {
1864 /* read increment again */
1867 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1868 MBINCR_VLC_BITS, 2);
1870 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1875 s->mb_skip_run += 33;
1876 } else if (code == 35) {
1877 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1878 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1881 goto eos; /* end of slice */
1883 /* otherwise, stuffing, nothing to do */
1885 s->mb_skip_run += code;
1889 if (s->mb_skip_run) {
1891 if (s->pict_type == AV_PICTURE_TYPE_I) {
1892 av_log(s->avctx, AV_LOG_ERROR,
1893 "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1899 for (i = 0; i < 12; i++)
1900 s->block_last_index[i] = -1;
1901 if (s->picture_structure == PICT_FRAME)
1902 s->mv_type = MV_TYPE_16X16;
1904 s->mv_type = MV_TYPE_FIELD;
1905 if (s->pict_type == AV_PICTURE_TYPE_P) {
1906 /* if P type, zero motion vector is implied */
1907 s->mv_dir = MV_DIR_FORWARD;
1908 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1909 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1910 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1911 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1913 /* if B type, reuse previous vectors and directions */
1914 s->mv[0][0][0] = s->last_mv[0][0][0];
1915 s->mv[0][0][1] = s->last_mv[0][0][1];
1916 s->mv[1][0][0] = s->last_mv[1][0][0];
1917 s->mv[1][0][1] = s->last_mv[1][0][1];
1922 eos: // end of slice
1923 *buf += (get_bits_count(&s->gb) - 1) / 8;
1924 av_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1928 static int slice_decode_thread(AVCodecContext *c, void *arg)
1930 MpegEncContext *s = *(void **) arg;
1931 const uint8_t *buf = s->gb.buffer;
1932 int mb_y = s->start_mb_y;
1933 const int field_pic = s->picture_structure != PICT_FRAME;
1935 s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
1938 uint32_t start_code;
1941 ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
1943 av_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1944 ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
1945 s->start_mb_y, s->end_mb_y, s->er.error_count);
1947 if (c->err_recognition & AV_EF_EXPLODE)
1949 if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
1950 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1952 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
1954 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1955 s->mb_x - 1, s->mb_y,
1956 ER_AC_END | ER_DC_END | ER_MV_END);
1959 if (s->mb_y == s->end_mb_y)
1963 buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
1964 mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
1965 if (s->picture_structure == PICT_BOTTOM_FIELD)
1967 if (mb_y < 0 || mb_y >= s->end_mb_y)
1973 * Handle slice ends.
1974 * @return 1 if it seems to be the last slice
1976 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
1978 Mpeg1Context *s1 = avctx->priv_data;
1979 MpegEncContext *s = &s1->mpeg_enc_ctx;
1981 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
1984 if (s->avctx->hwaccel) {
1985 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1986 av_log(avctx, AV_LOG_ERROR,
1987 "hardware accelerator failed to decode picture\n");
1991 FF_DISABLE_DEPRECATION_WARNINGS
1992 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1993 ff_xvmc_field_end(s);
1994 FF_ENABLE_DEPRECATION_WARNINGS
1995 #endif /* FF_API_XVMC */
1997 /* end of slice reached */
1998 if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field) {
2001 ff_er_frame_end(&s->er);
2003 ff_MPV_frame_end(s);
2005 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
2006 int ret = av_frame_ref(pict, s->current_picture_ptr->f);
2009 ff_print_debug_info(s, s->current_picture_ptr);
2011 if (avctx->active_thread_type & FF_THREAD_FRAME)
2012 s->picture_number++;
2013 /* latency of 1 frame for I- and P-frames */
2014 /* XXX: use another variable than picture_number */
2015 if (s->last_picture_ptr != NULL) {
2016 int ret = av_frame_ref(pict, s->last_picture_ptr->f);
2019 ff_print_debug_info(s, s->last_picture_ptr);
2029 static int mpeg1_decode_sequence(AVCodecContext *avctx,
2030 const uint8_t *buf, int buf_size)
2032 Mpeg1Context *s1 = avctx->priv_data;
2033 MpegEncContext *s = &s1->mpeg_enc_ctx;
2037 init_get_bits(&s->gb, buf, buf_size * 8);
2039 width = get_bits(&s->gb, 12);
2040 height = get_bits(&s->gb, 12);
2041 if (width == 0 || height == 0) {
2042 av_log(avctx, AV_LOG_WARNING,
2043 "Invalid horizontal or vertical size value.\n");
2044 if (avctx->err_recognition & AV_EF_BITSTREAM)
2045 return AVERROR_INVALIDDATA;
2047 s->aspect_ratio_info = get_bits(&s->gb, 4);
2048 if (s->aspect_ratio_info == 0) {
2049 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
2050 if (avctx->err_recognition & AV_EF_BITSTREAM)
2053 s->frame_rate_index = get_bits(&s->gb, 4);
2054 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
2056 s->bit_rate = get_bits(&s->gb, 18) * 400;
2057 if (get_bits1(&s->gb) == 0) /* marker */
2062 s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
2063 skip_bits(&s->gb, 1);
2066 if (get_bits1(&s->gb)) {
2067 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
2069 for (i = 0; i < 64; i++) {
2070 j = s->dsp.idct_permutation[i];
2071 v = ff_mpeg1_default_intra_matrix[i];
2072 s->intra_matrix[j] = v;
2073 s->chroma_intra_matrix[j] = v;
2076 if (get_bits1(&s->gb)) {
2077 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
2079 for (i = 0; i < 64; i++) {
2080 int j = s->dsp.idct_permutation[i];
2081 v = ff_mpeg1_default_non_intra_matrix[i];
2082 s->inter_matrix[j] = v;
2083 s->chroma_inter_matrix[j] = v;
2087 if (show_bits(&s->gb, 23) != 0) {
2088 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2092 /* We set MPEG-2 parameters so that it emulates MPEG-1. */
2093 s->progressive_sequence = 1;
2094 s->progressive_frame = 1;
2095 s->picture_structure = PICT_FRAME;
2096 s->frame_pred_frame_dct = 1;
2097 s->chroma_format = 1;
2099 s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
2100 s->out_format = FMT_MPEG1;
2101 if (s->flags & CODEC_FLAG_LOW_DELAY)
2104 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2105 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2106 s->avctx->rc_buffer_size, s->bit_rate);
2111 static int vcr2_init_sequence(AVCodecContext *avctx)
2113 Mpeg1Context *s1 = avctx->priv_data;
2114 MpegEncContext *s = &s1->mpeg_enc_ctx;
2117 /* start new MPEG-1 context decoding */
2118 s->out_format = FMT_MPEG1;
2119 if (s1->mpeg_enc_ctx_allocated) {
2120 ff_MPV_common_end(s);
2122 s->width = avctx->coded_width;
2123 s->height = avctx->coded_height;
2124 avctx->has_b_frames = 0; // true?
2127 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2130 if ((avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel) &&
2131 avctx->idct_algo == FF_IDCT_AUTO)
2133 if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
2134 #endif /* FF_API_XVMC */
2135 avctx->idct_algo = FF_IDCT_SIMPLE;
2137 if (ff_MPV_common_init(s) < 0)
2139 s1->mpeg_enc_ctx_allocated = 1;
2141 for (i = 0; i < 64; i++) {
2142 int j = s->dsp.idct_permutation[i];
2143 v = ff_mpeg1_default_intra_matrix[i];
2144 s->intra_matrix[j] = v;
2145 s->chroma_intra_matrix[j] = v;
2147 v = ff_mpeg1_default_non_intra_matrix[i];
2148 s->inter_matrix[j] = v;
2149 s->chroma_inter_matrix[j] = v;
2152 s->progressive_sequence = 1;
2153 s->progressive_frame = 1;
2154 s->picture_structure = PICT_FRAME;
2155 s->frame_pred_frame_dct = 1;
2156 s->chroma_format = 1;
2157 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
2158 s1->save_width = s->width;
2159 s1->save_height = s->height;
2160 s1->save_progressive_seq = s->progressive_sequence;
2164 static int mpeg_decode_a53_cc(AVCodecContext *avctx,
2165 const uint8_t *p, int buf_size)
2167 Mpeg1Context *s1 = avctx->priv_data;
2169 if (buf_size >= 6 &&
2170 p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
2171 p[4] == 3 && (p[5] & 0x40)) {
2172 /* extract A53 Part 4 CC data */
2173 int cc_count = p[5] & 0x1f;
2174 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2175 av_freep(&s1->a53_caption);
2176 s1->a53_caption_size = cc_count * 3;
2177 s1->a53_caption = av_malloc(s1->a53_caption_size);
2178 if (s1->a53_caption)
2179 memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
2182 } else if (buf_size >= 11 &&
2183 p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
2184 /* extract DVD CC data */
2187 // There is a caption count field in the data, but it is often
2188 // incorect. So count the number of captions present.
2189 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2191 // Transform the DVD format into A53 Part 4 format
2193 av_freep(&s1->a53_caption);
2194 s1->a53_caption_size = cc_count * 6;
2195 s1->a53_caption = av_malloc(s1->a53_caption_size);
2196 if (s1->a53_caption) {
2197 uint8_t field1 = !!(p[4] & 0x80);
2198 uint8_t *cap = s1->a53_caption;
2200 for (i = 0; i < cc_count; i++) {
2201 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2204 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2217 static void mpeg_decode_user_data(AVCodecContext *avctx,
2218 const uint8_t *p, int buf_size)
2220 const uint8_t *buf_end = p + buf_size;
2222 /* we parse the DTG active format information */
2223 if (buf_end - p >= 5 &&
2224 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2232 if (buf_end - p < 1)
2234 avctx->dtg_active_format = p[0] & 0x0f;
2236 } else if (buf_end - p >= 6 &&
2237 p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
2238 p[4] == 0x03) { // S3D_video_format_length
2239 // the 0x7F mask ignores the reserved_bit value
2240 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2242 if (S3D_video_format_type == 0x03 ||
2243 S3D_video_format_type == 0x04 ||
2244 S3D_video_format_type == 0x08 ||
2245 S3D_video_format_type == 0x23) {
2246 Mpeg1Context *s1 = avctx->priv_data;
2248 s1->has_stereo3d = 1;
2250 switch (S3D_video_format_type) {
2252 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
2255 s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
2258 s1->stereo3d.type = AV_STEREO3D_2D;
2261 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2265 } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
2270 static void mpeg_decode_gop(AVCodecContext *avctx,
2271 const uint8_t *buf, int buf_size)
2273 Mpeg1Context *s1 = avctx->priv_data;
2274 MpegEncContext *s = &s1->mpeg_enc_ctx;
2276 int time_code_hours, time_code_minutes;
2277 int time_code_seconds, time_code_pictures;
2280 init_get_bits(&s->gb, buf, buf_size * 8);
2282 skip_bits1(&s->gb); /* drop_frame_flag */
2284 time_code_hours = get_bits(&s->gb, 5);
2285 time_code_minutes = get_bits(&s->gb, 6);
2286 skip_bits1(&s->gb); // marker bit
2287 time_code_seconds = get_bits(&s->gb, 6);
2288 time_code_pictures = get_bits(&s->gb, 6);
2290 s1->closed_gop = get_bits1(&s->gb);
2291 /* broken_link indicate that after editing the
2292 * reference frames of the first B-Frames after GOP I-Frame
2293 * are missing (open gop) */
2294 broken_link = get_bits1(&s->gb);
2296 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2297 av_log(s->avctx, AV_LOG_DEBUG,
2298 "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
2299 time_code_hours, time_code_minutes, time_code_seconds,
2300 time_code_pictures, s1->closed_gop, broken_link);
2303 static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
2304 int *got_output, const uint8_t *buf, int buf_size)
2306 Mpeg1Context *s = avctx->priv_data;
2307 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2308 const uint8_t *buf_ptr = buf;
2309 const uint8_t *buf_end = buf + buf_size;
2310 int ret, input_size;
2311 int last_code = 0, skip_frame = 0;
2314 /* find next start code */
2315 uint32_t start_code = -1;
2316 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
2317 if (start_code > 0x1ff) {
2320 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2324 avctx->execute(avctx, slice_decode_thread,
2325 &s2->thread_context[0], NULL,
2326 s->slice_count, sizeof(void *));
2327 for (i = 0; i < s->slice_count; i++)
2328 s2->er.error_count += s2->thread_context[i]->er.error_count;
2331 ret = slice_end(avctx, picture);
2335 // FIXME: merge with the stuff in mpeg_decode_slice
2336 if (s2->last_picture_ptr || s2->low_delay)
2341 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2344 input_size = buf_end - buf_ptr;
2346 if (avctx->debug & FF_DEBUG_STARTCODE)
2347 av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %td left %d\n",
2348 start_code, buf_ptr - buf, input_size);
2350 /* prepare data for next start code */
2351 switch (start_code) {
2352 case SEQ_START_CODE:
2353 if (last_code == 0) {
2354 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2357 av_log(avctx, AV_LOG_ERROR,
2358 "ignoring SEQ_START_CODE after %X\n", last_code);
2359 if (avctx->err_recognition & AV_EF_EXPLODE)
2360 return AVERROR_INVALIDDATA;
2364 case PICTURE_START_CODE:
2365 if (s2->width <= 0 || s2->height <= 0) {
2366 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
2367 s2->width, s2->height);
2368 return AVERROR_INVALIDDATA;
2371 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
2372 !avctx->hwaccel && s->slice_count) {
2375 avctx->execute(avctx, slice_decode_thread,
2376 s2->thread_context, NULL,
2377 s->slice_count, sizeof(void *));
2378 for (i = 0; i < s->slice_count; i++)
2379 s2->er.error_count += s2->thread_context[i]->er.error_count;
2382 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2383 ret = mpeg_decode_postinit(avctx);
2385 av_log(avctx, AV_LOG_ERROR,
2386 "mpeg_decode_postinit() failure\n");
2390 /* We have a complete image: we try to decompress it. */
2391 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2394 last_code = PICTURE_START_CODE;
2396 av_log(avctx, AV_LOG_ERROR,
2397 "ignoring pic after %X\n", last_code);
2398 if (avctx->err_recognition & AV_EF_EXPLODE)
2399 return AVERROR_INVALIDDATA;
2402 case EXT_START_CODE:
2403 init_get_bits(&s2->gb, buf_ptr, input_size * 8);
2405 switch (get_bits(&s2->gb, 4)) {
2407 if (last_code == 0) {
2408 mpeg_decode_sequence_extension(s);
2410 av_log(avctx, AV_LOG_ERROR,
2411 "ignoring seq ext after %X\n", last_code);
2412 if (avctx->err_recognition & AV_EF_EXPLODE)
2413 return AVERROR_INVALIDDATA;
2417 mpeg_decode_sequence_display_extension(s);
2420 mpeg_decode_quant_matrix_extension(s2);
2423 mpeg_decode_picture_display_extension(s);
2426 if (last_code == PICTURE_START_CODE) {
2427 mpeg_decode_picture_coding_extension(s);
2429 av_log(avctx, AV_LOG_ERROR,
2430 "ignoring pic cod ext after %X\n", last_code);
2431 if (avctx->err_recognition & AV_EF_EXPLODE)
2432 return AVERROR_INVALIDDATA;
2437 case USER_START_CODE:
2438 mpeg_decode_user_data(avctx, buf_ptr, input_size);
2440 case GOP_START_CODE:
2441 if (last_code == 0) {
2442 s2->first_field = 0;
2443 mpeg_decode_gop(avctx, buf_ptr, input_size);
2446 av_log(avctx, AV_LOG_ERROR,
2447 "ignoring GOP_START_CODE after %X\n", last_code);
2448 if (avctx->err_recognition & AV_EF_EXPLODE)
2449 return AVERROR_INVALIDDATA;
2453 if (start_code >= SLICE_MIN_START_CODE &&
2454 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2455 const int field_pic = s2->picture_structure != PICT_FRAME;
2456 int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
2457 last_code = SLICE_MIN_START_CODE;
2459 if (s2->picture_structure == PICT_BOTTOM_FIELD)
2462 if (mb_y >= s2->mb_height) {
2463 av_log(s2->avctx, AV_LOG_ERROR,
2464 "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2468 if (s2->last_picture_ptr == NULL) {
2469 /* Skip B-frames if we do not have reference frames and
2470 * GOP is not closed. */
2471 if (s2->pict_type == AV_PICTURE_TYPE_B) {
2472 if (!s->closed_gop) {
2478 if (s2->pict_type == AV_PICTURE_TYPE_I)
2480 if (s2->next_picture_ptr == NULL) {
2481 /* Skip P-frames if we do not have a reference frame or
2482 * we have an invalid header. */
2483 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
2488 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
2489 s2->pict_type == AV_PICTURE_TYPE_B) ||
2490 (avctx->skip_frame >= AVDISCARD_NONKEY &&
2491 s2->pict_type != AV_PICTURE_TYPE_I) ||
2492 avctx->skip_frame >= AVDISCARD_ALL) {
2497 if (!s->mpeg_enc_ctx_allocated)
2500 if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2501 if (mb_y < avctx->skip_top ||
2502 mb_y >= s2->mb_height - avctx->skip_bottom)
2506 if (!s2->pict_type) {
2507 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2508 if (avctx->err_recognition & AV_EF_EXPLODE)
2509 return AVERROR_INVALIDDATA;
2513 if (s->first_slice) {
2516 if (mpeg_field_start(s2, buf, buf_size) < 0)
2519 if (!s2->current_picture_ptr) {
2520 av_log(avctx, AV_LOG_ERROR,
2521 "current_picture not initialized\n");
2522 return AVERROR_INVALIDDATA;
2526 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2528 int threshold = (s2->mb_height * s->slice_count +
2529 s2->slice_context_count / 2) /
2530 s2->slice_context_count;
2531 if (threshold <= mb_y) {
2532 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2534 thread_context->start_mb_y = mb_y;
2535 thread_context->end_mb_y = s2->mb_height;
2536 if (s->slice_count) {
2537 s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
2538 ret = ff_update_duplicate_context(thread_context, s2);
2542 init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
2545 buf_ptr += 2; // FIXME add minimum number of bytes per slice
2547 ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2551 if (avctx->err_recognition & AV_EF_EXPLODE)
2553 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2554 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2555 s2->resync_mb_y, s2->mb_x, s2->mb_y,
2556 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
2558 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2559 s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
2560 ER_AC_END | ER_DC_END | ER_MV_END);
2569 static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
2570 int *got_output, AVPacket *avpkt)
2572 const uint8_t *buf = avpkt->data;
2573 int buf_size = avpkt->size;
2574 Mpeg1Context *s = avctx->priv_data;
2575 AVFrame *picture = data;
2576 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2577 av_dlog(avctx, "fill_buffer\n");
2579 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2580 /* special case for last picture */
2581 if (s2->low_delay == 0 && s2->next_picture_ptr) {
2582 int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
2586 s2->next_picture_ptr = NULL;
2593 if (s2->flags & CODEC_FLAG_TRUNCATED) {
2594 int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
2597 if (ff_combine_frame(&s2->parse_context, next,
2598 (const uint8_t **) &buf, &buf_size) < 0)
2602 if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
2603 vcr2_init_sequence(avctx);
2607 if (avctx->extradata && !s->extradata_decoded) {
2608 int ret = decode_chunks(avctx, picture, got_output,
2609 avctx->extradata, avctx->extradata_size);
2610 s->extradata_decoded = 1;
2611 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
2615 return decode_chunks(avctx, picture, got_output, buf, buf_size);
2618 static void flush(AVCodecContext *avctx)
2620 Mpeg1Context *s = avctx->priv_data;
2625 ff_mpeg_flush(avctx);
2628 static av_cold int mpeg_decode_end(AVCodecContext *avctx)
2630 Mpeg1Context *s = avctx->priv_data;
2632 if (s->mpeg_enc_ctx_allocated)
2633 ff_MPV_common_end(&s->mpeg_enc_ctx);
2634 av_freep(&s->a53_caption);
2638 static const AVProfile mpeg2_video_profiles[] = {
2639 { FF_PROFILE_MPEG2_422, "4:2:2" },
2640 { FF_PROFILE_MPEG2_HIGH, "High" },
2641 { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2642 { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2643 { FF_PROFILE_MPEG2_MAIN, "Main" },
2644 { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2645 { FF_PROFILE_RESERVED, "Reserved" },
2646 { FF_PROFILE_RESERVED, "Reserved" },
2647 { FF_PROFILE_UNKNOWN },
2650 AVCodec ff_mpeg1video_decoder = {
2651 .name = "mpeg1video",
2652 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2653 .type = AVMEDIA_TYPE_VIDEO,
2654 .id = AV_CODEC_ID_MPEG1VIDEO,
2655 .priv_data_size = sizeof(Mpeg1Context),
2656 .init = mpeg_decode_init,
2657 .close = mpeg_decode_end,
2658 .decode = mpeg_decode_frame,
2659 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2660 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2661 CODEC_CAP_SLICE_THREADS,
2663 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2666 AVCodec ff_mpeg2video_decoder = {
2667 .name = "mpeg2video",
2668 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2669 .type = AVMEDIA_TYPE_VIDEO,
2670 .id = AV_CODEC_ID_MPEG2VIDEO,
2671 .priv_data_size = sizeof(Mpeg1Context),
2672 .init = mpeg_decode_init,
2673 .close = mpeg_decode_end,
2674 .decode = mpeg_decode_frame,
2675 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2676 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2677 CODEC_CAP_SLICE_THREADS,
2679 .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2683 #if CONFIG_MPEG_XVMC_DECODER
2684 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2686 if (avctx->active_thread_type & FF_THREAD_SLICE)
2688 if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2690 if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
2691 av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2693 mpeg_decode_init(avctx);
2695 avctx->pix_fmt = AV_PIX_FMT_XVMC_MPEG2_IDCT;
2696 avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2701 AVCodec ff_mpeg_xvmc_decoder = {
2702 .name = "mpegvideo_xvmc",
2703 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2704 .type = AVMEDIA_TYPE_VIDEO,
2705 .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2706 .priv_data_size = sizeof(Mpeg1Context),
2707 .init = mpeg_mc_decode_init,
2708 .close = mpeg_decode_end,
2709 .decode = mpeg_decode_frame,
2710 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2711 CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2716 #endif /* FF_API_XVMC */