xref: /third_party/ffmpeg/libavcodec/imc.c (revision cabdff1a)
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
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#include "config_components.h"
33
34#include <math.h>
35#include <stddef.h>
36#include <stdio.h>
37
38#include "libavutil/channel_layout.h"
39#include "libavutil/ffmath.h"
40#include "libavutil/float_dsp.h"
41#include "libavutil/internal.h"
42#include "libavutil/mem_internal.h"
43#include "libavutil/thread.h"
44
45#include "avcodec.h"
46#include "bswapdsp.h"
47#include "codec_internal.h"
48#include "get_bits.h"
49#include "fft.h"
50#include "internal.h"
51#include "sinewin.h"
52
53#include "imcdata.h"
54
55#define IMC_BLOCK_SIZE 64
56#define IMC_FRAME_ID 0x21
57#define BANDS 32
58#define COEFFS 256
59
60typedef struct IMCChannel {
61    float old_floor[BANDS];
62    float flcoeffs1[BANDS];
63    float flcoeffs2[BANDS];
64    float flcoeffs3[BANDS];
65    float flcoeffs4[BANDS];
66    float flcoeffs5[BANDS];
67    float flcoeffs6[BANDS];
68    float CWdecoded[COEFFS];
69
70    int bandWidthT[BANDS];     ///< codewords per band
71    int bitsBandT[BANDS];      ///< how many bits per codeword in band
72    int CWlengthT[COEFFS];     ///< how many bits in each codeword
73    int levlCoeffBuf[BANDS];
74    int bandFlagsBuf[BANDS];   ///< flags for each band
75    int sumLenArr[BANDS];      ///< bits for all coeffs in band
76    int skipFlagRaw[BANDS];    ///< skip flags are stored in raw form or not
77    int skipFlagBits[BANDS];   ///< bits used to code skip flags
78    int skipFlagCount[BANDS];  ///< skipped coefficients per band
79    int skipFlags[COEFFS];     ///< skip coefficient decoding or not
80    int codewords[COEFFS];     ///< raw codewords read from bitstream
81
82    float last_fft_im[COEFFS];
83
84    int decoder_reset;
85} IMCChannel;
86
87typedef struct IMCContext {
88    IMCChannel chctx[2];
89
90    /** MDCT tables */
91    //@{
92    float mdct_sine_window[COEFFS];
93    float post_cos[COEFFS];
94    float post_sin[COEFFS];
95    float pre_coef1[COEFFS];
96    float pre_coef2[COEFFS];
97    //@}
98
99    float sqrt_tab[30];
100    GetBitContext gb;
101
102    BswapDSPContext bdsp;
103    void (*butterflies_float)(float *av_restrict v1, float *av_restrict v2, int len);
104    FFTContext fft;
105    DECLARE_ALIGNED(32, FFTComplex, samples)[COEFFS / 2];
106    float *out_samples;
107
108    int coef0_pos;
109
110    int8_t cyclTab[32], cyclTab2[32];
111    float  weights1[31], weights2[31];
112
113    AVCodecContext *avctx;
114} IMCContext;
115
116static VLC huffman_vlc[4][4];
117
118#define IMC_VLC_BITS 9
119#define VLC_TABLES_SIZE 9512
120
121static VLCElem vlc_tables[VLC_TABLES_SIZE];
122
123static inline double freq2bark(double freq)
124{
125    return 3.5 * atan((freq / 7500.0) * (freq / 7500.0)) + 13.0 * atan(freq * 0.00076);
126}
127
128static av_cold void iac_generate_tabs(IMCContext *q, int sampling_rate)
129{
130    double freqmin[32], freqmid[32], freqmax[32];
131    double scale = sampling_rate / (256.0 * 2.0 * 2.0);
132    double nyquist_freq = sampling_rate * 0.5;
133    double freq, bark, prev_bark = 0, tf, tb;
134    int i, j;
135
136    for (i = 0; i < 32; i++) {
137        freq = (band_tab[i] + band_tab[i + 1] - 1) * scale;
138        bark = freq2bark(freq);
139
140        if (i > 0) {
141            tb = bark - prev_bark;
142            q->weights1[i - 1] = ff_exp10(-1.0 * tb);
143            q->weights2[i - 1] = ff_exp10(-2.7 * tb);
144        }
145        prev_bark = bark;
146
147        freqmid[i] = freq;
148
149        tf = freq;
150        while (tf < nyquist_freq) {
151            tf += 0.5;
152            tb =  freq2bark(tf);
153            if (tb > bark + 0.5)
154                break;
155        }
156        freqmax[i] = tf;
157
158        tf = freq;
159        while (tf > 0.0) {
160            tf -= 0.5;
161            tb =  freq2bark(tf);
162            if (tb <= bark - 0.5)
163                break;
164        }
165        freqmin[i] = tf;
166    }
167
168    for (i = 0; i < 32; i++) {
169        freq = freqmax[i];
170        for (j = 31; j > 0 && freq <= freqmid[j]; j--);
171        q->cyclTab[i] = j + 1;
172
173        freq = freqmin[i];
174        for (j = 0; j < 32 && freq >= freqmid[j]; j++);
175        q->cyclTab2[i] = j - 1;
176    }
177}
178
179static av_cold void imc_init_static(void)
180{
181    /* initialize the VLC tables */
182    for (int i = 0, offset = 0; i < 4 ; i++) {
183        for (int j = 0; j < 4; j++) {
184            huffman_vlc[i][j].table           = &vlc_tables[offset];
185            huffman_vlc[i][j].table_allocated = VLC_TABLES_SIZE - offset;
186            ff_init_vlc_from_lengths(&huffman_vlc[i][j], IMC_VLC_BITS, imc_huffman_sizes[i],
187                                     imc_huffman_lens[i][j], 1,
188                                     imc_huffman_syms[i][j], 1, 1,
189                                     0, INIT_VLC_STATIC_OVERLONG, NULL);
190            offset += huffman_vlc[i][j].table_size;
191        }
192    }
193}
194
195static av_cold int imc_decode_init(AVCodecContext *avctx)
196{
197    int i, j, ret;
198    IMCContext *q = avctx->priv_data;
199    static AVOnce init_static_once = AV_ONCE_INIT;
200    AVFloatDSPContext *fdsp;
201    double r1, r2;
202
203    if (avctx->codec_id == AV_CODEC_ID_IAC && avctx->sample_rate > 96000) {
204        av_log(avctx, AV_LOG_ERROR,
205               "Strange sample rate of %i, file likely corrupt or "
206               "needing a new table derivation method.\n",
207               avctx->sample_rate);
208        return AVERROR_PATCHWELCOME;
209    }
210
211    if (avctx->codec_id == AV_CODEC_ID_IMC) {
212        av_channel_layout_uninit(&avctx->ch_layout);
213        avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
214    }
215
216    if (avctx->ch_layout.nb_channels > 2) {
217        avpriv_request_sample(avctx, "Number of channels > 2");
218        return AVERROR_PATCHWELCOME;
219    }
220
221    for (j = 0; j < avctx->ch_layout.nb_channels; j++) {
222        q->chctx[j].decoder_reset = 1;
223
224        for (i = 0; i < BANDS; i++)
225            q->chctx[j].old_floor[i] = 1.0;
226
227        for (i = 0; i < COEFFS / 2; i++)
228            q->chctx[j].last_fft_im[i] = 0;
229    }
230
231    /* Build mdct window, a simple sine window normalized with sqrt(2) */
232    ff_sine_window_init(q->mdct_sine_window, COEFFS);
233    for (i = 0; i < COEFFS; i++)
234        q->mdct_sine_window[i] *= sqrt(2.0);
235    for (i = 0; i < COEFFS / 2; i++) {
236        q->post_cos[i] = (1.0f / 32768) * cos(i / 256.0 * M_PI);
237        q->post_sin[i] = (1.0f / 32768) * sin(i / 256.0 * M_PI);
238
239        r1 = sin((i * 4.0 + 1.0) / 1024.0 * M_PI);
240        r2 = cos((i * 4.0 + 1.0) / 1024.0 * M_PI);
241
242        if (i & 0x1) {
243            q->pre_coef1[i] =  (r1 + r2) * sqrt(2.0);
244            q->pre_coef2[i] = -(r1 - r2) * sqrt(2.0);
245        } else {
246            q->pre_coef1[i] = -(r1 + r2) * sqrt(2.0);
247            q->pre_coef2[i] =  (r1 - r2) * sqrt(2.0);
248        }
249    }
250
251    /* Generate a square root table */
252
253    for (i = 0; i < 30; i++)
254        q->sqrt_tab[i] = sqrt(i);
255
256    if (avctx->codec_id == AV_CODEC_ID_IAC) {
257        iac_generate_tabs(q, avctx->sample_rate);
258    } else {
259        memcpy(q->cyclTab,  cyclTab,  sizeof(cyclTab));
260        memcpy(q->cyclTab2, cyclTab2, sizeof(cyclTab2));
261        memcpy(q->weights1, imc_weights1, sizeof(imc_weights1));
262        memcpy(q->weights2, imc_weights2, sizeof(imc_weights2));
263    }
264
265    fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
266    if (!fdsp)
267        return AVERROR(ENOMEM);
268    q->butterflies_float = fdsp->butterflies_float;
269    av_free(fdsp);
270    if ((ret = ff_fft_init(&q->fft, 7, 1))) {
271        av_log(avctx, AV_LOG_INFO, "FFT init failed\n");
272        return ret;
273    }
274    ff_bswapdsp_init(&q->bdsp);
275
276    avctx->sample_fmt     = AV_SAMPLE_FMT_FLTP;
277
278    ff_thread_once(&init_static_once, imc_init_static);
279
280    return 0;
281}
282
283static void imc_calculate_coeffs(IMCContext *q, float *flcoeffs1,
284                                 float *flcoeffs2, int *bandWidthT,
285                                 float *flcoeffs3, float *flcoeffs5)
286{
287    float   workT1[BANDS];
288    float   workT2[BANDS];
289    float   workT3[BANDS];
290    float   snr_limit = 1.e-30;
291    float   accum = 0.0;
292    int i, cnt2;
293
294    for (i = 0; i < BANDS; i++) {
295        flcoeffs5[i] = workT2[i] = 0.0;
296        if (bandWidthT[i]) {
297            workT1[i] = flcoeffs1[i] * flcoeffs1[i];
298            flcoeffs3[i] = 2.0 * flcoeffs2[i];
299        } else {
300            workT1[i]    = 0.0;
301            flcoeffs3[i] = -30000.0;
302        }
303        workT3[i] = bandWidthT[i] * workT1[i] * 0.01;
304        if (workT3[i] <= snr_limit)
305            workT3[i] = 0.0;
306    }
307
308    for (i = 0; i < BANDS; i++) {
309        for (cnt2 = i; cnt2 < q->cyclTab[i]; cnt2++)
310            flcoeffs5[cnt2] = flcoeffs5[cnt2] + workT3[i];
311        workT2[cnt2 - 1] = workT2[cnt2 - 1] + workT3[i];
312    }
313
314    for (i = 1; i < BANDS; i++) {
315        accum = (workT2[i - 1] + accum) * q->weights1[i - 1];
316        flcoeffs5[i] += accum;
317    }
318
319    for (i = 0; i < BANDS; i++)
320        workT2[i] = 0.0;
321
322    for (i = 0; i < BANDS; i++) {
323        for (cnt2 = i - 1; cnt2 > q->cyclTab2[i]; cnt2--)
324            flcoeffs5[cnt2] += workT3[i];
325        workT2[cnt2+1] += workT3[i];
326    }
327
328    accum = 0.0;
329
330    for (i = BANDS-2; i >= 0; i--) {
331        accum = (workT2[i+1] + accum) * q->weights2[i];
332        flcoeffs5[i] += accum;
333        // there is missing code here, but it seems to never be triggered
334    }
335}
336
337
338static void imc_read_level_coeffs(IMCContext *q, int stream_format_code,
339                                  int *levlCoeffs)
340{
341    int i;
342    VLC *hufftab[4];
343    int start = 0;
344    const uint8_t *cb_sel;
345    int s;
346
347    s = stream_format_code >> 1;
348    hufftab[0] = &huffman_vlc[s][0];
349    hufftab[1] = &huffman_vlc[s][1];
350    hufftab[2] = &huffman_vlc[s][2];
351    hufftab[3] = &huffman_vlc[s][3];
352    cb_sel = imc_cb_select[s];
353
354    if (stream_format_code & 4)
355        start = 1;
356    if (start)
357        levlCoeffs[0] = get_bits(&q->gb, 7);
358    for (i = start; i < BANDS; i++) {
359        levlCoeffs[i] = get_vlc2(&q->gb, hufftab[cb_sel[i]]->table,
360                                 IMC_VLC_BITS, 2);
361        if (levlCoeffs[i] == 17)
362            levlCoeffs[i] += get_bits(&q->gb, 4);
363    }
364}
365
366static void imc_read_level_coeffs_raw(IMCContext *q, int stream_format_code,
367                                      int *levlCoeffs)
368{
369    int i;
370
371    q->coef0_pos  = get_bits(&q->gb, 5);
372    levlCoeffs[0] = get_bits(&q->gb, 7);
373    for (i = 1; i < BANDS; i++)
374        levlCoeffs[i] = get_bits(&q->gb, 4);
375}
376
377static void imc_decode_level_coefficients(IMCContext *q, int *levlCoeffBuf,
378                                          float *flcoeffs1, float *flcoeffs2)
379{
380    int i, level;
381    float tmp, tmp2;
382    // maybe some frequency division thingy
383
384    flcoeffs1[0] = 20000.0 / exp2 (levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
385    flcoeffs2[0] = log2f(flcoeffs1[0]);
386    tmp  = flcoeffs1[0];
387    tmp2 = flcoeffs2[0];
388
389    for (i = 1; i < BANDS; i++) {
390        level = levlCoeffBuf[i];
391        if (level == 16) {
392            flcoeffs1[i] = 1.0;
393            flcoeffs2[i] = 0.0;
394        } else {
395            if (level < 17)
396                level -= 7;
397            else if (level <= 24)
398                level -= 32;
399            else
400                level -= 16;
401
402            tmp  *= imc_exp_tab[15 + level];
403            tmp2 += 0.83048 * level;  // 0.83048 = log2(10) * 0.25
404            flcoeffs1[i] = tmp;
405            flcoeffs2[i] = tmp2;
406        }
407    }
408}
409
410
411static void imc_decode_level_coefficients2(IMCContext *q, int *levlCoeffBuf,
412                                           float *old_floor, float *flcoeffs1,
413                                           float *flcoeffs2)
414{
415    int i;
416    /* FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors
417     *       and flcoeffs2 old scale factors
418     *       might be incomplete due to a missing table that is in the binary code
419     */
420    for (i = 0; i < BANDS; i++) {
421        flcoeffs1[i] = 0;
422        if (levlCoeffBuf[i] < 16) {
423            flcoeffs1[i] = imc_exp_tab2[levlCoeffBuf[i]] * old_floor[i];
424            flcoeffs2[i] = (levlCoeffBuf[i] - 7) * 0.83048 + flcoeffs2[i]; // 0.83048 = log2(10) * 0.25
425        } else {
426            flcoeffs1[i] = old_floor[i];
427        }
428    }
429}
430
431static void imc_decode_level_coefficients_raw(IMCContext *q, int *levlCoeffBuf,
432                                              float *flcoeffs1, float *flcoeffs2)
433{
434    int i, level, pos;
435    float tmp, tmp2;
436
437    pos = q->coef0_pos;
438    flcoeffs1[pos] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
439    flcoeffs2[pos] = log2f(flcoeffs1[pos]);
440    tmp  = flcoeffs1[pos];
441    tmp2 = flcoeffs2[pos];
442
443    levlCoeffBuf++;
444    for (i = 0; i < BANDS; i++) {
445        if (i == pos)
446            continue;
447        level = *levlCoeffBuf++;
448        flcoeffs1[i] = tmp  * powf(10.0, -level * 0.4375); //todo tab
449        flcoeffs2[i] = tmp2 - 1.4533435415 * level; // 1.4533435415 = log2(10) * 0.4375
450    }
451}
452
453/**
454 * Perform bit allocation depending on bits available
455 */
456static int bit_allocation(IMCContext *q, IMCChannel *chctx,
457                          int stream_format_code, int freebits, int flag)
458{
459    int i, j;
460    const float limit = -1.e20;
461    float highest = 0.0;
462    int indx;
463    int t1 = 0;
464    int t2 = 1;
465    float summa = 0.0;
466    int iacc = 0;
467    int summer = 0;
468    int rres, cwlen;
469    float lowest = 1.e10;
470    int low_indx = 0;
471    float workT[32];
472    int flg;
473    int found_indx = 0;
474
475    for (i = 0; i < BANDS; i++)
476        highest = FFMAX(highest, chctx->flcoeffs1[i]);
477
478    for (i = 0; i < BANDS - 1; i++) {
479        if (chctx->flcoeffs5[i] <= 0) {
480            av_log(q->avctx, AV_LOG_ERROR, "flcoeffs5 %f invalid\n", chctx->flcoeffs5[i]);
481            return AVERROR_INVALIDDATA;
482        }
483        chctx->flcoeffs4[i] = chctx->flcoeffs3[i] - log2f(chctx->flcoeffs5[i]);
484    }
485    chctx->flcoeffs4[BANDS - 1] = limit;
486
487    highest = highest * 0.25;
488
489    for (i = 0; i < BANDS; i++) {
490        indx = -1;
491        if ((band_tab[i + 1] - band_tab[i]) == chctx->bandWidthT[i])
492            indx = 0;
493
494        if ((band_tab[i + 1] - band_tab[i]) > chctx->bandWidthT[i])
495            indx = 1;
496
497        if (((band_tab[i + 1] - band_tab[i]) / 2) >= chctx->bandWidthT[i])
498            indx = 2;
499
500        if (indx == -1)
501            return AVERROR_INVALIDDATA;
502
503        chctx->flcoeffs4[i] += xTab[(indx * 2 + (chctx->flcoeffs1[i] < highest)) * 2 + flag];
504    }
505
506    if (stream_format_code & 0x2) {
507        chctx->flcoeffs4[0] = limit;
508        chctx->flcoeffs4[1] = limit;
509        chctx->flcoeffs4[2] = limit;
510        chctx->flcoeffs4[3] = limit;
511    }
512
513    for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS - 1; i++) {
514        iacc  += chctx->bandWidthT[i];
515        summa += chctx->bandWidthT[i] * chctx->flcoeffs4[i];
516    }
517
518    if (!iacc)
519        return AVERROR_INVALIDDATA;
520
521    chctx->bandWidthT[BANDS - 1] = 0;
522    summa = (summa * 0.5 - freebits) / iacc;
523
524
525    for (i = 0; i < BANDS / 2; i++) {
526        rres = summer - freebits;
527        if ((rres >= -8) && (rres <= 8))
528            break;
529
530        summer = 0;
531        iacc   = 0;
532
533        for (j = (stream_format_code & 0x2) ? 4 : 0; j < BANDS; j++) {
534            cwlen = av_clipf(((chctx->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6);
535
536            chctx->bitsBandT[j] = cwlen;
537            summer += chctx->bandWidthT[j] * cwlen;
538
539            if (cwlen > 0)
540                iacc += chctx->bandWidthT[j];
541        }
542
543        flg = t2;
544        t2 = 1;
545        if (freebits < summer)
546            t2 = -1;
547        if (i == 0)
548            flg = t2;
549        if (flg != t2)
550            t1++;
551
552        summa = (float)(summer - freebits) / ((t1 + 1) * iacc) + summa;
553    }
554
555    for (i = (stream_format_code & 0x2) ? 4 : 0; i < BANDS; i++) {
556        for (j = band_tab[i]; j < band_tab[i + 1]; j++)
557            chctx->CWlengthT[j] = chctx->bitsBandT[i];
558    }
559
560    if (freebits > summer) {
561        for (i = 0; i < BANDS; i++) {
562            workT[i] = (chctx->bitsBandT[i] == 6) ? -1.e20
563                                              : (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] - 0.415);
564        }
565
566        highest = 0.0;
567
568        do {
569            if (highest <= -1.e20)
570                break;
571
572            found_indx = 0;
573            highest = -1.e20;
574
575            for (i = 0; i < BANDS; i++) {
576                if (workT[i] > highest) {
577                    highest = workT[i];
578                    found_indx = i;
579                }
580            }
581
582            if (highest > -1.e20) {
583                workT[found_indx] -= 2.0;
584                if (++chctx->bitsBandT[found_indx] == 6)
585                    workT[found_indx] = -1.e20;
586
587                for (j = band_tab[found_indx]; j < band_tab[found_indx + 1] && (freebits > summer); j++) {
588                    chctx->CWlengthT[j]++;
589                    summer++;
590                }
591            }
592        } while (freebits > summer);
593    }
594    if (freebits < summer) {
595        for (i = 0; i < BANDS; i++) {
596            workT[i] = chctx->bitsBandT[i] ? (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] + 1.585)
597                                       : 1.e20;
598        }
599        if (stream_format_code & 0x2) {
600            workT[0] = 1.e20;
601            workT[1] = 1.e20;
602            workT[2] = 1.e20;
603            workT[3] = 1.e20;
604        }
605        while (freebits < summer) {
606            lowest   = 1.e10;
607            low_indx = 0;
608            for (i = 0; i < BANDS; i++) {
609                if (workT[i] < lowest) {
610                    lowest   = workT[i];
611                    low_indx = i;
612                }
613            }
614            // if (lowest >= 1.e10)
615            //     break;
616            workT[low_indx] = lowest + 2.0;
617
618            if (!--chctx->bitsBandT[low_indx])
619                workT[low_indx] = 1.e20;
620
621            for (j = band_tab[low_indx]; j < band_tab[low_indx+1] && (freebits < summer); j++) {
622                if (chctx->CWlengthT[j] > 0) {
623                    chctx->CWlengthT[j]--;
624                    summer--;
625                }
626            }
627        }
628    }
629    return 0;
630}
631
632static void imc_get_skip_coeff(IMCContext *q, IMCChannel *chctx)
633{
634    int i, j;
635
636    memset(chctx->skipFlagBits,  0, sizeof(chctx->skipFlagBits));
637    memset(chctx->skipFlagCount, 0, sizeof(chctx->skipFlagCount));
638    for (i = 0; i < BANDS; i++) {
639        if (!chctx->bandFlagsBuf[i] || !chctx->bandWidthT[i])
640            continue;
641
642        if (!chctx->skipFlagRaw[i]) {
643            chctx->skipFlagBits[i] = band_tab[i + 1] - band_tab[i];
644
645            for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
646                chctx->skipFlags[j] = get_bits1(&q->gb);
647                if (chctx->skipFlags[j])
648                    chctx->skipFlagCount[i]++;
649            }
650        } else {
651            for (j = band_tab[i]; j < band_tab[i + 1] - 1; j += 2) {
652                if (!get_bits1(&q->gb)) { // 0
653                    chctx->skipFlagBits[i]++;
654                    chctx->skipFlags[j]      = 1;
655                    chctx->skipFlags[j + 1]  = 1;
656                    chctx->skipFlagCount[i] += 2;
657                } else {
658                    if (get_bits1(&q->gb)) { // 11
659                        chctx->skipFlagBits[i] += 2;
660                        chctx->skipFlags[j]     = 0;
661                        chctx->skipFlags[j + 1] = 1;
662                        chctx->skipFlagCount[i]++;
663                    } else {
664                        chctx->skipFlagBits[i] += 3;
665                        chctx->skipFlags[j + 1] = 0;
666                        if (!get_bits1(&q->gb)) { // 100
667                            chctx->skipFlags[j] = 1;
668                            chctx->skipFlagCount[i]++;
669                        } else { // 101
670                            chctx->skipFlags[j] = 0;
671                        }
672                    }
673                }
674            }
675
676            if (j < band_tab[i + 1]) {
677                chctx->skipFlagBits[i]++;
678                if ((chctx->skipFlags[j] = get_bits1(&q->gb)))
679                    chctx->skipFlagCount[i]++;
680            }
681        }
682    }
683}
684
685/**
686 * Increase highest' band coefficient sizes as some bits won't be used
687 */
688static void imc_adjust_bit_allocation(IMCContext *q, IMCChannel *chctx,
689                                      int summer)
690{
691    float workT[32];
692    int corrected = 0;
693    int i, j;
694    float highest  = 0;
695    int found_indx = 0;
696
697    for (i = 0; i < BANDS; i++) {
698        workT[i] = (chctx->bitsBandT[i] == 6) ? -1.e20
699                                          : (chctx->bitsBandT[i] * -2 + chctx->flcoeffs4[i] - 0.415);
700    }
701
702    while (corrected < summer) {
703        if (highest <= -1.e20)
704            break;
705
706        highest = -1.e20;
707
708        for (i = 0; i < BANDS; i++) {
709            if (workT[i] > highest) {
710                highest = workT[i];
711                found_indx = i;
712            }
713        }
714
715        if (highest > -1.e20) {
716            workT[found_indx] -= 2.0;
717            if (++(chctx->bitsBandT[found_indx]) == 6)
718                workT[found_indx] = -1.e20;
719
720            for (j = band_tab[found_indx]; j < band_tab[found_indx+1] && (corrected < summer); j++) {
721                if (!chctx->skipFlags[j] && (chctx->CWlengthT[j] < 6)) {
722                    chctx->CWlengthT[j]++;
723                    corrected++;
724                }
725            }
726        }
727    }
728}
729
730static void imc_imdct256(IMCContext *q, IMCChannel *chctx, int channels)
731{
732    int i;
733    float re, im;
734    float *dst1 = q->out_samples;
735    float *dst2 = q->out_samples + (COEFFS - 1);
736
737    /* prerotation */
738    for (i = 0; i < COEFFS / 2; i++) {
739        q->samples[i].re = -(q->pre_coef1[i] * chctx->CWdecoded[COEFFS - 1 - i * 2]) -
740                            (q->pre_coef2[i] * chctx->CWdecoded[i * 2]);
741        q->samples[i].im =  (q->pre_coef2[i] * chctx->CWdecoded[COEFFS - 1 - i * 2]) -
742                            (q->pre_coef1[i] * chctx->CWdecoded[i * 2]);
743    }
744
745    /* FFT */
746    q->fft.fft_permute(&q->fft, q->samples);
747    q->fft.fft_calc(&q->fft, q->samples);
748
749    /* postrotation, window and reorder */
750    for (i = 0; i < COEFFS / 2; i++) {
751        re = ( q->samples[i].re * q->post_cos[i]) + (-q->samples[i].im * q->post_sin[i]);
752        im = (-q->samples[i].im * q->post_cos[i]) - ( q->samples[i].re * q->post_sin[i]);
753        *dst1 =  (q->mdct_sine_window[COEFFS - 1 - i * 2] * chctx->last_fft_im[i])
754               + (q->mdct_sine_window[i * 2] * re);
755        *dst2 =  (q->mdct_sine_window[i * 2] * chctx->last_fft_im[i])
756               - (q->mdct_sine_window[COEFFS - 1 - i * 2] * re);
757        dst1 += 2;
758        dst2 -= 2;
759        chctx->last_fft_im[i] = im;
760    }
761}
762
763static int inverse_quant_coeff(IMCContext *q, IMCChannel *chctx,
764                               int stream_format_code)
765{
766    int i, j;
767    int middle_value, cw_len, max_size;
768    const float *quantizer;
769
770    for (i = 0; i < BANDS; i++) {
771        for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
772            chctx->CWdecoded[j] = 0;
773            cw_len = chctx->CWlengthT[j];
774
775            if (cw_len <= 0 || chctx->skipFlags[j])
776                continue;
777
778            max_size     = 1 << cw_len;
779            middle_value = max_size >> 1;
780
781            if (chctx->codewords[j] >= max_size || chctx->codewords[j] < 0)
782                return AVERROR_INVALIDDATA;
783
784            if (cw_len >= 4) {
785                quantizer = imc_quantizer2[(stream_format_code & 2) >> 1];
786                if (chctx->codewords[j] >= middle_value)
787                    chctx->CWdecoded[j] =  quantizer[chctx->codewords[j] - 8]                * chctx->flcoeffs6[i];
788                else
789                    chctx->CWdecoded[j] = -quantizer[max_size - chctx->codewords[j] - 8 - 1] * chctx->flcoeffs6[i];
790            }else{
791                quantizer = imc_quantizer1[((stream_format_code & 2) >> 1) | (chctx->bandFlagsBuf[i] << 1)];
792                if (chctx->codewords[j] >= middle_value)
793                    chctx->CWdecoded[j] =  quantizer[chctx->codewords[j] - 1]            * chctx->flcoeffs6[i];
794                else
795                    chctx->CWdecoded[j] = -quantizer[max_size - 2 - chctx->codewords[j]] * chctx->flcoeffs6[i];
796            }
797        }
798    }
799    return 0;
800}
801
802
803static void imc_get_coeffs(AVCodecContext *avctx,
804                           IMCContext *q, IMCChannel *chctx)
805{
806    int i, j, cw_len, cw;
807
808    for (i = 0; i < BANDS; i++) {
809        if (!chctx->sumLenArr[i])
810            continue;
811        if (chctx->bandFlagsBuf[i] || chctx->bandWidthT[i]) {
812            for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
813                cw_len = chctx->CWlengthT[j];
814                cw = 0;
815
816                if (cw_len && (!chctx->bandFlagsBuf[i] || !chctx->skipFlags[j])) {
817                    if (get_bits_count(&q->gb) + cw_len > 512) {
818                        av_log(avctx, AV_LOG_WARNING,
819                            "Potential problem on band %i, coefficient %i"
820                            ": cw_len=%i\n", i, j, cw_len);
821                    } else
822                        cw = get_bits(&q->gb, cw_len);
823                }
824
825                chctx->codewords[j] = cw;
826            }
827        }
828    }
829}
830
831static void imc_refine_bit_allocation(IMCContext *q, IMCChannel *chctx)
832{
833    int i, j;
834    int summer;
835
836    for (i = 0; i < BANDS; i++) {
837        chctx->sumLenArr[i]   = 0;
838        chctx->skipFlagRaw[i] = 0;
839        for (j = band_tab[i]; j < band_tab[i + 1]; j++)
840            chctx->sumLenArr[i] += chctx->CWlengthT[j];
841        if (chctx->bandFlagsBuf[i])
842            if (((int)((band_tab[i + 1] - band_tab[i]) * 1.5) > chctx->sumLenArr[i]) && (chctx->sumLenArr[i] > 0))
843                chctx->skipFlagRaw[i] = 1;
844    }
845
846    imc_get_skip_coeff(q, chctx);
847
848    for (i = 0; i < BANDS; i++) {
849        chctx->flcoeffs6[i] = chctx->flcoeffs1[i];
850        /* band has flag set and at least one coded coefficient */
851        if (chctx->bandFlagsBuf[i] && (band_tab[i + 1] - band_tab[i]) != chctx->skipFlagCount[i]) {
852            chctx->flcoeffs6[i] *= q->sqrt_tab[ band_tab[i + 1] - band_tab[i]] /
853                                   q->sqrt_tab[(band_tab[i + 1] - band_tab[i] - chctx->skipFlagCount[i])];
854        }
855    }
856
857    /* calculate bits left, bits needed and adjust bit allocation */
858    summer = 0;
859
860    for (i = 0; i < BANDS; i++) {
861        if (chctx->bandFlagsBuf[i]) {
862            for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
863                if (chctx->skipFlags[j]) {
864                    summer += chctx->CWlengthT[j];
865                    chctx->CWlengthT[j] = 0;
866                }
867            }
868            summer -= chctx->skipFlagBits[i];
869        }
870    }
871    imc_adjust_bit_allocation(q, chctx, summer);
872}
873
874static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch)
875{
876    int stream_format_code;
877    int imc_hdr, i, j, ret;
878    int flag;
879    int bits;
880    int bitscount;
881    IMCChannel *chctx = q->chctx + ch;
882
883
884    /* Check the frame header */
885    imc_hdr = get_bits(&q->gb, 9);
886    if (imc_hdr & 0x18) {
887        av_log(avctx, AV_LOG_ERROR, "frame header check failed!\n");
888        av_log(avctx, AV_LOG_ERROR, "got %X.\n", imc_hdr);
889        return AVERROR_INVALIDDATA;
890    }
891    stream_format_code = get_bits(&q->gb, 3);
892
893    if (stream_format_code & 0x04)
894        chctx->decoder_reset = 1;
895
896    if (chctx->decoder_reset) {
897        for (i = 0; i < BANDS; i++)
898            chctx->old_floor[i] = 1.0;
899        for (i = 0; i < COEFFS; i++)
900            chctx->CWdecoded[i] = 0;
901        chctx->decoder_reset = 0;
902    }
903
904    flag = get_bits1(&q->gb);
905    if (stream_format_code & 0x1)
906        imc_read_level_coeffs_raw(q, stream_format_code, chctx->levlCoeffBuf);
907    else
908        imc_read_level_coeffs(q, stream_format_code, chctx->levlCoeffBuf);
909
910    if (stream_format_code & 0x1)
911        imc_decode_level_coefficients_raw(q, chctx->levlCoeffBuf,
912                                          chctx->flcoeffs1, chctx->flcoeffs2);
913    else if (stream_format_code & 0x4)
914        imc_decode_level_coefficients(q, chctx->levlCoeffBuf,
915                                      chctx->flcoeffs1, chctx->flcoeffs2);
916    else
917        imc_decode_level_coefficients2(q, chctx->levlCoeffBuf, chctx->old_floor,
918                                       chctx->flcoeffs1, chctx->flcoeffs2);
919
920    for(i=0; i<BANDS; i++) {
921        if(chctx->flcoeffs1[i] > INT_MAX) {
922            av_log(avctx, AV_LOG_ERROR, "scalefactor out of range\n");
923            return AVERROR_INVALIDDATA;
924        }
925    }
926
927    memcpy(chctx->old_floor, chctx->flcoeffs1, 32 * sizeof(float));
928
929    if (stream_format_code & 0x1) {
930        for (i = 0; i < BANDS; i++) {
931            chctx->bandWidthT[i]   = band_tab[i + 1] - band_tab[i];
932            chctx->bandFlagsBuf[i] = 0;
933            chctx->flcoeffs3[i]    = chctx->flcoeffs2[i] * 2;
934            chctx->flcoeffs5[i]    = 1.0;
935        }
936    } else {
937        for (i = 0; i < BANDS; i++) {
938            if (chctx->levlCoeffBuf[i] == 16) {
939                chctx->bandWidthT[i] = 0;
940            } else
941                chctx->bandWidthT[i] = band_tab[i + 1] - band_tab[i];
942        }
943
944        memset(chctx->bandFlagsBuf, 0, BANDS * sizeof(int));
945        for (i = 0; i < BANDS - 1; i++)
946            if (chctx->bandWidthT[i])
947                chctx->bandFlagsBuf[i] = get_bits1(&q->gb);
948
949        imc_calculate_coeffs(q, chctx->flcoeffs1, chctx->flcoeffs2,
950                             chctx->bandWidthT, chctx->flcoeffs3,
951                             chctx->flcoeffs5);
952    }
953
954    bitscount = 0;
955    /* first 4 bands will be assigned 5 bits per coefficient */
956    if (stream_format_code & 0x2) {
957        bitscount += 15;
958
959        chctx->bitsBandT[0] = 5;
960        chctx->CWlengthT[0] = 5;
961        chctx->CWlengthT[1] = 5;
962        chctx->CWlengthT[2] = 5;
963        for (i = 1; i < 4; i++) {
964            if (stream_format_code & 0x1)
965                bits = 5;
966            else
967                bits = (chctx->levlCoeffBuf[i] == 16) ? 0 : 5;
968            chctx->bitsBandT[i] = bits;
969            for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
970                chctx->CWlengthT[j] = bits;
971                bitscount      += bits;
972            }
973        }
974    }
975    if (avctx->codec_id == AV_CODEC_ID_IAC) {
976        bitscount += !!chctx->bandWidthT[BANDS - 1];
977        if (!(stream_format_code & 0x2))
978            bitscount += 16;
979    }
980
981    if ((ret = bit_allocation(q, chctx, stream_format_code,
982                              512 - bitscount - get_bits_count(&q->gb),
983                              flag)) < 0) {
984        av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
985        chctx->decoder_reset = 1;
986        return ret;
987    }
988
989    if (stream_format_code & 0x1) {
990        for (i = 0; i < BANDS; i++)
991            chctx->skipFlags[i] = 0;
992    } else {
993        imc_refine_bit_allocation(q, chctx);
994    }
995
996    for (i = 0; i < BANDS; i++) {
997        chctx->sumLenArr[i] = 0;
998
999        for (j = band_tab[i]; j < band_tab[i + 1]; j++)
1000            if (!chctx->skipFlags[j])
1001                chctx->sumLenArr[i] += chctx->CWlengthT[j];
1002    }
1003
1004    memset(chctx->codewords, 0, sizeof(chctx->codewords));
1005
1006    imc_get_coeffs(avctx, q, chctx);
1007
1008    if (inverse_quant_coeff(q, chctx, stream_format_code) < 0) {
1009        av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n");
1010        chctx->decoder_reset = 1;
1011        return AVERROR_INVALIDDATA;
1012    }
1013
1014    memset(chctx->skipFlags, 0, sizeof(chctx->skipFlags));
1015
1016    imc_imdct256(q, chctx, avctx->ch_layout.nb_channels);
1017
1018    return 0;
1019}
1020
1021static int imc_decode_frame(AVCodecContext *avctx, AVFrame *frame,
1022                            int *got_frame_ptr, AVPacket *avpkt)
1023{
1024    const uint8_t *buf = avpkt->data;
1025    int buf_size = avpkt->size;
1026    int ret, i;
1027
1028    IMCContext *q = avctx->priv_data;
1029
1030    LOCAL_ALIGNED_16(uint16_t, buf16, [(IMC_BLOCK_SIZE + AV_INPUT_BUFFER_PADDING_SIZE) / 2]);
1031
1032    q->avctx = avctx;
1033
1034    if (buf_size < IMC_BLOCK_SIZE * avctx->ch_layout.nb_channels) {
1035        av_log(avctx, AV_LOG_ERROR, "frame too small!\n");
1036        return AVERROR_INVALIDDATA;
1037    }
1038
1039    /* get output buffer */
1040    frame->nb_samples = COEFFS;
1041    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1042        return ret;
1043
1044    for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
1045        q->out_samples = (float *)frame->extended_data[i];
1046
1047        q->bdsp.bswap16_buf(buf16, (const uint16_t *) buf, IMC_BLOCK_SIZE / 2);
1048
1049        init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8);
1050
1051        buf += IMC_BLOCK_SIZE;
1052
1053        if ((ret = imc_decode_block(avctx, q, i)) < 0)
1054            return ret;
1055    }
1056
1057    if (avctx->ch_layout.nb_channels == 2) {
1058        q->butterflies_float((float *)frame->extended_data[0],
1059                             (float *)frame->extended_data[1], COEFFS);
1060    }
1061
1062    *got_frame_ptr = 1;
1063
1064    return IMC_BLOCK_SIZE * avctx->ch_layout.nb_channels;
1065}
1066
1067static av_cold int imc_decode_close(AVCodecContext * avctx)
1068{
1069    IMCContext *q = avctx->priv_data;
1070
1071    ff_fft_end(&q->fft);
1072
1073    return 0;
1074}
1075
1076static av_cold void flush(AVCodecContext *avctx)
1077{
1078    IMCContext *q = avctx->priv_data;
1079
1080    q->chctx[0].decoder_reset =
1081    q->chctx[1].decoder_reset = 1;
1082}
1083
1084#if CONFIG_IMC_DECODER
1085const FFCodec ff_imc_decoder = {
1086    .p.name         = "imc",
1087    .p.long_name    = NULL_IF_CONFIG_SMALL("IMC (Intel Music Coder)"),
1088    .p.type         = AVMEDIA_TYPE_AUDIO,
1089    .p.id           = AV_CODEC_ID_IMC,
1090    .priv_data_size = sizeof(IMCContext),
1091    .init           = imc_decode_init,
1092    .close          = imc_decode_close,
1093    FF_CODEC_DECODE_CB(imc_decode_frame),
1094    .flush          = flush,
1095    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
1096    .p.sample_fmts  = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1097                                                      AV_SAMPLE_FMT_NONE },
1098    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
1099};
1100#endif
1101#if CONFIG_IAC_DECODER
1102const FFCodec ff_iac_decoder = {
1103    .p.name         = "iac",
1104    .p.long_name    = NULL_IF_CONFIG_SMALL("IAC (Indeo Audio Coder)"),
1105    .p.type         = AVMEDIA_TYPE_AUDIO,
1106    .p.id           = AV_CODEC_ID_IAC,
1107    .priv_data_size = sizeof(IMCContext),
1108    .init           = imc_decode_init,
1109    .close          = imc_decode_close,
1110    FF_CODEC_DECODE_CB(imc_decode_frame),
1111    .flush          = flush,
1112    .p.capabilities = AV_CODEC_CAP_DR1,
1113    .p.sample_fmts  = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
1114                                                      AV_SAMPLE_FMT_NONE },
1115    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
1116};
1117#endif
1118