/* 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:
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) {
break;
}
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
+
+ avcodec_get_frame_defaults(&c->frame);
+ avctx->coded_frame = &c->frame;
+
return 0;
}
*coded_samples = 0;
+ if(ch <= 0)
+ return 0;
+
switch (avctx->codec->id) {
/* constant, only check buf_size */
case CODEC_ID_ADPCM_EA_XAS:
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;
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) {
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;
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);
}
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;
/* 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);
coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
src++;
- shift_left = (*src >> 4 ) + 8;
- shift_right = (*src & 0x0F) + 8;
+ shift_left = 20 - (*src >> 4);
+ shift_right = 20 - (*src & 0x0F);
src++;
for (count2 = 0; count2 < 28; count2++) {
- next_left_sample = (int32_t)((*src & 0xF0) << 24) >> shift_left;
- next_right_sample = (int32_t)((*src & 0x0F) << 28) >> shift_right;
+ next_left_sample = sign_extend(*src >> 4, 4) << shift_left;
+ next_right_sample = sign_extend(*src, 4) << shift_right;
src++;
next_left_sample = (next_left_sample +
for(channel = 0; channel < avctx->channels; channel++) {
for (i=0; i<2; i++)
coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
- shift[channel] = (*src & 0x0F) + 8;
+ shift[channel] = 20 - (*src & 0x0F);
src++;
}
for (count1 = 0; count1 < nb_samples / 2; count1++) {
for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
for(channel = 0; channel < avctx->channels; channel++) {
- int32_t sample = (int32_t)(((*(src+channel) >> i) & 0x0F) << 0x1C) >> shift[channel];
+ int32_t sample = sign_extend(src[channel] >> i, 4) << shift[channel];
sample = (sample +
c->status[channel].sample1 * coeff[channel][0] +
c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
} else {
coeff1 = ea_adpcm_table[ *srcC>>4 ];
coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
- shift = (*srcC++ & 0x0F) + 8;
+ shift = 20 - (*srcC++ & 0x0F);
if (srcC > src_end - 14) break;
for (count2=0; count2<28; count2++) {
if (count2 & 1)
- next_sample = (int32_t)((*srcC++ & 0x0F) << 28) >> shift;
+ next_sample = sign_extend(*srcC++, 4) << shift;
else
- next_sample = (int32_t)((*srcC & 0xF0) << 24) >> shift;
+ next_sample = sign_extend(*srcC >> 4, 4) << shift;
next_sample += (current_sample * coeff1) +
(previous_sample * coeff2);
}
}
- out_size = count * 28 * avctx->channels * out_bps;
+ c->frame.nb_samples = count * 28;
src = src_end;
break;
}
for (n=0; n<4; n++, s+=32*avctx->channels) {
for (i=0; i<2; i++)
coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
- shift[n] = (src[2]&0x0F) + 8;
+ shift[n] = 20 - (src[2] & 0x0F);
for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
s2[0] = (src[0]&0xF0) + (src[1]<<8);
}
s = &samples[m*avctx->channels + channel];
for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
- int level = (int32_t)((*src & (0xF0>>i)) << (24+i)) >> shift[n];
+ int level = sign_extend(*src >> (4 - i), 4) << shift[n];
int pred = s2[-1*avctx->channels] * coeff[0][n]
+ s2[-2*avctx->channels] * coeff[1][n];
s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
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;
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++) {
int index = (*src >> 4) & 7;
- unsigned int exp = 28 - (*src++ & 15);
+ unsigned int exp = *src++ & 15;
int factor1 = table[ch][index * 2];
int factor2 = table[ch][index * 2 + 1];
/* Decode 14 samples. */
for (n = 0; n < 14; n++) {
int32_t sampledat;
- if(n&1) sampledat= *src++ <<28;
- else sampledat= (*src&0xF0)<<24;
+ if(n&1) sampledat = sign_extend(*src++, 4);
+ else sampledat = sign_extend(*src >> 4, 4);
sampledat = ((prev[ch][0]*factor1
- + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
+ + prev[ch][1]*factor2) >> 11) + (sampledat << exp);
*samples = av_clip_int16(sampledat);
prev[ch][1] = prev[ch][0];
prev[ch][0] = *samples++;
default:
return -1;
}
- *data_size = out_size;
+
+ *got_frame_ptr = 1;
+ *(AVFrame *)data = c->frame;
+
return src - buf;
}
.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_), \
}