]> git.sesse.net Git - ffmpeg/blob - libavcodec/aacenc_tns.c
Merge commit '167ea1fbf15ecefa30729f9b8d091ed431bf43bd'
[ffmpeg] / libavcodec / aacenc_tns.c
1 /*
2  * AAC encoder TNS
3  * Copyright (C) 2015 Rostislav Pehlivanov
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 temporal noise shaping
25  * @author Rostislav Pehlivanov ( atomnuker gmail com )
26  */
27
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 tmp = coef >> 2;
52     int res = ff_ctz(tmp);
53     if (res > 1)
54         return 1;       /* ...00 ->  compressable    */
55     else if (res == 1)
56         return 0;       /* ...10 ->  uncompressable  */
57     else if (ff_ctz(tmp >> 1) > 0)
58         return 0;       /* ...0 1 -> uncompressable  */
59     else
60         return 1;       /* ...1 1 -> compressable    */
61 }
62
63 static inline int compress_coef(int *coefs, int num)
64 {
65     int i, res = 0;
66     for (i = 0; i < num; i++)
67         res += coef_test_compression(coefs[i]);
68     return res == num ? 1 : 0;
69 }
70
71 /**
72  * Encode TNS data.
73  * Coefficient compression saves a single bit.
74  */
75 void ff_aac_encode_tns_info(AACEncContext *s, SingleChannelElement *sce)
76 {
77     int i, w, filt, coef_len, coef_compress;
78     const int coef_res = MAX_LPC_PRECISION == 4 ? 1 : 0;
79     const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
80
81     put_bits(&s->pb, 1, !!sce->tns.present);
82
83     if (!sce->tns.present)
84         return;
85
86     for (i = 0; i < sce->ics.num_windows; i++) {
87         put_bits(&s->pb, 2 - is8, sce->tns.n_filt[i]);
88         if (sce->tns.n_filt[i]) {
89             put_bits(&s->pb, 1, !!coef_res);
90             for (filt = 0; filt < sce->tns.n_filt[i]; filt++) {
91                 put_bits(&s->pb, 6 - 2 * is8, sce->tns.length[i][filt]);
92                 put_bits(&s->pb, 5 - 2 * is8, sce->tns.order[i][filt]);
93                 if (sce->tns.order[i][filt]) {
94                     coef_compress = compress_coef(sce->tns.coef_idx[i][filt],
95                                                   sce->tns.order[i][filt]);
96                     put_bits(&s->pb, 1, !!sce->tns.direction[i][filt]);
97                     put_bits(&s->pb, 1, !!coef_compress);
98                     coef_len = coef_res + 3 - coef_compress;
99                     for (w = 0; w < sce->tns.order[i][filt]; w++)
100                         put_bits(&s->pb, coef_len, sce->tns.coef_idx[i][filt][w]);
101                 }
102             }
103         }
104     }
105 }
106
107 static int process_tns_coeffs(TemporalNoiseShaping *tns, float *tns_coefs_raw,
108                               int order, int w, int filt)
109 {
110     int i, j;
111     int *idx = tns->coef_idx[w][filt];
112     float *lpc = tns->coef[w][filt];
113     const int iqfac_p = ((1 << (MAX_LPC_PRECISION-1)) - 0.5)/(M_PI/2.0);
114     const int iqfac_m = ((1 << (MAX_LPC_PRECISION-1)) + 0.5)/(M_PI/2.0);
115     float temp[TNS_MAX_ORDER] = {0.0f}, out[TNS_MAX_ORDER] = {0.0f};
116
117     /* Quantization */
118     for (i = 0; i < order; i++) {
119         idx[i] = ceilf(asin(tns_coefs_raw[i])*((tns_coefs_raw[i] >= 0) ? iqfac_p : iqfac_m));
120         lpc[i] = 2*sin(idx[i]/((idx[i] >= 0) ? iqfac_p : iqfac_m));
121     }
122
123     /* Trim any coeff less than 0.1f from the end */
124     for (i = order; i > -1; i--) {
125         lpc[i] = (fabs(lpc[i]) > 0.1f) ? lpc[i] : 0.0f;
126         if (lpc[i] != 0.0 ) {
127             order = i;
128             break;
129         }
130     }
131
132     if (!order)
133         return 0;
134
135     /* Step up procedure, convert to LPC coeffs */
136     out[0] = 1.0f;
137     for (i = 1; i <= order; i++) {
138         for (j = 1; j < i; j++) {
139             temp[j] = out[j] + lpc[i]*out[i-j];
140         }
141         for (j = 1; j <= i; j++) {
142             out[j] = temp[j];
143         }
144         out[i] = lpc[i-1];
145     }
146     memcpy(lpc, out, TNS_MAX_ORDER*sizeof(float));
147
148     return order;
149 }
150
151 static void apply_tns_filter(float *out, float *in, int order, int direction,
152                              float *tns_coefs, int ltp_used, int w, int filt,
153                              int start_i, int len)
154 {
155     int i, j, inc, start = start_i;
156     float tmp[TNS_MAX_ORDER+1];
157     if (direction) {
158         inc = -1;
159         start = (start + len) - 1;
160     } else {
161         inc = 1;
162     }
163     if (!ltp_used) {    /* AR filter */
164         for (i = 0; i < len; i++, start += inc)
165             out[i] = in[start];
166             for (j = 1; j <= FFMIN(i, order); j++)
167                 out[i] += tns_coefs[j]*in[start - j*inc];
168     } else {            /* MA filter */
169         for (i = 0; i < len; i++, start += inc) {
170             tmp[0] = out[i] = in[start];
171             for (j = 1; j <= FFMIN(i, order); j++)
172                 out[i] += tmp[j]*tns_coefs[j];
173             for (j = order; j > 0; j--)
174                 tmp[j] = tmp[j - 1];
175         }
176     }
177 }
178
179 void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
180 {
181     TemporalNoiseShaping *tns = &sce->tns;
182     int w, g, order, sfb_start, sfb_len, coef_start, shift[MAX_LPC_ORDER], count = 0;
183     const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
184     const int tns_max_order = is8 ? 7 : s->profile == FF_PROFILE_AAC_LOW ? 12 : TNS_MAX_ORDER;
185     const float freq_mult = mpeg4audio_sample_rates[s->samplerate_index]/(1024.0f/sce->ics.num_windows)/2.0f;
186     float max_coef = 0.0f;
187
188     sce->tns.present = 0;
189     return;
190
191     for (coef_start = 0; coef_start < 1024; coef_start++)
192         max_coef = FFMAX(max_coef, sce->pcoeffs[coef_start]);
193
194     for (w = 0; w < sce->ics.num_windows; w++) {
195         int filters = 1, start = 0, coef_len = 0;
196         int32_t conv_coeff[1024] = {0};
197         int32_t coefs_t[MAX_LPC_ORDER][MAX_LPC_ORDER] = {{0}};
198
199         /* Determine start sfb + coef - excludes anything below threshold */
200         for (g = 0;  g < sce->ics.num_swb; g++) {
201             if (start*freq_mult > TNS_LOW_LIMIT) {
202                 sfb_start = w*16+g;
203                 sfb_len   = (w+1)*16 + g - sfb_start;
204                 coef_start = sce->ics.swb_offset[sfb_start];
205                 coef_len  = sce->ics.swb_offset[sfb_start + sfb_len] - coef_start;
206                 break;
207             }
208             start += sce->ics.swb_sizes[g];
209         }
210
211         if (coef_len <= 0)
212             continue;
213
214         conv_to_int32(conv_coeff, &sce->pcoeffs[coef_start], coef_len, max_coef);
215
216         /* LPC */
217         order = ff_lpc_calc_coefs(&s->lpc, conv_coeff, coef_len,
218                                   TNS_MIN_PRED_ORDER, tns_max_order,
219                                   32, coefs_t, shift,
220                                   FF_LPC_TYPE_LEVINSON, 10,
221                                   ORDER_METHOD_EST, MAX_LPC_SHIFT, 0) - 1;
222
223         /* Works surprisingly well, remember to tweak MAX_LPC_SHIFT if you want to play around with this */
224         if (shift[order] > 3) {
225             int direction = 0;
226             float tns_coefs_raw[TNS_MAX_ORDER];
227             tns->n_filt[w] = filters++;
228             conv_to_float(tns_coefs_raw, coefs_t[order], order);
229             for (g = 0; g < tns->n_filt[w]; g++) {
230                 process_tns_coeffs(tns, tns_coefs_raw, order, w, g);
231                 apply_tns_filter(&sce->coeffs[coef_start], sce->pcoeffs, order, direction, tns->coef[w][g],
232                                  sce->ics.ltp.present, w, g, coef_start, coef_len);
233                 tns->order[w][g]     = order;
234                 tns->length[w][g]    = sfb_len;
235                 tns->direction[w][g] = direction;
236             }
237             count++;
238         }
239     }
240
241     sce->tns.present = !!count;
242 }