diff options
author | Michael Niedermayer <michaelni@gmx.at> | 2011-11-27 00:30:13 +0100 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2011-11-27 00:30:13 +0100 |
commit | 5c15b78e4a47c46de03d5e7291ed19a717f36885 (patch) | |
tree | a88ba284818fa1b64b1da37872bb4a60bd66a976 /libavcodec/adxdec.c | |
parent | 22cb8e7b3436b6ad12e4e2d588dd8775d2da1684 (diff) | |
parent | 7c5ce99bd92fb480b7235cbc9a005f7e6d31f1d7 (diff) | |
download | ffmpeg-5c15b78e4a47c46de03d5e7291ed19a717f36885.tar.gz |
Merge remote-tracking branch 'qatar/master'
* qatar/master: (42 commits)
swscale: fix signed overflow in yuv2mono_X_c_template
snow: fix integer overflows
svq1enc: remove stale altivec-related hack
snow: fix signed overflow in byte to 32-bit replication
adx: rename ff_adx_decode_header() to avpriv_adx_decode_header()
avformat: add CRI ADX format demuxer
adx: add an ADX parser.
adx: move header decoding to ADX common code
adx: calculate the number of blocks in a packet
adx: define and use 2 new macro constants BLOCK_SIZE and BLOCK_SAMPLES
adx: check for unsupported ADX formats
adx: simplify encoding by using put_sbits()
adx: calculate correct LPC coeffs
adx: use 12-bit coefficients instead of 14-bit to avoid integer overflow
adx: simplify adx_decode() by using get_sbits() to read residual samples
adx: fix the data offset parsing in adx_decode_header()
adx: remove unneeded post-decode channel interleaving
adx: validate header values
adx: cosmetics: general pretty-printing and comment clean-up
adx: remove useless comments
...
Conflicts:
Changelog
libavcodec/cook.c
libavcodec/fraps.c
libavcodec/nuv.c
libavcodec/pthread.c
libavcodec/version.h
libavformat/Makefile
libavformat/version.h
Merged-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavcodec/adxdec.c')
-rw-r--r-- | libavcodec/adxdec.c | 175 |
1 files changed, 72 insertions, 103 deletions
diff --git a/libavcodec/adxdec.c b/libavcodec/adxdec.c index df3e8dd355..0fed1220ef 100644 --- a/libavcodec/adxdec.c +++ b/libavcodec/adxdec.c @@ -22,6 +22,7 @@ #include "libavutil/intreadwrite.h" #include "avcodec.h" #include "adx.h" +#include "get_bits.h" /** * @file @@ -34,136 +35,105 @@ static av_cold int adx_decode_init(AVCodecContext *avctx) { + ADXContext *c = avctx->priv_data; + int ret, header_size; + + if (avctx->extradata_size < 24) + return AVERROR_INVALIDDATA; + + if ((ret = avpriv_adx_decode_header(avctx, avctx->extradata, + avctx->extradata_size, &header_size, + c->coeff)) < 0) { + av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n"); + return AVERROR_INVALIDDATA; + } + c->channels = avctx->channels; + avctx->sample_fmt = AV_SAMPLE_FMT_S16; return 0; } -/* 18 bytes <-> 32 samples */ - -static void adx_decode(short *out,const unsigned char *in,PREV *prev) +/** + * Decode 32 samples from 18 bytes. + * + * A 16-bit scalar value is applied to 32 residuals, which then have a + * 2nd-order LPC filter applied to it to form the output signal for a single + * channel. + */ +static int adx_decode(ADXContext *c, int16_t *out, const uint8_t *in, int ch) { + ADXChannelState *prev = &c->prev[ch]; + GetBitContext gb; int scale = AV_RB16(in); int i; - int s0,s1,s2,d; + int s0, s1, s2, d; -// printf("%x ",scale); + /* check if this is an EOF packet */ + if (scale & 0x8000) + return -1; - in+=2; + init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8); s1 = prev->s1; s2 = prev->s2; - for(i=0;i<16;i++) { - d = in[i]; - // d>>=4; if (d&8) d-=16; - d = ((signed char)d >> 4); - s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14; - s2 = s1; - s1 = av_clip_int16(s0); - *out++=s1; - - d = in[i]; - //d&=15; if (d&8) d-=16; - d = ((signed char)(d<<4) >> 4); - s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14; + for (i = 0; i < BLOCK_SAMPLES; i++) { + d = get_sbits(&gb, 4); + s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS; s2 = s1; s1 = av_clip_int16(s0); - *out++=s1; + *out = s1; + out += c->channels; } prev->s1 = s1; prev->s2 = s2; + return 0; } -static void adx_decode_stereo(short *out,const unsigned char *in,PREV *prev) -{ - short tmp[32*2]; - int i; - - adx_decode(tmp ,in ,prev); - adx_decode(tmp+32,in+18,prev+1); - for(i=0;i<32;i++) { - out[i*2] = tmp[i]; - out[i*2+1] = tmp[i+32]; - } -} - -/* return data offset or 0 */ -static int adx_decode_header(AVCodecContext *avctx,const unsigned char *buf,size_t bufsize) -{ - int offset; - - if (buf[0]!=0x80) return 0; - offset = (AV_RB32(buf)^0x80000000)+4; - if (bufsize<offset || memcmp(buf+offset-6,"(c)CRI",6)) return 0; - - avctx->channels = buf[7]; - avctx->sample_rate = AV_RB32(buf+8); - avctx->bit_rate = avctx->sample_rate*avctx->channels*18*8/32; - - return offset; -} - -static int adx_decode_frame(AVCodecContext *avctx, - void *data, int *data_size, - AVPacket *avpkt) +static int adx_decode_frame(AVCodecContext *avctx, void *data, int *data_size, + AVPacket *avpkt) { - const uint8_t *buf0 = avpkt->data; - int buf_size = avpkt->size; - ADXContext *c = avctx->priv_data; - short *samples = data; - const uint8_t *buf = buf0; - int rest = buf_size; - - if (!c->header_parsed) { - int hdrsize = adx_decode_header(avctx,buf,rest); - if (hdrsize==0) return -1; - c->header_parsed = 1; - buf += hdrsize; - rest -= hdrsize; + int buf_size = avpkt->size; + ADXContext *c = avctx->priv_data; + int16_t *samples = data; + const uint8_t *buf = avpkt->data; + int num_blocks, ch; + + if (c->eof) { + *data_size = 0; + return buf_size; } /* 18 bytes of data are expanded into 32*2 bytes of audio, so guard against buffer overflows */ - if(rest/18 > *data_size/64) - rest = (*data_size/64) * 18; - - if (c->in_temp) { - int copysize = 18*avctx->channels - c->in_temp; - memcpy(c->dec_temp+c->in_temp,buf,copysize); - rest -= copysize; - buf += copysize; - if (avctx->channels==1) { - adx_decode(samples,c->dec_temp,c->prev); - samples += 32; - } else { - adx_decode_stereo(samples,c->dec_temp,c->prev); - samples += 32*2; - } + num_blocks = buf_size / (BLOCK_SIZE * c->channels); + if (num_blocks > *data_size / (BLOCK_SAMPLES * c->channels)) { + buf_size = (*data_size / (BLOCK_SAMPLES * c->channels)) * BLOCK_SIZE; + num_blocks = buf_size / (BLOCK_SIZE * c->channels); } - // - if (avctx->channels==1) { - while(rest>=18) { - adx_decode(samples,buf,c->prev); - rest-=18; - buf+=18; - samples+=32; - } - } else { - while(rest>=18*2) { - adx_decode_stereo(samples,buf,c->prev); - rest-=18*2; - buf+=18*2; - samples+=32*2; + if (!buf_size || buf_size % (BLOCK_SIZE * avctx->channels)) { + if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) { + c->eof = 1; + *data_size = 0; + return avpkt->size; } + return AVERROR_INVALIDDATA; } - // - c->in_temp = rest; - if (rest) { - memcpy(c->dec_temp,buf,rest); - buf+=rest; + + while (num_blocks--) { + for (ch = 0; ch < c->channels; ch++) { + if (adx_decode(c, samples + ch, buf, ch)) { + c->eof = 1; + buf = avpkt->data + avpkt->size; + break; + } + buf_size -= BLOCK_SIZE; + buf += BLOCK_SIZE; + } + samples += BLOCK_SAMPLES * c->channels; } + *data_size = (uint8_t*)samples - (uint8_t*)data; -// printf("%d:%d ",buf-buf0,*data_size); fflush(stdout); - return buf-buf0; + return buf - avpkt->data; } AVCodec ff_adpcm_adx_decoder = { @@ -173,6 +143,5 @@ AVCodec ff_adpcm_adx_decoder = { .priv_data_size = sizeof(ADXContext), .init = adx_decode_init, .decode = adx_decode_frame, - .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"), + .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"), }; - |