]> git.sesse.net Git - ffmpeg/blob - libavcodec/aacenc_ltp.c
aacenc: add support for encoding files using Long Term Prediction
[ffmpeg] / libavcodec / aacenc_ltp.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 long term prediction
25  * @author Rostislav Pehlivanov ( atomnuker gmail com )
26  */
27
28 #include "aacenc_ltp.h"
29 #include "aacenc_quantization.h"
30 #include "aacenc_utils.h"
31
32 /**
33  * Encode LTP data.
34  */
35 void ff_aac_encode_ltp_info(AACEncContext *s, SingleChannelElement *sce,
36                             int common_window)
37 {
38     int i;
39     IndividualChannelStream *ics = &sce->ics;
40     if (s->profile != FF_PROFILE_AAC_LTP || !ics->predictor_present)
41         return;
42     if (common_window)
43         put_bits(&s->pb, 1, 0);
44     put_bits(&s->pb, 1, ics->ltp.present);
45     if (!ics->ltp.present)
46         return;
47     put_bits(&s->pb, 11, ics->ltp.lag);
48     put_bits(&s->pb, 3,  ics->ltp.coef_idx);
49     for (i = 0; i < FFMIN(ics->max_sfb, MAX_LTP_LONG_SFB); i++)
50         put_bits(&s->pb, 1, ics->ltp.used[i]);
51 }
52
53 void ff_aac_ltp_insert_new_frame(AACEncContext *s)
54 {
55     int i, ch, tag, chans, cur_channel, start_ch = 0;
56     ChannelElement *cpe;
57     SingleChannelElement *sce;
58     for (i = 0; i < s->chan_map[0]; i++) {
59         cpe = &s->cpe[i];
60         tag      = s->chan_map[i+1];
61         chans    = tag == TYPE_CPE ? 2 : 1;
62         for (ch = 0; ch < chans; ch++) {
63             sce = &cpe->ch[ch];
64             cur_channel = start_ch + ch;
65             /* New sample + overlap */
66             memcpy(&sce->ltp_state[0],    &sce->ltp_state[1024], 1024*sizeof(sce->ltp_state[0]));
67             memcpy(&sce->ltp_state[1024], &s->planar_samples[cur_channel][2048], 1024*sizeof(sce->ltp_state[0]));
68             memcpy(&sce->ltp_state[2048], &sce->ret_buf[0], 1024*sizeof(sce->ltp_state[0]));
69         }
70         start_ch += chans;
71     }
72 }
73
74 /**
75  * Process LTP parameters
76  * @see Patent WO2006070265A1
77  */
78 void ff_aac_update_ltp(AACEncContext *s, SingleChannelElement *sce)
79 {
80     int i, j, lag;
81     float corr, s0, s1, max_corr = 0.0f;
82     float *samples = &s->planar_samples[s->cur_channel][1024];
83     float *pred_signal = &sce->ltp_state[0];
84     int samples_num = 2048;
85
86     if (s->profile != FF_PROFILE_AAC_LTP)
87         return;
88
89     /* Calculate lag */
90     for (i = 0; i < samples_num; i++) {
91         s0 = s1 = 0.0f;
92         for (j = 0; j < samples_num; j++) {
93             if (j + 1024 < i)
94                 continue;
95             s0 += samples[j]*pred_signal[j-i+1024];
96             s1 += pred_signal[j-i+1024]*pred_signal[j-i+1024];
97         }
98         corr = s1 > 0.0f ? s0/sqrt(s1) : 0.0f;
99         if (corr > max_corr) {
100             max_corr = corr;
101             lag = i;
102         }
103     }
104     lag = av_clip(lag, 0, 2048); /* 11 bits => 2^11 = 2048 */
105
106     if (!lag) {
107         sce->ics.ltp.lag = lag;
108         return;
109     }
110
111     s0 = s1 = 0.0f;
112     for (i = 0; i < lag; i++) {
113         s0 += samples[i];
114         s1 += pred_signal[i-lag+1024];
115     }
116
117     sce->ics.ltp.coef_idx = quant_array_idx(s0/s1, ltp_coef, 8);
118     sce->ics.ltp.coef     = ltp_coef[sce->ics.ltp.coef_idx];
119
120     /* Predict the new samples */
121     if (lag < 1024)
122         samples_num = lag + 1024;
123     for (i = 0; i < samples_num; i++)
124         pred_signal[i+1024] = sce->ics.ltp.coef*pred_signal[i-lag+1024];
125     memset(&pred_signal[samples_num], 0, (2048 - samples_num)*sizeof(float));
126
127     sce->ics.ltp.lag = lag;
128 }
129
130 void ff_aac_adjust_common_ltp(AACEncContext *s, ChannelElement *cpe)
131 {
132     int sfb, count = 0;
133     SingleChannelElement *sce0 = &cpe->ch[0];
134     SingleChannelElement *sce1 = &cpe->ch[1];
135
136     if (!cpe->common_window ||
137         sce0->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE ||
138         sce1->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE)
139         return;
140
141     for (sfb = 0; sfb < FFMIN(sce0->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++) {
142         int sum = sce0->ics.ltp.used[sfb] + sce1->ics.ltp.used[sfb];
143         if (sum != 2) {
144             sce0->ics.ltp.used[sfb] = 0;
145         } else if (sum == 2) {
146             count++;
147         }
148     }
149
150     sce0->ics.ltp.present = !!count;
151     sce0->ics.predictor_present = !!count;
152 }
153
154 /**
155  * Mark LTP sfb's
156  */
157 void ff_aac_search_for_ltp(AACEncContext *s, SingleChannelElement *sce,
158                            int common_window)
159 {
160     int w, g, w2, i, start = 0, count = 0;
161     int saved_bits = -(15 + FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB));
162     float *C34 = &s->scoefs[128*0], *PCD = &s->scoefs[128*1];
163     float *PCD34 = &s->scoefs[128*2];
164     const int max_ltp = FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB);
165
166     if (sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE ||
167         !sce->ics.ltp.lag)
168         return;
169
170     for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
171         start = 0;
172         for (g = 0;  g < sce->ics.num_swb; g++) {
173             int bits1 = 0, bits2 = 0;
174             float dist1 = 0.0f, dist2 = 0.0f;
175             if (w*16+g > max_ltp) {
176                 start += sce->ics.swb_sizes[g];
177                 continue;
178             }
179             for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
180                 int bits_tmp1, bits_tmp2;
181                 FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
182                 for (i = 0; i < sce->ics.swb_sizes[g]; i++)
183                     PCD[i] = sce->coeffs[start+(w+w2)*128+i] - sce->lcoeffs[start+(w+w2)*128+i];
184                 abs_pow34_v(C34,  &sce->coeffs[start+(w+w2)*128],  sce->ics.swb_sizes[g]);
185                 abs_pow34_v(PCD34, PCD, sce->ics.swb_sizes[g]);
186                 dist1 += quantize_band_cost(s, &sce->coeffs[start+(w+w2)*128], C34, sce->ics.swb_sizes[g],
187                                             sce->sf_idx[(w+w2)*16+g], sce->band_type[(w+w2)*16+g],
188                                             s->lambda/band->threshold, INFINITY, &bits_tmp1, NULL, 0);
189                 dist2 += quantize_band_cost(s, PCD, PCD34, sce->ics.swb_sizes[g],
190                                             sce->sf_idx[(w+w2)*16+g],
191                                             sce->band_type[(w+w2)*16+g],
192                                             s->lambda/band->threshold, INFINITY, &bits_tmp2, NULL, 0);
193                 bits1 += bits_tmp1;
194                 bits2 += bits_tmp2;
195             }
196             if (dist2 < dist1 && bits2 < bits1) {
197                 for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
198                     for (i = 0; i < sce->ics.swb_sizes[g]; i++)
199                         sce->coeffs[start+(w+w2)*128+i] -= sce->lcoeffs[start+(w+w2)*128+i];
200                 sce->ics.ltp.used[w*16+g] = 1;
201                 saved_bits += bits1 - bits2;
202                 count++;
203             }
204             start += sce->ics.swb_sizes[g];
205         }
206     }
207
208     sce->ics.ltp.present = !!count && (saved_bits >= 0);
209     sce->ics.predictor_present = !!sce->ics.ltp.present;
210
211     /* Reset any marked sfbs */
212     if (!sce->ics.ltp.present && !!count) {
213         for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
214             start = 0;
215             for (g = 0;  g < sce->ics.num_swb; g++) {
216                 if (sce->ics.ltp.used[w*16+g]) {
217                     for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
218                         for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
219                             sce->coeffs[start+(w+w2)*128+i] += sce->lcoeffs[start+(w+w2)*128+i];
220                         }
221                     }
222                 }
223                 start += sce->ics.swb_sizes[g];
224             }
225         }
226     }
227 }