blob: d8a9bfb579996aab7563556ff540098ddea5f3be [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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
Mirko Bonadei06c2aa92018-02-01 15:11:41 +010019#include "modules/audio_coding/codecs/ilbc/defines.h"
20#include "modules/audio_coding/codecs/ilbc/constants.h"
21#include "modules/audio_coding/codecs/ilbc/enh_upsample.h"
22#include "modules/audio_coding/codecs/ilbc/my_corr.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
24/*----------------------------------------------------------------*
25 * find segment starting near idata+estSegPos that has highest
26 * correlation with idata+centerStartPos through
27 * idata+centerStartPos+ENH_BLOCKL-1 segment is found at a
28 * resolution of ENH_UPSO times the original of the original
29 * sampling rate
30 *---------------------------------------------------------------*/
31
32void WebRtcIlbcfix_Refiner(
Peter Kasting1380e262015-08-28 17:31:03 -070033 size_t *updStartPos, /* (o) updated start point (Q-2) */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000034 int16_t *idata, /* (i) original data buffer */
Peter Kasting1380e262015-08-28 17:31:03 -070035 size_t idatal, /* (i) dimension of idata */
36 size_t centerStartPos, /* (i) beginning center segment */
37 size_t estSegPos, /* (i) estimated beginning other segment (Q-2) */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000038 int16_t *surround, /* (i/o) The contribution from this sequence
niklase@google.com470e71d2011-07-07 08:21:25 +000039 summed with earlier contributions */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000040 int16_t gain /* (i) Gain to use for this sequence */
niklase@google.com470e71d2011-07-07 08:21:25 +000041 ){
Peter Kasting1380e262015-08-28 17:31:03 -070042 size_t estSegPosRounded, searchSegStartPos, searchSegEndPos, corrdim;
43 size_t tloc, tloc2, i;
niklase@google.com470e71d2011-07-07 08:21:25 +000044
pbos@webrtc.org0946a562013-04-09 00:28:06 +000045 int32_t maxtemp, scalefact;
46 int16_t *filtStatePtr, *polyPtr;
niklase@google.com470e71d2011-07-07 08:21:25 +000047 /* Stack based */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000048 int16_t filt[7];
49 int32_t corrVecUps[ENH_CORRDIM*ENH_UPS0];
50 int32_t corrVecTemp[ENH_CORRDIM];
51 int16_t vect[ENH_VECTL];
52 int16_t corrVec[ENH_CORRDIM];
niklase@google.com470e71d2011-07-07 08:21:25 +000053
niklase@google.com470e71d2011-07-07 08:21:25 +000054 /* defining array bounds */
55
bjornv@webrtc.orgf71785c2014-10-08 15:36:30 +000056 estSegPosRounded = (estSegPos - 2) >> 2;
niklase@google.com470e71d2011-07-07 08:21:25 +000057
Peter Kasting1380e262015-08-28 17:31:03 -070058 searchSegStartPos =
59 (estSegPosRounded < ENH_SLOP) ? 0 : (estSegPosRounded - ENH_SLOP);
niklase@google.com470e71d2011-07-07 08:21:25 +000060
Peter Kasting1380e262015-08-28 17:31:03 -070061 searchSegEndPos = estSegPosRounded + ENH_SLOP;
62 if ((searchSegEndPos + ENH_BLOCKL) >= idatal) {
63 searchSegEndPos = idatal - ENH_BLOCKL - 1;
niklase@google.com470e71d2011-07-07 08:21:25 +000064 }
niklase@google.com470e71d2011-07-07 08:21:25 +000065
Peter Kasting1380e262015-08-28 17:31:03 -070066 corrdim = searchSegEndPos + 1 - searchSegStartPos;
niklase@google.com470e71d2011-07-07 08:21:25 +000067
68 /* compute upsampled correlation and find
69 location of max */
70
Peter Kasting1380e262015-08-28 17:31:03 -070071 WebRtcIlbcfix_MyCorr(corrVecTemp, idata + searchSegStartPos,
72 corrdim + ENH_BLOCKL - 1, idata + centerStartPos,
73 ENH_BLOCKL);
niklase@google.com470e71d2011-07-07 08:21:25 +000074
75 /* Calculate the rescaling factor for the correlation in order to
pbos@webrtc.org0946a562013-04-09 00:28:06 +000076 put the correlation in a int16_t vector instead */
Peter Kasting1380e262015-08-28 17:31:03 -070077 maxtemp = WebRtcSpl_MaxAbsValueW32(corrVecTemp, corrdim);
niklase@google.com470e71d2011-07-07 08:21:25 +000078
Peter Kasting1380e262015-08-28 17:31:03 -070079 scalefact = WebRtcSpl_GetSizeInBits(maxtemp) - 15;
niklase@google.com470e71d2011-07-07 08:21:25 +000080
Peter Kasting1380e262015-08-28 17:31:03 -070081 if (scalefact > 0) {
82 for (i = 0; i < corrdim; i++) {
bjornv@webrtc.org78ea06d2014-10-21 07:17:24 +000083 corrVec[i] = (int16_t)(corrVecTemp[i] >> scalefact);
niklase@google.com470e71d2011-07-07 08:21:25 +000084 }
85 } else {
Peter Kasting1380e262015-08-28 17:31:03 -070086 for (i = 0; i < corrdim; i++) {
87 corrVec[i] = (int16_t)corrVecTemp[i];
niklase@google.com470e71d2011-07-07 08:21:25 +000088 }
89 }
90 /* In order to guarantee that all values are initialized */
Peter Kasting1380e262015-08-28 17:31:03 -070091 for (i = corrdim; i < ENH_CORRDIM; i++) {
92 corrVec[i] = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000093 }
94
95 /* Upsample the correlation */
Peter Kasting1380e262015-08-28 17:31:03 -070096 WebRtcIlbcfix_EnhUpsample(corrVecUps, corrVec);
niklase@google.com470e71d2011-07-07 08:21:25 +000097
98 /* Find maximum */
Peter Kasting1380e262015-08-28 17:31:03 -070099 tloc = WebRtcSpl_MaxIndexW32(corrVecUps, ENH_UPS0 * corrdim);
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
101 /* make vector can be upsampled without ever running outside
102 bounds */
Peter Kasting1380e262015-08-28 17:31:03 -0700103 *updStartPos = searchSegStartPos * 4 + tloc + 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
bjornv@webrtc.orgf71785c2014-10-08 15:36:30 +0000105 tloc2 = (tloc + 3) >> 2;
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
niklase@google.com470e71d2011-07-07 08:21:25 +0000107 /* initialize the vector to be filtered, stuff with zeros
108 when data is outside idata buffer */
Peter Kasting1380e262015-08-28 17:31:03 -0700109 if (ENH_FL0 > (searchSegStartPos + tloc2)) {
110 const size_t st = ENH_FL0 - searchSegStartPos - tloc2;
111 WebRtcSpl_MemSetW16(vect, 0, st);
112 WEBRTC_SPL_MEMCPY_W16(&vect[st], idata, ENH_VECTL - st);
113 } else {
114 const size_t st = searchSegStartPos + tloc2 - ENH_FL0;
115 if ((st + ENH_VECTL) > idatal) {
116 const size_t en = st + ENH_VECTL - idatal;
117 WEBRTC_SPL_MEMCPY_W16(vect, &idata[st], ENH_VECTL - en);
118 WebRtcSpl_MemSetW16(&vect[ENH_VECTL - en], 0, en);
119 } else {
niklase@google.com470e71d2011-07-07 08:21:25 +0000120 WEBRTC_SPL_MEMCPY_W16(vect, &idata[st], ENH_VECTL);
121 }
122 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
124 /* compute the segment (this is actually a convolution) */
niklase@google.com470e71d2011-07-07 08:21:25 +0000125 filtStatePtr = filt + 6;
Peter Kasting1380e262015-08-28 17:31:03 -0700126 polyPtr = (int16_t*)WebRtcIlbcfix_kEnhPolyPhaser[tloc2 * ENH_UPS0 - tloc];
127 for (i = 0; i < 7; i++) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000128 *filtStatePtr-- = *polyPtr++;
129 }
130
Peter Kasting1380e262015-08-28 17:31:03 -0700131 WebRtcSpl_FilterMAFastQ12(&vect[6], vect, filt, ENH_FLO_MULT2_PLUS1,
132 ENH_BLOCKL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000133
Peter Kasting1380e262015-08-28 17:31:03 -0700134 /* Add the contribution from this vector (scaled with gain) to the total
135 surround vector */
136 WebRtcSpl_AddAffineVectorToVector(surround, vect, gain, 32768, 16,
137 ENH_BLOCKL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
139 return;
140}