diff --git a/webrtc/modules/audio_coding/codecs/opus/interface/opus_interface.h b/webrtc/modules/audio_coding/codecs/opus/interface/opus_interface.h index 38ff00dbd..c1348565f 100644 --- a/webrtc/modules/audio_coding/codecs/opus/interface/opus_interface.h +++ b/webrtc/modules/audio_coding/codecs/opus/interface/opus_interface.h @@ -166,7 +166,9 @@ int WebRtcOpus_DecoderChannels(OpusDecInst* inst); * Return value : 0 - Success * -1 - Error */ +int16_t WebRtcOpus_DecoderInitNew(OpusDecInst* inst); int16_t WebRtcOpus_DecoderInit(OpusDecInst* inst); +int16_t WebRtcOpus_DecoderInitSlave(OpusDecInst* inst); /**************************************************************************** * WebRtcOpus_Decode(...) @@ -188,12 +190,21 @@ int16_t WebRtcOpus_DecoderInit(OpusDecInst* inst); * Return value : >0 - Samples per channel in decoded vector * -1 - Error */ +int16_t WebRtcOpus_DecodeNew(OpusDecInst* inst, const uint8_t* encoded, + int16_t encoded_bytes, int16_t* decoded, + int16_t* audio_type); int16_t WebRtcOpus_Decode(OpusDecInst* inst, const uint8_t* encoded, int16_t encoded_bytes, int16_t* decoded, int16_t* audio_type); +int16_t WebRtcOpus_DecodeSlave(OpusDecInst* inst, const uint8_t* encoded, + int16_t encoded_bytes, int16_t* decoded, + int16_t* audio_type); /**************************************************************************** * WebRtcOpus_DecodePlc(...) + * TODO(tlegrand): Remove master and slave functions when NetEq4 is in place. + * WebRtcOpus_DecodePlcMaster(...) + * WebRtcOpus_DecodePlcSlave(...) * * This function processes PLC for opus frame(s). * Input: @@ -208,6 +219,10 @@ int16_t WebRtcOpus_Decode(OpusDecInst* inst, const uint8_t* encoded, */ int16_t WebRtcOpus_DecodePlc(OpusDecInst* inst, int16_t* decoded, int16_t number_of_lost_frames); +int16_t WebRtcOpus_DecodePlcMaster(OpusDecInst* inst, int16_t* decoded, + int16_t number_of_lost_frames); +int16_t WebRtcOpus_DecodePlcSlave(OpusDecInst* inst, int16_t* decoded, + int16_t number_of_lost_frames); /**************************************************************************** * WebRtcOpus_DecodeFec(...) diff --git a/webrtc/modules/audio_coding/codecs/opus/opus_fec_test.cc b/webrtc/modules/audio_coding/codecs/opus/opus_fec_test.cc index 35f39729f..57d834354 100644 --- a/webrtc/modules/audio_coding/codecs/opus/opus_fec_test.cc +++ b/webrtc/modules/audio_coding/codecs/opus/opus_fec_test.cc @@ -156,8 +156,10 @@ void OpusFecTest::DecodeABlock(bool lost_previous, bool lost_current) { if (!lost_current) { // Decode current frame. - value_2 = WebRtcOpus_Decode(opus_decoder_, &bit_stream_[0], encoded_bytes_, - &out_data_[value_1 * channels_], &audio_type); + value_2 = WebRtcOpus_DecodeNew(opus_decoder_, &bit_stream_[0], + encoded_bytes_, + &out_data_[value_1 * channels_], + &audio_type); EXPECT_EQ(block_length_sample_, value_2); } } diff --git a/webrtc/modules/audio_coding/codecs/opus/opus_inst.h b/webrtc/modules/audio_coding/codecs/opus/opus_inst.h index 4597ab8eb..50caf8373 100644 --- a/webrtc/modules/audio_coding/codecs/opus/opus_inst.h +++ b/webrtc/modules/audio_coding/codecs/opus/opus_inst.h @@ -18,7 +18,8 @@ struct WebRtcOpusEncInst { }; struct WebRtcOpusDecInst { - OpusDecoder* decoder; + OpusDecoder* decoder_left; + OpusDecoder* decoder_right; int prev_decoded_samples; int channels; }; diff --git a/webrtc/modules/audio_coding/codecs/opus/opus_interface.c b/webrtc/modules/audio_coding/codecs/opus/opus_interface.c index ba7fe13ac..0c2644ba3 100644 --- a/webrtc/modules/audio_coding/codecs/opus/opus_interface.c +++ b/webrtc/modules/audio_coding/codecs/opus/opus_interface.c @@ -149,7 +149,8 @@ int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity) { } int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst, int channels) { - int error; + int error_l; + int error_r; OpusDecInst* state; if (inst != NULL) { @@ -159,9 +160,11 @@ int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst, int channels) { return -1; } - /* Create new memory, always at 48000 Hz. */ - state->decoder = opus_decoder_create(48000, channels, &error); - if (error == OPUS_OK && state->decoder != NULL) { + /* Create new memory for left and right channel, always at 48000 Hz. */ + state->decoder_left = opus_decoder_create(48000, channels, &error_l); + state->decoder_right = opus_decoder_create(48000, channels, &error_r); + if (error_l == OPUS_OK && error_r == OPUS_OK && state->decoder_left != NULL + && state->decoder_right != NULL) { /* Creation of memory all ok. */ state->channels = channels; state->prev_decoded_samples = kWebRtcOpusDefaultFrameSize; @@ -170,8 +173,11 @@ int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst, int channels) { } /* If memory allocation was unsuccessful, free the entire state. */ - if (state->decoder) { - opus_decoder_destroy(state->decoder); + if (state->decoder_left) { + opus_decoder_destroy(state->decoder_left); + } + if (state->decoder_right) { + opus_decoder_destroy(state->decoder_right); } free(state); } @@ -180,7 +186,8 @@ int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst, int channels) { int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst) { if (inst) { - opus_decoder_destroy(inst->decoder); + opus_decoder_destroy(inst->decoder_left); + opus_decoder_destroy(inst->decoder_right); free(inst); return 0; } else { @@ -192,8 +199,24 @@ int WebRtcOpus_DecoderChannels(OpusDecInst* inst) { return inst->channels; } +int16_t WebRtcOpus_DecoderInitNew(OpusDecInst* inst) { + int error = opus_decoder_ctl(inst->decoder_left, OPUS_RESET_STATE); + if (error == OPUS_OK) { + return 0; + } + return -1; +} + int16_t WebRtcOpus_DecoderInit(OpusDecInst* inst) { - int error = opus_decoder_ctl(inst->decoder, OPUS_RESET_STATE); + int error = opus_decoder_ctl(inst->decoder_left, OPUS_RESET_STATE); + if (error == OPUS_OK) { + return 0; + } + return -1; +} + +int16_t WebRtcOpus_DecoderInitSlave(OpusDecInst* inst) { + int error = opus_decoder_ctl(inst->decoder_right, OPUS_RESET_STATE); if (error == OPUS_OK) { return 0; } @@ -233,10 +256,10 @@ static int DecodeFec(OpusDecoder* inst, const uint8_t* encoded, return -1; } -int16_t WebRtcOpus_Decode(OpusDecInst* inst, const uint8_t* encoded, - int16_t encoded_bytes, int16_t* decoded, - int16_t* audio_type) { - int decoded_samples = DecodeNative(inst->decoder, +int16_t WebRtcOpus_DecodeNew(OpusDecInst* inst, const uint8_t* encoded, + int16_t encoded_bytes, int16_t* decoded, + int16_t* audio_type) { + int decoded_samples = DecodeNative(inst->decoder_left, encoded, encoded_bytes, kWebRtcOpusMaxFrameSizePerChannel, @@ -252,6 +275,70 @@ int16_t WebRtcOpus_Decode(OpusDecInst* inst, const uint8_t* encoded, return decoded_samples; } +int16_t WebRtcOpus_Decode(OpusDecInst* inst, const uint8_t* encoded, + int16_t encoded_bytes, int16_t* decoded, + int16_t* audio_type) { + int decoded_samples; + int i; + + /* If mono case, just do a regular call to the decoder. + * If stereo, call to WebRtcOpus_Decode() gives left channel as output, and + * calls to WebRtcOpus_Decode_slave() give right channel as output. + * This is to make stereo work with the current setup of NetEQ, which + * requires two calls to the decoder to produce stereo. */ + + decoded_samples = DecodeNative(inst->decoder_left, encoded, encoded_bytes, + kWebRtcOpusMaxFrameSizePerChannel, decoded, + audio_type); + if (decoded_samples < 0) { + return -1; + } + if (inst->channels == 2) { + /* The parameter |decoded_samples| holds the number of samples pairs, in + * case of stereo. Number of samples in |decoded| equals |decoded_samples| + * times 2. */ + for (i = 0; i < decoded_samples; i++) { + /* Take every second sample, starting at the first sample. This gives + * the left channel. */ + decoded[i] = decoded[i * 2]; + } + } + + /* Update decoded sample memory, to be used by the PLC in case of losses. */ + inst->prev_decoded_samples = decoded_samples; + + return decoded_samples; +} + +int16_t WebRtcOpus_DecodeSlave(OpusDecInst* inst, const uint8_t* encoded, + int16_t encoded_bytes, int16_t* decoded, + int16_t* audio_type) { + int decoded_samples; + int i; + + decoded_samples = DecodeNative(inst->decoder_right, encoded, encoded_bytes, + kWebRtcOpusMaxFrameSizePerChannel, decoded, + audio_type); + if (decoded_samples < 0) { + return -1; + } + if (inst->channels == 2) { + /* The parameter |decoded_samples| holds the number of samples pairs, in + * case of stereo. Number of samples in |decoded| equals |decoded_samples| + * times 2. */ + for (i = 0; i < decoded_samples; i++) { + /* Take every second sample, starting at the second sample. This gives + * the right channel. */ + decoded[i] = decoded[i * 2 + 1]; + } + } else { + /* Decode slave should never be called for mono packets. */ + return -1; + } + + return decoded_samples; +} + int16_t WebRtcOpus_DecodePlc(OpusDecInst* inst, int16_t* decoded, int16_t number_of_lost_frames) { int16_t audio_type = 0; @@ -264,7 +351,7 @@ int16_t WebRtcOpus_DecodePlc(OpusDecInst* inst, int16_t* decoded, plc_samples = number_of_lost_frames * inst->prev_decoded_samples; plc_samples = (plc_samples <= kWebRtcOpusMaxFrameSizePerChannel) ? plc_samples : kWebRtcOpusMaxFrameSizePerChannel; - decoded_samples = DecodeNative(inst->decoder, NULL, 0, plc_samples, + decoded_samples = DecodeNative(inst->decoder_left, NULL, 0, plc_samples, decoded, &audio_type); if (decoded_samples < 0) { return -1; @@ -273,6 +360,82 @@ int16_t WebRtcOpus_DecodePlc(OpusDecInst* inst, int16_t* decoded, return decoded_samples; } +int16_t WebRtcOpus_DecodePlcMaster(OpusDecInst* inst, int16_t* decoded, + int16_t number_of_lost_frames) { + int decoded_samples; + int16_t audio_type = 0; + int plc_samples; + int i; + + /* If mono case, just do a regular call to the decoder. + * If stereo, call to WebRtcOpus_DecodePlcMaster() gives left channel as + * output, and calls to WebRtcOpus_DecodePlcSlave() give right channel as + * output. This is to make stereo work with the current setup of NetEQ, which + * requires two calls to the decoder to produce stereo. */ + + /* The number of samples we ask for is |number_of_lost_frames| times + * |prev_decoded_samples_|. Limit the number of samples to maximum + * |kWebRtcOpusMaxFrameSizePerChannel|. */ + plc_samples = number_of_lost_frames * inst->prev_decoded_samples; + plc_samples = (plc_samples <= kWebRtcOpusMaxFrameSizePerChannel) ? + plc_samples : kWebRtcOpusMaxFrameSizePerChannel; + decoded_samples = DecodeNative(inst->decoder_left, NULL, 0, plc_samples, + decoded, &audio_type); + if (decoded_samples < 0) { + return -1; + } + + if (inst->channels == 2) { + /* The parameter |decoded_samples| holds the number of sample pairs, in + * case of stereo. The original number of samples in |decoded| equals + * |decoded_samples| times 2. */ + for (i = 0; i < decoded_samples; i++) { + /* Take every second sample, starting at the first sample. This gives + * the left channel. */ + decoded[i] = decoded[i * 2]; + } + } + + return decoded_samples; +} + +int16_t WebRtcOpus_DecodePlcSlave(OpusDecInst* inst, int16_t* decoded, + int16_t number_of_lost_frames) { + int decoded_samples; + int16_t audio_type = 0; + int plc_samples; + int i; + + /* Calls to WebRtcOpus_DecodePlcSlave() give right channel as output. + * The function should never be called in the mono case. */ + if (inst->channels != 2) { + return -1; + } + + /* The number of samples we ask for is |number_of_lost_frames| times + * |prev_decoded_samples_|. Limit the number of samples to maximum + * |kWebRtcOpusMaxFrameSizePerChannel|. */ + plc_samples = number_of_lost_frames * inst->prev_decoded_samples; + plc_samples = (plc_samples <= kWebRtcOpusMaxFrameSizePerChannel) + ? plc_samples : kWebRtcOpusMaxFrameSizePerChannel; + decoded_samples = DecodeNative(inst->decoder_right, NULL, 0, plc_samples, + decoded, &audio_type); + if (decoded_samples < 0) { + return -1; + } + + /* The parameter |decoded_samples| holds the number of sample pairs, + * The original number of samples in |decoded| equals |decoded_samples| + * times 2. */ + for (i = 0; i < decoded_samples; i++) { + /* Take every second sample, starting at the second sample. This gives + * the right channel. */ + decoded[i] = decoded[i * 2 + 1]; + } + + return decoded_samples; +} + int16_t WebRtcOpus_DecodeFec(OpusDecInst* inst, const uint8_t* encoded, int16_t encoded_bytes, int16_t* decoded, int16_t* audio_type) { @@ -285,7 +448,7 @@ int16_t WebRtcOpus_DecodeFec(OpusDecInst* inst, const uint8_t* encoded, fec_samples = opus_packet_get_samples_per_frame(encoded, 48000); - decoded_samples = DecodeFec(inst->decoder, encoded, encoded_bytes, + decoded_samples = DecodeFec(inst->decoder_left, encoded, encoded_bytes, fec_samples, decoded, audio_type); if (decoded_samples < 0) { return -1; diff --git a/webrtc/modules/audio_coding/codecs/opus/opus_speed_test.cc b/webrtc/modules/audio_coding/codecs/opus/opus_speed_test.cc index e7811e349..afbaee38e 100644 --- a/webrtc/modules/audio_coding/codecs/opus/opus_speed_test.cc +++ b/webrtc/modules/audio_coding/codecs/opus/opus_speed_test.cc @@ -72,8 +72,8 @@ float OpusSpeedTest::DecodeABlock(const uint8_t* bit_stream, int value; int16_t audio_type; clock_t clocks = clock(); - value = WebRtcOpus_Decode(opus_decoder_, bit_stream, encoded_bytes, out_data, - &audio_type); + value = WebRtcOpus_DecodeNew(opus_decoder_, bit_stream, encoded_bytes, + out_data, &audio_type); clocks = clock() - clocks; EXPECT_EQ(output_length_sample_, value); return 1000.0 * clocks / CLOCKS_PER_SEC; diff --git a/webrtc/modules/audio_coding/codecs/opus/opus_unittest.cc b/webrtc/modules/audio_coding/codecs/opus/opus_unittest.cc index 9c34f5169..e76dcdcd5 100644 --- a/webrtc/modules/audio_coding/codecs/opus/opus_unittest.cc +++ b/webrtc/modules/audio_coding/codecs/opus/opus_unittest.cc @@ -35,7 +35,9 @@ class OpusTest : public ::testing::Test { WebRtcOpusEncInst* opus_mono_encoder_; WebRtcOpusEncInst* opus_stereo_encoder_; WebRtcOpusDecInst* opus_mono_decoder_; + WebRtcOpusDecInst* opus_mono_decoder_new_; WebRtcOpusDecInst* opus_stereo_decoder_; + WebRtcOpusDecInst* opus_stereo_decoder_new_; int16_t speech_data_[kOpusMaxFrameSamples]; int16_t output_data_[kOpusMaxFrameSamples]; @@ -46,7 +48,9 @@ OpusTest::OpusTest() : opus_mono_encoder_(NULL), opus_stereo_encoder_(NULL), opus_mono_decoder_(NULL), - opus_stereo_decoder_(NULL) { + opus_mono_decoder_new_(NULL), + opus_stereo_decoder_(NULL), + opus_stereo_decoder_new_(NULL) { } void OpusTest::SetUp() { @@ -113,56 +117,91 @@ TEST_F(OpusTest, OpusEncodeDecodeMono) { // Create encoder memory. EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_mono_encoder_, 1)); EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_mono_decoder_, 1)); + EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_mono_decoder_new_, 1)); // Set bitrate. EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_mono_encoder_, 32000)); // Check number of channels for decoder. EXPECT_EQ(1, WebRtcOpus_DecoderChannels(opus_mono_decoder_)); + EXPECT_EQ(1, WebRtcOpus_DecoderChannels(opus_mono_decoder_new_)); // Encode & decode. int16_t encoded_bytes; int16_t audio_type; + int16_t output_data_decode_new[kOpusMaxFrameSamples]; int16_t output_data_decode[kOpusMaxFrameSamples]; encoded_bytes = WebRtcOpus_Encode(opus_mono_encoder_, speech_data_, kOpus20msFrameSamples, kMaxBytes, bitstream_); + EXPECT_EQ(kOpus20msFrameSamples, + WebRtcOpus_DecodeNew(opus_mono_decoder_new_, bitstream_, + encoded_bytes, output_data_decode_new, + &audio_type)); EXPECT_EQ(kOpus20msFrameSamples, WebRtcOpus_Decode(opus_mono_decoder_, bitstream_, encoded_bytes, output_data_decode, &audio_type)); + // Data in |output_data_decode_new| should be the same as in + // |output_data_decode|. + for (int i = 0; i < kOpus20msFrameSamples; i++) { + EXPECT_EQ(output_data_decode_new[i], output_data_decode[i]); + } + // Free memory. EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_mono_encoder_)); EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_mono_decoder_)); + EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_mono_decoder_new_)); } TEST_F(OpusTest, OpusEncodeDecodeStereo) { // Create encoder memory. EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_stereo_encoder_, 2)); EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_stereo_decoder_, 2)); + EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_stereo_decoder_new_, 2)); // Set bitrate. EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_stereo_encoder_, 64000)); // Check number of channels for decoder. EXPECT_EQ(2, WebRtcOpus_DecoderChannels(opus_stereo_decoder_)); + EXPECT_EQ(2, WebRtcOpus_DecoderChannels(opus_stereo_decoder_new_)); // Encode & decode. int16_t encoded_bytes; int16_t audio_type; + int16_t output_data_decode_new[kOpusMaxFrameSamples]; int16_t output_data_decode[kOpusMaxFrameSamples]; + int16_t output_data_decode_slave[kOpusMaxFrameSamples]; encoded_bytes = WebRtcOpus_Encode(opus_stereo_encoder_, speech_data_, kOpus20msFrameSamples, kMaxBytes, bitstream_); + EXPECT_EQ(kOpus20msFrameSamples, + WebRtcOpus_DecodeNew(opus_stereo_decoder_new_, bitstream_, + encoded_bytes, output_data_decode_new, + &audio_type)); EXPECT_EQ(kOpus20msFrameSamples, WebRtcOpus_Decode(opus_stereo_decoder_, bitstream_, encoded_bytes, output_data_decode, &audio_type)); + EXPECT_EQ(kOpus20msFrameSamples, + WebRtcOpus_DecodeSlave(opus_stereo_decoder_, bitstream_, + encoded_bytes, output_data_decode_slave, + &audio_type)); + + // Data in |output_data_decode_new| should be the same as in + // |output_data_decode| and |output_data_decode_slave| interleaved to a + // stereo signal. + for (int i = 0; i < kOpus20msFrameSamples; i++) { + EXPECT_EQ(output_data_decode_new[i * 2], output_data_decode[i]); + EXPECT_EQ(output_data_decode_new[i * 2 + 1], output_data_decode_slave[i]); + } // Free memory. EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_stereo_encoder_)); EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_stereo_decoder_)); + EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_stereo_decoder_new_)); } TEST_F(OpusTest, OpusSetBitRate) { @@ -210,30 +249,67 @@ TEST_F(OpusTest, OpusDecodeInit) { // Create encoder memory. EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_stereo_encoder_, 2)); EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_stereo_decoder_, 2)); + EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_stereo_decoder_new_, 2)); // Encode & decode. int16_t encoded_bytes; int16_t audio_type; + int16_t output_data_decode_new[kOpusMaxFrameSamples]; int16_t output_data_decode[kOpusMaxFrameSamples]; - + int16_t output_data_decode_slave[kOpusMaxFrameSamples]; encoded_bytes = WebRtcOpus_Encode(opus_stereo_encoder_, speech_data_, kOpus20msFrameSamples, kMaxBytes, bitstream_); EXPECT_EQ(kOpus20msFrameSamples, - WebRtcOpus_Decode(opus_stereo_decoder_, bitstream_, - encoded_bytes, output_data_decode, - &audio_type)); - - EXPECT_EQ(0, WebRtcOpus_DecoderInit(opus_stereo_decoder_)); - + WebRtcOpus_DecodeNew(opus_stereo_decoder_new_, bitstream_, + encoded_bytes, output_data_decode_new, + &audio_type)); EXPECT_EQ(kOpus20msFrameSamples, WebRtcOpus_Decode(opus_stereo_decoder_, bitstream_, encoded_bytes, output_data_decode, &audio_type)); + EXPECT_EQ(kOpus20msFrameSamples, + WebRtcOpus_DecodeSlave(opus_stereo_decoder_, bitstream_, + encoded_bytes, output_data_decode_slave, + &audio_type)); + + // Data in |output_data_decode_new| should be the same as in + // |output_data_decode| and |output_data_decode_slave| interleaved to a + // stereo signal. + for (int i = 0; i < kOpus20msFrameSamples; i++) { + EXPECT_EQ(output_data_decode_new[i * 2], output_data_decode[i]); + EXPECT_EQ(output_data_decode_new[i * 2 + 1], output_data_decode_slave[i]); + } + + EXPECT_EQ(0, WebRtcOpus_DecoderInitNew(opus_stereo_decoder_new_)); + EXPECT_EQ(0, WebRtcOpus_DecoderInit(opus_stereo_decoder_)); + EXPECT_EQ(0, WebRtcOpus_DecoderInitSlave(opus_stereo_decoder_)); + + EXPECT_EQ(kOpus20msFrameSamples, + WebRtcOpus_DecodeNew(opus_stereo_decoder_new_, bitstream_, + encoded_bytes, output_data_decode_new, + &audio_type)); + EXPECT_EQ(kOpus20msFrameSamples, + WebRtcOpus_Decode(opus_stereo_decoder_, bitstream_, + encoded_bytes, output_data_decode, + &audio_type)); + EXPECT_EQ(kOpus20msFrameSamples, + WebRtcOpus_DecodeSlave(opus_stereo_decoder_, bitstream_, + encoded_bytes, output_data_decode_slave, + &audio_type)); + + // Data in |output_data_decode_new| should be the same as in + // |output_data_decode| and |output_data_decode_slave| interleaved to a + // stereo signal. + for (int i = 0; i < kOpus20msFrameSamples; i++) { + EXPECT_EQ(output_data_decode_new[i * 2], output_data_decode[i]); + EXPECT_EQ(output_data_decode_new[i * 2 + 1], output_data_decode_slave[i]); + } // Free memory. EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_stereo_encoder_)); EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_stereo_decoder_)); + EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_stereo_decoder_new_)); } TEST_F(OpusTest, OpusEnableDisableFec) { @@ -306,33 +382,49 @@ TEST_F(OpusTest, OpusDecodePlcMono) { // Create encoder memory. EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_mono_encoder_, 1)); EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_mono_decoder_, 1)); + EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_mono_decoder_new_, 1)); // Set bitrate. EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_mono_encoder_, 32000)); // Check number of channels for decoder. EXPECT_EQ(1, WebRtcOpus_DecoderChannels(opus_mono_decoder_)); + EXPECT_EQ(1, WebRtcOpus_DecoderChannels(opus_mono_decoder_new_)); // Encode & decode. int16_t encoded_bytes; int16_t audio_type; + int16_t output_data_decode_new[kOpusMaxFrameSamples]; int16_t output_data_decode[kOpusMaxFrameSamples]; encoded_bytes = WebRtcOpus_Encode(opus_mono_encoder_, speech_data_, kOpus20msFrameSamples, kMaxBytes, bitstream_); + EXPECT_EQ(kOpus20msFrameSamples, + WebRtcOpus_DecodeNew(opus_mono_decoder_new_, bitstream_, + encoded_bytes, output_data_decode_new, + &audio_type)); EXPECT_EQ(kOpus20msFrameSamples, WebRtcOpus_Decode(opus_mono_decoder_, bitstream_, encoded_bytes, output_data_decode, &audio_type)); - // Call decoder PLC. + // Call decoder PLC for both versions of the decoder. int16_t plc_buffer[kOpusMaxFrameSamples]; + int16_t plc_buffer_new[kOpusMaxFrameSamples]; EXPECT_EQ(kOpus20msFrameSamples, - WebRtcOpus_DecodePlc(opus_mono_decoder_, plc_buffer, 1)); + WebRtcOpus_DecodePlcMaster(opus_mono_decoder_, plc_buffer, 1)); + EXPECT_EQ(kOpus20msFrameSamples, + WebRtcOpus_DecodePlc(opus_mono_decoder_new_, plc_buffer_new, 1)); + + // Data in |plc_buffer| should be the same as in |plc_buffer_new|. + for (int i = 0; i < kOpus20msFrameSamples; i++) { + EXPECT_EQ(plc_buffer[i], plc_buffer_new[i]); + } // Free memory. EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_mono_encoder_)); EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_mono_decoder_)); + EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_mono_decoder_new_)); } // PLC in stereo mode. @@ -340,33 +432,61 @@ TEST_F(OpusTest, OpusDecodePlcStereo) { // Create encoder memory. EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_stereo_encoder_, 2)); EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_stereo_decoder_, 2)); + EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_stereo_decoder_new_, 2)); // Set bitrate. EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_stereo_encoder_, 64000)); // Check number of channels for decoder. EXPECT_EQ(2, WebRtcOpus_DecoderChannels(opus_stereo_decoder_)); + EXPECT_EQ(2, WebRtcOpus_DecoderChannels(opus_stereo_decoder_new_)); // Encode & decode. int16_t encoded_bytes; int16_t audio_type; + int16_t output_data_decode_new[kOpusMaxFrameSamples]; int16_t output_data_decode[kOpusMaxFrameSamples]; + int16_t output_data_decode_slave[kOpusMaxFrameSamples]; encoded_bytes = WebRtcOpus_Encode(opus_stereo_encoder_, speech_data_, kOpus20msFrameSamples, kMaxBytes, bitstream_); + EXPECT_EQ(kOpus20msFrameSamples, + WebRtcOpus_DecodeNew(opus_stereo_decoder_new_, bitstream_, + encoded_bytes, output_data_decode_new, + &audio_type)); EXPECT_EQ(kOpus20msFrameSamples, WebRtcOpus_Decode(opus_stereo_decoder_, bitstream_, encoded_bytes, output_data_decode, &audio_type)); - - // Call decoder PLC. - int16_t plc_buffer[kOpusMaxFrameSamples]; EXPECT_EQ(kOpus20msFrameSamples, - WebRtcOpus_DecodePlc(opus_stereo_decoder_, plc_buffer, 1)); + WebRtcOpus_DecodeSlave(opus_stereo_decoder_, bitstream_, + encoded_bytes, + output_data_decode_slave, + &audio_type)); + + // Call decoder PLC for both versions of the decoder. + int16_t plc_buffer_left[kOpusMaxFrameSamples]; + int16_t plc_buffer_right[kOpusMaxFrameSamples]; + int16_t plc_buffer_new[kOpusMaxFrameSamples]; + EXPECT_EQ(kOpus20msFrameSamples, + WebRtcOpus_DecodePlcMaster(opus_stereo_decoder_, + plc_buffer_left, 1)); + EXPECT_EQ(kOpus20msFrameSamples, + WebRtcOpus_DecodePlcSlave(opus_stereo_decoder_, + plc_buffer_right, 1)); + EXPECT_EQ(kOpus20msFrameSamples, + WebRtcOpus_DecodePlc(opus_stereo_decoder_new_, plc_buffer_new, 1)); + // Data in |plc_buffer_left| and |plc_buffer_right|should be the same as the + // interleaved samples in |plc_buffer_new|. + for (int i = 0, j = 0; i < kOpus20msFrameSamples; i++) { + EXPECT_EQ(plc_buffer_left[i], plc_buffer_new[j++]); + EXPECT_EQ(plc_buffer_right[i], plc_buffer_new[j++]); + } // Free memory. EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_stereo_encoder_)); EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_stereo_decoder_)); + EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_stereo_decoder_new_)); } // Duration estimation. diff --git a/webrtc/modules/audio_coding/main/test/opus_test.cc b/webrtc/modules/audio_coding/main/test/opus_test.cc index ecc056a36..398d59da4 100644 --- a/webrtc/modules/audio_coding/main/test/opus_test.cc +++ b/webrtc/modules/audio_coding/main/test/opus_test.cc @@ -85,8 +85,8 @@ void OpusTest::Perform() { // Create Opus decoders for mono and stereo for stand-alone testing of Opus. ASSERT_GT(WebRtcOpus_DecoderCreate(&opus_mono_decoder_, 1), -1); ASSERT_GT(WebRtcOpus_DecoderCreate(&opus_stereo_decoder_, 2), -1); - ASSERT_GT(WebRtcOpus_DecoderInit(opus_mono_decoder_), -1); - ASSERT_GT(WebRtcOpus_DecoderInit(opus_stereo_decoder_), -1); + ASSERT_GT(WebRtcOpus_DecoderInitNew(opus_mono_decoder_), -1); + ASSERT_GT(WebRtcOpus_DecoderInitNew(opus_stereo_decoder_), -1); ASSERT_TRUE(acm_receiver_.get() != NULL); EXPECT_EQ(0, acm_receiver_->InitializeReceiver()); @@ -304,7 +304,7 @@ void OpusTest::Run(TestPackStereo* channel, int channels, int bitrate, // Run stand-alone Opus decoder, or decode PLC. if (channels == 1) { if (!lost_packet) { - decoded_samples += WebRtcOpus_Decode( + decoded_samples += WebRtcOpus_DecodeNew( opus_mono_decoder_, bitstream, bitstream_len_byte, &out_audio[decoded_samples * channels], &audio_type); } else { @@ -313,7 +313,7 @@ void OpusTest::Run(TestPackStereo* channel, int channels, int bitrate, } } else { if (!lost_packet) { - decoded_samples += WebRtcOpus_Decode( + decoded_samples += WebRtcOpus_DecodeNew( opus_stereo_decoder_, bitstream, bitstream_len_byte, &out_audio[decoded_samples * channels], &audio_type); } else { diff --git a/webrtc/modules/audio_coding/neteq/audio_decoder_impl.cc b/webrtc/modules/audio_coding/neteq/audio_decoder_impl.cc index 799ac7bdd..eb0782344 100644 --- a/webrtc/modules/audio_coding/neteq/audio_decoder_impl.cc +++ b/webrtc/modules/audio_coding/neteq/audio_decoder_impl.cc @@ -404,9 +404,9 @@ AudioDecoderOpus::~AudioDecoderOpus() { int AudioDecoderOpus::Decode(const uint8_t* encoded, size_t encoded_len, int16_t* decoded, SpeechType* speech_type) { int16_t temp_type = 1; // Default is speech. - int16_t ret = WebRtcOpus_Decode(dec_state_, encoded, - static_cast(encoded_len), decoded, - &temp_type); + int16_t ret = WebRtcOpus_DecodeNew(dec_state_, encoded, + static_cast(encoded_len), decoded, + &temp_type); if (ret > 0) ret *= static_cast(channels_); // Return total number of samples. *speech_type = ConvertSpeechType(temp_type); @@ -427,7 +427,7 @@ int AudioDecoderOpus::DecodeRedundant(const uint8_t* encoded, } int AudioDecoderOpus::Init() { - return WebRtcOpus_DecoderInit(dec_state_); + return WebRtcOpus_DecoderInitNew(dec_state_); } int AudioDecoderOpus::PacketDuration(const uint8_t* encoded,