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 #include "aptXHDbtenc.h"
17
18 #include "AptxEncoder.h"
19 #include "AptxParameters.h"
20 #include "AptxTables.h"
21 #include "CodewordPacker.h"
22 #include "SyncInserter.h"
23 #include "swversion.h"
24
25 typedef struct aptxhdbtenc_t {
26 /* m_endian should either be 0 (little endian) or 8 (big endian). */
27 int32_t m_endian;
28
29 /* Autosync inserter & Checker for use with the stereo aptX HD codec. */
30 /* The current phase of the sync word insertion (7 down to 0) */
31 uint32_t m_syncWordPhase;
32
33 /* Stereo channel aptX HD encoder (annotated to produce Kalimba test vectors
34 * for it's I/O. This will process valid PCM from a WAV file). */
35 /* Each Encoder_data structure requires 1592 bytes */
36 Encoder_data m_encoderData[2];
37 Qmf_storage m_qmf_l;
38 Qmf_storage m_qmf_r;
39 } aptxhdbtenc;
40
41 /* Constants */
42 /* Log to linear lookup table used in inverse quantiser*/
43 /* Size of Table: 32*4 = 128 bytes */
44 static const int32_t IQuant_tableLogT[32] = {
45 16384 * 256, 16744 * 256, 17112 * 256, 17488 * 256, 17864 * 256, 18256 * 256, 18656 * 256,
46 19064 * 256, 19480 * 256, 19912 * 256, 20344 * 256, 20792 * 256, 21248 * 256, 21712 * 256,
47 22192 * 256, 22672 * 256, 23168 * 256, 23680 * 256, 24200 * 256, 24728 * 256, 25264 * 256,
48 25824 * 256, 26384 * 256, 26968 * 256, 27552 * 256, 28160 * 256, 28776 * 256, 29408 * 256,
49 30048 * 256, 30704 * 256, 31376 * 256, 32064 * 256};
50
clearmem_HD(void * mem,int32_t sz)51 static void clearmem_HD(void* mem, int32_t sz) {
52 int8_t* m = (int8_t*)mem;
53 int32_t i = 0;
54 for (; i < sz; i++) {
55 *m = 0;
56 m++;
57 }
58 }
59
SizeofAptxhdbtenc()60 APTXHDBTENCEXPORT int SizeofAptxhdbtenc() { return sizeof(aptxhdbtenc); }
61
aptxhdbtenc_version()62 APTXHDBTENCEXPORT const char* aptxhdbtenc_version() { return swversion; }
63
aptxhdbtenc_init(void * _state,short endian)64 APTXHDBTENCEXPORT int aptxhdbtenc_init(void* _state, short endian) {
65 aptxhdbtenc* state = (aptxhdbtenc*)_state;
66
67 clearmem_HD(_state, sizeof(aptxhdbtenc));
68
69 if (state == 0) {
70 return 1;
71 }
72 state->m_syncWordPhase = 7L;
73
74 if (endian == 0) {
75 state->m_endian = 0;
76 } else {
77 state->m_endian = 8;
78 }
79
80 for (int j = 0; j < 2; j++) {
81 Encoder_data* encode_dat = &state->m_encoderData[j];
82 uint32_t i;
83
84 /* Create a quantiser and subband processor for each suband */
85 for (i = LL; i <= HH; i++) {
86 encode_dat->m_codewordHistory = 0L;
87
88 encode_dat->m_qdata[i].thresholdTablePtr = subbandParameters[i].threshTable;
89 encode_dat->m_qdata[i].thresholdTablePtr_sl1 = subbandParameters[i].threshTable_sl1;
90 encode_dat->m_qdata[i].ditherTablePtr = subbandParameters[i].dithTable;
91 encode_dat->m_qdata[i].minusLambdaDTable = subbandParameters[i].minusLambdaDTable;
92 encode_dat->m_qdata[i].codeBits = subbandParameters[i].numBits;
93 encode_dat->m_qdata[i].qCode = 0L;
94 encode_dat->m_qdata[i].altQcode = 0L;
95 encode_dat->m_qdata[i].distPenalty = 0L;
96
97 /* initialisation of inverseQuantiser data */
98 encode_dat->m_SubbandData[i].m_iqdata.thresholdTablePtr = subbandParameters[i].threshTable;
99 encode_dat->m_SubbandData[i].m_iqdata.thresholdTablePtr_sl1 =
100 subbandParameters[i].threshTable_sl1;
101 encode_dat->m_SubbandData[i].m_iqdata.ditherTablePtr_sf1 = subbandParameters[i].dithTable_sh1;
102 encode_dat->m_SubbandData[i].m_iqdata.incrTablePtr = subbandParameters[i].incrTable;
103 encode_dat->m_SubbandData[i].m_iqdata.maxLogDelta = subbandParameters[i].maxLogDelta;
104 encode_dat->m_SubbandData[i].m_iqdata.minLogDelta = subbandParameters[i].minLogDelta;
105 encode_dat->m_SubbandData[i].m_iqdata.delta = 0;
106 encode_dat->m_SubbandData[i].m_iqdata.logDelta = 0;
107 encode_dat->m_SubbandData[i].m_iqdata.invQ = 0;
108 encode_dat->m_SubbandData[i].m_iqdata.iquantTableLogPtr = &IQuant_tableLogT[0];
109
110 // Initializing data for predictor filter
111 encode_dat->m_SubbandData[i].m_predData.m_zeroDelayLine.modulo =
112 subbandParameters[i].numZeros;
113
114 for (int t = 0; t < 48; t++) {
115 encode_dat->m_SubbandData[i].m_predData.m_zeroDelayLine.buffer[t] = 0;
116 }
117
118 encode_dat->m_SubbandData[i].m_predData.m_zeroDelayLine.pointer = 0;
119 /* Initialise the previous zero filter output and predictor output to zero
120 */
121 encode_dat->m_SubbandData[i].m_predData.m_zeroVal = 0L;
122 encode_dat->m_SubbandData[i].m_predData.m_predVal = 0L;
123 encode_dat->m_SubbandData[i].m_predData.m_numZeros = subbandParameters[i].numZeros;
124 /* Initialise the contents of the pole data delay line to zero */
125 encode_dat->m_SubbandData[i].m_predData.m_poleDelayLine[0] = 0L;
126 encode_dat->m_SubbandData[i].m_predData.m_poleDelayLine[1] = 0L;
127
128 for (int k = 0; k < 24; k++) {
129 encode_dat->m_SubbandData[i].m_ZeroCoeffData.m_zeroCoeff[k] = 0;
130 }
131
132 // Initializing data for zerocoeff update function.
133 encode_dat->m_SubbandData[i].m_ZeroCoeffData.m_numZeros = subbandParameters[i].numZeros;
134
135 /* Initializing data for PoleCoeff Update function.
136 * Fill the adaptation delay line with +1 initially */
137 encode_dat->m_SubbandData[i].m_PoleCoeffData.m_poleAdaptDelayLine.s32 = 0x00010001;
138
139 /* Zero the pole coefficients */
140 encode_dat->m_SubbandData[i].m_PoleCoeffData.m_poleCoeff[0] = 0L;
141 encode_dat->m_SubbandData[i].m_PoleCoeffData.m_poleCoeff[1] = 0L;
142 }
143 }
144 return 0;
145 }
146
aptxhdbtenc_encodestereo(void * _state,void * _pcmL,void * _pcmR,void * _buffer)147 APTXHDBTENCEXPORT int aptxhdbtenc_encodestereo(void* _state, void* _pcmL, void* _pcmR,
148 void* _buffer) {
149 aptxhdbtenc* state = (aptxhdbtenc*)_state;
150 int32_t* pcmL = (int32_t*)_pcmL;
151 int32_t* pcmR = (int32_t*)_pcmR;
152 int32_t* buffer = (int32_t*)_buffer;
153
154 // Feed the PCM to the dual aptX HD encoders
155 aptxhdEncode(pcmL, &state->m_qmf_l, &state->m_encoderData[0]);
156 aptxhdEncode(pcmR, &state->m_qmf_r, &state->m_encoderData[1]);
157
158 // Insert the autosync information into the stereo quantised codes
159 xbtEncinsertSync(&state->m_encoderData[0], &state->m_encoderData[1], &state->m_syncWordPhase);
160
161 aptxhdPostEncode(&state->m_encoderData[0]);
162 aptxhdPostEncode(&state->m_encoderData[1]);
163
164 // Pack the (possibly adjusted) codes into a 24-bit codeword per channel
165 buffer[0] = packCodeword(&state->m_encoderData[0]);
166 buffer[1] = packCodeword(&state->m_encoderData[1]);
167
168 return 0;
169 }
170