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 */
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? */
66 int extradata_decoded;
69 #define MB_TYPE_ZERO_MV 0x20000000
71 static const uint32_t ptype2mb_type[7] = {
73 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
75 MB_TYPE_L0 | MB_TYPE_CBP,
76 MB_TYPE_QUANT | MB_TYPE_INTRA,
77 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
78 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
81 static const uint32_t btype2mb_type[11] = {
84 MB_TYPE_L1 | MB_TYPE_CBP,
86 MB_TYPE_L0 | MB_TYPE_CBP,
88 MB_TYPE_L0L1 | MB_TYPE_CBP,
89 MB_TYPE_QUANT | MB_TYPE_INTRA,
90 MB_TYPE_QUANT | MB_TYPE_L1 | MB_TYPE_CBP,
91 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
92 MB_TYPE_QUANT | MB_TYPE_L0L1 | MB_TYPE_CBP,
95 static const uint8_t non_linear_qscale[32] = {
96 0, 1, 2, 3, 4, 5, 6, 7,
97 8, 10, 12, 14, 16, 18, 20, 22,
98 24, 28, 32, 36, 40, 44, 48, 52,
99 56, 64, 72, 80, 88, 96, 104, 112,
102 /* as H.263, but only 17 codes */
103 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
105 int code, sign, val, shift;
107 code = get_vlc2(&s->gb, ff_mv_vlc.table, MV_VLC_BITS, 2);
113 sign = get_bits1(&s->gb);
117 val = (val - 1) << shift;
118 val |= get_bits(&s->gb, shift);
125 /* modulo decoding */
126 return sign_extend(val, 5 + shift);
129 #define check_scantable_index(ctx, x) \
132 av_log(ctx->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
133 ctx->mb_x, ctx->mb_y); \
134 return AVERROR_INVALIDDATA; \
138 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
139 int16_t *block, int n)
141 int level, dc, diff, i, j, run;
143 RLTable *rl = &ff_rl_mpeg1;
144 uint8_t *const scantable = s->intra_scantable.permutated;
145 const uint16_t *quant_matrix = s->intra_matrix;
146 const int qscale = s->qscale;
149 component = (n <= 3 ? 0 : n - 4 + 1);
150 diff = decode_dc(&s->gb, component);
153 dc = s->last_dc[component];
155 s->last_dc[component] = dc;
156 block[0] = dc * quant_matrix[0];
157 av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
160 OPEN_READER(re, &s->gb);
161 UPDATE_CACHE(re, &s->gb);
162 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
165 /* now quantify & encode AC coefficients */
167 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
172 check_scantable_index(s, i);
174 level = (level * qscale * quant_matrix[j]) >> 4;
175 level = (level - 1) | 1;
176 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
177 SHOW_SBITS(re, &s->gb, 1);
178 SKIP_BITS(re, &s->gb, 1);
181 run = SHOW_UBITS(re, &s->gb, 6) + 1;
182 LAST_SKIP_BITS(re, &s->gb, 6);
183 UPDATE_CACHE(re, &s->gb);
184 level = SHOW_SBITS(re, &s->gb, 8);
185 SKIP_BITS(re, &s->gb, 8);
187 level = SHOW_UBITS(re, &s->gb, 8) - 256;
188 SKIP_BITS(re, &s->gb, 8);
189 } else if (level == 0) {
190 level = SHOW_UBITS(re, &s->gb, 8);
191 SKIP_BITS(re, &s->gb, 8);
194 check_scantable_index(s, i);
198 level = (level * qscale * quant_matrix[j]) >> 4;
199 level = (level - 1) | 1;
202 level = (level * qscale * quant_matrix[j]) >> 4;
203 level = (level - 1) | 1;
208 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
211 UPDATE_CACHE(re, &s->gb);
214 LAST_SKIP_BITS(re, &s->gb, 2);
215 CLOSE_READER(re, &s->gb);
217 s->block_last_index[n] = i;
221 int ff_mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
223 return mpeg1_decode_block_intra(s, block, n);
226 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
227 int16_t *block, int n)
229 int level, i, j, run;
230 RLTable *rl = &ff_rl_mpeg1;
231 uint8_t *const scantable = s->intra_scantable.permutated;
232 const uint16_t *quant_matrix = s->inter_matrix;
233 const int qscale = s->qscale;
236 OPEN_READER(re, &s->gb);
238 // special case for first coefficient, no need to add second VLC table
239 UPDATE_CACHE(re, &s->gb);
240 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
241 level = (3 * qscale * quant_matrix[0]) >> 5;
242 level = (level - 1) | 1;
243 if (GET_CACHE(re, &s->gb) & 0x40000000)
247 SKIP_BITS(re, &s->gb, 2);
248 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
251 /* now quantify & encode AC coefficients */
253 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
258 check_scantable_index(s, i);
260 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
261 level = (level - 1) | 1;
262 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
263 SHOW_SBITS(re, &s->gb, 1);
264 SKIP_BITS(re, &s->gb, 1);
267 run = SHOW_UBITS(re, &s->gb, 6) + 1;
268 LAST_SKIP_BITS(re, &s->gb, 6);
269 UPDATE_CACHE(re, &s->gb);
270 level = SHOW_SBITS(re, &s->gb, 8);
271 SKIP_BITS(re, &s->gb, 8);
273 level = SHOW_UBITS(re, &s->gb, 8) - 256;
274 SKIP_BITS(re, &s->gb, 8);
275 } else if (level == 0) {
276 level = SHOW_UBITS(re, &s->gb, 8);
277 SKIP_BITS(re, &s->gb, 8);
280 check_scantable_index(s, i);
284 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
285 level = (level - 1) | 1;
288 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
289 level = (level - 1) | 1;
294 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
296 UPDATE_CACHE(re, &s->gb);
299 LAST_SKIP_BITS(re, &s->gb, 2);
300 CLOSE_READER(re, &s->gb);
302 s->block_last_index[n] = i;
307 * Note: this function can read out of range and crash for corrupt streams.
308 * Changing this would eat up any speed benefits it has.
309 * Do not use "fast" flag if you need the code to be robust.
311 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s,
312 int16_t *block, int n)
314 int level, i, j, run;
315 RLTable *rl = &ff_rl_mpeg1;
316 uint8_t *const scantable = s->intra_scantable.permutated;
317 const int qscale = s->qscale;
320 OPEN_READER(re, &s->gb);
322 // Special case for first coefficient, no need to add second VLC table.
323 UPDATE_CACHE(re, &s->gb);
324 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
325 level = (3 * qscale) >> 1;
326 level = (level - 1) | 1;
327 if (GET_CACHE(re, &s->gb) & 0x40000000)
331 SKIP_BITS(re, &s->gb, 2);
332 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
336 /* now quantify & encode AC coefficients */
338 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
343 check_scantable_index(s, i);
345 level = ((level * 2 + 1) * qscale) >> 1;
346 level = (level - 1) | 1;
347 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
348 SHOW_SBITS(re, &s->gb, 1);
349 SKIP_BITS(re, &s->gb, 1);
352 run = SHOW_UBITS(re, &s->gb, 6) + 1;
353 LAST_SKIP_BITS(re, &s->gb, 6);
354 UPDATE_CACHE(re, &s->gb);
355 level = SHOW_SBITS(re, &s->gb, 8);
356 SKIP_BITS(re, &s->gb, 8);
358 level = SHOW_UBITS(re, &s->gb, 8) - 256;
359 SKIP_BITS(re, &s->gb, 8);
360 } else if (level == 0) {
361 level = SHOW_UBITS(re, &s->gb, 8);
362 SKIP_BITS(re, &s->gb, 8);
365 check_scantable_index(s, i);
369 level = ((level * 2 + 1) * qscale) >> 1;
370 level = (level - 1) | 1;
373 level = ((level * 2 + 1) * qscale) >> 1;
374 level = (level - 1) | 1;
379 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
381 UPDATE_CACHE(re, &s->gb);
384 LAST_SKIP_BITS(re, &s->gb, 2);
385 CLOSE_READER(re, &s->gb);
387 s->block_last_index[n] = i;
391 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
392 int16_t *block, int n)
394 int level, i, j, run;
395 RLTable *rl = &ff_rl_mpeg1;
396 uint8_t *const scantable = s->intra_scantable.permutated;
397 const uint16_t *quant_matrix;
398 const int qscale = s->qscale;
404 OPEN_READER(re, &s->gb);
407 quant_matrix = s->inter_matrix;
409 quant_matrix = s->chroma_inter_matrix;
411 // Special case for first coefficient, no need to add second VLC table.
412 UPDATE_CACHE(re, &s->gb);
413 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
414 level = (3 * qscale * quant_matrix[0]) >> 5;
415 if (GET_CACHE(re, &s->gb) & 0x40000000)
420 SKIP_BITS(re, &s->gb, 2);
421 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
425 /* now quantify & encode AC coefficients */
427 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
432 check_scantable_index(s, i);
434 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
435 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
436 SHOW_SBITS(re, &s->gb, 1);
437 SKIP_BITS(re, &s->gb, 1);
440 run = SHOW_UBITS(re, &s->gb, 6) + 1;
441 LAST_SKIP_BITS(re, &s->gb, 6);
442 UPDATE_CACHE(re, &s->gb);
443 level = SHOW_SBITS(re, &s->gb, 12);
444 SKIP_BITS(re, &s->gb, 12);
447 check_scantable_index(s, i);
450 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
453 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
459 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
461 UPDATE_CACHE(re, &s->gb);
464 LAST_SKIP_BITS(re, &s->gb, 2);
465 CLOSE_READER(re, &s->gb);
467 block[63] ^= (mismatch & 1);
469 s->block_last_index[n] = i;
474 * Note: this function can read out of range and crash for corrupt streams.
475 * Changing this would eat up any speed benefits it has.
476 * Do not use "fast" flag if you need the code to be robust.
478 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
479 int16_t *block, int n)
481 int level, i, j, run;
482 RLTable *rl = &ff_rl_mpeg1;
483 uint8_t *const scantable = s->intra_scantable.permutated;
484 const int qscale = s->qscale;
485 OPEN_READER(re, &s->gb);
488 // special case for first coefficient, no need to add second VLC table
489 UPDATE_CACHE(re, &s->gb);
490 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
491 level = (3 * qscale) >> 1;
492 if (GET_CACHE(re, &s->gb) & 0x40000000)
496 SKIP_BITS(re, &s->gb, 2);
497 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
501 /* now quantify & encode AC coefficients */
503 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
508 level = ((level * 2 + 1) * qscale) >> 1;
509 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
510 SHOW_SBITS(re, &s->gb, 1);
511 SKIP_BITS(re, &s->gb, 1);
514 run = SHOW_UBITS(re, &s->gb, 6) + 1;
515 LAST_SKIP_BITS(re, &s->gb, 6);
516 UPDATE_CACHE(re, &s->gb);
517 level = SHOW_SBITS(re, &s->gb, 12);
518 SKIP_BITS(re, &s->gb, 12);
523 level = ((-level * 2 + 1) * qscale) >> 1;
526 level = ((level * 2 + 1) * qscale) >> 1;
531 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF || i > 63)
534 UPDATE_CACHE(re, &s->gb);
537 LAST_SKIP_BITS(re, &s->gb, 2);
538 CLOSE_READER(re, &s->gb);
539 s->block_last_index[n] = i;
543 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
544 int16_t *block, int n)
546 int level, dc, diff, i, j, run;
549 uint8_t *const scantable = s->intra_scantable.permutated;
550 const uint16_t *quant_matrix;
551 const int qscale = s->qscale;
556 quant_matrix = s->intra_matrix;
559 quant_matrix = s->chroma_intra_matrix;
560 component = (n & 1) + 1;
562 diff = decode_dc(&s->gb, component);
565 dc = s->last_dc[component];
567 s->last_dc[component] = dc;
568 block[0] = dc << (3 - s->intra_dc_precision);
569 av_dlog(s->avctx, "dc=%d\n", block[0]);
570 mismatch = block[0] ^ 1;
572 if (s->intra_vlc_format)
578 OPEN_READER(re, &s->gb);
579 /* now quantify & encode AC coefficients */
581 UPDATE_CACHE(re, &s->gb);
582 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
587 } else if (level != 0) {
589 check_scantable_index(s, i);
591 level = (level * qscale * quant_matrix[j]) >> 4;
592 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
593 SHOW_SBITS(re, &s->gb, 1);
594 LAST_SKIP_BITS(re, &s->gb, 1);
597 run = SHOW_UBITS(re, &s->gb, 6) + 1;
598 LAST_SKIP_BITS(re, &s->gb, 6);
599 UPDATE_CACHE(re, &s->gb);
600 level = SHOW_SBITS(re, &s->gb, 12);
601 SKIP_BITS(re, &s->gb, 12);
603 check_scantable_index(s, i);
606 level = (-level * qscale * quant_matrix[j]) >> 4;
609 level = (level * qscale * quant_matrix[j]) >> 4;
616 CLOSE_READER(re, &s->gb);
618 block[63] ^= mismatch & 1;
620 s->block_last_index[n] = i;
625 * Note: this function can read out of range and crash for corrupt streams.
626 * Changing this would eat up any speed benefits it has.
627 * Do not use "fast" flag if you need the code to be robust.
629 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
630 int16_t *block, int n)
632 int level, dc, diff, i, j, run;
635 uint8_t *const scantable = s->intra_scantable.permutated;
636 const uint16_t *quant_matrix;
637 const int qscale = s->qscale;
641 quant_matrix = s->intra_matrix;
644 quant_matrix = s->chroma_intra_matrix;
645 component = (n & 1) + 1;
647 diff = decode_dc(&s->gb, component);
650 dc = s->last_dc[component];
652 s->last_dc[component] = dc;
653 block[0] = dc << (3 - s->intra_dc_precision);
655 if (s->intra_vlc_format)
661 OPEN_READER(re, &s->gb);
662 /* now quantify & encode AC coefficients */
664 UPDATE_CACHE(re, &s->gb);
665 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
668 if (level >= 64 || i > 63) {
670 } else if (level != 0) {
673 level = (level * qscale * quant_matrix[j]) >> 4;
674 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
675 SHOW_SBITS(re, &s->gb, 1);
676 LAST_SKIP_BITS(re, &s->gb, 1);
679 run = SHOW_UBITS(re, &s->gb, 6) + 1;
680 LAST_SKIP_BITS(re, &s->gb, 6);
681 UPDATE_CACHE(re, &s->gb);
682 level = SHOW_SBITS(re, &s->gb, 12);
683 SKIP_BITS(re, &s->gb, 12);
687 level = (-level * qscale * quant_matrix[j]) >> 4;
690 level = (level * qscale * quant_matrix[j]) >> 4;
696 CLOSE_READER(re, &s->gb);
699 s->block_last_index[n] = i;
703 /******************************************/
706 static inline int get_dmv(MpegEncContext *s)
708 if (get_bits1(&s->gb))
709 return 1 - (get_bits1(&s->gb) << 1);
714 static inline int get_qscale(MpegEncContext *s)
716 int qscale = get_bits(&s->gb, 5);
718 return non_linear_qscale[qscale];
724 /* motion type (for MPEG-2) */
730 static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
732 int i, j, k, cbp, val, mb_type, motion_type;
733 const int mb_block_count = 4 + (1 << s->chroma_format);
735 av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
737 av_assert2(s->mb_skipped == 0);
739 if (s->mb_skip_run-- != 0) {
740 if (s->pict_type == AV_PICTURE_TYPE_P) {
742 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
743 MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
748 mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
750 // FIXME not sure if this is allowed in MPEG at all
751 mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
752 if (IS_INTRA(mb_type)) {
753 av_log(s->avctx, AV_LOG_ERROR, "skip with previntra\n");
756 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
757 mb_type | MB_TYPE_SKIP;
759 if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
766 switch (s->pict_type) {
768 case AV_PICTURE_TYPE_I:
769 if (get_bits1(&s->gb) == 0) {
770 if (get_bits1(&s->gb) == 0) {
771 av_log(s->avctx, AV_LOG_ERROR,
772 "invalid mb type in I Frame at %d %d\n",
776 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
778 mb_type = MB_TYPE_INTRA;
781 case AV_PICTURE_TYPE_P:
782 mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
784 av_log(s->avctx, AV_LOG_ERROR,
785 "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
788 mb_type = ptype2mb_type[mb_type];
790 case AV_PICTURE_TYPE_B:
791 mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
793 av_log(s->avctx, AV_LOG_ERROR,
794 "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
797 mb_type = btype2mb_type[mb_type];
800 av_dlog(s->avctx, "mb_type=%x\n", mb_type);
801 // motion_type = 0; /* avoid warning */
802 if (IS_INTRA(mb_type)) {
803 s->bdsp.clear_blocks(s->block[0]);
805 if (!s->chroma_y_shift)
806 s->bdsp.clear_blocks(s->block[6]);
808 /* compute DCT type */
809 // FIXME: add an interlaced_dct coded var?
810 if (s->picture_structure == PICT_FRAME &&
811 !s->frame_pred_frame_dct)
812 s->interlaced_dct = get_bits1(&s->gb);
814 if (IS_QUANT(mb_type))
815 s->qscale = get_qscale(s);
817 if (s->concealment_motion_vectors) {
818 /* just parse them */
819 if (s->picture_structure != PICT_FRAME)
820 skip_bits1(&s->gb); /* field select */
823 s->last_mv[0][0][0] =
824 s->last_mv[0][1][0] = mpeg_decode_motion(s, s->mpeg_f_code[0][0],
825 s->last_mv[0][0][0]);
827 s->last_mv[0][0][1] =
828 s->last_mv[0][1][1] = mpeg_decode_motion(s, s->mpeg_f_code[0][1],
829 s->last_mv[0][0][1]);
831 skip_bits1(&s->gb); /* marker */
833 /* reset mv prediction */
834 memset(s->last_mv, 0, sizeof(s->last_mv));
837 // if 1, we memcpy blocks in xvmcvideo
838 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
839 ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
841 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
842 if (s->flags2 & CODEC_FLAG2_FAST) {
843 for (i = 0; i < 6; i++)
844 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
846 for (i = 0; i < mb_block_count; i++)
847 if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
851 for (i = 0; i < 6; i++)
852 if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
856 if (mb_type & MB_TYPE_ZERO_MV) {
857 av_assert2(mb_type & MB_TYPE_CBP);
859 s->mv_dir = MV_DIR_FORWARD;
860 if (s->picture_structure == PICT_FRAME) {
861 if (s->picture_structure == PICT_FRAME
862 && !s->frame_pred_frame_dct)
863 s->interlaced_dct = get_bits1(&s->gb);
864 s->mv_type = MV_TYPE_16X16;
866 s->mv_type = MV_TYPE_FIELD;
867 mb_type |= MB_TYPE_INTERLACED;
868 s->field_select[0][0] = s->picture_structure - 1;
871 if (IS_QUANT(mb_type))
872 s->qscale = get_qscale(s);
874 s->last_mv[0][0][0] = 0;
875 s->last_mv[0][0][1] = 0;
876 s->last_mv[0][1][0] = 0;
877 s->last_mv[0][1][1] = 0;
881 av_assert2(mb_type & MB_TYPE_L0L1);
882 // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
883 /* get additional motion vector type */
884 if (s->picture_structure == PICT_FRAME && s->frame_pred_frame_dct) {
885 motion_type = MT_FRAME;
887 motion_type = get_bits(&s->gb, 2);
888 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
889 s->interlaced_dct = get_bits1(&s->gb);
892 if (IS_QUANT(mb_type))
893 s->qscale = get_qscale(s);
896 s->mv_dir = (mb_type >> 13) & 3;
897 av_dlog(s->avctx, "motion_type=%d\n", motion_type);
898 switch (motion_type) {
899 case MT_FRAME: /* or MT_16X8 */
900 if (s->picture_structure == PICT_FRAME) {
901 mb_type |= MB_TYPE_16x16;
902 s->mv_type = MV_TYPE_16X16;
903 for (i = 0; i < 2; i++) {
904 if (USES_LIST(mb_type, i)) {
907 s->last_mv[i][0][0] =
908 s->last_mv[i][1][0] =
909 mpeg_decode_motion(s, s->mpeg_f_code[i][0],
910 s->last_mv[i][0][0]);
912 s->last_mv[i][0][1] =
913 s->last_mv[i][1][1] =
914 mpeg_decode_motion(s, s->mpeg_f_code[i][1],
915 s->last_mv[i][0][1]);
916 /* full_pel: only for MPEG-1 */
917 if (s->full_pel[i]) {
918 s->mv[i][0][0] <<= 1;
919 s->mv[i][0][1] <<= 1;
924 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
925 s->mv_type = MV_TYPE_16X8;
926 for (i = 0; i < 2; i++) {
927 if (USES_LIST(mb_type, i)) {
929 for (j = 0; j < 2; j++) {
930 s->field_select[i][j] = get_bits1(&s->gb);
931 for (k = 0; k < 2; k++) {
932 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
933 s->last_mv[i][j][k]);
934 s->last_mv[i][j][k] = val;
935 s->mv[i][j][k] = val;
943 s->mv_type = MV_TYPE_FIELD;
944 if (s->picture_structure == PICT_FRAME) {
945 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
946 for (i = 0; i < 2; i++) {
947 if (USES_LIST(mb_type, i)) {
948 for (j = 0; j < 2; j++) {
949 s->field_select[i][j] = get_bits1(&s->gb);
950 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
951 s->last_mv[i][j][0]);
952 s->last_mv[i][j][0] = val;
953 s->mv[i][j][0] = val;
954 av_dlog(s->avctx, "fmx=%d\n", val);
955 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
956 s->last_mv[i][j][1] >> 1);
957 s->last_mv[i][j][1] = val << 1;
958 s->mv[i][j][1] = val;
959 av_dlog(s->avctx, "fmy=%d\n", val);
964 av_assert0(!s->progressive_sequence);
965 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
966 for (i = 0; i < 2; i++) {
967 if (USES_LIST(mb_type, i)) {
968 s->field_select[i][0] = get_bits1(&s->gb);
969 for (k = 0; k < 2; k++) {
970 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
971 s->last_mv[i][0][k]);
972 s->last_mv[i][0][k] = val;
973 s->last_mv[i][1][k] = val;
974 s->mv[i][0][k] = val;
981 if (s->progressive_sequence){
982 av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
985 s->mv_type = MV_TYPE_DMV;
986 for (i = 0; i < 2; i++) {
987 if (USES_LIST(mb_type, i)) {
988 int dmx, dmy, mx, my, m;
989 const int my_shift = s->picture_structure == PICT_FRAME;
991 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
992 s->last_mv[i][0][0]);
993 s->last_mv[i][0][0] = mx;
994 s->last_mv[i][1][0] = mx;
996 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
997 s->last_mv[i][0][1] >> my_shift);
1001 s->last_mv[i][0][1] = my << my_shift;
1002 s->last_mv[i][1][1] = my << my_shift;
1004 s->mv[i][0][0] = mx;
1005 s->mv[i][0][1] = my;
1006 s->mv[i][1][0] = mx; // not used
1007 s->mv[i][1][1] = my; // not used
1009 if (s->picture_structure == PICT_FRAME) {
1010 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
1012 // m = 1 + 2 * s->top_field_first;
1013 m = s->top_field_first ? 1 : 3;
1015 /* top -> top pred */
1016 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1017 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1019 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1020 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1022 mb_type |= MB_TYPE_16x16;
1024 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1025 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1026 if (s->picture_structure == PICT_TOP_FIELD)
1035 av_log(s->avctx, AV_LOG_ERROR,
1036 "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1042 if (HAS_CBP(mb_type)) {
1043 s->bdsp.clear_blocks(s->block[0]);
1045 cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1046 if (mb_block_count > 6) {
1047 cbp <<= mb_block_count - 6;
1048 cbp |= get_bits(&s->gb, mb_block_count - 6);
1049 s->bdsp.clear_blocks(s->block[6]);
1052 av_log(s->avctx, AV_LOG_ERROR,
1053 "invalid cbp %d at %d %d\n", cbp, s->mb_x, s->mb_y);
1057 // if 1, we memcpy blocks in xvmcvideo
1058 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
1059 ff_xvmc_pack_pblocks(s, cbp);
1061 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1062 if (s->flags2 & CODEC_FLAG2_FAST) {
1063 for (i = 0; i < 6; i++) {
1065 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
1067 s->block_last_index[i] = -1;
1071 cbp <<= 12 - mb_block_count;
1073 for (i = 0; i < mb_block_count; i++) {
1074 if (cbp & (1 << 11)) {
1075 if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
1078 s->block_last_index[i] = -1;
1084 if (s->flags2 & CODEC_FLAG2_FAST) {
1085 for (i = 0; i < 6; i++) {
1087 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
1089 s->block_last_index[i] = -1;
1093 for (i = 0; i < 6; i++) {
1095 if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
1098 s->block_last_index[i] = -1;
1105 for (i = 0; i < 12; i++)
1106 s->block_last_index[i] = -1;
1110 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
1115 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1117 Mpeg1Context *s = avctx->priv_data;
1118 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1121 /* we need some permutation to store matrices,
1122 * until MPV_common_init() sets the real permutation. */
1123 for (i = 0; i < 64; i++)
1124 s2->idsp.idct_permutation[i] = i;
1126 ff_MPV_decode_defaults(s2);
1128 s->mpeg_enc_ctx.avctx = avctx;
1129 s->mpeg_enc_ctx.flags = avctx->flags;
1130 s->mpeg_enc_ctx.flags2 = avctx->flags2;
1131 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1132 ff_mpeg12_init_vlcs();
1134 s->mpeg_enc_ctx_allocated = 0;
1135 s->mpeg_enc_ctx.picture_number = 0;
1136 s->repeat_field = 0;
1137 s->mpeg_enc_ctx.codec_id = avctx->codec->id;
1138 avctx->color_range = AVCOL_RANGE_MPEG;
1139 if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
1140 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1142 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1146 static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
1147 const AVCodecContext *avctx_from)
1149 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1150 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1153 if (avctx == avctx_from ||
1154 !ctx_from->mpeg_enc_ctx_allocated ||
1155 !s1->context_initialized)
1158 err = ff_mpeg_update_thread_context(avctx, avctx_from);
1162 if (!ctx->mpeg_enc_ctx_allocated)
1163 memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
1165 if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
1166 s->picture_number++;
1171 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1172 const uint8_t *new_perm)
1174 uint16_t temp_matrix[64];
1177 memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
1179 for (i = 0; i < 64; i++)
1180 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1183 static const enum AVPixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
1184 #if CONFIG_MPEG1_XVMC_HWACCEL
1187 #if CONFIG_MPEG1_VDPAU_HWACCEL
1188 AV_PIX_FMT_VDPAU_MPEG1,
1195 static const enum AVPixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
1196 #if CONFIG_MPEG2_XVMC_HWACCEL
1199 #if CONFIG_MPEG2_VDPAU_HWACCEL
1200 AV_PIX_FMT_VDPAU_MPEG2,
1203 #if CONFIG_MPEG2_DXVA2_HWACCEL
1204 AV_PIX_FMT_DXVA2_VLD,
1206 #if CONFIG_MPEG2_VAAPI_HWACCEL
1207 AV_PIX_FMT_VAAPI_VLD,
1213 static inline int uses_vdpau(AVCodecContext *avctx) {
1214 return avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == AV_PIX_FMT_VDPAU_MPEG2;
1217 static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
1219 Mpeg1Context *s1 = avctx->priv_data;
1220 MpegEncContext *s = &s1->mpeg_enc_ctx;
1222 if (s->chroma_format < 2)
1223 return ff_thread_get_format(avctx,
1224 avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO ?
1225 mpeg1_hwaccel_pixfmt_list_420 :
1226 mpeg2_hwaccel_pixfmt_list_420);
1227 else if (s->chroma_format == 2)
1228 return AV_PIX_FMT_YUV422P;
1230 return AV_PIX_FMT_YUV444P;
1233 static void setup_hwaccel_for_pixfmt(AVCodecContext *avctx)
1235 // until then pix_fmt may be changed right after codec init
1236 if (avctx->hwaccel || uses_vdpau(avctx))
1237 if (avctx->idct_algo == FF_IDCT_AUTO)
1238 avctx->idct_algo = FF_IDCT_SIMPLE;
1240 if (avctx->hwaccel && avctx->pix_fmt == AV_PIX_FMT_XVMC) {
1241 Mpeg1Context *s1 = avctx->priv_data;
1242 MpegEncContext *s = &s1->mpeg_enc_ctx;
1244 s->pack_pblocks = 1;
1246 avctx->xvmc_acceleration = 2;
1247 #endif /* FF_API_XVMC */
1251 /* Call this function when we know all parameters.
1252 * It may be called in different places for MPEG-1 and MPEG-2. */
1253 static int mpeg_decode_postinit(AVCodecContext *avctx)
1255 Mpeg1Context *s1 = avctx->priv_data;
1256 MpegEncContext *s = &s1->mpeg_enc_ctx;
1257 uint8_t old_permutation[64];
1260 if ((s1->mpeg_enc_ctx_allocated == 0) ||
1261 avctx->coded_width != s->width ||
1262 avctx->coded_height != s->height ||
1263 s1->save_width != s->width ||
1264 s1->save_height != s->height ||
1265 s1->save_aspect_info != s->aspect_ratio_info ||
1266 (s1->save_progressive_seq != s->progressive_sequence && FFALIGN(s->height, 16) != FFALIGN(s->height, 32)) ||
1268 if (s1->mpeg_enc_ctx_allocated) {
1269 ParseContext pc = s->parse_context;
1270 s->parse_context.buffer = 0;
1271 ff_MPV_common_end(s);
1272 s->parse_context = pc;
1273 s1->mpeg_enc_ctx_allocated = 0;
1276 if ((s->width == 0) || (s->height == 0))
1279 ret = ff_set_dimensions(avctx, s->width, s->height);
1283 if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->bit_rate) {
1284 avctx->rc_max_rate = s->bit_rate;
1285 } else if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO && s->bit_rate &&
1286 (s->bit_rate != 0x3FFFF*400 || s->vbv_delay != 0xFFFF)) {
1287 avctx->bit_rate = s->bit_rate;
1289 s1->save_aspect_info = s->aspect_ratio_info;
1290 s1->save_width = s->width;
1291 s1->save_height = s->height;
1292 s1->save_progressive_seq = s->progressive_sequence;
1294 /* low_delay may be forced, in this case we will have B-frames
1295 * that behave like P-frames. */
1296 avctx->has_b_frames = !s->low_delay;
1298 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1300 avctx->time_base.den = ff_mpeg12_frame_rate_tab[s->frame_rate_index].num;
1301 avctx->time_base.num = ff_mpeg12_frame_rate_tab[s->frame_rate_index].den;
1303 avctx->sample_aspect_ratio = av_d2q(1.0 / ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1304 avctx->ticks_per_frame = 1;
1307 av_reduce(&s->avctx->time_base.den,
1308 &s->avctx->time_base.num,
1309 ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num * 2,
1310 ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1312 avctx->ticks_per_frame = 2;
1314 if (s->aspect_ratio_info > 1) {
1316 av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1317 (AVRational) { s1->pan_scan.width,
1318 s1->pan_scan.height }),
1319 (AVRational) { s->width, s->height });
1321 /* We ignore the spec here and guess a bit as reality does not
1322 * match the spec, see for example res_change_ffmpeg_aspect.ts
1323 * and sequence-display-aspect.mpg.
1324 * issue1613, 621, 562 */
1325 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1326 (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
1327 av_cmp_q(dar, (AVRational) { 16, 9 }))) {
1328 s->avctx->sample_aspect_ratio =
1329 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1330 (AVRational) { s->width, s->height });
1332 s->avctx->sample_aspect_ratio =
1333 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1334 (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
1335 // issue1613 4/3 16/9 -> 16/9
1336 // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1337 // widescreen-issue562.mpg 4/3 16/9 -> 16/9
1338 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1339 av_dlog(avctx, "A %d/%d\n",
1340 ff_mpeg2_aspect[s->aspect_ratio_info].num,
1341 ff_mpeg2_aspect[s->aspect_ratio_info].den);
1342 av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
1343 s->avctx->sample_aspect_ratio.den);
1346 s->avctx->sample_aspect_ratio =
1347 ff_mpeg2_aspect[s->aspect_ratio_info];
1351 ff_set_sar(s->avctx, s->avctx->sample_aspect_ratio);
1353 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1354 setup_hwaccel_for_pixfmt(avctx);
1356 /* Quantization matrices may need reordering
1357 * if DCT permutation is changed. */
1358 memcpy(old_permutation, s->idsp.idct_permutation, 64 * sizeof(uint8_t));
1360 if (ff_MPV_common_init(s) < 0)
1363 quant_matrix_rebuild(s->intra_matrix, old_permutation, s->idsp.idct_permutation);
1364 quant_matrix_rebuild(s->inter_matrix, old_permutation, s->idsp.idct_permutation);
1365 quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->idsp.idct_permutation);
1366 quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->idsp.idct_permutation);
1368 s1->mpeg_enc_ctx_allocated = 1;
1373 static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
1376 Mpeg1Context *s1 = avctx->priv_data;
1377 MpegEncContext *s = &s1->mpeg_enc_ctx;
1378 int ref, f_code, vbv_delay;
1380 init_get_bits(&s->gb, buf, buf_size * 8);
1382 ref = get_bits(&s->gb, 10); /* temporal ref */
1383 s->pict_type = get_bits(&s->gb, 3);
1384 if (s->pict_type == 0 || s->pict_type > 3)
1387 vbv_delay = get_bits(&s->gb, 16);
1388 s->vbv_delay = vbv_delay;
1389 if (s->pict_type == AV_PICTURE_TYPE_P ||
1390 s->pict_type == AV_PICTURE_TYPE_B) {
1391 s->full_pel[0] = get_bits1(&s->gb);
1392 f_code = get_bits(&s->gb, 3);
1393 if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1396 s->mpeg_f_code[0][0] = f_code;
1397 s->mpeg_f_code[0][1] = f_code;
1399 if (s->pict_type == AV_PICTURE_TYPE_B) {
1400 s->full_pel[1] = get_bits1(&s->gb);
1401 f_code = get_bits(&s->gb, 3);
1402 if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
1405 s->mpeg_f_code[1][0] = f_code;
1406 s->mpeg_f_code[1][1] = f_code;
1408 s->current_picture.f->pict_type = s->pict_type;
1409 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1411 if (avctx->debug & FF_DEBUG_PICT_INFO)
1412 av_log(avctx, AV_LOG_DEBUG,
1413 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1420 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1422 MpegEncContext *s = &s1->mpeg_enc_ctx;
1423 int horiz_size_ext, vert_size_ext;
1426 skip_bits(&s->gb, 1); /* profile and level esc*/
1427 s->avctx->profile = get_bits(&s->gb, 3);
1428 s->avctx->level = get_bits(&s->gb, 4);
1429 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1430 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1431 horiz_size_ext = get_bits(&s->gb, 2);
1432 vert_size_ext = get_bits(&s->gb, 2);
1433 s->width |= (horiz_size_ext << 12);
1434 s->height |= (vert_size_ext << 12);
1435 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1436 s->bit_rate += (bit_rate_ext << 18) * 400;
1437 skip_bits1(&s->gb); /* marker */
1438 s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1440 s->low_delay = get_bits1(&s->gb);
1441 if (s->flags & CODEC_FLAG_LOW_DELAY)
1444 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1445 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1447 av_dlog(s->avctx, "sequence extension\n");
1448 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1450 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1451 av_log(s->avctx, AV_LOG_DEBUG,
1452 "profile: %d, level: %d ps: %d cf:%d vbv buffer: %d, bitrate:%d\n",
1453 s->avctx->profile, s->avctx->level, s->progressive_sequence, s->chroma_format,
1454 s->avctx->rc_buffer_size, s->bit_rate);
1457 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1459 MpegEncContext *s = &s1->mpeg_enc_ctx;
1460 int color_description, w, h;
1462 skip_bits(&s->gb, 3); /* video format */
1463 color_description = get_bits1(&s->gb);
1464 if (color_description) {
1465 s->avctx->color_primaries = get_bits(&s->gb, 8);
1466 s->avctx->color_trc = get_bits(&s->gb, 8);
1467 s->avctx->colorspace = get_bits(&s->gb, 8);
1469 w = get_bits(&s->gb, 14);
1470 skip_bits(&s->gb, 1); // marker
1471 h = get_bits(&s->gb, 14);
1472 // remaining 3 bits are zero padding
1474 s1->pan_scan.width = 16 * w;
1475 s1->pan_scan.height = 16 * h;
1477 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1478 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1481 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1483 MpegEncContext *s = &s1->mpeg_enc_ctx;
1487 if (s->progressive_sequence) {
1488 if (s->repeat_first_field) {
1490 if (s->top_field_first)
1494 if (s->picture_structure == PICT_FRAME) {
1496 if (s->repeat_first_field)
1500 for (i = 0; i < nofco; i++) {
1501 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1502 skip_bits(&s->gb, 1); // marker
1503 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1504 skip_bits(&s->gb, 1); // marker
1507 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1508 av_log(s->avctx, AV_LOG_DEBUG,
1509 "pde (%"PRId16",%"PRId16") (%"PRId16",%"PRId16") (%"PRId16",%"PRId16")\n",
1510 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1511 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1512 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1515 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
1516 uint16_t matrix1[64], int intra)
1520 for (i = 0; i < 64; i++) {
1521 int j = s->idsp.idct_permutation[ff_zigzag_direct[i]];
1522 int v = get_bits(&s->gb, 8);
1524 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1527 if (intra && i == 0 && v != 8) {
1528 av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v);
1529 v = 8; // needed by pink.mpg / issue1046
1538 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1540 av_dlog(s->avctx, "matrix extension\n");
1542 if (get_bits1(&s->gb))
1543 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1544 if (get_bits1(&s->gb))
1545 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1546 if (get_bits1(&s->gb))
1547 load_matrix(s, s->chroma_intra_matrix, NULL, 1);
1548 if (get_bits1(&s->gb))
1549 load_matrix(s, s->chroma_inter_matrix, NULL, 0);
1552 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1554 MpegEncContext *s = &s1->mpeg_enc_ctx;
1556 s->full_pel[0] = s->full_pel[1] = 0;
1557 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1558 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1559 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1560 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1561 if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1562 av_log(s->avctx, AV_LOG_ERROR,
1563 "Missing picture start code, guessing missing values\n");
1564 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1565 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1566 s->pict_type = AV_PICTURE_TYPE_I;
1568 s->pict_type = AV_PICTURE_TYPE_P;
1570 s->pict_type = AV_PICTURE_TYPE_B;
1571 s->current_picture.f->pict_type = s->pict_type;
1572 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1574 s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0];
1575 s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1];
1576 s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0];
1577 s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1];
1579 s->intra_dc_precision = get_bits(&s->gb, 2);
1580 s->picture_structure = get_bits(&s->gb, 2);
1581 s->top_field_first = get_bits1(&s->gb);
1582 s->frame_pred_frame_dct = get_bits1(&s->gb);
1583 s->concealment_motion_vectors = get_bits1(&s->gb);
1584 s->q_scale_type = get_bits1(&s->gb);
1585 s->intra_vlc_format = get_bits1(&s->gb);
1586 s->alternate_scan = get_bits1(&s->gb);
1587 s->repeat_first_field = get_bits1(&s->gb);
1588 s->chroma_420_type = get_bits1(&s->gb);
1589 s->progressive_frame = get_bits1(&s->gb);
1591 if (s->alternate_scan) {
1592 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
1593 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
1595 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
1596 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
1599 /* composite display not parsed */
1600 av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1601 av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1602 av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1603 av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1604 av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1605 av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1606 av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1607 av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1608 av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1611 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1613 AVCodecContext *avctx = s->avctx;
1614 Mpeg1Context *s1 = (Mpeg1Context *) s;
1616 /* start frame decoding */
1617 if (s->first_field || s->picture_structure == PICT_FRAME) {
1618 AVFrameSideData *pan_scan;
1620 if (ff_MPV_frame_start(s, avctx) < 0)
1623 ff_mpeg_er_frame_start(s);
1625 /* first check if we must repeat the frame */
1626 s->current_picture_ptr->f->repeat_pict = 0;
1627 if (s->repeat_first_field) {
1628 if (s->progressive_sequence) {
1629 if (s->top_field_first)
1630 s->current_picture_ptr->f->repeat_pict = 4;
1632 s->current_picture_ptr->f->repeat_pict = 2;
1633 } else if (s->progressive_frame) {
1634 s->current_picture_ptr->f->repeat_pict = 1;
1638 pan_scan = av_frame_new_side_data(s->current_picture_ptr->f,
1639 AV_FRAME_DATA_PANSCAN,
1640 sizeof(s1->pan_scan));
1642 return AVERROR(ENOMEM);
1643 memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
1645 if (s1->a53_caption) {
1646 AVFrameSideData *sd = av_frame_new_side_data(
1647 s->current_picture_ptr->f, AV_FRAME_DATA_A53_CC,
1648 s1->a53_caption_size);
1650 memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
1651 av_freep(&s1->a53_caption);
1654 if (s1->has_stereo3d) {
1655 AVStereo3D *stereo = av_stereo3d_create_side_data(s->current_picture_ptr->f);
1657 return AVERROR(ENOMEM);
1659 *stereo = s1->stereo3d;
1660 s1->has_stereo3d = 0;
1662 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1663 ff_thread_finish_setup(avctx);
1664 } else { // second field
1667 if (!s->current_picture_ptr) {
1668 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1672 if (s->avctx->hwaccel &&
1673 (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
1674 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1675 av_log(avctx, AV_LOG_ERROR,
1676 "hardware accelerator failed to decode first field\n");
1679 for (i = 0; i < 4; i++) {
1680 s->current_picture.f->data[i] = s->current_picture_ptr->f->data[i];
1681 if (s->picture_structure == PICT_BOTTOM_FIELD)
1682 s->current_picture.f->data[i] +=
1683 s->current_picture_ptr->f->linesize[i];
1687 if (avctx->hwaccel) {
1688 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
1695 #define DECODE_SLICE_ERROR -1
1696 #define DECODE_SLICE_OK 0
1700 * MpegEncContext.mb_y must be set to the MB row from the startcode.
1701 * @return DECODE_SLICE_ERROR if the slice is damaged,
1702 * DECODE_SLICE_OK if this slice is OK
1704 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1705 const uint8_t **buf, int buf_size)
1707 AVCodecContext *avctx = s->avctx;
1708 const int lowres = s->avctx->lowres;
1709 const int field_pic = s->picture_structure != PICT_FRAME;
1712 s->resync_mb_y = -1;
1714 av_assert0(mb_y < s->mb_height);
1716 init_get_bits(&s->gb, *buf, buf_size * 8);
1717 if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
1718 skip_bits(&s->gb, 3);
1720 ff_mpeg1_clean_buffers(s);
1721 s->interlaced_dct = 0;
1723 s->qscale = get_qscale(s);
1725 if (s->qscale == 0) {
1726 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1730 /* extra slice info */
1731 if (skip_1stop_8data_bits(&s->gb) < 0)
1732 return AVERROR_INVALIDDATA;
1736 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1739 while (get_bits_left(&s->gb) > 0) {
1740 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1741 MBINCR_VLC_BITS, 2);
1743 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1749 /* otherwise, stuffing, nothing to do */
1757 if (s->mb_x >= (unsigned) s->mb_width) {
1758 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1762 if (avctx->hwaccel && avctx->hwaccel->decode_slice) {
1763 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1764 int start_code = -1;
1765 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1766 if (buf_end < *buf + buf_size)
1769 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1770 return DECODE_SLICE_ERROR;
1772 return DECODE_SLICE_OK;
1775 s->resync_mb_x = s->mb_x;
1776 s->resync_mb_y = s->mb_y = mb_y;
1778 ff_init_block_index(s);
1780 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1781 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1782 av_log(s->avctx, AV_LOG_DEBUG,
1783 "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",
1785 s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
1786 s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1787 s->pict_type == AV_PICTURE_TYPE_I ? "I" :
1788 (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
1789 (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
1790 s->progressive_sequence ? "ps" : "",
1791 s->progressive_frame ? "pf" : "",
1792 s->alternate_scan ? "alt" : "",
1793 s->top_field_first ? "top" : "",
1794 s->intra_dc_precision, s->picture_structure,
1795 s->frame_pred_frame_dct, s->concealment_motion_vectors,
1796 s->q_scale_type, s->intra_vlc_format,
1797 s->repeat_first_field, s->chroma_420_type ? "420" : "");
1802 // If 1, we memcpy blocks in xvmcvideo.
1803 if ((CONFIG_MPEG1_XVMC_HWACCEL || CONFIG_MPEG2_XVMC_HWACCEL) && s->pack_pblocks)
1804 ff_xvmc_init_block(s); // set s->block
1806 if (mpeg_decode_mb(s, s->block) < 0)
1809 // Note motion_val is normally NULL unless we want to extract the MVs.
1810 if (s->current_picture.motion_val[0] && !s->encoding) {
1811 const int wrap = s->b8_stride;
1812 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1813 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1814 int motion_x, motion_y, dir, i;
1816 for (i = 0; i < 2; i++) {
1817 for (dir = 0; dir < 2; dir++) {
1819 (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1820 motion_x = motion_y = 0;
1821 } else if (s->mv_type == MV_TYPE_16X16 ||
1822 (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1823 motion_x = s->mv[dir][0][0];
1824 motion_y = s->mv[dir][0][1];
1825 } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
1826 motion_x = s->mv[dir][i][0];
1827 motion_y = s->mv[dir][i][1];
1830 s->current_picture.motion_val[dir][xy][0] = motion_x;
1831 s->current_picture.motion_val[dir][xy][1] = motion_y;
1832 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1833 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1834 s->current_picture.ref_index [dir][b8_xy] =
1835 s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1836 av_assert2(s->field_select[dir][i] == 0 ||
1837 s->field_select[dir][i] == 1);
1844 s->dest[0] += 16 >> lowres;
1845 s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
1846 s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
1848 ff_MPV_decode_mb(s, s->block);
1850 if (++s->mb_x >= s->mb_width) {
1851 const int mb_size = 16 >> s->avctx->lowres;
1853 ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
1854 ff_MPV_report_decode_progress(s);
1857 s->mb_y += 1 << field_pic;
1859 if (s->mb_y >= s->mb_height) {
1860 int left = get_bits_left(&s->gb);
1861 int is_d10 = s->chroma_format == 2 &&
1862 s->pict_type == AV_PICTURE_TYPE_I &&
1863 avctx->profile == 0 && avctx->level == 5 &&
1864 s->intra_dc_precision == 2 &&
1865 s->q_scale_type == 1 && s->alternate_scan == 0 &&
1866 s->progressive_frame == 0
1867 /* vbv_delay == 0xBBB || 0xE10 */;
1869 if (left >= 32 && !is_d10) {
1870 GetBitContext gb = s->gb;
1871 align_get_bits(&gb);
1872 if (show_bits(&gb, 24) == 0x060E2B) {
1873 av_log(avctx, AV_LOG_DEBUG, "Invalid MXF data found in video stream\n");
1879 (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
1880 ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) {
1881 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
1882 left, show_bits(&s->gb, FFMIN(left, 23)));
1888 ff_init_block_index(s);
1891 /* skip mb handling */
1892 if (s->mb_skip_run == -1) {
1893 /* read increment again */
1896 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1897 MBINCR_VLC_BITS, 2);
1899 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1904 s->mb_skip_run += 33;
1905 } else if (code == 35) {
1906 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1907 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1910 goto eos; /* end of slice */
1912 /* otherwise, stuffing, nothing to do */
1914 s->mb_skip_run += code;
1918 if (s->mb_skip_run) {
1920 if (s->pict_type == AV_PICTURE_TYPE_I) {
1921 av_log(s->avctx, AV_LOG_ERROR,
1922 "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1928 for (i = 0; i < 12; i++)
1929 s->block_last_index[i] = -1;
1930 if (s->picture_structure == PICT_FRAME)
1931 s->mv_type = MV_TYPE_16X16;
1933 s->mv_type = MV_TYPE_FIELD;
1934 if (s->pict_type == AV_PICTURE_TYPE_P) {
1935 /* if P type, zero motion vector is implied */
1936 s->mv_dir = MV_DIR_FORWARD;
1937 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1938 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1939 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1940 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1942 /* if B type, reuse previous vectors and directions */
1943 s->mv[0][0][0] = s->last_mv[0][0][0];
1944 s->mv[0][0][1] = s->last_mv[0][0][1];
1945 s->mv[1][0][0] = s->last_mv[1][0][0];
1946 s->mv[1][0][1] = s->last_mv[1][0][1];
1951 eos: // end of slice
1952 if (get_bits_left(&s->gb) < 0)
1953 return AVERROR_INVALIDDATA;
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;
1996 if (s->codec_id != AV_CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
1997 mb_y += (*buf&0xE0)<<2;
1999 if (s->picture_structure == PICT_BOTTOM_FIELD)
2001 if (mb_y < 0 || mb_y >= s->end_mb_y)
2007 * Handle slice ends.
2008 * @return 1 if it seems to be the last slice
2010 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
2012 Mpeg1Context *s1 = avctx->priv_data;
2013 MpegEncContext *s = &s1->mpeg_enc_ctx;
2015 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
2018 if (s->avctx->hwaccel) {
2019 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
2020 av_log(avctx, AV_LOG_ERROR,
2021 "hardware accelerator failed to decode picture\n");
2024 /* end of slice reached */
2025 if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field && !s1->first_slice) {
2028 ff_er_frame_end(&s->er);
2030 ff_MPV_frame_end(s);
2032 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
2033 int ret = av_frame_ref(pict, s->current_picture_ptr->f);
2036 ff_print_debug_info(s, s->current_picture_ptr, pict);
2037 ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG2);
2039 if (avctx->active_thread_type & FF_THREAD_FRAME)
2040 s->picture_number++;
2041 /* latency of 1 frame for I- and P-frames */
2042 /* XXX: use another variable than picture_number */
2043 if (s->last_picture_ptr != NULL) {
2044 int ret = av_frame_ref(pict, s->last_picture_ptr->f);
2047 ff_print_debug_info(s, s->last_picture_ptr, pict);
2048 ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG2);
2058 static int mpeg1_decode_sequence(AVCodecContext *avctx,
2059 const uint8_t *buf, int buf_size)
2061 Mpeg1Context *s1 = avctx->priv_data;
2062 MpegEncContext *s = &s1->mpeg_enc_ctx;
2066 init_get_bits(&s->gb, buf, buf_size * 8);
2068 width = get_bits(&s->gb, 12);
2069 height = get_bits(&s->gb, 12);
2070 if (width == 0 || height == 0) {
2071 av_log(avctx, AV_LOG_WARNING,
2072 "Invalid horizontal or vertical size value.\n");
2073 if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
2074 return AVERROR_INVALIDDATA;
2076 s->aspect_ratio_info = get_bits(&s->gb, 4);
2077 if (s->aspect_ratio_info == 0) {
2078 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
2079 if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
2082 s->frame_rate_index = get_bits(&s->gb, 4);
2083 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
2085 s->bit_rate = get_bits(&s->gb, 18) * 400;
2086 if (get_bits1(&s->gb) == 0) /* marker */
2091 s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
2092 skip_bits(&s->gb, 1);
2095 if (get_bits1(&s->gb)) {
2096 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
2098 for (i = 0; i < 64; i++) {
2099 j = s->idsp.idct_permutation[i];
2100 v = ff_mpeg1_default_intra_matrix[i];
2101 s->intra_matrix[j] = v;
2102 s->chroma_intra_matrix[j] = v;
2105 if (get_bits1(&s->gb)) {
2106 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
2108 for (i = 0; i < 64; i++) {
2109 int j = s->idsp.idct_permutation[i];
2110 v = ff_mpeg1_default_non_intra_matrix[i];
2111 s->inter_matrix[j] = v;
2112 s->chroma_inter_matrix[j] = v;
2116 if (show_bits(&s->gb, 23) != 0) {
2117 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2121 /* We set MPEG-2 parameters so that it emulates MPEG-1. */
2122 s->progressive_sequence = 1;
2123 s->progressive_frame = 1;
2124 s->picture_structure = PICT_FRAME;
2126 s->frame_pred_frame_dct = 1;
2127 s->chroma_format = 1;
2129 s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
2130 s->out_format = FMT_MPEG1;
2131 s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
2132 if (s->flags & CODEC_FLAG_LOW_DELAY)
2135 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2136 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2137 s->avctx->rc_buffer_size, s->bit_rate);
2142 static int vcr2_init_sequence(AVCodecContext *avctx)
2144 Mpeg1Context *s1 = avctx->priv_data;
2145 MpegEncContext *s = &s1->mpeg_enc_ctx;
2148 /* start new MPEG-1 context decoding */
2149 s->out_format = FMT_MPEG1;
2150 if (s1->mpeg_enc_ctx_allocated) {
2151 ff_MPV_common_end(s);
2152 s1->mpeg_enc_ctx_allocated = 0;
2154 s->width = avctx->coded_width;
2155 s->height = avctx->coded_height;
2156 avctx->has_b_frames = 0; // true?
2159 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2160 setup_hwaccel_for_pixfmt(avctx);
2162 if (ff_MPV_common_init(s) < 0)
2164 s1->mpeg_enc_ctx_allocated = 1;
2166 for (i = 0; i < 64; i++) {
2167 int j = s->idsp.idct_permutation[i];
2168 v = ff_mpeg1_default_intra_matrix[i];
2169 s->intra_matrix[j] = v;
2170 s->chroma_intra_matrix[j] = v;
2172 v = ff_mpeg1_default_non_intra_matrix[i];
2173 s->inter_matrix[j] = v;
2174 s->chroma_inter_matrix[j] = v;
2177 s->progressive_sequence = 1;
2178 s->progressive_frame = 1;
2179 s->picture_structure = PICT_FRAME;
2181 s->frame_pred_frame_dct = 1;
2182 s->chroma_format = 1;
2183 if (s->codec_tag == AV_RL32("BW10")) {
2184 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
2186 s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
2187 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
2189 s1->save_width = s->width;
2190 s1->save_height = s->height;
2191 s1->save_progressive_seq = s->progressive_sequence;
2195 static int mpeg_decode_a53_cc(AVCodecContext *avctx,
2196 const uint8_t *p, int buf_size)
2198 Mpeg1Context *s1 = avctx->priv_data;
2200 if (buf_size >= 6 &&
2201 p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
2202 p[4] == 3 && (p[5] & 0x40)) {
2203 /* extract A53 Part 4 CC data */
2204 int cc_count = p[5] & 0x1f;
2205 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2206 av_freep(&s1->a53_caption);
2207 s1->a53_caption_size = cc_count * 3;
2208 s1->a53_caption = av_malloc(s1->a53_caption_size);
2209 if (s1->a53_caption)
2210 memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
2213 } else if (buf_size >= 11 &&
2214 p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
2215 /* extract DVD CC data */
2218 // There is a caption count field in the data, but it is often
2219 // incorect. So count the number of captions present.
2220 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2222 // Transform the DVD format into A53 Part 4 format
2224 av_freep(&s1->a53_caption);
2225 s1->a53_caption_size = cc_count * 6;
2226 s1->a53_caption = av_malloc(s1->a53_caption_size);
2227 if (s1->a53_caption) {
2228 uint8_t field1 = !!(p[4] & 0x80);
2229 uint8_t *cap = s1->a53_caption;
2231 for (i = 0; i < cc_count; i++) {
2232 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2235 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2248 static void mpeg_decode_user_data(AVCodecContext *avctx,
2249 const uint8_t *p, int buf_size)
2251 Mpeg1Context *s = avctx->priv_data;
2252 const uint8_t *buf_end = p + buf_size;
2257 if (!memcmp(p+i, "\0TMPGEXS\0", 9)){
2261 /* for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
2262 av_log(avctx, AV_LOG_ERROR, "%c", p[i]);
2264 av_log(avctx, AV_LOG_ERROR, "\n");*/
2267 /* we parse the DTG active format information */
2268 if (buf_end - p >= 5 &&
2269 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2277 if (buf_end - p < 1)
2279 avctx->dtg_active_format = p[0] & 0x0f;
2281 } else if (buf_end - p >= 6 &&
2282 p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
2283 p[4] == 0x03) { // S3D_video_format_length
2284 // the 0x7F mask ignores the reserved_bit value
2285 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2287 if (S3D_video_format_type == 0x03 ||
2288 S3D_video_format_type == 0x04 ||
2289 S3D_video_format_type == 0x08 ||
2290 S3D_video_format_type == 0x23) {
2291 Mpeg1Context *s1 = avctx->priv_data;
2293 s1->has_stereo3d = 1;
2295 switch (S3D_video_format_type) {
2297 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
2300 s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
2303 s1->stereo3d.type = AV_STEREO3D_2D;
2306 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2310 } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
2315 static void mpeg_decode_gop(AVCodecContext *avctx,
2316 const uint8_t *buf, int buf_size)
2318 Mpeg1Context *s1 = avctx->priv_data;
2319 MpegEncContext *s = &s1->mpeg_enc_ctx;
2323 init_get_bits(&s->gb, buf, buf_size * 8);
2325 tc = avctx->timecode_frame_start = get_bits(&s->gb, 25);
2327 s->closed_gop = get_bits1(&s->gb);
2328 /* broken_link indicate that after editing the
2329 * reference frames of the first B-Frames after GOP I-Frame
2330 * are missing (open gop) */
2331 broken_link = get_bits1(&s->gb);
2333 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
2334 char tcbuf[AV_TIMECODE_STR_SIZE];
2335 av_timecode_make_mpeg_tc_string(tcbuf, tc);
2336 av_log(s->avctx, AV_LOG_DEBUG,
2337 "GOP (%s) closed_gop=%d broken_link=%d\n",
2338 tcbuf, s->closed_gop, broken_link);
2342 static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
2343 int *got_output, const uint8_t *buf, int buf_size)
2345 Mpeg1Context *s = avctx->priv_data;
2346 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2347 const uint8_t *buf_ptr = buf;
2348 const uint8_t *buf_end = buf + buf_size;
2349 int ret, input_size;
2350 int last_code = 0, skip_frame = 0;
2351 int picture_start_code_seen = 0;
2354 /* find next start code */
2355 uint32_t start_code = -1;
2356 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
2357 if (start_code > 0x1ff) {
2360 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2363 av_assert0(avctx->thread_count > 1);
2365 avctx->execute(avctx, slice_decode_thread,
2366 &s2->thread_context[0], NULL,
2367 s->slice_count, sizeof(void *));
2368 for (i = 0; i < s->slice_count; i++)
2369 s2->er.error_count += s2->thread_context[i]->er.error_count;
2372 if ((CONFIG_MPEG_VDPAU_DECODER || CONFIG_MPEG1_VDPAU_DECODER)
2373 && uses_vdpau(avctx))
2374 ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
2376 ret = slice_end(avctx, picture);
2380 // FIXME: merge with the stuff in mpeg_decode_slice
2381 if (s2->last_picture_ptr || s2->low_delay)
2387 if (avctx->err_recognition & AV_EF_EXPLODE && s2->er.error_count)
2388 return AVERROR_INVALIDDATA;
2390 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2393 input_size = buf_end - buf_ptr;
2395 if (avctx->debug & FF_DEBUG_STARTCODE)
2396 av_log(avctx, AV_LOG_DEBUG, "%3"PRIX32" at %"PTRDIFF_SPECIFIER" left %d\n",
2397 start_code, buf_ptr - buf, input_size);
2399 /* prepare data for next start code */
2400 switch (start_code) {
2401 case SEQ_START_CODE:
2402 if (last_code == 0) {
2403 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2404 if (buf != avctx->extradata)
2407 av_log(avctx, AV_LOG_ERROR,
2408 "ignoring SEQ_START_CODE after %X\n", last_code);
2409 if (avctx->err_recognition & AV_EF_EXPLODE)
2410 return AVERROR_INVALIDDATA;
2414 case PICTURE_START_CODE:
2415 if (picture_start_code_seen && s2->picture_structure == PICT_FRAME) {
2416 /* If it's a frame picture, there can't be more than one picture header.
2417 Yet, it does happen and we need to handle it. */
2418 av_log(avctx, AV_LOG_WARNING, "ignoring extra picture following a frame-picture\n");
2421 picture_start_code_seen = 1;
2423 if (s2->width <= 0 || s2->height <= 0) {
2424 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
2425 s2->width, s2->height);
2426 return AVERROR_INVALIDDATA;
2430 s2->intra_dc_precision= 3;
2431 s2->intra_matrix[0]= 1;
2433 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
2434 !avctx->hwaccel && s->slice_count) {
2437 avctx->execute(avctx, slice_decode_thread,
2438 s2->thread_context, NULL,
2439 s->slice_count, sizeof(void *));
2440 for (i = 0; i < s->slice_count; i++)
2441 s2->er.error_count += s2->thread_context[i]->er.error_count;
2444 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2445 ret = mpeg_decode_postinit(avctx);
2447 av_log(avctx, AV_LOG_ERROR,
2448 "mpeg_decode_postinit() failure\n");
2452 /* We have a complete image: we try to decompress it. */
2453 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2456 last_code = PICTURE_START_CODE;
2458 av_log(avctx, AV_LOG_ERROR,
2459 "ignoring pic after %X\n", last_code);
2460 if (avctx->err_recognition & AV_EF_EXPLODE)
2461 return AVERROR_INVALIDDATA;
2464 case EXT_START_CODE:
2465 init_get_bits(&s2->gb, buf_ptr, input_size * 8);
2467 switch (get_bits(&s2->gb, 4)) {
2469 if (last_code == 0) {
2470 mpeg_decode_sequence_extension(s);
2472 av_log(avctx, AV_LOG_ERROR,
2473 "ignoring seq ext after %X\n", last_code);
2474 if (avctx->err_recognition & AV_EF_EXPLODE)
2475 return AVERROR_INVALIDDATA;
2479 mpeg_decode_sequence_display_extension(s);
2482 mpeg_decode_quant_matrix_extension(s2);
2485 mpeg_decode_picture_display_extension(s);
2488 if (last_code == PICTURE_START_CODE) {
2489 mpeg_decode_picture_coding_extension(s);
2491 av_log(avctx, AV_LOG_ERROR,
2492 "ignoring pic cod ext after %X\n", last_code);
2493 if (avctx->err_recognition & AV_EF_EXPLODE)
2494 return AVERROR_INVALIDDATA;
2499 case USER_START_CODE:
2500 mpeg_decode_user_data(avctx, buf_ptr, input_size);
2502 case GOP_START_CODE:
2503 if (last_code == 0) {
2504 s2->first_field = 0;
2505 mpeg_decode_gop(avctx, buf_ptr, input_size);
2508 av_log(avctx, AV_LOG_ERROR,
2509 "ignoring GOP_START_CODE after %X\n", last_code);
2510 if (avctx->err_recognition & AV_EF_EXPLODE)
2511 return AVERROR_INVALIDDATA;
2515 if (start_code >= SLICE_MIN_START_CODE &&
2516 start_code <= SLICE_MAX_START_CODE && last_code == PICTURE_START_CODE) {
2517 if (s2->progressive_sequence && !s2->progressive_frame) {
2518 s2->progressive_frame = 1;
2519 av_log(s2->avctx, AV_LOG_ERROR,
2520 "interlaced frame in progressive sequence, ignoring\n");
2523 if (s2->picture_structure == 0 ||
2524 (s2->progressive_frame && s2->picture_structure != PICT_FRAME)) {
2525 av_log(s2->avctx, AV_LOG_ERROR,
2526 "picture_structure %d invalid, ignoring\n",
2527 s2->picture_structure);
2528 s2->picture_structure = PICT_FRAME;
2531 if (s2->progressive_sequence && !s2->frame_pred_frame_dct)
2532 av_log(s2->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
2534 if (s2->picture_structure == PICT_FRAME) {
2535 s2->first_field = 0;
2536 s2->v_edge_pos = 16 * s2->mb_height;
2538 s2->first_field ^= 1;
2539 s2->v_edge_pos = 8 * s2->mb_height;
2540 memset(s2->mbskip_table, 0, s2->mb_stride * s2->mb_height);
2543 if (start_code >= SLICE_MIN_START_CODE &&
2544 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2545 const int field_pic = s2->picture_structure != PICT_FRAME;
2546 int mb_y = start_code - SLICE_MIN_START_CODE;
2547 last_code = SLICE_MIN_START_CODE;
2548 if (s2->codec_id != AV_CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
2549 mb_y += (*buf_ptr&0xE0)<<2;
2552 if (s2->picture_structure == PICT_BOTTOM_FIELD)
2555 if (buf_end - buf_ptr < 2) {
2556 av_log(s2->avctx, AV_LOG_ERROR, "slice too small\n");
2557 return AVERROR_INVALIDDATA;
2560 if (mb_y >= s2->mb_height) {
2561 av_log(s2->avctx, AV_LOG_ERROR,
2562 "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2566 if (s2->last_picture_ptr == NULL) {
2567 /* Skip B-frames if we do not have reference frames and
2568 * GOP is not closed. */
2569 if (s2->pict_type == AV_PICTURE_TYPE_B) {
2570 if (!s2->closed_gop) {
2576 if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->flags2 & CODEC_FLAG2_SHOW_ALL))
2578 if (s2->next_picture_ptr == NULL) {
2579 /* Skip P-frames if we do not have a reference frame or
2580 * we have an invalid header. */
2581 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
2586 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
2587 s2->pict_type == AV_PICTURE_TYPE_B) ||
2588 (avctx->skip_frame >= AVDISCARD_NONKEY &&
2589 s2->pict_type != AV_PICTURE_TYPE_I) ||
2590 avctx->skip_frame >= AVDISCARD_ALL) {
2595 if (!s->mpeg_enc_ctx_allocated)
2598 if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2599 if (mb_y < avctx->skip_top ||
2600 mb_y >= s2->mb_height - avctx->skip_bottom)
2604 if (!s2->pict_type) {
2605 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2606 if (avctx->err_recognition & AV_EF_EXPLODE)
2607 return AVERROR_INVALIDDATA;
2611 if (s->first_slice) {
2614 if (mpeg_field_start(s2, buf, buf_size) < 0)
2617 if (!s2->current_picture_ptr) {
2618 av_log(avctx, AV_LOG_ERROR,
2619 "current_picture not initialized\n");
2620 return AVERROR_INVALIDDATA;
2623 if (uses_vdpau(avctx)) {
2629 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2631 int threshold = (s2->mb_height * s->slice_count +
2632 s2->slice_context_count / 2) /
2633 s2->slice_context_count;
2634 av_assert0(avctx->thread_count > 1);
2635 if (threshold <= mb_y) {
2636 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2638 thread_context->start_mb_y = mb_y;
2639 thread_context->end_mb_y = s2->mb_height;
2640 if (s->slice_count) {
2641 s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
2642 ret = ff_update_duplicate_context(thread_context, s2);
2646 init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
2649 buf_ptr += 2; // FIXME add minimum number of bytes per slice
2651 ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2655 if (avctx->err_recognition & AV_EF_EXPLODE)
2657 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2658 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2659 s2->resync_mb_y, s2->mb_x, s2->mb_y,
2660 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
2662 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2663 s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
2664 ER_AC_END | ER_DC_END | ER_MV_END);
2673 static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
2674 int *got_output, AVPacket *avpkt)
2676 const uint8_t *buf = avpkt->data;
2678 int buf_size = avpkt->size;
2679 Mpeg1Context *s = avctx->priv_data;
2680 AVFrame *picture = data;
2681 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2683 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2684 /* special case for last picture */
2685 if (s2->low_delay == 0 && s2->next_picture_ptr) {
2686 int ret = av_frame_ref(picture, s2->next_picture_ptr->f);
2690 s2->next_picture_ptr = NULL;
2697 if (s2->flags & CODEC_FLAG_TRUNCATED) {
2698 int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
2701 if (ff_combine_frame(&s2->parse_context, next,
2702 (const uint8_t **) &buf, &buf_size) < 0)
2706 s2->codec_tag = avpriv_toupper4(avctx->codec_tag);
2707 if (s->mpeg_enc_ctx_allocated == 0 && ( s2->codec_tag == AV_RL32("VCR2")
2708 || s2->codec_tag == AV_RL32("BW10")
2710 vcr2_init_sequence(avctx);
2714 if (avctx->extradata && !s->extradata_decoded) {
2715 ret = decode_chunks(avctx, picture, got_output,
2716 avctx->extradata, avctx->extradata_size);
2718 av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
2721 s->extradata_decoded = 1;
2722 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE)) {
2723 s2->current_picture_ptr = NULL;
2728 ret = decode_chunks(avctx, picture, got_output, buf, buf_size);
2729 if (ret<0 || *got_output)
2730 s2->current_picture_ptr = NULL;
2735 static void flush(AVCodecContext *avctx)
2737 Mpeg1Context *s = avctx->priv_data;
2741 ff_mpeg_flush(avctx);
2744 static av_cold int mpeg_decode_end(AVCodecContext *avctx)
2746 Mpeg1Context *s = avctx->priv_data;
2748 if (s->mpeg_enc_ctx_allocated)
2749 ff_MPV_common_end(&s->mpeg_enc_ctx);
2750 av_freep(&s->a53_caption);
2754 static const AVProfile mpeg2_video_profiles[] = {
2755 { FF_PROFILE_MPEG2_422, "4:2:2" },
2756 { FF_PROFILE_MPEG2_HIGH, "High" },
2757 { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2758 { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2759 { FF_PROFILE_MPEG2_MAIN, "Main" },
2760 { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2761 { FF_PROFILE_RESERVED, "Reserved" },
2762 { FF_PROFILE_RESERVED, "Reserved" },
2763 { FF_PROFILE_UNKNOWN },
2766 AVCodec ff_mpeg1video_decoder = {
2767 .name = "mpeg1video",
2768 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2769 .type = AVMEDIA_TYPE_VIDEO,
2770 .id = AV_CODEC_ID_MPEG1VIDEO,
2771 .priv_data_size = sizeof(Mpeg1Context),
2772 .init = mpeg_decode_init,
2773 .close = mpeg_decode_end,
2774 .decode = mpeg_decode_frame,
2775 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2776 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2777 CODEC_CAP_SLICE_THREADS,
2780 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2783 AVCodec ff_mpeg2video_decoder = {
2784 .name = "mpeg2video",
2785 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2786 .type = AVMEDIA_TYPE_VIDEO,
2787 .id = AV_CODEC_ID_MPEG2VIDEO,
2788 .priv_data_size = sizeof(Mpeg1Context),
2789 .init = mpeg_decode_init,
2790 .close = mpeg_decode_end,
2791 .decode = mpeg_decode_frame,
2792 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2793 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2794 CODEC_CAP_SLICE_THREADS,
2797 .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2801 AVCodec ff_mpegvideo_decoder = {
2802 .name = "mpegvideo",
2803 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2804 .type = AVMEDIA_TYPE_VIDEO,
2805 .id = AV_CODEC_ID_MPEG2VIDEO,
2806 .priv_data_size = sizeof(Mpeg1Context),
2807 .init = mpeg_decode_init,
2808 .close = mpeg_decode_end,
2809 .decode = mpeg_decode_frame,
2810 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
2816 #if CONFIG_MPEG_XVMC_DECODER
2817 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2819 if (avctx->active_thread_type & FF_THREAD_SLICE)
2821 if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2823 if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
2824 av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2826 mpeg_decode_init(avctx);
2828 avctx->pix_fmt = AV_PIX_FMT_XVMC_MPEG2_IDCT;
2829 avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2834 AVCodec ff_mpeg_xvmc_decoder = {
2835 .name = "mpegvideo_xvmc",
2836 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2837 .type = AVMEDIA_TYPE_VIDEO,
2838 .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2839 .priv_data_size = sizeof(Mpeg1Context),
2840 .init = mpeg_mc_decode_init,
2841 .close = mpeg_decode_end,
2842 .decode = mpeg_decode_frame,
2843 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2844 CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2849 #endif /* FF_API_XVMC */
2851 #if CONFIG_MPEG_VDPAU_DECODER
2852 AVCodec ff_mpeg_vdpau_decoder = {
2853 .name = "mpegvideo_vdpau",
2854 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
2855 .type = AVMEDIA_TYPE_VIDEO,
2856 .id = AV_CODEC_ID_MPEG2VIDEO,
2857 .priv_data_size = sizeof(Mpeg1Context),
2858 .init = mpeg_decode_init,
2859 .close = mpeg_decode_end,
2860 .decode = mpeg_decode_frame,
2861 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
2862 CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2867 #if CONFIG_MPEG1_VDPAU_DECODER
2868 AVCodec ff_mpeg1_vdpau_decoder = {
2869 .name = "mpeg1video_vdpau",
2870 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
2871 .type = AVMEDIA_TYPE_VIDEO,
2872 .id = AV_CODEC_ID_MPEG1VIDEO,
2873 .priv_data_size = sizeof(Mpeg1Context),
2874 .init = mpeg_decode_init,
2875 .close = mpeg_decode_end,
2876 .decode = mpeg_decode_frame,
2877 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
2878 CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,