xref: /aosp_15_r20/external/libxaac/encoder/ixheaace_mps_tree.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 <stdlib.h>
22 #include "ixheaac_type_def.h"
23 #include "ixheaac_error_standards.h"
24 #include "ixheaace_error_codes.h"
25 #include "ixheaace_mps_common_fix.h"
26 #include "ixheaace_mps_defines.h"
27 #include "ixheaace_mps_common_define.h"
28 #include "ixheaace_bitbuffer.h"
29 
30 #include "ixheaace_mps_bitstream.h"
31 #include "ixheaace_mps_param_extract.h"
32 #include "ixheaace_mps_frame_windowing.h"
33 #include "ixheaace_mps_tree.h"
34 #include "ixheaace_mps_buf.h"
35 #include "ixheaace_mps_lib.h"
36 #include "ixheaace_mps_onset_detect.h"
37 #include "ixheaace_mps_static_gain.h"
38 #include "ixheaace_mps_filter.h"
39 #include "ixheaace_mps_delay.h"
40 #include "ixheaace_mps_dmx_tdom_enh.h"
41 #include "ixheaace_mps_main_structure.h"
42 #include "ixheaace_mps_tools_rom.h"
43 #include "ixheaace_mps_qmf.h"
44 #include "ixheaace_mps_structure.h"
45 #include "ixheaace_mps_struct_def.h"
46 #include "ixheaace_mps_sac_polyphase.h"
47 #include "ixheaace_mps_sac_nlc_enc.h"
48 #include "ixheaace_mps_sac_hybfilter.h"
49 #include "ixheaace_mps_spatial_bitstream.h"
50 #include "ixheaace_mps_rom.h"
51 
ixheaace_mps_212_space_tree_frame_keep_212(const ixheaace_mps_pstr_space_tree pstr_space_tree,ixheaace_mps_spatial_frame * const pstr_spatial_frame,const WORD32 avoid_keep)52 static VOID ixheaace_mps_212_space_tree_frame_keep_212(
53     const ixheaace_mps_pstr_space_tree pstr_space_tree,
54     ixheaace_mps_spatial_frame *const pstr_spatial_frame, const WORD32 avoid_keep) {
55   WORD32 band;
56 
57   if (avoid_keep) {
58     for (band = 0; band < pstr_space_tree->num_param_bands; band++) {
59       pstr_space_tree->icc_prev[0][band] = pstr_spatial_frame->ott_data.icc[0][0][band];
60       pstr_space_tree->cld_prev[0][band] = pstr_spatial_frame->ott_data.cld[0][0][band];
61     }
62   } else {
63     if (pstr_space_tree->frame_count % 2) {
64       for (band = 0; band < pstr_space_tree->num_param_bands; band++) {
65         pstr_spatial_frame->ott_data.icc[0][0][band] = pstr_space_tree->icc_prev[0][band];
66         pstr_space_tree->cld_prev[0][band] = pstr_spatial_frame->ott_data.cld[0][0][band];
67       }
68     } else {
69       for (band = 0; band < pstr_space_tree->num_param_bands; band++) {
70         pstr_space_tree->icc_prev[0][band] = pstr_spatial_frame->ott_data.icc[0][0][band];
71         pstr_spatial_frame->ott_data.cld[0][0][band] = pstr_space_tree->cld_prev[0][band];
72       }
73     }
74   }
75   pstr_space_tree->frame_count++;
76   if (pstr_space_tree->frame_count == MAX_KEEP_FRAMECOUNT) {
77     pstr_space_tree->frame_count = 0;
78   }
79 }
80 
81 IA_ERRORCODE
ixheaace_mps_212_space_tree_init(ixheaace_mps_pstr_space_tree pstr_space_tree,const ixheaace_mps_space_tree_setup * const pstr_space_tree_setup,UWORD8 * ptr_parameter_band_2_hybrid_band_offset,const WORD32 frame_keep_flag,WORD32 aot)82 ixheaace_mps_212_space_tree_init(ixheaace_mps_pstr_space_tree pstr_space_tree,
83                                  const ixheaace_mps_space_tree_setup *const pstr_space_tree_setup,
84                                  UWORD8 *ptr_parameter_band_2_hybrid_band_offset,
85                                  const WORD32 frame_keep_flag, WORD32 aot) {
86   IA_ERRORCODE error = IA_NO_ERROR;
87   WORD32 box = 0;
88 
89   pstr_space_tree->frame_count = 0;
90   pstr_space_tree->frame_keep_flag = frame_keep_flag;
91   pstr_space_tree->num_param_bands = pstr_space_tree_setup->num_param_bands;
92   pstr_space_tree->use_coarse_quant_tto_icc_flag =
93       pstr_space_tree_setup->use_coarse_quant_tto_icc_flag;
94   pstr_space_tree->use_coarse_quant_tto_cld_flag =
95       pstr_space_tree_setup->use_coarse_quant_tto_cld_flag;
96   pstr_space_tree->quant_mode = pstr_space_tree_setup->quant_mode;
97   pstr_space_tree->num_channels_in_max = pstr_space_tree_setup->num_channels_in_max;
98   pstr_space_tree->num_hybrid_bands_max = pstr_space_tree_setup->num_hybrid_bands_max;
99   pstr_space_tree->descr.num_ott_boxes = 1;
100   pstr_space_tree->descr.num_in_channels = 1;
101   pstr_space_tree->descr.num_out_channels = 2;
102 
103   if (pstr_space_tree->descr.num_ott_boxes > IXHEAACE_MPS_MAX_NUM_BOXES) {
104     return IA_EXHEAACE_INIT_FATAL_MPS_INIT_FAILED;
105   }
106 
107   for (box = 0; box < pstr_space_tree->descr.num_ott_boxes; box++) {
108     ixheaace_mps_tto_box_config box_config;
109     box_config.b_use_coherence_icc_only = 0;
110     box_config.subband_config = pstr_space_tree->num_param_bands;
111     box_config.use_coarse_quant_cld_flag = pstr_space_tree->use_coarse_quant_tto_cld_flag;
112     box_config.use_coarse_quant_icc_flag = pstr_space_tree->use_coarse_quant_tto_icc_flag;
113     box_config.box_quant_mode = pstr_space_tree->quant_mode;
114     box_config.num_hybrid_bands_max = pstr_space_tree->num_hybrid_bands_max;
115     box_config.frame_keep_flag = pstr_space_tree->frame_keep_flag;
116 
117     error = ixheaace_mps_212_init_tto_box(pstr_space_tree->pstr_tto_box[box], &box_config,
118                                           ptr_parameter_band_2_hybrid_band_offset, aot);
119     if (error) {
120       return error;
121     }
122   }
123 
124   return error;
125 }
126 
ixheaace_mps_212_space_tree_apply(ixheaace_mps_pstr_space_tree pstr_space_tree,const WORD32 param_set,const WORD32 num_channels_in,const WORD32 num_time_slots,const WORD32 start_time_slot,const WORD32 num_hybrid_bands,FLOAT32 * p_frame_window_ana_mps,ixheaace_cmplx_str ppp_cmplx_hybrid_1[IXHEAACE_MPS_MAX_INPUT_CHANNELS][MAX_ANA_TIME_SLOT][MAX_QMF_BANDS],ixheaace_cmplx_str ppp_cmplx_hybrid_2[IXHEAACE_MPS_MAX_INPUT_CHANNELS][MAX_ANA_TIME_SLOT][MAX_QMF_BANDS],ixheaace_mps_spatial_frame * const pstr_spatial_frame,const WORD32 avoid_keep)127 IA_ERRORCODE ixheaace_mps_212_space_tree_apply(
128     ixheaace_mps_pstr_space_tree pstr_space_tree, const WORD32 param_set,
129     const WORD32 num_channels_in, const WORD32 num_time_slots, const WORD32 start_time_slot,
130     const WORD32 num_hybrid_bands, FLOAT32 *p_frame_window_ana_mps,
131     ixheaace_cmplx_str ppp_cmplx_hybrid_1[IXHEAACE_MPS_MAX_INPUT_CHANNELS][MAX_ANA_TIME_SLOT]
132                                          [MAX_QMF_BANDS],
133     ixheaace_cmplx_str ppp_cmplx_hybrid_2[IXHEAACE_MPS_MAX_INPUT_CHANNELS][MAX_ANA_TIME_SLOT]
134                                          [MAX_QMF_BANDS],
135     ixheaace_mps_spatial_frame *const pstr_spatial_frame, const WORD32 avoid_keep) {
136   IA_ERRORCODE error = IA_NO_ERROR;
137   WORD32 box;
138   const ixheaace_mps_tree_setup *pstr_tree_setup = NULL;
139   pstr_tree_setup = &tree_setup_table;
140 
141   if ((num_channels_in != pstr_tree_setup->num_channels_in) ||
142       (num_channels_in > pstr_space_tree->num_channels_in_max) ||
143       (num_hybrid_bands > pstr_space_tree->num_hybrid_bands_max)) {
144     return IA_EXHEAACE_CONFIG_NONFATAL_MPS_INVALID_CONFIG;
145   }
146 
147   for (box = 0; box < pstr_tree_setup->n_tto_boxes; box++) {
148     const ixheaace_mps_tto_descriptor *pstr_tto_descriptor =
149         &pstr_tree_setup->tto_descriptor[box];
150     WORD32 i;
151     WORD32 in_ch[2], out_ch[2], win[2];
152 
153     in_ch[0] = pstr_tto_descriptor->in_ch1;
154     in_ch[1] = pstr_tto_descriptor->in_ch2;
155     out_ch[0] = pstr_tto_descriptor->in_ch3;
156     out_ch[1] = pstr_tto_descriptor->in_ch4;
157     win[0] = pstr_tto_descriptor->w_ch1;
158     win[1] = pstr_tto_descriptor->w_ch2;
159 
160     for (i = 0; i < 2; i++) {
161       if (win[i] == WIN_ACTIV) {
162         ixheaace_mps_212_analysis_windowing(num_time_slots, start_time_slot,
163                                             p_frame_window_ana_mps, ppp_cmplx_hybrid_1[in_ch[i]],
164                                             ppp_cmplx_hybrid_2[out_ch[i]], num_hybrid_bands);
165       }
166     }
167 
168     error = ixheaace_mps_212_apply_tto_box(
169         pstr_space_tree->pstr_tto_box[pstr_tto_descriptor->box_id], num_time_slots,
170         start_time_slot, num_hybrid_bands, ppp_cmplx_hybrid_2[pstr_tto_descriptor->in_ch3],
171         ppp_cmplx_hybrid_2[pstr_tto_descriptor->in_ch4],
172         pstr_spatial_frame->ott_data.icc[pstr_tto_descriptor->box_id][param_set],
173         &(pstr_spatial_frame->icc_lossless_data
174               .bs_quant_coarse_xxx[pstr_tto_descriptor->box_id][param_set]),
175         pstr_spatial_frame->ott_data.cld[pstr_tto_descriptor->box_id][param_set],
176         &(pstr_spatial_frame->cld_lossless_data
177               .bs_quant_coarse_xxx[pstr_tto_descriptor->box_id][param_set]),
178         pstr_spatial_frame->b_use_bb_cues);
179     if (error) {
180       return error;
181     }
182   }
183 
184   if (pstr_space_tree->frame_keep_flag == 1) {
185     ixheaace_mps_212_space_tree_frame_keep_212(pstr_space_tree, pstr_spatial_frame, avoid_keep);
186   }
187   return error;
188 }
189