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.

860 lines
25KB

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