aboutsummaryrefslogtreecommitdiff
path: root/modules/audio_coding/codecs/isac/isac_webrtc_api_test.cc
blob: c4d7ab8fa8090e156921c8469fb18ea25473b9c4 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 *  Copyright (c) 2020 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 <array>
#include <memory>
#include <vector>

#include "absl/strings/string_view.h"
#include "api/array_view.h"
#include "api/audio_codecs/isac/audio_decoder_isac_fix.h"
#include "api/audio_codecs/isac/audio_decoder_isac_float.h"
#include "api/audio_codecs/isac/audio_encoder_isac_fix.h"
#include "api/audio_codecs/isac/audio_encoder_isac_float.h"
#include "modules/audio_coding/test/PCMFile.h"
#include "rtc_base/checks.h"
#include "rtc_base/strings/string_builder.h"
#include "test/gtest.h"
#include "test/testsupport/file_utils.h"

namespace webrtc {
namespace {

constexpr int kPayloadType = 42;

enum class IsacImpl { kFixed, kFloat };

absl::string_view IsacImplToString(IsacImpl impl) {
  switch (impl) {
    case IsacImpl::kFixed:
      return "fixed";
    case IsacImpl::kFloat:
      return "float";
  }
}

std::unique_ptr<PCMFile> GetPcmTestFileReader(int sample_rate_hz) {
  std::string filename;
  switch (sample_rate_hz) {
    case 16000:
      filename = test::ResourcePath("audio_coding/testfile16kHz", "pcm");
      break;
    case 32000:
      filename = test::ResourcePath("audio_coding/testfile32kHz", "pcm");
      break;
    default:
      RTC_NOTREACHED() << "No test file available for " << sample_rate_hz
                       << " Hz.";
  }
  auto pcm_file = std::make_unique<PCMFile>();
  pcm_file->ReadStereo(false);
  pcm_file->Open(filename, sample_rate_hz, "rb", /*auto_rewind=*/true);
  pcm_file->FastForward(/*num_10ms_blocks=*/100);  // Skip initial silence.
  RTC_CHECK(!pcm_file->EndOfFile());
  return pcm_file;
}

// Returns a view to the interleaved samples of an AudioFrame object.
rtc::ArrayView<const int16_t> AudioFrameToView(const AudioFrame& audio_frame) {
  return {audio_frame.data(),
          audio_frame.samples_per_channel() * audio_frame.num_channels()};
}

std::unique_ptr<AudioEncoder> CreateEncoder(IsacImpl impl,
                                            int sample_rate_hz,
                                            int frame_size_ms,
                                            int bitrate_bps) {
  RTC_CHECK(sample_rate_hz == 16000 || sample_rate_hz == 32000);
  RTC_CHECK(frame_size_ms == 30 || frame_size_ms == 60);
  RTC_CHECK_GT(bitrate_bps, 0);
  switch (impl) {
    case IsacImpl::kFixed: {
      AudioEncoderIsacFix::Config config;
      config.bit_rate = bitrate_bps;
      config.frame_size_ms = frame_size_ms;
      RTC_CHECK_EQ(16000, sample_rate_hz);
      return AudioEncoderIsacFix::MakeAudioEncoder(config, kPayloadType);
    }
    case IsacImpl::kFloat: {
      AudioEncoderIsacFloat::Config config;
      config.bit_rate = bitrate_bps;
      config.frame_size_ms = frame_size_ms;
      config.sample_rate_hz = sample_rate_hz;
      return AudioEncoderIsacFloat::MakeAudioEncoder(config, kPayloadType);
    }
  }
}

std::unique_ptr<AudioDecoder> CreateDecoder(IsacImpl impl, int sample_rate_hz) {
  RTC_CHECK(sample_rate_hz == 16000 || sample_rate_hz == 32000);
  switch (impl) {
    case IsacImpl::kFixed: {
      webrtc::AudioDecoderIsacFix::Config config;
      RTC_CHECK_EQ(16000, sample_rate_hz);
      return webrtc::AudioDecoderIsacFix::MakeAudioDecoder(config);
    }
    case IsacImpl::kFloat: {
      webrtc::AudioDecoderIsacFloat::Config config;
      config.sample_rate_hz = sample_rate_hz;
      return webrtc::AudioDecoderIsacFloat::MakeAudioDecoder(config);
    }
  }
}

struct EncoderTestParams {
  IsacImpl impl;
  int sample_rate_hz;
  int frame_size_ms;
};

class EncoderTest : public testing::TestWithParam<EncoderTestParams> {
 protected:
  EncoderTest() = default;
  IsacImpl GetIsacImpl() const { return GetParam().impl; }
  int GetSampleRateHz() const { return GetParam().sample_rate_hz; }
  int GetFrameSizeMs() const { return GetParam().frame_size_ms; }
};

TEST_P(EncoderTest, TestConfig) {
  for (int bitrate_bps : {10000, 21000, 32000}) {
    SCOPED_TRACE(bitrate_bps);
    auto encoder = CreateEncoder(GetIsacImpl(), GetSampleRateHz(),
                                 GetFrameSizeMs(), bitrate_bps);
    EXPECT_EQ(GetSampleRateHz(), encoder->SampleRateHz());
    EXPECT_EQ(size_t{1}, encoder->NumChannels());
    EXPECT_EQ(bitrate_bps, encoder->GetTargetBitrate());
  }
}

// Encodes an input audio sequence with a low and a high target bitrate and
// checks that the number of produces bytes in the first case is less than that
// of the second case.
TEST_P(EncoderTest, TestDifferentBitrates) {
  auto pcm_file = GetPcmTestFileReader(GetSampleRateHz());
  constexpr int kLowBps = 20000;
  constexpr int kHighBps = 25000;
  auto encoder_low = CreateEncoder(GetIsacImpl(), GetSampleRateHz(),
                                   GetFrameSizeMs(), kLowBps);
  auto encoder_high = CreateEncoder(GetIsacImpl(), GetSampleRateHz(),
                                    GetFrameSizeMs(), kHighBps);
  int num_bytes_low = 0;
  int num_bytes_high = 0;
  constexpr int kNumFrames = 12;
  for (int i = 0; i < kNumFrames; ++i) {
    AudioFrame in;
    pcm_file->Read10MsData(in);
    rtc::Buffer low, high;
    encoder_low->Encode(/*rtp_timestamp=*/0, AudioFrameToView(in), &low);
    encoder_high->Encode(/*rtp_timestamp=*/0, AudioFrameToView(in), &high);
    num_bytes_low += low.size();
    num_bytes_high += high.size();
  }
  EXPECT_LT(num_bytes_low, num_bytes_high);
}

// Checks that, given a target bitrate, the encoder does not overshoot too much.
TEST_P(EncoderTest, DoNotOvershootTargetBitrate) {
  for (int bitrate_bps : {10000, 15000, 20000, 26000, 32000}) {
    SCOPED_TRACE(bitrate_bps);
    auto pcm_file = GetPcmTestFileReader(GetSampleRateHz());
    auto e = CreateEncoder(GetIsacImpl(), GetSampleRateHz(), GetFrameSizeMs(),
                           bitrate_bps);
    int num_bytes = 0;
    constexpr int kNumFrames = 200;  // 2 seconds.
    for (int i = 0; i < kNumFrames; ++i) {
      AudioFrame in;
      pcm_file->Read10MsData(in);
      rtc::Buffer encoded;
      e->Encode(/*rtp_timestamp=*/0, AudioFrameToView(in), &encoded);
      num_bytes += encoded.size();
    }
    // Inverse of the duration of |kNumFrames| 10 ms frames (unit: seconds^-1).
    constexpr float kAudioDurationInv = 100.f / kNumFrames;
    const int measured_bitrate_bps = 8 * num_bytes * kAudioDurationInv;
    EXPECT_LT(measured_bitrate_bps, bitrate_bps + 2000);  // Max 2 kbps extra.
  }
}

// Creates tests for different encoder configurations and implementations.
INSTANTIATE_TEST_SUITE_P(
    IsacApiTest,
    EncoderTest,
    ::testing::ValuesIn([] {
      std::vector<EncoderTestParams> cases;
      for (IsacImpl impl : {IsacImpl::kFloat, IsacImpl::kFixed}) {
        for (int frame_size_ms : {30, 60}) {
          cases.push_back({impl, 16000, frame_size_ms});
        }
      }
      cases.push_back({IsacImpl::kFloat, 32000, 30});
      return cases;
    }()),
    [](const ::testing::TestParamInfo<EncoderTestParams>& info) {
      rtc::StringBuilder b;
      const auto& p = info.param;
      b << IsacImplToString(p.impl) << "_" << p.sample_rate_hz << "_"
        << p.frame_size_ms;
      return b.Release();
    });

struct DecoderTestParams {
  IsacImpl impl;
  int sample_rate_hz;
};

class DecoderTest : public testing::TestWithParam<DecoderTestParams> {
 protected:
  DecoderTest() = default;
  IsacImpl GetIsacImpl() const { return GetParam().impl; }
  int GetSampleRateHz() const { return GetParam().sample_rate_hz; }
};

TEST_P(DecoderTest, TestConfig) {
  auto decoder = CreateDecoder(GetIsacImpl(), GetSampleRateHz());
  EXPECT_EQ(GetSampleRateHz(), decoder->SampleRateHz());
  EXPECT_EQ(size_t{1}, decoder->Channels());
}

// Creates tests for different decoder configurations and implementations.
INSTANTIATE_TEST_SUITE_P(
    IsacApiTest,
    DecoderTest,
    ::testing::ValuesIn({DecoderTestParams{IsacImpl::kFixed, 16000},
                         DecoderTestParams{IsacImpl::kFloat, 16000},
                         DecoderTestParams{IsacImpl::kFloat, 32000}}),
    [](const ::testing::TestParamInfo<DecoderTestParams>& info) {
      const auto& p = info.param;
      return (rtc::StringBuilder()
              << IsacImplToString(p.impl) << "_" << p.sample_rate_hz)
          .Release();
    });

struct EncoderDecoderPairTestParams {
  int sample_rate_hz;
  int frame_size_ms;
  IsacImpl encoder_impl;
  IsacImpl decoder_impl;
};

class EncoderDecoderPairTest
    : public testing::TestWithParam<EncoderDecoderPairTestParams> {
 protected:
  EncoderDecoderPairTest() = default;
  int GetSampleRateHz() const { return GetParam().sample_rate_hz; }
  int GetEncoderFrameSizeMs() const { return GetParam().frame_size_ms; }
  IsacImpl GetEncoderIsacImpl() const { return GetParam().encoder_impl; }
  IsacImpl GetDecoderIsacImpl() const { return GetParam().decoder_impl; }
  int GetEncoderFrameSize() const {
    return GetEncoderFrameSizeMs() * GetSampleRateHz() / 1000;
  }
};

// Checks that the number of encoded and decoded samples match.
TEST_P(EncoderDecoderPairTest, EncodeDecode) {
  auto pcm_file = GetPcmTestFileReader(GetSampleRateHz());
  auto encoder = CreateEncoder(GetEncoderIsacImpl(), GetSampleRateHz(),
                               GetEncoderFrameSizeMs(), /*bitrate_bps=*/20000);
  auto decoder = CreateDecoder(GetDecoderIsacImpl(), GetSampleRateHz());
  const int encoder_frame_size = GetEncoderFrameSize();
  std::vector<int16_t> out(encoder_frame_size);
  size_t num_encoded_samples = 0;
  size_t num_decoded_samples = 0;
  constexpr int kNumFrames = 12;
  for (int i = 0; i < kNumFrames; ++i) {
    AudioFrame in;
    pcm_file->Read10MsData(in);
    rtc::Buffer encoded;
    encoder->Encode(/*rtp_timestamp=*/0, AudioFrameToView(in), &encoded);
    num_encoded_samples += in.samples_per_channel();
    if (encoded.empty()) {
      continue;
    }
    // Decode.
    const std::vector<AudioDecoder::ParseResult> parse_result =
        decoder->ParsePayload(std::move(encoded), /*timestamp=*/0);
    EXPECT_EQ(parse_result.size(), size_t{1});
    auto decode_result = parse_result[0].frame->Decode(out);
    EXPECT_TRUE(decode_result.has_value());
    EXPECT_EQ(out.size(), decode_result->num_decoded_samples);
    num_decoded_samples += decode_result->num_decoded_samples;
  }
  EXPECT_EQ(num_encoded_samples, num_decoded_samples);
}

// Creates tests for different encoder frame sizes and different
// encoder/decoder implementations.
INSTANTIATE_TEST_SUITE_P(
    IsacApiTest,
    EncoderDecoderPairTest,
    ::testing::ValuesIn([] {
      std::vector<EncoderDecoderPairTestParams> cases;
      for (int frame_size_ms : {30, 60}) {
        for (IsacImpl enc : {IsacImpl::kFloat, IsacImpl::kFixed}) {
          for (IsacImpl dec : {IsacImpl::kFloat, IsacImpl::kFixed}) {
            cases.push_back({16000, frame_size_ms, enc, dec});
          }
        }
      }
      cases.push_back({32000, 30, IsacImpl::kFloat, IsacImpl::kFloat});
      return cases;
    }()),
    [](const ::testing::TestParamInfo<EncoderDecoderPairTestParams>& info) {
      rtc::StringBuilder b;
      const auto& p = info.param;
      b << p.sample_rate_hz << "_" << p.frame_size_ms << "_"
        << IsacImplToString(p.encoder_impl) << "_"
        << IsacImplToString(p.decoder_impl);
      return b.Release();
    });

}  // namespace
}  // namespace webrtc