/src/libxaac/decoder/drc_src/impd_drc_bitbuffer.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
33 | 920k | WORD32 impd_read_bits_buf(ia_bit_buf_struct* it_bit_buff, WORD no_of_bits) { |
34 | 920k | UWORD32 ret_val; |
35 | 920k | UWORD8* ptr_read_next = it_bit_buff->ptr_read_next; |
36 | 920k | WORD bit_pos = it_bit_buff->bit_pos; |
37 | | |
38 | 920k | if (it_bit_buff->cnt_bits <= 0) { |
39 | 11.2k | it_bit_buff->error = 1; |
40 | 11.2k | return -1; |
41 | 11.2k | } |
42 | | |
43 | 909k | if (no_of_bits == 0) { |
44 | 18.7k | return 0; |
45 | 18.7k | } |
46 | | |
47 | 890k | it_bit_buff->cnt_bits -= no_of_bits; |
48 | 890k | ret_val = (UWORD32)*ptr_read_next; |
49 | | |
50 | 890k | bit_pos -= no_of_bits; |
51 | 1.15M | while (bit_pos < 0) { |
52 | 260k | bit_pos += 8; |
53 | 260k | ptr_read_next++; |
54 | | |
55 | 260k | if (ptr_read_next > it_bit_buff->ptr_bit_buf_end) { |
56 | 7.44k | ptr_read_next = it_bit_buff->ptr_bit_buf_base; |
57 | 7.44k | } |
58 | | |
59 | 260k | ret_val <<= 8; |
60 | | |
61 | 260k | ret_val |= (UWORD32)*ptr_read_next; |
62 | 260k | } |
63 | | |
64 | 890k | ret_val = ret_val << ((31 - no_of_bits) - bit_pos) >> (32 - no_of_bits); |
65 | 890k | it_bit_buff->ptr_read_next = ptr_read_next; |
66 | 890k | it_bit_buff->bit_pos = (WORD16)bit_pos; |
67 | 890k | return ret_val; |
68 | 909k | } |
69 | | |
70 | 28.1k | WORD32 impd_skip_bits_buf(ia_bit_buf_struct* it_bit_buff, WORD no_of_bits) { |
71 | 28.1k | UWORD8* ptr_read_next = it_bit_buff->ptr_read_next; |
72 | 28.1k | WORD bit_pos = it_bit_buff->bit_pos; |
73 | | |
74 | 28.1k | if (it_bit_buff->cnt_bits < no_of_bits) { |
75 | 2.47k | it_bit_buff->error = 1; |
76 | 2.47k | return -1; |
77 | 2.47k | } |
78 | | |
79 | 25.7k | it_bit_buff->cnt_bits -= no_of_bits; |
80 | | |
81 | 25.7k | bit_pos -= no_of_bits; |
82 | 414k | while (bit_pos < 0) { |
83 | 388k | bit_pos += 8; |
84 | 388k | ptr_read_next++; |
85 | 388k | } |
86 | 25.7k | it_bit_buff->ptr_read_next = ptr_read_next; |
87 | 25.7k | it_bit_buff->bit_pos = (WORD16)bit_pos; |
88 | 25.7k | return no_of_bits; |
89 | 28.1k | } |
90 | | VOID impd_create_bit_buf(ia_bit_buf_struct* it_bit_buff, |
91 | 27.7k | UWORD8* ptr_bit_buf_base, WORD32 bit_buf_size) { |
92 | 27.7k | it_bit_buff->ptr_bit_buf_base = ptr_bit_buf_base; |
93 | 27.7k | it_bit_buff->ptr_bit_buf_end = ptr_bit_buf_base + bit_buf_size - 1; |
94 | | |
95 | 27.7k | it_bit_buff->ptr_read_next = ptr_bit_buf_base; |
96 | 27.7k | it_bit_buff->bit_pos = 7; |
97 | | |
98 | 27.7k | it_bit_buff->cnt_bits = 0; |
99 | 27.7k | it_bit_buff->size = bit_buf_size << 3; |
100 | 27.7k | it_bit_buff->error = 0; |
101 | | |
102 | 27.7k | return; |
103 | 27.7k | } |
104 | | |
105 | | VOID impd_create_init_bit_buf(ia_bit_buf_struct* it_bit_buff, |
106 | 27.7k | UWORD8* ptr_bit_buf_base, WORD32 bit_buf_size) { |
107 | 27.7k | impd_create_bit_buf(it_bit_buff, ptr_bit_buf_base, bit_buf_size); |
108 | 27.7k | it_bit_buff->cnt_bits = (bit_buf_size << 3); |
109 | 27.7k | return; |
110 | 27.7k | } |
111 | | |
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 | 2.35k | WORD32* lfe_channel_map) { |
117 | 2.35k | WORD32 i, err_code = 0; |
118 | | |
119 | 2.35k | ia_drc_params_bs_dec_struct* ia_drc_params_struct = |
120 | 2.35k | &p_drc_bs_dec_struct->ia_drc_params_struct; |
121 | 2.35k | ia_drc_params_struct->drc_frame_size = frame_size; |
122 | 2.35k | if (sample_rate < MIN_DRC_SAMP_FREQ) { |
123 | 1 | return -1; |
124 | 1 | } |
125 | 2.35k | ia_drc_params_struct->delta_tmin_default = impd_get_delta_tmin(sample_rate); |
126 | 2.35k | ia_drc_params_struct->num_gain_values_max_default = |
127 | 2.35k | ia_drc_params_struct->drc_frame_size / |
128 | 2.35k | ia_drc_params_struct->delta_tmin_default; |
129 | 2.35k | ia_drc_params_struct->delay_mode = delay_mode; |
130 | | |
131 | 2.35k | if ((frame_size < 1) || (frame_size > AUDIO_CODEC_FRAME_SIZE_MAX) || |
132 | 2.35k | (ia_drc_params_struct->drc_frame_size < 0.001f * sample_rate)) { |
133 | 0 | return -1; |
134 | 0 | } |
135 | | |
136 | 2.35k | if (ia_drc_params_struct->delta_tmin_default > |
137 | 2.35k | ia_drc_params_struct->drc_frame_size) { |
138 | 0 | return -1; |
139 | 0 | } |
140 | | |
141 | 2.35k | if (lfe_channel_map_count >= 0) { |
142 | 0 | if ((lfe_channel_map == NULL) || |
143 | 0 | (lfe_channel_map_count > MAX_CHANNEL_COUNT)) { |
144 | 0 | return (-1); |
145 | 0 | } |
146 | | |
147 | 0 | ia_drc_params_struct->lfe_channel_map_count = lfe_channel_map_count; |
148 | |
|
149 | 0 | for (i = 0; i < lfe_channel_map_count; i++) { |
150 | 0 | ia_drc_params_struct->lfe_channel_map[i] = lfe_channel_map[i]; |
151 | 0 | } |
152 | 2.35k | } else { |
153 | 2.35k | ia_drc_params_struct->lfe_channel_map_count = -1; |
154 | | |
155 | 21.1k | for (i = 0; i < MAX_CHANNEL_COUNT; i++) { |
156 | 18.8k | ia_drc_params_struct->lfe_channel_map[i] = 0; |
157 | 18.8k | } |
158 | 2.35k | } |
159 | | |
160 | 2.35k | impd_init_tbls(ia_drc_params_struct->num_gain_values_max_default, |
161 | 2.35k | &p_drc_bs_dec_struct->tables_default); |
162 | | |
163 | 2.35k | return err_code; |
164 | 2.35k | } |
165 | | |
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 | 2.35k | WORD32 num_bytes) { |
170 | 2.35k | WORD32 err_code = 0; |
171 | | |
172 | 2.35k | impd_create_init_bit_buf(it_bit_buff, bitstream_config, num_bytes); |
173 | | |
174 | 2.35k | err_code = impd_parse_drc_config( |
175 | 2.35k | it_bit_buff, &p_drc_bs_dec_struct->ia_drc_params_struct, pstr_drc_config); |
176 | 2.35k | if (err_code) return (err_code); |
177 | | |
178 | 90 | return err_code; |
179 | 2.35k | } |
180 | | |
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 | 21.0k | WORD32* num_bits_read) { |
186 | 21.0k | WORD32 err_code = 0; |
187 | | |
188 | 21.0k | impd_create_init_bit_buf(it_bit_buff, bitstream_gain, num_bytes); |
189 | | |
190 | 21.0k | impd_read_bits_buf(it_bit_buff, num_bits_offset); |
191 | 21.0k | if (it_bit_buff->error) return it_bit_buff->error; |
192 | | |
193 | 18.6k | err_code = impd_drc_uni_gain_read(it_bit_buff, p_drc_bs_dec_struct, |
194 | 18.6k | pstr_drc_config, pstr_drc_gain); |
195 | | |
196 | 18.6k | if (err_code > PROC_COMPLETE) return (err_code); |
197 | | |
198 | 16.0k | *num_bits_read = (it_bit_buff->size) - it_bit_buff->cnt_bits; |
199 | | |
200 | 16.0k | if (err_code == PROC_COMPLETE) { |
201 | 7.98k | return err_code; |
202 | 7.98k | } |
203 | | |
204 | 8.11k | return 0; |
205 | 16.0k | } |
206 | | |
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 | 1.99k | UWORD8* bit_stream_loudness, WORD32 num_bytes_loudness) { |
211 | 1.99k | WORD32 err_code = 0; |
212 | | |
213 | 1.99k | impd_create_init_bit_buf(it_bit_buff, bit_stream_loudness, |
214 | 1.99k | num_bytes_loudness); |
215 | | |
216 | 1.99k | err_code = impd_parse_loudness_info_set(it_bit_buff, pstr_loudness_info); |
217 | 1.99k | return err_code; |
218 | 1.99k | } |