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.

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