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 aptxhdEncode. This function allows clients
19  *  to invoke aptX HD 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 24-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 HD encode on 4 new PCM samples */
aptxhdEncode(int32_t pcm[4],Qmf_storage * Qmf_St,Encoder_data * EncoderDataPt)38 XBT_INLINE_ void aptxhdEncode(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   predVals[0] = EncoderDataPt->m_SubbandData[0].m_predData.m_predVal;
46   qCodes[0] = EncoderDataPt->m_qdata[0].qCode;
47 
48   predVals[1] = EncoderDataPt->m_SubbandData[1].m_predData.m_predVal;
49   qCodes[1] = EncoderDataPt->m_qdata[1].qCode;
50 
51   predVals[2] = EncoderDataPt->m_SubbandData[2].m_predData.m_predVal;
52   qCodes[2] = EncoderDataPt->m_qdata[2].qCode;
53 
54   predVals[3] = EncoderDataPt->m_SubbandData[3].m_predData.m_predVal;
55   qCodes[3] = EncoderDataPt->m_qdata[3].qCode;
56 
57   /* Update codeword history, then generate new dither values. */
58   EncoderDataPt->m_codewordHistory =
59       xbtEncupdateCodewordHistory(qCodes, EncoderDataPt->m_codewordHistory);
60   EncoderDataPt->m_dithSyncRandBit = xbtEncgenerateDither(
61       EncoderDataPt->m_codewordHistory, EncoderDataPt->m_ditherOutputs);
62 
63   /* Run the analysis QMF */
64   QmfAnalysisFilter(pcm, Qmf_St, predVals, aqmfOutputs);
65 
66   /* Run the quantiser for each subband */
67   quantiseDifference_HDLL(aqmfOutputs[0], EncoderDataPt->m_ditherOutputs[0],
68                           EncoderDataPt->m_SubbandData[0].m_iqdata.delta,
69                           &EncoderDataPt->m_qdata[0]);
70   quantiseDifference_HDLH(aqmfOutputs[1], EncoderDataPt->m_ditherOutputs[1],
71                           EncoderDataPt->m_SubbandData[1].m_iqdata.delta,
72                           &EncoderDataPt->m_qdata[1]);
73   quantiseDifference_HDHL(aqmfOutputs[2], EncoderDataPt->m_ditherOutputs[2],
74                           EncoderDataPt->m_SubbandData[2].m_iqdata.delta,
75                           &EncoderDataPt->m_qdata[2]);
76   quantiseDifference_HDHH(aqmfOutputs[3], EncoderDataPt->m_ditherOutputs[3],
77                           EncoderDataPt->m_SubbandData[3].m_iqdata.delta,
78                           &EncoderDataPt->m_qdata[3]);
79 }
80 
aptxhdPostEncode(Encoder_data * EncoderDataPt)81 XBT_INLINE_ void aptxhdPostEncode(Encoder_data* EncoderDataPt) {
82   /* Run the remaining subband processing for each subband */
83   /* Manual inlining on the 4 subband */
84   processSubband_HDLL(EncoderDataPt->m_qdata[0].qCode,
85                       EncoderDataPt->m_ditherOutputs[0],
86                       &EncoderDataPt->m_SubbandData[0],
87                       &EncoderDataPt->m_SubbandData[0].m_iqdata);
88 
89   processSubband_HD(EncoderDataPt->m_qdata[1].qCode,
90                     EncoderDataPt->m_ditherOutputs[1],
91                     &EncoderDataPt->m_SubbandData[1],
92                     &EncoderDataPt->m_SubbandData[1].m_iqdata);
93 
94   processSubband_HDHL(EncoderDataPt->m_qdata[2].qCode,
95                       EncoderDataPt->m_ditherOutputs[2],
96                       &EncoderDataPt->m_SubbandData[2],
97                       &EncoderDataPt->m_SubbandData[2].m_iqdata);
98 
99   processSubband_HD(EncoderDataPt->m_qdata[3].qCode,
100                     EncoderDataPt->m_ditherOutputs[3],
101                     &EncoderDataPt->m_SubbandData[3],
102                     &EncoderDataPt->m_SubbandData[3].m_iqdata);
103 }
104 
105 #ifdef _GCC
106 #pragma GCC visibility pop
107 #endif
108 #endif  // APTXENCODER_H
109