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  *  All declarations relevant for aptxEncode. This function allows clients
19  *  to invoke bt-aptX encoding on 4 new PCM samples,
20  *  generating 4 new quantised codes. A separate function allows the
21  *  packing of the 4 codes into a 16-bit word.
22  *
23  *----------------------------------------------------------------------------*/
24 
25 #ifndef APTXENCODER_H
26 #define APTXENCODER_H
27 #ifdef _GCC
28 #pragma GCC visibility push(hidden)
29 #endif
30 
31 #include "AptxParameters.h"
32 #include "DitherGenerator.h"
33 #include "Qmf.h"
34 #include "Quantiser.h"
35 #include "SubbandFunctionsCommon.h"
36 
37 /* Function to carry out a single-channel aptX encode on 4 new PCM samples */
aptxEncode(const int32_t pcm[4],Qmf_storage * Qmf_St,Encoder_data * EncoderDataPt)38 XBT_INLINE_ void aptxEncode(const int32_t pcm[4], Qmf_storage* Qmf_St,
39                             Encoder_data* EncoderDataPt) {
40   int32_t predVals[4];
41   int32_t qCodes[4];
42   int32_t aqmfOutputs[4];
43 
44   /* Extract the previous predicted values and quantised codes into arrays */
45   for (int i = 0; i < 4; i++) {
46     predVals[i] = EncoderDataPt->m_SubbandData[i].m_predData.m_predVal;
47     qCodes[i] = EncoderDataPt->m_qdata[i].qCode;
48   }
49 
50   /* Update codeword history, then generate new dither values. */
51   EncoderDataPt->m_codewordHistory =
52       xbtEncupdateCodewordHistory(qCodes, EncoderDataPt->m_codewordHistory);
53   EncoderDataPt->m_dithSyncRandBit = xbtEncgenerateDither(
54       EncoderDataPt->m_codewordHistory, EncoderDataPt->m_ditherOutputs);
55 
56   /* Run the analysis QMF */
57   QmfAnalysisFilter(pcm, Qmf_St, predVals, aqmfOutputs);
58 
59   /* Run the quantiser for each subband */
60   quantiseDifferenceLL(aqmfOutputs[0], EncoderDataPt->m_ditherOutputs[0],
61                        EncoderDataPt->m_SubbandData[0].m_iqdata.delta,
62                        &EncoderDataPt->m_qdata[0]);
63   quantiseDifferenceLH(aqmfOutputs[1], EncoderDataPt->m_ditherOutputs[1],
64                        EncoderDataPt->m_SubbandData[1].m_iqdata.delta,
65                        &EncoderDataPt->m_qdata[1]);
66   quantiseDifferenceHL(aqmfOutputs[2], EncoderDataPt->m_ditherOutputs[2],
67                        EncoderDataPt->m_SubbandData[2].m_iqdata.delta,
68                        &EncoderDataPt->m_qdata[2]);
69   quantiseDifferenceHH(aqmfOutputs[3], EncoderDataPt->m_ditherOutputs[3],
70                        EncoderDataPt->m_SubbandData[3].m_iqdata.delta,
71                        &EncoderDataPt->m_qdata[3]);
72 }
73 
aptxPostEncode(Encoder_data * EncoderDataPt)74 XBT_INLINE_ void aptxPostEncode(Encoder_data* EncoderDataPt) {
75   /* Run the remaining subband processing for each subband */
76   /* Manual inlining on the 4 subband */
77   processSubbandLL(EncoderDataPt->m_qdata[0].qCode,
78                    EncoderDataPt->m_ditherOutputs[0],
79                    &EncoderDataPt->m_SubbandData[0],
80                    &EncoderDataPt->m_SubbandData[0].m_iqdata);
81 
82   processSubband(EncoderDataPt->m_qdata[1].qCode,
83                  EncoderDataPt->m_ditherOutputs[1],
84                  &EncoderDataPt->m_SubbandData[1],
85                  &EncoderDataPt->m_SubbandData[1].m_iqdata);
86 
87   processSubbandHL(EncoderDataPt->m_qdata[2].qCode,
88                    EncoderDataPt->m_ditherOutputs[2],
89                    &EncoderDataPt->m_SubbandData[2],
90                    &EncoderDataPt->m_SubbandData[2].m_iqdata);
91 
92   processSubband(EncoderDataPt->m_qdata[3].qCode,
93                  EncoderDataPt->m_ditherOutputs[3],
94                  &EncoderDataPt->m_SubbandData[3],
95                  &EncoderDataPt->m_SubbandData[3].m_iqdata);
96 }
97 
98 #ifdef _GCC
99 #pragma GCC visibility pop
100 #endif
101 #endif  // APTXENCODER_H
102