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 dprintf(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 dprintf(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 dprintf(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 dprintf(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 dprintf(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 dprintf(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 #if MIN_CACHE_BITS < 19
00722 UPDATE_CACHE(re, &s->gb);
00723 #endif
00724
00725 for(;;) {
00726 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00727
00728 if(level != 0) {
00729 i += run;
00730 j = scantable[i];
00731 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
00732 level= (level-1)|1;
00733 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00734 SKIP_BITS(re, &s->gb, 1);
00735 } else {
00736
00737 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00738 UPDATE_CACHE(re, &s->gb);
00739 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
00740 if (level == -128) {
00741 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
00742 } else if (level == 0) {
00743 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
00744 }
00745 i += run;
00746 j = scantable[i];
00747 if(level<0){
00748 level= -level;
00749 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
00750 level= (level-1)|1;
00751 level= -level;
00752 }else{
00753 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
00754 level= (level-1)|1;
00755 }
00756 }
00757 if (i > 63){
00758 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00759 return -1;
00760 }
00761
00762 block[j] = level;
00763 #if MIN_CACHE_BITS < 19
00764 UPDATE_CACHE(re, &s->gb);
00765 #endif
00766 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00767 break;
00768 #if MIN_CACHE_BITS >= 19
00769 UPDATE_CACHE(re, &s->gb);
00770 #endif
00771 }
00772 end:
00773 LAST_SKIP_BITS(re, &s->gb, 2);
00774 CLOSE_READER(re, &s->gb);
00775 }
00776 s->block_last_index[n] = i;
00777 return 0;
00778 }
00779
00780 static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
00781 {
00782 int level, i, j, run;
00783 RLTable *rl = &ff_rl_mpeg1;
00784 uint8_t * const scantable= s->intra_scantable.permutated;
00785 const int qscale= s->qscale;
00786
00787 {
00788 OPEN_READER(re, &s->gb);
00789 i = -1;
00790
00791 UPDATE_CACHE(re, &s->gb);
00792 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00793 level= (3*qscale)>>1;
00794 level= (level-1)|1;
00795 if(GET_CACHE(re, &s->gb)&0x40000000)
00796 level= -level;
00797 block[0] = level;
00798 i++;
00799 SKIP_BITS(re, &s->gb, 2);
00800 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00801 goto end;
00802 }
00803 #if MIN_CACHE_BITS < 19
00804 UPDATE_CACHE(re, &s->gb);
00805 #endif
00806
00807
00808 for(;;) {
00809 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00810
00811 if(level != 0) {
00812 i += run;
00813 j = scantable[i];
00814 level= ((level*2+1)*qscale)>>1;
00815 level= (level-1)|1;
00816 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00817 SKIP_BITS(re, &s->gb, 1);
00818 } else {
00819
00820 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00821 UPDATE_CACHE(re, &s->gb);
00822 level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
00823 if (level == -128) {
00824 level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
00825 } else if (level == 0) {
00826 level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
00827 }
00828 i += run;
00829 j = scantable[i];
00830 if(level<0){
00831 level= -level;
00832 level= ((level*2+1)*qscale)>>1;
00833 level= (level-1)|1;
00834 level= -level;
00835 }else{
00836 level= ((level*2+1)*qscale)>>1;
00837 level= (level-1)|1;
00838 }
00839 }
00840
00841 block[j] = level;
00842 #if MIN_CACHE_BITS < 19
00843 UPDATE_CACHE(re, &s->gb);
00844 #endif
00845 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00846 break;
00847 #if MIN_CACHE_BITS >= 19
00848 UPDATE_CACHE(re, &s->gb);
00849 #endif
00850 }
00851 end:
00852 LAST_SKIP_BITS(re, &s->gb, 2);
00853 CLOSE_READER(re, &s->gb);
00854 }
00855 s->block_last_index[n] = i;
00856 return 0;
00857 }
00858
00859
00860 static inline int mpeg2_decode_block_non_intra(MpegEncContext *s,
00861 DCTELEM *block,
00862 int n)
00863 {
00864 int level, i, j, run;
00865 RLTable *rl = &ff_rl_mpeg1;
00866 uint8_t * const scantable= s->intra_scantable.permutated;
00867 const uint16_t *quant_matrix;
00868 const int qscale= s->qscale;
00869 int mismatch;
00870
00871 mismatch = 1;
00872
00873 {
00874 OPEN_READER(re, &s->gb);
00875 i = -1;
00876 if (n < 4)
00877 quant_matrix = s->inter_matrix;
00878 else
00879 quant_matrix = s->chroma_inter_matrix;
00880
00881
00882 UPDATE_CACHE(re, &s->gb);
00883 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00884 level= (3*qscale*quant_matrix[0])>>5;
00885 if(GET_CACHE(re, &s->gb)&0x40000000)
00886 level= -level;
00887 block[0] = level;
00888 mismatch ^= level;
00889 i++;
00890 SKIP_BITS(re, &s->gb, 2);
00891 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00892 goto end;
00893 }
00894 #if MIN_CACHE_BITS < 19
00895 UPDATE_CACHE(re, &s->gb);
00896 #endif
00897
00898
00899 for(;;) {
00900 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00901
00902 if(level != 0) {
00903 i += run;
00904 j = scantable[i];
00905 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
00906 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00907 SKIP_BITS(re, &s->gb, 1);
00908 } else {
00909
00910 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00911 UPDATE_CACHE(re, &s->gb);
00912 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
00913
00914 i += run;
00915 j = scantable[i];
00916 if(level<0){
00917 level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
00918 level= -level;
00919 }else{
00920 level= ((level*2+1)*qscale*quant_matrix[j])>>5;
00921 }
00922 }
00923 if (i > 63){
00924 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
00925 return -1;
00926 }
00927
00928 mismatch ^= level;
00929 block[j] = level;
00930 #if MIN_CACHE_BITS < 19
00931 UPDATE_CACHE(re, &s->gb);
00932 #endif
00933 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00934 break;
00935 #if MIN_CACHE_BITS >= 19
00936 UPDATE_CACHE(re, &s->gb);
00937 #endif
00938 }
00939 end:
00940 LAST_SKIP_BITS(re, &s->gb, 2);
00941 CLOSE_READER(re, &s->gb);
00942 }
00943 block[63] ^= (mismatch & 1);
00944
00945 s->block_last_index[n] = i;
00946 return 0;
00947 }
00948
00949 static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
00950 DCTELEM *block,
00951 int n)
00952 {
00953 int level, i, j, run;
00954 RLTable *rl = &ff_rl_mpeg1;
00955 uint8_t * const scantable= s->intra_scantable.permutated;
00956 const int qscale= s->qscale;
00957 OPEN_READER(re, &s->gb);
00958 i = -1;
00959
00960
00961 UPDATE_CACHE(re, &s->gb);
00962 if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
00963 level= (3*qscale)>>1;
00964 if(GET_CACHE(re, &s->gb)&0x40000000)
00965 level= -level;
00966 block[0] = level;
00967 i++;
00968 SKIP_BITS(re, &s->gb, 2);
00969 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
00970 goto end;
00971 }
00972 #if MIN_CACHE_BITS < 19
00973 UPDATE_CACHE(re, &s->gb);
00974 #endif
00975
00976
00977 for(;;) {
00978 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
00979
00980 if(level != 0) {
00981 i += run;
00982 j = scantable[i];
00983 level= ((level*2+1)*qscale)>>1;
00984 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
00985 SKIP_BITS(re, &s->gb, 1);
00986 } else {
00987
00988 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
00989 UPDATE_CACHE(re, &s->gb);
00990 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
00991
00992 i += run;
00993 j = scantable[i];
00994 if(level<0){
00995 level= ((-level*2+1)*qscale)>>1;
00996 level= -level;
00997 }else{
00998 level= ((level*2+1)*qscale)>>1;
00999 }
01000 }
01001
01002 block[j] = level;
01003 #if MIN_CACHE_BITS < 19
01004 UPDATE_CACHE(re, &s->gb);
01005 #endif
01006 if(((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
01007 break;
01008 #if MIN_CACHE_BITS >=19
01009 UPDATE_CACHE(re, &s->gb);
01010 #endif
01011 }
01012 end:
01013 LAST_SKIP_BITS(re, &s->gb, 2);
01014 CLOSE_READER(re, &s->gb);
01015 s->block_last_index[n] = i;
01016 return 0;
01017 }
01018
01019
01020 static inline int mpeg2_decode_block_intra(MpegEncContext *s,
01021 DCTELEM *block,
01022 int n)
01023 {
01024 int level, dc, diff, i, j, run;
01025 int component;
01026 RLTable *rl;
01027 uint8_t * const scantable= s->intra_scantable.permutated;
01028 const uint16_t *quant_matrix;
01029 const int qscale= s->qscale;
01030 int mismatch;
01031
01032
01033 if (n < 4){
01034 quant_matrix = s->intra_matrix;
01035 component = 0;
01036 }else{
01037 quant_matrix = s->chroma_intra_matrix;
01038 component = (n&1) + 1;
01039 }
01040 diff = decode_dc(&s->gb, component);
01041 if (diff >= 0xffff)
01042 return -1;
01043 dc = s->last_dc[component];
01044 dc += diff;
01045 s->last_dc[component] = dc;
01046 block[0] = dc << (3 - s->intra_dc_precision);
01047 dprintf(s->avctx, "dc=%d\n", block[0]);
01048 mismatch = block[0] ^ 1;
01049 i = 0;
01050 if (s->intra_vlc_format)
01051 rl = &ff_rl_mpeg2;
01052 else
01053 rl = &ff_rl_mpeg1;
01054
01055 {
01056 OPEN_READER(re, &s->gb);
01057
01058 for(;;) {
01059 UPDATE_CACHE(re, &s->gb);
01060 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
01061
01062 if(level == 127){
01063 break;
01064 } else if(level != 0) {
01065 i += run;
01066 j = scantable[i];
01067 level= (level*qscale*quant_matrix[j])>>4;
01068 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
01069 LAST_SKIP_BITS(re, &s->gb, 1);
01070 } else {
01071
01072 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
01073 UPDATE_CACHE(re, &s->gb);
01074 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
01075 i += run;
01076 j = scantable[i];
01077 if(level<0){
01078 level= (-level*qscale*quant_matrix[j])>>4;
01079 level= -level;
01080 }else{
01081 level= (level*qscale*quant_matrix[j])>>4;
01082 }
01083 }
01084 if (i > 63){
01085 av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
01086 return -1;
01087 }
01088
01089 mismatch^= level;
01090 block[j] = level;
01091 }
01092 CLOSE_READER(re, &s->gb);
01093 }
01094 block[63]^= mismatch&1;
01095
01096 s->block_last_index[n] = i;
01097 return 0;
01098 }
01099
01100 static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s,
01101 DCTELEM *block,
01102 int n)
01103 {
01104 int level, dc, diff, j, run;
01105 int component;
01106 RLTable *rl;
01107 uint8_t * scantable= s->intra_scantable.permutated;
01108 const uint16_t *quant_matrix;
01109 const int qscale= s->qscale;
01110
01111
01112 if (n < 4){
01113 quant_matrix = s->intra_matrix;
01114 component = 0;
01115 }else{
01116 quant_matrix = s->chroma_intra_matrix;
01117 component = (n&1) + 1;
01118 }
01119 diff = decode_dc(&s->gb, component);
01120 if (diff >= 0xffff)
01121 return -1;
01122 dc = s->last_dc[component];
01123 dc += diff;
01124 s->last_dc[component] = dc;
01125 block[0] = dc << (3 - s->intra_dc_precision);
01126 if (s->intra_vlc_format)
01127 rl = &ff_rl_mpeg2;
01128 else
01129 rl = &ff_rl_mpeg1;
01130
01131 {
01132 OPEN_READER(re, &s->gb);
01133
01134 for(;;) {
01135 UPDATE_CACHE(re, &s->gb);
01136 GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
01137
01138 if(level == 127){
01139 break;
01140 } else if(level != 0) {
01141 scantable += run;
01142 j = *scantable;
01143 level= (level*qscale*quant_matrix[j])>>4;
01144 level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
01145 LAST_SKIP_BITS(re, &s->gb, 1);
01146 } else {
01147
01148 run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
01149 UPDATE_CACHE(re, &s->gb);
01150 level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
01151 scantable += run;
01152 j = *scantable;
01153 if(level<0){
01154 level= (-level*qscale*quant_matrix[j])>>4;
01155 level= -level;
01156 }else{
01157 level= (level*qscale*quant_matrix[j])>>4;
01158 }
01159 }
01160
01161 block[j] = level;
01162 }
01163 CLOSE_READER(re, &s->gb);
01164 }
01165
01166 s->block_last_index[n] = scantable - s->intra_scantable.permutated;
01167 return 0;
01168 }
01169
01170 typedef struct Mpeg1Context {
01171 MpegEncContext mpeg_enc_ctx;
01172 int mpeg_enc_ctx_allocated;
01173 int repeat_field;
01174 AVPanScan pan_scan;
01175 int slice_count;
01176 int swap_uv;
01177 int save_aspect_info;
01178 int save_width, save_height, save_progressive_seq;
01179 AVRational frame_rate_ext;
01180 int sync;
01181 } Mpeg1Context;
01182
01183 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
01184 {
01185 Mpeg1Context *s = avctx->priv_data;
01186 MpegEncContext *s2 = &s->mpeg_enc_ctx;
01187 int i;
01188
01189
01190
01191 for(i=0;i<64;i++)
01192 s2->dsp.idct_permutation[i]=i;
01193
01194 MPV_decode_defaults(s2);
01195
01196 s->mpeg_enc_ctx.avctx= avctx;
01197 s->mpeg_enc_ctx.flags= avctx->flags;
01198 s->mpeg_enc_ctx.flags2= avctx->flags2;
01199 ff_mpeg12_common_init(&s->mpeg_enc_ctx);
01200 ff_mpeg12_init_vlcs();
01201
01202 s->mpeg_enc_ctx_allocated = 0;
01203 s->mpeg_enc_ctx.picture_number = 0;
01204 s->repeat_field = 0;
01205 s->mpeg_enc_ctx.codec_id= avctx->codec->id;
01206 avctx->color_range= AVCOL_RANGE_MPEG;
01207 if (avctx->codec->id == CODEC_ID_MPEG1VIDEO)
01208 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
01209 else
01210 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
01211 return 0;
01212 }
01213
01214 static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
01215 const uint8_t *new_perm){
01216 uint16_t temp_matrix[64];
01217 int i;
01218
01219 memcpy(temp_matrix,matrix,64*sizeof(uint16_t));
01220
01221 for(i=0;i<64;i++){
01222 matrix[new_perm[i]] = temp_matrix[old_perm[i]];
01223 }
01224 }
01225
01226 static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx){
01227 Mpeg1Context *s1 = avctx->priv_data;
01228 MpegEncContext *s = &s1->mpeg_enc_ctx;
01229
01230 if(avctx->xvmc_acceleration)
01231 return avctx->get_format(avctx,pixfmt_xvmc_mpg2_420);
01232 else if(avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
01233 if(avctx->codec_id == CODEC_ID_MPEG1VIDEO)
01234 return PIX_FMT_VDPAU_MPEG1;
01235 else
01236 return PIX_FMT_VDPAU_MPEG2;
01237 }else{
01238 if(s->chroma_format < 2)
01239 return avctx->get_format(avctx,ff_hwaccel_pixfmt_list_420);
01240 else if(s->chroma_format == 2)
01241 return PIX_FMT_YUV422P;
01242 else
01243 return PIX_FMT_YUV444P;
01244 }
01245 }
01246
01247
01248
01249 static int mpeg_decode_postinit(AVCodecContext *avctx){
01250 Mpeg1Context *s1 = avctx->priv_data;
01251 MpegEncContext *s = &s1->mpeg_enc_ctx;
01252 uint8_t old_permutation[64];
01253
01254 if (
01255 (s1->mpeg_enc_ctx_allocated == 0)||
01256 avctx->coded_width != s->width ||
01257 avctx->coded_height != s->height||
01258 s1->save_width != s->width ||
01259 s1->save_height != s->height ||
01260 s1->save_aspect_info != s->aspect_ratio_info||
01261 s1->save_progressive_seq != s->progressive_sequence ||
01262 0)
01263 {
01264
01265 if (s1->mpeg_enc_ctx_allocated) {
01266 ParseContext pc= s->parse_context;
01267 s->parse_context.buffer=0;
01268 MPV_common_end(s);
01269 s->parse_context= pc;
01270 }
01271
01272 if( (s->width == 0 )||(s->height == 0))
01273 return -2;
01274
01275 avcodec_set_dimensions(avctx, s->width, s->height);
01276 avctx->bit_rate = s->bit_rate;
01277 s1->save_aspect_info = s->aspect_ratio_info;
01278 s1->save_width = s->width;
01279 s1->save_height = s->height;
01280 s1->save_progressive_seq = s->progressive_sequence;
01281
01282
01283
01284 avctx->has_b_frames = !(s->low_delay);
01285
01286 assert((avctx->sub_id==1) == (avctx->codec_id==CODEC_ID_MPEG1VIDEO));
01287 if(avctx->codec_id==CODEC_ID_MPEG1VIDEO){
01288
01289 avctx->time_base.den= ff_frame_rate_tab[s->frame_rate_index].num;
01290 avctx->time_base.num= ff_frame_rate_tab[s->frame_rate_index].den;
01291
01292 avctx->sample_aspect_ratio= av_d2q(
01293 1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
01294 avctx->ticks_per_frame=1;
01295 }else{
01296
01297 av_reduce(
01298 &s->avctx->time_base.den,
01299 &s->avctx->time_base.num,
01300 ff_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
01301 ff_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
01302 1<<30);
01303 avctx->ticks_per_frame=2;
01304
01305 if(s->aspect_ratio_info > 1){
01306
01307
01308 if( (s1->pan_scan.width == 0 )||(s1->pan_scan.height == 0) || 1){
01309 s->avctx->sample_aspect_ratio=
01310 av_div_q(
01311 ff_mpeg2_aspect[s->aspect_ratio_info],
01312 (AVRational){s->width, s->height}
01313 );
01314 }else{
01315 s->avctx->sample_aspect_ratio=
01316 av_div_q(
01317 ff_mpeg2_aspect[s->aspect_ratio_info],
01318 (AVRational){s1->pan_scan.width, s1->pan_scan.height}
01319 );
01320 }
01321 }else{
01322 s->avctx->sample_aspect_ratio=
01323 ff_mpeg2_aspect[s->aspect_ratio_info];
01324 }
01325 }
01326
01327 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
01328 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
01329
01330 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
01331 avctx->hwaccel ||
01332 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU )
01333 if( avctx->idct_algo == FF_IDCT_AUTO )
01334 avctx->idct_algo = FF_IDCT_SIMPLE;
01335
01336
01337
01338 memcpy(old_permutation,s->dsp.idct_permutation,64*sizeof(uint8_t));
01339
01340 if (MPV_common_init(s) < 0)
01341 return -2;
01342
01343 quant_matrix_rebuild(s->intra_matrix, old_permutation,s->dsp.idct_permutation);
01344 quant_matrix_rebuild(s->inter_matrix, old_permutation,s->dsp.idct_permutation);
01345 quant_matrix_rebuild(s->chroma_intra_matrix,old_permutation,s->dsp.idct_permutation);
01346 quant_matrix_rebuild(s->chroma_inter_matrix,old_permutation,s->dsp.idct_permutation);
01347
01348 s1->mpeg_enc_ctx_allocated = 1;
01349 }
01350 return 0;
01351 }
01352
01353 static int mpeg1_decode_picture(AVCodecContext *avctx,
01354 const uint8_t *buf, int buf_size)
01355 {
01356 Mpeg1Context *s1 = avctx->priv_data;
01357 MpegEncContext *s = &s1->mpeg_enc_ctx;
01358 int ref, f_code, vbv_delay;
01359
01360 init_get_bits(&s->gb, buf, buf_size*8);
01361
01362 ref = get_bits(&s->gb, 10);
01363 s->pict_type = get_bits(&s->gb, 3);
01364 if(s->pict_type == 0 || s->pict_type > 3)
01365 return -1;
01366
01367 vbv_delay= get_bits(&s->gb, 16);
01368 if (s->pict_type == FF_P_TYPE || s->pict_type == FF_B_TYPE) {
01369 s->full_pel[0] = get_bits1(&s->gb);
01370 f_code = get_bits(&s->gb, 3);
01371 if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
01372 return -1;
01373 s->mpeg_f_code[0][0] = f_code;
01374 s->mpeg_f_code[0][1] = f_code;
01375 }
01376 if (s->pict_type == FF_B_TYPE) {
01377 s->full_pel[1] = get_bits1(&s->gb);
01378 f_code = get_bits(&s->gb, 3);
01379 if (f_code == 0 && avctx->error_recognition >= FF_ER_COMPLIANT)
01380 return -1;
01381 s->mpeg_f_code[1][0] = f_code;
01382 s->mpeg_f_code[1][1] = f_code;
01383 }
01384 s->current_picture.pict_type= s->pict_type;
01385 s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
01386
01387 if(avctx->debug & FF_DEBUG_PICT_INFO)
01388 av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
01389
01390 s->y_dc_scale = 8;
01391 s->c_dc_scale = 8;
01392 return 0;
01393 }
01394
01395 static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
01396 {
01397 MpegEncContext *s= &s1->mpeg_enc_ctx;
01398 int horiz_size_ext, vert_size_ext;
01399 int bit_rate_ext;
01400
01401 skip_bits(&s->gb, 1);
01402 s->avctx->profile= get_bits(&s->gb, 3);
01403 s->avctx->level= get_bits(&s->gb, 4);
01404 s->progressive_sequence = get_bits1(&s->gb);
01405 s->chroma_format = get_bits(&s->gb, 2);
01406 horiz_size_ext = get_bits(&s->gb, 2);
01407 vert_size_ext = get_bits(&s->gb, 2);
01408 s->width |= (horiz_size_ext << 12);
01409 s->height |= (vert_size_ext << 12);
01410 bit_rate_ext = get_bits(&s->gb, 12);
01411 s->bit_rate += (bit_rate_ext << 18) * 400;
01412 skip_bits1(&s->gb);
01413 s->avctx->rc_buffer_size += get_bits(&s->gb, 8)*1024*16<<10;
01414
01415 s->low_delay = get_bits1(&s->gb);
01416 if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
01417
01418 s1->frame_rate_ext.num = get_bits(&s->gb, 2)+1;
01419 s1->frame_rate_ext.den = get_bits(&s->gb, 5)+1;
01420
01421 dprintf(s->avctx, "sequence extension\n");
01422 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
01423 s->avctx->sub_id = 2;
01424
01425 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
01426 av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
01427 s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
01428
01429 }
01430
01431 static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
01432 {
01433 MpegEncContext *s= &s1->mpeg_enc_ctx;
01434 int color_description, w, h;
01435
01436 skip_bits(&s->gb, 3);
01437 color_description= get_bits1(&s->gb);
01438 if(color_description){
01439 s->avctx->color_primaries= get_bits(&s->gb, 8);
01440 s->avctx->color_trc = get_bits(&s->gb, 8);
01441 s->avctx->colorspace = get_bits(&s->gb, 8);
01442 }
01443 w= get_bits(&s->gb, 14);
01444 skip_bits(&s->gb, 1);
01445 h= get_bits(&s->gb, 14);
01446
01447
01448 s1->pan_scan.width= 16*w;
01449 s1->pan_scan.height=16*h;
01450
01451 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
01452 av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
01453 }
01454
01455 static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
01456 {
01457 MpegEncContext *s= &s1->mpeg_enc_ctx;
01458 int i,nofco;
01459
01460 nofco = 1;
01461 if(s->progressive_sequence){
01462 if(s->repeat_first_field){
01463 nofco++;
01464 if(s->top_field_first)
01465 nofco++;
01466 }
01467 }else{
01468 if(s->picture_structure == PICT_FRAME){
01469 nofco++;
01470 if(s->repeat_first_field)
01471 nofco++;
01472 }
01473 }
01474 for(i=0; i<nofco; i++){
01475 s1->pan_scan.position[i][0]= get_sbits(&s->gb, 16);
01476 skip_bits(&s->gb, 1);
01477 s1->pan_scan.position[i][1]= get_sbits(&s->gb, 16);
01478 skip_bits(&s->gb, 1);
01479 }
01480
01481 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
01482 av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
01483 s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
01484 s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
01485 s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]
01486 );
01487 }
01488
01489 static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra){
01490 int i;
01491
01492 for(i=0; i<64; i++) {
01493 int j = s->dsp.idct_permutation[ ff_zigzag_direct[i] ];
01494 int v = get_bits(&s->gb, 8);
01495 if(v==0){
01496 av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
01497 return -1;
01498 }
01499 if(intra && i==0 && v!=8){
01500 av_log(s->avctx, AV_LOG_ERROR, "intra matrix invalid, ignoring\n");
01501 v= 8;
01502 }
01503 matrix0[j] = v;
01504 if(matrix1)
01505 matrix1[j] = v;
01506 }
01507 return 0;
01508 }
01509
01510 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
01511 {
01512 dprintf(s->avctx, "matrix extension\n");
01513
01514 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
01515 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
01516 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
01517 if(get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
01518 }
01519
01520 static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
01521 {
01522 MpegEncContext *s= &s1->mpeg_enc_ctx;
01523
01524 s->full_pel[0] = s->full_pel[1] = 0;
01525 s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
01526 s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
01527 s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
01528 s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
01529 if(!s->pict_type && s1->mpeg_enc_ctx_allocated){
01530 av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
01531 if(s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1]==15){
01532 if(s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
01533 s->pict_type= FF_I_TYPE;
01534 else
01535 s->pict_type= FF_P_TYPE;
01536 }else
01537 s->pict_type= FF_B_TYPE;
01538 s->current_picture.pict_type= s->pict_type;
01539 s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
01540 }
01541 s->intra_dc_precision = get_bits(&s->gb, 2);
01542 s->picture_structure = get_bits(&s->gb, 2);
01543 s->top_field_first = get_bits1(&s->gb);
01544 s->frame_pred_frame_dct = get_bits1(&s->gb);
01545 s->concealment_motion_vectors = get_bits1(&s->gb);
01546 s->q_scale_type = get_bits1(&s->gb);
01547 s->intra_vlc_format = get_bits1(&s->gb);
01548 s->alternate_scan = get_bits1(&s->gb);
01549 s->repeat_first_field = get_bits1(&s->gb);
01550 s->chroma_420_type = get_bits1(&s->gb);
01551 s->progressive_frame = get_bits1(&s->gb);
01552
01553 if(s->progressive_sequence && !s->progressive_frame){
01554 s->progressive_frame= 1;
01555 av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
01556 }
01557
01558 if(s->picture_structure==0 || (s->progressive_frame && s->picture_structure!=PICT_FRAME)){
01559 av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
01560 s->picture_structure= PICT_FRAME;
01561 }
01562
01563 if(s->progressive_sequence && !s->frame_pred_frame_dct){
01564 av_log(s->avctx, AV_LOG_ERROR, "invalid frame_pred_frame_dct\n");
01565 s->frame_pred_frame_dct= 1;
01566 }
01567
01568 if(s->picture_structure == PICT_FRAME){
01569 s->first_field=0;
01570 s->v_edge_pos= 16*s->mb_height;
01571 }else{
01572 s->first_field ^= 1;
01573 s->v_edge_pos= 8*s->mb_height;
01574 memset(s->mbskip_table, 0, s->mb_stride*s->mb_height);
01575 }
01576
01577 if(s->alternate_scan){
01578 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_alternate_vertical_scan);
01579 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_alternate_vertical_scan);
01580 }else{
01581 ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable , ff_zigzag_direct);
01582 ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable , ff_zigzag_direct);
01583 }
01584
01585
01586 dprintf(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
01587 dprintf(s->avctx, "picture_structure=%d\n", s->picture_structure);
01588 dprintf(s->avctx, "top field first=%d\n", s->top_field_first);
01589 dprintf(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
01590 dprintf(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
01591 dprintf(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
01592 dprintf(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
01593 dprintf(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
01594 dprintf(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
01595 }
01596
01597 static void exchange_uv(MpegEncContext *s){
01598 DCTELEM (*tmp)[64];
01599
01600 tmp = s->pblocks[4];
01601 s->pblocks[4] = s->pblocks[5];
01602 s->pblocks[5] = tmp;
01603 }
01604
01605 static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size){
01606 AVCodecContext *avctx= s->avctx;
01607 Mpeg1Context *s1 = (Mpeg1Context*)s;
01608
01609
01610 if(s->first_field || s->picture_structure==PICT_FRAME){
01611 if(MPV_frame_start(s, avctx) < 0)
01612 return -1;
01613
01614 ff_er_frame_start(s);
01615
01616
01617 s->current_picture_ptr->repeat_pict = 0;
01618 if (s->repeat_first_field) {
01619 if (s->progressive_sequence) {
01620 if (s->top_field_first)
01621 s->current_picture_ptr->repeat_pict = 4;
01622 else
01623 s->current_picture_ptr->repeat_pict = 2;
01624 } else if (s->progressive_frame) {
01625 s->current_picture_ptr->repeat_pict = 1;
01626 }
01627 }
01628
01629 *s->current_picture_ptr->pan_scan= s1->pan_scan;
01630 }else{
01631 int i;
01632
01633 if(!s->current_picture_ptr){
01634 av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
01635 return -1;
01636 }
01637
01638 for(i=0; i<4; i++){
01639 s->current_picture.data[i] = s->current_picture_ptr->data[i];
01640 if(s->picture_structure == PICT_BOTTOM_FIELD){
01641 s->current_picture.data[i] += s->current_picture_ptr->linesize[i];
01642 }
01643 }
01644 }
01645
01646 if (avctx->hwaccel) {
01647 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
01648 return -1;
01649 }
01650
01651
01652
01653 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
01654 if(ff_xvmc_field_start(s,avctx) < 0)
01655 return -1;
01656
01657 return 0;
01658 }
01659
01660 #define DECODE_SLICE_ERROR -1
01661 #define DECODE_SLICE_OK 0
01662
01668 static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
01669 const uint8_t **buf, int buf_size)
01670 {
01671 MpegEncContext *s = &s1->mpeg_enc_ctx;
01672 AVCodecContext *avctx= s->avctx;
01673 const int field_pic= s->picture_structure != PICT_FRAME;
01674 const int lowres= s->avctx->lowres;
01675
01676 s->resync_mb_x=
01677 s->resync_mb_y= -1;
01678
01679 assert(mb_y < s->mb_height);
01680
01681 init_get_bits(&s->gb, *buf, buf_size*8);
01682
01683 ff_mpeg1_clean_buffers(s);
01684 s->interlaced_dct = 0;
01685
01686 s->qscale = get_qscale(s);
01687
01688 if(s->qscale == 0){
01689 av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
01690 return -1;
01691 }
01692
01693
01694 while (get_bits1(&s->gb) != 0) {
01695 skip_bits(&s->gb, 8);
01696 }
01697
01698 s->mb_x=0;
01699
01700 if(mb_y==0 && s->codec_tag == AV_RL32("SLIF")){
01701 skip_bits1(&s->gb);
01702 }else{
01703 for(;;) {
01704 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
01705 if (code < 0){
01706 av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
01707 return -1;
01708 }
01709 if (code >= 33) {
01710 if (code == 33) {
01711 s->mb_x += 33;
01712 }
01713
01714 } else {
01715 s->mb_x += code;
01716 break;
01717 }
01718 }
01719 }
01720
01721 if(s->mb_x >= (unsigned)s->mb_width){
01722 av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
01723 return -1;
01724 }
01725
01726 if (avctx->hwaccel) {
01727 const uint8_t *buf_end, *buf_start = *buf - 4;
01728 int start_code = -1;
01729 buf_end = ff_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
01730 if (buf_end < *buf + buf_size)
01731 buf_end -= 4;
01732 s->mb_y = mb_y;
01733 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
01734 return DECODE_SLICE_ERROR;
01735 *buf = buf_end;
01736 return DECODE_SLICE_OK;
01737 }
01738
01739 s->resync_mb_x= s->mb_x;
01740 s->resync_mb_y= s->mb_y= mb_y;
01741 s->mb_skip_run= 0;
01742 ff_init_block_index(s);
01743
01744 if (s->mb_y==0 && s->mb_x==0 && (s->first_field || s->picture_structure==PICT_FRAME)) {
01745 if(s->avctx->debug&FF_DEBUG_PICT_INFO){
01746 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",
01747 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],
01748 s->pict_type == FF_I_TYPE ? "I" : (s->pict_type == FF_P_TYPE ? "P" : (s->pict_type == FF_B_TYPE ? "B" : "S")),
01749 s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
01750 s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
01751 s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
01752 }
01753 }
01754
01755 for(;;) {
01756
01757 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
01758 ff_xvmc_init_block(s);
01759
01760 if(mpeg_decode_mb(s, s->block) < 0)
01761 return -1;
01762
01763 if(s->current_picture.motion_val[0] && !s->encoding){
01764 const int wrap = s->b8_stride;
01765 int xy = s->mb_x*2 + s->mb_y*2*wrap;
01766 int b8_xy= 4*(s->mb_x + s->mb_y*s->mb_stride);
01767 int motion_x, motion_y, dir, i;
01768
01769 for(i=0; i<2; i++){
01770 for(dir=0; dir<2; dir++){
01771 if (s->mb_intra || (dir==1 && s->pict_type != FF_B_TYPE)) {
01772 motion_x = motion_y = 0;
01773 }else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)){
01774 motion_x = s->mv[dir][0][0];
01775 motion_y = s->mv[dir][0][1];
01776 } else {
01777 motion_x = s->mv[dir][i][0];
01778 motion_y = s->mv[dir][i][1];
01779 }
01780
01781 s->current_picture.motion_val[dir][xy ][0] = motion_x;
01782 s->current_picture.motion_val[dir][xy ][1] = motion_y;
01783 s->current_picture.motion_val[dir][xy + 1][0] = motion_x;
01784 s->current_picture.motion_val[dir][xy + 1][1] = motion_y;
01785 s->current_picture.ref_index [dir][b8_xy ]=
01786 s->current_picture.ref_index [dir][b8_xy + 1]= s->field_select[dir][i];
01787 assert(s->field_select[dir][i]==0 || s->field_select[dir][i]==1);
01788 }
01789 xy += wrap;
01790 b8_xy +=2;
01791 }
01792 }
01793
01794 s->dest[0] += 16 >> lowres;
01795 s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
01796 s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
01797
01798 MPV_decode_mb(s, s->block);
01799
01800 if (++s->mb_x >= s->mb_width) {
01801 const int mb_size= 16>>s->avctx->lowres;
01802
01803 ff_draw_horiz_band(s, mb_size*(s->mb_y>>field_pic), mb_size);
01804
01805 s->mb_x = 0;
01806 s->mb_y += 1<<field_pic;
01807
01808 if(s->mb_y >= s->mb_height){
01809 int left= get_bits_left(&s->gb);
01810 int is_d10= s->chroma_format==2 && s->pict_type==FF_I_TYPE && avctx->profile==0 && avctx->level==5
01811 && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
01812 && s->progressive_frame == 0 ;
01813
01814 if(left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
01815 || (avctx->error_recognition >= FF_ER_AGGRESSIVE && left>8)){
01816 av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
01817 return -1;
01818 }else
01819 goto eos;
01820 }
01821
01822 ff_init_block_index(s);
01823 }
01824
01825
01826 if (s->mb_skip_run == -1) {
01827
01828 s->mb_skip_run = 0;
01829 for(;;) {
01830 int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
01831 if (code < 0){
01832 av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
01833 return -1;
01834 }
01835 if (code >= 33) {
01836 if (code == 33) {
01837 s->mb_skip_run += 33;
01838 }else if(code == 35){
01839 if(s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0){
01840 av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
01841 return -1;
01842 }
01843 goto eos;
01844 }
01845
01846 } else {
01847 s->mb_skip_run += code;
01848 break;
01849 }
01850 }
01851 if(s->mb_skip_run){
01852 int i;
01853 if(s->pict_type == FF_I_TYPE){
01854 av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
01855 return -1;
01856 }
01857
01858
01859 s->mb_intra = 0;
01860 for(i=0;i<12;i++)
01861 s->block_last_index[i] = -1;
01862 if(s->picture_structure == PICT_FRAME)
01863 s->mv_type = MV_TYPE_16X16;
01864 else
01865 s->mv_type = MV_TYPE_FIELD;
01866 if (s->pict_type == FF_P_TYPE) {
01867
01868 s->mv_dir = MV_DIR_FORWARD;
01869 s->mv[0][0][0] = s->mv[0][0][1] = 0;
01870 s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
01871 s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
01872 s->field_select[0][0]= (s->picture_structure - 1) & 1;
01873 } else {
01874
01875 s->mv[0][0][0] = s->last_mv[0][0][0];
01876 s->mv[0][0][1] = s->last_mv[0][0][1];
01877 s->mv[1][0][0] = s->last_mv[1][0][0];
01878 s->mv[1][0][1] = s->last_mv[1][0][1];
01879 }
01880 }
01881 }
01882 }
01883 eos:
01884 *buf += (get_bits_count(&s->gb)-1)/8;
01885
01886 return 0;
01887 }
01888
01889 static int slice_decode_thread(AVCodecContext *c, void *arg){
01890 MpegEncContext *s= *(void**)arg;
01891 const uint8_t *buf= s->gb.buffer;
01892 int mb_y= s->start_mb_y;
01893 const int field_pic= s->picture_structure != PICT_FRAME;
01894
01895 s->error_count= (3*(s->end_mb_y - s->start_mb_y)*s->mb_width) >> field_pic;
01896
01897 for(;;){
01898 uint32_t start_code;
01899 int ret;
01900
01901 ret= mpeg_decode_slice((Mpeg1Context*)s, mb_y, &buf, s->gb.buffer_end - buf);
01902 emms_c();
01903
01904
01905 if(ret < 0){
01906 if(s->resync_mb_x>=0 && s->resync_mb_y>=0)
01907 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);
01908 }else{
01909 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);
01910 }
01911
01912 if(s->mb_y == s->end_mb_y)
01913 return 0;
01914
01915 start_code= -1;
01916 buf = ff_find_start_code(buf, s->gb.buffer_end, &start_code);
01917 mb_y= start_code - SLICE_MIN_START_CODE;
01918 if(mb_y < 0 || mb_y >= s->end_mb_y)
01919 return -1;
01920 }
01921
01922 return 0;
01923 }
01924
01929 static int slice_end(AVCodecContext *avctx, AVFrame *pict)
01930 {
01931 Mpeg1Context *s1 = avctx->priv_data;
01932 MpegEncContext *s = &s1->mpeg_enc_ctx;
01933
01934 if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
01935 return 0;
01936
01937 if (s->avctx->hwaccel) {
01938 if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
01939 av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
01940 }
01941
01942 if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
01943 ff_xvmc_field_end(s);
01944
01945
01946 if ( !s->first_field) {
01947
01948
01949 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2;
01950
01951 ff_er_frame_end(s);
01952
01953 MPV_frame_end(s);
01954
01955 if (s->pict_type == FF_B_TYPE || s->low_delay) {
01956 *pict= *(AVFrame*)s->current_picture_ptr;
01957 ff_print_debug_info(s, pict);
01958 } else {
01959 s->picture_number++;
01960
01961
01962 if (s->last_picture_ptr != NULL) {
01963 *pict= *(AVFrame*)s->last_picture_ptr;
01964 ff_print_debug_info(s, pict);
01965 }
01966 }
01967
01968 return 1;
01969 } else {
01970 return 0;
01971 }
01972 }
01973
01974 static int mpeg1_decode_sequence(AVCodecContext *avctx,
01975 const uint8_t *buf, int buf_size)
01976 {
01977 Mpeg1Context *s1 = avctx->priv_data;
01978 MpegEncContext *s = &s1->mpeg_enc_ctx;
01979 int width,height;
01980 int i, v, j;
01981
01982 init_get_bits(&s->gb, buf, buf_size*8);
01983
01984 width = get_bits(&s->gb, 12);
01985 height = get_bits(&s->gb, 12);
01986 if (width <= 0 || height <= 0)
01987 return -1;
01988 s->aspect_ratio_info= get_bits(&s->gb, 4);
01989 if (s->aspect_ratio_info == 0) {
01990 av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
01991 if (avctx->error_recognition >= FF_ER_COMPLIANT)
01992 return -1;
01993 }
01994 s->frame_rate_index = get_bits(&s->gb, 4);
01995 if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
01996 return -1;
01997 s->bit_rate = get_bits(&s->gb, 18) * 400;
01998 if (get_bits1(&s->gb) == 0)
01999 return -1;
02000 s->width = width;
02001 s->height = height;
02002
02003 s->avctx->rc_buffer_size= get_bits(&s->gb, 10) * 1024*16;
02004 skip_bits(&s->gb, 1);
02005
02006
02007 if (get_bits1(&s->gb)) {
02008 load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
02009 } else {
02010 for(i=0;i<64;i++) {
02011 j = s->dsp.idct_permutation[i];
02012 v = ff_mpeg1_default_intra_matrix[i];
02013 s->intra_matrix[j] = v;
02014 s->chroma_intra_matrix[j] = v;
02015 }
02016 }
02017 if (get_bits1(&s->gb)) {
02018 load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
02019 } else {
02020 for(i=0;i<64;i++) {
02021 int j= s->dsp.idct_permutation[i];
02022 v = ff_mpeg1_default_non_intra_matrix[i];
02023 s->inter_matrix[j] = v;
02024 s->chroma_inter_matrix[j] = v;
02025 }
02026 }
02027
02028 if(show_bits(&s->gb, 23) != 0){
02029 av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
02030 return -1;
02031 }
02032
02033
02034 s->progressive_sequence = 1;
02035 s->progressive_frame = 1;
02036 s->picture_structure = PICT_FRAME;
02037 s->frame_pred_frame_dct = 1;
02038 s->chroma_format = 1;
02039 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO;
02040 avctx->sub_id = 1;
02041 s->out_format = FMT_MPEG1;
02042 s->swap_uv = 0;
02043 if(s->flags & CODEC_FLAG_LOW_DELAY) s->low_delay=1;
02044
02045 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
02046 av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
02047 s->avctx->rc_buffer_size, s->bit_rate);
02048
02049 return 0;
02050 }
02051
02052 static int vcr2_init_sequence(AVCodecContext *avctx)
02053 {
02054 Mpeg1Context *s1 = avctx->priv_data;
02055 MpegEncContext *s = &s1->mpeg_enc_ctx;
02056 int i, v;
02057
02058
02059 s->out_format = FMT_MPEG1;
02060 if (s1->mpeg_enc_ctx_allocated) {
02061 MPV_common_end(s);
02062 }
02063 s->width = avctx->coded_width;
02064 s->height = avctx->coded_height;
02065 avctx->has_b_frames= 0;
02066 s->low_delay= 1;
02067
02068 avctx->pix_fmt = mpeg_get_pixelformat(avctx);
02069 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
02070
02071 if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel ||
02072 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU )
02073 if( avctx->idct_algo == FF_IDCT_AUTO )
02074 avctx->idct_algo = FF_IDCT_SIMPLE;
02075
02076 if (MPV_common_init(s) < 0)
02077 return -1;
02078 exchange_uv(s);
02079 s->swap_uv = 1;
02080 s1->mpeg_enc_ctx_allocated = 1;
02081
02082 for(i=0;i<64;i++) {
02083 int j= s->dsp.idct_permutation[i];
02084 v = ff_mpeg1_default_intra_matrix[i];
02085 s->intra_matrix[j] = v;
02086 s->chroma_intra_matrix[j] = v;
02087
02088 v = ff_mpeg1_default_non_intra_matrix[i];
02089 s->inter_matrix[j] = v;
02090 s->chroma_inter_matrix[j] = v;
02091 }
02092
02093 s->progressive_sequence = 1;
02094 s->progressive_frame = 1;
02095 s->picture_structure = PICT_FRAME;
02096 s->frame_pred_frame_dct = 1;
02097 s->chroma_format = 1;
02098 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO;
02099 avctx->sub_id = 2;
02100 s1->save_width = s->width;
02101 s1->save_height = s->height;
02102 s1->save_progressive_seq = s->progressive_sequence;
02103 return 0;
02104 }
02105
02106
02107 static void mpeg_decode_user_data(AVCodecContext *avctx,
02108 const uint8_t *p, int buf_size)
02109 {
02110 const uint8_t *buf_end = p+buf_size;
02111
02112
02113 if (buf_end - p >= 5 &&
02114 p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
02115 int flags = p[4];
02116 p += 5;
02117 if (flags & 0x80) {
02118
02119 p += 2;
02120 }
02121 if (flags & 0x40) {
02122 if (buf_end - p < 1)
02123 return;
02124 avctx->dtg_active_format = p[0] & 0x0f;
02125 }
02126 }
02127 }
02128
02129 static void mpeg_decode_gop(AVCodecContext *avctx,
02130 const uint8_t *buf, int buf_size){
02131 Mpeg1Context *s1 = avctx->priv_data;
02132 MpegEncContext *s = &s1->mpeg_enc_ctx;
02133
02134 int drop_frame_flag;
02135 int time_code_hours, time_code_minutes;
02136 int time_code_seconds, time_code_pictures;
02137 int broken_link;
02138
02139 init_get_bits(&s->gb, buf, buf_size*8);
02140
02141 drop_frame_flag = get_bits1(&s->gb);
02142
02143 time_code_hours=get_bits(&s->gb,5);
02144 time_code_minutes = get_bits(&s->gb,6);
02145 skip_bits1(&s->gb);
02146 time_code_seconds = get_bits(&s->gb,6);
02147 time_code_pictures = get_bits(&s->gb,6);
02148
02149 s->closed_gop = get_bits1(&s->gb);
02150
02151
02152
02153 broken_link = get_bits1(&s->gb);
02154
02155 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
02156 av_log(s->avctx, AV_LOG_DEBUG, "GOP (%2d:%02d:%02d.[%02d]) closed_gop=%d broken_link=%d\n",
02157 time_code_hours, time_code_minutes, time_code_seconds,
02158 time_code_pictures, s->closed_gop, broken_link);
02159 }
02164 int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
02165 {
02166 int i;
02167 uint32_t state= pc->state;
02168
02169
02170 if (buf_size == 0)
02171 return 0;
02172
02173
02174
02175
02176
02177
02178
02179
02180
02181 for(i=0; i<buf_size; i++){
02182 assert(pc->frame_start_found>=0 && pc->frame_start_found<=4);
02183 if(pc->frame_start_found&1){
02184 if(state == EXT_START_CODE && (buf[i]&0xF0) != 0x80)
02185 pc->frame_start_found--;
02186 else if(state == EXT_START_CODE+2){
02187 if((buf[i]&3) == 3) pc->frame_start_found= 0;
02188 else pc->frame_start_found= (pc->frame_start_found+1)&3;
02189 }
02190 state++;
02191 }else{
02192 i= ff_find_start_code(buf+i, buf+buf_size, &state) - buf - 1;
02193 if(pc->frame_start_found==0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
02194 i++;
02195 pc->frame_start_found=4;
02196 }
02197 if(state == SEQ_END_CODE){
02198 pc->state=-1;
02199 return i+1;
02200 }
02201 if(pc->frame_start_found==2 && state == SEQ_START_CODE)
02202 pc->frame_start_found= 0;
02203 if(pc->frame_start_found<4 && state == EXT_START_CODE)
02204 pc->frame_start_found++;
02205 if(pc->frame_start_found == 4 && (state&0xFFFFFF00) == 0x100){
02206 if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
02207 pc->frame_start_found=0;
02208 pc->state=-1;
02209 return i-3;
02210 }
02211 }
02212 if(pc->frame_start_found == 0 && s && state == PICTURE_START_CODE){
02213 ff_fetch_timestamp(s, i-3, 1);
02214 }
02215 }
02216 }
02217 pc->state= state;
02218 return END_NOT_FOUND;
02219 }
02220
02221 static int decode_chunks(AVCodecContext *avctx,
02222 AVFrame *picture, int *data_size,
02223 const uint8_t *buf, int buf_size);
02224
02225
02226 static int mpeg_decode_frame(AVCodecContext *avctx,
02227 void *data, int *data_size,
02228 AVPacket *avpkt)
02229 {
02230 const uint8_t *buf = avpkt->data;
02231 int buf_size = avpkt->size;
02232 Mpeg1Context *s = avctx->priv_data;
02233 AVFrame *picture = data;
02234 MpegEncContext *s2 = &s->mpeg_enc_ctx;
02235 dprintf(avctx, "fill_buffer\n");
02236
02237 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
02238
02239 if (s2->low_delay==0 && s2->next_picture_ptr) {
02240 *picture= *(AVFrame*)s2->next_picture_ptr;
02241 s2->next_picture_ptr= NULL;
02242
02243 *data_size = sizeof(AVFrame);
02244 }
02245 return buf_size;
02246 }
02247
02248 if(s2->flags&CODEC_FLAG_TRUNCATED){
02249 int next= ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
02250
02251 if( ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
02252 return buf_size;
02253 }
02254
02255 #if 0
02256 if (s->repeat_field % 2 == 1) {
02257 s->repeat_field++;
02258
02259
02260 if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
02261 *data_size = sizeof(AVPicture);
02262 goto the_end;
02263 }
02264 }
02265 #endif
02266
02267 if(s->mpeg_enc_ctx_allocated==0 && avctx->codec_tag == AV_RL32("VCR2"))
02268 vcr2_init_sequence(avctx);
02269
02270 s->slice_count= 0;
02271
02272 if(avctx->extradata && !avctx->frame_number)
02273 decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
02274
02275 return decode_chunks(avctx, picture, data_size, buf, buf_size);
02276 }
02277
02278 static int decode_chunks(AVCodecContext *avctx,
02279 AVFrame *picture, int *data_size,
02280 const uint8_t *buf, int buf_size)
02281 {
02282 Mpeg1Context *s = avctx->priv_data;
02283 MpegEncContext *s2 = &s->mpeg_enc_ctx;
02284 const uint8_t *buf_ptr = buf;
02285 const uint8_t *buf_end = buf + buf_size;
02286 int ret, input_size;
02287 int last_code= 0;
02288
02289 for(;;) {
02290
02291 uint32_t start_code = -1;
02292 buf_ptr = ff_find_start_code(buf_ptr,buf_end, &start_code);
02293 if (start_code > 0x1ff){
02294 if(s2->pict_type != FF_B_TYPE || avctx->skip_frame <= AVDISCARD_DEFAULT){
02295 if(avctx->thread_count > 1){
02296 int i;
02297
02298 avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
02299 for(i=0; i<s->slice_count; i++)
02300 s2->error_count += s2->thread_context[i]->error_count;
02301 }
02302
02303 if (CONFIG_MPEG_VDPAU_DECODER && avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
02304 ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
02305
02306 if (slice_end(avctx, picture)) {
02307 if(s2->last_picture_ptr || s2->low_delay)
02308 *data_size = sizeof(AVPicture);
02309 }
02310 }
02311 s2->pict_type= 0;
02312 return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
02313 }
02314
02315 input_size = buf_end - buf_ptr;
02316
02317 if(avctx->debug & FF_DEBUG_STARTCODE){
02318 av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
02319 }
02320
02321
02322 switch(start_code) {
02323 case SEQ_START_CODE:
02324 if(last_code == 0){
02325 mpeg1_decode_sequence(avctx, buf_ptr,
02326 input_size);
02327 s->sync=1;
02328 }else{
02329 av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
02330 }
02331 break;
02332
02333 case PICTURE_START_CODE:
02334 if(last_code == 0 || last_code == SLICE_MIN_START_CODE){
02335 if(mpeg_decode_postinit(avctx) < 0){
02336 av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
02337 return -1;
02338 }
02339
02340
02341 if(mpeg1_decode_picture(avctx,
02342 buf_ptr, input_size) < 0)
02343 s2->pict_type=0;
02344 s2->first_slice = 1;
02345 last_code= PICTURE_START_CODE;
02346 }else{
02347 av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
02348 }
02349 break;
02350 case EXT_START_CODE:
02351 init_get_bits(&s2->gb, buf_ptr, input_size*8);
02352
02353 switch(get_bits(&s2->gb, 4)) {
02354 case 0x1:
02355 if(last_code == 0){
02356 mpeg_decode_sequence_extension(s);
02357 }else{
02358 av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
02359 }
02360 break;
02361 case 0x2:
02362 mpeg_decode_sequence_display_extension(s);
02363 break;
02364 case 0x3:
02365 mpeg_decode_quant_matrix_extension(s2);
02366 break;
02367 case 0x7:
02368 mpeg_decode_picture_display_extension(s);
02369 break;
02370 case 0x8:
02371 if(last_code == PICTURE_START_CODE){
02372 mpeg_decode_picture_coding_extension(s);
02373 }else{
02374 av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
02375 }
02376 break;
02377 }
02378 break;
02379 case USER_START_CODE:
02380 mpeg_decode_user_data(avctx,
02381 buf_ptr, input_size);
02382 break;
02383 case GOP_START_CODE:
02384 if(last_code == 0){
02385 s2->first_field=0;
02386 mpeg_decode_gop(avctx,
02387 buf_ptr, input_size);
02388 s->sync=1;
02389 }else{
02390 av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
02391 }
02392 break;
02393 default:
02394 if (start_code >= SLICE_MIN_START_CODE &&
02395 start_code <= SLICE_MAX_START_CODE && last_code!=0) {
02396 const int field_pic= s2->picture_structure != PICT_FRAME;
02397 int mb_y= (start_code - SLICE_MIN_START_CODE) << field_pic;
02398 last_code= SLICE_MIN_START_CODE;
02399
02400 if(s2->picture_structure == PICT_BOTTOM_FIELD)
02401 mb_y++;
02402
02403 if (mb_y >= s2->mb_height){
02404 av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
02405 return -1;
02406 }
02407
02408 if(s2->last_picture_ptr==NULL){
02409
02410 if(s2->pict_type==FF_B_TYPE){
02411 if(!s2->closed_gop)
02412 break;
02413 }
02414 }
02415 if(s2->pict_type==FF_I_TYPE)
02416 s->sync=1;
02417 if(s2->next_picture_ptr==NULL){
02418
02419 if(s2->pict_type==FF_P_TYPE && !s->sync) break;
02420 }
02421
02422 if(avctx->hurry_up && s2->pict_type==FF_B_TYPE) break;
02423 if( (avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type==FF_B_TYPE)
02424 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type!=FF_I_TYPE)
02425 || avctx->skip_frame >= AVDISCARD_ALL)
02426 break;
02427
02428 if(avctx->hurry_up>=5) break;
02429
02430 if (!s->mpeg_enc_ctx_allocated) break;
02431
02432 if(s2->codec_id == CODEC_ID_MPEG2VIDEO){
02433 if(mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
02434 break;
02435 }
02436
02437 if(!s2->pict_type){
02438 av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
02439 break;
02440 }
02441
02442 if(s2->first_slice){
02443 s2->first_slice=0;
02444 if(mpeg_field_start(s2, buf, buf_size) < 0)
02445 return -1;
02446 }
02447 if(!s2->current_picture_ptr){
02448 av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
02449 return -1;
02450 }
02451
02452 if (avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
02453 s->slice_count++;
02454 break;
02455 }
02456
02457 if(avctx->thread_count > 1){
02458 int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
02459 if(threshold <= mb_y){
02460 MpegEncContext *thread_context= s2->thread_context[s->slice_count];
02461
02462 thread_context->start_mb_y= mb_y;
02463 thread_context->end_mb_y = s2->mb_height;
02464 if(s->slice_count){
02465 s2->thread_context[s->slice_count-1]->end_mb_y= mb_y;
02466 ff_update_duplicate_context(thread_context, s2);
02467 }
02468 init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
02469 s->slice_count++;
02470 }
02471 buf_ptr += 2;
02472 }else{
02473 ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
02474 emms_c();
02475
02476 if(ret < 0){
02477 if(s2->resync_mb_x>=0 && s2->resync_mb_y>=0)
02478 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);
02479 }else{
02480 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);
02481 }
02482 }
02483 }
02484 break;
02485 }
02486 }
02487 }
02488
02489 static void flush(AVCodecContext *avctx){
02490 Mpeg1Context *s = avctx->priv_data;
02491
02492 s->sync=0;
02493
02494 ff_mpeg_flush(avctx);
02495 }
02496
02497 static int mpeg_decode_end(AVCodecContext *avctx)
02498 {
02499 Mpeg1Context *s = avctx->priv_data;
02500
02501 if (s->mpeg_enc_ctx_allocated)
02502 MPV_common_end(&s->mpeg_enc_ctx);
02503 return 0;
02504 }
02505
02506 AVCodec mpeg1video_decoder = {
02507 "mpeg1video",
02508 AVMEDIA_TYPE_VIDEO,
02509 CODEC_ID_MPEG1VIDEO,
02510 sizeof(Mpeg1Context),
02511 mpeg_decode_init,
02512 NULL,
02513 mpeg_decode_end,
02514 mpeg_decode_frame,
02515 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
02516 .flush= flush,
02517 .max_lowres= 3,
02518 .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
02519 };
02520
02521 AVCodec mpeg2video_decoder = {
02522 "mpeg2video",
02523 AVMEDIA_TYPE_VIDEO,
02524 CODEC_ID_MPEG2VIDEO,
02525 sizeof(Mpeg1Context),
02526 mpeg_decode_init,
02527 NULL,
02528 mpeg_decode_end,
02529 mpeg_decode_frame,
02530 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
02531 .flush= flush,
02532 .max_lowres= 3,
02533 .long_name= NULL_IF_CONFIG_SMALL("MPEG-2 video"),
02534 };
02535
02536
02537 AVCodec mpegvideo_decoder = {
02538 "mpegvideo",
02539 AVMEDIA_TYPE_VIDEO,
02540 CODEC_ID_MPEG2VIDEO,
02541 sizeof(Mpeg1Context),
02542 mpeg_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_DELAY,
02547 .flush= flush,
02548 .max_lowres= 3,
02549 .long_name= NULL_IF_CONFIG_SMALL("MPEG-1 video"),
02550 };
02551
02552 #if CONFIG_MPEG_XVMC_DECODER
02553 static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx){
02554 if( avctx->thread_count > 1)
02555 return -1;
02556 if( !(avctx->slice_flags & SLICE_FLAG_CODED_ORDER) )
02557 return -1;
02558 if( !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD) ){
02559 dprintf(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
02560 }
02561 mpeg_decode_init(avctx);
02562
02563 avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
02564 avctx->xvmc_acceleration = 2;
02565
02566 return 0;
02567 }
02568
02569 AVCodec mpeg_xvmc_decoder = {
02570 "mpegvideo_xvmc",
02571 AVMEDIA_TYPE_VIDEO,
02572 CODEC_ID_MPEG2VIDEO_XVMC,
02573 sizeof(Mpeg1Context),
02574 mpeg_mc_decode_init,
02575 NULL,
02576 mpeg_decode_end,
02577 mpeg_decode_frame,
02578 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
02579 .flush= flush,
02580 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
02581 };
02582
02583 #endif
02584
02585 #if CONFIG_MPEG_VDPAU_DECODER
02586 AVCodec mpeg_vdpau_decoder = {
02587 "mpegvideo_vdpau",
02588 AVMEDIA_TYPE_VIDEO,
02589 CODEC_ID_MPEG2VIDEO,
02590 sizeof(Mpeg1Context),
02591 mpeg_decode_init,
02592 NULL,
02593 mpeg_decode_end,
02594 mpeg_decode_frame,
02595 CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
02596 .flush= flush,
02597 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
02598 };
02599 #endif
02600
02601 #if CONFIG_MPEG1_VDPAU_DECODER
02602 AVCodec mpeg1_vdpau_decoder = {
02603 "mpeg1video_vdpau",
02604 AVMEDIA_TYPE_VIDEO,
02605 CODEC_ID_MPEG1VIDEO,
02606 sizeof(Mpeg1Context),
02607 mpeg_decode_init,
02608 NULL,
02609 mpeg_decode_end,
02610 mpeg_decode_frame,
02611 CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
02612 .flush= flush,
02613 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
02614 };
02615 #endif
02616