File: cng_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (348 lines) | stat: -rw-r--r-- 13,277 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
#include <string>

#include "testing/gtest/include/gtest/gtest.h"
#include "webrtc/test/testsupport/fileutils.h"
#include "webrtc_cng.h"

namespace webrtc {

enum {
  kSidShortIntervalUpdate = 1,
  kSidNormalIntervalUpdate = 100,
  kSidLongIntervalUpdate = 10000
};

enum {
  kCNGNumParamsLow = 0,
  kCNGNumParamsNormal = 8,
  kCNGNumParamsHigh = WEBRTC_CNG_MAX_LPC_ORDER,
  kCNGNumParamsTooHigh = WEBRTC_CNG_MAX_LPC_ORDER + 1
};

enum {
  kNoSid,
  kForceSid
};

class CngTest : public ::testing::Test {
 protected:
  CngTest();
  virtual void SetUp();

  CNG_enc_inst* cng_enc_inst_;
  CNG_dec_inst* cng_dec_inst_;
  int16_t speech_data_[640];  // Max size of CNG internal buffers.
};

CngTest::CngTest()
    : cng_enc_inst_(NULL),
      cng_dec_inst_(NULL) {
}

void CngTest::SetUp() {
  FILE* input_file;
  const std::string file_name =
        webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
  input_file = fopen(file_name.c_str(), "rb");
  ASSERT_TRUE(input_file != NULL);
  ASSERT_EQ(640, static_cast<int32_t>(fread(speech_data_, sizeof(int16_t),
                                             640, input_file)));
  fclose(input_file);
  input_file = NULL;
}

// Test failing Create.
TEST_F(CngTest, CngCreateFail) {
  // Test to see that an invalid pointer is caught.
  EXPECT_EQ(-1, WebRtcCng_CreateEnc(NULL));
  EXPECT_EQ(-1, WebRtcCng_CreateDec(NULL));
}

// Test normal Create.
TEST_F(CngTest, CngCreate) {
  EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_));
  EXPECT_EQ(0, WebRtcCng_CreateDec(&cng_dec_inst_));
  EXPECT_TRUE(cng_enc_inst_ != NULL);
  EXPECT_TRUE(cng_dec_inst_ != NULL);
  // Free encoder and decoder memory.
  EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_));
  EXPECT_EQ(0, WebRtcCng_FreeDec(cng_dec_inst_));
}

// Create CNG encoder, init with faulty values, free CNG encoder.
TEST_F(CngTest, CngInitFail) {
  // Create encoder memory.
  EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_));

  // Call with too few parameters.
  EXPECT_EQ(-1, WebRtcCng_InitEnc(cng_enc_inst_, 8000, kSidNormalIntervalUpdate,
                                  kCNGNumParamsLow));
  EXPECT_EQ(6130, WebRtcCng_GetErrorCodeEnc(cng_enc_inst_));

  // Call with too many parameters.
  EXPECT_EQ(-1, WebRtcCng_InitEnc(cng_enc_inst_, 8000, kSidNormalIntervalUpdate,
                                  kCNGNumParamsTooHigh));
  EXPECT_EQ(6130, WebRtcCng_GetErrorCodeEnc(cng_enc_inst_));

  // Free encoder memory.
  EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_));
}

TEST_F(CngTest, CngEncode) {
  uint8_t sid_data[WEBRTC_CNG_MAX_LPC_ORDER + 1];
  int16_t number_bytes;

  // Create encoder memory.
  EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_));

  // 8 kHz, Normal number of parameters
  EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 8000, kSidNormalIntervalUpdate,
                                 kCNGNumParamsNormal));
  EXPECT_EQ(0, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 80, sid_data,
                                &number_bytes, kNoSid));
  EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode(
      cng_enc_inst_, speech_data_, 80, sid_data, &number_bytes, kForceSid));

  // 16 kHz, Normal number of parameters
  EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 16000, kSidNormalIntervalUpdate,
                                 kCNGNumParamsNormal));
  EXPECT_EQ(0, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 160, sid_data,
                                &number_bytes, kNoSid));
  EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode(
      cng_enc_inst_, speech_data_, 160, sid_data, &number_bytes, kForceSid));

  // 32 kHz, Max number of parameters
  EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 32000, kSidNormalIntervalUpdate,
                                 kCNGNumParamsHigh));
  EXPECT_EQ(0, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 320, sid_data,
                                &number_bytes, kNoSid));
  EXPECT_EQ(kCNGNumParamsHigh + 1, WebRtcCng_Encode(
      cng_enc_inst_, speech_data_, 320, sid_data, &number_bytes, kForceSid));

  // 48 kHz, Normal number of parameters
  EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 48000, kSidNormalIntervalUpdate,
                                 kCNGNumParamsNormal));
  EXPECT_EQ(0, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 480, sid_data,
                                &number_bytes, kNoSid));
  EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode(
      cng_enc_inst_, speech_data_, 480, sid_data, &number_bytes, kForceSid));

  // 64 kHz, Normal number of parameters
  EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 64000, kSidNormalIntervalUpdate,
                                 kCNGNumParamsNormal));
  EXPECT_EQ(0, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 640, sid_data,
                                &number_bytes, kNoSid));
  EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode(
      cng_enc_inst_, speech_data_, 640, sid_data, &number_bytes, kForceSid));

  // Free encoder memory.
  EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_));
}

// Encode Cng with too long input vector.
TEST_F(CngTest, CngEncodeTooLong) {
  uint8_t sid_data[WEBRTC_CNG_MAX_LPC_ORDER + 1];
  int16_t number_bytes;

  // Create and init encoder memory.
  EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_));
  EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 8000, kSidNormalIntervalUpdate,
                                 kCNGNumParamsNormal));

  // Run encoder with too much data.
  EXPECT_EQ(-1, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 641, sid_data,
                                 &number_bytes, kNoSid));
  EXPECT_EQ(6140, WebRtcCng_GetErrorCodeEnc(cng_enc_inst_));

  // Free encoder memory.
  EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_));
}

// Call encode without calling init.
TEST_F(CngTest, CngEncodeNoInit) {
  uint8_t sid_data[WEBRTC_CNG_MAX_LPC_ORDER + 1];
  int16_t number_bytes;

  // Create encoder memory.
  EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_));

  // Run encoder without calling init.
  EXPECT_EQ(-1, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 640, sid_data,
                                 &number_bytes, kNoSid));
  EXPECT_EQ(6120, WebRtcCng_GetErrorCodeEnc(cng_enc_inst_));

  // Free encoder memory.
  EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_));
}

// Update SID parameters, for both 9 and 16 parameters.
TEST_F(CngTest, CngUpdateSid) {
  uint8_t sid_data[WEBRTC_CNG_MAX_LPC_ORDER + 1];
  int16_t number_bytes;

  // Create and initialize encoder and decoder memory.
  EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_));
  EXPECT_EQ(0, WebRtcCng_CreateDec(&cng_dec_inst_));
  EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 16000, kSidNormalIntervalUpdate,
                                 kCNGNumParamsNormal));
  EXPECT_EQ(0, WebRtcCng_InitDec(cng_dec_inst_));

  // Run normal Encode and UpdateSid.
  EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode(
      cng_enc_inst_, speech_data_, 160, sid_data, &number_bytes, kForceSid));
  EXPECT_EQ(0, WebRtcCng_UpdateSid(cng_dec_inst_, sid_data,
                                   kCNGNumParamsNormal + 1));

  // Reinit with new length.
  EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 16000, kSidNormalIntervalUpdate,
                                 kCNGNumParamsHigh));
  EXPECT_EQ(0, WebRtcCng_InitDec(cng_dec_inst_));

  // Expect 0 because of unstable parameters after switching length.
  EXPECT_EQ(0, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 160, sid_data,
                                &number_bytes, kForceSid));
  EXPECT_EQ(kCNGNumParamsHigh + 1, WebRtcCng_Encode(
      cng_enc_inst_, speech_data_ + 160, 160, sid_data, &number_bytes,
      kForceSid));
  EXPECT_EQ(0, WebRtcCng_UpdateSid(cng_dec_inst_, sid_data,
                                   kCNGNumParamsNormal + 1));

  // Free encoder and decoder memory.
  EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_));
  EXPECT_EQ(0, WebRtcCng_FreeDec(cng_dec_inst_));
}

// Update SID parameters, with wrong parameters or without calling decode.
TEST_F(CngTest, CngUpdateSidErroneous) {
  uint8_t sid_data[WEBRTC_CNG_MAX_LPC_ORDER + 1];
  int16_t number_bytes;

  // Create encoder and decoder memory.
  EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_));
  EXPECT_EQ(0, WebRtcCng_CreateDec(&cng_dec_inst_));

  // Encode.
  EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 16000, kSidNormalIntervalUpdate,
                                 kCNGNumParamsNormal));
  EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode(
      cng_enc_inst_, speech_data_, 160, sid_data, &number_bytes, kForceSid));

  // Update Sid before initializing decoder.
  EXPECT_EQ(-1, WebRtcCng_UpdateSid(cng_dec_inst_, sid_data,
                                    kCNGNumParamsNormal + 1));
  EXPECT_EQ(6220, WebRtcCng_GetErrorCodeDec(cng_dec_inst_));

  // Initialize decoder.
  EXPECT_EQ(0, WebRtcCng_InitDec(cng_dec_inst_));

  // First run with valid parameters, then with too many CNG parameters.
  // The function will operate correctly by only reading the maximum number of
  // parameters, skipping the extra.
  EXPECT_EQ(0, WebRtcCng_UpdateSid(cng_dec_inst_, sid_data,
                                   kCNGNumParamsNormal + 1));
  EXPECT_EQ(0, WebRtcCng_UpdateSid(cng_dec_inst_, sid_data,
                                   kCNGNumParamsTooHigh + 1));

  // Free encoder and decoder memory.
  EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_));
  EXPECT_EQ(0, WebRtcCng_FreeDec(cng_dec_inst_));
}

// Test to generate cng data, by forcing SID. Both normal and faulty condition.
TEST_F(CngTest, CngGenerate) {
  uint8_t sid_data[WEBRTC_CNG_MAX_LPC_ORDER + 1];
  int16_t out_data[640];
  int16_t number_bytes;

  // Create and initialize encoder and decoder memory.
  EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_));
  EXPECT_EQ(0, WebRtcCng_CreateDec(&cng_dec_inst_));
  EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 16000, kSidNormalIntervalUpdate,
                                 kCNGNumParamsNormal));
  EXPECT_EQ(0, WebRtcCng_InitDec(cng_dec_inst_));

  // Normal Encode.
  EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode(
      cng_enc_inst_, speech_data_, 160, sid_data, &number_bytes, kForceSid));

  // Normal UpdateSid.
  EXPECT_EQ(0, WebRtcCng_UpdateSid(cng_dec_inst_, sid_data,
                                   kCNGNumParamsNormal + 1));

  // Two normal Generate, one with new_period.
  EXPECT_EQ(0, WebRtcCng_Generate(cng_dec_inst_, out_data, 640, 1));
  EXPECT_EQ(0, WebRtcCng_Generate(cng_dec_inst_, out_data, 640, 0));

  // Call Genereate with too much data.
  EXPECT_EQ(-1, WebRtcCng_Generate(cng_dec_inst_, out_data, 641, 0));
  EXPECT_EQ(6140, WebRtcCng_GetErrorCodeDec(cng_dec_inst_));

  // Free encoder and decoder memory.
  EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_));
  EXPECT_EQ(0, WebRtcCng_FreeDec(cng_dec_inst_));
}

// Test automatic SID.
TEST_F(CngTest, CngAutoSid) {
  uint8_t sid_data[WEBRTC_CNG_MAX_LPC_ORDER + 1];
  int16_t number_bytes;

  // Create and initialize encoder and decoder memory.
  EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_));
  EXPECT_EQ(0, WebRtcCng_CreateDec(&cng_dec_inst_));
  EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 16000, kSidNormalIntervalUpdate,
                                 kCNGNumParamsNormal));
  EXPECT_EQ(0, WebRtcCng_InitDec(cng_dec_inst_));

  // Normal Encode, 100 msec, where no SID data should be generated.
  for (int i = 0; i < 10; i++) {
    EXPECT_EQ(0, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 160, sid_data,
                                  &number_bytes, kNoSid));
  }

  // We have reached 100 msec, and SID data should be generated.
  EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode(
      cng_enc_inst_, speech_data_, 160, sid_data, &number_bytes, kNoSid));

  // Free encoder and decoder memory.
  EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_));
  EXPECT_EQ(0, WebRtcCng_FreeDec(cng_dec_inst_));
}

// Test automatic SID, with very short interval.
TEST_F(CngTest, CngAutoSidShort) {
  uint8_t sid_data[WEBRTC_CNG_MAX_LPC_ORDER + 1];
  int16_t number_bytes;

  // Create and initialize encoder and decoder memory.
  EXPECT_EQ(0, WebRtcCng_CreateEnc(&cng_enc_inst_));
  EXPECT_EQ(0, WebRtcCng_CreateDec(&cng_dec_inst_));
  EXPECT_EQ(0, WebRtcCng_InitEnc(cng_enc_inst_, 16000, kSidShortIntervalUpdate,
                                 kCNGNumParamsNormal));
  EXPECT_EQ(0, WebRtcCng_InitDec(cng_dec_inst_));

  // First call will never generate SID, unless forced to.
  EXPECT_EQ(0, WebRtcCng_Encode(cng_enc_inst_, speech_data_, 160, sid_data,
                                &number_bytes, kNoSid));

  // Normal Encode, 100 msec, SID data should be generated all the time.
  for (int i = 0; i < 10; i++) {
    EXPECT_EQ(kCNGNumParamsNormal + 1, WebRtcCng_Encode(
        cng_enc_inst_, speech_data_, 160, sid_data, &number_bytes, kNoSid));
  }

  // Free encoder and decoder memory.
  EXPECT_EQ(0, WebRtcCng_FreeEnc(cng_enc_inst_));
  EXPECT_EQ(0, WebRtcCng_FreeDec(cng_dec_inst_));
}

}  // namespace webrtc