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.

819 lines
24KB

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