util.c 25 KB

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