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