]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/imc.c
lavc: Drop deprecated way of setting codec dimensions
[ffmpeg] / libavcodec / imc.c
index d53693c848f7b426e9f6fd6c8a2b299a7356e437..145b2d56fc865e75dee7fe09f014fff75775b659 100644 (file)
@@ -27,7 +27,6 @@
  *  A mdct based codec using a 256 points large transform
  *  divided into 32 bands with some mix of scale factors.
  *  Only mono is supported.
- *
  */
 
 
 #include <stddef.h>
 #include <stdio.h>
 
+#include "libavutil/channel_layout.h"
+#include "libavutil/float_dsp.h"
+#include "libavutil/internal.h"
+
 #include "avcodec.h"
-#include "get_bits.h"
-#include "dsputil.h"
+#include "bitstream.h"
+#include "bswapdsp.h"
 #include "fft.h"
-#include "libavutil/audioconvert.h"
+#include "internal.h"
 #include "sinewin.h"
 
 #include "imcdata.h"
@@ -67,7 +70,7 @@ typedef struct IMCChannel {
     int sumLenArr[BANDS];      ///< bits for all coeffs in band
     int skipFlagRaw[BANDS];    ///< skip flags are stored in raw form or not
     int skipFlagBits[BANDS];   ///< bits used to code skip flags
-    int skipFlagCount[BANDS];  ///< skipped coeffients per band
+    int skipFlagCount[BANDS];  ///< skipped coefficients per band
     int skipFlags[COEFFS];     ///< skip coefficient decoding or not
     int codewords[COEFFS];     ///< raw codewords read from bitstream
 
@@ -76,10 +79,8 @@ typedef struct IMCChannel {
     int decoder_reset;
 } IMCChannel;
 
-typedef struct {
-    AVFrame frame;
-
-    IMCChannel chctx[1];
+typedef struct IMCContext {
+    IMCChannel chctx[2];
 
     /** MDCT tables */
     //@{
@@ -91,13 +92,18 @@ typedef struct {
     //@}
 
     float sqrt_tab[30];
-    GetBitContext gb;
-    float one_div_log2;
+    BitstreamContext bc;
 
-    DSPContext dsp;
+    BswapDSPContext bdsp;
+    AVFloatDSPContext fdsp;
     FFTContext fft;
     DECLARE_ALIGNED(32, FFTComplex, samples)[COEFFS / 2];
     float *out_samples;
+
+    int coef0_pos;
+
+    int8_t cyclTab[32], cyclTab2[32];
+    float  weights1[31], weights2[31];
 } IMCContext;
 
 static VLC huffman_vlc[4][4];
@@ -111,14 +117,73 @@ static const int vlc_offsets[17] = {
 
 static VLC_TYPE vlc_tables[VLC_TABLES_SIZE][2];
 
+static inline double freq2bark(double freq)
+{
+    return 3.5 * atan((freq / 7500.0) * (freq / 7500.0)) + 13.0 * atan(freq * 0.00076);
+}
+
+static av_cold void iac_generate_tabs(IMCContext *q, int sampling_rate)
+{
+    double freqmin[32], freqmid[32], freqmax[32];
+    double scale = sampling_rate / (256.0 * 2.0 * 2.0);
+    double nyquist_freq = sampling_rate * 0.5;
+    double freq, bark, prev_bark = 0, tf, tb;
+    int i, j;
+
+    for (i = 0; i < 32; i++) {
+        freq = (band_tab[i] + band_tab[i + 1] - 1) * scale;
+        bark = freq2bark(freq);
+
+        if (i > 0) {
+            tb = bark - prev_bark;
+            q->weights1[i - 1] = pow(10.0, -1.0 * tb);
+            q->weights2[i - 1] = pow(10.0, -2.7 * tb);
+        }
+        prev_bark = bark;
+
+        freqmid[i] = freq;
+
+        tf = freq;
+        while (tf < nyquist_freq) {
+            tf += 0.5;
+            tb =  freq2bark(tf);
+            if (tb > bark + 0.5)
+                break;
+        }
+        freqmax[i] = tf;
+
+        tf = freq;
+        while (tf > 0.0) {
+            tf -= 0.5;
+            tb =  freq2bark(tf);
+            if (tb <= bark - 0.5)
+                break;
+        }
+        freqmin[i] = tf;
+    }
+
+    for (i = 0; i < 32; i++) {
+        freq = freqmax[i];
+        for (j = 31; j > 0 && freq <= freqmid[j]; j--);
+        q->cyclTab[i] = j + 1;
+
+        freq = freqmin[i];
+        for (j = 0; j < 32 && freq >= freqmid[j]; j++);
+        q->cyclTab2[i] = j - 1;
+    }
+}
+
 static av_cold int imc_decode_init(AVCodecContext *avctx)
 {
     int i, j, ret;
     IMCContext *q = avctx->priv_data;
     double r1, r2;
 
-    if (avctx->channels != 1) {
-        av_log_ask_for_sample(avctx, "Number of channels is not supported\n");
+    if (avctx->codec_id == AV_CODEC_ID_IMC)
+        avctx->channels = 1;
+
+    if (avctx->channels > 2) {
+        avpriv_request_sample(avctx, "Number of channels > 2");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -167,20 +232,26 @@ static av_cold int imc_decode_init(AVCodecContext *avctx)
                      imc_huffman_bits[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
         }
     }
-    q->one_div_log2 = 1 / log(2);
+
+    if (avctx->codec_id == AV_CODEC_ID_IAC) {
+        iac_generate_tabs(q, avctx->sample_rate);
+    } else {
+        memcpy(q->cyclTab,  cyclTab,  sizeof(cyclTab));
+        memcpy(q->cyclTab2, cyclTab2, sizeof(cyclTab2));
+        memcpy(q->weights1, imc_weights1, sizeof(imc_weights1));
+        memcpy(q->weights2, imc_weights2, sizeof(imc_weights2));
+    }
 
     if ((ret = ff_fft_init(&q->fft, 7, 1))) {
         av_log(avctx, AV_LOG_INFO, "FFT init failed\n");
         return ret;
     }
-    ff_dsputil_init(&q->dsp, avctx);
-    avctx->sample_fmt     = AV_SAMPLE_FMT_FLT;
+    ff_bswapdsp_init(&q->bdsp);
+    avpriv_float_dsp_init(&q->fdsp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
+    avctx->sample_fmt     = AV_SAMPLE_FMT_FLTP;
     avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO
                                                  : AV_CH_LAYOUT_STEREO;
 
-    avcodec_get_frame_defaults(&q->frame);
-    avctx->coded_frame = &q->frame;
-
     return 0;
 }
 
@@ -210,13 +281,13 @@ static void imc_calculate_coeffs(IMCContext *q, float *flcoeffs1,
     }
 
     for (i = 0; i < BANDS; i++) {
-        for (cnt2 = i; cnt2 < cyclTab[i]; cnt2++)
+        for (cnt2 = i; cnt2 < q->cyclTab[i]; cnt2++)
             flcoeffs5[cnt2] = flcoeffs5[cnt2] + workT3[i];
         workT2[cnt2 - 1] = workT2[cnt2 - 1] + workT3[i];
     }
 
     for (i = 1; i < BANDS; i++) {
-        accum = (workT2[i - 1] + accum) * imc_weights1[i - 1];
+        accum = (workT2[i - 1] + accum) * q->weights1[i - 1];
         flcoeffs5[i] += accum;
     }
 
@@ -224,7 +295,7 @@ static void imc_calculate_coeffs(IMCContext *q, float *flcoeffs1,
         workT2[i] = 0.0;
 
     for (i = 0; i < BANDS; i++) {
-        for (cnt2 = i - 1; cnt2 > cyclTab2[i]; cnt2--)
+        for (cnt2 = i - 1; cnt2 > q->cyclTab2[i]; cnt2--)
             flcoeffs5[cnt2] += workT3[i];
         workT2[cnt2+1] += workT3[i];
     }
@@ -232,7 +303,7 @@ static void imc_calculate_coeffs(IMCContext *q, float *flcoeffs1,
     accum = 0.0;
 
     for (i = BANDS-2; i >= 0; i--) {
-        accum = (workT2[i+1] + accum) * imc_weights2[i];
+        accum = (workT2[i+1] + accum) * q->weights2[i];
         flcoeffs5[i] += accum;
         // there is missing code here, but it seems to never be triggered
     }
@@ -258,15 +329,26 @@ static void imc_read_level_coeffs(IMCContext *q, int stream_format_code,
     if (stream_format_code & 4)
         start = 1;
     if (start)
-        levlCoeffs[0] = get_bits(&q->gb, 7);
+        levlCoeffs[0] = bitstream_read(&q->bc, 7);
     for (i = start; i < BANDS; i++) {
-        levlCoeffs[i] = get_vlc2(&q->gb, hufftab[cb_sel[i]]->table,
-                                 hufftab[cb_sel[i]]->bits, 2);
+        levlCoeffs[i] = bitstream_read_vlc(&q->bc, hufftab[cb_sel[i]]->table,
+                                           hufftab[cb_sel[i]]->bits, 2);
         if (levlCoeffs[i] == 17)
-            levlCoeffs[i] += get_bits(&q->gb, 4);
+            levlCoeffs[i] += bitstream_read(&q->bc, 4);
     }
 }
 
+static void imc_read_level_coeffs_raw(IMCContext *q, int stream_format_code,
+                                      int *levlCoeffs)
+{
+    int i;
+
+    q->coef0_pos  = bitstream_read(&q->bc, 5);
+    levlCoeffs[0] = bitstream_read(&q->bc, 7);
+    for (i = 1; i < BANDS; i++)
+        levlCoeffs[i] = bitstream_read(&q->bc, 4);
+}
+
 static void imc_decode_level_coefficients(IMCContext *q, int *levlCoeffBuf,
                                           float *flcoeffs1, float *flcoeffs2)
 {
@@ -275,7 +357,7 @@ static void imc_decode_level_coefficients(IMCContext *q, int *levlCoeffBuf,
     // maybe some frequency division thingy
 
     flcoeffs1[0] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
-    flcoeffs2[0] = log(flcoeffs1[0]) / log(2);
+    flcoeffs2[0] = log2f(flcoeffs1[0]);
     tmp  = flcoeffs1[0];
     tmp2 = flcoeffs2[0];
 
@@ -321,6 +403,28 @@ static void imc_decode_level_coefficients2(IMCContext *q, int *levlCoeffBuf,
     }
 }
 
+static void imc_decode_level_coefficients_raw(IMCContext *q, int *levlCoeffBuf,
+                                              float *flcoeffs1, float *flcoeffs2)
+{
+    int i, level, pos;
+    float tmp, tmp2;
+
+    pos = q->coef0_pos;
+    flcoeffs1[pos] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
+    flcoeffs2[pos] = log2f(flcoeffs1[pos]);
+    tmp  = flcoeffs1[pos];
+    tmp2 = flcoeffs2[pos];
+
+    levlCoeffBuf++;
+    for (i = 0; i < BANDS; i++) {
+        if (i == pos)
+            continue;
+        level = *levlCoeffBuf++;
+        flcoeffs1[i] = tmp  * powf(10.0, -level * 0.4375); //todo tab
+        flcoeffs2[i] = tmp2 - 1.4533435415 * level; // 1.4533435415 = log2(10) * 0.4375
+    }
+}
+
 /**
  * Perform bit allocation depending on bits available
  */
@@ -347,7 +451,7 @@ static int bit_allocation(IMCContext *q, IMCChannel *chctx,
         highest = FFMAX(highest, chctx->flcoeffs1[i]);
 
     for (i = 0; i < BANDS - 1; i++)
-        chctx->flcoeffs4[i] = chctx->flcoeffs3[i] - log(chctx->flcoeffs5[i]) / log(2);
+        chctx->flcoeffs4[i] = chctx->flcoeffs3[i] - log2f(chctx->flcoeffs5[i]);
     chctx->flcoeffs4[BANDS - 1] = limit;
 
     highest = highest * 0.25;
@@ -380,6 +484,10 @@ static int bit_allocation(IMCContext *q, IMCChannel *chctx,
         iacc  += chctx->bandWidthT[i];
         summa += chctx->bandWidthT[i] * chctx->flcoeffs4[i];
     }
+
+    if (!iacc)
+        return AVERROR_INVALIDDATA;
+
     chctx->bandWidthT[BANDS - 1] = 0;
     summa = (summa * 0.5 - freebits) / iacc;
 
@@ -505,19 +613,19 @@ static void imc_get_skip_coeff(IMCContext *q, IMCChannel *chctx)
             chctx->skipFlagBits[i] = band_tab[i + 1] - band_tab[i];
 
             for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
-                chctx->skipFlags[j] = get_bits1(&q->gb);
+                chctx->skipFlags[j] = bitstream_read_bit(&q->bc);
                 if (chctx->skipFlags[j])
                     chctx->skipFlagCount[i]++;
             }
         } else {
             for (j = band_tab[i]; j < band_tab[i + 1] - 1; j += 2) {
-                if (!get_bits1(&q->gb)) { // 0
+                if (!bitstream_read_bit(&q->bc)) { // 0
                     chctx->skipFlagBits[i]++;
                     chctx->skipFlags[j]      = 1;
                     chctx->skipFlags[j + 1]  = 1;
                     chctx->skipFlagCount[i] += 2;
                 } else {
-                    if (get_bits1(&q->gb)) { // 11
+                    if (bitstream_read_bit(&q->bc)) { // 11
                         chctx->skipFlagBits[i] += 2;
                         chctx->skipFlags[j]     = 0;
                         chctx->skipFlags[j + 1] = 1;
@@ -525,7 +633,7 @@ static void imc_get_skip_coeff(IMCContext *q, IMCChannel *chctx)
                     } else {
                         chctx->skipFlagBits[i] += 3;
                         chctx->skipFlags[j + 1] = 0;
-                        if (!get_bits1(&q->gb)) { // 100
+                        if (!bitstream_read_bit(&q->bc)) { // 100
                             chctx->skipFlags[j] = 1;
                             chctx->skipFlagCount[i]++;
                         } else { // 101
@@ -537,7 +645,7 @@ static void imc_get_skip_coeff(IMCContext *q, IMCChannel *chctx)
 
             if (j < band_tab[i + 1]) {
                 chctx->skipFlagBits[i]++;
-                if ((chctx->skipFlags[j] = get_bits1(&q->gb)))
+                if ((chctx->skipFlags[j] = bitstream_read_bit(&q->bc)))
                     chctx->skipFlagCount[i]++;
             }
         }
@@ -589,10 +697,12 @@ static void imc_adjust_bit_allocation(IMCContext *q, IMCChannel *chctx,
     }
 }
 
-static void imc_imdct256(IMCContext *q, IMCChannel *chctx)
+static void imc_imdct256(IMCContext *q, IMCChannel *chctx, int channels)
 {
     int i;
     float re, im;
+    float *dst1 = q->out_samples;
+    float *dst2 = q->out_samples + (COEFFS - 1);
 
     /* prerotation */
     for (i = 0; i < COEFFS / 2; i++) {
@@ -610,10 +720,12 @@ static void imc_imdct256(IMCContext *q, IMCChannel *chctx)
     for (i = 0; i < COEFFS / 2; i++) {
         re = ( q->samples[i].re * q->post_cos[i]) + (-q->samples[i].im * q->post_sin[i]);
         im = (-q->samples[i].im * q->post_cos[i]) - ( q->samples[i].re * q->post_sin[i]);
-        q->out_samples[i * 2]              =  (q->mdct_sine_window[COEFFS - 1 - i * 2] * chctx->last_fft_im[i])
-                                            + (q->mdct_sine_window[i * 2] * re);
-        q->out_samples[COEFFS - 1 - i * 2] =  (q->mdct_sine_window[i * 2] * chctx->last_fft_im[i])
-                                            - (q->mdct_sine_window[COEFFS - 1 - i * 2] * re);
+        *dst1 =  (q->mdct_sine_window[COEFFS - 1 - i * 2] * chctx->last_fft_im[i])
+               + (q->mdct_sine_window[i * 2] * re);
+        *dst2 =  (q->mdct_sine_window[i * 2] * chctx->last_fft_im[i])
+               - (q->mdct_sine_window[COEFFS - 1 - i * 2] * re);
+        dst1 += 2;
+        dst2 -= 2;
         chctx->last_fft_im[i] = im;
     }
 }
@@ -670,13 +782,13 @@ static int imc_get_coeffs(IMCContext *q, IMCChannel *chctx)
                 cw_len = chctx->CWlengthT[j];
                 cw = 0;
 
-                if (get_bits_count(&q->gb) + cw_len > 512) {
-                    // av_log(NULL, 0, "Band %i coeff %i cw_len %i\n", i, j, cw_len);
+                if (bitstream_tell(&q->bc) + cw_len > 512) {
+                    ff_dlog(NULL, "Band %i coeff %i cw_len %i\n", i, j, cw_len);
                     return AVERROR_INVALIDDATA;
                 }
 
                 if (cw_len && (!chctx->bandFlagsBuf[i] || !chctx->skipFlags[j]))
-                    cw = get_bits(&q->gb, cw_len);
+                    cw = bitstream_read(&q->bc, cw_len);
 
                 chctx->codewords[j] = cw;
             }
@@ -685,37 +797,73 @@ static int imc_get_coeffs(IMCContext *q, IMCChannel *chctx)
     return 0;
 }
 
+static void imc_refine_bit_allocation(IMCContext *q, IMCChannel *chctx)
+{
+    int i, j;
+    int bits, summer;
+
+    for (i = 0; i < BANDS; i++) {
+        chctx->sumLenArr[i]   = 0;
+        chctx->skipFlagRaw[i] = 0;
+        for (j = band_tab[i]; j < band_tab[i + 1]; j++)
+            chctx->sumLenArr[i] += chctx->CWlengthT[j];
+        if (chctx->bandFlagsBuf[i])
+            if ((((band_tab[i + 1] - band_tab[i]) * 1.5) > chctx->sumLenArr[i]) && (chctx->sumLenArr[i] > 0))
+                chctx->skipFlagRaw[i] = 1;
+    }
+
+    imc_get_skip_coeff(q, chctx);
+
+    for (i = 0; i < BANDS; i++) {
+        chctx->flcoeffs6[i] = chctx->flcoeffs1[i];
+        /* band has flag set and at least one coded coefficient */
+        if (chctx->bandFlagsBuf[i] && (band_tab[i + 1] - band_tab[i]) != chctx->skipFlagCount[i]) {
+            chctx->flcoeffs6[i] *= q->sqrt_tab[ band_tab[i + 1] - band_tab[i]] /
+                                   q->sqrt_tab[(band_tab[i + 1] - band_tab[i] - chctx->skipFlagCount[i])];
+        }
+    }
+
+    /* calculate bits left, bits needed and adjust bit allocation */
+    bits = summer = 0;
+
+    for (i = 0; i < BANDS; i++) {
+        if (chctx->bandFlagsBuf[i]) {
+            for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
+                if (chctx->skipFlags[j]) {
+                    summer += chctx->CWlengthT[j];
+                    chctx->CWlengthT[j] = 0;
+                }
+            }
+            bits   += chctx->skipFlagBits[i];
+            summer -= chctx->skipFlagBits[i];
+        }
+    }
+    imc_adjust_bit_allocation(q, chctx, summer);
+}
+
 static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch)
 {
     int stream_format_code;
     int imc_hdr, i, j, ret;
     int flag;
-    int bits, summer;
+    int bits;
     int counter, bitscount;
     IMCChannel *chctx = q->chctx + ch;
 
 
     /* Check the frame header */
-    imc_hdr = get_bits(&q->gb, 9);
-    if (imc_hdr != IMC_FRAME_ID) {
-        av_log(avctx, AV_LOG_ERROR, "imc frame header check failed!\n");
-        av_log(avctx, AV_LOG_ERROR, "got %x instead of 0x21.\n", imc_hdr);
-        return AVERROR_INVALIDDATA;
-    }
-    stream_format_code = get_bits(&q->gb, 3);
-
-    if (stream_format_code & 1) {
-        av_log(avctx, AV_LOG_ERROR, "Stream code format %X is not supported\n", stream_format_code);
+    imc_hdr = bitstream_read(&q->bc, 9);
+    if (imc_hdr & 0x18) {
+        av_log(avctx, AV_LOG_ERROR, "frame header check failed!\n");
+        av_log(avctx, AV_LOG_ERROR, "got %X.\n", imc_hdr);
         return AVERROR_INVALIDDATA;
     }
-
-//    av_log(avctx, AV_LOG_DEBUG, "stream_format_code = %d\n", stream_format_code);
+    stream_format_code = bitstream_read(&q->bc, 3);
 
     if (stream_format_code & 0x04)
         chctx->decoder_reset = 1;
 
     if (chctx->decoder_reset) {
-        memset(q->out_samples, 0, sizeof(q->out_samples));
         for (i = 0; i < BANDS; i++)
             chctx->old_floor[i] = 1.0;
         for (i = 0; i < COEFFS; i++)
@@ -723,10 +871,16 @@ static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch)
         chctx->decoder_reset = 0;
     }
 
-    flag = get_bits1(&q->gb);
-    imc_read_level_coeffs(q, stream_format_code, chctx->levlCoeffBuf);
+    flag = bitstream_read_bit(&q->bc);
+    if (stream_format_code & 0x1)
+        imc_read_level_coeffs_raw(q, stream_format_code, chctx->levlCoeffBuf);
+    else
+        imc_read_level_coeffs(q, stream_format_code, chctx->levlCoeffBuf);
 
-    if (stream_format_code & 0x4)
+    if (stream_format_code & 0x1)
+        imc_decode_level_coefficients_raw(q, chctx->levlCoeffBuf,
+                                          chctx->flcoeffs1, chctx->flcoeffs2);
+    else if (stream_format_code & 0x4)
         imc_decode_level_coefficients(q, chctx->levlCoeffBuf,
                                       chctx->flcoeffs1, chctx->flcoeffs2);
     else
@@ -736,20 +890,31 @@ static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch)
     memcpy(chctx->old_floor, chctx->flcoeffs1, 32 * sizeof(float));
 
     counter = 0;
-    for (i = 0; i < BANDS; i++) {
-        if (chctx->levlCoeffBuf[i] == 16) {
-            chctx->bandWidthT[i] = 0;
-            counter++;
-        } else
-            chctx->bandWidthT[i] = band_tab[i + 1] - band_tab[i];
-    }
-    memset(chctx->bandFlagsBuf, 0, BANDS * sizeof(int));
-    for (i = 0; i < BANDS - 1; i++) {
-        if (chctx->bandWidthT[i])
-            chctx->bandFlagsBuf[i] = get_bits1(&q->gb);
-    }
+    if (stream_format_code & 0x1) {
+        for (i = 0; i < BANDS; i++) {
+            chctx->bandWidthT[i]   = band_tab[i + 1] - band_tab[i];
+            chctx->bandFlagsBuf[i] = 0;
+            chctx->flcoeffs3[i]    = chctx->flcoeffs2[i] * 2;
+            chctx->flcoeffs5[i]    = 1.0;
+        }
+    } else {
+        for (i = 0; i < BANDS; i++) {
+            if (chctx->levlCoeffBuf[i] == 16) {
+                chctx->bandWidthT[i] = 0;
+                counter++;
+            } else
+                chctx->bandWidthT[i] = band_tab[i + 1] - band_tab[i];
+        }
 
-    imc_calculate_coeffs(q, chctx->flcoeffs1, chctx->flcoeffs2, chctx->bandWidthT, chctx->flcoeffs3, chctx->flcoeffs5);
+        memset(chctx->bandFlagsBuf, 0, BANDS * sizeof(int));
+        for (i = 0; i < BANDS - 1; i++)
+            if (chctx->bandWidthT[i])
+                chctx->bandFlagsBuf[i] = bitstream_read_bit(&q->bc);
+
+        imc_calculate_coeffs(q, chctx->flcoeffs1, chctx->flcoeffs2,
+                             chctx->bandWidthT, chctx->flcoeffs3,
+                             chctx->flcoeffs5);
+    }
 
     bitscount = 0;
     /* first 4 bands will be assigned 5 bits per coefficient */
@@ -761,7 +926,10 @@ static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch)
         chctx->CWlengthT[1] = 5;
         chctx->CWlengthT[2] = 5;
         for (i = 1; i < 4; i++) {
-            bits = (chctx->levlCoeffBuf[i] == 16) ? 0 : 5;
+            if (stream_format_code & 0x1)
+                bits = 5;
+            else
+                bits = (chctx->levlCoeffBuf[i] == 16) ? 0 : 5;
             chctx->bitsBandT[i] = bits;
             for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
                 chctx->CWlengthT[j] = bits;
@@ -769,52 +937,26 @@ static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch)
             }
         }
     }
+    if (avctx->codec_id == AV_CODEC_ID_IAC) {
+        bitscount += !!chctx->bandWidthT[BANDS - 1];
+        if (!(stream_format_code & 0x2))
+            bitscount += 16;
+    }
 
     if ((ret = bit_allocation(q, chctx, stream_format_code,
-                              512 - bitscount - get_bits_count(&q->gb),
+                              512 - bitscount - bitstream_tell(&q->bc),
                               flag)) < 0) {
         av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
         chctx->decoder_reset = 1;
         return ret;
     }
 
-    for (i = 0; i < BANDS; i++) {
-        chctx->sumLenArr[i]   = 0;
-        chctx->skipFlagRaw[i] = 0;
-        for (j = band_tab[i]; j < band_tab[i + 1]; j++)
-            chctx->sumLenArr[i] += chctx->CWlengthT[j];
-        if (chctx->bandFlagsBuf[i])
-            if ((((band_tab[i + 1] - band_tab[i]) * 1.5) > chctx->sumLenArr[i]) && (chctx->sumLenArr[i] > 0))
-                chctx->skipFlagRaw[i] = 1;
-    }
-
-    imc_get_skip_coeff(q, chctx);
-
-    for (i = 0; i < BANDS; i++) {
-        chctx->flcoeffs6[i] = chctx->flcoeffs1[i];
-        /* band has flag set and at least one coded coefficient */
-        if (chctx->bandFlagsBuf[i] && (band_tab[i + 1] - band_tab[i]) != chctx->skipFlagCount[i]) {
-            chctx->flcoeffs6[i] *= q->sqrt_tab[ band_tab[i + 1] - band_tab[i]] /
-                                   q->sqrt_tab[(band_tab[i + 1] - band_tab[i] - chctx->skipFlagCount[i])];
-        }
-    }
-
-    /* calculate bits left, bits needed and adjust bit allocation */
-    bits = summer = 0;
-
-    for (i = 0; i < BANDS; i++) {
-        if (chctx->bandFlagsBuf[i]) {
-            for (j = band_tab[i]; j < band_tab[i + 1]; j++) {
-                if (chctx->skipFlags[j]) {
-                    summer += chctx->CWlengthT[j];
-                    chctx->CWlengthT[j] = 0;
-                }
-            }
-            bits   += chctx->skipFlagBits[i];
-            summer -= chctx->skipFlagBits[i];
-        }
+    if (stream_format_code & 0x1) {
+        for (i = 0; i < BANDS; i++)
+            chctx->skipFlags[i] = 0;
+    } else {
+        imc_refine_bit_allocation(q, chctx);
     }
-    imc_adjust_bit_allocation(q, chctx, summer);
 
     for (i = 0; i < BANDS; i++) {
         chctx->sumLenArr[i] = 0;
@@ -840,7 +982,7 @@ static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch)
 
     memset(chctx->skipFlags, 0, sizeof(chctx->skipFlags));
 
-    imc_imdct256(q, chctx);
+    imc_imdct256(q, chctx, avctx->channels);
 
     return 0;
 }
@@ -848,32 +990,33 @@ static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch)
 static int imc_decode_frame(AVCodecContext *avctx, void *data,
                             int *got_frame_ptr, AVPacket *avpkt)
 {
+    AVFrame *frame     = data;
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     int ret, i;
 
     IMCContext *q = avctx->priv_data;
 
-    LOCAL_ALIGNED_16(uint16_t, buf16, [IMC_BLOCK_SIZE / 2]);
+    LOCAL_ALIGNED_16(uint16_t, buf16, [(IMC_BLOCK_SIZE + AV_INPUT_BUFFER_PADDING_SIZE) / 2]);
 
-    if (buf_size < IMC_BLOCK_SIZE) {
-        av_log(avctx, AV_LOG_ERROR, "imc frame too small!\n");
+    if (buf_size < IMC_BLOCK_SIZE * avctx->channels) {
+        av_log(avctx, AV_LOG_ERROR, "frame too small!\n");
         return AVERROR_INVALIDDATA;
     }
 
     /* get output buffer */
-    q->frame.nb_samples = COEFFS;
-    if ((ret = avctx->get_buffer(avctx, &q->frame)) < 0) {
+    frame->nb_samples = COEFFS;
+    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
         return ret;
     }
 
     for (i = 0; i < avctx->channels; i++) {
-        q->out_samples = (float*)q->frame.data[0] + i;
+        q->out_samples = (float *)frame->extended_data[i];
 
-        q->dsp.bswap16_buf(buf16, (const uint16_t*)buf, IMC_BLOCK_SIZE / 2);
+        q->bdsp.bswap16_buf(buf16, (const uint16_t *) buf, IMC_BLOCK_SIZE / 2);
 
-        init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8);
+        bitstream_init8(&q->bc, (const uint8_t *)buf16, IMC_BLOCK_SIZE);
 
         buf += IMC_BLOCK_SIZE;
 
@@ -881,8 +1024,12 @@ static int imc_decode_frame(AVCodecContext *avctx, void *data,
             return ret;
     }
 
-    *got_frame_ptr   = 1;
-    *(AVFrame *)data = q->frame;
+    if (avctx->channels == 2) {
+        q->fdsp.butterflies_float((float *)frame->extended_data[0],
+                                  (float *)frame->extended_data[1], COEFFS);
+    }
+
+    *got_frame_ptr = 1;
 
     return IMC_BLOCK_SIZE * avctx->channels;
 }
@@ -900,12 +1047,28 @@ static av_cold int imc_decode_close(AVCodecContext * avctx)
 
 AVCodec ff_imc_decoder = {
     .name           = "imc",
+    .long_name      = NULL_IF_CONFIG_SMALL("IMC (Intel Music Coder)"),
     .type           = AVMEDIA_TYPE_AUDIO,
-    .id             = CODEC_ID_IMC,
+    .id             = AV_CODEC_ID_IMC,
     .priv_data_size = sizeof(IMCContext),
     .init           = imc_decode_init,
     .close          = imc_decode_close,
     .decode         = imc_decode_frame,
-    .capabilities   = CODEC_CAP_DR1,
-    .long_name      = NULL_IF_CONFIG_SMALL("IMC (Intel Music Coder)"),
+    .capabilities   = AV_CODEC_CAP_DR1,
+    .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
+                                                      AV_SAMPLE_FMT_NONE },
+};
+
+AVCodec ff_iac_decoder = {
+    .name           = "iac",
+    .long_name      = NULL_IF_CONFIG_SMALL("IAC (Indeo Audio Coder)"),
+    .type           = AVMEDIA_TYPE_AUDIO,
+    .id             = AV_CODEC_ID_IAC,
+    .priv_data_size = sizeof(IMCContext),
+    .init           = imc_decode_init,
+    .close          = imc_decode_close,
+    .decode         = imc_decode_frame,
+    .capabilities   = AV_CODEC_CAP_DR1,
+    .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
+                                                      AV_SAMPLE_FMT_NONE },
 };