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