2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
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.
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"
18 static const int kOpusBlockDurationMs = 20;
19 static const int kOpusSamplingKhz = 48;
21 class OpusSpeedTest : public AudioCodecSpeedTest {
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,
30 WebRtcOpusEncInst* opus_encoder_;
31 WebRtcOpusDecInst* opus_decoder_;
34 OpusSpeedTest::OpusSpeedTest()
35 : AudioCodecSpeedTest(kOpusBlockDurationMs,
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_));
48 EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, bit_rate_));
51 void OpusSpeedTest::TearDown() {
52 AudioCodecSpeedTest::TearDown();
54 EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
55 EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
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,
64 clocks = clock() - clocks;
66 *encoded_bytes = value;
67 return 1000.0 * clocks / CLOCKS_PER_SEC;
70 float OpusSpeedTest::DecodeABlock(const uint8_t* bit_stream,
71 int encoded_bytes, int16_t* out_data) {
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;
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); \
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)};
116 INSTANTIATE_TEST_CASE_P(AllTest, OpusSpeedTest,
117 ::testing::ValuesIn(param_set));
119 } // namespace webrtc