19
19
* You should have received a copy of the GNU Lesser General Public
20
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
21
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24
24
#include <stdlib.h>
27
27
#include "avcodec.h"
28
28
#include "dsputil.h"
29
29
#include "bitstream.h"
30
#include "mpegvideo.h"
33
32
#include "vp56data.h"
34
33
#include "vp5data.h"
37
static int vp5_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size,
36
static int vp5_parse_header(vp56_context_t *s, const uint8_t *buf, int buf_size,
40
39
vp56_range_coder_t *c = &s->c;
88
87
static void vp5_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vect)
90
89
vp56_range_coder_t *c = &s->c;
90
vp56_model_t *model = s->modelp;
93
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;
95
if (vp56_rac_get_prob(c, model->vector_dct[comp])) {
96
int sign = vp56_rac_get_prob(c, model->vector_sig[comp]);
97
di = vp56_rac_get_prob(c, model->vector_pdi[comp][0]);
98
di |= vp56_rac_get_prob(c, model->vector_pdi[comp][1]) << 1;
99
99
delta = vp56_rac_get_tree(c, vp56_pva_tree,
100
s->vector_model_pdv[comp]);
100
model->vector_pdv[comp]);
101
101
delta = di | (delta << 2);
102
102
delta = (delta ^ -sign) + sign;
111
111
static void vp5_parse_vector_models(vp56_context_t *s)
113
113
vp56_range_coder_t *c = &s->c;
114
vp56_model_t *model = s->modelp;
116
117
for (comp=0; comp<2; comp++) {
117
118
if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][0]))
118
s->vector_model_dct[comp] = vp56_rac_gets_nn(c, 7);
119
model->vector_dct[comp] = vp56_rac_gets_nn(c, 7);
119
120
if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][1]))
120
s->vector_model_sig[comp] = vp56_rac_gets_nn(c, 7);
121
model->vector_sig[comp] = vp56_rac_gets_nn(c, 7);
121
122
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
model->vector_pdi[comp][0] = vp56_rac_gets_nn(c, 7);
123
124
if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][3]))
124
s->vector_model_pdi[comp][1] = vp56_rac_gets_nn(c, 7);
125
model->vector_pdi[comp][1] = vp56_rac_gets_nn(c, 7);
127
128
for (comp=0; comp<2; comp++)
128
129
for (node=0; node<7; node++)
129
130
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);
131
model->vector_pdv[comp][node] = vp56_rac_gets_nn(c, 7);
133
134
static void vp5_parse_coeff_models(vp56_context_t *s)
135
136
vp56_range_coder_t *c = &s->c;
137
vp56_model_t *model = s->modelp;
136
138
uint8_t def_prob[11];
137
139
int node, cg, ctx;
138
140
int ct; /* code type */
144
146
for (node=0; node<11; node++)
145
147
if (vp56_rac_get_prob(c, vp5_dccv_pct[pt][node])) {
146
148
def_prob[node] = vp56_rac_gets_nn(c, 7);
147
s->coeff_model_dccv[pt][node] = def_prob[node];
149
model->coeff_dccv[pt][node] = def_prob[node];
148
150
} else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
149
s->coeff_model_dccv[pt][node] = def_prob[node];
151
model->coeff_dccv[pt][node] = def_prob[node];
152
154
for (ct=0; ct<3; ct++)
155
157
for (node=0; node<11; node++)
156
158
if (vp56_rac_get_prob(c, vp5_ract_pct[ct][pt][cg][node])) {
157
159
def_prob[node] = vp56_rac_gets_nn(c, 7);
158
s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
160
model->coeff_ract[pt][ct][cg][node] = def_prob[node];
159
161
} else if (s->framep[VP56_FRAME_CURRENT]->key_frame) {
160
s->coeff_model_ract[pt][ct][cg][node] = def_prob[node];
162
model->coeff_ract[pt][ct][cg][node] = def_prob[node];
163
/* coeff_model_dcct is a linear combination of coeff_model_dccv */
165
/* coeff_dcct is a linear combination of coeff_dccv */
164
166
for (pt=0; pt<2; pt++)
165
167
for (ctx=0; ctx<36; ctx++)
166
168
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
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);
169
/* coeff_model_acct is a linear combination of coeff_model_ract */
171
/* coeff_acct is a linear combination of coeff_ract */
170
172
for (ct=0; ct<3; ct++)
171
173
for (pt=0; pt<2; pt++)
172
174
for (cg=0; cg<3; cg++)
173
175
for (ctx=0; ctx<6; ctx++)
174
176
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);
177
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);
178
180
static void vp5_parse_coeff(vp56_context_t *s)
180
182
vp56_range_coder_t *c = &s->c;
183
vp56_model_t *model = s->modelp;
181
184
uint8_t *permute = s->scantable.permutated;
182
uint8_t *model, *model2;
185
uint8_t *model1, *model2;
183
186
int coeff, sign, coeff_idx;
184
187
int b, i, cg, idx, ctx, ctx_last;
185
188
int pt = 0; /* plane type (0 for Y, 1 for U or V) */
192
195
ctx = 6*s->coeff_ctx[vp56_b6to4[b]][0]
193
196
+ 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
model1 = model->coeff_dccv[pt];
198
model2 = model->coeff_dcct[pt][ctx];
197
200
for (coeff_idx=0; coeff_idx<64; ) {
198
201
if (vp56_rac_get_prob(c, model2[0])) {
199
202
if (vp56_rac_get_prob(c, model2[2])) {
200
203
if (vp56_rac_get_prob(c, model2[3])) {
201
204
s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 4;
202
idx = vp56_rac_get_tree(c, vp56_pc_tree, model);
205
idx = vp56_rac_get_tree(c, vp56_pc_tree, model1);
203
206
sign = vp56_rac_get(c);
204
coeff = vp56_coeff_bias[idx];
207
coeff = vp56_coeff_bias[idx+5];
205
208
for (i=vp56_coeff_bit_length[idx]; i>=0; i--)
206
209
coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i;
208
211
if (vp56_rac_get_prob(c, model2[4])) {
209
coeff = 3 + vp56_rac_get_prob(c, model[5]);
212
coeff = 3 + vp56_rac_get_prob(c, model1[5]);
210
213
s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 3;
235
238
cg = vp5_coeff_groups[++coeff_idx];
236
239
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];
240
model1 = model->coeff_ract[pt][ct][cg];
241
model2 = cg > 2 ? model1 : model->coeff_acct[pt][ct][cg][ctx];
241
244
ctx_last = FFMIN(s->coeff_ctx_last[vp56_b6to4[b]], 24);
250
253
static void vp5_default_models_init(vp56_context_t *s)
255
vp56_model_t *model = s->modelp;
254
258
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;
259
model->vector_sig[i] = 0x80;
260
model->vector_dct[i] = 0x80;
261
model->vector_pdi[i][0] = 0x55;
262
model->vector_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
memcpy(model->mb_types_stats, vp56_def_mb_types_stats, sizeof(model->mb_types_stats));
265
memset(model->vector_pdv, 0x80, sizeof(model->vector_pdv));
264
static int vp5_decode_init(AVCodecContext *avctx)
268
static av_cold int vp5_decode_init(AVCodecContext *avctx)
266
270
vp56_context_t *s = avctx->priv_data;
268
vp56_init(s, avctx, 1);
272
vp56_init(avctx, 1, 0);
269
273
s->vp56_coord_div = vp5_coord_div;
270
274
s->parse_vector_adjustment = vp5_parse_vector_adjustment;
271
275
s->adjust = vp5_adjust;