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.

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