xref: /aosp_15_r20/external/webrtc/modules/audio_coding/codecs/ilbc/cb_mem_energy.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_CbMemEnergy.c
16 
17 ******************************************************************/
18 
19 #include "modules/audio_coding/codecs/ilbc/cb_mem_energy.h"
20 
21 #include "modules/audio_coding/codecs/ilbc/cb_mem_energy_calc.h"
22 #include "modules/audio_coding/codecs/ilbc/constants.h"
23 #include "modules/audio_coding/codecs/ilbc/defines.h"
24 
25 /*----------------------------------------------------------------*
26  *  Function WebRtcIlbcfix_CbMemEnergy computes the energy of all
27  * the vectors in the codebook memory that will be used in the
28  * following search for the best match.
29  *----------------------------------------------------------------*/
30 
WebRtcIlbcfix_CbMemEnergy(size_t range,int16_t * CB,int16_t * filteredCB,size_t lMem,size_t lTarget,int16_t * energyW16,int16_t * energyShifts,int scale,size_t base_size)31 void WebRtcIlbcfix_CbMemEnergy(
32     size_t range,
33     int16_t *CB,   /* (i) The CB memory (1:st section) */
34     int16_t *filteredCB,  /* (i) The filtered CB memory (2:nd section) */
35     size_t lMem,   /* (i) Length of the CB memory */
36     size_t lTarget,   /* (i) Length of the target vector */
37     int16_t *energyW16,  /* (o) Energy in the CB vectors */
38     int16_t *energyShifts, /* (o) Shift value of the energy */
39     int scale,   /* (i) The scaling of all energy values */
40     size_t base_size  /* (i) Index to where energy values should be stored */
41                                ) {
42   int16_t *ppi, *ppo, *pp;
43   int32_t energy, tmp32;
44 
45   /* Compute the energy and store it in a vector. Also the
46    * corresponding shift values are stored. The energy values
47    * are reused in all three stages. */
48 
49   /* Calculate the energy in the first block of 'lTarget' sampels. */
50   ppi = CB+lMem-lTarget-1;
51   ppo = CB+lMem-1;
52 
53   pp=CB+lMem-lTarget;
54   energy = WebRtcSpl_DotProductWithScale( pp, pp, lTarget, scale);
55 
56   /* Normalize the energy and store the number of shifts */
57   energyShifts[0] = (int16_t)WebRtcSpl_NormW32(energy);
58   tmp32 = energy << energyShifts[0];
59   energyW16[0] = (int16_t)(tmp32 >> 16);
60 
61   /* Compute the energy of the rest of the cb memory
62    * by step wise adding and subtracting the next
63    * sample and the last sample respectively. */
64   WebRtcIlbcfix_CbMemEnergyCalc(energy, range, ppi, ppo, energyW16, energyShifts, scale, 0);
65 
66   /* Next, precompute the energy values for the filtered cb section */
67   energy=0;
68   pp=filteredCB+lMem-lTarget;
69 
70   energy = WebRtcSpl_DotProductWithScale( pp, pp, lTarget, scale);
71 
72   /* Normalize the energy and store the number of shifts */
73   energyShifts[base_size] = (int16_t)WebRtcSpl_NormW32(energy);
74   tmp32 = energy << energyShifts[base_size];
75   energyW16[base_size] = (int16_t)(tmp32 >> 16);
76 
77   ppi = filteredCB + lMem - 1 - lTarget;
78   ppo = filteredCB + lMem - 1;
79 
80   WebRtcIlbcfix_CbMemEnergyCalc(energy, range, ppi, ppo, energyW16, energyShifts, scale, base_size);
81 }
82