/src/libxaac/encoder/iusace_write_bitstream.c
Line | Count | Source |
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 <string.h> |
22 | | #include <math.h> |
23 | | #include "ixheaac_type_def.h" |
24 | | #include "iusace_bitbuffer.h" |
25 | | |
26 | | /* DRC */ |
27 | | #include "impd_drc_common_enc.h" |
28 | | #include "impd_drc_uni_drc.h" |
29 | | #include "impd_drc_tables.h" |
30 | | #include "impd_drc_api.h" |
31 | | #include "impd_drc_uni_drc_eq.h" |
32 | | #include "impd_drc_uni_drc_filter_bank.h" |
33 | | #include "impd_drc_gain_enc.h" |
34 | | #include "impd_drc_struct_def.h" |
35 | | |
36 | | #include "ixheaace_mps_common_define.h" |
37 | | #include "iusace_cnst.h" |
38 | | #include "iusace_tns_usac.h" |
39 | | #include "iusace_psy_mod.h" |
40 | | #include "iusace_tns_usac.h" |
41 | | #include "iusace_config.h" |
42 | | #include "iusace_arith_enc.h" |
43 | | #include "ixheaace_adjust_threshold_data.h" |
44 | | #include "iusace_block_switch_const.h" |
45 | | #include "iusace_block_switch_struct_def.h" |
46 | | #include "iusace_fd_qc_util.h" |
47 | | #include "iusace_fd_quant.h" |
48 | | #include "iusace_ms.h" |
49 | | #include "iusace_signal_classifier.h" |
50 | | #include "ixheaace_sbr_header.h" |
51 | | #include "ixheaace_config.h" |
52 | | #include "ixheaace_asc_write.h" |
53 | | #include "iusace_main.h" |
54 | | #include "iusace_rom.h" |
55 | | |
56 | | WORD32 iusace_write_scf_data(ia_bit_buf_struct *it_bit_buf, WORD32 max_sfb, WORD32 num_sfb, |
57 | | const WORD32 *ptr_scale_factors, WORD32 num_win_grps, |
58 | 943k | WORD32 global_gain, const WORD32 huff_tab[CODE_BOOK_ALPHA_LAV][2]) { |
59 | 943k | WORD32 write_flag = (it_bit_buf != NULL); |
60 | 943k | WORD32 i, j, bit_count = 0; |
61 | 943k | WORD32 diff, length, codeword; |
62 | 943k | WORD32 index = 0; |
63 | 943k | WORD32 previous_scale_factor = global_gain; |
64 | | |
65 | 2.84M | for (j = 0; j < num_win_grps; j++) { |
66 | 39.0M | for (i = 0; i < max_sfb; i++) { |
67 | 37.1M | if (!((i == 0) && (j == 0))) { |
68 | 36.2M | diff = ptr_scale_factors[index] - previous_scale_factor; |
69 | 36.2M | length = huff_tab[diff + 60][0]; |
70 | 36.2M | bit_count += length; |
71 | 36.2M | previous_scale_factor = ptr_scale_factors[index]; |
72 | 36.2M | if (write_flag == 1) { |
73 | 16.9M | codeword = huff_tab[diff + 60][1]; |
74 | 16.9M | iusace_write_bits_buf(it_bit_buf, codeword, (UWORD8)length); |
75 | 16.9M | } |
76 | 36.2M | } |
77 | 37.1M | index++; |
78 | 37.1M | } |
79 | 7.90M | for (; i < num_sfb; i++) { |
80 | 6.00M | index++; |
81 | 6.00M | } |
82 | 1.89M | } |
83 | | |
84 | 943k | return (bit_count); |
85 | 943k | } |
86 | | |
87 | | WORD32 iusace_write_ms_data(ia_bit_buf_struct *it_bit_buf, WORD32 ms_mask, |
88 | | WORD32 ms_used[MAX_SHORT_WINDOWS][MAX_SFB_LONG], WORD32 num_win_grps, |
89 | 192k | WORD32 nr_of_sfb) { |
90 | 192k | WORD32 write_flag = (it_bit_buf != NULL); |
91 | 192k | WORD32 bit_count = 0; |
92 | 192k | WORD32 i, j; |
93 | 192k | WORD32 ms_mask_write = ms_mask; |
94 | | |
95 | 192k | if (write_flag) iusace_write_bits_buf(it_bit_buf, ms_mask_write, 2); |
96 | 192k | bit_count += 2; |
97 | | |
98 | 192k | if (ms_mask_write == 1) { |
99 | 81.4k | for (i = 0; i < num_win_grps; i++) { |
100 | 1.17M | for (j = 0; j < nr_of_sfb; j++) { |
101 | 1.11M | if (write_flag) iusace_write_bits_buf(it_bit_buf, ms_used[i][j], 1); |
102 | 1.11M | bit_count += 1; |
103 | 1.11M | } |
104 | 58.3k | } |
105 | 23.1k | } |
106 | | |
107 | 192k | return bit_count; |
108 | 192k | } |
109 | | |
110 | | WORD32 iusace_write_tns_data(ia_bit_buf_struct *it_bit_buf, ia_tns_info *pstr_tns_info, |
111 | 160k | WORD32 window_sequence, WORD32 core_mode) { |
112 | 160k | WORD32 write_flag = (it_bit_buf != NULL); |
113 | 160k | WORD32 bit_count = 0; |
114 | 160k | WORD32 num_windows; |
115 | 160k | WORD32 len_tns_nfilt; |
116 | 160k | WORD32 len_tns_length; |
117 | 160k | WORD32 len_tns_order; |
118 | 160k | WORD32 filt; |
119 | 160k | WORD32 res_bits; |
120 | 160k | UWORD32 coeff; |
121 | 160k | WORD32 w; |
122 | | |
123 | 160k | if (window_sequence == EIGHT_SHORT_SEQUENCE) { |
124 | 19.2k | num_windows = MAX_SHORT_WINDOWS; |
125 | 19.2k | len_tns_nfilt = 1; |
126 | 19.2k | len_tns_length = 4; |
127 | 19.2k | len_tns_order = 3; |
128 | 141k | } else { |
129 | 141k | num_windows = 1; |
130 | 141k | len_tns_nfilt = 2; |
131 | 141k | len_tns_length = 6; |
132 | 141k | len_tns_order = 4; |
133 | 141k | } |
134 | 160k | if (core_mode == 1) { |
135 | 0 | num_windows = 1; |
136 | 0 | } |
137 | | |
138 | 456k | for (w = 0; w < num_windows; w++) { |
139 | 295k | ia_tns_window_data *ptr_win_data = &pstr_tns_info->window_data[w]; |
140 | 295k | WORD32 n_filt = ptr_win_data->n_filt; |
141 | 295k | if (write_flag) { |
142 | 147k | iusace_write_bits_buf(it_bit_buf, n_filt, (UWORD8)len_tns_nfilt); |
143 | 147k | } |
144 | 295k | bit_count += len_tns_nfilt; |
145 | 295k | if (n_filt) { |
146 | 223k | res_bits = ptr_win_data->coef_res; |
147 | 223k | if (write_flag) { |
148 | 111k | iusace_write_bits_buf(it_bit_buf, res_bits - DEF_TNS_RES_OFFSET, 1); |
149 | 111k | } |
150 | 223k | bit_count += 1; |
151 | 447k | for (filt = 0; filt < n_filt; filt++) { |
152 | 223k | ia_tns_filter_data *ptr_tns_filt = &ptr_win_data->tns_filter[filt]; |
153 | 223k | WORD32 order = ptr_tns_filt->order; |
154 | 223k | if (write_flag) { |
155 | 111k | iusace_write_bits_buf(it_bit_buf, ptr_tns_filt->length, (UWORD8)len_tns_length); |
156 | 111k | iusace_write_bits_buf(it_bit_buf, order, (UWORD8)len_tns_order); |
157 | 111k | } |
158 | 223k | bit_count += (len_tns_length + len_tns_order); |
159 | 223k | if (order) { |
160 | 222k | WORD32 i; |
161 | 222k | if (write_flag) { |
162 | 111k | iusace_write_bits_buf(it_bit_buf, ptr_tns_filt->direction, 1); |
163 | 111k | iusace_write_bits_buf(it_bit_buf, ptr_tns_filt->coef_compress, 1); |
164 | 111k | } |
165 | 222k | bit_count += 2; |
166 | 1.75M | for (i = 1; i <= order; i++) { |
167 | 1.53M | if (write_flag) { |
168 | 766k | coeff = (UWORD32)(ptr_tns_filt->index[i]) & ((1 << res_bits) - 1); |
169 | 766k | iusace_write_bits_buf(it_bit_buf, coeff, (UWORD8)res_bits); |
170 | 766k | } |
171 | 1.53M | bit_count += res_bits; |
172 | 1.53M | } |
173 | 222k | } |
174 | 223k | } |
175 | 223k | } |
176 | 295k | } |
177 | | |
178 | 160k | return bit_count; |
179 | 160k | } |
180 | | |
181 | 75.9k | static WORD32 iusace_calc_grouping_bits(const WORD32 *ptr_win_grp_len, WORD32 num_win_grps) { |
182 | 75.9k | WORD32 grouping_bits = 0; |
183 | 75.9k | WORD32 tmp[8] = {0}; |
184 | 75.9k | WORD32 i, j; |
185 | 75.9k | WORD32 index = 0; |
186 | | |
187 | 367k | for (i = 0; i < num_win_grps; i++) { |
188 | 899k | for (j = 0; j < ptr_win_grp_len[i]; j++) { |
189 | 607k | tmp[index++] = i; |
190 | 607k | } |
191 | 291k | } |
192 | | |
193 | 607k | for (i = 1; i < 8; i++) { |
194 | 531k | grouping_bits = grouping_bits << 1; |
195 | 531k | if (tmp[i] == tmp[i - 1]) { |
196 | 316k | grouping_bits++; |
197 | 316k | } |
198 | 531k | } |
199 | | |
200 | 75.9k | return (grouping_bits); |
201 | 75.9k | } |
202 | | |
203 | | WORD32 iusace_write_ics_info(ia_bit_buf_struct *it_bit_buf, ia_sfb_params_struct *pstr_sfb_prms, |
204 | 235k | WORD32 ch) { |
205 | 235k | WORD32 write_flag = (it_bit_buf != NULL); |
206 | 235k | WORD32 bit_count = 0; |
207 | 235k | WORD32 win_seq = 0; |
208 | 235k | WORD32 grouping_bits = 0; |
209 | 235k | WORD32 max_sfb = pstr_sfb_prms->max_sfb[ch]; |
210 | 235k | WORD32 window_sequence = pstr_sfb_prms->window_sequence[ch]; |
211 | 235k | WORD32 window_shape = pstr_sfb_prms->window_shape[ch]; |
212 | 235k | WORD32 num_win_grps = pstr_sfb_prms->num_window_groups[ch]; |
213 | | |
214 | 235k | switch (window_sequence) { |
215 | 75.9k | case EIGHT_SHORT_SEQUENCE: |
216 | 75.9k | win_seq = 2; |
217 | 75.9k | break; |
218 | 107k | case ONLY_LONG_SEQUENCE: |
219 | 107k | win_seq = 0; |
220 | 107k | break; |
221 | 27.5k | case LONG_START_SEQUENCE: |
222 | 27.6k | case STOP_START_SEQUENCE: |
223 | 27.6k | win_seq = 1; |
224 | 27.6k | break; |
225 | 24.1k | case LONG_STOP_SEQUENCE: |
226 | 24.1k | win_seq = 3; |
227 | 24.1k | break; |
228 | 0 | default: |
229 | 0 | win_seq = 3; |
230 | 0 | break; |
231 | 235k | } |
232 | 235k | if (write_flag) iusace_write_bits_buf(it_bit_buf, win_seq, 2); |
233 | 235k | bit_count += 2; |
234 | | |
235 | 235k | if (write_flag) iusace_write_bits_buf(it_bit_buf, window_shape, 1); |
236 | 235k | bit_count += 1; |
237 | | |
238 | 235k | if (window_sequence == EIGHT_SHORT_SEQUENCE) { |
239 | 75.9k | if (write_flag) iusace_write_bits_buf(it_bit_buf, max_sfb, 4); |
240 | 75.9k | bit_count += 4; |
241 | | |
242 | 75.9k | grouping_bits = |
243 | 75.9k | iusace_calc_grouping_bits(pstr_sfb_prms->window_group_length[ch], num_win_grps); |
244 | 75.9k | if (write_flag) iusace_write_bits_buf(it_bit_buf, grouping_bits, 7); |
245 | 75.9k | bit_count += 7; |
246 | 159k | } else { |
247 | 159k | if (write_flag) iusace_write_bits_buf(it_bit_buf, max_sfb, 6); |
248 | 159k | bit_count += 6; |
249 | 159k | } |
250 | | |
251 | 235k | return (bit_count); |
252 | 235k | } |
253 | | |
254 | | WORD32 iusace_write_cplx_pred_data(ia_bit_buf_struct *it_bit_buf, WORD32 num_win_grps, |
255 | | WORD32 num_sfb, WORD32 complex_coef, |
256 | | WORD32 pred_coeffs_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG], |
257 | | WORD32 pred_coeffs_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG], |
258 | | const WORD32 huff_tab[CODE_BOOK_ALPHA_LAV][2], |
259 | | WORD32 const usac_independency_flg, WORD32 pred_dir, |
260 | | WORD32 cplx_pred_used[MAX_SHORT_WINDOWS][MAX_SFB_LONG], |
261 | | WORD32 cplx_pred_all, WORD32 *ptr_prev_alpha_coeff_re, |
262 | 23.5k | WORD32 *ptr_prev_alpha_coeff_im, WORD32 *delta_code_time) { |
263 | 23.5k | WORD32 write_flag = (it_bit_buf != NULL); |
264 | 23.5k | WORD32 bit_count = 0; |
265 | 23.5k | WORD32 i, j; |
266 | 23.5k | WORD32 g; |
267 | 23.5k | WORD32 sfb; |
268 | 23.5k | const WORD32 sfb_per_pred_band = 2; |
269 | 23.5k | WORD32 length_temp1_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG], |
270 | 23.5k | length_temp2_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG], |
271 | 23.5k | length_temp1_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG], |
272 | 23.5k | length_temp2_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG]; |
273 | 23.5k | WORD32 code_word_temp1_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG], |
274 | 23.5k | code_word_temp2_re[MAX_SHORT_WINDOWS][MAX_SFB_LONG], |
275 | 23.5k | code_word_temp1_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG], |
276 | 23.5k | code_word_temp2_im[MAX_SHORT_WINDOWS][MAX_SFB_LONG]; |
277 | 23.5k | WORD32 length_tot1 = 0, length_tot2 = 0; |
278 | | |
279 | 23.5k | if (write_flag) iusace_write_bits_buf(it_bit_buf, cplx_pred_all, 1); |
280 | 23.5k | bit_count += 1; |
281 | | |
282 | 23.5k | if (cplx_pred_all == 0) { |
283 | 0 | for (g = 0; g < num_win_grps; g++) { |
284 | 0 | for (sfb = 0; sfb < num_sfb; sfb += sfb_per_pred_band) { |
285 | 0 | iusace_write_bits_buf(it_bit_buf, cplx_pred_used[g][sfb], 1); |
286 | 0 | bit_count += 1; |
287 | 0 | } |
288 | 0 | } |
289 | 0 | } |
290 | | |
291 | 23.5k | if (write_flag) iusace_write_bits_buf(it_bit_buf, pred_dir, 1); |
292 | 23.5k | bit_count += 1; |
293 | | |
294 | 23.5k | if (write_flag) iusace_write_bits_buf(it_bit_buf, complex_coef, 1); |
295 | 23.5k | bit_count += 1; |
296 | | |
297 | 23.5k | if (complex_coef) { |
298 | 53 | if (!usac_independency_flg) { |
299 | 37 | if (write_flag) iusace_write_bits_buf(it_bit_buf, 1, 1); /* use_prev_frame */ |
300 | 37 | bit_count += 1; |
301 | 37 | } |
302 | 53 | } |
303 | | |
304 | 23.5k | if (usac_independency_flg) { |
305 | 695 | *delta_code_time = 0; |
306 | 695 | } |
307 | | |
308 | | /* Switching mechanism for delta_code_time */ |
309 | 23.5k | WORD32 prev_pred_coeff_re_temp1 = 0, prev_pred_coeff_re_temp2 = 0; |
310 | 23.5k | WORD32 diff_pred_coeff_re_temp1 = 0, diff_pred_coeff_re_temp2 = 0; |
311 | 23.5k | WORD32 prev_pred_coeff_im_temp1 = 0, prev_pred_coeff_im_temp2 = 0; |
312 | 23.5k | WORD32 diff_pred_coeff_im_temp1 = 0, diff_pred_coeff_im_temp2 = 0; |
313 | | |
314 | 80.2k | for (i = 0; i < num_win_grps; i++) { |
315 | | /* delta_code_time = 0*/ |
316 | 56.7k | prev_pred_coeff_re_temp1 = 0; |
317 | 56.7k | if (complex_coef == 1) { |
318 | 119 | prev_pred_coeff_im_temp1 = 0; |
319 | 119 | } |
320 | | |
321 | 470k | for (j = 0; j < num_sfb; j += 2) { |
322 | 413k | if (!usac_independency_flg) { |
323 | | /* delta_code_time = 1*/ |
324 | 396k | if (i > 0) { |
325 | 145k | prev_pred_coeff_re_temp2 = pred_coeffs_re[i - 1][j]; |
326 | 145k | if (complex_coef == 1) { |
327 | 270 | prev_pred_coeff_im_temp2 = pred_coeffs_im[i - 1][j]; |
328 | 270 | } |
329 | 250k | } else { |
330 | 250k | prev_pred_coeff_re_temp2 = ptr_prev_alpha_coeff_re[j]; |
331 | 250k | if (complex_coef == 1) { |
332 | 648 | prev_pred_coeff_im_temp2 = ptr_prev_alpha_coeff_im[j]; |
333 | 648 | } |
334 | 250k | } |
335 | 396k | } |
336 | | |
337 | 413k | if (cplx_pred_used[i][j] == 1) { |
338 | | /*Differential Huffman coding of real prediction coefficients*/ |
339 | 413k | diff_pred_coeff_re_temp1 = |
340 | 413k | pred_coeffs_re[i][j] - prev_pred_coeff_re_temp1; /* delta_code_time = 0 */ |
341 | 413k | prev_pred_coeff_re_temp1 = pred_coeffs_re[i][j]; /* delta_code_time = 0 */ |
342 | 413k | if (!usac_independency_flg) { |
343 | 396k | diff_pred_coeff_re_temp2 = |
344 | 396k | pred_coeffs_re[i][j] - prev_pred_coeff_re_temp2; /* delta_code_time = 1 */ |
345 | 396k | } |
346 | | |
347 | | /* delta_code_time = 0 */ |
348 | 413k | length_temp1_re[i][j] = huff_tab[diff_pred_coeff_re_temp1 + 60][0]; |
349 | 413k | code_word_temp1_re[i][j] = huff_tab[diff_pred_coeff_re_temp1 + 60][1]; |
350 | | |
351 | 413k | length_tot1 += length_temp1_re[i][j]; |
352 | | |
353 | 413k | if (!usac_independency_flg) { |
354 | | /*delta_code_time = 1 */ |
355 | 396k | length_temp2_re[i][j] = huff_tab[diff_pred_coeff_re_temp2 + 60][0]; |
356 | 396k | code_word_temp2_re[i][j] = huff_tab[diff_pred_coeff_re_temp2 + 60][1]; |
357 | | |
358 | 396k | length_tot2 += length_temp2_re[i][j]; |
359 | 396k | } |
360 | | |
361 | 413k | if (complex_coef == 1) { |
362 | | /*Differential Huffman coding of imaginary prediction coefficients*/ |
363 | 1.37k | diff_pred_coeff_im_temp1 = |
364 | 1.37k | pred_coeffs_im[i][j] - prev_pred_coeff_im_temp1; /* delta_code_time = 0 */ |
365 | 1.37k | prev_pred_coeff_im_temp1 = pred_coeffs_im[i][j]; /* delta_code_time = 0*/ |
366 | | |
367 | 1.37k | if (!usac_independency_flg) { |
368 | 918 | diff_pred_coeff_im_temp2 = |
369 | 918 | pred_coeffs_im[i][j] - prev_pred_coeff_im_temp2; /* delta_code_time = 1 */ |
370 | 918 | } |
371 | | |
372 | | /*delta_code_time = 0*/ |
373 | 1.37k | length_temp1_im[i][j] = huff_tab[diff_pred_coeff_im_temp1 + 60][0]; |
374 | 1.37k | code_word_temp1_im[i][j] = huff_tab[diff_pred_coeff_im_temp1 + 60][1]; |
375 | | |
376 | 1.37k | length_tot1 += length_temp1_im[i][j]; |
377 | | |
378 | 1.37k | if (!usac_independency_flg) { |
379 | | /*delta_code_time = 1*/ |
380 | 918 | length_temp2_im[i][j] = huff_tab[diff_pred_coeff_im_temp2 + 60][0]; |
381 | 918 | code_word_temp2_im[i][j] = huff_tab[diff_pred_coeff_im_temp2 + 60][1]; |
382 | | |
383 | 918 | length_tot2 += length_temp2_im[i][j]; |
384 | 918 | } |
385 | 1.37k | } |
386 | 413k | } else { |
387 | 0 | pred_coeffs_re[i][j] = 0; |
388 | | /*delta_code_time = 0*/ |
389 | 0 | prev_pred_coeff_re_temp1 = pred_coeffs_re[i][j]; |
390 | 0 | if (complex_coef == 1) { |
391 | 0 | pred_coeffs_im[i][j] = 0; |
392 | | /*delta_code_time = 0*/ |
393 | 0 | prev_pred_coeff_im_temp1 = pred_coeffs_im[i][j]; |
394 | 0 | } |
395 | 0 | } |
396 | | |
397 | 413k | ptr_prev_alpha_coeff_re[j] = pred_coeffs_re[i][j]; |
398 | 413k | if (complex_coef == 1) { |
399 | 1.37k | ptr_prev_alpha_coeff_im[j] = pred_coeffs_im[i][j]; |
400 | 1.37k | } |
401 | 413k | } |
402 | | |
403 | 2.13M | for (j = num_sfb; j < MAX_SFB_LONG; j++) { |
404 | 2.07M | pred_coeffs_re[i][j] = 0; |
405 | 2.07M | ptr_prev_alpha_coeff_re[j] = 0; |
406 | 2.07M | if (complex_coef == 1) { |
407 | 3.41k | pred_coeffs_im[i][j] = 0; |
408 | 3.41k | ptr_prev_alpha_coeff_im[j] = 0; |
409 | 3.41k | } |
410 | 2.07M | } |
411 | 56.7k | } |
412 | | |
413 | | /*Make a decison on the value of delta_code_time per frame */ |
414 | 23.5k | if (!usac_independency_flg) { |
415 | | // Compare the code-word lengths |
416 | 22.8k | if (length_tot1 <= length_tot2) { |
417 | 11.8k | *delta_code_time = 0; |
418 | 11.8k | } else { |
419 | 10.9k | *delta_code_time = 1; |
420 | 10.9k | } |
421 | | |
422 | | /* Write the value of delta_code_time to bitstream */ |
423 | 22.8k | if (write_flag) iusace_write_bits_buf(it_bit_buf, *delta_code_time, 1); |
424 | 22.8k | bit_count += 1; |
425 | 22.8k | } |
426 | | |
427 | 23.5k | if (*delta_code_time == 0) { |
428 | 44.7k | for (i = 0; i < num_win_grps; i++) { |
429 | 279k | for (j = 0; j < num_sfb; j += 2) { |
430 | 246k | if (cplx_pred_used[i][j] == 1) { |
431 | 246k | if (write_flag) |
432 | 103k | iusace_write_bits_buf(it_bit_buf, code_word_temp1_re[i][j], |
433 | 103k | (UWORD8)length_temp1_re[i][j]); |
434 | 246k | bit_count += length_temp1_re[i][j]; |
435 | | |
436 | 246k | if (complex_coef == 1) { |
437 | 1.22k | if (write_flag) |
438 | 426 | iusace_write_bits_buf(it_bit_buf, code_word_temp1_im[i][j], |
439 | 426 | (UWORD8)length_temp1_im[i][j]); |
440 | 1.22k | bit_count += length_temp1_im[i][j]; |
441 | 1.22k | } |
442 | 246k | } |
443 | 246k | } |
444 | 32.2k | } |
445 | 12.5k | } else { |
446 | 35.5k | for (i = 0; i < num_win_grps; i++) { |
447 | 191k | for (j = 0; j < num_sfb; j += 2) { |
448 | 166k | if (cplx_pred_used[i][j] == 1) { |
449 | 166k | if (write_flag) |
450 | 82.8k | iusace_write_bits_buf(it_bit_buf, code_word_temp2_re[i][j], |
451 | 82.8k | (UWORD8)length_temp2_re[i][j]); |
452 | 166k | bit_count += length_temp2_re[i][j]; |
453 | | |
454 | 166k | if (complex_coef == 1) { |
455 | 144 | if (write_flag) |
456 | 60 | iusace_write_bits_buf(it_bit_buf, code_word_temp2_im[i][j], |
457 | 60 | (UWORD8)length_temp2_im[i][j]); |
458 | 144 | bit_count += length_temp2_im[i][j]; |
459 | 144 | } |
460 | 166k | } |
461 | 166k | } |
462 | 24.5k | } |
463 | 10.9k | } |
464 | | |
465 | 23.5k | return bit_count; |
466 | 23.5k | } |
467 | | |
468 | | WORD32 iusace_write_cpe(ia_sfb_params_struct *pstr_sfb_prms, ia_bit_buf_struct *it_bit_buf, |
469 | | WORD32 *tns_data_present, WORD32 const usac_independency_flg, |
470 | | ia_usac_encoder_config_struct *pstr_usac_config, |
471 | 192k | ia_usac_data_struct *pstr_usac_data, WORD32 ch) { |
472 | 192k | WORD32 bit_count = 0; |
473 | 192k | WORD32 ms_mask = pstr_usac_data->str_ms_info[ch].ms_mask; |
474 | 192k | WORD32 common_max_sfb = 1; |
475 | 192k | WORD32 tns_active = tns_data_present[0] || tns_data_present[1]; |
476 | 192k | ia_tns_info *pstr_tns_info = pstr_usac_data->pstr_tns_info[ch]; |
477 | 192k | (VOID) pstr_usac_config; |
478 | | |
479 | 192k | iusace_write_bits_buf(it_bit_buf, tns_active, 1); |
480 | 192k | bit_count += 1; |
481 | | |
482 | 192k | iusace_write_bits_buf(it_bit_buf, pstr_sfb_prms->common_win[ch], 1); |
483 | 192k | bit_count += 1; |
484 | | |
485 | 192k | if (pstr_sfb_prms->max_sfb[ch] != pstr_sfb_prms->max_sfb[ch + 1]) { |
486 | 1.57k | common_max_sfb = 0; |
487 | 1.57k | } |
488 | | |
489 | 192k | if (pstr_sfb_prms->common_win[ch]) { |
490 | 192k | bit_count += iusace_write_ics_info(it_bit_buf, pstr_sfb_prms, ch); |
491 | | |
492 | 192k | iusace_write_bits_buf(it_bit_buf, common_max_sfb, 1); |
493 | 192k | bit_count += 1; |
494 | | |
495 | 192k | if (common_max_sfb == 0) { |
496 | 1.57k | if (pstr_sfb_prms->window_sequence[ch] != EIGHT_SHORT_SEQUENCE) { |
497 | 869 | iusace_write_bits_buf(it_bit_buf, pstr_sfb_prms->max_sfb[ch + 1], 6); |
498 | 869 | bit_count += 6; |
499 | 869 | } else { |
500 | 704 | iusace_write_bits_buf(it_bit_buf, pstr_sfb_prms->max_sfb[ch + 1], 4); |
501 | 704 | bit_count += 4; |
502 | 704 | } |
503 | 1.57k | } |
504 | | |
505 | 192k | pstr_sfb_prms->max_sfb_ste = MAX(pstr_sfb_prms->max_sfb[ch], pstr_sfb_prms->max_sfb[ch + 1]); |
506 | | |
507 | 192k | bit_count += |
508 | 192k | iusace_write_ms_data(it_bit_buf, ms_mask, pstr_usac_data->str_ms_info[ch].ms_used, |
509 | 192k | pstr_sfb_prms->num_window_groups[ch], pstr_sfb_prms->max_sfb_ste); |
510 | | |
511 | 192k | { |
512 | 192k | if ((ms_mask == 3) && (pstr_usac_data->stereo_config_index == 0)) { |
513 | 10.4k | bit_count += iusace_write_cplx_pred_data( |
514 | 10.4k | it_bit_buf, pstr_sfb_prms->num_window_groups[ch], pstr_sfb_prms->max_sfb_ste, |
515 | 10.4k | pstr_usac_data->complex_coef[ch], pstr_usac_data->pred_coef_re[ch], |
516 | 10.4k | pstr_usac_data->pred_coef_im[ch], iusace_huffman_code_table, usac_independency_flg, |
517 | 10.4k | pstr_usac_data->pred_dir_idx[ch], pstr_usac_data->cplx_pred_used[ch], |
518 | 10.4k | pstr_usac_data->cplx_pred_all[ch], pstr_usac_data->pred_coef_re_prev[ch], |
519 | 10.4k | pstr_usac_data->pred_coef_im_prev[ch], &pstr_usac_data->delta_code_time[ch]); |
520 | 10.4k | } |
521 | 192k | } |
522 | 192k | } |
523 | | |
524 | 192k | if (tns_active) { |
525 | 44.7k | WORD32 common_tns = 0; |
526 | 44.7k | WORD32 tns_on_lr = 1; |
527 | 44.7k | WORD32 tns_present_both = tns_data_present[0] && tns_data_present[1]; |
528 | 44.7k | WORD32 tns_data_present1 = tns_data_present[1]; |
529 | | |
530 | 44.7k | if (pstr_sfb_prms->common_win[ch]) { |
531 | 44.7k | iusace_write_bits_buf(it_bit_buf, common_tns, 1); |
532 | 44.7k | bit_count += 1; |
533 | 44.7k | } |
534 | | |
535 | 44.7k | iusace_write_bits_buf(it_bit_buf, tns_on_lr, 1); |
536 | 44.7k | bit_count += 1; |
537 | | |
538 | 44.7k | if (common_tns) { |
539 | 0 | bit_count += |
540 | 0 | iusace_write_tns_data(it_bit_buf, pstr_tns_info, pstr_sfb_prms->window_sequence[ch], 0); |
541 | 44.7k | } else { |
542 | 44.7k | iusace_write_bits_buf(it_bit_buf, tns_present_both, 1); |
543 | 44.7k | bit_count += 1; |
544 | | |
545 | 44.7k | if (!tns_present_both) { |
546 | 12.0k | iusace_write_bits_buf(it_bit_buf, tns_data_present1, 1); |
547 | 12.0k | bit_count += 1; |
548 | 12.0k | } |
549 | 44.7k | } |
550 | 44.7k | } |
551 | | |
552 | 192k | return (bit_count); |
553 | 192k | } |
554 | | |
555 | | WORD32 iusace_write_fd_data(ia_bit_buf_struct *it_bit_buf, ia_sfb_params_struct *pstr_sfb_prms, |
556 | | WORD32 num_fac_bits, WORD32 usac_independency_flg, |
557 | | ia_usac_data_struct *pstr_usac_data, |
558 | | ia_usac_encoder_config_struct *pstr_usac_config, WORD32 ch_idx, |
559 | 427k | WORD32 ele_id, WORD32 idx) { |
560 | 427k | WORD32 bit_count = 0; |
561 | 427k | WORD32 fac_data_present = (num_fac_bits > 0) ? 1 : 0; |
562 | 427k | WORD16 *ptr_fac_data = pstr_usac_data->fac_out_stream[ch_idx]; |
563 | | |
564 | 427k | WORD32 is_noise_filling = pstr_usac_data->noise_filling[ele_id]; |
565 | 427k | WORD32 common_window = pstr_sfb_prms->common_win[ch_idx]; |
566 | 427k | ia_usac_quant_info_struct *pstr_quant_info = &(pstr_usac_data->str_quant_info[idx]); |
567 | 427k | ia_tns_info *pstr_tns_info = pstr_usac_data->pstr_tns_info[ch_idx]; |
568 | 427k | WORD32 global_gain = pstr_usac_data->str_quant_info[idx].scale_factor[0]; |
569 | | |
570 | 427k | iusace_write_bits_buf(it_bit_buf, global_gain, 8); |
571 | 427k | bit_count += 8; |
572 | | |
573 | 427k | if (is_noise_filling) { |
574 | 169k | iusace_write_bits_buf(it_bit_buf, pstr_usac_data->noise_level[idx], 3); |
575 | | |
576 | 169k | iusace_write_bits_buf(it_bit_buf, pstr_usac_data->noise_offset[idx], 5); |
577 | 169k | bit_count += 8; |
578 | 169k | } |
579 | | |
580 | 427k | if (!common_window) { |
581 | 42.6k | bit_count += iusace_write_ics_info(it_bit_buf, pstr_sfb_prms, ch_idx); |
582 | 42.6k | } |
583 | | |
584 | 427k | bit_count += iusace_write_scf_data( |
585 | 427k | it_bit_buf, pstr_sfb_prms->max_sfb[ch_idx], pstr_sfb_prms->num_sfb[ch_idx], |
586 | 427k | pstr_quant_info->scale_factor, pstr_sfb_prms->num_window_groups[ch_idx], global_gain, |
587 | 427k | iusace_huffman_code_table); |
588 | | |
589 | 427k | if (pstr_tns_info != NULL && pstr_tns_info->tns_data_present == 1) { |
590 | 80.3k | bit_count += iusace_write_tns_data(it_bit_buf, pstr_tns_info, |
591 | 80.3k | pstr_sfb_prms->window_sequence[ch_idx], 0); |
592 | 80.3k | } |
593 | | |
594 | 427k | if (!usac_independency_flg) { |
595 | 408k | iusace_write_bits_buf(it_bit_buf, pstr_quant_info->reset, 1); |
596 | 408k | bit_count += 1; |
597 | 408k | } |
598 | | |
599 | 427k | if (pstr_quant_info->max_spec_coeffs == FRAME_LEN_SHORT_768) { |
600 | 4.98k | pstr_quant_info->max_spec_coeffs = pstr_quant_info->max_spec_coeffs; |
601 | 4.98k | } |
602 | 427k | bit_count += iusace_arith_enc_spec( |
603 | 427k | it_bit_buf, pstr_sfb_prms->window_sequence[ch_idx], pstr_quant_info->quant_degroup, |
604 | 427k | pstr_quant_info->max_spec_coeffs, pstr_quant_info->c_pres, pstr_quant_info->c_prev, |
605 | 427k | &(pstr_quant_info->arith_size_prev), usac_independency_flg || pstr_quant_info->reset, |
606 | 427k | pstr_usac_config->ccfl); |
607 | | |
608 | 427k | iusace_write_bits_buf(it_bit_buf, fac_data_present, 1); |
609 | 427k | bit_count += 1; |
610 | | |
611 | 427k | if (fac_data_present) { |
612 | 1.26k | WORD32 i; |
613 | 29.2k | for (i = 0; i < num_fac_bits; i += 8) { |
614 | 27.9k | WORD32 bits_to_write = MIN(8, num_fac_bits - i); |
615 | 27.9k | iusace_write_bits_buf(it_bit_buf, ptr_fac_data[i / 8] >> (8 - bits_to_write), |
616 | 27.9k | (UWORD8)bits_to_write); |
617 | 27.9k | } |
618 | 1.26k | bit_count += num_fac_bits; |
619 | 1.26k | } |
620 | | |
621 | 427k | return (bit_count); |
622 | 427k | } |
623 | | |
624 | | WORD32 iusace_count_fd_bits(ia_sfb_params_struct *pstr_sfb_prms, |
625 | | ia_usac_data_struct *pstr_usac_data, WORD32 usac_independency_flg, |
626 | | ia_usac_encoder_config_struct *pstr_usac_config, WORD32 ch_idx, |
627 | 515k | WORD32 idx) { |
628 | 515k | WORD32 bit_count = 0; |
629 | 515k | ia_usac_quant_info_struct *pstr_quant_info = &pstr_usac_data->str_quant_info[idx]; |
630 | 515k | WORD32 window_sequence = pstr_sfb_prms->window_sequence[ch_idx]; |
631 | 515k | WORD32 global_gain = pstr_quant_info->scale_factor[0]; |
632 | 515k | WORD32 max_sfb = pstr_sfb_prms->max_sfb[ch_idx]; |
633 | 515k | WORD32 num_sfb = pstr_sfb_prms->num_sfb[ch_idx]; |
634 | 515k | WORD32 num_win_grps = pstr_sfb_prms->num_window_groups[ch_idx]; |
635 | | |
636 | 515k | bit_count += iusace_write_scf_data(NULL, max_sfb, num_sfb, pstr_quant_info->scale_factor, |
637 | 515k | num_win_grps, global_gain, iusace_huffman_code_table); |
638 | | |
639 | 515k | WORD32 temp_c_pres[516], temp_c_prev[516], temp_size = pstr_quant_info->arith_size_prev; |
640 | 515k | memcpy(temp_c_pres, pstr_quant_info->c_pres, 516 * sizeof(pstr_quant_info->c_pres[0])); |
641 | 515k | memcpy(temp_c_prev, pstr_quant_info->c_prev, 516 * sizeof(pstr_quant_info->c_prev[0])); |
642 | 515k | bit_count += iusace_arith_enc_spec( |
643 | 515k | NULL, window_sequence, pstr_quant_info->quant_degroup, pstr_quant_info->max_spec_coeffs, |
644 | 515k | temp_c_pres, temp_c_prev, &(temp_size), usac_independency_flg || pstr_quant_info->reset, |
645 | 515k | pstr_usac_config->ccfl); |
646 | | |
647 | 515k | return (bit_count); |
648 | 515k | } |
649 | | |
650 | 324k | WORD32 iusace_write_fill_ele(ia_bit_buf_struct *it_bit_buf, WORD32 num_bits) { |
651 | 324k | WORD32 write_flag = (it_bit_buf != NULL); |
652 | 324k | WORD32 bit_count = 0; |
653 | | |
654 | 324k | if (num_bits <= 8) { |
655 | 128k | if (write_flag) { |
656 | 128k | iusace_write_bits_buf(it_bit_buf, 0, 1); |
657 | 128k | } |
658 | 128k | bit_count++; |
659 | 128k | num_bits--; |
660 | 195k | } else { |
661 | 195k | if (write_flag) { |
662 | 195k | iusace_write_bits_buf(it_bit_buf, 1, 1); |
663 | 195k | } |
664 | 195k | bit_count++; |
665 | 195k | num_bits--; |
666 | | |
667 | 195k | if (num_bits <= 8) { |
668 | 43 | if (write_flag) { |
669 | 43 | iusace_write_bits_buf(it_bit_buf, 1, 1); |
670 | 43 | } |
671 | 43 | bit_count++; |
672 | 43 | num_bits--; |
673 | 195k | } else { |
674 | 195k | WORD32 bytes_to_write = 0; |
675 | 195k | if (write_flag) { |
676 | 195k | iusace_write_bits_buf(it_bit_buf, 0, 1); |
677 | 195k | } |
678 | 195k | bit_count++; |
679 | 195k | num_bits--; |
680 | 195k | bytes_to_write = num_bits >> 3; |
681 | | |
682 | 195k | if (bytes_to_write > 255) { |
683 | 127k | bytes_to_write -= 3; |
684 | 127k | if (write_flag) { |
685 | 127k | iusace_write_bits_buf(it_bit_buf, 255, 8); |
686 | 127k | } |
687 | 127k | if (write_flag) { |
688 | 127k | iusace_write_bits_buf(it_bit_buf, bytes_to_write - 253, 16); |
689 | 127k | } |
690 | 127k | bit_count += 24; |
691 | 127k | num_bits -= 24; |
692 | 127k | } else { |
693 | 68.5k | bytes_to_write--; |
694 | 68.5k | if (write_flag) { |
695 | 68.5k | iusace_write_bits_buf(it_bit_buf, bytes_to_write, 8); |
696 | 68.5k | } |
697 | 68.5k | bit_count += 8; |
698 | 68.5k | num_bits -= 8; |
699 | 68.5k | } |
700 | | |
701 | 81.2M | while (bytes_to_write > 0) { |
702 | 81.0M | if (write_flag) { |
703 | 81.0M | iusace_write_bits_buf(it_bit_buf, 0xA9, 8); |
704 | 81.0M | } |
705 | 81.0M | bit_count += 8; |
706 | 81.0M | num_bits -= 8; |
707 | 81.0M | bytes_to_write--; |
708 | 81.0M | } |
709 | 195k | } |
710 | 195k | } |
711 | 324k | return bit_count; |
712 | 324k | } |