00001
00024 #include <stdlib.h>
00025 #include <string.h>
00026
00027 #include "avcodec.h"
00028 #include "dsputil.h"
00029 #include "get_bits.h"
00030
00031 #include "vp56.h"
00032 #include "vp56data.h"
00033 #include "vp5data.h"
00034
00035
00036 static int vp5_parse_header(VP56Context *s, const uint8_t *buf, int buf_size,
00037 int *golden_frame)
00038 {
00039 VP56RangeCoder *c = &s->c;
00040 int rows, cols;
00041
00042 vp56_init_range_decoder(&s->c, buf, buf_size);
00043 s->framep[VP56_FRAME_CURRENT]->key_frame = !vp56_rac_get(c);
00044 vp56_rac_get(c);
00045 vp56_init_dequant(s, vp56_rac_gets(c, 6));
00046 if (s->framep[VP56_FRAME_CURRENT]->key_frame)
00047 {
00048 vp56_rac_gets(c, 8);
00049 if(vp56_rac_gets(c, 5) > 5)
00050 return 0;
00051 vp56_rac_gets(c, 2);
00052 if (vp56_rac_get(c)) {
00053 av_log(s->avctx, AV_LOG_ERROR, "interlacing not supported\n");
00054 return 0;
00055 }
00056 rows = vp56_rac_gets(c, 8);
00057 cols = vp56_rac_gets(c, 8);
00058 vp56_rac_gets(c, 8);
00059 vp56_rac_gets(c, 8);
00060 vp56_rac_gets(c, 2);
00061 if (!s->macroblocks ||
00062 16*cols != s->avctx->coded_width ||
00063 16*rows != s->avctx->coded_height) {
00064 avcodec_set_dimensions(s->avctx, 16*cols, 16*rows);
00065 return 2;
00066 }
00067 } else if (!s->macroblocks)
00068 return 0;
00069 return 1;
00070 }
00071
00072 static void vp5_parse_vector_adjustment(VP56Context *s, VP56mv *vect)
00073 {
00074 VP56RangeCoder *c = &s->c;
00075 VP56Model *model = s->modelp;
00076 int comp, di;
00077
00078 for (comp=0; comp<2; comp++) {
00079 int delta = 0;
00080 if (vp56_rac_get_prob(c, model->vector_dct[comp])) {
00081 int sign = vp56_rac_get_prob(c, model->vector_sig[comp]);
00082 di = vp56_rac_get_prob(c, model->vector_pdi[comp][0]);
00083 di |= vp56_rac_get_prob(c, model->vector_pdi[comp][1]) << 1;
00084 delta = vp56_rac_get_tree(c, vp56_pva_tree,
00085 model->vector_pdv[comp]);
00086 delta = di | (delta << 2);
00087 delta = (delta ^ -sign) + sign;
00088 }
00089 if (!comp)
00090 vect->x = delta;
00091 else
00092 vect->y = delta;
00093 }
00094 }
00095
00096 static void vp5_parse_vector_models(VP56Context *s)
00097 {
00098 VP56RangeCoder *c = &s->c;
00099 VP56Model *model = s->modelp;
00100 int comp, node;
00101
00102 for (comp=0; comp<2; comp++) {
00103 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][0]))
00104 model->vector_dct[comp] = vp56_rac_gets_nn(c, 7);
00105 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][1]))
00106 model->vector_sig[comp] = vp56_rac_gets_nn(c, 7);
00107 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][2]))
00108 model->vector_pdi[comp][0] = vp56_rac_gets_nn(c, 7);
00109 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][3]))
00110 model->vector_pdi[comp][1] = vp56_rac_gets_nn(c, 7);
00111 }
00112
00113 for (comp=0; comp<2; comp++)
00114 for (node=0; node<7; node++)
00115 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][4 + node]))
00116 model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
00117 }
00118
00119 static void vp5_parse_coeff_models(VP56Context *s)
00120 {
00121 VP56RangeCoder *c = &s->c;
00122 VP56Model *model = s->modelp;
00123 uint8_t def_prob[11];
00124 int node, cg, ctx;
00125 int ct;
00126 int pt;
00127
00128 memset(def_prob, 0x80, sizeof(def_prob));
00129
00130 for (pt=0; pt<2; pt++)
00131 for (node=0; node<11; node++)
00132 if (vp56_rac_get_prob(c, vp5_dccv_pct[pt][node])) {
00133 def_prob[node] = vp56_rac_gets_nn(c, 7);
00134 model->coeff_dccv[pt][node] = def_prob[node];
00135 } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
00136 model->coeff_dccv[pt][node] = def_prob[node];
00137 }
00138
00139 for (ct=0; ct<3; ct++)
00140 for (pt=0; pt<2; pt++)
00141 for (cg=0; cg<6; cg++)
00142 for (node=0; node<11; node++)
00143 if (vp56_rac_get_prob(c, vp5_ract_pct[ct][pt][cg][node])) {
00144 def_prob[node] = vp56_rac_gets_nn(c, 7);
00145 model->coeff_ract[pt][ct][cg][node] = def_prob[node];
00146 } else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
00147 model->coeff_ract[pt][ct][cg][node] = def_prob[node];
00148 }
00149
00150
00151 for (pt=0; pt<2; pt++)
00152 for (ctx=0; ctx<36; ctx++)
00153 for (node=0; node<5; node++)
00154 model->coeff_dcct[pt][ctx][node] = av_clip(((model->coeff_dccv[pt][node] * vp5_dccv_lc[node][ctx][0] + 128) >> 8) + vp5_dccv_lc[node][ctx][1], 1, 254);
00155
00156
00157 for (ct=0; ct<3; ct++)
00158 for (pt=0; pt<2; pt++)
00159 for (cg=0; cg<3; cg++)
00160 for (ctx=0; ctx<6; ctx++)
00161 for (node=0; node<5; node++)
00162 model->coeff_acct[pt][ct][cg][ctx][node] = av_clip(((model->coeff_ract[pt][ct][cg][node] * vp5_ract_lc[ct][cg][node][ctx][0] + 128) >> 8) + vp5_ract_lc[ct][cg][node][ctx][1], 1, 254);
00163 }
00164
00165 static void vp5_parse_coeff(VP56Context *s)
00166 {
00167 VP56RangeCoder *c = &s->c;
00168 VP56Model *model = s->modelp;
00169 uint8_t *permute = s->scantable.permutated;
00170 uint8_t *model1, *model2;
00171 int coeff, sign, coeff_idx;
00172 int b, i, cg, idx, ctx, ctx_last;
00173 int pt = 0;
00174
00175 for (b=0; b<6; b++) {
00176 int ct = 1;
00177
00178 if (b > 3) pt = 1;
00179
00180 ctx = 6*s->coeff_ctx[vp56_b6to4[b]][0]
00181 + s->above_blocks[s->above_block_idx[b]].not_null_dc;
00182 model1 = model->coeff_dccv[pt];
00183 model2 = model->coeff_dcct[pt][ctx];
00184
00185 for (coeff_idx=0; coeff_idx<64; ) {
00186 if (vp56_rac_get_prob(c, model2[0])) {
00187 if (vp56_rac_get_prob(c, model2[2])) {
00188 if (vp56_rac_get_prob(c, model2[3])) {
00189 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 4;
00190 idx = vp56_rac_get_tree(c, vp56_pc_tree, model1);
00191 sign = vp56_rac_get(c);
00192 coeff = vp56_coeff_bias[idx+5];
00193 for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
00194 coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
00195 } else {
00196 if (vp56_rac_get_prob(c, model2[4])) {
00197 coeff = 3 + vp56_rac_get_prob(c, model1[5]);
00198 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 3;
00199 } else {
00200 coeff = 2;
00201 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 2;
00202 }
00203 sign = vp56_rac_get(c);
00204 }
00205 ct = 2;
00206 } else {
00207 ct = 1;
00208 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 1;
00209 sign = vp56_rac_get(c);
00210 coeff = 1;
00211 }
00212 coeff = (coeff ^ -sign) + sign;
00213 if (coeff_idx)
00214 coeff *= s->dequant_ac;
00215 s->block_coeff[b][permute[coeff_idx]] = coeff;
00216 } else {
00217 if (ct && !vp56_rac_get_prob(c, model2[1]))
00218 break;
00219 ct = 0;
00220 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 0;
00221 }
00222
00223 cg = vp5_coeff_groups[++coeff_idx];
00224 ctx = s->coeff_ctx[vp56_b6to4[b]][coeff_idx];
00225 model1 = model->coeff_ract[pt][ct][cg];
00226 model2 = cg > 2 ? model1 : model->coeff_acct[pt][ct][cg][ctx];
00227 }
00228
00229 ctx_last = FFMIN(s->coeff_ctx_last[vp56_b6to4[b]], 24);
00230 s->coeff_ctx_last[vp56_b6to4[b]] = coeff_idx;
00231 if (coeff_idx < ctx_last)
00232 for (i=coeff_idx; i<=ctx_last; i++)
00233 s->coeff_ctx[vp56_b6to4[b]][i] = 5;
00234 s->above_blocks[s->above_block_idx[b]].not_null_dc = s->coeff_ctx[vp56_b6to4[b]][0];
00235 }
00236 }
00237
00238 static void vp5_default_models_init(VP56Context *s)
00239 {
00240 VP56Model *model = s->modelp;
00241 int i;
00242
00243 for (i=0; i<2; i++) {
00244 model->vector_sig[i] = 0x80;
00245 model->vector_dct[i] = 0x80;
00246 model->vector_pdi[i][0] = 0x55;
00247 model->vector_pdi[i][1] = 0x80;
00248 }
00249 memcpy(model->mb_types_stats, vp56_def_mb_types_stats, sizeof(model->mb_types_stats));
00250 memset(model->vector_pdv, 0x80, sizeof(model->vector_pdv));
00251 }
00252
00253 static av_cold int vp5_decode_init(AVCodecContext *avctx)
00254 {
00255 VP56Context *s = avctx->priv_data;
00256
00257 vp56_init(avctx, 1, 0);
00258 s->vp56_coord_div = vp5_coord_div;
00259 s->parse_vector_adjustment = vp5_parse_vector_adjustment;
00260 s->parse_coeff = vp5_parse_coeff;
00261 s->default_models_init = vp5_default_models_init;
00262 s->parse_vector_models = vp5_parse_vector_models;
00263 s->parse_coeff_models = vp5_parse_coeff_models;
00264 s->parse_header = vp5_parse_header;
00265
00266 return 0;
00267 }
00268
00269 AVCodec vp5_decoder = {
00270 "vp5",
00271 AVMEDIA_TYPE_VIDEO,
00272 CODEC_ID_VP5,
00273 sizeof(VP56Context),
00274 vp5_decode_init,
00275 NULL,
00276 vp56_free,
00277 vp56_decode_frame,
00278 CODEC_CAP_DR1,
00279 .long_name = NULL_IF_CONFIG_SMALL("On2 VP5"),
00280 };