1/*
2 * AAC encoder psychoacoustic model
3 * Copyright (C) 2008 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file
24 * AAC encoder psychoacoustic model
25 */
26
27#include "avcodec.h"
28#include "aactab.h"
29#include "psymodel.h"
30
31/***********************************
32 *              TODOs:
33 * thresholds linearization after their modifications for attaining given bitrate
34 * try other bitrate controlling mechanism (maybe use ratecontrol.c?)
35 * control quality for quality-based output
36 **********************************/
37
38/**
39 * constants for 3GPP AAC psychoacoustic model
40 * @{
41 */
42#define PSY_3GPP_SPREAD_LOW  1.5f // spreading factor for ascending threshold spreading  (15 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
47/**
48 * @}
49 */
50
51/**
52 * information for single band used by 3GPP TS26.403-inspired psychoacoustic model
53 */
54typedef struct Psy3gppBand{
55    float energy;    ///< band energy
56    float ffac;      ///< form factor
57    float thr;       ///< energy threshold
58    float min_snr;   ///< minimal SNR
59    float thr_quiet; ///< threshold in quiet
60}Psy3gppBand;
61
62/**
63 * single/pair channel context for psychoacoustic model
64 */
65typedef 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;
74
75/**
76 * psychoacoustic model frame type-dependent coefficients
77 */
78typedef struct Psy3gppCoeffs{
79    float ath       [64]; ///< absolute threshold of hearing per bands
80    float barks     [64]; ///< Bark value for each spectral band in long frame
81    float spread_low[64]; ///< spreading factor for low-to-high threshold spreading in long frame
82    float spread_hi [64]; ///< spreading factor for high-to-low threshold spreading in long frame
83}Psy3gppCoeffs;
84
85/**
86 * 3GPP TS26.403-inspired psychoacoustic model specific data
87 */
88typedef struct Psy3gppContext{
89    Psy3gppCoeffs psy_coef[2];
90    Psy3gppChannel *ch;
91}Psy3gppContext;
92
93/**
94 * Calculate Bark value for given line.
95 */
96static av_cold float calc_bark(float f)
97{
98    return 13.3f * atanf(0.00076f * f) + 3.5f * atanf((f / 7500.0f) * (f / 7500.0f));
99}
100
101#define ATH_ADD 4
102/**
103 * Calculate ATH value for given frequency.
104 * Borrowed from Lame.
105 */
106static 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
115static 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 = FFMIN(minscale, ath(ctx->avctx->sample_rate * (start + i) / 1024.0 / 2.0, ATH_ADD));
145            coeffs->ath[g] = minscale - minath;
146            start += ctx->bands[j][g];
147        }
148    }
149
150    pctx->ch = av_mallocz(sizeof(Psy3gppChannel) * ctx->avctx->channels);
151    return 0;
152}
153
154/**
155 * IIR filter used in block switching decision
156 */
157static float iir_filter(int in, float state[2])
158{
159    float ret;
160
161    ret = 0.7548f * (in - state[0]) + 0.5095f * state[1];
162    state[0] = in;
163    state[1] = ret;
164    return ret;
165}
166
167/**
168 * window grouping information stored as bits (0 - new group, 1 - group continues)
169 */
170static const uint8_t window_grouping[9] = {
171    0xB6, 0x6C, 0xD8, 0xB2, 0x66, 0xC6, 0x96, 0x36, 0x36
172};
173
174/**
175 * Tell encoder which window types to use.
176 * @see 3GPP TS26.403 5.4.1 "Blockswitching"
177 */
178static FFPsyWindowInfo psy_3gpp_window(FFPsyContext *ctx,
179                                       const int16_t *audio, const int16_t *la,
180                                       int channel, int prev_type)
181{
182    int i, j;
183    int br               = ctx->avctx->bit_rate / ctx->avctx->channels;
184    int attack_ratio     = br <= 16000 ? 18 : 10;
185    Psy3gppContext *pctx = (Psy3gppContext*) ctx->model_priv_data;
186    Psy3gppChannel *pch  = &pctx->ch[channel];
187    uint8_t grouping     = 0;
188    FFPsyWindowInfo wi;
189
190    memset(&wi, 0, sizeof(wi));
191    if (la) {
192        float s[8], v;
193        int switch_to_eight = 0;
194        float sum = 0.0, sum2 = 0.0;
195        int attack_n = 0;
196        for (i = 0; i < 8; i++) {
197            for (j = 0; j < 128; j++) {
198                v = iir_filter(audio[(i*128+j)*ctx->avctx->channels], pch->iir_state);
199                sum += v*v;
200            }
201            s[i]  = sum;
202            sum2 += sum;
203        }
204        for (i = 0; i < 8; i++) {
205            if (s[i] > pch->win_energy * attack_ratio) {
206                attack_n        = i + 1;
207                switch_to_eight = 1;
208                break;
209            }
210        }
211        pch->win_energy = pch->win_energy*7/8 + sum2/64;
212
213        wi.window_type[1] = prev_type;
214        switch (prev_type) {
215        case ONLY_LONG_SEQUENCE:
216            wi.window_type[0] = switch_to_eight ? LONG_START_SEQUENCE : ONLY_LONG_SEQUENCE;
217            break;
218        case LONG_START_SEQUENCE:
219            wi.window_type[0] = EIGHT_SHORT_SEQUENCE;
220            grouping = pch->next_grouping;
221            break;
222        case LONG_STOP_SEQUENCE:
223            wi.window_type[0] = ONLY_LONG_SEQUENCE;
224            break;
225        case EIGHT_SHORT_SEQUENCE:
226            wi.window_type[0] = switch_to_eight ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
227            grouping = switch_to_eight ? pch->next_grouping : 0;
228            break;
229        }
230        pch->next_grouping = window_grouping[attack_n];
231    } else {
232        for (i = 0; i < 3; i++)
233            wi.window_type[i] = prev_type;
234        grouping = (prev_type == EIGHT_SHORT_SEQUENCE) ? window_grouping[0] : 0;
235    }
236
237    wi.window_shape   = 1;
238    if (wi.window_type[0] != EIGHT_SHORT_SEQUENCE) {
239        wi.num_windows = 1;
240        wi.grouping[0] = 1;
241    } else {
242        int lastgrp = 0;
243        wi.num_windows = 8;
244        for (i = 0; i < 8; i++) {
245            if (!((grouping >> i) & 1))
246                lastgrp = i;
247            wi.grouping[lastgrp]++;
248        }
249    }
250
251    return wi;
252}
253
254/**
255 * Calculate band thresholds as suggested in 3GPP TS26.403
256 */
257static void psy_3gpp_analyze(FFPsyContext *ctx, int channel,
258                             const float *coefs, FFPsyWindowInfo *wi)
259{
260    Psy3gppContext *pctx = (Psy3gppContext*) ctx->model_priv_data;
261    Psy3gppChannel *pch  = &pctx->ch[channel];
262    int start = 0;
263    int i, w, g;
264    const int num_bands       = ctx->num_bands[wi->num_windows == 8];
265    const uint8_t* band_sizes = ctx->bands[wi->num_windows == 8];
266    Psy3gppCoeffs *coeffs     = &pctx->psy_coef[wi->num_windows == 8];
267
268    //calculate energies, initial thresholds and related values - 5.4.2 "Threshold Calculation"
269    for (w = 0; w < wi->num_windows*16; w += 16) {
270        for (g = 0; g < num_bands; g++) {
271            Psy3gppBand *band = &pch->band[w+g];
272            band->energy = 0.0f;
273            for (i = 0; i < band_sizes[g]; i++)
274                band->energy += coefs[start+i] * coefs[start+i];
275            band->energy *= 1.0f / (512*512);
276            band->thr     = band->energy * 0.001258925f;
277            start        += band_sizes[g];
278
279            ctx->psy_bands[channel*PSY_MAX_BANDS+w+g].energy = band->energy;
280        }
281    }
282    //modify thresholds - spread, threshold in quiet - 5.4.3 "Spreaded Energy Calculation"
283    for (w = 0; w < wi->num_windows*16; w += 16) {
284        Psy3gppBand *band = &pch->band[w];
285        for (g = 1; g < num_bands; g++)
286            band[g].thr = FFMAX(band[g].thr, band[g-1].thr * coeffs->spread_low[g-1]);
287        for (g = num_bands - 2; g >= 0; g--)
288            band[g].thr = FFMAX(band[g].thr, band[g+1].thr * coeffs->spread_hi [g]);
289        for (g = 0; g < num_bands; g++) {
290            band[g].thr_quiet = FFMAX(band[g].thr, coeffs->ath[g]);
291            if (wi->num_windows != 8 && wi->window_type[1] != EIGHT_SHORT_SEQUENCE)
292                band[g].thr_quiet = FFMAX(PSY_3GPP_RPEMIN*band[g].thr_quiet,
293                                          FFMIN(band[g].thr_quiet,
294                                          PSY_3GPP_RPELEV*pch->prev_band[w+g].thr_quiet));
295            band[g].thr = FFMAX(band[g].thr, band[g].thr_quiet * 0.25);
296
297            ctx->psy_bands[channel*PSY_MAX_BANDS+w+g].threshold = band[g].thr;
298        }
299    }
300    memcpy(pch->prev_band, pch->band, sizeof(pch->band));
301}
302
303static av_cold void psy_3gpp_end(FFPsyContext *apc)
304{
305    Psy3gppContext *pctx = (Psy3gppContext*) apc->model_priv_data;
306    av_freep(&pctx->ch);
307    av_freep(&apc->model_priv_data);
308}
309
310
311const FFPsyModel ff_aac_psy_model =
312{
313    .name    = "3GPP TS 26.403-inspired model",
314    .init    = psy_3gpp_init,
315    .window  = psy_3gpp_window,
316    .analyze = psy_3gpp_analyze,
317    .end     = psy_3gpp_end,
318};
319