• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 HD 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 > 0x7FFFFF) {
49     val = 0x7FFFFF;
50   }
51   if (val < -0x800000) {
52     val = -0x800000;
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   struct s_s32 {
70 #ifdef __BIGENDIAN
71     int32_t h;
72     int32_t l;
73 #else
74     int32_t l;
75     int32_t h;
76 #endif
77   } s32;
78 } reg64_t;
79 
80 typedef union u_reg32 {
81   uint32_t u32;
82   int32_t s32;
83   struct s_u16 {
84 #ifdef __BIGENDIAN
85     uint16_t h;
86     uint16_t l;
87 #else
88     uint16_t l;
89     uint16_t h;
90 #endif
91   } u16;
92   struct s_s16 {
93 #ifdef __BIGENDIAN
94     int16_t h;
95     int16_t l;
96 #else
97     int16_t l;
98     int16_t h;
99 #endif
100   } s16;
101 } reg32_t;
102 
103 /* Each aptX HD enc/dec round consumes/produces 4 PCM samples */
104 static const uint32_t numPcmSamples = 4;
105 
106 /* Symbolic constants for PCM data indices. */
107 enum { FirstPcm = 0, SecondPcm = 1, ThirdPcm = 2, FourthPcm = 3 };
108 
109 /* Number of subbands is fixed at 4 */
110 #define NUMSUBBANDS 4
111 
112 /* Symbolic constants for subband identification. */
113 typedef enum { LL = 0, LH = 1, HL = 2, HH = 3 } bands;
114 
115 /* Structure declaration to bind a set of subband parameters */
116 typedef struct {
117   const int32_t* threshTable;
118   const int32_t* threshTable_sl1;
119   const int32_t* dithTable;
120   const int32_t* dithTable_sh1;
121   const int32_t* minusLambdaDTable;
122   const int32_t* incrTable;
123   int32_t numBits;
124   int32_t maxLogDelta;
125   int32_t minLogDelta;
126   int32_t numZeros;
127 } SubbandParameters;
128 
129 /* Struct required for the polecoeffcalculator function of btaptXHD encoder and
130  * decoder*/
131 /* Size of structure: 16 Bytes */
132 typedef struct {
133   /* delay line for previous sgn values */
134   reg32_t m_poleAdaptDelayLine;
135   /* 2 pole filter coeffs */
136   int32_t m_poleCoeff[2];
137 } PoleCoeff_data;
138 
139 /* Struct required for the zerocoeffcalculator function of btaptXHD encoder and
140  * decoder*/
141 /* Size of structure: 100 Bytes */
142 typedef struct {
143   /* The zero filter length for this subband */
144   int32_t m_numZeros;
145   /* Maximum number of zeros for any subband is 24. */
146   /* 24 zero filter coeffs */
147   int32_t m_zeroCoeff[24];
148 } ZeroCoeff_data;
149 
150 /* Struct required for the prediction filtering function of btaptXHD encoder and
151  * decoder*/
152 /* Size of structure: 200+20=220 Bytes */
153 typedef struct {
154   /* Number of zeros associated with this subband */
155   int32_t m_numZeros;
156   /* Zero data delay line (circular) */
157   circularBuffer m_zeroDelayLine;
158   /* 2-tap pole data delay line */
159   int32_t m_poleDelayLine[2];
160   /* Output from zero filter */
161   int32_t m_zeroVal;
162   /* Output from overall ARMA filter */
163   int32_t m_predVal;
164 } Predictor_data;
165 
166 /* Struct required for the Quantisation function of btaptXHD encoder and
167  * decoder*/
168 /* Size of structure: 24 Bytes */
169 typedef struct {
170   /* Number of bits in the quantised code for this subband */
171   int32_t codeBits;
172   /* Pointer to threshold table */
173   const int32_t* thresholdTablePtr;
174   const int32_t* thresholdTablePtr_sl1;
175 
176   /* Pointer to dither table */
177   const int32_t* ditherTablePtr;
178   /* Pointer to minus Lambda table */
179   const int32_t* minusLambdaDTable;
180   /* Output quantised code */
181   int32_t qCode;
182   /* Alternative quantised code for sync purposes */
183   int32_t altQcode;
184   /* Penalty associated with choosing alternative code */
185   int32_t distPenalty;
186 } Quantiser_data;
187 
188 /* Struct required for the inverse Quantisation function of btaptXHD encoder and
189  * decoder*/
190 /* Size of structure: 32 Bytes */
191 typedef struct {
192   /* Pointer to threshold table */
193   const int32_t* thresholdTablePtr;
194   const int32_t* thresholdTablePtr_sl1;
195   /* Pointer to dither table */
196   const int32_t* ditherTablePtr_sf1;
197 
198   /* Pointer to increment table */
199   const int32_t* incrTablePtr;
200   /* Upper and lower bounds for logDelta */
201   int32_t maxLogDelta;
202   int32_t minLogDelta;
203   /* Delta (quantisation step size */
204   int32_t delta;
205   /* Delta, expressed as a log base 2 */
206   uint16_t logDelta;
207   /* Output dequantised signal */
208   int32_t invQ;
209   /* pointer to IQuant_tableLogT */
210   const int32_t* iquantTableLogPtr;
211 } IQuantiser_data;
212 
213 /* Subband data structure btaptXHD encoder*/
214 /* Size of structure: 116+220+32= 368 Bytes */
215 typedef struct {
216   /* Subband processing consists of inverse quantisation, predictor
217    * coefficient update, and predictor filtering. */
218   ZeroCoeff_data m_ZeroCoeffData;
219   PoleCoeff_data m_PoleCoeffData;
220   /* structure holding the data associated with the predictor */
221   Predictor_data m_predData;
222   /* iqdata holds the data associated with the instance of inverse quantiser */
223   IQuantiser_data m_iqdata;
224 } Subband_data;
225 
226 /* Encoder data structure btaptXHD encoder*/
227 /* Size of structure: 368*4+24+4*24 = 1592 Bytes */
228 typedef struct {
229   /* Subband processing consists of inverse quantisation, predictor
230    * coefficient update, and predictor filtering. */
231   Subband_data m_SubbandData[4];
232   int32_t m_codewordHistory;
233   int32_t m_dithSyncRandBit;
234   int32_t m_ditherOutputs[4];
235   /* structure holding data values for this quantiser */
236   Quantiser_data m_qdata[4];
237 } Encoder_data;
238 
239 /* Subband-specific number of predcitor zero filter coefficients. */
240 static const uint32_t numZeroFilterCoeffs[4] = {24, 12, 6, 12};
241 
242 /* Delta is scaled by 4 positions within the quantiser and inverse quantiser. */
243 static const uint32_t deltaScale = 4;
244 
245 #ifdef _GCC
246 #pragma GCC visibility pop
247 #endif
248 #endif  // APTXPARAMETERS_H
249