Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_coding / codecs / isac / fix / test / isac_speed_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/codecs/isac/fix/source/settings.h"
13 #include "webrtc/modules/audio_coding/codecs/tools/audio_codec_speed_test.h"
14
15 using ::std::string;
16
17 namespace webrtc {
18
19 static const int kIsacBlockDurationMs = 30;
20 static const int kIsacInputSamplingKhz = 16;
21 static const int kIsacOutputSamplingKhz = 16;
22
23 class IsacSpeedTest : public AudioCodecSpeedTest {
24  protected:
25   IsacSpeedTest();
26   virtual void SetUp() OVERRIDE;
27   virtual void TearDown() OVERRIDE;
28   virtual float EncodeABlock(int16_t* in_data, uint8_t* bit_stream,
29                              int max_bytes, int* encoded_bytes);
30   virtual float DecodeABlock(const uint8_t* bit_stream, int encoded_bytes,
31                              int16_t* out_data);
32   ISACFIX_MainStruct *ISACFIX_main_inst_;
33 };
34
35 IsacSpeedTest::IsacSpeedTest()
36     : AudioCodecSpeedTest(kIsacBlockDurationMs,
37                           kIsacInputSamplingKhz,
38                           kIsacOutputSamplingKhz),
39       ISACFIX_main_inst_(NULL) {
40 }
41
42 void IsacSpeedTest::SetUp() {
43   AudioCodecSpeedTest::SetUp();
44
45   // Check whether the allocated buffer for the bit stream is large enough.
46   EXPECT_GE(max_bytes_, STREAM_MAXW16_60MS);
47
48   // Create encoder memory.
49   EXPECT_EQ(0, WebRtcIsacfix_Create(&ISACFIX_main_inst_));
50   EXPECT_EQ(0, WebRtcIsacfix_EncoderInit(ISACFIX_main_inst_, 1));
51   EXPECT_EQ(0, WebRtcIsacfix_DecoderInit(ISACFIX_main_inst_));
52   // Set bitrate and block length.
53   EXPECT_EQ(0, WebRtcIsacfix_Control(ISACFIX_main_inst_, bit_rate_,
54                                      block_duration_ms_));
55 }
56
57 void IsacSpeedTest::TearDown() {
58   AudioCodecSpeedTest::TearDown();
59   // Free memory.
60   EXPECT_EQ(0, WebRtcIsacfix_Free(ISACFIX_main_inst_));
61 }
62
63 float IsacSpeedTest::EncodeABlock(int16_t* in_data, uint8_t* bit_stream,
64                                   int max_bytes, int* encoded_bytes) {
65   // ISAC takes 10 ms everycall
66   const int subblocks = block_duration_ms_ / 10;
67   const int subblock_length = 10 * input_sampling_khz_;
68   int value;
69
70   clock_t clocks = clock();
71   size_t pointer = 0;
72   for (int idx = 0; idx < subblocks; idx++, pointer += subblock_length) {
73     value = WebRtcIsacfix_Encode(ISACFIX_main_inst_, &in_data[pointer],
74                                  bit_stream);
75   }
76   clocks = clock() - clocks;
77   EXPECT_GT(value, 0);
78   assert(value <= max_bytes);
79   *encoded_bytes = value;
80   return 1000.0 * clocks / CLOCKS_PER_SEC;
81 }
82
83 float IsacSpeedTest::DecodeABlock(const uint8_t* bit_stream, int encoded_bytes,
84                                   int16_t* out_data) {
85   int value;
86   int16_t audio_type;
87   clock_t clocks = clock();
88   value = WebRtcIsacfix_Decode(ISACFIX_main_inst_,
89                                reinterpret_cast<const uint16_t*>(bit_stream),
90                                encoded_bytes, out_data, &audio_type);
91   clocks = clock() - clocks;
92   EXPECT_EQ(output_length_sample_, value);
93   return 1000.0 * clocks / CLOCKS_PER_SEC;
94 }
95
96 TEST_P(IsacSpeedTest, IsacEncodeDecodeTest) {
97   size_t kDurationSec = 400;  // Test audio length in second.
98   EncodeDecode(kDurationSec);
99 }
100
101 const coding_param param_set[] =
102     {::std::tr1::make_tuple(1, 32000, string("audio_coding/speech_mono_16kHz"),
103                             string("pcm"), true)};
104
105 INSTANTIATE_TEST_CASE_P(AllTest, IsacSpeedTest,
106                         ::testing::ValuesIn(param_set));
107
108 }  // namespace webrtc