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