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.

1108 lines
34KB

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