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.

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