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
29
#include "bitstream.h"
30
#include "mpegvideo.h"
37
static int vp5_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size,
40
vp56_range_coder_t *c = &s->c;
43
vp56_init_range_decoder(&s->c, buf, buf_size);
44
s->framep[VP56_FRAME_CURRENT]->key_frame = !vp56_rac_get(c);
46
vp56_init_dequant(s, vp56_rac_gets(c, 6));
47
if (s->framep[VP56_FRAME_CURRENT]->key_frame)
50
if(vp56_rac_gets(c, 5) > 5)
53
if (vp56_rac_get(c)) {
54
av_log(s->avctx, AV_LOG_ERROR, "interlacing not supported\n");
57
rows = vp56_rac_gets(c, 8); /* number of stored macroblock rows */
58
cols = vp56_rac_gets(c, 8); /* number of stored macroblock cols */
59
vp56_rac_gets(c, 8); /* number of displayed macroblock rows */
60
vp56_rac_gets(c, 8); /* number of displayed macroblock cols */
62
if (16*cols != s->avctx->coded_width ||
63
16*rows != s->avctx->coded_height) {
64
avcodec_set_dimensions(s->avctx, 16*cols, 16*rows);
71
/* Gives very similar result than the vp6 version except in a few cases */
72
static int vp5_adjust(int v, int t)
88
static void vp5_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vect)
90
vp56_range_coder_t *c = &s->c;
93
for (comp=0; comp<2; comp++) {
95
if (vp56_rac_get_prob(c, s->vector_model_dct[comp])) {
96
int sign = vp56_rac_get_prob(c, s->vector_model_sig[comp]);
97
di = vp56_rac_get_prob(c, s->vector_model_pdi[comp][0]);
98
di |= vp56_rac_get_prob(c, s->vector_model_pdi[comp][1]) << 1;
99
delta = vp56_rac_get_tree(c, vp56_pva_tree,
100
s->vector_model_pdv[comp]);
101
delta = di | (delta << 2);
102
delta = (delta ^ -sign) + sign;
111
static void vp5_parse_vector_models(vp56_context_t *s)
113
vp56_range_coder_t *c = &s->c;
116
for (comp=0; comp<2; comp++) {
117
if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][0]))
118
s->vector_model_dct[comp] = vp56_rac_gets_nn(c, 7);
119
if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][1]))
120
s->vector_model_sig[comp] = vp56_rac_gets_nn(c, 7);
121
if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][2]))
122
s->vector_model_pdi[comp][0] = vp56_rac_gets_nn(c, 7);
123
if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][3]))
124
s->vector_model_pdi[comp][1] = vp56_rac_gets_nn(c, 7);
127
for (comp=0; comp<2; comp++)
128
for (node=0; node<7; node++)
129
if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][4 + node]))
130
s->vector_model_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
133
static void vp5_parse_coeff_models(vp56_context_t *s)
135
vp56_range_coder_t *c = &s->c;
136
uint8_t def_prob[11];
138
int ct; /* code type */
139
int pt; /* plane type (0 for Y, 1 for U or V) */
141
memset(def_prob, 0x80, sizeof(def_prob));
143
for (pt=0; pt<2; pt++)
144
for (node=0; node<11; node++)
145
if (vp56_rac_get_prob(c, vp5_dccv_pct[pt][node])) {
146
def_prob[node] = vp56_rac_gets_nn(c, 7);
147
s->coeff_model_dccv[pt][node] = def_prob[node];
148
} else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
149
s->coeff_model_dccv[pt][node] = def_prob[node];
152
for (ct=0; ct<3; ct++)
153
for (pt=0; pt<2; pt++)
154
for (cg=0; cg<6; cg++)
155
for (node=0; node<11; node++)
156
if (vp56_rac_get_prob(c, vp5_ract_pct[ct][pt][cg][node])) {
157
def_prob[node] = vp56_rac_gets_nn(c, 7);
158
s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
159
} else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
160
s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
163
/* coeff_model_dcct is a linear combination of coeff_model_dccv */
164
for (pt=0; pt<2; pt++)
165
for (ctx=0; ctx<36; ctx++)
166
for (node=0; node<5; node++)
167
s->coeff_model_dcct[pt][ctx][node] = av_clip(((s->coeff_model_dccv[pt][node] * vp5_dccv_lc[node][ctx][0] + 128) >> 8) + vp5_dccv_lc[node][ctx][1], 1, 254);
169
/* coeff_model_acct is a linear combination of coeff_model_ract */
170
for (ct=0; ct<3; ct++)
171
for (pt=0; pt<2; pt++)
172
for (cg=0; cg<3; cg++)
173
for (ctx=0; ctx<6; ctx++)
174
for (node=0; node<5; node++)
175
s->coeff_model_acct[pt][ct][cg][ctx][node] = av_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);
178
static void vp5_parse_coeff(vp56_context_t *s)
180
vp56_range_coder_t *c = &s->c;
181
uint8_t *permute = s->scantable.permutated;
182
uint8_t *model, *model2;
183
int coeff, sign, coeff_idx;
184
int b, i, cg, idx, ctx, ctx_last;
185
int pt = 0; /* plane type (0 for Y, 1 for U or V) */
187
for (b=0; b<6; b++) {
188
int ct = 1; /* code type */
192
ctx = 6*s->coeff_ctx[vp56_b6to4[b]][0]
193
+ s->above_blocks[s->above_block_idx[b]].not_null_dc;
194
model = s->coeff_model_dccv[pt];
195
model2 = s->coeff_model_dcct[pt][ctx];
197
for (coeff_idx=0; coeff_idx<64; ) {
198
if (vp56_rac_get_prob(c, model2[0])) {
199
if (vp56_rac_get_prob(c, model2[2])) {
200
if (vp56_rac_get_prob(c, model2[3])) {
201
s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 4;
202
idx = vp56_rac_get_tree(c, vp56_pc_tree, model);
203
sign = vp56_rac_get(c);
204
coeff = vp56_coeff_bias[idx];
205
for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
206
coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
208
if (vp56_rac_get_prob(c, model2[4])) {
209
coeff = 3 + vp56_rac_get_prob(c, model[5]);
210
s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 3;
213
s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 2;
215
sign = vp56_rac_get(c);
220
s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 1;
221
sign = vp56_rac_get(c);
224
coeff = (coeff ^ -sign) + sign;
226
coeff *= s->dequant_ac;
227
s->block_coeff[b][permute[coeff_idx]] = coeff;
229
if (ct && !vp56_rac_get_prob(c, model2[1]))
232
s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 0;
235
cg = vp5_coeff_groups[++coeff_idx];
236
ctx = s->coeff_ctx[vp56_b6to4[b]][coeff_idx];
237
model = s->coeff_model_ract[pt][ct][cg];
238
model2 = cg > 2 ? model : s->coeff_model_acct[pt][ct][cg][ctx];
241
ctx_last = FFMIN(s->coeff_ctx_last[vp56_b6to4[b]], 24);
242
s->coeff_ctx_last[vp56_b6to4[b]] = coeff_idx;
243
if (coeff_idx < ctx_last)
244
for (i=coeff_idx; i<=ctx_last; i++)
245
s->coeff_ctx[vp56_b6to4[b]][i] = 5;
246
s->above_blocks[s->above_block_idx[b]].not_null_dc = s->coeff_ctx[vp56_b6to4[b]][0];
250
static void vp5_default_models_init(vp56_context_t *s)
254
for (i=0; i<2; i++) {
255
s->vector_model_sig[i] = 0x80;
256
s->vector_model_dct[i] = 0x80;
257
s->vector_model_pdi[i][0] = 0x55;
258
s->vector_model_pdi[i][1] = 0x80;
260
memcpy(s->mb_types_stats, vp56_def_mb_types_stats, sizeof(s->mb_types_stats));
261
memset(s->vector_model_pdv, 0x80, sizeof(s->vector_model_pdv));
264
static int vp5_decode_init(AVCodecContext *avctx)
266
vp56_context_t *s = avctx->priv_data;
268
vp56_init(s, avctx, 1);
269
s->vp56_coord_div = vp5_coord_div;
270
s->parse_vector_adjustment = vp5_parse_vector_adjustment;
271
s->adjust = vp5_adjust;
272
s->parse_coeff = vp5_parse_coeff;
273
s->default_models_init = vp5_default_models_init;
274
s->parse_vector_models = vp5_parse_vector_models;
275
s->parse_coeff_models = vp5_parse_coeff_models;
276
s->parse_header = vp5_parse_header;
281
AVCodec vp5_decoder = {
285
sizeof(vp56_context_t),