comparison aacpsy.c @ 9935:d09283aeeef8 libavcodec

Merge the AAC encoder from SoC svn. It is still considered experimental.
author alexc
date Wed, 08 Jul 2009 20:01:31 +0000
parents e9d9d946f213
children 7f42ae22c351
comparison
equal deleted inserted replaced
9934:ff96ee73b08b 9935:d09283aeeef8
23 * @file libavcodec/aacpsy.c 23 * @file libavcodec/aacpsy.c
24 * AAC encoder psychoacoustic model 24 * AAC encoder psychoacoustic model
25 */ 25 */
26 26
27 #include "avcodec.h" 27 #include "avcodec.h"
28 #include "aacpsy.h"
29 #include "aactab.h" 28 #include "aactab.h"
29 #include "psymodel.h"
30 30
31 /*********************************** 31 /***********************************
32 * TODOs: 32 * TODOs:
33 * General:
34 * better audio preprocessing (add DC highpass filter?)
35 * more psy models
36 * maybe improve coefficient quantization function in some way
37 *
38 * 3GPP-based psy model:
39 * thresholds linearization after their modifications for attaining given bitrate 33 * thresholds linearization after their modifications for attaining given bitrate
40 * try other bitrate controlling mechanism (maybe use ratecontrol.c?) 34 * try other bitrate controlling mechanism (maybe use ratecontrol.c?)
41 * control quality for quality-based output 35 * control quality for quality-based output
42 **********************************/ 36 **********************************/
43 37
44 /** 38 /**
45 * Quantize one coefficient.
46 * @return absolute value of the quantized coefficient
47 * @see 3GPP TS26.403 5.6.2 "Scalefactor determination"
48 */
49 static av_always_inline int quant(float coef, const float Q)
50 {
51 return av_clip((int)(pow(fabsf(coef) * Q, 0.75) + 0.4054), 0, 8191);
52 }
53
54 static inline float get_approximate_quant_error(float *c, int size, int scale_idx)
55 {
56 int i;
57 int q;
58 float coef, unquant, sum = 0.0f;
59 const float Q = ff_aac_pow2sf_tab[200 - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
60 const float IQ = ff_aac_pow2sf_tab[200 + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
61 for(i = 0; i < size; i++){
62 coef = fabs(c[i]);
63 q = quant(c[i], Q);
64 unquant = (q * cbrt(q)) * IQ;
65 sum += (coef - unquant) * (coef - unquant);
66 }
67 return sum;
68 }
69
70 /**
71 * constants for 3GPP AAC psychoacoustic model 39 * constants for 3GPP AAC psychoacoustic model
72 * @{ 40 * @{
73 */ 41 */
74 #define PSY_3GPP_SPREAD_LOW 1.5f // spreading factor for ascending threshold spreading (15 dB/Bark) 42 #define PSY_3GPP_SPREAD_LOW 1.5f // spreading factor for ascending threshold spreading (15 dB/Bark)
75 #define PSY_3GPP_SPREAD_HI 3.0f // spreading factor for descending threshold spreading (30 dB/Bark) 43 #define PSY_3GPP_SPREAD_HI 3.0f // spreading factor for descending threshold spreading (30 dB/Bark)
44
45 #define PSY_3GPP_RPEMIN 0.01f
46 #define PSY_3GPP_RPELEV 2.0f
76 /** 47 /**
77 * @} 48 * @}
78 */ 49 */
79 50
80 /** 51 /**
81 * information for single band used by 3GPP TS26.403-inspired psychoacoustic model 52 * information for single band used by 3GPP TS26.403-inspired psychoacoustic model
82 */ 53 */
83 typedef struct Psy3gppBand{ 54 typedef struct Psy3gppBand{
84 float energy; ///< band energy 55 float energy; ///< band energy
85 float ffac; ///< form factor 56 float ffac; ///< form factor
57 float thr; ///< energy threshold
58 float min_snr; ///< minimal SNR
59 float thr_quiet; ///< threshold in quiet
86 }Psy3gppBand; 60 }Psy3gppBand;
61
62 /**
63 * single/pair channel context for psychoacoustic model
64 */
65 typedef struct Psy3gppChannel{
66 Psy3gppBand band[128]; ///< bands information
67 Psy3gppBand prev_band[128]; ///< bands information from the previous frame
68
69 float win_energy; ///< sliding average of channel energy
70 float iir_state[2]; ///< hi-pass IIR filter state
71 uint8_t next_grouping; ///< stored grouping scheme for the next frame (in case of 8 short window sequence)
72 enum WindowSequence next_window_seq; ///< window sequence to be used in the next frame
73 }Psy3gppChannel;
87 74
88 /** 75 /**
89 * psychoacoustic model frame type-dependent coefficients 76 * psychoacoustic model frame type-dependent coefficients
90 */ 77 */
91 typedef struct Psy3gppCoeffs{ 78 typedef struct Psy3gppCoeffs{
94 float spread_low[64]; ///< spreading factor for low-to-high threshold spreading in long frame 81 float spread_low[64]; ///< spreading factor for low-to-high threshold spreading in long frame
95 float spread_hi [64]; ///< spreading factor for high-to-low threshold spreading in long frame 82 float spread_hi [64]; ///< spreading factor for high-to-low threshold spreading in long frame
96 }Psy3gppCoeffs; 83 }Psy3gppCoeffs;
97 84
98 /** 85 /**
86 * 3GPP TS26.403-inspired psychoacoustic model specific data
87 */
88 typedef struct Psy3gppContext{
89 Psy3gppCoeffs psy_coef[2];
90 Psy3gppChannel *ch;
91 }Psy3gppContext;
92
93 /**
99 * Calculate Bark value for given line. 94 * Calculate Bark value for given line.
100 */ 95 */
101 static inline float calc_bark(float f) 96 static av_cold float calc_bark(float f)
102 { 97 {
103 return 13.3f * atanf(0.00076f * f) + 3.5f * atanf((f / 7500.0f) * (f / 7500.0f)); 98 return 13.3f * atanf(0.00076f * f) + 3.5f * atanf((f / 7500.0f) * (f / 7500.0f));
104 } 99 }
100
101 #define ATH_ADD 4
102 /**
103 * Calculate ATH value for given frequency.
104 * Borrowed from Lame.
105 */
106 static av_cold float ath(float f, float add)
107 {
108 f /= 1000.0f;
109 return 3.64 * pow(f, -0.8)
110 - 6.8 * exp(-0.6 * (f - 3.4) * (f - 3.4))
111 + 6.0 * exp(-0.15 * (f - 8.7) * (f - 8.7))
112 + (0.6 + 0.04 * add) * 0.001 * f * f * f * f;
113 }
114
115 static av_cold int psy_3gpp_init(FFPsyContext *ctx){
116 Psy3gppContext *pctx;
117 float barks[1024];
118 int i, j, g, start;
119 float prev, minscale, minath;
120
121 ctx->model_priv_data = av_mallocz(sizeof(Psy3gppContext));
122 pctx = (Psy3gppContext*) ctx->model_priv_data;
123
124 for(i = 0; i < 1024; i++)
125 barks[i] = calc_bark(i * ctx->avctx->sample_rate / 2048.0);
126 minath = ath(3410, ATH_ADD);
127 for(j = 0; j < 2; j++){
128 Psy3gppCoeffs *coeffs = &pctx->psy_coef[j];
129 i = 0;
130 prev = 0.0;
131 for(g = 0; g < ctx->num_bands[j]; g++){
132 i += ctx->bands[j][g];
133 coeffs->barks[g] = (barks[i - 1] + prev) / 2.0;
134 prev = barks[i - 1];
135 }
136 for(g = 0; g < ctx->num_bands[j] - 1; g++){
137 coeffs->spread_low[g] = pow(10.0, -(coeffs->barks[g+1] - coeffs->barks[g]) * PSY_3GPP_SPREAD_LOW);
138 coeffs->spread_hi [g] = pow(10.0, -(coeffs->barks[g+1] - coeffs->barks[g]) * PSY_3GPP_SPREAD_HI);
139 }
140 start = 0;
141 for(g = 0; g < ctx->num_bands[j]; g++){
142 minscale = ath(ctx->avctx->sample_rate * start / 1024.0, ATH_ADD);
143 for(i = 1; i < ctx->bands[j][g]; i++){
144 minscale = fminf(minscale, ath(ctx->avctx->sample_rate * (start + i) / 1024.0 / 2.0, ATH_ADD));
145 }
146 coeffs->ath[g] = minscale - minath;
147 start += ctx->bands[j][g];
148 }
149 }
150
151 pctx->ch = av_mallocz(sizeof(Psy3gppChannel) * ctx->avctx->channels);
152 return 0;
153 }
154
155 /**
156 * IIR filter used in block switching decision
157 */
158 static float iir_filter(int in, float state[2])
159 {
160 float ret;
161
162 ret = 0.7548f * (in - state[0]) + 0.5095f * state[1];
163 state[0] = in;
164 state[1] = ret;
165 return ret;
166 }
167
168 /**
169 * window grouping information stored as bits (0 - new group, 1 - group continues)
170 */
171 static const uint8_t window_grouping[9] = {
172 0xB6, 0x6C, 0xD8, 0xB2, 0x66, 0xC6, 0x96, 0x36, 0x36
173 };
174
175 /**
176 * Tell encoder which window types to use.
177 * @see 3GPP TS26.403 5.4.1 "Blockswitching"
178 */
179 static FFPsyWindowInfo psy_3gpp_window(FFPsyContext *ctx,
180 const int16_t *audio, const int16_t *la,
181 int channel, int prev_type)
182 {
183 int i, j;
184 int br = ctx->avctx->bit_rate / ctx->avctx->channels;
185 int attack_ratio = br <= 16000 ? 18 : 10;
186 Psy3gppContext *pctx = (Psy3gppContext*) ctx->model_priv_data;
187 Psy3gppChannel *pch = &pctx->ch[channel];
188 uint8_t grouping = 0;
189 FFPsyWindowInfo wi;
190
191 memset(&wi, 0, sizeof(wi));
192 if(la){
193 float s[8], v;
194 int switch_to_eight = 0;
195 float sum = 0.0, sum2 = 0.0;
196 int attack_n = 0;
197 for(i = 0; i < 8; i++){
198 for(j = 0; j < 128; j++){
199 v = iir_filter(audio[(i*128+j)*ctx->avctx->channels], pch->iir_state);
200 sum += v*v;
201 }
202 s[i] = sum;
203 sum2 += sum;
204 }
205 for(i = 0; i < 8; i++){
206 if(s[i] > pch->win_energy * attack_ratio){
207 attack_n = i + 1;
208 switch_to_eight = 1;
209 break;
210 }
211 }
212 pch->win_energy = pch->win_energy*7/8 + sum2/64;
213
214 wi.window_type[1] = prev_type;
215 switch(prev_type){
216 case ONLY_LONG_SEQUENCE:
217 wi.window_type[0] = switch_to_eight ? LONG_START_SEQUENCE : ONLY_LONG_SEQUENCE;
218 break;
219 case LONG_START_SEQUENCE:
220 wi.window_type[0] = EIGHT_SHORT_SEQUENCE;
221 grouping = pch->next_grouping;
222 break;
223 case LONG_STOP_SEQUENCE:
224 wi.window_type[0] = ONLY_LONG_SEQUENCE;
225 break;
226 case EIGHT_SHORT_SEQUENCE:
227 wi.window_type[0] = switch_to_eight ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
228 grouping = switch_to_eight ? pch->next_grouping : 0;
229 break;
230 }
231 pch->next_grouping = window_grouping[attack_n];
232 }else{
233 for(i = 0; i < 3; i++)
234 wi.window_type[i] = prev_type;
235 grouping = (prev_type == EIGHT_SHORT_SEQUENCE) ? window_grouping[0] : 0;
236 }
237
238 wi.window_shape = 1;
239 if(wi.window_type[0] != EIGHT_SHORT_SEQUENCE){
240 wi.num_windows = 1;
241 wi.grouping[0] = 1;
242 }else{
243 int lastgrp = 0;
244 wi.num_windows = 8;
245 for(i = 0; i < 8; i++){
246 if(!((grouping >> i) & 1))
247 lastgrp = i;
248 wi.grouping[lastgrp]++;
249 }
250 }
251
252 return wi;
253 }
254
255 /**
256 * Calculate band thresholds as suggested in 3GPP TS26.403
257 */
258 static void psy_3gpp_analyze(FFPsyContext *ctx, int channel, const float *coefs,
259 FFPsyWindowInfo *wi)
260 {
261 Psy3gppContext *pctx = (Psy3gppContext*) ctx->model_priv_data;
262 Psy3gppChannel *pch = &pctx->ch[channel];
263 int start = 0;
264 int i, w, g;
265 const int num_bands = ctx->num_bands[wi->num_windows == 8];
266 const uint8_t* band_sizes = ctx->bands[wi->num_windows == 8];
267 Psy3gppCoeffs *coeffs = &pctx->psy_coef[wi->num_windows == 8];
268
269 //calculate energies, initial thresholds and related values - 5.4.2 "Threshold Calculation"
270 for(w = 0; w < wi->num_windows*16; w += 16){
271 for(g = 0; g < num_bands; g++){
272 Psy3gppBand *band = &pch->band[w+g];
273 band->energy = 0.0f;
274 for(i = 0; i < band_sizes[g]; i++)
275 band->energy += coefs[start+i] * coefs[start+i];
276 band->energy *= 1.0f / (512*512);
277 band->thr = band->energy * 0.001258925f;
278 start += band_sizes[g];
279
280 ctx->psy_bands[channel*PSY_MAX_BANDS+w+g].energy = band->energy;
281 }
282 }
283 //modify thresholds - spread, threshold in quiet - 5.4.3 "Spreaded Energy Calculation"
284 for(w = 0; w < wi->num_windows*16; w += 16){
285 Psy3gppBand *band = &pch->band[w];
286 for(g = 1; g < num_bands; g++){
287 band[g].thr = FFMAX(band[g].thr, band[g-1].thr * coeffs->spread_low[g-1]);
288 }
289 for(g = num_bands - 2; g >= 0; g--){
290 band[g].thr = FFMAX(band[g].thr, band[g+1].thr * coeffs->spread_hi [g]);
291 }
292 for(g = 0; g < num_bands; g++){
293 band[g].thr_quiet = FFMAX(band[g].thr, coeffs->ath[g]);
294 if(wi->num_windows != 8 && wi->window_type[1] != EIGHT_SHORT_SEQUENCE){
295 band[g].thr_quiet = fmaxf(PSY_3GPP_RPEMIN*band[g].thr_quiet,
296 fminf(band[g].thr_quiet,
297 PSY_3GPP_RPELEV*pch->prev_band[w+g].thr_quiet));
298 }
299 band[g].thr = FFMAX(band[g].thr, band[g].thr_quiet * 0.25);
300
301 ctx->psy_bands[channel*PSY_MAX_BANDS+w+g].threshold = band[g].thr;
302 }
303 }
304 memcpy(pch->prev_band, pch->band, sizeof(pch->band));
305 }
306
307 static av_cold void psy_3gpp_end(FFPsyContext *apc)
308 {
309 Psy3gppContext *pctx = (Psy3gppContext*) apc->model_priv_data;
310 av_freep(&pctx->ch);
311 av_freep(&apc->model_priv_data);
312 }
313
314
315 const FFPsyModel ff_aac_psy_model =
316 {
317 .name = "3GPP TS 26.403-inspired model",
318 .init = psy_3gpp_init,
319 .window = psy_3gpp_window,
320 .analyze = psy_3gpp_analyze,
321 .end = psy_3gpp_end,
322 };