adpcm: convert to new channel layout API

Signed-off-by: Vittorio Giovara <vittorio.giovara@gmail.com>
Signed-off-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: James Almer <jamrial@gmail.com>
release/5.1
Anton Khirnov 12 years ago committed by James Almer
parent 111ed1b16b
commit 984612a403
  1. 151
      libavcodec/adpcm.c
  2. 122
      libavcodec/adpcmenc.c

@ -271,14 +271,15 @@ static av_cold int adpcm_decode_init(AVCodecContext * avctx)
case AV_CODEC_ID_ADPCM_MTAF:
min_channels = 2;
max_channels = 8;
if (avctx->channels & 1) {
avpriv_request_sample(avctx, "channel count %d", avctx->channels);
if (avctx->ch_layout.nb_channels & 1) {
avpriv_request_sample(avctx, "channel count %d", avctx->ch_layout.nb_channels);
return AVERROR_PATCHWELCOME;
}
break;
case AV_CODEC_ID_ADPCM_PSX:
max_channels = 8;
if (avctx->channels <= 0 || avctx->block_align % (16 * avctx->channels))
if (avctx->ch_layout.nb_channels <= 0 ||
avctx->block_align % (16 * avctx->ch_layout.nb_channels))
return AVERROR_INVALIDDATA;
break;
case AV_CODEC_ID_ADPCM_IMA_DAT4:
@ -287,7 +288,8 @@ static av_cold int adpcm_decode_init(AVCodecContext * avctx)
max_channels = 14;
break;
}
if (avctx->channels < min_channels || avctx->channels > max_channels) {
if (avctx->ch_layout.nb_channels < min_channels ||
avctx->ch_layout.nb_channels > max_channels) {
av_log(avctx, AV_LOG_ERROR, "Invalid number of channels\n");
return AVERROR(EINVAL);
}
@ -298,7 +300,8 @@ static av_cold int adpcm_decode_init(AVCodecContext * avctx)
return AVERROR_INVALIDDATA;
break;
case AV_CODEC_ID_ADPCM_ARGO:
if (avctx->bits_per_coded_sample != 4 || avctx->block_align != 17 * avctx->channels)
if (avctx->bits_per_coded_sample != 4 ||
avctx->block_align != 17 * avctx->ch_layout.nb_channels)
return AVERROR_INVALIDDATA;
break;
case AV_CODEC_ID_ADPCM_ZORK:
@ -336,7 +339,7 @@ static av_cold int adpcm_decode_init(AVCodecContext * avctx)
AV_SAMPLE_FMT_S16;
break;
case AV_CODEC_ID_ADPCM_MS:
avctx->sample_fmt = avctx->channels > 2 ? AV_SAMPLE_FMT_S16P :
avctx->sample_fmt = avctx->ch_layout.nb_channels > 2 ? AV_SAMPLE_FMT_S16P :
AV_SAMPLE_FMT_S16;
break;
default:
@ -755,6 +758,7 @@ static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_
ADPCMDecodeContext *c = avctx->priv_data;
GetBitContext gb;
const int8_t *table;
int channels = avctx->ch_layout.nb_channels;
int k0, signmask, nb_bits, count;
int size = buf_size*8;
int i;
@ -767,16 +771,16 @@ static void adpcm_swf_decode(AVCodecContext *avctx, const uint8_t *buf, int buf_
k0 = 1 << (nb_bits-2);
signmask = 1 << (nb_bits-1);
while (get_bits_count(&gb) <= size - 22*avctx->channels) {
for (i = 0; i < avctx->channels; i++) {
while (get_bits_count(&gb) <= size - 22 * channels) {
for (i = 0; i < channels; i++) {
*samples++ = c->status[i].predictor = get_sbits(&gb, 16);
c->status[i].step_index = get_bits(&gb, 6);
}
for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
for (count = 0; get_bits_count(&gb) <= size - nb_bits * channels && count < 4095; count++) {
int i;
for (i = 0; i < avctx->channels; i++) {
for (i = 0; i < channels; i++) {
// similar to IMA adpcm
int delta = get_bits(&gb, nb_bits);
int step = ff_adpcm_step_table[c->status[i].step_index];
@ -840,7 +844,7 @@ static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb,
{
ADPCMDecodeContext *s = avctx->priv_data;
int nb_samples = 0;
int ch = avctx->channels;
int ch = avctx->ch_layout.nb_channels;
int has_coded_samples = 0;
int header_size;
@ -1061,6 +1065,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size;
ADPCMDecodeContext *c = avctx->priv_data;
int channels = avctx->ch_layout.nb_channels;
int16_t *samples;
int16_t **samples_p;
int st; /* stereo */
@ -1089,13 +1094,13 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
frame->nb_samples = nb_samples = coded_samples;
}
st = avctx->channels == 2 ? 1 : 0;
st = channels == 2 ? 1 : 0;
switch(avctx->codec->id) {
CASE(ADPCM_IMA_QT,
/* In QuickTime, IMA is encoded by chunks of 34 bytes (=64 samples).
Channel data is interleaved per-chunk. */
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
ADPCMChannelStatus *cs = &c->status[channel];
int predictor;
int step_index;
@ -1134,7 +1139,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
) /* End of CASE */
CASE(ADPCM_IMA_WAV,
for (int i = 0; i < avctx->channels; i++) {
for (int i = 0; i < channels; i++) {
ADPCMChannelStatus *cs = &c->status[i];
cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(&gb), 16);
@ -1153,12 +1158,12 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
GetBitContext g;
for (int n = 0; n < (nb_samples - 1) / samples_per_block; n++) {
for (int i = 0; i < avctx->channels; i++) {
for (int i = 0; i < channels; i++) {
ADPCMChannelStatus *cs = &c->status[i];
samples = &samples_p[i][1 + n * samples_per_block];
for (int j = 0; j < block_size; j++) {
temp[j] = buf[4 * avctx->channels + block_size * n * avctx->channels +
(j % 4) + (j / 4) * (avctx->channels * 4) + i * 4];
temp[j] = buf[4 * channels + block_size * n * channels +
(j % 4) + (j / 4) * (channels * 4) + i * 4];
}
ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size);
if (ret < 0)
@ -1169,10 +1174,10 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
}
}
bytestream2_skip(&gb, avctx->block_align - avctx->channels * 4);
bytestream2_skip(&gb, avctx->block_align - channels * 4);
} else {
for (int n = 0; n < (nb_samples - 1) / 8; n++) {
for (int i = 0; i < avctx->channels; i++) {
for (int i = 0; i < channels; i++) {
ADPCMChannelStatus *cs = &c->status[i];
samples = &samples_p[i][1 + n * 8];
for (int m = 0; m < 8; m += 2) {
@ -1185,10 +1190,10 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
) /* End of CASE */
CASE(ADPCM_4XM,
for (int i = 0; i < avctx->channels; i++)
for (int i = 0; i < channels; i++)
c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
for (int i = 0; i < avctx->channels; i++) {
for (int i = 0; i < channels; i++) {
c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
if (c->status[i].step_index > 88u) {
av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n",
@ -1197,7 +1202,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
}
for (int i = 0; i < avctx->channels; i++) {
for (int i = 0; i < channels; i++) {
ADPCMChannelStatus *cs = &c->status[i];
samples = (int16_t *)frame->data[i];
for (int n = nb_samples >> 1; n > 0; n--) {
@ -1208,9 +1213,9 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
) /* End of CASE */
CASE(ADPCM_AGM,
for (int i = 0; i < avctx->channels; i++)
for (int i = 0; i < channels; i++)
c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
for (int i = 0; i < avctx->channels; i++)
for (int i = 0; i < channels; i++)
c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16);
for (int n = 0; n < nb_samples >> (1 - st); n++) {
@ -1222,8 +1227,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
CASE(ADPCM_MS,
int block_predictor;
if (avctx->channels > 2) {
for (int channel = 0; channel < avctx->channels; channel++) {
if (avctx->ch_layout.nb_channels > 2) {
for (int channel = 0; channel < avctx->ch_layout.nb_channels; channel++) {
samples = samples_p[channel];
block_predictor = bytestream2_get_byteu(&gb);
if (block_predictor > 6) {
@ -1285,7 +1290,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
) /* End of CASE */
CASE(ADPCM_MTAF,
for (int channel = 0; channel < avctx->channels; channel += 2) {
for (int channel = 0; channel < channels; channel += 2) {
bytestream2_skipu(&gb, 4);
c->status[channel ].step = bytestream2_get_le16u(&gb) & 0x1f;
c->status[channel + 1].step = bytestream2_get_le16u(&gb) & 0x1f;
@ -1306,7 +1311,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
) /* End of CASE */
CASE(ADPCM_IMA_DK4,
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
ADPCMChannelStatus *cs = &c->status[channel];
cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(&gb), 16);
cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
@ -1338,7 +1343,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
int nibble;
int decode_top_nibble_next = 0;
int diff_channel;
const int16_t *samples_end = samples + avctx->channels * nb_samples;
const int16_t *samples_end = samples + channels * nb_samples;
bytestream2_skipu(&gb, 10);
c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
@ -1385,7 +1390,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
bytestream2_skip(&gb, 1);
) /* End of CASE */
CASE(ADPCM_IMA_ISS,
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
ADPCMChannelStatus *cs = &c->status[channel];
cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
@ -1412,7 +1417,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
) /* End of CASE */
CASE(ADPCM_IMA_MOFLEX,
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
ADPCMChannelStatus *cs = &c->status[channel];
cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
@ -1424,7 +1429,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
for (int subframe = 0; subframe < nb_samples / 256; subframe++) {
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
samples = samples_p[channel] + 256 * subframe;
for (int n = 0; n < 256; n += 2) {
int v = bytestream2_get_byteu(&gb);
@ -1435,7 +1440,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
) /* End of CASE */
CASE(ADPCM_IMA_DAT4,
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
ADPCMChannelStatus *cs = &c->status[channel];
samples = samples_p[channel];
bytestream2_skip(&gb, 4);
@ -1462,26 +1467,26 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
) /* End of CASE */
CASE(ADPCM_IMA_APM,
for (int n = nb_samples / 2; n > 0; n--) {
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
int v = bytestream2_get_byteu(&gb);
*samples++ = adpcm_ima_qt_expand_nibble(&c->status[channel], v >> 4 );
samples[st] = adpcm_ima_qt_expand_nibble(&c->status[channel], v & 0x0F);
}
samples += avctx->channels;
samples += channels;
}
) /* End of CASE */
CASE(ADPCM_IMA_ALP,
for (int n = nb_samples / 2; n > 0; n--) {
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
int v = bytestream2_get_byteu(&gb);
*samples++ = adpcm_ima_alp_expand_nibble(&c->status[channel], v >> 4 , 2);
samples[st] = adpcm_ima_alp_expand_nibble(&c->status[channel], v & 0x0F, 2);
}
samples += avctx->channels;
samples += channels;
}
) /* End of CASE */
CASE(ADPCM_IMA_CUNNING,
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
int16_t *smp = samples_p[channel];
for (int n = 0; n < nb_samples / 2; n++) {
int v = bytestream2_get_byteu(&gb);
@ -1498,7 +1503,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
) /* End of CASE */
CASE(ADPCM_IMA_RAD,
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
ADPCMChannelStatus *cs = &c->status[channel];
cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16);
cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
@ -1514,17 +1519,17 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
byte[0] = bytestream2_get_byteu(&gb);
if (st)
byte[1] = bytestream2_get_byteu(&gb);
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
*samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3);
}
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
*samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] >> 4 , 3);
}
}
) /* End of CASE */
CASE(ADPCM_IMA_WS,
if (c->vqa_version == 3) {
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
int16_t *smp = samples_p[channel];
for (int n = nb_samples / 2; n > 0; n--) {
@ -1535,12 +1540,12 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
} else {
for (int n = nb_samples / 2; n > 0; n--) {
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
int v = bytestream2_get_byteu(&gb);
*samples++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3);
samples[st] = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3);
}
samples += avctx->channels;
samples += channels;
}
}
bytestream2_seek(&gb, 0, SEEK_END);
@ -1548,13 +1553,13 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
CASE(ADPCM_XA,
int16_t *out0 = samples_p[0];
int16_t *out1 = samples_p[1];
int samples_per_block = 28 * (3 - avctx->channels) * 4;
int samples_per_block = 28 * (3 - channels) * 4;
int sample_offset = 0;
int bytes_remaining;
while (bytestream2_get_bytes_left(&gb) >= 128) {
if ((ret = xa_decode(avctx, out0, out1, buf + bytestream2_tell(&gb),
&c->status[0], &c->status[1],
avctx->channels, sample_offset)) < 0)
channels, sample_offset)) < 0)
return ret;
bytestream2_skipu(&gb, 128);
sample_offset += samples_per_block;
@ -1604,7 +1609,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
/* Each EA ADPCM frame has a 12-byte header followed by 30-byte pieces,
each coding 28 stereo samples. */
if(avctx->channels != 2)
if (channels != 2)
return AVERROR_INVALIDDATA;
current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16);
@ -1649,7 +1654,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
CASE(ADPCM_EA_MAXIS_XA,
int coeff[2][2], shift[2];
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
int byte = bytestream2_get_byteu(&gb);
for (int i = 0; i < 2; i++)
coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i];
@ -1661,7 +1666,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
byte[0] = bytestream2_get_byteu(&gb);
if (st) byte[1] = bytestream2_get_byteu(&gb);
for (int i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
int sample = sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]);
sample = (sample +
c->status[channel].sample1 * coeff[channel][0] +
@ -1690,12 +1695,12 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
int count = 0;
int offsets[6];
for (unsigned channel = 0; channel < avctx->channels; channel++)
for (unsigned channel = 0; channel < channels; channel++)
offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) :
bytestream2_get_le32(&gb)) +
(avctx->channels + 1) * 4;
(channels + 1) * 4;
for (unsigned channel = 0; channel < avctx->channels; channel++) {
for (unsigned channel = 0; channel < channels; channel++) {
int count1;
bytestream2_seek(&gb, offsets[channel], SEEK_SET);
@ -1759,7 +1764,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
#endif /* CONFIG_ADPCM_EA_Rx_DECODER */
CASE(ADPCM_EA_XAS,
for (int channel=0; channel < avctx->channels; channel++) {
for (int channel=0; channel < channels; channel++) {
int coeff[2][4], shift[4];
int16_t *s = samples_p[channel];
for (int n = 0; n < 4; n++, s += 32) {
@ -1791,7 +1796,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
) /* End of CASE */
CASE(ADPCM_IMA_ACORN,
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
ADPCMChannelStatus *cs = &c->status[channel];
cs->predictor = sign_extend(bytestream2_get_le16u(&gb), 16);
cs->step_index = bytestream2_get_le16u(&gb) & 0xFF;
@ -1808,7 +1813,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
) /* End of CASE */
CASE(ADPCM_IMA_AMV,
av_assert0(avctx->channels == 1);
av_assert0(channels == 1);
/*
* Header format:
@ -1848,7 +1853,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
) /* End of CASE */
CASE(ADPCM_IMA_SMJPEG,
for (int i = 0; i < avctx->channels; i++) {
for (int i = 0; i < channels; i++) {
c->status[i].predictor = sign_extend(bytestream2_get_be16u(&gb), 16);
c->status[i].step_index = bytestream2_get_byteu(&gb);
bytestream2_skipu(&gb, 1);
@ -1931,7 +1936,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
) /* End of CASE */
CASE(ADPCM_AICA,
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
samples = samples_p[channel];
for (int n = nb_samples >> 1; n > 0; n--) {
int v = bytestream2_get_byteu(&gb);
@ -1953,7 +1958,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
for (int m = 0; m < blocks; m++) {
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
int prev1 = c->status[channel].sample1;
int prev2 = c->status[channel].sample2;
@ -2005,33 +2010,33 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
if (avctx->extradata) {
GetByteContext tb;
if (avctx->extradata_size < 32 * avctx->channels) {
if (avctx->extradata_size < 32 * channels) {
av_log(avctx, AV_LOG_ERROR, "Missing coeff table\n");
return AVERROR_INVALIDDATA;
}
bytestream2_init(&tb, avctx->extradata, avctx->extradata_size);
for (int i = 0; i < avctx->channels; i++)
for (int i = 0; i < channels; i++)
for (int n = 0; n < 16; n++)
table[i][n] = THP_GET16(tb);
} else {
for (int i = 0; i < avctx->channels; i++)
for (int i = 0; i < channels; i++)
for (int n = 0; n < 16; n++)
table[i][n] = THP_GET16(gb);
if (!c->has_status) {
/* Initialize the previous sample. */
for (int i = 0; i < avctx->channels; i++) {
for (int i = 0; i < channels; i++) {
c->status[i].sample1 = THP_GET16(gb);
c->status[i].sample2 = THP_GET16(gb);
}
c->has_status = 1;
} else {
bytestream2_skip(&gb, avctx->channels * 4);
bytestream2_skip(&gb, channels * 4);
}
}
for (int ch = 0; ch < avctx->channels; ch++) {
for (int ch = 0; ch < channels; ch++) {
samples = samples_p[ch];
/* Read in every sample for this channel. */
@ -2065,7 +2070,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
#endif /* CONFIG_ADPCM_THP(_LE)_DECODER */
CASE(ADPCM_DTK,
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
samples = samples_p[channel];
/* Read in every sample for this channel. */
@ -2113,9 +2118,9 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
) /* End of CASE */
CASE(ADPCM_PSX,
for (int block = 0; block < avpkt->size / FFMAX(avctx->block_align, 16 * avctx->channels); block++) {
int nb_samples_per_block = 28 * FFMAX(avctx->block_align, 16 * avctx->channels) / (16 * avctx->channels);
for (int channel = 0; channel < avctx->channels; channel++) {
for (int block = 0; block < avpkt->size / FFMAX(avctx->block_align, 16 * channels); block++) {
int nb_samples_per_block = 28 * FFMAX(avctx->block_align, 16 * channels) / (16 * channels);
for (int channel = 0; channel < channels; channel++) {
samples = samples_p[channel] + block * nb_samples_per_block;
av_assert0((block + 1) * nb_samples_per_block <= nb_samples);
@ -2172,7 +2177,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
* They should be 0 initially.
*/
for (int block = 0; block < avpkt->size / avctx->block_align; block++) {
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < avctx->ch_layout.nb_channels; channel++) {
ADPCMChannelStatus *cs = c->status + channel;
int control, shift;
@ -2191,19 +2196,19 @@ static int adpcm_decode_frame(AVCodecContext *avctx, void *data,
}
) /* End of CASE */
CASE(ADPCM_ZORK,
for (int n = 0; n < nb_samples * avctx->channels; n++) {
for (int n = 0; n < nb_samples * channels; n++) {
int v = bytestream2_get_byteu(&gb);
*samples++ = adpcm_zork_expand_nibble(&c->status[n % avctx->channels], v);
*samples++ = adpcm_zork_expand_nibble(&c->status[n % channels], v);
}
) /* End of CASE */
CASE(ADPCM_IMA_MTF,
for (int n = nb_samples / 2; n > 0; n--) {
for (int channel = 0; channel < avctx->channels; channel++) {
for (int channel = 0; channel < channels; channel++) {
int v = bytestream2_get_byteu(&gb);
*samples++ = adpcm_ima_mtf_expand_nibble(&c->status[channel], v >> 4);
samples[st] = adpcm_ima_mtf_expand_nibble(&c->status[channel], v & 0x0F);
}
samples += avctx->channels;
samples += channels;
}
) /* End of CASE */
default:

@ -79,11 +79,7 @@ typedef struct ADPCMEncodeContext {
static av_cold int adpcm_encode_init(AVCodecContext *avctx)
{
ADPCMEncodeContext *s = avctx->priv_data;
if (avctx->channels > 2) {
av_log(avctx, AV_LOG_ERROR, "only stereo or mono is supported\n");
return AVERROR(EINVAL);
}
int channels = avctx->ch_layout.nb_channels;
/*
* AMV's block size has to match that of the corresponding video
@ -130,8 +126,8 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
CASE(ADPCM_IMA_WAV,
/* each 16 bits sample gives one nibble
and we have 4 bytes per channel overhead */
avctx->frame_size = (s->block_size - 4 * avctx->channels) * 8 /
(4 * avctx->channels) + 1;
avctx->frame_size = (s->block_size - 4 * channels) * 8 /
(4 * channels) + 1;
/* seems frame_size isn't taken into account...
have to buffer the samples :-( */
avctx->block_align = s->block_size;
@ -139,13 +135,13 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
) /* End of CASE */
CASE(ADPCM_IMA_QT,
avctx->frame_size = 64;
avctx->block_align = 34 * avctx->channels;
avctx->block_align = 34 * channels;
) /* End of CASE */
CASE(ADPCM_MS,
uint8_t *extradata;
/* each 16 bits sample gives one nibble
and we have 7 bytes per channel overhead */
avctx->frame_size = (s->block_size - 7 * avctx->channels) * 2 / avctx->channels + 2;
avctx->frame_size = (s->block_size - 7 * channels) * 2 / channels + 2;
avctx->bits_per_coded_sample = 4;
avctx->block_align = s->block_size;
if (!(avctx->extradata = av_malloc(32 + AV_INPUT_BUFFER_PADDING_SIZE)))
@ -160,7 +156,7 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
}
) /* End of CASE */
CASE(ADPCM_YAMAHA,
avctx->frame_size = s->block_size * 2 / avctx->channels;
avctx->frame_size = s->block_size * 2 / channels;
avctx->block_align = s->block_size;
) /* End of CASE */
CASE(ADPCM_SWF,
@ -172,11 +168,11 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
return AVERROR(EINVAL);
}
avctx->frame_size = 4096; /* Hardcoded according to the SWF spec. */
avctx->block_align = (2 + avctx->channels * (22 + 4 * (avctx->frame_size - 1)) + 7) / 8;
avctx->block_align = (2 + channels * (22 + 4 * (avctx->frame_size - 1)) + 7) / 8;
) /* End of CASE */
case AV_CODEC_ID_ADPCM_IMA_SSI:
case AV_CODEC_ID_ADPCM_IMA_ALP:
avctx->frame_size = s->block_size * 2 / avctx->channels;
avctx->frame_size = s->block_size * 2 / channels;
avctx->block_align = s->block_size;
break;
CASE(ADPCM_IMA_AMV,
@ -185,7 +181,7 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
return AVERROR(EINVAL);
}
if (avctx->channels != 1) {
if (channels != 1) {
av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
return AVERROR(EINVAL);
}
@ -194,7 +190,7 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
avctx->block_align = 8 + (FFALIGN(avctx->frame_size, 2) / 2);
) /* End of CASE */
CASE(ADPCM_IMA_APM,
avctx->frame_size = s->block_size * 2 / avctx->channels;
avctx->frame_size = s->block_size * 2 / channels;
avctx->block_align = s->block_size;
if (!(avctx->extradata = av_mallocz(28 + AV_INPUT_BUFFER_PADDING_SIZE)))
@ -203,11 +199,11 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
) /* End of CASE */
CASE(ADPCM_ARGO,
avctx->frame_size = 32;
avctx->block_align = 17 * avctx->channels;
avctx->block_align = 17 * channels;
) /* End of CASE */
CASE(ADPCM_IMA_WS,
/* each 16 bits sample gives one nibble */
avctx->frame_size = s->block_size * 2 / avctx->channels;
avctx->frame_size = s->block_size * 2 / channels;
avctx->block_align = s->block_size;
) /* End of CASE */
default:
@ -606,16 +602,17 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
int16_t **samples_p;
uint8_t *dst;
ADPCMEncodeContext *c = avctx->priv_data;
int channels = avctx->ch_layout.nb_channels;
samples = (const int16_t *)frame->data[0];
samples_p = (int16_t **)frame->extended_data;
st = avctx->channels == 2;
st = channels == 2;
if (avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_SSI ||
avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_ALP ||
avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_APM ||
avctx->codec_id == AV_CODEC_ID_ADPCM_IMA_WS)
pkt_size = (frame->nb_samples * avctx->channels) / 2;
pkt_size = (frame->nb_samples * channels) / 2;
else
pkt_size = avctx->block_align;
if ((ret = ff_get_encode_buffer(avctx, avpkt, pkt_size, 0)) < 0)
@ -626,7 +623,7 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
CASE(ADPCM_IMA_WAV,
int blocks = (frame->nb_samples - 1) / 8;
for (int ch = 0; ch < avctx->channels; ch++) {
for (int ch = 0; ch < channels; ch++) {
ADPCMChannelStatus *status = &c->status[ch];
status->prev_sample = samples_p[ch][0];
/* status->step_index = 0;
@ -639,15 +636,15 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
/* stereo: 4 bytes (8 samples) for left, 4 bytes for right */
if (avctx->trellis > 0) {
uint8_t *buf;
if (!FF_ALLOC_TYPED_ARRAY(buf, avctx->channels * blocks * 8))
if (!FF_ALLOC_TYPED_ARRAY(buf, channels * blocks * 8))
return AVERROR(ENOMEM);
for (int ch = 0; ch < avctx->channels; ch++) {
for (int ch = 0; ch < channels; ch++) {
adpcm_compress_trellis(avctx, &samples_p[ch][1],
buf + ch * blocks * 8, &c->status[ch],
blocks * 8, 1);
}
for (int i = 0; i < blocks; i++) {
for (int ch = 0; ch < avctx->channels; ch++) {
for (int ch = 0; ch < channels; ch++) {
uint8_t *buf1 = buf + ch * blocks * 8 + i * 8;
for (int j = 0; j < 8; j += 2)
*dst++ = buf1[j] | (buf1[j + 1] << 4);
@ -656,7 +653,7 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
av_free(buf);
} else {
for (int i = 0; i < blocks; i++) {
for (int ch = 0; ch < avctx->channels; ch++) {
for (int ch = 0; ch < channels; ch++) {
ADPCMChannelStatus *status = &c->status[ch];
const int16_t *smp = &samples_p[ch][1 + i * 8];
for (int j = 0; j < 8; j += 2) {
@ -672,7 +669,7 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
PutBitContext pb;
init_put_bits(&pb, dst, pkt_size);
for (int ch = 0; ch < avctx->channels; ch++) {
for (int ch = 0; ch < channels; ch++) {
ADPCMChannelStatus *status = &c->status[ch];
put_bits(&pb, 9, (status->prev_sample & 0xFFFF) >> 7);
put_bits(&pb, 7, status->step_index);
@ -703,7 +700,7 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
av_assert0(avctx->trellis == 0);
for (int i = 0; i < frame->nb_samples; i++) {
for (int ch = 0; ch < avctx->channels; ch++) {
for (int ch = 0; ch < channels; ch++) {
put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
}
}
@ -717,11 +714,11 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
av_assert0(avctx->trellis == 0);
for (int n = frame->nb_samples / 2; n > 0; n--) {
for (int ch = 0; ch < avctx->channels; ch++) {
for (int ch = 0; ch < channels; ch++) {
put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, *samples++));
put_bits(&pb, 4, adpcm_ima_alp_compress_sample(c->status + ch, samples[st]));
}
samples += avctx->channels;
samples += channels;
}
flush_put_bits(&pb);
@ -738,7 +735,7 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
put_bits(&pb, 2, 2); // set 4-bit flash adpcm format
// init the encoder state
for (int i = 0; i < avctx->channels; i++) {
for (int i = 0; i < channels; i++) {
// clip step so it fits 6 bits
c->status[i].step_index = av_clip_uintp2(c->status[i].step_index, 6);
put_sbits(&pb, 16, samples[i]);
@ -748,22 +745,22 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
if (avctx->trellis > 0) {
uint8_t buf[8190 /* = 2 * n */];
adpcm_compress_trellis(avctx, samples + avctx->channels, buf,
&c->status[0], n, avctx->channels);
if (avctx->channels == 2)
adpcm_compress_trellis(avctx, samples + avctx->channels + 1,
adpcm_compress_trellis(avctx, samples + channels, buf,
&c->status[0], n, channels);
if (channels == 2)
adpcm_compress_trellis(avctx, samples + channels + 1,
buf + n, &c->status[1], n,
avctx->channels);
channels);
for (int i = 0; i < n; i++) {
put_bits(&pb, 4, buf[i]);
if (avctx->channels == 2)
if (channels == 2)
put_bits(&pb, 4, buf[n + i]);
}
} else {
for (int i = 1; i < frame->nb_samples; i++) {
put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0],
samples[avctx->channels * i]));
if (avctx->channels == 2)
samples[channels * i]));
if (channels == 2)
put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1],
samples[2 * i + 1]));
}
@ -771,47 +768,47 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
flush_put_bits(&pb);
) /* End of CASE */
CASE(ADPCM_MS,
for (int i = 0; i < avctx->channels; i++) {
for (int i = 0; i < channels; i++) {
int predictor = 0;
*dst++ = predictor;
c->status[i].coeff1 = ff_adpcm_AdaptCoeff1[predictor];
c->status[i].coeff2 = ff_adpcm_AdaptCoeff2[predictor];
}
for (int i = 0; i < avctx->channels; i++) {
for (int i = 0; i < channels; i++) {
if (c->status[i].idelta < 16)
c->status[i].idelta = 16;
bytestream_put_le16(&dst, c->status[i].idelta);
}
for (int i = 0; i < avctx->channels; i++)
for (int i = 0; i < channels; i++)
c->status[i].sample2= *samples++;
for (int i = 0; i < avctx->channels; i++) {
for (int i = 0; i < channels; i++) {
c->status[i].sample1 = *samples++;
bytestream_put_le16(&dst, c->status[i].sample1);
}
for (int i = 0; i < avctx->channels; i++)
for (int i = 0; i < channels; i++)
bytestream_put_le16(&dst, c->status[i].sample2);
if (avctx->trellis > 0) {
const int n = avctx->block_align - 7 * avctx->channels;
const int n = avctx->block_align - 7 * channels;
uint8_t *buf = av_malloc(2 * n);
if (!buf)
return AVERROR(ENOMEM);
if (avctx->channels == 1) {
if (channels == 1) {
adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
avctx->channels);
channels);
for (int i = 0; i < n; i += 2)
*dst++ = (buf[i] << 4) | buf[i + 1];
} else {
adpcm_compress_trellis(avctx, samples, buf,
&c->status[0], n, avctx->channels);
&c->status[0], n, channels);
adpcm_compress_trellis(avctx, samples + 1, buf + n,
&c->status[1], n, avctx->channels);
&c->status[1], n, channels);
for (int i = 0; i < n; i++)
*dst++ = (buf[i] << 4) | buf[n + i];
}
av_free(buf);
} else {
for (int i = 7 * avctx->channels; i < avctx->block_align; i++) {
for (int i = 7 * channels; i < avctx->block_align; i++) {
int nibble;
nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++) << 4;
nibble |= adpcm_ms_compress_sample(&c->status[st], *samples++);
@ -826,22 +823,22 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
if (!buf)
return AVERROR(ENOMEM);
n *= 2;
if (avctx->channels == 1) {
if (channels == 1) {
adpcm_compress_trellis(avctx, samples, buf, &c->status[0], n,
avctx->channels);
channels);
for (int i = 0; i < n; i += 2)
*dst++ = buf[i] | (buf[i + 1] << 4);
} else {
adpcm_compress_trellis(avctx, samples, buf,
&c->status[0], n, avctx->channels);
&c->status[0], n, channels);
adpcm_compress_trellis(avctx, samples + 1, buf + n,
&c->status[1], n, avctx->channels);
&c->status[1], n, channels);
for (int i = 0; i < n; i++)
*dst++ = buf[i] | (buf[n + i] << 4);
}
av_free(buf);
} else
for (n *= avctx->channels; n > 0; n--) {
for (n *= channels; n > 0; n--) {
int nibble;
nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
@ -855,17 +852,17 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
av_assert0(avctx->trellis == 0);
for (int n = frame->nb_samples / 2; n > 0; n--) {
for (int ch = 0; ch < avctx->channels; ch++) {
for (int ch = 0; ch < channels; ch++) {
put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, *samples++));
put_bits(&pb, 4, adpcm_ima_qt_compress_sample(c->status + ch, samples[st]));
}
samples += avctx->channels;
samples += channels;
}
flush_put_bits(&pb);
) /* End of CASE */
CASE(ADPCM_IMA_AMV,
av_assert0(avctx->channels == 1);
av_assert0(channels == 1);
c->status[0].prev_sample = *samples;
bytestream_put_le16(&dst, c->status[0].prev_sample);
@ -880,7 +877,7 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
if (!buf)
return AVERROR(ENOMEM);
adpcm_compress_trellis(avctx, samples, buf, &c->status[0], 2 * n, avctx->channels);
adpcm_compress_trellis(avctx, samples, buf, &c->status[0], 2 * n, channels);
for (int i = 0; i < n; i++)
bytestream_put_byte(&dst, (buf[2 * i] << 4) | buf[2 * i + 1]);
@ -904,7 +901,7 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
av_assert0(frame->nb_samples == 32);
for (int ch = 0; ch < avctx->channels; ch++) {
for (int ch = 0; ch < channels; ch++) {
int64_t error = INT64_MAX, tmperr = INT64_MAX;
int shift = 2, flag = 0;
int saved1 = c->status[ch].sample1;
@ -941,14 +938,14 @@ static int adpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
av_assert0(avctx->trellis == 0);
for (int n = frame->nb_samples / 2; n > 0; n--) {
/* stereo: 1 byte (2 samples) for left, 1 byte for right */
for (int ch = 0; ch < avctx->channels; ch++) {
for (int ch = 0; ch < channels; ch++) {
int t1, t2;
t1 = adpcm_ima_compress_sample(&c->status[ch], *samples++);
t2 = adpcm_ima_compress_sample(&c->status[ch], samples[st]);
put_bits(&pb, 4, t2);
put_bits(&pb, 4, t1);
}
samples += avctx->channels;
samples += channels;
}
flush_put_bits(&pb);
) /* End of CASE */
@ -968,6 +965,12 @@ static const enum AVSampleFormat sample_fmts_p[] = {
AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_NONE
};
static const AVChannelLayout ch_layouts[] = {
AV_CHANNEL_LAYOUT_MONO,
AV_CHANNEL_LAYOUT_STEREO,
{ 0 },
};
static const AVOption options[] = {
{
.name = "block_size",
@ -1001,6 +1004,7 @@ const AVCodec ff_ ## name_ ## _encoder = { \
.encode2 = adpcm_encode_frame, \
.close = adpcm_encode_close, \
.sample_fmts = sample_fmts_, \
.ch_layouts = ch_layouts, \
.capabilities = capabilities_ | AV_CODEC_CAP_DR1, \
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE, \
.priv_class = &adpcm_encoder_class, \

Loading…
Cancel
Save