Browse code

aacenc: add support for encoding files using Long Term Prediction

Long Term Prediction allows for prediction of spectral coefficients
via the previously decoded time-dependent samples. This feature
works well with harmonic content 2 or more frames long, like speech,
human or non-human, piano music or any constant tones at very low
bitrates.

It should be noted that the current coder is highly efficient and
the rate control system is unable to encode files at extremely
low bitrates (less than 14kbps seems to be impossible) so this
extension isn't capable of optimum operation. Dramatic difference
is observable with some types of audio and speech but for the most
part the audiable differences are subtle. The spectrum looks better
however so the encoder is able to harvest the additional bits that
this feature provies, should the user choose to enable it. So
it's best to enable this feature only if encoding at the absolutely
lowest bitrate that the encoder is capable of.

Rostislav Pehlivanov authored on 2015/10/17 10:22:51
Showing 7 changed files
... ...
@@ -135,6 +135,7 @@ OBJS-$(CONFIG_AAC_ENCODER)             += aacenc.o aaccoder.o aacenctab.o    \
135 135
                                           aacpsy.o aactab.o      \
136 136
                                           aacenc_is.o \
137 137
                                           aacenc_tns.o \
138
+                                          aacenc_ltp.o \
138 139
                                           aacenc_pred.o \
139 140
                                           psymodel.o mpeg4audio.o kbdwin.o
140 141
 OBJS-$(CONFIG_AASC_DECODER)            += aasc.o msrledec.o
... ...
@@ -161,6 +161,7 @@ typedef struct PredictorState {
161 161
 typedef struct LongTermPrediction {
162 162
     int8_t present;
163 163
     int16_t lag;
164
+    int coef_idx;
164 165
     INTFLOAT coef;
165 166
     int8_t used[MAX_LTP_LONG_SFB];
166 167
 } LongTermPrediction;
... ...
@@ -260,6 +261,7 @@ typedef struct SingleChannelElement {
260 260
     DECLARE_ALIGNED(32, INTFLOAT, saved)[1536];     ///< overlap
261 261
     DECLARE_ALIGNED(32, INTFLOAT, ret_buf)[2048];   ///< PCM output buffer
262 262
     DECLARE_ALIGNED(16, INTFLOAT, ltp_state)[3072]; ///< time signal for LTP
263
+    DECLARE_ALIGNED(32, AAC_FLOAT, lcoeffs)[1024];  ///< MDCT of LTP coefficients (used by encoder)
263 264
     DECLARE_ALIGNED(32, AAC_FLOAT, prcoeffs)[1024]; ///< Main prediction coefs (used by encoder)
264 265
     PredictorState predictor_state[MAX_PREDICTORS];
265 266
     INTFLOAT *ret;                                  ///< PCM output
... ...
@@ -48,6 +48,7 @@
48 48
 
49 49
 #include "aacenc_is.h"
50 50
 #include "aacenc_tns.h"
51
+#include "aacenc_ltp.h"
51 52
 #include "aacenc_pred.h"
52 53
 
53 54
 #include "libavcodec/aaccoder_twoloop.h"
... ...
@@ -911,14 +912,19 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
911 911
         encode_window_bands_info,
912 912
         quantize_and_encode_band,
913 913
         ff_aac_encode_tns_info,
914
+        ff_aac_encode_ltp_info,
914 915
         ff_aac_encode_main_pred,
915 916
         ff_aac_adjust_common_pred,
917
+        ff_aac_adjust_common_ltp,
916 918
         ff_aac_apply_main_pred,
917 919
         ff_aac_apply_tns,
920
+        ff_aac_update_ltp,
921
+        ff_aac_ltp_insert_new_frame,
918 922
         set_special_band_scalefactors,
919 923
         search_for_pns,
920 924
         mark_pns,
921 925
         ff_aac_search_for_tns,
926
+        ff_aac_search_for_ltp,
922 927
         search_for_ms,
923 928
         ff_aac_search_for_is,
924 929
         ff_aac_search_for_pred,
... ...
@@ -928,14 +934,19 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
928 928
         encode_window_bands_info,
929 929
         quantize_and_encode_band,
930 930
         ff_aac_encode_tns_info,
931
+        ff_aac_encode_ltp_info,
931 932
         ff_aac_encode_main_pred,
932 933
         ff_aac_adjust_common_pred,
934
+        ff_aac_adjust_common_ltp,
933 935
         ff_aac_apply_main_pred,
934 936
         ff_aac_apply_tns,
937
+        ff_aac_update_ltp,
938
+        ff_aac_ltp_insert_new_frame,
935 939
         set_special_band_scalefactors,
936 940
         search_for_pns,
937 941
         mark_pns,
938 942
         ff_aac_search_for_tns,
943
+        ff_aac_search_for_ltp,
939 944
         search_for_ms,
940 945
         ff_aac_search_for_is,
941 946
         ff_aac_search_for_pred,
... ...
@@ -945,14 +956,19 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
945 945
         codebook_trellis_rate,
946 946
         quantize_and_encode_band,
947 947
         ff_aac_encode_tns_info,
948
+        ff_aac_encode_ltp_info,
948 949
         ff_aac_encode_main_pred,
949 950
         ff_aac_adjust_common_pred,
951
+        ff_aac_adjust_common_ltp,
950 952
         ff_aac_apply_main_pred,
951 953
         ff_aac_apply_tns,
954
+        ff_aac_update_ltp,
955
+        ff_aac_ltp_insert_new_frame,
952 956
         set_special_band_scalefactors,
953 957
         search_for_pns,
954 958
         mark_pns,
955 959
         ff_aac_search_for_tns,
960
+        ff_aac_search_for_ltp,
956 961
         search_for_ms,
957 962
         ff_aac_search_for_is,
958 963
         ff_aac_search_for_pred,
... ...
@@ -962,14 +978,19 @@ AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB] = {
962 962
         encode_window_bands_info,
963 963
         quantize_and_encode_band,
964 964
         ff_aac_encode_tns_info,
965
+        ff_aac_encode_ltp_info,
965 966
         ff_aac_encode_main_pred,
966 967
         ff_aac_adjust_common_pred,
968
+        ff_aac_adjust_common_ltp,
967 969
         ff_aac_apply_main_pred,
968 970
         ff_aac_apply_tns,
971
+        ff_aac_update_ltp,
972
+        ff_aac_ltp_insert_new_frame,
969 973
         set_special_band_scalefactors,
970 974
         search_for_pns,
971 975
         mark_pns,
972 976
         ff_aac_search_for_tns,
977
+        ff_aac_search_for_ltp,
973 978
         search_for_ms,
974 979
         ff_aac_search_for_is,
975 980
         ff_aac_search_for_pred,
... ...
@@ -60,6 +60,7 @@ static const struct AACProfileOptions aacenc_profiles[] = {
60 60
             .mid_side = 0,
61 61
             .pns = 1,
62 62
             .tns = 0,
63
+            .ltp = OPT_BANNED,
63 64
             .pred = OPT_REQUIRED,
64 65
             .intensity_stereo = 1,
65 66
         },
... ...
@@ -69,6 +70,7 @@ static const struct AACProfileOptions aacenc_profiles[] = {
69 69
             .mid_side = 0,
70 70
             .pns = 1,
71 71
             .tns = 0,
72
+            .ltp = OPT_NEEDS_LTP,
72 73
             .pred = OPT_NEEDS_MAIN,
73 74
             .intensity_stereo = 1,
74 75
         },
... ...
@@ -78,6 +80,17 @@ static const struct AACProfileOptions aacenc_profiles[] = {
78 78
             .mid_side = 0,
79 79
             .pns = OPT_BANNED,
80 80
             .tns = 0,
81
+            .ltp = OPT_BANNED,
82
+            .pred = OPT_BANNED,
83
+            .intensity_stereo = 1,
84
+        },
85
+    },
86
+    {FF_PROFILE_AAC_LTP,
87
+        {  /* Long term prediction profile */
88
+            .mid_side = 0,
89
+            .pns = 1,
90
+            .tns = 0,
91
+            .ltp = OPT_REQUIRED,
81 92
             .pred = OPT_BANNED,
82 93
             .intensity_stereo = 1,
83 94
         },
... ...
@@ -475,6 +488,8 @@ static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
475 475
         put_ics_info(s, &sce->ics);
476 476
         if (s->coder->encode_main_pred)
477 477
             s->coder->encode_main_pred(s, sce);
478
+        if (s->coder->encode_ltp_info)
479
+            s->coder->encode_ltp_info(s, sce, 0);
478 480
     }
479 481
     encode_band_info(s, sce);
480 482
     encode_scale_factors(avctx, s, sce);
... ...
@@ -625,6 +640,13 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
625 625
             }
626 626
 
627 627
             apply_window_and_mdct(s, sce, overlap);
628
+
629
+            if (s->options.ltp && s->coder->update_ltp) {
630
+                s->coder->update_ltp(s, sce);
631
+                apply_window[sce->ics.window_sequence[0]](s->fdsp, sce, &sce->ltp_state[0]);
632
+                s->mdct1024.mdct_calc(&s->mdct1024, sce->lcoeffs, sce->ret_buf);
633
+            }
634
+
628 635
             if (isnan(cpe->ch->coeffs[0])) {
629 636
                 av_log(avctx, AV_LOG_ERROR, "Input contains NaN\n");
630 637
                 return AVERROR(EINVAL);
... ...
@@ -659,6 +681,8 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
659 659
                 sce = &cpe->ch[ch];
660 660
                 coeffs[ch] = sce->coeffs;
661 661
                 sce->ics.predictor_present = 0;
662
+                sce->ics.ltp.present = 0;
663
+                memset(sce->ics.ltp.used, 0, sizeof(sce->ics.ltp.used));
662 664
                 memset(sce->ics.prediction_used, 0, sizeof(sce->ics.prediction_used));
663 665
                 memset(&sce->tns, 0, sizeof(TemporalNoiseShaping));
664 666
                 for (w = 0; w < 128; w++)
... ...
@@ -738,12 +762,26 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
738 738
                 apply_mid_side_stereo(cpe);
739 739
             }
740 740
             adjust_frame_information(cpe, chans);
741
+            if (s->options.ltp) { /* LTP */
742
+                for (ch = 0; ch < chans; ch++) {
743
+                    sce = &cpe->ch[ch];
744
+                    s->cur_channel = start_ch + ch;
745
+                    if (s->coder->search_for_ltp)
746
+                        s->coder->search_for_ltp(s, sce, cpe->common_window);
747
+                    if (sce->ics.ltp.present) pred_mode = 1;
748
+                }
749
+                s->cur_channel = start_ch;
750
+                if (s->coder->adjust_common_ltp)
751
+                    s->coder->adjust_common_ltp(s, cpe);
752
+            }
741 753
             if (chans == 2) {
742 754
                 put_bits(&s->pb, 1, cpe->common_window);
743 755
                 if (cpe->common_window) {
744 756
                     put_ics_info(s, &cpe->ch[0].ics);
745 757
                     if (s->coder->encode_main_pred)
746 758
                         s->coder->encode_main_pred(s, &cpe->ch[0]);
759
+                    if (s->coder->encode_ltp_info)
760
+                        s->coder->encode_ltp_info(s, &cpe->ch[0], 1);
747 761
                     encode_ms_info(&s->pb, cpe);
748 762
                     if (cpe->ms_mode) ms_mode = 1;
749 763
                 }
... ...
@@ -816,6 +854,9 @@ static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
816 816
         }
817 817
     } while (1);
818 818
 
819
+    if (s->options.ltp && s->coder->ltp_insert_new_frame)
820
+        s->coder->ltp_insert_new_frame(s);
821
+
819 822
     put_bits(&s->pb, 3, TYPE_END);
820 823
     flush_put_bits(&s->pb);
821 824
     avctx->frame_bits = put_bits_count(&s->pb);
... ...
@@ -935,6 +976,7 @@ static av_cold int aac_encode_init(AVCodecContext *avctx)
935 935
     AAC_OPT_SET(&s->options, p_opt, 1, coder);
936 936
     AAC_OPT_SET(&s->options, p_opt, 0, pns);
937 937
     AAC_OPT_SET(&s->options, p_opt, 1, tns);
938
+    AAC_OPT_SET(&s->options, p_opt, 0, ltp);
938 939
     AAC_OPT_SET(&s->options, p_opt, 0, pred);
939 940
     AAC_OPT_SET(&s->options, p_opt, 1, mid_side);
940 941
     AAC_OPT_SET(&s->options, p_opt, 0, intensity_stereo);
... ...
@@ -993,6 +1035,7 @@ static const AVOption aacenc_options[] = {
993 993
     {"aac_is", "Intensity stereo coding", offsetof(AACEncContext, options.intensity_stereo), AV_OPT_TYPE_BOOL, {.i64 = OPT_AUTO}, -1, 1, AACENC_FLAGS},
994 994
     {"aac_pns", "Perceptual noise substitution", offsetof(AACEncContext, options.pns), AV_OPT_TYPE_BOOL, {.i64 = OPT_AUTO}, -1, 1, AACENC_FLAGS},
995 995
     {"aac_tns", "Temporal noise shaping", offsetof(AACEncContext, options.tns), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
996
+    {"aac_ltp", "Long term prediction", offsetof(AACEncContext, options.ltp), AV_OPT_TYPE_BOOL, {.i64 = OPT_AUTO}, -1, 1, AACENC_FLAGS},
996 997
     {"aac_pred", "AAC-Main prediction", offsetof(AACEncContext, options.pred), AV_OPT_TYPE_BOOL, {.i64 = OPT_AUTO}, -1, 1, AACENC_FLAGS},
997 998
     {NULL}
998 999
 };
... ...
@@ -45,6 +45,7 @@ typedef struct AACEncOptions {
45 45
     int coder;
46 46
     int pns;
47 47
     int tns;
48
+    int ltp;
48 49
     int pred;
49 50
     int mid_side;
50 51
     int intensity_stereo;
... ...
@@ -60,14 +61,19 @@ typedef struct AACCoefficientsEncoder {
60 60
     void (*quantize_and_encode_band)(struct AACEncContext *s, PutBitContext *pb, const float *in, float *out, int size,
61 61
                                      int scale_idx, int cb, const float lambda, int rtz);
62 62
     void (*encode_tns_info)(struct AACEncContext *s, SingleChannelElement *sce);
63
+    void (*encode_ltp_info)(struct AACEncContext *s, SingleChannelElement *sce, int common_window);
63 64
     void (*encode_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);
64 65
     void (*adjust_common_pred)(struct AACEncContext *s, ChannelElement *cpe);
66
+    void (*adjust_common_ltp)(struct AACEncContext *s, ChannelElement *cpe);
65 67
     void (*apply_main_pred)(struct AACEncContext *s, SingleChannelElement *sce);
66 68
     void (*apply_tns_filt)(struct AACEncContext *s, SingleChannelElement *sce);
69
+    void (*update_ltp)(struct AACEncContext *s, SingleChannelElement *sce);
70
+    void (*ltp_insert_new_frame)(struct AACEncContext *s);
67 71
     void (*set_special_band_scalefactors)(struct AACEncContext *s, SingleChannelElement *sce);
68 72
     void (*search_for_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
69 73
     void (*mark_pns)(struct AACEncContext *s, AVCodecContext *avctx, SingleChannelElement *sce);
70 74
     void (*search_for_tns)(struct AACEncContext *s, SingleChannelElement *sce);
75
+    void (*search_for_ltp)(struct AACEncContext *s, SingleChannelElement *sce, int common_window);
71 76
     void (*search_for_ms)(struct AACEncContext *s, ChannelElement *cpe);
72 77
     void (*search_for_is)(struct AACEncContext *s, AVCodecContext *avctx, ChannelElement *cpe);
73 78
     void (*search_for_pred)(struct AACEncContext *s, SingleChannelElement *sce);
74 79
new file mode 100644
... ...
@@ -0,0 +1,227 @@
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 long term prediction
24
+ * @author Rostislav Pehlivanov ( atomnuker gmail com )
25
+ */
26
+
27
+#include "aacenc_ltp.h"
28
+#include "aacenc_quantization.h"
29
+#include "aacenc_utils.h"
30
+
31
+/**
32
+ * Encode LTP data.
33
+ */
34
+void ff_aac_encode_ltp_info(AACEncContext *s, SingleChannelElement *sce,
35
+                            int common_window)
36
+{
37
+    int i;
38
+    IndividualChannelStream *ics = &sce->ics;
39
+    if (s->profile != FF_PROFILE_AAC_LTP || !ics->predictor_present)
40
+        return;
41
+    if (common_window)
42
+        put_bits(&s->pb, 1, 0);
43
+    put_bits(&s->pb, 1, ics->ltp.present);
44
+    if (!ics->ltp.present)
45
+        return;
46
+    put_bits(&s->pb, 11, ics->ltp.lag);
47
+    put_bits(&s->pb, 3,  ics->ltp.coef_idx);
48
+    for (i = 0; i < FFMIN(ics->max_sfb, MAX_LTP_LONG_SFB); i++)
49
+        put_bits(&s->pb, 1, ics->ltp.used[i]);
50
+}
51
+
52
+void ff_aac_ltp_insert_new_frame(AACEncContext *s)
53
+{
54
+    int i, ch, tag, chans, cur_channel, start_ch = 0;
55
+    ChannelElement *cpe;
56
+    SingleChannelElement *sce;
57
+    for (i = 0; i < s->chan_map[0]; i++) {
58
+        cpe = &s->cpe[i];
59
+        tag      = s->chan_map[i+1];
60
+        chans    = tag == TYPE_CPE ? 2 : 1;
61
+        for (ch = 0; ch < chans; ch++) {
62
+            sce = &cpe->ch[ch];
63
+            cur_channel = start_ch + ch;
64
+            /* New sample + overlap */
65
+            memcpy(&sce->ltp_state[0],    &sce->ltp_state[1024], 1024*sizeof(sce->ltp_state[0]));
66
+            memcpy(&sce->ltp_state[1024], &s->planar_samples[cur_channel][2048], 1024*sizeof(sce->ltp_state[0]));
67
+            memcpy(&sce->ltp_state[2048], &sce->ret_buf[0], 1024*sizeof(sce->ltp_state[0]));
68
+        }
69
+        start_ch += chans;
70
+    }
71
+}
72
+
73
+/**
74
+ * Process LTP parameters
75
+ * @see Patent WO2006070265A1
76
+ */
77
+void ff_aac_update_ltp(AACEncContext *s, SingleChannelElement *sce)
78
+{
79
+    int i, j, lag;
80
+    float corr, s0, s1, max_corr = 0.0f;
81
+    float *samples = &s->planar_samples[s->cur_channel][1024];
82
+    float *pred_signal = &sce->ltp_state[0];
83
+    int samples_num = 2048;
84
+
85
+    if (s->profile != FF_PROFILE_AAC_LTP)
86
+        return;
87
+
88
+    /* Calculate lag */
89
+    for (i = 0; i < samples_num; i++) {
90
+        s0 = s1 = 0.0f;
91
+        for (j = 0; j < samples_num; j++) {
92
+            if (j + 1024 < i)
93
+                continue;
94
+            s0 += samples[j]*pred_signal[j-i+1024];
95
+            s1 += pred_signal[j-i+1024]*pred_signal[j-i+1024];
96
+        }
97
+        corr = s1 > 0.0f ? s0/sqrt(s1) : 0.0f;
98
+        if (corr > max_corr) {
99
+            max_corr = corr;
100
+            lag = i;
101
+        }
102
+    }
103
+    lag = av_clip(lag, 0, 2048); /* 11 bits => 2^11 = 2048 */
104
+
105
+    if (!lag) {
106
+        sce->ics.ltp.lag = lag;
107
+        return;
108
+    }
109
+
110
+    s0 = s1 = 0.0f;
111
+    for (i = 0; i < lag; i++) {
112
+        s0 += samples[i];
113
+        s1 += pred_signal[i-lag+1024];
114
+    }
115
+
116
+    sce->ics.ltp.coef_idx = quant_array_idx(s0/s1, ltp_coef, 8);
117
+    sce->ics.ltp.coef     = ltp_coef[sce->ics.ltp.coef_idx];
118
+
119
+    /* Predict the new samples */
120
+    if (lag < 1024)
121
+        samples_num = lag + 1024;
122
+    for (i = 0; i < samples_num; i++)
123
+        pred_signal[i+1024] = sce->ics.ltp.coef*pred_signal[i-lag+1024];
124
+    memset(&pred_signal[samples_num], 0, (2048 - samples_num)*sizeof(float));
125
+
126
+    sce->ics.ltp.lag = lag;
127
+}
128
+
129
+void ff_aac_adjust_common_ltp(AACEncContext *s, ChannelElement *cpe)
130
+{
131
+    int sfb, count = 0;
132
+    SingleChannelElement *sce0 = &cpe->ch[0];
133
+    SingleChannelElement *sce1 = &cpe->ch[1];
134
+
135
+    if (!cpe->common_window ||
136
+        sce0->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE ||
137
+        sce1->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE)
138
+        return;
139
+
140
+    for (sfb = 0; sfb < FFMIN(sce0->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++) {
141
+        int sum = sce0->ics.ltp.used[sfb] + sce1->ics.ltp.used[sfb];
142
+        if (sum != 2) {
143
+            sce0->ics.ltp.used[sfb] = 0;
144
+        } else if (sum == 2) {
145
+            count++;
146
+        }
147
+    }
148
+
149
+    sce0->ics.ltp.present = !!count;
150
+    sce0->ics.predictor_present = !!count;
151
+}
152
+
153
+/**
154
+ * Mark LTP sfb's
155
+ */
156
+void ff_aac_search_for_ltp(AACEncContext *s, SingleChannelElement *sce,
157
+                           int common_window)
158
+{
159
+    int w, g, w2, i, start = 0, count = 0;
160
+    int saved_bits = -(15 + FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB));
161
+    float *C34 = &s->scoefs[128*0], *PCD = &s->scoefs[128*1];
162
+    float *PCD34 = &s->scoefs[128*2];
163
+    const int max_ltp = FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB);
164
+
165
+    if (sce->ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE ||
166
+        !sce->ics.ltp.lag)
167
+        return;
168
+
169
+    for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
170
+        start = 0;
171
+        for (g = 0;  g < sce->ics.num_swb; g++) {
172
+            int bits1 = 0, bits2 = 0;
173
+            float dist1 = 0.0f, dist2 = 0.0f;
174
+            if (w*16+g > max_ltp) {
175
+                start += sce->ics.swb_sizes[g];
176
+                continue;
177
+            }
178
+            for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
179
+                int bits_tmp1, bits_tmp2;
180
+                FFPsyBand *band = &s->psy.ch[s->cur_channel].psy_bands[(w+w2)*16+g];
181
+                for (i = 0; i < sce->ics.swb_sizes[g]; i++)
182
+                    PCD[i] = sce->coeffs[start+(w+w2)*128+i] - sce->lcoeffs[start+(w+w2)*128+i];
183
+                abs_pow34_v(C34,  &sce->coeffs[start+(w+w2)*128],  sce->ics.swb_sizes[g]);
184
+                abs_pow34_v(PCD34, PCD, sce->ics.swb_sizes[g]);
185
+                dist1 += quantize_band_cost(s, &sce->coeffs[start+(w+w2)*128], C34, sce->ics.swb_sizes[g],
186
+                                            sce->sf_idx[(w+w2)*16+g], sce->band_type[(w+w2)*16+g],
187
+                                            s->lambda/band->threshold, INFINITY, &bits_tmp1, NULL, 0);
188
+                dist2 += quantize_band_cost(s, PCD, PCD34, sce->ics.swb_sizes[g],
189
+                                            sce->sf_idx[(w+w2)*16+g],
190
+                                            sce->band_type[(w+w2)*16+g],
191
+                                            s->lambda/band->threshold, INFINITY, &bits_tmp2, NULL, 0);
192
+                bits1 += bits_tmp1;
193
+                bits2 += bits_tmp2;
194
+            }
195
+            if (dist2 < dist1 && bits2 < bits1) {
196
+                for (w2 = 0; w2 < sce->ics.group_len[w]; w2++)
197
+                    for (i = 0; i < sce->ics.swb_sizes[g]; i++)
198
+                        sce->coeffs[start+(w+w2)*128+i] -= sce->lcoeffs[start+(w+w2)*128+i];
199
+                sce->ics.ltp.used[w*16+g] = 1;
200
+                saved_bits += bits1 - bits2;
201
+                count++;
202
+            }
203
+            start += sce->ics.swb_sizes[g];
204
+        }
205
+    }
206
+
207
+    sce->ics.ltp.present = !!count && (saved_bits >= 0);
208
+    sce->ics.predictor_present = !!sce->ics.ltp.present;
209
+
210
+    /* Reset any marked sfbs */
211
+    if (!sce->ics.ltp.present && !!count) {
212
+        for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
213
+            start = 0;
214
+            for (g = 0;  g < sce->ics.num_swb; g++) {
215
+                if (sce->ics.ltp.used[w*16+g]) {
216
+                    for (w2 = 0; w2 < sce->ics.group_len[w]; w2++) {
217
+                        for (i = 0; i < sce->ics.swb_sizes[g]; i++) {
218
+                            sce->coeffs[start+(w+w2)*128+i] += sce->lcoeffs[start+(w+w2)*128+i];
219
+                        }
220
+                    }
221
+                }
222
+                start += sce->ics.swb_sizes[g];
223
+            }
224
+        }
225
+    }
226
+}
0 227
new file mode 100644
... ...
@@ -0,0 +1,41 @@
0
+/*
1
+ * AAC encoder long term prediction extension
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 long term prediction extension
24
+ * @author Rostislav Pehlivanov ( atomnuker gmail com )
25
+ */
26
+
27
+#ifndef AVCODEC_AACENC_LTP_H
28
+#define AVCODEC_AACENC_LTP_H
29
+
30
+#include "aacenc.h"
31
+
32
+void ff_aac_encode_ltp_info(AACEncContext *s, SingleChannelElement *sce,
33
+                            int common_window);
34
+void ff_aac_update_ltp(AACEncContext *s, SingleChannelElement *sce);
35
+void ff_aac_adjust_common_ltp(AACEncContext *s, ChannelElement *cpe);
36
+void ff_aac_ltp_insert_new_frame(AACEncContext *s);
37
+void ff_aac_search_for_ltp(AACEncContext *s, SingleChannelElement *sce,
38
+                           int common_window);
39
+
40
+#endif /* AVCODEC_AACENC_LTP_H */