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
28void WebRtcIlbcfix_GetSyncSeq(
29    int16_t *idata,   /* (i) original data */
30    int16_t idatal,   /* (i) dimension of data */
31    int16_t centerStartPos, /* (i) where current block starts */
32    int16_t *period,   /* (i) rough-pitch-period array       (Q-2) */
33    int16_t *plocs,   /* (i) where periods of period array are taken (Q-2) */
34    int16_t periodl,   /* (i) dimension period array */
35    int16_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  int16_t i,centerEndPos,q;
40  /* Stack based */
41  int16_t lagBlock[2*ENH_HL+1];
42  int16_t blockStartPos[2*ENH_HL+1]; /* Defines the position to search around (Q2) */
43  int16_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,plocs,
50                                (int16_t)WEBRTC_SPL_MUL_16_16(2, (centerStartPos+centerEndPos)),
51                                periodl);
52
53  blockStartPos[hl]=(int16_t)WEBRTC_SPL_MUL_16_16(4, centerStartPos);
54
55  /* past (find predicted position and perform a refined
56     search to find the best sequence) */
57
58  for(q=hl-1;q>=0;q--) {
59    blockStartPos[q]=blockStartPos[q+1]-period[lagBlock[q+1]];
60
61    WebRtcIlbcfix_NearestNeighbor(lagBlock+q, plocs,
62                                  (int16_t)(blockStartPos[q] + (int16_t)WEBRTC_SPL_MUL_16_16(4, ENH_BLOCKL_HALF)-period[lagBlock[q+1]]),
63                                  periodl);
64
65    if((blockStartPos[q]-(int16_t)WEBRTC_SPL_MUL_16_16(4, ENH_OVERHANG))>=0) {
66
67      /* Find the best possible sequence in the 4 times upsampled
68         domain around blockStartPos+q */
69      WebRtcIlbcfix_Refiner(blockStartPos+q,idata,idatal,
70                            centerStartPos,blockStartPos[q],surround,WebRtcIlbcfix_kEnhWt[q]);
71
72    } else {
73      /* Don't add anything since this sequence would
74         be outside the buffer */
75    }
76  }
77
78  /* future (find predicted position and perform a refined
79     search to find the best sequence) */
80
81  for(i=0;i<periodl;i++) {
82    plocs2[i]=(plocs[i]-period[i]);
83  }
84
85  for(q=hl+1;q<=WEBRTC_SPL_MUL_16_16(2, hl);q++) {
86
87    WebRtcIlbcfix_NearestNeighbor(lagBlock+q,plocs2,
88                                  (int16_t)(blockStartPos[q-1]+
89                                                  (int16_t)WEBRTC_SPL_MUL_16_16(4, ENH_BLOCKL_HALF)),periodl);
90
91    blockStartPos[q]=blockStartPos[q-1]+period[lagBlock[q]];
92
93    if( (blockStartPos[q]+(int16_t)WEBRTC_SPL_MUL_16_16(4, (ENH_BLOCKL+ENH_OVERHANG)))
94        <
95        (int16_t)WEBRTC_SPL_MUL_16_16(4, idatal)) {
96
97      /* Find the best possible sequence in the 4 times upsampled
98         domain around blockStartPos+q */
99      WebRtcIlbcfix_Refiner(blockStartPos+q, idata, idatal,
100                            centerStartPos,blockStartPos[q],surround,WebRtcIlbcfix_kEnhWt[2*hl-q]);
101
102    }
103    else {
104      /* Don't add anything since this sequence would
105         be outside the buffer */
106    }
107  }
108}
109