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"
43 #define MBINCR_VLC_BITS 9
44 #define MB_PAT_VLC_BITS 9
45 #define MB_PTYPE_VLC_BITS 6
46 #define MB_BTYPE_VLC_BITS 6
48 static inline int mpeg1_decode_block_inter(MpegEncContext
*s
,
51 static inline int mpeg1_decode_block_intra(MpegEncContext
*s
,
54 static inline int mpeg1_fast_decode_block_inter(MpegEncContext
*s
, DCTELEM
*block
, int n
);
55 static inline int mpeg2_decode_block_non_intra(MpegEncContext
*s
,
58 static inline int mpeg2_decode_block_intra(MpegEncContext
*s
,
61 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext
*s
, DCTELEM
*block
, int n
);
62 static inline int mpeg2_fast_decode_block_intra(MpegEncContext
*s
, DCTELEM
*block
, int n
);
63 static int mpeg_decode_motion(MpegEncContext
*s
, int fcode
, int pred
);
64 static void exchange_uv(MpegEncContext
*s
);
66 extern int XVMC_field_start(MpegEncContext
*s
, AVCodecContext
*avctx
);
67 extern int XVMC_field_end(MpegEncContext
*s
);
68 extern void XVMC_pack_pblocks(MpegEncContext
*s
,int cbp
);
69 extern void XVMC_init_block(MpegEncContext
*s
);//set s->block
71 extern int ff_VDPAU_mpeg_field_start(MpegEncContext
*s
);
72 extern void ff_VDPAU_mpeg_picture_complete(MpegEncContext
*s
, const uint8_t *buf
, int buf_size
, int slice_count
);
74 static const enum PixelFormat pixfmt_xvmc_mpg2_420
[] = {
75 PIX_FMT_XVMC_MPEG2_IDCT
,
76 PIX_FMT_XVMC_MPEG2_MC
,
78 static const enum PixelFormat pixfmt_vdpau_mpg1_420
[] = {
81 static const enum PixelFormat pixfmt_vdpau_mpg2simple_420
[] = {
82 PIX_FMT_VDPAU_MPEG2_SIMPLE
,
84 static const enum PixelFormat pixfmt_vdpau_mpg2main_420
[] = {
85 PIX_FMT_VDPAU_MPEG2_MAIN
,
88 uint8_t ff_mpeg12_static_rl_table_store
[2][2][2*MAX_RUN
+ MAX_LEVEL
+ 3];
91 #define INIT_2D_VLC_RL(rl, static_size)\
93 static RL_VLC_ELEM rl_vlc_table[static_size];\
94 INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
95 &rl.table_vlc[0][1], 4, 2,\
96 &rl.table_vlc[0][0], 4, 2, static_size);\
98 rl.rl_vlc[0]= rl_vlc_table;\
102 static void init_2d_vlc_rl(RLTable
*rl
)
106 for(i
=0; i
<rl
->vlc
.table_size
; i
++){
107 int code
= rl
->vlc
.table
[i
][0];
108 int len
= rl
->vlc
.table
[i
][1];
111 if(len
==0){ // illegal code
114 }else if(len
<0){ //more bits needed
118 if(code
==rl
->n
){ //esc
121 }else if(code
==rl
->n
+1){ //eob
125 run
= rl
->table_run
[code
] + 1;
126 level
= rl
->table_level
[code
];
129 rl
->rl_vlc
[0][i
].len
= len
;
130 rl
->rl_vlc
[0][i
].level
= level
;
131 rl
->rl_vlc
[0][i
].run
= run
;
135 void ff_mpeg12_common_init(MpegEncContext
*s
)
139 s
->c_dc_scale_table
= mpeg2_dc_scale_table
[s
->intra_dc_precision
];
143 void ff_mpeg1_clean_buffers(MpegEncContext
*s
){
144 s
->last_dc
[0] = 1 << (7 + s
->intra_dc_precision
);
145 s
->last_dc
[1] = s
->last_dc
[0];
146 s
->last_dc
[2] = s
->last_dc
[0];
147 memset(s
->last_mv
, 0, sizeof(s
->last_mv
));
151 /******************************************/
155 static VLC mbincr_vlc
;
156 static VLC mb_ptype_vlc
;
157 static VLC mb_btype_vlc
;
158 static VLC mb_pat_vlc
;
160 av_cold
void ff_mpeg12_init_vlcs(void)
167 INIT_VLC_STATIC(&dc_lum_vlc
, DC_VLC_BITS
, 12,
168 ff_mpeg12_vlc_dc_lum_bits
, 1, 1,
169 ff_mpeg12_vlc_dc_lum_code
, 2, 2, 512);
170 INIT_VLC_STATIC(&dc_chroma_vlc
, DC_VLC_BITS
, 12,
171 ff_mpeg12_vlc_dc_chroma_bits
, 1, 1,
172 ff_mpeg12_vlc_dc_chroma_code
, 2, 2, 514);
173 INIT_VLC_STATIC(&mv_vlc
, MV_VLC_BITS
, 17,
174 &ff_mpeg12_mbMotionVectorTable
[0][1], 2, 1,
175 &ff_mpeg12_mbMotionVectorTable
[0][0], 2, 1, 518);
176 INIT_VLC_STATIC(&mbincr_vlc
, MBINCR_VLC_BITS
, 36,
177 &ff_mpeg12_mbAddrIncrTable
[0][1], 2, 1,
178 &ff_mpeg12_mbAddrIncrTable
[0][0], 2, 1, 538);
179 INIT_VLC_STATIC(&mb_pat_vlc
, MB_PAT_VLC_BITS
, 64,
180 &ff_mpeg12_mbPatTable
[0][1], 2, 1,
181 &ff_mpeg12_mbPatTable
[0][0], 2, 1, 512);
183 INIT_VLC_STATIC(&mb_ptype_vlc
, MB_PTYPE_VLC_BITS
, 7,
184 &table_mb_ptype
[0][1], 2, 1,
185 &table_mb_ptype
[0][0], 2, 1, 64);
186 INIT_VLC_STATIC(&mb_btype_vlc
, MB_BTYPE_VLC_BITS
, 11,
187 &table_mb_btype
[0][1], 2, 1,
188 &table_mb_btype
[0][0], 2, 1, 64);
189 init_rl(&ff_rl_mpeg1
, ff_mpeg12_static_rl_table_store
[0]);
190 init_rl(&ff_rl_mpeg2
, ff_mpeg12_static_rl_table_store
[1]);
192 INIT_2D_VLC_RL(ff_rl_mpeg1
, 680);
193 INIT_2D_VLC_RL(ff_rl_mpeg2
, 674);
197 static inline int get_dmv(MpegEncContext
*s
)
199 if(get_bits1(&s
->gb
))
200 return 1 - (get_bits1(&s
->gb
) << 1);
205 static inline int get_qscale(MpegEncContext
*s
)
207 int qscale
= get_bits(&s
->gb
, 5);
208 if (s
->q_scale_type
) {
209 return non_linear_qscale
[qscale
];
215 /* motion type (for MPEG-2) */
221 static int mpeg_decode_mb(MpegEncContext
*s
,
222 DCTELEM block
[12][64])
224 int i
, j
, k
, cbp
, val
, mb_type
, motion_type
;
225 const int mb_block_count
= 4 + (1<< s
->chroma_format
);
227 dprintf(s
->avctx
, "decode_mb: x=%d y=%d\n", s
->mb_x
, s
->mb_y
);
229 assert(s
->mb_skipped
==0);
231 if (s
->mb_skip_run
-- != 0) {
232 if (s
->pict_type
== FF_P_TYPE
) {
234 s
->current_picture
.mb_type
[ s
->mb_x
+ s
->mb_y
*s
->mb_stride
]= MB_TYPE_SKIP
| MB_TYPE_L0
| MB_TYPE_16x16
;
239 mb_type
= s
->current_picture
.mb_type
[ s
->mb_x
+ s
->mb_y
*s
->mb_stride
- 1];
241 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
242 if(IS_INTRA(mb_type
))
245 s
->current_picture
.mb_type
[ s
->mb_x
+ s
->mb_y
*s
->mb_stride
]=
246 mb_type
| MB_TYPE_SKIP
;
247 // assert(s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1]&(MB_TYPE_16x16|MB_TYPE_16x8));
249 if((s
->mv
[0][0][0]|s
->mv
[0][0][1]|s
->mv
[1][0][0]|s
->mv
[1][0][1])==0)
256 switch(s
->pict_type
) {
259 if (get_bits1(&s
->gb
) == 0) {
260 if (get_bits1(&s
->gb
) == 0){
261 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid mb type in I Frame at %d %d\n", s
->mb_x
, s
->mb_y
);
264 mb_type
= MB_TYPE_QUANT
| MB_TYPE_INTRA
;
266 mb_type
= MB_TYPE_INTRA
;
270 mb_type
= get_vlc2(&s
->gb
, mb_ptype_vlc
.table
, MB_PTYPE_VLC_BITS
, 1);
272 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid mb type in P Frame at %d %d\n", s
->mb_x
, s
->mb_y
);
275 mb_type
= ptype2mb_type
[ mb_type
];
278 mb_type
= get_vlc2(&s
->gb
, mb_btype_vlc
.table
, MB_BTYPE_VLC_BITS
, 1);
280 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid mb type in B Frame at %d %d\n", s
->mb_x
, s
->mb_y
);
283 mb_type
= btype2mb_type
[ mb_type
];
286 dprintf(s
->avctx
, "mb_type=%x\n", mb_type
);
287 // motion_type = 0; /* avoid warning */
288 if (IS_INTRA(mb_type
)) {
289 s
->dsp
.clear_blocks(s
->block
[0]);
291 if(!s
->chroma_y_shift
){
292 s
->dsp
.clear_blocks(s
->block
[6]);
295 /* compute DCT type */
296 if (s
->picture_structure
== PICT_FRAME
&& //FIXME add an interlaced_dct coded var?
297 !s
->frame_pred_frame_dct
) {
298 s
->interlaced_dct
= get_bits1(&s
->gb
);
301 if (IS_QUANT(mb_type
))
302 s
->qscale
= get_qscale(s
);
304 if (s
->concealment_motion_vectors
) {
305 /* just parse them */
306 if (s
->picture_structure
!= PICT_FRAME
)
307 skip_bits1(&s
->gb
); /* field select */
309 s
->mv
[0][0][0]= s
->last_mv
[0][0][0]= s
->last_mv
[0][1][0] =
310 mpeg_decode_motion(s
, s
->mpeg_f_code
[0][0], s
->last_mv
[0][0][0]);
311 s
->mv
[0][0][1]= s
->last_mv
[0][0][1]= s
->last_mv
[0][1][1] =
312 mpeg_decode_motion(s
, s
->mpeg_f_code
[0][1], s
->last_mv
[0][0][1]);
314 skip_bits1(&s
->gb
); /* marker */
316 memset(s
->last_mv
, 0, sizeof(s
->last_mv
)); /* reset mv prediction */
319 //if 1, we memcpy blocks in xvmcvideo
320 if(s
->avctx
->xvmc_acceleration
> 1){
321 XVMC_pack_pblocks(s
,-1);//inter are always full blocks
328 if (s
->codec_id
== CODEC_ID_MPEG2VIDEO
) {
329 if(s
->flags2
& CODEC_FLAG2_FAST
){
331 mpeg2_fast_decode_block_intra(s
, s
->pblocks
[i
], i
);
334 for(i
=0;i
<mb_block_count
;i
++) {
335 if (mpeg2_decode_block_intra(s
, s
->pblocks
[i
], i
) < 0)
341 if (mpeg1_decode_block_intra(s
, s
->pblocks
[i
], i
) < 0)
346 if (mb_type
& MB_TYPE_ZERO_MV
){
347 assert(mb_type
& MB_TYPE_CBP
);
349 s
->mv_dir
= MV_DIR_FORWARD
;
350 if(s
->picture_structure
== PICT_FRAME
){
351 if(!s
->frame_pred_frame_dct
)
352 s
->interlaced_dct
= get_bits1(&s
->gb
);
353 s
->mv_type
= MV_TYPE_16X16
;
355 s
->mv_type
= MV_TYPE_FIELD
;
356 mb_type
|= MB_TYPE_INTERLACED
;
357 s
->field_select
[0][0]= s
->picture_structure
- 1;
360 if (IS_QUANT(mb_type
))
361 s
->qscale
= get_qscale(s
);
363 s
->last_mv
[0][0][0] = 0;
364 s
->last_mv
[0][0][1] = 0;
365 s
->last_mv
[0][1][0] = 0;
366 s
->last_mv
[0][1][1] = 0;
370 assert(mb_type
& MB_TYPE_L0L1
);
371 //FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
372 /* get additional motion vector type */
373 if (s
->frame_pred_frame_dct
)
374 motion_type
= MT_FRAME
;
376 motion_type
= get_bits(&s
->gb
, 2);
377 if (s
->picture_structure
== PICT_FRAME
&& HAS_CBP(mb_type
))
378 s
->interlaced_dct
= get_bits1(&s
->gb
);
381 if (IS_QUANT(mb_type
))
382 s
->qscale
= get_qscale(s
);
385 s
->mv_dir
= (mb_type
>>13)&3;
386 dprintf(s
->avctx
, "motion_type=%d\n", motion_type
);
387 switch(motion_type
) {
388 case MT_FRAME
: /* or MT_16X8 */
389 if (s
->picture_structure
== PICT_FRAME
) {
390 mb_type
|= MB_TYPE_16x16
;
391 s
->mv_type
= MV_TYPE_16X16
;
393 if (USES_LIST(mb_type
, i
)) {
395 s
->mv
[i
][0][0]= s
->last_mv
[i
][0][0]= s
->last_mv
[i
][1][0] =
396 mpeg_decode_motion(s
, s
->mpeg_f_code
[i
][0], s
->last_mv
[i
][0][0]);
397 s
->mv
[i
][0][1]= s
->last_mv
[i
][0][1]= s
->last_mv
[i
][1][1] =
398 mpeg_decode_motion(s
, s
->mpeg_f_code
[i
][1], s
->last_mv
[i
][0][1]);
399 /* full_pel: only for MPEG-1 */
401 s
->mv
[i
][0][0] <<= 1;
402 s
->mv
[i
][0][1] <<= 1;
407 mb_type
|= MB_TYPE_16x8
| MB_TYPE_INTERLACED
;
408 s
->mv_type
= MV_TYPE_16X8
;
410 if (USES_LIST(mb_type
, i
)) {
413 s
->field_select
[i
][j
] = get_bits1(&s
->gb
);
415 val
= mpeg_decode_motion(s
, s
->mpeg_f_code
[i
][k
],
416 s
->last_mv
[i
][j
][k
]);
417 s
->last_mv
[i
][j
][k
] = val
;
418 s
->mv
[i
][j
][k
] = val
;
426 s
->mv_type
= MV_TYPE_FIELD
;
427 if (s
->picture_structure
== PICT_FRAME
) {
428 mb_type
|= MB_TYPE_16x8
| MB_TYPE_INTERLACED
;
430 if (USES_LIST(mb_type
, i
)) {
432 s
->field_select
[i
][j
] = get_bits1(&s
->gb
);
433 val
= mpeg_decode_motion(s
, s
->mpeg_f_code
[i
][0],
434 s
->last_mv
[i
][j
][0]);
435 s
->last_mv
[i
][j
][0] = val
;
436 s
->mv
[i
][j
][0] = val
;
437 dprintf(s
->avctx
, "fmx=%d\n", val
);
438 val
= mpeg_decode_motion(s
, s
->mpeg_f_code
[i
][1],
439 s
->last_mv
[i
][j
][1] >> 1);
440 s
->last_mv
[i
][j
][1] = val
<< 1;
441 s
->mv
[i
][j
][1] = val
;
442 dprintf(s
->avctx
, "fmy=%d\n", val
);
447 mb_type
|= MB_TYPE_16x16
| MB_TYPE_INTERLACED
;
449 if (USES_LIST(mb_type
, i
)) {
450 s
->field_select
[i
][0] = get_bits1(&s
->gb
);
452 val
= mpeg_decode_motion(s
, s
->mpeg_f_code
[i
][k
],
453 s
->last_mv
[i
][0][k
]);
454 s
->last_mv
[i
][0][k
] = val
;
455 s
->last_mv
[i
][1][k
] = val
;
456 s
->mv
[i
][0][k
] = val
;
463 s
->mv_type
= MV_TYPE_DMV
;
465 if (USES_LIST(mb_type
, i
)) {
466 int dmx
, dmy
, mx
, my
, m
;
467 mx
= mpeg_decode_motion(s
, s
->mpeg_f_code
[i
][0],
468 s
->last_mv
[i
][0][0]);
469 s
->last_mv
[i
][0][0] = mx
;
470 s
->last_mv
[i
][1][0] = mx
;
472 my
= mpeg_decode_motion(s
, s
->mpeg_f_code
[i
][1],
473 s
->last_mv
[i
][0][1] >> 1);
477 s
->last_mv
[i
][0][1] = my
<<1;
478 s
->last_mv
[i
][1][1] = my
<<1;
482 s
->mv
[i
][1][0] = mx
;//not used
483 s
->mv
[i
][1][1] = my
;//not used
485 if (s
->picture_structure
== PICT_FRAME
) {
486 mb_type
|= MB_TYPE_16x16
| MB_TYPE_INTERLACED
;
488 //m = 1 + 2 * s->top_field_first;
489 m
= s
->top_field_first
? 1 : 3;
491 /* top -> top pred */
492 s
->mv
[i
][2][0] = ((mx
* m
+ (mx
> 0)) >> 1) + dmx
;
493 s
->mv
[i
][2][1] = ((my
* m
+ (my
> 0)) >> 1) + dmy
- 1;
495 s
->mv
[i
][3][0] = ((mx
* m
+ (mx
> 0)) >> 1) + dmx
;
496 s
->mv
[i
][3][1] = ((my
* m
+ (my
> 0)) >> 1) + dmy
+ 1;
498 mb_type
|= MB_TYPE_16x16
;
500 s
->mv
[i
][2][0] = ((mx
+ (mx
> 0)) >> 1) + dmx
;
501 s
->mv
[i
][2][1] = ((my
+ (my
> 0)) >> 1) + dmy
;
502 if(s
->picture_structure
== PICT_TOP_FIELD
)
511 av_log(s
->avctx
, AV_LOG_ERROR
, "00 motion_type at %d %d\n", s
->mb_x
, s
->mb_y
);
517 if (HAS_CBP(mb_type
)) {
518 s
->dsp
.clear_blocks(s
->block
[0]);
520 cbp
= get_vlc2(&s
->gb
, mb_pat_vlc
.table
, MB_PAT_VLC_BITS
, 1);
521 if(mb_block_count
> 6){
522 cbp
<<= mb_block_count
-6;
523 cbp
|= get_bits(&s
->gb
, mb_block_count
-6);
524 s
->dsp
.clear_blocks(s
->block
[6]);
527 av_log(s
->avctx
, AV_LOG_ERROR
, "invalid cbp at %d %d\n", s
->mb_x
, s
->mb_y
);
532 //if 1, we memcpy blocks in xvmcvideo
533 if(s
->avctx
->xvmc_acceleration
> 1){
534 XVMC_pack_pblocks(s
,cbp
);
541 if (s
->codec_id
== CODEC_ID_MPEG2VIDEO
) {
542 if(s
->flags2
& CODEC_FLAG2_FAST
){
545 mpeg2_fast_decode_block_non_intra(s
, s
->pblocks
[i
], i
);
547 s
->block_last_index
[i
] = -1;
552 cbp
<<= 12-mb_block_count
;
554 for(i
=0;i
<mb_block_count
;i
++) {
555 if ( cbp
& (1<<11) ) {
556 if (mpeg2_decode_block_non_intra(s
, s
->pblocks
[i
], i
) < 0)
559 s
->block_last_index
[i
] = -1;
565 if(s
->flags2
& CODEC_FLAG2_FAST
){
568 mpeg1_fast_decode_block_inter(s
, s
->pblocks
[i
], i
);
570 s
->block_last_index
[i
] = -1;
577 if (mpeg1_decode_block_inter(s
, s
->pblocks
[i
], i
) < 0)
580 s
->block_last_index
[i
] = -1;
588 s
->block_last_index
[i
] = -1;
592 s
->current_picture
.mb_type
[ s
->mb_x
+ s
->mb_y
*s
->mb_stride
]= mb_type
;
597 /* as H.263, but only 17 codes */
598 static int mpeg_decode_motion(MpegEncContext
*s
, int fcode
, int pred
)
600 int code
, sign
, val
, l
, shift
;
602 code
= get_vlc2(&s
->gb
, mv_vlc
.table
, MV_VLC_BITS
, 2);
610 sign
= get_bits1(&s
->gb
);
614 val
= (val
- 1) << shift
;
615 val
|= get_bits(&s
->gb
, shift
);
622 /* modulo decoding */
623 l
= INT_BIT
- 5 - shift
;
628 static inline int mpeg1_decode_block_intra(MpegEncContext
*s
,
632 int level
, dc
, diff
, i
, j
, run
;
634 RLTable
*rl
= &ff_rl_mpeg1
;
635 uint8_t * const scantable
= s
->intra_scantable
.permutated
;
636 const uint16_t *quant_matrix
= s
->intra_matrix
;
637 const int qscale
= s
->qscale
;
640 component
= (n
<= 3 ? 0 : n
- 4 + 1);
641 diff
= decode_dc(&s
->gb
, component
);
644 dc
= s
->last_dc
[component
];
646 s
->last_dc
[component
] = dc
;
648 dprintf(s
->avctx
, "dc=%d diff=%d\n", dc
, diff
);
651 OPEN_READER(re
, &s
->gb
);
652 /* now quantify & encode AC coefficients */
654 UPDATE_CACHE(re
, &s
->gb
);
655 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl
->rl_vlc
[0], TEX_VLC_BITS
, 2, 0);
659 } else if(level
!= 0) {
662 level
= (level
*qscale
*quant_matrix
[j
])>>4;
664 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
665 LAST_SKIP_BITS(re
, &s
->gb
, 1);
668 run
= SHOW_UBITS(re
, &s
->gb
, 6)+1; LAST_SKIP_BITS(re
, &s
->gb
, 6);
669 UPDATE_CACHE(re
, &s
->gb
);
670 level
= SHOW_SBITS(re
, &s
->gb
, 8); SKIP_BITS(re
, &s
->gb
, 8);
672 level
= SHOW_UBITS(re
, &s
->gb
, 8) - 256; LAST_SKIP_BITS(re
, &s
->gb
, 8);
673 } else if (level
== 0) {
674 level
= SHOW_UBITS(re
, &s
->gb
, 8) ; LAST_SKIP_BITS(re
, &s
->gb
, 8);
680 level
= (level
*qscale
*quant_matrix
[j
])>>4;
684 level
= (level
*qscale
*quant_matrix
[j
])>>4;
689 av_log(s
->avctx
, AV_LOG_ERROR
, "ac-tex damaged at %d %d\n", s
->mb_x
, s
->mb_y
);
695 CLOSE_READER(re
, &s
->gb
);
697 s
->block_last_index
[n
] = i
;
701 static inline int mpeg1_decode_block_inter(MpegEncContext
*s
,
705 int level
, i
, j
, run
;
706 RLTable
*rl
= &ff_rl_mpeg1
;
707 uint8_t * const scantable
= s
->intra_scantable
.permutated
;
708 const uint16_t *quant_matrix
= s
->inter_matrix
;
709 const int qscale
= s
->qscale
;
712 OPEN_READER(re
, &s
->gb
);
714 // special case for first coefficient, no need to add second VLC table
715 UPDATE_CACHE(re
, &s
->gb
);
716 if (((int32_t)GET_CACHE(re
, &s
->gb
)) < 0) {
717 level
= (3*qscale
*quant_matrix
[0])>>5;
719 if(GET_CACHE(re
, &s
->gb
)&0x40000000)
723 SKIP_BITS(re
, &s
->gb
, 2);
724 if(((int32_t)GET_CACHE(re
, &s
->gb
)) <= (int32_t)0xBFFFFFFF)
727 #if MIN_CACHE_BITS < 19
728 UPDATE_CACHE(re
, &s
->gb
);
730 /* now quantify & encode AC coefficients */
732 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl
->rl_vlc
[0], TEX_VLC_BITS
, 2, 0);
737 level
= ((level
*2+1)*qscale
*quant_matrix
[j
])>>5;
739 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
740 SKIP_BITS(re
, &s
->gb
, 1);
743 run
= SHOW_UBITS(re
, &s
->gb
, 6)+1; LAST_SKIP_BITS(re
, &s
->gb
, 6);
744 UPDATE_CACHE(re
, &s
->gb
);
745 level
= SHOW_SBITS(re
, &s
->gb
, 8); SKIP_BITS(re
, &s
->gb
, 8);
747 level
= SHOW_UBITS(re
, &s
->gb
, 8) - 256; SKIP_BITS(re
, &s
->gb
, 8);
748 } else if (level
== 0) {
749 level
= SHOW_UBITS(re
, &s
->gb
, 8) ; SKIP_BITS(re
, &s
->gb
, 8);
755 level
= ((level
*2+1)*qscale
*quant_matrix
[j
])>>5;
759 level
= ((level
*2+1)*qscale
*quant_matrix
[j
])>>5;
764 av_log(s
->avctx
, AV_LOG_ERROR
, "ac-tex damaged at %d %d\n", s
->mb_x
, s
->mb_y
);
769 #if MIN_CACHE_BITS < 19
770 UPDATE_CACHE(re
, &s
->gb
);
772 if(((int32_t)GET_CACHE(re
, &s
->gb
)) <= (int32_t)0xBFFFFFFF)
774 #if MIN_CACHE_BITS >= 19
775 UPDATE_CACHE(re
, &s
->gb
);
779 LAST_SKIP_BITS(re
, &s
->gb
, 2);
780 CLOSE_READER(re
, &s
->gb
);
782 s
->block_last_index
[n
] = i
;
786 static inline int mpeg1_fast_decode_block_inter(MpegEncContext
*s
, DCTELEM
*block
, int n
)
788 int level
, i
, j
, run
;
789 RLTable
*rl
= &ff_rl_mpeg1
;
790 uint8_t * const scantable
= s
->intra_scantable
.permutated
;
791 const int qscale
= s
->qscale
;
794 OPEN_READER(re
, &s
->gb
);
796 // special case for first coefficient, no need to add second VLC table
797 UPDATE_CACHE(re
, &s
->gb
);
798 if (((int32_t)GET_CACHE(re
, &s
->gb
)) < 0) {
799 level
= (3*qscale
)>>1;
801 if(GET_CACHE(re
, &s
->gb
)&0x40000000)
805 SKIP_BITS(re
, &s
->gb
, 2);
806 if(((int32_t)GET_CACHE(re
, &s
->gb
)) <= (int32_t)0xBFFFFFFF)
809 #if MIN_CACHE_BITS < 19
810 UPDATE_CACHE(re
, &s
->gb
);
813 /* now quantify & encode AC coefficients */
815 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl
->rl_vlc
[0], TEX_VLC_BITS
, 2, 0);
820 level
= ((level
*2+1)*qscale
)>>1;
822 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
823 SKIP_BITS(re
, &s
->gb
, 1);
826 run
= SHOW_UBITS(re
, &s
->gb
, 6)+1; LAST_SKIP_BITS(re
, &s
->gb
, 6);
827 UPDATE_CACHE(re
, &s
->gb
);
828 level
= SHOW_SBITS(re
, &s
->gb
, 8); SKIP_BITS(re
, &s
->gb
, 8);
830 level
= SHOW_UBITS(re
, &s
->gb
, 8) - 256; SKIP_BITS(re
, &s
->gb
, 8);
831 } else if (level
== 0) {
832 level
= SHOW_UBITS(re
, &s
->gb
, 8) ; SKIP_BITS(re
, &s
->gb
, 8);
838 level
= ((level
*2+1)*qscale
)>>1;
842 level
= ((level
*2+1)*qscale
)>>1;
848 #if MIN_CACHE_BITS < 19
849 UPDATE_CACHE(re
, &s
->gb
);
851 if(((int32_t)GET_CACHE(re
, &s
->gb
)) <= (int32_t)0xBFFFFFFF)
853 #if MIN_CACHE_BITS >= 19
854 UPDATE_CACHE(re
, &s
->gb
);
858 LAST_SKIP_BITS(re
, &s
->gb
, 2);
859 CLOSE_READER(re
, &s
->gb
);
861 s
->block_last_index
[n
] = i
;
866 static inline int mpeg2_decode_block_non_intra(MpegEncContext
*s
,
870 int level
, i
, j
, run
;
871 RLTable
*rl
= &ff_rl_mpeg1
;
872 uint8_t * const scantable
= s
->intra_scantable
.permutated
;
873 const uint16_t *quant_matrix
;
874 const int qscale
= s
->qscale
;
880 OPEN_READER(re
, &s
->gb
);
883 quant_matrix
= s
->inter_matrix
;
885 quant_matrix
= s
->chroma_inter_matrix
;
887 // special case for first coefficient, no need to add second VLC table
888 UPDATE_CACHE(re
, &s
->gb
);
889 if (((int32_t)GET_CACHE(re
, &s
->gb
)) < 0) {
890 level
= (3*qscale
*quant_matrix
[0])>>5;
891 if(GET_CACHE(re
, &s
->gb
)&0x40000000)
896 SKIP_BITS(re
, &s
->gb
, 2);
897 if(((int32_t)GET_CACHE(re
, &s
->gb
)) <= (int32_t)0xBFFFFFFF)
900 #if MIN_CACHE_BITS < 19
901 UPDATE_CACHE(re
, &s
->gb
);
904 /* now quantify & encode AC coefficients */
906 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl
->rl_vlc
[0], TEX_VLC_BITS
, 2, 0);
911 level
= ((level
*2+1)*qscale
*quant_matrix
[j
])>>5;
912 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
913 SKIP_BITS(re
, &s
->gb
, 1);
916 run
= SHOW_UBITS(re
, &s
->gb
, 6)+1; LAST_SKIP_BITS(re
, &s
->gb
, 6);
917 UPDATE_CACHE(re
, &s
->gb
);
918 level
= SHOW_SBITS(re
, &s
->gb
, 12); SKIP_BITS(re
, &s
->gb
, 12);
923 level
= ((-level
*2+1)*qscale
*quant_matrix
[j
])>>5;
926 level
= ((level
*2+1)*qscale
*quant_matrix
[j
])>>5;
930 av_log(s
->avctx
, AV_LOG_ERROR
, "ac-tex damaged at %d %d\n", s
->mb_x
, s
->mb_y
);
936 #if MIN_CACHE_BITS < 19
937 UPDATE_CACHE(re
, &s
->gb
);
939 if(((int32_t)GET_CACHE(re
, &s
->gb
)) <= (int32_t)0xBFFFFFFF)
941 #if MIN_CACHE_BITS >= 19
942 UPDATE_CACHE(re
, &s
->gb
);
946 LAST_SKIP_BITS(re
, &s
->gb
, 2);
947 CLOSE_READER(re
, &s
->gb
);
949 block
[63] ^= (mismatch
& 1);
951 s
->block_last_index
[n
] = i
;
955 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext
*s
,
959 int level
, i
, j
, run
;
960 RLTable
*rl
= &ff_rl_mpeg1
;
961 uint8_t * const scantable
= s
->intra_scantable
.permutated
;
962 const int qscale
= s
->qscale
;
963 OPEN_READER(re
, &s
->gb
);
966 // special case for first coefficient, no need to add second VLC table
967 UPDATE_CACHE(re
, &s
->gb
);
968 if (((int32_t)GET_CACHE(re
, &s
->gb
)) < 0) {
969 level
= (3*qscale
)>>1;
970 if(GET_CACHE(re
, &s
->gb
)&0x40000000)
974 SKIP_BITS(re
, &s
->gb
, 2);
975 if(((int32_t)GET_CACHE(re
, &s
->gb
)) <= (int32_t)0xBFFFFFFF)
978 #if MIN_CACHE_BITS < 19
979 UPDATE_CACHE(re
, &s
->gb
);
982 /* now quantify & encode AC coefficients */
984 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl
->rl_vlc
[0], TEX_VLC_BITS
, 2, 0);
989 level
= ((level
*2+1)*qscale
)>>1;
990 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
991 SKIP_BITS(re
, &s
->gb
, 1);
994 run
= SHOW_UBITS(re
, &s
->gb
, 6)+1; LAST_SKIP_BITS(re
, &s
->gb
, 6);
995 UPDATE_CACHE(re
, &s
->gb
);
996 level
= SHOW_SBITS(re
, &s
->gb
, 12); SKIP_BITS(re
, &s
->gb
, 12);
1001 level
= ((-level
*2+1)*qscale
)>>1;
1004 level
= ((level
*2+1)*qscale
)>>1;
1009 #if MIN_CACHE_BITS < 19
1010 UPDATE_CACHE(re
, &s
->gb
);
1012 if(((int32_t)GET_CACHE(re
, &s
->gb
)) <= (int32_t)0xBFFFFFFF)
1014 #if MIN_CACHE_BITS >=19
1015 UPDATE_CACHE(re
, &s
->gb
);
1019 LAST_SKIP_BITS(re
, &s
->gb
, 2);
1020 CLOSE_READER(re
, &s
->gb
);
1021 s
->block_last_index
[n
] = i
;
1026 static inline int mpeg2_decode_block_intra(MpegEncContext
*s
,
1030 int level
, dc
, diff
, i
, j
, run
;
1033 uint8_t * const scantable
= s
->intra_scantable
.permutated
;
1034 const uint16_t *quant_matrix
;
1035 const int qscale
= s
->qscale
;
1038 /* DC coefficient */
1040 quant_matrix
= s
->intra_matrix
;
1043 quant_matrix
= s
->chroma_intra_matrix
;
1044 component
= (n
&1) + 1;
1046 diff
= decode_dc(&s
->gb
, component
);
1049 dc
= s
->last_dc
[component
];
1051 s
->last_dc
[component
] = dc
;
1052 block
[0] = dc
<< (3 - s
->intra_dc_precision
);
1053 dprintf(s
->avctx
, "dc=%d\n", block
[0]);
1054 mismatch
= block
[0] ^ 1;
1056 if (s
->intra_vlc_format
)
1062 OPEN_READER(re
, &s
->gb
);
1063 /* now quantify & encode AC coefficients */
1065 UPDATE_CACHE(re
, &s
->gb
);
1066 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl
->rl_vlc
[0], TEX_VLC_BITS
, 2, 0);
1070 } else if(level
!= 0) {
1073 level
= (level
*qscale
*quant_matrix
[j
])>>4;
1074 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
1075 LAST_SKIP_BITS(re
, &s
->gb
, 1);
1078 run
= SHOW_UBITS(re
, &s
->gb
, 6)+1; LAST_SKIP_BITS(re
, &s
->gb
, 6);
1079 UPDATE_CACHE(re
, &s
->gb
);
1080 level
= SHOW_SBITS(re
, &s
->gb
, 12); SKIP_BITS(re
, &s
->gb
, 12);
1084 level
= (-level
*qscale
*quant_matrix
[j
])>>4;
1087 level
= (level
*qscale
*quant_matrix
[j
])>>4;
1091 av_log(s
->avctx
, AV_LOG_ERROR
, "ac-tex damaged at %d %d\n", s
->mb_x
, s
->mb_y
);
1098 CLOSE_READER(re
, &s
->gb
);
1100 block
[63]^= mismatch
&1;
1102 s
->block_last_index
[n
] = i
;
1106 static inline int mpeg2_fast_decode_block_intra(MpegEncContext
*s
,
1110 int level
, dc
, diff
, j
, run
;
1113 uint8_t * scantable
= s
->intra_scantable
.permutated
;
1114 const uint16_t *quant_matrix
;
1115 const int qscale
= s
->qscale
;
1117 /* DC coefficient */
1119 quant_matrix
= s
->intra_matrix
;
1122 quant_matrix
= s
->chroma_intra_matrix
;
1123 component
= (n
&1) + 1;
1125 diff
= decode_dc(&s
->gb
, component
);
1128 dc
= s
->last_dc
[component
];
1130 s
->last_dc
[component
] = dc
;
1131 block
[0] = dc
<< (3 - s
->intra_dc_precision
);
1132 if (s
->intra_vlc_format
)
1138 OPEN_READER(re
, &s
->gb
);
1139 /* now quantify & encode AC coefficients */
1141 UPDATE_CACHE(re
, &s
->gb
);
1142 GET_RL_VLC(level
, run
, re
, &s
->gb
, rl
->rl_vlc
[0], TEX_VLC_BITS
, 2, 0);
1146 } else if(level
!= 0) {
1149 level
= (level
*qscale
*quant_matrix
[j
])>>4;
1150 level
= (level
^ SHOW_SBITS(re
, &s
->gb
, 1)) - SHOW_SBITS(re
, &s
->gb
, 1);
1151 LAST_SKIP_BITS(re
, &s
->gb
, 1);
1154 run
= SHOW_UBITS(re
, &s
->gb
, 6)+1; LAST_SKIP_BITS(re
, &s
->gb
, 6);
1155 UPDATE_CACHE(re
, &s
->gb
);
1156 level
= SHOW_SBITS(re
, &s
->gb
, 12); SKIP_BITS(re
, &s
->gb
, 12);
1160 level
= (-level
*qscale
*quant_matrix
[j
])>>4;
1163 level
= (level
*qscale
*quant_matrix
[j
])>>4;
1169 CLOSE_READER(re
, &s
->gb
);
1172 s
->block_last_index
[n
] = scantable
- s
->intra_scantable
.permutated
;
1176 typedef struct Mpeg1Context
{
1177 MpegEncContext mpeg_enc_ctx
;
1178 int mpeg_enc_ctx_allocated
; /* true if decoding context allocated */
1179 int repeat_field
; /* true if we must repeat the field */
1180 AVPanScan pan_scan
; /** some temporary storage for the panscan */
1182 int swap_uv
;//indicate VCR2
1183 int save_aspect_info
;
1184 int save_width
, save_height
;
1185 AVRational frame_rate_ext
; ///< MPEG-2 specific framerate modificator
1189 static av_cold
int mpeg_decode_init(AVCodecContext
*avctx
)
1191 Mpeg1Context
*s
= avctx
->priv_data
;
1192 MpegEncContext
*s2
= &s
->mpeg_enc_ctx
;
1195 /* we need some permutation to store matrices,
1196 * until MPV_common_init() sets the real permutation. */
1198 s2
->dsp
.idct_permutation
[i
]=i
;
1200 MPV_decode_defaults(s2
);
1202 s
->mpeg_enc_ctx
.avctx
= avctx
;
1203 s
->mpeg_enc_ctx
.flags
= avctx
->flags
;
1204 s
->mpeg_enc_ctx
.flags2
= avctx
->flags2
;
1205 ff_mpeg12_common_init(&s
->mpeg_enc_ctx
);
1206 ff_mpeg12_init_vlcs();
1208 s
->mpeg_enc_ctx_allocated
= 0;
1209 s
->mpeg_enc_ctx
.picture_number
= 0;
1210 s
->repeat_field
= 0;
1211 s
->mpeg_enc_ctx
.codec_id
= avctx
->codec
->id
;
1215 static void quant_matrix_rebuild(uint16_t *matrix
, const uint8_t *old_perm
,
1216 const uint8_t *new_perm
){
1217 uint16_t temp_matrix
[64];
1220 memcpy(temp_matrix
,matrix
,64*sizeof(uint16_t));
1223 matrix
[new_perm
[i
]] = temp_matrix
[old_perm
[i
]];
1227 static int vdpau_fill_pix_fmt(MpegEncContext
*s
, AVCodecContext
*avctx
){
1228 if(s
->chroma_format
>= 2)
1230 if(avctx
->sub_id
== 1)
1231 avctx
->pix_fmt
= avctx
->get_format(avctx
,pixfmt_vdpau_mpg1_420
);
1233 if(avctx
->profile
== 5)
1234 avctx
->pix_fmt
= avctx
->get_format(avctx
,pixfmt_vdpau_mpg2simple_420
);
1235 else if(avctx
->profile
== 4)
1236 avctx
->pix_fmt
= avctx
->get_format(avctx
,pixfmt_vdpau_mpg2main_420
);
1243 /* Call this function when we know all parameters.
1244 * It may be called in different places for MPEG-1 and MPEG-2. */
1245 static int mpeg_decode_postinit(AVCodecContext
*avctx
){
1246 Mpeg1Context
*s1
= avctx
->priv_data
;
1247 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1248 uint8_t old_permutation
[64];
1251 (s1
->mpeg_enc_ctx_allocated
== 0)||
1252 avctx
->coded_width
!= s
->width
||
1253 avctx
->coded_height
!= s
->height
||
1254 s1
->save_width
!= s
->width
||
1255 s1
->save_height
!= s
->height
||
1256 s1
->save_aspect_info
!= s
->aspect_ratio_info
||
1260 if (s1
->mpeg_enc_ctx_allocated
) {
1261 ParseContext pc
= s
->parse_context
;
1262 s
->parse_context
.buffer
=0;
1264 s
->parse_context
= pc
;
1267 if( (s
->width
== 0 )||(s
->height
== 0))
1270 avcodec_set_dimensions(avctx
, s
->width
, s
->height
);
1271 avctx
->bit_rate
= s
->bit_rate
;
1272 s1
->save_aspect_info
= s
->aspect_ratio_info
;
1273 s1
->save_width
= s
->width
;
1274 s1
->save_height
= s
->height
;
1276 /* low_delay may be forced, in this case we will have B-frames
1277 * that behave like P-frames. */
1278 avctx
->has_b_frames
= !(s
->low_delay
);
1280 if(avctx
->sub_id
==1){//s->codec_id==avctx->codec_id==CODEC_ID
1282 avctx
->time_base
.den
= ff_frame_rate_tab
[s
->frame_rate_index
].num
;
1283 avctx
->time_base
.num
= ff_frame_rate_tab
[s
->frame_rate_index
].den
;
1285 avctx
->sample_aspect_ratio
= av_d2q(
1286 1.0/ff_mpeg1_aspect
[s
->aspect_ratio_info
], 255);
1291 &s
->avctx
->time_base
.den
,
1292 &s
->avctx
->time_base
.num
,
1293 ff_frame_rate_tab
[s
->frame_rate_index
].num
* s1
->frame_rate_ext
.num
,
1294 ff_frame_rate_tab
[s
->frame_rate_index
].den
* s1
->frame_rate_ext
.den
,
1297 if(s
->aspect_ratio_info
> 1){
1298 //we ignore the spec here as reality does not match the spec, see for example
1299 // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
1300 if( (s1
->pan_scan
.width
== 0 )||(s1
->pan_scan
.height
== 0) || 1){
1301 s
->avctx
->sample_aspect_ratio
=
1303 ff_mpeg2_aspect
[s
->aspect_ratio_info
],
1304 (AVRational
){s
->width
, s
->height
}
1307 s
->avctx
->sample_aspect_ratio
=
1309 ff_mpeg2_aspect
[s
->aspect_ratio_info
],
1310 (AVRational
){s1
->pan_scan
.width
, s1
->pan_scan
.height
}
1314 s
->avctx
->sample_aspect_ratio
=
1315 ff_mpeg2_aspect
[s
->aspect_ratio_info
];
1319 if(avctx
->vdpau_acceleration
){
1320 if(vdpau_fill_pix_fmt(s
, avctx
)<0)
1323 if(avctx
->xvmc_acceleration
){
1324 avctx
->pix_fmt
= avctx
->get_format(avctx
,pixfmt_xvmc_mpg2_420
);
1326 if(s
->chroma_format
< 2){
1327 avctx
->pix_fmt
= PIX_FMT_YUV420P
;
1329 if(s
->chroma_format
== 2){
1330 avctx
->pix_fmt
= PIX_FMT_YUV422P
;
1332 if(s
->chroma_format
> 2){
1333 avctx
->pix_fmt
= PIX_FMT_YUV444P
;
1336 //until then pix_fmt may be changed right after codec init
1337 if( avctx
->pix_fmt
== PIX_FMT_XVMC_MPEG2_IDCT
)
1338 if( avctx
->idct_algo
== FF_IDCT_AUTO
)
1339 avctx
->idct_algo
= FF_IDCT_SIMPLE
;
1341 /* Quantization matrices may need reordering
1342 * if DCT permutation is changed. */
1343 memcpy(old_permutation
,s
->dsp
.idct_permutation
,64*sizeof(uint8_t));
1345 if (MPV_common_init(s
) < 0)
1348 quant_matrix_rebuild(s
->intra_matrix
, old_permutation
,s
->dsp
.idct_permutation
);
1349 quant_matrix_rebuild(s
->inter_matrix
, old_permutation
,s
->dsp
.idct_permutation
);
1350 quant_matrix_rebuild(s
->chroma_intra_matrix
,old_permutation
,s
->dsp
.idct_permutation
);
1351 quant_matrix_rebuild(s
->chroma_inter_matrix
,old_permutation
,s
->dsp
.idct_permutation
);
1353 s1
->mpeg_enc_ctx_allocated
= 1;
1358 static int mpeg1_decode_picture(AVCodecContext
*avctx
,
1359 const uint8_t *buf
, int buf_size
)
1361 Mpeg1Context
*s1
= avctx
->priv_data
;
1362 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1363 int ref
, f_code
, vbv_delay
;
1365 if(mpeg_decode_postinit(s
->avctx
) < 0)
1368 init_get_bits(&s
->gb
, buf
, buf_size
*8);
1370 ref
= get_bits(&s
->gb
, 10); /* temporal ref */
1371 s
->pict_type
= get_bits(&s
->gb
, 3);
1372 if(s
->pict_type
== 0 || s
->pict_type
> 3)
1375 vbv_delay
= get_bits(&s
->gb
, 16);
1376 if (s
->pict_type
== FF_P_TYPE
|| s
->pict_type
== FF_B_TYPE
) {
1377 s
->full_pel
[0] = get_bits1(&s
->gb
);
1378 f_code
= get_bits(&s
->gb
, 3);
1379 if (f_code
== 0 && avctx
->error_recognition
>= FF_ER_COMPLIANT
)
1381 s
->mpeg_f_code
[0][0] = f_code
;
1382 s
->mpeg_f_code
[0][1] = f_code
;
1384 if (s
->pict_type
== FF_B_TYPE
) {
1385 s
->full_pel
[1] = get_bits1(&s
->gb
);
1386 f_code
= get_bits(&s
->gb
, 3);
1387 if (f_code
== 0 && avctx
->error_recognition
>= FF_ER_COMPLIANT
)
1389 s
->mpeg_f_code
[1][0] = f_code
;
1390 s
->mpeg_f_code
[1][1] = f_code
;
1392 s
->current_picture
.pict_type
= s
->pict_type
;
1393 s
->current_picture
.key_frame
= s
->pict_type
== FF_I_TYPE
;
1395 if(avctx
->debug
& FF_DEBUG_PICT_INFO
)
1396 av_log(avctx
, AV_LOG_DEBUG
, "vbv_delay %d, ref %d type:%d\n", vbv_delay
, ref
, s
->pict_type
);
1404 static void mpeg_decode_sequence_extension(Mpeg1Context
*s1
)
1406 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1407 int horiz_size_ext
, vert_size_ext
;
1410 skip_bits(&s
->gb
, 1); /* profile and level esc*/
1411 s
->avctx
->profile
= get_bits(&s
->gb
, 3);
1412 s
->avctx
->level
= get_bits(&s
->gb
, 4);
1413 s
->progressive_sequence
= get_bits1(&s
->gb
); /* progressive_sequence */
1414 s
->chroma_format
= get_bits(&s
->gb
, 2); /* chroma_format 1=420, 2=422, 3=444 */
1415 horiz_size_ext
= get_bits(&s
->gb
, 2);
1416 vert_size_ext
= get_bits(&s
->gb
, 2);
1417 s
->width
|= (horiz_size_ext
<< 12);
1418 s
->height
|= (vert_size_ext
<< 12);
1419 bit_rate_ext
= get_bits(&s
->gb
, 12); /* XXX: handle it */
1420 s
->bit_rate
+= (bit_rate_ext
<< 18) * 400;
1421 skip_bits1(&s
->gb
); /* marker */
1422 s
->avctx
->rc_buffer_size
+= get_bits(&s
->gb
, 8)*1024*16<<10;
1424 s
->low_delay
= get_bits1(&s
->gb
);
1425 if(s
->flags
& CODEC_FLAG_LOW_DELAY
) s
->low_delay
=1;
1427 s1
->frame_rate_ext
.num
= get_bits(&s
->gb
, 2)+1;
1428 s1
->frame_rate_ext
.den
= get_bits(&s
->gb
, 5)+1;
1430 dprintf(s
->avctx
, "sequence extension\n");
1431 s
->codec_id
= s
->avctx
->codec_id
= CODEC_ID_MPEG2VIDEO
;
1432 s
->avctx
->sub_id
= 2; /* indicates MPEG-2 found */
1434 if(s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
1435 av_log(s
->avctx
, AV_LOG_DEBUG
, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
1436 s
->avctx
->profile
, s
->avctx
->level
, s
->avctx
->rc_buffer_size
, s
->bit_rate
);
1440 static void mpeg_decode_sequence_display_extension(Mpeg1Context
*s1
)
1442 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1443 int color_description
, w
, h
;
1445 skip_bits(&s
->gb
, 3); /* video format */
1446 color_description
= get_bits1(&s
->gb
);
1447 if(color_description
){
1448 skip_bits(&s
->gb
, 8); /* color primaries */
1449 skip_bits(&s
->gb
, 8); /* transfer_characteristics */
1450 skip_bits(&s
->gb
, 8); /* matrix_coefficients */
1452 w
= get_bits(&s
->gb
, 14);
1453 skip_bits(&s
->gb
, 1); //marker
1454 h
= get_bits(&s
->gb
, 14);
1455 skip_bits(&s
->gb
, 1); //marker
1457 s1
->pan_scan
.width
= 16*w
;
1458 s1
->pan_scan
.height
=16*h
;
1460 if(s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
1461 av_log(s
->avctx
, AV_LOG_DEBUG
, "sde w:%d, h:%d\n", w
, h
);
1464 static void mpeg_decode_picture_display_extension(Mpeg1Context
*s1
)
1466 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1470 if(s
->progressive_sequence
){
1471 if(s
->repeat_first_field
){
1473 if(s
->top_field_first
)
1477 if(s
->picture_structure
== PICT_FRAME
){
1479 if(s
->repeat_first_field
)
1483 for(i
=0; i
<nofco
; i
++){
1484 s1
->pan_scan
.position
[i
][0]= get_sbits(&s
->gb
, 16);
1485 skip_bits(&s
->gb
, 1); //marker
1486 s1
->pan_scan
.position
[i
][1]= get_sbits(&s
->gb
, 16);
1487 skip_bits(&s
->gb
, 1); //marker
1490 if(s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
1491 av_log(s
->avctx
, AV_LOG_DEBUG
, "pde (%d,%d) (%d,%d) (%d,%d)\n",
1492 s1
->pan_scan
.position
[0][0], s1
->pan_scan
.position
[0][1],
1493 s1
->pan_scan
.position
[1][0], s1
->pan_scan
.position
[1][1],
1494 s1
->pan_scan
.position
[2][0], s1
->pan_scan
.position
[2][1]
1498 static void mpeg_decode_quant_matrix_extension(MpegEncContext
*s
)
1502 dprintf(s
->avctx
, "matrix extension\n");
1504 if (get_bits1(&s
->gb
)) {
1506 v
= get_bits(&s
->gb
, 8);
1507 j
= s
->dsp
.idct_permutation
[ ff_zigzag_direct
[i
] ];
1508 s
->intra_matrix
[j
] = v
;
1509 s
->chroma_intra_matrix
[j
] = v
;
1512 if (get_bits1(&s
->gb
)) {
1514 v
= get_bits(&s
->gb
, 8);
1515 j
= s
->dsp
.idct_permutation
[ ff_zigzag_direct
[i
] ];
1516 s
->inter_matrix
[j
] = v
;
1517 s
->chroma_inter_matrix
[j
] = v
;
1520 if (get_bits1(&s
->gb
)) {
1522 v
= get_bits(&s
->gb
, 8);
1523 j
= s
->dsp
.idct_permutation
[ ff_zigzag_direct
[i
] ];
1524 s
->chroma_intra_matrix
[j
] = v
;
1527 if (get_bits1(&s
->gb
)) {
1529 v
= get_bits(&s
->gb
, 8);
1530 j
= s
->dsp
.idct_permutation
[ ff_zigzag_direct
[i
] ];
1531 s
->chroma_inter_matrix
[j
] = v
;
1536 static void mpeg_decode_picture_coding_extension(Mpeg1Context
*s1
)
1538 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1540 s
->full_pel
[0] = s
->full_pel
[1] = 0;
1541 s
->mpeg_f_code
[0][0] = get_bits(&s
->gb
, 4);
1542 s
->mpeg_f_code
[0][1] = get_bits(&s
->gb
, 4);
1543 s
->mpeg_f_code
[1][0] = get_bits(&s
->gb
, 4);
1544 s
->mpeg_f_code
[1][1] = get_bits(&s
->gb
, 4);
1545 if(!s
->pict_type
&& s1
->mpeg_enc_ctx_allocated
){
1546 av_log(s
->avctx
, AV_LOG_ERROR
, "Missing picture start code, guessing missing values\n");
1547 if(s
->mpeg_f_code
[1][0] == 15 && s
->mpeg_f_code
[1][1]==15){
1548 if(s
->mpeg_f_code
[0][0] == 15 && s
->mpeg_f_code
[0][1] == 15)
1549 s
->pict_type
= FF_I_TYPE
;
1551 s
->pict_type
= FF_P_TYPE
;
1553 s
->pict_type
= FF_B_TYPE
;
1554 s
->current_picture
.pict_type
= s
->pict_type
;
1555 s
->current_picture
.key_frame
= s
->pict_type
== FF_I_TYPE
;
1558 s
->intra_dc_precision
= get_bits(&s
->gb
, 2);
1559 s
->picture_structure
= get_bits(&s
->gb
, 2);
1560 s
->top_field_first
= get_bits1(&s
->gb
);
1561 s
->frame_pred_frame_dct
= get_bits1(&s
->gb
);
1562 s
->concealment_motion_vectors
= get_bits1(&s
->gb
);
1563 s
->q_scale_type
= get_bits1(&s
->gb
);
1564 s
->intra_vlc_format
= get_bits1(&s
->gb
);
1565 s
->alternate_scan
= get_bits1(&s
->gb
);
1566 s
->repeat_first_field
= get_bits1(&s
->gb
);
1567 s
->chroma_420_type
= get_bits1(&s
->gb
);
1568 s
->progressive_frame
= get_bits1(&s
->gb
);
1570 if(s
->picture_structure
== PICT_FRAME
){
1572 s
->v_edge_pos
= 16*s
->mb_height
;
1574 s
->first_field
^= 1;
1575 s
->v_edge_pos
= 8*s
->mb_height
;
1576 memset(s
->mbskip_table
, 0, s
->mb_stride
*s
->mb_height
);
1579 if(s
->alternate_scan
){
1580 ff_init_scantable(s
->dsp
.idct_permutation
, &s
->inter_scantable
, ff_alternate_vertical_scan
);
1581 ff_init_scantable(s
->dsp
.idct_permutation
, &s
->intra_scantable
, ff_alternate_vertical_scan
);
1583 ff_init_scantable(s
->dsp
.idct_permutation
, &s
->inter_scantable
, ff_zigzag_direct
);
1584 ff_init_scantable(s
->dsp
.idct_permutation
, &s
->intra_scantable
, ff_zigzag_direct
);
1587 /* composite display not parsed */
1588 dprintf(s
->avctx
, "intra_dc_precision=%d\n", s
->intra_dc_precision
);
1589 dprintf(s
->avctx
, "picture_structure=%d\n", s
->picture_structure
);
1590 dprintf(s
->avctx
, "top field first=%d\n", s
->top_field_first
);
1591 dprintf(s
->avctx
, "repeat first field=%d\n", s
->repeat_first_field
);
1592 dprintf(s
->avctx
, "conceal=%d\n", s
->concealment_motion_vectors
);
1593 dprintf(s
->avctx
, "intra_vlc_format=%d\n", s
->intra_vlc_format
);
1594 dprintf(s
->avctx
, "alternate_scan=%d\n", s
->alternate_scan
);
1595 dprintf(s
->avctx
, "frame_pred_frame_dct=%d\n", s
->frame_pred_frame_dct
);
1596 dprintf(s
->avctx
, "progressive_frame=%d\n", s
->progressive_frame
);
1599 static void mpeg_decode_extension(AVCodecContext
*avctx
,
1600 const uint8_t *buf
, int buf_size
)
1602 Mpeg1Context
*s1
= avctx
->priv_data
;
1603 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1606 init_get_bits(&s
->gb
, buf
, buf_size
*8);
1608 ext_type
= get_bits(&s
->gb
, 4);
1611 mpeg_decode_sequence_extension(s1
);
1614 mpeg_decode_sequence_display_extension(s1
);
1617 mpeg_decode_quant_matrix_extension(s
);
1620 mpeg_decode_picture_display_extension(s1
);
1623 mpeg_decode_picture_coding_extension(s1
);
1628 static void exchange_uv(MpegEncContext
*s
){
1629 short * tmp
= s
->pblocks
[4];
1630 s
->pblocks
[4] = s
->pblocks
[5];
1631 s
->pblocks
[5] = tmp
;
1634 static int mpeg_field_start(MpegEncContext
*s
){
1635 AVCodecContext
*avctx
= s
->avctx
;
1636 Mpeg1Context
*s1
= (Mpeg1Context
*)s
;
1638 /* start frame decoding */
1639 if(s
->first_field
|| s
->picture_structure
==PICT_FRAME
){
1640 if(MPV_frame_start(s
, avctx
) < 0)
1643 ff_er_frame_start(s
);
1645 /* first check if we must repeat the frame */
1646 s
->current_picture_ptr
->repeat_pict
= 0;
1647 if (s
->repeat_first_field
) {
1648 if (s
->progressive_sequence
) {
1649 if (s
->top_field_first
)
1650 s
->current_picture_ptr
->repeat_pict
= 4;
1652 s
->current_picture_ptr
->repeat_pict
= 2;
1653 } else if (s
->progressive_frame
) {
1654 s
->current_picture_ptr
->repeat_pict
= 1;
1658 *s
->current_picture_ptr
->pan_scan
= s1
->pan_scan
;
1659 }else{ //second field
1662 if(!s
->current_picture_ptr
){
1663 av_log(s
->avctx
, AV_LOG_ERROR
, "first field missing\n");
1668 s
->current_picture
.data
[i
] = s
->current_picture_ptr
->data
[i
];
1669 if(s
->picture_structure
== PICT_BOTTOM_FIELD
){
1670 s
->current_picture
.data
[i
] += s
->current_picture_ptr
->linesize
[i
];
1675 // MPV_frame_start will call this function too,
1676 // but we need to call it on every field
1677 if(s
->avctx
->xvmc_acceleration
)
1678 XVMC_field_start(s
,avctx
);
1682 if(s
->avctx
->vdpau_acceleration
)
1683 ff_VDPAU_mpeg_field_start(s
);
1689 #define DECODE_SLICE_ERROR -1
1690 #define DECODE_SLICE_OK 0
1693 * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
1694 * @return DECODE_SLICE_ERROR if the slice is damaged<br>
1695 * DECODE_SLICE_OK if this slice is ok<br>
1697 static int mpeg_decode_slice(Mpeg1Context
*s1
, int mb_y
,
1698 const uint8_t **buf
, int buf_size
)
1700 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1701 AVCodecContext
*avctx
= s
->avctx
;
1702 const int field_pic
= s
->picture_structure
!= PICT_FRAME
;
1703 const int lowres
= s
->avctx
->lowres
;
1708 if (mb_y
<<field_pic
>= s
->mb_height
){
1709 av_log(s
->avctx
, AV_LOG_ERROR
, "slice below image (%d >= %d)\n", mb_y
, s
->mb_height
);
1713 init_get_bits(&s
->gb
, *buf
, buf_size
*8);
1715 ff_mpeg1_clean_buffers(s
);
1716 s
->interlaced_dct
= 0;
1718 s
->qscale
= get_qscale(s
);
1721 av_log(s
->avctx
, AV_LOG_ERROR
, "qscale == 0\n");
1725 /* extra slice info */
1726 while (get_bits1(&s
->gb
) != 0) {
1727 skip_bits(&s
->gb
, 8);
1733 int code
= get_vlc2(&s
->gb
, mbincr_vlc
.table
, MBINCR_VLC_BITS
, 2);
1735 av_log(s
->avctx
, AV_LOG_ERROR
, "first mb_incr damaged\n");
1742 /* otherwise, stuffing, nothing to do */
1748 if(s
->mb_x
>= (unsigned)s
->mb_width
){
1749 av_log(s
->avctx
, AV_LOG_ERROR
, "initial skip overflow\n");
1753 s
->resync_mb_x
= s
->mb_x
;
1754 s
->resync_mb_y
= s
->mb_y
= mb_y
;
1756 ff_init_block_index(s
);
1758 if (s
->mb_y
==0 && s
->mb_x
==0 && (s
->first_field
|| s
->picture_structure
==PICT_FRAME
)) {
1759 if(s
->avctx
->debug
&FF_DEBUG_PICT_INFO
){
1760 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",
1761 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],
1762 s
->pict_type
== FF_I_TYPE
? "I" : (s
->pict_type
== FF_P_TYPE
? "P" : (s
->pict_type
== FF_B_TYPE
? "B" : "S")),
1763 s
->progressive_sequence
? "ps" :"", s
->progressive_frame
? "pf" : "", s
->alternate_scan
? "alt" :"", s
->top_field_first
? "top" :"",
1764 s
->intra_dc_precision
, s
->picture_structure
, s
->frame_pred_frame_dct
, s
->concealment_motion_vectors
,
1765 s
->q_scale_type
, s
->intra_vlc_format
, s
->repeat_first_field
, s
->chroma_420_type
? "420" :"");
1771 //If 1, we memcpy blocks in xvmcvideo.
1772 if(s
->avctx
->xvmc_acceleration
> 1)
1773 XVMC_init_block(s
);//set s->block
1776 if(mpeg_decode_mb(s
, s
->block
) < 0)
1779 if(s
->current_picture
.motion_val
[0] && !s
->encoding
){ //note motion_val is normally NULL unless we want to extract the MVs
1780 const int wrap
= field_pic
? 2*s
->b8_stride
: s
->b8_stride
;
1781 int xy
= s
->mb_x
*2 + s
->mb_y
*2*wrap
;
1782 int motion_x
, motion_y
, dir
, i
;
1783 if(field_pic
&& !s
->first_field
)
1787 for(dir
=0; dir
<2; dir
++){
1788 if (s
->mb_intra
|| (dir
==1 && s
->pict_type
!= FF_B_TYPE
)) {
1789 motion_x
= motion_y
= 0;
1790 }else if (s
->mv_type
== MV_TYPE_16X16
|| (s
->mv_type
== MV_TYPE_FIELD
&& field_pic
)){
1791 motion_x
= s
->mv
[dir
][0][0];
1792 motion_y
= s
->mv
[dir
][0][1];
1793 } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
1794 motion_x
= s
->mv
[dir
][i
][0];
1795 motion_y
= s
->mv
[dir
][i
][1];
1798 s
->current_picture
.motion_val
[dir
][xy
][0] = motion_x
;
1799 s
->current_picture
.motion_val
[dir
][xy
][1] = motion_y
;
1800 s
->current_picture
.motion_val
[dir
][xy
+ 1][0] = motion_x
;
1801 s
->current_picture
.motion_val
[dir
][xy
+ 1][1] = motion_y
;
1802 s
->current_picture
.ref_index
[dir
][xy
]=
1803 s
->current_picture
.ref_index
[dir
][xy
+ 1]= s
->field_select
[dir
][i
];
1804 assert(s
->field_select
[dir
][i
]==0 || s
->field_select
[dir
][i
]==1);
1810 s
->dest
[0] += 16 >> lowres
;
1811 s
->dest
[1] +=(16 >> lowres
) >> s
->chroma_x_shift
;
1812 s
->dest
[2] +=(16 >> lowres
) >> s
->chroma_x_shift
;
1814 MPV_decode_mb(s
, s
->block
);
1816 if (++s
->mb_x
>= s
->mb_width
) {
1817 const int mb_size
= 16>>s
->avctx
->lowres
;
1819 ff_draw_horiz_band(s
, mb_size
*s
->mb_y
, mb_size
);
1824 if(s
->mb_y
<<field_pic
>= s
->mb_height
){
1825 int left
= s
->gb
.size_in_bits
- get_bits_count(&s
->gb
);
1826 int is_d10
= s
->chroma_format
==2 && s
->pict_type
==FF_I_TYPE
&& avctx
->profile
==0 && avctx
->level
==5
1827 && s
->intra_dc_precision
== 2 && s
->q_scale_type
== 1 && s
->alternate_scan
== 0
1828 && s
->progressive_frame
== 0 /* vbv_delay == 0xBBB || 0xE10*/;
1830 if(left
< 0 || (left
&& show_bits(&s
->gb
, FFMIN(left
, 23)) && !is_d10
)
1831 || (avctx
->error_recognition
>= FF_ER_AGGRESSIVE
&& left
>8)){
1832 av_log(avctx
, AV_LOG_ERROR
, "end mismatch left=%d %0X\n", left
, show_bits(&s
->gb
, FFMIN(left
, 23)));
1838 ff_init_block_index(s
);
1841 /* skip mb handling */
1842 if (s
->mb_skip_run
== -1) {
1843 /* read increment again */
1846 int code
= get_vlc2(&s
->gb
, mbincr_vlc
.table
, MBINCR_VLC_BITS
, 2);
1848 av_log(s
->avctx
, AV_LOG_ERROR
, "mb incr damaged\n");
1853 s
->mb_skip_run
+= 33;
1854 }else if(code
== 35){
1855 if(s
->mb_skip_run
!= 0 || show_bits(&s
->gb
, 15) != 0){
1856 av_log(s
->avctx
, AV_LOG_ERROR
, "slice mismatch\n");
1859 goto eos
; /* end of slice */
1861 /* otherwise, stuffing, nothing to do */
1863 s
->mb_skip_run
+= code
;
1869 if(s
->pict_type
== FF_I_TYPE
){
1870 av_log(s
->avctx
, AV_LOG_ERROR
, "skipped MB in I frame at %d %d\n", s
->mb_x
, s
->mb_y
);
1877 s
->block_last_index
[i
] = -1;
1878 if(s
->picture_structure
== PICT_FRAME
)
1879 s
->mv_type
= MV_TYPE_16X16
;
1881 s
->mv_type
= MV_TYPE_FIELD
;
1882 if (s
->pict_type
== FF_P_TYPE
) {
1883 /* if P type, zero motion vector is implied */
1884 s
->mv_dir
= MV_DIR_FORWARD
;
1885 s
->mv
[0][0][0] = s
->mv
[0][0][1] = 0;
1886 s
->last_mv
[0][0][0] = s
->last_mv
[0][0][1] = 0;
1887 s
->last_mv
[0][1][0] = s
->last_mv
[0][1][1] = 0;
1888 s
->field_select
[0][0]= s
->picture_structure
- 1;
1890 /* if B type, reuse previous vectors and directions */
1891 s
->mv
[0][0][0] = s
->last_mv
[0][0][0];
1892 s
->mv
[0][0][1] = s
->last_mv
[0][0][1];
1893 s
->mv
[1][0][0] = s
->last_mv
[1][0][0];
1894 s
->mv
[1][0][1] = s
->last_mv
[1][0][1];
1899 eos
: // end of slice
1900 *buf
+= (get_bits_count(&s
->gb
)-1)/8;
1901 //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1905 static int slice_decode_thread(AVCodecContext
*c
, void *arg
){
1906 MpegEncContext
*s
= *(void**)arg
;
1907 const uint8_t *buf
= s
->gb
.buffer
;
1908 int mb_y
= s
->start_mb_y
;
1910 s
->error_count
= 3*(s
->end_mb_y
- s
->start_mb_y
)*s
->mb_width
;
1913 uint32_t start_code
;
1916 ret
= mpeg_decode_slice((Mpeg1Context
*)s
, mb_y
, &buf
, s
->gb
.buffer_end
- buf
);
1918 //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
1919 //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);
1921 if(s
->resync_mb_x
>=0 && s
->resync_mb_y
>=0)
1922 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
);
1924 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
);
1927 if(s
->mb_y
== s
->end_mb_y
)
1931 buf
= ff_find_start_code(buf
, s
->gb
.buffer_end
, &start_code
);
1932 mb_y
= start_code
- SLICE_MIN_START_CODE
;
1933 if(mb_y
< 0 || mb_y
>= s
->end_mb_y
)
1937 return 0; //not reached
1941 * Handles slice ends.
1942 * @return 1 if it seems to be the last slice
1944 static int slice_end(AVCodecContext
*avctx
, AVFrame
*pict
)
1946 Mpeg1Context
*s1
= avctx
->priv_data
;
1947 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1949 if (!s1
->mpeg_enc_ctx_allocated
|| !s
->current_picture_ptr
)
1953 if(s
->avctx
->xvmc_acceleration
)
1956 /* end of slice reached */
1957 if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s
->first_field
) {
1960 s
->current_picture_ptr
->qscale_type
= FF_QSCALE_TYPE_MPEG2
;
1963 if(!s
->avctx
->vdpau_acceleration
)
1969 if (s
->pict_type
== FF_B_TYPE
|| s
->low_delay
) {
1970 *pict
= *(AVFrame
*)s
->current_picture_ptr
;
1971 ff_print_debug_info(s
, pict
);
1973 s
->picture_number
++;
1974 /* latency of 1 frame for I- and P-frames */
1975 /* XXX: use another variable than picture_number */
1976 if (s
->last_picture_ptr
!= NULL
) {
1977 *pict
= *(AVFrame
*)s
->last_picture_ptr
;
1978 ff_print_debug_info(s
, pict
);
1988 static int mpeg1_decode_sequence(AVCodecContext
*avctx
,
1989 const uint8_t *buf
, int buf_size
)
1991 Mpeg1Context
*s1
= avctx
->priv_data
;
1992 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
1996 init_get_bits(&s
->gb
, buf
, buf_size
*8);
1998 width
= get_bits(&s
->gb
, 12);
1999 height
= get_bits(&s
->gb
, 12);
2000 if (width
<= 0 || height
<= 0)
2002 s
->aspect_ratio_info
= get_bits(&s
->gb
, 4);
2003 if (s
->aspect_ratio_info
== 0) {
2004 av_log(avctx
, AV_LOG_ERROR
, "aspect ratio has forbidden 0 value\n");
2005 if (avctx
->error_recognition
>= FF_ER_COMPLIANT
)
2008 s
->frame_rate_index
= get_bits(&s
->gb
, 4);
2009 if (s
->frame_rate_index
== 0 || s
->frame_rate_index
> 13)
2011 s
->bit_rate
= get_bits(&s
->gb
, 18) * 400;
2012 if (get_bits1(&s
->gb
) == 0) /* marker */
2017 s
->avctx
->rc_buffer_size
= get_bits(&s
->gb
, 10) * 1024*16;
2018 skip_bits(&s
->gb
, 1);
2021 if (get_bits1(&s
->gb
)) {
2023 v
= get_bits(&s
->gb
, 8);
2025 av_log(s
->avctx
, AV_LOG_ERROR
, "intra matrix damaged\n");
2028 j
= s
->dsp
.idct_permutation
[ ff_zigzag_direct
[i
] ];
2029 s
->intra_matrix
[j
] = v
;
2030 s
->chroma_intra_matrix
[j
] = v
;
2033 dprintf(s
->avctx
, "intra matrix present\n");
2035 dprintf(s
->avctx
, " %d", s
->intra_matrix
[s
->dsp
.idct_permutation
[i
]]);
2036 dprintf(s
->avctx
, "\n");
2040 j
= s
->dsp
.idct_permutation
[i
];
2041 v
= ff_mpeg1_default_intra_matrix
[i
];
2042 s
->intra_matrix
[j
] = v
;
2043 s
->chroma_intra_matrix
[j
] = v
;
2046 if (get_bits1(&s
->gb
)) {
2048 v
= get_bits(&s
->gb
, 8);
2050 av_log(s
->avctx
, AV_LOG_ERROR
, "inter matrix damaged\n");
2053 j
= s
->dsp
.idct_permutation
[ ff_zigzag_direct
[i
] ];
2054 s
->inter_matrix
[j
] = v
;
2055 s
->chroma_inter_matrix
[j
] = v
;
2058 dprintf(s
->avctx
, "non-intra matrix present\n");
2060 dprintf(s
->avctx
, " %d", s
->inter_matrix
[s
->dsp
.idct_permutation
[i
]]);
2061 dprintf(s
->avctx
, "\n");
2065 int j
= s
->dsp
.idct_permutation
[i
];
2066 v
= ff_mpeg1_default_non_intra_matrix
[i
];
2067 s
->inter_matrix
[j
] = v
;
2068 s
->chroma_inter_matrix
[j
] = v
;
2072 if(show_bits(&s
->gb
, 23) != 0){
2073 av_log(s
->avctx
, AV_LOG_ERROR
, "sequence header damaged\n");
2077 /* we set MPEG-2 parameters so that it emulates MPEG-1 */
2078 s
->progressive_sequence
= 1;
2079 s
->progressive_frame
= 1;
2080 s
->picture_structure
= PICT_FRAME
;
2081 s
->frame_pred_frame_dct
= 1;
2082 s
->chroma_format
= 1;
2083 s
->codec_id
= s
->avctx
->codec_id
= CODEC_ID_MPEG1VIDEO
;
2084 avctx
->sub_id
= 1; /* indicates MPEG-1 */
2085 s
->out_format
= FMT_MPEG1
;
2086 s
->swap_uv
= 0;//AFAIK VCR2 does not have SEQ_HEADER
2087 if(s
->flags
& CODEC_FLAG_LOW_DELAY
) s
->low_delay
=1;
2089 if(s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
2090 av_log(s
->avctx
, AV_LOG_DEBUG
, "vbv buffer: %d, bitrate:%d\n",
2091 s
->avctx
->rc_buffer_size
, s
->bit_rate
);
2096 static int vcr2_init_sequence(AVCodecContext
*avctx
)
2098 Mpeg1Context
*s1
= avctx
->priv_data
;
2099 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
2102 /* start new MPEG-1 context decoding */
2103 s
->out_format
= FMT_MPEG1
;
2104 if (s1
->mpeg_enc_ctx_allocated
) {
2107 s
->width
= avctx
->coded_width
;
2108 s
->height
= avctx
->coded_height
;
2109 avctx
->has_b_frames
= 0; //true?
2112 if(avctx
->vdpau_acceleration
){
2113 if(vdpau_fill_pix_fmt(s
, avctx
)<0)
2116 if(avctx
->xvmc_acceleration
){
2117 avctx
->pix_fmt
= avctx
->get_format(avctx
,pixfmt_xvmc_mpg2_420
);
2119 avctx
->pix_fmt
= PIX_FMT_YUV420P
;
2122 if( avctx
->pix_fmt
== PIX_FMT_XVMC_MPEG2_IDCT
)
2123 if( avctx
->idct_algo
== FF_IDCT_AUTO
)
2124 avctx
->idct_algo
= FF_IDCT_SIMPLE
;
2126 if (MPV_common_init(s
) < 0)
2128 exchange_uv(s
);//common init reset pblocks, so we swap them here
2129 s
->swap_uv
= 1;// in case of xvmc we need to swap uv for each MB
2130 s1
->mpeg_enc_ctx_allocated
= 1;
2133 int j
= s
->dsp
.idct_permutation
[i
];
2134 v
= ff_mpeg1_default_intra_matrix
[i
];
2135 s
->intra_matrix
[j
] = v
;
2136 s
->chroma_intra_matrix
[j
] = v
;
2138 v
= ff_mpeg1_default_non_intra_matrix
[i
];
2139 s
->inter_matrix
[j
] = v
;
2140 s
->chroma_inter_matrix
[j
] = v
;
2143 s
->progressive_sequence
= 1;
2144 s
->progressive_frame
= 1;
2145 s
->picture_structure
= PICT_FRAME
;
2146 s
->frame_pred_frame_dct
= 1;
2147 s
->chroma_format
= 1;
2148 s
->codec_id
= s
->avctx
->codec_id
= CODEC_ID_MPEG2VIDEO
;
2149 avctx
->sub_id
= 2; /* indicates MPEG-2 */
2154 static void mpeg_decode_user_data(AVCodecContext
*avctx
,
2155 const uint8_t *buf
, int buf_size
)
2162 /* we parse the DTG active format information */
2164 p
[0] == 'D' && p
[1] == 'T' && p
[2] == 'G' && p
[3] == '1') {
2178 avctx
->dtg_active_format
= p
[0] & 0x0f;
2183 static void mpeg_decode_gop(AVCodecContext
*avctx
,
2184 const uint8_t *buf
, int buf_size
){
2185 Mpeg1Context
*s1
= avctx
->priv_data
;
2186 MpegEncContext
*s
= &s1
->mpeg_enc_ctx
;
2188 int drop_frame_flag
;
2189 int time_code_hours
, time_code_minutes
;
2190 int time_code_seconds
, time_code_pictures
;
2191 int closed_gop
, broken_link
;
2193 init_get_bits(&s
->gb
, buf
, buf_size
*8);
2195 drop_frame_flag
= get_bits1(&s
->gb
);
2197 time_code_hours
=get_bits(&s
->gb
,5);
2198 time_code_minutes
= get_bits(&s
->gb
,6);
2199 skip_bits1(&s
->gb
);//marker bit
2200 time_code_seconds
= get_bits(&s
->gb
,6);
2201 time_code_pictures
= get_bits(&s
->gb
,6);
2203 closed_gop
= get_bits1(&s
->gb
);
2204 /*broken_link indicate that after editing the
2205 reference frames of the first B-Frames after GOP I-Frame
2206 are missing (open gop)*/
2207 broken_link
= get_bits1(&s
->gb
);
2209 if(s
->avctx
->debug
& FF_DEBUG_PICT_INFO
)
2210 av_log(s
->avctx
, AV_LOG_DEBUG
, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
2211 time_code_hours
, time_code_minutes
, time_code_seconds
,
2212 time_code_pictures
, closed_gop
, broken_link
);
2215 * Finds the end of the current frame in the bitstream.
2216 * @return the position of the first byte of the next frame, or -1
2218 int ff_mpeg1_find_frame_end(ParseContext
*pc
, const uint8_t *buf
, int buf_size
)
2221 uint32_t state
= pc
->state
;
2223 /* EOF considered as end of frame */
2228 0 frame start -> 1/4
2229 1 first_SEQEXT -> 0/2
2230 2 first field start -> 3/0
2231 3 second_SEQEXT -> 2/0
2235 for(i
=0; i
<buf_size
; i
++){
2236 assert(pc
->frame_start_found
>=0 && pc
->frame_start_found
<=4);
2237 if(pc
->frame_start_found
&1){
2238 if(state
== EXT_START_CODE
&& (buf
[i
]&0xF0) != 0x80)
2239 pc
->frame_start_found
--;
2240 else if(state
== EXT_START_CODE
+2){
2241 if((buf
[i
]&3) == 3) pc
->frame_start_found
= 0;
2242 else pc
->frame_start_found
= (pc
->frame_start_found
+1)&3;
2246 i
= ff_find_start_code(buf
+i
, buf
+buf_size
, &state
) - buf
- 1;
2247 if(pc
->frame_start_found
==0 && state
>= SLICE_MIN_START_CODE
&& state
<= SLICE_MAX_START_CODE
){
2249 pc
->frame_start_found
=4;
2251 if(state
== SEQ_END_CODE
){
2255 if(pc
->frame_start_found
==2 && state
== SEQ_START_CODE
)
2256 pc
->frame_start_found
= 0;
2257 if(pc
->frame_start_found
<4 && state
== EXT_START_CODE
)
2258 pc
->frame_start_found
++;
2259 if(pc
->frame_start_found
== 4 && (state
&0xFFFFFF00) == 0x100){
2260 if(state
< SLICE_MIN_START_CODE
|| state
> SLICE_MAX_START_CODE
){
2261 pc
->frame_start_found
=0;
2269 return END_NOT_FOUND
;
2272 static int decode_chunks(AVCodecContext
*avctx
,
2273 AVFrame
*picture
, int *data_size
,
2274 const uint8_t *buf
, int buf_size
);
2276 /* handle buffering and image synchronisation */
2277 static int mpeg_decode_frame(AVCodecContext
*avctx
,
2278 void *data
, int *data_size
,
2279 const uint8_t *buf
, int buf_size
)
2281 Mpeg1Context
*s
= avctx
->priv_data
;
2282 AVFrame
*picture
= data
;
2283 MpegEncContext
*s2
= &s
->mpeg_enc_ctx
;
2284 dprintf(avctx
, "fill_buffer\n");
2286 if (buf_size
== 0 || (buf_size
== 4 && AV_RB32(buf
) == SEQ_END_CODE
)) {
2287 /* special case for last picture */
2288 if (s2
->low_delay
==0 && s2
->next_picture_ptr
) {
2289 *picture
= *(AVFrame
*)s2
->next_picture_ptr
;
2290 s2
->next_picture_ptr
= NULL
;
2292 *data_size
= sizeof(AVFrame
);
2297 if(s2
->flags
&CODEC_FLAG_TRUNCATED
){
2298 int next
= ff_mpeg1_find_frame_end(&s2
->parse_context
, buf
, buf_size
);
2300 if( ff_combine_frame(&s2
->parse_context
, next
, (const uint8_t **)&buf
, &buf_size
) < 0 )
2305 if (s
->repeat_field
% 2 == 1) {
2307 //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
2308 // s2->picture_number, s->repeat_field);
2309 if (avctx
->flags
& CODEC_FLAG_REPEAT_FIELD
) {
2310 *data_size
= sizeof(AVPicture
);
2316 if(s
->mpeg_enc_ctx_allocated
==0 && avctx
->codec_tag
== ff_get_fourcc("VCR2"))
2317 vcr2_init_sequence(avctx
);
2321 if(avctx
->extradata
&& !avctx
->frame_number
)
2322 decode_chunks(avctx
, picture
, data_size
, avctx
->extradata
, avctx
->extradata_size
);
2324 return decode_chunks(avctx
, picture
, data_size
, buf
, buf_size
);
2327 static int decode_chunks(AVCodecContext
*avctx
,
2328 AVFrame
*picture
, int *data_size
,
2329 const uint8_t *buf
, int buf_size
)
2331 Mpeg1Context
*s
= avctx
->priv_data
;
2332 MpegEncContext
*s2
= &s
->mpeg_enc_ctx
;
2333 const uint8_t *buf_ptr
= buf
;
2334 const uint8_t *buf_end
= buf
+ buf_size
;
2335 int ret
, input_size
;
2338 /* find next start code */
2339 uint32_t start_code
= -1;
2340 buf_ptr
= ff_find_start_code(buf_ptr
,buf_end
, &start_code
);
2341 if (start_code
> 0x1ff){
2342 if(s2
->pict_type
!= FF_B_TYPE
|| avctx
->skip_frame
<= AVDISCARD_DEFAULT
){
2343 if(avctx
->thread_count
> 1){
2346 avctx
->execute(avctx
, slice_decode_thread
, (void**)&(s2
->thread_context
[0]), NULL
, s
->slice_count
, sizeof(void*));
2347 for(i
=0; i
<s
->slice_count
; i
++)
2348 s2
->error_count
+= s2
->thread_context
[i
]->error_count
;
2352 if (avctx
->vdpau_acceleration
) {
2353 /* Fills mpeg12 picture informations before returing from libavcodec. */
2354 ff_VDPAU_mpeg_picture_complete(s2
, buf
, buf_size
, s
->slice_count
);
2358 if (slice_end(avctx
, picture
)) {
2359 if(s2
->last_picture_ptr
|| s2
->low_delay
) //FIXME merge with the stuff in mpeg_decode_slice
2360 *data_size
= sizeof(AVPicture
);
2364 return FFMAX(0, buf_ptr
- buf
- s2
->parse_context
.last_index
);
2367 input_size
= buf_end
- buf_ptr
;
2369 if(avctx
->debug
& FF_DEBUG_STARTCODE
){
2370 av_log(avctx
, AV_LOG_DEBUG
, "%3X at %td left %d\n", start_code
, buf_ptr
-buf
, input_size
);
2373 /* prepare data for next start code */
2374 switch(start_code
) {
2375 case SEQ_START_CODE
:
2376 mpeg1_decode_sequence(avctx
, buf_ptr
,
2380 case PICTURE_START_CODE
:
2381 /* we have a complete image: we try to decompress it */
2382 if(mpeg1_decode_picture(avctx
,
2383 buf_ptr
, input_size
) < 0)
2386 case EXT_START_CODE
:
2387 mpeg_decode_extension(avctx
,
2388 buf_ptr
, input_size
);
2390 case USER_START_CODE
:
2391 mpeg_decode_user_data(avctx
,
2392 buf_ptr
, input_size
);
2394 case GOP_START_CODE
:
2396 mpeg_decode_gop(avctx
,
2397 buf_ptr
, input_size
);
2400 if (start_code
>= SLICE_MIN_START_CODE
&&
2401 start_code
<= SLICE_MAX_START_CODE
) {
2402 int mb_y
= start_code
- SLICE_MIN_START_CODE
;
2404 if(s2
->last_picture_ptr
==NULL
){
2405 /* Skip B-frames if we do not have reference frames. */
2406 if(s2
->pict_type
==FF_B_TYPE
) break;
2408 if(s2
->next_picture_ptr
==NULL
){
2409 /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
2410 if(s2
->pict_type
==FF_P_TYPE
&& (s2
->first_field
|| s2
->picture_structure
==PICT_FRAME
)) break;
2412 /* Skip B-frames if we are in a hurry. */
2413 if(avctx
->hurry_up
&& s2
->pict_type
==FF_B_TYPE
) break;
2414 if( (avctx
->skip_frame
>= AVDISCARD_NONREF
&& s2
->pict_type
==FF_B_TYPE
)
2415 ||(avctx
->skip_frame
>= AVDISCARD_NONKEY
&& s2
->pict_type
!=FF_I_TYPE
)
2416 || avctx
->skip_frame
>= AVDISCARD_ALL
)
2418 /* Skip everything if we are in a hurry>=5. */
2419 if(avctx
->hurry_up
>=5) break;
2421 if (!s
->mpeg_enc_ctx_allocated
) break;
2423 if(s2
->codec_id
== CODEC_ID_MPEG2VIDEO
){
2424 if(mb_y
< avctx
->skip_top
|| mb_y
>= s2
->mb_height
- avctx
->skip_bottom
)
2429 av_log(avctx
, AV_LOG_ERROR
, "Missing picture start code\n");
2433 if(s2
->first_slice
){
2435 if(mpeg_field_start(s2
) < 0)
2438 if(!s2
->current_picture_ptr
){
2439 av_log(avctx
, AV_LOG_ERROR
, "current_picture not initialized\n");
2443 if (avctx
->vdpau_acceleration
) {
2448 if(avctx
->thread_count
> 1){
2449 int threshold
= (s2
->mb_height
*s
->slice_count
+ avctx
->thread_count
/2) / avctx
->thread_count
;
2450 if(threshold
<= mb_y
){
2451 MpegEncContext
*thread_context
= s2
->thread_context
[s
->slice_count
];
2453 thread_context
->start_mb_y
= mb_y
;
2454 thread_context
->end_mb_y
= s2
->mb_height
;
2456 s2
->thread_context
[s
->slice_count
-1]->end_mb_y
= mb_y
;
2457 ff_update_duplicate_context(thread_context
, s2
);
2459 init_get_bits(&thread_context
->gb
, buf_ptr
, input_size
*8);
2462 buf_ptr
+= 2; //FIXME add minimum number of bytes per slice
2464 ret
= mpeg_decode_slice(s
, mb_y
, &buf_ptr
, input_size
);
2468 if(s2
->resync_mb_x
>=0 && s2
->resync_mb_y
>=0)
2469 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
);
2471 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
);
2480 static int mpeg_decode_end(AVCodecContext
*avctx
)
2482 Mpeg1Context
*s
= avctx
->priv_data
;
2484 if (s
->mpeg_enc_ctx_allocated
)
2485 MPV_common_end(&s
->mpeg_enc_ctx
);
2489 AVCodec mpeg1video_decoder
= {
2492 CODEC_ID_MPEG1VIDEO
,
2493 sizeof(Mpeg1Context
),
2498 CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
| CODEC_CAP_TRUNCATED
| CODEC_CAP_DELAY
,
2499 .flush
= ff_mpeg_flush
,
2500 .long_name
= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2503 AVCodec mpeg2video_decoder
= {
2506 CODEC_ID_MPEG2VIDEO
,
2507 sizeof(Mpeg1Context
),
2512 CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
| CODEC_CAP_TRUNCATED
| CODEC_CAP_DELAY
,
2513 .flush
= ff_mpeg_flush
,
2514 .long_name
= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
2518 AVCodec mpegvideo_decoder
= {
2521 CODEC_ID_MPEG2VIDEO
,
2522 sizeof(Mpeg1Context
),
2527 CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
| CODEC_CAP_TRUNCATED
| CODEC_CAP_DELAY
,
2528 .flush
= ff_mpeg_flush
,
2529 .long_name
= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
2533 static av_cold
int mpeg_mc_decode_init(AVCodecContext
*avctx
){
2536 if( avctx
->thread_count
> 1)
2538 if( !(avctx
->slice_flags
& SLICE_FLAG_CODED_ORDER
) )
2540 if( !(avctx
->slice_flags
& SLICE_FLAG_ALLOW_FIELD
) ){
2541 dprintf(avctx
, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
2543 mpeg_decode_init(avctx
);
2544 s
= avctx
->priv_data
;
2546 avctx
->pix_fmt
= PIX_FMT_XVMC_MPEG2_IDCT
;
2547 avctx
->xvmc_acceleration
= 2;//2 - the blocks are packed!
2552 AVCodec mpeg_xvmc_decoder
= {
2555 CODEC_ID_MPEG2VIDEO_XVMC
,
2556 sizeof(Mpeg1Context
),
2557 mpeg_mc_decode_init
,
2561 CODEC_CAP_DRAW_HORIZ_BAND
| CODEC_CAP_DR1
| CODEC_CAP_TRUNCATED
| CODEC_CAP_HWACCEL
| CODEC_CAP_DELAY
,
2562 .flush
= ff_mpeg_flush
,
2563 .long_name
= NULL_IF_CONFIG_SMALL("MPEG-1 video XvMC (X-Video Motion Compensation)"),
2569 static av_cold
int mpeg_vdpau_decode_init(AVCodecContext
*avctx
){
2570 if( avctx
->thread_count
> 1)
2572 if( !(avctx
->slice_flags
& SLICE_FLAG_CODED_ORDER
) )
2574 if( !(avctx
->slice_flags
& SLICE_FLAG_ALLOW_FIELD
) ){
2575 dprintf(avctx
, "mpeg12.c: VDPAU decoder does not set SLICE_FLAG_ALLOW_FIELD\n");
2577 mpeg_decode_init(avctx
);
2579 // Set in mpeg_decode_postinit() once initial parsing is complete
2580 avctx
->pix_fmt
= PIX_FMT_NONE
;
2581 avctx
->vdpau_acceleration
= 1;
2586 AVCodec mpeg_vdpau_decoder
= {
2589 CODEC_ID_MPEGVIDEO_VDPAU
,
2590 sizeof(Mpeg1Context
),
2591 mpeg_vdpau_decode_init
,
2595 CODEC_CAP_DR1
| CODEC_CAP_TRUNCATED
| CODEC_CAP_HWACCEL_VDPAU
| CODEC_CAP_DELAY
,
2596 .flush
= ff_mpeg_flush
,
2597 .long_name
= NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),