3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "libavutil/attributes.h"
31 #include "libavutil/internal.h"
32 #include "libavutil/stereo3d.h"
35 #include "bytestream.h"
36 #include "error_resilience.h"
41 #include "mpeg12data.h"
42 #include "mpegutils.h"
43 #include "mpegvideo.h"
46 #include "xvmc_internal.h"
48 typedef struct Mpeg1Context {
49 MpegEncContext mpeg_enc_ctx;
50 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
51 int repeat_field; /* true if we must repeat the field */
52 AVPanScan pan_scan; /* some temporary storage for the panscan */
61 int save_width, save_height, save_progressive_seq;
62 AVRational frame_rate_ext; /* MPEG-2 specific framerate modificator */
63 int sync; /* Did we reach a sync point like a GOP/SEQ/KEYFrame? */
64 int closed_gop; /* GOP is closed */
66 int extradata_decoded;
69 #define MB_TYPE_ZERO_MV 0x20000000
71 static const uint32_t ptype2mb_type[7] = {
73 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
75 MB_TYPE_L0 | MB_TYPE_CBP,
76 MB_TYPE_QUANT | MB_TYPE_INTRA,
77 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
78 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
81 static const uint32_t btype2mb_type[11] = {
84 MB_TYPE_L1 | MB_TYPE_CBP,
86 MB_TYPE_L0 | MB_TYPE_CBP,
88 MB_TYPE_L0L1 | MB_TYPE_CBP,
89 MB_TYPE_QUANT | MB_TYPE_INTRA,
90 MB_TYPE_QUANT | MB_TYPE_L1 | MB_TYPE_CBP,
91 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
92 MB_TYPE_QUANT | MB_TYPE_L0L1 | MB_TYPE_CBP,
95 static const uint8_t non_linear_qscale[32] = {
96 0, 1, 2, 3, 4, 5, 6, 7,
97 8, 10, 12, 14, 16, 18, 20, 22,
98 24, 28, 32, 36, 40, 44, 48, 52,
99 56, 64, 72, 80, 88, 96, 104, 112,
102 /* as H.263, but only 17 codes */
103 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
105 int code, sign, val, shift;
107 code = get_vlc2(&s->gb, ff_mv_vlc.table, MV_VLC_BITS, 2);
113 sign = get_bits1(&s->gb);
117 val = (val - 1) << shift;
118 val |= get_bits(&s->gb, shift);
125 /* modulo decoding */
126 return sign_extend(val, 5 + shift);
129 #define check_scantable_index(ctx, x) \
132 av_log(ctx->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
133 ctx->mb_x, ctx->mb_y); \
134 return AVERROR_INVALIDDATA; \
138 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
139 int16_t *block, int n)
141 int level, dc, diff, i, j, run;
143 RLTable *rl = &ff_rl_mpeg1;
144 uint8_t *const scantable = s->intra_scantable.permutated;
145 const uint16_t *quant_matrix = s->intra_matrix;
146 const int qscale = s->qscale;
149 component = (n <= 3 ? 0 : n - 4 + 1);
150 diff = decode_dc(&s->gb, component);
152 return AVERROR_INVALIDDATA;
153 dc = s->last_dc[component];
155 s->last_dc[component] = dc;
156 block[0] = dc * quant_matrix[0];
157 av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
160 OPEN_READER(re, &s->gb);
161 /* now quantify & encode AC coefficients */
163 UPDATE_CACHE(re, &s->gb);
164 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
169 } else if (level != 0) {
171 check_scantable_index(s, i);
173 level = (level * qscale * quant_matrix[j]) >> 4;
174 level = (level - 1) | 1;
175 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
176 SHOW_SBITS(re, &s->gb, 1);
177 LAST_SKIP_BITS(re, &s->gb, 1);
180 run = SHOW_UBITS(re, &s->gb, 6) + 1;
181 LAST_SKIP_BITS(re, &s->gb, 6);
182 UPDATE_CACHE(re, &s->gb);
183 level = SHOW_SBITS(re, &s->gb, 8);
184 SKIP_BITS(re, &s->gb, 8);
186 level = SHOW_UBITS(re, &s->gb, 8) - 256;
187 LAST_SKIP_BITS(re, &s->gb, 8);
188 } else if (level == 0) {
189 level = SHOW_UBITS(re, &s->gb, 8);
190 LAST_SKIP_BITS(re, &s->gb, 8);
193 check_scantable_index(s, i);
197 level = (level * qscale * quant_matrix[j]) >> 4;
198 level = (level - 1) | 1;
201 level = (level * qscale * quant_matrix[j]) >> 4;
202 level = (level - 1) | 1;
208 CLOSE_READER(re, &s->gb);
210 s->block_last_index[n] = i;
214 int ff_mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
216 return mpeg1_decode_block_intra(s, block, n);
219 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
220 int16_t *block, int n)
222 int level, i, j, run;
223 RLTable *rl = &ff_rl_mpeg1;
224 uint8_t *const scantable = s->intra_scantable.permutated;
225 const uint16_t *quant_matrix = s->inter_matrix;
226 const int qscale = s->qscale;
229 OPEN_READER(re, &s->gb);
231 // special case for first coefficient, no need to add second VLC table
232 UPDATE_CACHE(re, &s->gb);
233 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
234 level = (3 * qscale * quant_matrix[0]) >> 5;
235 level = (level - 1) | 1;
236 if (GET_CACHE(re, &s->gb) & 0x40000000)
240 SKIP_BITS(re, &s->gb, 2);
241 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
244 /* now quantify & encode AC coefficients */
246 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
251 check_scantable_index(s, i);
253 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
254 level = (level - 1) | 1;
255 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
256 SHOW_SBITS(re, &s->gb, 1);
257 SKIP_BITS(re, &s->gb, 1);
260 run = SHOW_UBITS(re, &s->gb, 6) + 1;
261 LAST_SKIP_BITS(re, &s->gb, 6);
262 UPDATE_CACHE(re, &s->gb);
263 level = SHOW_SBITS(re, &s->gb, 8);
264 SKIP_BITS(re, &s->gb, 8);
266 level = SHOW_UBITS(re, &s->gb, 8) - 256;
267 SKIP_BITS(re, &s->gb, 8);
268 } else if (level == 0) {
269 level = SHOW_UBITS(re, &s->gb, 8);
270 SKIP_BITS(re, &s->gb, 8);
273 check_scantable_index(s, i);
277 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
278 level = (level - 1) | 1;
281 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
282 level = (level - 1) | 1;
287 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
289 UPDATE_CACHE(re, &s->gb);
292 LAST_SKIP_BITS(re, &s->gb, 2);
293 CLOSE_READER(re, &s->gb);
295 s->block_last_index[n] = i;
299 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s,
300 int16_t *block, int n)
302 int level, i, j, run;
303 RLTable *rl = &ff_rl_mpeg1;
304 uint8_t *const scantable = s->intra_scantable.permutated;
305 const int qscale = s->qscale;
308 OPEN_READER(re, &s->gb);
310 // Special case for first coefficient, no need to add second VLC table.
311 UPDATE_CACHE(re, &s->gb);
312 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
313 level = (3 * qscale) >> 1;
314 level = (level - 1) | 1;
315 if (GET_CACHE(re, &s->gb) & 0x40000000)
319 SKIP_BITS(re, &s->gb, 2);
320 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
324 /* now quantify & encode AC coefficients */
326 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
331 check_scantable_index(s, i);
333 level = ((level * 2 + 1) * qscale) >> 1;
334 level = (level - 1) | 1;
335 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
336 SHOW_SBITS(re, &s->gb, 1);
337 SKIP_BITS(re, &s->gb, 1);
340 run = SHOW_UBITS(re, &s->gb, 6) + 1;
341 LAST_SKIP_BITS(re, &s->gb, 6);
342 UPDATE_CACHE(re, &s->gb);
343 level = SHOW_SBITS(re, &s->gb, 8);
344 SKIP_BITS(re, &s->gb, 8);
346 level = SHOW_UBITS(re, &s->gb, 8) - 256;
347 SKIP_BITS(re, &s->gb, 8);
348 } else if (level == 0) {
349 level = SHOW_UBITS(re, &s->gb, 8);
350 SKIP_BITS(re, &s->gb, 8);
353 check_scantable_index(s, i);
357 level = ((level * 2 + 1) * qscale) >> 1;
358 level = (level - 1) | 1;
361 level = ((level * 2 + 1) * qscale) >> 1;
362 level = (level - 1) | 1;
367 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
369 UPDATE_CACHE(re, &s->gb);
372 LAST_SKIP_BITS(re, &s->gb, 2);
373 CLOSE_READER(re, &s->gb);
375 s->block_last_index[n] = i;
379 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
380 int16_t *block, int n)
382 int level, i, j, run;
383 RLTable *rl = &ff_rl_mpeg1;
384 uint8_t *const scantable = s->intra_scantable.permutated;
385 const uint16_t *quant_matrix;
386 const int qscale = s->qscale;
392 OPEN_READER(re, &s->gb);
395 quant_matrix = s->inter_matrix;
397 quant_matrix = s->chroma_inter_matrix;
399 // Special case for first coefficient, no need to add second VLC table.
400 UPDATE_CACHE(re, &s->gb);
401 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
402 level = (3 * qscale * quant_matrix[0]) >> 5;
403 if (GET_CACHE(re, &s->gb) & 0x40000000)
408 SKIP_BITS(re, &s->gb, 2);
409 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
413 /* now quantify & encode AC coefficients */
415 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
420 check_scantable_index(s, i);
422 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
423 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
424 SHOW_SBITS(re, &s->gb, 1);
425 SKIP_BITS(re, &s->gb, 1);
428 run = SHOW_UBITS(re, &s->gb, 6) + 1;
429 LAST_SKIP_BITS(re, &s->gb, 6);
430 UPDATE_CACHE(re, &s->gb);
431 level = SHOW_SBITS(re, &s->gb, 12);
432 SKIP_BITS(re, &s->gb, 12);
435 check_scantable_index(s, i);
438 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
441 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
447 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
449 UPDATE_CACHE(re, &s->gb);
452 LAST_SKIP_BITS(re, &s->gb, 2);
453 CLOSE_READER(re, &s->gb);
455 block[63] ^= (mismatch & 1);
457 s->block_last_index[n] = i;
461 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
462 int16_t *block, int n)
464 int level, i, j, run;
465 RLTable *rl = &ff_rl_mpeg1;
466 uint8_t *const scantable = s->intra_scantable.permutated;
467 const int qscale = s->qscale;
468 OPEN_READER(re, &s->gb);
471 // special case for first coefficient, no need to add second VLC table
472 UPDATE_CACHE(re, &s->gb);
473 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
474 level = (3 * qscale) >> 1;
475 if (GET_CACHE(re, &s->gb) & 0x40000000)
479 SKIP_BITS(re, &s->gb, 2);
480 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
484 /* now quantify & encode AC coefficients */
486 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
490 check_scantable_index(s, i);
492 level = ((level * 2 + 1) * qscale) >> 1;
493 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
494 SHOW_SBITS(re, &s->gb, 1);
495 SKIP_BITS(re, &s->gb, 1);
498 run = SHOW_UBITS(re, &s->gb, 6) + 1;
499 LAST_SKIP_BITS(re, &s->gb, 6);
500 UPDATE_CACHE(re, &s->gb);
501 level = SHOW_SBITS(re, &s->gb, 12);
502 SKIP_BITS(re, &s->gb, 12);
505 check_scantable_index(s, i);
508 level = ((-level * 2 + 1) * qscale) >> 1;
511 level = ((level * 2 + 1) * qscale) >> 1;
516 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
518 UPDATE_CACHE(re, &s->gb);
521 LAST_SKIP_BITS(re, &s->gb, 2);
522 CLOSE_READER(re, &s->gb);
523 s->block_last_index[n] = i;
527 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
528 int16_t *block, int n)
530 int level, dc, diff, i, j, run;
533 uint8_t *const scantable = s->intra_scantable.permutated;
534 const uint16_t *quant_matrix;
535 const int qscale = s->qscale;
540 quant_matrix = s->intra_matrix;
543 quant_matrix = s->chroma_intra_matrix;
544 component = (n & 1) + 1;
546 diff = decode_dc(&s->gb, component);
548 return AVERROR_INVALIDDATA;
549 dc = s->last_dc[component];
551 s->last_dc[component] = dc;
552 block[0] = dc << (3 - s->intra_dc_precision);
553 av_dlog(s->avctx, "dc=%d\n", block[0]);
554 mismatch = block[0] ^ 1;
556 if (s->intra_vlc_format)
562 OPEN_READER(re, &s->gb);
563 /* now quantify & encode AC coefficients */
565 UPDATE_CACHE(re, &s->gb);
566 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
571 } else if (level != 0) {
573 check_scantable_index(s, i);
575 level = (level * qscale * quant_matrix[j]) >> 4;
576 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
577 SHOW_SBITS(re, &s->gb, 1);
578 LAST_SKIP_BITS(re, &s->gb, 1);
581 run = SHOW_UBITS(re, &s->gb, 6) + 1;
582 LAST_SKIP_BITS(re, &s->gb, 6);
583 UPDATE_CACHE(re, &s->gb);
584 level = SHOW_SBITS(re, &s->gb, 12);
585 SKIP_BITS(re, &s->gb, 12);
587 check_scantable_index(s, i);
590 level = (-level * qscale * quant_matrix[j]) >> 4;
593 level = (level * qscale * quant_matrix[j]) >> 4;
600 CLOSE_READER(re, &s->gb);
602 block[63] ^= mismatch & 1;
604 s->block_last_index[n] = i;
608 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
609 int16_t *block, int n)
611 int level, dc, diff, i, j, run;
614 uint8_t *const scantable = s->intra_scantable.permutated;
615 const uint16_t *quant_matrix;
616 const int qscale = s->qscale;
620 quant_matrix = s->intra_matrix;
623 quant_matrix = s->chroma_intra_matrix;
624 component = (n & 1) + 1;
626 diff = decode_dc(&s->gb, component);
628 return AVERROR_INVALIDDATA;
629 dc = s->last_dc[component];
631 s->last_dc[component] = dc;
632 block[0] = dc << (3 - s->intra_dc_precision);
634 if (s->intra_vlc_format)
640 OPEN_READER(re, &s->gb);
641 /* now quantify & encode AC coefficients */
643 UPDATE_CACHE(re, &s->gb);
644 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
649 } else if (level != 0) {
651 check_scantable_index(s, i);
653 level = (level * qscale * quant_matrix[j]) >> 4;
654 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
655 SHOW_SBITS(re, &s->gb, 1);
656 LAST_SKIP_BITS(re, &s->gb, 1);
659 run = SHOW_UBITS(re, &s->gb, 6) + 1;
660 LAST_SKIP_BITS(re, &s->gb, 6);
661 UPDATE_CACHE(re, &s->gb);
662 level = SHOW_SBITS(re, &s->gb, 12);
663 SKIP_BITS(re, &s->gb, 12);
665 check_scantable_index(s, i);
668 level = (-level * qscale * quant_matrix[j]) >> 4;
671 level = (level * qscale * quant_matrix[j]) >> 4;
677 CLOSE_READER(re, &s->gb);
680 s->block_last_index[n] = i;
684 /******************************************/
687 static inline int get_dmv(MpegEncContext *s)
689 if (get_bits1(&s->gb))
690 return 1 - (get_bits1(&s->gb) << 1);
695 static inline int get_qscale(MpegEncContext *s)
697 int qscale = get_bits(&s->gb, 5);
699 return non_linear_qscale[qscale];
704 /* motion type (for MPEG-2) */
710 static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
712 int i, j, k, cbp, val, mb_type, motion_type;
713 const int mb_block_count = 4 + (1 << s->chroma_format);
716 av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
718 assert(s->mb_skipped == 0);
720 if (s->mb_skip_run-- != 0) {
721 if (s->pict_type == AV_PICTURE_TYPE_P) {
723 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
724 MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
729 mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
731 // FIXME not sure if this is allowed in MPEG at all
732 mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
733 if (IS_INTRA(mb_type))
734 return AVERROR_INVALIDDATA;
735 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
736 mb_type | MB_TYPE_SKIP;
737 // assert(s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1] & (MB_TYPE_16x16 | MB_TYPE_16x8));
739 if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
746 switch (s->pict_type) {
748 case AV_PICTURE_TYPE_I:
749 if (get_bits1(&s->gb) == 0) {
750 if (get_bits1(&s->gb) == 0) {
751 av_log(s->avctx, AV_LOG_ERROR,
752 "invalid mb type in I Frame at %d %d\n",
754 return AVERROR_INVALIDDATA;
756 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
758 mb_type = MB_TYPE_INTRA;
761 case AV_PICTURE_TYPE_P:
762 mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
764 av_log(s->avctx, AV_LOG_ERROR,
765 "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
766 return AVERROR_INVALIDDATA;
768 mb_type = ptype2mb_type[mb_type];
770 case AV_PICTURE_TYPE_B:
771 mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
773 av_log(s->avctx, AV_LOG_ERROR,
774 "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
775 return AVERROR_INVALIDDATA;
777 mb_type = btype2mb_type[mb_type];
780 av_dlog(s->avctx, "mb_type=%x\n", mb_type);
781 // motion_type = 0; /* avoid warning */
782 if (IS_INTRA(mb_type)) {
783 s->bdsp.clear_blocks(s->block[0]);
785 if (!s->chroma_y_shift)
786 s->bdsp.clear_blocks(s->block[6]);
788 /* compute DCT type */
789 // FIXME: add an interlaced_dct coded var?
790 if (s->picture_structure == PICT_FRAME &&
791 !s->frame_pred_frame_dct)
792 s->interlaced_dct = get_bits1(&s->gb);
794 if (IS_QUANT(mb_type))
795 s->qscale = get_qscale(s);
797 if (s->concealment_motion_vectors) {
798 /* just parse them */
799 if (s->picture_structure != PICT_FRAME)
800 skip_bits1(&s->gb); /* field select */
803 s->last_mv[0][0][0] =
804 s->last_mv[0][1][0] = mpeg_decode_motion(s, s->mpeg_f_code[0][0],
805 s->last_mv[0][0][0]);
807 s->last_mv[0][0][1] =
808 s->last_mv[0][1][1] = mpeg_decode_motion(s, s->mpeg_f_code[0][1],
809 s->last_mv[0][0][1]);
811 skip_bits1(&s->gb); /* marker */
813 /* reset mv prediction */
814 memset(s->last_mv, 0, sizeof(s->last_mv));
818 FF_DISABLE_DEPRECATION_WARNINGS
819 // if 1, we memcpy blocks in xvmcvideo
820 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
821 ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
822 FF_ENABLE_DEPRECATION_WARNINGS
823 #endif /* FF_API_XVMC */
825 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
826 if (s->flags2 & CODEC_FLAG2_FAST) {
827 for (i = 0; i < 6; i++)
828 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
830 for (i = 0; i < mb_block_count; i++)
831 if ((ret = mpeg2_decode_block_intra(s, *s->pblocks[i], i)) < 0)
835 for (i = 0; i < 6; i++)
836 if ((ret = mpeg1_decode_block_intra(s, *s->pblocks[i], i)) < 0)
840 if (mb_type & MB_TYPE_ZERO_MV) {
841 assert(mb_type & MB_TYPE_CBP);
843 s->mv_dir = MV_DIR_FORWARD;
844 if (s->picture_structure == PICT_FRAME) {
845 if (!s->frame_pred_frame_dct)
846 s->interlaced_dct = get_bits1(&s->gb);
847 s->mv_type = MV_TYPE_16X16;
849 s->mv_type = MV_TYPE_FIELD;
850 mb_type |= MB_TYPE_INTERLACED;
851 s->field_select[0][0] = s->picture_structure - 1;
854 if (IS_QUANT(mb_type))
855 s->qscale = get_qscale(s);
857 s->last_mv[0][0][0] = 0;
858 s->last_mv[0][0][1] = 0;
859 s->last_mv[0][1][0] = 0;
860 s->last_mv[0][1][1] = 0;
864 assert(mb_type & MB_TYPE_L0L1);
865 // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
866 /* get additional motion vector type */
867 if (s->frame_pred_frame_dct) {
868 motion_type = MT_FRAME;
870 motion_type = get_bits(&s->gb, 2);
871 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
872 s->interlaced_dct = get_bits1(&s->gb);
875 if (IS_QUANT(mb_type))
876 s->qscale = get_qscale(s);
879 s->mv_dir = (mb_type >> 13) & 3;
880 av_dlog(s->avctx, "motion_type=%d\n", motion_type);
881 switch (motion_type) {
882 case MT_FRAME: /* or MT_16X8 */
883 if (s->picture_structure == PICT_FRAME) {
884 mb_type |= MB_TYPE_16x16;
885 s->mv_type = MV_TYPE_16X16;
886 for (i = 0; i < 2; i++) {
887 if (USES_LIST(mb_type, i)) {
890 s->last_mv[i][0][0] =
891 s->last_mv[i][1][0] =
892 mpeg_decode_motion(s, s->mpeg_f_code[i][0],
893 s->last_mv[i][0][0]);
895 s->last_mv[i][0][1] =
896 s->last_mv[i][1][1] =
897 mpeg_decode_motion(s, s->mpeg_f_code[i][1],
898 s->last_mv[i][0][1]);
899 /* full_pel: only for MPEG-1 */
900 if (s->full_pel[i]) {
901 s->mv[i][0][0] <<= 1;
902 s->mv[i][0][1] <<= 1;
907 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
908 s->mv_type = MV_TYPE_16X8;
909 for (i = 0; i < 2; i++) {
910 if (USES_LIST(mb_type, i)) {
912 for (j = 0; j < 2; j++) {
913 s->field_select[i][j] = get_bits1(&s->gb);
914 for (k = 0; k < 2; k++) {
915 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
916 s->last_mv[i][j][k]);
917 s->last_mv[i][j][k] = val;
918 s->mv[i][j][k] = val;
926 s->mv_type = MV_TYPE_FIELD;
927 if (s->picture_structure == PICT_FRAME) {
928 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
929 for (i = 0; i < 2; i++) {
930 if (USES_LIST(mb_type, i)) {
931 for (j = 0; j < 2; j++) {
932 s->field_select[i][j] = get_bits1(&s->gb);
933 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
934 s->last_mv[i][j][0]);
935 s->last_mv[i][j][0] = val;
936 s->mv[i][j][0] = val;
937 av_dlog(s->avctx, "fmx=%d\n", val);
938 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
939 s->last_mv[i][j][1] >> 1);
940 s->last_mv[i][j][1] = val << 1;
941 s->mv[i][j][1] = val;
942 av_dlog(s->avctx, "fmy=%d\n", val);
947 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
948 for (i = 0; i < 2; i++) {
949 if (USES_LIST(mb_type, i)) {
950 s->field_select[i][0] = get_bits1(&s->gb);
951 for (k = 0; k < 2; k++) {
952 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
953 s->last_mv[i][0][k]);
954 s->last_mv[i][0][k] = val;
955 s->last_mv[i][1][k] = val;
956 s->mv[i][0][k] = val;
963 s->mv_type = MV_TYPE_DMV;
964 for (i = 0; i < 2; i++) {
965 if (USES_LIST(mb_type, i)) {
966 int dmx, dmy, mx, my, m;
967 const int my_shift = s->picture_structure == PICT_FRAME;
969 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
970 s->last_mv[i][0][0]);
971 s->last_mv[i][0][0] = mx;
972 s->last_mv[i][1][0] = mx;
974 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
975 s->last_mv[i][0][1] >> my_shift);
979 s->last_mv[i][0][1] = my << my_shift;
980 s->last_mv[i][1][1] = my << my_shift;
984 s->mv[i][1][0] = mx; // not used
985 s->mv[i][1][1] = my; // not used
987 if (s->picture_structure == PICT_FRAME) {
988 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
990 // m = 1 + 2 * s->top_field_first;
991 m = s->top_field_first ? 1 : 3;
993 /* top -> top pred */
994 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
995 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
997 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
998 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1000 mb_type |= MB_TYPE_16x16;
1002 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1003 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1004 if (s->picture_structure == PICT_TOP_FIELD)
1013 av_log(s->avctx, AV_LOG_ERROR,
1014 "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1015 return AVERROR_INVALIDDATA;
1020 if (HAS_CBP(mb_type)) {
1021 s->bdsp.clear_blocks(s->block[0]);
1023 cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1024 if (mb_block_count > 6) {
1025 cbp <<= mb_block_count - 6;
1026 cbp |= get_bits(&s->gb, mb_block_count - 6);
1027 s->bdsp.clear_blocks(s->block[6]);
1030 av_log(s->avctx, AV_LOG_ERROR,
1031 "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
1032 return AVERROR_INVALIDDATA;
1036 FF_DISABLE_DEPRECATION_WARNINGS
1037 // if 1, we memcpy blocks in xvmcvideo
1038 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1039 ff_xvmc_pack_pblocks(s, cbp);
1040 FF_ENABLE_DEPRECATION_WARNINGS
1041 #endif /* FF_API_XVMC */
1043 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1044 if (s->flags2 & CODEC_FLAG2_FAST) {
1045 for (i = 0; i < 6; i++) {
1047 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
1049 s->block_last_index[i] = -1;
1053 cbp <<= 12 - mb_block_count;
1055 for (i = 0; i < mb_block_count; i++) {
1056 if (cbp & (1 << 11)) {
1057 if ((ret = mpeg2_decode_block_non_intra(s, *s->pblocks[i], i)) < 0)
1060 s->block_last_index[i] = -1;
1066 if (s->flags2 & CODEC_FLAG2_FAST) {
1067 for (i = 0; i < 6; i++) {
1069 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
1071 s->block_last_index[i] = -1;
1075 for (i = 0; i < 6; i++) {
1077 if ((ret = mpeg1_decode_block_inter(s, *s->pblocks[i], i)) < 0)
1080 s->block_last_index[i] = -1;
1087 for (i = 0; i < 12; i++)
1088 s->block_last_index[i] = -1;
1092 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
1097 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1099 Mpeg1Context *s = avctx->priv_data;
1100 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1102 ff_mpv_decode_defaults(s2);
1104 s->mpeg_enc_ctx.avctx = avctx;
1105 s->mpeg_enc_ctx.flags = avctx->flags;
1106 s->mpeg_enc_ctx.flags2 = avctx->flags2;
1108 /* we need some permutation to store matrices,
1109 * until the decoder sets the real permutation. */
1110 ff_mpv_idct_init(s2);
1111 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1112 ff_mpeg12_init_vlcs();
1114 s->mpeg_enc_ctx_allocated = 0;
1115 s->mpeg_enc_ctx.picture_number = 0;
1116 s->repeat_field = 0;
1117 s->mpeg_enc_ctx.codec_id = avctx->codec->id;
1118 avctx->color_range = AVCOL_RANGE_MPEG;
1119 if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
1120 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1122 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1126 static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
1127 const AVCodecContext *avctx_from)
1129 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1130 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1133 if (avctx == avctx_from ||
1134 !ctx_from->mpeg_enc_ctx_allocated ||
1135 !s1->context_initialized)
1138 err = ff_mpeg_update_thread_context(avctx, avctx_from);
1142 if (!ctx->mpeg_enc_ctx_allocated) {
1143 // copy the whole context after the initial MpegEncContext structure
1144 memcpy(ctx, ctx_from, sizeof(*ctx));
1145 memset(&ctx->mpeg_enc_ctx, 0, sizeof(ctx->mpeg_enc_ctx));
1148 if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
1149 s->picture_number++;
1154 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1155 const uint8_t *new_perm)
1157 uint16_t temp_matrix[64];
1160 memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
1162 for (i = 0; i < 64; i++)
1163 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1167 static const enum AVPixelFormat pixfmt_xvmc_mpg2_420[] = {
1168 AV_PIX_FMT_XVMC_MPEG2_IDCT,
1169 AV_PIX_FMT_XVMC_MPEG2_MC,
1172 #endif /* FF_API_XVMC */
1174 static const enum AVPixelFormat mpeg12_hwaccel_pixfmt_list_420[] = {
1175 #if CONFIG_MPEG2_DXVA2_HWACCEL
1176 AV_PIX_FMT_DXVA2_VLD,
1178 #if CONFIG_MPEG2_VAAPI_HWACCEL
1179 AV_PIX_FMT_VAAPI_VLD,
1181 #if CONFIG_MPEG1_VDPAU_HWACCEL | CONFIG_MPEG2_VDPAU_HWACCEL
1188 static const enum AVPixelFormat mpeg12_pixfmt_list_422[] = {
1193 static const enum AVPixelFormat mpeg12_pixfmt_list_444[] = {
1198 static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
1200 Mpeg1Context *s1 = avctx->priv_data;
1201 MpegEncContext *s = &s1->mpeg_enc_ctx;
1202 const enum AVPixelFormat *pix_fmts;
1205 FF_DISABLE_DEPRECATION_WARNINGS
1206 if (avctx->xvmc_acceleration)
1207 return ff_get_format(avctx, pixfmt_xvmc_mpg2_420);
1208 FF_ENABLE_DEPRECATION_WARNINGS
1209 #endif /* FF_API_XVMC */
1211 if (s->chroma_format < 2)
1212 pix_fmts = mpeg12_hwaccel_pixfmt_list_420;
1213 else if (s->chroma_format == 2)
1214 pix_fmts = mpeg12_pixfmt_list_422;
1216 pix_fmts = mpeg12_pixfmt_list_444;
1218 return ff_get_format(avctx, pix_fmts);
1221 /* Call this function when we know all parameters.
1222 * It may be called in different places for MPEG-1 and MPEG-2. */
1223 static int mpeg_decode_postinit(AVCodecContext *avctx)
1225 Mpeg1Context *s1 = avctx->priv_data;
1226 MpegEncContext *s = &s1->mpeg_enc_ctx;
1227 uint8_t old_permutation[64];
1230 if ((s1->mpeg_enc_ctx_allocated == 0) ||
1231 avctx->coded_width != s->width ||
1232 avctx->coded_height != s->height ||
1233 s1->save_width != s->width ||
1234 s1->save_height != s->height ||
1235 s1->save_aspect_info != s->aspect_ratio_info ||
1236 s1->save_progressive_seq != s->progressive_sequence ||
1238 if (s1->mpeg_enc_ctx_allocated) {
1239 ParseContext pc = s->parse_context;
1240 s->parse_context.buffer = 0;
1241 ff_mpv_common_end(s);
1242 s->parse_context = pc;
1245 ret = ff_set_dimensions(avctx, s->width, s->height);
1249 avctx->bit_rate = s->bit_rate;
1250 s1->save_aspect_info = s->aspect_ratio_info;
1251 s1->save_width = s->width;
1252 s1->save_height = s->height;
1253 s1->save_progressive_seq = s->progressive_sequence;
1255 /* low_delay may be forced, in this case we will have B-frames
1256 * that behave like P-frames. */
1257 avctx->has_b_frames = !s->low_delay;
1259 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1261 avctx->framerate = ff_mpeg12_frame_rate_tab[s->frame_rate_index];
1263 avctx->sample_aspect_ratio = av_d2q(1.0 / ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1264 avctx->ticks_per_frame = 1;
1267 av_reduce(&s->avctx->framerate.num,
1268 &s->avctx->framerate.den,
1269 ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num * 2,
1270 ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1272 avctx->ticks_per_frame = 2;
1274 if (s->aspect_ratio_info > 1) {
1276 av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1277 (AVRational) { s1->pan_scan.width,
1278 s1->pan_scan.height }),
1279 (AVRational) { s->width, s->height });
1281 /* We ignore the spec here and guess a bit as reality does not
1282 * match the spec, see for example res_change_ffmpeg_aspect.ts
1283 * and sequence-display-aspect.mpg.
1284 * issue1613, 621, 562 */
1285 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1286 (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
1287 av_cmp_q(dar, (AVRational) { 16, 9 }))) {
1288 s->avctx->sample_aspect_ratio =
1289 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1290 (AVRational) { s->width, s->height });
1292 s->avctx->sample_aspect_ratio =
1293 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1294 (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
1295 // issue1613 4/3 16/9 -> 16/9
1296 // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1297 // widescreen-issue562.mpg 4/3 16/9 -> 16/9
1298 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1299 av_dlog(avctx, "A %d/%d\n",
1300 ff_mpeg2_aspect[s->aspect_ratio_info].num,
1301 ff_mpeg2_aspect[s->aspect_ratio_info].den);
1302 av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
1303 s->avctx->sample_aspect_ratio.den);
1306 s->avctx->sample_aspect_ratio =
1307 ff_mpeg2_aspect[s->aspect_ratio_info];
1311 ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
1313 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1314 // until then pix_fmt may be changed right after codec init
1316 if ((avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT ||
1317 avctx->hwaccel) && avctx->idct_algo == FF_IDCT_AUTO)
1319 if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
1320 #endif /* FF_API_XVMC */
1321 avctx->idct_algo = FF_IDCT_SIMPLE;
1323 /* Quantization matrices may need reordering
1324 * if DCT permutation is changed. */
1325 memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
1327 ff_mpv_idct_init(s);
1328 if ((ret = ff_mpv_common_init(s)) < 0)
1331 quant_matrix_rebuild(s->intra_matrix, old_permutation, s->idsp.idct_permutation);
1332 quant_matrix_rebuild(s->inter_matrix, old_permutation, s->idsp.idct_permutation);
1333 quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
1334 quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
1336 s1->mpeg_enc_ctx_allocated = 1;
1341 static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
1344 Mpeg1Context *s1 = avctx->priv_data;
1345 MpegEncContext *s = &s1->mpeg_enc_ctx;
1346 int ref, f_code, vbv_delay;
1348 init_get_bits(&s->gb, buf, buf_size * 8);
1350 ref = get_bits(&s->gb, 10); /* temporal ref */
1351 s->pict_type = get_bits(&s->gb, 3);
1352 if (s->pict_type == 0 || s->pict_type > 3)
1353 return AVERROR_INVALIDDATA;
1355 vbv_delay = get_bits(&s->gb, 16);
1356 if (s->pict_type == AV_PICTURE_TYPE_P ||
1357 s->pict_type == AV_PICTURE_TYPE_B) {
1358 s->full_pel[0] = get_bits1(&s->gb);
1359 f_code = get_bits(&s->gb, 3);
1360 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1361 return AVERROR_INVALIDDATA;
1362 s->mpeg_f_code[0][0] = f_code;
1363 s->mpeg_f_code[0][1] = f_code;
1365 if (s->pict_type == AV_PICTURE_TYPE_B) {
1366 s->full_pel[1] = get_bits1(&s->gb);
1367 f_code = get_bits(&s->gb, 3);
1368 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1369 return AVERROR_INVALIDDATA;
1370 s->mpeg_f_code[1][0] = f_code;
1371 s->mpeg_f_code[1][1] = f_code;
1373 s->current_picture.f->pict_type = s->pict_type;
1374 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1376 if (avctx->debug & FF_DEBUG_PICT_INFO)
1377 av_log(avctx, AV_LOG_DEBUG,
1378 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1385 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1387 MpegEncContext *s = &s1->mpeg_enc_ctx;
1388 int horiz_size_ext, vert_size_ext;
1391 skip_bits(&s->gb, 1); /* profile and level esc*/
1392 s->avctx->profile = get_bits(&s->gb, 3);
1393 s->avctx->level = get_bits(&s->gb, 4);
1394 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1395 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1396 horiz_size_ext = get_bits(&s->gb, 2);
1397 vert_size_ext = get_bits(&s->gb, 2);
1398 s->width |= (horiz_size_ext << 12);
1399 s->height |= (vert_size_ext << 12);
1400 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1401 s->bit_rate += (bit_rate_ext << 18) * 400;
1402 skip_bits1(&s->gb); /* marker */
1403 s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1405 s->low_delay = get_bits1(&s->gb);
1406 if (s->flags & CODEC_FLAG_LOW_DELAY)
1409 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1410 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1412 av_dlog(s->avctx, "sequence extension\n");
1413 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1415 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1416 av_log(s->avctx, AV_LOG_DEBUG,
1417 "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1418 s->avctx->profile, s->avctx->level,
1419 s->avctx->rc_buffer_size, s->bit_rate);
1422 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1424 MpegEncContext *s = &s1->mpeg_enc_ctx;
1425 int color_description, w, h;
1427 skip_bits(&s->gb, 3); /* video format */
1428 color_description = get_bits1(&s->gb);
1429 if (color_description) {
1430 s->avctx->color_primaries = get_bits(&s->gb, 8);
1431 s->avctx->color_trc = get_bits(&s->gb, 8);
1432 s->avctx->colorspace = get_bits(&s->gb, 8);
1434 w = get_bits(&s->gb, 14);
1435 skip_bits(&s->gb, 1); // marker
1436 h = get_bits(&s->gb, 14);
1437 // remaining 3 bits are zero padding
1439 s1->pan_scan.width = 16 * w;
1440 s1->pan_scan.height = 16 * h;
1442 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1443 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1446 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1448 MpegEncContext *s = &s1->mpeg_enc_ctx;
1452 if (s->progressive_sequence) {
1453 if (s->repeat_first_field) {
1455 if (s->top_field_first)
1459 if (s->picture_structure == PICT_FRAME) {
1461 if (s->repeat_first_field)
1465 for (i = 0; i < nofco; i++) {
1466 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1467 skip_bits(&s->gb, 1); // marker
1468 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1469 skip_bits(&s->gb, 1); // marker
1472 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1473 av_log(s->avctx, AV_LOG_DEBUG,
1474 "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
1475 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1476 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1477 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1480 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
1481 uint16_t matrix1[64], int intra)
1485 for (i = 0; i < 64; i++) {
1486 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1487 int v = get_bits(&s->gb, 8);
1489 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1490 return AVERROR_INVALIDDATA;
1492 if (intra && i == 0 && v != 8) {
1493 av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
1494 v = 8; // needed by pink.mpg / issue1046
1503 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1505 av_dlog(s->avctx, "matrix extension\n");
1507 if (get_bits1(&s->gb))
1508 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1509 if (get_bits1(&s->gb))
1510 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1511 if (get_bits1(&s->gb))
1512 load_matrix(s, s->chroma_intra_matrix, NULL, 1);
1513 if (get_bits1(&s->gb))
1514 load_matrix(s, s->chroma_inter_matrix, NULL, 0);
1517 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1519 MpegEncContext *s = &s1->mpeg_enc_ctx;
1521 s->full_pel[0] = s->full_pel[1] = 0;
1522 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1523 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1524 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1525 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1526 if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1527 av_log(s->avctx, AV_LOG_ERROR,
1528 "Missing picture start code, guessing missing values\n");
1529 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1530 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1531 s->pict_type = AV_PICTURE_TYPE_I;
1533 s->pict_type = AV_PICTURE_TYPE_P;
1535 s->pict_type = AV_PICTURE_TYPE_B;
1536 s->current_picture.f->pict_type = s->pict_type;
1537 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1539 s->intra_dc_precision = get_bits(&s->gb, 2);
1540 s->picture_structure = get_bits(&s->gb, 2);
1541 s->top_field_first = get_bits1(&s->gb);
1542 s->frame_pred_frame_dct = get_bits1(&s->gb);
1543 s->concealment_motion_vectors = get_bits1(&s->gb);
1544 s->q_scale_type = get_bits1(&s->gb);
1545 s->intra_vlc_format = get_bits1(&s->gb);
1546 s->alternate_scan = get_bits1(&s->gb);
1547 s->repeat_first_field = get_bits1(&s->gb);
1548 s->chroma_420_type = get_bits1(&s->gb);
1549 s->progressive_frame = get_bits1(&s->gb);
1551 if (s->progressive_sequence && !s->progressive_frame) {
1552 s->progressive_frame = 1;
1553 av_log(s->avctx, AV_LOG_ERROR,
1554 "interlaced frame in progressive sequence, ignoring\n");
1557 if (s->picture_structure == 0 ||
1558 (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
1559 av_log(s->avctx, AV_LOG_ERROR,
1560 "picture_structure %d invalid, ignoring\n",
1561 s->picture_structure);
1562 s->picture_structure = PICT_FRAME;
1565 if (s->progressive_sequence && !s->frame_pred_frame_dct)
1566 av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
1568 if (s->picture_structure == PICT_FRAME) {
1570 s->v_edge_pos = 16 * s->mb_height;
1572 s->first_field ^= 1;
1573 s->v_edge_pos = 8 * s->mb_height;
1574 memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
1577 if (s->alternate_scan) {
1578 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
1579 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
1581 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
1582 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
1585 /* composite display not parsed */
1586 av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1587 av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1588 av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1589 av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1590 av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1591 av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1592 av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1593 av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1594 av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1597 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1599 AVCodecContext *avctx = s->avctx;
1600 Mpeg1Context *s1 = (Mpeg1Context *) s;
1603 /* start frame decoding */
1604 if (s->first_field || s->picture_structure == PICT_FRAME) {
1605 AVFrameSideData *pan_scan;
1607 if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
1610 ff_mpeg_er_frame_start(s);
1612 /* first check if we must repeat the frame */
1613 s->current_picture_ptr->f->repeat_pict = 0;
1614 if (s->repeat_first_field) {
1615 if (s->progressive_sequence) {
1616 if (s->top_field_first)
1617 s->current_picture_ptr->f->repeat_pict = 4;
1619 s->current_picture_ptr->f->repeat_pict = 2;
1620 } else if (s->progressive_frame) {
1621 s->current_picture_ptr->f->repeat_pict = 1;
1625 pan_scan = av_frame_new_side_data(s->current_picture_ptr->f,
1626 AV_FRAME_DATA_PANSCAN,
1627 sizeof(s1->pan_scan));
1629 return AVERROR(ENOMEM);
1630 memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
1632 if (s1->a53_caption) {
1633 AVFrameSideData *sd = av_frame_new_side_data(
1634 s->current_picture_ptr->f, AV_FRAME_DATA_A53_CC,
1635 s1->a53_caption_size);
1637 memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
1638 av_freep(&s1->a53_caption);
1641 if (s1->has_stereo3d) {
1642 AVStereo3D *stereo = av_stereo3d_create_side_data(s->current_picture_ptr->f);
1644 return AVERROR(ENOMEM);
1646 *stereo = s1->stereo3d;
1647 s1->has_stereo3d = 0;
1651 AVFrameSideData *sd =
1652 av_frame_new_side_data(s->current_picture_ptr->f,
1653 AV_FRAME_DATA_AFD, 1);
1655 return AVERROR(ENOMEM);
1657 *sd->data = s1->afd;
1661 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1662 ff_thread_finish_setup(avctx);
1663 } else { // second field
1666 if (!s->current_picture_ptr) {
1667 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1668 return AVERROR_INVALIDDATA;
1671 if (s->avctx->hwaccel &&
1672 (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
1673 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1674 av_log(avctx, AV_LOG_ERROR,
1675 "hardware accelerator failed to decode first field\n");
1678 for (i = 0; i < 4; i++) {
1679 s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
1680 if (s->picture_structure == PICT_BOTTOM_FIELD)
1681 s->current_picture.f->data[i] +=
1682 s->current_picture_ptr->f->linesize[i];
1686 if (avctx->hwaccel) {
1687 if ((ret = avctx->hwaccel->start_frame(avctx, buf, buf_size)) < 0)
1692 FF_DISABLE_DEPRECATION_WARNINGS
1693 // ff_mpv_frame_start will call this function too,
1694 // but we need to call it on every field
1695 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1696 if (ff_xvmc_field_start(s, avctx) < 0)
1698 FF_ENABLE_DEPRECATION_WARNINGS
1699 #endif /* FF_API_XVMC */
1704 #define DECODE_SLICE_ERROR -1
1705 #define DECODE_SLICE_OK 0
1709 * MpegEncContext.mb_y must be set to the MB row from the startcode.
1710 * @return DECODE_SLICE_ERROR if the slice is damaged,
1711 * DECODE_SLICE_OK if this slice is OK
1713 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1714 const uint8_t **buf, int buf_size)
1716 AVCodecContext *avctx = s->avctx;
1717 const int field_pic = s->picture_structure != PICT_FRAME;
1721 s->resync_mb_y = -1;
1723 assert(mb_y < s->mb_height);
1725 init_get_bits(&s->gb, *buf, buf_size * 8);
1727 ff_mpeg1_clean_buffers(s);
1728 s->interlaced_dct = 0;
1730 s->qscale = get_qscale(s);
1732 if (s->qscale == 0) {
1733 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1734 return AVERROR_INVALIDDATA;
1737 /* extra slice info */
1738 while (get_bits1(&s->gb) != 0)
1739 skip_bits(&s->gb, 8);
1743 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1746 while (get_bits_left(&s->gb) > 0) {
1747 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1748 MBINCR_VLC_BITS, 2);
1750 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1751 return AVERROR_INVALIDDATA;
1756 /* otherwise, stuffing, nothing to do */
1764 if (s->mb_x >= (unsigned) s->mb_width) {
1765 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1766 return AVERROR_INVALIDDATA;
1769 if (avctx->hwaccel) {
1770 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1771 int start_code = -1;
1772 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1773 if (buf_end < *buf + buf_size)
1776 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1777 return DECODE_SLICE_ERROR;
1779 return DECODE_SLICE_OK;
1782 s->resync_mb_x = s->mb_x;
1783 s->resync_mb_y = s->mb_y = mb_y;
1785 ff_init_block_index(s);
1787 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1788 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1789 av_log(s->avctx, AV_LOG_DEBUG,
1790 "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",
1792 s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
1793 s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1794 s->pict_type == AV_PICTURE_TYPE_I ? "I" :
1795 (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
1796 (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
1797 s->progressive_sequence ? "ps" : "",
1798 s->progressive_frame ? "pf" : "",
1799 s->alternate_scan ? "alt" : "",
1800 s->top_field_first ? "top" : "",
1801 s->intra_dc_precision, s->picture_structure,
1802 s->frame_pred_frame_dct, s->concealment_motion_vectors,
1803 s->q_scale_type, s->intra_vlc_format,
1804 s->repeat_first_field, s->chroma_420_type ? "420" : "");
1810 FF_DISABLE_DEPRECATION_WARNINGS
1811 // If 1, we memcpy blocks in xvmcvideo.
1812 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1813 ff_xvmc_init_block(s); // set s->block
1814 FF_ENABLE_DEPRECATION_WARNINGS
1815 #endif /* FF_API_XVMC */
1817 if ((ret = mpeg_decode_mb(s, s->block)) < 0)
1820 // Note motion_val is normally NULL unless we want to extract the MVs.
1821 if (s->current_picture.motion_val[0] && !s->encoding) {
1822 const int wrap = s->b8_stride;
1823 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1824 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1825 int motion_x, motion_y, dir, i;
1827 for (i = 0; i < 2; i++) {
1828 for (dir = 0; dir < 2; dir++) {
1830 (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1831 motion_x = motion_y = 0;
1832 } else if (s->mv_type == MV_TYPE_16X16 ||
1833 (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1834 motion_x = s->mv[dir][0][0];
1835 motion_y = s->mv[dir][0][1];
1836 } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
1837 motion_x = s->mv[dir][i][0];
1838 motion_y = s->mv[dir][i][1];
1841 s->current_picture.motion_val[dir][xy][0] = motion_x;
1842 s->current_picture.motion_val[dir][xy][1] = motion_y;
1843 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1844 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1845 s->current_picture.ref_index [dir][b8_xy] =
1846 s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1847 assert(s->field_select[dir][i] == 0 ||
1848 s->field_select[dir][i] == 1);
1856 s->dest[1] += 16 >> s->chroma_x_shift;
1857 s->dest[2] += 16 >> s->chroma_x_shift;
1859 ff_mpv_decode_mb(s, s->block);
1861 if (++s->mb_x >= s->mb_width) {
1862 const int mb_size = 16;
1864 ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
1865 ff_mpv_report_decode_progress(s);
1868 s->mb_y += 1 << field_pic;
1870 if (s->mb_y >= s->mb_height) {
1871 int left = get_bits_left(&s->gb);
1872 int is_d10 = s->chroma_format == 2 &&
1873 s->pict_type == AV_PICTURE_TYPE_I &&
1874 avctx->profile == 0 && avctx->level == 5 &&
1875 s->intra_dc_precision == 2 &&
1876 s->q_scale_type == 1 && s->alternate_scan == 0 &&
1877 s->progressive_frame == 0
1878 /* vbv_delay == 0xBBB || 0xE10 */;
1881 (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
1882 ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
1883 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
1884 left, show_bits(&s->gb, FFMIN(left, 23)));
1885 return AVERROR_INVALIDDATA;
1890 ff_init_block_index(s);
1893 /* skip mb handling */
1894 if (s->mb_skip_run == -1) {
1895 /* read increment again */
1898 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1899 MBINCR_VLC_BITS, 2);
1901 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1902 return AVERROR_INVALIDDATA;
1906 s->mb_skip_run += 33;
1907 } else if (code == 35) {
1908 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1909 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1910 return AVERROR_INVALIDDATA;
1912 goto eos; /* end of slice */
1914 /* otherwise, stuffing, nothing to do */
1916 s->mb_skip_run += code;
1920 if (s->mb_skip_run) {
1922 if (s->pict_type == AV_PICTURE_TYPE_I) {
1923 av_log(s->avctx, AV_LOG_ERROR,
1924 "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1925 return AVERROR_INVALIDDATA;
1930 for (i = 0; i < 12; i++)
1931 s->block_last_index[i] = -1;
1932 if (s->picture_structure == PICT_FRAME)
1933 s->mv_type = MV_TYPE_16X16;
1935 s->mv_type = MV_TYPE_FIELD;
1936 if (s->pict_type == AV_PICTURE_TYPE_P) {
1937 /* if P type, zero motion vector is implied */
1938 s->mv_dir = MV_DIR_FORWARD;
1939 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1940 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1941 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1942 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1944 /* if B type, reuse previous vectors and directions */
1945 s->mv[0][0][0] = s->last_mv[0][0][0];
1946 s->mv[0][0][1] = s->last_mv[0][0][1];
1947 s->mv[1][0][0] = s->last_mv[1][0][0];
1948 s->mv[1][0][1] = s->last_mv[1][0][1];
1953 eos: // end of slice
1954 *buf += (get_bits_count(&s->gb) - 1) / 8;
1955 av_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1959 static int slice_decode_thread(AVCodecContext *c, void *arg)
1961 MpegEncContext *s = *(void **) arg;
1962 const uint8_t *buf = s->gb.buffer;
1963 int mb_y = s->start_mb_y;
1964 const int field_pic = s->picture_structure != PICT_FRAME;
1966 s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
1969 uint32_t start_code;
1972 ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
1974 av_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1975 ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
1976 s->start_mb_y, s->end_mb_y, s->er.error_count);
1978 if (c->err_recognition & AV_EF_EXPLODE)
1980 if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
1981 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1983 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
1985 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1986 s->mb_x - 1, s->mb_y,
1987 ER_AC_END | ER_DC_END | ER_MV_END);
1990 if (s->mb_y == s->end_mb_y)
1994 buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
1995 mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
1996 if (s->picture_structure == PICT_BOTTOM_FIELD)
1998 if (mb_y < 0 || mb_y >= s->end_mb_y)
1999 return AVERROR_INVALIDDATA;
2004 * Handle slice ends.
2005 * @return 1 if it seems to be the last slice
2007 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
2009 Mpeg1Context *s1 = avctx->priv_data;
2010 MpegEncContext *s = &s1->mpeg_enc_ctx;
2012 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
2015 if (s->avctx->hwaccel) {
2016 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
2017 av_log(avctx, AV_LOG_ERROR,
2018 "hardware accelerator failed to decode picture\n");
2022 FF_DISABLE_DEPRECATION_WARNINGS
2023 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
2024 ff_xvmc_field_end(s);
2025 FF_ENABLE_DEPRECATION_WARNINGS
2026 #endif /* FF_API_XVMC */
2028 /* end of slice reached */
2029 if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field) {
2032 ff_er_frame_end(&s->er);
2034 ff_mpv_frame_end(s);
2036 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
2037 int ret = av_frame_ref(pict, s->current_picture_ptr->f);
2040 ff_print_debug_info(s, s->current_picture_ptr);
2042 if (avctx->active_thread_type & FF_THREAD_FRAME)
2043 s->picture_number++;
2044 /* latency of 1 frame for I- and P-frames */
2045 /* XXX: use another variable than picture_number */
2046 if (s->last_picture_ptr) {
2047 int ret = av_frame_ref(pict, s->last_picture_ptr->f);
2050 ff_print_debug_info(s, s->last_picture_ptr);
2060 static int mpeg1_decode_sequence(AVCodecContext *avctx,
2061 const uint8_t *buf, int buf_size)
2063 Mpeg1Context *s1 = avctx->priv_data;
2064 MpegEncContext *s = &s1->mpeg_enc_ctx;
2068 init_get_bits(&s->gb, buf, buf_size * 8);
2070 width = get_bits(&s->gb, 12);
2071 height = get_bits(&s->gb, 12);
2072 if (width == 0 || height == 0) {
2073 av_log(avctx, AV_LOG_WARNING,
2074 "Invalid horizontal or vertical size value.\n");
2075 if (avctx->err_recognition & AV_EF_BITSTREAM)
2076 return AVERROR_INVALIDDATA;
2078 s->aspect_ratio_info = get_bits(&s->gb, 4);
2079 if (s->aspect_ratio_info == 0) {
2080 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
2081 if (avctx->err_recognition & AV_EF_BITSTREAM)
2082 return AVERROR_INVALIDDATA;
2084 s->frame_rate_index = get_bits(&s->gb, 4);
2085 if (s->frame_rate_index == 0 || s->frame_rate_index > 13) {
2086 av_log(avctx, AV_LOG_WARNING,
2087 "frame_rate_index %d is invalid\n", s->frame_rate_index);
2088 return AVERROR_INVALIDDATA;
2090 s->bit_rate = get_bits(&s->gb, 18) * 400;
2091 if (get_bits1(&s->gb) == 0) { /* marker */
2092 av_log(avctx, AV_LOG_ERROR, "Marker in sequence header missing\n");
2093 return AVERROR_INVALIDDATA;
2098 s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
2099 skip_bits(&s->gb, 1);
2102 if (get_bits1(&s->gb)) {
2103 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
2105 for (i = 0; i < 64; i++) {
2106 j = s->idsp.idct_permutation[i];
2107 v = ff_mpeg1_default_intra_matrix[i];
2108 s->intra_matrix[j] = v;
2109 s->chroma_intra_matrix[j] = v;
2112 if (get_bits1(&s->gb)) {
2113 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
2115 for (i = 0; i < 64; i++) {
2116 int j = s->idsp.idct_permutation[i];
2117 v = ff_mpeg1_default_non_intra_matrix[i];
2118 s->inter_matrix[j] = v;
2119 s->chroma_inter_matrix[j] = v;
2123 if (show_bits(&s->gb, 23) != 0) {
2124 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2125 return AVERROR_INVALIDDATA;
2128 /* We set MPEG-2 parameters so that it emulates MPEG-1. */
2129 s->progressive_sequence = 1;
2130 s->progressive_frame = 1;
2131 s->picture_structure = PICT_FRAME;
2132 s->frame_pred_frame_dct = 1;
2133 s->chroma_format = 1;
2135 s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
2136 s->out_format = FMT_MPEG1;
2137 if (s->flags & CODEC_FLAG_LOW_DELAY)
2140 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2141 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2142 s->avctx->rc_buffer_size, s->bit_rate);
2147 static int vcr2_init_sequence(AVCodecContext *avctx)
2149 Mpeg1Context *s1 = avctx->priv_data;
2150 MpegEncContext *s = &s1->mpeg_enc_ctx;
2153 /* start new MPEG-1 context decoding */
2154 s->out_format = FMT_MPEG1;
2155 if (s1->mpeg_enc_ctx_allocated) {
2156 ff_mpv_common_end(s);
2158 s->width = avctx->coded_width;
2159 s->height = avctx->coded_height;
2160 avctx->has_b_frames = 0; // true?
2163 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2166 if ((avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel) &&
2167 avctx->idct_algo == FF_IDCT_AUTO)
2169 if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
2170 #endif /* FF_API_XVMC */
2171 avctx->idct_algo = FF_IDCT_SIMPLE;
2173 ff_mpv_idct_init(s);
2174 if ((ret = ff_mpv_common_init(s)) < 0)
2176 s1->mpeg_enc_ctx_allocated = 1;
2178 for (i = 0; i < 64; i++) {
2179 int j = s->idsp.idct_permutation[i];
2180 v = ff_mpeg1_default_intra_matrix[i];
2181 s->intra_matrix[j] = v;
2182 s->chroma_intra_matrix[j] = v;
2184 v = ff_mpeg1_default_non_intra_matrix[i];
2185 s->inter_matrix[j] = v;
2186 s->chroma_inter_matrix[j] = v;
2189 s->progressive_sequence = 1;
2190 s->progressive_frame = 1;
2191 s->picture_structure = PICT_FRAME;
2192 s->frame_pred_frame_dct = 1;
2193 s->chroma_format = 1;
2194 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
2195 s1->save_width = s->width;
2196 s1->save_height = s->height;
2197 s1->save_progressive_seq = s->progressive_sequence;
2201 static int mpeg_decode_a53_cc(AVCodecContext *avctx,
2202 const uint8_t *p, int buf_size)
2204 Mpeg1Context *s1 = avctx->priv_data;
2206 if (buf_size >= 6 &&
2207 p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
2208 p[4] == 3 && (p[5] & 0x40)) {
2209 /* extract A53 Part 4 CC data */
2210 int cc_count = p[5] & 0x1f;
2211 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2212 av_freep(&s1->a53_caption);
2213 s1->a53_caption_size = cc_count * 3;
2214 s1->a53_caption = av_malloc(s1->a53_caption_size);
2215 if (s1->a53_caption)
2216 memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
2219 } else if (buf_size >= 11 &&
2220 p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
2221 /* extract DVD CC data */
2224 // There is a caption count field in the data, but it is often
2225 // incorect. So count the number of captions present.
2226 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2228 // Transform the DVD format into A53 Part 4 format
2230 av_freep(&s1->a53_caption);
2231 s1->a53_caption_size = cc_count * 6;
2232 s1->a53_caption = av_malloc(s1->a53_caption_size);
2233 if (s1->a53_caption) {
2234 uint8_t field1 = !!(p[4] & 0x80);
2235 uint8_t *cap = s1->a53_caption;
2237 for (i = 0; i < cc_count; i++) {
2238 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2241 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2254 static void mpeg_decode_user_data(AVCodecContext *avctx,
2255 const uint8_t *p, int buf_size)
2257 const uint8_t *buf_end = p + buf_size;
2258 Mpeg1Context *s1 = avctx->priv_data;
2260 /* we parse the DTG active format information */
2261 if (buf_end - p >= 5 &&
2262 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2270 if (buf_end - p < 1)
2273 FF_DISABLE_DEPRECATION_WARNINGS
2274 avctx->dtg_active_format = p[0] & 0x0f;
2275 FF_ENABLE_DEPRECATION_WARNINGS
2276 #endif /* FF_API_AFD */
2278 s1->afd = p[0] & 0x0f;
2280 } else if (buf_end - p >= 6 &&
2281 p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
2282 p[4] == 0x03) { // S3D_video_format_length
2283 // the 0x7F mask ignores the reserved_bit value
2284 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2286 if (S3D_video_format_type == 0x03 ||
2287 S3D_video_format_type == 0x04 ||
2288 S3D_video_format_type == 0x08 ||
2289 S3D_video_format_type == 0x23) {
2291 s1->has_stereo3d = 1;
2293 switch (S3D_video_format_type) {
2295 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
2298 s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
2301 s1->stereo3d.type = AV_STEREO3D_2D;
2304 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2308 } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
2313 static void mpeg_decode_gop(AVCodecContext *avctx,
2314 const uint8_t *buf, int buf_size)
2316 Mpeg1Context *s1 = avctx->priv_data;
2317 MpegEncContext *s = &s1->mpeg_enc_ctx;
2319 int time_code_hours, time_code_minutes;
2320 int time_code_seconds, time_code_pictures;
2323 init_get_bits(&s->gb, buf, buf_size * 8);
2325 skip_bits1(&s->gb); /* drop_frame_flag */
2327 time_code_hours = get_bits(&s->gb, 5);
2328 time_code_minutes = get_bits(&s->gb, 6);
2329 skip_bits1(&s->gb); // marker bit
2330 time_code_seconds = get_bits(&s->gb, 6);
2331 time_code_pictures = get_bits(&s->gb, 6);
2333 s1->closed_gop = get_bits1(&s->gb);
2334 /* broken_link indicate that after editing the
2335 * reference frames of the first B-Frames after GOP I-Frame
2336 * are missing (open gop) */
2337 broken_link = get_bits1(&s->gb);
2339 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2340 av_log(s->avctx, AV_LOG_DEBUG,
2341 "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
2342 time_code_hours, time_code_minutes, time_code_seconds,
2343 time_code_pictures, s1->closed_gop, broken_link);
2346 static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
2347 int *got_output, const uint8_t *buf, int buf_size)
2349 Mpeg1Context *s = avctx->priv_data;
2350 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2351 const uint8_t *buf_ptr = buf;
2352 const uint8_t *buf_end = buf + buf_size;
2353 int ret, input_size;
2354 int last_code = 0, skip_frame = 0;
2357 /* find next start code */
2358 uint32_t start_code = -1;
2359 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
2360 if (start_code > 0x1ff) {
2363 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2367 avctx->execute(avctx, slice_decode_thread,
2368 &s2->thread_context[0], NULL,
2369 s->slice_count, sizeof(void *));
2370 for (i = 0; i < s->slice_count; i++)
2371 s2->er.error_count += s2->thread_context[i]->er.error_count;
2374 ret = slice_end(avctx, picture);
2378 // FIXME: merge with the stuff in mpeg_decode_slice
2379 if (s2->last_picture_ptr || s2->low_delay)
2384 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2387 input_size = buf_end - buf_ptr;
2389 if (avctx->debug & FF_DEBUG_STARTCODE)
2390 av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %td left %d\n",
2391 start_code, buf_ptr - buf, input_size);
2393 /* prepare data for next start code */
2394 switch (start_code) {
2395 case SEQ_START_CODE:
2396 if (last_code == 0) {
2397 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2400 av_log(avctx, AV_LOG_ERROR,
2401 "ignoring SEQ_START_CODE after %X\n", last_code);
2402 if (avctx->err_recognition & AV_EF_EXPLODE)
2403 return AVERROR_INVALIDDATA;
2407 case PICTURE_START_CODE:
2408 if (s2->width <= 0 || s2->height <= 0) {
2409 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
2410 s2->width, s2->height);
2411 return AVERROR_INVALIDDATA;
2414 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
2415 !avctx->hwaccel && s->slice_count) {
2418 avctx->execute(avctx, slice_decode_thread,
2419 s2->thread_context, NULL,
2420 s->slice_count, sizeof(void *));
2421 for (i = 0; i < s->slice_count; i++)
2422 s2->er.error_count += s2->thread_context[i]->er.error_count;
2425 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2426 ret = mpeg_decode_postinit(avctx);
2428 av_log(avctx, AV_LOG_ERROR,
2429 "mpeg_decode_postinit() failure\n");
2433 /* We have a complete image: we try to decompress it. */
2434 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2437 last_code = PICTURE_START_CODE;
2439 av_log(avctx, AV_LOG_ERROR,
2440 "ignoring pic after %X\n", last_code);
2441 if (avctx->err_recognition & AV_EF_EXPLODE)
2442 return AVERROR_INVALIDDATA;
2445 case EXT_START_CODE:
2446 init_get_bits(&s2->gb, buf_ptr, input_size * 8);
2448 switch (get_bits(&s2->gb, 4)) {
2450 if (last_code == 0) {
2451 mpeg_decode_sequence_extension(s);
2453 av_log(avctx, AV_LOG_ERROR,
2454 "ignoring seq ext after %X\n", last_code);
2455 if (avctx->err_recognition & AV_EF_EXPLODE)
2456 return AVERROR_INVALIDDATA;
2460 mpeg_decode_sequence_display_extension(s);
2463 mpeg_decode_quant_matrix_extension(s2);
2466 mpeg_decode_picture_display_extension(s);
2469 if (last_code == PICTURE_START_CODE) {
2470 mpeg_decode_picture_coding_extension(s);
2472 av_log(avctx, AV_LOG_ERROR,
2473 "ignoring pic cod ext after %X\n", last_code);
2474 if (avctx->err_recognition & AV_EF_EXPLODE)
2475 return AVERROR_INVALIDDATA;
2480 case USER_START_CODE:
2481 mpeg_decode_user_data(avctx, buf_ptr, input_size);
2483 case GOP_START_CODE:
2484 if (last_code == 0) {
2485 s2->first_field = 0;
2486 mpeg_decode_gop(avctx, buf_ptr, input_size);
2489 av_log(avctx, AV_LOG_ERROR,
2490 "ignoring GOP_START_CODE after %X\n", last_code);
2491 if (avctx->err_recognition & AV_EF_EXPLODE)
2492 return AVERROR_INVALIDDATA;
2496 if (start_code >= SLICE_MIN_START_CODE &&
2497 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2498 const int field_pic = s2->picture_structure != PICT_FRAME;
2499 int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
2500 last_code = SLICE_MIN_START_CODE;
2502 if (s2->picture_structure == PICT_BOTTOM_FIELD)
2505 if (mb_y >= s2->mb_height) {
2506 av_log(s2->avctx, AV_LOG_ERROR,
2507 "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2508 return AVERROR_INVALIDDATA;
2511 if (!s2->last_picture_ptr) {
2512 /* Skip B-frames if we do not have reference frames and
2513 * GOP is not closed. */
2514 if (s2->pict_type == AV_PICTURE_TYPE_B) {
2515 if (!s->closed_gop) {
2521 if (s2->pict_type == AV_PICTURE_TYPE_I)
2523 if (!s2->next_picture_ptr) {
2524 /* Skip P-frames if we do not have a reference frame or
2525 * we have an invalid header. */
2526 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
2531 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
2532 s2->pict_type == AV_PICTURE_TYPE_B) ||
2533 (avctx->skip_frame >= AVDISCARD_NONKEY &&
2534 s2->pict_type != AV_PICTURE_TYPE_I) ||
2535 avctx->skip_frame >= AVDISCARD_ALL) {
2540 if (!s->mpeg_enc_ctx_allocated)
2543 if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2544 if (mb_y < avctx->skip_top ||
2545 mb_y >= s2->mb_height - avctx->skip_bottom)
2549 if (!s2->pict_type) {
2550 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2551 if (avctx->err_recognition & AV_EF_EXPLODE)
2552 return AVERROR_INVALIDDATA;
2556 if (s->first_slice) {
2559 if ((ret = mpeg_field_start(s2, buf, buf_size)) < 0)
2562 if (!s2->current_picture_ptr) {
2563 av_log(avctx, AV_LOG_ERROR,
2564 "current_picture not initialized\n");
2565 return AVERROR_INVALIDDATA;
2569 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2571 int threshold = (s2->mb_height * s->slice_count +
2572 s2->slice_context_count / 2) /
2573 s2->slice_context_count;
2574 if (threshold <= mb_y) {
2575 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2577 thread_context->start_mb_y = mb_y;
2578 thread_context->end_mb_y = s2->mb_height;
2579 if (s->slice_count) {
2580 s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
2581 ret = ff_update_duplicate_context(thread_context, s2);
2585 init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
2588 buf_ptr += 2; // FIXME add minimum number of bytes per slice
2590 ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2594 if (avctx->err_recognition & AV_EF_EXPLODE)
2596 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2597 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2598 s2->resync_mb_y, s2->mb_x, s2->mb_y,
2599 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
2601 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2602 s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
2603 ER_AC_END | ER_DC_END | ER_MV_END);
2612 static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
2613 int *got_output, AVPacket *avpkt)
2615 const uint8_t *buf = avpkt->data;
2616 int buf_size = avpkt->size;
2617 Mpeg1Context *s = avctx->priv_data;
2618 AVFrame *picture = data;
2619 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2620 av_dlog(avctx, "fill_buffer\n");
2622 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2623 /* special case for last picture */
2624 if (s2->low_delay == 0 && s2->next_picture_ptr) {
2625 int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
2629 s2->next_picture_ptr = NULL;
2636 if (s2->flags & CODEC_FLAG_TRUNCATED) {
2637 int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
2640 if (ff_combine_frame(&s2->parse_context, next,
2641 (const uint8_t **) &buf, &buf_size) < 0)
2645 if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
2646 vcr2_init_sequence(avctx);
2650 if (avctx->extradata && !s->extradata_decoded) {
2651 int ret = decode_chunks(avctx, picture, got_output,
2652 avctx->extradata, avctx->extradata_size);
2653 s->extradata_decoded = 1;
2654 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
2658 return decode_chunks(avctx, picture, got_output, buf, buf_size);
2661 static void flush(AVCodecContext *avctx)
2663 Mpeg1Context *s = avctx->priv_data;
2668 ff_mpeg_flush(avctx);
2671 static av_cold int mpeg_decode_end(AVCodecContext *avctx)
2673 Mpeg1Context *s = avctx->priv_data;
2675 if (s->mpeg_enc_ctx_allocated)
2676 ff_mpv_common_end(&s->mpeg_enc_ctx);
2677 av_freep(&s->a53_caption);
2681 static const AVProfile mpeg2_video_profiles[] = {
2682 { FF_PROFILE_MPEG2_422, "4:2:2" },
2683 { FF_PROFILE_MPEG2_HIGH, "High" },
2684 { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2685 { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2686 { FF_PROFILE_MPEG2_MAIN, "Main" },
2687 { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2688 { FF_PROFILE_RESERVED, "Reserved" },
2689 { FF_PROFILE_RESERVED, "Reserved" },
2690 { FF_PROFILE_UNKNOWN },
2693 AVCodec ff_mpeg1video_decoder = {
2694 .name = "mpeg1video",
2695 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2696 .type = AVMEDIA_TYPE_VIDEO,
2697 .id = AV_CODEC_ID_MPEG1VIDEO,
2698 .priv_data_size = sizeof(Mpeg1Context),
2699 .init = mpeg_decode_init,
2700 .close = mpeg_decode_end,
2701 .decode = mpeg_decode_frame,
2702 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2703 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2704 CODEC_CAP_SLICE_THREADS,
2706 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2709 AVCodec ff_mpeg2video_decoder = {
2710 .name = "mpeg2video",
2711 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2712 .type = AVMEDIA_TYPE_VIDEO,
2713 .id = AV_CODEC_ID_MPEG2VIDEO,
2714 .priv_data_size = sizeof(Mpeg1Context),
2715 .init = mpeg_decode_init,
2716 .close = mpeg_decode_end,
2717 .decode = mpeg_decode_frame,
2718 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2719 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2720 CODEC_CAP_SLICE_THREADS,
2722 .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2726 #if CONFIG_MPEG_XVMC_DECODER
2727 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2729 if (avctx->active_thread_type & FF_THREAD_SLICE)
2731 if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2733 if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
2734 av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2736 mpeg_decode_init(avctx);
2738 avctx->pix_fmt = AV_PIX_FMT_XVMC_MPEG2_IDCT;
2739 avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2744 AVCodec ff_mpeg_xvmc_decoder = {
2745 .name = "mpegvideo_xvmc",
2746 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2747 .type = AVMEDIA_TYPE_VIDEO,
2748 .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2749 .priv_data_size = sizeof(Mpeg1Context),
2750 .init = mpeg_mc_decode_init,
2751 .close = mpeg_decode_end,
2752 .decode = mpeg_decode_frame,
2753 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2754 CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2759 #endif /* FF_API_XVMC */