2 * IMC compatible decoder
3 * Copyright (c) 2002-2004 Maxim Poliakovski
4 * Copyright (c) 2006 Benjamin Larsson
5 * Copyright (c) 2006 Konstantin Shishkov
7 * This file is part of FFmpeg.
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.
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.
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
26 * IMC - Intel Music Coder
27 * A mdct based codec using a 256 points large transform
28 * divided into 32 bands with some mix of scale factors.
29 * Only mono is supported.
37 #include "libavutil/channel_layout.h"
38 #include "libavutil/ffmath.h"
39 #include "libavutil/float_dsp.h"
40 #include "libavutil/internal.h"
41 #include "libavutil/mem_internal.h"
42 #include "libavutil/thread.h"
53 #define IMC_BLOCK_SIZE 64
54 #define IMC_FRAME_ID 0x21
58 typedef struct IMCChannel {
59 float old_floor[BANDS];
60 float flcoeffs1[BANDS];
61 float flcoeffs2[BANDS];
62 float flcoeffs3[BANDS];
63 float flcoeffs4[BANDS];
64 float flcoeffs5[BANDS];
65 float flcoeffs6[BANDS];
66 float CWdecoded[COEFFS];
68 int bandWidthT[BANDS]; ///< codewords per band
69 int bitsBandT[BANDS]; ///< how many bits per codeword in band
70 int CWlengthT[COEFFS]; ///< how many bits in each codeword
71 int levlCoeffBuf[BANDS];
72 int bandFlagsBuf[BANDS]; ///< flags for each band
73 int sumLenArr[BANDS]; ///< bits for all coeffs in band
74 int skipFlagRaw[BANDS]; ///< skip flags are stored in raw form or not
75 int skipFlagBits[BANDS]; ///< bits used to code skip flags
76 int skipFlagCount[BANDS]; ///< skipped coefficients per band
77 int skipFlags[COEFFS]; ///< skip coefficient decoding or not
78 int codewords[COEFFS]; ///< raw codewords read from bitstream
80 float last_fft_im[COEFFS];
85 typedef struct IMCContext {
90 float mdct_sine_window[COEFFS];
91 float post_cos[COEFFS];
92 float post_sin[COEFFS];
93 float pre_coef1[COEFFS];
94 float pre_coef2[COEFFS];
100 BswapDSPContext bdsp;
101 void (*butterflies_float)(float *av_restrict v1, float *av_restrict v2, int len);
103 DECLARE_ALIGNED(32, FFTComplex, samples)[COEFFS / 2];
108 int8_t cyclTab[32], cyclTab2[32];
109 float weights1[31], weights2[31];
111 AVCodecContext *avctx;
114 static VLC huffman_vlc[4][4];
116 #define IMC_VLC_BITS 9
117 #define VLC_TABLES_SIZE 9512
119 static VLC_TYPE vlc_tables[VLC_TABLES_SIZE][2];
121 static inline double freq2bark(double freq)
123 return 3.5 * atan((freq / 7500.0) * (freq / 7500.0)) + 13.0 * atan(freq * 0.00076);
126 static av_cold void iac_generate_tabs(IMCContext *q, int sampling_rate)
128 double freqmin[32], freqmid[32], freqmax[32];
129 double scale = sampling_rate / (256.0 * 2.0 * 2.0);
130 double nyquist_freq = sampling_rate * 0.5;
131 double freq, bark, prev_bark = 0, tf, tb;
134 for (i = 0; i < 32; i++) {
135 freq = (band_tab[i] + band_tab[i + 1] - 1) * scale;
136 bark = freq2bark(freq);
139 tb = bark - prev_bark;
140 q->weights1[i - 1] = ff_exp10(-1.0 * tb);
141 q->weights2[i - 1] = ff_exp10(-2.7 * tb);
148 while (tf < nyquist_freq) {
160 if (tb <= bark - 0.5)
166 for (i = 0; i < 32; i++) {
168 for (j = 31; j > 0 && freq <= freqmid[j]; j--);
169 q->cyclTab[i] = j + 1;
172 for (j = 0; j < 32 && freq >= freqmid[j]; j++);
173 q->cyclTab2[i] = j - 1;
177 static av_cold void imc_init_static(void)
179 /* initialize the VLC tables */
180 for (int i = 0, offset = 0; i < 4 ; i++) {
181 for (int j = 0; j < 4; j++) {
182 huffman_vlc[i][j].table = &vlc_tables[offset];
183 huffman_vlc[i][j].table_allocated = VLC_TABLES_SIZE - offset;;
184 ff_init_vlc_from_lengths(&huffman_vlc[i][j], IMC_VLC_BITS, imc_huffman_sizes[i],
185 imc_huffman_lens[i][j], 1,
186 imc_huffman_syms[i][j], 1, 1,
187 0, INIT_VLC_STATIC_OVERLONG, NULL);
188 offset += huffman_vlc[i][j].table_size;
193 static av_cold int imc_decode_init(AVCodecContext *avctx)
196 IMCContext *q = avctx->priv_data;
197 static AVOnce init_static_once = AV_ONCE_INIT;
198 AVFloatDSPContext *fdsp;
201 if (avctx->codec_id == AV_CODEC_ID_IAC && avctx->sample_rate > 96000) {
202 av_log(avctx, AV_LOG_ERROR,
203 "Strange sample rate of %i, file likely corrupt or "
204 "needing a new table derivation method.\n",
206 return AVERROR_PATCHWELCOME;
209 if (avctx->codec_id == AV_CODEC_ID_IMC)
212 if (avctx->channels > 2) {
213 avpriv_request_sample(avctx, "Number of channels > 2");
214 return AVERROR_PATCHWELCOME;
217 for (j = 0; j < avctx->channels; j++) {
218 q->chctx[j].decoder_reset = 1;
220 for (i = 0; i < BANDS; i++)
221 q->chctx[j].old_floor[i] = 1.0;
223 for (i = 0; i < COEFFS / 2; i++)
224 q->chctx[j].last_fft_im[i] = 0;
227 /* Build mdct window, a simple sine window normalized with sqrt(2) */
228 ff_sine_window_init(q->mdct_sine_window, COEFFS);
229 for (i = 0; i < COEFFS; i++)
230 q->mdct_sine_window[i] *= sqrt(2.0);
231 for (i = 0; i < COEFFS / 2; i++) {
232 q->post_cos[i] = (1.0f / 32768) * cos(i / 256.0 * M_PI);
233 q->post_sin[i] = (1.0f / 32768) * sin(i / 256.0 * M_PI);
235 r1 = sin((i * 4.0 + 1.0) / 1024.0 * M_PI);
236 r2 = cos((i * 4.0 + 1.0) / 1024.0 * M_PI);
239 q->pre_coef1[i] = (r1 + r2) * sqrt(2.0);
240 q->pre_coef2[i] = -(r1 - r2) * sqrt(2.0);
242 q->pre_coef1[i] = -(r1 + r2) * sqrt(2.0);
243 q->pre_coef2[i] = (r1 - r2) * sqrt(2.0);
247 /* Generate a square root table */
249 for (i = 0; i < 30; i++)
250 q->sqrt_tab[i] = sqrt(i);
252 if (avctx->codec_id == AV_CODEC_ID_IAC) {
253 iac_generate_tabs(q, avctx->sample_rate);
255 memcpy(q->cyclTab, cyclTab, sizeof(cyclTab));
256 memcpy(q->cyclTab2, cyclTab2, sizeof(cyclTab2));
257 memcpy(q->weights1, imc_weights1, sizeof(imc_weights1));
258 memcpy(q->weights2, imc_weights2, sizeof(imc_weights2));
261 fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
263 return AVERROR(ENOMEM);
264 q->butterflies_float = fdsp->butterflies_float;
266 if ((ret = ff_fft_init(&q->fft, 7, 1))) {
267 av_log(avctx, AV_LOG_INFO, "FFT init failed\n");
270 ff_bswapdsp_init(&q->bdsp);
272 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
273 avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO
274 : AV_CH_LAYOUT_STEREO;
276 ff_thread_once(&init_static_once, imc_init_static);
281 static void imc_calculate_coeffs(IMCContext *q, float *flcoeffs1,
282 float *flcoeffs2, int *bandWidthT,
283 float *flcoeffs3, float *flcoeffs5)
288 float snr_limit = 1.e-30;
292 for (i = 0; i < BANDS; i++) {
293 flcoeffs5[i] = workT2[i] = 0.0;
295 workT1[i] = flcoeffs1[i] * flcoeffs1[i];
296 flcoeffs3[i] = 2.0 * flcoeffs2[i];
299 flcoeffs3[i] = -30000.0;
301 workT3[i] = bandWidthT[i] * workT1[i] * 0.01;
302 if (workT3[i] <= snr_limit)
306 for (i = 0; i < BANDS; i++) {
307 for (cnt2 = i; cnt2 < q->cyclTab[i]; cnt2++)
308 flcoeffs5[cnt2] = flcoeffs5[cnt2] + workT3[i];
309 workT2[cnt2 - 1] = workT2[cnt2 - 1] + workT3[i];
312 for (i = 1; i < BANDS; i++) {
313 accum = (workT2[i - 1] + accum) * q->weights1[i - 1];
314 flcoeffs5[i] += accum;
317 for (i = 0; i < BANDS; i++)
320 for (i = 0; i < BANDS; i++) {
321 for (cnt2 = i - 1; cnt2 > q->cyclTab2[i]; cnt2--)
322 flcoeffs5[cnt2] += workT3[i];
323 workT2[cnt2+1] += workT3[i];
328 for (i = BANDS-2; i >= 0; i--) {
329 accum = (workT2[i+1] + accum) * q->weights2[i];
330 flcoeffs5[i] += accum;
331 // there is missing code here, but it seems to never be triggered
336 static void imc_read_level_coeffs(IMCContext *q, int stream_format_code,
342 const uint8_t *cb_sel;
345 s = stream_format_code >> 1;
346 hufftab[0] = &huffman_vlc[s][0];
347 hufftab[1] = &huffman_vlc[s][1];
348 hufftab[2] = &huffman_vlc[s][2];
349 hufftab[3] = &huffman_vlc[s][3];
350 cb_sel = imc_cb_select[s];
352 if (stream_format_code & 4)
355 levlCoeffs[0] = get_bits(&q->gb, 7);
356 for (i = start; i < BANDS; i++) {
357 levlCoeffs[i] = get_vlc2(&q->gb, hufftab[cb_sel[i]]->table,
359 if (levlCoeffs[i] == 17)
360 levlCoeffs[i] += get_bits(&q->gb, 4);
364 static void imc_read_level_coeffs_raw(IMCContext *q, int stream_format_code,
369 q->coef0_pos = get_bits(&q->gb, 5);
370 levlCoeffs[0] = get_bits(&q->gb, 7);
371 for (i = 1; i < BANDS; i++)
372 levlCoeffs[i] = get_bits(&q->gb, 4);
375 static void imc_decode_level_coefficients(IMCContext *q, int *levlCoeffBuf,
376 float *flcoeffs1, float *flcoeffs2)
380 // maybe some frequency division thingy
382 flcoeffs1[0] = 20000.0 / exp2 (levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
383 flcoeffs2[0] = log2f(flcoeffs1[0]);
387 for (i = 1; i < BANDS; i++) {
388 level = levlCoeffBuf[i];
395 else if (level <= 24)
400 tmp *= imc_exp_tab[15 + level];
401 tmp2 += 0.83048 * level; // 0.83048 = log2(10) * 0.25
409 static void imc_decode_level_coefficients2(IMCContext *q, int *levlCoeffBuf,
410 float *old_floor, float *flcoeffs1,
414 /* FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors
415 * and flcoeffs2 old scale factors
416 * might be incomplete due to a missing table that is in the binary code
418 for (i = 0; i < BANDS; i++) {
420 if (levlCoeffBuf[i] < 16) {
421 flcoeffs1[i] = imc_exp_tab2[levlCoeffBuf[i]] * old_floor[i];
422 flcoeffs2[i] = (levlCoeffBuf[i] - 7) * 0.83048 + flcoeffs2[i]; // 0.83048 = log2(10) * 0.25
424 flcoeffs1[i] = old_floor[i];
429 static void imc_decode_level_coefficients_raw(IMCContext *q, int *levlCoeffBuf,
430 float *flcoeffs1, float *flcoeffs2)
436 flcoeffs1[pos] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
437 flcoeffs2[pos] = log2f(flcoeffs1[pos]);
438 tmp = flcoeffs1[pos];
439 tmp2 = flcoeffs2[pos];
442 for (i = 0; i < BANDS; i++) {
445 level = *levlCoeffBuf++;
446 flcoeffs1[i] = tmp * powf(10.0, -level * 0.4375); //todo tab
447 flcoeffs2[i] = tmp2 - 1.4533435415 * level; // 1.4533435415 = log2(10) * 0.4375
452 * Perform bit allocation depending on bits available
454 static int bit_allocation(IMCContext *q, IMCChannel *chctx,
455 int stream_format_code, int freebits, int flag)
458 const float limit = -1.e20;
467 float lowest = 1.e10;
473 for (i = 0; i < BANDS; i++)
474 highest = FFMAX(highest, chctx->flcoeffs1[i]);
476 for (i = 0; i < BANDS - 1; i++) {
477 if (chctx->flcoeffs5[i] <= 0) {
478 av_log(q->avctx, AV_LOG_ERROR, "flcoeffs5 %f invalid\n", chctx->flcoeffs5[i]);
479 return AVERROR_INVALIDDATA;
481 chctx->flcoeffs4[i] = chctx->flcoeffs3[i] - log2f(chctx->flcoeffs5[i]);
483 chctx->flcoeffs4[BANDS - 1] = limit;
485 highest = highest * 0.25;
487 for (i = 0; i < BANDS; i++) {
489 if ((band_tab[i + 1] - band_tab[i]) == chctx->bandWidthT[i])
492 if ((band_tab[i + 1] - band_tab[i]) > chctx->bandWidthT[i])
495 if (((band_tab[i + 1] - band_tab[i]) / 2) >= chctx->bandWidthT[i])
499 return AVERROR_INVALIDDATA;
501 chctx->flcoeffs4[i] += xTab[(indx * 2 + (chctx->flcoeffs1[i] < highest)) * 2 + flag];
504 if (stream_format_code & 0x2) {
505 chctx->flcoeffs4[0] = limit;
506 chctx->flcoeffs4[1] = limit;
507 chctx->flcoeffs4[2] = limit;
508 chctx->flcoeffs4[3] = limit;
511 for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS - 1; i++) {
512 iacc += chctx->bandWidthT[i];
513 summa += chctx->bandWidthT[i] * chctx->flcoeffs4[i];
517 return AVERROR_INVALIDDATA;
519 chctx->bandWidthT[BANDS - 1] = 0;
520 summa = (summa * 0.5 - freebits) / iacc;
523 for (i = 0; i < BANDS / 2; i++) {
524 rres = summer - freebits;
525 if ((rres >= -8) && (rres <= 8))
531 for (j = (stream_format_code & 0x2) ? 4 : 0; j < BANDS; j++) {
532 cwlen = av_clipf(((chctx->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6);
534 chctx->bitsBandT[j] = cwlen;
535 summer += chctx->bandWidthT[j] * cwlen;
538 iacc += chctx->bandWidthT[j];
543 if (freebits < summer)
550 summa = (float)(summer - freebits) / ((t1 + 1) * iacc) + summa;
553 for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS; i++) {
554 for (j = band_tab[i]; j < band_tab[i + 1]; j++)
555 chctx->CWlengthT[j] = chctx->bitsBandT[i];
558 if (freebits > summer) {
559 for (i = 0; i < BANDS; i++) {
560 workT[i] = (chctx->bitsBandT[i] == 6) ? -1.e20
561 : (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] - 0.415);
567 if (highest <= -1.e20)
573 for (i = 0; i < BANDS; i++) {
574 if (workT[i] > highest) {
580 if (highest > -1.e20) {
581 workT[found_indx] -= 2.0;
582 if (++chctx->bitsBandT[found_indx] == 6)
583 workT[found_indx] = -1.e20;
585 for (j = band_tab[found_indx]; j < band_tab[found_indx + 1] && (freebits > summer); j++) {
586 chctx->CWlengthT[j]++;
590 } while (freebits > summer);
592 if (freebits < summer) {
593 for (i = 0; i < BANDS; i++) {
594 workT[i] = chctx->bitsBandT[i] ? (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] + 1.585)
597 if (stream_format_code & 0x2) {
603 while (freebits < summer) {
606 for (i = 0; i < BANDS; i++) {
607 if (workT[i] < lowest) {
612 // if (lowest >= 1.e10)
614 workT[low_indx] = lowest + 2.0;
616 if (!--chctx->bitsBandT[low_indx])
617 workT[low_indx] = 1.e20;
619 for (j = band_tab[low_indx]; j < band_tab[low_indx+1] && (freebits < summer); j++) {
620 if (chctx->CWlengthT[j] > 0) {
621 chctx->CWlengthT[j]--;
630 static void imc_get_skip_coeff(IMCContext *q, IMCChannel *chctx)
634 memset(chctx->skipFlagBits, 0, sizeof(chctx->skipFlagBits));
635 memset(chctx->skipFlagCount, 0, sizeof(chctx->skipFlagCount));
636 for (i = 0; i < BANDS; i++) {
637 if (!chctx->bandFlagsBuf[i] || !chctx->bandWidthT[i])
640 if (!chctx->skipFlagRaw[i]) {
641 chctx->skipFlagBits[i] = band_tab[i + 1] - band_tab[i];
643 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
644 chctx->skipFlags[j] = get_bits1(&q->gb);
645 if (chctx->skipFlags[j])
646 chctx->skipFlagCount[i]++;
649 for (j = band_tab[i]; j < band_tab[i + 1] - 1; j += 2) {
650 if (!get_bits1(&q->gb)) { // 0
651 chctx->skipFlagBits[i]++;
652 chctx->skipFlags[j] = 1;
653 chctx->skipFlags[j + 1] = 1;
654 chctx->skipFlagCount[i] += 2;
656 if (get_bits1(&q->gb)) { // 11
657 chctx->skipFlagBits[i] += 2;
658 chctx->skipFlags[j] = 0;
659 chctx->skipFlags[j + 1] = 1;
660 chctx->skipFlagCount[i]++;
662 chctx->skipFlagBits[i] += 3;
663 chctx->skipFlags[j + 1] = 0;
664 if (!get_bits1(&q->gb)) { // 100
665 chctx->skipFlags[j] = 1;
666 chctx->skipFlagCount[i]++;
668 chctx->skipFlags[j] = 0;
674 if (j < band_tab[i + 1]) {
675 chctx->skipFlagBits[i]++;
676 if ((chctx->skipFlags[j] = get_bits1(&q->gb)))
677 chctx->skipFlagCount[i]++;
684 * Increase highest' band coefficient sizes as some bits won't be used
686 static void imc_adjust_bit_allocation(IMCContext *q, IMCChannel *chctx,
695 for (i = 0; i < BANDS; i++) {
696 workT[i] = (chctx->bitsBandT[i] == 6) ? -1.e20
697 : (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] - 0.415);
700 while (corrected < summer) {
701 if (highest <= -1.e20)
706 for (i = 0; i < BANDS; i++) {
707 if (workT[i] > highest) {
713 if (highest > -1.e20) {
714 workT[found_indx] -= 2.0;
715 if (++(chctx->bitsBandT[found_indx]) == 6)
716 workT[found_indx] = -1.e20;
718 for (j = band_tab[found_indx]; j < band_tab[found_indx+1] && (corrected < summer); j++) {
719 if (!chctx->skipFlags[j] && (chctx->CWlengthT[j] < 6)) {
720 chctx->CWlengthT[j]++;
728 static void imc_imdct256(IMCContext *q, IMCChannel *chctx, int channels)
732 float *dst1 = q->out_samples;
733 float *dst2 = q->out_samples + (COEFFS - 1);
736 for (i = 0; i < COEFFS / 2; i++) {
737 q->samples[i].re = -(q->pre_coef1[i] * chctx->CWdecoded[COEFFS - 1 - i * 2]) -
738 (q->pre_coef2[i] * chctx->CWdecoded[i * 2]);
739 q->samples[i].im = (q->pre_coef2[i] * chctx->CWdecoded[COEFFS - 1 - i * 2]) -
740 (q->pre_coef1[i] * chctx->CWdecoded[i * 2]);
744 q->fft.fft_permute(&q->fft, q->samples);
745 q->fft.fft_calc(&q->fft, q->samples);
747 /* postrotation, window and reorder */
748 for (i = 0; i < COEFFS / 2; i++) {
749 re = ( q->samples[i].re * q->post_cos[i]) + (-q->samples[i].im * q->post_sin[i]);
750 im = (-q->samples[i].im * q->post_cos[i]) - ( q->samples[i].re * q->post_sin[i]);
751 *dst1 = (q->mdct_sine_window[COEFFS - 1 - i * 2] * chctx->last_fft_im[i])
752 + (q->mdct_sine_window[i * 2] * re);
753 *dst2 = (q->mdct_sine_window[i * 2] * chctx->last_fft_im[i])
754 - (q->mdct_sine_window[COEFFS - 1 - i * 2] * re);
757 chctx->last_fft_im[i] = im;
761 static int inverse_quant_coeff(IMCContext *q, IMCChannel *chctx,
762 int stream_format_code)
765 int middle_value, cw_len, max_size;
766 const float *quantizer;
768 for (i = 0; i < BANDS; i++) {
769 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
770 chctx->CWdecoded[j] = 0;
771 cw_len = chctx->CWlengthT[j];
773 if (cw_len <= 0 || chctx->skipFlags[j])
776 max_size = 1 << cw_len;
777 middle_value = max_size >> 1;
779 if (chctx->codewords[j] >= max_size || chctx->codewords[j] < 0)
780 return AVERROR_INVALIDDATA;
783 quantizer = imc_quantizer2[(stream_format_code & 2) >> 1];
784 if (chctx->codewords[j] >= middle_value)
785 chctx->CWdecoded[j] = quantizer[chctx->codewords[j] - 8] * chctx->flcoeffs6[i];
787 chctx->CWdecoded[j] = -quantizer[max_size - chctx->codewords[j] - 8 - 1] * chctx->flcoeffs6[i];
789 quantizer = imc_quantizer1[((stream_format_code & 2) >> 1) | (chctx->bandFlagsBuf[i] << 1)];
790 if (chctx->codewords[j] >= middle_value)
791 chctx->CWdecoded[j] = quantizer[chctx->codewords[j] - 1] * chctx->flcoeffs6[i];
793 chctx->CWdecoded[j] = -quantizer[max_size - 2 - chctx->codewords[j]] * chctx->flcoeffs6[i];
801 static void imc_get_coeffs(AVCodecContext *avctx,
802 IMCContext *q, IMCChannel *chctx)
804 int i, j, cw_len, cw;
806 for (i = 0; i < BANDS; i++) {
807 if (!chctx->sumLenArr[i])
809 if (chctx->bandFlagsBuf[i] || chctx->bandWidthT[i]) {
810 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
811 cw_len = chctx->CWlengthT[j];
814 if (cw_len && (!chctx->bandFlagsBuf[i] || !chctx->skipFlags[j])) {
815 if (get_bits_count(&q->gb) + cw_len > 512) {
816 av_log(avctx, AV_LOG_WARNING,
817 "Potential problem on band %i, coefficient %i"
818 ": cw_len=%i\n", i, j, cw_len);
820 cw = get_bits(&q->gb, cw_len);
823 chctx->codewords[j] = cw;
829 static void imc_refine_bit_allocation(IMCContext *q, IMCChannel *chctx)
834 for (i = 0; i < BANDS; i++) {
835 chctx->sumLenArr[i] = 0;
836 chctx->skipFlagRaw[i] = 0;
837 for (j = band_tab[i]; j < band_tab[i + 1]; j++)
838 chctx->sumLenArr[i] += chctx->CWlengthT[j];
839 if (chctx->bandFlagsBuf[i])
840 if (((int)((band_tab[i + 1] - band_tab[i]) * 1.5) > chctx->sumLenArr[i]) && (chctx->sumLenArr[i] > 0))
841 chctx->skipFlagRaw[i] = 1;
844 imc_get_skip_coeff(q, chctx);
846 for (i = 0; i < BANDS; i++) {
847 chctx->flcoeffs6[i] = chctx->flcoeffs1[i];
848 /* band has flag set and at least one coded coefficient */
849 if (chctx->bandFlagsBuf[i] && (band_tab[i + 1] - band_tab[i]) != chctx->skipFlagCount[i]) {
850 chctx->flcoeffs6[i] *= q->sqrt_tab[ band_tab[i + 1] - band_tab[i]] /
851 q->sqrt_tab[(band_tab[i + 1] - band_tab[i] - chctx->skipFlagCount[i])];
855 /* calculate bits left, bits needed and adjust bit allocation */
858 for (i = 0; i < BANDS; i++) {
859 if (chctx->bandFlagsBuf[i]) {
860 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
861 if (chctx->skipFlags[j]) {
862 summer += chctx->CWlengthT[j];
863 chctx->CWlengthT[j] = 0;
866 bits += chctx->skipFlagBits[i];
867 summer -= chctx->skipFlagBits[i];
870 imc_adjust_bit_allocation(q, chctx, summer);
873 static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch)
875 int stream_format_code;
876 int imc_hdr, i, j, ret;
879 int counter, bitscount;
880 IMCChannel *chctx = q->chctx + ch;
883 /* Check the frame header */
884 imc_hdr = get_bits(&q->gb, 9);
885 if (imc_hdr & 0x18) {
886 av_log(avctx, AV_LOG_ERROR, "frame header check failed!\n");
887 av_log(avctx, AV_LOG_ERROR, "got %X.\n", imc_hdr);
888 return AVERROR_INVALIDDATA;
890 stream_format_code = get_bits(&q->gb, 3);
892 if (stream_format_code & 0x04)
893 chctx->decoder_reset = 1;
895 if (chctx->decoder_reset) {
896 for (i = 0; i < BANDS; i++)
897 chctx->old_floor[i] = 1.0;
898 for (i = 0; i < COEFFS; i++)
899 chctx->CWdecoded[i] = 0;
900 chctx->decoder_reset = 0;
903 flag = get_bits1(&q->gb);
904 if (stream_format_code & 0x1)
905 imc_read_level_coeffs_raw(q, stream_format_code, chctx->levlCoeffBuf);
907 imc_read_level_coeffs(q, stream_format_code, chctx->levlCoeffBuf);
909 if (stream_format_code & 0x1)
910 imc_decode_level_coefficients_raw(q, chctx->levlCoeffBuf,
911 chctx->flcoeffs1, chctx->flcoeffs2);
912 else if (stream_format_code & 0x4)
913 imc_decode_level_coefficients(q, chctx->levlCoeffBuf,
914 chctx->flcoeffs1, chctx->flcoeffs2);
916 imc_decode_level_coefficients2(q, chctx->levlCoeffBuf, chctx->old_floor,
917 chctx->flcoeffs1, chctx->flcoeffs2);
919 for(i=0; i<BANDS; i++) {
920 if(chctx->flcoeffs1[i] > INT_MAX) {
921 av_log(avctx, AV_LOG_ERROR, "scalefactor out of range\n");
922 return AVERROR_INVALIDDATA;
926 memcpy(chctx->old_floor, chctx->flcoeffs1, 32 * sizeof(float));
929 if (stream_format_code & 0x1) {
930 for (i = 0; i < BANDS; i++) {
931 chctx->bandWidthT[i] = band_tab[i + 1] - band_tab[i];
932 chctx->bandFlagsBuf[i] = 0;
933 chctx->flcoeffs3[i] = chctx->flcoeffs2[i] * 2;
934 chctx->flcoeffs5[i] = 1.0;
937 for (i = 0; i < BANDS; i++) {
938 if (chctx->levlCoeffBuf[i] == 16) {
939 chctx->bandWidthT[i] = 0;
942 chctx->bandWidthT[i] = band_tab[i + 1] - band_tab[i];
945 memset(chctx->bandFlagsBuf, 0, BANDS * sizeof(int));
946 for (i = 0; i < BANDS - 1; i++)
947 if (chctx->bandWidthT[i])
948 chctx->bandFlagsBuf[i] = get_bits1(&q->gb);
950 imc_calculate_coeffs(q, chctx->flcoeffs1, chctx->flcoeffs2,
951 chctx->bandWidthT, chctx->flcoeffs3,
956 /* first 4 bands will be assigned 5 bits per coefficient */
957 if (stream_format_code & 0x2) {
960 chctx->bitsBandT[0] = 5;
961 chctx->CWlengthT[0] = 5;
962 chctx->CWlengthT[1] = 5;
963 chctx->CWlengthT[2] = 5;
964 for (i = 1; i < 4; i++) {
965 if (stream_format_code & 0x1)
968 bits = (chctx->levlCoeffBuf[i] == 16) ? 0 : 5;
969 chctx->bitsBandT[i] = bits;
970 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
971 chctx->CWlengthT[j] = bits;
976 if (avctx->codec_id == AV_CODEC_ID_IAC) {
977 bitscount += !!chctx->bandWidthT[BANDS - 1];
978 if (!(stream_format_code & 0x2))
982 if ((ret = bit_allocation(q, chctx, stream_format_code,
983 512 - bitscount - get_bits_count(&q->gb),
985 av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
986 chctx->decoder_reset = 1;
990 if (stream_format_code & 0x1) {
991 for (i = 0; i < BANDS; i++)
992 chctx->skipFlags[i] = 0;
994 imc_refine_bit_allocation(q, chctx);
997 for (i = 0; i < BANDS; i++) {
998 chctx->sumLenArr[i] = 0;
1000 for (j = band_tab[i]; j < band_tab[i + 1]; j++)
1001 if (!chctx->skipFlags[j])
1002 chctx->sumLenArr[i] += chctx->CWlengthT[j];
1005 memset(chctx->codewords, 0, sizeof(chctx->codewords));
1007 imc_get_coeffs(avctx, q, chctx);
1009 if (inverse_quant_coeff(q, chctx, stream_format_code) < 0) {
1010 av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n");
1011 chctx->decoder_reset = 1;
1012 return AVERROR_INVALIDDATA;
1015 memset(chctx->skipFlags, 0, sizeof(chctx->skipFlags));
1017 imc_imdct256(q, chctx, avctx->channels);
1022 static int imc_decode_frame(AVCodecContext *avctx, void *data,
1023 int *got_frame_ptr, AVPacket *avpkt)
1025 AVFrame *frame = data;
1026 const uint8_t *buf = avpkt->data;
1027 int buf_size = avpkt->size;
1030 IMCContext *q = avctx->priv_data;
1032 LOCAL_ALIGNED_16(uint16_t, buf16, [(IMC_BLOCK_SIZE + AV_INPUT_BUFFER_PADDING_SIZE) / 2]);
1036 if (buf_size < IMC_BLOCK_SIZE * avctx->channels) {
1037 av_log(avctx, AV_LOG_ERROR, "frame too small!\n");
1038 return AVERROR_INVALIDDATA;
1041 /* get output buffer */
1042 frame->nb_samples = COEFFS;
1043 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1046 for (i = 0; i < avctx->channels; i++) {
1047 q->out_samples = (float *)frame->extended_data[i];
1049 q->bdsp.bswap16_buf(buf16, (const uint16_t *) buf, IMC_BLOCK_SIZE / 2);
1051 init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8);
1053 buf += IMC_BLOCK_SIZE;
1055 if ((ret = imc_decode_block(avctx, q, i)) < 0)
1059 if (avctx->channels == 2) {
1060 q->butterflies_float((float *)frame->extended_data[0],
1061 (float *)frame->extended_data[1], COEFFS);
1066 return IMC_BLOCK_SIZE * avctx->channels;
1069 static av_cold int imc_decode_close(AVCodecContext * avctx)
1071 IMCContext *q = avctx->priv_data;
1073 ff_fft_end(&q->fft);
1078 static av_cold void flush(AVCodecContext *avctx)
1080 IMCContext *q = avctx->priv_data;
1082 q->chctx[0].decoder_reset =
1083 q->chctx[1].decoder_reset = 1;
1086 #if CONFIG_IMC_DECODER
1087 AVCodec ff_imc_decoder = {
1089 .long_name = NULL_IF_CONFIG_SMALL("IMC (Intel Music Coder)"),
1090 .type = AVMEDIA_TYPE_AUDIO,
1091 .id = AV_CODEC_ID_IMC,
1092 .priv_data_size = sizeof(IMCContext),
1093 .init = imc_decode_init,
1094 .close = imc_decode_close,
1095 .decode = imc_decode_frame,
1097 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
1098 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1099 AV_SAMPLE_FMT_NONE },
1100 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
1103 #if CONFIG_IAC_DECODER
1104 AVCodec ff_iac_decoder = {
1106 .long_name = NULL_IF_CONFIG_SMALL("IAC (Indeo Audio Coder)"),
1107 .type = AVMEDIA_TYPE_AUDIO,
1108 .id = AV_CODEC_ID_IAC,
1109 .priv_data_size = sizeof(IMCContext),
1110 .init = imc_decode_init,
1111 .close = imc_decode_close,
1112 .decode = imc_decode_frame,
1114 .capabilities = AV_CODEC_CAP_DR1,
1115 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1116 AV_SAMPLE_FMT_NONE },
1117 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,