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, 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 return sign_extend(val, 5 + shift);
83 static inline int mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
85 int level, dc, diff, i, j, run;
87 RLTable *rl = &ff_rl_mpeg1;
88 uint8_t * const scantable = s->intra_scantable.permutated;
89 const uint16_t *quant_matrix = s->intra_matrix;
90 const int qscale = s->qscale;
93 component = (n <= 3 ? 0 : n - 4 + 1);
94 diff = decode_dc(&s->gb, component);
97 dc = s->last_dc[component];
99 s->last_dc[component] = dc;
100 block[0] = dc * quant_matrix[0];
101 av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
104 OPEN_READER(re, &s->gb);
105 /* now quantify & encode AC coefficients */
107 UPDATE_CACHE(re, &s->gb);
108 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
112 } else if (level != 0) {
115 level = (level * qscale * quant_matrix[j]) >> 4;
116 level = (level - 1) | 1;
117 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
118 LAST_SKIP_BITS(re, &s->gb, 1);
121 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
122 UPDATE_CACHE(re, &s->gb);
123 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
125 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
126 } else if (level == 0) {
127 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
133 level = (level * qscale * quant_matrix[j]) >> 4;
134 level = (level - 1) | 1;
137 level = (level * qscale * quant_matrix[j]) >> 4;
138 level = (level - 1) | 1;
142 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
148 CLOSE_READER(re, &s->gb);
150 s->block_last_index[n] = i;
154 int ff_mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
156 return mpeg1_decode_block_intra(s, block, n);
159 static inline int mpeg1_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
161 int level, i, j, run;
162 RLTable *rl = &ff_rl_mpeg1;
163 uint8_t * const scantable = s->intra_scantable.permutated;
164 const uint16_t *quant_matrix = s->inter_matrix;
165 const int qscale = s->qscale;
168 OPEN_READER(re, &s->gb);
170 // special case for first coefficient, no need to add second VLC table
171 UPDATE_CACHE(re, &s->gb);
172 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
173 level = (3 * qscale * quant_matrix[0]) >> 5;
174 level = (level - 1) | 1;
175 if (GET_CACHE(re, &s->gb) & 0x40000000)
179 SKIP_BITS(re, &s->gb, 2);
180 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
183 /* now quantify & encode AC coefficients */
185 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
190 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
191 level = (level - 1) | 1;
192 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
193 SKIP_BITS(re, &s->gb, 1);
196 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
197 UPDATE_CACHE(re, &s->gb);
198 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
200 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
201 } else if (level == 0) {
202 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
208 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
209 level = (level - 1) | 1;
212 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
213 level = (level - 1) | 1;
217 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
222 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
224 UPDATE_CACHE(re, &s->gb);
227 LAST_SKIP_BITS(re, &s->gb, 2);
228 CLOSE_READER(re, &s->gb);
230 s->block_last_index[n] = i;
234 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
236 int level, i, j, run;
237 RLTable *rl = &ff_rl_mpeg1;
238 uint8_t * const scantable = s->intra_scantable.permutated;
239 const int qscale = s->qscale;
242 OPEN_READER(re, &s->gb);
244 // special case for first coefficient, no need to add second VLC table
245 UPDATE_CACHE(re, &s->gb);
246 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
247 level = (3 * qscale) >> 1;
248 level = (level - 1) | 1;
249 if (GET_CACHE(re, &s->gb) & 0x40000000)
253 SKIP_BITS(re, &s->gb, 2);
254 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
258 /* now quantify & encode AC coefficients */
260 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
265 level = ((level * 2 + 1) * qscale) >> 1;
266 level = (level - 1) | 1;
267 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
268 SKIP_BITS(re, &s->gb, 1);
271 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
272 UPDATE_CACHE(re, &s->gb);
273 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
275 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
276 } else if (level == 0) {
277 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
283 level = ((level * 2 + 1) * qscale) >> 1;
284 level = (level - 1) | 1;
287 level = ((level * 2 + 1) * qscale) >> 1;
288 level = (level - 1) | 1;
293 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
295 UPDATE_CACHE(re, &s->gb);
298 LAST_SKIP_BITS(re, &s->gb, 2);
299 CLOSE_READER(re, &s->gb);
301 s->block_last_index[n] = i;
306 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n)
308 int level, i, j, run;
309 RLTable *rl = &ff_rl_mpeg1;
310 uint8_t * const scantable = s->intra_scantable.permutated;
311 const uint16_t *quant_matrix;
312 const int qscale = s->qscale;
318 OPEN_READER(re, &s->gb);
321 quant_matrix = s->inter_matrix;
323 quant_matrix = s->chroma_inter_matrix;
325 // special case for first coefficient, no need to add second VLC table
326 UPDATE_CACHE(re, &s->gb);
327 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
328 level= (3 * qscale * quant_matrix[0]) >> 5;
329 if (GET_CACHE(re, &s->gb) & 0x40000000)
334 SKIP_BITS(re, &s->gb, 2);
335 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
339 /* now quantify & encode AC coefficients */
341 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
346 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
347 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
348 SKIP_BITS(re, &s->gb, 1);
351 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
352 UPDATE_CACHE(re, &s->gb);
353 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
358 level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
361 level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
365 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
371 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
373 UPDATE_CACHE(re, &s->gb);
376 LAST_SKIP_BITS(re, &s->gb, 2);
377 CLOSE_READER(re, &s->gb);
379 block[63] ^= (mismatch & 1);
381 s->block_last_index[n] = i;
385 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
386 DCTELEM *block, int n)
388 int level, i, j, run;
389 RLTable *rl = &ff_rl_mpeg1;
390 uint8_t * const scantable = s->intra_scantable.permutated;
391 const int qscale = s->qscale;
392 OPEN_READER(re, &s->gb);
395 // special case for first coefficient, no need to add second VLC table
396 UPDATE_CACHE(re, &s->gb);
397 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
398 level = (3 * qscale) >> 1;
399 if (GET_CACHE(re, &s->gb) & 0x40000000)
403 SKIP_BITS(re, &s->gb, 2);
404 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
408 /* now quantify & encode AC coefficients */
410 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
415 level = ((level * 2 + 1) * qscale) >> 1;
416 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
417 SKIP_BITS(re, &s->gb, 1);
420 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
421 UPDATE_CACHE(re, &s->gb);
422 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
427 level = ((-level * 2 + 1) * qscale) >> 1;
430 level = ((level * 2 + 1) * qscale) >> 1;
435 if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
437 UPDATE_CACHE(re, &s->gb);
440 LAST_SKIP_BITS(re, &s->gb, 2);
441 CLOSE_READER(re, &s->gb);
442 s->block_last_index[n] = i;
447 static inline int mpeg2_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
449 int level, dc, diff, i, j, run;
452 uint8_t * const scantable = s->intra_scantable.permutated;
453 const uint16_t *quant_matrix;
454 const int qscale = s->qscale;
459 quant_matrix = s->intra_matrix;
462 quant_matrix = s->chroma_intra_matrix;
463 component = (n & 1) + 1;
465 diff = decode_dc(&s->gb, component);
468 dc = s->last_dc[component];
470 s->last_dc[component] = dc;
471 block[0] = dc << (3 - s->intra_dc_precision);
472 av_dlog(s->avctx, "dc=%d\n", block[0]);
473 mismatch = block[0] ^ 1;
475 if (s->intra_vlc_format)
481 OPEN_READER(re, &s->gb);
482 /* now quantify & encode AC coefficients */
484 UPDATE_CACHE(re, &s->gb);
485 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
489 } else if (level != 0) {
492 level = (level * qscale * quant_matrix[j]) >> 4;
493 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
494 LAST_SKIP_BITS(re, &s->gb, 1);
497 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
498 UPDATE_CACHE(re, &s->gb);
499 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
503 level = (-level * qscale * quant_matrix[j]) >> 4;
506 level = (level * qscale * quant_matrix[j]) >> 4;
510 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
517 CLOSE_READER(re, &s->gb);
519 block[63] ^= mismatch & 1;
521 s->block_last_index[n] = i;
525 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
527 int level, dc, diff, j, run;
530 uint8_t * scantable = s->intra_scantable.permutated;
531 const uint16_t *quant_matrix;
532 const int qscale = s->qscale;
536 quant_matrix = s->intra_matrix;
539 quant_matrix = s->chroma_intra_matrix;
540 component = (n & 1) + 1;
542 diff = decode_dc(&s->gb, component);
545 dc = s->last_dc[component];
547 s->last_dc[component] = dc;
548 block[0] = dc << (3 - s->intra_dc_precision);
549 if (s->intra_vlc_format)
555 OPEN_READER(re, &s->gb);
556 /* now quantify & encode AC coefficients */
558 UPDATE_CACHE(re, &s->gb);
559 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
563 } else if (level != 0) {
566 level = (level * qscale * quant_matrix[j]) >> 4;
567 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
568 LAST_SKIP_BITS(re, &s->gb, 1);
571 run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
572 UPDATE_CACHE(re, &s->gb);
573 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
577 level = (-level * qscale * quant_matrix[j]) >> 4;
580 level = (level * qscale * quant_matrix[j]) >> 4;
586 CLOSE_READER(re, &s->gb);
589 s->block_last_index[n] = scantable - s->intra_scantable.permutated;
593 uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
595 #define INIT_2D_VLC_RL(rl, static_size)\
597 static RL_VLC_ELEM rl_vlc_table[static_size];\
598 INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
599 &rl.table_vlc[0][1], 4, 2,\
600 &rl.table_vlc[0][0], 4, 2, static_size);\
602 rl.rl_vlc[0] = rl_vlc_table;\
603 init_2d_vlc_rl(&rl);\
606 static void init_2d_vlc_rl(RLTable *rl)
610 for (i = 0; i < rl->vlc.table_size; i++) {
611 int code = rl->vlc.table[i][0];
612 int len = rl->vlc.table[i][1];
615 if (len == 0) { // illegal code
618 } else if (len<0) { //more bits needed
622 if (code == rl->n) { //esc
625 } else if (code == rl->n+1) { //eob
629 run = rl->table_run [code] + 1;
630 level = rl->table_level[code];
633 rl->rl_vlc[0][i].len = len;
634 rl->rl_vlc[0][i].level = level;
635 rl->rl_vlc[0][i].run = run;
639 void ff_mpeg12_common_init(MpegEncContext *s)
642 s->y_dc_scale_table =
643 s->c_dc_scale_table = ff_mpeg2_dc_scale_table[s->intra_dc_precision];
647 void ff_mpeg1_clean_buffers(MpegEncContext *s)
649 s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
650 s->last_dc[1] = s->last_dc[0];
651 s->last_dc[2] = s->last_dc[0];
652 memset(s->last_mv, 0, sizeof(s->last_mv));
656 /******************************************/
660 VLC ff_dc_chroma_vlc;
662 static VLC mbincr_vlc;
663 static VLC mb_ptype_vlc;
664 static VLC mb_btype_vlc;
665 static VLC mb_pat_vlc;
667 av_cold void ff_mpeg12_init_vlcs(void)
674 INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
675 ff_mpeg12_vlc_dc_lum_bits, 1, 1,
676 ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
677 INIT_VLC_STATIC(&ff_dc_chroma_vlc, DC_VLC_BITS, 12,
678 ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
679 ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
680 INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
681 &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
682 &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
683 INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
684 &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
685 &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
686 INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
687 &ff_mpeg12_mbPatTable[0][1], 2, 1,
688 &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
690 INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
691 &table_mb_ptype[0][1], 2, 1,
692 &table_mb_ptype[0][0], 2, 1, 64);
693 INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
694 &table_mb_btype[0][1], 2, 1,
695 &table_mb_btype[0][0], 2, 1, 64);
696 init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
697 init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
699 INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
700 INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
704 static inline int get_dmv(MpegEncContext *s)
706 if (get_bits1(&s->gb))
707 return 1 - (get_bits1(&s->gb) << 1);
712 static inline int get_qscale(MpegEncContext *s)
714 int qscale = get_bits(&s->gb, 5);
715 if (s->q_scale_type) {
716 return non_linear_qscale[qscale];
722 static void exchange_uv(MpegEncContext *s)
727 s->pblocks[4] = s->pblocks[5];
731 /* motion type (for MPEG-2) */
737 static int mpeg_decode_mb(Mpeg1Context *s1, DCTELEM block[12][64])
739 MpegEncContext *s = &s1->mpeg_enc_ctx;
740 int i, j, k, cbp, val, mb_type, motion_type;
741 const int mb_block_count = 4 + (1 << s->chroma_format);
743 av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
745 assert(s->mb_skipped == 0);
747 if (s->mb_skip_run-- != 0) {
748 if (s->pict_type == AV_PICTURE_TYPE_P) {
750 s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
755 mb_type = s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
757 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
758 if (IS_INTRA(mb_type))
760 s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride] =
761 mb_type | MB_TYPE_SKIP;
762 // assert(s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1] & (MB_TYPE_16x16 | MB_TYPE_16x8));
764 if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
771 switch (s->pict_type) {
773 case AV_PICTURE_TYPE_I:
774 if (get_bits1(&s->gb) == 0) {
775 if (get_bits1(&s->gb) == 0) {
776 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
779 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
781 mb_type = MB_TYPE_INTRA;
784 case AV_PICTURE_TYPE_P:
785 mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
787 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
790 mb_type = ptype2mb_type[mb_type];
792 case AV_PICTURE_TYPE_B:
793 mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
795 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
798 mb_type = btype2mb_type[mb_type];
801 av_dlog(s->avctx, "mb_type=%x\n", mb_type);
802 // motion_type = 0; /* avoid warning */
803 if (IS_INTRA(mb_type)) {
804 s->dsp.clear_blocks(s->block[0]);
806 if (!s->chroma_y_shift) {
807 s->dsp.clear_blocks(s->block[6]);
810 /* compute DCT type */
811 if (s->picture_structure == PICT_FRAME && // FIXME add an interlaced_dct coded var?
812 !s->frame_pred_frame_dct) {
813 s->interlaced_dct = get_bits1(&s->gb);
816 if (IS_QUANT(mb_type))
817 s->qscale = get_qscale(s);
819 if (s->concealment_motion_vectors) {
820 /* just parse them */
821 if (s->picture_structure != PICT_FRAME)
822 skip_bits1(&s->gb); /* field select */
824 s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
825 mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
826 s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
827 mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
829 skip_bits1(&s->gb); /* marker */
831 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
833 // if 1, we memcpy blocks in xvmcvideo
834 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
835 ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
841 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
842 if (s->flags2 & CODEC_FLAG2_FAST) {
843 for (i = 0; i < 6; i++) {
844 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
847 for (i = 0; i < mb_block_count; i++) {
848 if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
853 for (i = 0; i < 6; i++) {
854 if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
859 if (mb_type & MB_TYPE_ZERO_MV) {
860 assert(mb_type & MB_TYPE_CBP);
862 s->mv_dir = MV_DIR_FORWARD;
863 if (s->picture_structure == PICT_FRAME) {
864 if (!s->frame_pred_frame_dct)
865 s->interlaced_dct = get_bits1(&s->gb);
866 s->mv_type = MV_TYPE_16X16;
868 s->mv_type = MV_TYPE_FIELD;
869 mb_type |= MB_TYPE_INTERLACED;
870 s->field_select[0][0] = s->picture_structure - 1;
873 if (IS_QUANT(mb_type))
874 s->qscale = get_qscale(s);
876 s->last_mv[0][0][0] = 0;
877 s->last_mv[0][0][1] = 0;
878 s->last_mv[0][1][0] = 0;
879 s->last_mv[0][1][1] = 0;
883 assert(mb_type & MB_TYPE_L0L1);
884 // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
885 /* get additional motion vector type */
886 if (s->frame_pred_frame_dct)
887 motion_type = MT_FRAME;
889 motion_type = get_bits(&s->gb, 2);
890 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
891 s->interlaced_dct = get_bits1(&s->gb);
894 if (IS_QUANT(mb_type))
895 s->qscale = get_qscale(s);
898 s->mv_dir = (mb_type >> 13) & 3;
899 av_dlog(s->avctx, "motion_type=%d\n", motion_type);
900 switch (motion_type) {
901 case MT_FRAME: /* or MT_16X8 */
902 if (s->picture_structure == PICT_FRAME) {
903 mb_type |= MB_TYPE_16x16;
904 s->mv_type = MV_TYPE_16X16;
905 for (i = 0; i < 2; i++) {
906 if (USES_LIST(mb_type, i)) {
908 s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
909 mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
910 s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
911 mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
912 /* full_pel: only for MPEG-1 */
913 if (s1->full_pel[i]) {
914 s->mv[i][0][0] <<= 1;
915 s->mv[i][0][1] <<= 1;
920 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
921 s->mv_type = MV_TYPE_16X8;
922 for (i = 0; i < 2; i++) {
923 if (USES_LIST(mb_type, i)) {
925 for (j = 0; j < 2; j++) {
926 s->field_select[i][j] = get_bits1(&s->gb);
927 for (k = 0; k < 2; k++) {
928 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
929 s->last_mv[i][j][k]);
930 s->last_mv[i][j][k] = val;
931 s->mv[i][j][k] = val;
939 s->mv_type = MV_TYPE_FIELD;
940 if (s->picture_structure == PICT_FRAME) {
941 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
942 for (i = 0; i < 2; i++) {
943 if (USES_LIST(mb_type, i)) {
944 for (j = 0; j < 2; j++) {
945 s->field_select[i][j] = get_bits1(&s->gb);
946 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
947 s->last_mv[i][j][0]);
948 s->last_mv[i][j][0] = val;
949 s->mv[i][j][0] = val;
950 av_dlog(s->avctx, "fmx=%d\n", val);
951 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
952 s->last_mv[i][j][1] >> 1);
953 s->last_mv[i][j][1] = val << 1;
954 s->mv[i][j][1] = val;
955 av_dlog(s->avctx, "fmy=%d\n", val);
960 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
961 for (i = 0; i < 2; i++) {
962 if (USES_LIST(mb_type, i)) {
963 s->field_select[i][0] = get_bits1(&s->gb);
964 for (k = 0; k < 2; k++) {
965 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
966 s->last_mv[i][0][k]);
967 s->last_mv[i][0][k] = val;
968 s->last_mv[i][1][k] = val;
969 s->mv[i][0][k] = val;
976 s->mv_type = MV_TYPE_DMV;
977 for (i = 0; i < 2; i++) {
978 if (USES_LIST(mb_type, i)) {
979 int dmx, dmy, mx, my, m;
980 const int my_shift = s->picture_structure == PICT_FRAME;
982 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
983 s->last_mv[i][0][0]);
984 s->last_mv[i][0][0] = mx;
985 s->last_mv[i][1][0] = mx;
987 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
988 s->last_mv[i][0][1] >> my_shift);
992 s->last_mv[i][0][1] = my << my_shift;
993 s->last_mv[i][1][1] = my << my_shift;
997 s->mv[i][1][0] = mx; // not used
998 s->mv[i][1][1] = my; // not used
1000 if (s->picture_structure == PICT_FRAME) {
1001 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
1003 // m = 1 + 2 * s->top_field_first;
1004 m = s->top_field_first ? 1 : 3;
1006 /* top -> top pred */
1007 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1008 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
1010 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
1011 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
1013 mb_type |= MB_TYPE_16x16;
1015 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
1016 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
1017 if (s->picture_structure == PICT_TOP_FIELD)
1026 av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
1032 if (HAS_CBP(mb_type)) {
1033 s->dsp.clear_blocks(s->block[0]);
1035 cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
1036 if (mb_block_count > 6) {
1037 cbp <<= mb_block_count - 6;
1038 cbp |= get_bits(&s->gb, mb_block_count - 6);
1039 s->dsp.clear_blocks(s->block[6]);
1042 av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
1046 //if 1, we memcpy blocks in xvmcvideo
1047 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
1048 ff_xvmc_pack_pblocks(s, cbp);
1054 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
1055 if (s->flags2 & CODEC_FLAG2_FAST) {
1056 for (i = 0; i < 6; i++) {
1058 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
1060 s->block_last_index[i] = -1;
1065 cbp <<= 12-mb_block_count;
1067 for (i = 0; i < mb_block_count; i++) {
1068 if (cbp & (1 << 11)) {
1069 if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
1072 s->block_last_index[i] = -1;
1078 if (s->flags2 & CODEC_FLAG2_FAST) {
1079 for (i = 0; i < 6; i++) {
1081 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
1083 s->block_last_index[i] = -1;
1088 for (i = 0; i < 6; i++) {
1090 if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
1093 s->block_last_index[i] = -1;
1100 for (i = 0; i < 12; i++)
1101 s->block_last_index[i] = -1;
1105 s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
1110 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1112 Mpeg1Context *s = avctx->priv_data;
1113 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1116 /* we need some permutation to store matrices,
1117 * until MPV_common_init() sets the real permutation. */
1118 for (i = 0; i < 64; i++)
1119 s2->dsp.idct_permutation[i]=i;
1121 MPV_decode_defaults(s2);
1123 s->mpeg_enc_ctx.avctx = avctx;
1124 s->mpeg_enc_ctx.flags = avctx->flags;
1125 s->mpeg_enc_ctx.flags2 = avctx->flags2;
1126 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1127 ff_mpeg12_init_vlcs();
1129 s->mpeg_enc_ctx_allocated = 0;
1130 s->mpeg_enc_ctx.picture_number = 0;
1131 s->repeat_field = 0;
1132 s->mpeg_enc_ctx.codec_id = avctx->codec->id;
1133 avctx->color_range = AVCOL_RANGE_MPEG;
1134 if (avctx->codec->id == CODEC_ID_MPEG1VIDEO)
1135 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1137 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1141 static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
1143 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1144 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1147 if (avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized)
1150 err = ff_mpeg_update_thread_context(avctx, avctx_from);
1151 if (err) return err;
1153 if (!ctx->mpeg_enc_ctx_allocated)
1154 memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
1156 if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
1157 s->picture_number++;
1162 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1163 const uint8_t *new_perm)
1165 uint16_t temp_matrix[64];
1168 memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
1170 for (i = 0; i < 64; i++) {
1171 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1175 static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
1176 PIX_FMT_XVMC_MPEG2_IDCT,
1177 PIX_FMT_XVMC_MPEG2_MC,
1180 static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
1182 Mpeg1Context *s1 = avctx->priv_data;
1183 MpegEncContext *s = &s1->mpeg_enc_ctx;
1185 if (avctx->xvmc_acceleration)
1186 return avctx->get_format(avctx, pixfmt_xvmc_mpg2_420);
1187 else if (avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
1188 if (avctx->codec_id == CODEC_ID_MPEG1VIDEO)
1189 return PIX_FMT_VDPAU_MPEG1;
1191 return PIX_FMT_VDPAU_MPEG2;
1193 if (s->chroma_format < 2)
1194 return avctx->get_format(avctx, ff_hwaccel_pixfmt_list_420);
1195 else if (s->chroma_format == 2)
1196 return PIX_FMT_YUV422P;
1198 return PIX_FMT_YUV444P;
1202 /* Call this function when we know all parameters.
1203 * It may be called in different places for MPEG-1 and MPEG-2. */
1204 static int mpeg_decode_postinit(AVCodecContext *avctx)
1206 Mpeg1Context *s1 = avctx->priv_data;
1207 MpegEncContext *s = &s1->mpeg_enc_ctx;
1208 uint8_t old_permutation[64];
1210 if ((s1->mpeg_enc_ctx_allocated == 0) ||
1211 avctx->coded_width != s->width ||
1212 avctx->coded_height != s->height ||
1213 s1->save_width != s->width ||
1214 s1->save_height != s->height ||
1215 s1->save_aspect_info != s->aspect_ratio_info ||
1216 s1->save_progressive_seq != s->progressive_sequence ||
1220 if (s1->mpeg_enc_ctx_allocated) {
1221 ParseContext pc = s->parse_context;
1222 s->parse_context.buffer = 0;
1224 s->parse_context = pc;
1227 if ((s->width == 0) || (s->height == 0))
1230 avcodec_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 assert((avctx->sub_id == 1) == (avctx->codec_id == CODEC_ID_MPEG1VIDEO));
1242 if (avctx->codec_id == CODEC_ID_MPEG1VIDEO) {
1244 avctx->time_base.den = avpriv_frame_rate_tab[s->frame_rate_index].num;
1245 avctx->time_base.num = avpriv_frame_rate_tab[s->frame_rate_index].den;
1247 avctx->sample_aspect_ratio = av_d2q(1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1248 avctx->ticks_per_frame=1;
1251 av_reduce(&s->avctx->time_base.den,
1252 &s->avctx->time_base.num,
1253 avpriv_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
1254 avpriv_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1256 avctx->ticks_per_frame = 2;
1258 if (s->aspect_ratio_info > 1) {
1260 av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1261 (AVRational) {s1->pan_scan.width, s1->pan_scan.height}),
1262 (AVRational) {s->width, s->height});
1264 // we ignore the spec here and guess a bit as reality does not match the spec, see for example
1265 // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
1266 // issue1613, 621, 562
1267 if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
1268 (av_cmp_q(dar, (AVRational) {4, 3}) && av_cmp_q(dar, (AVRational) {16, 9}))) {
1269 s->avctx->sample_aspect_ratio =
1270 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1271 (AVRational) {s->width, s->height});
1273 s->avctx->sample_aspect_ratio =
1274 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1275 (AVRational) {s1->pan_scan.width, s1->pan_scan.height});
1276 //issue1613 4/3 16/9 -> 16/9
1277 //res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1278 //widescreen-issue562.mpg 4/3 16/9 -> 16/9
1279 // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
1280 //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);
1281 //av_log(NULL, AV_LOG_ERROR, "B %d/%d\n", s->avctx->sample_aspect_ratio.num, s->avctx->sample_aspect_ratio.den);
1284 s->avctx->sample_aspect_ratio =
1285 ff_mpeg2_aspect[s->aspect_ratio_info];
1289 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1290 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
1291 // until then pix_fmt may be changed right after codec init
1292 if (avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
1294 s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
1295 if (avctx->idct_algo == FF_IDCT_AUTO)
1296 avctx->idct_algo = FF_IDCT_SIMPLE;
1298 /* Quantization matrices may need reordering
1299 * if DCT permutation is changed. */
1300 memcpy(old_permutation, s->dsp.idct_permutation, 64 * sizeof(uint8_t));
1302 if (MPV_common_init(s) < 0)
1305 quant_matrix_rebuild(s->intra_matrix, old_permutation, s->dsp.idct_permutation);
1306 quant_matrix_rebuild(s->inter_matrix, old_permutation, s->dsp.idct_permutation);
1307 quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->dsp.idct_permutation);
1308 quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->dsp.idct_permutation);
1310 s1->mpeg_enc_ctx_allocated = 1;
1315 static int mpeg1_decode_picture(AVCodecContext *avctx,
1316 const uint8_t *buf, int buf_size)
1318 Mpeg1Context *s1 = avctx->priv_data;
1319 MpegEncContext *s = &s1->mpeg_enc_ctx;
1320 int ref, f_code, vbv_delay;
1322 init_get_bits(&s->gb, buf, buf_size*8);
1324 ref = get_bits(&s->gb, 10); /* temporal ref */
1325 s->pict_type = get_bits(&s->gb, 3);
1326 if (s->pict_type == 0 || s->pict_type > 3)
1329 vbv_delay = get_bits(&s->gb, 16);
1330 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
1331 s1->full_pel[0] = get_bits1(&s->gb);
1332 f_code = get_bits(&s->gb, 3);
1333 if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1335 s->mpeg_f_code[0][0] = f_code;
1336 s->mpeg_f_code[0][1] = f_code;
1338 if (s->pict_type == AV_PICTURE_TYPE_B) {
1339 s1->full_pel[1] = 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[1][0] = f_code;
1344 s->mpeg_f_code[1][1] = f_code;
1346 s->current_picture.f.pict_type = s->pict_type;
1347 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1349 if (avctx->debug & FF_DEBUG_PICT_INFO)
1350 av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1357 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1359 MpegEncContext *s= &s1->mpeg_enc_ctx;
1360 int horiz_size_ext, vert_size_ext;
1363 skip_bits(&s->gb, 1); /* profile and level esc*/
1364 s->avctx->profile = get_bits(&s->gb, 3);
1365 s->avctx->level = get_bits(&s->gb, 4);
1366 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1367 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1368 horiz_size_ext = get_bits(&s->gb, 2);
1369 vert_size_ext = get_bits(&s->gb, 2);
1370 s->width |= (horiz_size_ext << 12);
1371 s->height |= (vert_size_ext << 12);
1372 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1373 s->bit_rate += (bit_rate_ext << 18) * 400;
1374 skip_bits1(&s->gb); /* marker */
1375 s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
1377 s->low_delay = get_bits1(&s->gb);
1378 if (s->flags & CODEC_FLAG_LOW_DELAY)
1381 s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
1382 s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
1384 av_dlog(s->avctx, "sequence extension\n");
1385 s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO;
1386 s->avctx->sub_id = 2; /* indicates MPEG-2 found */
1388 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1389 av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1390 s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
1394 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1396 MpegEncContext *s = &s1->mpeg_enc_ctx;
1397 int color_description, w, h;
1399 skip_bits(&s->gb, 3); /* video format */
1400 color_description = get_bits1(&s->gb);
1401 if (color_description) {
1402 s->avctx->color_primaries = get_bits(&s->gb, 8);
1403 s->avctx->color_trc = get_bits(&s->gb, 8);
1404 s->avctx->colorspace = get_bits(&s->gb, 8);
1406 w = get_bits(&s->gb, 14);
1407 skip_bits(&s->gb, 1); //marker
1408 h = get_bits(&s->gb, 14);
1409 // remaining 3 bits are zero padding
1411 s1->pan_scan.width = 16 * w;
1412 s1->pan_scan.height = 16 * h;
1414 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1415 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1418 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1420 MpegEncContext *s = &s1->mpeg_enc_ctx;
1424 if (s->progressive_sequence) {
1425 if (s->repeat_first_field) {
1427 if (s->top_field_first)
1431 if (s->picture_structure == PICT_FRAME) {
1433 if (s->repeat_first_field)
1437 for (i = 0; i < nofco; i++) {
1438 s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
1439 skip_bits(&s->gb, 1); // marker
1440 s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
1441 skip_bits(&s->gb, 1); // marker
1444 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
1445 av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
1446 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1447 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1448 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
1451 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra)
1455 for (i = 0; i < 64; i++) {
1456 int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
1457 int v = get_bits(&s->gb, 8);
1459 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1462 if (intra && i == 0 && v != 8) {
1463 av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
1464 v = 8; // needed by pink.mpg / issue1046
1473 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1475 av_dlog(s->avctx, "matrix extension\n");
1477 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1478 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1479 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
1480 if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
1483 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1485 MpegEncContext *s = &s1->mpeg_enc_ctx;
1487 s1->full_pel[0] = s1->full_pel[1] = 0;
1488 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1489 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1490 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1491 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1492 if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
1493 av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
1494 if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
1495 if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1496 s->pict_type = AV_PICTURE_TYPE_I;
1498 s->pict_type = AV_PICTURE_TYPE_P;
1500 s->pict_type = AV_PICTURE_TYPE_B;
1501 s->current_picture.f.pict_type = s->pict_type;
1502 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1504 s->intra_dc_precision = get_bits(&s->gb, 2);
1505 s->picture_structure = get_bits(&s->gb, 2);
1506 s->top_field_first = get_bits1(&s->gb);
1507 s->frame_pred_frame_dct = get_bits1(&s->gb);
1508 s->concealment_motion_vectors = get_bits1(&s->gb);
1509 s->q_scale_type = get_bits1(&s->gb);
1510 s->intra_vlc_format = get_bits1(&s->gb);
1511 s->alternate_scan = get_bits1(&s->gb);
1512 s->repeat_first_field = get_bits1(&s->gb);
1513 s->chroma_420_type = get_bits1(&s->gb);
1514 s->progressive_frame = get_bits1(&s->gb);
1516 if (s->progressive_sequence && !s->progressive_frame) {
1517 s->progressive_frame = 1;
1518 av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
1521 if (s->picture_structure == 0 || (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
1522 av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
1523 s->picture_structure = PICT_FRAME;
1526 if (s->progressive_sequence && !s->frame_pred_frame_dct) {
1527 av_log(s->avctx, AV_LOG_ERROR, "invalid frame_pred_frame_dct\n");
1528 s->frame_pred_frame_dct = 1;
1531 if (s->picture_structure == PICT_FRAME) {
1533 s->v_edge_pos = 16 * s->mb_height;
1535 s->first_field ^= 1;
1536 s->v_edge_pos = 8 * s->mb_height;
1537 memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
1540 if (s->alternate_scan) {
1541 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
1542 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
1544 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
1545 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
1548 /* composite display not parsed */
1549 av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1550 av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1551 av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1552 av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1553 av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1554 av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1555 av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1556 av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1557 av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1560 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
1562 AVCodecContext *avctx = s->avctx;
1563 Mpeg1Context *s1 = (Mpeg1Context*)s;
1565 /* start frame decoding */
1566 if (s->first_field || s->picture_structure == PICT_FRAME) {
1567 if (MPV_frame_start(s, avctx) < 0)
1570 ff_er_frame_start(s);
1572 /* first check if we must repeat the frame */
1573 s->current_picture_ptr->f.repeat_pict = 0;
1574 if (s->repeat_first_field) {
1575 if (s->progressive_sequence) {
1576 if (s->top_field_first)
1577 s->current_picture_ptr->f.repeat_pict = 4;
1579 s->current_picture_ptr->f.repeat_pict = 2;
1580 } else if (s->progressive_frame) {
1581 s->current_picture_ptr->f.repeat_pict = 1;
1585 *s->current_picture_ptr->f.pan_scan = s1->pan_scan;
1587 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1588 ff_thread_finish_setup(avctx);
1589 } else { // second field
1592 if (!s->current_picture_ptr) {
1593 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1597 for (i = 0; i < 4; i++) {
1598 s->current_picture.f.data[i] = s->current_picture_ptr->f.data[i];
1599 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1600 s->current_picture.f.data[i] += s->current_picture_ptr->f.linesize[i];
1605 if (avctx->hwaccel) {
1606 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
1610 // MPV_frame_start will call this function too,
1611 // but we need to call it on every field
1612 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1613 if (ff_xvmc_field_start(s, avctx) < 0)
1619 #define DECODE_SLICE_ERROR -1
1620 #define DECODE_SLICE_OK 0
1623 * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
1624 * @return DECODE_SLICE_ERROR if the slice is damaged<br>
1625 * DECODE_SLICE_OK if this slice is ok<br>
1627 static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
1628 const uint8_t **buf, int buf_size)
1630 MpegEncContext *s = &s1->mpeg_enc_ctx;
1631 AVCodecContext *avctx = s->avctx;
1632 const int lowres = s->avctx->lowres;
1633 const int field_pic = s->picture_structure != PICT_FRAME;
1636 s->resync_mb_y = -1;
1638 assert(mb_y < s->mb_height);
1640 init_get_bits(&s->gb, *buf, buf_size * 8);
1642 ff_mpeg1_clean_buffers(s);
1643 s->interlaced_dct = 0;
1645 s->qscale = get_qscale(s);
1647 if (s->qscale == 0) {
1648 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1652 /* extra slice info */
1653 while (get_bits1(&s->gb) != 0) {
1654 skip_bits(&s->gb, 8);
1659 if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
1663 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1665 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1672 /* otherwise, stuffing, nothing to do */
1680 if (s->mb_x >= (unsigned)s->mb_width) {
1681 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1685 if (avctx->hwaccel) {
1686 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1687 int start_code = -1;
1688 buf_end = avpriv_mpv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1689 if (buf_end < *buf + buf_size)
1692 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1693 return DECODE_SLICE_ERROR;
1695 return DECODE_SLICE_OK;
1698 s->resync_mb_x = s->mb_x;
1699 s->resync_mb_y = s->mb_y = mb_y;
1701 ff_init_block_index(s);
1703 if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
1704 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
1705 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",
1706 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],
1707 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")),
1708 s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
1709 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
1710 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
1715 // If 1, we memcpy blocks in xvmcvideo.
1716 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1717 ff_xvmc_init_block(s); // set s->block
1719 if (mpeg_decode_mb(s1, s->block) < 0)
1722 if (s->current_picture.f.motion_val[0] && !s->encoding) { // note motion_val is normally NULL unless we want to extract the MVs
1723 const int wrap = s->b8_stride;
1724 int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
1725 int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
1726 int motion_x, motion_y, dir, i;
1728 for (i = 0; i < 2; i++) {
1729 for (dir = 0; dir < 2; dir++) {
1730 if (s->mb_intra || (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1731 motion_x = motion_y = 0;
1732 } else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)) {
1733 motion_x = s->mv[dir][0][0];
1734 motion_y = s->mv[dir][0][1];
1735 } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
1736 motion_x = s->mv[dir][i][0];
1737 motion_y = s->mv[dir][i][1];
1740 s->current_picture.f.motion_val[dir][xy ][0] = motion_x;
1741 s->current_picture.f.motion_val[dir][xy ][1] = motion_y;
1742 s->current_picture.f.motion_val[dir][xy + 1][0] = motion_x;
1743 s->current_picture.f.motion_val[dir][xy + 1][1] = motion_y;
1744 s->current_picture.f.ref_index [dir][b8_xy ] =
1745 s->current_picture.f.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
1746 assert(s->field_select[dir][i] == 0 || s->field_select[dir][i] == 1);
1753 s->dest[0] += 16 >> lowres;
1754 s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
1755 s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
1757 MPV_decode_mb(s, s->block);
1759 if (++s->mb_x >= s->mb_width) {
1760 const int mb_size = 16 >> s->avctx->lowres;
1762 ff_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
1763 MPV_report_decode_progress(s);
1766 s->mb_y += 1 << field_pic;
1768 if (s->mb_y >= s->mb_height) {
1769 int left = get_bits_left(&s->gb);
1770 int is_d10 = s->chroma_format == 2 && s->pict_type == AV_PICTURE_TYPE_I && avctx->profile == 0 && avctx->level == 5
1771 && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
1772 && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
1774 if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
1775 || ((avctx->err_recognition & AV_EF_BUFFER) && left > 8)) {
1776 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
1782 ff_init_block_index(s);
1785 /* skip mb handling */
1786 if (s->mb_skip_run == -1) {
1787 /* read increment again */
1790 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1792 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1797 s->mb_skip_run += 33;
1798 } else if (code == 35) {
1799 if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
1800 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1803 goto eos; /* end of slice */
1805 /* otherwise, stuffing, nothing to do */
1807 s->mb_skip_run += code;
1811 if (s->mb_skip_run) {
1813 if (s->pict_type == AV_PICTURE_TYPE_I) {
1814 av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1820 for (i = 0; i < 12; i++)
1821 s->block_last_index[i] = -1;
1822 if (s->picture_structure == PICT_FRAME)
1823 s->mv_type = MV_TYPE_16X16;
1825 s->mv_type = MV_TYPE_FIELD;
1826 if (s->pict_type == AV_PICTURE_TYPE_P) {
1827 /* if P type, zero motion vector is implied */
1828 s->mv_dir = MV_DIR_FORWARD;
1829 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1830 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1831 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1832 s->field_select[0][0] = (s->picture_structure - 1) & 1;
1834 /* if B type, reuse previous vectors and directions */
1835 s->mv[0][0][0] = s->last_mv[0][0][0];
1836 s->mv[0][0][1] = s->last_mv[0][0][1];
1837 s->mv[1][0][0] = s->last_mv[1][0][0];
1838 s->mv[1][0][1] = s->last_mv[1][0][1];
1843 eos: // end of slice
1844 *buf += (get_bits_count(&s->gb)-1)/8;
1845 //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1849 static int slice_decode_thread(AVCodecContext *c, void *arg)
1851 MpegEncContext *s = *(void**)arg;
1852 const uint8_t *buf = s->gb.buffer;
1853 int mb_y = s->start_mb_y;
1854 const int field_pic = s->picture_structure != PICT_FRAME;
1856 s->error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
1859 uint32_t start_code;
1862 ret = mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
1864 //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1865 //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);
1867 if (c->err_recognition & AV_EF_EXPLODE)
1869 if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
1870 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);
1872 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);
1875 if (s->mb_y == s->end_mb_y)
1879 buf = avpriv_mpv_find_start_code(buf, s->gb.buffer_end, &start_code);
1880 mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
1881 if (s->picture_structure == PICT_BOTTOM_FIELD)
1883 if (mb_y < 0 || mb_y >= s->end_mb_y)
1889 * Handle slice ends.
1890 * @return 1 if it seems to be the last slice
1892 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
1894 Mpeg1Context *s1 = avctx->priv_data;
1895 MpegEncContext *s = &s1->mpeg_enc_ctx;
1897 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
1900 if (s->avctx->hwaccel) {
1901 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1902 av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
1905 if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1906 ff_xvmc_field_end(s);
1908 /* end of slice reached */
1909 if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field) {
1912 s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_MPEG2;
1918 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
1919 *pict = *(AVFrame*)s->current_picture_ptr;
1920 ff_print_debug_info(s, pict);
1922 if (avctx->active_thread_type & FF_THREAD_FRAME)
1923 s->picture_number++;
1924 /* latency of 1 frame for I- and P-frames */
1925 /* XXX: use another variable than picture_number */
1926 if (s->last_picture_ptr != NULL) {
1927 *pict = *(AVFrame*)s->last_picture_ptr;
1928 ff_print_debug_info(s, pict);
1938 static int mpeg1_decode_sequence(AVCodecContext *avctx,
1939 const uint8_t *buf, int buf_size)
1941 Mpeg1Context *s1 = avctx->priv_data;
1942 MpegEncContext *s = &s1->mpeg_enc_ctx;
1946 init_get_bits(&s->gb, buf, buf_size*8);
1948 width = get_bits(&s->gb, 12);
1949 height = get_bits(&s->gb, 12);
1950 if (width <= 0 || height <= 0)
1952 s->aspect_ratio_info = get_bits(&s->gb, 4);
1953 if (s->aspect_ratio_info == 0) {
1954 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
1955 if (avctx->err_recognition & AV_EF_BITSTREAM)
1958 s->frame_rate_index = get_bits(&s->gb, 4);
1959 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
1961 s->bit_rate = get_bits(&s->gb, 18) * 400;
1962 if (get_bits1(&s->gb) == 0) /* marker */
1967 s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
1968 skip_bits(&s->gb, 1);
1971 if (get_bits1(&s->gb)) {
1972 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1974 for (i = 0; i < 64; i++) {
1975 j = s->dsp.idct_permutation[i];
1976 v = ff_mpeg1_default_intra_matrix[i];
1977 s->intra_matrix[j] = v;
1978 s->chroma_intra_matrix[j] = v;
1981 if (get_bits1(&s->gb)) {
1982 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1984 for (i = 0; i < 64; i++) {
1985 int j = s->dsp.idct_permutation[i];
1986 v = ff_mpeg1_default_non_intra_matrix[i];
1987 s->inter_matrix[j] = v;
1988 s->chroma_inter_matrix[j] = v;
1992 if (show_bits(&s->gb, 23) != 0) {
1993 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
1997 /* we set MPEG-2 parameters so that it emulates MPEG-1 */
1998 s->progressive_sequence = 1;
1999 s->progressive_frame = 1;
2000 s->picture_structure = PICT_FRAME;
2001 s->frame_pred_frame_dct = 1;
2002 s->chroma_format = 1;
2003 s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG1VIDEO;
2004 avctx->sub_id = 1; /* indicates MPEG-1 */
2005 s->out_format = FMT_MPEG1;
2006 s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
2007 if (s->flags & CODEC_FLAG_LOW_DELAY)
2010 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2011 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2012 s->avctx->rc_buffer_size, s->bit_rate);
2017 static int vcr2_init_sequence(AVCodecContext *avctx)
2019 Mpeg1Context *s1 = avctx->priv_data;
2020 MpegEncContext *s = &s1->mpeg_enc_ctx;
2023 /* start new MPEG-1 context decoding */
2024 s->out_format = FMT_MPEG1;
2025 if (s1->mpeg_enc_ctx_allocated) {
2028 s->width = avctx->coded_width;
2029 s->height = avctx->coded_height;
2030 avctx->has_b_frames = 0; // true?
2033 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2034 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
2036 if (avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel ||
2037 s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
2038 if (avctx->idct_algo == FF_IDCT_AUTO)
2039 avctx->idct_algo = FF_IDCT_SIMPLE;
2041 if (MPV_common_init(s) < 0)
2043 exchange_uv(s); // common init reset pblocks, so we swap them here
2044 s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
2045 s1->mpeg_enc_ctx_allocated = 1;
2047 for (i = 0; i < 64; i++) {
2048 int j = s->dsp.idct_permutation[i];
2049 v = ff_mpeg1_default_intra_matrix[i];
2050 s->intra_matrix[j] = v;
2051 s->chroma_intra_matrix[j] = v;
2053 v = ff_mpeg1_default_non_intra_matrix[i];
2054 s->inter_matrix[j] = v;
2055 s->chroma_inter_matrix[j] = v;
2058 s->progressive_sequence = 1;
2059 s->progressive_frame = 1;
2060 s->picture_structure = PICT_FRAME;
2061 s->frame_pred_frame_dct = 1;
2062 s->chroma_format = 1;
2063 s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO;
2064 avctx->sub_id = 2; /* indicates MPEG-2 */
2065 s1->save_width = s->width;
2066 s1->save_height = s->height;
2067 s1->save_progressive_seq = s->progressive_sequence;
2072 static void mpeg_decode_user_data(AVCodecContext *avctx,
2073 const uint8_t *p, int buf_size)
2075 const uint8_t *buf_end = p + buf_size;
2077 /* we parse the DTG active format information */
2078 if (buf_end - p >= 5 &&
2079 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2087 if (buf_end - p < 1)
2089 avctx->dtg_active_format = p[0] & 0x0f;
2094 static void mpeg_decode_gop(AVCodecContext *avctx,
2095 const uint8_t *buf, int buf_size)
2097 Mpeg1Context *s1 = avctx->priv_data;
2098 MpegEncContext *s = &s1->mpeg_enc_ctx;
2100 int time_code_hours, time_code_minutes;
2101 int time_code_seconds, time_code_pictures;
2104 init_get_bits(&s->gb, buf, buf_size*8);
2106 skip_bits1(&s->gb); /* drop_frame_flag */
2108 time_code_hours = get_bits(&s->gb, 5);
2109 time_code_minutes = get_bits(&s->gb, 6);
2110 skip_bits1(&s->gb); // marker bit
2111 time_code_seconds = get_bits(&s->gb, 6);
2112 time_code_pictures = get_bits(&s->gb, 6);
2114 s->closed_gop = get_bits1(&s->gb);
2115 /*broken_link indicate that after editing the
2116 reference frames of the first B-Frames after GOP I-Frame
2117 are missing (open gop)*/
2118 broken_link = get_bits1(&s->gb);
2120 if (s->avctx->debug & FF_DEBUG_PICT_INFO)
2121 av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
2122 time_code_hours, time_code_minutes, time_code_seconds,
2123 time_code_pictures, s->closed_gop, broken_link);
2126 * Find the end of the current frame in the bitstream.
2127 * @return the position of the first byte of the next frame, or -1
2129 int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
2132 uint32_t state = pc->state;
2134 /* EOF considered as end of frame */
2139 0 frame start -> 1/4
2140 1 first_SEQEXT -> 0/2
2141 2 first field start -> 3/0
2142 3 second_SEQEXT -> 2/0
2146 for (i = 0; i < buf_size; i++) {
2147 assert(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
2148 if (pc->frame_start_found & 1) {
2149 if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
2150 pc->frame_start_found--;
2151 else if (state == EXT_START_CODE + 2) {
2152 if ((buf[i] & 3) == 3)
2153 pc->frame_start_found = 0;
2155 pc->frame_start_found = (pc->frame_start_found + 1) & 3;
2159 i = avpriv_mpv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
2160 if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
2162 pc->frame_start_found = 4;
2164 if (state == SEQ_END_CODE) {
2168 if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
2169 pc->frame_start_found = 0;
2170 if (pc->frame_start_found < 4 && state == EXT_START_CODE)
2171 pc->frame_start_found++;
2172 if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
2173 if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
2174 pc->frame_start_found = 0;
2179 if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
2180 ff_fetch_timestamp(s, i - 3, 1);
2185 return END_NOT_FOUND;
2188 static int decode_chunks(AVCodecContext *avctx,
2189 AVFrame *picture, int *data_size,
2190 const uint8_t *buf, int buf_size);
2192 /* handle buffering and image synchronisation */
2193 static int mpeg_decode_frame(AVCodecContext *avctx,
2194 void *data, int *data_size,
2197 const uint8_t *buf = avpkt->data;
2198 int buf_size = avpkt->size;
2199 Mpeg1Context *s = avctx->priv_data;
2200 AVFrame *picture = data;
2201 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2202 av_dlog(avctx, "fill_buffer\n");
2204 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2205 /* special case for last picture */
2206 if (s2->low_delay == 0 && s2->next_picture_ptr) {
2207 *picture = *(AVFrame*)s2->next_picture_ptr;
2208 s2->next_picture_ptr = NULL;
2210 *data_size = sizeof(AVFrame);
2215 if (s2->flags & CODEC_FLAG_TRUNCATED) {
2216 int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
2218 if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
2222 if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
2223 vcr2_init_sequence(avctx);
2227 if (avctx->extradata && !avctx->frame_number) {
2228 int ret = decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
2229 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
2233 return decode_chunks(avctx, picture, data_size, buf, buf_size);
2236 static int decode_chunks(AVCodecContext *avctx,
2237 AVFrame *picture, int *data_size,
2238 const uint8_t *buf, int buf_size)
2240 Mpeg1Context *s = avctx->priv_data;
2241 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2242 const uint8_t *buf_ptr = buf;
2243 const uint8_t *buf_end = buf + buf_size;
2244 int ret, input_size;
2248 /* find next start code */
2249 uint32_t start_code = -1;
2250 buf_ptr = avpriv_mpv_find_start_code(buf_ptr, buf_end, &start_code);
2251 if (start_code > 0x1ff) {
2252 if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {
2253 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
2256 avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
2257 for (i = 0; i < s->slice_count; i++)
2258 s2->error_count += s2->thread_context[i]->error_count;
2261 if (CONFIG_MPEG_VDPAU_DECODER && avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
2262 ff_vdpau_mpeg_picture_complete(s, buf, buf_size, s->slice_count);
2264 if (slice_end(avctx, picture)) {
2265 if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
2266 *data_size = sizeof(AVPicture);
2270 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2273 input_size = buf_end - buf_ptr;
2275 if (avctx->debug & FF_DEBUG_STARTCODE) {
2276 av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
2279 /* prepare data for next start code */
2280 switch (start_code) {
2281 case SEQ_START_CODE:
2282 if (last_code == 0) {
2283 mpeg1_decode_sequence(avctx, buf_ptr, input_size);
2286 av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
2287 if (avctx->err_recognition & AV_EF_EXPLODE)
2288 return AVERROR_INVALIDDATA;
2292 case PICTURE_START_CODE:
2293 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {
2296 avctx->execute(avctx, slice_decode_thread,
2297 s2->thread_context, NULL,
2298 s->slice_count, sizeof(void*));
2299 for (i = 0; i < s->slice_count; i++)
2300 s2->error_count += s2->thread_context[i]->error_count;
2303 if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
2304 ret = mpeg_decode_postinit(avctx);
2306 av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
2310 /* we have a complete image: we try to decompress it */
2311 if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
2313 s2->first_slice = 1;
2314 last_code = PICTURE_START_CODE;
2316 av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
2317 if (avctx->err_recognition & AV_EF_EXPLODE)
2318 return AVERROR_INVALIDDATA;
2321 case EXT_START_CODE:
2322 init_get_bits(&s2->gb, buf_ptr, input_size*8);
2324 switch (get_bits(&s2->gb, 4)) {
2326 if (last_code == 0) {
2327 mpeg_decode_sequence_extension(s);
2329 av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
2330 if (avctx->err_recognition & AV_EF_EXPLODE)
2331 return AVERROR_INVALIDDATA;
2335 mpeg_decode_sequence_display_extension(s);
2338 mpeg_decode_quant_matrix_extension(s2);
2341 mpeg_decode_picture_display_extension(s);
2344 if (last_code == PICTURE_START_CODE) {
2345 mpeg_decode_picture_coding_extension(s);
2347 av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
2348 if (avctx->err_recognition & AV_EF_EXPLODE)
2349 return AVERROR_INVALIDDATA;
2354 case USER_START_CODE:
2355 mpeg_decode_user_data(avctx, buf_ptr, input_size);
2357 case GOP_START_CODE:
2358 if (last_code == 0) {
2360 mpeg_decode_gop(avctx, buf_ptr, input_size);
2363 av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
2364 if (avctx->err_recognition & AV_EF_EXPLODE)
2365 return AVERROR_INVALIDDATA;
2369 if (start_code >= SLICE_MIN_START_CODE &&
2370 start_code <= SLICE_MAX_START_CODE && last_code != 0) {
2371 const int field_pic = s2->picture_structure != PICT_FRAME;
2372 int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
2373 last_code = SLICE_MIN_START_CODE;
2375 if (s2->picture_structure == PICT_BOTTOM_FIELD)
2378 if (mb_y >= s2->mb_height) {
2379 av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2383 if (s2->last_picture_ptr == NULL) {
2384 /* Skip B-frames if we do not have reference frames and gop is not closed */
2385 if (s2->pict_type == AV_PICTURE_TYPE_B) {
2386 if (!s2->closed_gop)
2390 if (s2->pict_type == AV_PICTURE_TYPE_I)
2392 if (s2->next_picture_ptr == NULL) {
2393 /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
2394 if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) break;
2396 if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||
2397 (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||
2398 avctx->skip_frame >= AVDISCARD_ALL)
2401 if (!s->mpeg_enc_ctx_allocated)
2404 if (s2->codec_id == CODEC_ID_MPEG2VIDEO) {
2405 if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
2409 if (!s2->pict_type) {
2410 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2411 if (avctx->err_recognition & AV_EF_EXPLODE)
2412 return AVERROR_INVALIDDATA;
2416 if (s2->first_slice) {
2417 s2->first_slice = 0;
2418 if (mpeg_field_start(s2, buf, buf_size) < 0)
2421 if (!s2->current_picture_ptr) {
2422 av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
2423 return AVERROR_INVALIDDATA;
2426 if (avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU) {
2431 if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
2432 int threshold= (s2->mb_height * s->slice_count + avctx->thread_count / 2) / avctx->thread_count;
2433 if (threshold <= mb_y) {
2434 MpegEncContext *thread_context = s2->thread_context[s->slice_count];
2436 thread_context->start_mb_y = mb_y;
2437 thread_context->end_mb_y = s2->mb_height;
2438 if (s->slice_count) {
2439 s2->thread_context[s->slice_count-1]->end_mb_y = mb_y;
2440 ff_update_duplicate_context(thread_context, s2);
2442 init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
2445 buf_ptr += 2; // FIXME add minimum number of bytes per slice
2447 ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
2451 if (avctx->err_recognition & AV_EF_EXPLODE)
2453 if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
2454 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);
2456 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);
2465 static void flush(AVCodecContext *avctx)
2467 Mpeg1Context *s = avctx->priv_data;
2471 ff_mpeg_flush(avctx);
2474 static int mpeg_decode_end(AVCodecContext *avctx)
2476 Mpeg1Context *s = avctx->priv_data;
2478 if (s->mpeg_enc_ctx_allocated)
2479 MPV_common_end(&s->mpeg_enc_ctx);
2483 static const AVProfile mpeg2_video_profiles[] = {
2484 { FF_PROFILE_MPEG2_422, "4:2:2" },
2485 { FF_PROFILE_MPEG2_HIGH, "High" },
2486 { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2487 { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2488 { FF_PROFILE_MPEG2_MAIN, "Main" },
2489 { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2490 { FF_PROFILE_RESERVED, "Reserved" },
2491 { FF_PROFILE_RESERVED, "Reserved" },
2492 { FF_PROFILE_UNKNOWN },
2496 AVCodec ff_mpeg1video_decoder = {
2497 .name = "mpeg1video",
2498 .type = AVMEDIA_TYPE_VIDEO,
2499 .id = CODEC_ID_MPEG1VIDEO,
2500 .priv_data_size = sizeof(Mpeg1Context),
2501 .init = mpeg_decode_init,
2502 .close = mpeg_decode_end,
2503 .decode = mpeg_decode_frame,
2504 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
2507 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2508 .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2511 AVCodec ff_mpeg2video_decoder = {
2512 .name = "mpeg2video",
2513 .type = AVMEDIA_TYPE_VIDEO,
2514 .id = CODEC_ID_MPEG2VIDEO,
2515 .priv_data_size = sizeof(Mpeg1Context),
2516 .init = mpeg_decode_init,
2517 .close = mpeg_decode_end,
2518 .decode = mpeg_decode_frame,
2519 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
2522 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2523 .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2527 AVCodec ff_mpegvideo_decoder = {
2528 .name = "mpegvideo",
2529 .type = AVMEDIA_TYPE_VIDEO,
2530 .id = CODEC_ID_MPEG2VIDEO,
2531 .priv_data_size = sizeof(Mpeg1Context),
2532 .init = mpeg_decode_init,
2533 .close = mpeg_decode_end,
2534 .decode = mpeg_decode_frame,
2535 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
2538 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2541 #if CONFIG_MPEG_XVMC_DECODER
2542 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
2544 if (avctx->active_thread_type & FF_THREAD_SLICE)
2546 if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
2548 if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
2549 av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2551 mpeg_decode_init(avctx);
2553 avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
2554 avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
2559 AVCodec ff_mpeg_xvmc_decoder = {
2560 .name = "mpegvideo_xvmc",
2561 .type = AVMEDIA_TYPE_VIDEO,
2562 .id = CODEC_ID_MPEG2VIDEO_XVMC,
2563 .priv_data_size = sizeof(Mpeg1Context),
2564 .init = mpeg_mc_decode_init,
2565 .close = mpeg_decode_end,
2566 .decode = mpeg_decode_frame,
2567 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2569 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2574 #if CONFIG_MPEG_VDPAU_DECODER
2575 AVCodec ff_mpeg_vdpau_decoder = {
2576 .name = "mpegvideo_vdpau",
2577 .type = AVMEDIA_TYPE_VIDEO,
2578 .id = CODEC_ID_MPEG2VIDEO,
2579 .priv_data_size = sizeof(Mpeg1Context),
2580 .init = mpeg_decode_init,
2581 .close = mpeg_decode_end,
2582 .decode = mpeg_decode_frame,
2583 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2585 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
2589 #if CONFIG_MPEG1_VDPAU_DECODER
2590 AVCodec ff_mpeg1_vdpau_decoder = {
2591 .name = "mpeg1video_vdpau",
2592 .type = AVMEDIA_TYPE_VIDEO,
2593 .id = CODEC_ID_MPEG1VIDEO,
2594 .priv_data_size = sizeof(Mpeg1Context),
2595 .init = mpeg_decode_init,
2596 .close = mpeg_decode_end,
2597 .decode = mpeg_decode_frame,
2598 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2600 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),