/src/libxaac/encoder/ixheaace_ps_bitenc.c
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * * |
3 | | * Copyright (C) 2023 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 | | |
21 | | #include "ixheaac_type_def.h" |
22 | | |
23 | | #include "ixheaace_sbr_header.h" |
24 | | #include "ixheaace_sbr_def.h" |
25 | | #include "ixheaace_resampler.h" |
26 | | #include "ixheaace_sbr_rom.h" |
27 | | #include "ixheaace_common_rom.h" |
28 | | #include "ixheaace_bitbuffer.h" |
29 | | #include "ixheaace_sbr_main.h" |
30 | | |
31 | | #include "ixheaace_sbr_hybrid.h" |
32 | | #include "ixheaace_sbr_ps_enc.h" |
33 | | #include "ixheaace_sbr_ps_bitenc.h" |
34 | | #include "ixheaace_env_bit.h" |
35 | | |
36 | | static WORD32 iexheaax_append_bitstream(ixheaace_bit_buf_handle hdl_bitbuf_write, |
37 | | ixheaace_bit_buf_handle hdl_bitbuf_read, |
38 | 25.3k | UWORD8 num_bits) { |
39 | 25.3k | WORD32 idx; |
40 | 25.3k | UWORD32 value; |
41 | | |
42 | 25.3k | if (num_bits > 16) { |
43 | 3.18k | WORD32 cnt; |
44 | 3.18k | UWORD8 rem; |
45 | 3.18k | cnt = num_bits >> 4; |
46 | 3.18k | rem = num_bits % 16; |
47 | | |
48 | 18.2k | for (idx = 0; idx < cnt; idx++) { |
49 | 15.0k | value = ixheaace_readbits(hdl_bitbuf_read, 16); |
50 | 15.0k | ixheaace_write_bits(hdl_bitbuf_write, value, 16); |
51 | 15.0k | } |
52 | 3.18k | if (rem) { |
53 | 2.97k | value = ixheaace_readbits(hdl_bitbuf_read, rem); |
54 | 2.97k | ixheaace_write_bits(hdl_bitbuf_write, value, rem); |
55 | 2.97k | } |
56 | 22.1k | } else { |
57 | 22.1k | value = ixheaace_readbits(hdl_bitbuf_read, num_bits); |
58 | 22.1k | ixheaace_write_bits(hdl_bitbuf_write, value, num_bits); |
59 | 22.1k | } |
60 | | |
61 | 25.3k | return num_bits; |
62 | 25.3k | } |
63 | | |
64 | | WORD32 |
65 | | ixheaace_enc_write_ps_data(ixheaace_pstr_ps_enc pstr_ps_handle, WORD32 b_header_active, |
66 | 25.3k | ixheaace_str_ps_tab *ps_tables) { |
67 | 25.3k | WORD32 tmp_var, gr; |
68 | 25.3k | const WORD32 *aa_huff_book_iid_c; |
69 | 25.3k | const WORD16 *aa_huff_book_icc_c; |
70 | 25.3k | const WORD8 *aa_huff_book_iid_l; |
71 | 25.3k | const WORD8 *aa_huff_book_icc_l; |
72 | 25.3k | WORD32 *aa_delta_iid; |
73 | 25.3k | WORD32 *aa_delta_icc; |
74 | | |
75 | 25.3k | WORD8 index, last_index; |
76 | 25.3k | WORD32 no_bits_f = 0; |
77 | 25.3k | WORD32 no_bits_t = 0; |
78 | | |
79 | 25.3k | WORD32 aa_delta_iid_t[NUMBER_OF_IID_BINS] = {0}; |
80 | 25.3k | WORD32 aa_delta_icc_t[NUMBER_OF_ICC_BINS] = {0}; |
81 | | |
82 | 25.3k | WORD32 aa_delta_iid_f[NUMBER_OF_IID_BINS] = {0}; |
83 | 25.3k | WORD32 aa_delta_icc_f[NUMBER_OF_ICC_BINS] = {0}; |
84 | | |
85 | 25.3k | WORD32 ab_dt_flag_iid; |
86 | 25.3k | WORD32 ab_dt_flag_icc; |
87 | | |
88 | 25.3k | WORD32 b_send_header; |
89 | | |
90 | 25.3k | UWORD32 b_zero_iid = 1; |
91 | 25.3k | UWORD32 b_zero_icc = 1; |
92 | 25.3k | UWORD32 b_keep_params = 1; |
93 | | |
94 | 25.3k | ixheaace_bit_buf_handle bb = &pstr_ps_handle->ps_bit_buf; |
95 | | |
96 | 25.3k | tmp_var = ia_enhaacplus_enc_get_bits_available(bb); |
97 | | |
98 | | /* bit buffer shall be empty */ |
99 | 25.3k | if (tmp_var != 0) { |
100 | 0 | return -1; |
101 | 0 | } |
102 | | |
103 | 25.3k | if (b_header_active) { |
104 | 2.74k | b_keep_params = 0; |
105 | 2.74k | } |
106 | | |
107 | 25.3k | last_index = 0; |
108 | | |
109 | 477k | for (gr = 0; gr < pstr_ps_handle->iid_icc_bins; gr++) { |
110 | 452k | WORD IID_flag; |
111 | 452k | FLOAT32 pan_value = pstr_ps_handle->aaa_IID_data_buf[gr][SYSTEMLOOKAHEAD]; |
112 | 452k | IID_flag = 0; |
113 | 452k | if ((pan_value >= -ps_tables->pan_class[0]) && (pan_value <= ps_tables->pan_class[0])) { |
114 | 426k | IID_flag = 1; |
115 | 426k | } |
116 | | |
117 | 452k | if (IID_flag) { |
118 | 426k | index = 0; |
119 | 426k | } else { |
120 | 25.3k | if (pan_value < 0) { |
121 | 68.1k | for (index = NO_IID_STEPS - 1; pan_value > -ps_tables->pan_class[index]; index--) { |
122 | 55.5k | } |
123 | 12.5k | index = -index - 1; |
124 | 12.7k | } else { |
125 | 67.1k | for (index = NO_IID_STEPS - 1; pan_value < ps_tables->pan_class[index]; index--) { |
126 | 54.3k | } |
127 | 12.7k | index++; |
128 | 12.7k | } |
129 | | |
130 | 25.3k | b_zero_iid = 0; |
131 | 25.3k | } |
132 | | |
133 | 452k | if (gr == 0) { |
134 | 25.3k | aa_delta_iid_f[gr] = index; |
135 | 25.3k | no_bits_t = 0; |
136 | | |
137 | 25.3k | no_bits_f = ps_tables->a_book_ps_iid_freq_length[index + CODE_BCK_LAV_IID]; |
138 | 426k | } else { |
139 | 426k | aa_delta_iid_f[gr] = index - last_index; |
140 | | |
141 | 426k | no_bits_f += ps_tables->a_book_ps_iid_freq_length[aa_delta_iid_f[gr] + CODE_BCK_LAV_IID]; |
142 | 426k | } |
143 | | |
144 | 452k | last_index = index; |
145 | | |
146 | 452k | aa_delta_iid_t[gr] = index - pstr_ps_handle->a_last_iid_index[gr]; |
147 | | |
148 | 452k | pstr_ps_handle->a_last_iid_index[gr] = index; |
149 | | |
150 | 452k | no_bits_t += ps_tables->a_book_ps_iid_time_length[aa_delta_iid_t[gr] + CODE_BCK_LAV_IID]; |
151 | | |
152 | 452k | if (aa_delta_iid_t[gr] != 0) { |
153 | 36.3k | b_keep_params = 0; |
154 | 36.3k | } |
155 | 452k | } |
156 | | |
157 | 25.3k | if (no_bits_t < no_bits_f && !b_header_active) { |
158 | 757 | ab_dt_flag_iid = 1; |
159 | 757 | aa_delta_iid = aa_delta_iid_t; |
160 | 757 | aa_huff_book_iid_c = ps_tables->a_book_ps_iid_time_code; |
161 | 757 | aa_huff_book_iid_l = ps_tables->a_book_ps_iid_time_length; |
162 | 24.6k | } else { |
163 | 24.6k | ab_dt_flag_iid = 0; |
164 | 24.6k | aa_delta_iid = aa_delta_iid_f; |
165 | 24.6k | aa_huff_book_iid_c = ps_tables->a_book_ps_iid_freq_code; |
166 | 24.6k | aa_huff_book_iid_l = ps_tables->a_book_ps_iid_freq_length; |
167 | 24.6k | } |
168 | | |
169 | 25.3k | last_index = 0; |
170 | | |
171 | 477k | for (gr = 0; gr < pstr_ps_handle->iid_icc_bins; gr++) { |
172 | 452k | WORD ICC_flag; |
173 | 452k | FLOAT32 sa_value = pstr_ps_handle->aaa_ICC_data_buf[gr][SYSTEMLOOKAHEAD]; |
174 | 452k | ICC_flag = 0; |
175 | 452k | if (sa_value <= ps_tables->sa_class[0]) { |
176 | 411k | ICC_flag = 1; |
177 | 411k | } |
178 | 452k | if (ICC_flag) { |
179 | 411k | index = 0; |
180 | 411k | } else { |
181 | 181k | for (index = NO_ICC_STEPS - 2; sa_value < ps_tables->sa_class[index]; index--) { |
182 | 141k | } |
183 | 40.1k | index++; |
184 | | |
185 | 40.1k | b_zero_icc = 0; |
186 | 40.1k | } |
187 | | |
188 | 452k | if (gr == 0) { |
189 | 25.3k | aa_delta_icc_f[gr] = index; |
190 | | |
191 | 25.3k | no_bits_f = ps_tables->a_book_ps_icc_freq_length[index + CODE_BCK_LAV_ICC]; |
192 | | |
193 | 25.3k | no_bits_t = 0; |
194 | 426k | } else { |
195 | 426k | aa_delta_icc_f[gr] = index - last_index; |
196 | | |
197 | 426k | no_bits_f += ps_tables->a_book_ps_icc_freq_length[aa_delta_icc_f[gr] + CODE_BCK_LAV_ICC]; |
198 | 426k | } |
199 | | |
200 | 452k | last_index = index; |
201 | | |
202 | 452k | aa_delta_icc_t[gr] = index - pstr_ps_handle->a_last_icc_index[gr]; |
203 | | |
204 | 452k | pstr_ps_handle->a_last_icc_index[gr] = index; |
205 | | |
206 | 452k | no_bits_t += ps_tables->a_book_ps_icc_time_length[aa_delta_icc_t[gr] + CODE_BCK_LAV_ICC]; |
207 | | |
208 | 452k | if (aa_delta_icc_t[gr] != 0) { |
209 | 48.4k | b_keep_params = 0; |
210 | 48.4k | } |
211 | 452k | } |
212 | | |
213 | 25.3k | if (no_bits_t < no_bits_f && !b_header_active) { |
214 | 488 | ab_dt_flag_icc = 1; |
215 | 488 | aa_delta_icc = aa_delta_icc_t; |
216 | 488 | aa_huff_book_icc_c = ps_tables->a_book_ps_icc_time_code; |
217 | 488 | aa_huff_book_icc_l = ps_tables->a_book_ps_icc_time_length; |
218 | 24.8k | } else { |
219 | 24.8k | ab_dt_flag_icc = 0; |
220 | 24.8k | aa_delta_icc = aa_delta_icc_f; |
221 | 24.8k | aa_huff_book_icc_c = ps_tables->a_book_ps_icc_freq_code; |
222 | 24.8k | aa_huff_book_icc_l = ps_tables->a_book_ps_icc_freq_length; |
223 | 24.8k | } |
224 | | |
225 | 25.3k | { |
226 | 25.3k | static WORD32 initheader = 0; |
227 | | |
228 | 25.3k | if (!initheader || b_header_active) { |
229 | 2.74k | initheader = 1; |
230 | 2.74k | pstr_ps_handle->b_enable_header = 1; |
231 | 22.6k | } else { |
232 | 22.6k | pstr_ps_handle->b_enable_header = 0; |
233 | 22.6k | } |
234 | 25.3k | } |
235 | | |
236 | 25.3k | b_send_header = pstr_ps_handle->b_enable_header || |
237 | 25.3k | pstr_ps_handle->b_prev_zero_iid != b_zero_iid || |
238 | 25.3k | pstr_ps_handle->b_prev_zero_icc != b_zero_icc; |
239 | | |
240 | 25.3k | ixheaace_write_bits(bb, b_send_header, 1); |
241 | | |
242 | 25.3k | if (b_send_header) { |
243 | 4.70k | ixheaace_write_bits(bb, !b_zero_iid, 1); |
244 | | |
245 | 4.70k | if (!b_zero_iid) { |
246 | 1.29k | ixheaace_write_bits(bb, (pstr_ps_handle->b_hi_freq_res_iid_icc) ? 1 : 0, 3); |
247 | 1.29k | } |
248 | | |
249 | 4.70k | ixheaace_write_bits(bb, !b_zero_icc, 1); |
250 | | |
251 | 4.70k | if (!b_zero_icc) { |
252 | 1.39k | ixheaace_write_bits(bb, (pstr_ps_handle->b_hi_freq_res_iid_icc) ? 1 : 0, 3); |
253 | 1.39k | } |
254 | | |
255 | 4.70k | ixheaace_write_bits(bb, 0, 1); |
256 | 4.70k | } |
257 | | |
258 | 25.3k | ixheaace_write_bits(bb, 0, 1); |
259 | | |
260 | 25.3k | ixheaace_write_bits(bb, 1 - b_keep_params, 2); |
261 | | |
262 | 25.3k | if (!b_keep_params) { |
263 | 6.45k | if (!b_zero_iid) { |
264 | 3.02k | ixheaace_write_bits(bb, ab_dt_flag_iid, 1); |
265 | | |
266 | 60.4k | for (gr = 0; gr < pstr_ps_handle->iid_icc_bins; gr++) { |
267 | 57.4k | ixheaace_write_bits(bb, aa_huff_book_iid_c[aa_delta_iid[gr] + CODE_BCK_LAV_IID], |
268 | 57.4k | aa_huff_book_iid_l[aa_delta_iid[gr] + CODE_BCK_LAV_IID]); |
269 | 57.4k | } |
270 | 3.02k | } |
271 | 6.45k | } |
272 | | |
273 | 25.3k | if (!b_keep_params) { |
274 | 6.45k | if (!b_zero_icc) { |
275 | 3.14k | ixheaace_write_bits(bb, ab_dt_flag_icc, 1); |
276 | | |
277 | 62.5k | for (gr = 0; gr < pstr_ps_handle->iid_icc_bins; gr++) { |
278 | 59.4k | ixheaace_write_bits(bb, aa_huff_book_icc_c[aa_delta_icc[gr] + CODE_BCK_LAV_ICC], |
279 | 59.4k | aa_huff_book_icc_l[aa_delta_icc[gr] + CODE_BCK_LAV_ICC]); |
280 | 59.4k | } |
281 | 3.14k | } |
282 | 6.45k | } |
283 | | |
284 | 25.3k | pstr_ps_handle->b_prev_zero_iid = b_zero_iid; |
285 | 25.3k | pstr_ps_handle->b_prev_zero_icc = b_zero_icc; |
286 | | |
287 | 25.3k | return ia_enhaacplus_enc_get_bits_available(bb); |
288 | 25.3k | } |
289 | | |
290 | | WORD32 |
291 | | ixheaace_append_ps_bitstream(ixheaace_pstr_ps_enc pstr_ps_handle, ixheaace_bit_buf_handle hdl_bs, |
292 | 50.7k | WORD32 *sbr_hdr_bits) { |
293 | 50.7k | if (!pstr_ps_handle) { |
294 | 0 | return 0; |
295 | 0 | } |
296 | 50.7k | if (!hdl_bs) { |
297 | 25.3k | return ia_enhaacplus_enc_get_bits_available(&pstr_ps_handle->ps_bit_buf); |
298 | 25.3k | } else { |
299 | 25.3k | UWORD8 num_bits = (UWORD8)ia_enhaacplus_enc_get_bits_available(&pstr_ps_handle->ps_bit_buf); |
300 | | |
301 | 25.3k | ixheaace_write_bits(hdl_bs, EXTENSION_ID_PS_CODING, SI_SBR_EXTENSION_ID_BITS); |
302 | 25.3k | iexheaax_append_bitstream(hdl_bs, &pstr_ps_handle->ps_bit_buf, num_bits); |
303 | | |
304 | 25.3k | pstr_ps_handle->bit_buf_read_offset = (WORD32)(pstr_ps_handle->ps_bit_buf.ptr_read_next - |
305 | 25.3k | pstr_ps_handle->ps_bit_buf.ptr_bit_buf_base); |
306 | 25.3k | pstr_ps_handle->bit_buf_write_offset = (WORD32)(pstr_ps_handle->ps_bit_buf.ptr_write_next - |
307 | 25.3k | pstr_ps_handle->ps_bit_buf.ptr_bit_buf_base); |
308 | | |
309 | 25.3k | return ia_enhaacplus_enc_get_bits_available(hdl_bs) - (*sbr_hdr_bits) - |
310 | 25.3k | SI_FILL_EXTENTION_BITS; |
311 | 25.3k | } |
312 | 50.7k | } |