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.

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