File: opus_speed_test.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (155 lines) | stat: -rw-r--r-- 5,326 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <string>
#include <tuple>

#include "modules/audio_coding/codecs/opus/opus_interface.h"
#include "modules/audio_coding/codecs/tools/audio_codec_speed_test.h"
#include "test/gtest.h"

using ::std::string;

namespace webrtc {

static const int kOpusBlockDurationMs = 20;
static const int kOpusSamplingKhz = 48;

class OpusSpeedTest : public AudioCodecSpeedTest {
 protected:
  OpusSpeedTest();
  void SetUp() override;
  void TearDown() override;
  float EncodeABlock(int16_t* in_data,
                     uint8_t* bit_stream,
                     size_t max_bytes,
                     size_t* encoded_bytes) override;
  float DecodeABlock(const uint8_t* bit_stream,
                     size_t encoded_bytes,
                     int16_t* out_data) override;
  WebRtcOpusEncInst* opus_encoder_;
  WebRtcOpusDecInst* opus_decoder_;
};

OpusSpeedTest::OpusSpeedTest()
    : AudioCodecSpeedTest(kOpusBlockDurationMs,
                          kOpusSamplingKhz,
                          kOpusSamplingKhz),
      opus_encoder_(nullptr),
      opus_decoder_(nullptr) {}

void OpusSpeedTest::SetUp() {
  AudioCodecSpeedTest::SetUp();
  // If channels_ == 1, use Opus VOIP mode, otherwise, audio mode.
  int app = channels_ == 1 ? 0 : 1;
  /* Create encoder memory. */
  EXPECT_EQ(0, WebRtcOpus_EncoderCreate(&opus_encoder_, channels_, app, 48000));
  EXPECT_EQ(0, WebRtcOpus_DecoderCreate(&opus_decoder_, channels_, 48000));
  /* Set bitrate. */
  EXPECT_EQ(0, WebRtcOpus_SetBitRate(opus_encoder_, bit_rate_));
}

void OpusSpeedTest::TearDown() {
  AudioCodecSpeedTest::TearDown();
  /* Free memory. */
  EXPECT_EQ(0, WebRtcOpus_EncoderFree(opus_encoder_));
  EXPECT_EQ(0, WebRtcOpus_DecoderFree(opus_decoder_));
}

float OpusSpeedTest::EncodeABlock(int16_t* in_data,
                                  uint8_t* bit_stream,
                                  size_t max_bytes,
                                  size_t* encoded_bytes) {
  clock_t clocks = clock();
  int value = WebRtcOpus_Encode(opus_encoder_, in_data, input_length_sample_,
                                max_bytes, bit_stream);
  clocks = clock() - clocks;
  EXPECT_GT(value, 0);
  *encoded_bytes = static_cast<size_t>(value);
  return 1000.0 * clocks / CLOCKS_PER_SEC;
}

float OpusSpeedTest::DecodeABlock(const uint8_t* bit_stream,
                                  size_t encoded_bytes,
                                  int16_t* out_data) {
  int value;
  int16_t audio_type;
  clock_t clocks = clock();
  value = WebRtcOpus_Decode(opus_decoder_, bit_stream, encoded_bytes, out_data,
                            &audio_type);
  clocks = clock() - clocks;
  EXPECT_EQ(output_length_sample_, static_cast<size_t>(value));
  return 1000.0 * clocks / CLOCKS_PER_SEC;
}

/* Test audio length in second. */
constexpr size_t kDurationSec = 400;

#define ADD_TEST(complexity)                                           \
  TEST_P(OpusSpeedTest, OpusSetComplexityTest##complexity) {           \
    /* Set complexity. */                                              \
    printf("Setting complexity to %d ...\n", complexity);              \
    EXPECT_EQ(0, WebRtcOpus_SetComplexity(opus_encoder_, complexity)); \
    EncodeDecode(kDurationSec);                                        \
  }

ADD_TEST(10)
ADD_TEST(9)
ADD_TEST(8)
ADD_TEST(7)
ADD_TEST(6)
ADD_TEST(5)
ADD_TEST(4)
ADD_TEST(3)
ADD_TEST(2)
ADD_TEST(1)
ADD_TEST(0)

#define ADD_BANDWIDTH_TEST(bandwidth)                                \
  TEST_P(OpusSpeedTest, OpusSetBandwidthTest##bandwidth) {           \
    /* Set bandwidth. */                                             \
    printf("Setting bandwidth to %d ...\n", bandwidth);              \
    EXPECT_EQ(0, WebRtcOpus_SetBandwidth(opus_encoder_, bandwidth)); \
    EncodeDecode(kDurationSec);                                      \
  }

ADD_BANDWIDTH_TEST(OPUS_BANDWIDTH_NARROWBAND)
ADD_BANDWIDTH_TEST(OPUS_BANDWIDTH_MEDIUMBAND)
ADD_BANDWIDTH_TEST(OPUS_BANDWIDTH_WIDEBAND)
ADD_BANDWIDTH_TEST(OPUS_BANDWIDTH_SUPERWIDEBAND)
ADD_BANDWIDTH_TEST(OPUS_BANDWIDTH_FULLBAND)

// List all test cases: (channel, bit rat, filename, extension).
const coding_param param_set[] = {
    std::make_tuple(1,
                    64000,
                    string("audio_coding/speech_mono_32_48kHz"),
                    string("pcm"),
                    true),
    std::make_tuple(1,
                    32000,
                    string("audio_coding/speech_mono_32_48kHz"),
                    string("pcm"),
                    true),
    std::make_tuple(2,
                    64000,
                    string("audio_coding/music_stereo_48kHz"),
                    string("pcm"),
                    true)};

INSTANTIATE_TEST_SUITE_P(AllTest,
                         OpusSpeedTest,
                         ::testing::ValuesIn(param_set));

}  // namespace webrtc