1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/**
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*------------------------------------------------------------------------------
*
* Subband processing consists of:
* inverse quantisation (defined in a separate file),
* predictor coefficient update (Pole and Zero Coeff update),
* predictor filtering.
*
*----------------------------------------------------------------------------*/
#ifndef SUBBANDFUNCTIONS_H
#define SUBBANDFUNCTIONS_H
#ifdef _GCC
#pragma GCC visibility push(hidden)
#endif
#include "AptxParameters.h"
XBT_INLINE_ void updatePredictorPoleCoefficients(
const int32_t invQ, const int32_t prevZfiltOutput,
PoleCoeff_data* PoleCoeffDataPt) {
int32_t adaptSum;
int32_t sgnP[3];
int32_t newCoeffs[2];
int32_t Bacc;
int32_t acc;
int32_t acc2;
int32_t tmp3_round0;
int16_t tmp2_round0;
int16_t tmp_round0;
/* Various constants in various Q formats */
const int32_t oneQ22 = 4194304L;
const int32_t minusOneQ22 = -4194304L;
const int32_t pointFiveQ21 = 1048576L;
const int32_t minusPointFiveQ21 = -1048576L;
const int32_t pointSevenFiveQ22 = 3145728L;
const int32_t minusPointSevenFiveQ22 = -3145728L;
const int32_t oneMinusTwoPowerMinusFourQ22 = 3932160L;
/* Symbolic indices for the pole coefficient arrays. Here we are using a1
* to represent the first pole filter coefficient and a2 the second. This
* seems to be common ADPCM terminology. */
enum { a1 = 0, a2 = 1 };
/* Symbolic indices for the sgn array (k, k-1 and k-2 respectively) */
enum { k = 0, k_1 = 1, k_2 = 2 };
/* Form the sum of the inverse quantiser and previous zero filter values */
adaptSum = invQ + prevZfiltOutput;
adaptSum = ssat24(adaptSum);
/* Form the sgn of the sum just formed (note +1 and -1 are Q22) */
if (adaptSum < 0L) {
sgnP[k] = minusOneQ22;
sgnP[k_1] = -(((int32_t)PoleCoeffDataPt->m_poleAdaptDelayLine.s16.l) << 22);
sgnP[k_2] = -(((int32_t)PoleCoeffDataPt->m_poleAdaptDelayLine.s16.h) << 22);
PoleCoeffDataPt->m_poleAdaptDelayLine.s16.h =
PoleCoeffDataPt->m_poleAdaptDelayLine.s16.l;
PoleCoeffDataPt->m_poleAdaptDelayLine.s16.l = -1;
} else if (adaptSum == 0L) {
sgnP[k] = 0L;
sgnP[k_1] = 0L;
sgnP[k_2] = 0L;
PoleCoeffDataPt->m_poleAdaptDelayLine.s16.h =
PoleCoeffDataPt->m_poleAdaptDelayLine.s16.l;
PoleCoeffDataPt->m_poleAdaptDelayLine.s16.l = 1;
} else { // adaptSum > 0L
sgnP[k] = oneQ22;
sgnP[k_1] = ((int32_t)PoleCoeffDataPt->m_poleAdaptDelayLine.s16.l) << 22;
sgnP[k_2] = ((int32_t)PoleCoeffDataPt->m_poleAdaptDelayLine.s16.h) << 22;
PoleCoeffDataPt->m_poleAdaptDelayLine.s16.h =
PoleCoeffDataPt->m_poleAdaptDelayLine.s16.l;
PoleCoeffDataPt->m_poleAdaptDelayLine.s16.l = 1;
}
/* Clear the accumulator and form -a1(k) * sgn(p(k))sgn(p(k-1)) in Q21. Clip
* it to +/- 0.5 (Q21) so that we can take f(a1) = 4 * a1. This is a partial
* result for the new a2 */
acc = 0;
acc -= PoleCoeffDataPt->m_poleCoeff[a1] * (sgnP[k_1] >> 22);
tmp3_round0 = acc & 0x3L;
acc += 0x001;
acc >>= 1;
if (tmp3_round0 == 0x001L) {
acc--;
}
newCoeffs[a2] = acc;
if (newCoeffs[a2] < minusPointFiveQ21) {
newCoeffs[a2] = minusPointFiveQ21;
}
if (newCoeffs[a2] > pointFiveQ21) {
newCoeffs[a2] = pointFiveQ21;
}
/* Load the accumulator with sgn(p(k))sgn(p(k-2)) right-shifted by 3. The
* 3-position shift is to multiply it by 0.25 and convert from Q22 to Q21.
*/
Bacc = (sgnP[k_2] >> 3);
/* Add the current a2 update value to the accumulator (Q21) */
Bacc += newCoeffs[a2];
/* Shift the accumulator right by 4 positions.
* Right 7 places to multiply by 2^(-7)
* Left 2 places to scale by 4 (0.25A + B -> A + 4B)
* Left 1 place to convert from Q21 to Q22
*/
Bacc >>= 4;
/* Add a2(k-1) * (1 - 2^(-7)) to the accumulator. Note that the constant is
* expressed as Q23, hence the product is Q22. Get the accumulator value
* back out. */
acc2 = PoleCoeffDataPt->m_poleCoeff[a2] << 8;
acc2 -= PoleCoeffDataPt->m_poleCoeff[a2] << 1;
Bacc = (int32_t)((uint32_t)Bacc << 8);
Bacc += acc2;
tmp2_round0 = (int16_t)Bacc & 0x01FFL;
Bacc += 0x0080L;
Bacc >>= 8;
if (tmp2_round0 == 0x0080L) {
Bacc--;
}
newCoeffs[a2] = Bacc;
/* Clip the new a2(k) value to +/- 0.75 (Q22) */
if (newCoeffs[a2] < minusPointSevenFiveQ22) {
newCoeffs[a2] = minusPointSevenFiveQ22;
}
if (newCoeffs[a2] > pointSevenFiveQ22) {
newCoeffs[a2] = pointSevenFiveQ22;
}
PoleCoeffDataPt->m_poleCoeff[a2] = newCoeffs[a2];
/* Form sgn(p(k))sgn(p(k-1)) * (3 * 2^(-8)). The constant is Q23, hence the
* product is Q22. */
/* Add a1(k-1) * (1 - 2^(-8)) to the accumulator. The constant is Q23, hence
* the product is Q22. Get the value from the accumulator. */
acc2 = PoleCoeffDataPt->m_poleCoeff[a1] << 8;
acc2 -= PoleCoeffDataPt->m_poleCoeff[a1];
acc2 += (sgnP[k_1] << 2);
acc2 -= (sgnP[k_1]);
tmp_round0 = (int16_t)acc2 & 0x01FF;
acc2 += 0x0080;
acc = (acc2 >> 8);
if (tmp_round0 == 0x0080) {
acc--;
}
newCoeffs[a1] = acc;
/* Clip the new value of a1(k) to +/- (1 - 2^4 - a2(k)). The constant 1 -
* 2^4 is expressed in Q22 format (as is a1 and a2) */
if (newCoeffs[a1] < (newCoeffs[a2] - oneMinusTwoPowerMinusFourQ22)) {
newCoeffs[a1] = newCoeffs[a2] - oneMinusTwoPowerMinusFourQ22;
}
if (newCoeffs[a1] > (oneMinusTwoPowerMinusFourQ22 - newCoeffs[a2])) {
newCoeffs[a1] = oneMinusTwoPowerMinusFourQ22 - newCoeffs[a2];
}
PoleCoeffDataPt->m_poleCoeff[a1] = newCoeffs[a1];
}
#ifdef _GCC
#pragma GCC visibility pop
#endif
#endif // SUBBANDFUNCTIONS_H
|