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 "vp3dsp.h"
00034 #include "vp56dsp.h"
00035
00036 typedef struct vp56_context VP56Context;
00037
00038 typedef struct {
00039 DECLARE_ALIGNED(4, int16_t, x);
00040 int16_t y;
00041 } 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);
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 VP3DSPContext vp3dsp;
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
00108 int golden_frame;
00109 int plane_width[4];
00110 int plane_height[4];
00111 int mb_width;
00112 int mb_height;
00113 int block_offset[6];
00114
00115 int quantizer;
00116 uint16_t dequant_dc;
00117 uint16_t dequant_ac;
00118 int8_t *qscale_table;
00119
00120
00121 VP56RefDc *above_blocks;
00122 VP56RefDc left_block[4];
00123 int above_block_idx[6];
00124 DCTELEM prev_dc[3][3];
00125
00126
00127 VP56mb mb_type;
00128 VP56Macroblock *macroblocks;
00129 DECLARE_ALIGNED(16, DCTELEM, block_coeff)[6][64];
00130
00131
00132 VP56mv mv[6];
00133 VP56mv vector_candidate[2];
00134 int vector_candidate_pos;
00135
00136
00137 int filter_header;
00138 int deblock_filtering;
00139 int filter_selection;
00140 int filter_mode;
00141 int max_vector_length;
00142 int sample_variance_threshold;
00143
00144 uint8_t coeff_ctx[4][64];
00145 uint8_t coeff_ctx_last[4];
00146
00147 int has_alpha;
00148
00149
00150 int flip;
00151 int frbi;
00152 int srbi;
00153 int stride[4];
00154
00155 const uint8_t *vp56_coord_div;
00156 VP56ParseVectorAdjustment parse_vector_adjustment;
00157 VP56Filter filter;
00158 VP56ParseCoeff parse_coeff;
00159 VP56DefaultModelsInit default_models_init;
00160 VP56ParseVectorModels parse_vector_models;
00161 VP56ParseCoeffModels parse_coeff_models;
00162 VP56ParseHeader parse_header;
00163
00164
00165 VP56Context *alpha_context;
00166
00167 VP56Model *modelp;
00168 VP56Model model;
00169
00170
00171 int use_huffman;
00172 GetBitContext gb;
00173 VLC dccv_vlc[2];
00174 VLC runv_vlc[2];
00175 VLC ract_vlc[2][3][6];
00176 unsigned int nb_null[2][2];
00177 };
00178
00179
00180 void ff_vp56_init(AVCodecContext *avctx, int flip, int has_alpha);
00181 void ff_vp56_init_context(AVCodecContext *avctx, VP56Context *s,
00182 int flip, int has_alpha);
00183 int ff_vp56_free(AVCodecContext *avctx);
00184 int ff_vp56_free_context(VP56Context *s);
00185 void ff_vp56_init_dequant(VP56Context *s, int quantizer);
00186 int ff_vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00187 AVPacket *avpkt);
00188
00189
00194 extern const uint8_t ff_vp56_norm_shift[256];
00195 void ff_vp56_init_range_decoder(VP56RangeCoder *c, const uint8_t *buf, int buf_size);
00196
00197 static av_always_inline unsigned int vp56_rac_renorm(VP56RangeCoder *c)
00198 {
00199 int shift = ff_vp56_norm_shift[c->high];
00200 int bits = c->bits;
00201 unsigned int code_word = c->code_word;
00202
00203 c->high <<= shift;
00204 code_word <<= shift;
00205 bits += shift;
00206 if(bits >= 0 && c->buffer < c->end) {
00207 code_word |= bytestream_get_be16(&c->buffer) << bits;
00208 bits -= 16;
00209 }
00210 c->bits = bits;
00211 return code_word;
00212 }
00213
00214 #if ARCH_ARM
00215 #include "arm/vp56_arith.h"
00216 #elif ARCH_X86
00217 #include "x86/vp56_arith.h"
00218 #endif
00219
00220 #ifndef vp56_rac_get_prob
00221 #define vp56_rac_get_prob vp56_rac_get_prob
00222 static av_always_inline int vp56_rac_get_prob(VP56RangeCoder *c, uint8_t prob)
00223 {
00224 unsigned int code_word = vp56_rac_renorm(c);
00225 unsigned int low = 1 + (((c->high - 1) * prob) >> 8);
00226 unsigned int low_shift = low << 16;
00227 int bit = code_word >= low_shift;
00228
00229 c->high = bit ? c->high - low : low;
00230 c->code_word = bit ? code_word - low_shift : code_word;
00231
00232 return bit;
00233 }
00234 #endif
00235
00236 #ifndef vp56_rac_get_prob_branchy
00237
00238 static av_always_inline int vp56_rac_get_prob_branchy(VP56RangeCoder *c, int prob)
00239 {
00240 unsigned long code_word = vp56_rac_renorm(c);
00241 unsigned low = 1 + (((c->high - 1) * prob) >> 8);
00242 unsigned low_shift = low << 16;
00243
00244 if (code_word >= low_shift) {
00245 c->high -= low;
00246 c->code_word = code_word - low_shift;
00247 return 1;
00248 }
00249
00250 c->high = low;
00251 c->code_word = code_word;
00252 return 0;
00253 }
00254 #endif
00255
00256 static av_always_inline int vp56_rac_get(VP56RangeCoder *c)
00257 {
00258 unsigned int code_word = vp56_rac_renorm(c);
00259
00260 int low = (c->high + 1) >> 1;
00261 unsigned int low_shift = low << 16;
00262 int bit = code_word >= low_shift;
00263 if (bit) {
00264 c->high -= low;
00265 code_word -= low_shift;
00266 } else {
00267 c->high = low;
00268 }
00269
00270 c->code_word = code_word;
00271 return bit;
00272 }
00273
00274
00275 static av_always_inline int vp8_rac_get(VP56RangeCoder *c)
00276 {
00277 return vp56_rac_get_prob(c, 128);
00278 }
00279
00280 static av_unused int vp56_rac_gets(VP56RangeCoder *c, int bits)
00281 {
00282 int value = 0;
00283
00284 while (bits--) {
00285 value = (value << 1) | vp56_rac_get(c);
00286 }
00287
00288 return value;
00289 }
00290
00291 static av_unused int vp8_rac_get_uint(VP56RangeCoder *c, int bits)
00292 {
00293 int value = 0;
00294
00295 while (bits--) {
00296 value = (value << 1) | vp8_rac_get(c);
00297 }
00298
00299 return value;
00300 }
00301
00302
00303 static av_unused int vp8_rac_get_sint(VP56RangeCoder *c, int bits)
00304 {
00305 int v;
00306
00307 if (!vp8_rac_get(c))
00308 return 0;
00309
00310 v = vp8_rac_get_uint(c, bits);
00311
00312 if (vp8_rac_get(c))
00313 v = -v;
00314
00315 return v;
00316 }
00317
00318
00319 static av_unused int vp56_rac_gets_nn(VP56RangeCoder *c, int bits)
00320 {
00321 int v = vp56_rac_gets(c, 7) << 1;
00322 return v + !v;
00323 }
00324
00325 static av_unused int vp8_rac_get_nn(VP56RangeCoder *c)
00326 {
00327 int v = vp8_rac_get_uint(c, 7) << 1;
00328 return v + !v;
00329 }
00330
00331 static av_always_inline
00332 int vp56_rac_get_tree(VP56RangeCoder *c,
00333 const VP56Tree *tree,
00334 const uint8_t *probs)
00335 {
00336 while (tree->val > 0) {
00337 if (vp56_rac_get_prob(c, probs[tree->prob_idx]))
00338 tree += tree->val;
00339 else
00340 tree++;
00341 }
00342 return -tree->val;
00343 }
00344
00345
00346
00347 static av_always_inline int vp8_rac_get_tree(VP56RangeCoder *c, const int8_t (*tree)[2],
00348 const uint8_t *probs)
00349 {
00350 int i = 0;
00351
00352 do {
00353 i = tree[i][vp56_rac_get_prob(c, probs[i])];
00354 } while (i > 0);
00355
00356 return -i;
00357 }
00358
00359
00360 static av_always_inline int vp8_rac_get_coeff(VP56RangeCoder *c, const uint8_t *prob)
00361 {
00362 int v = 0;
00363
00364 do {
00365 v = (v<<1) + vp56_rac_get_prob(c, *prob++);
00366 } while (*prob);
00367
00368 return v;
00369 }
00370
00371 #endif