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