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