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