00001
00024 #ifndef FFMPEG_VP56_H
00025 #define FFMPEG_VP56_H
00026
00027 #include "vp56data.h"
00028 #include "dsputil.h"
00029 #include "mpegvideo.h"
00030 #include "bytestream.h"
00031
00032
00033 typedef struct vp56_context vp56_context_t;
00034 typedef struct vp56_mv vp56_mv_t;
00035
00036 typedef void (*vp56_parse_vector_adjustment_t)(vp56_context_t *s,
00037 vp56_mv_t *vect);
00038 typedef int (*vp56_adjust_t)(int v, int t);
00039 typedef void (*vp56_filter_t)(vp56_context_t *s, uint8_t *dst, uint8_t *src,
00040 int offset1, int offset2, int stride,
00041 vp56_mv_t mv, int mask, int select, int luma);
00042 typedef void (*vp56_parse_coeff_t)(vp56_context_t *s);
00043 typedef void (*vp56_default_models_init_t)(vp56_context_t *s);
00044 typedef void (*vp56_parse_vector_models_t)(vp56_context_t *s);
00045 typedef void (*vp56_parse_coeff_models_t)(vp56_context_t *s);
00046 typedef int (*vp56_parse_header_t)(vp56_context_t *s, const uint8_t *buf,
00047 int buf_size, int *golden_frame);
00048
00049 typedef struct {
00050 int high;
00051 int bits;
00052 const uint8_t *buffer;
00053 unsigned long code_word;
00054 } vp56_range_coder_t;
00055
00056 typedef struct {
00057 uint8_t not_null_dc;
00058 vp56_frame_t ref_frame;
00059 DCTELEM dc_coeff;
00060 } vp56_ref_dc_t;
00061
00062 struct vp56_mv {
00063 int x;
00064 int y;
00065 };
00066
00067 typedef struct {
00068 uint8_t type;
00069 vp56_mv_t mv;
00070 } vp56_macroblock_t;
00071
00072 typedef struct {
00073 uint8_t coeff_reorder[64];
00074 uint8_t coeff_index_to_pos[64];
00075 uint8_t vector_sig[2];
00076 uint8_t vector_dct[2];
00077 uint8_t vector_pdi[2][2];
00078 uint8_t vector_pdv[2][7];
00079 uint8_t vector_fdv[2][8];
00080 uint8_t coeff_dccv[2][11];
00081 uint8_t coeff_ract[2][3][6][11];
00082 uint8_t coeff_acct[2][3][3][6][5];
00083 uint8_t coeff_dcct[2][36][5];
00084 uint8_t coeff_runv[2][14];
00085 uint8_t mb_type[3][10][10];
00086 uint8_t mb_types_stats[3][10][2];
00087 } vp56_model_t;
00088
00089 struct vp56_context {
00090 AVCodecContext *avctx;
00091 DSPContext dsp;
00092 ScanTable scantable;
00093 AVFrame frames[4];
00094 AVFrame *framep[6];
00095 uint8_t *edge_emu_buffer_alloc;
00096 uint8_t *edge_emu_buffer;
00097 vp56_range_coder_t c;
00098 vp56_range_coder_t cc;
00099 vp56_range_coder_t *ccp;
00100 int sub_version;
00101
00102
00103 int plane_width[4];
00104 int plane_height[4];
00105 int mb_width;
00106 int mb_height;
00107 int block_offset[6];
00108
00109 int quantizer;
00110 uint16_t dequant_dc;
00111 uint16_t dequant_ac;
00112
00113
00114 vp56_ref_dc_t *above_blocks;
00115 vp56_ref_dc_t left_block[4];
00116 int above_block_idx[6];
00117 DCTELEM prev_dc[3][3];
00118
00119
00120 vp56_mb_t mb_type;
00121 vp56_macroblock_t *macroblocks;
00122 DECLARE_ALIGNED_16(DCTELEM, block_coeff[6][64]);
00123
00124
00125 vp56_mv_t mv[6];
00126 vp56_mv_t vector_candidate[2];
00127 int vector_candidate_pos;
00128
00129
00130 int filter_header;
00131 int deblock_filtering;
00132 int filter_selection;
00133 int filter_mode;
00134 int max_vector_length;
00135 int sample_variance_threshold;
00136
00137 uint8_t coeff_ctx[4][64];
00138 uint8_t coeff_ctx_last[4];
00139
00140 int has_alpha;
00141
00142
00143 int flip;
00144 int frbi;
00145 int srbi;
00146 int stride[4];
00147
00148 const uint8_t *vp56_coord_div;
00149 vp56_parse_vector_adjustment_t parse_vector_adjustment;
00150 vp56_adjust_t adjust;
00151 vp56_filter_t filter;
00152 vp56_parse_coeff_t parse_coeff;
00153 vp56_default_models_init_t default_models_init;
00154 vp56_parse_vector_models_t parse_vector_models;
00155 vp56_parse_coeff_models_t parse_coeff_models;
00156 vp56_parse_header_t parse_header;
00157
00158 vp56_model_t *modelp;
00159 vp56_model_t models[2];
00160
00161
00162 int use_huffman;
00163 GetBitContext gb;
00164 VLC dccv_vlc[2];
00165 VLC runv_vlc[2];
00166 VLC ract_vlc[2][3][6];
00167 unsigned int nb_null[2][2];
00168 };
00169
00170
00171 void vp56_init(AVCodecContext *avctx, int flip, int has_alpha);
00172 int vp56_free(AVCodecContext *avctx);
00173 void vp56_init_dequant(vp56_context_t *s, int quantizer);
00174 int vp56_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00175 const uint8_t *buf, int buf_size);
00176
00177
00182 static inline void vp56_init_range_decoder(vp56_range_coder_t *c,
00183 const uint8_t *buf, int buf_size)
00184 {
00185 c->high = 255;
00186 c->bits = 8;
00187 c->buffer = buf;
00188 c->code_word = bytestream_get_be16(&c->buffer);
00189 }
00190
00191 static inline int vp56_rac_get_prob(vp56_range_coder_t *c, uint8_t prob)
00192 {
00193 unsigned int low = 1 + (((c->high - 1) * prob) / 256);
00194 unsigned int low_shift = low << 8;
00195 int bit = c->code_word >= low_shift;
00196
00197 if (bit) {
00198 c->high -= low;
00199 c->code_word -= low_shift;
00200 } else {
00201 c->high = low;
00202 }
00203
00204
00205 while (c->high < 128) {
00206 c->high <<= 1;
00207 c->code_word <<= 1;
00208 if (--c->bits == 0) {
00209 c->bits = 8;
00210 c->code_word |= *c->buffer++;
00211 }
00212 }
00213 return bit;
00214 }
00215
00216 static inline int vp56_rac_get(vp56_range_coder_t *c)
00217 {
00218
00219 int low = (c->high + 1) >> 1;
00220 unsigned int low_shift = low << 8;
00221 int bit = c->code_word >= low_shift;
00222 if (bit) {
00223 c->high = (c->high - low) << 1;
00224 c->code_word -= low_shift;
00225 } else {
00226 c->high = low << 1;
00227 }
00228
00229
00230 c->code_word <<= 1;
00231 if (--c->bits == 0) {
00232 c->bits = 8;
00233 c->code_word |= *c->buffer++;
00234 }
00235 return bit;
00236 }
00237
00238 static inline int vp56_rac_gets(vp56_range_coder_t *c, int bits)
00239 {
00240 int value = 0;
00241
00242 while (bits--) {
00243 value = (value << 1) | vp56_rac_get(c);
00244 }
00245
00246 return value;
00247 }
00248
00249 static inline int vp56_rac_gets_nn(vp56_range_coder_t *c, int bits)
00250 {
00251 int v = vp56_rac_gets(c, 7) << 1;
00252 return v + !v;
00253 }
00254
00255 static inline int vp56_rac_get_tree(vp56_range_coder_t *c,
00256 const vp56_tree_t *tree,
00257 const uint8_t *probs)
00258 {
00259 while (tree->val > 0) {
00260 if (vp56_rac_get_prob(c, probs[tree->prob_idx]))
00261 tree += tree->val;
00262 else
00263 tree++;
00264 }
00265 return -tree->val;
00266 }
00267
00268 #endif