3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "mpegvideo.h"
33 #include "libavutil/avassert.h"
36 #include "mpeg12data.h"
37 #include "mpeg12decdata.h"
38 #include "bytestream.h"
39 #include "vdpau_internal.h"
40 #include "xvmc_internal.h"
48 #define MBINCR_VLC_BITS 9
49 #define MB_PAT_VLC_BITS 9
50 #define MB_PTYPE_VLC_BITS 6
51 #define MB_BTYPE_VLC_BITS 6
53 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
56 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
59 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
60 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
63 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
66 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
67 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
68 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
69 static void exchange_uv(MpegEncContext *s);
71 uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
74 #define INIT_2D_VLC_RL(rl, static_size)\
76 static RL_VLC_ELEM rl_vlc_table[static_size];\
77 INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
78 &rl.table_vlc[0][1], 4, 2,\
79 &rl.table_vlc[0][0], 4, 2, static_size);\
81 rl.rl_vlc[0]= rl_vlc_table;\
85 static void init_2d_vlc_rl(RLTable *rl)
89 for(i=0; i<rl->vlc.table_size; i++){
90 int code= rl->vlc.table[i][0];
91 int len = rl->vlc.table[i][1];
94 if(len==0){ // illegal code
97 }else if(len<0){ //more bits needed
101 if(code==rl->n){ //esc
104 }else if(code==rl->n+1){ //eob
108 run= rl->table_run [code] + 1;
109 level= rl->table_level[code];
112 rl->rl_vlc[0][i].len= len;
113 rl->rl_vlc[0][i].level= level;
114 rl->rl_vlc[0][i].run= run;
118 void ff_mpeg12_common_init(MpegEncContext *s)
122 s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
126 void ff_mpeg1_clean_buffers(MpegEncContext *s){
127 s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
128 s->last_dc[1] = s->last_dc[0];
129 s->last_dc[2] = s->last_dc[0];
130 memset(s->last_mv, 0, sizeof(s->last_mv));
134 /******************************************/
138 VLC ff_dc_chroma_vlc;
141 static VLC mbincr_vlc;
142 static VLC mb_ptype_vlc;
143 static VLC mb_btype_vlc;
144 static VLC mb_pat_vlc;
146 av_cold void ff_mpeg12_init_vlcs(void)
153 INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
154 ff_mpeg12_vlc_dc_lum_bits, 1, 1,
155 ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
156 INIT_VLC_STATIC(&ff_dc_chroma_vlc, DC_VLC_BITS, 12,
157 ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
158 ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
159 INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
160 &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
161 &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
162 INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
163 &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
164 &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
165 INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
166 &ff_mpeg12_mbPatTable[0][1], 2, 1,
167 &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
169 INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
170 &table_mb_ptype[0][1], 2, 1,
171 &table_mb_ptype[0][0], 2, 1, 64);
172 INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
173 &table_mb_btype[0][1], 2, 1,
174 &table_mb_btype[0][0], 2, 1, 64);
175 init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
176 init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
178 INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
179 INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
183 static inline int get_dmv(MpegEncContext *s)
185 if(get_bits1(&s->gb))
186 return 1 - (get_bits1(&s->gb) << 1);
191 static inline int get_qscale(MpegEncContext *s)
193 int qscale = get_bits(&s->gb, 5);
194 if (s->q_scale_type) {
195 return non_linear_qscale[qscale];
201 /* motion type (for MPEG-2) */
207 static int mpeg_decode_mb(MpegEncContext *s,
208 DCTELEM block[12][64])
210 int i, j, k, cbp, val, mb_type, motion_type;
211 const int mb_block_count = 4 + (1<< s->chroma_format);
213 av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
215 assert(s->mb_skipped==0);
217 if (s->mb_skip_run-- != 0) {
218 if (s->pict_type == AV_PICTURE_TYPE_P) {
220 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
225 mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
227 mb_type= s->current_picture.mb_type[ s->mb_width + (s->mb_y-1)*s->mb_stride - 1]; // FIXME not sure if this is allowed in MPEG at all
228 if(IS_INTRA(mb_type))
231 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
232 mb_type | MB_TYPE_SKIP;
233 // assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
235 if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
242 switch(s->pict_type) {
244 case AV_PICTURE_TYPE_I:
245 if (get_bits1(&s->gb) == 0) {
246 if (get_bits1(&s->gb) == 0){
247 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
250 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
252 mb_type = MB_TYPE_INTRA;
255 case AV_PICTURE_TYPE_P:
256 mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
258 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
261 mb_type = ptype2mb_type[ mb_type ];
263 case AV_PICTURE_TYPE_B:
264 mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
266 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
269 mb_type = btype2mb_type[ mb_type ];
272 av_dlog(s->avctx, "mb_type=%x\n", mb_type);
273 // motion_type = 0; /* avoid warning */
274 if (IS_INTRA(mb_type)) {
275 s->dsp.clear_blocks(s->block[0]);
277 if(!s->chroma_y_shift){
278 s->dsp.clear_blocks(s->block[6]);
281 /* compute DCT type */
282 if (s->picture_structure == PICT_FRAME && //FIXME add an interlaced_dct coded var?
283 !s->frame_pred_frame_dct) {
284 s->interlaced_dct = get_bits1(&s->gb);
287 if (IS_QUANT(mb_type))
288 s->qscale = get_qscale(s);
290 if (s->concealment_motion_vectors) {
291 /* just parse them */
292 if (s->picture_structure != PICT_FRAME)
293 skip_bits1(&s->gb); /* field select */
295 s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
296 mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
297 s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
298 mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
300 skip_bits1(&s->gb); /* marker */
302 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
304 //if 1, we memcpy blocks in xvmcvideo
305 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1){
306 ff_xvmc_pack_pblocks(s,-1);//inter are always full blocks
312 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
313 if(s->flags2 & CODEC_FLAG2_FAST){
315 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
318 for(i=0;i<mb_block_count;i++) {
319 if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
325 if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
330 if (mb_type & MB_TYPE_ZERO_MV){
331 assert(mb_type & MB_TYPE_CBP);
333 s->mv_dir = MV_DIR_FORWARD;
334 if(s->picture_structure == PICT_FRAME){
335 if(!s->frame_pred_frame_dct)
336 s->interlaced_dct = get_bits1(&s->gb);
337 s->mv_type = MV_TYPE_16X16;
339 s->mv_type = MV_TYPE_FIELD;
340 mb_type |= MB_TYPE_INTERLACED;
341 s->field_select[0][0]= s->picture_structure - 1;
344 if (IS_QUANT(mb_type))
345 s->qscale = get_qscale(s);
347 s->last_mv[0][0][0] = 0;
348 s->last_mv[0][0][1] = 0;
349 s->last_mv[0][1][0] = 0;
350 s->last_mv[0][1][1] = 0;
354 assert(mb_type & MB_TYPE_L0L1);
355 //FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
356 /* get additional motion vector type */
357 if (s->frame_pred_frame_dct)
358 motion_type = MT_FRAME;
360 motion_type = get_bits(&s->gb, 2);
361 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
362 s->interlaced_dct = get_bits1(&s->gb);
365 if (IS_QUANT(mb_type))
366 s->qscale = get_qscale(s);
369 s->mv_dir= (mb_type>>13)&3;
370 av_dlog(s->avctx, "motion_type=%d\n", motion_type);
371 switch(motion_type) {
372 case MT_FRAME: /* or MT_16X8 */
373 if (s->picture_structure == PICT_FRAME) {
374 mb_type |= MB_TYPE_16x16;
375 s->mv_type = MV_TYPE_16X16;
377 if (USES_LIST(mb_type, i)) {
379 s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
380 mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
381 s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
382 mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
383 /* full_pel: only for MPEG-1 */
385 s->mv[i][0][0] <<= 1;
386 s->mv[i][0][1] <<= 1;
391 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
392 s->mv_type = MV_TYPE_16X8;
394 if (USES_LIST(mb_type, i)) {
397 s->field_select[i][j] = get_bits1(&s->gb);
399 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
400 s->last_mv[i][j][k]);
401 s->last_mv[i][j][k] = val;
402 s->mv[i][j][k] = val;
410 if(s->progressive_sequence){
411 av_log(s->avctx, AV_LOG_ERROR, "MT_FIELD in progressive_sequence\n");
414 s->mv_type = MV_TYPE_FIELD;
415 if (s->picture_structure == PICT_FRAME) {
416 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
418 if (USES_LIST(mb_type, i)) {
420 s->field_select[i][j] = get_bits1(&s->gb);
421 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
422 s->last_mv[i][j][0]);
423 s->last_mv[i][j][0] = val;
424 s->mv[i][j][0] = val;
425 av_dlog(s->avctx, "fmx=%d\n", val);
426 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
427 s->last_mv[i][j][1] >> 1);
428 s->last_mv[i][j][1] = val << 1;
429 s->mv[i][j][1] = val;
430 av_dlog(s->avctx, "fmy=%d\n", val);
435 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
437 if (USES_LIST(mb_type, i)) {
438 s->field_select[i][0] = get_bits1(&s->gb);
440 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
441 s->last_mv[i][0][k]);
442 s->last_mv[i][0][k] = val;
443 s->last_mv[i][1][k] = val;
444 s->mv[i][0][k] = val;
451 if(s->progressive_sequence){
452 av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
455 s->mv_type = MV_TYPE_DMV;
457 if (USES_LIST(mb_type, i)) {
458 int dmx, dmy, mx, my, m;
459 const int my_shift= s->picture_structure == PICT_FRAME;
461 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
462 s->last_mv[i][0][0]);
463 s->last_mv[i][0][0] = mx;
464 s->last_mv[i][1][0] = mx;
466 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
467 s->last_mv[i][0][1] >> my_shift);
471 s->last_mv[i][0][1] = my<<my_shift;
472 s->last_mv[i][1][1] = my<<my_shift;
476 s->mv[i][1][0] = mx;//not used
477 s->mv[i][1][1] = my;//not used
479 if (s->picture_structure == PICT_FRAME) {
480 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
482 //m = 1 + 2 * s->top_field_first;
483 m = s->top_field_first ? 1 : 3;
485 /* top -> top pred */
486 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
487 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
489 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
490 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
492 mb_type |= MB_TYPE_16x16;
494 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
495 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
496 if(s->picture_structure == PICT_TOP_FIELD)
505 av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
511 if (HAS_CBP(mb_type)) {
512 s->dsp.clear_blocks(s->block[0]);
514 cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
515 if(mb_block_count > 6){
516 cbp<<= mb_block_count-6;
517 cbp |= get_bits(&s->gb, mb_block_count-6);
518 s->dsp.clear_blocks(s->block[6]);
521 av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
525 //if 1, we memcpy blocks in xvmcvideo
526 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1){
527 ff_xvmc_pack_pblocks(s,cbp);
533 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
534 if(s->flags2 & CODEC_FLAG2_FAST){
537 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
539 s->block_last_index[i] = -1;
544 cbp<<= 12-mb_block_count;
546 for(i=0;i<mb_block_count;i++) {
547 if ( cbp & (1<<11) ) {
548 if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
551 s->block_last_index[i] = -1;
557 if(s->flags2 & CODEC_FLAG2_FAST){
560 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
562 s->block_last_index[i] = -1;
569 if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
572 s->block_last_index[i] = -1;
580 s->block_last_index[i] = -1;
584 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
589 /* as H.263, but only 17 codes */
590 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
592 int code, sign, val, l, shift;
594 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
602 sign = get_bits1(&s->gb);
606 val = (val - 1) << shift;
607 val |= get_bits(&s->gb, shift);
614 /* modulo decoding */
615 l= INT_BIT - 5 - shift;
620 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
624 int level, dc, diff, i, j, run;
626 RLTable *rl = &ff_rl_mpeg1;
627 uint8_t * const scantable= s->intra_scantable.permutated;
628 const uint16_t *quant_matrix= s->intra_matrix;
629 const int qscale= s->qscale;
632 component = (n <= 3 ? 0 : n - 4 + 1);
633 diff = decode_dc(&s->gb, component);
636 dc = s->last_dc[component];
638 s->last_dc[component] = dc;
639 block[0] = dc*quant_matrix[0];
640 av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
643 OPEN_READER(re, &s->gb);
644 /* now quantify & encode AC coefficients */
646 UPDATE_CACHE(re, &s->gb);
647 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
651 } else if(level != 0) {
654 level= (level*qscale*quant_matrix[j])>>4;
656 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
657 LAST_SKIP_BITS(re, &s->gb, 1);
660 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
661 UPDATE_CACHE(re, &s->gb);
662 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
664 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
665 } else if (level == 0) {
666 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
672 level= (level*qscale*quant_matrix[j])>>4;
676 level= (level*qscale*quant_matrix[j])>>4;
681 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
687 CLOSE_READER(re, &s->gb);
689 s->block_last_index[n] = i;
693 int ff_mpeg1_decode_block_intra(MpegEncContext *s,
697 return mpeg1_decode_block_intra(s, block, n);
700 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
704 int level, i, j, run;
705 RLTable *rl = &ff_rl_mpeg1;
706 uint8_t * const scantable= s->intra_scantable.permutated;
707 const uint16_t *quant_matrix= s->inter_matrix;
708 const int qscale= s->qscale;
711 OPEN_READER(re, &s->gb);
713 // special case for first coefficient, no need to add second VLC table
714 UPDATE_CACHE(re, &s->gb);
715 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
716 level= (3*qscale*quant_matrix[0])>>5;
718 if(GET_CACHE(re, &s->gb)&0x40000000)
722 SKIP_BITS(re, &s->gb, 2);
723 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
726 /* now quantify & encode AC coefficients */
728 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
733 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
735 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
736 SKIP_BITS(re, &s->gb, 1);
739 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
740 UPDATE_CACHE(re, &s->gb);
741 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
743 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
744 } else if (level == 0) {
745 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
751 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
755 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
760 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
765 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
767 UPDATE_CACHE(re, &s->gb);
770 LAST_SKIP_BITS(re, &s->gb, 2);
771 CLOSE_READER(re, &s->gb);
773 s->block_last_index[n] = i;
777 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
779 int level, i, j, run;
780 RLTable *rl = &ff_rl_mpeg1;
781 uint8_t * const scantable= s->intra_scantable.permutated;
782 const int qscale= s->qscale;
785 OPEN_READER(re, &s->gb);
787 // special case for first coefficient, no need to add second VLC table
788 UPDATE_CACHE(re, &s->gb);
789 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
790 level= (3*qscale)>>1;
792 if(GET_CACHE(re, &s->gb)&0x40000000)
796 SKIP_BITS(re, &s->gb, 2);
797 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
801 /* now quantify & encode AC coefficients */
803 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
808 level= ((level*2+1)*qscale)>>1;
810 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
811 SKIP_BITS(re, &s->gb, 1);
814 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
815 UPDATE_CACHE(re, &s->gb);
816 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
818 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
819 } else if (level == 0) {
820 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
826 level= ((level*2+1)*qscale)>>1;
830 level= ((level*2+1)*qscale)>>1;
836 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
838 UPDATE_CACHE(re, &s->gb);
841 LAST_SKIP_BITS(re, &s->gb, 2);
842 CLOSE_READER(re, &s->gb);
844 s->block_last_index[n] = i;
849 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
853 int level, i, j, run;
854 RLTable *rl = &ff_rl_mpeg1;
855 uint8_t * const scantable= s->intra_scantable.permutated;
856 const uint16_t *quant_matrix;
857 const int qscale= s->qscale;
863 OPEN_READER(re, &s->gb);
866 quant_matrix = s->inter_matrix;
868 quant_matrix = s->chroma_inter_matrix;
870 // special case for first coefficient, no need to add second VLC table
871 UPDATE_CACHE(re, &s->gb);
872 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
873 level= (3*qscale*quant_matrix[0])>>5;
874 if(GET_CACHE(re, &s->gb)&0x40000000)
879 SKIP_BITS(re, &s->gb, 2);
880 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
884 /* now quantify & encode AC coefficients */
886 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
891 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
892 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
893 SKIP_BITS(re, &s->gb, 1);
896 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
897 UPDATE_CACHE(re, &s->gb);
898 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
903 level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
906 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
910 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
916 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
918 UPDATE_CACHE(re, &s->gb);
921 LAST_SKIP_BITS(re, &s->gb, 2);
922 CLOSE_READER(re, &s->gb);
924 block[63] ^= (mismatch & 1);
926 s->block_last_index[n] = i;
930 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
934 int level, i, j, run;
935 RLTable *rl = &ff_rl_mpeg1;
936 uint8_t * const scantable= s->intra_scantable.permutated;
937 const int qscale= s->qscale;
938 OPEN_READER(re, &s->gb);
941 // special case for first coefficient, no need to add second VLC table
942 UPDATE_CACHE(re, &s->gb);
943 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
944 level= (3*qscale)>>1;
945 if(GET_CACHE(re, &s->gb)&0x40000000)
949 SKIP_BITS(re, &s->gb, 2);
950 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
954 /* now quantify & encode AC coefficients */
956 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
961 level= ((level*2+1)*qscale)>>1;
962 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
963 SKIP_BITS(re, &s->gb, 1);
966 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
967 UPDATE_CACHE(re, &s->gb);
968 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
973 level= ((-level*2+1)*qscale)>>1;
976 level= ((level*2+1)*qscale)>>1;
981 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
983 UPDATE_CACHE(re, &s->gb);
986 LAST_SKIP_BITS(re, &s->gb, 2);
987 CLOSE_READER(re, &s->gb);
988 s->block_last_index[n] = i;
993 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
997 int level, dc, diff, i, j, run;
1000 uint8_t * const scantable= s->intra_scantable.permutated;
1001 const uint16_t *quant_matrix;
1002 const int qscale= s->qscale;
1005 /* DC coefficient */
1007 quant_matrix = s->intra_matrix;
1010 quant_matrix = s->chroma_intra_matrix;
1011 component = (n&1) + 1;
1013 diff = decode_dc(&s->gb, component);
1016 dc = s->last_dc[component];
1018 s->last_dc[component] = dc;
1019 block[0] = dc << (3 - s->intra_dc_precision);
1020 av_dlog(s->avctx, "dc=%d\n", block[0]);
1021 mismatch = block[0] ^ 1;
1023 if (s->intra_vlc_format)
1029 OPEN_READER(re, &s->gb);
1030 /* now quantify & encode AC coefficients */
1032 UPDATE_CACHE(re, &s->gb);
1033 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1037 } else if(level != 0) {
1040 level= (level*qscale*quant_matrix[j])>>4;
1041 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1042 LAST_SKIP_BITS(re, &s->gb, 1);
1045 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1046 UPDATE_CACHE(re, &s->gb);
1047 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1051 level= (-level*qscale*quant_matrix[j])>>4;
1054 level= (level*qscale*quant_matrix[j])>>4;
1058 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1065 CLOSE_READER(re, &s->gb);
1067 block[63]^= mismatch&1;
1069 s->block_last_index[n] = i;
1073 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
1077 int level, dc, diff, j, run;
1080 uint8_t * scantable= s->intra_scantable.permutated;
1081 const uint16_t *quant_matrix;
1082 const int qscale= s->qscale;
1084 /* DC coefficient */
1086 quant_matrix = s->intra_matrix;
1089 quant_matrix = s->chroma_intra_matrix;
1090 component = (n&1) + 1;
1092 diff = decode_dc(&s->gb, component);
1095 dc = s->last_dc[component];
1097 s->last_dc[component] = dc;
1098 block[0] = dc << (3 - s->intra_dc_precision);
1099 if (s->intra_vlc_format)
1105 OPEN_READER(re, &s->gb);
1106 /* now quantify & encode AC coefficients */
1108 UPDATE_CACHE(re, &s->gb);
1109 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1113 } else if(level != 0) {
1116 level= (level*qscale*quant_matrix[j])>>4;
1117 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1118 LAST_SKIP_BITS(re, &s->gb, 1);
1121 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1122 UPDATE_CACHE(re, &s->gb);
1123 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1127 level= (-level*qscale*quant_matrix[j])>>4;
1130 level= (level*qscale*quant_matrix[j])>>4;
1136 CLOSE_READER(re, &s->gb);
1139 s->block_last_index[n] = scantable - s->intra_scantable.permutated;
1143 typedef struct Mpeg1Context {
1144 MpegEncContext mpeg_enc_ctx;
1145 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1146 int repeat_field; /* true if we must repeat the field */
1147 AVPanScan pan_scan; /**< some temporary storage for the panscan */
1149 int swap_uv;//indicate VCR2
1150 int save_aspect_info;
1151 int save_width, save_height, save_progressive_seq;
1152 AVRational frame_rate_ext; ///< MPEG-2 specific framerate modificator
1153 int sync; ///< Did we reach a sync point like a GOP/SEQ/KEYFrame?
1156 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1158 Mpeg1Context *s = avctx->priv_data;
1159 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1162 /* we need some permutation to store matrices,
1163 * until MPV_common_init() sets the real permutation. */
1165 s2->dsp.idct_permutation[i]=i;
1167 MPV_decode_defaults(s2);
1169 s->mpeg_enc_ctx.avctx= avctx;
1170 s->mpeg_enc_ctx.flags= avctx->flags;
1171 s->mpeg_enc_ctx.flags2= avctx->flags2;
1172 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1173 ff_mpeg12_init_vlcs();
1175 s->mpeg_enc_ctx_allocated = 0;
1176 s->mpeg_enc_ctx.picture_number = 0;
1177 s->repeat_field = 0;
1178 s->mpeg_enc_ctx.codec_id= avctx->codec->id;
1179 avctx->color_range= AVCOL_RANGE_MPEG;
1180 if (avctx->codec->id == CODEC_ID_MPEG1VIDEO)
1181 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1183 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1187 static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
1189 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
1190 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
1193 if(avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized)
1196 err = ff_mpeg_update_thread_context(avctx, avctx_from);
1199 if(!ctx->mpeg_enc_ctx_allocated)
1200 memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
1202 if(!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
1203 s->picture_number++;
1208 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1209 const uint8_t *new_perm){
1210 uint16_t temp_matrix[64];
1213 memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
1216 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1220 static const enum PixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
1221 #if CONFIG_MPEG_XVMC_DECODER
1222 PIX_FMT_XVMC_MPEG2_IDCT,
1223 PIX_FMT_XVMC_MPEG2_MC,
1225 #if CONFIG_MPEG1_VDPAU_HWACCEL
1226 PIX_FMT_VDPAU_MPEG1,
1232 static const enum PixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
1233 #if CONFIG_MPEG_XVMC_DECODER
1234 PIX_FMT_XVMC_MPEG2_IDCT,
1235 PIX_FMT_XVMC_MPEG2_MC,
1237 #if CONFIG_MPEG2_VDPAU_HWACCEL
1238 PIX_FMT_VDPAU_MPEG2,
1240 #if CONFIG_MPEG2_DXVA2_HWACCEL
1243 #if CONFIG_MPEG2_VAAPI_HWACCEL
1250 static inline int uses_vdpau(AVCodecContext *avctx) {
1251 return avctx->pix_fmt == PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == PIX_FMT_VDPAU_MPEG2;
1254 static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx){
1255 Mpeg1Context *s1 = avctx->priv_data;
1256 MpegEncContext *s = &s1->mpeg_enc_ctx;
1258 if(s->chroma_format < 2) {
1259 enum PixelFormat res;
1260 res = avctx->get_format(avctx,
1261 avctx->codec_id == CODEC_ID_MPEG1VIDEO ?
1262 mpeg1_hwaccel_pixfmt_list_420 :
1263 mpeg2_hwaccel_pixfmt_list_420);
1264 if (res != PIX_FMT_XVMC_MPEG2_IDCT && res != PIX_FMT_XVMC_MPEG2_MC) {
1265 avctx->xvmc_acceleration = 0;
1266 } else if (!avctx->xvmc_acceleration) {
1267 avctx->xvmc_acceleration = 2;
1270 } else if(s->chroma_format == 2)
1271 return PIX_FMT_YUV422P;
1273 return PIX_FMT_YUV444P;
1276 /* Call this function when we know all parameters.
1277 * It may be called in different places for MPEG-1 and MPEG-2. */
1278 static int mpeg_decode_postinit(AVCodecContext *avctx){
1279 Mpeg1Context *s1 = avctx->priv_data;
1280 MpegEncContext *s = &s1->mpeg_enc_ctx;
1281 uint8_t old_permutation[64];
1284 (s1->mpeg_enc_ctx_allocated == 0)||
1285 avctx->coded_width != s->width ||
1286 avctx->coded_height != s->height||
1287 s1->save_width != s->width ||
1288 s1->save_height != s->height ||
1289 s1->save_aspect_info != s->aspect_ratio_info||
1290 s1->save_progressive_seq != s->progressive_sequence ||
1294 if (s1->mpeg_enc_ctx_allocated) {
1295 ParseContext pc= s->parse_context;
1296 s->parse_context.buffer=0;
1298 s->parse_context= pc;
1301 if( (s->width == 0 )||(s->height == 0))
1304 avcodec_set_dimensions(avctx, s->width, s->height);
1305 avctx->bit_rate = s->bit_rate;
1306 s1->save_aspect_info = s->aspect_ratio_info;
1307 s1->save_width = s->width;
1308 s1->save_height = s->height;
1309 s1->save_progressive_seq = s->progressive_sequence;
1311 /* low_delay may be forced, in this case we will have B-frames
1312 * that behave like P-frames. */
1313 avctx->has_b_frames = !(s->low_delay);
1315 assert((avctx->sub_id==1) == (avctx->codec_id==CODEC_ID_MPEG1VIDEO));
1316 if(avctx->codec_id==CODEC_ID_MPEG1VIDEO){
1318 avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
1319 avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
1321 avctx->sample_aspect_ratio= av_d2q(
1322 1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1323 avctx->ticks_per_frame=1;
1327 &s->avctx->time_base.den,
1328 &s->avctx->time_base.num,
1329 ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
1330 ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1332 avctx->ticks_per_frame=2;
1334 if(s->aspect_ratio_info > 1){
1337 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
1338 (AVRational){s1->pan_scan.width, s1->pan_scan.height}),
1339 (AVRational){s->width, s->height});
1341 // we ignore the spec here and guess a bit as reality does not match the spec, see for example
1342 // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
1343 // issue1613, 621, 562
1344 if((s1->pan_scan.width == 0 ) || (s1->pan_scan.height == 0) ||
1345 (av_cmp_q(dar,(AVRational){4,3}) && av_cmp_q(dar,(AVRational){16,9}))) {
1346 s->avctx->sample_aspect_ratio=
1348 ff_mpeg2_aspect[s->aspect_ratio_info],
1349 (AVRational){s->width, s->height}
1352 s->avctx->sample_aspect_ratio=
1354 ff_mpeg2_aspect[s->aspect_ratio_info],
1355 (AVRational){s1->pan_scan.width, s1->pan_scan.height}
1357 //issue1613 4/3 16/9 -> 16/9
1358 //res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
1359 //widescreen-issue562.mpg 4/3 16/9 -> 16/9
1360 // s->avctx->sample_aspect_ratio= av_mul_q(s->avctx->sample_aspect_ratio, (AVRational){s->width, s->height});
1361 //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);
1362 //av_log(NULL, AV_LOG_ERROR, "B %d/%d\n",s->avctx->sample_aspect_ratio.num, s->avctx->sample_aspect_ratio.den);
1365 s->avctx->sample_aspect_ratio=
1366 ff_mpeg2_aspect[s->aspect_ratio_info];
1370 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1371 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
1372 //until then pix_fmt may be changed right after codec init
1373 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
1375 if( avctx->idct_algo == FF_IDCT_AUTO )
1376 avctx->idct_algo = FF_IDCT_SIMPLE;
1378 /* Quantization matrices may need reordering
1379 * if DCT permutation is changed. */
1380 memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
1382 if (MPV_common_init(s) < 0)
1385 quant_matrix_rebuild(s->intra_matrix, old_permutation,s->dsp.idct_permutation);
1386 quant_matrix_rebuild(s->inter_matrix, old_permutation,s->dsp.idct_permutation);
1387 quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
1388 quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
1390 s1->mpeg_enc_ctx_allocated = 1;
1395 static int mpeg1_decode_picture(AVCodecContext *avctx,
1396 const uint8_t *buf, int buf_size)
1398 Mpeg1Context *s1 = avctx->priv_data;
1399 MpegEncContext *s = &s1->mpeg_enc_ctx;
1400 int ref, f_code, vbv_delay;
1402 init_get_bits(&s->gb, buf, buf_size*8);
1404 ref = get_bits(&s->gb, 10); /* temporal ref */
1405 s->pict_type = get_bits(&s->gb, 3);
1406 if(s->pict_type == 0 || s->pict_type > 3)
1409 vbv_delay= get_bits(&s->gb, 16);
1410 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
1411 s->full_pel[0] = get_bits1(&s->gb);
1412 f_code = get_bits(&s->gb, 3);
1413 if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
1415 s->mpeg_f_code[0][0] = f_code;
1416 s->mpeg_f_code[0][1] = f_code;
1418 if (s->pict_type == AV_PICTURE_TYPE_B) {
1419 s->full_pel[1] = get_bits1(&s->gb);
1420 f_code = get_bits(&s->gb, 3);
1421 if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
1423 s->mpeg_f_code[1][0] = f_code;
1424 s->mpeg_f_code[1][1] = f_code;
1426 s->current_picture.pict_type= s->pict_type;
1427 s->current_picture.key_frame= s->pict_type == AV_PICTURE_TYPE_I;
1429 if(avctx->debug & FF_DEBUG_PICT_INFO)
1430 av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1437 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1439 MpegEncContext *s= &s1->mpeg_enc_ctx;
1440 int horiz_size_ext, vert_size_ext;
1443 skip_bits(&s->gb, 1); /* profile and level esc*/
1444 s->avctx->profile= get_bits(&s->gb, 3);
1445 s->avctx->level= get_bits(&s->gb, 4);
1446 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1447 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1448 horiz_size_ext = get_bits(&s->gb, 2);
1449 vert_size_ext = get_bits(&s->gb, 2);
1450 s->width |= (horiz_size_ext << 12);
1451 s->height |= (vert_size_ext << 12);
1452 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1453 s->bit_rate += (bit_rate_ext << 18) * 400;
1454 skip_bits1(&s->gb); /* marker */
1455 s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
1457 s->low_delay = get_bits1(&s->gb);
1458 if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
1460 s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
1461 s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
1463 av_dlog(s->avctx, "sequence extension\n");
1464 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
1465 s->avctx->sub_id = 2; /* indicates MPEG-2 found */
1467 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1468 av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1469 s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
1473 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1475 MpegEncContext *s= &s1->mpeg_enc_ctx;
1476 int color_description, w, h;
1478 skip_bits(&s->gb, 3); /* video format */
1479 color_description= get_bits1(&s->gb);
1480 if(color_description){
1481 s->avctx->color_primaries= get_bits(&s->gb, 8);
1482 s->avctx->color_trc = get_bits(&s->gb, 8);
1483 s->avctx->colorspace = get_bits(&s->gb, 8);
1485 w= get_bits(&s->gb, 14);
1486 skip_bits(&s->gb, 1); //marker
1487 h= get_bits(&s->gb, 14);
1488 // remaining 3 bits are zero padding
1490 s1->pan_scan.width= 16*w;
1491 s1->pan_scan.height=16*h;
1493 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1494 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1497 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1499 MpegEncContext *s= &s1->mpeg_enc_ctx;
1503 if(s->progressive_sequence){
1504 if(s->repeat_first_field){
1506 if(s->top_field_first)
1510 if(s->picture_structure == PICT_FRAME){
1512 if(s->repeat_first_field)
1516 for(i=0; i<nofco; i++){
1517 s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
1518 skip_bits(&s->gb, 1); //marker
1519 s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
1520 skip_bits(&s->gb, 1); //marker
1523 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1524 av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
1525 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1526 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1527 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
1531 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra){
1534 for(i=0; i<64; i++) {
1535 int j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1536 int v = get_bits(&s->gb, 8);
1538 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1541 if(intra && i==0 && v!=8){
1542 av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
1543 v= 8; // needed by pink.mpg / issue1046
1552 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1554 av_dlog(s->avctx, "matrix extension\n");
1556 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1557 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1558 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
1559 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
1562 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1564 MpegEncContext *s= &s1->mpeg_enc_ctx;
1566 s->full_pel[0] = s->full_pel[1] = 0;
1567 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1568 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1569 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1570 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1571 if(!s->pict_type && s1->mpeg_enc_ctx_allocated){
1572 av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
1573 if(s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1]==15){
1574 if(s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1575 s->pict_type= AV_PICTURE_TYPE_I;
1577 s->pict_type= AV_PICTURE_TYPE_P;
1579 s->pict_type= AV_PICTURE_TYPE_B;
1580 s->current_picture.pict_type= s->pict_type;
1581 s->current_picture.key_frame= s->pict_type == AV_PICTURE_TYPE_I;
1583 s->intra_dc_precision = get_bits(&s->gb, 2);
1584 s->picture_structure = get_bits(&s->gb, 2);
1585 s->top_field_first = get_bits1(&s->gb);
1586 s->frame_pred_frame_dct = get_bits1(&s->gb);
1587 s->concealment_motion_vectors = get_bits1(&s->gb);
1588 s->q_scale_type = get_bits1(&s->gb);
1589 s->intra_vlc_format = get_bits1(&s->gb);
1590 s->alternate_scan = get_bits1(&s->gb);
1591 s->repeat_first_field = get_bits1(&s->gb);
1592 s->chroma_420_type = get_bits1(&s->gb);
1593 s->progressive_frame = get_bits1(&s->gb);
1595 if(s->progressive_sequence && !s->progressive_frame){
1596 s->progressive_frame= 1;
1597 av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
1600 if(s->picture_structure==0 || (s->progressive_frame && s->picture_structure!=PICT_FRAME)){
1601 av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
1602 s->picture_structure= PICT_FRAME;
1605 if(s->progressive_sequence && !s->frame_pred_frame_dct){
1606 av_log(s->avctx, AV_LOG_ERROR, "invalid frame_pred_frame_dct\n");
1609 if(s->picture_structure == PICT_FRAME){
1611 s->v_edge_pos= 16*s->mb_height;
1613 s->first_field ^= 1;
1614 s->v_edge_pos= 8*s->mb_height;
1615 memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
1618 if(s->alternate_scan){
1619 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
1620 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
1622 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
1623 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
1626 /* composite display not parsed */
1627 av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1628 av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1629 av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1630 av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1631 av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1632 av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1633 av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1634 av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1635 av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1638 static void exchange_uv(MpegEncContext *s){
1641 tmp = s->pblocks[4];
1642 s->pblocks[4] = s->pblocks[5];
1643 s->pblocks[5] = tmp;
1646 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size){
1647 AVCodecContext *avctx= s->avctx;
1648 Mpeg1Context *s1 = (Mpeg1Context*)s;
1650 /* start frame decoding */
1651 if(s->first_field || s->picture_structure==PICT_FRAME){
1652 if(MPV_frame_start(s, avctx) < 0)
1655 ff_er_frame_start(s);
1657 /* first check if we must repeat the frame */
1658 s->current_picture_ptr->repeat_pict = 0;
1659 if (s->repeat_first_field) {
1660 if (s->progressive_sequence) {
1661 if (s->top_field_first)
1662 s->current_picture_ptr->repeat_pict = 4;
1664 s->current_picture_ptr->repeat_pict = 2;
1665 } else if (s->progressive_frame) {
1666 s->current_picture_ptr->repeat_pict = 1;
1670 *s->current_picture_ptr->pan_scan= s1->pan_scan;
1672 if (HAVE_PTHREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
1673 ff_thread_finish_setup(avctx);
1674 }else{ //second field
1677 if(!s->current_picture_ptr){
1678 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1683 s->current_picture.data[i] = s->current_picture_ptr->data[i];
1684 if(s->picture_structure == PICT_BOTTOM_FIELD){
1685 s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
1690 if (avctx->hwaccel) {
1691 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
1695 // MPV_frame_start will call this function too,
1696 // but we need to call it on every field
1697 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1698 if(ff_xvmc_field_start(s,avctx) < 0)
1704 #define DECODE_SLICE_ERROR -1
1705 #define DECODE_SLICE_OK 0
1708 * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
1709 * @return DECODE_SLICE_ERROR if the slice is damaged<br>
1710 * DECODE_SLICE_OK if this slice is ok<br>
1712 static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
1713 const uint8_t **buf, int buf_size)
1715 MpegEncContext *s = &s1->mpeg_enc_ctx;
1716 AVCodecContext *avctx= s->avctx;
1717 const int field_pic= s->picture_structure != PICT_FRAME;
1718 const int lowres= s->avctx->lowres;
1723 assert(mb_y < s->mb_height);
1725 init_get_bits(&s->gb, *buf, buf_size*8);
1727 ff_mpeg1_clean_buffers(s);
1728 s->interlaced_dct = 0;
1730 s->qscale = get_qscale(s);
1733 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1737 /* extra slice info */
1738 while (get_bits1(&s->gb) != 0) {
1739 skip_bits(&s->gb, 8);
1744 if(mb_y==0 && s->codec_tag == AV_RL32("SLIF")){
1748 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1750 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1757 /* otherwise, stuffing, nothing to do */
1765 if(s->mb_x >= (unsigned)s->mb_width){
1766 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1770 if (avctx->hwaccel) {
1771 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1772 int start_code = -1;
1773 buf_end = ff_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1774 if (buf_end < *buf + buf_size)
1777 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1778 return DECODE_SLICE_ERROR;
1780 return DECODE_SLICE_OK;
1783 s->resync_mb_x= s->mb_x;
1784 s->resync_mb_y= s->mb_y= mb_y;
1786 ff_init_block_index(s);
1788 if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
1789 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
1790 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",
1791 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],
1792 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")),
1793 s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
1794 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
1795 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
1800 //If 1, we memcpy blocks in xvmcvideo.
1801 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1802 ff_xvmc_init_block(s);//set s->block
1804 if(mpeg_decode_mb(s, s->block) < 0)
1807 if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
1808 const int wrap = s->b8_stride;
1809 int xy = s->mb_x*2 + s->mb_y*2*wrap;
1810 int b8_xy= 4*(s->mb_x + s->mb_y*s->mb_stride);
1811 int motion_x, motion_y, dir, i;
1814 for(dir=0; dir<2; dir++){
1815 if (s->mb_intra || (dir==1 && s->pict_type != AV_PICTURE_TYPE_B)) {
1816 motion_x = motion_y = 0;
1817 }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
1818 motion_x = s->mv[dir][0][0];
1819 motion_y = s->mv[dir][0][1];
1820 } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
1821 motion_x = s->mv[dir][i][0];
1822 motion_y = s->mv[dir][i][1];
1825 s->current_picture.motion_val[dir][xy ][0] = motion_x;
1826 s->current_picture.motion_val[dir][xy ][1] = motion_y;
1827 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1828 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1829 s->current_picture.ref_index [dir][b8_xy ]=
1830 s->current_picture.ref_index [dir][b8_xy + 1]= s->field_select[dir][i];
1831 assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
1838 s->dest[0] += 16 >> lowres;
1839 s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
1840 s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
1842 MPV_decode_mb(s, s->block);
1844 if (++s->mb_x >= s->mb_width) {
1845 const int mb_size= 16>>s->avctx->lowres;
1847 ff_draw_horiz_band(s, mb_size*(s->mb_y>>field_pic), mb_size);
1848 MPV_report_decode_progress(s);
1851 s->mb_y += 1<<field_pic;
1853 if(s->mb_y >= s->mb_height){
1854 int left= get_bits_left(&s->gb);
1855 int is_d10= s->chroma_format==2 && s->pict_type==AV_PICTURE_TYPE_I && avctx->profile==0 && avctx->level==5
1856 && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
1857 && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
1859 if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
1860 || (avctx->error_recognition >= FF_ER_AGGRESSIVE && left>8)){
1861 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
1867 ff_init_block_index(s);
1870 /* skip mb handling */
1871 if (s->mb_skip_run == -1) {
1872 /* read increment again */
1875 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1877 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1882 s->mb_skip_run += 33;
1883 }else if(code == 35){
1884 if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
1885 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1888 goto eos; /* end of slice */
1890 /* otherwise, stuffing, nothing to do */
1892 s->mb_skip_run += code;
1898 if(s->pict_type == AV_PICTURE_TYPE_I){
1899 av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1906 s->block_last_index[i] = -1;
1907 if(s->picture_structure == PICT_FRAME)
1908 s->mv_type = MV_TYPE_16X16;
1910 s->mv_type = MV_TYPE_FIELD;
1911 if (s->pict_type == AV_PICTURE_TYPE_P) {
1912 /* if P type, zero motion vector is implied */
1913 s->mv_dir = MV_DIR_FORWARD;
1914 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1915 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1916 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1917 s->field_select[0][0]= (s->picture_structure - 1) & 1;
1919 /* if B type, reuse previous vectors and directions */
1920 s->mv[0][0][0] = s->last_mv[0][0][0];
1921 s->mv[0][0][1] = s->last_mv[0][0][1];
1922 s->mv[1][0][0] = s->last_mv[1][0][0];
1923 s->mv[1][0][1] = s->last_mv[1][0][1];
1928 eos: // end of slice
1929 *buf += (get_bits_count(&s->gb)-1)/8;
1930 //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1934 static int slice_decode_thread(AVCodecContext *c, void *arg){
1935 MpegEncContext *s= *(void**)arg;
1936 const uint8_t *buf= s->gb.buffer;
1937 int mb_y= s->start_mb_y;
1938 const int field_pic= s->picture_structure != PICT_FRAME;
1940 s->error_count= (3*(s->end_mb_y - s->start_mb_y)*s->mb_width) >> field_pic;
1943 uint32_t start_code;
1946 ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
1948 //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1949 //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);
1951 if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
1952 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);
1954 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);
1957 if(s->mb_y == s->end_mb_y)
1961 buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
1962 mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
1963 if (s->picture_structure == PICT_BOTTOM_FIELD)
1965 if(mb_y < 0 || mb_y >= s->end_mb_y)
1969 return 0; //not reached
1973 * Handle slice ends.
1974 * @return 1 if it seems to be the last slice
1976 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
1978 Mpeg1Context *s1 = avctx->priv_data;
1979 MpegEncContext *s = &s1->mpeg_enc_ctx;
1981 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
1984 if (s->avctx->hwaccel) {
1985 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1986 av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
1989 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1990 ff_xvmc_field_end(s);
1992 /* end of slice reached */
1993 if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
1996 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
2002 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
2003 *pict= *(AVFrame*)s->current_picture_ptr;
2004 ff_print_debug_info(s, pict);
2006 if (avctx->active_thread_type & FF_THREAD_FRAME)
2007 s->picture_number++;
2008 /* latency of 1 frame for I- and P-frames */
2009 /* XXX: use another variable than picture_number */
2010 if (s->last_picture_ptr != NULL) {
2011 *pict= *(AVFrame*)s->last_picture_ptr;
2012 ff_print_debug_info(s, pict);
2022 static int mpeg1_decode_sequence(AVCodecContext *avctx,
2023 const uint8_t *buf, int buf_size)
2025 Mpeg1Context *s1 = avctx->priv_data;
2026 MpegEncContext *s = &s1->mpeg_enc_ctx;
2030 init_get_bits(&s->gb, buf, buf_size*8);
2032 width = get_bits(&s->gb, 12);
2033 height = get_bits(&s->gb, 12);
2034 if (width <= 0 || height <= 0)
2036 s->aspect_ratio_info= get_bits(&s->gb, 4);
2037 if (s->aspect_ratio_info == 0) {
2038 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
2039 if (avctx->error_recognition >= FF_ER_COMPLIANT)
2042 s->frame_rate_index = get_bits(&s->gb, 4);
2043 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
2045 s->bit_rate = get_bits(&s->gb, 18) * 400;
2046 if (get_bits1(&s->gb) == 0) /* marker */
2051 s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
2052 skip_bits(&s->gb, 1);
2055 if (get_bits1(&s->gb)) {
2056 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
2059 j = s->dsp.idct_permutation[i];
2060 v = ff_mpeg1_default_intra_matrix[i];
2061 s->intra_matrix[j] = v;
2062 s->chroma_intra_matrix[j] = v;
2065 if (get_bits1(&s->gb)) {
2066 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
2069 int j= s->dsp.idct_permutation[i];
2070 v = ff_mpeg1_default_non_intra_matrix[i];
2071 s->inter_matrix[j] = v;
2072 s->chroma_inter_matrix[j] = v;
2076 if(show_bits(&s->gb, 23) != 0){
2077 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2081 /* we set MPEG-2 parameters so that it emulates MPEG-1 */
2082 s->progressive_sequence = 1;
2083 s->progressive_frame = 1;
2084 s->picture_structure = PICT_FRAME;
2085 s->frame_pred_frame_dct = 1;
2086 s->chroma_format = 1;
2087 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
2088 avctx->sub_id = 1; /* indicates MPEG-1 */
2089 s->out_format = FMT_MPEG1;
2090 s->swap_uv = 0;//AFAIK VCR2 does not have SEQ_HEADER
2091 if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
2093 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2094 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2095 s->avctx->rc_buffer_size, s->bit_rate);
2100 static int vcr2_init_sequence(AVCodecContext *avctx)
2102 Mpeg1Context *s1 = avctx->priv_data;
2103 MpegEncContext *s = &s1->mpeg_enc_ctx;
2106 /* start new MPEG-1 context decoding */
2107 s->out_format = FMT_MPEG1;
2108 if (s1->mpeg_enc_ctx_allocated) {
2111 s->width = avctx->coded_width;
2112 s->height = avctx->coded_height;
2113 avctx->has_b_frames= 0; //true?
2116 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2117 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
2119 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel )
2120 if( avctx->idct_algo == FF_IDCT_AUTO )
2121 avctx->idct_algo = FF_IDCT_SIMPLE;
2123 if (MPV_common_init(s) < 0)
2125 exchange_uv(s);//common init reset pblocks, so we swap them here
2126 s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
2127 s1->mpeg_enc_ctx_allocated = 1;
2130 int j= s->dsp.idct_permutation[i];
2131 v = ff_mpeg1_default_intra_matrix[i];
2132 s->intra_matrix[j] = v;
2133 s->chroma_intra_matrix[j] = v;
2135 v = ff_mpeg1_default_non_intra_matrix[i];
2136 s->inter_matrix[j] = v;
2137 s->chroma_inter_matrix[j] = v;
2140 s->progressive_sequence = 1;
2141 s->progressive_frame = 1;
2142 s->picture_structure = PICT_FRAME;
2143 s->frame_pred_frame_dct = 1;
2144 s->chroma_format = 1;
2145 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
2146 avctx->sub_id = 2; /* indicates MPEG-2 */
2147 s1->save_width = s->width;
2148 s1->save_height = s->height;
2149 s1->save_progressive_seq = s->progressive_sequence;
2154 static void mpeg_decode_user_data(AVCodecContext *avctx,
2155 const uint8_t *p, int buf_size)
2157 const uint8_t *buf_end = p+buf_size;
2159 /* we parse the DTG active format information */
2160 if (buf_end - p >= 5 &&
2161 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2169 if (buf_end - p < 1)
2171 avctx->dtg_active_format = p[0] & 0x0f;
2176 static void mpeg_decode_gop(AVCodecContext *avctx,
2177 const uint8_t *buf, int buf_size){
2178 Mpeg1Context *s1 = avctx->priv_data;
2179 MpegEncContext *s = &s1->mpeg_enc_ctx;
2181 int time_code_hours, time_code_minutes;
2182 int time_code_seconds, time_code_pictures;
2185 init_get_bits(&s->gb, buf, buf_size*8);
2187 skip_bits1(&s->gb); /* drop_frame_flag */
2189 time_code_hours=get_bits(&s->gb,5);
2190 time_code_minutes = get_bits(&s->gb,6);
2191 skip_bits1(&s->gb);//marker bit
2192 time_code_seconds = get_bits(&s->gb,6);
2193 time_code_pictures = get_bits(&s->gb,6);
2195 s->closed_gop = get_bits1(&s->gb);
2196 /*broken_link indicate that after editing the
2197 reference frames of the first B-Frames after GOP I-Frame
2198 are missing (open gop)*/
2199 broken_link = get_bits1(&s->gb);
2201 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2202 av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
2203 time_code_hours, time_code_minutes, time_code_seconds,
2204 time_code_pictures, s->closed_gop, broken_link);
2207 * Find the end of the current frame in the bitstream.
2208 * @return the position of the first byte of the next frame, or -1
2210 int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
2213 uint32_t state= pc->state;
2215 /* EOF considered as end of frame */
2220 0 frame start -> 1/4
2221 1 first_SEQEXT -> 0/2
2222 2 first field start -> 3/0
2223 3 second_SEQEXT -> 2/0
2227 for(i=0; i<buf_size; i++){
2228 assert(pc->frame_start_found>=0 && pc->frame_start_found<=4);
2229 if(pc->frame_start_found&1){
2230 if(state == EXT_START_CODE && (buf[i]&0xF0) != 0x80)
2231 pc->frame_start_found--;
2232 else if(state == EXT_START_CODE+2){
2233 if((buf[i]&3) == 3) pc->frame_start_found= 0;
2234 else pc->frame_start_found= (pc->frame_start_found+1)&3;
2238 i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
2239 if(pc->frame_start_found==0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
2241 pc->frame_start_found=4;
2243 if(state == SEQ_END_CODE){
2247 if(pc->frame_start_found==2 && state == SEQ_START_CODE)
2248 pc->frame_start_found= 0;
2249 if(pc->frame_start_found<4 && state == EXT_START_CODE)
2250 pc->frame_start_found++;
2251 if(pc->frame_start_found == 4 && (state&0xFFFFFF00) == 0x100){
2252 if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
2253 pc->frame_start_found=0;
2258 if(pc->frame_start_found == 0 && s && state == PICTURE_START_CODE){
2259 ff_fetch_timestamp(s, i-3, 1);
2264 return END_NOT_FOUND;
2267 static int decode_chunks(AVCodecContext *avctx,
2268 AVFrame *picture, int *data_size,
2269 const uint8_t *buf, int buf_size);
2271 /* handle buffering and image synchronisation */
2272 static int mpeg_decode_frame(AVCodecContext *avctx,
2273 void *data, int *data_size,
2276 const uint8_t *buf = avpkt->data;
2277 int buf_size = avpkt->size;
2278 Mpeg1Context *s = avctx->priv_data;
2279 AVFrame *picture = data;
2280 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2281 av_dlog(avctx, "fill_buffer\n");
2283 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2284 /* special case for last picture */
2285 if (s2->low_delay==0 && s2->next_picture_ptr) {
2286 *picture= *(AVFrame*)s2->next_picture_ptr;
2287 s2->next_picture_ptr= NULL;
2289 *data_size = sizeof(AVFrame);
2294 if(s2->flags&CODEC_FLAG_TRUNCATED){
2295 int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
2297 if( ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
2302 if (s->repeat_field % 2 == 1) {
2304 //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
2305 // s2->picture_number, s->repeat_field);
2306 if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
2307 *data_size = sizeof(AVPicture);
2313 if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == AV_RL32("VCR2"))
2314 vcr2_init_sequence(avctx);
2318 if(avctx->extradata && !avctx->frame_number)
2319 decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
2321 return decode_chunks(avctx, picture, data_size, buf, buf_size);
2324 static int decode_chunks(AVCodecContext *avctx,
2325 AVFrame *picture, int *data_size,
2326 const uint8_t *buf, int buf_size)
2328 Mpeg1Context *s = avctx->priv_data;
2329 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2330 const uint8_t *buf_ptr = buf;
2331 const uint8_t *buf_end = buf + buf_size;
2332 int ret, input_size;
2336 /* find next start code */
2337 uint32_t start_code = -1;
2338 buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
2339 if (start_code > 0x1ff){
2340 if(s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT){
2341 if(HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)){
2343 av_assert0(avctx->thread_count > 1);
2345 avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
2346 for(i=0; i<s->slice_count; i++)
2347 s2->error_count += s2->thread_context[i]->error_count;
2350 if (CONFIG_VDPAU && uses_vdpau(avctx))
2351 ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
2353 if (slice_end(avctx, picture)) {
2354 if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
2355 *data_size = sizeof(AVPicture);
2359 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2362 input_size = buf_end - buf_ptr;
2364 if(avctx->debug & FF_DEBUG_STARTCODE){
2365 av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
2368 /* prepare data for next start code */
2369 switch(start_code) {
2370 case SEQ_START_CODE:
2372 mpeg1_decode_sequence(avctx, buf_ptr,
2376 av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
2380 case PICTURE_START_CODE:
2381 if (HAVE_THREADS && (avctx->active_thread_type&FF_THREAD_SLICE) && s->slice_count) {
2384 avctx->execute(avctx, slice_decode_thread,
2385 s2->thread_context, NULL,
2386 s->slice_count, sizeof(void*));
2387 for (i = 0; i < s->slice_count; i++)
2388 s2->error_count += s2->thread_context[i]->error_count;
2391 if(last_code == 0 || last_code == SLICE_MIN_START_CODE){
2392 if(mpeg_decode_postinit(avctx) < 0){
2393 av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
2397 /* we have a complete image: we try to decompress it */
2398 if(mpeg1_decode_picture(avctx,
2399 buf_ptr, input_size) < 0)
2401 s2->first_slice = 1;
2402 last_code= PICTURE_START_CODE;
2404 av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
2407 case EXT_START_CODE:
2408 init_get_bits(&s2->gb, buf_ptr, input_size*8);
2410 switch(get_bits(&s2->gb, 4)) {
2413 mpeg_decode_sequence_extension(s);
2415 av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
2419 mpeg_decode_sequence_display_extension(s);
2422 mpeg_decode_quant_matrix_extension(s2);
2425 mpeg_decode_picture_display_extension(s);
2428 if(last_code == PICTURE_START_CODE){
2429 mpeg_decode_picture_coding_extension(s);
2431 av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
2436 case USER_START_CODE:
2437 mpeg_decode_user_data(avctx,
2438 buf_ptr, input_size);
2440 case GOP_START_CODE:
2443 mpeg_decode_gop(avctx,
2444 buf_ptr, input_size);
2447 av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
2451 if (start_code >= SLICE_MIN_START_CODE &&
2452 start_code <= SLICE_MAX_START_CODE && last_code!=0) {
2453 const int field_pic= s2->picture_structure != PICT_FRAME;
2454 int mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
2455 last_code= SLICE_MIN_START_CODE;
2457 if(s2->picture_structure == PICT_BOTTOM_FIELD)
2460 if (mb_y >= s2->mb_height){
2461 av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2465 if(s2->last_picture_ptr==NULL){
2466 /* Skip B-frames if we do not have reference frames and gop is not closed */
2467 if(s2->pict_type==AV_PICTURE_TYPE_B){
2472 if(s2->pict_type==AV_PICTURE_TYPE_I)
2474 if(s2->next_picture_ptr==NULL){
2475 /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
2476 if(s2->pict_type==AV_PICTURE_TYPE_P && !s->sync) break;
2478 if( (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==AV_PICTURE_TYPE_B)
2479 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=AV_PICTURE_TYPE_I)
2480 || avctx->skip_frame >= AVDISCARD_ALL)
2483 if (!s->mpeg_enc_ctx_allocated) break;
2485 if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
2486 if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
2491 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2495 if(s2->first_slice){
2497 if(mpeg_field_start(s2, buf, buf_size) < 0)
2500 if(!s2->current_picture_ptr){
2501 av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
2505 if (uses_vdpau(avctx)) {
2510 if(HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)){
2511 int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
2512 av_assert0(avctx->thread_count > 1);
2513 if(threshold <= mb_y){
2514 MpegEncContext *thread_context= s2->thread_context[s->slice_count];
2516 thread_context->start_mb_y= mb_y;
2517 thread_context->end_mb_y = s2->mb_height;
2519 s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
2520 ff_update_duplicate_context(thread_context, s2);
2522 init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
2525 buf_ptr += 2; //FIXME add minimum number of bytes per slice
2527 ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
2531 if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
2532 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);
2534 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);
2543 static void flush(AVCodecContext *avctx){
2544 Mpeg1Context *s = avctx->priv_data;
2548 ff_mpeg_flush(avctx);
2551 static int mpeg_decode_end(AVCodecContext *avctx)
2553 Mpeg1Context *s = avctx->priv_data;
2555 if (s->mpeg_enc_ctx_allocated)
2556 MPV_common_end(&s->mpeg_enc_ctx);
2560 static const AVProfile mpeg2_video_profiles[] = {
2561 { FF_PROFILE_MPEG2_422, "4:2:2" },
2562 { FF_PROFILE_MPEG2_HIGH, "High" },
2563 { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
2564 { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
2565 { FF_PROFILE_MPEG2_MAIN, "Main" },
2566 { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
2567 { FF_PROFILE_RESERVED, "Reserved" },
2568 { FF_PROFILE_RESERVED, "Reserved" },
2569 { FF_PROFILE_UNKNOWN },
2573 AVCodec ff_mpeg1video_decoder = {
2576 CODEC_ID_MPEG1VIDEO,
2577 sizeof(Mpeg1Context),
2582 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
2585 .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2586 .update_thread_context= ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
2589 AVCodec ff_mpeg2video_decoder = {
2592 CODEC_ID_MPEG2VIDEO,
2593 sizeof(Mpeg1Context),
2598 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
2601 .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2602 .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
2606 AVCodec ff_mpegvideo_decoder = {
2609 CODEC_ID_MPEG2VIDEO,
2610 sizeof(Mpeg1Context),
2615 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
2618 .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2621 #if CONFIG_MPEG_XVMC_DECODER
2622 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx){
2623 if( avctx->active_thread_type & FF_THREAD_SLICE )
2625 if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
2627 if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
2628 av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2630 mpeg_decode_init(avctx);
2632 avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
2633 avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
2638 AVCodec ff_mpeg_xvmc_decoder = {
2641 CODEC_ID_MPEG2VIDEO_XVMC,
2642 sizeof(Mpeg1Context),
2643 mpeg_mc_decode_init,
2647 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2649 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2654 #if CONFIG_MPEG_VDPAU_DECODER
2655 AVCodec ff_mpeg_vdpau_decoder = {
2658 CODEC_ID_MPEG2VIDEO,
2659 sizeof(Mpeg1Context),
2664 CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2666 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
2670 #if CONFIG_MPEG1_VDPAU_DECODER
2671 AVCodec ff_mpeg1_vdpau_decoder = {
2674 CODEC_ID_MPEG1VIDEO,
2675 sizeof(Mpeg1Context),
2680 CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2682 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),