blob: 86df81c460fb5c32a35d6948a274b026ec4960d3 [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
19#include "defines.h"
20#include "constants.h"
21#include "enh_upsample.h"
22#include "my_corr.h"
23
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(
pbos@webrtc.org0946a562013-04-09 00:28:06 +000033 int16_t *updStartPos, /* (o) updated start point (Q-2) */
34 int16_t *idata, /* (i) original data buffer */
35 int16_t idatal, /* (i) dimension of idata */
36 int16_t centerStartPos, /* (i) beginning center segment */
37 int16_t estSegPos, /* (i) estimated beginning other segment (Q-2) */
38 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 Kastingdce40cf2015-08-24 14:52:23 -070042 int16_t estSegPosRounded,searchSegStartPos,searchSegEndPos;
43 size_t corrdim,i;
44 int16_t tloc,tloc2,st,en,fraction;
niklase@google.com470e71d2011-07-07 08:21:25 +000045
pbos@webrtc.org0946a562013-04-09 00:28:06 +000046 int32_t maxtemp, scalefact;
47 int16_t *filtStatePtr, *polyPtr;
niklase@google.com470e71d2011-07-07 08:21:25 +000048 /* Stack based */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000049 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.com470e71d2011-07-07 08:21:25 +000054
niklase@google.com470e71d2011-07-07 08:21:25 +000055 /* defining array bounds */
56
bjornv@webrtc.orgf71785c2014-10-08 15:36:30 +000057 estSegPosRounded = (estSegPos - 2) >> 2;
niklase@google.com470e71d2011-07-07 08:21:25 +000058
59 searchSegStartPos=estSegPosRounded-ENH_SLOP;
60
61 if (searchSegStartPos<0) {
62 searchSegStartPos=0;
63 }
64 searchSegEndPos=estSegPosRounded+ENH_SLOP;
65
66 if(searchSegEndPos+ENH_BLOCKL >= idatal) {
67 searchSegEndPos=idatal-ENH_BLOCKL-1;
68 }
Peter Kastingdce40cf2015-08-24 14:52:23 -070069 corrdim=(size_t)(searchSegEndPos-searchSegStartPos+1);
niklase@google.com470e71d2011-07-07 08:21:25 +000070
71 /* compute upsampled correlation and find
72 location of max */
73
74 WebRtcIlbcfix_MyCorr(corrVecTemp,idata+searchSegStartPos,
Peter Kastingdce40cf2015-08-24 14:52:23 -070075 corrdim+ENH_BLOCKL-1,idata+centerStartPos,ENH_BLOCKL);
niklase@google.com470e71d2011-07-07 08:21:25 +000076
77 /* Calculate the rescaling factor for the correlation in order to
pbos@webrtc.org0946a562013-04-09 00:28:06 +000078 put the correlation in a int16_t vector instead */
Peter Kastingb7e50542015-06-11 12:55:50 -070079 maxtemp=WebRtcSpl_MaxAbsValueW32(corrVecTemp, corrdim);
niklase@google.com470e71d2011-07-07 08:21:25 +000080
81 scalefact=WebRtcSpl_GetSizeInBits(maxtemp)-15;
82
83 if (scalefact>0) {
84 for (i=0;i<corrdim;i++) {
bjornv@webrtc.org78ea06d2014-10-21 07:17:24 +000085 corrVec[i] = (int16_t)(corrVecTemp[i] >> scalefact);
niklase@google.com470e71d2011-07-07 08:21:25 +000086 }
87 } else {
88 for (i=0;i<corrdim;i++) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +000089 corrVec[i]=(int16_t)corrVecTemp[i];
niklase@google.com470e71d2011-07-07 08:21:25 +000090 }
91 }
92 /* In order to guarantee that all values are initialized */
93 for (i=corrdim;i<ENH_CORRDIM;i++) {
94 corrVec[i]=0;
95 }
96
97 /* Upsample the correlation */
98 WebRtcIlbcfix_EnhUpsample(corrVecUps,corrVec);
99
100 /* Find maximum */
Peter Kastingb7e50542015-06-11 12:55:50 -0700101 tloc=WebRtcSpl_MaxIndexW32(corrVecUps, ENH_UPS0 * corrdim);
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
103 /* make vector can be upsampled without ever running outside
104 bounds */
bjornv@webrtc.orgba97ea62015-02-13 09:51:40 +0000105 *updStartPos = (int16_t)(searchSegStartPos * 4) + tloc + 4;
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
bjornv@webrtc.orgf71785c2014-10-08 15:36:30 +0000107 tloc2 = (tloc + 3) >> 2;
niklase@google.com470e71d2011-07-07 08:21:25 +0000108
109 st=searchSegStartPos+tloc2-ENH_FL0;
110
111 /* initialize the vector to be filtered, stuff with zeros
112 when data is outside idata buffer */
113 if(st<0){
Peter Kastingdce40cf2015-08-24 14:52:23 -0700114 WebRtcSpl_MemSetW16(vect, 0, (size_t)(-st));
niklase@google.com470e71d2011-07-07 08:21:25 +0000115 WEBRTC_SPL_MEMCPY_W16(&vect[-st], idata, (ENH_VECTL+st));
116 }
117 else{
118 en=st+ENH_VECTL;
119
120 if(en>idatal){
121 WEBRTC_SPL_MEMCPY_W16(vect, &idata[st],
122 (ENH_VECTL-(en-idatal)));
123 WebRtcSpl_MemSetW16(&vect[ENH_VECTL-(en-idatal)], 0,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700124 (size_t)(en-idatal));
niklase@google.com470e71d2011-07-07 08:21:25 +0000125 }
126 else {
127 WEBRTC_SPL_MEMCPY_W16(vect, &idata[st], ENH_VECTL);
128 }
129 }
130 /* Calculate which of the 4 fractions to use */
bjornv@webrtc.orgba97ea62015-02-13 09:51:40 +0000131 fraction = (int16_t)(tloc2 * ENH_UPS0) - tloc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
133 /* compute the segment (this is actually a convolution) */
134
135 filtStatePtr = filt + 6;
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000136 polyPtr = (int16_t*)WebRtcIlbcfix_kEnhPolyPhaser[fraction];
niklase@google.com470e71d2011-07-07 08:21:25 +0000137 for (i=0;i<7;i++) {
138 *filtStatePtr-- = *polyPtr++;
139 }
140
141 WebRtcSpl_FilterMAFastQ12(
142 &vect[6], vect, filt,
143 ENH_FLO_MULT2_PLUS1, ENH_BLOCKL);
144
145 /* Add the contribution from this vector (scaled with gain) to the total surround vector */
146 WebRtcSpl_AddAffineVectorToVector(
147 surround, vect, gain,
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000148 (int32_t)32768, 16, ENH_BLOCKL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000149
150 return;
151}