Adding cbr support for Opus

BUG=webrtc:7394

Review-Url: https://codereview.webrtc.org/2772773002
Cr-Commit-Position: refs/heads/master@{#17564}
This commit is contained in:
soren
2017-04-06 05:48:36 -07:00
committed by Commit bot
parent 388fe425c7
commit 28dc285f22
5 changed files with 104 additions and 0 deletions

View File

@ -40,6 +40,8 @@ class OpusTest : public TestWithParam<::testing::tuple<int, int>> {
void TestDtxEffect(bool dtx, int block_length_ms);
void TestCbrEffect(bool dtx, int block_length_ms);
// Prepare |speech_data_| for encoding, read from a hard-coded file.
// After preparation, |speech_data_.GetNextBlock()| returns a pointer to a
// block of |block_length_ms| milliseconds. The data is looped every
@ -300,6 +302,52 @@ void OpusTest::TestDtxEffect(bool dtx, int block_length_ms) {
EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
}
// Test if CBR does what we expect.
void OpusTest::TestCbrEffect(bool cbr, int block_length_ms) {
PrepareSpeechData(channels_, block_length_ms, 2000);
const size_t samples = kOpusRateKhz * block_length_ms;
int32_t max_pkt_size_diff = 0;
int32_t prev_pkt_size = 0;
// Create encoder memory.
EXPECT_EQ(0,
WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, application_));
EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_));
// Set bitrate.
EXPECT_EQ(
0, WebRtcOpus_SetBitRate(opus_encoder_, channels_ == 1 ? 32000 : 64000));
// Setting CBR.
EXPECT_EQ(0, cbr ? WebRtcOpus_EnableCbr(opus_encoder_)
: WebRtcOpus_DisableCbr(opus_encoder_));
int16_t audio_type;
std::vector<int16_t> audio_out(samples * channels_);
for (int i = 0; i < 100; ++i) {
EXPECT_EQ(samples, static_cast<size_t>(EncodeDecode(
opus_encoder_, speech_data_.GetNextBlock(),
opus_decoder_, audio_out.data(), &audio_type)));
if (prev_pkt_size > 0) {
int32_t diff = std::abs((int32_t)encoded_bytes_ - prev_pkt_size);
max_pkt_size_diff = std::max(max_pkt_size_diff, diff);
}
prev_pkt_size = encoded_bytes_;
}
if (cbr) {
EXPECT_EQ(max_pkt_size_diff, 0);
} else {
EXPECT_GT(max_pkt_size_diff, 0);
}
// Free memory.
EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
}
// Test failing Create.
TEST(OpusTest, OpusCreateFail) {
WebRtcOpusEncInst* opus_encoder;
@ -525,6 +573,18 @@ TEST_P(OpusTest, OpusDtxOn) {
TestDtxEffect(true, 40);
}
TEST_P(OpusTest, OpusCbrOff) {
TestCbrEffect(false, 10);
TestCbrEffect(false, 20);
TestCbrEffect(false, 40);
}
TEST_P(OpusTest, OpusCbrOn) {
TestCbrEffect(true, 10);
TestCbrEffect(true, 20);
TestCbrEffect(true, 40);
}
TEST_P(OpusTest, OpusSetPacketLossRate) {
// Test without creating encoder memory.
EXPECT_EQ(-1, WebRtcOpus_SetPacketLossRate(opus_encoder_, 50));