• 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 WORD32 impd_parse_loud_eq_instructions(
32     ia_bit_buf_struct* it_bit_buff,
33     ia_loud_eq_instructions_struct* loud_eq_instructions);
34 
35 WORD32 impd_parse_eq_coefficients(ia_bit_buf_struct* it_bit_buff,
36                                   ia_eq_coeff_struct* str_eq_coeff);
37 
38 WORD32 impd_parse_eq_instructions(
39     ia_bit_buf_struct* it_bit_buff, ia_drc_config* drc_config,
40     ia_eq_instructions_struct* str_eq_instructions);
impd_dec_initial_gain(ia_bit_buf_struct * it_bit_buff,const WORD32 gain_coding_profile,FLOAT32 * initial_gain)41 WORD32 impd_dec_initial_gain(ia_bit_buf_struct* it_bit_buff,
42                              const WORD32 gain_coding_profile,
43                              FLOAT32* initial_gain) {
44   WORD32 sign, magn, bit_2_extract;
45   switch (gain_coding_profile) {
46     case GAIN_CODING_PROFILE_REGULAR:
47       sign = impd_read_bits_buf(it_bit_buff, 1);
48       if (it_bit_buff->error) return it_bit_buff->error;
49       magn = impd_read_bits_buf(it_bit_buff, 8);
50       if (it_bit_buff->error) return it_bit_buff->error;
51       *initial_gain = magn * 0.125f;
52       if (sign) *initial_gain = -*initial_gain;
53       break;
54     case GAIN_CODING_PROFILE_FADING:
55     case GAIN_CODING_PROFILE_CLIPPING:
56       bit_2_extract =
57           (gain_coding_profile == GAIN_CODING_PROFILE_FADING) ? 10 : 8;
58       sign = impd_read_bits_buf(it_bit_buff, 1);
59       if (it_bit_buff->error) return it_bit_buff->error;
60       if (sign == 0)
61         *initial_gain = 0.0f;
62       else {
63         magn = impd_read_bits_buf(it_bit_buff, bit_2_extract);
64         if (it_bit_buff->error) return it_bit_buff->error;
65         *initial_gain = -(magn + 1) * 0.125f;
66       }
67       break;
68 
69     case GAIN_CODING_PROFILE_CONSTANT:
70       break;
71     default:
72       return (UNEXPECTED_ERROR);
73   }
74   return (0);
75 }
76 
impd_dec_gains(ia_bit_buf_struct * it_bit_buff,WORD32 no_nodes,WORD32 gain_coding_profile,ia_node_struct * str_node)77 WORD32 impd_dec_gains(ia_bit_buf_struct* it_bit_buff, WORD32 no_nodes,
78                       WORD32 gain_coding_profile, ia_node_struct* str_node) {
79   WORD32 err = 0, k, e, m;
80   WORD32 bit;
81   WORD32 num_bits_read;
82   WORD32 code;
83   WORD32 code_found;
84   FLOAT32 drc_gain_delta = 0;
85   const ia_delta_gain_code_table_struct* ptr_delta_gain_code_table;
86   WORD32 no_delta_gain_entries;
87 
88   err = impd_dec_initial_gain(it_bit_buff, gain_coding_profile,
89                               &(str_node[0].loc_db_gain));
90   if (err) return (err);
91 
92   impd_get_delta_gain_code_tbl(gain_coding_profile, &ptr_delta_gain_code_table,
93                                &no_delta_gain_entries);
94   for (k = 1; k < no_nodes; k++) {
95     num_bits_read = 0;
96     code = 0;
97     code_found = 0;
98     e = 0;
99     while ((e < no_delta_gain_entries) && (!code_found)) {
100       for (m = 0; m < ptr_delta_gain_code_table[e].size - num_bits_read; m++) {
101         bit = impd_read_bits_buf(it_bit_buff, 1);
102         if (it_bit_buff->error) return it_bit_buff->error;
103         code = (code << 1) + bit;
104         num_bits_read++;
105       }
106       while (num_bits_read == ptr_delta_gain_code_table[e].size) {
107         if (code == ptr_delta_gain_code_table[e].code) {
108           drc_gain_delta = ptr_delta_gain_code_table[e].value;
109           code_found = 1;
110           break;
111         }
112         e++;
113       }
114     }
115     if (code_found == 0) {
116       return (UNEXPECTED_ERROR);
117     }
118     str_node[k].loc_db_gain = str_node[k - 1].loc_db_gain + drc_gain_delta;
119   }
120   return (0);
121 }
122 
impd_dec_slopes(ia_bit_buf_struct * it_bit_buff,WORD32 * no_nodes,WORD32 gain_interpolation_type,ia_node_struct * str_node)123 WORD32 impd_dec_slopes(ia_bit_buf_struct* it_bit_buff, WORD32* no_nodes,
124                        WORD32 gain_interpolation_type,
125                        ia_node_struct* str_node) {
126   WORD32 k, e, bit;
127   WORD32 code;
128   WORD32 code_found;
129   FLOAT32 slope_value = 0;
130   bool end_marker = 0;
131   WORD32 num_bits_read;
132   const ia_slope_code_table_struct* ptr_slope_code_table;
133   WORD32 no_slope_code_entries;
134 
135   ptr_slope_code_table = &(slope_code_tbl_entries_by_size[0]);
136   no_slope_code_entries = NUM_SLOPE_TBL_ENTRIES;
137 
138   k = 0;
139   while (end_marker != 1) {
140     k++;
141     end_marker = impd_read_bits_buf(it_bit_buff, 1);
142     if (it_bit_buff->error) return it_bit_buff->error;
143   }
144   if (k > NODE_COUNT_MAX) return UNEXPECTED_ERROR;
145   *no_nodes = k;
146 
147   if (gain_interpolation_type == GAIN_INTERPOLATION_TYPE_SPLINE) {
148     for (k = 0; k < *no_nodes; k++) {
149       num_bits_read = 0;
150       code = 0;
151       code_found = 0;
152       e = 0;
153       while ((e < no_slope_code_entries) && (!code_found)) {
154         while (num_bits_read < ptr_slope_code_table[e].size) {
155           bit = impd_read_bits_buf(it_bit_buff, 1);
156           if (it_bit_buff->error) return it_bit_buff->error;
157           code = (code << 1) + bit;
158           num_bits_read++;
159         }
160         while (num_bits_read == ptr_slope_code_table[e].size) {
161           if (code == ptr_slope_code_table[e].code) {
162             slope_value = ptr_slope_code_table[e].value;
163             code_found = 1;
164             break;
165           }
166           e++;
167           if (e >= no_slope_code_entries) return UNEXPECTED_ERROR;
168         }
169       }
170       str_node[k].slope = slope_value;
171     }
172   } else {
173     for (k = 0; k < *no_nodes; k++) {
174       str_node[k].slope = 0.0f;
175     }
176   }
177   return (0);
178 }
179 
impd_dec_times(ia_bit_buf_struct * it_bit_buff,ia_tables_struct * str_tables,WORD32 num_nodes,WORD32 delta_tmin,WORD32 drc_frame_size,WORD32 full_frame,WORD32 time_offset,ia_node_struct * str_node)180 WORD32 impd_dec_times(ia_bit_buf_struct* it_bit_buff,
181                       ia_tables_struct* str_tables, WORD32 num_nodes,
182                       WORD32 delta_tmin, WORD32 drc_frame_size,
183                       WORD32 full_frame, WORD32 time_offset,
184                       ia_node_struct* str_node) {
185   WORD32 k, e, m;
186   WORD32 bit;
187   WORD32 num_bits_read;
188   WORD32 code;
189   WORD32 code_found = 0;
190   WORD32 time_delta = 0;
191   WORD32 time_offs = time_offset;
192   ia_delta_time_code_table_entry_struct* delta_time_code_table =
193       str_tables->delta_time_code_table;
194   bool frame_end_flag;
195   WORD32 node_time_tmp;
196   bool node_res_flag;
197   WORD32 exit_cnt;
198   if (full_frame == 0) {
199     frame_end_flag = impd_read_bits_buf(it_bit_buff, 1);
200     if (it_bit_buff->error) return it_bit_buff->error;
201   } else {
202     frame_end_flag = 1;
203   }
204 
205   if (frame_end_flag == 1) {
206     node_res_flag = 0;
207     for (k = 0; k < num_nodes - 1; k++) {
208       num_bits_read = 0;
209       code = 0;
210       code_found = 0;
211       exit_cnt = 0;
212       e = 1;
213       while ((e < N_DELTA_TIME_CODE_TABLE_ENTRIES_MAX) && (!code_found)) {
214         exit_cnt++;
215         if (exit_cnt > 100000) {
216           return -1;
217         }
218         for (m = 0; m < delta_time_code_table[e].size - num_bits_read; m++) {
219           bit = impd_read_bits_buf(it_bit_buff, 1);
220           if (it_bit_buff->error) return it_bit_buff->error;
221           code = (code << 1) + bit;
222           num_bits_read++;
223         }
224         while (num_bits_read == delta_time_code_table[e].size) {
225           if (code == delta_time_code_table[e].code) {
226             time_delta = delta_time_code_table[e].value;
227             code_found = 1;
228             break;
229           }
230           e++;
231         }
232       }
233       node_time_tmp = time_offs + time_delta * delta_tmin;
234       if (node_time_tmp > drc_frame_size + time_offset) {
235         if (node_res_flag == 0) {
236           str_node[k].time = drc_frame_size + time_offset;
237           node_res_flag = 1;
238         }
239         str_node[k + 1].time = node_time_tmp;
240       } else {
241         str_node[k].time = node_time_tmp;
242       }
243       time_offs = node_time_tmp;
244     }
245     if (node_res_flag == 0) {
246       str_node[k].time = drc_frame_size + time_offset;
247     }
248   } else {
249     for (k = 0; k < num_nodes; k++) {
250       num_bits_read = 0;
251       code = 0;
252       code_found = 0;
253       e = 1;
254       exit_cnt = 0;
255       while ((e < N_DELTA_TIME_CODE_TABLE_ENTRIES_MAX) && (!code_found)) {
256         exit_cnt++;
257         if (exit_cnt > 100000) {
258           return (BITSTREAM_ERROR);
259         }
260         for (m = 0; m < delta_time_code_table[e].size - num_bits_read; m++) {
261           bit = impd_read_bits_buf(it_bit_buff, 1);
262           if (it_bit_buff->error) return it_bit_buff->error;
263           code = (code << 1) + bit;
264           num_bits_read++;
265         }
266         while (num_bits_read == delta_time_code_table[e].size) {
267           if (code == delta_time_code_table[e].code) {
268             time_delta = delta_time_code_table[e].value;
269             code_found = 1;
270             break;
271           }
272           e++;
273         }
274       }
275       time_offs += time_delta * delta_tmin;
276       if (time_offs >= (2 * AUDIO_CODEC_FRAME_SIZE_MAX - drc_frame_size))
277         return UNEXPECTED_ERROR;
278       str_node[k].time = time_offs;
279     }
280   }
281   return (0);
282 }
283 
impd_drc_uni_gain_read(ia_bit_buf_struct * it_bit_buff,ia_drc_bits_dec_struct * pstr_drc_uni_bs_dec,ia_drc_config * drc_config,ia_drc_gain_struct * pstr_uni_drc_gain)284 WORD32 impd_drc_uni_gain_read(ia_bit_buf_struct* it_bit_buff,
285                               ia_drc_bits_dec_struct* pstr_drc_uni_bs_dec,
286                               ia_drc_config* drc_config,
287                               ia_drc_gain_struct* pstr_uni_drc_gain) {
288   WORD32 err = 0;
289   WORD32 seq;
290   static WORD32 pkt_loss_frame_cnt = 0;
291   ia_spline_nodes_struct* str_spline_nodes = {0};
292 
293   {
294     WORD32 gain_sequence_count =
295         drc_config->str_p_loc_drc_coefficients_uni_drc[0].gain_sequence_count;
296 
297     for (seq = 0; seq < gain_sequence_count; seq++) {
298       WORD32 index = drc_config->str_p_loc_drc_coefficients_uni_drc[0]
299                          .gain_set_params_index_for_gain_sequence[seq];
300       ia_gain_set_params_struct str_gain_set_params = {0};
301       ia_gain_set_params_struct* gain_set_params = &str_gain_set_params;
302 
303       if (index != -1) {
304         gain_set_params = &(drc_config->str_p_loc_drc_coefficients_uni_drc
305                                 ->gain_set_params[index]);
306       }
307       if (gain_set_params->gain_coding_profile ==
308           GAIN_CODING_PROFILE_CONSTANT) {
309         str_spline_nodes =
310             &(pstr_uni_drc_gain->drc_gain_sequence[seq].str_spline_nodes[0]);
311         str_spline_nodes->num_nodes = 1;
312         str_spline_nodes->str_node[0].slope = 0.0;
313         str_spline_nodes->str_node[0].time =
314             (pstr_drc_uni_bs_dec->ia_drc_params_struct).drc_frame_size - 1;
315         str_spline_nodes->str_node[0].loc_db_gain = 0.0f;
316       } else {
317         err = impd_parse_drc_gain_sequence(
318             it_bit_buff, pstr_drc_uni_bs_dec, gain_set_params,
319             &(pstr_uni_drc_gain->drc_gain_sequence[seq]));
320         if (err) return (err);
321       }
322     }
323   }
324 
325   if (it_bit_buff->ptr_bit_buf_base == NULL) {
326     pkt_loss_frame_cnt++;
327 
328     if (pkt_loss_frame_cnt *
329             (FLOAT32)pstr_drc_uni_bs_dec->ia_drc_params_struct.drc_frame_size /
330             drc_config->sampling_rate >
331         MAXPACKETLOSSTIME) {
332       drc_config->apply_drc = 0;
333     }
334   } else {
335     pstr_uni_drc_gain->uni_drc_gain_ext_flag =
336         impd_read_bits_buf(it_bit_buff, 1);
337     if (it_bit_buff->error) return it_bit_buff->error;
338     if (pstr_uni_drc_gain->uni_drc_gain_ext_flag == 1) {
339       err = impd_parse_uni_drc_gain_ext(it_bit_buff,
340                                         &(pstr_uni_drc_gain->uni_drc_gain_ext));
341       if (err) return (err);
342     }
343     pkt_loss_frame_cnt = 0;
344     drc_config->apply_drc = 1;
345   }
346 
347   return (0);
348 }
349 
impd_parse_uni_drc_gain_ext(ia_bit_buf_struct * it_bit_buff,ia_uni_drc_gain_ext_struct * uni_drc_gain_ext)350 WORD32 impd_parse_uni_drc_gain_ext(
351     ia_bit_buf_struct* it_bit_buff,
352     ia_uni_drc_gain_ext_struct* uni_drc_gain_ext) {
353   WORD32 k;
354   WORD32 bit_size_len, ext_size_bits, bit_size, other_bit;
355 
356   k = 0;
357   uni_drc_gain_ext->uni_drc_gain_ext_type[k] =
358       impd_read_bits_buf(it_bit_buff, 4);
359   if (it_bit_buff->error) return it_bit_buff->error;
360   while (uni_drc_gain_ext->uni_drc_gain_ext_type[k] != UNIDRCGAINEXT_TERM) {
361     if (k >= (EXT_COUNT_MAX - 1)) return UNEXPECTED_ERROR;
362     bit_size_len = impd_read_bits_buf(it_bit_buff, 3);
363     if (it_bit_buff->error) return it_bit_buff->error;
364     ext_size_bits = bit_size_len + 4;
365 
366     bit_size = impd_read_bits_buf(it_bit_buff, ext_size_bits);
367     if (it_bit_buff->error) return it_bit_buff->error;
368     uni_drc_gain_ext->ext_bit_size[k] = bit_size + 1;
369 
370     other_bit =
371         impd_skip_bits_buf(it_bit_buff, uni_drc_gain_ext->ext_bit_size[k]);
372     if (it_bit_buff->error) return it_bit_buff->error;
373     k++;
374     uni_drc_gain_ext->uni_drc_gain_ext_type[k] =
375         impd_read_bits_buf(it_bit_buff, 4);
376     if (it_bit_buff->error) return it_bit_buff->error;
377   }
378 
379   return (0);
380 }
381 
impd_parse_spline_nodes(ia_bit_buf_struct * it_bit_buff,ia_drc_bits_dec_struct * pstr_drc_uni_bs_dec,ia_gain_set_params_struct * gain_set_params,ia_spline_nodes_struct * str_spline_nodes)382 WORD32 impd_parse_spline_nodes(ia_bit_buf_struct* it_bit_buff,
383                                ia_drc_bits_dec_struct* pstr_drc_uni_bs_dec,
384                                ia_gain_set_params_struct* gain_set_params,
385                                ia_spline_nodes_struct* str_spline_nodes) {
386   WORD32 err = 0;
387   WORD32 time_offset;
388   if (gain_set_params->time_alignment == 0) {
389     time_offset = -1;
390   } else {
391     if (gain_set_params->time_delt_min_flag) {
392       time_offset = -gain_set_params->time_delt_min_val +
393                     (gain_set_params->time_delt_min_val - 1) / 2;
394     } else {
395       time_offset =
396           -pstr_drc_uni_bs_dec->ia_drc_params_struct.delta_tmin_default +
397           (pstr_drc_uni_bs_dec->ia_drc_params_struct.delta_tmin_default - 1) /
398               2;
399     }
400   }
401 
402   if (it_bit_buff->ptr_bit_buf_base == NULL) {
403     if ((str_spline_nodes->num_nodes < 1) ||
404         (str_spline_nodes->num_nodes > NODE_COUNT_MAX))
405       return UNEXPECTED_ERROR;
406     FLOAT32 prev_db_gain =
407         str_spline_nodes->str_node[str_spline_nodes->num_nodes - 1].loc_db_gain;
408     str_spline_nodes->drc_gain_coding_mode = 0;
409 
410     str_spline_nodes->num_nodes = 1;
411 
412     if (prev_db_gain < 0) {
413       str_spline_nodes->str_node[0].loc_db_gain = prev_db_gain;
414     } else {
415       str_spline_nodes->str_node[0].loc_db_gain = 0.f;
416     }
417 
418     str_spline_nodes->str_node[0].slope = 0.0;
419     str_spline_nodes->str_node[0].time =
420         (pstr_drc_uni_bs_dec->ia_drc_params_struct).drc_frame_size +
421         time_offset;
422   } else {
423     str_spline_nodes->drc_gain_coding_mode = impd_read_bits_buf(it_bit_buff, 1);
424     if (it_bit_buff->error == PROC_COMPLETE) {
425       if ((str_spline_nodes->num_nodes < 1) ||
426           (str_spline_nodes->num_nodes > NODE_COUNT_MAX))
427         return UNEXPECTED_ERROR;
428       str_spline_nodes->drc_gain_coding_mode = 0;
429       str_spline_nodes->str_node[0].slope = 0.0;
430       str_spline_nodes->str_node[0].time =
431           (pstr_drc_uni_bs_dec->ia_drc_params_struct).drc_frame_size +
432           time_offset;
433       str_spline_nodes->str_node[0].loc_db_gain =
434           str_spline_nodes->str_node[str_spline_nodes->num_nodes - 1]
435               .loc_db_gain;
436       str_spline_nodes->num_nodes = 1;
437     } else {
438       if (it_bit_buff->error) return (it_bit_buff->error);
439     }
440     if (str_spline_nodes->drc_gain_coding_mode == 0) {
441       str_spline_nodes->num_nodes = 1;
442 
443       err = impd_dec_initial_gain(it_bit_buff,
444                                   gain_set_params->gain_coding_profile,
445                                   &(str_spline_nodes->str_node[0].loc_db_gain));
446       if (err) return (err);
447 
448       str_spline_nodes->str_node[0].slope = 0.0;
449       str_spline_nodes->str_node[0].time =
450           (pstr_drc_uni_bs_dec->ia_drc_params_struct).drc_frame_size +
451           time_offset;
452     } else {
453       err = impd_dec_slopes(it_bit_buff, &str_spline_nodes->num_nodes,
454                             gain_set_params->gain_interpolation_type,
455                             str_spline_nodes->str_node);
456       if (err) return (err);
457       if (gain_set_params->time_delt_min_flag) {
458         err = impd_dec_times(
459             it_bit_buff, &gain_set_params->str_tables,
460             str_spline_nodes->num_nodes, gain_set_params->time_delt_min_val,
461             (pstr_drc_uni_bs_dec->ia_drc_params_struct).drc_frame_size,
462             gain_set_params->full_frame, time_offset,
463             str_spline_nodes->str_node);
464         if (err) return (err);
465         err = impd_dec_gains(it_bit_buff, str_spline_nodes->num_nodes,
466                              gain_set_params->gain_coding_profile,
467                              str_spline_nodes->str_node);
468         if (err) return (err);
469       } else {
470         err = impd_dec_times(
471             it_bit_buff, &pstr_drc_uni_bs_dec->tables_default,
472             str_spline_nodes->num_nodes,
473             (pstr_drc_uni_bs_dec->ia_drc_params_struct).delta_tmin_default,
474             (pstr_drc_uni_bs_dec->ia_drc_params_struct).drc_frame_size,
475             gain_set_params->full_frame, time_offset,
476             str_spline_nodes->str_node);
477         if (err) return (err);
478         err = impd_dec_gains(it_bit_buff, str_spline_nodes->num_nodes,
479                              gain_set_params->gain_coding_profile,
480                              str_spline_nodes->str_node);
481         if (err) return (err);
482       }
483     }
484   }
485   return (0);
486 }
487 
impd_parse_drc_gain_sequence(ia_bit_buf_struct * it_bit_buff,ia_drc_bits_dec_struct * pstr_drc_uni_bs_dec,ia_gain_set_params_struct * gain_set_params,ia_drc_gain_sequence_struct * drc_gain_sequence)488 WORD32 impd_parse_drc_gain_sequence(
489     ia_bit_buf_struct* it_bit_buff, ia_drc_bits_dec_struct* pstr_drc_uni_bs_dec,
490     ia_gain_set_params_struct* gain_set_params,
491     ia_drc_gain_sequence_struct* drc_gain_sequence) {
492   WORD32 err = 0, i;
493   WORD32 prev_frame_time_buf[NODE_COUNT_MAX],
494       cur_frame_time_buf[NODE_COUNT_MAX];
495   WORD32 num_nodes_node_reservoir, num_nodes_cur, k, m;
496 
497   if (((pstr_drc_uni_bs_dec->ia_drc_params_struct).delay_mode ==
498        DELAY_MODE_LOW_DELAY) &&
499       (gain_set_params->full_frame == 0)) {
500     return (PARAM_ERROR);
501   }
502   i = 0;
503   {
504     err = impd_parse_spline_nodes(it_bit_buff, pstr_drc_uni_bs_dec,
505                                   gain_set_params,
506                                   &(drc_gain_sequence->str_spline_nodes[i]));
507     if (err) return (err);
508 
509     num_nodes_node_reservoir = 0;
510     num_nodes_cur = 0;
511     for (k = 0; k < drc_gain_sequence->str_spline_nodes[i].num_nodes; k++) {
512       if (drc_gain_sequence->str_spline_nodes[i].str_node[k].time >=
513           pstr_drc_uni_bs_dec->ia_drc_params_struct.drc_frame_size) {
514         prev_frame_time_buf[num_nodes_node_reservoir] =
515             drc_gain_sequence->str_spline_nodes[i].str_node[k].time;
516         num_nodes_node_reservoir++;
517       } else {
518         cur_frame_time_buf[num_nodes_cur] =
519             drc_gain_sequence->str_spline_nodes[i].str_node[k].time;
520         num_nodes_cur++;
521       }
522     }
523     for (k = 0; k < num_nodes_node_reservoir; k++) {
524       WORD32 tmp = prev_frame_time_buf[k] -
525                    2 * pstr_drc_uni_bs_dec->ia_drc_params_struct.drc_frame_size;
526       if (tmp >= (2 * AUDIO_CODEC_FRAME_SIZE_MAX -
527                   pstr_drc_uni_bs_dec->ia_drc_params_struct.drc_frame_size))
528         return UNEXPECTED_ERROR;
529       drc_gain_sequence->str_spline_nodes[i].str_node[k].time = tmp;
530     }
531     for (m = 0; m < num_nodes_cur; m++, k++) {
532       drc_gain_sequence->str_spline_nodes[i].str_node[k].time =
533           cur_frame_time_buf[m];
534     }
535   }
536   return (0);
537 }
impd_parse_drc_ext_v1(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)538 WORD32 impd_parse_drc_ext_v1(ia_bit_buf_struct* it_bit_buff,
539                              ia_drc_params_bs_dec_struct* ia_drc_params_struct,
540                              ia_drc_config* drc_config,
541                              ia_drc_config_ext* str_drc_config_ext) {
542   WORD32 dwnmix_instructions_v1_flag;
543   WORD32 dwnmix_instructions_v1_count;
544   WORD32 drc_coeffs_and_instructions_uni_drc_v1_flag;
545   WORD32 drc_coefficients_uni_drc_v1_count;
546   WORD32 drc_instructions_uni_drc_v1_count;
547 
548   WORD32 i = 0, err = 0;
549   const WORD32 version = 1;
550 
551   dwnmix_instructions_v1_flag = impd_read_bits_buf(it_bit_buff, 1);
552   if (it_bit_buff->error) return it_bit_buff->error;
553   if (dwnmix_instructions_v1_flag == 1) {
554     dwnmix_instructions_v1_count = impd_read_bits_buf(it_bit_buff, 7);
555     if (it_bit_buff->error) return it_bit_buff->error;
556     if ((dwnmix_instructions_v1_count + drc_config->dwnmix_instructions_count) >
557         DOWNMIX_INSTRUCTION_COUNT_MAX)
558       return UNEXPECTED_ERROR;
559     for (i = 0; i < dwnmix_instructions_v1_count; i++) {
560       err = impd_parse_dwnmix_instructions(
561           it_bit_buff, version, ia_drc_params_struct,
562           &drc_config->channel_layout,
563           &drc_config
564                ->dwnmix_instructions[i +
565                                      drc_config->dwnmix_instructions_count]);
566       if (err) return (err);
567     }
568     drc_config->dwnmix_instructions_count += dwnmix_instructions_v1_count;
569   }
570 
571   drc_coeffs_and_instructions_uni_drc_v1_flag =
572       impd_read_bits_buf(it_bit_buff, 1);
573   if (it_bit_buff->error) return it_bit_buff->error;
574   if (drc_coeffs_and_instructions_uni_drc_v1_flag == 1) {
575     drc_coefficients_uni_drc_v1_count = impd_read_bits_buf(it_bit_buff, 3);
576     if ((drc_coefficients_uni_drc_v1_count +
577          drc_config->drc_coefficients_drc_count) > DRC_COEFF_COUNT_MAX) {
578       return (UNEXPECTED_ERROR);
579     }
580     if (it_bit_buff->error) return it_bit_buff->error;
581     for (i = 0; i < drc_coefficients_uni_drc_v1_count; i++) {
582       err = impd_drc_parse_coeff(
583           it_bit_buff, version, ia_drc_params_struct,
584           &drc_config->str_p_loc_drc_coefficients_uni_drc
585                [i + drc_config->drc_coefficients_drc_count]);
586       if (err) return (err);
587     }
588     drc_config->drc_coefficients_drc_count += drc_coefficients_uni_drc_v1_count;
589 
590     drc_instructions_uni_drc_v1_count = impd_read_bits_buf(it_bit_buff, 6);
591     if (it_bit_buff->error) return it_bit_buff->error;
592     if (drc_config->drc_instructions_uni_drc_count +
593             drc_instructions_uni_drc_v1_count >
594         DRC_INSTRUCTIONS_COUNT_MAX)
595       return (UNEXPECTED_ERROR);
596 
597     for (i = 0; i < drc_instructions_uni_drc_v1_count; i++) {
598       err = impd_parse_drc_instructions_uni_drc(
599           it_bit_buff, version, drc_config,
600           &drc_config->str_drc_instruction_str
601                [i + drc_config->drc_instructions_uni_drc_count]);
602       if (err) return (err);
603     }
604     drc_config->drc_instructions_uni_drc_count +=
605         drc_instructions_uni_drc_v1_count;
606   }
607 
608   str_drc_config_ext->loud_eq_instructions_flag =
609       impd_read_bits_buf(it_bit_buff, 1);
610   if (it_bit_buff->error) return it_bit_buff->error;
611   if (str_drc_config_ext->loud_eq_instructions_flag == 1) {
612     str_drc_config_ext->loud_eq_instructions_count =
613         impd_read_bits_buf(it_bit_buff, 4);
614 
615     if (it_bit_buff->error) return it_bit_buff->error;
616     if (str_drc_config_ext->loud_eq_instructions_count >
617         LOUD_EQ_INSTRUCTIONS_COUNT_MAX)
618       return UNEXPECTED_ERROR;
619     for (i = 0; i < str_drc_config_ext->loud_eq_instructions_count; i++) {
620       err = impd_parse_loud_eq_instructions(
621           it_bit_buff, &str_drc_config_ext->loud_eq_instructions[i]);
622       if (err) return (err);
623     }
624   } else {
625     str_drc_config_ext->loud_eq_instructions_count = 0;
626   }
627 
628   str_drc_config_ext->eq_flag = impd_read_bits_buf(it_bit_buff, 1);
629   if (it_bit_buff->error) return it_bit_buff->error;
630   if (str_drc_config_ext->eq_flag == 1) {
631     err = impd_parse_eq_coefficients(it_bit_buff,
632                                      &str_drc_config_ext->str_eq_coeff);
633     if (err) return (err);
634     str_drc_config_ext->eq_instructions_count =
635         impd_read_bits_buf(it_bit_buff, 4);
636     if (str_drc_config_ext->eq_instructions_count > EQ_INSTRUCTIONS_COUNT_MAX)
637       return UNEXPECTED_ERROR;
638     if (it_bit_buff->error) return it_bit_buff->error;
639     for (i = 0; i < str_drc_config_ext->eq_instructions_count; i++) {
640       err = impd_parse_eq_instructions(
641           it_bit_buff, drc_config, &str_drc_config_ext->str_eq_instructions[i]);
642       if (err) return (err);
643     }
644   }
645   return 0;
646 }
647 
impd_parse_filt_block(ia_bit_buf_struct * it_bit_buff,ia_filt_block_struct * str_filter_block,WORD32 block_count)648 WORD32 impd_parse_filt_block(ia_bit_buf_struct* it_bit_buff,
649                              ia_filt_block_struct* str_filter_block,
650                              WORD32 block_count) {
651   //    WORD32 err = 0;
652   WORD32 k, j, temp;
653   ia_filt_ele_struct* str_filter_element;
654 
655   for (j = 0; j < block_count; j++) {
656     str_filter_block->filter_element_count = impd_read_bits_buf(it_bit_buff, 6);
657     if (it_bit_buff->error) return it_bit_buff->error;
658     if (str_filter_block->filter_element_count > FILTER_ELEMENT_COUNT_MAX)
659       return UNEXPECTED_ERROR;
660     str_filter_element = &str_filter_block->str_filter_element[0];
661     for (k = 0; k < str_filter_block->filter_element_count; k++) {
662       temp = impd_read_bits_buf(it_bit_buff, 7);
663       if (it_bit_buff->error) return it_bit_buff->error;
664 
665       str_filter_element->filt_ele_idx = (temp & 0x7E) >> 1;
666       if (str_filter_element->filt_ele_idx >= FILTER_ELEMENT_COUNT_MAX)
667         return (UNEXPECTED_ERROR);
668       str_filter_element->filt_ele_gain_flag = temp & 1;
669       ;
670 
671       if (str_filter_element->filt_ele_gain_flag) {
672         WORD32 bs_filter_element_gain;
673         bs_filter_element_gain = impd_read_bits_buf(it_bit_buff, 10);
674         if (it_bit_buff->error) return it_bit_buff->error;
675         str_filter_element->filt_ele_gain =
676             bs_filter_element_gain * 0.125f - 96.0f;
677       }
678 
679       str_filter_element++;
680     }
681     str_filter_block++;
682   }
683   return (0);
684 }
685 
impd_parse_unique_td_filt_ele(ia_bit_buf_struct * it_bit_buff,ia_unique_td_filt_element * unique_td_filt_ele,WORD32 td_filter_element_count)686 WORD32 impd_parse_unique_td_filt_ele(
687     ia_bit_buf_struct* it_bit_buff,
688     ia_unique_td_filt_element* unique_td_filt_ele,
689     WORD32 td_filter_element_count) {
690   WORD32 m, sign, j, temp;
691   FLOAT32 tmp;
692 
693   for (j = 0; j < td_filter_element_count; j++) {
694     unique_td_filt_ele->eq_filter_format = impd_read_bits_buf(it_bit_buff, 1);
695     if (it_bit_buff->error) return it_bit_buff->error;
696 
697     if (unique_td_filt_ele->eq_filter_format == 0) {
698       WORD32 bs_real_zero_radius, bs_generic_zero_radius, bs_generic_zero_angle;
699       WORD32 bs_real_pole_radius, bs_cmplx_pole_radius, bs_cmplx_pole_angle;
700       WORD32 bs_real_zero_radius_one_count;
701 
702       temp = impd_read_bits_buf(it_bit_buff, 23);
703       if (it_bit_buff->error) return it_bit_buff->error;
704 
705       bs_real_zero_radius_one_count = (temp >> 20) & 7;
706 
707       unique_td_filt_ele->bs_real_zero_radius_one_count =
708           2 * bs_real_zero_radius_one_count;
709       unique_td_filt_ele->real_zero_count = (temp & 0xFC000) >> 14;
710 
711       unique_td_filt_ele->generic_zero_count = (temp & 0x3F00) >> 8;
712 
713       unique_td_filt_ele->real_pole_count = (temp & 0xF0) >> 4;
714 
715       unique_td_filt_ele->cmplx_pole_count = temp & 0xF;
716 
717       temp = impd_read_bits_buf(
718           it_bit_buff, unique_td_filt_ele->bs_real_zero_radius_one_count);
719       if (it_bit_buff->error) return it_bit_buff->error;
720 
721       for (m = unique_td_filt_ele->bs_real_zero_radius_one_count - 1; m >= 0;
722            m--) {
723         unique_td_filt_ele->zero_sign[m] = (temp & 1);
724         temp = temp >> 1;
725       }
726 
727       for (m = 0; m < unique_td_filt_ele->real_zero_count; m++) {
728         temp = impd_read_bits_buf(it_bit_buff, 8);
729         if (it_bit_buff->error) return it_bit_buff->error;
730 
731         bs_real_zero_radius = (temp & 0xFE) >> 1;
732 
733         sign = temp & 0x01;
734 
735         tmp = 1.0f - zero_pole_radius_tbl[bs_real_zero_radius];
736 
737         sign = sign << 1;
738 
739         unique_td_filt_ele->real_zero_radius[m] = (1 - sign) * tmp;
740       }
741       for (m = 0; m < unique_td_filt_ele->generic_zero_count; m++) {
742         temp = impd_read_bits_buf(it_bit_buff, 14);
743         if (it_bit_buff->error) return it_bit_buff->error;
744 
745         bs_generic_zero_radius = (temp & 0x3F80) >> 7;
746 
747         unique_td_filt_ele->generic_zero_radius[m] =
748             1.0f - zero_pole_radius_tbl[bs_generic_zero_radius];
749 
750         bs_generic_zero_angle = (temp & 0x7F);
751 
752         unique_td_filt_ele->generic_zero_angle[m] =
753             zero_pole_angle_tbl[bs_generic_zero_angle];
754       }
755       for (m = 0; m < unique_td_filt_ele->real_pole_count; m++) {
756         temp = impd_read_bits_buf(it_bit_buff, 8);
757         if (it_bit_buff->error) return it_bit_buff->error;
758 
759         bs_real_pole_radius = (temp & 0xFE) >> 1;
760 
761         sign = temp & 0x01;
762 
763         tmp = 1.0f - zero_pole_radius_tbl[bs_real_pole_radius];
764 
765         sign = sign << 1;
766 
767         unique_td_filt_ele->real_pole_radius[m] = (1 - sign) * tmp;
768       }
769       for (m = 0; m < unique_td_filt_ele->cmplx_pole_count; m++) {
770         temp = impd_read_bits_buf(it_bit_buff, 14);
771 
772         if (it_bit_buff->error) return it_bit_buff->error;
773 
774         bs_cmplx_pole_radius = (temp & 0x3F80) >> 7;
775 
776         unique_td_filt_ele->complex_pole_radius[m] =
777             1.0f - zero_pole_radius_tbl[bs_cmplx_pole_radius];
778 
779         bs_cmplx_pole_angle = (temp & 0x7F);
780 
781         unique_td_filt_ele->complex_pole_angle[m] =
782             zero_pole_angle_tbl[bs_cmplx_pole_angle];
783       }
784     } else {
785       temp = impd_read_bits_buf(it_bit_buff, 8);
786       if (it_bit_buff->error) return it_bit_buff->error;
787 
788       unique_td_filt_ele->fir_filt_order = (temp & 0xFE) >> 1;
789 
790       unique_td_filt_ele->fir_symmetry = temp & 0x01;
791 
792       for (m = 0; m < unique_td_filt_ele->fir_filt_order / 2 + 1; m++) {
793         WORD32 sign, bs_fir_coeff;
794         FLOAT32 tmp;
795 
796         temp = impd_read_bits_buf(it_bit_buff, 11);
797         if (it_bit_buff->error) return it_bit_buff->error;
798         sign = (temp >> 10) & 0x01;
799 
800         bs_fir_coeff = temp & 0x03FF;
801 
802         tmp = (FLOAT32)pow(10.0f, -0.05f * bs_fir_coeff * 0.0625f);
803 
804         sign = sign << 1;
805 
806         unique_td_filt_ele->fir_coeff[m] = (1 - sign) * tmp;
807       }
808     }
809     unique_td_filt_ele++;
810   }
811   return (0);
812 }
813 
impd_decode_eq_slope_code(ia_bit_buf_struct * it_bit_buff,FLOAT32 * eq_slope,WORD32 num_eq_nodes)814 WORD32 impd_decode_eq_slope_code(ia_bit_buf_struct* it_bit_buff,
815                                  FLOAT32* eq_slope, WORD32 num_eq_nodes) {
816   WORD32 bits = 0;
817   WORD32 k;
818 
819   for (k = 0; k < num_eq_nodes; k++) {
820     bits = impd_read_bits_buf(it_bit_buff, 1);
821     if (it_bit_buff->error) return it_bit_buff->error;
822     if (bits == 0x1) {
823       *eq_slope = 0.0f;
824     } else {
825       bits = impd_read_bits_buf(it_bit_buff, 4);
826       if (it_bit_buff->error) return it_bit_buff->error;
827       *eq_slope = eq_slope_tbl[bits];
828     }
829     eq_slope++;
830   }
831   return (0);
832 }
833 
834 WORD32
impd_decode_gain_initial_code(ia_bit_buf_struct * it_bit_buff,FLOAT32 * eq_gain_initial)835 impd_decode_gain_initial_code(ia_bit_buf_struct* it_bit_buff,
836                               FLOAT32* eq_gain_initial) {
837   WORD32 bits, bits1;
838 
839   bits1 = impd_read_bits_buf(it_bit_buff, 2);
840   if (it_bit_buff->error) return it_bit_buff->error;
841 
842   switch (bits1) {
843     case 0x0:
844       bits = impd_read_bits_buf(it_bit_buff, 5);
845       if (it_bit_buff->error) return it_bit_buff->error;
846       *eq_gain_initial = 0.5f * bits - 8.0f;
847       break;
848     case 0x1:
849     case 0x2:
850 
851       bits = impd_read_bits_buf(it_bit_buff, 4);
852       if (it_bit_buff->error) return it_bit_buff->error;
853       if (bits < 8) {
854         *eq_gain_initial = bits1 * bits - bits1 * 16.0f;
855       } else {
856         *eq_gain_initial = (FLOAT32)bits1 * bits;
857       }
858       break;
859 
860     case 0x3:
861       bits = impd_read_bits_buf(it_bit_buff, 3);
862       if (it_bit_buff->error) return it_bit_buff->error;
863       *eq_gain_initial = 4.0f * bits - 64.0f;
864       break;
865 
866     default:
867       break;
868   }
869   return (0);
870 }
871 
impd_parse_eq_subband_gain_spline(ia_bit_buf_struct * it_bit_buff,ia_eq_subband_gain_spline_struct * str_eq_subband_gain_spline,WORD32 eq_subband_gains_count)872 WORD32 impd_parse_eq_subband_gain_spline(
873     ia_bit_buf_struct* it_bit_buff,
874     ia_eq_subband_gain_spline_struct* str_eq_subband_gain_spline,
875     WORD32 eq_subband_gains_count) {
876   WORD32 err = 0, eq_nodes_cnt, j, k, bits, *eq_freq_delta;
877   FLOAT32* peq_gain_delta;
878 
879   for (j = 0; j < eq_subband_gains_count; j++) {
880     eq_nodes_cnt = impd_read_bits_buf(it_bit_buff, 5);
881     if (it_bit_buff->error) return it_bit_buff->error;
882 
883     str_eq_subband_gain_spline->num_eq_nodes = eq_nodes_cnt + 2;
884 
885     err = impd_decode_eq_slope_code(it_bit_buff,
886                                     &(str_eq_subband_gain_spline->eq_slope[0]),
887                                     str_eq_subband_gain_spline->num_eq_nodes);
888     if (err) return (err);
889 
890     eq_freq_delta = &(str_eq_subband_gain_spline->eq_freq_delta[1]);
891     for (k = 1; k < str_eq_subband_gain_spline->num_eq_nodes; k++) {
892       bits = impd_read_bits_buf(it_bit_buff, 4);
893       if (it_bit_buff->error) return it_bit_buff->error;
894       *eq_freq_delta = bits + 1;
895       eq_freq_delta++;
896     }
897 
898     err = impd_decode_gain_initial_code(
899         it_bit_buff, &(str_eq_subband_gain_spline->eq_gain_initial));
900     if (err) return (err);
901 
902     peq_gain_delta = &(str_eq_subband_gain_spline->eq_gain_delta[1]);
903     for (k = 1; k < str_eq_subband_gain_spline->num_eq_nodes; k++) {
904       bits = impd_read_bits_buf(it_bit_buff, 5);
905       if (it_bit_buff->error) return it_bit_buff->error;
906 
907       *peq_gain_delta = eq_gain_delta_tbl[bits];
908       peq_gain_delta++;
909     }
910     str_eq_subband_gain_spline++;
911   }
912   return (0);
913 }
914 
impd_parse_eq_subband_gain_vector(ia_bit_buf_struct * it_bit_buff,const WORD32 eq_subband_gain_count,ia_eq_subband_gain_vector * str_eq_subband_gain_vector,WORD32 eq_subband_gains_count)915 WORD32 impd_parse_eq_subband_gain_vector(
916     ia_bit_buf_struct* it_bit_buff, const WORD32 eq_subband_gain_count,
917     ia_eq_subband_gain_vector* str_eq_subband_gain_vector,
918     WORD32 eq_subband_gains_count) {
919   WORD32 m, k, temp;
920 
921   for (k = 0; k < eq_subband_gains_count; k++) {
922     for (m = 0; m < eq_subband_gain_count; m++) {
923       WORD32 sign, bs_eq_subband_gain;
924       temp = impd_read_bits_buf(it_bit_buff, 9);
925       if (it_bit_buff->error) return it_bit_buff->error;
926 
927       sign = (temp >> 8) & 1;
928       bs_eq_subband_gain = temp & 0x7F;
929 
930       sign = sign << 1;
931       str_eq_subband_gain_vector->eq_subband_gain[m] =
932           ((1 - sign) * bs_eq_subband_gain) * 0.125f;
933     }
934     str_eq_subband_gain_vector++;
935   }
936   return (0);
937 }
938 
impd_parse_eq_coefficients(ia_bit_buf_struct * it_bit_buff,ia_eq_coeff_struct * str_eq_coeff)939 WORD32 impd_parse_eq_coefficients(ia_bit_buf_struct* it_bit_buff,
940                                   ia_eq_coeff_struct* str_eq_coeff) {
941   WORD32 err = 0;
942   WORD32 eq_gain_cnt, mu, nu, temp;
943   static const WORD32 subband_gain_len_tbl[7] = {0, 32, 39, 64, 71, 128, 135};
944 
945   str_eq_coeff->eq_delay_max_present = impd_read_bits_buf(it_bit_buff, 1);
946   if (it_bit_buff->error) return it_bit_buff->error;
947 
948   if (str_eq_coeff->eq_delay_max_present) {
949     mu = impd_read_bits_buf(it_bit_buff, 5);
950     if (it_bit_buff->error) return it_bit_buff->error;
951     nu = impd_read_bits_buf(it_bit_buff, 3);
952     if (it_bit_buff->error) return it_bit_buff->error;
953     str_eq_coeff->eq_delay_max = 16 * mu * (1 << nu);
954   }
955 
956   str_eq_coeff->unique_filter_block_count = impd_read_bits_buf(it_bit_buff, 6);
957   if (it_bit_buff->error) return it_bit_buff->error;
958 
959   if (str_eq_coeff->unique_filter_block_count > FILTER_BLOCK_COUNT_MAX) {
960     return (UNEXPECTED_ERROR);
961   }
962 
963   err = impd_parse_filt_block(it_bit_buff, &(str_eq_coeff->str_filter_block[0]),
964                               str_eq_coeff->unique_filter_block_count);
965   if (err) return (err);
966 
967   str_eq_coeff->unique_td_filter_element_count =
968       impd_read_bits_buf(it_bit_buff, 6);
969   if (str_eq_coeff->unique_td_filter_element_count > FILTER_ELEMENT_COUNT_MAX)
970     return (UNEXPECTED_ERROR);
971   if (it_bit_buff->error) return it_bit_buff->error;
972 
973   err = impd_parse_unique_td_filt_ele(
974       it_bit_buff, &(str_eq_coeff->unique_td_filt_ele[0]),
975       str_eq_coeff->unique_td_filter_element_count);
976   if (err) return (err);
977 
978   str_eq_coeff->unique_eq_subband_gains_count =
979       impd_read_bits_buf(it_bit_buff, 6);
980   if (str_eq_coeff->unique_eq_subband_gains_count >
981       UNIQUE_SUBBAND_GAIN_COUNT_MAX)
982     return (UNEXPECTED_ERROR);
983   if (it_bit_buff->error) return it_bit_buff->error;
984 
985   if (str_eq_coeff->unique_eq_subband_gains_count > 0) {
986     temp = impd_read_bits_buf(it_bit_buff, 5);
987     if (it_bit_buff->error) return it_bit_buff->error;
988     str_eq_coeff->eq_subband_gain_representation = (temp >> 4) & 0x01;
989 
990     str_eq_coeff->eq_subband_gain_format = temp & 0x0F;
991     if ((str_eq_coeff->eq_subband_gain_format > 0) &&
992         (str_eq_coeff->eq_subband_gain_format < GAINFORMAT_UNIFORM)) {
993       str_eq_coeff->eq_subband_gain_count =
994           subband_gain_len_tbl[str_eq_coeff->eq_subband_gain_format];
995     } else {
996       /* Gain format 0 or any value between 7 to 15 is considered as default
997        * case */
998       eq_gain_cnt = impd_read_bits_buf(it_bit_buff, 8);
999 
1000       if (it_bit_buff->error) return it_bit_buff->error;
1001       str_eq_coeff->eq_subband_gain_count = eq_gain_cnt + 1;
1002 
1003       if (str_eq_coeff->eq_subband_gain_count > EQ_SUBBAND_GAIN_COUNT_MAX)
1004         return UNEXPECTED_ERROR;
1005 
1006     }
1007 
1008     if (str_eq_coeff->eq_subband_gain_representation == 1) {
1009       err = impd_parse_eq_subband_gain_spline(
1010           it_bit_buff, &(str_eq_coeff->str_eq_subband_gain_spline[0]),
1011           str_eq_coeff->unique_eq_subband_gains_count);
1012       if (err) return (err);
1013     } else {
1014       err = impd_parse_eq_subband_gain_vector(
1015           it_bit_buff, str_eq_coeff->eq_subband_gain_count,
1016           &(str_eq_coeff->str_eq_subband_gain_vector[0]),
1017           str_eq_coeff->unique_eq_subband_gains_count);
1018       if (err) return (err);
1019     }
1020   }
1021 
1022   return (0);
1023 }
1024 
impd_parser_td_filter_cascade(ia_bit_buf_struct * it_bit_buff,ia_eq_instructions_struct * str_eq_instructions,ia_td_filter_cascade_struct * str_td_filter_cascade)1025 WORD32 impd_parser_td_filter_cascade(
1026     ia_bit_buf_struct* it_bit_buff,
1027     ia_eq_instructions_struct* str_eq_instructions,
1028     ia_td_filter_cascade_struct* str_td_filter_cascade) {
1029   // WORD32 err=0,
1030   WORD32 i, ii, k;
1031   WORD32 eq_cascade_gain;
1032   ia_filter_block_refs_struct* str_filter_block_refs =
1033       &(str_td_filter_cascade->str_filter_block_refs[0]);
1034 
1035   for (i = 0; i < str_eq_instructions->eq_ch_group_count; i++) {
1036     str_td_filter_cascade->eq_cascade_gain_present[i] =
1037         impd_read_bits_buf(it_bit_buff, 1);
1038     if (it_bit_buff->error) return it_bit_buff->error;
1039     if (str_td_filter_cascade->eq_cascade_gain_present[i]) {
1040       eq_cascade_gain = impd_read_bits_buf(it_bit_buff, 10);
1041       if (it_bit_buff->error) return it_bit_buff->error;
1042       str_td_filter_cascade->eq_cascade_gain[i] =
1043           0.125f * eq_cascade_gain - 96.0f;
1044     } else {
1045       str_td_filter_cascade->eq_cascade_gain[i] = 0.0f;
1046     }
1047 
1048     str_filter_block_refs->filter_block_count =
1049         impd_read_bits_buf(it_bit_buff, 4);
1050     if (it_bit_buff->error) return it_bit_buff->error;
1051     if (str_filter_block_refs->filter_block_count > EQ_FILTER_BLOCK_COUNT_MAX) {
1052       return (UNEXPECTED_ERROR);
1053     }
1054 
1055     for (ii = 0; ii < str_filter_block_refs->filter_block_count; ii++) {
1056       str_filter_block_refs->filter_block_index[ii] =
1057           impd_read_bits_buf(it_bit_buff, 7);
1058       if (it_bit_buff->error) return it_bit_buff->error;
1059       if (str_filter_block_refs->filter_block_index[ii] >=
1060           FILTER_BLOCK_COUNT_MAX)
1061         return (UNEXPECTED_ERROR);
1062     }
1063     str_filter_block_refs++;
1064   }
1065 
1066   str_td_filter_cascade->eq_phase_alignment_present =
1067       impd_read_bits_buf(it_bit_buff, 1);
1068   if (it_bit_buff->error) return it_bit_buff->error;
1069 
1070   if (str_td_filter_cascade->eq_phase_alignment_present) {
1071     for (i = 0; i < str_eq_instructions->eq_ch_group_count; i++) {
1072       for (k = i + 1; k < str_eq_instructions->eq_ch_group_count; k++) {
1073         str_td_filter_cascade->eq_phase_alignment[i][k] =
1074             impd_read_bits_buf(it_bit_buff, 1);
1075         if (it_bit_buff->error) return it_bit_buff->error;
1076       }
1077     }
1078 
1079   } else {
1080     for (i = 0; i < str_eq_instructions->eq_ch_group_count; i++) {
1081       for (k = i + 1; k < str_eq_instructions->eq_ch_group_count; k++)
1082         str_td_filter_cascade->eq_phase_alignment[i][k] = 1;
1083     }
1084   }
1085 
1086   return (0);
1087 }
1088 
impd_parse_eq_instructions(ia_bit_buf_struct * it_bit_buff,ia_drc_config * drc_config,ia_eq_instructions_struct * str_eq_instructions)1089 WORD32 impd_parse_eq_instructions(
1090     ia_bit_buf_struct* it_bit_buff, ia_drc_config* drc_config,
1091     ia_eq_instructions_struct* str_eq_instructions) {
1092   WORD32 i, channel_count, temp;
1093   WORD32 dmix_id_present, additional_dmix_id_present,
1094       additional_dmix_id_cnt = 0;
1095   WORD32 additional_drc_set_id_present, additional_drc_set_id_cnt;
1096 
1097   temp = impd_read_bits_buf(it_bit_buff, 11);
1098   if (it_bit_buff->error) return it_bit_buff->error;
1099 
1100   str_eq_instructions->eq_set_id = (temp >> 5) & 0x3F;
1101 
1102   if (str_eq_instructions->eq_set_id >= EQ_INSTRUCTIONS_COUNT_MAX)
1103     return UNEXPECTED_ERROR;
1104 
1105   str_eq_instructions->eq_set_complexity_level = (temp >> 1) & 0x0F;
1106 
1107   dmix_id_present = temp & 0x01;
1108 
1109   if (dmix_id_present) {
1110     temp = impd_read_bits_buf(it_bit_buff, 9);
1111     if (it_bit_buff->error) return it_bit_buff->error;
1112 
1113     str_eq_instructions->downmix_id[0] = (temp >> 2) & 0x7F;
1114 
1115     str_eq_instructions->eq_apply_to_downmix = (temp >> 1) & 0x01;
1116 
1117     additional_dmix_id_present = temp & 0x01;
1118 
1119     if (additional_dmix_id_present) {
1120       additional_dmix_id_cnt = impd_read_bits_buf(it_bit_buff, 7);
1121       if (it_bit_buff->error) return it_bit_buff->error;
1122 
1123       if (additional_dmix_id_cnt >= DOWNMIX_ID_COUNT_MAX)
1124         return UNEXPECTED_ERROR;
1125 
1126       for (i = 1; i < additional_dmix_id_cnt + 1; i++) {
1127         str_eq_instructions->downmix_id[i] = impd_read_bits_buf(it_bit_buff, 7);
1128         if (it_bit_buff->error) return it_bit_buff->error;
1129       }
1130     }
1131   } else {
1132     str_eq_instructions->downmix_id[0] = 0;
1133   }
1134 
1135   str_eq_instructions->dwnmix_id_count = 1 + additional_dmix_id_cnt;
1136 
1137   temp = impd_read_bits_buf(it_bit_buff, 7);
1138   if (it_bit_buff->error) return it_bit_buff->error;
1139 
1140   str_eq_instructions->drc_set_id[0] = (temp >> 1) & 0x3F;
1141 
1142   additional_drc_set_id_present = temp & 0x01;
1143 
1144   if (additional_drc_set_id_present) {
1145     additional_drc_set_id_cnt = impd_read_bits_buf(it_bit_buff, 6);
1146     if (it_bit_buff->error) return it_bit_buff->error;
1147     if (additional_drc_set_id_cnt >= DRC_SET_ID_COUNT_MAX)
1148       return UNEXPECTED_ERROR;
1149 
1150     for (i = 1; i < additional_drc_set_id_cnt + 1; i++) {
1151       str_eq_instructions->drc_set_id[i] = impd_read_bits_buf(it_bit_buff, 6);
1152       if (it_bit_buff->error) return it_bit_buff->error;
1153     }
1154   } else {
1155     additional_drc_set_id_cnt = 0;
1156   }
1157   str_eq_instructions->drc_set_id_count = 1 + additional_drc_set_id_cnt;
1158 
1159   temp = impd_read_bits_buf(it_bit_buff, 17);
1160   if (it_bit_buff->error) return it_bit_buff->error;
1161 
1162   str_eq_instructions->eq_set_purpose = (temp >> 1) & 0xFFFF;
1163 
1164   str_eq_instructions->depends_on_eq_set_present = temp & 0x01;
1165 
1166   if (str_eq_instructions->depends_on_eq_set_present) {
1167     str_eq_instructions->depends_on_eq_set = impd_read_bits_buf(it_bit_buff, 6);
1168     if (it_bit_buff->error) return it_bit_buff->error;
1169   } else {
1170     str_eq_instructions->no_independent_eq_use =
1171         impd_read_bits_buf(it_bit_buff, 1);
1172     if (it_bit_buff->error) return it_bit_buff->error;
1173   }
1174 
1175   str_eq_instructions->eq_channel_count = channel_count =
1176       drc_config->channel_layout.base_channel_count;
1177 
1178   if ((dmix_id_present == 1) &&
1179       (str_eq_instructions->eq_apply_to_downmix == 1) &&
1180       (str_eq_instructions->downmix_id[0] != 0) &&
1181       (str_eq_instructions->downmix_id[0] != ID_FOR_ANY_DOWNMIX) &&
1182       (str_eq_instructions->dwnmix_id_count == 1)) {
1183     for (i = 0; i < drc_config->dwnmix_instructions_count; i++) {
1184       if (str_eq_instructions->downmix_id[0] ==
1185           drc_config->dwnmix_instructions[i].downmix_id)
1186         break;
1187     }
1188     if (i == drc_config->dwnmix_instructions_count) {
1189       return UNEXPECTED_ERROR;
1190     }
1191 
1192     str_eq_instructions->eq_channel_count = channel_count =
1193         drc_config->dwnmix_instructions[i].target_channel_count;
1194   } else if ((str_eq_instructions->downmix_id[0] == ID_FOR_ANY_DOWNMIX) ||
1195              (str_eq_instructions->dwnmix_id_count > 1)) {
1196     channel_count = 1;
1197   }
1198 
1199   str_eq_instructions->eq_ch_group_count = 0;
1200 
1201   for (i = 0; i < channel_count; i++) {
1202     WORD32 tmp = impd_read_bits_buf(it_bit_buff, 7);
1203     if (it_bit_buff->error) return it_bit_buff->error;
1204     if (tmp >= EQ_CHANNEL_GROUP_COUNT_MAX) return UNEXPECTED_ERROR;
1205     str_eq_instructions->eq_ch_group_of_channel[i] = tmp;
1206   }
1207   {
1208     WORD32 total;
1209     WORD32 groups_used[EQ_CHANNEL_GROUP_COUNT_MAX] = {0};
1210     for (i = 0; i < channel_count; i++)
1211       groups_used[str_eq_instructions->eq_ch_group_of_channel[i]] = 1;
1212     total = 0;
1213     for (i = 0; i < EQ_CHANNEL_GROUP_COUNT_MAX; i++)
1214       if (groups_used[i]) total++;
1215     str_eq_instructions->eq_ch_group_count = total;
1216   }
1217 
1218   if (str_eq_instructions->eq_ch_group_count > EQ_CHANNEL_GROUP_COUNT_MAX)
1219     return (UNEXPECTED_ERROR);
1220 
1221   str_eq_instructions->td_filter_cascade_present =
1222       impd_read_bits_buf(it_bit_buff, 1);
1223   if (it_bit_buff->error) return it_bit_buff->error;
1224 
1225   if (str_eq_instructions->td_filter_cascade_present) {
1226     WORD32 err = impd_parser_td_filter_cascade(
1227         it_bit_buff, str_eq_instructions,
1228         &(str_eq_instructions->str_td_filter_cascade));
1229     if (err) return err;
1230   }
1231 
1232   str_eq_instructions->subband_gains_present =
1233       impd_read_bits_buf(it_bit_buff, 1);
1234   if (it_bit_buff->error) return it_bit_buff->error;
1235 
1236   if (str_eq_instructions->subband_gains_present) {
1237     for (i = 0; i < str_eq_instructions->eq_ch_group_count; i++) {
1238       WORD32 tmp = impd_read_bits_buf(it_bit_buff, 6);
1239       if (it_bit_buff->error) return it_bit_buff->error;
1240       if (tmp >= UNIQUE_SUBBAND_GAIN_COUNT_MAX) return UNEXPECTED_ERROR;
1241       str_eq_instructions->subband_gains_index[i] = tmp;
1242     }
1243   }
1244 
1245   str_eq_instructions->eq_transition_duration_present =
1246       impd_read_bits_buf(it_bit_buff, 1);
1247   if (it_bit_buff->error) return it_bit_buff->error;
1248 
1249   if (str_eq_instructions->eq_transition_duration_present) {
1250     WORD32 bs_eq_transition_duration;
1251     bs_eq_transition_duration = impd_read_bits_buf(it_bit_buff, 5);
1252     if (it_bit_buff->error) return it_bit_buff->error;
1253 
1254     str_eq_instructions->eq_transition_duration = (WORD32)(
1255         0.001f *
1256         (FLOAT32)pow(2.0f, 2.0f + bs_eq_transition_duration * 0.0625f));
1257   }
1258   return (0);
1259 }
1260 
impd_parse_loud_eq_instructions(ia_bit_buf_struct * it_bit_buff,ia_loud_eq_instructions_struct * loud_eq_instructions)1261 WORD32 impd_parse_loud_eq_instructions(
1262     ia_bit_buf_struct* it_bit_buff,
1263     ia_loud_eq_instructions_struct* loud_eq_instructions) {
1264   WORD32 i, bs_loud_eq_scaling, bs_loud_eq_offset, temp;
1265   WORD32 dmix_id_present, additional_dmix_id_present,
1266       additional_dmix_id_cnt = 0;
1267   WORD32 drc_set_id_present, additional_drc_set_id_present,
1268       additional_drc_set_id_cnt = 0;
1269   WORD32 eq_set_id_present, additional_eq_set_id_present,
1270       additional_eq_set_id_cnt = 0;
1271 
1272   temp = impd_read_bits_buf(it_bit_buff, 9);
1273   if (it_bit_buff->error) return it_bit_buff->error;
1274   loud_eq_instructions->loud_eq_set_id = (temp >> 5) & 0x0F;
1275 
1276   loud_eq_instructions->drc_location = (temp >> 1) & 0x0F;
1277 
1278   dmix_id_present = temp & 0x01;
1279 
1280   if (dmix_id_present) {
1281     temp = impd_read_bits_buf(it_bit_buff, 8);
1282     if (it_bit_buff->error) return it_bit_buff->error;
1283 
1284     loud_eq_instructions->downmix_id[0] = (temp >> 1) & 0x7F;
1285 
1286     additional_dmix_id_present = temp & 0x01;
1287 
1288     if (additional_dmix_id_present) {
1289       additional_dmix_id_cnt = impd_read_bits_buf(it_bit_buff, 7);
1290       if (it_bit_buff->error) return it_bit_buff->error;
1291       if (additional_dmix_id_cnt >= DOWNMIX_ID_COUNT_MAX)
1292         return UNEXPECTED_ERROR;
1293       for (i = 1; i < additional_dmix_id_cnt + 1; i++) {
1294         loud_eq_instructions->downmix_id[i] =
1295             impd_read_bits_buf(it_bit_buff, 7);
1296         if (it_bit_buff->error) return it_bit_buff->error;
1297       }
1298     }
1299   } else {
1300     loud_eq_instructions->downmix_id[0] = 0;
1301   }
1302 
1303   loud_eq_instructions->dwnmix_id_count = 1 + additional_dmix_id_cnt;
1304 
1305   drc_set_id_present = impd_read_bits_buf(it_bit_buff, 1);
1306   if (it_bit_buff->error) return it_bit_buff->error;
1307 
1308   if (drc_set_id_present) {
1309     temp = impd_read_bits_buf(it_bit_buff, 7);
1310     if (it_bit_buff->error) return it_bit_buff->error;
1311 
1312     loud_eq_instructions->drc_set_id[0] = (temp >> 1) & 0x3F;
1313 
1314     additional_drc_set_id_present = temp & 0x01;
1315 
1316     if (additional_drc_set_id_present) {
1317       additional_drc_set_id_cnt = impd_read_bits_buf(it_bit_buff, 6);
1318       if (it_bit_buff->error) return it_bit_buff->error;
1319       if ((additional_drc_set_id_cnt >= DRC_SET_ID_COUNT_MAX))
1320         return UNEXPECTED_ERROR;
1321 
1322       for (i = 1; i < additional_drc_set_id_cnt + 1; i++) {
1323         loud_eq_instructions->drc_set_id[i] =
1324             impd_read_bits_buf(it_bit_buff, 6);
1325         if (it_bit_buff->error) return it_bit_buff->error;
1326       }
1327     }
1328   } else {
1329     loud_eq_instructions->drc_set_id[0] = 0;
1330   }
1331 
1332   loud_eq_instructions->drc_set_id_count = 1 + additional_drc_set_id_cnt;
1333 
1334   eq_set_id_present = impd_read_bits_buf(it_bit_buff, 1);
1335 
1336   if (it_bit_buff->error) return it_bit_buff->error;
1337 
1338   if (eq_set_id_present) {
1339     temp = impd_read_bits_buf(it_bit_buff, 7);
1340     if (it_bit_buff->error) return it_bit_buff->error;
1341 
1342     loud_eq_instructions->eq_set_id[0] = (temp >> 1) & 0x3F;
1343 
1344     additional_eq_set_id_present = temp & 0x01;
1345 
1346     if (additional_eq_set_id_present) {
1347       additional_eq_set_id_cnt = impd_read_bits_buf(it_bit_buff, 6);
1348       if (it_bit_buff->error) return it_bit_buff->error;
1349       if (additional_eq_set_id_cnt >= EQ_SET_ID_COUNT_MAX)
1350         return UNEXPECTED_ERROR;
1351       for (i = 0; i < additional_eq_set_id_cnt; i++) {
1352         loud_eq_instructions->eq_set_id[i + 1] =
1353             impd_read_bits_buf(it_bit_buff, 6);
1354         if (it_bit_buff->error) return it_bit_buff->error;
1355       }
1356     }
1357   } else {
1358     loud_eq_instructions->eq_set_id[0] = 0;
1359   }
1360   loud_eq_instructions->eq_set_id_count = 1 + additional_eq_set_id_cnt;
1361 
1362   temp = impd_read_bits_buf(it_bit_buff, 8);
1363   if (it_bit_buff->error) return it_bit_buff->error;
1364 
1365   /* Parsed but unused */
1366   loud_eq_instructions->loudness_after_drc = (temp >> 7) & 0x01;
1367 
1368   /* Parsed but unused */
1369   loud_eq_instructions->loudness_after_eq = (temp >> 6) & 0x01;
1370 
1371   /* Parsed but unused */
1372   loud_eq_instructions->loud_eq_gain_sequence_count = temp & 0x3F;
1373 
1374   if (loud_eq_instructions->loud_eq_gain_sequence_count >
1375       LOUD_EQ_GAIN_SEQUENCE_COUNT_MAX)
1376     return UNEXPECTED_ERROR;
1377 
1378   /* Section under for loop, Parsed but unused */
1379   for (i = 0; i < loud_eq_instructions->loud_eq_gain_sequence_count; i++) {
1380     temp = impd_read_bits_buf(it_bit_buff, 7);
1381     if (it_bit_buff->error) return it_bit_buff->error;
1382 
1383     loud_eq_instructions->gain_seq_idx[i] = (temp >> 1) & 0x3F;
1384 
1385     loud_eq_instructions->drc_characteristic_format_is_cicp[i] = temp & 0x01;
1386 
1387     if (loud_eq_instructions->drc_characteristic_format_is_cicp[i]) {
1388       loud_eq_instructions->drc_characteristic[i] =
1389           impd_read_bits_buf(it_bit_buff, 7);
1390       if (it_bit_buff->error) return it_bit_buff->error;
1391     } else {
1392       temp = impd_read_bits_buf(it_bit_buff, 8);
1393       if (it_bit_buff->error) return it_bit_buff->error;
1394 
1395       loud_eq_instructions->drc_characteristic_left_index[i] =
1396           (temp >> 4) & 0x0F;
1397 
1398       loud_eq_instructions->drc_characteristic_right_index[i] = temp & 0x0F;
1399     }
1400 
1401     temp = impd_read_bits_buf(it_bit_buff, 9);
1402     if (it_bit_buff->error) return it_bit_buff->error;
1403 
1404     loud_eq_instructions->frequency_range_index[i] = (temp >> 3) & 0x3F;
1405 
1406     bs_loud_eq_scaling = temp & 0x07;
1407 
1408     loud_eq_instructions->loud_eq_scaling[i] =
1409         (FLOAT32)pow(2.0f, -0.5f * bs_loud_eq_scaling);
1410 
1411     bs_loud_eq_offset = impd_read_bits_buf(it_bit_buff, 5);
1412     if (it_bit_buff->error) return it_bit_buff->error;
1413 
1414     loud_eq_instructions->loud_eq_offset[i] = 1.5f * bs_loud_eq_offset - 16.0f;
1415   }
1416   return (0);
1417 }
1418