]> git.sesse.net Git - ffmpeg/blob - libavcodec/imc.c
avcodec: Don't anonymously typedef structs
[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 Libav.
8  *
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.
13  *
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.
18  *
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
22  */
23
24 /**
25  *  @file
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.
30  *
31  */
32
33
34 #include <math.h>
35 #include <stddef.h>
36 #include <stdio.h>
37
38 #include "libavutil/channel_layout.h"
39 #include "libavutil/float_dsp.h"
40 #include "libavutil/internal.h"
41 #include "avcodec.h"
42 #include "bswapdsp.h"
43 #include "get_bits.h"
44 #include "fft.h"
45 #include "internal.h"
46 #include "sinewin.h"
47
48 #include "imcdata.h"
49
50 #define IMC_BLOCK_SIZE 64
51 #define IMC_FRAME_ID 0x21
52 #define BANDS 32
53 #define COEFFS 256
54
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];
64
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 coeffients per band
74     int skipFlags[COEFFS];     ///< skip coefficient decoding or not
75     int codewords[COEFFS];     ///< raw codewords read from bitstream
76
77     float last_fft_im[COEFFS];
78
79     int decoder_reset;
80 } IMCChannel;
81
82 typedef struct IMCContext {
83     IMCChannel chctx[2];
84
85     /** MDCT tables */
86     //@{
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];
92     //@}
93
94     float sqrt_tab[30];
95     GetBitContext gb;
96
97     BswapDSPContext bdsp;
98     AVFloatDSPContext fdsp;
99     FFTContext fft;
100     DECLARE_ALIGNED(32, FFTComplex, samples)[COEFFS / 2];
101     float *out_samples;
102
103     int coef0_pos;
104
105     int8_t cyclTab[32], cyclTab2[32];
106     float  weights1[31], weights2[31];
107 } IMCContext;
108
109 static VLC huffman_vlc[4][4];
110
111 #define VLC_TABLES_SIZE 9512
112
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
116 };
117
118 static VLC_TYPE vlc_tables[VLC_TABLES_SIZE][2];
119
120 static inline double freq2bark(double freq)
121 {
122     return 3.5 * atan((freq / 7500.0) * (freq / 7500.0)) + 13.0 * atan(freq * 0.00076);
123 }
124
125 static av_cold void iac_generate_tabs(IMCContext *q, int sampling_rate)
126 {
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;
131     int i, j;
132
133     for (i = 0; i < 32; i++) {
134         freq = (band_tab[i] + band_tab[i + 1] - 1) * scale;
135         bark = freq2bark(freq);
136
137         if (i > 0) {
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);
141         }
142         prev_bark = bark;
143
144         freqmid[i] = freq;
145
146         tf = freq;
147         while (tf < nyquist_freq) {
148             tf += 0.5;
149             tb =  freq2bark(tf);
150             if (tb > bark + 0.5)
151                 break;
152         }
153         freqmax[i] = tf;
154
155         tf = freq;
156         while (tf > 0.0) {
157             tf -= 0.5;
158             tb =  freq2bark(tf);
159             if (tb <= bark - 0.5)
160                 break;
161         }
162         freqmin[i] = tf;
163     }
164
165     for (i = 0; i < 32; i++) {
166         freq = freqmax[i];
167         for (j = 31; j > 0 && freq <= freqmid[j]; j--);
168         q->cyclTab[i] = j + 1;
169
170         freq = freqmin[i];
171         for (j = 0; j < 32 && freq >= freqmid[j]; j++);
172         q->cyclTab2[i] = j - 1;
173     }
174 }
175
176 static av_cold int imc_decode_init(AVCodecContext *avctx)
177 {
178     int i, j, ret;
179     IMCContext *q = avctx->priv_data;
180     double r1, r2;
181
182     if (avctx->codec_id == AV_CODEC_ID_IMC)
183         avctx->channels = 1;
184
185     if (avctx->channels > 2) {
186         avpriv_request_sample(avctx, "Number of channels > 2");
187         return AVERROR_PATCHWELCOME;
188     }
189
190     for (j = 0; j < avctx->channels; j++) {
191         q->chctx[j].decoder_reset = 1;
192
193         for (i = 0; i < BANDS; i++)
194             q->chctx[j].old_floor[i] = 1.0;
195
196         for (i = 0; i < COEFFS / 2; i++)
197             q->chctx[j].last_fft_im[i] = 0;
198     }
199
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);
207
208         r1 = sin((i * 4.0 + 1.0) / 1024.0 * M_PI);
209         r2 = cos((i * 4.0 + 1.0) / 1024.0 * M_PI);
210
211         if (i & 0x1) {
212             q->pre_coef1[i] =  (r1 + r2) * sqrt(2.0);
213             q->pre_coef2[i] = -(r1 - r2) * sqrt(2.0);
214         } else {
215             q->pre_coef1[i] = -(r1 + r2) * sqrt(2.0);
216             q->pre_coef2[i] =  (r1 - r2) * sqrt(2.0);
217         }
218     }
219
220     /* Generate a square root table */
221
222     for (i = 0; i < 30; i++)
223         q->sqrt_tab[i] = sqrt(i);
224
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);
233         }
234     }
235
236     if (avctx->codec_id == AV_CODEC_ID_IAC) {
237         iac_generate_tabs(q, avctx->sample_rate);
238     } else {
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));
243     }
244
245     if ((ret = ff_fft_init(&q->fft, 7, 1))) {
246         av_log(avctx, AV_LOG_INFO, "FFT init failed\n");
247         return ret;
248     }
249     ff_bswapdsp_init(&q->bdsp);
250     avpriv_float_dsp_init(&q->fdsp, avctx->flags & 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;
254
255     return 0;
256 }
257
258 static void imc_calculate_coeffs(IMCContext *q, float *flcoeffs1,
259                                  float *flcoeffs2, int *bandWidthT,
260                                  float *flcoeffs3, float *flcoeffs5)
261 {
262     float   workT1[BANDS];
263     float   workT2[BANDS];
264     float   workT3[BANDS];
265     float   snr_limit = 1.e-30;
266     float   accum = 0.0;
267     int i, cnt2;
268
269     for (i = 0; i < BANDS; i++) {
270         flcoeffs5[i] = workT2[i] = 0.0;
271         if (bandWidthT[i]) {
272             workT1[i] = flcoeffs1[i] * flcoeffs1[i];
273             flcoeffs3[i] = 2.0 * flcoeffs2[i];
274         } else {
275             workT1[i]    = 0.0;
276             flcoeffs3[i] = -30000.0;
277         }
278         workT3[i] = bandWidthT[i] * workT1[i] * 0.01;
279         if (workT3[i] <= snr_limit)
280             workT3[i] = 0.0;
281     }
282
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];
287     }
288
289     for (i = 1; i < BANDS; i++) {
290         accum = (workT2[i - 1] + accum) * q->weights1[i - 1];
291         flcoeffs5[i] += accum;
292     }
293
294     for (i = 0; i < BANDS; i++)
295         workT2[i] = 0.0;
296
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];
301     }
302
303     accum = 0.0;
304
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
309     }
310 }
311
312
313 static void imc_read_level_coeffs(IMCContext *q, int stream_format_code,
314                                   int *levlCoeffs)
315 {
316     int i;
317     VLC *hufftab[4];
318     int start = 0;
319     const uint8_t *cb_sel;
320     int s;
321
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];
328
329     if (stream_format_code & 4)
330         start = 1;
331     if (start)
332         levlCoeffs[0] = get_bits(&q->gb, 7);
333     for (i = start; i < BANDS; i++) {
334         levlCoeffs[i] = get_vlc2(&q->gb, hufftab[cb_sel[i]]->table,
335                                  hufftab[cb_sel[i]]->bits, 2);
336         if (levlCoeffs[i] == 17)
337             levlCoeffs[i] += get_bits(&q->gb, 4);
338     }
339 }
340
341 static void imc_read_level_coeffs_raw(IMCContext *q, int stream_format_code,
342                                       int *levlCoeffs)
343 {
344     int i;
345
346     q->coef0_pos  = get_bits(&q->gb, 5);
347     levlCoeffs[0] = get_bits(&q->gb, 7);
348     for (i = 1; i < BANDS; i++)
349         levlCoeffs[i] = get_bits(&q->gb, 4);
350 }
351
352 static void imc_decode_level_coefficients(IMCContext *q, int *levlCoeffBuf,
353                                           float *flcoeffs1, float *flcoeffs2)
354 {
355     int i, level;
356     float tmp, tmp2;
357     // maybe some frequency division thingy
358
359     flcoeffs1[0] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
360     flcoeffs2[0] = log2f(flcoeffs1[0]);
361     tmp  = flcoeffs1[0];
362     tmp2 = flcoeffs2[0];
363
364     for (i = 1; i < BANDS; i++) {
365         level = levlCoeffBuf[i];
366         if (level == 16) {
367             flcoeffs1[i] = 1.0;
368             flcoeffs2[i] = 0.0;
369         } else {
370             if (level < 17)
371                 level -= 7;
372             else if (level <= 24)
373                 level -= 32;
374             else
375                 level -= 16;
376
377             tmp  *= imc_exp_tab[15 + level];
378             tmp2 += 0.83048 * level;  // 0.83048 = log2(10) * 0.25
379             flcoeffs1[i] = tmp;
380             flcoeffs2[i] = tmp2;
381         }
382     }
383 }
384
385
386 static void imc_decode_level_coefficients2(IMCContext *q, int *levlCoeffBuf,
387                                            float *old_floor, float *flcoeffs1,
388                                            float *flcoeffs2)
389 {
390     int i;
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
394      */
395     for (i = 0; i < BANDS; i++) {
396         flcoeffs1[i] = 0;
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
400         } else {
401             flcoeffs1[i] = old_floor[i];
402         }
403     }
404 }
405
406 static void imc_decode_level_coefficients_raw(IMCContext *q, int *levlCoeffBuf,
407                                               float *flcoeffs1, float *flcoeffs2)
408 {
409     int i, level, pos;
410     float tmp, tmp2;
411
412     pos = q->coef0_pos;
413     flcoeffs1[pos] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
414     flcoeffs2[pos] = log2f(flcoeffs1[0]);
415     tmp  = flcoeffs1[pos];
416     tmp2 = flcoeffs2[pos];
417
418     levlCoeffBuf++;
419     for (i = 0; i < BANDS; i++) {
420         if (i == pos)
421             continue;
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
425     }
426 }
427
428 /**
429  * Perform bit allocation depending on bits available
430  */
431 static int bit_allocation(IMCContext *q, IMCChannel *chctx,
432                           int stream_format_code, int freebits, int flag)
433 {
434     int i, j;
435     const float limit = -1.e20;
436     float highest = 0.0;
437     int indx;
438     int t1 = 0;
439     int t2 = 1;
440     float summa = 0.0;
441     int iacc = 0;
442     int summer = 0;
443     int rres, cwlen;
444     float lowest = 1.e10;
445     int low_indx = 0;
446     float workT[32];
447     int flg;
448     int found_indx = 0;
449
450     for (i = 0; i < BANDS; i++)
451         highest = FFMAX(highest, chctx->flcoeffs1[i]);
452
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;
456
457     highest = highest * 0.25;
458
459     for (i = 0; i < BANDS; i++) {
460         indx = -1;
461         if ((band_tab[i + 1] - band_tab[i]) == chctx->bandWidthT[i])
462             indx = 0;
463
464         if ((band_tab[i + 1] - band_tab[i]) > chctx->bandWidthT[i])
465             indx = 1;
466
467         if (((band_tab[i + 1] - band_tab[i]) / 2) >= chctx->bandWidthT[i])
468             indx = 2;
469
470         if (indx == -1)
471             return AVERROR_INVALIDDATA;
472
473         chctx->flcoeffs4[i] += xTab[(indx * 2 + (chctx->flcoeffs1[i] < highest)) * 2 + flag];
474     }
475
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;
481     }
482
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];
486     }
487
488     if (!iacc)
489         return AVERROR_INVALIDDATA;
490
491     chctx->bandWidthT[BANDS - 1] = 0;
492     summa = (summa * 0.5 - freebits) / iacc;
493
494
495     for (i = 0; i < BANDS / 2; i++) {
496         rres = summer - freebits;
497         if ((rres >= -8) && (rres <= 8))
498             break;
499
500         summer = 0;
501         iacc   = 0;
502
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);
505
506             chctx->bitsBandT[j] = cwlen;
507             summer += chctx->bandWidthT[j] * cwlen;
508
509             if (cwlen > 0)
510                 iacc += chctx->bandWidthT[j];
511         }
512
513         flg = t2;
514         t2 = 1;
515         if (freebits < summer)
516             t2 = -1;
517         if (i == 0)
518             flg = t2;
519         if (flg != t2)
520             t1++;
521
522         summa = (float)(summer - freebits) / ((t1 + 1) * iacc) + summa;
523     }
524
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];
528     }
529
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);
534         }
535
536         highest = 0.0;
537
538         do {
539             if (highest <= -1.e20)
540                 break;
541
542             found_indx = 0;
543             highest = -1.e20;
544
545             for (i = 0; i < BANDS; i++) {
546                 if (workT[i] > highest) {
547                     highest = workT[i];
548                     found_indx = i;
549                 }
550             }
551
552             if (highest > -1.e20) {
553                 workT[found_indx] -= 2.0;
554                 if (++chctx->bitsBandT[found_indx] == 6)
555                     workT[found_indx] = -1.e20;
556
557                 for (j = band_tab[found_indx]; j < band_tab[found_indx + 1] && (freebits > summer); j++) {
558                     chctx->CWlengthT[j]++;
559                     summer++;
560                 }
561             }
562         } while (freebits > summer);
563     }
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)
567                                        : 1.e20;
568         }
569         if (stream_format_code & 0x2) {
570             workT[0] = 1.e20;
571             workT[1] = 1.e20;
572             workT[2] = 1.e20;
573             workT[3] = 1.e20;
574         }
575         while (freebits < summer) {
576             lowest   = 1.e10;
577             low_indx = 0;
578             for (i = 0; i < BANDS; i++) {
579                 if (workT[i] < lowest) {
580                     lowest   = workT[i];
581                     low_indx = i;
582                 }
583             }
584             // if (lowest >= 1.e10)
585             //     break;
586             workT[low_indx] = lowest + 2.0;
587
588             if (!--chctx->bitsBandT[low_indx])
589                 workT[low_indx] = 1.e20;
590
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]--;
594                     summer--;
595                 }
596             }
597         }
598     }
599     return 0;
600 }
601
602 static void imc_get_skip_coeff(IMCContext *q, IMCChannel *chctx)
603 {
604     int i, j;
605
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])
610             continue;
611
612         if (!chctx->skipFlagRaw[i]) {
613             chctx->skipFlagBits[i] = band_tab[i + 1] - band_tab[i];
614
615             for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
616                 chctx->skipFlags[j] = get_bits1(&q->gb);
617                 if (chctx->skipFlags[j])
618                     chctx->skipFlagCount[i]++;
619             }
620         } else {
621             for (j = band_tab[i]; j < band_tab[i + 1] - 1; j += 2) {
622                 if (!get_bits1(&q->gb)) { // 0
623                     chctx->skipFlagBits[i]++;
624                     chctx->skipFlags[j]      = 1;
625                     chctx->skipFlags[j + 1]  = 1;
626                     chctx->skipFlagCount[i] += 2;
627                 } else {
628                     if (get_bits1(&q->gb)) { // 11
629                         chctx->skipFlagBits[i] += 2;
630                         chctx->skipFlags[j]     = 0;
631                         chctx->skipFlags[j + 1] = 1;
632                         chctx->skipFlagCount[i]++;
633                     } else {
634                         chctx->skipFlagBits[i] += 3;
635                         chctx->skipFlags[j + 1] = 0;
636                         if (!get_bits1(&q->gb)) { // 100
637                             chctx->skipFlags[j] = 1;
638                             chctx->skipFlagCount[i]++;
639                         } else { // 101
640                             chctx->skipFlags[j] = 0;
641                         }
642                     }
643                 }
644             }
645
646             if (j < band_tab[i + 1]) {
647                 chctx->skipFlagBits[i]++;
648                 if ((chctx->skipFlags[j] = get_bits1(&q->gb)))
649                     chctx->skipFlagCount[i]++;
650             }
651         }
652     }
653 }
654
655 /**
656  * Increase highest' band coefficient sizes as some bits won't be used
657  */
658 static void imc_adjust_bit_allocation(IMCContext *q, IMCChannel *chctx,
659                                       int summer)
660 {
661     float workT[32];
662     int corrected = 0;
663     int i, j;
664     float highest  = 0;
665     int found_indx = 0;
666
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);
670     }
671
672     while (corrected < summer) {
673         if (highest <= -1.e20)
674             break;
675
676         highest = -1.e20;
677
678         for (i = 0; i < BANDS; i++) {
679             if (workT[i] > highest) {
680                 highest = workT[i];
681                 found_indx = i;
682             }
683         }
684
685         if (highest > -1.e20) {
686             workT[found_indx] -= 2.0;
687             if (++(chctx->bitsBandT[found_indx]) == 6)
688                 workT[found_indx] = -1.e20;
689
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]++;
693                     corrected++;
694                 }
695             }
696         }
697     }
698 }
699
700 static void imc_imdct256(IMCContext *q, IMCChannel *chctx, int channels)
701 {
702     int i;
703     float re, im;
704     float *dst1 = q->out_samples;
705     float *dst2 = q->out_samples + (COEFFS - 1);
706
707     /* prerotation */
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]);
713     }
714
715     /* FFT */
716     q->fft.fft_permute(&q->fft, q->samples);
717     q->fft.fft_calc(&q->fft, q->samples);
718
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);
727         dst1 += 2;
728         dst2 -= 2;
729         chctx->last_fft_im[i] = im;
730     }
731 }
732
733 static int inverse_quant_coeff(IMCContext *q, IMCChannel *chctx,
734                                int stream_format_code)
735 {
736     int i, j;
737     int middle_value, cw_len, max_size;
738     const float *quantizer;
739
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];
744
745             if (cw_len <= 0 || chctx->skipFlags[j])
746                 continue;
747
748             max_size     = 1 << cw_len;
749             middle_value = max_size >> 1;
750
751             if (chctx->codewords[j] >= max_size || chctx->codewords[j] < 0)
752                 return AVERROR_INVALIDDATA;
753
754             if (cw_len >= 4) {
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];
758                 else
759                     chctx->CWdecoded[j] = -quantizer[max_size - chctx->codewords[j] - 8 - 1] * chctx->flcoeffs6[i];
760             }else{
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];
764                 else
765                     chctx->CWdecoded[j] = -quantizer[max_size - 2 - chctx->codewords[j]] * chctx->flcoeffs6[i];
766             }
767         }
768     }
769     return 0;
770 }
771
772
773 static int imc_get_coeffs(IMCContext *q, IMCChannel *chctx)
774 {
775     int i, j, cw_len, cw;
776
777     for (i = 0; i < BANDS; i++) {
778         if (!chctx->sumLenArr[i])
779             continue;
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];
783                 cw = 0;
784
785                 if (get_bits_count(&q->gb) + cw_len > 512) {
786                     av_dlog(NULL, "Band %i coeff %i cw_len %i\n", i, j, cw_len);
787                     return AVERROR_INVALIDDATA;
788                 }
789
790                 if (cw_len && (!chctx->bandFlagsBuf[i] || !chctx->skipFlags[j]))
791                     cw = get_bits(&q->gb, cw_len);
792
793                 chctx->codewords[j] = cw;
794             }
795         }
796     }
797     return 0;
798 }
799
800 static void imc_refine_bit_allocation(IMCContext *q, IMCChannel *chctx)
801 {
802     int i, j;
803     int bits, summer;
804
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;
813     }
814
815     imc_get_skip_coeff(q, chctx);
816
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])];
823         }
824     }
825
826     /* calculate bits left, bits needed and adjust bit allocation */
827     bits = summer = 0;
828
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;
835                 }
836             }
837             bits   += chctx->skipFlagBits[i];
838             summer -= chctx->skipFlagBits[i];
839         }
840     }
841     imc_adjust_bit_allocation(q, chctx, summer);
842 }
843
844 static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch)
845 {
846     int stream_format_code;
847     int imc_hdr, i, j, ret;
848     int flag;
849     int bits;
850     int counter, bitscount;
851     IMCChannel *chctx = q->chctx + ch;
852
853
854     /* Check the frame header */
855     imc_hdr = get_bits(&q->gb, 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;
860     }
861     stream_format_code = get_bits(&q->gb, 3);
862
863     if (stream_format_code & 0x04)
864         chctx->decoder_reset = 1;
865
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;
872     }
873
874     flag = get_bits1(&q->gb);
875     if (stream_format_code & 0x1)
876         imc_read_level_coeffs_raw(q, stream_format_code, chctx->levlCoeffBuf);
877     else
878         imc_read_level_coeffs(q, stream_format_code, chctx->levlCoeffBuf);
879
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);
886     else
887         imc_decode_level_coefficients2(q, chctx->levlCoeffBuf, chctx->old_floor,
888                                        chctx->flcoeffs1, chctx->flcoeffs2);
889
890     memcpy(chctx->old_floor, chctx->flcoeffs1, 32 * sizeof(float));
891
892     counter = 0;
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;
899         }
900     } else {
901         for (i = 0; i < BANDS; i++) {
902             if (chctx->levlCoeffBuf[i] == 16) {
903                 chctx->bandWidthT[i] = 0;
904                 counter++;
905             } else
906                 chctx->bandWidthT[i] = band_tab[i + 1] - band_tab[i];
907         }
908
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] = get_bits1(&q->gb);
913
914         imc_calculate_coeffs(q, chctx->flcoeffs1, chctx->flcoeffs2,
915                              chctx->bandWidthT, chctx->flcoeffs3,
916                              chctx->flcoeffs5);
917     }
918
919     bitscount = 0;
920     /* first 4 bands will be assigned 5 bits per coefficient */
921     if (stream_format_code & 0x2) {
922         bitscount += 15;
923
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)
930                 bits = 5;
931             else
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;
936                 bitscount      += bits;
937             }
938         }
939     }
940     if (avctx->codec_id == AV_CODEC_ID_IAC) {
941         bitscount += !!chctx->bandWidthT[BANDS - 1];
942         if (!(stream_format_code & 0x2))
943             bitscount += 16;
944     }
945
946     if ((ret = bit_allocation(q, chctx, stream_format_code,
947                               512 - bitscount - get_bits_count(&q->gb),
948                               flag)) < 0) {
949         av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
950         chctx->decoder_reset = 1;
951         return ret;
952     }
953
954     if (stream_format_code & 0x1) {
955         for (i = 0; i < BANDS; i++)
956             chctx->skipFlags[i] = 0;
957     } else {
958         imc_refine_bit_allocation(q, chctx);
959     }
960
961     for (i = 0; i < BANDS; i++) {
962         chctx->sumLenArr[i] = 0;
963
964         for (j = band_tab[i]; j < band_tab[i + 1]; j++)
965             if (!chctx->skipFlags[j])
966                 chctx->sumLenArr[i] += chctx->CWlengthT[j];
967     }
968
969     memset(chctx->codewords, 0, sizeof(chctx->codewords));
970
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;
975     }
976
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;
981     }
982
983     memset(chctx->skipFlags, 0, sizeof(chctx->skipFlags));
984
985     imc_imdct256(q, chctx, avctx->channels);
986
987     return 0;
988 }
989
990 static int imc_decode_frame(AVCodecContext *avctx, void *data,
991                             int *got_frame_ptr, AVPacket *avpkt)
992 {
993     AVFrame *frame     = data;
994     const uint8_t *buf = avpkt->data;
995     int buf_size = avpkt->size;
996     int ret, i;
997
998     IMCContext *q = avctx->priv_data;
999
1000     LOCAL_ALIGNED_16(uint16_t, buf16, [IMC_BLOCK_SIZE / 2]);
1001
1002     if (buf_size < IMC_BLOCK_SIZE * avctx->channels) {
1003         av_log(avctx, AV_LOG_ERROR, "frame too small!\n");
1004         return AVERROR_INVALIDDATA;
1005     }
1006
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");
1011         return ret;
1012     }
1013
1014     for (i = 0; i < avctx->channels; i++) {
1015         q->out_samples = (float *)frame->extended_data[i];
1016
1017         q->bdsp.bswap16_buf(buf16, (const uint16_t *) buf, IMC_BLOCK_SIZE / 2);
1018
1019         init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8);
1020
1021         buf += IMC_BLOCK_SIZE;
1022
1023         if ((ret = imc_decode_block(avctx, q, i)) < 0)
1024             return ret;
1025     }
1026
1027     if (avctx->channels == 2) {
1028         q->fdsp.butterflies_float((float *)frame->extended_data[0],
1029                                   (float *)frame->extended_data[1], COEFFS);
1030     }
1031
1032     *got_frame_ptr = 1;
1033
1034     return IMC_BLOCK_SIZE * avctx->channels;
1035 }
1036
1037
1038 static av_cold int imc_decode_close(AVCodecContext * avctx)
1039 {
1040     IMCContext *q = avctx->priv_data;
1041
1042     ff_fft_end(&q->fft);
1043
1044     return 0;
1045 }
1046
1047
1048 AVCodec ff_imc_decoder = {
1049     .name           = "imc",
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   = CODEC_CAP_DR1,
1058     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1059                                                       AV_SAMPLE_FMT_NONE },
1060 };
1061
1062 AVCodec ff_iac_decoder = {
1063     .name           = "iac",
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   = CODEC_CAP_DR1,
1072     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1073                                                       AV_SAMPLE_FMT_NONE },
1074 };