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