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 * divied into 32 bands with some mix of scale factors.
29 * Only mono is supported.
38 #define ALT_BITSTREAM_READER
43 #include "libavutil/audioconvert.h"
48 #define IMC_BLOCK_SIZE 64
49 #define IMC_FRAME_ID 0x21
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];
67 float mdct_sine_window[COEFFS];
68 float post_cos[COEFFS];
69 float post_sin[COEFFS];
70 float pre_coef1[COEFFS];
71 float pre_coef2[COEFFS];
72 float last_fft_im[COEFFS];
75 int bandWidthT[BANDS]; ///< codewords per band
76 int bitsBandT[BANDS]; ///< how many bits per codeword in band
77 int CWlengthT[COEFFS]; ///< how many bits in each codeword
78 int levlCoeffBuf[BANDS];
79 int bandFlagsBuf[BANDS]; ///< flags for each band
80 int sumLenArr[BANDS]; ///< bits for all coeffs in band
81 int skipFlagRaw[BANDS]; ///< skip flags are stored in raw form or not
82 int skipFlagBits[BANDS]; ///< bits used to code skip flags
83 int skipFlagCount[BANDS]; ///< skipped coeffients per band
84 int skipFlags[COEFFS]; ///< skip coefficient decoding or not
85 int codewords[COEFFS]; ///< raw codewords read from bitstream
93 DECLARE_ALIGNED(32, FFTComplex, samples)[COEFFS/2];
97 static VLC huffman_vlc[4][4];
99 #define VLC_TABLES_SIZE 9512
101 static const int vlc_offsets[17] = {
102 0, 640, 1156, 1732, 2308, 2852, 3396, 3924,
103 4452, 5220, 5860, 6628, 7268, 7908, 8424, 8936, VLC_TABLES_SIZE};
105 static VLC_TYPE vlc_tables[VLC_TABLES_SIZE][2];
107 static av_cold int imc_decode_init(AVCodecContext * avctx)
110 IMCContext *q = avctx->priv_data;
113 if (avctx->channels != 1) {
114 av_log_ask_for_sample(avctx, "Number of channels is not supported\n");
115 return AVERROR_PATCHWELCOME;
118 q->decoder_reset = 1;
120 for(i = 0; i < BANDS; i++)
121 q->old_floor[i] = 1.0;
123 /* Build mdct window, a simple sine window normalized with sqrt(2) */
124 ff_sine_window_init(q->mdct_sine_window, COEFFS);
125 for(i = 0; i < COEFFS; i++)
126 q->mdct_sine_window[i] *= sqrt(2.0);
127 for(i = 0; i < COEFFS/2; i++){
128 q->post_cos[i] = (1.0f / 32768) * cos(i / 256.0 * M_PI);
129 q->post_sin[i] = (1.0f / 32768) * sin(i / 256.0 * M_PI);
131 r1 = sin((i * 4.0 + 1.0) / 1024.0 * M_PI);
132 r2 = cos((i * 4.0 + 1.0) / 1024.0 * M_PI);
136 q->pre_coef1[i] = (r1 + r2) * sqrt(2.0);
137 q->pre_coef2[i] = -(r1 - r2) * sqrt(2.0);
141 q->pre_coef1[i] = -(r1 + r2) * sqrt(2.0);
142 q->pre_coef2[i] = (r1 - r2) * sqrt(2.0);
145 q->last_fft_im[i] = 0;
148 /* Generate a square root table */
150 for(i = 0; i < 30; i++) {
151 q->sqrt_tab[i] = sqrt(i);
154 /* initialize the VLC tables */
155 for(i = 0; i < 4 ; i++) {
156 for(j = 0; j < 4; j++) {
157 huffman_vlc[i][j].table = &vlc_tables[vlc_offsets[i * 4 + j]];
158 huffman_vlc[i][j].table_allocated = vlc_offsets[i * 4 + j + 1] - vlc_offsets[i * 4 + j];
159 init_vlc(&huffman_vlc[i][j], 9, imc_huffman_sizes[i],
160 imc_huffman_lens[i][j], 1, 1,
161 imc_huffman_bits[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
164 q->one_div_log2 = 1/log(2);
166 if ((ret = ff_fft_init(&q->fft, 7, 1))) {
167 av_log(avctx, AV_LOG_INFO, "FFT init failed\n");
170 dsputil_init(&q->dsp, avctx);
171 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
172 avctx->channel_layout = AV_CH_LAYOUT_MONO;
174 avcodec_get_frame_defaults(&q->frame);
175 avctx->coded_frame = &q->frame;
180 static void imc_calculate_coeffs(IMCContext* q, float* flcoeffs1, float* flcoeffs2, int* bandWidthT,
181 float* flcoeffs3, float* flcoeffs5)
186 float snr_limit = 1.e-30;
190 for(i = 0; i < BANDS; i++) {
191 flcoeffs5[i] = workT2[i] = 0.0;
193 workT1[i] = flcoeffs1[i] * flcoeffs1[i];
194 flcoeffs3[i] = 2.0 * flcoeffs2[i];
197 flcoeffs3[i] = -30000.0;
199 workT3[i] = bandWidthT[i] * workT1[i] * 0.01;
200 if (workT3[i] <= snr_limit)
204 for(i = 0; i < BANDS; i++) {
205 for(cnt2 = i; cnt2 < cyclTab[i]; cnt2++)
206 flcoeffs5[cnt2] = flcoeffs5[cnt2] + workT3[i];
207 workT2[cnt2-1] = workT2[cnt2-1] + workT3[i];
210 for(i = 1; i < BANDS; i++) {
211 accum = (workT2[i-1] + accum) * imc_weights1[i-1];
212 flcoeffs5[i] += accum;
215 for(i = 0; i < BANDS; i++)
218 for(i = 0; i < BANDS; i++) {
219 for(cnt2 = i-1; cnt2 > cyclTab2[i]; cnt2--)
220 flcoeffs5[cnt2] += workT3[i];
221 workT2[cnt2+1] += workT3[i];
226 for(i = BANDS-2; i >= 0; i--) {
227 accum = (workT2[i+1] + accum) * imc_weights2[i];
228 flcoeffs5[i] += accum;
229 //there is missing code here, but it seems to never be triggered
234 static void imc_read_level_coeffs(IMCContext* q, int stream_format_code, int* levlCoeffs)
239 const uint8_t *cb_sel;
242 s = stream_format_code >> 1;
243 hufftab[0] = &huffman_vlc[s][0];
244 hufftab[1] = &huffman_vlc[s][1];
245 hufftab[2] = &huffman_vlc[s][2];
246 hufftab[3] = &huffman_vlc[s][3];
247 cb_sel = imc_cb_select[s];
249 if(stream_format_code & 4)
252 levlCoeffs[0] = get_bits(&q->gb, 7);
253 for(i = start; i < BANDS; i++){
254 levlCoeffs[i] = get_vlc2(&q->gb, hufftab[cb_sel[i]]->table, hufftab[cb_sel[i]]->bits, 2);
255 if(levlCoeffs[i] == 17)
256 levlCoeffs[i] += get_bits(&q->gb, 4);
260 static void imc_decode_level_coefficients(IMCContext* q, int* levlCoeffBuf, float* flcoeffs1,
265 //maybe some frequency division thingy
267 flcoeffs1[0] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
268 flcoeffs2[0] = log(flcoeffs1[0])/log(2);
272 for(i = 1; i < BANDS; i++) {
273 level = levlCoeffBuf[i];
280 else if (level <= 24)
285 tmp *= imc_exp_tab[15 + level];
286 tmp2 += 0.83048 * level; // 0.83048 = log2(10) * 0.25
294 static void imc_decode_level_coefficients2(IMCContext* q, int* levlCoeffBuf, float* old_floor, float* flcoeffs1,
297 //FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors
298 // and flcoeffs2 old scale factors
299 // might be incomplete due to a missing table that is in the binary code
300 for(i = 0; i < BANDS; i++) {
302 if(levlCoeffBuf[i] < 16) {
303 flcoeffs1[i] = imc_exp_tab2[levlCoeffBuf[i]] * old_floor[i];
304 flcoeffs2[i] = (levlCoeffBuf[i]-7) * 0.83048 + flcoeffs2[i]; // 0.83048 = log2(10) * 0.25
306 flcoeffs1[i] = old_floor[i];
312 * Perform bit allocation depending on bits available
314 static int bit_allocation (IMCContext* q, int stream_format_code, int freebits, int flag) {
316 const float limit = -1.e20;
325 float lowest = 1.e10;
331 for(i = 0; i < BANDS; i++)
332 highest = FFMAX(highest, q->flcoeffs1[i]);
334 for(i = 0; i < BANDS-1; i++) {
335 q->flcoeffs4[i] = q->flcoeffs3[i] - log(q->flcoeffs5[i])/log(2);
337 q->flcoeffs4[BANDS - 1] = limit;
339 highest = highest * 0.25;
341 for(i = 0; i < BANDS; i++) {
343 if ((band_tab[i+1] - band_tab[i]) == q->bandWidthT[i])
346 if ((band_tab[i+1] - band_tab[i]) > q->bandWidthT[i])
349 if (((band_tab[i+1] - band_tab[i])/2) >= q->bandWidthT[i])
353 return AVERROR_INVALIDDATA;
355 q->flcoeffs4[i] = q->flcoeffs4[i] + xTab[(indx*2 + (q->flcoeffs1[i] < highest)) * 2 + flag];
358 if (stream_format_code & 0x2) {
359 q->flcoeffs4[0] = limit;
360 q->flcoeffs4[1] = limit;
361 q->flcoeffs4[2] = limit;
362 q->flcoeffs4[3] = limit;
365 for(i = (stream_format_code & 0x2)?4:0; i < BANDS-1; i++) {
366 iacc += q->bandWidthT[i];
367 summa += q->bandWidthT[i] * q->flcoeffs4[i];
369 q->bandWidthT[BANDS-1] = 0;
370 summa = (summa * 0.5 - freebits) / iacc;
373 for(i = 0; i < BANDS/2; i++) {
374 rres = summer - freebits;
375 if((rres >= -8) && (rres <= 8)) break;
380 for(j = (stream_format_code & 0x2)?4:0; j < BANDS; j++) {
381 cwlen = av_clipf(((q->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6);
383 q->bitsBandT[j] = cwlen;
384 summer += q->bandWidthT[j] * cwlen;
387 iacc += q->bandWidthT[j];
392 if (freebits < summer)
399 summa = (float)(summer - freebits) / ((t1 + 1) * iacc) + summa;
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 q->CWlengthT[j] = q->bitsBandT[i];
407 if (freebits > summer) {
408 for(i = 0; i < BANDS; i++) {
409 workT[i] = (q->bitsBandT[i] == 6) ? -1.e20 : (q->bitsBandT[i] * -2 + q->flcoeffs4[i] - 0.415);
415 if (highest <= -1.e20)
421 for(i = 0; i < BANDS; i++) {
422 if (workT[i] > highest) {
428 if (highest > -1.e20) {
429 workT[found_indx] -= 2.0;
430 if (++(q->bitsBandT[found_indx]) == 6)
431 workT[found_indx] = -1.e20;
433 for(j = band_tab[found_indx]; j < band_tab[found_indx+1] && (freebits > summer); j++){
438 }while (freebits > summer);
440 if (freebits < summer) {
441 for(i = 0; i < BANDS; i++) {
442 workT[i] = q->bitsBandT[i] ? (q->bitsBandT[i] * -2 + q->flcoeffs4[i] + 1.585) : 1.e20;
444 if (stream_format_code & 0x2) {
450 while (freebits < summer){
453 for(i = 0; i < BANDS; i++) {
454 if (workT[i] < lowest) {
459 //if(lowest >= 1.e10) break;
460 workT[low_indx] = lowest + 2.0;
462 if (!(--q->bitsBandT[low_indx]))
463 workT[low_indx] = 1.e20;
465 for(j = band_tab[low_indx]; j < band_tab[low_indx+1] && (freebits < summer); j++){
466 if(q->CWlengthT[j] > 0){
476 static void imc_get_skip_coeff(IMCContext* q) {
479 memset(q->skipFlagBits, 0, sizeof(q->skipFlagBits));
480 memset(q->skipFlagCount, 0, sizeof(q->skipFlagCount));
481 for(i = 0; i < BANDS; i++) {
482 if (!q->bandFlagsBuf[i] || !q->bandWidthT[i])
485 if (!q->skipFlagRaw[i]) {
486 q->skipFlagBits[i] = band_tab[i+1] - band_tab[i];
488 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
489 if ((q->skipFlags[j] = get_bits1(&q->gb)))
490 q->skipFlagCount[i]++;
493 for(j = band_tab[i]; j < (band_tab[i+1]-1); j += 2) {
494 if(!get_bits1(&q->gb)){//0
495 q->skipFlagBits[i]++;
498 q->skipFlagCount[i] += 2;
500 if(get_bits1(&q->gb)){//11
501 q->skipFlagBits[i] +=2;
504 q->skipFlagCount[i]++;
506 q->skipFlagBits[i] +=3;
508 if(!get_bits1(&q->gb)){//100
510 q->skipFlagCount[i]++;
518 if (j < band_tab[i+1]) {
519 q->skipFlagBits[i]++;
520 if ((q->skipFlags[j] = get_bits1(&q->gb)))
521 q->skipFlagCount[i]++;
528 * Increase highest' band coefficient sizes as some bits won't be used
530 static void imc_adjust_bit_allocation (IMCContext* q, int summer) {
537 for(i = 0; i < BANDS; i++) {
538 workT[i] = (q->bitsBandT[i] == 6) ? -1.e20 : (q->bitsBandT[i] * -2 + q->flcoeffs4[i] - 0.415);
541 while (corrected < summer) {
542 if(highest <= -1.e20)
547 for(i = 0; i < BANDS; i++) {
548 if (workT[i] > highest) {
554 if (highest > -1.e20) {
555 workT[found_indx] -= 2.0;
556 if (++(q->bitsBandT[found_indx]) == 6)
557 workT[found_indx] = -1.e20;
559 for(j = band_tab[found_indx]; j < band_tab[found_indx+1] && (corrected < summer); j++) {
560 if (!q->skipFlags[j] && (q->CWlengthT[j] < 6)) {
569 static void imc_imdct256(IMCContext *q) {
574 for(i=0; i < COEFFS/2; i++){
575 q->samples[i].re = -(q->pre_coef1[i] * q->CWdecoded[COEFFS-1-i*2]) -
576 (q->pre_coef2[i] * q->CWdecoded[i*2]);
577 q->samples[i].im = (q->pre_coef2[i] * q->CWdecoded[COEFFS-1-i*2]) -
578 (q->pre_coef1[i] * q->CWdecoded[i*2]);
582 q->fft.fft_permute(&q->fft, q->samples);
583 q->fft.fft_calc (&q->fft, q->samples);
585 /* postrotation, window and reorder */
586 for(i = 0; i < COEFFS/2; i++){
587 re = (q->samples[i].re * q->post_cos[i]) + (-q->samples[i].im * q->post_sin[i]);
588 im = (-q->samples[i].im * q->post_cos[i]) - (q->samples[i].re * q->post_sin[i]);
589 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);
590 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);
591 q->last_fft_im[i] = im;
595 static int inverse_quant_coeff (IMCContext* q, int stream_format_code) {
597 int middle_value, cw_len, max_size;
598 const float* quantizer;
600 for(i = 0; i < BANDS; i++) {
601 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
603 cw_len = q->CWlengthT[j];
605 if (cw_len <= 0 || q->skipFlags[j])
608 max_size = 1 << cw_len;
609 middle_value = max_size >> 1;
611 if (q->codewords[j] >= max_size || q->codewords[j] < 0)
612 return AVERROR_INVALIDDATA;
615 quantizer = imc_quantizer2[(stream_format_code & 2) >> 1];
616 if (q->codewords[j] >= middle_value)
617 q->CWdecoded[j] = quantizer[q->codewords[j] - 8] * q->flcoeffs6[i];
619 q->CWdecoded[j] = -quantizer[max_size - q->codewords[j] - 8 - 1] * q->flcoeffs6[i];
621 quantizer = imc_quantizer1[((stream_format_code & 2) >> 1) | (q->bandFlagsBuf[i] << 1)];
622 if (q->codewords[j] >= middle_value)
623 q->CWdecoded[j] = quantizer[q->codewords[j] - 1] * q->flcoeffs6[i];
625 q->CWdecoded[j] = -quantizer[max_size - 2 - q->codewords[j]] * q->flcoeffs6[i];
633 static int imc_get_coeffs (IMCContext* q) {
634 int i, j, cw_len, cw;
636 for(i = 0; i < BANDS; i++) {
637 if(!q->sumLenArr[i]) continue;
638 if (q->bandFlagsBuf[i] || q->bandWidthT[i]) {
639 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
640 cw_len = q->CWlengthT[j];
643 if (get_bits_count(&q->gb) + cw_len > 512){
644 //av_log(NULL,0,"Band %i coeff %i cw_len %i\n",i,j,cw_len);
645 return AVERROR_INVALIDDATA;
648 if(cw_len && (!q->bandFlagsBuf[i] || !q->skipFlags[j]))
649 cw = get_bits(&q->gb, cw_len);
651 q->codewords[j] = cw;
658 static int imc_decode_frame(AVCodecContext * avctx, void *data,
659 int *got_frame_ptr, AVPacket *avpkt)
661 const uint8_t *buf = avpkt->data;
662 int buf_size = avpkt->size;
664 IMCContext *q = avctx->priv_data;
666 int stream_format_code;
667 int imc_hdr, i, j, ret;
670 int counter, bitscount;
671 LOCAL_ALIGNED_16(uint16_t, buf16, [IMC_BLOCK_SIZE / 2]);
673 if (buf_size < IMC_BLOCK_SIZE) {
674 av_log(avctx, AV_LOG_ERROR, "imc frame too small!\n");
675 return AVERROR_INVALIDDATA;
678 /* get output buffer */
679 q->frame.nb_samples = COEFFS;
680 if ((ret = avctx->get_buffer(avctx, &q->frame)) < 0) {
681 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
684 q->out_samples = (float *)q->frame.data[0];
686 q->dsp.bswap16_buf(buf16, (const uint16_t*)buf, IMC_BLOCK_SIZE / 2);
688 init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8);
690 /* Check the frame header */
691 imc_hdr = get_bits(&q->gb, 9);
692 if (imc_hdr != IMC_FRAME_ID) {
693 av_log(avctx, AV_LOG_ERROR, "imc frame header check failed!\n");
694 av_log(avctx, AV_LOG_ERROR, "got %x instead of 0x21.\n", imc_hdr);
695 return AVERROR_INVALIDDATA;
697 stream_format_code = get_bits(&q->gb, 3);
699 if(stream_format_code & 1){
700 av_log(avctx, AV_LOG_ERROR, "Stream code format %X is not supported\n", stream_format_code);
701 return AVERROR_INVALIDDATA;
704 // av_log(avctx, AV_LOG_DEBUG, "stream_format_code = %d\n", stream_format_code);
706 if (stream_format_code & 0x04)
707 q->decoder_reset = 1;
709 if(q->decoder_reset) {
710 memset(q->out_samples, 0, sizeof(q->out_samples));
711 for(i = 0; i < BANDS; i++)q->old_floor[i] = 1.0;
712 for(i = 0; i < COEFFS; i++)q->CWdecoded[i] = 0;
713 q->decoder_reset = 0;
716 flag = get_bits1(&q->gb);
717 imc_read_level_coeffs(q, stream_format_code, q->levlCoeffBuf);
719 if (stream_format_code & 0x4)
720 imc_decode_level_coefficients(q, q->levlCoeffBuf, q->flcoeffs1, q->flcoeffs2);
722 imc_decode_level_coefficients2(q, q->levlCoeffBuf, q->old_floor, q->flcoeffs1, q->flcoeffs2);
724 memcpy(q->old_floor, q->flcoeffs1, 32 * sizeof(float));
727 for (i=0 ; i<BANDS ; i++) {
728 if (q->levlCoeffBuf[i] == 16) {
729 q->bandWidthT[i] = 0;
732 q->bandWidthT[i] = band_tab[i+1] - band_tab[i];
734 memset(q->bandFlagsBuf, 0, BANDS * sizeof(int));
735 for(i = 0; i < BANDS-1; i++) {
736 if (q->bandWidthT[i])
737 q->bandFlagsBuf[i] = get_bits1(&q->gb);
740 imc_calculate_coeffs(q, q->flcoeffs1, q->flcoeffs2, q->bandWidthT, q->flcoeffs3, q->flcoeffs5);
743 /* first 4 bands will be assigned 5 bits per coefficient */
744 if (stream_format_code & 0x2) {
751 for(i = 1; i < 4; i++){
752 bits = (q->levlCoeffBuf[i] == 16) ? 0 : 5;
753 q->bitsBandT[i] = bits;
754 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
755 q->CWlengthT[j] = bits;
761 if((ret = bit_allocation (q, stream_format_code,
762 512 - bitscount - get_bits_count(&q->gb), flag)) < 0) {
763 av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
764 q->decoder_reset = 1;
768 for(i = 0; i < BANDS; i++) {
770 q->skipFlagRaw[i] = 0;
771 for(j = band_tab[i]; j < band_tab[i+1]; j++)
772 q->sumLenArr[i] += q->CWlengthT[j];
773 if (q->bandFlagsBuf[i])
774 if( (((band_tab[i+1] - band_tab[i]) * 1.5) > q->sumLenArr[i]) && (q->sumLenArr[i] > 0))
775 q->skipFlagRaw[i] = 1;
778 imc_get_skip_coeff(q);
780 for(i = 0; i < BANDS; i++) {
781 q->flcoeffs6[i] = q->flcoeffs1[i];
782 /* band has flag set and at least one coded coefficient */
783 if (q->bandFlagsBuf[i] && (band_tab[i+1] - band_tab[i]) != q->skipFlagCount[i]){
784 q->flcoeffs6[i] *= q->sqrt_tab[band_tab[i+1] - band_tab[i]] /
785 q->sqrt_tab[(band_tab[i+1] - band_tab[i] - q->skipFlagCount[i])];
789 /* calculate bits left, bits needed and adjust bit allocation */
792 for(i = 0; i < BANDS; i++) {
793 if (q->bandFlagsBuf[i]) {
794 for(j = band_tab[i]; j < band_tab[i+1]; j++) {
795 if(q->skipFlags[j]) {
796 summer += q->CWlengthT[j];
800 bits += q->skipFlagBits[i];
801 summer -= q->skipFlagBits[i];
804 imc_adjust_bit_allocation(q, summer);
806 for(i = 0; i < BANDS; i++) {
809 for(j = band_tab[i]; j < band_tab[i+1]; j++)
810 if (!q->skipFlags[j])
811 q->sumLenArr[i] += q->CWlengthT[j];
814 memset(q->codewords, 0, sizeof(q->codewords));
816 if(imc_get_coeffs(q) < 0) {
817 av_log(avctx, AV_LOG_ERROR, "Read coefficients failed\n");
818 q->decoder_reset = 1;
819 return AVERROR_INVALIDDATA;
822 if(inverse_quant_coeff(q, stream_format_code) < 0) {
823 av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n");
824 q->decoder_reset = 1;
825 return AVERROR_INVALIDDATA;
828 memset(q->skipFlags, 0, sizeof(q->skipFlags));
833 *(AVFrame *)data = q->frame;
835 return IMC_BLOCK_SIZE;
839 static av_cold int imc_decode_close(AVCodecContext * avctx)
841 IMCContext *q = avctx->priv_data;
849 AVCodec ff_imc_decoder = {
851 .type = AVMEDIA_TYPE_AUDIO,
853 .priv_data_size = sizeof(IMCContext),
854 .init = imc_decode_init,
855 .close = imc_decode_close,
856 .decode = imc_decode_frame,
857 .capabilities = CODEC_CAP_DR1,
858 .long_name = NULL_IF_CONFIG_SMALL("IMC (Intel Music Coder)"),