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.

1018 lines
31KB

  1. /*
  2. * IMC compatible decoder
  3. * Copyright (c) 2002-2004 Maxim Poliakovski
  4. * Copyright (c) 2006 Benjamin Larsson
  5. * Copyright (c) 2006 Konstantin Shishkov
  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. /**
  24. * @file
  25. * IMC - Intel Music Coder
  26. * A mdct based codec using a 256 points large transform
  27. * divided into 32 bands with some mix of scale factors.
  28. * Only mono is supported.
  29. *
  30. */
  31. #include <math.h>
  32. #include <stddef.h>
  33. #include <stdio.h>
  34. #include "avcodec.h"
  35. #include "get_bits.h"
  36. #include "dsputil.h"
  37. #include "fft.h"
  38. #include "libavutil/audioconvert.h"
  39. #include "sinewin.h"
  40. #include "imcdata.h"
  41. #define IMC_BLOCK_SIZE 64
  42. #define IMC_FRAME_ID 0x21
  43. #define BANDS 32
  44. #define COEFFS 256
  45. typedef struct IMCChannel {
  46. float old_floor[BANDS];
  47. float flcoeffs1[BANDS];
  48. float flcoeffs2[BANDS];
  49. float flcoeffs3[BANDS];
  50. float flcoeffs4[BANDS];
  51. float flcoeffs5[BANDS];
  52. float flcoeffs6[BANDS];
  53. float CWdecoded[COEFFS];
  54. int bandWidthT[BANDS]; ///< codewords per band
  55. int bitsBandT[BANDS]; ///< how many bits per codeword in band
  56. int CWlengthT[COEFFS]; ///< how many bits in each codeword
  57. int levlCoeffBuf[BANDS];
  58. int bandFlagsBuf[BANDS]; ///< flags for each band
  59. int sumLenArr[BANDS]; ///< bits for all coeffs in band
  60. int skipFlagRaw[BANDS]; ///< skip flags are stored in raw form or not
  61. int skipFlagBits[BANDS]; ///< bits used to code skip flags
  62. int skipFlagCount[BANDS]; ///< skipped coeffients per band
  63. int skipFlags[COEFFS]; ///< skip coefficient decoding or not
  64. int codewords[COEFFS]; ///< raw codewords read from bitstream
  65. float last_fft_im[COEFFS];
  66. int decoder_reset;
  67. } IMCChannel;
  68. typedef struct {
  69. AVFrame frame;
  70. IMCChannel chctx[2];
  71. /** MDCT tables */
  72. //@{
  73. float mdct_sine_window[COEFFS];
  74. float post_cos[COEFFS];
  75. float post_sin[COEFFS];
  76. float pre_coef1[COEFFS];
  77. float pre_coef2[COEFFS];
  78. //@}
  79. float sqrt_tab[30];
  80. GetBitContext gb;
  81. float one_div_log2;
  82. DSPContext dsp;
  83. FFTContext fft;
  84. DECLARE_ALIGNED(32, FFTComplex, samples)[COEFFS / 2];
  85. float *out_samples;
  86. int8_t cyclTab[32], cyclTab2[32];
  87. float weights1[31], weights2[31];
  88. } IMCContext;
  89. static VLC huffman_vlc[4][4];
  90. #define VLC_TABLES_SIZE 9512
  91. static const int vlc_offsets[17] = {
  92. 0, 640, 1156, 1732, 2308, 2852, 3396, 3924,
  93. 4452, 5220, 5860, 6628, 7268, 7908, 8424, 8936, VLC_TABLES_SIZE
  94. };
  95. static VLC_TYPE vlc_tables[VLC_TABLES_SIZE][2];
  96. static inline double freq2bark(double freq)
  97. {
  98. return 3.5 * atan((freq / 7500.0) * (freq / 7500.0)) + 13.0 * atan(freq * 0.00076);
  99. }
  100. static av_cold void iac_generate_tabs(IMCContext *q, int sampling_rate)
  101. {
  102. double freqmin[32], freqmid[32], freqmax[32];
  103. double scale = sampling_rate / (256.0 * 2.0 * 2.0);
  104. double nyquist_freq = sampling_rate * 0.5;
  105. double freq, bark, prev_bark = 0, tf, tb;
  106. int i, j;
  107. for (i = 0; i < 32; i++) {
  108. freq = (band_tab[i] + band_tab[i + 1] - 1) * scale;
  109. bark = freq2bark(freq);
  110. if (i > 0) {
  111. tb = bark - prev_bark;
  112. q->weights1[i - 1] = pow(10.0, -1.0 * tb);
  113. q->weights2[i - 1] = pow(10.0, -2.7 * tb);
  114. }
  115. prev_bark = bark;
  116. freqmid[i] = freq;
  117. tf = freq;
  118. while (tf < nyquist_freq) {
  119. tf += 0.5;
  120. tb = freq2bark(tf);
  121. if (tb > bark + 0.5)
  122. break;
  123. }
  124. freqmax[i] = tf;
  125. tf = freq;
  126. while (tf > 0.0) {
  127. tf -= 0.5;
  128. tb = freq2bark(tf);
  129. if (tb <= bark - 0.5)
  130. break;
  131. }
  132. freqmin[i] = tf;
  133. }
  134. for (i = 0; i < 32; i++) {
  135. freq = freqmax[i];
  136. for (j = 31; j > 0 && freq <= freqmid[j]; j--);
  137. q->cyclTab[i] = j + 1;
  138. freq = freqmin[i];
  139. for (j = 0; j < 32 && freq >= freqmid[j]; j++);
  140. q->cyclTab2[i] = j - 1;
  141. }
  142. }
  143. static av_cold int imc_decode_init(AVCodecContext *avctx)
  144. {
  145. int i, j, ret;
  146. IMCContext *q = avctx->priv_data;
  147. double r1, r2;
  148. if ((avctx->codec_id == CODEC_ID_IMC && avctx->channels != 1)
  149. || (avctx->codec_id == CODEC_ID_IAC && avctx->channels > 2)) {
  150. av_log_ask_for_sample(avctx, "Number of channels is not supported\n");
  151. return AVERROR_PATCHWELCOME;
  152. }
  153. for (j = 0; j < avctx->channels; j++) {
  154. q->chctx[j].decoder_reset = 1;
  155. for (i = 0; i < BANDS; i++)
  156. q->chctx[j].old_floor[i] = 1.0;
  157. for (i = 0; i < COEFFS / 2; i++)
  158. q->chctx[j].last_fft_im[i] = 0;
  159. }
  160. /* Build mdct window, a simple sine window normalized with sqrt(2) */
  161. ff_sine_window_init(q->mdct_sine_window, COEFFS);
  162. for (i = 0; i < COEFFS; i++)
  163. q->mdct_sine_window[i] *= sqrt(2.0);
  164. for (i = 0; i < COEFFS / 2; i++) {
  165. q->post_cos[i] = (1.0f / 32768) * cos(i / 256.0 * M_PI);
  166. q->post_sin[i] = (1.0f / 32768) * sin(i / 256.0 * M_PI);
  167. r1 = sin((i * 4.0 + 1.0) / 1024.0 * M_PI);
  168. r2 = cos((i * 4.0 + 1.0) / 1024.0 * M_PI);
  169. if (i & 0x1) {
  170. q->pre_coef1[i] = (r1 + r2) * sqrt(2.0);
  171. q->pre_coef2[i] = -(r1 - r2) * sqrt(2.0);
  172. } else {
  173. q->pre_coef1[i] = -(r1 + r2) * sqrt(2.0);
  174. q->pre_coef2[i] = (r1 - r2) * sqrt(2.0);
  175. }
  176. }
  177. /* Generate a square root table */
  178. for (i = 0; i < 30; i++)
  179. q->sqrt_tab[i] = sqrt(i);
  180. /* initialize the VLC tables */
  181. for (i = 0; i < 4 ; i++) {
  182. for (j = 0; j < 4; j++) {
  183. huffman_vlc[i][j].table = &vlc_tables[vlc_offsets[i * 4 + j]];
  184. huffman_vlc[i][j].table_allocated = vlc_offsets[i * 4 + j + 1] - vlc_offsets[i * 4 + j];
  185. init_vlc(&huffman_vlc[i][j], 9, imc_huffman_sizes[i],
  186. imc_huffman_lens[i][j], 1, 1,
  187. imc_huffman_bits[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
  188. }
  189. }
  190. q->one_div_log2 = 1 / log(2);
  191. if (avctx->codec_id == CODEC_ID_IAC) {
  192. }
  193. if (avctx->codec_id == CODEC_ID_IAC) {
  194. iac_generate_tabs(q, avctx->sample_rate);
  195. } else {
  196. memcpy(q->cyclTab, cyclTab, sizeof(cyclTab));
  197. memcpy(q->cyclTab2, cyclTab2, sizeof(cyclTab2));
  198. memcpy(q->weights1, imc_weights1, sizeof(imc_weights1));
  199. memcpy(q->weights2, imc_weights2, sizeof(imc_weights2));
  200. }
  201. if ((ret = ff_fft_init(&q->fft, 7, 1))) {
  202. av_log(avctx, AV_LOG_INFO, "FFT init failed\n");
  203. return ret;
  204. }
  205. ff_dsputil_init(&q->dsp, avctx);
  206. avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
  207. avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO
  208. : AV_CH_LAYOUT_STEREO;
  209. avcodec_get_frame_defaults(&q->frame);
  210. avctx->coded_frame = &q->frame;
  211. return 0;
  212. }
  213. static void imc_calculate_coeffs(IMCContext *q, float *flcoeffs1,
  214. float *flcoeffs2, int *bandWidthT,
  215. float *flcoeffs3, float *flcoeffs5)
  216. {
  217. float workT1[BANDS];
  218. float workT2[BANDS];
  219. float workT3[BANDS];
  220. float snr_limit = 1.e-30;
  221. float accum = 0.0;
  222. int i, cnt2;
  223. for (i = 0; i < BANDS; i++) {
  224. flcoeffs5[i] = workT2[i] = 0.0;
  225. if (bandWidthT[i]) {
  226. workT1[i] = flcoeffs1[i] * flcoeffs1[i];
  227. flcoeffs3[i] = 2.0 * flcoeffs2[i];
  228. } else {
  229. workT1[i] = 0.0;
  230. flcoeffs3[i] = -30000.0;
  231. }
  232. workT3[i] = bandWidthT[i] * workT1[i] * 0.01;
  233. if (workT3[i] <= snr_limit)
  234. workT3[i] = 0.0;
  235. }
  236. for (i = 0; i < BANDS; i++) {
  237. for (cnt2 = i; cnt2 < q->cyclTab[i]; cnt2++)
  238. flcoeffs5[cnt2] = flcoeffs5[cnt2] + workT3[i];
  239. workT2[cnt2 - 1] = workT2[cnt2 - 1] + workT3[i];
  240. }
  241. for (i = 1; i < BANDS; i++) {
  242. accum = (workT2[i - 1] + accum) * q->weights1[i - 1];
  243. flcoeffs5[i] += accum;
  244. }
  245. for (i = 0; i < BANDS; i++)
  246. workT2[i] = 0.0;
  247. for (i = 0; i < BANDS; i++) {
  248. for (cnt2 = i - 1; cnt2 > q->cyclTab2[i]; cnt2--)
  249. flcoeffs5[cnt2] += workT3[i];
  250. workT2[cnt2+1] += workT3[i];
  251. }
  252. accum = 0.0;
  253. for (i = BANDS-2; i >= 0; i--) {
  254. accum = (workT2[i+1] + accum) * q->weights2[i];
  255. flcoeffs5[i] += accum;
  256. // there is missing code here, but it seems to never be triggered
  257. }
  258. }
  259. static void imc_read_level_coeffs(IMCContext *q, int stream_format_code,
  260. int *levlCoeffs)
  261. {
  262. int i;
  263. VLC *hufftab[4];
  264. int start = 0;
  265. const uint8_t *cb_sel;
  266. int s;
  267. s = stream_format_code >> 1;
  268. hufftab[0] = &huffman_vlc[s][0];
  269. hufftab[1] = &huffman_vlc[s][1];
  270. hufftab[2] = &huffman_vlc[s][2];
  271. hufftab[3] = &huffman_vlc[s][3];
  272. cb_sel = imc_cb_select[s];
  273. if (stream_format_code & 4)
  274. start = 1;
  275. if (start)
  276. levlCoeffs[0] = get_bits(&q->gb, 7);
  277. for (i = start; i < BANDS; i++) {
  278. levlCoeffs[i] = get_vlc2(&q->gb, hufftab[cb_sel[i]]->table,
  279. hufftab[cb_sel[i]]->bits, 2);
  280. if (levlCoeffs[i] == 17)
  281. levlCoeffs[i] += get_bits(&q->gb, 4);
  282. }
  283. }
  284. static void imc_decode_level_coefficients(IMCContext *q, int *levlCoeffBuf,
  285. float *flcoeffs1, float *flcoeffs2)
  286. {
  287. int i, level;
  288. float tmp, tmp2;
  289. // maybe some frequency division thingy
  290. flcoeffs1[0] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
  291. flcoeffs2[0] = log(flcoeffs1[0]) / log(2);
  292. tmp = flcoeffs1[0];
  293. tmp2 = flcoeffs2[0];
  294. for (i = 1; i < BANDS; i++) {
  295. level = levlCoeffBuf[i];
  296. if (level == 16) {
  297. flcoeffs1[i] = 1.0;
  298. flcoeffs2[i] = 0.0;
  299. } else {
  300. if (level < 17)
  301. level -= 7;
  302. else if (level <= 24)
  303. level -= 32;
  304. else
  305. level -= 16;
  306. tmp *= imc_exp_tab[15 + level];
  307. tmp2 += 0.83048 * level; // 0.83048 = log2(10) * 0.25
  308. flcoeffs1[i] = tmp;
  309. flcoeffs2[i] = tmp2;
  310. }
  311. }
  312. }
  313. static void imc_decode_level_coefficients2(IMCContext *q, int *levlCoeffBuf,
  314. float *old_floor, float *flcoeffs1,
  315. float *flcoeffs2)
  316. {
  317. int i;
  318. /* FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors
  319. * and flcoeffs2 old scale factors
  320. * might be incomplete due to a missing table that is in the binary code
  321. */
  322. for (i = 0; i < BANDS; i++) {
  323. flcoeffs1[i] = 0;
  324. if (levlCoeffBuf[i] < 16) {
  325. flcoeffs1[i] = imc_exp_tab2[levlCoeffBuf[i]] * old_floor[i];
  326. flcoeffs2[i] = (levlCoeffBuf[i] - 7) * 0.83048 + flcoeffs2[i]; // 0.83048 = log2(10) * 0.25
  327. } else {
  328. flcoeffs1[i] = old_floor[i];
  329. }
  330. }
  331. }
  332. /**
  333. * Perform bit allocation depending on bits available
  334. */
  335. static int bit_allocation(IMCContext *q, IMCChannel *chctx,
  336. int stream_format_code, int freebits, int flag)
  337. {
  338. int i, j;
  339. const float limit = -1.e20;
  340. float highest = 0.0;
  341. int indx;
  342. int t1 = 0;
  343. int t2 = 1;
  344. float summa = 0.0;
  345. int iacc = 0;
  346. int summer = 0;
  347. int rres, cwlen;
  348. float lowest = 1.e10;
  349. int low_indx = 0;
  350. float workT[32];
  351. int flg;
  352. int found_indx = 0;
  353. for (i = 0; i < BANDS; i++)
  354. highest = FFMAX(highest, chctx->flcoeffs1[i]);
  355. for (i = 0; i < BANDS - 1; i++)
  356. chctx->flcoeffs4[i] = chctx->flcoeffs3[i] - log(chctx->flcoeffs5[i]) / log(2);
  357. chctx->flcoeffs4[BANDS - 1] = limit;
  358. highest = highest * 0.25;
  359. for (i = 0; i < BANDS; i++) {
  360. indx = -1;
  361. if ((band_tab[i + 1] - band_tab[i]) == chctx->bandWidthT[i])
  362. indx = 0;
  363. if ((band_tab[i + 1] - band_tab[i]) > chctx->bandWidthT[i])
  364. indx = 1;
  365. if (((band_tab[i + 1] - band_tab[i]) / 2) >= chctx->bandWidthT[i])
  366. indx = 2;
  367. if (indx == -1)
  368. return AVERROR_INVALIDDATA;
  369. chctx->flcoeffs4[i] += xTab[(indx * 2 + (chctx->flcoeffs1[i] < highest)) * 2 + flag];
  370. }
  371. if (stream_format_code & 0x2) {
  372. chctx->flcoeffs4[0] = limit;
  373. chctx->flcoeffs4[1] = limit;
  374. chctx->flcoeffs4[2] = limit;
  375. chctx->flcoeffs4[3] = limit;
  376. }
  377. for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS - 1; i++) {
  378. iacc += chctx->bandWidthT[i];
  379. summa += chctx->bandWidthT[i] * chctx->flcoeffs4[i];
  380. }
  381. chctx->bandWidthT[BANDS - 1] = 0;
  382. summa = (summa * 0.5 - freebits) / iacc;
  383. for (i = 0; i < BANDS / 2; i++) {
  384. rres = summer - freebits;
  385. if ((rres >= -8) && (rres <= 8))
  386. break;
  387. summer = 0;
  388. iacc = 0;
  389. for (j = (stream_format_code & 0x2) ? 4 : 0; j < BANDS; j++) {
  390. cwlen = av_clipf(((chctx->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6);
  391. chctx->bitsBandT[j] = cwlen;
  392. summer += chctx->bandWidthT[j] * cwlen;
  393. if (cwlen > 0)
  394. iacc += chctx->bandWidthT[j];
  395. }
  396. flg = t2;
  397. t2 = 1;
  398. if (freebits < summer)
  399. t2 = -1;
  400. if (i == 0)
  401. flg = t2;
  402. if (flg != t2)
  403. t1++;
  404. summa = (float)(summer - freebits) / ((t1 + 1) * iacc) + summa;
  405. }
  406. for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS; i++) {
  407. for (j = band_tab[i]; j < band_tab[i + 1]; j++)
  408. chctx->CWlengthT[j] = chctx->bitsBandT[i];
  409. }
  410. if (freebits > summer) {
  411. for (i = 0; i < BANDS; i++) {
  412. workT[i] = (chctx->bitsBandT[i] == 6) ? -1.e20
  413. : (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] - 0.415);
  414. }
  415. highest = 0.0;
  416. do {
  417. if (highest <= -1.e20)
  418. break;
  419. found_indx = 0;
  420. highest = -1.e20;
  421. for (i = 0; i < BANDS; i++) {
  422. if (workT[i] > highest) {
  423. highest = workT[i];
  424. found_indx = i;
  425. }
  426. }
  427. if (highest > -1.e20) {
  428. workT[found_indx] -= 2.0;
  429. if (++chctx->bitsBandT[found_indx] == 6)
  430. workT[found_indx] = -1.e20;
  431. for (j = band_tab[found_indx]; j < band_tab[found_indx + 1] && (freebits > summer); j++) {
  432. chctx->CWlengthT[j]++;
  433. summer++;
  434. }
  435. }
  436. } while (freebits > summer);
  437. }
  438. if (freebits < summer) {
  439. for (i = 0; i < BANDS; i++) {
  440. workT[i] = chctx->bitsBandT[i] ? (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] + 1.585)
  441. : 1.e20;
  442. }
  443. if (stream_format_code & 0x2) {
  444. workT[0] = 1.e20;
  445. workT[1] = 1.e20;
  446. workT[2] = 1.e20;
  447. workT[3] = 1.e20;
  448. }
  449. while (freebits < summer) {
  450. lowest = 1.e10;
  451. low_indx = 0;
  452. for (i = 0; i < BANDS; i++) {
  453. if (workT[i] < lowest) {
  454. lowest = workT[i];
  455. low_indx = i;
  456. }
  457. }
  458. // if (lowest >= 1.e10)
  459. // break;
  460. workT[low_indx] = lowest + 2.0;
  461. if (!--chctx->bitsBandT[low_indx])
  462. workT[low_indx] = 1.e20;
  463. for (j = band_tab[low_indx]; j < band_tab[low_indx+1] && (freebits < summer); j++) {
  464. if (chctx->CWlengthT[j] > 0) {
  465. chctx->CWlengthT[j]--;
  466. summer--;
  467. }
  468. }
  469. }
  470. }
  471. return 0;
  472. }
  473. static void imc_get_skip_coeff(IMCContext *q, IMCChannel *chctx)
  474. {
  475. int i, j;
  476. memset(chctx->skipFlagBits, 0, sizeof(chctx->skipFlagBits));
  477. memset(chctx->skipFlagCount, 0, sizeof(chctx->skipFlagCount));
  478. for (i = 0; i < BANDS; i++) {
  479. if (!chctx->bandFlagsBuf[i] || !chctx->bandWidthT[i])
  480. continue;
  481. if (!chctx->skipFlagRaw[i]) {
  482. chctx->skipFlagBits[i] = band_tab[i + 1] - band_tab[i];
  483. for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
  484. chctx->skipFlags[j] = get_bits1(&q->gb);
  485. if (chctx->skipFlags[j])
  486. chctx->skipFlagCount[i]++;
  487. }
  488. } else {
  489. for (j = band_tab[i]; j < band_tab[i + 1] - 1; j += 2) {
  490. if (!get_bits1(&q->gb)) { // 0
  491. chctx->skipFlagBits[i]++;
  492. chctx->skipFlags[j] = 1;
  493. chctx->skipFlags[j + 1] = 1;
  494. chctx->skipFlagCount[i] += 2;
  495. } else {
  496. if (get_bits1(&q->gb)) { // 11
  497. chctx->skipFlagBits[i] += 2;
  498. chctx->skipFlags[j] = 0;
  499. chctx->skipFlags[j + 1] = 1;
  500. chctx->skipFlagCount[i]++;
  501. } else {
  502. chctx->skipFlagBits[i] += 3;
  503. chctx->skipFlags[j + 1] = 0;
  504. if (!get_bits1(&q->gb)) { // 100
  505. chctx->skipFlags[j] = 1;
  506. chctx->skipFlagCount[i]++;
  507. } else { // 101
  508. chctx->skipFlags[j] = 0;
  509. }
  510. }
  511. }
  512. }
  513. if (j < band_tab[i + 1]) {
  514. chctx->skipFlagBits[i]++;
  515. if ((chctx->skipFlags[j] = get_bits1(&q->gb)))
  516. chctx->skipFlagCount[i]++;
  517. }
  518. }
  519. }
  520. }
  521. /**
  522. * Increase highest' band coefficient sizes as some bits won't be used
  523. */
  524. static void imc_adjust_bit_allocation(IMCContext *q, IMCChannel *chctx,
  525. int summer)
  526. {
  527. float workT[32];
  528. int corrected = 0;
  529. int i, j;
  530. float highest = 0;
  531. int found_indx = 0;
  532. for (i = 0; i < BANDS; i++) {
  533. workT[i] = (chctx->bitsBandT[i] == 6) ? -1.e20
  534. : (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] - 0.415);
  535. }
  536. while (corrected < summer) {
  537. if (highest <= -1.e20)
  538. break;
  539. highest = -1.e20;
  540. for (i = 0; i < BANDS; i++) {
  541. if (workT[i] > highest) {
  542. highest = workT[i];
  543. found_indx = i;
  544. }
  545. }
  546. if (highest > -1.e20) {
  547. workT[found_indx] -= 2.0;
  548. if (++(chctx->bitsBandT[found_indx]) == 6)
  549. workT[found_indx] = -1.e20;
  550. for (j = band_tab[found_indx]; j < band_tab[found_indx+1] && (corrected < summer); j++) {
  551. if (!chctx->skipFlags[j] && (chctx->CWlengthT[j] < 6)) {
  552. chctx->CWlengthT[j]++;
  553. corrected++;
  554. }
  555. }
  556. }
  557. }
  558. }
  559. static void imc_imdct256(IMCContext *q, IMCChannel *chctx, int channels)
  560. {
  561. int i;
  562. float re, im;
  563. float *dst1 = q->out_samples;
  564. float *dst2 = q->out_samples + (COEFFS - 1) * channels;
  565. /* prerotation */
  566. for (i = 0; i < COEFFS / 2; i++) {
  567. q->samples[i].re = -(q->pre_coef1[i] * chctx->CWdecoded[COEFFS - 1 - i * 2]) -
  568. (q->pre_coef2[i] * chctx->CWdecoded[i * 2]);
  569. q->samples[i].im = (q->pre_coef2[i] * chctx->CWdecoded[COEFFS - 1 - i * 2]) -
  570. (q->pre_coef1[i] * chctx->CWdecoded[i * 2]);
  571. }
  572. /* FFT */
  573. q->fft.fft_permute(&q->fft, q->samples);
  574. q->fft.fft_calc(&q->fft, q->samples);
  575. /* postrotation, window and reorder */
  576. for (i = 0; i < COEFFS / 2; i++) {
  577. re = ( q->samples[i].re * q->post_cos[i]) + (-q->samples[i].im * q->post_sin[i]);
  578. im = (-q->samples[i].im * q->post_cos[i]) - ( q->samples[i].re * q->post_sin[i]);
  579. *dst1 = (q->mdct_sine_window[COEFFS - 1 - i * 2] * chctx->last_fft_im[i])
  580. + (q->mdct_sine_window[i * 2] * re);
  581. *dst2 = (q->mdct_sine_window[i * 2] * chctx->last_fft_im[i])
  582. - (q->mdct_sine_window[COEFFS - 1 - i * 2] * re);
  583. dst1 += channels * 2;
  584. dst2 -= channels * 2;
  585. chctx->last_fft_im[i] = im;
  586. }
  587. }
  588. static int inverse_quant_coeff(IMCContext *q, IMCChannel *chctx,
  589. int stream_format_code)
  590. {
  591. int i, j;
  592. int middle_value, cw_len, max_size;
  593. const float *quantizer;
  594. for (i = 0; i < BANDS; i++) {
  595. for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
  596. chctx->CWdecoded[j] = 0;
  597. cw_len = chctx->CWlengthT[j];
  598. if (cw_len <= 0 || chctx->skipFlags[j])
  599. continue;
  600. max_size = 1 << cw_len;
  601. middle_value = max_size >> 1;
  602. if (chctx->codewords[j] >= max_size || chctx->codewords[j] < 0)
  603. return AVERROR_INVALIDDATA;
  604. if (cw_len >= 4) {
  605. quantizer = imc_quantizer2[(stream_format_code & 2) >> 1];
  606. if (chctx->codewords[j] >= middle_value)
  607. chctx->CWdecoded[j] = quantizer[chctx->codewords[j] - 8] * chctx->flcoeffs6[i];
  608. else
  609. chctx->CWdecoded[j] = -quantizer[max_size - chctx->codewords[j] - 8 - 1] * chctx->flcoeffs6[i];
  610. }else{
  611. quantizer = imc_quantizer1[((stream_format_code & 2) >> 1) | (chctx->bandFlagsBuf[i] << 1)];
  612. if (chctx->codewords[j] >= middle_value)
  613. chctx->CWdecoded[j] = quantizer[chctx->codewords[j] - 1] * chctx->flcoeffs6[i];
  614. else
  615. chctx->CWdecoded[j] = -quantizer[max_size - 2 - chctx->codewords[j]] * chctx->flcoeffs6[i];
  616. }
  617. }
  618. }
  619. return 0;
  620. }
  621. static int imc_get_coeffs(IMCContext *q, IMCChannel *chctx)
  622. {
  623. int i, j, cw_len, cw;
  624. for (i = 0; i < BANDS; i++) {
  625. if (!chctx->sumLenArr[i])
  626. continue;
  627. if (chctx->bandFlagsBuf[i] || chctx->bandWidthT[i]) {
  628. for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
  629. cw_len = chctx->CWlengthT[j];
  630. cw = 0;
  631. if (get_bits_count(&q->gb) + cw_len > 512) {
  632. // av_log(NULL, 0, "Band %i coeff %i cw_len %i\n", i, j, cw_len);
  633. return AVERROR_INVALIDDATA;
  634. }
  635. if (cw_len && (!chctx->bandFlagsBuf[i] || !chctx->skipFlags[j]))
  636. cw = get_bits(&q->gb, cw_len);
  637. chctx->codewords[j] = cw;
  638. }
  639. }
  640. }
  641. return 0;
  642. }
  643. static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch)
  644. {
  645. int stream_format_code;
  646. int imc_hdr, i, j, ret;
  647. int flag;
  648. int bits, summer;
  649. int counter, bitscount;
  650. IMCChannel *chctx = q->chctx + ch;
  651. /* Check the frame header */
  652. imc_hdr = get_bits(&q->gb, 9);
  653. if (imc_hdr & 0x18) {
  654. av_log(avctx, AV_LOG_ERROR, "frame header check failed!\n");
  655. av_log(avctx, AV_LOG_ERROR, "got %X.\n", imc_hdr);
  656. return AVERROR_INVALIDDATA;
  657. }
  658. stream_format_code = get_bits(&q->gb, 3);
  659. if (stream_format_code & 1) {
  660. av_log_ask_for_sample(avctx, "Stream format %X is not supported\n",
  661. stream_format_code);
  662. return AVERROR_PATCHWELCOME;
  663. }
  664. // av_log(avctx, AV_LOG_DEBUG, "stream_format_code = %d\n", stream_format_code);
  665. if (stream_format_code & 0x04)
  666. chctx->decoder_reset = 1;
  667. if (chctx->decoder_reset) {
  668. memset(q->out_samples, 0, sizeof(q->out_samples));
  669. for (i = 0; i < BANDS; i++)
  670. chctx->old_floor[i] = 1.0;
  671. for (i = 0; i < COEFFS; i++)
  672. chctx->CWdecoded[i] = 0;
  673. chctx->decoder_reset = 0;
  674. }
  675. flag = get_bits1(&q->gb);
  676. imc_read_level_coeffs(q, stream_format_code, chctx->levlCoeffBuf);
  677. if (stream_format_code & 0x4)
  678. imc_decode_level_coefficients(q, chctx->levlCoeffBuf,
  679. chctx->flcoeffs1, chctx->flcoeffs2);
  680. else
  681. imc_decode_level_coefficients2(q, chctx->levlCoeffBuf, chctx->old_floor,
  682. chctx->flcoeffs1, chctx->flcoeffs2);
  683. memcpy(chctx->old_floor, chctx->flcoeffs1, 32 * sizeof(float));
  684. counter = 0;
  685. for (i = 0; i < BANDS; i++) {
  686. if (chctx->levlCoeffBuf[i] == 16) {
  687. chctx->bandWidthT[i] = 0;
  688. counter++;
  689. } else
  690. chctx->bandWidthT[i] = band_tab[i + 1] - band_tab[i];
  691. }
  692. memset(chctx->bandFlagsBuf, 0, BANDS * sizeof(int));
  693. for (i = 0; i < BANDS - 1; i++) {
  694. if (chctx->bandWidthT[i])
  695. chctx->bandFlagsBuf[i] = get_bits1(&q->gb);
  696. }
  697. imc_calculate_coeffs(q, chctx->flcoeffs1, chctx->flcoeffs2, chctx->bandWidthT, chctx->flcoeffs3, chctx->flcoeffs5);
  698. bitscount = 0;
  699. /* first 4 bands will be assigned 5 bits per coefficient */
  700. if (stream_format_code & 0x2) {
  701. bitscount += 15;
  702. chctx->bitsBandT[0] = 5;
  703. chctx->CWlengthT[0] = 5;
  704. chctx->CWlengthT[1] = 5;
  705. chctx->CWlengthT[2] = 5;
  706. for (i = 1; i < 4; i++) {
  707. bits = (chctx->levlCoeffBuf[i] == 16) ? 0 : 5;
  708. chctx->bitsBandT[i] = bits;
  709. for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
  710. chctx->CWlengthT[j] = bits;
  711. bitscount += bits;
  712. }
  713. }
  714. }
  715. if (avctx->codec_id == CODEC_ID_IAC) {
  716. bitscount += !!chctx->bandWidthT[BANDS - 1];
  717. if (!(stream_format_code & 0x2))
  718. bitscount += 16;
  719. }
  720. if ((ret = bit_allocation(q, chctx, stream_format_code,
  721. 512 - bitscount - get_bits_count(&q->gb),
  722. flag)) < 0) {
  723. av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
  724. chctx->decoder_reset = 1;
  725. return ret;
  726. }
  727. for (i = 0; i < BANDS; i++) {
  728. chctx->sumLenArr[i] = 0;
  729. chctx->skipFlagRaw[i] = 0;
  730. for (j = band_tab[i]; j < band_tab[i + 1]; j++)
  731. chctx->sumLenArr[i] += chctx->CWlengthT[j];
  732. if (chctx->bandFlagsBuf[i])
  733. if ((((band_tab[i + 1] - band_tab[i]) * 1.5) > chctx->sumLenArr[i]) && (chctx->sumLenArr[i] > 0))
  734. chctx->skipFlagRaw[i] = 1;
  735. }
  736. imc_get_skip_coeff(q, chctx);
  737. for (i = 0; i < BANDS; i++) {
  738. chctx->flcoeffs6[i] = chctx->flcoeffs1[i];
  739. /* band has flag set and at least one coded coefficient */
  740. if (chctx->bandFlagsBuf[i] && (band_tab[i + 1] - band_tab[i]) != chctx->skipFlagCount[i]) {
  741. chctx->flcoeffs6[i] *= q->sqrt_tab[ band_tab[i + 1] - band_tab[i]] /
  742. q->sqrt_tab[(band_tab[i + 1] - band_tab[i] - chctx->skipFlagCount[i])];
  743. }
  744. }
  745. /* calculate bits left, bits needed and adjust bit allocation */
  746. bits = summer = 0;
  747. for (i = 0; i < BANDS; i++) {
  748. if (chctx->bandFlagsBuf[i]) {
  749. for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
  750. if (chctx->skipFlags[j]) {
  751. summer += chctx->CWlengthT[j];
  752. chctx->CWlengthT[j] = 0;
  753. }
  754. }
  755. bits += chctx->skipFlagBits[i];
  756. summer -= chctx->skipFlagBits[i];
  757. }
  758. }
  759. imc_adjust_bit_allocation(q, chctx, summer);
  760. for (i = 0; i < BANDS; i++) {
  761. chctx->sumLenArr[i] = 0;
  762. for (j = band_tab[i]; j < band_tab[i + 1]; j++)
  763. if (!chctx->skipFlags[j])
  764. chctx->sumLenArr[i] += chctx->CWlengthT[j];
  765. }
  766. memset(chctx->codewords, 0, sizeof(chctx->codewords));
  767. if (imc_get_coeffs(q, chctx) < 0) {
  768. av_log(avctx, AV_LOG_ERROR, "Read coefficients failed\n");
  769. chctx->decoder_reset = 1;
  770. return AVERROR_INVALIDDATA;
  771. }
  772. if (inverse_quant_coeff(q, chctx, stream_format_code) < 0) {
  773. av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n");
  774. chctx->decoder_reset = 1;
  775. return AVERROR_INVALIDDATA;
  776. }
  777. memset(chctx->skipFlags, 0, sizeof(chctx->skipFlags));
  778. imc_imdct256(q, chctx, avctx->channels);
  779. return 0;
  780. }
  781. static int imc_decode_frame(AVCodecContext *avctx, void *data,
  782. int *got_frame_ptr, AVPacket *avpkt)
  783. {
  784. const uint8_t *buf = avpkt->data;
  785. int buf_size = avpkt->size;
  786. int ret, i;
  787. IMCContext *q = avctx->priv_data;
  788. LOCAL_ALIGNED_16(uint16_t, buf16, [IMC_BLOCK_SIZE / 2]);
  789. if (buf_size < IMC_BLOCK_SIZE * avctx->channels) {
  790. av_log(avctx, AV_LOG_ERROR, "frame too small!\n");
  791. return AVERROR_INVALIDDATA;
  792. }
  793. /* get output buffer */
  794. q->frame.nb_samples = COEFFS;
  795. if ((ret = avctx->get_buffer(avctx, &q->frame)) < 0) {
  796. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  797. return ret;
  798. }
  799. for (i = 0; i < avctx->channels; i++) {
  800. q->out_samples = (float*)q->frame.data[0] + i;
  801. q->dsp.bswap16_buf(buf16, (const uint16_t*)buf, IMC_BLOCK_SIZE / 2);
  802. init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8);
  803. buf += IMC_BLOCK_SIZE;
  804. if ((ret = imc_decode_block(avctx, q, i)) < 0)
  805. return ret;
  806. }
  807. if (avctx->channels == 2) {
  808. float *src = (float*)q->frame.data[0], t1, t2;
  809. for (i = 0; i < COEFFS; i++) {
  810. t1 = src[0];
  811. t2 = src[1];
  812. src[0] = t1 + t2;
  813. src[1] = t1 - t2;
  814. src += 2;
  815. }
  816. }
  817. *got_frame_ptr = 1;
  818. *(AVFrame *)data = q->frame;
  819. return IMC_BLOCK_SIZE * avctx->channels;
  820. }
  821. static av_cold int imc_decode_close(AVCodecContext * avctx)
  822. {
  823. IMCContext *q = avctx->priv_data;
  824. ff_fft_end(&q->fft);
  825. return 0;
  826. }
  827. AVCodec ff_imc_decoder = {
  828. .name = "imc",
  829. .type = AVMEDIA_TYPE_AUDIO,
  830. .id = CODEC_ID_IMC,
  831. .priv_data_size = sizeof(IMCContext),
  832. .init = imc_decode_init,
  833. .close = imc_decode_close,
  834. .decode = imc_decode_frame,
  835. .capabilities = CODEC_CAP_DR1,
  836. .long_name = NULL_IF_CONFIG_SMALL("IMC (Intel Music Coder)"),
  837. };
  838. AVCodec ff_iac_decoder = {
  839. .name = "iac",
  840. .type = AVMEDIA_TYPE_AUDIO,
  841. .id = CODEC_ID_IAC,
  842. .priv_data_size = sizeof(IMCContext),
  843. .init = imc_decode_init,
  844. .close = imc_decode_close,
  845. .decode = imc_decode_frame,
  846. .capabilities = CODEC_CAP_DR1,
  847. .long_name = NULL_IF_CONFIG_SMALL("IAC (Indeo Audio Coder)"),
  848. };