imc.c 25 KB

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