• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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$ */
24 
25 #ifdef HAVE_CONFIG_H
26 # include <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
free_id3tag(lame_internal_flags * const gfc)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
free_global_data(lame_internal_flags * gfc)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
freegfc(lame_internal_flags * const gfc)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
calloc_aligned(aligned_pointer_t * ptr,unsigned int size,unsigned int bytes)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
free_aligned(aligned_pointer_t * ptr)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
ATHformula_GB(FLOAT f,FLOAT value,FLOAT f_min,FLOAT f_max)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
ATHformula(SessionConfig_t const * cfg,FLOAT freq)250 ATHformula(SessionConfig_t const *cfg, FLOAT freq)
251 {
252     FLOAT   ath;
253     switch (cfg->ATHtype) {
254     case 0:
255         ath = ATHformula_GB(freq, 9, 0.1f, 24.0f);
256         break;
257     case 1:
258         ath = ATHformula_GB(freq, -1, 0.1f, 24.0f); /*over sensitive, should probably be removed */
259         break;
260     case 2:
261         ath = ATHformula_GB(freq, 0, 0.1f, 24.0f);
262         break;
263     case 3:
264         ath = ATHformula_GB(freq, 1, 0.1f, 24.0f) + 6; /*modification of GB formula by Roel */
265         break;
266     case 4:
267         ath = ATHformula_GB(freq, cfg->ATHcurve, 0.1f, 24.0f);
268         break;
269     case 5:
270         ath = ATHformula_GB(freq, cfg->ATHcurve, 3.41f, 16.1f);
271         break;
272     default:
273         ath = ATHformula_GB(freq, 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
freq2bark(FLOAT freq)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
FindNearestBitrate(int bRate,int version,int samplerate)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
nearestBitrateFullIndex(uint16_t bitrate)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
map2MP3Frequency(int freq)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
BitrateIndex(int bRate,int version,int samplerate)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
SmpFrqIndex(int sample_freq,int * const version)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
blackman(FLOAT x,FLOAT fcn,int l)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
gcd(int i,int j)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
fill_buffer_resample(lame_internal_flags * gfc,sample_t * outbuf,int desired_len,sample_t const * inbuf,int len,int * num_used,int ch)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;
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         FLOAT   xvalue;
594         int     joff;
595 
596         j = floor(time0 - esv->itime[ch]);
597 
598         /* check if we need more input data */
599         if ((filter_l + j - filter_l / 2) >= len)
600             break;
601 
602         /* blackman filter.  by default, window centered at j+.5(filter_l%2) */
603         /* but we want a window centered at time0.   */
604         offset = (time0 - esv->itime[ch] - (j + .5 * (filter_l % 2)));
605         assert(fabs(offset) <= .501);
606 
607         /* find the closest precomputed window for this offset: */
608         joff = floor((offset * 2 * bpc) + bpc + .5);
609 
610         xvalue = 0.;
611         for (i = 0; i <= filter_l; ++i) {
612             int const j2 = i + j - filter_l / 2;
613             sample_t y;
614             assert(j2 < len);
615             assert(j2 + BLACKSIZE >= 0);
616             y = (j2 < 0) ? inbuf_old[BLACKSIZE + j2] : inbuf[j2];
617 #ifdef PRECOMPUTE
618             xvalue += y * esv->blackfilt[joff][i];
619 #else
620             xvalue += y * blackman(i - offset, fcn, filter_l); /* very slow! */
621 #endif
622         }
623         outbuf[k] = xvalue;
624     }
625 
626 
627     /* k = number of samples added to outbuf */
628     /* last k sample used data from [j-filter_l/2,j+filter_l-filter_l/2]  */
629 
630     /* how many samples of input data were used:  */
631     *num_used = Min(len, filter_l + j - filter_l / 2);
632 
633     /* adjust our input time counter.  Incriment by the number of samples used,
634      * then normalize so that next output sample is at time 0, next
635      * input buffer is at time itime[ch] */
636     esv->itime[ch] += *num_used - k * resample_ratio;
637 
638     /* save the last BLACKSIZE samples into the inbuf_old buffer */
639     if (*num_used >= BLACKSIZE) {
640         for (i = 0; i < BLACKSIZE; i++)
641             inbuf_old[i] = inbuf[*num_used + i - BLACKSIZE];
642     }
643     else {
644         /* shift in *num_used samples into inbuf_old  */
645         int const n_shift = BLACKSIZE - *num_used; /* number of samples to shift */
646 
647         /* shift n_shift samples by *num_used, to make room for the
648          * num_used new samples */
649         for (i = 0; i < n_shift; ++i)
650             inbuf_old[i] = inbuf_old[i + *num_used];
651 
652         /* shift in the *num_used samples */
653         for (j = 0; i < BLACKSIZE; ++i, ++j)
654             inbuf_old[i] = inbuf[j];
655 
656         assert(j == *num_used);
657     }
658     return k;           /* return the number samples created at the new samplerate */
659 }
660 
661 int
isResamplingNecessary(SessionConfig_t const * cfg)662 isResamplingNecessary(SessionConfig_t const* cfg)
663 {
664     int const l = cfg->samplerate_out * 0.9995f;
665     int const h = cfg->samplerate_out * 1.0005f;
666     return (cfg->samplerate_in < l) || (h < cfg->samplerate_in) ? 1 : 0;
667 }
668 
669 /* copy in new samples from in_buffer into mfbuf, with resampling
670    if necessary.  n_in = number of samples from the input buffer that
671    were used.  n_out = number of samples copied into mfbuf  */
672 
673 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)674 fill_buffer(lame_internal_flags * gfc,
675             sample_t * const mfbuf[2], sample_t const * const in_buffer[2], int nsamples, int *n_in, int *n_out)
676 {
677     SessionConfig_t const *const cfg = &gfc->cfg;
678     int     mf_size = gfc->sv_enc.mf_size;
679     int     framesize = 576 * cfg->mode_gr;
680     int     nout, ch = 0;
681     int     nch = cfg->channels_out;
682 
683     /* copy in new samples into mfbuf, with resampling if necessary */
684     if (isResamplingNecessary(cfg)) {
685         do {
686             nout =
687                 fill_buffer_resample(gfc, &mfbuf[ch][mf_size],
688                                      framesize, in_buffer[ch], nsamples, n_in, ch);
689         } while (++ch < nch);
690         *n_out = nout;
691     }
692     else {
693         nout = Min(framesize, nsamples);
694         do {
695             memcpy(&mfbuf[ch][mf_size], &in_buffer[ch][0], nout * sizeof(mfbuf[0][0]));
696         } while (++ch < nch);
697         *n_out = nout;
698         *n_in = nout;
699     }
700 }
701 
702 
703 
704 
705 
706 
707 
708 /***********************************************************************
709 *
710 *  Message Output
711 *
712 ***********************************************************************/
713 
714 void
lame_report_def(const char * format,va_list args)715 lame_report_def(const char *format, va_list args)
716 {
717     (void) vfprintf(stderr, format, args);
718     fflush(stderr); /* an debug function should flush immediately */
719 }
720 
721 void
lame_report_fnc(lame_report_function print_f,const char * format,...)722 lame_report_fnc(lame_report_function print_f, const char *format, ...)
723 {
724     if (print_f) {
725         va_list args;
726         va_start(args, format);
727         print_f(format, args);
728         va_end(args);
729     }
730 }
731 
732 
733 void
lame_debugf(const lame_internal_flags * gfc,const char * format,...)734 lame_debugf(const lame_internal_flags* gfc, const char *format, ...)
735 {
736     if (gfc && gfc->report_dbg) {
737         va_list args;
738         va_start(args, format);
739         gfc->report_dbg(format, args);
740         va_end(args);
741     }
742 }
743 
744 
745 void
lame_msgf(const lame_internal_flags * gfc,const char * format,...)746 lame_msgf(const lame_internal_flags* gfc, const char *format, ...)
747 {
748     if (gfc && gfc->report_msg) {
749         va_list args;
750         va_start(args, format);
751         gfc->report_msg(format, args);
752         va_end(args);
753     }
754 }
755 
756 
757 void
lame_errorf(const lame_internal_flags * gfc,const char * format,...)758 lame_errorf(const lame_internal_flags* gfc, const char *format, ...)
759 {
760     if (gfc && gfc->report_err) {
761         va_list args;
762         va_start(args, format);
763         gfc->report_err(format, args);
764         va_end(args);
765     }
766 }
767 
768 
769 
770 /***********************************************************************
771  *
772  *      routines to detect CPU specific features like 3DNow, MMX, SSE
773  *
774  *  donated by Frank Klemm
775  *  added Robert Hegemann 2000-10-10
776  *
777  ***********************************************************************/
778 
779 #ifdef HAVE_NASM
780 extern int has_MMX_nasm(void);
781 extern int has_3DNow_nasm(void);
782 extern int has_SSE_nasm(void);
783 extern int has_SSE2_nasm(void);
784 #endif
785 
786 int
has_MMX(void)787 has_MMX(void)
788 {
789 #ifdef HAVE_NASM
790     return has_MMX_nasm();
791 #else
792     return 0;           /* don't know, assume not */
793 #endif
794 }
795 
796 int
has_3DNow(void)797 has_3DNow(void)
798 {
799 #ifdef HAVE_NASM
800     return has_3DNow_nasm();
801 #else
802     return 0;           /* don't know, assume not */
803 #endif
804 }
805 
806 int
has_SSE(void)807 has_SSE(void)
808 {
809 #ifdef HAVE_NASM
810     return has_SSE_nasm();
811 #else
812 #if defined( _M_X64 ) || defined( MIN_ARCH_SSE )
813     return 1;
814 #else
815     return 0;           /* don't know, assume not */
816 #endif
817 #endif
818 }
819 
820 int
has_SSE2(void)821 has_SSE2(void)
822 {
823 #ifdef HAVE_NASM
824     return has_SSE2_nasm();
825 #else
826 #if defined( _M_X64 ) || defined( MIN_ARCH_SSE )
827     return 1;
828 #else
829     return 0;           /* don't know, assume not */
830 #endif
831 #endif
832 }
833 
834 void
disable_FPE(void)835 disable_FPE(void)
836 {
837 /* extremly system dependent stuff, move to a lib to make the code readable */
838 /*==========================================================================*/
839 
840 
841 
842     /*
843      *  Disable floating point exceptions
844      */
845 
846 
847 
848 
849 #if defined(__FreeBSD__) && !defined(__alpha__)
850     {
851         /* seet floating point mask to the Linux default */
852         fp_except_t mask;
853         mask = fpgetmask();
854         /* if bit is set, we get SIGFPE on that error! */
855         fpsetmask(mask & ~(FP_X_INV | FP_X_DZ));
856         /*  DEBUGF("FreeBSD mask is 0x%x\n",mask); */
857     }
858 #endif
859 
860 #if defined(__riscos__) && !defined(ABORTFP)
861     /* Disable FPE's under RISC OS */
862     /* if bit is set, we disable trapping that error! */
863     /*   _FPE_IVO : invalid operation */
864     /*   _FPE_DVZ : divide by zero */
865     /*   _FPE_OFL : overflow */
866     /*   _FPE_UFL : underflow */
867     /*   _FPE_INX : inexact */
868     DisableFPETraps(_FPE_IVO | _FPE_DVZ | _FPE_OFL);
869 #endif
870 
871     /*
872      *  Debugging stuff
873      *  The default is to ignore FPE's, unless compiled with -DABORTFP
874      *  so add code below to ENABLE FPE's.
875      */
876 
877 #if defined(ABORTFP)
878 #if defined(_MSC_VER)
879     {
880 #if 0
881         /* rh 061207
882            the following fix seems to be a workaround for a problem in the
883            parent process calling LAME. It would be better to fix the broken
884            application => code disabled.
885          */
886 
887         /* set affinity to a single CPU.  Fix for EAC/lame on SMP systems from
888            "Todd Richmond" <todd.richmond@openwave.com> */
889         SYSTEM_INFO si;
890         GetSystemInfo(&si);
891         SetProcessAffinityMask(GetCurrentProcess(), si.dwActiveProcessorMask);
892 #endif
893 #include <float.h>
894         unsigned int mask;
895         mask = _controlfp(0, 0);
896         mask &= ~(_EM_OVERFLOW | _EM_UNDERFLOW | _EM_ZERODIVIDE | _EM_INVALID);
897         mask = _controlfp(mask, _MCW_EM);
898     }
899 #elif defined(__CYGWIN__)
900 #  define _FPU_GETCW(cw) __asm__ ("fnstcw %0" : "=m" (*&cw))
901 #  define _FPU_SETCW(cw) __asm__ ("fldcw %0" : : "m" (*&cw))
902 
903 #  define _EM_INEXACT     0x00000020 /* inexact (precision) */
904 #  define _EM_UNDERFLOW   0x00000010 /* underflow */
905 #  define _EM_OVERFLOW    0x00000008 /* overflow */
906 #  define _EM_ZERODIVIDE  0x00000004 /* zero divide */
907 #  define _EM_INVALID     0x00000001 /* invalid */
908     {
909         unsigned int mask;
910         _FPU_GETCW(mask);
911         /* Set the FPU control word to abort on most FPEs */
912         mask &= ~(_EM_OVERFLOW | _EM_ZERODIVIDE | _EM_INVALID);
913         _FPU_SETCW(mask);
914     }
915 # elif defined(__linux__)
916     {
917 
918 #  include <fpu_control.h>
919 #  ifndef _FPU_GETCW
920 #  define _FPU_GETCW(cw) __asm__ ("fnstcw %0" : "=m" (*&cw))
921 #  endif
922 #  ifndef _FPU_SETCW
923 #  define _FPU_SETCW(cw) __asm__ ("fldcw %0" : : "m" (*&cw))
924 #  endif
925 
926         /*
927          * Set the Linux mask to abort on most FPE's
928          * if bit is set, we _mask_ SIGFPE on that error!
929          *  mask &= ~( _FPU_MASK_IM | _FPU_MASK_ZM | _FPU_MASK_OM | _FPU_MASK_UM );
930          */
931 
932         unsigned int mask;
933         _FPU_GETCW(mask);
934         mask &= ~(_FPU_MASK_IM | _FPU_MASK_ZM | _FPU_MASK_OM);
935         _FPU_SETCW(mask);
936     }
937 #endif
938 #endif /* ABORTFP */
939 }
940 
941 
942 
943 
944 
945 #ifdef USE_FAST_LOG
946 /***********************************************************************
947  *
948  * Fast Log Approximation for log2, used to approximate every other log
949  * (log10 and log)
950  * maximum absolute error for log10 is around 10-6
951  * maximum *relative* error can be high when x is almost 1 because error/log10(x) tends toward x/e
952  *
953  * use it if typical RESULT values are > 1e-5 (for example if x>1.00001 or x<0.99999)
954  * or if the relative precision in the domain around 1 is not important (result in 1 is exact and 0)
955  *
956  ***********************************************************************/
957 
958 
959 #define LOG2_SIZE       (512)
960 #define LOG2_SIZE_L2    (9)
961 
962 static ieee754_float32_t log_table[LOG2_SIZE + 1];
963 
964 
965 
966 void
init_log_table(void)967 init_log_table(void)
968 {
969     int     j;
970     static int init = 0;
971 
972     /* Range for log2(x) over [1,2[ is [0,1[ */
973     assert((1 << LOG2_SIZE_L2) == LOG2_SIZE);
974 
975     if (!init) {
976         for (j = 0; j < LOG2_SIZE + 1; j++)
977             log_table[j] = log(1.0f + j / (ieee754_float32_t) LOG2_SIZE) / log(2.0f);
978     }
979     init = 1;
980 }
981 
982 
983 
984 ieee754_float32_t
fast_log2(ieee754_float32_t x)985 fast_log2(ieee754_float32_t x)
986 {
987     ieee754_float32_t log2val, partial;
988     union {
989         ieee754_float32_t f;
990         int     i;
991     } fi;
992     int     mantisse;
993     fi.f = x;
994     mantisse = fi.i & 0x7fffff;
995     log2val = ((fi.i >> 23) & 0xFF) - 0x7f;
996     partial = (mantisse & ((1 << (23 - LOG2_SIZE_L2)) - 1));
997     partial *= 1.0f / ((1 << (23 - LOG2_SIZE_L2)));
998 
999 
1000     mantisse >>= (23 - LOG2_SIZE_L2);
1001 
1002     /* log2val += log_table[mantisse];  without interpolation the results are not good */
1003     log2val += log_table[mantisse] * (1.0f - partial) + log_table[mantisse + 1] * partial;
1004 
1005     return log2val;
1006 }
1007 
1008 #else /* Don't use FAST_LOG */
1009 
1010 
1011 void
init_log_table(void)1012 init_log_table(void)
1013 {
1014 }
1015 
1016 
1017 #endif
1018 
1019 /* end of util.c */
1020