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.

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