imc.c 31 KB

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