• 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         pstr_gain_modifiers->target_characteristic_left_index[b] =
1418             impd_read_bits_buf(it_bit_buff, 4);
1419         if (it_bit_buff->error) return it_bit_buff->error;
1420       }
1421       pstr_gain_modifiers->target_characteristic_right_present[b] =
1422           impd_read_bits_buf(it_bit_buff, 1);
1423       if (it_bit_buff->error) return it_bit_buff->error;
1424       if (pstr_gain_modifiers->target_characteristic_right_present[b]) {
1425         pstr_gain_modifiers->target_characteristic_right_index[b] =
1426             impd_read_bits_buf(it_bit_buff, 4);
1427         if (it_bit_buff->error) return it_bit_buff->error;
1428       }
1429       pstr_gain_modifiers->gain_scaling_flag[b] =
1430           impd_read_bits_buf(it_bit_buff, 1);
1431       if (it_bit_buff->error) return it_bit_buff->error;
1432       if (pstr_gain_modifiers->gain_scaling_flag[b]) {
1433         temp = impd_read_bits_buf(it_bit_buff, 8);
1434         if (it_bit_buff->error) return it_bit_buff->error;
1435 
1436         pstr_gain_modifiers->attn_scaling[b] = ((temp >> 4) & 0xf) * 0.125f;
1437         pstr_gain_modifiers->ampl_scaling[b] = (temp & 0xf) * 0.125f;
1438       }
1439 
1440       pstr_gain_modifiers->gain_offset_flag[b] =
1441           impd_read_bits_buf(it_bit_buff, 1);
1442       if (it_bit_buff->error) return it_bit_buff->error;
1443       if (pstr_gain_modifiers->gain_offset_flag[b]) {
1444         FLOAT32 gain_offset;
1445         temp = impd_read_bits_buf(it_bit_buff, 6);
1446         if (it_bit_buff->error) return it_bit_buff->error;
1447 
1448         sign = ((temp >> 5) & 1);
1449         gain_offset = (1 + (temp & 0x1f)) * 0.25f;
1450 
1451         if (sign) {
1452           gain_offset = -gain_offset;
1453         }
1454         pstr_gain_modifiers->gain_offset[b] = gain_offset;
1455       }
1456     }
1457     if (band_count == 1) {
1458       WORD32 tmp;
1459       pstr_gain_modifiers->shape_filter_flag =
1460           impd_read_bits_buf(it_bit_buff, 1);
1461       if (it_bit_buff->error) return it_bit_buff->error;
1462       if (pstr_gain_modifiers->shape_filter_flag) {
1463         tmp = impd_read_bits_buf(it_bit_buff, 4);
1464         if (it_bit_buff->error) return it_bit_buff->error;
1465         if (tmp >= (SHAPE_FILTER_COUNT_MAX + 1)) return UNEXPECTED_ERROR;
1466         pstr_gain_modifiers->shape_filter_idx = tmp;
1467       }
1468     }
1469   } else if (version == 0) {
1470     WORD32 b, gain_scaling_flag, gain_offset_flag;
1471     FLOAT32 attn_scaling = 1.0f, ampl_scaling = 1.0f, gain_offset = 0.0f;
1472 
1473     gain_scaling_flag = impd_read_bits_buf(it_bit_buff, 1);
1474     if (it_bit_buff->error) return it_bit_buff->error;
1475     if (gain_scaling_flag) {
1476       temp = impd_read_bits_buf(it_bit_buff, 8);
1477       if (it_bit_buff->error) return it_bit_buff->error;
1478 
1479       attn_scaling = ((temp >> 4) & 0xf) * 0.125f;
1480       ampl_scaling = (temp & 0xf) * 0.125f;
1481     }
1482 
1483     gain_offset_flag = impd_read_bits_buf(it_bit_buff, 1);
1484     if (it_bit_buff->error) return it_bit_buff->error;
1485     if (gain_offset_flag) {
1486       temp = impd_read_bits_buf(it_bit_buff, 6);
1487       if (it_bit_buff->error) return it_bit_buff->error;
1488 
1489       sign = ((temp >> 5) & 1);
1490       gain_offset = (1 + (temp & 0x1f)) * 0.25f;
1491 
1492       if (sign) {
1493         gain_offset = -gain_offset;
1494       }
1495     }
1496     for (b = 0; b < band_count; b++) {
1497       pstr_gain_modifiers->target_characteristic_left_present[b] = 0;
1498       pstr_gain_modifiers->target_characteristic_right_present[b] = 0;
1499       pstr_gain_modifiers->gain_scaling_flag[b] = gain_scaling_flag;
1500       pstr_gain_modifiers->attn_scaling[b] = attn_scaling;
1501       pstr_gain_modifiers->ampl_scaling[b] = ampl_scaling;
1502       pstr_gain_modifiers->gain_offset_flag[b] = gain_offset_flag;
1503       pstr_gain_modifiers->gain_offset[b] = gain_offset;
1504     }
1505     pstr_gain_modifiers->shape_filter_flag = 0;
1506   }
1507   return (0);
1508 }
1509 
1510 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)1511 impd_parse_gain_set_params(ia_bit_buf_struct* it_bit_buff, WORD32 version,
1512                            WORD32* gain_seq_idx,
1513                            ia_gain_set_params_struct* gain_set_params) {
1514   WORD32 err = 0, i, temp;
1515 
1516   temp = impd_read_bits_buf(it_bit_buff, 6);
1517   if (it_bit_buff->error) return it_bit_buff->error;
1518 
1519   gain_set_params->gain_coding_profile = (temp >> 4) & 3;
1520   gain_set_params->gain_interpolation_type = (temp >> 3) & 1;
1521   gain_set_params->full_frame = (temp >> 2) & 1;
1522   gain_set_params->time_alignment = (temp >> 1) & 1;
1523   gain_set_params->time_delt_min_flag = temp & 1;
1524 
1525   if (gain_set_params->time_delt_min_flag) {
1526     WORD32 time_delta_min;
1527     time_delta_min = impd_read_bits_buf(it_bit_buff, 11);
1528     if (it_bit_buff->error) return it_bit_buff->error;
1529     gain_set_params->time_delt_min_val = time_delta_min + 1;
1530   }
1531 
1532   if (gain_set_params->gain_coding_profile == GAIN_CODING_PROFILE_CONSTANT) {
1533     gain_set_params->band_count = 1;
1534     *gain_seq_idx = (*gain_seq_idx) + 1;
1535   } else {
1536     gain_set_params->band_count = impd_read_bits_buf(it_bit_buff, 4);
1537     if (it_bit_buff->error) return it_bit_buff->error;
1538 
1539     if (gain_set_params->band_count > BAND_COUNT_MAX) return (UNEXPECTED_ERROR);
1540 
1541     if (gain_set_params->band_count > 1) {
1542       gain_set_params->drc_band_type = impd_read_bits_buf(it_bit_buff, 1);
1543       if (it_bit_buff->error) return it_bit_buff->error;
1544     }
1545     for (i = 0; i < gain_set_params->band_count; i++) {
1546       if (version == 0) {
1547         *gain_seq_idx = (*gain_seq_idx) + 1;
1548       } else {
1549         WORD32 indexPresent;
1550         indexPresent = impd_read_bits_buf(it_bit_buff, 1);
1551         if (it_bit_buff->error) return it_bit_buff->error;
1552         if (indexPresent) {
1553           WORD32 bsIndex;
1554           bsIndex = impd_read_bits_buf(it_bit_buff, 6);
1555           if (it_bit_buff->error) return it_bit_buff->error;
1556           *gain_seq_idx = bsIndex;
1557         } else {
1558           *gain_seq_idx = (*gain_seq_idx) + 1;
1559         }
1560       }
1561 
1562       if (*gain_seq_idx >= SEQUENCE_COUNT_MAX) return UNEXPECTED_ERROR;
1563 
1564       gain_set_params->gain_params[i].gain_seq_idx = *gain_seq_idx;
1565       err = impd_parse_gain_set_params_characteristics(
1566           it_bit_buff, version, &(gain_set_params->gain_params[i]));
1567       if (err) return (err);
1568     }
1569     if (gain_set_params->drc_band_type) {
1570       for (i = 1; i < gain_set_params->band_count; i++) {
1571         gain_set_params->gain_params[i].crossover_freq_idx =
1572             impd_read_bits_buf(it_bit_buff, 4);
1573         if (it_bit_buff->error) return it_bit_buff->error;
1574       }
1575     } else {
1576       for (i = 1; i < gain_set_params->band_count; i++) {
1577         gain_set_params->gain_params[i].start_subband_index =
1578             impd_read_bits_buf(it_bit_buff, 10);
1579         if (it_bit_buff->error) return it_bit_buff->error;
1580       }
1581     }
1582   }
1583 
1584   return (0);
1585 }
1586 
1587 WORD32
impd_sel_drc_coeff(ia_drc_config * drc_config,WORD32 location,ia_uni_drc_coeffs_struct ** str_p_loc_drc_coefficients_uni_drc)1588 impd_sel_drc_coeff(
1589     ia_drc_config* drc_config, WORD32 location,
1590     ia_uni_drc_coeffs_struct** str_p_loc_drc_coefficients_uni_drc) {
1591   WORD32 n;
1592   WORD32 c1 = -1;
1593   WORD32 c0 = -1;
1594   for (n = 0; n < drc_config->drc_coefficients_drc_count; n++) {
1595     if (drc_config->str_p_loc_drc_coefficients_uni_drc[n].drc_location ==
1596         location) {
1597       if (drc_config->str_p_loc_drc_coefficients_uni_drc[n].version == 0) {
1598         c0 = n;
1599       } else {
1600         c1 = n;
1601       }
1602     }
1603   }
1604   if (c1 >= 0) {
1605     *str_p_loc_drc_coefficients_uni_drc =
1606         &(drc_config->str_p_loc_drc_coefficients_uni_drc[c1]);
1607   } else if (c0 >= 0) {
1608     *str_p_loc_drc_coefficients_uni_drc =
1609         &(drc_config->str_p_loc_drc_coefficients_uni_drc[c0]);
1610   } else {
1611     *str_p_loc_drc_coefficients_uni_drc = NULL;
1612   }
1613   return (0);
1614 }
1615 
1616 WORD32
impd_parse_loudness_info_set_ext(ia_bit_buf_struct * it_bit_buff,ia_drc_loudness_info_set_struct * loudness_info_set)1617 impd_parse_loudness_info_set_ext(
1618     ia_bit_buf_struct* it_bit_buff,
1619     ia_drc_loudness_info_set_struct* loudness_info_set) {
1620   WORD32 err = 0, i, k;
1621   WORD32 bit_size_len, ext_size_bits, bit_size, other_bit;
1622 
1623   k = 0;
1624   loudness_info_set->str_loudness_info_set_ext.loudness_info_set_ext_type[k] =
1625       impd_read_bits_buf(it_bit_buff, 4);
1626   if (it_bit_buff->error) return it_bit_buff->error;
1627   while (loudness_info_set->str_loudness_info_set_ext
1628              .loudness_info_set_ext_type[k] != UNIDRCLOUDEXT_TERM) {
1629     bit_size_len = impd_read_bits_buf(it_bit_buff, 4);
1630     if (it_bit_buff->error) return it_bit_buff->error;
1631     ext_size_bits = bit_size_len + 4;
1632 
1633     bit_size = impd_read_bits_buf(it_bit_buff, ext_size_bits);
1634     if (it_bit_buff->error) return it_bit_buff->error;
1635     if (k >= (EXT_COUNT_MAX - 1)) return UNEXPECTED_ERROR;
1636     loudness_info_set->str_loudness_info_set_ext.ext_bit_size[k] = bit_size + 1;
1637 
1638     switch (loudness_info_set->str_loudness_info_set_ext
1639                 .loudness_info_set_ext_type[k]) {
1640       case UNIDRCLOUDEXT_EQ:
1641         err = impd_parse_loud_info_set_ext_eq(it_bit_buff, loudness_info_set);
1642         if (err) return (err);
1643         break;
1644       default:
1645         for (i = 0;
1646              i < loudness_info_set->str_loudness_info_set_ext.ext_bit_size[k];
1647              i++) {
1648           other_bit = impd_read_bits_buf(it_bit_buff, 1);
1649           if (it_bit_buff->error) return it_bit_buff->error;
1650         }
1651         break;
1652     }
1653     k++;
1654     loudness_info_set->str_loudness_info_set_ext.loudness_info_set_ext_type[k] =
1655         impd_read_bits_buf(it_bit_buff, 4);
1656     if (it_bit_buff->error) return it_bit_buff->error;
1657   }
1658 
1659   return (0);
1660 }
1661 
1662 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)1663 impd_drc_parse_coeff(
1664     ia_bit_buf_struct* it_bit_buff, WORD32 version,
1665     ia_drc_params_bs_dec_struct* ia_drc_params_struct,
1666     ia_uni_drc_coeffs_struct* str_p_loc_drc_coefficients_uni_drc) {
1667   WORD32 err = 0, i, drc_frame_size, temp;
1668   WORD32 gain_seq_idx = -1;
1669 
1670   str_p_loc_drc_coefficients_uni_drc->version = version;
1671   if (version == 0) {
1672     WORD32 gain_sequence_count = 0;
1673     temp = impd_read_bits_buf(it_bit_buff, 5);
1674     if (it_bit_buff->error) return it_bit_buff->error;
1675 
1676     str_p_loc_drc_coefficients_uni_drc->drc_location = (temp >> 1) & 0xf;
1677     str_p_loc_drc_coefficients_uni_drc->drc_frame_size_present = temp & 1;
1678 
1679     if (str_p_loc_drc_coefficients_uni_drc->drc_frame_size_present == 1) {
1680       drc_frame_size = impd_read_bits_buf(it_bit_buff, 15);
1681       if (it_bit_buff->error) return it_bit_buff->error;
1682       str_p_loc_drc_coefficients_uni_drc->drc_frame_size = drc_frame_size + 1;
1683       if (str_p_loc_drc_coefficients_uni_drc->drc_frame_size >
1684           MAX_DRC_FRAME_SIZE)
1685         return UNEXPECTED_ERROR;
1686     }
1687 
1688     str_p_loc_drc_coefficients_uni_drc->drc_characteristic_left_present = 0;
1689     str_p_loc_drc_coefficients_uni_drc->drc_characteristic_right_present = 0;
1690     str_p_loc_drc_coefficients_uni_drc->shape_filters_present = 0;
1691     str_p_loc_drc_coefficients_uni_drc->gain_set_count =
1692         impd_read_bits_buf(it_bit_buff, 6);
1693     if (it_bit_buff->error) return it_bit_buff->error;
1694 
1695     if (str_p_loc_drc_coefficients_uni_drc->gain_set_count > GAIN_SET_COUNT_MAX)
1696       return (UNEXPECTED_ERROR);
1697 
1698     str_p_loc_drc_coefficients_uni_drc->gain_set_count_plus =
1699         str_p_loc_drc_coefficients_uni_drc->gain_set_count;
1700     for (i = 0; i < str_p_loc_drc_coefficients_uni_drc->gain_set_count; i++) {
1701       err = impd_parse_gain_set_params(
1702           it_bit_buff, version, &gain_seq_idx,
1703           &(str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]));
1704       if (err) return (err);
1705 
1706       if (str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1707               .time_delt_min_flag) {
1708         if (str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1709                 .time_delt_min_val > ia_drc_params_struct->drc_frame_size) {
1710           /* drc time interval too big */
1711           return (PARAM_ERROR);
1712         }
1713         str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1714             .num_gain_max_values =
1715             ia_drc_params_struct->drc_frame_size /
1716             str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1717                 .time_delt_min_val;
1718         if (str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1719                 .num_gain_max_values >
1720             (N_DELTA_TIME_CODE_TABLE_ENTRIES_MAX / 2 - 1))
1721           return (UNEXPECTED_ERROR);
1722         err = impd_init_tbls(
1723             str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1724                 .num_gain_max_values,
1725             &(str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1726                   .str_tables));
1727         if (err) return (err);
1728       }
1729       gain_sequence_count +=
1730           str_p_loc_drc_coefficients_uni_drc->gain_set_params[i].band_count;
1731     }
1732     str_p_loc_drc_coefficients_uni_drc->gain_sequence_count =
1733         gain_sequence_count;
1734   } else {
1735     ia_shape_filter_block_params_struct* pstr_shape_filter_block_params;
1736     for (i = 0; i < SEQUENCE_COUNT_MAX; i++) {
1737       str_p_loc_drc_coefficients_uni_drc
1738           ->gain_set_params_index_for_gain_sequence[i] = -1;
1739     }
1740 
1741     temp = impd_read_bits_buf(it_bit_buff, 5);
1742     if (it_bit_buff->error) return it_bit_buff->error;
1743 
1744     str_p_loc_drc_coefficients_uni_drc->drc_location = (temp >> 1) & 0xf;
1745     str_p_loc_drc_coefficients_uni_drc->drc_frame_size_present = temp & 1;
1746 
1747     if (str_p_loc_drc_coefficients_uni_drc->drc_frame_size_present == 1) {
1748       drc_frame_size = impd_read_bits_buf(it_bit_buff, 15);
1749       if (it_bit_buff->error) return it_bit_buff->error;
1750       str_p_loc_drc_coefficients_uni_drc->drc_frame_size = drc_frame_size + 1;
1751     }
1752     str_p_loc_drc_coefficients_uni_drc->drc_characteristic_left_present =
1753         impd_read_bits_buf(it_bit_buff, 1);
1754     if (it_bit_buff->error) return it_bit_buff->error;
1755     if (str_p_loc_drc_coefficients_uni_drc->drc_characteristic_left_present ==
1756         1) {
1757       str_p_loc_drc_coefficients_uni_drc->characteristic_left_count =
1758           impd_read_bits_buf(it_bit_buff, 4);
1759       if (it_bit_buff->error) return it_bit_buff->error;
1760 
1761       if (str_p_loc_drc_coefficients_uni_drc->characteristic_left_count >
1762           SPLIT_CHARACTERISTIC_COUNT_MAX)
1763         return (UNEXPECTED_ERROR);
1764 
1765       for (i = 1;
1766            i <= str_p_loc_drc_coefficients_uni_drc->characteristic_left_count;
1767            i++) {
1768         err = impd_parse_split_drc_characteristic(
1769             it_bit_buff, LEFT_SIDE, &(str_p_loc_drc_coefficients_uni_drc
1770                                           ->str_split_characteristic_left[i]));
1771         if (err) return (err);
1772       }
1773     }
1774     str_p_loc_drc_coefficients_uni_drc->drc_characteristic_right_present =
1775         impd_read_bits_buf(it_bit_buff, 1);
1776     if (it_bit_buff->error) return it_bit_buff->error;
1777     if (str_p_loc_drc_coefficients_uni_drc->drc_characteristic_right_present ==
1778         1) {
1779       str_p_loc_drc_coefficients_uni_drc->characteristic_right_count =
1780           impd_read_bits_buf(it_bit_buff, 4);
1781       if (it_bit_buff->error) return it_bit_buff->error;
1782 
1783       if (str_p_loc_drc_coefficients_uni_drc->characteristic_right_count >
1784           SPLIT_CHARACTERISTIC_COUNT_MAX)
1785         return (UNEXPECTED_ERROR);
1786       for (i = 1;
1787            i <= str_p_loc_drc_coefficients_uni_drc->characteristic_right_count;
1788            i++) {
1789         err = impd_parse_split_drc_characteristic(
1790             it_bit_buff, RIGHT_SIDE,
1791             &(str_p_loc_drc_coefficients_uni_drc
1792                   ->str_split_characteristic_right[i]));
1793         if (err) return (err);
1794       }
1795     }
1796     str_p_loc_drc_coefficients_uni_drc->shape_filters_present =
1797         impd_read_bits_buf(it_bit_buff, 1);
1798     if (it_bit_buff->error) return it_bit_buff->error;
1799     if (str_p_loc_drc_coefficients_uni_drc->shape_filters_present == 1) {
1800       str_p_loc_drc_coefficients_uni_drc->shape_num_filter =
1801           impd_read_bits_buf(it_bit_buff, 4);
1802       if (it_bit_buff->error) return it_bit_buff->error;
1803       if (str_p_loc_drc_coefficients_uni_drc->shape_num_filter >
1804           SHAPE_FILTER_COUNT_MAX)
1805         return (UNEXPECTED_ERROR);
1806       for (i = 1; i <= str_p_loc_drc_coefficients_uni_drc->shape_num_filter;
1807            i++) {
1808         pstr_shape_filter_block_params =
1809             &(str_p_loc_drc_coefficients_uni_drc
1810                   ->str_shape_filter_block_params[i]);
1811         pstr_shape_filter_block_params->lf_cut_filter_present =
1812             impd_read_bits_buf(it_bit_buff, 1);
1813         if (it_bit_buff->error) return it_bit_buff->error;
1814 
1815         if (pstr_shape_filter_block_params->lf_cut_filter_present == 1) {
1816           temp = impd_read_bits_buf(it_bit_buff, 5);
1817           if (it_bit_buff->error) return it_bit_buff->error;
1818 
1819           pstr_shape_filter_block_params->str_lf_cut_params.corner_freq_index =
1820               (temp >> 2) & 7;
1821           pstr_shape_filter_block_params->str_lf_cut_params
1822               .filter_strength_index = temp & 3;
1823         }
1824         pstr_shape_filter_block_params->lf_boost_filter_present =
1825             impd_read_bits_buf(it_bit_buff, 1);
1826         if (it_bit_buff->error) return it_bit_buff->error;
1827         if (pstr_shape_filter_block_params->lf_boost_filter_present == 1) {
1828           temp = impd_read_bits_buf(it_bit_buff, 5);
1829           if (it_bit_buff->error) return it_bit_buff->error;
1830 
1831           pstr_shape_filter_block_params->str_lf_boost_params
1832               .corner_freq_index = (temp >> 2) & 7;
1833           pstr_shape_filter_block_params->str_lf_boost_params
1834               .filter_strength_index = temp & 3;
1835         }
1836         pstr_shape_filter_block_params->hf_cut_filter_present =
1837             impd_read_bits_buf(it_bit_buff, 1);
1838         if (it_bit_buff->error) return it_bit_buff->error;
1839         if (pstr_shape_filter_block_params->hf_cut_filter_present == 1) {
1840           temp = impd_read_bits_buf(it_bit_buff, 5);
1841           if (it_bit_buff->error) return it_bit_buff->error;
1842 
1843           pstr_shape_filter_block_params->str_hfCutParams.corner_freq_index =
1844               (temp >> 2) & 7;
1845           pstr_shape_filter_block_params->str_hfCutParams
1846               .filter_strength_index = temp & 3;
1847         }
1848         pstr_shape_filter_block_params->hf_boost_filter_present =
1849             impd_read_bits_buf(it_bit_buff, 1);
1850         if (it_bit_buff->error) return it_bit_buff->error;
1851         if (pstr_shape_filter_block_params->hf_boost_filter_present == 1) {
1852           temp = impd_read_bits_buf(it_bit_buff, 5);
1853           if (it_bit_buff->error) return it_bit_buff->error;
1854 
1855           pstr_shape_filter_block_params->str_hf_boost_params
1856               .corner_freq_index = (temp >> 2) & 7;
1857           pstr_shape_filter_block_params->str_hf_boost_params
1858               .filter_strength_index = temp & 3;
1859         }
1860       }
1861     }
1862 
1863     temp = impd_read_bits_buf(it_bit_buff, 12);
1864     if (it_bit_buff->error) return it_bit_buff->error;
1865 
1866     str_p_loc_drc_coefficients_uni_drc->gain_sequence_count =
1867         (temp >> 6) & 0x3f;
1868 
1869     if (str_p_loc_drc_coefficients_uni_drc->gain_sequence_count >
1870         SEQUENCE_COUNT_MAX)
1871       return UNEXPECTED_ERROR;
1872 
1873     str_p_loc_drc_coefficients_uni_drc->gain_set_count = temp & 0x3f;
1874 
1875     if (str_p_loc_drc_coefficients_uni_drc->gain_set_count > GAIN_SET_COUNT_MAX)
1876       return (UNEXPECTED_ERROR);
1877 
1878     str_p_loc_drc_coefficients_uni_drc->gain_set_count_plus =
1879         str_p_loc_drc_coefficients_uni_drc->gain_set_count;
1880     for (i = 0; i < str_p_loc_drc_coefficients_uni_drc->gain_set_count; i++) {
1881       err = impd_parse_gain_set_params(
1882           it_bit_buff, version, &gain_seq_idx,
1883           &(str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]));
1884       if (err) return (err);
1885 
1886       if (str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1887               .time_delt_min_flag) {
1888         if (str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1889                 .time_delt_min_val > ia_drc_params_struct->drc_frame_size) {
1890           /* drc time interval too big */
1891           return (PARAM_ERROR);
1892         }
1893         str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1894             .num_gain_max_values =
1895             ia_drc_params_struct->drc_frame_size /
1896             str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1897                 .time_delt_min_val;
1898         if (str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1899                 .num_gain_max_values >
1900             (N_DELTA_TIME_CODE_TABLE_ENTRIES_MAX / 2 - 1))
1901           return (UNEXPECTED_ERROR);
1902 
1903         err = impd_init_tbls(
1904             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         if (err) return (err);
1909       }
1910     }
1911 
1912     for (i = 0; i < str_p_loc_drc_coefficients_uni_drc->gain_set_count; i++) {
1913       WORD32 b;
1914       for (b = 0;
1915            b <
1916            str_p_loc_drc_coefficients_uni_drc->gain_set_params[i].band_count;
1917            b++) {
1918         str_p_loc_drc_coefficients_uni_drc
1919             ->gain_set_params_index_for_gain_sequence
1920                 [str_p_loc_drc_coefficients_uni_drc->gain_set_params[i]
1921                      .gain_params[b]
1922                      .gain_seq_idx] = i;
1923       }
1924     }
1925   }
1926   return (0);
1927 }
1928 
1929 WORD32
impd_drc_parse_instructions_basic(ia_bit_buf_struct * it_bit_buff,ia_drc_instructions_basic_struct * str_drc_instructions_basic)1930 impd_drc_parse_instructions_basic(
1931     ia_bit_buf_struct* it_bit_buff,
1932     ia_drc_instructions_basic_struct* str_drc_instructions_basic) {
1933   // WORD32 err = 0;
1934   WORD32 i, limiter_peak_target, temp;
1935   WORD32 additional_dmix_id_present, additional_dmix_id_cnt;
1936 
1937   temp = impd_read_bits_buf(it_bit_buff, 18);
1938   if (it_bit_buff->error) return it_bit_buff->error;
1939 
1940   str_drc_instructions_basic->drc_set_id = (temp >> 12) & 0x3f;
1941   str_drc_instructions_basic->drc_location = (temp >> 8) & 0xf;
1942   str_drc_instructions_basic->downmix_id[0] = (temp >> 1) & 0x7f;
1943   additional_dmix_id_present = temp & 1;
1944   str_drc_instructions_basic->dwnmix_id_count = 1;
1945 
1946   if (additional_dmix_id_present) {
1947     additional_dmix_id_cnt = impd_read_bits_buf(it_bit_buff, 3);
1948     if (it_bit_buff->error) return it_bit_buff->error;
1949     for (i = 0; i < additional_dmix_id_cnt; i++) {
1950       str_drc_instructions_basic->downmix_id[i + 1] =
1951           impd_read_bits_buf(it_bit_buff, 7);
1952       if (it_bit_buff->error) return it_bit_buff->error;
1953     }
1954     str_drc_instructions_basic->dwnmix_id_count = 1 + additional_dmix_id_cnt;
1955   }
1956 
1957   str_drc_instructions_basic->drc_set_effect =
1958       impd_read_bits_buf(it_bit_buff, 16);
1959   if (it_bit_buff->error) return it_bit_buff->error;
1960 
1961   if ((str_drc_instructions_basic->drc_set_effect &
1962        (EFFECT_BIT_DUCK_OTHER | EFFECT_BIT_DUCK_SELF)) == 0) {
1963     str_drc_instructions_basic->limiter_peak_target_present =
1964         impd_read_bits_buf(it_bit_buff, 1);
1965     if (it_bit_buff->error) return it_bit_buff->error;
1966     if (str_drc_instructions_basic->limiter_peak_target_present) {
1967       limiter_peak_target = impd_read_bits_buf(it_bit_buff, 8);
1968       if (it_bit_buff->error) return it_bit_buff->error;
1969       str_drc_instructions_basic->limiter_peak_target =
1970           -limiter_peak_target * 0.125f;
1971     }
1972   }
1973 
1974   str_drc_instructions_basic->drc_set_target_loudness_present =
1975       impd_read_bits_buf(it_bit_buff, 1);
1976   if (it_bit_buff->error) return it_bit_buff->error;
1977 
1978   str_drc_instructions_basic->drc_set_target_loudness_value_upper = 0;
1979   str_drc_instructions_basic->drc_set_target_loudness_value_lower = -63;
1980 
1981   if (str_drc_instructions_basic->drc_set_target_loudness_present == 1) {
1982     WORD32 bsDrcSetTargetLoudnessValueUpper, bsDrcSetTargetLoudnessValueLower;
1983     bsDrcSetTargetLoudnessValueUpper = impd_read_bits_buf(it_bit_buff, 6);
1984     if (it_bit_buff->error) return it_bit_buff->error;
1985     str_drc_instructions_basic->drc_set_target_loudness_value_upper =
1986         bsDrcSetTargetLoudnessValueUpper - 63;
1987 
1988     str_drc_instructions_basic->drc_set_target_loudness_value_lower_present =
1989         impd_read_bits_buf(it_bit_buff, 1);
1990     if (it_bit_buff->error) return it_bit_buff->error;
1991 
1992     if (str_drc_instructions_basic
1993             ->drc_set_target_loudness_value_lower_present == 1) {
1994       bsDrcSetTargetLoudnessValueLower = impd_read_bits_buf(it_bit_buff, 6);
1995       if (it_bit_buff->error) return it_bit_buff->error;
1996       str_drc_instructions_basic->drc_set_target_loudness_value_lower =
1997           bsDrcSetTargetLoudnessValueLower - 63;
1998     }
1999   }
2000 
2001   return (0);
2002 }
2003 
2004 WORD32
impd_dec_ducking_scaling(ia_bit_buf_struct * it_bit_buff,WORD32 * ducking_scaling_flag,FLOAT32 * p_ducking_scaling)2005 impd_dec_ducking_scaling(ia_bit_buf_struct* it_bit_buff,
2006                          WORD32* ducking_scaling_flag,
2007                          FLOAT32* p_ducking_scaling) {
2008   WORD32 ducking_scaling_present, ducking_scaling, sigma, mu;
2009 
2010   ducking_scaling_present = impd_read_bits_buf(it_bit_buff, 1);
2011   if (it_bit_buff->error) return it_bit_buff->error;
2012 
2013   if (ducking_scaling_present == 0) {
2014     *ducking_scaling_flag = 0;
2015     *p_ducking_scaling = 1.0f;
2016   } else {
2017     *ducking_scaling_flag = 1;
2018     ducking_scaling = impd_read_bits_buf(it_bit_buff, 4);
2019     if (it_bit_buff->error) return it_bit_buff->error;
2020 
2021     sigma = ducking_scaling >> 3;
2022     mu = ducking_scaling & 0x7;
2023 
2024     if (sigma == 0) {
2025       *p_ducking_scaling = 1.0f + 0.125f * (1.0f + mu);
2026     } else {
2027       *p_ducking_scaling = 1.0f - 0.125f * (1.0f + mu);
2028     }
2029   }
2030   return (0);
2031 }
2032 
2033 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)2034 impd_parse_drc_instructions_uni_drc(
2035     ia_bit_buf_struct* it_bit_buff, WORD32 version, ia_drc_config* drc_config,
2036     ia_drc_instructions_struct* str_drc_instruction_str) {
2037   WORD32 err = 0, i, n, k, g, c, limiter_peak_target, idx;
2038   WORD32 additional_dmix_id_present, additional_dmix_id_cnt;
2039   ia_uni_drc_coeffs_struct* str_p_loc_drc_coefficients_uni_drc = NULL;
2040   WORD32 ch_cnt;
2041   WORD32 unique_idx[MAX_CHANNEL_COUNT];
2042   FLOAT32 unique_scaling[MAX_CHANNEL_COUNT];
2043   WORD32 match;
2044   WORD32 dmix_id_present;
2045   WORD32 repeat_parameters, repeat_parameters_cnt;
2046   WORD32 ducking_sequence;
2047   FLOAT32 factor;
2048 
2049   str_drc_instruction_str->drc_set_id = impd_read_bits_buf(it_bit_buff, 6);
2050   if (it_bit_buff->error) return it_bit_buff->error;
2051   if (str_drc_instruction_str->drc_set_id >= DRC_INSTRUCTIONS_COUNT_MAX)
2052     return UNEXPECTED_ERROR;
2053   if (version == 0) {
2054     str_drc_instruction_str->drc_set_complexity_level =
2055         DRC_COMPLEXITY_LEVEL_MAX;
2056   } else {
2057     str_drc_instruction_str->drc_set_complexity_level =
2058         impd_read_bits_buf(it_bit_buff, 4);
2059     if (it_bit_buff->error) return it_bit_buff->error;
2060   }
2061   str_drc_instruction_str->drc_location = impd_read_bits_buf(it_bit_buff, 4);
2062   if (it_bit_buff->error) return it_bit_buff->error;
2063   dmix_id_present = 1;
2064   if (version >= 1) {
2065     dmix_id_present = impd_read_bits_buf(it_bit_buff, 1);
2066     if (it_bit_buff->error) return it_bit_buff->error;
2067   }
2068   if (dmix_id_present == 1) {
2069     str_drc_instruction_str->downmix_id[0] = impd_read_bits_buf(it_bit_buff, 7);
2070     if (it_bit_buff->error) return it_bit_buff->error;
2071     if (version >= 1) {
2072       str_drc_instruction_str->drc_apply_to_dwnmix =
2073           impd_read_bits_buf(it_bit_buff, 1);
2074       if (it_bit_buff->error) return it_bit_buff->error;
2075     }
2076     if (version == 0) {
2077       if (str_drc_instruction_str->downmix_id[0] == 0) {
2078         str_drc_instruction_str->drc_apply_to_dwnmix = 0;
2079       } else {
2080         str_drc_instruction_str->drc_apply_to_dwnmix = 1;
2081       }
2082     }
2083     additional_dmix_id_present = impd_read_bits_buf(it_bit_buff, 1);
2084     if (it_bit_buff->error) return it_bit_buff->error;
2085 
2086     if (additional_dmix_id_present) {
2087       additional_dmix_id_cnt = impd_read_bits_buf(it_bit_buff, 3);
2088       if (it_bit_buff->error) return it_bit_buff->error;
2089       for (i = 0; i < additional_dmix_id_cnt; i++) {
2090         str_drc_instruction_str->downmix_id[i + 1] =
2091             impd_read_bits_buf(it_bit_buff, 7);
2092         if (it_bit_buff->error) return it_bit_buff->error;
2093       }
2094       str_drc_instruction_str->dwnmix_id_count = 1 + additional_dmix_id_cnt;
2095     } else {
2096       str_drc_instruction_str->dwnmix_id_count = 1;
2097     }
2098   } else {
2099     str_drc_instruction_str->downmix_id[0] = 0;
2100     str_drc_instruction_str->dwnmix_id_count = 1;
2101   }
2102 
2103   str_drc_instruction_str->drc_set_effect = impd_read_bits_buf(it_bit_buff, 16);
2104   if (it_bit_buff->error) return it_bit_buff->error;
2105 
2106   if ((str_drc_instruction_str->drc_set_effect &
2107        (EFFECT_BIT_DUCK_OTHER | EFFECT_BIT_DUCK_SELF)) == 0) {
2108     str_drc_instruction_str->limiter_peak_target_present =
2109         impd_read_bits_buf(it_bit_buff, 1);
2110     if (it_bit_buff->error) return it_bit_buff->error;
2111     if (str_drc_instruction_str->limiter_peak_target_present) {
2112       limiter_peak_target = impd_read_bits_buf(it_bit_buff, 8);
2113       if (it_bit_buff->error) return it_bit_buff->error;
2114       str_drc_instruction_str->limiter_peak_target =
2115           -limiter_peak_target * 0.125f;
2116     }
2117   }
2118 
2119   str_drc_instruction_str->drc_set_target_loudness_present =
2120       impd_read_bits_buf(it_bit_buff, 1);
2121   if (it_bit_buff->error) return it_bit_buff->error;
2122 
2123   str_drc_instruction_str->drc_set_target_loudness_value_upper = 0;
2124   str_drc_instruction_str->drc_set_target_loudness_value_lower = -63;
2125 
2126   if (str_drc_instruction_str->drc_set_target_loudness_present == 1) {
2127     WORD32 bsDrcSetTargetLoudnessValueUpper, bsDrcSetTargetLoudnessValueLower;
2128     bsDrcSetTargetLoudnessValueUpper = impd_read_bits_buf(it_bit_buff, 6);
2129     if (it_bit_buff->error) return it_bit_buff->error;
2130     str_drc_instruction_str->drc_set_target_loudness_value_upper =
2131         bsDrcSetTargetLoudnessValueUpper - 63;
2132     str_drc_instruction_str->drc_set_target_loudness_value_lower_present =
2133         impd_read_bits_buf(it_bit_buff, 1);
2134     if (it_bit_buff->error) return it_bit_buff->error;
2135     if (str_drc_instruction_str->drc_set_target_loudness_value_lower_present ==
2136         1) {
2137       bsDrcSetTargetLoudnessValueLower = impd_read_bits_buf(it_bit_buff, 6);
2138       if (it_bit_buff->error) return it_bit_buff->error;
2139       str_drc_instruction_str->drc_set_target_loudness_value_lower =
2140           bsDrcSetTargetLoudnessValueLower - 63;
2141     }
2142   }
2143 
2144   str_drc_instruction_str->depends_on_drc_set_present =
2145       impd_read_bits_buf(it_bit_buff, 1);
2146   if (it_bit_buff->error) return it_bit_buff->error;
2147 
2148   str_drc_instruction_str->no_independent_use = 0;
2149   if (str_drc_instruction_str->depends_on_drc_set_present) {
2150     str_drc_instruction_str->depends_on_drc_set =
2151         impd_read_bits_buf(it_bit_buff, 6);
2152     if (it_bit_buff->error) return it_bit_buff->error;
2153   } else {
2154     str_drc_instruction_str->no_independent_use =
2155         impd_read_bits_buf(it_bit_buff, 1);
2156     if (it_bit_buff->error) return it_bit_buff->error;
2157   }
2158   if (version == 0) {
2159     str_drc_instruction_str->requires_eq = 0;
2160   } else {
2161     str_drc_instruction_str->requires_eq = impd_read_bits_buf(it_bit_buff, 1);
2162     if (it_bit_buff->error) return it_bit_buff->error;
2163   }
2164 
2165   err = impd_sel_drc_coeff(drc_config, str_drc_instruction_str->drc_location,
2166                            &str_p_loc_drc_coefficients_uni_drc);
2167   if (err) return (err);
2168 
2169   ch_cnt = drc_config->channel_layout.base_channel_count;
2170 
2171   if (ch_cnt > MAX_CHANNEL_COUNT) return (UNEXPECTED_ERROR);
2172   for (c = 0; c < MAX_CHANNEL_COUNT; c++) {
2173     unique_idx[c] = -10;
2174     unique_scaling[c] = -10.0f;
2175   }
2176 
2177   if (str_drc_instruction_str->drc_set_effect &
2178       (EFFECT_BIT_DUCK_OTHER | EFFECT_BIT_DUCK_SELF)) {
2179     c = 0;
2180     while (c < ch_cnt) {
2181       WORD32 bs_gain_set_idx;
2182       bs_gain_set_idx = impd_read_bits_buf(it_bit_buff, 6);
2183       if (it_bit_buff->error) return it_bit_buff->error;
2184       if (bs_gain_set_idx > GAIN_SET_COUNT_MAX) return UNEXPECTED_ERROR;
2185       str_drc_instruction_str->gain_set_index[c] = bs_gain_set_idx - 1;
2186       impd_dec_ducking_scaling(
2187           it_bit_buff,
2188           &(str_drc_instruction_str->str_ducking_modifiers_for_channel[c]
2189                 .ducking_scaling_flag),
2190           &(str_drc_instruction_str->str_ducking_modifiers_for_channel[c]
2191                 .ducking_scaling));
2192 
2193       c++;
2194 
2195       repeat_parameters = impd_read_bits_buf(it_bit_buff, 1);
2196       if (it_bit_buff->error) return it_bit_buff->error;
2197 
2198       if (repeat_parameters == 1) {
2199         repeat_parameters_cnt = impd_read_bits_buf(it_bit_buff, 5);
2200         if (it_bit_buff->error) return it_bit_buff->error;
2201 
2202         repeat_parameters_cnt += 1;
2203         if ((c + repeat_parameters_cnt) > MAX_CHANNEL_COUNT)
2204           return (UNEXPECTED_ERROR);
2205         for (k = 0; k < repeat_parameters_cnt; k++) {
2206           str_drc_instruction_str->gain_set_index[c] =
2207               str_drc_instruction_str->gain_set_index[c - 1];
2208           str_drc_instruction_str->str_ducking_modifiers_for_channel[c] =
2209               str_drc_instruction_str->str_ducking_modifiers_for_channel[c - 1];
2210           c++;
2211         }
2212       }
2213     }
2214     if (c > ch_cnt) {
2215       return (UNEXPECTED_ERROR);
2216     }
2217     ducking_sequence = -1;
2218     g = 0;
2219     if (str_drc_instruction_str->drc_set_effect & EFFECT_BIT_DUCK_OTHER) {
2220       for (c = 0; c < ch_cnt; c++) {
2221         match = 0;
2222         idx = str_drc_instruction_str->gain_set_index[c];
2223         factor = str_drc_instruction_str->str_ducking_modifiers_for_channel[c]
2224                      .ducking_scaling;
2225         if (idx < 0) {
2226           for (n = 0; n < g; n++) {
2227             if (unique_scaling[n] == factor) {
2228               match = 1;
2229               str_drc_instruction_str->channel_group_of_ch[c] = n;
2230               break;
2231             }
2232           }
2233           if (match == 0) {
2234             unique_idx[g] = idx;
2235             unique_scaling[g] = factor;
2236             str_drc_instruction_str->channel_group_of_ch[c] = g;
2237             g++;
2238           }
2239         } else {
2240           if ((ducking_sequence > 0) && (ducking_sequence != idx)) {
2241             /* drc for ducking can have only one ducking sequence */
2242             return (UNEXPECTED_ERROR);
2243           }
2244           ducking_sequence = idx;
2245           str_drc_instruction_str->channel_group_of_ch[c] = -1;
2246         }
2247       }
2248       str_drc_instruction_str->num_drc_ch_groups = g;
2249       if (ducking_sequence == -1) {
2250         /* ducking sequence not found */
2251         return (UNEXPECTED_ERROR);
2252       }
2253     } else if (str_drc_instruction_str->drc_set_effect & EFFECT_BIT_DUCK_SELF) {
2254       for (c = 0; c < ch_cnt; c++) {
2255         match = 0;
2256         idx = str_drc_instruction_str->gain_set_index[c];
2257         factor = str_drc_instruction_str->str_ducking_modifiers_for_channel[c]
2258                      .ducking_scaling;
2259         if (idx >= 0) {
2260           for (n = 0; n < g; n++) {
2261             if ((unique_idx[n] == idx) && (unique_scaling[n] == factor)) {
2262               match = 1;
2263               str_drc_instruction_str->channel_group_of_ch[c] = n;
2264               break;
2265             }
2266           }
2267           if (match == 0) {
2268             unique_idx[g] = idx;
2269             unique_scaling[g] = factor;
2270             str_drc_instruction_str->channel_group_of_ch[c] = g;
2271             g++;
2272           }
2273         } else {
2274           str_drc_instruction_str->channel_group_of_ch[c] = -1;
2275         }
2276       }
2277       str_drc_instruction_str->num_drc_ch_groups = g;
2278     }
2279 
2280     if (str_drc_instruction_str->num_drc_ch_groups >
2281         min(CHANNEL_GROUP_COUNT_MAX, MAX_CHANNEL_COUNT))
2282       return UNEXPECTED_ERROR;
2283     for (g = 0; g < str_drc_instruction_str->num_drc_ch_groups; g++) {
2284       WORD32 set =
2285           (str_drc_instruction_str->drc_set_effect & EFFECT_BIT_DUCK_OTHER)
2286               ? ducking_sequence
2287               : unique_idx[g];
2288       if (set < 0) return UNEXPECTED_ERROR;
2289       str_drc_instruction_str->gain_set_index_for_channel_group[g] = set;
2290       str_drc_instruction_str->str_ducking_modifiers_for_channel_group[g]
2291           .ducking_scaling = unique_scaling[g];
2292       if (unique_scaling[g] != 1.0f) {
2293         str_drc_instruction_str->str_ducking_modifiers_for_channel_group[g]
2294             .ducking_scaling_flag = 1;
2295       } else {
2296         str_drc_instruction_str->str_ducking_modifiers_for_channel_group[g]
2297             .ducking_scaling_flag = 0;
2298       }
2299       str_drc_instruction_str->band_count_of_ch_group[g] = 1;
2300     }
2301   } else {
2302     if (((version == 0) ||
2303          (str_drc_instruction_str->drc_apply_to_dwnmix != 0)) &&
2304         (str_drc_instruction_str->downmix_id[0] != 0) &&
2305         (str_drc_instruction_str->downmix_id[0] != ID_FOR_ANY_DOWNMIX) &&
2306         (str_drc_instruction_str->dwnmix_id_count == 1)) {
2307       for (i = 0; i < drc_config->dwnmix_instructions_count; i++) {
2308         if (str_drc_instruction_str->downmix_id[0] ==
2309             drc_config->dwnmix_instructions[i].downmix_id)
2310           break;
2311       }
2312       if (i == drc_config->dwnmix_instructions_count) {
2313         /* dwnmix_instructions not found */
2314         return (UNEXPECTED_ERROR);
2315       }
2316       ch_cnt = drc_config->dwnmix_instructions[i].target_channel_count;
2317     } else if (((version == 0) ||
2318                 (str_drc_instruction_str->drc_apply_to_dwnmix != 0)) &&
2319                ((str_drc_instruction_str->downmix_id[0] ==
2320                  ID_FOR_ANY_DOWNMIX) ||
2321                 (str_drc_instruction_str->dwnmix_id_count > 1))) {
2322       ch_cnt = 1;
2323     }
2324 
2325     if (ch_cnt > MAX_CHANNEL_COUNT) return (UNEXPECTED_ERROR);
2326     c = 0;
2327     while (c < ch_cnt) {
2328       WORD32 bs_gain_set_idx;
2329       WORD32 repeat_gain_set_idx, repeat_gain_set_idx_cnt, temp;
2330 
2331       temp = impd_read_bits_buf(it_bit_buff, 7);
2332       if (it_bit_buff->error) return it_bit_buff->error;
2333 
2334       bs_gain_set_idx = (temp >> 1) & 0x7f;
2335       repeat_gain_set_idx = temp & 1;
2336 
2337       if (bs_gain_set_idx > GAIN_SET_COUNT_MAX) return UNEXPECTED_ERROR;
2338 
2339       str_drc_instruction_str->gain_set_index[c] = bs_gain_set_idx - 1;
2340       c++;
2341 
2342       if (repeat_gain_set_idx == 1) {
2343         repeat_gain_set_idx_cnt = impd_read_bits_buf(it_bit_buff, 5);
2344         if (it_bit_buff->error) return it_bit_buff->error;
2345 
2346         repeat_gain_set_idx_cnt += 1;
2347         if ((c + repeat_gain_set_idx_cnt) > MAX_CHANNEL_COUNT)
2348           return (UNEXPECTED_ERROR);
2349         for (k = 0; k < repeat_gain_set_idx_cnt; k++) {
2350           str_drc_instruction_str->gain_set_index[c] = bs_gain_set_idx - 1;
2351           c++;
2352         }
2353       }
2354     }
2355     if (c > ch_cnt) {
2356       return (UNEXPECTED_ERROR);
2357     }
2358 
2359     g = 0;
2360     if ((str_drc_instruction_str->downmix_id[0] == ID_FOR_ANY_DOWNMIX) ||
2361         (str_drc_instruction_str->dwnmix_id_count > 1)) {
2362       WORD32 idx = str_drc_instruction_str->gain_set_index[0];
2363       if (idx >= 0) {
2364         unique_idx[0] = idx;
2365         g = 1;
2366       }
2367     } else {
2368       for (c = 0; c < ch_cnt; c++) {
2369         WORD32 idx = str_drc_instruction_str->gain_set_index[c];
2370         match = 0;
2371         if (idx >= 0) {
2372           for (n = 0; n < g; n++) {
2373             if (unique_idx[n] == idx) {
2374               match = 1;
2375               str_drc_instruction_str->channel_group_of_ch[c] = n;
2376               break;
2377             }
2378           }
2379           if (match == 0) {
2380             unique_idx[g] = idx;
2381             str_drc_instruction_str->channel_group_of_ch[c] = g;
2382             g++;
2383           }
2384         } else {
2385           str_drc_instruction_str->channel_group_of_ch[c] = -1;
2386         }
2387       }
2388     }
2389 
2390     str_drc_instruction_str->num_drc_ch_groups = g;
2391 
2392     if (str_drc_instruction_str->num_drc_ch_groups >
2393         min(CHANNEL_GROUP_COUNT_MAX, MAX_CHANNEL_COUNT))
2394       return UNEXPECTED_ERROR;
2395     for (g = 0; g < str_drc_instruction_str->num_drc_ch_groups; g++) {
2396       WORD32 set, band_count;
2397 
2398       set = unique_idx[g];
2399       if (set < 0) return UNEXPECTED_ERROR;
2400       str_drc_instruction_str->gain_set_index_for_channel_group[g] = set;
2401 
2402       if (str_p_loc_drc_coefficients_uni_drc != NULL &&
2403           set < str_p_loc_drc_coefficients_uni_drc->gain_set_count) {
2404         band_count =
2405             str_p_loc_drc_coefficients_uni_drc->gain_set_params[set].band_count;
2406       } else {
2407         band_count = 1;
2408       }
2409 
2410       err = impd_dec_gain_modifiers(
2411           it_bit_buff, version, band_count,
2412           &(str_drc_instruction_str->str_gain_modifiers_of_ch_group[g]));
2413       if (err) return (err);
2414     }
2415   }
2416 
2417   return (0);
2418 }
2419 WORD32
impd_parse_loudness_info(ia_bit_buf_struct * it_bit_buff,WORD32 version,ia_loudness_info_struct * loudness_info)2420 impd_parse_loudness_info(ia_bit_buf_struct* it_bit_buff, WORD32 version,
2421                          ia_loudness_info_struct* loudness_info) {
2422   WORD32 err = 0, sample_peak_level, true_peak_level, i, temp;
2423 
2424   loudness_info->drc_set_id = impd_read_bits_buf(it_bit_buff, 6);
2425   if (it_bit_buff->error) return it_bit_buff->error;
2426 
2427   if (version >= 1) {
2428     loudness_info->eq_set_id = impd_read_bits_buf(it_bit_buff, 6);
2429     if (it_bit_buff->error) return it_bit_buff->error;
2430   } else {
2431     loudness_info->eq_set_id = 0;
2432   }
2433 
2434   temp = impd_read_bits_buf(it_bit_buff, 8);
2435   if (it_bit_buff->error) return it_bit_buff->error;
2436 
2437   loudness_info->downmix_id = (temp >> 1) & 0x7f;
2438   loudness_info->sample_peak_level_present = temp & 1;
2439 
2440   if (loudness_info->sample_peak_level_present) {
2441     sample_peak_level = impd_read_bits_buf(it_bit_buff, 12);
2442     if (it_bit_buff->error) return it_bit_buff->error;
2443 
2444     if (sample_peak_level == 0) {
2445       loudness_info->sample_peak_level_present = 0;
2446       loudness_info->sample_peak_level = 0.0f;
2447     } else {
2448       loudness_info->sample_peak_level = 20.0f - sample_peak_level * 0.03125f;
2449     }
2450   }
2451 
2452   loudness_info->true_peak_level_present = impd_read_bits_buf(it_bit_buff, 1);
2453   if (it_bit_buff->error) return it_bit_buff->error;
2454 
2455   if (loudness_info->true_peak_level_present) {
2456     true_peak_level = impd_read_bits_buf(it_bit_buff, 12);
2457     if (it_bit_buff->error) return it_bit_buff->error;
2458 
2459     if (true_peak_level == 0) {
2460       loudness_info->true_peak_level_present = 0;
2461       loudness_info->true_peak_level = 0.0f;
2462     } else {
2463       loudness_info->true_peak_level = 20.0f - true_peak_level * 0.03125f;
2464     }
2465 
2466     temp = impd_read_bits_buf(it_bit_buff, 6);
2467     if (it_bit_buff->error) return it_bit_buff->error;
2468 
2469     /* Parsed but unused */
2470     loudness_info->true_peak_level_measurement_system = (temp >> 2) & 0xf;
2471     /* Parsed but unused */
2472     loudness_info->true_peak_level_reliability = temp & 3;
2473   }
2474 
2475   loudness_info->measurement_count = impd_read_bits_buf(it_bit_buff, 4);
2476   if (it_bit_buff->error) return it_bit_buff->error;
2477 
2478   for (i = 0; i < loudness_info->measurement_count; i++) {
2479     err = impd_parse_loudness_measure(it_bit_buff,
2480                                       &(loudness_info->loudness_measure[i]));
2481     if (err) return (err);
2482   }
2483 
2484   return (0);
2485 }
2486