/src/ffmpeg/libavcodec/dss_sp.c
Line | Count | Source |
1 | | /* |
2 | | * Digital Speech Standard - Standard Play mode (DSS SP) audio decoder. |
3 | | * Copyright (C) 2014 Oleksij Rempel <linux@rempel-privat.de> |
4 | | * |
5 | | * This file is part of FFmpeg. |
6 | | * |
7 | | * FFmpeg is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * FFmpeg is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with FFmpeg; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | #include "libavutil/channel_layout.h" |
23 | | #include "libavutil/common.h" |
24 | | #include "libavutil/mem_internal.h" |
25 | | |
26 | | #include "avcodec.h" |
27 | | #include "codec_internal.h" |
28 | | #include "decode.h" |
29 | | #include "get_bits.h" |
30 | | |
31 | 109M | #define SUBFRAMES 4 |
32 | 2.90M | #define PULSE_MAX 8 |
33 | | |
34 | 9.44M | #define DSS_SP_FRAME_SIZE 42 |
35 | 362k | #define DSS_SP_SAMPLE_COUNT (66 * SUBFRAMES) |
36 | 139M | #define DSS_SP_FORMULA(a, b, c) ((int)((((a) * (1 << 15)) + (b) * (unsigned)(c)) + 0x4000) >> 15) |
37 | | |
38 | | typedef struct DssSpSubframe { |
39 | | int16_t gain; |
40 | | int32_t combined_pulse_pos; |
41 | | int16_t pulse_pos[7]; |
42 | | int16_t pulse_val[7]; |
43 | | } DssSpSubframe; |
44 | | |
45 | | typedef struct DssSpFrame { |
46 | | int16_t filter_idx[14]; |
47 | | int16_t sf_adaptive_gain[SUBFRAMES]; |
48 | | int16_t pitch_lag[SUBFRAMES]; |
49 | | struct DssSpSubframe sf[SUBFRAMES]; |
50 | | } DssSpFrame; |
51 | | |
52 | | typedef struct DssSpContext { |
53 | | AVCodecContext *avctx; |
54 | | int32_t excitation[288 + 6]; |
55 | | int32_t history[187]; |
56 | | DssSpFrame fparam; |
57 | | int32_t working_buffer[SUBFRAMES][72]; |
58 | | int32_t audio_buf[15]; |
59 | | int32_t err_buf1[15]; |
60 | | int32_t lpc_filter[14]; |
61 | | int32_t filter[15]; |
62 | | int32_t vector_buf[72]; |
63 | | int noise_state; |
64 | | int32_t err_buf2[15]; |
65 | | |
66 | | int pulse_dec_mode; |
67 | | |
68 | | DECLARE_ALIGNED(16, uint8_t, bits)[DSS_SP_FRAME_SIZE + |
69 | | AV_INPUT_BUFFER_PADDING_SIZE]; |
70 | | } DssSpContext; |
71 | | |
72 | | /* |
73 | | * Used for the coding/decoding of the pulse positions for the MP-MLQ codebook. |
74 | | */ |
75 | | static const uint32_t dss_sp_combinatorial_table[PULSE_MAX][72] = { |
76 | | { 0, 0, 0, 0, 0, 0, |
77 | | 0, 0, 0, 0, 0, 0, |
78 | | 0, 0, 0, 0, 0, 0, |
79 | | 0, 0, 0, 0, 0, 0, |
80 | | 0, 0, 0, 0, 0, 0, |
81 | | 0, 0, 0, 0, 0, 0, |
82 | | 0, 0, 0, 0, 0, 0, |
83 | | 0, 0, 0, 0, 0, 0, |
84 | | 0, 0, 0, 0, 0, 0, |
85 | | 0, 0, 0, 0, 0, 0, |
86 | | 0, 0, 0, 0, 0, 0, |
87 | | 0, 0, 0, 0, 0, 0 }, |
88 | | { 0, 1, 2, 3, 4, 5, |
89 | | 6, 7, 8, 9, 10, 11, |
90 | | 12, 13, 14, 15, 16, 17, |
91 | | 18, 19, 20, 21, 22, 23, |
92 | | 24, 25, 26, 27, 28, 29, |
93 | | 30, 31, 32, 33, 34, 35, |
94 | | 36, 37, 38, 39, 40, 41, |
95 | | 42, 43, 44, 45, 46, 47, |
96 | | 48, 49, 50, 51, 52, 53, |
97 | | 54, 55, 56, 57, 58, 59, |
98 | | 60, 61, 62, 63, 64, 65, |
99 | | 66, 67, 68, 69, 70, 71 }, |
100 | | { 0, 0, 1, 3, 6, 10, |
101 | | 15, 21, 28, 36, 45, 55, |
102 | | 66, 78, 91, 105, 120, 136, |
103 | | 153, 171, 190, 210, 231, 253, |
104 | | 276, 300, 325, 351, 378, 406, |
105 | | 435, 465, 496, 528, 561, 595, |
106 | | 630, 666, 703, 741, 780, 820, |
107 | | 861, 903, 946, 990, 1035, 1081, |
108 | | 1128, 1176, 1225, 1275, 1326, 1378, |
109 | | 1431, 1485, 1540, 1596, 1653, 1711, |
110 | | 1770, 1830, 1891, 1953, 2016, 2080, |
111 | | 2145, 2211, 2278, 2346, 2415, 2485 }, |
112 | | { 0, 0, 0, 1, 4, 10, |
113 | | 20, 35, 56, 84, 120, 165, |
114 | | 220, 286, 364, 455, 560, 680, |
115 | | 816, 969, 1140, 1330, 1540, 1771, |
116 | | 2024, 2300, 2600, 2925, 3276, 3654, |
117 | | 4060, 4495, 4960, 5456, 5984, 6545, |
118 | | 7140, 7770, 8436, 9139, 9880, 10660, |
119 | | 11480, 12341, 13244, 14190, 15180, 16215, |
120 | | 17296, 18424, 19600, 20825, 22100, 23426, |
121 | | 24804, 26235, 27720, 29260, 30856, 32509, |
122 | | 34220, 35990, 37820, 39711, 41664, 43680, |
123 | | 45760, 47905, 50116, 52394, 54740, 57155 }, |
124 | | { 0, 0, 0, 0, 1, 5, |
125 | | 15, 35, 70, 126, 210, 330, |
126 | | 495, 715, 1001, 1365, 1820, 2380, |
127 | | 3060, 3876, 4845, 5985, 7315, 8855, |
128 | | 10626, 12650, 14950, 17550, 20475, 23751, |
129 | | 27405, 31465, 35960, 40920, 46376, 52360, |
130 | | 58905, 66045, 73815, 82251, 91390, 101270, |
131 | | 111930, 123410, 135751, 148995, 163185, 178365, |
132 | | 194580, 211876, 230300, 249900, 270725, 292825, |
133 | | 316251, 341055, 367290, 395010, 424270, 455126, |
134 | | 487635, 521855, 557845, 595665, 635376, 677040, |
135 | | 720720, 766480, 814385, 864501, 916895, 971635 }, |
136 | | { 0, 0, 0, 0, 0, 1, |
137 | | 6, 21, 56, 126, 252, 462, |
138 | | 792, 1287, 2002, 3003, 4368, 6188, |
139 | | 8568, 11628, 15504, 20349, 26334, 33649, |
140 | | 42504, 53130, 65780, 80730, 98280, 118755, |
141 | | 142506, 169911, 201376, 237336, 278256, 324632, |
142 | | 376992, 435897, 501942, 575757, 658008, 749398, |
143 | | 850668, 962598, 1086008, 1221759, 1370754, 1533939, |
144 | | 1712304, 1906884, 2118760, 2349060, 2598960, 2869685, |
145 | | 3162510, 3478761, 3819816, 4187106, 4582116, 5006386, |
146 | | 5461512, 5949147, 6471002, 7028847, 7624512, 8259888, |
147 | | 8936928, 9657648, 10424128, 11238513, 12103014, 13019909 }, |
148 | | { 0, 0, 0, 0, 0, 0, |
149 | | 1, 7, 28, 84, 210, 462, |
150 | | 924, 1716, 3003, 5005, 8008, 12376, |
151 | | 18564, 27132, 38760, 54264, 74613, 100947, |
152 | | 134596, 177100, 230230, 296010, 376740, 475020, |
153 | | 593775, 736281, 906192, 1107568, 1344904, 1623160, |
154 | | 1947792, 2324784, 2760681, 3262623, 3838380, 4496388, |
155 | | 5245786, 6096454, 7059052, 8145060, 9366819, 10737573, |
156 | | 12271512, 13983816, 15890700, 18009460, 20358520, 22957480, |
157 | | 25827165, 28989675, 32468436, 36288252, 40475358, 45057474, |
158 | | 50063860, 55525372, 61474519, 67945521, 74974368, 82598880, |
159 | | 90858768, 99795696, 109453344, 119877472, 131115985, 143218999 }, |
160 | | { 0, 0, 0, 0, 0, 0, |
161 | | 0, 1, 8, 36, 120, 330, |
162 | | 792, 1716, 3432, 6435, 11440, 19448, |
163 | | 31824, 50388, 77520, 116280, 170544, 245157, |
164 | | 346104, 480700, 657800, 888030, 1184040, 1560780, |
165 | | 2035800, 2629575, 3365856, 4272048, 5379616, 6724520, |
166 | | 8347680, 10295472, 12620256, 15380937, 18643560, 22481940, |
167 | | 26978328, 32224114, 38320568, 45379620, 53524680, 62891499, |
168 | | 73629072, 85900584, 99884400, 115775100, 133784560, 154143080, |
169 | | 177100560, 202927725, 231917400, 264385836, 300674088, 341149446, |
170 | | 386206920, 436270780, 491796152, 553270671, 621216192, 696190560, |
171 | | 778789440, 869648208, 969443904, 1078897248, 1198774720, 1329890705 }, |
172 | | }; |
173 | | |
174 | | static const int16_t dss_sp_filter_cb[14][32] = { |
175 | | { -32653, -32587, -32515, -32438, -32341, -32216, -32062, -31881, |
176 | | -31665, -31398, -31080, -30724, -30299, -29813, -29248, -28572, |
177 | | -27674, -26439, -24666, -22466, -19433, -16133, -12218, -7783, |
178 | | -2834, 1819, 6544, 11260, 16050, 20220, 24774, 28120 }, |
179 | | |
180 | | { -27503, -24509, -20644, -17496, -14187, -11277, -8420, -5595, |
181 | | -3013, -624, 1711, 3880, 5844, 7774, 9739, 11592, |
182 | | 13364, 14903, 16426, 17900, 19250, 20586, 21803, 23006, |
183 | | 24142, 25249, 26275, 27300, 28359, 29249, 30118, 31183 }, |
184 | | |
185 | | { -27827, -24208, -20943, -17781, -14843, -11848, -9066, -6297, |
186 | | -3660, -910, 1918, 5025, 8223, 11649, 15086, 18423, |
187 | | 0, 0, 0, 0, 0, 0, 0, 0, |
188 | | 0, 0, 0, 0, 0, 0, 0, 0 }, |
189 | | |
190 | | { -17128, -11975, -8270, -5123, -2296, 183, 2503, 4707, |
191 | | 6798, 8945, 11045, 13239, 15528, 18248, 21115, 24785, |
192 | | 0, 0, 0, 0, 0, 0, 0, 0, |
193 | | 0, 0, 0, 0, 0, 0, 0, 0 }, |
194 | | |
195 | | { -21557, -17280, -14286, -11644, -9268, -7087, -4939, -2831, |
196 | | -691, 1407, 3536, 5721, 8125, 10677, 13721, 17731, |
197 | | 0, 0, 0, 0, 0, 0, 0, 0, |
198 | | 0, 0, 0, 0, 0, 0, 0, 0 }, |
199 | | |
200 | | { -15030, -10377, -7034, -4327, -1900, 364, 2458, 4450, |
201 | | 6422, 8374, 10374, 12486, 14714, 16997, 19626, 22954, |
202 | | 0, 0, 0, 0, 0, 0, 0, 0, |
203 | | 0, 0, 0, 0, 0, 0, 0, 0 }, |
204 | | |
205 | | { -16155, -12362, -9698, -7460, -5258, -3359, -1547, 219, |
206 | | 1916, 3599, 5299, 6994, 8963, 11226, 13716, 16982, |
207 | | 0, 0, 0, 0, 0, 0, 0, 0, |
208 | | 0, 0, 0, 0, 0, 0, 0, 0 }, |
209 | | |
210 | | { -14742, -9848, -6921, -4648, -2769, -1065, 499, 2083, |
211 | | 3633, 5219, 6857, 8580, 10410, 12672, 15561, 20101, |
212 | | 0, 0, 0, 0, 0, 0, 0, 0, |
213 | | 0, 0, 0, 0, 0, 0, 0, 0 }, |
214 | | |
215 | | { -11099, -7014, -3855, -1025, 1680, 4544, 7807, 11932, |
216 | | 0, 0, 0, 0, 0, 0, 0, 0, |
217 | | 0, 0, 0, 0, 0, 0, 0, 0, |
218 | | 0, 0, 0, 0, 0, 0, 0, 0 }, |
219 | | |
220 | | { -9060, -4570, -1381, 1419, 4034, 6728, 9865, 14149, |
221 | | 0, 0, 0, 0, 0, 0, 0, 0, |
222 | | 0, 0, 0, 0, 0, 0, 0, 0, |
223 | | 0, 0, 0, 0, 0, 0, 0, 0 }, |
224 | | |
225 | | { -12450, -7985, -4596, -1734, 961, 3629, 6865, 11142, |
226 | | 0, 0, 0, 0, 0, 0, 0, 0, |
227 | | 0, 0, 0, 0, 0, 0, 0, 0, |
228 | | 0, 0, 0, 0, 0, 0, 0, 0 }, |
229 | | |
230 | | { -11831, -7404, -4010, -1096, 1606, 4291, 7386, 11482, |
231 | | 0, 0, 0, 0, 0, 0, 0, 0, |
232 | | 0, 0, 0, 0, 0, 0, 0, 0, |
233 | | 0, 0, 0, 0, 0, 0, 0, 0 }, |
234 | | |
235 | | { -13404, -9250, -5995, -3312, -890, 1594, 4464, 8198, |
236 | | 0, 0, 0, 0, 0, 0, 0, 0, |
237 | | 0, 0, 0, 0, 0, 0, 0, 0, |
238 | | 0, 0, 0, 0, 0, 0, 0, 0 }, |
239 | | |
240 | | { -11239, -7220, -4040, -1406, 971, 3321, 6006, 9697, |
241 | | 0, 0, 0, 0, 0, 0, 0, 0, |
242 | | 0, 0, 0, 0, 0, 0, 0, 0, |
243 | | 0, 0, 0, 0, 0, 0, 0, 0 }, |
244 | | }; |
245 | | |
246 | | static const uint16_t dss_sp_fixed_cb_gain[64] = { |
247 | | 0, 4, 8, 13, 17, 22, 26, 31, |
248 | | 35, 40, 44, 48, 53, 58, 63, 69, |
249 | | 76, 83, 91, 99, 109, 119, 130, 142, |
250 | | 155, 170, 185, 203, 222, 242, 265, 290, |
251 | | 317, 346, 378, 414, 452, 494, 540, 591, |
252 | | 646, 706, 771, 843, 922, 1007, 1101, 1204, |
253 | | 1316, 1438, 1572, 1719, 1879, 2053, 2244, 2453, |
254 | | 2682, 2931, 3204, 3502, 3828, 4184, 4574, 5000, |
255 | | }; |
256 | | |
257 | | static const int16_t dss_sp_pulse_val[8] = { |
258 | | -31182, -22273, -13364, -4455, 4455, 13364, 22273, 31182 |
259 | | }; |
260 | | |
261 | | static const uint16_t binary_decreasing_array[] = { |
262 | | 32767, 16384, 8192, 4096, 2048, 1024, 512, 256, |
263 | | 128, 64, 32, 16, 8, 4, 2, |
264 | | }; |
265 | | |
266 | | static const uint16_t dss_sp_unc_decreasing_array[] = { |
267 | | 32767, 26214, 20972, 16777, 13422, 10737, 8590, 6872, |
268 | | 5498, 4398, 3518, 2815, 2252, 1801, 1441, |
269 | | }; |
270 | | |
271 | | static const uint16_t dss_sp_adaptive_gain[] = { |
272 | | 102, 231, 360, 488, 617, 746, 875, 1004, |
273 | | 1133, 1261, 1390, 1519, 1648, 1777, 1905, 2034, |
274 | | 2163, 2292, 2421, 2550, 2678, 2807, 2936, 3065, |
275 | | 3194, 3323, 3451, 3580, 3709, 3838, 3967, 4096, |
276 | | }; |
277 | | |
278 | | static const int32_t dss_sp_sinc[67] = { |
279 | | 262, 293, 323, 348, 356, 336, 269, 139, |
280 | | -67, -358, -733, -1178, -1668, -2162, -2607, -2940, |
281 | | -3090, -2986, -2562, -1760, -541, 1110, 3187, 5651, |
282 | | 8435, 11446, 14568, 17670, 20611, 23251, 25460, 27125, |
283 | | 28160, 28512, 28160, |
284 | | 27125, 25460, 23251, 20611, 17670, 14568, 11446, 8435, |
285 | | 5651, 3187, 1110, -541, -1760, -2562, -2986, -3090, |
286 | | -2940, -2607, -2162, -1668, -1178, -733, -358, -67, |
287 | | 139, 269, 336, 356, 348, 323, 293, 262, |
288 | | }; |
289 | | |
290 | | static av_cold int dss_sp_decode_init(AVCodecContext *avctx) |
291 | 1.03k | { |
292 | 1.03k | DssSpContext *p = avctx->priv_data; |
293 | 1.03k | avctx->sample_fmt = AV_SAMPLE_FMT_S16; |
294 | 1.03k | avctx->sample_rate = 11025; |
295 | 1.03k | av_channel_layout_uninit(&avctx->ch_layout); |
296 | 1.03k | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; |
297 | | |
298 | 1.03k | p->pulse_dec_mode = 1; |
299 | 1.03k | p->avctx = avctx; |
300 | | |
301 | 1.03k | return 0; |
302 | 1.03k | } |
303 | | |
304 | | static void dss_sp_unpack_coeffs(DssSpContext *p, const uint8_t *src) |
305 | 362k | { |
306 | 362k | GetBitContext gb; |
307 | 362k | DssSpFrame *fparam = &p->fparam; |
308 | 362k | int i; |
309 | 362k | int subframe_idx; |
310 | 362k | uint32_t combined_pitch; |
311 | 362k | uint32_t tmp; |
312 | 362k | uint32_t pitch_lag; |
313 | | |
314 | 7.97M | for (i = 0; i < DSS_SP_FRAME_SIZE; i += 2) { |
315 | 7.61M | p->bits[i] = src[i + 1]; |
316 | 7.61M | p->bits[i + 1] = src[i]; |
317 | 7.61M | } |
318 | | |
319 | 362k | init_get_bits(&gb, p->bits, DSS_SP_FRAME_SIZE * 8); |
320 | | |
321 | 1.08M | for (i = 0; i < 2; i++) |
322 | 725k | fparam->filter_idx[i] = get_bits(&gb, 5); |
323 | 2.53M | for (; i < 8; i++) |
324 | 2.17M | fparam->filter_idx[i] = get_bits(&gb, 4); |
325 | 2.53M | for (; i < 14; i++) |
326 | 2.17M | fparam->filter_idx[i] = get_bits(&gb, 3); |
327 | | |
328 | 1.81M | for (subframe_idx = 0; subframe_idx < 4; subframe_idx++) { |
329 | 1.45M | fparam->sf_adaptive_gain[subframe_idx] = get_bits(&gb, 5); |
330 | | |
331 | 1.45M | fparam->sf[subframe_idx].combined_pulse_pos = get_bits_long(&gb, 31); |
332 | | |
333 | 1.45M | fparam->sf[subframe_idx].gain = get_bits(&gb, 6); |
334 | | |
335 | 11.6M | for (i = 0; i < 7; i++) |
336 | 10.1M | fparam->sf[subframe_idx].pulse_val[i] = get_bits(&gb, 3); |
337 | 1.45M | } |
338 | | |
339 | 1.81M | for (subframe_idx = 0; subframe_idx < 4; subframe_idx++) { |
340 | 1.45M | unsigned int C72_binomials[PULSE_MAX] = { |
341 | 1.45M | 72, 2556, 59640, 1028790, 13991544, 156238908, 1473109704, |
342 | 1.45M | 3379081753 |
343 | 1.45M | }; |
344 | 1.45M | unsigned int combined_pulse_pos = |
345 | 1.45M | fparam->sf[subframe_idx].combined_pulse_pos; |
346 | 1.45M | int index = 6; |
347 | | |
348 | 1.45M | if (combined_pulse_pos < C72_binomials[PULSE_MAX - 1]) { |
349 | 1.45M | if (p->pulse_dec_mode) { |
350 | 1.45M | int pulse, pulse_idx; |
351 | 1.45M | pulse = PULSE_MAX - 1; |
352 | 1.45M | pulse_idx = 71; |
353 | 1.45M | combined_pulse_pos = |
354 | 1.45M | fparam->sf[subframe_idx].combined_pulse_pos; |
355 | | |
356 | | /* this part seems to be close to g723.1 gen_fcb_excitation() |
357 | | * RATE_6300 */ |
358 | | |
359 | | /* TODO: what is 7? size of subframe? */ |
360 | 11.6M | for (i = 0; i < 7; i++) { |
361 | 10.1M | for (; |
362 | 88.7M | combined_pulse_pos < |
363 | 88.7M | dss_sp_combinatorial_table[pulse][pulse_idx]; |
364 | 78.5M | --pulse_idx) |
365 | 78.5M | ; |
366 | 10.1M | combined_pulse_pos -= |
367 | 10.1M | dss_sp_combinatorial_table[pulse][pulse_idx]; |
368 | 10.1M | pulse--; |
369 | 10.1M | fparam->sf[subframe_idx].pulse_pos[i] = pulse_idx; |
370 | 10.1M | } |
371 | 1.45M | } |
372 | 1.45M | } else { |
373 | 0 | p->pulse_dec_mode = 0; |
374 | | |
375 | | /* why do we need this? */ |
376 | 0 | fparam->sf[subframe_idx].pulse_pos[6] = 0; |
377 | |
|
378 | 0 | for (i = 71; i >= 0; i--) { |
379 | 0 | if (C72_binomials[index] <= combined_pulse_pos) { |
380 | 0 | combined_pulse_pos -= C72_binomials[index]; |
381 | |
|
382 | 0 | fparam->sf[subframe_idx].pulse_pos[6 - index] = i; |
383 | |
|
384 | 0 | if (!index) |
385 | 0 | break; |
386 | 0 | --index; |
387 | 0 | } |
388 | 0 | --C72_binomials[0]; |
389 | 0 | if (index) { |
390 | 0 | int a; |
391 | 0 | for (a = 0; a < index; a++) |
392 | 0 | C72_binomials[a + 1] -= C72_binomials[a]; |
393 | 0 | } |
394 | 0 | } |
395 | 0 | } |
396 | 1.45M | } |
397 | | |
398 | 362k | combined_pitch = get_bits(&gb, 24); |
399 | | |
400 | 362k | fparam->pitch_lag[0] = (combined_pitch % 151) + 36; |
401 | | |
402 | 362k | combined_pitch /= 151; |
403 | | |
404 | 1.08M | for (i = 1; i < SUBFRAMES - 1; i++) { |
405 | 725k | fparam->pitch_lag[i] = combined_pitch % 48; |
406 | 725k | combined_pitch /= 48; |
407 | 725k | } |
408 | 362k | if (combined_pitch > 47) { |
409 | 16.3k | av_log (p->avctx, AV_LOG_WARNING, "combined_pitch was too large\n"); |
410 | 16.3k | combined_pitch = 0; |
411 | 16.3k | } |
412 | 362k | fparam->pitch_lag[i] = combined_pitch; |
413 | | |
414 | 362k | pitch_lag = fparam->pitch_lag[0]; |
415 | 1.45M | for (i = 1; i < SUBFRAMES; i++) { |
416 | 1.08M | if (pitch_lag > 162) { |
417 | 51.2k | fparam->pitch_lag[i] += 162 - 23; |
418 | 1.03M | } else { |
419 | 1.03M | tmp = pitch_lag - 23; |
420 | 1.03M | if (tmp < 36) |
421 | 214k | tmp = 36; |
422 | 1.03M | fparam->pitch_lag[i] += tmp; |
423 | 1.03M | } |
424 | 1.08M | pitch_lag = fparam->pitch_lag[i]; |
425 | 1.08M | } |
426 | 362k | } |
427 | | |
428 | | static void dss_sp_unpack_filter(DssSpContext *p) |
429 | 362k | { |
430 | 362k | int i; |
431 | | |
432 | 5.43M | for (i = 0; i < 14; i++) |
433 | 5.07M | p->lpc_filter[i] = dss_sp_filter_cb[i][p->fparam.filter_idx[i]]; |
434 | 362k | } |
435 | | |
436 | | static void dss_sp_convert_coeffs(int32_t *lpc_filter, int32_t *coeffs) |
437 | 362k | { |
438 | 362k | int a, a_plus, i; |
439 | | |
440 | 362k | coeffs[0] = 0x2000; |
441 | 5.43M | for (a = 0; a < 14; a++) { |
442 | 5.07M | a_plus = a + 1; |
443 | 5.07M | coeffs[a_plus] = lpc_filter[a] >> 2; |
444 | 5.07M | if (a_plus / 2 >= 1) { |
445 | 22.4M | for (i = 1; i <= a_plus / 2; i++) { |
446 | 17.7M | int coeff_1, coeff_2, tmp; |
447 | | |
448 | 17.7M | coeff_1 = coeffs[i]; |
449 | 17.7M | coeff_2 = coeffs[a_plus - i]; |
450 | | |
451 | 17.7M | tmp = DSS_SP_FORMULA(coeff_1, lpc_filter[a], coeff_2); |
452 | 17.7M | coeffs[i] = av_clip_int16(tmp); |
453 | | |
454 | 17.7M | tmp = DSS_SP_FORMULA(coeff_2, lpc_filter[a], coeff_1); |
455 | 17.7M | coeffs[a_plus - i] = av_clip_int16(tmp); |
456 | 17.7M | } |
457 | 4.71M | } |
458 | 5.07M | } |
459 | 362k | } |
460 | | |
461 | | static void dss_sp_add_pulses(int32_t *vector_buf, |
462 | | const struct DssSpSubframe *sf) |
463 | 1.45M | { |
464 | 1.45M | int i; |
465 | | |
466 | 11.6M | for (i = 0; i < 7; i++) |
467 | 10.1M | vector_buf[sf->pulse_pos[i]] += (dss_sp_fixed_cb_gain[sf->gain] * |
468 | 10.1M | dss_sp_pulse_val[sf->pulse_val[i]] + |
469 | 10.1M | 0x4000) >> 15; |
470 | 1.45M | } |
471 | | |
472 | | static void dss_sp_gen_exc(int32_t *vector, int32_t *prev_exc, |
473 | | int pitch_lag, int gain) |
474 | 1.45M | { |
475 | 1.45M | int i; |
476 | | |
477 | | /* do we actually need this check? we can use just [a3 - i % a3] |
478 | | * for both cases */ |
479 | 1.45M | if (pitch_lag < 72) |
480 | 25.9M | for (i = 0; i < 72; i++) |
481 | 25.6M | vector[i] = prev_exc[pitch_lag - i % pitch_lag]; |
482 | 1.09M | else |
483 | 79.8M | for (i = 0; i < 72; i++) |
484 | 78.7M | vector[i] = prev_exc[pitch_lag - i]; |
485 | | |
486 | 105M | for (i = 0; i < 72; i++) { |
487 | 104M | int tmp = gain * vector[i] >> 11; |
488 | 104M | vector[i] = av_clip_int16(tmp); |
489 | 104M | } |
490 | 1.45M | } |
491 | | |
492 | | static void dss_sp_scale_vector(int32_t *vec, int bits, int size) |
493 | 8.70M | { |
494 | 8.70M | int i; |
495 | | |
496 | 8.70M | if (bits < 0) |
497 | 128M | for (i = 0; i < size; i++) |
498 | 126M | vec[i] = vec[i] >> -bits; |
499 | 6.45M | else |
500 | 175M | for (i = 0; i < size; i++) |
501 | 169M | vec[i] = vec[i] * (1 << bits); |
502 | 8.70M | } |
503 | | |
504 | | static void dss_sp_update_buf(int32_t *hist, int32_t *vector) |
505 | 1.45M | { |
506 | 1.45M | int i; |
507 | | |
508 | 166M | for (i = 114; i > 0; i--) |
509 | 165M | vector[i + 72] = vector[i]; |
510 | | |
511 | 105M | for (i = 0; i < 72; i++) |
512 | 104M | vector[72 - i] = hist[i]; |
513 | 1.45M | } |
514 | | |
515 | | static void dss_sp_shift_sq_sub(const int32_t *filter_buf, |
516 | | int32_t *error_buf, int32_t *dst) |
517 | 2.90M | { |
518 | 2.90M | int a; |
519 | | |
520 | 211M | for (a = 0; a < 72; a++) { |
521 | 208M | int i, tmp; |
522 | | |
523 | 208M | tmp = dst[a] * filter_buf[0]; |
524 | | |
525 | 3.13G | for (i = 14; i > 0; i--) |
526 | 2.92G | tmp -= error_buf[i] * (unsigned)filter_buf[i]; |
527 | | |
528 | 3.13G | for (i = 14; i > 0; i--) |
529 | 2.92G | error_buf[i] = error_buf[i - 1]; |
530 | | |
531 | 208M | tmp = (int)(tmp + 4096U) >> 13; |
532 | | |
533 | 208M | error_buf[1] = tmp; |
534 | | |
535 | 208M | dst[a] = av_clip_int16(tmp); |
536 | 208M | } |
537 | 2.90M | } |
538 | | |
539 | | static void dss_sp_shift_sq_add(const int32_t *filter_buf, int32_t *audio_buf, |
540 | | int32_t *dst) |
541 | 1.45M | { |
542 | 1.45M | int a; |
543 | | |
544 | 105M | for (a = 0; a < 72; a++) { |
545 | 104M | int i, tmp = 0; |
546 | | |
547 | 104M | audio_buf[0] = dst[a]; |
548 | | |
549 | 1.67G | for (i = 14; i >= 0; i--) |
550 | 1.56G | tmp += audio_buf[i] * filter_buf[i]; |
551 | | |
552 | 1.56G | for (i = 14; i > 0; i--) |
553 | 1.46G | audio_buf[i] = audio_buf[i - 1]; |
554 | | |
555 | 104M | tmp = (tmp + 4096) >> 13; |
556 | | |
557 | 104M | dst[a] = av_clip_int16(tmp); |
558 | 104M | } |
559 | 1.45M | } |
560 | | |
561 | | static void dss_sp_vec_mult(const int32_t *src, int32_t *dst, |
562 | | const int16_t *mult) |
563 | 2.90M | { |
564 | 2.90M | int i; |
565 | | |
566 | 2.90M | dst[0] = src[0]; |
567 | | |
568 | 43.5M | for (i = 1; i < 15; i++) |
569 | 40.6M | dst[i] = (src[i] * mult[i] + 0x4000) >> 15; |
570 | 2.90M | } |
571 | | |
572 | | static int dss_sp_get_normalize_bits(int32_t *vector_buf, int16_t size) |
573 | 1.45M | { |
574 | 1.45M | unsigned int val; |
575 | 1.45M | int max_val; |
576 | 1.45M | int i; |
577 | | |
578 | 1.45M | val = 1; |
579 | 105M | for (i = 0; i < size; i++) |
580 | 104M | val |= FFABS(vector_buf[i]); |
581 | | |
582 | 2.26M | for (max_val = 0; val <= 0x4000; ++max_val) |
583 | 810k | val *= 2; |
584 | 1.45M | return max_val; |
585 | 1.45M | } |
586 | | |
587 | | static int dss_sp_vector_sum(DssSpContext *p, int size) |
588 | 2.90M | { |
589 | 2.90M | int i, sum = 0; |
590 | 211M | for (i = 0; i < size; i++) |
591 | 208M | sum += FFABS(p->vector_buf[i]); |
592 | 2.90M | return sum; |
593 | 2.90M | } |
594 | | |
595 | | static void dss_sp_sf_synthesis(DssSpContext *p, int32_t lpc_filter, |
596 | | int32_t *dst, int size) |
597 | 1.45M | { |
598 | 1.45M | int32_t tmp_buf[15]; |
599 | 1.45M | int32_t noise[72]; |
600 | 1.45M | int bias, vsum_2 = 0, vsum_1 = 0, v36, normalize_bits; |
601 | 1.45M | int i, tmp; |
602 | | |
603 | 1.45M | if (size > 0) { |
604 | 1.45M | vsum_1 = dss_sp_vector_sum(p, size); |
605 | | |
606 | 1.45M | if (vsum_1 > 0xFFFFF) |
607 | 1.06M | vsum_1 = 0xFFFFF; |
608 | 1.45M | } |
609 | | |
610 | 1.45M | normalize_bits = dss_sp_get_normalize_bits(p->vector_buf, size); |
611 | | |
612 | 1.45M | dss_sp_scale_vector(p->vector_buf, normalize_bits - 3, size); |
613 | 1.45M | dss_sp_scale_vector(p->audio_buf, normalize_bits, 15); |
614 | 1.45M | dss_sp_scale_vector(p->err_buf1, normalize_bits, 15); |
615 | | |
616 | 1.45M | v36 = p->err_buf1[1]; |
617 | | |
618 | 1.45M | dss_sp_vec_mult(p->filter, tmp_buf, binary_decreasing_array); |
619 | 1.45M | dss_sp_shift_sq_add(tmp_buf, p->audio_buf, p->vector_buf); |
620 | | |
621 | 1.45M | dss_sp_vec_mult(p->filter, tmp_buf, dss_sp_unc_decreasing_array); |
622 | 1.45M | dss_sp_shift_sq_sub(tmp_buf, p->err_buf1, p->vector_buf); |
623 | | |
624 | | /* lpc_filter can be negative */ |
625 | 1.45M | lpc_filter = lpc_filter >> 1; |
626 | 1.45M | if (lpc_filter >= 0) |
627 | 145k | lpc_filter = 0; |
628 | | |
629 | 1.45M | if (size > 1) { |
630 | 104M | for (i = size - 1; i > 0; i--) { |
631 | 102M | tmp = DSS_SP_FORMULA(p->vector_buf[i], lpc_filter, |
632 | 102M | p->vector_buf[i - 1]); |
633 | 102M | p->vector_buf[i] = av_clip_int16(tmp); |
634 | 102M | } |
635 | 1.45M | } |
636 | | |
637 | 1.45M | tmp = DSS_SP_FORMULA(p->vector_buf[0], lpc_filter, v36); |
638 | 1.45M | p->vector_buf[0] = av_clip_int16(tmp); |
639 | | |
640 | 1.45M | dss_sp_scale_vector(p->vector_buf, -normalize_bits, size); |
641 | 1.45M | dss_sp_scale_vector(p->audio_buf, -normalize_bits, 15); |
642 | 1.45M | dss_sp_scale_vector(p->err_buf1, -normalize_bits, 15); |
643 | | |
644 | 1.45M | if (size > 0) |
645 | 1.45M | vsum_2 = dss_sp_vector_sum(p, size); |
646 | | |
647 | 1.45M | if (vsum_2 >= 0x40) |
648 | 1.44M | tmp = (vsum_1 << 11) / vsum_2; |
649 | 1.44k | else |
650 | 1.44k | tmp = 1; |
651 | | |
652 | 1.45M | bias = 409 * tmp >> 15 << 15; |
653 | 1.45M | tmp = (bias + 32358 * p->noise_state) >> 15; |
654 | 1.45M | noise[0] = av_clip_int16(tmp); |
655 | | |
656 | 104M | for (i = 1; i < size; i++) { |
657 | 102M | tmp = (bias + 32358 * noise[i - 1]) >> 15; |
658 | 102M | noise[i] = av_clip_int16(tmp); |
659 | 102M | } |
660 | | |
661 | 1.45M | p->noise_state = noise[size - 1]; |
662 | 105M | for (i = 0; i < size; i++) { |
663 | 104M | tmp = (p->vector_buf[i] * noise[i]) >> 11; |
664 | 104M | dst[i] = av_clip_int16(tmp); |
665 | 104M | } |
666 | 1.45M | } |
667 | | |
668 | | static void dss_sp_update_state(DssSpContext *p, int32_t *dst) |
669 | 362k | { |
670 | 362k | int i, offset = 6, counter = 0, a = 0; |
671 | | |
672 | 2.53M | for (i = 0; i < 6; i++) |
673 | 2.17M | p->excitation[i] = p->excitation[288 + i]; |
674 | | |
675 | 104M | for (i = 0; i < 72 * SUBFRAMES; i++) |
676 | 104M | p->excitation[6 + i] = dst[i]; |
677 | | |
678 | 95.7M | do { |
679 | 95.7M | int tmp = 0; |
680 | | |
681 | 669M | for (i = 0; i < 6; i++) |
682 | 574M | tmp += p->excitation[offset--] * dss_sp_sinc[a + i * 11]; |
683 | | |
684 | 95.7M | offset += 7; |
685 | | |
686 | 95.7M | tmp >>= 15; |
687 | 95.7M | dst[counter] = av_clip_int16(tmp); |
688 | | |
689 | 95.7M | counter++; |
690 | | |
691 | 95.7M | a = (a + 1) % 11; |
692 | 95.7M | if (!a) |
693 | 8.70M | offset++; |
694 | 95.7M | } while (offset < FF_ARRAY_ELEMS(p->excitation)); |
695 | 362k | } |
696 | | |
697 | | static void dss_sp_32to16bit(int16_t *dst, int32_t *src, int size) |
698 | 362k | { |
699 | 362k | int i; |
700 | | |
701 | 96.0M | for (i = 0; i < size; i++) |
702 | 95.7M | dst[i] = av_clip_int16(src[i]); |
703 | 362k | } |
704 | | |
705 | | static int dss_sp_decode_one_frame(DssSpContext *p, |
706 | | int16_t *abuf_dst, const uint8_t *abuf_src) |
707 | 362k | { |
708 | 362k | int i, j; |
709 | | |
710 | 362k | dss_sp_unpack_coeffs(p, abuf_src); |
711 | | |
712 | 362k | dss_sp_unpack_filter(p); |
713 | | |
714 | 362k | dss_sp_convert_coeffs(p->lpc_filter, p->filter); |
715 | | |
716 | 1.81M | for (j = 0; j < SUBFRAMES; j++) { |
717 | 1.45M | dss_sp_gen_exc(p->vector_buf, p->history, |
718 | 1.45M | p->fparam.pitch_lag[j], |
719 | 1.45M | dss_sp_adaptive_gain[p->fparam.sf_adaptive_gain[j]]); |
720 | | |
721 | 1.45M | dss_sp_add_pulses(p->vector_buf, &p->fparam.sf[j]); |
722 | | |
723 | 1.45M | dss_sp_update_buf(p->vector_buf, p->history); |
724 | | |
725 | 105M | for (i = 0; i < 72; i++) |
726 | 104M | p->vector_buf[i] = p->history[72 - i]; |
727 | | |
728 | 1.45M | dss_sp_shift_sq_sub(p->filter, |
729 | 1.45M | p->err_buf2, p->vector_buf); |
730 | | |
731 | 1.45M | dss_sp_sf_synthesis(p, p->lpc_filter[0], |
732 | 1.45M | &p->working_buffer[j][0], 72); |
733 | 1.45M | } |
734 | | |
735 | 362k | dss_sp_update_state(p, &p->working_buffer[0][0]); |
736 | | |
737 | 362k | dss_sp_32to16bit(abuf_dst, |
738 | 362k | &p->working_buffer[0][0], 264); |
739 | 362k | return 0; |
740 | 362k | } |
741 | | |
742 | | static int dss_sp_decode_frame(AVCodecContext *avctx, AVFrame *frame, |
743 | | int *got_frame_ptr, AVPacket *avpkt) |
744 | 554k | { |
745 | 554k | DssSpContext *p = avctx->priv_data; |
746 | 554k | const uint8_t *buf = avpkt->data; |
747 | 554k | int buf_size = avpkt->size; |
748 | | |
749 | 554k | int16_t *out; |
750 | 554k | int ret; |
751 | | |
752 | 554k | if (buf_size < DSS_SP_FRAME_SIZE) { |
753 | 191k | if (buf_size) |
754 | 191k | av_log(avctx, AV_LOG_WARNING, |
755 | 191k | "Expected %d bytes, got %d - skipping packet.\n", |
756 | 191k | DSS_SP_FRAME_SIZE, buf_size); |
757 | 191k | *got_frame_ptr = 0; |
758 | 191k | return AVERROR_INVALIDDATA; |
759 | 191k | } |
760 | | |
761 | 362k | frame->nb_samples = DSS_SP_SAMPLE_COUNT; |
762 | 362k | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
763 | 0 | return ret; |
764 | | |
765 | 362k | out = (int16_t *)frame->data[0]; |
766 | | |
767 | 362k | dss_sp_decode_one_frame(p, out, buf); |
768 | | |
769 | 362k | *got_frame_ptr = 1; |
770 | | |
771 | 362k | return DSS_SP_FRAME_SIZE; |
772 | 362k | } |
773 | | |
774 | | const FFCodec ff_dss_sp_decoder = { |
775 | | .p.name = "dss_sp", |
776 | | CODEC_LONG_NAME("Digital Speech Standard - Standard Play mode (DSS SP)"), |
777 | | .p.type = AVMEDIA_TYPE_AUDIO, |
778 | | .p.id = AV_CODEC_ID_DSS_SP, |
779 | | .priv_data_size = sizeof(DssSpContext), |
780 | | .init = dss_sp_decode_init, |
781 | | FF_CODEC_DECODE_CB(dss_sp_decode_frame), |
782 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF, |
783 | | }; |