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.

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