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.

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