xref: /btstack/3rd-party/lc3-google/src/ltpf.c (revision 6897da5c53aac5b1f90f41b5b15d0bd43d61dfff)
19a19cd78SMatthias Ringwald /******************************************************************************
29a19cd78SMatthias Ringwald  *
34930cef6SMatthias Ringwald  *  Copyright 2022 Google LLC
49a19cd78SMatthias Ringwald  *
59a19cd78SMatthias Ringwald  *  Licensed under the Apache License, Version 2.0 (the "License");
69a19cd78SMatthias Ringwald  *  you may not use this file except in compliance with the License.
79a19cd78SMatthias Ringwald  *  You may obtain a copy of the License at:
89a19cd78SMatthias Ringwald  *
99a19cd78SMatthias Ringwald  *  http://www.apache.org/licenses/LICENSE-2.0
109a19cd78SMatthias Ringwald  *
119a19cd78SMatthias Ringwald  *  Unless required by applicable law or agreed to in writing, software
129a19cd78SMatthias Ringwald  *  distributed under the License is distributed on an "AS IS" BASIS,
139a19cd78SMatthias Ringwald  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149a19cd78SMatthias Ringwald  *  See the License for the specific language governing permissions and
159a19cd78SMatthias Ringwald  *  limitations under the License.
169a19cd78SMatthias Ringwald  *
179a19cd78SMatthias Ringwald  ******************************************************************************/
189a19cd78SMatthias Ringwald 
199a19cd78SMatthias Ringwald #include "ltpf.h"
209a19cd78SMatthias Ringwald #include "tables.h"
219a19cd78SMatthias Ringwald 
224930cef6SMatthias Ringwald #include "ltpf_neon.h"
234930cef6SMatthias Ringwald #include "ltpf_arm.h"
244930cef6SMatthias Ringwald 
259a19cd78SMatthias Ringwald 
269a19cd78SMatthias Ringwald /* ----------------------------------------------------------------------------
279a19cd78SMatthias Ringwald  *  Resampling
289a19cd78SMatthias Ringwald  * -------------------------------------------------------------------------- */
299a19cd78SMatthias Ringwald 
309a19cd78SMatthias Ringwald /**
314930cef6SMatthias Ringwald  * Resampling coefficients
324930cef6SMatthias Ringwald  * The coefficients, in fixed Q15, are reordered by phase for each source
334930cef6SMatthias Ringwald  * samplerate (coefficient matrix transposed)
344930cef6SMatthias Ringwald  */
354930cef6SMatthias Ringwald 
364930cef6SMatthias Ringwald #ifndef resample_8k_12k8
374930cef6SMatthias Ringwald static const int16_t h_8k_12k8_q15[8*10] = {
384930cef6SMatthias Ringwald       214,   417, -1052, -4529, 26233, -4529, -1052,   417,   214,     0,
394930cef6SMatthias Ringwald       180,     0, -1522, -2427, 24506, -5289,     0,   763,   156,   -28,
404930cef6SMatthias Ringwald        92,  -323, -1361,     0, 19741, -3885,  1317,   861,     0,   -61,
414930cef6SMatthias Ringwald         0,  -457,  -752,  1873, 13068,     0,  2389,   598,  -213,   -79,
424930cef6SMatthias Ringwald       -61,  -398,     0,  2686,  5997,  5997,  2686,     0,  -398,   -61,
434930cef6SMatthias Ringwald       -79,  -213,   598,  2389,     0, 13068,  1873,  -752,  -457,     0,
444930cef6SMatthias Ringwald       -61,     0,   861,  1317, -3885, 19741,     0, -1361,  -323,    92,
454930cef6SMatthias Ringwald       -28,   156,   763,     0, -5289, 24506, -2427, -1522,     0,   180,
464930cef6SMatthias Ringwald };
474930cef6SMatthias Ringwald #endif /* resample_8k_12k8 */
484930cef6SMatthias Ringwald 
494930cef6SMatthias Ringwald #ifndef resample_16k_12k8
504930cef6SMatthias Ringwald static const int16_t h_16k_12k8_q15[4*20] = {
514930cef6SMatthias Ringwald       -61,   214,  -398,   417,     0, -1052,  2686, -4529,  5997, 26233,
524930cef6SMatthias Ringwald      5997, -4529,  2686, -1052,     0,   417,  -398,   214,   -61,     0,
534930cef6SMatthias Ringwald 
544930cef6SMatthias Ringwald       -79,   180,  -213,     0,   598, -1522,  2389, -2427,     0, 24506,
554930cef6SMatthias Ringwald     13068, -5289,  1873,     0,  -752,   763,  -457,   156,     0,   -28,
564930cef6SMatthias Ringwald 
574930cef6SMatthias Ringwald       -61,    92,     0,  -323,   861, -1361,  1317,     0, -3885, 19741,
584930cef6SMatthias Ringwald     19741, -3885,     0,  1317, -1361,   861,  -323,     0,    92,   -61,
594930cef6SMatthias Ringwald 
604930cef6SMatthias Ringwald       -28,     0,   156,  -457,   763,  -752,     0,  1873, -5289, 13068,
614930cef6SMatthias Ringwald     24506,     0, -2427,  2389, -1522,   598,     0,  -213,   180,   -79,
624930cef6SMatthias Ringwald };
634930cef6SMatthias Ringwald #endif /* resample_16k_12k8 */
644930cef6SMatthias Ringwald 
654930cef6SMatthias Ringwald #ifndef resample_32k_12k8
664930cef6SMatthias Ringwald static const int16_t h_32k_12k8_q15[2*40] = {
674930cef6SMatthias Ringwald       -30,   -31,    46,   107,     0,  -199,  -162,   209,   430,     0,
684930cef6SMatthias Ringwald      -681,  -526,   658,  1343,     0, -2264, -1943,  2999,  9871, 13116,
694930cef6SMatthias Ringwald      9871,  2999, -1943, -2264,     0,  1343,   658,  -526,  -681,     0,
704930cef6SMatthias Ringwald       430,   209,  -162,  -199,     0,   107,    46,   -31,   -30,     0,
714930cef6SMatthias Ringwald 
724930cef6SMatthias Ringwald       -14,   -39,     0,    90,    78,  -106,  -229,     0,   382,   299,
734930cef6SMatthias Ringwald      -376,  -761,     0,  1194,   937, -1214, -2644,     0,  6534, 12253,
744930cef6SMatthias Ringwald     12253,  6534,     0, -2644, -1214,   937,  1194,     0,  -761,  -376,
754930cef6SMatthias Ringwald       299,   382,     0,  -229,  -106,    78,    90,     0,   -39,   -14,
764930cef6SMatthias Ringwald };
774930cef6SMatthias Ringwald #endif /* resample_32k_12k8 */
784930cef6SMatthias Ringwald 
794930cef6SMatthias Ringwald #ifndef resample_24k_12k8
804930cef6SMatthias Ringwald static const int16_t h_24k_12k8_q15[8*30] = {
814930cef6SMatthias Ringwald       -50,    19,   143,   -93,  -290,   278,   485,  -658,  -701,  1396,
824930cef6SMatthias Ringwald       901, -3019, -1042, 10276, 17488, 10276, -1042, -3019,   901,  1396,
834930cef6SMatthias Ringwald      -701,  -658,   485,   278,  -290,   -93,   143,    19,   -50,     0,
844930cef6SMatthias Ringwald 
854930cef6SMatthias Ringwald       -46,     0,   141,   -45,  -305,   185,   543,  -501,  -854,  1153,
864930cef6SMatthias Ringwald      1249, -2619, -1908,  8712, 17358, 11772,     0, -3319,   480,  1593,
874930cef6SMatthias Ringwald      -504,  -796,   399,   367,  -261,  -142,   138,    40,   -52,    -5,
884930cef6SMatthias Ringwald 
894930cef6SMatthias Ringwald       -41,   -17,   133,     0,  -304,    91,   574,  -334,  -959,   878,
904930cef6SMatthias Ringwald      1516, -2143, -2590,  7118, 16971, 13161,  1202, -3495,     0,  1731,
914930cef6SMatthias Ringwald      -267,  -908,   287,   445,  -215,  -188,   125,    62,   -52,   -12,
924930cef6SMatthias Ringwald 
934930cef6SMatthias Ringwald       -34,   -30,   120,    41,  -291,     0,   577,  -164, -1015,   585,
944930cef6SMatthias Ringwald      1697, -1618, -3084,  5534, 16337, 14406,  2544, -3526,  -523,  1800,
954930cef6SMatthias Ringwald         0,  -985,   152,   509,  -156,  -230,   104,    83,   -48,   -19,
964930cef6SMatthias Ringwald 
974930cef6SMatthias Ringwald       -26,   -41,   103,    76,  -265,   -83,   554,     0, -1023,   288,
984930cef6SMatthias Ringwald      1791, -1070, -3393,  3998, 15474, 15474,  3998, -3393, -1070,  1791,
994930cef6SMatthias Ringwald       288, -1023,     0,   554,   -83,  -265,    76,   103,   -41,   -26,
1004930cef6SMatthias Ringwald 
1014930cef6SMatthias Ringwald       -19,   -48,    83,   104,  -230,  -156,   509,   152,  -985,     0,
1024930cef6SMatthias Ringwald      1800,  -523, -3526,  2544, 14406, 16337,  5534, -3084, -1618,  1697,
1034930cef6SMatthias Ringwald       585, -1015,  -164,   577,     0,  -291,    41,   120,   -30,   -34,
1044930cef6SMatthias Ringwald 
1054930cef6SMatthias Ringwald       -12,   -52,    62,   125,  -188,  -215,   445,   287,  -908,  -267,
1064930cef6SMatthias Ringwald      1731,     0, -3495,  1202, 13161, 16971,  7118, -2590, -2143,  1516,
1074930cef6SMatthias Ringwald       878,  -959,  -334,   574,    91,  -304,     0,   133,   -17,   -41,
1084930cef6SMatthias Ringwald 
1094930cef6SMatthias Ringwald        -5,   -52,    40,   138,  -142,  -261,   367,   399,  -796,  -504,
1104930cef6SMatthias Ringwald      1593,   480, -3319,     0, 11772, 17358,  8712, -1908, -2619,  1249,
1114930cef6SMatthias Ringwald      1153,  -854,  -501,   543,   185,  -305,   -45,   141,     0,   -46,
1124930cef6SMatthias Ringwald };
1134930cef6SMatthias Ringwald #endif /* resample_24k_12k8 */
1144930cef6SMatthias Ringwald 
1154930cef6SMatthias Ringwald #ifndef resample_48k_12k8
1164930cef6SMatthias Ringwald static const int16_t h_48k_12k8_q15[4*60] = {
1174930cef6SMatthias Ringwald       -13,   -25,   -20,    10,    51,    71,    38,   -47,  -133,  -145,
1184930cef6SMatthias Ringwald       -42,   139,   277,   242,     0,  -329,  -511,  -351,   144,   698,
1194930cef6SMatthias Ringwald       895,   450,  -535, -1510, -1697,  -521,  1999,  5138,  7737,  8744,
1204930cef6SMatthias Ringwald      7737,  5138,  1999,  -521, -1697, -1510,  -535,   450,   895,   698,
1214930cef6SMatthias Ringwald       144,  -351,  -511,  -329,     0,   242,   277,   139,   -42,  -145,
1224930cef6SMatthias Ringwald      -133,   -47,    38,    71,    51,    10,   -20,   -25,   -13,     0,
1234930cef6SMatthias Ringwald 
1244930cef6SMatthias Ringwald        -9,   -23,   -24,     0,    41,    71,    52,   -23,  -115,  -152,
1254930cef6SMatthias Ringwald       -78,    92,   254,   272,    76,  -251,  -493,  -427,     0,   576,
1264930cef6SMatthias Ringwald       900,   624,  -262, -1309, -1763,  -954,  1272,  4356,  7203,  8679,
1274930cef6SMatthias Ringwald      8169,  5886,  2767,     0, -1542, -1660,  -809,   240,   848,   796,
1284930cef6SMatthias Ringwald       292,  -252,  -507,  -398,   -82,   199,   288,   183,     0,  -130,
1294930cef6SMatthias Ringwald      -145,   -71,    20,    69,    60,    20,   -15,   -26,   -17,    -3,
1304930cef6SMatthias Ringwald 
1314930cef6SMatthias Ringwald        -6,   -20,   -26,    -8,    31,    67,    62,     0,   -94,  -152,
1324930cef6SMatthias Ringwald      -108,    45,   223,   287,   143,  -167,  -454,  -480,  -134,   439,
1334930cef6SMatthias Ringwald       866,   758,     0, -1071, -1748, -1295,   601,  3559,  6580,  8485,
1344930cef6SMatthias Ringwald      8485,  6580,  3559,   601, -1295, -1748, -1071,     0,   758,   866,
1354930cef6SMatthias Ringwald       439,  -134,  -480,  -454,  -167,   143,   287,   223,    45,  -108,
1364930cef6SMatthias Ringwald      -152,   -94,     0,    62,    67,    31,    -8,   -26,   -20,    -6,
1374930cef6SMatthias Ringwald 
1384930cef6SMatthias Ringwald        -3,   -17,   -26,   -15,    20,    60,    69,    20,   -71,  -145,
1394930cef6SMatthias Ringwald      -130,     0,   183,   288,   199,   -82,  -398,  -507,  -252,   292,
1404930cef6SMatthias Ringwald       796,   848,   240,  -809, -1660, -1542,     0,  2767,  5886,  8169,
1414930cef6SMatthias Ringwald      8679,  7203,  4356,  1272,  -954, -1763, -1309,  -262,   624,   900,
1424930cef6SMatthias Ringwald       576,     0,  -427,  -493,  -251,    76,   272,   254,    92,   -78,
1434930cef6SMatthias Ringwald      -152,  -115,   -23,    52,    71,    41,     0,   -24,   -23,    -9,
1444930cef6SMatthias Ringwald };
1454930cef6SMatthias Ringwald #endif /* resample_48k_12k8 */
1464930cef6SMatthias Ringwald 
147*6897da5cSDirk Helbig #ifndef resample_96k_12k8
148*6897da5cSDirk Helbig static const int16_t h_96k_12k8_q15[2*120] = {
149*6897da5cSDirk Helbig        -3,    -7,   -10,   -13,   -13,   -10,    -4,     5,    15,    26,
150*6897da5cSDirk Helbig        33,    36,    31,    19,     0,   -23,   -47,   -66,   -76,   -73,
151*6897da5cSDirk Helbig       -54,   -21,    23,    70,   111,   139,   143,   121,    72,     0,
152*6897da5cSDirk Helbig       -84,  -165,  -227,  -256,  -240,  -175,   -67,    72,   219,   349,
153*6897da5cSDirk Helbig       433,   448,   379,   225,     0,  -268,  -536,  -755,  -874,  -848,
154*6897da5cSDirk Helbig      -648,  -260,   301,  1000,  1780,  2569,  3290,  3869,  4243,  4372,
155*6897da5cSDirk Helbig      4243,  3869,  3290,  2569,  1780,  1000,   301,  -260,  -648,  -848,
156*6897da5cSDirk Helbig      -874,  -755,  -536,  -268,     0,   225,   379,   448,   433,   349,
157*6897da5cSDirk Helbig       219,    72,   -67,  -175,  -240,  -256,  -227,  -165,   -84,     0,
158*6897da5cSDirk Helbig        72,   121,   143,   139,   111,    70,    23,   -21,   -54,   -73,
159*6897da5cSDirk Helbig       -76,   -66,   -47,   -23,     0,    19,    31,    36,    33,    26,
160*6897da5cSDirk Helbig        15,     5,    -4,   -10,   -13,   -13,   -10,    -7,    -3,     0,
161*6897da5cSDirk Helbig 
162*6897da5cSDirk Helbig        -1,    -5,    -8,   -12,   -13,   -12,    -8,     0,    10,    21,
163*6897da5cSDirk Helbig        30,    35,    34,    26,    10,   -11,   -35,   -58,   -73,   -76,
164*6897da5cSDirk Helbig       -65,   -39,     0,    46,    92,   127,   144,   136,   100,    38,
165*6897da5cSDirk Helbig       -41,  -125,  -199,  -246,  -254,  -214,  -126,     0,   146,   288,
166*6897da5cSDirk Helbig       398,   450,   424,   312,   120,  -131,  -405,  -655,  -830,  -881,
167*6897da5cSDirk Helbig      -771,  -477,     0,   636,  1384,  2178,  2943,  3601,  4084,  4340,
168*6897da5cSDirk Helbig      4340,  4084,  3601,  2943,  2178,  1384,   636,     0,  -477,  -771,
169*6897da5cSDirk Helbig      -881,  -830,  -655,  -405,  -131,   120,   312,   424,   450,   398,
170*6897da5cSDirk Helbig       288,   146,     0,  -126,  -214,  -254,  -246,  -199,  -125,   -41,
171*6897da5cSDirk Helbig        38,   100,   136,   144,   127,    92,    46,     0,   -39,   -65,
172*6897da5cSDirk Helbig       -76,   -73,   -58,   -35,   -11,    10,    26,    34,    35,    30,
173*6897da5cSDirk Helbig        21,    10,     0,    -8,   -12,   -13,   -12,    -8,    -5,    -1,
174*6897da5cSDirk Helbig };
175*6897da5cSDirk Helbig #endif /* resample_96k_12k8 */
176*6897da5cSDirk Helbig 
1774930cef6SMatthias Ringwald 
1784930cef6SMatthias Ringwald /**
1794930cef6SMatthias Ringwald  * High-pass 50Hz filtering, at 12.8 KHz samplerate
1804930cef6SMatthias Ringwald  * hp50            Biquad filter state
1814930cef6SMatthias Ringwald  * xn              Input sample, in fixed Q30
1824930cef6SMatthias Ringwald  * return          Filtered sample, in fixed Q30
1834930cef6SMatthias Ringwald  */
filter_hp50(struct lc3_ltpf_hp50_state * hp50,int32_t xn)1844930cef6SMatthias Ringwald LC3_HOT static inline int32_t filter_hp50(
1854930cef6SMatthias Ringwald     struct lc3_ltpf_hp50_state *hp50, int32_t xn)
1864930cef6SMatthias Ringwald {
1874930cef6SMatthias Ringwald     int32_t yn;
1884930cef6SMatthias Ringwald 
1894930cef6SMatthias Ringwald     const int32_t a1 = -2110217691, a2 = 1037111617;
1904930cef6SMatthias Ringwald     const int32_t b1 = -2110535566, b2 = 1055267782;
1914930cef6SMatthias Ringwald 
1924930cef6SMatthias Ringwald     yn       = (hp50->s1 + (int64_t)xn * b2) >> 30;
1934930cef6SMatthias Ringwald     hp50->s1 = (hp50->s2 + (int64_t)xn * b1 - (int64_t)yn * a1);
1944930cef6SMatthias Ringwald     hp50->s2 = (           (int64_t)xn * b2 - (int64_t)yn * a2);
1954930cef6SMatthias Ringwald 
1964930cef6SMatthias Ringwald     return yn;
1974930cef6SMatthias Ringwald }
1984930cef6SMatthias Ringwald 
1994930cef6SMatthias Ringwald /**
2004930cef6SMatthias Ringwald  * Resample from 8 / 16 / 32 KHz to 12.8 KHz Template
2014930cef6SMatthias Ringwald  * p               Resampling factor with compared to 192 KHz (8, 4 or 2)
2024930cef6SMatthias Ringwald  * h               Arrange by phase coefficients table
2034930cef6SMatthias Ringwald  * hp50            High-Pass biquad filter state
2044930cef6SMatthias Ringwald  * x               [-d..-1] Previous, [0..ns-1] Current samples, Q15
2054930cef6SMatthias Ringwald  * y, n            [0..n-1] Output `n` processed samples, Q14
2064930cef6SMatthias Ringwald  *
2074930cef6SMatthias Ringwald  * The `x` vector is aligned on 32 bits
2084930cef6SMatthias Ringwald  * The number of previous samples `d` accessed on `x` is :
2094930cef6SMatthias Ringwald  *   d: { 10, 20, 40 } - 1 for resampling factors 8, 4 and 2.
2104930cef6SMatthias Ringwald  */
2114930cef6SMatthias Ringwald #if !defined(resample_8k_12k8) || !defined(resample_16k_12k8) \
2124930cef6SMatthias Ringwald     || !defined(resample_32k_12k8)
resample_x64k_12k8(const int p,const int16_t * h,struct lc3_ltpf_hp50_state * hp50,const int16_t * x,int16_t * y,int n)2134930cef6SMatthias Ringwald LC3_HOT static inline void resample_x64k_12k8(const int p, const int16_t *h,
2144930cef6SMatthias Ringwald     struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
2154930cef6SMatthias Ringwald {
2164930cef6SMatthias Ringwald     const int w = 2*(40 / p);
2174930cef6SMatthias Ringwald 
2184930cef6SMatthias Ringwald     x -= w - 1;
2194930cef6SMatthias Ringwald 
2204930cef6SMatthias Ringwald     for (int i = 0; i < 5*n; i += 5) {
2214930cef6SMatthias Ringwald         const int16_t *hn = h + (i % p) * w;
2224930cef6SMatthias Ringwald         const int16_t *xn = x + (i / p);
2234930cef6SMatthias Ringwald         int32_t un = 0;
2244930cef6SMatthias Ringwald 
2254930cef6SMatthias Ringwald         for (int k = 0; k < w; k += 10) {
2264930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2274930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2284930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2294930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2304930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2314930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2324930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2334930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2344930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2354930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2364930cef6SMatthias Ringwald         }
2374930cef6SMatthias Ringwald 
2384930cef6SMatthias Ringwald         int32_t yn = filter_hp50(hp50, un);
2394930cef6SMatthias Ringwald         *(y++) = (yn + (1 << 15)) >> 16;
2404930cef6SMatthias Ringwald     }
2414930cef6SMatthias Ringwald }
2424930cef6SMatthias Ringwald #endif
2434930cef6SMatthias Ringwald 
2444930cef6SMatthias Ringwald /**
2454930cef6SMatthias Ringwald  * Resample from 24 / 48 KHz to 12.8 KHz Template
2464930cef6SMatthias Ringwald  * p               Resampling factor with compared to 192 KHz (8 or 4)
2474930cef6SMatthias Ringwald  * h               Arrange by phase coefficients table
2484930cef6SMatthias Ringwald  * hp50            High-Pass biquad filter state
2494930cef6SMatthias Ringwald  * x               [-d..-1] Previous, [0..ns-1] Current samples, Q15
2504930cef6SMatthias Ringwald  * y, n            [0..n-1] Output `n` processed samples, Q14
2514930cef6SMatthias Ringwald  *
2524930cef6SMatthias Ringwald  * The `x` vector is aligned on 32 bits
2534930cef6SMatthias Ringwald  * The number of previous samples `d` accessed on `x` is :
2544930cef6SMatthias Ringwald  *   d: { 30, 60 } - 1 for resampling factors 8 and 4.
2554930cef6SMatthias Ringwald  */
256*6897da5cSDirk Helbig #if !defined(resample_24k_12k8) || !defined(resample_48k_12k8) \
257*6897da5cSDirk Helbig     || !defined(resample_96k_12k8)
resample_x192k_12k8(const int p,const int16_t * h,struct lc3_ltpf_hp50_state * hp50,const int16_t * x,int16_t * y,int n)2584930cef6SMatthias Ringwald LC3_HOT static inline void resample_x192k_12k8(const int p, const int16_t *h,
2594930cef6SMatthias Ringwald     struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
2604930cef6SMatthias Ringwald {
2614930cef6SMatthias Ringwald     const int w = 2*(120 / p);
2624930cef6SMatthias Ringwald 
2634930cef6SMatthias Ringwald     x -= w - 1;
2644930cef6SMatthias Ringwald 
2654930cef6SMatthias Ringwald     for (int i = 0; i < 15*n; i += 15) {
2664930cef6SMatthias Ringwald         const int16_t *hn = h + (i % p) * w;
2674930cef6SMatthias Ringwald         const int16_t *xn = x + (i / p);
2684930cef6SMatthias Ringwald         int32_t un = 0;
2694930cef6SMatthias Ringwald 
2704930cef6SMatthias Ringwald         for (int k = 0; k < w; k += 15) {
2714930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2724930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2734930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2744930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2754930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2764930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2774930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2784930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2794930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2804930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2814930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2824930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2834930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2844930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2854930cef6SMatthias Ringwald             un += *(xn++) * *(hn++);
2864930cef6SMatthias Ringwald         }
2874930cef6SMatthias Ringwald 
2884930cef6SMatthias Ringwald         int32_t yn = filter_hp50(hp50, un);
2894930cef6SMatthias Ringwald         *(y++) = (yn + (1 << 15)) >> 16;
2904930cef6SMatthias Ringwald     }
2914930cef6SMatthias Ringwald }
2924930cef6SMatthias Ringwald #endif
2934930cef6SMatthias Ringwald 
2944930cef6SMatthias Ringwald /**
2954930cef6SMatthias Ringwald  * Resample from 8 Khz to 12.8 KHz
2964930cef6SMatthias Ringwald  * hp50            High-Pass biquad filter state
2974930cef6SMatthias Ringwald  * x               [-10..-1] Previous, [0..ns-1] Current samples, Q15
2984930cef6SMatthias Ringwald  * y, n            [0..n-1] Output `n` processed samples, Q14
2994930cef6SMatthias Ringwald  *
3004930cef6SMatthias Ringwald  * The `x` vector is aligned on 32 bits
3014930cef6SMatthias Ringwald  */
3024930cef6SMatthias Ringwald #ifndef resample_8k_12k8
resample_8k_12k8(struct lc3_ltpf_hp50_state * hp50,const int16_t * x,int16_t * y,int n)3034930cef6SMatthias Ringwald LC3_HOT static void resample_8k_12k8(
3044930cef6SMatthias Ringwald     struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
3054930cef6SMatthias Ringwald {
3064930cef6SMatthias Ringwald     resample_x64k_12k8(8, h_8k_12k8_q15, hp50, x, y, n);
3074930cef6SMatthias Ringwald }
3084930cef6SMatthias Ringwald #endif /* resample_8k_12k8 */
3094930cef6SMatthias Ringwald 
3104930cef6SMatthias Ringwald /**
3114930cef6SMatthias Ringwald  * Resample from 16 Khz to 12.8 KHz
3124930cef6SMatthias Ringwald  * hp50            High-Pass biquad filter state
3134930cef6SMatthias Ringwald  * x               [-20..-1] Previous, [0..ns-1] Current samples, in fixed Q15
3144930cef6SMatthias Ringwald  * y, n            [0..n-1] Output `n` processed samples, in fixed Q14
3154930cef6SMatthias Ringwald  *
3164930cef6SMatthias Ringwald  * The `x` vector is aligned on 32 bits
3174930cef6SMatthias Ringwald  */
3184930cef6SMatthias Ringwald #ifndef resample_16k_12k8
resample_16k_12k8(struct lc3_ltpf_hp50_state * hp50,const int16_t * x,int16_t * y,int n)3194930cef6SMatthias Ringwald LC3_HOT static void resample_16k_12k8(
3204930cef6SMatthias Ringwald     struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
3214930cef6SMatthias Ringwald {
3224930cef6SMatthias Ringwald     resample_x64k_12k8(4, h_16k_12k8_q15, hp50, x, y, n);
3234930cef6SMatthias Ringwald }
3244930cef6SMatthias Ringwald #endif /* resample_16k_12k8 */
3254930cef6SMatthias Ringwald 
3264930cef6SMatthias Ringwald /**
3274930cef6SMatthias Ringwald  * Resample from 32 Khz to 12.8 KHz
3284930cef6SMatthias Ringwald  * hp50            High-Pass biquad filter state
3294930cef6SMatthias Ringwald  * x               [-30..-1] Previous, [0..ns-1] Current samples, in fixed Q15
3304930cef6SMatthias Ringwald  * y, n            [0..n-1] Output `n` processed samples, in fixed Q14
3314930cef6SMatthias Ringwald  *
3324930cef6SMatthias Ringwald  * The `x` vector is aligned on 32 bits
3334930cef6SMatthias Ringwald  */
3344930cef6SMatthias Ringwald #ifndef resample_32k_12k8
resample_32k_12k8(struct lc3_ltpf_hp50_state * hp50,const int16_t * x,int16_t * y,int n)3354930cef6SMatthias Ringwald LC3_HOT static void resample_32k_12k8(
3364930cef6SMatthias Ringwald     struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
3374930cef6SMatthias Ringwald {
3384930cef6SMatthias Ringwald     resample_x64k_12k8(2, h_32k_12k8_q15, hp50, x, y, n);
3394930cef6SMatthias Ringwald }
3404930cef6SMatthias Ringwald #endif /* resample_32k_12k8 */
3414930cef6SMatthias Ringwald 
3424930cef6SMatthias Ringwald /**
3434930cef6SMatthias Ringwald  * Resample from 24 Khz to 12.8 KHz
3444930cef6SMatthias Ringwald  * hp50            High-Pass biquad filter state
3454930cef6SMatthias Ringwald  * x               [-30..-1] Previous, [0..ns-1] Current samples, in fixed Q15
3464930cef6SMatthias Ringwald  * y, n            [0..n-1] Output `n` processed samples, in fixed Q14
3474930cef6SMatthias Ringwald  *
3484930cef6SMatthias Ringwald  * The `x` vector is aligned on 32 bits
3494930cef6SMatthias Ringwald  */
3504930cef6SMatthias Ringwald #ifndef resample_24k_12k8
resample_24k_12k8(struct lc3_ltpf_hp50_state * hp50,const int16_t * x,int16_t * y,int n)3514930cef6SMatthias Ringwald LC3_HOT static void resample_24k_12k8(
3524930cef6SMatthias Ringwald     struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
3534930cef6SMatthias Ringwald {
3544930cef6SMatthias Ringwald     resample_x192k_12k8(8, h_24k_12k8_q15, hp50, x, y, n);
3554930cef6SMatthias Ringwald }
3564930cef6SMatthias Ringwald #endif /* resample_24k_12k8 */
3574930cef6SMatthias Ringwald 
3584930cef6SMatthias Ringwald /**
3594930cef6SMatthias Ringwald  * Resample from 48 Khz to 12.8 KHz
3604930cef6SMatthias Ringwald  * hp50            High-Pass biquad filter state
3614930cef6SMatthias Ringwald  * x               [-60..-1] Previous, [0..ns-1] Current samples, in fixed Q15
3624930cef6SMatthias Ringwald  * y, n            [0..n-1] Output `n` processed samples, in fixed Q14
3634930cef6SMatthias Ringwald  *
3644930cef6SMatthias Ringwald * The `x` vector is aligned on 32 bits
3654930cef6SMatthias Ringwald */
3664930cef6SMatthias Ringwald #ifndef resample_48k_12k8
resample_48k_12k8(struct lc3_ltpf_hp50_state * hp50,const int16_t * x,int16_t * y,int n)3674930cef6SMatthias Ringwald LC3_HOT static void resample_48k_12k8(
3684930cef6SMatthias Ringwald     struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
3694930cef6SMatthias Ringwald {
3704930cef6SMatthias Ringwald     resample_x192k_12k8(4, h_48k_12k8_q15, hp50, x, y, n);
3714930cef6SMatthias Ringwald }
3724930cef6SMatthias Ringwald #endif /* resample_48k_12k8 */
3734930cef6SMatthias Ringwald 
3744930cef6SMatthias Ringwald /**
375*6897da5cSDirk Helbig  * Resample from 96 Khz to 12.8 KHz
376*6897da5cSDirk Helbig  * hp50            High-Pass biquad filter state
377*6897da5cSDirk Helbig  * x               [-120..-1] Previous, [0..ns-1] Current samples, in fixed Q15
378*6897da5cSDirk Helbig  * y, n            [0..n-1] Output `n` processed samples, in fixed Q14
379*6897da5cSDirk Helbig  *
380*6897da5cSDirk Helbig * The `x` vector is aligned on 32 bits
381*6897da5cSDirk Helbig */
382*6897da5cSDirk Helbig #ifndef resample_96k_12k8
resample_96k_12k8(struct lc3_ltpf_hp50_state * hp50,const int16_t * x,int16_t * y,int n)383*6897da5cSDirk Helbig LC3_HOT static void resample_96k_12k8(
384*6897da5cSDirk Helbig     struct lc3_ltpf_hp50_state *hp50, const int16_t *x, int16_t *y, int n)
385*6897da5cSDirk Helbig {
386*6897da5cSDirk Helbig     resample_x192k_12k8(2, h_96k_12k8_q15, hp50, x, y, n);
387*6897da5cSDirk Helbig }
388*6897da5cSDirk Helbig #endif /* resample_96k_12k8 */
389*6897da5cSDirk Helbig 
390*6897da5cSDirk Helbig /**
3914930cef6SMatthias Ringwald * Resample to 6.4 KHz
3924930cef6SMatthias Ringwald * x               [-3..-1] Previous, [0..n-1] Current samples
3939a19cd78SMatthias Ringwald * y, n            [0..n-1] Output `n` processed samples
3949a19cd78SMatthias Ringwald *
3954930cef6SMatthias Ringwald * The `x` vector is aligned on 32 bits
3969a19cd78SMatthias Ringwald  */
3974930cef6SMatthias Ringwald #ifndef resample_6k4
resample_6k4(const int16_t * x,int16_t * y,int n)3984930cef6SMatthias Ringwald LC3_HOT static void resample_6k4(const int16_t *x, int16_t *y, int n)
3999a19cd78SMatthias Ringwald {
4004930cef6SMatthias Ringwald     static const int16_t h[] = { 18477, 15424, 8105 };
4014930cef6SMatthias Ringwald     const int16_t *ye = y + n;
4029a19cd78SMatthias Ringwald 
4034930cef6SMatthias Ringwald     for (x--; y < ye; x += 2)
4044930cef6SMatthias Ringwald         *(y++) = (x[0] * h[0] + (x[-1] + x[1]) * h[1]
4054930cef6SMatthias Ringwald                               + (x[-2] + x[2]) * h[2]) >> 16;
4069a19cd78SMatthias Ringwald }
4074930cef6SMatthias Ringwald #endif /* resample_6k4 */
4089a19cd78SMatthias Ringwald 
4099a19cd78SMatthias Ringwald /**
4109a19cd78SMatthias Ringwald  * LTPF Resample to 12.8 KHz implementations for each samplerates
4119a19cd78SMatthias Ringwald  */
4129a19cd78SMatthias Ringwald 
4139a19cd78SMatthias Ringwald static void (* const resample_12k8[])
4144930cef6SMatthias Ringwald     (struct lc3_ltpf_hp50_state *, const int16_t *, int16_t *, int ) =
4159a19cd78SMatthias Ringwald {
4169a19cd78SMatthias Ringwald     [LC3_SRATE_8K    ] = resample_8k_12k8,
4179a19cd78SMatthias Ringwald     [LC3_SRATE_16K   ] = resample_16k_12k8,
4189a19cd78SMatthias Ringwald     [LC3_SRATE_24K   ] = resample_24k_12k8,
4199a19cd78SMatthias Ringwald     [LC3_SRATE_32K   ] = resample_32k_12k8,
4209a19cd78SMatthias Ringwald     [LC3_SRATE_48K   ] = resample_48k_12k8,
421*6897da5cSDirk Helbig     [LC3_SRATE_48K_HR] = resample_48k_12k8,
422*6897da5cSDirk Helbig     [LC3_SRATE_96K_HR] = resample_96k_12k8,
4239a19cd78SMatthias Ringwald };
4249a19cd78SMatthias Ringwald 
4259a19cd78SMatthias Ringwald 
4269a19cd78SMatthias Ringwald /* ----------------------------------------------------------------------------
4279a19cd78SMatthias Ringwald  *  Analysis
4289a19cd78SMatthias Ringwald  * -------------------------------------------------------------------------- */
4299a19cd78SMatthias Ringwald 
4309a19cd78SMatthias Ringwald /**
4319a19cd78SMatthias Ringwald  * Return dot product of 2 vectors
4324930cef6SMatthias Ringwald  * a, b, n         The 2 vectors of size `n` (> 0 and <= 128)
4339a19cd78SMatthias Ringwald  * return          sum( a[i] * b[i] ), i = [0..n-1]
4344930cef6SMatthias Ringwald  *
4354930cef6SMatthias Ringwald  * The size `n` of vectors must be multiple of 16, and less or equal to 128
4369a19cd78SMatthias Ringwald */
4374930cef6SMatthias Ringwald #ifndef dot
dot(const int16_t * a,const int16_t * b,int n)4384930cef6SMatthias Ringwald LC3_HOT static inline float dot(const int16_t *a, const int16_t *b, int n)
4399a19cd78SMatthias Ringwald {
4404930cef6SMatthias Ringwald     int64_t v = 0;
4419a19cd78SMatthias Ringwald 
4424930cef6SMatthias Ringwald     for (int i = 0; i < (n >> 4); i++)
4434930cef6SMatthias Ringwald         for (int j = 0; j < 16; j++)
4449a19cd78SMatthias Ringwald             v += *(a++) * *(b++);
4459a19cd78SMatthias Ringwald 
4464930cef6SMatthias Ringwald     int32_t v32 = (v + (1 << 5)) >> 6;
4474930cef6SMatthias Ringwald     return (float)v32;
4489a19cd78SMatthias Ringwald }
4494930cef6SMatthias Ringwald #endif /* dot */
4509a19cd78SMatthias Ringwald 
4519a19cd78SMatthias Ringwald /**
4529a19cd78SMatthias Ringwald  * Return vector of correlations
4534930cef6SMatthias Ringwald  * a, b, n         The 2 vector of size `n` (> 0 and <= 128)
4549a19cd78SMatthias Ringwald  * y, nc           Output the correlation vector of size `nc`
4559a19cd78SMatthias Ringwald  *
4564930cef6SMatthias Ringwald  * The first vector `a` is aligned of 32 bits
4574930cef6SMatthias Ringwald  * The size `n` of vectors is multiple of 16, and less or equal to 128
4589a19cd78SMatthias Ringwald  */
4594930cef6SMatthias Ringwald #ifndef correlate
correlate(const int16_t * a,const int16_t * b,int n,float * y,int nc)4604930cef6SMatthias Ringwald LC3_HOT static void correlate(
4614930cef6SMatthias Ringwald     const int16_t *a, const int16_t *b, int n, float *y, int nc)
4629a19cd78SMatthias Ringwald {
4639a19cd78SMatthias Ringwald     for (const float *ye = y + nc; y < ye; )
4649a19cd78SMatthias Ringwald         *(y++) = dot(a, b--, n);
4659a19cd78SMatthias Ringwald }
4664930cef6SMatthias Ringwald #endif /* correlate */
4679a19cd78SMatthias Ringwald 
4689a19cd78SMatthias Ringwald /**
4699a19cd78SMatthias Ringwald  * Search the maximum value and returns its argument
4709a19cd78SMatthias Ringwald  * x, n            The input vector of size `n`
4719a19cd78SMatthias Ringwald  * x_max           Return the maximum value
4729a19cd78SMatthias Ringwald  * return          Return the argument of the maximum
4739a19cd78SMatthias Ringwald  */
argmax(const float * x,int n,float * x_max)4744930cef6SMatthias Ringwald LC3_HOT static int argmax(const float *x, int n, float *x_max)
4759a19cd78SMatthias Ringwald {
4769a19cd78SMatthias Ringwald     int arg = 0;
4779a19cd78SMatthias Ringwald 
4789a19cd78SMatthias Ringwald     *x_max = x[arg = 0];
4799a19cd78SMatthias Ringwald     for (int i = 1; i < n; i++)
4809a19cd78SMatthias Ringwald         if (*x_max < x[i])
4819a19cd78SMatthias Ringwald             *x_max = x[arg = i];
4829a19cd78SMatthias Ringwald 
4839a19cd78SMatthias Ringwald     return arg;
4849a19cd78SMatthias Ringwald }
4859a19cd78SMatthias Ringwald 
4869a19cd78SMatthias Ringwald /**
4879a19cd78SMatthias Ringwald  * Search the maximum weithed value and returns its argument
4889a19cd78SMatthias Ringwald  * x, n            The input vector of size `n`
4899a19cd78SMatthias Ringwald  * w_incr          Increment of the weight
4909a19cd78SMatthias Ringwald  * x_max, xw_max   Return the maximum not weighted value
4919a19cd78SMatthias Ringwald  * return          Return the argument of the weigthed maximum
4929a19cd78SMatthias Ringwald  */
argmax_weighted(const float * x,int n,float w_incr,float * x_max)4934930cef6SMatthias Ringwald LC3_HOT static int argmax_weighted(
4949a19cd78SMatthias Ringwald     const float *x, int n, float w_incr, float *x_max)
4959a19cd78SMatthias Ringwald {
4969a19cd78SMatthias Ringwald     int arg;
4979a19cd78SMatthias Ringwald 
4989a19cd78SMatthias Ringwald     float xw_max = (*x_max = x[arg = 0]);
4999a19cd78SMatthias Ringwald     float w = 1 + w_incr;
5009a19cd78SMatthias Ringwald 
5019a19cd78SMatthias Ringwald     for (int i = 1; i < n; i++, w += w_incr)
5029a19cd78SMatthias Ringwald         if (xw_max < x[i] * w)
5039a19cd78SMatthias Ringwald             xw_max = (*x_max = x[arg = i]) * w;
5049a19cd78SMatthias Ringwald 
5059a19cd78SMatthias Ringwald     return arg;
5069a19cd78SMatthias Ringwald }
5079a19cd78SMatthias Ringwald 
5089a19cd78SMatthias Ringwald /**
509*6897da5cSDirk Helbig  * Interpolate from pitch detected value
5109a19cd78SMatthias Ringwald  * x, n            [-2..-1] Previous, [0..n] Current input
5119a19cd78SMatthias Ringwald  * d               The phase of interpolation (0 to 3)
5129a19cd78SMatthias Ringwald  * return          The interpolated vector
5139a19cd78SMatthias Ringwald  *
5149a19cd78SMatthias Ringwald  * The size `n` of vectors must be multiple of 4
5159a19cd78SMatthias Ringwald  */
interpolate(const int16_t * x,int n,int d,int16_t * y)5164930cef6SMatthias Ringwald LC3_HOT static void interpolate(const int16_t *x, int n, int d, int16_t *y)
5179a19cd78SMatthias Ringwald {
5184930cef6SMatthias Ringwald     static const int16_t h4_q15[][4] = {
5194930cef6SMatthias Ringwald         { 6877, 19121,  6877,     0 }, { 3506, 18025, 11000,   220 },
5204930cef6SMatthias Ringwald         { 1300, 15048, 15048,  1300 }, {  220, 11000, 18025,  3506 } };
5219a19cd78SMatthias Ringwald 
5224930cef6SMatthias Ringwald     const int16_t *h = h4_q15[d];
5234930cef6SMatthias Ringwald     int16_t x3 = x[-2], x2 = x[-1], x1, x0;
5249a19cd78SMatthias Ringwald 
5259a19cd78SMatthias Ringwald     x1 = (*x++);
5264930cef6SMatthias Ringwald     for (const int16_t *ye = y + n; y < ye; ) {
5274930cef6SMatthias Ringwald         int32_t yn;
5284930cef6SMatthias Ringwald 
5294930cef6SMatthias Ringwald         yn = (x0 = *(x++)) * h[0] + x1 * h[1] + x2 * h[2] + x3 * h[3];
5304930cef6SMatthias Ringwald         *(y++) = yn >> 15;
5314930cef6SMatthias Ringwald 
5324930cef6SMatthias Ringwald         yn = (x3 = *(x++)) * h[0] + x0 * h[1] + x1 * h[2] + x2 * h[3];
5334930cef6SMatthias Ringwald         *(y++) = yn >> 15;
5344930cef6SMatthias Ringwald 
5354930cef6SMatthias Ringwald         yn = (x2 = *(x++)) * h[0] + x3 * h[1] + x0 * h[2] + x1 * h[3];
5364930cef6SMatthias Ringwald         *(y++) = yn >> 15;
5374930cef6SMatthias Ringwald 
5384930cef6SMatthias Ringwald         yn = (x1 = *(x++)) * h[0] + x2 * h[1] + x3 * h[2] + x0 * h[3];
5394930cef6SMatthias Ringwald         *(y++) = yn >> 15;
5409a19cd78SMatthias Ringwald     }
5419a19cd78SMatthias Ringwald }
5429a19cd78SMatthias Ringwald 
5439a19cd78SMatthias Ringwald /**
544*6897da5cSDirk Helbig  * Interpolate autocorrelation
5459a19cd78SMatthias Ringwald  * x               [-4..-1] Previous, [0..4] Current input
5469a19cd78SMatthias Ringwald  * d               The phase of interpolation (-3 to 3)
5479a19cd78SMatthias Ringwald  * return          The interpolated value
5489a19cd78SMatthias Ringwald  */
interpolate_corr(const float * x,int d)5494930cef6SMatthias Ringwald LC3_HOT static float interpolate_corr(const float *x, int d)
5509a19cd78SMatthias Ringwald {
5519a19cd78SMatthias Ringwald     static const float h4[][8] = {
5529a19cd78SMatthias Ringwald         {  1.53572770e-02, -4.72963246e-02,  8.35788573e-02,  8.98638285e-01,
5539a19cd78SMatthias Ringwald            8.35788573e-02, -4.72963246e-02,  1.53572770e-02,                 },
5549a19cd78SMatthias Ringwald         {  2.74547165e-03,  4.59833449e-03, -7.54404636e-02,  8.17488686e-01,
5559a19cd78SMatthias Ringwald            3.30182571e-01, -1.05835916e-01,  2.86823405e-02, -2.87456116e-03 },
5569a19cd78SMatthias Ringwald         { -3.00125103e-03,  2.95038503e-02, -1.30305021e-01,  6.03297008e-01,
5579a19cd78SMatthias Ringwald            6.03297008e-01, -1.30305021e-01,  2.95038503e-02, -3.00125103e-03 },
5589a19cd78SMatthias Ringwald         { -2.87456116e-03,  2.86823405e-02, -1.05835916e-01,  3.30182571e-01,
5599a19cd78SMatthias Ringwald            8.17488686e-01, -7.54404636e-02,  4.59833449e-03,  2.74547165e-03 },
5609a19cd78SMatthias Ringwald     };
5619a19cd78SMatthias Ringwald 
5629a19cd78SMatthias Ringwald     const float *h = h4[(4+d) % 4];
5639a19cd78SMatthias Ringwald 
5649a19cd78SMatthias Ringwald     float y = d < 0 ? x[-4] * *(h++) :
5659a19cd78SMatthias Ringwald               d > 0 ? x[ 4] * *(h+7) : 0;
5669a19cd78SMatthias Ringwald 
5679a19cd78SMatthias Ringwald     y += x[-3] * h[0] + x[-2] * h[1] + x[-1] * h[2] + x[0] * h[3] +
5689a19cd78SMatthias Ringwald          x[ 1] * h[4] + x[ 2] * h[5] + x[ 3] * h[6];
5699a19cd78SMatthias Ringwald 
5709a19cd78SMatthias Ringwald     return y;
5719a19cd78SMatthias Ringwald }
5729a19cd78SMatthias Ringwald 
5739a19cd78SMatthias Ringwald /**
574*6897da5cSDirk Helbig  * Pitch detection algorithm
5759a19cd78SMatthias Ringwald  * ltpf            Context of analysis
5769a19cd78SMatthias Ringwald  * x, n            [-114..-17] Previous, [0..n-1] Current 6.4KHz samples
5779a19cd78SMatthias Ringwald  * tc              Return the pitch-lag estimation
5789a19cd78SMatthias Ringwald  * return          True when pitch present
5794930cef6SMatthias Ringwald  *
5804930cef6SMatthias Ringwald  * The `x` vector is aligned on 32 bits
5819a19cd78SMatthias Ringwald  */
detect_pitch(struct lc3_ltpf_analysis * ltpf,const int16_t * x,int n,int * tc)582*6897da5cSDirk Helbig static bool detect_pitch(struct lc3_ltpf_analysis *ltpf,
583*6897da5cSDirk Helbig     const int16_t *x, int n, int *tc)
5849a19cd78SMatthias Ringwald {
5859a19cd78SMatthias Ringwald     float rm1, rm2;
5869a19cd78SMatthias Ringwald     float r[98];
5879a19cd78SMatthias Ringwald 
5889a19cd78SMatthias Ringwald     const int r0 = 17, nr = 98;
5899a19cd78SMatthias Ringwald     int k0 = LC3_MAX(   0, ltpf->tc-4);
5909a19cd78SMatthias Ringwald     int nk = LC3_MIN(nr-1, ltpf->tc+4) - k0 + 1;
5919a19cd78SMatthias Ringwald 
5929a19cd78SMatthias Ringwald     correlate(x, x - r0, n, r, nr);
5939a19cd78SMatthias Ringwald 
5944930cef6SMatthias Ringwald     int t1 = argmax_weighted(r, nr, -.5f/(nr-1), &rm1);
5959a19cd78SMatthias Ringwald     int t2 = k0 + argmax(r + k0, nk, &rm2);
5969a19cd78SMatthias Ringwald 
5974930cef6SMatthias Ringwald     const int16_t *x1 = x - (r0 + t1);
5984930cef6SMatthias Ringwald     const int16_t *x2 = x - (r0 + t2);
5999a19cd78SMatthias Ringwald 
6009a19cd78SMatthias Ringwald     float nc1 = rm1 <= 0 ? 0 :
6019a19cd78SMatthias Ringwald         rm1 / sqrtf(dot(x, x, n) * dot(x1, x1, n));
6029a19cd78SMatthias Ringwald 
6039a19cd78SMatthias Ringwald     float nc2 = rm2 <= 0 ? 0 :
6049a19cd78SMatthias Ringwald         rm2 / sqrtf(dot(x, x, n) * dot(x2, x2, n));
6059a19cd78SMatthias Ringwald 
6064930cef6SMatthias Ringwald     int t1sel = nc2 <= 0.85f * nc1;
6079a19cd78SMatthias Ringwald     ltpf->tc = (t1sel ? t1 : t2);
6089a19cd78SMatthias Ringwald 
6099a19cd78SMatthias Ringwald     *tc = r0 + ltpf->tc;
6104930cef6SMatthias Ringwald     return (t1sel ? nc1 : nc2) > 0.6f;
6119a19cd78SMatthias Ringwald }
6129a19cd78SMatthias Ringwald 
6139a19cd78SMatthias Ringwald /**
614*6897da5cSDirk Helbig  * Pitch-lag parameter
6154930cef6SMatthias Ringwald  * x, n            [-232..-28] Previous, [0..n-1] Current 12.8KHz samples, Q14
6169a19cd78SMatthias Ringwald  * tc              Pitch-lag estimation
6179a19cd78SMatthias Ringwald  * pitch           The pitch value, in fixed .4
6189a19cd78SMatthias Ringwald  * return          The bitstream pitch index value
6194930cef6SMatthias Ringwald  *
6204930cef6SMatthias Ringwald  * The `x` vector is aligned on 32 bits
6219a19cd78SMatthias Ringwald  */
refine_pitch(const int16_t * x,int n,int tc,int * pitch)6224930cef6SMatthias Ringwald static int refine_pitch(const int16_t *x, int n, int tc, int *pitch)
6239a19cd78SMatthias Ringwald {
6249a19cd78SMatthias Ringwald     float r[17], rm;
6259a19cd78SMatthias Ringwald     int e, f;
6269a19cd78SMatthias Ringwald 
6279a19cd78SMatthias Ringwald     int r0 = LC3_MAX( 32, 2*tc - 4);
6289a19cd78SMatthias Ringwald     int nr = LC3_MIN(228, 2*tc + 4) - r0 + 1;
6299a19cd78SMatthias Ringwald 
6309a19cd78SMatthias Ringwald     correlate(x, x - (r0 - 4), n, r, nr + 8);
6319a19cd78SMatthias Ringwald 
6329a19cd78SMatthias Ringwald     e = r0 + argmax(r + 4, nr, &rm);
6339a19cd78SMatthias Ringwald     const float *re = r + (e - (r0 - 4));
6349a19cd78SMatthias Ringwald 
6354930cef6SMatthias Ringwald     float dm = interpolate_corr(re, f = 0);
6369a19cd78SMatthias Ringwald     for (int i = 1; i <= 3; i++) {
6379a19cd78SMatthias Ringwald         float d;
6389a19cd78SMatthias Ringwald 
6399a19cd78SMatthias Ringwald         if (e >= 127 && ((i & 1) | (e >= 157)))
6409a19cd78SMatthias Ringwald             continue;
6419a19cd78SMatthias Ringwald 
6424930cef6SMatthias Ringwald         if ((d = interpolate_corr(re, i)) > dm)
6439a19cd78SMatthias Ringwald             dm = d, f = i;
6449a19cd78SMatthias Ringwald 
6454930cef6SMatthias Ringwald         if (e > 32 && (d = interpolate_corr(re, -i)) > dm)
6469a19cd78SMatthias Ringwald             dm = d, f = -i;
6479a19cd78SMatthias Ringwald     }
6489a19cd78SMatthias Ringwald 
6499a19cd78SMatthias Ringwald     e -=   (f < 0);
6509a19cd78SMatthias Ringwald     f += 4*(f < 0);
6519a19cd78SMatthias Ringwald 
6529a19cd78SMatthias Ringwald     *pitch = 4*e + f;
6539a19cd78SMatthias Ringwald     return e < 127 ? 4*e +  f       - 128 :
6549a19cd78SMatthias Ringwald            e < 157 ? 2*e + (f >> 1) + 126 : e + 283;
6559a19cd78SMatthias Ringwald }
6569a19cd78SMatthias Ringwald 
6579a19cd78SMatthias Ringwald /**
6589a19cd78SMatthias Ringwald  * LTPF Analysis
6599a19cd78SMatthias Ringwald  */
lc3_ltpf_analyse(enum lc3_dt dt,enum lc3_srate sr,struct lc3_ltpf_analysis * ltpf,const int16_t * x,struct lc3_ltpf_data * data)6604930cef6SMatthias Ringwald bool lc3_ltpf_analyse(
6614930cef6SMatthias Ringwald     enum lc3_dt dt, enum lc3_srate sr, struct lc3_ltpf_analysis *ltpf,
6624930cef6SMatthias Ringwald     const int16_t *x, struct lc3_ltpf_data *data)
6639a19cd78SMatthias Ringwald {
6649a19cd78SMatthias Ringwald     /* --- Resampling to 12.8 KHz --- */
6659a19cd78SMatthias Ringwald 
6664930cef6SMatthias Ringwald     int z_12k8 = sizeof(ltpf->x_12k8) / sizeof(*ltpf->x_12k8);
667*6897da5cSDirk Helbig     int n_12k8 = (1 + dt) * 32;
6689a19cd78SMatthias Ringwald 
6699a19cd78SMatthias Ringwald     memmove(ltpf->x_12k8, ltpf->x_12k8 + n_12k8,
6704930cef6SMatthias Ringwald         (z_12k8 - n_12k8) * sizeof(*ltpf->x_12k8));
6719a19cd78SMatthias Ringwald 
6724930cef6SMatthias Ringwald     int16_t *x_12k8 = ltpf->x_12k8 + (z_12k8 - n_12k8);
6734930cef6SMatthias Ringwald 
6749a19cd78SMatthias Ringwald     resample_12k8[sr](&ltpf->hp50, x, x_12k8, n_12k8);
6759a19cd78SMatthias Ringwald 
6769a19cd78SMatthias Ringwald     x_12k8 -= (dt == LC3_DT_7M5 ? 44 : 24);
6779a19cd78SMatthias Ringwald 
6789a19cd78SMatthias Ringwald     /* --- Resampling to 6.4 KHz --- */
6799a19cd78SMatthias Ringwald 
6804930cef6SMatthias Ringwald     int z_6k4 = sizeof(ltpf->x_6k4) / sizeof(*ltpf->x_6k4);
6819a19cd78SMatthias Ringwald     int n_6k4 = n_12k8 >> 1;
6829a19cd78SMatthias Ringwald 
6839a19cd78SMatthias Ringwald     memmove(ltpf->x_6k4, ltpf->x_6k4 + n_6k4,
6844930cef6SMatthias Ringwald         (z_6k4 - n_6k4) * sizeof(*ltpf->x_6k4));
6859a19cd78SMatthias Ringwald 
6864930cef6SMatthias Ringwald     int16_t *x_6k4 = ltpf->x_6k4 + (z_6k4 - n_6k4);
6874930cef6SMatthias Ringwald 
6889a19cd78SMatthias Ringwald     resample_6k4(x_12k8, x_6k4, n_6k4);
6899a19cd78SMatthias Ringwald 
690*6897da5cSDirk Helbig     /* --- Enlarge for small frame size --- */
691*6897da5cSDirk Helbig 
692*6897da5cSDirk Helbig     if (dt == LC3_DT_2M5) {
693*6897da5cSDirk Helbig         x_12k8 -= n_12k8, x_6k4 -= n_6k4;
694*6897da5cSDirk Helbig         n_12k8 += n_12k8; n_6k4 += n_6k4;
695*6897da5cSDirk Helbig     }
696*6897da5cSDirk Helbig 
6979a19cd78SMatthias Ringwald     /* --- Pitch detection --- */
6989a19cd78SMatthias Ringwald 
6999a19cd78SMatthias Ringwald     int tc, pitch = 0;
7009a19cd78SMatthias Ringwald     float nc = 0;
7019a19cd78SMatthias Ringwald 
7029a19cd78SMatthias Ringwald     bool pitch_present = detect_pitch(ltpf, x_6k4, n_6k4, &tc);
7039a19cd78SMatthias Ringwald 
7049a19cd78SMatthias Ringwald     if (pitch_present) {
705*6897da5cSDirk Helbig         int16_t u[128], v[128];
7069a19cd78SMatthias Ringwald 
7079a19cd78SMatthias Ringwald         data->pitch_index = refine_pitch(x_12k8, n_12k8, tc, &pitch);
7089a19cd78SMatthias Ringwald 
7099a19cd78SMatthias Ringwald         interpolate(x_12k8, n_12k8, 0, u);
7109a19cd78SMatthias Ringwald         interpolate(x_12k8 - (pitch >> 2), n_12k8, pitch & 3, v);
7119a19cd78SMatthias Ringwald 
7129a19cd78SMatthias Ringwald         nc = dot(u, v, n_12k8) / sqrtf(dot(u, u, n_12k8) * dot(v, v, n_12k8));
7139a19cd78SMatthias Ringwald     }
7149a19cd78SMatthias Ringwald 
7159a19cd78SMatthias Ringwald     /* --- Activation --- */
7169a19cd78SMatthias Ringwald 
7179a19cd78SMatthias Ringwald      if (ltpf->active) {
7189a19cd78SMatthias Ringwald         int pitch_diff =
7199a19cd78SMatthias Ringwald             LC3_MAX(pitch, ltpf->pitch) - LC3_MIN(pitch, ltpf->pitch);
7209a19cd78SMatthias Ringwald         float nc_diff = nc - ltpf->nc[0];
7219a19cd78SMatthias Ringwald 
722*6897da5cSDirk Helbig         data->active = !lc3_hr(sr) && pitch_present &&
7234930cef6SMatthias Ringwald             ((nc > 0.9f) || (nc > 0.84f && pitch_diff < 8 && nc_diff > -0.1f));
7249a19cd78SMatthias Ringwald 
7259a19cd78SMatthias Ringwald      } else {
726*6897da5cSDirk Helbig          data->active = !lc3_hr(sr) && pitch_present &&
7274930cef6SMatthias Ringwald             ( (dt == LC3_DT_10M || ltpf->nc[1] > 0.94f) &&
7284930cef6SMatthias Ringwald               (ltpf->nc[0] > 0.94f && nc > 0.94f) );
7299a19cd78SMatthias Ringwald      }
7309a19cd78SMatthias Ringwald 
7319a19cd78SMatthias Ringwald      ltpf->active = data->active;
7329a19cd78SMatthias Ringwald      ltpf->pitch = pitch;
7339a19cd78SMatthias Ringwald      ltpf->nc[1] = ltpf->nc[0];
7349a19cd78SMatthias Ringwald      ltpf->nc[0] = nc;
7359a19cd78SMatthias Ringwald 
7369a19cd78SMatthias Ringwald      return pitch_present;
7379a19cd78SMatthias Ringwald }
7389a19cd78SMatthias Ringwald 
7399a19cd78SMatthias Ringwald 
7409a19cd78SMatthias Ringwald /* ----------------------------------------------------------------------------
7419a19cd78SMatthias Ringwald  *  Synthesis
7429a19cd78SMatthias Ringwald  * -------------------------------------------------------------------------- */
7439a19cd78SMatthias Ringwald 
7449a19cd78SMatthias Ringwald /**
745*6897da5cSDirk Helbig  * Width of synthesis filter
746*6897da5cSDirk Helbig  */
747*6897da5cSDirk Helbig 
748*6897da5cSDirk Helbig #define MAX_FILTER_WIDTH \
749*6897da5cSDirk Helbig     (LC3_MAX_SRATE_HZ / 4000)
750*6897da5cSDirk Helbig 
751*6897da5cSDirk Helbig 
752*6897da5cSDirk Helbig /**
7539a19cd78SMatthias Ringwald  * Synthesis filter template
7544930cef6SMatthias Ringwald  * xh, nh          History ring buffer of filtered samples
7554930cef6SMatthias Ringwald  * lag             Lag parameter in the ring buffer
7564930cef6SMatthias Ringwald  * x0              w-1 previous input samples
7579a19cd78SMatthias Ringwald  * x, n            Current samples as input, filtered as output
7584930cef6SMatthias Ringwald  * c, w            Coefficients `den` then `num`, and width of filter
7599a19cd78SMatthias Ringwald  * fade            Fading mode of filter  -1: Out  1: In  0: None
7609a19cd78SMatthias Ringwald  */
synthesize_template(const float * xh,int nh,int lag,const float * x0,float * x,int n,const float * c,const int w,int fade)7614930cef6SMatthias Ringwald LC3_HOT static inline void synthesize_template(
7624930cef6SMatthias Ringwald     const float *xh, int nh, int lag,
7634930cef6SMatthias Ringwald     const float *x0, float *x, int n,
7644930cef6SMatthias Ringwald     const float *c, const int w, int fade)
7659a19cd78SMatthias Ringwald {
7669a19cd78SMatthias Ringwald     float g = (float)(fade <= 0);
7679a19cd78SMatthias Ringwald     float g_incr = (float)((fade > 0) - (fade < 0)) / n;
768*6897da5cSDirk Helbig     float u[MAX_FILTER_WIDTH];
7699a19cd78SMatthias Ringwald 
7709a19cd78SMatthias Ringwald     /* --- Load previous samples --- */
7719a19cd78SMatthias Ringwald 
7724930cef6SMatthias Ringwald     lag += (w >> 1);
7739a19cd78SMatthias Ringwald 
7744930cef6SMatthias Ringwald     const float *y = x - xh < lag ? x + (nh - lag) : x - lag;
7754930cef6SMatthias Ringwald     const float *y_end = xh + nh - 1;
7764930cef6SMatthias Ringwald 
7774930cef6SMatthias Ringwald     for (int j = 0; j < w-1; j++) {
7784930cef6SMatthias Ringwald 
7794930cef6SMatthias Ringwald         u[j] = 0;
7804930cef6SMatthias Ringwald 
7814930cef6SMatthias Ringwald         float yi = *y, xi = *(x0++);
7824930cef6SMatthias Ringwald         y = y < y_end ? y + 1 : xh;
7834930cef6SMatthias Ringwald 
7844930cef6SMatthias Ringwald         for (int k = 0; k <= j; k++)
7854930cef6SMatthias Ringwald             u[j-k] -= yi * c[k];
7864930cef6SMatthias Ringwald 
7874930cef6SMatthias Ringwald         for (int k = 0; k <= j; k++)
7884930cef6SMatthias Ringwald             u[j-k] += xi * c[w+k];
7899a19cd78SMatthias Ringwald     }
7909a19cd78SMatthias Ringwald 
7919a19cd78SMatthias Ringwald     u[w-1] = 0;
7929a19cd78SMatthias Ringwald 
7934930cef6SMatthias Ringwald     /* --- Process by filter length --- */
7949a19cd78SMatthias Ringwald 
7954930cef6SMatthias Ringwald     for (int i = 0; i < n; i += w)
7969a19cd78SMatthias Ringwald         for (int j = 0; j < w; j++, g += g_incr) {
7979a19cd78SMatthias Ringwald 
7984930cef6SMatthias Ringwald             float yi = *y, xi = *x;
7994930cef6SMatthias Ringwald             y = y < y_end ? y + 1 : xh;
8004930cef6SMatthias Ringwald 
8014930cef6SMatthias Ringwald             for (int k = 0; k < w; k++)
8024930cef6SMatthias Ringwald                 u[(j+(w-1)-k)%w] -= yi * c[k];
8034930cef6SMatthias Ringwald 
8044930cef6SMatthias Ringwald             for (int k = 0; k < w; k++)
8054930cef6SMatthias Ringwald                 u[(j+(w-1)-k)%w] += xi * c[w+k];
8069a19cd78SMatthias Ringwald 
8079a19cd78SMatthias Ringwald             *(x++) = xi - g * u[j];
8089a19cd78SMatthias Ringwald             u[j] = 0;
8099a19cd78SMatthias Ringwald         }
8109a19cd78SMatthias Ringwald }
8119a19cd78SMatthias Ringwald 
8129a19cd78SMatthias Ringwald /**
8139a19cd78SMatthias Ringwald  * Synthesis filter for each samplerates (width of filter)
8149a19cd78SMatthias Ringwald  */
8159a19cd78SMatthias Ringwald 
816*6897da5cSDirk Helbig 
synthesize_4(const float * xh,int nh,int lag,const float * x0,float * x,int n,const float * c,int fade)8174930cef6SMatthias Ringwald LC3_HOT static void synthesize_4(const float *xh, int nh, int lag,
8184930cef6SMatthias Ringwald     const float *x0, float *x, int n, const float *c, int fade)
8199a19cd78SMatthias Ringwald {
8204930cef6SMatthias Ringwald     synthesize_template(xh, nh, lag, x0, x, n, c, 4, fade);
8219a19cd78SMatthias Ringwald }
8229a19cd78SMatthias Ringwald 
synthesize_6(const float * xh,int nh,int lag,const float * x0,float * x,int n,const float * c,int fade)8234930cef6SMatthias Ringwald LC3_HOT static void synthesize_6(const float *xh, int nh, int lag,
8244930cef6SMatthias Ringwald     const float *x0, float *x, int n, const float *c, int fade)
8259a19cd78SMatthias Ringwald {
8264930cef6SMatthias Ringwald     synthesize_template(xh, nh, lag, x0, x, n, c, 6, fade);
8279a19cd78SMatthias Ringwald }
8289a19cd78SMatthias Ringwald 
synthesize_8(const float * xh,int nh,int lag,const float * x0,float * x,int n,const float * c,int fade)8294930cef6SMatthias Ringwald LC3_HOT static void synthesize_8(const float *xh, int nh, int lag,
8304930cef6SMatthias Ringwald     const float *x0, float *x, int n, const float *c, int fade)
8319a19cd78SMatthias Ringwald {
8324930cef6SMatthias Ringwald     synthesize_template(xh, nh, lag, x0, x, n, c, 8, fade);
8339a19cd78SMatthias Ringwald }
8349a19cd78SMatthias Ringwald 
synthesize_12(const float * xh,int nh,int lag,const float * x0,float * x,int n,const float * c,int fade)8354930cef6SMatthias Ringwald LC3_HOT static void synthesize_12(const float *xh, int nh, int lag,
8364930cef6SMatthias Ringwald     const float *x0, float *x, int n, const float *c, int fade)
8379a19cd78SMatthias Ringwald {
8384930cef6SMatthias Ringwald     synthesize_template(xh, nh, lag, x0, x, n, c, 12, fade);
8399a19cd78SMatthias Ringwald }
8409a19cd78SMatthias Ringwald 
8414930cef6SMatthias Ringwald static void (* const synthesize[])(const float *, int, int,
8424930cef6SMatthias Ringwald     const float *, float *, int, const float *, int) =
8439a19cd78SMatthias Ringwald {
8449a19cd78SMatthias Ringwald     [LC3_SRATE_8K ] = synthesize_4,
8459a19cd78SMatthias Ringwald     [LC3_SRATE_16K] = synthesize_4,
8469a19cd78SMatthias Ringwald     [LC3_SRATE_24K] = synthesize_6,
8479a19cd78SMatthias Ringwald     [LC3_SRATE_32K] = synthesize_8,
8489a19cd78SMatthias Ringwald     [LC3_SRATE_48K] = synthesize_12,
8499a19cd78SMatthias Ringwald };
8509a19cd78SMatthias Ringwald 
8514930cef6SMatthias Ringwald 
8529a19cd78SMatthias Ringwald /**
8539a19cd78SMatthias Ringwald  * LTPF Synthesis
8549a19cd78SMatthias Ringwald  */
lc3_ltpf_synthesize(enum lc3_dt dt,enum lc3_srate sr,int nbytes,lc3_ltpf_synthesis_t * ltpf,const lc3_ltpf_data_t * data,const float * xh,float * x)8554930cef6SMatthias Ringwald void lc3_ltpf_synthesize(enum lc3_dt dt, enum lc3_srate sr, int nbytes,
8564930cef6SMatthias Ringwald     lc3_ltpf_synthesis_t *ltpf, const lc3_ltpf_data_t *data,
8574930cef6SMatthias Ringwald     const float *xh, float *x)
8589a19cd78SMatthias Ringwald {
859*6897da5cSDirk Helbig     int nh = lc3_ns(dt, sr) + lc3_nh(dt, sr);
8609a19cd78SMatthias Ringwald 
8619a19cd78SMatthias Ringwald     /* --- Filter parameters --- */
8629a19cd78SMatthias Ringwald 
8639a19cd78SMatthias Ringwald     int p_idx = data ? data->pitch_index : 0;
8649a19cd78SMatthias Ringwald     int pitch =
8659a19cd78SMatthias Ringwald         p_idx >= 440 ? (((p_idx     ) - 283) << 2)  :
8669a19cd78SMatthias Ringwald         p_idx >= 380 ? (((p_idx >> 1) -  63) << 2) + (((p_idx & 1)) << 1) :
8679a19cd78SMatthias Ringwald                        (((p_idx >> 2) +  32) << 2) + (((p_idx & 3)) << 0)  ;
8689a19cd78SMatthias Ringwald 
869*6897da5cSDirk Helbig     pitch = (pitch * lc3_ns(LC3_DT_10M, sr) + 64) / 128;
8709a19cd78SMatthias Ringwald 
871*6897da5cSDirk Helbig     int nbits = (nbytes*8 * (1 + LC3_DT_10M)) / (1 + dt);
872*6897da5cSDirk Helbig     if (dt == LC3_DT_2M5)
873*6897da5cSDirk Helbig       nbits = (6 * nbits + 5) / 10;
874*6897da5cSDirk Helbig     if (dt == LC3_DT_5M)
875*6897da5cSDirk Helbig       nbits -= 160;
876*6897da5cSDirk Helbig 
877*6897da5cSDirk Helbig     int g_idx = LC3_MAX(nbits / 80, (int)(3 + sr)) - (3 + sr);
8789a19cd78SMatthias Ringwald     bool active = data && data->active && g_idx < 4;
8799a19cd78SMatthias Ringwald 
880*6897da5cSDirk Helbig     int w = LC3_MAX(4, lc3_ns_4m[sr] >> 4);
881*6897da5cSDirk Helbig     float c[2 * MAX_FILTER_WIDTH];
8829a19cd78SMatthias Ringwald 
8839a19cd78SMatthias Ringwald     for (int i = 0; i < w; i++) {
8849a19cd78SMatthias Ringwald         float g = active ? 0.4f - 0.05f * g_idx : 0;
8854930cef6SMatthias Ringwald         c[  i] = g * lc3_ltpf_cden[sr][pitch & 3][(w-1)-i];
8864930cef6SMatthias Ringwald         c[w+i] = 0.85f * g * lc3_ltpf_cnum[sr][LC3_MIN(g_idx, 3)][(w-1)-i];
8879a19cd78SMatthias Ringwald     }
8889a19cd78SMatthias Ringwald 
8899a19cd78SMatthias Ringwald     /* --- Transition handling --- */
8909a19cd78SMatthias Ringwald 
891*6897da5cSDirk Helbig     int ns = lc3_ns(dt, sr);
892*6897da5cSDirk Helbig     int nt = ns / (1 + dt);
893*6897da5cSDirk Helbig     float x0[2][MAX_FILTER_WIDTH];
894*6897da5cSDirk Helbig 
895*6897da5cSDirk Helbig     memcpy(x0[0], ltpf->x, (w-1) * sizeof(float));
896*6897da5cSDirk Helbig     memcpy(ltpf->x, x + ns - (w-1), (w-1) * sizeof(float));
8979a19cd78SMatthias Ringwald 
8989a19cd78SMatthias Ringwald     if (active)
899*6897da5cSDirk Helbig         memcpy(x0[1], x + nt-(w-1), (w-1) * sizeof(float));
9009a19cd78SMatthias Ringwald 
9019a19cd78SMatthias Ringwald     if (!ltpf->active && active)
902*6897da5cSDirk Helbig         synthesize[sr](xh, nh, pitch/4, x0[0], x, nt, c, 1);
9039a19cd78SMatthias Ringwald     else if (ltpf->active && !active)
904*6897da5cSDirk Helbig         synthesize[sr](xh, nh, ltpf->pitch/4, x0[0], x, nt, ltpf->c, -1);
9059a19cd78SMatthias Ringwald     else if (ltpf->active && active && ltpf->pitch == pitch)
906*6897da5cSDirk Helbig         synthesize[sr](xh, nh, pitch/4, x0[0], x, nt, c, 0);
9079a19cd78SMatthias Ringwald     else if (ltpf->active && active) {
908*6897da5cSDirk Helbig         synthesize[sr](xh, nh, ltpf->pitch/4, x0[0], x, nt, ltpf->c, -1);
9094930cef6SMatthias Ringwald         synthesize[sr](xh, nh, pitch/4,
9104930cef6SMatthias Ringwald             (x <= xh ? x + nh : x) - (w-1), x, nt, c, 1);
9119a19cd78SMatthias Ringwald     }
9129a19cd78SMatthias Ringwald 
9139a19cd78SMatthias Ringwald     /* --- Remainder --- */
9149a19cd78SMatthias Ringwald 
915*6897da5cSDirk Helbig     if (active && ns > nt)
916*6897da5cSDirk Helbig         synthesize[sr](xh, nh, pitch/4, x0[1], x + nt, ns-nt, c, 0);
9179a19cd78SMatthias Ringwald 
9189a19cd78SMatthias Ringwald     /* --- Update state --- */
9199a19cd78SMatthias Ringwald 
9209a19cd78SMatthias Ringwald     ltpf->active = active;
9219a19cd78SMatthias Ringwald     ltpf->pitch = pitch;
9224930cef6SMatthias Ringwald     memcpy(ltpf->c, c, 2*w * sizeof(*ltpf->c));
9239a19cd78SMatthias Ringwald }
9249a19cd78SMatthias Ringwald 
9259a19cd78SMatthias Ringwald 
9269a19cd78SMatthias Ringwald /* ----------------------------------------------------------------------------
9279a19cd78SMatthias Ringwald  *  Bitstream data
9289a19cd78SMatthias Ringwald  * -------------------------------------------------------------------------- */
9299a19cd78SMatthias Ringwald 
9309a19cd78SMatthias Ringwald /**
9319a19cd78SMatthias Ringwald  * LTPF disable
9329a19cd78SMatthias Ringwald  */
lc3_ltpf_disable(struct lc3_ltpf_data * data)9339a19cd78SMatthias Ringwald void lc3_ltpf_disable(struct lc3_ltpf_data *data)
9349a19cd78SMatthias Ringwald {
9359a19cd78SMatthias Ringwald     data->active = false;
9369a19cd78SMatthias Ringwald }
9379a19cd78SMatthias Ringwald 
9389a19cd78SMatthias Ringwald /**
9399a19cd78SMatthias Ringwald  * Return number of bits coding the bitstream data
9409a19cd78SMatthias Ringwald  */
lc3_ltpf_get_nbits(bool pitch)9419a19cd78SMatthias Ringwald int lc3_ltpf_get_nbits(bool pitch)
9429a19cd78SMatthias Ringwald {
9439a19cd78SMatthias Ringwald     return 1 + 10 * pitch;
9449a19cd78SMatthias Ringwald }
9459a19cd78SMatthias Ringwald 
9469a19cd78SMatthias Ringwald /**
9479a19cd78SMatthias Ringwald  * Put bitstream data
9489a19cd78SMatthias Ringwald  */
lc3_ltpf_put_data(lc3_bits_t * bits,const struct lc3_ltpf_data * data)9499a19cd78SMatthias Ringwald void lc3_ltpf_put_data(lc3_bits_t *bits,
9509a19cd78SMatthias Ringwald     const struct lc3_ltpf_data *data)
9519a19cd78SMatthias Ringwald {
9529a19cd78SMatthias Ringwald     lc3_put_bit(bits, data->active);
9539a19cd78SMatthias Ringwald     lc3_put_bits(bits, data->pitch_index, 9);
9549a19cd78SMatthias Ringwald }
9559a19cd78SMatthias Ringwald 
9569a19cd78SMatthias Ringwald /**
9579a19cd78SMatthias Ringwald  * Get bitstream data
9589a19cd78SMatthias Ringwald  */
lc3_ltpf_get_data(lc3_bits_t * bits,struct lc3_ltpf_data * data)959*6897da5cSDirk Helbig void lc3_ltpf_get_data(lc3_bits_t *bits,
960*6897da5cSDirk Helbig     struct lc3_ltpf_data *data)
9619a19cd78SMatthias Ringwald {
9629a19cd78SMatthias Ringwald     data->active = lc3_get_bit(bits);
9639a19cd78SMatthias Ringwald     data->pitch_index = lc3_get_bits(bits, 9);
9649a19cd78SMatthias Ringwald }
965