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