truespeech.c 10 KB

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