You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1075 lines
33KB

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