Libav

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 {
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         }
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     /* get coded block pattern */
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     /* get quantizer */
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  * macroblock level
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     /* get intra prediction modes from stream */
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) // if either is not available
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     /* get coded block pattern */
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; //qp_delta
00232 
00233     /* luma intra prediction interleaved with residual decode/transform/add */
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     /* chroma intra prediction */
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     /* reset all MVs */
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             /* intra MB at co-location, do in-plane prediction */
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             /* direct prediction from co-located P MB, block-wise */
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                     /* intra MB at co-location, do in-plane prediction */
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) { /* 16x8 macroblock types */
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 {          /* 8x16 macroblock types */
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  * slice level
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     /* mark top macroblocks as unavailable */
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     /* inter frame or second slice can have weighting params */
00427     if((h->pic_type != FF_I_TYPE) || (!h->pic_structure && h->mby >= h->mb_width/2))
00428         if(get_bits1(gb)) { //slice_weighting_flag
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     /* check for stuffing byte */
00443     if(!align && (show_bits(gb,8) == 0x80))
00444         align = 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  * frame level
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);//bbv_dwlay
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         /* make sure we have the reference frames we need */
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);//time_code
00486         /* old sample clips were all progressive and no low_delay,
00487            bump stream revision if detected otherwise */
00488         if((s->low_delay) || !(show_bits(&s->gb,9) & 1))
00489             h->stream_revision = 1;
00490         /* similarly test top_field_first and repeat_first_field */
00491         else if(show_bits(&s->gb,11) & 3)
00492             h->stream_revision = 1;
00493         if(h->stream_revision > 0)
00494             skip_bits(&s->gb,1); //marker_bit
00495     }
00496     /* release last B frame */
00497     if(h->picture.data[0])
00498         s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture);
00499 
00500     s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture);
00501     ff_cavs_init_pic(h);
00502     h->picture.poc = get_bits(&s->gb,8)*2;
00503 
00504     /* get temporal distances and MV scaling factors */
00505     if(h->pic_type != FF_B_TYPE) {
00506         h->dist[0] = (h->picture.poc - h->DPB[0].poc  + 512) % 512;
00507     } else {
00508         h->dist[0] = (h->DPB[0].poc  - h->picture.poc + 512) % 512;
00509     }
00510     h->dist[1] = (h->picture.poc - h->DPB[1].poc  + 512) % 512;
00511     h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
00512     h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
00513     if(h->pic_type == FF_B_TYPE) {
00514         h->sym_factor = h->dist[0]*h->scale_den[1];
00515     } else {
00516         h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
00517         h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
00518     }
00519 
00520     if(s->low_delay)
00521         get_ue_golomb(&s->gb); //bbv_check_times
00522     h->progressive             = get_bits1(&s->gb);
00523     h->pic_structure = 1;
00524     if(!h->progressive)
00525         h->pic_structure = get_bits1(&s->gb);
00526     if(!h->pic_structure && h->stc == PIC_PB_START_CODE)
00527         skip_bits1(&s->gb);     //advanced_pred_mode_disable
00528     skip_bits1(&s->gb);        //top_field_first
00529     skip_bits1(&s->gb);        //repeat_first_field
00530     h->qp_fixed                = get_bits1(&s->gb);
00531     h->qp                      = get_bits(&s->gb,6);
00532     if(h->pic_type == FF_I_TYPE) {
00533         if(!h->progressive && !h->pic_structure)
00534             skip_bits1(&s->gb);//what is this?
00535         skip_bits(&s->gb,4);   //reserved bits
00536     } else {
00537         if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1))
00538             h->ref_flag        = get_bits1(&s->gb);
00539         skip_bits(&s->gb,4);   //reserved bits
00540         h->skip_mode_flag      = get_bits1(&s->gb);
00541     }
00542     h->loop_filter_disable     = get_bits1(&s->gb);
00543     if(!h->loop_filter_disable && get_bits1(&s->gb)) {
00544         h->alpha_offset        = get_se_golomb(&s->gb);
00545         h->beta_offset         = get_se_golomb(&s->gb);
00546     } else {
00547         h->alpha_offset = h->beta_offset  = 0;
00548     }
00549     if(h->pic_type == FF_I_TYPE) {
00550         do {
00551             check_for_slice(h);
00552             decode_mb_i(h, 0);
00553         } while(ff_cavs_next_mb(h));
00554     } else if(h->pic_type == FF_P_TYPE) {
00555         do {
00556             if(check_for_slice(h))
00557                 skip_count = -1;
00558             if(h->skip_mode_flag && (skip_count < 0))
00559                 skip_count = get_ue_golomb(&s->gb);
00560             if(h->skip_mode_flag && skip_count--) {
00561                 decode_mb_p(h,P_SKIP);
00562             } else {
00563                 mb_type = get_ue_golomb(&s->gb) + P_SKIP + h->skip_mode_flag;
00564                 if(mb_type > P_8X8)
00565                     decode_mb_i(h, mb_type - P_8X8 - 1);
00566                 else
00567                     decode_mb_p(h,mb_type);
00568             }
00569         } while(ff_cavs_next_mb(h));
00570     } else { /* FF_B_TYPE */
00571         do {
00572             if(check_for_slice(h))
00573                 skip_count = -1;
00574             if(h->skip_mode_flag && (skip_count < 0))
00575                 skip_count = get_ue_golomb(&s->gb);
00576             if(h->skip_mode_flag && skip_count--) {
00577                 decode_mb_b(h,B_SKIP);
00578             } else {
00579                 mb_type = get_ue_golomb(&s->gb) + B_SKIP + h->skip_mode_flag;
00580                 if(mb_type > B_8X8)
00581                     decode_mb_i(h, mb_type - B_8X8 - 1);
00582                 else
00583                     decode_mb_b(h,mb_type);
00584             }
00585         } while(ff_cavs_next_mb(h));
00586     }
00587     if(h->pic_type != FF_B_TYPE) {
00588         if(h->DPB[1].data[0])
00589             s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]);
00590         h->DPB[1] = h->DPB[0];
00591         h->DPB[0] = h->picture;
00592         memset(&h->picture,0,sizeof(Picture));
00593     }
00594     return 0;
00595 }
00596 
00597 /*****************************************************************************
00598  *
00599  * headers and interface
00600  *
00601  ****************************************************************************/
00602 
00603 static int decode_seq_header(AVSContext *h) {
00604     MpegEncContext *s = &h->s;
00605     int frame_rate_code;
00606 
00607     h->profile =         get_bits(&s->gb,8);
00608     h->level =           get_bits(&s->gb,8);
00609     skip_bits1(&s->gb); //progressive sequence
00610     s->width =           get_bits(&s->gb,14);
00611     s->height =          get_bits(&s->gb,14);
00612     skip_bits(&s->gb,2); //chroma format
00613     skip_bits(&s->gb,3); //sample_precision
00614     h->aspect_ratio =    get_bits(&s->gb,4);
00615     frame_rate_code =    get_bits(&s->gb,4);
00616     skip_bits(&s->gb,18);//bit_rate_lower
00617     skip_bits1(&s->gb);  //marker_bit
00618     skip_bits(&s->gb,12);//bit_rate_upper
00619     s->low_delay =       get_bits1(&s->gb);
00620     h->mb_width  = (s->width  + 15) >> 4;
00621     h->mb_height = (s->height + 15) >> 4;
00622     h->s.avctx->time_base.den = ff_frame_rate_tab[frame_rate_code].num;
00623     h->s.avctx->time_base.num = ff_frame_rate_tab[frame_rate_code].den;
00624     h->s.avctx->width  = s->width;
00625     h->s.avctx->height = s->height;
00626     if(!h->top_qp)
00627         ff_cavs_init_top_lines(h);
00628     return 0;
00629 }
00630 
00631 static void cavs_flush(AVCodecContext * avctx) {
00632     AVSContext *h = avctx->priv_data;
00633     h->got_keyframe = 0;
00634 }
00635 
00636 static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
00637                              AVPacket *avpkt) {
00638     const uint8_t *buf = avpkt->data;
00639     int buf_size = avpkt->size;
00640     AVSContext *h = avctx->priv_data;
00641     MpegEncContext *s = &h->s;
00642     int input_size;
00643     const uint8_t *buf_end;
00644     const uint8_t *buf_ptr;
00645     AVFrame *picture = data;
00646     uint32_t stc = -1;
00647 
00648     s->avctx = avctx;
00649 
00650     if (buf_size == 0) {
00651         if(!s->low_delay && h->DPB[0].data[0]) {
00652             *data_size = sizeof(AVPicture);
00653             *picture = *(AVFrame *) &h->DPB[0];
00654         }
00655         return 0;
00656     }
00657 
00658     buf_ptr = buf;
00659     buf_end = buf + buf_size;
00660     for(;;) {
00661         buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc);
00662         if(stc & 0xFFFFFE00)
00663             return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
00664         input_size = (buf_end - buf_ptr)*8;
00665         switch(stc) {
00666         case CAVS_START_CODE:
00667             init_get_bits(&s->gb, buf_ptr, input_size);
00668             decode_seq_header(h);
00669             break;
00670         case PIC_I_START_CODE:
00671             if(!h->got_keyframe) {
00672                 if(h->DPB[0].data[0])
00673                     avctx->release_buffer(avctx, (AVFrame *)&h->DPB[0]);
00674                 if(h->DPB[1].data[0])
00675                     avctx->release_buffer(avctx, (AVFrame *)&h->DPB[1]);
00676                 h->got_keyframe = 1;
00677             }
00678         case PIC_PB_START_CODE:
00679             *data_size = 0;
00680             if(!h->got_keyframe)
00681                 break;
00682             init_get_bits(&s->gb, buf_ptr, input_size);
00683             h->stc = stc;
00684             if(decode_pic(h))
00685                 break;
00686             *data_size = sizeof(AVPicture);
00687             if(h->pic_type != FF_B_TYPE) {
00688                 if(h->DPB[1].data[0]) {
00689                     *picture = *(AVFrame *) &h->DPB[1];
00690                 } else {
00691                     *data_size = 0;
00692                 }
00693             } else
00694                 *picture = *(AVFrame *) &h->picture;
00695             break;
00696         case EXT_START_CODE:
00697             //mpeg_decode_extension(avctx,buf_ptr, input_size);
00698             break;
00699         case USER_START_CODE:
00700             //mpeg_decode_user_data(avctx,buf_ptr, input_size);
00701             break;
00702         default:
00703             if (stc <= SLICE_MAX_START_CODE) {
00704                 init_get_bits(&s->gb, buf_ptr, input_size);
00705                 decode_slice_header(h, &s->gb);
00706             }
00707             break;
00708         }
00709     }
00710 }
00711 
00712 AVCodec cavs_decoder = {
00713     "cavs",
00714     AVMEDIA_TYPE_VIDEO,
00715     CODEC_ID_CAVS,
00716     sizeof(AVSContext),
00717     ff_cavs_init,
00718     NULL,
00719     ff_cavs_end,
00720     cavs_decode_frame,
00721     CODEC_CAP_DR1 | CODEC_CAP_DELAY,
00722     .flush= cavs_flush,
00723     .long_name= NULL_IF_CONFIG_SMALL("Chinese AVS video (AVS1-P2, JiZhun profile)"),
00724 };