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