blob: 7227ac9d45aed2a4034f0cfa89998067740e754a [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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
Timothy Gu31117832020-12-18 22:25:57 -080019#include "modules/audio_coding/codecs/ilbc/state_search.h"
20
Mirko Bonadei06c2aa92018-02-01 15:11:41 +010021#include "modules/audio_coding/codecs/ilbc/abs_quant.h"
Timothy Gu31117832020-12-18 22:25:57 -080022#include "modules/audio_coding/codecs/ilbc/constants.h"
23#include "modules/audio_coding/codecs/ilbc/defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000024
25/*----------------------------------------------------------------*
26 * encoding of start state
27 *---------------------------------------------------------------*/
28
29void WebRtcIlbcfix_StateSearch(
pbos@webrtc.orgeb544462014-12-17 15:23:29 +000030 IlbcEncoder *iLBCenc_inst,
niklase@google.com470e71d2011-07-07 08:21:25 +000031 /* (i) Encoder instance */
32 iLBC_bits *iLBC_encbits,/* (i/o) Encoded bits (output idxForMax
33 and idxVec, input state_first) */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000034 int16_t *residual, /* (i) target residual vector */
35 int16_t *syntDenum, /* (i) lpc synthesis filter */
36 int16_t *weightDenum /* (i) weighting filter denuminator */
niklase@google.com470e71d2011-07-07 08:21:25 +000037 ) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070038 size_t k, index;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000039 int16_t maxVal;
40 int16_t scale, shift;
41 int32_t maxValsq;
42 int16_t scaleRes;
43 int16_t max;
niklase@google.com470e71d2011-07-07 08:21:25 +000044 int i;
45 /* Stack based */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000046 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;
niklase@google.com470e71d2011-07-07 08:21:25 +000051
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);
Peter Kastingdce40cf2015-08-24 14:52:23 -070069 WebRtcSpl_FilterMAFastQ12(residualLong, sampleMa, numerator,
70 LPC_FILTERORDER + 1,
71 iLBCenc_inst->state_short_len + LPC_FILTERORDER);
niklase@google.com470e71d2011-07-07 08:21:25 +000072 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,
Peter Kastingb7e50542015-06-11 12:55:50 -070076 syntDenum, LPC_FILTERORDER+1, 2 * iLBCenc_inst->state_short_len);
niklase@google.com470e71d2011-07-07 08:21:25 +000077
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
pbos@webrtc.org0946a562013-04-09 00:28:06 +000087 if ((((int32_t)maxVal)<<scaleRes)<23170) {
88 maxValsq=((int32_t)maxVal*maxVal)<<(2+2*scaleRes);
niklase@google.com470e71d2011-07-07 08:21:25 +000089 } else {
pbos@webrtc.org0946a562013-04-09 00:28:06 +000090 maxValsq=(int32_t)WEBRTC_SPL_WORD32_MAX;
niklase@google.com470e71d2011-07-07 08:21:25 +000091 }
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,
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000115 iLBCenc_inst->state_short_len, (int16_t)(shift-scaleRes));
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
117 /* Quantize the values in fout[] */
118 WebRtcIlbcfix_AbsQuant(iLBCenc_inst, iLBC_encbits, sampleAr, weightDenum);
119
120 return;
121}