Browse code

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>

Rostislav Pehlivanov authored on 2015/08/22 03:27:38
Showing 7 changed files
... ...
@@ -133,6 +133,7 @@ OBJS-$(CONFIG_AAC_FIXED_DECODER)       += aacdec_fixed.o aactab.o aacsbr_fixed.o
133 133
 OBJS-$(CONFIG_AAC_ENCODER)             += aacenc.o aaccoder.o aacenctab.o    \
134 134
                                           aacpsy.o aactab.o      \
135 135
                                           aacenc_is.o \
136
+                                          aacenc_tns.o \
136 137
                                           psymodel.o mpeg4audio.o kbdwin.o
137 138
 OBJS-$(CONFIG_AASC_DECODER)            += aasc.o msrledec.o
138 139
 OBJS-$(CONFIG_AC3_DECODER)             += ac3dec_float.o ac3dec_data.o ac3.o kbdwin.o
... ...
@@ -195,6 +195,7 @@ typedef struct TemporalNoiseShaping {
195 195
     int length[8][4];
196 196
     int direction[8][4];
197 197
     int order[8][4];
198
+    int coef_idx[8][4][TNS_MAX_ORDER];
198 199
     INTFLOAT coef[8][4][TNS_MAX_ORDER];
199 200
 } TemporalNoiseShaping;
200 201
 
... ...
@@ -45,6 +45,7 @@
45 45
 #include "aac_tablegen_decl.h"
46 46
 
47 47
 #include "aacenc_is.h"
48
+#include "aacenc_tns.h"
48 49
 
49 50
 /** Frequency in Hz for lower limit of noise substitution **/
50 51
 #define NOISE_LOW_LIMIT 4500
... ...
@@ -958,8 +959,10 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
958 958
         search_for_quantizers_faac,
959 959
         encode_window_bands_info,
960 960
         quantize_and_encode_band,
961
+        encode_tns_info,
961 962
         set_special_band_scalefactors,
962 963
         search_for_pns,
964
+        search_for_tns,
963 965
         search_for_ms,
964 966
         search_for_is,
965 967
     },
... ...
@@ -967,8 +970,10 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
967 967
         search_for_quantizers_anmr,
968 968
         encode_window_bands_info,
969 969
         quantize_and_encode_band,
970
+        encode_tns_info,
970 971
         set_special_band_scalefactors,
971 972
         search_for_pns,
973
+        search_for_tns,
972 974
         search_for_ms,
973 975
         search_for_is,
974 976
     },
... ...
@@ -976,8 +981,10 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
976 976
         search_for_quantizers_twoloop,
977 977
         codebook_trellis_rate,
978 978
         quantize_and_encode_band,
979
+        encode_tns_info,
979 980
         set_special_band_scalefactors,
980 981
         search_for_pns,
982
+        search_for_tns,
981 983
         search_for_ms,
982 984
         search_for_is,
983 985
     },
... ...
@@ -985,8 +992,10 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
985 985
         search_for_quantizers_fast,
986 986
         encode_window_bands_info,
987 987
         quantize_and_encode_band,
988
+        encode_tns_info,
988 989
         set_special_band_scalefactors,
989 990
         search_for_pns,
991
+        search_for_tns,
990 992
         search_for_ms,
991 993
         search_for_is,
992 994
     },
... ...
@@ -401,7 +401,10 @@ static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
401 401
     encode_band_info(s, sce);
402 402
     encode_scale_factors(avctx, s, sce);
403 403
     encode_pulses(s, &sce->pulse);
404
-    put_bits(&s->pb, 1, 0); //tns
404
+    if (s->coder->encode_tns_info)
405
+        s->coder->encode_tns_info(s, sce);
406
+    else
407
+        put_bits(&s->pb, 1, 0);
405 408
     put_bits(&s->pb, 1, 0); //ssr
406 409
     encode_spectral_coeffs(s, sce);
407 410
     return 0;
... ...
@@ -571,6 +574,7 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
571 571
             for (ch = 0; ch < chans; ch++) {
572 572
                 sce = &cpe->ch[ch];
573 573
                 coeffs[ch] = sce->coeffs;
574
+                memset(&sce->tns, 0, sizeof(TemporalNoiseShaping));
574 575
                 for (w = 0; w < 128; w++)
575 576
                     if (sce->band_type[w] > RESERVED_BT)
576 577
                         sce->band_type[w] = 0;
... ...
@@ -593,11 +597,15 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
593 593
                     }
594 594
                 }
595 595
             }
596
-            if (s->options.pns && s->coder->search_for_pns) {
597
-                for (ch = 0; ch < chans; ch++) {
598
-                    s->cur_channel = start_ch + ch;
599
-                    s->coder->search_for_pns(s, avctx, &cpe->ch[ch]);
600
-                }
596
+            for (ch = 0; ch < chans; ch++) {
597
+                sce = &cpe->ch[ch];
598
+                s->cur_channel = start_ch + ch;
599
+                if (s->options.pns && s->coder->search_for_pns)
600
+                    s->coder->search_for_pns(s, avctx, sce);
601
+                if (s->options.tns && s->coder->search_for_tns)
602
+                    s->coder->search_for_tns(s, sce);
603
+                if (sce->tns.present)
604
+                    tns_mode = 1;
601 605
             }
602 606
             s->cur_channel = start_ch;
603 607
             if (s->options.stereo_mode && cpe->common_window) {
... ...
@@ -824,6 +832,9 @@ static const AVOption aacenc_options[] = {
824 824
     {"aac_is", "Intensity stereo coding", offsetof(AACEncContext, options.intensity_stereo), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AACENC_FLAGS, "intensity_stereo"},
825 825
         {"disable",  "Disable intensity stereo coding", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, AACENC_FLAGS, "intensity_stereo"},
826 826
         {"enable",   "Enable intensity stereo coding", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, AACENC_FLAGS, "intensity_stereo"},
827
+    {"aac_tns", "Temporal noise shaping", offsetof(AACEncContext, options.tns), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AACENC_FLAGS, "aac_tns"},
828
+        {"disable",  "Disable temporal noise shaping", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_tns"},
829
+        {"enable",   "Enable temporal noise shaping", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, AACENC_FLAGS, "aac_tns"},
827 830
     {NULL}
828 831
 };
829 832
 
... ...
@@ -45,6 +45,7 @@ typedef struct AACEncOptions {
45 45
     int stereo_mode;
46 46
     int aac_coder;
47 47
     int pns;
48
+    int tns;
48 49
     int pred;
49 50
     int intensity_stereo;
50 51
 } AACEncOptions;
... ...
@@ -58,8 +59,10 @@ typedef struct AACCoefficientsEncoder {
58 58
                                      int win, int group_len, const float lambda);
59 59
     void (*quantize_and_encode_band)(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, int size,
60 60
                                      int scale_idx, int cb, const float lambda, int rtz);
61
+    void (*encode_tns_info)(struct AACEncContext *s, SingleChannelElement *sce);
61 62
     void (*set_special_band_scalefactors)(struct AACEncContext *s, SingleChannelElement *sce);
62 63
     void (*search_for_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
64
+    void (*search_for_tns)(struct AACEncContext *s, SingleChannelElement *sce);
63 65
     void (*search_for_ms)(struct AACEncContext *s, ChannelElement *cpe);
64 66
     void (*search_for_is)(struct AACEncContext *s, AVCodecContext *avctx, ChannelElement *cpe);
65 67
 } AACCoefficientsEncoder;
66 68
new file mode 100644
... ...
@@ -0,0 +1,235 @@
0
+/*
1
+ * AAC encoder TNS
2
+ * Copyright (C) 2015 Rostislav Pehlivanov
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * AAC encoder temporal noise shaping
24
+ * @author Rostislav Pehlivanov ( atomnuker gmail com )
25
+ */
26
+
27
+#include <strings.h>
28
+#include "aacenc.h"
29
+#include "aacenc_tns.h"
30
+#include "aactab.h"
31
+#include "aacenc_utils.h"
32
+#include "aacenc_quantization.h"
33
+
34
+static inline void conv_to_int32(int32_t *loc, float *samples, int num, float norm)
35
+{
36
+    int i;
37
+    for (i = 0; i < num; i++)
38
+        loc[i] = ceilf((samples[i]/norm)*INT32_MAX);
39
+}
40
+
41
+static inline void conv_to_float(float *arr, int32_t *cof, int num)
42
+{
43
+    int i;
44
+    for (i = 0; i < num; i++)
45
+        arr[i] = (float)cof[i]/INT32_MAX;
46
+}
47
+
48
+/* Input: quantized 4 bit coef, output: 1 if first (MSB) 2 bits are the same */
49
+static inline int coef_test_compression(int coef)
50
+{
51
+    int res = 0;
52
+    coef = coef >> 3;
53
+    res += ffs(coef);
54
+    coef = coef >> 1;
55
+    res += ffs(coef);
56
+    return res == 1 ? 0 : 1;
57
+}
58
+
59
+static inline int compress_coef(int *coefs, int num)
60
+{
61
+    int i, res = 0;
62
+    for (i = 0; i < num; i++)
63
+        res += coef_test_compression(coefs[i]);
64
+    return res != num ? 0 : 1;
65
+}
66
+
67
+/**
68
+ * Encode TNS data.
69
+ * Coefficient compression saves a single bit.
70
+ */
71
+void encode_tns_info(AACEncContext *s, SingleChannelElement *sce)
72
+{
73
+    int i, w, filt, coef_len, coef_compress;
74
+    const int coef_res = MAX_LPC_PRECISION == 4 ? 1 : 0;
75
+    const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
76
+
77
+    put_bits(&s->pb, 1, !!sce->tns.present);
78
+
79
+    if (!sce->tns.present)
80
+        return;
81
+
82
+    for (i = 0; i < sce->ics.num_windows; i++) {
83
+        put_bits(&s->pb, 2 - is8, sce->tns.n_filt[i]);
84
+        if (sce->tns.n_filt[i]) {
85
+            put_bits(&s->pb, 1, !!coef_res);
86
+            for (filt = 0; filt < sce->tns.n_filt[i]; filt++) {
87
+                put_bits(&s->pb, 6 - 2 * is8, sce->tns.length[i][filt]);
88
+                put_bits(&s->pb, 5 - 2 * is8, sce->tns.order[i][filt]);
89
+                if (sce->tns.order[i][filt]) {
90
+                    coef_compress = compress_coef(sce->tns.coef_idx[i][filt],
91
+                                                  sce->tns.order[i][filt]);
92
+                    put_bits(&s->pb, 1, !!sce->tns.direction[i][filt]);
93
+                    put_bits(&s->pb, 1, !!coef_compress);
94
+                    coef_len = coef_res + 3 - coef_compress;
95
+                    for (w = 0; w < sce->tns.order[i][filt]; w++)
96
+                        put_bits(&s->pb, coef_len, sce->tns.coef_idx[i][filt][w]);
97
+                }
98
+            }
99
+        }
100
+    }
101
+}
102
+
103
+static int process_tns_coeffs(TemporalNoiseShaping *tns, float *tns_coefs_raw,
104
+                              int order, int w, int filt)
105
+{
106
+    int i, j;
107
+    int *idx = tns->coef_idx[w][filt];
108
+    float *lpc = tns->coef[w][filt];
109
+    const int iqfac_p = ((1 << (MAX_LPC_PRECISION-1)) - 0.5)/(M_PI/2.0);
110
+    const int iqfac_m = ((1 << (MAX_LPC_PRECISION-1)) + 0.5)/(M_PI/2.0);
111
+    float temp[TNS_MAX_ORDER] = {0.0f}, out[TNS_MAX_ORDER] = {0.0f};
112
+
113
+    /* Quantization */
114
+    for (i = 0; i < order; i++) {
115
+        idx[i] = ceilf(asin(tns_coefs_raw[i])*((tns_coefs_raw[i] >= 0) ? iqfac_p : iqfac_m));
116
+        lpc[i] = 2*sin(idx[i]/((idx[i] >= 0) ? iqfac_p : iqfac_m));
117
+    }
118
+
119
+    /* Trim any coeff less than 0.1f from the end */
120
+    for (i = order; i > -1; i--) {
121
+        lpc[i] = (fabs(lpc[i]) > 0.1f) ? lpc[i] : 0.0f;
122
+        if (lpc[i] != 0.0 ) {
123
+            order = i;
124
+            break;
125
+        }
126
+    }
127
+
128
+    if (!order)
129
+        return 0;
130
+
131
+    /* Step up procedure, convert to LPC coeffs */
132
+    out[0] = 1.0f;
133
+    for (i = 1; i <= order; i++) {
134
+        for (j = 1; j < i; j++) {
135
+            temp[j] = out[j] + lpc[i]*out[i-j];
136
+        }
137
+        for (j = 1; j <= i; j++) {
138
+            out[j] = temp[j];
139
+        }
140
+        out[i] = lpc[i-1];
141
+    }
142
+    memcpy(lpc, out, TNS_MAX_ORDER*sizeof(float));
143
+
144
+    return order;
145
+}
146
+
147
+static void apply_tns_filter(float *out, float *in, int order, int direction,
148
+                             float *tns_coefs, int ltp_used, int w, int filt, int start_i, int len)
149
+{
150
+    int i, j, inc, start = start_i;
151
+    float tmp[TNS_MAX_ORDER+1];
152
+    if (direction) {
153
+        inc = -1;
154
+        start = (start + len) - 1;
155
+    } else {
156
+        inc = 1;
157
+    }
158
+    if (!ltp_used) {    /* AR filter */
159
+        for (i = 0; i < len; i++, start += inc)
160
+            out[i] = in[start];
161
+            for (j = 1; j <= FFMIN(i, order); j++)
162
+                out[i] += tns_coefs[j]*in[start - j*inc];
163
+    } else {            /* MA filter */
164
+        for (i = 0; i < len; i++, start += inc) {
165
+            tmp[0] = out[i] = in[start];
166
+            for (j = 1; j <= FFMIN(i, order); j++)
167
+                out[i] += tmp[j]*tns_coefs[j];
168
+            for (j = order; j > 0; j--)
169
+                tmp[j] = tmp[j - 1];
170
+        }
171
+    }
172
+}
173
+
174
+void search_for_tns(AACEncContext *s, SingleChannelElement *sce)
175
+{
176
+    TemporalNoiseShaping *tns = &sce->tns;
177
+    int w, g, order, sfb_start, sfb_len, coef_start, shift[MAX_LPC_ORDER], count = 0;
178
+    const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
179
+    const int tns_max_order = is8 ? 7 : s->profile == FF_PROFILE_AAC_LOW ? 12 : TNS_MAX_ORDER;
180
+    const float freq_mult = mpeg4audio_sample_rates[s->samplerate_index]/(1024.0f/sce->ics.num_windows)/2.0f;
181
+    float max_coef = 0.0f;
182
+
183
+    for (coef_start = 0; coef_start < 1024; coef_start++)
184
+        max_coef = FFMAX(max_coef, sce->pcoeffs[coef_start]);
185
+
186
+    for (w = 0; w < sce->ics.num_windows; w++) {
187
+        int filters = 1, start = 0, coef_len = 0;
188
+        int32_t conv_coeff[1024] = {0};
189
+        int32_t coefs_t[MAX_LPC_ORDER][MAX_LPC_ORDER] = {{0}};
190
+
191
+        /* Determine start sfb + coef - excludes anything below threshold */
192
+        for (g = 0;  g < sce->ics.num_swb; g++) {
193
+            if (start*freq_mult > TNS_LOW_LIMIT) {
194
+                sfb_start = w*16+g;
195
+                sfb_len   = (w+1)*16 + g - sfb_start;
196
+                coef_start = sce->ics.swb_offset[sfb_start];
197
+                coef_len  = sce->ics.swb_offset[sfb_start + sfb_len] - coef_start;
198
+                break;
199
+            }
200
+            start += sce->ics.swb_sizes[g];
201
+        }
202
+
203
+        if (coef_len <= 0)
204
+            continue;
205
+
206
+        conv_to_int32(conv_coeff, &sce->pcoeffs[coef_start], coef_len, max_coef);
207
+
208
+        /* LPC */
209
+        order = ff_lpc_calc_coefs(&s->lpc, conv_coeff, coef_len,
210
+                                  TNS_MIN_PRED_ORDER, tns_max_order,
211
+                                  32, coefs_t, shift,
212
+                                  FF_LPC_TYPE_LEVINSON, 10,
213
+                                  ORDER_METHOD_EST, MAX_LPC_SHIFT, 0) - 1;
214
+
215
+        /* Works surprisingly well, remember to tweak MAX_LPC_SHIFT if you want to play around with this */
216
+        if (shift[order] > 3) {
217
+            int direction = 0;
218
+            float tns_coefs_raw[TNS_MAX_ORDER];
219
+            tns->n_filt[w] = filters++;
220
+            conv_to_float(tns_coefs_raw, coefs_t[order], order);
221
+            for (g = 0; g < tns->n_filt[w]; g++) {
222
+                process_tns_coeffs(tns, tns_coefs_raw, order, w, g);
223
+                apply_tns_filter(&sce->coeffs[coef_start], sce->pcoeffs, order, direction, tns->coef[w][g],
224
+                                 sce->ics.ltp.present, w, g, coef_start, coef_len);
225
+                tns->order[w][g]     = order;
226
+                tns->length[w][g]    = sfb_len;
227
+                tns->direction[w][g] = direction;
228
+            }
229
+            count++;
230
+        }
231
+    }
232
+
233
+    sce->tns.present = !!count;
234
+}
0 235
new file mode 100644
... ...
@@ -0,0 +1,45 @@
0
+/*
1
+ * AAC encoder TNS
2
+ * Copyright (C) 2015 Rostislav Pehlivanov
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * AAC encoder temporal noise shaping
24
+ * @author Rostislav Pehlivanov ( atomnuker gmail com )
25
+ */
26
+
27
+#ifndef AVCODEC_AACENC_TNS_H
28
+#define AVCODEC_AACENC_TNS_H
29
+
30
+#include "aac.h"
31
+
32
+/** Frequency in Hz for lower limit of TNS **/
33
+#define TNS_LOW_LIMIT 2150
34
+
35
+/** LPC settings */
36
+#define TNS_MIN_PRED_ORDER 0
37
+#define MAX_LPC_PRECISION  4   /* 4 bits ltp coeff precision */
38
+#define TNS_LPC_PASSES     2
39
+#define MAX_LPC_SHIFT      4
40
+
41
+void encode_tns_info(AACEncContext *s, SingleChannelElement *sce);
42
+void search_for_tns(AACEncContext *s, SingleChannelElement *sce);
43
+
44
+#endif /* AVCODEC_AACENC_TNS_H */