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"
35 #include "mpeg12data.h"
36 #include "mpeg12decdata.h"
37 #include "bytestream.h"
38 #include "vdpau_internal.h"
39 #include "xvmc_internal.h"
46 #define MBINCR_VLC_BITS 9
47 #define MB_PAT_VLC_BITS 9
48 #define MB_PTYPE_VLC_BITS 6
49 #define MB_BTYPE_VLC_BITS 6
51 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
54 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
57 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
58 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
61 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
64 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
65 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
66 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
67 static void exchange_uv(MpegEncContext *s);
69 static const enum PixelFormat pixfmt_xvmc_mpg2_420[] = {
70 PIX_FMT_XVMC_MPEG2_IDCT,
71 PIX_FMT_XVMC_MPEG2_MC,
74 uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
77 #define INIT_2D_VLC_RL(rl, static_size)\
79 static RL_VLC_ELEM rl_vlc_table[static_size];\
80 INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
81 &rl.table_vlc[0][1], 4, 2,\
82 &rl.table_vlc[0][0], 4, 2, static_size);\
84 rl.rl_vlc[0]= rl_vlc_table;\
88 static void init_2d_vlc_rl(RLTable *rl)
92 for(i=0; i<rl->vlc.table_size; i++){
93 int code= rl->vlc.table[i][0];
94 int len = rl->vlc.table[i][1];
97 if(len==0){ // illegal code
100 }else if(len<0){ //more bits needed
104 if(code==rl->n){ //esc
107 }else if(code==rl->n+1){ //eob
111 run= rl->table_run [code] + 1;
112 level= rl->table_level[code];
115 rl->rl_vlc[0][i].len= len;
116 rl->rl_vlc[0][i].level= level;
117 rl->rl_vlc[0][i].run= run;
121 void ff_mpeg12_common_init(MpegEncContext *s)
125 s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
129 void ff_mpeg1_clean_buffers(MpegEncContext *s){
130 s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
131 s->last_dc[1] = s->last_dc[0];
132 s->last_dc[2] = s->last_dc[0];
133 memset(s->last_mv, 0, sizeof(s->last_mv));
137 /******************************************/
141 VLC ff_dc_chroma_vlc;
144 static VLC mbincr_vlc;
145 static VLC mb_ptype_vlc;
146 static VLC mb_btype_vlc;
147 static VLC mb_pat_vlc;
149 av_cold void ff_mpeg12_init_vlcs(void)
156 INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
157 ff_mpeg12_vlc_dc_lum_bits, 1, 1,
158 ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
159 INIT_VLC_STATIC(&ff_dc_chroma_vlc, DC_VLC_BITS, 12,
160 ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
161 ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
162 INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
163 &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
164 &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
165 INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
166 &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
167 &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
168 INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
169 &ff_mpeg12_mbPatTable[0][1], 2, 1,
170 &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
172 INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
173 &table_mb_ptype[0][1], 2, 1,
174 &table_mb_ptype[0][0], 2, 1, 64);
175 INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
176 &table_mb_btype[0][1], 2, 1,
177 &table_mb_btype[0][0], 2, 1, 64);
178 init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
179 init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
181 INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
182 INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
186 static inline int get_dmv(MpegEncContext *s)
188 if(get_bits1(&s->gb))
189 return 1 - (get_bits1(&s->gb) << 1);
194 static inline int get_qscale(MpegEncContext *s)
196 int qscale = get_bits(&s->gb, 5);
197 if (s->q_scale_type) {
198 return non_linear_qscale[qscale];
204 /* motion type (for MPEG-2) */
210 static int mpeg_decode_mb(MpegEncContext *s,
211 DCTELEM block[12][64])
213 int i, j, k, cbp, val, mb_type, motion_type;
214 const int mb_block_count = 4 + (1<< s->chroma_format);
216 av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
218 assert(s->mb_skipped==0);
220 if (s->mb_skip_run-- != 0) {
221 if (s->pict_type == FF_P_TYPE) {
223 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
228 mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
230 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
231 if(IS_INTRA(mb_type))
234 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
235 mb_type | MB_TYPE_SKIP;
236 // assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
238 if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
245 switch(s->pict_type) {
248 if (get_bits1(&s->gb) == 0) {
249 if (get_bits1(&s->gb) == 0){
250 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
253 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
255 mb_type = MB_TYPE_INTRA;
259 mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
261 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
264 mb_type = ptype2mb_type[ mb_type ];
267 mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
269 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
272 mb_type = btype2mb_type[ mb_type ];
275 av_dlog(s->avctx, "mb_type=%x\n", mb_type);
276 // motion_type = 0; /* avoid warning */
277 if (IS_INTRA(mb_type)) {
278 s->dsp.clear_blocks(s->block[0]);
280 if(!s->chroma_y_shift){
281 s->dsp.clear_blocks(s->block[6]);
284 /* compute DCT type */
285 if (s->picture_structure == PICT_FRAME && //FIXME add an interlaced_dct coded var?
286 !s->frame_pred_frame_dct) {
287 s->interlaced_dct = get_bits1(&s->gb);
290 if (IS_QUANT(mb_type))
291 s->qscale = get_qscale(s);
293 if (s->concealment_motion_vectors) {
294 /* just parse them */
295 if (s->picture_structure != PICT_FRAME)
296 skip_bits1(&s->gb); /* field select */
298 s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
299 mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
300 s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
301 mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
303 skip_bits1(&s->gb); /* marker */
305 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
307 //if 1, we memcpy blocks in xvmcvideo
308 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1){
309 ff_xvmc_pack_pblocks(s,-1);//inter are always full blocks
315 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
316 if(s->flags2 & CODEC_FLAG2_FAST){
318 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
321 for(i=0;i<mb_block_count;i++) {
322 if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
328 if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
333 if (mb_type & MB_TYPE_ZERO_MV){
334 assert(mb_type & MB_TYPE_CBP);
336 s->mv_dir = MV_DIR_FORWARD;
337 if(s->picture_structure == PICT_FRAME){
338 if(!s->frame_pred_frame_dct)
339 s->interlaced_dct = get_bits1(&s->gb);
340 s->mv_type = MV_TYPE_16X16;
342 s->mv_type = MV_TYPE_FIELD;
343 mb_type |= MB_TYPE_INTERLACED;
344 s->field_select[0][0]= s->picture_structure - 1;
347 if (IS_QUANT(mb_type))
348 s->qscale = get_qscale(s);
350 s->last_mv[0][0][0] = 0;
351 s->last_mv[0][0][1] = 0;
352 s->last_mv[0][1][0] = 0;
353 s->last_mv[0][1][1] = 0;
357 assert(mb_type & MB_TYPE_L0L1);
358 //FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
359 /* get additional motion vector type */
360 if (s->frame_pred_frame_dct)
361 motion_type = MT_FRAME;
363 motion_type = get_bits(&s->gb, 2);
364 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
365 s->interlaced_dct = get_bits1(&s->gb);
368 if (IS_QUANT(mb_type))
369 s->qscale = get_qscale(s);
372 s->mv_dir= (mb_type>>13)&3;
373 av_dlog(s->avctx, "motion_type=%d\n", motion_type);
374 switch(motion_type) {
375 case MT_FRAME: /* or MT_16X8 */
376 if (s->picture_structure == PICT_FRAME) {
377 mb_type |= MB_TYPE_16x16;
378 s->mv_type = MV_TYPE_16X16;
380 if (USES_LIST(mb_type, i)) {
382 s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
383 mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
384 s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
385 mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
386 /* full_pel: only for MPEG-1 */
388 s->mv[i][0][0] <<= 1;
389 s->mv[i][0][1] <<= 1;
394 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
395 s->mv_type = MV_TYPE_16X8;
397 if (USES_LIST(mb_type, i)) {
400 s->field_select[i][j] = get_bits1(&s->gb);
402 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
403 s->last_mv[i][j][k]);
404 s->last_mv[i][j][k] = val;
405 s->mv[i][j][k] = val;
413 s->mv_type = MV_TYPE_FIELD;
414 if (s->picture_structure == PICT_FRAME) {
415 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
417 if (USES_LIST(mb_type, i)) {
419 s->field_select[i][j] = get_bits1(&s->gb);
420 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
421 s->last_mv[i][j][0]);
422 s->last_mv[i][j][0] = val;
423 s->mv[i][j][0] = val;
424 av_dlog(s->avctx, "fmx=%d\n", val);
425 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
426 s->last_mv[i][j][1] >> 1);
427 s->last_mv[i][j][1] = val << 1;
428 s->mv[i][j][1] = val;
429 av_dlog(s->avctx, "fmy=%d\n", val);
434 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
436 if (USES_LIST(mb_type, i)) {
437 s->field_select[i][0] = get_bits1(&s->gb);
439 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
440 s->last_mv[i][0][k]);
441 s->last_mv[i][0][k] = val;
442 s->last_mv[i][1][k] = val;
443 s->mv[i][0][k] = val;
450 s->mv_type = MV_TYPE_DMV;
452 if (USES_LIST(mb_type, i)) {
453 int dmx, dmy, mx, my, m;
454 const int my_shift= s->picture_structure == PICT_FRAME;
456 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
457 s->last_mv[i][0][0]);
458 s->last_mv[i][0][0] = mx;
459 s->last_mv[i][1][0] = mx;
461 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
462 s->last_mv[i][0][1] >> my_shift);
466 s->last_mv[i][0][1] = my<<my_shift;
467 s->last_mv[i][1][1] = my<<my_shift;
471 s->mv[i][1][0] = mx;//not used
472 s->mv[i][1][1] = my;//not used
474 if (s->picture_structure == PICT_FRAME) {
475 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
477 //m = 1 + 2 * s->top_field_first;
478 m = s->top_field_first ? 1 : 3;
480 /* top -> top pred */
481 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
482 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
484 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
485 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
487 mb_type |= MB_TYPE_16x16;
489 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
490 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
491 if(s->picture_structure == PICT_TOP_FIELD)
500 av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
506 if (HAS_CBP(mb_type)) {
507 s->dsp.clear_blocks(s->block[0]);
509 cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
510 if(mb_block_count > 6){
511 cbp<<= mb_block_count-6;
512 cbp |= get_bits(&s->gb, mb_block_count-6);
513 s->dsp.clear_blocks(s->block[6]);
516 av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
520 //if 1, we memcpy blocks in xvmcvideo
521 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1){
522 ff_xvmc_pack_pblocks(s,cbp);
528 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
529 if(s->flags2 & CODEC_FLAG2_FAST){
532 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
534 s->block_last_index[i] = -1;
539 cbp<<= 12-mb_block_count;
541 for(i=0;i<mb_block_count;i++) {
542 if ( cbp & (1<<11) ) {
543 if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
546 s->block_last_index[i] = -1;
552 if(s->flags2 & CODEC_FLAG2_FAST){
555 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
557 s->block_last_index[i] = -1;
564 if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
567 s->block_last_index[i] = -1;
575 s->block_last_index[i] = -1;
579 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
584 /* as H.263, but only 17 codes */
585 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
587 int code, sign, val, l, shift;
589 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
597 sign = get_bits1(&s->gb);
601 val = (val - 1) << shift;
602 val |= get_bits(&s->gb, shift);
609 /* modulo decoding */
610 l= INT_BIT - 5 - shift;
615 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
619 int level, dc, diff, i, j, run;
621 RLTable *rl = &ff_rl_mpeg1;
622 uint8_t * const scantable= s->intra_scantable.permutated;
623 const uint16_t *quant_matrix= s->intra_matrix;
624 const int qscale= s->qscale;
627 component = (n <= 3 ? 0 : n - 4 + 1);
628 diff = decode_dc(&s->gb, component);
631 dc = s->last_dc[component];
633 s->last_dc[component] = dc;
634 block[0] = dc*quant_matrix[0];
635 av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
638 OPEN_READER(re, &s->gb);
639 /* now quantify & encode AC coefficients */
641 UPDATE_CACHE(re, &s->gb);
642 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
646 } else if(level != 0) {
649 level= (level*qscale*quant_matrix[j])>>4;
651 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
652 LAST_SKIP_BITS(re, &s->gb, 1);
655 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
656 UPDATE_CACHE(re, &s->gb);
657 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
659 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
660 } else if (level == 0) {
661 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
667 level= (level*qscale*quant_matrix[j])>>4;
671 level= (level*qscale*quant_matrix[j])>>4;
676 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
682 CLOSE_READER(re, &s->gb);
684 s->block_last_index[n] = i;
688 int ff_mpeg1_decode_block_intra(MpegEncContext *s,
692 return mpeg1_decode_block_intra(s, block, n);
695 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
699 int level, i, j, run;
700 RLTable *rl = &ff_rl_mpeg1;
701 uint8_t * const scantable= s->intra_scantable.permutated;
702 const uint16_t *quant_matrix= s->inter_matrix;
703 const int qscale= s->qscale;
706 OPEN_READER(re, &s->gb);
708 // special case for first coefficient, no need to add second VLC table
709 UPDATE_CACHE(re, &s->gb);
710 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
711 level= (3*qscale*quant_matrix[0])>>5;
713 if(GET_CACHE(re, &s->gb)&0x40000000)
717 SKIP_BITS(re, &s->gb, 2);
718 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
721 /* now quantify & encode AC coefficients */
723 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
728 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
730 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
731 SKIP_BITS(re, &s->gb, 1);
734 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
735 UPDATE_CACHE(re, &s->gb);
736 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
738 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
739 } else if (level == 0) {
740 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
746 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
750 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
755 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
760 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
762 UPDATE_CACHE(re, &s->gb);
765 LAST_SKIP_BITS(re, &s->gb, 2);
766 CLOSE_READER(re, &s->gb);
768 s->block_last_index[n] = i;
772 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
774 int level, i, j, run;
775 RLTable *rl = &ff_rl_mpeg1;
776 uint8_t * const scantable= s->intra_scantable.permutated;
777 const int qscale= s->qscale;
780 OPEN_READER(re, &s->gb);
782 // special case for first coefficient, no need to add second VLC table
783 UPDATE_CACHE(re, &s->gb);
784 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
785 level= (3*qscale)>>1;
787 if(GET_CACHE(re, &s->gb)&0x40000000)
791 SKIP_BITS(re, &s->gb, 2);
792 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
796 /* now quantify & encode AC coefficients */
798 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
803 level= ((level*2+1)*qscale)>>1;
805 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
806 SKIP_BITS(re, &s->gb, 1);
809 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
810 UPDATE_CACHE(re, &s->gb);
811 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
813 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
814 } else if (level == 0) {
815 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
821 level= ((level*2+1)*qscale)>>1;
825 level= ((level*2+1)*qscale)>>1;
831 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
833 UPDATE_CACHE(re, &s->gb);
836 LAST_SKIP_BITS(re, &s->gb, 2);
837 CLOSE_READER(re, &s->gb);
839 s->block_last_index[n] = i;
844 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
848 int level, i, j, run;
849 RLTable *rl = &ff_rl_mpeg1;
850 uint8_t * const scantable= s->intra_scantable.permutated;
851 const uint16_t *quant_matrix;
852 const int qscale= s->qscale;
858 OPEN_READER(re, &s->gb);
861 quant_matrix = s->inter_matrix;
863 quant_matrix = s->chroma_inter_matrix;
865 // special case for first coefficient, no need to add second VLC table
866 UPDATE_CACHE(re, &s->gb);
867 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
868 level= (3*qscale*quant_matrix[0])>>5;
869 if(GET_CACHE(re, &s->gb)&0x40000000)
874 SKIP_BITS(re, &s->gb, 2);
875 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
879 /* now quantify & encode AC coefficients */
881 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
886 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
887 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
888 SKIP_BITS(re, &s->gb, 1);
891 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
892 UPDATE_CACHE(re, &s->gb);
893 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
898 level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
901 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
905 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
911 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
913 UPDATE_CACHE(re, &s->gb);
916 LAST_SKIP_BITS(re, &s->gb, 2);
917 CLOSE_READER(re, &s->gb);
919 block[63] ^= (mismatch & 1);
921 s->block_last_index[n] = i;
925 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
929 int level, i, j, run;
930 RLTable *rl = &ff_rl_mpeg1;
931 uint8_t * const scantable= s->intra_scantable.permutated;
932 const int qscale= s->qscale;
933 OPEN_READER(re, &s->gb);
936 // special case for first coefficient, no need to add second VLC table
937 UPDATE_CACHE(re, &s->gb);
938 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
939 level= (3*qscale)>>1;
940 if(GET_CACHE(re, &s->gb)&0x40000000)
944 SKIP_BITS(re, &s->gb, 2);
945 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
949 /* now quantify & encode AC coefficients */
951 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
956 level= ((level*2+1)*qscale)>>1;
957 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
958 SKIP_BITS(re, &s->gb, 1);
961 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
962 UPDATE_CACHE(re, &s->gb);
963 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
968 level= ((-level*2+1)*qscale)>>1;
971 level= ((level*2+1)*qscale)>>1;
976 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
978 UPDATE_CACHE(re, &s->gb);
981 LAST_SKIP_BITS(re, &s->gb, 2);
982 CLOSE_READER(re, &s->gb);
983 s->block_last_index[n] = i;
988 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
992 int level, dc, diff, i, j, run;
995 uint8_t * const scantable= s->intra_scantable.permutated;
996 const uint16_t *quant_matrix;
997 const int qscale= s->qscale;
1000 /* DC coefficient */
1002 quant_matrix = s->intra_matrix;
1005 quant_matrix = s->chroma_intra_matrix;
1006 component = (n&1) + 1;
1008 diff = decode_dc(&s->gb, component);
1011 dc = s->last_dc[component];
1013 s->last_dc[component] = dc;
1014 block[0] = dc << (3 - s->intra_dc_precision);
1015 av_dlog(s->avctx, "dc=%d\n", block[0]);
1016 mismatch = block[0] ^ 1;
1018 if (s->intra_vlc_format)
1024 OPEN_READER(re, &s->gb);
1025 /* now quantify & encode AC coefficients */
1027 UPDATE_CACHE(re, &s->gb);
1028 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1032 } else if(level != 0) {
1035 level= (level*qscale*quant_matrix[j])>>4;
1036 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1037 LAST_SKIP_BITS(re, &s->gb, 1);
1040 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1041 UPDATE_CACHE(re, &s->gb);
1042 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1046 level= (-level*qscale*quant_matrix[j])>>4;
1049 level= (level*qscale*quant_matrix[j])>>4;
1053 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1060 CLOSE_READER(re, &s->gb);
1062 block[63]^= mismatch&1;
1064 s->block_last_index[n] = i;
1068 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
1072 int level, dc, diff, j, run;
1075 uint8_t * scantable= s->intra_scantable.permutated;
1076 const uint16_t *quant_matrix;
1077 const int qscale= s->qscale;
1079 /* DC coefficient */
1081 quant_matrix = s->intra_matrix;
1084 quant_matrix = s->chroma_intra_matrix;
1085 component = (n&1) + 1;
1087 diff = decode_dc(&s->gb, component);
1090 dc = s->last_dc[component];
1092 s->last_dc[component] = dc;
1093 block[0] = dc << (3 - s->intra_dc_precision);
1094 if (s->intra_vlc_format)
1100 OPEN_READER(re, &s->gb);
1101 /* now quantify & encode AC coefficients */
1103 UPDATE_CACHE(re, &s->gb);
1104 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1108 } else if(level != 0) {
1111 level= (level*qscale*quant_matrix[j])>>4;
1112 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
1113 LAST_SKIP_BITS(re, &s->gb, 1);
1116 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
1117 UPDATE_CACHE(re, &s->gb);
1118 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
1122 level= (-level*qscale*quant_matrix[j])>>4;
1125 level= (level*qscale*quant_matrix[j])>>4;
1131 CLOSE_READER(re, &s->gb);
1134 s->block_last_index[n] = scantable - s->intra_scantable.permutated;
1138 typedef struct Mpeg1Context {
1139 MpegEncContext mpeg_enc_ctx;
1140 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1141 int repeat_field; /* true if we must repeat the field */
1142 AVPanScan pan_scan; /**< some temporary storage for the panscan */
1144 int swap_uv;//indicate VCR2
1145 int save_aspect_info;
1146 int save_width, save_height, save_progressive_seq;
1147 AVRational frame_rate_ext; ///< MPEG-2 specific framerate modificator
1148 int sync; ///< Did we reach a sync point like a GOP/SEQ/KEYFrame?
1151 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1153 Mpeg1Context *s = avctx->priv_data;
1154 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1157 /* we need some permutation to store matrices,
1158 * until MPV_common_init() sets the real permutation. */
1160 s2->dsp.idct_permutation[i]=i;
1162 MPV_decode_defaults(s2);
1164 s->mpeg_enc_ctx.avctx= avctx;
1165 s->mpeg_enc_ctx.flags= avctx->flags;
1166 s->mpeg_enc_ctx.flags2= avctx->flags2;
1167 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
1168 ff_mpeg12_init_vlcs();
1170 s->mpeg_enc_ctx_allocated = 0;
1171 s->mpeg_enc_ctx.picture_number = 0;
1172 s->repeat_field = 0;
1173 s->mpeg_enc_ctx.codec_id= avctx->codec->id;
1174 avctx->color_range= AVCOL_RANGE_MPEG;
1175 if (avctx->codec->id == CODEC_ID_MPEG1VIDEO)
1176 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
1178 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
1182 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
1183 const uint8_t *new_perm){
1184 uint16_t temp_matrix[64];
1187 memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
1190 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
1194 static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx){
1195 Mpeg1Context *s1 = avctx->priv_data;
1196 MpegEncContext *s = &s1->mpeg_enc_ctx;
1198 if(avctx->xvmc_acceleration)
1199 return avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
1200 else if(avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
1201 if(avctx->codec_id == CODEC_ID_MPEG1VIDEO)
1202 return PIX_FMT_VDPAU_MPEG1;
1204 return PIX_FMT_VDPAU_MPEG2;
1206 if(s->chroma_format < 2)
1207 return avctx->get_format(avctx,ff_hwaccel_pixfmt_list_420);
1208 else if(s->chroma_format == 2)
1209 return PIX_FMT_YUV422P;
1211 return PIX_FMT_YUV444P;
1215 /* Call this function when we know all parameters.
1216 * It may be called in different places for MPEG-1 and MPEG-2. */
1217 static int mpeg_decode_postinit(AVCodecContext *avctx){
1218 Mpeg1Context *s1 = avctx->priv_data;
1219 MpegEncContext *s = &s1->mpeg_enc_ctx;
1220 uint8_t old_permutation[64];
1223 (s1->mpeg_enc_ctx_allocated == 0)||
1224 avctx->coded_width != s->width ||
1225 avctx->coded_height != s->height||
1226 s1->save_width != s->width ||
1227 s1->save_height != s->height ||
1228 s1->save_aspect_info != s->aspect_ratio_info||
1229 s1->save_progressive_seq != s->progressive_sequence ||
1233 if (s1->mpeg_enc_ctx_allocated) {
1234 ParseContext pc= s->parse_context;
1235 s->parse_context.buffer=0;
1237 s->parse_context= pc;
1240 if( (s->width == 0 )||(s->height == 0))
1243 avcodec_set_dimensions(avctx, s->width, s->height);
1244 avctx->bit_rate = s->bit_rate;
1245 s1->save_aspect_info = s->aspect_ratio_info;
1246 s1->save_width = s->width;
1247 s1->save_height = s->height;
1248 s1->save_progressive_seq = s->progressive_sequence;
1250 /* low_delay may be forced, in this case we will have B-frames
1251 * that behave like P-frames. */
1252 avctx->has_b_frames = !(s->low_delay);
1254 assert((avctx->sub_id==1) == (avctx->codec_id==CODEC_ID_MPEG1VIDEO));
1255 if(avctx->codec_id==CODEC_ID_MPEG1VIDEO){
1257 avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
1258 avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
1260 avctx->sample_aspect_ratio= av_d2q(
1261 1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1262 avctx->ticks_per_frame=1;
1266 &s->avctx->time_base.den,
1267 &s->avctx->time_base.num,
1268 ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
1269 ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
1271 avctx->ticks_per_frame=2;
1273 if(s->aspect_ratio_info > 1){
1274 //we ignore the spec here as reality does not match the spec, see for example
1275 // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
1276 if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) || 1){
1277 s->avctx->sample_aspect_ratio=
1279 ff_mpeg2_aspect[s->aspect_ratio_info],
1280 (AVRational){s->width, s->height}
1283 s->avctx->sample_aspect_ratio=
1285 ff_mpeg2_aspect[s->aspect_ratio_info],
1286 (AVRational){s1->pan_scan.width, s1->pan_scan.height}
1290 s->avctx->sample_aspect_ratio=
1291 ff_mpeg2_aspect[s->aspect_ratio_info];
1295 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
1296 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
1297 //until then pix_fmt may be changed right after codec init
1298 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
1300 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU )
1301 if( avctx->idct_algo == FF_IDCT_AUTO )
1302 avctx->idct_algo = FF_IDCT_SIMPLE;
1304 /* Quantization matrices may need reordering
1305 * if DCT permutation is changed. */
1306 memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
1308 if (MPV_common_init(s) < 0)
1311 quant_matrix_rebuild(s->intra_matrix, old_permutation,s->dsp.idct_permutation);
1312 quant_matrix_rebuild(s->inter_matrix, old_permutation,s->dsp.idct_permutation);
1313 quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
1314 quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
1316 s1->mpeg_enc_ctx_allocated = 1;
1321 static int mpeg1_decode_picture(AVCodecContext *avctx,
1322 const uint8_t *buf, int buf_size)
1324 Mpeg1Context *s1 = avctx->priv_data;
1325 MpegEncContext *s = &s1->mpeg_enc_ctx;
1326 int ref, f_code, vbv_delay;
1328 init_get_bits(&s->gb, buf, buf_size*8);
1330 ref = get_bits(&s->gb, 10); /* temporal ref */
1331 s->pict_type = get_bits(&s->gb, 3);
1332 if(s->pict_type == 0 || s->pict_type > 3)
1335 vbv_delay= get_bits(&s->gb, 16);
1336 if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) {
1337 s->full_pel[0] = get_bits1(&s->gb);
1338 f_code = get_bits(&s->gb, 3);
1339 if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
1341 s->mpeg_f_code[0][0] = f_code;
1342 s->mpeg_f_code[0][1] = f_code;
1344 if (s->pict_type == FF_B_TYPE) {
1345 s->full_pel[1] = get_bits1(&s->gb);
1346 f_code = get_bits(&s->gb, 3);
1347 if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
1349 s->mpeg_f_code[1][0] = f_code;
1350 s->mpeg_f_code[1][1] = f_code;
1352 s->current_picture.pict_type= s->pict_type;
1353 s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
1355 if(avctx->debug & FF_DEBUG_PICT_INFO)
1356 av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
1363 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1365 MpegEncContext *s= &s1->mpeg_enc_ctx;
1366 int horiz_size_ext, vert_size_ext;
1369 skip_bits(&s->gb, 1); /* profile and level esc*/
1370 s->avctx->profile= get_bits(&s->gb, 3);
1371 s->avctx->level= get_bits(&s->gb, 4);
1372 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
1373 s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
1374 horiz_size_ext = get_bits(&s->gb, 2);
1375 vert_size_ext = get_bits(&s->gb, 2);
1376 s->width |= (horiz_size_ext << 12);
1377 s->height |= (vert_size_ext << 12);
1378 bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
1379 s->bit_rate += (bit_rate_ext << 18) * 400;
1380 skip_bits1(&s->gb); /* marker */
1381 s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
1383 s->low_delay = get_bits1(&s->gb);
1384 if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
1386 s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
1387 s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
1389 av_dlog(s->avctx, "sequence extension\n");
1390 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
1391 s->avctx->sub_id = 2; /* indicates MPEG-2 found */
1393 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1394 av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1395 s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
1399 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
1401 MpegEncContext *s= &s1->mpeg_enc_ctx;
1402 int color_description, w, h;
1404 skip_bits(&s->gb, 3); /* video format */
1405 color_description= get_bits1(&s->gb);
1406 if(color_description){
1407 s->avctx->color_primaries= get_bits(&s->gb, 8);
1408 s->avctx->color_trc = get_bits(&s->gb, 8);
1409 s->avctx->colorspace = get_bits(&s->gb, 8);
1411 w= get_bits(&s->gb, 14);
1412 skip_bits(&s->gb, 1); //marker
1413 h= get_bits(&s->gb, 14);
1414 // remaining 3 bits are zero padding
1416 s1->pan_scan.width= 16*w;
1417 s1->pan_scan.height=16*h;
1419 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1420 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
1423 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
1425 MpegEncContext *s= &s1->mpeg_enc_ctx;
1429 if(s->progressive_sequence){
1430 if(s->repeat_first_field){
1432 if(s->top_field_first)
1436 if(s->picture_structure == PICT_FRAME){
1438 if(s->repeat_first_field)
1442 for(i=0; i<nofco; i++){
1443 s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
1444 skip_bits(&s->gb, 1); //marker
1445 s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
1446 skip_bits(&s->gb, 1); //marker
1449 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
1450 av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
1451 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
1452 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
1453 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
1457 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra){
1460 for(i=0; i<64; i++) {
1461 int j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
1462 int v = get_bits(&s->gb, 8);
1464 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
1467 if(intra && i==0 && v!=8){
1468 av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
1469 v= 8; // needed by pink.mpg / issue1046
1478 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
1480 av_dlog(s->avctx, "matrix extension\n");
1482 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1483 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1484 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
1485 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
1488 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1490 MpegEncContext *s= &s1->mpeg_enc_ctx;
1492 s->full_pel[0] = s->full_pel[1] = 0;
1493 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1494 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1495 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
1496 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
1497 if(!s->pict_type && s1->mpeg_enc_ctx_allocated){
1498 av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
1499 if(s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1]==15){
1500 if(s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
1501 s->pict_type= FF_I_TYPE;
1503 s->pict_type= FF_P_TYPE;
1505 s->pict_type= FF_B_TYPE;
1506 s->current_picture.pict_type= s->pict_type;
1507 s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
1509 s->intra_dc_precision = get_bits(&s->gb, 2);
1510 s->picture_structure = get_bits(&s->gb, 2);
1511 s->top_field_first = get_bits1(&s->gb);
1512 s->frame_pred_frame_dct = get_bits1(&s->gb);
1513 s->concealment_motion_vectors = get_bits1(&s->gb);
1514 s->q_scale_type = get_bits1(&s->gb);
1515 s->intra_vlc_format = get_bits1(&s->gb);
1516 s->alternate_scan = get_bits1(&s->gb);
1517 s->repeat_first_field = get_bits1(&s->gb);
1518 s->chroma_420_type = get_bits1(&s->gb);
1519 s->progressive_frame = get_bits1(&s->gb);
1521 if(s->progressive_sequence && !s->progressive_frame){
1522 s->progressive_frame= 1;
1523 av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
1526 if(s->picture_structure==0 || (s->progressive_frame && s->picture_structure!=PICT_FRAME)){
1527 av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
1528 s->picture_structure= PICT_FRAME;
1531 if(s->progressive_sequence && !s->frame_pred_frame_dct){
1532 av_log(s->avctx, AV_LOG_ERROR, "invalid frame_pred_frame_dct\n");
1533 s->frame_pred_frame_dct= 1;
1536 if(s->picture_structure == PICT_FRAME){
1538 s->v_edge_pos= 16*s->mb_height;
1540 s->first_field ^= 1;
1541 s->v_edge_pos= 8*s->mb_height;
1542 memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
1545 if(s->alternate_scan){
1546 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
1547 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
1549 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
1550 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
1553 /* composite display not parsed */
1554 av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
1555 av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
1556 av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
1557 av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
1558 av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
1559 av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
1560 av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
1561 av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
1562 av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
1565 static void exchange_uv(MpegEncContext *s){
1568 tmp = s->pblocks[4];
1569 s->pblocks[4] = s->pblocks[5];
1570 s->pblocks[5] = tmp;
1573 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size){
1574 AVCodecContext *avctx= s->avctx;
1575 Mpeg1Context *s1 = (Mpeg1Context*)s;
1577 /* start frame decoding */
1578 if(s->first_field || s->picture_structure==PICT_FRAME){
1579 if(MPV_frame_start(s, avctx) < 0)
1582 ff_er_frame_start(s);
1584 /* first check if we must repeat the frame */
1585 s->current_picture_ptr->repeat_pict = 0;
1586 if (s->repeat_first_field) {
1587 if (s->progressive_sequence) {
1588 if (s->top_field_first)
1589 s->current_picture_ptr->repeat_pict = 4;
1591 s->current_picture_ptr->repeat_pict = 2;
1592 } else if (s->progressive_frame) {
1593 s->current_picture_ptr->repeat_pict = 1;
1597 *s->current_picture_ptr->pan_scan= s1->pan_scan;
1598 }else{ //second field
1601 if(!s->current_picture_ptr){
1602 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1607 s->current_picture.data[i] = s->current_picture_ptr->data[i];
1608 if(s->picture_structure == PICT_BOTTOM_FIELD){
1609 s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
1614 if (avctx->hwaccel) {
1615 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
1619 // MPV_frame_start will call this function too,
1620 // but we need to call it on every field
1621 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1622 if(ff_xvmc_field_start(s,avctx) < 0)
1628 #define DECODE_SLICE_ERROR -1
1629 #define DECODE_SLICE_OK 0
1632 * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
1633 * @return DECODE_SLICE_ERROR if the slice is damaged<br>
1634 * DECODE_SLICE_OK if this slice is ok<br>
1636 static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
1637 const uint8_t **buf, int buf_size)
1639 MpegEncContext *s = &s1->mpeg_enc_ctx;
1640 AVCodecContext *avctx= s->avctx;
1641 const int field_pic= s->picture_structure != PICT_FRAME;
1642 const int lowres= s->avctx->lowres;
1647 assert(mb_y < s->mb_height);
1649 init_get_bits(&s->gb, *buf, buf_size*8);
1651 ff_mpeg1_clean_buffers(s);
1652 s->interlaced_dct = 0;
1654 s->qscale = get_qscale(s);
1657 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1661 /* extra slice info */
1662 while (get_bits1(&s->gb) != 0) {
1663 skip_bits(&s->gb, 8);
1668 if(mb_y==0 && s->codec_tag == AV_RL32("SLIF")){
1672 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1674 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1681 /* otherwise, stuffing, nothing to do */
1689 if(s->mb_x >= (unsigned)s->mb_width){
1690 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1694 if (avctx->hwaccel) {
1695 const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
1696 int start_code = -1;
1697 buf_end = ff_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
1698 if (buf_end < *buf + buf_size)
1701 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
1702 return DECODE_SLICE_ERROR;
1704 return DECODE_SLICE_OK;
1707 s->resync_mb_x= s->mb_x;
1708 s->resync_mb_y= s->mb_y= mb_y;
1710 ff_init_block_index(s);
1712 if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
1713 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
1714 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",
1715 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],
1716 s->pict_type == FF_I_TYPE ? "I" : (s->pict_type == FF_P_TYPE ? "P" : (s->pict_type == FF_B_TYPE ? "B" : "S")),
1717 s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
1718 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
1719 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
1724 //If 1, we memcpy blocks in xvmcvideo.
1725 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1726 ff_xvmc_init_block(s);//set s->block
1728 if(mpeg_decode_mb(s, s->block) < 0)
1731 if(s->current_picture.motion_val[0] && !s->encoding){ //note motion_val is normally NULL unless we want to extract the MVs
1732 const int wrap = s->b8_stride;
1733 int xy = s->mb_x*2 + s->mb_y*2*wrap;
1734 int b8_xy= 4*(s->mb_x + s->mb_y*s->mb_stride);
1735 int motion_x, motion_y, dir, i;
1738 for(dir=0; dir<2; dir++){
1739 if (s->mb_intra || (dir==1 && s->pict_type != FF_B_TYPE)) {
1740 motion_x = motion_y = 0;
1741 }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
1742 motion_x = s->mv[dir][0][0];
1743 motion_y = s->mv[dir][0][1];
1744 } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
1745 motion_x = s->mv[dir][i][0];
1746 motion_y = s->mv[dir][i][1];
1749 s->current_picture.motion_val[dir][xy ][0] = motion_x;
1750 s->current_picture.motion_val[dir][xy ][1] = motion_y;
1751 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
1752 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
1753 s->current_picture.ref_index [dir][b8_xy ]=
1754 s->current_picture.ref_index [dir][b8_xy + 1]= s->field_select[dir][i];
1755 assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
1762 s->dest[0] += 16 >> lowres;
1763 s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
1764 s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
1766 MPV_decode_mb(s, s->block);
1768 if (++s->mb_x >= s->mb_width) {
1769 const int mb_size= 16>>s->avctx->lowres;
1771 ff_draw_horiz_band(s, mb_size*(s->mb_y>>field_pic), mb_size);
1774 s->mb_y += 1<<field_pic;
1776 if(s->mb_y >= s->mb_height){
1777 int left= get_bits_left(&s->gb);
1778 int is_d10= s->chroma_format==2 && s->pict_type==FF_I_TYPE && avctx->profile==0 && avctx->level==5
1779 && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
1780 && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
1782 if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
1783 || (avctx->error_recognition >= FF_ER_AGGRESSIVE && left>8)){
1784 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
1790 ff_init_block_index(s);
1793 /* skip mb handling */
1794 if (s->mb_skip_run == -1) {
1795 /* read increment again */
1798 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1800 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1805 s->mb_skip_run += 33;
1806 }else if(code == 35){
1807 if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
1808 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
1811 goto eos; /* end of slice */
1813 /* otherwise, stuffing, nothing to do */
1815 s->mb_skip_run += code;
1821 if(s->pict_type == FF_I_TYPE){
1822 av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
1829 s->block_last_index[i] = -1;
1830 if(s->picture_structure == PICT_FRAME)
1831 s->mv_type = MV_TYPE_16X16;
1833 s->mv_type = MV_TYPE_FIELD;
1834 if (s->pict_type == FF_P_TYPE) {
1835 /* if P type, zero motion vector is implied */
1836 s->mv_dir = MV_DIR_FORWARD;
1837 s->mv[0][0][0] = s->mv[0][0][1] = 0;
1838 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
1839 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
1840 s->field_select[0][0]= (s->picture_structure - 1) & 1;
1842 /* if B type, reuse previous vectors and directions */
1843 s->mv[0][0][0] = s->last_mv[0][0][0];
1844 s->mv[0][0][1] = s->last_mv[0][0][1];
1845 s->mv[1][0][0] = s->last_mv[1][0][0];
1846 s->mv[1][0][1] = s->last_mv[1][0][1];
1851 eos: // end of slice
1852 *buf += (get_bits_count(&s->gb)-1)/8;
1853 //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1857 static int slice_decode_thread(AVCodecContext *c, void *arg){
1858 MpegEncContext *s= *(void**)arg;
1859 const uint8_t *buf= s->gb.buffer;
1860 int mb_y= s->start_mb_y;
1861 const int field_pic= s->picture_structure != PICT_FRAME;
1863 s->error_count= (3*(s->end_mb_y - s->start_mb_y)*s->mb_width) >> field_pic;
1866 uint32_t start_code;
1869 ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
1871 //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1872 //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);
1874 if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
1875 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);
1877 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);
1880 if(s->mb_y == s->end_mb_y)
1884 buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
1885 mb_y= start_code - SLICE_MIN_START_CODE;
1886 if(mb_y < 0 || mb_y >= s->end_mb_y)
1890 return 0; //not reached
1894 * Handle slice ends.
1895 * @return 1 if it seems to be the last slice
1897 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
1899 Mpeg1Context *s1 = avctx->priv_data;
1900 MpegEncContext *s = &s1->mpeg_enc_ctx;
1902 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
1905 if (s->avctx->hwaccel) {
1906 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
1907 av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
1910 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1911 ff_xvmc_field_end(s);
1913 /* end of slice reached */
1914 if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
1917 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
1923 if (s->pict_type == FF_B_TYPE || s->low_delay) {
1924 *pict= *(AVFrame*)s->current_picture_ptr;
1925 ff_print_debug_info(s, pict);
1927 s->picture_number++;
1928 /* latency of 1 frame for I- and P-frames */
1929 /* XXX: use another variable than picture_number */
1930 if (s->last_picture_ptr != NULL) {
1931 *pict= *(AVFrame*)s->last_picture_ptr;
1932 ff_print_debug_info(s, pict);
1942 static int mpeg1_decode_sequence(AVCodecContext *avctx,
1943 const uint8_t *buf, int buf_size)
1945 Mpeg1Context *s1 = avctx->priv_data;
1946 MpegEncContext *s = &s1->mpeg_enc_ctx;
1950 init_get_bits(&s->gb, buf, buf_size*8);
1952 width = get_bits(&s->gb, 12);
1953 height = get_bits(&s->gb, 12);
1954 if (width <= 0 || height <= 0)
1956 s->aspect_ratio_info= get_bits(&s->gb, 4);
1957 if (s->aspect_ratio_info == 0) {
1958 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
1959 if (avctx->error_recognition >= FF_ER_COMPLIANT)
1962 s->frame_rate_index = get_bits(&s->gb, 4);
1963 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
1965 s->bit_rate = get_bits(&s->gb, 18) * 400;
1966 if (get_bits1(&s->gb) == 0) /* marker */
1971 s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
1972 skip_bits(&s->gb, 1);
1975 if (get_bits1(&s->gb)) {
1976 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
1979 j = s->dsp.idct_permutation[i];
1980 v = ff_mpeg1_default_intra_matrix[i];
1981 s->intra_matrix[j] = v;
1982 s->chroma_intra_matrix[j] = v;
1985 if (get_bits1(&s->gb)) {
1986 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
1989 int j= s->dsp.idct_permutation[i];
1990 v = ff_mpeg1_default_non_intra_matrix[i];
1991 s->inter_matrix[j] = v;
1992 s->chroma_inter_matrix[j] = v;
1996 if(show_bits(&s->gb, 23) != 0){
1997 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
2001 /* we set MPEG-2 parameters so that it emulates MPEG-1 */
2002 s->progressive_sequence = 1;
2003 s->progressive_frame = 1;
2004 s->picture_structure = PICT_FRAME;
2005 s->frame_pred_frame_dct = 1;
2006 s->chroma_format = 1;
2007 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
2008 avctx->sub_id = 1; /* indicates MPEG-1 */
2009 s->out_format = FMT_MPEG1;
2010 s->swap_uv = 0;//AFAIK VCR2 does not have SEQ_HEADER
2011 if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
2013 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2014 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
2015 s->avctx->rc_buffer_size, s->bit_rate);
2020 static int vcr2_init_sequence(AVCodecContext *avctx)
2022 Mpeg1Context *s1 = avctx->priv_data;
2023 MpegEncContext *s = &s1->mpeg_enc_ctx;
2026 /* start new MPEG-1 context decoding */
2027 s->out_format = FMT_MPEG1;
2028 if (s1->mpeg_enc_ctx_allocated) {
2031 s->width = avctx->coded_width;
2032 s->height = avctx->coded_height;
2033 avctx->has_b_frames= 0; //true?
2036 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
2037 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
2039 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel ||
2040 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU )
2041 if( avctx->idct_algo == FF_IDCT_AUTO )
2042 avctx->idct_algo = FF_IDCT_SIMPLE;
2044 if (MPV_common_init(s) < 0)
2046 exchange_uv(s);//common init reset pblocks, so we swap them here
2047 s->swap_uv = 1;// in case of xvmc we need to swap uv for each MB
2048 s1->mpeg_enc_ctx_allocated = 1;
2051 int j= s->dsp.idct_permutation[i];
2052 v = ff_mpeg1_default_intra_matrix[i];
2053 s->intra_matrix[j] = v;
2054 s->chroma_intra_matrix[j] = v;
2056 v = ff_mpeg1_default_non_intra_matrix[i];
2057 s->inter_matrix[j] = v;
2058 s->chroma_inter_matrix[j] = v;
2061 s->progressive_sequence = 1;
2062 s->progressive_frame = 1;
2063 s->picture_structure = PICT_FRAME;
2064 s->frame_pred_frame_dct = 1;
2065 s->chroma_format = 1;
2066 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
2067 avctx->sub_id = 2; /* indicates MPEG-2 */
2068 s1->save_width = s->width;
2069 s1->save_height = s->height;
2070 s1->save_progressive_seq = s->progressive_sequence;
2075 static void mpeg_decode_user_data(AVCodecContext *avctx,
2076 const uint8_t *p, int buf_size)
2078 const uint8_t *buf_end = p+buf_size;
2080 /* we parse the DTG active format information */
2081 if (buf_end - p >= 5 &&
2082 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2090 if (buf_end - p < 1)
2092 avctx->dtg_active_format = p[0] & 0x0f;
2097 static void mpeg_decode_gop(AVCodecContext *avctx,
2098 const uint8_t *buf, int buf_size){
2099 Mpeg1Context *s1 = avctx->priv_data;
2100 MpegEncContext *s = &s1->mpeg_enc_ctx;
2102 int drop_frame_flag;
2103 int time_code_hours, time_code_minutes;
2104 int time_code_seconds, time_code_pictures;
2107 init_get_bits(&s->gb, buf, buf_size*8);
2109 drop_frame_flag = get_bits1(&s->gb);
2111 time_code_hours=get_bits(&s->gb,5);
2112 time_code_minutes = get_bits(&s->gb,6);
2113 skip_bits1(&s->gb);//marker bit
2114 time_code_seconds = get_bits(&s->gb,6);
2115 time_code_pictures = get_bits(&s->gb,6);
2117 s->closed_gop = get_bits1(&s->gb);
2118 /*broken_link indicate that after editing the
2119 reference frames of the first B-Frames after GOP I-Frame
2120 are missing (open gop)*/
2121 broken_link = get_bits1(&s->gb);
2123 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
2124 av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
2125 time_code_hours, time_code_minutes, time_code_seconds,
2126 time_code_pictures, s->closed_gop, broken_link);
2129 * Find the end of the current frame in the bitstream.
2130 * @return the position of the first byte of the next frame, or -1
2132 int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
2135 uint32_t state= pc->state;
2137 /* EOF considered as end of frame */
2142 0 frame start -> 1/4
2143 1 first_SEQEXT -> 0/2
2144 2 first field start -> 3/0
2145 3 second_SEQEXT -> 2/0
2149 for(i=0; i<buf_size; i++){
2150 assert(pc->frame_start_found>=0 && pc->frame_start_found<=4);
2151 if(pc->frame_start_found&1){
2152 if(state == EXT_START_CODE && (buf[i]&0xF0) != 0x80)
2153 pc->frame_start_found--;
2154 else if(state == EXT_START_CODE+2){
2155 if((buf[i]&3) == 3) pc->frame_start_found= 0;
2156 else pc->frame_start_found= (pc->frame_start_found+1)&3;
2160 i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
2161 if(pc->frame_start_found==0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
2163 pc->frame_start_found=4;
2165 if(state == SEQ_END_CODE){
2169 if(pc->frame_start_found==2 && state == SEQ_START_CODE)
2170 pc->frame_start_found= 0;
2171 if(pc->frame_start_found<4 && state == EXT_START_CODE)
2172 pc->frame_start_found++;
2173 if(pc->frame_start_found == 4 && (state&0xFFFFFF00) == 0x100){
2174 if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
2175 pc->frame_start_found=0;
2180 if(pc->frame_start_found == 0 && s && state == PICTURE_START_CODE){
2181 ff_fetch_timestamp(s, i-3, 1);
2186 return END_NOT_FOUND;
2189 static int decode_chunks(AVCodecContext *avctx,
2190 AVFrame *picture, int *data_size,
2191 const uint8_t *buf, int buf_size);
2193 /* handle buffering and image synchronisation */
2194 static int mpeg_decode_frame(AVCodecContext *avctx,
2195 void *data, int *data_size,
2198 const uint8_t *buf = avpkt->data;
2199 int buf_size = avpkt->size;
2200 Mpeg1Context *s = avctx->priv_data;
2201 AVFrame *picture = data;
2202 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2203 av_dlog(avctx, "fill_buffer\n");
2205 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
2206 /* special case for last picture */
2207 if (s2->low_delay==0 && s2->next_picture_ptr) {
2208 *picture= *(AVFrame*)s2->next_picture_ptr;
2209 s2->next_picture_ptr= NULL;
2211 *data_size = sizeof(AVFrame);
2216 if(s2->flags&CODEC_FLAG_TRUNCATED){
2217 int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
2219 if( ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
2224 if (s->repeat_field % 2 == 1) {
2226 //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
2227 // s2->picture_number, s->repeat_field);
2228 if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
2229 *data_size = sizeof(AVPicture);
2235 if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == AV_RL32("VCR2"))
2236 vcr2_init_sequence(avctx);
2240 if(avctx->extradata && !avctx->frame_number)
2241 decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
2243 return decode_chunks(avctx, picture, data_size, buf, buf_size);
2246 static int decode_chunks(AVCodecContext *avctx,
2247 AVFrame *picture, int *data_size,
2248 const uint8_t *buf, int buf_size)
2250 Mpeg1Context *s = avctx->priv_data;
2251 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2252 const uint8_t *buf_ptr = buf;
2253 const uint8_t *buf_end = buf + buf_size;
2254 int ret, input_size;
2258 /* find next start code */
2259 uint32_t start_code = -1;
2260 buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
2261 if (start_code > 0x1ff){
2262 if(s2->pict_type != FF_B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){
2263 if(avctx->thread_count > 1){
2266 avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
2267 for(i=0; i<s->slice_count; i++)
2268 s2->error_count += s2->thread_context[i]->error_count;
2271 if (CONFIG_MPEG_VDPAU_DECODER && avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
2272 ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
2274 if (slice_end(avctx, picture)) {
2275 if(s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
2276 *data_size = sizeof(AVPicture);
2280 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
2283 input_size = buf_end - buf_ptr;
2285 if(avctx->debug & FF_DEBUG_STARTCODE){
2286 av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
2289 /* prepare data for next start code */
2290 switch(start_code) {
2291 case SEQ_START_CODE:
2293 mpeg1_decode_sequence(avctx, buf_ptr,
2297 av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
2301 case PICTURE_START_CODE:
2302 if(last_code == 0 || last_code == SLICE_MIN_START_CODE){
2303 if(mpeg_decode_postinit(avctx) < 0){
2304 av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
2308 /* we have a complete image: we try to decompress it */
2309 if(mpeg1_decode_picture(avctx,
2310 buf_ptr, input_size) < 0)
2312 s2->first_slice = 1;
2313 last_code= PICTURE_START_CODE;
2315 av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
2318 case EXT_START_CODE:
2319 init_get_bits(&s2->gb, buf_ptr, input_size*8);
2321 switch(get_bits(&s2->gb, 4)) {
2324 mpeg_decode_sequence_extension(s);
2326 av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
2330 mpeg_decode_sequence_display_extension(s);
2333 mpeg_decode_quant_matrix_extension(s2);
2336 mpeg_decode_picture_display_extension(s);
2339 if(last_code == PICTURE_START_CODE){
2340 mpeg_decode_picture_coding_extension(s);
2342 av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
2347 case USER_START_CODE:
2348 mpeg_decode_user_data(avctx,
2349 buf_ptr, input_size);
2351 case GOP_START_CODE:
2354 mpeg_decode_gop(avctx,
2355 buf_ptr, input_size);
2358 av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
2362 if (start_code >= SLICE_MIN_START_CODE &&
2363 start_code <= SLICE_MAX_START_CODE && last_code!=0) {
2364 const int field_pic= s2->picture_structure != PICT_FRAME;
2365 int mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
2366 last_code= SLICE_MIN_START_CODE;
2368 if(s2->picture_structure == PICT_BOTTOM_FIELD)
2371 if (mb_y >= s2->mb_height){
2372 av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
2376 if(s2->last_picture_ptr==NULL){
2377 /* Skip B-frames if we do not have reference frames and gop is not closed */
2378 if(s2->pict_type==FF_B_TYPE){
2383 if(s2->pict_type==FF_I_TYPE)
2385 if(s2->next_picture_ptr==NULL){
2386 /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
2387 if(s2->pict_type==FF_P_TYPE && !s->sync) break;
2389 /* Skip B-frames if we are in a hurry. */
2390 if(avctx->hurry_up && s2->pict_type==FF_B_TYPE) break;
2391 if( (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==FF_B_TYPE)
2392 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=FF_I_TYPE)
2393 || avctx->skip_frame >= AVDISCARD_ALL)
2395 /* Skip everything if we are in a hurry>=5. */
2396 if(avctx->hurry_up>=5) break;
2398 if (!s->mpeg_enc_ctx_allocated) break;
2400 if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
2401 if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
2406 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2410 if(s2->first_slice){
2412 if(mpeg_field_start(s2, buf, buf_size) < 0)
2415 if(!s2->current_picture_ptr){
2416 av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
2420 if (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
2425 if(avctx->thread_count > 1){
2426 int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
2427 if(threshold <= mb_y){
2428 MpegEncContext *thread_context= s2->thread_context[s->slice_count];
2430 thread_context->start_mb_y= mb_y;
2431 thread_context->end_mb_y = s2->mb_height;
2433 s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
2434 ff_update_duplicate_context(thread_context, s2);
2436 init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
2439 buf_ptr += 2; //FIXME add minimum number of bytes per slice
2441 ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
2445 if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
2446 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);
2448 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);
2457 static void flush(AVCodecContext *avctx){
2458 Mpeg1Context *s = avctx->priv_data;
2462 ff_mpeg_flush(avctx);
2465 static int mpeg_decode_end(AVCodecContext *avctx)
2467 Mpeg1Context *s = avctx->priv_data;
2469 if (s->mpeg_enc_ctx_allocated)
2470 MPV_common_end(&s->mpeg_enc_ctx);
2474 AVCodec ff_mpeg1video_decoder = {
2477 CODEC_ID_MPEG1VIDEO,
2478 sizeof(Mpeg1Context),
2483 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
2486 .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2489 AVCodec ff_mpeg2video_decoder = {
2492 CODEC_ID_MPEG2VIDEO,
2493 sizeof(Mpeg1Context),
2498 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
2501 .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2505 AVCodec ff_mpegvideo_decoder = {
2508 CODEC_ID_MPEG2VIDEO,
2509 sizeof(Mpeg1Context),
2514 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
2517 .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2520 #if CONFIG_MPEG_XVMC_DECODER
2521 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx){
2522 if( avctx->thread_count > 1)
2524 if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
2526 if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
2527 av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2529 mpeg_decode_init(avctx);
2531 avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
2532 avctx->xvmc_acceleration = 2;//2 - the blocks are packed!
2537 AVCodec ff_mpeg_xvmc_decoder = {
2540 CODEC_ID_MPEG2VIDEO_XVMC,
2541 sizeof(Mpeg1Context),
2542 mpeg_mc_decode_init,
2546 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
2548 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
2553 #if CONFIG_MPEG_VDPAU_DECODER
2554 AVCodec ff_mpeg_vdpau_decoder = {
2557 CODEC_ID_MPEG2VIDEO,
2558 sizeof(Mpeg1Context),
2563 CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2565 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
2569 #if CONFIG_MPEG1_VDPAU_DECODER
2570 AVCodec ff_mpeg1_vdpau_decoder = {
2573 CODEC_ID_MPEG1VIDEO,
2574 sizeof(Mpeg1Context),
2579 CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
2581 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),