xref: /aosp_15_r20/external/webrtc/modules/audio_coding/codecs/ilbc/state_search.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_StateSearch.c
16 
17 ******************************************************************/
18 
19 #include "modules/audio_coding/codecs/ilbc/state_search.h"
20 
21 #include "modules/audio_coding/codecs/ilbc/abs_quant.h"
22 #include "modules/audio_coding/codecs/ilbc/constants.h"
23 #include "modules/audio_coding/codecs/ilbc/defines.h"
24 
25 /*----------------------------------------------------------------*
26  *  encoding of start state
27  *---------------------------------------------------------------*/
28 
WebRtcIlbcfix_StateSearch(IlbcEncoder * iLBCenc_inst,iLBC_bits * iLBC_encbits,int16_t * residual,int16_t * syntDenum,int16_t * weightDenum)29 void WebRtcIlbcfix_StateSearch(
30     IlbcEncoder *iLBCenc_inst,
31     /* (i) Encoder instance */
32     iLBC_bits *iLBC_encbits,/* (i/o) Encoded bits (output idxForMax
33                                and idxVec, input state_first) */
34     int16_t *residual,   /* (i) target residual vector */
35     int16_t *syntDenum,  /* (i) lpc synthesis filter */
36     int16_t *weightDenum  /* (i) weighting filter denuminator */
37                                ) {
38   size_t k, index;
39   int16_t maxVal;
40   int16_t scale, shift;
41   int32_t maxValsq;
42   int16_t scaleRes;
43   int16_t max;
44   int i;
45   /* Stack based */
46   int16_t numerator[1+LPC_FILTERORDER];
47   int16_t residualLongVec[2*STATE_SHORT_LEN_30MS+LPC_FILTERORDER];
48   int16_t sampleMa[2*STATE_SHORT_LEN_30MS];
49   int16_t *residualLong = &residualLongVec[LPC_FILTERORDER];
50   int16_t *sampleAr = residualLong;
51 
52   /* Scale to maximum 12 bits to avoid saturation in circular convolution filter */
53   max = WebRtcSpl_MaxAbsValueW16(residual, iLBCenc_inst->state_short_len);
54   scaleRes = WebRtcSpl_GetSizeInBits(max)-12;
55   scaleRes = WEBRTC_SPL_MAX(0, scaleRes);
56   /* Set up the filter coefficients for the circular convolution */
57   for (i=0; i<LPC_FILTERORDER+1; i++) {
58     numerator[i] = (syntDenum[LPC_FILTERORDER-i]>>scaleRes);
59   }
60 
61   /* Copy the residual to a temporary buffer that we can filter
62    * and set the remaining samples to zero.
63    */
64   WEBRTC_SPL_MEMCPY_W16(residualLong, residual, iLBCenc_inst->state_short_len);
65   WebRtcSpl_MemSetW16(residualLong + iLBCenc_inst->state_short_len, 0, iLBCenc_inst->state_short_len);
66 
67   /* Run the Zero-Pole filter (Ciurcular convolution) */
68   WebRtcSpl_MemSetW16(residualLongVec, 0, LPC_FILTERORDER);
69   WebRtcSpl_FilterMAFastQ12(residualLong, sampleMa, numerator,
70                             LPC_FILTERORDER + 1,
71                             iLBCenc_inst->state_short_len + LPC_FILTERORDER);
72   WebRtcSpl_MemSetW16(&sampleMa[iLBCenc_inst->state_short_len + LPC_FILTERORDER], 0, iLBCenc_inst->state_short_len - LPC_FILTERORDER);
73 
74   WebRtcSpl_FilterARFastQ12(
75       sampleMa, sampleAr,
76       syntDenum, LPC_FILTERORDER+1, 2 * iLBCenc_inst->state_short_len);
77 
78   for(k=0;k<iLBCenc_inst->state_short_len;k++){
79     sampleAr[k] += sampleAr[k+iLBCenc_inst->state_short_len];
80   }
81 
82   /* Find maximum absolute value in the vector */
83   maxVal=WebRtcSpl_MaxAbsValueW16(sampleAr, iLBCenc_inst->state_short_len);
84 
85   /* Find the best index */
86 
87   if ((((int32_t)maxVal)<<scaleRes)<23170) {
88     maxValsq=((int32_t)maxVal*maxVal)<<(2+2*scaleRes);
89   } else {
90     maxValsq=(int32_t)WEBRTC_SPL_WORD32_MAX;
91   }
92 
93   index=0;
94   for (i=0;i<63;i++) {
95 
96     if (maxValsq>=WebRtcIlbcfix_kChooseFrgQuant[i]) {
97       index=i+1;
98     } else {
99       i=63;
100     }
101   }
102   iLBC_encbits->idxForMax=index;
103 
104   /* Rescale the vector before quantization */
105   scale=WebRtcIlbcfix_kScale[index];
106 
107   if (index<27) { /* scale table is in Q16, fout[] is in Q(-1) and we want the result to be in Q11 */
108     shift=4;
109   } else { /* scale table is in Q21, fout[] is in Q(-1) and we want the result to be in Q11 */
110     shift=9;
111   }
112 
113   /* Set up vectors for AbsQuant and rescale it with the scale factor */
114   WebRtcSpl_ScaleVectorWithSat(sampleAr, sampleAr, scale,
115                               iLBCenc_inst->state_short_len, (int16_t)(shift-scaleRes));
116 
117   /* Quantize the values in fout[] */
118   WebRtcIlbcfix_AbsQuant(iLBCenc_inst, iLBC_encbits, sampleAr, weightDenum);
119 
120   return;
121 }
122