3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2013 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "libavutil/attributes.h"
30 #include "libavutil/internal.h"
34 #include "mpegvideo.h"
35 #include "error_resilience.h"
37 #include "mpeg12data.h"
38 #include "mpeg12decdata.h"
39 #include "bytestream.h"
40 #include "vdpau_internal.h"
41 #include "xvmc_internal.h"
44 typedef struct Mpeg1Context {
45 MpegEncContext mpeg_enc_ctx;
46 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
47 int repeat_field; /* true if we must repeat the field */
48 AVPanScan pan_scan; /**< some temporary storage for the panscan */
50 int swap_uv;//indicate VCR2
52 int save_width, save_height, save_progressive_seq;
53 AVRational frame_rate_ext; ///< MPEG-2 specific framerate modificator
54 int sync; ///< Did we reach a sync point like a GOP/SEQ/KEYFrame?
56 int extradata_decoded;
59 /* as H.263, but only 17 codes */
60 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
62 int code, sign, val, shift;
64 code = get_vlc2(&s->gb, ff_mv_vlc.table, MV_VLC_BITS, 2);
72 sign = get_bits1(&s->gb);
76 val = (val - 1) << shift;
77 val |= get_bits(&s->gb, shift);
85 return sign_extend(val, 5 + shift);
88 static inline int mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
90 int level, dc, diff, i, j, run;
92 RLTable *rl = &ff_rl_mpeg1;
93 uint8_t * const scantable = s->intra_scantable.permutated;
94 const uint16_t *quant_matrix = s->intra_matrix;
95 const int qscale = s->qscale;
98 component = (n <= 3 ? 0 : n - 4 + 1);
99 diff = decode_dc(&s->gb, component);
102 dc = s->last_dc[component];
104 s->last_dc[component] = dc;
105 block[0] = dc * quant_matrix[0];
106 av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
109 OPEN_READER(re, &s->gb);
110 /* now quantify & encode AC coefficients */
112 UPDATE_CACHE(re, &s->gb);
113 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
117 } else if (level != 0) {
120 level = (level * qscale * quant_matrix[j]) >> 4;
121 level = (level - 1) | 1;
122 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
123 LAST_SKIP_BITS(re, &s->gb, 1);
126 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
127 UPDATE_CACHE(re, &s->gb);
128 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
130 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
131 } else if (level == 0) {
132 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
138 level = (level * qscale * quant_matrix[j]) >> 4;
139 level = (level - 1) | 1;
142 level = (level * qscale * quant_matrix[j]) >> 4;
143 level = (level - 1) | 1;
147 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
153 CLOSE_READER(re, &s->gb);
155 s->block_last_index[n] = i;
159 int ff_mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
161 return mpeg1_decode_block_intra(s, block, n);
164 static inline int mpeg1_decode_block_inter(MpegEncContext *s, int16_t *block, int n)
166 int level, i, j, run;
167 RLTable *rl = &ff_rl_mpeg1;
168 uint8_t * const scantable = s->intra_scantable.permutated;
169 const uint16_t *quant_matrix = s->inter_matrix;
170 const int qscale = s->qscale;
173 OPEN_READER(re, &s->gb);
175 // special case for first coefficient, no need to add second VLC table
176 UPDATE_CACHE(re, &s->gb);
177 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
178 level = (3 * qscale * quant_matrix[0]) >> 5;
179 level = (level - 1) | 1;
180 if (GET_CACHE(re, &s->gb) & 0x40000000)
184 SKIP_BITS(re, &s->gb, 2);
185 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
188 /* now quantify & encode AC coefficients */
190 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
195 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
196 level = (level - 1) | 1;
197 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
198 SKIP_BITS(re, &s->gb, 1);
201 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
202 UPDATE_CACHE(re, &s->gb);
203 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
205 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
206 } else if (level == 0) {
207 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
213 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
214 level = (level - 1) | 1;
217 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
218 level = (level - 1) | 1;
222 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
227 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
229 UPDATE_CACHE(re, &s->gb);
232 LAST_SKIP_BITS(re, &s->gb, 2);
233 CLOSE_READER(re, &s->gb);
235 s->block_last_index[n] = i;
240 * Note: this function can read out of range and crash for corrupt streams.
241 * Changing this would eat up any speed benefits it has.
242 * Do not use "fast" flag if you need the code to be robust.
244 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, int16_t *block, int n)
246 int level, i, j, run;
247 RLTable *rl = &ff_rl_mpeg1;
248 uint8_t * const scantable = s->intra_scantable.permutated;
249 const int qscale = s->qscale;
252 OPEN_READER(re, &s->gb);
254 // special case for first coefficient, no need to add second VLC table
255 UPDATE_CACHE(re, &s->gb);
256 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
257 level = (3 * qscale) >> 1;
258 level = (level - 1) | 1;
259 if (GET_CACHE(re, &s->gb) & 0x40000000)
263 SKIP_BITS(re, &s->gb, 2);
264 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
268 /* now quantify & encode AC coefficients */
270 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
275 level = ((level * 2 + 1) * qscale) >> 1;
276 level = (level - 1) | 1;
277 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
278 SKIP_BITS(re, &s->gb, 1);
281 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
282 UPDATE_CACHE(re, &s->gb);
283 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
285 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
286 } else if (level == 0) {
287 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
293 level = ((level * 2 + 1) * qscale) >> 1;
294 level = (level - 1) | 1;
297 level = ((level * 2 + 1) * qscale) >> 1;
298 level = (level - 1) | 1;
303 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
305 UPDATE_CACHE(re, &s->gb);
308 LAST_SKIP_BITS(re, &s->gb, 2);
309 CLOSE_READER(re, &s->gb);
311 s->block_last_index[n] = i;
316 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, int16_t *block, int n)
318 int level, i, j, run;
319 RLTable *rl = &ff_rl_mpeg1;
320 uint8_t * const scantable = s->intra_scantable.permutated;
321 const uint16_t *quant_matrix;
322 const int qscale = s->qscale;
328 OPEN_READER(re, &s->gb);
331 quant_matrix = s->inter_matrix;
333 quant_matrix = s->chroma_inter_matrix;
335 // special case for first coefficient, no need to add second VLC table
336 UPDATE_CACHE(re, &s->gb);
337 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
338 level= (3 * qscale * quant_matrix[0]) >> 5;
339 if (GET_CACHE(re, &s->gb) & 0x40000000)
344 SKIP_BITS(re, &s->gb, 2);
345 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
349 /* now quantify & encode AC coefficients */
351 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
356 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
357 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
358 SKIP_BITS(re, &s->gb, 1);
361 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
362 UPDATE_CACHE(re, &s->gb);
363 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
368 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
371 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
375 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
381 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
383 UPDATE_CACHE(re, &s->gb);
386 LAST_SKIP_BITS(re, &s->gb, 2);
387 CLOSE_READER(re, &s->gb);
389 block[63] ^= (mismatch & 1);
391 s->block_last_index[n] = i;
396 * Note: this function can read out of range and crash for corrupt streams.
397 * Changing this would eat up any speed benefits it has.
398 * Do not use "fast" flag if you need the code to be robust.
400 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
401 int16_t *block, int n)
403 int level, i, j, run;
404 RLTable *rl = &ff_rl_mpeg1;
405 uint8_t * const scantable = s->intra_scantable.permutated;
406 const int qscale = s->qscale;
407 OPEN_READER(re, &s->gb);
410 // special case for first coefficient, no need to add second VLC table
411 UPDATE_CACHE(re, &s->gb);
412 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
413 level = (3 * qscale) >> 1;
414 if (GET_CACHE(re, &s->gb) & 0x40000000)
418 SKIP_BITS(re, &s->gb, 2);
419 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
423 /* now quantify & encode AC coefficients */
425 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
430 level = ((level * 2 + 1) * qscale) >> 1;
431 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
432 SKIP_BITS(re, &s->gb, 1);
435 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
436 UPDATE_CACHE(re, &s->gb);
437 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
442 level = ((-level * 2 + 1) * qscale) >> 1;
445 level = ((level * 2 + 1) * qscale) >> 1;
450 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
452 UPDATE_CACHE(re, &s->gb);
455 LAST_SKIP_BITS(re, &s->gb, 2);
456 CLOSE_READER(re, &s->gb);
457 s->block_last_index[n] = i;
462 static inline int mpeg2_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
464 int level, dc, diff, i, j, run;
467 uint8_t * const scantable = s->intra_scantable.permutated;
468 const uint16_t *quant_matrix;
469 const int qscale = s->qscale;
474 quant_matrix = s->intra_matrix;
477 quant_matrix = s->chroma_intra_matrix;
478 component = (n & 1) + 1;
480 diff = decode_dc(&s->gb, component);
483 dc = s->last_dc[component];
485 s->last_dc[component] = dc;
486 block[0] = dc << (3 - s->intra_dc_precision);
487 av_dlog(s->avctx, "dc=%d\n", block[0]);
488 mismatch = block[0] ^ 1;
490 if (s->intra_vlc_format)
496 OPEN_READER(re, &s->gb);
497 /* now quantify & encode AC coefficients */
499 UPDATE_CACHE(re, &s->gb);
500 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
504 } else if (level != 0) {
507 level = (level * qscale * quant_matrix[j]) >> 4;
508 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
509 LAST_SKIP_BITS(re, &s->gb, 1);
512 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
513 UPDATE_CACHE(re, &s->gb);
514 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
518 level = (-level * qscale * quant_matrix[j]) >> 4;
521 level = (level * qscale * quant_matrix[j]) >> 4;
525 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
532 CLOSE_READER(re, &s->gb);
534 block[63] ^= mismatch & 1;
536 s->block_last_index[n] = i;
541 * Note: this function can read out of range and crash for corrupt streams.
542 * Changing this would eat up any speed benefits it has.
543 * Do not use "fast" flag if you need the code to be robust.
545 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
547 int level, dc, diff, j, run;
550 uint8_t * scantable = s->intra_scantable.permutated;
551 const uint16_t *quant_matrix;
552 const int qscale = s->qscale;
556 quant_matrix = s->intra_matrix;
559 quant_matrix = s->chroma_intra_matrix;
560 component = (n & 1) + 1;
562 diff = decode_dc(&s->gb, component);
565 dc = s->last_dc[component];
567 s->last_dc[component] = dc;
568 block[0] = dc << (3 - s->intra_dc_precision);
569 if (s->intra_vlc_format)
575 OPEN_READER(re, &s->gb);
576 /* now quantify & encode AC coefficients */
578 UPDATE_CACHE(re, &s->gb);
579 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
583 } else if (level != 0) {
586 level = (level * qscale * quant_matrix[j]) >> 4;
587 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
588 LAST_SKIP_BITS(re, &s->gb, 1);
591 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
592 UPDATE_CACHE(re, &s->gb);
593 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
597 level = (-level * qscale * quant_matrix[j]) >> 4;
600 level = (level * qscale * quant_matrix[j]) >> 4;
606 CLOSE_READER(re, &s->gb);
609 s->block_last_index[n] = scantable - s->intra_scantable.permutated;
613 /******************************************/
616 static inline int get_dmv(MpegEncContext *s)
618 if (get_bits1(&s->gb))
619 return 1 - (get_bits1(&s->gb) << 1);
624 static inline int get_qscale(MpegEncContext *s)
626 int qscale = get_bits(&s->gb, 5);
627 if (s->q_scale_type) {
628 return non_linear_qscale[qscale];
634 static void exchange_uv(MpegEncContext *s)
639 s->pblocks[4] = s->pblocks[5];
643 /* motion type (for MPEG-2) */
649 static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
651 int i, j, k, cbp, val, mb_type, motion_type;
652 const int mb_block_count = 4 + (1 << s->chroma_format);
654 av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
656 av_assert2(s->mb_skipped == 0);
658 if (s->mb_skip_run-- != 0) {
659 if (s->pict_type == AV_PICTURE_TYPE_P) {
661 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
666 mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
668 mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1]; // FIXME not sure if this is allowed in MPEG at all
669 if (IS_INTRA(mb_type)) {
670 av_log(s->avctx, AV_LOG_ERROR, "skip with previntra\n");
673 s->current_picture.mb_type[s->mb_x + s->mb_y*s->mb_stride] =
674 mb_type | MB_TYPE_SKIP;
676 // assert(s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1] & (MB_TYPE_16x16 | MB_TYPE_16x8));
678 if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
685 switch (s->pict_type) {
687 case AV_PICTURE_TYPE_I:
688 if (get_bits1(&s->gb) == 0) {
689 if (get_bits1(&s->gb) == 0) {
690 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
693 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
695 mb_type = MB_TYPE_INTRA;
698 case AV_PICTURE_TYPE_P:
699 mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
701 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
704 mb_type = ptype2mb_type[mb_type];
706 case AV_PICTURE_TYPE_B:
707 mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
709 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
712 mb_type = btype2mb_type[mb_type];
715 av_dlog(s->avctx, "mb_type=%x\n", mb_type);
716 // motion_type = 0; /* avoid warning */
717 if (IS_INTRA(mb_type)) {
718 s->dsp.clear_blocks(s->block[0]);
720 if (!s->chroma_y_shift) {
721 s->dsp.clear_blocks(s->block[6]);
724 /* compute DCT type */
725 if (s->picture_structure == PICT_FRAME && // FIXME add an interlaced_dct coded var?
726 !s->frame_pred_frame_dct) {
727 s->interlaced_dct = get_bits1(&s->gb);
730 if (IS_QUANT(mb_type))
731 s->qscale = get_qscale(s);
733 if (s->concealment_motion_vectors) {
734 /* just parse them */
735 if (s->picture_structure != PICT_FRAME)
736 skip_bits1(&s->gb); /* field select */
738 s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
739 mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
740 s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
741 mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
743 skip_bits1(&s->gb); /* marker */
745 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
747 // if 1, we memcpy blocks in xvmcvideo
748 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
749 ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
755 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
756 if (s->flags2 & CODEC_FLAG2_FAST) {
757 for (i = 0; i < 6; i++) {
758 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
761 for (i = 0; i < mb_block_count; i++) {
762 if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
767 for (i = 0; i < 6; i++) {
768 if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
773 if (mb_type & MB_TYPE_ZERO_MV) {
774 av_assert2(mb_type & MB_TYPE_CBP);
776 s->mv_dir = MV_DIR_FORWARD;
777 if (s->picture_structure == PICT_FRAME) {
778 if (s->picture_structure == PICT_FRAME
779 && !s->frame_pred_frame_dct)
780 s->interlaced_dct = get_bits1(&s->gb);
781 s->mv_type = MV_TYPE_16X16;
783 s->mv_type = MV_TYPE_FIELD;
784 mb_type |= MB_TYPE_INTERLACED;
785 s->field_select[0][0] = s->picture_structure - 1;
788 if (IS_QUANT(mb_type))
789 s->qscale = get_qscale(s);
791 s->last_mv[0][0][0] = 0;
792 s->last_mv[0][0][1] = 0;
793 s->last_mv[0][1][0] = 0;
794 s->last_mv[0][1][1] = 0;
798 av_assert2(mb_type & MB_TYPE_L0L1);
799 // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
800 /* get additional motion vector type */
801 if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct)
802 motion_type = MT_FRAME;
804 motion_type = get_bits(&s->gb, 2);
805 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
806 s->interlaced_dct = get_bits1(&s->gb);
809 if (IS_QUANT(mb_type))
810 s->qscale = get_qscale(s);
813 s->mv_dir = (mb_type >> 13) & 3;
814 av_dlog(s->avctx, "motion_type=%d\n", motion_type);
815 switch (motion_type) {
816 case MT_FRAME: /* or MT_16X8 */
817 if (s->picture_structure == PICT_FRAME) {
818 mb_type |= MB_TYPE_16x16;
819 s->mv_type = MV_TYPE_16X16;
820 for (i = 0; i < 2; i++) {
821 if (USES_LIST(mb_type, i)) {
823 s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
824 mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
825 s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
826 mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
827 /* full_pel: only for MPEG-1 */
828 if (s->full_pel[i]) {
829 s->mv[i][0][0] <<= 1;
830 s->mv[i][0][1] <<= 1;
835 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
836 s->mv_type = MV_TYPE_16X8;
837 for (i = 0; i < 2; i++) {
838 if (USES_LIST(mb_type, i)) {
840 for (j = 0; j < 2; j++) {
841 s->field_select[i][j] = get_bits1(&s->gb);
842 for (k = 0; k < 2; k++) {
843 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
844 s->last_mv[i][j][k]);
845 s->last_mv[i][j][k] = val;
846 s->mv[i][j][k] = val;
854 s->mv_type = MV_TYPE_FIELD;
855 if (s->picture_structure == PICT_FRAME) {
856 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
857 for (i = 0; i < 2; i++) {
858 if (USES_LIST(mb_type, i)) {
859 for (j = 0; j < 2; j++) {
860 s->field_select[i][j] = get_bits1(&s->gb);
861 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
862 s->last_mv[i][j][0]);
863 s->last_mv[i][j][0] = val;
864 s->mv[i][j][0] = val;
865 av_dlog(s->avctx, "fmx=%d\n", val);
866 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
867 s->last_mv[i][j][1] >> 1);
868 s->last_mv[i][j][1] = val << 1;
869 s->mv[i][j][1] = val;
870 av_dlog(s->avctx, "fmy=%d\n", val);
875 av_assert0(!s->progressive_sequence);
876 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
877 for (i = 0; i < 2; i++) {
878 if (USES_LIST(mb_type, i)) {
879 s->field_select[i][0] = get_bits1(&s->gb);
880 for (k = 0; k < 2; k++) {
881 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
882 s->last_mv[i][0][k]);
883 s->last_mv[i][0][k] = val;
884 s->last_mv[i][1][k] = val;
885 s->mv[i][0][k] = val;
892 if(s->progressive_sequence){
893 av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
896 s->mv_type = MV_TYPE_DMV;
897 for (i = 0; i < 2; i++) {
898 if (USES_LIST(mb_type, i)) {
899 int dmx, dmy, mx, my, m;
900 const int my_shift = s->picture_structure == PICT_FRAME;
902 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
903 s->last_mv[i][0][0]);
904 s->last_mv[i][0][0] = mx;
905 s->last_mv[i][1][0] = mx;
907 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
908 s->last_mv[i][0][1] >> my_shift);
912 s->last_mv[i][0][1] = my << my_shift;
913 s->last_mv[i][1][1] = my << my_shift;
917 s->mv[i][1][0] = mx; // not used
918 s->mv[i][1][1] = my; // not used
920 if (s->picture_structure == PICT_FRAME) {
921 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
923 // m = 1 + 2 * s->top_field_first;
924 m = s->top_field_first ? 1 : 3;
926 /* top -> top pred */
927 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
928 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
930 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
931 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
933 mb_type |= MB_TYPE_16x16;
935 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
936 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
937 if (s->picture_structure == PICT_TOP_FIELD)
946 av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
952 if (HAS_CBP(mb_type)) {
953 s->dsp.clear_blocks(s->block[0]);
955 cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
956 if (mb_block_count > 6) {
957 cbp <<= mb_block_count - 6;
958 cbp |= get_bits(&s->gb, mb_block_count - 6);
959 s->dsp.clear_blocks(s->block[6]);
962 av_log(s->avctx, AV_LOG_ERROR, "invalid cbp %d at %d %d\n", cbp, s->mb_x, s->mb_y);
966 //if 1, we memcpy blocks in xvmcvideo
967 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
968 ff_xvmc_pack_pblocks(s, cbp);
974 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
975 if (s->flags2 & CODEC_FLAG2_FAST) {
976 for (i = 0; i < 6; i++) {
978 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
980 s->block_last_index[i] = -1;
985 cbp <<= 12-mb_block_count;
987 for (i = 0; i < mb_block_count; i++) {
988 if (cbp & (1 << 11)) {
989 if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
992 s->block_last_index[i] = -1;
998 if (s->flags2 & CODEC_FLAG2_FAST) {
999 for (i = 0; i < 6; i++) {
1001 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
1003 s->block_last_index[i] = -1;
1008 for (i = 0; i < 6; i++) {
1010 if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
1013 s->block_last_index[i] = -1;
1020 for (i = 0; i < 12; i++)
1021 s->block_last_index[i] = -1;
1025 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
1030 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1032 Mpeg1Context *s = avctx->priv_data;
1033 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1036 /* we need some permutation to store matrices,
1037 * until MPV_common_init() sets the real permutation. */
1038 for (i = 0; i < 64; i++)
1039 s2->dsp.idct_permutation[i]=i;
1041 ff_MPV_decode_defaults(s2);
1043 s->mpeg_enc_ctx.avctx = avctx;
1044 s->mpeg_enc_ctx.flags = avctx->flags;
1045 s->mpeg_enc_ctx.flags2 = avctx->flags2;
1046 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1047 ff_mpeg12_init_vlcs();
1049 s->mpeg_enc_ctx_allocated = 0;
1050 s->mpeg_enc_ctx.picture_number = 0;
1051 s->repeat_field = 0;
1052 s->mpeg_enc_ctx.codec_id = avctx->codec->id;
1053 avctx->color_range = AVCOL_RANGE_MPEG;
1054 if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
1055 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1057 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1061 static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
1063 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1064 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1067 if (avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized)
1070 err = ff_mpeg_update_thread_context(avctx, avctx_from);
1071 if (err) return err;
1073 if (!ctx->mpeg_enc_ctx_allocated)
1074 memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
1076 if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
1077 s->picture_number++;
1082 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1083 const uint8_t *new_perm)
1085 uint16_t temp_matrix[64];
1088 memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
1090 for (i = 0; i < 64; i++) {
1091 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1095 static const enum AVPixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
1096 #if CONFIG_MPEG_XVMC_DECODER
1097 AV_PIX_FMT_XVMC_MPEG2_IDCT,
1098 AV_PIX_FMT_XVMC_MPEG2_MC,
1100 #if CONFIG_MPEG1_VDPAU_HWACCEL
1101 AV_PIX_FMT_VDPAU_MPEG1,
1108 static const enum AVPixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
1109 #if CONFIG_MPEG_XVMC_DECODER
1110 AV_PIX_FMT_XVMC_MPEG2_IDCT,
1111 AV_PIX_FMT_XVMC_MPEG2_MC,
1113 #if CONFIG_MPEG2_VDPAU_HWACCEL
1114 AV_PIX_FMT_VDPAU_MPEG2,
1117 #if CONFIG_MPEG2_DXVA2_HWACCEL
1118 AV_PIX_FMT_DXVA2_VLD,
1120 #if CONFIG_MPEG2_VAAPI_HWACCEL
1121 AV_PIX_FMT_VAAPI_VLD,
1127 static inline int uses_vdpau(AVCodecContext *avctx) {
1128 return avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG2;
1131 static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
1133 Mpeg1Context *s1 = avctx->priv_data;
1134 MpegEncContext *s = &s1->mpeg_enc_ctx;
1136 if(s->chroma_format < 2) {
1137 return ff_thread_get_format(avctx,
1138 avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO ?
1139 mpeg1_hwaccel_pixfmt_list_420 :
1140 mpeg2_hwaccel_pixfmt_list_420);
1141 } else if(s->chroma_format == 2)
1142 return AV_PIX_FMT_YUV422P;
1144 return AV_PIX_FMT_YUV444P;
1147 static void setup_hwaccel_for_pixfmt(AVCodecContext *avctx)
1149 if (avctx->pix_fmt != AV_PIX_FMT_XVMC_MPEG2_IDCT && avctx->pix_fmt != AV_PIX_FMT_XVMC_MPEG2_MC) {
1150 avctx->xvmc_acceleration = 0;
1151 } else if (!avctx->xvmc_acceleration) {
1152 avctx->xvmc_acceleration = 2;
1154 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
1155 // until then pix_fmt may be changed right after codec init
1156 if (avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT ||
1157 avctx->hwaccel || uses_vdpau(avctx))
1158 if (avctx->idct_algo == FF_IDCT_AUTO)
1159 avctx->idct_algo = FF_IDCT_SIMPLE;
1162 /* Call this function when we know all parameters.
1163 * It may be called in different places for MPEG-1 and MPEG-2. */
1164 static int mpeg_decode_postinit(AVCodecContext *avctx)
1166 Mpeg1Context *s1 = avctx->priv_data;
1167 MpegEncContext *s = &s1->mpeg_enc_ctx;
1168 uint8_t old_permutation[64];
1170 if ((s1->mpeg_enc_ctx_allocated == 0) ||
1171 avctx->coded_width != s->width ||
1172 avctx->coded_height != s->height ||
1173 s1->save_width != s->width ||
1174 s1->save_height != s->height ||
1175 s1->save_aspect_info != s->aspect_ratio_info ||
1176 s1->save_progressive_seq != s->progressive_sequence ||
1180 if (s1->mpeg_enc_ctx_allocated) {
1181 ParseContext pc = s->parse_context;
1182 s->parse_context.buffer = 0;
1183 ff_MPV_common_end(s);
1184 s->parse_context = pc;
1187 if ((s->width == 0) || (s->height == 0))
1190 avcodec_set_dimensions(avctx, s->width, s->height);
1191 if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->bit_rate) {
1192 avctx->rc_max_rate = s->bit_rate;
1193 } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && s->bit_rate &&
1194 (s->bit_rate != 0x3FFFF*400 || s->vbv_delay != 0xFFFF)) {
1195 avctx->bit_rate = s->bit_rate;
1197 s1->save_aspect_info = s->aspect_ratio_info;
1198 s1->save_width = s->width;
1199 s1->save_height = s->height;
1200 s1->save_progressive_seq = s->progressive_sequence;
1202 /* low_delay may be forced, in this case we will have B-frames
1203 * that behave like P-frames. */
1204 avctx->has_b_frames = !s->low_delay;
1206 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1208 avctx->time_base.den = ff_mpeg12_frame_rate_tab[s->frame_rate_index].num;
1209 avctx->time_base.num = ff_mpeg12_frame_rate_tab[s->frame_rate_index].den;
1211 avctx->sample_aspect_ratio = av_d2q(1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1212 avctx->ticks_per_frame=1;
1215 av_reduce(&s->avctx->time_base.den,
1216 &s->avctx->time_base.num,
1217 ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
1218 ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1220 avctx->ticks_per_frame = 2;
1222 if (s->aspect_ratio_info > 1) {
1224 av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1225 (AVRational) {s1->pan_scan.width, s1->pan_scan.height}),
1226 (AVRational) {s->width, s->height});
1228 // we ignore the spec here and guess a bit as reality does not match the spec, see for example
1229 // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
1230 // issue1613, 621, 562
1231 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1232 (av_cmp_q(dar, (AVRational) {4, 3}) && av_cmp_q(dar, (AVRational) {16, 9}))) {
1233 s->avctx->sample_aspect_ratio =
1234 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1235 (AVRational) {s->width, s->height});
1237 s->avctx->sample_aspect_ratio =
1238 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1239 (AVRational) {s1->pan_scan.width, s1->pan_scan.height});
1240 //issue1613 4/3 16/9 -> 16/9
1241 //res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1242 //widescreen-issue562.mpg 4/3 16/9 -> 16/9
1243 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1244 av_dlog(avctx, "A %d/%d\n",
1245 ff_mpeg2_aspect[s->aspect_ratio_info].num, ff_mpeg2_aspect[s->aspect_ratio_info].den);
1246 av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
1247 s->avctx->sample_aspect_ratio.den);
1250 s->avctx->sample_aspect_ratio =
1251 ff_mpeg2_aspect[s->aspect_ratio_info];
1255 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1256 setup_hwaccel_for_pixfmt(avctx);
1258 /* Quantization matrices may need reordering
1259 * if DCT permutation is changed. */
1260 memcpy(old_permutation, s->dsp.idct_permutation, 64 * sizeof(uint8_t));
1262 if (ff_MPV_common_init(s) < 0)
1265 quant_matrix_rebuild(s->intra_matrix, old_permutation, s->dsp.idct_permutation);
1266 quant_matrix_rebuild(s->inter_matrix, old_permutation, s->dsp.idct_permutation);
1267 quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->dsp.idct_permutation);
1268 quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->dsp.idct_permutation);
1270 s1->mpeg_enc_ctx_allocated = 1;
1275 static int mpeg1_decode_picture(AVCodecContext *avctx,
1276 const uint8_t *buf, int buf_size)
1278 Mpeg1Context *s1 = avctx->priv_data;
1279 MpegEncContext *s = &s1->mpeg_enc_ctx;
1280 int ref, f_code, vbv_delay;
1282 init_get_bits(&s->gb, buf, buf_size*8);
1284 ref = get_bits(&s->gb, 10); /* temporal ref */
1285 s->pict_type = get_bits(&s->gb, 3);
1286 if (s->pict_type == 0 || s->pict_type > 3)
1289 vbv_delay = get_bits(&s->gb, 16);
1290 s->vbv_delay = vbv_delay;
1291 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
1292 s->full_pel[0] = get_bits1(&s->gb);
1293 f_code = get_bits(&s->gb, 3);
1294 if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1297 s->mpeg_f_code[0][0] = f_code;
1298 s->mpeg_f_code[0][1] = f_code;
1300 if (s->pict_type == AV_PICTURE_TYPE_B) {
1301 s->full_pel[1] = get_bits1(&s->gb);
1302 f_code = get_bits(&s->gb, 3);
1303 if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1306 s->mpeg_f_code[1][0] = f_code;
1307 s->mpeg_f_code[1][1] = f_code;
1309 s->current_picture.f.pict_type = s->pict_type;
1310 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1312 if (avctx->debug & FF_DEBUG_PICT_INFO)
1313 av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1320 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1322 MpegEncContext *s= &s1->mpeg_enc_ctx;
1323 int horiz_size_ext, vert_size_ext;
1326 skip_bits(&s->gb, 1); /* profile and level esc*/
1327 s->avctx->profile = get_bits(&s->gb, 3);
1328 s->avctx->level = get_bits(&s->gb, 4);
1329 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1330 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1331 horiz_size_ext = get_bits(&s->gb, 2);
1332 vert_size_ext = get_bits(&s->gb, 2);
1333 s->width |= (horiz_size_ext << 12);
1334 s->height |= (vert_size_ext << 12);
1335 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1336 s->bit_rate += (bit_rate_ext << 18) * 400;
1337 skip_bits1(&s->gb); /* marker */
1338 s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1340 s->low_delay = get_bits1(&s->gb);
1341 if (s->flags & CODEC_FLAG_LOW_DELAY)
1344 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1345 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1347 av_dlog(s->avctx, "sequence extension\n");
1348 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1350 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1351 av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1352 s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
1356 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1358 MpegEncContext *s = &s1->mpeg_enc_ctx;
1359 int color_description, w, h;
1361 skip_bits(&s->gb, 3); /* video format */
1362 color_description = get_bits1(&s->gb);
1363 if (color_description) {
1364 s->avctx->color_primaries = get_bits(&s->gb, 8);
1365 s->avctx->color_trc = get_bits(&s->gb, 8);
1366 s->avctx->colorspace = get_bits(&s->gb, 8);
1368 w = get_bits(&s->gb, 14);
1369 skip_bits(&s->gb, 1); //marker
1370 h = get_bits(&s->gb, 14);
1371 // remaining 3 bits are zero padding
1373 s1->pan_scan.width = 16 * w;
1374 s1->pan_scan.height = 16 * h;
1376 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1377 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1380 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1382 MpegEncContext *s = &s1->mpeg_enc_ctx;
1386 if (s->progressive_sequence) {
1387 if (s->repeat_first_field) {
1389 if (s->top_field_first)
1393 if (s->picture_structure == PICT_FRAME) {
1395 if (s->repeat_first_field)
1399 for (i = 0; i < nofco; i++) {
1400 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1401 skip_bits(&s->gb, 1); // marker
1402 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1403 skip_bits(&s->gb, 1); // marker
1406 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1407 av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
1408 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1409 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1410 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1413 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra)
1417 for (i = 0; i < 64; i++) {
1418 int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1419 int v = get_bits(&s->gb, 8);
1421 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1424 if (intra && i == 0 && v != 8) {
1425 av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v);
1426 v = 8; // needed by pink.mpg / issue1046
1435 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1437 av_dlog(s->avctx, "matrix extension\n");
1439 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1440 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1441 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
1442 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
1445 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1447 MpegEncContext *s = &s1->mpeg_enc_ctx;
1449 s->full_pel[0] = s->full_pel[1] = 0;
1450 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1451 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1452 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1453 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1454 if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1455 av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
1456 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1457 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1458 s->pict_type = AV_PICTURE_TYPE_I;
1460 s->pict_type = AV_PICTURE_TYPE_P;
1462 s->pict_type = AV_PICTURE_TYPE_B;
1463 s->current_picture.f.pict_type = s->pict_type;
1464 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1466 s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0];
1467 s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1];
1468 s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0];
1469 s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1];
1471 s->intra_dc_precision = get_bits(&s->gb, 2);
1472 s->picture_structure = get_bits(&s->gb, 2);
1473 s->top_field_first = get_bits1(&s->gb);
1474 s->frame_pred_frame_dct = get_bits1(&s->gb);
1475 s->concealment_motion_vectors = get_bits1(&s->gb);
1476 s->q_scale_type = get_bits1(&s->gb);
1477 s->intra_vlc_format = get_bits1(&s->gb);
1478 s->alternate_scan = get_bits1(&s->gb);
1479 s->repeat_first_field = get_bits1(&s->gb);
1480 s->chroma_420_type = get_bits1(&s->gb);
1481 s->progressive_frame = get_bits1(&s->gb);
1484 if (s->alternate_scan) {
1485 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
1486 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
1488 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
1489 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
1492 /* composite display not parsed */
1493 av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1494 av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1495 av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1496 av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1497 av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1498 av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1499 av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1500 av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1501 av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1504 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1506 AVCodecContext *avctx = s->avctx;
1507 Mpeg1Context *s1 = (Mpeg1Context*)s;
1509 /* start frame decoding */
1510 if (s->first_field || s->picture_structure == PICT_FRAME) {
1511 AVFrameSideData *pan_scan;
1513 if (ff_MPV_frame_start(s, avctx) < 0)
1516 ff_mpeg_er_frame_start(s);
1518 /* first check if we must repeat the frame */
1519 s->current_picture_ptr->f.repeat_pict = 0;
1520 if (s->repeat_first_field) {
1521 if (s->progressive_sequence) {
1522 if (s->top_field_first)
1523 s->current_picture_ptr->f.repeat_pict = 4;
1525 s->current_picture_ptr->f.repeat_pict = 2;
1526 } else if (s->progressive_frame) {
1527 s->current_picture_ptr->f.repeat_pict = 1;
1531 pan_scan = av_frame_new_side_data(&s->current_picture_ptr->f,
1532 AV_FRAME_DATA_PANSCAN,
1533 sizeof(s1->pan_scan));
1535 return AVERROR(ENOMEM);
1536 memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
1538 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1539 ff_thread_finish_setup(avctx);
1540 } else { // second field
1543 if (!s->current_picture_ptr) {
1544 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1548 if (s->avctx->hwaccel &&
1549 (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
1550 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1551 av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode first field\n");
1554 for (i = 0; i < 4; i++) {
1555 s->current_picture.f.data[i] = s->current_picture_ptr->f.data[i];
1556 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1557 s->current_picture.f.data[i] += s->current_picture_ptr->f.linesize[i];
1562 if (avctx->hwaccel) {
1563 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
1567 // MPV_frame_start will call this function too,
1568 // but we need to call it on every field
1569 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1570 if (ff_xvmc_field_start(s, avctx) < 0)
1576 #define DECODE_SLICE_ERROR -1
1577 #define DECODE_SLICE_OK 0
1581 * MpegEncContext.mb_y must be set to the MB row from the startcode.
1582 * @return DECODE_SLICE_ERROR if the slice is damaged,
1583 * DECODE_SLICE_OK if this slice is OK
1585 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1586 const uint8_t **buf, int buf_size)
1588 AVCodecContext *avctx = s->avctx;
1589 const int lowres = s->avctx->lowres;
1590 const int field_pic = s->picture_structure != PICT_FRAME;
1593 s->resync_mb_y = -1;
1595 av_assert0(mb_y < s->mb_height);
1597 init_get_bits(&s->gb, *buf, buf_size * 8);
1598 if(s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
1599 skip_bits(&s->gb, 3);
1601 ff_mpeg1_clean_buffers(s);
1602 s->interlaced_dct = 0;
1604 s->qscale = get_qscale(s);
1606 if (s->qscale == 0) {
1607 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1611 /* extra slice info */
1612 while (get_bits1(&s->gb) != 0) {
1613 skip_bits(&s->gb, 8);
1618 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1621 while (get_bits_left(&s->gb) > 0) {
1622 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1623 MBINCR_VLC_BITS, 2);
1625 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1632 /* otherwise, stuffing, nothing to do */
1640 if (s->mb_x >= (unsigned)s->mb_width) {
1641 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1645 if (avctx->hwaccel) {
1646 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1647 int start_code = -1;
1648 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1649 if (buf_end < *buf + buf_size)
1652 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1653 return DECODE_SLICE_ERROR;
1655 return DECODE_SLICE_OK;
1658 s->resync_mb_x = s->mb_x;
1659 s->resync_mb_y = s->mb_y = mb_y;
1661 ff_init_block_index(s);
1663 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1664 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1665 av_log(s->avctx, AV_LOG_DEBUG, "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",
1666 s->qscale, s->mpeg_f_code[0][0], s->mpeg_f_code[0][1], s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1667 s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
1668 s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
1669 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
1670 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
1675 // If 1, we memcpy blocks in xvmcvideo.
1676 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1677 ff_xvmc_init_block(s); // set s->block
1679 if (mpeg_decode_mb(s, s->block) < 0)
1682 if (s->current_picture.motion_val[0] && !s->encoding) { // note motion_val is normally NULL unless we want to extract the MVs
1683 const int wrap = s->b8_stride;
1684 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1685 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1686 int motion_x, motion_y, dir, i;
1688 for (i = 0; i < 2; i++) {
1689 for (dir = 0; dir < 2; dir++) {
1690 if (s->mb_intra || (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1691 motion_x = motion_y = 0;
1692 } else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1693 motion_x = s->mv[dir][0][0];
1694 motion_y = s->mv[dir][0][1];
1695 } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
1696 motion_x = s->mv[dir][i][0];
1697 motion_y = s->mv[dir][i][1];
1700 s->current_picture.motion_val[dir][xy ][0] = motion_x;
1701 s->current_picture.motion_val[dir][xy ][1] = motion_y;
1702 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1703 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1704 s->current_picture.ref_index [dir][b8_xy ] =
1705 s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1706 av_assert2(s->field_select[dir][i] == 0 || s->field_select[dir][i] == 1);
1713 s->dest[0] += 16 >> lowres;
1714 s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
1715 s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
1717 ff_MPV_decode_mb(s, s->block);
1719 if (++s->mb_x >= s->mb_width) {
1720 const int mb_size = 16 >> s->avctx->lowres;
1722 ff_mpeg_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
1723 ff_MPV_report_decode_progress(s);
1726 s->mb_y += 1 << field_pic;
1728 if (s->mb_y >= s->mb_height) {
1729 int left = get_bits_left(&s->gb);
1730 int is_d10 = s->chroma_format == 2 && s->pict_type == AV_PICTURE_TYPE_I && avctx->profile == 0 && avctx->level == 5
1731 && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
1732 && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
1734 if (left >= 32 && !is_d10) {
1735 GetBitContext gb = s->gb;
1736 align_get_bits(&gb);
1737 if (show_bits(&gb, 24) == 0x060E2B) {
1738 av_log(avctx, AV_LOG_DEBUG, "Invalid MXF data found in video stream\n");
1743 if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
1744 || ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) {
1745 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
1751 ff_init_block_index(s);
1754 /* skip mb handling */
1755 if (s->mb_skip_run == -1) {
1756 /* read increment again */
1759 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1760 MBINCR_VLC_BITS, 2);
1762 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1767 s->mb_skip_run += 33;
1768 } else if (code == 35) {
1769 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1770 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1773 goto eos; /* end of slice */
1775 /* otherwise, stuffing, nothing to do */
1777 s->mb_skip_run += code;
1781 if (s->mb_skip_run) {
1783 if (s->pict_type == AV_PICTURE_TYPE_I) {
1784 av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1790 for (i = 0; i < 12; i++)
1791 s->block_last_index[i] = -1;
1792 if (s->picture_structure == PICT_FRAME)
1793 s->mv_type = MV_TYPE_16X16;
1795 s->mv_type = MV_TYPE_FIELD;
1796 if (s->pict_type == AV_PICTURE_TYPE_P) {
1797 /* if P type, zero motion vector is implied */
1798 s->mv_dir = MV_DIR_FORWARD;
1799 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1800 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1801 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1802 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1804 /* if B type, reuse previous vectors and directions */
1805 s->mv[0][0][0] = s->last_mv[0][0][0];
1806 s->mv[0][0][1] = s->last_mv[0][0][1];
1807 s->mv[1][0][0] = s->last_mv[1][0][0];
1808 s->mv[1][0][1] = s->last_mv[1][0][1];
1813 eos: // end of slice
1814 *buf += (get_bits_count(&s->gb)-1)/8;
1815 av_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1819 static int slice_decode_thread(AVCodecContext *c, void *arg)
1821 MpegEncContext *s = *(void**)arg;
1822 const uint8_t *buf = s->gb.buffer;
1823 int mb_y = s->start_mb_y;
1824 const int field_pic = s->picture_structure != PICT_FRAME;
1826 s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
1829 uint32_t start_code;
1832 ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
1834 av_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1835 ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
1836 s->start_mb_y, s->end_mb_y, s->er.error_count);
1838 if (c->err_recognition & AV_EF_EXPLODE)
1840 if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
1841 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
1843 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_AC_END | ER_DC_END | ER_MV_END);
1846 if (s->mb_y == s->end_mb_y)
1850 buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
1851 mb_y= start_code - SLICE_MIN_START_CODE;
1852 if(s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
1853 mb_y += (*buf&0xE0)<<2;
1855 if (s->picture_structure == PICT_BOTTOM_FIELD)
1857 if (mb_y < 0 || mb_y >= s->end_mb_y)
1863 * Handle slice ends.
1864 * @return 1 if it seems to be the last slice
1866 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
1868 Mpeg1Context *s1 = avctx->priv_data;
1869 MpegEncContext *s = &s1->mpeg_enc_ctx;
1871 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
1874 if (s->avctx->hwaccel) {
1875 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1876 av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
1879 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1880 ff_xvmc_field_end(s);
1882 /* end of slice reached */
1883 if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field && !s->first_slice) {
1886 ff_er_frame_end(&s->er);
1888 ff_MPV_frame_end(s);
1890 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
1891 int ret = av_frame_ref(pict, &s->current_picture_ptr->f);
1894 ff_print_debug_info(s, s->current_picture_ptr, pict);
1895 ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG2);
1897 if (avctx->active_thread_type & FF_THREAD_FRAME)
1898 s->picture_number++;
1899 /* latency of 1 frame for I- and P-frames */
1900 /* XXX: use another variable than picture_number */
1901 if (s->last_picture_ptr != NULL) {
1902 int ret = av_frame_ref(pict, &s->last_picture_ptr->f);
1905 ff_print_debug_info(s, s->last_picture_ptr, pict);
1906 ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG2);
1916 static int mpeg1_decode_sequence(AVCodecContext *avctx,
1917 const uint8_t *buf, int buf_size)
1919 Mpeg1Context *s1 = avctx->priv_data;
1920 MpegEncContext *s = &s1->mpeg_enc_ctx;
1924 init_get_bits(&s->gb, buf, buf_size*8);
1926 width = get_bits(&s->gb, 12);
1927 height = get_bits(&s->gb, 12);
1928 if (width == 0 || height == 0) {
1929 av_log(avctx, AV_LOG_WARNING, "Invalid horizontal or vertical size "
1931 if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
1932 return AVERROR_INVALIDDATA;
1934 s->aspect_ratio_info = get_bits(&s->gb, 4);
1935 if (s->aspect_ratio_info == 0) {
1936 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
1937 if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
1940 s->frame_rate_index = get_bits(&s->gb, 4);
1941 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
1943 s->bit_rate = get_bits(&s->gb, 18) * 400;
1944 if (get_bits1(&s->gb) == 0) /* marker */
1949 s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
1950 skip_bits(&s->gb, 1);
1953 if (get_bits1(&s->gb)) {
1954 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1956 for (i = 0; i < 64; i++) {
1957 j = s->dsp.idct_permutation[i];
1958 v = ff_mpeg1_default_intra_matrix[i];
1959 s->intra_matrix[j] = v;
1960 s->chroma_intra_matrix[j] = v;
1963 if (get_bits1(&s->gb)) {
1964 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1966 for (i = 0; i < 64; i++) {
1967 int j = s->dsp.idct_permutation[i];
1968 v = ff_mpeg1_default_non_intra_matrix[i];
1969 s->inter_matrix[j] = v;
1970 s->chroma_inter_matrix[j] = v;
1974 if (show_bits(&s->gb, 23) != 0) {
1975 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
1979 /* we set MPEG-2 parameters so that it emulates MPEG-1 */
1980 s->progressive_sequence = 1;
1981 s->progressive_frame = 1;
1982 s->picture_structure = PICT_FRAME;
1984 s->frame_pred_frame_dct = 1;
1985 s->chroma_format = 1;
1986 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
1987 s->out_format = FMT_MPEG1;
1988 s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
1989 if (s->flags & CODEC_FLAG_LOW_DELAY)
1992 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1993 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
1994 s->avctx->rc_buffer_size, s->bit_rate);
1999 static int vcr2_init_sequence(AVCodecContext *avctx)
2001 Mpeg1Context *s1 = avctx->priv_data;
2002 MpegEncContext *s = &s1->mpeg_enc_ctx;
2005 /* start new MPEG-1 context decoding */
2006 s->out_format = FMT_MPEG1;
2007 if (s1->mpeg_enc_ctx_allocated) {
2008 ff_MPV_common_end(s);
2010 s->width = avctx->coded_width;
2011 s->height = avctx->coded_height;
2012 avctx->has_b_frames = 0; // true?
2015 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2016 setup_hwaccel_for_pixfmt(avctx);
2018 if (ff_MPV_common_init(s) < 0)
2020 s1->mpeg_enc_ctx_allocated = 1;
2022 for (i = 0; i < 64; i++) {
2023 int j = s->dsp.idct_permutation[i];
2024 v = ff_mpeg1_default_intra_matrix[i];
2025 s->intra_matrix[j] = v;
2026 s->chroma_intra_matrix[j] = v;
2028 v = ff_mpeg1_default_non_intra_matrix[i];
2029 s->inter_matrix[j] = v;
2030 s->chroma_inter_matrix[j] = v;
2033 s->progressive_sequence = 1;
2034 s->progressive_frame = 1;
2035 s->picture_structure = PICT_FRAME;
2037 s->frame_pred_frame_dct = 1;
2038 s->chroma_format = 1;
2039 if (s->codec_tag == AV_RL32("BW10")) {
2040 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
2042 exchange_uv(s); // common init reset pblocks, so we swap them here
2043 s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
2044 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
2046 s1->save_width = s->width;
2047 s1->save_height = s->height;
2048 s1->save_progressive_seq = s->progressive_sequence;
2053 static void mpeg_decode_user_data(AVCodecContext *avctx,
2054 const uint8_t *p, int buf_size)
2056 Mpeg1Context *s = avctx->priv_data;
2057 const uint8_t *buf_end = p + buf_size;
2062 if(!memcmp(p+i, "\0TMPGEXS\0", 9)){
2066 /* for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
2067 av_log(avctx, AV_LOG_ERROR, "%c", p[i]);
2069 av_log(avctx, AV_LOG_ERROR, "\n");*/
2072 /* we parse the DTG active format information */
2073 if (buf_end - p >= 5 &&
2074 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2082 if (buf_end - p < 1)
2084 avctx->dtg_active_format = p[0] & 0x0f;
2089 static void mpeg_decode_gop(AVCodecContext *avctx,
2090 const uint8_t *buf, int buf_size)
2092 Mpeg1Context *s1 = avctx->priv_data;
2093 MpegEncContext *s = &s1->mpeg_enc_ctx;
2097 init_get_bits(&s->gb, buf, buf_size*8);
2099 tc = avctx->timecode_frame_start = get_bits(&s->gb, 25);
2101 s->closed_gop = get_bits1(&s->gb);
2102 /*broken_link indicate that after editing the
2103 reference frames of the first B-Frames after GOP I-Frame
2104 are missing (open gop)*/
2105 broken_link = get_bits1(&s->gb);
2107 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2108 char tcbuf[AV_TIMECODE_STR_SIZE];
2109 av_timecode_make_mpeg_tc_string(tcbuf, tc);
2110 av_log(s->avctx, AV_LOG_DEBUG,
2111 "GOP (%s) closed_gop=%d broken_link=%d\n",
2112 tcbuf, s->closed_gop, broken_link);
2116 static int decode_chunks(AVCodecContext *avctx,
2117 AVFrame *picture, int *got_output,
2118 const uint8_t *buf, int buf_size)
2120 Mpeg1Context *s = avctx->priv_data;
2121 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2122 const uint8_t *buf_ptr = buf;
2123 const uint8_t *buf_end = buf + buf_size;
2124 int ret, input_size;
2126 int picture_start_code_seen = 0;
2129 /* find next start code */
2130 uint32_t start_code = -1;
2131 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
2132 if (start_code > 0x1ff) {
2133 if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {
2134 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
2136 av_assert0(avctx->thread_count > 1);
2138 avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
2139 for (i = 0; i < s->slice_count; i++)
2140 s2->er.error_count += s2->thread_context[i]->er.error_count;
2143 if ((CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER)
2144 && uses_vdpau(avctx))
2145 ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
2147 ret = slice_end(avctx, picture);
2151 if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
2156 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2159 input_size = buf_end - buf_ptr;
2161 if (avctx->debug & FF_DEBUG_STARTCODE) {
2162 av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
2165 /* prepare data for next start code */
2166 switch (start_code) {
2167 case SEQ_START_CODE:
2168 if (last_code == 0) {
2169 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2170 if(buf != avctx->extradata)
2173 av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
2174 if (avctx->err_recognition & AV_EF_EXPLODE)
2175 return AVERROR_INVALIDDATA;
2179 case PICTURE_START_CODE:
2180 if (picture_start_code_seen && s2->picture_structure == PICT_FRAME) {
2181 /* If it's a frame picture, there can't be more than one picture header.
2182 Yet, it does happen and we need to handle it. */
2183 av_log(avctx, AV_LOG_WARNING, "ignoring extra picture following a frame-picture\n");
2186 picture_start_code_seen = 1;
2188 if (s2->width <= 0 || s2->height <= 0) {
2189 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
2190 s2->width, s2->height);
2191 return AVERROR_INVALIDDATA;
2195 s2->intra_dc_precision= 3;
2196 s2->intra_matrix[0]= 1;
2198 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {
2201 avctx->execute(avctx, slice_decode_thread,
2202 s2->thread_context, NULL,
2203 s->slice_count, sizeof(void*));
2204 for (i = 0; i < s->slice_count; i++)
2205 s2->er.error_count += s2->thread_context[i]->er.error_count;
2208 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2209 ret = mpeg_decode_postinit(avctx);
2211 av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
2215 /* we have a complete image: we try to decompress it */
2216 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2218 s2->first_slice = 1;
2219 last_code = PICTURE_START_CODE;
2221 av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
2222 if (avctx->err_recognition & AV_EF_EXPLODE)
2223 return AVERROR_INVALIDDATA;
2226 case EXT_START_CODE:
2227 init_get_bits(&s2->gb, buf_ptr, input_size*8);
2229 switch (get_bits(&s2->gb, 4)) {
2231 if (last_code == 0) {
2232 mpeg_decode_sequence_extension(s);
2234 av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
2235 if (avctx->err_recognition & AV_EF_EXPLODE)
2236 return AVERROR_INVALIDDATA;
2240 mpeg_decode_sequence_display_extension(s);
2243 mpeg_decode_quant_matrix_extension(s2);
2246 mpeg_decode_picture_display_extension(s);
2249 if (last_code == PICTURE_START_CODE) {
2250 mpeg_decode_picture_coding_extension(s);
2252 av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
2253 if (avctx->err_recognition & AV_EF_EXPLODE)
2254 return AVERROR_INVALIDDATA;
2259 case USER_START_CODE:
2260 mpeg_decode_user_data(avctx, buf_ptr, input_size);
2262 case GOP_START_CODE:
2263 if (last_code == 0) {
2265 mpeg_decode_gop(avctx, buf_ptr, input_size);
2268 av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
2269 if (avctx->err_recognition & AV_EF_EXPLODE)
2270 return AVERROR_INVALIDDATA;
2274 if (start_code >= SLICE_MIN_START_CODE &&
2275 start_code <= SLICE_MAX_START_CODE && last_code == PICTURE_START_CODE) {
2277 if (s2->progressive_sequence && !s2->progressive_frame) {
2278 s2->progressive_frame = 1;
2279 av_log(s2->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
2282 if (s2->picture_structure == 0 || (s2->progressive_frame && s2->picture_structure != PICT_FRAME)) {
2283 av_log(s2->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s2->picture_structure);
2284 s2->picture_structure = PICT_FRAME;
2287 if (s2->progressive_sequence && !s2->frame_pred_frame_dct) {
2288 av_log(s2->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
2291 if (s2->picture_structure == PICT_FRAME) {
2292 s2->first_field = 0;
2293 s2->v_edge_pos = 16 * s2->mb_height;
2295 s2->first_field ^= 1;
2296 s2->v_edge_pos = 8 * s2->mb_height;
2297 memset(s2->mbskip_table, 0, s2->mb_stride * s2->mb_height);
2300 if (start_code >= SLICE_MIN_START_CODE &&
2301 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2302 const int field_pic = s2->picture_structure != PICT_FRAME;
2303 int mb_y = start_code - SLICE_MIN_START_CODE;
2304 last_code = SLICE_MIN_START_CODE;
2305 if(s2->codec_id != AV_CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
2306 mb_y += (*buf_ptr&0xE0)<<2;
2309 if (s2->picture_structure == PICT_BOTTOM_FIELD)
2312 if (mb_y >= s2->mb_height) {
2313 av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2317 if (s2->last_picture_ptr == NULL) {
2318 /* Skip B-frames if we do not have reference frames and gop is not closed */
2319 if (s2->pict_type == AV_PICTURE_TYPE_B) {
2320 if (!s2->closed_gop)
2324 if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->flags2 & CODEC_FLAG2_SHOW_ALL))
2326 if (s2->next_picture_ptr == NULL) {
2327 /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
2328 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) break;
2330 if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||
2331 (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||
2332 avctx->skip_frame >= AVDISCARD_ALL)
2335 if (!s->mpeg_enc_ctx_allocated)
2338 if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2339 if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
2343 if (!s2->pict_type) {
2344 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2345 if (avctx->err_recognition & AV_EF_EXPLODE)
2346 return AVERROR_INVALIDDATA;
2350 if (s2->first_slice) {
2351 s2->first_slice = 0;
2352 if (mpeg_field_start(s2, buf, buf_size) < 0)
2355 if (!s2->current_picture_ptr) {
2356 av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
2357 return AVERROR_INVALIDDATA;
2360 if (uses_vdpau(avctx)) {
2365 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
2366 int threshold = (s2->mb_height * s->slice_count +
2367 s2->slice_context_count / 2) /
2368 s2->slice_context_count;
2369 av_assert0(avctx->thread_count > 1);
2370 if (threshold <= mb_y) {
2371 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2373 thread_context->start_mb_y = mb_y;
2374 thread_context->end_mb_y = s2->mb_height;
2375 if (s->slice_count) {
2376 s2->thread_context[s->slice_count-1]->end_mb_y = mb_y;
2377 ret = ff_update_duplicate_context(thread_context,
2382 init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
2385 buf_ptr += 2; // FIXME add minimum number of bytes per slice
2387 ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2391 if (avctx->err_recognition & AV_EF_EXPLODE)
2393 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2394 ff_er_add_slice(&s2->er, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
2396 ff_er_add_slice(&s2->er, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, ER_AC_END | ER_DC_END | ER_MV_END);
2405 static int mpeg_decode_frame(AVCodecContext *avctx,
2406 void *data, int *got_output,
2409 const uint8_t *buf = avpkt->data;
2411 int buf_size = avpkt->size;
2412 Mpeg1Context *s = avctx->priv_data;
2413 AVFrame *picture = data;
2414 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2415 av_dlog(avctx, "fill_buffer\n");
2417 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2418 /* special case for last picture */
2419 if (s2->low_delay == 0 && s2->next_picture_ptr) {
2420 int ret = av_frame_ref(picture, &s2->next_picture_ptr->f);
2424 s2->next_picture_ptr = NULL;
2431 if (s2->flags & CODEC_FLAG_TRUNCATED) {
2432 int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
2434 if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
2438 s2->codec_tag = avpriv_toupper4(avctx->codec_tag);
2439 if (s->mpeg_enc_ctx_allocated == 0 && ( s2->codec_tag == AV_RL32("VCR2")
2440 || s2->codec_tag == AV_RL32("BW10")
2442 vcr2_init_sequence(avctx);
2446 if (avctx->extradata && !s->extradata_decoded) {
2447 ret = decode_chunks(avctx, picture, got_output, avctx->extradata, avctx->extradata_size);
2449 av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
2452 s->extradata_decoded = 1;
2453 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) {
2454 s2->current_picture_ptr = NULL;
2459 ret = decode_chunks(avctx, picture, got_output, buf, buf_size);
2460 if (ret<0 || *got_output)
2461 s2->current_picture_ptr = NULL;
2467 static void flush(AVCodecContext *avctx)
2469 Mpeg1Context *s = avctx->priv_data;
2473 ff_mpeg_flush(avctx);
2476 static av_cold int mpeg_decode_end(AVCodecContext *avctx)
2478 Mpeg1Context *s = avctx->priv_data;
2480 if (s->mpeg_enc_ctx_allocated)
2481 ff_MPV_common_end(&s->mpeg_enc_ctx);
2485 static const AVProfile mpeg2_video_profiles[] = {
2486 { FF_PROFILE_MPEG2_422, "4:2:2" },
2487 { FF_PROFILE_MPEG2_HIGH, "High" },
2488 { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2489 { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2490 { FF_PROFILE_MPEG2_MAIN, "Main" },
2491 { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2492 { FF_PROFILE_RESERVED, "Reserved" },
2493 { FF_PROFILE_RESERVED, "Reserved" },
2494 { FF_PROFILE_UNKNOWN },
2498 AVCodec ff_mpeg1video_decoder = {
2499 .name = "mpeg1video",
2500 .type = AVMEDIA_TYPE_VIDEO,
2501 .id = AV_CODEC_ID_MPEG1VIDEO,
2502 .priv_data_size = sizeof(Mpeg1Context),
2503 .init = mpeg_decode_init,
2504 .close = mpeg_decode_end,
2505 .decode = mpeg_decode_frame,
2506 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2507 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2508 CODEC_CAP_SLICE_THREADS,
2511 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2512 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2515 AVCodec ff_mpeg2video_decoder = {
2516 .name = "mpeg2video",
2517 .type = AVMEDIA_TYPE_VIDEO,
2518 .id = AV_CODEC_ID_MPEG2VIDEO,
2519 .priv_data_size = sizeof(Mpeg1Context),
2520 .init = mpeg_decode_init,
2521 .close = mpeg_decode_end,
2522 .decode = mpeg_decode_frame,
2523 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2524 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2525 CODEC_CAP_SLICE_THREADS,
2528 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2529 .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2533 AVCodec ff_mpegvideo_decoder = {
2534 .name = "mpegvideo",
2535 .type = AVMEDIA_TYPE_VIDEO,
2536 .id = AV_CODEC_ID_MPEG2VIDEO,
2537 .priv_data_size = sizeof(Mpeg1Context),
2538 .init = mpeg_decode_init,
2539 .close = mpeg_decode_end,
2540 .decode = mpeg_decode_frame,
2541 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
2544 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2547 #if CONFIG_MPEG_XVMC_DECODER
2548 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2550 if (avctx->active_thread_type & FF_THREAD_SLICE)
2552 if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2554 if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
2555 av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2557 mpeg_decode_init(avctx);
2559 avctx->pix_fmt = AV_PIX_FMT_XVMC_MPEG2_IDCT;
2560 avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2565 AVCodec ff_mpeg_xvmc_decoder = {
2566 .name = "mpegvideo_xvmc",
2567 .type = AVMEDIA_TYPE_VIDEO,
2568 .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2569 .priv_data_size = sizeof(Mpeg1Context),
2570 .init = mpeg_mc_decode_init,
2571 .close = mpeg_decode_end,
2572 .decode = mpeg_decode_frame,
2573 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2574 CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2576 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2581 #if CONFIG_MPEG_VDPAU_DECODER
2582 AVCodec ff_mpeg_vdpau_decoder = {
2583 .name = "mpegvideo_vdpau",
2584 .type = AVMEDIA_TYPE_VIDEO,
2585 .id = AV_CODEC_ID_MPEG2VIDEO,
2586 .priv_data_size = sizeof(Mpeg1Context),
2587 .init = mpeg_decode_init,
2588 .close = mpeg_decode_end,
2589 .decode = mpeg_decode_frame,
2590 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
2591 CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2593 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
2597 #if CONFIG_MPEG1_VDPAU_DECODER
2598 AVCodec ff_mpeg1_vdpau_decoder = {
2599 .name = "mpeg1video_vdpau",
2600 .type = AVMEDIA_TYPE_VIDEO,
2601 .id = AV_CODEC_ID_MPEG1VIDEO,
2602 .priv_data_size = sizeof(Mpeg1Context),
2603 .init = mpeg_decode_init,
2604 .close = mpeg_decode_end,
2605 .decode = mpeg_decode_frame,
2606 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
2607 CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2609 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),