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