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.

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