FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dss_sp.c
Date: 2022-07-04 19:11:22
Exec Total Coverage
Lines: 242 265 91.3%
Branches: 107 130 82.3%

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