Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: webrtc/modules/audio_coding/codecs/cng/webrtc_cng.cc

Issue 1868143002: Convert CNG into C++ and remove it from AudioDecoder (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Converted WebRtcCng to C++ Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2012 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/cng/webrtc_cng.h"
12
13 #include <algorithm>
14
15 #include <cstring>
hlundin-webrtc 2016/04/13 07:05:23 We tend to use the C-version of header files that
ossu 2016/04/13 11:57:06 Honestly, these should probably go away anyway. Al
kwiberg-webrtc 2016/04/14 09:42:47 Because that's how we've always done it. Also, it
hlundin-webrtc 2016/04/14 15:20:51 I don't know much more, but I know pbos did an eff
16 #include <cstdlib>
17
18 #include "webrtc/common_audio/signal_processing/include/signal_processing_librar y.h"
19
hlundin-webrtc 2016/04/13 07:05:23 Add a TODO to update variable an member names to f
ossu 2016/04/13 11:57:06 Acknowledged. I'll do that as a separate CL, I gue
hlundin-webrtc 2016/04/14 15:20:51 Acknowledged.
20 namespace {
21 void WebRtcCng_K2a16(
22 int16_t* k, int useOrder, int16_t* a);
23
24 const int32_t WebRtcCng_kDbov[94] = {
25 1081109975, 858756178, 682134279, 541838517, 430397633, 341876992,
26 271562548, 215709799, 171344384, 136103682, 108110997, 85875618,
27 68213428, 54183852, 43039763, 34187699, 27156255, 21570980,
28 17134438, 13610368, 10811100, 8587562, 6821343, 5418385,
29 4303976, 3418770, 2715625, 2157098, 1713444, 1361037,
30 1081110, 858756, 682134, 541839, 430398, 341877,
31 271563, 215710, 171344, 136104, 108111, 85876,
32 68213, 54184, 43040, 34188, 27156, 21571,
33 17134, 13610, 10811, 8588, 6821, 5418,
34 4304, 3419, 2716, 2157, 1713, 1361,
35 1081, 859, 682, 542, 430, 342,
36 272, 216, 171, 136, 108, 86,
37 68, 54, 43, 34, 27, 22,
38 17, 14, 11, 9, 7, 5,
39 4, 3, 3, 2, 2, 1,
40 1, 1, 1, 1
41 };
42
43 const int16_t WebRtcCng_kCorrWindow[WEBRTC_CNG_MAX_LPC_ORDER] = {
44 32702, 32636, 32570, 32505, 32439, 32374,
45 32309, 32244, 32179, 32114, 32049, 31985
46 };
47
48 } // namespace
49
50 namespace webrtc {
51
52 ComfortNoiseDecoder::ComfortNoiseDecoder() {
53 /* Needed to get the right function pointers in SPLIB. */
54 WebRtcSpl_Init();
55 Reset();
56 }
57
58 void ComfortNoiseDecoder::Reset() {
59 dec_seed_ = 7777; /* For debugging only. */
60 dec_target_energy_ = 0;
61 dec_used_energy_ = 0;
62 for (auto& c : dec_target_reflCoefs_)
63 c = 0;
64 for (auto& c : dec_used_reflCoefs_)
65 c = 0;
66 for (auto& c : dec_filtstate_)
67 c = 0;
68 for (auto& c : dec_filtstateLow_)
69 c = 0;
70 dec_order_ = 5;
71 dec_target_scale_factor_ = 0;
72 dec_used_scale_factor_ = 0;
73 }
74
75 void ComfortNoiseDecoder::UpdateSid(rtc::ArrayView<const uint8_t> SID) {
76 int16_t refCs[WEBRTC_CNG_MAX_LPC_ORDER];
77 int32_t targetEnergy;
78 size_t length = SID.size();
79 /* Throw away reflection coefficients of higher order than we can handle. */
80 if (length > (WEBRTC_CNG_MAX_LPC_ORDER + 1))
81 length = WEBRTC_CNG_MAX_LPC_ORDER + 1;
82
83 dec_order_ = static_cast<uint16_t>(length - 1);
84
85 uint8_t sid0 = std::min<uint8_t>(SID[0], 93);
86 targetEnergy = WebRtcCng_kDbov[sid0];
87 /* Take down target energy to 75%. */
88 targetEnergy = targetEnergy >> 1;
89 targetEnergy += targetEnergy >> 2;
90
91 dec_target_energy_ = targetEnergy;
92
93 /* Reconstruct coeffs with tweak for WebRtc implementation of RFC3389. */
94 if (dec_order_ == WEBRTC_CNG_MAX_LPC_ORDER) {
95 for (size_t i = 0; i < (dec_order_); i++) {
96 refCs[i] = SID[i + 1] << 8; /* Q7 to Q15*/
97 dec_target_reflCoefs_[i] = refCs[i];
98 }
99 } else {
100 for (size_t i = 0; i < (dec_order_); i++) {
101 refCs[i] = (SID[i + 1] - 127) << 8; /* Q7 to Q15. */
102 dec_target_reflCoefs_[i] = refCs[i];
103 }
104 }
105
106 for (size_t i = (dec_order_); i < WEBRTC_CNG_MAX_LPC_ORDER; i++) {
107 refCs[i] = 0;
108 dec_target_reflCoefs_[i] = refCs[i];
109 }
110 }
111
112 bool ComfortNoiseDecoder::Generate(rtc::ArrayView<int16_t> outData,
113 bool new_period) {
114 int16_t excitation[WEBRTC_CNG_MAX_OUTSIZE_ORDER];
115 int16_t low[WEBRTC_CNG_MAX_OUTSIZE_ORDER];
116 int16_t lpPoly[WEBRTC_CNG_MAX_LPC_ORDER + 1];
117 int16_t ReflBetaStd = 26214; /* 0.8 in q15. */
118 int16_t ReflBetaCompStd = 6553; /* 0.2 in q15. */
119 int16_t ReflBetaNewP = 19661; /* 0.6 in q15. */
120 int16_t ReflBetaCompNewP = 13107; /* 0.4 in q15. */
121 int16_t Beta, BetaC, tmp1, tmp2, tmp3;
122 int32_t targetEnergy;
123 int16_t En;
124 int16_t temp16;
125 const size_t nrOfSamples = outData.size();
126
127 if (nrOfSamples > WEBRTC_CNG_MAX_OUTSIZE_ORDER) {
128 return false;
129 }
130
131 if (new_period) {
132 dec_used_scale_factor_ = dec_target_scale_factor_;
133 Beta = ReflBetaNewP;
134 BetaC = ReflBetaCompNewP;
135 } else {
136 Beta = ReflBetaStd;
137 BetaC = ReflBetaCompStd;
138 }
139
140 /* Here we use a 0.5 weighting, should possibly be modified to 0.6. */
141 tmp1 = dec_used_scale_factor_ << 2; /* Q13->Q15 */
142 tmp2 = dec_target_scale_factor_ << 2; /* Q13->Q15 */
143 tmp3 = (int16_t) WEBRTC_SPL_MUL_16_16_RSFT(tmp1, Beta, 15);
144 tmp3 += (int16_t) WEBRTC_SPL_MUL_16_16_RSFT(tmp2, BetaC, 15);
145 dec_used_scale_factor_ = tmp3 >> 2; /* Q15->Q13 */
146
147 dec_used_energy_ = dec_used_energy_ >> 1;
148 dec_used_energy_ += dec_target_energy_ >> 1;
149
150 /* Do the same for the reflection coeffs. */
151 for (size_t i = 0; i < WEBRTC_CNG_MAX_LPC_ORDER; i++) {
152 dec_used_reflCoefs_[i] = (int16_t) WEBRTC_SPL_MUL_16_16_RSFT(
153 dec_used_reflCoefs_[i], Beta, 15);
154 dec_used_reflCoefs_[i] += (int16_t) WEBRTC_SPL_MUL_16_16_RSFT(
155 dec_target_reflCoefs_[i], BetaC, 15);
156 }
157
158 /* Compute the polynomial coefficients. */
159 WebRtcCng_K2a16(dec_used_reflCoefs_, WEBRTC_CNG_MAX_LPC_ORDER, lpPoly);
160
161
162 targetEnergy = dec_used_energy_;
163
164 /* Calculate scaling factor based on filter energy. */
165 En = 8192; /* 1.0 in Q13. */
166 for (size_t i = 0; i < (WEBRTC_CNG_MAX_LPC_ORDER); i++) {
167 /* Floating point value for reference.
168 E *= 1.0 - (dec_used_reflCoefs_[i] / 32768.0) *
169 (dec_used_reflCoefs_[i] / 32768.0);
170 */
171
172 /* Same in fixed point. */
173 /* K(i).^2 in Q15. */
174 temp16 = (int16_t) WEBRTC_SPL_MUL_16_16_RSFT(
175 dec_used_reflCoefs_[i], dec_used_reflCoefs_[i], 15);
176 /* 1 - K(i).^2 in Q15. */
177 temp16 = 0x7fff - temp16;
178 En = (int16_t) WEBRTC_SPL_MUL_16_16_RSFT(En, temp16, 15);
179 }
180
181 /* float scaling= sqrt(E * dec_target_energy_ / (1 << 24)); */
182
183 /* Calculate sqrt(En * target_energy / excitation energy) */
184 targetEnergy = WebRtcSpl_Sqrt(dec_used_energy_);
185
186 En = (int16_t) WebRtcSpl_Sqrt(En) << 6;
187 En = (En * 3) >> 1; /* 1.5 estimates sqrt(2). */
188 dec_used_scale_factor_ = (int16_t)((En * targetEnergy) >> 12);
189
190 /* Generate excitation. */
191 /* Excitation energy per sample is 2.^24 - Q13 N(0,1). */
192 for (size_t i = 0; i < nrOfSamples; i++) {
193 excitation[i] = WebRtcSpl_RandN(&dec_seed_) >> 1;
194 }
195
196 /* Scale to correct energy. */
197 WebRtcSpl_ScaleVector(excitation, excitation, dec_used_scale_factor_,
198 nrOfSamples, 13);
199
200 /* |lpPoly| - Coefficients in Q12.
201 * |excitation| - Speech samples.
202 * |nst->dec_filtstate| - State preservation.
203 * |outData| - Filtered speech samples. */
204 WebRtcSpl_FilterAR(lpPoly, WEBRTC_CNG_MAX_LPC_ORDER + 1, excitation,
205 nrOfSamples, dec_filtstate_, WEBRTC_CNG_MAX_LPC_ORDER,
206 dec_filtstateLow_, WEBRTC_CNG_MAX_LPC_ORDER,
207 outData.data(), low, nrOfSamples);
208
209 return true;
210 }
211
212 ComfortNoiseEncoder::ComfortNoiseEncoder(int fs, int interval, int quality)
213 : enc_nrOfCoefs_(quality),
214 enc_sampfreq_(fs),
215 enc_interval_(interval),
216 enc_msSinceSID_(0),
217 enc_Energy_(0),
218 enc_reflCoefs_{0},
219 enc_corrVector_{0},
220 enc_seed_(7777) /* For debugging only. */ {
221 RTC_CHECK(quality <= WEBRTC_CNG_MAX_LPC_ORDER && quality > 0);
222 /* Needed to get the right function pointers in SPLIB. */
223 WebRtcSpl_Init();
224 }
225
226 void ComfortNoiseEncoder::Reset(int fs, int interval, int quality) {
227 RTC_CHECK(quality <= WEBRTC_CNG_MAX_LPC_ORDER && quality > 0);
228 enc_nrOfCoefs_ = quality;
229 enc_sampfreq_ = fs;
230 enc_interval_ = interval;
231 enc_msSinceSID_ = 0;
232 enc_Energy_ = 0;
233 for (auto& c : enc_reflCoefs_)
234 c = 0;
235 for (auto& c : enc_corrVector_)
236 c = 0;
237 enc_seed_ = 7777; /* For debugging only. */
238 }
239
240 int ComfortNoiseEncoder::Encode(rtc::ArrayView<const int16_t> speech,
241 rtc::ArrayView<uint8_t> output,
242 bool forceSID) {
243 int16_t arCoefs[WEBRTC_CNG_MAX_LPC_ORDER + 1];
244 int32_t corrVector[WEBRTC_CNG_MAX_LPC_ORDER + 1];
245 int16_t refCs[WEBRTC_CNG_MAX_LPC_ORDER + 1];
246 int16_t hanningW[WEBRTC_CNG_MAX_OUTSIZE_ORDER];
247 int16_t ReflBeta = 19661; /* 0.6 in q15. */
248 int16_t ReflBetaComp = 13107; /* 0.4 in q15. */
249 int32_t outEnergy;
250 int outShifts;
251 size_t i;
252 int stab;
253 int acorrScale;
254 size_t index;
255 size_t ind, factor;
256 int32_t* bptr;
257 int32_t blo, bhi;
258 int16_t negate;
259 const int16_t* aptr;
260 int16_t speechBuf[WEBRTC_CNG_MAX_OUTSIZE_ORDER];
261
262 const size_t nrOfSamples = speech.size();
263
264 /* Check framesize. */
265 if (nrOfSamples > WEBRTC_CNG_MAX_OUTSIZE_ORDER) {
266 return -1;
267 }
268
269 RTC_CHECK_GE(output.size(), enc_nrOfCoefs_ + 1);
270
271 for (i = 0; i < nrOfSamples; i++) {
272 speechBuf[i] = speech[i];
273 }
274
275 factor = nrOfSamples;
276
277 /* Calculate energy and a coefficients. */
278 outEnergy = WebRtcSpl_Energy(speechBuf, nrOfSamples, &outShifts);
279 while (outShifts > 0) {
280 /* We can only do 5 shifts without destroying accuracy in
281 * division factor. */
282 if (outShifts > 5) {
283 outEnergy <<= (outShifts - 5);
284 outShifts = 5;
285 } else {
286 factor /= 2;
287 outShifts--;
288 }
289 }
290 outEnergy = WebRtcSpl_DivW32W16(outEnergy, (int16_t)factor);
291
292 if (outEnergy > 1) {
293 /* Create Hanning Window. */
294 WebRtcSpl_GetHanningWindow(hanningW, nrOfSamples / 2);
295 for (i = 0; i < (nrOfSamples / 2); i++)
296 hanningW[nrOfSamples - i - 1] = hanningW[i];
297
298 WebRtcSpl_ElementwiseVectorMult(speechBuf, hanningW, speechBuf, nrOfSamples,
299 14);
300
301 WebRtcSpl_AutoCorrelation(speechBuf, nrOfSamples, enc_nrOfCoefs_,
302 corrVector, &acorrScale);
303
304 if (*corrVector == 0)
305 *corrVector = WEBRTC_SPL_WORD16_MAX;
306
307 /* Adds the bandwidth expansion. */
308 aptr = WebRtcCng_kCorrWindow;
309 bptr = corrVector;
310
311 /* (zzz) lpc16_1 = 17+1+820+2+2 = 842 (ordo2=700). */
312 for (ind = 0; ind < enc_nrOfCoefs_; ind++) {
313 /* The below code multiplies the 16 b corrWindow values (Q15) with
314 * the 32 b corrvector (Q0) and shifts the result down 15 steps. */
315 negate = *bptr < 0;
316 if (negate)
317 *bptr = -*bptr;
318
319 blo = (int32_t) * aptr * (*bptr & 0xffff);
320 bhi = ((blo >> 16) & 0xffff)
321 + ((int32_t)(*aptr++) * ((*bptr >> 16) & 0xffff));
322 blo = (blo & 0xffff) | ((bhi & 0xffff) << 16);
323
324 *bptr = (((bhi >> 16) & 0x7fff) << 17) | ((uint32_t) blo >> 15);
325 if (negate)
326 *bptr = -*bptr;
327 bptr++;
328 }
329 /* End of bandwidth expansion. */
330
331 stab = WebRtcSpl_LevinsonDurbin(corrVector, arCoefs, refCs,
332 enc_nrOfCoefs_);
333
334 if (!stab) {
335 /* Disregard from this frame */
336 return 0;
337 }
338
339 } else {
340 for (i = 0; i < enc_nrOfCoefs_; i++)
341 refCs[i] = 0;
342 }
343
344 if (forceSID) {
345 /* Read instantaneous values instead of averaged. */
346 for (i = 0; i < enc_nrOfCoefs_; i++)
347 enc_reflCoefs_[i] = refCs[i];
348 enc_Energy_ = outEnergy;
349 } else {
350 /* Average history with new values. */
351 for (i = 0; i < enc_nrOfCoefs_; i++) {
352 enc_reflCoefs_[i] = (int16_t) WEBRTC_SPL_MUL_16_16_RSFT(
353 enc_reflCoefs_[i], ReflBeta, 15);
354 enc_reflCoefs_[i] +=
355 (int16_t) WEBRTC_SPL_MUL_16_16_RSFT(refCs[i], ReflBetaComp, 15);
356 }
357 enc_Energy_ =
358 (outEnergy >> 2) + (enc_Energy_ >> 1) + (enc_Energy_ >> 2);
359 }
360
361 if (enc_Energy_ < 1) {
362 enc_Energy_ = 1;
363 }
364
365 if ((enc_msSinceSID_ > (enc_interval_ - 1)) || forceSID) {
366 /* Search for best dbov value. */
367 index = 0;
368 for (i = 1; i < 93; i++) {
369 /* Always round downwards. */
370 if ((enc_Energy_ - WebRtcCng_kDbov[i]) > 0) {
371 index = i;
372 break;
373 }
374 }
375 if ((i == 93) && (index == 0))
376 index = 94;
377 output[0] = (uint8_t)index;
378
379 /* Quantize coefficients with tweak for WebRtc implementation of RFC3389. */
380 if (enc_nrOfCoefs_ == WEBRTC_CNG_MAX_LPC_ORDER) {
381 for (i = 0; i < enc_nrOfCoefs_; i++) {
382 /* Q15 to Q7 with rounding. */
383 output[i + 1] = ((enc_reflCoefs_[i] + 128) >> 8);
384 }
385 } else {
386 for (i = 0; i < enc_nrOfCoefs_; i++) {
387 /* Q15 to Q7 with rounding. */
388 output[i + 1] = (127 + ((enc_reflCoefs_[i] + 128) >> 8));
389 }
390 }
391
392 enc_msSinceSID_ =
393 static_cast<int16_t>((1000 * nrOfSamples) / enc_sampfreq_);
394 return static_cast<int>(enc_nrOfCoefs_ + 1);
395 } else {
396 enc_msSinceSID_ +=
397 static_cast<int16_t>((1000 * nrOfSamples) / enc_sampfreq_);
398 return 0;
399 }
400 }
401
402 } // namespace webrtc
403
404 namespace {
405 /* Values in |k| are Q15, and |a| Q12. */
406 void WebRtcCng_K2a16(int16_t* k, int useOrder, int16_t* a) {
407 int16_t any[WEBRTC_SPL_MAX_LPC_ORDER + 1];
408 int16_t* aptr;
409 int16_t* aptr2;
410 int16_t* anyptr;
411 const int16_t* kptr;
412 int m, i;
413
414 kptr = k;
415 *a = 4096; /* i.e., (Word16_MAX >> 3) + 1 */
416 *any = *a;
417 a[1] = (*k + 4) >> 3;
418 for (m = 1; m < useOrder; m++) {
419 kptr++;
420 aptr = a;
421 aptr++;
422 aptr2 = &a[m];
423 anyptr = any;
424 anyptr++;
425
426 any[m + 1] = (*kptr + 4) >> 3;
427 for (i = 0; i < m; i++) {
428 *anyptr++ =
429 (*aptr++) +
430 (int16_t)((((int32_t)(*aptr2--) * (int32_t)*kptr) + 16384) >> 15);
431 }
432
433 aptr = a;
434 anyptr = any;
435 for (i = 0; i < (m + 2); i++) {
436 *aptr++ = *anyptr++;
437 }
438 }
439 }
440
441 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698