You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

781 lines
28KB

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