| 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 <math.h> |
| 26 |
|
| 27 |
#include "twolame.h" |
| 28 |
#include "common.h" |
| 29 |
#include "ath.h" |
| 30 |
#include "mem.h" |
| 31 |
#include "psycho_0.h" |
| 32 |
|
| 33 |
/* MFC Mar 03 |
| 34 |
It's almost obscene how well this psycho model works for the amount of |
| 35 |
computational effort that's put in. |
| 36 |
|
| 37 |
I got the idea from: |
| 38 |
Hyen-O Oh et al "Low power mpeg audio encoders using simplified psychoacoustic model |
| 39 |
and fast bit allocation" |
| 40 |
IEEE Trans on Consumer Electronics v47 n3 August 2001. p613 |
| 41 |
|
| 42 |
All this model does is look at the lowest ATH value within the subband, and then looks |
| 43 |
at the scalefactors. It combines the two in a real dodgy way to get the SMRs. |
| 44 |
|
| 45 |
Although the output values aren't really close to any of the other psycho models, |
| 46 |
the spread of values and the relative sizes of the values for the different subbands |
| 47 |
is about right |
| 48 |
|
| 49 |
Feel free to make any sort of generic change you want. Add or subtract numbers, take |
| 50 |
logs, whatever. Fiddle with the numbers until we get a good SMR output */ |
| 51 |
|
| 52 |
|
| 53 |
static psycho_0_mem *twolame_psycho_0_init(twolame_options * glopts, int sfreq) |
| 54 |
{ |
| 55 |
FLOAT freqperline = (FLOAT) sfreq / 1024.0; |
| 56 |
psycho_0_mem *mem = (psycho_0_mem *) TWOLAME_MALLOC(sizeof(psycho_0_mem)); |
| 57 |
int sb, i; |
| 58 |
|
| 59 |
for (sb = 0; sb < SBLIMIT; sb++) { |
| 60 |
mem->ath_min[sb] = 1000; /* set it huge */ |
| 61 |
} |
| 62 |
|
| 63 |
/* Find the minimum ATH in each subband */ |
| 64 |
for (i = 0; i < 512; i++) { |
| 65 |
FLOAT thisfreq = i * freqperline; |
| 66 |
FLOAT ath_val = twolame_ath_db(thisfreq, 0); |
| 67 |
if (ath_val < mem->ath_min[i >> 4]) |
| 68 |
mem->ath_min[i >> 4] = ath_val; |
| 69 |
} |
| 70 |
|
| 71 |
return mem; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
void twolame_psycho_0(twolame_options * glopts, FLOAT SMR[2][SBLIMIT], unsigned int scalar[2][3][SBLIMIT]) |
| 77 |
{ |
| 78 |
psycho_0_mem *mem; |
| 79 |
int nch = glopts->num_channels_out; |
| 80 |
int sfreq = glopts->samplerate_out; |
| 81 |
int ch, sb, gr; |
| 82 |
unsigned int minscaleindex[2][SBLIMIT]; /* Smaller scale indexes mean bigger scalefactors */ |
| 83 |
|
| 84 |
if (!glopts->p0mem) { |
| 85 |
glopts->p0mem = twolame_psycho_0_init(glopts, sfreq); |
| 86 |
} |
| 87 |
mem = glopts->p0mem; |
| 88 |
|
| 89 |
|
| 90 |
/* call functions for critical boundaries, freq. */ |
| 91 |
if (!glopts->p0mem) { /* bands, bark values, and mapping */ |
| 92 |
|
| 93 |
} else { |
| 94 |
|
| 95 |
mem = glopts->p0mem; |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/* Find the minimum scalefactor index for each ch/sb */ |
| 101 |
for (ch = 0; ch < nch; ch++) |
| 102 |
for (sb = 0; sb < SBLIMIT; sb++) |
| 103 |
minscaleindex[ch][sb] = scalar[ch][0][sb]; |
| 104 |
|
| 105 |
for (ch = 0; ch < nch; ch++) |
| 106 |
for (gr = 1; gr < 3; gr++) |
| 107 |
for (sb = 0; sb < SBLIMIT; sb++) |
| 108 |
if (minscaleindex[ch][sb] > scalar[ch][gr][sb]) |
| 109 |
minscaleindex[ch][sb] = scalar[ch][gr][sb]; |
| 110 |
|
| 111 |
/* Oh yeah. Fudge the hell out of the SMR calculations by combining the scalefactor table index |
| 112 |
and the min ATH in that subband There are probably more elegant/correct ways of combining |
| 113 |
these values, but who cares? It works pretty well MFC Mar 03 */ |
| 114 |
for (ch = 0; ch < nch; ch++) |
| 115 |
for (sb = 0; sb < SBLIMIT; sb++) |
| 116 |
SMR[ch][sb] = 2.0 * (30.0 - minscaleindex[ch][sb]) - mem->ath_min[sb]; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
void twolame_psycho_0_deinit(psycho_0_mem ** mem) |
| 121 |
{ |
| 122 |
|
| 123 |
if (mem == NULL || *mem == NULL) |
| 124 |
return; |
| 125 |
|
| 126 |
TWOLAME_FREE(*mem); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
// vim:ts=4:sw=4:nowrap: |