Coverage Report

Created: 2024-09-21 06:25

/src/lame/libmp3lame/util.c
Line
Count
Source (jump to first uncovered line)
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
50
free_id3tag(lame_internal_flags * const gfc)
51
8.57k
{
52
8.57k
    gfc->tag_spec.language[0] = 0;
53
8.57k
    if (gfc->tag_spec.title != 0) {
54
127
        free(gfc->tag_spec.title);
55
127
        gfc->tag_spec.title = 0;
56
127
    }
57
8.57k
    if (gfc->tag_spec.artist != 0) {
58
130
        free(gfc->tag_spec.artist);
59
130
        gfc->tag_spec.artist = 0;
60
130
    }
61
8.57k
    if (gfc->tag_spec.album != 0) {
62
117
        free(gfc->tag_spec.album);
63
117
        gfc->tag_spec.album = 0;
64
117
    }
65
8.57k
    if (gfc->tag_spec.comment != 0) {
66
122
        free(gfc->tag_spec.comment);
67
122
        gfc->tag_spec.comment = 0;
68
122
    }
69
70
8.57k
    if (gfc->tag_spec.albumart != 0) {
71
29
        free(gfc->tag_spec.albumart);
72
29
        gfc->tag_spec.albumart = 0;
73
29
        gfc->tag_spec.albumart_size = 0;
74
29
        gfc->tag_spec.albumart_mimetype = MIMETYPE_NONE;
75
29
    }
76
8.57k
    if (gfc->tag_spec.v2_head != 0) {
77
1.31k
        FrameDataNode *node = gfc->tag_spec.v2_head;
78
2.50k
        do {
79
2.50k
            void   *p = node->dsc.ptr.b;
80
2.50k
            void   *q = node->txt.ptr.b;
81
2.50k
            void   *r = node;
82
2.50k
            node = node->nxt;
83
2.50k
            free(p);
84
2.50k
            free(q);
85
2.50k
            free(r);
86
2.50k
        } while (node != 0);
87
1.31k
        gfc->tag_spec.v2_head = 0;
88
1.31k
        gfc->tag_spec.v2_tail = 0;
89
1.31k
    }
90
8.57k
}
91
92
93
static void
94
free_global_data(lame_internal_flags * gfc)
95
7.25k
{
96
7.25k
    if (gfc && gfc->cd_psy) {
97
5.16k
        if (gfc->cd_psy->l.s3) {
98
            /* XXX allocated in psymodel_init() */
99
5.16k
            free(gfc->cd_psy->l.s3);
100
5.16k
        }
101
5.16k
        if (gfc->cd_psy->s.s3) {
102
            /* XXX allocated in psymodel_init() */
103
5.16k
            free(gfc->cd_psy->s.s3);
104
5.16k
        }
105
5.16k
        free(gfc->cd_psy);
106
5.16k
        gfc->cd_psy = 0;
107
5.16k
    }
108
7.25k
}
109
110
111
void
112
freegfc(lame_internal_flags * const gfc)
113
7.25k
{                       /* bit stream structure */
114
7.25k
    int     i;
115
116
7.25k
    if (gfc == 0) return;
117
118
4.65M
    for (i = 0; i <= 2 * BPC; i++)
119
4.65M
        if (gfc->sv_enc.blackfilt[i] != NULL) {
120
666k
            free(gfc->sv_enc.blackfilt[i]);
121
666k
            gfc->sv_enc.blackfilt[i] = NULL;
122
666k
        }
123
7.25k
    if (gfc->sv_enc.inbuf_old[0]) {
124
2.84k
        free(gfc->sv_enc.inbuf_old[0]);
125
2.84k
        gfc->sv_enc.inbuf_old[0] = NULL;
126
2.84k
    }
127
7.25k
    if (gfc->sv_enc.inbuf_old[1]) {
128
2.84k
        free(gfc->sv_enc.inbuf_old[1]);
129
2.84k
        gfc->sv_enc.inbuf_old[1] = NULL;
130
2.84k
    }
131
132
7.25k
    if (gfc->bs.buf != NULL) {
133
5.16k
        free(gfc->bs.buf);
134
5.16k
        gfc->bs.buf = NULL;
135
5.16k
    }
136
137
7.25k
    if (gfc->VBR_seek_table.bag) {
138
3.52k
        free(gfc->VBR_seek_table.bag);
139
3.52k
        gfc->VBR_seek_table.bag = NULL;
140
3.52k
        gfc->VBR_seek_table.size = 0;
141
3.52k
    }
142
7.25k
    if (gfc->ATH) {
143
7.25k
        free(gfc->ATH);
144
7.25k
    }
145
7.25k
    if (gfc->sv_rpg.rgdata) {
146
7.25k
        free(gfc->sv_rpg.rgdata);
147
7.25k
    }
148
7.25k
    if (gfc->sv_enc.in_buffer_0) {
149
4.49k
        free(gfc->sv_enc.in_buffer_0);
150
4.49k
    }
151
7.25k
    if (gfc->sv_enc.in_buffer_1) {
152
4.49k
        free(gfc->sv_enc.in_buffer_1);
153
4.49k
    }
154
7.25k
    free_id3tag(gfc);
155
156
7.25k
#ifdef DECODE_ON_THE_FLY
157
7.25k
    if (gfc->hip) {
158
0
        hip_decode_exit(gfc->hip);
159
0
        gfc->hip = 0;
160
0
    }
161
7.25k
#endif
162
163
7.25k
    free_global_data(gfc);
164
165
7.25k
    free(gfc);
166
7.25k
}
167
168
void
169
calloc_aligned(aligned_pointer_t * ptr, unsigned int size, unsigned int bytes)
170
0
{
171
0
    if (ptr) {
172
0
        if (!ptr->pointer) {
173
0
            ptr->pointer = malloc(size + bytes);
174
0
            if (ptr->pointer != 0) {
175
0
                memset(ptr->pointer, 0, size + bytes);
176
0
                if (bytes > 0) {
177
0
                    ptr->aligned = (void *) ((((size_t) ptr->pointer + bytes - 1) / bytes) * bytes);
178
0
                }
179
0
                else {
180
0
                    ptr->aligned = ptr->pointer;
181
0
                }
182
0
            }
183
0
            else {
184
0
                ptr->aligned = 0;
185
0
            }
186
0
        }
187
0
    }
188
0
}
189
190
void
191
free_aligned(aligned_pointer_t * ptr)
192
0
{
193
0
    if (ptr) {
194
0
        if (ptr->pointer) {
195
0
            free(ptr->pointer);
196
0
            ptr->pointer = 0;
197
0
            ptr->aligned = 0;
198
0
        }
199
0
    }
200
0
}
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
10.6M
{
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
10.6M
    FLOAT   ath;
231
232
    /* the following Hack allows to ask for the lowest value */
233
10.6M
    if (f < -.3)
234
5.16k
        f = 3410;
235
236
10.6M
    f /= 1000;          /* convert to khz */
237
10.6M
    f = Max(f_min, f);
238
10.6M
    f = Min(f_max, f);
239
240
10.6M
    ath = 3.640 * pow(f, -0.8)
241
10.6M
        - 6.800 * exp(-0.6 * pow(f - 3.4, 2.0))
242
10.6M
        + 6.000 * exp(-0.15 * pow(f - 8.7, 2.0))
243
10.6M
        + (0.6 + 0.04 * value) * 0.001 * pow(f, 4.0);
244
10.6M
    return ath;
245
10.6M
}
246
247
248
249
FLOAT
250
ATHformula(SessionConfig_t const *cfg, FLOAT freq)
251
10.6M
{
252
10.6M
    FLOAT   ath;
253
10.6M
    switch (cfg->ATHtype) {
254
0
    case 0:
255
0
        ath = ATHformula_GB(freq, 9, 0.1f, 24.0f);
256
0
        break;
257
0
    case 1:
258
0
        ath = ATHformula_GB(freq, -1, 0.1f, 24.0f); /*over sensitive, should probably be removed */
259
0
        break;
260
0
    case 2:
261
0
        ath = ATHformula_GB(freq, 0, 0.1f, 24.0f);
262
0
        break;
263
0
    case 3:
264
0
        ath = ATHformula_GB(freq, 1, 0.1f, 24.0f) + 6; /*modification of GB formula by Roel */
265
0
        break;
266
7.63M
    case 4:
267
7.63M
        ath = ATHformula_GB(freq, cfg->ATHcurve, 0.1f, 24.0f);
268
7.63M
        break;
269
3.00M
    case 5:
270
3.00M
        ath = ATHformula_GB(freq, cfg->ATHcurve, 3.41f, 16.1f);
271
3.00M
        break;
272
0
    default:
273
0
        ath = ATHformula_GB(freq, 0, 0.1f, 24.0f);
274
0
        break;
275
10.6M
    }
276
10.6M
    return ath;
277
10.6M
}
278
279
/* see for example "Zwicker: Psychoakustik, 1982; ISBN 3-540-11401-7 */
280
FLOAT
281
freq2bark(FLOAT freq)
282
10.6M
{
283
    /* input: freq in hz  output: barks */
284
10.6M
    if (freq < 0)
285
10.3k
        freq = 0;
286
10.6M
    freq = freq * 0.001;
287
10.6M
    return 13.0 * atan(.76 * freq) + 3.5 * atan(freq * freq / (7.5 * 7.5));
288
10.6M
}
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
95.9k
#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
4.34k
{                       /* MPEG-1 or MPEG-2 LSF */
313
4.34k
    int     bitrate;
314
4.34k
    int     i;
315
316
4.34k
    if (samplerate < 16000)
317
1.42k
        version = 2;
318
319
4.34k
    bitrate = bitrate_table[version][1];
320
321
60.8k
    for (i = 2; i <= 14; i++) {
322
56.5k
        if (bitrate_table[version][i] > 0) {
323
47.9k
            if (ABS(bitrate_table[version][i] - bRate) < ABS(bitrate - bRate))
324
15.0k
                bitrate = bitrate_table[version][i];
325
47.9k
        }
326
56.5k
    }
327
4.34k
    return bitrate;
328
4.34k
}
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
5.63k
{
353
    /* borrowed from DM abr presets */
354
355
5.63k
    const int full_bitrate_table[] =
356
5.63k
        { 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 };
357
358
359
5.63k
    int     lower_range = 0, lower_range_kbps = 0, upper_range = 0, upper_range_kbps = 0;
360
361
362
5.63k
    int     b;
363
364
365
    /* We assume specified bitrate will be 320kbps */
366
5.63k
    upper_range_kbps = full_bitrate_table[16];
367
5.63k
    upper_range = 16;
368
5.63k
    lower_range_kbps = full_bitrate_table[16];
369
5.63k
    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
36.4k
    for (b = 0; b < 16; b++) {
375
35.8k
        if ((Max(bitrate, full_bitrate_table[b + 1])) != bitrate) {
376
5.05k
            upper_range_kbps = full_bitrate_table[b + 1];
377
5.05k
            upper_range = b + 1;
378
5.05k
            lower_range_kbps = full_bitrate_table[b];
379
5.05k
            lower_range = (b);
380
5.05k
            break;      /* We found upper range */
381
5.05k
        }
382
35.8k
    }
383
384
    /* Determine which range the value specified is closer to */
385
5.63k
    if ((upper_range_kbps - bitrate) > (bitrate - lower_range_kbps)) {
386
4.44k
        return lower_range;
387
4.44k
    }
388
1.19k
    return upper_range;
389
5.63k
}
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
0
{
402
0
    if (freq <= 8000)
403
0
        return 8000;
404
0
    if (freq <= 11025)
405
0
        return 11025;
406
0
    if (freq <= 12000)
407
0
        return 12000;
408
0
    if (freq <= 16000)
409
0
        return 16000;
410
0
    if (freq <= 22050)
411
0
        return 22050;
412
0
    if (freq <= 24000)
413
0
        return 24000;
414
0
    if (freq <= 32000)
415
0
        return 32000;
416
0
    if (freq <= 44100)
417
0
        return 44100;
418
419
0
    return 48000;
420
0
}
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
7.86k
{                       /* convert bitrate in kbps to index */
427
7.86k
    int     i;
428
7.86k
    if (samplerate < 16000)
429
1.73k
        version = 2;
430
59.1k
    for (i = 0; i <= 14; i++) {
431
59.1k
        if (bitrate_table[version][i] > 0) {
432
51.3k
            if (bitrate_table[version][i] == bRate) {
433
7.86k
                return i;
434
7.86k
            }
435
51.3k
        }
436
59.1k
    }
437
0
    return -1;
438
7.86k
}
439
440
/* convert samp freq in Hz to index */
441
442
int
443
SmpFrqIndex(int sample_freq, int *const version)
444
13.0k
{
445
13.0k
    switch (sample_freq) {
446
1.79k
    case 44100:
447
1.79k
        *version = 1;
448
1.79k
        return 0;
449
2.43k
    case 48000:
450
2.43k
        *version = 1;
451
2.43k
        return 1;
452
511
    case 32000:
453
511
        *version = 1;
454
511
        return 2;
455
366
    case 22050:
456
366
        *version = 0;
457
366
        return 0;
458
341
    case 24000:
459
341
        *version = 0;
460
341
        return 1;
461
363
    case 16000:
462
363
        *version = 0;
463
363
        return 2;
464
218
    case 11025:
465
218
        *version = 0;
466
218
        return 0;
467
61
    case 12000:
468
61
        *version = 0;
469
61
        return 1;
470
1.70k
    case 8000:
471
1.70k
        *version = 0;
472
1.70k
        return 2;
473
5.24k
    default:
474
5.24k
        *version = 0;
475
5.24k
        return -1;
476
13.0k
    }
477
13.0k
}
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
21.3M
{
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
21.3M
    FLOAT   bkwn, x2;
504
21.3M
    FLOAT const wcn = (PI * fcn);
505
506
21.3M
    x /= l;
507
21.3M
    if (x < 0)
508
332k
        x = 0;
509
21.3M
    if (x > 1)
510
332k
        x = 1;
511
21.3M
    x2 = x - .5;
512
513
21.3M
    bkwn = 0.42 - 0.5 * cos(2 * x * PI) + 0.08 * cos(4 * x * PI);
514
21.3M
    if (fabs(x2) < 1e-9)
515
5.39k
        return wcn / PI;
516
21.3M
    else
517
21.3M
        return (bkwn * sin(l * wcn * x2) / (PI * l * x2));
518
519
520
21.3M
}
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
543k
{
531
    /*    assert ( i > 0  &&  j > 0 ); */
532
543k
    return j ? gcd(j, i % j) : i;
533
543k
}
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
88.1k
{
542
88.1k
    SessionConfig_t const *const cfg = &gfc->cfg;
543
88.1k
    EncStateVar_t *esv = &gfc->sv_enc;
544
88.1k
    double  resample_ratio = (double)cfg->samplerate_in / (double)cfg->samplerate_out;
545
88.1k
    int     BLACKSIZE;
546
88.1k
    FLOAT   offset;
547
88.1k
    int     i, j = 0, k;
548
88.1k
    int     filter_l;
549
88.1k
    FLOAT   fcn, intratio;
550
88.1k
    FLOAT  *inbuf_old;
551
88.1k
    int     bpc;             /* number of convolution functions to pre-compute */
552
88.1k
    bpc = cfg->samplerate_out / gcd(cfg->samplerate_out, cfg->samplerate_in);
553
88.1k
    if (bpc > BPC)
554
0
        bpc = BPC;
555
556
88.1k
    intratio = (fabs(resample_ratio - floor(.5 + resample_ratio)) < FLT_EPSILON);
557
88.1k
    fcn = 1.00 / resample_ratio;
558
88.1k
    if (fcn > 1.00)
559
26.3k
        fcn = 1.00;
560
88.1k
    filter_l = 31;     /* must be odd */
561
88.1k
    filter_l += intratio; /* unless resample_ratio=int, it must be even */
562
563
564
88.1k
    BLACKSIZE = filter_l + 1; /* size of data needed for FIR */
565
566
88.1k
    if (gfc->fill_buffer_resample_init == 0) {
567
2.84k
        esv->inbuf_old[0] = lame_calloc(sample_t, BLACKSIZE);
568
2.84k
        esv->inbuf_old[1] = lame_calloc(sample_t, BLACKSIZE);
569
669k
        for (i = 0; i <= 2 * bpc; ++i)
570
666k
            esv->blackfilt[i] = lame_calloc(sample_t, BLACKSIZE);
571
572
2.84k
        esv->itime[0] = 0;
573
2.84k
        esv->itime[1] = 0;
574
575
        /* precompute blackman filter coefficients */
576
669k
        for (j = 0; j <= 2 * bpc; j++) {
577
666k
            FLOAT   sum = 0.;
578
666k
            offset = (j - bpc) / (2. * bpc);
579
22.0M
            for (i = 0; i <= filter_l; i++)
580
21.3M
                sum += esv->blackfilt[j][i] = blackman(i - offset, fcn, filter_l);
581
22.0M
            for (i = 0; i <= filter_l; i++)
582
21.3M
                esv->blackfilt[j][i] /= sum;
583
666k
        }
584
2.84k
        gfc->fill_buffer_resample_init = 1;
585
2.84k
    }
586
587
88.1k
    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
43.9M
    for (k = 0; k < desired_len; k++) {
592
43.9M
        double  time0 = k * resample_ratio; /* time of k'th output sample */
593
43.9M
        FLOAT   xvalue;
594
43.9M
        int     joff;
595
596
43.9M
        j = floor(time0 - esv->itime[ch]);
597
598
        /* check if we need more input data */
599
43.9M
        if ((filter_l + j - filter_l / 2) >= len)
600
42.9k
            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
43.8M
        offset = (time0 - esv->itime[ch] - (j + .5 * (filter_l % 2)));
605
43.8M
        assert(fabs(offset) <= .501);
606
607
        /* find the closest precomputed window for this offset: */
608
43.8M
        joff = floor((offset * 2 * bpc) + bpc + .5);
609
610
43.8M
        xvalue = 0.;
611
1.45G
        for (i = 0; i <= filter_l; ++i) {
612
1.40G
            int const j2 = i + j - filter_l / 2;
613
1.40G
            sample_t y;
614
1.40G
            assert(j2 < len);
615
1.40G
            assert(j2 + BLACKSIZE >= 0);
616
1.40G
            y = (j2 < 0) ? inbuf_old[BLACKSIZE + j2] : inbuf[j2];
617
1.40G
#ifdef PRECOMPUTE
618
1.40G
            xvalue += y * esv->blackfilt[joff][i];
619
#else
620
            xvalue += y * blackman(i - offset, fcn, filter_l); /* very slow! */
621
#endif
622
1.40G
        }
623
43.8M
        outbuf[k] = xvalue;
624
43.8M
    }
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
88.1k
    *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
88.1k
    esv->itime[ch] += *num_used - k * resample_ratio;
637
638
    /* save the last BLACKSIZE samples into the inbuf_old buffer */
639
88.1k
    if (*num_used >= BLACKSIZE) {
640
2.42M
        for (i = 0; i < BLACKSIZE; i++)
641
2.35M
            inbuf_old[i] = inbuf[*num_used + i - BLACKSIZE];
642
73.2k
    }
643
14.8k
    else {
644
        /* shift in *num_used samples into inbuf_old  */
645
14.8k
        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
465k
        for (i = 0; i < n_shift; ++i)
650
451k
            inbuf_old[i] = inbuf_old[i + *num_used];
651
652
        /* shift in the *num_used samples */
653
41.6k
        for (j = 0; i < BLACKSIZE; ++i, ++j)
654
26.7k
            inbuf_old[i] = inbuf[j];
655
656
14.8k
        assert(j == *num_used);
657
14.8k
    }
658
88.1k
    return k;           /* return the number samples created at the new samplerate */
659
88.1k
}
660
661
int
662
isResamplingNecessary(SessionConfig_t const* cfg)
663
91.8k
{
664
91.8k
    int const l = cfg->samplerate_out * 0.9995f;
665
91.8k
    int const h = cfg->samplerate_out * 1.0005f;
666
91.8k
    return (cfg->samplerate_in < l) || (h < cfg->samplerate_in) ? 1 : 0;
667
91.8k
}
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
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
85.4k
{
677
85.4k
    SessionConfig_t const *const cfg = &gfc->cfg;
678
85.4k
    int     mf_size = gfc->sv_enc.mf_size;
679
85.4k
    int     framesize = 576 * cfg->mode_gr;
680
85.4k
    int     nout, ch = 0;
681
85.4k
    int     nch = cfg->channels_out;
682
683
    /* copy in new samples into mfbuf, with resampling if necessary */
684
85.4k
    if (isResamplingNecessary(cfg)) {
685
88.1k
        do {
686
88.1k
            nout =
687
88.1k
                fill_buffer_resample(gfc, &mfbuf[ch][mf_size],
688
88.1k
                                     framesize, in_buffer[ch], nsamples, n_in, ch);
689
88.1k
        } while (++ch < nch);
690
51.2k
        *n_out = nout;
691
51.2k
    }
692
34.2k
    else {
693
34.2k
        nout = Min(framesize, nsamples);
694
60.1k
        do {
695
60.1k
            memcpy(&mfbuf[ch][mf_size], &in_buffer[ch][0], nout * sizeof(mfbuf[0][0]));
696
60.1k
        } while (++ch < nch);
697
34.2k
        *n_out = nout;
698
34.2k
        *n_in = nout;
699
34.2k
    }
700
85.4k
}
701
702
703
704
705
706
707
708
/***********************************************************************
709
*
710
*  Message Output
711
*
712
***********************************************************************/
713
714
void
715
lame_report_def(const char *format, va_list args)
716
0
{
717
0
    (void) vfprintf(stderr, format, args);
718
0
    fflush(stderr); /* an debug function should flush immediately */
719
0
}
720
721
void 
722
lame_report_fnc(lame_report_function print_f, const char *format, ...)
723
0
{
724
0
    if (print_f) {
725
0
        va_list args;
726
0
        va_start(args, format);
727
0
        print_f(format, args);
728
0
        va_end(args);
729
0
    }
730
0
}
731
732
733
void
734
lame_debugf(const lame_internal_flags* gfc, const char *format, ...)
735
0
{
736
0
    if (gfc && gfc->report_dbg) {
737
0
        va_list args;
738
0
        va_start(args, format);
739
0
        gfc->report_dbg(format, args);
740
0
        va_end(args);
741
0
    }
742
0
}
743
744
745
void
746
lame_msgf(const lame_internal_flags* gfc, const char *format, ...)
747
0
{
748
0
    if (gfc && gfc->report_msg) {
749
0
        va_list args;
750
0
        va_start(args, format);
751
0
        gfc->report_msg(format, args);
752
0
        va_end(args);
753
0
    }
754
0
}
755
756
757
void
758
lame_errorf(const lame_internal_flags* gfc, const char *format, ...)
759
0
{
760
0
    if (gfc && gfc->report_err) {
761
0
        va_list args;
762
0
        va_start(args, format);
763
0
        gfc->report_err(format, args);
764
0
        va_end(args);
765
0
    }
766
0
}
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
787
has_MMX(void)
788
5.16k
{
789
#ifdef HAVE_NASM
790
    return has_MMX_nasm();
791
#else
792
5.16k
    return 0;           /* don't know, assume not */
793
5.16k
#endif
794
5.16k
}
795
796
int
797
has_3DNow(void)
798
5.16k
{
799
#ifdef HAVE_NASM
800
    return has_3DNow_nasm();
801
#else
802
5.16k
    return 0;           /* don't know, assume not */
803
5.16k
#endif
804
5.16k
}
805
806
int
807
has_SSE(void)
808
5.16k
{
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
5.16k
    return 0;           /* don't know, assume not */
816
5.16k
#endif
817
5.16k
#endif
818
5.16k
}
819
820
int
821
has_SSE2(void)
822
5.16k
{
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
5.16k
    return 0;           /* don't know, assume not */
830
5.16k
#endif
831
5.16k
#endif
832
5.16k
}
833
834
void
835
disable_FPE(void)
836
7.25k
{
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(FP_X_INV) && defined(FP_X_DZ)
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
7.25k
}
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
1.02k
#define LOG2_SIZE       (512)
960
304M
#define LOG2_SIZE_L2    (9)
961
962
static ieee754_float32_t log_table[LOG2_SIZE + 1];
963
964
965
966
void
967
init_log_table(void)
968
7.25k
{
969
7.25k
    int     j;
970
7.25k
    static int init = 0;
971
972
    /* Range for log2(x) over [1,2[ is [0,1[ */
973
7.25k
    assert((1 << LOG2_SIZE_L2) == LOG2_SIZE);
974
975
7.25k
    if (!init) {
976
514
        for (j = 0; j < LOG2_SIZE + 1; j++)
977
513
            log_table[j] = log(1.0f + j / (ieee754_float32_t) LOG2_SIZE) / log(2.0f);
978
1
    }
979
7.25k
    init = 1;
980
7.25k
}
981
982
983
984
ieee754_float32_t
985
fast_log2(ieee754_float32_t x)
986
101M
{
987
101M
    ieee754_float32_t log2val, partial;
988
101M
    union {
989
101M
        ieee754_float32_t f;
990
101M
        int     i;
991
101M
    } fi;
992
101M
    int     mantisse;
993
101M
    fi.f = x;
994
101M
    mantisse = fi.i & 0x7fffff;
995
101M
    log2val = ((fi.i >> 23) & 0xFF) - 0x7f;
996
101M
    partial = (mantisse & ((1 << (23 - LOG2_SIZE_L2)) - 1));
997
101M
    partial *= 1.0f / ((1 << (23 - LOG2_SIZE_L2)));
998
999
1000
101M
    mantisse >>= (23 - LOG2_SIZE_L2);
1001
1002
    /* log2val += log_table[mantisse];  without interpolation the results are not good */
1003
101M
    log2val += log_table[mantisse] * (1.0f - partial) + log_table[mantisse + 1] * partial;
1004
1005
101M
    return log2val;
1006
101M
}
1007
1008
#else /* Don't use FAST_LOG */
1009
1010
1011
void
1012
init_log_table(void)
1013
{
1014
}
1015
1016
1017
#endif
1018
1019
/* end of util.c */