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
28 #include "libavutil/attributes.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/stereo3d.h"
33 #include "bytestream.h"
35 #include "error_resilience.h"
38 #include "mpeg12data.h"
39 #include "mpegvideo.h"
42 #include "xvmc_internal.h"
44 typedef struct Mpeg1Context {
45 MpegEncContext mpeg_enc_ctx;
46 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
47 int repeat_field; /* true if we must repeat the field */
48 AVPanScan pan_scan; /* some temporary storage for the panscan */
55 int save_width, save_height, save_progressive_seq;
56 AVRational frame_rate_ext; /* MPEG-2 specific framerate modificator */
57 int sync; /* Did we reach a sync point like a GOP/SEQ/KEYFrame? */
58 int closed_gop; /* GOP is closed */
60 int extradata_decoded;
63 #define MB_TYPE_ZERO_MV 0x20000000
65 static const uint32_t ptype2mb_type[7] = {
67 MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
69 MB_TYPE_L0 | MB_TYPE_CBP,
70 MB_TYPE_QUANT | MB_TYPE_INTRA,
71 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP | MB_TYPE_ZERO_MV | MB_TYPE_16x16,
72 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
75 static const uint32_t btype2mb_type[11] = {
78 MB_TYPE_L1 | MB_TYPE_CBP,
80 MB_TYPE_L0 | MB_TYPE_CBP,
82 MB_TYPE_L0L1 | MB_TYPE_CBP,
83 MB_TYPE_QUANT | MB_TYPE_INTRA,
84 MB_TYPE_QUANT | MB_TYPE_L1 | MB_TYPE_CBP,
85 MB_TYPE_QUANT | MB_TYPE_L0 | MB_TYPE_CBP,
86 MB_TYPE_QUANT | MB_TYPE_L0L1 | MB_TYPE_CBP,
89 static const uint8_t non_linear_qscale[32] = {
90 0, 1, 2, 3, 4, 5, 6, 7,
91 8, 10, 12, 14, 16, 18, 20, 22,
92 24, 28, 32, 36, 40, 44, 48, 52,
93 56, 64, 72, 80, 88, 96, 104, 112,
96 /* as H.263, but only 17 codes */
97 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
99 int code, sign, val, shift;
101 code = get_vlc2(&s->gb, ff_mv_vlc.table, MV_VLC_BITS, 2);
107 sign = get_bits1(&s->gb);
111 val = (val - 1) << shift;
112 val |= get_bits(&s->gb, shift);
119 /* modulo decoding */
120 return sign_extend(val, 5 + shift);
123 #define check_scantable_index(ctx, x) \
126 av_log(ctx->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", \
127 ctx->mb_x, ctx->mb_y); \
128 return AVERROR_INVALIDDATA; \
132 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
133 int16_t *block, int n)
135 int level, dc, diff, i, j, run;
137 RLTable *rl = &ff_rl_mpeg1;
138 uint8_t *const scantable = s->intra_scantable.permutated;
139 const uint16_t *quant_matrix = s->intra_matrix;
140 const int qscale = s->qscale;
143 component = (n <= 3 ? 0 : n - 4 + 1);
144 diff = decode_dc(&s->gb, component);
147 dc = s->last_dc[component];
149 s->last_dc[component] = dc;
150 block[0] = dc * quant_matrix[0];
151 av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
154 OPEN_READER(re, &s->gb);
155 /* now quantify & encode AC coefficients */
157 UPDATE_CACHE(re, &s->gb);
158 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
163 } else if (level != 0) {
165 check_scantable_index(s, i);
167 level = (level * qscale * quant_matrix[j]) >> 4;
168 level = (level - 1) | 1;
169 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
170 SHOW_SBITS(re, &s->gb, 1);
171 LAST_SKIP_BITS(re, &s->gb, 1);
174 run = SHOW_UBITS(re, &s->gb, 6) + 1;
175 LAST_SKIP_BITS(re, &s->gb, 6);
176 UPDATE_CACHE(re, &s->gb);
177 level = SHOW_SBITS(re, &s->gb, 8);
178 SKIP_BITS(re, &s->gb, 8);
180 level = SHOW_UBITS(re, &s->gb, 8) - 256;
181 LAST_SKIP_BITS(re, &s->gb, 8);
182 } else if (level == 0) {
183 level = SHOW_UBITS(re, &s->gb, 8);
184 LAST_SKIP_BITS(re, &s->gb, 8);
187 check_scantable_index(s, i);
191 level = (level * qscale * quant_matrix[j]) >> 4;
192 level = (level - 1) | 1;
195 level = (level * qscale * quant_matrix[j]) >> 4;
196 level = (level - 1) | 1;
202 CLOSE_READER(re, &s->gb);
204 s->block_last_index[n] = i;
208 int ff_mpeg1_decode_block_intra(MpegEncContext *s, int16_t *block, int n)
210 return mpeg1_decode_block_intra(s, block, n);
213 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
214 int16_t *block, int n)
216 int level, i, j, run;
217 RLTable *rl = &ff_rl_mpeg1;
218 uint8_t *const scantable = s->intra_scantable.permutated;
219 const uint16_t *quant_matrix = s->inter_matrix;
220 const int qscale = s->qscale;
223 OPEN_READER(re, &s->gb);
225 // special case for first coefficient, no need to add second VLC table
226 UPDATE_CACHE(re, &s->gb);
227 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
228 level = (3 * qscale * quant_matrix[0]) >> 5;
229 level = (level - 1) | 1;
230 if (GET_CACHE(re, &s->gb) & 0x40000000)
234 SKIP_BITS(re, &s->gb, 2);
235 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
238 /* now quantify & encode AC coefficients */
240 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
245 check_scantable_index(s, i);
247 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
248 level = (level - 1) | 1;
249 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
250 SHOW_SBITS(re, &s->gb, 1);
251 SKIP_BITS(re, &s->gb, 1);
254 run = SHOW_UBITS(re, &s->gb, 6) + 1;
255 LAST_SKIP_BITS(re, &s->gb, 6);
256 UPDATE_CACHE(re, &s->gb);
257 level = SHOW_SBITS(re, &s->gb, 8);
258 SKIP_BITS(re, &s->gb, 8);
260 level = SHOW_UBITS(re, &s->gb, 8) - 256;
261 SKIP_BITS(re, &s->gb, 8);
262 } else if (level == 0) {
263 level = SHOW_UBITS(re, &s->gb, 8);
264 SKIP_BITS(re, &s->gb, 8);
267 check_scantable_index(s, i);
271 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
272 level = (level - 1) | 1;
275 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
276 level = (level - 1) | 1;
281 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
283 UPDATE_CACHE(re, &s->gb);
286 LAST_SKIP_BITS(re, &s->gb, 2);
287 CLOSE_READER(re, &s->gb);
289 s->block_last_index[n] = i;
293 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s,
294 int16_t *block, int n)
296 int level, i, j, run;
297 RLTable *rl = &ff_rl_mpeg1;
298 uint8_t *const scantable = s->intra_scantable.permutated;
299 const int qscale = s->qscale;
302 OPEN_READER(re, &s->gb);
304 // Special case for first coefficient, no need to add second VLC table.
305 UPDATE_CACHE(re, &s->gb);
306 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
307 level = (3 * qscale) >> 1;
308 level = (level - 1) | 1;
309 if (GET_CACHE(re, &s->gb) & 0x40000000)
313 SKIP_BITS(re, &s->gb, 2);
314 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
318 /* now quantify & encode AC coefficients */
320 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
325 check_scantable_index(s, i);
327 level = ((level * 2 + 1) * qscale) >> 1;
328 level = (level - 1) | 1;
329 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
330 SHOW_SBITS(re, &s->gb, 1);
331 SKIP_BITS(re, &s->gb, 1);
334 run = SHOW_UBITS(re, &s->gb, 6) + 1;
335 LAST_SKIP_BITS(re, &s->gb, 6);
336 UPDATE_CACHE(re, &s->gb);
337 level = SHOW_SBITS(re, &s->gb, 8);
338 SKIP_BITS(re, &s->gb, 8);
340 level = SHOW_UBITS(re, &s->gb, 8) - 256;
341 SKIP_BITS(re, &s->gb, 8);
342 } else if (level == 0) {
343 level = SHOW_UBITS(re, &s->gb, 8);
344 SKIP_BITS(re, &s->gb, 8);
347 check_scantable_index(s, i);
351 level = ((level * 2 + 1) * qscale) >> 1;
352 level = (level - 1) | 1;
355 level = ((level * 2 + 1) * qscale) >> 1;
356 level = (level - 1) | 1;
361 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
363 UPDATE_CACHE(re, &s->gb);
366 LAST_SKIP_BITS(re, &s->gb, 2);
367 CLOSE_READER(re, &s->gb);
369 s->block_last_index[n] = i;
373 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
374 int16_t *block, int n)
376 int level, i, j, run;
377 RLTable *rl = &ff_rl_mpeg1;
378 uint8_t *const scantable = s->intra_scantable.permutated;
379 const uint16_t *quant_matrix;
380 const int qscale = s->qscale;
386 OPEN_READER(re, &s->gb);
389 quant_matrix = s->inter_matrix;
391 quant_matrix = s->chroma_inter_matrix;
393 // Special case for first coefficient, no need to add second VLC table.
394 UPDATE_CACHE(re, &s->gb);
395 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
396 level = (3 * qscale * quant_matrix[0]) >> 5;
397 if (GET_CACHE(re, &s->gb) & 0x40000000)
402 SKIP_BITS(re, &s->gb, 2);
403 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
407 /* now quantify & encode AC coefficients */
409 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
414 check_scantable_index(s, i);
416 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
417 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
418 SHOW_SBITS(re, &s->gb, 1);
419 SKIP_BITS(re, &s->gb, 1);
422 run = SHOW_UBITS(re, &s->gb, 6) + 1;
423 LAST_SKIP_BITS(re, &s->gb, 6);
424 UPDATE_CACHE(re, &s->gb);
425 level = SHOW_SBITS(re, &s->gb, 12);
426 SKIP_BITS(re, &s->gb, 12);
429 check_scantable_index(s, i);
432 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
435 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
441 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
443 UPDATE_CACHE(re, &s->gb);
446 LAST_SKIP_BITS(re, &s->gb, 2);
447 CLOSE_READER(re, &s->gb);
449 block[63] ^= (mismatch & 1);
451 s->block_last_index[n] = i;
455 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
456 int16_t *block, int n)
458 int level, i, j, run;
459 RLTable *rl = &ff_rl_mpeg1;
460 uint8_t *const scantable = s->intra_scantable.permutated;
461 const int qscale = s->qscale;
462 OPEN_READER(re, &s->gb);
465 // special case for first coefficient, no need to add second VLC table
466 UPDATE_CACHE(re, &s->gb);
467 if (((int32_t) GET_CACHE(re, &s->gb)) < 0) {
468 level = (3 * qscale) >> 1;
469 if (GET_CACHE(re, &s->gb) & 0x40000000)
473 SKIP_BITS(re, &s->gb, 2);
474 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
478 /* now quantify & encode AC coefficients */
480 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
484 check_scantable_index(s, i);
486 level = ((level * 2 + 1) * qscale) >> 1;
487 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
488 SHOW_SBITS(re, &s->gb, 1);
489 SKIP_BITS(re, &s->gb, 1);
492 run = SHOW_UBITS(re, &s->gb, 6) + 1;
493 LAST_SKIP_BITS(re, &s->gb, 6);
494 UPDATE_CACHE(re, &s->gb);
495 level = SHOW_SBITS(re, &s->gb, 12);
496 SKIP_BITS(re, &s->gb, 12);
499 check_scantable_index(s, i);
502 level = ((-level * 2 + 1) * qscale) >> 1;
505 level = ((level * 2 + 1) * qscale) >> 1;
510 if (((int32_t) GET_CACHE(re, &s->gb)) <= (int32_t) 0xBFFFFFFF)
512 UPDATE_CACHE(re, &s->gb);
515 LAST_SKIP_BITS(re, &s->gb, 2);
516 CLOSE_READER(re, &s->gb);
517 s->block_last_index[n] = i;
521 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
522 int16_t *block, int n)
524 int level, dc, diff, i, j, run;
527 uint8_t *const scantable = s->intra_scantable.permutated;
528 const uint16_t *quant_matrix;
529 const int qscale = s->qscale;
534 quant_matrix = s->intra_matrix;
537 quant_matrix = s->chroma_intra_matrix;
538 component = (n & 1) + 1;
540 diff = decode_dc(&s->gb, component);
543 dc = s->last_dc[component];
545 s->last_dc[component] = dc;
546 block[0] = dc << (3 - s->intra_dc_precision);
547 av_dlog(s->avctx, "dc=%d\n", block[0]);
548 mismatch = block[0] ^ 1;
550 if (s->intra_vlc_format)
556 OPEN_READER(re, &s->gb);
557 /* now quantify & encode AC coefficients */
559 UPDATE_CACHE(re, &s->gb);
560 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
565 } else if (level != 0) {
567 check_scantable_index(s, i);
569 level = (level * qscale * quant_matrix[j]) >> 4;
570 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
571 SHOW_SBITS(re, &s->gb, 1);
572 LAST_SKIP_BITS(re, &s->gb, 1);
575 run = SHOW_UBITS(re, &s->gb, 6) + 1;
576 LAST_SKIP_BITS(re, &s->gb, 6);
577 UPDATE_CACHE(re, &s->gb);
578 level = SHOW_SBITS(re, &s->gb, 12);
579 SKIP_BITS(re, &s->gb, 12);
581 check_scantable_index(s, i);
584 level = (-level * qscale * quant_matrix[j]) >> 4;
587 level = (level * qscale * quant_matrix[j]) >> 4;
594 CLOSE_READER(re, &s->gb);
596 block[63] ^= mismatch & 1;
598 s->block_last_index[n] = i;
602 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
603 int16_t *block, int n)
605 int level, dc, diff, i, j, run;
608 uint8_t *const scantable = s->intra_scantable.permutated;
609 const uint16_t *quant_matrix;
610 const int qscale = s->qscale;
614 quant_matrix = s->intra_matrix;
617 quant_matrix = s->chroma_intra_matrix;
618 component = (n & 1) + 1;
620 diff = decode_dc(&s->gb, component);
623 dc = s->last_dc[component];
625 s->last_dc[component] = dc;
626 block[0] = dc << (3 - s->intra_dc_precision);
628 if (s->intra_vlc_format)
634 OPEN_READER(re, &s->gb);
635 /* now quantify & encode AC coefficients */
637 UPDATE_CACHE(re, &s->gb);
638 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0],
643 } else if (level != 0) {
645 check_scantable_index(s, i);
647 level = (level * qscale * quant_matrix[j]) >> 4;
648 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) -
649 SHOW_SBITS(re, &s->gb, 1);
650 LAST_SKIP_BITS(re, &s->gb, 1);
653 run = SHOW_UBITS(re, &s->gb, 6) + 1;
654 LAST_SKIP_BITS(re, &s->gb, 6);
655 UPDATE_CACHE(re, &s->gb);
656 level = SHOW_SBITS(re, &s->gb, 12);
657 SKIP_BITS(re, &s->gb, 12);
659 check_scantable_index(s, i);
662 level = (-level * qscale * quant_matrix[j]) >> 4;
665 level = (level * qscale * quant_matrix[j]) >> 4;
671 CLOSE_READER(re, &s->gb);
674 s->block_last_index[n] = i;
678 /******************************************/
681 static inline int get_dmv(MpegEncContext *s)
683 if (get_bits1(&s->gb))
684 return 1 - (get_bits1(&s->gb) << 1);
689 static inline int get_qscale(MpegEncContext *s)
691 int qscale = get_bits(&s->gb, 5);
693 return non_linear_qscale[qscale];
698 /* motion type (for MPEG-2) */
704 static int mpeg_decode_mb(MpegEncContext *s, int16_t block[12][64])
706 int i, j, k, cbp, val, mb_type, motion_type;
707 const int mb_block_count = 4 + (1 << s->chroma_format);
709 av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
711 assert(s->mb_skipped == 0);
713 if (s->mb_skip_run-- != 0) {
714 if (s->pict_type == AV_PICTURE_TYPE_P) {
716 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
717 MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
722 mb_type = s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
724 // FIXME not sure if this is allowed in MPEG at all
725 mb_type = s->current_picture.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1];
726 if (IS_INTRA(mb_type))
728 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] =
729 mb_type | MB_TYPE_SKIP;
730 // assert(s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1] & (MB_TYPE_16x16 | MB_TYPE_16x8));
732 if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
739 switch (s->pict_type) {
741 case AV_PICTURE_TYPE_I:
742 if (get_bits1(&s->gb) == 0) {
743 if (get_bits1(&s->gb) == 0) {
744 av_log(s->avctx, AV_LOG_ERROR,
745 "invalid mb type in I Frame at %d %d\n",
749 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
751 mb_type = MB_TYPE_INTRA;
754 case AV_PICTURE_TYPE_P:
755 mb_type = get_vlc2(&s->gb, ff_mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
757 av_log(s->avctx, AV_LOG_ERROR,
758 "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
761 mb_type = ptype2mb_type[mb_type];
763 case AV_PICTURE_TYPE_B:
764 mb_type = get_vlc2(&s->gb, ff_mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
766 av_log(s->avctx, AV_LOG_ERROR,
767 "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
770 mb_type = btype2mb_type[mb_type];
773 av_dlog(s->avctx, "mb_type=%x\n", mb_type);
774 // motion_type = 0; /* avoid warning */
775 if (IS_INTRA(mb_type)) {
776 s->dsp.clear_blocks(s->block[0]);
778 if (!s->chroma_y_shift)
779 s->dsp.clear_blocks(s->block[6]);
781 /* compute DCT type */
782 // FIXME: add an interlaced_dct coded var?
783 if (s->picture_structure == PICT_FRAME &&
784 !s->frame_pred_frame_dct)
785 s->interlaced_dct = get_bits1(&s->gb);
787 if (IS_QUANT(mb_type))
788 s->qscale = get_qscale(s);
790 if (s->concealment_motion_vectors) {
791 /* just parse them */
792 if (s->picture_structure != PICT_FRAME)
793 skip_bits1(&s->gb); /* field select */
796 s->last_mv[0][0][0] =
797 s->last_mv[0][1][0] = mpeg_decode_motion(s, s->mpeg_f_code[0][0],
798 s->last_mv[0][0][0]);
800 s->last_mv[0][0][1] =
801 s->last_mv[0][1][1] = mpeg_decode_motion(s, s->mpeg_f_code[0][1],
802 s->last_mv[0][0][1]);
804 skip_bits1(&s->gb); /* marker */
806 /* reset mv prediction */
807 memset(s->last_mv, 0, sizeof(s->last_mv));
811 FF_DISABLE_DEPRECATION_WARNINGS
812 // if 1, we memcpy blocks in xvmcvideo
813 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
814 ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
815 FF_ENABLE_DEPRECATION_WARNINGS
816 #endif /* FF_API_XVMC */
818 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
819 if (s->flags2 & CODEC_FLAG2_FAST) {
820 for (i = 0; i < 6; i++)
821 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
823 for (i = 0; i < mb_block_count; i++)
824 if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
828 for (i = 0; i < 6; i++)
829 if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
833 if (mb_type & MB_TYPE_ZERO_MV) {
834 assert(mb_type & MB_TYPE_CBP);
836 s->mv_dir = MV_DIR_FORWARD;
837 if (s->picture_structure == PICT_FRAME) {
838 if (!s->frame_pred_frame_dct)
839 s->interlaced_dct = get_bits1(&s->gb);
840 s->mv_type = MV_TYPE_16X16;
842 s->mv_type = MV_TYPE_FIELD;
843 mb_type |= MB_TYPE_INTERLACED;
844 s->field_select[0][0] = s->picture_structure - 1;
847 if (IS_QUANT(mb_type))
848 s->qscale = get_qscale(s);
850 s->last_mv[0][0][0] = 0;
851 s->last_mv[0][0][1] = 0;
852 s->last_mv[0][1][0] = 0;
853 s->last_mv[0][1][1] = 0;
857 assert(mb_type & MB_TYPE_L0L1);
858 // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
859 /* get additional motion vector type */
860 if (s->frame_pred_frame_dct) {
861 motion_type = MT_FRAME;
863 motion_type = get_bits(&s->gb, 2);
864 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
865 s->interlaced_dct = get_bits1(&s->gb);
868 if (IS_QUANT(mb_type))
869 s->qscale = get_qscale(s);
872 s->mv_dir = (mb_type >> 13) & 3;
873 av_dlog(s->avctx, "motion_type=%d\n", motion_type);
874 switch (motion_type) {
875 case MT_FRAME: /* or MT_16X8 */
876 if (s->picture_structure == PICT_FRAME) {
877 mb_type |= MB_TYPE_16x16;
878 s->mv_type = MV_TYPE_16X16;
879 for (i = 0; i < 2; i++) {
880 if (USES_LIST(mb_type, i)) {
883 s->last_mv[i][0][0] =
884 s->last_mv[i][1][0] =
885 mpeg_decode_motion(s, s->mpeg_f_code[i][0],
886 s->last_mv[i][0][0]);
888 s->last_mv[i][0][1] =
889 s->last_mv[i][1][1] =
890 mpeg_decode_motion(s, s->mpeg_f_code[i][1],
891 s->last_mv[i][0][1]);
892 /* full_pel: only for MPEG-1 */
893 if (s->full_pel[i]) {
894 s->mv[i][0][0] <<= 1;
895 s->mv[i][0][1] <<= 1;
900 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
901 s->mv_type = MV_TYPE_16X8;
902 for (i = 0; i < 2; i++) {
903 if (USES_LIST(mb_type, i)) {
905 for (j = 0; j < 2; j++) {
906 s->field_select[i][j] = get_bits1(&s->gb);
907 for (k = 0; k < 2; k++) {
908 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
909 s->last_mv[i][j][k]);
910 s->last_mv[i][j][k] = val;
911 s->mv[i][j][k] = val;
919 s->mv_type = MV_TYPE_FIELD;
920 if (s->picture_structure == PICT_FRAME) {
921 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
922 for (i = 0; i < 2; i++) {
923 if (USES_LIST(mb_type, i)) {
924 for (j = 0; j < 2; j++) {
925 s->field_select[i][j] = get_bits1(&s->gb);
926 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
927 s->last_mv[i][j][0]);
928 s->last_mv[i][j][0] = val;
929 s->mv[i][j][0] = val;
930 av_dlog(s->avctx, "fmx=%d\n", val);
931 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
932 s->last_mv[i][j][1] >> 1);
933 s->last_mv[i][j][1] = val << 1;
934 s->mv[i][j][1] = val;
935 av_dlog(s->avctx, "fmy=%d\n", val);
940 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
941 for (i = 0; i < 2; i++) {
942 if (USES_LIST(mb_type, i)) {
943 s->field_select[i][0] = get_bits1(&s->gb);
944 for (k = 0; k < 2; k++) {
945 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
946 s->last_mv[i][0][k]);
947 s->last_mv[i][0][k] = val;
948 s->last_mv[i][1][k] = val;
949 s->mv[i][0][k] = val;
956 s->mv_type = MV_TYPE_DMV;
957 for (i = 0; i < 2; i++) {
958 if (USES_LIST(mb_type, i)) {
959 int dmx, dmy, mx, my, m;
960 const int my_shift = s->picture_structure == PICT_FRAME;
962 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
963 s->last_mv[i][0][0]);
964 s->last_mv[i][0][0] = mx;
965 s->last_mv[i][1][0] = mx;
967 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
968 s->last_mv[i][0][1] >> my_shift);
972 s->last_mv[i][0][1] = my << my_shift;
973 s->last_mv[i][1][1] = my << my_shift;
977 s->mv[i][1][0] = mx; // not used
978 s->mv[i][1][1] = my; // not used
980 if (s->picture_structure == PICT_FRAME) {
981 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
983 // m = 1 + 2 * s->top_field_first;
984 m = s->top_field_first ? 1 : 3;
986 /* top -> top pred */
987 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
988 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
990 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
991 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
993 mb_type |= MB_TYPE_16x16;
995 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
996 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
997 if (s->picture_structure == PICT_TOP_FIELD)
1006 av_log(s->avctx, AV_LOG_ERROR,
1007 "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1013 if (HAS_CBP(mb_type)) {
1014 s->dsp.clear_blocks(s->block[0]);
1016 cbp = get_vlc2(&s->gb, ff_mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1017 if (mb_block_count > 6) {
1018 cbp <<= mb_block_count - 6;
1019 cbp |= get_bits(&s->gb, mb_block_count - 6);
1020 s->dsp.clear_blocks(s->block[6]);
1023 av_log(s->avctx, AV_LOG_ERROR,
1024 "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
1029 FF_DISABLE_DEPRECATION_WARNINGS
1030 // if 1, we memcpy blocks in xvmcvideo
1031 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1032 ff_xvmc_pack_pblocks(s, cbp);
1033 FF_ENABLE_DEPRECATION_WARNINGS
1034 #endif /* FF_API_XVMC */
1036 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1037 if (s->flags2 & CODEC_FLAG2_FAST) {
1038 for (i = 0; i < 6; i++) {
1040 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
1042 s->block_last_index[i] = -1;
1046 cbp <<= 12 - mb_block_count;
1048 for (i = 0; i < mb_block_count; i++) {
1049 if (cbp & (1 << 11)) {
1050 if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
1053 s->block_last_index[i] = -1;
1059 if (s->flags2 & CODEC_FLAG2_FAST) {
1060 for (i = 0; i < 6; i++) {
1062 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
1064 s->block_last_index[i] = -1;
1068 for (i = 0; i < 6; i++) {
1070 if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
1073 s->block_last_index[i] = -1;
1080 for (i = 0; i < 12; i++)
1081 s->block_last_index[i] = -1;
1085 s->current_picture.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
1090 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1092 Mpeg1Context *s = avctx->priv_data;
1093 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1096 /* we need some permutation to store matrices,
1097 * until MPV_common_init() sets the real permutation. */
1098 for (i = 0; i < 64; i++)
1099 s2->dsp.idct_permutation[i] = i;
1101 ff_MPV_decode_defaults(s2);
1103 s->mpeg_enc_ctx.avctx = avctx;
1104 s->mpeg_enc_ctx.flags = avctx->flags;
1105 s->mpeg_enc_ctx.flags2 = avctx->flags2;
1106 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1107 ff_mpeg12_init_vlcs();
1109 s->mpeg_enc_ctx_allocated = 0;
1110 s->mpeg_enc_ctx.picture_number = 0;
1111 s->repeat_field = 0;
1112 s->mpeg_enc_ctx.codec_id = avctx->codec->id;
1113 avctx->color_range = AVCOL_RANGE_MPEG;
1114 if (avctx->codec->id == AV_CODEC_ID_MPEG1VIDEO)
1115 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1117 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1121 static int mpeg_decode_update_thread_context(AVCodecContext *avctx,
1122 const AVCodecContext *avctx_from)
1124 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1125 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1128 if (avctx == avctx_from ||
1129 !ctx_from->mpeg_enc_ctx_allocated ||
1130 !s1->context_initialized)
1133 err = ff_mpeg_update_thread_context(avctx, avctx_from);
1137 if (!ctx->mpeg_enc_ctx_allocated)
1138 memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
1140 if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
1141 s->picture_number++;
1146 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1147 const uint8_t *new_perm)
1149 uint16_t temp_matrix[64];
1152 memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
1154 for (i = 0; i < 64; i++)
1155 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1159 static const enum AVPixelFormat pixfmt_xvmc_mpg2_420[] = {
1160 AV_PIX_FMT_XVMC_MPEG2_IDCT,
1161 AV_PIX_FMT_XVMC_MPEG2_MC,
1164 #endif /* FF_API_XVMC */
1166 static const enum AVPixelFormat mpeg12_hwaccel_pixfmt_list_420[] = {
1167 #if CONFIG_MPEG2_DXVA2_HWACCEL
1168 AV_PIX_FMT_DXVA2_VLD,
1170 #if CONFIG_MPEG2_VAAPI_HWACCEL
1171 AV_PIX_FMT_VAAPI_VLD,
1173 #if CONFIG_MPEG1_VDPAU_HWACCEL | CONFIG_MPEG2_VDPAU_HWACCEL
1180 static enum AVPixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
1182 Mpeg1Context *s1 = avctx->priv_data;
1183 MpegEncContext *s = &s1->mpeg_enc_ctx;
1186 FF_DISABLE_DEPRECATION_WARNINGS
1187 if (avctx->xvmc_acceleration)
1188 return avctx->get_format(avctx, pixfmt_xvmc_mpg2_420);
1189 FF_ENABLE_DEPRECATION_WARNINGS
1190 #endif /* FF_API_XVMC */
1192 if (s->chroma_format < 2)
1193 return avctx->get_format(avctx, mpeg12_hwaccel_pixfmt_list_420);
1194 else if (s->chroma_format == 2)
1195 return AV_PIX_FMT_YUV422P;
1197 return AV_PIX_FMT_YUV444P;
1200 /* Call this function when we know all parameters.
1201 * It may be called in different places for MPEG-1 and MPEG-2. */
1202 static int mpeg_decode_postinit(AVCodecContext *avctx)
1204 Mpeg1Context *s1 = avctx->priv_data;
1205 MpegEncContext *s = &s1->mpeg_enc_ctx;
1206 uint8_t old_permutation[64];
1209 if ((s1->mpeg_enc_ctx_allocated == 0) ||
1210 avctx->coded_width != s->width ||
1211 avctx->coded_height != s->height ||
1212 s1->save_width != s->width ||
1213 s1->save_height != s->height ||
1214 s1->save_aspect_info != s->aspect_ratio_info ||
1215 s1->save_progressive_seq != s->progressive_sequence ||
1217 if (s1->mpeg_enc_ctx_allocated) {
1218 ParseContext pc = s->parse_context;
1219 s->parse_context.buffer = 0;
1220 ff_MPV_common_end(s);
1221 s->parse_context = pc;
1224 if ((s->width == 0) || (s->height == 0))
1227 ret = ff_set_dimensions(avctx, s->width, s->height);
1231 avctx->bit_rate = s->bit_rate;
1232 s1->save_aspect_info = s->aspect_ratio_info;
1233 s1->save_width = s->width;
1234 s1->save_height = s->height;
1235 s1->save_progressive_seq = s->progressive_sequence;
1237 /* low_delay may be forced, in this case we will have B-frames
1238 * that behave like P-frames. */
1239 avctx->has_b_frames = !s->low_delay;
1241 if (avctx->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
1243 avctx->time_base.den = ff_mpeg12_frame_rate_tab[s->frame_rate_index].num;
1244 avctx->time_base.num = ff_mpeg12_frame_rate_tab[s->frame_rate_index].den;
1246 avctx->sample_aspect_ratio = av_d2q(1.0 / ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1247 avctx->ticks_per_frame = 1;
1250 av_reduce(&s->avctx->time_base.den,
1251 &s->avctx->time_base.num,
1252 ff_mpeg12_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num * 2,
1253 ff_mpeg12_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1255 avctx->ticks_per_frame = 2;
1257 if (s->aspect_ratio_info > 1) {
1259 av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1260 (AVRational) { s1->pan_scan.width,
1261 s1->pan_scan.height }),
1262 (AVRational) { s->width, s->height });
1264 /* We ignore the spec here and guess a bit as reality does not
1265 * match the spec, see for example res_change_ffmpeg_aspect.ts
1266 * and sequence-display-aspect.mpg.
1267 * issue1613, 621, 562 */
1268 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1269 (av_cmp_q(dar, (AVRational) { 4, 3 }) &&
1270 av_cmp_q(dar, (AVRational) { 16, 9 }))) {
1271 s->avctx->sample_aspect_ratio =
1272 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1273 (AVRational) { s->width, s->height });
1275 s->avctx->sample_aspect_ratio =
1276 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1277 (AVRational) { s1->pan_scan.width, s1->pan_scan.height });
1278 // issue1613 4/3 16/9 -> 16/9
1279 // res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1280 // widescreen-issue562.mpg 4/3 16/9 -> 16/9
1281 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1282 av_dlog(avctx, "A %d/%d\n",
1283 ff_mpeg2_aspect[s->aspect_ratio_info].num,
1284 ff_mpeg2_aspect[s->aspect_ratio_info].den);
1285 av_dlog(avctx, "B %d/%d\n", s->avctx->sample_aspect_ratio.num,
1286 s->avctx->sample_aspect_ratio.den);
1289 s->avctx->sample_aspect_ratio =
1290 ff_mpeg2_aspect[s->aspect_ratio_info];
1294 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1295 avctx->hwaccel = ff_find_hwaccel(avctx);
1296 // until then pix_fmt may be changed right after codec init
1298 if ((avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT ||
1299 avctx->hwaccel) && avctx->idct_algo == FF_IDCT_AUTO)
1301 if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
1302 #endif /* FF_API_XVMC */
1303 avctx->idct_algo = FF_IDCT_SIMPLE;
1305 /* Quantization matrices may need reordering
1306 * if DCT permutation is changed. */
1307 memcpy(old_permutation, s->dsp.idct_permutation, 64 * sizeof(uint8_t));
1309 if (ff_MPV_common_init(s) < 0)
1312 quant_matrix_rebuild(s->intra_matrix, old_permutation, s->dsp.idct_permutation);
1313 quant_matrix_rebuild(s->inter_matrix, old_permutation, s->dsp.idct_permutation);
1314 quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->dsp.idct_permutation);
1315 quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->dsp.idct_permutation);
1317 s1->mpeg_enc_ctx_allocated = 1;
1322 static int mpeg1_decode_picture(AVCodecContext *avctx, const uint8_t *buf,
1325 Mpeg1Context *s1 = avctx->priv_data;
1326 MpegEncContext *s = &s1->mpeg_enc_ctx;
1327 int ref, f_code, vbv_delay;
1329 init_get_bits(&s->gb, buf, buf_size * 8);
1331 ref = get_bits(&s->gb, 10); /* temporal ref */
1332 s->pict_type = get_bits(&s->gb, 3);
1333 if (s->pict_type == 0 || s->pict_type > 3)
1336 vbv_delay = get_bits(&s->gb, 16);
1337 if (s->pict_type == AV_PICTURE_TYPE_P ||
1338 s->pict_type == AV_PICTURE_TYPE_B) {
1339 s->full_pel[0] = get_bits1(&s->gb);
1340 f_code = get_bits(&s->gb, 3);
1341 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1343 s->mpeg_f_code[0][0] = f_code;
1344 s->mpeg_f_code[0][1] = f_code;
1346 if (s->pict_type == AV_PICTURE_TYPE_B) {
1347 s->full_pel[1] = get_bits1(&s->gb);
1348 f_code = get_bits(&s->gb, 3);
1349 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1351 s->mpeg_f_code[1][0] = f_code;
1352 s->mpeg_f_code[1][1] = f_code;
1354 s->current_picture.f.pict_type = s->pict_type;
1355 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1357 if (avctx->debug & FF_DEBUG_PICT_INFO)
1358 av_log(avctx, AV_LOG_DEBUG,
1359 "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1366 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1368 MpegEncContext *s = &s1->mpeg_enc_ctx;
1369 int horiz_size_ext, vert_size_ext;
1372 skip_bits(&s->gb, 1); /* profile and level esc*/
1373 s->avctx->profile = get_bits(&s->gb, 3);
1374 s->avctx->level = get_bits(&s->gb, 4);
1375 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1376 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1377 horiz_size_ext = get_bits(&s->gb, 2);
1378 vert_size_ext = get_bits(&s->gb, 2);
1379 s->width |= (horiz_size_ext << 12);
1380 s->height |= (vert_size_ext << 12);
1381 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1382 s->bit_rate += (bit_rate_ext << 18) * 400;
1383 skip_bits1(&s->gb); /* marker */
1384 s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1386 s->low_delay = get_bits1(&s->gb);
1387 if (s->flags & CODEC_FLAG_LOW_DELAY)
1390 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1391 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1393 av_dlog(s->avctx, "sequence extension\n");
1394 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
1396 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1397 av_log(s->avctx, AV_LOG_DEBUG,
1398 "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1399 s->avctx->profile, s->avctx->level,
1400 s->avctx->rc_buffer_size, s->bit_rate);
1403 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1405 MpegEncContext *s = &s1->mpeg_enc_ctx;
1406 int color_description, w, h;
1408 skip_bits(&s->gb, 3); /* video format */
1409 color_description = get_bits1(&s->gb);
1410 if (color_description) {
1411 s->avctx->color_primaries = get_bits(&s->gb, 8);
1412 s->avctx->color_trc = get_bits(&s->gb, 8);
1413 s->avctx->colorspace = get_bits(&s->gb, 8);
1415 w = get_bits(&s->gb, 14);
1416 skip_bits(&s->gb, 1); // marker
1417 h = get_bits(&s->gb, 14);
1418 // remaining 3 bits are zero padding
1420 s1->pan_scan.width = 16 * w;
1421 s1->pan_scan.height = 16 * h;
1423 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1424 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1427 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1429 MpegEncContext *s = &s1->mpeg_enc_ctx;
1433 if (s->progressive_sequence) {
1434 if (s->repeat_first_field) {
1436 if (s->top_field_first)
1440 if (s->picture_structure == PICT_FRAME) {
1442 if (s->repeat_first_field)
1446 for (i = 0; i < nofco; i++) {
1447 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1448 skip_bits(&s->gb, 1); // marker
1449 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1450 skip_bits(&s->gb, 1); // marker
1453 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1454 av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
1455 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1456 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1457 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1460 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64],
1461 uint16_t matrix1[64], int intra)
1465 for (i = 0; i < 64; i++) {
1466 int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1467 int v = get_bits(&s->gb, 8);
1469 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1472 if (intra && i == 0 && v != 8) {
1473 av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
1474 v = 8; // needed by pink.mpg / issue1046
1483 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1485 av_dlog(s->avctx, "matrix extension\n");
1487 if (get_bits1(&s->gb))
1488 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1489 if (get_bits1(&s->gb))
1490 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1491 if (get_bits1(&s->gb))
1492 load_matrix(s, s->chroma_intra_matrix, NULL, 1);
1493 if (get_bits1(&s->gb))
1494 load_matrix(s, s->chroma_inter_matrix, NULL, 0);
1497 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1499 MpegEncContext *s = &s1->mpeg_enc_ctx;
1501 s->full_pel[0] = s->full_pel[1] = 0;
1502 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1503 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1504 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1505 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1506 if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1507 av_log(s->avctx, AV_LOG_ERROR,
1508 "Missing picture start code, guessing missing values\n");
1509 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1510 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1511 s->pict_type = AV_PICTURE_TYPE_I;
1513 s->pict_type = AV_PICTURE_TYPE_P;
1515 s->pict_type = AV_PICTURE_TYPE_B;
1516 s->current_picture.f.pict_type = s->pict_type;
1517 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1519 s->intra_dc_precision = get_bits(&s->gb, 2);
1520 s->picture_structure = get_bits(&s->gb, 2);
1521 s->top_field_first = get_bits1(&s->gb);
1522 s->frame_pred_frame_dct = get_bits1(&s->gb);
1523 s->concealment_motion_vectors = get_bits1(&s->gb);
1524 s->q_scale_type = get_bits1(&s->gb);
1525 s->intra_vlc_format = get_bits1(&s->gb);
1526 s->alternate_scan = get_bits1(&s->gb);
1527 s->repeat_first_field = get_bits1(&s->gb);
1528 s->chroma_420_type = get_bits1(&s->gb);
1529 s->progressive_frame = get_bits1(&s->gb);
1531 if (s->progressive_sequence && !s->progressive_frame) {
1532 s->progressive_frame = 1;
1533 av_log(s->avctx, AV_LOG_ERROR,
1534 "interlaced frame in progressive sequence, ignoring\n");
1537 if (s->picture_structure == 0 ||
1538 (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
1539 av_log(s->avctx, AV_LOG_ERROR,
1540 "picture_structure %d invalid, ignoring\n",
1541 s->picture_structure);
1542 s->picture_structure = PICT_FRAME;
1545 if (s->progressive_sequence && !s->frame_pred_frame_dct)
1546 av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
1548 if (s->picture_structure == PICT_FRAME) {
1550 s->v_edge_pos = 16 * s->mb_height;
1552 s->first_field ^= 1;
1553 s->v_edge_pos = 8 * s->mb_height;
1554 memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
1557 if (s->alternate_scan) {
1558 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
1559 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
1561 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
1562 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
1565 /* composite display not parsed */
1566 av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1567 av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1568 av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1569 av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1570 av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1571 av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1572 av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1573 av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1574 av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1577 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1579 AVCodecContext *avctx = s->avctx;
1580 Mpeg1Context *s1 = (Mpeg1Context *) s;
1582 /* start frame decoding */
1583 if (s->first_field || s->picture_structure == PICT_FRAME) {
1584 AVFrameSideData *pan_scan;
1586 if (ff_MPV_frame_start(s, avctx) < 0)
1589 ff_mpeg_er_frame_start(s);
1591 /* first check if we must repeat the frame */
1592 s->current_picture_ptr->f.repeat_pict = 0;
1593 if (s->repeat_first_field) {
1594 if (s->progressive_sequence) {
1595 if (s->top_field_first)
1596 s->current_picture_ptr->f.repeat_pict = 4;
1598 s->current_picture_ptr->f.repeat_pict = 2;
1599 } else if (s->progressive_frame) {
1600 s->current_picture_ptr->f.repeat_pict = 1;
1604 pan_scan = av_frame_new_side_data(&s->current_picture_ptr->f,
1605 AV_FRAME_DATA_PANSCAN,
1606 sizeof(s1->pan_scan));
1608 return AVERROR(ENOMEM);
1609 memcpy(pan_scan->data, &s1->pan_scan, sizeof(s1->pan_scan));
1611 if (s1->a53_caption) {
1612 AVFrameSideData *sd = av_frame_new_side_data(
1613 &s->current_picture_ptr->f, AV_FRAME_DATA_A53_CC,
1614 s1->a53_caption_size);
1616 memcpy(sd->data, s1->a53_caption, s1->a53_caption_size);
1617 av_freep(&s1->a53_caption);
1620 if (s1->has_stereo3d) {
1621 AVStereo3D *stereo = av_stereo3d_create_side_data(&s->current_picture_ptr->f);
1623 return AVERROR(ENOMEM);
1625 *stereo = s1->stereo3d;
1626 s1->has_stereo3d = 0;
1628 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1629 ff_thread_finish_setup(avctx);
1630 } else { // second field
1633 if (!s->current_picture_ptr) {
1634 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1638 if (s->avctx->hwaccel &&
1639 (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
1640 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1641 av_log(avctx, AV_LOG_ERROR,
1642 "hardware accelerator failed to decode first field\n");
1645 for (i = 0; i < 4; i++) {
1646 s->current_picture.f.data[i] = s->current_picture_ptr->f.data[i];
1647 if (s->picture_structure == PICT_BOTTOM_FIELD)
1648 s->current_picture.f.data[i] +=
1649 s->current_picture_ptr->f.linesize[i];
1653 if (avctx->hwaccel) {
1654 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
1659 FF_DISABLE_DEPRECATION_WARNINGS
1660 // MPV_frame_start will call this function too,
1661 // but we need to call it on every field
1662 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1663 if (ff_xvmc_field_start(s, avctx) < 0)
1665 FF_ENABLE_DEPRECATION_WARNINGS
1666 #endif /* FF_API_XVMC */
1671 #define DECODE_SLICE_ERROR -1
1672 #define DECODE_SLICE_OK 0
1676 * MpegEncContext.mb_y must be set to the MB row from the startcode.
1677 * @return DECODE_SLICE_ERROR if the slice is damaged,
1678 * DECODE_SLICE_OK if this slice is OK
1680 static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
1681 const uint8_t **buf, int buf_size)
1683 AVCodecContext *avctx = s->avctx;
1684 const int field_pic = s->picture_structure != PICT_FRAME;
1687 s->resync_mb_y = -1;
1689 assert(mb_y < s->mb_height);
1691 init_get_bits(&s->gb, *buf, buf_size * 8);
1693 ff_mpeg1_clean_buffers(s);
1694 s->interlaced_dct = 0;
1696 s->qscale = get_qscale(s);
1698 if (s->qscale == 0) {
1699 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1703 /* extra slice info */
1704 while (get_bits1(&s->gb) != 0)
1705 skip_bits(&s->gb, 8);
1709 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1712 while (get_bits_left(&s->gb) > 0) {
1713 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1714 MBINCR_VLC_BITS, 2);
1716 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1722 /* otherwise, stuffing, nothing to do */
1730 if (s->mb_x >= (unsigned) s->mb_width) {
1731 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1735 if (avctx->hwaccel) {
1736 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1737 int start_code = -1;
1738 buf_end = avpriv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1739 if (buf_end < *buf + buf_size)
1742 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1743 return DECODE_SLICE_ERROR;
1745 return DECODE_SLICE_OK;
1748 s->resync_mb_x = s->mb_x;
1749 s->resync_mb_y = s->mb_y = mb_y;
1751 ff_init_block_index(s);
1753 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1754 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1755 av_log(s->avctx, AV_LOG_DEBUG,
1756 "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",
1758 s->mpeg_f_code[0][0], s->mpeg_f_code[0][1],
1759 s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1760 s->pict_type == AV_PICTURE_TYPE_I ? "I" :
1761 (s->pict_type == AV_PICTURE_TYPE_P ? "P" :
1762 (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
1763 s->progressive_sequence ? "ps" : "",
1764 s->progressive_frame ? "pf" : "",
1765 s->alternate_scan ? "alt" : "",
1766 s->top_field_first ? "top" : "",
1767 s->intra_dc_precision, s->picture_structure,
1768 s->frame_pred_frame_dct, s->concealment_motion_vectors,
1769 s->q_scale_type, s->intra_vlc_format,
1770 s->repeat_first_field, s->chroma_420_type ? "420" : "");
1776 FF_DISABLE_DEPRECATION_WARNINGS
1777 // If 1, we memcpy blocks in xvmcvideo.
1778 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1779 ff_xvmc_init_block(s); // set s->block
1780 FF_ENABLE_DEPRECATION_WARNINGS
1781 #endif /* FF_API_XVMC */
1783 if (mpeg_decode_mb(s, s->block) < 0)
1786 // Note motion_val is normally NULL unless we want to extract the MVs.
1787 if (s->current_picture.motion_val[0] && !s->encoding) {
1788 const int wrap = s->b8_stride;
1789 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1790 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1791 int motion_x, motion_y, dir, i;
1793 for (i = 0; i < 2; i++) {
1794 for (dir = 0; dir < 2; dir++) {
1796 (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1797 motion_x = motion_y = 0;
1798 } else if (s->mv_type == MV_TYPE_16X16 ||
1799 (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1800 motion_x = s->mv[dir][0][0];
1801 motion_y = s->mv[dir][0][1];
1802 } else { /* if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8)) */
1803 motion_x = s->mv[dir][i][0];
1804 motion_y = s->mv[dir][i][1];
1807 s->current_picture.motion_val[dir][xy][0] = motion_x;
1808 s->current_picture.motion_val[dir][xy][1] = motion_y;
1809 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1810 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1811 s->current_picture.ref_index [dir][b8_xy] =
1812 s->current_picture.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1813 assert(s->field_select[dir][i] == 0 ||
1814 s->field_select[dir][i] == 1);
1822 s->dest[1] += 16 >> s->chroma_x_shift;
1823 s->dest[2] += 16 >> s->chroma_x_shift;
1825 ff_MPV_decode_mb(s, s->block);
1827 if (++s->mb_x >= s->mb_width) {
1828 const int mb_size = 16;
1830 ff_mpeg_draw_horiz_band(s, mb_size * (s->mb_y >> field_pic), mb_size);
1831 ff_MPV_report_decode_progress(s);
1834 s->mb_y += 1 << field_pic;
1836 if (s->mb_y >= s->mb_height) {
1837 int left = get_bits_left(&s->gb);
1838 int is_d10 = s->chroma_format == 2 &&
1839 s->pict_type == AV_PICTURE_TYPE_I &&
1840 avctx->profile == 0 && avctx->level == 5 &&
1841 s->intra_dc_precision == 2 &&
1842 s->q_scale_type == 1 && s->alternate_scan == 0 &&
1843 s->progressive_frame == 0
1844 /* vbv_delay == 0xBBB || 0xE10 */;
1847 (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10) ||
1848 ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
1849 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n",
1850 left, show_bits(&s->gb, FFMIN(left, 23)));
1856 ff_init_block_index(s);
1859 /* skip mb handling */
1860 if (s->mb_skip_run == -1) {
1861 /* read increment again */
1864 int code = get_vlc2(&s->gb, ff_mbincr_vlc.table,
1865 MBINCR_VLC_BITS, 2);
1867 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1872 s->mb_skip_run += 33;
1873 } else if (code == 35) {
1874 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1875 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1878 goto eos; /* end of slice */
1880 /* otherwise, stuffing, nothing to do */
1882 s->mb_skip_run += code;
1886 if (s->mb_skip_run) {
1888 if (s->pict_type == AV_PICTURE_TYPE_I) {
1889 av_log(s->avctx, AV_LOG_ERROR,
1890 "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1896 for (i = 0; i < 12; i++)
1897 s->block_last_index[i] = -1;
1898 if (s->picture_structure == PICT_FRAME)
1899 s->mv_type = MV_TYPE_16X16;
1901 s->mv_type = MV_TYPE_FIELD;
1902 if (s->pict_type == AV_PICTURE_TYPE_P) {
1903 /* if P type, zero motion vector is implied */
1904 s->mv_dir = MV_DIR_FORWARD;
1905 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1906 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1907 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1908 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1910 /* if B type, reuse previous vectors and directions */
1911 s->mv[0][0][0] = s->last_mv[0][0][0];
1912 s->mv[0][0][1] = s->last_mv[0][0][1];
1913 s->mv[1][0][0] = s->last_mv[1][0][0];
1914 s->mv[1][0][1] = s->last_mv[1][0][1];
1919 eos: // end of slice
1920 *buf += (get_bits_count(&s->gb) - 1) / 8;
1921 av_dlog(s, "y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1925 static int slice_decode_thread(AVCodecContext *c, void *arg)
1927 MpegEncContext *s = *(void **) arg;
1928 const uint8_t *buf = s->gb.buffer;
1929 int mb_y = s->start_mb_y;
1930 const int field_pic = s->picture_structure != PICT_FRAME;
1932 s->er.error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
1935 uint32_t start_code;
1938 ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
1940 av_dlog(c, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1941 ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
1942 s->start_mb_y, s->end_mb_y, s->er.error_count);
1944 if (c->err_recognition & AV_EF_EXPLODE)
1946 if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
1947 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1949 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
1951 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
1952 s->mb_x - 1, s->mb_y,
1953 ER_AC_END | ER_DC_END | ER_MV_END);
1956 if (s->mb_y == s->end_mb_y)
1960 buf = avpriv_find_start_code(buf, s->gb.buffer_end, &start_code);
1961 mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
1962 if (s->picture_structure == PICT_BOTTOM_FIELD)
1964 if (mb_y < 0 || mb_y >= s->end_mb_y)
1970 * Handle slice ends.
1971 * @return 1 if it seems to be the last slice
1973 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
1975 Mpeg1Context *s1 = avctx->priv_data;
1976 MpegEncContext *s = &s1->mpeg_enc_ctx;
1978 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
1981 if (s->avctx->hwaccel) {
1982 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1983 av_log(avctx, AV_LOG_ERROR,
1984 "hardware accelerator failed to decode picture\n");
1988 FF_DISABLE_DEPRECATION_WARNINGS
1989 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1990 ff_xvmc_field_end(s);
1991 FF_ENABLE_DEPRECATION_WARNINGS
1992 #endif /* FF_API_XVMC */
1994 /* end of slice reached */
1995 if (/* s->mb_y << field_pic == s->mb_height && */ !s->first_field) {
1998 ff_er_frame_end(&s->er);
2000 ff_MPV_frame_end(s);
2002 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
2003 int ret = av_frame_ref(pict, &s->current_picture_ptr->f);
2006 ff_print_debug_info(s, s->current_picture_ptr);
2008 if (avctx->active_thread_type & FF_THREAD_FRAME)
2009 s->picture_number++;
2010 /* latency of 1 frame for I- and P-frames */
2011 /* XXX: use another variable than picture_number */
2012 if (s->last_picture_ptr != NULL) {
2013 int ret = av_frame_ref(pict, &s->last_picture_ptr->f);
2016 ff_print_debug_info(s, s->last_picture_ptr);
2026 static int mpeg1_decode_sequence(AVCodecContext *avctx,
2027 const uint8_t *buf, int buf_size)
2029 Mpeg1Context *s1 = avctx->priv_data;
2030 MpegEncContext *s = &s1->mpeg_enc_ctx;
2034 init_get_bits(&s->gb, buf, buf_size * 8);
2036 width = get_bits(&s->gb, 12);
2037 height = get_bits(&s->gb, 12);
2038 if (width == 0 || height == 0) {
2039 av_log(avctx, AV_LOG_WARNING,
2040 "Invalid horizontal or vertical size value.\n");
2041 if (avctx->err_recognition & AV_EF_BITSTREAM)
2042 return AVERROR_INVALIDDATA;
2044 s->aspect_ratio_info = get_bits(&s->gb, 4);
2045 if (s->aspect_ratio_info == 0) {
2046 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
2047 if (avctx->err_recognition & AV_EF_BITSTREAM)
2050 s->frame_rate_index = get_bits(&s->gb, 4);
2051 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
2053 s->bit_rate = get_bits(&s->gb, 18) * 400;
2054 if (get_bits1(&s->gb) == 0) /* marker */
2059 s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
2060 skip_bits(&s->gb, 1);
2063 if (get_bits1(&s->gb)) {
2064 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
2066 for (i = 0; i < 64; i++) {
2067 j = s->dsp.idct_permutation[i];
2068 v = ff_mpeg1_default_intra_matrix[i];
2069 s->intra_matrix[j] = v;
2070 s->chroma_intra_matrix[j] = v;
2073 if (get_bits1(&s->gb)) {
2074 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
2076 for (i = 0; i < 64; i++) {
2077 int j = s->dsp.idct_permutation[i];
2078 v = ff_mpeg1_default_non_intra_matrix[i];
2079 s->inter_matrix[j] = v;
2080 s->chroma_inter_matrix[j] = v;
2084 if (show_bits(&s->gb, 23) != 0) {
2085 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2089 /* We set MPEG-2 parameters so that it emulates MPEG-1. */
2090 s->progressive_sequence = 1;
2091 s->progressive_frame = 1;
2092 s->picture_structure = PICT_FRAME;
2093 s->frame_pred_frame_dct = 1;
2094 s->chroma_format = 1;
2096 s->avctx->codec_id = AV_CODEC_ID_MPEG1VIDEO;
2097 s->out_format = FMT_MPEG1;
2098 if (s->flags & CODEC_FLAG_LOW_DELAY)
2101 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2102 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2103 s->avctx->rc_buffer_size, s->bit_rate);
2108 static int vcr2_init_sequence(AVCodecContext *avctx)
2110 Mpeg1Context *s1 = avctx->priv_data;
2111 MpegEncContext *s = &s1->mpeg_enc_ctx;
2114 /* start new MPEG-1 context decoding */
2115 s->out_format = FMT_MPEG1;
2116 if (s1->mpeg_enc_ctx_allocated) {
2117 ff_MPV_common_end(s);
2119 s->width = avctx->coded_width;
2120 s->height = avctx->coded_height;
2121 avctx->has_b_frames = 0; // true?
2124 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2125 avctx->hwaccel = ff_find_hwaccel(avctx);
2128 if ((avctx->pix_fmt == AV_PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel) &&
2129 avctx->idct_algo == FF_IDCT_AUTO)
2131 if (avctx->hwaccel && avctx->idct_algo == FF_IDCT_AUTO)
2132 #endif /* FF_API_XVMC */
2133 avctx->idct_algo = FF_IDCT_SIMPLE;
2135 if (ff_MPV_common_init(s) < 0)
2137 s1->mpeg_enc_ctx_allocated = 1;
2139 for (i = 0; i < 64; i++) {
2140 int j = s->dsp.idct_permutation[i];
2141 v = ff_mpeg1_default_intra_matrix[i];
2142 s->intra_matrix[j] = v;
2143 s->chroma_intra_matrix[j] = v;
2145 v = ff_mpeg1_default_non_intra_matrix[i];
2146 s->inter_matrix[j] = v;
2147 s->chroma_inter_matrix[j] = v;
2150 s->progressive_sequence = 1;
2151 s->progressive_frame = 1;
2152 s->picture_structure = PICT_FRAME;
2153 s->frame_pred_frame_dct = 1;
2154 s->chroma_format = 1;
2155 s->codec_id = s->avctx->codec_id = AV_CODEC_ID_MPEG2VIDEO;
2156 s1->save_width = s->width;
2157 s1->save_height = s->height;
2158 s1->save_progressive_seq = s->progressive_sequence;
2162 static int mpeg_decode_a53_cc(AVCodecContext *avctx,
2163 const uint8_t *p, int buf_size)
2165 Mpeg1Context *s1 = avctx->priv_data;
2167 if (buf_size >= 6 &&
2168 p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
2169 p[4] == 3 && (p[5] & 0x40)) {
2170 /* extract A53 Part 4 CC data */
2171 int cc_count = p[5] & 0x1f;
2172 if (cc_count > 0 && buf_size >= 7 + cc_count * 3) {
2173 av_freep(&s1->a53_caption);
2174 s1->a53_caption_size = cc_count * 3;
2175 s1->a53_caption = av_malloc(s1->a53_caption_size);
2176 if (s1->a53_caption)
2177 memcpy(s1->a53_caption, p + 7, s1->a53_caption_size);
2180 } else if (buf_size >= 11 &&
2181 p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
2182 /* extract DVD CC data */
2185 // There is a caption count field in the data, but it is often
2186 // incorect. So count the number of captions present.
2187 for (i = 5; i + 6 <= buf_size && ((p[i] & 0xfe) == 0xfe); i += 6)
2189 // Transform the DVD format into A53 Part 4 format
2191 av_freep(&s1->a53_caption);
2192 s1->a53_caption_size = cc_count * 6;
2193 s1->a53_caption = av_malloc(s1->a53_caption_size);
2194 if (s1->a53_caption) {
2195 uint8_t field1 = !!(p[4] & 0x80);
2196 uint8_t *cap = s1->a53_caption;
2198 for (i = 0; i < cc_count; i++) {
2199 cap[0] = (p[0] == 0xff && field1) ? 0xfc : 0xfd;
2202 cap[3] = (p[3] == 0xff && !field1) ? 0xfc : 0xfd;
2215 static void mpeg_decode_user_data(AVCodecContext *avctx,
2216 const uint8_t *p, int buf_size)
2218 const uint8_t *buf_end = p + buf_size;
2220 /* we parse the DTG active format information */
2221 if (buf_end - p >= 5 &&
2222 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2230 if (buf_end - p < 1)
2232 avctx->dtg_active_format = p[0] & 0x0f;
2234 } else if (buf_end - p >= 6 &&
2235 p[0] == 'J' && p[1] == 'P' && p[2] == '3' && p[3] == 'D' &&
2236 p[4] == 0x03) { // S3D_video_format_length
2237 // the 0x7F mask ignores the reserved_bit value
2238 const uint8_t S3D_video_format_type = p[5] & 0x7F;
2240 if (S3D_video_format_type == 0x03 ||
2241 S3D_video_format_type == 0x04 ||
2242 S3D_video_format_type == 0x08 ||
2243 S3D_video_format_type == 0x23) {
2244 Mpeg1Context *s1 = avctx->priv_data;
2246 s1->has_stereo3d = 1;
2248 switch (S3D_video_format_type) {
2250 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE;
2253 s1->stereo3d.type = AV_STEREO3D_TOPBOTTOM;
2256 s1->stereo3d.type = AV_STEREO3D_2D;
2259 s1->stereo3d.type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
2263 } else if (mpeg_decode_a53_cc(avctx, p, buf_size)) {
2268 static void mpeg_decode_gop(AVCodecContext *avctx,
2269 const uint8_t *buf, int buf_size)
2271 Mpeg1Context *s1 = avctx->priv_data;
2272 MpegEncContext *s = &s1->mpeg_enc_ctx;
2274 int time_code_hours, time_code_minutes;
2275 int time_code_seconds, time_code_pictures;
2278 init_get_bits(&s->gb, buf, buf_size * 8);
2280 skip_bits1(&s->gb); /* drop_frame_flag */
2282 time_code_hours = get_bits(&s->gb, 5);
2283 time_code_minutes = get_bits(&s->gb, 6);
2284 skip_bits1(&s->gb); // marker bit
2285 time_code_seconds = get_bits(&s->gb, 6);
2286 time_code_pictures = get_bits(&s->gb, 6);
2288 s1->closed_gop = get_bits1(&s->gb);
2289 /* broken_link indicate that after editing the
2290 * reference frames of the first B-Frames after GOP I-Frame
2291 * are missing (open gop) */
2292 broken_link = get_bits1(&s->gb);
2294 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2295 av_log(s->avctx, AV_LOG_DEBUG,
2296 "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
2297 time_code_hours, time_code_minutes, time_code_seconds,
2298 time_code_pictures, s1->closed_gop, broken_link);
2301 static int decode_chunks(AVCodecContext *avctx, AVFrame *picture,
2302 int *got_output, const uint8_t *buf, int buf_size)
2304 Mpeg1Context *s = avctx->priv_data;
2305 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2306 const uint8_t *buf_ptr = buf;
2307 const uint8_t *buf_end = buf + buf_size;
2308 int ret, input_size;
2309 int last_code = 0, skip_frame = 0;
2312 /* find next start code */
2313 uint32_t start_code = -1;
2314 buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &start_code);
2315 if (start_code > 0x1ff) {
2318 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2322 avctx->execute(avctx, slice_decode_thread,
2323 &s2->thread_context[0], NULL,
2324 s->slice_count, sizeof(void *));
2325 for (i = 0; i < s->slice_count; i++)
2326 s2->er.error_count += s2->thread_context[i]->er.error_count;
2329 ret = slice_end(avctx, picture);
2333 // FIXME: merge with the stuff in mpeg_decode_slice
2334 if (s2->last_picture_ptr || s2->low_delay)
2339 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2342 input_size = buf_end - buf_ptr;
2344 if (avctx->debug & FF_DEBUG_STARTCODE)
2345 av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n",
2346 start_code, buf_ptr - buf, input_size);
2348 /* prepare data for next start code */
2349 switch (start_code) {
2350 case SEQ_START_CODE:
2351 if (last_code == 0) {
2352 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2355 av_log(avctx, AV_LOG_ERROR,
2356 "ignoring SEQ_START_CODE after %X\n", last_code);
2357 if (avctx->err_recognition & AV_EF_EXPLODE)
2358 return AVERROR_INVALIDDATA;
2362 case PICTURE_START_CODE:
2363 if (s2->width <= 0 || s2->height <= 0) {
2364 av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d.\n",
2365 s2->width, s2->height);
2366 return AVERROR_INVALIDDATA;
2369 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) &&
2370 !avctx->hwaccel && s->slice_count) {
2373 avctx->execute(avctx, slice_decode_thread,
2374 s2->thread_context, NULL,
2375 s->slice_count, sizeof(void *));
2376 for (i = 0; i < s->slice_count; i++)
2377 s2->er.error_count += s2->thread_context[i]->er.error_count;
2380 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2381 ret = mpeg_decode_postinit(avctx);
2383 av_log(avctx, AV_LOG_ERROR,
2384 "mpeg_decode_postinit() failure\n");
2388 /* We have a complete image: we try to decompress it. */
2389 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2392 last_code = PICTURE_START_CODE;
2394 av_log(avctx, AV_LOG_ERROR,
2395 "ignoring pic after %X\n", last_code);
2396 if (avctx->err_recognition & AV_EF_EXPLODE)
2397 return AVERROR_INVALIDDATA;
2400 case EXT_START_CODE:
2401 init_get_bits(&s2->gb, buf_ptr, input_size * 8);
2403 switch (get_bits(&s2->gb, 4)) {
2405 if (last_code == 0) {
2406 mpeg_decode_sequence_extension(s);
2408 av_log(avctx, AV_LOG_ERROR,
2409 "ignoring seq ext after %X\n", last_code);
2410 if (avctx->err_recognition & AV_EF_EXPLODE)
2411 return AVERROR_INVALIDDATA;
2415 mpeg_decode_sequence_display_extension(s);
2418 mpeg_decode_quant_matrix_extension(s2);
2421 mpeg_decode_picture_display_extension(s);
2424 if (last_code == PICTURE_START_CODE) {
2425 mpeg_decode_picture_coding_extension(s);
2427 av_log(avctx, AV_LOG_ERROR,
2428 "ignoring pic cod ext after %X\n", last_code);
2429 if (avctx->err_recognition & AV_EF_EXPLODE)
2430 return AVERROR_INVALIDDATA;
2435 case USER_START_CODE:
2436 mpeg_decode_user_data(avctx, buf_ptr, input_size);
2438 case GOP_START_CODE:
2439 if (last_code == 0) {
2440 s2->first_field = 0;
2441 mpeg_decode_gop(avctx, buf_ptr, input_size);
2444 av_log(avctx, AV_LOG_ERROR,
2445 "ignoring GOP_START_CODE after %X\n", last_code);
2446 if (avctx->err_recognition & AV_EF_EXPLODE)
2447 return AVERROR_INVALIDDATA;
2451 if (start_code >= SLICE_MIN_START_CODE &&
2452 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2453 const int field_pic = s2->picture_structure != PICT_FRAME;
2454 int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
2455 last_code = SLICE_MIN_START_CODE;
2457 if (s2->picture_structure == PICT_BOTTOM_FIELD)
2460 if (mb_y >= s2->mb_height) {
2461 av_log(s2->avctx, AV_LOG_ERROR,
2462 "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2466 if (s2->last_picture_ptr == NULL) {
2467 /* Skip B-frames if we do not have reference frames and
2468 * GOP is not closed. */
2469 if (s2->pict_type == AV_PICTURE_TYPE_B) {
2470 if (!s->closed_gop) {
2476 if (s2->pict_type == AV_PICTURE_TYPE_I)
2478 if (s2->next_picture_ptr == NULL) {
2479 /* Skip P-frames if we do not have a reference frame or
2480 * we have an invalid header. */
2481 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) {
2486 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
2487 s2->pict_type == AV_PICTURE_TYPE_B) ||
2488 (avctx->skip_frame >= AVDISCARD_NONKEY &&
2489 s2->pict_type != AV_PICTURE_TYPE_I) ||
2490 avctx->skip_frame >= AVDISCARD_ALL) {
2495 if (!s->mpeg_enc_ctx_allocated)
2498 if (s2->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
2499 if (mb_y < avctx->skip_top ||
2500 mb_y >= s2->mb_height - avctx->skip_bottom)
2504 if (!s2->pict_type) {
2505 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2506 if (avctx->err_recognition & AV_EF_EXPLODE)
2507 return AVERROR_INVALIDDATA;
2511 if (s->first_slice) {
2514 if (mpeg_field_start(s2, buf, buf_size) < 0)
2517 if (!s2->current_picture_ptr) {
2518 av_log(avctx, AV_LOG_ERROR,
2519 "current_picture not initialized\n");
2520 return AVERROR_INVALIDDATA;
2524 (avctx->active_thread_type & FF_THREAD_SLICE) &&
2526 int threshold = (s2->mb_height * s->slice_count +
2527 s2->slice_context_count / 2) /
2528 s2->slice_context_count;
2529 if (threshold <= mb_y) {
2530 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2532 thread_context->start_mb_y = mb_y;
2533 thread_context->end_mb_y = s2->mb_height;
2534 if (s->slice_count) {
2535 s2->thread_context[s->slice_count - 1]->end_mb_y = mb_y;
2536 ret = ff_update_duplicate_context(thread_context, s2);
2540 init_get_bits(&thread_context->gb, buf_ptr, input_size * 8);
2543 buf_ptr += 2; // FIXME add minimum number of bytes per slice
2545 ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
2549 if (avctx->err_recognition & AV_EF_EXPLODE)
2551 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2552 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2553 s2->resync_mb_y, s2->mb_x, s2->mb_y,
2554 ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
2556 ff_er_add_slice(&s2->er, s2->resync_mb_x,
2557 s2->resync_mb_y, s2->mb_x - 1, s2->mb_y,
2558 ER_AC_END | ER_DC_END | ER_MV_END);
2567 static int mpeg_decode_frame(AVCodecContext *avctx, void *data,
2568 int *got_output, AVPacket *avpkt)
2570 const uint8_t *buf = avpkt->data;
2571 int buf_size = avpkt->size;
2572 Mpeg1Context *s = avctx->priv_data;
2573 AVFrame *picture = data;
2574 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2575 av_dlog(avctx, "fill_buffer\n");
2577 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2578 /* special case for last picture */
2579 if (s2->low_delay == 0 && s2->next_picture_ptr) {
2580 int ret = av_frame_ref(picture, &s2->next_picture_ptr->f);
2584 s2->next_picture_ptr = NULL;
2591 if (s2->flags & CODEC_FLAG_TRUNCATED) {
2592 int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf,
2595 if (ff_combine_frame(&s2->parse_context, next,
2596 (const uint8_t **) &buf, &buf_size) < 0)
2600 if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
2601 vcr2_init_sequence(avctx);
2605 if (avctx->extradata && !s->extradata_decoded) {
2606 int ret = decode_chunks(avctx, picture, got_output,
2607 avctx->extradata, avctx->extradata_size);
2608 s->extradata_decoded = 1;
2609 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
2613 return decode_chunks(avctx, picture, got_output, buf, buf_size);
2616 static void flush(AVCodecContext *avctx)
2618 Mpeg1Context *s = avctx->priv_data;
2623 ff_mpeg_flush(avctx);
2626 static av_cold int mpeg_decode_end(AVCodecContext *avctx)
2628 Mpeg1Context *s = avctx->priv_data;
2630 if (s->mpeg_enc_ctx_allocated)
2631 ff_MPV_common_end(&s->mpeg_enc_ctx);
2632 av_freep(&s->a53_caption);
2636 static const AVProfile mpeg2_video_profiles[] = {
2637 { FF_PROFILE_MPEG2_422, "4:2:2" },
2638 { FF_PROFILE_MPEG2_HIGH, "High" },
2639 { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2640 { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2641 { FF_PROFILE_MPEG2_MAIN, "Main" },
2642 { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2643 { FF_PROFILE_RESERVED, "Reserved" },
2644 { FF_PROFILE_RESERVED, "Reserved" },
2645 { FF_PROFILE_UNKNOWN },
2648 AVCodec ff_mpeg1video_decoder = {
2649 .name = "mpeg1video",
2650 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2651 .type = AVMEDIA_TYPE_VIDEO,
2652 .id = AV_CODEC_ID_MPEG1VIDEO,
2653 .priv_data_size = sizeof(Mpeg1Context),
2654 .init = mpeg_decode_init,
2655 .close = mpeg_decode_end,
2656 .decode = mpeg_decode_frame,
2657 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2658 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2659 CODEC_CAP_SLICE_THREADS,
2661 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2664 AVCodec ff_mpeg2video_decoder = {
2665 .name = "mpeg2video",
2666 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2667 .type = AVMEDIA_TYPE_VIDEO,
2668 .id = AV_CODEC_ID_MPEG2VIDEO,
2669 .priv_data_size = sizeof(Mpeg1Context),
2670 .init = mpeg_decode_init,
2671 .close = mpeg_decode_end,
2672 .decode = mpeg_decode_frame,
2673 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2674 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
2675 CODEC_CAP_SLICE_THREADS,
2677 .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2681 #if CONFIG_MPEG_XVMC_DECODER
2682 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2684 if (avctx->active_thread_type & FF_THREAD_SLICE)
2686 if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2688 if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
2689 av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2691 mpeg_decode_init(avctx);
2693 avctx->pix_fmt = AV_PIX_FMT_XVMC_MPEG2_IDCT;
2694 avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2699 AVCodec ff_mpeg_xvmc_decoder = {
2700 .name = "mpegvideo_xvmc",
2701 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2702 .type = AVMEDIA_TYPE_VIDEO,
2703 .id = AV_CODEC_ID_MPEG2VIDEO_XVMC,
2704 .priv_data_size = sizeof(Mpeg1Context),
2705 .init = mpeg_mc_decode_init,
2706 .close = mpeg_decode_end,
2707 .decode = mpeg_decode_frame,
2708 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
2709 CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2714 #endif /* FF_API_XVMC */