xref: /aosp_15_r20/external/webrtc/modules/audio_coding/codecs/ilbc/create_augmented_vec.c (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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_CreateAugmentedVec.c
16 
17 ******************************************************************/
18 
19 #include "modules/audio_coding/codecs/ilbc/create_augmented_vec.h"
20 
21 #include "common_audio/signal_processing/include/signal_processing_library.h"
22 #include "modules/audio_coding/codecs/ilbc/constants.h"
23 #include "modules/audio_coding/codecs/ilbc/defines.h"
24 #include "rtc_base/sanitizer.h"
25 
26 /*----------------------------------------------------------------*
27  *  Recreate a specific codebook vector from the augmented part.
28  *
29  *----------------------------------------------------------------*/
30 
WebRtcIlbcfix_CreateAugmentedVec(size_t index,const int16_t * buffer,int16_t * cbVec)31 void WebRtcIlbcfix_CreateAugmentedVec(
32     size_t index,          /* (i) Index for the augmented vector to be
33                               created */
34     const int16_t* buffer, /* (i) Pointer to the end of the codebook memory
35                               that is used for creation of the augmented
36                               codebook */
37     int16_t* cbVec) {      /* (o) The constructed codebook vector */
38   size_t ilow;
39   const int16_t *ppo, *ppi;
40   int16_t cbVecTmp[4];
41   /* Interpolation starts 4 elements before cbVec+index, but must not start
42      outside `cbVec`; clamping interp_len to stay within `cbVec`.
43    */
44   size_t interp_len = WEBRTC_SPL_MIN(index, 4);
45 
46   rtc_MsanCheckInitialized(buffer - index - interp_len, sizeof(buffer[0]),
47                            index + interp_len);
48 
49   ilow = index - interp_len;
50 
51   /* copy the first noninterpolated part */
52   ppo = buffer-index;
53   WEBRTC_SPL_MEMCPY_W16(cbVec, ppo, index);
54 
55   /* interpolation */
56   ppo = buffer - interp_len;
57   ppi = buffer - index - interp_len;
58 
59   /* perform cbVec[ilow+k] = ((ppi[k]*alphaTbl[k])>>15) +
60                              ((ppo[k]*alphaTbl[interp_len-1-k])>>15);
61      for k = 0..interp_len-1
62   */
63   WebRtcSpl_ElementwiseVectorMult(&cbVec[ilow], ppi, WebRtcIlbcfix_kAlpha,
64                                   interp_len, 15);
65   WebRtcSpl_ReverseOrderMultArrayElements(
66       cbVecTmp, ppo, &WebRtcIlbcfix_kAlpha[interp_len - 1], interp_len, 15);
67   WebRtcSpl_AddVectorsAndShift(&cbVec[ilow], &cbVec[ilow], cbVecTmp, interp_len,
68                                0);
69 
70   /* copy the second noninterpolated part */
71   ppo = buffer - index;
72   /* `tempbuff2` is declared in WebRtcIlbcfix_GetCbVec and is SUBL+5 elements
73      long. `buffer` points one element past the end of that vector, i.e., at
74      tempbuff2+SUBL+5. Since ppo=buffer-index, we cannot read any more than
75      `index` elements from `ppo`.
76 
77      `cbVec` is declared to be SUBL elements long in WebRtcIlbcfix_CbConstruct.
78      Therefore, we can only write SUBL-index elements to cbVec+index.
79 
80      These two conditions limit the number of elements to copy.
81    */
82   WEBRTC_SPL_MEMCPY_W16(cbVec+index, ppo, WEBRTC_SPL_MIN(SUBL-index, index));
83 }
84