X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;ds=sidebyside;f=libavcodec%2Fadpcm.c;h=c176b5e03deaad3025b8ee47192d43ca1a27040a;hb=4231bbbf4c54c22fbda6832002675b43f24d3802;hp=1281acc33aab5c55c746bf033826429eb4e1c160;hpb=c2553a5585c381971eca55e9e008cf4caee919fe;p=ffmpeg diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c index 1281acc33aa..c176b5e03de 100644 --- a/libavcodec/adpcm.c +++ b/libavcodec/adpcm.c @@ -84,15 +84,20 @@ static const int swf_index_tables[4][16] = { /* end of tables */ typedef struct ADPCMDecodeContext { + AVFrame frame; ADPCMChannelStatus status[6]; } ADPCMDecodeContext; static av_cold int adpcm_decode_init(AVCodecContext * avctx) { ADPCMDecodeContext *c = avctx->priv_data; + unsigned int min_channels = 1; unsigned int max_channels = 2; switch(avctx->codec->id) { + case CODEC_ID_ADPCM_EA: + min_channels = 2; + break; case CODEC_ID_ADPCM_EA_R1: case CODEC_ID_ADPCM_EA_R2: case CODEC_ID_ADPCM_EA_R3: @@ -100,8 +105,9 @@ static av_cold int adpcm_decode_init(AVCodecContext * avctx) max_channels = 6; break; } - if(avctx->channels > max_channels){ - return -1; + if (avctx->channels < min_channels || avctx->channels > max_channels) { + av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n"); + return AVERROR(EINVAL); } switch(avctx->codec->id) { @@ -124,6 +130,10 @@ static av_cold int adpcm_decode_init(AVCodecContext * avctx) break; } avctx->sample_fmt = AV_SAMPLE_FMT_S16; + + avcodec_get_frame_defaults(&c->frame); + avctx->coded_frame = &c->frame; + return 0; } @@ -335,6 +345,9 @@ static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf, *coded_samples = 0; + if(ch <= 0) + return 0; + switch (avctx->codec->id) { /* constant, only check buf_size */ case CODEC_ID_ADPCM_EA_XAS: @@ -501,9 +514,8 @@ static int get_nb_samples(AVCodecContext *avctx, const uint8_t *buf, decode_top_nibble_next = 1; \ } -static int adpcm_decode_frame(AVCodecContext *avctx, - void *data, int *data_size, - AVPacket *avpkt) +static int adpcm_decode_frame(AVCodecContext *avctx, void *data, + int *got_frame_ptr, AVPacket *avpkt) { const uint8_t *buf = avpkt->data; int buf_size = avpkt->size; @@ -514,7 +526,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, const uint8_t *src; int st; /* stereo */ int count1, count2; - int nb_samples, coded_samples, out_bps, out_size; + int nb_samples, coded_samples, ret; nb_samples = get_nb_samples(avctx, buf, buf_size, &coded_samples); if (nb_samples <= 0) { @@ -522,22 +534,22 @@ static int adpcm_decode_frame(AVCodecContext *avctx, return AVERROR_INVALIDDATA; } - out_bps = av_get_bytes_per_sample(avctx->sample_fmt); - out_size = nb_samples * avctx->channels * out_bps; - if (*data_size < out_size) { - av_log(avctx, AV_LOG_ERROR, "output buffer is too small\n"); - return AVERROR(EINVAL); + /* get output buffer */ + c->frame.nb_samples = nb_samples; + if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) { + av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); + return ret; } + samples = (short *)c->frame.data[0]; + /* use coded_samples when applicable */ /* it is always <= nb_samples, so the output buffer will be large enough */ if (coded_samples) { if (coded_samples != nb_samples) av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n"); - nb_samples = coded_samples; - out_size = nb_samples * avctx->channels * out_bps; + c->frame.nb_samples = nb_samples = coded_samples; } - samples = data; src = buf; st = avctx->channels == 2 ? 1 : 0; @@ -576,7 +588,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, cs->step_index = 88; } - samples = (short*)data + channel; + samples = (short *)c->frame.data[0] + channel; for (m = 0; m < 32; m++) { *samples = adpcm_ima_qt_expand_nibble(cs, src[0] & 0x0F, 3); @@ -628,7 +640,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, } for (i = 0; i < avctx->channels; i++) { - samples = (short*)data + i; + samples = (short *)c->frame.data[0] + i; cs = &c->status[i]; for (n = nb_samples >> 1; n > 0; n--, src++) { uint8_t v = *src; @@ -810,6 +822,9 @@ static int adpcm_decode_frame(AVCodecContext *avctx, /* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces, each coding 28 stereo samples. */ + if(avctx->channels != 2) + return AVERROR_INVALIDDATA; + src += 4; // skip sample count (already read) current_left_sample = (int16_t)bytestream_get_le16(&src); @@ -965,7 +980,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, } } - out_size = count * 28 * avctx->channels * out_bps; + c->frame.nb_samples = count * 28; src = src_end; break; } @@ -996,11 +1011,15 @@ static int adpcm_decode_frame(AVCodecContext *avctx, break; case CODEC_ID_ADPCM_IMA_AMV: case CODEC_ID_ADPCM_IMA_SMJPEG: - c->status[0].predictor = (int16_t)bytestream_get_le16(&src); - c->status[0].step_index = bytestream_get_le16(&src); - - if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV) - src+=4; + if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV) { + c->status[0].predictor = sign_extend(bytestream_get_le16(&src), 16); + c->status[0].step_index = bytestream_get_le16(&src); + src += 4; + } else { + c->status[0].predictor = sign_extend(bytestream_get_be16(&src), 16); + c->status[0].step_index = bytestream_get_byte(&src); + src += 1; + } for (n = nb_samples >> (1 - st); n > 0; n--, src++) { char hi, lo; @@ -1144,7 +1163,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, prev[0][i] = (int16_t)bytestream_get_be16(&src); for (ch = 0; ch <= st; ch++) { - samples = (unsigned short *) data + ch; + samples = (short *)c->frame.data[0] + ch; /* Read in every sample for this channel. */ for (i = 0; i < nb_samples / 14; i++) { @@ -1177,7 +1196,10 @@ static int adpcm_decode_frame(AVCodecContext *avctx, default: return -1; } - *data_size = out_size; + + *got_frame_ptr = 1; + *(AVFrame *)data = c->frame; + return src - buf; } @@ -1190,6 +1212,7 @@ AVCodec ff_ ## name_ ## _decoder = { \ .priv_data_size = sizeof(ADPCMDecodeContext), \ .init = adpcm_decode_init, \ .decode = adpcm_decode_frame, \ + .capabilities = CODEC_CAP_DR1, \ .long_name = NULL_IF_CONFIG_SMALL(long_name_), \ }