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
31 #include "mpegvideo.h"
34 #include "mpeg12data.h"
35 #include "mpeg12decdata.h"
36 #include "bytestream.h"
44 #define MBINCR_VLC_BITS 9
45 #define MB_PAT_VLC_BITS 9
46 #define MB_PTYPE_VLC_BITS 6
47 #define MB_BTYPE_VLC_BITS 6
48 #define TEX_VLC_BITS 9
50 static inline int mpeg1_decode_block_inter(MpegEncContext
*s
,
53 static inline int mpeg1_decode_block_intra(MpegEncContext
*s
,
56 static inline int mpeg1_fast_decode_block_inter(MpegEncContext
*s
, DCTELEM
*block
, int n
);
57 static inline int mpeg2_decode_block_non_intra(MpegEncContext
*s
,
60 static inline int mpeg2_decode_block_intra(MpegEncContext
*s
,
63 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext
*s
, DCTELEM
*block
, int n
);
64 static inline int mpeg2_fast_decode_block_intra(MpegEncContext
*s
, DCTELEM
*block
, int n
);
65 static int mpeg_decode_motion(MpegEncContext
*s
, int fcode
, int pred
);
66 static void exchange_uv(MpegEncContext
*s
);
68 extern int XVMC_field_start(MpegEncContext
*s
, AVCodecContext
*avctx
);
69 extern int XVMC_field_end(MpegEncContext
*s
);
70 extern void XVMC_pack_pblocks(MpegEncContext
*s
,int cbp
);
71 extern void XVMC_init_block(MpegEncContext
*s
);//set s->block
73 static const enum PixelFormat pixfmt_yuv_420
[]= {PIX_FMT_YUV420P
,PIX_FMT_NONE
};
74 static const enum PixelFormat pixfmt_yuv_422
[]= {PIX_FMT_YUV422P
,PIX_FMT_NONE
};
75 static const enum PixelFormat pixfmt_yuv_444
[]= {PIX_FMT_YUV444P
,PIX_FMT_NONE
};
76 static const enum PixelFormat pixfmt_xvmc_mpg2_420
[] = {
77 PIX_FMT_XVMC_MPEG2_IDCT
,
78 PIX_FMT_XVMC_MPEG2_MC
,
81 uint8_t ff_mpeg12_static_rl_table_store
[2][2][2*MAX_RUN
+ MAX_LEVEL
+ 3];
83 static void init_2d_vlc_rl(RLTable
*rl
)
87 init_vlc(&rl
->vlc
, TEX_VLC_BITS
, rl
->n
+ 2,
88 &rl
->table_vlc
[0][1], 4, 2,
89 &rl
->table_vlc
[0][0], 4, 2, 1);
91 rl
->rl_vlc
[0]= av_mallocz_static(rl
->vlc
.table_size
*sizeof(RL_VLC_ELEM
));
93 for(i
=0; i
<rl
->vlc
.table_size
; i
++){
94 int code
= rl
->vlc
.table
[i
][0];
95 int len
= rl
->vlc
.table
[i
][1];
98 if(len
==0){ // illegal code
101 }else if(len
<0){ //more bits needed
105 if(code
==rl
->n
){ //esc
108 }else if(code
==rl
->n
+1){ //eob
112 run
= rl
->table_run
[code
] + 1;
113 level
= rl
->table_level
[code
];
116 rl
->rl_vlc
[0][i
].len
= len
;
117 rl
->rl_vlc
[0][i
].level
= level
;
118 rl
->rl_vlc
[0][i
].run
= run
;
122 void ff_mpeg12_common_init(MpegEncContext
*s
)
126 s
->c_dc_scale_table
= mpeg2_dc_scale_table
[s
->intra_dc_precision
];
130 void ff_mpeg1_clean_buffers(MpegEncContext
*s
){
131 s
->last_dc
[0] = 1 << (7 + s
->intra_dc_precision
);
132 s
->last_dc
[1] = s
->last_dc
[0];
133 s
->last_dc
[2] = s
->last_dc
[0];
134 memset(s
->last_mv
, 0, sizeof(s
->last_mv
));
138 /******************************************/
141 static VLC dc_lum_vlc
;
142 static VLC 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 static void init_vlcs(void)
156 INIT_VLC_STATIC(&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(&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
);
182 init_2d_vlc_rl(&ff_rl_mpeg2
);
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 mpeg2) */
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 dprintf(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 dprintf(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 a 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 */
308 //one 1 we memcpy blocks in xvmcvideo
309 if(s
->avctx
->xvmc_acceleration
> 1){
310 XVMC_pack_pblocks(s
,-1);//inter are always full blocks
317 if (s
->codec_id
== CODEC_ID_MPEG2VIDEO
) {
318 if(s
->flags2
& CODEC_FLAG2_FAST
){
320 mpeg2_fast_decode_block_intra(s
, s
->pblocks
[i
], i
);
323 for(i
=0;i
<mb_block_count
;i
++) {
324 if (mpeg2_decode_block_intra(s
, s
->pblocks
[i
], i
) < 0)
330 if (mpeg1_decode_block_intra(s
, s
->pblocks
[i
], i
) < 0)
335 if (mb_type
& MB_TYPE_ZERO_MV
){
336 assert(mb_type
& MB_TYPE_CBP
);
338 s
->mv_dir
= MV_DIR_FORWARD
;
339 if(s
->picture_structure
== PICT_FRAME
){
340 if(!s
->frame_pred_frame_dct
)
341 s
->interlaced_dct
= get_bits1(&s
->gb
);
342 s
->mv_type
= MV_TYPE_16X16
;
344 s
->mv_type
= MV_TYPE_FIELD
;
345 mb_type
|= MB_TYPE_INTERLACED
;
346 s
->field_select
[0][0]= s
->picture_structure
- 1;
349 if (IS_QUANT(mb_type
))
350 s
->qscale
= get_qscale(s
);
352 s
->last_mv
[0][0][0] = 0;
353 s
->last_mv
[0][0][1] = 0;
354 s
->last_mv
[0][1][0] = 0;
355 s
->last_mv
[0][1][1] = 0;
359 assert(mb_type
& MB_TYPE_L0L1
);
360 //FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
361 /* get additional motion vector type */
362 if (s
->frame_pred_frame_dct
)
363 motion_type
= MT_FRAME
;
365 motion_type
= get_bits(&s
->gb
, 2);
366 if (s
->picture_structure
== PICT_FRAME
&& HAS_CBP(mb_type
))
367 s
->interlaced_dct
= get_bits1(&s
->gb
);
370 if (IS_QUANT(mb_type
))
371 s
->qscale
= get_qscale(s
);
374 s
->mv_dir
= (mb_type
>>13)&3;
375 dprintf(s
->avctx
, "motion_type=%d\n", motion_type
);
376 switch(motion_type
) {
377 case MT_FRAME
: /* or MT_16X8 */
378 if (s
->picture_structure
== PICT_FRAME
) {
379 mb_type
|= MB_TYPE_16x16
;
380 s
->mv_type
= MV_TYPE_16X16
;
382 if (USES_LIST(mb_type
, i
)) {
384 s
->mv
[i
][0][0]= s
->last_mv
[i
][0][0]= s
->last_mv
[i
][1][0] =
385 mpeg_decode_motion(s
, s
->mpeg_f_code
[i
][0], s
->last_mv
[i
][0][0]);
386 s
->mv
[i
][0][1]= s
->last_mv
[i
][0][1]= s
->last_mv
[i
][1][1] =
387 mpeg_decode_motion(s
, s
->mpeg_f_code
[i
][1], s
->last_mv
[i
][0][1]);
388 /* full_pel: only for mpeg1 */
390 s
->mv
[i
][0][0] <<= 1;
391 s
->mv
[i
][0][1] <<= 1;
396 mb_type
|= MB_TYPE_16x8
| MB_TYPE_INTERLACED
;
397 s
->mv_type
= MV_TYPE_16X8
;
399 if (USES_LIST(mb_type
, i
)) {
402 s
->field_select
[i
][j
] = get_bits1(&s
->gb
);
404 val
= mpeg_decode_motion(s
, s
->mpeg_f_code
[i
][k
],
405 s
->last_mv
[i
][j
][k
]);
406 s
->last_mv
[i
][j
][k
] = val
;
407 s
->mv
[i
][j
][k
] = val
;
415 s
->mv_type
= MV_TYPE_FIELD
;
416 if (s
->picture_structure
== PICT_FRAME
) {
417 mb_type
|= MB_TYPE_16x8
| MB_TYPE_INTERLACED
;
419 if (USES_LIST(mb_type
, i
)) {
421 s
->field_select
[i
][j
] = get_bits1(&s
->gb
);
422 val
= mpeg_decode_motion(s
, s
->mpeg_f_code
[i
][0],
423 s
->last_mv
[i
][j
][0]);
424 s
->last_mv
[i
][j
][0] = val
;
425 s
->mv
[i
][j
][0] = val
;
426 dprintf(s
->avctx
, "fmx=%d\n", val
);
427 val
= mpeg_decode_motion(s
, s
->mpeg_f_code
[i
][1],
428 s
->last_mv
[i
][j
][1] >> 1);
429 s
->last_mv
[i
][j
][1] = val
<< 1;
430 s
->mv
[i
][j
][1] = val
;
431 dprintf(s
->avctx
, "fmy=%d\n", val
);
436 mb_type
|= MB_TYPE_16x16
| MB_TYPE_INTERLACED
;
438 if (USES_LIST(mb_type
, i
)) {
439 s
->field_select
[i
][0] = get_bits1(&s
->gb
);
441 val
= mpeg_decode_motion(s
, s
->mpeg_f_code
[i
][k
],
442 s
->last_mv
[i
][0][k
]);
443 s
->last_mv
[i
][0][k
] = val
;
444 s
->last_mv
[i
][1][k
] = val
;
445 s
->mv
[i
][0][k
] = val
;
452 s
->mv_type
= MV_TYPE_DMV
;
454 if (USES_LIST(mb_type
, i
)) {
455 int dmx
, dmy
, mx
, my
, m
;
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] >> 1);
466 s
->last_mv
[i
][0][1] = my
<<1;
467 s
->last_mv
[i
][1][1] = my
<<1;
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
);
521 //on 1 we memcpy blocks in xvmcvideo
522 if(s
->avctx
->xvmc_acceleration
> 1){
523 XVMC_pack_pblocks(s
,cbp
);
530 if (s
->codec_id
== CODEC_ID_MPEG2VIDEO
) {
531 if(s
->flags2
& CODEC_FLAG2_FAST
){
534 mpeg2_fast_decode_block_non_intra(s
, s
->pblocks
[i
], i
);
536 s
->block_last_index
[i
] = -1;
541 cbp
<<= 12-mb_block_count
;
543 for(i
=0;i
<mb_block_count
;i
++) {
544 if ( cbp
& (1<<11) ) {
545 if (mpeg2_decode_block_non_intra(s
, s
->pblocks
[i
], i
) < 0)
548 s
->block_last_index
[i
] = -1;
554 if(s
->flags2
& CODEC_FLAG2_FAST
){
557 mpeg1_fast_decode_block_inter(s
, s
->pblocks
[i
], i
);
559 s
->block_last_index
[i
] = -1;
566 if (mpeg1_decode_block_inter(s
, s
->pblocks
[i
], i
) < 0)
569 s
->block_last_index
[i
] = -1;
577 s
->block_last_index
[i
] = -1;
581 s
->current_picture
.mb_type
[ s
->mb_x
+ s
->mb_y
*s
->mb_stride
]= mb_type
;
586 /* as h263, but only 17 codes */
587 static int mpeg_decode_motion(MpegEncContext
*s
, int fcode
, int pred
)
589 int code
, sign
, val
, l
, shift
;
591 code
= get_vlc2(&s
->gb
, mv_vlc
.table
, MV_VLC_BITS
, 2);
599 sign
= get_bits1(&s
->gb
);
603 val
= (val
- 1) << shift
;
604 val
|= get_bits(&s
->gb
, shift
);
611 /* modulo decoding */
612 l
= INT_BIT
- 5 - shift
;
617 static inline int decode_dc(GetBitContext
*gb
, int component
)
621 if (component
== 0) {
622 code
= get_vlc2(gb
, dc_lum_vlc
.table
, DC_VLC_BITS
, 2);
624 code
= get_vlc2(gb
, dc_chroma_vlc
.table
, DC_VLC_BITS
, 2);
627 av_log(NULL
, AV_LOG_ERROR
, "invalid dc code at\n");
633 diff
= get_xbits(gb
, code
);
638 static inline int mpeg1_decode_block_intra(MpegEncContext
*s
,
642 int level
, dc
, diff
, i
, j
, run
;
644 RLTable
*rl
= &ff_rl_mpeg1
;
645 uint8_t * const scantable
= s
->intra_scantable
.permutated
;
646 const uint16_t *quant_matrix
= s
->intra_matrix
;
647 const int qscale
= s
->qscale
;
650 component
= (n
<= 3 ?
0 : n
- 4 + 1);
651 diff
= decode_dc(&s
->gb
, component
);
654 dc
= s
->last_dc
[component
];
656 s
->last_dc
[component
] = dc
;
658 dprintf(s
->avctx
, "dc=%d diff=%d\n", dc
, diff
);
661 OPEN_READER(re
, &s
->gb
);
662 /* now quantify & encode AC coefs */
664 UPDATE_CACHE(re
, &s
->gb
);
665 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl
->rl_vlc
[0], TEX_VLC_BITS
, 2, 0);
669 } else if(level
!= 0) {
672 level
= (level
*qscale
*quant_matrix
[j
])>>4;
674 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
675 LAST_SKIP_BITS(re
, &s
->gb
, 1);
678 run
= SHOW_UBITS(re
, &s
->gb
, 6)+1; LAST_SKIP_BITS(re
, &s
->gb
, 6);
679 UPDATE_CACHE(re
, &s
->gb
);
680 level
= SHOW_SBITS(re
, &s
->gb
, 8); SKIP_BITS(re
, &s
->gb
, 8);
682 level
= SHOW_UBITS(re
, &s
->gb
, 8) - 256; LAST_SKIP_BITS(re
, &s
->gb
, 8);
683 } else if (level
== 0) {
684 level
= SHOW_UBITS(re
, &s
->gb
, 8) ; LAST_SKIP_BITS(re
, &s
->gb
, 8);
690 level
= (level
*qscale
*quant_matrix
[j
])>>4;
694 level
= (level
*qscale
*quant_matrix
[j
])>>4;
699 av_log(s
->avctx
, AV_LOG_ERROR
, "ac-tex damaged at %d %d\n", s
->mb_x
, s
->mb_y
);
705 CLOSE_READER(re
, &s
->gb
);
707 s
->block_last_index
[n
] = i
;
711 static inline int mpeg1_decode_block_inter(MpegEncContext
*s
,
715 int level
, i
, j
, run
;
716 RLTable
*rl
= &ff_rl_mpeg1
;
717 uint8_t * const scantable
= s
->intra_scantable
.permutated
;
718 const uint16_t *quant_matrix
= s
->inter_matrix
;
719 const int qscale
= s
->qscale
;
722 OPEN_READER(re
, &s
->gb
);
724 /* special case for the first coef. no need to add a second vlc table */
725 UPDATE_CACHE(re
, &s
->gb
);
726 if (((int32_t)GET_CACHE(re
, &s
->gb
)) < 0) {
727 level
= (3*qscale
*quant_matrix
[0])>>5;
729 if(GET_CACHE(re
, &s
->gb
)&0x40000000)
733 SKIP_BITS(re
, &s
->gb
, 2);
734 if(((int32_t)GET_CACHE(re
, &s
->gb
)) <= (int32_t)0xBFFFFFFF)
737 #if MIN_CACHE_BITS < 19
738 UPDATE_CACHE(re
, &s
->gb
);
740 /* now quantify & encode AC coefs */
742 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl
->rl_vlc
[0], TEX_VLC_BITS
, 2, 0);
747 level
= ((level
*2+1)*qscale
*quant_matrix
[j
])>>5;
749 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
750 SKIP_BITS(re
, &s
->gb
, 1);
753 run
= SHOW_UBITS(re
, &s
->gb
, 6)+1; LAST_SKIP_BITS(re
, &s
->gb
, 6);
754 UPDATE_CACHE(re
, &s
->gb
);
755 level
= SHOW_SBITS(re
, &s
->gb
, 8); SKIP_BITS(re
, &s
->gb
, 8);
757 level
= SHOW_UBITS(re
, &s
->gb
, 8) - 256; SKIP_BITS(re
, &s
->gb
, 8);
758 } else if (level
== 0) {
759 level
= SHOW_UBITS(re
, &s
->gb
, 8) ; SKIP_BITS(re
, &s
->gb
, 8);
765 level
= ((level
*2+1)*qscale
*quant_matrix
[j
])>>5;
769 level
= ((level
*2+1)*qscale
*quant_matrix
[j
])>>5;
774 av_log(s
->avctx
, AV_LOG_ERROR
, "ac-tex damaged at %d %d\n", s
->mb_x
, s
->mb_y
);
779 #if MIN_CACHE_BITS < 19
780 UPDATE_CACHE(re
, &s
->gb
);
782 if(((int32_t)GET_CACHE(re
, &s
->gb
)) <= (int32_t)0xBFFFFFFF)
784 #if MIN_CACHE_BITS >= 19
785 UPDATE_CACHE(re
, &s
->gb
);
789 LAST_SKIP_BITS(re
, &s
->gb
, 2);
790 CLOSE_READER(re
, &s
->gb
);
792 s
->block_last_index
[n
] = i
;
796 static inline int mpeg1_fast_decode_block_inter(MpegEncContext
*s
, DCTELEM
*block
, int n
)
798 int level
, i
, j
, run
;
799 RLTable
*rl
= &ff_rl_mpeg1
;
800 uint8_t * const scantable
= s
->intra_scantable
.permutated
;
801 const int qscale
= s
->qscale
;
804 OPEN_READER(re
, &s
->gb
);
806 /* special case for the first coef. no need to add a second vlc table */
807 UPDATE_CACHE(re
, &s
->gb
);
808 if (((int32_t)GET_CACHE(re
, &s
->gb
)) < 0) {
809 level
= (3*qscale
)>>1;
811 if(GET_CACHE(re
, &s
->gb
)&0x40000000)
815 SKIP_BITS(re
, &s
->gb
, 2);
816 if(((int32_t)GET_CACHE(re
, &s
->gb
)) <= (int32_t)0xBFFFFFFF)
819 #if MIN_CACHE_BITS < 19
820 UPDATE_CACHE(re
, &s
->gb
);
823 /* now quantify & encode AC coefs */
825 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl
->rl_vlc
[0], TEX_VLC_BITS
, 2, 0);
830 level
= ((level
*2+1)*qscale
)>>1;
832 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
833 SKIP_BITS(re
, &s
->gb
, 1);
836 run
= SHOW_UBITS(re
, &s
->gb
, 6)+1; LAST_SKIP_BITS(re
, &s
->gb
, 6);
837 UPDATE_CACHE(re
, &s
->gb
);
838 level
= SHOW_SBITS(re
, &s
->gb
, 8); SKIP_BITS(re
, &s
->gb
, 8);
840 level
= SHOW_UBITS(re
, &s
->gb
, 8) - 256; SKIP_BITS(re
, &s
->gb
, 8);
841 } else if (level
== 0) {
842 level
= SHOW_UBITS(re
, &s
->gb
, 8) ; SKIP_BITS(re
, &s
->gb
, 8);
848 level
= ((level
*2+1)*qscale
)>>1;
852 level
= ((level
*2+1)*qscale
)>>1;
858 #if MIN_CACHE_BITS < 19
859 UPDATE_CACHE(re
, &s
->gb
);
861 if(((int32_t)GET_CACHE(re
, &s
->gb
)) <= (int32_t)0xBFFFFFFF)
863 #if MIN_CACHE_BITS >= 19
864 UPDATE_CACHE(re
, &s
->gb
);
868 LAST_SKIP_BITS(re
, &s
->gb
, 2);
869 CLOSE_READER(re
, &s
->gb
);
871 s
->block_last_index
[n
] = i
;
876 static inline int mpeg2_decode_block_non_intra(MpegEncContext
*s
,
880 int level
, i
, j
, run
;
881 RLTable
*rl
= &ff_rl_mpeg1
;
882 uint8_t * const scantable
= s
->intra_scantable
.permutated
;
883 const uint16_t *quant_matrix
;
884 const int qscale
= s
->qscale
;
890 OPEN_READER(re
, &s
->gb
);
893 quant_matrix
= s
->inter_matrix
;
895 quant_matrix
= s
->chroma_inter_matrix
;
897 /* special case for the first coef. no need to add a second vlc table */
898 UPDATE_CACHE(re
, &s
->gb
);
899 if (((int32_t)GET_CACHE(re
, &s
->gb
)) < 0) {
900 level
= (3*qscale
*quant_matrix
[0])>>5;
901 if(GET_CACHE(re
, &s
->gb
)&0x40000000)
906 SKIP_BITS(re
, &s
->gb
, 2);
907 if(((int32_t)GET_CACHE(re
, &s
->gb
)) <= (int32_t)0xBFFFFFFF)
910 #if MIN_CACHE_BITS < 19
911 UPDATE_CACHE(re
, &s
->gb
);
914 /* now quantify & encode AC coefs */
916 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl
->rl_vlc
[0], TEX_VLC_BITS
, 2, 0);
921 level
= ((level
*2+1)*qscale
*quant_matrix
[j
])>>5;
922 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
923 SKIP_BITS(re
, &s
->gb
, 1);
926 run
= SHOW_UBITS(re
, &s
->gb
, 6)+1; LAST_SKIP_BITS(re
, &s
->gb
, 6);
927 UPDATE_CACHE(re
, &s
->gb
);
928 level
= SHOW_SBITS(re
, &s
->gb
, 12); SKIP_BITS(re
, &s
->gb
, 12);
933 level
= ((-level
*2+1)*qscale
*quant_matrix
[j
])>>5;
936 level
= ((level
*2+1)*qscale
*quant_matrix
[j
])>>5;
940 av_log(s
->avctx
, AV_LOG_ERROR
, "ac-tex damaged at %d %d\n", s
->mb_x
, s
->mb_y
);
946 #if MIN_CACHE_BITS < 19
947 UPDATE_CACHE(re
, &s
->gb
);
949 if(((int32_t)GET_CACHE(re
, &s
->gb
)) <= (int32_t)0xBFFFFFFF)
951 #if MIN_CACHE_BITS >= 19
952 UPDATE_CACHE(re
, &s
->gb
);
956 LAST_SKIP_BITS(re
, &s
->gb
, 2);
957 CLOSE_READER(re
, &s
->gb
);
959 block
[63] ^= (mismatch
& 1);
961 s
->block_last_index
[n
] = i
;
965 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext
*s
,
969 int level
, i
, j
, run
;
970 RLTable
*rl
= &ff_rl_mpeg1
;
971 uint8_t * const scantable
= s
->intra_scantable
.permutated
;
972 const int qscale
= s
->qscale
;
973 OPEN_READER(re
, &s
->gb
);
976 /* special case for the first coef. no need to add a second vlc table */
977 UPDATE_CACHE(re
, &s
->gb
);
978 if (((int32_t)GET_CACHE(re
, &s
->gb
)) < 0) {
979 level
= (3*qscale
)>>1;
980 if(GET_CACHE(re
, &s
->gb
)&0x40000000)
984 SKIP_BITS(re
, &s
->gb
, 2);
985 if(((int32_t)GET_CACHE(re
, &s
->gb
)) <= (int32_t)0xBFFFFFFF)
988 #if MIN_CACHE_BITS < 19
989 UPDATE_CACHE(re
, &s
->gb
);
992 /* now quantify & encode AC coefs */
994 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl
->rl_vlc
[0], TEX_VLC_BITS
, 2, 0);
999 level
= ((level
*2+1)*qscale
)>>1;
1000 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
1001 SKIP_BITS(re
, &s
->gb
, 1);
1004 run
= SHOW_UBITS(re
, &s
->gb
, 6)+1; LAST_SKIP_BITS(re
, &s
->gb
, 6);
1005 UPDATE_CACHE(re
, &s
->gb
);
1006 level
= SHOW_SBITS(re
, &s
->gb
, 12); SKIP_BITS(re
, &s
->gb
, 12);
1011 level
= ((-level
*2+1)*qscale
)>>1;
1014 level
= ((level
*2+1)*qscale
)>>1;
1019 #if MIN_CACHE_BITS < 19
1020 UPDATE_CACHE(re
, &s
->gb
);
1022 if(((int32_t)GET_CACHE(re
, &s
->gb
)) <= (int32_t)0xBFFFFFFF)
1024 #if MIN_CACHE_BITS >=19
1025 UPDATE_CACHE(re
, &s
->gb
);
1029 LAST_SKIP_BITS(re
, &s
->gb
, 2);
1030 CLOSE_READER(re
, &s
->gb
);
1031 s
->block_last_index
[n
] = i
;
1036 static inline int mpeg2_decode_block_intra(MpegEncContext
*s
,
1040 int level
, dc
, diff
, i
, j
, run
;
1043 uint8_t * const scantable
= s
->intra_scantable
.permutated
;
1044 const uint16_t *quant_matrix
;
1045 const int qscale
= s
->qscale
;
1050 quant_matrix
= s
->intra_matrix
;
1053 quant_matrix
= s
->chroma_intra_matrix
;
1054 component
= (n
&1) + 1;
1056 diff
= decode_dc(&s
->gb
, component
);
1059 dc
= s
->last_dc
[component
];
1061 s
->last_dc
[component
] = dc
;
1062 block
[0] = dc
<< (3 - s
->intra_dc_precision
);
1063 dprintf(s
->avctx
, "dc=%d\n", block
[0]);
1064 mismatch
= block
[0] ^ 1;
1066 if (s
->intra_vlc_format
)
1072 OPEN_READER(re
, &s
->gb
);
1073 /* now quantify & encode AC coefs */
1075 UPDATE_CACHE(re
, &s
->gb
);
1076 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl
->rl_vlc
[0], TEX_VLC_BITS
, 2, 0);
1080 } else if(level
!= 0) {
1083 level
= (level
*qscale
*quant_matrix
[j
])>>4;
1084 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
1085 LAST_SKIP_BITS(re
, &s
->gb
, 1);
1088 run
= SHOW_UBITS(re
, &s
->gb
, 6)+1; LAST_SKIP_BITS(re
, &s
->gb
, 6);
1089 UPDATE_CACHE(re
, &s
->gb
);
1090 level
= SHOW_SBITS(re
, &s
->gb
, 12); SKIP_BITS(re
, &s
->gb
, 12);
1094 level
= (-level
*qscale
*quant_matrix
[j
])>>4;
1097 level
= (level
*qscale
*quant_matrix
[j
])>>4;
1101 av_log(s
->avctx
, AV_LOG_ERROR
, "ac-tex damaged at %d %d\n", s
->mb_x
, s
->mb_y
);
1108 CLOSE_READER(re
, &s
->gb
);
1110 block
[63]^= mismatch
&1;
1112 s
->block_last_index
[n
] = i
;
1116 static inline int mpeg2_fast_decode_block_intra(MpegEncContext
*s
,
1120 int level
, dc
, diff
, j
, run
;
1123 uint8_t * scantable
= s
->intra_scantable
.permutated
;
1124 const uint16_t *quant_matrix
;
1125 const int qscale
= s
->qscale
;
1129 quant_matrix
= s
->intra_matrix
;
1132 quant_matrix
= s
->chroma_intra_matrix
;
1133 component
= (n
&1) + 1;
1135 diff
= decode_dc(&s
->gb
, component
);
1138 dc
= s
->last_dc
[component
];
1140 s
->last_dc
[component
] = dc
;
1141 block
[0] = dc
<< (3 - s
->intra_dc_precision
);
1142 if (s
->intra_vlc_format
)
1148 OPEN_READER(re
, &s
->gb
);
1149 /* now quantify & encode AC coefs */
1151 UPDATE_CACHE(re
, &s
->gb
);
1152 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl
->rl_vlc
[0], TEX_VLC_BITS
, 2, 0);
1156 } else if(level
!= 0) {
1159 level
= (level
*qscale
*quant_matrix
[j
])>>4;
1160 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
1161 LAST_SKIP_BITS(re
, &s
->gb
, 1);
1164 run
= SHOW_UBITS(re
, &s
->gb
, 6)+1; LAST_SKIP_BITS(re
, &s
->gb
, 6);
1165 UPDATE_CACHE(re
, &s
->gb
);
1166 level
= SHOW_SBITS(re
, &s
->gb
, 12); SKIP_BITS(re
, &s
->gb
, 12);
1170 level
= (-level
*qscale
*quant_matrix
[j
])>>4;
1173 level
= (level
*qscale
*quant_matrix
[j
])>>4;
1179 CLOSE_READER(re
, &s
->gb
);
1182 s
->block_last_index
[n
] = scantable
- s
->intra_scantable
.permutated
;
1186 typedef struct Mpeg1Context
{
1187 MpegEncContext mpeg_enc_ctx
;
1188 int mpeg_enc_ctx_allocated
; /* true if decoding context allocated */
1189 int repeat_field
; /* true if we must repeat the field */
1190 AVPanScan pan_scan
; /** some temporary storage for the panscan */
1192 int swap_uv
;//indicate VCR2
1193 int save_aspect_info
;
1194 int save_width
, save_height
;
1195 AVRational frame_rate_ext
; ///< MPEG-2 specific framerate modificator
1199 static av_cold
int mpeg_decode_init(AVCodecContext
*avctx
)
1201 Mpeg1Context
*s
= avctx
->priv_data
;
1202 MpegEncContext
*s2
= &s
->mpeg_enc_ctx
;
1205 //we need some parmutation to store
1206 //matrixes, until MPV_common_init()
1207 //set the real permutatuon
1209 s2
->dsp
.idct_permutation
[i
]=i
;
1211 MPV_decode_defaults(s2
);
1213 s
->mpeg_enc_ctx
.avctx
= avctx
;
1214 s
->mpeg_enc_ctx
.flags
= avctx
->flags
;
1215 s
->mpeg_enc_ctx
.flags2
= avctx
->flags2
;
1216 ff_mpeg12_common_init(&s
->mpeg_enc_ctx
);
1219 s
->mpeg_enc_ctx_allocated
= 0;
1220 s
->mpeg_enc_ctx
.picture_number
= 0;
1221 s
->repeat_field
= 0;
1222 s
->mpeg_enc_ctx
.codec_id
= avctx
->codec
->id
;
1226 static void quant_matrix_rebuild(uint16_t *matrix
, const uint8_t *old_perm
,
1227 const uint8_t *new_perm
){
1228 uint16_t temp_matrix
[64];
1231 memcpy(temp_matrix
,matrix
,64*sizeof(uint16_t));
1234 matrix
[new_perm
[i
]] = temp_matrix
[old_perm
[i
]];
1238 //Call this function when we know all parameters
1239 //it may be called in different places for mpeg1 and mpeg2
1240 static int mpeg_decode_postinit(AVCodecContext
*avctx
){
1241 Mpeg1Context
*s1
= avctx
->priv_data
;
1242 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1243 uint8_t old_permutation
[64];
1246 (s1
->mpeg_enc_ctx_allocated
== 0)||
1247 avctx
->coded_width
!= s
->width
||
1248 avctx
->coded_height
!= s
->height
||
1249 s1
->save_width
!= s
->width
||
1250 s1
->save_height
!= s
->height
||
1251 s1
->save_aspect_info
!= s
->aspect_ratio_info
||
1255 if (s1
->mpeg_enc_ctx_allocated
) {
1256 ParseContext pc
= s
->parse_context
;
1257 s
->parse_context
.buffer
=0;
1259 s
->parse_context
= pc
;
1262 if( (s
->width
== 0 )||(s
->height
== 0))
1265 avcodec_set_dimensions(avctx
, s
->width
, s
->height
);
1266 avctx
->bit_rate
= s
->bit_rate
;
1267 s1
->save_aspect_info
= s
->aspect_ratio_info
;
1268 s1
->save_width
= s
->width
;
1269 s1
->save_height
= s
->height
;
1271 //low_delay may be forced, in this case we will have B frames
1272 //that behave like P frames
1273 avctx
->has_b_frames
= !(s
->low_delay
);
1275 if(avctx
->sub_id
==1){//s->codec_id==avctx->codec_id==CODEC_ID
1277 avctx
->time_base
.den
= ff_frame_rate_tab
[s
->frame_rate_index
].num
;
1278 avctx
->time_base
.num
= ff_frame_rate_tab
[s
->frame_rate_index
].den
;
1280 avctx
->sample_aspect_ratio
= av_d2q(
1281 1.0/ff_mpeg1_aspect
[s
->aspect_ratio_info
], 255);
1286 &s
->avctx
->time_base
.den
,
1287 &s
->avctx
->time_base
.num
,
1288 ff_frame_rate_tab
[s
->frame_rate_index
].num
* s1
->frame_rate_ext
.num
,
1289 ff_frame_rate_tab
[s
->frame_rate_index
].den
* s1
->frame_rate_ext
.den
,
1292 if(s
->aspect_ratio_info
> 1){
1293 if( (s1
->pan_scan
.width
== 0 )||(s1
->pan_scan
.height
== 0) ){
1294 s
->avctx
->sample_aspect_ratio
=
1296 ff_mpeg2_aspect
[s
->aspect_ratio_info
],
1297 (AVRational
){s
->width
, s
->height
}
1300 s
->avctx
->sample_aspect_ratio
=
1302 ff_mpeg2_aspect
[s
->aspect_ratio_info
],
1303 (AVRational
){s1
->pan_scan
.width
, s1
->pan_scan
.height
}
1307 s
->avctx
->sample_aspect_ratio
=
1308 ff_mpeg2_aspect
[s
->aspect_ratio_info
];
1312 if(avctx
->xvmc_acceleration
){
1313 avctx
->pix_fmt
= avctx
->get_format(avctx
,pixfmt_xvmc_mpg2_420
);
1315 if(s
->chroma_format
< 2){
1316 avctx
->pix_fmt
= avctx
->get_format(avctx
,pixfmt_yuv_420
);
1318 if(s
->chroma_format
== 2){
1319 avctx
->pix_fmt
= avctx
->get_format(avctx
,pixfmt_yuv_422
);
1321 if(s
->chroma_format
> 2){
1322 avctx
->pix_fmt
= avctx
->get_format(avctx
,pixfmt_yuv_444
);
1325 //until then pix_fmt may be changed right after codec init
1326 if( avctx
->pix_fmt
== PIX_FMT_XVMC_MPEG2_IDCT
)
1327 if( avctx
->idct_algo
== FF_IDCT_AUTO
)
1328 avctx
->idct_algo
= FF_IDCT_SIMPLE
;
1330 //quantization matrixes may need reordering
1331 //if dct permutation is changed
1332 memcpy(old_permutation
,s
->dsp
.idct_permutation
,64*sizeof(uint8_t));
1334 if (MPV_common_init(s
) < 0)
1337 quant_matrix_rebuild(s
->intra_matrix
, old_permutation
,s
->dsp
.idct_permutation
);
1338 quant_matrix_rebuild(s
->inter_matrix
, old_permutation
,s
->dsp
.idct_permutation
);
1339 quant_matrix_rebuild(s
->chroma_intra_matrix
,old_permutation
,s
->dsp
.idct_permutation
);
1340 quant_matrix_rebuild(s
->chroma_inter_matrix
,old_permutation
,s
->dsp
.idct_permutation
);
1342 s1
->mpeg_enc_ctx_allocated
= 1;
1347 static int mpeg1_decode_picture(AVCodecContext
*avctx
,
1348 const uint8_t *buf
, int buf_size
)
1350 Mpeg1Context
*s1
= avctx
->priv_data
;
1351 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1352 int ref
, f_code
, vbv_delay
;
1354 if(mpeg_decode_postinit(s
->avctx
) < 0)
1357 init_get_bits(&s
->gb
, buf
, buf_size
*8);
1359 ref
= get_bits(&s
->gb
, 10); /* temporal ref */
1360 s
->pict_type
= get_bits(&s
->gb
, 3);
1361 if(s
->pict_type
== 0 || s
->pict_type
> 3)
1364 vbv_delay
= get_bits(&s
->gb
, 16);
1365 if (s
->pict_type
== FF_P_TYPE
|| s
->pict_type
== FF_B_TYPE
) {
1366 s
->full_pel
[0] = get_bits1(&s
->gb
);
1367 f_code
= get_bits(&s
->gb
, 3);
1368 if (f_code
== 0 && avctx
->error_resilience
>= FF_ER_COMPLIANT
)
1370 s
->mpeg_f_code
[0][0] = f_code
;
1371 s
->mpeg_f_code
[0][1] = f_code
;
1373 if (s
->pict_type
== FF_B_TYPE
) {
1374 s
->full_pel
[1] = get_bits1(&s
->gb
);
1375 f_code
= get_bits(&s
->gb
, 3);
1376 if (f_code
== 0 && avctx
->error_resilience
>= FF_ER_COMPLIANT
)
1378 s
->mpeg_f_code
[1][0] = f_code
;
1379 s
->mpeg_f_code
[1][1] = f_code
;
1381 s
->current_picture
.pict_type
= s
->pict_type
;
1382 s
->current_picture
.key_frame
= s
->pict_type
== FF_I_TYPE
;
1384 if(avctx
->debug
& FF_DEBUG_PICT_INFO
)
1385 av_log(avctx
, AV_LOG_DEBUG
, "vbv_delay %d, ref %d type:%d\n", vbv_delay
, ref
, s
->pict_type
);
1393 static void mpeg_decode_sequence_extension(Mpeg1Context
*s1
)
1395 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1396 int horiz_size_ext
, vert_size_ext
;
1399 skip_bits(&s
->gb
, 1); /* profil and level esc*/
1400 s
->avctx
->profile
= get_bits(&s
->gb
, 3);
1401 s
->avctx
->level
= get_bits(&s
->gb
, 4);
1402 s
->progressive_sequence
= get_bits1(&s
->gb
); /* progressive_sequence */
1403 s
->chroma_format
= get_bits(&s
->gb
, 2); /* chroma_format 1=420, 2=422, 3=444 */
1404 horiz_size_ext
= get_bits(&s
->gb
, 2);
1405 vert_size_ext
= get_bits(&s
->gb
, 2);
1406 s
->width
|= (horiz_size_ext
<< 12);
1407 s
->height
|= (vert_size_ext
<< 12);
1408 bit_rate_ext
= get_bits(&s
->gb
, 12); /* XXX: handle it */
1409 s
->bit_rate
+= (bit_rate_ext
<< 18) * 400;
1410 skip_bits1(&s
->gb
); /* marker */
1411 s
->avctx
->rc_buffer_size
+= get_bits(&s
->gb
, 8)*1024*16<<10;
1413 s
->low_delay
= get_bits1(&s
->gb
);
1414 if(s
->flags
& CODEC_FLAG_LOW_DELAY
) s
->low_delay
=1;
1416 s1
->frame_rate_ext
.num
= get_bits(&s
->gb
, 2)+1;
1417 s1
->frame_rate_ext
.den
= get_bits(&s
->gb
, 5)+1;
1419 dprintf(s
->avctx
, "sequence extension\n");
1420 s
->codec_id
= s
->avctx
->codec_id
= CODEC_ID_MPEG2VIDEO
;
1421 s
->avctx
->sub_id
= 2; /* indicates mpeg2 found */
1423 if(s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
1424 av_log(s
->avctx
, AV_LOG_DEBUG
, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1425 s
->avctx
->profile
, s
->avctx
->level
, s
->avctx
->rc_buffer_size
, s
->bit_rate
);
1429 static void mpeg_decode_sequence_display_extension(Mpeg1Context
*s1
)
1431 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1432 int color_description
, w
, h
;
1434 skip_bits(&s
->gb
, 3); /* video format */
1435 color_description
= get_bits1(&s
->gb
);
1436 if(color_description
){
1437 skip_bits(&s
->gb
, 8); /* color primaries */
1438 skip_bits(&s
->gb
, 8); /* transfer_characteristics */
1439 skip_bits(&s
->gb
, 8); /* matrix_coefficients */
1441 w
= get_bits(&s
->gb
, 14);
1442 skip_bits(&s
->gb
, 1); //marker
1443 h
= get_bits(&s
->gb
, 14);
1444 skip_bits(&s
->gb
, 1); //marker
1446 s1
->pan_scan
.width
= 16*w
;
1447 s1
->pan_scan
.height
=16*h
;
1449 if(s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
1450 av_log(s
->avctx
, AV_LOG_DEBUG
, "sde w:%d, h:%d\n", w
, h
);
1453 static void mpeg_decode_picture_display_extension(Mpeg1Context
*s1
)
1455 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1459 if(s
->progressive_sequence
){
1460 if(s
->repeat_first_field
){
1462 if(s
->top_field_first
)
1466 if(s
->picture_structure
== PICT_FRAME
){
1468 if(s
->repeat_first_field
)
1472 for(i
=0; i
<nofco
; i
++){
1473 s1
->pan_scan
.position
[i
][0]= get_sbits(&s
->gb
, 16);
1474 skip_bits(&s
->gb
, 1); //marker
1475 s1
->pan_scan
.position
[i
][1]= get_sbits(&s
->gb
, 16);
1476 skip_bits(&s
->gb
, 1); //marker
1479 if(s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
1480 av_log(s
->avctx
, AV_LOG_DEBUG
, "pde (%d,%d) (%d,%d) (%d,%d)\n",
1481 s1
->pan_scan
.position
[0][0], s1
->pan_scan
.position
[0][1],
1482 s1
->pan_scan
.position
[1][0], s1
->pan_scan
.position
[1][1],
1483 s1
->pan_scan
.position
[2][0], s1
->pan_scan
.position
[2][1]
1487 static void mpeg_decode_quant_matrix_extension(MpegEncContext
*s
)
1491 dprintf(s
->avctx
, "matrix extension\n");
1493 if (get_bits1(&s
->gb
)) {
1495 v
= get_bits(&s
->gb
, 8);
1496 j
= s
->dsp
.idct_permutation
[ ff_zigzag_direct
[i
] ];
1497 s
->intra_matrix
[j
] = v
;
1498 s
->chroma_intra_matrix
[j
] = v
;
1501 if (get_bits1(&s
->gb
)) {
1503 v
= get_bits(&s
->gb
, 8);
1504 j
= s
->dsp
.idct_permutation
[ ff_zigzag_direct
[i
] ];
1505 s
->inter_matrix
[j
] = v
;
1506 s
->chroma_inter_matrix
[j
] = v
;
1509 if (get_bits1(&s
->gb
)) {
1511 v
= get_bits(&s
->gb
, 8);
1512 j
= s
->dsp
.idct_permutation
[ ff_zigzag_direct
[i
] ];
1513 s
->chroma_intra_matrix
[j
] = v
;
1516 if (get_bits1(&s
->gb
)) {
1518 v
= get_bits(&s
->gb
, 8);
1519 j
= s
->dsp
.idct_permutation
[ ff_zigzag_direct
[i
] ];
1520 s
->chroma_inter_matrix
[j
] = v
;
1525 static void mpeg_decode_picture_coding_extension(MpegEncContext
*s
)
1527 s
->full_pel
[0] = s
->full_pel
[1] = 0;
1528 s
->mpeg_f_code
[0][0] = get_bits(&s
->gb
, 4);
1529 s
->mpeg_f_code
[0][1] = get_bits(&s
->gb
, 4);
1530 s
->mpeg_f_code
[1][0] = get_bits(&s
->gb
, 4);
1531 s
->mpeg_f_code
[1][1] = get_bits(&s
->gb
, 4);
1532 s
->intra_dc_precision
= get_bits(&s
->gb
, 2);
1533 s
->picture_structure
= get_bits(&s
->gb
, 2);
1534 s
->top_field_first
= get_bits1(&s
->gb
);
1535 s
->frame_pred_frame_dct
= get_bits1(&s
->gb
);
1536 s
->concealment_motion_vectors
= get_bits1(&s
->gb
);
1537 s
->q_scale_type
= get_bits1(&s
->gb
);
1538 s
->intra_vlc_format
= get_bits1(&s
->gb
);
1539 s
->alternate_scan
= get_bits1(&s
->gb
);
1540 s
->repeat_first_field
= get_bits1(&s
->gb
);
1541 s
->chroma_420_type
= get_bits1(&s
->gb
);
1542 s
->progressive_frame
= get_bits1(&s
->gb
);
1544 if(s
->picture_structure
== PICT_FRAME
){
1546 s
->v_edge_pos
= 16*s
->mb_height
;
1548 s
->first_field
^= 1;
1549 s
->v_edge_pos
= 8*s
->mb_height
;
1550 memset(s
->mbskip_table
, 0, s
->mb_stride
*s
->mb_height
);
1553 if(s
->alternate_scan
){
1554 ff_init_scantable(s
->dsp
.idct_permutation
, &s
->inter_scantable
, ff_alternate_vertical_scan
);
1555 ff_init_scantable(s
->dsp
.idct_permutation
, &s
->intra_scantable
, ff_alternate_vertical_scan
);
1557 ff_init_scantable(s
->dsp
.idct_permutation
, &s
->inter_scantable
, ff_zigzag_direct
);
1558 ff_init_scantable(s
->dsp
.idct_permutation
, &s
->intra_scantable
, ff_zigzag_direct
);
1561 /* composite display not parsed */
1562 dprintf(s
->avctx
, "intra_dc_precision=%d\n", s
->intra_dc_precision
);
1563 dprintf(s
->avctx
, "picture_structure=%d\n", s
->picture_structure
);
1564 dprintf(s
->avctx
, "top field first=%d\n", s
->top_field_first
);
1565 dprintf(s
->avctx
, "repeat first field=%d\n", s
->repeat_first_field
);
1566 dprintf(s
->avctx
, "conceal=%d\n", s
->concealment_motion_vectors
);
1567 dprintf(s
->avctx
, "intra_vlc_format=%d\n", s
->intra_vlc_format
);
1568 dprintf(s
->avctx
, "alternate_scan=%d\n", s
->alternate_scan
);
1569 dprintf(s
->avctx
, "frame_pred_frame_dct=%d\n", s
->frame_pred_frame_dct
);
1570 dprintf(s
->avctx
, "progressive_frame=%d\n", s
->progressive_frame
);
1573 static void mpeg_decode_extension(AVCodecContext
*avctx
,
1574 const uint8_t *buf
, int buf_size
)
1576 Mpeg1Context
*s1
= avctx
->priv_data
;
1577 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1580 init_get_bits(&s
->gb
, buf
, buf_size
*8);
1582 ext_type
= get_bits(&s
->gb
, 4);
1585 mpeg_decode_sequence_extension(s1
);
1588 mpeg_decode_sequence_display_extension(s1
);
1591 mpeg_decode_quant_matrix_extension(s
);
1594 mpeg_decode_picture_display_extension(s1
);
1597 mpeg_decode_picture_coding_extension(s
);
1602 static void exchange_uv(MpegEncContext
*s
){
1603 short * tmp
= s
->pblocks
[4];
1604 s
->pblocks
[4] = s
->pblocks
[5];
1605 s
->pblocks
[5] = tmp
;
1608 static int mpeg_field_start(MpegEncContext
*s
){
1609 AVCodecContext
*avctx
= s
->avctx
;
1610 Mpeg1Context
*s1
= (Mpeg1Context
*)s
;
1612 /* start frame decoding */
1613 if(s
->first_field
|| s
->picture_structure
==PICT_FRAME
){
1614 if(MPV_frame_start(s
, avctx
) < 0)
1617 ff_er_frame_start(s
);
1619 /* first check if we must repeat the frame */
1620 s
->current_picture_ptr
->repeat_pict
= 0;
1621 if (s
->repeat_first_field
) {
1622 if (s
->progressive_sequence
) {
1623 if (s
->top_field_first
)
1624 s
->current_picture_ptr
->repeat_pict
= 4;
1626 s
->current_picture_ptr
->repeat_pict
= 2;
1627 } else if (s
->progressive_frame
) {
1628 s
->current_picture_ptr
->repeat_pict
= 1;
1632 *s
->current_picture_ptr
->pan_scan
= s1
->pan_scan
;
1633 }else{ //second field
1636 if(!s
->current_picture_ptr
){
1637 av_log(s
->avctx
, AV_LOG_ERROR
, "first field missing\n");
1642 s
->current_picture
.data
[i
] = s
->current_picture_ptr
->data
[i
];
1643 if(s
->picture_structure
== PICT_BOTTOM_FIELD
){
1644 s
->current_picture
.data
[i
] += s
->current_picture_ptr
->linesize
[i
];
1649 // MPV_frame_start will call this function too,
1650 // but we need to call it on every field
1651 if(s
->avctx
->xvmc_acceleration
)
1652 XVMC_field_start(s
,avctx
);
1658 #define DECODE_SLICE_ERROR -1
1659 #define DECODE_SLICE_OK 0
1662 * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
1663 * @return DECODE_SLICE_ERROR if the slice is damaged<br>
1664 * DECODE_SLICE_OK if this slice is ok<br>
1666 static int mpeg_decode_slice(Mpeg1Context
*s1
, int mb_y
,
1667 const uint8_t **buf
, int buf_size
)
1669 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1670 AVCodecContext
*avctx
= s
->avctx
;
1671 const int field_pic
= s
->picture_structure
!= PICT_FRAME
;
1672 const int lowres
= s
->avctx
->lowres
;
1677 if (mb_y
<<field_pic
>= s
->mb_height
){
1678 av_log(s
->avctx
, AV_LOG_ERROR
, "slice below image (%d >= %d)\n", mb_y
, s
->mb_height
);
1682 init_get_bits(&s
->gb
, *buf
, buf_size
*8);
1684 ff_mpeg1_clean_buffers(s
);
1685 s
->interlaced_dct
= 0;
1687 s
->qscale
= get_qscale(s
);
1690 av_log(s
->avctx
, AV_LOG_ERROR
, "qscale == 0\n");
1694 /* extra slice info */
1695 while (get_bits1(&s
->gb
) != 0) {
1696 skip_bits(&s
->gb
, 8);
1702 int code
= get_vlc2(&s
->gb
, mbincr_vlc
.table
, MBINCR_VLC_BITS
, 2);
1704 av_log(s
->avctx
, AV_LOG_ERROR
, "first mb_incr damaged\n");
1711 /* otherwise, stuffing, nothing to do */
1717 if(s
->mb_x
>= (unsigned)s
->mb_width
){
1718 av_log(s
->avctx
, AV_LOG_ERROR
, "initial skip overflow\n");
1722 s
->resync_mb_x
= s
->mb_x
;
1723 s
->resync_mb_y
= s
->mb_y
= mb_y
;
1725 ff_init_block_index(s
);
1727 if (s
->mb_y
==0 && s
->mb_x
==0 && (s
->first_field
|| s
->picture_structure
==PICT_FRAME
)) {
1728 if(s
->avctx
->debug
&FF_DEBUG_PICT_INFO
){
1729 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",
1730 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],
1731 s
->pict_type
== FF_I_TYPE ?
"I" : (s
->pict_type
== FF_P_TYPE ?
"P" : (s
->pict_type
== FF_B_TYPE ?
"B" : "S")),
1732 s
->progressive_sequence ?
"ps" :"", s
->progressive_frame ?
"pf" : "", s
->alternate_scan ?
"alt" :"", s
->top_field_first ?
"top" :"",
1733 s
->intra_dc_precision
, s
->picture_structure
, s
->frame_pred_frame_dct
, s
->concealment_motion_vectors
,
1734 s
->q_scale_type
, s
->intra_vlc_format
, s
->repeat_first_field
, s
->chroma_420_type ?
"420" :"");
1740 //one 1 we memcpy blocks in xvmcvideo
1741 if(s
->avctx
->xvmc_acceleration
> 1)
1742 XVMC_init_block(s
);//set s->block
1745 if(mpeg_decode_mb(s
, s
->block
) < 0)
1748 if(s
->current_picture
.motion_val
[0] && !s
->encoding
){ //note motion_val is normally NULL unless we want to extract the MVs
1749 const int wrap
= field_pic ?
2*s
->b8_stride
: s
->b8_stride
;
1750 int xy
= s
->mb_x
*2 + s
->mb_y
*2*wrap
;
1751 int motion_x
, motion_y
, dir
, i
;
1752 if(field_pic
&& !s
->first_field
)
1756 for(dir
=0; dir
<2; dir
++){
1757 if (s
->mb_intra
|| (dir
==1 && s
->pict_type
!= FF_B_TYPE
)) {
1758 motion_x
= motion_y
= 0;
1759 }else if (s
->mv_type
== MV_TYPE_16X16
|| (s
->mv_type
== MV_TYPE_FIELD
&& field_pic
)){
1760 motion_x
= s
->mv
[dir
][0][0];
1761 motion_y
= s
->mv
[dir
][0][1];
1762 } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
1763 motion_x
= s
->mv
[dir
][i
][0];
1764 motion_y
= s
->mv
[dir
][i
][1];
1767 s
->current_picture
.motion_val
[dir
][xy
][0] = motion_x
;
1768 s
->current_picture
.motion_val
[dir
][xy
][1] = motion_y
;
1769 s
->current_picture
.motion_val
[dir
][xy
+ 1][0] = motion_x
;
1770 s
->current_picture
.motion_val
[dir
][xy
+ 1][1] = motion_y
;
1771 s
->current_picture
.ref_index
[dir
][xy
]=
1772 s
->current_picture
.ref_index
[dir
][xy
+ 1]= s
->field_select
[dir
][i
];
1773 assert(s
->field_select
[dir
][i
]==0 || s
->field_select
[dir
][i
]==1);
1779 s
->dest
[0] += 16 >> lowres
;
1780 s
->dest
[1] +=(16 >> lowres
) >> s
->chroma_x_shift
;
1781 s
->dest
[2] +=(16 >> lowres
) >> s
->chroma_x_shift
;
1783 MPV_decode_mb(s
, s
->block
);
1785 if (++s
->mb_x
>= s
->mb_width
) {
1786 const int mb_size
= 16>>s
->avctx
->lowres
;
1788 ff_draw_horiz_band(s
, mb_size
*s
->mb_y
, mb_size
);
1793 if(s
->mb_y
<<field_pic
>= s
->mb_height
){
1794 int left
= s
->gb
.size_in_bits
- get_bits_count(&s
->gb
);
1795 int is_d10
= s
->chroma_format
==2 && s
->pict_type
==FF_I_TYPE
&& avctx
->profile
==0 && avctx
->level
==5
1796 && s
->intra_dc_precision
== 2 && s
->q_scale_type
== 1 && s
->alternate_scan
== 0
1797 && s
->progressive_frame
== 0 /* vbv_delay == 0xBBB || 0xE10*/;
1799 if(left
< 0 || (left
&& show_bits(&s
->gb
, FFMIN(left
, 23)) && !is_d10
)
1800 || (avctx
->error_resilience
>= FF_ER_AGGRESSIVE
&& left
>8)){
1801 av_log(avctx
, AV_LOG_ERROR
, "end mismatch left=%d %0X\n", left
, show_bits(&s
->gb
, FFMIN(left
, 23)));
1807 ff_init_block_index(s
);
1810 /* skip mb handling */
1811 if (s
->mb_skip_run
== -1) {
1812 /* read again increment */
1815 int code
= get_vlc2(&s
->gb
, mbincr_vlc
.table
, MBINCR_VLC_BITS
, 2);
1817 av_log(s
->avctx
, AV_LOG_ERROR
, "mb incr damaged\n");
1822 s
->mb_skip_run
+= 33;
1823 }else if(code
== 35){
1824 if(s
->mb_skip_run
!= 0 || show_bits(&s
->gb
, 15) != 0){
1825 av_log(s
->avctx
, AV_LOG_ERROR
, "slice mismatch\n");
1828 goto eos
; /* end of slice */
1830 /* otherwise, stuffing, nothing to do */
1832 s
->mb_skip_run
+= code
;
1838 if(s
->pict_type
== FF_I_TYPE
){
1839 av_log(s
->avctx
, AV_LOG_ERROR
, "skipped MB in I frame at %d %d\n", s
->mb_x
, s
->mb_y
);
1846 s
->block_last_index
[i
] = -1;
1847 if(s
->picture_structure
== PICT_FRAME
)
1848 s
->mv_type
= MV_TYPE_16X16
;
1850 s
->mv_type
= MV_TYPE_FIELD
;
1851 if (s
->pict_type
== FF_P_TYPE
) {
1852 /* if P type, zero motion vector is implied */
1853 s
->mv_dir
= MV_DIR_FORWARD
;
1854 s
->mv
[0][0][0] = s
->mv
[0][0][1] = 0;
1855 s
->last_mv
[0][0][0] = s
->last_mv
[0][0][1] = 0;
1856 s
->last_mv
[0][1][0] = s
->last_mv
[0][1][1] = 0;
1857 s
->field_select
[0][0]= s
->picture_structure
- 1;
1859 /* if B type, reuse previous vectors and directions */
1860 s
->mv
[0][0][0] = s
->last_mv
[0][0][0];
1861 s
->mv
[0][0][1] = s
->last_mv
[0][0][1];
1862 s
->mv
[1][0][0] = s
->last_mv
[1][0][0];
1863 s
->mv
[1][0][1] = s
->last_mv
[1][0][1];
1868 eos
: // end of slice
1869 *buf
+= (get_bits_count(&s
->gb
)-1)/8;
1870 //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1874 static int slice_decode_thread(AVCodecContext
*c
, void *arg
){
1875 MpegEncContext
*s
= arg
;
1876 const uint8_t *buf
= s
->gb
.buffer
;
1877 int mb_y
= s
->start_mb_y
;
1879 s
->error_count
= 3*(s
->end_mb_y
- s
->start_mb_y
)*s
->mb_width
;
1882 uint32_t start_code
;
1885 ret
= mpeg_decode_slice((Mpeg1Context
*)s
, mb_y
, &buf
, s
->gb
.buffer_end
- buf
);
1887 //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1888 //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);
1890 if(s
->resync_mb_x
>=0 && s
->resync_mb_y
>=0)
1891 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
);
1893 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
);
1896 if(s
->mb_y
== s
->end_mb_y
)
1900 buf
= ff_find_start_code(buf
, s
->gb
.buffer_end
, &start_code
);
1901 mb_y
= start_code
- SLICE_MIN_START_CODE
;
1902 if(mb_y
< 0 || mb_y
>= s
->end_mb_y
)
1906 return 0; //not reached
1910 * handles slice ends.
1911 * @return 1 if it seems to be the last slice of
1913 static int slice_end(AVCodecContext
*avctx
, AVFrame
*pict
)
1915 Mpeg1Context
*s1
= avctx
->priv_data
;
1916 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1918 if (!s1
->mpeg_enc_ctx_allocated
|| !s
->current_picture_ptr
)
1922 if(s
->avctx
->xvmc_acceleration
)
1925 /* end of slice reached */
1926 if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s
->first_field
) {
1929 s
->current_picture_ptr
->qscale_type
= FF_QSCALE_TYPE_MPEG2
;
1935 if (s
->pict_type
== FF_B_TYPE
|| s
->low_delay
) {
1936 *pict
= *(AVFrame
*)s
->current_picture_ptr
;
1937 ff_print_debug_info(s
, pict
);
1939 s
->picture_number
++;
1940 /* latency of 1 frame for I and P frames */
1941 /* XXX: use another variable than picture_number */
1942 if (s
->last_picture_ptr
!= NULL
) {
1943 *pict
= *(AVFrame
*)s
->last_picture_ptr
;
1944 ff_print_debug_info(s
, pict
);
1954 static int mpeg1_decode_sequence(AVCodecContext
*avctx
,
1955 const uint8_t *buf
, int buf_size
)
1957 Mpeg1Context
*s1
= avctx
->priv_data
;
1958 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1962 init_get_bits(&s
->gb
, buf
, buf_size
*8);
1964 width
= get_bits(&s
->gb
, 12);
1965 height
= get_bits(&s
->gb
, 12);
1966 if (width
<= 0 || height
<= 0)
1968 s
->aspect_ratio_info
= get_bits(&s
->gb
, 4);
1969 if (s
->aspect_ratio_info
== 0) {
1970 av_log(avctx
, AV_LOG_ERROR
, "aspect ratio has forbidden 0 value\n");
1971 if (avctx
->error_resilience
>= FF_ER_COMPLIANT
)
1974 s
->frame_rate_index
= get_bits(&s
->gb
, 4);
1975 if (s
->frame_rate_index
== 0 || s
->frame_rate_index
> 13)
1977 s
->bit_rate
= get_bits(&s
->gb
, 18) * 400;
1978 if (get_bits1(&s
->gb
) == 0) /* marker */
1983 s
->avctx
->rc_buffer_size
= get_bits(&s
->gb
, 10) * 1024*16;
1984 skip_bits(&s
->gb
, 1);
1987 if (get_bits1(&s
->gb
)) {
1989 v
= get_bits(&s
->gb
, 8);
1991 av_log(s
->avctx
, AV_LOG_ERROR
, "intra matrix damaged\n");
1994 j
= s
->dsp
.idct_permutation
[ ff_zigzag_direct
[i
] ];
1995 s
->intra_matrix
[j
] = v
;
1996 s
->chroma_intra_matrix
[j
] = v
;
1999 dprintf(s
->avctx
, "intra matrix present\n");
2001 dprintf(s
->avctx
, " %d", s
->intra_matrix
[s
->dsp
.idct_permutation
[i
]]);
2002 dprintf(s
->avctx
, "\n");
2006 j
= s
->dsp
.idct_permutation
[i
];
2007 v
= ff_mpeg1_default_intra_matrix
[i
];
2008 s
->intra_matrix
[j
] = v
;
2009 s
->chroma_intra_matrix
[j
] = v
;
2012 if (get_bits1(&s
->gb
)) {
2014 v
= get_bits(&s
->gb
, 8);
2016 av_log(s
->avctx
, AV_LOG_ERROR
, "inter matrix damaged\n");
2019 j
= s
->dsp
.idct_permutation
[ ff_zigzag_direct
[i
] ];
2020 s
->inter_matrix
[j
] = v
;
2021 s
->chroma_inter_matrix
[j
] = v
;
2024 dprintf(s
->avctx
, "non intra matrix present\n");
2026 dprintf(s
->avctx
, " %d", s
->inter_matrix
[s
->dsp
.idct_permutation
[i
]]);
2027 dprintf(s
->avctx
, "\n");
2031 int j
= s
->dsp
.idct_permutation
[i
];
2032 v
= ff_mpeg1_default_non_intra_matrix
[i
];
2033 s
->inter_matrix
[j
] = v
;
2034 s
->chroma_inter_matrix
[j
] = v
;
2038 if(show_bits(&s
->gb
, 23) != 0){
2039 av_log(s
->avctx
, AV_LOG_ERROR
, "sequence header damaged\n");
2043 /* we set mpeg2 parameters so that it emulates mpeg1 */
2044 s
->progressive_sequence
= 1;
2045 s
->progressive_frame
= 1;
2046 s
->picture_structure
= PICT_FRAME
;
2047 s
->frame_pred_frame_dct
= 1;
2048 s
->chroma_format
= 1;
2049 s
->codec_id
= s
->avctx
->codec_id
= CODEC_ID_MPEG1VIDEO
;
2050 avctx
->sub_id
= 1; /* indicates mpeg1 */
2051 s
->out_format
= FMT_MPEG1
;
2052 s
->swap_uv
= 0;//AFAIK VCR2 don't have SEQ_HEADER
2053 if(s
->flags
& CODEC_FLAG_LOW_DELAY
) s
->low_delay
=1;
2055 if(s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
2056 av_log(s
->avctx
, AV_LOG_DEBUG
, "vbv buffer: %d, bitrate:%d\n",
2057 s
->avctx
->rc_buffer_size
, s
->bit_rate
);
2062 static int vcr2_init_sequence(AVCodecContext
*avctx
)
2064 Mpeg1Context
*s1
= avctx
->priv_data
;
2065 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
2068 /* start new mpeg1 context decoding */
2069 s
->out_format
= FMT_MPEG1
;
2070 if (s1
->mpeg_enc_ctx_allocated
) {
2073 s
->width
= avctx
->coded_width
;
2074 s
->height
= avctx
->coded_height
;
2075 avctx
->has_b_frames
= 0; //true?
2078 if(avctx
->xvmc_acceleration
){
2079 avctx
->pix_fmt
= avctx
->get_format(avctx
,pixfmt_xvmc_mpg2_420
);
2081 avctx
->pix_fmt
= avctx
->get_format(avctx
,pixfmt_yuv_420
);
2084 if( avctx
->pix_fmt
== PIX_FMT_XVMC_MPEG2_IDCT
)
2085 if( avctx
->idct_algo
== FF_IDCT_AUTO
)
2086 avctx
->idct_algo
= FF_IDCT_SIMPLE
;
2088 if (MPV_common_init(s
) < 0)
2090 exchange_uv(s
);//common init reset pblocks, so we swap them here
2091 s
->swap_uv
= 1;// in case of xvmc we need to swap uv for each MB
2092 s1
->mpeg_enc_ctx_allocated
= 1;
2095 int j
= s
->dsp
.idct_permutation
[i
];
2096 v
= ff_mpeg1_default_intra_matrix
[i
];
2097 s
->intra_matrix
[j
] = v
;
2098 s
->chroma_intra_matrix
[j
] = v
;
2100 v
= ff_mpeg1_default_non_intra_matrix
[i
];
2101 s
->inter_matrix
[j
] = v
;
2102 s
->chroma_inter_matrix
[j
] = v
;
2105 s
->progressive_sequence
= 1;
2106 s
->progressive_frame
= 1;
2107 s
->picture_structure
= PICT_FRAME
;
2108 s
->frame_pred_frame_dct
= 1;
2109 s
->chroma_format
= 1;
2110 s
->codec_id
= s
->avctx
->codec_id
= CODEC_ID_MPEG2VIDEO
;
2111 avctx
->sub_id
= 2; /* indicates mpeg2 */
2116 static void mpeg_decode_user_data(AVCodecContext
*avctx
,
2117 const uint8_t *buf
, int buf_size
)
2124 /* we parse the DTG active format information */
2126 p
[0] == 'D' && p
[1] == 'T' && p
[2] == 'G' && p
[3] == '1') {
2140 avctx
->dtg_active_format
= p
[0] & 0x0f;
2145 static void mpeg_decode_gop(AVCodecContext
*avctx
,
2146 const uint8_t *buf
, int buf_size
){
2147 Mpeg1Context
*s1
= avctx
->priv_data
;
2148 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
2150 int drop_frame_flag
;
2151 int time_code_hours
, time_code_minutes
;
2152 int time_code_seconds
, time_code_pictures
;
2155 init_get_bits(&s
->gb
, buf
, buf_size
*8);
2157 drop_frame_flag
= get_bits1(&s
->gb
);
2159 time_code_hours
=get_bits(&s
->gb
,5);
2160 time_code_minutes
= get_bits(&s
->gb
,6);
2161 skip_bits1(&s
->gb
);//marker bit
2162 time_code_seconds
= get_bits(&s
->gb
,6);
2163 time_code_pictures
= get_bits(&s
->gb
,6);
2165 /*broken_link indicate that after editing the
2166 reference frames of the first B-Frames after GOP I-Frame
2167 are missing (open gop)*/
2168 broken_link
= get_bits1(&s
->gb
);
2170 if(s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
2171 av_log(s
->avctx
, AV_LOG_DEBUG
, "GOP (%2d:%02d:%02d.[%02d]) broken_link=%d\n",
2172 time_code_hours
, time_code_minutes
, time_code_seconds
,
2173 time_code_pictures
, broken_link
);
2176 * finds the end of the current frame in the bitstream.
2177 * @return the position of the first byte of the next frame, or -1
2179 int ff_mpeg1_find_frame_end(ParseContext
*pc
, const uint8_t *buf
, int buf_size
)
2182 uint32_t state
= pc
->state
;
2184 /* EOF considered as end of frame */
2189 0 frame start -> 1/4
2190 1 first_SEQEXT -> 0/2
2191 2 first field start -> 3/0
2192 3 second_SEQEXT -> 2/0
2196 for(i
=0; i
<buf_size
; i
++){
2197 assert(pc
->frame_start_found
>=0 && pc
->frame_start_found
<=4);
2198 if(pc
->frame_start_found
&1){
2199 if(state
== EXT_START_CODE
&& (buf
[i
]&0xF0) != 0x80)
2200 pc
->frame_start_found
--;
2201 else if(state
== EXT_START_CODE
+2){
2202 if((buf
[i
]&3) == 3) pc
->frame_start_found
= 0;
2203 else pc
->frame_start_found
= (pc
->frame_start_found
+1)&3;
2207 i
= ff_find_start_code(buf
+i
, buf
+buf_size
, &state
) - buf
- 1;
2208 if(pc
->frame_start_found
==0 && state
>= SLICE_MIN_START_CODE
&& state
<= SLICE_MAX_START_CODE
){
2210 pc
->frame_start_found
=4;
2212 if(state
== SEQ_END_CODE
){
2216 if(pc
->frame_start_found
==2 && state
== SEQ_START_CODE
)
2217 pc
->frame_start_found
= 0;
2218 if(pc
->frame_start_found
<4 && state
== EXT_START_CODE
)
2219 pc
->frame_start_found
++;
2220 if(pc
->frame_start_found
== 4 && (state
&0xFFFFFF00) == 0x100){
2221 if(state
< SLICE_MIN_START_CODE
|| state
> SLICE_MAX_START_CODE
){
2222 pc
->frame_start_found
=0;
2230 return END_NOT_FOUND
;
2233 static int decode_chunks(AVCodecContext
*avctx
,
2234 AVFrame
*picture
, int *data_size
,
2235 const uint8_t *buf
, int buf_size
);
2237 /* handle buffering and image synchronisation */
2238 static int mpeg_decode_frame(AVCodecContext
*avctx
,
2239 void *data
, int *data_size
,
2240 const uint8_t *buf
, int buf_size
)
2242 Mpeg1Context
*s
= avctx
->priv_data
;
2243 AVFrame
*picture
= data
;
2244 MpegEncContext
*s2
= &s
->mpeg_enc_ctx
;
2245 dprintf(avctx
, "fill_buffer\n");
2247 if (buf_size
== 0 || (buf_size
== 4 && AV_RB32(buf
) == SEQ_END_CODE
)) {
2248 /* special case for last picture */
2249 if (s2
->low_delay
==0 && s2
->next_picture_ptr
) {
2250 *picture
= *(AVFrame
*)s2
->next_picture_ptr
;
2251 s2
->next_picture_ptr
= NULL
;
2253 *data_size
= sizeof(AVFrame
);
2258 if(s2
->flags
&CODEC_FLAG_TRUNCATED
){
2259 int next
= ff_mpeg1_find_frame_end(&s2
->parse_context
, buf
, buf_size
);
2261 if( ff_combine_frame(&s2
->parse_context
, next
, (const uint8_t **)&buf
, &buf_size
) < 0 )
2266 if (s
->repeat_field
% 2 == 1) {
2268 //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
2269 // s2->picture_number, s->repeat_field);
2270 if (avctx
->flags
& CODEC_FLAG_REPEAT_FIELD
) {
2271 *data_size
= sizeof(AVPicture
);
2277 if(s
->mpeg_enc_ctx_allocated
==0 && avctx
->codec_tag
== ff_get_fourcc("VCR2"))
2278 vcr2_init_sequence(avctx
);
2282 if(avctx
->extradata
&& !avctx
->frame_number
)
2283 decode_chunks(avctx
, picture
, data_size
, avctx
->extradata
, avctx
->extradata_size
);
2285 return decode_chunks(avctx
, picture
, data_size
, buf
, buf_size
);
2288 static int decode_chunks(AVCodecContext
*avctx
,
2289 AVFrame
*picture
, int *data_size
,
2290 const uint8_t *buf
, int buf_size
)
2292 Mpeg1Context
*s
= avctx
->priv_data
;
2293 MpegEncContext
*s2
= &s
->mpeg_enc_ctx
;
2294 const uint8_t *buf_ptr
= buf
;
2295 const uint8_t *buf_end
= buf
+ buf_size
;
2296 int ret
, input_size
;
2299 /* find start next code */
2300 uint32_t start_code
= -1;
2301 buf_ptr
= ff_find_start_code(buf_ptr
,buf_end
, &start_code
);
2302 if (start_code
> 0x1ff){
2303 if(s2
->pict_type
!= FF_B_TYPE
|| avctx
->skip_frame
<= AVDISCARD_DEFAULT
){
2304 if(avctx
->thread_count
> 1){
2307 avctx
->execute(avctx
, slice_decode_thread
, (void**)&(s2
->thread_context
[0]), NULL
, s
->slice_count
);
2308 for(i
=0; i
<s
->slice_count
; i
++)
2309 s2
->error_count
+= s2
->thread_context
[i
]->error_count
;
2311 if (slice_end(avctx
, picture
)) {
2312 if(s2
->last_picture_ptr
|| s2
->low_delay
) //FIXME merge with the stuff in mpeg_decode_slice
2313 *data_size
= sizeof(AVPicture
);
2316 return FFMAX(0, buf_ptr
- buf
- s2
->parse_context
.last_index
);
2319 input_size
= buf_end
- buf_ptr
;
2321 if(avctx
->debug
& FF_DEBUG_STARTCODE
){
2322 av_log(avctx
, AV_LOG_DEBUG
, "%3X at %td left %d\n", start_code
, buf_ptr
-buf
, input_size
);
2325 /* prepare data for next start code */
2326 switch(start_code
) {
2327 case SEQ_START_CODE
:
2328 mpeg1_decode_sequence(avctx
, buf_ptr
,
2332 case PICTURE_START_CODE
:
2333 /* we have a complete image : we try to decompress it */
2334 mpeg1_decode_picture(avctx
,
2335 buf_ptr
, input_size
);
2337 case EXT_START_CODE
:
2338 mpeg_decode_extension(avctx
,
2339 buf_ptr
, input_size
);
2341 case USER_START_CODE
:
2342 mpeg_decode_user_data(avctx
,
2343 buf_ptr
, input_size
);
2345 case GOP_START_CODE
:
2347 mpeg_decode_gop(avctx
,
2348 buf_ptr
, input_size
);
2351 if (start_code
>= SLICE_MIN_START_CODE
&&
2352 start_code
<= SLICE_MAX_START_CODE
) {
2353 int mb_y
= start_code
- SLICE_MIN_START_CODE
;
2355 if(s2
->last_picture_ptr
==NULL
){
2356 /* Skip B-frames if we do not have reference frames. */
2357 if(s2
->pict_type
==FF_B_TYPE
) break;
2359 if(s2
->next_picture_ptr
==NULL
){
2360 /* Skip P-frames if we do not have reference frame no valid header. */
2361 if(s2
->pict_type
==FF_P_TYPE
&& (s2
->first_field
|| s2
->picture_structure
==PICT_FRAME
)) break;
2363 /* Skip B-frames if we are in a hurry. */
2364 if(avctx
->hurry_up
&& s2
->pict_type
==FF_B_TYPE
) break;
2365 if( (avctx
->skip_frame
>= AVDISCARD_NONREF
&& s2
->pict_type
==FF_B_TYPE
)
2366 ||(avctx
->skip_frame
>= AVDISCARD_NONKEY
&& s2
->pict_type
!=FF_I_TYPE
)
2367 || avctx
->skip_frame
>= AVDISCARD_ALL
)
2369 /* Skip everything if we are in a hurry>=5. */
2370 if(avctx
->hurry_up
>=5) break;
2372 if (!s
->mpeg_enc_ctx_allocated
) break;
2374 if(s2
->codec_id
== CODEC_ID_MPEG2VIDEO
){
2375 if(mb_y
< avctx
->skip_top
|| mb_y
>= s2
->mb_height
- avctx
->skip_bottom
)
2379 if(s2
->first_slice
){
2381 if(mpeg_field_start(s2
) < 0)
2384 if(!s2
->current_picture_ptr
){
2385 av_log(avctx
, AV_LOG_ERROR
, "current_picture not initialized\n");
2389 if(avctx
->thread_count
> 1){
2390 int threshold
= (s2
->mb_height
*s
->slice_count
+ avctx
->thread_count
/2) / avctx
->thread_count
;
2391 if(threshold
<= mb_y
){
2392 MpegEncContext
*thread_context
= s2
->thread_context
[s
->slice_count
];
2394 thread_context
->start_mb_y
= mb_y
;
2395 thread_context
->end_mb_y
= s2
->mb_height
;
2397 s2
->thread_context
[s
->slice_count
-1]->end_mb_y
= mb_y
;
2398 ff_update_duplicate_context(thread_context
, s2
);
2400 init_get_bits(&thread_context
->gb
, buf_ptr
, input_size
*8);
2403 buf_ptr
+= 2; //FIXME add minimum num of bytes per slice
2405 ret
= mpeg_decode_slice(s
, mb_y
, &buf_ptr
, input_size
);
2409 if(s2
->resync_mb_x
>=0 && s2
->resync_mb_y
>=0)
2410 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
);
2412 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
);
2421 static int mpeg_decode_end(AVCodecContext
*avctx
)
2423 Mpeg1Context
*s
= avctx
->priv_data
;
2425 if (s
->mpeg_enc_ctx_allocated
)
2426 MPV_common_end(&s
->mpeg_enc_ctx
);
2430 AVCodec mpeg1video_decoder
= {
2433 CODEC_ID_MPEG1VIDEO
,
2434 sizeof(Mpeg1Context
),
2439 CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
| CODEC_CAP_TRUNCATED
| CODEC_CAP_DELAY
,
2440 .flush
= ff_mpeg_flush
,
2441 .long_name
= "MPEG-1 video",
2444 AVCodec mpeg2video_decoder
= {
2447 CODEC_ID_MPEG2VIDEO
,
2448 sizeof(Mpeg1Context
),
2453 CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
| CODEC_CAP_TRUNCATED
| CODEC_CAP_DELAY
,
2454 .flush
= ff_mpeg_flush
,
2455 .long_name
= "MPEG-2 video",
2459 AVCodec mpegvideo_decoder
= {
2462 CODEC_ID_MPEG2VIDEO
,
2463 sizeof(Mpeg1Context
),
2468 CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
| CODEC_CAP_TRUNCATED
| CODEC_CAP_DELAY
,
2469 .flush
= ff_mpeg_flush
,
2470 .long_name
= "MPEG-1 video",
2474 static av_cold
int mpeg_mc_decode_init(AVCodecContext
*avctx
){
2477 if( avctx
->thread_count
> 1)
2479 if( !(avctx
->slice_flags
& SLICE_FLAG_CODED_ORDER
) )
2481 if( !(avctx
->slice_flags
& SLICE_FLAG_ALLOW_FIELD
) ){
2482 dprintf(avctx
, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2484 mpeg_decode_init(avctx
);
2485 s
= avctx
->priv_data
;
2487 avctx
->pix_fmt
= PIX_FMT_XVMC_MPEG2_IDCT
;
2488 avctx
->xvmc_acceleration
= 2;//2 - the blocks are packed!
2493 AVCodec mpeg_xvmc_decoder
= {
2496 CODEC_ID_MPEG2VIDEO_XVMC
,
2497 sizeof(Mpeg1Context
),
2498 mpeg_mc_decode_init
,
2502 CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
| CODEC_CAP_TRUNCATED
| CODEC_CAP_HWACCEL
| CODEC_CAP_DELAY
,
2503 .flush
= ff_mpeg_flush
,
2504 .long_name
= "MPEG-1 video XvMC (X-Video Motion Compensation)",
2509 /* this is ugly i know, but the alternative is too make
2510 hundreds of vars global and prefix them with ff_mpeg1_
2511 which is far uglier. */