X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fadpcm.c;h=dbb57e5bf24a22699e0fe449eeaf5030b893da56;hb=73c44cb2869bfdbea829942eb35efa6d4c4e2f91;hp=d923fbe3a09bde93cda8e1309dd805e7a2ea5f7a;hpb=8a06cb14f9f57f14e0de52451a4554e688bb854a;p=ffmpeg diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c index d923fbe3a09..dbb57e5bf24 100644 --- a/libavcodec/adpcm.c +++ b/libavcodec/adpcm.c @@ -24,7 +24,7 @@ #include "bytestream.h" /** - * @file libavcodec/adpcm.c + * @file * ADPCM codecs. * First version by Francois Revol (revol@free.fr) * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood) @@ -86,10 +86,12 @@ static const int AdaptationTable[] = { 768, 614, 512, 409, 307, 230, 230, 230 }; +/** Divided by 4 to fit in 8-bit integers */ static const uint8_t AdaptCoeff1[] = { 64, 128, 0, 48, 60, 115, 98 }; +/** Divided by 4 to fit in 8-bit integers */ static const int8_t AdaptCoeff2[] = { 0, -64, 0, 16, 0, -52, -58 }; @@ -143,15 +145,36 @@ typedef struct ADPCMChannelStatus { int idelta; } ADPCMChannelStatus; +typedef struct TrellisPath { + int nibble; + int prev; +} TrellisPath; + +typedef struct TrellisNode { + uint32_t ssd; + int path; + int sample1; + int sample2; + int step; +} TrellisNode; + typedef struct ADPCMContext { ADPCMChannelStatus status[6]; + TrellisPath *paths; + TrellisNode *node_buf; + TrellisNode **nodep_buf; } ADPCMContext; +#define FREEZE_INTERVAL 128 + /* XXX: implement encoding */ #if CONFIG_ENCODERS static av_cold int adpcm_encode_init(AVCodecContext *avctx) { + ADPCMContext *s = avctx->priv_data; + uint8_t *extradata; + int i; if (avctx->channels > 2) return -1; /* only stereo or mono =) */ @@ -160,6 +183,14 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx) return -1; } + if (avctx->trellis) { + int frontier = 1 << avctx->trellis; + int max_paths = frontier * FREEZE_INTERVAL; + FF_ALLOC_OR_GOTO(avctx, s->paths, max_paths * sizeof(*s->paths), error); + FF_ALLOC_OR_GOTO(avctx, s->node_buf, 2 * frontier * sizeof(*s->node_buf), error); + FF_ALLOC_OR_GOTO(avctx, s->nodep_buf, 2 * frontier * sizeof(*s->nodep_buf), error); + } + switch(avctx->codec->id) { case CODEC_ID_ADPCM_IMA_WAV: avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */ @@ -175,6 +206,16 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx) avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */ /* and we have 7 bytes per channel overhead */ avctx->block_align = BLKSIZE; + avctx->extradata_size = 32; + extradata = avctx->extradata = av_malloc(avctx->extradata_size); + if (!extradata) + return AVERROR(ENOMEM); + bytestream_put_le16(&extradata, avctx->frame_size); + bytestream_put_le16(&extradata, 7); /* wNumCoef */ + for (i = 0; i < 7; i++) { + bytestream_put_le16(&extradata, AdaptCoeff1[i] * 4); + bytestream_put_le16(&extradata, AdaptCoeff2[i] * 4); + } break; case CODEC_ID_ADPCM_YAMAHA: avctx->frame_size = BLKSIZE * avctx->channels; @@ -185,24 +226,32 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx) avctx->sample_rate != 22050 && avctx->sample_rate != 44100) { av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\n"); - return -1; + goto error; } avctx->frame_size = 512 * (avctx->sample_rate / 11025); break; default: - return -1; - break; + goto error; } avctx->coded_frame= avcodec_alloc_frame(); avctx->coded_frame->key_frame= 1; return 0; +error: + av_freep(&s->paths); + av_freep(&s->node_buf); + av_freep(&s->nodep_buf); + return -1; } static av_cold int adpcm_encode_close(AVCodecContext *avctx) { + ADPCMContext *s = avctx->priv_data; av_freep(&avctx->coded_frame); + av_freep(&s->paths); + av_freep(&s->node_buf); + av_freep(&s->nodep_buf); return 0; } @@ -263,39 +312,23 @@ static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, return nibble; } -typedef struct TrellisPath { - int nibble; - int prev; -} TrellisPath; - -typedef struct TrellisNode { - uint32_t ssd; - int path; - int sample1; - int sample2; - int step; -} TrellisNode; - static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples, uint8_t *dst, ADPCMChannelStatus *c, int n) { -#define FREEZE_INTERVAL 128 //FIXME 6% faster if frontier is a compile-time constant + ADPCMContext *s = avctx->priv_data; const int frontier = 1 << avctx->trellis; const int stride = avctx->channels; const int version = avctx->codec->id; - const int max_paths = frontier*FREEZE_INTERVAL; - TrellisPath paths[max_paths], *p; - TrellisNode node_buf[2][frontier]; - TrellisNode *nodep_buf[2][frontier]; - TrellisNode **nodes = nodep_buf[0]; // nodes[] is always sorted by .ssd - TrellisNode **nodes_next = nodep_buf[1]; + TrellisPath *paths = s->paths, *p; + TrellisNode *node_buf = s->node_buf; + TrellisNode **nodep_buf = s->nodep_buf; + TrellisNode **nodes = nodep_buf; // nodes[] is always sorted by .ssd + TrellisNode **nodes_next = nodep_buf + frontier; int pathn = 0, froze = -1, i, j, k; - assert(!(max_paths&(max_paths-1))); - - memset(nodep_buf, 0, sizeof(nodep_buf)); - nodes[0] = &node_buf[1][0]; + memset(nodep_buf, 0, 2 * frontier * sizeof(*nodep_buf)); + nodes[0] = node_buf + frontier; nodes[0]->ssd = 0; nodes[0]->path = 0; nodes[0]->step = c->step_index; @@ -316,7 +349,7 @@ static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples, } for(i=0; issd) {\ TrellisNode *u = nodes_next[frontier-1];\ if(!u) {\ - assert(pathn < max_paths);\ + assert(pathn < FREEZE_INTERVAL<trellis);\ u = t++;\ u->path = pathn++;\ }\ @@ -441,6 +474,7 @@ static int adpcm_encode_frame(AVCodecContext *avctx, short *samples; unsigned char *dst; ADPCMContext *c = avctx->priv_data; + uint8_t *buf; dst = frame; samples = (short *)data; @@ -467,22 +501,24 @@ static int adpcm_encode_frame(AVCodecContext *avctx, /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */ if(avctx->trellis > 0) { - uint8_t buf[2][n*8]; - adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n*8); + FF_ALLOC_OR_GOTO(avctx, buf, 2*n*8, error); + adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n*8); if(avctx->channels == 2) - adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n*8); + adpcm_compress_trellis(avctx, samples+1, buf + n*8, &c->status[1], n*8); for(i=0; ichannels == 2) { - *dst++ = buf[1][8*i+0] | (buf[1][8*i+1] << 4); - *dst++ = buf[1][8*i+2] | (buf[1][8*i+3] << 4); - *dst++ = buf[1][8*i+4] | (buf[1][8*i+5] << 4); - *dst++ = buf[1][8*i+6] | (buf[1][8*i+7] << 4); + uint8_t *buf1 = buf + n*8; + *dst++ = buf1[8*i+0] | (buf1[8*i+1] << 4); + *dst++ = buf1[8*i+2] | (buf1[8*i+3] << 4); + *dst++ = buf1[8*i+4] | (buf1[8*i+5] << 4); + *dst++ = buf1[8*i+6] | (buf1[8*i+7] << 4); } } + av_free(buf); } else for (; n>0; n--) { *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]); @@ -565,15 +601,16 @@ static int adpcm_encode_frame(AVCodecContext *avctx, } if(avctx->trellis > 0) { - uint8_t buf[2][n]; - adpcm_compress_trellis(avctx, samples+2, buf[0], &c->status[0], n); + FF_ALLOC_OR_GOTO(avctx, buf, 2*n, error); + adpcm_compress_trellis(avctx, samples+2, buf, &c->status[0], n); if (avctx->channels == 2) - adpcm_compress_trellis(avctx, samples+3, buf[1], &c->status[1], n); + adpcm_compress_trellis(avctx, samples+3, buf+n, &c->status[1], n); for(i=0; ichannels == 2) - put_bits(&pb, 4, buf[1][i]); + put_bits(&pb, 4, buf[n+i]); } + av_free(buf); } else { for (i=1; iframe_size; i++) { put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels*i])); @@ -612,18 +649,18 @@ static int adpcm_encode_frame(AVCodecContext *avctx, if(avctx->trellis > 0) { int n = avctx->block_align - 7*avctx->channels; - uint8_t buf[2][n]; + FF_ALLOC_OR_GOTO(avctx, buf, 2*n, error); if(avctx->channels == 1) { - n *= 2; - adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n); + adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n); for(i=0; istatus[0], n); - adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n); + adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n); + adpcm_compress_trellis(avctx, samples+1, buf+n, &c->status[1], n); for(i=0; ichannels; iblock_align; i++) { int nibble; @@ -635,30 +672,29 @@ static int adpcm_encode_frame(AVCodecContext *avctx, case CODEC_ID_ADPCM_YAMAHA: n = avctx->frame_size / 2; if(avctx->trellis > 0) { - uint8_t buf[2][n*2]; + FF_ALLOC_OR_GOTO(avctx, buf, 2*n*2, error); n *= 2; if(avctx->channels == 1) { - adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n); + adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n); for(i=0; istatus[0], n); - adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n); + adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n); + adpcm_compress_trellis(avctx, samples+1, buf+n, &c->status[1], n); for(i=0; i0; n--) { - for(i = 0; i < avctx->channels; i++) { + for (n *= avctx->channels; n>0; n--) { int nibble; - nibble = adpcm_yamaha_compress_sample(&c->status[i], samples[i]); - nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4; + nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++); + nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4; *dst++ = nibble; } - samples += 2 * avctx->channels; - } break; default: + error: return -1; } return dst - frame; @@ -685,6 +721,12 @@ static av_cold int adpcm_decode_init(AVCodecContext * avctx) case CODEC_ID_ADPCM_CT: c->status[0].step = c->status[1].step = 511; break; + case CODEC_ID_ADPCM_IMA_WAV: + if (avctx->bits_per_coded_sample != 4) { + av_log(avctx, AV_LOG_ERROR, "Only 4-bit ADPCM IMA WAV files are supported\n"); + return -1; + } + break; case CODEC_ID_ADPCM_IMA_WS: if (avctx->extradata && avctx->extradata_size == 2 * 4) { c->status[0].predictor = AV_RL32(avctx->extradata); @@ -1209,11 +1251,11 @@ static int adpcm_decode_frame(AVCodecContext *avctx, } break; case CODEC_ID_ADPCM_EA: - samples_in_chunk = AV_RL32(src); - if (samples_in_chunk >= ((buf_size - 12) * 2)) { + if (buf_size < 4 || AV_RL32(src) >= ((buf_size - 12) * 2)) { src += buf_size; break; } + samples_in_chunk = AV_RL32(src); src += 4; current_left_sample = (int16_t)bytestream_get_le16(&src); previous_left_sample = (int16_t)bytestream_get_le16(&src); @@ -1628,14 +1670,14 @@ static int adpcm_decode_frame(AVCodecContext *avctx, #define ADPCM_ENCODER(id,name,long_name_) \ AVCodec name ## _encoder = { \ #name, \ - CODEC_TYPE_AUDIO, \ + AVMEDIA_TYPE_AUDIO, \ id, \ sizeof(ADPCMContext), \ adpcm_encode_init, \ adpcm_encode_frame, \ adpcm_encode_close, \ NULL, \ - .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, \ + .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, \ .long_name = NULL_IF_CONFIG_SMALL(long_name_), \ }; #else @@ -1646,7 +1688,7 @@ AVCodec name ## _encoder = { \ #define ADPCM_DECODER(id,name,long_name_) \ AVCodec name ## _decoder = { \ #name, \ - CODEC_TYPE_AUDIO, \ + AVMEDIA_TYPE_AUDIO, \ id, \ sizeof(ADPCMContext), \ adpcm_decode_init, \