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 "impd_type_def.h"
22 #include "impd_drc_extr_delta_coded_info.h"
23 #include "impd_drc_common.h"
24 #include "impd_drc_struct.h"
25 #include "impd_parametric_drc_dec.h"
26 #include "impd_drc_gain_dec.h"
27 #include "impd_drc_filter_bank.h"
28 #include "impd_drc_multi_band.h"
29
impd_init_drc_params(WORD32 frame_size,WORD32 sample_rate,WORD32 gain_delay_samples,WORD32 delay_mode,WORD32 sub_band_domain_mode,ia_drc_params_struct * ia_drc_params_struct)30 WORD32 impd_init_drc_params(WORD32 frame_size, WORD32 sample_rate,
31 WORD32 gain_delay_samples, WORD32 delay_mode,
32 WORD32 sub_band_domain_mode,
33 ia_drc_params_struct* ia_drc_params_struct) {
34 WORD32 k;
35 if (frame_size < 1 || frame_size > AUDIO_CODEC_FRAME_SIZE_MAX) {
36 return -1;
37 }
38
39 if (sample_rate < 1000) {
40 return -1;
41 }
42
43 ia_drc_params_struct->drc_frame_size = frame_size;
44
45 if (ia_drc_params_struct->drc_frame_size < 0.001f * sample_rate) {
46 return -1;
47 }
48
49 ia_drc_params_struct->sample_rate = sample_rate;
50
51 ia_drc_params_struct->delta_tmin_default = impd_get_delta_tmin(sample_rate);
52
53 if (ia_drc_params_struct->delta_tmin_default >
54 ia_drc_params_struct->drc_frame_size) {
55 return -1;
56 }
57
58 if ((delay_mode != DELAY_MODE_REGULAR_DELAY) &&
59 (delay_mode != DELAY_MODE_LOW_DELAY)) {
60 return -1;
61 }
62
63 ia_drc_params_struct->delay_mode = delay_mode;
64
65 ia_drc_params_struct->drc_set_counter = 0;
66 ia_drc_params_struct->multiband_sel_drc_idx = -1;
67
68 for (k = 0; k < SEL_DRC_COUNT; k++) {
69 ia_drc_params_struct->sel_drc_array[k].drc_instructions_index = -1;
70 ia_drc_params_struct->sel_drc_array[k].dwnmix_instructions_index = -1;
71 ia_drc_params_struct->sel_drc_array[k].drc_coeff_idx = -1;
72 }
73
74 if ((gain_delay_samples > MAX_SIGNAL_DELAY) || (gain_delay_samples < 0)) {
75 return -1;
76 } else {
77 ia_drc_params_struct->gain_delay_samples = gain_delay_samples;
78 }
79
80 switch (sub_band_domain_mode) {
81 case SUBBAND_DOMAIN_MODE_OFF:
82 case SUBBAND_DOMAIN_MODE_QMF64:
83 case SUBBAND_DOMAIN_MODE_QMF71:
84 case SUBBAND_DOMAIN_MODE_STFT256:
85 ia_drc_params_struct->sub_band_domain_mode = sub_band_domain_mode;
86 break;
87 default:
88 return -1;
89 break;
90 }
91
92 ia_drc_params_struct->parametric_drc_delay = 0;
93 ia_drc_params_struct->eq_delay = 0;
94
95 return 0;
96 }
97
impd_select_drc_coefficients(ia_drc_config * drc_config,ia_uni_drc_coeffs_struct ** drc_coefficients_drc,WORD32 * drc_coefficients_selected)98 WORD32 impd_select_drc_coefficients(
99 ia_drc_config* drc_config, ia_uni_drc_coeffs_struct** drc_coefficients_drc,
100 WORD32* drc_coefficients_selected) {
101 WORD32 i;
102 WORD32 cof1 = -1;
103 WORD32 cof0 = -1;
104 for (i = 0; i < drc_config->drc_coefficients_drc_count; i++) {
105 if (drc_config->str_p_loc_drc_coefficients_uni_drc[i].drc_location == 1) {
106 if (drc_config->str_p_loc_drc_coefficients_uni_drc[i].version == 0) {
107 cof0 = i;
108 *drc_coefficients_selected = cof0;
109 } else {
110 cof1 = i;
111 *drc_coefficients_selected = cof1;
112 }
113 }
114 }
115
116 if (cof1 >= 0) {
117 *drc_coefficients_drc =
118 &(drc_config->str_p_loc_drc_coefficients_uni_drc[cof1]);
119 } else if (cof0 >= 0) {
120 *drc_coefficients_drc =
121 &(drc_config->str_p_loc_drc_coefficients_uni_drc[cof0]);
122 } else {
123 *drc_coefficients_drc = NULL;
124 }
125
126 return 0;
127 }
128
impd_init_selected_drc_set(ia_drc_config * drc_config,ia_drc_params_struct * ia_drc_params_struct,ia_parametric_drc_params_struct * p_parametric_drc_params,WORD32 audio_num_chan,WORD32 drc_set_id_selected,WORD32 downmix_id_selected,ia_filter_banks_struct * ia_filter_banks_struct,ia_overlap_params_struct * pstr_overlap_params,shape_filter_block * shape_filter_block)129 WORD32 impd_init_selected_drc_set(
130 ia_drc_config* drc_config, ia_drc_params_struct* ia_drc_params_struct,
131 ia_parametric_drc_params_struct* p_parametric_drc_params,
132 WORD32 audio_num_chan, WORD32 drc_set_id_selected,
133 WORD32 downmix_id_selected, ia_filter_banks_struct* ia_filter_banks_struct,
134 ia_overlap_params_struct* pstr_overlap_params
135
136 ,
137 shape_filter_block* shape_filter_block) {
138 WORD32 g, n, c, err = 0;
139 WORD32 channel_count = 0;
140 WORD32 i;
141
142 ia_drc_instructions_struct* drc_instructions_uni_drc = NULL;
143 ia_uni_drc_coeffs_struct* drc_coefficients_uni_drc = NULL;
144 WORD32 selected_drc_is_multiband = 0;
145 WORD32 drc_instructions_selected = -1;
146 WORD32 downmix_instructions_selected = -1;
147 WORD32 drc_coefficients_selected = -1;
148 p_parametric_drc_params->parametric_drc_instance_count = 0;
149
150 if (drc_config->drc_coefficients_drc_count &&
151 drc_config->str_p_loc_drc_coefficients_uni_drc->drc_frame_size_present) {
152 if (drc_config->str_p_loc_drc_coefficients_uni_drc->drc_frame_size !=
153 ia_drc_params_struct->drc_frame_size) {
154 return -1;
155 }
156 }
157
158 for (n = 0; n < drc_config->drc_instructions_count_plus; n++) {
159 if (drc_config->str_drc_instruction_str[n].drc_set_id ==
160 drc_set_id_selected)
161 break;
162 }
163 if (n == drc_config->drc_instructions_count_plus) {
164 return -1;
165 }
166 drc_instructions_selected = n;
167 drc_instructions_uni_drc =
168 &(drc_config->str_drc_instruction_str[drc_instructions_selected]);
169
170 if (downmix_id_selected == ID_FOR_BASE_LAYOUT) {
171 channel_count = drc_config->channel_layout.base_channel_count;
172 } else if (downmix_id_selected == ID_FOR_ANY_DOWNMIX) {
173 channel_count = audio_num_chan;
174 } else {
175 for (n = 0; n < drc_config->dwnmix_instructions_count; n++) {
176 if (drc_config->dwnmix_instructions[n].downmix_id == downmix_id_selected)
177 break;
178 }
179 if (n == drc_config->dwnmix_instructions_count) {
180 return (UNEXPECTED_ERROR);
181 }
182 channel_count = drc_config->dwnmix_instructions[n].target_channel_count;
183
184 downmix_instructions_selected = n;
185 }
186 drc_instructions_uni_drc->audio_num_chan = channel_count;
187
188 if (drc_instructions_uni_drc->drc_set_id <= 0) {
189 drc_coefficients_selected = 0;
190 } else {
191 err = impd_select_drc_coefficients(drc_config, &drc_coefficients_uni_drc,
192 &drc_coefficients_selected);
193 if (err) return err;
194 }
195
196 ia_drc_params_struct->sel_drc_array[ia_drc_params_struct->drc_set_counter]
197 .drc_instructions_index = drc_instructions_selected;
198 ia_drc_params_struct->sel_drc_array[ia_drc_params_struct->drc_set_counter]
199 .dwnmix_instructions_index = downmix_instructions_selected;
200 ia_drc_params_struct->sel_drc_array[ia_drc_params_struct->drc_set_counter]
201 .drc_coeff_idx = drc_coefficients_selected;
202
203 if ((drc_instructions_uni_drc->downmix_id[0] == ID_FOR_ANY_DOWNMIX) ||
204 (drc_instructions_uni_drc->dwnmix_id_count > 1)) {
205 WORD32 idx = drc_instructions_uni_drc->gain_set_index[0];
206 for (c = 0; c < drc_instructions_uni_drc->audio_num_chan; c++) {
207 drc_instructions_uni_drc->channel_group_of_ch[c] = (idx >= 0) ? 0 : -1;
208 }
209 }
210
211 for (g = 0; g < drc_instructions_uni_drc->num_drc_ch_groups; g++) {
212 drc_instructions_uni_drc->num_chan_per_ch_group[g] = 0;
213 for (c = 0; c < drc_instructions_uni_drc->audio_num_chan; c++) {
214 if (drc_instructions_uni_drc->channel_group_of_ch[c] == g) {
215 drc_instructions_uni_drc->num_chan_per_ch_group[g]++;
216 }
217 }
218 }
219
220 if (drc_instructions_uni_drc->drc_set_effect &
221 (EFFECT_BIT_DUCK_OTHER | EFFECT_BIT_DUCK_SELF)) {
222 drc_instructions_uni_drc->multiband_audio_sig_count =
223 drc_instructions_uni_drc->audio_num_chan;
224 } else {
225 drc_instructions_uni_drc->multiband_audio_sig_count = 0;
226 for (c = 0; c < drc_instructions_uni_drc->audio_num_chan; c++) {
227 g = drc_instructions_uni_drc->channel_group_of_ch[c];
228 if (g < 0) {
229 drc_instructions_uni_drc->multiband_audio_sig_count++;
230 } else {
231 drc_instructions_uni_drc->multiband_audio_sig_count +=
232 drc_instructions_uni_drc->band_count_of_ch_group[g];
233 }
234 }
235 }
236
237 for (g = 0; g < drc_instructions_uni_drc->num_drc_ch_groups; g++) {
238 if (g == 0) {
239 drc_instructions_uni_drc->parametric_drc_look_ahead_samples_max = 0;
240 }
241 if (drc_instructions_uni_drc->ch_group_parametric_drc_flag[g] == 0) {
242 if (drc_instructions_uni_drc->band_count_of_ch_group[g] > 1) {
243 if (ia_drc_params_struct->multiband_sel_drc_idx != -1) {
244 return (UNEXPECTED_ERROR);
245 }
246 selected_drc_is_multiband = 1;
247 }
248 } else {
249 WORD32 gain_set_index =
250 drc_instructions_uni_drc->gain_set_idx_of_ch_group_parametric_drc[g];
251 WORD32 parametric_drc_id =
252 drc_config->str_drc_config_ext.str_drc_coeff_param_drc
253 .str_parametric_drc_gain_set_params[gain_set_index]
254 .parametric_drc_id;
255 WORD32 parametric_drc_look_ahead_samples = 0;
256 ia_parametric_drc_instructions_struct* str_parametric_drc_instructions;
257
258 for (i = 0;
259 i < drc_config->str_drc_config_ext.parametric_drc_instructions_count;
260 i++) {
261 if (parametric_drc_id ==
262 drc_config->str_drc_config_ext.str_parametric_drc_instructions[i]
263 .parametric_drc_id)
264 break;
265 }
266 if (i ==
267 drc_config->str_drc_config_ext.parametric_drc_instructions_count) {
268 return (UNEXPECTED_ERROR);
269 }
270 str_parametric_drc_instructions =
271 &drc_config->str_drc_config_ext.str_parametric_drc_instructions[i];
272
273 p_parametric_drc_params->parametric_drc_idx
274 [p_parametric_drc_params->parametric_drc_instance_count] = i;
275 p_parametric_drc_params->gain_set_index
276 [p_parametric_drc_params->parametric_drc_instance_count] =
277 gain_set_index;
278 if (drc_instructions_uni_drc->drc_apply_to_dwnmix == 0) {
279 p_parametric_drc_params->dwnmix_id_from_drc_instructions
280 [p_parametric_drc_params->parametric_drc_instance_count] =
281 ID_FOR_BASE_LAYOUT;
282 } else {
283 if (drc_instructions_uni_drc->dwnmix_id_count > 1) {
284 p_parametric_drc_params->dwnmix_id_from_drc_instructions
285 [p_parametric_drc_params->parametric_drc_instance_count] =
286 ID_FOR_ANY_DOWNMIX;
287 } else {
288 p_parametric_drc_params->dwnmix_id_from_drc_instructions
289 [p_parametric_drc_params->parametric_drc_instance_count] =
290 drc_instructions_uni_drc->downmix_id[0];
291 }
292 }
293 p_parametric_drc_params->audio_num_chan = channel_count;
294 for (i = 0; i < p_parametric_drc_params->audio_num_chan; i++) {
295 if (drc_instructions_uni_drc->channel_group_of_ch[i] == g) {
296 p_parametric_drc_params->channel_map
297 [p_parametric_drc_params->parametric_drc_instance_count][i] = 1;
298 } else {
299 p_parametric_drc_params->channel_map
300 [p_parametric_drc_params->parametric_drc_instance_count][i] = 0;
301 }
302 }
303 drc_instructions_uni_drc->parametric_drc_look_ahead_samples[g] = 0;
304 if (str_parametric_drc_instructions->parametric_drc_look_ahead_flag) {
305 parametric_drc_look_ahead_samples = (WORD32)(
306 (FLOAT32)
307 str_parametric_drc_instructions->parametric_drc_look_ahead *
308 (FLOAT32)p_parametric_drc_params->sampling_rate * 0.001f);
309 } else {
310 if (str_parametric_drc_instructions->parametric_drc_type ==
311 PARAM_DRC_TYPE_FF) {
312 parametric_drc_look_ahead_samples = (WORD32)(
313 10.0f * (FLOAT32)p_parametric_drc_params->sampling_rate * 0.001f);
314 } else if (str_parametric_drc_instructions->parametric_drc_type ==
315 PARAM_DRC_TYPE_LIM) {
316 parametric_drc_look_ahead_samples = (WORD32)(
317 5.0f * (FLOAT32)p_parametric_drc_params->sampling_rate * 0.001f);
318 } else {
319 return (UNEXPECTED_ERROR);
320 }
321 }
322 drc_instructions_uni_drc->parametric_drc_look_ahead_samples[g] =
323 parametric_drc_look_ahead_samples;
324 if (drc_instructions_uni_drc->parametric_drc_look_ahead_samples_max <
325 drc_instructions_uni_drc->parametric_drc_look_ahead_samples[g]) {
326 drc_instructions_uni_drc->parametric_drc_look_ahead_samples_max =
327 drc_instructions_uni_drc->parametric_drc_look_ahead_samples[g];
328 }
329 p_parametric_drc_params->parametric_drc_instance_count += 1;
330 selected_drc_is_multiband = 0;
331 }
332 }
333 ia_drc_params_struct->parametric_drc_delay +=
334 drc_instructions_uni_drc->parametric_drc_look_ahead_samples_max;
335
336 if (selected_drc_is_multiband == 1) {
337 ia_drc_params_struct->multiband_sel_drc_idx =
338 ia_drc_params_struct->drc_set_counter;
339 err = impd_init_all_filter_banks(
340 drc_coefficients_uni_drc,
341 &(drc_config->str_drc_instruction_str[drc_instructions_selected]),
342 ia_filter_banks_struct);
343 if (err) return (err);
344
345 impd_init_overlap_weight(
346 drc_coefficients_uni_drc,
347 &(drc_config->str_drc_instruction_str[drc_instructions_selected]),
348 ia_drc_params_struct->sub_band_domain_mode, pstr_overlap_params);
349
350 } else {
351 ia_gain_modifiers_struct* gain_modifiers =
352 drc_config->str_drc_instruction_str->str_gain_modifiers_of_ch_group;
353 for (g = 0; g < drc_instructions_uni_drc->num_drc_ch_groups; g++) {
354 if (gain_modifiers[g].shape_filter_flag == 1) {
355 impd_shape_filt_block_init(
356 &drc_coefficients_uni_drc->str_shape_filter_block_params
357 [gain_modifiers[g].shape_filter_idx],
358 &shape_filter_block[g]);
359 } else {
360 shape_filter_block[g].shape_flter_block_flag = 0;
361 }
362 }
363 }
364
365 ia_drc_params_struct->drc_set_counter++;
366
367 return (0);
368 }
369