* Simon & Schuster Interactive ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
* Ubisoft ADPCM decoder by Zane van Iperen (zane@zanevaniperen.com)
* High Voltage Software ALP decoder by Zane van Iperen (zane@zanevaniperen.com)
+ * Cunning Developments decoder by Zane van Iperen (zane@zanevaniperen.com)
*
* This file is part of FFmpeg.
*
unsigned int max_channels = 2;
switch(avctx->codec->id) {
+ case AV_CODEC_ID_ADPCM_IMA_CUNNING:
+ max_channels = 1;
+ break;
case AV_CODEC_ID_ADPCM_DTK:
case AV_CODEC_ID_ADPCM_EA:
min_channels = 2;
}
break;
case AV_CODEC_ID_ADPCM_IMA_APM:
- if (avctx->extradata && avctx->extradata_size >= 16) {
- c->status[0].predictor = AV_RL32(avctx->extradata + 0);
- c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 4), 0, 88);
- c->status[1].predictor = AV_RL32(avctx->extradata + 8);
- c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 12), 0, 88);
+ if (avctx->extradata) {
+ if (avctx->extradata_size >= 28) {
+ c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 16), 18);
+ c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 20), 0, 88);
+ c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 4), 18);
+ c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 8), 0, 88);
+ } else if (avctx->extradata_size >= 16) {
+ c->status[0].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 0), 18);
+ c->status[0].step_index = av_clip(AV_RL32(avctx->extradata + 4), 0, 88);
+ c->status[1].predictor = av_clip_intp2(AV_RL32(avctx->extradata + 8), 18);
+ c->status[1].step_index = av_clip(AV_RL32(avctx->extradata + 12), 0, 88);
+ }
}
break;
case AV_CODEC_ID_ADPCM_IMA_WS:
return (int16_t)c->predictor;
}
+static inline int16_t adpcm_ima_cunning_expand_nibble(ADPCMChannelStatus *c, int8_t nibble)
+{
+ int step_index;
+ int predictor;
+ int step;
+
+ nibble = sign_extend(nibble & 0xF, 4);
+
+ step = ff_adpcm_ima_cunning_step_table[c->step_index];
+ step_index = c->step_index + ff_adpcm_ima_cunning_index_table[abs(nibble)];
+ step_index = av_clip(step_index, 0, 60);
+
+ predictor = c->predictor + step * nibble;
+
+ c->predictor = av_clip_int16(predictor);
+ c->step_index = step_index;
+
+ return c->predictor;
+}
+
static inline int16_t adpcm_ima_wav_expand_nibble(ADPCMChannelStatus *c, GetBitContext *gb, int bps)
{
int nibble, step_index, predictor, sign, delta, diff, step, shift;
avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
filter=0;
}
+ if (shift < 0) {
+ avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
+ shift = 0;
+ }
f0 = xa_adpcm_table[filter][0];
f1 = xa_adpcm_table[filter][1];
shift = 12 - (in[5+i*2] & 15);
filter = in[5+i*2] >> 4;
- if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) {
+ if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table) || shift < 0) {
avpriv_request_sample(avctx, "unknown XA-ADPCM filter %d", filter);
filter=0;
}
+ if (shift < 0) {
+ avpriv_request_sample(avctx, "unknown XA-ADPCM shift %d", shift);
+ shift = 0;
+ }
f0 = xa_adpcm_table[filter][0];
f1 = xa_adpcm_table[filter][1];
}
}
-static inline int16_t adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int control, int shift)
+int16_t ff_adpcm_argo_expand_nibble(ADPCMChannelStatus *cs, int nibble, int shift, int flag)
{
- int sample = nibble * (1 << shift);
+ int sample = sign_extend(nibble, 4) * (1 << shift);
- if (control & 0x04)
+ if (flag)
sample += (8 * cs->sample1) - (4 * cs->sample2);
else
sample += 4 * cs->sample1;
/* simple 4-bit adpcm */
case AV_CODEC_ID_ADPCM_CT:
case AV_CODEC_ID_ADPCM_IMA_APC:
+ case AV_CODEC_ID_ADPCM_IMA_CUNNING:
case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
case AV_CODEC_ID_ADPCM_IMA_OKI:
case AV_CODEC_ID_ADPCM_IMA_WS:
samples += avctx->channels;
}
break;
+ case AV_CODEC_ID_ADPCM_IMA_CUNNING:
+ for (n = 0; n < nb_samples / 2; n++) {
+ int v = bytestream2_get_byteu(&gb);
+ *samples++ = adpcm_ima_cunning_expand_nibble(&c->status[0], v & 0x0F);
+ *samples++ = adpcm_ima_cunning_expand_nibble(&c->status[0], v >> 4);
+ }
+ break;
case AV_CODEC_ID_ADPCM_IMA_OKI:
for (n = nb_samples >> (1 - st); n > 0; n--) {
int v = bytestream2_get_byteu(&gb);
int byte = bytestream2_get_byteu(&gb);
int index = (byte >> 4) & 7;
unsigned int exp = byte & 0x0F;
- int factor1 = table[ch][index * 2];
- int factor2 = table[ch][index * 2 + 1];
+ int64_t factor1 = table[ch][index * 2];
+ int64_t factor2 = table[ch][index * 2 + 1];
/* Decode 14 samples. */
for (n = 0; n < 14 && (i * 14 + n < nb_samples); n++) {
* uint4_t right_samples[nb_samples];
*
* Format of the control byte:
- * MSB [SSSSDRRR] LSB
+ * MSB [SSSSRDRR] LSB
* S = (Shift Amount - 2)
* D = Decoder flag.
* R = Reserved
for (n = 0; n < nb_samples / 2; n++) {
int sample = bytestream2_get_byteu(&gb);
- *samples++ = adpcm_argo_expand_nibble(cs, sign_extend(sample >> 4, 4), control, shift);
- *samples++ = adpcm_argo_expand_nibble(cs, sign_extend(sample >> 0, 4), control, shift);
+ *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 4, shift, control & 0x04);
+ *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 0, shift, control & 0x04);
}
}
break;
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_AMV, sample_fmts_s16, adpcm_ima_amv, "ADPCM IMA AMV");
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APC, sample_fmts_s16, adpcm_ima_apc, "ADPCM IMA CRYO APC");
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_APM, sample_fmts_s16, adpcm_ima_apm, "ADPCM IMA Ubisoft APM");
+ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_CUNNING, sample_fmts_s16, adpcm_ima_cunning, "ADPCM IMA Cunning Developments");
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DAT4, sample_fmts_s16, adpcm_ima_dat4, "ADPCM IMA Eurocom DAT4");
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK3, sample_fmts_s16, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
ADPCM_DECODER(AV_CODEC_ID_ADPCM_IMA_DK4, sample_fmts_s16, adpcm_ima_dk4, "ADPCM IMA Duck DK4");