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
00422 if (h->stc >= h->mb_height)
00423 return -1;
00424
00425 h->mby = h->stc;
00426 h->mbidx = h->mby*h->mb_width;
00427
00428
00429 h->flags &= ~(B_AVAIL|C_AVAIL);
00430 if((h->mby == 0) && (!h->qp_fixed)){
00431 h->qp_fixed = get_bits1(gb);
00432 h->qp = get_bits(gb,6);
00433 }
00434
00435 if((h->pic_type != AV_PICTURE_TYPE_I) || (!h->pic_structure && h->mby >= h->mb_width/2))
00436 if(get_bits1(gb)) {
00437 av_log(h->s.avctx, AV_LOG_ERROR,
00438 "weighted prediction not yet supported\n");
00439 }
00440 return 0;
00441 }
00442
00443 static inline int check_for_slice(AVSContext *h) {
00444 GetBitContext *gb = &h->s.gb;
00445 int align;
00446
00447 if(h->mbx)
00448 return 0;
00449 align = (-get_bits_count(gb)) & 7;
00450
00451 if(!align && (show_bits(gb,8) == 0x80))
00452 align = 8;
00453 if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
00454 skip_bits_long(gb,24+align);
00455 h->stc = get_bits(gb,8);
00456 if (h->stc >= h->mb_height)
00457 return 0;
00458 decode_slice_header(h,gb);
00459 return 1;
00460 }
00461 return 0;
00462 }
00463
00464
00465
00466
00467
00468
00469
00470 static int decode_pic(AVSContext *h) {
00471 MpegEncContext *s = &h->s;
00472 int skip_count = -1;
00473 enum cavs_mb mb_type;
00474
00475 if (!s->context_initialized) {
00476 s->avctx->idct_algo = FF_IDCT_CAVS;
00477 if (ff_MPV_common_init(s) < 0)
00478 return -1;
00479 ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
00480 }
00481 skip_bits(&s->gb,16);
00482 if(h->stc == PIC_PB_START_CODE) {
00483 h->pic_type = get_bits(&s->gb,2) + AV_PICTURE_TYPE_I;
00484 if(h->pic_type > AV_PICTURE_TYPE_B) {
00485 av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
00486 return -1;
00487 }
00488
00489 if(!h->DPB[0].f.data[0] ||
00490 (!h->DPB[1].f.data[0] && h->pic_type == AV_PICTURE_TYPE_B))
00491 return -1;
00492 } else {
00493 h->pic_type = AV_PICTURE_TYPE_I;
00494 if(get_bits1(&s->gb))
00495 skip_bits(&s->gb,24);
00496
00497
00498 if (s->low_delay || !(show_bits(&s->gb,9) & 1))
00499 h->stream_revision = 1;
00500
00501 else if(show_bits(&s->gb,11) & 3)
00502 h->stream_revision = 1;
00503 if(h->stream_revision > 0)
00504 skip_bits(&s->gb,1);
00505 }
00506
00507 if(h->picture.f.data[0])
00508 s->avctx->release_buffer(s->avctx, &h->picture.f);
00509
00510 s->avctx->get_buffer(s->avctx, &h->picture.f);
00511 ff_cavs_init_pic(h);
00512 h->picture.poc = get_bits(&s->gb,8)*2;
00513
00514
00515 if(h->pic_type != AV_PICTURE_TYPE_B) {
00516 h->dist[0] = (h->picture.poc - h->DPB[0].poc + 512) % 512;
00517 } else {
00518 h->dist[0] = (h->DPB[0].poc - h->picture.poc + 512) % 512;
00519 }
00520 h->dist[1] = (h->picture.poc - h->DPB[1].poc + 512) % 512;
00521 h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
00522 h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
00523 if(h->pic_type == AV_PICTURE_TYPE_B) {
00524 h->sym_factor = h->dist[0]*h->scale_den[1];
00525 } else {
00526 h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
00527 h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
00528 }
00529
00530 if(s->low_delay)
00531 get_ue_golomb(&s->gb);
00532 h->progressive = get_bits1(&s->gb);
00533 h->pic_structure = 1;
00534 if(!h->progressive)
00535 h->pic_structure = get_bits1(&s->gb);
00536 if(!h->pic_structure && h->stc == PIC_PB_START_CODE)
00537 skip_bits1(&s->gb);
00538 skip_bits1(&s->gb);
00539 skip_bits1(&s->gb);
00540 h->qp_fixed = get_bits1(&s->gb);
00541 h->qp = get_bits(&s->gb,6);
00542 if(h->pic_type == AV_PICTURE_TYPE_I) {
00543 if(!h->progressive && !h->pic_structure)
00544 skip_bits1(&s->gb);
00545 skip_bits(&s->gb,4);
00546 } else {
00547 if(!(h->pic_type == AV_PICTURE_TYPE_B && h->pic_structure == 1))
00548 h->ref_flag = get_bits1(&s->gb);
00549 skip_bits(&s->gb,4);
00550 h->skip_mode_flag = get_bits1(&s->gb);
00551 }
00552 h->loop_filter_disable = get_bits1(&s->gb);
00553 if(!h->loop_filter_disable && get_bits1(&s->gb)) {
00554 h->alpha_offset = get_se_golomb(&s->gb);
00555 h->beta_offset = get_se_golomb(&s->gb);
00556 } else {
00557 h->alpha_offset = h->beta_offset = 0;
00558 }
00559 if(h->pic_type == AV_PICTURE_TYPE_I) {
00560 do {
00561 check_for_slice(h);
00562 decode_mb_i(h, 0);
00563 } while(ff_cavs_next_mb(h));
00564 } else if(h->pic_type == AV_PICTURE_TYPE_P) {
00565 do {
00566 if(check_for_slice(h))
00567 skip_count = -1;
00568 if(h->skip_mode_flag && (skip_count < 0))
00569 skip_count = get_ue_golomb(&s->gb);
00570 if(h->skip_mode_flag && skip_count--) {
00571 decode_mb_p(h,P_SKIP);
00572 } else {
00573 mb_type = get_ue_golomb(&s->gb) + P_SKIP + h->skip_mode_flag;
00574 if(mb_type > P_8X8)
00575 decode_mb_i(h, mb_type - P_8X8 - 1);
00576 else
00577 decode_mb_p(h,mb_type);
00578 }
00579 } while(ff_cavs_next_mb(h));
00580 } else {
00581 do {
00582 if(check_for_slice(h))
00583 skip_count = -1;
00584 if(h->skip_mode_flag && (skip_count < 0))
00585 skip_count = get_ue_golomb(&s->gb);
00586 if(h->skip_mode_flag && skip_count--) {
00587 decode_mb_b(h,B_SKIP);
00588 } else {
00589 mb_type = get_ue_golomb(&s->gb) + B_SKIP + h->skip_mode_flag;
00590 if(mb_type > B_8X8)
00591 decode_mb_i(h, mb_type - B_8X8 - 1);
00592 else
00593 decode_mb_b(h,mb_type);
00594 }
00595 } while(ff_cavs_next_mb(h));
00596 }
00597 if(h->pic_type != AV_PICTURE_TYPE_B) {
00598 if(h->DPB[1].f.data[0])
00599 s->avctx->release_buffer(s->avctx, &h->DPB[1].f);
00600 h->DPB[1] = h->DPB[0];
00601 h->DPB[0] = h->picture;
00602 memset(&h->picture,0,sizeof(Picture));
00603 }
00604 return 0;
00605 }
00606
00607
00608
00609
00610
00611
00612
00613 static int decode_seq_header(AVSContext *h) {
00614 MpegEncContext *s = &h->s;
00615 int frame_rate_code;
00616 int width, height;
00617
00618 h->profile = get_bits(&s->gb,8);
00619 h->level = get_bits(&s->gb,8);
00620 skip_bits1(&s->gb);
00621 width = get_bits(&s->gb,14);
00622 height = get_bits(&s->gb,14);
00623 if ((s->width || s->height) && (s->width != width || s->height != height)) {
00624 av_log_missing_feature(s, "Width/height changing in CAVS is", 0);
00625 return -1;
00626 }
00627 s->width = width;
00628 s->height = height;
00629 skip_bits(&s->gb,2);
00630 skip_bits(&s->gb,3);
00631 h->aspect_ratio = get_bits(&s->gb,4);
00632 frame_rate_code = get_bits(&s->gb,4);
00633 skip_bits(&s->gb,18);
00634 skip_bits1(&s->gb);
00635 skip_bits(&s->gb,12);
00636 s->low_delay = get_bits1(&s->gb);
00637 h->mb_width = (s->width + 15) >> 4;
00638 h->mb_height = (s->height + 15) >> 4;
00639 h->s.avctx->time_base.den = avpriv_frame_rate_tab[frame_rate_code].num;
00640 h->s.avctx->time_base.num = avpriv_frame_rate_tab[frame_rate_code].den;
00641 h->s.avctx->width = s->width;
00642 h->s.avctx->height = s->height;
00643 if(!h->top_qp)
00644 ff_cavs_init_top_lines(h);
00645 return 0;
00646 }
00647
00648 static void cavs_flush(AVCodecContext * avctx) {
00649 AVSContext *h = avctx->priv_data;
00650 h->got_keyframe = 0;
00651 }
00652
00653 static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
00654 AVPacket *avpkt) {
00655 const uint8_t *buf = avpkt->data;
00656 int buf_size = avpkt->size;
00657 AVSContext *h = avctx->priv_data;
00658 MpegEncContext *s = &h->s;
00659 int input_size;
00660 const uint8_t *buf_end;
00661 const uint8_t *buf_ptr;
00662 AVFrame *picture = data;
00663 uint32_t stc = -1;
00664
00665 s->avctx = avctx;
00666
00667 if (buf_size == 0) {
00668 if (!s->low_delay && h->DPB[0].f.data[0]) {
00669 *data_size = sizeof(AVPicture);
00670 *picture = h->DPB[0].f;
00671 memset(&h->DPB[0], 0, sizeof(h->DPB[0]));
00672 }
00673 return 0;
00674 }
00675
00676 buf_ptr = buf;
00677 buf_end = buf + buf_size;
00678 for(;;) {
00679 buf_ptr = avpriv_mpv_find_start_code(buf_ptr,buf_end, &stc);
00680 if((stc & 0xFFFFFE00) || buf_ptr == buf_end)
00681 return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
00682 input_size = (buf_end - buf_ptr)*8;
00683 switch(stc) {
00684 case CAVS_START_CODE:
00685 init_get_bits(&s->gb, buf_ptr, input_size);
00686 decode_seq_header(h);
00687 break;
00688 case PIC_I_START_CODE:
00689 if(!h->got_keyframe) {
00690 if(h->DPB[0].f.data[0])
00691 avctx->release_buffer(avctx, &h->DPB[0].f);
00692 if(h->DPB[1].f.data[0])
00693 avctx->release_buffer(avctx, &h->DPB[1].f);
00694 h->got_keyframe = 1;
00695 }
00696 case PIC_PB_START_CODE:
00697 *data_size = 0;
00698 if(!h->got_keyframe)
00699 break;
00700 if(!h->top_qp)
00701 break;
00702 init_get_bits(&s->gb, buf_ptr, input_size);
00703 h->stc = stc;
00704 if(decode_pic(h))
00705 break;
00706 *data_size = sizeof(AVPicture);
00707 if(h->pic_type != AV_PICTURE_TYPE_B) {
00708 if(h->DPB[1].f.data[0]) {
00709 *picture = h->DPB[1].f;
00710 } else {
00711 *data_size = 0;
00712 }
00713 } else
00714 *picture = h->picture.f;
00715 break;
00716 case EXT_START_CODE:
00717
00718 break;
00719 case USER_START_CODE:
00720
00721 break;
00722 default:
00723 if (stc <= SLICE_MAX_START_CODE) {
00724 init_get_bits(&s->gb, buf_ptr, input_size);
00725 decode_slice_header(h, &s->gb);
00726 }
00727 break;
00728 }
00729 }
00730 }
00731
00732 AVCodec ff_cavs_decoder = {
00733 .name = "cavs",
00734 .type = AVMEDIA_TYPE_VIDEO,
00735 .id = CODEC_ID_CAVS,
00736 .priv_data_size = sizeof(AVSContext),
00737 .init = ff_cavs_init,
00738 .close = ff_cavs_end,
00739 .decode = cavs_decode_frame,
00740 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
00741 .flush = cavs_flush,
00742 .long_name = NULL_IF_CONFIG_SMALL("Chinese AVS video (AVS1-P2, JiZhun profile)"),
00743 };