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
26 #include "impd_type_def.h"
27 #include "impd_drc_bitbuffer.h"
28 #include "impd_drc_extr_delta_coded_info.h"
29 #include "impd_drc_common.h"
30 #include "impd_drc_struct.h"
31 #include "impd_drc_parser.h"
32
impd_read_bits_buf(ia_bit_buf_struct * it_bit_buff,WORD no_of_bits)33 WORD32 impd_read_bits_buf(ia_bit_buf_struct* it_bit_buff, WORD no_of_bits) {
34 UWORD32 ret_val;
35 UWORD8* ptr_read_next = it_bit_buff->ptr_read_next;
36 WORD bit_pos = it_bit_buff->bit_pos;
37
38 if (it_bit_buff->cnt_bits <= 0) {
39 it_bit_buff->error = 1;
40 return -1;
41 }
42
43 if (no_of_bits == 0) {
44 return 0;
45 }
46
47 it_bit_buff->cnt_bits -= no_of_bits;
48 ret_val = (UWORD32)*ptr_read_next;
49
50 bit_pos -= no_of_bits;
51 while (bit_pos < 0) {
52 bit_pos += 8;
53 ptr_read_next++;
54
55 if (ptr_read_next > it_bit_buff->ptr_bit_buf_end) {
56 ptr_read_next = it_bit_buff->ptr_bit_buf_base;
57 }
58
59 ret_val <<= 8;
60
61 ret_val |= (UWORD32)*ptr_read_next;
62 }
63
64 ret_val = ret_val << ((31 - no_of_bits) - bit_pos) >> (32 - no_of_bits);
65 it_bit_buff->ptr_read_next = ptr_read_next;
66 it_bit_buff->bit_pos = (WORD16)bit_pos;
67 return ret_val;
68 }
69
impd_skip_bits_buf(ia_bit_buf_struct * it_bit_buff,WORD no_of_bits)70 WORD32 impd_skip_bits_buf(ia_bit_buf_struct* it_bit_buff, WORD no_of_bits) {
71 UWORD8* ptr_read_next = it_bit_buff->ptr_read_next;
72 WORD bit_pos = it_bit_buff->bit_pos;
73
74 if (it_bit_buff->cnt_bits < no_of_bits) {
75 it_bit_buff->error = 1;
76 return -1;
77 }
78
79 it_bit_buff->cnt_bits -= no_of_bits;
80
81 bit_pos -= no_of_bits;
82 while (bit_pos < 0) {
83 bit_pos += 8;
84 ptr_read_next++;
85 }
86 it_bit_buff->ptr_read_next = ptr_read_next;
87 it_bit_buff->bit_pos = (WORD16)bit_pos;
88 return no_of_bits;
89 }
impd_create_bit_buf(ia_bit_buf_struct * it_bit_buff,UWORD8 * ptr_bit_buf_base,WORD32 bit_buf_size)90 ia_bit_buf_struct* impd_create_bit_buf(ia_bit_buf_struct* it_bit_buff,
91 UWORD8* ptr_bit_buf_base,
92 WORD32 bit_buf_size) {
93 it_bit_buff->ptr_bit_buf_base = ptr_bit_buf_base;
94 it_bit_buff->ptr_bit_buf_end = ptr_bit_buf_base + bit_buf_size - 1;
95
96 it_bit_buff->ptr_read_next = ptr_bit_buf_base;
97 it_bit_buff->bit_pos = 7;
98
99 it_bit_buff->cnt_bits = 0;
100 it_bit_buff->size = bit_buf_size << 3;
101 it_bit_buff->error = 0;
102
103 return it_bit_buff;
104 }
105
impd_create_init_bit_buf(ia_bit_buf_struct * it_bit_buff,UWORD8 * ptr_bit_buf_base,WORD32 bit_buf_size)106 ia_bit_buf_struct* impd_create_init_bit_buf(ia_bit_buf_struct* it_bit_buff,
107 UWORD8* ptr_bit_buf_base,
108 WORD32 bit_buf_size) {
109 impd_create_bit_buf(it_bit_buff, ptr_bit_buf_base, bit_buf_size);
110 it_bit_buff->cnt_bits = (bit_buf_size << 3);
111 return (it_bit_buff);
112 }
113
impd_init_drc_bitstream_dec(ia_drc_bits_dec_struct * p_drc_bs_dec_struct,WORD32 sample_rate,WORD32 frame_size,WORD32 delay_mode,WORD32 lfe_channel_map_count,WORD32 * lfe_channel_map)114 WORD32 impd_init_drc_bitstream_dec(ia_drc_bits_dec_struct* p_drc_bs_dec_struct,
115 WORD32 sample_rate, WORD32 frame_size,
116 WORD32 delay_mode,
117 WORD32 lfe_channel_map_count,
118 WORD32* lfe_channel_map) {
119 WORD32 i, err_code = 0;
120
121 ia_drc_params_bs_dec_struct* ia_drc_params_struct =
122 &p_drc_bs_dec_struct->ia_drc_params_struct;
123 ia_drc_params_struct->drc_frame_size = frame_size;
124 ia_drc_params_struct->delta_tmin_default = impd_get_delta_tmin(sample_rate);
125 ia_drc_params_struct->num_gain_values_max_default =
126 ia_drc_params_struct->drc_frame_size /
127 ia_drc_params_struct->delta_tmin_default;
128 ia_drc_params_struct->delay_mode = delay_mode;
129
130 if ((frame_size < 1) || (frame_size > AUDIO_CODEC_FRAME_SIZE_MAX) ||
131 (ia_drc_params_struct->drc_frame_size < 0.001f * sample_rate)) {
132 return -1;
133 }
134 if (sample_rate < 1000) {
135 return -1;
136 }
137
138 if (ia_drc_params_struct->delta_tmin_default >
139 ia_drc_params_struct->drc_frame_size) {
140 return -1;
141 }
142
143 if (lfe_channel_map_count >= 0) {
144 if ((lfe_channel_map == NULL) ||
145 (lfe_channel_map_count > MAX_CHANNEL_COUNT)) {
146 return (-1);
147 }
148
149 ia_drc_params_struct->lfe_channel_map_count = lfe_channel_map_count;
150
151 for (i = 0; i < lfe_channel_map_count; i++) {
152 ia_drc_params_struct->lfe_channel_map[i] = lfe_channel_map[i];
153 }
154 } else {
155 ia_drc_params_struct->lfe_channel_map_count = -1;
156
157 for (i = 0; i < MAX_CHANNEL_COUNT; i++) {
158 ia_drc_params_struct->lfe_channel_map[i] = 0;
159 }
160 }
161
162 err_code = impd_init_tbls(ia_drc_params_struct->num_gain_values_max_default,
163 &p_drc_bs_dec_struct->tables_default);
164
165 return err_code;
166 }
167
impd_process_drc_bitstream_dec(ia_drc_bits_dec_struct * p_drc_bs_dec_struct,ia_bit_buf_struct * it_bit_buff,ia_drc_config * pstr_drc_config,ia_drc_loudness_info_set_struct * pstr_loudness_info,UWORD8 * bitstream_config,WORD32 num_bytes,WORD32 num_bits_offset,WORD32 * num_bits_read)168 WORD32 impd_process_drc_bitstream_dec(
169 ia_drc_bits_dec_struct* p_drc_bs_dec_struct, ia_bit_buf_struct* it_bit_buff,
170 ia_drc_config* pstr_drc_config,
171 ia_drc_loudness_info_set_struct* pstr_loudness_info,
172 UWORD8* bitstream_config, WORD32 num_bytes, WORD32 num_bits_offset,
173 WORD32* num_bits_read) {
174 WORD32 err_code = 0;
175
176 WORD32 loudness_info_set_present, drc_config_present, dummy;
177
178 if (bitstream_config == NULL) {
179 *num_bits_read = 0;
180 } else {
181 it_bit_buff =
182 impd_create_init_bit_buf(it_bit_buff, bitstream_config, num_bytes);
183
184 dummy = impd_read_bits_buf(it_bit_buff, num_bits_offset);
185 if (it_bit_buff->error) return it_bit_buff->error;
186
187 loudness_info_set_present = impd_read_bits_buf(it_bit_buff, 1);
188 if (it_bit_buff->error) return it_bit_buff->error;
189
190 if (loudness_info_set_present) {
191 drc_config_present = impd_read_bits_buf(it_bit_buff, 1);
192 if (it_bit_buff->error) return it_bit_buff->error;
193
194 if (drc_config_present) {
195 err_code = impd_parse_drc_config(
196 it_bit_buff, &p_drc_bs_dec_struct->ia_drc_params_struct,
197 pstr_drc_config);
198
199 if (err_code) return (err_code);
200 }
201
202 err_code = impd_parse_loudness_info_set(it_bit_buff, pstr_loudness_info);
203
204 if (err_code) return (err_code);
205 }
206
207 *num_bits_read = it_bit_buff->size - it_bit_buff->cnt_bits;
208 }
209
210 return err_code;
211 }
212
impd_process_drc_bitstream_dec_config(ia_drc_bits_dec_struct * p_drc_bs_dec_struct,ia_bit_buf_struct * it_bit_buff,ia_drc_config * pstr_drc_config,UWORD8 * bitstream_config,WORD32 num_bytes)213 WORD32 impd_process_drc_bitstream_dec_config(
214 ia_drc_bits_dec_struct* p_drc_bs_dec_struct, ia_bit_buf_struct* it_bit_buff,
215 ia_drc_config* pstr_drc_config, UWORD8* bitstream_config,
216 WORD32 num_bytes) {
217 WORD32 err_code = 0;
218
219 it_bit_buff =
220 impd_create_init_bit_buf(it_bit_buff, bitstream_config, num_bytes);
221
222 err_code = impd_parse_drc_config(
223 it_bit_buff, &p_drc_bs_dec_struct->ia_drc_params_struct, pstr_drc_config);
224 if (err_code) return (err_code);
225
226 return err_code;
227 }
228
impd_process_drc_bitstream_dec_gain(ia_drc_bits_dec_struct * p_drc_bs_dec_struct,ia_bit_buf_struct * it_bit_buff,ia_drc_config * pstr_drc_config,ia_drc_gain_struct * pstr_drc_gain,UWORD8 * bitstream_gain,WORD32 num_bytes,WORD32 num_bits_offset,WORD32 * num_bits_read)229 WORD32 impd_process_drc_bitstream_dec_gain(
230 ia_drc_bits_dec_struct* p_drc_bs_dec_struct, ia_bit_buf_struct* it_bit_buff,
231 ia_drc_config* pstr_drc_config, ia_drc_gain_struct* pstr_drc_gain,
232 UWORD8* bitstream_gain, WORD32 num_bytes, WORD32 num_bits_offset,
233 WORD32* num_bits_read) {
234 WORD32 err_code = 0;
235
236 WORD32 dummy;
237
238 it_bit_buff =
239 impd_create_init_bit_buf(it_bit_buff, bitstream_gain, num_bytes);
240
241 dummy = impd_read_bits_buf(it_bit_buff, num_bits_offset);
242 if (it_bit_buff->error) return it_bit_buff->error;
243
244 err_code = impd_drc_uni_gain_read(it_bit_buff, p_drc_bs_dec_struct,
245 pstr_drc_config, pstr_drc_gain);
246
247 if (err_code > PROC_COMPLETE) return (err_code);
248
249 *num_bits_read = (it_bit_buff->size) - it_bit_buff->cnt_bits;
250
251 if (err_code == PROC_COMPLETE) {
252 return err_code;
253 }
254
255 return 0;
256 }
257
impd_process_drc_bitstream_dec_loudness_info_set(ia_bit_buf_struct * it_bit_buff,ia_drc_loudness_info_set_struct * pstr_loudness_info,UWORD8 * bit_stream_loudness,WORD32 num_bytes_loudness)258 WORD32 impd_process_drc_bitstream_dec_loudness_info_set(
259 ia_bit_buf_struct* it_bit_buff,
260 ia_drc_loudness_info_set_struct* pstr_loudness_info,
261 UWORD8* bit_stream_loudness, WORD32 num_bytes_loudness) {
262 WORD32 err_code = 0;
263
264 it_bit_buff = impd_create_init_bit_buf(it_bit_buff, bit_stream_loudness,
265 num_bytes_loudness);
266
267 err_code = impd_parse_loudness_info_set(it_bit_buff, pstr_loudness_info);
268 if (err_code) return (err_code);
269
270 return 0;
271 }
272