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