Debian Codegraph @

external/lame/util.c

raw symbols SCIP
1 /*
2 * lame utility library source file
3 *
4 * Copyright (c) 1999 Albert L Faber
5 * Copyright (c) 2000-2005 Alexander Leidinger
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 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 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23 /* $Id: util.c,v 1.159 2017/09/06 15:07:30 robert Exp $ */
24
25 //#ifdef HAVE_CONFIG_H
26 #include "lame__config.h"
27 //#endif
28
29 #include <float.h>
30 #include "lame.h"
31 #include "machine.h"
32 #include "encoder.h"
33 #include "util.h"
34 #include "tables.h"
35
36 #define PRECOMPUTE
37 #if defined(__FreeBSD__) && !defined(__alpha__)
38 # include <machine/floatingpoint.h>
39 #endif
40
41
42 /***********************************************************************
43 *
44 * Global Function Definitions
45 *
46 ***********************************************************************/
47 /*empty and close mallocs in gfc */
48
49 void
50 free_id3tag(lame_internal_flags * const gfc)
51 {
52 gfc->tag_spec.language[0] = 0;
53 if (gfc->tag_spec.title != 0) {
54 free(gfc->tag_spec.title);
55 gfc->tag_spec.title = 0;
56 }
57 if (gfc->tag_spec.artist != 0) {
58 free(gfc->tag_spec.artist);
59 gfc->tag_spec.artist = 0;
60 }
61 if (gfc->tag_spec.album != 0) {
62 free(gfc->tag_spec.album);
63 gfc->tag_spec.album = 0;
64 }
65 if (gfc->tag_spec.comment != 0) {
66 free(gfc->tag_spec.comment);
67 gfc->tag_spec.comment = 0;
68 }
69
70 if (gfc->tag_spec.albumart != 0) {
71 free(gfc->tag_spec.albumart);
72 gfc->tag_spec.albumart = 0;
73 gfc->tag_spec.albumart_size = 0;
74 gfc->tag_spec.albumart_mimetype = MIMETYPE_NONE;
75 }
76 if (gfc->tag_spec.v2_head != 0) {
77 FrameDataNode *node = gfc->tag_spec.v2_head;
78 do {
79 void *p = node->dsc.ptr.b;
80 void *q = node->txt.ptr.b;
81 void *r = node;
82 node = node->nxt;
83 free(p);
84 free(q);
85 free(r);
86 } while (node != 0);
87 gfc->tag_spec.v2_head = 0;
88 gfc->tag_spec.v2_tail = 0;
89 }
90 }
91
92
93 static void
94 free_global_data(lame_internal_flags * gfc)
95 {
96 if (gfc && gfc->cd_psy) {
97 if (gfc->cd_psy->l.s3) {
98 /* XXX allocated in psymodel_init() */
99 free(gfc->cd_psy->l.s3);
100 }
101 if (gfc->cd_psy->s.s3) {
102 /* XXX allocated in psymodel_init() */
103 free(gfc->cd_psy->s.s3);
104 }
105 free(gfc->cd_psy);
106 gfc->cd_psy = 0;
107 }
108 }
109
110
111 void
112 freegfc(lame_internal_flags * const gfc)
113 { /* bit stream structure */
114 int i;
115
116 if (gfc == 0) return;
117
118 for (i = 0; i <= 2 * BPC; i++)
119 if (gfc->sv_enc.blackfilt[i] != NULL) {
120 free(gfc->sv_enc.blackfilt[i]);
121 gfc->sv_enc.blackfilt[i] = NULL;
122 }
123 if (gfc->sv_enc.inbuf_old[0]) {
124 free(gfc->sv_enc.inbuf_old[0]);
125 gfc->sv_enc.inbuf_old[0] = NULL;
126 }
127 if (gfc->sv_enc.inbuf_old[1]) {
128 free(gfc->sv_enc.inbuf_old[1]);
129 gfc->sv_enc.inbuf_old[1] = NULL;
130 }
131
132 if (gfc->bs.buf != NULL) {
133 free(gfc->bs.buf);
134 gfc->bs.buf = NULL;
135 }
136
137 if (gfc->VBR_seek_table.bag) {
138 free(gfc->VBR_seek_table.bag);
139 gfc->VBR_seek_table.bag = NULL;
140 gfc->VBR_seek_table.size = 0;
141 }
142 if (gfc->ATH) {
143 free(gfc->ATH);
144 }
145 if (gfc->sv_rpg.rgdata) {
146 free(gfc->sv_rpg.rgdata);
147 }
148 if (gfc->sv_enc.in_buffer_0) {
149 free(gfc->sv_enc.in_buffer_0);
150 }
151 if (gfc->sv_enc.in_buffer_1) {
152 free(gfc->sv_enc.in_buffer_1);
153 }
154 free_id3tag(gfc);
155
156 #ifdef DECODE_ON_THE_FLY
157 if (gfc->hip) {
158 hip_decode_exit(gfc->hip);
159 gfc->hip = 0;
160 }
161 #endif
162
163 free_global_data(gfc);
164
165 free(gfc);
166 }
167
168 void
169 calloc_aligned(aligned_pointer_t * ptr, unsigned int size, unsigned int bytes)
170 {
171 if (ptr) {
172 if (!ptr->pointer) {
173 ptr->pointer = malloc(size + bytes);
174 if (ptr->pointer != 0) {
175 memset(ptr->pointer, 0, size + bytes);
176 if (bytes > 0) {
177 ptr->aligned = (void *) ((((size_t) ptr->pointer + bytes - 1) / bytes) * bytes);
178 }
179 else {
180 ptr->aligned = ptr->pointer;
181 }
182 }
183 else {
184 ptr->aligned = 0;
185 }
186 }
187 }
188 }
189
190 void
191 free_aligned(aligned_pointer_t * ptr)
192 {
193 if (ptr) {
194 if (ptr->pointer) {
195 free(ptr->pointer);
196 ptr->pointer = 0;
197 ptr->aligned = 0;
198 }
199 }
200 }
201
202 /*those ATH formulas are returning
203 their minimum value for input = -1*/
204
205 static FLOAT
206 ATHformula_GB(FLOAT f, FLOAT value, FLOAT f_min, FLOAT f_max)
207 {
208 /* from Painter & Spanias
209 modified by Gabriel Bouvigne to better fit the reality
210 ath = 3.640 * pow(f,-0.8)
211 - 6.800 * exp(-0.6*pow(f-3.4,2.0))
212 + 6.000 * exp(-0.15*pow(f-8.7,2.0))
213 + 0.6* 0.001 * pow(f,4.0);
214
215
216 In the past LAME was using the Painter &Spanias formula.
217 But we had some recurrent problems with HF content.
218 We measured real ATH values, and found the older formula
219 to be inacurate in the higher part. So we made this new
220 formula and this solved most of HF problematic testcases.
221 The tradeoff is that in VBR mode it increases a lot the
222 bitrate. */
223
224
225 /*this curve can be udjusted according to the VBR scale:
226 it adjusts from something close to Painter & Spanias
227 on V9 up to Bouvigne's formula for V0. This way the VBR
228 bitrate is more balanced according to the -V value.*/
229
230 FLOAT ath;
231
232 /* the following Hack allows to ask for the lowest value */
233 if (f < -.3)
234 f = 3410;
235
236 f /= 1000; /* convert to khz */
237 f = Max(f_min, f);
238 f = Min(f_max, f);
239
240 ath = 3.640 * pow(f, -0.8)
241 - 6.800 * exp(-0.6 * pow(f - 3.4, 2.0))
242 + 6.000 * exp(-0.15 * pow(f - 8.7, 2.0))
243 + (0.6 + 0.04 * value) * 0.001 * pow(f, 4.0);
244 return ath;
245 }
246
247
248
249 FLOAT
250 ATHformula(SessionConfig_t const *cfg, FLOAT f)
251 {
252 FLOAT ath;
253 switch (cfg->ATHtype) {
254 case 0:
255 ath = ATHformula_GB(f, 9, 0.1f, 24.0f);
256 break;
257 case 1:
258 ath = ATHformula_GB(f, -1, 0.1f, 24.0f); /*over sensitive, should probably be removed */
259 break;
260 case 2:
261 ath = ATHformula_GB(f, 0, 0.1f, 24.0f);
262 break;
263 case 3:
264 ath = ATHformula_GB(f, 1, 0.1f, 24.0f) + 6; /*modification of GB formula by Roel */
265 break;
266 case 4:
267 ath = ATHformula_GB(f, cfg->ATHcurve, 0.1f, 24.0f);
268 break;
269 case 5:
270 ath = ATHformula_GB(f, cfg->ATHcurve, 3.41f, 16.1f);
271 break;
272 default:
273 ath = ATHformula_GB(f, 0, 0.1f, 24.0f);
274 break;
275 }
276 return ath;
277 }
278
279 /* see for example "Zwicker: Psychoakustik, 1982; ISBN 3-540-11401-7 */
280 FLOAT
281 freq2bark(FLOAT freq)
282 {
283 /* input: freq in hz output: barks */
284 if (freq < 0)
285 freq = 0;
286 freq = freq * 0.001;
287 return 13.0 * atan(.76 * freq) + 3.5 * atan(freq * freq / (7.5 * 7.5));
288 }
289
290 #if 0
291 extern FLOAT freq2cbw(FLOAT freq);
292
293 /* see for example "Zwicker: Psychoakustik, 1982; ISBN 3-540-11401-7 */
294 FLOAT
295 freq2cbw(FLOAT freq)
296 {
297 /* input: freq in hz output: critical band width */
298 freq = freq * 0.001;
299 return 25 + 75 * pow(1 + 1.4 * (freq * freq), 0.69);
300 }
301
302 #endif
303
304
305
306
307 #define ABS(A) (((A)>0) ? (A) : -(A))
308
309 int
310 FindNearestBitrate(int bRate, /* legal rates from 8 to 320 */
311 int version, int samplerate)
312 { /* MPEG-1 or MPEG-2 LSF */
313 int bitrate;
314 int i;
315
316 if (samplerate < 16000)
317 version = 2;
318
319 bitrate = bitrate_table[version][1];
320
321 for (i = 2; i <= 14; i++) {
322 if (bitrate_table[version][i] > 0) {
323 if (ABS(bitrate_table[version][i] - bRate) < ABS(bitrate - bRate))
324 bitrate = bitrate_table[version][i];
325 }
326 }
327 return bitrate;
328 }
329
330
331
332
333
334 #ifndef Min
335 #define Min(A, B) ((A) < (B) ? (A) : (B))
336 #endif
337 #ifndef Max
338 #define Max(A, B) ((A) > (B) ? (A) : (B))
339 #endif
340
341
342 /* Used to find table index when
343 * we need bitrate-based values
344 * determined using tables
345 *
346 * bitrate in kbps
347 *
348 * Gabriel Bouvigne 2002-11-03
349 */
350 int
351 nearestBitrateFullIndex(uint16_t bitrate)
352 {
353 /* borrowed from DM abr presets */
354
355 const int full_bitrate_table[] =
356 { 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 };
357
358
359 int lower_range = 0, lower_range_kbps = 0, upper_range = 0, upper_range_kbps = 0;
360
361
362 int b;
363
364
365 /* We assume specified bitrate will be 320kbps */
366 upper_range_kbps = full_bitrate_table[16];
367 upper_range = 16;
368 lower_range_kbps = full_bitrate_table[16];
369 lower_range = 16;
370
371 /* Determine which significant bitrates the value specified falls between,
372 * if loop ends without breaking then we were correct above that the value was 320
373 */
374 for (b = 0; b < 16; b++) {
375 if ((Max(bitrate, full_bitrate_table[b + 1])) != bitrate) {
376 upper_range_kbps = full_bitrate_table[b + 1];
377 upper_range = b + 1;
378 lower_range_kbps = full_bitrate_table[b];
379 lower_range = (b);
380 break; /* We found upper range */
381 }
382 }
383
384 /* Determine which range the value specified is closer to */
385 if ((upper_range_kbps - bitrate) > (bitrate - lower_range_kbps)) {
386 return lower_range;
387 }
388 return upper_range;
389 }
390
391
392
393
394
395 /* map frequency to a valid MP3 sample frequency
396 *
397 * Robert Hegemann 2000-07-01
398 */
399 int
400 map2MP3Frequency(int freq)
401 {
402 if (freq <= 8000)
403 return 8000;
404 if (freq <= 11025)
405 return 11025;
406 if (freq <= 12000)
407 return 12000;
408 if (freq <= 16000)
409 return 16000;
410 if (freq <= 22050)
411 return 22050;
412 if (freq <= 24000)
413 return 24000;
414 if (freq <= 32000)
415 return 32000;
416 if (freq <= 44100)
417 return 44100;
418
419 return 48000;
420 }
421
422 int
423 BitrateIndex(int bRate, /* legal rates from 32 to 448 kbps */
424 int version, /* MPEG-1 or MPEG-2/2.5 LSF */
425 int samplerate)
426 { /* convert bitrate in kbps to index */
427 int i;
428 if (samplerate < 16000)
429 version = 2;
430 for (i = 0; i <= 14; i++) {
431 if (bitrate_table[version][i] > 0) {
432 if (bitrate_table[version][i] == bRate) {
433 return i;
434 }
435 }
436 }
437 return -1;
438 }
439
440 /* convert samp freq in Hz to index */
441
442 int
443 SmpFrqIndex(int sample_freq, int *const version)
444 {
445 switch (sample_freq) {
446 case 44100:
447 *version = 1;
448 return 0;
449 case 48000:
450 *version = 1;
451 return 1;
452 case 32000:
453 *version = 1;
454 return 2;
455 case 22050:
456 *version = 0;
457 return 0;
458 case 24000:
459 *version = 0;
460 return 1;
461 case 16000:
462 *version = 0;
463 return 2;
464 case 11025:
465 *version = 0;
466 return 0;
467 case 12000:
468 *version = 0;
469 return 1;
470 case 8000:
471 *version = 0;
472 return 2;
473 default:
474 *version = 0;
475 return -1;
476 }
477 }
478
479
480 /*****************************************************************************
481 *
482 * End of bit_stream.c package
483 *
484 *****************************************************************************/
485
486
487
488
489
490
491
492
493
494
495 /* resampling via FIR filter, blackman window */
496 inline static FLOAT
497 blackman(FLOAT x, FLOAT fcn, int l)
498 {
499 /* This algorithm from:
500 SIGNAL PROCESSING ALGORITHMS IN FORTRAN AND C
501 S.D. Stearns and R.A. David, Prentice-Hall, 1992
502 */
503 FLOAT bkwn, x2;
504 FLOAT const wcn = (PI * fcn);
505
506 x /= l;
507 if (x < 0)
508 x = 0;
509 if (x > 1)
510 x = 1;
511 x2 = x - .5;
512
513 bkwn = 0.42 - 0.5 * cos(2 * x * PI) + 0.08 * cos(4 * x * PI);
514 if (fabs(x2) < 1e-9)
515 return wcn / PI;
516 else
517 return (bkwn * sin(l * wcn * x2) / (PI * l * x2));
518
519
520 }
521
522
523
524
525 /* gcd - greatest common divisor */
526 /* Joint work of Euclid and M. Hendry */
527
528 static int
529 gcd(int i, int j)
530 {
531 /* assert ( i > 0 && j > 0 ); */
532 return j ? gcd(j, i % j) : i;
533 }
534
535
536
537 static int
538 fill_buffer_resample(lame_internal_flags * gfc,
539 sample_t * outbuf,
540 int desired_len, sample_t const *inbuf, int len, int *num_used, int ch)
541 {
542 SessionConfig_t const *const cfg = &gfc->cfg;
543 EncStateVar_t *esv = &gfc->sv_enc;
544 double resample_ratio = (double)cfg->samplerate_in / (double)cfg->samplerate_out;
545 int BLACKSIZE;
546 FLOAT offset, xvalue;
547 int i, j = 0, k;
548 int filter_l;
549 FLOAT fcn, intratio;
550 FLOAT *inbuf_old;
551 int bpc; /* number of convolution functions to pre-compute */
552 bpc = cfg->samplerate_out / gcd(cfg->samplerate_out, cfg->samplerate_in);
553 if (bpc > BPC)
554 bpc = BPC;
555
556 intratio = (fabs(resample_ratio - floor(.5 + resample_ratio)) < FLT_EPSILON);
557 fcn = 1.00 / resample_ratio;
558 if (fcn > 1.00)
559 fcn = 1.00;
560 filter_l = 31; /* must be odd */
561 filter_l += intratio; /* unless resample_ratio=int, it must be even */
562
563
564 BLACKSIZE = filter_l + 1; /* size of data needed for FIR */
565
566 if (gfc->fill_buffer_resample_init == 0) {
567 esv->inbuf_old[0] = lame_calloc(sample_t, BLACKSIZE);
568 esv->inbuf_old[1] = lame_calloc(sample_t, BLACKSIZE);
569 for (i = 0; i <= 2 * bpc; ++i)
570 esv->blackfilt[i] = lame_calloc(sample_t, BLACKSIZE);
571
572 esv->itime[0] = 0;
573 esv->itime[1] = 0;
574
575 /* precompute blackman filter coefficients */
576 for (j = 0; j <= 2 * bpc; j++) {
577 FLOAT sum = 0.;
578 offset = (j - bpc) / (2. * bpc);
579 for (i = 0; i <= filter_l; i++)
580 sum += esv->blackfilt[j][i] = blackman(i - offset, fcn, filter_l);
581 for (i = 0; i <= filter_l; i++)
582 esv->blackfilt[j][i] /= sum;
583 }
584 gfc->fill_buffer_resample_init = 1;
585 }
586
587 inbuf_old = esv->inbuf_old[ch];
588
589 /* time of j'th element in inbuf = itime + j/ifreq; */
590 /* time of k'th element in outbuf = j/ofreq */
591 for (k = 0; k < desired_len; k++) {
592 double time0 = k * resample_ratio; /* time of k'th output sample */
593 int joff;
594
595 j = floor(time0 - esv->itime[ch]);
596
597 /* check if we need more input data */
598 if ((filter_l + j - filter_l / 2) >= len)
599 break;
600
601 /* blackman filter. by default, window centered at j+.5(filter_l%2) */
602 /* but we want a window centered at time0. */
603 offset = (time0 - esv->itime[ch] - (j + .5 * (filter_l % 2)));
604 assert(fabs(offset) <= .501);
605
606 /* find the closest precomputed window for this offset: */
607 joff = floor((offset * 2 * bpc) + bpc + .5);
608
609 xvalue = 0.;
610 for (i = 0; i <= filter_l; ++i) {
611 int const j2 = i + j - filter_l / 2;
612 sample_t y;
613 assert(j2 < len);
614 assert(j2 + BLACKSIZE >= 0);
615 y = (j2 < 0) ? inbuf_old[BLACKSIZE + j2] : inbuf[j2];
616 #ifdef PRECOMPUTE
617 xvalue += y * esv->blackfilt[joff][i];
618 #else
619 xvalue += y * blackman(i - offset, fcn, filter_l); /* very slow! */
620 #endif
621 }
622 outbuf[k] = xvalue;
623 }
624
625
626 /* k = number of samples added to outbuf */
627 /* last k sample used data from [j-filter_l/2,j+filter_l-filter_l/2] */
628
629 /* how many samples of input data were used: */
630 *num_used = Min(len, filter_l + j - filter_l / 2);
631
632 /* adjust our input time counter. Incriment by the number of samples used,
633 * then normalize so that next output sample is at time 0, next
634 * input buffer is at time itime[ch] */
635 esv->itime[ch] += *num_used - k * resample_ratio;
636
637 /* save the last BLACKSIZE samples into the inbuf_old buffer */
638 if (*num_used >= BLACKSIZE) {
639 for (i = 0; i < BLACKSIZE; i++)
640 inbuf_old[i] = inbuf[*num_used + i - BLACKSIZE];
641 }
642 else {
643 /* shift in *num_used samples into inbuf_old */
644 int const n_shift = BLACKSIZE - *num_used; /* number of samples to shift */
645
646 /* shift n_shift samples by *num_used, to make room for the
647 * num_used new samples */
648 for (i = 0; i < n_shift; ++i)
649 inbuf_old[i] = inbuf_old[i + *num_used];
650
651 /* shift in the *num_used samples */
652 for (j = 0; i < BLACKSIZE; ++i, ++j)
653 inbuf_old[i] = inbuf[j];
654
655 assert(j == *num_used);
656 }
657 return k; /* return the number samples created at the new samplerate */
658 }
659
660 int
661 isResamplingNecessary(SessionConfig_t const* cfg)
662 {
663 int const l = cfg->samplerate_out * 0.9995f;
664 int const h = cfg->samplerate_out * 1.0005f;
665 return (cfg->samplerate_in < l) || (h < cfg->samplerate_in) ? 1 : 0;
666 }
667
668 /* copy in new samples from in_buffer into mfbuf, with resampling
669 if necessary. n_in = number of samples from the input buffer that
670 were used. n_out = number of samples copied into mfbuf */
671
672 void
673 fill_buffer(lame_internal_flags * gfc,
674 sample_t * const mfbuf[2], sample_t const * const in_buffer[2], int nsamples, int *n_in, int *n_out)
675 {
676 SessionConfig_t const *const cfg = &gfc->cfg;
677 int mf_size = gfc->sv_enc.mf_size;
678 int framesize = 576 * cfg->mode_gr;
679 int nout, ch = 0;
680 int nch = cfg->channels_out;
681
682 /* copy in new samples into mfbuf, with resampling if necessary */
683 if (isResamplingNecessary(cfg)) {
684 do {
685 nout =
686 fill_buffer_resample(gfc, &mfbuf[ch][mf_size],
687 framesize, in_buffer[ch], nsamples, n_in, ch);
688 } while (++ch < nch);
689 *n_out = nout;
690 }
691 else {
692 nout = Min(framesize, nsamples);
693 do {
694 memcpy(&mfbuf[ch][mf_size], &in_buffer[ch][0], nout * sizeof(mfbuf[0][0]));
695 } while (++ch < nch);
696 *n_out = nout;
697 *n_in = nout;
698 }
699 }
700
701
702
703
704
705
706
707 /***********************************************************************
708 *
709 * Message Output
710 *
711 ***********************************************************************/
712
713 void
714 lame_report_def(const char *format, va_list args)
715 {
716 (void) vfprintf(stderr, format, args);
717 fflush(stderr); /* an debug function should flush immediately */
718 }
719
720 void
721 lame_report_fnc(lame_report_function print_f, const char *format, ...)
722 {
723 if (print_f) {
724 va_list args;
725 va_start(args, format);
726 print_f(format, args);
727 va_end(args);
728 }
729 }
730
731
732 void
733 lame_debugf(const lame_internal_flags* gfc, const char *format, ...)
734 {
735 if (gfc && gfc->report_dbg) {
736 va_list args;
737 va_start(args, format);
738 gfc->report_dbg(format, args);
739 va_end(args);
740 }
741 }
742
743
744 void
745 lame_msgf(const lame_internal_flags* gfc, const char *format, ...)
746 {
747 if (gfc && gfc->report_msg) {
748 va_list args;
749 va_start(args, format);
750 gfc->report_msg(format, args);
751 va_end(args);
752 }
753 }
754
755
756 void
757 lame_errorf(const lame_internal_flags* gfc, const char *format, ...)
758 {
759 if (gfc && gfc->report_err) {
760 va_list args;
761 va_start(args, format);
762 gfc->report_err(format, args);
763 va_end(args);
764 }
765 }
766
767
768
769 /***********************************************************************
770 *
771 * routines to detect CPU specific features like 3DNow, MMX, SSE
772 *
773 * donated by Frank Klemm
774 * added Robert Hegemann 2000-10-10
775 *
776 ***********************************************************************/
777
778 #ifdef HAVE_NASM
779 extern int has_MMX_nasm(void);
780 extern int has_3DNow_nasm(void);
781 extern int has_SSE_nasm(void);
782 extern int has_SSE2_nasm(void);
783 #endif
784
785 int
786 has_MMX(void)
787 {
788 #ifdef HAVE_NASM
789 return has_MMX_nasm();
790 #else
791 return 0; /* don't know, assume not */
792 #endif
793 }
794
795 int
796 has_3DNow(void)
797 {
798 #ifdef HAVE_NASM
799 return has_3DNow_nasm();
800 #else
801 return 0; /* don't know, assume not */
802 #endif
803 }
804
805 int
806 has_SSE(void)
807 {
808 #ifdef HAVE_NASM
809 return has_SSE_nasm();
810 #else
811 #if defined( _M_X64 ) || defined( MIN_ARCH_SSE )
812 return 1;
813 #else
814 return 0; /* don't know, assume not */
815 #endif
816 #endif
817 }
818
819 int
820 has_SSE2(void)
821 {
822 #ifdef HAVE_NASM
823 return has_SSE2_nasm();
824 #else
825 #if defined( _M_X64 ) || defined( MIN_ARCH_SSE )
826 return 1;
827 #else
828 return 0; /* don't know, assume not */
829 #endif
830 #endif
831 }
832
833 void
834 disable_FPE(void)
835 {
836 /* extremly system dependent stuff, move to a lib to make the code readable */
837 /*==========================================================================*/
838
839
840
841 /*
842 * Disable floating point exceptions
843 */
844
845
846
847
848 #if defined(__FreeBSD__) && !defined(__alpha__)
849 {
850 /* seet floating point mask to the Linux default */
851 fp_except_t mask;
852 mask = fpgetmask();
853 /* if bit is set, we get SIGFPE on that error! */
854 fpsetmask(mask & ~(FP_X_INV | FP_X_DZ));
855 /* DEBUGF("FreeBSD mask is 0x%x\n",mask); */
856 }
857 #endif
858
859 #if defined(__riscos__) && !defined(ABORTFP)
860 /* Disable FPE's under RISC OS */
861 /* if bit is set, we disable trapping that error! */
862 /* _FPE_IVO : invalid operation */
863 /* _FPE_DVZ : divide by zero */
864 /* _FPE_OFL : overflow */
865 /* _FPE_UFL : underflow */
866 /* _FPE_INX : inexact */
867 DisableFPETraps(_FPE_IVO | _FPE_DVZ | _FPE_OFL);
868 #endif
869
870 /*
871 * Debugging stuff
872 * The default is to ignore FPE's, unless compiled with -DABORTFP
873 * so add code below to ENABLE FPE's.
874 */
875
876 #if defined(ABORTFP)
877 #if defined(_MSC_VER)
878 {
879 #if 0
880 /* rh 061207
881 the following fix seems to be a workaround for a problem in the
882 parent process calling LAME. It would be better to fix the broken
883 application => code disabled.
884 */
885
886 /* set affinity to a single CPU. Fix for EAC/lame on SMP systems from
887 "Todd Richmond" <[email protected]> */
888 SYSTEM_INFO si;
889 GetSystemInfo(&si);
890 SetProcessAffinityMask(GetCurrentProcess(), si.dwActiveProcessorMask);
891 #endif
892 #include <float.h>
893 unsigned int mask;
894 mask = _controlfp(0, 0);
895 mask &= ~(_EM_OVERFLOW | _EM_UNDERFLOW | _EM_ZERODIVIDE | _EM_INVALID);
896 mask = _controlfp(mask, _MCW_EM);
897 }
898 #elif defined(__CYGWIN__)
899 # define _FPU_GETCW(cw) __asm__ ("fnstcw %0" : "=m" (*&cw))
900 # define _FPU_SETCW(cw) __asm__ ("fldcw %0" : : "m" (*&cw))
901
902 # define _EM_INEXACT 0x00000020 /* inexact (precision) */
903 # define _EM_UNDERFLOW 0x00000010 /* underflow */
904 # define _EM_OVERFLOW 0x00000008 /* overflow */
905 # define _EM_ZERODIVIDE 0x00000004 /* zero divide */
906 # define _EM_INVALID 0x00000001 /* invalid */
907 {
908 unsigned int mask;
909 _FPU_GETCW(mask);
910 /* Set the FPU control word to abort on most FPEs */
911 mask &= ~(_EM_OVERFLOW | _EM_ZERODIVIDE | _EM_INVALID);
912 _FPU_SETCW(mask);
913 }
914 # elif defined(__linux__)
915 {
916
917 # include <fpu_control.h>
918 # ifndef _FPU_GETCW
919 # define _FPU_GETCW(cw) __asm__ ("fnstcw %0" : "=m" (*&cw))
920 # endif
921 # ifndef _FPU_SETCW
922 # define _FPU_SETCW(cw) __asm__ ("fldcw %0" : : "m" (*&cw))
923 # endif
924
925 /*
926 * Set the Linux mask to abort on most FPE's
927 * if bit is set, we _mask_ SIGFPE on that error!
928 * mask &= ~( _FPU_MASK_IM | _FPU_MASK_ZM | _FPU_MASK_OM | _FPU_MASK_UM );
929 */
930
931 unsigned int mask;
932 _FPU_GETCW(mask);
933 mask &= ~(_FPU_MASK_IM | _FPU_MASK_ZM | _FPU_MASK_OM);
934 _FPU_SETCW(mask);
935 }
936 #endif
937 #endif /* ABORTFP */
938 }
939
940
941
942
943
944 #ifdef USE_FAST_LOG
945 /***********************************************************************
946 *
947 * Fast Log Approximation for log2, used to approximate every other log
948 * (log10 and log)
949 * maximum absolute error for log10 is around 10-6
950 * maximum *relative* error can be high when x is almost 1 because error/log10(x) tends toward x/e
951 *
952 * use it if typical RESULT values are > 1e-5 (for example if x>1.00001 or x<0.99999)
953 * or if the relative precision in the domain around 1 is not important (result in 1 is exact and 0)
954 *
955 ***********************************************************************/
956
957
958 #define LOG2_SIZE (512)
959 #define LOG2_SIZE_L2 (9)
960
961 static ieee754_float32_t log_table[LOG2_SIZE + 1];
962
963
964
965 void
966 init_log_table(void)
967 {
968 int j;
969 static int init = 0;
970
971 /* Range for log2(x) over [1,2[ is [0,1[ */
972 assert((1 << LOG2_SIZE_L2) == LOG2_SIZE);
973
974 if (!init) {
975 for (j = 0; j < LOG2_SIZE + 1; j++)
976 log_table[j] = log(1.0f + j / (ieee754_float32_t) LOG2_SIZE) / log(2.0f);
977 }
978 init = 1;
979 }
980
981
982
983 ieee754_float32_t
984 fast_log2(ieee754_float32_t x)
985 {
986 ieee754_float32_t log2val, partial;
987 union {
988 ieee754_float32_t f;
989 int i;
990 } fi;
991 int mantisse;
992 fi.f = x;
993 mantisse = fi.i & 0x7fffff;
994 log2val = ((fi.i >> 23) & 0xFF) - 0x7f;
995 partial = (mantisse & ((1 << (23 - LOG2_SIZE_L2)) - 1));
996 partial *= 1.0f / ((1 << (23 - LOG2_SIZE_L2)));
997
998
999 mantisse >>= (23 - LOG2_SIZE_L2);
1000
1001 /* log2val += log_table[mantisse]; without interpolation the results are not good */
1002 log2val += log_table[mantisse] * (1.0f - partial) + log_table[mantisse + 1] * partial;
1003
1004 return log2val;
1005 }
1006
1007 #else /* Don't use FAST_LOG */
1008
1009
1010 void
1011 init_log_table(void)
1012 {
1013 }
1014
1015
1016 #endif
1017
1018 /* end of util.c */