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.

386 lines
10KB

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