diff options
Diffstat (limited to 'libavcodec/imc.c')
-rw-r--r-- | libavcodec/imc.c | 90 |
1 files changed, 62 insertions, 28 deletions
diff --git a/libavcodec/imc.c b/libavcodec/imc.c index cb95d39..2fa910a 100644 --- a/libavcodec/imc.c +++ b/libavcodec/imc.c @@ -4,20 +4,20 @@ * Copyright (c) 2006 Benjamin Larsson * Copyright (c) 2006 Konstantin Shishkov * - * This file is part of Libav. + * This file is part of FFmpeg. * - * Libav is free software; you can redistribute it and/or + * 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. * - * Libav is distributed in the hope that it will be useful, + * 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 Libav; if not, write to the Free Software + * License along with FFmpeg; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ @@ -38,6 +38,7 @@ #include "libavutil/channel_layout.h" #include "libavutil/float_dsp.h" #include "libavutil/internal.h" +#include "libavutil/libm.h" #include "avcodec.h" #include "bswapdsp.h" #include "get_bits.h" @@ -95,7 +96,7 @@ typedef struct IMCContext { GetBitContext gb; BswapDSPContext bdsp; - AVFloatDSPContext fdsp; + AVFloatDSPContext *fdsp; FFTContext fft; DECLARE_ALIGNED(32, FFTComplex, samples)[COEFFS / 2]; float *out_samples; @@ -179,6 +180,14 @@ static av_cold int imc_decode_init(AVCodecContext *avctx) IMCContext *q = avctx->priv_data; double r1, r2; + if (avctx->codec_id == AV_CODEC_ID_IAC && avctx->sample_rate > 96000) { + av_log(avctx, AV_LOG_ERROR, + "Strange sample rate of %i, file likely corrupt or " + "needing a new table derivation method.\n", + avctx->sample_rate); + return AVERROR_PATCHWELCOME; + } + if (avctx->codec_id == AV_CODEC_ID_IMC) avctx->channels = 1; @@ -247,7 +256,13 @@ static av_cold int imc_decode_init(AVCodecContext *avctx) return ret; } ff_bswapdsp_init(&q->bdsp); - avpriv_float_dsp_init(&q->fdsp, avctx->flags & CODEC_FLAG_BITEXACT); + q->fdsp = avpriv_float_dsp_alloc(avctx->flags & CODEC_FLAG_BITEXACT); + if (!q->fdsp) { + ff_fft_end(&q->fft); + + return AVERROR(ENOMEM); + } + avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; avctx->channel_layout = avctx->channels == 1 ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO; @@ -356,7 +371,7 @@ static void imc_decode_level_coefficients(IMCContext *q, int *levlCoeffBuf, float tmp, tmp2; // maybe some frequency division thingy - flcoeffs1[0] = 20000.0 / pow (2, levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125 + flcoeffs1[0] = 20000.0 / exp2 (levlCoeffBuf[0] * 0.18945); // 0.18945 = log2(10) * 0.05703125 flcoeffs2[0] = log2f(flcoeffs1[0]); tmp = flcoeffs1[0]; tmp2 = flcoeffs2[0]; @@ -450,8 +465,13 @@ static int bit_allocation(IMCContext *q, IMCChannel *chctx, for (i = 0; i < BANDS; i++) highest = FFMAX(highest, chctx->flcoeffs1[i]); - for (i = 0; i < BANDS - 1; i++) + for (i = 0; i < BANDS - 1; i++) { + if (chctx->flcoeffs5[i] <= 0) { + av_log(NULL, AV_LOG_ERROR, "flcoeffs5 %f invalid\n", chctx->flcoeffs5[i]); + return AVERROR_INVALIDDATA; + } chctx->flcoeffs4[i] = chctx->flcoeffs3[i] - log2f(chctx->flcoeffs5[i]); + } chctx->flcoeffs4[BANDS - 1] = limit; highest = highest * 0.25; @@ -770,7 +790,8 @@ static int inverse_quant_coeff(IMCContext *q, IMCChannel *chctx, } -static int imc_get_coeffs(IMCContext *q, IMCChannel *chctx) +static void imc_get_coeffs(AVCodecContext *avctx, + IMCContext *q, IMCChannel *chctx) { int i, j, cw_len, cw; @@ -782,19 +803,19 @@ 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_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])) { + if (get_bits_count(&q->gb) + cw_len > 512) { + av_log(avctx, AV_LOG_WARNING, + "Potential problem on band %i, coefficient %i" + ": cw_len=%i\n", i, j, cw_len); + } else + cw = get_bits(&q->gb, cw_len); } - if (cw_len && (!chctx->bandFlagsBuf[i] || !chctx->skipFlags[j])) - cw = get_bits(&q->gb, cw_len); - chctx->codewords[j] = cw; } } } - return 0; } static void imc_refine_bit_allocation(IMCContext *q, IMCChannel *chctx) @@ -887,6 +908,13 @@ static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch) imc_decode_level_coefficients2(q, chctx->levlCoeffBuf, chctx->old_floor, chctx->flcoeffs1, chctx->flcoeffs2); + for(i=0; i<BANDS; i++) { + if(chctx->flcoeffs1[i] > INT_MAX) { + av_log(avctx, AV_LOG_ERROR, "scalefactor out of range\n"); + return AVERROR_INVALIDDATA; + } + } + memcpy(chctx->old_floor, chctx->flcoeffs1, 32 * sizeof(float)); counter = 0; @@ -968,11 +996,7 @@ static int imc_decode_block(AVCodecContext *avctx, IMCContext *q, int ch) memset(chctx->codewords, 0, sizeof(chctx->codewords)); - if (imc_get_coeffs(q, chctx) < 0) { - av_log(avctx, AV_LOG_ERROR, "Read coefficients failed\n"); - chctx->decoder_reset = 1; - return AVERROR_INVALIDDATA; - } + imc_get_coeffs(avctx, q, chctx); if (inverse_quant_coeff(q, chctx, stream_format_code) < 0) { av_log(avctx, AV_LOG_ERROR, "Inverse quantization of coefficients failed\n"); @@ -997,7 +1021,7 @@ static int imc_decode_frame(AVCodecContext *avctx, void *data, IMCContext *q = avctx->priv_data; - LOCAL_ALIGNED_16(uint16_t, buf16, [IMC_BLOCK_SIZE / 2]); + LOCAL_ALIGNED_16(uint16_t, buf16, [IMC_BLOCK_SIZE / 2 + FF_INPUT_BUFFER_PADDING_SIZE/2]); if (buf_size < IMC_BLOCK_SIZE * avctx->channels) { av_log(avctx, AV_LOG_ERROR, "frame too small!\n"); @@ -1006,10 +1030,8 @@ static int imc_decode_frame(AVCodecContext *avctx, void *data, /* get output buffer */ frame->nb_samples = COEFFS; - if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) { - av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) return ret; - } for (i = 0; i < avctx->channels; i++) { q->out_samples = (float *)frame->extended_data[i]; @@ -1025,7 +1047,7 @@ static int imc_decode_frame(AVCodecContext *avctx, void *data, } if (avctx->channels == 2) { - q->fdsp.butterflies_float((float *)frame->extended_data[0], + q->fdsp->butterflies_float((float *)frame->extended_data[0], (float *)frame->extended_data[1], COEFFS); } @@ -1034,17 +1056,25 @@ static int imc_decode_frame(AVCodecContext *avctx, void *data, return IMC_BLOCK_SIZE * avctx->channels; } - static av_cold int imc_decode_close(AVCodecContext * avctx) { IMCContext *q = avctx->priv_data; ff_fft_end(&q->fft); + av_freep(&q->fdsp); return 0; } +static av_cold void flush(AVCodecContext *avctx) +{ + IMCContext *q = avctx->priv_data; + + q->chctx[0].decoder_reset = + q->chctx[1].decoder_reset = 1; +} +#if CONFIG_IMC_DECODER AVCodec ff_imc_decoder = { .name = "imc", .long_name = NULL_IF_CONFIG_SMALL("IMC (Intel Music Coder)"), @@ -1054,11 +1084,13 @@ AVCodec ff_imc_decoder = { .init = imc_decode_init, .close = imc_decode_close, .decode = imc_decode_frame, + .flush = flush, .capabilities = CODEC_CAP_DR1, .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE }, }; - +#endif +#if CONFIG_IAC_DECODER AVCodec ff_iac_decoder = { .name = "iac", .long_name = NULL_IF_CONFIG_SMALL("IAC (Indeo Audio Coder)"), @@ -1068,7 +1100,9 @@ AVCodec ff_iac_decoder = { .init = imc_decode_init, .close = imc_decode_close, .decode = imc_decode_frame, + .flush = flush, .capabilities = CODEC_CAP_DR1, .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE }, }; +#endif |