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_Refiner.c |
| 16 | |
| 17 | ******************************************************************/ |
| 18 | |
Timothy Gu | 3111783 | 2020-12-18 22:25:57 -0800 | [diff] [blame] | 19 | #include "modules/audio_coding/codecs/ilbc/refiner.h" |
| 20 | |
Mirko Bonadei | 06c2aa9 | 2018-02-01 15:11:41 +0100 | [diff] [blame] | 21 | #include "modules/audio_coding/codecs/ilbc/constants.h" |
| 22 | #include "modules/audio_coding/codecs/ilbc/enh_upsample.h" |
| 23 | #include "modules/audio_coding/codecs/ilbc/my_corr.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | |
| 25 | /*----------------------------------------------------------------* |
| 26 | * find segment starting near idata+estSegPos that has highest |
| 27 | * correlation with idata+centerStartPos through |
| 28 | * idata+centerStartPos+ENH_BLOCKL-1 segment is found at a |
| 29 | * resolution of ENH_UPSO times the original of the original |
| 30 | * sampling rate |
| 31 | *---------------------------------------------------------------*/ |
| 32 | |
| 33 | void WebRtcIlbcfix_Refiner( |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 34 | size_t *updStartPos, /* (o) updated start point (Q-2) */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 35 | int16_t *idata, /* (i) original data buffer */ |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 36 | size_t idatal, /* (i) dimension of idata */ |
| 37 | size_t centerStartPos, /* (i) beginning center segment */ |
| 38 | size_t estSegPos, /* (i) estimated beginning other segment (Q-2) */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 39 | int16_t *surround, /* (i/o) The contribution from this sequence |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | summed with earlier contributions */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 41 | int16_t gain /* (i) Gain to use for this sequence */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | ){ |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 43 | size_t estSegPosRounded, searchSegStartPos, searchSegEndPos, corrdim; |
| 44 | size_t tloc, tloc2, i; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 46 | int32_t maxtemp, scalefact; |
| 47 | int16_t *filtStatePtr, *polyPtr; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | /* Stack based */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 49 | int16_t filt[7]; |
| 50 | int32_t corrVecUps[ENH_CORRDIM*ENH_UPS0]; |
| 51 | int32_t corrVecTemp[ENH_CORRDIM]; |
| 52 | int16_t vect[ENH_VECTL]; |
| 53 | int16_t corrVec[ENH_CORRDIM]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | /* defining array bounds */ |
| 56 | |
bjornv@webrtc.org | f71785c | 2014-10-08 15:36:30 +0000 | [diff] [blame] | 57 | estSegPosRounded = (estSegPos - 2) >> 2; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 59 | searchSegStartPos = |
| 60 | (estSegPosRounded < ENH_SLOP) ? 0 : (estSegPosRounded - ENH_SLOP); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 62 | searchSegEndPos = estSegPosRounded + ENH_SLOP; |
| 63 | if ((searchSegEndPos + ENH_BLOCKL) >= idatal) { |
| 64 | searchSegEndPos = idatal - ENH_BLOCKL - 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 65 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 67 | corrdim = searchSegEndPos + 1 - searchSegStartPos; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | |
| 69 | /* compute upsampled correlation and find |
| 70 | location of max */ |
| 71 | |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 72 | WebRtcIlbcfix_MyCorr(corrVecTemp, idata + searchSegStartPos, |
| 73 | corrdim + ENH_BLOCKL - 1, idata + centerStartPos, |
| 74 | ENH_BLOCKL); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | |
| 76 | /* Calculate the rescaling factor for the correlation in order to |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 77 | put the correlation in a int16_t vector instead */ |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 78 | maxtemp = WebRtcSpl_MaxAbsValueW32(corrVecTemp, corrdim); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 80 | scalefact = WebRtcSpl_GetSizeInBits(maxtemp) - 15; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 82 | if (scalefact > 0) { |
| 83 | for (i = 0; i < corrdim; i++) { |
bjornv@webrtc.org | 78ea06d | 2014-10-21 07:17:24 +0000 | [diff] [blame] | 84 | corrVec[i] = (int16_t)(corrVecTemp[i] >> scalefact); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | } |
| 86 | } else { |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 87 | for (i = 0; i < corrdim; i++) { |
| 88 | corrVec[i] = (int16_t)corrVecTemp[i]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | /* In order to guarantee that all values are initialized */ |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 92 | for (i = corrdim; i < ENH_CORRDIM; i++) { |
| 93 | corrVec[i] = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* Upsample the correlation */ |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 97 | WebRtcIlbcfix_EnhUpsample(corrVecUps, corrVec); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | |
| 99 | /* Find maximum */ |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 100 | tloc = WebRtcSpl_MaxIndexW32(corrVecUps, ENH_UPS0 * corrdim); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | |
| 102 | /* make vector can be upsampled without ever running outside |
| 103 | bounds */ |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 104 | *updStartPos = searchSegStartPos * 4 + tloc + 4; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | |
bjornv@webrtc.org | f71785c | 2014-10-08 15:36:30 +0000 | [diff] [blame] | 106 | tloc2 = (tloc + 3) >> 2; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 108 | /* initialize the vector to be filtered, stuff with zeros |
| 109 | when data is outside idata buffer */ |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 110 | if (ENH_FL0 > (searchSegStartPos + tloc2)) { |
| 111 | const size_t st = ENH_FL0 - searchSegStartPos - tloc2; |
| 112 | WebRtcSpl_MemSetW16(vect, 0, st); |
| 113 | WEBRTC_SPL_MEMCPY_W16(&vect[st], idata, ENH_VECTL - st); |
| 114 | } else { |
| 115 | const size_t st = searchSegStartPos + tloc2 - ENH_FL0; |
| 116 | if ((st + ENH_VECTL) > idatal) { |
| 117 | const size_t en = st + ENH_VECTL - idatal; |
| 118 | WEBRTC_SPL_MEMCPY_W16(vect, &idata[st], ENH_VECTL - en); |
| 119 | WebRtcSpl_MemSetW16(&vect[ENH_VECTL - en], 0, en); |
| 120 | } else { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | WEBRTC_SPL_MEMCPY_W16(vect, &idata[st], ENH_VECTL); |
| 122 | } |
| 123 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | |
| 125 | /* compute the segment (this is actually a convolution) */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 126 | filtStatePtr = filt + 6; |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 127 | polyPtr = (int16_t*)WebRtcIlbcfix_kEnhPolyPhaser[tloc2 * ENH_UPS0 - tloc]; |
| 128 | for (i = 0; i < 7; i++) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | *filtStatePtr-- = *polyPtr++; |
| 130 | } |
| 131 | |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 132 | WebRtcSpl_FilterMAFastQ12(&vect[6], vect, filt, ENH_FLO_MULT2_PLUS1, |
| 133 | ENH_BLOCKL); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 134 | |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 135 | /* Add the contribution from this vector (scaled with gain) to the total |
| 136 | surround vector */ |
| 137 | WebRtcSpl_AddAffineVectorToVector(surround, vect, gain, 32768, 16, |
| 138 | ENH_BLOCKL); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | |
| 140 | return; |
| 141 | } |