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 Libav.
9 * Libav 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 * Libav 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 Libav; 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/float_dsp.h"
39 #include "libavutil/internal.h"
42 #include "bitstream.h"
50 #define IMC_BLOCK_SIZE 64
51 #define IMC_FRAME_ID 0x21
55 typedef struct IMCChannel {
56 float old_floor[BANDS];
57 float flcoeffs1[BANDS];
58 float flcoeffs2[BANDS];
59 float flcoeffs3[BANDS];
60 float flcoeffs4[BANDS];
61 float flcoeffs5[BANDS];
62 float flcoeffs6[BANDS];
63 float CWdecoded[COEFFS];
65 int bandWidthT[BANDS]; ///< codewords per band
66 int bitsBandT[BANDS]; ///< how many bits per codeword in band
67 int CWlengthT[COEFFS]; ///< how many bits in each codeword
68 int levlCoeffBuf[BANDS];
69 int bandFlagsBuf[BANDS]; ///< flags for each band
70 int sumLenArr[BANDS]; ///< bits for all coeffs in band
71 int skipFlagRaw[BANDS]; ///< skip flags are stored in raw form or not
72 int skipFlagBits[BANDS]; ///< bits used to code skip flags
73 int skipFlagCount[BANDS]; ///< skipped coefficients per band
74 int skipFlags[COEFFS]; ///< skip coefficient decoding or not
75 int codewords[COEFFS]; ///< raw codewords read from bitstream
77 float last_fft_im[COEFFS];
82 typedef struct IMCContext {
87 float mdct_sine_window[COEFFS];
88 float post_cos[COEFFS];
89 float post_sin[COEFFS];
90 float pre_coef1[COEFFS];
91 float pre_coef2[COEFFS];
98 AVFloatDSPContext fdsp;
100 DECLARE_ALIGNED(32, FFTComplex, samples)[COEFFS / 2];
105 int8_t cyclTab[32], cyclTab2[32];
106 float weights1[31], weights2[31];
109 static VLC huffman_vlc[4][4];
111 #define VLC_TABLES_SIZE 9512
113 static const int vlc_offsets[17] = {
114 0, 640, 1156, 1732, 2308, 2852, 3396, 3924,
115 4452, 5220, 5860, 6628, 7268, 7908, 8424, 8936, VLC_TABLES_SIZE
118 static VLC_TYPE vlc_tables[VLC_TABLES_SIZE][2];
120 static inline double freq2bark(double freq)
122 return 3.5 * atan((freq / 7500.0) * (freq / 7500.0)) + 13.0 * atan(freq * 0.00076);
125 static av_cold void iac_generate_tabs(IMCContext *q, int sampling_rate)
127 double freqmin[32], freqmid[32], freqmax[32];
128 double scale = sampling_rate / (256.0 * 2.0 * 2.0);
129 double nyquist_freq = sampling_rate * 0.5;
130 double freq, bark, prev_bark = 0, tf, tb;
133 for (i = 0; i < 32; i++) {
134 freq = (band_tab[i] + band_tab[i + 1] - 1) * scale;
135 bark = freq2bark(freq);
138 tb = bark - prev_bark;
139 q->weights1[i - 1] = pow(10.0, -1.0 * tb);
140 q->weights2[i - 1] = pow(10.0, -2.7 * tb);
147 while (tf < nyquist_freq) {
159 if (tb <= bark - 0.5)
165 for (i = 0; i < 32; i++) {
167 for (j = 31; j > 0 && freq <= freqmid[j]; j--);
168 q->cyclTab[i] = j + 1;
171 for (j = 0; j < 32 && freq >= freqmid[j]; j++);
172 q->cyclTab2[i] = j - 1;
176 static av_cold int imc_decode_init(AVCodecContext *avctx)
179 IMCContext *q = avctx->priv_data;
182 if (avctx->codec_id == AV_CODEC_ID_IMC)
185 if (avctx->channels > 2) {
186 avpriv_request_sample(avctx, "Number of channels > 2");
187 return AVERROR_PATCHWELCOME;
190 for (j = 0; j < avctx->channels; j++) {
191 q->chctx[j].decoder_reset = 1;
193 for (i = 0; i < BANDS; i++)
194 q->chctx[j].old_floor[i] = 1.0;
196 for (i = 0; i < COEFFS / 2; i++)
197 q->chctx[j].last_fft_im[i] = 0;
200 /* Build mdct window, a simple sine window normalized with sqrt(2) */
201 ff_sine_window_init(q->mdct_sine_window, COEFFS);
202 for (i = 0; i < COEFFS; i++)
203 q->mdct_sine_window[i] *= sqrt(2.0);
204 for (i = 0; i < COEFFS / 2; i++) {
205 q->post_cos[i] = (1.0f / 32768) * cos(i / 256.0 * M_PI);
206 q->post_sin[i] = (1.0f / 32768) * sin(i / 256.0 * M_PI);
208 r1 = sin((i * 4.0 + 1.0) / 1024.0 * M_PI);
209 r2 = cos((i * 4.0 + 1.0) / 1024.0 * M_PI);
212 q->pre_coef1[i] = (r1 + r2) * sqrt(2.0);
213 q->pre_coef2[i] = -(r1 - r2) * sqrt(2.0);
215 q->pre_coef1[i] = -(r1 + r2) * sqrt(2.0);
216 q->pre_coef2[i] = (r1 - r2) * sqrt(2.0);
220 /* Generate a square root table */
222 for (i = 0; i < 30; i++)
223 q->sqrt_tab[i] = sqrt(i);
225 /* initialize the VLC tables */
226 for (i = 0; i < 4 ; i++) {
227 for (j = 0; j < 4; j++) {
228 huffman_vlc[i][j].table = &vlc_tables[vlc_offsets[i * 4 + j]];
229 huffman_vlc[i][j].table_allocated = vlc_offsets[i * 4 + j + 1] - vlc_offsets[i * 4 + j];
230 init_vlc(&huffman_vlc[i][j], 9, imc_huffman_sizes[i],
231 imc_huffman_lens[i][j], 1, 1,
232 imc_huffman_bits[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
236 if (avctx->codec_id == AV_CODEC_ID_IAC) {
237 iac_generate_tabs(q, avctx->sample_rate);
239 memcpy(q->cyclTab, cyclTab, sizeof(cyclTab));
240 memcpy(q->cyclTab2, cyclTab2, sizeof(cyclTab2));
241 memcpy(q->weights1, imc_weights1, sizeof(imc_weights1));
242 memcpy(q->weights2, imc_weights2, sizeof(imc_weights2));
245 if ((ret = ff_fft_init(&q->fft, 7, 1))) {
246 av_log(avctx, AV_LOG_INFO, "FFT init failed\n");
249 ff_bswapdsp_init(&q->bdsp);
250 avpriv_float_dsp_init(&q->fdsp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
251 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
252 avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO
253 : AV_CH_LAYOUT_STEREO;
258 static void imc_calculate_coeffs(IMCContext *q, float *flcoeffs1,
259 float *flcoeffs2, int *bandWidthT,
260 float *flcoeffs3, float *flcoeffs5)
265 float snr_limit = 1.e-30;
269 for (i = 0; i < BANDS; i++) {
270 flcoeffs5[i] = workT2[i] = 0.0;
272 workT1[i] = flcoeffs1[i] * flcoeffs1[i];
273 flcoeffs3[i] = 2.0 * flcoeffs2[i];
276 flcoeffs3[i] = -30000.0;
278 workT3[i] = bandWidthT[i] * workT1[i] * 0.01;
279 if (workT3[i] <= snr_limit)
283 for (i = 0; i < BANDS; i++) {
284 for (cnt2 = i; cnt2 < q->cyclTab[i]; cnt2++)
285 flcoeffs5[cnt2] = flcoeffs5[cnt2] + workT3[i];
286 workT2[cnt2 - 1] = workT2[cnt2 - 1] + workT3[i];
289 for (i = 1; i < BANDS; i++) {
290 accum = (workT2[i - 1] + accum) * q->weights1[i - 1];
291 flcoeffs5[i] += accum;
294 for (i = 0; i < BANDS; i++)
297 for (i = 0; i < BANDS; i++) {
298 for (cnt2 = i - 1; cnt2 > q->cyclTab2[i]; cnt2--)
299 flcoeffs5[cnt2] += workT3[i];
300 workT2[cnt2+1] += workT3[i];
305 for (i = BANDS-2; i >= 0; i--) {
306 accum = (workT2[i+1] + accum) * q->weights2[i];
307 flcoeffs5[i] += accum;
308 // there is missing code here, but it seems to never be triggered
313 static void imc_read_level_coeffs(IMCContext *q, int stream_format_code,
319 const uint8_t *cb_sel;
322 s = stream_format_code >> 1;
323 hufftab[0] = &huffman_vlc[s][0];
324 hufftab[1] = &huffman_vlc[s][1];
325 hufftab[2] = &huffman_vlc[s][2];
326 hufftab[3] = &huffman_vlc[s][3];
327 cb_sel = imc_cb_select[s];
329 if (stream_format_code & 4)
332 levlCoeffs[0] = bitstream_read(&q->bc, 7);
333 for (i = start; i < BANDS; i++) {
334 levlCoeffs[i] = bitstream_read_vlc(&q->bc, hufftab[cb_sel[i]]->table,
335 hufftab[cb_sel[i]]->bits, 2);
336 if (levlCoeffs[i] == 17)
337 levlCoeffs[i] += bitstream_read(&q->bc, 4);
341 static void imc_read_level_coeffs_raw(IMCContext *q, int stream_format_code,
346 q->coef0_pos = bitstream_read(&q->bc, 5);
347 levlCoeffs[0] = bitstream_read(&q->bc, 7);
348 for (i = 1; i < BANDS; i++)
349 levlCoeffs[i] = bitstream_read(&q->bc, 4);
352 static void imc_decode_level_coefficients(IMCContext *q, int *levlCoeffBuf,
353 float *flcoeffs1, float *flcoeffs2)
357 // maybe some frequency division thingy
359 flcoeffs1[0] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
360 flcoeffs2[0] = log2f(flcoeffs1[0]);
364 for (i = 1; i < BANDS; i++) {
365 level = levlCoeffBuf[i];
372 else if (level <= 24)
377 tmp *= imc_exp_tab[15 + level];
378 tmp2 += 0.83048 * level; // 0.83048 = log2(10) * 0.25
386 static void imc_decode_level_coefficients2(IMCContext *q, int *levlCoeffBuf,
387 float *old_floor, float *flcoeffs1,
391 /* FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors
392 * and flcoeffs2 old scale factors
393 * might be incomplete due to a missing table that is in the binary code
395 for (i = 0; i < BANDS; i++) {
397 if (levlCoeffBuf[i] < 16) {
398 flcoeffs1[i] = imc_exp_tab2[levlCoeffBuf[i]] * old_floor[i];
399 flcoeffs2[i] = (levlCoeffBuf[i] - 7) * 0.83048 + flcoeffs2[i]; // 0.83048 = log2(10) * 0.25
401 flcoeffs1[i] = old_floor[i];
406 static void imc_decode_level_coefficients_raw(IMCContext *q, int *levlCoeffBuf,
407 float *flcoeffs1, float *flcoeffs2)
413 flcoeffs1[pos] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
414 flcoeffs2[pos] = log2f(flcoeffs1[pos]);
415 tmp = flcoeffs1[pos];
416 tmp2 = flcoeffs2[pos];
419 for (i = 0; i < BANDS; i++) {
422 level = *levlCoeffBuf++;
423 flcoeffs1[i] = tmp * powf(10.0, -level * 0.4375); //todo tab
424 flcoeffs2[i] = tmp2 - 1.4533435415 * level; // 1.4533435415 = log2(10) * 0.4375
429 * Perform bit allocation depending on bits available
431 static int bit_allocation(IMCContext *q, IMCChannel *chctx,
432 int stream_format_code, int freebits, int flag)
435 const float limit = -1.e20;
444 float lowest = 1.e10;
450 for (i = 0; i < BANDS; i++)
451 highest = FFMAX(highest, chctx->flcoeffs1[i]);
453 for (i = 0; i < BANDS - 1; i++)
454 chctx->flcoeffs4[i] = chctx->flcoeffs3[i] - log2f(chctx->flcoeffs5[i]);
455 chctx->flcoeffs4[BANDS - 1] = limit;
457 highest = highest * 0.25;
459 for (i = 0; i < BANDS; i++) {
461 if ((band_tab[i + 1] - band_tab[i]) == chctx->bandWidthT[i])
464 if ((band_tab[i + 1] - band_tab[i]) > chctx->bandWidthT[i])
467 if (((band_tab[i + 1] - band_tab[i]) / 2) >= chctx->bandWidthT[i])
471 return AVERROR_INVALIDDATA;
473 chctx->flcoeffs4[i] += xTab[(indx * 2 + (chctx->flcoeffs1[i] < highest)) * 2 + flag];
476 if (stream_format_code & 0x2) {
477 chctx->flcoeffs4[0] = limit;
478 chctx->flcoeffs4[1] = limit;
479 chctx->flcoeffs4[2] = limit;
480 chctx->flcoeffs4[3] = limit;
483 for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS - 1; i++) {
484 iacc += chctx->bandWidthT[i];
485 summa += chctx->bandWidthT[i] * chctx->flcoeffs4[i];
489 return AVERROR_INVALIDDATA;
491 chctx->bandWidthT[BANDS - 1] = 0;
492 summa = (summa * 0.5 - freebits) / iacc;
495 for (i = 0; i < BANDS / 2; i++) {
496 rres = summer - freebits;
497 if ((rres >= -8) && (rres <= 8))
503 for (j = (stream_format_code & 0x2) ? 4 : 0; j < BANDS; j++) {
504 cwlen = av_clipf(((chctx->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6);
506 chctx->bitsBandT[j] = cwlen;
507 summer += chctx->bandWidthT[j] * cwlen;
510 iacc += chctx->bandWidthT[j];
515 if (freebits < summer)
522 summa = (float)(summer - freebits) / ((t1 + 1) * iacc) + summa;
525 for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS; i++) {
526 for (j = band_tab[i]; j < band_tab[i + 1]; j++)
527 chctx->CWlengthT[j] = chctx->bitsBandT[i];
530 if (freebits > summer) {
531 for (i = 0; i < BANDS; i++) {
532 workT[i] = (chctx->bitsBandT[i] == 6) ? -1.e20
533 : (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] - 0.415);
539 if (highest <= -1.e20)
545 for (i = 0; i < BANDS; i++) {
546 if (workT[i] > highest) {
552 if (highest > -1.e20) {
553 workT[found_indx] -= 2.0;
554 if (++chctx->bitsBandT[found_indx] == 6)
555 workT[found_indx] = -1.e20;
557 for (j = band_tab[found_indx]; j < band_tab[found_indx + 1] && (freebits > summer); j++) {
558 chctx->CWlengthT[j]++;
562 } while (freebits > summer);
564 if (freebits < summer) {
565 for (i = 0; i < BANDS; i++) {
566 workT[i] = chctx->bitsBandT[i] ? (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] + 1.585)
569 if (stream_format_code & 0x2) {
575 while (freebits < summer) {
578 for (i = 0; i < BANDS; i++) {
579 if (workT[i] < lowest) {
584 // if (lowest >= 1.e10)
586 workT[low_indx] = lowest + 2.0;
588 if (!--chctx->bitsBandT[low_indx])
589 workT[low_indx] = 1.e20;
591 for (j = band_tab[low_indx]; j < band_tab[low_indx+1] && (freebits < summer); j++) {
592 if (chctx->CWlengthT[j] > 0) {
593 chctx->CWlengthT[j]--;
602 static void imc_get_skip_coeff(IMCContext *q, IMCChannel *chctx)
606 memset(chctx->skipFlagBits, 0, sizeof(chctx->skipFlagBits));
607 memset(chctx->skipFlagCount, 0, sizeof(chctx->skipFlagCount));
608 for (i = 0; i < BANDS; i++) {
609 if (!chctx->bandFlagsBuf[i] || !chctx->bandWidthT[i])
612 if (!chctx->skipFlagRaw[i]) {
613 chctx->skipFlagBits[i] = band_tab[i + 1] - band_tab[i];
615 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
616 chctx->skipFlags[j] = bitstream_read_bit(&q->bc);
617 if (chctx->skipFlags[j])
618 chctx->skipFlagCount[i]++;
621 for (j = band_tab[i]; j < band_tab[i + 1] - 1; j += 2) {
622 if (!bitstream_read_bit(&q->bc)) { // 0
623 chctx->skipFlagBits[i]++;
624 chctx->skipFlags[j] = 1;
625 chctx->skipFlags[j + 1] = 1;
626 chctx->skipFlagCount[i] += 2;
628 if (bitstream_read_bit(&q->bc)) { // 11
629 chctx->skipFlagBits[i] += 2;
630 chctx->skipFlags[j] = 0;
631 chctx->skipFlags[j + 1] = 1;
632 chctx->skipFlagCount[i]++;
634 chctx->skipFlagBits[i] += 3;
635 chctx->skipFlags[j + 1] = 0;
636 if (!bitstream_read_bit(&q->bc)) { // 100
637 chctx->skipFlags[j] = 1;
638 chctx->skipFlagCount[i]++;
640 chctx->skipFlags[j] = 0;
646 if (j < band_tab[i + 1]) {
647 chctx->skipFlagBits[i]++;
648 if ((chctx->skipFlags[j] = bitstream_read_bit(&q->bc)))
649 chctx->skipFlagCount[i]++;
656 * Increase highest' band coefficient sizes as some bits won't be used
658 static void imc_adjust_bit_allocation(IMCContext *q, IMCChannel *chctx,
667 for (i = 0; i < BANDS; i++) {
668 workT[i] = (chctx->bitsBandT[i] == 6) ? -1.e20
669 : (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] - 0.415);
672 while (corrected < summer) {
673 if (highest <= -1.e20)
678 for (i = 0; i < BANDS; i++) {
679 if (workT[i] > highest) {
685 if (highest > -1.e20) {
686 workT[found_indx] -= 2.0;
687 if (++(chctx->bitsBandT[found_indx]) == 6)
688 workT[found_indx] = -1.e20;
690 for (j = band_tab[found_indx]; j < band_tab[found_indx+1] && (corrected < summer); j++) {
691 if (!chctx->skipFlags[j] && (chctx->CWlengthT[j] < 6)) {
692 chctx->CWlengthT[j]++;
700 static void imc_imdct256(IMCContext *q, IMCChannel *chctx, int channels)
704 float *dst1 = q->out_samples;
705 float *dst2 = q->out_samples + (COEFFS - 1);
708 for (i = 0; i < COEFFS / 2; i++) {
709 q->samples[i].re = -(q->pre_coef1[i] * chctx->CWdecoded[COEFFS - 1 - i * 2]) -
710 (q->pre_coef2[i] * chctx->CWdecoded[i * 2]);
711 q->samples[i].im = (q->pre_coef2[i] * chctx->CWdecoded[COEFFS - 1 - i * 2]) -
712 (q->pre_coef1[i] * chctx->CWdecoded[i * 2]);
716 q->fft.fft_permute(&q->fft, q->samples);
717 q->fft.fft_calc(&q->fft, q->samples);
719 /* postrotation, window and reorder */
720 for (i = 0; i < COEFFS / 2; i++) {
721 re = ( q->samples[i].re * q->post_cos[i]) + (-q->samples[i].im * q->post_sin[i]);
722 im = (-q->samples[i].im * q->post_cos[i]) - ( q->samples[i].re * q->post_sin[i]);
723 *dst1 = (q->mdct_sine_window[COEFFS - 1 - i * 2] * chctx->last_fft_im[i])
724 + (q->mdct_sine_window[i * 2] * re);
725 *dst2 = (q->mdct_sine_window[i * 2] * chctx->last_fft_im[i])
726 - (q->mdct_sine_window[COEFFS - 1 - i * 2] * re);
729 chctx->last_fft_im[i] = im;
733 static int inverse_quant_coeff(IMCContext *q, IMCChannel *chctx,
734 int stream_format_code)
737 int middle_value, cw_len, max_size;
738 const float *quantizer;
740 for (i = 0; i < BANDS; i++) {
741 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
742 chctx->CWdecoded[j] = 0;
743 cw_len = chctx->CWlengthT[j];
745 if (cw_len <= 0 || chctx->skipFlags[j])
748 max_size = 1 << cw_len;
749 middle_value = max_size >> 1;
751 if (chctx->codewords[j] >= max_size || chctx->codewords[j] < 0)
752 return AVERROR_INVALIDDATA;
755 quantizer = imc_quantizer2[(stream_format_code & 2) >> 1];
756 if (chctx->codewords[j] >= middle_value)
757 chctx->CWdecoded[j] = quantizer[chctx->codewords[j] - 8] * chctx->flcoeffs6[i];
759 chctx->CWdecoded[j] = -quantizer[max_size - chctx->codewords[j] - 8 - 1] * chctx->flcoeffs6[i];
761 quantizer = imc_quantizer1[((stream_format_code & 2) >> 1) | (chctx->bandFlagsBuf[i] << 1)];
762 if (chctx->codewords[j] >= middle_value)
763 chctx->CWdecoded[j] = quantizer[chctx->codewords[j] - 1] * chctx->flcoeffs6[i];
765 chctx->CWdecoded[j] = -quantizer[max_size - 2 - chctx->codewords[j]] * chctx->flcoeffs6[i];
773 static int imc_get_coeffs(IMCContext *q, IMCChannel *chctx)
775 int i, j, cw_len, cw;
777 for (i = 0; i < BANDS; i++) {
778 if (!chctx->sumLenArr[i])
780 if (chctx->bandFlagsBuf[i] || chctx->bandWidthT[i]) {
781 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
782 cw_len = chctx->CWlengthT[j];
785 if (bitstream_tell(&q->bc) + cw_len > 512) {
786 ff_dlog(NULL, "Band %i coeff %i cw_len %i\n", i, j, cw_len);
787 return AVERROR_INVALIDDATA;
790 if (cw_len && (!chctx->bandFlagsBuf[i] || !chctx->skipFlags[j]))
791 cw = bitstream_read(&q->bc, cw_len);
793 chctx->codewords[j] = cw;
800 static void imc_refine_bit_allocation(IMCContext *q, IMCChannel *chctx)
805 for (i = 0; i < BANDS; i++) {
806 chctx->sumLenArr[i] = 0;
807 chctx->skipFlagRaw[i] = 0;
808 for (j = band_tab[i]; j < band_tab[i + 1]; j++)
809 chctx->sumLenArr[i] += chctx->CWlengthT[j];
810 if (chctx->bandFlagsBuf[i])
811 if ((((band_tab[i + 1] - band_tab[i]) * 1.5) > chctx->sumLenArr[i]) && (chctx->sumLenArr[i] > 0))
812 chctx->skipFlagRaw[i] = 1;
815 imc_get_skip_coeff(q, chctx);
817 for (i = 0; i < BANDS; i++) {
818 chctx->flcoeffs6[i] = chctx->flcoeffs1[i];
819 /* band has flag set and at least one coded coefficient */
820 if (chctx->bandFlagsBuf[i] && (band_tab[i + 1] - band_tab[i]) != chctx->skipFlagCount[i]) {
821 chctx->flcoeffs6[i] *= q->sqrt_tab[ band_tab[i + 1] - band_tab[i]] /
822 q->sqrt_tab[(band_tab[i + 1] - band_tab[i] - chctx->skipFlagCount[i])];
826 /* calculate bits left, bits needed and adjust bit allocation */
829 for (i = 0; i < BANDS; i++) {
830 if (chctx->bandFlagsBuf[i]) {
831 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
832 if (chctx->skipFlags[j]) {
833 summer += chctx->CWlengthT[j];
834 chctx->CWlengthT[j] = 0;
837 bits += chctx->skipFlagBits[i];
838 summer -= chctx->skipFlagBits[i];
841 imc_adjust_bit_allocation(q, chctx, summer);
844 static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch)
846 int stream_format_code;
847 int imc_hdr, i, j, ret;
850 int counter, bitscount;
851 IMCChannel *chctx = q->chctx + ch;
854 /* Check the frame header */
855 imc_hdr = bitstream_read(&q->bc, 9);
856 if (imc_hdr & 0x18) {
857 av_log(avctx, AV_LOG_ERROR, "frame header check failed!\n");
858 av_log(avctx, AV_LOG_ERROR, "got %X.\n", imc_hdr);
859 return AVERROR_INVALIDDATA;
861 stream_format_code = bitstream_read(&q->bc, 3);
863 if (stream_format_code & 0x04)
864 chctx->decoder_reset = 1;
866 if (chctx->decoder_reset) {
867 for (i = 0; i < BANDS; i++)
868 chctx->old_floor[i] = 1.0;
869 for (i = 0; i < COEFFS; i++)
870 chctx->CWdecoded[i] = 0;
871 chctx->decoder_reset = 0;
874 flag = bitstream_read_bit(&q->bc);
875 if (stream_format_code & 0x1)
876 imc_read_level_coeffs_raw(q, stream_format_code, chctx->levlCoeffBuf);
878 imc_read_level_coeffs(q, stream_format_code, chctx->levlCoeffBuf);
880 if (stream_format_code & 0x1)
881 imc_decode_level_coefficients_raw(q, chctx->levlCoeffBuf,
882 chctx->flcoeffs1, chctx->flcoeffs2);
883 else if (stream_format_code & 0x4)
884 imc_decode_level_coefficients(q, chctx->levlCoeffBuf,
885 chctx->flcoeffs1, chctx->flcoeffs2);
887 imc_decode_level_coefficients2(q, chctx->levlCoeffBuf, chctx->old_floor,
888 chctx->flcoeffs1, chctx->flcoeffs2);
890 memcpy(chctx->old_floor, chctx->flcoeffs1, 32 * sizeof(float));
893 if (stream_format_code & 0x1) {
894 for (i = 0; i < BANDS; i++) {
895 chctx->bandWidthT[i] = band_tab[i + 1] - band_tab[i];
896 chctx->bandFlagsBuf[i] = 0;
897 chctx->flcoeffs3[i] = chctx->flcoeffs2[i] * 2;
898 chctx->flcoeffs5[i] = 1.0;
901 for (i = 0; i < BANDS; i++) {
902 if (chctx->levlCoeffBuf[i] == 16) {
903 chctx->bandWidthT[i] = 0;
906 chctx->bandWidthT[i] = band_tab[i + 1] - band_tab[i];
909 memset(chctx->bandFlagsBuf, 0, BANDS * sizeof(int));
910 for (i = 0; i < BANDS - 1; i++)
911 if (chctx->bandWidthT[i])
912 chctx->bandFlagsBuf[i] = bitstream_read_bit(&q->bc);
914 imc_calculate_coeffs(q, chctx->flcoeffs1, chctx->flcoeffs2,
915 chctx->bandWidthT, chctx->flcoeffs3,
920 /* first 4 bands will be assigned 5 bits per coefficient */
921 if (stream_format_code & 0x2) {
924 chctx->bitsBandT[0] = 5;
925 chctx->CWlengthT[0] = 5;
926 chctx->CWlengthT[1] = 5;
927 chctx->CWlengthT[2] = 5;
928 for (i = 1; i < 4; i++) {
929 if (stream_format_code & 0x1)
932 bits = (chctx->levlCoeffBuf[i] == 16) ? 0 : 5;
933 chctx->bitsBandT[i] = bits;
934 for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
935 chctx->CWlengthT[j] = bits;
940 if (avctx->codec_id == AV_CODEC_ID_IAC) {
941 bitscount += !!chctx->bandWidthT[BANDS - 1];
942 if (!(stream_format_code & 0x2))
946 if ((ret = bit_allocation(q, chctx, stream_format_code,
947 512 - bitscount - bitstream_tell(&q->bc),
949 av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
950 chctx->decoder_reset = 1;
954 if (stream_format_code & 0x1) {
955 for (i = 0; i < BANDS; i++)
956 chctx->skipFlags[i] = 0;
958 imc_refine_bit_allocation(q, chctx);
961 for (i = 0; i < BANDS; i++) {
962 chctx->sumLenArr[i] = 0;
964 for (j = band_tab[i]; j < band_tab[i + 1]; j++)
965 if (!chctx->skipFlags[j])
966 chctx->sumLenArr[i] += chctx->CWlengthT[j];
969 memset(chctx->codewords, 0, sizeof(chctx->codewords));
971 if (imc_get_coeffs(q, chctx) < 0) {
972 av_log(avctx, AV_LOG_ERROR, "Read coefficients failed\n");
973 chctx->decoder_reset = 1;
974 return AVERROR_INVALIDDATA;
977 if (inverse_quant_coeff(q, chctx, stream_format_code) < 0) {
978 av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n");
979 chctx->decoder_reset = 1;
980 return AVERROR_INVALIDDATA;
983 memset(chctx->skipFlags, 0, sizeof(chctx->skipFlags));
985 imc_imdct256(q, chctx, avctx->channels);
990 static int imc_decode_frame(AVCodecContext *avctx, void *data,
991 int *got_frame_ptr, AVPacket *avpkt)
993 AVFrame *frame = data;
994 const uint8_t *buf = avpkt->data;
995 int buf_size = avpkt->size;
998 IMCContext *q = avctx->priv_data;
1000 LOCAL_ALIGNED_16(uint16_t, buf16, [(IMC_BLOCK_SIZE + AV_INPUT_BUFFER_PADDING_SIZE) / 2]);
1002 if (buf_size < IMC_BLOCK_SIZE * avctx->channels) {
1003 av_log(avctx, AV_LOG_ERROR, "frame too small!\n");
1004 return AVERROR_INVALIDDATA;
1007 /* get output buffer */
1008 frame->nb_samples = COEFFS;
1009 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
1010 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1014 for (i = 0; i < avctx->channels; i++) {
1015 q->out_samples = (float *)frame->extended_data[i];
1017 q->bdsp.bswap16_buf(buf16, (const uint16_t *) buf, IMC_BLOCK_SIZE / 2);
1019 bitstream_init8(&q->bc, (const uint8_t *)buf16, IMC_BLOCK_SIZE);
1021 buf += IMC_BLOCK_SIZE;
1023 if ((ret = imc_decode_block(avctx, q, i)) < 0)
1027 if (avctx->channels == 2) {
1028 q->fdsp.butterflies_float((float *)frame->extended_data[0],
1029 (float *)frame->extended_data[1], COEFFS);
1034 return IMC_BLOCK_SIZE * avctx->channels;
1038 static av_cold int imc_decode_close(AVCodecContext * avctx)
1040 IMCContext *q = avctx->priv_data;
1042 ff_fft_end(&q->fft);
1048 AVCodec ff_imc_decoder = {
1050 .long_name = NULL_IF_CONFIG_SMALL("IMC (Intel Music Coder)"),
1051 .type = AVMEDIA_TYPE_AUDIO,
1052 .id = AV_CODEC_ID_IMC,
1053 .priv_data_size = sizeof(IMCContext),
1054 .init = imc_decode_init,
1055 .close = imc_decode_close,
1056 .decode = imc_decode_frame,
1057 .capabilities = AV_CODEC_CAP_DR1,
1058 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1059 AV_SAMPLE_FMT_NONE },
1062 AVCodec ff_iac_decoder = {
1064 .long_name = NULL_IF_CONFIG_SMALL("IAC (Indeo Audio Coder)"),
1065 .type = AVMEDIA_TYPE_AUDIO,
1066 .id = AV_CODEC_ID_IAC,
1067 .priv_data_size = sizeof(IMCContext),
1068 .init = imc_decode_init,
1069 .close = imc_decode_close,
1070 .decode = imc_decode_frame,
1071 .capabilities = AV_CODEC_CAP_DR1,
1072 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1073 AV_SAMPLE_FMT_NONE },