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.

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