00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00028
00029 #include "internal.h"
00030 #include "avcodec.h"
00031 #include "dsputil.h"
00032 #include "mpegvideo.h"
00033 #include "libavutil/avassert.h"
00034
00035 #include "mpeg12.h"
00036 #include "mpeg12data.h"
00037 #include "mpeg12decdata.h"
00038 #include "bytestream.h"
00039 #include "vdpau_internal.h"
00040 #include "xvmc_internal.h"
00041 #include "thread.h"
00042
00043
00044
00045
00046
00047 #define MV_VLC_BITS 9
00048 #define MBINCR_VLC_BITS 9
00049 #define MB_PAT_VLC_BITS 9
00050 #define MB_PTYPE_VLC_BITS 6
00051 #define MB_BTYPE_VLC_BITS 6
00052
00053 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
00054 DCTELEM *block,
00055 int n);
00056 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
00057 DCTELEM *block,
00058 int n);
00059 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n);
00060 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
00061 DCTELEM *block,
00062 int n);
00063 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
00064 DCTELEM *block,
00065 int n);
00066 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n);
00067 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n);
00068 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
00069 static void exchange_uv(MpegEncContext *s);
00070
00071 uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
00072
00073
00074 #define INIT_2D_VLC_RL(rl, static_size)\
00075 {\
00076 static RL_VLC_ELEM rl_vlc_table[static_size];\
00077 INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
00078 &rl.table_vlc[0][1], 4, 2,\
00079 &rl.table_vlc[0][0], 4, 2, static_size);\
00080 \
00081 rl.rl_vlc[0]= rl_vlc_table;\
00082 init_2d_vlc_rl(&rl);\
00083 }
00084
00085 static void init_2d_vlc_rl(RLTable *rl)
00086 {
00087 int i;
00088
00089 for(i=0; i<rl->vlc.table_size; i++){
00090 int code= rl->vlc.table[i][0];
00091 int len = rl->vlc.table[i][1];
00092 int level, run;
00093
00094 if(len==0){
00095 run= 65;
00096 level= MAX_LEVEL;
00097 }else if(len<0){
00098 run= 0;
00099 level= code;
00100 }else{
00101 if(code==rl->n){
00102 run= 65;
00103 level= 0;
00104 }else if(code==rl->n+1){
00105 run= 0;
00106 level= 127;
00107 }else{
00108 run= rl->table_run [code] + 1;
00109 level= rl->table_level[code];
00110 }
00111 }
00112 rl->rl_vlc[0][i].len= len;
00113 rl->rl_vlc[0][i].level= level;
00114 rl->rl_vlc[0][i].run= run;
00115 }
00116 }
00117
00118 void ff_mpeg12_common_init(MpegEncContext *s)
00119 {
00120
00121 s->y_dc_scale_table=
00122 s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
00123
00124 }
00125
00126 void ff_mpeg1_clean_buffers(MpegEncContext *s){
00127 s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
00128 s->last_dc[1] = s->last_dc[0];
00129 s->last_dc[2] = s->last_dc[0];
00130 memset(s->last_mv, 0, sizeof(s->last_mv));
00131 }
00132
00133
00134
00135
00136
00137 VLC ff_dc_lum_vlc;
00138 VLC ff_dc_chroma_vlc;
00139
00140 static VLC mv_vlc;
00141 static VLC mbincr_vlc;
00142 static VLC mb_ptype_vlc;
00143 static VLC mb_btype_vlc;
00144 static VLC mb_pat_vlc;
00145
00146 av_cold void ff_mpeg12_init_vlcs(void)
00147 {
00148 static int done = 0;
00149
00150 if (!done) {
00151 done = 1;
00152
00153 INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
00154 ff_mpeg12_vlc_dc_lum_bits, 1, 1,
00155 ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
00156 INIT_VLC_STATIC(&ff_dc_chroma_vlc, DC_VLC_BITS, 12,
00157 ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
00158 ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
00159 INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
00160 &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
00161 &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
00162 INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
00163 &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
00164 &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
00165 INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
00166 &ff_mpeg12_mbPatTable[0][1], 2, 1,
00167 &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
00168
00169 INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
00170 &table_mb_ptype[0][1], 2, 1,
00171 &table_mb_ptype[0][0], 2, 1, 64);
00172 INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
00173 &table_mb_btype[0][1], 2, 1,
00174 &table_mb_btype[0][0], 2, 1, 64);
00175 init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
00176 init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
00177
00178 INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
00179 INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
00180 }
00181 }
00182
00183 static inline int get_dmv(MpegEncContext *s)
00184 {
00185 if(get_bits1(&s->gb))
00186 return 1 - (get_bits1(&s->gb) << 1);
00187 else
00188 return 0;
00189 }
00190
00191 static inline int get_qscale(MpegEncContext *s)
00192 {
00193 int qscale = get_bits(&s->gb, 5);
00194 if (s->q_scale_type) {
00195 return non_linear_qscale[qscale];
00196 } else {
00197 return qscale << 1;
00198 }
00199 }
00200
00201
00202 #define MT_FIELD 1
00203 #define MT_FRAME 2
00204 #define MT_16X8 2
00205 #define MT_DMV 3
00206
00207 static int mpeg_decode_mb(MpegEncContext *s,
00208 DCTELEM block[12][64])
00209 {
00210 int i, j, k, cbp, val, mb_type, motion_type;
00211 const int mb_block_count = 4 + (1<< s->chroma_format);
00212
00213 av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
00214
00215 assert(s->mb_skipped==0);
00216
00217 if (s->mb_skip_run-- != 0) {
00218 if (s->pict_type == AV_PICTURE_TYPE_P) {
00219 s->mb_skipped = 1;
00220 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
00221 } else {
00222 int mb_type;
00223
00224 if(s->mb_x)
00225 mb_type= s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride - 1];
00226 else
00227 mb_type= s->current_picture.mb_type[ s->mb_width + (s->mb_y-1)*s->mb_stride - 1];
00228 if(IS_INTRA(mb_type))
00229 return -1;
00230
00231 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]=
00232 mb_type | MB_TYPE_SKIP;
00233
00234
00235 if((s->mv[0][0][0]|s->mv[0][0][1]|s->mv[1][0][0]|s->mv[1][0][1])==0)
00236 s->mb_skipped = 1;
00237 }
00238
00239 return 0;
00240 }
00241
00242 switch(s->pict_type) {
00243 default:
00244 case AV_PICTURE_TYPE_I:
00245 if (get_bits1(&s->gb) == 0) {
00246 if (get_bits1(&s->gb) == 0){
00247 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
00248 return -1;
00249 }
00250 mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
00251 } else {
00252 mb_type = MB_TYPE_INTRA;
00253 }
00254 break;
00255 case AV_PICTURE_TYPE_P:
00256 mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
00257 if (mb_type < 0){
00258 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
00259 return -1;
00260 }
00261 mb_type = ptype2mb_type[ mb_type ];
00262 break;
00263 case AV_PICTURE_TYPE_B:
00264 mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
00265 if (mb_type < 0){
00266 av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
00267 return -1;
00268 }
00269 mb_type = btype2mb_type[ mb_type ];
00270 break;
00271 }
00272 av_dlog(s->avctx, "mb_type=%x\n", mb_type);
00273
00274 if (IS_INTRA(mb_type)) {
00275 s->dsp.clear_blocks(s->block[0]);
00276
00277 if(!s->chroma_y_shift){
00278 s->dsp.clear_blocks(s->block[6]);
00279 }
00280
00281
00282 if (s->picture_structure == PICT_FRAME &&
00283 !s->frame_pred_frame_dct) {
00284 s->interlaced_dct = get_bits1(&s->gb);
00285 }
00286
00287 if (IS_QUANT(mb_type))
00288 s->qscale = get_qscale(s);
00289
00290 if (s->concealment_motion_vectors) {
00291
00292 if (s->picture_structure != PICT_FRAME)
00293 skip_bits1(&s->gb);
00294
00295 s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
00296 mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
00297 s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
00298 mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
00299
00300 skip_bits1(&s->gb);
00301 }else
00302 memset(s->last_mv, 0, sizeof(s->last_mv));
00303 s->mb_intra = 1;
00304
00305 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1){
00306 ff_xvmc_pack_pblocks(s,-1);
00307 if(s->swap_uv){
00308 exchange_uv(s);
00309 }
00310 }
00311
00312 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
00313 if(s->flags2 & CODEC_FLAG2_FAST){
00314 for(i=0;i<6;i++) {
00315 mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
00316 }
00317 }else{
00318 for(i=0;i<mb_block_count;i++) {
00319 if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
00320 return -1;
00321 }
00322 }
00323 } else {
00324 for(i=0;i<6;i++) {
00325 if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
00326 return -1;
00327 }
00328 }
00329 } else {
00330 if (mb_type & MB_TYPE_ZERO_MV){
00331 assert(mb_type & MB_TYPE_CBP);
00332
00333 s->mv_dir = MV_DIR_FORWARD;
00334 if(s->picture_structure == PICT_FRAME){
00335 if(!s->frame_pred_frame_dct)
00336 s->interlaced_dct = get_bits1(&s->gb);
00337 s->mv_type = MV_TYPE_16X16;
00338 }else{
00339 s->mv_type = MV_TYPE_FIELD;
00340 mb_type |= MB_TYPE_INTERLACED;
00341 s->field_select[0][0]= s->picture_structure - 1;
00342 }
00343
00344 if (IS_QUANT(mb_type))
00345 s->qscale = get_qscale(s);
00346
00347 s->last_mv[0][0][0] = 0;
00348 s->last_mv[0][0][1] = 0;
00349 s->last_mv[0][1][0] = 0;
00350 s->last_mv[0][1][1] = 0;
00351 s->mv[0][0][0] = 0;
00352 s->mv[0][0][1] = 0;
00353 }else{
00354 assert(mb_type & MB_TYPE_L0L1);
00355
00356
00357 if (s->frame_pred_frame_dct)
00358 motion_type = MT_FRAME;
00359 else{
00360 motion_type = get_bits(&s->gb, 2);
00361 if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
00362 s->interlaced_dct = get_bits1(&s->gb);
00363 }
00364
00365 if (IS_QUANT(mb_type))
00366 s->qscale = get_qscale(s);
00367
00368
00369 s->mv_dir= (mb_type>>13)&3;
00370 av_dlog(s->avctx, "motion_type=%d\n", motion_type);
00371 switch(motion_type) {
00372 case MT_FRAME:
00373 if (s->picture_structure == PICT_FRAME) {
00374 mb_type |= MB_TYPE_16x16;
00375 s->mv_type = MV_TYPE_16X16;
00376 for(i=0;i<2;i++) {
00377 if (USES_LIST(mb_type, i)) {
00378
00379 s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
00380 mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
00381 s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
00382 mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
00383
00384 if (s->full_pel[i]){
00385 s->mv[i][0][0] <<= 1;
00386 s->mv[i][0][1] <<= 1;
00387 }
00388 }
00389 }
00390 } else {
00391 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
00392 s->mv_type = MV_TYPE_16X8;
00393 for(i=0;i<2;i++) {
00394 if (USES_LIST(mb_type, i)) {
00395
00396 for(j=0;j<2;j++) {
00397 s->field_select[i][j] = get_bits1(&s->gb);
00398 for(k=0;k<2;k++) {
00399 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
00400 s->last_mv[i][j][k]);
00401 s->last_mv[i][j][k] = val;
00402 s->mv[i][j][k] = val;
00403 }
00404 }
00405 }
00406 }
00407 }
00408 break;
00409 case MT_FIELD:
00410 if(s->progressive_sequence){
00411 av_log(s->avctx, AV_LOG_ERROR, "MT_FIELD in progressive_sequence\n");
00412 return -1;
00413 }
00414 s->mv_type = MV_TYPE_FIELD;
00415 if (s->picture_structure == PICT_FRAME) {
00416 mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
00417 for(i=0;i<2;i++) {
00418 if (USES_LIST(mb_type, i)) {
00419 for(j=0;j<2;j++) {
00420 s->field_select[i][j] = get_bits1(&s->gb);
00421 val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
00422 s->last_mv[i][j][0]);
00423 s->last_mv[i][j][0] = val;
00424 s->mv[i][j][0] = val;
00425 av_dlog(s->avctx, "fmx=%d\n", val);
00426 val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
00427 s->last_mv[i][j][1] >> 1);
00428 s->last_mv[i][j][1] = val << 1;
00429 s->mv[i][j][1] = val;
00430 av_dlog(s->avctx, "fmy=%d\n", val);
00431 }
00432 }
00433 }
00434 } else {
00435 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
00436 for(i=0;i<2;i++) {
00437 if (USES_LIST(mb_type, i)) {
00438 s->field_select[i][0] = get_bits1(&s->gb);
00439 for(k=0;k<2;k++) {
00440 val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
00441 s->last_mv[i][0][k]);
00442 s->last_mv[i][0][k] = val;
00443 s->last_mv[i][1][k] = val;
00444 s->mv[i][0][k] = val;
00445 }
00446 }
00447 }
00448 }
00449 break;
00450 case MT_DMV:
00451 if(s->progressive_sequence){
00452 av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
00453 return -1;
00454 }
00455 s->mv_type = MV_TYPE_DMV;
00456 for(i=0;i<2;i++) {
00457 if (USES_LIST(mb_type, i)) {
00458 int dmx, dmy, mx, my, m;
00459 const int my_shift= s->picture_structure == PICT_FRAME;
00460
00461 mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
00462 s->last_mv[i][0][0]);
00463 s->last_mv[i][0][0] = mx;
00464 s->last_mv[i][1][0] = mx;
00465 dmx = get_dmv(s);
00466 my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
00467 s->last_mv[i][0][1] >> my_shift);
00468 dmy = get_dmv(s);
00469
00470
00471 s->last_mv[i][0][1] = my<<my_shift;
00472 s->last_mv[i][1][1] = my<<my_shift;
00473
00474 s->mv[i][0][0] = mx;
00475 s->mv[i][0][1] = my;
00476 s->mv[i][1][0] = mx;
00477 s->mv[i][1][1] = my;
00478
00479 if (s->picture_structure == PICT_FRAME) {
00480 mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
00481
00482
00483 m = s->top_field_first ? 1 : 3;
00484
00485
00486 s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
00487 s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
00488 m = 4 - m;
00489 s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
00490 s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
00491 } else {
00492 mb_type |= MB_TYPE_16x16;
00493
00494 s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
00495 s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
00496 if(s->picture_structure == PICT_TOP_FIELD)
00497 s->mv[i][2][1]--;
00498 else
00499 s->mv[i][2][1]++;
00500 }
00501 }
00502 }
00503 break;
00504 default:
00505 av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
00506 return -1;
00507 }
00508 }
00509
00510 s->mb_intra = 0;
00511 if (HAS_CBP(mb_type)) {
00512 s->dsp.clear_blocks(s->block[0]);
00513
00514 cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
00515 if(mb_block_count > 6){
00516 cbp<<= mb_block_count-6;
00517 cbp |= get_bits(&s->gb, mb_block_count-6);
00518 s->dsp.clear_blocks(s->block[6]);
00519 }
00520 if (cbp <= 0){
00521 av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
00522 return -1;
00523 }
00524
00525
00526 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1){
00527 ff_xvmc_pack_pblocks(s,cbp);
00528 if(s->swap_uv){
00529 exchange_uv(s);
00530 }
00531 }
00532
00533 if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
00534 if(s->flags2 & CODEC_FLAG2_FAST){
00535 for(i=0;i<6;i++) {
00536 if(cbp & 32) {
00537 mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
00538 } else {
00539 s->block_last_index[i] = -1;
00540 }
00541 cbp+=cbp;
00542 }
00543 }else{
00544 cbp<<= 12-mb_block_count;
00545
00546 for(i=0;i<mb_block_count;i++) {
00547 if ( cbp & (1<<11) ) {
00548 if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
00549 return -1;
00550 } else {
00551 s->block_last_index[i] = -1;
00552 }
00553 cbp+=cbp;
00554 }
00555 }
00556 } else {
00557 if(s->flags2 & CODEC_FLAG2_FAST){
00558 for(i=0;i<6;i++) {
00559 if (cbp & 32) {
00560 mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
00561 } else {
00562 s->block_last_index[i] = -1;
00563 }
00564 cbp+=cbp;
00565 }
00566 }else{
00567 for(i=0;i<6;i++) {
00568 if (cbp & 32) {
00569 if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
00570 return -1;
00571 } else {
00572 s->block_last_index[i] = -1;
00573 }
00574 cbp+=cbp;
00575 }
00576 }
00577 }
00578 }else{
00579 for(i=0;i<12;i++)
00580 s->block_last_index[i] = -1;
00581 }
00582 }
00583
00584 s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ]= mb_type;
00585
00586 return 0;
00587 }
00588
00589
00590 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
00591 {
00592 int code, sign, val, l, shift;
00593
00594 code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
00595 if (code == 0) {
00596 return pred;
00597 }
00598 if (code < 0) {
00599 return 0xffff;
00600 }
00601
00602 sign = get_bits1(&s->gb);
00603 shift = fcode - 1;
00604 val = code;
00605 if (shift) {
00606 val = (val - 1) << shift;
00607 val |= get_bits(&s->gb, shift);
00608 val++;
00609 }
00610 if (sign)
00611 val = -val;
00612 val += pred;
00613
00614
00615 l= INT_BIT - 5 - shift;
00616 val = (val<<l)>>l;
00617 return val;
00618 }
00619
00620 static inline int mpeg1_decode_block_intra(MpegEncContext *s,
00621 DCTELEM *block,
00622 int n)
00623 {
00624 int level, dc, diff, i, j, run;
00625 int component;
00626 RLTable *rl = &ff_rl_mpeg1;
00627 uint8_t * const scantable= s->intra_scantable.permutated;
00628 const uint16_t *quant_matrix= s->intra_matrix;
00629 const int qscale= s->qscale;
00630
00631
00632 component = (n <= 3 ? 0 : n - 4 + 1);
00633 diff = decode_dc(&s->gb, component);
00634 if (diff >= 0xffff)
00635 return -1;
00636 dc = s->last_dc[component];
00637 dc += diff;
00638 s->last_dc[component] = dc;
00639 block[0] = dc*quant_matrix[0];
00640 av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
00641 i = 0;
00642 {
00643 OPEN_READER(re, &s->gb);
00644
00645 for(;;) {
00646 UPDATE_CACHE(re, &s->gb);
00647 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00648
00649 if(level == 127){
00650 break;
00651 } else if(level != 0) {
00652 i += run;
00653 j = scantable[i];
00654 level= (level*qscale*quant_matrix[j])>>4;
00655 level= (level-1)|1;
00656 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00657 LAST_SKIP_BITS(re, &s->gb, 1);
00658 } else {
00659
00660 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00661 UPDATE_CACHE(re, &s->gb);
00662 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
00663 if (level == -128) {
00664 level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
00665 } else if (level == 0) {
00666 level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
00667 }
00668 i += run;
00669 j = scantable[i];
00670 if(level<0){
00671 level= -level;
00672 level= (level*qscale*quant_matrix[j])>>4;
00673 level= (level-1)|1;
00674 level= -level;
00675 }else{
00676 level= (level*qscale*quant_matrix[j])>>4;
00677 level= (level-1)|1;
00678 }
00679 }
00680 if (i > 63){
00681 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00682 return -1;
00683 }
00684
00685 block[j] = level;
00686 }
00687 CLOSE_READER(re, &s->gb);
00688 }
00689 s->block_last_index[n] = i;
00690 return 0;
00691 }
00692
00693 int ff_mpeg1_decode_block_intra(MpegEncContext *s,
00694 DCTELEM *block,
00695 int n)
00696 {
00697 return mpeg1_decode_block_intra(s, block, n);
00698 }
00699
00700 static inline int mpeg1_decode_block_inter(MpegEncContext *s,
00701 DCTELEM *block,
00702 int n)
00703 {
00704 int level, i, j, run;
00705 RLTable *rl = &ff_rl_mpeg1;
00706 uint8_t * const scantable= s->intra_scantable.permutated;
00707 const uint16_t *quant_matrix= s->inter_matrix;
00708 const int qscale= s->qscale;
00709
00710 {
00711 OPEN_READER(re, &s->gb);
00712 i = -1;
00713
00714 UPDATE_CACHE(re, &s->gb);
00715 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00716 level= (3*qscale*quant_matrix[0])>>5;
00717 level= (level-1)|1;
00718 if(GET_CACHE(re, &s->gb)&0x40000000)
00719 level= -level;
00720 block[0] = level;
00721 i++;
00722 SKIP_BITS(re, &s->gb, 2);
00723 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00724 goto end;
00725 }
00726
00727 for(;;) {
00728 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00729
00730 if(level != 0) {
00731 i += run;
00732 j = scantable[i];
00733 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
00734 level= (level-1)|1;
00735 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00736 SKIP_BITS(re, &s->gb, 1);
00737 } else {
00738
00739 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00740 UPDATE_CACHE(re, &s->gb);
00741 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
00742 if (level == -128) {
00743 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
00744 } else if (level == 0) {
00745 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
00746 }
00747 i += run;
00748 j = scantable[i];
00749 if(level<0){
00750 level= -level;
00751 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
00752 level= (level-1)|1;
00753 level= -level;
00754 }else{
00755 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
00756 level= (level-1)|1;
00757 }
00758 }
00759 if (i > 63){
00760 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00761 return -1;
00762 }
00763
00764 block[j] = level;
00765 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00766 break;
00767 UPDATE_CACHE(re, &s->gb);
00768 }
00769 end:
00770 LAST_SKIP_BITS(re, &s->gb, 2);
00771 CLOSE_READER(re, &s->gb);
00772 }
00773 s->block_last_index[n] = i;
00774 return 0;
00775 }
00776
00777 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
00778 {
00779 int level, i, j, run;
00780 RLTable *rl = &ff_rl_mpeg1;
00781 uint8_t * const scantable= s->intra_scantable.permutated;
00782 const int qscale= s->qscale;
00783
00784 {
00785 OPEN_READER(re, &s->gb);
00786 i = -1;
00787
00788 UPDATE_CACHE(re, &s->gb);
00789 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00790 level= (3*qscale)>>1;
00791 level= (level-1)|1;
00792 if(GET_CACHE(re, &s->gb)&0x40000000)
00793 level= -level;
00794 block[0] = level;
00795 i++;
00796 SKIP_BITS(re, &s->gb, 2);
00797 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00798 goto end;
00799 }
00800
00801
00802 for(;;) {
00803 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00804
00805 if(level != 0) {
00806 i += run;
00807 j = scantable[i];
00808 level= ((level*2+1)*qscale)>>1;
00809 level= (level-1)|1;
00810 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00811 SKIP_BITS(re, &s->gb, 1);
00812 } else {
00813
00814 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00815 UPDATE_CACHE(re, &s->gb);
00816 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
00817 if (level == -128) {
00818 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
00819 } else if (level == 0) {
00820 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
00821 }
00822 i += run;
00823 j = scantable[i];
00824 if(level<0){
00825 level= -level;
00826 level= ((level*2+1)*qscale)>>1;
00827 level= (level-1)|1;
00828 level= -level;
00829 }else{
00830 level= ((level*2+1)*qscale)>>1;
00831 level= (level-1)|1;
00832 }
00833 }
00834
00835 block[j] = level;
00836 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00837 break;
00838 UPDATE_CACHE(re, &s->gb);
00839 }
00840 end:
00841 LAST_SKIP_BITS(re, &s->gb, 2);
00842 CLOSE_READER(re, &s->gb);
00843 }
00844 s->block_last_index[n] = i;
00845 return 0;
00846 }
00847
00848
00849 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
00850 DCTELEM *block,
00851 int n)
00852 {
00853 int level, i, j, run;
00854 RLTable *rl = &ff_rl_mpeg1;
00855 uint8_t * const scantable= s->intra_scantable.permutated;
00856 const uint16_t *quant_matrix;
00857 const int qscale= s->qscale;
00858 int mismatch;
00859
00860 mismatch = 1;
00861
00862 {
00863 OPEN_READER(re, &s->gb);
00864 i = -1;
00865 if (n < 4)
00866 quant_matrix = s->inter_matrix;
00867 else
00868 quant_matrix = s->chroma_inter_matrix;
00869
00870
00871 UPDATE_CACHE(re, &s->gb);
00872 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00873 level= (3*qscale*quant_matrix[0])>>5;
00874 if(GET_CACHE(re, &s->gb)&0x40000000)
00875 level= -level;
00876 block[0] = level;
00877 mismatch ^= level;
00878 i++;
00879 SKIP_BITS(re, &s->gb, 2);
00880 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00881 goto end;
00882 }
00883
00884
00885 for(;;) {
00886 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00887
00888 if(level != 0) {
00889 i += run;
00890 j = scantable[i];
00891 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
00892 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00893 SKIP_BITS(re, &s->gb, 1);
00894 } else {
00895
00896 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00897 UPDATE_CACHE(re, &s->gb);
00898 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
00899
00900 i += run;
00901 j = scantable[i];
00902 if(level<0){
00903 level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
00904 level= -level;
00905 }else{
00906 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
00907 }
00908 }
00909 if (i > 63){
00910 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00911 return -1;
00912 }
00913
00914 mismatch ^= level;
00915 block[j] = level;
00916 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00917 break;
00918 UPDATE_CACHE(re, &s->gb);
00919 }
00920 end:
00921 LAST_SKIP_BITS(re, &s->gb, 2);
00922 CLOSE_READER(re, &s->gb);
00923 }
00924 block[63] ^= (mismatch & 1);
00925
00926 s->block_last_index[n] = i;
00927 return 0;
00928 }
00929
00930 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
00931 DCTELEM *block,
00932 int n)
00933 {
00934 int level, i, j, run;
00935 RLTable *rl = &ff_rl_mpeg1;
00936 uint8_t * const scantable= s->intra_scantable.permutated;
00937 const int qscale= s->qscale;
00938 OPEN_READER(re, &s->gb);
00939 i = -1;
00940
00941
00942 UPDATE_CACHE(re, &s->gb);
00943 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00944 level= (3*qscale)>>1;
00945 if(GET_CACHE(re, &s->gb)&0x40000000)
00946 level= -level;
00947 block[0] = level;
00948 i++;
00949 SKIP_BITS(re, &s->gb, 2);
00950 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00951 goto end;
00952 }
00953
00954
00955 for(;;) {
00956 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00957
00958 if(level != 0) {
00959 i += run;
00960 j = scantable[i];
00961 level= ((level*2+1)*qscale)>>1;
00962 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00963 SKIP_BITS(re, &s->gb, 1);
00964 } else {
00965
00966 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00967 UPDATE_CACHE(re, &s->gb);
00968 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
00969
00970 i += run;
00971 j = scantable[i];
00972 if(level<0){
00973 level= ((-level*2+1)*qscale)>>1;
00974 level= -level;
00975 }else{
00976 level= ((level*2+1)*qscale)>>1;
00977 }
00978 }
00979
00980 block[j] = level;
00981 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00982 break;
00983 UPDATE_CACHE(re, &s->gb);
00984 }
00985 end:
00986 LAST_SKIP_BITS(re, &s->gb, 2);
00987 CLOSE_READER(re, &s->gb);
00988 s->block_last_index[n] = i;
00989 return 0;
00990 }
00991
00992
00993 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
00994 DCTELEM *block,
00995 int n)
00996 {
00997 int level, dc, diff, i, j, run;
00998 int component;
00999 RLTable *rl;
01000 uint8_t * const scantable= s->intra_scantable.permutated;
01001 const uint16_t *quant_matrix;
01002 const int qscale= s->qscale;
01003 int mismatch;
01004
01005
01006 if (n < 4){
01007 quant_matrix = s->intra_matrix;
01008 component = 0;
01009 }else{
01010 quant_matrix = s->chroma_intra_matrix;
01011 component = (n&1) + 1;
01012 }
01013 diff = decode_dc(&s->gb, component);
01014 if (diff >= 0xffff)
01015 return -1;
01016 dc = s->last_dc[component];
01017 dc += diff;
01018 s->last_dc[component] = dc;
01019 block[0] = dc << (3 - s->intra_dc_precision);
01020 av_dlog(s->avctx, "dc=%d\n", block[0]);
01021 mismatch = block[0] ^ 1;
01022 i = 0;
01023 if (s->intra_vlc_format)
01024 rl = &ff_rl_mpeg2;
01025 else
01026 rl = &ff_rl_mpeg1;
01027
01028 {
01029 OPEN_READER(re, &s->gb);
01030
01031 for(;;) {
01032 UPDATE_CACHE(re, &s->gb);
01033 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
01034
01035 if(level == 127){
01036 break;
01037 } else if(level != 0) {
01038 i += run;
01039 j = scantable[i];
01040 level= (level*qscale*quant_matrix[j])>>4;
01041 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
01042 LAST_SKIP_BITS(re, &s->gb, 1);
01043 } else {
01044
01045 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
01046 UPDATE_CACHE(re, &s->gb);
01047 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
01048 i += run;
01049 j = scantable[i];
01050 if(level<0){
01051 level= (-level*qscale*quant_matrix[j])>>4;
01052 level= -level;
01053 }else{
01054 level= (level*qscale*quant_matrix[j])>>4;
01055 }
01056 }
01057 if (i > 63){
01058 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
01059 return -1;
01060 }
01061
01062 mismatch^= level;
01063 block[j] = level;
01064 }
01065 CLOSE_READER(re, &s->gb);
01066 }
01067 block[63]^= mismatch&1;
01068
01069 s->block_last_index[n] = i;
01070 return 0;
01071 }
01072
01073 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
01074 DCTELEM *block,
01075 int n)
01076 {
01077 int level, dc, diff, j, run;
01078 int component;
01079 RLTable *rl;
01080 uint8_t * scantable= s->intra_scantable.permutated;
01081 const uint16_t *quant_matrix;
01082 const int qscale= s->qscale;
01083
01084
01085 if (n < 4){
01086 quant_matrix = s->intra_matrix;
01087 component = 0;
01088 }else{
01089 quant_matrix = s->chroma_intra_matrix;
01090 component = (n&1) + 1;
01091 }
01092 diff = decode_dc(&s->gb, component);
01093 if (diff >= 0xffff)
01094 return -1;
01095 dc = s->last_dc[component];
01096 dc += diff;
01097 s->last_dc[component] = dc;
01098 block[0] = dc << (3 - s->intra_dc_precision);
01099 if (s->intra_vlc_format)
01100 rl = &ff_rl_mpeg2;
01101 else
01102 rl = &ff_rl_mpeg1;
01103
01104 {
01105 OPEN_READER(re, &s->gb);
01106
01107 for(;;) {
01108 UPDATE_CACHE(re, &s->gb);
01109 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
01110
01111 if(level == 127){
01112 break;
01113 } else if(level != 0) {
01114 scantable += run;
01115 j = *scantable;
01116 level= (level*qscale*quant_matrix[j])>>4;
01117 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
01118 LAST_SKIP_BITS(re, &s->gb, 1);
01119 } else {
01120
01121 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
01122 UPDATE_CACHE(re, &s->gb);
01123 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
01124 scantable += run;
01125 j = *scantable;
01126 if(level<0){
01127 level= (-level*qscale*quant_matrix[j])>>4;
01128 level= -level;
01129 }else{
01130 level= (level*qscale*quant_matrix[j])>>4;
01131 }
01132 }
01133
01134 block[j] = level;
01135 }
01136 CLOSE_READER(re, &s->gb);
01137 }
01138
01139 s->block_last_index[n] = scantable - s->intra_scantable.permutated;
01140 return 0;
01141 }
01142
01143 typedef struct Mpeg1Context {
01144 MpegEncContext mpeg_enc_ctx;
01145 int mpeg_enc_ctx_allocated;
01146 int repeat_field;
01147 AVPanScan pan_scan;
01148 int slice_count;
01149 int swap_uv;
01150 int save_aspect_info;
01151 int save_width, save_height, save_progressive_seq;
01152 AVRational frame_rate_ext;
01153 int sync;
01154 } Mpeg1Context;
01155
01156 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
01157 {
01158 Mpeg1Context *s = avctx->priv_data;
01159 MpegEncContext *s2 = &s->mpeg_enc_ctx;
01160 int i;
01161
01162
01163
01164 for(i=0;i<64;i++)
01165 s2->dsp.idct_permutation[i]=i;
01166
01167 MPV_decode_defaults(s2);
01168
01169 s->mpeg_enc_ctx.avctx= avctx;
01170 s->mpeg_enc_ctx.flags= avctx->flags;
01171 s->mpeg_enc_ctx.flags2= avctx->flags2;
01172 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
01173 ff_mpeg12_init_vlcs();
01174
01175 s->mpeg_enc_ctx_allocated = 0;
01176 s->mpeg_enc_ctx.picture_number = 0;
01177 s->repeat_field = 0;
01178 s->mpeg_enc_ctx.codec_id= avctx->codec->id;
01179 avctx->color_range= AVCOL_RANGE_MPEG;
01180 if (avctx->codec->id == CODEC_ID_MPEG1VIDEO)
01181 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
01182 else
01183 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
01184 return 0;
01185 }
01186
01187 static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
01188 {
01189 Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
01190 MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
01191 int err;
01192
01193 if(avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized)
01194 return 0;
01195
01196 err = ff_mpeg_update_thread_context(avctx, avctx_from);
01197 if(err) return err;
01198
01199 if(!ctx->mpeg_enc_ctx_allocated)
01200 memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
01201
01202 if(!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
01203 s->picture_number++;
01204
01205 return 0;
01206 }
01207
01208 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
01209 const uint8_t *new_perm){
01210 uint16_t temp_matrix[64];
01211 int i;
01212
01213 memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
01214
01215 for(i=0;i<64;i++){
01216 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
01217 }
01218 }
01219
01220 static const enum PixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
01221 #if CONFIG_MPEG_XVMC_DECODER
01222 PIX_FMT_XVMC_MPEG2_IDCT,
01223 PIX_FMT_XVMC_MPEG2_MC,
01224 #endif
01225 #if CONFIG_MPEG1_VDPAU_HWACCEL
01226 PIX_FMT_VDPAU_MPEG1,
01227 #endif
01228 PIX_FMT_YUV420P,
01229 PIX_FMT_NONE
01230 };
01231
01232 static const enum PixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
01233 #if CONFIG_MPEG_XVMC_DECODER
01234 PIX_FMT_XVMC_MPEG2_IDCT,
01235 PIX_FMT_XVMC_MPEG2_MC,
01236 #endif
01237 #if CONFIG_MPEG2_VDPAU_HWACCEL
01238 PIX_FMT_VDPAU_MPEG2,
01239 #endif
01240 #if CONFIG_MPEG2_DXVA2_HWACCEL
01241 PIX_FMT_DXVA2_VLD,
01242 #endif
01243 #if CONFIG_MPEG2_VAAPI_HWACCEL
01244 PIX_FMT_VAAPI_VLD,
01245 #endif
01246 PIX_FMT_YUV420P,
01247 PIX_FMT_NONE
01248 };
01249
01250 static inline int uses_vdpau(AVCodecContext *avctx) {
01251 return avctx->pix_fmt == PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == PIX_FMT_VDPAU_MPEG2;
01252 }
01253
01254 static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx){
01255 Mpeg1Context *s1 = avctx->priv_data;
01256 MpegEncContext *s = &s1->mpeg_enc_ctx;
01257
01258 if(s->chroma_format < 2) {
01259 enum PixelFormat res;
01260 res = avctx->get_format(avctx,
01261 avctx->codec_id == CODEC_ID_MPEG1VIDEO ?
01262 mpeg1_hwaccel_pixfmt_list_420 :
01263 mpeg2_hwaccel_pixfmt_list_420);
01264 if (res != PIX_FMT_XVMC_MPEG2_IDCT && res != PIX_FMT_XVMC_MPEG2_MC) {
01265 avctx->xvmc_acceleration = 0;
01266 } else if (!avctx->xvmc_acceleration) {
01267 avctx->xvmc_acceleration = 2;
01268 }
01269 return res;
01270 } else if(s->chroma_format == 2)
01271 return PIX_FMT_YUV422P;
01272 else
01273 return PIX_FMT_YUV444P;
01274 }
01275
01276
01277
01278 static int mpeg_decode_postinit(AVCodecContext *avctx){
01279 Mpeg1Context *s1 = avctx->priv_data;
01280 MpegEncContext *s = &s1->mpeg_enc_ctx;
01281 uint8_t old_permutation[64];
01282
01283 if (
01284 (s1->mpeg_enc_ctx_allocated == 0)||
01285 avctx->coded_width != s->width ||
01286 avctx->coded_height != s->height||
01287 s1->save_width != s->width ||
01288 s1->save_height != s->height ||
01289 s1->save_aspect_info != s->aspect_ratio_info||
01290 s1->save_progressive_seq != s->progressive_sequence ||
01291 0)
01292 {
01293
01294 if (s1->mpeg_enc_ctx_allocated) {
01295 ParseContext pc= s->parse_context;
01296 s->parse_context.buffer=0;
01297 MPV_common_end(s);
01298 s->parse_context= pc;
01299 }
01300
01301 if( (s->width == 0 )||(s->height == 0))
01302 return -2;
01303
01304 avcodec_set_dimensions(avctx, s->width, s->height);
01305 avctx->bit_rate = s->bit_rate;
01306 s1->save_aspect_info = s->aspect_ratio_info;
01307 s1->save_width = s->width;
01308 s1->save_height = s->height;
01309 s1->save_progressive_seq = s->progressive_sequence;
01310
01311
01312
01313 avctx->has_b_frames = !(s->low_delay);
01314
01315 assert((avctx->sub_id==1) == (avctx->codec_id==CODEC_ID_MPEG1VIDEO));
01316 if(avctx->codec_id==CODEC_ID_MPEG1VIDEO){
01317
01318 avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
01319 avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
01320
01321 avctx->sample_aspect_ratio= av_d2q(
01322 1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
01323 avctx->ticks_per_frame=1;
01324 }else{
01325
01326 av_reduce(
01327 &s->avctx->time_base.den,
01328 &s->avctx->time_base.num,
01329 ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
01330 ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
01331 1<<30);
01332 avctx->ticks_per_frame=2;
01333
01334 if(s->aspect_ratio_info > 1){
01335 AVRational dar =
01336 av_mul_q(
01337 av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
01338 (AVRational){s1->pan_scan.width, s1->pan_scan.height}),
01339 (AVRational){s->width, s->height});
01340
01341
01342
01343
01344 if((s1->pan_scan.width == 0 ) || (s1->pan_scan.height == 0) ||
01345 (av_cmp_q(dar,(AVRational){4,3}) && av_cmp_q(dar,(AVRational){16,9}))) {
01346 s->avctx->sample_aspect_ratio=
01347 av_div_q(
01348 ff_mpeg2_aspect[s->aspect_ratio_info],
01349 (AVRational){s->width, s->height}
01350 );
01351 }else{
01352 s->avctx->sample_aspect_ratio=
01353 av_div_q(
01354 ff_mpeg2_aspect[s->aspect_ratio_info],
01355 (AVRational){s1->pan_scan.width, s1->pan_scan.height}
01356 );
01357
01358
01359
01360
01361
01362
01363 }
01364 }else{
01365 s->avctx->sample_aspect_ratio=
01366 ff_mpeg2_aspect[s->aspect_ratio_info];
01367 }
01368 }
01369
01370 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
01371 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
01372
01373 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
01374 avctx->hwaccel )
01375 if( avctx->idct_algo == FF_IDCT_AUTO )
01376 avctx->idct_algo = FF_IDCT_SIMPLE;
01377
01378
01379
01380 memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
01381
01382 if (MPV_common_init(s) < 0)
01383 return -2;
01384
01385 quant_matrix_rebuild(s->intra_matrix, old_permutation,s->dsp.idct_permutation);
01386 quant_matrix_rebuild(s->inter_matrix, old_permutation,s->dsp.idct_permutation);
01387 quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
01388 quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
01389
01390 s1->mpeg_enc_ctx_allocated = 1;
01391 }
01392 return 0;
01393 }
01394
01395 static int mpeg1_decode_picture(AVCodecContext *avctx,
01396 const uint8_t *buf, int buf_size)
01397 {
01398 Mpeg1Context *s1 = avctx->priv_data;
01399 MpegEncContext *s = &s1->mpeg_enc_ctx;
01400 int ref, f_code, vbv_delay;
01401
01402 init_get_bits(&s->gb, buf, buf_size*8);
01403
01404 ref = get_bits(&s->gb, 10);
01405 s->pict_type = get_bits(&s->gb, 3);
01406 if(s->pict_type == 0 || s->pict_type > 3)
01407 return -1;
01408
01409 vbv_delay= get_bits(&s->gb, 16);
01410 if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
01411 s->full_pel[0] = get_bits1(&s->gb);
01412 f_code = get_bits(&s->gb, 3);
01413 if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
01414 return -1;
01415 s->mpeg_f_code[0][0] = f_code;
01416 s->mpeg_f_code[0][1] = f_code;
01417 }
01418 if (s->pict_type == AV_PICTURE_TYPE_B) {
01419 s->full_pel[1] = get_bits1(&s->gb);
01420 f_code = get_bits(&s->gb, 3);
01421 if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
01422 return -1;
01423 s->mpeg_f_code[1][0] = f_code;
01424 s->mpeg_f_code[1][1] = f_code;
01425 }
01426 s->current_picture.pict_type= s->pict_type;
01427 s->current_picture.key_frame= s->pict_type == AV_PICTURE_TYPE_I;
01428
01429 if(avctx->debug & FF_DEBUG_PICT_INFO)
01430 av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
01431
01432 s->y_dc_scale = 8;
01433 s->c_dc_scale = 8;
01434 return 0;
01435 }
01436
01437 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
01438 {
01439 MpegEncContext *s= &s1->mpeg_enc_ctx;
01440 int horiz_size_ext, vert_size_ext;
01441 int bit_rate_ext;
01442
01443 skip_bits(&s->gb, 1);
01444 s->avctx->profile= get_bits(&s->gb, 3);
01445 s->avctx->level= get_bits(&s->gb, 4);
01446 s->progressive_sequence = get_bits1(&s->gb);
01447 s->chroma_format = get_bits(&s->gb, 2);
01448 horiz_size_ext = get_bits(&s->gb, 2);
01449 vert_size_ext = get_bits(&s->gb, 2);
01450 s->width |= (horiz_size_ext << 12);
01451 s->height |= (vert_size_ext << 12);
01452 bit_rate_ext = get_bits(&s->gb, 12);
01453 s->bit_rate += (bit_rate_ext << 18) * 400;
01454 skip_bits1(&s->gb);
01455 s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
01456
01457 s->low_delay = get_bits1(&s->gb);
01458 if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
01459
01460 s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
01461 s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
01462
01463 av_dlog(s->avctx, "sequence extension\n");
01464 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
01465 s->avctx->sub_id = 2;
01466
01467 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
01468 av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
01469 s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
01470
01471 }
01472
01473 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
01474 {
01475 MpegEncContext *s= &s1->mpeg_enc_ctx;
01476 int color_description, w, h;
01477
01478 skip_bits(&s->gb, 3);
01479 color_description= get_bits1(&s->gb);
01480 if(color_description){
01481 s->avctx->color_primaries= get_bits(&s->gb, 8);
01482 s->avctx->color_trc = get_bits(&s->gb, 8);
01483 s->avctx->colorspace = get_bits(&s->gb, 8);
01484 }
01485 w= get_bits(&s->gb, 14);
01486 skip_bits(&s->gb, 1);
01487 h= get_bits(&s->gb, 14);
01488
01489
01490 s1->pan_scan.width= 16*w;
01491 s1->pan_scan.height=16*h;
01492
01493 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
01494 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
01495 }
01496
01497 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
01498 {
01499 MpegEncContext *s= &s1->mpeg_enc_ctx;
01500 int i,nofco;
01501
01502 nofco = 1;
01503 if(s->progressive_sequence){
01504 if(s->repeat_first_field){
01505 nofco++;
01506 if(s->top_field_first)
01507 nofco++;
01508 }
01509 }else{
01510 if(s->picture_structure == PICT_FRAME){
01511 nofco++;
01512 if(s->repeat_first_field)
01513 nofco++;
01514 }
01515 }
01516 for(i=0; i<nofco; i++){
01517 s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
01518 skip_bits(&s->gb, 1);
01519 s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
01520 skip_bits(&s->gb, 1);
01521 }
01522
01523 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
01524 av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
01525 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
01526 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
01527 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
01528 );
01529 }
01530
01531 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra){
01532 int i;
01533
01534 for(i=0; i<64; i++) {
01535 int j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
01536 int v = get_bits(&s->gb, 8);
01537 if(v==0){
01538 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
01539 return -1;
01540 }
01541 if(intra && i==0 && v!=8){
01542 av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
01543 v= 8;
01544 }
01545 matrix0[j] = v;
01546 if(matrix1)
01547 matrix1[j] = v;
01548 }
01549 return 0;
01550 }
01551
01552 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
01553 {
01554 av_dlog(s->avctx, "matrix extension\n");
01555
01556 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
01557 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
01558 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
01559 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
01560 }
01561
01562 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
01563 {
01564 MpegEncContext *s= &s1->mpeg_enc_ctx;
01565
01566 s->full_pel[0] = s->full_pel[1] = 0;
01567 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
01568 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
01569 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
01570 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
01571 if(!s->pict_type && s1->mpeg_enc_ctx_allocated){
01572 av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
01573 if(s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1]==15){
01574 if(s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
01575 s->pict_type= AV_PICTURE_TYPE_I;
01576 else
01577 s->pict_type= AV_PICTURE_TYPE_P;
01578 }else
01579 s->pict_type= AV_PICTURE_TYPE_B;
01580 s->current_picture.pict_type= s->pict_type;
01581 s->current_picture.key_frame= s->pict_type == AV_PICTURE_TYPE_I;
01582 }
01583 s->intra_dc_precision = get_bits(&s->gb, 2);
01584 s->picture_structure = get_bits(&s->gb, 2);
01585 s->top_field_first = get_bits1(&s->gb);
01586 s->frame_pred_frame_dct = get_bits1(&s->gb);
01587 s->concealment_motion_vectors = get_bits1(&s->gb);
01588 s->q_scale_type = get_bits1(&s->gb);
01589 s->intra_vlc_format = get_bits1(&s->gb);
01590 s->alternate_scan = get_bits1(&s->gb);
01591 s->repeat_first_field = get_bits1(&s->gb);
01592 s->chroma_420_type = get_bits1(&s->gb);
01593 s->progressive_frame = get_bits1(&s->gb);
01594
01595 if(s->progressive_sequence && !s->progressive_frame){
01596 s->progressive_frame= 1;
01597 av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
01598 }
01599
01600 if(s->picture_structure==0 || (s->progressive_frame && s->picture_structure!=PICT_FRAME)){
01601 av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
01602 s->picture_structure= PICT_FRAME;
01603 }
01604
01605 if(s->progressive_sequence && !s->frame_pred_frame_dct){
01606 av_log(s->avctx, AV_LOG_ERROR, "invalid frame_pred_frame_dct\n");
01607 }
01608
01609 if(s->picture_structure == PICT_FRAME){
01610 s->first_field=0;
01611 s->v_edge_pos= 16*s->mb_height;
01612 }else{
01613 s->first_field ^= 1;
01614 s->v_edge_pos= 8*s->mb_height;
01615 memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
01616 }
01617
01618 if(s->alternate_scan){
01619 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
01620 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
01621 }else{
01622 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
01623 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
01624 }
01625
01626
01627 av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
01628 av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
01629 av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
01630 av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
01631 av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
01632 av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
01633 av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
01634 av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
01635 av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
01636 }
01637
01638 static void exchange_uv(MpegEncContext *s){
01639 DCTELEM (*tmp)[64];
01640
01641 tmp = s->pblocks[4];
01642 s->pblocks[4] = s->pblocks[5];
01643 s->pblocks[5] = tmp;
01644 }
01645
01646 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size){
01647 AVCodecContext *avctx= s->avctx;
01648 Mpeg1Context *s1 = (Mpeg1Context*)s;
01649
01650
01651 if(s->first_field || s->picture_structure==PICT_FRAME){
01652 if(MPV_frame_start(s, avctx) < 0)
01653 return -1;
01654
01655 ff_er_frame_start(s);
01656
01657
01658 s->current_picture_ptr->repeat_pict = 0;
01659 if (s->repeat_first_field) {
01660 if (s->progressive_sequence) {
01661 if (s->top_field_first)
01662 s->current_picture_ptr->repeat_pict = 4;
01663 else
01664 s->current_picture_ptr->repeat_pict = 2;
01665 } else if (s->progressive_frame) {
01666 s->current_picture_ptr->repeat_pict = 1;
01667 }
01668 }
01669
01670 *s->current_picture_ptr->pan_scan= s1->pan_scan;
01671
01672 if (HAVE_PTHREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
01673 ff_thread_finish_setup(avctx);
01674 }else{
01675 int i;
01676
01677 if(!s->current_picture_ptr){
01678 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
01679 return -1;
01680 }
01681
01682 for(i=0; i<4; i++){
01683 s->current_picture.data[i] = s->current_picture_ptr->data[i];
01684 if(s->picture_structure == PICT_BOTTOM_FIELD){
01685 s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
01686 }
01687 }
01688 }
01689
01690 if (avctx->hwaccel) {
01691 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
01692 return -1;
01693 }
01694
01695
01696
01697 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
01698 if(ff_xvmc_field_start(s,avctx) < 0)
01699 return -1;
01700
01701 return 0;
01702 }
01703
01704 #define DECODE_SLICE_ERROR -1
01705 #define DECODE_SLICE_OK 0
01706
01712 static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
01713 const uint8_t **buf, int buf_size)
01714 {
01715 MpegEncContext *s = &s1->mpeg_enc_ctx;
01716 AVCodecContext *avctx= s->avctx;
01717 const int field_pic= s->picture_structure != PICT_FRAME;
01718 const int lowres= s->avctx->lowres;
01719
01720 s->resync_mb_x=
01721 s->resync_mb_y= -1;
01722
01723 assert(mb_y < s->mb_height);
01724
01725 init_get_bits(&s->gb, *buf, buf_size*8);
01726
01727 ff_mpeg1_clean_buffers(s);
01728 s->interlaced_dct = 0;
01729
01730 s->qscale = get_qscale(s);
01731
01732 if(s->qscale == 0){
01733 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
01734 return -1;
01735 }
01736
01737
01738 while (get_bits1(&s->gb) != 0) {
01739 skip_bits(&s->gb, 8);
01740 }
01741
01742 s->mb_x=0;
01743
01744 if(mb_y==0 && s->codec_tag == AV_RL32("SLIF")){
01745 skip_bits1(&s->gb);
01746 }else{
01747 for(;;) {
01748 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
01749 if (code < 0){
01750 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
01751 return -1;
01752 }
01753 if (code >= 33) {
01754 if (code == 33) {
01755 s->mb_x += 33;
01756 }
01757
01758 } else {
01759 s->mb_x += code;
01760 break;
01761 }
01762 }
01763 }
01764
01765 if(s->mb_x >= (unsigned)s->mb_width){
01766 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
01767 return -1;
01768 }
01769
01770 if (avctx->hwaccel) {
01771 const uint8_t *buf_end, *buf_start = *buf - 4;
01772 int start_code = -1;
01773 buf_end = ff_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
01774 if (buf_end < *buf + buf_size)
01775 buf_end -= 4;
01776 s->mb_y = mb_y;
01777 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
01778 return DECODE_SLICE_ERROR;
01779 *buf = buf_end;
01780 return DECODE_SLICE_OK;
01781 }
01782
01783 s->resync_mb_x= s->mb_x;
01784 s->resync_mb_y= s->mb_y= mb_y;
01785 s->mb_skip_run= 0;
01786 ff_init_block_index(s);
01787
01788 if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
01789 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
01790 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",
01791 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],
01792 s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
01793 s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
01794 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
01795 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
01796 }
01797 }
01798
01799 for(;;) {
01800
01801 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
01802 ff_xvmc_init_block(s);
01803
01804 if(mpeg_decode_mb(s, s->block) < 0)
01805 return -1;
01806
01807 if(s->current_picture.motion_val[0] && !s->encoding){
01808 const int wrap = s->b8_stride;
01809 int xy = s->mb_x*2 + s->mb_y*2*wrap;
01810 int b8_xy= 4*(s->mb_x + s->mb_y*s->mb_stride);
01811 int motion_x, motion_y, dir, i;
01812
01813 for(i=0; i<2; i++){
01814 for(dir=0; dir<2; dir++){
01815 if (s->mb_intra || (dir==1 && s->pict_type != AV_PICTURE_TYPE_B)) {
01816 motion_x = motion_y = 0;
01817 }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
01818 motion_x = s->mv[dir][0][0];
01819 motion_y = s->mv[dir][0][1];
01820 } else {
01821 motion_x = s->mv[dir][i][0];
01822 motion_y = s->mv[dir][i][1];
01823 }
01824
01825 s->current_picture.motion_val[dir][xy ][0] = motion_x;
01826 s->current_picture.motion_val[dir][xy ][1] = motion_y;
01827 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
01828 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
01829 s->current_picture.ref_index [dir][b8_xy ]=
01830 s->current_picture.ref_index [dir][b8_xy + 1]= s->field_select[dir][i];
01831 assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
01832 }
01833 xy += wrap;
01834 b8_xy +=2;
01835 }
01836 }
01837
01838 s->dest[0] += 16 >> lowres;
01839 s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
01840 s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
01841
01842 MPV_decode_mb(s, s->block);
01843
01844 if (++s->mb_x >= s->mb_width) {
01845 const int mb_size= 16>>s->avctx->lowres;
01846
01847 ff_draw_horiz_band(s, mb_size*(s->mb_y>>field_pic), mb_size);
01848 MPV_report_decode_progress(s);
01849
01850 s->mb_x = 0;
01851 s->mb_y += 1<<field_pic;
01852
01853 if(s->mb_y >= s->mb_height){
01854 int left= get_bits_left(&s->gb);
01855 int is_d10= s->chroma_format==2 && s->pict_type==AV_PICTURE_TYPE_I && avctx->profile==0 && avctx->level==5
01856 && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
01857 && s->progressive_frame == 0 ;
01858
01859 if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
01860 || (avctx->error_recognition >= FF_ER_AGGRESSIVE && left>8)){
01861 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
01862 return -1;
01863 }else
01864 goto eos;
01865 }
01866
01867 ff_init_block_index(s);
01868 }
01869
01870
01871 if (s->mb_skip_run == -1) {
01872
01873 s->mb_skip_run = 0;
01874 for(;;) {
01875 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
01876 if (code < 0){
01877 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
01878 return -1;
01879 }
01880 if (code >= 33) {
01881 if (code == 33) {
01882 s->mb_skip_run += 33;
01883 }else if(code == 35){
01884 if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
01885 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
01886 return -1;
01887 }
01888 goto eos;
01889 }
01890
01891 } else {
01892 s->mb_skip_run += code;
01893 break;
01894 }
01895 }
01896 if(s->mb_skip_run){
01897 int i;
01898 if(s->pict_type == AV_PICTURE_TYPE_I){
01899 av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
01900 return -1;
01901 }
01902
01903
01904 s->mb_intra = 0;
01905 for(i=0;i<12;i++)
01906 s->block_last_index[i] = -1;
01907 if(s->picture_structure == PICT_FRAME)
01908 s->mv_type = MV_TYPE_16X16;
01909 else
01910 s->mv_type = MV_TYPE_FIELD;
01911 if (s->pict_type == AV_PICTURE_TYPE_P) {
01912
01913 s->mv_dir = MV_DIR_FORWARD;
01914 s->mv[0][0][0] = s->mv[0][0][1] = 0;
01915 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
01916 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
01917 s->field_select[0][0]= (s->picture_structure - 1) & 1;
01918 } else {
01919
01920 s->mv[0][0][0] = s->last_mv[0][0][0];
01921 s->mv[0][0][1] = s->last_mv[0][0][1];
01922 s->mv[1][0][0] = s->last_mv[1][0][0];
01923 s->mv[1][0][1] = s->last_mv[1][0][1];
01924 }
01925 }
01926 }
01927 }
01928 eos:
01929 *buf += (get_bits_count(&s->gb)-1)/8;
01930
01931 return 0;
01932 }
01933
01934 static int slice_decode_thread(AVCodecContext *c, void *arg){
01935 MpegEncContext *s= *(void**)arg;
01936 const uint8_t *buf= s->gb.buffer;
01937 int mb_y= s->start_mb_y;
01938 const int field_pic= s->picture_structure != PICT_FRAME;
01939
01940 s->error_count= (3*(s->end_mb_y - s->start_mb_y)*s->mb_width) >> field_pic;
01941
01942 for(;;){
01943 uint32_t start_code;
01944 int ret;
01945
01946 ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
01947 emms_c();
01948
01949
01950 if(ret < 0){
01951 if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
01952 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);
01953 }else{
01954 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);
01955 }
01956
01957 if(s->mb_y == s->end_mb_y)
01958 return 0;
01959
01960 start_code= -1;
01961 buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
01962 mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
01963 if (s->picture_structure == PICT_BOTTOM_FIELD)
01964 mb_y++;
01965 if(mb_y < 0 || mb_y >= s->end_mb_y)
01966 return -1;
01967 }
01968
01969 return 0;
01970 }
01971
01976 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
01977 {
01978 Mpeg1Context *s1 = avctx->priv_data;
01979 MpegEncContext *s = &s1->mpeg_enc_ctx;
01980
01981 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
01982 return 0;
01983
01984 if (s->avctx->hwaccel) {
01985 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
01986 av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
01987 }
01988
01989 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
01990 ff_xvmc_field_end(s);
01991
01992
01993 if ( !s->first_field) {
01994
01995
01996 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
01997
01998 ff_er_frame_end(s);
01999
02000 MPV_frame_end(s);
02001
02002 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
02003 *pict= *(AVFrame*)s->current_picture_ptr;
02004 ff_print_debug_info(s, pict);
02005 } else {
02006 if (avctx->active_thread_type & FF_THREAD_FRAME)
02007 s->picture_number++;
02008
02009
02010 if (s->last_picture_ptr != NULL) {
02011 *pict= *(AVFrame*)s->last_picture_ptr;
02012 ff_print_debug_info(s, pict);
02013 }
02014 }
02015
02016 return 1;
02017 } else {
02018 return 0;
02019 }
02020 }
02021
02022 static int mpeg1_decode_sequence(AVCodecContext *avctx,
02023 const uint8_t *buf, int buf_size)
02024 {
02025 Mpeg1Context *s1 = avctx->priv_data;
02026 MpegEncContext *s = &s1->mpeg_enc_ctx;
02027 int width,height;
02028 int i, v, j;
02029
02030 init_get_bits(&s->gb, buf, buf_size*8);
02031
02032 width = get_bits(&s->gb, 12);
02033 height = get_bits(&s->gb, 12);
02034 if (width <= 0 || height <= 0)
02035 return -1;
02036 s->aspect_ratio_info= get_bits(&s->gb, 4);
02037 if (s->aspect_ratio_info == 0) {
02038 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
02039 if (avctx->error_recognition >= FF_ER_COMPLIANT)
02040 return -1;
02041 }
02042 s->frame_rate_index = get_bits(&s->gb, 4);
02043 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
02044 return -1;
02045 s->bit_rate = get_bits(&s->gb, 18) * 400;
02046 if (get_bits1(&s->gb) == 0)
02047 return -1;
02048 s->width = width;
02049 s->height = height;
02050
02051 s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
02052 skip_bits(&s->gb, 1);
02053
02054
02055 if (get_bits1(&s->gb)) {
02056 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
02057 } else {
02058 for(i=0;i<64;i++) {
02059 j = s->dsp.idct_permutation[i];
02060 v = ff_mpeg1_default_intra_matrix[i];
02061 s->intra_matrix[j] = v;
02062 s->chroma_intra_matrix[j] = v;
02063 }
02064 }
02065 if (get_bits1(&s->gb)) {
02066 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
02067 } else {
02068 for(i=0;i<64;i++) {
02069 int j= s->dsp.idct_permutation[i];
02070 v = ff_mpeg1_default_non_intra_matrix[i];
02071 s->inter_matrix[j] = v;
02072 s->chroma_inter_matrix[j] = v;
02073 }
02074 }
02075
02076 if(show_bits(&s->gb, 23) != 0){
02077 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
02078 return -1;
02079 }
02080
02081
02082 s->progressive_sequence = 1;
02083 s->progressive_frame = 1;
02084 s->picture_structure = PICT_FRAME;
02085 s->frame_pred_frame_dct = 1;
02086 s->chroma_format = 1;
02087 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
02088 avctx->sub_id = 1;
02089 s->out_format = FMT_MPEG1;
02090 s->swap_uv = 0;
02091 if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
02092
02093 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
02094 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
02095 s->avctx->rc_buffer_size, s->bit_rate);
02096
02097 return 0;
02098 }
02099
02100 static int vcr2_init_sequence(AVCodecContext *avctx)
02101 {
02102 Mpeg1Context *s1 = avctx->priv_data;
02103 MpegEncContext *s = &s1->mpeg_enc_ctx;
02104 int i, v;
02105
02106
02107 s->out_format = FMT_MPEG1;
02108 if (s1->mpeg_enc_ctx_allocated) {
02109 MPV_common_end(s);
02110 }
02111 s->width = avctx->coded_width;
02112 s->height = avctx->coded_height;
02113 avctx->has_b_frames= 0;
02114 s->low_delay= 1;
02115
02116 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
02117 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
02118
02119 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel )
02120 if( avctx->idct_algo == FF_IDCT_AUTO )
02121 avctx->idct_algo = FF_IDCT_SIMPLE;
02122
02123 if (MPV_common_init(s) < 0)
02124 return -1;
02125 exchange_uv(s);
02126 s->swap_uv = 1;
02127 s1->mpeg_enc_ctx_allocated = 1;
02128
02129 for(i=0;i<64;i++) {
02130 int j= s->dsp.idct_permutation[i];
02131 v = ff_mpeg1_default_intra_matrix[i];
02132 s->intra_matrix[j] = v;
02133 s->chroma_intra_matrix[j] = v;
02134
02135 v = ff_mpeg1_default_non_intra_matrix[i];
02136 s->inter_matrix[j] = v;
02137 s->chroma_inter_matrix[j] = v;
02138 }
02139
02140 s->progressive_sequence = 1;
02141 s->progressive_frame = 1;
02142 s->picture_structure = PICT_FRAME;
02143 s->frame_pred_frame_dct = 1;
02144 s->chroma_format = 1;
02145 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
02146 avctx->sub_id = 2;
02147 s1->save_width = s->width;
02148 s1->save_height = s->height;
02149 s1->save_progressive_seq = s->progressive_sequence;
02150 return 0;
02151 }
02152
02153
02154 static void mpeg_decode_user_data(AVCodecContext *avctx,
02155 const uint8_t *p, int buf_size)
02156 {
02157 const uint8_t *buf_end = p+buf_size;
02158
02159
02160 if (buf_end - p >= 5 &&
02161 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
02162 int flags = p[4];
02163 p += 5;
02164 if (flags & 0x80) {
02165
02166 p += 2;
02167 }
02168 if (flags & 0x40) {
02169 if (buf_end - p < 1)
02170 return;
02171 avctx->dtg_active_format = p[0] & 0x0f;
02172 }
02173 }
02174 }
02175
02176 static void mpeg_decode_gop(AVCodecContext *avctx,
02177 const uint8_t *buf, int buf_size){
02178 Mpeg1Context *s1 = avctx->priv_data;
02179 MpegEncContext *s = &s1->mpeg_enc_ctx;
02180
02181 int time_code_hours, time_code_minutes;
02182 int time_code_seconds, time_code_pictures;
02183 int broken_link;
02184
02185 init_get_bits(&s->gb, buf, buf_size*8);
02186
02187 skip_bits1(&s->gb);
02188
02189 time_code_hours=get_bits(&s->gb,5);
02190 time_code_minutes = get_bits(&s->gb,6);
02191 skip_bits1(&s->gb);
02192 time_code_seconds = get_bits(&s->gb,6);
02193 time_code_pictures = get_bits(&s->gb,6);
02194
02195 s->closed_gop = get_bits1(&s->gb);
02196
02197
02198
02199 broken_link = get_bits1(&s->gb);
02200
02201 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
02202 av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
02203 time_code_hours, time_code_minutes, time_code_seconds,
02204 time_code_pictures, s->closed_gop, broken_link);
02205 }
02210 int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
02211 {
02212 int i;
02213 uint32_t state= pc->state;
02214
02215
02216 if (buf_size == 0)
02217 return 0;
02218
02219
02220
02221
02222
02223
02224
02225
02226
02227 for(i=0; i<buf_size; i++){
02228 assert(pc->frame_start_found>=0 && pc->frame_start_found<=4);
02229 if(pc->frame_start_found&1){
02230 if(state == EXT_START_CODE && (buf[i]&0xF0) != 0x80)
02231 pc->frame_start_found--;
02232 else if(state == EXT_START_CODE+2){
02233 if((buf[i]&3) == 3) pc->frame_start_found= 0;
02234 else pc->frame_start_found= (pc->frame_start_found+1)&3;
02235 }
02236 state++;
02237 }else{
02238 i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
02239 if(pc->frame_start_found==0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
02240 i++;
02241 pc->frame_start_found=4;
02242 }
02243 if(state == SEQ_END_CODE){
02244 pc->state=-1;
02245 return i+1;
02246 }
02247 if(pc->frame_start_found==2 && state == SEQ_START_CODE)
02248 pc->frame_start_found= 0;
02249 if(pc->frame_start_found<4 && state == EXT_START_CODE)
02250 pc->frame_start_found++;
02251 if(pc->frame_start_found == 4 && (state&0xFFFFFF00) == 0x100){
02252 if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
02253 pc->frame_start_found=0;
02254 pc->state=-1;
02255 return i-3;
02256 }
02257 }
02258 if(pc->frame_start_found == 0 && s && state == PICTURE_START_CODE){
02259 ff_fetch_timestamp(s, i-3, 1);
02260 }
02261 }
02262 }
02263 pc->state= state;
02264 return END_NOT_FOUND;
02265 }
02266
02267 static int decode_chunks(AVCodecContext *avctx,
02268 AVFrame *picture, int *data_size,
02269 const uint8_t *buf, int buf_size);
02270
02271
02272 static int mpeg_decode_frame(AVCodecContext *avctx,
02273 void *data, int *data_size,
02274 AVPacket *avpkt)
02275 {
02276 const uint8_t *buf = avpkt->data;
02277 int buf_size = avpkt->size;
02278 Mpeg1Context *s = avctx->priv_data;
02279 AVFrame *picture = data;
02280 MpegEncContext *s2 = &s->mpeg_enc_ctx;
02281 av_dlog(avctx, "fill_buffer\n");
02282
02283 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
02284
02285 if (s2->low_delay==0 && s2->next_picture_ptr) {
02286 *picture= *(AVFrame*)s2->next_picture_ptr;
02287 s2->next_picture_ptr= NULL;
02288
02289 *data_size = sizeof(AVFrame);
02290 }
02291 return buf_size;
02292 }
02293
02294 if(s2->flags&CODEC_FLAG_TRUNCATED){
02295 int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
02296
02297 if( ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
02298 return buf_size;
02299 }
02300
02301 #if 0
02302 if (s->repeat_field % 2 == 1) {
02303 s->repeat_field++;
02304
02305
02306 if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
02307 *data_size = sizeof(AVPicture);
02308 goto the_end;
02309 }
02310 }
02311 #endif
02312
02313 if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == AV_RL32("VCR2"))
02314 vcr2_init_sequence(avctx);
02315
02316 s->slice_count= 0;
02317
02318 if(avctx->extradata && !avctx->frame_number)
02319 decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
02320
02321 return decode_chunks(avctx, picture, data_size, buf, buf_size);
02322 }
02323
02324 static int decode_chunks(AVCodecContext *avctx,
02325 AVFrame *picture, int *data_size,
02326 const uint8_t *buf, int buf_size)
02327 {
02328 Mpeg1Context *s = avctx->priv_data;
02329 MpegEncContext *s2 = &s->mpeg_enc_ctx;
02330 const uint8_t *buf_ptr = buf;
02331 const uint8_t *buf_end = buf + buf_size;
02332 int ret, input_size;
02333 int last_code= 0;
02334
02335 for(;;) {
02336
02337 uint32_t start_code = -1;
02338 buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
02339 if (start_code > 0x1ff){
02340 if(s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT){
02341 if(HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)){
02342 int i;
02343 av_assert0(avctx->thread_count > 1);
02344
02345 avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
02346 for(i=0; i<s->slice_count; i++)
02347 s2->error_count += s2->thread_context[i]->error_count;
02348 }
02349
02350 if (CONFIG_VDPAU && uses_vdpau(avctx))
02351 ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
02352
02353 if (slice_end(avctx, picture)) {
02354 if(s2->last_picture_ptr || s2->low_delay)
02355 *data_size = sizeof(AVPicture);
02356 }
02357 }
02358 s2->pict_type= 0;
02359 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
02360 }
02361
02362 input_size = buf_end - buf_ptr;
02363
02364 if(avctx->debug & FF_DEBUG_STARTCODE){
02365 av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
02366 }
02367
02368
02369 switch(start_code) {
02370 case SEQ_START_CODE:
02371 if(last_code == 0){
02372 mpeg1_decode_sequence(avctx, buf_ptr,
02373 input_size);
02374 s->sync=1;
02375 }else{
02376 av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
02377 }
02378 break;
02379
02380 case PICTURE_START_CODE:
02381 if (HAVE_THREADS && (avctx->active_thread_type&FF_THREAD_SLICE) && s->slice_count) {
02382 int i;
02383
02384 avctx->execute(avctx, slice_decode_thread,
02385 s2->thread_context, NULL,
02386 s->slice_count, sizeof(void*));
02387 for (i = 0; i < s->slice_count; i++)
02388 s2->error_count += s2->thread_context[i]->error_count;
02389 s->slice_count = 0;
02390 }
02391 if(last_code == 0 || last_code == SLICE_MIN_START_CODE){
02392 if(mpeg_decode_postinit(avctx) < 0){
02393 av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
02394 return -1;
02395 }
02396
02397
02398 if(mpeg1_decode_picture(avctx,
02399 buf_ptr, input_size) < 0)
02400 s2->pict_type=0;
02401 s2->first_slice = 1;
02402 last_code= PICTURE_START_CODE;
02403 }else{
02404 av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
02405 }
02406 break;
02407 case EXT_START_CODE:
02408 init_get_bits(&s2->gb, buf_ptr, input_size*8);
02409
02410 switch(get_bits(&s2->gb, 4)) {
02411 case 0x1:
02412 if(last_code == 0){
02413 mpeg_decode_sequence_extension(s);
02414 }else{
02415 av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
02416 }
02417 break;
02418 case 0x2:
02419 mpeg_decode_sequence_display_extension(s);
02420 break;
02421 case 0x3:
02422 mpeg_decode_quant_matrix_extension(s2);
02423 break;
02424 case 0x7:
02425 mpeg_decode_picture_display_extension(s);
02426 break;
02427 case 0x8:
02428 if(last_code == PICTURE_START_CODE){
02429 mpeg_decode_picture_coding_extension(s);
02430 }else{
02431 av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
02432 }
02433 break;
02434 }
02435 break;
02436 case USER_START_CODE:
02437 mpeg_decode_user_data(avctx,
02438 buf_ptr, input_size);
02439 break;
02440 case GOP_START_CODE:
02441 if(last_code == 0){
02442 s2->first_field=0;
02443 mpeg_decode_gop(avctx,
02444 buf_ptr, input_size);
02445 s->sync=1;
02446 }else{
02447 av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
02448 }
02449 break;
02450 default:
02451 if (start_code >= SLICE_MIN_START_CODE &&
02452 start_code <= SLICE_MAX_START_CODE && last_code!=0) {
02453 const int field_pic= s2->picture_structure != PICT_FRAME;
02454 int mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
02455 last_code= SLICE_MIN_START_CODE;
02456
02457 if(s2->picture_structure == PICT_BOTTOM_FIELD)
02458 mb_y++;
02459
02460 if (mb_y >= s2->mb_height){
02461 av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
02462 return -1;
02463 }
02464
02465 if(s2->last_picture_ptr==NULL){
02466
02467 if(s2->pict_type==AV_PICTURE_TYPE_B){
02468 if(!s2->closed_gop)
02469 break;
02470 }
02471 }
02472 if(s2->pict_type==AV_PICTURE_TYPE_I)
02473 s->sync=1;
02474 if(s2->next_picture_ptr==NULL){
02475
02476 if(s2->pict_type==AV_PICTURE_TYPE_P && !s->sync) break;
02477 }
02478 #if FF_API_HURRY_UP
02479
02480 if(avctx->hurry_up && s2->pict_type==FF_B_TYPE) break;
02481 #endif
02482 if( (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==AV_PICTURE_TYPE_B)
02483 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=AV_PICTURE_TYPE_I)
02484 || avctx->skip_frame >= AVDISCARD_ALL)
02485 break;
02486 #if FF_API_HURRY_UP
02487
02488 if(avctx->hurry_up>=5) break;
02489 #endif
02490
02491 if (!s->mpeg_enc_ctx_allocated) break;
02492
02493 if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
02494 if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
02495 break;
02496 }
02497
02498 if(!s2->pict_type){
02499 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
02500 break;
02501 }
02502
02503 if(s2->first_slice){
02504 s2->first_slice=0;
02505 if(mpeg_field_start(s2, buf, buf_size) < 0)
02506 return -1;
02507 }
02508 if(!s2->current_picture_ptr){
02509 av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
02510 return -1;
02511 }
02512
02513 if (uses_vdpau(avctx)) {
02514 s->slice_count++;
02515 break;
02516 }
02517
02518 if(HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)){
02519 int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
02520 av_assert0(avctx->thread_count > 1);
02521 if(threshold <= mb_y){
02522 MpegEncContext *thread_context= s2->thread_context[s->slice_count];
02523
02524 thread_context->start_mb_y= mb_y;
02525 thread_context->end_mb_y = s2->mb_height;
02526 if(s->slice_count){
02527 s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
02528 ff_update_duplicate_context(thread_context, s2);
02529 }
02530 init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
02531 s->slice_count++;
02532 }
02533 buf_ptr += 2;
02534 }else{
02535 ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
02536 emms_c();
02537
02538 if(ret < 0){
02539 if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
02540 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);
02541 }else{
02542 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);
02543 }
02544 }
02545 }
02546 break;
02547 }
02548 }
02549 }
02550
02551 static void flush(AVCodecContext *avctx){
02552 Mpeg1Context *s = avctx->priv_data;
02553
02554 s->sync=0;
02555
02556 ff_mpeg_flush(avctx);
02557 }
02558
02559 static int mpeg_decode_end(AVCodecContext *avctx)
02560 {
02561 Mpeg1Context *s = avctx->priv_data;
02562
02563 if (s->mpeg_enc_ctx_allocated)
02564 MPV_common_end(&s->mpeg_enc_ctx);
02565 return 0;
02566 }
02567
02568 static const AVProfile mpeg2_video_profiles[] = {
02569 { FF_PROFILE_MPEG2_422, "4:2:2" },
02570 { FF_PROFILE_MPEG2_HIGH, "High" },
02571 { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
02572 { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
02573 { FF_PROFILE_MPEG2_MAIN, "Main" },
02574 { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
02575 { FF_PROFILE_RESERVED, "Reserved" },
02576 { FF_PROFILE_RESERVED, "Reserved" },
02577 { FF_PROFILE_UNKNOWN },
02578 };
02579
02580
02581 AVCodec ff_mpeg1video_decoder = {
02582 "mpeg1video",
02583 AVMEDIA_TYPE_VIDEO,
02584 CODEC_ID_MPEG1VIDEO,
02585 sizeof(Mpeg1Context),
02586 mpeg_decode_init,
02587 NULL,
02588 mpeg_decode_end,
02589 mpeg_decode_frame,
02590 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
02591 .flush= flush,
02592 .max_lowres= 3,
02593 .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
02594 .update_thread_context= ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
02595 };
02596
02597 AVCodec ff_mpeg2video_decoder = {
02598 "mpeg2video",
02599 AVMEDIA_TYPE_VIDEO,
02600 CODEC_ID_MPEG2VIDEO,
02601 sizeof(Mpeg1Context),
02602 mpeg_decode_init,
02603 NULL,
02604 mpeg_decode_end,
02605 mpeg_decode_frame,
02606 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
02607 .flush= flush,
02608 .max_lowres= 3,
02609 .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
02610 .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
02611 };
02612
02613
02614 AVCodec ff_mpegvideo_decoder = {
02615 "mpegvideo",
02616 AVMEDIA_TYPE_VIDEO,
02617 CODEC_ID_MPEG2VIDEO,
02618 sizeof(Mpeg1Context),
02619 mpeg_decode_init,
02620 NULL,
02621 mpeg_decode_end,
02622 mpeg_decode_frame,
02623 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
02624 .flush= flush,
02625 .max_lowres= 3,
02626 .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
02627 };
02628
02629 #if CONFIG_MPEG_XVMC_DECODER
02630 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx){
02631 if( avctx->active_thread_type & FF_THREAD_SLICE )
02632 return -1;
02633 if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
02634 return -1;
02635 if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
02636 av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
02637 }
02638 mpeg_decode_init(avctx);
02639
02640 avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
02641 avctx->xvmc_acceleration = 2;
02642
02643 return 0;
02644 }
02645
02646 AVCodec ff_mpeg_xvmc_decoder = {
02647 "mpegvideo_xvmc",
02648 AVMEDIA_TYPE_VIDEO,
02649 CODEC_ID_MPEG2VIDEO_XVMC,
02650 sizeof(Mpeg1Context),
02651 mpeg_mc_decode_init,
02652 NULL,
02653 mpeg_decode_end,
02654 mpeg_decode_frame,
02655 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
02656 .flush= flush,
02657 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
02658 };
02659
02660 #endif
02661
02662 #if CONFIG_MPEG_VDPAU_DECODER
02663 AVCodec ff_mpeg_vdpau_decoder = {
02664 "mpegvideo_vdpau",
02665 AVMEDIA_TYPE_VIDEO,
02666 CODEC_ID_MPEG2VIDEO,
02667 sizeof(Mpeg1Context),
02668 mpeg_decode_init,
02669 NULL,
02670 mpeg_decode_end,
02671 mpeg_decode_frame,
02672 CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
02673 .flush= flush,
02674 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
02675 };
02676 #endif
02677
02678 #if CONFIG_MPEG1_VDPAU_DECODER
02679 AVCodec ff_mpeg1_vdpau_decoder = {
02680 "mpeg1video_vdpau",
02681 AVMEDIA_TYPE_VIDEO,
02682 CODEC_ID_MPEG1VIDEO,
02683 sizeof(Mpeg1Context),
02684 mpeg_decode_init,
02685 NULL,
02686 mpeg_decode_end,
02687 mpeg_decode_frame,
02688 CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
02689 .flush= flush,
02690 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
02691 };
02692 #endif
02693