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_CbMemEnergyAugmentation.c
16 
17 ******************************************************************/
18 
19 #include "modules/audio_coding/codecs/ilbc/defines.h"
20 #include "modules/audio_coding/codecs/ilbc/constants.h"
21 
WebRtcIlbcfix_CbMemEnergyAugmentation(int16_t * interpSamples,int16_t * CBmem,int scale,size_t base_size,int16_t * energyW16,int16_t * energyShifts)22 void WebRtcIlbcfix_CbMemEnergyAugmentation(
23     int16_t *interpSamples, /* (i) The interpolated samples */
24     int16_t *CBmem,   /* (i) The CB memory */
25     int scale,   /* (i) The scaling of all energy values */
26     size_t base_size,  /* (i) Index to where energy values should be stored */
27     int16_t *energyW16,  /* (o) Energy in the CB vectors */
28     int16_t *energyShifts /* (o) Shift value of the energy */
29                                            ){
30   int32_t energy, tmp32;
31   int16_t *ppe, *pp, *interpSamplesPtr;
32   int16_t *CBmemPtr;
33   size_t lagcount;
34   int16_t *enPtr=&energyW16[base_size-20];
35   int16_t *enShPtr=&energyShifts[base_size-20];
36   int32_t nrjRecursive;
37 
38   CBmemPtr = CBmem+147;
39   interpSamplesPtr = interpSamples;
40 
41   /* Compute the energy for the first (low-5) noninterpolated samples */
42   nrjRecursive = WebRtcSpl_DotProductWithScale( CBmemPtr-19, CBmemPtr-19, 15, scale);
43   ppe = CBmemPtr - 20;
44 
45   for (lagcount=20; lagcount<=39; lagcount++) {
46 
47     /* Update the energy recursively to save complexity */
48     nrjRecursive += (*ppe * *ppe) >> scale;
49     ppe--;
50     energy = nrjRecursive;
51 
52     /* interpolation */
53     energy += WebRtcSpl_DotProductWithScale(interpSamplesPtr, interpSamplesPtr, 4, scale);
54     interpSamplesPtr += 4;
55 
56     /* Compute energy for the remaining samples */
57     pp = CBmemPtr - lagcount;
58     energy += WebRtcSpl_DotProductWithScale(pp, pp, SUBL-lagcount, scale);
59 
60     /* Normalize the energy and store the number of shifts */
61     (*enShPtr) = (int16_t)WebRtcSpl_NormW32(energy);
62     tmp32 = energy << *enShPtr;
63     *enPtr = (int16_t)(tmp32 >> 16);
64     enShPtr++;
65     enPtr++;
66   }
67 }
68