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