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.

1098 lines
33KB

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