Libav 0.7.1
libavcodec/vp56.h
Go to the documentation of this file.
00001 
00024 #ifndef AVCODEC_VP56_H
00025 #define AVCODEC_VP56_H
00026 
00027 #include "vp56data.h"
00028 #include "dsputil.h"
00029 #include "get_bits.h"
00030 #include "bytestream.h"
00031 #include "cabac.h"
00032 #include "vp56dsp.h"
00033 
00034 typedef struct vp56_context VP56Context;
00035 
00036 typedef struct {
00037     int16_t x;
00038     int16_t y;
00039 } DECLARE_ALIGNED(4, , VP56mv);
00040 
00041 typedef void (*VP56ParseVectorAdjustment)(VP56Context *s,
00042                                           VP56mv *vect);
00043 typedef void (*VP56Filter)(VP56Context *s, uint8_t *dst, uint8_t *src,
00044                            int offset1, int offset2, int stride,
00045                            VP56mv mv, int mask, int select, int luma);
00046 typedef void (*VP56ParseCoeff)(VP56Context *s);
00047 typedef void (*VP56DefaultModelsInit)(VP56Context *s);
00048 typedef void (*VP56ParseVectorModels)(VP56Context *s);
00049 typedef void (*VP56ParseCoeffModels)(VP56Context *s);
00050 typedef int  (*VP56ParseHeader)(VP56Context *s, const uint8_t *buf,
00051                                 int buf_size, int *golden_frame);
00052 
00053 typedef struct {
00054     int high;
00055     int bits; /* stored negated (i.e. negative "bits" is a positive number of
00056                  bits left) in order to eliminate a negate in cache refilling */
00057     const uint8_t *buffer;
00058     const uint8_t *end;
00059     unsigned int code_word;
00060 } VP56RangeCoder;
00061 
00062 typedef struct {
00063     uint8_t not_null_dc;
00064     VP56Frame ref_frame;
00065     DCTELEM dc_coeff;
00066 } VP56RefDc;
00067 
00068 typedef struct {
00069     uint8_t type;
00070     VP56mv mv;
00071 } VP56Macroblock;
00072 
00073 typedef struct {
00074     uint8_t coeff_reorder[64];       /* used in vp6 only */
00075     uint8_t coeff_index_to_pos[64];  /* used in vp6 only */
00076     uint8_t vector_sig[2];           /* delta sign */
00077     uint8_t vector_dct[2];           /* delta coding types */
00078     uint8_t vector_pdi[2][2];        /* predefined delta init */
00079     uint8_t vector_pdv[2][7];        /* predefined delta values */
00080     uint8_t vector_fdv[2][8];        /* 8 bit delta value definition */
00081     uint8_t coeff_dccv[2][11];       /* DC coeff value */
00082     uint8_t coeff_ract[2][3][6][11]; /* Run/AC coding type and AC coeff value */
00083     uint8_t coeff_acct[2][3][3][6][5];/* vp5 only AC coding type for coding group < 3 */
00084     uint8_t coeff_dcct[2][36][5];    /* DC coeff coding type */
00085     uint8_t coeff_runv[2][14];       /* run value (vp6 only) */
00086     uint8_t mb_type[3][10][10];      /* model for decoding MB type */
00087     uint8_t mb_types_stats[3][10][2];/* contextual, next MB type stats */
00088 } VP56Model;
00089 
00090 struct vp56_context {
00091     AVCodecContext *avctx;
00092     DSPContext dsp;
00093     VP56DSPContext vp56dsp;
00094     ScanTable scantable;
00095     AVFrame frames[4];
00096     AVFrame *framep[6];
00097     uint8_t *edge_emu_buffer_alloc;
00098     uint8_t *edge_emu_buffer;
00099     VP56RangeCoder c;
00100     VP56RangeCoder cc;
00101     VP56RangeCoder *ccp;
00102     int sub_version;
00103 
00104     /* frame info */
00105     int plane_width[4];
00106     int plane_height[4];
00107     int mb_width;   /* number of horizontal MB */
00108     int mb_height;  /* number of vertical MB */
00109     int block_offset[6];
00110 
00111     int quantizer;
00112     uint16_t dequant_dc;
00113     uint16_t dequant_ac;
00114     int8_t *qscale_table;
00115 
00116     /* DC predictors management */
00117     VP56RefDc *above_blocks;
00118     VP56RefDc left_block[4];
00119     int above_block_idx[6];
00120     DCTELEM prev_dc[3][3];    /* [plan][ref_frame] */
00121 
00122     /* blocks / macroblock */
00123     VP56mb mb_type;
00124     VP56Macroblock *macroblocks;
00125     DECLARE_ALIGNED(16, DCTELEM, block_coeff)[6][64];
00126 
00127     /* motion vectors */
00128     VP56mv mv[6];  /* vectors for each block in MB */
00129     VP56mv vector_candidate[2];
00130     int vector_candidate_pos;
00131 
00132     /* filtering hints */
00133     int filter_header;               /* used in vp6 only */
00134     int deblock_filtering;
00135     int filter_selection;
00136     int filter_mode;
00137     int max_vector_length;
00138     int sample_variance_threshold;
00139 
00140     uint8_t coeff_ctx[4][64];              /* used in vp5 only */
00141     uint8_t coeff_ctx_last[4];             /* used in vp5 only */
00142 
00143     int has_alpha;
00144 
00145     /* upside-down flipping hints */
00146     int flip;  /* are we flipping ? */
00147     int frbi;  /* first row block index in MB */
00148     int srbi;  /* second row block index in MB */
00149     int stride[4];  /* stride for each plan */
00150 
00151     const uint8_t *vp56_coord_div;
00152     VP56ParseVectorAdjustment parse_vector_adjustment;
00153     VP56Filter filter;
00154     VP56ParseCoeff parse_coeff;
00155     VP56DefaultModelsInit default_models_init;
00156     VP56ParseVectorModels parse_vector_models;
00157     VP56ParseCoeffModels parse_coeff_models;
00158     VP56ParseHeader parse_header;
00159 
00160     VP56Model *modelp;
00161     VP56Model models[2];
00162 
00163     /* huffman decoding */
00164     int use_huffman;
00165     GetBitContext gb;
00166     VLC dccv_vlc[2];
00167     VLC runv_vlc[2];
00168     VLC ract_vlc[2][3][6];
00169     unsigned int nb_null[2][2];       /* number of consecutive NULL DC/AC */
00170 };
00171 
00172 
00173 void ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha);
00174 int ff_vp56_free(AVCodecContext *avctx);
00175 void ff_vp56_init_dequant(VP56Context *s, int quantizer);
00176 int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00177                          AVPacket *avpkt);
00178 
00179 
00184 extern const uint8_t ff_vp56_norm_shift[256];
00185 void ff_vp56_init_range_decoder(VP56RangeCoder *c, const uint8_t *buf, int buf_size);
00186 
00187 static av_always_inline unsigned int vp56_rac_renorm(VP56RangeCoder *c)
00188 {
00189     int shift = ff_vp56_norm_shift[c->high];
00190     int bits = c->bits;
00191     unsigned int code_word = c->code_word;
00192 
00193     c->high   <<= shift;
00194     code_word <<= shift;
00195     bits       += shift;
00196     if(bits >= 0 && c->buffer < c->end) {
00197         code_word |= bytestream_get_be16(&c->buffer) << bits;
00198         bits -= 16;
00199     }
00200     c->bits = bits;
00201     return code_word;
00202 }
00203 
00204 #if   ARCH_ARM
00205 #include "arm/vp56_arith.h"
00206 #elif ARCH_X86
00207 #include "x86/vp56_arith.h"
00208 #endif
00209 
00210 #ifndef vp56_rac_get_prob
00211 #define vp56_rac_get_prob vp56_rac_get_prob
00212 static av_always_inline int vp56_rac_get_prob(VP56RangeCoder *c, uint8_t prob)
00213 {
00214     unsigned int code_word = vp56_rac_renorm(c);
00215     unsigned int low = 1 + (((c->high - 1) * prob) >> 8);
00216     unsigned int low_shift = low << 16;
00217     int bit = code_word >= low_shift;
00218 
00219     c->high = bit ? c->high - low : low;
00220     c->code_word = bit ? code_word - low_shift : code_word;
00221 
00222     return bit;
00223 }
00224 #endif
00225 
00226 #ifndef vp56_rac_get_prob_branchy
00227 // branchy variant, to be used where there's a branch based on the bit decoded
00228 static av_always_inline int vp56_rac_get_prob_branchy(VP56RangeCoder *c, int prob)
00229 {
00230     unsigned long code_word = vp56_rac_renorm(c);
00231     unsigned low = 1 + (((c->high - 1) * prob) >> 8);
00232     unsigned low_shift = low << 16;
00233 
00234     if (code_word >= low_shift) {
00235         c->high     -= low;
00236         c->code_word = code_word - low_shift;
00237         return 1;
00238     }
00239 
00240     c->high = low;
00241     c->code_word = code_word;
00242     return 0;
00243 }
00244 #endif
00245 
00246 static av_always_inline int vp56_rac_get(VP56RangeCoder *c)
00247 {
00248     unsigned int code_word = vp56_rac_renorm(c);
00249     /* equiprobable */
00250     int low = (c->high + 1) >> 1;
00251     unsigned int low_shift = low << 16;
00252     int bit = code_word >= low_shift;
00253     if (bit) {
00254         c->high   -= low;
00255         code_word -= low_shift;
00256     } else {
00257         c->high = low;
00258     }
00259 
00260     c->code_word = code_word;
00261     return bit;
00262 }
00263 
00264 // rounding is different than vp56_rac_get, is vp56_rac_get wrong?
00265 static av_always_inline int vp8_rac_get(VP56RangeCoder *c)
00266 {
00267     return vp56_rac_get_prob(c, 128);
00268 }
00269 
00270 static av_unused int vp56_rac_gets(VP56RangeCoder *c, int bits)
00271 {
00272     int value = 0;
00273 
00274     while (bits--) {
00275         value = (value << 1) | vp56_rac_get(c);
00276     }
00277 
00278     return value;
00279 }
00280 
00281 static av_unused int vp8_rac_get_uint(VP56RangeCoder *c, int bits)
00282 {
00283     int value = 0;
00284 
00285     while (bits--) {
00286         value = (value << 1) | vp8_rac_get(c);
00287     }
00288 
00289     return value;
00290 }
00291 
00292 // fixme: add 1 bit to all the calls to this?
00293 static av_unused int vp8_rac_get_sint(VP56RangeCoder *c, int bits)
00294 {
00295     int v;
00296 
00297     if (!vp8_rac_get(c))
00298         return 0;
00299 
00300     v = vp8_rac_get_uint(c, bits);
00301 
00302     if (vp8_rac_get(c))
00303         v = -v;
00304 
00305     return v;
00306 }
00307 
00308 // P(7)
00309 static av_unused int vp56_rac_gets_nn(VP56RangeCoder *c, int bits)
00310 {
00311     int v = vp56_rac_gets(c, 7) << 1;
00312     return v + !v;
00313 }
00314 
00315 static av_unused int vp8_rac_get_nn(VP56RangeCoder *c)
00316 {
00317     int v = vp8_rac_get_uint(c, 7) << 1;
00318     return v + !v;
00319 }
00320 
00321 static av_always_inline
00322 int vp56_rac_get_tree(VP56RangeCoder *c,
00323                       const VP56Tree *tree,
00324                       const uint8_t *probs)
00325 {
00326     while (tree->val > 0) {
00327         if (vp56_rac_get_prob(c, probs[tree->prob_idx]))
00328             tree += tree->val;
00329         else
00330             tree++;
00331     }
00332     return -tree->val;
00333 }
00334 
00340 static av_always_inline
00341 int vp8_rac_get_tree_with_offset(VP56RangeCoder *c, const int8_t (*tree)[2],
00342                                  const uint8_t *probs, int i)
00343 {
00344     do {
00345         i = tree[i][vp56_rac_get_prob(c, probs[i])];
00346     } while (i > 0);
00347 
00348     return -i;
00349 }
00350 
00351 // how probabilities are associated with decisions is different I think
00352 // well, the new scheme fits in the old but this way has one fewer branches per decision
00353 static av_always_inline
00354 int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t (*tree)[2],
00355                      const uint8_t *probs)
00356 {
00357     return vp8_rac_get_tree_with_offset(c, tree, probs, 0);
00358 }
00359 
00360 // DCTextra
00361 static av_always_inline int vp8_rac_get_coeff(VP56RangeCoder *c, const uint8_t *prob)
00362 {
00363     int v = 0;
00364 
00365     do {
00366         v = (v<<1) + vp56_rac_get_prob(c, *prob++);
00367     } while (*prob);
00368 
00369     return v;
00370 }
00371 
00372 #endif /* AVCODEC_VP56_H */