1 /*
2 * Copyright (c) 2014 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/codecs/tools/audio_codec_speed_test.h"
12
13 #include "rtc_base/checks.h"
14 #include "test/gtest.h"
15 #include "test/testsupport/file_utils.h"
16
17 using ::std::get;
18
19 namespace webrtc {
20
AudioCodecSpeedTest(int block_duration_ms,int input_sampling_khz,int output_sampling_khz)21 AudioCodecSpeedTest::AudioCodecSpeedTest(int block_duration_ms,
22 int input_sampling_khz,
23 int output_sampling_khz)
24 : block_duration_ms_(block_duration_ms),
25 input_sampling_khz_(input_sampling_khz),
26 output_sampling_khz_(output_sampling_khz),
27 input_length_sample_(
28 static_cast<size_t>(block_duration_ms_ * input_sampling_khz_)),
29 output_length_sample_(
30 static_cast<size_t>(block_duration_ms_ * output_sampling_khz_)),
31 data_pointer_(0),
32 loop_length_samples_(0),
33 max_bytes_(0),
34 encoded_bytes_(0),
35 encoding_time_ms_(0.0),
36 decoding_time_ms_(0.0),
37 out_file_(NULL) {}
38
SetUp()39 void AudioCodecSpeedTest::SetUp() {
40 channels_ = get<0>(GetParam());
41 bit_rate_ = get<1>(GetParam());
42 in_filename_ = test::ResourcePath(get<2>(GetParam()), get<3>(GetParam()));
43 save_out_data_ = get<4>(GetParam());
44
45 FILE* fp = fopen(in_filename_.c_str(), "rb");
46 RTC_DCHECK(fp);
47
48 // Obtain file size.
49 fseek(fp, 0, SEEK_END);
50 loop_length_samples_ = ftell(fp) / sizeof(int16_t);
51 rewind(fp);
52
53 // Allocate memory to contain the whole file.
54 in_data_.reset(
55 new int16_t[loop_length_samples_ + input_length_sample_ * channels_]);
56
57 data_pointer_ = 0;
58
59 // Copy the file into the buffer.
60 ASSERT_EQ(fread(&in_data_[0], sizeof(int16_t), loop_length_samples_, fp),
61 loop_length_samples_);
62 fclose(fp);
63
64 // Add an extra block length of samples to the end of the array, starting
65 // over again from the beginning of the array. This is done to simplify
66 // the reading process when reading over the end of the loop.
67 memcpy(&in_data_[loop_length_samples_], &in_data_[0],
68 input_length_sample_ * channels_ * sizeof(int16_t));
69
70 max_bytes_ = input_length_sample_ * channels_ * sizeof(int16_t);
71 out_data_.reset(new int16_t[output_length_sample_ * channels_]);
72 bit_stream_.reset(new uint8_t[max_bytes_]);
73
74 if (save_out_data_) {
75 std::string out_filename =
76 ::testing::UnitTest::GetInstance()->current_test_info()->name();
77
78 // Erase '/'
79 size_t found;
80 while ((found = out_filename.find('/')) != std::string::npos)
81 out_filename.replace(found, 1, "_");
82
83 out_filename = test::OutputPath() + out_filename + ".pcm";
84
85 out_file_ = fopen(out_filename.c_str(), "wb");
86 RTC_DCHECK(out_file_);
87
88 printf("Output to be saved in %s.\n", out_filename.c_str());
89 }
90 }
91
TearDown()92 void AudioCodecSpeedTest::TearDown() {
93 if (save_out_data_) {
94 fclose(out_file_);
95 }
96 }
97
EncodeDecode(size_t audio_duration_sec)98 void AudioCodecSpeedTest::EncodeDecode(size_t audio_duration_sec) {
99 size_t time_now_ms = 0;
100 float time_ms;
101
102 printf("Coding %d kHz-sampled %zu-channel audio at %d bps ...\n",
103 input_sampling_khz_, channels_, bit_rate_);
104
105 while (time_now_ms < audio_duration_sec * 1000) {
106 // Encode & decode.
107 time_ms = EncodeABlock(&in_data_[data_pointer_], &bit_stream_[0],
108 max_bytes_, &encoded_bytes_);
109 encoding_time_ms_ += time_ms;
110 time_ms = DecodeABlock(&bit_stream_[0], encoded_bytes_, &out_data_[0]);
111 decoding_time_ms_ += time_ms;
112 if (save_out_data_) {
113 fwrite(&out_data_[0], sizeof(int16_t), output_length_sample_ * channels_,
114 out_file_);
115 }
116 data_pointer_ = (data_pointer_ + input_length_sample_ * channels_) %
117 loop_length_samples_;
118 time_now_ms += block_duration_ms_;
119 }
120
121 printf("Encoding: %.2f%% real time,\nDecoding: %.2f%% real time.\n",
122 (encoding_time_ms_ / audio_duration_sec) / 10.0,
123 (decoding_time_ms_ / audio_duration_sec) / 10.0);
124 }
125
126 } // namespace webrtc
127