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