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
28 #define UNCHECKED_BITSTREAM_READER 1
31 #include "libavutil/attributes.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/stereo3d.h"
36 #include "bytestream.h"
37 #include "error_resilience.h"
42 #include "mpeg12data.h"
43 #include "mpegutils.h"
44 #include "mpegvideo.h"
47 #include "vdpau_internal.h"
48 #include "xvmc_internal.h"
50 typedef struct Mpeg1Context {
51 MpegEncContext mpeg_enc_ctx;
52 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
53 int repeat_field; /* true if we must repeat the field */
54 AVPanScan pan_scan; /* some temporary storage for the panscan */
63 int save_width, save_height, save_progressive_seq;
64 AVRational frame_rate_ext; /* MPEG-2 specific framerate modificator */
65 int sync; /* Did we reach a sync point like a GOP/SEQ/KEYFrame? */
68 int extradata_decoded;
71 #define MB_TYPE_ZERO_MV 0x20000000
73 static const uint32_t ptype2mb_type[7] = {
75 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
77 MB_TYPE_L0 | MB_TYPE_CBP,
78 MB_TYPE_QUANT | MB_TYPE_INTRA,
79 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
80 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
83 static const uint32_t btype2mb_type[11] = {
86 MB_TYPE_L1 | MB_TYPE_CBP,
88 MB_TYPE_L0 | MB_TYPE_CBP,
90 MB_TYPE_L0L1 | MB_TYPE_CBP,
91 MB_TYPE_QUANT | MB_TYPE_INTRA,
92 MB_TYPE_QUANT | MB_TYPE_L1 | MB_TYPE_CBP,
93 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
94 MB_TYPE_QUANT | MB_TYPE_L0L1 | MB_TYPE_CBP,
97 static const uint8_t non_linear_qscale[32] = {
98 0, 1, 2, 3, 4, 5, 6, 7,
99 8, 10, 12, 14, 16, 18, 20, 22,
100 24, 28, 32, 36, 40, 44, 48, 52,
101 56, 64, 72, 80, 88, 96, 104, 112,
104 /* as H.263, but only 17 codes */
105 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
107 int code, sign, val, shift;
109 code = get_vlc2(&s->gb, ff_mv_vlc.table, MV_VLC_BITS, 2);
115 sign = get_bits1(&s->gb);
119 val = (val - 1) << shift;
120 val |= get_bits(&s->gb, shift);
127 /* modulo decoding */
128 return sign_extend(val, 5 + shift);
131 #define check_scantable_index(ctx, x) \
134 av_log(ctx->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
135 ctx->mb_x, ctx->mb_y); \
136 return AVERROR_INVALIDDATA; \
140 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
141 int16_t *block, int n)
143 int level, dc, diff, i, j, run;
145 RLTable *rl = &ff_rl_mpeg1;
146 uint8_t *const scantable = s->intra_scantable.permutated;
147 const uint16_t *quant_matrix = s->intra_matrix;
148 const int qscale = s->qscale;
151 component = (n <= 3 ? 0 : n - 4 + 1);
152 diff = decode_dc(&s->gb, component);
155 dc = s->last_dc[component];
157 s->last_dc[component] = dc;
158 block[0] = dc * quant_matrix[0];
159 av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
162 OPEN_READER(re, &s->gb);
163 UPDATE_CACHE(re, &s->gb);
164 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
167 /* now quantify & encode AC coefficients */
169 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
174 check_scantable_index(s, i);
176 level = (level * qscale * quant_matrix[j]) >> 4;
177 level = (level - 1) | 1;
178 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
179 SHOW_SBITS(re, &s->gb, 1);
180 SKIP_BITS(re, &s->gb, 1);
183 run = SHOW_UBITS(re, &s->gb, 6) + 1;
184 LAST_SKIP_BITS(re, &s->gb, 6);
185 UPDATE_CACHE(re, &s->gb);
186 level = SHOW_SBITS(re, &s->gb, 8);
187 SKIP_BITS(re, &s->gb, 8);
189 level = SHOW_UBITS(re, &s->gb, 8) - 256;
190 SKIP_BITS(re, &s->gb, 8);
191 } else if (level == 0) {
192 level = SHOW_UBITS(re, &s->gb, 8);
193 SKIP_BITS(re, &s->gb, 8);
196 check_scantable_index(s, i);
200 level = (level * qscale * quant_matrix[j]) >> 4;
201 level = (level - 1) | 1;
204 level = (level * qscale * quant_matrix[j]) >> 4;
205 level = (level - 1) | 1;
210 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
213 UPDATE_CACHE(re, &s->gb);
216 LAST_SKIP_BITS(re, &s->gb, 2);
217 CLOSE_READER(re, &s->gb);
219 s->block_last_index[n] = i;
223 int ff_mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
225 return mpeg1_decode_block_intra(s, block, n);
228 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
229 int16_t *block, int n)
231 int level, i, j, run;
232 RLTable *rl = &ff_rl_mpeg1;
233 uint8_t *const scantable = s->intra_scantable.permutated;
234 const uint16_t *quant_matrix = s->inter_matrix;
235 const int qscale = s->qscale;
238 OPEN_READER(re, &s->gb);
240 // special case for first coefficient, no need to add second VLC table
241 UPDATE_CACHE(re, &s->gb);
242 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
243 level = (3 * qscale * quant_matrix[0]) >> 5;
244 level = (level - 1) | 1;
245 if (GET_CACHE(re, &s->gb) & 0x40000000)
249 SKIP_BITS(re, &s->gb, 2);
250 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
253 /* now quantify & encode AC coefficients */
255 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
260 check_scantable_index(s, i);
262 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
263 level = (level - 1) | 1;
264 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
265 SHOW_SBITS(re, &s->gb, 1);
266 SKIP_BITS(re, &s->gb, 1);
269 run = SHOW_UBITS(re, &s->gb, 6) + 1;
270 LAST_SKIP_BITS(re, &s->gb, 6);
271 UPDATE_CACHE(re, &s->gb);
272 level = SHOW_SBITS(re, &s->gb, 8);
273 SKIP_BITS(re, &s->gb, 8);
275 level = SHOW_UBITS(re, &s->gb, 8) - 256;
276 SKIP_BITS(re, &s->gb, 8);
277 } else if (level == 0) {
278 level = SHOW_UBITS(re, &s->gb, 8);
279 SKIP_BITS(re, &s->gb, 8);
282 check_scantable_index(s, i);
286 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
287 level = (level - 1) | 1;
290 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
291 level = (level - 1) | 1;
296 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
298 UPDATE_CACHE(re, &s->gb);
301 LAST_SKIP_BITS(re, &s->gb, 2);
302 CLOSE_READER(re, &s->gb);
304 s->block_last_index[n] = i;
309 * Note: this function can read out of range and crash for corrupt streams.
310 * Changing this would eat up any speed benefits it has.
311 * Do not use "fast" flag if you need the code to be robust.
313 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s,
314 int16_t *block, int n)
316 int level, i, j, run;
317 RLTable *rl = &ff_rl_mpeg1;
318 uint8_t *const scantable = s->intra_scantable.permutated;
319 const int qscale = s->qscale;
322 OPEN_READER(re, &s->gb);
324 // Special case for first coefficient, no need to add second VLC table.
325 UPDATE_CACHE(re, &s->gb);
326 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
327 level = (3 * qscale) >> 1;
328 level = (level - 1) | 1;
329 if (GET_CACHE(re, &s->gb) & 0x40000000)
333 SKIP_BITS(re, &s->gb, 2);
334 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
338 /* now quantify & encode AC coefficients */
340 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
345 check_scantable_index(s, i);
347 level = ((level * 2 + 1) * qscale) >> 1;
348 level = (level - 1) | 1;
349 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
350 SHOW_SBITS(re, &s->gb, 1);
351 SKIP_BITS(re, &s->gb, 1);
354 run = SHOW_UBITS(re, &s->gb, 6) + 1;
355 LAST_SKIP_BITS(re, &s->gb, 6);
356 UPDATE_CACHE(re, &s->gb);
357 level = SHOW_SBITS(re, &s->gb, 8);
358 SKIP_BITS(re, &s->gb, 8);
360 level = SHOW_UBITS(re, &s->gb, 8) - 256;
361 SKIP_BITS(re, &s->gb, 8);
362 } else if (level == 0) {
363 level = SHOW_UBITS(re, &s->gb, 8);
364 SKIP_BITS(re, &s->gb, 8);
367 check_scantable_index(s, i);
371 level = ((level * 2 + 1) * qscale) >> 1;
372 level = (level - 1) | 1;
375 level = ((level * 2 + 1) * qscale) >> 1;
376 level = (level - 1) | 1;
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 s->block_last_index[n] = i;
393 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
394 int16_t *block, int n)
396 int level, i, j, run;
397 RLTable *rl = &ff_rl_mpeg1;
398 uint8_t *const scantable = s->intra_scantable.permutated;
399 const uint16_t *quant_matrix;
400 const int qscale = s->qscale;
406 OPEN_READER(re, &s->gb);
409 quant_matrix = s->inter_matrix;
411 quant_matrix = s->chroma_inter_matrix;
413 // Special case for first coefficient, no need to add second VLC table.
414 UPDATE_CACHE(re, &s->gb);
415 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
416 level = (3 * qscale * quant_matrix[0]) >> 5;
417 if (GET_CACHE(re, &s->gb) & 0x40000000)
422 SKIP_BITS(re, &s->gb, 2);
423 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
427 /* now quantify & encode AC coefficients */
429 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
434 check_scantable_index(s, i);
436 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
437 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
438 SHOW_SBITS(re, &s->gb, 1);
439 SKIP_BITS(re, &s->gb, 1);
442 run = SHOW_UBITS(re, &s->gb, 6) + 1;
443 LAST_SKIP_BITS(re, &s->gb, 6);
444 UPDATE_CACHE(re, &s->gb);
445 level = SHOW_SBITS(re, &s->gb, 12);
446 SKIP_BITS(re, &s->gb, 12);
449 check_scantable_index(s, i);
452 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
455 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
461 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
463 UPDATE_CACHE(re, &s->gb);
466 LAST_SKIP_BITS(re, &s->gb, 2);
467 CLOSE_READER(re, &s->gb);
469 block[63] ^= (mismatch & 1);
471 s->block_last_index[n] = i;
476 * Note: this function can read out of range and crash for corrupt streams.
477 * Changing this would eat up any speed benefits it has.
478 * Do not use "fast" flag if you need the code to be robust.
480 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
481 int16_t *block, int n)
483 int level, i, j, run;
484 RLTable *rl = &ff_rl_mpeg1;
485 uint8_t *const scantable = s->intra_scantable.permutated;
486 const int qscale = s->qscale;
487 OPEN_READER(re, &s->gb);
490 // special case for first coefficient, no need to add second VLC table
491 UPDATE_CACHE(re, &s->gb);
492 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
493 level = (3 * qscale) >> 1;
494 if (GET_CACHE(re, &s->gb) & 0x40000000)
498 SKIP_BITS(re, &s->gb, 2);
499 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
503 /* now quantify & encode AC coefficients */
505 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
510 level = ((level * 2 + 1) * qscale) >> 1;
511 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
512 SHOW_SBITS(re, &s->gb, 1);
513 SKIP_BITS(re, &s->gb, 1);
516 run = SHOW_UBITS(re, &s->gb, 6) + 1;
517 LAST_SKIP_BITS(re, &s->gb, 6);
518 UPDATE_CACHE(re, &s->gb);
519 level = SHOW_SBITS(re, &s->gb, 12);
520 SKIP_BITS(re, &s->gb, 12);
525 level = ((-level * 2 + 1) * qscale) >> 1;
528 level = ((level * 2 + 1) * qscale) >> 1;
533 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF || i > 63)
536 UPDATE_CACHE(re, &s->gb);
539 LAST_SKIP_BITS(re, &s->gb, 2);
540 CLOSE_READER(re, &s->gb);
541 s->block_last_index[n] = i;
545 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
546 int16_t *block, int n)
548 int level, dc, diff, i, j, run;
551 uint8_t *const scantable = s->intra_scantable.permutated;
552 const uint16_t *quant_matrix;
553 const int qscale = s->qscale;
558 quant_matrix = s->intra_matrix;
561 quant_matrix = s->chroma_intra_matrix;
562 component = (n & 1) + 1;
564 diff = decode_dc(&s->gb, component);
567 dc = s->last_dc[component];
569 s->last_dc[component] = dc;
570 block[0] = dc << (3 - s->intra_dc_precision);
571 av_dlog(s->avctx, "dc=%d\n", block[0]);
572 mismatch = block[0] ^ 1;
574 if (s->intra_vlc_format)
580 OPEN_READER(re, &s->gb);
581 /* now quantify & encode AC coefficients */
583 UPDATE_CACHE(re, &s->gb);
584 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
589 } else if (level != 0) {
591 check_scantable_index(s, i);
593 level = (level * qscale * quant_matrix[j]) >> 4;
594 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
595 SHOW_SBITS(re, &s->gb, 1);
596 LAST_SKIP_BITS(re, &s->gb, 1);
599 run = SHOW_UBITS(re, &s->gb, 6) + 1;
600 LAST_SKIP_BITS(re, &s->gb, 6);
601 UPDATE_CACHE(re, &s->gb);
602 level = SHOW_SBITS(re, &s->gb, 12);
603 SKIP_BITS(re, &s->gb, 12);
605 check_scantable_index(s, i);
608 level = (-level * qscale * quant_matrix[j]) >> 4;
611 level = (level * qscale * quant_matrix[j]) >> 4;
618 CLOSE_READER(re, &s->gb);
620 block[63] ^= mismatch & 1;
622 s->block_last_index[n] = i;
627 * Note: this function can read out of range and crash for corrupt streams.
628 * Changing this would eat up any speed benefits it has.
629 * Do not use "fast" flag if you need the code to be robust.
631 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
632 int16_t *block, int n)
634 int level, dc, diff, i, j, run;
637 uint8_t *const scantable = s->intra_scantable.permutated;
638 const uint16_t *quant_matrix;
639 const int qscale = s->qscale;
643 quant_matrix = s->intra_matrix;
646 quant_matrix = s->chroma_intra_matrix;
647 component = (n & 1) + 1;
649 diff = decode_dc(&s->gb, component);
652 dc = s->last_dc[component];
654 s->last_dc[component] = dc;
655 block[0] = dc << (3 - s->intra_dc_precision);
657 if (s->intra_vlc_format)
663 OPEN_READER(re, &s->gb);
664 /* now quantify & encode AC coefficients */
666 UPDATE_CACHE(re, &s->gb);
667 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
670 if (level >= 64 || i > 63) {
672 } else if (level != 0) {
675 level = (level * qscale * quant_matrix[j]) >> 4;
676 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
677 SHOW_SBITS(re, &s->gb, 1);
678 LAST_SKIP_BITS(re, &s->gb, 1);
681 run = SHOW_UBITS(re, &s->gb, 6) + 1;
682 LAST_SKIP_BITS(re, &s->gb, 6);
683 UPDATE_CACHE(re, &s->gb);
684 level = SHOW_SBITS(re, &s->gb, 12);
685 SKIP_BITS(re, &s->gb, 12);
689 level = (-level * qscale * quant_matrix[j]) >> 4;
692 level = (level * qscale * quant_matrix[j]) >> 4;
698 CLOSE_READER(re, &s->gb);
701 s->block_last_index[n] = i;
705 /******************************************/
708 static inline int get_dmv(MpegEncContext *s)
710 if (get_bits1(&s->gb))
711 return 1 - (get_bits1(&s->gb) << 1);
716 static inline int get_qscale(MpegEncContext *s)
718 int qscale = get_bits(&s->gb, 5);
720 return non_linear_qscale[qscale];
726 /* motion type (for MPEG-2) */
732 static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
734 int i, j, k, cbp, val, mb_type, motion_type;
735 const int mb_block_count = 4 + (1 << s->chroma_format);
737 av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
739 av_assert2(s->mb_skipped == 0);
741 if (s->mb_skip_run-- != 0) {
742 if (s->pict_type == AV_PICTURE_TYPE_P) {
744 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
745 MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
750 mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
752 // FIXME not sure if this is allowed in MPEG at all
753 mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
754 if (IS_INTRA(mb_type)) {
755 av_log(s->avctx, AV_LOG_ERROR, "skip with previntra\n");
758 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
759 mb_type | MB_TYPE_SKIP;
761 if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
768 switch (s->pict_type) {
770 case AV_PICTURE_TYPE_I:
771 if (get_bits1(&s->gb) == 0) {
772 if (get_bits1(&s->gb) == 0) {
773 av_log(s->avctx, AV_LOG_ERROR,
774 "invalid mb type in I Frame at %d %d\n",
778 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
780 mb_type = MB_TYPE_INTRA;
783 case AV_PICTURE_TYPE_P:
784 mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
786 av_log(s->avctx, AV_LOG_ERROR,
787 "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
790 mb_type = ptype2mb_type[mb_type];
792 case AV_PICTURE_TYPE_B:
793 mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
795 av_log(s->avctx, AV_LOG_ERROR,
796 "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
799 mb_type = btype2mb_type[mb_type];
802 av_dlog(s->avctx, "mb_type=%x\n", mb_type);
803 // motion_type = 0; /* avoid warning */
804 if (IS_INTRA(mb_type)) {
805 s->bdsp.clear_blocks(s->block[0]);
807 if (!s->chroma_y_shift)
808 s->bdsp.clear_blocks(s->block[6]);
810 /* compute DCT type */
811 // FIXME: add an interlaced_dct coded var?
812 if (s->picture_structure == PICT_FRAME &&
813 !s->frame_pred_frame_dct)
814 s->interlaced_dct = get_bits1(&s->gb);
816 if (IS_QUANT(mb_type))
817 s->qscale = get_qscale(s);
819 if (s->concealment_motion_vectors) {
820 /* just parse them */
821 if (s->picture_structure != PICT_FRAME)
822 skip_bits1(&s->gb); /* field select */
825 s->last_mv[0][0][0] =
826 s->last_mv[0][1][0] = mpeg_decode_motion(s, s->mpeg_f_code[0][0],
827 s->last_mv[0][0][0]);
829 s->last_mv[0][0][1] =
830 s->last_mv[0][1][1] = mpeg_decode_motion(s, s->mpeg_f_code[0][1],
831 s->last_mv[0][0][1]);
833 skip_bits1(&s->gb); /* marker */
835 /* reset mv prediction */
836 memset(s->last_mv, 0, sizeof(s->last_mv));
839 // if 1, we memcpy blocks in xvmcvideo
840 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
841 ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
843 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
844 if (s->flags2 & CODEC_FLAG2_FAST) {
845 for (i = 0; i < 6; i++)
846 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
848 for (i = 0; i < mb_block_count; i++)
849 if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
853 for (i = 0; i < 6; i++)
854 if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
858 if (mb_type & MB_TYPE_ZERO_MV) {
859 av_assert2(mb_type & MB_TYPE_CBP);
861 s->mv_dir = MV_DIR_FORWARD;
862 if (s->picture_structure == PICT_FRAME) {
863 if (s->picture_structure == PICT_FRAME
864 && !s->frame_pred_frame_dct)
865 s->interlaced_dct = get_bits1(&s->gb);
866 s->mv_type = MV_TYPE_16X16;
868 s->mv_type = MV_TYPE_FIELD;
869 mb_type |= MB_TYPE_INTERLACED;
870 s->field_select[0][0] = s->picture_structure - 1;
873 if (IS_QUANT(mb_type))
874 s->qscale = get_qscale(s);
876 s->last_mv[0][0][0] = 0;
877 s->last_mv[0][0][1] = 0;
878 s->last_mv[0][1][0] = 0;
879 s->last_mv[0][1][1] = 0;
883 av_assert2(mb_type & MB_TYPE_L0L1);
884 // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
885 /* get additional motion vector type */
886 if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct) {
887 motion_type = MT_FRAME;
889 motion_type = get_bits(&s->gb, 2);
890 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
891 s->interlaced_dct = get_bits1(&s->gb);
894 if (IS_QUANT(mb_type))
895 s->qscale = get_qscale(s);
898 s->mv_dir = (mb_type >> 13) & 3;
899 av_dlog(s->avctx, "motion_type=%d\n", motion_type);
900 switch (motion_type) {
901 case MT_FRAME: /* or MT_16X8 */
902 if (s->picture_structure == PICT_FRAME) {
903 mb_type |= MB_TYPE_16x16;
904 s->mv_type = MV_TYPE_16X16;
905 for (i = 0; i < 2; i++) {
906 if (USES_LIST(mb_type, i)) {
909 s->last_mv[i][0][0] =
910 s->last_mv[i][1][0] =
911 mpeg_decode_motion(s, s->mpeg_f_code[i][0],
912 s->last_mv[i][0][0]);
914 s->last_mv[i][0][1] =
915 s->last_mv[i][1][1] =
916 mpeg_decode_motion(s, s->mpeg_f_code[i][1],
917 s->last_mv[i][0][1]);
918 /* full_pel: only for MPEG-1 */
919 if (s->full_pel[i]) {
920 s->mv[i][0][0] <<= 1;
921 s->mv[i][0][1] <<= 1;
926 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
927 s->mv_type = MV_TYPE_16X8;
928 for (i = 0; i < 2; i++) {
929 if (USES_LIST(mb_type, i)) {
931 for (j = 0; j < 2; j++) {
932 s->field_select[i][j] = get_bits1(&s->gb);
933 for (k = 0; k < 2; k++) {
934 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
935 s->last_mv[i][j][k]);
936 s->last_mv[i][j][k] = val;
937 s->mv[i][j][k] = val;
945 s->mv_type = MV_TYPE_FIELD;
946 if (s->picture_structure == PICT_FRAME) {
947 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
948 for (i = 0; i < 2; i++) {
949 if (USES_LIST(mb_type, i)) {
950 for (j = 0; j < 2; j++) {
951 s->field_select[i][j] = get_bits1(&s->gb);
952 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
953 s->last_mv[i][j][0]);
954 s->last_mv[i][j][0] = val;
955 s->mv[i][j][0] = val;
956 av_dlog(s->avctx, "fmx=%d\n", val);
957 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
958 s->last_mv[i][j][1] >> 1);
959 s->last_mv[i][j][1] = val << 1;
960 s->mv[i][j][1] = val;
961 av_dlog(s->avctx, "fmy=%d\n", val);
966 av_assert0(!s->progressive_sequence);
967 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
968 for (i = 0; i < 2; i++) {
969 if (USES_LIST(mb_type, i)) {
970 s->field_select[i][0] = get_bits1(&s->gb);
971 for (k = 0; k < 2; k++) {
972 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
973 s->last_mv[i][0][k]);
974 s->last_mv[i][0][k] = val;
975 s->last_mv[i][1][k] = val;
976 s->mv[i][0][k] = val;
983 if (s->progressive_sequence){
984 av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
987 s->mv_type = MV_TYPE_DMV;
988 for (i = 0; i < 2; i++) {
989 if (USES_LIST(mb_type, i)) {
990 int dmx, dmy, mx, my, m;
991 const int my_shift = s->picture_structure == PICT_FRAME;
993 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
994 s->last_mv[i][0][0]);
995 s->last_mv[i][0][0] = mx;
996 s->last_mv[i][1][0] = mx;
998 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
999 s->last_mv[i][0][1] >> my_shift);
1003 s->last_mv[i][0][1] = my << my_shift;
1004 s->last_mv[i][1][1] = my << my_shift;
1006 s->mv[i][0][0] = mx;
1007 s->mv[i][0][1] = my;
1008 s->mv[i][1][0] = mx; // not used
1009 s->mv[i][1][1] = my; // not used
1011 if (s->picture_structure == PICT_FRAME) {
1012 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
1014 // m = 1 + 2 * s->top_field_first;
1015 m = s->top_field_first ? 1 : 3;
1017 /* top -> top pred */
1018 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1019 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1021 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1022 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1024 mb_type |= MB_TYPE_16x16;
1026 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1027 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1028 if (s->picture_structure == PICT_TOP_FIELD)
1037 av_log(s->avctx, AV_LOG_ERROR,
1038 "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1044 if (HAS_CBP(mb_type)) {
1045 s->bdsp.clear_blocks(s->block[0]);
1047 cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1048 if (mb_block_count > 6) {
1049 cbp <<= mb_block_count - 6;
1050 cbp |= get_bits(&s->gb, mb_block_count - 6);
1051 s->bdsp.clear_blocks(s->block[6]);
1054 av_log(s->avctx, AV_LOG_ERROR,
1055 "invalid cbp %d at %d %d\n", cbp, s->mb_x, s->mb_y);
1059 // if 1, we memcpy blocks in xvmcvideo
1060 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
1061 ff_xvmc_pack_pblocks(s, cbp);
1063 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1064 if (s->flags2 & CODEC_FLAG2_FAST) {
1065 for (i = 0; i < 6; i++) {
1067 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
1069 s->block_last_index[i] = -1;
1073 cbp <<= 12 - mb_block_count;
1075 for (i = 0; i < mb_block_count; i++) {
1076 if (cbp & (1 << 11)) {
1077 if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
1080 s->block_last_index[i] = -1;
1086 if (s->flags2 & CODEC_FLAG2_FAST) {
1087 for (i = 0; i < 6; i++) {
1089 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
1091 s->block_last_index[i] = -1;
1095 for (i = 0; i < 6; i++) {
1097 if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
1100 s->block_last_index[i] = -1;
1107 for (i = 0; i < 12; i++)
1108 s->block_last_index[i] = -1;
1112 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
1117 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1119 Mpeg1Context *s = avctx->priv_data;
1120 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1122 ff_mpv_decode_defaults(s2);
1123 ff_mpv_decode_init(s2, avctx);
1125 s->mpeg_enc_ctx.avctx = avctx;
1127 /* we need some permutation to store matrices,
1128 * until the decoder sets the real permutation. */
1129 ff_mpv_idct_init(s2);
1130 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1131 ff_mpeg12_init_vlcs();
1133 s->mpeg_enc_ctx_allocated = 0;
1134 s->mpeg_enc_ctx.picture_number = 0;
1135 s->repeat_field = 0;
1136 s->mpeg_enc_ctx.codec_id = avctx->codec->id;
1137 avctx->color_range = AVCOL_RANGE_MPEG;
1138 if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
1139 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1141 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1145 static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
1146 const AVCodecContext *avctx_from)
1148 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1149 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1152 if (avctx == avctx_from ||
1153 !ctx_from->mpeg_enc_ctx_allocated ||
1154 !s1->context_initialized)
1157 err = ff_mpeg_update_thread_context(avctx, avctx_from);
1161 if (!ctx->mpeg_enc_ctx_allocated)
1162 memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
1164 if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
1165 s->picture_number++;
1170 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1171 const uint8_t *new_perm)
1173 uint16_t temp_matrix[64];
1176 memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
1178 for (i = 0; i < 64; i++)
1179 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1182 static const enum AVPixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
1183 #if CONFIG_MPEG1_XVMC_HWACCEL
1186 #if CONFIG_MPEG1_VDPAU_HWACCEL
1187 AV_PIX_FMT_VDPAU_MPEG1,
1194 static const enum AVPixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
1195 #if CONFIG_MPEG2_XVMC_HWACCEL
1198 #if CONFIG_MPEG2_VDPAU_HWACCEL
1199 AV_PIX_FMT_VDPAU_MPEG2,
1202 #if CONFIG_MPEG2_DXVA2_HWACCEL
1203 AV_PIX_FMT_DXVA2_VLD,
1205 #if CONFIG_MPEG2_VAAPI_HWACCEL
1206 AV_PIX_FMT_VAAPI_VLD,
1212 static const enum AVPixelFormat mpeg12_pixfmt_list_422[] = {
1217 static const enum AVPixelFormat mpeg12_pixfmt_list_444[] = {
1222 static inline int uses_vdpau(AVCodecContext *avctx) {
1223 return avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG2;
1226 static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
1228 Mpeg1Context *s1 = avctx->priv_data;
1229 MpegEncContext *s = &s1->mpeg_enc_ctx;
1230 const enum AVPixelFormat *pix_fmts;
1232 if (s->chroma_format < 2)
1233 pix_fmts = avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO ?
1234 mpeg1_hwaccel_pixfmt_list_420 :
1235 mpeg2_hwaccel_pixfmt_list_420;
1236 else if (s->chroma_format == 2)
1237 pix_fmts = mpeg12_pixfmt_list_422;
1239 pix_fmts = mpeg12_pixfmt_list_444;
1241 return ff_thread_get_format(avctx, pix_fmts);
1244 static void setup_hwaccel_for_pixfmt(AVCodecContext *avctx)
1246 // until then pix_fmt may be changed right after codec init
1247 if (avctx->hwaccel || uses_vdpau(avctx))
1248 if (avctx->idct_algo == FF_IDCT_AUTO)
1249 avctx->idct_algo = FF_IDCT_SIMPLE;
1251 if (avctx->hwaccel && avctx->pix_fmt == AV_PIX_FMT_XVMC) {
1252 Mpeg1Context *s1 = avctx->priv_data;
1253 MpegEncContext *s = &s1->mpeg_enc_ctx;
1255 s->pack_pblocks = 1;
1257 avctx->xvmc_acceleration = 2;
1258 #endif /* FF_API_XVMC */
1262 /* Call this function when we know all parameters.
1263 * It may be called in different places for MPEG-1 and MPEG-2. */
1264 static int mpeg_decode_postinit(AVCodecContext *avctx)
1266 Mpeg1Context *s1 = avctx->priv_data;
1267 MpegEncContext *s = &s1->mpeg_enc_ctx;
1268 uint8_t old_permutation[64];
1271 if ((s1->mpeg_enc_ctx_allocated == 0) ||
1272 avctx->coded_width != s->width ||
1273 avctx->coded_height != s->height ||
1274 s1->save_width != s->width ||
1275 s1->save_height != s->height ||
1276 s1->save_aspect_info != s->aspect_ratio_info ||
1277 (s1->save_progressive_seq != s->progressive_sequence && FFALIGN(s->height, 16) != FFALIGN(s->height, 32)) ||
1279 if (s1->mpeg_enc_ctx_allocated) {
1280 ParseContext pc = s->parse_context;
1281 s->parse_context.buffer = 0;
1282 ff_mpv_common_end(s);
1283 s->parse_context = pc;
1284 s1->mpeg_enc_ctx_allocated = 0;
1287 if ((s->width == 0) || (s->height == 0))
1290 ret = ff_set_dimensions(avctx, s->width, s->height);
1294 if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->bit_rate) {
1295 avctx->rc_max_rate = s->bit_rate;
1296 } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && s->bit_rate &&
1297 (s->bit_rate != 0x3FFFF*400 || s->vbv_delay != 0xFFFF)) {
1298 avctx->bit_rate = s->bit_rate;
1300 s1->save_aspect_info = s->aspect_ratio_info;
1301 s1->save_width = s->width;
1302 s1->save_height = s->height;
1303 s1->save_progressive_seq = s->progressive_sequence;
1305 /* low_delay may be forced, in this case we will have B-frames
1306 * that behave like P-frames. */
1307 avctx->has_b_frames = !s->low_delay;
1309 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1311 avctx->framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
1313 avctx->sample_aspect_ratio = av_d2q(1.0 / ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1314 avctx->ticks_per_frame = 1;
1317 av_reduce(&s->avctx->framerate.num,
1318 &s->avctx->framerate.den,
1319 ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num,
1320 ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1322 avctx->ticks_per_frame = 2;
1324 if (s->aspect_ratio_info > 1) {
1326 av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1327 (AVRational) { s1->pan_scan.width,
1328 s1->pan_scan.height }),
1329 (AVRational) { s->width, s->height });
1331 /* We ignore the spec here and guess a bit as reality does not
1332 * match the spec, see for example res_change_ffmpeg_aspect.ts
1333 * and sequence-display-aspect.mpg.
1334 * issue1613, 621, 562 */
1335 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1336 (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
1337 av_cmp_q(dar, (AVRational) { 16, 9 }))) {
1338 s->avctx->sample_aspect_ratio =
1339 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1340 (AVRational) { s->width, s->height });
1342 s->avctx->sample_aspect_ratio =
1343 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1344 (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
1345 // issue1613 4/3 16/9 -> 16/9
1346 // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1347 // widescreen-issue562.mpg 4/3 16/9 -> 16/9
1348 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1349 av_dlog(avctx, "A %d/%d\n",
1350 ff_mpeg2_aspect[s->aspect_ratio_info].num,
1351 ff_mpeg2_aspect[s->aspect_ratio_info].den);
1352 av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
1353 s->avctx->sample_aspect_ratio.den);
1356 s->avctx->sample_aspect_ratio =
1357 ff_mpeg2_aspect[s->aspect_ratio_info];
1361 ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
1363 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1364 setup_hwaccel_for_pixfmt(avctx);
1366 /* Quantization matrices may need reordering
1367 * if DCT permutation is changed. */
1368 memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
1370 ff_mpv_idct_init(s);
1371 if (ff_mpv_common_init(s) < 0)
1374 quant_matrix_rebuild(s->intra_matrix, old_permutation, s->idsp.idct_permutation);
1375 quant_matrix_rebuild(s->inter_matrix, old_permutation, s->idsp.idct_permutation);
1376 quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
1377 quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
1379 s1->mpeg_enc_ctx_allocated = 1;
1384 static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
1387 Mpeg1Context *s1 = avctx->priv_data;
1388 MpegEncContext *s = &s1->mpeg_enc_ctx;
1389 int ref, f_code, vbv_delay;
1391 init_get_bits(&s->gb, buf, buf_size * 8);
1393 ref = get_bits(&s->gb, 10); /* temporal ref */
1394 s->pict_type = get_bits(&s->gb, 3);
1395 if (s->pict_type == 0 || s->pict_type > 3)
1398 vbv_delay = get_bits(&s->gb, 16);
1399 s->vbv_delay = vbv_delay;
1400 if (s->pict_type == AV_PICTURE_TYPE_P ||
1401 s->pict_type == AV_PICTURE_TYPE_B) {
1402 s->full_pel[0] = get_bits1(&s->gb);
1403 f_code = get_bits(&s->gb, 3);
1404 if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1407 s->mpeg_f_code[0][0] = f_code;
1408 s->mpeg_f_code[0][1] = f_code;
1410 if (s->pict_type == AV_PICTURE_TYPE_B) {
1411 s->full_pel[1] = get_bits1(&s->gb);
1412 f_code = get_bits(&s->gb, 3);
1413 if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1416 s->mpeg_f_code[1][0] = f_code;
1417 s->mpeg_f_code[1][1] = f_code;
1419 s->current_picture.f->pict_type = s->pict_type;
1420 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1422 if (avctx->debug & FF_DEBUG_PICT_INFO)
1423 av_log(avctx, AV_LOG_DEBUG,
1424 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1431 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1433 MpegEncContext *s = &s1->mpeg_enc_ctx;
1434 int horiz_size_ext, vert_size_ext;
1437 skip_bits(&s->gb, 1); /* profile and level esc*/
1438 s->avctx->profile = get_bits(&s->gb, 3);
1439 s->avctx->level = get_bits(&s->gb, 4);
1440 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1441 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1442 horiz_size_ext = get_bits(&s->gb, 2);
1443 vert_size_ext = get_bits(&s->gb, 2);
1444 s->width |= (horiz_size_ext << 12);
1445 s->height |= (vert_size_ext << 12);
1446 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1447 s->bit_rate += (bit_rate_ext << 18) * 400;
1448 skip_bits1(&s->gb); /* marker */
1449 s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1451 s->low_delay = get_bits1(&s->gb);
1452 if (s->flags & CODEC_FLAG_LOW_DELAY)
1455 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1456 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1458 av_dlog(s->avctx, "sequence extension\n");
1459 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1461 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1462 av_log(s->avctx, AV_LOG_DEBUG,
1463 "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%d\n",
1464 s->avctx->profile, s->avctx->level, s->progressive_sequence, s->chroma_format,
1465 s->avctx->rc_buffer_size, s->bit_rate);
1468 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1470 MpegEncContext *s = &s1->mpeg_enc_ctx;
1471 int color_description, w, h;
1473 skip_bits(&s->gb, 3); /* video format */
1474 color_description = get_bits1(&s->gb);
1475 if (color_description) {
1476 s->avctx->color_primaries = get_bits(&s->gb, 8);
1477 s->avctx->color_trc = get_bits(&s->gb, 8);
1478 s->avctx->colorspace = get_bits(&s->gb, 8);
1480 w = get_bits(&s->gb, 14);
1481 skip_bits(&s->gb, 1); // marker
1482 h = get_bits(&s->gb, 14);
1483 // remaining 3 bits are zero padding
1485 s1->pan_scan.width = 16 * w;
1486 s1->pan_scan.height = 16 * h;
1488 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1489 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1492 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1494 MpegEncContext *s = &s1->mpeg_enc_ctx;
1498 if (s->progressive_sequence) {
1499 if (s->repeat_first_field) {
1501 if (s->top_field_first)
1505 if (s->picture_structure == PICT_FRAME) {
1507 if (s->repeat_first_field)
1511 for (i = 0; i < nofco; i++) {
1512 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1513 skip_bits(&s->gb, 1); // marker
1514 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1515 skip_bits(&s->gb, 1); // marker
1518 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1519 av_log(s->avctx, AV_LOG_DEBUG,
1520 "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
1521 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1522 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1523 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1526 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
1527 uint16_t matrix1[64], int intra)
1531 for (i = 0; i < 64; i++) {
1532 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1533 int v = get_bits(&s->gb, 8);
1535 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1538 if (intra && i == 0 && v != 8) {
1539 av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v);
1540 v = 8; // needed by pink.mpg / issue1046
1549 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1551 av_dlog(s->avctx, "matrix extension\n");
1553 if (get_bits1(&s->gb))
1554 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1555 if (get_bits1(&s->gb))
1556 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1557 if (get_bits1(&s->gb))
1558 load_matrix(s, s->chroma_intra_matrix, NULL, 1);
1559 if (get_bits1(&s->gb))
1560 load_matrix(s, s->chroma_inter_matrix, NULL, 0);
1563 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1565 MpegEncContext *s = &s1->mpeg_enc_ctx;
1567 s->full_pel[0] = s->full_pel[1] = 0;
1568 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1569 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1570 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1571 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1572 if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1573 av_log(s->avctx, AV_LOG_ERROR,
1574 "Missing picture start code, guessing missing values\n");
1575 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1576 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1577 s->pict_type = AV_PICTURE_TYPE_I;
1579 s->pict_type = AV_PICTURE_TYPE_P;
1581 s->pict_type = AV_PICTURE_TYPE_B;
1582 s->current_picture.f->pict_type = s->pict_type;
1583 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1585 s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0];
1586 s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1];
1587 s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0];
1588 s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1];
1590 s->intra_dc_precision = get_bits(&s->gb, 2);
1591 s->picture_structure = get_bits(&s->gb, 2);
1592 s->top_field_first = get_bits1(&s->gb);
1593 s->frame_pred_frame_dct = get_bits1(&s->gb);
1594 s->concealment_motion_vectors = get_bits1(&s->gb);
1595 s->q_scale_type = get_bits1(&s->gb);
1596 s->intra_vlc_format = get_bits1(&s->gb);
1597 s->alternate_scan = get_bits1(&s->gb);
1598 s->repeat_first_field = get_bits1(&s->gb);
1599 s->chroma_420_type = get_bits1(&s->gb);
1600 s->progressive_frame = get_bits1(&s->gb);
1602 if (s->alternate_scan) {
1603 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
1604 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
1606 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
1607 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
1610 /* composite display not parsed */
1611 av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1612 av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1613 av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1614 av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1615 av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1616 av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1617 av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1618 av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1619 av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1622 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1624 AVCodecContext *avctx = s->avctx;
1625 Mpeg1Context *s1 = (Mpeg1Context *) s;
1627 /* start frame decoding */
1628 if (s->first_field || s->picture_structure == PICT_FRAME) {
1629 AVFrameSideData *pan_scan;
1631 if (ff_mpv_frame_start(s, avctx) < 0)
1634 ff_mpeg_er_frame_start(s);
1636 /* first check if we must repeat the frame */
1637 s->current_picture_ptr->f->repeat_pict = 0;
1638 if (s->repeat_first_field) {
1639 if (s->progressive_sequence) {
1640 if (s->top_field_first)
1641 s->current_picture_ptr->f->repeat_pict = 4;
1643 s->current_picture_ptr->f->repeat_pict = 2;
1644 } else if (s->progressive_frame) {
1645 s->current_picture_ptr->f->repeat_pict = 1;
1649 pan_scan = av_frame_new_side_data(s->current_picture_ptr->f,
1650 AV_FRAME_DATA_PANSCAN,
1651 sizeof(s1->pan_scan));
1653 return AVERROR(ENOMEM);
1654 memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
1656 if (s1->a53_caption) {
1657 AVFrameSideData *sd = av_frame_new_side_data(
1658 s->current_picture_ptr->f, AV_FRAME_DATA_A53_CC,
1659 s1->a53_caption_size);
1661 memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
1662 av_freep(&s1->a53_caption);
1665 if (s1->has_stereo3d) {
1666 AVStereo3D *stereo = av_stereo3d_create_side_data(s->current_picture_ptr->f);
1668 return AVERROR(ENOMEM);
1670 *stereo = s1->stereo3d;
1671 s1->has_stereo3d = 0;
1675 AVFrameSideData *sd =
1676 av_frame_new_side_data(s->current_picture_ptr->f,
1677 AV_FRAME_DATA_AFD, 1);
1679 return AVERROR(ENOMEM);
1681 *sd->data = s1->afd;
1685 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1686 ff_thread_finish_setup(avctx);
1687 } else { // second field
1690 if (!s->current_picture_ptr) {
1691 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1695 if (s->avctx->hwaccel &&
1696 (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
1697 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1698 av_log(avctx, AV_LOG_ERROR,
1699 "hardware accelerator failed to decode first field\n");
1702 for (i = 0; i < 4; i++) {
1703 s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
1704 if (s->picture_structure == PICT_BOTTOM_FIELD)
1705 s->current_picture.f->data[i] +=
1706 s->current_picture_ptr->f->linesize[i];
1710 if (avctx->hwaccel) {
1711 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
1718 #define DECODE_SLICE_ERROR -1
1719 #define DECODE_SLICE_OK 0
1723 * MpegEncContext.mb_y must be set to the MB row from the startcode.
1724 * @return DECODE_SLICE_ERROR if the slice is damaged,
1725 * DECODE_SLICE_OK if this slice is OK
1727 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1728 const uint8_t **buf, int buf_size)
1730 AVCodecContext *avctx = s->avctx;
1731 const int lowres = s->avctx->lowres;
1732 const int field_pic = s->picture_structure != PICT_FRAME;
1735 s->resync_mb_y = -1;
1737 av_assert0(mb_y < s->mb_height);
1739 init_get_bits(&s->gb, *buf, buf_size * 8);
1740 if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
1741 skip_bits(&s->gb, 3);
1743 ff_mpeg1_clean_buffers(s);
1744 s->interlaced_dct = 0;
1746 s->qscale = get_qscale(s);
1748 if (s->qscale == 0) {
1749 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1753 /* extra slice info */
1754 if (skip_1stop_8data_bits(&s->gb) < 0)
1755 return AVERROR_INVALIDDATA;
1759 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1762 while (get_bits_left(&s->gb) > 0) {
1763 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1764 MBINCR_VLC_BITS, 2);
1766 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1772 /* otherwise, stuffing, nothing to do */
1780 if (s->mb_x >= (unsigned) s->mb_width) {
1781 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1785 if (avctx->hwaccel && avctx->hwaccel->decode_slice) {
1786 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1787 int start_code = -1;
1788 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1789 if (buf_end < *buf + buf_size)
1792 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1793 return DECODE_SLICE_ERROR;
1795 return DECODE_SLICE_OK;
1798 s->resync_mb_x = s->mb_x;
1799 s->resync_mb_y = s->mb_y = mb_y;
1801 ff_init_block_index(s);
1803 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1804 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1805 av_log(s->avctx, AV_LOG_DEBUG,
1806 "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",
1808 s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
1809 s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1810 s->pict_type == AV_PICTURE_TYPE_I ? "I" :
1811 (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
1812 (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
1813 s->progressive_sequence ? "ps" : "",
1814 s->progressive_frame ? "pf" : "",
1815 s->alternate_scan ? "alt" : "",
1816 s->top_field_first ? "top" : "",
1817 s->intra_dc_precision, s->picture_structure,
1818 s->frame_pred_frame_dct, s->concealment_motion_vectors,
1819 s->q_scale_type, s->intra_vlc_format,
1820 s->repeat_first_field, s->chroma_420_type ? "420" : "");
1825 // If 1, we memcpy blocks in xvmcvideo.
1826 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
1827 ff_xvmc_init_block(s); // set s->block
1829 if (mpeg_decode_mb(s, s->block) < 0)
1832 // Note motion_val is normally NULL unless we want to extract the MVs.
1833 if (s->current_picture.motion_val[0] && !s->encoding) {
1834 const int wrap = s->b8_stride;
1835 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1836 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1837 int motion_x, motion_y, dir, i;
1839 for (i = 0; i < 2; i++) {
1840 for (dir = 0; dir < 2; dir++) {
1842 (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1843 motion_x = motion_y = 0;
1844 } else if (s->mv_type == MV_TYPE_16X16 ||
1845 (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1846 motion_x = s->mv[dir][0][0];
1847 motion_y = s->mv[dir][0][1];
1848 } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
1849 motion_x = s->mv[dir][i][0];
1850 motion_y = s->mv[dir][i][1];
1853 s->current_picture.motion_val[dir][xy][0] = motion_x;
1854 s->current_picture.motion_val[dir][xy][1] = motion_y;
1855 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1856 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1857 s->current_picture.ref_index [dir][b8_xy] =
1858 s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1859 av_assert2(s->field_select[dir][i] == 0 ||
1860 s->field_select[dir][i] == 1);
1867 s->dest[0] += 16 >> lowres;
1868 s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
1869 s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
1871 ff_mpv_decode_mb(s, s->block);
1873 if (++s->mb_x >= s->mb_width) {
1874 const int mb_size = 16 >> s->avctx->lowres;
1876 ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
1877 ff_mpv_report_decode_progress(s);
1880 s->mb_y += 1 << field_pic;
1882 if (s->mb_y >= s->mb_height) {
1883 int left = get_bits_left(&s->gb);
1884 int is_d10 = s->chroma_format == 2 &&
1885 s->pict_type == AV_PICTURE_TYPE_I &&
1886 avctx->profile == 0 && avctx->level == 5 &&
1887 s->intra_dc_precision == 2 &&
1888 s->q_scale_type == 1 && s->alternate_scan == 0 &&
1889 s->progressive_frame == 0
1890 /* vbv_delay == 0xBBB || 0xE10 */;
1892 if (left >= 32 && !is_d10) {
1893 GetBitContext gb = s->gb;
1894 align_get_bits(&gb);
1895 if (show_bits(&gb, 24) == 0x060E2B) {
1896 av_log(avctx, AV_LOG_DEBUG, "Invalid MXF data found in video stream\n");
1902 (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
1903 ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) {
1904 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
1905 left, show_bits(&s->gb, FFMIN(left, 23)));
1910 // There are some files out there which are missing the last slice
1911 // in cases where the slice is completely outside the visible
1912 // area, we detect this here instead of running into the end expecting
1914 if (s->mb_y >= ((s->height + 15) >> 4) &&
1915 s->progressive_frame &&
1916 !s->progressive_sequence &&
1917 get_bits_left(&s->gb) <= 8 &&
1918 get_bits_left(&s->gb) >= 0 &&
1919 s->mb_skip_run == -1 &&
1920 show_bits(&s->gb, 8) == 0)
1923 ff_init_block_index(s);
1926 /* skip mb handling */
1927 if (s->mb_skip_run == -1) {
1928 /* read increment again */
1931 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1932 MBINCR_VLC_BITS, 2);
1934 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1939 s->mb_skip_run += 33;
1940 } else if (code == 35) {
1941 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1942 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1945 goto eos; /* end of slice */
1947 /* otherwise, stuffing, nothing to do */
1949 s->mb_skip_run += code;
1953 if (s->mb_skip_run) {
1955 if (s->pict_type == AV_PICTURE_TYPE_I) {
1956 av_log(s->avctx, AV_LOG_ERROR,
1957 "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1963 for (i = 0; i < 12; i++)
1964 s->block_last_index[i] = -1;
1965 if (s->picture_structure == PICT_FRAME)
1966 s->mv_type = MV_TYPE_16X16;
1968 s->mv_type = MV_TYPE_FIELD;
1969 if (s->pict_type == AV_PICTURE_TYPE_P) {
1970 /* if P type, zero motion vector is implied */
1971 s->mv_dir = MV_DIR_FORWARD;
1972 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1973 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1974 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1975 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1977 /* if B type, reuse previous vectors and directions */
1978 s->mv[0][0][0] = s->last_mv[0][0][0];
1979 s->mv[0][0][1] = s->last_mv[0][0][1];
1980 s->mv[1][0][0] = s->last_mv[1][0][0];
1981 s->mv[1][0][1] = s->last_mv[1][0][1];
1986 eos: // end of slice
1987 if (get_bits_left(&s->gb) < 0) {
1988 av_log(s, AV_LOG_ERROR, "overread %d\n", -get_bits_left(&s->gb));
1989 return AVERROR_INVALIDDATA;
1991 *buf += (get_bits_count(&s->gb) - 1) / 8;
1992 av_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1996 static int slice_decode_thread(AVCodecContext *c, void *arg)
1998 MpegEncContext *s = *(void **) arg;
1999 const uint8_t *buf = s->gb.buffer;
2000 int mb_y = s->start_mb_y;
2001 const int field_pic = s->picture_structure != PICT_FRAME;
2003 s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
2006 uint32_t start_code;
2009 ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
2011 av_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
2012 ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
2013 s->start_mb_y, s->end_mb_y, s->er.error_count);
2015 if (c->err_recognition & AV_EF_EXPLODE)
2017 if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
2018 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
2020 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
2022 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
2023 s->mb_x - 1, s->mb_y,
2024 ER_AC_END | ER_DC_END | ER_MV_END);
2027 if (s->mb_y == s->end_mb_y)
2031 buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
2032 mb_y = start_code - SLICE_MIN_START_CODE;
2033 if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
2034 mb_y += (*buf&0xE0)<<2;
2036 if (s->picture_structure == PICT_BOTTOM_FIELD)
2038 if (mb_y < 0 || mb_y >= s->end_mb_y)
2044 * Handle slice ends.
2045 * @return 1 if it seems to be the last slice
2047 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
2049 Mpeg1Context *s1 = avctx->priv_data;
2050 MpegEncContext *s = &s1->mpeg_enc_ctx;
2052 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
2055 if (s->avctx->hwaccel) {
2056 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
2057 av_log(avctx, AV_LOG_ERROR,
2058 "hardware accelerator failed to decode picture\n");
2061 /* end of slice reached */
2062 if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field && !s1->first_slice) {
2065 ff_er_frame_end(&s->er);
2067 ff_mpv_frame_end(s);
2069 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
2070 int ret = av_frame_ref(pict, s->current_picture_ptr->f);
2073 ff_print_debug_info(s, s->current_picture_ptr, pict);
2074 ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG2);
2076 if (avctx->active_thread_type & FF_THREAD_FRAME)
2077 s->picture_number++;
2078 /* latency of 1 frame for I- and P-frames */
2079 /* XXX: use another variable than picture_number */
2080 if (s->last_picture_ptr) {
2081 int ret = av_frame_ref(pict, s->last_picture_ptr->f);
2084 ff_print_debug_info(s, s->last_picture_ptr, pict);
2085 ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG2);
2095 static int mpeg1_decode_sequence(AVCodecContext *avctx,
2096 const uint8_t *buf, int buf_size)
2098 Mpeg1Context *s1 = avctx->priv_data;
2099 MpegEncContext *s = &s1->mpeg_enc_ctx;
2103 init_get_bits(&s->gb, buf, buf_size * 8);
2105 width = get_bits(&s->gb, 12);
2106 height = get_bits(&s->gb, 12);
2107 if (width == 0 || height == 0) {
2108 av_log(avctx, AV_LOG_WARNING,
2109 "Invalid horizontal or vertical size value.\n");
2110 if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
2111 return AVERROR_INVALIDDATA;
2113 s->aspect_ratio_info = get_bits(&s->gb, 4);
2114 if (s->aspect_ratio_info == 0) {
2115 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
2116 if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
2119 s->frame_rate_index = get_bits(&s->gb, 4);
2120 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
2122 s->bit_rate = get_bits(&s->gb, 18) * 400;
2123 if (get_bits1(&s->gb) == 0) /* marker */
2128 s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
2129 skip_bits(&s->gb, 1);
2132 if (get_bits1(&s->gb)) {
2133 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
2135 for (i = 0; i < 64; i++) {
2136 j = s->idsp.idct_permutation[i];
2137 v = ff_mpeg1_default_intra_matrix[i];
2138 s->intra_matrix[j] = v;
2139 s->chroma_intra_matrix[j] = v;
2142 if (get_bits1(&s->gb)) {
2143 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
2145 for (i = 0; i < 64; i++) {
2146 int j = s->idsp.idct_permutation[i];
2147 v = ff_mpeg1_default_non_intra_matrix[i];
2148 s->inter_matrix[j] = v;
2149 s->chroma_inter_matrix[j] = v;
2153 if (show_bits(&s->gb, 23) != 0) {
2154 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2158 /* We set MPEG-2 parameters so that it emulates MPEG-1. */
2159 s->progressive_sequence = 1;
2160 s->progressive_frame = 1;
2161 s->picture_structure = PICT_FRAME;
2163 s->frame_pred_frame_dct = 1;
2164 s->chroma_format = 1;
2166 s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
2167 s->out_format = FMT_MPEG1;
2168 s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
2169 if (s->flags & CODEC_FLAG_LOW_DELAY)
2172 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2173 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d, aspect_ratio_info: %d \n",
2174 s->avctx->rc_buffer_size, s->bit_rate, s->aspect_ratio_info);
2179 static int vcr2_init_sequence(AVCodecContext *avctx)
2181 Mpeg1Context *s1 = avctx->priv_data;
2182 MpegEncContext *s = &s1->mpeg_enc_ctx;
2185 /* start new MPEG-1 context decoding */
2186 s->out_format = FMT_MPEG1;
2187 if (s1->mpeg_enc_ctx_allocated) {
2188 ff_mpv_common_end(s);
2189 s1->mpeg_enc_ctx_allocated = 0;
2191 s->width = avctx->coded_width;
2192 s->height = avctx->coded_height;
2193 avctx->has_b_frames = 0; // true?
2196 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2197 setup_hwaccel_for_pixfmt(avctx);
2199 ff_mpv_idct_init(s);
2200 if (ff_mpv_common_init(s) < 0)
2202 s1->mpeg_enc_ctx_allocated = 1;
2204 for (i = 0; i < 64; i++) {
2205 int j = s->idsp.idct_permutation[i];
2206 v = ff_mpeg1_default_intra_matrix[i];
2207 s->intra_matrix[j] = v;
2208 s->chroma_intra_matrix[j] = v;
2210 v = ff_mpeg1_default_non_intra_matrix[i];
2211 s->inter_matrix[j] = v;
2212 s->chroma_inter_matrix[j] = v;
2215 s->progressive_sequence = 1;
2216 s->progressive_frame = 1;
2217 s->picture_structure = PICT_FRAME;
2219 s->frame_pred_frame_dct = 1;
2220 s->chroma_format = 1;
2221 if (s->codec_tag == AV_RL32("BW10")) {
2222 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
2224 s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
2225 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
2227 s1->save_width = s->width;
2228 s1->save_height = s->height;
2229 s1->save_progressive_seq = s->progressive_sequence;
2233 static int mpeg_decode_a53_cc(AVCodecContext *avctx,
2234 const uint8_t *p, int buf_size)
2236 Mpeg1Context *s1 = avctx->priv_data;
2238 if (buf_size >= 6 &&
2239 p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
2240 p[4] == 3 && (p[5] & 0x40)) {
2241 /* extract A53 Part 4 CC data */
2242 int cc_count = p[5] & 0x1f;
2243 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2244 av_freep(&s1->a53_caption);
2245 s1->a53_caption_size = cc_count * 3;
2246 s1->a53_caption = av_malloc(s1->a53_caption_size);
2247 if (s1->a53_caption)
2248 memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
2251 } else if (buf_size >= 11 &&
2252 p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
2253 /* extract DVD CC data */
2256 // There is a caption count field in the data, but it is often
2257 // incorect. So count the number of captions present.
2258 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2260 // Transform the DVD format into A53 Part 4 format
2262 av_freep(&s1->a53_caption);
2263 s1->a53_caption_size = cc_count * 6;
2264 s1->a53_caption = av_malloc(s1->a53_caption_size);
2265 if (s1->a53_caption) {
2266 uint8_t field1 = !!(p[4] & 0x80);
2267 uint8_t *cap = s1->a53_caption;
2269 for (i = 0; i < cc_count; i++) {
2270 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2273 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2286 static void mpeg_decode_user_data(AVCodecContext *avctx,
2287 const uint8_t *p, int buf_size)
2289 Mpeg1Context *s = avctx->priv_data;
2290 const uint8_t *buf_end = p + buf_size;
2291 Mpeg1Context *s1 = avctx->priv_data;
2296 if (!memcmp(p+i, "\0TMPGEXS\0", 9)){
2300 /* for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
2301 av_log(avctx, AV_LOG_ERROR, "%c", p[i]);
2303 av_log(avctx, AV_LOG_ERROR, "\n");*/
2306 /* we parse the DTG active format information */
2307 if (buf_end - p >= 5 &&
2308 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2316 if (buf_end - p < 1)
2319 FF_DISABLE_DEPRECATION_WARNINGS
2320 avctx->dtg_active_format = p[0] & 0x0f;
2321 FF_ENABLE_DEPRECATION_WARNINGS
2322 #endif /* FF_API_AFD */
2324 s1->afd = p[0] & 0x0f;
2326 } else if (buf_end - p >= 6 &&
2327 p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
2328 p[4] == 0x03) { // S3D_video_format_length
2329 // the 0x7F mask ignores the reserved_bit value
2330 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2332 if (S3D_video_format_type == 0x03 ||
2333 S3D_video_format_type == 0x04 ||
2334 S3D_video_format_type == 0x08 ||
2335 S3D_video_format_type == 0x23) {
2337 s1->has_stereo3d = 1;
2339 switch (S3D_video_format_type) {
2341 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
2344 s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
2347 s1->stereo3d.type = AV_STEREO3D_2D;
2350 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2354 } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
2359 static void mpeg_decode_gop(AVCodecContext *avctx,
2360 const uint8_t *buf, int buf_size)
2362 Mpeg1Context *s1 = avctx->priv_data;
2363 MpegEncContext *s = &s1->mpeg_enc_ctx;
2367 init_get_bits(&s->gb, buf, buf_size * 8);
2369 tc = avctx->timecode_frame_start = get_bits(&s->gb, 25);
2371 s->closed_gop = get_bits1(&s->gb);
2372 /* broken_link indicate that after editing the
2373 * reference frames of the first B-Frames after GOP I-Frame
2374 * are missing (open gop) */
2375 broken_link = get_bits1(&s->gb);
2377 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2378 char tcbuf[AV_TIMECODE_STR_SIZE];
2379 av_timecode_make_mpeg_tc_string(tcbuf, tc);
2380 av_log(s->avctx, AV_LOG_DEBUG,
2381 "GOP (%s) closed_gop=%d broken_link=%d\n",
2382 tcbuf, s->closed_gop, broken_link);
2386 static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
2387 int *got_output, const uint8_t *buf, int buf_size)
2389 Mpeg1Context *s = avctx->priv_data;
2390 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2391 const uint8_t *buf_ptr = buf;
2392 const uint8_t *buf_end = buf + buf_size;
2393 int ret, input_size;
2394 int last_code = 0, skip_frame = 0;
2395 int picture_start_code_seen = 0;
2398 /* find next start code */
2399 uint32_t start_code = -1;
2400 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
2401 if (start_code > 0x1ff) {
2404 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2407 av_assert0(avctx->thread_count > 1);
2409 avctx->execute(avctx, slice_decode_thread,
2410 &s2->thread_context[0], NULL,
2411 s->slice_count, sizeof(void *));
2412 for (i = 0; i < s->slice_count; i++)
2413 s2->er.error_count += s2->thread_context[i]->er.error_count;
2416 if ((CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER)
2417 && uses_vdpau(avctx))
2418 ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
2420 ret = slice_end(avctx, picture);
2424 // FIXME: merge with the stuff in mpeg_decode_slice
2425 if (s2->last_picture_ptr || s2->low_delay)
2431 if (avctx->err_recognition & AV_EF_EXPLODE && s2->er.error_count)
2432 return AVERROR_INVALIDDATA;
2434 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2437 input_size = buf_end - buf_ptr;
2439 if (avctx->debug & FF_DEBUG_STARTCODE)
2440 av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %"PTRDIFF_SPECIFIER" left %d\n",
2441 start_code, buf_ptr - buf, input_size);
2443 /* prepare data for next start code */
2444 switch (start_code) {
2445 case SEQ_START_CODE:
2446 if (last_code == 0) {
2447 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2448 if (buf != avctx->extradata)
2451 av_log(avctx, AV_LOG_ERROR,
2452 "ignoring SEQ_START_CODE after %X\n", last_code);
2453 if (avctx->err_recognition & AV_EF_EXPLODE)
2454 return AVERROR_INVALIDDATA;
2458 case PICTURE_START_CODE:
2459 if (picture_start_code_seen && s2->picture_structure == PICT_FRAME) {
2460 /* If it's a frame picture, there can't be more than one picture header.
2461 Yet, it does happen and we need to handle it. */
2462 av_log(avctx, AV_LOG_WARNING, "ignoring extra picture following a frame-picture\n");
2465 picture_start_code_seen = 1;
2467 if (s2->width <= 0 || s2->height <= 0) {
2468 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
2469 s2->width, s2->height);
2470 return AVERROR_INVALIDDATA;
2474 s2->intra_dc_precision= 3;
2475 s2->intra_matrix[0]= 1;
2477 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
2478 !avctx->hwaccel && s->slice_count) {
2481 avctx->execute(avctx, slice_decode_thread,
2482 s2->thread_context, NULL,
2483 s->slice_count, sizeof(void *));
2484 for (i = 0; i < s->slice_count; i++)
2485 s2->er.error_count += s2->thread_context[i]->er.error_count;
2488 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2489 ret = mpeg_decode_postinit(avctx);
2491 av_log(avctx, AV_LOG_ERROR,
2492 "mpeg_decode_postinit() failure\n");
2496 /* We have a complete image: we try to decompress it. */
2497 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2500 last_code = PICTURE_START_CODE;
2502 av_log(avctx, AV_LOG_ERROR,
2503 "ignoring pic after %X\n", last_code);
2504 if (avctx->err_recognition & AV_EF_EXPLODE)
2505 return AVERROR_INVALIDDATA;
2508 case EXT_START_CODE:
2509 init_get_bits(&s2->gb, buf_ptr, input_size * 8);
2511 switch (get_bits(&s2->gb, 4)) {
2513 if (last_code == 0) {
2514 mpeg_decode_sequence_extension(s);
2516 av_log(avctx, AV_LOG_ERROR,
2517 "ignoring seq ext after %X\n", last_code);
2518 if (avctx->err_recognition & AV_EF_EXPLODE)
2519 return AVERROR_INVALIDDATA;
2523 mpeg_decode_sequence_display_extension(s);
2526 mpeg_decode_quant_matrix_extension(s2);
2529 mpeg_decode_picture_display_extension(s);
2532 if (last_code == PICTURE_START_CODE) {
2533 mpeg_decode_picture_coding_extension(s);
2535 av_log(avctx, AV_LOG_ERROR,
2536 "ignoring pic cod ext after %X\n", last_code);
2537 if (avctx->err_recognition & AV_EF_EXPLODE)
2538 return AVERROR_INVALIDDATA;
2543 case USER_START_CODE:
2544 mpeg_decode_user_data(avctx, buf_ptr, input_size);
2546 case GOP_START_CODE:
2547 if (last_code == 0) {
2548 s2->first_field = 0;
2549 mpeg_decode_gop(avctx, buf_ptr, input_size);
2552 av_log(avctx, AV_LOG_ERROR,
2553 "ignoring GOP_START_CODE after %X\n", last_code);
2554 if (avctx->err_recognition & AV_EF_EXPLODE)
2555 return AVERROR_INVALIDDATA;
2559 if (start_code >= SLICE_MIN_START_CODE &&
2560 start_code <= SLICE_MAX_START_CODE && last_code == PICTURE_START_CODE) {
2561 if (s2->progressive_sequence && !s2->progressive_frame) {
2562 s2->progressive_frame = 1;
2563 av_log(s2->avctx, AV_LOG_ERROR,
2564 "interlaced frame in progressive sequence, ignoring\n");
2567 if (s2->picture_structure == 0 ||
2568 (s2->progressive_frame && s2->picture_structure != PICT_FRAME)) {
2569 av_log(s2->avctx, AV_LOG_ERROR,
2570 "picture_structure %d invalid, ignoring\n",
2571 s2->picture_structure);
2572 s2->picture_structure = PICT_FRAME;
2575 if (s2->progressive_sequence && !s2->frame_pred_frame_dct)
2576 av_log(s2->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
2578 if (s2->picture_structure == PICT_FRAME) {
2579 s2->first_field = 0;
2580 s2->v_edge_pos = 16 * s2->mb_height;
2582 s2->first_field ^= 1;
2583 s2->v_edge_pos = 8 * s2->mb_height;
2584 memset(s2->mbskip_table, 0, s2->mb_stride * s2->mb_height);
2587 if (start_code >= SLICE_MIN_START_CODE &&
2588 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2589 const int field_pic = s2->picture_structure != PICT_FRAME;
2590 int mb_y = start_code - SLICE_MIN_START_CODE;
2591 last_code = SLICE_MIN_START_CODE;
2592 if (s2->codec_id != AV_CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
2593 mb_y += (*buf_ptr&0xE0)<<2;
2596 if (s2->picture_structure == PICT_BOTTOM_FIELD)
2599 if (buf_end - buf_ptr < 2) {
2600 av_log(s2->avctx, AV_LOG_ERROR, "slice too small\n");
2601 return AVERROR_INVALIDDATA;
2604 if (mb_y >= s2->mb_height) {
2605 av_log(s2->avctx, AV_LOG_ERROR,
2606 "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2610 if (!s2->last_picture_ptr) {
2611 /* Skip B-frames if we do not have reference frames and
2612 * GOP is not closed. */
2613 if (s2->pict_type == AV_PICTURE_TYPE_B) {
2614 if (!s2->closed_gop) {
2620 if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->flags2 & CODEC_FLAG2_SHOW_ALL))
2622 if (!s2->next_picture_ptr) {
2623 /* Skip P-frames if we do not have a reference frame or
2624 * we have an invalid header. */
2625 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
2630 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
2631 s2->pict_type == AV_PICTURE_TYPE_B) ||
2632 (avctx->skip_frame >= AVDISCARD_NONKEY &&
2633 s2->pict_type != AV_PICTURE_TYPE_I) ||
2634 avctx->skip_frame >= AVDISCARD_ALL) {
2639 if (!s->mpeg_enc_ctx_allocated)
2642 if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2643 if (mb_y < avctx->skip_top ||
2644 mb_y >= s2->mb_height - avctx->skip_bottom)
2648 if (!s2->pict_type) {
2649 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2650 if (avctx->err_recognition & AV_EF_EXPLODE)
2651 return AVERROR_INVALIDDATA;
2655 if (s->first_slice) {
2658 if (mpeg_field_start(s2, buf, buf_size) < 0)
2661 if (!s2->current_picture_ptr) {
2662 av_log(avctx, AV_LOG_ERROR,
2663 "current_picture not initialized\n");
2664 return AVERROR_INVALIDDATA;
2667 if (uses_vdpau(avctx)) {
2673 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2675 int threshold = (s2->mb_height * s->slice_count +
2676 s2->slice_context_count / 2) /
2677 s2->slice_context_count;
2678 av_assert0(avctx->thread_count > 1);
2679 if (threshold <= mb_y) {
2680 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2682 thread_context->start_mb_y = mb_y;
2683 thread_context->end_mb_y = s2->mb_height;
2684 if (s->slice_count) {
2685 s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
2686 ret = ff_update_duplicate_context(thread_context, s2);
2690 init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
2693 buf_ptr += 2; // FIXME add minimum number of bytes per slice
2695 ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2699 if (avctx->err_recognition & AV_EF_EXPLODE)
2701 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2702 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2703 s2->resync_mb_y, s2->mb_x, s2->mb_y,
2704 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
2706 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2707 s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
2708 ER_AC_END | ER_DC_END | ER_MV_END);
2717 static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
2718 int *got_output, AVPacket *avpkt)
2720 const uint8_t *buf = avpkt->data;
2722 int buf_size = avpkt->size;
2723 Mpeg1Context *s = avctx->priv_data;
2724 AVFrame *picture = data;
2725 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2727 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2728 /* special case for last picture */
2729 if (s2->low_delay == 0 && s2->next_picture_ptr) {
2730 int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
2734 s2->next_picture_ptr = NULL;
2741 if (s2->flags & CODEC_FLAG_TRUNCATED) {
2742 int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
2745 if (ff_combine_frame(&s2->parse_context, next,
2746 (const uint8_t **) &buf, &buf_size) < 0)
2750 s2->codec_tag = avpriv_toupper4(avctx->codec_tag);
2751 if (s->mpeg_enc_ctx_allocated == 0 && ( s2->codec_tag == AV_RL32("VCR2")
2752 || s2->codec_tag == AV_RL32("BW10")
2754 vcr2_init_sequence(avctx);
2758 if (avctx->extradata && !s->extradata_decoded) {
2759 ret = decode_chunks(avctx, picture, got_output,
2760 avctx->extradata, avctx->extradata_size);
2762 av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
2765 s->extradata_decoded = 1;
2766 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) {
2767 s2->current_picture_ptr = NULL;
2772 ret = decode_chunks(avctx, picture, got_output, buf, buf_size);
2773 if (ret<0 || *got_output)
2774 s2->current_picture_ptr = NULL;
2779 static void flush(AVCodecContext *avctx)
2781 Mpeg1Context *s = avctx->priv_data;
2785 ff_mpeg_flush(avctx);
2788 static av_cold int mpeg_decode_end(AVCodecContext *avctx)
2790 Mpeg1Context *s = avctx->priv_data;
2792 if (s->mpeg_enc_ctx_allocated)
2793 ff_mpv_common_end(&s->mpeg_enc_ctx);
2794 av_freep(&s->a53_caption);
2798 static const AVProfile mpeg2_video_profiles[] = {
2799 { FF_PROFILE_MPEG2_422, "4:2:2" },
2800 { FF_PROFILE_MPEG2_HIGH, "High" },
2801 { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2802 { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2803 { FF_PROFILE_MPEG2_MAIN, "Main" },
2804 { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2805 { FF_PROFILE_RESERVED, "Reserved" },
2806 { FF_PROFILE_RESERVED, "Reserved" },
2807 { FF_PROFILE_UNKNOWN },
2810 AVCodec ff_mpeg1video_decoder = {
2811 .name = "mpeg1video",
2812 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2813 .type = AVMEDIA_TYPE_VIDEO,
2814 .id = AV_CODEC_ID_MPEG1VIDEO,
2815 .priv_data_size = sizeof(Mpeg1Context),
2816 .init = mpeg_decode_init,
2817 .close = mpeg_decode_end,
2818 .decode = mpeg_decode_frame,
2819 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2820 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2821 CODEC_CAP_SLICE_THREADS,
2824 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2827 AVCodec ff_mpeg2video_decoder = {
2828 .name = "mpeg2video",
2829 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2830 .type = AVMEDIA_TYPE_VIDEO,
2831 .id = AV_CODEC_ID_MPEG2VIDEO,
2832 .priv_data_size = sizeof(Mpeg1Context),
2833 .init = mpeg_decode_init,
2834 .close = mpeg_decode_end,
2835 .decode = mpeg_decode_frame,
2836 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2837 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2838 CODEC_CAP_SLICE_THREADS,
2841 .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2845 AVCodec ff_mpegvideo_decoder = {
2846 .name = "mpegvideo",
2847 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2848 .type = AVMEDIA_TYPE_VIDEO,
2849 .id = AV_CODEC_ID_MPEG2VIDEO,
2850 .priv_data_size = sizeof(Mpeg1Context),
2851 .init = mpeg_decode_init,
2852 .close = mpeg_decode_end,
2853 .decode = mpeg_decode_frame,
2854 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
2860 #if CONFIG_MPEG_XVMC_DECODER
2861 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2863 if (avctx->active_thread_type & FF_THREAD_SLICE)
2865 if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2867 if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
2868 av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2870 mpeg_decode_init(avctx);
2872 avctx->pix_fmt = AV_PIX_FMT_XVMC_MPEG2_IDCT;
2873 avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2878 AVCodec ff_mpeg_xvmc_decoder = {
2879 .name = "mpegvideo_xvmc",
2880 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2881 .type = AVMEDIA_TYPE_VIDEO,
2882 .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2883 .priv_data_size = sizeof(Mpeg1Context),
2884 .init = mpeg_mc_decode_init,
2885 .close = mpeg_decode_end,
2886 .decode = mpeg_decode_frame,
2887 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2888 CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2893 #endif /* FF_API_XVMC */
2895 #if CONFIG_MPEG_VDPAU_DECODER
2896 AVCodec ff_mpeg_vdpau_decoder = {
2897 .name = "mpegvideo_vdpau",
2898 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
2899 .type = AVMEDIA_TYPE_VIDEO,
2900 .id = AV_CODEC_ID_MPEG2VIDEO,
2901 .priv_data_size = sizeof(Mpeg1Context),
2902 .init = mpeg_decode_init,
2903 .close = mpeg_decode_end,
2904 .decode = mpeg_decode_frame,
2905 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
2906 CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2911 #if CONFIG_MPEG1_VDPAU_DECODER
2912 AVCodec ff_mpeg1_vdpau_decoder = {
2913 .name = "mpeg1video_vdpau",
2914 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
2915 .type = AVMEDIA_TYPE_VIDEO,
2916 .id = AV_CODEC_ID_MPEG1VIDEO,
2917 .priv_data_size = sizeof(Mpeg1Context),
2918 .init = mpeg_decode_init,
2919 .close = mpeg_decode_end,
2920 .decode = mpeg_decode_frame,
2921 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
2922 CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,