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