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.

1075 lines
33KB

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