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.

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