imc.c 25 KB

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