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 "webrtc/modules/audio_coding/codecs/opus/interface/opus_interface.h" 12 #include "webrtc/modules/audio_coding/codecs/tools/audio_codec_speed_test.h" 13 14 using ::std::string; 15 16 namespace webrtc { 17 18 static const int kOpusBlockDurationMs = 20; 19 static const int kOpusSamplingKhz = 48; 20 21 class OpusSpeedTest : public AudioCodecSpeedTest { 22 protected: 23 OpusSpeedTest(); 24 virtual void SetUp() OVERRIDE; 25 virtual void TearDown() OVERRIDE; 26 virtual float EncodeABlock(int16_t* in_data, uint8_t* bit_stream, 27 int max_bytes, int* encoded_bytes); 28 virtual float DecodeABlock(const uint8_t* bit_stream, int encoded_bytes, 29 int16_t* out_data); 30 WebRtcOpusEncInst* opus_encoder_; 31 WebRtcOpusDecInst* opus_decoder_; 32 }; 33 34 OpusSpeedTest::OpusSpeedTest() 35 : AudioCodecSpeedTest(kOpusBlockDurationMs, 36 kOpusSamplingKhz, 37 kOpusSamplingKhz), 38 opus_encoder_(NULL), 39 opus_decoder_(NULL) { 40 } 41 42 void OpusSpeedTest::SetUp() { 43 AudioCodecSpeedTest::SetUp(); 44 /* Create encoder memory. */ 45 EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_, channels_)); 46 EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_)); 47 /* Set bitrate. */ 48 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, bit_rate_)); 49 } 50 51 void OpusSpeedTest::TearDown() { 52 AudioCodecSpeedTest::TearDown(); 53 /* Free memory. */ 54 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_)); 55 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_)); 56 } 57 58 float OpusSpeedTest::EncodeABlock(int16_t* in_data, uint8_t* bit_stream, 59 int max_bytes, int* encoded_bytes) { 60 clock_t clocks = clock(); 61 int value = WebRtcOpus_Encode(opus_encoder_, in_data, 62 input_length_sample_, max_bytes, 63 bit_stream); 64 clocks = clock() - clocks; 65 EXPECT_GT(value, 0); 66 *encoded_bytes = value; 67 return 1000.0 * clocks / CLOCKS_PER_SEC; 68 } 69 70 float OpusSpeedTest::DecodeABlock(const uint8_t* bit_stream, 71 int encoded_bytes, int16_t* out_data) { 72 int value; 73 int16_t audio_type; 74 clock_t clocks = clock(); 75 value = WebRtcOpus_DecodeNew(opus_decoder_, bit_stream, encoded_bytes, 76 out_data, &audio_type); 77 clocks = clock() - clocks; 78 EXPECT_EQ(output_length_sample_, value); 79 return 1000.0 * clocks / CLOCKS_PER_SEC; 80 } 81 82 #define ADD_TEST(complexity) \ 83 TEST_P(OpusSpeedTest, OpusSetComplexityTest##complexity) { \ 84 /* Test audio length in second. */ \ 85 size_t kDurationSec = 400; \ 86 /* Set complexity. */ \ 87 printf("Setting complexity to %d ...\n", complexity); \ 88 EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, complexity)); \ 89 EncodeDecode(kDurationSec); \ 90 } 91 92 ADD_TEST(10); 93 ADD_TEST(9); 94 ADD_TEST(8); 95 ADD_TEST(7); 96 ADD_TEST(6); 97 ADD_TEST(5); 98 ADD_TEST(4); 99 ADD_TEST(3); 100 ADD_TEST(2); 101 ADD_TEST(1); 102 ADD_TEST(0); 103 104 // List all test cases: (channel, bit rat, filename, extension). 105 const coding_param param_set[] = 106 {::std::tr1::make_tuple(1, 64000, 107 string("audio_coding/speech_mono_32_48kHz"), 108 string("pcm"), true), 109 ::std::tr1::make_tuple(1, 32000, 110 string("audio_coding/speech_mono_32_48kHz"), 111 string("pcm"), true), 112 ::std::tr1::make_tuple(2, 64000, 113 string("audio_coding/music_stereo_48kHz"), 114 string("pcm"), true)}; 115 116 INSTANTIATE_TEST_CASE_P(AllTest, OpusSpeedTest, 117 ::testing::ValuesIn(param_set)); 118 119 } // namespace webrtc 120