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