tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

opus_complexity_unittest.cc (4730B)


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