aacenc_tns: rework the way coefficients are calculated
[ffmpeg.git] / 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 int compress_coef(int *coefs, int num)
35 {
36     int i, c = 0;
37     for (i = 0; i < num; i++)
38         c += coefs[i] < 4 || coefs[i] > 11;
39     return c == num;
40 }
41
42 /**
43  * Encode TNS data.
44  * Coefficient compression saves a single bit per coefficient.
45  */
46 void ff_aac_encode_tns_info(AACEncContext *s, SingleChannelElement *sce)
47 {
48     int i, w, filt, coef_len, coef_compress;
49     const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
50
51     if (!sce->tns.present)
52         return;
53
54     for (i = 0; i < sce->ics.num_windows; i++) {
55         put_bits(&s->pb, 2 - is8, sce->tns.n_filt[i]);
56         if (sce->tns.n_filt[i]) {
57             put_bits(&s->pb, 1, 1);
58             for (filt = 0; filt < sce->tns.n_filt[i]; filt++) {
59                 put_bits(&s->pb, 6 - 2 * is8, sce->tns.length[i][filt]);
60                 put_bits(&s->pb, 5 - 2 * is8, sce->tns.order[i][filt]);
61                 if (sce->tns.order[i][filt]) {
62                     coef_compress = compress_coef(sce->tns.coef_idx[i][filt],
63                                                   sce->tns.order[i][filt]);
64                     put_bits(&s->pb, 1, !!sce->tns.direction[i][filt]);
65                     put_bits(&s->pb, 1, !!coef_compress);
66                     coef_len = 4 - coef_compress;
67                     for (w = 0; w < sce->tns.order[i][filt]; w++)
68                         put_bits(&s->pb, coef_len, sce->tns.coef_idx[i][filt][w]);
69                 }
70             }
71         }
72     }
73 }
74
75 static void process_tns_coeffs(TemporalNoiseShaping *tns, double *coef_raw,
76                                int *order_p, int w, int filt)
77 {
78     int i, j, order = *order_p;
79     int *idx = tns->coef_idx[w][filt];
80     float *lpc = tns->coef[w][filt];
81     float temp[TNS_MAX_ORDER] = {0.0f}, out[TNS_MAX_ORDER] = {0.0f};
82
83     if (!order)
84         return;
85
86     /* Not what the specs say, but it's better */
87     for (i = 0; i < order; i++) {
88         idx[i] = quant_array_idx(coef_raw[i], tns_tmp2_map_0_4, 16);
89         lpc[i] = tns_tmp2_map_0_4[idx[i]];
90     }
91
92     /* Trim any coeff less than 0.1f from the end */
93     for (i = order-1; i > -1; i--) {
94         lpc[i] = (fabs(lpc[i]) > 0.1f) ? lpc[i] : 0.0f;
95         if (lpc[i] != 0.0 ) {
96             order = i;
97             break;
98         }
99     }
100
101     /* Step up procedure, convert to LPC coeffs */
102     out[0] = 1.0f;
103     for (i = 1; i <= order; i++) {
104         for (j = 1; j < i; j++) {
105             temp[j] = out[j] + lpc[i]*out[i-j];
106         }
107         for (j = 1; j <= i; j++) {
108             out[j] = temp[j];
109         }
110         out[i] = lpc[i-1];
111     }
112     *order_p = order;
113     memcpy(lpc, out, TNS_MAX_ORDER*sizeof(float));
114 }
115
116 /* Apply TNS filter */
117 void ff_aac_apply_tns(SingleChannelElement *sce)
118 {
119     const int mmm = FFMIN(sce->ics.tns_max_bands, sce->ics.max_sfb);
120     float *coef = sce->pcoeffs;
121     TemporalNoiseShaping *tns = &sce->tns;
122     int w, filt, m, i;
123     int bottom, top, order, start, end, size, inc;
124     float *lpc, tmp[TNS_MAX_ORDER+1];
125
126     return;
127
128     for (w = 0; w < sce->ics.num_windows; w++) {
129         bottom = sce->ics.num_swb;
130         for (filt = 0; filt < tns->n_filt[w]; filt++) {
131             top    = bottom;
132             bottom = FFMAX(0, top - tns->length[w][filt]);
133             order  = tns->order[w][filt];
134             lpc    = tns->coef[w][filt];
135             if (!order)
136                 continue;
137
138             start = sce->ics.swb_offset[FFMIN(bottom, mmm)];
139             end   = sce->ics.swb_offset[FFMIN(   top, mmm)];
140             if ((size = end - start) <= 0)
141                 continue;
142             if (tns->direction[w][filt]) {
143                 inc = -1;
144                 start = end - 1;
145             } else {
146                 inc = 1;
147             }
148             start += w * 128;
149
150             if (!sce->ics.ltp.present) {
151                 // ar filter
152                 for (m = 0; m < size; m++, start += inc)
153                     for (i = 1; i <= FFMIN(m, order); i++)
154                         coef[start] += coef[start - i * inc]*lpc[i - 1];
155             } else {
156                 // ma filter
157                 for (m = 0; m < size; m++, start += inc) {
158                     tmp[0] = coef[start];
159                     for (i = 1; i <= FFMIN(m, order); i++)
160                         coef[start] += tmp[i]*lpc[i - 1];
161                     for (i = order; i > 0; i--)
162                         tmp[i] = tmp[i - 1];
163                 }
164             }
165         }
166     }
167 }
168
169 void ff_aac_search_for_tns(AACEncContext *s, SingleChannelElement *sce)
170 {
171     TemporalNoiseShaping *tns = &sce->tns;
172     int w, g, w2, prev_end_sfb = 0, count = 0;
173     const int is8 = sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
174     const int tns_max_order = is8 ? 7 : s->profile == FF_PROFILE_AAC_LOW ? 12 : TNS_MAX_ORDER;
175
176     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
177         int order = 0, filters = 1;
178         int sfb_start = 0, sfb_len = 0;
179         int coef_start = 0, coef_len = 0;
180         float energy = 0.0f, threshold = 0.0f;
181         double coefs[MAX_LPC_ORDER][MAX_LPC_ORDER] = {{0}};
182         for (g = 0;  g < sce->ics.num_swb; g++) {
183             if (!sfb_start && w*16+g > TNS_LOW_LIMIT && w*16+g > prev_end_sfb) {
184                 sfb_start = w*16+g;
185                 coef_start =  sce->ics.swb_offset[sfb_start];
186             }
187             if (sfb_start) {
188                 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
189                     FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
190                     if (!sfb_len && band->energy < band->threshold*1.3f) {
191                         sfb_len = (w+w2)*16+g - sfb_start;
192                         prev_end_sfb = sfb_start + sfb_len;
193                         coef_len = sce->ics.swb_offset[sfb_start + sfb_len] - coef_start;
194                         break;
195                     }
196                     energy += band->energy;
197                     threshold += band->threshold;
198                 }
199                 if (!sfb_len) {
200                     sfb_len = (w+sce->ics.group_len[w])*16+g - sfb_start;
201                     coef_len = sce->ics.swb_offset[sfb_start + sfb_len] - coef_start;
202                 }
203             }
204         }
205
206         if (sfb_len <= 0 || coef_len <= 0)
207             continue;
208         if (coef_start + coef_len > 1024)
209             coef_len = 1024 - coef_start;
210
211         /* LPC */
212         order = ff_lpc_calc_levinsion(&s->lpc, &sce->coeffs[coef_start], coef_len,
213                                       coefs, 0, tns_max_order, ORDER_METHOD_LOG);
214
215         if (energy > threshold) {
216             int direction = 0;
217             tns->n_filt[w] = filters++;
218             for (g = 0; g < tns->n_filt[w]; g++) {
219                 process_tns_coeffs(tns, coefs[order], &order, w, g);
220                 tns->order[w][g]     = order;
221                 tns->length[w][g]    = sfb_len;
222                 tns->direction[w][g] = direction;
223             }
224             count++;
225         }
226     }
227
228     sce->tns.present = !!count;
229 }