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 <stdlib.h>
22 #include <math.h>
23 #include <assert.h>
24 #include <string.h>
25 #include "impd_type_def.h"
26 
27 #include "impd_drc_bitbuffer.h"
28 #include "impd_drc_common.h"
29 #include "impd_drc_interface.h"
30 #include "impd_drc_parser_interface.h"
31 
32 WORD32
impd_drc_dec_interface_process(ia_bit_buf_struct * it_bit_buff,ia_drc_interface_struct * pstr_drc_interface,UWORD8 * it_bit_buf,WORD32 num_bit_stream_bits,WORD32 * num_bits_read)33 impd_drc_dec_interface_process(ia_bit_buf_struct* it_bit_buff,
34                                ia_drc_interface_struct* pstr_drc_interface,
35                                UWORD8* it_bit_buf, WORD32 num_bit_stream_bits,
36                                WORD32* num_bits_read) {
37   WORD32 err = 0;
38 
39   if (it_bit_buff != NULL && num_bit_stream_bits) {
40     it_bit_buff = impd_create_init_bit_buf(it_bit_buff, it_bit_buf,
41                                            num_bit_stream_bits / 8);
42 
43   } else {
44     return -1;
45   }
46 
47   err = impd_unidrc_interface_read(it_bit_buff, pstr_drc_interface);
48   if (err) return (err);
49 
50   *num_bits_read = (it_bit_buff->size) - it_bit_buff->cnt_bits;
51 
52   return err;
53 }
54 
55 WORD32
impd_drc_dec_interface_add_effect_type(ia_drc_interface_struct * pstr_drc_interface,WORD32 drc_effect_type,WORD32 target_loudness,WORD32 loud_norm)56 impd_drc_dec_interface_add_effect_type(
57     ia_drc_interface_struct* pstr_drc_interface, WORD32 drc_effect_type,
58     WORD32 target_loudness, WORD32 loud_norm) {
59   WORD32 err = 0;
60   WORD32 i = 0;
61 
62   if (pstr_drc_interface != NULL) {
63     pstr_drc_interface->interface_signat_flag = 0;
64     for (i = 0; i < MAX_SIGNATURE_DATA_LENGTH_PLUS_ONE * 8; i++)
65       pstr_drc_interface->drc_uni_interface_signature.interface_signat_data[i] =
66           0;
67 
68     pstr_drc_interface->drc_uni_interface_signature.interface_signat_data_len =
69         0;
70 
71     pstr_drc_interface->system_interface_flag = 1;
72 
73     pstr_drc_interface->system_interface.target_config_request_type = 0;
74     pstr_drc_interface->system_interface.num_downmix_id_requests = 0;
75     for (i = 0; i < MAX_NUM_DOWNMIX_ID_REQUESTS; i++) {
76       pstr_drc_interface->system_interface.requested_dwnmix_id[i] = 0;
77     }
78     pstr_drc_interface->system_interface.requested_target_layout = 0;
79     pstr_drc_interface->system_interface.requested_target_ch_count = 0;
80 
81     pstr_drc_interface->loudness_norm_ctrl_interface_flag = 1;
82     if (loud_norm == 1) {
83       pstr_drc_interface->loudness_norm_ctrl_interface
84           .loudness_normalization_on = 1;
85     } else {
86       pstr_drc_interface->loudness_norm_ctrl_interface
87           .loudness_normalization_on = 0;
88     }
89     pstr_drc_interface->loudness_norm_ctrl_interface.target_loudness =
90         (FLOAT32)target_loudness;
91 
92     pstr_drc_interface->loudness_norm_parameter_interface_flag = 1;
93     pstr_drc_interface->loudness_norm_param_interface.album_mode = 0;
94     pstr_drc_interface->loudness_norm_param_interface.peak_limiter = 0;
95     pstr_drc_interface->loudness_norm_param_interface
96         .change_loudness_deviation_max = 1;
97     pstr_drc_interface->loudness_norm_param_interface.loudness_deviation_max =
98         63;
99     pstr_drc_interface->loudness_norm_param_interface
100         .change_loudness_measur_method = 1;
101     pstr_drc_interface->loudness_norm_param_interface
102         .loudness_measurement_method = 1;
103     pstr_drc_interface->loudness_norm_param_interface
104         .change_loudness_measur_system = 1;
105     pstr_drc_interface->loudness_norm_param_interface
106         .loudness_measurement_system = 1;
107     pstr_drc_interface->loudness_norm_param_interface
108         .change_loudness_measur_pre_proc = 0;
109     pstr_drc_interface->loudness_norm_param_interface
110         .loudness_measurement_pre_proc = 0;
111     pstr_drc_interface->loudness_norm_param_interface
112         .change_device_cut_off_freq = 1;
113     pstr_drc_interface->loudness_norm_param_interface.device_cut_off_frequency =
114         20;
115     pstr_drc_interface->loudness_norm_param_interface
116         .change_loudness_norm_gain_db_max = 1;
117     pstr_drc_interface->loudness_norm_param_interface
118         .loudness_norm_gain_db_max = 1000.0;
119     pstr_drc_interface->loudness_norm_param_interface
120         .change_loudness_norm_gain_modification_db = 1;
121     pstr_drc_interface->loudness_norm_param_interface
122         .loudness_norm_gain_modification_db = 0.0;
123     pstr_drc_interface->loudness_norm_param_interface
124         .change_output_peak_level_max = 1;
125     pstr_drc_interface->loudness_norm_param_interface.output_peak_level_max =
126         0.0;
127 
128     pstr_drc_interface->drc_interface_flag = 1;
129     if (drc_effect_type == -1) {
130       pstr_drc_interface->drc_ctrl_interface.dynamic_range_control_on = 0;
131       pstr_drc_interface->drc_ctrl_interface.requested_drc_effect_type[0][0] =
132           0;
133     } else if (drc_effect_type == 0) {
134       pstr_drc_interface->drc_ctrl_interface.dynamic_range_control_on = 1;
135       pstr_drc_interface->drc_ctrl_interface.num_drc_feature_requests = 0;
136     } else {
137       pstr_drc_interface->drc_ctrl_interface.dynamic_range_control_on = 1;
138       pstr_drc_interface->drc_ctrl_interface.requested_drc_effect_type[0][0] =
139           drc_effect_type;
140       pstr_drc_interface->drc_ctrl_interface.num_drc_feature_requests = 3;
141       pstr_drc_interface->drc_ctrl_interface.drc_feature_req_type[0] = 0;
142       pstr_drc_interface->drc_ctrl_interface.drc_feature_req_type[1] = 1;
143       pstr_drc_interface->drc_ctrl_interface.drc_feature_req_type[2] = 2;
144       pstr_drc_interface->drc_ctrl_interface.requested_num_drc_effects[0] = 1;
145       pstr_drc_interface->drc_ctrl_interface
146           .desired_num_drc_effects_of_requested[0] = 1;
147     }
148 
149     pstr_drc_interface->drc_ctrl_interface
150         .requested_dyn_rng_measurement_type[0] = 0;
151     pstr_drc_interface->drc_ctrl_interface
152         .requested_dyn_range_is_single_val_flag[0] = 0;
153     pstr_drc_interface->drc_ctrl_interface
154         .requested_dyn_range_is_single_val_flag[1] = 0;
155     pstr_drc_interface->drc_ctrl_interface.requested_dyn_range_min_val[1] =
156         3.0f;
157     pstr_drc_interface->drc_ctrl_interface.requested_dyn_range_max_val[1] =
158         10.0f;
159     pstr_drc_interface->drc_ctrl_interface.requested_drc_characteristic[2] = 3;
160 
161     pstr_drc_interface->drc_parameter_interface_flag = 1;
162     pstr_drc_interface->drc_parameter_interface.change_compress = 1;
163     pstr_drc_interface->drc_parameter_interface.change_boost = 1;
164     pstr_drc_interface->drc_parameter_interface.compress = 1.0f;
165     pstr_drc_interface->drc_parameter_interface.boost = 1.0f;
166     pstr_drc_interface->drc_parameter_interface
167         .change_drc_characteristic_target = 1;
168     pstr_drc_interface->drc_parameter_interface.drc_characteristic_target = 0;
169 
170     pstr_drc_interface->drc_uni_interface_ext_flag = 0;
171 
172   } else {
173     return UNEXPECTED_ERROR;
174   }
175 
176   return err;
177 }
178