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.

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