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.

344 lines
11KB

  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 <math.h>
  24. #include <stdint.h>
  25. #define BITSTREAM_READER_LE
  26. #include "libavutil/channel_layout.h"
  27. #include "libavutil/float_dsp.h"
  28. #include "avcodec.h"
  29. #include "get_bits.h"
  30. #include "fft.h"
  31. #include "internal.h"
  32. #include "lsp.h"
  33. #include "sinewin.h"
  34. #include "twinvq.h"
  35. #include "metasound_data.h"
  36. static void add_peak(float period, int width, const float *shape,
  37. float ppc_gain, float *speech, int len)
  38. {
  39. int i, j, center;
  40. const float *shape_end = shape + len;
  41. // First peak centered around zero
  42. for (i = 0; i < width / 2; i++)
  43. speech[i] += ppc_gain * *shape++;
  44. for (i = 1; i < ROUNDED_DIV(len, width); i++) {
  45. center = (int)(i * period + 0.5);
  46. for (j = -width / 2; j < (width + 1) / 2; j++)
  47. speech[j + center] += ppc_gain * *shape++;
  48. }
  49. // For the last block, be careful not to go beyond the end of the buffer
  50. center = (int)(i * period + 0.5);
  51. for (j = -width / 2; j < (width + 1) / 2 && shape < shape_end; j++)
  52. speech[j + center] += ppc_gain * *shape++;
  53. }
  54. static void decode_ppc(TwinVQContext *tctx, int period_coef, int g_coef,
  55. const float *shape, float *speech)
  56. {
  57. const TwinVQModeTab *mtab = tctx->mtab;
  58. int isampf = tctx->avctx->sample_rate / 1000;
  59. int ibps = tctx->avctx->bit_rate / (1000 * tctx->avctx->channels);
  60. int width;
  61. float ratio = (float)mtab->size / isampf;
  62. float min_period, max_period, period_range, period;
  63. float some_mult;
  64. float pgain_base, pgain_step, ppc_gain;
  65. if (tctx->avctx->channels == 1) {
  66. min_period = log2(ratio * 0.2);
  67. max_period = min_period + log2(6);
  68. } else {
  69. min_period = (int)(ratio * 0.2 * 400 + 0.5) / 400.0;
  70. max_period = (int)(ratio * 0.2 * 400 * 6 + 0.5) / 400.0;
  71. }
  72. period_range = max_period - min_period;
  73. period = min_period + period_coef * period_range /
  74. ((1 << mtab->ppc_period_bit) - 1);
  75. if (tctx->avctx->channels == 1)
  76. period = powf(2.0, period);
  77. else
  78. period = (int)(period * 400 + 0.5) / 400.0;
  79. switch (isampf) {
  80. case 8: some_mult = 2.0; break;
  81. case 11: some_mult = 3.0; break;
  82. case 16: some_mult = 3.0; break;
  83. case 22: some_mult = ibps == 32 ? 2.0 : 4.0; break;
  84. case 44: some_mult = 8.0; break;
  85. default: some_mult = 4.0;
  86. }
  87. width = (int)(some_mult / (mtab->size / period) * mtab->ppc_shape_len);
  88. if (isampf == 22 && ibps == 32)
  89. width = (int)((2.0 / period + 1) * width + 0.5);
  90. pgain_base = tctx->avctx->channels == 2 ? 25000.0 : 20000.0;
  91. pgain_step = pgain_base / ((1 << mtab->pgain_bit) - 1);
  92. ppc_gain = 1.0 / 8192 *
  93. twinvq_mulawinv(pgain_step * g_coef + pgain_step / 2,
  94. pgain_base, TWINVQ_PGAIN_MU);
  95. add_peak(period, width, shape, ppc_gain, speech, mtab->ppc_shape_len);
  96. }
  97. static void dec_bark_env(TwinVQContext *tctx, const uint8_t *in, int use_hist,
  98. int ch, float *out, float gain,
  99. enum TwinVQFrameType ftype)
  100. {
  101. const TwinVQModeTab *mtab = tctx->mtab;
  102. int i, j;
  103. float *hist = tctx->bark_hist[ftype][ch];
  104. float val = ((const float []) { 0.4, 0.35, 0.28 })[ftype];
  105. int bark_n_coef = mtab->fmode[ftype].bark_n_coef;
  106. int fw_cb_len = mtab->fmode[ftype].bark_env_size / bark_n_coef;
  107. int idx = 0;
  108. if (tctx->avctx->channels == 1)
  109. val = 0.5;
  110. for (i = 0; i < fw_cb_len; i++)
  111. for (j = 0; j < bark_n_coef; j++, idx++) {
  112. float tmp2 = mtab->fmode[ftype].bark_cb[fw_cb_len * in[j] + i] *
  113. (1.0 / 2048);
  114. float st;
  115. if (tctx->avctx->channels == 1)
  116. st = use_hist ?
  117. tmp2 + val * hist[idx] + 1.0 : tmp2 + 1.0;
  118. else
  119. st = use_hist ? (1.0 - val) * tmp2 + val * hist[idx] + 1.0
  120. : tmp2 + 1.0;
  121. hist[idx] = tmp2;
  122. if (st < 0.1)
  123. st = 0.1;
  124. twinvq_memset_float(out, st * gain,
  125. mtab->fmode[ftype].bark_tab[idx]);
  126. out += mtab->fmode[ftype].bark_tab[idx];
  127. }
  128. }
  129. static void read_cb_data(TwinVQContext *tctx, GetBitContext *gb,
  130. uint8_t *dst, enum TwinVQFrameType ftype)
  131. {
  132. int i;
  133. for (i = 0; i < tctx->n_div[ftype]; i++) {
  134. int bs_second_part = (i >= tctx->bits_main_spec_change[ftype]);
  135. *dst++ = get_bits(gb, tctx->bits_main_spec[0][ftype][bs_second_part]);
  136. *dst++ = get_bits(gb, tctx->bits_main_spec[1][ftype][bs_second_part]);
  137. }
  138. }
  139. static int metasound_read_bitstream(AVCodecContext *avctx, TwinVQContext *tctx,
  140. const uint8_t *buf, int buf_size)
  141. {
  142. TwinVQFrameData *bits = &tctx->bits;
  143. const TwinVQModeTab *mtab = tctx->mtab;
  144. int channels = tctx->avctx->channels;
  145. int sub;
  146. GetBitContext gb;
  147. int i, j, k;
  148. init_get_bits(&gb, buf, buf_size * 8);
  149. bits->window_type = get_bits(&gb, TWINVQ_WINDOW_TYPE_BITS);
  150. if (bits->window_type > 8) {
  151. av_log(avctx, AV_LOG_ERROR, "Invalid window type, broken sample?\n");
  152. return AVERROR_INVALIDDATA;
  153. }
  154. bits->ftype = ff_twinvq_wtype_to_ftype_table[tctx->bits.window_type];
  155. sub = mtab->fmode[bits->ftype].sub;
  156. if (bits->ftype != TWINVQ_FT_SHORT)
  157. get_bits(&gb, 2);
  158. read_cb_data(tctx, &gb, bits->main_coeffs, bits->ftype);
  159. for (i = 0; i < channels; i++)
  160. for (j = 0; j < sub; j++)
  161. for (k = 0; k < mtab->fmode[bits->ftype].bark_n_coef; k++)
  162. bits->bark1[i][j][k] =
  163. get_bits(&gb, mtab->fmode[bits->ftype].bark_n_bit);
  164. for (i = 0; i < channels; i++)
  165. for (j = 0; j < sub; j++)
  166. bits->bark_use_hist[i][j] = get_bits1(&gb);
  167. if (bits->ftype == TWINVQ_FT_LONG) {
  168. for (i = 0; i < channels; i++)
  169. bits->gain_bits[i] = get_bits(&gb, TWINVQ_GAIN_BITS);
  170. } else {
  171. for (i = 0; i < channels; i++) {
  172. bits->gain_bits[i] = get_bits(&gb, TWINVQ_GAIN_BITS);
  173. for (j = 0; j < sub; j++)
  174. bits->sub_gain_bits[i * sub + j] =
  175. get_bits(&gb, TWINVQ_SUB_GAIN_BITS);
  176. }
  177. }
  178. for (i = 0; i < channels; i++) {
  179. bits->lpc_hist_idx[i] = get_bits(&gb, mtab->lsp_bit0);
  180. bits->lpc_idx1[i] = get_bits(&gb, mtab->lsp_bit1);
  181. for (j = 0; j < mtab->lsp_split; j++)
  182. bits->lpc_idx2[i][j] = get_bits(&gb, mtab->lsp_bit2);
  183. }
  184. if (bits->ftype == TWINVQ_FT_LONG) {
  185. read_cb_data(tctx, &gb, bits->ppc_coeffs, 3);
  186. for (i = 0; i < channels; i++) {
  187. bits->p_coef[i] = get_bits(&gb, mtab->ppc_period_bit);
  188. bits->g_coef[i] = get_bits(&gb, mtab->pgain_bit);
  189. }
  190. }
  191. return 0;
  192. }
  193. typedef struct MetasoundProps {
  194. uint32_t tag;
  195. int bit_rate;
  196. int channels;
  197. int sample_rate;
  198. } MetasoundProps;
  199. static const MetasoundProps codec_props[] = {
  200. { MKTAG('V','X','0','3'), 6, 1, 8000 },
  201. { MKTAG('V','X','0','4'), 12, 2, 8000 },
  202. { MKTAG('V','O','X','i'), 8, 1, 8000 },
  203. { MKTAG('V','O','X','j'), 10, 1, 11025 },
  204. { MKTAG('V','O','X','k'), 16, 1, 16000 },
  205. { MKTAG('V','O','X','L'), 24, 1, 22050 },
  206. { MKTAG('V','O','X','q'), 32, 1, 44100 },
  207. { MKTAG('V','O','X','r'), 40, 1, 44100 },
  208. { MKTAG('V','O','X','s'), 48, 1, 44100 },
  209. { MKTAG('V','O','X','t'), 16, 2, 8000 },
  210. { MKTAG('V','O','X','u'), 20, 2, 11025 },
  211. { MKTAG('V','O','X','v'), 32, 2, 16000 },
  212. { MKTAG('V','O','X','w'), 48, 2, 22050 },
  213. { MKTAG('V','O','X','x'), 64, 2, 44100 },
  214. { MKTAG('V','O','X','y'), 80, 2, 44100 },
  215. { MKTAG('V','O','X','z'), 96, 2, 44100 },
  216. { 0, 0, 0, 0 }
  217. };
  218. static av_cold int metasound_decode_init(AVCodecContext *avctx)
  219. {
  220. int isampf, ibps;
  221. TwinVQContext *tctx = avctx->priv_data;
  222. uint32_t tag;
  223. const MetasoundProps *props = codec_props;
  224. if (!avctx->extradata || avctx->extradata_size < 16) {
  225. av_log(avctx, AV_LOG_ERROR, "Missing or incomplete extradata\n");
  226. return AVERROR_INVALIDDATA;
  227. }
  228. tag = AV_RL32(avctx->extradata + 12);
  229. for (;;) {
  230. if (!props->tag) {
  231. av_log(avctx, AV_LOG_ERROR, "Could not find tag %08X\n", tag);
  232. return AVERROR_INVALIDDATA;
  233. }
  234. if (props->tag == tag) {
  235. avctx->sample_rate = props->sample_rate;
  236. avctx->channels = props->channels;
  237. avctx->bit_rate = props->bit_rate * 1000;
  238. isampf = avctx->sample_rate / 1000;
  239. break;
  240. }
  241. props++;
  242. }
  243. if (avctx->channels <= 0 || avctx->channels > TWINVQ_CHANNELS_MAX) {
  244. av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %i\n",
  245. avctx->channels);
  246. return AVERROR_INVALIDDATA;
  247. }
  248. avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO
  249. : AV_CH_LAYOUT_STEREO;
  250. ibps = avctx->bit_rate / (1000 * avctx->channels);
  251. switch ((avctx->channels << 16) + (isampf << 8) + ibps) {
  252. case (1 << 16) + ( 8 << 8) + 8:
  253. tctx->mtab = &ff_metasound_mode0808;
  254. break;
  255. case (1 << 16) + (16 << 8) + 16:
  256. tctx->mtab = &ff_metasound_mode1616;
  257. break;
  258. case (1 << 16) + (44 << 8) + 32:
  259. tctx->mtab = &ff_metasound_mode4432;
  260. break;
  261. case (2 << 16) + (44 << 8) + 48:
  262. tctx->mtab = &ff_metasound_mode4448s;
  263. break;
  264. default:
  265. av_log(avctx, AV_LOG_ERROR,
  266. "This version does not support %d kHz - %d kbit/s/ch mode.\n",
  267. isampf, isampf);
  268. return AVERROR(ENOSYS);
  269. }
  270. avctx->block_align = (avctx->bit_rate * tctx->mtab->size
  271. / avctx->sample_rate + 7) / 8;
  272. tctx->codec = TWINVQ_CODEC_METASOUND;
  273. tctx->read_bitstream = metasound_read_bitstream;
  274. tctx->dec_bark_env = dec_bark_env;
  275. tctx->decode_ppc = decode_ppc;
  276. return ff_twinvq_decode_init(avctx);
  277. }
  278. AVCodec ff_metasound_decoder = {
  279. .name = "metasound",
  280. .long_name = NULL_IF_CONFIG_SMALL("Voxware MetaSound"),
  281. .type = AVMEDIA_TYPE_AUDIO,
  282. .id = AV_CODEC_ID_METASOUND,
  283. .priv_data_size = sizeof(TwinVQContext),
  284. .init = metasound_decode_init,
  285. .close = ff_twinvq_decode_close,
  286. .decode = ff_twinvq_decode_frame,
  287. .capabilities = CODEC_CAP_DR1,
  288. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
  289. AV_SAMPLE_FMT_NONE },
  290. };