1 /*
2 * Copyright (c) 2016 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 "api/audio_codecs/opus/audio_encoder_opus.h"
12 #include "modules/audio_coding/neteq/tools/audio_loop.h"
13 #include "rtc_base/format_macros.h"
14 #include "rtc_base/time_utils.h"
15 #include "test/gtest.h"
16 #include "test/testsupport/file_utils.h"
17 #include "test/testsupport/perf_test.h"
18
19 namespace webrtc {
20
21 namespace {
RunComplexityTest(const AudioEncoderOpusConfig & config)22 int64_t RunComplexityTest(const AudioEncoderOpusConfig& config) {
23 // Create encoder.
24 constexpr int payload_type = 17;
25 const auto encoder = AudioEncoderOpus::MakeAudioEncoder(config, payload_type);
26 // Open speech file.
27 const std::string kInputFileName =
28 webrtc::test::ResourcePath("audio_coding/speech_mono_32_48kHz", "pcm");
29 test::AudioLoop audio_loop;
30 constexpr int kSampleRateHz = 48000;
31 EXPECT_EQ(kSampleRateHz, encoder->SampleRateHz());
32 constexpr size_t kMaxLoopLengthSamples =
33 kSampleRateHz * 10; // 10 second loop.
34 constexpr size_t kInputBlockSizeSamples =
35 10 * kSampleRateHz / 1000; // 60 ms.
36 EXPECT_TRUE(audio_loop.Init(kInputFileName, kMaxLoopLengthSamples,
37 kInputBlockSizeSamples));
38 // Encode.
39 const int64_t start_time_ms = rtc::TimeMillis();
40 AudioEncoder::EncodedInfo info;
41 rtc::Buffer encoded(500);
42 uint32_t rtp_timestamp = 0u;
43 for (size_t i = 0; i < 10000; ++i) {
44 encoded.Clear();
45 info = encoder->Encode(rtp_timestamp, audio_loop.GetNextBlock(), &encoded);
46 rtp_timestamp += kInputBlockSizeSamples;
47 }
48 return rtc::TimeMillis() - start_time_ms;
49 }
50 } // namespace
51
52 // This test encodes an audio file using Opus twice with different bitrates
53 // (~11 kbps and 15.5 kbps). The runtime for each is measured, and the ratio
54 // between the two is calculated and tracked. This test explicitly sets the
55 // low_rate_complexity to 9. When running on desktop platforms, this is the same
56 // as the regular complexity, and the expectation is that the resulting ratio
57 // should be less than 100% (since the encoder runs faster at lower bitrates,
58 // given a fixed complexity setting). On the other hand, when running on
59 // mobiles, the regular complexity is 5, and we expect the resulting ratio to
60 // be higher, since we have explicitly asked for a higher complexity setting at
61 // the lower rate.
TEST(AudioEncoderOpusComplexityAdaptationTest,AdaptationOn)62 TEST(AudioEncoderOpusComplexityAdaptationTest, AdaptationOn) {
63 // Create config.
64 AudioEncoderOpusConfig config;
65 // The limit -- including the hysteresis window -- at which the complexity
66 // shuold be increased.
67 config.bitrate_bps = 11000 - 1;
68 config.low_rate_complexity = 9;
69 int64_t runtime_10999bps = RunComplexityTest(config);
70
71 config.bitrate_bps = 15500;
72 int64_t runtime_15500bps = RunComplexityTest(config);
73
74 test::PrintResult("opus_encoding_complexity_ratio", "", "adaptation_on",
75 100.0 * runtime_10999bps / runtime_15500bps, "percent",
76 true);
77 }
78
79 // This test is identical to the one above, but without the complexity
80 // adaptation enabled (neither on desktop, nor on mobile). The expectation is
81 // that the resulting ratio is less than 100% at all times.
TEST(AudioEncoderOpusComplexityAdaptationTest,AdaptationOff)82 TEST(AudioEncoderOpusComplexityAdaptationTest, AdaptationOff) {
83 // Create config.
84 AudioEncoderOpusConfig config;
85 // The limit -- including the hysteresis window -- at which the complexity
86 // shuold be increased (but not in this test since complexity adaptation is
87 // disabled).
88 config.bitrate_bps = 11000 - 1;
89 int64_t runtime_10999bps = RunComplexityTest(config);
90
91 config.bitrate_bps = 15500;
92 int64_t runtime_15500bps = RunComplexityTest(config);
93
94 test::PrintResult("opus_encoding_complexity_ratio", "", "adaptation_off",
95 100.0 * runtime_10999bps / runtime_15500bps, "percent",
96 true);
97 }
98 } // namespace webrtc
99