2008-08-18 08:39:44 +03:00
|
|
|
/*
|
|
|
|
* AAC encoder psychoacoustic model
|
|
|
|
* Copyright (C) 2008 Konstantin Shishkov
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2008-08-18 08:39:44 +03:00
|
|
|
* AAC encoder psychoacoustic model
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
|
|
|
#include "aactab.h"
|
2009-07-08 23:01:31 +03:00
|
|
|
#include "psymodel.h"
|
2008-08-18 08:39:44 +03:00
|
|
|
|
|
|
|
/***********************************
|
|
|
|
* TODOs:
|
|
|
|
* thresholds linearization after their modifications for attaining given bitrate
|
|
|
|
* try other bitrate controlling mechanism (maybe use ratecontrol.c?)
|
|
|
|
* control quality for quality-based output
|
|
|
|
**********************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* constants for 3GPP AAC psychoacoustic model
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
#define PSY_3GPP_SPREAD_LOW 1.5f // spreading factor for ascending threshold spreading (15 dB/Bark)
|
|
|
|
#define PSY_3GPP_SPREAD_HI 3.0f // spreading factor for descending threshold spreading (30 dB/Bark)
|
2009-07-08 23:01:31 +03:00
|
|
|
|
|
|
|
#define PSY_3GPP_RPEMIN 0.01f
|
|
|
|
#define PSY_3GPP_RPELEV 2.0f
|
2008-08-18 08:39:44 +03:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* information for single band used by 3GPP TS26.403-inspired psychoacoustic model
|
|
|
|
*/
|
|
|
|
typedef struct Psy3gppBand{
|
|
|
|
float energy; ///< band energy
|
|
|
|
float ffac; ///< form factor
|
2009-07-08 23:01:31 +03:00
|
|
|
float thr; ///< energy threshold
|
|
|
|
float min_snr; ///< minimal SNR
|
|
|
|
float thr_quiet; ///< threshold in quiet
|
2008-08-18 08:39:44 +03:00
|
|
|
}Psy3gppBand;
|
|
|
|
|
2009-07-08 23:01:31 +03:00
|
|
|
/**
|
|
|
|
* single/pair channel context for psychoacoustic model
|
|
|
|
*/
|
|
|
|
typedef struct Psy3gppChannel{
|
|
|
|
Psy3gppBand band[128]; ///< bands information
|
|
|
|
Psy3gppBand prev_band[128]; ///< bands information from the previous frame
|
|
|
|
|
|
|
|
float win_energy; ///< sliding average of channel energy
|
|
|
|
float iir_state[2]; ///< hi-pass IIR filter state
|
|
|
|
uint8_t next_grouping; ///< stored grouping scheme for the next frame (in case of 8 short window sequence)
|
|
|
|
enum WindowSequence next_window_seq; ///< window sequence to be used in the next frame
|
|
|
|
}Psy3gppChannel;
|
|
|
|
|
2008-08-18 08:39:44 +03:00
|
|
|
/**
|
|
|
|
* psychoacoustic model frame type-dependent coefficients
|
|
|
|
*/
|
|
|
|
typedef struct Psy3gppCoeffs{
|
|
|
|
float ath [64]; ///< absolute threshold of hearing per bands
|
|
|
|
float barks [64]; ///< Bark value for each spectral band in long frame
|
|
|
|
float spread_low[64]; ///< spreading factor for low-to-high threshold spreading in long frame
|
|
|
|
float spread_hi [64]; ///< spreading factor for high-to-low threshold spreading in long frame
|
|
|
|
}Psy3gppCoeffs;
|
|
|
|
|
2009-07-08 23:01:31 +03:00
|
|
|
/**
|
|
|
|
* 3GPP TS26.403-inspired psychoacoustic model specific data
|
|
|
|
*/
|
|
|
|
typedef struct Psy3gppContext{
|
|
|
|
Psy3gppCoeffs psy_coef[2];
|
|
|
|
Psy3gppChannel *ch;
|
|
|
|
}Psy3gppContext;
|
|
|
|
|
2008-08-18 08:39:44 +03:00
|
|
|
/**
|
|
|
|
* Calculate Bark value for given line.
|
|
|
|
*/
|
2009-07-08 23:01:31 +03:00
|
|
|
static av_cold float calc_bark(float f)
|
2008-08-18 08:39:44 +03:00
|
|
|
{
|
|
|
|
return 13.3f * atanf(0.00076f * f) + 3.5f * atanf((f / 7500.0f) * (f / 7500.0f));
|
|
|
|
}
|
2009-07-08 23:01:31 +03:00
|
|
|
|
|
|
|
#define ATH_ADD 4
|
|
|
|
/**
|
|
|
|
* Calculate ATH value for given frequency.
|
|
|
|
* Borrowed from Lame.
|
|
|
|
*/
|
|
|
|
static av_cold float ath(float f, float add)
|
|
|
|
{
|
|
|
|
f /= 1000.0f;
|
2009-07-09 00:16:06 +03:00
|
|
|
return 3.64 * pow(f, -0.8)
|
2009-07-08 23:01:31 +03:00
|
|
|
- 6.8 * exp(-0.6 * (f - 3.4) * (f - 3.4))
|
|
|
|
+ 6.0 * exp(-0.15 * (f - 8.7) * (f - 8.7))
|
|
|
|
+ (0.6 + 0.04 * add) * 0.001 * f * f * f * f;
|
|
|
|
}
|
|
|
|
|
2009-07-08 23:36:45 +03:00
|
|
|
static av_cold int psy_3gpp_init(FFPsyContext *ctx) {
|
2009-07-08 23:01:31 +03:00
|
|
|
Psy3gppContext *pctx;
|
2010-07-14 07:10:31 +03:00
|
|
|
float bark;
|
2009-07-08 23:01:31 +03:00
|
|
|
int i, j, g, start;
|
|
|
|
float prev, minscale, minath;
|
|
|
|
|
|
|
|
ctx->model_priv_data = av_mallocz(sizeof(Psy3gppContext));
|
|
|
|
pctx = (Psy3gppContext*) ctx->model_priv_data;
|
|
|
|
|
|
|
|
minath = ath(3410, ATH_ADD);
|
2009-07-08 23:36:45 +03:00
|
|
|
for (j = 0; j < 2; j++) {
|
2009-07-08 23:01:31 +03:00
|
|
|
Psy3gppCoeffs *coeffs = &pctx->psy_coef[j];
|
2010-07-14 07:10:52 +03:00
|
|
|
float line_to_frequency = ctx->avctx->sample_rate / (j ? 256.f : 2048.0f);
|
2009-07-08 23:01:31 +03:00
|
|
|
i = 0;
|
|
|
|
prev = 0.0;
|
2009-07-08 23:36:45 +03:00
|
|
|
for (g = 0; g < ctx->num_bands[j]; g++) {
|
2009-07-08 23:01:31 +03:00
|
|
|
i += ctx->bands[j][g];
|
2010-07-14 07:10:47 +03:00
|
|
|
bark = calc_bark((i-1) * line_to_frequency);
|
2010-07-14 07:10:31 +03:00
|
|
|
coeffs->barks[g] = (bark + prev) / 2.0;
|
|
|
|
prev = bark;
|
2009-07-08 23:01:31 +03:00
|
|
|
}
|
2009-07-08 23:36:45 +03:00
|
|
|
for (g = 0; g < ctx->num_bands[j] - 1; g++) {
|
2009-07-08 23:01:31 +03:00
|
|
|
coeffs->spread_low[g] = pow(10.0, -(coeffs->barks[g+1] - coeffs->barks[g]) * PSY_3GPP_SPREAD_LOW);
|
|
|
|
coeffs->spread_hi [g] = pow(10.0, -(coeffs->barks[g+1] - coeffs->barks[g]) * PSY_3GPP_SPREAD_HI);
|
|
|
|
}
|
|
|
|
start = 0;
|
2009-07-08 23:36:45 +03:00
|
|
|
for (g = 0; g < ctx->num_bands[j]; g++) {
|
2010-07-14 07:10:47 +03:00
|
|
|
minscale = ath(start * line_to_frequency, ATH_ADD);
|
2009-07-09 00:36:33 +03:00
|
|
|
for (i = 1; i < ctx->bands[j][g]; i++)
|
2010-07-14 07:10:47 +03:00
|
|
|
minscale = FFMIN(minscale, ath((start + i) * line_to_frequency, ATH_ADD));
|
2009-07-08 23:01:31 +03:00
|
|
|
coeffs->ath[g] = minscale - minath;
|
|
|
|
start += ctx->bands[j][g];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pctx->ch = av_mallocz(sizeof(Psy3gppChannel) * ctx->avctx->channels);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IIR filter used in block switching decision
|
|
|
|
*/
|
|
|
|
static float iir_filter(int in, float state[2])
|
|
|
|
{
|
|
|
|
float ret;
|
|
|
|
|
|
|
|
ret = 0.7548f * (in - state[0]) + 0.5095f * state[1];
|
|
|
|
state[0] = in;
|
|
|
|
state[1] = ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* window grouping information stored as bits (0 - new group, 1 - group continues)
|
|
|
|
*/
|
|
|
|
static const uint8_t window_grouping[9] = {
|
|
|
|
0xB6, 0x6C, 0xD8, 0xB2, 0x66, 0xC6, 0x96, 0x36, 0x36
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tell encoder which window types to use.
|
|
|
|
* @see 3GPP TS26.403 5.4.1 "Blockswitching"
|
|
|
|
*/
|
|
|
|
static FFPsyWindowInfo psy_3gpp_window(FFPsyContext *ctx,
|
|
|
|
const int16_t *audio, const int16_t *la,
|
|
|
|
int channel, int prev_type)
|
|
|
|
{
|
|
|
|
int i, j;
|
2009-07-09 00:16:06 +03:00
|
|
|
int br = ctx->avctx->bit_rate / ctx->avctx->channels;
|
|
|
|
int attack_ratio = br <= 16000 ? 18 : 10;
|
2009-07-08 23:01:31 +03:00
|
|
|
Psy3gppContext *pctx = (Psy3gppContext*) ctx->model_priv_data;
|
2009-07-09 00:16:06 +03:00
|
|
|
Psy3gppChannel *pch = &pctx->ch[channel];
|
|
|
|
uint8_t grouping = 0;
|
2010-06-29 00:49:26 +03:00
|
|
|
int next_type = pch->next_window_seq;
|
2009-07-08 23:01:31 +03:00
|
|
|
FFPsyWindowInfo wi;
|
|
|
|
|
|
|
|
memset(&wi, 0, sizeof(wi));
|
2009-07-08 23:36:45 +03:00
|
|
|
if (la) {
|
2009-07-08 23:01:31 +03:00
|
|
|
float s[8], v;
|
|
|
|
int switch_to_eight = 0;
|
|
|
|
float sum = 0.0, sum2 = 0.0;
|
|
|
|
int attack_n = 0;
|
2010-06-29 00:49:26 +03:00
|
|
|
int stay_short = 0;
|
2009-07-08 23:36:45 +03:00
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
for (j = 0; j < 128; j++) {
|
2010-05-14 19:49:11 +03:00
|
|
|
v = iir_filter(la[(i*128+j)*ctx->avctx->channels], pch->iir_state);
|
2009-07-08 23:01:31 +03:00
|
|
|
sum += v*v;
|
|
|
|
}
|
2009-07-09 00:16:06 +03:00
|
|
|
s[i] = sum;
|
2009-07-08 23:01:31 +03:00
|
|
|
sum2 += sum;
|
|
|
|
}
|
2009-07-08 23:36:45 +03:00
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
if (s[i] > pch->win_energy * attack_ratio) {
|
2009-07-09 00:16:06 +03:00
|
|
|
attack_n = i + 1;
|
2009-07-08 23:01:31 +03:00
|
|
|
switch_to_eight = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pch->win_energy = pch->win_energy*7/8 + sum2/64;
|
|
|
|
|
|
|
|
wi.window_type[1] = prev_type;
|
2009-07-08 23:36:45 +03:00
|
|
|
switch (prev_type) {
|
2009-07-08 23:01:31 +03:00
|
|
|
case ONLY_LONG_SEQUENCE:
|
|
|
|
wi.window_type[0] = switch_to_eight ? LONG_START_SEQUENCE : ONLY_LONG_SEQUENCE;
|
2010-06-29 00:49:26 +03:00
|
|
|
next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : ONLY_LONG_SEQUENCE;
|
2009-07-08 23:01:31 +03:00
|
|
|
break;
|
|
|
|
case LONG_START_SEQUENCE:
|
|
|
|
wi.window_type[0] = EIGHT_SHORT_SEQUENCE;
|
|
|
|
grouping = pch->next_grouping;
|
2010-06-29 00:49:26 +03:00
|
|
|
next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
|
2009-07-08 23:01:31 +03:00
|
|
|
break;
|
|
|
|
case LONG_STOP_SEQUENCE:
|
2010-06-29 00:49:26 +03:00
|
|
|
wi.window_type[0] = switch_to_eight ? LONG_START_SEQUENCE : ONLY_LONG_SEQUENCE;
|
|
|
|
next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : ONLY_LONG_SEQUENCE;
|
2009-07-08 23:01:31 +03:00
|
|
|
break;
|
|
|
|
case EIGHT_SHORT_SEQUENCE:
|
2010-06-29 00:49:26 +03:00
|
|
|
stay_short = next_type == EIGHT_SHORT_SEQUENCE || switch_to_eight;
|
|
|
|
wi.window_type[0] = stay_short ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
|
|
|
|
grouping = next_type == EIGHT_SHORT_SEQUENCE ? pch->next_grouping : 0;
|
|
|
|
next_type = switch_to_eight ? EIGHT_SHORT_SEQUENCE : LONG_STOP_SEQUENCE;
|
2009-07-08 23:01:31 +03:00
|
|
|
break;
|
|
|
|
}
|
2010-06-29 00:49:26 +03:00
|
|
|
|
2009-07-08 23:01:31 +03:00
|
|
|
pch->next_grouping = window_grouping[attack_n];
|
2010-06-29 00:49:26 +03:00
|
|
|
pch->next_window_seq = next_type;
|
2009-07-08 23:36:45 +03:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < 3; i++)
|
2009-07-08 23:01:31 +03:00
|
|
|
wi.window_type[i] = prev_type;
|
|
|
|
grouping = (prev_type == EIGHT_SHORT_SEQUENCE) ? window_grouping[0] : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
wi.window_shape = 1;
|
2009-07-08 23:36:45 +03:00
|
|
|
if (wi.window_type[0] != EIGHT_SHORT_SEQUENCE) {
|
2009-07-08 23:01:31 +03:00
|
|
|
wi.num_windows = 1;
|
|
|
|
wi.grouping[0] = 1;
|
2009-07-08 23:36:45 +03:00
|
|
|
} else {
|
2009-07-08 23:01:31 +03:00
|
|
|
int lastgrp = 0;
|
|
|
|
wi.num_windows = 8;
|
2009-07-08 23:36:45 +03:00
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
if (!((grouping >> i) & 1))
|
2009-07-08 23:01:31 +03:00
|
|
|
lastgrp = i;
|
|
|
|
wi.grouping[lastgrp]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return wi;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate band thresholds as suggested in 3GPP TS26.403
|
|
|
|
*/
|
2009-07-09 00:16:06 +03:00
|
|
|
static void psy_3gpp_analyze(FFPsyContext *ctx, int channel,
|
|
|
|
const float *coefs, FFPsyWindowInfo *wi)
|
2009-07-08 23:01:31 +03:00
|
|
|
{
|
|
|
|
Psy3gppContext *pctx = (Psy3gppContext*) ctx->model_priv_data;
|
2009-07-09 00:16:06 +03:00
|
|
|
Psy3gppChannel *pch = &pctx->ch[channel];
|
2009-07-08 23:01:31 +03:00
|
|
|
int start = 0;
|
|
|
|
int i, w, g;
|
2009-07-09 00:16:06 +03:00
|
|
|
const int num_bands = ctx->num_bands[wi->num_windows == 8];
|
2009-07-08 23:01:31 +03:00
|
|
|
const uint8_t* band_sizes = ctx->bands[wi->num_windows == 8];
|
2009-07-09 00:16:06 +03:00
|
|
|
Psy3gppCoeffs *coeffs = &pctx->psy_coef[wi->num_windows == 8];
|
2009-07-08 23:01:31 +03:00
|
|
|
|
|
|
|
//calculate energies, initial thresholds and related values - 5.4.2 "Threshold Calculation"
|
2009-07-08 23:36:45 +03:00
|
|
|
for (w = 0; w < wi->num_windows*16; w += 16) {
|
|
|
|
for (g = 0; g < num_bands; g++) {
|
2009-07-08 23:01:31 +03:00
|
|
|
Psy3gppBand *band = &pch->band[w+g];
|
|
|
|
band->energy = 0.0f;
|
2009-07-08 23:36:45 +03:00
|
|
|
for (i = 0; i < band_sizes[g]; i++)
|
2009-07-08 23:01:31 +03:00
|
|
|
band->energy += coefs[start+i] * coefs[start+i];
|
|
|
|
band->energy *= 1.0f / (512*512);
|
2009-07-09 00:16:06 +03:00
|
|
|
band->thr = band->energy * 0.001258925f;
|
|
|
|
start += band_sizes[g];
|
2009-07-08 23:01:31 +03:00
|
|
|
|
|
|
|
ctx->psy_bands[channel*PSY_MAX_BANDS+w+g].energy = band->energy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//modify thresholds - spread, threshold in quiet - 5.4.3 "Spreaded Energy Calculation"
|
2009-07-08 23:36:45 +03:00
|
|
|
for (w = 0; w < wi->num_windows*16; w += 16) {
|
2009-07-08 23:01:31 +03:00
|
|
|
Psy3gppBand *band = &pch->band[w];
|
2009-07-09 00:36:33 +03:00
|
|
|
for (g = 1; g < num_bands; g++)
|
2009-07-08 23:01:31 +03:00
|
|
|
band[g].thr = FFMAX(band[g].thr, band[g-1].thr * coeffs->spread_low[g-1]);
|
2009-07-09 00:36:33 +03:00
|
|
|
for (g = num_bands - 2; g >= 0; g--)
|
2009-07-08 23:01:31 +03:00
|
|
|
band[g].thr = FFMAX(band[g].thr, band[g+1].thr * coeffs->spread_hi [g]);
|
2009-07-08 23:36:45 +03:00
|
|
|
for (g = 0; g < num_bands; g++) {
|
2009-07-08 23:01:31 +03:00
|
|
|
band[g].thr_quiet = FFMAX(band[g].thr, coeffs->ath[g]);
|
2009-07-09 00:36:33 +03:00
|
|
|
if (wi->num_windows != 8 && wi->window_type[1] != EIGHT_SHORT_SEQUENCE)
|
2009-07-09 20:22:59 +03:00
|
|
|
band[g].thr_quiet = FFMAX(PSY_3GPP_RPEMIN*band[g].thr_quiet,
|
|
|
|
FFMIN(band[g].thr_quiet,
|
2009-07-08 23:01:31 +03:00
|
|
|
PSY_3GPP_RPELEV*pch->prev_band[w+g].thr_quiet));
|
|
|
|
band[g].thr = FFMAX(band[g].thr, band[g].thr_quiet * 0.25);
|
|
|
|
|
|
|
|
ctx->psy_bands[channel*PSY_MAX_BANDS+w+g].threshold = band[g].thr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(pch->prev_band, pch->band, sizeof(pch->band));
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void psy_3gpp_end(FFPsyContext *apc)
|
|
|
|
{
|
|
|
|
Psy3gppContext *pctx = (Psy3gppContext*) apc->model_priv_data;
|
|
|
|
av_freep(&pctx->ch);
|
|
|
|
av_freep(&apc->model_priv_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const FFPsyModel ff_aac_psy_model =
|
|
|
|
{
|
|
|
|
.name = "3GPP TS 26.403-inspired model",
|
|
|
|
.init = psy_3gpp_init,
|
|
|
|
.window = psy_3gpp_window,
|
|
|
|
.analyze = psy_3gpp_analyze,
|
|
|
|
.end = psy_3gpp_end,
|
|
|
|
};
|