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.

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