• 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 <stdio.h>
21 #include <string.h>
22 #include <math.h>
23 #include "impd_type_def.h"
24 #include "impd_drc_bitbuffer.h"
25 #include "impd_drc_extr_delta_coded_info.h"
26 #include "impd_drc_common.h"
27 #include "impd_drc_struct.h"
28 #include "impd_drc_parser.h"
29 #include "impd_drc_filter_bank.h"
30 #include "impd_drc_rom.h"
31 
impd_parametric_drc_ffwd_init_drc_curve_params(WORD32 drc_characteristic,ia_parametric_drc_type_feed_forward_struct * str_parametric_drc_type_feed_forward)32 static VOID impd_parametric_drc_ffwd_init_drc_curve_params(
33     WORD32 drc_characteristic, ia_parametric_drc_type_feed_forward_struct*
34                                    str_parametric_drc_type_feed_forward) {
35   WORD32* node_level = str_parametric_drc_type_feed_forward->node_level;
36   WORD32* node_gain = str_parametric_drc_type_feed_forward->node_gain;
37 
38   switch (drc_characteristic) {
39     case 7:
40       str_parametric_drc_type_feed_forward->node_count = 5;
41       node_level[0] = -22;
42       node_gain[0] = 6;
43       node_level[1] = -10;
44       node_gain[1] = 0;
45       node_level[2] = 10;
46       node_gain[2] = 0;
47       node_level[3] = 20;
48       node_gain[3] = -5;
49       node_level[4] = 40;
50       node_gain[4] = -24;
51       break;
52     case 8:
53       str_parametric_drc_type_feed_forward->node_count = 5;
54       node_level[0] = -12;
55       node_gain[0] = 6;
56       node_level[1] = 0;
57       node_gain[1] = 0;
58       node_level[2] = 5;
59       node_gain[2] = 0;
60       node_level[3] = 15;
61       node_gain[3] = -5;
62       node_level[4] = 35;
63       node_gain[4] = -24;
64       break;
65     case 9:
66       str_parametric_drc_type_feed_forward->node_count = 4;
67       node_level[0] = -34;
68       node_gain[0] = 12;
69       node_level[1] = -10;
70       node_gain[1] = 0;
71       node_level[2] = 10;
72       node_gain[2] = 0;
73       node_level[3] = 40;
74       node_gain[3] = -15;
75       break;
76     case 10:
77       str_parametric_drc_type_feed_forward->node_count = 5;
78       node_level[0] = -24;
79       node_gain[0] = 12;
80       node_level[1] = 0;
81       node_gain[1] = 0;
82       node_level[2] = 5;
83       node_gain[2] = 0;
84       node_level[3] = 15;
85       node_gain[3] = -5;
86       node_level[4] = 35;
87       node_gain[4] = -24;
88       break;
89     case 11:
90       str_parametric_drc_type_feed_forward->node_count = 5;
91       node_level[0] = -19;
92       node_gain[0] = 15;
93       node_level[1] = 0;
94       node_gain[1] = 0;
95       node_level[2] = 5;
96       node_gain[2] = 0;
97       node_level[3] = 15;
98       node_gain[3] = -5;
99       node_level[4] = 35;
100       node_gain[4] = -24;
101       break;
102     default:
103       str_parametric_drc_type_feed_forward->disable_paramteric_drc = 1;
104   }
105 
106   return;
107 }
108 
impd_parametric_drc_ffwd_init_drc_gain_smooth_params(WORD32 drc_characteristic,ia_parametric_drc_type_feed_forward_struct * str_parametric_drc_type_feed_forward)109 static VOID impd_parametric_drc_ffwd_init_drc_gain_smooth_params(
110     WORD32 drc_characteristic, ia_parametric_drc_type_feed_forward_struct*
111                                    str_parametric_drc_type_feed_forward) {
112   str_parametric_drc_type_feed_forward->gain_smooth_attack_time_slow = 100;
113   str_parametric_drc_type_feed_forward->gain_smooth_time_fast_present = 1;
114   str_parametric_drc_type_feed_forward->gain_smooth_attack_time_fast = 10;
115   str_parametric_drc_type_feed_forward->gain_smooth_threshold_present = 1;
116   str_parametric_drc_type_feed_forward->gain_smooth_hold_off_count_present = 1;
117   str_parametric_drc_type_feed_forward->gain_smooth_hold_off = 10;
118 
119   switch (drc_characteristic) {
120     case 7:
121     case 8:
122     case 9:
123       str_parametric_drc_type_feed_forward->gain_smooth_release_time_slow =
124           3000;
125       str_parametric_drc_type_feed_forward->gain_smooth_release_time_fast =
126           1000;
127       str_parametric_drc_type_feed_forward->gain_smooth_attack_threshold = 15;
128       str_parametric_drc_type_feed_forward->gain_smooth_rel_threshold = 20;
129       break;
130     case 10:
131       str_parametric_drc_type_feed_forward->gain_smooth_release_time_slow =
132           10000;
133       str_parametric_drc_type_feed_forward->gain_smooth_release_time_fast =
134           1000;
135       str_parametric_drc_type_feed_forward->gain_smooth_attack_threshold = 15;
136       str_parametric_drc_type_feed_forward->gain_smooth_rel_threshold = 20;
137       break;
138     case 11:
139       str_parametric_drc_type_feed_forward->gain_smooth_release_time_slow =
140           1000;
141       str_parametric_drc_type_feed_forward->gain_smooth_release_time_fast = 200;
142       str_parametric_drc_type_feed_forward->gain_smooth_attack_threshold = 10;
143       str_parametric_drc_type_feed_forward->gain_smooth_rel_threshold = 10;
144       break;
145     default:
146       str_parametric_drc_type_feed_forward->gain_smooth_release_time_slow =
147           3000;
148       str_parametric_drc_type_feed_forward->gain_smooth_release_time_fast =
149           1000;
150       str_parametric_drc_type_feed_forward->gain_smooth_attack_threshold = 15;
151       str_parametric_drc_type_feed_forward->gain_smooth_rel_threshold = 20;
152       break;
153   }
154 
155   return;
156 }
157 
impd_parse_parametric_drc_ffwd(ia_bit_buf_struct * it_bit_buff,WORD32 parametric_drc_frame_size,ia_parametric_drc_type_feed_forward_struct * str_parametric_drc_type_feed_forward)158 static WORD32 impd_parse_parametric_drc_ffwd(
159     ia_bit_buf_struct* it_bit_buff, WORD32 parametric_drc_frame_size,
160     ia_parametric_drc_type_feed_forward_struct*
161         str_parametric_drc_type_feed_forward) {
162   WORD32 i = 0, tmp = 0;
163   // WORD32 err = 0;
164 
165   str_parametric_drc_type_feed_forward->disable_paramteric_drc = 0;
166 
167   tmp = impd_read_bits_buf(it_bit_buff, 3);
168   if (it_bit_buff->error) return it_bit_buff->error;
169 
170   str_parametric_drc_type_feed_forward->level_estim_k_weighting_type =
171       (tmp >> 1) & 3;
172   str_parametric_drc_type_feed_forward->level_estim_integration_time_present =
173       tmp & 1;
174 
175   if (str_parametric_drc_type_feed_forward
176           ->level_estim_integration_time_present) {
177     tmp = impd_read_bits_buf(it_bit_buff, 6);
178     if (it_bit_buff->error) return it_bit_buff->error;
179     str_parametric_drc_type_feed_forward->level_estim_integration_time =
180         (tmp + 1) * parametric_drc_frame_size;
181   } else {
182     str_parametric_drc_type_feed_forward->level_estim_integration_time =
183         parametric_drc_frame_size;
184   }
185 
186   str_parametric_drc_type_feed_forward->drc_curve_definition_type =
187       impd_read_bits_buf(it_bit_buff, 1);
188   if (it_bit_buff->error) return it_bit_buff->error;
189 
190   if (str_parametric_drc_type_feed_forward->drc_curve_definition_type == 0) {
191     str_parametric_drc_type_feed_forward->drc_characteristic =
192         impd_read_bits_buf(it_bit_buff, 7);
193     if (it_bit_buff->error) return it_bit_buff->error;
194     impd_parametric_drc_ffwd_init_drc_curve_params(
195         str_parametric_drc_type_feed_forward->drc_characteristic,
196         str_parametric_drc_type_feed_forward);
197   } else {
198     str_parametric_drc_type_feed_forward->drc_characteristic = 0;
199 
200     tmp = impd_read_bits_buf(it_bit_buff, 15);
201     if (it_bit_buff->error) return it_bit_buff->error;
202 
203     str_parametric_drc_type_feed_forward->node_count = ((tmp >> 12) & 3) + 2;
204     str_parametric_drc_type_feed_forward->node_level[0] =
205         -11 - ((tmp >> 6) & 0x3f);
206     str_parametric_drc_type_feed_forward->node_gain[0] = (tmp & 0x3f) - 39;
207 
208     for (i = 1; i < str_parametric_drc_type_feed_forward->node_count; i++) {
209       tmp = impd_read_bits_buf(it_bit_buff, 11);
210       if (it_bit_buff->error) return it_bit_buff->error;
211 
212       str_parametric_drc_type_feed_forward->node_level[i] =
213           str_parametric_drc_type_feed_forward->node_level[i - 1] + 1 +
214           ((tmp >> 6) & 0x1f);
215       str_parametric_drc_type_feed_forward->node_gain[i] = (tmp & 0x3f) - 39;
216     }
217   }
218 
219   impd_parametric_drc_ffwd_init_drc_gain_smooth_params(
220       str_parametric_drc_type_feed_forward->drc_characteristic,
221       str_parametric_drc_type_feed_forward);
222 
223   str_parametric_drc_type_feed_forward->drc_gain_smooth_parameters_present =
224       impd_read_bits_buf(it_bit_buff, 1);
225   if (it_bit_buff->error) return it_bit_buff->error;
226   if (str_parametric_drc_type_feed_forward
227           ->drc_gain_smooth_parameters_present) {
228     tmp = impd_read_bits_buf(it_bit_buff, 17);
229     if (it_bit_buff->error) return it_bit_buff->error;
230 
231     str_parametric_drc_type_feed_forward->gain_smooth_attack_time_slow =
232         ((tmp >> 9) & 0xff) * 5;
233     str_parametric_drc_type_feed_forward->gain_smooth_release_time_slow =
234         ((tmp >> 1) & 0xff) * 40;
235     str_parametric_drc_type_feed_forward->gain_smooth_time_fast_present =
236         tmp & 1;
237 
238     if (str_parametric_drc_type_feed_forward->gain_smooth_time_fast_present) {
239       tmp = impd_read_bits_buf(it_bit_buff, 17);
240       if (it_bit_buff->error) return it_bit_buff->error;
241 
242       str_parametric_drc_type_feed_forward->gain_smooth_attack_time_fast =
243           ((tmp >> 9) & 0xff) * 5;
244       str_parametric_drc_type_feed_forward->gain_smooth_release_time_fast =
245           ((tmp >> 1) & 0xff) * 20;
246       str_parametric_drc_type_feed_forward->gain_smooth_threshold_present =
247           tmp & 1;
248 
249       if (str_parametric_drc_type_feed_forward->gain_smooth_threshold_present) {
250         str_parametric_drc_type_feed_forward->gain_smooth_attack_threshold =
251             impd_read_bits_buf(it_bit_buff, 5);
252         if (it_bit_buff->error) return it_bit_buff->error;
253         if (str_parametric_drc_type_feed_forward
254                 ->gain_smooth_attack_threshold == 31) {
255           str_parametric_drc_type_feed_forward->gain_smooth_attack_threshold =
256               1000;
257         }
258 
259         str_parametric_drc_type_feed_forward->gain_smooth_rel_threshold =
260             impd_read_bits_buf(it_bit_buff, 5);
261         if (it_bit_buff->error) return it_bit_buff->error;
262         if (str_parametric_drc_type_feed_forward->gain_smooth_rel_threshold ==
263             31) {
264           str_parametric_drc_type_feed_forward->gain_smooth_rel_threshold =
265               1000;
266         }
267       }
268     } else {
269       str_parametric_drc_type_feed_forward->gain_smooth_attack_time_fast =
270           str_parametric_drc_type_feed_forward->gain_smooth_attack_time_slow;
271       str_parametric_drc_type_feed_forward->gain_smooth_release_time_fast =
272           str_parametric_drc_type_feed_forward->gain_smooth_release_time_slow;
273     }
274 
275     str_parametric_drc_type_feed_forward->gain_smooth_hold_off_count_present =
276         impd_read_bits_buf(it_bit_buff, 1);
277     if (it_bit_buff->error) return it_bit_buff->error;
278 
279     if (str_parametric_drc_type_feed_forward
280             ->gain_smooth_hold_off_count_present) {
281       str_parametric_drc_type_feed_forward->gain_smooth_hold_off =
282           impd_read_bits_buf(it_bit_buff, 7);
283       if (it_bit_buff->error) return it_bit_buff->error;
284     }
285   }
286   return 0;
287 }
288 
impd_parse_parametric_drc_lim(ia_bit_buf_struct * it_bit_buff,ia_parametric_drc_lim_struct * parametric_drc_lim)289 static WORD32 impd_parse_parametric_drc_lim(
290     ia_bit_buf_struct* it_bit_buff,
291     ia_parametric_drc_lim_struct* parametric_drc_lim) {
292   // WORD32 err = 0;
293   WORD32 tmp = 0;
294 
295   parametric_drc_lim->disable_paramteric_drc = 0;
296 
297   parametric_drc_lim->parametric_lim_threshold_present =
298       impd_read_bits_buf(it_bit_buff, 1);
299   if (it_bit_buff->error) return it_bit_buff->error;
300 
301   if (parametric_drc_lim->parametric_lim_threshold_present) {
302     tmp = impd_read_bits_buf(it_bit_buff, 8);
303     if (it_bit_buff->error) return it_bit_buff->error;
304     parametric_drc_lim->parametric_lim_threshold = -tmp * 0.125f;
305   } else {
306     parametric_drc_lim->parametric_lim_threshold =
307         PARAM_DRC_TYPE_LIM_THRESHOLD_DEFAULT;
308   }
309 
310   parametric_drc_lim->parametric_lim_release_present =
311       impd_read_bits_buf(it_bit_buff, 1);
312   if (it_bit_buff->error) return it_bit_buff->error;
313   if (parametric_drc_lim->parametric_lim_release_present) {
314     tmp = impd_read_bits_buf(it_bit_buff, 8);
315     if (it_bit_buff->error) return it_bit_buff->error;
316     parametric_drc_lim->parametric_lim_release = tmp * 10;
317   } else {
318     parametric_drc_lim->parametric_lim_release =
319         PARAM_DRC_TYPE_LIM_RELEASE_DEFAULT;
320   }
321 
322   parametric_drc_lim->parametric_lim_attack = PARAM_DRC_TYPE_LIM_ATTACK_DEFAULT;
323   parametric_drc_lim->drc_characteristic = 0;
324 
325   return 0;
326 }
327 
328 WORD32
impd_parametric_drc_parse_gain_set_params(ia_bit_buf_struct * it_bit_buff,ia_drc_config * drc_config,ia_parametric_drc_gain_set_params_struct * str_parametric_drc_gain_set_params)329 impd_parametric_drc_parse_gain_set_params(
330     ia_bit_buf_struct* it_bit_buff, ia_drc_config* drc_config,
331     ia_parametric_drc_gain_set_params_struct*
332         str_parametric_drc_gain_set_params) {
333   WORD32 i = 0, bsDrcInputLoudness = 0, bs_channel_weight = 0, temp;
334   // WORD32 err = 0;
335   temp = impd_read_bits_buf(it_bit_buff, 7);
336   if (it_bit_buff->error) return it_bit_buff->error;
337 
338   str_parametric_drc_gain_set_params->parametric_drc_id = (temp >> 3) & 0xf;
339   str_parametric_drc_gain_set_params->side_chain_config_type = temp & 7;
340 
341   if (str_parametric_drc_gain_set_params->side_chain_config_type == 1) {
342     temp = impd_read_bits_buf(it_bit_buff, 8);
343     if (it_bit_buff->error) return it_bit_buff->error;
344 
345     str_parametric_drc_gain_set_params->downmix_id = (temp >> 1) & 0x7f;
346     str_parametric_drc_gain_set_params->level_estim_channel_weight_format =
347         temp & 1;
348 
349     if (str_parametric_drc_gain_set_params->downmix_id == ID_FOR_BASE_LAYOUT) {
350       str_parametric_drc_gain_set_params->ch_count_from_dwnmix_id =
351           drc_config->channel_layout.base_channel_count;
352     } else if (str_parametric_drc_gain_set_params->downmix_id ==
353                ID_FOR_ANY_DOWNMIX) {
354       str_parametric_drc_gain_set_params->ch_count_from_dwnmix_id = 1;
355     } else {
356       for (i = 0; i < drc_config->dwnmix_instructions_count; i++) {
357         if (str_parametric_drc_gain_set_params->downmix_id ==
358             drc_config->dwnmix_instructions[i].downmix_id)
359           break;
360       }
361       if (i == drc_config->dwnmix_instructions_count) {
362         /* dwnmix_instructions not found */
363         return (UNEXPECTED_ERROR);
364       }
365       str_parametric_drc_gain_set_params->ch_count_from_dwnmix_id =
366           drc_config->dwnmix_instructions[i].target_channel_count;
367     }
368 
369     for (i = 0; i < str_parametric_drc_gain_set_params->ch_count_from_dwnmix_id;
370          i++) {
371       if (str_parametric_drc_gain_set_params
372               ->level_estim_channel_weight_format == 0) {
373         str_parametric_drc_gain_set_params->level_estim_ch_weight[i] =
374             (FLOAT32)impd_read_bits_buf(it_bit_buff, 1);
375         if (it_bit_buff->error) return it_bit_buff->error;
376       } else {
377         bs_channel_weight = impd_read_bits_buf(it_bit_buff, 4);
378         if (it_bit_buff->error) return it_bit_buff->error;
379         str_parametric_drc_gain_set_params->level_estim_ch_weight[i] =
380             (FLOAT32)pow(10.0f, 0.05f * channel_weight[bs_channel_weight]);
381       }
382     }
383   } else {
384     str_parametric_drc_gain_set_params->downmix_id = 0;
385     str_parametric_drc_gain_set_params->ch_count_from_dwnmix_id = 0;
386   }
387 
388   str_parametric_drc_gain_set_params->drc_input_loudness_present =
389       impd_read_bits_buf(it_bit_buff, 1);
390   if (it_bit_buff->error) return it_bit_buff->error;
391 
392   if (str_parametric_drc_gain_set_params->drc_input_loudness_present) {
393     bsDrcInputLoudness = impd_read_bits_buf(it_bit_buff, 8);
394     if (it_bit_buff->error) return it_bit_buff->error;
395     str_parametric_drc_gain_set_params->drc_input_loudness =
396         -57.75f + bsDrcInputLoudness * 0.25f;
397   }
398 
399   return 0;
400 }
401 
impd_parametric_drc_gen_virtual_gain_sets(ia_drc_config * drc_config)402 static WORD32 impd_parametric_drc_gen_virtual_gain_sets(
403     ia_drc_config* drc_config) {
404   WORD32 i = 0, j = 0, c1 = -1, c0 = -1, parametric_drc_id = 0,
405          drc_characteristic = 0;
406   ia_uni_drc_coeffs_struct* str_p_loc_drc_coefficients_uni_drc;
407   ia_parametric_drc_instructions_struct* str_parametric_drc_instructions;
408   ia_drc_coeff_parametric_drc_struct* str_drc_coeff_param_drc =
409       &(drc_config->str_drc_config_ext.str_drc_coeff_param_drc);
410 
411   for (i = 0; i < drc_config->drc_coefficients_drc_count; i++) {
412     if (drc_config->str_p_loc_drc_coefficients_uni_drc[i].drc_location ==
413         str_drc_coeff_param_drc->drc_location) {
414       if (drc_config->str_p_loc_drc_coefficients_uni_drc[i].version == 0) {
415         c0 = i;
416       } else {
417         c1 = i;
418       }
419     }
420   }
421   if (c1 >= 0) {
422     str_p_loc_drc_coefficients_uni_drc =
423         &(drc_config->str_p_loc_drc_coefficients_uni_drc[c1]);
424   } else if (c0 >= 0) {
425     str_p_loc_drc_coefficients_uni_drc =
426         &(drc_config->str_p_loc_drc_coefficients_uni_drc[c0]);
427   } else {
428     str_p_loc_drc_coefficients_uni_drc =
429         &drc_config->str_p_loc_drc_coefficients_uni_drc
430              [drc_config->drc_coefficients_drc_count];
431 
432     str_p_loc_drc_coefficients_uni_drc->version = 1;
433     str_p_loc_drc_coefficients_uni_drc->drc_location =
434         str_drc_coeff_param_drc->drc_location;
435     str_p_loc_drc_coefficients_uni_drc->drc_frame_size_present = 0;
436 
437     str_p_loc_drc_coefficients_uni_drc->gain_set_count = 0;
438     str_p_loc_drc_coefficients_uni_drc->gain_set_count_plus = 0;
439 
440     str_p_loc_drc_coefficients_uni_drc->drc_characteristic_left_present = 0;
441     str_p_loc_drc_coefficients_uni_drc->drc_characteristic_right_present = 0;
442     str_p_loc_drc_coefficients_uni_drc->shape_filters_present = 0;
443     str_p_loc_drc_coefficients_uni_drc->gain_sequence_count = 0;
444     drc_config->drc_coefficients_drc_count += 1;
445   }
446   {
447     WORD32 tmp = str_p_loc_drc_coefficients_uni_drc->gain_set_count +
448                  str_drc_coeff_param_drc->parametric_drc_gain_set_count;
449     if (tmp > GAIN_SET_COUNT_MAX) return UNEXPECTED_ERROR;
450     str_p_loc_drc_coefficients_uni_drc->gain_set_count_plus = tmp;
451   }
452 
453   for (i = str_p_loc_drc_coefficients_uni_drc->gain_set_count;
454        i < str_p_loc_drc_coefficients_uni_drc->gain_set_count_plus; i++) {
455     str_p_loc_drc_coefficients_uni_drc->gain_set_params[i].band_count = 1;
456 
457     parametric_drc_id =
458         drc_config->str_drc_config_ext.str_drc_coeff_param_drc
459             .str_parametric_drc_gain_set_params
460                 [i - str_p_loc_drc_coefficients_uni_drc->gain_set_count]
461             .parametric_drc_id;
462 
463     for (j = 0;
464          j < drc_config->str_drc_config_ext.parametric_drc_instructions_count;
465          j++) {
466       if (parametric_drc_id ==
467           drc_config->str_drc_config_ext.str_parametric_drc_instructions[j]
468               .parametric_drc_id)
469         break;
470     }
471     if (j == drc_config->str_drc_config_ext.parametric_drc_instructions_count) {
472       /* str_parametric_drc_instructions not found */
473       return (UNEXPECTED_ERROR);
474     }
475     str_parametric_drc_instructions =
476         &drc_config->str_drc_config_ext.str_parametric_drc_instructions[j];
477 
478     drc_characteristic = 0;
479     if (str_parametric_drc_instructions->parametric_drc_preset_id_present) {
480       drc_characteristic = str_parametric_drc_instructions->drc_characteristic;
481     } else if (str_parametric_drc_instructions->parametric_drc_type ==
482                PARAM_DRC_TYPE_FF) {
483       if (str_parametric_drc_instructions->str_parametric_drc_type_feed_forward
484               .drc_curve_definition_type == 0) {
485         drc_characteristic =
486             str_parametric_drc_instructions
487                 ->str_parametric_drc_type_feed_forward.drc_characteristic;
488       }
489     }
490     if (drc_characteristic != 0) {
491       str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
492           .gain_params[0]
493           .drc_characteristic_present = 1;
494       str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
495           .gain_params[0]
496           .drc_characteristic_format_is_cicp = 1;
497       str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
498           .gain_params[0]
499           .drc_characteristic = drc_characteristic;
500     } else {
501       str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
502           .gain_params[0]
503           .drc_characteristic_present = 0;
504       str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
505           .gain_params[0]
506           .drc_characteristic_format_is_cicp = 0;
507       str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
508           .gain_params[0]
509           .drc_characteristic = 0;
510     }
511   }
512 
513   return 0;
514 }
515 
impd_parametic_drc_parse_coeff(ia_bit_buf_struct * it_bit_buff,ia_drc_config * drc_config,ia_drc_coeff_parametric_drc_struct * str_drc_coeff_param_drc)516 static WORD32 impd_parametic_drc_parse_coeff(
517     ia_bit_buf_struct* it_bit_buff, ia_drc_config* drc_config,
518     ia_drc_coeff_parametric_drc_struct* str_drc_coeff_param_drc) {
519   WORD32 i = 0, err = 0, code = 0, mu = 0, nu = 0, temp;
520 
521   temp = impd_read_bits_buf(it_bit_buff, 5);
522   if (it_bit_buff->error) return it_bit_buff->error;
523 
524   str_drc_coeff_param_drc->drc_location = (temp >> 1) & 0xf;
525   if (str_drc_coeff_param_drc->drc_location < 1 ||
526       str_drc_coeff_param_drc->drc_location > 4)
527     return UNEXPECTED_ERROR;
528   str_drc_coeff_param_drc->parametric_drc_frame_size_format = temp & 1;
529 
530   if (str_drc_coeff_param_drc->parametric_drc_frame_size_format) {
531     code = impd_read_bits_buf(it_bit_buff, 15);
532     if (it_bit_buff->error) return it_bit_buff->error;
533     str_drc_coeff_param_drc->parametric_drc_frame_size = code + 1;
534   } else {
535     code = impd_read_bits_buf(it_bit_buff, 4);
536     if (it_bit_buff->error) return it_bit_buff->error;
537     str_drc_coeff_param_drc->parametric_drc_frame_size = 1 << code;
538   }
539 
540   str_drc_coeff_param_drc->parametric_drc_delay_max_present =
541       impd_read_bits_buf(it_bit_buff, 1);
542   if (it_bit_buff->error) return it_bit_buff->error;
543   if (str_drc_coeff_param_drc->parametric_drc_delay_max_present) {
544     temp = impd_read_bits_buf(it_bit_buff, 8);
545     if (it_bit_buff->error) return it_bit_buff->error;
546 
547     mu = (temp >> 3) & 0x1f;
548     nu = temp & 3;
549 
550     str_drc_coeff_param_drc->parametric_drc_delay_max = 16 * mu * (1 << nu);
551   }
552 
553   temp = impd_read_bits_buf(it_bit_buff, 7);
554   if (it_bit_buff->error) return it_bit_buff->error;
555 
556   str_drc_coeff_param_drc->reset_parametric_drc = (temp >> 6) & 1;
557   str_drc_coeff_param_drc->parametric_drc_gain_set_count = temp & 0x3f;
558 
559   if (str_drc_coeff_param_drc->parametric_drc_gain_set_count >
560       SEQUENCE_COUNT_MAX)
561     return (UNEXPECTED_ERROR);
562 
563   for (i = 0; i < str_drc_coeff_param_drc->parametric_drc_gain_set_count; i++) {
564     err = impd_parametric_drc_parse_gain_set_params(
565         it_bit_buff, drc_config,
566         &(str_drc_coeff_param_drc->str_parametric_drc_gain_set_params[i]));
567     if (err) return (err);
568   }
569 
570   return 0;
571 }
572 
impd_parse_parametric_drc_instructions(ia_bit_buf_struct * it_bit_buff,WORD32 parametric_drc_frame_size,ia_parametric_drc_instructions_struct * str_parametric_drc_instructions)573 static WORD32 impd_parse_parametric_drc_instructions(
574     ia_bit_buf_struct* it_bit_buff, WORD32 parametric_drc_frame_size,
575     ia_parametric_drc_instructions_struct* str_parametric_drc_instructions) {
576   WORD32 i = 0, err = 0, temp;
577   WORD32 bit_size_len, bit_size, other_bit;
578 
579   str_parametric_drc_instructions->drc_characteristic = 0;
580   str_parametric_drc_instructions->disable_paramteric_drc = 0;
581 
582   temp = impd_read_bits_buf(it_bit_buff, 5);
583   if (it_bit_buff->error) return it_bit_buff->error;
584 
585   str_parametric_drc_instructions->parametric_drc_id = (temp >> 1) & 0xf;
586   str_parametric_drc_instructions->parametric_drc_look_ahead_flag = temp & 1;
587 
588   if (str_parametric_drc_instructions->parametric_drc_look_ahead_flag) {
589     str_parametric_drc_instructions->parametric_drc_look_ahead =
590         impd_read_bits_buf(it_bit_buff, 7);
591     if (it_bit_buff->error) return it_bit_buff->error;
592   } else {
593     str_parametric_drc_instructions->parametric_drc_look_ahead = 0;
594   }
595 
596   str_parametric_drc_instructions->parametric_drc_preset_id_present =
597       impd_read_bits_buf(it_bit_buff, 1);
598   if (it_bit_buff->error) return it_bit_buff->error;
599 
600   if (str_parametric_drc_instructions->parametric_drc_preset_id_present) {
601     str_parametric_drc_instructions->parametric_drc_preset_id =
602         impd_read_bits_buf(it_bit_buff, 7);
603     if (it_bit_buff->error) return it_bit_buff->error;
604 
605     switch (str_parametric_drc_instructions->parametric_drc_preset_id) {
606       case 0:
607       case 1:
608       case 2:
609       case 3:
610       case 4:
611         str_parametric_drc_instructions->drc_characteristic =
612             str_parametric_drc_instructions->parametric_drc_preset_id + 7;
613         str_parametric_drc_instructions->parametric_drc_type =
614             PARAM_DRC_TYPE_FF;
615 
616         str_parametric_drc_instructions->str_parametric_drc_type_feed_forward
617             .level_estim_k_weighting_type = 2;
618         str_parametric_drc_instructions->str_parametric_drc_type_feed_forward
619             .level_estim_integration_time = parametric_drc_frame_size;
620 
621         impd_parametric_drc_ffwd_init_drc_curve_params(
622             str_parametric_drc_instructions->drc_characteristic,
623             &str_parametric_drc_instructions
624                  ->str_parametric_drc_type_feed_forward);
625         impd_parametric_drc_ffwd_init_drc_gain_smooth_params(
626             str_parametric_drc_instructions->drc_characteristic,
627             &str_parametric_drc_instructions
628                  ->str_parametric_drc_type_feed_forward);
629 
630         break;
631       default:
632         str_parametric_drc_instructions->disable_paramteric_drc = 1;
633         break;
634     }
635   } else {
636     str_parametric_drc_instructions->parametric_drc_type =
637         impd_read_bits_buf(it_bit_buff, 3);
638     if (it_bit_buff->error) return it_bit_buff->error;
639 
640     if (str_parametric_drc_instructions->parametric_drc_type ==
641         PARAM_DRC_TYPE_FF) {
642       err = impd_parse_parametric_drc_ffwd(
643           it_bit_buff, parametric_drc_frame_size,
644           &(str_parametric_drc_instructions
645                 ->str_parametric_drc_type_feed_forward));
646       if (err) return (err);
647       str_parametric_drc_instructions->disable_paramteric_drc =
648           str_parametric_drc_instructions->str_parametric_drc_type_feed_forward
649               .disable_paramteric_drc;
650       str_parametric_drc_instructions->drc_characteristic =
651           str_parametric_drc_instructions->str_parametric_drc_type_feed_forward
652               .drc_characteristic;
653     } else if (str_parametric_drc_instructions->parametric_drc_type ==
654                PARAM_DRC_TYPE_LIM) {
655       err = impd_parse_parametric_drc_lim(
656           it_bit_buff, &(str_parametric_drc_instructions->parametric_drc_lim));
657       if (err) return (err);
658       str_parametric_drc_instructions->disable_paramteric_drc =
659           str_parametric_drc_instructions->parametric_drc_lim
660               .disable_paramteric_drc;
661       str_parametric_drc_instructions->drc_characteristic =
662           str_parametric_drc_instructions->parametric_drc_lim
663               .drc_characteristic;
664       if (str_parametric_drc_instructions->parametric_drc_look_ahead_flag) {
665         str_parametric_drc_instructions->parametric_drc_lim
666             .parametric_lim_attack =
667             str_parametric_drc_instructions->parametric_drc_look_ahead;
668       }
669     } else {
670       bit_size_len = impd_read_bits_buf(it_bit_buff, 3) + 4;
671       if (it_bit_buff->error) return it_bit_buff->error;
672 
673       bit_size = impd_read_bits_buf(it_bit_buff, bit_size_len);
674       if (it_bit_buff->error) return it_bit_buff->error;
675       str_parametric_drc_instructions->len_bit_size = bit_size + 1;
676 
677       switch (str_parametric_drc_instructions->parametric_drc_type) {
678         default:
679           str_parametric_drc_instructions->disable_paramteric_drc = 1;
680           for (i = 0; i < str_parametric_drc_instructions->len_bit_size; i++) {
681             other_bit = impd_read_bits_buf(it_bit_buff, 1);
682             if (it_bit_buff->error) return it_bit_buff->error;
683           }
684           break;
685       }
686     }
687   }
688 
689   return 0;
690 }
691 
impd_parse_loud_info_set_ext_eq(ia_bit_buf_struct * it_bit_buff,ia_drc_loudness_info_set_struct * loudness_info_set)692 WORD32 impd_parse_loud_info_set_ext_eq(
693     ia_bit_buf_struct* it_bit_buff,
694     ia_drc_loudness_info_set_struct* loudness_info_set) {
695   WORD32 err, i, offset, version = 1, temp;
696   WORD32 loudness_info_v1_album_cnt, loudness_info_v1_cnt;
697 
698   temp = impd_read_bits_buf(it_bit_buff, 12);
699   if (it_bit_buff->error) return it_bit_buff->error;
700 
701   loudness_info_v1_album_cnt = (temp >> 6) & 0x3f;
702   loudness_info_v1_cnt = temp & 0x3f;
703 
704   offset = loudness_info_set->loudness_info_album_count;
705   loudness_info_set->loudness_info_album_count += loudness_info_v1_album_cnt;
706   if ((offset + loudness_info_v1_album_cnt) > LOUDNESS_INFO_COUNT_MAX)
707     return (UNEXPECTED_ERROR);
708   for (i = 0; i < loudness_info_v1_album_cnt; i++) {
709     err = impd_parse_loudness_info(
710         it_bit_buff, version,
711         &loudness_info_set->str_loudness_info_album[i + offset]);
712     if (err) return (err);
713   }
714   offset = loudness_info_set->loudness_info_count;
715   loudness_info_set->loudness_info_count += loudness_info_v1_cnt;
716   if (offset + loudness_info_v1_cnt > LOUDNESS_INFO_COUNT_MAX)
717     return (UNEXPECTED_ERROR);
718   for (i = 0; i < loudness_info_v1_cnt; i++) {
719     err = impd_parse_loudness_info(
720         it_bit_buff, version, &loudness_info_set->loudness_info[i + offset]);
721     if (err) return (err);
722   }
723   return (0);
724 }
725 
impd_parse_ch_layout(ia_bit_buf_struct * it_bit_buff,ia_drc_params_bs_dec_struct * ia_drc_params_struct,ia_channel_layout_struct * channel_layout)726 WORD32 impd_parse_ch_layout(ia_bit_buf_struct* it_bit_buff,
727                             ia_drc_params_bs_dec_struct* ia_drc_params_struct,
728                             ia_channel_layout_struct* channel_layout) {
729   // WORD32 err = 0;
730   WORD32 i;
731 
732   channel_layout->base_channel_count = impd_read_bits_buf(it_bit_buff, 7);
733   if (it_bit_buff->error) return it_bit_buff->error;
734   if (channel_layout->base_channel_count > MAX_CHANNEL_COUNT)
735     return (UNEXPECTED_ERROR);
736   if (ia_drc_params_struct->lfe_channel_map_count != -1 &&
737       channel_layout->base_channel_count !=
738           ia_drc_params_struct->lfe_channel_map_count) {
739     return (UNEXPECTED_ERROR);
740   }
741   channel_layout->layout_signaling_present = impd_read_bits_buf(it_bit_buff, 1);
742   if (it_bit_buff->error) return it_bit_buff->error;
743 
744   if (channel_layout->layout_signaling_present) {
745     channel_layout->defined_layout = impd_read_bits_buf(it_bit_buff, 8);
746     if (it_bit_buff->error) return it_bit_buff->error;
747 
748     if (channel_layout->defined_layout == 0) {
749       for (i = 0; i < channel_layout->base_channel_count; i++) {
750         channel_layout->speaker_position[i] =
751             impd_read_bits_buf(it_bit_buff, 7);
752         if (it_bit_buff->error) return it_bit_buff->error;
753         if (channel_layout->speaker_position[i] == 3 ||
754             channel_layout->speaker_position[i] == 26) {
755           ia_drc_params_struct->lfe_channel_map[i] = 1;
756         } else {
757           ia_drc_params_struct->lfe_channel_map[i] = 0;
758         }
759       }
760     }
761   }
762 
763   return (0);
764 }
765 
766 WORD32
impd_parse_dwnmix_instructions(ia_bit_buf_struct * it_bit_buff,WORD32 version,ia_drc_params_bs_dec_struct * ia_drc_params_struct,ia_channel_layout_struct * channel_layout,ia_downmix_instructions_struct * dwnmix_instructions)767 impd_parse_dwnmix_instructions(
768     ia_bit_buf_struct* it_bit_buff, WORD32 version,
769     ia_drc_params_bs_dec_struct* ia_drc_params_struct,
770     ia_channel_layout_struct* channel_layout,
771     ia_downmix_instructions_struct* dwnmix_instructions) {
772   // WORD32 err = 0;
773   WORD32 i, j, k, temp;
774 
775   temp = impd_read_bits_buf(it_bit_buff, 23);
776   if (it_bit_buff->error) return it_bit_buff->error;
777 
778   dwnmix_instructions->downmix_id = (temp >> 16) & 0x7f;
779   dwnmix_instructions->target_channel_count = (temp >> 9) & 0x7f;
780   if (dwnmix_instructions->target_channel_count > MAX_CHANNEL_COUNT)
781     return (UNEXPECTED_ERROR);
782   dwnmix_instructions->target_layout = (temp >> 1) & 0xff;
783   dwnmix_instructions->downmix_coefficients_present = temp & 1;
784 
785   if (dwnmix_instructions->downmix_coefficients_present) {
786     if (version == 0) {
787       WORD32 dmix_coeff;
788       k = 0;
789       for (i = 0; i < dwnmix_instructions->target_channel_count; i++) {
790         for (j = 0; j < channel_layout->base_channel_count; j++) {
791           dmix_coeff = impd_read_bits_buf(it_bit_buff, 4);
792           if (it_bit_buff->error) return it_bit_buff->error;
793 
794           if (k >= DOWNMIX_COEFF_COUNT_MAX) return (UNEXPECTED_ERROR);
795           if (ia_drc_params_struct->lfe_channel_map[j]) {
796             dwnmix_instructions->downmix_coefficient[k] =
797                 (FLOAT32)pow(10.0f, 0.05f * dwnmix_coeff_lfe[dmix_coeff]);
798           } else {
799             dwnmix_instructions->downmix_coefficient[k] =
800                 (FLOAT32)pow(10.0f, 0.05f * dwnmix_coeff[dmix_coeff]);
801           }
802           k++;
803         }
804       }
805     } else {
806       WORD32 dmix_coeff_v1, bs_dmix_offset;
807       FLOAT32 a, b, dmix_offset, sum;
808 
809       bs_dmix_offset = impd_read_bits_buf(it_bit_buff, 4);
810       if (it_bit_buff->error) return it_bit_buff->error;
811       k = 0;
812 
813       for (i = 0; i < dwnmix_instructions->target_channel_count; i++) {
814         for (j = 0; j < channel_layout->base_channel_count; j++) {
815           dmix_coeff_v1 = impd_read_bits_buf(it_bit_buff, 5);
816           if (it_bit_buff->error) return it_bit_buff->error;
817           if (k >= DOWNMIX_COEFF_COUNT_MAX) return (UNEXPECTED_ERROR);
818           dwnmix_instructions->downmix_coefficient[k] =
819               dwnmix_coeff_v1[dmix_coeff_v1];
820           k++;
821         }
822       }
823       switch (bs_dmix_offset) {
824         case 0:
825           dmix_offset = 0.0f;
826           break;
827         case 1:
828           a = 20.0f * (FLOAT32)log10(
829                           (FLOAT32)dwnmix_instructions->target_channel_count /
830                           (FLOAT32)channel_layout->base_channel_count);
831           dmix_offset = 0.5f * (FLOAT32)floor(0.5f + a);
832           break;
833         case 2:
834           a = 20.0f * (FLOAT32)log10(
835                           (FLOAT32)dwnmix_instructions->target_channel_count /
836                           (FLOAT32)channel_layout->base_channel_count);
837           dmix_offset = 0.5f * (FLOAT32)floor(0.5f + 2.0f * a);
838           break;
839         case 3:
840           sum = 0.0f;
841           for (k = 0; k < dwnmix_instructions->target_channel_count *
842                               channel_layout->base_channel_count;
843                k++) {
844             sum += (FLOAT32)pow(
845                 10.0f, 0.1f * dwnmix_instructions->downmix_coefficient[k]);
846           }
847           b = 10.0f * (FLOAT32)log10(sum);
848           dmix_offset = 0.5f * (FLOAT32)floor(0.5f + 2.0f * b);
849           break;
850 
851         default:
852           return (BITSTREAM_ERROR);
853           break;
854       }
855       for (k = 0; k < dwnmix_instructions->target_channel_count *
856                           channel_layout->base_channel_count;
857            k++) {
858         dwnmix_instructions->downmix_coefficient[k] = (FLOAT32)pow(
859             10.0f, 0.05f * (dwnmix_instructions->downmix_coefficient[k] +
860                             dmix_offset));
861       }
862     }
863   }
864   return (0);
865 }
866 
impd_drc_gen_instructions_for_drc_off(ia_drc_config * drc_config)867 VOID impd_drc_gen_instructions_for_drc_off(ia_drc_config* drc_config) {
868   WORD32 i, k, s;
869   ia_drc_instructions_struct* str_drc_instruction_str;
870   s = -1;
871 
872   k = drc_config->drc_instructions_uni_drc_count;
873 
874   str_drc_instruction_str = &(drc_config->str_drc_instruction_str[k]);
875   memset(str_drc_instruction_str, 0, sizeof(ia_drc_instructions_struct));
876   str_drc_instruction_str->drc_set_id = s;
877   s--;
878   str_drc_instruction_str->downmix_id[0] = ID_FOR_BASE_LAYOUT;
879   str_drc_instruction_str->dwnmix_id_count = 1;
880   str_drc_instruction_str->drc_apply_to_dwnmix = 0;
881   str_drc_instruction_str->depends_on_drc_set_present = 0;
882   str_drc_instruction_str->no_independent_use = 0;
883   str_drc_instruction_str->gain_element_count = 0;
884   for (i = 1; i < drc_config->dwnmix_instructions_count + 1; i++) {
885     str_drc_instruction_str = &(drc_config->str_drc_instruction_str[k + i]);
886     memset(str_drc_instruction_str, 0, sizeof(ia_drc_instructions_struct));
887     str_drc_instruction_str->drc_set_id = s;
888     s--;
889     str_drc_instruction_str->drc_set_complexity_level = 0;
890     str_drc_instruction_str->requires_eq = 0;
891     str_drc_instruction_str->downmix_id[0] =
892         drc_config->dwnmix_instructions[i - 1].downmix_id;
893     str_drc_instruction_str->dwnmix_id_count = 1;
894     str_drc_instruction_str->drc_apply_to_dwnmix = 0;
895     str_drc_instruction_str->depends_on_drc_set_present = 0;
896     str_drc_instruction_str->no_independent_use = 0;
897     str_drc_instruction_str->gain_element_count = 0;
898   }
899   drc_config->drc_instructions_count_plus =
900       drc_config->drc_instructions_uni_drc_count +
901       drc_config->dwnmix_instructions_count + 1;
902   return;
903 }
904 
905 WORD32
impd_parse_drc_config_ext(ia_bit_buf_struct * it_bit_buff,ia_drc_params_bs_dec_struct * ia_drc_params_struct,ia_drc_config * drc_config,ia_drc_config_ext * str_drc_config_ext)906 impd_parse_drc_config_ext(ia_bit_buf_struct* it_bit_buff,
907                           ia_drc_params_bs_dec_struct* ia_drc_params_struct,
908                           ia_drc_config* drc_config,
909                           ia_drc_config_ext* str_drc_config_ext) {
910   WORD32 err = 0, i, k;
911   WORD32 bit_size_len, ext_size_bits, bit_size, other_bit;
912 
913   k = 0;
914   str_drc_config_ext->drc_config_ext_type[k] =
915       impd_read_bits_buf(it_bit_buff, 4);
916   if (it_bit_buff->error) return it_bit_buff->error;
917   while (str_drc_config_ext->drc_config_ext_type[k] != UNIDRCCONFEXT_TERM) {
918     if (k >= (EXT_COUNT_MAX - 1)) return UNEXPECTED_ERROR;
919     bit_size_len = impd_read_bits_buf(it_bit_buff, 4);
920     if (it_bit_buff->error) return it_bit_buff->error;
921     ext_size_bits = bit_size_len + 4;
922 
923     bit_size = impd_read_bits_buf(it_bit_buff, ext_size_bits);
924     if (it_bit_buff->error) return it_bit_buff->error;
925     str_drc_config_ext->ext_bit_size[k] = bit_size + 1;
926 
927     switch (str_drc_config_ext->drc_config_ext_type[k]) {
928       case UNIDRCCONFEXT_PARAM_DRC:
929         str_drc_config_ext->parametric_drc_present = 1;
930         err = impd_parametic_drc_parse_coeff(
931             it_bit_buff, drc_config,
932             &(str_drc_config_ext->str_drc_coeff_param_drc));
933         if (err) return (err);
934         str_drc_config_ext->parametric_drc_instructions_count =
935             impd_read_bits_buf(it_bit_buff, 4);
936         if (it_bit_buff->error) return it_bit_buff->error;
937         if (str_drc_config_ext->parametric_drc_instructions_count >
938             PARAM_DRC_INSTRUCTIONS_COUNT_MAX)
939           return (UNEXPECTED_ERROR);
940 
941         for (i = 0; i < str_drc_config_ext->parametric_drc_instructions_count;
942              i++) {
943           err = impd_parse_parametric_drc_instructions(
944               it_bit_buff, str_drc_config_ext->str_drc_coeff_param_drc
945                                .parametric_drc_frame_size,
946               &(str_drc_config_ext->str_parametric_drc_instructions[i]));
947           if (err) return (err);
948         }
949         break;
950       case UNIDRCCONFEXT_V1:
951         str_drc_config_ext->drc_extension_v1_present = 1;
952         err = impd_parse_drc_ext_v1(it_bit_buff, ia_drc_params_struct,
953                                     drc_config, str_drc_config_ext);
954         if (err) return (err);
955         break;
956       default:
957         for (i = 0; i < str_drc_config_ext->ext_bit_size[k]; i++) {
958           other_bit = impd_read_bits_buf(it_bit_buff, 1);
959           if (it_bit_buff->error) return it_bit_buff->error;
960         }
961         break;
962     }
963     k++;
964     str_drc_config_ext->drc_config_ext_type[k] =
965         impd_read_bits_buf(it_bit_buff, 4);
966     if (it_bit_buff->error) return it_bit_buff->error;
967   }
968 
969   return (0);
970 }
971 
impd_parse_split_drc_characteristic(ia_bit_buf_struct * it_bit_buff,const WORD32 side,ia_split_drc_characteristic_struct * split_drc_characteristic)972 static WORD32 impd_parse_split_drc_characteristic(
973     ia_bit_buf_struct* it_bit_buff, const WORD32 side,
974     ia_split_drc_characteristic_struct* split_drc_characteristic) {
975   // WORD32 err = 0;
976   WORD32 i, temp;
977 
978   split_drc_characteristic->characteristic_format =
979       impd_read_bits_buf(it_bit_buff, 1);
980   if (it_bit_buff->error) return it_bit_buff->error;
981   if (split_drc_characteristic->characteristic_format == 0) {
982     WORD32 bsGain, bsIoRatio, bsExp;
983     bsGain = impd_read_bits_buf(it_bit_buff, 6);
984     if (it_bit_buff->error) return it_bit_buff->error;
985     if (side == LEFT_SIDE) {
986       split_drc_characteristic->gain = (FLOAT32)bsGain;
987     } else {
988       split_drc_characteristic->gain = (FLOAT32)-bsGain;
989     }
990     temp = impd_read_bits_buf(it_bit_buff, 8);
991     if (it_bit_buff->error) return it_bit_buff->error;
992 
993     bsIoRatio = (temp >> 4) & 0xf;
994     bsExp = temp & 0xf;
995     split_drc_characteristic->in_out_ratio = 0.05f + 0.15f * bsIoRatio;
996 
997     if (bsExp < 15) {
998       split_drc_characteristic->exp = 1.0f + 2.0f * bsExp;
999     } else {
1000       split_drc_characteristic->exp = 1000.0f;
1001     }
1002     split_drc_characteristic->flip_sign = impd_read_bits_buf(it_bit_buff, 1);
1003     if (it_bit_buff->error) return it_bit_buff->error;
1004   } else {
1005     WORD32 char_node_cnt, node_level_delta, node_gain;
1006     char_node_cnt = impd_read_bits_buf(it_bit_buff, 2);
1007     if (it_bit_buff->error) return it_bit_buff->error;
1008     split_drc_characteristic->characteristic_node_count = char_node_cnt + 1;
1009     split_drc_characteristic->node_level[0] = DRC_INPUT_LOUDNESS_TARGET;
1010     split_drc_characteristic->node_gain[0] = 0.0f;
1011     for (i = 1; i <= split_drc_characteristic->characteristic_node_count; i++) {
1012       node_level_delta = impd_read_bits_buf(it_bit_buff, 5);
1013       if (it_bit_buff->error) return it_bit_buff->error;
1014       if (side == LEFT_SIDE) {
1015         split_drc_characteristic->node_level[i] =
1016             split_drc_characteristic->node_level[i - 1] -
1017             (1.0f + node_level_delta);
1018       } else {
1019         split_drc_characteristic->node_level[i] =
1020             split_drc_characteristic->node_level[i - 1] +
1021             (1.0f + node_level_delta);
1022       }
1023       node_gain = impd_read_bits_buf(it_bit_buff, 8);
1024       if (it_bit_buff->error) return it_bit_buff->error;
1025       split_drc_characteristic->node_gain[i] = 0.5f * node_gain - 64.0f;
1026     }
1027   }
1028   return (0);
1029 }
1030 
1031 WORD32
impd_drc_gen_instructions_derived_data(ia_drc_config * drc_config,ia_drc_params_bs_dec_struct * ia_drc_params_struct,ia_drc_instructions_struct * str_drc_instruction_str)1032 impd_drc_gen_instructions_derived_data(
1033     ia_drc_config* drc_config,
1034     ia_drc_params_bs_dec_struct* ia_drc_params_struct,
1035     ia_drc_instructions_struct* str_drc_instruction_str) {
1036   WORD32 n, g;
1037   ia_uni_drc_coeffs_struct* str_p_loc_drc_coefficients_uni_drc = NULL;
1038   ia_drc_coeff_parametric_drc_struct* str_drc_coeff_param_drc = NULL;
1039   WORD32 gain_element_count = 0;
1040 
1041   for (n = 0; n < drc_config->drc_coefficients_drc_count; n++) {
1042     if (drc_config->str_p_loc_drc_coefficients_uni_drc[n].drc_location ==
1043         str_drc_instruction_str->drc_location)
1044       break;
1045   }
1046   if ((n == drc_config->drc_coefficients_drc_count) &&
1047       (drc_config->drc_coefficients_drc_count > 0)) {
1048     return -1;
1049   }
1050   str_p_loc_drc_coefficients_uni_drc =
1051       &(drc_config->str_p_loc_drc_coefficients_uni_drc[n]);
1052 
1053   if (drc_config->drc_config_ext_present &&
1054       drc_config->str_drc_config_ext.parametric_drc_present &&
1055       drc_config->str_drc_config_ext.str_drc_coeff_param_drc.drc_location ==
1056           str_drc_instruction_str->drc_location) {
1057     str_drc_coeff_param_drc =
1058         &drc_config->str_drc_config_ext.str_drc_coeff_param_drc;
1059   }
1060 
1061   for (g = 0; g < str_drc_instruction_str->num_drc_ch_groups; g++) {
1062     WORD32 seq = str_drc_instruction_str->gain_set_index_for_channel_group[g];
1063     if (seq != -1 &&
1064         (drc_config->drc_coefficients_drc_count == 0 ||
1065          seq >= str_p_loc_drc_coefficients_uni_drc->gain_set_count)) {
1066       str_drc_instruction_str->ch_group_parametric_drc_flag[g] = 1;
1067       if (drc_config->drc_coefficients_drc_count != 0) {
1068         seq = seq - str_p_loc_drc_coefficients_uni_drc->gain_set_count;
1069       }
1070       str_drc_instruction_str->gain_set_idx_of_ch_group_parametric_drc[g] = seq;
1071 
1072       if (str_drc_coeff_param_drc == NULL ||
1073           seq >= str_drc_coeff_param_drc->parametric_drc_gain_set_count) {
1074         /* parametric drc gain set not available */
1075         return (EXTERNAL_ERROR);
1076       }
1077       str_drc_instruction_str->gain_interpolation_type_for_channel_group[g] = 1;
1078       str_drc_instruction_str->time_delta_min_for_channel_group[g] =
1079           str_drc_coeff_param_drc->parametric_drc_frame_size;
1080       str_drc_instruction_str->time_alignment_for_channel_group[g] = 0;
1081     } else {
1082       str_drc_instruction_str->ch_group_parametric_drc_flag[g] = 0;
1083     }
1084     if (str_drc_instruction_str->ch_group_parametric_drc_flag[g] == 0) {
1085       if (seq >= str_p_loc_drc_coefficients_uni_drc->gain_set_count) {
1086         return -1;
1087       }
1088       str_drc_instruction_str->gain_interpolation_type_for_channel_group[g] =
1089           str_p_loc_drc_coefficients_uni_drc->gain_set_params[seq]
1090               .gain_interpolation_type;
1091       if (str_p_loc_drc_coefficients_uni_drc->gain_set_params[seq]
1092               .time_delt_min_flag) {
1093         str_drc_instruction_str->time_delta_min_for_channel_group[g] =
1094             str_p_loc_drc_coefficients_uni_drc->gain_set_params[seq]
1095                 .time_delt_min_val;
1096       } else {
1097         str_drc_instruction_str->time_delta_min_for_channel_group[g] =
1098             ia_drc_params_struct->delta_tmin_default;
1099       }
1100       str_drc_instruction_str->time_alignment_for_channel_group[g] =
1101           str_p_loc_drc_coefficients_uni_drc->gain_set_params[seq]
1102               .time_alignment;
1103     }
1104   }
1105 
1106   if (str_drc_instruction_str->drc_set_effect &
1107       (EFFECT_BIT_DUCK_OTHER | EFFECT_BIT_DUCK_SELF)) {
1108     str_drc_instruction_str->gain_element_count =
1109         str_drc_instruction_str->num_drc_ch_groups;
1110   } else {
1111     for (g = 0; g < str_drc_instruction_str->num_drc_ch_groups; g++) {
1112       if (str_drc_instruction_str->ch_group_parametric_drc_flag[g] == 1) {
1113         gain_element_count++;
1114         str_drc_instruction_str->band_count_of_ch_group[g] = 1;
1115       } else {
1116         WORD32 seq, band_count;
1117         seq = str_drc_instruction_str->gain_set_index_for_channel_group[g];
1118         band_count =
1119             str_p_loc_drc_coefficients_uni_drc->gain_set_params[seq].band_count;
1120         str_drc_instruction_str->band_count_of_ch_group[g] = band_count;
1121         gain_element_count += band_count;
1122       }
1123     }
1124     str_drc_instruction_str->gain_element_count = gain_element_count;
1125   }
1126 
1127   return (0);
1128 }
1129 
1130 WORD32
impd_parse_drc_config(ia_bit_buf_struct * it_bit_buff,ia_drc_params_bs_dec_struct * ia_drc_params_struct,ia_drc_config * drc_config)1131 impd_parse_drc_config(ia_bit_buf_struct* it_bit_buff,
1132                       ia_drc_params_bs_dec_struct* ia_drc_params_struct,
1133                       ia_drc_config* drc_config) {
1134   WORD32 i, err = 0, temp;
1135   WORD32 version = 0;
1136 
1137   drc_config->sample_rate_present = impd_read_bits_buf(it_bit_buff, 1);
1138   if (it_bit_buff->error) return it_bit_buff->error;
1139 
1140   if (drc_config->sample_rate_present == 1) {
1141     WORD32 bssample_rate;
1142     bssample_rate = impd_read_bits_buf(it_bit_buff, 18);
1143     if (it_bit_buff->error) return it_bit_buff->error;
1144     drc_config->sampling_rate = bssample_rate + 1000;
1145   }
1146 
1147   temp = impd_read_bits_buf(it_bit_buff, 8);
1148   if (it_bit_buff->error) return it_bit_buff->error;
1149 
1150   drc_config->dwnmix_instructions_count = (temp >> 1) & 0x7f;
1151   if (drc_config->dwnmix_instructions_count > DOWNMIX_INSTRUCTION_COUNT_MAX)
1152     return (UNEXPECTED_ERROR);
1153 
1154   drc_config->drc_description_basic_present = temp & 1;
1155 
1156   if (drc_config->drc_description_basic_present == 1) {
1157     temp = impd_read_bits_buf(it_bit_buff, 7);
1158     if (it_bit_buff->error) return it_bit_buff->error;
1159 
1160     drc_config->drc_coefficients_basic_count = (temp >> 4) & 7;
1161     drc_config->drc_instructions_basic_count = temp & 0xf;
1162 
1163   } else {
1164     drc_config->drc_coefficients_basic_count = 0;
1165     drc_config->drc_instructions_basic_count = 0;
1166   }
1167 
1168   temp = impd_read_bits_buf(it_bit_buff, 9);
1169   if (it_bit_buff->error) return it_bit_buff->error;
1170 
1171   drc_config->drc_coefficients_drc_count = (temp >> 6) & 7;
1172   drc_config->drc_instructions_uni_drc_count = temp & 0x3f;
1173 
1174   if (drc_config->drc_instructions_uni_drc_count > DRC_INSTRUCTIONS_COUNT_MAX)
1175     return (UNEXPECTED_ERROR);
1176 
1177   err = impd_parse_ch_layout(it_bit_buff, ia_drc_params_struct,
1178                              &drc_config->channel_layout);
1179   if (err) return (err);
1180 
1181   for (i = 0; i < drc_config->dwnmix_instructions_count; i++) {
1182     err = impd_parse_dwnmix_instructions(
1183         it_bit_buff, version, ia_drc_params_struct, &drc_config->channel_layout,
1184         &(drc_config->dwnmix_instructions[i]));
1185     if (err) return (err);
1186   }
1187   for (i = 0; i < drc_config->drc_coefficients_basic_count; i++) {
1188     temp = impd_read_bits_buf(it_bit_buff, 11);
1189     if (it_bit_buff->error) return it_bit_buff->error;
1190 
1191     drc_config->str_drc_coefficients_basic[i].drc_location = (temp >> 7) & 0xf;
1192     drc_config->str_drc_coefficients_basic[i].drc_characteristic = temp & 0x3f;
1193   }
1194   for (i = 0; i < drc_config->drc_instructions_basic_count; i++) {
1195     err = impd_drc_parse_instructions_basic(
1196         it_bit_buff, &(drc_config->str_drc_instructions_basic[i]));
1197     if (err) return (err);
1198   }
1199   for (i = 0; i < drc_config->drc_coefficients_drc_count; i++) {
1200     err = impd_drc_parse_coeff(
1201         it_bit_buff, version, ia_drc_params_struct,
1202         &(drc_config->str_p_loc_drc_coefficients_uni_drc[i]));
1203     if (err) return (err);
1204   }
1205   for (i = 0; i < drc_config->drc_instructions_uni_drc_count; i++) {
1206     err = impd_parse_drc_instructions_uni_drc(
1207         it_bit_buff, version, drc_config,
1208         &(drc_config->str_drc_instruction_str[i]));
1209     if (err) return (err);
1210   }
1211 
1212   drc_config->drc_config_ext_present = impd_read_bits_buf(it_bit_buff, 1);
1213   if (it_bit_buff->error) return it_bit_buff->error;
1214 
1215   if (drc_config->drc_config_ext_present == 1) {
1216     err =
1217         impd_parse_drc_config_ext(it_bit_buff, ia_drc_params_struct, drc_config,
1218                                   &(drc_config->str_drc_config_ext));
1219     if (err) return (err);
1220   }
1221 
1222   if (drc_config->str_drc_config_ext.parametric_drc_present) {
1223     err = impd_parametric_drc_gen_virtual_gain_sets(drc_config);
1224     if (err) return (err);
1225   }
1226 
1227   for (i = 0; i < drc_config->drc_instructions_uni_drc_count; i++) {
1228     err = impd_drc_gen_instructions_derived_data(
1229         drc_config, ia_drc_params_struct,
1230         &(drc_config->str_drc_instruction_str[i]));
1231     if (err) return (err);
1232   }
1233 
1234   if ((drc_config->drc_instructions_uni_drc_count +
1235        drc_config->dwnmix_instructions_count) >= DRC_INSTRUCTIONS_COUNT_MAX)
1236     return (UNEXPECTED_ERROR);
1237   impd_drc_gen_instructions_for_drc_off(drc_config);
1238   return (0);
1239 }
1240 
1241 WORD32
impd_dec_method_value(ia_bit_buf_struct * it_bit_buff,WORD32 method_def,FLOAT32 * method_val)1242 impd_dec_method_value(ia_bit_buf_struct* it_bit_buff, WORD32 method_def,
1243                       FLOAT32* method_val) {
1244   // WORD32 err = 0;
1245   WORD32 tmp;
1246   FLOAT32 val;
1247   switch (method_def) {
1248     case METHOD_DEFINITION_UNKNOWN_OTHER:
1249     case METHOD_DEFINITION_PROGRAM_LOUDNESS:
1250     case METHOD_DEFINITION_ANCHOR_LOUDNESS:
1251     case METHOD_DEFINITION_MAX_OF_LOUDNESS_RANGE:
1252     case METHOD_DEFINITION_MOMENTARY_LOUDNESS_MAX:
1253     case METHOD_DEFINITION_SHORT_TERM_LOUDNESS_MAX:
1254       tmp = impd_read_bits_buf(it_bit_buff, 8);
1255       if (it_bit_buff->error) return it_bit_buff->error;
1256       val = -57.75f + tmp * 0.25f;
1257       break;
1258     case METHOD_DEFINITION_LOUDNESS_RANGE:
1259       tmp = impd_read_bits_buf(it_bit_buff, 8);
1260       if (it_bit_buff->error) return it_bit_buff->error;
1261       if (tmp == 0)
1262         val = 0.0f;
1263       else if (tmp <= 128)
1264         val = tmp * 0.25f;
1265       else if (tmp <= 204)
1266         val = 0.5f * tmp - 32.0f;
1267       else
1268         val = tmp - 134.0f;
1269       break;
1270     case METHOD_DEFINITION_MIXING_LEVEL:
1271       tmp = impd_read_bits_buf(it_bit_buff, 5);
1272       if (it_bit_buff->error) return it_bit_buff->error;
1273       val = tmp + 80.0f;
1274       break;
1275     case METHOD_DEFINITION_ROOM_TYPE:
1276       tmp = impd_read_bits_buf(it_bit_buff, 2);
1277       if (it_bit_buff->error) return it_bit_buff->error;
1278       val = (FLOAT32)tmp;
1279       break;
1280     case METHOD_DEFINITION_SHORT_TERM_LOUDNESS:
1281       tmp = impd_read_bits_buf(it_bit_buff, 8);
1282       if (it_bit_buff->error) return it_bit_buff->error;
1283       val = -116.f + tmp * 0.5f;
1284       break;
1285     default:
1286       return -1;
1287       break;
1288   }
1289   *method_val = val;
1290   return 0;
1291 }
1292 
1293 WORD32
impd_parse_loudness_info_set(ia_bit_buf_struct * it_bit_buff,ia_drc_loudness_info_set_struct * loudness_info_set)1294 impd_parse_loudness_info_set(
1295     ia_bit_buf_struct* it_bit_buff,
1296     ia_drc_loudness_info_set_struct* loudness_info_set) {
1297   WORD32 err = 0, i, version = 0, offset, temp;
1298   WORD32 loudness_info_album_count, loudness_info_count;
1299 
1300   temp = impd_read_bits_buf(it_bit_buff, 12);
1301   if (it_bit_buff->error) return it_bit_buff->error;
1302 
1303   loudness_info_album_count = (temp >> 6) & 0x3f;
1304   loudness_info_count = temp & 0x3f;
1305 
1306   offset = loudness_info_set->loudness_info_album_count;
1307   loudness_info_set->loudness_info_album_count += loudness_info_album_count;
1308   if ((offset + loudness_info_set->loudness_info_album_count) >
1309       LOUDNESS_INFO_COUNT_MAX)
1310     return (UNEXPECTED_ERROR);
1311   for (i = 0; i < loudness_info_set->loudness_info_album_count; i++) {
1312     err = impd_parse_loudness_info(
1313         it_bit_buff, version,
1314         &(loudness_info_set->str_loudness_info_album[i + offset]));
1315     if (err) return (err);
1316   }
1317 
1318   offset = loudness_info_set->loudness_info_count;
1319   loudness_info_set->loudness_info_count += loudness_info_count;
1320   if ((offset + loudness_info_set->loudness_info_count) >
1321       LOUDNESS_INFO_COUNT_MAX)
1322     return (UNEXPECTED_ERROR);
1323   for (i = 0; i < loudness_info_set->loudness_info_count; i++) {
1324     err = impd_parse_loudness_info(
1325         it_bit_buff, version, &(loudness_info_set->loudness_info[i + offset]));
1326     if (err) return (err);
1327   }
1328 
1329   loudness_info_set->loudness_info_set_ext_present =
1330       impd_read_bits_buf(it_bit_buff, 1);
1331   if (it_bit_buff->error) return it_bit_buff->error;
1332 
1333   if (loudness_info_set->loudness_info_set_ext_present == 1) {
1334     err = impd_parse_loudness_info_set_ext(it_bit_buff, loudness_info_set);
1335     if (err) return (err);
1336   }
1337 
1338   return (0);
1339 }
1340 
1341 WORD32
impd_parse_gain_set_params_characteristics(ia_bit_buf_struct * it_bit_buff,WORD32 version,ia_gain_params_struct * gain_params)1342 impd_parse_gain_set_params_characteristics(ia_bit_buf_struct* it_bit_buff,
1343                                            WORD32 version,
1344                                            ia_gain_params_struct* gain_params) {
1345   // WORD32 err = 0;
1346   WORD32 temp;
1347   if (version == 0) {
1348     gain_params->drc_characteristic = impd_read_bits_buf(it_bit_buff, 7);
1349     if (it_bit_buff->error) return it_bit_buff->error;
1350     if (gain_params->drc_characteristic > 0) {
1351       gain_params->drc_characteristic_present = 1;
1352       gain_params->drc_characteristic_format_is_cicp = 1;
1353     } else {
1354       gain_params->drc_characteristic_present = 0;
1355     }
1356   } else {
1357     gain_params->drc_characteristic_present =
1358         impd_read_bits_buf(it_bit_buff, 1);
1359     if (it_bit_buff->error) return it_bit_buff->error;
1360     if (gain_params->drc_characteristic_present) {
1361       gain_params->drc_characteristic_format_is_cicp =
1362           impd_read_bits_buf(it_bit_buff, 1);
1363       if (it_bit_buff->error) return it_bit_buff->error;
1364       if (gain_params->drc_characteristic_format_is_cicp) {
1365         gain_params->drc_characteristic = impd_read_bits_buf(it_bit_buff, 7);
1366         if (it_bit_buff->error) return it_bit_buff->error;
1367       } else {
1368         temp = impd_read_bits_buf(it_bit_buff, 8);
1369         if (it_bit_buff->error) return it_bit_buff->error;
1370 
1371         gain_params->drc_characteristic_left_index = (temp >> 4) & 0xf;
1372         gain_params->drc_characteristic_right_index = temp & 0xf;
1373       }
1374     }
1375   }
1376   return (0);
1377 }
1378 
1379 WORD32
impd_parse_loudness_measure(ia_bit_buf_struct * it_bit_buff,ia_loudness_measure_struct * loudness_measure)1380 impd_parse_loudness_measure(ia_bit_buf_struct* it_bit_buff,
1381                             ia_loudness_measure_struct* loudness_measure) {
1382   WORD32 err = 0, temp;
1383 
1384   loudness_measure->method_def = impd_read_bits_buf(it_bit_buff, 4);
1385   if (it_bit_buff->error) return it_bit_buff->error;
1386 
1387   err = impd_dec_method_value(it_bit_buff, loudness_measure->method_def,
1388                               &(loudness_measure->method_val));
1389   if (err) return err;
1390 
1391   temp = impd_read_bits_buf(it_bit_buff, 6);
1392   if (it_bit_buff->error) return it_bit_buff->error;
1393 
1394   loudness_measure->measurement_system = (temp >> 2) & 0xf;
1395   if (loudness_measure->measurement_system > MEASUREMENT_SYSTEM_RESERVED_E)
1396     return (UNEXPECTED_ERROR);
1397   /* Parsed but unused */
1398   loudness_measure->reliability = temp & 3;
1399 
1400   return (0);
1401 }
1402 
1403 WORD32
impd_dec_gain_modifiers(ia_bit_buf_struct * it_bit_buff,WORD32 version,WORD32 band_count,ia_gain_modifiers_struct * pstr_gain_modifiers)1404 impd_dec_gain_modifiers(ia_bit_buf_struct* it_bit_buff, WORD32 version,
1405                         WORD32 band_count,
1406                         ia_gain_modifiers_struct* pstr_gain_modifiers) {
1407   // WORD32 err = 0;
1408   WORD32 sign, temp;
1409 
1410   if (version > 0) {
1411     WORD32 b;
1412     for (b = 0; b < band_count; b++) {
1413       pstr_gain_modifiers->target_characteristic_left_present[b] =
1414           impd_read_bits_buf(it_bit_buff, 1);
1415       if (it_bit_buff->error) return it_bit_buff->error;
1416       if (pstr_gain_modifiers->target_characteristic_left_present[b]) {
1417         WORD32 tmp_index = impd_read_bits_buf(it_bit_buff, 4);
1418         if (it_bit_buff->error) return it_bit_buff->error;
1419         if (tmp_index >= SPLIT_CHARACTERISTIC_COUNT_MAX)
1420           return (UNEXPECTED_ERROR);
1421         pstr_gain_modifiers->target_characteristic_left_index[b] = tmp_index;
1422       }
1423       pstr_gain_modifiers->target_characteristic_right_present[b] =
1424           impd_read_bits_buf(it_bit_buff, 1);
1425       if (it_bit_buff->error) return it_bit_buff->error;
1426       if (pstr_gain_modifiers->target_characteristic_right_present[b]) {
1427         WORD32 tmp_index = impd_read_bits_buf(it_bit_buff, 4);
1428         if (it_bit_buff->error) return it_bit_buff->error;
1429         if (tmp_index >= SPLIT_CHARACTERISTIC_COUNT_MAX)
1430           return (UNEXPECTED_ERROR);
1431         pstr_gain_modifiers->target_characteristic_right_index[b] = tmp_index;
1432       }
1433       pstr_gain_modifiers->gain_scaling_flag[b] =
1434           impd_read_bits_buf(it_bit_buff, 1);
1435       if (it_bit_buff->error) return it_bit_buff->error;
1436       if (pstr_gain_modifiers->gain_scaling_flag[b]) {
1437         temp = impd_read_bits_buf(it_bit_buff, 8);
1438         if (it_bit_buff->error) return it_bit_buff->error;
1439 
1440         pstr_gain_modifiers->attn_scaling[b] = ((temp >> 4) & 0xf) * 0.125f;
1441         pstr_gain_modifiers->ampl_scaling[b] = (temp & 0xf) * 0.125f;
1442       }
1443 
1444       pstr_gain_modifiers->gain_offset_flag[b] =
1445           impd_read_bits_buf(it_bit_buff, 1);
1446       if (it_bit_buff->error) return it_bit_buff->error;
1447       if (pstr_gain_modifiers->gain_offset_flag[b]) {
1448         FLOAT32 gain_offset;
1449         temp = impd_read_bits_buf(it_bit_buff, 6);
1450         if (it_bit_buff->error) return it_bit_buff->error;
1451 
1452         sign = ((temp >> 5) & 1);
1453         gain_offset = (1 + (temp & 0x1f)) * 0.25f;
1454 
1455         if (sign) {
1456           gain_offset = -gain_offset;
1457         }
1458         pstr_gain_modifiers->gain_offset[b] = gain_offset;
1459       }
1460     }
1461     if (band_count == 1) {
1462       WORD32 tmp;
1463       pstr_gain_modifiers->shape_filter_flag =
1464           impd_read_bits_buf(it_bit_buff, 1);
1465       if (it_bit_buff->error) return it_bit_buff->error;
1466       if (pstr_gain_modifiers->shape_filter_flag) {
1467         tmp = impd_read_bits_buf(it_bit_buff, 4);
1468         if (it_bit_buff->error) return it_bit_buff->error;
1469         if (tmp >= (SHAPE_FILTER_COUNT_MAX + 1)) return UNEXPECTED_ERROR;
1470         pstr_gain_modifiers->shape_filter_idx = tmp;
1471       }
1472     }
1473   } else if (version == 0) {
1474     WORD32 b, gain_scaling_flag, gain_offset_flag;
1475     FLOAT32 attn_scaling = 1.0f, ampl_scaling = 1.0f, gain_offset = 0.0f;
1476 
1477     gain_scaling_flag = impd_read_bits_buf(it_bit_buff, 1);
1478     if (it_bit_buff->error) return it_bit_buff->error;
1479     if (gain_scaling_flag) {
1480       temp = impd_read_bits_buf(it_bit_buff, 8);
1481       if (it_bit_buff->error) return it_bit_buff->error;
1482 
1483       attn_scaling = ((temp >> 4) & 0xf) * 0.125f;
1484       ampl_scaling = (temp & 0xf) * 0.125f;
1485     }
1486 
1487     gain_offset_flag = impd_read_bits_buf(it_bit_buff, 1);
1488     if (it_bit_buff->error) return it_bit_buff->error;
1489     if (gain_offset_flag) {
1490       temp = impd_read_bits_buf(it_bit_buff, 6);
1491       if (it_bit_buff->error) return it_bit_buff->error;
1492 
1493       sign = ((temp >> 5) & 1);
1494       gain_offset = (1 + (temp & 0x1f)) * 0.25f;
1495 
1496       if (sign) {
1497         gain_offset = -gain_offset;
1498       }
1499     }
1500     for (b = 0; b < band_count; b++) {
1501       pstr_gain_modifiers->target_characteristic_left_present[b] = 0;
1502       pstr_gain_modifiers->target_characteristic_right_present[b] = 0;
1503       pstr_gain_modifiers->gain_scaling_flag[b] = gain_scaling_flag;
1504       pstr_gain_modifiers->attn_scaling[b] = attn_scaling;
1505       pstr_gain_modifiers->ampl_scaling[b] = ampl_scaling;
1506       pstr_gain_modifiers->gain_offset_flag[b] = gain_offset_flag;
1507       pstr_gain_modifiers->gain_offset[b] = gain_offset;
1508     }
1509     pstr_gain_modifiers->shape_filter_flag = 0;
1510   }
1511   return (0);
1512 }
1513 
1514 WORD32
impd_parse_gain_set_params(ia_bit_buf_struct * it_bit_buff,WORD32 version,WORD32 * gain_seq_idx,ia_gain_set_params_struct * gain_set_params)1515 impd_parse_gain_set_params(ia_bit_buf_struct* it_bit_buff, WORD32 version,
1516                            WORD32* gain_seq_idx,
1517                            ia_gain_set_params_struct* gain_set_params) {
1518   WORD32 err = 0, i, temp;
1519 
1520   temp = impd_read_bits_buf(it_bit_buff, 6);
1521   if (it_bit_buff->error) return it_bit_buff->error;
1522 
1523   gain_set_params->gain_coding_profile = (temp >> 4) & 3;
1524   gain_set_params->gain_interpolation_type = (temp >> 3) & 1;
1525   gain_set_params->full_frame = (temp >> 2) & 1;
1526   gain_set_params->time_alignment = (temp >> 1) & 1;
1527   gain_set_params->time_delt_min_flag = temp & 1;
1528 
1529   if (gain_set_params->time_delt_min_flag) {
1530     WORD32 time_delta_min;
1531     time_delta_min = impd_read_bits_buf(it_bit_buff, 11);
1532     if (it_bit_buff->error) return it_bit_buff->error;
1533     gain_set_params->time_delt_min_val = time_delta_min + 1;
1534   }
1535 
1536   if (gain_set_params->gain_coding_profile == GAIN_CODING_PROFILE_CONSTANT) {
1537     gain_set_params->band_count = 1;
1538     *gain_seq_idx = (*gain_seq_idx) + 1;
1539   } else {
1540     gain_set_params->band_count = impd_read_bits_buf(it_bit_buff, 4);
1541     if (it_bit_buff->error) return it_bit_buff->error;
1542 
1543     if (gain_set_params->band_count > BAND_COUNT_MAX) return (UNEXPECTED_ERROR);
1544 
1545     if (gain_set_params->band_count > 1) {
1546       gain_set_params->drc_band_type = impd_read_bits_buf(it_bit_buff, 1);
1547       if (it_bit_buff->error) return it_bit_buff->error;
1548     }
1549     for (i = 0; i < gain_set_params->band_count; i++) {
1550       if (version == 0) {
1551         *gain_seq_idx = (*gain_seq_idx) + 1;
1552       } else {
1553         WORD32 indexPresent;
1554         indexPresent = impd_read_bits_buf(it_bit_buff, 1);
1555         if (it_bit_buff->error) return it_bit_buff->error;
1556         if (indexPresent) {
1557           WORD32 bsIndex;
1558           bsIndex = impd_read_bits_buf(it_bit_buff, 6);
1559           if (it_bit_buff->error) return it_bit_buff->error;
1560           *gain_seq_idx = bsIndex;
1561         } else {
1562           *gain_seq_idx = (*gain_seq_idx) + 1;
1563         }
1564       }
1565 
1566       if (*gain_seq_idx >= SEQUENCE_COUNT_MAX) return UNEXPECTED_ERROR;
1567 
1568       gain_set_params->gain_params[i].gain_seq_idx = *gain_seq_idx;
1569       err = impd_parse_gain_set_params_characteristics(
1570           it_bit_buff, version, &(gain_set_params->gain_params[i]));
1571       if (err) return (err);
1572     }
1573     if (gain_set_params->drc_band_type) {
1574       for (i = 1; i < gain_set_params->band_count; i++) {
1575         gain_set_params->gain_params[i].crossover_freq_idx =
1576             impd_read_bits_buf(it_bit_buff, 4);
1577         if (it_bit_buff->error) return it_bit_buff->error;
1578       }
1579     } else {
1580       for (i = 1; i < gain_set_params->band_count; i++) {
1581         gain_set_params->gain_params[i].start_subband_index =
1582             impd_read_bits_buf(it_bit_buff, 10);
1583         if (it_bit_buff->error) return it_bit_buff->error;
1584       }
1585     }
1586   }
1587 
1588   return (0);
1589 }
1590 
1591 WORD32
impd_sel_drc_coeff(ia_drc_config * drc_config,WORD32 location,ia_uni_drc_coeffs_struct ** str_p_loc_drc_coefficients_uni_drc)1592 impd_sel_drc_coeff(
1593     ia_drc_config* drc_config, WORD32 location,
1594     ia_uni_drc_coeffs_struct** str_p_loc_drc_coefficients_uni_drc) {
1595   WORD32 n;
1596   WORD32 c1 = -1;
1597   WORD32 c0 = -1;
1598   for (n = 0; n < drc_config->drc_coefficients_drc_count; n++) {
1599     if (drc_config->str_p_loc_drc_coefficients_uni_drc[n].drc_location ==
1600         location) {
1601       if (drc_config->str_p_loc_drc_coefficients_uni_drc[n].version == 0) {
1602         c0 = n;
1603       } else {
1604         c1 = n;
1605       }
1606     }
1607   }
1608   if (c1 >= 0) {
1609     *str_p_loc_drc_coefficients_uni_drc =
1610         &(drc_config->str_p_loc_drc_coefficients_uni_drc[c1]);
1611   } else if (c0 >= 0) {
1612     *str_p_loc_drc_coefficients_uni_drc =
1613         &(drc_config->str_p_loc_drc_coefficients_uni_drc[c0]);
1614   } else {
1615     *str_p_loc_drc_coefficients_uni_drc = NULL;
1616   }
1617   return (0);
1618 }
1619 
1620 WORD32
impd_parse_loudness_info_set_ext(ia_bit_buf_struct * it_bit_buff,ia_drc_loudness_info_set_struct * loudness_info_set)1621 impd_parse_loudness_info_set_ext(
1622     ia_bit_buf_struct* it_bit_buff,
1623     ia_drc_loudness_info_set_struct* loudness_info_set) {
1624   WORD32 err = 0, i, k;
1625   WORD32 bit_size_len, ext_size_bits, bit_size, other_bit;
1626 
1627   k = 0;
1628   loudness_info_set->str_loudness_info_set_ext.loudness_info_set_ext_type[k] =
1629       impd_read_bits_buf(it_bit_buff, 4);
1630   if (it_bit_buff->error) return it_bit_buff->error;
1631   while (loudness_info_set->str_loudness_info_set_ext
1632              .loudness_info_set_ext_type[k] != UNIDRCLOUDEXT_TERM) {
1633     bit_size_len = impd_read_bits_buf(it_bit_buff, 4);
1634     if (it_bit_buff->error) return it_bit_buff->error;
1635     ext_size_bits = bit_size_len + 4;
1636 
1637     bit_size = impd_read_bits_buf(it_bit_buff, ext_size_bits);
1638     if (it_bit_buff->error) return it_bit_buff->error;
1639     if (k >= (EXT_COUNT_MAX - 1)) return UNEXPECTED_ERROR;
1640     loudness_info_set->str_loudness_info_set_ext.ext_bit_size[k] = bit_size + 1;
1641 
1642     switch (loudness_info_set->str_loudness_info_set_ext
1643                 .loudness_info_set_ext_type[k]) {
1644       case UNIDRCLOUDEXT_EQ:
1645         err = impd_parse_loud_info_set_ext_eq(it_bit_buff, loudness_info_set);
1646         if (err) return (err);
1647         break;
1648       default:
1649         for (i = 0;
1650              i < loudness_info_set->str_loudness_info_set_ext.ext_bit_size[k];
1651              i++) {
1652           other_bit = impd_read_bits_buf(it_bit_buff, 1);
1653           if (it_bit_buff->error) return it_bit_buff->error;
1654         }
1655         break;
1656     }
1657     k++;
1658     loudness_info_set->str_loudness_info_set_ext.loudness_info_set_ext_type[k] =
1659         impd_read_bits_buf(it_bit_buff, 4);
1660     if (it_bit_buff->error) return it_bit_buff->error;
1661   }
1662 
1663   return (0);
1664 }
1665 
1666 WORD32
impd_drc_parse_coeff(ia_bit_buf_struct * it_bit_buff,WORD32 version,ia_drc_params_bs_dec_struct * ia_drc_params_struct,ia_uni_drc_coeffs_struct * str_p_loc_drc_coefficients_uni_drc)1667 impd_drc_parse_coeff(
1668     ia_bit_buf_struct* it_bit_buff, WORD32 version,
1669     ia_drc_params_bs_dec_struct* ia_drc_params_struct,
1670     ia_uni_drc_coeffs_struct* str_p_loc_drc_coefficients_uni_drc) {
1671   WORD32 err = 0, i, drc_frame_size, temp;
1672   WORD32 gain_seq_idx = -1;
1673 
1674   str_p_loc_drc_coefficients_uni_drc->version = version;
1675   if (version == 0) {
1676     WORD32 gain_sequence_count = 0;
1677     temp = impd_read_bits_buf(it_bit_buff, 5);
1678     if (it_bit_buff->error) return it_bit_buff->error;
1679 
1680     str_p_loc_drc_coefficients_uni_drc->drc_location = (temp >> 1) & 0xf;
1681     str_p_loc_drc_coefficients_uni_drc->drc_frame_size_present = temp & 1;
1682 
1683     if (str_p_loc_drc_coefficients_uni_drc->drc_frame_size_present == 1) {
1684       drc_frame_size = impd_read_bits_buf(it_bit_buff, 15);
1685       if (it_bit_buff->error) return it_bit_buff->error;
1686       str_p_loc_drc_coefficients_uni_drc->drc_frame_size = drc_frame_size + 1;
1687       if (str_p_loc_drc_coefficients_uni_drc->drc_frame_size >
1688           MAX_DRC_FRAME_SIZE)
1689         return UNEXPECTED_ERROR;
1690     }
1691 
1692     str_p_loc_drc_coefficients_uni_drc->drc_characteristic_left_present = 0;
1693     str_p_loc_drc_coefficients_uni_drc->drc_characteristic_right_present = 0;
1694     str_p_loc_drc_coefficients_uni_drc->shape_filters_present = 0;
1695     str_p_loc_drc_coefficients_uni_drc->gain_set_count =
1696         impd_read_bits_buf(it_bit_buff, 6);
1697     if (it_bit_buff->error) return it_bit_buff->error;
1698 
1699     if (str_p_loc_drc_coefficients_uni_drc->gain_set_count > GAIN_SET_COUNT_MAX)
1700       return (UNEXPECTED_ERROR);
1701 
1702     str_p_loc_drc_coefficients_uni_drc->gain_set_count_plus =
1703         str_p_loc_drc_coefficients_uni_drc->gain_set_count;
1704     for (i = 0; i < str_p_loc_drc_coefficients_uni_drc->gain_set_count; i++) {
1705       err = impd_parse_gain_set_params(
1706           it_bit_buff, version, &gain_seq_idx,
1707           &(str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]));
1708       if (err) return (err);
1709 
1710       if (str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1711               .time_delt_min_flag) {
1712         if (str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1713                 .time_delt_min_val > ia_drc_params_struct->drc_frame_size) {
1714           /* drc time interval too big */
1715           return (PARAM_ERROR);
1716         }
1717         str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1718             .num_gain_max_values =
1719             ia_drc_params_struct->drc_frame_size /
1720             str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1721                 .time_delt_min_val;
1722         if (str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1723                 .num_gain_max_values >
1724             (N_DELTA_TIME_CODE_TABLE_ENTRIES_MAX / 2 - 1))
1725           return (UNEXPECTED_ERROR);
1726         impd_init_tbls(str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1727                            .num_gain_max_values,
1728                        &(str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1729                              .str_tables));
1730       }
1731       gain_sequence_count +=
1732           str_p_loc_drc_coefficients_uni_drc->gain_set_params[i].band_count;
1733     }
1734     str_p_loc_drc_coefficients_uni_drc->gain_sequence_count =
1735         gain_sequence_count;
1736   } else {
1737     ia_shape_filter_block_params_struct* pstr_shape_filter_block_params;
1738     for (i = 0; i < SEQUENCE_COUNT_MAX; i++) {
1739       str_p_loc_drc_coefficients_uni_drc
1740           ->gain_set_params_index_for_gain_sequence[i] = -1;
1741     }
1742 
1743     temp = impd_read_bits_buf(it_bit_buff, 5);
1744     if (it_bit_buff->error) return it_bit_buff->error;
1745 
1746     str_p_loc_drc_coefficients_uni_drc->drc_location = (temp >> 1) & 0xf;
1747     str_p_loc_drc_coefficients_uni_drc->drc_frame_size_present = temp & 1;
1748 
1749     if (str_p_loc_drc_coefficients_uni_drc->drc_frame_size_present == 1) {
1750       drc_frame_size = impd_read_bits_buf(it_bit_buff, 15);
1751       if (it_bit_buff->error) return it_bit_buff->error;
1752       str_p_loc_drc_coefficients_uni_drc->drc_frame_size = drc_frame_size + 1;
1753     }
1754     str_p_loc_drc_coefficients_uni_drc->drc_characteristic_left_present =
1755         impd_read_bits_buf(it_bit_buff, 1);
1756     if (it_bit_buff->error) return it_bit_buff->error;
1757     if (str_p_loc_drc_coefficients_uni_drc->drc_characteristic_left_present ==
1758         1) {
1759       str_p_loc_drc_coefficients_uni_drc->characteristic_left_count =
1760           impd_read_bits_buf(it_bit_buff, 4);
1761       if (it_bit_buff->error) return it_bit_buff->error;
1762 
1763       if (str_p_loc_drc_coefficients_uni_drc->characteristic_left_count >
1764           SPLIT_CHARACTERISTIC_COUNT_MAX)
1765         return (UNEXPECTED_ERROR);
1766 
1767       for (i = 1;
1768            i <= str_p_loc_drc_coefficients_uni_drc->characteristic_left_count;
1769            i++) {
1770         err = impd_parse_split_drc_characteristic(
1771             it_bit_buff, LEFT_SIDE, &(str_p_loc_drc_coefficients_uni_drc
1772                                           ->str_split_characteristic_left[i]));
1773         if (err) return (err);
1774       }
1775     }
1776     str_p_loc_drc_coefficients_uni_drc->drc_characteristic_right_present =
1777         impd_read_bits_buf(it_bit_buff, 1);
1778     if (it_bit_buff->error) return it_bit_buff->error;
1779     if (str_p_loc_drc_coefficients_uni_drc->drc_characteristic_right_present ==
1780         1) {
1781       str_p_loc_drc_coefficients_uni_drc->characteristic_right_count =
1782           impd_read_bits_buf(it_bit_buff, 4);
1783       if (it_bit_buff->error) return it_bit_buff->error;
1784 
1785       if (str_p_loc_drc_coefficients_uni_drc->characteristic_right_count >
1786           SPLIT_CHARACTERISTIC_COUNT_MAX)
1787         return (UNEXPECTED_ERROR);
1788       for (i = 1;
1789            i <= str_p_loc_drc_coefficients_uni_drc->characteristic_right_count;
1790            i++) {
1791         err = impd_parse_split_drc_characteristic(
1792             it_bit_buff, RIGHT_SIDE,
1793             &(str_p_loc_drc_coefficients_uni_drc
1794                   ->str_split_characteristic_right[i]));
1795         if (err) return (err);
1796       }
1797     }
1798     str_p_loc_drc_coefficients_uni_drc->shape_filters_present =
1799         impd_read_bits_buf(it_bit_buff, 1);
1800     if (it_bit_buff->error) return it_bit_buff->error;
1801     if (str_p_loc_drc_coefficients_uni_drc->shape_filters_present == 1) {
1802       str_p_loc_drc_coefficients_uni_drc->shape_num_filter =
1803           impd_read_bits_buf(it_bit_buff, 4);
1804       if (it_bit_buff->error) return it_bit_buff->error;
1805       if (str_p_loc_drc_coefficients_uni_drc->shape_num_filter >
1806           SHAPE_FILTER_COUNT_MAX)
1807         return (UNEXPECTED_ERROR);
1808       for (i = 1; i <= str_p_loc_drc_coefficients_uni_drc->shape_num_filter;
1809            i++) {
1810         pstr_shape_filter_block_params =
1811             &(str_p_loc_drc_coefficients_uni_drc
1812                   ->str_shape_filter_block_params[i]);
1813         pstr_shape_filter_block_params->lf_cut_filter_present =
1814             impd_read_bits_buf(it_bit_buff, 1);
1815         if (it_bit_buff->error) return it_bit_buff->error;
1816 
1817         if (pstr_shape_filter_block_params->lf_cut_filter_present == 1) {
1818           temp = impd_read_bits_buf(it_bit_buff, 5);
1819           if (it_bit_buff->error) return it_bit_buff->error;
1820 
1821           pstr_shape_filter_block_params->str_lf_cut_params.corner_freq_index =
1822               (temp >> 2) & 7;
1823           pstr_shape_filter_block_params->str_lf_cut_params
1824               .filter_strength_index = temp & 3;
1825         }
1826         pstr_shape_filter_block_params->lf_boost_filter_present =
1827             impd_read_bits_buf(it_bit_buff, 1);
1828         if (it_bit_buff->error) return it_bit_buff->error;
1829         if (pstr_shape_filter_block_params->lf_boost_filter_present == 1) {
1830           temp = impd_read_bits_buf(it_bit_buff, 5);
1831           if (it_bit_buff->error) return it_bit_buff->error;
1832 
1833           pstr_shape_filter_block_params->str_lf_boost_params
1834               .corner_freq_index = (temp >> 2) & 7;
1835           pstr_shape_filter_block_params->str_lf_boost_params
1836               .filter_strength_index = temp & 3;
1837         }
1838         pstr_shape_filter_block_params->hf_cut_filter_present =
1839             impd_read_bits_buf(it_bit_buff, 1);
1840         if (it_bit_buff->error) return it_bit_buff->error;
1841         if (pstr_shape_filter_block_params->hf_cut_filter_present == 1) {
1842           temp = impd_read_bits_buf(it_bit_buff, 5);
1843           if (it_bit_buff->error) return it_bit_buff->error;
1844 
1845           pstr_shape_filter_block_params->str_hfCutParams.corner_freq_index =
1846               (temp >> 2) & 7;
1847           pstr_shape_filter_block_params->str_hfCutParams
1848               .filter_strength_index = temp & 3;
1849         }
1850         pstr_shape_filter_block_params->hf_boost_filter_present =
1851             impd_read_bits_buf(it_bit_buff, 1);
1852         if (it_bit_buff->error) return it_bit_buff->error;
1853         if (pstr_shape_filter_block_params->hf_boost_filter_present == 1) {
1854           temp = impd_read_bits_buf(it_bit_buff, 5);
1855           if (it_bit_buff->error) return it_bit_buff->error;
1856 
1857           pstr_shape_filter_block_params->str_hf_boost_params
1858               .corner_freq_index = (temp >> 2) & 7;
1859           pstr_shape_filter_block_params->str_hf_boost_params
1860               .filter_strength_index = temp & 3;
1861         }
1862       }
1863     }
1864 
1865     temp = impd_read_bits_buf(it_bit_buff, 12);
1866     if (it_bit_buff->error) return it_bit_buff->error;
1867 
1868     str_p_loc_drc_coefficients_uni_drc->gain_sequence_count =
1869         (temp >> 6) & 0x3f;
1870 
1871     if (str_p_loc_drc_coefficients_uni_drc->gain_sequence_count >
1872         SEQUENCE_COUNT_MAX)
1873       return UNEXPECTED_ERROR;
1874 
1875     str_p_loc_drc_coefficients_uni_drc->gain_set_count = temp & 0x3f;
1876 
1877     if (str_p_loc_drc_coefficients_uni_drc->gain_set_count > GAIN_SET_COUNT_MAX)
1878       return (UNEXPECTED_ERROR);
1879 
1880     str_p_loc_drc_coefficients_uni_drc->gain_set_count_plus =
1881         str_p_loc_drc_coefficients_uni_drc->gain_set_count;
1882     for (i = 0; i < str_p_loc_drc_coefficients_uni_drc->gain_set_count; i++) {
1883       err = impd_parse_gain_set_params(
1884           it_bit_buff, version, &gain_seq_idx,
1885           &(str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]));
1886       if (err) return (err);
1887 
1888       if (str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1889               .time_delt_min_flag) {
1890         if (str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1891                 .time_delt_min_val > ia_drc_params_struct->drc_frame_size) {
1892           /* drc time interval too big */
1893           return (PARAM_ERROR);
1894         }
1895         str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1896             .num_gain_max_values =
1897             ia_drc_params_struct->drc_frame_size /
1898             str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1899                 .time_delt_min_val;
1900         if (str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1901                 .num_gain_max_values >
1902             (N_DELTA_TIME_CODE_TABLE_ENTRIES_MAX / 2 - 1))
1903           return (UNEXPECTED_ERROR);
1904         impd_init_tbls(str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1905                            .num_gain_max_values,
1906                        &(str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1907                              .str_tables));
1908       }
1909     }
1910 
1911     for (i = 0; i < str_p_loc_drc_coefficients_uni_drc->gain_set_count; i++) {
1912       WORD32 b;
1913       for (b = 0;
1914            b <
1915            str_p_loc_drc_coefficients_uni_drc->gain_set_params[i].band_count;
1916            b++) {
1917         str_p_loc_drc_coefficients_uni_drc
1918             ->gain_set_params_index_for_gain_sequence
1919                 [str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1920                      .gain_params[b]
1921                      .gain_seq_idx] = i;
1922       }
1923     }
1924   }
1925   return (0);
1926 }
1927 
1928 WORD32
impd_drc_parse_instructions_basic(ia_bit_buf_struct * it_bit_buff,ia_drc_instructions_basic_struct * str_drc_instructions_basic)1929 impd_drc_parse_instructions_basic(
1930     ia_bit_buf_struct* it_bit_buff,
1931     ia_drc_instructions_basic_struct* str_drc_instructions_basic) {
1932   // WORD32 err = 0;
1933   WORD32 i, limiter_peak_target, temp;
1934   WORD32 additional_dmix_id_present, additional_dmix_id_cnt;
1935 
1936   temp = impd_read_bits_buf(it_bit_buff, 18);
1937   if (it_bit_buff->error) return it_bit_buff->error;
1938 
1939   str_drc_instructions_basic->drc_set_id = (temp >> 12) & 0x3f;
1940   str_drc_instructions_basic->drc_location = (temp >> 8) & 0xf;
1941   str_drc_instructions_basic->downmix_id[0] = (temp >> 1) & 0x7f;
1942   additional_dmix_id_present = temp & 1;
1943   str_drc_instructions_basic->dwnmix_id_count = 1;
1944 
1945   if (additional_dmix_id_present) {
1946     additional_dmix_id_cnt = impd_read_bits_buf(it_bit_buff, 3);
1947     if (it_bit_buff->error) return it_bit_buff->error;
1948     for (i = 0; i < additional_dmix_id_cnt; i++) {
1949       str_drc_instructions_basic->downmix_id[i + 1] =
1950           impd_read_bits_buf(it_bit_buff, 7);
1951       if (it_bit_buff->error) return it_bit_buff->error;
1952     }
1953     str_drc_instructions_basic->dwnmix_id_count = 1 + additional_dmix_id_cnt;
1954   }
1955 
1956   str_drc_instructions_basic->drc_set_effect =
1957       impd_read_bits_buf(it_bit_buff, 16);
1958   if (it_bit_buff->error) return it_bit_buff->error;
1959 
1960   if ((str_drc_instructions_basic->drc_set_effect &
1961        (EFFECT_BIT_DUCK_OTHER | EFFECT_BIT_DUCK_SELF)) == 0) {
1962     str_drc_instructions_basic->limiter_peak_target_present =
1963         impd_read_bits_buf(it_bit_buff, 1);
1964     if (it_bit_buff->error) return it_bit_buff->error;
1965     if (str_drc_instructions_basic->limiter_peak_target_present) {
1966       limiter_peak_target = impd_read_bits_buf(it_bit_buff, 8);
1967       if (it_bit_buff->error) return it_bit_buff->error;
1968       str_drc_instructions_basic->limiter_peak_target =
1969           -limiter_peak_target * 0.125f;
1970     }
1971   }
1972 
1973   str_drc_instructions_basic->drc_set_target_loudness_present =
1974       impd_read_bits_buf(it_bit_buff, 1);
1975   if (it_bit_buff->error) return it_bit_buff->error;
1976 
1977   str_drc_instructions_basic->drc_set_target_loudness_value_upper = 0;
1978   str_drc_instructions_basic->drc_set_target_loudness_value_lower = -63;
1979 
1980   if (str_drc_instructions_basic->drc_set_target_loudness_present == 1) {
1981     WORD32 bsDrcSetTargetLoudnessValueUpper, bsDrcSetTargetLoudnessValueLower;
1982     bsDrcSetTargetLoudnessValueUpper = impd_read_bits_buf(it_bit_buff, 6);
1983     if (it_bit_buff->error) return it_bit_buff->error;
1984     str_drc_instructions_basic->drc_set_target_loudness_value_upper =
1985         bsDrcSetTargetLoudnessValueUpper - 63;
1986 
1987     str_drc_instructions_basic->drc_set_target_loudness_value_lower_present =
1988         impd_read_bits_buf(it_bit_buff, 1);
1989     if (it_bit_buff->error) return it_bit_buff->error;
1990 
1991     if (str_drc_instructions_basic
1992             ->drc_set_target_loudness_value_lower_present == 1) {
1993       bsDrcSetTargetLoudnessValueLower = impd_read_bits_buf(it_bit_buff, 6);
1994       if (it_bit_buff->error) return it_bit_buff->error;
1995       str_drc_instructions_basic->drc_set_target_loudness_value_lower =
1996           bsDrcSetTargetLoudnessValueLower - 63;
1997     }
1998   }
1999 
2000   return (0);
2001 }
2002 
2003 WORD32
impd_dec_ducking_scaling(ia_bit_buf_struct * it_bit_buff,WORD32 * ducking_scaling_flag,FLOAT32 * p_ducking_scaling)2004 impd_dec_ducking_scaling(ia_bit_buf_struct* it_bit_buff,
2005                          WORD32* ducking_scaling_flag,
2006                          FLOAT32* p_ducking_scaling) {
2007   WORD32 ducking_scaling_present, ducking_scaling, sigma, mu;
2008 
2009   ducking_scaling_present = impd_read_bits_buf(it_bit_buff, 1);
2010   if (it_bit_buff->error) return it_bit_buff->error;
2011 
2012   if (ducking_scaling_present == 0) {
2013     *ducking_scaling_flag = 0;
2014     *p_ducking_scaling = 1.0f;
2015   } else {
2016     *ducking_scaling_flag = 1;
2017     ducking_scaling = impd_read_bits_buf(it_bit_buff, 4);
2018     if (it_bit_buff->error) return it_bit_buff->error;
2019 
2020     sigma = ducking_scaling >> 3;
2021     mu = ducking_scaling & 0x7;
2022 
2023     if (sigma == 0) {
2024       *p_ducking_scaling = 1.0f + 0.125f * (1.0f + mu);
2025     } else {
2026       *p_ducking_scaling = 1.0f - 0.125f * (1.0f + mu);
2027     }
2028   }
2029   return (0);
2030 }
2031 
2032 WORD32
impd_parse_drc_instructions_uni_drc(ia_bit_buf_struct * it_bit_buff,WORD32 version,ia_drc_config * drc_config,ia_drc_instructions_struct * str_drc_instruction_str)2033 impd_parse_drc_instructions_uni_drc(
2034     ia_bit_buf_struct* it_bit_buff, WORD32 version, ia_drc_config* drc_config,
2035     ia_drc_instructions_struct* str_drc_instruction_str) {
2036   WORD32 err = 0, i, n, k, g, c, limiter_peak_target, idx;
2037   WORD32 additional_dmix_id_present, additional_dmix_id_cnt;
2038   ia_uni_drc_coeffs_struct* str_p_loc_drc_coefficients_uni_drc = NULL;
2039   WORD32 ch_cnt;
2040   WORD32 unique_idx[MAX_CHANNEL_COUNT];
2041   FLOAT32 unique_scaling[MAX_CHANNEL_COUNT];
2042   WORD32 match;
2043   WORD32 dmix_id_present;
2044   WORD32 repeat_parameters, repeat_parameters_cnt;
2045   WORD32 ducking_sequence;
2046   FLOAT32 factor;
2047 
2048   str_drc_instruction_str->drc_set_id = impd_read_bits_buf(it_bit_buff, 6);
2049   if (it_bit_buff->error) return it_bit_buff->error;
2050   if (str_drc_instruction_str->drc_set_id >= DRC_INSTRUCTIONS_COUNT_MAX)
2051     return UNEXPECTED_ERROR;
2052   if (version == 0) {
2053     str_drc_instruction_str->drc_set_complexity_level =
2054         DRC_COMPLEXITY_LEVEL_MAX;
2055   } else {
2056     str_drc_instruction_str->drc_set_complexity_level =
2057         impd_read_bits_buf(it_bit_buff, 4);
2058     if (it_bit_buff->error) return it_bit_buff->error;
2059   }
2060   str_drc_instruction_str->drc_location = impd_read_bits_buf(it_bit_buff, 4);
2061   if (it_bit_buff->error) return it_bit_buff->error;
2062   dmix_id_present = 1;
2063   if (version >= 1) {
2064     dmix_id_present = impd_read_bits_buf(it_bit_buff, 1);
2065     if (it_bit_buff->error) return it_bit_buff->error;
2066   }
2067   if (dmix_id_present == 1) {
2068     str_drc_instruction_str->downmix_id[0] = impd_read_bits_buf(it_bit_buff, 7);
2069     if (it_bit_buff->error) return it_bit_buff->error;
2070     if (version >= 1) {
2071       str_drc_instruction_str->drc_apply_to_dwnmix =
2072           impd_read_bits_buf(it_bit_buff, 1);
2073       if (it_bit_buff->error) return it_bit_buff->error;
2074     }
2075     if (version == 0) {
2076       if (str_drc_instruction_str->downmix_id[0] == 0) {
2077         str_drc_instruction_str->drc_apply_to_dwnmix = 0;
2078       } else {
2079         str_drc_instruction_str->drc_apply_to_dwnmix = 1;
2080       }
2081     }
2082     additional_dmix_id_present = impd_read_bits_buf(it_bit_buff, 1);
2083     if (it_bit_buff->error) return it_bit_buff->error;
2084 
2085     if (additional_dmix_id_present) {
2086       additional_dmix_id_cnt = impd_read_bits_buf(it_bit_buff, 3);
2087       if (it_bit_buff->error) return it_bit_buff->error;
2088       for (i = 0; i < additional_dmix_id_cnt; i++) {
2089         str_drc_instruction_str->downmix_id[i + 1] =
2090             impd_read_bits_buf(it_bit_buff, 7);
2091         if (it_bit_buff->error) return it_bit_buff->error;
2092       }
2093       str_drc_instruction_str->dwnmix_id_count = 1 + additional_dmix_id_cnt;
2094     } else {
2095       str_drc_instruction_str->dwnmix_id_count = 1;
2096     }
2097   } else {
2098     str_drc_instruction_str->downmix_id[0] = 0;
2099     str_drc_instruction_str->dwnmix_id_count = 1;
2100   }
2101 
2102   str_drc_instruction_str->drc_set_effect = impd_read_bits_buf(it_bit_buff, 16);
2103   if (it_bit_buff->error) return it_bit_buff->error;
2104 
2105   if ((str_drc_instruction_str->drc_set_effect &
2106        (EFFECT_BIT_DUCK_OTHER | EFFECT_BIT_DUCK_SELF)) == 0) {
2107     str_drc_instruction_str->limiter_peak_target_present =
2108         impd_read_bits_buf(it_bit_buff, 1);
2109     if (it_bit_buff->error) return it_bit_buff->error;
2110     if (str_drc_instruction_str->limiter_peak_target_present) {
2111       limiter_peak_target = impd_read_bits_buf(it_bit_buff, 8);
2112       if (it_bit_buff->error) return it_bit_buff->error;
2113       str_drc_instruction_str->limiter_peak_target =
2114           -limiter_peak_target * 0.125f;
2115     }
2116   }
2117 
2118   str_drc_instruction_str->drc_set_target_loudness_present =
2119       impd_read_bits_buf(it_bit_buff, 1);
2120   if (it_bit_buff->error) return it_bit_buff->error;
2121 
2122   str_drc_instruction_str->drc_set_target_loudness_value_upper = 0;
2123   str_drc_instruction_str->drc_set_target_loudness_value_lower = -63;
2124 
2125   if (str_drc_instruction_str->drc_set_target_loudness_present == 1) {
2126     WORD32 bsDrcSetTargetLoudnessValueUpper, bsDrcSetTargetLoudnessValueLower;
2127     bsDrcSetTargetLoudnessValueUpper = impd_read_bits_buf(it_bit_buff, 6);
2128     if (it_bit_buff->error) return it_bit_buff->error;
2129     str_drc_instruction_str->drc_set_target_loudness_value_upper =
2130         bsDrcSetTargetLoudnessValueUpper - 63;
2131     str_drc_instruction_str->drc_set_target_loudness_value_lower_present =
2132         impd_read_bits_buf(it_bit_buff, 1);
2133     if (it_bit_buff->error) return it_bit_buff->error;
2134     if (str_drc_instruction_str->drc_set_target_loudness_value_lower_present ==
2135         1) {
2136       bsDrcSetTargetLoudnessValueLower = impd_read_bits_buf(it_bit_buff, 6);
2137       if (it_bit_buff->error) return it_bit_buff->error;
2138       str_drc_instruction_str->drc_set_target_loudness_value_lower =
2139           bsDrcSetTargetLoudnessValueLower - 63;
2140     }
2141   }
2142 
2143   str_drc_instruction_str->depends_on_drc_set_present =
2144       impd_read_bits_buf(it_bit_buff, 1);
2145   if (it_bit_buff->error) return it_bit_buff->error;
2146 
2147   str_drc_instruction_str->no_independent_use = 0;
2148   if (str_drc_instruction_str->depends_on_drc_set_present) {
2149     str_drc_instruction_str->depends_on_drc_set =
2150         impd_read_bits_buf(it_bit_buff, 6);
2151     if (it_bit_buff->error) return it_bit_buff->error;
2152   } else {
2153     str_drc_instruction_str->no_independent_use =
2154         impd_read_bits_buf(it_bit_buff, 1);
2155     if (it_bit_buff->error) return it_bit_buff->error;
2156   }
2157   if (version == 0) {
2158     str_drc_instruction_str->requires_eq = 0;
2159   } else {
2160     str_drc_instruction_str->requires_eq = impd_read_bits_buf(it_bit_buff, 1);
2161     if (it_bit_buff->error) return it_bit_buff->error;
2162   }
2163 
2164   err = impd_sel_drc_coeff(drc_config, str_drc_instruction_str->drc_location,
2165                            &str_p_loc_drc_coefficients_uni_drc);
2166   if (err) return (err);
2167 
2168   ch_cnt = drc_config->channel_layout.base_channel_count;
2169 
2170   if (ch_cnt > MAX_CHANNEL_COUNT) return (UNEXPECTED_ERROR);
2171   for (c = 0; c < MAX_CHANNEL_COUNT; c++) {
2172     unique_idx[c] = -10;
2173     unique_scaling[c] = -10.0f;
2174   }
2175 
2176   if (str_drc_instruction_str->drc_set_effect &
2177       (EFFECT_BIT_DUCK_OTHER | EFFECT_BIT_DUCK_SELF)) {
2178     c = 0;
2179     while (c < ch_cnt) {
2180       WORD32 bs_gain_set_idx;
2181       bs_gain_set_idx = impd_read_bits_buf(it_bit_buff, 6);
2182       if (it_bit_buff->error) return it_bit_buff->error;
2183       if (bs_gain_set_idx > GAIN_SET_COUNT_MAX) return UNEXPECTED_ERROR;
2184       str_drc_instruction_str->gain_set_index[c] = bs_gain_set_idx - 1;
2185       impd_dec_ducking_scaling(
2186           it_bit_buff,
2187           &(str_drc_instruction_str->str_ducking_modifiers_for_channel[c]
2188                 .ducking_scaling_flag),
2189           &(str_drc_instruction_str->str_ducking_modifiers_for_channel[c]
2190                 .ducking_scaling));
2191 
2192       c++;
2193 
2194       repeat_parameters = impd_read_bits_buf(it_bit_buff, 1);
2195       if (it_bit_buff->error) return it_bit_buff->error;
2196 
2197       if (repeat_parameters == 1) {
2198         repeat_parameters_cnt = impd_read_bits_buf(it_bit_buff, 5);
2199         if (it_bit_buff->error) return it_bit_buff->error;
2200 
2201         repeat_parameters_cnt += 1;
2202         if ((c + repeat_parameters_cnt) > MAX_CHANNEL_COUNT)
2203           return (UNEXPECTED_ERROR);
2204         for (k = 0; k < repeat_parameters_cnt; k++) {
2205           str_drc_instruction_str->gain_set_index[c] =
2206               str_drc_instruction_str->gain_set_index[c - 1];
2207           str_drc_instruction_str->str_ducking_modifiers_for_channel[c] =
2208               str_drc_instruction_str->str_ducking_modifiers_for_channel[c - 1];
2209           c++;
2210         }
2211       }
2212     }
2213     if (c > ch_cnt) {
2214       return (UNEXPECTED_ERROR);
2215     }
2216     ducking_sequence = -1;
2217     g = 0;
2218     if (str_drc_instruction_str->drc_set_effect & EFFECT_BIT_DUCK_OTHER) {
2219       for (c = 0; c < ch_cnt; c++) {
2220         match = 0;
2221         idx = str_drc_instruction_str->gain_set_index[c];
2222         factor = str_drc_instruction_str->str_ducking_modifiers_for_channel[c]
2223                      .ducking_scaling;
2224         if (idx < 0) {
2225           for (n = 0; n < g; n++) {
2226             if (unique_scaling[n] == factor) {
2227               match = 1;
2228               str_drc_instruction_str->channel_group_of_ch[c] = n;
2229               break;
2230             }
2231           }
2232           if (match == 0) {
2233             unique_idx[g] = idx;
2234             unique_scaling[g] = factor;
2235             str_drc_instruction_str->channel_group_of_ch[c] = g;
2236             g++;
2237           }
2238         } else {
2239           if ((ducking_sequence > 0) && (ducking_sequence != idx)) {
2240             /* drc for ducking can have only one ducking sequence */
2241             return (UNEXPECTED_ERROR);
2242           }
2243           ducking_sequence = idx;
2244           str_drc_instruction_str->channel_group_of_ch[c] = -1;
2245         }
2246       }
2247       str_drc_instruction_str->num_drc_ch_groups = g;
2248       if (ducking_sequence == -1) {
2249         /* ducking sequence not found */
2250         return (UNEXPECTED_ERROR);
2251       }
2252     } else if (str_drc_instruction_str->drc_set_effect & EFFECT_BIT_DUCK_SELF) {
2253       for (c = 0; c < ch_cnt; c++) {
2254         match = 0;
2255         idx = str_drc_instruction_str->gain_set_index[c];
2256         factor = str_drc_instruction_str->str_ducking_modifiers_for_channel[c]
2257                      .ducking_scaling;
2258         if (idx >= 0) {
2259           for (n = 0; n < g; n++) {
2260             if ((unique_idx[n] == idx) && (unique_scaling[n] == factor)) {
2261               match = 1;
2262               str_drc_instruction_str->channel_group_of_ch[c] = n;
2263               break;
2264             }
2265           }
2266           if (match == 0) {
2267             unique_idx[g] = idx;
2268             unique_scaling[g] = factor;
2269             str_drc_instruction_str->channel_group_of_ch[c] = g;
2270             g++;
2271           }
2272         } else {
2273           str_drc_instruction_str->channel_group_of_ch[c] = -1;
2274         }
2275       }
2276       str_drc_instruction_str->num_drc_ch_groups = g;
2277     }
2278 
2279     if (str_drc_instruction_str->num_drc_ch_groups >
2280         min(CHANNEL_GROUP_COUNT_MAX, MAX_CHANNEL_COUNT))
2281       return UNEXPECTED_ERROR;
2282     for (g = 0; g < str_drc_instruction_str->num_drc_ch_groups; g++) {
2283       WORD32 set =
2284           (str_drc_instruction_str->drc_set_effect & EFFECT_BIT_DUCK_OTHER)
2285               ? ducking_sequence
2286               : unique_idx[g];
2287       if (set < 0) return UNEXPECTED_ERROR;
2288       str_drc_instruction_str->gain_set_index_for_channel_group[g] = set;
2289       str_drc_instruction_str->str_ducking_modifiers_for_channel_group[g]
2290           .ducking_scaling = unique_scaling[g];
2291       if (unique_scaling[g] != 1.0f) {
2292         str_drc_instruction_str->str_ducking_modifiers_for_channel_group[g]
2293             .ducking_scaling_flag = 1;
2294       } else {
2295         str_drc_instruction_str->str_ducking_modifiers_for_channel_group[g]
2296             .ducking_scaling_flag = 0;
2297       }
2298       str_drc_instruction_str->band_count_of_ch_group[g] = 1;
2299     }
2300   } else {
2301     if (((version == 0) ||
2302          (str_drc_instruction_str->drc_apply_to_dwnmix != 0)) &&
2303         (str_drc_instruction_str->downmix_id[0] != 0) &&
2304         (str_drc_instruction_str->downmix_id[0] != ID_FOR_ANY_DOWNMIX) &&
2305         (str_drc_instruction_str->dwnmix_id_count == 1)) {
2306       for (i = 0; i < drc_config->dwnmix_instructions_count; i++) {
2307         if (str_drc_instruction_str->downmix_id[0] ==
2308             drc_config->dwnmix_instructions[i].downmix_id)
2309           break;
2310       }
2311       if (i == drc_config->dwnmix_instructions_count) {
2312         /* dwnmix_instructions not found */
2313         return (UNEXPECTED_ERROR);
2314       }
2315       ch_cnt = drc_config->dwnmix_instructions[i].target_channel_count;
2316     } else if (((version == 0) ||
2317                 (str_drc_instruction_str->drc_apply_to_dwnmix != 0)) &&
2318                ((str_drc_instruction_str->downmix_id[0] ==
2319                  ID_FOR_ANY_DOWNMIX) ||
2320                 (str_drc_instruction_str->dwnmix_id_count > 1))) {
2321       ch_cnt = 1;
2322     }
2323 
2324     if (ch_cnt > MAX_CHANNEL_COUNT) return (UNEXPECTED_ERROR);
2325     c = 0;
2326     while (c < ch_cnt) {
2327       WORD32 bs_gain_set_idx;
2328       WORD32 repeat_gain_set_idx, repeat_gain_set_idx_cnt, temp;
2329 
2330       temp = impd_read_bits_buf(it_bit_buff, 7);
2331       if (it_bit_buff->error) return it_bit_buff->error;
2332 
2333       bs_gain_set_idx = (temp >> 1) & 0x7f;
2334       repeat_gain_set_idx = temp & 1;
2335 
2336       if (bs_gain_set_idx > GAIN_SET_COUNT_MAX) return UNEXPECTED_ERROR;
2337 
2338       str_drc_instruction_str->gain_set_index[c] = bs_gain_set_idx - 1;
2339       c++;
2340 
2341       if (repeat_gain_set_idx == 1) {
2342         repeat_gain_set_idx_cnt = impd_read_bits_buf(it_bit_buff, 5);
2343         if (it_bit_buff->error) return it_bit_buff->error;
2344 
2345         repeat_gain_set_idx_cnt += 1;
2346         if ((c + repeat_gain_set_idx_cnt) > MAX_CHANNEL_COUNT)
2347           return (UNEXPECTED_ERROR);
2348         for (k = 0; k < repeat_gain_set_idx_cnt; k++) {
2349           str_drc_instruction_str->gain_set_index[c] = bs_gain_set_idx - 1;
2350           c++;
2351         }
2352       }
2353     }
2354     if (c > ch_cnt) {
2355       return (UNEXPECTED_ERROR);
2356     }
2357 
2358     g = 0;
2359     if ((str_drc_instruction_str->downmix_id[0] == ID_FOR_ANY_DOWNMIX) ||
2360         (str_drc_instruction_str->dwnmix_id_count > 1)) {
2361       WORD32 idx = str_drc_instruction_str->gain_set_index[0];
2362       if (idx >= 0) {
2363         unique_idx[0] = idx;
2364         g = 1;
2365       }
2366     } else {
2367       for (c = 0; c < ch_cnt; c++) {
2368         WORD32 idx = str_drc_instruction_str->gain_set_index[c];
2369         match = 0;
2370         if (idx >= 0) {
2371           for (n = 0; n < g; n++) {
2372             if (unique_idx[n] == idx) {
2373               match = 1;
2374               str_drc_instruction_str->channel_group_of_ch[c] = n;
2375               break;
2376             }
2377           }
2378           if (match == 0) {
2379             unique_idx[g] = idx;
2380             str_drc_instruction_str->channel_group_of_ch[c] = g;
2381             g++;
2382           }
2383         } else {
2384           str_drc_instruction_str->channel_group_of_ch[c] = -1;
2385         }
2386       }
2387     }
2388 
2389     str_drc_instruction_str->num_drc_ch_groups = g;
2390 
2391     if (str_drc_instruction_str->num_drc_ch_groups >
2392         min(CHANNEL_GROUP_COUNT_MAX, MAX_CHANNEL_COUNT))
2393       return UNEXPECTED_ERROR;
2394     for (g = 0; g < str_drc_instruction_str->num_drc_ch_groups; g++) {
2395       WORD32 set, band_count;
2396 
2397       set = unique_idx[g];
2398       if (set < 0) return UNEXPECTED_ERROR;
2399       str_drc_instruction_str->gain_set_index_for_channel_group[g] = set;
2400 
2401       if (str_p_loc_drc_coefficients_uni_drc != NULL &&
2402           set < str_p_loc_drc_coefficients_uni_drc->gain_set_count) {
2403         band_count =
2404             str_p_loc_drc_coefficients_uni_drc->gain_set_params[set].band_count;
2405       } else {
2406         band_count = 1;
2407       }
2408 
2409       err = impd_dec_gain_modifiers(
2410           it_bit_buff, version, band_count,
2411           &(str_drc_instruction_str->str_gain_modifiers_of_ch_group[g]));
2412       if (err) return (err);
2413     }
2414   }
2415 
2416   return (0);
2417 }
2418 WORD32
impd_parse_loudness_info(ia_bit_buf_struct * it_bit_buff,WORD32 version,ia_loudness_info_struct * loudness_info)2419 impd_parse_loudness_info(ia_bit_buf_struct* it_bit_buff, WORD32 version,
2420                          ia_loudness_info_struct* loudness_info) {
2421   WORD32 err = 0, sample_peak_level, true_peak_level, i, temp;
2422 
2423   loudness_info->drc_set_id = impd_read_bits_buf(it_bit_buff, 6);
2424   if (it_bit_buff->error) return it_bit_buff->error;
2425 
2426   if (version >= 1) {
2427     loudness_info->eq_set_id = impd_read_bits_buf(it_bit_buff, 6);
2428     if (it_bit_buff->error) return it_bit_buff->error;
2429   } else {
2430     loudness_info->eq_set_id = 0;
2431   }
2432 
2433   temp = impd_read_bits_buf(it_bit_buff, 8);
2434   if (it_bit_buff->error) return it_bit_buff->error;
2435 
2436   loudness_info->downmix_id = (temp >> 1) & 0x7f;
2437   loudness_info->sample_peak_level_present = temp & 1;
2438 
2439   if (loudness_info->sample_peak_level_present) {
2440     sample_peak_level = impd_read_bits_buf(it_bit_buff, 12);
2441     if (it_bit_buff->error) return it_bit_buff->error;
2442 
2443     if (sample_peak_level == 0) {
2444       loudness_info->sample_peak_level_present = 0;
2445       loudness_info->sample_peak_level = 0.0f;
2446     } else {
2447       loudness_info->sample_peak_level = 20.0f - sample_peak_level * 0.03125f;
2448     }
2449   }
2450 
2451   loudness_info->true_peak_level_present = impd_read_bits_buf(it_bit_buff, 1);
2452   if (it_bit_buff->error) return it_bit_buff->error;
2453 
2454   if (loudness_info->true_peak_level_present) {
2455     true_peak_level = impd_read_bits_buf(it_bit_buff, 12);
2456     if (it_bit_buff->error) return it_bit_buff->error;
2457 
2458     if (true_peak_level == 0) {
2459       loudness_info->true_peak_level_present = 0;
2460       loudness_info->true_peak_level = 0.0f;
2461     } else {
2462       loudness_info->true_peak_level = 20.0f - true_peak_level * 0.03125f;
2463     }
2464 
2465     temp = impd_read_bits_buf(it_bit_buff, 6);
2466     if (it_bit_buff->error) return it_bit_buff->error;
2467 
2468     /* Parsed but unused */
2469     loudness_info->true_peak_level_measurement_system = (temp >> 2) & 0xf;
2470     /* Parsed but unused */
2471     loudness_info->true_peak_level_reliability = temp & 3;
2472   }
2473 
2474   loudness_info->measurement_count = impd_read_bits_buf(it_bit_buff, 4);
2475   if (it_bit_buff->error) return it_bit_buff->error;
2476 
2477   for (i = 0; i < loudness_info->measurement_count; i++) {
2478     err = impd_parse_loudness_measure(it_bit_buff,
2479                                       &(loudness_info->loudness_measure[i]));
2480     if (err) return (err);
2481   }
2482 
2483   return (0);
2484 }
2485