Skip to content

Commit

Permalink
Correct empty decoding result
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Oct 28, 2024
1 parent 714a1d8 commit 7c0471b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
38 changes: 26 additions & 12 deletions src/ADPCM.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -207,6 +210,17 @@ class ADPCMDecoder : public ADPCMCodec {
std::vector<int16_t> 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<len;j++){
if (data[j]!=0) return true;
if (ext_data[j]!=0) return false;
}
return false;
}

};

} // namespace adpcm_ffmpeg
6 changes: 3 additions & 3 deletions src/adpcm-ffmpeg/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ enum av_errors {


static av_always_inline av_const int av_clip(int a, int amin, int amax) {
#if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >= 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)
Expand Down
2 changes: 1 addition & 1 deletion src/adpcm-ffmpeg/config-adpcm.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int16_t> frame_vector;
Expand Down

0 comments on commit 7c0471b

Please sign in to comment.