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.

784 lines
25KB

  1. /*
  2. * QDMC compatible decoder
  3. * Copyright (c) 2017 Paul B Mahol
  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 <math.h>
  22. #include <stddef.h>
  23. #include <stdio.h>
  24. #define BITSTREAM_READER_LE
  25. #include "libavutil/channel_layout.h"
  26. #include "avcodec.h"
  27. #include "bytestream.h"
  28. #include "get_bits.h"
  29. #include "internal.h"
  30. #include "fft.h"
  31. typedef struct QDMCTone {
  32. uint8_t mode;
  33. uint8_t phase;
  34. uint8_t offset;
  35. int16_t freq;
  36. int16_t amplitude;
  37. } QDMCTone;
  38. typedef struct QDMCContext {
  39. AVCodecContext *avctx;
  40. uint8_t frame_bits;
  41. int band_index;
  42. int frame_size;
  43. int subframe_size;
  44. int fft_offset;
  45. int buffer_offset;
  46. int nb_channels;
  47. int checksum_size;
  48. uint8_t noise[2][19][17];
  49. QDMCTone tones[5][8192];
  50. int nb_tones[5];
  51. int cur_tone[5];
  52. float alt_sin[5][31];
  53. float fft_buffer[4][8192 * 2];
  54. float noise2_buffer[4096 * 2];
  55. float noise_buffer[4096 * 2];
  56. float buffer[2 * 32768];
  57. float *buffer_ptr;
  58. int rndval;
  59. DECLARE_ALIGNED(32, FFTComplex, cmplx)[2][512];
  60. FFTContext fft_ctx;
  61. } QDMCContext;
  62. static float sin_table[512];
  63. static VLC vtable[6];
  64. static const unsigned code_prefix[] = {
  65. 0x0, 0x1, 0x2, 0x3, 0x4, 0x6, 0x8, 0xA,
  66. 0xC, 0x10, 0x14, 0x18, 0x1C, 0x24, 0x2C, 0x34,
  67. 0x3C, 0x4C, 0x5C, 0x6C, 0x7C, 0x9C, 0xBC, 0xDC,
  68. 0xFC, 0x13C, 0x17C, 0x1BC, 0x1FC, 0x27C, 0x2FC, 0x37C,
  69. 0x3FC, 0x4FC, 0x5FC, 0x6FC, 0x7FC, 0x9FC, 0xBFC, 0xDFC,
  70. 0xFFC, 0x13FC, 0x17FC, 0x1BFC, 0x1FFC, 0x27FC, 0x2FFC, 0x37FC,
  71. 0x3FFC, 0x4FFC, 0x5FFC, 0x6FFC, 0x7FFC, 0x9FFC, 0xBFFC, 0xDFFC,
  72. 0xFFFC, 0x13FFC, 0x17FFC, 0x1BFFC, 0x1FFFC, 0x27FFC, 0x2FFFC, 0x37FFC,
  73. 0x3FFFC
  74. };
  75. static const float amplitude_tab[64] = {
  76. 1.18750000f, 1.68359380f, 2.37500000f, 3.36718750f, 4.75000000f,
  77. 6.73437500f, 9.50000000f, 13.4687500f, 19.0000000f, 26.9375000f,
  78. 38.0000000f, 53.8750000f, 76.0000000f, 107.750000f, 152.000000f,
  79. 215.500000f, 304.000000f, 431.000000f, 608.000000f, 862.000000f,
  80. 1216.00000f, 1724.00000f, 2432.00000f, 3448.00000f, 4864.00000f,
  81. 6896.00000f, 9728.00000f, 13792.0000f, 19456.0000f, 27584.0000f,
  82. 38912.0000f, 55168.0000f, 77824.0000f, 110336.000f, 155648.000f,
  83. 220672.000f, 311296.000f, 441344.000f, 622592.000f, 882688.000f,
  84. 1245184.00f, 1765376.00f, 2490368.00f, 3530752.00f, 4980736.00f,
  85. 7061504.00f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  86. };
  87. static const uint16_t qdmc_nodes[112] = {
  88. 0, 1, 2, 4, 6, 8, 12, 16, 24, 32, 48, 56, 64,
  89. 80, 96, 120, 144, 176, 208, 240, 256,
  90. 0, 2, 4, 8, 16, 24, 32, 48, 56, 64, 80, 104,
  91. 128, 160, 208, 256, 0, 0, 0, 0, 0,
  92. 0, 2, 4, 8, 16, 32, 48, 64, 80, 112, 160, 208,
  93. 256, 0, 0, 0, 0, 0, 0, 0, 0,
  94. 0, 4, 8, 16, 32, 48, 64, 96, 144, 208, 256,
  95. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  96. 0, 4, 16, 32, 64, 256, 0, 0, 0, 0, 0, 0, 0, 0,
  97. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  98. };
  99. static const uint8_t noise_bands_size[] = {
  100. 19, 14, 11, 9, 4, 2, 0
  101. };
  102. static const uint8_t noise_bands_selector[] = {
  103. 4, 3, 2, 1, 0, 0, 0,
  104. };
  105. static const uint8_t noise_value_bits[] = {
  106. 12, 7, 9, 7, 10, 9, 11, 9, 9, 2, 9, 9, 9, 9,
  107. 9, 3, 9, 10, 10, 12, 2, 3, 3, 5, 5, 6, 7,
  108. };
  109. static const uint8_t noise_value_symbols[] = {
  110. 0, 10, 11, 12, 13, 14, 15, 16, 18, 1, 20, 22, 24,
  111. 26, 28, 2, 30, 32, 34, 36, 3, 4, 5, 6, 7, 8, 9,
  112. };
  113. static const uint16_t noise_value_codes[] = {
  114. 0xC7A, 0x002, 0x0FA, 0x03A, 0x35A, 0x1C2, 0x07A, 0x1FA,
  115. 0x17A, 0x000, 0x0DA, 0x142, 0x0C2, 0x042, 0x1DA, 0x001,
  116. 0x05A, 0x15A, 0x27A, 0x47A, 0x003, 0x005, 0x006, 0x012,
  117. 0x00A, 0x022, 0x01A,
  118. };
  119. static const uint8_t noise_segment_length_bits[] = {
  120. 10, 8, 5, 1, 2, 4, 4, 4, 6, 7, 9, 10,
  121. };
  122. static const uint8_t noise_segment_length_symbols[] = {
  123. 0, 13, 17, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  124. };
  125. static const uint16_t noise_segment_length_codes[] = {
  126. 0x30B, 0x8B, 0x1B, 0x0, 0x1, 0x3, 0x7, 0xF, 0x2b, 0x4B, 0xB, 0x10B,
  127. };
  128. static const uint8_t freq_diff_bits[] = {
  129. 18, 2, 4, 4, 5, 4, 4, 5, 5, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 6,
  130. 7, 6, 6, 6, 7, 7, 7, 7, 7, 8, 9, 9, 8, 9, 11, 11, 12, 12, 13, 12,
  131. 14, 15, 18, 16, 17,
  132. };
  133. static const uint32_t freq_diff_codes[] = {
  134. 0x2AD46, 0x1, 0x0, 0x3, 0xC, 0xA, 0x7, 0x18, 0x12, 0xE, 0x4, 0x16,
  135. 0xF, 0x1C, 0x8, 0x22, 0x26, 0x2, 0x3B, 0x34, 0x74, 0x1F, 0x14, 0x2B,
  136. 0x1B, 0x3F, 0x28, 0x54, 0x6, 0x4B, 0xB, 0x68, 0xE8, 0x46, 0xC6, 0x1E8,
  137. 0x146, 0x346, 0x546, 0x746, 0x1D46, 0xF46, 0xD46, 0x6D46, 0xAD46, 0x2D46,
  138. 0x1AD46,
  139. };
  140. static const uint8_t amplitude_bits[] = {
  141. 13, 7, 8, 9, 10, 10, 10, 10, 10, 9, 8, 7, 6,
  142. 5, 4, 3, 3, 2, 3, 3, 4, 5, 7, 8, 9, 11, 12, 13,
  143. };
  144. static const uint16_t amplitude_codes[] = {
  145. 0x1EC6, 0x6, 0xC2, 0x142, 0x242, 0x246, 0xC6, 0x46, 0x42, 0x146, 0xA2,
  146. 0x62, 0x26, 0x16, 0xE, 0x5, 0x4, 0x3, 0x0, 0x1, 0xA, 0x12, 0x2, 0x22,
  147. 0x1C6, 0x2C6, 0x6C6, 0xEC6,
  148. };
  149. static const uint8_t amplitude_diff_bits[] = {
  150. 8, 2, 1, 3, 4, 5, 6, 7, 8,
  151. };
  152. static const uint8_t amplitude_diff_codes[] = {
  153. 0xFE, 0x0, 0x1, 0x2, 0x6, 0xE, 0x1E, 0x3E, 0x7E,
  154. };
  155. static const uint8_t phase_diff_bits[] = {
  156. 6, 2, 2, 4, 4, 6, 5, 4, 2,
  157. };
  158. static const uint8_t phase_diff_codes[] = {
  159. 0x35, 0x2, 0x0, 0x1, 0xD, 0x15, 0x5, 0x9, 0x3,
  160. };
  161. #define INIT_VLC_STATIC_LE(vlc, nb_bits, nb_codes, \
  162. bits, bits_wrap, bits_size, \
  163. codes, codes_wrap, codes_size, \
  164. symbols, symbols_wrap, symbols_size, \
  165. static_size) \
  166. do { \
  167. static VLC_TYPE table[static_size][2]; \
  168. (vlc)->table = table; \
  169. (vlc)->table_allocated = static_size; \
  170. ff_init_vlc_sparse(vlc, nb_bits, nb_codes, \
  171. bits, bits_wrap, bits_size, \
  172. codes, codes_wrap, codes_size, \
  173. symbols, symbols_wrap, symbols_size, \
  174. INIT_VLC_LE | INIT_VLC_USE_NEW_STATIC); \
  175. } while (0)
  176. static av_cold void qdmc_init_static_data(AVCodec *codec)
  177. {
  178. int i;
  179. INIT_VLC_STATIC_LE(&vtable[0], 12, FF_ARRAY_ELEMS(noise_value_bits),
  180. noise_value_bits, 1, 1, noise_value_codes, 2, 2, noise_value_symbols, 1, 1, 4096);
  181. INIT_VLC_STATIC_LE(&vtable[1], 10, FF_ARRAY_ELEMS(noise_segment_length_bits),
  182. noise_segment_length_bits, 1, 1, noise_segment_length_codes, 2, 2,
  183. noise_segment_length_symbols, 1, 1, 1024);
  184. INIT_VLC_STATIC_LE(&vtable[2], 13, FF_ARRAY_ELEMS(amplitude_bits),
  185. amplitude_bits, 1, 1, amplitude_codes, 2, 2, NULL, 0, 0, 8192);
  186. INIT_VLC_STATIC_LE(&vtable[3], 18, FF_ARRAY_ELEMS(freq_diff_bits),
  187. freq_diff_bits, 1, 1, freq_diff_codes, 4, 4, NULL, 0, 0, 262144);
  188. INIT_VLC_STATIC_LE(&vtable[4], 8, FF_ARRAY_ELEMS(amplitude_diff_bits),
  189. amplitude_diff_bits, 1, 1, amplitude_diff_codes, 1, 1, NULL, 0, 0, 256);
  190. INIT_VLC_STATIC_LE(&vtable[5], 6, FF_ARRAY_ELEMS(phase_diff_bits),
  191. phase_diff_bits, 1, 1, phase_diff_codes, 1, 1, NULL, 0, 0, 64);
  192. for (i = 0; i < 512; i++)
  193. sin_table[i] = sin(2.0f * i * M_PI * 0.001953125f);
  194. }
  195. static void make_noises(QDMCContext *s)
  196. {
  197. int i, j, n0, n1, n2, diff;
  198. float *nptr;
  199. for (j = 0; j < noise_bands_size[s->band_index]; j++) {
  200. n0 = qdmc_nodes[j + 21 * s->band_index ];
  201. n1 = qdmc_nodes[j + 21 * s->band_index + 1];
  202. n2 = qdmc_nodes[j + 21 * s->band_index + 2];
  203. nptr = s->noise_buffer + 256 * j;
  204. for (i = 0; i + n0 < n1; i++, nptr++)
  205. nptr[0] = i / (float)(n1 - n0);
  206. diff = n2 - n1;
  207. nptr = s->noise_buffer + (j << 8) + n1 - n0;
  208. for (i = n1; i < n2; i++, nptr++, diff--)
  209. nptr[0] = diff / (float)(n2 - n1);
  210. }
  211. }
  212. static av_cold int qdmc_decode_init(AVCodecContext *avctx)
  213. {
  214. QDMCContext *s = avctx->priv_data;
  215. int fft_size, fft_order, size, g, j, x;
  216. GetByteContext b;
  217. if (!avctx->extradata || (avctx->extradata_size < 48)) {
  218. av_log(avctx, AV_LOG_ERROR, "extradata missing or truncated\n");
  219. return AVERROR_INVALIDDATA;
  220. }
  221. bytestream2_init(&b, avctx->extradata, avctx->extradata_size);
  222. while (bytestream2_get_bytes_left(&b) > 8) {
  223. if (bytestream2_peek_be64(&b) == (((uint64_t)MKBETAG('f','r','m','a') << 32) |
  224. (uint64_t)MKBETAG('Q','D','M','C')))
  225. break;
  226. bytestream2_skipu(&b, 1);
  227. }
  228. bytestream2_skipu(&b, 8);
  229. if (bytestream2_get_bytes_left(&b) < 36) {
  230. av_log(avctx, AV_LOG_ERROR, "not enough extradata (%i)\n",
  231. bytestream2_get_bytes_left(&b));
  232. return AVERROR_INVALIDDATA;
  233. }
  234. size = bytestream2_get_be32u(&b);
  235. if (size > bytestream2_get_bytes_left(&b)) {
  236. av_log(avctx, AV_LOG_ERROR, "extradata size too small, %i < %i\n",
  237. bytestream2_get_bytes_left(&b), size);
  238. return AVERROR_INVALIDDATA;
  239. }
  240. if (bytestream2_get_be32u(&b) != MKBETAG('Q','D','C','A')) {
  241. av_log(avctx, AV_LOG_ERROR, "invalid extradata, expecting QDCA\n");
  242. return AVERROR_INVALIDDATA;
  243. }
  244. bytestream2_skipu(&b, 4);
  245. avctx->channels = s->nb_channels = bytestream2_get_be32u(&b);
  246. if (s->nb_channels <= 0 || s->nb_channels > 2) {
  247. av_log(avctx, AV_LOG_ERROR, "invalid number of channels\n");
  248. return AVERROR_INVALIDDATA;
  249. }
  250. avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
  251. AV_CH_LAYOUT_MONO;
  252. avctx->sample_rate = bytestream2_get_be32u(&b);
  253. avctx->bit_rate = bytestream2_get_be32u(&b);
  254. bytestream2_skipu(&b, 4);
  255. fft_size = bytestream2_get_be32u(&b);
  256. fft_order = av_log2(fft_size) + 1;
  257. s->checksum_size = bytestream2_get_be32u(&b);
  258. if (s->checksum_size >= 1U << 28) {
  259. av_log(avctx, AV_LOG_ERROR, "data block size too large (%u)\n", s->checksum_size);
  260. return AVERROR_INVALIDDATA;
  261. }
  262. if (avctx->sample_rate >= 32000) {
  263. x = 28000;
  264. s->frame_bits = 13;
  265. } else if (avctx->sample_rate >= 16000) {
  266. x = 20000;
  267. s->frame_bits = 12;
  268. } else {
  269. x = 16000;
  270. s->frame_bits = 11;
  271. }
  272. s->frame_size = 1 << s->frame_bits;
  273. s->subframe_size = s->frame_size >> 5;
  274. if (avctx->channels == 2)
  275. x = 3 * x / 2;
  276. s->band_index = noise_bands_selector[FFMIN(6, llrint(floor(avctx->bit_rate * 3.0 / (double)x + 0.5)))];
  277. if ((fft_order < 7) || (fft_order > 9)) {
  278. avpriv_request_sample(avctx, "Unknown FFT order %d", fft_order);
  279. return AVERROR_PATCHWELCOME;
  280. }
  281. if (fft_size != (1 << (fft_order - 1))) {
  282. av_log(avctx, AV_LOG_ERROR, "FFT size %d not power of 2.\n", fft_size);
  283. return AVERROR_INVALIDDATA;
  284. }
  285. ff_fft_init(&s->fft_ctx, fft_order, 1);
  286. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  287. for (g = 5; g > 0; g--) {
  288. for (j = 0; j < (1 << g) - 1; j++)
  289. s->alt_sin[5-g][j] = sin_table[(((j+1) << (8 - g)) & 0x1FF)];
  290. }
  291. make_noises(s);
  292. return 0;
  293. }
  294. static av_cold int qdmc_decode_close(AVCodecContext *avctx)
  295. {
  296. QDMCContext *s = avctx->priv_data;
  297. ff_fft_end(&s->fft_ctx);
  298. return 0;
  299. }
  300. static int qdmc_get_vlc(GetBitContext *gb, VLC *table, int flag)
  301. {
  302. int v;
  303. v = get_vlc2(gb, table->table, table->bits, 1);
  304. if (v < 0)
  305. return AVERROR_INVALIDDATA;
  306. if (v)
  307. v = v - 1;
  308. else
  309. v = get_bits(gb, get_bits(gb, 3) + 1);
  310. if (flag) {
  311. if (v >= FF_ARRAY_ELEMS(code_prefix))
  312. return AVERROR_INVALIDDATA;
  313. v = code_prefix[v] + get_bitsz(gb, v >> 2);
  314. }
  315. return v;
  316. }
  317. static int skip_label(QDMCContext *s, GetBitContext *gb)
  318. {
  319. uint32_t label = get_bits_long(gb, 32);
  320. uint16_t sum = 226, checksum = get_bits(gb, 16);
  321. const uint8_t *ptr = gb->buffer + 6;
  322. int i;
  323. if (label != MKTAG('Q', 'M', 'C', 1))
  324. return AVERROR_INVALIDDATA;
  325. for (i = 0; i < s->checksum_size - 6; i++)
  326. sum += ptr[i];
  327. return sum != checksum;
  328. }
  329. static int read_noise_data(QDMCContext *s, GetBitContext *gb)
  330. {
  331. int ch, j, k, v, idx, band, lastval, newval, len;
  332. for (ch = 0; ch < s->nb_channels; ch++) {
  333. for (band = 0; band < noise_bands_size[s->band_index]; band++) {
  334. v = qdmc_get_vlc(gb, &vtable[0], 0);
  335. if (v < 0)
  336. return AVERROR_INVALIDDATA;
  337. if (v & 1)
  338. v = v + 1;
  339. else
  340. v = -v;
  341. lastval = v / 2;
  342. s->noise[ch][band][0] = lastval - 1;
  343. for (j = 0; j < 15;) {
  344. len = qdmc_get_vlc(gb, &vtable[1], 1);
  345. if (len < 0)
  346. return AVERROR_INVALIDDATA;
  347. len += 1;
  348. v = qdmc_get_vlc(gb, &vtable[0], 0);
  349. if (v < 0)
  350. return AVERROR_INVALIDDATA;
  351. if (v & 1)
  352. newval = lastval + (v + 1) / 2;
  353. else
  354. newval = lastval - v / 2;
  355. idx = j + 1;
  356. if (len + idx > 16)
  357. return AVERROR_INVALIDDATA;
  358. for (k = 1; idx <= j + len; k++, idx++)
  359. s->noise[ch][band][idx] = lastval + k * (newval - lastval) / len - 1;
  360. lastval = newval;
  361. j += len;
  362. }
  363. }
  364. }
  365. return 0;
  366. }
  367. static void add_tone(QDMCContext *s, int group, int offset, int freq, int stereo_mode, int amplitude, int phase)
  368. {
  369. const int index = s->nb_tones[group];
  370. if (index >= FF_ARRAY_ELEMS(s->tones[group])) {
  371. av_log(s->avctx, AV_LOG_WARNING, "Too many tones already in buffer, ignoring tone!\n");
  372. return;
  373. }
  374. s->tones[group][index].offset = offset;
  375. s->tones[group][index].freq = freq;
  376. s->tones[group][index].mode = stereo_mode;
  377. s->tones[group][index].amplitude = amplitude;
  378. s->tones[group][index].phase = phase;
  379. s->nb_tones[group]++;
  380. }
  381. static int read_wave_data(QDMCContext *s, GetBitContext *gb)
  382. {
  383. int amp, phase, stereo_mode = 0, i, group, freq, group_size, group_bits;
  384. int amp2, phase2, pos2, off;
  385. for (group = 0; group < 5; group++) {
  386. group_size = 1 << (s->frame_bits - group - 1);
  387. group_bits = 4 - group;
  388. pos2 = 0;
  389. off = 0;
  390. for (i = 1; ; i = freq + 1) {
  391. int v;
  392. v = qdmc_get_vlc(gb, &vtable[3], 1);
  393. if (v < 0)
  394. return AVERROR_INVALIDDATA;
  395. freq = i + v;
  396. while (freq >= group_size - 1) {
  397. freq += 2 - group_size;
  398. pos2 += group_size;
  399. off += 1 << group_bits;
  400. }
  401. if (pos2 >= s->frame_size)
  402. break;
  403. if (s->nb_channels > 1)
  404. stereo_mode = get_bits(gb, 2);
  405. amp = qdmc_get_vlc(gb, &vtable[2], 0);
  406. if (amp < 0)
  407. return AVERROR_INVALIDDATA;
  408. phase = get_bits(gb, 3);
  409. if (stereo_mode > 1) {
  410. amp2 = qdmc_get_vlc(gb, &vtable[4], 0);
  411. if (amp2 < 0)
  412. return AVERROR_INVALIDDATA;
  413. amp2 = amp - amp2;
  414. phase2 = qdmc_get_vlc(gb, &vtable[5], 0);
  415. if (phase2 < 0)
  416. return AVERROR_INVALIDDATA;
  417. phase2 = phase - phase2;
  418. if (phase2 < 0)
  419. phase2 += 8;
  420. }
  421. if ((freq >> group_bits) + 1 < s->subframe_size) {
  422. add_tone(s, group, off, freq, stereo_mode & 1, amp, phase);
  423. if (stereo_mode > 1)
  424. add_tone(s, group, off, freq, ~stereo_mode & 1, amp2, phase2);
  425. }
  426. }
  427. }
  428. return 0;
  429. }
  430. static void lin_calc(QDMCContext *s, float amplitude, int node1, int node2, int index)
  431. {
  432. int subframe_size, i, j, k, length;
  433. float scale, *noise_ptr;
  434. scale = 0.5 * amplitude;
  435. subframe_size = s->subframe_size;
  436. if (subframe_size >= node2)
  437. subframe_size = node2;
  438. length = (subframe_size - node1) & 0xFFFC;
  439. j = node1;
  440. noise_ptr = &s->noise_buffer[256 * index];
  441. for (i = 0; i < length; i += 4, j+= 4, noise_ptr += 4) {
  442. s->noise2_buffer[j ] += scale * noise_ptr[0];
  443. s->noise2_buffer[j + 1] += scale * noise_ptr[1];
  444. s->noise2_buffer[j + 2] += scale * noise_ptr[2];
  445. s->noise2_buffer[j + 3] += scale * noise_ptr[3];
  446. }
  447. k = length + node1;
  448. noise_ptr = s->noise_buffer + length + (index << 8);
  449. for (i = length; i < subframe_size - node1; i++, k++, noise_ptr++)
  450. s->noise2_buffer[k] += scale * noise_ptr[0];
  451. }
  452. static void add_noise(QDMCContext *s, int ch, int current_subframe)
  453. {
  454. int i, j, aindex;
  455. float amplitude;
  456. float *im = &s->fft_buffer[0 + ch][s->fft_offset + s->subframe_size * current_subframe];
  457. float *re = &s->fft_buffer[2 + ch][s->fft_offset + s->subframe_size * current_subframe];
  458. memset(s->noise2_buffer, 0, 4 * s->subframe_size);
  459. for (i = 0; i < noise_bands_size[s->band_index]; i++) {
  460. if (qdmc_nodes[i + 21 * s->band_index] > s->subframe_size - 1)
  461. break;
  462. aindex = s->noise[ch][i][current_subframe / 2];
  463. amplitude = aindex > 0 ? amplitude_tab[aindex & 0x3F] : 0.0f;
  464. lin_calc(s, amplitude, qdmc_nodes[21 * s->band_index + i],
  465. qdmc_nodes[21 * s->band_index + i + 2], i);
  466. }
  467. for (j = 2; j < s->subframe_size - 1; j++) {
  468. float rnd_re, rnd_im;
  469. s->rndval = 214013U * s->rndval + 2531011;
  470. rnd_im = ((s->rndval & 0x7FFF) - 16384.0f) * 0.000030517578f * s->noise2_buffer[j];
  471. s->rndval = 214013U * s->rndval + 2531011;
  472. rnd_re = ((s->rndval & 0x7FFF) - 16384.0f) * 0.000030517578f * s->noise2_buffer[j];
  473. im[j ] += rnd_im;
  474. re[j ] += rnd_re;
  475. im[j+1] -= rnd_im;
  476. re[j+1] -= rnd_re;
  477. }
  478. }
  479. static void add_wave(QDMCContext *s, int offset, int freqs, int group, int stereo_mode, int amp, int phase)
  480. {
  481. int j, group_bits, pos, pindex;
  482. float im, re, amplitude, level, *imptr, *reptr;
  483. if (s->nb_channels == 1)
  484. stereo_mode = 0;
  485. group_bits = 4 - group;
  486. pos = freqs >> (4 - group);
  487. amplitude = amplitude_tab[amp & 0x3F];
  488. imptr = &s->fft_buffer[ stereo_mode][s->fft_offset + s->subframe_size * offset + pos];
  489. reptr = &s->fft_buffer[2 + stereo_mode][s->fft_offset + s->subframe_size * offset + pos];
  490. pindex = (phase << 6) - ((2 * (freqs >> (4 - group)) + 1) << 7);
  491. for (j = 0; j < (1 << (group_bits + 1)) - 1; j++) {
  492. pindex += (2 * freqs + 1) << (7 - group_bits);
  493. level = amplitude * s->alt_sin[group][j];
  494. im = level * sin_table[ pindex & 0x1FF];
  495. re = level * sin_table[(pindex + 128) & 0x1FF];
  496. imptr[0] += im;
  497. imptr[1] -= im;
  498. reptr[0] += re;
  499. reptr[1] -= re;
  500. imptr += s->subframe_size;
  501. reptr += s->subframe_size;
  502. if (imptr >= &s->fft_buffer[stereo_mode][2 * s->frame_size]) {
  503. imptr = &s->fft_buffer[0 + stereo_mode][pos];
  504. reptr = &s->fft_buffer[2 + stereo_mode][pos];
  505. }
  506. }
  507. }
  508. static void add_wave0(QDMCContext *s, int offset, int freqs, int stereo_mode, int amp, int phase)
  509. {
  510. float level, im, re;
  511. int pos;
  512. if (s->nb_channels == 1)
  513. stereo_mode = 0;
  514. level = amplitude_tab[amp & 0x3F];
  515. im = level * sin_table[ (phase << 6) & 0x1FF];
  516. re = level * sin_table[((phase << 6) + 128) & 0x1FF];
  517. pos = s->fft_offset + freqs + s->subframe_size * offset;
  518. s->fft_buffer[ stereo_mode][pos ] += im;
  519. s->fft_buffer[2 + stereo_mode][pos ] += re;
  520. s->fft_buffer[ stereo_mode][pos + 1] -= im;
  521. s->fft_buffer[2 + stereo_mode][pos + 1] -= re;
  522. }
  523. static void add_waves(QDMCContext *s, int current_subframe)
  524. {
  525. int w, g;
  526. for (g = 0; g < 4; g++) {
  527. for (w = s->cur_tone[g]; w < s->nb_tones[g]; w++) {
  528. QDMCTone *t = &s->tones[g][w];
  529. if (current_subframe < t->offset)
  530. break;
  531. add_wave(s, t->offset, t->freq, g, t->mode, t->amplitude, t->phase);
  532. }
  533. s->cur_tone[g] = w;
  534. }
  535. for (w = s->cur_tone[4]; w < s->nb_tones[4]; w++) {
  536. QDMCTone *t = &s->tones[4][w];
  537. if (current_subframe < t->offset)
  538. break;
  539. add_wave0(s, t->offset, t->freq, t->mode, t->amplitude, t->phase);
  540. }
  541. s->cur_tone[4] = w;
  542. }
  543. static int decode_frame(QDMCContext *s, GetBitContext *gb, int16_t *out)
  544. {
  545. int ret, ch, i, n;
  546. if (skip_label(s, gb))
  547. return AVERROR_INVALIDDATA;
  548. s->fft_offset = s->frame_size - s->fft_offset;
  549. s->buffer_ptr = &s->buffer[s->nb_channels * s->buffer_offset];
  550. ret = read_noise_data(s, gb);
  551. if (ret < 0)
  552. return ret;
  553. ret = read_wave_data(s, gb);
  554. if (ret < 0)
  555. return ret;
  556. for (n = 0; n < 32; n++) {
  557. float *r;
  558. for (ch = 0; ch < s->nb_channels; ch++)
  559. add_noise(s, ch, n);
  560. add_waves(s, n);
  561. for (ch = 0; ch < s->nb_channels; ch++) {
  562. for (i = 0; i < s->subframe_size; i++) {
  563. s->cmplx[ch][i].re = s->fft_buffer[ch + 2][s->fft_offset + n * s->subframe_size + i];
  564. s->cmplx[ch][i].im = s->fft_buffer[ch + 0][s->fft_offset + n * s->subframe_size + i];
  565. s->cmplx[ch][s->subframe_size + i].re = 0;
  566. s->cmplx[ch][s->subframe_size + i].im = 0;
  567. }
  568. }
  569. for (ch = 0; ch < s->nb_channels; ch++) {
  570. s->fft_ctx.fft_permute(&s->fft_ctx, s->cmplx[ch]);
  571. s->fft_ctx.fft_calc(&s->fft_ctx, s->cmplx[ch]);
  572. }
  573. r = &s->buffer_ptr[s->nb_channels * n * s->subframe_size];
  574. for (i = 0; i < 2 * s->subframe_size; i++) {
  575. for (ch = 0; ch < s->nb_channels; ch++) {
  576. *r++ += s->cmplx[ch][i].re;
  577. }
  578. }
  579. r = &s->buffer_ptr[n * s->subframe_size * s->nb_channels];
  580. for (i = 0; i < s->nb_channels * s->subframe_size; i++) {
  581. out[i] = av_clipf(r[i], INT16_MIN, INT16_MAX);
  582. }
  583. out += s->subframe_size * s->nb_channels;
  584. for (ch = 0; ch < s->nb_channels; ch++) {
  585. memset(s->fft_buffer[ch+0] + s->fft_offset + n * s->subframe_size, 0, 4 * s->subframe_size);
  586. memset(s->fft_buffer[ch+2] + s->fft_offset + n * s->subframe_size, 0, 4 * s->subframe_size);
  587. }
  588. memset(s->buffer + s->nb_channels * (n * s->subframe_size + s->frame_size + s->buffer_offset), 0, 4 * s->subframe_size * s->nb_channels);
  589. }
  590. s->buffer_offset += s->frame_size;
  591. if (s->buffer_offset >= 32768 - s->frame_size) {
  592. memcpy(s->buffer, &s->buffer[s->nb_channels * s->buffer_offset], 4 * s->frame_size * s->nb_channels);
  593. s->buffer_offset = 0;
  594. }
  595. return 0;
  596. }
  597. static av_cold void qdmc_flush(AVCodecContext *avctx)
  598. {
  599. QDMCContext *s = avctx->priv_data;
  600. memset(s->buffer, 0, sizeof(s->buffer));
  601. memset(s->fft_buffer, 0, sizeof(s->fft_buffer));
  602. s->fft_offset = 0;
  603. s->buffer_offset = 0;
  604. }
  605. static int qdmc_decode_frame(AVCodecContext *avctx, void *data,
  606. int *got_frame_ptr, AVPacket *avpkt)
  607. {
  608. QDMCContext *s = avctx->priv_data;
  609. AVFrame *frame = data;
  610. GetBitContext gb;
  611. int ret;
  612. if (!avpkt->data)
  613. return 0;
  614. if (avpkt->size < s->checksum_size)
  615. return AVERROR_INVALIDDATA;
  616. s->avctx = avctx;
  617. frame->nb_samples = s->frame_size;
  618. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  619. return ret;
  620. if ((ret = init_get_bits8(&gb, avpkt->data, s->checksum_size)) < 0)
  621. return ret;
  622. memset(s->nb_tones, 0, sizeof(s->nb_tones));
  623. memset(s->cur_tone, 0, sizeof(s->cur_tone));
  624. ret = decode_frame(s, &gb, (int16_t *)frame->data[0]);
  625. if (ret >= 0) {
  626. *got_frame_ptr = 1;
  627. return s->checksum_size;
  628. }
  629. qdmc_flush(avctx);
  630. return ret;
  631. }
  632. AVCodec ff_qdmc_decoder = {
  633. .name = "qdmc",
  634. .long_name = NULL_IF_CONFIG_SMALL("QDesign Music Codec 1"),
  635. .type = AVMEDIA_TYPE_AUDIO,
  636. .id = AV_CODEC_ID_QDMC,
  637. .priv_data_size = sizeof(QDMCContext),
  638. .init = qdmc_decode_init,
  639. .init_static_data = qdmc_init_static_data,
  640. .close = qdmc_decode_close,
  641. .decode = qdmc_decode_frame,
  642. .flush = qdmc_flush,
  643. .capabilities = AV_CODEC_CAP_DR1,
  644. };