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 VOID impd_create_bit_buf(ia_bit_buf_struct* it_bit_buff,
91 UWORD8* ptr_bit_buf_base, WORD32 bit_buf_size) {
92 it_bit_buff->ptr_bit_buf_base = ptr_bit_buf_base;
93 it_bit_buff->ptr_bit_buf_end = ptr_bit_buf_base + bit_buf_size - 1;
94
95 it_bit_buff->ptr_read_next = ptr_bit_buf_base;
96 it_bit_buff->bit_pos = 7;
97
98 it_bit_buff->cnt_bits = 0;
99 it_bit_buff->size = bit_buf_size << 3;
100 it_bit_buff->error = 0;
101
102 return;
103 }
104
impd_create_init_bit_buf(ia_bit_buf_struct * it_bit_buff,UWORD8 * ptr_bit_buf_base,WORD32 bit_buf_size)105 VOID impd_create_init_bit_buf(ia_bit_buf_struct* it_bit_buff,
106 UWORD8* ptr_bit_buf_base, WORD32 bit_buf_size) {
107 impd_create_bit_buf(it_bit_buff, ptr_bit_buf_base, bit_buf_size);
108 it_bit_buff->cnt_bits = (bit_buf_size << 3);
109 return;
110 }
111
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)112 WORD32 impd_init_drc_bitstream_dec(ia_drc_bits_dec_struct* p_drc_bs_dec_struct,
113 WORD32 sample_rate, WORD32 frame_size,
114 WORD32 delay_mode,
115 WORD32 lfe_channel_map_count,
116 WORD32* lfe_channel_map) {
117 WORD32 i, err_code = 0;
118
119 ia_drc_params_bs_dec_struct* ia_drc_params_struct =
120 &p_drc_bs_dec_struct->ia_drc_params_struct;
121 ia_drc_params_struct->drc_frame_size = frame_size;
122 if (sample_rate < MIN_DRC_SAMP_FREQ) {
123 return -1;
124 }
125 ia_drc_params_struct->delta_tmin_default = impd_get_delta_tmin(sample_rate);
126 ia_drc_params_struct->num_gain_values_max_default =
127 ia_drc_params_struct->drc_frame_size /
128 ia_drc_params_struct->delta_tmin_default;
129 ia_drc_params_struct->delay_mode = delay_mode;
130
131 if ((frame_size < 1) || (frame_size > AUDIO_CODEC_FRAME_SIZE_MAX) ||
132 (ia_drc_params_struct->drc_frame_size < 0.001f * sample_rate)) {
133 return -1;
134 }
135
136 if (ia_drc_params_struct->delta_tmin_default >
137 ia_drc_params_struct->drc_frame_size) {
138 return -1;
139 }
140
141 if (lfe_channel_map_count >= 0) {
142 if ((lfe_channel_map == NULL) ||
143 (lfe_channel_map_count > MAX_CHANNEL_COUNT)) {
144 return (-1);
145 }
146
147 ia_drc_params_struct->lfe_channel_map_count = lfe_channel_map_count;
148
149 for (i = 0; i < lfe_channel_map_count; i++) {
150 ia_drc_params_struct->lfe_channel_map[i] = lfe_channel_map[i];
151 }
152 } else {
153 ia_drc_params_struct->lfe_channel_map_count = -1;
154
155 for (i = 0; i < MAX_CHANNEL_COUNT; i++) {
156 ia_drc_params_struct->lfe_channel_map[i] = 0;
157 }
158 }
159
160 impd_init_tbls(ia_drc_params_struct->num_gain_values_max_default,
161 &p_drc_bs_dec_struct->tables_default);
162
163 return err_code;
164 }
165
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)166 WORD32 impd_process_drc_bitstream_dec_config(
167 ia_drc_bits_dec_struct* p_drc_bs_dec_struct, ia_bit_buf_struct* it_bit_buff,
168 ia_drc_config* pstr_drc_config, UWORD8* bitstream_config,
169 WORD32 num_bytes) {
170 WORD32 err_code = 0;
171
172 impd_create_init_bit_buf(it_bit_buff, bitstream_config, num_bytes);
173
174 err_code = impd_parse_drc_config(
175 it_bit_buff, &p_drc_bs_dec_struct->ia_drc_params_struct, pstr_drc_config);
176 if (err_code) return (err_code);
177
178 return err_code;
179 }
180
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)181 WORD32 impd_process_drc_bitstream_dec_gain(
182 ia_drc_bits_dec_struct* p_drc_bs_dec_struct, ia_bit_buf_struct* it_bit_buff,
183 ia_drc_config* pstr_drc_config, ia_drc_gain_struct* pstr_drc_gain,
184 UWORD8* bitstream_gain, WORD32 num_bytes, WORD32 num_bits_offset,
185 WORD32* num_bits_read) {
186 WORD32 err_code = 0;
187
188 impd_create_init_bit_buf(it_bit_buff, bitstream_gain, num_bytes);
189
190 impd_read_bits_buf(it_bit_buff, num_bits_offset);
191 if (it_bit_buff->error) return it_bit_buff->error;
192
193 err_code = impd_drc_uni_gain_read(it_bit_buff, p_drc_bs_dec_struct,
194 pstr_drc_config, pstr_drc_gain);
195
196 if (err_code > PROC_COMPLETE) return (err_code);
197
198 *num_bits_read = (it_bit_buff->size) - it_bit_buff->cnt_bits;
199
200 if (err_code == PROC_COMPLETE) {
201 return err_code;
202 }
203
204 return 0;
205 }
206
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)207 WORD32 impd_process_drc_bitstream_dec_loudness_info_set(
208 ia_bit_buf_struct* it_bit_buff,
209 ia_drc_loudness_info_set_struct* pstr_loudness_info,
210 UWORD8* bit_stream_loudness, WORD32 num_bytes_loudness) {
211 WORD32 err_code = 0;
212
213 impd_create_init_bit_buf(it_bit_buff, bit_stream_loudness,
214 num_bytes_loudness);
215
216 err_code = impd_parse_loudness_info_set(it_bit_buff, pstr_loudness_info);
217 return err_code;
218 }
219