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.

882 lines
26KB

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