Browse code

aacdec: Implement LTP support.

Ported from gsoc svn.

Young Han Lee authored on 2011/02/14 18:09:43
Showing 4 changed files
... ...
@@ -43,6 +43,7 @@
43 43
 #define MAX_ELEM_ID 16
44 44
 
45 45
 #define TNS_MAX_ORDER 20
46
+#define MAX_LTP_LONG_SFB 40
46 47
 
47 48
 enum RawDataBlockType {
48 49
     TYPE_SCE,
... ...
@@ -131,6 +132,16 @@ typedef struct {
131 131
 #define SCALE_DIFF_ZERO  60    ///< codebook index corresponding to zero scalefactor indices difference
132 132
 
133 133
 /**
134
+ * Long Term Prediction
135
+ */
136
+typedef struct {
137
+    int8_t present;
138
+    int16_t lag;
139
+    float coef;
140
+    int8_t used[MAX_LTP_LONG_SFB];
141
+} LongTermPrediction;
142
+
143
+/**
134 144
  * Individual Channel Stream
135 145
  */
136 146
 typedef struct {
... ...
@@ -139,6 +150,7 @@ typedef struct {
139 139
     uint8_t use_kb_window[2];   ///< If set, use Kaiser-Bessel window, otherwise use a sinus window.
140 140
     int num_window_groups;
141 141
     uint8_t group_len[8];
142
+    LongTermPrediction ltp;
142 143
     const uint16_t *swb_offset; ///< table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular window
143 144
     const uint8_t *swb_sizes;   ///< table of scalefactor band sizes for a particular window
144 145
     int num_swb;                ///< number of scalefactor window bands
... ...
@@ -206,14 +218,15 @@ typedef struct {
206 206
     IndividualChannelStream ics;
207 207
     TemporalNoiseShaping tns;
208 208
     Pulse pulse;
209
-    enum BandType band_type[128];             ///< band types
210
-    int band_type_run_end[120];               ///< band type run end points
211
-    float sf[120];                            ///< scalefactors
212
-    int sf_idx[128];                          ///< scalefactor indices (used by encoder)
213
-    uint8_t zeroes[128];                      ///< band is not coded (used by encoder)
214
-    DECLARE_ALIGNED(16, float, coeffs)[1024]; ///< coefficients for IMDCT
215
-    DECLARE_ALIGNED(16, float, saved)[1024];  ///< overlap
216
-    DECLARE_ALIGNED(16, float, ret)[2048];    ///< PCM output
209
+    enum BandType band_type[128];                   ///< band types
210
+    int band_type_run_end[120];                     ///< band type run end points
211
+    float sf[120];                                  ///< scalefactors
212
+    int sf_idx[128];                                ///< scalefactor indices (used by encoder)
213
+    uint8_t zeroes[128];                            ///< band is not coded (used by encoder)
214
+    DECLARE_ALIGNED(16, float,   coeffs)[1024];     ///< coefficients for IMDCT
215
+    DECLARE_ALIGNED(16, float,   saved)[1024];      ///< overlap
216
+    DECLARE_ALIGNED(16, float,   ret)[2048];        ///< PCM output
217
+    DECLARE_ALIGNED(16, int16_t, ltp_state)[3072];  ///< time signal for LTP
217 218
     PredictorState predictor_state[MAX_PREDICTORS];
218 219
 } SingleChannelElement;
219 220
 
... ...
@@ -259,7 +272,7 @@ typedef struct {
259 259
      * @defgroup temporary aligned temporary buffers (We do not want to have these on the stack.)
260 260
      * @{
261 261
      */
262
-    DECLARE_ALIGNED(16, float, buf_mdct)[1024];
262
+    DECLARE_ALIGNED(16, float, buf_mdct)[2048];
263 263
     /** @} */
264 264
 
265 265
     /**
... ...
@@ -268,6 +281,7 @@ typedef struct {
268 268
      */
269 269
     FFTContext mdct;
270 270
     FFTContext mdct_small;
271
+    FFTContext mdct_ltp;
271 272
     DSPContext dsp;
272 273
     FmtConvertContext fmt_conv;
273 274
     int random_state;
... ...
@@ -42,7 +42,7 @@
42 42
  * Y                    filterbank - standard
43 43
  * N (code in SoC repo) filterbank - Scalable Sample Rate
44 44
  * Y                    Temporal Noise Shaping
45
- * N (code in SoC repo) Long Term Prediction
45
+ * Y                    Long Term Prediction
46 46
  * Y                    intensity stereo
47 47
  * Y                    channel coupling
48 48
  * Y                    frequency domain prediction
... ...
@@ -478,6 +478,7 @@ static int decode_audio_specific_config(AACContext *ac,
478 478
     switch (m4ac->object_type) {
479 479
     case AOT_AAC_MAIN:
480 480
     case AOT_AAC_LC:
481
+    case AOT_AAC_LTP:
481 482
         if (decode_ga_specific_config(ac, avctx, &gb, m4ac, m4ac->chan_config))
482 483
             return -1;
483 484
         break;
... ...
@@ -580,8 +581,9 @@ static av_cold int aac_decode_init(AVCodecContext *avctx)
580 580
                     ff_aac_scalefactor_code, sizeof(ff_aac_scalefactor_code[0]), sizeof(ff_aac_scalefactor_code[0]),
581 581
                     352);
582 582
 
583
-    ff_mdct_init(&ac->mdct, 11, 1, 1.0);
584
-    ff_mdct_init(&ac->mdct_small, 8, 1, 1.0);
583
+    ff_mdct_init(&ac->mdct,       11, 1, 1.0);
584
+    ff_mdct_init(&ac->mdct_small,  8, 1, 1.0);
585
+    ff_mdct_init(&ac->mdct_ltp,   11, 0, 1.0);
585 586
     // window initialization
586 587
     ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
587 588
     ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
... ...
@@ -631,6 +633,20 @@ static int decode_prediction(AACContext *ac, IndividualChannelStream *ics,
631 631
 }
632 632
 
633 633
 /**
634
+ * Decode Long Term Prediction data; reference: table 4.xx.
635
+ */
636
+static void decode_ltp(AACContext *ac, LongTermPrediction *ltp,
637
+                       GetBitContext *gb, uint8_t max_sfb)
638
+{
639
+    int sfb;
640
+
641
+    ltp->lag  = get_bits(gb, 11);
642
+    ltp->coef = ltp_coef[get_bits(gb, 3)] * ac->sf_scale;
643
+    for (sfb = 0; sfb < FFMIN(max_sfb, MAX_LTP_LONG_SFB); sfb++)
644
+        ltp->used[sfb] = get_bits1(gb);
645
+}
646
+
647
+/**
634 648
  * Decode Individual Channel Stream info; reference: table 4.6.
635 649
  *
636 650
  * @param   common_window   Channels have independent [0], or shared [1], Individual Channel Stream information.
... ...
@@ -684,9 +700,8 @@ static int decode_ics_info(AACContext *ac, IndividualChannelStream *ics,
684 684
                 memset(ics, 0, sizeof(IndividualChannelStream));
685 685
                 return -1;
686 686
             } else {
687
-                av_log_missing_feature(ac->avctx, "Predictor bit set but LTP is", 1);
688
-                memset(ics, 0, sizeof(IndividualChannelStream));
689
-                return -1;
687
+                if ((ics->ltp.present = get_bits(gb, 1)))
688
+                    decode_ltp(ac, &ics->ltp, gb, ics->max_sfb);
690 689
             }
691 690
         }
692 691
     }
... ...
@@ -1420,6 +1435,9 @@ static int decode_cpe(AACContext *ac, GetBitContext *gb, ChannelElement *cpe)
1420 1420
         i = cpe->ch[1].ics.use_kb_window[0];
1421 1421
         cpe->ch[1].ics = cpe->ch[0].ics;
1422 1422
         cpe->ch[1].ics.use_kb_window[1] = i;
1423
+        if (cpe->ch[1].ics.predictor_present && (ac->m4ac.object_type != AOT_AAC_MAIN))
1424
+            if ((cpe->ch[1].ics.ltp.present = get_bits(gb, 1)))
1425
+                decode_ltp(ac, &cpe->ch[1].ics.ltp, gb, cpe->ch[1].ics.max_sfb);
1423 1426
         ms_present = get_bits(gb, 2);
1424 1427
         if (ms_present == 3) {
1425 1428
             av_log(ac->avctx, AV_LOG_ERROR, "ms_present = 3 is reserved.\n");
... ...
@@ -1659,6 +1677,7 @@ static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
1659 1659
     int w, filt, m, i;
1660 1660
     int bottom, top, order, start, end, size, inc;
1661 1661
     float lpc[TNS_MAX_ORDER];
1662
+    float tmp[TNS_MAX_ORDER];
1662 1663
 
1663 1664
     for (w = 0; w < ics->num_windows; w++) {
1664 1665
         bottom = ics->num_swb;
... ...
@@ -1684,15 +1703,119 @@ static void apply_tns(float coef[1024], TemporalNoiseShaping *tns,
1684 1684
             }
1685 1685
             start += w * 128;
1686 1686
 
1687
-            // ar filter
1688
-            for (m = 0; m < size; m++, start += inc)
1689
-                for (i = 1; i <= FFMIN(m, order); i++)
1690
-                    coef[start] -= coef[start - i * inc] * lpc[i - 1];
1687
+            if (decode) {
1688
+                // ar filter
1689
+                for (m = 0; m < size; m++, start += inc)
1690
+                    for (i = 1; i <= FFMIN(m, order); i++)
1691
+                        coef[start] -= coef[start - i * inc] * lpc[i - 1];
1692
+            } else {
1693
+                // ma filter
1694
+                for (m = 0; m < size; m++, start += inc) {
1695
+                    tmp[0] = coef[start];
1696
+                    for (i = 1; i <= FFMIN(m, order); i++)
1697
+                        coef[start] += tmp[i] * lpc[i - 1];
1698
+                    for (i = order; i > 0; i--)
1699
+                        tmp[i] = tmp[i - 1];
1700
+                }
1701
+            }
1691 1702
         }
1692 1703
     }
1693 1704
 }
1694 1705
 
1695 1706
 /**
1707
+ *  Apply windowing and MDCT to obtain the spectral
1708
+ *  coefficient from the predicted sample by LTP.
1709
+ */
1710
+static void windowing_and_mdct_ltp(AACContext *ac, float *out,
1711
+                                   float *in, IndividualChannelStream *ics)
1712
+{
1713
+    const float *lwindow      = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
1714
+    const float *swindow      = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
1715
+    const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
1716
+    const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
1717
+
1718
+    if (ics->window_sequence[0] != LONG_STOP_SEQUENCE) {
1719
+        ac->dsp.vector_fmul(in, in, lwindow_prev, 1024);
1720
+    } else {
1721
+        memset(in, 0, 448 * sizeof(float));
1722
+        ac->dsp.vector_fmul(in + 448, in + 448, swindow_prev, 128);
1723
+        memcpy(in + 576, in + 576, 448 * sizeof(float));
1724
+    }
1725
+    if (ics->window_sequence[0] != LONG_START_SEQUENCE) {
1726
+        ac->dsp.vector_fmul_reverse(in + 1024, in + 1024, lwindow, 1024);
1727
+    } else {
1728
+        memcpy(in + 1024, in + 1024, 448 * sizeof(float));
1729
+        ac->dsp.vector_fmul_reverse(in + 1024 + 448, in + 1024 + 448, swindow, 128);
1730
+        memset(in + 1024 + 576, 0, 448 * sizeof(float));
1731
+    }
1732
+    ff_mdct_calc(&ac->mdct_ltp, out, in);
1733
+}
1734
+
1735
+/**
1736
+ * Apply the long term prediction
1737
+ */
1738
+static void apply_ltp(AACContext *ac, SingleChannelElement *sce)
1739
+{
1740
+    const LongTermPrediction *ltp = &sce->ics.ltp;
1741
+    const uint16_t *offsets = sce->ics.swb_offset;
1742
+    int i, sfb;
1743
+
1744
+    if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
1745
+        float *predTime = ac->buf_mdct;
1746
+        float *predFreq = sce->ret;
1747
+        int16_t num_samples = 2048;
1748
+
1749
+        if (ltp->lag < 1024)
1750
+            num_samples = ltp->lag + 1024;
1751
+        for (i = 0; i < num_samples; i++)
1752
+            predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
1753
+        memset(&predTime[i], 0, (2048 - i) * sizeof(float));
1754
+
1755
+        windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
1756
+
1757
+        if (sce->tns.present)
1758
+            apply_tns(predFreq, &sce->tns, &sce->ics, 0);
1759
+
1760
+        for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
1761
+            if (ltp->used[sfb])
1762
+                for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
1763
+                    sce->coeffs[i] += predFreq[i];
1764
+    }
1765
+}
1766
+
1767
+/**
1768
+ * Update the LTP buffer for next frame
1769
+ */
1770
+static void update_ltp(AACContext *ac, SingleChannelElement *sce)
1771
+{
1772
+    IndividualChannelStream *ics = &sce->ics;
1773
+    float *saved     = sce->saved;
1774
+    float *saved_ltp = sce->coeffs;
1775
+    const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
1776
+    const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
1777
+    int i;
1778
+
1779
+    for (i = 0; i < 512; i++)
1780
+        ac->buf_mdct[1535 - i] = ac->buf_mdct[512 + i];
1781
+
1782
+    if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
1783
+        memcpy(saved_ltp,       saved, 512 * sizeof(float));
1784
+        memset(saved_ltp + 576, 0,     448 * sizeof(float));
1785
+        ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960,     swindow,     128);
1786
+    } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
1787
+        memcpy(saved_ltp,       ac->buf_mdct + 512, 448 * sizeof(float));
1788
+        memset(saved_ltp + 576, 0,                  448 * sizeof(float));
1789
+        ac->dsp.vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960,     swindow,     128);
1790
+    } else { // LONG_STOP or ONLY_LONG
1791
+        ac->dsp.vector_fmul_reverse(saved_ltp,       ac->buf_mdct + 512,     lwindow,     1024);
1792
+    }
1793
+
1794
+    memcpy(sce->ltp_state, &sce->ltp_state[1024], 1024 * sizeof(int16_t));
1795
+    ac->fmt_conv.float_to_int16(&(sce->ltp_state[1024]), sce->ret,  1024);
1796
+    ac->fmt_conv.float_to_int16(&(sce->ltp_state[2048]), saved_ltp, 1024);
1797
+}
1798
+
1799
+/**
1696 1800
  * Conduct IMDCT and windowing.
1697 1801
  */
1698 1802
 static void imdct_and_windowing(AACContext *ac, SingleChannelElement *sce)
... ...
@@ -1857,6 +1980,14 @@ static void spectral_to_sample(AACContext *ac)
1857 1857
             if (che) {
1858 1858
                 if (type <= TYPE_CPE)
1859 1859
                     apply_channel_coupling(ac, che, type, i, BEFORE_TNS, apply_dependent_coupling);
1860
+                if (ac->m4ac.object_type == AOT_AAC_LTP) {
1861
+                    if (che->ch[0].ics.predictor_present) {
1862
+                        if (che->ch[0].ics.ltp.present)
1863
+                            apply_ltp(ac, &che->ch[0]);
1864
+                        if (che->ch[1].ics.ltp.present && type == TYPE_CPE)
1865
+                            apply_ltp(ac, &che->ch[1]);
1866
+                    }
1867
+                }
1860 1868
                 if (che->ch[0].tns.present)
1861 1869
                     apply_tns(che->ch[0].coeffs, &che->ch[0].tns, &che->ch[0].ics, 1);
1862 1870
                 if (che->ch[1].tns.present)
... ...
@@ -1865,8 +1996,12 @@ static void spectral_to_sample(AACContext *ac)
1865 1865
                     apply_channel_coupling(ac, che, type, i, BETWEEN_TNS_AND_IMDCT, apply_dependent_coupling);
1866 1866
                 if (type != TYPE_CCE || che->coup.coupling_point == AFTER_IMDCT) {
1867 1867
                     imdct_and_windowing(ac, &che->ch[0]);
1868
+                    if (ac->m4ac.object_type == AOT_AAC_LTP)
1869
+                        update_ltp(ac, &che->ch[0]);
1868 1870
                     if (type == TYPE_CPE) {
1869 1871
                         imdct_and_windowing(ac, &che->ch[1]);
1872
+                        if (ac->m4ac.object_type == AOT_AAC_LTP)
1873
+                            update_ltp(ac, &che->ch[1]);
1870 1874
                     }
1871 1875
                     if (ac->m4ac.sbr > 0) {
1872 1876
                         ff_sbr_apply(ac, &che->sbr, type, che->ch[0].ret, che->ch[1].ret);
... ...
@@ -2080,6 +2215,7 @@ static av_cold int aac_decode_close(AVCodecContext *avctx)
2080 2080
 
2081 2081
     ff_mdct_end(&ac->mdct);
2082 2082
     ff_mdct_end(&ac->mdct_small);
2083
+    ff_mdct_end(&ac->mdct_ltp);
2083 2084
     return 0;
2084 2085
 }
2085 2086
 
... ...
@@ -35,6 +35,14 @@
35 35
 
36 36
 #include <stdint.h>
37 37
 
38
+/* @name ltp_coef
39
+ * Table of the LTP coefficient (multiplied by 2)
40
+ */
41
+static const float ltp_coef[8] = {
42
+     1.141658,    1.393232,    1.626008,    1.822608,
43
+     1.969800,    2.135788,    2.2389202,   2.739066,
44
+};
45
+
38 46
 /* @name tns_tmp2_map
39 47
  * Tables of the tmp2[] arrays of LPC coefficients used for TNS.
40 48
  * The suffix _M_N[] indicate the values of coef_compress and coef_res
... ...
@@ -57,7 +57,7 @@ enum AudioObjectType {
57 57
     AOT_AAC_MAIN,              ///< Y                       Main
58 58
     AOT_AAC_LC,                ///< Y                       Low Complexity
59 59
     AOT_AAC_SSR,               ///< N (code in SoC repo)    Scalable Sample Rate
60
-    AOT_AAC_LTP,               ///< N (code in SoC repo)    Long Term Prediction
60
+    AOT_AAC_LTP,               ///< Y                       Long Term Prediction
61 61
     AOT_SBR,                   ///< Y                       Spectral Band Replication
62 62
     AOT_AAC_SCALABLE,          ///< N                       Scalable
63 63
     AOT_TWINVQ,                ///< N                       Twin Vector Quantizer