• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/cavsdec.c

Go to the documentation of this file.
00001 /*
00002  * Chinese AVS video (AVS1-P2, JiZhun profile) decoder.
00003  * Copyright (c) 2006  Stefan Gehrer <stefan.gehrer@gmx.de>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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  * motion vector prediction
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     /* scale the co-located motion vector according to its temporal span */
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     /* backward mv is the scaled and negated forward mv */
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  * residual data decoding
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) //end of block signal
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     /* get coded block pattern */
00167     int cbp= get_ue_golomb(&h->s.gb);
00168     if(cbp > 63){
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     /* get quantizer */
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  * macroblock level
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     /* get intra prediction modes from stream */
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) // if either is not available
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     /* get coded block pattern */
00226     if(h->pic_type == FF_I_TYPE)
00227         cbp_code = get_ue_golomb(gb);
00228     if(cbp_code > 63){
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; //qp_delta
00235 
00236     /* luma intra prediction interleaved with residual decode/transform/add */
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     /* chroma intra prediction */
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     /* reset all MVs */
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             /* intra MB at co-location, do in-plane prediction */
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             /* direct prediction from co-located P MB, block-wise */
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                     /* intra MB at co-location, do in-plane prediction */
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) { /* 16x8 macroblock types */
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 {          /* 8x16 macroblock types */
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  * slice level
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     /* mark top macroblocks as unavailable */
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     /* inter frame or second slice can have weighting params */
00430     if((h->pic_type != FF_I_TYPE) || (!h->pic_structure && h->mby >= h->mb_width/2))
00431         if(get_bits1(gb)) { //slice_weighting_flag
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     /* check for stuffing byte */
00446     if(!align && (show_bits(gb,8) == 0x80))
00447         align = 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  * frame level
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);//bbv_dwlay
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         /* make sure we have the reference frames we need */
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);//time_code
00491         /* old sample clips were all progressive and no low_delay,
00492            bump stream revision if detected otherwise */
00493         if((s->low_delay) || !(show_bits(&s->gb,9) & 1))
00494             h->stream_revision = 1;
00495         /* similarly test top_field_first and repeat_first_field */
00496         else if(show_bits(&s->gb,11) & 3)
00497             h->stream_revision = 1;
00498         if(h->stream_revision > 0)
00499             skip_bits(&s->gb,1); //marker_bit
00500     }
00501     /* release last B frame */
00502     if(h->picture.data[0])
00503         s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture);
00504 
00505     s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture);
00506     ff_cavs_init_pic(h);
00507     h->picture.poc = get_bits(&s->gb,8)*2;
00508 
00509     /* get temporal distances and MV scaling factors */
00510     if(h->pic_type != FF_B_TYPE) {
00511         h->dist[0] = (h->picture.poc - h->DPB[0].poc  + 512) % 512;
00512     } else {
00513         h->dist[0] = (h->DPB[0].poc  - h->picture.poc + 512) % 512;
00514     }
00515     h->dist[1] = (h->picture.poc - h->DPB[1].poc  + 512) % 512;
00516     h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
00517     h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
00518     if(h->pic_type == FF_B_TYPE) {
00519         h->sym_factor = h->dist[0]*h->scale_den[1];
00520     } else {
00521         h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
00522         h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
00523     }
00524 
00525     if(s->low_delay)
00526         get_ue_golomb(&s->gb); //bbv_check_times
00527     h->progressive             = get_bits1(&s->gb);
00528     h->pic_structure = 1;
00529     if(!h->progressive)
00530         h->pic_structure = get_bits1(&s->gb);
00531     if(!h->pic_structure && h->stc == PIC_PB_START_CODE)
00532         skip_bits1(&s->gb);     //advanced_pred_mode_disable
00533     skip_bits1(&s->gb);        //top_field_first
00534     skip_bits1(&s->gb);        //repeat_first_field
00535     h->qp_fixed                = get_bits1(&s->gb);
00536     h->qp                      = get_bits(&s->gb,6);
00537     if(h->pic_type == FF_I_TYPE) {
00538         if(!h->progressive && !h->pic_structure)
00539             skip_bits1(&s->gb);//what is this?
00540         skip_bits(&s->gb,4);   //reserved bits
00541     } else {
00542         if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1))
00543             h->ref_flag        = get_bits1(&s->gb);
00544         skip_bits(&s->gb,4);   //reserved bits
00545         h->skip_mode_flag      = get_bits1(&s->gb);
00546     }
00547     h->loop_filter_disable     = get_bits1(&s->gb);
00548     if(!h->loop_filter_disable && get_bits1(&s->gb)) {
00549         h->alpha_offset        = get_se_golomb(&s->gb);
00550         h->beta_offset         = get_se_golomb(&s->gb);
00551     } else {
00552         h->alpha_offset = h->beta_offset  = 0;
00553     }
00554     if(h->pic_type == FF_I_TYPE) {
00555         do {
00556             check_for_slice(h);
00557             decode_mb_i(h, 0);
00558         } while(ff_cavs_next_mb(h));
00559     } else if(h->pic_type == FF_P_TYPE) {
00560         do {
00561             if(check_for_slice(h))
00562                 skip_count = -1;
00563             if(h->skip_mode_flag && (skip_count < 0))
00564                 skip_count = get_ue_golomb(&s->gb);
00565             if(h->skip_mode_flag && skip_count--) {
00566                 decode_mb_p(h,P_SKIP);
00567             } else {
00568                 mb_type = get_ue_golomb(&s->gb) + P_SKIP + h->skip_mode_flag;
00569                 if(mb_type > P_8X8)
00570                     decode_mb_i(h, mb_type - P_8X8 - 1);
00571                 else
00572                     decode_mb_p(h,mb_type);
00573             }
00574         } while(ff_cavs_next_mb(h));
00575     } else { /* FF_B_TYPE */
00576         do {
00577             if(check_for_slice(h))
00578                 skip_count = -1;
00579             if(h->skip_mode_flag && (skip_count < 0))
00580                 skip_count = get_ue_golomb(&s->gb);
00581             if(h->skip_mode_flag && skip_count--) {
00582                 decode_mb_b(h,B_SKIP);
00583             } else {
00584                 mb_type = get_ue_golomb(&s->gb) + B_SKIP + h->skip_mode_flag;
00585                 if(mb_type > B_8X8)
00586                     decode_mb_i(h, mb_type - B_8X8 - 1);
00587                 else
00588                     decode_mb_b(h,mb_type);
00589             }
00590         } while(ff_cavs_next_mb(h));
00591     }
00592     if(h->pic_type != FF_B_TYPE) {
00593         if(h->DPB[1].data[0])
00594             s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]);
00595         h->DPB[1] = h->DPB[0];
00596         h->DPB[0] = h->picture;
00597         memset(&h->picture,0,sizeof(Picture));
00598     }
00599     return 0;
00600 }
00601 
00602 /*****************************************************************************
00603  *
00604  * headers and interface
00605  *
00606  ****************************************************************************/
00607 
00608 static int decode_seq_header(AVSContext *h) {
00609     MpegEncContext *s = &h->s;
00610     int frame_rate_code;
00611 
00612     h->profile =         get_bits(&s->gb,8);
00613     h->level =           get_bits(&s->gb,8);
00614     skip_bits1(&s->gb); //progressive sequence
00615     s->width =           get_bits(&s->gb,14);
00616     s->height =          get_bits(&s->gb,14);
00617     skip_bits(&s->gb,2); //chroma format
00618     skip_bits(&s->gb,3); //sample_precision
00619     h->aspect_ratio =    get_bits(&s->gb,4);
00620     frame_rate_code =    get_bits(&s->gb,4);
00621     skip_bits(&s->gb,18);//bit_rate_lower
00622     skip_bits1(&s->gb);  //marker_bit
00623     skip_bits(&s->gb,12);//bit_rate_upper
00624     s->low_delay =       get_bits1(&s->gb);
00625     h->mb_width  = (s->width  + 15) >> 4;
00626     h->mb_height = (s->height + 15) >> 4;
00627     h->s.avctx->time_base.den = ff_frame_rate_tab[frame_rate_code].num;
00628     h->s.avctx->time_base.num = ff_frame_rate_tab[frame_rate_code].den;
00629     h->s.avctx->width  = s->width;
00630     h->s.avctx->height = s->height;
00631     if(!h->top_qp)
00632         ff_cavs_init_top_lines(h);
00633     return 0;
00634 }
00635 
00636 static void cavs_flush(AVCodecContext * avctx) {
00637     AVSContext *h = avctx->priv_data;
00638     h->got_keyframe = 0;
00639 }
00640 
00641 static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
00642                              AVPacket *avpkt) {
00643     const uint8_t *buf = avpkt->data;
00644     int buf_size = avpkt->size;
00645     AVSContext *h = avctx->priv_data;
00646     MpegEncContext *s = &h->s;
00647     int input_size;
00648     const uint8_t *buf_end;
00649     const uint8_t *buf_ptr;
00650     AVFrame *picture = data;
00651     uint32_t stc = -1;
00652 
00653     s->avctx = avctx;
00654 
00655     if (buf_size == 0) {
00656         if(!s->low_delay && h->DPB[0].data[0]) {
00657             *data_size = sizeof(AVPicture);
00658             *picture = *(AVFrame *) &h->DPB[0];
00659         }
00660         return 0;
00661     }
00662 
00663     buf_ptr = buf;
00664     buf_end = buf + buf_size;
00665     for(;;) {
00666         buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc);
00667         if((stc & 0xFFFFFE00) || buf_ptr == buf_end)
00668             return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
00669         input_size = (buf_end - buf_ptr)*8;
00670         switch(stc) {
00671         case CAVS_START_CODE:
00672             init_get_bits(&s->gb, buf_ptr, input_size);
00673             decode_seq_header(h);
00674             break;
00675         case PIC_I_START_CODE:
00676             if(!h->got_keyframe) {
00677                 if(h->DPB[0].data[0])
00678                     avctx->release_buffer(avctx, (AVFrame *)&h->DPB[0]);
00679                 if(h->DPB[1].data[0])
00680                     avctx->release_buffer(avctx, (AVFrame *)&h->DPB[1]);
00681                 h->got_keyframe = 1;
00682             }
00683         case PIC_PB_START_CODE:
00684             *data_size = 0;
00685             if(!h->got_keyframe)
00686                 break;
00687             init_get_bits(&s->gb, buf_ptr, input_size);
00688             h->stc = stc;
00689             if(decode_pic(h))
00690                 break;
00691             *data_size = sizeof(AVPicture);
00692             if(h->pic_type != FF_B_TYPE) {
00693                 if(h->DPB[1].data[0]) {
00694                     *picture = *(AVFrame *) &h->DPB[1];
00695                 } else {
00696                     *data_size = 0;
00697                 }
00698             } else
00699                 *picture = *(AVFrame *) &h->picture;
00700             break;
00701         case EXT_START_CODE:
00702             //mpeg_decode_extension(avctx,buf_ptr, input_size);
00703             break;
00704         case USER_START_CODE:
00705             //mpeg_decode_user_data(avctx,buf_ptr, input_size);
00706             break;
00707         default:
00708             if (stc <= SLICE_MAX_START_CODE) {
00709                 init_get_bits(&s->gb, buf_ptr, input_size);
00710                 decode_slice_header(h, &s->gb);
00711             }
00712             break;
00713         }
00714     }
00715 }
00716 
00717 AVCodec cavs_decoder = {
00718     "cavs",
00719     AVMEDIA_TYPE_VIDEO,
00720     CODEC_ID_CAVS,
00721     sizeof(AVSContext),
00722     ff_cavs_init,
00723     NULL,
00724     ff_cavs_end,
00725     cavs_decode_frame,
00726     CODEC_CAP_DR1 | CODEC_CAP_DELAY,
00727     .flush= cavs_flush,
00728     .long_name= NULL_IF_CONFIG_SMALL("Chinese AVS video (AVS1-P2, JiZhun profile)"),
00729 };

Generated on Fri Sep 16 2011 17:17:34 for FFmpeg by  doxygen 1.7.1