niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 | /****************************************************************** |
| 12 | |
| 13 | iLBC Speech Coder ANSI-C Source Code |
| 14 | |
| 15 | WebRtcIlbcfix_CbConstruct.c |
| 16 | |
| 17 | ******************************************************************/ |
| 18 | |
Mirko Bonadei | 06c2aa9 | 2018-02-01 15:11:41 +0100 | [diff] [blame] | 19 | #include "modules/audio_coding/codecs/ilbc/cb_construct.h" |
kwiberg | 619a211 | 2016-08-24 02:46:44 -0700 | [diff] [blame] | 20 | |
Mirko Bonadei | 06c2aa9 | 2018-02-01 15:11:41 +0100 | [diff] [blame] | 21 | #include "modules/audio_coding/codecs/ilbc/defines.h" |
| 22 | #include "modules/audio_coding/codecs/ilbc/gain_dequant.h" |
| 23 | #include "modules/audio_coding/codecs/ilbc/get_cd_vec.h" |
Karl Wiberg | 6f3d01c | 2018-04-26 11:18:33 +0200 | [diff] [blame] | 24 | #include "rtc_base/sanitizer.h" |
| 25 | |
| 26 | // An arithmetic operation that is allowed to overflow. (It's still undefined |
| 27 | // behavior, so not a good idea; this just makes UBSan ignore the violation, so |
| 28 | // that our old code can continue to do what it's always been doing.) |
| 29 | static inline int32_t RTC_NO_SANITIZE("signed-integer-overflow") |
| 30 | OverflowingAddS32S32ToS32(int32_t a, int32_t b) { |
| 31 | return a + b; |
| 32 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | |
| 34 | /*----------------------------------------------------------------* |
| 35 | * Construct decoded vector from codebook and gains. |
| 36 | *---------------------------------------------------------------*/ |
| 37 | |
kwiberg | 619a211 | 2016-08-24 02:46:44 -0700 | [diff] [blame] | 38 | bool WebRtcIlbcfix_CbConstruct( |
kwiberg | c31446f | 2016-08-30 05:36:56 -0700 | [diff] [blame] | 39 | int16_t* decvector, /* (o) Decoded vector */ |
| 40 | const int16_t* index, /* (i) Codebook indices */ |
| 41 | const int16_t* gain_index, /* (i) Gain quantization indices */ |
| 42 | int16_t* mem, /* (i) Buffer for codevector construction */ |
| 43 | size_t lMem, /* (i) Length of buffer */ |
| 44 | size_t veclen) { /* (i) Length of vector */ |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 45 | size_t j; |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 46 | int16_t gain[CB_NSTAGES]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | /* Stack based */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 48 | int16_t cbvec0[SUBL]; |
| 49 | int16_t cbvec1[SUBL]; |
| 50 | int16_t cbvec2[SUBL]; |
| 51 | int32_t a32; |
| 52 | int16_t *gainPtr; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 53 | |
| 54 | /* gain de-quantization */ |
| 55 | |
| 56 | gain[0] = WebRtcIlbcfix_GainDequant(gain_index[0], 16384, 0); |
| 57 | gain[1] = WebRtcIlbcfix_GainDequant(gain_index[1], gain[0], 1); |
| 58 | gain[2] = WebRtcIlbcfix_GainDequant(gain_index[2], gain[1], 2); |
| 59 | |
| 60 | /* codebook vector construction and construction of total vector */ |
| 61 | |
| 62 | /* Stack based */ |
kwiberg | 619a211 | 2016-08-24 02:46:44 -0700 | [diff] [blame] | 63 | if (!WebRtcIlbcfix_GetCbVec(cbvec0, mem, (size_t)index[0], lMem, veclen)) |
| 64 | return false; // Failure. |
| 65 | if (!WebRtcIlbcfix_GetCbVec(cbvec1, mem, (size_t)index[1], lMem, veclen)) |
| 66 | return false; // Failure. |
| 67 | if (!WebRtcIlbcfix_GetCbVec(cbvec2, mem, (size_t)index[2], lMem, veclen)) |
| 68 | return false; // Failure. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | |
| 70 | gainPtr = &gain[0]; |
| 71 | for (j=0;j<veclen;j++) { |
bjornv@webrtc.org | ba97ea6 | 2015-02-13 09:51:40 +0000 | [diff] [blame] | 72 | a32 = (*gainPtr++) * cbvec0[j]; |
| 73 | a32 += (*gainPtr++) * cbvec1[j]; |
Karl Wiberg | 6f3d01c | 2018-04-26 11:18:33 +0200 | [diff] [blame] | 74 | a32 = OverflowingAddS32S32ToS32(a32, (*gainPtr) * cbvec2[j]); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | gainPtr -= 2; |
bjornv@webrtc.org | 78ea06d | 2014-10-21 07:17:24 +0000 | [diff] [blame] | 76 | decvector[j] = (int16_t)((a32 + 8192) >> 14); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 77 | } |
| 78 | |
kwiberg | 619a211 | 2016-08-24 02:46:44 -0700 | [diff] [blame] | 79 | return true; // Success. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | } |