- fix Opus decoder for stereo audio

This commit is contained in:
Dmytro Bogovych 2023-04-10 13:10:43 +03:00
parent 5182cb8379
commit 8ee306fb76
2 changed files with 10 additions and 8 deletions

View File

@ -493,16 +493,18 @@ int OpusCodec::encode(const void* input, int inputBytes, void* output, int outpu
int OpusCodec::decode(const void* input, int inputBytes, void* output, int outputCapacity) int OpusCodec::decode(const void* input, int inputBytes, void* output, int outputCapacity)
{ {
int nrOfChannelsInPacket = opus_packet_get_nb_channels((const unsigned char*)input); int nr_of_channels = opus_packet_get_nb_channels((const unsigned char*)input);
assert(nrOfChannelsInPacket == 1); assert(nr_of_channels == channels());
int nrOfSamplesInPacket = opus_decoder_get_nb_samples(mDecoderCtx, (const unsigned char*)input, inputBytes); int nr_of_frames = opus_decoder_get_nb_samples(mDecoderCtx, (const unsigned char*)input, inputBytes);
if (nrOfSamplesInPacket > 0) if (nr_of_frames > 0)
{ {
// Send number of bytes for input and number of samples for output // Send number of bytes for input and number of samples for output
int capacity = nrOfSamplesInPacket * sizeof(opus_int16) * channels(); int capacity = nr_of_frames * sizeof(opus_int16) * channels();
// Dangerous !
opus_int16* buffer = (opus_int16*)alloca(capacity); opus_int16* buffer = (opus_int16*)alloca(capacity);
int decoded = opus_decode(mDecoderCtx, (const unsigned char*)input, inputBytes, (opus_int16*)buffer, capacity / (sizeof(short) * channels()), 1); int decoded = opus_decode(mDecoderCtx, (const unsigned char*)input, inputBytes, (opus_int16*)buffer, capacity / (sizeof(short) * channels()), 0 /* FEC decoding is for lost packets */);
if (decoded < 0) if (decoded < 0)
return 0; return 0;
else else

View File

@ -132,7 +132,7 @@ CodecList::CodecList(const Settings& settings)
:mSettings(settings) :mSettings(settings)
{ {
//mFactoryList.push_back(new OpusCodec::OpusFactory(16000, 1)); //mFactoryList.push_back(new OpusCodec::OpusFactory(16000, 1));
/*
#if defined(USE_OPUS_CODEC) #if defined(USE_OPUS_CODEC)
if (settings.mOpusSpec.empty()) if (settings.mOpusSpec.empty())
{ {
@ -146,7 +146,7 @@ CodecList::CodecList(const Settings& settings)
} }
} }
#endif #endif
*/
#if !defined(TARGET_ANDROID) && !defined(TARGET_OPENWRT) && !defined(TARGET_RPI) #if !defined(TARGET_ANDROID) && !defined(TARGET_OPENWRT) && !defined(TARGET_RPI)
#if defined(USE_AMR_CODEC) #if defined(USE_AMR_CODEC)