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