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