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