History

avcodec: add native iLBC decoder

Paul B Mahol committed 9 years ago

Commit 009597c1 parent 59256de7 files · +1736 −1Browse files

Unified · Split

7 files
  1. Changelog +1 −0
  2. libavcodec/Makefile +1 −0
  3. libavcodec/allcodecs.c +1 −0
  4. libavcodec/ilbcdata.h +243 −0
  5. libavcodec/ilbcdec.c +1487 −0
  6. libavcodec/version.h +1 −1
  7. tests/ref/fate/source +2 −0
Changelog+1−0
@@ -30,6 +30,7 @@ version <next>: · 29 unmodified lines
30- amultiply filter30- amultiply filter
31- Block-Matching 3d (bm3d) denoising filter31- Block-Matching 3d (bm3d) denoising filter
32- acrossover filter32- acrossover filter
33- ilbc decoder
33 34
34 35
35version 4.0:36version 4.0:
libavcodec/Makefile+1−0
@@ -384,6 +384,7 @@ OBJS-$(CONFIG_HUFFYUV_ENCODER) += huffyuv.o huffyuvenc.o · 383 unmodified lines
384OBJS-$(CONFIG_IDCIN_DECODER) += idcinvideo.o384OBJS-$(CONFIG_IDCIN_DECODER) += idcinvideo.o
385OBJS-$(CONFIG_IDF_DECODER) += bintext.o cga_data.o385OBJS-$(CONFIG_IDF_DECODER) += bintext.o cga_data.o
386OBJS-$(CONFIG_IFF_ILBM_DECODER) += iff.o386OBJS-$(CONFIG_IFF_ILBM_DECODER) += iff.o
387OBJS-$(CONFIG_ILBC_DECODER) += ilbcdec.o
387OBJS-$(CONFIG_IMC_DECODER) += imc.o388OBJS-$(CONFIG_IMC_DECODER) += imc.o
388OBJS-$(CONFIG_IMM4_DECODER) += imm4.o389OBJS-$(CONFIG_IMM4_DECODER) += imm4.o
389OBJS-$(CONFIG_INDEO2_DECODER) += indeo2.o390OBJS-$(CONFIG_INDEO2_DECODER) += indeo2.o
libavcodec/allcodecs.c+1−0
@@ -424,6 +424,7 @@ extern AVCodec ff_g729_decoder; · 423 unmodified lines
424extern AVCodec ff_gsm_decoder;424extern AVCodec ff_gsm_decoder;
425extern AVCodec ff_gsm_ms_decoder;425extern AVCodec ff_gsm_ms_decoder;
426extern AVCodec ff_iac_decoder;426extern AVCodec ff_iac_decoder;
427extern AVCodec ff_ilbc_decoder;
427extern AVCodec ff_imc_decoder;428extern AVCodec ff_imc_decoder;
428extern AVCodec ff_interplay_acm_decoder;429extern AVCodec ff_interplay_acm_decoder;
429extern AVCodec ff_mace3_decoder;430extern AVCodec ff_mace3_decoder;
libavcodec/ilbcdata.h+243−0
@@ -0,0 +1,243 @@
1/*
2 * Copyright (c) 2013, The WebRTC project authors. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * * Neither the name of Google nor the names of its contributors may
17 * be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef AVCODEC_ILBCDATA_H
34#define AVCODEC_ILBCDATA_H
35
36#include "libavutil/common.h"
37
38static const uint8_t lsf_dim_codebook[] = { 3, 3, 4 };
39static const uint8_t lsf_size_codebook[] = { 64, 128, 128 };
40static const int16_t lsf_weight_20ms[] = { 12288, 8192, 4096, 0 };
41static const int16_t lsf_weight_30ms[] = { 8192, 16384, 10923, 5461, 0, 0 };
42
43static const int16_t hp_out_coeffs[] = { 3849, -7699, 3849, 7918, -3833 };
44
45static const int16_t kPlcPfSlope[] = { 26667, 18729, 13653, 10258, 7901, 6214 };
46
47static const int16_t kPlcPitchFact[] = { 0, 5462, 10922, 16384, 21846, 27306 };
48
49static const int16_t kCbFiltersRev[] = {
50 -140, 446, -755, 3302, 2922, -590, 343, -138
51};
52
53static const int16_t kPlcPerSqr[] = { 839, 1343, 2048, 2998, 4247, 5849 };
54
55static const int16_t alpha[] = {
56 6554, 13107, 19661, 26214
57};
58
59static const int16_t kLpcChirpSyntDenum[] = {
60 32767, 29573, 26690, 24087, 21739, 19619, 17707, 15980, 14422, 13016, 11747
61};
62
63static const int16_t LpcChirpWeightDenum[] = {
64 32767, 13835, 5841, 2466, 1041, 440, 186, 78, 33, 14, 6
65};
66
67static const int16_t cos_tbl[64] = {
68 32767, 32729, 32610, 32413, 32138, 31786, 31357, 30853,
69 30274, 29622, 28899, 28106, 27246, 26320, 25330, 24279,
70 23170, 22006, 20788, 19520, 18205, 16846, 15447, 14010,
71 12540, 11039, 9512, 7962, 6393, 4808, 3212, 1608,
72 0, -1608, -3212, -4808, -6393, -7962, -9512, -11039,
73 -12540, -14010, -15447, -16846, -18205, -19520, -20788, -22006,
74 -23170, -24279, -25330, -26320, -27246, -28106, -28899, -29622,
75 -30274, -30853, -31357, -31786, -32138, -32413, -32610, -32729,
76};
77
78static const int16_t cos_derivative_tbl[64] = {
79 -632, -1893, -3150, -4399, -5638, -6863, -8072, -9261,
80 -10428, -11570, -12684, -13767, -14817, -15832, -16808, -17744,
81 -18637, -19486, -20287, -21039, -21741, -22390, -22986, -23526,
82 -24009, -24435, -24801, -25108, -25354, -25540, -25664, -25726,
83 -25726, -25664, -25540, -25354, -25108, -24801, -24435, -24009,
84 -23526, -22986, -22390, -21741, -21039, -20287, -19486, -18637,
85 -17744, -16808, -15832, -14817, -13767, -12684, -11570, -10428,
86 -9261, -8072, -6863, -5638, -4399, -3150, -1893, -632
87};
88
89static const int16_t lsf_codebook[64 * 3 + 128 * 3 + 128 * 4] = {
90 1273, 2238, 3696, 3199, 5309, 8209, 3606, 5671, 7829,
91 2815, 5262, 8778, 2608, 4027, 5493, 1582, 3076, 5945,
92 2983, 4181, 5396, 2437, 4322, 6902, 1861, 2998, 4613,
93 2007, 3250, 5214, 1388, 2459, 4262, 2563, 3805, 5269,
94 2036, 3522, 5129, 1935, 4025, 6694, 2744, 5121, 7338,
95 2810, 4248, 5723, 3054, 5405, 7745, 1449, 2593, 4763,
96 3411, 5128, 6596, 2484, 4659, 7496, 1668, 2879, 4818,
97 1812, 3072, 5036, 1638, 2649, 3900, 2464, 3550, 4644,
98 1853, 2900, 4158, 2458, 4163, 5830, 2556, 4036, 6254,
99 2703, 4432, 6519, 3062, 4953, 7609, 1725, 3703, 6187,
100 2221, 3877, 5427, 2339, 3579, 5197, 2021, 4633, 7037,
101 2216, 3328, 4535, 2961, 4739, 6667, 2807, 3955, 5099,
102 2788, 4501, 6088, 1642, 2755, 4431, 3341, 5282, 7333,
103 2414, 3726, 5727, 1582, 2822, 5269, 2259, 3447, 4905,
104 3117, 4986, 7054, 1825, 3491, 5542, 3338, 5736, 8627,
105 1789, 3090, 5488, 2566, 3720, 4923, 2846, 4682, 7161,
106 1950, 3321, 5976, 1834, 3383, 6734, 3238, 4769, 6094,
107 2031, 3978, 5903, 1877, 4068, 7436, 2131, 4644, 8296,
108 2764, 5010, 8013, 2194, 3667, 6302, 2053, 3127, 4342,
109 3523, 6595, 10010, 3134, 4457, 5748, 3142, 5819, 9414,
110 2223, 4334, 6353, 2022, 3224, 4822, 2186, 3458, 5544,
111 2552, 4757, 6870, 10905, 12917, 14578, 9503, 11485, 14485,
112 9518, 12494, 14052, 6222, 7487, 9174, 7759, 9186, 10506,
113 8315, 12755, 14786, 9609, 11486, 13866, 8909, 12077, 13643,
114 7369, 9054, 11520, 9408, 12163, 14715, 6436, 9911, 12843,
115 7109, 9556, 11884, 7557, 10075, 11640, 6482, 9202, 11547,
116 6463, 7914, 10980, 8611, 10427, 12752, 7101, 9676, 12606,
117 7428, 11252, 13172, 10197, 12955, 15842, 7487, 10955, 12613,
118 5575, 7858, 13621, 7268, 11719, 14752, 7476, 11744, 13795,
119 7049, 8686, 11922, 8234, 11314, 13983, 6560, 11173, 14984,
120 6405, 9211, 12337, 8222, 12054, 13801, 8039, 10728, 13255,
121 10066, 12733, 14389, 6016, 7338, 10040, 6896, 8648, 10234,
122 7538, 9170, 12175, 7327, 12608, 14983, 10516, 12643, 15223,
123 5538, 7644, 12213, 6728, 12221, 14253, 7563, 9377, 12948,
124 8661, 11023, 13401, 7280, 8806, 11085, 7723, 9793, 12333,
125 12225, 14648, 16709, 8768, 13389, 15245, 10267, 12197, 13812,
126 5301, 7078, 11484, 7100, 10280, 11906, 8716, 12555, 14183,
127 9567, 12464, 15434, 7832, 12305, 14300, 7608, 10556, 12121,
128 8913, 11311, 12868, 7414, 9722, 11239, 8666, 11641, 13250,
129 9079, 10752, 12300, 8024, 11608, 13306, 10453, 13607, 16449,
130 8135, 9573, 10909, 6375, 7741, 10125, 10025, 12217, 14874,
131 6985, 11063, 14109, 9296, 13051, 14642, 8613, 10975, 12542,
132 6583, 10414, 13534, 6191, 9368, 13430, 5742, 6859, 9260,
133 7723, 9813, 13679, 8137, 11291, 12833, 6562, 8973, 10641,
134 6062, 8462, 11335, 6928, 8784, 12647, 7501, 8784, 10031,
135 8372, 10045, 12135, 8191, 9864, 12746, 5917, 7487, 10979,
136 5516, 6848, 10318, 6819, 9899, 11421, 7882, 12912, 15670,
137 9558, 11230, 12753, 7752, 9327, 11472, 8479, 9980, 11358,
138 11418, 14072, 16386, 7968, 10330, 14423, 8423, 10555, 12162,
139 6337, 10306, 14391, 8850, 10879, 14276, 6750, 11885, 15710,
140 7037, 8328, 9764, 6914, 9266, 13476, 9746, 13949, 15519,
141 11032, 14444, 16925, 8032, 10271, 11810, 10962, 13451, 15833,
142 10021, 11667, 13324, 6273, 8226, 12936, 8543, 10397, 13496,
143 7936, 10302, 12745, 6769, 8138, 10446, 6081, 7786, 11719,
144 8637, 11795, 14975, 8790, 10336, 11812, 7040, 8490, 10771,
145 7338, 10381, 13153, 6598, 7888, 9358, 6518, 8237, 12030,
146 9055, 10763, 12983, 6490, 10009, 12007, 9589, 12023, 13632,
147 6867, 9447, 10995, 7930, 9816, 11397, 10241, 13300, 14939,
148 5830, 8670, 12387, 9870, 11915, 14247, 9318, 11647, 13272,
149 6721, 10836, 12929, 6543, 8233, 9944, 8034, 10854, 12394,
150 9112, 11787, 14218, 9302, 11114, 13400, 9022, 11366, 13816,
151 6962, 10461, 12480, 11288, 13333, 15222, 7249, 8974, 10547,
152 10566, 12336, 14390, 6697, 11339, 13521, 11851, 13944, 15826,
153 6847, 8381, 11349, 7509, 9331, 10939, 8029, 9618, 11909,
154 13973, 17644, 19647, 22474, 14722, 16522, 20035, 22134, 16305, 18179, 21106, 23048,
155 15150, 17948, 21394, 23225, 13582, 15191, 17687, 22333, 11778, 15546, 18458, 21753,
156 16619, 18410, 20827, 23559, 14229, 15746, 17907, 22474, 12465, 15327, 20700, 22831,
157 15085, 16799, 20182, 23410, 13026, 16935, 19890, 22892, 14310, 16854, 19007, 22944,
158 14210, 15897, 18891, 23154, 14633, 18059, 20132, 22899, 15246, 17781, 19780, 22640,
159 16396, 18904, 20912, 23035, 14618, 17401, 19510, 21672, 15473, 17497, 19813, 23439,
160 18851, 20736, 22323, 23864, 15055, 16804, 18530, 20916, 16490, 18196, 19990, 21939,
161 11711, 15223, 21154, 23312, 13294, 15546, 19393, 21472, 12956, 16060, 20610, 22417,
162 11628, 15843, 19617, 22501, 14106, 16872, 19839, 22689, 15655, 18192, 20161, 22452,
163 12953, 15244, 20619, 23549, 15322, 17193, 19926, 21762, 16873, 18676, 20444, 22359,
164 14874, 17871, 20083, 21959, 11534, 14486, 19194, 21857, 17766, 19617, 21338, 23178,
165 13404, 15284, 19080, 23136, 15392, 17527, 19470, 21953, 14462, 16153, 17985, 21192,
166 17734, 19750, 21903, 23783, 16973, 19096, 21675, 23815, 16597, 18936, 21257, 23461,
167 15966, 17865, 20602, 22920, 15416, 17456, 20301, 22972, 18335, 20093, 21732, 23497,
168 15548, 17217, 20679, 23594, 15208, 16995, 20816, 22870, 13890, 18015, 20531, 22468,
169 13211, 15377, 19951, 22388, 12852, 14635, 17978, 22680, 16002, 17732, 20373, 23544,
170 11373, 14134, 19534, 22707, 17329, 19151, 21241, 23462, 15612, 17296, 19362, 22850,
171 15422, 19104, 21285, 23164, 13792, 17111, 19349, 21370, 15352, 17876, 20776, 22667,
172 15253, 16961, 18921, 22123, 14108, 17264, 20294, 23246, 15785, 17897, 20010, 21822,
173 17399, 19147, 20915, 22753, 13010, 15659, 18127, 20840, 16826, 19422, 22218, 24084,
174 18108, 20641, 22695, 24237, 18018, 20273, 22268, 23920, 16057, 17821, 21365, 23665,
175 16005, 17901, 19892, 23016, 13232, 16683, 21107, 23221, 13280, 16615, 19915, 21829,
176 14950, 18575, 20599, 22511, 16337, 18261, 20277, 23216, 14306, 16477, 21203, 23158,
177 12803, 17498, 20248, 22014, 14327, 17068, 20160, 22006, 14402, 17461, 21599, 23688,
178 16968, 18834, 20896, 23055, 15070, 17157, 20451, 22315, 15419, 17107, 21601, 23946,
179 16039, 17639, 19533, 21424, 16326, 19261, 21745, 23673, 16489, 18534, 21658, 23782,
180 16594, 18471, 20549, 22807, 18973, 21212, 22890, 24278, 14264, 18674, 21123, 23071,
181 15117, 16841, 19239, 23118, 13762, 15782, 20478, 23230, 14111, 15949, 20058, 22354,
182 14990, 16738, 21139, 23492, 13735, 16971, 19026, 22158, 14676, 17314, 20232, 22807,
183 16196, 18146, 20459, 22339, 14747, 17258, 19315, 22437, 14973, 17778, 20692, 23367,
184 15715, 17472, 20385, 22349, 15702, 18228, 20829, 23410, 14428, 16188, 20541, 23630,
185 16824, 19394, 21365, 23246, 13069, 16392, 18900, 21121, 12047, 16640, 19463, 21689,
186 14757, 17433, 19659, 23125, 15185, 16930, 19900, 22540, 16026, 17725, 19618, 22399,
187 16086, 18643, 21179, 23472, 15462, 17248, 19102, 21196, 17368, 20016, 22396, 24096,
188 12340, 14475, 19665, 23362, 13636, 16229, 19462, 22728, 14096, 16211, 19591, 21635,
189 12152, 14867, 19943, 22301, 14492, 17503, 21002, 22728, 14834, 16788, 19447, 21411,
190 14650, 16433, 19326, 22308, 14624, 16328, 19659, 23204, 13888, 16572, 20665, 22488,
191 12977, 16102, 18841, 22246, 15523, 18431, 21757, 23738, 14095, 16349, 18837, 20947,
192 13266, 17809, 21088, 22839, 15427, 18190, 20270, 23143, 11859, 16753, 20935, 22486,
193 12310, 17667, 21736, 23319, 14021, 15926, 18702, 22002, 12286, 15299, 19178, 21126,
194 15703, 17491, 21039, 23151, 12272, 14018, 18213, 22570, 14817, 16364, 18485, 22598,
195 17109, 19683, 21851, 23677, 12657, 14903, 19039, 22061, 14713, 16487, 20527, 22814,
196 14635, 16726, 18763, 21715, 15878, 18550, 20718, 22906
197};
198
199static const int16_t gain3[9]={
200 -16384, -10813, -5407, 0, 4096, 8192, 12288, 16384, 32767
201};
202
203static const int16_t gain4[17]={
204 -17203, -14746, -12288, -9830, -7373, -4915, -2458, 0, 2458, 4915, 7373, 9830,
205 12288, 14746, 17203, 19661, 32767
206};
207
208static const int16_t gain5[33]={
209 614, 1229, 1843, 2458, 3072, 3686,
210 4301, 4915, 5530, 6144, 6758, 7373,
211 7987, 8602, 9216, 9830, 10445, 11059,
212 11674, 12288, 12902, 13517, 14131, 14746,
213 15360, 15974, 16589, 17203, 17818, 18432,
214 19046, 19661, 32767
215};
216
217static const int16_t *const ilbc_gain[] = {
218 gain5, gain4, gain3,
219};
220
221static const int16_t ilbc_state[8] = {
222 -30473, -17838, -9257, -2537, 3639, 10893, 19958, 32636
223};
224
225static const int16_t frg_quant_mod[64] = {
226 /* First 37 values in Q8 */
227 569, 671, 786, 916, 1077, 1278,
228 1529, 1802, 2109, 2481, 2898, 3440,
229 3943, 4535, 5149, 5778, 6464, 7208,
230 7904, 8682, 9397, 10285, 11240, 12246,
231 13313, 14382, 15492, 16735, 18131, 19693,
232 21280, 22912, 24624, 26544, 28432, 30488,
233 32720,
234 /* 22 values in Q5 */
235 4383, 4684, 5012, 5363, 5739, 6146,
236 6603, 7113, 7679, 8285, 9040, 9850,
237 10838, 11882, 13103, 14467, 15950, 17669,
238 19712, 22016, 24800, 28576,
239 /* 5 values in Q3 */
240 8240, 9792, 12040, 15440, 22472
241};
242
243#endif /* AVCODEC_ILBCDATA_H */
libavcodec/ilbcdec.c+1487−0
@@ -0,0 +1,1487 @@
1/*
2 * Copyright (c) 2013, The WebRTC project authors. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * * Neither the name of Google nor the names of its contributors may
17 * be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "avcodec.h"
34#include "internal.h"
35#include "get_bits.h"
36#include "ilbcdata.h"
37
38#define LPC_N_20MS 1
39#define LPC_N_30MS 2
40#define LPC_N_MAX 2
41#define LSF_NSPLIT 3
42#define NASUB_MAX 4
43#define LPC_FILTERORDER 10
44#define NSUB_MAX 6
45#define SUBL 40
46
47#define ST_MEM_L_TBL 85
48#define MEM_LF_TBL 147
49#define STATE_SHORT_LEN_20MS 57
50#define STATE_SHORT_LEN_30MS 58
51
52#define BLOCKL_MAX 240
53#define CB_MEML 147
54#define CB_NSTAGES 3
55#define CB_HALFFILTERLEN 4
56#define CB_FILTERLEN 8
57
58#define ENH_NBLOCKS_TOT 8
59#define ENH_BLOCKL 80
60#define ENH_BUFL (ENH_NBLOCKS_TOT)*ENH_BLOCKL
61#define ENH_BUFL_FILTEROVERHEAD 3
62#define BLOCKL_MAX 240
63#define NSUB_20MS 4
64#define NSUB_30MS 6
65#define NSUB_MAX 6
66#define NASUB_20MS 2
67#define NASUB_30MS 4
68#define NASUB_MAX 4
69#define STATE_LEN 80
70#define STATE_SHORT_LEN_30MS 58
71#define STATE_SHORT_LEN_20MS 57
72
73#define SPL_MUL_16_16(a, b) ((int32_t) (((int16_t)(a)) * ((int16_t)(b))))
74#define SPL_MUL_16_16_RSFT(a, b, c) (SPL_MUL_16_16(a, b) >> (c))
75
76typedef struct ILBCFrame {
77 int16_t lsf[LSF_NSPLIT*LPC_N_MAX];
78 int16_t cb_index[CB_NSTAGES*(NASUB_MAX + 1)];
79 int16_t gain_index[CB_NSTAGES*(NASUB_MAX + 1)];
80 int16_t ifm;
81 int16_t state_first;
82 int16_t idx[STATE_SHORT_LEN_30MS];
83 int16_t firstbits;
84 int16_t start;
85} ILBCFrame;
86
87typedef struct ILBCContext {
88 AVClass *class;
89 int enhancer;
90
91 int mode;
92 GetBitContext gb;
93 ILBCFrame frame;
94
95 int prev_enh_pl;
96 int consPLICount;
97 int last_lag;
98 int state_short_len;
99 int lpc_n;
100 int16_t nasub;
101 int16_t nsub;
102 int block_samples;
103 int16_t no_of_words;
104 int16_t no_of_bytes;
105 int16_t lsfdeq[LPC_FILTERORDER*LPC_N_MAX];
106 int16_t lsfold[LPC_FILTERORDER];
107 int16_t syntMem[LPC_FILTERORDER];
108 int16_t lsfdeqold[LPC_FILTERORDER];
109 int16_t weightdenum[(LPC_FILTERORDER + 1) * NSUB_MAX];
110 int16_t syntdenum[NSUB_MAX * (LPC_FILTERORDER + 1)];
111 int16_t old_syntdenum[NSUB_MAX * (LPC_FILTERORDER + 1)];
112 int16_t enh_buf[ENH_BUFL+ENH_BUFL_FILTEROVERHEAD];
113 int16_t enh_period[ENH_NBLOCKS_TOT];
114 int16_t prevResidual[NSUB_MAX*SUBL];
115 int16_t decresidual[BLOCKL_MAX];
116 int16_t plc_residual[BLOCKL_MAX + LPC_FILTERORDER];
117 int16_t seed;
118 int16_t prevPLI;
119 int16_t prevScale;
120 int16_t prevLag;
121 int16_t per_square;
122 int16_t prev_lpc[LPC_FILTERORDER + 1];
123 int16_t plc_lpc[LPC_FILTERORDER + 1];
124 int16_t hpimemx[2];
125 int16_t hpimemy[4];
126} ILBCContext;
127
128static int unpack_frame(ILBCContext *s)
129{
130 ILBCFrame *frame = &s->frame;
131 GetBitContext *gb = &s->gb;
132 int j;
133
134 frame->lsf[0] = get_bits(gb, 6);
135 frame->lsf[1] = get_bits(gb, 7);
136 frame->lsf[2] = get_bits(gb, 7);
137
138 if (s->mode = 20) {
139 frame->start = get_bits(gb, 2);
140 frame->state_first = get_bits1(gb);
141 frame->ifm = get_bits(gb, 6);
142 frame->cb_index[0] = get_bits(gb, 6) << 1;
143 frame->gain_index[0] = get_bits(gb, 2) << 3;
144 frame->gain_index[1] = get_bits1(gb) << 3;
145 frame->cb_index[3] = get_bits(gb, 7) << 1;
146 frame->gain_index[3] = get_bits1(gb) << 4;
147 frame->gain_index[4] = get_bits1(gb) << 3;
148 frame->gain_index[6] = get_bits1(gb) << 4;
149 } else {
150 frame->lsf[3] = get_bits(gb, 6);
151 frame->lsf[4] = get_bits(gb, 7);
152 frame->lsf[5] = get_bits(gb, 7);
153 frame->start = get_bits(gb, 3);
154 frame->state_first = get_bits1(gb);
155 frame->ifm = get_bits(gb, 6);
156 frame->cb_index[0] = get_bits(gb, 4) << 3;
157 frame->gain_index[0] = get_bits1(gb) << 4;
158 frame->gain_index[1] = get_bits1(gb) << 3;
159 frame->cb_index[3] = get_bits(gb, 6) << 2;
160 frame->gain_index[3] = get_bits1(gb) << 4;
161 frame->gain_index[4] = get_bits1(gb) << 3;
162 }
163
164 for (j = 0; j < 48; j++)
165 frame->idx[j] = get_bits1(gb) << 2;
166
167 if (s->mode == 20) {
168 for (; j < 57; j++)
169 frame->idx[j] = get_bits1(gb) << 2;
170
171 frame->gain_index[1] |= get_bits1(gb) << 2;
172 frame->gain_index[3] |= get_bits(gb, 2) << 2;
173 frame->gain_index[4] |= get_bits1(gb) << 2;
174 frame->gain_index[6] |= get_bits1(gb) << 3;
175 frame->gain_index[7] = get_bits(gb, 2) << 2;
176 } else {
177 for (; j < 58; j++)
178 frame->idx[j] = get_bits1(gb) << 2;
179
180 frame->cb_index[0] |= get_bits(gb, 2) << 1;
181 frame->gain_index[0] |= get_bits1(gb) << 3;
182 frame->gain_index[1] |= get_bits1(gb) << 2;
183 frame->cb_index[3] |= get_bits1(gb) << 1;
184 frame->cb_index[6] = get_bits1(gb) << 7;
185 frame->cb_index[6] |= get_bits(gb, 6) << 1;
186 frame->cb_index[9] = get_bits(gb, 7) << 1;
187 frame->cb_index[12] = get_bits(gb, 3) << 5;
188 frame->cb_index[12] |= get_bits(gb, 4) << 1;
189 frame->gain_index[3] |= get_bits(gb, 2) << 2;
190 frame->gain_index[4] |= get_bits(gb, 2) << 1;
191 frame->gain_index[6] = get_bits(gb, 2) << 3;
192 frame->gain_index[7] = get_bits(gb, 2) << 2;
193 frame->gain_index[9] = get_bits1(gb) << 4;
194 frame->gain_index[10] = get_bits1(gb) << 3;
195 frame->gain_index[12] = get_bits1(gb) << 4;
196 frame->gain_index[13] = get_bits1(gb) << 3;
197 }
198
199 for (j = 0; j < 56; j++)
200 frame->idx[j] |= get_bits(gb, 2);
201
202 if (s->mode == 20) {
203 frame->idx[56] |= get_bits(gb, 2);
204 frame->cb_index[0] |= get_bits1(gb);
205 frame->cb_index[1] = get_bits(gb, 7);
206 frame->cb_index[2] = get_bits(gb, 6) << 1;
207 frame->cb_index[2] |= get_bits1(gb);
208 frame->gain_index[0] |= get_bits(gb, 3);
209 frame->gain_index[1] |= get_bits(gb, 2);
210 frame->gain_index[2] = get_bits(gb, 3);
211 frame->cb_index[3] |= get_bits1(gb);
212 frame->cb_index[4] = get_bits(gb, 6) << 1;
213 frame->cb_index[4] |= get_bits1(gb);
214 frame->cb_index[5] = get_bits(gb, 7);
215 frame->cb_index[6] = get_bits(gb, 8);
216 frame->cb_index[7] = get_bits(gb, 8);
217 frame->cb_index[8] = get_bits(gb, 8);
218 frame->gain_index[3] |= get_bits(gb, 2);
219 frame->gain_index[4] |= get_bits(gb, 2);
220 frame->gain_index[5] = get_bits(gb, 3);
221 frame->gain_index[6] |= get_bits(gb, 3);
222 frame->gain_index[7] |= get_bits(gb, 2);
223 frame->gain_index[8] = get_bits(gb, 3);
224 } else {
225 frame->idx[56] |= get_bits(gb, 2);
226 frame->idx[57] |= get_bits(gb, 2);
227 frame->cb_index[0] |= get_bits1(gb);
228 frame->cb_index[1] = get_bits(gb, 7);
229 frame->cb_index[2] = get_bits(gb, 4) << 3;
230 frame->cb_index[2] |= get_bits(gb, 3);
231 frame->gain_index[0] |= get_bits(gb, 3);
232 frame->gain_index[1] |= get_bits(gb, 2);
233 frame->gain_index[2] = get_bits(gb, 3);
234 frame->cb_index[3] |= get_bits1(gb);
235 frame->cb_index[4] = get_bits(gb, 4) << 3;
236 frame->cb_index[4] |= get_bits(gb, 3);
237 frame->cb_index[5] = get_bits(gb, 7);
238 frame->cb_index[6] |= get_bits1(gb);
239 frame->cb_index[7] = get_bits(gb, 5) << 3;
240 frame->cb_index[7] |= get_bits(gb, 3);
241 frame->cb_index[8] = get_bits(gb, 8);
242 frame->cb_index[9] |= get_bits1(gb);
243 frame->cb_index[10] = get_bits(gb, 4) << 4;
244 frame->cb_index[10] |= get_bits(gb, 4);
245 frame->cb_index[11] = get_bits(gb, 8);
246 frame->cb_index[12] |= get_bits1(gb);
247 frame->cb_index[13] = get_bits(gb, 3) << 5;
248 frame->cb_index[13] |= get_bits(gb, 5);
249 frame->cb_index[14] = get_bits(gb, 8);
250 frame->gain_index[3] |= get_bits(gb, 2);
251 frame->gain_index[4] |= get_bits1(gb);
252 frame->gain_index[5] = get_bits(gb, 3);
253 frame->gain_index[6] |= get_bits(gb, 3);
254 frame->gain_index[7] |= get_bits(gb, 2);
255 frame->gain_index[8] = get_bits(gb, 3);
256 frame->gain_index[9] |= get_bits(gb, 4);
257 frame->gain_index[10] |= get_bits1(gb) << 2;
258 frame->gain_index[10] |= get_bits(gb, 2);
259 frame->gain_index[11] = get_bits(gb, 3);
260 frame->gain_index[12] |= get_bits(gb, 4);
261 frame->gain_index[13] |= get_bits(gb, 3);
262 frame->gain_index[14] = get_bits(gb, 3);
263 }
264
265 return get_bits1(gb);
266}
267
268static void index_conv(int16_t *index)
269{
270 int k;
271
272 for (k = 4; k < 6; k++) {
273 if (index[k] >= 44 && index[k] < 108) {
274 index[k] += 64;
275 } else if (index[k] >= 108 && index[k] < 128) {
276 index[k] += 128;
277 }
278 }
279}
280
281static void lsf_dequantization(int16_t *lsfdeq, int16_t *index, int16_t lpc_n)
282{
283 int i, j, pos = 0, cb_pos = 0;
284
285 for (i = 0; i < LSF_NSPLIT; i++) {
286 for (j = 0; j < lsf_dim_codebook[i]; j++) {
287 lsfdeq[pos + j] = lsf_codebook[cb_pos + index[i] * lsf_dim_codebook[i] + j];
288 }
289
290 pos += lsf_dim_codebook[i];
291 cb_pos += lsf_size_codebook[i] * lsf_dim_codebook[i];
292 }
293
294 if (lpc_n > 1) {
295 pos = 0;
296 cb_pos = 0;
297 for (i = 0; i < LSF_NSPLIT; i++) {
298 for (j = 0; j < lsf_dim_codebook[i]; j++) {
299 lsfdeq[LPC_FILTERORDER + pos + j] = lsf_codebook[cb_pos +
300 index[LSF_NSPLIT + i] * lsf_dim_codebook[i] + j];
301 }
302
303 pos += lsf_dim_codebook[i];
304 cb_pos += lsf_size_codebook[i] * lsf_dim_codebook[i];
305 }
306 }
307}
308
309static void lsf_check_stability(int16_t *lsf, int dim, int nb_vectors)
310{
311 for (int n = 0; n < 2; n++) {
312 for (int m = 0; m < nb_vectors; m++) {
313 for (int k = 0; k < dim - 1; k++) {
314 int i = m * dim + k;
315
316 if ((lsf[i + 1] - lsf[i]) < 319) {
317 if (lsf[i + 1] < lsf[i]) {
318 lsf[i + 1] = lsf[i] + 160;
319 lsf[i] = lsf[i + 1] - 160;
320 } else {
321 lsf[i] -= 160;
322 lsf[i + 1] += 160;
323 }
324 }
325
326 lsf[i] = av_clip(lsf[i], 82, 25723);
327 }
328 }
329 }
330}
331
332static void lsf_interpolate(int16_t *out, int16_t *in1,
333 int16_t *in2, int16_t coef,
334 int size)
335{
336 int invcoef = 16384 - coef, i;
337
338 for (i = 0; i < size; i++)
339 out[i] = (coef * in1[i] + invcoef * in2[i] + 8192) >> 14;
340}
341
342static void lsf2lsp(int16_t *lsf, int16_t *lsp, int order)
343{
344 int16_t diff, freq;
345 int32_t tmp;
346 int i, k;
347
348 for (i = 0; i < order; i++) {
349 freq = (lsf[i] * 20861) >> 15;
350 /* 20861: 1.0/(2.0*PI) in Q17 */
351 /*
352 Upper 8 bits give the index k and
353 Lower 8 bits give the difference, which needs
354 to be approximated linearly
355 */
356 k = FFMIN(freq >> 8, 63);
357 diff = freq & 0xFF;
358
359 /* Calculate linear approximation */
360 tmp = cos_derivative_tbl[k] * diff;
361 lsp[i] = cos_tbl[k] + (tmp >> 12);
362 }
363}
364
365static void get_lsp_poly(int16_t *lsp, int32_t *f)
366{
367 int16_t high, low;
368 int i, j, k, l;
369 int32_t tmp;
370
371 f[0] = 16777216;
372 f[1] = lsp[0] * -1024;
373
374 for (i = 2, k = 2, l = 2; i <= 5; i++, k += 2) {
375 f[l] = f[l - 2];
376
377 for (j = i; j > 1; j--, l--) {
378 high = f[l - 1] >> 16;
379 low = (f[l - 1] - (high << 16)) >> 1;
380
381 tmp = ((high * lsp[k]) << 2) + (((low * lsp[k]) >> 15) << 2);
382
383 f[l] += f[l - 2];
384 f[l] -= tmp;
385 }
386
387 f[l] -= lsp[k] << 10;
388 l += i;
389 }
390}
391
392static void lsf2poly(int16_t *a, int16_t *lsf)
393{
394 int32_t f[2][6];
395 int16_t lsp[10];
396 int32_t tmp;
397 int i;
398
399 lsf2lsp(lsf, lsp, LPC_FILTERORDER);
400
401 get_lsp_poly(&lsp[0], f[0]);
402 get_lsp_poly(&lsp[1], f[1]);
403
404 for (i = 5; i > 0; i--) {
405 f[0][i] += f[0][i - 1];
406 f[1][i] -= f[1][i - 1];
407 }
408
409 a[0] = 4096;
410 for (i = 5; i > 0; i--) {
411 tmp = f[0][6 - i] + f[1][6 - i];
412 a[6 - i] = (tmp + 4096) >> 13;
413
414 tmp = f[0][6 - i] - f[1][6 - i];
415 a[5 + i] = (tmp + 4096) >> 13;
416 }
417}
418
419static void lsp_interpolate2polydec(int16_t *a, int16_t *lsf1,
420 int16_t *lsf2, int coef, int length)
421{
422 int16_t lsftmp[LPC_FILTERORDER];
423
424 lsf_interpolate(lsftmp, lsf1, lsf2, coef, length);
425 lsf2poly(a, lsftmp);
426}
427
428static void bw_expand(int16_t *out, const int16_t *in, const int16_t *coef, int length)
429{
430 int i;
431
432 out[0] = in[0];
433 for (i = 1; i < length; i++)
434 out[i] = (coef[i] * in[i] + 16384) >> 15;
435}
436
437static void lsp_interpolate(int16_t *syntdenum, int16_t *weightdenum,
438 int16_t *lsfdeq, int16_t length,
439 ILBCContext *s)
440{
441 int16_t lp[LPC_FILTERORDER + 1], *lsfdeq2;
442 int i, pos, lp_length;
443
444 lsfdeq2 = lsfdeq + length;
445 lp_length = length + 1;
446
447 if (s->mode == 30) {
448 lsp_interpolate2polydec(lp, (*s).lsfdeqold, lsfdeq, lsf_weight_30ms[0], length);
449 memcpy(syntdenum, lp, lp_length * 2);
450 bw_expand(weightdenum, lp, kLpcChirpSyntDenum, lp_length);
451
452 pos = lp_length;
453 for (i = 1; i < 6; i++) {
454 lsp_interpolate2polydec(lp, lsfdeq, lsfdeq2,
455 lsf_weight_30ms[i],
456 length);
457 memcpy(syntdenum + pos, lp, lp_length * 2);
458 bw_expand(weightdenum + pos, lp, kLpcChirpSyntDenum, lp_length);
459 pos += lp_length;
460 }
461 } else {
462 pos = 0;
463 for (i = 0; i < s->nsub; i++) {
464 lsp_interpolate2polydec(lp, s->lsfdeqold, lsfdeq,
465 lsf_weight_20ms[i], length);
466 memcpy(syntdenum + pos, lp, lp_length * 2);
467 bw_expand(weightdenum + pos, lp, kLpcChirpSyntDenum, lp_length);
468 pos += lp_length;
469 }
470 }
471
472 if (s->mode == 30) {
473 memcpy(s->lsfdeqold, lsfdeq2, length * 2);
474 } else {
475 memcpy(s->lsfdeqold, lsfdeq, length * 2);
476 }
477}
478
479static void filter_mafq12(int16_t *in_ptr, int16_t *out_ptr,
480 int16_t *B, int16_t B_length,
481 int16_t length)
482{
483 int o, i, j;
484
485 for (i = 0; i < length; i++) {
486 const int16_t *b_ptr = &B[0];
487 const int16_t *x_ptr = &in_ptr[i];
488
489 o = 0;
490 for (j = 0; j < B_length; j++)
491 o += b_ptr[j] * *x_ptr--;
492
493 o = av_clip(o, -134217728, 134215679);
494
495 out_ptr[i] = ((o + 2048) >> 12);
496 }
497}
498
499static void filter_arfq12(const int16_t *data_in,
500 int16_t *data_out,
501 const int16_t *coefficients,
502 int coefficients_length,
503 int data_length)
504{
505 int i, j;
506
507 for (i = 0; i < data_length; i++) {
508 int output = 0, sum = 0;
509
510 for (j = coefficients_length - 1; j > 0; j--) {
511 sum += coefficients[j] * data_out[i - j];
512 }
513
514 output = coefficients[0] * data_in[i] - sum;
515 output = av_clip(output, -134217728, 134215679);
516
517 data_out[i] = (output + 2048) >> 12;
518 }
519}
520
521static void state_construct(int16_t ifm, int16_t *idx,
522 int16_t *synt_denum, int16_t *Out_fix,
523 int16_t len)
524{
525 int k;
526 int16_t maxVal;
527 int16_t *tmp1, *tmp2, *tmp3;
528 /* Stack based */
529 int16_t numerator[1 + LPC_FILTERORDER];
530 int16_t sampleValVec[2 * STATE_SHORT_LEN_30MS + LPC_FILTERORDER];
531 int16_t sampleMaVec[2 * STATE_SHORT_LEN_30MS + LPC_FILTERORDER];
532 int16_t *sampleVal = &sampleValVec[LPC_FILTERORDER];
533 int16_t *sampleMa = &sampleMaVec[LPC_FILTERORDER];
534 int16_t *sampleAr = &sampleValVec[LPC_FILTERORDER];
535
536 /* initialization of coefficients */
537
538 for (k = 0; k < LPC_FILTERORDER + 1; k++) {
539 numerator[k] = synt_denum[LPC_FILTERORDER - k];
540 }
541
542 /* decoding of the maximum value */
543
544 maxVal = frg_quant_mod[ifm];
545
546 /* decoding of the sample values */
547 tmp1 = sampleVal;
548 tmp2 = &idx[len - 1];
549
550 if (ifm < 37) {
551 for (k = 0; k < len; k++) {
552 /*the shifting is due to the Q13 in sq4_fixQ13[i], also the adding of 2097152 (= 0.5 << 22)
553 maxVal is in Q8 and result is in Q(-1) */
554 (*tmp1) = (int16_t) ((SPL_MUL_16_16(maxVal, ilbc_state[(*tmp2)]) + 2097152) >> 22);
555 tmp1++;
556 tmp2--;
557 }
558 } else if (ifm < 59) {
559 for (k = 0; k < len; k++) {
560 /*the shifting is due to the Q13 in sq4_fixQ13[i], also the adding of 262144 (= 0.5 << 19)
561 maxVal is in Q5 and result is in Q(-1) */
562 (*tmp1) = (int16_t) ((SPL_MUL_16_16(maxVal, ilbc_state[(*tmp2)]) + 262144) >> 19);
563 tmp1++;
564 tmp2--;
565 }
566 } else {
567 for (k = 0; k < len; k++) {
568 /*the shifting is due to the Q13 in sq4_fixQ13[i], also the adding of 65536 (= 0.5 << 17)
569 maxVal is in Q3 and result is in Q(-1) */
570 (*tmp1) = (int16_t) ((SPL_MUL_16_16(maxVal, ilbc_state[(*tmp2)]) + 65536) >> 17);
571 tmp1++;
572 tmp2--;
573 }
574 }
575
576 /* Set the rest of the data to zero */
577 memset(&sampleVal[len], 0, len * 2);
578
579 /* circular convolution with all-pass filter */
580
581 /* Set the state to zero */
582 memset(sampleValVec, 0, LPC_FILTERORDER * 2);
583
584 /* Run MA filter + AR filter */
585 filter_mafq12(sampleVal, sampleMa, numerator, LPC_FILTERORDER + 1, len + LPC_FILTERORDER);
586 memset(&sampleMa[len + LPC_FILTERORDER], 0, (len - LPC_FILTERORDER) * 2);
587 filter_arfq12(sampleMa, sampleAr, synt_denum, LPC_FILTERORDER + 1, 2 * len);
588
589 tmp1 = &sampleAr[len - 1];
590 tmp2 = &sampleAr[2 * len - 1];
591 tmp3 = Out_fix;
592 for (k = 0; k < len; k++) {
593 (*tmp3) = (*tmp1) + (*tmp2);
594 tmp1--;
595 tmp2--;
596 tmp3++;
597 }
598}
599
600static int16_t gain_dequantization(int index, int max_in, int stage)
601{
602 int16_t scale = FFMAX(1638, FFABS(max_in));
603
604 return ((scale * ilbc_gain[stage][index]) + 8192) >> 14;
605}
606
607static void vector_rmultiplication(int16_t *out, const int16_t *in,
608 const int16_t *win,
609 int length, int shift)
610{
611 for (int i = 0; i < length; i++)
612 out[i] = (in[i] * win[-i]) >> shift;
613}
614
615static void vector_multiplication(int16_t *out, const int16_t *in,
616 const int16_t *win, int length,
617 int shift)
618{
619 for (int i = 0; i < length; i++)
620 out[i] = (in[i] * win[i]) >> shift;
621}
622
623static void add_vector_and_shift(int16_t *out, const int16_t *in1,
624 const int16_t *in2, int length,
625 int shift)
626{
627 for (int i = 0; i < length; i++)
628 out[i] = (in1[i] + in2[i]) >> shift;
629}
630
631static void create_augmented_vector(int index, int16_t *buffer, int16_t *cbVec)
632{
633 int16_t cbVecTmp[4];
634 int16_t ilow = index - 4;
635
636 memcpy(cbVec, buffer - index, index * 2);
637
638 vector_multiplication(&cbVec[ilow], buffer - index - 4, alpha, 4, 15);
639 vector_rmultiplication(cbVecTmp, buffer - 4, &alpha[3], 4, 15);
640 add_vector_and_shift(&cbVec[ilow], &cbVec[ilow], cbVecTmp, 4, 0);
641
642 memcpy(cbVec + index, buffer - index, (SUBL - index) * sizeof(*cbVec));
643}
644
645static void get_codebook(int16_t * cbvec, /* (o) Constructed codebook vector */
646 int16_t * mem, /* (i) Codebook buffer */
647 int16_t index, /* (i) Codebook index */
648 int16_t lMem, /* (i) Length of codebook buffer */
649 int16_t cbveclen /* (i) Codebook vector length */
650)
651{
652 int16_t k, base_size;
653 int16_t lag;
654 /* Stack based */
655 int16_t tempbuff2[SUBL + 5];
656
657 /* Determine size of codebook sections */
658 base_size = lMem - cbveclen + 1;
659
660 if (cbveclen == SUBL) {
661 base_size += cbveclen / 2;
662 }
663
664 /* No filter -> First codebook section */
665 if (index < lMem - cbveclen + 1) {
666 /* first non-interpolated vectors */
667
668 k = index + cbveclen;
669 /* get vector */
670 memcpy(cbvec, mem + lMem - k, cbveclen * 2);
671 } else if (index < base_size) {
672
673 /* Calculate lag */
674
675 k = (int16_t) SPL_MUL_16_16(2, (index - (lMem - cbveclen + 1))) + cbveclen;
676
677 lag = k / 2;
678
679 create_augmented_vector(lag, mem + lMem, cbvec);
680 } else {
681 int16_t memIndTest;
682
683 /* first non-interpolated vectors */
684
685 if (index - base_size < lMem - cbveclen + 1) {
686
687 /* Set up filter memory, stuff zeros outside memory buffer */
688
689 memIndTest = lMem - (index - base_size + cbveclen);
690
691 memset(mem - CB_HALFFILTERLEN, 0, CB_HALFFILTERLEN * 2);
692 memset(mem + lMem, 0, CB_HALFFILTERLEN * 2);
693
694 /* do filtering to get the codebook vector */
695
696 filter_mafq12(&mem[memIndTest + 4], cbvec, (int16_t *) kCbFiltersRev, CB_FILTERLEN, cbveclen);
697 } else {
698 /* interpolated vectors */
699 /* Stuff zeros outside memory buffer */
700 memIndTest = lMem - cbveclen - CB_FILTERLEN;
701 memset(mem + lMem, 0, CB_HALFFILTERLEN * 2);
702
703 /* do filtering */
704 filter_mafq12(&mem[memIndTest + 7], tempbuff2, (int16_t *) kCbFiltersRev, CB_FILTERLEN, (int16_t) (cbveclen + 5));
705
706 /* Calculate lag index */
707 lag = (cbveclen << 1) - 20 + index - base_size - lMem - 1;
708
709 create_augmented_vector(lag, tempbuff2 + SUBL + 5, cbvec);
710 }
711 }
712}
713
714static void construct_vector (
715 int16_t *decvector, /* (o) Decoded vector */
716 int16_t *index, /* (i) Codebook indices */
717 int16_t *gain_index, /* (i) Gain quantization indices */
718 int16_t *mem, /* (i) Buffer for codevector construction */
719 int16_t lMem, /* (i) Length of buffer */
720 int16_t veclen)
721{
722 int16_t gain[CB_NSTAGES];
723 int16_t cbvec0[SUBL];
724 int16_t cbvec1[SUBL];
725 int16_t cbvec2[SUBL];
726 int32_t a32;
727 int16_t *gainPtr;
728 int j;
729
730 /* gain de-quantization */
731
732 gain[0] = gain_dequantization(gain_index[0], 16384, 0);
733 gain[1] = gain_dequantization(gain_index[1], gain[0], 1);
734 gain[2] = gain_dequantization(gain_index[2], gain[1], 2);
735
736 /* codebook vector construction and construction of total vector */
737
738 /* Stack based */
739 get_codebook(cbvec0, mem, index[0], lMem, veclen);
740 get_codebook(cbvec1, mem, index[1], lMem, veclen);
741 get_codebook(cbvec2, mem, index[2], lMem, veclen);
742
743 gainPtr = &gain[0];
744 for (j = 0; j < veclen; j++) {
745 a32 = SPL_MUL_16_16(*gainPtr++, cbvec0[j]);
746 a32 += SPL_MUL_16_16(*gainPtr++, cbvec1[j]);
747 a32 += SPL_MUL_16_16(*gainPtr, cbvec2[j]);
748 gainPtr -= 2;
749 decvector[j] = (a32 + 8192) >> 14;
750 }
751}
752
753static void reverse_memcpy(int16_t *dest, int16_t *source, int length)
754{
755 int16_t* destPtr = dest;
756 int16_t* sourcePtr = source;
757 int j;
758
759 for (j = 0; j < length; j++)
760 *destPtr-- = *sourcePtr++;
761}
762
763static void decode_residual(ILBCContext *s,
764 ILBCFrame *encbits,
765 int16_t *decresidual,
766 int16_t *syntdenum)
767{
768 int16_t meml_gotten, Nfor, Nback, diff, start_pos;
769 int16_t subcount, subframe;
770 int16_t *reverseDecresidual = s->enh_buf; /* Reversed decoded data, used for decoding backwards in time (reuse memory in state) */
771 int16_t *memVec = s->prevResidual;
772 int16_t *mem = &memVec[CB_HALFFILTERLEN]; /* Memory for codebook */
773
774 diff = STATE_LEN - s->state_short_len;
775
776 if (encbits->state_first == 1) {
777 start_pos = (encbits->start - 1) * SUBL;
778 } else {
779 start_pos = (encbits->start - 1) * SUBL + diff;
780 }
781
782 /* decode scalar part of start state */
783
784 state_construct(encbits->ifm, encbits->idx, &syntdenum[(encbits->start - 1) * (LPC_FILTERORDER + 1)], &decresidual[start_pos], s->state_short_len);
785
786 if (encbits->state_first) { /* put adaptive part in the end */
787 /* setup memory */
788 memset(mem, 0, (int16_t) (CB_MEML - s->state_short_len) * 2);
789 memcpy(mem + CB_MEML - s->state_short_len, decresidual + start_pos, s->state_short_len * 2);
790
791 /* construct decoded vector */
792
793 construct_vector(&decresidual[start_pos + s->state_short_len], encbits->cb_index, encbits->gain_index, mem + CB_MEML - ST_MEM_L_TBL, ST_MEM_L_TBL, (int16_t) diff);
794
795 } else { /* put adaptive part in the beginning */
796 /* setup memory */
797 meml_gotten = s->state_short_len;
798 reverse_memcpy(mem + CB_MEML - 1, decresidual + start_pos, meml_gotten);
799 memset(mem, 0, (int16_t) (CB_MEML - meml_gotten) * 2);
800
801 /* construct decoded vector */
802 construct_vector(reverseDecresidual, encbits->cb_index, encbits->gain_index, mem + CB_MEML - ST_MEM_L_TBL, ST_MEM_L_TBL, diff);
803
804 /* get decoded residual from reversed vector */
805 reverse_memcpy(&decresidual[start_pos - 1], reverseDecresidual, diff);
806 }
807
808 /* counter for predicted subframes */
809 subcount = 1;
810
811 /* forward prediction of subframes */
812 Nfor = s->nsub - encbits->start - 1;
813
814 if (Nfor > 0) {
815 /* setup memory */
816 memset(mem, 0, (CB_MEML - STATE_LEN) * 2);
817 memcpy(mem + CB_MEML - STATE_LEN, decresidual + (encbits->start - 1) * SUBL, STATE_LEN * 2);
818
819 /* loop over subframes to encode */
820 for (subframe = 0; subframe < Nfor; subframe++) {
821 /* construct decoded vector */
822 construct_vector(&decresidual[(encbits->start + 1 + subframe) * SUBL], encbits->cb_index + subcount * CB_NSTAGES, encbits->gain_index + subcount * CB_NSTAGES, mem, MEM_LF_TBL, SUBL);
823
824 /* update memory */
825 memmove(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(*mem));
826 memcpy(mem + CB_MEML - SUBL, &decresidual[(encbits->start + 1 + subframe) * SUBL], SUBL * 2);
827
828 subcount++;
829 }
830
831 }
832
833 /* backward prediction of subframes */
834 Nback = encbits->start - 1;
835
836 if (Nback > 0) {
837 /* setup memory */
838 meml_gotten = SUBL * (s->nsub + 1 - encbits->start);
839 if (meml_gotten > CB_MEML) {
840 meml_gotten = CB_MEML;
841 }
842
843 reverse_memcpy(mem + CB_MEML - 1, decresidual + (encbits->start - 1) * SUBL, meml_gotten);
844 memset(mem, 0, (int16_t) (CB_MEML - meml_gotten) * 2);
845
846 /* loop over subframes to decode */
847 for (subframe = 0; subframe < Nback; subframe++) {
848 /* construct decoded vector */
849 construct_vector(&reverseDecresidual[subframe * SUBL], encbits->cb_index + subcount * CB_NSTAGES,
850 encbits->gain_index + subcount * CB_NSTAGES, mem, MEM_LF_TBL, SUBL);
851
852 /* update memory */
853 memmove(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(*mem));
854 memcpy(mem + CB_MEML - SUBL, &reverseDecresidual[subframe * SUBL], SUBL * 2);
855
856 subcount++;
857 }
858
859 /* get decoded residual from reversed vector */
860 reverse_memcpy(decresidual + SUBL * Nback - 1, reverseDecresidual, SUBL * Nback);
861 }
862}
863
864static int16_t max_abs_value_w16(const int16_t* vector, int length)
865{
866 int i = 0, absolute = 0, maximum = 0;
867
868 if (vector == NULL || length <= 0) {
869 return -1;
870 }
871
872 for (i = 0; i < length; i++) {
873 absolute = FFABS(vector[i]);
874 if (absolute > maximum)
875 maximum = absolute;
876 }
877
878 // Guard the case for abs(-32768).
879 return FFMIN(maximum, INT16_MAX);
880}
881
882static int16_t get_size_in_bits(uint32_t n)
883{
884 int16_t bits;
885
886 if (0xFFFF0000 & n) {
887 bits = 16;
888 } else {
889 bits = 0;
890 }
891
892 if (0x0000FF00 & (n >> bits)) bits += 8;
893 if (0x000000F0 & (n >> bits)) bits += 4;
894 if (0x0000000C & (n >> bits)) bits += 2;
895 if (0x00000002 & (n >> bits)) bits += 1;
896 if (0x00000001 & (n >> bits)) bits += 1;
897
898 return bits;
899}
900
901static int32_t scale_dot_product(const int16_t *v1, const int16_t *v2, int length, int scaling)
902{
903 int32_t sum = 0;
904
905 for (int i = 0; i < length; i++)
906 sum += (v1[i] * v2[i]) >> scaling;
907
908 return sum;
909}
910
911static void correlation(int32_t *corr, int32_t *ener, int16_t *buffer,
912 int16_t lag, int16_t blen, int16_t srange, int16_t scale)
913{
914 int16_t *w16ptr;
915
916 w16ptr = &buffer[blen - srange - lag];
917
918 *corr = scale_dot_product(&buffer[blen - srange], w16ptr, srange, scale);
919 *ener = scale_dot_product(w16ptr, w16ptr, srange, scale);
920
921 if (*ener == 0) {
922 *corr = 0;
923 *ener = 1;
924 }
925}
926
927#define SPL_SHIFT_W32(x, c) (((c) >= 0) ? ((x) << (c)) : ((x) >> (-(c))))
928
929static int16_t norm_w32(int32_t a)
930{
931 if (a == 0) {
932 return 0;
933 } else if (a < 0) {
934 a = ~a;
935 }
936
937 return ff_clz(a);
938}
939
940static int32_t div_w32_w16(int32_t num, int16_t den)
941{
942 if (den != 0)
943 return num / den;
944 else
945 return 0x7FFFFFFF;
946}
947
948static void do_plc(int16_t *plc_residual, /* (o) concealed residual */
949 int16_t *plc_lpc, /* (o) concealed LP parameters */
950 int16_t PLI, /* (i) packet loss indicator
951 0 - no PL, 1 = PL */
952 int16_t *decresidual, /* (i) decoded residual */
953 int16_t *lpc, /* (i) decoded LPC (only used for no PL) */
954 int16_t inlag, /* (i) pitch lag */
955 ILBCContext *s) /* (i/o) decoder instance */
956{
957 int16_t i, pick;
958 int32_t cross, ener, cross_comp, ener_comp = 0;
959 int32_t measure, max_measure, energy;
960 int16_t max, cross_square_max, cross_square;
961 int16_t j, lag, tmp1, tmp2, randlag;
962 int16_t shift1, shift2, shift3, shift_max;
963 int16_t scale3;
964 int16_t corrLen;
965 int32_t tmpW32, tmp2W32;
966 int16_t use_gain;
967 int16_t tot_gain;
968 int16_t max_perSquare;
969 int16_t scale1, scale2;
970 int16_t totscale;
971 int32_t nom;
972 int16_t denom;
973 int16_t pitchfact;
974 int16_t use_lag;
975 int ind;
976 int16_t randvec[BLOCKL_MAX];
977
978 /* Packet Loss */
979 if (PLI == 1) {
980
981 s->consPLICount += 1;
982
983 /* if previous frame not lost,
984 determine pitch pred. gain */
985
986 if (s->prevPLI != 1) {
987
988 /* Maximum 60 samples are correlated, preserve as high accuracy
989 as possible without getting overflow */
990 max = max_abs_value_w16(s->prevResidual, s->block_samples);
991 scale3 = (get_size_in_bits(max) << 1) - 25;
992 if (scale3 < 0) {
993 scale3 = 0;
994 }
995
996 /* Store scale for use when interpolating between the
997 * concealment and the received packet */
998 s->prevScale = scale3;
999
1000 /* Search around the previous lag +/-3 to find the
1001 best pitch period */
1002 lag = inlag - 3;
1003
1004 /* Guard against getting outside the frame */
1005 corrLen = FFMIN(60, s->block_samples - (inlag + 3));
1006
1007 correlation(&cross, &ener, s->prevResidual, lag, s->block_samples, corrLen, scale3);
1008
1009 /* Normalize and store cross^2 and the number of shifts */
1010 shift_max = get_size_in_bits(FFABS(cross)) - 15;
1011 cross_square_max = (int16_t) SPL_MUL_16_16_RSFT(SPL_SHIFT_W32(cross, -shift_max), SPL_SHIFT_W32(cross, -shift_max), 15);
1012
1013 for (j = inlag - 2; j <= inlag + 3; j++) {
1014 correlation(&cross_comp, &ener_comp, s->prevResidual, j, s->block_samples, corrLen, scale3);
1015
1016 /* Use the criteria (corr*corr)/energy to compare if
1017 this lag is better or not. To avoid the division,
1018 do a cross multiplication */
1019 shift1 = get_size_in_bits(FFABS(cross_comp)) - 15;
1020 cross_square = (int16_t) SPL_MUL_16_16_RSFT(SPL_SHIFT_W32(cross_comp, -shift1), SPL_SHIFT_W32(cross_comp, -shift1), 15);
1021
1022 shift2 = get_size_in_bits(ener) - 15;
1023 measure = SPL_MUL_16_16(SPL_SHIFT_W32(ener, -shift2), cross_square);
1024
1025 shift3 = get_size_in_bits(ener_comp) - 15;
1026 max_measure = SPL_MUL_16_16(SPL_SHIFT_W32(ener_comp, -shift3), cross_square_max);
1027
1028 /* Calculate shift value, so that the two measures can
1029 be put in the same Q domain */
1030 if (((shift_max << 1) + shift3) > ((shift1 << 1) + shift2)) {
1031 tmp1 = FFMIN(31, (shift_max << 1) + shift3 - (shift1 << 1) - shift2);
1032 tmp2 = 0;
1033 } else {
1034 tmp1 = 0;
1035 tmp2 = FFMIN(31, (shift1 << 1) + shift2 - (shift_max << 1) - shift3);
1036 }
1037
1038 if ((measure >> tmp1) > (max_measure >> tmp2)) {
1039 /* New lag is better => record lag, measure and domain */
1040 lag = j;
1041 cross_square_max = cross_square;
1042 cross = cross_comp;
1043 shift_max = shift1;
1044 ener = ener_comp;
1045 }
1046 }
1047
1048 /* Calculate the periodicity for the lag with the maximum correlation.
1049
1050 Definition of the periodicity:
1051 abs(corr(vec1, vec2))/(sqrt(energy(vec1))*sqrt(energy(vec2)))
1052
1053 Work in the Square domain to simplify the calculations
1054 max_perSquare is less than 1 (in Q15)
1055 */
1056 tmp2W32 = scale_dot_product(&s->prevResidual[s->block_samples - corrLen], &s->prevResidual[s->block_samples - corrLen], corrLen, scale3);
1057
1058 if ((tmp2W32 > 0) && (ener_comp > 0)) {
1059 /* norm energies to int16_t, compute the product of the energies and
1060 use the upper int16_t as the denominator */
1061
1062 scale1 = norm_w32(tmp2W32) - 16;
1063 tmp1 = SPL_SHIFT_W32(tmp2W32, scale1);
1064
1065 scale2 = norm_w32(ener) - 16;
1066 tmp2 = SPL_SHIFT_W32(ener, scale2);
1067 denom = SPL_MUL_16_16_RSFT(tmp1, tmp2, 16); /* denom in Q(scale1+scale2-16) */
1068
1069 /* Square the cross correlation and norm it such that max_perSquare
1070 will be in Q15 after the division */
1071
1072 totscale = scale1 + scale2 - 1;
1073 tmp1 = SPL_SHIFT_W32(cross, (totscale >> 1));
1074 tmp2 = SPL_SHIFT_W32(cross, totscale - (totscale >> 1));
1075
1076 nom = SPL_MUL_16_16(tmp1, tmp2);
1077 max_perSquare = div_w32_w16(nom, denom);
1078 } else {
1079 max_perSquare = 0;
1080 }
1081 } else {
1082 /* previous frame lost, use recorded lag and gain */
1083 lag = s->prevLag;
1084 max_perSquare = s->per_square;
1085 }
1086
1087 /* Attenuate signal and scale down pitch pred gain if
1088 several frames lost consecutively */
1089
1090 use_gain = 32767; /* 1.0 in Q15 */
1091
1092 if (s->consPLICount * s->block_samples > 320) {
1093 use_gain = 29491; /* 0.9 in Q15 */
1094 } else if (s->consPLICount * s->block_samples > 640) {
1095 use_gain = 22938; /* 0.7 in Q15 */
1096 } else if (s->consPLICount * s->block_samples > 960) {
1097 use_gain = 16384; /* 0.5 in Q15 */
1098 } else if (s->consPLICount * s->block_samples > 1280) {
1099 use_gain = 0; /* 0.0 in Q15 */
1100 }
1101
1102 /* Compute mixing factor of picth repeatition and noise:
1103 for max_per>0.7 set periodicity to 1.0
1104 0.4<max_per<0.7 set periodicity to (maxper-0.4)/0.7-0.4)
1105 max_per<0.4 set periodicity to 0.0
1106 */
1107
1108 if (max_perSquare > 7868) { /* periodicity > 0.7 (0.7^4=0.2401 in Q15) */
1109 pitchfact = 32767;
1110 } else if (max_perSquare > 839) { /* 0.4 < periodicity < 0.7 (0.4^4=0.0256 in Q15) */
1111 /* find best index and interpolate from that */
1112 ind = 5;
1113 while ((max_perSquare < kPlcPerSqr[ind]) && (ind > 0)) {
1114 ind--;
1115 }
1116 /* pitch fact is approximated by first order */
1117 tmpW32 = kPlcPitchFact[ind] + SPL_MUL_16_16_RSFT(kPlcPfSlope[ind], (max_perSquare - kPlcPerSqr[ind]), 11);
1118
1119 pitchfact = FFMIN(tmpW32, 32767); /* guard against overflow */
1120
1121 } else { /* periodicity < 0.4 */
1122 pitchfact = 0;
1123 }
1124
1125 /* avoid repetition of same pitch cycle (buzzyness) */
1126 use_lag = lag;
1127 if (lag < 80) {
1128 use_lag = 2 * lag;
1129 }
1130
1131 /* compute concealed residual */
1132 energy = 0;
1133
1134 for (i = 0; i < s->block_samples; i++) {
1135 /* noise component - 52 < randlagFIX < 117 */
1136 s->seed = SPL_MUL_16_16(s->seed, 31821) + 13849;
1137 randlag = 53 + (s->seed & 63);
1138
1139 pick = i - randlag;
1140
1141 if (pick < 0) {
1142 randvec[i] = s->prevResidual[s->block_samples + pick];
1143 } else {
1144 randvec[i] = s->prevResidual[pick];
1145 }
1146
1147 /* pitch repeatition component */
1148 pick = i - use_lag;
1149
1150 if (pick < 0) {
1151 plc_residual[i] = s->prevResidual[s->block_samples + pick];
1152 } else {
1153 plc_residual[i] = plc_residual[pick];
1154 }
1155
1156 /* Attinuate total gain for each 10 ms */
1157 if (i < 80) {
1158 tot_gain = use_gain;
1159 } else if (i < 160) {
1160 tot_gain = SPL_MUL_16_16_RSFT(31130, use_gain, 15); /* 0.95*use_gain */
1161 } else {
1162 tot_gain = SPL_MUL_16_16_RSFT(29491, use_gain, 15); /* 0.9*use_gain */
1163 }
1164
1165 /* mix noise and pitch repeatition */
1166 plc_residual[i] = SPL_MUL_16_16_RSFT(tot_gain, (pitchfact * plc_residual[i] + (32767 - pitchfact) * randvec[i] + 16384) >> 15, 15);
1167
1168 /* Shifting down the result one step extra to ensure that no overflow
1169 will occur */
1170 energy += SPL_MUL_16_16_RSFT(plc_residual[i], plc_residual[i], (s->prevScale + 1));
1171
1172 }
1173
1174 /* less than 30 dB, use only noise */
1175 if (energy < SPL_SHIFT_W32(s->block_samples * 900, -s->prevScale - 1)) {
1176 energy = 0;
1177 for (i = 0; i < s->block_samples; i++) {
1178 plc_residual[i] = randvec[i];
1179 }
1180 }
1181
1182 /* use the old LPC */
1183 memcpy(plc_lpc, (*s).prev_lpc, (LPC_FILTERORDER + 1) * 2);
1184
1185 /* Update state in case there are multiple frame losses */
1186 s->prevLag = lag;
1187 s->per_square = max_perSquare;
1188 } else { /* no packet loss, copy input */
1189 memcpy(plc_residual, decresidual, s->block_samples * 2);
1190 memcpy(plc_lpc, lpc, (LPC_FILTERORDER + 1) * 2);
1191 s->consPLICount = 0;
1192 }
1193
1194 /* update state */
1195 s->prevPLI = PLI;
1196 memcpy(s->prev_lpc, plc_lpc, (LPC_FILTERORDER + 1) * 2);
1197 memcpy(s->prevResidual, plc_residual, s->block_samples * 2);
1198
1199 return;
1200}
1201
1202static int xcorr_coeff(int16_t *target, int16_t *regressor,
1203 int16_t subl, int16_t searchLen,
1204 int16_t offset, int16_t step)
1205{
1206 int16_t maxlag;
1207 int16_t pos;
1208 int16_t max;
1209 int16_t cross_corr_scale, energy_scale;
1210 int16_t cross_corr_sg_mod, cross_corr_sg_mod_max;
1211 int32_t cross_corr, energy;
1212 int16_t cross_corr_mod, energy_mod, enery_mod_max;
1213 int16_t *tp, *rp;
1214 int16_t *rp_beg, *rp_end;
1215 int16_t totscale, totscale_max;
1216 int16_t scalediff;
1217 int32_t new_crit, max_crit;
1218 int shifts;
1219 int k;
1220
1221 /* Initializations, to make sure that the first one is selected */
1222 cross_corr_sg_mod_max = 0;
1223 enery_mod_max = INT16_MAX;
1224 totscale_max = -500;
1225 maxlag = 0;
1226 pos = 0;
1227
1228 /* Find scale value and start position */
1229 if (step == 1) {
1230 max = max_abs_value_w16(regressor, (int16_t) (subl + searchLen - 1));
1231 rp_beg = regressor;
1232 rp_end = &regressor[subl];
1233 } else { /* step== -1 */
1234 max = max_abs_value_w16(&regressor[-searchLen], (int16_t) (subl + searchLen - 1));
1235 rp_beg = &regressor[-1];
1236 rp_end = &regressor[subl - 1];
1237 }
1238
1239 /* Introduce a scale factor on the energy in int32_t in
1240 order to make sure that the calculation does not
1241 overflow */
1242
1243 if (max > 5000) {
1244 shifts = 2;
1245 } else {
1246 shifts = 0;
1247 }
1248
1249 /* Calculate the first energy, then do a +/- to get the other energies */
1250 energy = scale_dot_product(regressor, regressor, subl, shifts);
1251
1252 for (k = 0; k < searchLen; k++) {
1253 tp = target;
1254 rp = &regressor[pos];
1255
1256 cross_corr = scale_dot_product(tp, rp, subl, shifts);
1257
1258 if ((energy > 0) && (cross_corr > 0)) {
1259 /* Put cross correlation and energy on 16 bit word */
1260 cross_corr_scale = norm_w32(cross_corr) - 16;
1261 cross_corr_mod = (int16_t) SPL_SHIFT_W32(cross_corr, cross_corr_scale);
1262 energy_scale = norm_w32(energy) - 16;
1263 energy_mod = (int16_t) SPL_SHIFT_W32(energy, energy_scale);
1264
1265 /* Square cross correlation and store upper int16_t */
1266 cross_corr_sg_mod = (int16_t) SPL_MUL_16_16_RSFT(cross_corr_mod, cross_corr_mod, 16);
1267
1268 /* Calculate the total number of (dynamic) right shifts that have
1269 been performed on (cross_corr*cross_corr)/energy
1270 */
1271 totscale = energy_scale - (cross_corr_scale << 1);
1272
1273 /* Calculate the shift difference in order to be able to compare the two
1274 (cross_corr*cross_corr)/energy in the same domain
1275 */
1276 scalediff = totscale - totscale_max;
1277 scalediff = FFMIN(scalediff, 31);
1278 scalediff = FFMAX(scalediff, -31);
1279
1280 /* Compute the cross multiplication between the old best criteria
1281 and the new one to be able to compare them without using a
1282 division */
1283
1284 if (scalediff < 0) {
1285 new_crit = ((int32_t) cross_corr_sg_mod * enery_mod_max) >> (-scalediff);
1286 max_crit = ((int32_t) cross_corr_sg_mod_max * energy_mod);
1287 } else {
1288 new_crit = ((int32_t) cross_corr_sg_mod * enery_mod_max);
1289 max_crit = ((int32_t) cross_corr_sg_mod_max * energy_mod) >> scalediff;
1290 }
1291
1292 /* Store the new lag value if the new criteria is larger
1293 than previous largest criteria */
1294
1295 if (new_crit > max_crit) {
1296 cross_corr_sg_mod_max = cross_corr_sg_mod;
1297 enery_mod_max = energy_mod;
1298 totscale_max = totscale;
1299 maxlag = k;
1300 }
1301 }
1302 pos += step;
1303
1304 /* Do a +/- to get the next energy */
1305 energy += step * ((*rp_end * *rp_end - *rp_beg * *rp_beg) >> shifts);
1306 rp_beg += step;
1307 rp_end += step;
1308 }
1309
1310 return maxlag + offset;
1311}
1312
1313static void hp_output(int16_t *signal, const int16_t *ba, int16_t *y,
1314 int16_t *x, int16_t len)
1315{
1316 int32_t tmp;
1317
1318 for (int i = 0; i < len; i++) {
1319 tmp = SPL_MUL_16_16(y[1], ba[3]); /* (-a[1])*y[i-1] (low part) */
1320 tmp += SPL_MUL_16_16(y[3], ba[4]); /* (-a[2])*y[i-2] (low part) */
1321 tmp = (tmp >> 15);
1322 tmp += SPL_MUL_16_16(y[0], ba[3]); /* (-a[1])*y[i-1] (high part) */
1323 tmp += SPL_MUL_16_16(y[2], ba[4]); /* (-a[2])*y[i-2] (high part) */
1324 tmp = (tmp << 1);
1325
1326 tmp += SPL_MUL_16_16(signal[i], ba[0]); /* b[0]*x[0] */
1327 tmp += SPL_MUL_16_16(x[0], ba[1]); /* b[1]*x[i-1] */
1328 tmp += SPL_MUL_16_16(x[1], ba[2]); /* b[2]*x[i-2] */
1329
1330 /* Update state (input part) */
1331 x[1] = x[0];
1332 x[0] = signal[i];
1333
1334 /* Convert back to Q0 and multiply with 2 */
1335 signal[i] = av_clip_intp2(tmp + 1024, 26) >> 11;
1336
1337 /* Update state (filtered part) */
1338 y[2] = y[0];
1339 y[3] = y[1];
1340
1341 /* upshift tmp by 3 with saturation */
1342 if (tmp > 268435455) {
1343 tmp = INT32_MAX;
1344 } else if (tmp < -268435456) {
1345 tmp = INT32_MIN;
1346 } else {
1347 tmp = tmp << 3;
1348 }
1349
1350 y[0] = tmp >> 16;
1351 y[1] = (tmp - (y[0] << 16)) >> 1;
1352 }
1353}
1354
1355static int ilbc_decode_frame(AVCodecContext *avctx, void *data,
1356 int *got_frame_ptr, AVPacket *avpkt)
1357{
1358 const uint8_t *buf = avpkt->data;
1359 AVFrame *frame = data;
1360 ILBCContext *s = avctx->priv_data;
1361 int mode = s->mode, ret;
1362 int16_t *plc_data = &s->plc_residual[LPC_FILTERORDER];
1363
1364 if ((ret = init_get_bits8(&s->gb, buf, avpkt->size)) < 0)
1365 return ret;
1366 memset(&s->frame, 0, sizeof(ILBCFrame));
1367
1368 frame->nb_samples = s->block_samples;
1369 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1370 return ret;
1371
1372 if (unpack_frame(s))
1373 mode = 0;
1374 if (s->frame.start < 1)
1375 mode = 0;
1376
1377 if (mode) {
1378 index_conv(s->frame.cb_index);
1379
1380 lsf_dequantization(s->lsfdeq, s->frame.lsf, s->lpc_n);
1381 lsf_check_stability(s->lsfdeq, LPC_FILTERORDER, s->lpc_n);
1382 lsp_interpolate(s->syntdenum, s->weightdenum,
1383 s->lsfdeq, LPC_FILTERORDER, s);
1384 decode_residual(s, &s->frame, s->decresidual, s->syntdenum);
1385
1386 do_plc(s->plc_residual, s->plc_lpc, 0,
1387 s->decresidual, s->syntdenum + (LPC_FILTERORDER + 1) * (s->nsub - 1),
1388 s->last_lag, s);
1389
1390 memcpy(s->decresidual, s->plc_residual, s->block_samples * 2);
1391 }
1392
1393 if (s->enhancer) {
1394 /* TODO */
1395 } else {
1396 int16_t lag, i;
1397
1398 /* Find last lag (since the enhancer is not called to give this info) */
1399 if (s->mode == 20) {
1400 lag = xcorr_coeff(&s->decresidual[s->block_samples-60], &s->decresidual[s->block_samples-80],
1401 60, 80, 20, -1);
1402 } else {
1403 lag = xcorr_coeff(&s->decresidual[s->block_samples-ENH_BLOCKL],
1404 &s->decresidual[s->block_samples-ENH_BLOCKL-20],
1405 ENH_BLOCKL, 100, 20, -1);
1406 }
1407
1408 /* Store lag (it is needed if next packet is lost) */
1409 s->last_lag = lag;
1410
1411 /* copy data and run synthesis filter */
1412 memcpy(plc_data, s->decresidual, s->block_samples * 2);
1413
1414 /* Set up the filter state */
1415 memcpy(&plc_data[-LPC_FILTERORDER], s->syntMem, LPC_FILTERORDER * 2);
1416
1417 for (i = 0; i < s->nsub; i++) {
1418 filter_arfq12(plc_data+i*SUBL, plc_data+i*SUBL,
1419 s->syntdenum + i*(LPC_FILTERORDER + 1),
1420 LPC_FILTERORDER + 1, SUBL);
1421 }
1422
1423 /* Save the filter state */
1424 memcpy(s->syntMem, &plc_data[s->block_samples-LPC_FILTERORDER], LPC_FILTERORDER * 2);
1425 }
1426
1427 memcpy(frame->data[0], plc_data, s->block_samples * 2);
1428
1429 hp_output((int16_t *)frame->data[0], hp_out_coeffs,
1430 s->hpimemy, s->hpimemx, s->block_samples);
1431
1432 memcpy(s->old_syntdenum, s->syntdenum, s->nsub*(LPC_FILTERORDER + 1) * 2);
1433
1434 s->prev_enh_pl = 0;
1435 if (mode == 0)
1436 s->prev_enh_pl = 1;
1437
1438 *got_frame_ptr = 1;
1439
1440 return avpkt->size;
1441}
1442
1443static av_cold int ilbc_decode_init(AVCodecContext *avctx)
1444{
1445 ILBCContext *s = avctx->priv_data;
1446
1447 if (avctx->block_align == 38)
1448 s->mode = 20;
1449 else if (avctx->block_align == 50)
1450 s->mode = 30;
1451 else if (avctx->bit_rate > 0)
1452 s->mode = avctx->bit_rate <= 14000 ? 30 : 20;
1453 else
1454 return AVERROR_INVALIDDATA;
1455
1456 avctx->channels = 1;
1457 avctx->channel_layout = AV_CH_LAYOUT_MONO;
1458 avctx->sample_rate = 8000;
1459 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
1460
1461 if (s->mode == 30) {
1462 s->block_samples = 240;
1463 s->nsub = NSUB_30MS;
1464 s->nasub = NASUB_30MS;
1465 s->lpc_n = LPC_N_30MS;
1466 s->state_short_len = STATE_SHORT_LEN_30MS;
1467 } else {
1468 s->block_samples = 160;
1469 s->nsub = NSUB_20MS;
1470 s->nasub = NASUB_20MS;
1471 s->lpc_n = LPC_N_20MS;
1472 s->state_short_len = STATE_SHORT_LEN_20MS;
1473 }
1474
1475 return 0;
1476}
1477
1478AVCodec ff_ilbc_decoder = {
1479 .name = "ilbc",
1480 .long_name = NULL_IF_CONFIG_SMALL("iLBC (Internet Low Bitrate Codec)"),
1481 .type = AVMEDIA_TYPE_AUDIO,
1482 .id = AV_CODEC_ID_ILBC,
1483 .init = ilbc_decode_init,
1484 .decode = ilbc_decode_frame,
1485 .capabilities = AV_CODEC_CAP_DR1,
1486 .priv_data_size = sizeof(ILBCContext),
1487};
libavcodec/version.h+1−1
@@ -28,7 +28,7 @@ · 27 unmodified lines
28#include "libavutil/version.h"28#include "libavutil/version.h"
29 29
30#define LIBAVCODEC_VERSION_MAJOR 5830#define LIBAVCODEC_VERSION_MAJOR 58
31#define LIBAVCODEC_VERSION_MINOR 3031#define LIBAVCODEC_VERSION_MINOR 31
32#define LIBAVCODEC_VERSION_MICRO 10032#define LIBAVCODEC_VERSION_MICRO 100
33 33
34#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \34#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
tests/ref/fate/source+2−0
@@ -2,6 +2,8 @@ Files without standard license headers: · 1 unmodified line
2compat/avisynth/windowsPorts/basicDataTypeConversions.h2compat/avisynth/windowsPorts/basicDataTypeConversions.h
3compat/avisynth/windowsPorts/windows2linux.h3compat/avisynth/windowsPorts/windows2linux.h
4libavcodec/file_open.c4libavcodec/file_open.c
5libavcodec/ilbcdata.h
6libavcodec/ilbcdec.c
5libavcodec/interplayacm.c7libavcodec/interplayacm.c
6libavcodec/log2_tab.c8libavcodec/log2_tab.c
7libavcodec/reverse.c9libavcodec/reverse.c

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t