From 7c0471b5a5ed61af2231a52960c2f806b2eaa64d Mon Sep 17 00:00:00 2001 From: pschatzmann Date: Mon, 28 Oct 2024 22:55:37 +0100 Subject: [PATCH] Correct empty decoding result --- src/ADPCM.h | 38 ++++++++++++++++++++++----------- src/adpcm-ffmpeg/compat.h | 6 +++--- src/adpcm-ffmpeg/config-adpcm.h | 2 +- test/test.cpp | 2 +- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/ADPCM.h b/src/ADPCM.h index 12828ef..573e2af 100644 --- a/src/ADPCM.h +++ b/src/ADPCM.h @@ -174,28 +174,31 @@ class ADPCMDecoder : public ADPCMCodec { AVFrame &decode(AVPacket &packet) { int got_packet_ptr = 0; - // frame.nb_samples = avctx.frame_size; + //frame.nb_samples = avctx.frame_size; - // clear frame data result - // std::fill(frame_data_vector.begin(), frame_data_vector.end(), 0); - // std::fill(frame_extended_data_vector1.begin(), - // frame_extended_data_vector1.end(), 0); - // std::fill(frame_extended_data_vector2.begin(), - // frame_extended_data_vector2.end(), 0); + //clear frame data result + std::fill(frame_data_vector.begin(), frame_data_vector.end(), 0); + std::fill(frame_extended_data_vector1.begin(), + frame_extended_data_vector1.end(), 0); + std::fill(frame_extended_data_vector2.begin(), + frame_extended_data_vector2.end(), 0); int rc = adpcm_decode_frame(&avctx, &frame, &got_packet_ptr, &packet); if (rc == 0 || !got_packet_ptr) { frame.nb_samples = 0; } - int16_t *result16 = (int16_t *)frame.data[0]; - int pos = 0; - for (int j = 0; j < frame.nb_samples; j++) { - for (int ch = 0; ch < avctx.nb_channels; ch++) { - result16[pos++] = frame.extended_data[ch][j]; + if (!hasFrameData((int16_t*)(frame.data[0]), frame.extended_data[0], frame.nb_samples)){ + int16_t *result16 = (int16_t *)frame.data[0]; + int pos = 0; + for (int j = 0; j < frame.nb_samples; j++) { + for (int ch = 0; ch < avctx.nb_channels; ch++) { + result16[pos++] = frame.extended_data[ch][j]; + } } } + return frame; } @@ -207,6 +210,17 @@ class ADPCMDecoder : public ADPCMCodec { std::vector frame_extended_data_vector2; int16_t *extended_data[2] = {NULL}; uint8_t *data[AV_NUM_DATA_POINTERS] = {NULL}; + + /// The result is not returned consistently: sometimes it is in the frame data, + /// sometimes it is in the extra data. Here we check where it actually is! + bool hasFrameData(int16_t* frame_data,int16_t* ext_data, int len){ + for (int j=0;j= 2 - if (amin > amax) abort(); -#endif +//#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 2 + assert (amin <= amax) ; +//#endif if (a < amin) return amin; else if (a > amax) diff --git a/src/adpcm-ffmpeg/config-adpcm.h b/src/adpcm-ffmpeg/config-adpcm.h index b2c01b3..8f3b5fb 100644 --- a/src/adpcm-ffmpeg/config-adpcm.h +++ b/src/adpcm-ffmpeg/config-adpcm.h @@ -1,7 +1,7 @@ #pragma once // setting to 1 uses a ima wav decoder and encoder that does not use any macros and thus can be debugged -# define DEBUG 1 +# define DEBUG 0 # define AV_HAVE_BIGENDIAN 0 # define CACHED_BITSTREAM_READER 0 diff --git a/test/test.cpp b/test/test.cpp index 46178db..aab8bd5 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -13,7 +13,7 @@ using namespace std; using namespace adpcm_ffmpeg; -AVCodecID code = AV_CODEC_ID_ADPCM_MS; // AV_CODEC_ID_ADPCM_IMA_WAV +AVCodecID code = AV_CODEC_ID_ADPCM_IMA_WAV; //AV_CODEC_ID_ADPCM_MS; // AV_CODEC_ID_ADPCM_IMA_WAV ADPCMDecoder decoder{code}; ADPCMEncoder encoder{code}; vector frame_vector;