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.

391 lines
13KB

  1. /*
  2. * Voxware MetaSound decoder
  3. * Copyright (c) 2013 Konstantin Shishkov
  4. * based on TwinVQ decoder
  5. * Copyright (c) 2009 Vitor Sessak
  6. *
  7. * This file is part of Libav.
  8. *
  9. * Libav is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * Libav is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <inttypes.h>
  24. #include <math.h>
  25. #include <stdint.h>
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/float_dsp.h"
  28. #define BITSTREAM_READER_LE
  29. #include "avcodec.h"
  30. #include "bitstream.h"
  31. #include "fft.h"
  32. #include "internal.h"
  33. #include "lsp.h"
  34. #include "sinewin.h"
  35. #include "twinvq.h"
  36. #include "metasound_data.h"
  37. static void add_peak(float period, int width, const float *shape,
  38. float ppc_gain, float *speech, int len)
  39. {
  40. int i, j, center;
  41. const float *shape_end = shape + len;
  42. // First peak centered around zero
  43. for (i = 0; i < width / 2; i++)
  44. speech[i] += ppc_gain * *shape++;
  45. for (i = 1; i < ROUNDED_DIV(len, width); i++) {
  46. center = (int)(i * period + 0.5);
  47. for (j = -width / 2; j < (width + 1) / 2; j++)
  48. speech[j + center] += ppc_gain * *shape++;
  49. }
  50. // For the last block, be careful not to go beyond the end of the buffer
  51. center = (int)(i * period + 0.5);
  52. for (j = -width / 2; j < (width + 1) / 2 && shape < shape_end; j++)
  53. speech[j + center] += ppc_gain * *shape++;
  54. }
  55. static void decode_ppc(TwinVQContext *tctx, int period_coef, int g_coef,
  56. const float *shape, float *speech)
  57. {
  58. const TwinVQModeTab *mtab = tctx->mtab;
  59. int isampf = tctx->avctx->sample_rate / 1000;
  60. int ibps = tctx->avctx->bit_rate / (1000 * tctx->avctx->channels);
  61. int width;
  62. float ratio = (float)mtab->size / isampf;
  63. float min_period, max_period, period_range, period;
  64. float some_mult;
  65. float pgain_base, pgain_step, ppc_gain;
  66. if (tctx->avctx->channels == 1) {
  67. min_period = log2(ratio * 0.2);
  68. max_period = min_period + log2(6);
  69. } else {
  70. min_period = (int)(ratio * 0.2 * 400 + 0.5) / 400.0;
  71. max_period = (int)(ratio * 0.2 * 400 * 6 + 0.5) / 400.0;
  72. }
  73. period_range = max_period - min_period;
  74. period = min_period + period_coef * period_range /
  75. ((1 << mtab->ppc_period_bit) - 1);
  76. if (tctx->avctx->channels == 1)
  77. period = powf(2.0, period);
  78. else
  79. period = (int)(period * 400 + 0.5) / 400.0;
  80. switch (isampf) {
  81. case 8: some_mult = 2.0; break;
  82. case 11: some_mult = 3.0; break;
  83. case 16: some_mult = 3.0; break;
  84. case 22: some_mult = ibps == 32 ? 2.0 : 4.0; break;
  85. case 44: some_mult = 8.0; break;
  86. default: some_mult = 4.0;
  87. }
  88. width = (int)(some_mult / (mtab->size / period) * mtab->ppc_shape_len);
  89. if (isampf == 22 && ibps == 32)
  90. width = (int)((2.0 / period + 1) * width + 0.5);
  91. pgain_base = tctx->avctx->channels == 2 ? 25000.0 : 20000.0;
  92. pgain_step = pgain_base / ((1 << mtab->pgain_bit) - 1);
  93. ppc_gain = 1.0 / 8192 *
  94. twinvq_mulawinv(pgain_step * g_coef + pgain_step / 2,
  95. pgain_base, TWINVQ_PGAIN_MU);
  96. add_peak(period, width, shape, ppc_gain, speech, mtab->ppc_shape_len);
  97. }
  98. static void dec_bark_env(TwinVQContext *tctx, const uint8_t *in, int use_hist,
  99. int ch, float *out, float gain,
  100. enum TwinVQFrameType ftype)
  101. {
  102. const TwinVQModeTab *mtab = tctx->mtab;
  103. int i, j;
  104. float *hist = tctx->bark_hist[ftype][ch];
  105. float val = ((const float []) { 0.4, 0.35, 0.28 })[ftype];
  106. int bark_n_coef = mtab->fmode[ftype].bark_n_coef;
  107. int fw_cb_len = mtab->fmode[ftype].bark_env_size / bark_n_coef;
  108. int idx = 0;
  109. if (tctx->avctx->channels == 1)
  110. val = 0.5;
  111. for (i = 0; i < fw_cb_len; i++)
  112. for (j = 0; j < bark_n_coef; j++, idx++) {
  113. float tmp2 = mtab->fmode[ftype].bark_cb[fw_cb_len * in[j] + i] *
  114. (1.0 / 2048);
  115. float st;
  116. if (tctx->avctx->channels == 1)
  117. st = use_hist ?
  118. tmp2 + val * hist[idx] + 1.0 : tmp2 + 1.0;
  119. else
  120. st = use_hist ? (1.0 - val) * tmp2 + val * hist[idx] + 1.0
  121. : tmp2 + 1.0;
  122. hist[idx] = tmp2;
  123. if (st < 0.1)
  124. st = 0.1;
  125. twinvq_memset_float(out, st * gain,
  126. mtab->fmode[ftype].bark_tab[idx]);
  127. out += mtab->fmode[ftype].bark_tab[idx];
  128. }
  129. }
  130. static void read_cb_data(TwinVQContext *tctx, BitstreamContext *bc,
  131. uint8_t *dst, enum TwinVQFrameType ftype)
  132. {
  133. int i;
  134. for (i = 0; i < tctx->n_div[ftype]; i++) {
  135. int bs_second_part = (i >= tctx->bits_main_spec_change[ftype]);
  136. *dst++ = bitstream_read(bc, tctx->bits_main_spec[0][ftype][bs_second_part]);
  137. *dst++ = bitstream_read(bc, tctx->bits_main_spec[1][ftype][bs_second_part]);
  138. }
  139. }
  140. static int metasound_read_bitstream(AVCodecContext *avctx, TwinVQContext *tctx,
  141. const uint8_t *buf, int buf_size)
  142. {
  143. TwinVQFrameData *bits;
  144. const TwinVQModeTab *mtab = tctx->mtab;
  145. int channels = tctx->avctx->channels;
  146. int sub;
  147. BitstreamContext bc;
  148. int i, j, k;
  149. bitstream_init8(&bc, buf, buf_size);
  150. for (tctx->cur_frame = 0; tctx->cur_frame < tctx->frames_per_packet;
  151. tctx->cur_frame++) {
  152. bits = tctx->bits + tctx->cur_frame;
  153. bits->window_type = bitstream_read(&bc, TWINVQ_WINDOW_TYPE_BITS);
  154. if (bits->window_type > 8) {
  155. av_log(avctx, AV_LOG_ERROR, "Invalid window type, broken sample?\n");
  156. return AVERROR_INVALIDDATA;
  157. }
  158. bits->ftype = ff_twinvq_wtype_to_ftype_table[tctx->bits[tctx->cur_frame].window_type];
  159. sub = mtab->fmode[bits->ftype].sub;
  160. if (bits->ftype != TWINVQ_FT_SHORT && !tctx->is_6kbps)
  161. bitstream_read(&bc, 2);
  162. read_cb_data(tctx, &bc, bits->main_coeffs, bits->ftype);
  163. for (i = 0; i < channels; i++)
  164. for (j = 0; j < sub; j++)
  165. for (k = 0; k < mtab->fmode[bits->ftype].bark_n_coef; k++)
  166. bits->bark1[i][j][k] =
  167. bitstream_read(&bc, mtab->fmode[bits->ftype].bark_n_bit);
  168. for (i = 0; i < channels; i++)
  169. for (j = 0; j < sub; j++)
  170. bits->bark_use_hist[i][j] = bitstream_read_bit(&bc);
  171. if (bits->ftype == TWINVQ_FT_LONG) {
  172. for (i = 0; i < channels; i++)
  173. bits->gain_bits[i] = bitstream_read(&bc, TWINVQ_GAIN_BITS);
  174. } else {
  175. for (i = 0; i < channels; i++) {
  176. bits->gain_bits[i] = bitstream_read(&bc, TWINVQ_GAIN_BITS);
  177. for (j = 0; j < sub; j++)
  178. bits->sub_gain_bits[i * sub + j] =
  179. bitstream_read(&bc, TWINVQ_SUB_GAIN_BITS);
  180. }
  181. }
  182. for (i = 0; i < channels; i++) {
  183. bits->lpc_hist_idx[i] = bitstream_read(&bc, mtab->lsp_bit0);
  184. bits->lpc_idx1[i] = bitstream_read(&bc, mtab->lsp_bit1);
  185. for (j = 0; j < mtab->lsp_split; j++)
  186. bits->lpc_idx2[i][j] = bitstream_read(&bc, mtab->lsp_bit2);
  187. }
  188. if (bits->ftype == TWINVQ_FT_LONG) {
  189. read_cb_data(tctx, &bc, bits->ppc_coeffs, 3);
  190. for (i = 0; i < channels; i++) {
  191. bits->p_coef[i] = bitstream_read(&bc, mtab->ppc_period_bit);
  192. bits->g_coef[i] = bitstream_read(&bc, mtab->pgain_bit);
  193. }
  194. }
  195. // subframes are aligned to nibbles
  196. if (bitstream_tell(&bc) & 3)
  197. bitstream_skip(&bc, 4 - (bitstream_tell(&bc) & 3));
  198. }
  199. return 0;
  200. }
  201. typedef struct MetasoundProps {
  202. uint32_t tag;
  203. int bit_rate;
  204. int channels;
  205. int sample_rate;
  206. } MetasoundProps;
  207. static const MetasoundProps codec_props[] = {
  208. { MKTAG('V','X','0','3'), 6, 1, 8000 },
  209. { MKTAG('V','X','0','4'), 12, 2, 8000 },
  210. { MKTAG('V','O','X','i'), 8, 1, 8000 },
  211. { MKTAG('V','O','X','j'), 10, 1, 11025 },
  212. { MKTAG('V','O','X','k'), 16, 1, 16000 },
  213. { MKTAG('V','O','X','L'), 24, 1, 22050 },
  214. { MKTAG('V','O','X','q'), 32, 1, 44100 },
  215. { MKTAG('V','O','X','r'), 40, 1, 44100 },
  216. { MKTAG('V','O','X','s'), 48, 1, 44100 },
  217. { MKTAG('V','O','X','t'), 16, 2, 8000 },
  218. { MKTAG('V','O','X','u'), 20, 2, 11025 },
  219. { MKTAG('V','O','X','v'), 32, 2, 16000 },
  220. { MKTAG('V','O','X','w'), 48, 2, 22050 },
  221. { MKTAG('V','O','X','x'), 64, 2, 44100 },
  222. { MKTAG('V','O','X','y'), 80, 2, 44100 },
  223. { MKTAG('V','O','X','z'), 96, 2, 44100 },
  224. { 0, 0, 0, 0 }
  225. };
  226. static av_cold int metasound_decode_init(AVCodecContext *avctx)
  227. {
  228. int isampf, ibps;
  229. TwinVQContext *tctx = avctx->priv_data;
  230. uint32_t tag;
  231. const MetasoundProps *props = codec_props;
  232. if (!avctx->extradata || avctx->extradata_size < 16) {
  233. av_log(avctx, AV_LOG_ERROR, "Missing or incomplete extradata\n");
  234. return AVERROR_INVALIDDATA;
  235. }
  236. tag = AV_RL32(avctx->extradata + 12);
  237. for (;;) {
  238. if (!props->tag) {
  239. av_log(avctx, AV_LOG_ERROR, "Could not find tag %08"PRIX32"\n", tag);
  240. return AVERROR_INVALIDDATA;
  241. }
  242. if (props->tag == tag) {
  243. avctx->sample_rate = props->sample_rate;
  244. avctx->channels = props->channels;
  245. avctx->bit_rate = props->bit_rate * 1000;
  246. isampf = avctx->sample_rate / 1000;
  247. break;
  248. }
  249. props++;
  250. }
  251. if (avctx->channels <= 0 || avctx->channels > TWINVQ_CHANNELS_MAX) {
  252. av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %i\n",
  253. avctx->channels);
  254. return AVERROR_INVALIDDATA;
  255. }
  256. avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO
  257. : AV_CH_LAYOUT_STEREO;
  258. ibps = avctx->bit_rate / (1000 * avctx->channels);
  259. switch ((avctx->channels << 16) + (isampf << 8) + ibps) {
  260. case (1 << 16) + ( 8 << 8) + 6:
  261. tctx->mtab = &ff_metasound_mode0806;
  262. break;
  263. case (2 << 16) + ( 8 << 8) + 6:
  264. tctx->mtab = &ff_metasound_mode0806s;
  265. break;
  266. case (1 << 16) + ( 8 << 8) + 8:
  267. tctx->mtab = &ff_metasound_mode0808;
  268. break;
  269. case (2 << 16) + ( 8 << 8) + 8:
  270. tctx->mtab = &ff_metasound_mode0808s;
  271. break;
  272. case (1 << 16) + (11 << 8) + 10:
  273. tctx->mtab = &ff_metasound_mode1110;
  274. break;
  275. case (2 << 16) + (11 << 8) + 10:
  276. tctx->mtab = &ff_metasound_mode1110s;
  277. break;
  278. case (1 << 16) + (16 << 8) + 16:
  279. tctx->mtab = &ff_metasound_mode1616;
  280. break;
  281. case (2 << 16) + (16 << 8) + 16:
  282. tctx->mtab = &ff_metasound_mode1616s;
  283. break;
  284. case (1 << 16) + (22 << 8) + 24:
  285. tctx->mtab = &ff_metasound_mode2224;
  286. break;
  287. case (2 << 16) + (22 << 8) + 24:
  288. tctx->mtab = &ff_metasound_mode2224s;
  289. break;
  290. case (1 << 16) + (44 << 8) + 32:
  291. tctx->mtab = &ff_metasound_mode4432;
  292. break;
  293. case (2 << 16) + (44 << 8) + 32:
  294. tctx->mtab = &ff_metasound_mode4432s;
  295. break;
  296. case (1 << 16) + (44 << 8) + 40:
  297. tctx->mtab = &ff_metasound_mode4440;
  298. break;
  299. case (2 << 16) + (44 << 8) + 40:
  300. tctx->mtab = &ff_metasound_mode4440s;
  301. break;
  302. case (1 << 16) + (44 << 8) + 48:
  303. tctx->mtab = &ff_metasound_mode4448;
  304. break;
  305. case (2 << 16) + (44 << 8) + 48:
  306. tctx->mtab = &ff_metasound_mode4448s;
  307. break;
  308. default:
  309. av_log(avctx, AV_LOG_ERROR,
  310. "This version does not support %d kHz - %d kbit/s/ch mode.\n",
  311. isampf, ibps);
  312. return AVERROR(ENOSYS);
  313. }
  314. tctx->codec = TWINVQ_CODEC_METASOUND;
  315. tctx->read_bitstream = metasound_read_bitstream;
  316. tctx->dec_bark_env = dec_bark_env;
  317. tctx->decode_ppc = decode_ppc;
  318. tctx->frame_size = avctx->bit_rate * tctx->mtab->size
  319. / avctx->sample_rate;
  320. tctx->is_6kbps = ibps == 6;
  321. return ff_twinvq_decode_init(avctx);
  322. }
  323. AVCodec ff_metasound_decoder = {
  324. .name = "metasound",
  325. .long_name = NULL_IF_CONFIG_SMALL("Voxware MetaSound"),
  326. .type = AVMEDIA_TYPE_AUDIO,
  327. .id = AV_CODEC_ID_METASOUND,
  328. .priv_data_size = sizeof(TwinVQContext),
  329. .init = metasound_decode_init,
  330. .close = ff_twinvq_decode_close,
  331. .decode = ff_twinvq_decode_frame,
  332. .capabilities = AV_CODEC_CAP_DR1,
  333. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
  334. AV_SAMPLE_FMT_NONE },
  335. };