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