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.

382 lines
9.9KB

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