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