1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

aacenc_tns: implement temporal noise shaping

This commit implements temporal noise shaping support in the
encoder, along with an -aac_tns option to toggle it on or off
(off by default for now). TNS will increase audio quality
and reduce quantization noise by applying a multitap FIR filter
across allowed coefficients and transmit side information to the
decoder so it could create an inverse filter.

Users are encouraged to test the new functionality by enabling
-aac_tns 1 during encoding.

No major bugs are observable at this time so after a while if no
new problems appear and if the current implementation is deemed
of high enough quality and stability it will be enabled by default,
possibly at the same time the encoder has its experimental flag
removed and becomes the standard aac encoder in ffmpeg.

Signed-off-by: Rostislav Pehlivanov <atomnuker@gmail.com>
This commit is contained in:
Rostislav Pehlivanov 2015-08-21 19:27:38 +01:00
parent eab12d072e
commit a1c487e921
7 changed files with 311 additions and 6 deletions

View File

@ -133,6 +133,7 @@ OBJS-$(CONFIG_AAC_FIXED_DECODER) += aacdec_fixed.o aactab.o aacsbr_fixed.o
OBJS-$(CONFIG_AAC_ENCODER) += aacenc.o aaccoder.o aacenctab.o \ OBJS-$(CONFIG_AAC_ENCODER) += aacenc.o aaccoder.o aacenctab.o \
aacpsy.o aactab.o \ aacpsy.o aactab.o \
aacenc_is.o \ aacenc_is.o \
aacenc_tns.o \
psymodel.o mpeg4audio.o kbdwin.o psymodel.o mpeg4audio.o kbdwin.o
OBJS-$(CONFIG_AASC_DECODER) += aasc.o msrledec.o OBJS-$(CONFIG_AASC_DECODER) += aasc.o msrledec.o
OBJS-$(CONFIG_AC3_DECODER) += ac3dec_float.o ac3dec_data.o ac3.o kbdwin.o OBJS-$(CONFIG_AC3_DECODER) += ac3dec_float.o ac3dec_data.o ac3.o kbdwin.o

View File

@ -195,6 +195,7 @@ typedef struct TemporalNoiseShaping {
int length[8][4]; int length[8][4];
int direction[8][4]; int direction[8][4];
int order[8][4]; int order[8][4];
int coef_idx[8][4][TNS_MAX_ORDER];
INTFLOAT coef[8][4][TNS_MAX_ORDER]; INTFLOAT coef[8][4][TNS_MAX_ORDER];
} TemporalNoiseShaping; } TemporalNoiseShaping;

View File

@ -45,6 +45,7 @@
#include "aac_tablegen_decl.h" #include "aac_tablegen_decl.h"
#include "aacenc_is.h" #include "aacenc_is.h"
#include "aacenc_tns.h"
/** Frequency in Hz for lower limit of noise substitution **/ /** Frequency in Hz for lower limit of noise substitution **/
#define NOISE_LOW_LIMIT 4500 #define NOISE_LOW_LIMIT 4500
@ -958,8 +959,10 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
search_for_quantizers_faac, search_for_quantizers_faac,
encode_window_bands_info, encode_window_bands_info,
quantize_and_encode_band, quantize_and_encode_band,
encode_tns_info,
set_special_band_scalefactors, set_special_band_scalefactors,
search_for_pns, search_for_pns,
search_for_tns,
search_for_ms, search_for_ms,
search_for_is, search_for_is,
}, },
@ -967,8 +970,10 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
search_for_quantizers_anmr, search_for_quantizers_anmr,
encode_window_bands_info, encode_window_bands_info,
quantize_and_encode_band, quantize_and_encode_band,
encode_tns_info,
set_special_band_scalefactors, set_special_band_scalefactors,
search_for_pns, search_for_pns,
search_for_tns,
search_for_ms, search_for_ms,
search_for_is, search_for_is,
}, },
@ -976,8 +981,10 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
search_for_quantizers_twoloop, search_for_quantizers_twoloop,
codebook_trellis_rate, codebook_trellis_rate,
quantize_and_encode_band, quantize_and_encode_band,
encode_tns_info,
set_special_band_scalefactors, set_special_band_scalefactors,
search_for_pns, search_for_pns,
search_for_tns,
search_for_ms, search_for_ms,
search_for_is, search_for_is,
}, },
@ -985,8 +992,10 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
search_for_quantizers_fast, search_for_quantizers_fast,
encode_window_bands_info, encode_window_bands_info,
quantize_and_encode_band, quantize_and_encode_band,
encode_tns_info,
set_special_band_scalefactors, set_special_band_scalefactors,
search_for_pns, search_for_pns,
search_for_tns,
search_for_ms, search_for_ms,
search_for_is, search_for_is,
}, },

View File

@ -401,7 +401,10 @@ static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
encode_band_info(s, sce); encode_band_info(s, sce);
encode_scale_factors(avctx, s, sce); encode_scale_factors(avctx, s, sce);
encode_pulses(s, &sce->pulse); encode_pulses(s, &sce->pulse);
put_bits(&s->pb, 1, 0); //tns if (s->coder->encode_tns_info)
s->coder->encode_tns_info(s, sce);
else
put_bits(&s->pb, 1, 0);
put_bits(&s->pb, 1, 0); //ssr put_bits(&s->pb, 1, 0); //ssr
encode_spectral_coeffs(s, sce); encode_spectral_coeffs(s, sce);
return 0; return 0;
@ -571,6 +574,7 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
for (ch = 0; ch < chans; ch++) { for (ch = 0; ch < chans; ch++) {
sce = &cpe->ch[ch]; sce = &cpe->ch[ch];
coeffs[ch] = sce->coeffs; coeffs[ch] = sce->coeffs;
memset(&sce->tns, 0, sizeof(TemporalNoiseShaping));
for (w = 0; w < 128; w++) for (w = 0; w < 128; w++)
if (sce->band_type[w] > RESERVED_BT) if (sce->band_type[w] > RESERVED_BT)
sce->band_type[w] = 0; sce->band_type[w] = 0;
@ -593,11 +597,15 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
} }
} }
} }
if (s->options.pns && s->coder->search_for_pns) {
for (ch = 0; ch < chans; ch++) { for (ch = 0; ch < chans; ch++) {
sce = &cpe->ch[ch];
s->cur_channel = start_ch + ch; s->cur_channel = start_ch + ch;
s->coder->search_for_pns(s, avctx, &cpe->ch[ch]); if (s->options.pns && s->coder->search_for_pns)
} s->coder->search_for_pns(s, avctx, sce);
if (s->options.tns && s->coder->search_for_tns)
s->coder->search_for_tns(s, sce);
if (sce->tns.present)
tns_mode = 1;
} }
s->cur_channel = start_ch; s->cur_channel = start_ch;
if (s->options.stereo_mode && cpe->common_window) { if (s->options.stereo_mode && cpe->common_window) {
@ -824,6 +832,9 @@ static const AVOption aacenc_options[] = {
{"aac_is", "Intensity stereo coding", offsetof(AACEncContext, options.intensity_stereo), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AACENC_FLAGS, "intensity_stereo"}, {"aac_is", "Intensity stereo coding", offsetof(AACEncContext, options.intensity_stereo), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AACENC_FLAGS, "intensity_stereo"},
{"disable", "Disable intensity stereo coding", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, AACENC_FLAGS, "intensity_stereo"}, {"disable", "Disable intensity stereo coding", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, AACENC_FLAGS, "intensity_stereo"},
{"enable", "Enable intensity stereo coding", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, AACENC_FLAGS, "intensity_stereo"}, {"enable", "Enable intensity stereo coding", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, AACENC_FLAGS, "intensity_stereo"},
{"aac_tns", "Temporal noise shaping", offsetof(AACEncContext, options.tns), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AACENC_FLAGS, "aac_tns"},
{"disable", "Disable temporal noise shaping", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_tns"},
{"enable", "Enable temporal noise shaping", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_tns"},
{NULL} {NULL}
}; };

View File

@ -45,6 +45,7 @@ typedef struct AACEncOptions {
int stereo_mode; int stereo_mode;
int aac_coder; int aac_coder;
int pns; int pns;
int tns;
int pred; int pred;
int intensity_stereo; int intensity_stereo;
} AACEncOptions; } AACEncOptions;
@ -58,8 +59,10 @@ typedef struct AACCoefficientsEncoder {
int win, int group_len, const float lambda); int win, int group_len, const float lambda);
void (*quantize_and_encode_band)(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, int size, void (*quantize_and_encode_band)(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, int size,
int scale_idx, int cb, const float lambda, int rtz); int scale_idx, int cb, const float lambda, int rtz);
void (*encode_tns_info)(struct AACEncContext *s, SingleChannelElement *sce);
void (*set_special_band_scalefactors)(struct AACEncContext *s, SingleChannelElement *sce); void (*set_special_band_scalefactors)(struct AACEncContext *s, SingleChannelElement *sce);
void (*search_for_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce); void (*search_for_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
void (*search_for_tns)(struct AACEncContext *s, SingleChannelElement *sce);
void (*search_for_ms)(struct AACEncContext *s, ChannelElement *cpe); void (*search_for_ms)(struct AACEncContext *s, ChannelElement *cpe);
void (*search_for_is)(struct AACEncContext *s, AVCodecContext *avctx, ChannelElement *cpe); void (*search_for_is)(struct AACEncContext *s, AVCodecContext *avctx, ChannelElement *cpe);
} AACCoefficientsEncoder; } AACCoefficientsEncoder;

235
libavcodec/aacenc_tns.c Normal file
View File

@ -0,0 +1,235 @@
/*
* AAC encoder TNS
* Copyright (C) 2015 Rostislav Pehlivanov
*
* 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
*/
/**
* @file
* AAC encoder temporal noise shaping
* @author Rostislav Pehlivanov ( atomnuker gmail com )
*/
#include <strings.h>
#include "aacenc.h"
#include "aacenc_tns.h"
#include "aactab.h"
#include "aacenc_utils.h"
#include "aacenc_quantization.h"
static inline void conv_to_int32(int32_t *loc, float *samples, int num, float norm)
{
int i;
for (i = 0; i < num; i++)
loc[i] = ceilf((samples[i]/norm)*INT32_MAX);
}
static inline void conv_to_float(float *arr, int32_t *cof, int num)
{
int i;
for (i = 0; i < num; i++)
arr[i] = (float)cof[i]/INT32_MAX;
}
/* Input: quantized 4 bit coef, output: 1 if first (MSB) 2 bits are the same */
static inline int coef_test_compression(int coef)
{
int res = 0;
coef = coef >> 3;
res += ffs(coef);
coef = coef >> 1;
res += ffs(coef);
return res == 1 ? 0 : 1;
}
static inline int compress_coef(int *coefs, int num)
{
int i, res = 0;
for (i = 0; i < num; i++)
res += coef_test_compression(coefs[i]);
return res != num ? 0 : 1;
}
/**
* Encode TNS data.
* Coefficient compression saves a single bit.
*/
void encode_tns_info(AACEncContext *s, SingleChannelElement *sce)
{
int i, w, filt, coef_len, coef_compress;
const int coef_res = MAX_LPC_PRECISION == 4 ? 1 : 0;
const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
put_bits(&s->pb, 1, !!sce->tns.present);
if (!sce->tns.present)
return;
for (i = 0; i < sce->ics.num_windows; i++) {
put_bits(&s->pb, 2 - is8, sce->tns.n_filt[i]);
if (sce->tns.n_filt[i]) {
put_bits(&s->pb, 1, !!coef_res);
for (filt = 0; filt < sce->tns.n_filt[i]; filt++) {
put_bits(&s->pb, 6 - 2 * is8, sce->tns.length[i][filt]);
put_bits(&s->pb, 5 - 2 * is8, sce->tns.order[i][filt]);
if (sce->tns.order[i][filt]) {
coef_compress = compress_coef(sce->tns.coef_idx[i][filt],
sce->tns.order[i][filt]);
put_bits(&s->pb, 1, !!sce->tns.direction[i][filt]);
put_bits(&s->pb, 1, !!coef_compress);
coef_len = coef_res + 3 - coef_compress;
for (w = 0; w < sce->tns.order[i][filt]; w++)
put_bits(&s->pb, coef_len, sce->tns.coef_idx[i][filt][w]);
}
}
}
}
}
static int process_tns_coeffs(TemporalNoiseShaping *tns, float *tns_coefs_raw,
int order, int w, int filt)
{
int i, j;
int *idx = tns->coef_idx[w][filt];
float *lpc = tns->coef[w][filt];
const int iqfac_p = ((1 << (MAX_LPC_PRECISION-1)) - 0.5)/(M_PI/2.0);
const int iqfac_m = ((1 << (MAX_LPC_PRECISION-1)) + 0.5)/(M_PI/2.0);
float temp[TNS_MAX_ORDER] = {0.0f}, out[TNS_MAX_ORDER] = {0.0f};
/* Quantization */
for (i = 0; i < order; i++) {
idx[i] = ceilf(asin(tns_coefs_raw[i])*((tns_coefs_raw[i] >= 0) ? iqfac_p : iqfac_m));
lpc[i] = 2*sin(idx[i]/((idx[i] >= 0) ? iqfac_p : iqfac_m));
}
/* Trim any coeff less than 0.1f from the end */
for (i = order; i > -1; i--) {
lpc[i] = (fabs(lpc[i]) > 0.1f) ? lpc[i] : 0.0f;
if (lpc[i] != 0.0 ) {
order = i;
break;
}
}
if (!order)
return 0;
/* Step up procedure, convert to LPC coeffs */
out[0] = 1.0f;
for (i = 1; i <= order; i++) {
for (j = 1; j < i; j++) {
temp[j] = out[j] + lpc[i]*out[i-j];
}
for (j = 1; j <= i; j++) {
out[j] = temp[j];
}
out[i] = lpc[i-1];
}
memcpy(lpc, out, TNS_MAX_ORDER*sizeof(float));
return order;
}
static void apply_tns_filter(float *out, float *in, int order, int direction,
float *tns_coefs, int ltp_used, int w, int filt, int start_i, int len)
{
int i, j, inc, start = start_i;
float tmp[TNS_MAX_ORDER+1];
if (direction) {
inc = -1;
start = (start + len) - 1;
} else {
inc = 1;
}
if (!ltp_used) { /* AR filter */
for (i = 0; i < len; i++, start += inc)
out[i] = in[start];
for (j = 1; j <= FFMIN(i, order); j++)
out[i] += tns_coefs[j]*in[start - j*inc];
} else { /* MA filter */
for (i = 0; i < len; i++, start += inc) {
tmp[0] = out[i] = in[start];
for (j = 1; j <= FFMIN(i, order); j++)
out[i] += tmp[j]*tns_coefs[j];
for (j = order; j > 0; j--)
tmp[j] = tmp[j - 1];
}
}
}
void search_for_tns(AACEncContext *s, SingleChannelElement *sce)
{
TemporalNoiseShaping *tns = &sce->tns;
int w, g, order, sfb_start, sfb_len, coef_start, shift[MAX_LPC_ORDER], count = 0;
const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
const int tns_max_order = is8 ? 7 : s->profile == FF_PROFILE_AAC_LOW ? 12 : TNS_MAX_ORDER;
const float freq_mult = mpeg4audio_sample_rates[s->samplerate_index]/(1024.0f/sce->ics.num_windows)/2.0f;
float max_coef = 0.0f;
for (coef_start = 0; coef_start < 1024; coef_start++)
max_coef = FFMAX(max_coef, sce->pcoeffs[coef_start]);
for (w = 0; w < sce->ics.num_windows; w++) {
int filters = 1, start = 0, coef_len = 0;
int32_t conv_coeff[1024] = {0};
int32_t coefs_t[MAX_LPC_ORDER][MAX_LPC_ORDER] = {{0}};
/* Determine start sfb + coef - excludes anything below threshold */
for (g = 0; g < sce->ics.num_swb; g++) {
if (start*freq_mult > TNS_LOW_LIMIT) {
sfb_start = w*16+g;
sfb_len = (w+1)*16 + g - sfb_start;
coef_start = sce->ics.swb_offset[sfb_start];
coef_len = sce->ics.swb_offset[sfb_start + sfb_len] - coef_start;
break;
}
start += sce->ics.swb_sizes[g];
}
if (coef_len <= 0)
continue;
conv_to_int32(conv_coeff, &sce->pcoeffs[coef_start], coef_len, max_coef);
/* LPC */
order = ff_lpc_calc_coefs(&s->lpc, conv_coeff, coef_len,
TNS_MIN_PRED_ORDER, tns_max_order,
32, coefs_t, shift,
FF_LPC_TYPE_LEVINSON, 10,
ORDER_METHOD_EST, MAX_LPC_SHIFT, 0) - 1;
/* Works surprisingly well, remember to tweak MAX_LPC_SHIFT if you want to play around with this */
if (shift[order] > 3) {
int direction = 0;
float tns_coefs_raw[TNS_MAX_ORDER];
tns->n_filt[w] = filters++;
conv_to_float(tns_coefs_raw, coefs_t[order], order);
for (g = 0; g < tns->n_filt[w]; g++) {
process_tns_coeffs(tns, tns_coefs_raw, order, w, g);
apply_tns_filter(&sce->coeffs[coef_start], sce->pcoeffs, order, direction, tns->coef[w][g],
sce->ics.ltp.present, w, g, coef_start, coef_len);
tns->order[w][g] = order;
tns->length[w][g] = sfb_len;
tns->direction[w][g] = direction;
}
count++;
}
}
sce->tns.present = !!count;
}

45
libavcodec/aacenc_tns.h Normal file
View File

@ -0,0 +1,45 @@
/*
* AAC encoder TNS
* Copyright (C) 2015 Rostislav Pehlivanov
*
* 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
*/
/**
* @file
* AAC encoder temporal noise shaping
* @author Rostislav Pehlivanov ( atomnuker gmail com )
*/
#ifndef AVCODEC_AACENC_TNS_H
#define AVCODEC_AACENC_TNS_H
#include "aac.h"
/** Frequency in Hz for lower limit of TNS **/
#define TNS_LOW_LIMIT 2150
/** LPC settings */
#define TNS_MIN_PRED_ORDER 0
#define MAX_LPC_PRECISION 4 /* 4 bits ltp coeff precision */
#define TNS_LPC_PASSES 2
#define MAX_LPC_SHIFT 4
void encode_tns_info(AACEncContext *s, SingleChannelElement *sce);
void search_for_tns(AACEncContext *s, SingleChannelElement *sce);
#endif /* AVCODEC_AACENC_TNS_H */