xref: /aosp_15_r20/external/libxaac/encoder/ixheaace_mps_qmf.c (revision 15dc779a375ca8b5125643b829a8aa4b70d7f451)
1 /******************************************************************************
2  *                                                                            *
3  * Copyright (C) 2023 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *****************************************************************************
18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19  */
20 
21 #include <string.h>
22 #include <stdlib.h>
23 #include "ixheaac_type_def.h"
24 #include "ixheaac_error_standards.h"
25 #include "ixheaace_error_codes.h"
26 #include "ixheaace_mps_common_fix.h"
27 #include "ixheaace_mps_defines.h"
28 #include "ixheaace_mps_common_define.h"
29 #include "ixheaace_mps_buf.h"
30 #include "ixheaace_mps_lib.h"
31 #include "ixheaace_mps_main_structure.h"
32 #include "ixheaace_mps_tools_rom.h"
33 #include "ixheaace_mps_dct.h"
34 #include "ixheaace_mps_qmf.h"
35 #include "ixheaac_constants.h"
36 #include "ixheaac_basic_ops32.h"
37 #include "ixheaac_basic_ops40.h"
38 #include "ixheaac_basic_ops.h"
39 
ixheaace_mps_212_qmf_forward_modulation_hq(ixheaace_mps_pstr_qmf_filter_bank pstr_qmf_filter_bank,const FLOAT32 * time_in,FLOAT32 * real_subband,FLOAT32 * imag_subband,WORD8 * ptr_scratch)40 static IA_ERRORCODE ixheaace_mps_212_qmf_forward_modulation_hq(
41     ixheaace_mps_pstr_qmf_filter_bank pstr_qmf_filter_bank, const FLOAT32 *time_in,
42     FLOAT32 *real_subband, FLOAT32 *imag_subband, WORD8 *ptr_scratch) {
43   IA_ERRORCODE error;
44   WORD32 idx;
45   WORD32 qmf_bands = pstr_qmf_filter_bank->no_channels;
46   WORD32 qmf_bands_by_2 = qmf_bands << 1;
47   FLOAT32 intermediate_band;
48   FLOAT32 x0, x1, y0, y1;
49   if (qmf_bands == 64) {
50     x0 = time_in[1];
51     y0 = time_in[0];
52     real_subband[0] = (x0 + y0) / 2;
53     imag_subband[0] = (x0 - y0) / 2;
54     for (idx = 1; idx < qmf_bands; idx++) {
55       x0 = time_in[idx + 1];
56       y0 = time_in[qmf_bands_by_2 - idx];
57       real_subband[idx] = (x0 - y0) / 2;
58       imag_subband[idx] = (x0 + y0) / 2;
59     }
60   } else {
61     for (idx = 0; idx < qmf_bands; idx += 2) {
62       x0 = time_in[idx + 0];
63       x1 = time_in[idx + 1];
64       y0 = time_in[qmf_bands_by_2 - 1 - idx];
65       y1 = time_in[qmf_bands_by_2 - 2 - idx];
66 
67       real_subband[idx + 0] = (x0 - y0) / 2;
68       real_subband[idx + 1] = (x1 - y1) / 2;
69       imag_subband[idx + 0] = (x0 + y0) / 2;
70       imag_subband[idx + 1] = (x1 + y1) / 2;
71     }
72   }
73   error = ixheaace_mps_212_dct_iv(real_subband, qmf_bands, ptr_scratch);
74   if (error != IA_NO_ERROR) {
75     return error;
76   }
77   error = ixheaace_mps_212_dst_iv(imag_subband, qmf_bands, ptr_scratch);
78   if (error != IA_NO_ERROR) {
79     return error;
80   }
81 
82   for (idx = 0; idx < MIN(pstr_qmf_filter_bank->lsb, qmf_bands); idx += 2) {
83     intermediate_band = real_subband[idx];
84     real_subband[idx] = -imag_subband[idx];
85     imag_subband[idx] = intermediate_band;
86 
87     intermediate_band = -real_subband[idx + 1];
88     real_subband[idx + 1] = imag_subband[idx + 1];
89     imag_subband[idx + 1] = intermediate_band;
90   }
91   return IA_NO_ERROR;
92 }
93 
ixheaace_mps_212_qmf_ana_prototype_fir_slot(FLOAT32 * ptr_analysis_buffer,WORD32 qmf_bands,const FLOAT32 * ptr_filter,WORD32 stride,FLOAT32 * ptr_filter_states)94 static VOID ixheaace_mps_212_qmf_ana_prototype_fir_slot(FLOAT32 *ptr_analysis_buffer,
95                                                         WORD32 qmf_bands,
96                                                         const FLOAT32 *ptr_filter, WORD32 stride,
97                                                         FLOAT32 *ptr_filter_states) {
98   const FLOAT32 *p_flt = ptr_filter;
99   WORD32 poly, band;
100   FLOAT32 accumlate;
101 
102   for (band = 0; band < 2 * qmf_bands; band++) {
103     accumlate = 0;
104     p_flt += QMF_NO_POLY * (stride - 1);
105     for (poly = 0; poly < QMF_NO_POLY; poly++) {
106       accumlate += (((*p_flt++) * ptr_filter_states[2 * qmf_bands * poly]));
107     }
108     ptr_analysis_buffer[2 * qmf_bands - 1 - band] = accumlate;
109     ptr_filter_states++;
110   }
111 }
112 
ixheaace_mps_212_qmf_analysis_filtering_slot(ixheaace_mps_pstr_qmf_filter_bank pstr_qmf_filter_bank,FLOAT32 * real_subband,FLOAT32 * imag_subband,const FLOAT32 * time_in,const WORD32 stride,FLOAT32 * p_work_buffer,WORD8 * ptr_scratch)113 IA_ERRORCODE ixheaace_mps_212_qmf_analysis_filtering_slot(
114     ixheaace_mps_pstr_qmf_filter_bank pstr_qmf_filter_bank, FLOAT32 *real_subband,
115     FLOAT32 *imag_subband, const FLOAT32 *time_in, const WORD32 stride, FLOAT32 *p_work_buffer,
116     WORD8 *ptr_scratch) {
117   IA_ERRORCODE error = IA_NO_ERROR;
118   WORD32 idx;
119   WORD32 offset = pstr_qmf_filter_bank->no_channels * (QMF_NO_POLY * 2 - 1);
120   FLOAT32 *pstr_filter_states_ana_tmp =
121       ((FLOAT32 *)pstr_qmf_filter_bank->ptr_filter_states) + offset;
122   for (idx = 0; idx < pstr_qmf_filter_bank->no_channels; idx++) {
123     *pstr_filter_states_ana_tmp++ = (FLOAT32)*time_in;
124     time_in += stride;
125   }
126   ixheaace_mps_212_qmf_ana_prototype_fir_slot(
127       p_work_buffer, pstr_qmf_filter_bank->no_channels, pstr_qmf_filter_bank->pstr_filter,
128       pstr_qmf_filter_bank->p_stride, (FLOAT32 *)pstr_qmf_filter_bank->ptr_filter_states);
129 
130   error = ixheaace_mps_212_qmf_forward_modulation_hq(pstr_qmf_filter_bank, p_work_buffer,
131                                                      real_subband, imag_subband, ptr_scratch);
132   if (error != IA_NO_ERROR) {
133     return error;
134   }
135 
136   memmove(pstr_qmf_filter_bank->ptr_filter_states,
137           (FLOAT32 *)pstr_qmf_filter_bank->ptr_filter_states + pstr_qmf_filter_bank->no_channels,
138           offset * sizeof(FLOAT32));
139   return error;
140 }
141 
142 IA_ERRORCODE
ixheaace_mps_212_qmf_init_filter_bank(ixheaace_mps_pstr_qmf_filter_bank pstr_qmf_filter_bank,VOID * ptr_filter_states,WORD32 num_cols,WORD32 lsb,WORD32 usb,WORD32 no_channels)143 ixheaace_mps_212_qmf_init_filter_bank(ixheaace_mps_pstr_qmf_filter_bank pstr_qmf_filter_bank,
144                                       VOID *ptr_filter_states, WORD32 num_cols, WORD32 lsb,
145                                       WORD32 usb, WORD32 no_channels) {
146   memset(pstr_qmf_filter_bank, 0, sizeof(ixheaace_mps_qmf_filter_bank));
147   pstr_qmf_filter_bank->no_channels = no_channels;
148   pstr_qmf_filter_bank->no_col = num_cols;
149   pstr_qmf_filter_bank->lsb = MIN(lsb, pstr_qmf_filter_bank->no_channels);
150   pstr_qmf_filter_bank->usb = MIN(usb, pstr_qmf_filter_bank->no_channels);
151   pstr_qmf_filter_bank->ptr_filter_states = (VOID *)ptr_filter_states;
152   memset(pstr_qmf_filter_bank->ptr_filter_states, 0,
153          (2 * QMF_NO_POLY - 1) * pstr_qmf_filter_bank->no_channels * sizeof(FLOAT32));
154   pstr_qmf_filter_bank->p_stride = 1;
155   if (no_channels == 64) {
156     pstr_qmf_filter_bank->pstr_filter = qmf_mps_ld_fb_640;
157     pstr_qmf_filter_bank->filter_size = 640;
158   } else {
159     pstr_qmf_filter_bank->pstr_filter = qmf_mps_ld_fb_320;
160     pstr_qmf_filter_bank->filter_size = 320;
161   }
162 
163   return IA_NO_ERROR;
164 }
165