]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/imc.c
adpcm_ima_ws: fix stereo decoding
[ffmpeg] / libavcodec / imc.c
index a7045ef3e18ca5f3886092f3efb1b7662a432d95..ff8e31e9e64ac485ae918b8f4607eb0dfd18aefd 100644 (file)
@@ -4,26 +4,26 @@
  * Copyright (c) 2006 Benjamin Larsson
  * Copyright (c) 2006 Konstantin Shishkov
  *
- * This file is part of FFmpeg.
+ * This file is part of Libav.
  *
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * FFmpeg is distributed in the hope that it will be useful,
+ * Libav is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
  */
 
 /**
- *  @file imc.c IMC - Intel Music Coder
+ *  @file
+ *  IMC - Intel Music Coder
  *  A mdct based codec using a 256 points large transform
  *  divied into 32 bands with some mix of scale factors.
  *  Only mono is supported.
 #include <stddef.h>
 #include <stdio.h>
 
-#define ALT_BITSTREAM_READER
 #include "avcodec.h"
-#include "bitstream.h"
+#include "get_bits.h"
 #include "dsputil.h"
+#include "fft.h"
+#include "libavutil/audioconvert.h"
+#include "sinewin.h"
 
 #include "imcdata.h"
 
+#define IMC_BLOCK_SIZE 64
 #define IMC_FRAME_ID 0x21
 #define BANDS 32
 #define COEFFS 256
 
 typedef struct {
+    AVFrame frame;
+
     float old_floor[BANDS];
     float flcoeffs1[BANDS];
     float flcoeffs2[BANDS];
@@ -79,35 +84,48 @@ typedef struct {
     int codewords[COEFFS];     ///< raw codewords read from bitstream
     float sqrt_tab[30];
     GetBitContext gb;
-    VLC huffman_vlc[4][4];
-    float flcf1, flcf2;
     int decoder_reset;
     float one_div_log2;
 
     DSPContext dsp;
     FFTContext fft;
-    DECLARE_ALIGNED_16(FFTComplex, samples[COEFFS/2]);
-    DECLARE_ALIGNED_16(float, out_samples[COEFFS]);
+    DECLARE_ALIGNED(32, FFTComplex, samples)[COEFFS/2];
+    float *out_samples;
 } IMCContext;
 
+static VLC huffman_vlc[4][4];
+
+#define VLC_TABLES_SIZE 9512
+
+static const int vlc_offsets[17] = {
+    0,     640, 1156, 1732, 2308, 2852, 3396, 3924,
+    4452, 5220, 5860, 6628, 7268, 7908, 8424, 8936, VLC_TABLES_SIZE};
+
+static VLC_TYPE vlc_tables[VLC_TABLES_SIZE][2];
 
-static int imc_decode_init(AVCodecContext * avctx)
+static av_cold int imc_decode_init(AVCodecContext * avctx)
 {
-    int i, j;
+    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");
+        return AVERROR_PATCHWELCOME;
+    }
+
     q->decoder_reset = 1;
 
     for(i = 0; i < BANDS; i++)
         q->old_floor[i] = 1.0;
 
     /* Build mdct window, a simple sine window normalized with sqrt(2) */
+    ff_sine_window_init(q->mdct_sine_window, COEFFS);
     for(i = 0; i < COEFFS; i++)
-        q->mdct_sine_window[i] = sin((i + 0.5) / 512.0 * M_PI) * sqrt(2.0);
+        q->mdct_sine_window[i] *= sqrt(2.0);
     for(i = 0; i < COEFFS/2; i++){
-        q->post_cos[i] = cos(i / 256.0 * M_PI);
-        q->post_sin[i] = sin(i / 256.0 * M_PI);
+        q->post_cos[i] = (1.0f / 32768) * cos(i / 256.0 * M_PI);
+        q->post_sin[i] = (1.0f / 32768) * sin(i / 256.0 * M_PI);
 
         r1 = sin((i * 4.0 + 1.0) / 1024.0 * M_PI);
         r2 = cos((i * 4.0 + 1.0) / 1024.0 * M_PI);
@@ -125,8 +143,6 @@ static int imc_decode_init(AVCodecContext * avctx)
 
         q->last_fft_im[i] = 0;
     }
-    q->flcf1 = log2(10) * 0.05703125;
-    q->flcf2 = log2(10) * 0.25;
 
     /* Generate a square root table */
 
@@ -137,15 +153,26 @@ static int imc_decode_init(AVCodecContext * avctx)
     /* initialize the VLC tables */
     for(i = 0; i < 4 ; i++) {
         for(j = 0; j < 4; j++) {
-            init_vlc (&q->huffman_vlc[i][j], 9, imc_huffman_sizes[i],
+            huffman_vlc[i][j].table = &vlc_tables[vlc_offsets[i * 4 + j]];
+            huffman_vlc[i][j].table_allocated = vlc_offsets[i * 4 + j + 1] - vlc_offsets[i * 4 + j];
+            init_vlc(&huffman_vlc[i][j], 9, imc_huffman_sizes[i],
                      imc_huffman_lens[i][j], 1, 1,
-                     imc_huffman_bits[i][j], 2, 2, 0);
+                     imc_huffman_bits[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
         }
     }
     q->one_div_log2 = 1/log(2);
 
-    ff_fft_init(&q->fft, 7, 1);
+    if ((ret = ff_fft_init(&q->fft, 7, 1))) {
+        av_log(avctx, AV_LOG_INFO, "FFT init failed\n");
+        return ret;
+    }
     dsputil_init(&q->dsp, avctx);
+    avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
+    avctx->channel_layout = AV_CH_LAYOUT_MONO;
+
+    avcodec_get_frame_defaults(&q->frame);
+    avctx->coded_frame = &q->frame;
+
     return 0;
 }
 
@@ -212,10 +239,10 @@ static void imc_read_level_coeffs(IMCContext* q, int stream_format_code, int* le
     int s;
 
     s = stream_format_code >> 1;
-    hufftab[0] = &q->huffman_vlc[s][0];
-    hufftab[1] = &q->huffman_vlc[s][1];
-    hufftab[2] = &q->huffman_vlc[s][2];
-    hufftab[3] = &q->huffman_vlc[s][3];
+    hufftab[0] = &huffman_vlc[s][0];
+    hufftab[1] = &huffman_vlc[s][1];
+    hufftab[2] = &huffman_vlc[s][2];
+    hufftab[3] = &huffman_vlc[s][3];
     cb_sel = imc_cb_select[s];
 
     if(stream_format_code & 4)
@@ -236,8 +263,8 @@ static void imc_decode_level_coefficients(IMCContext* q, int* levlCoeffBuf, floa
     float tmp, tmp2;
     //maybe some frequency division thingy
 
-    flcoeffs1[0] = 20000.0 / pow (2, levlCoeffBuf[0] * q->flcf1);
-    flcoeffs2[0] = log2(flcoeffs1[0]);
+    flcoeffs1[0] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125
+    flcoeffs2[0] = log(flcoeffs1[0])/log(2);
     tmp = flcoeffs1[0];
     tmp2 = flcoeffs2[0];
 
@@ -255,7 +282,7 @@ static void imc_decode_level_coefficients(IMCContext* q, int* levlCoeffBuf, floa
                 level -=16;
 
             tmp  *= imc_exp_tab[15 + level];
-            tmp2 += q->flcf2 * level;
+            tmp2 += 0.83048 * level;  // 0.83048 = log2(10) * 0.25
             flcoeffs1[i] = tmp;
             flcoeffs2[i] = tmp2;
         }
@@ -273,7 +300,7 @@ static void imc_decode_level_coefficients2(IMCContext* q, int* levlCoeffBuf, flo
         flcoeffs1[i] = 0;
         if(levlCoeffBuf[i] < 16) {
             flcoeffs1[i] = imc_exp_tab2[levlCoeffBuf[i]] * old_floor[i];
-            flcoeffs2[i] = (levlCoeffBuf[i]-7) * q->flcf2 + flcoeffs2[i];
+            flcoeffs2[i] = (levlCoeffBuf[i]-7) * 0.83048 + flcoeffs2[i]; // 0.83048 = log2(10) * 0.25
         } else {
             flcoeffs1[i] = old_floor[i];
         }
@@ -304,7 +331,7 @@ static int bit_allocation (IMCContext* q, int stream_format_code, int freebits,
         highest = FFMAX(highest, q->flcoeffs1[i]);
 
     for(i = 0; i < BANDS-1; i++) {
-        q->flcoeffs4[i] = q->flcoeffs3[i] - log2(q->flcoeffs5[i]);
+        q->flcoeffs4[i] = q->flcoeffs3[i] - log(q->flcoeffs5[i])/log(2);
     }
     q->flcoeffs4[BANDS - 1] = limit;
 
@@ -322,7 +349,7 @@ static int bit_allocation (IMCContext* q, int stream_format_code, int freebits,
             indx = 2;
 
         if (indx == -1)
-            return -1;
+            return AVERROR_INVALIDDATA;
 
         q->flcoeffs4[i] = q->flcoeffs4[i] + xTab[(indx*2 + (q->flcoeffs1[i] < highest)) * 2 + flag];
     }
@@ -350,7 +377,7 @@ static int bit_allocation (IMCContext* q, int stream_format_code, int freebits,
         iacc = 0;
 
         for(j = (stream_format_code & 0x2)?4:0; j < BANDS; j++) {
-            cwlen = clip((int)((q->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6);
+            cwlen = av_clipf(((q->flcoeffs4[j] * 0.5) - summa + 0.5), 0, 6);
 
             q->bitsBandT[j] = cwlen;
             summer += q->bandWidthT[j] * cwlen;
@@ -458,7 +485,7 @@ static void imc_get_skip_coeff(IMCContext* q) {
             q->skipFlagBits[i] = band_tab[i+1] - band_tab[i];
 
             for(j = band_tab[i]; j < band_tab[i+1]; j++) {
-                if ((q->skipFlags[j] = get_bits(&q->gb,1)))
+                if ((q->skipFlags[j] = get_bits1(&q->gb)))
                     q->skipFlagCount[i]++;
             }
         } else {
@@ -489,7 +516,7 @@ static void imc_get_skip_coeff(IMCContext* q) {
 
             if (j < band_tab[i+1]) {
                 q->skipFlagBits[i]++;
-                if ((q->skipFlags[j] = get_bits(&q->gb,1)))
+                if ((q->skipFlags[j] = get_bits1(&q->gb)))
                     q->skipFlagCount[i]++;
             }
         }
@@ -538,7 +565,7 @@ static void imc_adjust_bit_allocation (IMCContext* q, int summer) {
     }
 }
 
-void imc_imdct256(IMCContext *q) {
+static void imc_imdct256(IMCContext *q) {
     int i;
     float re, im;
 
@@ -551,8 +578,8 @@ void imc_imdct256(IMCContext *q) {
     }
 
     /* FFT */
-    ff_fft_permute(&q->fft, q->samples);
-    ff_fft_calc (&q->fft, q->samples);
+    q->fft.fft_permute(&q->fft, q->samples);
+    q->fft.fft_calc   (&q->fft, q->samples);
 
     /* postrotation, window and reorder */
     for(i = 0; i < COEFFS/2; i++){
@@ -581,7 +608,7 @@ static int inverse_quant_coeff (IMCContext* q, int stream_format_code) {
             middle_value = max_size >> 1;
 
             if (q->codewords[j] >= max_size || q->codewords[j] < 0)
-                return -1;
+                return AVERROR_INVALIDDATA;
 
             if (cw_len >= 4){
                 quantizer = imc_quantizer2[(stream_format_code & 2) >> 1];
@@ -614,7 +641,7 @@ static int imc_get_coeffs (IMCContext* q) {
 
                 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);
-                    return -1;
+                    return AVERROR_INVALIDDATA;
                 }
 
                 if(cw_len && (!q->bandFlagsBuf[i] || !q->skipFlags[j]))
@@ -627,38 +654,50 @@ static int imc_get_coeffs (IMCContext* q) {
     return 0;
 }
 
-static int imc_decode_frame(AVCodecContext * avctx,
-                            void *data, int *data_size,
-                            uint8_t * buf, int buf_size)
+static int imc_decode_frame(AVCodecContext * avctx, void *data,
+                            int *got_frame_ptr, AVPacket *avpkt)
 {
+    const uint8_t *buf = avpkt->data;
+    int buf_size = avpkt->size;
 
     IMCContext *q = avctx->priv_data;
 
     int stream_format_code;
-    int imc_hdr, i, j;
+    int imc_hdr, i, j, ret;
     int flag;
     int bits, summer;
     int counter, bitscount;
-    uint16_t *buf16 = (uint16_t *) buf;
+    LOCAL_ALIGNED_16(uint16_t, buf16, [IMC_BLOCK_SIZE / 2]);
+
+    if (buf_size < IMC_BLOCK_SIZE) {
+        av_log(avctx, AV_LOG_ERROR, "imc frame too small!\n");
+        return AVERROR_INVALIDDATA;
+    }
 
-    /* FIXME: input should not be modified */
-    for(i = 0; i < FFMIN(buf_size, avctx->block_align) / 2; i++)
-        buf16[i] = bswap_16(buf16[i]);
+    /* get output buffer */
+    q->frame.nb_samples = COEFFS;
+    if ((ret = avctx->get_buffer(avctx, &q->frame)) < 0) {
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        return ret;
+    }
+    q->out_samples = (float *)q->frame.data[0];
+
+    q->dsp.bswap16_buf(buf16, (const uint16_t*)buf, IMC_BLOCK_SIZE / 2);
 
-    init_get_bits(&q->gb, buf, 512);
+    init_get_bits(&q->gb, (const uint8_t*)buf16, IMC_BLOCK_SIZE * 8);
 
     /* 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 -1;
+        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);
-        return -1;
+        return AVERROR_INVALIDDATA;
     }
 
 //    av_log(avctx, AV_LOG_DEBUG, "stream_format_code = %d\n", stream_format_code);
@@ -718,10 +757,11 @@ static int imc_decode_frame(AVCodecContext * avctx,
         }
     }
 
-    if(bit_allocation (q, stream_format_code, 512 - bitscount - get_bits_count(&q->gb), flag) < 0) {
+    if((ret = bit_allocation (q, stream_format_code,
+                              512 - bitscount - get_bits_count(&q->gb), flag)) < 0) {
         av_log(avctx, AV_LOG_ERROR, "Bit allocations failed\n");
         q->decoder_reset = 1;
-        return -1;
+        return ret;
     }
 
     for(i = 0; i < BANDS; i++) {
@@ -775,42 +815,44 @@ static int imc_decode_frame(AVCodecContext * avctx,
     if(imc_get_coeffs(q) < 0) {
         av_log(avctx, AV_LOG_ERROR, "Read coefficients failed\n");
         q->decoder_reset = 1;
-        return 0;
+        return AVERROR_INVALIDDATA;
     }
 
     if(inverse_quant_coeff(q, stream_format_code) < 0) {
         av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n");
         q->decoder_reset = 1;
-        return 0;
+        return AVERROR_INVALIDDATA;
     }
 
     memset(q->skipFlags, 0, sizeof(q->skipFlags));
 
     imc_imdct256(q);
 
-    q->dsp.float_to_int16(data, q->out_samples, COEFFS);
+    *got_frame_ptr   = 1;
+    *(AVFrame *)data = q->frame;
 
-    *data_size = COEFFS * sizeof(int16_t);
-
-    return avctx->block_align;
+    return IMC_BLOCK_SIZE;
 }
 
 
-static int imc_decode_close(AVCodecContext * avctx)
+static av_cold int imc_decode_close(AVCodecContext * avctx)
 {
     IMCContext *q = avctx->priv_data;
 
     ff_fft_end(&q->fft);
+
     return 0;
 }
 
 
-AVCodec imc_decoder = {
+AVCodec ff_imc_decoder = {
     .name = "imc",
-    .type = CODEC_TYPE_AUDIO,
+    .type = AVMEDIA_TYPE_AUDIO,
     .id = 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)"),
 };