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