Mercurial > libavcodec.hg
comparison vp5.c @ 3695:6795c9e5f983 libavcodec
VP5 and VP6 video decoder
author | aurel |
---|---|
date | Sat, 09 Sep 2006 17:19:37 +0000 |
parents | |
children | 596e62da8f29 |
comparison
equal
deleted
inserted
replaced
3694:8765ee4eaa45 | 3695:6795c9e5f983 |
---|---|
1 /** | |
2 * @file vp5.c | |
3 * VP5 compatible video decoder | |
4 * | |
5 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org> | |
6 * | |
7 * This library is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public | |
9 * License as published by the Free Software Foundation; either | |
10 * version 2.1 of the License, or (at your option) any later version. | |
11 * | |
12 * This library is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
18 * License along with this library; if not, write to the Free Software | |
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 */ | |
21 | |
22 #include <stdlib.h> | |
23 #include <string.h> | |
24 #include <inttypes.h> | |
25 | |
26 #include "avcodec.h" | |
27 #include "dsputil.h" | |
28 #include "bitstream.h" | |
29 #include "mpegvideo.h" | |
30 | |
31 #include "vp56.h" | |
32 #include "vp56data.h" | |
33 #include "vp5data.h" | |
34 | |
35 | |
36 static int vp5_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size, | |
37 int *golden_frame) | |
38 { | |
39 vp56_range_coder_t *c = &s->c; | |
40 int rows, cols; | |
41 | |
42 vp56_init_range_decoder(&s->c, buf, buf_size); | |
43 s->frames[VP56_FRAME_CURRENT].key_frame = !vp56_rac_get(c); | |
44 vp56_rac_get(c); | |
45 vp56_init_dequant(s, vp56_rac_gets(c, 6)); | |
46 if (s->frames[VP56_FRAME_CURRENT].key_frame) | |
47 { | |
48 vp56_rac_gets(c, 8); | |
49 if(vp56_rac_gets(c, 5) > 5) | |
50 return 0; | |
51 vp56_rac_gets(c, 2); | |
52 if (vp56_rac_get(c)) { | |
53 av_log(s->avctx, AV_LOG_ERROR, "interlacing not supported\n"); | |
54 return 0; | |
55 } | |
56 rows = vp56_rac_gets(c, 8); /* number of stored macroblock rows */ | |
57 cols = vp56_rac_gets(c, 8); /* number of stored macroblock cols */ | |
58 vp56_rac_gets(c, 8); /* number of displayed macroblock rows */ | |
59 vp56_rac_gets(c, 8); /* number of displayed macroblock cols */ | |
60 vp56_rac_gets(c, 2); | |
61 if (16*cols != s->avctx->coded_width || | |
62 16*rows != s->avctx->coded_height) { | |
63 avcodec_set_dimensions(s->avctx, 16*cols, 16*rows); | |
64 return 2; | |
65 } | |
66 } | |
67 return 1; | |
68 } | |
69 | |
70 /* Gives very similar result than the vp6 version except in a few cases */ | |
71 static int vp5_adjust(int v, int t) | |
72 { | |
73 int s2, s1 = v >> 31; | |
74 v ^= s1; | |
75 v -= s1; | |
76 v *= v < 2*t; | |
77 v -= t; | |
78 s2 = v >> 31; | |
79 v ^= s2; | |
80 v -= s2; | |
81 v = t - v; | |
82 v += s1; | |
83 v ^= s1; | |
84 return v; | |
85 } | |
86 | |
87 static void vp5_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vector) | |
88 { | |
89 vp56_range_coder_t *c = &s->c; | |
90 int comp, di; | |
91 | |
92 for (comp=0; comp<2; comp++) { | |
93 int delta = 0; | |
94 if (vp56_rac_get_prob(c, s->vector_model_dct[comp])) { | |
95 int sign = vp56_rac_get_prob(c, s->vector_model_sig[comp]); | |
96 di = vp56_rac_get_prob(c, s->vector_model_pdi[comp][0]); | |
97 di |= vp56_rac_get_prob(c, s->vector_model_pdi[comp][1]) << 1; | |
98 delta = vp56_rac_get_tree(c, vp56_pva_tree, | |
99 s->vector_model_pdv[comp]); | |
100 delta = di | (delta << 2); | |
101 delta = (delta ^ -sign) + sign; | |
102 } | |
103 if (!comp) | |
104 vector->x = delta; | |
105 else | |
106 vector->y = delta; | |
107 } | |
108 } | |
109 | |
110 static void vp5_parse_vector_models(vp56_context_t *s) | |
111 { | |
112 vp56_range_coder_t *c = &s->c; | |
113 int comp, node; | |
114 | |
115 for (comp=0; comp<2; comp++) { | |
116 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][0])) | |
117 s->vector_model_dct[comp] = vp56_rac_gets_nn(c, 7); | |
118 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][1])) | |
119 s->vector_model_sig[comp] = vp56_rac_gets_nn(c, 7); | |
120 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][2])) | |
121 s->vector_model_pdi[comp][0] = vp56_rac_gets_nn(c, 7); | |
122 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][3])) | |
123 s->vector_model_pdi[comp][1] = vp56_rac_gets_nn(c, 7); | |
124 } | |
125 | |
126 for (comp=0; comp<2; comp++) | |
127 for (node=0; node<7; node++) | |
128 if (vp56_rac_get_prob(c, vp5_vmc_pct[comp][4 + node])) | |
129 s->vector_model_pdv[comp][node] = vp56_rac_gets_nn(c, 7); | |
130 } | |
131 | |
132 static void vp5_parse_coeff_models(vp56_context_t *s) | |
133 { | |
134 vp56_range_coder_t *c = &s->c; | |
135 uint8_t def_prob[11]; | |
136 int node, cg, ctx; | |
137 int ct; /* code type */ | |
138 int pt; /* plane type (0 for Y, 1 for U or V) */ | |
139 | |
140 memset(def_prob, 0x80, sizeof(def_prob)); | |
141 | |
142 for (pt=0; pt<2; pt++) | |
143 for (node=0; node<11; node++) | |
144 if (vp56_rac_get_prob(c, vp5_dccv_pct[pt][node])) { | |
145 def_prob[node] = vp56_rac_gets_nn(c, 7); | |
146 s->coeff_model_dccv[pt][node] = def_prob[node]; | |
147 } else if (s->frames[VP56_FRAME_CURRENT].key_frame) { | |
148 s->coeff_model_dccv[pt][node] = def_prob[node]; | |
149 } | |
150 | |
151 for (ct=0; ct<3; ct++) | |
152 for (pt=0; pt<2; pt++) | |
153 for (cg=0; cg<6; cg++) | |
154 for (node=0; node<11; node++) | |
155 if (vp56_rac_get_prob(c, vp5_ract_pct[ct][pt][cg][node])) { | |
156 def_prob[node] = vp56_rac_gets_nn(c, 7); | |
157 s->coeff_model_ract[pt][ct][cg][node] = def_prob[node]; | |
158 } else if (s->frames[VP56_FRAME_CURRENT].key_frame) { | |
159 s->coeff_model_ract[pt][ct][cg][node] = def_prob[node]; | |
160 } | |
161 | |
162 /* coeff_model_dcct is a linear combination of coeff_model_dccv */ | |
163 for (pt=0; pt<2; pt++) | |
164 for (ctx=0; ctx<36; ctx++) | |
165 for (node=0; node<5; node++) | |
166 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); | |
167 | |
168 /* coeff_model_acct is a linear combination of coeff_model_ract */ | |
169 for (ct=0; ct<3; ct++) | |
170 for (pt=0; pt<2; pt++) | |
171 for (cg=0; cg<3; cg++) | |
172 for (ctx=0; ctx<6; ctx++) | |
173 for (node=0; node<5; node++) | |
174 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); | |
175 } | |
176 | |
177 static void vp5_parse_coeff(vp56_context_t *s) | |
178 { | |
179 vp56_range_coder_t *c = &s->c; | |
180 uint8_t *permute = s->scantable.permutated; | |
181 uint8_t *model, *model2; | |
182 int coeff, sign, coeff_idx; | |
183 int b, i, cg, idx, ctx, ctx_last; | |
184 int pt = 0; /* plane type (0 for Y, 1 for U or V) */ | |
185 | |
186 for (b=0; b<6; b++) { | |
187 int ct = 1; /* code type */ | |
188 | |
189 if (b > 3) pt = 1; | |
190 | |
191 ctx = 6*s->coeff_ctx[vp56_b6to4[b]][0] | |
192 + s->above_blocks[s->above_block_idx[b]].not_null_dc; | |
193 model = s->coeff_model_dccv[pt]; | |
194 model2 = s->coeff_model_dcct[pt][ctx]; | |
195 | |
196 for (coeff_idx=0; coeff_idx<64; ) { | |
197 if (vp56_rac_get_prob(c, model2[0])) { | |
198 if (vp56_rac_get_prob(c, model2[2])) { | |
199 if (vp56_rac_get_prob(c, model2[3])) { | |
200 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 4; | |
201 idx = vp56_rac_get_tree(c, vp56_pc_tree, model); | |
202 sign = vp56_rac_get(c); | |
203 coeff = vp56_coeff_bias[idx]; | |
204 for (i=vp56_coeff_bit_length[idx]; i>=0; i--) | |
205 coeff += vp56_rac_get_prob(c, vp56_coeff_parse_table[idx][i]) << i; | |
206 } else { | |
207 if (vp56_rac_get_prob(c, model2[4])) { | |
208 coeff = 3 + vp56_rac_get_prob(c, model[5]); | |
209 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 3; | |
210 } else { | |
211 coeff = 2; | |
212 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 2; | |
213 } | |
214 sign = vp56_rac_get(c); | |
215 } | |
216 ct = 2; | |
217 } else { | |
218 ct = 1; | |
219 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 1; | |
220 sign = vp56_rac_get(c); | |
221 coeff = 1; | |
222 } | |
223 coeff = (coeff ^ -sign) + sign; | |
224 if (coeff_idx) | |
225 coeff *= s->dequant_ac; | |
226 s->block_coeff[b][permute[coeff_idx]] = coeff; | |
227 } else { | |
228 if (ct && !vp56_rac_get_prob(c, model2[1])) | |
229 break; | |
230 ct = 0; | |
231 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 0; | |
232 } | |
233 | |
234 cg = vp5_coeff_groups[++coeff_idx]; | |
235 ctx = s->coeff_ctx[vp56_b6to4[b]][coeff_idx]; | |
236 model = s->coeff_model_ract[pt][ct][cg]; | |
237 model2 = cg > 2 ? model : s->coeff_model_acct[pt][ct][cg][ctx]; | |
238 } | |
239 | |
240 ctx_last = FFMIN(s->coeff_ctx_last[vp56_b6to4[b]], 24); | |
241 s->coeff_ctx_last[vp56_b6to4[b]] = coeff_idx; | |
242 if (coeff_idx < ctx_last) | |
243 for (i=coeff_idx; i<=ctx_last; i++) | |
244 s->coeff_ctx[vp56_b6to4[b]][i] = 5; | |
245 s->above_blocks[s->above_block_idx[b]].not_null_dc = s->coeff_ctx[vp56_b6to4[b]][0]; | |
246 } | |
247 } | |
248 | |
249 static void vp5_default_models_init(vp56_context_t *s) | |
250 { | |
251 int i; | |
252 | |
253 for (i=0; i<2; i++) { | |
254 s->vector_model_sig[i] = 0x80; | |
255 s->vector_model_dct[i] = 0x80; | |
256 s->vector_model_pdi[i][0] = 0x55; | |
257 s->vector_model_pdi[i][1] = 0x80; | |
258 } | |
259 memcpy(s->mb_types_stats, vp56_def_mb_types_stats, sizeof(s->mb_types_stats)); | |
260 memset(s->vector_model_pdv, 0x80, sizeof(s->vector_model_pdv)); | |
261 } | |
262 | |
263 static int vp5_decode_init(AVCodecContext *avctx) | |
264 { | |
265 vp56_context_t *s = avctx->priv_data; | |
266 | |
267 vp56_init(s, avctx, 1); | |
268 s->vp56_coord_div = vp5_coord_div; | |
269 s->parse_vector_adjustment = vp5_parse_vector_adjustment; | |
270 s->adjust = vp5_adjust; | |
271 s->parse_coeff = vp5_parse_coeff; | |
272 s->default_models_init = vp5_default_models_init; | |
273 s->parse_vector_models = vp5_parse_vector_models; | |
274 s->parse_coeff_models = vp5_parse_coeff_models; | |
275 s->parse_header = vp5_parse_header; | |
276 | |
277 return 0; | |
278 } | |
279 | |
280 AVCodec vp5_decoder = { | |
281 "vp5", | |
282 CODEC_TYPE_VIDEO, | |
283 CODEC_ID_VP5, | |
284 sizeof(vp56_context_t), | |
285 vp5_decode_init, | |
286 NULL, | |
287 vp56_free, | |
288 vp56_decode_frame, | |
289 }; |