Merge branch 'mirror' into vdpau
[FFMpeg-mirror/ffmpeg-vdpau.git] / libavcodec / mpeg12.c
blobc178785afe9d45c3b26ce959353066bf35a296be
1 /*
2 * MPEG-1/2 decoder
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
23 /**
24 * @file mpeg12.c
25 * MPEG-1/2 decoder
28 //#define DEBUG
29 #include "avcodec.h"
30 #include "dsputil.h"
31 #include "mpegvideo.h"
33 #include "mpeg12.h"
34 #include "mpeg12data.h"
35 #include "mpeg12decdata.h"
36 #include "bytestream.h"
38 //#undef NDEBUG
39 //#include <assert.h>
42 #define MV_VLC_BITS 9
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,
49 DCTELEM *block,
50 int n);
51 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
52 DCTELEM *block,
53 int n);
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,
56 DCTELEM *block,
57 int n);
58 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
59 DCTELEM *block,
60 int n);
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,
77 PIX_FMT_NONE};
78 static const enum PixelFormat pixfmt_vdpau_mpg1_420[] = {
79 PIX_FMT_VDPAU_MPEG1,
80 PIX_FMT_NONE};
81 static const enum PixelFormat pixfmt_vdpau_mpg2simple_420[] = {
82 PIX_FMT_VDPAU_MPEG2_SIMPLE,
83 PIX_FMT_NONE};
84 static const enum PixelFormat pixfmt_vdpau_mpg2main_420[] = {
85 PIX_FMT_VDPAU_MPEG2_MAIN,
86 PIX_FMT_NONE};
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;\
99 init_2d_vlc_rl(&rl);\
102 static void init_2d_vlc_rl(RLTable *rl)
104 int i;
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];
109 int level, run;
111 if(len==0){ // illegal code
112 run= 65;
113 level= MAX_LEVEL;
114 }else if(len<0){ //more bits needed
115 run= 0;
116 level= code;
117 }else{
118 if(code==rl->n){ //esc
119 run= 65;
120 level= 0;
121 }else if(code==rl->n+1){ //eob
122 run= 0;
123 level= 127;
124 }else{
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)
138 s->y_dc_scale_table=
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 /******************************************/
152 /* decoding */
154 static VLC mv_vlc;
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)
162 static int done = 0;
164 if (!done) {
165 done = 1;
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);
201 else
202 return 0;
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];
210 } else {
211 return qscale << 1;
215 /* motion type (for MPEG-2) */
216 #define MT_FIELD 1
217 #define MT_FRAME 2
218 #define MT_16X8 2
219 #define MT_DMV 3
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) {
233 s->mb_skipped = 1;
234 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
235 } else {
236 int mb_type;
238 if(s->mb_x)
239 mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
240 else
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))
243 return -1;
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)
250 s->mb_skipped = 1;
253 return 0;
256 switch(s->pict_type) {
257 default:
258 case FF_I_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);
262 return -1;
264 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
265 } else {
266 mb_type = MB_TYPE_INTRA;
268 break;
269 case FF_P_TYPE:
270 mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
271 if (mb_type < 0){
272 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
273 return -1;
275 mb_type = ptype2mb_type[ mb_type ];
276 break;
277 case FF_B_TYPE:
278 mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
279 if (mb_type < 0){
280 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
281 return -1;
283 mb_type = btype2mb_type[ mb_type ];
284 break;
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 */
315 }else
316 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
317 s->mb_intra = 1;
318 #ifdef HAVE_XVMC
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
322 if(s->swap_uv){
323 exchange_uv(s);
326 #endif
328 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
329 if(s->flags2 & CODEC_FLAG2_FAST){
330 for(i=0;i<6;i++) {
331 mpeg2_fast_decode_block_intra(s, s->pblocks[i], i);
333 }else{
334 for(i=0;i<mb_block_count;i++) {
335 if (mpeg2_decode_block_intra(s, s->pblocks[i], i) < 0)
336 return -1;
339 } else {
340 for(i=0;i<6;i++) {
341 if (mpeg1_decode_block_intra(s, s->pblocks[i], i) < 0)
342 return -1;
345 } else {
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;
354 }else{
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;
367 s->mv[0][0][0] = 0;
368 s->mv[0][0][1] = 0;
369 }else{
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;
375 else{
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);
384 /* motion vectors */
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;
392 for(i=0;i<2;i++) {
393 if (USES_LIST(mb_type, i)) {
394 /* MT_FRAME */
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 */
400 if (s->full_pel[i]){
401 s->mv[i][0][0] <<= 1;
402 s->mv[i][0][1] <<= 1;
406 } else {
407 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
408 s->mv_type = MV_TYPE_16X8;
409 for(i=0;i<2;i++) {
410 if (USES_LIST(mb_type, i)) {
411 /* MT_16X8 */
412 for(j=0;j<2;j++) {
413 s->field_select[i][j] = get_bits1(&s->gb);
414 for(k=0;k<2;k++) {
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;
424 break;
425 case MT_FIELD:
426 s->mv_type = MV_TYPE_FIELD;
427 if (s->picture_structure == PICT_FRAME) {
428 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
429 for(i=0;i<2;i++) {
430 if (USES_LIST(mb_type, i)) {
431 for(j=0;j<2;j++) {
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);
446 } else {
447 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
448 for(i=0;i<2;i++) {
449 if (USES_LIST(mb_type, i)) {
450 s->field_select[i][0] = get_bits1(&s->gb);
451 for(k=0;k<2;k++) {
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;
461 break;
462 case MT_DMV:
463 s->mv_type = MV_TYPE_DMV;
464 for(i=0;i<2;i++) {
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;
471 dmx = get_dmv(s);
472 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
473 s->last_mv[i][0][1] >> 1);
474 dmy = get_dmv(s);
477 s->last_mv[i][0][1] = my<<1;
478 s->last_mv[i][1][1] = my<<1;
480 s->mv[i][0][0] = mx;
481 s->mv[i][0][1] = my;
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;
494 m = 4 - m;
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;
497 } else {
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)
503 s->mv[i][2][1]--;
504 else
505 s->mv[i][2][1]++;
509 break;
510 default:
511 av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
512 return -1;
516 s->mb_intra = 0;
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]);
526 if (cbp <= 0){
527 av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
528 return -1;
531 #ifdef HAVE_XVMC
532 //if 1, we memcpy blocks in xvmcvideo
533 if(s->avctx->xvmc_acceleration > 1){
534 XVMC_pack_pblocks(s,cbp);
535 if(s->swap_uv){
536 exchange_uv(s);
539 #endif
541 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
542 if(s->flags2 & CODEC_FLAG2_FAST){
543 for(i=0;i<6;i++) {
544 if(cbp & 32) {
545 mpeg2_fast_decode_block_non_intra(s, s->pblocks[i], i);
546 } else {
547 s->block_last_index[i] = -1;
549 cbp+=cbp;
551 }else{
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)
557 return -1;
558 } else {
559 s->block_last_index[i] = -1;
561 cbp+=cbp;
564 } else {
565 if(s->flags2 & CODEC_FLAG2_FAST){
566 for(i=0;i<6;i++) {
567 if (cbp & 32) {
568 mpeg1_fast_decode_block_inter(s, s->pblocks[i], i);
569 } else {
570 s->block_last_index[i] = -1;
572 cbp+=cbp;
574 }else{
575 for(i=0;i<6;i++) {
576 if (cbp & 32) {
577 if (mpeg1_decode_block_inter(s, s->pblocks[i], i) < 0)
578 return -1;
579 } else {
580 s->block_last_index[i] = -1;
582 cbp+=cbp;
586 }else{
587 for(i=0;i<12;i++)
588 s->block_last_index[i] = -1;
592 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
594 return 0;
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);
603 if (code == 0) {
604 return pred;
606 if (code < 0) {
607 return 0xffff;
610 sign = get_bits1(&s->gb);
611 shift = fcode - 1;
612 val = code;
613 if (shift) {
614 val = (val - 1) << shift;
615 val |= get_bits(&s->gb, shift);
616 val++;
618 if (sign)
619 val = -val;
620 val += pred;
622 /* modulo decoding */
623 l= INT_BIT - 5 - shift;
624 val = (val<<l)>>l;
625 return val;
628 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
629 DCTELEM *block,
630 int n)
632 int level, dc, diff, i, j, run;
633 int component;
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;
639 /* DC coefficient */
640 component = (n <= 3 ? 0 : n - 4 + 1);
641 diff = decode_dc(&s->gb, component);
642 if (diff >= 0xffff)
643 return -1;
644 dc = s->last_dc[component];
645 dc += diff;
646 s->last_dc[component] = dc;
647 block[0] = dc<<3;
648 dprintf(s->avctx, "dc=%d diff=%d\n", dc, diff);
649 i = 0;
651 OPEN_READER(re, &s->gb);
652 /* now quantify & encode AC coefficients */
653 for(;;) {
654 UPDATE_CACHE(re, &s->gb);
655 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
657 if(level == 127){
658 break;
659 } else if(level != 0) {
660 i += run;
661 j = scantable[i];
662 level= (level*qscale*quant_matrix[j])>>4;
663 level= (level-1)|1;
664 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
665 LAST_SKIP_BITS(re, &s->gb, 1);
666 } else {
667 /* escape */
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);
671 if (level == -128) {
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);
676 i += run;
677 j = scantable[i];
678 if(level<0){
679 level= -level;
680 level= (level*qscale*quant_matrix[j])>>4;
681 level= (level-1)|1;
682 level= -level;
683 }else{
684 level= (level*qscale*quant_matrix[j])>>4;
685 level= (level-1)|1;
688 if (i > 63){
689 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
690 return -1;
693 block[j] = level;
695 CLOSE_READER(re, &s->gb);
697 s->block_last_index[n] = i;
698 return 0;
701 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
702 DCTELEM *block,
703 int n)
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);
713 i = -1;
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;
718 level= (level-1)|1;
719 if(GET_CACHE(re, &s->gb)&0x40000000)
720 level= -level;
721 block[0] = level;
722 i++;
723 SKIP_BITS(re, &s->gb, 2);
724 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
725 goto end;
727 #if MIN_CACHE_BITS < 19
728 UPDATE_CACHE(re, &s->gb);
729 #endif
730 /* now quantify & encode AC coefficients */
731 for(;;) {
732 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
734 if(level != 0) {
735 i += run;
736 j = scantable[i];
737 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
738 level= (level-1)|1;
739 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
740 SKIP_BITS(re, &s->gb, 1);
741 } else {
742 /* escape */
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);
746 if (level == -128) {
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);
751 i += run;
752 j = scantable[i];
753 if(level<0){
754 level= -level;
755 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
756 level= (level-1)|1;
757 level= -level;
758 }else{
759 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
760 level= (level-1)|1;
763 if (i > 63){
764 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
765 return -1;
768 block[j] = level;
769 #if MIN_CACHE_BITS < 19
770 UPDATE_CACHE(re, &s->gb);
771 #endif
772 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
773 break;
774 #if MIN_CACHE_BITS >= 19
775 UPDATE_CACHE(re, &s->gb);
776 #endif
778 end:
779 LAST_SKIP_BITS(re, &s->gb, 2);
780 CLOSE_READER(re, &s->gb);
782 s->block_last_index[n] = i;
783 return 0;
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);
795 i = -1;
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;
800 level= (level-1)|1;
801 if(GET_CACHE(re, &s->gb)&0x40000000)
802 level= -level;
803 block[0] = level;
804 i++;
805 SKIP_BITS(re, &s->gb, 2);
806 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
807 goto end;
809 #if MIN_CACHE_BITS < 19
810 UPDATE_CACHE(re, &s->gb);
811 #endif
813 /* now quantify & encode AC coefficients */
814 for(;;) {
815 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
817 if(level != 0) {
818 i += run;
819 j = scantable[i];
820 level= ((level*2+1)*qscale)>>1;
821 level= (level-1)|1;
822 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
823 SKIP_BITS(re, &s->gb, 1);
824 } else {
825 /* escape */
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);
829 if (level == -128) {
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);
834 i += run;
835 j = scantable[i];
836 if(level<0){
837 level= -level;
838 level= ((level*2+1)*qscale)>>1;
839 level= (level-1)|1;
840 level= -level;
841 }else{
842 level= ((level*2+1)*qscale)>>1;
843 level= (level-1)|1;
847 block[j] = level;
848 #if MIN_CACHE_BITS < 19
849 UPDATE_CACHE(re, &s->gb);
850 #endif
851 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
852 break;
853 #if MIN_CACHE_BITS >= 19
854 UPDATE_CACHE(re, &s->gb);
855 #endif
857 end:
858 LAST_SKIP_BITS(re, &s->gb, 2);
859 CLOSE_READER(re, &s->gb);
861 s->block_last_index[n] = i;
862 return 0;
866 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
867 DCTELEM *block,
868 int n)
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;
875 int mismatch;
877 mismatch = 1;
880 OPEN_READER(re, &s->gb);
881 i = -1;
882 if (n < 4)
883 quant_matrix = s->inter_matrix;
884 else
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)
892 level= -level;
893 block[0] = level;
894 mismatch ^= level;
895 i++;
896 SKIP_BITS(re, &s->gb, 2);
897 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
898 goto end;
900 #if MIN_CACHE_BITS < 19
901 UPDATE_CACHE(re, &s->gb);
902 #endif
904 /* now quantify & encode AC coefficients */
905 for(;;) {
906 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
908 if(level != 0) {
909 i += run;
910 j = scantable[i];
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);
914 } else {
915 /* escape */
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);
920 i += run;
921 j = scantable[i];
922 if(level<0){
923 level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
924 level= -level;
925 }else{
926 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
929 if (i > 63){
930 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
931 return -1;
934 mismatch ^= level;
935 block[j] = level;
936 #if MIN_CACHE_BITS < 19
937 UPDATE_CACHE(re, &s->gb);
938 #endif
939 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
940 break;
941 #if MIN_CACHE_BITS >= 19
942 UPDATE_CACHE(re, &s->gb);
943 #endif
945 end:
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;
952 return 0;
955 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
956 DCTELEM *block,
957 int n)
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);
964 i = -1;
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)
971 level= -level;
972 block[0] = level;
973 i++;
974 SKIP_BITS(re, &s->gb, 2);
975 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
976 goto end;
978 #if MIN_CACHE_BITS < 19
979 UPDATE_CACHE(re, &s->gb);
980 #endif
982 /* now quantify & encode AC coefficients */
983 for(;;) {
984 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
986 if(level != 0) {
987 i += run;
988 j = scantable[i];
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);
992 } else {
993 /* escape */
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);
998 i += run;
999 j = scantable[i];
1000 if(level<0){
1001 level= ((-level*2+1)*qscale)>>1;
1002 level= -level;
1003 }else{
1004 level= ((level*2+1)*qscale)>>1;
1008 block[j] = level;
1009 #if MIN_CACHE_BITS < 19
1010 UPDATE_CACHE(re, &s->gb);
1011 #endif
1012 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
1013 break;
1014 #if MIN_CACHE_BITS >=19
1015 UPDATE_CACHE(re, &s->gb);
1016 #endif
1018 end:
1019 LAST_SKIP_BITS(re, &s->gb, 2);
1020 CLOSE_READER(re, &s->gb);
1021 s->block_last_index[n] = i;
1022 return 0;
1026 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
1027 DCTELEM *block,
1028 int n)
1030 int level, dc, diff, i, j, run;
1031 int component;
1032 RLTable *rl;
1033 uint8_t * const scantable= s->intra_scantable.permutated;
1034 const uint16_t *quant_matrix;
1035 const int qscale= s->qscale;
1036 int mismatch;
1038 /* DC coefficient */
1039 if (n < 4){
1040 quant_matrix = s->intra_matrix;
1041 component = 0;
1042 }else{
1043 quant_matrix = s->chroma_intra_matrix;
1044 component = (n&1) + 1;
1046 diff = decode_dc(&s->gb, component);
1047 if (diff >= 0xffff)
1048 return -1;
1049 dc = s->last_dc[component];
1050 dc += diff;
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;
1055 i = 0;
1056 if (s->intra_vlc_format)
1057 rl = &ff_rl_mpeg2;
1058 else
1059 rl = &ff_rl_mpeg1;
1062 OPEN_READER(re, &s->gb);
1063 /* now quantify & encode AC coefficients */
1064 for(;;) {
1065 UPDATE_CACHE(re, &s->gb);
1066 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1068 if(level == 127){
1069 break;
1070 } else if(level != 0) {
1071 i += run;
1072 j = scantable[i];
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);
1076 } else {
1077 /* escape */
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);
1081 i += run;
1082 j = scantable[i];
1083 if(level<0){
1084 level= (-level*qscale*quant_matrix[j])>>4;
1085 level= -level;
1086 }else{
1087 level= (level*qscale*quant_matrix[j])>>4;
1090 if (i > 63){
1091 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
1092 return -1;
1095 mismatch^= level;
1096 block[j] = level;
1098 CLOSE_READER(re, &s->gb);
1100 block[63]^= mismatch&1;
1102 s->block_last_index[n] = i;
1103 return 0;
1106 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
1107 DCTELEM *block,
1108 int n)
1110 int level, dc, diff, j, run;
1111 int component;
1112 RLTable *rl;
1113 uint8_t * scantable= s->intra_scantable.permutated;
1114 const uint16_t *quant_matrix;
1115 const int qscale= s->qscale;
1117 /* DC coefficient */
1118 if (n < 4){
1119 quant_matrix = s->intra_matrix;
1120 component = 0;
1121 }else{
1122 quant_matrix = s->chroma_intra_matrix;
1123 component = (n&1) + 1;
1125 diff = decode_dc(&s->gb, component);
1126 if (diff >= 0xffff)
1127 return -1;
1128 dc = s->last_dc[component];
1129 dc += diff;
1130 s->last_dc[component] = dc;
1131 block[0] = dc << (3 - s->intra_dc_precision);
1132 if (s->intra_vlc_format)
1133 rl = &ff_rl_mpeg2;
1134 else
1135 rl = &ff_rl_mpeg1;
1138 OPEN_READER(re, &s->gb);
1139 /* now quantify & encode AC coefficients */
1140 for(;;) {
1141 UPDATE_CACHE(re, &s->gb);
1142 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
1144 if(level == 127){
1145 break;
1146 } else if(level != 0) {
1147 scantable += run;
1148 j = *scantable;
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);
1152 } else {
1153 /* escape */
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);
1157 scantable += run;
1158 j = *scantable;
1159 if(level<0){
1160 level= (-level*qscale*quant_matrix[j])>>4;
1161 level= -level;
1162 }else{
1163 level= (level*qscale*quant_matrix[j])>>4;
1167 block[j] = level;
1169 CLOSE_READER(re, &s->gb);
1172 s->block_last_index[n] = scantable - s->intra_scantable.permutated;
1173 return 0;
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 */
1181 int slice_count;
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
1187 } Mpeg1Context;
1189 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1191 Mpeg1Context *s = avctx->priv_data;
1192 MpegEncContext *s2 = &s->mpeg_enc_ctx;
1193 int i;
1195 /* we need some permutation to store matrices,
1196 * until MPV_common_init() sets the real permutation. */
1197 for(i=0;i<64;i++)
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;
1212 return 0;
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];
1218 int i;
1220 memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
1222 for(i=0;i<64;i++){
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)
1229 return -1;
1230 if(avctx->sub_id == 1)
1231 avctx->pix_fmt = avctx->get_format(avctx,pixfmt_vdpau_mpg1_420);
1232 else
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);
1237 else
1238 return -1;
1239 return 0;
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];
1250 if (
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;
1263 MPV_common_end(s);
1264 s->parse_context= pc;
1267 if( (s->width == 0 )||(s->height == 0))
1268 return -2;
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
1281 //MPEG-1 fps
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;
1284 //MPEG-1 aspect
1285 avctx->sample_aspect_ratio= av_d2q(
1286 1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
1288 }else{//MPEG-2
1289 //MPEG-2 fps
1290 av_reduce(
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,
1295 1<<30);
1296 //MPEG-2 aspect
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=
1302 av_div_q(
1303 ff_mpeg2_aspect[s->aspect_ratio_info],
1304 (AVRational){s->width, s->height}
1306 }else{
1307 s->avctx->sample_aspect_ratio=
1308 av_div_q(
1309 ff_mpeg2_aspect[s->aspect_ratio_info],
1310 (AVRational){s1->pan_scan.width, s1->pan_scan.height}
1313 }else{
1314 s->avctx->sample_aspect_ratio=
1315 ff_mpeg2_aspect[s->aspect_ratio_info];
1317 }//MPEG-2
1319 if(avctx->vdpau_acceleration){
1320 if(vdpau_fill_pix_fmt(s, avctx)<0)
1321 return -2;
1322 }else
1323 if(avctx->xvmc_acceleration){
1324 avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
1325 }else{
1326 if(s->chroma_format < 2){
1327 avctx->pix_fmt = PIX_FMT_YUV420P;
1328 }else
1329 if(s->chroma_format == 2){
1330 avctx->pix_fmt = PIX_FMT_YUV422P;
1331 }else
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)
1346 return -2;
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;
1355 return 0;
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)
1366 return -2;
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)
1373 return -1;
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)
1380 return -1;
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)
1388 return -1;
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);
1398 s->y_dc_scale = 8;
1399 s->c_dc_scale = 8;
1400 s->first_slice = 1;
1401 return 0;
1404 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
1406 MpegEncContext *s= &s1->mpeg_enc_ctx;
1407 int horiz_size_ext, vert_size_ext;
1408 int bit_rate_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;
1467 int i,nofco;
1469 nofco = 1;
1470 if(s->progressive_sequence){
1471 if(s->repeat_first_field){
1472 nofco++;
1473 if(s->top_field_first)
1474 nofco++;
1476 }else{
1477 if(s->picture_structure == PICT_FRAME){
1478 nofco++;
1479 if(s->repeat_first_field)
1480 nofco++;
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)
1500 int i, v, j;
1502 dprintf(s->avctx, "matrix extension\n");
1504 if (get_bits1(&s->gb)) {
1505 for(i=0;i<64;i++) {
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)) {
1513 for(i=0;i<64;i++) {
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)) {
1521 for(i=0;i<64;i++) {
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)) {
1528 for(i=0;i<64;i++) {
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;
1550 else
1551 s->pict_type= FF_P_TYPE;
1552 }else
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;
1556 s->first_slice= 1;
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){
1571 s->first_field=0;
1572 s->v_edge_pos= 16*s->mb_height;
1573 }else{
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);
1582 }else{
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;
1604 int ext_type;
1606 init_get_bits(&s->gb, buf, buf_size*8);
1608 ext_type = get_bits(&s->gb, 4);
1609 switch(ext_type) {
1610 case 0x1:
1611 mpeg_decode_sequence_extension(s1);
1612 break;
1613 case 0x2:
1614 mpeg_decode_sequence_display_extension(s1);
1615 break;
1616 case 0x3:
1617 mpeg_decode_quant_matrix_extension(s);
1618 break;
1619 case 0x7:
1620 mpeg_decode_picture_display_extension(s1);
1621 break;
1622 case 0x8:
1623 mpeg_decode_picture_coding_extension(s1);
1624 break;
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)
1641 return -1;
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;
1651 else
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
1660 int i;
1662 if(!s->current_picture_ptr){
1663 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
1664 return -1;
1667 for(i=0; i<4; i++){
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];
1674 #ifdef HAVE_XVMC
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);
1679 #endif
1681 #ifdef HAVE_VDPAU
1682 if(s->avctx->vdpau_acceleration)
1683 ff_VDPAU_mpeg_field_start(s);
1684 #endif
1686 return 0;
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;
1705 s->resync_mb_x=
1706 s->resync_mb_y= -1;
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);
1710 return -1;
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);
1720 if(s->qscale == 0){
1721 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
1722 return -1;
1725 /* extra slice info */
1726 while (get_bits1(&s->gb) != 0) {
1727 skip_bits(&s->gb, 8);
1730 s->mb_x=0;
1732 for(;;) {
1733 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1734 if (code < 0){
1735 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
1736 return -1;
1738 if (code >= 33) {
1739 if (code == 33) {
1740 s->mb_x += 33;
1742 /* otherwise, stuffing, nothing to do */
1743 } else {
1744 s->mb_x += code;
1745 break;
1748 if(s->mb_x >= (unsigned)s->mb_width){
1749 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
1750 return -1;
1753 s->resync_mb_x= s->mb_x;
1754 s->resync_mb_y= s->mb_y= mb_y;
1755 s->mb_skip_run= 0;
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" :"");
1769 for(;;) {
1770 #ifdef HAVE_XVMC
1771 //If 1, we memcpy blocks in xvmcvideo.
1772 if(s->avctx->xvmc_acceleration > 1)
1773 XVMC_init_block(s);//set s->block
1774 #endif
1776 if(mpeg_decode_mb(s, s->block) < 0)
1777 return -1;
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)
1784 xy += wrap/2;
1786 for(i=0; i<2; i++){
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);
1806 xy += wrap;
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);
1821 s->mb_x = 0;
1822 s->mb_y++;
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)));
1833 return -1;
1834 }else
1835 goto eos;
1838 ff_init_block_index(s);
1841 /* skip mb handling */
1842 if (s->mb_skip_run == -1) {
1843 /* read increment again */
1844 s->mb_skip_run = 0;
1845 for(;;) {
1846 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
1847 if (code < 0){
1848 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
1849 return -1;
1851 if (code >= 33) {
1852 if (code == 33) {
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");
1857 return -1;
1859 goto eos; /* end of slice */
1861 /* otherwise, stuffing, nothing to do */
1862 } else {
1863 s->mb_skip_run += code;
1864 break;
1867 if(s->mb_skip_run){
1868 int i;
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);
1871 return -1;
1874 /* skip mb */
1875 s->mb_intra = 0;
1876 for(i=0;i<12;i++)
1877 s->block_last_index[i] = -1;
1878 if(s->picture_structure == PICT_FRAME)
1879 s->mv_type = MV_TYPE_16X16;
1880 else
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;
1889 } else {
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);
1902 return 0;
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;
1912 for(;;){
1913 uint32_t start_code;
1914 int ret;
1916 ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
1917 emms_c();
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);
1920 if(ret < 0){
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);
1923 }else{
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)
1928 return 0;
1930 start_code= -1;
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)
1934 return -1;
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)
1950 return 0;
1952 #ifdef HAVE_XVMC
1953 if(s->avctx->xvmc_acceleration)
1954 XVMC_field_end(s);
1955 #endif
1956 /* end of slice reached */
1957 if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) {
1958 /* end of image */
1960 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
1962 #ifdef HAVE_VDPAU
1963 if(!s->avctx->vdpau_acceleration)
1964 #endif
1965 ff_er_frame_end(s);
1967 MPV_frame_end(s);
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);
1972 } else {
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);
1982 return 1;
1983 } else {
1984 return 0;
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;
1993 int width,height;
1994 int i, v, j;
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)
2001 return -1;
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)
2006 return -1;
2008 s->frame_rate_index = get_bits(&s->gb, 4);
2009 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
2010 return -1;
2011 s->bit_rate = get_bits(&s->gb, 18) * 400;
2012 if (get_bits1(&s->gb) == 0) /* marker */
2013 return -1;
2014 s->width = width;
2015 s->height = height;
2017 s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
2018 skip_bits(&s->gb, 1);
2020 /* get matrix */
2021 if (get_bits1(&s->gb)) {
2022 for(i=0;i<64;i++) {
2023 v = get_bits(&s->gb, 8);
2024 if(v==0){
2025 av_log(s->avctx, AV_LOG_ERROR, "intra matrix damaged\n");
2026 return -1;
2028 j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2029 s->intra_matrix[j] = v;
2030 s->chroma_intra_matrix[j] = v;
2032 #ifdef DEBUG
2033 dprintf(s->avctx, "intra matrix present\n");
2034 for(i=0;i<64;i++)
2035 dprintf(s->avctx, " %d", s->intra_matrix[s->dsp.idct_permutation[i]]);
2036 dprintf(s->avctx, "\n");
2037 #endif
2038 } else {
2039 for(i=0;i<64;i++) {
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)) {
2047 for(i=0;i<64;i++) {
2048 v = get_bits(&s->gb, 8);
2049 if(v==0){
2050 av_log(s->avctx, AV_LOG_ERROR, "inter matrix damaged\n");
2051 return -1;
2053 j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
2054 s->inter_matrix[j] = v;
2055 s->chroma_inter_matrix[j] = v;
2057 #ifdef DEBUG
2058 dprintf(s->avctx, "non-intra matrix present\n");
2059 for(i=0;i<64;i++)
2060 dprintf(s->avctx, " %d", s->inter_matrix[s->dsp.idct_permutation[i]]);
2061 dprintf(s->avctx, "\n");
2062 #endif
2063 } else {
2064 for(i=0;i<64;i++) {
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");
2074 return -1;
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);
2093 return 0;
2096 static int vcr2_init_sequence(AVCodecContext *avctx)
2098 Mpeg1Context *s1 = avctx->priv_data;
2099 MpegEncContext *s = &s1->mpeg_enc_ctx;
2100 int i, v;
2102 /* start new MPEG-1 context decoding */
2103 s->out_format = FMT_MPEG1;
2104 if (s1->mpeg_enc_ctx_allocated) {
2105 MPV_common_end(s);
2107 s->width = avctx->coded_width;
2108 s->height = avctx->coded_height;
2109 avctx->has_b_frames= 0; //true?
2110 s->low_delay= 1;
2112 if(avctx->vdpau_acceleration){
2113 if(vdpau_fill_pix_fmt(s, avctx)<0)
2114 return -2;
2115 }else
2116 if(avctx->xvmc_acceleration){
2117 avctx->pix_fmt = avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
2118 }else{
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)
2127 return -1;
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;
2132 for(i=0;i<64;i++) {
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 */
2150 return 0;
2154 static void mpeg_decode_user_data(AVCodecContext *avctx,
2155 const uint8_t *buf, int buf_size)
2157 const uint8_t *p;
2158 int len, flags;
2159 p = buf;
2160 len = buf_size;
2162 /* we parse the DTG active format information */
2163 if (len >= 5 &&
2164 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
2165 flags = p[4];
2166 p += 5;
2167 len -= 5;
2168 if (flags & 0x80) {
2169 /* skip event id */
2170 if (len < 2)
2171 return;
2172 p += 2;
2173 len -= 2;
2175 if (flags & 0x40) {
2176 if (len < 1)
2177 return;
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)
2220 int i;
2221 uint32_t state= pc->state;
2223 /* EOF considered as end of frame */
2224 if (buf_size == 0)
2225 return 0;
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
2232 4 searching end
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;
2244 state++;
2245 }else{
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){
2248 i++;
2249 pc->frame_start_found=4;
2251 if(state == SEQ_END_CODE){
2252 pc->state=-1;
2253 return i+1;
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;
2262 pc->state=-1;
2263 return i-3;
2268 pc->state= state;
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);
2294 return buf_size;
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 )
2301 return buf_size;
2304 #if 0
2305 if (s->repeat_field % 2 == 1) {
2306 s->repeat_field++;
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);
2311 goto the_end;
2314 #endif
2316 if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == ff_get_fourcc("VCR2"))
2317 vcr2_init_sequence(avctx);
2319 s->slice_count= 0;
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;
2337 for(;;) {
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){
2344 int i;
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;
2351 #ifdef HAVE_VDPAU
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);
2356 #endif
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);
2363 s2->pict_type= 0;
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,
2377 input_size);
2378 break;
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)
2384 s2->pict_type=0;
2385 break;
2386 case EXT_START_CODE:
2387 mpeg_decode_extension(avctx,
2388 buf_ptr, input_size);
2389 break;
2390 case USER_START_CODE:
2391 mpeg_decode_user_data(avctx,
2392 buf_ptr, input_size);
2393 break;
2394 case GOP_START_CODE:
2395 s2->first_field=0;
2396 mpeg_decode_gop(avctx,
2397 buf_ptr, input_size);
2398 break;
2399 default:
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)
2417 break;
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)
2425 break;
2428 if(!s2->pict_type){
2429 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
2430 break;
2433 if(s2->first_slice){
2434 s2->first_slice=0;
2435 if(mpeg_field_start(s2) < 0)
2436 return -1;
2438 if(!s2->current_picture_ptr){
2439 av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
2440 return -1;
2443 if (avctx->vdpau_acceleration) {
2444 s->slice_count++;
2445 break;
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;
2455 if(s->slice_count){
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);
2460 s->slice_count++;
2462 buf_ptr += 2; //FIXME add minimum number of bytes per slice
2463 }else{
2464 ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
2465 emms_c();
2467 if(ret < 0){
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);
2470 }else{
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);
2475 break;
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);
2486 return 0;
2489 AVCodec mpeg1video_decoder = {
2490 "mpeg1video",
2491 CODEC_TYPE_VIDEO,
2492 CODEC_ID_MPEG1VIDEO,
2493 sizeof(Mpeg1Context),
2494 mpeg_decode_init,
2495 NULL,
2496 mpeg_decode_end,
2497 mpeg_decode_frame,
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 = {
2504 "mpeg2video",
2505 CODEC_TYPE_VIDEO,
2506 CODEC_ID_MPEG2VIDEO,
2507 sizeof(Mpeg1Context),
2508 mpeg_decode_init,
2509 NULL,
2510 mpeg_decode_end,
2511 mpeg_decode_frame,
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"),
2517 //legacy decoder
2518 AVCodec mpegvideo_decoder = {
2519 "mpegvideo",
2520 CODEC_TYPE_VIDEO,
2521 CODEC_ID_MPEG2VIDEO,
2522 sizeof(Mpeg1Context),
2523 mpeg_decode_init,
2524 NULL,
2525 mpeg_decode_end,
2526 mpeg_decode_frame,
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"),
2532 #ifdef HAVE_XVMC
2533 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx){
2534 Mpeg1Context *s;
2536 if( avctx->thread_count > 1)
2537 return -1;
2538 if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
2539 return -1;
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!
2549 return 0;
2552 AVCodec mpeg_xvmc_decoder = {
2553 "mpegvideo_xvmc",
2554 CODEC_TYPE_VIDEO,
2555 CODEC_ID_MPEG2VIDEO_XVMC,
2556 sizeof(Mpeg1Context),
2557 mpeg_mc_decode_init,
2558 NULL,
2559 mpeg_decode_end,
2560 mpeg_decode_frame,
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)"),
2566 #endif
2568 #ifdef HAVE_VDPAU
2569 static av_cold int mpeg_vdpau_decode_init(AVCodecContext *avctx){
2570 if( avctx->thread_count > 1)
2571 return -1;
2572 if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
2573 return -1;
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;
2583 return 0;
2586 AVCodec mpeg_vdpau_decoder = {
2587 "mpegvideo_vdpau",
2588 CODEC_TYPE_VIDEO,
2589 CODEC_ID_MPEGVIDEO_VDPAU,
2590 sizeof(Mpeg1Context),
2591 mpeg_vdpau_decode_init,
2592 NULL,
2593 mpeg_decode_end,
2594 mpeg_decode_frame,
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)"),
2599 #endif