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