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  *  This file includes the coefficient tables or the two convolution function
19  *  It also includes the definition of Qmf_storage and the prototype of all
20  *  necessary functions required to implement the QMF filtering.
21  *
22  *----------------------------------------------------------------------------*/
23 
24 #ifndef QMF_H
25 #define QMF_H
26 
27 #include "AptxParameters.h"
28 
29 typedef struct {
30   int16_t QmfL_buf[32];
31   int16_t QmfH_buf[32];
32   int32_t QmfLH_buf[32];
33   int32_t QmfHL_buf[32];
34   int32_t QmfLL_buf[32];
35   int32_t QmfHH_buf[32];
36   int32_t QmfI_pt;
37   int32_t QmfO_pt;
38 } Qmf_storage;
39 
40 /* Outer QMF filter for Enhanced aptX is a symmetrical 32-tap filter (16
41  * different coefficients). The table in defined in QmfConv.c */
42 #ifndef _STDQMFOUTERCOEFF
43 static const int32_t Qmf_outerCoeffs[12] = {
44     /* (C(1/30)C(3/28)), C(5/26), C(7/24) */
45     0xFE6302DA,
46     0xFFFFDA75,
47     0x0000AA6A,
48     /*  C(9/22), C(11/20), C(13/18), C(15/16) */
49     0xFFFE273E,
50     0x00041E95,
51     0xFFF710B5,
52     0x002AC12E,
53     /*  C(17/14), C(19/12), (C(21/10)C(23/8)) */
54     0x000AA328,
55     0xFFFD8D1F,
56     0x211E6BDB,
57     /* (C(25/6)C(27/4)), (C(29/2)C(31/0)) */
58     0x0DB7D8C5,
59     0xFC7F02B0,
60 };
61 #else
62 static const int32_t Qmf_outerCoeffs[16] = {
63     730,    -413,    -9611, 43626, -121026, 269973, -585547, 2801966,
64     697128, -160481, 27611, 8478,  -10043,  3511,   688,     -897,
65 };
66 #endif
67 
68 /* Each inner QMF filter for Enhanced aptX is a symmetrical 32-tap filter (16
69  * different coefficients) */
70 static const int32_t Qmf_innerCoeffs[16] = {
71     1033,   -584,    -13592, 61697, -171156, 381799, -828088, 3962579,
72     985888, -226954, 39048,  11990, -14203,  4966,   973,     -1268,
73 };
74 
75 void AsmQmfConvI(const int32_t* p1dl_buffPtr, const int32_t* p2dl_buffPtr,
76                  const int32_t* coeffPtr, int32_t* filterOutputs);
77 void AsmQmfConvO(const int16_t* p1dl_buffPtr, const int16_t* p2dl_buffPtr,
78                  const int32_t* coeffPtr, int32_t* convSumDiff);
79 
QmfAnalysisFilter(const int32_t pcm[4],Qmf_storage * Qmf_St,const int32_t predVals[4],int32_t * aqmfOutputs)80 XBT_INLINE_ void QmfAnalysisFilter(const int32_t pcm[4], Qmf_storage* Qmf_St,
81                                    const int32_t predVals[4],
82                                    int32_t* aqmfOutputs) {
83   int32_t convSumDiff[4];
84   int32_t filterOutputs[4];
85 
86   int32_t lc_QmfO_pt = (Qmf_St->QmfO_pt);
87   int32_t lc_QmfI_pt = (Qmf_St->QmfI_pt);
88 
89   /* Symbolic constants to represent the first and second set out outer filter
90    * outputs. */
91   enum { FirstOuterOutputs = 0, SecondOuterOutputs = 1 };
92 
93   /* Load outer filter phase1 and phase2 delay lines with the first 2 PCM
94    * samples. Convolve the filter and get the 2 convolution results. */
95   Qmf_St->QmfL_buf[lc_QmfO_pt + 16] = (int16_t)pcm[FirstPcm];
96   Qmf_St->QmfL_buf[lc_QmfO_pt] = (int16_t)pcm[FirstPcm];
97   Qmf_St->QmfH_buf[lc_QmfO_pt + 16] = (int16_t)pcm[SecondPcm];
98   Qmf_St->QmfH_buf[lc_QmfO_pt++] = (int16_t)pcm[SecondPcm];
99   lc_QmfO_pt &= 0xF;
100 
101   AsmQmfConvO(&Qmf_St->QmfL_buf[lc_QmfO_pt + 15], &Qmf_St->QmfH_buf[lc_QmfO_pt],
102               Qmf_outerCoeffs, &convSumDiff[0]);
103 
104   /* Load outer filter phase1 and phase2 delay lines with the second 2 PCM
105    * samples. Convolve the filter and get the 2 convolution results. */
106   Qmf_St->QmfL_buf[lc_QmfO_pt + 16] = (int16_t)pcm[ThirdPcm];
107   Qmf_St->QmfL_buf[lc_QmfO_pt] = (int16_t)pcm[ThirdPcm];
108   Qmf_St->QmfH_buf[lc_QmfO_pt + 16] = (int16_t)pcm[FourthPcm];
109   Qmf_St->QmfH_buf[lc_QmfO_pt++] = (int16_t)pcm[FourthPcm];
110   lc_QmfO_pt &= 0xF;
111 
112   AsmQmfConvO(&Qmf_St->QmfL_buf[lc_QmfO_pt + 15], &Qmf_St->QmfH_buf[lc_QmfO_pt],
113               Qmf_outerCoeffs, &convSumDiff[1]);
114 
115   /* Load the first inner filter phase1 and phase2 delay lines with the 2
116    * convolution sum (low-pass) outer filter outputs. Convolve the filter and
117    * get the 2 convolution results. The first 2 analysis filter outputs are
118    * the sum and difference values for the first inner filter convolutions. */
119   Qmf_St->QmfLL_buf[lc_QmfI_pt + 16] = convSumDiff[0];
120   Qmf_St->QmfLL_buf[lc_QmfI_pt] = convSumDiff[0];
121   Qmf_St->QmfLH_buf[lc_QmfI_pt + 16] = convSumDiff[1];
122   Qmf_St->QmfLH_buf[lc_QmfI_pt] = convSumDiff[1];
123 
124   AsmQmfConvI(&Qmf_St->QmfLL_buf[lc_QmfI_pt + 16],
125               &Qmf_St->QmfLH_buf[lc_QmfI_pt + 1], &Qmf_innerCoeffs[0],
126               &filterOutputs[LL]);
127 
128   /* Load the second inner filter phase1 and phase2 delay lines with the 2
129    * convolution difference (high-pass) outer filter outputs. Convolve the
130    * filter and get the 2 convolution results. The second 2 analysis filter
131    * outputs are the sum and difference values for the second inner filter
132    * convolutions. */
133   Qmf_St->QmfHL_buf[lc_QmfI_pt + 16] = convSumDiff[2];
134   Qmf_St->QmfHL_buf[lc_QmfI_pt] = convSumDiff[2];
135   Qmf_St->QmfHH_buf[lc_QmfI_pt + 16] = convSumDiff[3];
136   Qmf_St->QmfHH_buf[lc_QmfI_pt++] = convSumDiff[3];
137   lc_QmfI_pt &= 0xF;
138 
139   AsmQmfConvI(&Qmf_St->QmfHL_buf[lc_QmfI_pt + 15],
140               &Qmf_St->QmfHH_buf[lc_QmfI_pt], &Qmf_innerCoeffs[0],
141               &filterOutputs[HL]);
142 
143   /* Subtracted the previous predicted value from the filter output on a
144    * per-subband basis. Ensure these values are saturated, if necessary.
145    * Manual unrolling */
146   aqmfOutputs[LL] = filterOutputs[LL] - predVals[LL];
147   aqmfOutputs[LL] = ssat24(aqmfOutputs[LL]);
148 
149   aqmfOutputs[LH] = filterOutputs[LH] - predVals[LH];
150   aqmfOutputs[LH] = ssat24(aqmfOutputs[LH]);
151 
152   aqmfOutputs[HL] = filterOutputs[HL] - predVals[HL];
153   aqmfOutputs[HL] = ssat24(aqmfOutputs[HL]);
154 
155   aqmfOutputs[HH] = filterOutputs[HH] - predVals[HH];
156   aqmfOutputs[HH] = ssat24(aqmfOutputs[HH]);
157 
158   (Qmf_St->QmfO_pt) = lc_QmfO_pt;
159   (Qmf_St->QmfI_pt) = lc_QmfI_pt;
160 }
161 
162 #endif  // QMF_H
163