3 * VP5 compatible video decoder
5 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org>
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "bitstream.h"
31 #include "mpegvideo.h"
38 static int vp5_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size,
41 vp56_range_coder_t *c = &s->c;
44 vp56_init_range_decoder(&s->c, buf, buf_size);
45 s->frames[VP56_FRAME_CURRENT].key_frame = !vp56_rac_get(c);
47 vp56_init_dequant(s, vp56_rac_gets(c, 6));
48 if (s->frames[VP56_FRAME_CURRENT].key_frame)
51 if(vp56_rac_gets(c, 5) > 5)
54 if (vp56_rac_get(c)) {
55 av_log(s->avctx, AV_LOG_ERROR, "interlacing not supported\n");
58 rows = vp56_rac_gets(c, 8); /* number of stored macroblock rows */
59 cols = vp56_rac_gets(c, 8); /* number of stored macroblock cols */
60 vp56_rac_gets(c, 8); /* number of displayed macroblock rows */
61 vp56_rac_gets(c, 8); /* number of displayed macroblock cols */
63 if (16*cols != s->avctx->coded_width ||
64 16*rows != s->avctx->coded_height) {
65 avcodec_set_dimensions(s->avctx, 16*cols, 16*rows);
72 /* Gives very similar result than the vp6 version except in a few cases */
73 static int vp5_adjust(int v, int t)
89 static void vp5_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vect)
91 vp56_range_coder_t *c = &s->c;
94 for (comp=0; comp<2; comp++) {
96 if (vp56_rac_get_prob(c, s->vector_model_dct[comp])) {
97 int sign = vp56_rac_get_prob(c, s->vector_model_sig[comp]);
98 di = vp56_rac_get_prob(c, s->vector_model_pdi[comp][0]);
99 di |= vp56_rac_get_prob(c, s->vector_model_pdi[comp][1]) << 1;
100 delta = vp56_rac_get_tree(c, vp56_pva_tree,
101 s->vector_model_pdv[comp]);
102 delta = di | (delta << 2);
103 delta = (delta ^ -sign) + sign;
112 static void vp5_parse_vector_models(vp56_context_t *s)
114 vp56_range_coder_t *c = &s->c;
117 for (comp=0; comp<2; comp++) {
118 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][0]))
119 s->vector_model_dct[comp] = vp56_rac_gets_nn(c, 7);
120 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][1]))
121 s->vector_model_sig[comp] = vp56_rac_gets_nn(c, 7);
122 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][2]))
123 s->vector_model_pdi[comp][0] = vp56_rac_gets_nn(c, 7);
124 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][3]))
125 s->vector_model_pdi[comp][1] = vp56_rac_gets_nn(c, 7);
128 for (comp=0; comp<2; comp++)
129 for (node=0; node<7; node++)
130 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][4 + node]))
131 s->vector_model_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
134 static void vp5_parse_coeff_models(vp56_context_t *s)
136 vp56_range_coder_t *c = &s->c;
137 uint8_t def_prob[11];
139 int ct; /* code type */
140 int pt; /* plane type (0 for Y, 1 for U or V) */
142 memset(def_prob, 0x80, sizeof(def_prob));
144 for (pt=0; pt<2; pt++)
145 for (node=0; node<11; node++)
146 if (vp56_rac_get_prob(c, vp5_dccv_pct[pt][node])) {
147 def_prob[node] = vp56_rac_gets_nn(c, 7);
148 s->coeff_model_dccv[pt][node] = def_prob[node];
149 } else if (s->frames[VP56_FRAME_CURRENT].key_frame) {
150 s->coeff_model_dccv[pt][node] = def_prob[node];
153 for (ct=0; ct<3; ct++)
154 for (pt=0; pt<2; pt++)
155 for (cg=0; cg<6; cg++)
156 for (node=0; node<11; node++)
157 if (vp56_rac_get_prob(c, vp5_ract_pct[ct][pt][cg][node])) {
158 def_prob[node] = vp56_rac_gets_nn(c, 7);
159 s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
160 } else if (s->frames[VP56_FRAME_CURRENT].key_frame) {
161 s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
164 /* coeff_model_dcct is a linear combination of coeff_model_dccv */
165 for (pt=0; pt<2; pt++)
166 for (ctx=0; ctx<36; ctx++)
167 for (node=0; node<5; node++)
168 s->coeff_model_dcct[pt][ctx][node] = clip(((s->coeff_model_dccv[pt][node] * vp5_dccv_lc[node][ctx][0] + 128) >> 8) + vp5_dccv_lc[node][ctx][1], 1, 254);
170 /* coeff_model_acct is a linear combination of coeff_model_ract */
171 for (ct=0; ct<3; ct++)
172 for (pt=0; pt<2; pt++)
173 for (cg=0; cg<3; cg++)
174 for (ctx=0; ctx<6; ctx++)
175 for (node=0; node<5; node++)
176 s->coeff_model_acct[pt][ct][cg][ctx][node] = clip(((s->coeff_model_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);
179 static void vp5_parse_coeff(vp56_context_t *s)
181 vp56_range_coder_t *c = &s->c;
182 uint8_t *permute = s->scantable.permutated;
183 uint8_t *model, *model2;
184 int coeff, sign, coeff_idx;
185 int b, i, cg, idx, ctx, ctx_last;
186 int pt = 0; /* plane type (0 for Y, 1 for U or V) */
188 for (b=0; b<6; b++) {
189 int ct = 1; /* code type */
193 ctx = 6*s->coeff_ctx[vp56_b6to4[b]][0]
194 + s->above_blocks[s->above_block_idx[b]].not_null_dc;
195 model = s->coeff_model_dccv[pt];
196 model2 = s->coeff_model_dcct[pt][ctx];
198 for (coeff_idx=0; coeff_idx<64; ) {
199 if (vp56_rac_get_prob(c, model2[0])) {
200 if (vp56_rac_get_prob(c, model2[2])) {
201 if (vp56_rac_get_prob(c, model2[3])) {
202 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 4;
203 idx = vp56_rac_get_tree(c, vp56_pc_tree, model);
204 sign = vp56_rac_get(c);
205 coeff = vp56_coeff_bias[idx];
206 for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
207 coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
209 if (vp56_rac_get_prob(c, model2[4])) {
210 coeff = 3 + vp56_rac_get_prob(c, model[5]);
211 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 3;
214 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 2;
216 sign = vp56_rac_get(c);
221 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 1;
222 sign = vp56_rac_get(c);
225 coeff = (coeff ^ -sign) + sign;
227 coeff *= s->dequant_ac;
228 s->block_coeff[b][permute[coeff_idx]] = coeff;
230 if (ct && !vp56_rac_get_prob(c, model2[1]))
233 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 0;
236 cg = vp5_coeff_groups[++coeff_idx];
237 ctx = s->coeff_ctx[vp56_b6to4[b]][coeff_idx];
238 model = s->coeff_model_ract[pt][ct][cg];
239 model2 = cg > 2 ? model : s->coeff_model_acct[pt][ct][cg][ctx];
242 ctx_last = FFMIN(s->coeff_ctx_last[vp56_b6to4[b]], 24);
243 s->coeff_ctx_last[vp56_b6to4[b]] = coeff_idx;
244 if (coeff_idx < ctx_last)
245 for (i=coeff_idx; i<=ctx_last; i++)
246 s->coeff_ctx[vp56_b6to4[b]][i] = 5;
247 s->above_blocks[s->above_block_idx[b]].not_null_dc = s->coeff_ctx[vp56_b6to4[b]][0];
251 static void vp5_default_models_init(vp56_context_t *s)
255 for (i=0; i<2; i++) {
256 s->vector_model_sig[i] = 0x80;
257 s->vector_model_dct[i] = 0x80;
258 s->vector_model_pdi[i][0] = 0x55;
259 s->vector_model_pdi[i][1] = 0x80;
261 memcpy(s->mb_types_stats, vp56_def_mb_types_stats, sizeof(s->mb_types_stats));
262 memset(s->vector_model_pdv, 0x80, sizeof(s->vector_model_pdv));
265 static int vp5_decode_init(AVCodecContext *avctx)
267 vp56_context_t *s = avctx->priv_data;
269 vp56_init(s, avctx, 1);
270 s->vp56_coord_div = vp5_coord_div;
271 s->parse_vector_adjustment = vp5_parse_vector_adjustment;
272 s->adjust = vp5_adjust;
273 s->parse_coeff = vp5_parse_coeff;
274 s->default_models_init = vp5_default_models_init;
275 s->parse_vector_models = vp5_parse_vector_models;
276 s->parse_coeff_models = vp5_parse_coeff_models;
277 s->parse_header = vp5_parse_header;
282 AVCodec vp5_decoder = {
286 sizeof(vp56_context_t),