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_GetCbVec.c
16 
17 ******************************************************************/
18 
19 #include "get_cd_vec.h"
20 
21 #include "defines.h"
22 #include "constants.h"
23 #include "create_augmented_vec.h"
24 
25 /*----------------------------------------------------------------*
26  *  Construct codebook vector for given index.
27  *---------------------------------------------------------------*/
28 
WebRtcIlbcfix_GetCbVec(int16_t * cbvec,int16_t * mem,size_t index,size_t lMem,size_t cbveclen)29 bool WebRtcIlbcfix_GetCbVec(
30     int16_t *cbvec,   /* (o) Constructed codebook vector */
31     int16_t *mem,   /* (i) Codebook buffer */
32     size_t index,   /* (i) Codebook index */
33     size_t lMem,   /* (i) Length of codebook buffer */
34     size_t cbveclen   /* (i) Codebook vector length */
35                             ){
36   size_t k, base_size;
37   size_t lag;
38   /* Stack based */
39   int16_t tempbuff2[SUBL+5];
40 
41   /* Determine size of codebook sections */
42 
43   base_size=lMem-cbveclen+1;
44 
45   if (cbveclen==SUBL) {
46     base_size += cbveclen / 2;
47   }
48 
49   /* No filter -> First codebook section */
50 
51   if (index<lMem-cbveclen+1) {
52 
53     /* first non-interpolated vectors */
54 
55     k=index+cbveclen;
56     /* get vector */
57     WEBRTC_SPL_MEMCPY_W16(cbvec, mem+lMem-k, cbveclen);
58 
59   } else if (index < base_size) {
60 
61     /* Calculate lag */
62 
63     k = (2 * (index - (lMem - cbveclen + 1))) + cbveclen;
64 
65     lag = k / 2;
66 
67     WebRtcIlbcfix_CreateAugmentedVec(lag, mem+lMem, cbvec);
68 
69   }
70 
71   /* Higher codebbok section based on filtering */
72 
73   else {
74 
75     size_t memIndTest;
76 
77     /* first non-interpolated vectors */
78 
79     if (index-base_size<lMem-cbveclen+1) {
80 
81       /* Set up filter memory, stuff zeros outside memory buffer */
82 
83       memIndTest = lMem-(index-base_size+cbveclen);
84 
85       WebRtcSpl_MemSetW16(mem-CB_HALFFILTERLEN, 0, CB_HALFFILTERLEN);
86       WebRtcSpl_MemSetW16(mem+lMem, 0, CB_HALFFILTERLEN);
87 
88       /* do filtering to get the codebook vector */
89 
90       WebRtcSpl_FilterMAFastQ12(
91           &mem[memIndTest+4], cbvec, (int16_t*)WebRtcIlbcfix_kCbFiltersRev,
92           CB_FILTERLEN, cbveclen);
93     }
94 
95     /* interpolated vectors */
96 
97     else {
98       if (cbveclen < SUBL) {
99         // We're going to fill in cbveclen + 5 elements of tempbuff2 in
100         // WebRtcSpl_FilterMAFastQ12, less than the SUBL + 5 elements we'll be
101         // using in WebRtcIlbcfix_CreateAugmentedVec. This error is caused by
102         // bad values in |index| (which come from the encoded stream). Tell the
103         // caller that things went south, and that the decoder state is now
104         // corrupt (because it's half-way through an update that we can't
105         // complete).
106         return false;
107       }
108 
109       /* Stuff zeros outside memory buffer  */
110       memIndTest = lMem-cbveclen-CB_FILTERLEN;
111       WebRtcSpl_MemSetW16(mem+lMem, 0, CB_HALFFILTERLEN);
112 
113       /* do filtering */
114       WebRtcSpl_FilterMAFastQ12(
115           &mem[memIndTest+7], tempbuff2, (int16_t*)WebRtcIlbcfix_kCbFiltersRev,
116           CB_FILTERLEN, cbveclen+5);
117 
118       /* Calculate lag index */
119       lag = (cbveclen<<1)-20+index-base_size-lMem-1;
120 
121       WebRtcIlbcfix_CreateAugmentedVec(lag, tempbuff2+SUBL+5, cbvec);
122     }
123   }
124 
125   return true;  // Success.
126 }
127