From 19789bca53548d672bff30b88a8838edaa876bdb Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Tue, 15 Aug 2023 15:25:22 +0200 Subject: [PATCH 4/4] avcodec/adpcm: consume all input when decoding Stops multiple decoding calls for single packet. Also makes decoding faster. Signed-off-by: Paul B Mahol --- libavcodec/adpcm.c | 388 +++++++++++++------------ tests/ref/fate/adpcm-creative-8-2.6bit | 2 +- tests/ref/fate/adpcm-creative-8-2bit | 2 +- tests/ref/fate/adpcm-creative-8-4bit | 2 +- tests/ref/fate/adpcm-ms-mono | 60 +--- 5 files changed, 227 insertions(+), 227 deletions(-) diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c index 9993c9e531..520c76a6be 100644 --- a/libavcodec/adpcm.c +++ b/libavcodec/adpcm.c @@ -1066,43 +1066,29 @@ static int get_nb_samples(AVCodecContext *avctx, GetByteContext *gb, return nb_samples; } -static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, - int *got_frame_ptr, AVPacket *avpkt) +static int adpcm_decode_block(AVCodecContext *avctx, + int16_t *samples, int16_t **samples_p, + GetByteContext *gb) { - 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 */ + const int st = channels == 2 ? 1 : 0; /* stereo */ int nb_samples, coded_samples, approx_nb_samples, ret; - GetByteContext gb; - bytestream2_init(&gb, buf, buf_size); - nb_samples = get_nb_samples(avctx, &gb, buf_size, &coded_samples, &approx_nb_samples); + nb_samples = get_nb_samples(avctx, gb, bytestream2_get_bytes_left(gb), &coded_samples, &approx_nb_samples); if (nb_samples <= 0) { av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n"); return AVERROR_INVALIDDATA; } - /* get output buffer */ - frame->nb_samples = nb_samples; - if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) - return ret; - samples = (int16_t *)frame->data[0]; - samples_p = (int16_t **)frame->extended_data; - /* use coded_samples when applicable */ /* it is always <= nb_samples, so the output buffer will be large enough */ if (coded_samples) { if (!approx_nb_samples && coded_samples != nb_samples) av_log(avctx, AV_LOG_WARNING, "mismatch in coded sample count\n"); - frame->nb_samples = nb_samples = coded_samples; + nb_samples = coded_samples; } - 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). @@ -1114,7 +1100,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, /* (pppppp) (piiiiiii) */ /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */ - predictor = sign_extend(bytestream2_get_be16u(&gb), 16); + predictor = sign_extend(bytestream2_get_be16u(gb), 16); step_index = predictor & 0x7F; predictor &= ~0x7F; @@ -1139,7 +1125,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, samples = samples_p[channel]; for (int m = 0; m < 64; m += 2) { - int byte = bytestream2_get_byteu(&gb); + int byte = bytestream2_get_byteu(gb); samples[m ] = adpcm_ima_qt_expand_nibble(cs, byte & 0x0F); samples[m + 1] = adpcm_ima_qt_expand_nibble(cs, byte >> 4 ); } @@ -1148,9 +1134,9 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, CASE(ADPCM_IMA_WAV, 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); + cs->predictor = samples_p[i][0] = sign_extend(bytestream2_get_le16u(gb), 16); - cs->step_index = sign_extend(bytestream2_get_le16u(&gb), 16); + cs->step_index = sign_extend(bytestream2_get_le16u(gb), 16); if (cs->step_index > 88u){ av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n", i, cs->step_index); @@ -1162,6 +1148,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, int samples_per_block = ff_adpcm_ima_block_samples[avctx->bits_per_coded_sample - 2]; int block_size = ff_adpcm_ima_block_sizes[avctx->bits_per_coded_sample - 2]; uint8_t temp[20 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 }; + const uint8_t *buf = gb->buffer; GetBitContext g; for (int n = 0; n < (nb_samples - 1) / samples_per_block; n++) { @@ -1169,7 +1156,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, 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 * channels + block_size * n * channels + + temp[j] = buf[block_size * n * channels + (j % 4) + (j / 4) * (channels * 4) + i * 4]; } ret = init_get_bits8(&g, (const uint8_t *)&temp, block_size); @@ -1181,14 +1168,14 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } } } - bytestream2_skip(&gb, avctx->block_align - 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 < channels; i++) { ADPCMChannelStatus *cs = &c->status[i]; samples = &samples_p[i][1 + n * 8]; for (int m = 0; m < 8; m += 2) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); samples[m ] = adpcm_ima_expand_nibble(cs, v & 0x0F, 3); samples[m + 1] = adpcm_ima_expand_nibble(cs, v >> 4 , 3); } @@ -1198,10 +1185,10 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, ) /* End of CASE */ CASE(ADPCM_4XM, for (int i = 0; i < channels; i++) - c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16); + c->status[i].predictor = sign_extend(bytestream2_get_le16u(gb), 16); for (int i = 0; i < channels; i++) { - c->status[i].step_index = sign_extend(bytestream2_get_le16u(&gb), 16); + 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", i, c->status[i].step_index); @@ -1213,7 +1200,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, ADPCMChannelStatus *cs = &c->status[i]; samples = samples_p[i]; for (int n = nb_samples >> 1; n > 0; n--) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 4); *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 4); } @@ -1221,12 +1208,12 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, ) /* End of CASE */ CASE(ADPCM_AGM, for (int i = 0; i < channels; i++) - c->status[i].predictor = sign_extend(bytestream2_get_le16u(&gb), 16); + c->status[i].predictor = sign_extend(bytestream2_get_le16u(gb), 16); for (int i = 0; i < channels; i++) - c->status[i].step = sign_extend(bytestream2_get_le16u(&gb), 16); + c->status[i].step = sign_extend(bytestream2_get_le16u(gb), 16); for (int n = 0; n < nb_samples >> (1 - st); n++) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); *samples++ = adpcm_agm_expand_nibble(&c->status[0], v & 0xF); *samples++ = adpcm_agm_expand_nibble(&c->status[st], v >> 4 ); } @@ -1238,7 +1225,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, for (int channel = 0; channel < avctx->ch_layout.nb_channels; channel++) { ADPCMChannelStatus *status = &c->status[channel]; samples = samples_p[channel]; - block_predictor = bytestream2_get_byteu(&gb); + block_predictor = bytestream2_get_byteu(gb); if (block_predictor > 6) { av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[%d] = %d\n", channel, block_predictor); @@ -1246,13 +1233,13 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } status->coeff1 = ff_adpcm_AdaptCoeff1[block_predictor]; status->coeff2 = ff_adpcm_AdaptCoeff2[block_predictor]; - status->idelta = sign_extend(bytestream2_get_le16u(&gb), 16); - status->sample1 = sign_extend(bytestream2_get_le16u(&gb), 16); - status->sample2 = sign_extend(bytestream2_get_le16u(&gb), 16); + status->idelta = sign_extend(bytestream2_get_le16u(gb), 16); + status->sample1 = sign_extend(bytestream2_get_le16u(gb), 16); + status->sample2 = sign_extend(bytestream2_get_le16u(gb), 16); samples[0] = status->sample2; samples[1] = status->sample1; for (int n = 2; n < nb_samples; n += 2) { - int byte = bytestream2_get_byteu(&gb); + int byte = bytestream2_get_byteu(gb); samples[n ] = adpcm_ms_expand_nibble(status, byte >> 4 ); samples[n+1] = adpcm_ms_expand_nibble(status, byte & 0x0F); } @@ -1260,7 +1247,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } else { ADPCMChannelStatus *status0 = &c->status[0]; ADPCMChannelStatus *status1 = &c->status[st]; - block_predictor = bytestream2_get_byteu(&gb); + block_predictor = bytestream2_get_byteu(gb); if (block_predictor > 6) { av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[0] = %d\n", block_predictor); @@ -1269,7 +1256,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, status0->coeff1 = ff_adpcm_AdaptCoeff1[block_predictor]; status0->coeff2 = ff_adpcm_AdaptCoeff2[block_predictor]; if (st) { - block_predictor = bytestream2_get_byteu(&gb); + block_predictor = bytestream2_get_byteu(gb); if (block_predictor > 6) { av_log(avctx, AV_LOG_ERROR, "ERROR: block_predictor[1] = %d\n", block_predictor); @@ -1278,22 +1265,22 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, status1->coeff1 = ff_adpcm_AdaptCoeff1[block_predictor]; status1->coeff2 = ff_adpcm_AdaptCoeff2[block_predictor]; } - status0->idelta = sign_extend(bytestream2_get_le16u(&gb), 16); + status0->idelta = sign_extend(bytestream2_get_le16u(gb), 16); if (st){ - status1->idelta = sign_extend(bytestream2_get_le16u(&gb), 16); + status1->idelta = sign_extend(bytestream2_get_le16u(gb), 16); } - status0->sample1 = sign_extend(bytestream2_get_le16u(&gb), 16); - if (st) status1->sample1 = sign_extend(bytestream2_get_le16u(&gb), 16); - status0->sample2 = sign_extend(bytestream2_get_le16u(&gb), 16); - if (st) status1->sample2 = sign_extend(bytestream2_get_le16u(&gb), 16); + status0->sample1 = sign_extend(bytestream2_get_le16u(gb), 16); + if (st) status1->sample1 = sign_extend(bytestream2_get_le16u(gb), 16); + status0->sample2 = sign_extend(bytestream2_get_le16u(gb), 16); + if (st) status1->sample2 = sign_extend(bytestream2_get_le16u(gb), 16); *samples++ = status0->sample2; if (st) *samples++ = status1->sample2; *samples++ = status0->sample1; if (st) *samples++ = status1->sample1; for (int n = 0; n < (nb_samples - 2) * (st + 1); n += 2) { - int byte = bytestream2_get_byteu(&gb); + int byte = bytestream2_get_byteu(gb); samples[n ] = adpcm_ms_expand_nibble(status0, byte >> 4 ); samples[n+1] = adpcm_ms_expand_nibble(status1, byte & 0x0F); } @@ -1301,20 +1288,20 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, ) /* End of CASE */ CASE(ADPCM_MTAF, 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; - c->status[channel ].predictor = sign_extend(bytestream2_get_le16u(&gb), 16); - bytestream2_skipu(&gb, 2); - c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16); - bytestream2_skipu(&gb, 2); + bytestream2_skipu(gb, 4); + c->status[channel ].step = bytestream2_get_le16u(gb) & 0x1f; + c->status[channel + 1].step = bytestream2_get_le16u(gb) & 0x1f; + c->status[channel ].predictor = sign_extend(bytestream2_get_le16u(gb), 16); + bytestream2_skipu(gb, 2); + c->status[channel + 1].predictor = sign_extend(bytestream2_get_le16u(gb), 16); + bytestream2_skipu(gb, 2); for (int n = 0; n < nb_samples; n += 2) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); samples_p[channel][n ] = adpcm_mtaf_expand_nibble(&c->status[channel], v & 0x0F); samples_p[channel][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel], v >> 4 ); } for (int n = 0; n < nb_samples; n += 2) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); samples_p[channel + 1][n ] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v & 0x0F); samples_p[channel + 1][n + 1] = adpcm_mtaf_expand_nibble(&c->status[channel + 1], v >> 4 ); } @@ -1323,8 +1310,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, CASE(ADPCM_IMA_DK4, 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); + cs->predictor = *samples++ = sign_extend(bytestream2_get_le16u(gb), 16); + cs->step_index = sign_extend(bytestream2_get_le16u(gb), 16); if (cs->step_index > 88u){ av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n", channel, cs->step_index); @@ -1332,7 +1319,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } } for (int n = (nb_samples - 1) >> (1 - st); n > 0; n--) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); *samples++ = adpcm_ima_expand_nibble(&c->status[0 ], v >> 4 , 3); *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3); } @@ -1344,7 +1331,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, nibble = last_byte >> 4; \ decode_top_nibble_next = 0; \ } else { \ - last_byte = bytestream2_get_byteu(&gb); \ + last_byte = bytestream2_get_byteu(gb); \ nibble = last_byte & 0x0F; \ decode_top_nibble_next = 1; \ } @@ -1355,11 +1342,11 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, int diff_channel; const int16_t *samples_end = samples + channels * nb_samples; - bytestream2_skipu(&gb, 10); - c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16); - c->status[1].predictor = sign_extend(bytestream2_get_le16u(&gb), 16); - c->status[0].step_index = bytestream2_get_byteu(&gb); - c->status[1].step_index = bytestream2_get_byteu(&gb); + bytestream2_skipu(gb, 10); + c->status[0].predictor = sign_extend(bytestream2_get_le16u(gb), 16); + c->status[1].predictor = sign_extend(bytestream2_get_le16u(gb), 16); + c->status[0].step_index = bytestream2_get_byteu(gb); + c->status[1].step_index = bytestream2_get_byteu(gb); if (c->status[0].step_index > 88u || c->status[1].step_index > 88u){ av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i/%i\n", c->status[0].step_index, c->status[1].step_index); @@ -1396,14 +1383,14 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, *samples++ = c->status[0].predictor - c->status[1].predictor; } - if ((bytestream2_tell(&gb) & 1)) - bytestream2_skip(&gb, 1); + if ((bytestream2_tell(gb) & 1)) + bytestream2_skip(gb, 1); ) /* End of CASE */ CASE(ADPCM_IMA_ISS, 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); + cs->predictor = sign_extend(bytestream2_get_le16u(gb), 16); + cs->step_index = sign_extend(bytestream2_get_le16u(gb), 16); if (cs->step_index > 88u){ av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n", channel, cs->step_index); @@ -1413,7 +1400,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, for (int n = nb_samples >> (1 - st); n > 0; n--) { int v1, v2; - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); /* nibbles are swapped for mono */ if (st) { v1 = v >> 4; @@ -1429,8 +1416,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, CASE(ADPCM_IMA_MOFLEX, 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); + cs->step_index = sign_extend(bytestream2_get_le16u(gb), 16); + cs->predictor = sign_extend(bytestream2_get_le16u(gb), 16); if (cs->step_index > 88u){ av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n", channel, cs->step_index); @@ -1442,7 +1429,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, 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); + int v = bytestream2_get_byteu(gb); *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3); *samples++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3); } @@ -1453,9 +1440,9 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, for (int channel = 0; channel < channels; channel++) { ADPCMChannelStatus *cs = &c->status[channel]; samples = samples_p[channel]; - bytestream2_skip(&gb, 4); + bytestream2_skip(gb, 4); for (int n = 0; n < nb_samples; n += 2) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); *samples++ = adpcm_ima_expand_nibble(cs, v >> 4 , 3); *samples++ = adpcm_ima_expand_nibble(cs, v & 0x0F, 3); } @@ -1463,14 +1450,14 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, ) /* End of CASE */ CASE(ADPCM_IMA_APC, for (int n = nb_samples >> (1 - st); n > 0; n--) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4 , 3); *samples++ = adpcm_ima_expand_nibble(&c->status[st], v & 0x0F, 3); } ) /* End of CASE */ CASE(ADPCM_IMA_SSI, for (int n = nb_samples >> (1 - st); n > 0; n--) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0], v >> 4 ); *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0x0F); } @@ -1478,7 +1465,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, CASE(ADPCM_IMA_APM, for (int n = nb_samples / 2; n > 0; n--) { for (int channel = 0; channel < channels; channel++) { - int v = bytestream2_get_byteu(&gb); + 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); } @@ -1488,7 +1475,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, CASE(ADPCM_IMA_ALP, for (int n = nb_samples / 2; n > 0; n--) { for (int channel = 0; channel < channels; channel++) { - int v = bytestream2_get_byteu(&gb); + 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); } @@ -1499,7 +1486,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, 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); + int v = bytestream2_get_byteu(gb); *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v & 0x0F); *smp++ = adpcm_ima_cunning_expand_nibble(&c->status[channel], v >> 4); } @@ -1507,7 +1494,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, ) /* End of CASE */ CASE(ADPCM_IMA_OKI, for (int n = nb_samples >> (1 - st); n > 0; n--) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); *samples++ = adpcm_ima_oki_expand_nibble(&c->status[0], v >> 4 ); *samples++ = adpcm_ima_oki_expand_nibble(&c->status[st], v & 0x0F); } @@ -1515,8 +1502,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, CASE(ADPCM_IMA_RAD, 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); + cs->step_index = sign_extend(bytestream2_get_le16u(gb), 16); + cs->predictor = sign_extend(bytestream2_get_le16u(gb), 16); if (cs->step_index > 88u){ av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n", channel, cs->step_index); @@ -1526,9 +1513,9 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, for (int n = 0; n < nb_samples / 2; n++) { int byte[2]; - byte[0] = bytestream2_get_byteu(&gb); + byte[0] = bytestream2_get_byteu(gb); if (st) - byte[1] = bytestream2_get_byteu(&gb); + byte[1] = bytestream2_get_byteu(gb); for (int channel = 0; channel < channels; channel++) { *samples++ = adpcm_ima_expand_nibble(&c->status[channel], byte[channel] & 0x0F, 3); } @@ -1543,7 +1530,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, int16_t *smp = samples_p[channel]; for (int n = nb_samples / 2; n > 0; n--) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v & 0x0F, 3); *smp++ = adpcm_ima_expand_nibble(&c->status[channel], v >> 4 , 3); } @@ -1551,32 +1538,32 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } else { for (int n = nb_samples / 2; n > 0; n--) { for (int channel = 0; channel < channels; channel++) { - int v = bytestream2_get_byteu(&gb); + 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 += channels; } } - bytestream2_seek(&gb, 0, SEEK_END); + bytestream2_seek(gb, 0, SEEK_END); ) /* End of CASE */ CASE(ADPCM_XMD, int bytes_remaining, block = 0; - while (bytestream2_get_bytes_left(&gb) >= 21 * channels) { + while (bytestream2_get_bytes_left(gb) >= 21 * channels) { for (int channel = 0; channel < channels; channel++) { int16_t *out = samples_p[channel] + block * 32; int16_t history[2]; uint16_t scale; - history[1] = sign_extend(bytestream2_get_le16(&gb), 16); - history[0] = sign_extend(bytestream2_get_le16(&gb), 16); - scale = bytestream2_get_le16(&gb); + history[1] = sign_extend(bytestream2_get_le16(gb), 16); + history[0] = sign_extend(bytestream2_get_le16(gb), 16); + scale = bytestream2_get_le16(gb); out[0] = history[1]; out[1] = history[0]; for (int n = 0; n < 15; n++) { - unsigned byte = bytestream2_get_byte(&gb); + unsigned byte = bytestream2_get_byte(gb); int32_t nibble[2]; nibble[0] = sign_extend(byte & 15, 4); @@ -1594,9 +1581,9 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, block++; } - bytes_remaining = bytestream2_get_bytes_left(&gb); + bytes_remaining = bytestream2_get_bytes_left(gb); if (bytes_remaining > 0) { - bytestream2_skip(&gb, bytes_remaining); + bytestream2_skip(gb, bytes_remaining); } ) /* End of CASE */ CASE(ADPCM_XA, @@ -1605,24 +1592,24 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, 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), + while (bytestream2_get_bytes_left(gb) >= 128) { + if ((ret = xa_decode(avctx, out0, out1, gb->buffer, &c->status[0], &c->status[1], channels, sample_offset)) < 0) return ret; - bytestream2_skipu(&gb, 128); + bytestream2_skipu(gb, 128); sample_offset += samples_per_block; } /* Less than a full block of data left, e.g. when reading from * 2324 byte per sector XA; the remainder is padding */ - bytes_remaining = bytestream2_get_bytes_left(&gb); + bytes_remaining = bytestream2_get_bytes_left(gb); if (bytes_remaining > 0) { - bytestream2_skip(&gb, bytes_remaining); + bytestream2_skip(gb, bytes_remaining); } ) /* End of CASE */ CASE(ADPCM_IMA_EA_EACS, for (int i = 0; i <= st; i++) { - c->status[i].step_index = bytestream2_get_le32u(&gb); + c->status[i].step_index = bytestream2_get_le32u(gb); if (c->status[i].step_index > 88u) { av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n", i, c->status[i].step_index); @@ -1630,20 +1617,20 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } } for (int i = 0; i <= st; i++) { - c->status[i].predictor = bytestream2_get_le32u(&gb); + c->status[i].predictor = bytestream2_get_le32u(gb); if (FFABS((int64_t)c->status[i].predictor) > (1<<16)) return AVERROR_INVALIDDATA; } for (int n = nb_samples >> (1 - st); n > 0; n--) { - int byte = bytestream2_get_byteu(&gb); + int byte = bytestream2_get_byteu(gb); *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 3); *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 3); } ) /* End of CASE */ CASE(ADPCM_IMA_EA_SEAD, for (int n = nb_samples >> (1 - st); n > 0; n--) { - int byte = bytestream2_get_byteu(&gb); + int byte = bytestream2_get_byteu(gb); *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte >> 4, 6); *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte & 0x0F, 6); } @@ -1661,24 +1648,24 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, if (channels != 2) return AVERROR_INVALIDDATA; - current_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16); - previous_left_sample = sign_extend(bytestream2_get_le16u(&gb), 16); - current_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16); - previous_right_sample = sign_extend(bytestream2_get_le16u(&gb), 16); + current_left_sample = sign_extend(bytestream2_get_le16u(gb), 16); + previous_left_sample = sign_extend(bytestream2_get_le16u(gb), 16); + current_right_sample = sign_extend(bytestream2_get_le16u(gb), 16); + previous_right_sample = sign_extend(bytestream2_get_le16u(gb), 16); for (int count1 = 0; count1 < nb_samples / 28; count1++) { - int byte = bytestream2_get_byteu(&gb); + int byte = bytestream2_get_byteu(gb); coeff1l = ea_adpcm_table[ byte >> 4 ]; coeff2l = ea_adpcm_table[(byte >> 4 ) + 4]; coeff1r = ea_adpcm_table[ byte & 0x0F]; coeff2r = ea_adpcm_table[(byte & 0x0F) + 4]; - byte = bytestream2_get_byteu(&gb); + byte = bytestream2_get_byteu(gb); shift_left = 20 - (byte >> 4); shift_right = 20 - (byte & 0x0F); for (int count2 = 0; count2 < 28; count2++) { - byte = bytestream2_get_byteu(&gb); + byte = bytestream2_get_byteu(gb); next_left_sample = sign_extend(byte >> 4, 4) * (1 << shift_left); next_right_sample = sign_extend(byte, 4) * (1 << shift_right); @@ -1698,13 +1685,13 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } } - bytestream2_skip(&gb, 2); // Skip terminating 0x0000 + bytestream2_skip(gb, 2); // Skip terminating 0x0000 ) /* End of CASE */ CASE(ADPCM_EA_MAXIS_XA, int coeff[2][2], shift[2]; for (int channel = 0; channel < channels; channel++) { - int byte = bytestream2_get_byteu(&gb); + int byte = bytestream2_get_byteu(gb); for (int i = 0; i < 2; i++) coeff[channel][i] = ea_adpcm_table[(byte >> 4) + 4*i]; shift[channel] = 20 - (byte & 0x0F); @@ -1712,8 +1699,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, for (int count1 = 0; count1 < nb_samples / 2; count1++) { int byte[2]; - byte[0] = bytestream2_get_byteu(&gb); - if (st) byte[1] = bytestream2_get_byteu(&gb); + 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 < channels; channel++) { int sample = sign_extend(byte[channel] >> i, 4) * (1 << shift[channel]); @@ -1726,7 +1713,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } } } - bytestream2_seek(&gb, 0, SEEK_END); + bytestream2_seek(gb, 0, SEEK_END); ) /* End of CASE */ #if CONFIG_ADPCM_EA_R1_DECODER || CONFIG_ADPCM_EA_R2_DECODER || CONFIG_ADPCM_EA_R3_DECODER case AV_CODEC_ID_ADPCM_EA_R1: @@ -1745,32 +1732,32 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, int offsets[6]; for (unsigned channel = 0; channel < channels; channel++) - offsets[channel] = (big_endian ? bytestream2_get_be32(&gb) : - bytestream2_get_le32(&gb)) + + offsets[channel] = (big_endian ? bytestream2_get_be32(gb) : + bytestream2_get_le32(gb)) + (channels + 1) * 4; for (unsigned channel = 0; channel < channels; channel++) { int count1; - bytestream2_seek(&gb, offsets[channel], SEEK_SET); + bytestream2_seek(gb, offsets[channel], SEEK_SET); samplesC = samples_p[channel]; if (avctx->codec->id == AV_CODEC_ID_ADPCM_EA_R1) { - current_sample = sign_extend(bytestream2_get_le16(&gb), 16); - previous_sample = sign_extend(bytestream2_get_le16(&gb), 16); + current_sample = sign_extend(bytestream2_get_le16(gb), 16); + previous_sample = sign_extend(bytestream2_get_le16(gb), 16); } else { current_sample = c->status[channel].predictor; previous_sample = c->status[channel].prev_sample; } for (count1 = 0; count1 < nb_samples / 28; count1++) { - int byte = bytestream2_get_byte(&gb); + int byte = bytestream2_get_byte(gb); if (byte == 0xEE) { /* only seen in R2 and R3 */ - current_sample = sign_extend(bytestream2_get_be16(&gb), 16); - previous_sample = sign_extend(bytestream2_get_be16(&gb), 16); + current_sample = sign_extend(bytestream2_get_be16(gb), 16); + previous_sample = sign_extend(bytestream2_get_be16(gb), 16); for (int count2 = 0; count2 < 28; count2++) - *samplesC++ = sign_extend(bytestream2_get_be16(&gb), 16); + *samplesC++ = sign_extend(bytestream2_get_be16(gb), 16); } else { coeff1 = ea_adpcm_table[ byte >> 4 ]; coeff2 = ea_adpcm_table[(byte >> 4) + 4]; @@ -1780,7 +1767,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, if (count2 & 1) next_sample = (unsigned)sign_extend(byte, 4) << shift; else { - byte = bytestream2_get_byte(&gb); + byte = bytestream2_get_byte(gb); next_sample = (unsigned)sign_extend(byte >> 4, 4) << shift; } @@ -1807,8 +1794,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } } - frame->nb_samples = count * 28; - bytestream2_seek(&gb, 0, SEEK_END); + nb_samples = count * 28; + bytestream2_seek(gb, 0, SEEK_END); break; } #endif /* CONFIG_ADPCM_EA_Rx_DECODER */ @@ -1817,12 +1804,12 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, int coeff[2][4], shift[4]; int16_t *s = samples_p[channel]; for (int n = 0; n < 4; n++, s += 32) { - int val = sign_extend(bytestream2_get_le16u(&gb), 16); + int val = sign_extend(bytestream2_get_le16u(gb), 16); for (int i = 0; i < 2; i++) coeff[i][n] = ea_adpcm_table[(val&0x0F)+4*i]; s[0] = val & ~0x0F; - val = sign_extend(bytestream2_get_le16u(&gb), 16); + val = sign_extend(bytestream2_get_le16u(gb), 16); shift[n] = 20 - (val & 0x0F); s[1] = val & ~0x0F; } @@ -1831,7 +1818,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, s = &samples_p[channel][m]; for (int n = 0; n < 4; n++, s += 32) { int level, pred; - int byte = bytestream2_get_byteu(&gb); + int byte = bytestream2_get_byteu(gb); level = sign_extend(byte >> 4, 4) * (1 << shift[n]); pred = s[-1] * coeff[0][n] + s[-2] * coeff[1][n]; @@ -1847,8 +1834,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, CASE(ADPCM_IMA_ACORN, 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; + cs->predictor = sign_extend(bytestream2_get_le16u(gb), 16); + cs->step_index = bytestream2_get_le16u(gb) & 0xFF; if (cs->step_index > 88u){ av_log(avctx, AV_LOG_ERROR, "ERROR: step_index[%d] = %i\n", channel, cs->step_index); @@ -1856,7 +1843,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } } for (int n = nb_samples >> (1 - st); n > 0; n--) { - int byte = bytestream2_get_byteu(&gb); + int byte = bytestream2_get_byteu(gb); *samples++ = adpcm_ima_expand_nibble(&c->status[0], byte & 0x0F, 3); *samples++ = adpcm_ima_expand_nibble(&c->status[st], byte >> 4, 3); } @@ -1874,9 +1861,9 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, * Some implementations have step_index as 16-bits, but others * only use the lower 8 and store garbage in the upper 8. */ - c->status[0].predictor = sign_extend(bytestream2_get_le16u(&gb), 16); - c->status[0].step_index = bytestream2_get_byteu(&gb); - bytestream2_skipu(&gb, 5); + c->status[0].predictor = sign_extend(bytestream2_get_le16u(gb), 16); + c->status[0].step_index = bytestream2_get_byteu(gb); + bytestream2_skipu(gb, 5); if (c->status[0].step_index > 88u) { av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", c->status[0].step_index); @@ -1884,14 +1871,14 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } for (int n = nb_samples >> 1; n > 0; n--) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3); *samples++ = adpcm_ima_expand_nibble(&c->status[0], v & 0xf, 3); } if (nb_samples & 1) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); *samples++ = adpcm_ima_expand_nibble(&c->status[0], v >> 4, 3); if (v & 0x0F) { @@ -1903,9 +1890,9 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, ) /* End of CASE */ CASE(ADPCM_IMA_SMJPEG, 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); + c->status[i].predictor = sign_extend(bytestream2_get_be16u(gb), 16); + c->status[i].step_index = bytestream2_get_byteu(gb); + bytestream2_skipu(gb, 1); if (c->status[i].step_index > 88u) { av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", c->status[i].step_index); @@ -1914,7 +1901,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } for (int n = nb_samples >> (1 - st); n > 0; n--) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); *samples++ = adpcm_ima_qt_expand_nibble(&c->status[0 ], v >> 4 ); *samples++ = adpcm_ima_qt_expand_nibble(&c->status[st], v & 0xf); @@ -1922,7 +1909,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, ) /* End of CASE */ CASE(ADPCM_CT, for (int n = nb_samples >> (1 - st); n > 0; n--) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); *samples++ = adpcm_ct_expand_nibble(&c->status[0 ], v >> 4 ); *samples++ = adpcm_ct_expand_nibble(&c->status[st], v & 0x0F); } @@ -1934,15 +1921,15 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, case AV_CODEC_ID_ADPCM_SBPRO_2: if (!c->status[0].step_index) { /* the first byte is a raw sample */ - *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80); + *samples++ = 128 * (bytestream2_get_byteu(gb) - 0x80); if (st) - *samples++ = 128 * (bytestream2_get_byteu(&gb) - 0x80); + *samples++ = 128 * (bytestream2_get_byteu(gb) - 0x80); c->status[0].step_index = 1; nb_samples--; } if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_4) { for (int n = nb_samples >> (1 - st); n > 0; n--) { - int byte = bytestream2_get_byteu(&gb); + int byte = bytestream2_get_byteu(gb); *samples++ = adpcm_sbpro_expand_nibble(&c->status[0], byte >> 4, 4, 0); *samples++ = adpcm_sbpro_expand_nibble(&c->status[st], @@ -1950,7 +1937,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } } else if (avctx->codec->id == AV_CODEC_ID_ADPCM_SBPRO_3) { for (int n = (nb_samples< 0; n--) { - int byte = bytestream2_get_byteu(&gb); + int byte = bytestream2_get_byteu(gb); *samples++ = adpcm_sbpro_expand_nibble(&c->status[0], byte >> 5 , 3, 0); *samples++ = adpcm_sbpro_expand_nibble(&c->status[0], @@ -1960,7 +1947,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } } else { for (int n = nb_samples >> (2 - st); n > 0; n--) { - int byte = bytestream2_get_byteu(&gb); + int byte = bytestream2_get_byteu(gb); *samples++ = adpcm_sbpro_expand_nibble(&c->status[0], byte >> 6 , 2, 2); *samples++ = adpcm_sbpro_expand_nibble(&c->status[st], @@ -1974,12 +1961,12 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, break; #endif /* CONFIG_ADPCM_SBPRO_x_DECODER */ CASE(ADPCM_SWF, - adpcm_swf_decode(avctx, buf, buf_size, samples); - bytestream2_seek(&gb, 0, SEEK_END); + adpcm_swf_decode(avctx, gb->buffer_start, bytestream2_size(gb), samples); + bytestream2_seek(gb, 0, SEEK_END); ) /* End of CASE */ CASE(ADPCM_YAMAHA, for (int n = nb_samples >> (1 - st); n > 0; n--) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); *samples++ = adpcm_yamaha_expand_nibble(&c->status[0 ], v & 0x0F); *samples++ = adpcm_yamaha_expand_nibble(&c->status[st], v >> 4 ); } @@ -1988,7 +1975,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, 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); + int v = bytestream2_get_byteu(gb); *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v & 0x0F); *samples++ = adpcm_yamaha_expand_nibble(&c->status[channel], v >> 4 ); } @@ -2014,7 +2001,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, samples = samples_p[channel] + m * 16; /* Read in every sample for this channel. */ for (int i = 0; i < samples_per_block; i++) { - int byte = bytestream2_get_byteu(&gb); + int byte = bytestream2_get_byteu(gb); int scale = 1 << (byte >> 4); int index = byte & 0xf; int factor1 = afc_coeffs[0][index]; @@ -2027,7 +2014,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, if (n & 1) { sampledat = sign_extend(byte, 4); } else { - byte = bytestream2_get_byteu(&gb); + byte = bytestream2_get_byteu(gb); sampledat = sign_extend(byte >> 4, 4); } @@ -2043,7 +2030,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, c->status[channel].sample2 = prev2; } } - bytestream2_seek(&gb, 0, SEEK_END); + bytestream2_seek(gb, 0, SEEK_END); ) /* End of CASE */ #if CONFIG_ADPCM_THP_DECODER || CONFIG_ADPCM_THP_LE_DECODER case AV_CODEC_ID_ADPCM_THP: @@ -2054,8 +2041,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, #define THP_GET16(g) \ sign_extend( \ avctx->codec->id == AV_CODEC_ID_ADPCM_THP_LE ? \ - bytestream2_get_le16u(&(g)) : \ - bytestream2_get_be16u(&(g)), 16) + bytestream2_get_le16u((g)) : \ + bytestream2_get_be16u((g)), 16) if (avctx->extradata) { GetByteContext tb; @@ -2067,7 +2054,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, bytestream2_init(&tb, avctx->extradata, avctx->extradata_size); for (int i = 0; i < channels; i++) for (int n = 0; n < 16; n++) - table[i][n] = THP_GET16(tb); + table[i][n] = THP_GET16(&tb); } else { for (int i = 0; i < channels; i++) for (int n = 0; n < 16; n++) @@ -2081,7 +2068,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } c->has_status = 1; } else { - bytestream2_skip(&gb, channels * 4); + bytestream2_skip(gb, channels * 4); } } @@ -2090,7 +2077,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, /* Read in every sample for this channel. */ for (int i = 0; i < (nb_samples + 13) / 14; i++) { - int byte = bytestream2_get_byteu(&gb); + int byte = bytestream2_get_byteu(gb); int index = (byte >> 4) & 7; unsigned int exp = byte & 0x0F; int64_t factor1 = table[ch][index * 2]; @@ -2103,7 +2090,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, if (n & 1) { sampledat = sign_extend(byte, 4); } else { - byte = bytestream2_get_byteu(&gb); + byte = bytestream2_get_byteu(gb); sampledat = sign_extend(byte >> 4, 4); } @@ -2126,9 +2113,9 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, for (int i = 0; i < nb_samples / 28; i++) { int byte, header; if (channel) - bytestream2_skipu(&gb, 1); - header = bytestream2_get_byteu(&gb); - bytestream2_skipu(&gb, 3 - channel); + bytestream2_skipu(gb, 1); + header = bytestream2_get_byteu(gb); + bytestream2_skipu(gb, 3 - channel); /* Decode 28 samples. */ for (int n = 0; n < 28; n++) { @@ -2150,7 +2137,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, prev = av_clip_intp2((prev + 0x20) >> 6, 21); - byte = bytestream2_get_byteu(&gb); + byte = bytestream2_get_byteu(gb); if (!channel) sampledat = sign_extend(byte, 4); else @@ -2163,11 +2150,11 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, } } if (!channel) - bytestream2_seek(&gb, 0, SEEK_SET); + bytestream2_seek(gb, 0, SEEK_SET); } ) /* End of CASE */ CASE(ADPCM_PSX, - for (int block = 0; block < avpkt->size / FFMAX(avctx->block_align, 16 * channels); block++) { + for (int block = 0; block < bytestream2_size(gb) / 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; @@ -2177,12 +2164,12 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, for (int i = 0; i < nb_samples_per_block / 28; i++) { int filter, shift, flag, byte; - filter = bytestream2_get_byteu(&gb); + filter = bytestream2_get_byteu(gb); shift = filter & 0xf; filter = filter >> 4; if (filter >= FF_ARRAY_ELEMS(xa_adpcm_table)) return AVERROR_INVALIDDATA; - flag = bytestream2_get_byteu(&gb) & 0x7; + flag = bytestream2_get_byteu(gb) & 0x7; /* Decode 28 samples. */ for (int n = 0; n < 28; n++) { @@ -2191,7 +2178,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, if (n & 1) { scale = sign_extend(byte >> 4, 4); } else { - byte = bytestream2_get_byteu(&gb); + byte = bytestream2_get_byteu(gb); scale = sign_extend(byte, 4); } @@ -2225,7 +2212,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, * Each block relies on the previous two samples of each channel. * They should be 0 initially. */ - for (int block = 0; block < avpkt->size / avctx->block_align; block++) { + for (int block = 0; block < bytestream2_size(gb) / avctx->block_align; block++) { for (int channel = 0; channel < avctx->ch_layout.nb_channels; channel++) { ADPCMChannelStatus *cs = c->status + channel; int control, shift; @@ -2233,11 +2220,11 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, samples = samples_p[channel] + block * 32; /* Get the control byte and decode the samples, 2 at a time. */ - control = bytestream2_get_byteu(&gb); + control = bytestream2_get_byteu(gb); shift = (control >> 4) + 2; for (int n = 0; n < 16; n++) { - int sample = bytestream2_get_byteu(&gb); + int sample = bytestream2_get_byteu(gb); *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 4, shift, control & 0x04); *samples++ = ff_adpcm_argo_expand_nibble(cs, sample >> 0, shift, control & 0x04); } @@ -2246,14 +2233,14 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, ) /* End of CASE */ CASE(ADPCM_ZORK, for (int n = 0; n < nb_samples * channels; n++) { - int v = bytestream2_get_byteu(&gb); + int v = bytestream2_get_byteu(gb); *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 < channels; channel++) { - int v = bytestream2_get_byteu(&gb); + 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); } @@ -2264,6 +2251,51 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, av_assert0(0); // unsupported codec_id should not happen } + return nb_samples; +} + +static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, + int *got_frame_ptr, AVPacket *avpkt) +{ + const uint8_t *buf = avpkt->data; + const int channels = avctx->ch_layout.nb_channels; + int nb_samples, coded_samples, approx_nb_samples, ret; + int buf_size = avpkt->size; + int nb_blocks = avctx->block_align > 0 ? FFMAX(1, buf_size / avctx->block_align) : 1; + int16_t *extended_data[14]; + int frame_nb_samples = 0; + GetByteContext gb; + + bytestream2_init(&gb, buf, buf_size); + + nb_samples = get_nb_samples(avctx, &gb, bytestream2_get_bytes_left(&gb), &coded_samples, &approx_nb_samples); + if (nb_samples <= 0) { + av_log(avctx, AV_LOG_ERROR, "invalid number of samples in packet\n"); + return AVERROR_INVALIDDATA; + } + + /* get output buffer */ + frame->nb_samples = nb_samples * nb_blocks; + if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) + return ret; + + bytestream2_seek(&gb, 0, SEEK_SET); + for (int block = 0; block < nb_blocks; block++) { + for (int ch = 0; ch < channels; ch++) + extended_data[ch] = ((int16_t *)frame->extended_data[ch]) + frame_nb_samples; + + ret = adpcm_decode_block(avctx, ((int16_t *)frame->data[0]) + frame_nb_samples * channels, + extended_data, &gb); + if (ret < 0) + return ret; + frame_nb_samples += ret; + + if (bytestream2_get_bytes_left(&gb) <= 0) + break; + } + + frame->nb_samples = frame_nb_samples; + if (avpkt->size && bytestream2_tell(&gb) == 0) { av_log(avctx, AV_LOG_ERROR, "Nothing consumed\n"); return AVERROR_INVALIDDATA; @@ -2271,10 +2303,8 @@ static int adpcm_decode_frame(AVCodecContext *avctx, AVFrame *frame, *got_frame_ptr = 1; - if (avpkt->size < bytestream2_tell(&gb)) { + if (avpkt->size < bytestream2_tell(&gb)) av_log(avctx, AV_LOG_ERROR, "Overread of %d < %d\n", avpkt->size, bytestream2_tell(&gb)); - return avpkt->size; - } return bytestream2_tell(&gb); } diff --git a/tests/ref/fate/adpcm-creative-8-2.6bit b/tests/ref/fate/adpcm-creative-8-2.6bit index 835760448b..f4619d1c7a 100644 --- a/tests/ref/fate/adpcm-creative-8-2.6bit +++ b/tests/ref/fate/adpcm-creative-8-2.6bit @@ -1 +1 @@ -dee4417597abc2db70a175d6809870e7 +1a3fce8740f2d3f7b22ba7df23691a63 diff --git a/tests/ref/fate/adpcm-creative-8-2bit b/tests/ref/fate/adpcm-creative-8-2bit index 4166846dcb..c747cba5fc 100644 --- a/tests/ref/fate/adpcm-creative-8-2bit +++ b/tests/ref/fate/adpcm-creative-8-2bit @@ -1 +1 @@ -832846066fbce28821b7f0717c4d3a90 +d81563d1c4960f060038e9b62037ecbb diff --git a/tests/ref/fate/adpcm-creative-8-4bit b/tests/ref/fate/adpcm-creative-8-4bit index 2d579aa91b..f66c59ec5b 100644 --- a/tests/ref/fate/adpcm-creative-8-4bit +++ b/tests/ref/fate/adpcm-creative-8-4bit @@ -1 +1 @@ -1813d196cef83f7030bb150399b2903e +a89b291b8a6a33edf459a56655587da7 diff --git a/tests/ref/fate/adpcm-ms-mono b/tests/ref/fate/adpcm-ms-mono index 9823ba77b5..10521548ef 100644 --- a/tests/ref/fate/adpcm-ms-mono +++ b/tests/ref/fate/adpcm-ms-mono @@ -3,48 +3,18 @@ #codec_id 0: pcm_s16le #sample_rate 0: 11025 #channel_layout_name 0: mono -0, 0, 0, 500, 1000, 0x64cd9403 -0, 500, 500, 500, 1000, 0xa4ef8a9d -0, 1000, 1000, 500, 1000, 0x75c19868 -0, 1500, 1500, 500, 1000, 0x93db6f79 -0, 2000, 2000, 500, 1000, 0x6835625d -0, 2500, 2500, 500, 1000, 0xb3affa8f -0, 3000, 3000, 500, 1000, 0x159fdcc8 -0, 3500, 3500, 500, 1000, 0x79f9f7f1 -0, 4000, 4000, 500, 1000, 0xd7d1131e -0, 4500, 4500, 500, 1000, 0x52a6f797 -0, 5000, 5000, 500, 1000, 0x748202ca -0, 5500, 5500, 500, 1000, 0x0ef92449 -0, 6000, 6000, 500, 1000, 0x6a3760ab -0, 6500, 6500, 500, 1000, 0xce5c5abf -0, 7000, 7000, 500, 1000, 0x23396792 -0, 7500, 7500, 500, 1000, 0xa5276238 -0, 8000, 8000, 500, 1000, 0x288adf1b -0, 8500, 8500, 500, 1000, 0xe7de6fb2 -0, 9000, 9000, 500, 1000, 0x2c2c707f -0, 9500, 9500, 500, 1000, 0xd66d6daf -0, 10000, 10000, 500, 1000, 0xbcea7d64 -0, 10500, 10500, 500, 1000, 0x766feea5 -0, 11000, 11000, 500, 1000, 0xd2e1d63a -0, 11500, 11500, 500, 1000, 0x2f7ef4ed -0, 12000, 12000, 500, 1000, 0xb655cba4 -0, 12500, 12500, 500, 1000, 0x4507d37b -0, 13000, 13000, 500, 1000, 0x0c57f794 -0, 13500, 13500, 500, 1000, 0x0ecbe5cc -0, 14000, 14000, 500, 1000, 0x9bf6e345 -0, 14500, 14500, 500, 1000, 0xc461443c -0, 15000, 15000, 500, 1000, 0xad9657bf -0, 15500, 15500, 500, 1000, 0x466fe91c -0, 16000, 16000, 500, 1000, 0x9ee377fe -0, 16500, 16500, 500, 1000, 0x09956428 -0, 17000, 17000, 500, 1000, 0x9b285f0a -0, 17500, 17500, 500, 1000, 0x0a3e61a6 -0, 18000, 18000, 500, 1000, 0xacc25d6b -0, 18500, 18500, 500, 1000, 0x377be319 -0, 19000, 19000, 500, 1000, 0xe4890504 -0, 19500, 19500, 500, 1000, 0xe90a6497 -0, 20000, 20000, 500, 1000, 0xd00fe950 -0, 20500, 20500, 500, 1000, 0xf195eb44 -0, 21000, 21000, 500, 1000, 0xa491f3ef -0, 21500, 21500, 500, 1000, 0x2c036e18 -0, 22000, 22000, 50, 100, 0x0bd81f05 +0, 0, 0, 1500, 3000, 0xafc2b717 +0, 1500, 1500, 1500, 3000, 0x15fecc74 +0, 3000, 3000, 1500, 3000, 0x653ae7e6 +0, 4500, 4500, 1500, 3000, 0x78e71eb9 +0, 6000, 6000, 1500, 3000, 0x4eed230b +0, 7500, 7500, 1500, 3000, 0xec9fb114 +0, 9000, 9000, 1500, 3000, 0x5ffe5ba1 +0, 10500, 10500, 1500, 3000, 0x52cfb9ea +0, 12000, 12000, 1500, 3000, 0x9f1996d1 +0, 13500, 13500, 1500, 3000, 0x1bbd0d6b +0, 15000, 15000, 1500, 3000, 0x0a1ab8e8 +0, 16500, 16500, 1500, 3000, 0xaa2924e7 +0, 18000, 18000, 1500, 3000, 0x153b4597 +0, 19500, 19500, 1500, 3000, 0x4a7a3949 +0, 21000, 21000, 1050, 2100, 0x44bc811b -- 2.39.1