Mercurial > libavcodec.hg
annotate vp5.c @ 3974:93746612bc78 libavcodec
optimize branchless C CABAC decoder
author | michael |
---|---|
date | Mon, 09 Oct 2006 20:44:11 +0000 |
parents | c8c591fe26f8 |
children | 6a10cc9adb8a |
rev | line source |
---|---|
3695 | 1 /** |
2 * @file vp5.c | |
3 * VP5 compatible video decoder | |
4 * | |
5 * Copyright (C) 2006 Aurelien Jacobs <aurel@gnuage.org> | |
6 * | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3697
diff
changeset
|
7 * This file is part of FFmpeg. |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3697
diff
changeset
|
8 * |
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3697
diff
changeset
|
9 * FFmpeg is free software; you can redistribute it and/or |
3695 | 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. | |
13 * | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3697
diff
changeset
|
14 * FFmpeg is distributed in the hope that it will be useful, |
3695 | 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. | |
18 * | |
19 * You should have received a copy of the GNU Lesser General Public | |
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3697
diff
changeset
|
20 * License along with FFmpeg; if not, write to the Free Software |
3695 | 21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
22 */ | |
23 | |
24 #include <stdlib.h> | |
25 #include <string.h> | |
26 #include <inttypes.h> | |
27 | |
28 #include "avcodec.h" | |
29 #include "dsputil.h" | |
30 #include "bitstream.h" | |
31 #include "mpegvideo.h" | |
32 | |
33 #include "vp56.h" | |
34 #include "vp56data.h" | |
35 #include "vp5data.h" | |
36 | |
37 | |
38 static int vp5_parse_header(vp56_context_t *s, uint8_t *buf, int buf_size, | |
39 int *golden_frame) | |
40 { | |
41 vp56_range_coder_t *c = &s->c; | |
42 int rows, cols; | |
43 | |
44 vp56_init_range_decoder(&s->c, buf, buf_size); | |
45 s->frames[VP56_FRAME_CURRENT].key_frame = !vp56_rac_get(c); | |
46 vp56_rac_get(c); | |
47 vp56_init_dequant(s, vp56_rac_gets(c, 6)); | |
48 if (s->frames[VP56_FRAME_CURRENT].key_frame) | |
49 { | |
50 vp56_rac_gets(c, 8); | |
51 if(vp56_rac_gets(c, 5) > 5) | |
52 return 0; | |
53 vp56_rac_gets(c, 2); | |
54 if (vp56_rac_get(c)) { | |
55 av_log(s->avctx, AV_LOG_ERROR, "interlacing not supported\n"); | |
56 return 0; | |
57 } | |
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 */ | |
62 vp56_rac_gets(c, 2); | |
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); | |
66 return 2; | |
67 } | |
68 } | |
69 return 1; | |
70 } | |
71 | |
72 /* Gives very similar result than the vp6 version except in a few cases */ | |
73 static int vp5_adjust(int v, int t) | |
74 { | |
75 int s2, s1 = v >> 31; | |
76 v ^= s1; | |
77 v -= s1; | |
78 v *= v < 2*t; | |
79 v -= t; | |
80 s2 = v >> 31; | |
81 v ^= s2; | |
82 v -= s2; | |
83 v = t - v; | |
84 v += s1; | |
85 v ^= s1; | |
86 return v; | |
87 } | |
88 | |
3697 | 89 static void vp5_parse_vector_adjustment(vp56_context_t *s, vp56_mv_t *vect) |
3695 | 90 { |
91 vp56_range_coder_t *c = &s->c; | |
92 int comp, di; | |
93 | |
94 for (comp=0; comp<2; comp++) { | |
95 int delta = 0; | |
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; | |
104 } | |
105 if (!comp) | |
3697 | 106 vect->x = delta; |
3695 | 107 else |
3697 | 108 vect->y = delta; |
3695 | 109 } |
110 } | |
111 | |
112 static void vp5_parse_vector_models(vp56_context_t *s) | |
113 { | |
114 vp56_range_coder_t *c = &s->c; | |
115 int comp, node; | |
116 | |
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); | |
126 } | |
127 | |
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); | |
132 } | |
133 | |
134 static void vp5_parse_coeff_models(vp56_context_t *s) | |
135 { | |
136 vp56_range_coder_t *c = &s->c; | |
137 uint8_t def_prob[11]; | |
138 int node, cg, ctx; | |
139 int ct; /* code type */ | |
140 int pt; /* plane type (0 for Y, 1 for U or V) */ | |
141 | |
142 memset(def_prob, 0x80, sizeof(def_prob)); | |
143 | |
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]; | |
151 } | |
152 | |
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]; | |
162 } | |
163 | |
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); | |
169 | |
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); | |
177 } | |
178 | |
179 static void vp5_parse_coeff(vp56_context_t *s) | |
180 { | |
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) */ | |
187 | |
188 for (b=0; b<6; b++) { | |
189 int ct = 1; /* code type */ | |
190 | |
191 if (b > 3) pt = 1; | |
192 | |
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]; | |
197 | |
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; | |
208 } else { | |
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; | |
212 } else { | |
213 coeff = 2; | |
214 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 2; | |
215 } | |
216 sign = vp56_rac_get(c); | |
217 } | |
218 ct = 2; | |
219 } else { | |
220 ct = 1; | |
221 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 1; | |
222 sign = vp56_rac_get(c); | |
223 coeff = 1; | |
224 } | |
225 coeff = (coeff ^ -sign) + sign; | |
226 if (coeff_idx) | |
227 coeff *= s->dequant_ac; | |
228 s->block_coeff[b][permute[coeff_idx]] = coeff; | |
229 } else { | |
230 if (ct && !vp56_rac_get_prob(c, model2[1])) | |
231 break; | |
232 ct = 0; | |
233 s->coeff_ctx[vp56_b6to4[b]][coeff_idx] = 0; | |
234 } | |
235 | |
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]; | |
240 } | |
241 | |
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]; | |
248 } | |
249 } | |
250 | |
251 static void vp5_default_models_init(vp56_context_t *s) | |
252 { | |
253 int i; | |
254 | |
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; | |
260 } | |
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)); | |
263 } | |
264 | |
265 static int vp5_decode_init(AVCodecContext *avctx) | |
266 { | |
267 vp56_context_t *s = avctx->priv_data; | |
268 | |
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; | |
278 | |
279 return 0; | |
280 } | |
281 | |
282 AVCodec vp5_decoder = { | |
283 "vp5", | |
284 CODEC_TYPE_VIDEO, | |
285 CODEC_ID_VP5, | |
286 sizeof(vp56_context_t), | |
287 vp5_decode_init, | |
288 NULL, | |
289 vp56_free, | |
290 vp56_decode_frame, | |
291 }; |