comparison vorbis.c @ 4971:cb647e7b4b8b libavcodec

loosen vorbis_enc.c dependencies by spliting vorbis.c
author aurel
date Thu, 10 May 2007 15:17:17 +0000
parents 7595ead28402
children
comparison
equal deleted inserted replaced
4970:28b67304e5d0 4971:cb647e7b4b8b
164 /* Helper functions */ 164 /* Helper functions */
165 165
166 #define BARK(x) \ 166 #define BARK(x) \
167 (13.1f*atan(0.00074f*(x))+2.24f*atan(1.85e-8f*(x)*(x))+1e-4f*(x)) 167 (13.1f*atan(0.00074f*(x))+2.24f*atan(1.85e-8f*(x)*(x))+1e-4f*(x))
168 168
169 unsigned int ff_vorbis_nth_root(unsigned int x, unsigned int n) { // x^(1/n)
170 unsigned int ret=0, i, j;
171
172 do {
173 ++ret;
174 for(i=0,j=ret;i<n-1;i++) j*=ret;
175 } while (j<=x);
176
177 return (ret-1);
178 }
179
180 static float vorbisfloat2float(uint_fast32_t val) { 169 static float vorbisfloat2float(uint_fast32_t val) {
181 double mant=val&0x1fffff; 170 double mant=val&0x1fffff;
182 long exp=(val&0x7fe00000L)>>21; 171 long exp=(val&0x7fe00000L)>>21;
183 if (val&0x80000000) mant=-mant; 172 if (val&0x80000000) mant=-mant;
184 return(ldexp(mant, exp-20-768)); 173 return(ldexp(mant, exp-20-768));
185 } 174 }
186 175
187
188 // Generate vlc codes from vorbis huffman code lengths
189
190 int ff_vorbis_len2vlc(uint8_t *bits, uint32_t *codes, uint_fast32_t num) {
191 uint_fast32_t exit_at_level[33]={404,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
192 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
193
194 uint_fast8_t i,j;
195 uint_fast32_t code,p;
196
197 #ifdef V_DEBUG
198 GetBitContext gb;
199 #endif
200
201 for(p=0;(bits[p]==0) && (p<num);++p);
202 if (p==num) {
203 // av_log(vc->avccontext, AV_LOG_INFO, "An empty codebook. Heh?! \n");
204 return 0;
205 }
206
207 codes[p]=0;
208 for(i=0;i<bits[p];++i) {
209 exit_at_level[i+1]=1<<i;
210 }
211
212 #ifdef V_DEBUG
213 av_log(NULL, AV_LOG_INFO, " %d. of %d code len %d code %d - ", p, num, bits[p], codes[p]);
214 init_get_bits(&gb, (uint_fast8_t *)&codes[p], bits[p]);
215 for(i=0;i<bits[p];++i) {
216 av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
217 }
218 av_log(NULL, AV_LOG_INFO, "\n");
219 #endif
220
221 ++p;
222
223 for(;p<num;++p) {
224 if (bits[p]==0) continue;
225 // find corresponding exit(node which the tree can grow further from)
226 for(i=bits[p];i>0;--i) {
227 if (exit_at_level[i]) break;
228 }
229 if (!i) return 1; // overspecified tree
230 code=exit_at_level[i];
231 exit_at_level[i]=0;
232 // construct code (append 0s to end) and introduce new exits
233 for(j=i+1;j<=bits[p];++j) {
234 exit_at_level[j]=code+(1<<(j-1));
235 }
236 codes[p]=code;
237
238 #ifdef V_DEBUG
239 av_log(NULL, AV_LOG_INFO, " %d. code len %d code %d - ", p, bits[p], codes[p]);
240 init_get_bits(&gb, (uint_fast8_t *)&codes[p], bits[p]);
241 for(i=0;i<bits[p];++i) {
242 av_log(NULL, AV_LOG_INFO, "%s", get_bits1(&gb) ? "1" : "0");
243 }
244 av_log(NULL, AV_LOG_INFO, "\n");
245 #endif
246
247 }
248
249 //no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
250 for (p=1; p<33; p++)
251 if (exit_at_level[p]) return 1;
252
253 return 0;
254 }
255
256 void ff_vorbis_ready_floor1_list(floor1_entry_t * list, int values) {
257 int i;
258 list[0].sort = 0;
259 list[1].sort = 1;
260 for (i = 2; i < values; i++) {
261 int j;
262 list[i].low = 0;
263 list[i].high = 1;
264 list[i].sort = i;
265 for (j = 2; j < i; j++) {
266 int tmp = list[j].x;
267 if (tmp < list[i].x) {
268 if (tmp > list[list[i].low].x) list[i].low = j;
269 } else {
270 if (tmp < list[list[i].high].x) list[i].high = j;
271 }
272 }
273 }
274 for (i = 0; i < values - 1; i++) {
275 int j;
276 for (j = i + 1; j < values; j++) {
277 if (list[list[i].sort].x > list[list[j].sort].x) {
278 int tmp = list[i].sort;
279 list[i].sort = list[j].sort;
280 list[j].sort = tmp;
281 }
282 }
283 }
284 }
285 176
286 // Free all allocated memory ----------------------------------------- 177 // Free all allocated memory -----------------------------------------
287 178
288 static void vorbis_free(vorbis_context *vc) { 179 static void vorbis_free(vorbis_context *vc) {
289 int_fast16_t i; 180 int_fast16_t i;
1207 AV_DEBUG(" Floor0 decoded\n"); 1098 AV_DEBUG(" Floor0 decoded\n");
1208 1099
1209 return 0; 1100 return 0;
1210 } 1101 }
1211 1102
1212 static void render_line(int x0, int y0, int x1, int y1, float * buf, int n) {
1213 int dy = y1 - y0;
1214 int adx = x1 - x0;
1215 int ady = FFABS(dy);
1216 int base = dy / adx;
1217 int x = x0;
1218 int y = y0;
1219 int err = 0;
1220 int sy;
1221 if (dy < 0) sy = base - 1;
1222 else sy = base + 1;
1223 ady = ady - FFABS(base) * adx;
1224 if (x >= n) return;
1225 buf[x] = ff_vorbis_floor1_inverse_db_table[y];
1226 for (x = x0 + 1; x < x1; x++) {
1227 if (x >= n) return;
1228 err += ady;
1229 if (err >= adx) {
1230 err -= adx;
1231 y += sy;
1232 } else {
1233 y += base;
1234 }
1235 buf[x] = ff_vorbis_floor1_inverse_db_table[y];
1236 }
1237 }
1238
1239 void ff_vorbis_floor1_render_list(floor1_entry_t * list, int values, uint_fast16_t * y_list, int * flag, int multiplier, float * out, int samples) {
1240 int lx, ly, i;
1241 lx = 0;
1242 ly = y_list[0] * multiplier;
1243 for (i = 1; i < values; i++) {
1244 int pos = list[i].sort;
1245 if (flag[pos]) {
1246 render_line(lx, ly, list[pos].x, y_list[pos] * multiplier, out, samples);
1247 lx = list[pos].x;
1248 ly = y_list[pos] * multiplier;
1249 }
1250 if (lx >= samples) break;
1251 }
1252 if (lx < samples) render_line(lx, ly, samples, ly, out, samples);
1253 }
1254
1255 static uint_fast8_t vorbis_floor1_decode(vorbis_context *vc, vorbis_floor_data *vfu, float *vec) { 1103 static uint_fast8_t vorbis_floor1_decode(vorbis_context *vc, vorbis_floor_data *vfu, float *vec) {
1256 vorbis_floor1 * vf=&vfu->t1; 1104 vorbis_floor1 * vf=&vfu->t1;
1257 GetBitContext *gb=&vc->gb; 1105 GetBitContext *gb=&vc->gb;
1258 uint_fast16_t range_v[4]={ 256, 128, 86, 64 }; 1106 uint_fast16_t range_v[4]={ 256, 128, 86, 64 };
1259 uint_fast16_t range=range_v[vf->multiplier-1]; 1107 uint_fast16_t range=range_v[vf->multiplier-1];