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