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_GetSyncSeq.c
16 
17 ******************************************************************/
18 
19 #include "defines.h"
20 #include "constants.h"
21 #include "refiner.h"
22 #include "nearest_neighbor.h"
23 
24 /*----------------------------------------------------------------*
25  * get the pitch-synchronous sample sequence
26  *---------------------------------------------------------------*/
27 
WebRtcIlbcfix_GetSyncSeq(int16_t * idata,size_t idatal,size_t centerStartPos,size_t * period,const size_t * plocs,size_t periodl,size_t hl,int16_t * surround)28 void WebRtcIlbcfix_GetSyncSeq(
29     int16_t *idata,   /* (i) original data */
30     size_t idatal,   /* (i) dimension of data */
31     size_t centerStartPos, /* (i) where current block starts */
32     size_t *period,   /* (i) rough-pitch-period array       (Q-2) */
33     const size_t *plocs, /* (i) where periods of period array are taken (Q-2) */
34     size_t periodl,   /* (i) dimension period array */
35     size_t hl,    /* (i) 2*hl+1 is the number of sequences */
36     int16_t *surround  /* (i/o) The contribution from this sequence
37                                 summed with earlier contributions */
38                               ){
39   size_t i, centerEndPos, q;
40   /* Stack based */
41   size_t lagBlock[2 * ENH_HL + 1];
42   size_t blockStartPos[2 * ENH_HL + 1]; /* The position to search around (Q2) */
43   size_t plocs2[ENH_PLOCSL];
44 
45   centerEndPos = centerStartPos + ENH_BLOCKL - 1;
46 
47   /* present (find predicted lag from this position) */
48 
49   WebRtcIlbcfix_NearestNeighbor(lagBlock + hl,
50                                 plocs,
51                                 2 * (centerStartPos + centerEndPos),
52                                 periodl);
53 
54   blockStartPos[hl] = 4 * centerStartPos;
55 
56   /* past (find predicted position and perform a refined
57      search to find the best sequence) */
58 
59   for (q = hl; q > 0; q--) {
60     size_t qq = q - 1;
61     size_t period_q = period[lagBlock[q]];
62     /* Stop if this sequence would be outside the buffer; that means all
63        further-past sequences would also be outside the buffer. */
64     if (blockStartPos[q] < period_q + (4 * ENH_OVERHANG))
65       break;
66     blockStartPos[qq] = blockStartPos[q] - period_q;
67 
68     size_t value = blockStartPos[qq] + 4 * ENH_BLOCKL_HALF;
69     value = (value > period_q) ? (value - period_q) : 0;
70     WebRtcIlbcfix_NearestNeighbor(lagBlock + qq, plocs, value, periodl);
71 
72     /* Find the best possible sequence in the 4 times upsampled
73         domain around blockStartPos+q */
74     WebRtcIlbcfix_Refiner(blockStartPos + qq, idata, idatal, centerStartPos,
75                           blockStartPos[qq], surround,
76                           WebRtcIlbcfix_kEnhWt[qq]);
77   }
78 
79   /* future (find predicted position and perform a refined
80      search to find the best sequence) */
81 
82   for (i = 0; i < periodl; i++) {
83     plocs2[i] = plocs[i] - period[i];
84   }
85 
86   for (q = hl + 1; q <= (2 * hl); q++) {
87 
88     WebRtcIlbcfix_NearestNeighbor(
89         lagBlock + q,
90         plocs2,
91         blockStartPos[q - 1] + 4 * ENH_BLOCKL_HALF,
92         periodl);
93 
94     blockStartPos[q]=blockStartPos[q-1]+period[lagBlock[q]];
95 
96     if (blockStartPos[q] + 4 * (ENH_BLOCKL + ENH_OVERHANG) < 4 * idatal) {
97 
98       /* Find the best possible sequence in the 4 times upsampled
99          domain around blockStartPos+q */
100       WebRtcIlbcfix_Refiner(blockStartPos + q, idata, idatal, centerStartPos,
101                             blockStartPos[q], surround,
102                             WebRtcIlbcfix_kEnhWt[2 * hl - q]);
103 
104     } else {
105       /* Don't add anything since this sequence would
106          be outside the buffer */
107     }
108   }
109 }
110