Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_coding / neteq / test / neteq_isac_quality_test.cc
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/isac/fix/interface/isacfix.h"
12 #include "webrtc/modules/audio_coding/neteq/tools/neteq_quality_test.h"
13 #include "webrtc/test/testsupport/fileutils.h"
14
15 using google::RegisterFlagValidator;
16 using google::ParseCommandLineFlags;
17 using std::string;
18 using testing::InitGoogleTest;
19
20 namespace webrtc {
21 namespace test {
22
23 static const int kIsacBlockDurationMs = 30;
24 static const int kIsacInputSamplingKhz = 16;
25 static const int kIsacOutputSamplingKhz = 16;
26
27 // Define switch for input file name.
28 static bool ValidateInFilename(const char* flagname, const string& value) {
29   FILE* fid = fopen(value.c_str(), "rb");
30   if (fid != NULL) {
31     fclose(fid);
32     return true;
33   }
34   printf("Invalid input filename.");
35   return false;
36 }
37
38 DEFINE_string(in_filename,
39               ResourcePath("audio_coding/speech_mono_16kHz", "pcm"),
40               "Filename for input audio (should be 16 kHz sampled mono).");
41
42 static const bool in_filename_dummy =
43     RegisterFlagValidator(&FLAGS_in_filename, &ValidateInFilename);
44
45 // Define switch for output file name.
46 static bool ValidateOutFilename(const char* flagname, const string& value) {
47   FILE* fid = fopen(value.c_str(), "wb");
48   if (fid != NULL) {
49     fclose(fid);
50     return true;
51   }
52   printf("Invalid output filename.");
53   return false;
54 }
55
56 DEFINE_string(out_filename, OutputPath() + "neteq4_isac_quality_test.pcm",
57               "Name of output audio file.");
58
59 static const bool out_filename_dummy =
60     RegisterFlagValidator(&FLAGS_out_filename, &ValidateOutFilename);
61
62 // Define switch for bir rate.
63 static bool ValidateBitRate(const char* flagname, int32_t value) {
64   if (value >= 10 && value <= 32)
65     return true;
66   printf("Invalid bit rate, should be between 10 and 32 kbps.");
67   return false;
68 }
69
70 DEFINE_int32(bit_rate_kbps, 32, "Target bit rate (kbps).");
71
72 static const bool bit_rate_dummy =
73     RegisterFlagValidator(&FLAGS_bit_rate_kbps, &ValidateBitRate);
74
75 // Define switch for runtime.
76 static bool ValidateRuntime(const char* flagname, int32_t value) {
77   if (value > 0)
78     return true;
79   printf("Invalid runtime, should be greater than 0.");
80   return false;
81 }
82
83 DEFINE_int32(runtime_ms, 10000, "Simulated runtime (milliseconds).");
84
85 static const bool runtime_dummy =
86     RegisterFlagValidator(&FLAGS_runtime_ms, &ValidateRuntime);
87
88 class NetEqIsacQualityTest : public NetEqQualityTest {
89  protected:
90   NetEqIsacQualityTest();
91   virtual void SetUp() OVERRIDE;
92   virtual void TearDown() OVERRIDE;
93   virtual int EncodeBlock(int16_t* in_data, int block_size_samples,
94                           uint8_t* payload, int max_bytes);
95  private:
96   ISACFIX_MainStruct* isac_encoder_;
97   int bit_rate_kbps_;
98 };
99
100 NetEqIsacQualityTest::NetEqIsacQualityTest()
101     : NetEqQualityTest(kIsacBlockDurationMs, kIsacInputSamplingKhz,
102                        kIsacOutputSamplingKhz,
103                        kDecoderISAC,
104                        1,
105                        FLAGS_in_filename,
106                        FLAGS_out_filename),
107       isac_encoder_(NULL),
108       bit_rate_kbps_(FLAGS_bit_rate_kbps) {
109 }
110
111 void NetEqIsacQualityTest::SetUp() {
112   // Create encoder memory.
113   WebRtcIsacfix_Create(&isac_encoder_);
114   ASSERT_TRUE(isac_encoder_ != NULL);
115   EXPECT_EQ(0, WebRtcIsacfix_EncoderInit(isac_encoder_, 1));
116   // Set bitrate and block length.
117   EXPECT_EQ(0, WebRtcIsacfix_Control(isac_encoder_, bit_rate_kbps_ * 1000,
118                                      kIsacBlockDurationMs));
119   NetEqQualityTest::SetUp();
120 }
121
122 void NetEqIsacQualityTest::TearDown() {
123   // Free memory.
124   EXPECT_EQ(0, WebRtcIsacfix_Free(isac_encoder_));
125   NetEqQualityTest::TearDown();
126 }
127
128 int NetEqIsacQualityTest::EncodeBlock(int16_t* in_data,
129                                       int block_size_samples,
130                                       uint8_t* payload, int max_bytes) {
131   // ISAC takes 10 ms for every call.
132   const int subblocks = kIsacBlockDurationMs / 10;
133   const int subblock_length = 10 * kIsacInputSamplingKhz;
134   int value = 0;
135
136   int pointer = 0;
137   for (int idx = 0; idx < subblocks; idx++, pointer += subblock_length) {
138     // The Isac encoder does not perform encoding (and returns 0) until it
139     // receives a sequence of sub-blocks that amount to the frame duration.
140     EXPECT_EQ(0, value);
141     value = WebRtcIsacfix_Encode(isac_encoder_, &in_data[pointer], payload);
142   }
143   EXPECT_GT(value, 0);
144   return value;
145 }
146
147 TEST_F(NetEqIsacQualityTest, Test) {
148   Simulate(FLAGS_runtime_ms);
149 }
150
151 }  // namespace test
152 }  // namespace webrtc