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.

389 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 <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;
  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. for (tctx->cur_frame = 0; tctx->cur_frame < tctx->frames_per_packet;
  150. tctx->cur_frame++) {
  151. bits = tctx->bits + tctx->cur_frame;
  152. bits->window_type = get_bits(&gb, TWINVQ_WINDOW_TYPE_BITS);
  153. if (bits->window_type > 8) {
  154. av_log(avctx, AV_LOG_ERROR, "Invalid window type, broken sample?\n");
  155. return AVERROR_INVALIDDATA;
  156. }
  157. bits->ftype = ff_twinvq_wtype_to_ftype_table[tctx->bits[tctx->cur_frame].window_type];
  158. sub = mtab->fmode[bits->ftype].sub;
  159. if (bits->ftype != TWINVQ_FT_SHORT && !tctx->is_6kbps)
  160. get_bits(&gb, 2);
  161. read_cb_data(tctx, &gb, bits->main_coeffs, bits->ftype);
  162. for (i = 0; i < channels; i++)
  163. for (j = 0; j < sub; j++)
  164. for (k = 0; k < mtab->fmode[bits->ftype].bark_n_coef; k++)
  165. bits->bark1[i][j][k] =
  166. get_bits(&gb, mtab->fmode[bits->ftype].bark_n_bit);
  167. for (i = 0; i < channels; i++)
  168. for (j = 0; j < sub; j++)
  169. bits->bark_use_hist[i][j] = get_bits1(&gb);
  170. if (bits->ftype == TWINVQ_FT_LONG) {
  171. for (i = 0; i < channels; i++)
  172. bits->gain_bits[i] = get_bits(&gb, TWINVQ_GAIN_BITS);
  173. } else {
  174. for (i = 0; i < channels; i++) {
  175. bits->gain_bits[i] = get_bits(&gb, TWINVQ_GAIN_BITS);
  176. for (j = 0; j < sub; j++)
  177. bits->sub_gain_bits[i * sub + j] =
  178. get_bits(&gb, TWINVQ_SUB_GAIN_BITS);
  179. }
  180. }
  181. for (i = 0; i < channels; i++) {
  182. bits->lpc_hist_idx[i] = get_bits(&gb, mtab->lsp_bit0);
  183. bits->lpc_idx1[i] = get_bits(&gb, mtab->lsp_bit1);
  184. for (j = 0; j < mtab->lsp_split; j++)
  185. bits->lpc_idx2[i][j] = get_bits(&gb, mtab->lsp_bit2);
  186. }
  187. if (bits->ftype == TWINVQ_FT_LONG) {
  188. read_cb_data(tctx, &gb, bits->ppc_coeffs, 3);
  189. for (i = 0; i < channels; i++) {
  190. bits->p_coef[i] = get_bits(&gb, mtab->ppc_period_bit);
  191. bits->g_coef[i] = get_bits(&gb, mtab->pgain_bit);
  192. }
  193. }
  194. // subframes are aligned to nibbles
  195. if (get_bits_count(&gb) & 3)
  196. skip_bits(&gb, 4 - (get_bits_count(&gb) & 3));
  197. }
  198. return 0;
  199. }
  200. typedef struct MetasoundProps {
  201. uint32_t tag;
  202. int bit_rate;
  203. int channels;
  204. int sample_rate;
  205. } MetasoundProps;
  206. static const MetasoundProps codec_props[] = {
  207. { MKTAG('V','X','0','3'), 6, 1, 8000 },
  208. { MKTAG('V','X','0','4'), 12, 2, 8000 },
  209. { MKTAG('V','O','X','i'), 8, 1, 8000 },
  210. { MKTAG('V','O','X','j'), 10, 1, 11025 },
  211. { MKTAG('V','O','X','k'), 16, 1, 16000 },
  212. { MKTAG('V','O','X','L'), 24, 1, 22050 },
  213. { MKTAG('V','O','X','q'), 32, 1, 44100 },
  214. { MKTAG('V','O','X','r'), 40, 1, 44100 },
  215. { MKTAG('V','O','X','s'), 48, 1, 44100 },
  216. { MKTAG('V','O','X','t'), 16, 2, 8000 },
  217. { MKTAG('V','O','X','u'), 20, 2, 11025 },
  218. { MKTAG('V','O','X','v'), 32, 2, 16000 },
  219. { MKTAG('V','O','X','w'), 48, 2, 22050 },
  220. { MKTAG('V','O','X','x'), 64, 2, 44100 },
  221. { MKTAG('V','O','X','y'), 80, 2, 44100 },
  222. { MKTAG('V','O','X','z'), 96, 2, 44100 },
  223. { 0, 0, 0, 0 }
  224. };
  225. static av_cold int metasound_decode_init(AVCodecContext *avctx)
  226. {
  227. int isampf, ibps;
  228. TwinVQContext *tctx = avctx->priv_data;
  229. uint32_t tag;
  230. const MetasoundProps *props = codec_props;
  231. if (!avctx->extradata || avctx->extradata_size < 16) {
  232. av_log(avctx, AV_LOG_ERROR, "Missing or incomplete extradata\n");
  233. return AVERROR_INVALIDDATA;
  234. }
  235. tag = AV_RL32(avctx->extradata + 12);
  236. for (;;) {
  237. if (!props->tag) {
  238. av_log(avctx, AV_LOG_ERROR, "Could not find tag %08X\n", tag);
  239. return AVERROR_INVALIDDATA;
  240. }
  241. if (props->tag == tag) {
  242. avctx->sample_rate = props->sample_rate;
  243. avctx->channels = props->channels;
  244. avctx->bit_rate = props->bit_rate * 1000;
  245. isampf = avctx->sample_rate / 1000;
  246. break;
  247. }
  248. props++;
  249. }
  250. if (avctx->channels <= 0 || avctx->channels > TWINVQ_CHANNELS_MAX) {
  251. av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %i\n",
  252. avctx->channels);
  253. return AVERROR_INVALIDDATA;
  254. }
  255. avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO
  256. : AV_CH_LAYOUT_STEREO;
  257. ibps = avctx->bit_rate / (1000 * avctx->channels);
  258. switch ((avctx->channels << 16) + (isampf << 8) + ibps) {
  259. case (1 << 16) + ( 8 << 8) + 6:
  260. tctx->mtab = &ff_metasound_mode0806;
  261. break;
  262. case (2 << 16) + ( 8 << 8) + 6:
  263. tctx->mtab = &ff_metasound_mode0806s;
  264. break;
  265. case (1 << 16) + ( 8 << 8) + 8:
  266. tctx->mtab = &ff_metasound_mode0808;
  267. break;
  268. case (2 << 16) + ( 8 << 8) + 8:
  269. tctx->mtab = &ff_metasound_mode0808s;
  270. break;
  271. case (1 << 16) + (11 << 8) + 10:
  272. tctx->mtab = &ff_metasound_mode1110;
  273. break;
  274. case (2 << 16) + (11 << 8) + 10:
  275. tctx->mtab = &ff_metasound_mode1110s;
  276. break;
  277. case (1 << 16) + (16 << 8) + 16:
  278. tctx->mtab = &ff_metasound_mode1616;
  279. break;
  280. case (2 << 16) + (16 << 8) + 16:
  281. tctx->mtab = &ff_metasound_mode1616s;
  282. break;
  283. case (1 << 16) + (22 << 8) + 24:
  284. tctx->mtab = &ff_metasound_mode2224;
  285. break;
  286. case (2 << 16) + (22 << 8) + 24:
  287. tctx->mtab = &ff_metasound_mode2224s;
  288. break;
  289. case (1 << 16) + (44 << 8) + 32:
  290. tctx->mtab = &ff_metasound_mode4432;
  291. break;
  292. case (2 << 16) + (44 << 8) + 32:
  293. tctx->mtab = &ff_metasound_mode4432s;
  294. break;
  295. case (1 << 16) + (44 << 8) + 40:
  296. tctx->mtab = &ff_metasound_mode4440;
  297. break;
  298. case (2 << 16) + (44 << 8) + 40:
  299. tctx->mtab = &ff_metasound_mode4440s;
  300. break;
  301. case (1 << 16) + (44 << 8) + 48:
  302. tctx->mtab = &ff_metasound_mode4448;
  303. break;
  304. case (2 << 16) + (44 << 8) + 48:
  305. tctx->mtab = &ff_metasound_mode4448s;
  306. break;
  307. default:
  308. av_log(avctx, AV_LOG_ERROR,
  309. "This version does not support %d kHz - %d kbit/s/ch mode.\n",
  310. isampf, ibps);
  311. return AVERROR(ENOSYS);
  312. }
  313. tctx->codec = TWINVQ_CODEC_METASOUND;
  314. tctx->read_bitstream = metasound_read_bitstream;
  315. tctx->dec_bark_env = dec_bark_env;
  316. tctx->decode_ppc = decode_ppc;
  317. tctx->frame_size = avctx->bit_rate * tctx->mtab->size
  318. / avctx->sample_rate;
  319. tctx->is_6kbps = ibps == 6;
  320. return ff_twinvq_decode_init(avctx);
  321. }
  322. AVCodec ff_metasound_decoder = {
  323. .name = "metasound",
  324. .long_name = NULL_IF_CONFIG_SMALL("Voxware MetaSound"),
  325. .type = AVMEDIA_TYPE_AUDIO,
  326. .id = AV_CODEC_ID_METASOUND,
  327. .priv_data_size = sizeof(TwinVQContext),
  328. .init = metasound_decode_init,
  329. .close = ff_twinvq_decode_close,
  330. .decode = ff_twinvq_decode_frame,
  331. .capabilities = CODEC_CAP_DR1,
  332. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
  333. AV_SAMPLE_FMT_NONE },
  334. };