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.

378 lines
9.8KB

  1. /*
  2. * DSP Group TrueSpeech compatible decoder
  3. * Copyright (c) 2005 Konstantin Shishkov
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avcodec.h"
  20. #include "truespeech_data.h"
  21. /**
  22. * @file truespeech.c
  23. * TrueSpeech decoder.
  24. */
  25. /**
  26. * TrueSpeech decoder context
  27. */
  28. typedef struct {
  29. /* input data */
  30. int16_t vector[8]; //< input vector: 5/5/4/4/4/3/3/3
  31. int offset1[2]; //< 8-bit value, used in one copying offset
  32. int offset2[4]; //< 7-bit value, encodes offsets for copying and for two-point filter
  33. int pulseoff[4]; //< 4-bit offset of pulse values block
  34. int pulsepos[4]; //< 27-bit variable, encodes 7 pulse positions
  35. int pulseval[4]; //< 7x2-bit pulse values
  36. int flag; //< 1-bit flag, shows how to choose filters
  37. /* temporary data */
  38. int filtbuf[146]; // some big vector used for storing filters
  39. int prevfilt[8]; // filter from previous frame
  40. int16_t tmp1[8]; // coefficients for adding to out
  41. int16_t tmp2[8]; // coefficients for adding to out
  42. int16_t tmp3[8]; // coefficients for adding to out
  43. int16_t cvector[8]; // correlated input vector
  44. int filtval; // gain value for one function
  45. int16_t newvec[60]; // tmp vector
  46. int16_t filters[32]; // filters for every subframe
  47. } TSContext;
  48. static int truespeech_decode_init(AVCodecContext * avctx)
  49. {
  50. // TSContext *c = avctx->priv_data;
  51. return 0;
  52. }
  53. static void truespeech_read_frame(TSContext *dec, uint8_t *input)
  54. {
  55. uint32_t t;
  56. /* first dword */
  57. t = LE_32(input);
  58. input += 4;
  59. dec->flag = t & 1;
  60. dec->vector[0] = ts_codebook[0][(t >> 1) & 0x1F];
  61. dec->vector[1] = ts_codebook[1][(t >> 6) & 0x1F];
  62. dec->vector[2] = ts_codebook[2][(t >> 11) & 0xF];
  63. dec->vector[3] = ts_codebook[3][(t >> 15) & 0xF];
  64. dec->vector[4] = ts_codebook[4][(t >> 19) & 0xF];
  65. dec->vector[5] = ts_codebook[5][(t >> 23) & 0x7];
  66. dec->vector[6] = ts_codebook[6][(t >> 26) & 0x7];
  67. dec->vector[7] = ts_codebook[7][(t >> 29) & 0x7];
  68. /* second dword */
  69. t = LE_32(input);
  70. input += 4;
  71. dec->offset2[0] = (t >> 0) & 0x7F;
  72. dec->offset2[1] = (t >> 7) & 0x7F;
  73. dec->offset2[2] = (t >> 14) & 0x7F;
  74. dec->offset2[3] = (t >> 21) & 0x7F;
  75. dec->offset1[0] = ((t >> 28) & 0xF) << 4;
  76. /* third dword */
  77. t = LE_32(input);
  78. input += 4;
  79. dec->pulseval[0] = (t >> 0) & 0x3FFF;
  80. dec->pulseval[1] = (t >> 14) & 0x3FFF;
  81. dec->offset1[1] = (t >> 28) & 0x0F;
  82. /* fourth dword */
  83. t = LE_32(input);
  84. input += 4;
  85. dec->pulseval[2] = (t >> 0) & 0x3FFF;
  86. dec->pulseval[3] = (t >> 14) & 0x3FFF;
  87. dec->offset1[1] |= ((t >> 28) & 0x0F) << 4;
  88. /* fifth dword */
  89. t = LE_32(input);
  90. input += 4;
  91. dec->pulsepos[0] = (t >> 4) & 0x7FFFFFF;
  92. dec->pulseoff[0] = (t >> 0) & 0xF;
  93. dec->offset1[0] |= (t >> 31) & 1;
  94. /* sixth dword */
  95. t = LE_32(input);
  96. input += 4;
  97. dec->pulsepos[1] = (t >> 4) & 0x7FFFFFF;
  98. dec->pulseoff[1] = (t >> 0) & 0xF;
  99. dec->offset1[0] |= ((t >> 31) & 1) << 1;
  100. /* seventh dword */
  101. t = LE_32(input);
  102. input += 4;
  103. dec->pulsepos[2] = (t >> 4) & 0x7FFFFFF;
  104. dec->pulseoff[2] = (t >> 0) & 0xF;
  105. dec->offset1[0] |= ((t >> 31) & 1) << 2;
  106. /* eighth dword */
  107. t = LE_32(input);
  108. input += 4;
  109. dec->pulsepos[3] = (t >> 4) & 0x7FFFFFF;
  110. dec->pulseoff[3] = (t >> 0) & 0xF;
  111. dec->offset1[0] |= ((t >> 31) & 1) << 3;
  112. }
  113. static void truespeech_correlate_filter(TSContext *dec)
  114. {
  115. int16_t tmp[8];
  116. int i, j;
  117. for(i = 0; i < 8; i++){
  118. if(i > 0){
  119. memcpy(tmp, dec->cvector, i * 2);
  120. for(j = 0; j < i; j++)
  121. dec->cvector[j] = ((tmp[i - j - 1] * dec->vector[i]) +
  122. (dec->cvector[j] << 15) + 0x4000) >> 15;
  123. }
  124. dec->cvector[i] = (8 - dec->vector[i]) >> 3;
  125. }
  126. for(i = 0; i < 8; i++)
  127. dec->cvector[i] = (dec->cvector[i] * ts_230[i]) >> 15;
  128. dec->filtval = dec->vector[0];
  129. }
  130. static void truespeech_filters_merge(TSContext *dec)
  131. {
  132. int i;
  133. if(!dec->flag){
  134. for(i = 0; i < 8; i++){
  135. dec->filters[i + 0] = dec->prevfilt[i];
  136. dec->filters[i + 8] = dec->prevfilt[i];
  137. }
  138. }else{
  139. for(i = 0; i < 8; i++){
  140. dec->filters[i + 0]=(dec->cvector[i] * 21846 + dec->prevfilt[i] * 10923 + 16384) >> 15;
  141. dec->filters[i + 8]=(dec->cvector[i] * 10923 + dec->prevfilt[i] * 21846 + 16384) >> 15;
  142. }
  143. }
  144. for(i = 0; i < 8; i++){
  145. dec->filters[i + 16] = dec->cvector[i];
  146. dec->filters[i + 24] = dec->cvector[i];
  147. }
  148. }
  149. static void truespeech_apply_twopoint_filter(TSContext *dec, int quart)
  150. {
  151. int16_t tmp[146 + 60], *ptr0, *ptr1, *filter;
  152. int i, t, off;
  153. t = dec->offset2[quart];
  154. if(t == 127){
  155. memset(dec->newvec, 0, 60 * 2);
  156. return;
  157. }
  158. for(i = 0; i < 146; i++)
  159. tmp[i] = dec->filtbuf[i];
  160. off = (t / 25) + dec->offset1[quart >> 1] + 18;
  161. ptr0 = tmp + 145 - off;
  162. ptr1 = tmp + 146;
  163. filter = (int16_t*)ts_240 + (t % 25) * 2;
  164. for(i = 0; i < 60; i++){
  165. t = (ptr0[0] * filter[0] + ptr0[1] * filter[1] + 0x2000) >> 14;
  166. ptr0++;
  167. dec->newvec[i] = t;
  168. ptr1[i] = t;
  169. }
  170. }
  171. static void truespeech_place_pulses(TSContext *dec, int16_t *out, int quart)
  172. {
  173. int16_t tmp[7];
  174. int i, j, t;
  175. int16_t *ptr1, *ptr2;
  176. int coef;
  177. memset(out, 0, 60 * 2);
  178. for(i = 0; i < 7; i++) {
  179. t = dec->pulseval[quart] & 3;
  180. dec->pulseval[quart] >>= 2;
  181. tmp[6 - i] = ts_562[dec->pulseoff[quart] * 4 + t];
  182. }
  183. coef = dec->pulsepos[quart] >> 15;
  184. ptr1 = (int16_t*)ts_140 + 30;
  185. ptr2 = tmp;
  186. for(i = 0, j = 3; (i < 30) && (j > 0); i++){
  187. t = *ptr1++;
  188. if(coef >= t)
  189. coef -= t;
  190. else{
  191. out[i] = *ptr2++;
  192. ptr1 += 30;
  193. j--;
  194. }
  195. }
  196. coef = dec->pulsepos[quart] & 0x7FFF;
  197. ptr1 = (int16_t*)ts_140;
  198. for(i = 30, j = 4; (i < 60) && (j > 0); i++){
  199. t = *ptr1++;
  200. if(coef >= t)
  201. coef -= t;
  202. else{
  203. out[i] = *ptr2++;
  204. ptr1 += 30;
  205. j--;
  206. }
  207. }
  208. }
  209. static void truespeech_update_filters(TSContext *dec, int16_t *out, int quart)
  210. {
  211. int i;
  212. for(i = 0; i < 86; i++)
  213. dec->filtbuf[i] = dec->filtbuf[i + 60];
  214. for(i = 0; i < 60; i++){
  215. dec->filtbuf[i + 86] = out[i] + dec->newvec[i] - (dec->newvec[i] >> 3);
  216. out[i] += dec->newvec[i];
  217. }
  218. }
  219. static void truespeech_synth(TSContext *dec, int16_t *out, int quart)
  220. {
  221. int i,k;
  222. int t[8];
  223. int16_t *ptr0, *ptr1;
  224. ptr0 = dec->tmp1;
  225. ptr1 = dec->filters + quart * 8;
  226. for(i = 0; i < 60; i++){
  227. int sum = 0;
  228. for(k = 0; k < 8; k++)
  229. sum += ptr0[k] * ptr1[k];
  230. sum = (sum + (out[i] << 12) + 0x800) >> 12;
  231. out[i] = clip(sum, -0x7FFE, 0x7FFE);
  232. for(k = 7; k > 0; k--)
  233. ptr0[k] = ptr0[k - 1];
  234. ptr0[0] = out[i];
  235. }
  236. for(i = 0; i < 8; i++)
  237. t[i] = (ts_5E2[i] * ptr1[i]) >> 15;
  238. ptr0 = dec->tmp2;
  239. for(i = 0; i < 60; i++){
  240. int sum = 0;
  241. for(k = 0; k < 8; k++)
  242. sum += ptr0[k] * t[k];
  243. for(k = 7; k > 0; k--)
  244. ptr0[k] = ptr0[k - 1];
  245. ptr0[0] = out[i];
  246. out[i] = ((out[i] << 12) - sum) >> 12;
  247. }
  248. for(i = 0; i < 8; i++)
  249. t[i] = (ts_5F2[i] * ptr1[i]) >> 15;
  250. ptr0 = dec->tmp3;
  251. for(i = 0; i < 60; i++){
  252. int sum = out[i] << 12;
  253. for(k = 0; k < 8; k++)
  254. sum += ptr0[k] * t[k];
  255. for(k = 7; k > 0; k--)
  256. ptr0[k] = ptr0[k - 1];
  257. ptr0[0] = clip((sum + 0x800) >> 12, -0x7FFE, 0x7FFE);
  258. sum = ((ptr0[1] * (dec->filtval - (dec->filtval >> 2))) >> 4) + sum;
  259. sum = sum - (sum >> 3);
  260. out[i] = clip((sum + 0x800) >> 12, -0x7FFE, 0x7FFE);
  261. }
  262. }
  263. static void truespeech_save_prevvec(TSContext *c)
  264. {
  265. int i;
  266. for(i = 0; i < 8; i++)
  267. c->prevfilt[i] = c->cvector[i];
  268. }
  269. static int truespeech_decode_frame(AVCodecContext *avctx,
  270. void *data, int *data_size,
  271. uint8_t *buf, int buf_size)
  272. {
  273. TSContext *c = avctx->priv_data;
  274. int i;
  275. short *samples = data;
  276. int consumed = 0;
  277. int16_t out_buf[240];
  278. if (!buf_size)
  279. return 0;
  280. while (consumed < buf_size) {
  281. truespeech_read_frame(c, buf + consumed);
  282. consumed += 32;
  283. truespeech_correlate_filter(c);
  284. truespeech_filters_merge(c);
  285. memset(out_buf, 0, 240 * 2);
  286. for(i = 0; i < 4; i++) {
  287. truespeech_apply_twopoint_filter(c, i);
  288. truespeech_place_pulses(c, out_buf + i * 60, i);
  289. truespeech_update_filters(c, out_buf + i * 60, i);
  290. truespeech_synth(c, out_buf + i * 60, i);
  291. }
  292. truespeech_save_prevvec(c);
  293. /* finally output decoded frame */
  294. for(i = 0; i < 240; i++)
  295. *samples++ = out_buf[i];
  296. }
  297. *data_size = consumed * 15;
  298. return buf_size;
  299. }
  300. AVCodec truespeech_decoder = {
  301. "truespeech",
  302. CODEC_TYPE_AUDIO,
  303. CODEC_ID_TRUESPEECH,
  304. sizeof(TSContext),
  305. truespeech_decode_init,
  306. NULL,
  307. NULL,
  308. truespeech_decode_frame,
  309. };