xref: /aosp_15_r20/external/libxaac/decoder/drc_src/impd_drc_init.c (revision 15dc779a375ca8b5125643b829a8aa4b70d7f451)
1 /******************************************************************************
2  *
3  * Copyright (C) 2018 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 #include <stdlib.h>
21 #include <math.h>
22 #include <string.h>
23 #include "impd_type_def.h"
24 #include "impd_error_standards.h"
25 #include <string.h>
26 #include "impd_drc_extr_delta_coded_info.h"
27 #include "ixheaac_constants.h"
28 #include "impd_drc_common.h"
29 #include "impd_drc_struct.h"
30 #include "impd_drc_interface.h"
31 #include "impd_memory_standards.h"
32 #include "impd_drc_peak_limiter.h"
33 #include "impd_drc_bitbuffer.h"
34 #include "impd_drc_bitstream_dec_api.h"
35 #include "impd_drc_gain_dec.h"
36 #include "impd_drc_filter_bank.h"
37 #include "impd_drc_multi_band.h"
38 #include "impd_drc_process_audio.h"
39 #include "impd_parametric_drc_dec.h"
40 #include "impd_drc_eq.h"
41 #include "impd_drc_gain_decoder.h"
42 #include "impd_drc_selection_process.h"
43 #include "impd_drc_api_struct_def.h"
44 #include "impd_drc_peak_limiter.h"
45 
46 #define PARAMETRIC_DRC_DELAY_MAX_DEFAULT 4096
47 #define EQ_DELAY_MAX_DEFAULT 256
48 
49 IA_ERRORCODE impd_drc_mem_api(ia_drc_api_struct *p_obj_drc, WORD32 iCmd,
50                               WORD32 iIdx, pVOID pvValue);
51 
52 IA_ERRORCODE impd_drc_fill_mem_tables(ia_drc_api_struct *p_obj_drc);
53 
54 WORD32
55 impd_drc_dec_interface_process(ia_bit_buf_struct *it_bit_buff,
56                                ia_drc_interface_struct *pstr_drc_interface,
57                                UWORD8 *it_bit_buf, WORD32 num_bit_stream_bits,
58                                WORD32 *num_bits_read);
59 
60 WORD32
61 impd_drc_dec_interface_add_effect_type(
62     ia_drc_interface_struct *pstr_drc_interface, WORD32 drc_effect_type,
63     WORD32 target_loudness, WORD32 loud_norm, WORD32 album_mode, FLOAT32 boost,
64     FLOAT32 compress);
65 
66 #define BITSTREAM_FILE_FORMAT_SPLIT 1
67 #define LIM_DEFAULT_THRESHOLD (0.89125094f)
68 
impd_match_downmix(WORD32 downmix_id,WORD32 dec_downmix_id)69 static WORD32 impd_match_downmix(WORD32 downmix_id, WORD32 dec_downmix_id) {
70   WORD32 id_match = 0;
71 
72   switch (dec_downmix_id) {
73     case 0:
74       id_match = (downmix_id == 0);
75       break;
76     case 1:
77       id_match = ((downmix_id == 0) || (downmix_id == 0x7F));
78       break;
79     case 2:
80       id_match = (downmix_id == 0x7F);
81       break;
82     case 3:
83       id_match = ((downmix_id != 0) && (downmix_id != 0x7F));
84       break;
85     case 4:
86       id_match = (downmix_id != 0);
87       break;
88   }
89   return id_match;
90 }
91 
impd_drc_set_default_config(ia_drc_api_struct * p_obj_drc)92 IA_ERRORCODE impd_drc_set_default_config(ia_drc_api_struct *p_obj_drc) {
93   memset(p_obj_drc, 0, sizeof(*p_obj_drc));
94   p_obj_drc->str_config.bitstream_file_format = 1;
95   p_obj_drc->str_config.dec_type = 0;
96   p_obj_drc->str_config.sub_band_domain_mode = 0;
97   p_obj_drc->str_config.sub_band_count = 0;
98   p_obj_drc->str_config.sub_band_down_sampling_factor = 0;
99   p_obj_drc->str_config.sampling_rate = 0;
100   p_obj_drc->str_config.frame_size = 1024;
101   p_obj_drc->str_config.num_ch_in = -1;
102   p_obj_drc->str_config.num_ch_out = -1;
103   p_obj_drc->str_config.control_parameter_index = -1;
104   p_obj_drc->str_config.peak_limiter = 0;
105   p_obj_drc->str_config.delay_mode = 0;
106   p_obj_drc->str_config.interface_bitstream_present = 1;
107   p_obj_drc->str_config.gain_delay_samples = 0;
108   p_obj_drc->str_config.absorb_delay_on = 1;
109   p_obj_drc->str_config.subband_domain_io_flag = 0;
110   p_obj_drc->str_bit_handler.gain_stream_flag = 1;
111   p_obj_drc->str_config.constant_delay_on = 0;
112   p_obj_drc->str_config.audio_delay_samples = 0;
113   p_obj_drc->str_config.effect_type = 0;
114   p_obj_drc->str_config.target_loudness = -24;
115   p_obj_drc->str_config.loud_norm_flag = 0;
116   p_obj_drc->str_config.album_mode = 0;
117   p_obj_drc->str_config.boost = 1.0f;
118   p_obj_drc->str_config.compress = 1.0f;
119   memset(&p_obj_drc->str_bit_handler, 0, sizeof(p_obj_drc->str_bit_handler));
120 
121   return IA_NO_ERROR;
122 }
123 
impd_drc_set_default_bitstream_config(ia_drc_config * pstr_drc_config)124 IA_ERRORCODE impd_drc_set_default_bitstream_config(
125     ia_drc_config *pstr_drc_config) {
126   WORD32 i;
127 
128   pstr_drc_config->sample_rate_present = 0;
129   pstr_drc_config->sampling_rate = 0;
130   pstr_drc_config->dwnmix_instructions_count = 0;
131   pstr_drc_config->drc_coefficients_drc_count = 1;
132   pstr_drc_config->drc_instructions_uni_drc_count = 4;
133   pstr_drc_config->drc_instructions_count_plus = 5;
134   pstr_drc_config->drc_description_basic_present = 0;
135   pstr_drc_config->drc_coefficients_basic_count = 0;
136   pstr_drc_config->drc_instructions_basic_count = 0;
137   pstr_drc_config->drc_config_ext_present = 1;
138   pstr_drc_config->apply_drc = 0;
139   pstr_drc_config->str_drc_config_ext.drc_config_ext_type[0] = 2;
140   pstr_drc_config->str_drc_config_ext.ext_bit_size[0] = 345;
141   pstr_drc_config->str_drc_config_ext.parametric_drc_present = 0;
142   pstr_drc_config->str_drc_config_ext.drc_extension_v1_present = 1;
143   pstr_drc_config->str_p_loc_drc_coefficients_uni_drc[0].version = 1;
144   pstr_drc_config->str_p_loc_drc_coefficients_uni_drc[0].drc_location = 1;
145   pstr_drc_config->str_p_loc_drc_coefficients_uni_drc[0].gain_set_count = 4;
146   for (i = 0;
147        i <
148        pstr_drc_config->str_p_loc_drc_coefficients_uni_drc[0].gain_set_count;
149        i++) {
150     pstr_drc_config->str_p_loc_drc_coefficients_uni_drc[0]
151         .gain_set_params[i]
152         .gain_interpolation_type = 1;
153     pstr_drc_config->str_p_loc_drc_coefficients_uni_drc[0]
154         .gain_set_params[i]
155         .band_count = 1;
156   }
157   pstr_drc_config->str_p_loc_drc_coefficients_uni_drc[0].gain_sequence_count =
158       4;
159   pstr_drc_config->str_p_loc_drc_coefficients_uni_drc[0]
160       .gain_set_params_index_for_gain_sequence[0] = 0;
161   pstr_drc_config->str_p_loc_drc_coefficients_uni_drc[0]
162       .gain_set_params_index_for_gain_sequence[1] = 1;
163   pstr_drc_config->str_p_loc_drc_coefficients_uni_drc[0]
164       .gain_set_params_index_for_gain_sequence[2] = 2;
165   pstr_drc_config->str_p_loc_drc_coefficients_uni_drc[0]
166       .gain_set_params_index_for_gain_sequence[3] = 3;
167   pstr_drc_config->str_p_loc_drc_coefficients_uni_drc[0].gain_set_count_plus =
168       4;
169   pstr_drc_config->str_drc_instruction_str[0].drc_set_id = 1;
170   pstr_drc_config->str_drc_instruction_str[0].drc_set_complexity_level = 2;
171   pstr_drc_config->str_drc_instruction_str[0].drc_location = 1;
172   pstr_drc_config->str_drc_instruction_str[0].dwnmix_id_count = 1;
173   pstr_drc_config->str_drc_instruction_str[0].drc_set_effect = 1;
174   pstr_drc_config->str_drc_instruction_str[0].gain_set_index[1] = 1;
175   pstr_drc_config->str_drc_instruction_str[0]
176       .drc_set_target_loudness_value_lower = -63;
177   pstr_drc_config->str_drc_instruction_str[0].num_drc_ch_groups = 2;
178   pstr_drc_config->str_drc_instruction_str[0]
179       .gain_set_index_for_channel_group[1] = 1;
180   pstr_drc_config->str_drc_instruction_str[0].band_count_of_ch_group[0] = 1;
181   pstr_drc_config->str_drc_instruction_str[0].band_count_of_ch_group[1] = 1;
182   pstr_drc_config->str_drc_instruction_str[0]
183       .gain_interpolation_type_for_channel_group[0] = 1;
184   pstr_drc_config->str_drc_instruction_str[0]
185       .gain_interpolation_type_for_channel_group[1] = 1;
186   pstr_drc_config->str_drc_instruction_str[0]
187       .time_delta_min_for_channel_group[0] = 32;
188   pstr_drc_config->str_drc_instruction_str[0]
189       .time_delta_min_for_channel_group[1] = 32;
190   pstr_drc_config->str_drc_instruction_str[0].channel_group_of_ch[1] = 1;
191   pstr_drc_config->str_drc_instruction_str[0].gain_element_count = 2;
192 
193   pstr_drc_config->str_drc_instruction_str[1].drc_set_id = 2;
194   pstr_drc_config->str_drc_instruction_str[1].drc_set_complexity_level = 2;
195   pstr_drc_config->str_drc_instruction_str[1].drc_location = 1;
196   pstr_drc_config->str_drc_instruction_str[1].dwnmix_id_count = 1;
197   pstr_drc_config->str_drc_instruction_str[1].drc_set_effect = 2;
198   pstr_drc_config->str_drc_instruction_str[1].gain_set_index[0] = 1;
199   pstr_drc_config->str_drc_instruction_str[1].gain_set_index[1] = 2;
200   pstr_drc_config->str_drc_instruction_str[1]
201       .drc_set_target_loudness_value_lower = -63;
202   pstr_drc_config->str_drc_instruction_str[1].num_drc_ch_groups = 2;
203   pstr_drc_config->str_drc_instruction_str[1]
204       .gain_set_index_for_channel_group[0] = 1;
205   pstr_drc_config->str_drc_instruction_str[1]
206       .gain_set_index_for_channel_group[1] = 2;
207   pstr_drc_config->str_drc_instruction_str[1].band_count_of_ch_group[0] = 1;
208   pstr_drc_config->str_drc_instruction_str[1].band_count_of_ch_group[1] = 1;
209   pstr_drc_config->str_drc_instruction_str[1]
210       .gain_interpolation_type_for_channel_group[0] = 1;
211   pstr_drc_config->str_drc_instruction_str[1]
212       .gain_interpolation_type_for_channel_group[1] = 1;
213   pstr_drc_config->str_drc_instruction_str[1]
214       .time_delta_min_for_channel_group[0] = 32;
215   pstr_drc_config->str_drc_instruction_str[1]
216       .time_delta_min_for_channel_group[1] = 32;
217   pstr_drc_config->str_drc_instruction_str[1].channel_group_of_ch[1] = 1;
218   pstr_drc_config->str_drc_instruction_str[1].gain_element_count = 2;
219 
220   pstr_drc_config->str_drc_instruction_str[2].drc_set_id = 3;
221   pstr_drc_config->str_drc_instruction_str[2].drc_set_complexity_level = 2;
222   pstr_drc_config->str_drc_instruction_str[2].drc_location = 1;
223   pstr_drc_config->str_drc_instruction_str[2].dwnmix_id_count = 1;
224   pstr_drc_config->str_drc_instruction_str[2].drc_set_effect = 4;
225   pstr_drc_config->str_drc_instruction_str[2].gain_set_index[0] = 2;
226   pstr_drc_config->str_drc_instruction_str[2].gain_set_index[1] = 3;
227   pstr_drc_config->str_drc_instruction_str[2]
228       .drc_set_target_loudness_value_lower = -63;
229   pstr_drc_config->str_drc_instruction_str[2].num_drc_ch_groups = 2;
230   pstr_drc_config->str_drc_instruction_str[2]
231       .gain_set_index_for_channel_group[0] = 2;
232   pstr_drc_config->str_drc_instruction_str[2]
233       .gain_set_index_for_channel_group[1] = 3;
234   pstr_drc_config->str_drc_instruction_str[2].band_count_of_ch_group[0] = 1;
235   pstr_drc_config->str_drc_instruction_str[2].band_count_of_ch_group[1] = 1;
236   pstr_drc_config->str_drc_instruction_str[2]
237       .gain_interpolation_type_for_channel_group[0] = 1;
238   pstr_drc_config->str_drc_instruction_str[2]
239       .gain_interpolation_type_for_channel_group[1] = 1;
240   pstr_drc_config->str_drc_instruction_str[2]
241       .time_delta_min_for_channel_group[0] = 32;
242   pstr_drc_config->str_drc_instruction_str[2]
243       .time_delta_min_for_channel_group[1] = 32;
244   pstr_drc_config->str_drc_instruction_str[2].channel_group_of_ch[1] = 1;
245   pstr_drc_config->str_drc_instruction_str[2].gain_element_count = 2;
246 
247   pstr_drc_config->str_drc_instruction_str[3].drc_set_id = 4;
248   pstr_drc_config->str_drc_instruction_str[3].drc_set_complexity_level = 2;
249   pstr_drc_config->str_drc_instruction_str[3].drc_location = 1;
250   pstr_drc_config->str_drc_instruction_str[3].dwnmix_id_count = 1;
251   pstr_drc_config->str_drc_instruction_str[3].drc_set_effect = 32;
252   pstr_drc_config->str_drc_instruction_str[3].gain_set_index[0] = 3;
253   pstr_drc_config->str_drc_instruction_str[3].gain_set_index[1] = 0;
254   pstr_drc_config->str_drc_instruction_str[3]
255       .drc_set_target_loudness_value_lower = -63;
256   pstr_drc_config->str_drc_instruction_str[3].num_drc_ch_groups = 2;
257   pstr_drc_config->str_drc_instruction_str[3]
258       .gain_set_index_for_channel_group[0] = 3;
259   pstr_drc_config->str_drc_instruction_str[3]
260       .gain_set_index_for_channel_group[1] = 0;
261   pstr_drc_config->str_drc_instruction_str[3].band_count_of_ch_group[0] = 1;
262   pstr_drc_config->str_drc_instruction_str[3].band_count_of_ch_group[1] = 1;
263   pstr_drc_config->str_drc_instruction_str[3]
264       .gain_interpolation_type_for_channel_group[0] = 1;
265   pstr_drc_config->str_drc_instruction_str[3]
266       .gain_interpolation_type_for_channel_group[1] = 1;
267   pstr_drc_config->str_drc_instruction_str[3]
268       .time_delta_min_for_channel_group[0] = 32;
269   pstr_drc_config->str_drc_instruction_str[3]
270       .time_delta_min_for_channel_group[1] = 32;
271   pstr_drc_config->str_drc_instruction_str[3].channel_group_of_ch[1] = 1;
272   pstr_drc_config->str_drc_instruction_str[3].gain_element_count = 2;
273 
274   pstr_drc_config->str_drc_instruction_str[4].drc_set_id = -1;
275   pstr_drc_config->str_drc_instruction_str[4].dwnmix_id_count = 1;
276   pstr_drc_config->channel_layout.base_channel_count = 2;
277 
278   return IA_NO_ERROR;
279 }
280 
impd_drc_set_struct_pointer(ia_drc_api_struct * p_obj_drc)281 IA_ERRORCODE impd_drc_set_struct_pointer(ia_drc_api_struct *p_obj_drc) {
282   pUWORD8 persistent_ptr = (pUWORD8)p_obj_drc->p_state->persistent_ptr;
283 
284   UWORD64 persistent_size_consumed = 0;
285   p_obj_drc->str_payload.pstr_bitstream_dec = (ia_drc_bits_dec_struct *)persistent_ptr;
286   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_drc_bits_dec_struct), BYTE_ALIGN_8);
287 
288   p_obj_drc->str_payload.pstr_gain_dec[0] = (ia_drc_gain_dec_struct *)persistent_ptr;
289   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_drc_gain_dec_struct), BYTE_ALIGN_8);
290 
291   p_obj_drc->str_payload.pstr_gain_dec[1] = (ia_drc_gain_dec_struct *)persistent_ptr;
292   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_drc_gain_dec_struct), BYTE_ALIGN_8);
293 
294   p_obj_drc->str_payload.pstr_loudness_info = (ia_drc_loudness_info_set_struct *)persistent_ptr;
295   persistent_ptr +=
296       IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_drc_loudness_info_set_struct), BYTE_ALIGN_8);
297 
298   p_obj_drc->str_payload.pstr_drc_gain = (ia_drc_gain_struct *)persistent_ptr;
299   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_drc_gain_struct), BYTE_ALIGN_8);
300 
301   p_obj_drc->str_payload.pstr_drc_interface = (ia_drc_interface_struct *)persistent_ptr;
302   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_drc_interface_struct), BYTE_ALIGN_8);
303 
304   p_obj_drc->str_payload.pstr_drc_config = (ia_drc_config *)persistent_ptr;
305   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_drc_config), BYTE_ALIGN_8);
306 
307   p_obj_drc->str_payload.pstr_selection_proc = (ia_drc_sel_pro_struct *)persistent_ptr;
308   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_drc_sel_pro_struct), BYTE_ALIGN_8);
309 
310   p_obj_drc->str_bit_handler.it_bit_buf = (UWORD8 *)persistent_ptr;
311   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(MAX_DRC_BS_BUF_SIZE, BYTE_ALIGN_8);
312 
313   p_obj_drc->str_payload.pstr_drc_sel_proc_params =
314       (ia_drc_sel_proc_params_struct *)persistent_ptr;
315   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_drc_sel_proc_params_struct), BYTE_ALIGN_8);
316 
317   p_obj_drc->str_payload.pstr_drc_sel_proc_output =
318       (ia_drc_sel_proc_output_struct *)persistent_ptr;
319   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_drc_sel_proc_output_struct), BYTE_ALIGN_8);
320 
321   p_obj_drc->str_bit_handler.bitstream_drc_config = (UWORD8 *)persistent_ptr;
322   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(MAX_BS_BUF_SIZE, BYTE_ALIGN_8);
323 
324   p_obj_drc->str_bit_handler.bitstream_loudness_info = (UWORD8 *)persistent_ptr;
325   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(MAX_BS_BUF_SIZE, BYTE_ALIGN_8);
326 
327   p_obj_drc->str_bit_handler.bitstream_unidrc_interface = (UWORD8 *)persistent_ptr;
328   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(MAX_BS_BUF_SIZE, BYTE_ALIGN_8);
329 
330   p_obj_drc->str_payload.pstr_peak_limiter = (ia_drc_peak_limiter_struct *)persistent_ptr;
331   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_drc_peak_limiter_struct), BYTE_ALIGN_8);
332 
333   p_obj_drc->str_payload.pstr_peak_limiter->buffer = (FLOAT32 *)persistent_ptr;
334   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(PEAK_LIM_BUF_SIZE, BYTE_ALIGN_8);
335 
336   p_obj_drc->str_payload.pstr_qmf_filter = (ia_drc_qmf_filt_struct *)persistent_ptr;
337   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(sizeof(ia_drc_qmf_filt_struct), BYTE_ALIGN_8);
338 
339   p_obj_drc->str_payload.pstr_qmf_filter->ana_buff = (FLOAT64 *)persistent_ptr;
340   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(ANALY_BUF_SIZE, BYTE_ALIGN_8);
341 
342   p_obj_drc->str_payload.pstr_qmf_filter->syn_buff = (FLOAT64 *)persistent_ptr;
343   persistent_ptr += IXHEAAC_GET_SIZE_ALIGNED(SYNTH_BUF_SIZE, BYTE_ALIGN_8);
344 
345   persistent_size_consumed =
346       (UWORD64)(persistent_ptr - (pUWORD8)p_obj_drc->p_state->persistent_ptr);
347 
348   if ((UWORD64)p_obj_drc->p_mem_info[IA_MEMTYPE_PERSIST].ui_size <
349       persistent_size_consumed)
350     return IA_FATAL_ERROR;
351 
352   p_obj_drc->p_state->persistent_ptr = (pVOID)persistent_ptr;
353   return IA_NO_ERROR;
354 }
355 
init_qmf_filt_bank(ia_drc_qmf_filt_struct * qmf_filt)356 VOID init_qmf_filt_bank(ia_drc_qmf_filt_struct *qmf_filt) {
357   WORD32 l, k;
358 
359   FLOAT64 gain_ana = 64.0 / QMF_FILT_RESOLUTION;
360   FLOAT64 gain_syn = 1.0 / 64.0;
361   for (l = 0; l < 2 * QMF_FILT_RESOLUTION; l++) {
362     for (k = 0; k < QMF_FILT_RESOLUTION; k++) {
363       qmf_filt->syn_tab_real[l][k] =
364           gain_syn * cos((0.0245436926) * (k + 0.5) * (2 * l - 255.0));
365       qmf_filt->syn_tab_imag[l][k] =
366           gain_syn * sin((0.0245436926) * (k + 0.5) * (2 * l - 255.0));
367       qmf_filt->ana_tab_real[k][l] =
368           gain_ana * cos((0.0245436926) * (k + 0.5) * (2 * l - 1.0));
369       qmf_filt->ana_tab_imag[k][l] =
370           gain_ana * sin((0.0245436926) * (k + 0.5) * (2 * l - 1.0));
371     }
372   }
373 }
374 
impd_drc_init(ia_drc_api_struct * p_obj_drc)375 IA_ERRORCODE impd_drc_init(ia_drc_api_struct *p_obj_drc) {
376   IA_ERRORCODE err_code = IA_NO_ERROR;
377   WORD32 i, j;
378 
379   pVOID persistent_ptr = p_obj_drc->p_state->persistent_ptr;
380 
381   WORD32 decDownmixIdList[NUM_GAIN_DEC_INSTANCES] = {0, 4};
382 
383   p_obj_drc->p_state->delay_in_output = 0;
384   p_obj_drc->str_payload.pstr_selection_proc->first_frame = 1;
385 
386   impd_create_init_bit_buf(&p_obj_drc->str_bit_buf,
387                            p_obj_drc->str_bit_handler.it_bit_buf,
388                            p_obj_drc->str_bit_handler.num_bytes_bs / 8);
389 
390   p_obj_drc->pstr_bit_buf = &p_obj_drc->str_bit_buf;
391 
392   err_code = impd_init_drc_bitstream_dec(
393       p_obj_drc->str_payload.pstr_bitstream_dec,
394       p_obj_drc->str_config.sampling_rate, p_obj_drc->str_config.frame_size,
395       p_obj_drc->str_config.delay_mode, -1, 0);
396   if (err_code != IA_NO_ERROR) return err_code;
397 
398   for (i = 0; i < NUM_GAIN_DEC_INSTANCES; i++) {
399     err_code = impd_init_drc_decode(p_obj_drc->str_config.frame_size,
400                                     p_obj_drc->str_config.sampling_rate,
401                                     p_obj_drc->str_config.gain_delay_samples,
402                                     p_obj_drc->str_config.delay_mode,
403                                     p_obj_drc->str_config.sub_band_domain_mode,
404                                     p_obj_drc->str_payload.pstr_gain_dec[i]);
405     if (err_code != IA_NO_ERROR) return err_code;
406   }
407 
408   if (!p_obj_drc->str_config.boost_set) p_obj_drc->str_config.boost = 1.0f;
409 
410   if (!p_obj_drc->str_config.compress_set)
411     p_obj_drc->str_config.compress = 1.0f;
412 
413   err_code = impd_drc_dec_interface_add_effect_type(
414       p_obj_drc->str_payload.pstr_drc_interface,
415       p_obj_drc->str_config.effect_type, p_obj_drc->str_config.target_loudness,
416       p_obj_drc->str_config.loud_norm_flag, p_obj_drc->str_config.album_mode,
417       p_obj_drc->str_config.boost, p_obj_drc->str_config.compress);
418 
419   if (err_code != IA_NO_ERROR) return err_code;
420 
421   err_code = impd_drc_uni_selction_proc_init(
422       p_obj_drc->str_payload.pstr_selection_proc, 0,
423       p_obj_drc->str_payload.pstr_drc_interface,
424       p_obj_drc->str_config.sub_band_domain_mode);
425   if (err_code != IA_NO_ERROR) return err_code;
426 
427   if (p_obj_drc->str_payload.pstr_drc_interface
428           ->loudness_norm_parameter_interface_flag &&
429       p_obj_drc->str_payload.pstr_drc_interface->loudness_norm_param_interface
430           .peak_limiter) {
431     p_obj_drc->str_config.peak_limiter = 1;
432   }
433 
434   p_obj_drc->str_payload.pstr_loudness_info->loudness_info_album_count = 0;
435   p_obj_drc->str_payload.pstr_loudness_info->loudness_info_count = 0;
436   p_obj_drc->str_payload.pstr_loudness_info->loudness_info_set_ext_present = 0;
437   p_obj_drc->p_state->ui_exe_done = 0;
438 
439   err_code = impd_process_drc_bitstream_dec_config(
440       p_obj_drc->str_payload.pstr_bitstream_dec, p_obj_drc->pstr_bit_buf,
441       p_obj_drc->str_payload.pstr_drc_config,
442       &p_obj_drc->str_bit_handler.bitstream_drc_config[0],
443       p_obj_drc->str_bit_handler.num_bytes_bs_drc_config);
444 
445   if (err_code == 1) {
446     memset(p_obj_drc->str_payload.pstr_drc_config, 0, sizeof(ia_drc_config));
447     err_code = impd_drc_set_default_bitstream_config(
448         p_obj_drc->str_payload.pstr_drc_config);
449     p_obj_drc->str_payload.pstr_drc_config->channel_layout.base_channel_count =
450         p_obj_drc->str_config.num_ch_in;
451   }
452 
453   if (err_code != IA_NO_ERROR) return err_code;
454   err_code = impd_process_drc_bitstream_dec_loudness_info_set(
455       p_obj_drc->pstr_bit_buf, p_obj_drc->str_payload.pstr_loudness_info,
456       &p_obj_drc->str_bit_handler.bitstream_loudness_info[0],
457       p_obj_drc->str_bit_handler.num_bytes_bs_loudness_info);
458   if (err_code != IA_NO_ERROR) return err_code;
459 
460   if (p_obj_drc->str_payload.pstr_loudness_info->loudness_info
461           ->anchor_loudness_present)
462     p_obj_drc->str_payload.pstr_selection_proc->uni_drc_sel_proc_params
463         .loudness_measurement_method = METHOD_DEFINITION_ANCHOR_LOUDNESS;
464 
465   if (p_obj_drc->str_payload.pstr_loudness_info->loudness_info
466           ->expert_loudness_present)
467     p_obj_drc->str_payload.pstr_selection_proc->uni_drc_sel_proc_params
468         .loudness_measurement_system = USER_MEASUREMENT_SYSTEM_EXPERT_PANEL;
469 
470   err_code = impd_drc_uni_sel_proc_process(
471       p_obj_drc->str_payload.pstr_selection_proc,
472       p_obj_drc->str_payload.pstr_drc_config,
473       p_obj_drc->str_payload.pstr_loudness_info,
474       p_obj_drc->str_payload.pstr_drc_sel_proc_output);
475   if (err_code != IA_NO_ERROR) return err_code;
476 
477   for (i = 0; i < NUM_GAIN_DEC_INSTANCES; i++) {
478     WORD32 audio_num_chan = 0;
479     WORD32 numMatchingDrcSets = 0;
480     WORD32 matchingDrcSetIds[3], matchingDownmixIds[3];
481     for (j = 0;
482          j < p_obj_drc->str_payload.pstr_drc_sel_proc_output->num_sel_drc_sets;
483          j++) {
484       if (impd_match_downmix(p_obj_drc->str_payload.pstr_drc_sel_proc_output
485                                  ->sel_downmix_ids[j],
486                              decDownmixIdList[i])) {
487         matchingDrcSetIds[numMatchingDrcSets] =
488             p_obj_drc->str_payload.pstr_drc_sel_proc_output->sel_drc_set_ids[j];
489         matchingDownmixIds[numMatchingDrcSets] =
490             p_obj_drc->str_payload.pstr_drc_sel_proc_output->sel_downmix_ids[j];
491         numMatchingDrcSets++;
492       }
493     }
494     if (i == 0) {
495       if (p_obj_drc->str_config.num_ch_in !=
496           p_obj_drc->str_payload.pstr_drc_sel_proc_output->base_channel_count)
497 
498         return -1;
499       audio_num_chan = p_obj_drc->str_config.num_ch_in;
500     } else if (i == 1) {
501       p_obj_drc->str_config.num_ch_out =
502           p_obj_drc->str_payload.pstr_drc_sel_proc_output->target_channel_count;
503       audio_num_chan = p_obj_drc->str_config.num_ch_out;
504     }
505 
506     err_code = impd_init_drc_decode_post_config(
507         audio_num_chan, matchingDrcSetIds, matchingDownmixIds,
508         numMatchingDrcSets,
509         p_obj_drc->str_payload.pstr_drc_sel_proc_output->sel_eq_set_ids[i]
510 
511         ,
512         p_obj_drc->str_payload.pstr_gain_dec[i],
513         p_obj_drc->str_payload.pstr_drc_config,
514         p_obj_drc->str_payload.pstr_loudness_info, &persistent_ptr);
515     if (err_code) return err_code;
516 
517     impd_get_parametric_drc_delay(
518         p_obj_drc->str_payload.pstr_gain_dec[i],
519         p_obj_drc->str_payload.pstr_drc_config,
520         &p_obj_drc->str_config.parametric_drc_delay_gain_dec_instance,
521         &p_obj_drc->str_config.parametric_drc_delay_max);
522     impd_get_eq_delay(p_obj_drc->str_payload.pstr_gain_dec[i],
523                       p_obj_drc->str_payload.pstr_drc_config,
524                       &p_obj_drc->str_config.eq_delay_gain_dec_instance,
525                       &p_obj_drc->str_config.eq_delay_max);
526     p_obj_drc->str_config.parametric_drc_delay +=
527         p_obj_drc->str_config.parametric_drc_delay_gain_dec_instance;
528     p_obj_drc->str_config.eq_delay +=
529         p_obj_drc->str_config.eq_delay_gain_dec_instance;
530   }
531 
532   {
533     if (p_obj_drc->str_config.parametric_drc_delay_max == -1) {
534       p_obj_drc->str_config.parametric_drc_delay_max =
535           PARAMETRIC_DRC_DELAY_MAX_DEFAULT;
536     }
537     if (p_obj_drc->str_config.eq_delay_max == -1) {
538       p_obj_drc->str_config.eq_delay_max = EQ_DELAY_MAX_DEFAULT;
539     }
540 
541     if (!p_obj_drc->str_config.constant_delay_on) {
542       p_obj_drc->p_state->delay_in_output +=
543           p_obj_drc->str_config.parametric_drc_delay +
544           p_obj_drc->str_config.eq_delay +
545           p_obj_drc->str_config.audio_delay_samples;
546       p_obj_drc->str_config.delay_line_samples =
547           p_obj_drc->str_config.audio_delay_samples;
548 
549       if (!p_obj_drc->str_config.absorb_delay_on) {
550         p_obj_drc->p_state->delay_in_output = 0;
551       }
552     } else {
553       p_obj_drc->p_state->delay_in_output +=
554           p_obj_drc->str_config.parametric_drc_delay_max +
555           p_obj_drc->str_config.eq_delay_max +
556           p_obj_drc->str_config.audio_delay_samples;
557       p_obj_drc->str_config.delay_line_samples =
558           p_obj_drc->p_state->delay_in_output -
559           p_obj_drc->str_config.parametric_drc_delay +
560           p_obj_drc->str_config.eq_delay;
561 
562       if (!p_obj_drc->str_config.absorb_delay_on) {
563         p_obj_drc->p_state->delay_in_output = 0;
564       }
565     }
566   }
567   if (p_obj_drc->str_config.dec_type == 1) {
568     init_qmf_filt_bank(p_obj_drc->str_payload.pstr_qmf_filter);
569   }
570 
571   if (p_obj_drc->str_config.peak_limiter) {
572     err_code = impd_peak_limiter_init(
573         p_obj_drc->str_payload.pstr_peak_limiter, DEFAULT_ATTACK_TIME_MS,
574         DEFAULT_RELEASE_TIME_MS, LIM_DEFAULT_THRESHOLD,
575         p_obj_drc->str_config.num_ch_out, p_obj_drc->str_config.sampling_rate,
576         p_obj_drc->str_payload.pstr_peak_limiter->buffer);
577     if (err_code) return (err_code);
578   }
579 
580   return IA_NO_ERROR;
581 }
582