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