]> git.sesse.net Git - ffmpeg/blob - libavcodec/aacsbr.c
avcodec: Template creation for AAC decoder (SBR-module)
[ffmpeg] / libavcodec / aacsbr.c
1 /*
2  * AAC Spectral Band Replication decoding functions
3  * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl )
4  * Copyright (c) 2009-2010 Alex Converse <alex.converse@gmail.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * AAC Spectral Band Replication decoding functions
26  * @author Robert Swain ( rob opendot cl )
27  */
28
29 #include "aac.h"
30 #include "sbr.h"
31 #include "aacsbr.h"
32 #include "aacsbrdata.h"
33 #include "aacsbr_tablegen.h"
34 #include "fft.h"
35 #include "aacps.h"
36 #include "sbrdsp.h"
37 #include "libavutil/internal.h"
38 #include "libavutil/libm.h"
39 #include "libavutil/avassert.h"
40
41 #include <stdint.h>
42 #include <float.h>
43 #include <math.h>
44
45 #if ARCH_MIPS
46 #include "mips/aacsbr_mips.h"
47 #endif /* ARCH_MIPS */
48
49 static VLC vlc_sbr[10];
50 static void aacsbr_func_ptr_init(AACSBRContext *c);
51
52 static void make_bands(int16_t* bands, int start, int stop, int num_bands)
53 {
54     int k, previous, present;
55     float base, prod;
56
57     base = powf((float)stop / start, 1.0f / num_bands);
58     prod = start;
59     previous = start;
60
61     for (k = 0; k < num_bands-1; k++) {
62         prod *= base;
63         present  = lrintf(prod);
64         bands[k] = present - previous;
65         previous = present;
66     }
67     bands[num_bands-1] = stop - previous;
68 }
69
70 /// Dequantization and stereo decoding (14496-3 sp04 p203)
71 static void sbr_dequant(SpectralBandReplication *sbr, int id_aac)
72 {
73     int k, e;
74     int ch;
75
76     if (id_aac == TYPE_CPE && sbr->bs_coupling) {
77         float alpha      = sbr->data[0].bs_amp_res ?  1.0f :  0.5f;
78         float pan_offset = sbr->data[0].bs_amp_res ? 12.0f : 24.0f;
79         for (e = 1; e <= sbr->data[0].bs_num_env; e++) {
80             for (k = 0; k < sbr->n[sbr->data[0].bs_freq_res[e]]; k++) {
81                 float temp1 = exp2f(sbr->data[0].env_facs[e][k] * alpha + 7.0f);
82                 float temp2 = exp2f((pan_offset - sbr->data[1].env_facs[e][k]) * alpha);
83                 float fac;
84                 if (temp1 > 1E20) {
85                     av_log(NULL, AV_LOG_ERROR, "envelope scalefactor overflow in dequant\n");
86                     temp1 = 1;
87                 }
88                 fac   = temp1 / (1.0f + temp2);
89                 sbr->data[0].env_facs[e][k] = fac;
90                 sbr->data[1].env_facs[e][k] = fac * temp2;
91             }
92         }
93         for (e = 1; e <= sbr->data[0].bs_num_noise; e++) {
94             for (k = 0; k < sbr->n_q; k++) {
95                 float temp1 = exp2f(NOISE_FLOOR_OFFSET - sbr->data[0].noise_facs[e][k] + 1);
96                 float temp2 = exp2f(12 - sbr->data[1].noise_facs[e][k]);
97                 float fac;
98                 if (temp1 > 1E20) {
99                     av_log(NULL, AV_LOG_ERROR, "envelope scalefactor overflow in dequant\n");
100                     temp1 = 1;
101                 }
102                 fac = temp1 / (1.0f + temp2);
103                 sbr->data[0].noise_facs[e][k] = fac;
104                 sbr->data[1].noise_facs[e][k] = fac * temp2;
105             }
106         }
107     } else { // SCE or one non-coupled CPE
108         for (ch = 0; ch < (id_aac == TYPE_CPE) + 1; ch++) {
109             float alpha = sbr->data[ch].bs_amp_res ? 1.0f : 0.5f;
110             for (e = 1; e <= sbr->data[ch].bs_num_env; e++)
111                 for (k = 0; k < sbr->n[sbr->data[ch].bs_freq_res[e]]; k++){
112                     sbr->data[ch].env_facs[e][k] =
113                         exp2f(alpha * sbr->data[ch].env_facs[e][k] + 6.0f);
114                     if (sbr->data[ch].env_facs[e][k] > 1E20) {
115                         av_log(NULL, AV_LOG_ERROR, "envelope scalefactor overflow in dequant\n");
116                         sbr->data[ch].env_facs[e][k] = 1;
117                     }
118                 }
119
120             for (e = 1; e <= sbr->data[ch].bs_num_noise; e++)
121                 for (k = 0; k < sbr->n_q; k++)
122                     sbr->data[ch].noise_facs[e][k] =
123                         exp2f(NOISE_FLOOR_OFFSET - sbr->data[ch].noise_facs[e][k]);
124         }
125     }
126 }
127
128 /** High Frequency Generation (14496-3 sp04 p214+) and Inverse Filtering
129  * (14496-3 sp04 p214)
130  * Warning: This routine does not seem numerically stable.
131  */
132 static void sbr_hf_inverse_filter(SBRDSPContext *dsp,
133                                   float (*alpha0)[2], float (*alpha1)[2],
134                                   const float X_low[32][40][2], int k0)
135 {
136     int k;
137     for (k = 0; k < k0; k++) {
138         LOCAL_ALIGNED_16(float, phi, [3], [2][2]);
139         float dk;
140
141         dsp->autocorrelate(X_low[k], phi);
142
143         dk =  phi[2][1][0] * phi[1][0][0] -
144              (phi[1][1][0] * phi[1][1][0] + phi[1][1][1] * phi[1][1][1]) / 1.000001f;
145
146         if (!dk) {
147             alpha1[k][0] = 0;
148             alpha1[k][1] = 0;
149         } else {
150             float temp_real, temp_im;
151             temp_real = phi[0][0][0] * phi[1][1][0] -
152                         phi[0][0][1] * phi[1][1][1] -
153                         phi[0][1][0] * phi[1][0][0];
154             temp_im   = phi[0][0][0] * phi[1][1][1] +
155                         phi[0][0][1] * phi[1][1][0] -
156                         phi[0][1][1] * phi[1][0][0];
157
158             alpha1[k][0] = temp_real / dk;
159             alpha1[k][1] = temp_im   / dk;
160         }
161
162         if (!phi[1][0][0]) {
163             alpha0[k][0] = 0;
164             alpha0[k][1] = 0;
165         } else {
166             float temp_real, temp_im;
167             temp_real = phi[0][0][0] + alpha1[k][0] * phi[1][1][0] +
168                                        alpha1[k][1] * phi[1][1][1];
169             temp_im   = phi[0][0][1] + alpha1[k][1] * phi[1][1][0] -
170                                        alpha1[k][0] * phi[1][1][1];
171
172             alpha0[k][0] = -temp_real / phi[1][0][0];
173             alpha0[k][1] = -temp_im   / phi[1][0][0];
174         }
175
176         if (alpha1[k][0] * alpha1[k][0] + alpha1[k][1] * alpha1[k][1] >= 16.0f ||
177            alpha0[k][0] * alpha0[k][0] + alpha0[k][1] * alpha0[k][1] >= 16.0f) {
178             alpha1[k][0] = 0;
179             alpha1[k][1] = 0;
180             alpha0[k][0] = 0;
181             alpha0[k][1] = 0;
182         }
183     }
184 }
185
186 /// Chirp Factors (14496-3 sp04 p214)
187 static void sbr_chirp(SpectralBandReplication *sbr, SBRData *ch_data)
188 {
189     int i;
190     float new_bw;
191     static const float bw_tab[] = { 0.0f, 0.75f, 0.9f, 0.98f };
192
193     for (i = 0; i < sbr->n_q; i++) {
194         if (ch_data->bs_invf_mode[0][i] + ch_data->bs_invf_mode[1][i] == 1) {
195             new_bw = 0.6f;
196         } else
197             new_bw = bw_tab[ch_data->bs_invf_mode[0][i]];
198
199         if (new_bw < ch_data->bw_array[i]) {
200             new_bw = 0.75f    * new_bw + 0.25f    * ch_data->bw_array[i];
201         } else
202             new_bw = 0.90625f * new_bw + 0.09375f * ch_data->bw_array[i];
203         ch_data->bw_array[i] = new_bw < 0.015625f ? 0.0f : new_bw;
204     }
205 }
206
207 /**
208  * Calculation of levels of additional HF signal components (14496-3 sp04 p219)
209  * and Calculation of gain (14496-3 sp04 p219)
210  */
211 static void sbr_gain_calc(AACContext *ac, SpectralBandReplication *sbr,
212                           SBRData *ch_data, const int e_a[2])
213 {
214     int e, k, m;
215     // max gain limits : -3dB, 0dB, 3dB, inf dB (limiter off)
216     static const float limgain[4] = { 0.70795, 1.0, 1.41254, 10000000000 };
217
218     for (e = 0; e < ch_data->bs_num_env; e++) {
219         int delta = !((e == e_a[1]) || (e == e_a[0]));
220         for (k = 0; k < sbr->n_lim; k++) {
221             float gain_boost, gain_max;
222             float sum[2] = { 0.0f, 0.0f };
223             for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
224                 const float temp = sbr->e_origmapped[e][m] / (1.0f + sbr->q_mapped[e][m]);
225                 sbr->q_m[e][m] = sqrtf(temp * sbr->q_mapped[e][m]);
226                 sbr->s_m[e][m] = sqrtf(temp * ch_data->s_indexmapped[e + 1][m]);
227                 if (!sbr->s_mapped[e][m]) {
228                     sbr->gain[e][m] = sqrtf(sbr->e_origmapped[e][m] /
229                                             ((1.0f + sbr->e_curr[e][m]) *
230                                              (1.0f + sbr->q_mapped[e][m] * delta)));
231                 } else {
232                     sbr->gain[e][m] = sqrtf(sbr->e_origmapped[e][m] * sbr->q_mapped[e][m] /
233                                             ((1.0f + sbr->e_curr[e][m]) *
234                                              (1.0f + sbr->q_mapped[e][m])));
235                 }
236             }
237             for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
238                 sum[0] += sbr->e_origmapped[e][m];
239                 sum[1] += sbr->e_curr[e][m];
240             }
241             gain_max = limgain[sbr->bs_limiter_gains] * sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));
242             gain_max = FFMIN(100000.f, gain_max);
243             for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
244                 float q_m_max   = sbr->q_m[e][m] * gain_max / sbr->gain[e][m];
245                 sbr->q_m[e][m]  = FFMIN(sbr->q_m[e][m], q_m_max);
246                 sbr->gain[e][m] = FFMIN(sbr->gain[e][m], gain_max);
247             }
248             sum[0] = sum[1] = 0.0f;
249             for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
250                 sum[0] += sbr->e_origmapped[e][m];
251                 sum[1] += sbr->e_curr[e][m] * sbr->gain[e][m] * sbr->gain[e][m]
252                           + sbr->s_m[e][m] * sbr->s_m[e][m]
253                           + (delta && !sbr->s_m[e][m]) * sbr->q_m[e][m] * sbr->q_m[e][m];
254             }
255             gain_boost = sqrtf((FLT_EPSILON + sum[0]) / (FLT_EPSILON + sum[1]));
256             gain_boost = FFMIN(1.584893192f, gain_boost);
257             for (m = sbr->f_tablelim[k] - sbr->kx[1]; m < sbr->f_tablelim[k + 1] - sbr->kx[1]; m++) {
258                 sbr->gain[e][m] *= gain_boost;
259                 sbr->q_m[e][m]  *= gain_boost;
260                 sbr->s_m[e][m]  *= gain_boost;
261             }
262         }
263     }
264 }
265
266 /// Assembling HF Signals (14496-3 sp04 p220)
267 static void sbr_hf_assemble(float Y1[38][64][2],
268                             const float X_high[64][40][2],
269                             SpectralBandReplication *sbr, SBRData *ch_data,
270                             const int e_a[2])
271 {
272     int e, i, j, m;
273     const int h_SL = 4 * !sbr->bs_smoothing_mode;
274     const int kx = sbr->kx[1];
275     const int m_max = sbr->m[1];
276     static const float h_smooth[5] = {
277         0.33333333333333,
278         0.30150283239582,
279         0.21816949906249,
280         0.11516383427084,
281         0.03183050093751,
282     };
283     float (*g_temp)[48] = ch_data->g_temp, (*q_temp)[48] = ch_data->q_temp;
284     int indexnoise = ch_data->f_indexnoise;
285     int indexsine  = ch_data->f_indexsine;
286
287     if (sbr->reset) {
288         for (i = 0; i < h_SL; i++) {
289             memcpy(g_temp[i + 2*ch_data->t_env[0]], sbr->gain[0], m_max * sizeof(sbr->gain[0][0]));
290             memcpy(q_temp[i + 2*ch_data->t_env[0]], sbr->q_m[0],  m_max * sizeof(sbr->q_m[0][0]));
291         }
292     } else if (h_SL) {
293         for (i = 0; i < 4; i++) {
294             memcpy(g_temp[i + 2 * ch_data->t_env[0]],
295                    g_temp[i + 2 * ch_data->t_env_num_env_old],
296                    sizeof(g_temp[0]));
297             memcpy(q_temp[i + 2 * ch_data->t_env[0]],
298                    q_temp[i + 2 * ch_data->t_env_num_env_old],
299                    sizeof(q_temp[0]));
300         }
301     }
302
303     for (e = 0; e < ch_data->bs_num_env; e++) {
304         for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
305             memcpy(g_temp[h_SL + i], sbr->gain[e], m_max * sizeof(sbr->gain[0][0]));
306             memcpy(q_temp[h_SL + i], sbr->q_m[e],  m_max * sizeof(sbr->q_m[0][0]));
307         }
308     }
309
310     for (e = 0; e < ch_data->bs_num_env; e++) {
311         for (i = 2 * ch_data->t_env[e]; i < 2 * ch_data->t_env[e + 1]; i++) {
312             LOCAL_ALIGNED_16(float, g_filt_tab, [48]);
313             LOCAL_ALIGNED_16(float, q_filt_tab, [48]);
314             float *g_filt, *q_filt;
315
316             if (h_SL && e != e_a[0] && e != e_a[1]) {
317                 g_filt = g_filt_tab;
318                 q_filt = q_filt_tab;
319                 for (m = 0; m < m_max; m++) {
320                     const int idx1 = i + h_SL;
321                     g_filt[m] = 0.0f;
322                     q_filt[m] = 0.0f;
323                     for (j = 0; j <= h_SL; j++) {
324                         g_filt[m] += g_temp[idx1 - j][m] * h_smooth[j];
325                         q_filt[m] += q_temp[idx1 - j][m] * h_smooth[j];
326                     }
327                 }
328             } else {
329                 g_filt = g_temp[i + h_SL];
330                 q_filt = q_temp[i];
331             }
332
333             sbr->dsp.hf_g_filt(Y1[i] + kx, X_high + kx, g_filt, m_max,
334                                i + ENVELOPE_ADJUSTMENT_OFFSET);
335
336             if (e != e_a[0] && e != e_a[1]) {
337                 sbr->dsp.hf_apply_noise[indexsine](Y1[i] + kx, sbr->s_m[e],
338                                                    q_filt, indexnoise,
339                                                    kx, m_max);
340             } else {
341                 int idx = indexsine&1;
342                 int A = (1-((indexsine+(kx & 1))&2));
343                 int B = (A^(-idx)) + idx;
344                 float *out = &Y1[i][kx][idx];
345                 float *in  = sbr->s_m[e];
346                 for (m = 0; m+1 < m_max; m+=2) {
347                     out[2*m  ] += in[m  ] * A;
348                     out[2*m+2] += in[m+1] * B;
349                 }
350                 if(m_max&1)
351                     out[2*m  ] += in[m  ] * A;
352             }
353             indexnoise = (indexnoise + m_max) & 0x1ff;
354             indexsine = (indexsine + 1) & 3;
355         }
356     }
357     ch_data->f_indexnoise = indexnoise;
358     ch_data->f_indexsine  = indexsine;
359 }
360
361 #include "aacsbr_template.c"