00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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;
00057
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];
00076 uint8_t coeff_index_to_pos[64];
00077 uint8_t vector_sig[2];
00078 uint8_t vector_dct[2];
00079 uint8_t vector_pdi[2][2];
00080 uint8_t vector_pdv[2][7];
00081 uint8_t vector_fdv[2][8];
00082 uint8_t coeff_dccv[2][11];
00083 uint8_t coeff_ract[2][3][6][11];
00084 uint8_t coeff_acct[2][3][3][6][5];
00085 uint8_t coeff_dcct[2][36][5];
00086 uint8_t coeff_runv[2][14];
00087 uint8_t mb_type[3][10][10];
00088 uint8_t mb_types_stats[3][10][2];
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
00106 int plane_width[4];
00107 int plane_height[4];
00108 int mb_width;
00109 int mb_height;
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
00118 VP56RefDc *above_blocks;
00119 VP56RefDc left_block[4];
00120 int above_block_idx[6];
00121 DCTELEM prev_dc[3][3];
00122
00123
00124 VP56mb mb_type;
00125 VP56Macroblock *macroblocks;
00126 DECLARE_ALIGNED(16, DCTELEM, block_coeff)[6][64];
00127
00128
00129 VP56mv mv[6];
00130 VP56mv vector_candidate[2];
00131 int vector_candidate_pos;
00132
00133
00134 int filter_header;
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];
00142 uint8_t coeff_ctx_last[4];
00143
00144 int has_alpha;
00145
00146
00147 int flip;
00148 int frbi;
00149 int srbi;
00150 int stride[4];
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
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];
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
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
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
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
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
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
00336
00337
00338 static av_always_inline int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t (*tree)[2],
00339 const uint8_t *probs)
00340 {
00341 int i = 0;
00342
00343 do {
00344 i = tree[i][vp56_rac_get_prob(c, probs[i])];
00345 } while (i > 0);
00346
00347 return -i;
00348 }
00349
00350
00351 static av_always_inline int vp8_rac_get_coeff(VP56RangeCoder *c, const uint8_t *prob)
00352 {
00353 int v = 0;
00354
00355 do {
00356 v = (v<<1) + vp56_rac_get_prob(c, *prob++);
00357 } while (*prob);
00358
00359 return v;
00360 }
00361
00362 #endif