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.

1009 lines
31KB

  1. /*
  2. * IMC compatible decoder
  3. * Copyright (c) 2002-2004 Maxim Poliakovski
  4. * Copyright (c) 2006 Benjamin Larsson
  5. * Copyright (c) 2006 Konstantin Shishkov
  6. *
  7. * This file is part of Libav.
  8. *
  9. * Libav is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * Libav is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with Libav; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * IMC - Intel Music Coder
  26. * A mdct based codec using a 256 points large transform
  27. * divided into 32 bands with some mix of scale factors.
  28. * Only mono is supported.
  29. *
  30. */
  31. #include <math.h>
  32. #include <stddef.h>
  33. #include <stdio.h>
  34. #include "libavutil/channel_layout.h"
  35. #include "libavutil/float_dsp.h"
  36. #include "libavutil/internal.h"
  37. #include "avcodec.h"
  38. #include "get_bits.h"
  39. #include "dsputil.h"
  40. #include "fft.h"
  41. #include "internal.h"
  42. #include "sinewin.h"
  43. #include "imcdata.h"
  44. #define IMC_BLOCK_SIZE 64
  45. #define IMC_FRAME_ID 0x21
  46. #define BANDS 32
  47. #define COEFFS 256
  48. typedef struct IMCChannel {
  49. float old_floor[BANDS];
  50. float flcoeffs1[BANDS];
  51. float flcoeffs2[BANDS];
  52. float flcoeffs3[BANDS];
  53. float flcoeffs4[BANDS];
  54. float flcoeffs5[BANDS];
  55. float flcoeffs6[BANDS];
  56. float CWdecoded[COEFFS];
  57. int bandWidthT[BANDS]; ///< codewords per band
  58. int bitsBandT[BANDS]; ///< how many bits per codeword in band
  59. int CWlengthT[COEFFS]; ///< how many bits in each codeword
  60. int levlCoeffBuf[BANDS];
  61. int bandFlagsBuf[BANDS]; ///< flags for each band
  62. int sumLenArr[BANDS]; ///< bits for all coeffs in band
  63. int skipFlagRaw[BANDS]; ///< skip flags are stored in raw form or not
  64. int skipFlagBits[BANDS]; ///< bits used to code skip flags
  65. int skipFlagCount[BANDS]; ///< skipped coeffients per band
  66. int skipFlags[COEFFS]; ///< skip coefficient decoding or not
  67. int codewords[COEFFS]; ///< raw codewords read from bitstream
  68. float last_fft_im[COEFFS];
  69. int decoder_reset;
  70. } IMCChannel;
  71. typedef struct {
  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. DSPContext dsp;
  84. AVFloatDSPContext fdsp;
  85. FFTContext fft;
  86. DECLARE_ALIGNED(32, FFTComplex, samples)[COEFFS / 2];
  87. float *out_samples;
  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. av_log_ask_for_sample(avctx, "Number of channels is not supported\n");
  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_dsputil_init(&q->dsp, avctx);
  206. avpriv_float_dsp_init(&q->fdsp, avctx->flags & 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] = get_bits(&q->gb, 7);
  276. for (i = start; i < BANDS; i++) {
  277. levlCoeffs[i] = get_vlc2(&q->gb, hufftab[cb_sel[i]]->table,
  278. hufftab[cb_sel[i]]->bits, 2);
  279. if (levlCoeffs[i] == 17)
  280. levlCoeffs[i] += get_bits(&q->gb, 4);
  281. }
  282. }
  283. static void imc_decode_level_coefficients(IMCContext *q, int *levlCoeffBuf,
  284. float *flcoeffs1, float *flcoeffs2)
  285. {
  286. int i, level;
  287. float tmp, tmp2;
  288. // maybe some frequency division thingy
  289. flcoeffs1[0] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
  290. flcoeffs2[0] = log2f(flcoeffs1[0]);
  291. tmp = flcoeffs1[0];
  292. tmp2 = flcoeffs2[0];
  293. for (i = 1; i < BANDS; i++) {
  294. level = levlCoeffBuf[i];
  295. if (level == 16) {
  296. flcoeffs1[i] = 1.0;
  297. flcoeffs2[i] = 0.0;
  298. } else {
  299. if (level < 17)
  300. level -= 7;
  301. else if (level <= 24)
  302. level -= 32;
  303. else
  304. level -= 16;
  305. tmp *= imc_exp_tab[15 + level];
  306. tmp2 += 0.83048 * level; // 0.83048 = log2(10) * 0.25
  307. flcoeffs1[i] = tmp;
  308. flcoeffs2[i] = tmp2;
  309. }
  310. }
  311. }
  312. static void imc_decode_level_coefficients2(IMCContext *q, int *levlCoeffBuf,
  313. float *old_floor, float *flcoeffs1,
  314. float *flcoeffs2)
  315. {
  316. int i;
  317. /* FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors
  318. * and flcoeffs2 old scale factors
  319. * might be incomplete due to a missing table that is in the binary code
  320. */
  321. for (i = 0; i < BANDS; i++) {
  322. flcoeffs1[i] = 0;
  323. if (levlCoeffBuf[i] < 16) {
  324. flcoeffs1[i] = imc_exp_tab2[levlCoeffBuf[i]] * old_floor[i];
  325. flcoeffs2[i] = (levlCoeffBuf[i] - 7) * 0.83048 + flcoeffs2[i]; // 0.83048 = log2(10) * 0.25
  326. } else {
  327. flcoeffs1[i] = old_floor[i];
  328. }
  329. }
  330. }
  331. /**
  332. * Perform bit allocation depending on bits available
  333. */
  334. static int bit_allocation(IMCContext *q, IMCChannel *chctx,
  335. int stream_format_code, int freebits, int flag)
  336. {
  337. int i, j;
  338. const float limit = -1.e20;
  339. float highest = 0.0;
  340. int indx;
  341. int t1 = 0;
  342. int t2 = 1;
  343. float summa = 0.0;
  344. int iacc = 0;
  345. int summer = 0;
  346. int rres, cwlen;
  347. float lowest = 1.e10;
  348. int low_indx = 0;
  349. float workT[32];
  350. int flg;
  351. int found_indx = 0;
  352. for (i = 0; i < BANDS; i++)
  353. highest = FFMAX(highest, chctx->flcoeffs1[i]);
  354. for (i = 0; i < BANDS - 1; i++)
  355. chctx->flcoeffs4[i] = chctx->flcoeffs3[i] - log2f(chctx->flcoeffs5[i]);
  356. chctx->flcoeffs4[BANDS - 1] = limit;
  357. highest = highest * 0.25;
  358. for (i = 0; i < BANDS; i++) {
  359. indx = -1;
  360. if ((band_tab[i + 1] - band_tab[i]) == chctx->bandWidthT[i])
  361. indx = 0;
  362. if ((band_tab[i + 1] - band_tab[i]) > chctx->bandWidthT[i])
  363. indx = 1;
  364. if (((band_tab[i + 1] - band_tab[i]) / 2) >= chctx->bandWidthT[i])
  365. indx = 2;
  366. if (indx == -1)
  367. return AVERROR_INVALIDDATA;
  368. chctx->flcoeffs4[i] += xTab[(indx * 2 + (chctx->flcoeffs1[i] < highest)) * 2 + flag];
  369. }
  370. if (stream_format_code & 0x2) {
  371. chctx->flcoeffs4[0] = limit;
  372. chctx->flcoeffs4[1] = limit;
  373. chctx->flcoeffs4[2] = limit;
  374. chctx->flcoeffs4[3] = limit;
  375. }
  376. for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS - 1; i++) {
  377. iacc += chctx->bandWidthT[i];
  378. summa += chctx->bandWidthT[i] * chctx->flcoeffs4[i];
  379. }
  380. chctx->bandWidthT[BANDS - 1] = 0;
  381. summa = (summa * 0.5 - freebits) / iacc;
  382. for (i = 0; i < BANDS / 2; i++) {
  383. rres = summer - freebits;
  384. if ((rres >= -8) && (rres <= 8))
  385. break;
  386. summer = 0;
  387. iacc = 0;
  388. for (j = (stream_format_code & 0x2) ? 4 : 0; j < BANDS; j++) {
  389. cwlen = av_clipf(((chctx->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6);
  390. chctx->bitsBandT[j] = cwlen;
  391. summer += chctx->bandWidthT[j] * cwlen;
  392. if (cwlen > 0)
  393. iacc += chctx->bandWidthT[j];
  394. }
  395. flg = t2;
  396. t2 = 1;
  397. if (freebits < summer)
  398. t2 = -1;
  399. if (i == 0)
  400. flg = t2;
  401. if (flg != t2)
  402. t1++;
  403. summa = (float)(summer - freebits) / ((t1 + 1) * iacc) + summa;
  404. }
  405. for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS; i++) {
  406. for (j = band_tab[i]; j < band_tab[i + 1]; j++)
  407. chctx->CWlengthT[j] = chctx->bitsBandT[i];
  408. }
  409. if (freebits > summer) {
  410. for (i = 0; i < BANDS; i++) {
  411. workT[i] = (chctx->bitsBandT[i] == 6) ? -1.e20
  412. : (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] - 0.415);
  413. }
  414. highest = 0.0;
  415. do {
  416. if (highest <= -1.e20)
  417. break;
  418. found_indx = 0;
  419. highest = -1.e20;
  420. for (i = 0; i < BANDS; i++) {
  421. if (workT[i] > highest) {
  422. highest = workT[i];
  423. found_indx = i;
  424. }
  425. }
  426. if (highest > -1.e20) {
  427. workT[found_indx] -= 2.0;
  428. if (++chctx->bitsBandT[found_indx] == 6)
  429. workT[found_indx] = -1.e20;
  430. for (j = band_tab[found_indx]; j < band_tab[found_indx + 1] && (freebits > summer); j++) {
  431. chctx->CWlengthT[j]++;
  432. summer++;
  433. }
  434. }
  435. } while (freebits > summer);
  436. }
  437. if (freebits < summer) {
  438. for (i = 0; i < BANDS; i++) {
  439. workT[i] = chctx->bitsBandT[i] ? (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] + 1.585)
  440. : 1.e20;
  441. }
  442. if (stream_format_code & 0x2) {
  443. workT[0] = 1.e20;
  444. workT[1] = 1.e20;
  445. workT[2] = 1.e20;
  446. workT[3] = 1.e20;
  447. }
  448. while (freebits < summer) {
  449. lowest = 1.e10;
  450. low_indx = 0;
  451. for (i = 0; i < BANDS; i++) {
  452. if (workT[i] < lowest) {
  453. lowest = workT[i];
  454. low_indx = i;
  455. }
  456. }
  457. // if (lowest >= 1.e10)
  458. // break;
  459. workT[low_indx] = lowest + 2.0;
  460. if (!--chctx->bitsBandT[low_indx])
  461. workT[low_indx] = 1.e20;
  462. for (j = band_tab[low_indx]; j < band_tab[low_indx+1] && (freebits < summer); j++) {
  463. if (chctx->CWlengthT[j] > 0) {
  464. chctx->CWlengthT[j]--;
  465. summer--;
  466. }
  467. }
  468. }
  469. }
  470. return 0;
  471. }
  472. static void imc_get_skip_coeff(IMCContext *q, IMCChannel *chctx)
  473. {
  474. int i, j;
  475. memset(chctx->skipFlagBits, 0, sizeof(chctx->skipFlagBits));
  476. memset(chctx->skipFlagCount, 0, sizeof(chctx->skipFlagCount));
  477. for (i = 0; i < BANDS; i++) {
  478. if (!chctx->bandFlagsBuf[i] || !chctx->bandWidthT[i])
  479. continue;
  480. if (!chctx->skipFlagRaw[i]) {
  481. chctx->skipFlagBits[i] = band_tab[i + 1] - band_tab[i];
  482. for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
  483. chctx->skipFlags[j] = get_bits1(&q->gb);
  484. if (chctx->skipFlags[j])
  485. chctx->skipFlagCount[i]++;
  486. }
  487. } else {
  488. for (j = band_tab[i]; j < band_tab[i + 1] - 1; j += 2) {
  489. if (!get_bits1(&q->gb)) { // 0
  490. chctx->skipFlagBits[i]++;
  491. chctx->skipFlags[j] = 1;
  492. chctx->skipFlags[j + 1] = 1;
  493. chctx->skipFlagCount[i] += 2;
  494. } else {
  495. if (get_bits1(&q->gb)) { // 11
  496. chctx->skipFlagBits[i] += 2;
  497. chctx->skipFlags[j] = 0;
  498. chctx->skipFlags[j + 1] = 1;
  499. chctx->skipFlagCount[i]++;
  500. } else {
  501. chctx->skipFlagBits[i] += 3;
  502. chctx->skipFlags[j + 1] = 0;
  503. if (!get_bits1(&q->gb)) { // 100
  504. chctx->skipFlags[j] = 1;
  505. chctx->skipFlagCount[i]++;
  506. } else { // 101
  507. chctx->skipFlags[j] = 0;
  508. }
  509. }
  510. }
  511. }
  512. if (j < band_tab[i + 1]) {
  513. chctx->skipFlagBits[i]++;
  514. if ((chctx->skipFlags[j] = get_bits1(&q->gb)))
  515. chctx->skipFlagCount[i]++;
  516. }
  517. }
  518. }
  519. }
  520. /**
  521. * Increase highest' band coefficient sizes as some bits won't be used
  522. */
  523. static void imc_adjust_bit_allocation(IMCContext *q, IMCChannel *chctx,
  524. int summer)
  525. {
  526. float workT[32];
  527. int corrected = 0;
  528. int i, j;
  529. float highest = 0;
  530. int found_indx = 0;
  531. for (i = 0; i < BANDS; i++) {
  532. workT[i] = (chctx->bitsBandT[i] == 6) ? -1.e20
  533. : (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] - 0.415);
  534. }
  535. while (corrected < summer) {
  536. if (highest <= -1.e20)
  537. break;
  538. highest = -1.e20;
  539. for (i = 0; i < BANDS; i++) {
  540. if (workT[i] > highest) {
  541. highest = workT[i];
  542. found_indx = i;
  543. }
  544. }
  545. if (highest > -1.e20) {
  546. workT[found_indx] -= 2.0;
  547. if (++(chctx->bitsBandT[found_indx]) == 6)
  548. workT[found_indx] = -1.e20;
  549. for (j = band_tab[found_indx]; j < band_tab[found_indx+1] && (corrected < summer); j++) {
  550. if (!chctx->skipFlags[j] && (chctx->CWlengthT[j] < 6)) {
  551. chctx->CWlengthT[j]++;
  552. corrected++;
  553. }
  554. }
  555. }
  556. }
  557. }
  558. static void imc_imdct256(IMCContext *q, IMCChannel *chctx, int channels)
  559. {
  560. int i;
  561. float re, im;
  562. float *dst1 = q->out_samples;
  563. float *dst2 = q->out_samples + (COEFFS - 1);
  564. /* prerotation */
  565. for (i = 0; i < COEFFS / 2; i++) {
  566. q->samples[i].re = -(q->pre_coef1[i] * chctx->CWdecoded[COEFFS - 1 - i * 2]) -
  567. (q->pre_coef2[i] * chctx->CWdecoded[i * 2]);
  568. q->samples[i].im = (q->pre_coef2[i] * chctx->CWdecoded[COEFFS - 1 - i * 2]) -
  569. (q->pre_coef1[i] * chctx->CWdecoded[i * 2]);
  570. }
  571. /* FFT */
  572. q->fft.fft_permute(&q->fft, q->samples);
  573. q->fft.fft_calc(&q->fft, q->samples);
  574. /* postrotation, window and reorder */
  575. for (i = 0; i < COEFFS / 2; i++) {
  576. re = ( q->samples[i].re * q->post_cos[i]) + (-q->samples[i].im * q->post_sin[i]);
  577. im = (-q->samples[i].im * q->post_cos[i]) - ( q->samples[i].re * q->post_sin[i]);
  578. *dst1 = (q->mdct_sine_window[COEFFS - 1 - i * 2] * chctx->last_fft_im[i])
  579. + (q->mdct_sine_window[i * 2] * re);
  580. *dst2 = (q->mdct_sine_window[i * 2] * chctx->last_fft_im[i])
  581. - (q->mdct_sine_window[COEFFS - 1 - i * 2] * re);
  582. dst1 += 2;
  583. dst2 -= 2;
  584. chctx->last_fft_im[i] = im;
  585. }
  586. }
  587. static int inverse_quant_coeff(IMCContext *q, IMCChannel *chctx,
  588. int stream_format_code)
  589. {
  590. int i, j;
  591. int middle_value, cw_len, max_size;
  592. const float *quantizer;
  593. for (i = 0; i < BANDS; i++) {
  594. for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
  595. chctx->CWdecoded[j] = 0;
  596. cw_len = chctx->CWlengthT[j];
  597. if (cw_len <= 0 || chctx->skipFlags[j])
  598. continue;
  599. max_size = 1 << cw_len;
  600. middle_value = max_size >> 1;
  601. if (chctx->codewords[j] >= max_size || chctx->codewords[j] < 0)
  602. return AVERROR_INVALIDDATA;
  603. if (cw_len >= 4) {
  604. quantizer = imc_quantizer2[(stream_format_code & 2) >> 1];
  605. if (chctx->codewords[j] >= middle_value)
  606. chctx->CWdecoded[j] = quantizer[chctx->codewords[j] - 8] * chctx->flcoeffs6[i];
  607. else
  608. chctx->CWdecoded[j] = -quantizer[max_size - chctx->codewords[j] - 8 - 1] * chctx->flcoeffs6[i];
  609. }else{
  610. quantizer = imc_quantizer1[((stream_format_code & 2) >> 1) | (chctx->bandFlagsBuf[i] << 1)];
  611. if (chctx->codewords[j] >= middle_value)
  612. chctx->CWdecoded[j] = quantizer[chctx->codewords[j] - 1] * chctx->flcoeffs6[i];
  613. else
  614. chctx->CWdecoded[j] = -quantizer[max_size - 2 - chctx->codewords[j]] * chctx->flcoeffs6[i];
  615. }
  616. }
  617. }
  618. return 0;
  619. }
  620. static int imc_get_coeffs(IMCContext *q, IMCChannel *chctx)
  621. {
  622. int i, j, cw_len, cw;
  623. for (i = 0; i < BANDS; i++) {
  624. if (!chctx->sumLenArr[i])
  625. continue;
  626. if (chctx->bandFlagsBuf[i] || chctx->bandWidthT[i]) {
  627. for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
  628. cw_len = chctx->CWlengthT[j];
  629. cw = 0;
  630. if (get_bits_count(&q->gb) + cw_len > 512) {
  631. av_dlog(NULL, "Band %i coeff %i cw_len %i\n", i, j, cw_len);
  632. return AVERROR_INVALIDDATA;
  633. }
  634. if (cw_len && (!chctx->bandFlagsBuf[i] || !chctx->skipFlags[j]))
  635. cw = get_bits(&q->gb, cw_len);
  636. chctx->codewords[j] = cw;
  637. }
  638. }
  639. }
  640. return 0;
  641. }
  642. static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch)
  643. {
  644. int stream_format_code;
  645. int imc_hdr, i, j, ret;
  646. int flag;
  647. int bits, summer;
  648. int counter, bitscount;
  649. IMCChannel *chctx = q->chctx + ch;
  650. /* Check the frame header */
  651. imc_hdr = get_bits(&q->gb, 9);
  652. if (imc_hdr & 0x18) {
  653. av_log(avctx, AV_LOG_ERROR, "frame header check failed!\n");
  654. av_log(avctx, AV_LOG_ERROR, "got %X.\n", imc_hdr);
  655. return AVERROR_INVALIDDATA;
  656. }
  657. stream_format_code = get_bits(&q->gb, 3);
  658. if (stream_format_code & 1) {
  659. av_log_ask_for_sample(avctx, "Stream format %X is not supported\n",
  660. stream_format_code);
  661. return AVERROR_PATCHWELCOME;
  662. }
  663. if (stream_format_code & 0x04)
  664. chctx->decoder_reset = 1;
  665. if (chctx->decoder_reset) {
  666. for (i = 0; i < BANDS; i++)
  667. chctx->old_floor[i] = 1.0;
  668. for (i = 0; i < COEFFS; i++)
  669. chctx->CWdecoded[i] = 0;
  670. chctx->decoder_reset = 0;
  671. }
  672. flag = get_bits1(&q->gb);
  673. imc_read_level_coeffs(q, stream_format_code, chctx->levlCoeffBuf);
  674. if (stream_format_code & 0x4)
  675. imc_decode_level_coefficients(q, chctx->levlCoeffBuf,
  676. chctx->flcoeffs1, chctx->flcoeffs2);
  677. else
  678. imc_decode_level_coefficients2(q, chctx->levlCoeffBuf, chctx->old_floor,
  679. chctx->flcoeffs1, chctx->flcoeffs2);
  680. memcpy(chctx->old_floor, chctx->flcoeffs1, 32 * sizeof(float));
  681. counter = 0;
  682. for (i = 0; i < BANDS; i++) {
  683. if (chctx->levlCoeffBuf[i] == 16) {
  684. chctx->bandWidthT[i] = 0;
  685. counter++;
  686. } else
  687. chctx->bandWidthT[i] = band_tab[i + 1] - band_tab[i];
  688. }
  689. memset(chctx->bandFlagsBuf, 0, BANDS * sizeof(int));
  690. for (i = 0; i < BANDS - 1; i++) {
  691. if (chctx->bandWidthT[i])
  692. chctx->bandFlagsBuf[i] = get_bits1(&q->gb);
  693. }
  694. imc_calculate_coeffs(q, chctx->flcoeffs1, chctx->flcoeffs2, chctx->bandWidthT, chctx->flcoeffs3, chctx->flcoeffs5);
  695. bitscount = 0;
  696. /* first 4 bands will be assigned 5 bits per coefficient */
  697. if (stream_format_code & 0x2) {
  698. bitscount += 15;
  699. chctx->bitsBandT[0] = 5;
  700. chctx->CWlengthT[0] = 5;
  701. chctx->CWlengthT[1] = 5;
  702. chctx->CWlengthT[2] = 5;
  703. for (i = 1; i < 4; i++) {
  704. bits = (chctx->levlCoeffBuf[i] == 16) ? 0 : 5;
  705. chctx->bitsBandT[i] = bits;
  706. for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
  707. chctx->CWlengthT[j] = bits;
  708. bitscount += bits;
  709. }
  710. }
  711. }
  712. if (avctx->codec_id == AV_CODEC_ID_IAC) {
  713. bitscount += !!chctx->bandWidthT[BANDS - 1];
  714. if (!(stream_format_code & 0x2))
  715. bitscount += 16;
  716. }
  717. if ((ret = bit_allocation(q, chctx, stream_format_code,
  718. 512 - bitscount - get_bits_count(&q->gb),
  719. flag)) < 0) {
  720. av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
  721. chctx->decoder_reset = 1;
  722. return ret;
  723. }
  724. for (i = 0; i < BANDS; i++) {
  725. chctx->sumLenArr[i] = 0;
  726. chctx->skipFlagRaw[i] = 0;
  727. for (j = band_tab[i]; j < band_tab[i + 1]; j++)
  728. chctx->sumLenArr[i] += chctx->CWlengthT[j];
  729. if (chctx->bandFlagsBuf[i])
  730. if ((((band_tab[i + 1] - band_tab[i]) * 1.5) > chctx->sumLenArr[i]) && (chctx->sumLenArr[i] > 0))
  731. chctx->skipFlagRaw[i] = 1;
  732. }
  733. imc_get_skip_coeff(q, chctx);
  734. for (i = 0; i < BANDS; i++) {
  735. chctx->flcoeffs6[i] = chctx->flcoeffs1[i];
  736. /* band has flag set and at least one coded coefficient */
  737. if (chctx->bandFlagsBuf[i] && (band_tab[i + 1] - band_tab[i]) != chctx->skipFlagCount[i]) {
  738. chctx->flcoeffs6[i] *= q->sqrt_tab[ band_tab[i + 1] - band_tab[i]] /
  739. q->sqrt_tab[(band_tab[i + 1] - band_tab[i] - chctx->skipFlagCount[i])];
  740. }
  741. }
  742. /* calculate bits left, bits needed and adjust bit allocation */
  743. bits = summer = 0;
  744. for (i = 0; i < BANDS; i++) {
  745. if (chctx->bandFlagsBuf[i]) {
  746. for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
  747. if (chctx->skipFlags[j]) {
  748. summer += chctx->CWlengthT[j];
  749. chctx->CWlengthT[j] = 0;
  750. }
  751. }
  752. bits += chctx->skipFlagBits[i];
  753. summer -= chctx->skipFlagBits[i];
  754. }
  755. }
  756. imc_adjust_bit_allocation(q, chctx, summer);
  757. for (i = 0; i < BANDS; i++) {
  758. chctx->sumLenArr[i] = 0;
  759. for (j = band_tab[i]; j < band_tab[i + 1]; j++)
  760. if (!chctx->skipFlags[j])
  761. chctx->sumLenArr[i] += chctx->CWlengthT[j];
  762. }
  763. memset(chctx->codewords, 0, sizeof(chctx->codewords));
  764. if (imc_get_coeffs(q, chctx) < 0) {
  765. av_log(avctx, AV_LOG_ERROR, "Read coefficients failed\n");
  766. chctx->decoder_reset = 1;
  767. return AVERROR_INVALIDDATA;
  768. }
  769. if (inverse_quant_coeff(q, chctx, stream_format_code) < 0) {
  770. av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n");
  771. chctx->decoder_reset = 1;
  772. return AVERROR_INVALIDDATA;
  773. }
  774. memset(chctx->skipFlags, 0, sizeof(chctx->skipFlags));
  775. imc_imdct256(q, chctx, avctx->channels);
  776. return 0;
  777. }
  778. static int imc_decode_frame(AVCodecContext *avctx, void *data,
  779. int *got_frame_ptr, AVPacket *avpkt)
  780. {
  781. AVFrame *frame = data;
  782. const uint8_t *buf = avpkt->data;
  783. int buf_size = avpkt->size;
  784. int ret, i;
  785. IMCContext *q = avctx->priv_data;
  786. LOCAL_ALIGNED_16(uint16_t, buf16, [IMC_BLOCK_SIZE / 2]);
  787. if (buf_size < IMC_BLOCK_SIZE * avctx->channels) {
  788. av_log(avctx, AV_LOG_ERROR, "frame too small!\n");
  789. return AVERROR_INVALIDDATA;
  790. }
  791. /* get output buffer */
  792. frame->nb_samples = COEFFS;
  793. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  794. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  795. return ret;
  796. }
  797. for (i = 0; i < avctx->channels; i++) {
  798. q->out_samples = (float *)frame->extended_data[i];
  799. q->dsp.bswap16_buf(buf16, (const uint16_t*)buf, IMC_BLOCK_SIZE / 2);
  800. init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8);
  801. buf += IMC_BLOCK_SIZE;
  802. if ((ret = imc_decode_block(avctx, q, i)) < 0)
  803. return ret;
  804. }
  805. if (avctx->channels == 2) {
  806. q->fdsp.butterflies_float((float *)frame->extended_data[0],
  807. (float *)frame->extended_data[1], COEFFS);
  808. }
  809. *got_frame_ptr = 1;
  810. return IMC_BLOCK_SIZE * avctx->channels;
  811. }
  812. static av_cold int imc_decode_close(AVCodecContext * avctx)
  813. {
  814. IMCContext *q = avctx->priv_data;
  815. ff_fft_end(&q->fft);
  816. return 0;
  817. }
  818. AVCodec ff_imc_decoder = {
  819. .name = "imc",
  820. .type = AVMEDIA_TYPE_AUDIO,
  821. .id = AV_CODEC_ID_IMC,
  822. .priv_data_size = sizeof(IMCContext),
  823. .init = imc_decode_init,
  824. .close = imc_decode_close,
  825. .decode = imc_decode_frame,
  826. .capabilities = CODEC_CAP_DR1,
  827. .long_name = NULL_IF_CONFIG_SMALL("IMC (Intel Music Coder)"),
  828. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
  829. AV_SAMPLE_FMT_NONE },
  830. };
  831. AVCodec ff_iac_decoder = {
  832. .name = "iac",
  833. .type = AVMEDIA_TYPE_AUDIO,
  834. .id = AV_CODEC_ID_IAC,
  835. .priv_data_size = sizeof(IMCContext),
  836. .init = imc_decode_init,
  837. .close = imc_decode_close,
  838. .decode = imc_decode_frame,
  839. .capabilities = CODEC_CAP_DR1,
  840. .long_name = NULL_IF_CONFIG_SMALL("IAC (Indeo Audio Coder)"),
  841. .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
  842. AV_SAMPLE_FMT_NONE },
  843. };