1 /**
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 /*------------------------------------------------------------------------------
17  *
18  *  General shared aptX parameters.
19  *
20  *----------------------------------------------------------------------------*/
21 
22 #ifndef APTXPARAMETERS_H
23 #define APTXPARAMETERS_H
24 #ifdef _GCC
25 #pragma GCC visibility push(hidden)
26 #endif
27 
28 #include <stdint.h>
29 
30 #include "CBStruct.h"
31 
32 #if defined _MSC_VER
33 #define XBT_INLINE_ inline
34 #define _STDQMFOUTERCOEFF 1
35 #elif defined __clang__
36 #define XBT_INLINE_ static inline
37 #define _STDQMFOUTERCOEFF 1
38 #elif defined __GNUC__
39 #define XBT_INLINE_ inline
40 #define _STDQMFOUTERCOEFF 1
41 #else
42 #define XBT_INLINE_ static
43 #define _STDQMFOUTERCOEFF 1
44 #endif
45 
46 /* Signed saturate to a 24bit value */
ssat24(int32_t val)47 XBT_INLINE_ int32_t ssat24(int32_t val) {
48   if (val > 8388607) {
49     val = 8388607;
50   }
51   if (val < -8388608) {
52     val = -8388608;
53   }
54   return val;
55 }
56 
57 typedef union u_reg64 {
58   uint64_t u64;
59   int64_t s64;
60   struct s_u32 {
61 #ifdef __BIGENDIAN
62     uint32_t h;
63     uint32_t l;
64 #else
65     uint32_t l;
66     uint32_t h;
67 #endif
68   } u32;
69 
70   struct s_s32 {
71 #ifdef __BIGENDIAN
72     int32_t h;
73     int32_t l;
74 #else
75     int32_t l;
76     int32_t h;
77 #endif
78   } s32;
79 } reg64_t;
80 
81 typedef union u_reg32 {
82   uint32_t u32;
83   int32_t s32;
84 
85   struct s_u16 {
86 #ifdef __BIGENDIAN
87     uint16_t h;
88     uint16_t l;
89 #else
90     uint16_t l;
91     uint16_t h;
92 #endif
93   } u16;
94   struct s_s16 {
95 #ifdef __BIGENDIAN
96     int16_t h;
97     int16_t l;
98 #else
99     int16_t l;
100     int16_t h;
101 #endif
102   } s16;
103 } reg32_t;
104 
105 /* Each aptX enc/dec round consumes/produces 4 PCM samples */
106 static const uint32_t numPcmSamples = 4;
107 
108 /* Symbolic constants for PCM data indices. */
109 enum { FirstPcm = 0, SecondPcm = 1, ThirdPcm = 2, FourthPcm = 3 };
110 
111 /* Symbolic constants for sync modes. */
112 enum { stereo = 0, dualmono = 1, no_sync = 2 };
113 
114 /* Number of subbands is fixed at 4 */
115 #define NUMSUBBANDS 4
116 
117 /* Symbolic constants for subband identification. */
118 typedef enum { LL = 0, LH = 1, HL = 2, HH = 3 } bands;
119 
120 /* Structure declaration to bind a set of subband parameters */
121 typedef struct {
122   const int32_t* threshTable;
123   const int32_t* threshTable_sl1;
124   const int32_t* dithTable;
125   const int32_t* dithTable_sh1;
126   const int32_t* minusLambdaDTable;
127   const int32_t* incrTable;
128   int32_t numBits;
129   int32_t maxLogDelta;
130   int32_t minLogDelta;
131   int32_t numZeros;
132 } SubbandParameters;
133 
134 /* Struct required for the polecoeffcalculator function of bt-aptX encoder and
135  * decoder*/
136 /* Size of structure: 16 Bytes */
137 typedef struct {
138   /* 2-tap delay line for previous sgn values */
139   reg32_t m_poleAdaptDelayLine;
140   /* 2 pole filter coeffs */
141   int32_t m_poleCoeff[2];
142 } PoleCoeff_data;
143 
144 /* Struct required for the zerocoeffcalculator function of bt-aptX encoder and
145  * decoder*/
146 /* Size of structure: 100 Bytes */
147 typedef struct {
148   /* The zero filter length for this subband */
149   int32_t m_numZeros;
150   /* Maximum number of zeros for any subband is 24. */
151   /* 24 zero filter coeffs */
152   int32_t m_zeroCoeff[24];
153 } ZeroCoeff_data;
154 
155 /* Struct required for the prediction filtering function of bt-aptX encoder and
156  * decoder*/
157 /* Size of structure: 200+20=220 Bytes */
158 typedef struct {
159   /* Number of zeros associated with this subband */
160   int32_t m_numZeros;
161   /* Zero data delay line (circular) */
162   circularBuffer m_zeroDelayLine;
163   /* 2-tap pole data delay line */
164   int32_t m_poleDelayLine[2];
165   /* Output from zero filter */
166   int32_t m_zeroVal;
167   /* Output from overall ARMA filter */
168   int32_t m_predVal;
169 } Predictor_data;
170 
171 /* Struct required for the Quantisation function of bt-aptX encoder and
172  * decoder*/
173 /* Size of structure: 24 Bytes */
174 typedef struct {
175   /* Number of bits in the quantised code for this subband */
176   int32_t codeBits;
177   /* Pointer to threshold table */
178   const int32_t* thresholdTablePtr;
179   const int32_t* thresholdTablePtr_sl1;
180   /* Pointer to dither table */
181   const int32_t* ditherTablePtr;
182   /* Pointer to minus Lambda table */
183   const int32_t* minusLambdaDTable;
184   /* Output quantised code */
185   int32_t qCode;
186   /* Alternative quantised code for sync purposes */
187   int32_t altQcode;
188   /* Penalty associated with choosing alternative code */
189   int32_t distPenalty;
190 } Quantiser_data;
191 
192 /* Struct required for the inverse Quantisation function of bt-aptX encoder and
193  * decoder*/
194 /* Size of structure: 32 Bytes */
195 typedef struct {
196   /* Pointer to threshold table */
197   const int32_t* thresholdTablePtr;
198   const int32_t* thresholdTablePtr_sl1;
199   /* Pointer to dither table */
200   const int32_t* ditherTablePtr_sf1;
201   /* Pointer to increment table */
202   const int32_t* incrTablePtr;
203   /* Upper and lower bounds for logDelta */
204   int32_t maxLogDelta;
205   int32_t minLogDelta;
206   /* Delta (quantisation step size */
207   int32_t delta;
208   /* Delta, expressed as a log base 2 */
209   uint16_t logDelta;
210   /* Output dequantised signal */
211   int32_t invQ;
212   /* pointer to IQuant_tableLogT */
213   const int32_t* iquantTableLogPtr;
214 } IQuantiser_data;
215 
216 /* Subband data structure bt-aptX encoder*/
217 /* Size of structure: 116+220+32= 368 Bytes */
218 typedef struct {
219   /* Subband processing consists of inverse quantisation, predictor
220    * coefficient update, and predictor filtering. */
221   ZeroCoeff_data m_ZeroCoeffData;
222   PoleCoeff_data m_PoleCoeffData;
223   /* structure holding the data associated with the predictor */
224   Predictor_data m_predData;
225   /* iqdata holds the data associated with the instance of inverse quantiser */
226   IQuantiser_data m_iqdata;
227 } Subband_data;
228 
229 /* Encoder data structure bt-aptX encoder*/
230 /* Size of structure: 368*4+24+4*24 = 1592 Bytes */
231 typedef struct {
232   /* Subband processing consists of inverse quantisation, predictor
233    * coefficient update, and predictor filtering. */
234   Subband_data m_SubbandData[4];
235   int32_t m_codewordHistory;
236   int32_t m_dithSyncRandBit;
237   int32_t m_ditherOutputs[4];
238   /* structure holding data values for this quantiser */
239   Quantiser_data m_qdata[4];
240 } Encoder_data;
241 
242 /* Number of predictor pole filter coefficients is fixed at 2 for all subbands
243  */
244 static const uint32_t numPoleFilterCoeffs = 2;
245 
246 /* Subband-specific number of predictor zero filter coefficients. */
247 static const uint32_t numZeroFilterCoeffs[4] = {24, 12, 6, 12};
248 
249 /* Delta is scaled by 4 positions within the quantiser and inverse quantiser. */
250 static const uint32_t deltaScale = 4;
251 
252 #ifdef _GCC
253 #pragma GCC visibility pop
254 #endif
255 #endif  // APTXPARAMETERS_H
256