imc.c 24 KB

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