tor-browser

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

opus_test.cc (13491B)


      1 /*
      2 *  Copyright (c) 2013 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 "modules/audio_coding/test/opus_test.h"
     12 
     13 #include <cmath>
     14 #include <cstddef>
     15 #include <cstdint>
     16 #include <sstream>
     17 #include <string>
     18 
     19 #include "api/audio/audio_frame.h"
     20 #include "api/audio/audio_view.h"
     21 #include "api/audio_codecs/audio_format.h"
     22 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
     23 #include "api/environment/environment_factory.h"
     24 #include "api/neteq/default_neteq_factory.h"
     25 #include "api/neteq/neteq.h"
     26 #include "modules/audio_coding/codecs/opus/opus_interface.h"
     27 #include "modules/audio_coding/include/audio_coding_module_typedefs.h"
     28 #include "modules/audio_coding/test/TestStereo.h"
     29 #include "test/gtest.h"
     30 #include "test/testsupport/file_utils.h"
     31 
     32 namespace webrtc {
     33 
     34 OpusTest::OpusTest()
     35    : neteq_(DefaultNetEqFactory().Create(CreateEnvironment(),
     36                                          NetEq::Config(),
     37                                          CreateBuiltinAudioDecoderFactory())),
     38      channel_a2b_(nullptr),
     39      counter_(0),
     40      payload_type_(255),
     41      rtp_timestamp_(0) {}
     42 
     43 OpusTest::~OpusTest() {
     44  if (channel_a2b_ != nullptr) {
     45    delete channel_a2b_;
     46    channel_a2b_ = nullptr;
     47  }
     48  if (opus_mono_encoder_ != nullptr) {
     49    WebRtcOpus_EncoderFree(opus_mono_encoder_);
     50    opus_mono_encoder_ = nullptr;
     51  }
     52  if (opus_stereo_encoder_ != nullptr) {
     53    WebRtcOpus_EncoderFree(opus_stereo_encoder_);
     54    opus_stereo_encoder_ = nullptr;
     55  }
     56  if (opus_mono_decoder_ != nullptr) {
     57    WebRtcOpus_DecoderFree(opus_mono_decoder_);
     58    opus_mono_decoder_ = nullptr;
     59  }
     60  if (opus_stereo_decoder_ != nullptr) {
     61    WebRtcOpus_DecoderFree(opus_stereo_decoder_);
     62    opus_stereo_decoder_ = nullptr;
     63  }
     64 }
     65 
     66 void OpusTest::Perform() {
     67 #ifndef WEBRTC_CODEC_OPUS
     68  // Opus isn't defined, exit.
     69  return;
     70 #else
     71  uint16_t frequency_hz;
     72  size_t audio_channels;
     73  int16_t test_cntr = 0;
     74 
     75  // Open both mono and stereo test files in 32 kHz.
     76  const std::string file_name_stereo =
     77      test::ResourcePath("audio_coding/teststereo32kHz", "pcm");
     78  const std::string file_name_mono =
     79      test::ResourcePath("audio_coding/testfile32kHz", "pcm");
     80  frequency_hz = 32000;
     81  in_file_stereo_.Open(file_name_stereo, frequency_hz, "rb");
     82  in_file_stereo_.ReadStereo(true);
     83  in_file_mono_.Open(file_name_mono, frequency_hz, "rb");
     84  in_file_mono_.ReadStereo(false);
     85 
     86  // Create Opus encoders for mono and stereo.
     87  ASSERT_GT(WebRtcOpus_EncoderCreate(&opus_mono_encoder_, 1, 0, 48000), -1);
     88  ASSERT_GT(WebRtcOpus_EncoderCreate(&opus_stereo_encoder_, 2, 1, 48000), -1);
     89 
     90  // Create Opus decoders for mono and stereo for stand-alone testing of Opus.
     91  ASSERT_GT(WebRtcOpus_DecoderCreate(&opus_mono_decoder_, 1, 48000), -1);
     92  ASSERT_GT(WebRtcOpus_DecoderCreate(&opus_stereo_decoder_, 2, 48000), -1);
     93  WebRtcOpus_DecoderInit(opus_mono_decoder_);
     94  WebRtcOpus_DecoderInit(opus_stereo_decoder_);
     95 
     96  ASSERT_TRUE(neteq_.get() != nullptr);
     97  neteq_->FlushBuffers();
     98 
     99  // Register Opus stereo as receiving codec.
    100  constexpr int kOpusPayloadType = 120;
    101  const SdpAudioFormat kOpusFormatStereo("opus", 48000, 2, {{"stereo", "1"}});
    102  payload_type_ = kOpusPayloadType;
    103  neteq_->SetCodecs({{kOpusPayloadType, kOpusFormatStereo}});
    104 
    105  // Create and connect the channel.
    106  channel_a2b_ = new TestPackStereo;
    107  channel_a2b_->RegisterReceiverNetEq(neteq_.get());
    108 
    109  //
    110  // Test Stereo.
    111  //
    112 
    113  channel_a2b_->set_codec_mode(kStereo);
    114  audio_channels = 2;
    115  test_cntr++;
    116  OpenOutFile(test_cntr);
    117 
    118  // Run Opus with 2.5 ms frame size.
    119  Run(channel_a2b_, audio_channels, 64000, 120);
    120 
    121  // Run Opus with 5 ms frame size.
    122  Run(channel_a2b_, audio_channels, 64000, 240);
    123 
    124  // Run Opus with 10 ms frame size.
    125  Run(channel_a2b_, audio_channels, 64000, 480);
    126 
    127  // Run Opus with 20 ms frame size.
    128  Run(channel_a2b_, audio_channels, 64000, 960);
    129 
    130  // Run Opus with 40 ms frame size.
    131  Run(channel_a2b_, audio_channels, 64000, 1920);
    132 
    133  // Run Opus with 60 ms frame size.
    134  Run(channel_a2b_, audio_channels, 64000, 2880);
    135 
    136  out_file_.Close();
    137  out_file_standalone_.Close();
    138 
    139  //
    140  // Test Opus stereo with packet-losses.
    141  //
    142 
    143  test_cntr++;
    144  OpenOutFile(test_cntr);
    145 
    146  // Run Opus with 20 ms frame size, 1% packet loss.
    147  Run(channel_a2b_, audio_channels, 64000, 960, 1);
    148 
    149  // Run Opus with 20 ms frame size, 5% packet loss.
    150  Run(channel_a2b_, audio_channels, 64000, 960, 5);
    151 
    152  // Run Opus with 20 ms frame size, 10% packet loss.
    153  Run(channel_a2b_, audio_channels, 64000, 960, 10);
    154 
    155  out_file_.Close();
    156  out_file_standalone_.Close();
    157 
    158  //
    159  // Test Mono.
    160  //
    161  channel_a2b_->set_codec_mode(kMono);
    162  audio_channels = 1;
    163  test_cntr++;
    164  OpenOutFile(test_cntr);
    165 
    166  // Register Opus mono as receiving codec.
    167  const SdpAudioFormat kOpusFormatMono("opus", 48000, 2);
    168  neteq_->SetCodecs({{kOpusPayloadType, kOpusFormatMono}});
    169 
    170  // Run Opus with 2.5 ms frame size.
    171  Run(channel_a2b_, audio_channels, 32000, 120);
    172 
    173  // Run Opus with 5 ms frame size.
    174  Run(channel_a2b_, audio_channels, 32000, 240);
    175 
    176  // Run Opus with 10 ms frame size.
    177  Run(channel_a2b_, audio_channels, 32000, 480);
    178 
    179  // Run Opus with 20 ms frame size.
    180  Run(channel_a2b_, audio_channels, 32000, 960);
    181 
    182  // Run Opus with 40 ms frame size.
    183  Run(channel_a2b_, audio_channels, 32000, 1920);
    184 
    185  // Run Opus with 60 ms frame size.
    186  Run(channel_a2b_, audio_channels, 32000, 2880);
    187 
    188  out_file_.Close();
    189  out_file_standalone_.Close();
    190 
    191  //
    192  // Test Opus mono with packet-losses.
    193  //
    194  test_cntr++;
    195  OpenOutFile(test_cntr);
    196 
    197  // Run Opus with 20 ms frame size, 1% packet loss.
    198  Run(channel_a2b_, audio_channels, 64000, 960, 1);
    199 
    200  // Run Opus with 20 ms frame size, 5% packet loss.
    201  Run(channel_a2b_, audio_channels, 64000, 960, 5);
    202 
    203  // Run Opus with 20 ms frame size, 10% packet loss.
    204  Run(channel_a2b_, audio_channels, 64000, 960, 10);
    205 
    206  // Close the files.
    207  in_file_stereo_.Close();
    208  in_file_mono_.Close();
    209  out_file_.Close();
    210  out_file_standalone_.Close();
    211 #endif
    212 }
    213 
    214 void OpusTest::Run(TestPackStereo* channel,
    215                   size_t channels,
    216                   int bitrate,
    217                   size_t frame_length,
    218                   int percent_loss) {
    219  AudioFrame audio_frame;
    220  int32_t out_freq_hz_b = out_file_.SamplingFrequency();
    221  const size_t kBufferSizeSamples = 480 * 12 * 2;  // 120 ms stereo audio.
    222  int16_t audio[kBufferSizeSamples];
    223  int16_t out_audio[kBufferSizeSamples];
    224  int16_t audio_type;
    225  size_t written_samples = 0;
    226  size_t read_samples = 0;
    227  size_t decoded_samples = 0;
    228  bool first_packet = true;
    229  uint32_t start_time_stamp = 0;
    230 
    231  channel->reset_payload_size();
    232  counter_ = 0;
    233 
    234  // Set encoder rate.
    235  EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_mono_encoder_, bitrate));
    236  EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_stereo_encoder_, bitrate));
    237 
    238 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_ARCH_ARM)
    239  // If we are on Android, iOS and/or ARM, use a lower complexity setting as
    240  // default.
    241  const int kOpusComplexity5 = 5;
    242  EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_mono_encoder_, kOpusComplexity5));
    243  EXPECT_EQ(0,
    244            WebRtcOpus_SetComplexity(opus_stereo_encoder_, kOpusComplexity5));
    245 #endif
    246 
    247  // Fast-forward 1 second (100 blocks) since the files start with silence.
    248  in_file_stereo_.FastForward(100);
    249  in_file_mono_.FastForward(100);
    250 
    251  // Limit the runtime to 1000 blocks of 10 ms each.
    252  for (size_t audio_length = 0; audio_length < 1000; audio_length += 10) {
    253    bool lost_packet = false;
    254 
    255    // Get 10 msec of audio.
    256    if (channels == 1) {
    257      if (in_file_mono_.EndOfFile()) {
    258        break;
    259      }
    260      in_file_mono_.Read10MsData(audio_frame);
    261    } else {
    262      if (in_file_stereo_.EndOfFile()) {
    263        break;
    264      }
    265      in_file_stereo_.Read10MsData(audio_frame);
    266    }
    267 
    268    // If input audio is sampled at 32 kHz, resampling to 48 kHz is required.
    269    InterleavedView<int16_t> dst(&audio[written_samples], 480, channels);
    270    resampler_.Resample(audio_frame.data_view(), dst);
    271    written_samples += dst.size();
    272 
    273    // Sometimes we need to loop over the audio vector to produce the right
    274    // number of packets.
    275    size_t loop_encode =
    276        (written_samples - read_samples) / (channels * frame_length);
    277 
    278    if (loop_encode > 0) {
    279      const size_t kMaxBytes = 1000;  // Maximum number of bytes for one packet.
    280      size_t bitstream_len_byte;
    281      uint8_t bitstream[kMaxBytes];
    282      for (size_t i = 0; i < loop_encode; i++) {
    283        int bitstream_len_byte_int = WebRtcOpus_Encode(
    284            (channels == 1) ? opus_mono_encoder_ : opus_stereo_encoder_,
    285            &audio[read_samples], frame_length, kMaxBytes, bitstream);
    286        ASSERT_GE(bitstream_len_byte_int, 0);
    287        bitstream_len_byte = static_cast<size_t>(bitstream_len_byte_int);
    288 
    289        // Simulate packet loss by setting `packet_loss_` to "true" in
    290        // `percent_loss` percent of the loops.
    291        // TODO(tlegrand): Move handling of loss simulation to TestPackStereo.
    292        if (percent_loss > 0) {
    293          if (counter_ == floor((100 / percent_loss) + 0.5)) {
    294            counter_ = 0;
    295            lost_packet = true;
    296            channel->set_lost_packet(true);
    297          } else {
    298            lost_packet = false;
    299            channel->set_lost_packet(false);
    300          }
    301          counter_++;
    302        }
    303 
    304        // Run stand-alone Opus decoder, or decode PLC.
    305        if (channels == 1) {
    306          if (!lost_packet) {
    307            decoded_samples += WebRtcOpus_Decode(
    308                opus_mono_decoder_, bitstream, bitstream_len_byte,
    309                &out_audio[decoded_samples * channels], &audio_type);
    310          } else {
    311            // Call decoder PLC.
    312            constexpr int kPlcDurationMs = 10;
    313            constexpr int kPlcSamples = 48 * kPlcDurationMs;
    314            size_t total_plc_samples = 0;
    315            while (total_plc_samples < frame_length) {
    316              int ret = WebRtcOpus_Decode(
    317                  opus_mono_decoder_, nullptr, 0,
    318                  &out_audio[decoded_samples * channels], &audio_type);
    319              EXPECT_EQ(ret, kPlcSamples);
    320              decoded_samples += ret;
    321              total_plc_samples += ret;
    322            }
    323            EXPECT_EQ(total_plc_samples, frame_length);
    324          }
    325        } else {
    326          if (!lost_packet) {
    327            decoded_samples += WebRtcOpus_Decode(
    328                opus_stereo_decoder_, bitstream, bitstream_len_byte,
    329                &out_audio[decoded_samples * channels], &audio_type);
    330          } else {
    331            // Call decoder PLC.
    332            constexpr int kPlcDurationMs = 10;
    333            constexpr int kPlcSamples = 48 * kPlcDurationMs;
    334            size_t total_plc_samples = 0;
    335            while (total_plc_samples < frame_length) {
    336              int ret = WebRtcOpus_Decode(
    337                  opus_stereo_decoder_, nullptr, 0,
    338                  &out_audio[decoded_samples * channels], &audio_type);
    339              EXPECT_EQ(ret, kPlcSamples);
    340              decoded_samples += ret;
    341              total_plc_samples += ret;
    342            }
    343            EXPECT_EQ(total_plc_samples, frame_length);
    344          }
    345        }
    346 
    347        // Send data to the channel. "channel" will handle the loss simulation.
    348        channel->SendData(AudioFrameType::kAudioFrameSpeech, payload_type_,
    349                          rtp_timestamp_, bitstream, bitstream_len_byte, 0);
    350        if (first_packet) {
    351          first_packet = false;
    352          start_time_stamp = rtp_timestamp_;
    353        }
    354        rtp_timestamp_ += static_cast<uint32_t>(frame_length);
    355        read_samples += frame_length * channels;
    356      }
    357      if (read_samples == written_samples) {
    358        read_samples = 0;
    359        written_samples = 0;
    360      }
    361    }
    362 
    363    // Run received side of ACM.
    364    bool muted;
    365    ASSERT_EQ(NetEq::kOK, neteq_->GetAudio(&audio_frame, &muted));
    366    ASSERT_TRUE(resampler_helper_.MaybeResample(out_freq_hz_b, &audio_frame));
    367 
    368    // Write output speech to file.
    369    out_file_.Write10MsData(
    370        audio_frame.data(),
    371        audio_frame.samples_per_channel_ * audio_frame.num_channels_);
    372 
    373    // Write stand-alone speech to file.
    374    out_file_standalone_.Write10MsData(out_audio, decoded_samples * channels);
    375 
    376    if (audio_frame.timestamp_ > start_time_stamp) {
    377      // Number of channels should be the same for both stand-alone and
    378      // ACM-decoding.
    379      EXPECT_EQ(audio_frame.num_channels_, channels);
    380    }
    381 
    382    decoded_samples = 0;
    383  }
    384 
    385  if (in_file_mono_.EndOfFile()) {
    386    in_file_mono_.Rewind();
    387  }
    388  if (in_file_stereo_.EndOfFile()) {
    389    in_file_stereo_.Rewind();
    390  }
    391  // Reset in case we ended with a lost packet.
    392  channel->set_lost_packet(false);
    393 }
    394 
    395 void OpusTest::OpenOutFile(int test_number) {
    396  std::string file_name;
    397  std::stringstream file_stream;
    398  file_stream << test::OutputPath() << "opustest_out_" << test_number << ".pcm";
    399  file_name = file_stream.str();
    400  out_file_.Open(file_name, 48000, "wb");
    401  file_stream.str("");
    402  file_name = file_stream.str();
    403  file_stream << test::OutputPath() << "opusstandalone_out_" << test_number
    404              << ".pcm";
    405  file_name = file_stream.str();
    406  out_file_standalone_.Open(file_name, 48000, "wb");
    407 }
    408 
    409 }  // namespace webrtc