opus_speed_test.cc (5326B)
1 /* 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include <cstddef> 12 #include <cstdint> 13 #include <cstdio> 14 #include <ctime> 15 #include <string> 16 #include <tuple> 17 18 #include "modules/audio_coding/codecs/opus/opus_interface.h" 19 #include "modules/audio_coding/codecs/tools/audio_codec_speed_test.h" 20 #include "test/gtest.h" 21 22 using ::std::string; 23 24 namespace webrtc { 25 26 static const int kOpusBlockDurationMs = 20; 27 static const int kOpusSamplingKhz = 48; 28 29 class OpusSpeedTest : public AudioCodecSpeedTest { 30 protected: 31 OpusSpeedTest(); 32 void SetUp() override; 33 void TearDown() override; 34 float EncodeABlock(int16_t* in_data, 35 uint8_t* bit_stream, 36 size_t max_bytes, 37 size_t* encoded_bytes) override; 38 float DecodeABlock(const uint8_t* bit_stream, 39 size_t encoded_bytes, 40 int16_t* out_data) override; 41 WebRtcOpusEncInst* opus_encoder_; 42 WebRtcOpusDecInst* opus_decoder_; 43 }; 44 45 OpusSpeedTest::OpusSpeedTest() 46 : AudioCodecSpeedTest(kOpusBlockDurationMs, 47 kOpusSamplingKhz, 48 kOpusSamplingKhz), 49 opus_encoder_(nullptr), 50 opus_decoder_(nullptr) {} 51 52 void OpusSpeedTest::SetUp() { 53 AudioCodecSpeedTest::SetUp(); 54 // If channels_ == 1, use Opus VOIP mode, otherwise, audio mode. 55 int app = channels_ == 1 ? 0 : 1; 56 /* Create encoder memory. */ 57 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, app, 48000)); 58 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_, 48000)); 59 /* Set bitrate. */ 60 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, bit_rate_)); 61 } 62 63 void OpusSpeedTest::TearDown() { 64 AudioCodecSpeedTest::TearDown(); 65 /* Free memory. */ 66 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); 67 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_)); 68 } 69 70 float OpusSpeedTest::EncodeABlock(int16_t* in_data, 71 uint8_t* bit_stream, 72 size_t max_bytes, 73 size_t* encoded_bytes) { 74 clock_t clocks = clock(); 75 int value = WebRtcOpus_Encode(opus_encoder_, in_data, input_length_sample_, 76 max_bytes, bit_stream); 77 clocks = clock() - clocks; 78 EXPECT_GT(value, 0); 79 *encoded_bytes = static_cast<size_t>(value); 80 return 1000.0 * clocks / CLOCKS_PER_SEC; 81 } 82 83 float OpusSpeedTest::DecodeABlock(const uint8_t* bit_stream, 84 size_t encoded_bytes, 85 int16_t* out_data) { 86 int value; 87 int16_t audio_type; 88 clock_t clocks = clock(); 89 value = WebRtcOpus_Decode(opus_decoder_, bit_stream, encoded_bytes, out_data, 90 &audio_type); 91 clocks = clock() - clocks; 92 EXPECT_EQ(output_length_sample_, static_cast<size_t>(value)); 93 return 1000.0 * clocks / CLOCKS_PER_SEC; 94 } 95 96 /* Test audio length in second. */ 97 constexpr size_t kDurationSec = 400; 98 99 #define ADD_TEST(complexity) \ 100 TEST_P(OpusSpeedTest, OpusSetComplexityTest##complexity) { \ 101 /* Set complexity. */ \ 102 printf("Setting complexity to %d ...\n", complexity); \ 103 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, complexity)); \ 104 EncodeDecode(kDurationSec); \ 105 } 106 107 ADD_TEST(10) 108 ADD_TEST(9) 109 ADD_TEST(8) 110 ADD_TEST(7) 111 ADD_TEST(6) 112 ADD_TEST(5) 113 ADD_TEST(4) 114 ADD_TEST(3) 115 ADD_TEST(2) 116 ADD_TEST(1) 117 ADD_TEST(0) 118 119 #define ADD_BANDWIDTH_TEST(bandwidth) \ 120 TEST_P(OpusSpeedTest, OpusSetBandwidthTest##bandwidth) { \ 121 /* Set bandwidth. */ \ 122 printf("Setting bandwidth to %d ...\n", bandwidth); \ 123 EXPECT_EQ(0, WebRtcOpus_SetBandwidth(opus_encoder_, bandwidth)); \ 124 EncodeDecode(kDurationSec); \ 125 } 126 127 ADD_BANDWIDTH_TEST(OPUS_BANDWIDTH_NARROWBAND) 128 ADD_BANDWIDTH_TEST(OPUS_BANDWIDTH_MEDIUMBAND) 129 ADD_BANDWIDTH_TEST(OPUS_BANDWIDTH_WIDEBAND) 130 ADD_BANDWIDTH_TEST(OPUS_BANDWIDTH_SUPERWIDEBAND) 131 ADD_BANDWIDTH_TEST(OPUS_BANDWIDTH_FULLBAND) 132 133 // List all test cases: (channel, bit rat, filename, extension). 134 const coding_param param_set[] = { 135 std::make_tuple(1, 136 64000, 137 string("audio_coding/speech_mono_32_48kHz"), 138 string("pcm"), 139 true), 140 std::make_tuple(1, 141 32000, 142 string("audio_coding/speech_mono_32_48kHz"), 143 string("pcm"), 144 true), 145 std::make_tuple(2, 146 64000, 147 string("audio_coding/music_stereo_48kHz"), 148 string("pcm"), 149 true)}; 150 151 INSTANTIATE_TEST_SUITE_P(AllTest, 152 OpusSpeedTest, 153 ::testing::ValuesIn(param_set)); 154 155 } // namespace webrtc