Mercurial > libavcodec.hg
changeset 4106:21aa7e801c7b libavcodec
IMC decoder
author | kostya |
---|---|
date | Mon, 30 Oct 2006 13:48:48 +0000 |
parents | 225d287b337f |
children | e678c86986b3 |
files | Makefile allcodecs.c avcodec.h imc.c imcdata.h |
diffstat | 5 files changed, 988 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Mon Oct 30 10:15:37 2006 +0000 +++ b/Makefile Mon Oct 30 13:48:48 2006 +0000 @@ -88,6 +88,7 @@ OBJS-$(CONFIG_HUFFYUV_DECODER) += huffyuv.o OBJS-$(CONFIG_HUFFYUV_ENCODER) += huffyuv.o OBJS-$(CONFIG_IDCIN_DECODER) += idcinvideo.o +OBJS-$(CONFIG_IMC_DECODER) += imc.o OBJS-$(CONFIG_INDEO2_DECODER) += indeo2.o OBJS-$(CONFIG_INDEO3_DECODER) += indeo3.o OBJS-$(CONFIG_INTERPLAY_VIDEO_DECODER) += interplayvideo.o
--- a/allcodecs.c Mon Oct 30 10:15:37 2006 +0000 +++ b/allcodecs.c Mon Oct 30 13:48:48 2006 +0000 @@ -563,6 +563,9 @@ #ifdef CONFIG_TIFF_DECODER register_avcodec(&tiff_decoder); #endif //CONFIG_TIFF_DECODER +#ifdef CONFIG_IMC_DECODER + register_avcodec(&imc_decoder); +#endif //CONFIG_IMC_DECODER #if defined(CONFIG_AMR_NB) || defined(CONFIG_AMR_NB_FIXED) #ifdef CONFIG_AMR_NB_DECODER
--- a/avcodec.h Mon Oct 30 10:15:37 2006 +0000 +++ b/avcodec.h Mon Oct 30 13:48:48 2006 +0000 @@ -37,8 +37,8 @@ #define AV_STRINGIFY(s) AV_TOSTRING(s) #define AV_TOSTRING(s) #s -#define LIBAVCODEC_VERSION_INT ((51<<16)+(22<<8)+0) -#define LIBAVCODEC_VERSION 51.22.0 +#define LIBAVCODEC_VERSION_INT ((51<<16)+(23<<8)+0) +#define LIBAVCODEC_VERSION 51.23.0 #define LIBAVCODEC_BUILD LIBAVCODEC_VERSION_INT #define LIBAVCODEC_IDENT "Lavc" AV_STRINGIFY(LIBAVCODEC_VERSION) @@ -233,6 +233,7 @@ CODEC_ID_QCELP, CODEC_ID_WAVPACK, CODEC_ID_DSICINAUDIO, + CODEC_ID_IMC, /* subtitle codecs */ CODEC_ID_DVD_SUBTITLE= 0x17000, @@ -2304,6 +2305,7 @@ extern AVCodec dsicinaudio_decoder; extern AVCodec tiertexseqvideo_decoder; extern AVCodec tiff_decoder; +extern AVCodec imc_decoder; /* pcm codecs */ #define PCM_CODEC(id, name) \
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/imc.c Mon Oct 30 13:48:48 2006 +0000 @@ -0,0 +1,816 @@ +/* + * IMC compatible decoder + * Copyright (c) 2002-2004 Maxim Poliakovski + * Copyright (c) 2006 Benjamin Larsson + * Copyright (c) 2006 Konstantin Shishkov + * + * This file is part of FFmpeg. + * + * FFmpeg 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, + * 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 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +/** + * @file imc.c 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 <math.h> +#include <stddef.h> +#include <stdio.h> + +#define ALT_BITSTREAM_READER +#include "avcodec.h" +#include "bitstream.h" +#include "dsputil.h" + +#include "imcdata.h" + +#define IMC_FRAME_ID 0x21 +#define BANDS 32 +#define COEFFS 256 + +typedef struct { + float old_floor[BANDS]; + float flcoeffs1[BANDS]; + float flcoeffs2[BANDS]; + float flcoeffs3[BANDS]; + float flcoeffs4[BANDS]; + float flcoeffs5[BANDS]; + float flcoeffs6[BANDS]; + float CWdecoded[COEFFS]; + + /** MDCT tables */ + //@{ + float mdct_sine_window[COEFFS]; + float post_cos[COEFFS]; + float post_sin[COEFFS]; + float pre_coef1[COEFFS]; + float pre_coef2[COEFFS]; + float last_fft_im[COEFFS]; + //@} + + int bandWidthT[BANDS]; ///< codewords per band + int bitsBandT[BANDS]; ///< how many bits per codeword in band + int CWlengthT[COEFFS]; ///< how many bits in each codeword + int levlCoeffBuf[BANDS]; + int bandFlagsBuf[BANDS]; ///< flags for each band + 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 skipFlags[COEFFS]; ///< skip coefficient decoding or not + 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]); +} IMCContext; + + +static int imc_decode_init(AVCodecContext * avctx) +{ + int i, j; + IMCContext *q = avctx->priv_data; + double r1, r2; + + 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) */ + for(i = 0; i < COEFFS; i++) + q->mdct_sine_window[i] = sin((i + 0.5) / 512.0 * M_PI) * 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); + + r1 = sin((i * 4.0 + 1.0) / 1024.0 * M_PI); + r2 = cos((i * 4.0 + 1.0) / 1024.0 * M_PI); + + if (i & 0x1) + { + q->pre_coef1[i] = (r1 + r2) * sqrt(2.0); + q->pre_coef2[i] = -(r1 - r2) * sqrt(2.0); + } + else + { + q->pre_coef1[i] = -(r1 + r2) * sqrt(2.0); + q->pre_coef2[i] = (r1 - r2) * sqrt(2.0); + } + + q->last_fft_im[i] = 0; + } + q->flcf1 = log2(10) * 0.05703125; + q->flcf2 = log2(10) * 0.25; + + /* Generate a square root table */ + + for(i = 0; i < 30; i++) { + q->sqrt_tab[i] = sqrt(i); + } + + /* 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], + imc_huffman_lens[i][j], 1, 1, + imc_huffman_bits[i][j], 2, 2, 0); + } + } + q->one_div_log2 = 1/log(2); + + ff_fft_init(&q->fft, 7, 1); + dsputil_init(&q->dsp, avctx); + return 0; +} + +static void imc_calculate_coeffs(IMCContext* q, float* flcoeffs1, float* flcoeffs2, int* bandWidthT, + float* flcoeffs3, float* flcoeffs5) +{ + float workT1[BANDS]; + float workT2[BANDS]; + float workT3[BANDS]; + float snr_limit = 1.e-30; + float accum = 0.0; + int i, cnt2; + + for(i = 0; i < BANDS; i++) { + flcoeffs5[i] = workT2[i] = 0.0; + if (bandWidthT[i]){ + workT1[i] = flcoeffs1[i] * flcoeffs1[i]; + flcoeffs3[i] = 2.0 * flcoeffs2[i]; + } else { + workT1[i] = 0.0; + flcoeffs3[i] = -30000.0; + } + workT3[i] = bandWidthT[i] * workT1[i] * 0.01; + if (workT3[i] <= snr_limit) + workT3[i] = 0.0; + } + + for(i = 0; i < BANDS; i++) { + for(cnt2 = i; cnt2 < 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]; + flcoeffs5[i] += accum; + } + + for(i = 0; i < BANDS; i++) + workT2[i] = 0.0; + + for(i = 0; i < BANDS; i++) { + for(cnt2 = i-1; cnt2 > cyclTab2[i]; cnt2--) + flcoeffs5[cnt2] += workT3[i]; + workT2[cnt2+1] += workT3[i]; + } + + accum = 0.0; + + for(i = BANDS-2; i >= 0; i--) { + accum = (workT2[i+1] + accum) * imc_weights2[i]; + flcoeffs5[i] += accum; + //there is missing code here, but it seems to never be triggered + } +} + + +static void imc_read_level_coeffs(IMCContext* q, int stream_format_code, int* levlCoeffs) +{ + int i; + VLC *hufftab[4]; + int start = 0; + const uint8_t *cb_sel; + 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]; + cb_sel = imc_cb_select[s]; + + if(stream_format_code & 4) + start = 1; + if(start) + levlCoeffs[0] = get_bits(&q->gb, 7); + for(i = start; i < BANDS; i++){ + levlCoeffs[i] = get_vlc2(&q->gb, hufftab[cb_sel[i]]->table, hufftab[cb_sel[i]]->bits, 2); + if(levlCoeffs[i] == 17) + levlCoeffs[i] += get_bits(&q->gb, 4); + } +} + +static void imc_decode_level_coefficients(IMCContext* q, int* levlCoeffBuf, float* flcoeffs1, + float* flcoeffs2) +{ + int i, level; + float tmp, tmp2; + //maybe some frequency division thingy + + flcoeffs1[0] = 20000.0 / pow (2, levlCoeffBuf[0] * q->flcf1); + flcoeffs2[0] = log2(flcoeffs1[0]); + tmp = flcoeffs1[0]; + tmp2 = flcoeffs2[0]; + + for(i = 1; i < BANDS; i++) { + level = levlCoeffBuf[i]; + if (level == 16) { + flcoeffs1[i] = 1.0; + flcoeffs2[i] = 0.0; + } else { + if (level < 17) + level -=7; + else if (level <= 24) + level -=32; + else + level -=16; + + tmp *= imc_exp_tab[15 + level]; + tmp2 += q->flcf2 * level; + flcoeffs1[i] = tmp; + flcoeffs2[i] = tmp2; + } + } +} + + +static void imc_decode_level_coefficients2(IMCContext* q, int* levlCoeffBuf, float* old_floor, float* flcoeffs1, + float* flcoeffs2) { + int i; + //FIXME maybe flag_buf = noise coding and flcoeffs1 = new scale factors + // and flcoeffs2 old scale factors + // might be incomplete due to a missing table that is in the binary code + for(i = 0; i < BANDS; i++) { + 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]; + } else { + flcoeffs1[i] = old_floor[i]; + } + } +} + +/** + * Perform bit allocation depending on bits available + */ +static int bit_allocation (IMCContext* q, int stream_format_code, int freebits, int flag) { + int i, j; + const float limit = -1.e20; + float highest = 0.0; + int indx; + int t1 = 0; + int t2 = 1; + float summa = 0.0; + int iacc = 0; + int summer = 0; + int rres, cwlen; + float lowest = 1.e10; + int low_indx = 0; + float workT[32]; + int flg; + int found_indx = 0; + + for(i = 0; i < BANDS; i++) + 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[BANDS - 1] = limit; + + highest = highest * 0.25; + + for(i = 0; i < BANDS; i++) { + indx = -1; + if ((band_tab[i+1] - band_tab[i]) == q->bandWidthT[i]) + indx = 0; + + if ((band_tab[i+1] - band_tab[i]) > q->bandWidthT[i]) + indx = 1; + + if (((band_tab[i+1] - band_tab[i])/2) >= q->bandWidthT[i]) + indx = 2; + + if (indx == -1) + return -1; + + q->flcoeffs4[i] = q->flcoeffs4[i] + xTab[(indx*2 + (q->flcoeffs1[i] < highest)) * 2 + flag]; + } + + if (stream_format_code & 0x2) { + q->flcoeffs4[0] = limit; + q->flcoeffs4[1] = limit; + q->flcoeffs4[2] = limit; + q->flcoeffs4[3] = limit; + } + + for(i = (stream_format_code & 0x2)?4:0; i < BANDS-1; i++) { + iacc += q->bandWidthT[i]; + summa += q->bandWidthT[i] * q->flcoeffs4[i]; + } + q->bandWidthT[BANDS-1] = 0; + summa = (summa * 0.5 - freebits) / iacc; + + + for(i = 0; i < BANDS/2; i++) { + rres = summer - freebits; + if((rres >= -8) && (rres <= 8)) break; + + summer = 0; + 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); + + q->bitsBandT[j] = cwlen; + summer += q->bandWidthT[j] * cwlen; + + if (cwlen > 0) + iacc += q->bandWidthT[j]; + } + + flg = t2; + t2 = 1; + if (freebits < summer) + t2 = -1; + if (i == 0) + flg = t2; + if(flg != t2) + t1++; + + summa = (float)(summer - freebits) / ((t1 + 1) * iacc) + summa; + } + + for(i = (stream_format_code & 0x2)?4:0; i < BANDS; i++) { + for(j = band_tab[i]; j < band_tab[i+1]; j++) + q->CWlengthT[j] = q->bitsBandT[i]; + } + + if (freebits > summer) { + for(i = 0; i < BANDS; i++) { + workT[i] = (q->bitsBandT[i] == 6) ? -1.e20 : (q->bitsBandT[i] * -2 + q->flcoeffs4[i] - 0.415); + } + + highest = 0.0; + + do{ + if (highest <= -1.e20) + break; + + found_indx = 0; + highest = -1.e20; + + for(i = 0; i < BANDS; i++) { + if (workT[i] > highest) { + highest = workT[i]; + found_indx = i; + } + } + + if (highest > -1.e20) { + workT[found_indx] -= 2.0; + if (++(q->bitsBandT[found_indx]) == 6) + workT[found_indx] = -1.e20; + + for(j = band_tab[found_indx]; j < band_tab[found_indx+1] && (freebits > summer); j++){ + q->CWlengthT[j]++; + summer++; + } + } + }while (freebits > summer); + } + if (freebits < summer) { + for(i = 0; i < BANDS; i++) { + workT[i] = q->bitsBandT[i] ? (q->bitsBandT[i] * -2 + q->flcoeffs4[i] + 1.585) : 1.e20; + } + if (stream_format_code & 0x2) { + workT[0] = 1.e20; + workT[1] = 1.e20; + workT[2] = 1.e20; + workT[3] = 1.e20; + } + while (freebits < summer){ + lowest = 1.e10; + low_indx = 0; + for(i = 0; i < BANDS; i++) { + if (workT[i] < lowest) { + lowest = workT[i]; + low_indx = i; + } + } + //if(lowest >= 1.e10) break; + workT[low_indx] = lowest + 2.0; + + if (!(--q->bitsBandT[low_indx])) + workT[low_indx] = 1.e20; + + for(j = band_tab[low_indx]; j < band_tab[low_indx+1] && (freebits < summer); j++){ + if(q->CWlengthT[j] > 0){ + q->CWlengthT[j]--; + summer--; + } + } + } + } + return 0; +} + +static void imc_get_skip_coeff(IMCContext* q) { + int i, j; + + memset(q->skipFlagBits, 0, sizeof(q->skipFlagBits)); + memset(q->skipFlagCount, 0, sizeof(q->skipFlagCount)); + for(i = 0; i < BANDS; i++) { + if (!q->bandFlagsBuf[i] || !q->bandWidthT[i]) + continue; + + if (!q->skipFlagRaw[i]) { + 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))) + q->skipFlagCount[i]++; + } + } else { + for(j = band_tab[i]; j < (band_tab[i+1]-1); j += 2) { + if(!get_bits1(&q->gb)){//0 + q->skipFlagBits[i]++; + q->skipFlags[j]=1; + q->skipFlags[j+1]=1; + q->skipFlagCount[i] += 2; + }else{ + if(get_bits1(&q->gb)){//11 + q->skipFlagBits[i] +=2; + q->skipFlags[j]=0; + q->skipFlags[j+1]=1; + q->skipFlagCount[i]++; + }else{ + q->skipFlagBits[i] +=3; + q->skipFlags[j+1]=0; + if(!get_bits1(&q->gb)){//100 + q->skipFlags[j]=1; + q->skipFlagCount[i]++; + }else{//101 + q->skipFlags[j]=0; + } + } + } + } + + if (j < band_tab[i+1]) { + q->skipFlagBits[i]++; + if ((q->skipFlags[j] = get_bits(&q->gb,1))) + q->skipFlagCount[i]++; + } + } + } +} + +/** + * Increase highest' band coefficient sizes as some bits won't be used + */ +static void imc_adjust_bit_allocation (IMCContext* q, int summer) { + float workT[32]; + int corrected = 0; + int i, j; + float highest = 0; + int found_indx=0; + + for(i = 0; i < BANDS; i++) { + workT[i] = (q->bitsBandT[i] == 6) ? -1.e20 : (q->bitsBandT[i] * -2 + q->flcoeffs4[i] - 0.415); + } + + while (corrected < summer) { + if(highest <= -1.e20) + break; + + highest = -1.e20; + + for(i = 0; i < BANDS; i++) { + if (workT[i] > highest) { + highest = workT[i]; + found_indx = i; + } + } + + if (highest > -1.e20) { + workT[found_indx] -= 2.0; + if (++(q->bitsBandT[found_indx]) == 6) + workT[found_indx] = -1.e20; + + for(j = band_tab[found_indx]; j < band_tab[found_indx+1] && (corrected < summer); j++) { + if (!q->skipFlags[j] && (q->CWlengthT[j] < 6)) { + q->CWlengthT[j]++; + corrected++; + } + } + } + } +} + +void imc_imdct256(IMCContext *q) { + int i; + float re, im; + + /* prerotation */ + for(i=0; i < COEFFS/2; i++){ + q->samples[i].re = -(q->pre_coef1[i] * q->CWdecoded[COEFFS-1-i*2]) - + (q->pre_coef2[i] * q->CWdecoded[i*2]); + q->samples[i].im = (q->pre_coef2[i] * q->CWdecoded[COEFFS-1-i*2]) - + (q->pre_coef1[i] * q->CWdecoded[i*2]); + } + + /* FFT */ + ff_fft_permute(&q->fft, q->samples); + ff_fft_calc (&q->fft, q->samples); + + /* postrotation, window and reorder */ + 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] * q->last_fft_im[i]) + (q->mdct_sine_window[i*2] * re); + q->out_samples[COEFFS-1-i*2] = (q->mdct_sine_window[i*2] * q->last_fft_im[i]) - (q->mdct_sine_window[COEFFS-1-i*2] * re); + q->last_fft_im[i] = im; + } +} + +static int inverse_quant_coeff (IMCContext* q, int stream_format_code) { + int i, j; + int middle_value, cw_len, max_size; + const float* quantizer; + + for(i = 0; i < BANDS; i++) { + for(j = band_tab[i]; j < band_tab[i+1]; j++) { + q->CWdecoded[j] = 0; + cw_len = q->CWlengthT[j]; + + if (cw_len <= 0 || q->skipFlags[j]) + continue; + + max_size = 1 << cw_len; + middle_value = max_size >> 1; + + if (q->codewords[j] >= max_size || q->codewords[j] < 0) + return -1; + + if (cw_len >= 4){ + quantizer = imc_quantizer2[(stream_format_code & 2) >> 1]; + if (q->codewords[j] >= middle_value) + q->CWdecoded[j] = quantizer[q->codewords[j] - 8] * q->flcoeffs6[i]; + else + q->CWdecoded[j] = -quantizer[max_size - q->codewords[j] - 8 - 1] * q->flcoeffs6[i]; + }else{ + quantizer = imc_quantizer1[((stream_format_code & 2) >> 1) | (q->bandFlagsBuf[i] << 1)]; + if (q->codewords[j] >= middle_value) + q->CWdecoded[j] = quantizer[q->codewords[j] - 1] * q->flcoeffs6[i]; + else + q->CWdecoded[j] = -quantizer[max_size - 2 - q->codewords[j]] * q->flcoeffs6[i]; + } + } + } + return 0; +} + + +static int imc_get_coeffs (IMCContext* q) { + int i, j, cw_len, cw; + + for(i = 0; i < BANDS; i++) { + if(!q->sumLenArr[i]) continue; + if (q->bandFlagsBuf[i] || q->bandWidthT[i]) { + for(j = band_tab[i]; j < band_tab[i+1]; j++) { + cw_len = q->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); + return -1; + } + + if(cw_len && (!q->bandFlagsBuf[i] || !q->skipFlags[j])) + cw = get_bits(&q->gb, cw_len); + + q->codewords[j] = cw; + } + } + } + return 0; +} + +static int imc_decode_frame(AVCodecContext * avctx, + void *data, int *data_size, + uint8_t * buf, int buf_size) +{ + + IMCContext *q = avctx->priv_data; + + int stream_format_code; + int imc_hdr, i, j; + int flag; + int bits, summer; + int counter, bitscount; + uint16_t *buf16 = (uint16_t *) buf; + + /* FIXME: input should not be modified */ + for(i = 0; i < FFMIN(buf_size, avctx->block_align) / 2; i++) + buf16[i] = bswap_16(buf16[i]); + + init_get_bits(&q->gb, buf, 512); + + /* 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; + } + 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; + } + +// av_log(avctx, AV_LOG_DEBUG, "stream_format_code = %d\n", stream_format_code); + + if (stream_format_code & 0x04) + q->decoder_reset = 1; + + if(q->decoder_reset) { + memset(q->out_samples, 0, sizeof(q->out_samples)); + for(i = 0; i < BANDS; i++)q->old_floor[i] = 1.0; + for(i = 0; i < COEFFS; i++)q->CWdecoded[i] = 0; + q->decoder_reset = 0; + } + + flag = get_bits1(&q->gb); + imc_read_level_coeffs(q, stream_format_code, q->levlCoeffBuf); + + if (stream_format_code & 0x4) + imc_decode_level_coefficients(q, q->levlCoeffBuf, q->flcoeffs1, q->flcoeffs2); + else + imc_decode_level_coefficients2(q, q->levlCoeffBuf, q->old_floor, q->flcoeffs1, q->flcoeffs2); + + memcpy(q->old_floor, q->flcoeffs1, 32 * sizeof(float)); + + counter = 0; + for (i=0 ; i<BANDS ; i++) { + if (q->levlCoeffBuf[i] == 16) { + q->bandWidthT[i] = 0; + counter++; + } else + q->bandWidthT[i] = band_tab[i+1] - band_tab[i]; + } + memset(q->bandFlagsBuf, 0, BANDS * sizeof(int)); + for(i = 0; i < BANDS-1; i++) { + if (q->bandWidthT[i]) + q->bandFlagsBuf[i] = get_bits1(&q->gb); + } + + imc_calculate_coeffs(q, q->flcoeffs1, q->flcoeffs2, q->bandWidthT, q->flcoeffs3, q->flcoeffs5); + + bitscount = 0; + /* first 4 bands will be assigned 5 bits per coefficient */ + if (stream_format_code & 0x2) { + bitscount += 15; + + q->bitsBandT[0] = 5; + q->CWlengthT[0] = 5; + q->CWlengthT[1] = 5; + q->CWlengthT[2] = 5; + for(i = 1; i < 4; i++){ + bits = (q->levlCoeffBuf[i] == 16) ? 0 : 5; + q->bitsBandT[i] = bits; + for(j = band_tab[i]; j < band_tab[i+1]; j++) { + q->CWlengthT[j] = bits; + bitscount += bits; + } + } + } + + if(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; + } + + for(i = 0; i < BANDS; i++) { + q->sumLenArr[i] = 0; + q->skipFlagRaw[i] = 0; + for(j = band_tab[i]; j < band_tab[i+1]; j++) + q->sumLenArr[i] += q->CWlengthT[j]; + if (q->bandFlagsBuf[i]) + if( (((band_tab[i+1] - band_tab[i]) * 1.5) > q->sumLenArr[i]) && (q->sumLenArr[i] > 0)) + q->skipFlagRaw[i] = 1; + } + + imc_get_skip_coeff(q); + + for(i = 0; i < BANDS; i++) { + q->flcoeffs6[i] = q->flcoeffs1[i]; + /* band has flag set and at least one coded coefficient */ + if (q->bandFlagsBuf[i] && (band_tab[i+1] - band_tab[i]) != q->skipFlagCount[i]){ + q->flcoeffs6[i] *= q->sqrt_tab[band_tab[i+1] - band_tab[i]] / + q->sqrt_tab[(band_tab[i+1] - band_tab[i] - q->skipFlagCount[i])]; + } + } + + /* calculate bits left, bits needed and adjust bit allocation */ + bits = summer = 0; + + for(i = 0; i < BANDS; i++) { + if (q->bandFlagsBuf[i]) { + for(j = band_tab[i]; j < band_tab[i+1]; j++) { + if(q->skipFlags[j]) { + summer += q->CWlengthT[j]; + q->CWlengthT[j] = 0; + } + } + bits += q->skipFlagBits[i]; + summer -= q->skipFlagBits[i]; + } + } + imc_adjust_bit_allocation(q, summer); + + for(i = 0; i < BANDS; i++) { + q->sumLenArr[i] = 0; + + for(j = band_tab[i]; j < band_tab[i+1]; j++) + if (!q->skipFlags[j]) + q->sumLenArr[i] += q->CWlengthT[j]; + } + + memset(q->codewords, 0, sizeof(q->codewords)); + + if(imc_get_coeffs(q) < 0) { + av_log(avctx, AV_LOG_ERROR, "Read coefficients failed\n"); + q->decoder_reset = 1; + return 0; + } + + 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; + } + + memset(q->skipFlags, 0, sizeof(q->skipFlags)); + + imc_imdct256(q); + + q->dsp.float_to_int16(data, q->out_samples, COEFFS); + + *data_size = COEFFS * sizeof(int16_t); + + return avctx->block_align; +} + + +static int imc_decode_close(AVCodecContext * avctx) +{ + IMCContext *q = avctx->priv_data; + + ff_fft_end(&q->fft); + return 0; +} + + +AVCodec imc_decoder = { + .name = "imc", + .type = CODEC_TYPE_AUDIO, + .id = CODEC_ID_IMC, + .priv_data_size = sizeof(IMCContext), + .init = imc_decode_init, + .close = imc_decode_close, + .decode = imc_decode_frame, +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/imcdata.h Mon Oct 30 13:48:48 2006 +0000 @@ -0,0 +1,164 @@ +/* + * IMC compatible decoder + * Copyright (c) 2002-2004 Maxim Poliakovski + * Copyright (c) 2006 Benjamin Larsson + * Copyright (c) 2006 Konstantin Shishkov + * + * This file is part of FFmpeg. + * + * FFmpeg 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, + * 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 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +static const uint16_t band_tab[33] = { + 0, 3, 6, 9, 12, 16, 20, 24, 29, 34, 40, + 46, 53, 60, 68, 76, 84, 93, 102, 111, 121, 131, + 141, 151, 162, 173, 184, 195, 207, 219, 231, 243, 256, +}; + + +static const int8_t cyclTab[32] = { + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, + 12, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31, 32, 32, +}; + +static const int8_t cyclTab2[32] = { + -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, + 12, 13, 14, 15, 16, 17, 17, 18, 19, 20, 21, 22, +23, 24, 25, 26, 27, 28, 29}; + +static const float imc_weights1[31] = { + 0.119595, 0.123124, 0.129192, 9.97377e-2, 8.1923e-2, 9.61153e-2, 8.77885e-2, 8.61174e-2, + 9.00882e-2, 9.91658e-2, 0.112991, 0.131126, 0.152886, 0.177292, 0.221782, 0.244917, 0.267386, + 0.306816, 0.323046, 0.33729, 0.366773, 0.392557, 0.398076, 0.403302, 0.42451, 0.444777, + 0.449188, 0.455445, 0.477853, 0.500669, 0.510395}; + +static const float imc_weights2[31] = { + 3.23466e-3, 3.49886e-3, 3.98413e-3, 1.98116e-3, 1.16465e-3, 1.79283e-3, 1.40372e-3, 1.33274e-3, + 1.50523e-3, 1.95064e-3, 2.77472e-3, 4.14725e-3, 6.2776e-3, 9.36401e-3, 1.71397e-2, 2.24052e-2, + 2.83971e-2, 4.11689e-2, 4.73165e-2, 5.31631e-2, 6.66614e-2, 8.00824e-2, 8.31588e-2, 8.61397e-2, + 9.89229e-2, 0.112197, 0.115227, 0.119613, 0.136174, 0.15445, 0.162685}; + +static const float imc_quantizer1[4][8] = { + { 8.4431201e-1, 4.7358301e-1, 1.448354, 2.7073899e-1, 7.4449003e-1, 1.241991, 1.845484, 0.0}, + { 8.6876702e-1, 4.7659001e-1, 1.478224, 2.5672799e-1, 7.55777e-1, 1.3229851, 2.03438, 0.0}, + { 7.5891501e-1, 6.2272799e-1, 1.271322, 3.47904e-1, 7.5317699e-1, 1.150767, 1.628476, 0.0}, + { 7.65257e-1, 6.44647e-1, 1.263824, 3.4548101e-1, 7.6384902e-1, 1.214466, 1.7638789, 0.0}, +}; + +static const float imc_quantizer2[2][56] = { + { 1.39236e-1, 3.50548e-1, 5.9547901e-1, 8.5772401e-1, 1.121545, 1.3882281, 1.695882, 2.1270809, + 7.2221003e-2, 1.85177e-1, 2.9521701e-1, 4.12568e-1, 5.4068601e-1, 6.7679501e-1, 8.1196898e-1, 9.4765198e-1, + 1.0779999, 1.203415, 1.337265, 1.481871, 1.639982, 1.814766, 2.0701399, 2.449862, + 3.7533998e-2, 1.02722e-1, 1.6021401e-1, 2.16043e-1, 2.7231601e-1, 3.3025399e-1, 3.9022601e-1, 4.52849e-1, + 5.1794899e-1, 5.8529502e-1, 6.53956e-1, 7.2312802e-1, 7.9150802e-1, 8.5891002e-1, 9.28141e-1, 9.9706203e-1, + 1.062153, 1.12564, 1.189834, 1.256122, 1.324469, 1.3955311, 1.468906, 1.545084, + 1.6264729, 1.711524, 1.802705, 1.91023, 2.0533991, 2.22333, 2.4830019, 3.253329 }, + { 1.11654e-1, 3.54469e-1, 6.4232099e-1, 9.6128798e-1, 1.295053, 1.61777, 1.989839, 2.51107, + 5.7721999e-2, 1.69879e-1, 2.97589e-1, 4.3858799e-1, 5.9039903e-1, 7.4934798e-1, 9.1628098e-1, 1.087297, + 1.262751, 1.4288321, 1.6040879, 1.79067, 2.000668, 2.2394669, 2.649332, 5.2760072, + 2.9722e-2, 8.7316997e-2, 1.4445201e-1, 2.04247e-1, 2.6879501e-1, 3.3716801e-1, 4.08811e-1, 4.8306999e-1, + 5.6049401e-1, 6.3955498e-1, 7.2044599e-1, 8.0427998e-1, 8.8933599e-1, 9.7537601e-1, 1.062461, 1.1510431, + 1.240236, 1.326715, 1.412513, 1.500502, 1.591749, 1.686413, 1.785239, 1.891233, + 2.0051291, 2.127681, 2.2709141, 2.475826, 2.7219379, 3.101985, 4.686213, 6.2287788}, +}; + + +static const float xTab[14] = {7.6, 3.6, 4.4, 3.7, 6.1, 5.1, 2.3, 1.6, 6.2, 1.5, 1.8, 1.2, 0, 0}; //10014048 + +/* precomputed table for 10^(i/4), i=-15..16 */ +static const float imc_exp_tab[32] = { + 1.778280e-4, 3.162278e-4, 5.623413e-4, 1.000000e-3, + 1.778280e-3, 3.162278e-3, 5.623413e-3, 1.000000e-2, + 1.778280e-2, 3.162278e-2, 5.623413e-2, 1.000000e-1, + 1.778280e-1, 3.162278e-1, 5.623413e-1, 1.000000e00, + 1.778280e00, 3.162278e00, 5.623413e00, 1.000000e01, + 1.778280e01, 3.162278e01, 5.623413e01, 1.000000e02, + 1.778280e02, 3.162278e02, 5.623413e02, 1.000000e03, + 1.778280e03, 3.162278e03, 5.623413e03, 1.000000e04 +}; +static const float *imc_exp_tab2 = imc_exp_tab + 8; + + +static const uint8_t imc_cb_select[4][32] = { + { 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2 }, + { 0, 2, 0, 3, 2, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, + { 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2 }, + { 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } +}; + +static const uint8_t imc_huffman_sizes[4] = { + 17, 17, 18, 18 +}; + +static const uint8_t imc_huffman_lens[4][4][18] = { + { + { 16, 15, 13, 11, 8, 5, 3, 1, 2, 4, 6, 9, 10, 12, 14, 16, 7, 0 }, + { 10, 8, 7, 6, 4, 4, 3, 2, 2, 3, 4, 6, 7, 9, 11, 11, 7, 0 }, + { 15, 15, 14, 11, 8, 6, 4, 2, 1, 4, 5, 7, 9, 10, 12, 13, 4, 0 }, + { 13, 11, 10, 8, 6, 4, 2, 2, 2, 3, 5, 7, 9, 12, 15, 15, 14, 0 }, + }, + { + { 14, 12, 10, 8, 7, 4, 2, 2, 2, 3, 5, 7, 9, 11, 13, 14, 7, 0 }, + { 14, 13, 11, 8, 6, 4, 3, 2, 2, 3, 5, 7, 9, 10, 12, 14, 3, 0 }, + { 13, 12, 10, 7, 5, 4, 3, 2, 2, 3, 4, 6, 8, 9, 11, 13, 4, 0 }, + { 13, 12, 10, 7, 5, 4, 3, 2, 2, 3, 4, 6, 8, 9, 11, 13, 4, 0 }, + }, + { + { 16, 14, 12, 10, 8, 5, 3, 1, 2, 4, 7, 9, 11, 13, 15, 17, 6, 17 }, + { 15, 13, 11, 8, 6, 4, 2, 2, 2, 3, 5, 7, 10, 12, 14, 16, 9, 16 }, + { 14, 12, 11, 9, 8, 6, 3, 1, 2, 5, 7, 10, 13, 15, 16, 17, 4, 17 }, + { 16, 14, 12, 9, 7, 5, 2, 2, 2, 3, 4, 6, 8, 11, 13, 15, 10, 16 }, + }, + { + { 13, 11, 10, 8, 7, 5, 2, 2, 2, 4, 6, 9, 12, 14, 15, 16, 3, 16 }, + { 11, 11, 10, 9, 8, 7, 5, 4, 3, 3, 3, 3, 3, 3, 4, 5, 6, 5 }, + { 9, 9, 7, 6, 5, 4, 3, 3, 2, 3, 4, 5, 4, 5, 5, 6, 8, 6 }, + { 13, 12, 10, 8, 5, 3, 3, 2, 2, 3, 4, 7, 9, 11, 14, 15, 6, 15 }, + } +}; + +static const uint16_t imc_huffman_bits[4][4][18] = { + { + { 0xCC32, 0x6618, 0x1987, 0x0660, 0x00CD, 0x0018, 0x0007, 0x0000, 0x0002, 0x000D, 0x0032, 0x0199, 0x0331, 0x0CC2, 0x330D, 0xCC33, 0x0067, 0x0000 }, + { 0x02FE, 0x00BE, 0x005E, 0x002D, 0x000A, 0x0009, 0x0003, 0x0003, 0x0000, 0x0002, 0x0008, 0x002C, 0x005D, 0x017E, 0x05FE, 0x05FF, 0x005C, 0x0000 }, + { 0x5169, 0x5168, 0x28B5, 0x0517, 0x00A3, 0x0029, 0x0008, 0x0003, 0x0000, 0x0009, 0x0015, 0x0050, 0x0144, 0x028A, 0x0A2C, 0x145B, 0x000B, 0x0000 }, + { 0x1231, 0x048D, 0x0247, 0x0090, 0x0025, 0x0008, 0x0001, 0x0003, 0x0000, 0x0005, 0x0013, 0x0049, 0x0122, 0x0919, 0x48C3, 0x48C2, 0x2460, 0x0000 }, + }, + { + { 0x2D1D, 0x0B46, 0x02D0, 0x00B5, 0x0059, 0x000A, 0x0003, 0x0001, 0x0000, 0x0004, 0x0017, 0x005B, 0x0169, 0x05A2, 0x168F, 0x2D1C, 0x0058, 0x0000 }, + { 0x1800, 0x0C01, 0x0301, 0x0061, 0x0019, 0x0007, 0x0004, 0x0003, 0x0000, 0x0005, 0x000D, 0x0031, 0x00C1, 0x0181, 0x0601, 0x1801, 0x0002, 0x0000 }, + { 0x1556, 0x0AAA, 0x02AB, 0x0054, 0x0014, 0x000B, 0x0002, 0x0003, 0x0000, 0x0003, 0x0008, 0x002B, 0x00AB, 0x0154, 0x0554, 0x1557, 0x0009, 0x0000 }, + { 0x1556, 0x0AAA, 0x02AB, 0x0054, 0x0014, 0x000B, 0x0002, 0x0003, 0x0000, 0x0003, 0x0008, 0x002B, 0x00AB, 0x0154, 0x0554, 0x1557, 0x0009, 0x0000 }, + }, + { + { 0x2993, 0x0A65, 0x0298, 0x00A7, 0x0028, 0x0004, 0x0000, 0x0001, 0x0001, 0x0003, 0x0015, 0x0052, 0x014D, 0x0533, 0x14C8, 0x5324, 0x000B, 0x5325 }, + { 0x09B8, 0x026F, 0x009A, 0x0012, 0x0005, 0x0000, 0x0001, 0x0002, 0x0003, 0x0001, 0x0003, 0x0008, 0x004C, 0x0136, 0x04DD, 0x1373, 0x0027, 0x1372 }, + { 0x0787, 0x01E0, 0x00F1, 0x003D, 0x001F, 0x0006, 0x0001, 0x0001, 0x0001, 0x0002, 0x000E, 0x0079, 0x03C2, 0x0F0D, 0x1E19, 0x3C30, 0x0000, 0x3C31 }, + { 0x4B06, 0x12C0, 0x04B1, 0x0097, 0x0024, 0x0008, 0x0002, 0x0003, 0x0000, 0x0003, 0x0005, 0x0013, 0x004A, 0x0259, 0x0961, 0x2582, 0x012D, 0x4B07 }, + }, + { + { 0x0A5A, 0x0297, 0x014A, 0x0053, 0x0028, 0x000B, 0x0003, 0x0000, 0x0002, 0x0004, 0x0015, 0x00A4, 0x052C, 0x14B7, 0x296C, 0x52DB, 0x0003, 0x52DA }, + { 0x0193, 0x0192, 0x00C8, 0x0065, 0x0033, 0x0018, 0x0007, 0x0004, 0x0000, 0x0004, 0x0005, 0x0007, 0x0006, 0x0003, 0x0005, 0x0005, 0x000D, 0x0004 }, + { 0x0012, 0x0013, 0x0005, 0x0003, 0x0000, 0x0003, 0x0005, 0x0004, 0x0003, 0x0003, 0x0005, 0x0005, 0x0004, 0x0004, 0x0003, 0x0005, 0x0008, 0x0004 }, + { 0x0D66, 0x06B2, 0x01AD, 0x006A, 0x000C, 0x0005, 0x0004, 0x0000, 0x0003, 0x0002, 0x0007, 0x0034, 0x00D7, 0x0358, 0x1ACF, 0x359C, 0x001B, 0x359D }, + } +}; +