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.

859 lines
26KB

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