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
32 #include "mpegvideo.h"
35 #include "mpeg12data.h"
36 #include "mpeg12decdata.h"
37 #include "bytestream.h"
38 #include "vdpau_internal.h"
39 #include "xvmc_internal.h"
47 #define MBINCR_VLC_BITS 9
48 #define MB_PAT_VLC_BITS 9
49 #define MB_PTYPE_VLC_BITS 6
50 #define MB_BTYPE_VLC_BITS 6
54 /* as H.263, but only 17 codes */
55 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
57 int code, sign, val, l, shift;
59 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
67 sign = get_bits1(&s->gb);
71 val = (val - 1) << shift;
72 val |= get_bits(&s->gb, shift);
80 l = INT_BIT - 5 - shift;
81 val = (val << l) >> l;
85 static inline int mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
87 int level, dc, diff, i, j, run;
89 RLTable *rl = &ff_rl_mpeg1;
90 uint8_t * const scantable = s->intra_scantable.permutated;
91 const uint16_t *quant_matrix = s->intra_matrix;
92 const int qscale = s->qscale;
95 component = (n <= 3 ? 0 : n - 4 + 1);
96 diff = decode_dc(&s->gb, component);
99 dc = s->last_dc[component];
101 s->last_dc[component] = dc;
102 block[0] = dc * quant_matrix[0];
103 av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
106 OPEN_READER(re, &s->gb);
107 /* now quantify & encode AC coefficients */
109 UPDATE_CACHE(re, &s->gb);
110 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
114 } else if (level != 0) {
117 level = (level * qscale * quant_matrix[j]) >> 4;
118 level = (level - 1) | 1;
119 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
120 LAST_SKIP_BITS(re, &s->gb, 1);
123 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
124 UPDATE_CACHE(re, &s->gb);
125 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
127 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
128 } else if (level == 0) {
129 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
135 level = (level * qscale * quant_matrix[j]) >> 4;
136 level = (level - 1) | 1;
139 level = (level * qscale * quant_matrix[j]) >> 4;
140 level = (level - 1) | 1;
144 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
150 CLOSE_READER(re, &s->gb);
152 s->block_last_index[n] = i;
156 int ff_mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
158 return mpeg1_decode_block_intra(s, block, n);
161 static inline int mpeg1_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
163 int level, i, j, run;
164 RLTable *rl = &ff_rl_mpeg1;
165 uint8_t * const scantable = s->intra_scantable.permutated;
166 const uint16_t *quant_matrix = s->inter_matrix;
167 const int qscale = s->qscale;
170 OPEN_READER(re, &s->gb);
172 // special case for first coefficient, no need to add second VLC table
173 UPDATE_CACHE(re, &s->gb);
174 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
175 level = (3 * qscale * quant_matrix[0]) >> 5;
176 level = (level - 1) | 1;
177 if (GET_CACHE(re, &s->gb) & 0x40000000)
181 SKIP_BITS(re, &s->gb, 2);
182 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
185 /* now quantify & encode AC coefficients */
187 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
192 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
193 level = (level - 1) | 1;
194 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
195 SKIP_BITS(re, &s->gb, 1);
198 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
199 UPDATE_CACHE(re, &s->gb);
200 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
202 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
203 } else if (level == 0) {
204 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
210 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
211 level = (level - 1) | 1;
214 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
215 level = (level - 1) | 1;
219 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
224 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
226 UPDATE_CACHE(re, &s->gb);
229 LAST_SKIP_BITS(re, &s->gb, 2);
230 CLOSE_READER(re, &s->gb);
232 s->block_last_index[n] = i;
236 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
238 int level, i, j, run;
239 RLTable *rl = &ff_rl_mpeg1;
240 uint8_t * const scantable = s->intra_scantable.permutated;
241 const int qscale = s->qscale;
244 OPEN_READER(re, &s->gb);
246 // special case for first coefficient, no need to add second VLC table
247 UPDATE_CACHE(re, &s->gb);
248 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
249 level = (3 * qscale) >> 1;
250 level = (level - 1) | 1;
251 if (GET_CACHE(re, &s->gb) & 0x40000000)
255 SKIP_BITS(re, &s->gb, 2);
256 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
260 /* now quantify & encode AC coefficients */
262 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
267 level = ((level * 2 + 1) * qscale) >> 1;
268 level = (level - 1) | 1;
269 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
270 SKIP_BITS(re, &s->gb, 1);
273 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
274 UPDATE_CACHE(re, &s->gb);
275 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
277 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
278 } else if (level == 0) {
279 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
285 level = ((level * 2 + 1) * qscale) >> 1;
286 level = (level - 1) | 1;
289 level = ((level * 2 + 1) * qscale) >> 1;
290 level = (level - 1) | 1;
295 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
297 UPDATE_CACHE(re, &s->gb);
300 LAST_SKIP_BITS(re, &s->gb, 2);
301 CLOSE_READER(re, &s->gb);
303 s->block_last_index[n] = i;
308 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n)
310 int level, i, j, run;
311 RLTable *rl = &ff_rl_mpeg1;
312 uint8_t * const scantable = s->intra_scantable.permutated;
313 const uint16_t *quant_matrix;
314 const int qscale = s->qscale;
320 OPEN_READER(re, &s->gb);
323 quant_matrix = s->inter_matrix;
325 quant_matrix = s->chroma_inter_matrix;
327 // special case for first coefficient, no need to add second VLC table
328 UPDATE_CACHE(re, &s->gb);
329 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
330 level= (3 * qscale * quant_matrix[0]) >> 5;
331 if (GET_CACHE(re, &s->gb) & 0x40000000)
336 SKIP_BITS(re, &s->gb, 2);
337 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
341 /* now quantify & encode AC coefficients */
343 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
348 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
349 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
350 SKIP_BITS(re, &s->gb, 1);
353 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
354 UPDATE_CACHE(re, &s->gb);
355 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
360 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
363 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
367 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
373 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
375 UPDATE_CACHE(re, &s->gb);
378 LAST_SKIP_BITS(re, &s->gb, 2);
379 CLOSE_READER(re, &s->gb);
381 block[63] ^= (mismatch & 1);
383 s->block_last_index[n] = i;
387 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
388 DCTELEM *block, int n)
390 int level, i, j, run;
391 RLTable *rl = &ff_rl_mpeg1;
392 uint8_t * const scantable = s->intra_scantable.permutated;
393 const int qscale = s->qscale;
394 OPEN_READER(re, &s->gb);
397 // special case for first coefficient, no need to add second VLC table
398 UPDATE_CACHE(re, &s->gb);
399 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
400 level = (3 * qscale) >> 1;
401 if (GET_CACHE(re, &s->gb) & 0x40000000)
405 SKIP_BITS(re, &s->gb, 2);
406 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
410 /* now quantify & encode AC coefficients */
412 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
417 level = ((level * 2 + 1) * qscale) >> 1;
418 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
419 SKIP_BITS(re, &s->gb, 1);
422 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
423 UPDATE_CACHE(re, &s->gb);
424 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
429 level = ((-level * 2 + 1) * qscale) >> 1;
432 level = ((level * 2 + 1) * qscale) >> 1;
437 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
439 UPDATE_CACHE(re, &s->gb);
442 LAST_SKIP_BITS(re, &s->gb, 2);
443 CLOSE_READER(re, &s->gb);
444 s->block_last_index[n] = i;
449 static inline int mpeg2_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
451 int level, dc, diff, i, j, run;
454 uint8_t * const scantable = s->intra_scantable.permutated;
455 const uint16_t *quant_matrix;
456 const int qscale = s->qscale;
461 quant_matrix = s->intra_matrix;
464 quant_matrix = s->chroma_intra_matrix;
465 component = (n & 1) + 1;
467 diff = decode_dc(&s->gb, component);
470 dc = s->last_dc[component];
472 s->last_dc[component] = dc;
473 block[0] = dc << (3 - s->intra_dc_precision);
474 av_dlog(s->avctx, "dc=%d\n", block[0]);
475 mismatch = block[0] ^ 1;
477 if (s->intra_vlc_format)
483 OPEN_READER(re, &s->gb);
484 /* now quantify & encode AC coefficients */
486 UPDATE_CACHE(re, &s->gb);
487 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
491 } else if (level != 0) {
494 level = (level * qscale * quant_matrix[j]) >> 4;
495 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
496 LAST_SKIP_BITS(re, &s->gb, 1);
499 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
500 UPDATE_CACHE(re, &s->gb);
501 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
505 level = (-level * qscale * quant_matrix[j]) >> 4;
508 level = (level * qscale * quant_matrix[j]) >> 4;
512 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
519 CLOSE_READER(re, &s->gb);
521 block[63] ^= mismatch & 1;
523 s->block_last_index[n] = i;
527 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
529 int level, dc, diff, j, run;
532 uint8_t * scantable = s->intra_scantable.permutated;
533 const uint16_t *quant_matrix;
534 const int qscale = s->qscale;
538 quant_matrix = s->intra_matrix;
541 quant_matrix = s->chroma_intra_matrix;
542 component = (n & 1) + 1;
544 diff = decode_dc(&s->gb, component);
547 dc = s->last_dc[component];
549 s->last_dc[component] = dc;
550 block[0] = dc << (3 - s->intra_dc_precision);
551 if (s->intra_vlc_format)
557 OPEN_READER(re, &s->gb);
558 /* now quantify & encode AC coefficients */
560 UPDATE_CACHE(re, &s->gb);
561 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
565 } else if (level != 0) {
568 level = (level * qscale * quant_matrix[j]) >> 4;
569 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
570 LAST_SKIP_BITS(re, &s->gb, 1);
573 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
574 UPDATE_CACHE(re, &s->gb);
575 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
579 level = (-level * qscale * quant_matrix[j]) >> 4;
582 level = (level * qscale * quant_matrix[j]) >> 4;
588 CLOSE_READER(re, &s->gb);
591 s->block_last_index[n] = scantable - s->intra_scantable.permutated;
595 uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
597 #define INIT_2D_VLC_RL(rl, static_size)\
599 static RL_VLC_ELEM rl_vlc_table[static_size];\
600 INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
601 &rl.table_vlc[0][1], 4, 2,\
602 &rl.table_vlc[0][0], 4, 2, static_size);\
604 rl.rl_vlc[0] = rl_vlc_table;\
605 init_2d_vlc_rl(&rl);\
608 static void init_2d_vlc_rl(RLTable *rl)
612 for (i = 0; i < rl->vlc.table_size; i++) {
613 int code = rl->vlc.table[i][0];
614 int len = rl->vlc.table[i][1];
617 if (len == 0) { // illegal code
620 } else if (len<0) { //more bits needed
624 if (code == rl->n) { //esc
627 } else if (code == rl->n+1) { //eob
631 run = rl->table_run [code] + 1;
632 level = rl->table_level[code];
635 rl->rl_vlc[0][i].len = len;
636 rl->rl_vlc[0][i].level = level;
637 rl->rl_vlc[0][i].run = run;
641 void ff_mpeg12_common_init(MpegEncContext *s)
644 s->y_dc_scale_table =
645 s->c_dc_scale_table = ff_mpeg2_dc_scale_table[s->intra_dc_precision];
649 void ff_mpeg1_clean_buffers(MpegEncContext *s)
651 s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
652 s->last_dc[1] = s->last_dc[0];
653 s->last_dc[2] = s->last_dc[0];
654 memset(s->last_mv, 0, sizeof(s->last_mv));
658 /******************************************/
662 VLC ff_dc_chroma_vlc;
664 static VLC mbincr_vlc;
665 static VLC mb_ptype_vlc;
666 static VLC mb_btype_vlc;
667 static VLC mb_pat_vlc;
669 av_cold void ff_mpeg12_init_vlcs(void)
676 INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
677 ff_mpeg12_vlc_dc_lum_bits, 1, 1,
678 ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
679 INIT_VLC_STATIC(&ff_dc_chroma_vlc, DC_VLC_BITS, 12,
680 ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
681 ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
682 INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
683 &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
684 &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
685 INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
686 &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
687 &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
688 INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
689 &ff_mpeg12_mbPatTable[0][1], 2, 1,
690 &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
692 INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
693 &table_mb_ptype[0][1], 2, 1,
694 &table_mb_ptype[0][0], 2, 1, 64);
695 INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
696 &table_mb_btype[0][1], 2, 1,
697 &table_mb_btype[0][0], 2, 1, 64);
698 init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
699 init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
701 INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
702 INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
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);
717 if (s->q_scale_type) {
718 return non_linear_qscale[qscale];
724 static void exchange_uv(MpegEncContext *s)
729 s->pblocks[4] = s->pblocks[5];
733 /* motion type (for MPEG-2) */
739 static int mpeg_decode_mb(MpegEncContext *s, DCTELEM block[12][64])
741 int i, j, k, cbp, val, mb_type, motion_type;
742 const int mb_block_count = 4 + (1 << s->chroma_format);
744 av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
746 assert(s->mb_skipped == 0);
748 if (s->mb_skip_run-- != 0) {
749 if (s->pict_type == AV_PICTURE_TYPE_P) {
751 s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
756 mb_type = s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
758 mb_type = s->current_picture.f.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1]; // FIXME not sure if this is allowed in MPEG at all
759 if (IS_INTRA(mb_type))
761 s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride] =
762 mb_type | MB_TYPE_SKIP;
763 // assert(s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1] & (MB_TYPE_16x16 | MB_TYPE_16x8));
765 if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
772 switch (s->pict_type) {
774 case AV_PICTURE_TYPE_I:
775 if (get_bits1(&s->gb) == 0) {
776 if (get_bits1(&s->gb) == 0) {
777 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
780 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
782 mb_type = MB_TYPE_INTRA;
785 case AV_PICTURE_TYPE_P:
786 mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
788 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
791 mb_type = ptype2mb_type[mb_type];
793 case AV_PICTURE_TYPE_B:
794 mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
796 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
799 mb_type = btype2mb_type[mb_type];
802 av_dlog(s->avctx, "mb_type=%x\n", mb_type);
803 // motion_type = 0; /* avoid warning */
804 if (IS_INTRA(mb_type)) {
805 s->dsp.clear_blocks(s->block[0]);
807 if (!s->chroma_y_shift) {
808 s->dsp.clear_blocks(s->block[6]);
811 /* compute DCT type */
812 if (s->picture_structure == PICT_FRAME && // FIXME add an interlaced_dct coded var?
813 !s->frame_pred_frame_dct) {
814 s->interlaced_dct = get_bits1(&s->gb);
817 if (IS_QUANT(mb_type))
818 s->qscale = get_qscale(s);
820 if (s->concealment_motion_vectors) {
821 /* just parse them */
822 if (s->picture_structure != PICT_FRAME)
823 skip_bits1(&s->gb); /* field select */
825 s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
826 mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
827 s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
828 mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
830 skip_bits1(&s->gb); /* marker */
832 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
834 // if 1, we memcpy blocks in xvmcvideo
835 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
836 ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
842 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
843 if (s->flags2 & CODEC_FLAG2_FAST) {
844 for (i = 0; i < 6; i++) {
845 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
848 for (i = 0; i < mb_block_count; i++) {
849 if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
854 for (i = 0; i < 6; i++) {
855 if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
860 if (mb_type & MB_TYPE_ZERO_MV) {
861 assert(mb_type & MB_TYPE_CBP);
863 s->mv_dir = MV_DIR_FORWARD;
864 if (s->picture_structure == PICT_FRAME) {
865 if (!s->frame_pred_frame_dct)
866 s->interlaced_dct = get_bits1(&s->gb);
867 s->mv_type = MV_TYPE_16X16;
869 s->mv_type = MV_TYPE_FIELD;
870 mb_type |= MB_TYPE_INTERLACED;
871 s->field_select[0][0] = s->picture_structure - 1;
874 if (IS_QUANT(mb_type))
875 s->qscale = get_qscale(s);
877 s->last_mv[0][0][0] = 0;
878 s->last_mv[0][0][1] = 0;
879 s->last_mv[0][1][0] = 0;
880 s->last_mv[0][1][1] = 0;
884 assert(mb_type & MB_TYPE_L0L1);
885 // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
886 /* get additional motion vector type */
887 if (s->frame_pred_frame_dct)
888 motion_type = MT_FRAME;
890 motion_type = get_bits(&s->gb, 2);
891 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
892 s->interlaced_dct = get_bits1(&s->gb);
895 if (IS_QUANT(mb_type))
896 s->qscale = get_qscale(s);
899 s->mv_dir = (mb_type >> 13) & 3;
900 av_dlog(s->avctx, "motion_type=%d\n", motion_type);
901 switch (motion_type) {
902 case MT_FRAME: /* or MT_16X8 */
903 if (s->picture_structure == PICT_FRAME) {
904 mb_type |= MB_TYPE_16x16;
905 s->mv_type = MV_TYPE_16X16;
906 for (i = 0; i < 2; i++) {
907 if (USES_LIST(mb_type, i)) {
909 s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
910 mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
911 s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
912 mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
913 /* full_pel: only for MPEG-1 */
914 if (s->full_pel[i]) {
915 s->mv[i][0][0] <<= 1;
916 s->mv[i][0][1] <<= 1;
921 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
922 s->mv_type = MV_TYPE_16X8;
923 for (i = 0; i < 2; i++) {
924 if (USES_LIST(mb_type, i)) {
926 for (j = 0; j < 2; j++) {
927 s->field_select[i][j] = get_bits1(&s->gb);
928 for (k = 0; k < 2; k++) {
929 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
930 s->last_mv[i][j][k]);
931 s->last_mv[i][j][k] = val;
932 s->mv[i][j][k] = val;
940 s->mv_type = MV_TYPE_FIELD;
941 if (s->picture_structure == PICT_FRAME) {
942 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
943 for (i = 0; i < 2; i++) {
944 if (USES_LIST(mb_type, i)) {
945 for (j = 0; j < 2; j++) {
946 s->field_select[i][j] = get_bits1(&s->gb);
947 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
948 s->last_mv[i][j][0]);
949 s->last_mv[i][j][0] = val;
950 s->mv[i][j][0] = val;
951 av_dlog(s->avctx, "fmx=%d\n", val);
952 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
953 s->last_mv[i][j][1] >> 1);
954 s->last_mv[i][j][1] = val << 1;
955 s->mv[i][j][1] = val;
956 av_dlog(s->avctx, "fmy=%d\n", val);
961 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
962 for (i = 0; i < 2; i++) {
963 if (USES_LIST(mb_type, i)) {
964 s->field_select[i][0] = get_bits1(&s->gb);
965 for (k = 0; k < 2; k++) {
966 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
967 s->last_mv[i][0][k]);
968 s->last_mv[i][0][k] = val;
969 s->last_mv[i][1][k] = val;
970 s->mv[i][0][k] = val;
977 s->mv_type = MV_TYPE_DMV;
978 for (i = 0; i < 2; i++) {
979 if (USES_LIST(mb_type, i)) {
980 int dmx, dmy, mx, my, m;
981 const int my_shift = s->picture_structure == PICT_FRAME;
983 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
984 s->last_mv[i][0][0]);
985 s->last_mv[i][0][0] = mx;
986 s->last_mv[i][1][0] = mx;
988 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
989 s->last_mv[i][0][1] >> my_shift);
993 s->last_mv[i][0][1] = my << my_shift;
994 s->last_mv[i][1][1] = my << my_shift;
998 s->mv[i][1][0] = mx; // not used
999 s->mv[i][1][1] = my; // not used
1001 if (s->picture_structure == PICT_FRAME) {
1002 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
1004 // m = 1 + 2 * s->top_field_first;
1005 m = s->top_field_first ? 1 : 3;
1007 /* top -> top pred */
1008 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1009 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1011 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1012 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1014 mb_type |= MB_TYPE_16x16;
1016 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1017 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1018 if (s->picture_structure == PICT_TOP_FIELD)
1027 av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1033 if (HAS_CBP(mb_type)) {
1034 s->dsp.clear_blocks(s->block[0]);
1036 cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1037 if (mb_block_count > 6) {
1038 cbp <<= mb_block_count - 6;
1039 cbp |= get_bits(&s->gb, mb_block_count - 6);
1040 s->dsp.clear_blocks(s->block[6]);
1043 av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
1047 //if 1, we memcpy blocks in xvmcvideo
1048 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
1049 ff_xvmc_pack_pblocks(s, cbp);
1055 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1056 if (s->flags2 & CODEC_FLAG2_FAST) {
1057 for (i = 0; i < 6; i++) {
1059 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
1061 s->block_last_index[i] = -1;
1066 cbp <<= 12-mb_block_count;
1068 for (i = 0; i < mb_block_count; i++) {
1069 if (cbp & (1 << 11)) {
1070 if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
1073 s->block_last_index[i] = -1;
1079 if (s->flags2 & CODEC_FLAG2_FAST) {
1080 for (i = 0; i < 6; i++) {
1082 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
1084 s->block_last_index[i] = -1;
1089 for (i = 0; i < 6; i++) {
1091 if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
1094 s->block_last_index[i] = -1;
1101 for (i = 0; i < 12; i++)
1102 s->block_last_index[i] = -1;
1106 s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
1111 typedef struct Mpeg1Context {
1112 MpegEncContext mpeg_enc_ctx;
1113 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1114 int repeat_field; /* true if we must repeat the field */
1115 AVPanScan pan_scan; /**< some temporary storage for the panscan */
1117 int swap_uv;//indicate VCR2
1118 int save_aspect_info;
1119 int save_width, save_height, save_progressive_seq;
1120 AVRational frame_rate_ext; ///< MPEG-2 specific framerate modificator
1121 int sync; ///< Did we reach a sync point like a GOP/SEQ/KEYFrame?
1124 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1126 Mpeg1Context *s = avctx->priv_data;
1127 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1130 /* we need some permutation to store matrices,
1131 * until MPV_common_init() sets the real permutation. */
1132 for (i = 0; i < 64; i++)
1133 s2->dsp.idct_permutation[i]=i;
1135 MPV_decode_defaults(s2);
1137 s->mpeg_enc_ctx.avctx = avctx;
1138 s->mpeg_enc_ctx.flags = avctx->flags;
1139 s->mpeg_enc_ctx.flags2 = avctx->flags2;
1140 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1141 ff_mpeg12_init_vlcs();
1143 s->mpeg_enc_ctx_allocated = 0;
1144 s->mpeg_enc_ctx.picture_number = 0;
1145 s->repeat_field = 0;
1146 s->mpeg_enc_ctx.codec_id = avctx->codec->id;
1147 avctx->color_range = AVCOL_RANGE_MPEG;
1148 if (avctx->codec->id == CODEC_ID_MPEG1VIDEO)
1149 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1151 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1155 static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
1157 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1158 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1161 if (avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized)
1164 err = ff_mpeg_update_thread_context(avctx, avctx_from);
1165 if (err) return err;
1167 if (!ctx->mpeg_enc_ctx_allocated)
1168 memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
1170 if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
1171 s->picture_number++;
1176 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1177 const uint8_t *new_perm)
1179 uint16_t temp_matrix[64];
1182 memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
1184 for (i = 0; i < 64; i++) {
1185 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1189 static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
1190 PIX_FMT_XVMC_MPEG2_IDCT,
1191 PIX_FMT_XVMC_MPEG2_MC,
1194 static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
1196 Mpeg1Context *s1 = avctx->priv_data;
1197 MpegEncContext *s = &s1->mpeg_enc_ctx;
1199 if (avctx->xvmc_acceleration)
1200 return avctx->get_format(avctx, pixfmt_xvmc_mpg2_420);
1201 else if (avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
1202 if (avctx->codec_id == CODEC_ID_MPEG1VIDEO)
1203 return PIX_FMT_VDPAU_MPEG1;
1205 return PIX_FMT_VDPAU_MPEG2;
1207 if (s->chroma_format < 2)
1208 return avctx->get_format(avctx, ff_hwaccel_pixfmt_list_420);
1209 else if (s->chroma_format == 2)
1210 return PIX_FMT_YUV422P;
1212 return PIX_FMT_YUV444P;
1216 /* Call this function when we know all parameters.
1217 * It may be called in different places for MPEG-1 and MPEG-2. */
1218 static int mpeg_decode_postinit(AVCodecContext *avctx)
1220 Mpeg1Context *s1 = avctx->priv_data;
1221 MpegEncContext *s = &s1->mpeg_enc_ctx;
1222 uint8_t old_permutation[64];
1224 if ((s1->mpeg_enc_ctx_allocated == 0) ||
1225 avctx->coded_width != s->width ||
1226 avctx->coded_height != s->height ||
1227 s1->save_width != s->width ||
1228 s1->save_height != s->height ||
1229 s1->save_aspect_info != s->aspect_ratio_info ||
1230 s1->save_progressive_seq != s->progressive_sequence ||
1234 if (s1->mpeg_enc_ctx_allocated) {
1235 ParseContext pc = s->parse_context;
1236 s->parse_context.buffer = 0;
1238 s->parse_context = pc;
1241 if ((s->width == 0) || (s->height == 0))
1244 avcodec_set_dimensions(avctx, s->width, s->height);
1245 avctx->bit_rate = s->bit_rate;
1246 s1->save_aspect_info = s->aspect_ratio_info;
1247 s1->save_width = s->width;
1248 s1->save_height = s->height;
1249 s1->save_progressive_seq = s->progressive_sequence;
1251 /* low_delay may be forced, in this case we will have B-frames
1252 * that behave like P-frames. */
1253 avctx->has_b_frames = !(s->low_delay);
1255 assert((avctx->sub_id == 1) == (avctx->codec_id == CODEC_ID_MPEG1VIDEO));
1256 if (avctx->codec_id == CODEC_ID_MPEG1VIDEO) {
1258 avctx->time_base.den = ff_frame_rate_tab[s->frame_rate_index].num;
1259 avctx->time_base.num = ff_frame_rate_tab[s->frame_rate_index].den;
1261 avctx->sample_aspect_ratio = av_d2q(1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1262 avctx->ticks_per_frame=1;
1265 av_reduce(&s->avctx->time_base.den,
1266 &s->avctx->time_base.num,
1267 ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
1268 ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1270 avctx->ticks_per_frame = 2;
1272 if (s->aspect_ratio_info > 1) {
1274 av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1275 (AVRational) {s1->pan_scan.width, s1->pan_scan.height}),
1276 (AVRational) {s->width, s->height});
1278 // we ignore the spec here and guess a bit as reality does not match the spec, see for example
1279 // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
1280 // issue1613, 621, 562
1281 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1282 (av_cmp_q(dar, (AVRational) {4, 3}) && av_cmp_q(dar, (AVRational) {16, 9}))) {
1283 s->avctx->sample_aspect_ratio =
1284 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1285 (AVRational) {s->width, s->height});
1287 s->avctx->sample_aspect_ratio =
1288 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1289 (AVRational) {s1->pan_scan.width, s1->pan_scan.height});
1290 //issue1613 4/3 16/9 -> 16/9
1291 //res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1292 //widescreen-issue562.mpg 4/3 16/9 -> 16/9
1293 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1294 //av_log(NULL, AV_LOG_ERROR, "A %d/%d\n", ff_mpeg2_aspect[s->aspect_ratio_info].num, ff_mpeg2_aspect[s->aspect_ratio_info].den);
1295 //av_log(NULL, AV_LOG_ERROR, "B %d/%d\n", s->avctx->sample_aspect_ratio.num, s->avctx->sample_aspect_ratio.den);
1298 s->avctx->sample_aspect_ratio =
1299 ff_mpeg2_aspect[s->aspect_ratio_info];
1303 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1304 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
1305 // until then pix_fmt may be changed right after codec init
1306 if (avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
1308 s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
1309 if (avctx->idct_algo == FF_IDCT_AUTO)
1310 avctx->idct_algo = FF_IDCT_SIMPLE;
1312 /* Quantization matrices may need reordering
1313 * if DCT permutation is changed. */
1314 memcpy(old_permutation, s->dsp.idct_permutation, 64 * sizeof(uint8_t));
1316 if (MPV_common_init(s) < 0)
1319 quant_matrix_rebuild(s->intra_matrix, old_permutation, s->dsp.idct_permutation);
1320 quant_matrix_rebuild(s->inter_matrix, old_permutation, s->dsp.idct_permutation);
1321 quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->dsp.idct_permutation);
1322 quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->dsp.idct_permutation);
1324 s1->mpeg_enc_ctx_allocated = 1;
1329 static int mpeg1_decode_picture(AVCodecContext *avctx,
1330 const uint8_t *buf, int buf_size)
1332 Mpeg1Context *s1 = avctx->priv_data;
1333 MpegEncContext *s = &s1->mpeg_enc_ctx;
1334 int ref, f_code, vbv_delay;
1336 init_get_bits(&s->gb, buf, buf_size*8);
1338 ref = get_bits(&s->gb, 10); /* temporal ref */
1339 s->pict_type = get_bits(&s->gb, 3);
1340 if (s->pict_type == 0 || s->pict_type > 3)
1343 vbv_delay = get_bits(&s->gb, 16);
1344 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
1345 s->full_pel[0] = get_bits1(&s->gb);
1346 f_code = get_bits(&s->gb, 3);
1347 if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
1349 s->mpeg_f_code[0][0] = f_code;
1350 s->mpeg_f_code[0][1] = f_code;
1352 if (s->pict_type == AV_PICTURE_TYPE_B) {
1353 s->full_pel[1] = get_bits1(&s->gb);
1354 f_code = get_bits(&s->gb, 3);
1355 if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
1357 s->mpeg_f_code[1][0] = f_code;
1358 s->mpeg_f_code[1][1] = f_code;
1360 s->current_picture.f.pict_type = s->pict_type;
1361 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1363 if (avctx->debug & FF_DEBUG_PICT_INFO)
1364 av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1371 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1373 MpegEncContext *s= &s1->mpeg_enc_ctx;
1374 int horiz_size_ext, vert_size_ext;
1377 skip_bits(&s->gb, 1); /* profile and level esc*/
1378 s->avctx->profile = get_bits(&s->gb, 3);
1379 s->avctx->level = get_bits(&s->gb, 4);
1380 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1381 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1382 horiz_size_ext = get_bits(&s->gb, 2);
1383 vert_size_ext = get_bits(&s->gb, 2);
1384 s->width |= (horiz_size_ext << 12);
1385 s->height |= (vert_size_ext << 12);
1386 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1387 s->bit_rate += (bit_rate_ext << 18) * 400;
1388 skip_bits1(&s->gb); /* marker */
1389 s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1391 s->low_delay = get_bits1(&s->gb);
1392 if (s->flags & CODEC_FLAG_LOW_DELAY)
1395 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1396 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1398 av_dlog(s->avctx, "sequence extension\n");
1399 s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO;
1400 s->avctx->sub_id = 2; /* indicates MPEG-2 found */
1402 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1403 av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1404 s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
1408 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1410 MpegEncContext *s = &s1->mpeg_enc_ctx;
1411 int color_description, w, h;
1413 skip_bits(&s->gb, 3); /* video format */
1414 color_description = get_bits1(&s->gb);
1415 if (color_description) {
1416 s->avctx->color_primaries = get_bits(&s->gb, 8);
1417 s->avctx->color_trc = get_bits(&s->gb, 8);
1418 s->avctx->colorspace = get_bits(&s->gb, 8);
1420 w = get_bits(&s->gb, 14);
1421 skip_bits(&s->gb, 1); //marker
1422 h = get_bits(&s->gb, 14);
1423 // remaining 3 bits are zero padding
1425 s1->pan_scan.width = 16 * w;
1426 s1->pan_scan.height = 16 * h;
1428 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1429 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1432 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1434 MpegEncContext *s = &s1->mpeg_enc_ctx;
1438 if (s->progressive_sequence) {
1439 if (s->repeat_first_field) {
1441 if (s->top_field_first)
1445 if (s->picture_structure == PICT_FRAME) {
1447 if (s->repeat_first_field)
1451 for (i = 0; i < nofco; i++) {
1452 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1453 skip_bits(&s->gb, 1); // marker
1454 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1455 skip_bits(&s->gb, 1); // marker
1458 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1459 av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
1460 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1461 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1462 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1465 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra)
1469 for (i = 0; i < 64; i++) {
1470 int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1471 int v = get_bits(&s->gb, 8);
1473 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1476 if (intra && i == 0 && v != 8) {
1477 av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
1478 v = 8; // needed by pink.mpg / issue1046
1487 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1489 av_dlog(s->avctx, "matrix extension\n");
1491 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1492 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1493 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
1494 if (get_bits1(&s->gb)) 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, "Missing picture start code, guessing missing values\n");
1508 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1509 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1510 s->pict_type = AV_PICTURE_TYPE_I;
1512 s->pict_type = AV_PICTURE_TYPE_P;
1514 s->pict_type = AV_PICTURE_TYPE_B;
1515 s->current_picture.f.pict_type = s->pict_type;
1516 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1518 s->intra_dc_precision = get_bits(&s->gb, 2);
1519 s->picture_structure = get_bits(&s->gb, 2);
1520 s->top_field_first = get_bits1(&s->gb);
1521 s->frame_pred_frame_dct = get_bits1(&s->gb);
1522 s->concealment_motion_vectors = get_bits1(&s->gb);
1523 s->q_scale_type = get_bits1(&s->gb);
1524 s->intra_vlc_format = get_bits1(&s->gb);
1525 s->alternate_scan = get_bits1(&s->gb);
1526 s->repeat_first_field = get_bits1(&s->gb);
1527 s->chroma_420_type = get_bits1(&s->gb);
1528 s->progressive_frame = get_bits1(&s->gb);
1530 if (s->progressive_sequence && !s->progressive_frame) {
1531 s->progressive_frame = 1;
1532 av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
1535 if (s->picture_structure == 0 || (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
1536 av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
1537 s->picture_structure = PICT_FRAME;
1540 if (s->progressive_sequence && !s->frame_pred_frame_dct) {
1541 av_log(s->avctx, AV_LOG_ERROR, "invalid frame_pred_frame_dct\n");
1542 s->frame_pred_frame_dct = 1;
1545 if (s->picture_structure == PICT_FRAME) {
1547 s->v_edge_pos = 16 * s->mb_height;
1549 s->first_field ^= 1;
1550 s->v_edge_pos = 8 * s->mb_height;
1551 memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
1554 if (s->alternate_scan) {
1555 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
1556 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
1558 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
1559 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
1562 /* composite display not parsed */
1563 av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1564 av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1565 av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1566 av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1567 av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1568 av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1569 av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1570 av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1571 av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1574 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1576 AVCodecContext *avctx = s->avctx;
1577 Mpeg1Context *s1 = (Mpeg1Context*)s;
1579 /* start frame decoding */
1580 if (s->first_field || s->picture_structure == PICT_FRAME) {
1581 if (MPV_frame_start(s, avctx) < 0)
1584 ff_er_frame_start(s);
1586 /* first check if we must repeat the frame */
1587 s->current_picture_ptr->f.repeat_pict = 0;
1588 if (s->repeat_first_field) {
1589 if (s->progressive_sequence) {
1590 if (s->top_field_first)
1591 s->current_picture_ptr->f.repeat_pict = 4;
1593 s->current_picture_ptr->f.repeat_pict = 2;
1594 } else if (s->progressive_frame) {
1595 s->current_picture_ptr->f.repeat_pict = 1;
1599 *s->current_picture_ptr->f.pan_scan = s1->pan_scan;
1601 if (HAVE_PTHREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1602 ff_thread_finish_setup(avctx);
1603 } else { // second field
1606 if (!s->current_picture_ptr) {
1607 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1611 for (i = 0; i < 4; i++) {
1612 s->current_picture.f.data[i] = s->current_picture_ptr->f.data[i];
1613 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1614 s->current_picture.f.data[i] += s->current_picture_ptr->f.linesize[i];
1619 if (avctx->hwaccel) {
1620 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
1624 // MPV_frame_start will call this function too,
1625 // but we need to call it on every field
1626 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1627 if (ff_xvmc_field_start(s, avctx) < 0)
1633 #define DECODE_SLICE_ERROR -1
1634 #define DECODE_SLICE_OK 0
1637 * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
1638 * @return DECODE_SLICE_ERROR if the slice is damaged<br>
1639 * DECODE_SLICE_OK if this slice is ok<br>
1641 static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
1642 const uint8_t **buf, int buf_size)
1644 MpegEncContext *s = &s1->mpeg_enc_ctx;
1645 AVCodecContext *avctx = s->avctx;
1646 const int lowres = s->avctx->lowres;
1647 const int field_pic = s->picture_structure != PICT_FRAME;
1650 s->resync_mb_y = -1;
1652 assert(mb_y < s->mb_height);
1654 init_get_bits(&s->gb, *buf, buf_size * 8);
1656 ff_mpeg1_clean_buffers(s);
1657 s->interlaced_dct = 0;
1659 s->qscale = get_qscale(s);
1661 if (s->qscale == 0) {
1662 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1666 /* extra slice info */
1667 while (get_bits1(&s->gb) != 0) {
1668 skip_bits(&s->gb, 8);
1673 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1677 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1679 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1686 /* otherwise, stuffing, nothing to do */
1694 if (s->mb_x >= (unsigned)s->mb_width) {
1695 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1699 if (avctx->hwaccel) {
1700 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1701 int start_code = -1;
1702 buf_end = ff_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1703 if (buf_end < *buf + buf_size)
1706 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1707 return DECODE_SLICE_ERROR;
1709 return DECODE_SLICE_OK;
1712 s->resync_mb_x = s->mb_x;
1713 s->resync_mb_y = s->mb_y = mb_y;
1715 ff_init_block_index(s);
1717 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1718 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1719 av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
1720 s->qscale, s->mpeg_f_code[0][0], s->mpeg_f_code[0][1], s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
1721 s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
1722 s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
1723 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
1724 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
1729 // If 1, we memcpy blocks in xvmcvideo.
1730 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1731 ff_xvmc_init_block(s); // set s->block
1733 if (mpeg_decode_mb(s, s->block) < 0)
1736 if (s->current_picture.f.motion_val[0] && !s->encoding) { // note motion_val is normally NULL unless we want to extract the MVs
1737 const int wrap = s->b8_stride;
1738 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1739 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1740 int motion_x, motion_y, dir, i;
1742 for (i = 0; i < 2; i++) {
1743 for (dir = 0; dir < 2; dir++) {
1744 if (s->mb_intra || (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1745 motion_x = motion_y = 0;
1746 } else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1747 motion_x = s->mv[dir][0][0];
1748 motion_y = s->mv[dir][0][1];
1749 } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
1750 motion_x = s->mv[dir][i][0];
1751 motion_y = s->mv[dir][i][1];
1754 s->current_picture.f.motion_val[dir][xy ][0] = motion_x;
1755 s->current_picture.f.motion_val[dir][xy ][1] = motion_y;
1756 s->current_picture.f.motion_val[dir][xy + 1][0] = motion_x;
1757 s->current_picture.f.motion_val[dir][xy + 1][1] = motion_y;
1758 s->current_picture.f.ref_index [dir][b8_xy ] =
1759 s->current_picture.f.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1760 assert(s->field_select[dir][i] == 0 || s->field_select[dir][i] == 1);
1767 s->dest[0] += 16 >> lowres;
1768 s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
1769 s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
1771 MPV_decode_mb(s, s->block);
1773 if (++s->mb_x >= s->mb_width) {
1774 const int mb_size = 16 >> s->avctx->lowres;
1776 ff_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
1777 MPV_report_decode_progress(s);
1780 s->mb_y += 1 << field_pic;
1782 if (s->mb_y >= s->mb_height) {
1783 int left = get_bits_left(&s->gb);
1784 int is_d10 = s->chroma_format == 2 && s->pict_type == AV_PICTURE_TYPE_I && avctx->profile == 0 && avctx->level == 5
1785 && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
1786 && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
1788 if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
1789 || (avctx->error_recognition >= FF_ER_AGGRESSIVE && left > 8)) {
1790 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
1796 ff_init_block_index(s);
1799 /* skip mb handling */
1800 if (s->mb_skip_run == -1) {
1801 /* read increment again */
1804 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1806 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1811 s->mb_skip_run += 33;
1812 } else if (code == 35) {
1813 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1814 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1817 goto eos; /* end of slice */
1819 /* otherwise, stuffing, nothing to do */
1821 s->mb_skip_run += code;
1825 if (s->mb_skip_run) {
1827 if (s->pict_type == AV_PICTURE_TYPE_I) {
1828 av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1834 for (i = 0; i < 12; i++)
1835 s->block_last_index[i] = -1;
1836 if (s->picture_structure == PICT_FRAME)
1837 s->mv_type = MV_TYPE_16X16;
1839 s->mv_type = MV_TYPE_FIELD;
1840 if (s->pict_type == AV_PICTURE_TYPE_P) {
1841 /* if P type, zero motion vector is implied */
1842 s->mv_dir = MV_DIR_FORWARD;
1843 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1844 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1845 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1846 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1848 /* if B type, reuse previous vectors and directions */
1849 s->mv[0][0][0] = s->last_mv[0][0][0];
1850 s->mv[0][0][1] = s->last_mv[0][0][1];
1851 s->mv[1][0][0] = s->last_mv[1][0][0];
1852 s->mv[1][0][1] = s->last_mv[1][0][1];
1857 eos: // end of slice
1858 *buf += (get_bits_count(&s->gb)-1)/8;
1859 //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1863 static int slice_decode_thread(AVCodecContext *c, void *arg)
1865 MpegEncContext *s = *(void**)arg;
1866 const uint8_t *buf = s->gb.buffer;
1867 int mb_y = s->start_mb_y;
1868 const int field_pic = s->picture_structure != PICT_FRAME;
1870 s->error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
1873 uint32_t start_code;
1876 ret = mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
1878 //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1879 //ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, s->start_mb_y, s->end_mb_y, s->error_count);
1881 if (c->error_recognition >= FF_ER_EXPLODE)
1883 if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
1884 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, AC_ERROR | DC_ERROR | MV_ERROR);
1886 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END | DC_END | MV_END);
1889 if (s->mb_y == s->end_mb_y)
1893 buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
1894 mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
1895 if (s->picture_structure == PICT_BOTTOM_FIELD)
1897 if (mb_y < 0 || mb_y >= s->end_mb_y)
1903 * Handle slice ends.
1904 * @return 1 if it seems to be the last slice
1906 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
1908 Mpeg1Context *s1 = avctx->priv_data;
1909 MpegEncContext *s = &s1->mpeg_enc_ctx;
1911 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
1914 if (s->avctx->hwaccel) {
1915 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1916 av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
1919 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1920 ff_xvmc_field_end(s);
1922 /* end of slice reached */
1923 if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field) {
1926 s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_MPEG2;
1932 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
1933 *pict = *(AVFrame*)s->current_picture_ptr;
1934 ff_print_debug_info(s, pict);
1936 if (avctx->active_thread_type & FF_THREAD_FRAME)
1937 s->picture_number++;
1938 /* latency of 1 frame for I- and P-frames */
1939 /* XXX: use another variable than picture_number */
1940 if (s->last_picture_ptr != NULL) {
1941 *pict = *(AVFrame*)s->last_picture_ptr;
1942 ff_print_debug_info(s, pict);
1952 static int mpeg1_decode_sequence(AVCodecContext *avctx,
1953 const uint8_t *buf, int buf_size)
1955 Mpeg1Context *s1 = avctx->priv_data;
1956 MpegEncContext *s = &s1->mpeg_enc_ctx;
1960 init_get_bits(&s->gb, buf, buf_size*8);
1962 width = get_bits(&s->gb, 12);
1963 height = get_bits(&s->gb, 12);
1964 if (width <= 0 || height <= 0)
1966 s->aspect_ratio_info = get_bits(&s->gb, 4);
1967 if (s->aspect_ratio_info == 0) {
1968 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
1969 if (avctx->error_recognition >= FF_ER_COMPLIANT)
1972 s->frame_rate_index = get_bits(&s->gb, 4);
1973 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
1975 s->bit_rate = get_bits(&s->gb, 18) * 400;
1976 if (get_bits1(&s->gb) == 0) /* marker */
1981 s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
1982 skip_bits(&s->gb, 1);
1985 if (get_bits1(&s->gb)) {
1986 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1988 for (i = 0; i < 64; i++) {
1989 j = s->dsp.idct_permutation[i];
1990 v = ff_mpeg1_default_intra_matrix[i];
1991 s->intra_matrix[j] = v;
1992 s->chroma_intra_matrix[j] = v;
1995 if (get_bits1(&s->gb)) {
1996 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1998 for (i = 0; i < 64; i++) {
1999 int j = s->dsp.idct_permutation[i];
2000 v = ff_mpeg1_default_non_intra_matrix[i];
2001 s->inter_matrix[j] = v;
2002 s->chroma_inter_matrix[j] = v;
2006 if (show_bits(&s->gb, 23) != 0) {
2007 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2011 /* we set MPEG-2 parameters so that it emulates MPEG-1 */
2012 s->progressive_sequence = 1;
2013 s->progressive_frame = 1;
2014 s->picture_structure = PICT_FRAME;
2015 s->frame_pred_frame_dct = 1;
2016 s->chroma_format = 1;
2017 s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG1VIDEO;
2018 avctx->sub_id = 1; /* indicates MPEG-1 */
2019 s->out_format = FMT_MPEG1;
2020 s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
2021 if (s->flags & CODEC_FLAG_LOW_DELAY)
2024 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2025 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2026 s->avctx->rc_buffer_size, s->bit_rate);
2031 static int vcr2_init_sequence(AVCodecContext *avctx)
2033 Mpeg1Context *s1 = avctx->priv_data;
2034 MpegEncContext *s = &s1->mpeg_enc_ctx;
2037 /* start new MPEG-1 context decoding */
2038 s->out_format = FMT_MPEG1;
2039 if (s1->mpeg_enc_ctx_allocated) {
2042 s->width = avctx->coded_width;
2043 s->height = avctx->coded_height;
2044 avctx->has_b_frames = 0; // true?
2047 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2048 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
2050 if (avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel ||
2051 s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
2052 if (avctx->idct_algo == FF_IDCT_AUTO)
2053 avctx->idct_algo = FF_IDCT_SIMPLE;
2055 if (MPV_common_init(s) < 0)
2057 exchange_uv(s); // common init reset pblocks, so we swap them here
2058 s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
2059 s1->mpeg_enc_ctx_allocated = 1;
2061 for (i = 0; i < 64; i++) {
2062 int j = s->dsp.idct_permutation[i];
2063 v = ff_mpeg1_default_intra_matrix[i];
2064 s->intra_matrix[j] = v;
2065 s->chroma_intra_matrix[j] = v;
2067 v = ff_mpeg1_default_non_intra_matrix[i];
2068 s->inter_matrix[j] = v;
2069 s->chroma_inter_matrix[j] = v;
2072 s->progressive_sequence = 1;
2073 s->progressive_frame = 1;
2074 s->picture_structure = PICT_FRAME;
2075 s->frame_pred_frame_dct = 1;
2076 s->chroma_format = 1;
2077 s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO;
2078 avctx->sub_id = 2; /* indicates MPEG-2 */
2079 s1->save_width = s->width;
2080 s1->save_height = s->height;
2081 s1->save_progressive_seq = s->progressive_sequence;
2086 static void mpeg_decode_user_data(AVCodecContext *avctx,
2087 const uint8_t *p, int buf_size)
2089 const uint8_t *buf_end = p + buf_size;
2091 /* we parse the DTG active format information */
2092 if (buf_end - p >= 5 &&
2093 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2101 if (buf_end - p < 1)
2103 avctx->dtg_active_format = p[0] & 0x0f;
2108 static void mpeg_decode_gop(AVCodecContext *avctx,
2109 const uint8_t *buf, int buf_size)
2111 Mpeg1Context *s1 = avctx->priv_data;
2112 MpegEncContext *s = &s1->mpeg_enc_ctx;
2114 int time_code_hours, time_code_minutes;
2115 int time_code_seconds, time_code_pictures;
2118 init_get_bits(&s->gb, buf, buf_size*8);
2120 skip_bits1(&s->gb); /* drop_frame_flag */
2122 time_code_hours = get_bits(&s->gb, 5);
2123 time_code_minutes = get_bits(&s->gb, 6);
2124 skip_bits1(&s->gb); // marker bit
2125 time_code_seconds = get_bits(&s->gb, 6);
2126 time_code_pictures = get_bits(&s->gb, 6);
2128 s->closed_gop = get_bits1(&s->gb);
2129 /*broken_link indicate that after editing the
2130 reference frames of the first B-Frames after GOP I-Frame
2131 are missing (open gop)*/
2132 broken_link = get_bits1(&s->gb);
2134 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2135 av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
2136 time_code_hours, time_code_minutes, time_code_seconds,
2137 time_code_pictures, s->closed_gop, broken_link);
2140 * Find the end of the current frame in the bitstream.
2141 * @return the position of the first byte of the next frame, or -1
2143 int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
2146 uint32_t state = pc->state;
2148 /* EOF considered as end of frame */
2153 0 frame start -> 1/4
2154 1 first_SEQEXT -> 0/2
2155 2 first field start -> 3/0
2156 3 second_SEQEXT -> 2/0
2160 for (i = 0; i < buf_size; i++) {
2161 assert(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
2162 if (pc->frame_start_found & 1) {
2163 if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
2164 pc->frame_start_found--;
2165 else if (state == EXT_START_CODE + 2) {
2166 if ((buf[i] & 3) == 3)
2167 pc->frame_start_found = 0;
2169 pc->frame_start_found = (pc->frame_start_found + 1) & 3;
2173 i = ff_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
2174 if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
2176 pc->frame_start_found = 4;
2178 if (state == SEQ_END_CODE) {
2182 if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
2183 pc->frame_start_found = 0;
2184 if (pc->frame_start_found < 4 && state == EXT_START_CODE)
2185 pc->frame_start_found++;
2186 if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
2187 if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
2188 pc->frame_start_found = 0;
2193 if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
2194 ff_fetch_timestamp(s, i - 3, 1);
2199 return END_NOT_FOUND;
2202 static int decode_chunks(AVCodecContext *avctx,
2203 AVFrame *picture, int *data_size,
2204 const uint8_t *buf, int buf_size);
2206 /* handle buffering and image synchronisation */
2207 static int mpeg_decode_frame(AVCodecContext *avctx,
2208 void *data, int *data_size,
2211 const uint8_t *buf = avpkt->data;
2212 int buf_size = avpkt->size;
2213 Mpeg1Context *s = avctx->priv_data;
2214 AVFrame *picture = data;
2215 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2216 av_dlog(avctx, "fill_buffer\n");
2218 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2219 /* special case for last picture */
2220 if (s2->low_delay == 0 && s2->next_picture_ptr) {
2221 *picture = *(AVFrame*)s2->next_picture_ptr;
2222 s2->next_picture_ptr = NULL;
2224 *data_size = sizeof(AVFrame);
2229 if (s2->flags & CODEC_FLAG_TRUNCATED) {
2230 int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
2232 if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
2236 if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
2237 vcr2_init_sequence(avctx);
2241 if (avctx->extradata && !avctx->frame_number) {
2242 int ret = decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
2243 if (ret < 0 && avctx->error_recognition >= FF_ER_EXPLODE)
2247 return decode_chunks(avctx, picture, data_size, buf, buf_size);
2250 static int decode_chunks(AVCodecContext *avctx,
2251 AVFrame *picture, int *data_size,
2252 const uint8_t *buf, int buf_size)
2254 Mpeg1Context *s = avctx->priv_data;
2255 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2256 const uint8_t *buf_ptr = buf;
2257 const uint8_t *buf_end = buf + buf_size;
2258 int ret, input_size;
2262 /* find next start code */
2263 uint32_t start_code = -1;
2264 buf_ptr = ff_find_start_code(buf_ptr, buf_end, &start_code);
2265 if (start_code > 0x1ff) {
2266 if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {
2267 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
2270 avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
2271 for (i = 0; i < s->slice_count; i++)
2272 s2->error_count += s2->thread_context[i]->error_count;
2275 if (CONFIG_MPEG_VDPAU_DECODER && avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
2276 ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
2278 if (slice_end(avctx, picture)) {
2279 if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
2280 *data_size = sizeof(AVPicture);
2284 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2287 input_size = buf_end - buf_ptr;
2289 if (avctx->debug & FF_DEBUG_STARTCODE) {
2290 av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
2293 /* prepare data for next start code */
2294 switch (start_code) {
2295 case SEQ_START_CODE:
2296 if (last_code == 0) {
2297 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2300 av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
2301 if (avctx->error_recognition >= FF_ER_EXPLODE)
2302 return AVERROR_INVALIDDATA;
2306 case PICTURE_START_CODE:
2307 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {
2310 avctx->execute(avctx, slice_decode_thread,
2311 s2->thread_context, NULL,
2312 s->slice_count, sizeof(void*));
2313 for (i = 0; i < s->slice_count; i++)
2314 s2->error_count += s2->thread_context[i]->error_count;
2317 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2318 ret = mpeg_decode_postinit(avctx);
2320 av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
2324 /* we have a complete image: we try to decompress it */
2325 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2327 s2->first_slice = 1;
2328 last_code = PICTURE_START_CODE;
2330 av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
2331 if (avctx->error_recognition >= FF_ER_EXPLODE)
2332 return AVERROR_INVALIDDATA;
2335 case EXT_START_CODE:
2336 init_get_bits(&s2->gb, buf_ptr, input_size*8);
2338 switch (get_bits(&s2->gb, 4)) {
2340 if (last_code == 0) {
2341 mpeg_decode_sequence_extension(s);
2343 av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
2344 if (avctx->error_recognition >= FF_ER_EXPLODE)
2345 return AVERROR_INVALIDDATA;
2349 mpeg_decode_sequence_display_extension(s);
2352 mpeg_decode_quant_matrix_extension(s2);
2355 mpeg_decode_picture_display_extension(s);
2358 if (last_code == PICTURE_START_CODE) {
2359 mpeg_decode_picture_coding_extension(s);
2361 av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
2362 if (avctx->error_recognition >= FF_ER_EXPLODE)
2363 return AVERROR_INVALIDDATA;
2368 case USER_START_CODE:
2369 mpeg_decode_user_data(avctx, buf_ptr, input_size);
2371 case GOP_START_CODE:
2372 if (last_code == 0) {
2374 mpeg_decode_gop(avctx, buf_ptr, input_size);
2377 av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
2378 if (avctx->error_recognition >= FF_ER_EXPLODE)
2379 return AVERROR_INVALIDDATA;
2383 if (start_code >= SLICE_MIN_START_CODE &&
2384 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2385 const int field_pic = s2->picture_structure != PICT_FRAME;
2386 int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
2387 last_code = SLICE_MIN_START_CODE;
2389 if (s2->picture_structure == PICT_BOTTOM_FIELD)
2392 if (mb_y >= s2->mb_height) {
2393 av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2397 if (s2->last_picture_ptr == NULL) {
2398 /* Skip B-frames if we do not have reference frames and gop is not closed */
2399 if (s2->pict_type == AV_PICTURE_TYPE_B) {
2400 if (!s2->closed_gop)
2404 if (s2->pict_type == AV_PICTURE_TYPE_I)
2406 if (s2->next_picture_ptr == NULL) {
2407 /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
2408 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) break;
2410 if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||
2411 (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||
2412 avctx->skip_frame >= AVDISCARD_ALL)
2415 if (!s->mpeg_enc_ctx_allocated)
2418 if (s2->codec_id == CODEC_ID_MPEG2VIDEO) {
2419 if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
2423 if (!s2->pict_type) {
2424 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2425 if (avctx->error_recognition >= FF_ER_EXPLODE)
2426 return AVERROR_INVALIDDATA;
2430 if (s2->first_slice) {
2431 s2->first_slice = 0;
2432 if (mpeg_field_start(s2, buf, buf_size) < 0)
2435 if (!s2->current_picture_ptr) {
2436 av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
2437 return AVERROR_INVALIDDATA;
2440 if (avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
2445 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
2446 int threshold= (s2->mb_height * s->slice_count + avctx->thread_count / 2) / avctx->thread_count;
2447 if (threshold <= mb_y) {
2448 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2450 thread_context->start_mb_y = mb_y;
2451 thread_context->end_mb_y = s2->mb_height;
2452 if (s->slice_count) {
2453 s2->thread_context[s->slice_count-1]->end_mb_y = mb_y;
2454 ff_update_duplicate_context(thread_context, s2);
2456 init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
2459 buf_ptr += 2; // FIXME add minimum number of bytes per slice
2461 ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
2465 if (avctx->error_recognition >= FF_ER_EXPLODE)
2467 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2468 ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR | DC_ERROR | MV_ERROR);
2470 ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, AC_END | DC_END | MV_END);
2479 static void flush(AVCodecContext *avctx)
2481 Mpeg1Context *s = avctx->priv_data;
2485 ff_mpeg_flush(avctx);
2488 static int mpeg_decode_end(AVCodecContext *avctx)
2490 Mpeg1Context *s = avctx->priv_data;
2492 if (s->mpeg_enc_ctx_allocated)
2493 MPV_common_end(&s->mpeg_enc_ctx);
2497 static const AVProfile mpeg2_video_profiles[] = {
2498 { FF_PROFILE_MPEG2_422, "4:2:2" },
2499 { FF_PROFILE_MPEG2_HIGH, "High" },
2500 { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2501 { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2502 { FF_PROFILE_MPEG2_MAIN, "Main" },
2503 { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2504 { FF_PROFILE_RESERVED, "Reserved" },
2505 { FF_PROFILE_RESERVED, "Reserved" },
2506 { FF_PROFILE_UNKNOWN },
2510 AVCodec ff_mpeg1video_decoder = {
2511 .name = "mpeg1video",
2512 .type = AVMEDIA_TYPE_VIDEO,
2513 .id = CODEC_ID_MPEG1VIDEO,
2514 .priv_data_size = sizeof(Mpeg1Context),
2515 .init = mpeg_decode_init,
2516 .close = mpeg_decode_end,
2517 .decode = mpeg_decode_frame,
2518 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
2521 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2522 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2525 AVCodec ff_mpeg2video_decoder = {
2526 .name = "mpeg2video",
2527 .type = AVMEDIA_TYPE_VIDEO,
2528 .id = CODEC_ID_MPEG2VIDEO,
2529 .priv_data_size = sizeof(Mpeg1Context),
2530 .init = mpeg_decode_init,
2531 .close = mpeg_decode_end,
2532 .decode = mpeg_decode_frame,
2533 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
2536 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2537 .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2541 AVCodec ff_mpegvideo_decoder = {
2542 .name = "mpegvideo",
2543 .type = AVMEDIA_TYPE_VIDEO,
2544 .id = CODEC_ID_MPEG2VIDEO,
2545 .priv_data_size = sizeof(Mpeg1Context),
2546 .init = mpeg_decode_init,
2547 .close = mpeg_decode_end,
2548 .decode = mpeg_decode_frame,
2549 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
2552 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2555 #if CONFIG_MPEG_XVMC_DECODER
2556 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2558 if (avctx->active_thread_type & FF_THREAD_SLICE)
2560 if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2562 if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
2563 av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2565 mpeg_decode_init(avctx);
2567 avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
2568 avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2573 AVCodec ff_mpeg_xvmc_decoder = {
2574 .name = "mpegvideo_xvmc",
2575 .type = AVMEDIA_TYPE_VIDEO,
2576 .id = CODEC_ID_MPEG2VIDEO_XVMC,
2577 .priv_data_size = sizeof(Mpeg1Context),
2578 .init = mpeg_mc_decode_init,
2579 .close = mpeg_decode_end,
2580 .decode = mpeg_decode_frame,
2581 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2583 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2588 #if CONFIG_MPEG_VDPAU_DECODER
2589 AVCodec ff_mpeg_vdpau_decoder = {
2590 .name = "mpegvideo_vdpau",
2591 .type = AVMEDIA_TYPE_VIDEO,
2592 .id = CODEC_ID_MPEG2VIDEO,
2593 .priv_data_size = sizeof(Mpeg1Context),
2594 .init = mpeg_decode_init,
2595 .close = mpeg_decode_end,
2596 .decode = mpeg_decode_frame,
2597 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2599 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
2603 #if CONFIG_MPEG1_VDPAU_DECODER
2604 AVCodec ff_mpeg1_vdpau_decoder = {
2605 .name = "mpeg1video_vdpau",
2606 .type = AVMEDIA_TYPE_VIDEO,
2607 .id = CODEC_ID_MPEG1VIDEO,
2608 .priv_data_size = sizeof(Mpeg1Context),
2609 .init = mpeg_decode_init,
2610 .close = mpeg_decode_end,
2611 .decode = mpeg_decode_frame,
2612 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2614 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),