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