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