00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "avcodec.h"
00029 #include "bitstream.h"
00030 #include "golomb.h"
00031 #include "cavs.h"
00032
00033 static const uint8_t mv_scan[4] = {
00034 MV_FWD_X0,MV_FWD_X1,
00035 MV_FWD_X2,MV_FWD_X3
00036 };
00037
00038 static const uint8_t cbp_tab[64][2] = {
00039 {63, 0},{15,15},{31,63},{47,31},{ 0,16},{14,32},{13,47},{11,13},
00040 { 7,14},{ 5,11},{10,12},{ 8, 5},{12,10},{61, 7},{ 4,48},{55, 3},
00041 { 1, 2},{ 2, 8},{59, 4},{ 3, 1},{62,61},{ 9,55},{ 6,59},{29,62},
00042 {45,29},{51,27},{23,23},{39,19},{27,30},{46,28},{53, 9},{30, 6},
00043 {43,60},{37,21},{60,44},{16,26},{21,51},{28,35},{19,18},{35,20},
00044 {42,24},{26,53},{44,17},{32,37},{58,39},{24,45},{20,58},{17,43},
00045 {18,42},{48,46},{22,36},{33,33},{25,34},{49,40},{40,52},{36,49},
00046 {34,50},{50,56},{52,25},{54,22},{41,54},{56,57},{38,41},{57,38}
00047 };
00048
00049
00050
00051
00052
00053
00054
00055 static inline void store_mvs(AVSContext *h) {
00056 h->col_mv[h->mbidx*4 + 0] = h->mv[MV_FWD_X0];
00057 h->col_mv[h->mbidx*4 + 1] = h->mv[MV_FWD_X1];
00058 h->col_mv[h->mbidx*4 + 2] = h->mv[MV_FWD_X2];
00059 h->col_mv[h->mbidx*4 + 3] = h->mv[MV_FWD_X3];
00060 }
00061
00062 static inline void mv_pred_direct(AVSContext *h, cavs_vector *pmv_fw,
00063 cavs_vector *col_mv) {
00064 cavs_vector *pmv_bw = pmv_fw + MV_BWD_OFFS;
00065 int den = h->direct_den[col_mv->ref];
00066 int m = col_mv->x >> 31;
00067
00068 pmv_fw->dist = h->dist[1];
00069 pmv_bw->dist = h->dist[0];
00070 pmv_fw->ref = 1;
00071 pmv_bw->ref = 0;
00072
00073 pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m;
00074 pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m);
00075 m = col_mv->y >> 31;
00076 pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m;
00077 pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m);
00078 }
00079
00080 static inline void mv_pred_sym(AVSContext *h, cavs_vector *src, enum cavs_block size) {
00081 cavs_vector *dst = src + MV_BWD_OFFS;
00082
00083
00084 dst->x = -((src->x * h->sym_factor + 256) >> 9);
00085 dst->y = -((src->y * h->sym_factor + 256) >> 9);
00086 dst->ref = 0;
00087 dst->dist = h->dist[0];
00088 set_mvs(dst, size);
00089 }
00090
00091
00092
00093
00094
00095
00096
00098 static inline int get_ue_code(GetBitContext *gb, int order) {
00099 if(order) {
00100 int ret = get_ue_golomb(gb) << order;
00101 return ret + get_bits(gb,order);
00102 }
00103 return get_ue_golomb(gb);
00104 }
00105
00115 static int decode_residual_block(AVSContext *h, GetBitContext *gb,
00116 const struct dec_2dvlc *r, int esc_golomb_order,
00117 int qp, uint8_t *dst, int stride) {
00118 int i, level_code, esc_code, level, run, mask;
00119 DCTELEM level_buf[65];
00120 uint8_t run_buf[65];
00121 DCTELEM *block = h->block;
00122
00123 for(i=0;i<65;i++) {
00124 level_code = get_ue_code(gb,r->golomb_order);
00125 if(level_code >= ESCAPE_CODE) {
00126 run = ((level_code - ESCAPE_CODE) >> 1) + 1;
00127 esc_code = get_ue_code(gb,esc_golomb_order);
00128 level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
00129 while(level > r->inc_limit)
00130 r++;
00131 mask = -(level_code & 1);
00132 level = (level^mask) - mask;
00133 } else if (level_code >= 0) {
00134 level = r->rltab[level_code][0];
00135 if(!level)
00136 break;
00137 run = r->rltab[level_code][1];
00138 r += r->rltab[level_code][2];
00139 } else {
00140 break;
00141 }
00142 level_buf[i] = level;
00143 run_buf[i] = run;
00144 }
00145 if(dequant(h,level_buf, run_buf, block, ff_cavs_dequant_mul[qp],
00146 ff_cavs_dequant_shift[qp], i))
00147 return -1;
00148 h->s.dsp.cavs_idct8_add(dst,block,stride);
00149 h->s.dsp.clear_block(block);
00150 return 0;
00151 }
00152
00153
00154 static inline void decode_residual_chroma(AVSContext *h) {
00155 if(h->cbp & (1<<4))
00156 decode_residual_block(h,&h->s.gb,ff_cavs_chroma_dec,0,
00157 ff_cavs_chroma_qp[h->qp],h->cu,h->c_stride);
00158 if(h->cbp & (1<<5))
00159 decode_residual_block(h,&h->s.gb,ff_cavs_chroma_dec,0,
00160 ff_cavs_chroma_qp[h->qp],h->cv,h->c_stride);
00161 }
00162
00163 static inline int decode_residual_inter(AVSContext *h) {
00164 int block;
00165
00166
00167 int cbp= get_ue_golomb(&h->s.gb);
00168 if(cbp > 63U){
00169 av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n");
00170 return -1;
00171 }
00172 h->cbp = cbp_tab[cbp][1];
00173
00174
00175 if(h->cbp && !h->qp_fixed)
00176 h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;
00177 for(block=0;block<4;block++)
00178 if(h->cbp & (1<<block))
00179 decode_residual_block(h,&h->s.gb,ff_cavs_inter_dec,0,h->qp,
00180 h->cy + h->luma_scan[block], h->l_stride);
00181 decode_residual_chroma(h);
00182
00183 return 0;
00184 }
00185
00186
00187
00188
00189
00190
00191
00192 static int decode_mb_i(AVSContext *h, int cbp_code) {
00193 GetBitContext *gb = &h->s.gb;
00194 unsigned pred_mode_uv;
00195 int block;
00196 uint8_t top[18];
00197 uint8_t *left = NULL;
00198 uint8_t *d;
00199
00200 ff_cavs_init_mb(h);
00201
00202
00203 for(block=0;block<4;block++) {
00204 int nA,nB,predpred;
00205 int pos = ff_cavs_scan3x3[block];
00206
00207 nA = h->pred_mode_Y[pos-1];
00208 nB = h->pred_mode_Y[pos-3];
00209 predpred = FFMIN(nA,nB);
00210 if(predpred == NOT_AVAIL)
00211 predpred = INTRA_L_LP;
00212 if(!get_bits1(gb)){
00213 int rem_mode= get_bits(gb, 2);
00214 predpred = rem_mode + (rem_mode >= predpred);
00215 }
00216 h->pred_mode_Y[pos] = predpred;
00217 }
00218 pred_mode_uv = get_ue_golomb(gb);
00219 if(pred_mode_uv > 6) {
00220 av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
00221 return -1;
00222 }
00223 ff_cavs_modify_mb_i(h, &pred_mode_uv);
00224
00225
00226 if(h->pic_type == FF_I_TYPE)
00227 cbp_code = get_ue_golomb(gb);
00228 if(cbp_code > 63U){
00229 av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n");
00230 return -1;
00231 }
00232 h->cbp = cbp_tab[cbp_code][0];
00233 if(h->cbp && !h->qp_fixed)
00234 h->qp = (h->qp + get_se_golomb(gb)) & 63;
00235
00236
00237 for(block=0;block<4;block++) {
00238 d = h->cy + h->luma_scan[block];
00239 ff_cavs_load_intra_pred_luma(h, top, &left, block);
00240 h->intra_pred_l[h->pred_mode_Y[ff_cavs_scan3x3[block]]]
00241 (d, top, left, h->l_stride);
00242 if(h->cbp & (1<<block))
00243 decode_residual_block(h,gb,ff_cavs_intra_dec,1,h->qp,d,h->l_stride);
00244 }
00245
00246
00247 ff_cavs_load_intra_pred_chroma(h);
00248 h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10],
00249 h->left_border_u, h->c_stride);
00250 h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10],
00251 h->left_border_v, h->c_stride);
00252
00253 decode_residual_chroma(h);
00254 ff_cavs_filter(h,I_8X8);
00255 set_mv_intra(h);
00256 return 0;
00257 }
00258
00259 static void decode_mb_p(AVSContext *h, enum cavs_mb mb_type) {
00260 GetBitContext *gb = &h->s.gb;
00261 int ref[4];
00262
00263 ff_cavs_init_mb(h);
00264 switch(mb_type) {
00265 case P_SKIP:
00266 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
00267 break;
00268 case P_16X16:
00269 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
00270 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16,ref[0]);
00271 break;
00272 case P_16X8:
00273 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
00274 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
00275 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, ref[0]);
00276 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, ref[2]);
00277 break;
00278 case P_8X16:
00279 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
00280 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
00281 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, ref[0]);
00282 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, ref[1]);
00283 break;
00284 case P_8X8:
00285 ref[0] = h->ref_flag ? 0 : get_bits1(gb);
00286 ref[1] = h->ref_flag ? 0 : get_bits1(gb);
00287 ref[2] = h->ref_flag ? 0 : get_bits1(gb);
00288 ref[3] = h->ref_flag ? 0 : get_bits1(gb);
00289 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN, BLK_8X8, ref[0]);
00290 ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN, BLK_8X8, ref[1]);
00291 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN, BLK_8X8, ref[2]);
00292 ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN, BLK_8X8, ref[3]);
00293 }
00294 ff_cavs_inter(h, mb_type);
00295 set_intra_mode_default(h);
00296 store_mvs(h);
00297 if(mb_type != P_SKIP)
00298 decode_residual_inter(h);
00299 ff_cavs_filter(h,mb_type);
00300 h->col_type_base[h->mbidx] = mb_type;
00301 }
00302
00303 static void decode_mb_b(AVSContext *h, enum cavs_mb mb_type) {
00304 int block;
00305 enum cavs_sub_mb sub_type[4];
00306 int flags;
00307
00308 ff_cavs_init_mb(h);
00309
00310
00311 h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
00312 set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
00313 h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
00314 set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
00315 switch(mb_type) {
00316 case B_SKIP:
00317 case B_DIRECT:
00318 if(!h->col_type_base[h->mbidx]) {
00319
00320 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
00321 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
00322 } else
00323
00324 for(block=0;block<4;block++)
00325 mv_pred_direct(h,&h->mv[mv_scan[block]],
00326 &h->col_mv[h->mbidx*4 + block]);
00327 break;
00328 case B_FWD_16X16:
00329 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
00330 break;
00331 case B_SYM_16X16:
00332 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
00333 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
00334 break;
00335 case B_BWD_16X16:
00336 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
00337 break;
00338 case B_8X8:
00339 for(block=0;block<4;block++)
00340 sub_type[block] = get_bits(&h->s.gb,2);
00341 for(block=0;block<4;block++) {
00342 switch(sub_type[block]) {
00343 case B_SUB_DIRECT:
00344 if(!h->col_type_base[h->mbidx]) {
00345
00346 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
00347 MV_PRED_BSKIP, BLK_8X8, 1);
00348 ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
00349 mv_scan[block]-3+MV_BWD_OFFS,
00350 MV_PRED_BSKIP, BLK_8X8, 0);
00351 } else
00352 mv_pred_direct(h,&h->mv[mv_scan[block]],
00353 &h->col_mv[h->mbidx*4 + block]);
00354 break;
00355 case B_SUB_FWD:
00356 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
00357 MV_PRED_MEDIAN, BLK_8X8, 1);
00358 break;
00359 case B_SUB_SYM:
00360 ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
00361 MV_PRED_MEDIAN, BLK_8X8, 1);
00362 mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
00363 break;
00364 }
00365 }
00366 for(block=0;block<4;block++) {
00367 if(sub_type[block] == B_SUB_BWD)
00368 ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
00369 mv_scan[block]+MV_BWD_OFFS-3,
00370 MV_PRED_MEDIAN, BLK_8X8, 0);
00371 }
00372 break;
00373 default:
00374 assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
00375 flags = ff_cavs_partition_flags[mb_type];
00376 if(mb_type & 1) {
00377 if(flags & FWD0)
00378 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP, BLK_16X8, 1);
00379 if(flags & SYM0)
00380 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
00381 if(flags & FWD1)
00382 ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
00383 if(flags & SYM1)
00384 mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
00385 if(flags & BWD0)
00386 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP, BLK_16X8, 0);
00387 if(flags & BWD1)
00388 ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
00389 } else {
00390 if(flags & FWD0)
00391 ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
00392 if(flags & SYM0)
00393 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
00394 if(flags & FWD1)
00395 ff_cavs_mv(h,MV_FWD_X1,MV_FWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,1);
00396 if(flags & SYM1)
00397 mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
00398 if(flags & BWD0)
00399 ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
00400 if(flags & BWD1)
00401 ff_cavs_mv(h,MV_BWD_X1,MV_BWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,0);
00402 }
00403 }
00404 ff_cavs_inter(h, mb_type);
00405 set_intra_mode_default(h);
00406 if(mb_type != B_SKIP)
00407 decode_residual_inter(h);
00408 ff_cavs_filter(h,mb_type);
00409 }
00410
00411
00412
00413
00414
00415
00416
00417 static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
00418 if(h->stc > 0xAF)
00419 av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
00420 h->mby = h->stc;
00421 h->mbidx = h->mby*h->mb_width;
00422
00423
00424 h->flags &= ~(B_AVAIL|C_AVAIL);
00425 if((h->mby == 0) && (!h->qp_fixed)){
00426 h->qp_fixed = get_bits1(gb);
00427 h->qp = get_bits(gb,6);
00428 }
00429
00430 if((h->pic_type != FF_I_TYPE) || (!h->pic_structure && h->mby >= h->mb_width/2))
00431 if(get_bits1(gb)) {
00432 av_log(h->s.avctx, AV_LOG_ERROR,
00433 "weighted prediction not yet supported\n");
00434 }
00435 return 0;
00436 }
00437
00438 static inline int check_for_slice(AVSContext *h) {
00439 GetBitContext *gb = &h->s.gb;
00440 int align;
00441
00442 if(h->mbx)
00443 return 0;
00444 align = (-get_bits_count(gb)) & 7;
00445
00446 if(!align && (show_bits(gb,8) == 0x80))
00447 get_bits(gb,8);
00448 if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
00449 skip_bits_long(gb,24+align);
00450 h->stc = get_bits(gb,8);
00451 if (h->stc >= h->mb_height)
00452 return 0;
00453 decode_slice_header(h,gb);
00454 return 1;
00455 }
00456 return 0;
00457 }
00458
00459
00460
00461
00462
00463
00464
00465 static int decode_pic(AVSContext *h) {
00466 MpegEncContext *s = &h->s;
00467 int skip_count = -1;
00468 enum cavs_mb mb_type;
00469
00470 if (!s->context_initialized) {
00471 s->avctx->idct_algo = FF_IDCT_CAVS;
00472 if (MPV_common_init(s) < 0)
00473 return -1;
00474 ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
00475 }
00476 skip_bits(&s->gb,16);
00477 if(h->stc == PIC_PB_START_CODE) {
00478 h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE;
00479 if(h->pic_type > FF_B_TYPE) {
00480 av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
00481 return -1;
00482 }
00483
00484 if(!h->DPB[0].data[0] ||
00485 (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE))
00486 return -1;
00487 } else {
00488 h->pic_type = FF_I_TYPE;
00489 if(get_bits1(&s->gb))
00490 skip_bits(&s->gb,24);
00491 }
00492
00493 if(h->picture.data[0])
00494 s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture);
00495
00496 s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture);
00497 ff_cavs_init_pic(h);
00498 h->picture.poc = get_bits(&s->gb,8)*2;
00499
00500
00501 if(h->pic_type != FF_B_TYPE) {
00502 h->dist[0] = (h->picture.poc - h->DPB[0].poc + 512) % 512;
00503 } else {
00504 h->dist[0] = (h->DPB[0].poc - h->picture.poc + 512) % 512;
00505 }
00506 h->dist[1] = (h->picture.poc - h->DPB[1].poc + 512) % 512;
00507 h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
00508 h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
00509 if(h->pic_type == FF_B_TYPE) {
00510 h->sym_factor = h->dist[0]*h->scale_den[1];
00511 } else {
00512 h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
00513 h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
00514 }
00515
00516 if(s->low_delay)
00517 get_ue_golomb(&s->gb);
00518 h->progressive = get_bits1(&s->gb);
00519 h->pic_structure = 1;
00520 if(!h->progressive)
00521 h->pic_structure = get_bits1(&s->gb);
00522 if(!h->pic_structure && h->stc == PIC_PB_START_CODE)
00523 skip_bits1(&s->gb);
00524 skip_bits1(&s->gb);
00525 skip_bits1(&s->gb);
00526 h->qp_fixed = get_bits1(&s->gb);
00527 h->qp = get_bits(&s->gb,6);
00528 if(h->pic_type == FF_I_TYPE) {
00529 if(!h->progressive && !h->pic_structure)
00530 skip_bits1(&s->gb);
00531 skip_bits(&s->gb,4);
00532 } else {
00533 if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1))
00534 h->ref_flag = get_bits1(&s->gb);
00535 skip_bits(&s->gb,4);
00536 h->skip_mode_flag = get_bits1(&s->gb);
00537 }
00538 h->loop_filter_disable = get_bits1(&s->gb);
00539 if(!h->loop_filter_disable && get_bits1(&s->gb)) {
00540 h->alpha_offset = get_se_golomb(&s->gb);
00541 h->beta_offset = get_se_golomb(&s->gb);
00542 } else {
00543 h->alpha_offset = h->beta_offset = 0;
00544 }
00545 if(h->pic_type == FF_I_TYPE) {
00546 do {
00547 check_for_slice(h);
00548 decode_mb_i(h, 0);
00549 } while(ff_cavs_next_mb(h));
00550 } else if(h->pic_type == FF_P_TYPE) {
00551 do {
00552 if(check_for_slice(h))
00553 skip_count = -1;
00554 if(h->skip_mode_flag && (skip_count < 0))
00555 skip_count = get_ue_golomb(&s->gb);
00556 if(h->skip_mode_flag && skip_count--) {
00557 decode_mb_p(h,P_SKIP);
00558 } else {
00559 mb_type = get_ue_golomb(&s->gb) + P_SKIP + h->skip_mode_flag;
00560 if(mb_type > P_8X8)
00561 decode_mb_i(h, mb_type - P_8X8 - 1);
00562 else
00563 decode_mb_p(h,mb_type);
00564 }
00565 } while(ff_cavs_next_mb(h));
00566 } else {
00567 do {
00568 if(check_for_slice(h))
00569 skip_count = -1;
00570 if(h->skip_mode_flag && (skip_count < 0))
00571 skip_count = get_ue_golomb(&s->gb);
00572 if(h->skip_mode_flag && skip_count--) {
00573 decode_mb_b(h,B_SKIP);
00574 } else {
00575 mb_type = get_ue_golomb(&s->gb) + B_SKIP + h->skip_mode_flag;
00576 if(mb_type > B_8X8)
00577 decode_mb_i(h, mb_type - B_8X8 - 1);
00578 else
00579 decode_mb_b(h,mb_type);
00580 }
00581 } while(ff_cavs_next_mb(h));
00582 }
00583 if(h->pic_type != FF_B_TYPE) {
00584 if(h->DPB[1].data[0])
00585 s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]);
00586 h->DPB[1] = h->DPB[0];
00587 h->DPB[0] = h->picture;
00588 memset(&h->picture,0,sizeof(Picture));
00589 }
00590 return 0;
00591 }
00592
00593
00594
00595
00596
00597
00598
00599 static int decode_seq_header(AVSContext *h) {
00600 MpegEncContext *s = &h->s;
00601 int frame_rate_code;
00602
00603 h->profile = get_bits(&s->gb,8);
00604 h->level = get_bits(&s->gb,8);
00605 skip_bits1(&s->gb);
00606 s->width = get_bits(&s->gb,14);
00607 s->height = get_bits(&s->gb,14);
00608 skip_bits(&s->gb,2);
00609 skip_bits(&s->gb,3);
00610 h->aspect_ratio = get_bits(&s->gb,4);
00611 frame_rate_code = get_bits(&s->gb,4);
00612 skip_bits(&s->gb,18);
00613 skip_bits1(&s->gb);
00614 skip_bits(&s->gb,12);
00615 s->low_delay = get_bits1(&s->gb);
00616 h->mb_width = (s->width + 15) >> 4;
00617 h->mb_height = (s->height + 15) >> 4;
00618 h->s.avctx->time_base.den = ff_frame_rate_tab[frame_rate_code].num;
00619 h->s.avctx->time_base.num = ff_frame_rate_tab[frame_rate_code].den;
00620 h->s.avctx->width = s->width;
00621 h->s.avctx->height = s->height;
00622 if(!h->top_qp)
00623 ff_cavs_init_top_lines(h);
00624 return 0;
00625 }
00626
00627 static void cavs_flush(AVCodecContext * avctx) {
00628 AVSContext *h = avctx->priv_data;
00629 h->got_keyframe = 0;
00630 }
00631
00632 static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
00633 const uint8_t * buf, int buf_size) {
00634 AVSContext *h = avctx->priv_data;
00635 MpegEncContext *s = &h->s;
00636 int input_size;
00637 const uint8_t *buf_end;
00638 const uint8_t *buf_ptr;
00639 AVFrame *picture = data;
00640 uint32_t stc = -1;
00641
00642 s->avctx = avctx;
00643
00644 if (buf_size == 0) {
00645 if(!s->low_delay && h->DPB[0].data[0]) {
00646 *data_size = sizeof(AVPicture);
00647 *picture = *(AVFrame *) &h->DPB[0];
00648 }
00649 return 0;
00650 }
00651
00652 buf_ptr = buf;
00653 buf_end = buf + buf_size;
00654 for(;;) {
00655 buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc);
00656 if((stc & 0xFFFFFE00) || buf_ptr == buf_end)
00657 return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
00658 input_size = (buf_end - buf_ptr)*8;
00659 switch(stc) {
00660 case CAVS_START_CODE:
00661 init_get_bits(&s->gb, buf_ptr, input_size);
00662 decode_seq_header(h);
00663 break;
00664 case PIC_I_START_CODE:
00665 if(!h->got_keyframe) {
00666 if(h->DPB[0].data[0])
00667 avctx->release_buffer(avctx, (AVFrame *)&h->DPB[0]);
00668 if(h->DPB[1].data[0])
00669 avctx->release_buffer(avctx, (AVFrame *)&h->DPB[1]);
00670 h->got_keyframe = 1;
00671 }
00672 case PIC_PB_START_CODE:
00673 *data_size = 0;
00674 if(!h->got_keyframe)
00675 break;
00676 init_get_bits(&s->gb, buf_ptr, input_size);
00677 h->stc = stc;
00678 if(decode_pic(h))
00679 break;
00680 *data_size = sizeof(AVPicture);
00681 if(h->pic_type != FF_B_TYPE) {
00682 if(h->DPB[1].data[0]) {
00683 *picture = *(AVFrame *) &h->DPB[1];
00684 } else {
00685 *data_size = 0;
00686 }
00687 } else
00688 *picture = *(AVFrame *) &h->picture;
00689 break;
00690 case EXT_START_CODE:
00691
00692 break;
00693 case USER_START_CODE:
00694
00695 break;
00696 default:
00697 if (stc <= SLICE_MAX_START_CODE) {
00698 init_get_bits(&s->gb, buf_ptr, input_size);
00699 decode_slice_header(h, &s->gb);
00700 }
00701 break;
00702 }
00703 }
00704 }
00705
00706 AVCodec cavs_decoder = {
00707 "cavs",
00708 CODEC_TYPE_VIDEO,
00709 CODEC_ID_CAVS,
00710 sizeof(AVSContext),
00711 ff_cavs_init,
00712 NULL,
00713 ff_cavs_end,
00714 cavs_decode_frame,
00715 CODEC_CAP_DR1 | CODEC_CAP_DELAY,
00716 .flush= cavs_flush,
00717 .long_name= NULL_IF_CONFIG_SMALL("Chinese AVS video (AVS1-P2, JiZhun profile)"),
00718 };