Debian Codegraph @

libtwolame/psycho_2.c

raw
1 /*
2 * TwoLAME: an optimized MPEG Audio Layer Two encoder
3 *
4 * Copyright (C) 2001-2004 Michael Cheng
5 * Copyright (C) 2004-2018 The TwoLAME Project
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 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 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <math.h>
27 #include <string.h>
28
29 #include "twolame.h"
30 #include "common.h"
31 #include "mem.h"
32 #include "fft.h"
33 #include "psycho_2.h"
34
35 /* The static variables "r", "phi_sav", "new", "old" and "oldest" have */
36 /* to be remembered for the unpredictability measure. For "r" and */
37 /* "phi_sav", the first index from the left is the channel select and */
38 /* the second index is the "age" of the data. */
39
40
41 /* The following static variables are constants. */
42
43 static const FLOAT nmt = 5.5;
44
45 static const FLOAT crit_band[27] = { 0, 100, 200, 300, 400, 510, 630, 770,
46 920, 1080, 1270, 1480, 1720, 2000, 2320, 2700,
47 3150, 3700, 4400, 5300, 6400, 7700, 9500, 12000,
48 15500, 25000, 30000
49 };
50
51 static const FLOAT bmax[27] = { 20.0, 20.0, 20.0, 20.0, 20.0, 17.0, 15.0,
52 10.0, 7.0, 4.4, 4.5, 4.5, 4.5, 4.5,
53 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5,
54 4.5, 4.5, 4.5, 3.5, 3.5, 3.5
55 };
56
57 static void psycho_2_read_absthr(absthr, table)
58 FLOAT *absthr;
59 int table;
60 {
61 int j;
62 #include "psycho_2_absthr.h"
63
64 if ((table < 0) || (table > 3)) {
65 fprintf(stderr, "internal error: wrong table number");
66 return;
67 }
68
69 for (j = 0; j < HBLKSIZE; j++) {
70 absthr[j] = absthr_table[table][j];
71 }
72 return;
73 }
74
75 /********************************
76 * init psycho model 2
77 ********************************/
78 psycho_2_mem *twolame_psycho_2_init(twolame_options * glopts, int sfreq)
79 {
80 psycho_2_mem *mem;
81 FLOAT *cbval, *rnorm;
82 FLOAT *window;
83 int *numlines;
84 int *partition;
85 FCB *s;
86 FLOAT *tmn;
87
88 int i, j, itemp2;
89 FLOAT freq_mult;
90 FLOAT temp1, ftemp2, temp3;
91 FLOAT bval_lo, *fthr;
92
93 int sfreq_idx;
94
95 {
96 mem = (psycho_2_mem *) TWOLAME_MALLOC(sizeof(psycho_2_mem));
97 if (!mem)
98 return NULL;
99
100 mem->tmn = (FLOAT *) TWOLAME_MALLOC(sizeof(DCB));
101 mem->s = (FCB *) TWOLAME_MALLOC(sizeof(FCBCB));
102 mem->lthr = (FHBLK *) TWOLAME_MALLOC(sizeof(F2HBLK));
103 mem->r = (F2HBLK *) TWOLAME_MALLOC(sizeof(F22HBLK));
104 mem->phi_sav = (F2HBLK *) TWOLAME_MALLOC(sizeof(F22HBLK));
105
106 // static int new = 0, old = 1, oldest = 0;
107 mem->new = 0;
108 mem->old = 1;
109 mem->oldest = 0;
110
111 mem->flush = (int) (384 * 3.0 / 2.0);
112 mem->syncsize = 1056;
113 mem->sync_flush = mem->syncsize - mem->flush;
114 }
115
116 {
117 cbval = mem->cbval;
118 rnorm = mem->rnorm;
119 window = mem->window;
120 numlines = mem->numlines;
121 partition = mem->partition;
122 s = mem->s;
123 tmn = mem->tmn;
124 fthr = mem->fthr;
125 }
126
127 switch (sfreq) {
128 case 32000:
129 case 16000:
130 sfreq_idx = 0;
131 break;
132 case 44100:
133 case 22050:
134 sfreq_idx = 1;
135 break;
136 case 48000:
137 case 24000:
138 sfreq_idx = 2;
139 break;
140 default:
141 fprintf(stderr, "error, invalid sampling frequency: %d Hz\n", sfreq);
142 return NULL;
143 }
144 fprintf(stderr, "absthr[][] sampling frequency index: %d\n", sfreq_idx);
145 psycho_2_read_absthr(mem->absthr, sfreq_idx);
146
147
148 /* calculate HANN window coefficients */
149 /* for(i=0;i<BLKSIZE;i++)window[i]=0.5*(1-cos(2.0*PI*i/(BLKSIZE-1.0))); */
150 for (i = 0; i < BLKSIZE; i++)
151 window[i] = 0.5 * (1 - cos(2.0 * PI * (i - 0.5) / BLKSIZE));
152 /* reset states used in unpredictability measure */
153 for (i = 0; i < HBLKSIZE; i++) {
154 mem->r[0][0][i] = mem->r[1][0][i] = mem->r[0][1][i] = mem->r[1][1][i] = 0;
155 mem->phi_sav[0][0][i] = mem->phi_sav[1][0][i] = 0;
156 mem->phi_sav[0][1][i] = mem->phi_sav[1][1][i] = 0;
157 mem->lthr[0][i] = 60802371420160.0;
158 mem->lthr[1][i] = 60802371420160.0;
159 }
160 /*****************************************************************************
161 * Initialization: Compute the following constants for use later *
162 * partition[HBLKSIZE] = the partition number associated with each *
163 * frequency line *
164 * cbval[CBANDS] = the center (average) bark value of each *
165 * partition *
166 * numlines[CBANDS] = the number of frequency lines in each partition *
167 * tmn[CBANDS] = tone masking noise *
168 *****************************************************************************/
169 /* compute fft frequency multiplicand */
170 freq_mult = (FLOAT) sfreq / (FLOAT) BLKSIZE;
171
172 /* calculate fft frequency, then bval of each line (use fthr[] as tmp storage) */
173 for (i = 0; i < HBLKSIZE; i++) {
174 temp1 = i * freq_mult;
175 j = 1;
176 while (temp1 > crit_band[j])
177 j++;
178 fthr[i] = j - 1 + (temp1 - crit_band[j - 1]) / (crit_band[j] - crit_band[j - 1]);
179 }
180 partition[0] = 0;
181 /* temp2 is the counter of the number of frequency lines in each partition */
182 itemp2 = 1;
183 cbval[0] = fthr[0];
184 bval_lo = fthr[0];
185 for (i = 1; i < HBLKSIZE; i++) {
186 if ((fthr[i] - bval_lo) > 0.33) {
187 partition[i] = partition[i - 1] + 1;
188 cbval[partition[i - 1]] = cbval[partition[i - 1]] / itemp2;
189 cbval[partition[i]] = fthr[i];
190 bval_lo = fthr[i];
191 numlines[partition[i - 1]] = itemp2;
192 itemp2 = 1;
193 } else {
194 partition[i] = partition[i - 1];
195 cbval[partition[i]] += fthr[i];
196 itemp2++;
197 }
198 }
199 numlines[partition[i - 1]] = itemp2;
200 cbval[partition[i - 1]] = cbval[partition[i - 1]] / itemp2;
201
202 /************************************************************************
203 * Now compute the spreading function, s[j][i], the value of the spread-*
204 * ing function, centered at band j, for band i, store for later use *
205 ************************************************************************/
206 for (j = 0; j < CBANDS; j++) {
207 for (i = 0; i < CBANDS; i++) {
208 temp1 = (cbval[i] - cbval[j]) * 1.05;
209 if (temp1 >= 0.5 && temp1 <= 2.5) {
210 ftemp2 = temp1 - 0.5;
211 ftemp2 = 8.0 * (ftemp2 * ftemp2 - 2.0 * ftemp2);
212 } else
213 ftemp2 = 0.0;
214 temp1 += 0.474;
215 temp3 = 15.811389 + 7.5 * temp1 - 17.5 * sqrt((FLOAT) (1.0 + temp1 * temp1));
216 if (temp3 <= -100)
217 s[i][j] = 0;
218 else {
219 temp3 = (ftemp2 + temp3) * LN_TO_LOG10;
220 s[i][j] = exp(temp3);
221 }
222 }
223 }
224
225 /* Calculate Tone Masking Noise values */
226 for (j = 0; j < CBANDS; j++) {
227 temp1 = 15.5 + cbval[j];
228 tmn[j] = (temp1 > 24.5) ? temp1 : 24.5;
229 /* Calculate normalization factors for the net spreading functions */
230 rnorm[j] = 0;
231 for (i = 0; i < CBANDS; i++) {
232 rnorm[j] += s[j][i];
233 }
234 }
235
236 if (glopts->verbosity > 5) {
237 /* Dump All the Values to stderr and exit */
238 int wlow, whigh = 0;
239 fprintf(stderr, "psy model 2 init\n");
240 fprintf(stderr, "index \tnlines \twlow \twhigh \tbval \tminval \ttmn\n");
241 for (i = 0; i < CBANDS; i++) {
242 wlow = whigh + 1;
243 whigh = wlow + numlines[i] - 1;
244 fprintf(stderr, "%i \t%i \t%i \t%i \t%5.2f \t%4.2f \t%4.2f\n", i + 1, numlines[i], wlow,
245 whigh, cbval[i], bmax[(int) (cbval[i] + 0.5)], tmn[i]);
246 }
247 }
248
249 return (mem);
250 }
251
252 void twolame_psycho_2(twolame_options * glopts, short int buffer[2][1152],
253 short int savebuf[2][1056], FLOAT smr[2][32])
254 {
255 psycho_2_mem *mem;
256 unsigned int i, j, k, ch;
257 int new, old, oldest;
258 FLOAT r_prime, phi_prime;
259 FLOAT minthres, sum_energy;
260 FLOAT tb, temp1, temp2, temp3;
261 FLOAT *grouped_c, *grouped_e;
262 FLOAT *nb, *cb, *ecb, *bc;
263 FLOAT *cbval, *rnorm;
264 FLOAT *wsamp_r, *phi, *energy, *window;
265 FLOAT *c;
266 FLOAT *fthr;
267
268 FLOAT *snrtmp[2];
269 int *numlines;
270 int *partition;
271 FLOAT *tmn;
272 FCB *s;
273 FHBLK *lthr;
274 F2HBLK *r, *phi_sav;
275 FLOAT *absthr;
276
277 int nch = glopts->num_channels_out;
278 int sfreq = glopts->samplerate_out;
279
280
281 if (!glopts->p2mem) {
282 glopts->p2mem = twolame_psycho_2_init(glopts, sfreq);
283 }
284 mem = glopts->p2mem;
285 {
286 grouped_c = mem->grouped_c;
287 grouped_e = mem->grouped_e;
288 nb = mem->nb;
289 cb = mem->cb;
290 ecb = mem->ecb;
291 bc = mem->bc;
292 rnorm = mem->rnorm;
293 cbval = mem->cbval;
294 wsamp_r = mem->wsamp_r;
295 phi = mem->phi;
296 energy = mem->energy;
297 window = mem->window;
298 c = mem->c;
299
300 snrtmp[0] = mem->snrtmp[0];
301 snrtmp[1] = mem->snrtmp[1];
302
303 numlines = mem->numlines;
304 partition = mem->partition;
305 tmn = mem->tmn;
306 s = mem->s;
307 lthr = mem->lthr;
308 r = mem->r;
309 phi_sav = mem->phi_sav;
310 fthr = mem->fthr;
311 absthr = mem->absthr;
312 }
313
314
315 for (ch = 0; ch < nch; ch++) {
316 for (i = 0; i < 2; i++) {
317 /*****************************************************************************
318 * Net offset is 480 samples (1056-576) for layer 2; this is because one must*
319 * stagger input data by 256 samples to synchronize psychoacoustic model with*
320 * filter bank outputs, then stagger so that center of 1024 FFT window lines *
321 * up with center of 576 "new" audio samples. *
322
323 flush = 384*3.0/2.0; = 576
324 syncsize = 1056;
325 sync_flush = syncsize - flush; 480
326 BLKSIZE = 1024
327 *****************************************************************************/
328 {
329 short int *bufferp = buffer[ch];
330 for (j = 0; j < 480; j++) {
331 savebuf[ch][j] = savebuf[ch][j + mem->flush];
332 wsamp_r[j] = window[j] * ((FLOAT) savebuf[ch][j]);
333 }
334 for (; j < 1024; j++) {
335 savebuf[ch][j] = *bufferp++;
336 wsamp_r[j] = window[j] * ((FLOAT) savebuf[ch][j]);
337 }
338 for (; j < 1056; j++)
339 savebuf[ch][j] = *bufferp++;
340 }
341
342 /**Compute FFT****************************************************************/
343 twolame_psycho_2_fft(wsamp_r, energy, phi);
344 /*****************************************************************************
345 * calculate the unpredictability measure, given energy[f] and phi[f] *
346 *****************************************************************************/
347 /* only update data "age" pointers after you are done with both channels */
348 /* for layer 1 computations, for the layer 2 FLOAT computations, the pointers */
349 /* are reset automatically on the second pass */
350 {
351 if (mem->new == 0) {
352 mem->new = 1;
353 mem->oldest = 1;
354 } else {
355 mem->new = 0;
356 mem->oldest = 0;
357 }
358 if (mem->old == 0)
359 mem->old = 1;
360 else
361 mem->old = 0;
362
363 new = mem->new;
364 old = mem->old;
365 oldest = mem->oldest;
366 }
367
368
369 for (j = 0; j < HBLKSIZE; j++) {
370 r_prime = 2.0 * r[ch][old][j] - r[ch][oldest][j];
371 phi_prime = 2.0 * phi_sav[ch][old][j] - phi_sav[ch][oldest][j];
372 r[ch][new][j] = sqrt((FLOAT) energy[j]);
373 phi_sav[ch][new][j] = phi[j];
374 #ifdef SINCOS
375 {
376 // 12% faster
377 // #warning "Use __sincos"
378 FLOAT sphi, cphi, sprime, cprime;
379 __sincos((FLOAT) phi[j], &sphi, &cphi);
380 __sincos((FLOAT) phi_prime, &sprime, &cprime);
381 temp1 = r[chn][new][j] * cphi - r_prime * cprime;
382 temp2 = r[chn][new][j] * sphi - r_prime * sprime;
383 }
384 #else
385 temp1 = r[ch][new][j] * cos((FLOAT) phi[j]) - r_prime * cos((FLOAT) phi_prime);
386 temp2 = r[ch][new][j] * sin((FLOAT) phi[j]) - r_prime * sin((FLOAT) phi_prime);
387 #endif
388
389 temp3 = r[ch][new][j] + fabs((FLOAT) r_prime);
390 if (temp3 != 0)
391 c[j] = sqrt(temp1 * temp1 + temp2 * temp2) / temp3;
392 else
393 c[j] = 0;
394 }
395 /*****************************************************************************
396 * Calculate the grouped, energy-weighted, unpredictability measure, *
397 * grouped_c[], and the grouped energy. grouped_e[] *
398 *****************************************************************************/
399
400 for (j = 1; j < CBANDS; j++) {
401 grouped_e[j] = 0;
402 grouped_c[j] = 0;
403 }
404 grouped_e[0] = energy[0];
405 grouped_c[0] = energy[0] * c[0];
406 for (j = 1; j < HBLKSIZE; j++) {
407 grouped_e[partition[j]] += energy[j];
408 grouped_c[partition[j]] += energy[j] * c[j];
409 }
410
411 /*****************************************************************************
412 * convolve the grouped energy-weighted unpredictability measure *
413 * and the grouped energy with the spreading function, s[j][k] *
414 *****************************************************************************/
415 for (j = 0; j < CBANDS; j++) {
416 ecb[j] = 0;
417 cb[j] = 0;
418 for (k = 0; k < CBANDS; k++) {
419 if (s[j][k] != 0.0) {
420 ecb[j] += s[j][k] * grouped_e[k];
421 cb[j] += s[j][k] * grouped_c[k];
422 }
423 }
424 if (ecb[j] != 0)
425 cb[j] = cb[j] / ecb[j];
426 else
427 cb[j] = 0;
428 }
429
430 /*****************************************************************************
431 * Calculate the required SNR for each of the frequency partitions *
432 * this whole section can be accomplished by a table lookup *
433 *****************************************************************************/
434 for (j = 0; j < CBANDS; j++) {
435 if (cb[j] < .05)
436 cb[j] = 0.05;
437 else if (cb[j] > .5)
438 cb[j] = 0.5;
439 tb = -0.434294482 * log((FLOAT) cb[j]) - 0.301029996;
440 cb[j] = tb;
441 bc[j] = tmn[j] * tb + nmt * (1.0 - tb);
442 k = (int) (cbval[j] + 0.5);
443 bc[j] = (bc[j] > bmax[k]) ? bc[j] : bmax[k];
444 bc[j] = exp((FLOAT) - bc[j] * LN_TO_LOG10);
445 }
446
447 /*****************************************************************************
448 * Calculate the permissible noise energy level in each of the frequency *
449 * partitions. Include absolute threshold and pre-echo controls *
450 * this whole section can be accomplished by a table lookup *
451 *****************************************************************************/
452 for (j = 0; j < CBANDS; j++)
453 if (rnorm[j] && numlines[j])
454 nb[j] = ecb[j] * bc[j] / (rnorm[j] * numlines[j]);
455 else
456 nb[j] = 0;
457 for (j = 0; j < HBLKSIZE; j++) {
458 /* temp1 is the preliminary threshold */
459 temp1 = nb[partition[j]];
460 temp1 = (temp1 > absthr[j]) ? temp1 : absthr[j];
461 #ifdef LAYERI
462 /* do not use pre-echo control for layer 2 because it may do bad things to the */
463 /* MUSICAM bit allocation algorithm */
464 if (lay == 1) {
465 fthr[j] = (temp1 < lthr[ch][j]) ? temp1 : lthr[ch][j];
466 temp2 = temp1 * 0.00316;
467 fthr[j] = (temp2 > fthr[j]) ? temp2 : fthr[j];
468 } else
469 fthr[j] = temp1;
470 lthr[ch][j] = LXMIN * temp1;
471 #else
472 fthr[j] = temp1;
473 lthr[ch][j] = LXMIN * temp1;
474 #endif
475 }
476
477 /*****************************************************************************
478 * Translate the 512 threshold values to the 32 filter bands of the coder *
479 *****************************************************************************/
480 for (j = 0; j < 193; j += 16) {
481 minthres = 60802371420160.0;
482 sum_energy = 0.0;
483 for (k = 0; k < 17; k++) {
484 if (minthres > fthr[j + k])
485 minthres = fthr[j + k];
486 sum_energy += energy[j + k];
487 }
488 snrtmp[i][j / 16] = sum_energy / (minthres * 17.0);
489 snrtmp[i][j / 16] = 4.342944819 * log((FLOAT) snrtmp[i][j / 16]);
490 }
491 for (j = 208; j < (HBLKSIZE - 1); j += 16) {
492 minthres = 0.0;
493 sum_energy = 0.0;
494 for (k = 0; k < 17; k++) {
495 minthres += fthr[j + k];
496 sum_energy += energy[j + k];
497 }
498 snrtmp[i][j / 16] = sum_energy / minthres;
499 snrtmp[i][j / 16] = 4.342944819 * log((FLOAT) snrtmp[i][j / 16]);
500 }
501 /*****************************************************************************
502 * End of Psychoacuostic calculation loop *
503 *****************************************************************************/
504 }
505 for (i = 0; i < 32; i++) {
506 smr[ch][i] = (snrtmp[0][i] > snrtmp[1][i]) ? snrtmp[0][i] : snrtmp[1][i];
507 }
508
509 } // next channel
510
511 }
512
513 void twolame_psycho_2_deinit(psycho_2_mem ** mem)
514 {
515
516 if (mem == NULL || *mem == NULL)
517 return;
518
519 TWOLAME_FREE((*mem)->tmn);
520 TWOLAME_FREE((*mem)->s);
521 TWOLAME_FREE((*mem)->lthr);
522 TWOLAME_FREE((*mem)->r);
523 TWOLAME_FREE((*mem)->phi_sav);
524
525 TWOLAME_FREE((*mem));
526 }
527
528
529 // vim:ts=4:sw=4:nowrap: