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