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 "cabac.h"
00034 #include "vp56dsp.h"
00035
00036 typedef struct vp56_context VP56Context;
00037
00038 typedef struct {
00039 int16_t x;
00040 int16_t y;
00041 } DECLARE_ALIGNED(4, , VP56mv);
00042
00043 typedef void (*VP56ParseVectorAdjustment)(VP56Context *s,
00044 VP56mv *vect);
00045 typedef void (*VP56Filter)(VP56Context *s, uint8_t *dst, uint8_t *src,
00046 int offset1, int offset2, int stride,
00047 VP56mv mv, int mask, int select, int luma);
00048 typedef void (*VP56ParseCoeff)(VP56Context *s);
00049 typedef void (*VP56DefaultModelsInit)(VP56Context *s);
00050 typedef void (*VP56ParseVectorModels)(VP56Context *s);
00051 typedef int (*VP56ParseCoeffModels)(VP56Context *s);
00052 typedef int (*VP56ParseHeader)(VP56Context *s, const uint8_t *buf,
00053 int buf_size, int *golden_frame);
00054
00055 typedef struct {
00056 int high;
00057 int bits;
00058
00059 const uint8_t *buffer;
00060 const uint8_t *end;
00061 unsigned int code_word;
00062 } VP56RangeCoder;
00063
00064 typedef struct {
00065 uint8_t not_null_dc;
00066 VP56Frame ref_frame;
00067 DCTELEM dc_coeff;
00068 } VP56RefDc;
00069
00070 typedef struct {
00071 uint8_t type;
00072 VP56mv mv;
00073 } VP56Macroblock;
00074
00075 typedef struct {
00076 uint8_t coeff_reorder[64];
00077 uint8_t coeff_index_to_pos[64];
00078 uint8_t vector_sig[2];
00079 uint8_t vector_dct[2];
00080 uint8_t vector_pdi[2][2];
00081 uint8_t vector_pdv[2][7];
00082 uint8_t vector_fdv[2][8];
00083 uint8_t coeff_dccv[2][11];
00084 uint8_t coeff_ract[2][3][6][11];
00085 uint8_t coeff_acct[2][3][3][6][5];
00086 uint8_t coeff_dcct[2][36][5];
00087 uint8_t coeff_runv[2][14];
00088 uint8_t mb_type[3][10][10];
00089 uint8_t mb_types_stats[3][10][2];
00090 } VP56Model;
00091
00092 struct vp56_context {
00093 AVCodecContext *avctx;
00094 DSPContext dsp;
00095 VP56DSPContext vp56dsp;
00096 ScanTable scantable;
00097 AVFrame frames[4];
00098 AVFrame *framep[6];
00099 uint8_t *edge_emu_buffer_alloc;
00100 uint8_t *edge_emu_buffer;
00101 VP56RangeCoder c;
00102 VP56RangeCoder cc;
00103 VP56RangeCoder *ccp;
00104 int sub_version;
00105
00106
00107 int plane_width[4];
00108 int plane_height[4];
00109 int mb_width;
00110 int mb_height;
00111 int block_offset[6];
00112
00113 int quantizer;
00114 uint16_t dequant_dc;
00115 uint16_t dequant_ac;
00116 int8_t *qscale_table;
00117
00118
00119 VP56RefDc *above_blocks;
00120 VP56RefDc left_block[4];
00121 int above_block_idx[6];
00122 DCTELEM prev_dc[3][3];
00123
00124
00125 VP56mb mb_type;
00126 VP56Macroblock *macroblocks;
00127 DECLARE_ALIGNED(16, DCTELEM, block_coeff)[6][64];
00128
00129
00130 VP56mv mv[6];
00131 VP56mv vector_candidate[2];
00132 int vector_candidate_pos;
00133
00134
00135 int filter_header;
00136 int deblock_filtering;
00137 int filter_selection;
00138 int filter_mode;
00139 int max_vector_length;
00140 int sample_variance_threshold;
00141
00142 uint8_t coeff_ctx[4][64];
00143 uint8_t coeff_ctx_last[4];
00144
00145 int has_alpha;
00146
00147
00148 int flip;
00149 int frbi;
00150 int srbi;
00151 int stride[4];
00152
00153 const uint8_t *vp56_coord_div;
00154 VP56ParseVectorAdjustment parse_vector_adjustment;
00155 VP56Filter filter;
00156 VP56ParseCoeff parse_coeff;
00157 VP56DefaultModelsInit default_models_init;
00158 VP56ParseVectorModels parse_vector_models;
00159 VP56ParseCoeffModels parse_coeff_models;
00160 VP56ParseHeader parse_header;
00161
00162 VP56Model *modelp;
00163 VP56Model models[2];
00164
00165
00166 int use_huffman;
00167 GetBitContext gb;
00168 VLC dccv_vlc[2];
00169 VLC runv_vlc[2];
00170 VLC ract_vlc[2][3][6];
00171 unsigned int nb_null[2][2];
00172 };
00173
00174
00175 void ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha);
00176 int ff_vp56_free(AVCodecContext *avctx);
00177 void ff_vp56_init_dequant(VP56Context *s, int quantizer);
00178 int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00179 AVPacket *avpkt);
00180
00181
00186 extern const uint8_t ff_vp56_norm_shift[256];
00187 void ff_vp56_init_range_decoder(VP56RangeCoder *c, const uint8_t *buf, int buf_size);
00188
00189 static av_always_inline unsigned int vp56_rac_renorm(VP56RangeCoder *c)
00190 {
00191 int shift = ff_vp56_norm_shift[c->high];
00192 int bits = c->bits;
00193 unsigned int code_word = c->code_word;
00194
00195 c->high <<= shift;
00196 code_word <<= shift;
00197 bits += shift;
00198 if(bits >= 0 && c->buffer < c->end) {
00199 code_word |= bytestream_get_be16(&c->buffer) << bits;
00200 bits -= 16;
00201 }
00202 c->bits = bits;
00203 return code_word;
00204 }
00205
00206 #if ARCH_ARM
00207 #include "arm/vp56_arith.h"
00208 #elif ARCH_X86
00209 #include "x86/vp56_arith.h"
00210 #endif
00211
00212 #ifndef vp56_rac_get_prob
00213 #define vp56_rac_get_prob vp56_rac_get_prob
00214 static av_always_inline int vp56_rac_get_prob(VP56RangeCoder *c, uint8_t prob)
00215 {
00216 unsigned int code_word = vp56_rac_renorm(c);
00217 unsigned int low = 1 + (((c->high - 1) * prob) >> 8);
00218 unsigned int low_shift = low << 16;
00219 int bit = code_word >= low_shift;
00220
00221 c->high = bit ? c->high - low : low;
00222 c->code_word = bit ? code_word - low_shift : code_word;
00223
00224 return bit;
00225 }
00226 #endif
00227
00228 #ifndef vp56_rac_get_prob_branchy
00229
00230 static av_always_inline int vp56_rac_get_prob_branchy(VP56RangeCoder *c, int prob)
00231 {
00232 unsigned long code_word = vp56_rac_renorm(c);
00233 unsigned low = 1 + (((c->high - 1) * prob) >> 8);
00234 unsigned low_shift = low << 16;
00235
00236 if (code_word >= low_shift) {
00237 c->high -= low;
00238 c->code_word = code_word - low_shift;
00239 return 1;
00240 }
00241
00242 c->high = low;
00243 c->code_word = code_word;
00244 return 0;
00245 }
00246 #endif
00247
00248 static av_always_inline int vp56_rac_get(VP56RangeCoder *c)
00249 {
00250 unsigned int code_word = vp56_rac_renorm(c);
00251
00252 int low = (c->high + 1) >> 1;
00253 unsigned int low_shift = low << 16;
00254 int bit = code_word >= low_shift;
00255 if (bit) {
00256 c->high -= low;
00257 code_word -= low_shift;
00258 } else {
00259 c->high = low;
00260 }
00261
00262 c->code_word = code_word;
00263 return bit;
00264 }
00265
00266
00267 static av_always_inline int vp8_rac_get(VP56RangeCoder *c)
00268 {
00269 return vp56_rac_get_prob(c, 128);
00270 }
00271
00272 static av_unused int vp56_rac_gets(VP56RangeCoder *c, int bits)
00273 {
00274 int value = 0;
00275
00276 while (bits--) {
00277 value = (value << 1) | vp56_rac_get(c);
00278 }
00279
00280 return value;
00281 }
00282
00283 static av_unused int vp8_rac_get_uint(VP56RangeCoder *c, int bits)
00284 {
00285 int value = 0;
00286
00287 while (bits--) {
00288 value = (value << 1) | vp8_rac_get(c);
00289 }
00290
00291 return value;
00292 }
00293
00294
00295 static av_unused int vp8_rac_get_sint(VP56RangeCoder *c, int bits)
00296 {
00297 int v;
00298
00299 if (!vp8_rac_get(c))
00300 return 0;
00301
00302 v = vp8_rac_get_uint(c, bits);
00303
00304 if (vp8_rac_get(c))
00305 v = -v;
00306
00307 return v;
00308 }
00309
00310
00311 static av_unused int vp56_rac_gets_nn(VP56RangeCoder *c, int bits)
00312 {
00313 int v = vp56_rac_gets(c, 7) << 1;
00314 return v + !v;
00315 }
00316
00317 static av_unused int vp8_rac_get_nn(VP56RangeCoder *c)
00318 {
00319 int v = vp8_rac_get_uint(c, 7) << 1;
00320 return v + !v;
00321 }
00322
00323 static av_always_inline
00324 int vp56_rac_get_tree(VP56RangeCoder *c,
00325 const VP56Tree *tree,
00326 const uint8_t *probs)
00327 {
00328 while (tree->val > 0) {
00329 if (vp56_rac_get_prob(c, probs[tree->prob_idx]))
00330 tree += tree->val;
00331 else
00332 tree++;
00333 }
00334 return -tree->val;
00335 }
00336
00337
00338
00339 static av_always_inline int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t (*tree)[2],
00340 const uint8_t *probs)
00341 {
00342 int i = 0;
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
00352 static av_always_inline int vp8_rac_get_coeff(VP56RangeCoder *c, const uint8_t *prob)
00353 {
00354 int v = 0;
00355
00356 do {
00357 v = (v<<1) + vp56_rac_get_prob(c, *prob++);
00358 } while (*prob);
00359
00360 return v;
00361 }
00362
00363 #endif