/src/libxaac/encoder/ixheaace_mps_nlc_enc.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 <string.h> |
22 | | #include <stdlib.h> |
23 | | #include <math.h> |
24 | | #include "ixheaac_type_def.h" |
25 | | #include "ixheaac_error_standards.h" |
26 | | #include "ixheaace_error_codes.h" |
27 | | #include "ixheaace_mps_common_fix.h" |
28 | | #include "ixheaace_mps_defines.h" |
29 | | #include "ixheaace_mps_common_define.h" |
30 | | #include "ixheaace_bitbuffer.h" |
31 | | #include "ixheaace_mps_nlc_enc.h" |
32 | | #include "ixheaace_mps_huff_tab.h" |
33 | | #include "ixheaace_mps_param_extract.h" |
34 | | #include "ixheaace_mps_sac_nlc_enc.h" |
35 | | #include "ixheaace_mps_bitstream.h" |
36 | | #include "ixheaace_mps_struct_def.h" |
37 | | #include "ixheaace_mps_sac_polyphase.h" |
38 | | #include "ixheaace_mps_sac_nlc_enc.h" |
39 | | #include "ixheaace_mps_sac_hybfilter.h" |
40 | | #include "ixheaace_mps_spatial_bitstream.h" |
41 | | #include "ixheaace_mps_tree.h" |
42 | | #include "ixheaace_mps_rom.h" |
43 | | #include "ixheaace_common_utils.h" |
44 | | #include "ixheaac_constants.h" |
45 | | #include "ixheaac_basic_ops32.h" |
46 | | #include "ixheaac_basic_ops40.h" |
47 | | #include "ixheaac_basic_ops.h" |
48 | | |
49 | | static UWORD8 ixheaace_mps_212_sym_check(WORD16 data[2], const WORD16 lav, |
50 | 789k | WORD16 *const p_sym_bits) { |
51 | 789k | UWORD8 sym_bits = 0; |
52 | 789k | UWORD8 num_sbits = 0; |
53 | 789k | WORD16 sum_val = data[0] + data[1]; |
54 | 789k | WORD16 diff_val = data[0] - data[1]; |
55 | | |
56 | 789k | if (sum_val != 0) { |
57 | 245k | WORD8 sum_neg = (sum_val < 0) ? 1 : 0; |
58 | 245k | if (sum_neg) { |
59 | 116k | sum_val = -sum_val; |
60 | 116k | diff_val = -diff_val; |
61 | 116k | } |
62 | 245k | sym_bits = (sym_bits << 1) | sum_neg; |
63 | 245k | num_sbits++; |
64 | 245k | } |
65 | | |
66 | 789k | if (diff_val != 0) { |
67 | 230k | WORD8 diff_neg = (diff_val < 0) ? 1 : 0; |
68 | 230k | if (diff_neg) { |
69 | 118k | diff_val = -diff_val; |
70 | 118k | } |
71 | 230k | sym_bits = (sym_bits << 1) | diff_neg; |
72 | 230k | num_sbits++; |
73 | 230k | } |
74 | 789k | *p_sym_bits = sym_bits; |
75 | | |
76 | 789k | if (!(sum_val % 2)) { |
77 | 629k | data[0] = (sum_val / 2); |
78 | 629k | data[1] = (diff_val / 2); |
79 | 629k | } else { |
80 | 160k | data[0] = (lav - sum_val / 2); |
81 | 160k | data[1] = (lav - diff_val / 2); |
82 | 160k | } |
83 | | |
84 | 789k | return num_sbits; |
85 | 789k | } |
86 | | |
87 | 35.9k | static WORD16 ixheaace_mps_212_calc_pcm_bits(const WORD16 num_val, const WORD16 n_levels) { |
88 | 35.9k | WORD16 num_complete_chunks = 0, rest_chunk_size = 0; |
89 | 35.9k | WORD16 max_grp_len = 0, bits_pcm = 0; |
90 | 35.9k | WORD32 chunk_levels, lvl; |
91 | | |
92 | 35.9k | switch (n_levels) { |
93 | 0 | case 7: |
94 | 0 | max_grp_len = 6; |
95 | 0 | break; |
96 | 0 | case 3: |
97 | 0 | case 6: |
98 | 0 | max_grp_len = 5; |
99 | 0 | break; |
100 | 0 | case 13: |
101 | 0 | case 19: |
102 | 0 | case 51: |
103 | 0 | max_grp_len = 4; |
104 | 0 | break; |
105 | 0 | case 25: |
106 | 0 | max_grp_len = 3; |
107 | 0 | break; |
108 | 6 | case 11: |
109 | 6 | max_grp_len = 2; |
110 | 6 | break; |
111 | 35.9k | default: |
112 | 35.9k | max_grp_len = 1; |
113 | 35.9k | } |
114 | | |
115 | 35.9k | num_complete_chunks = num_val / max_grp_len; |
116 | 35.9k | rest_chunk_size = num_val % max_grp_len; |
117 | | |
118 | 35.9k | chunk_levels = 1; |
119 | 71.9k | for (lvl = 1; lvl <= max_grp_len; lvl++) { |
120 | 35.9k | chunk_levels *= n_levels; |
121 | 35.9k | } |
122 | 35.9k | bits_pcm = (WORD16)(round(log(chunk_levels) / log(2)) * num_complete_chunks); |
123 | 35.9k | bits_pcm += (WORD16)(round(log(chunk_levels) / log(2)) * rest_chunk_size); |
124 | | |
125 | 35.9k | return bits_pcm; |
126 | 35.9k | } |
127 | | |
128 | | static VOID ixheaace_mps_212_apply_pcm_coding(ixheaace_bit_buf_handle pstr_bit_buf, |
129 | | const WORD16 *const in_data_1, |
130 | | const WORD16 *const in_data_2, const WORD16 offset, |
131 | 2.35k | const WORD16 num_val, const WORD16 n_levels) { |
132 | 2.35k | WORD16 val = 0, lvl = 0, idx = 0; |
133 | 2.35k | WORD16 max_grp_len = 0, grp_len = 0, next_val = 0; |
134 | 2.35k | WORD32 grp_val = 0, chunk_levels = 0; |
135 | 2.35k | UWORD8 pcm_block_size[7] = {0}; |
136 | | |
137 | 2.35k | switch (n_levels) { |
138 | 0 | case 7: |
139 | 0 | max_grp_len = 6; |
140 | 0 | break; |
141 | 0 | case 3: |
142 | 0 | case 6: |
143 | 0 | case 9: |
144 | 0 | max_grp_len = 5; |
145 | 0 | break; |
146 | 0 | case 13: |
147 | 0 | case 19: |
148 | 0 | case 51: |
149 | 0 | max_grp_len = 4; |
150 | 0 | break; |
151 | 0 | case 5: |
152 | 0 | case 25: |
153 | 0 | max_grp_len = 3; |
154 | 0 | break; |
155 | 6 | case 11: |
156 | 6 | max_grp_len = 2; |
157 | 6 | break; |
158 | 2.34k | default: |
159 | 2.34k | max_grp_len = 1; |
160 | 2.35k | } |
161 | | |
162 | 2.35k | chunk_levels = 1; |
163 | 4.71k | for (lvl = 1; lvl <= max_grp_len; lvl++) { |
164 | 2.36k | chunk_levels *= n_levels; |
165 | 2.36k | pcm_block_size[lvl] = (UWORD8)round(log(chunk_levels) / log(2)); |
166 | 2.36k | } |
167 | | |
168 | 45.6k | for (val = 0; val < num_val; val += max_grp_len) { |
169 | 43.2k | grp_len = MIN(max_grp_len, num_val - val); |
170 | 43.2k | grp_val = 0; |
171 | 86.5k | for (lvl = 0; lvl < grp_len; lvl++) { |
172 | 43.2k | idx = val + lvl; |
173 | 43.2k | next_val = (in_data_2 == NULL) |
174 | 43.2k | ? in_data_1[idx] |
175 | 43.2k | : (in_data_1 == NULL) |
176 | 1.47k | ? in_data_2[idx] |
177 | 1.47k | : ((idx & 01) ? in_data_2[idx >> 1] : in_data_1[idx >> 1]); |
178 | 43.2k | grp_val = grp_val * n_levels + next_val + offset; |
179 | 43.2k | } |
180 | 43.2k | ixheaace_write_bits(pstr_bit_buf, grp_val, pcm_block_size[grp_len]); |
181 | 43.2k | } |
182 | 2.35k | } |
183 | | |
184 | | static IA_ERRORCODE ixheaace_mps_212_huff_enc_1_d(ixheaace_bit_buf_handle pstr_bit_buf, |
185 | | const WORD32 data_type, const WORD32 dim_1, |
186 | | WORD16 *const in_data, const WORD16 num_bands, |
187 | | const WORD16 part_0_flag, |
188 | 110k | UWORD16 *huff_code_bits) { |
189 | 110k | WORD32 band, offset = 0; |
190 | 110k | UWORD16 huff_bits = 0; |
191 | 110k | ixheaace_mps_huff_entry part0; |
192 | 110k | const ixheaace_mps_huff_entry *p_huff_tab = NULL; |
193 | | |
194 | 110k | *huff_code_bits = 0; |
195 | 110k | if (data_type == IXHEAACE_MPS_SAC_DATA_TYPE_ICC) { |
196 | 54.3k | p_huff_tab = ixheaace_mps_212_huff_icc_tab.h1_d[dim_1]; |
197 | 55.7k | } else if (data_type == IXHEAACE_MPS_SAC_DATA_TYPE_CLD) { |
198 | 55.7k | p_huff_tab = ixheaace_mps_212_huff_cld_tab.h1_d[dim_1]; |
199 | 55.7k | } else { |
200 | 0 | return IA_EXHEAACE_EXE_FATAL_MPS_INVALID_HUFF_DATA_TYPE; |
201 | 0 | } |
202 | | |
203 | 110k | if (part_0_flag) { |
204 | 48.3k | if (data_type == IXHEAACE_MPS_SAC_DATA_TYPE_ICC) { |
205 | 19.4k | part0 = ixheaace_mps_212_huff_part_0_tab.icc[in_data[0]]; |
206 | 28.8k | } else if (data_type == IXHEAACE_MPS_SAC_DATA_TYPE_CLD) { |
207 | 28.8k | part0 = ixheaace_mps_212_huff_part_0_tab.cld[in_data[0]]; |
208 | 28.8k | } else { |
209 | 0 | return IA_EXHEAACE_EXE_FATAL_MPS_INVALID_HUFF_DATA_TYPE; |
210 | 0 | } |
211 | 48.3k | huff_bits += ixheaace_write_bits(pstr_bit_buf, part0.value, part0.length); |
212 | 48.3k | offset = 1; |
213 | 48.3k | } |
214 | | |
215 | 1.67M | for (band = offset; band < num_bands; band++) { |
216 | 1.56M | WORD32 id_sign = 0; |
217 | 1.56M | WORD32 id = in_data[band]; |
218 | | |
219 | 1.56M | if (id != 0) { |
220 | 407k | id_sign = 0; |
221 | 407k | if (id < 0) { |
222 | 192k | id = -id; |
223 | 192k | id_sign = 1; |
224 | 192k | } |
225 | 407k | } |
226 | | |
227 | 1.56M | huff_bits += ixheaace_write_bits(pstr_bit_buf, p_huff_tab[id].value, p_huff_tab[id].length); |
228 | | |
229 | 1.56M | if (id != 0) { |
230 | 407k | huff_bits += ixheaace_write_bits(pstr_bit_buf, id_sign, 1); |
231 | 407k | } |
232 | 1.56M | } |
233 | | |
234 | 110k | *huff_code_bits = huff_bits; |
235 | 110k | return IA_NO_ERROR; |
236 | 110k | } |
237 | | |
238 | | static VOID ixheaace_mps_212_get_huff_entry(const WORD32 lav, const WORD32 data_type, |
239 | | const WORD32 band, WORD16 tab_idx_2_d[2], |
240 | | WORD16 in_data[][2], |
241 | | ixheaace_mps_huff_entry *const pstr_huff_entry, |
242 | 789k | ixheaace_mps_huff_entry *const pstr_huff_escape) { |
243 | 789k | const ixheaace_mps_huff_cld_tab_2d *pstr_huff_cld_tab_2d = |
244 | 789k | &ixheaace_mps_212_huff_cld_tab.h2_d[tab_idx_2_d[0]][tab_idx_2_d[1]]; |
245 | 789k | const ixheaace_mps_huff_icc_tab_2d *pstr_huff_icc_tab_2d = |
246 | 789k | &ixheaace_mps_212_huff_icc_tab.h2_d[tab_idx_2_d[0]][tab_idx_2_d[1]]; |
247 | | |
248 | 789k | switch (lav) { |
249 | 4.51k | case 9: { |
250 | 4.51k | *pstr_huff_entry = pstr_huff_cld_tab_2d->lav9.entry[in_data[band][0]][in_data[band][1]]; |
251 | 4.51k | *pstr_huff_escape = pstr_huff_cld_tab_2d->lav9.escape; |
252 | 4.51k | } break; |
253 | 38.5k | case 7: { |
254 | 38.5k | switch (data_type) { |
255 | 10.0k | case IXHEAACE_MPS_SAC_DATA_TYPE_CLD: |
256 | 10.0k | *pstr_huff_entry = pstr_huff_cld_tab_2d->lav7.entry[in_data[band][0]][in_data[band][1]]; |
257 | 10.0k | *pstr_huff_escape = pstr_huff_cld_tab_2d->lav7.escape; |
258 | 10.0k | break; |
259 | 28.4k | case IXHEAACE_MPS_SAC_DATA_TYPE_ICC: |
260 | 28.4k | *pstr_huff_entry = pstr_huff_icc_tab_2d->lav7.entry[in_data[band][0]][in_data[band][1]]; |
261 | 28.4k | *pstr_huff_escape = pstr_huff_icc_tab_2d->lav7.escape; |
262 | 28.4k | break; |
263 | 38.5k | } |
264 | 38.5k | } break; |
265 | 80.1k | case 5: { |
266 | 80.1k | switch (data_type) { |
267 | 21.6k | case IXHEAACE_MPS_SAC_DATA_TYPE_CLD: |
268 | 21.6k | *pstr_huff_entry = pstr_huff_cld_tab_2d->lav5.entry[in_data[band][0]][in_data[band][1]]; |
269 | 21.6k | *pstr_huff_escape = pstr_huff_cld_tab_2d->lav5.escape; |
270 | 21.6k | break; |
271 | 58.5k | case IXHEAACE_MPS_SAC_DATA_TYPE_ICC: |
272 | 58.5k | *pstr_huff_entry = pstr_huff_icc_tab_2d->lav5.entry[in_data[band][0]][in_data[band][1]]; |
273 | 58.5k | *pstr_huff_escape = pstr_huff_icc_tab_2d->lav5.escape; |
274 | 58.5k | break; |
275 | 80.1k | } |
276 | 80.1k | } break; |
277 | 389k | case 3: { |
278 | 389k | switch (data_type) { |
279 | 307k | case IXHEAACE_MPS_SAC_DATA_TYPE_CLD: |
280 | 307k | *pstr_huff_entry = pstr_huff_cld_tab_2d->lav3.entry[in_data[band][0]][in_data[band][1]]; |
281 | 307k | *pstr_huff_escape = pstr_huff_cld_tab_2d->lav3.escape; |
282 | 307k | break; |
283 | 82.1k | case IXHEAACE_MPS_SAC_DATA_TYPE_ICC: |
284 | 82.1k | *pstr_huff_entry = pstr_huff_icc_tab_2d->lav3.entry[in_data[band][0]][in_data[band][1]]; |
285 | 82.1k | *pstr_huff_escape = pstr_huff_icc_tab_2d->lav3.escape; |
286 | 82.1k | break; |
287 | 389k | } |
288 | 389k | } break; |
289 | 389k | case 1: { |
290 | 277k | *pstr_huff_entry = pstr_huff_icc_tab_2d->lav1.entry[in_data[band][0]][in_data[band][1]]; |
291 | 277k | *pstr_huff_escape = pstr_huff_icc_tab_2d->lav1.escape; |
292 | 277k | } break; |
293 | 789k | } |
294 | 789k | } |
295 | | |
296 | | static IA_ERRORCODE ixheaace_mps_212_huff_enc_2_d(ixheaace_bit_buf_handle pstr_bit_buf, |
297 | | const WORD32 data_type, WORD16 tab_idx_2_d[2], |
298 | | WORD16 lav_idx, WORD16 in_data[][2], |
299 | | WORD16 num_bands, WORD16 stride, |
300 | 68.9k | WORD16 *p_0_data[2], UWORD16 *huff_bits) { |
301 | 68.9k | WORD16 band = 0, lav = 0, sym_bits = 0, esc_idx = 0; |
302 | 68.9k | UWORD8 num_sbits = 0; |
303 | 68.9k | WORD16 esc_data[2][IXHEAACE_MPS_SAC_MAX_FREQ_BANDS] = {{0}}; |
304 | | |
305 | 68.9k | *huff_bits = 0; |
306 | 68.9k | const ixheaace_mps_huff_entry *pstr_huff_entry = NULL; |
307 | 68.9k | if (data_type == IXHEAACE_MPS_SAC_DATA_TYPE_ICC) { |
308 | 38.9k | lav = 2 * lav_idx + 1; |
309 | 38.9k | pstr_huff_entry = ixheaace_mps_212_huff_part_0_tab.icc; |
310 | 38.9k | } else if (data_type == IXHEAACE_MPS_SAC_DATA_TYPE_CLD) { |
311 | 29.9k | lav = 2 * lav_idx + 3; |
312 | 29.9k | pstr_huff_entry = ixheaace_mps_212_huff_part_0_tab.cld; |
313 | 29.9k | } else { |
314 | 0 | return IA_EXHEAACE_EXE_FATAL_MPS_INVALID_HUFF_DATA_TYPE; |
315 | 0 | } |
316 | | |
317 | 68.9k | if (p_0_data[0] != NULL) { |
318 | 49.7k | ixheaace_mps_huff_entry entry = pstr_huff_entry[*p_0_data[0]]; |
319 | 49.7k | *huff_bits += ixheaace_write_bits(pstr_bit_buf, entry.value, entry.length); |
320 | 49.7k | } |
321 | 68.9k | if (p_0_data[1] != NULL) { |
322 | 0 | ixheaace_mps_huff_entry entry = pstr_huff_entry[*p_0_data[1]]; |
323 | 0 | *huff_bits += ixheaace_write_bits(pstr_bit_buf, entry.value, entry.length); |
324 | 0 | } |
325 | | |
326 | 858k | for (band = 0; band < num_bands; band += stride) { |
327 | 789k | ixheaace_mps_huff_entry entry = {0}; |
328 | 789k | ixheaace_mps_huff_entry escape = {0}; |
329 | | |
330 | 789k | esc_data[0][esc_idx] = in_data[band][0] + lav; |
331 | 789k | esc_data[1][esc_idx] = in_data[band][1] + lav; |
332 | | |
333 | 789k | num_sbits = ixheaace_mps_212_sym_check(in_data[band], lav, &sym_bits); |
334 | | |
335 | 789k | ixheaace_mps_212_get_huff_entry(lav, data_type, band, tab_idx_2_d, in_data, &entry, &escape); |
336 | | |
337 | 789k | *huff_bits += ixheaace_write_bits(pstr_bit_buf, entry.value, entry.length); |
338 | | |
339 | 789k | if ((entry.value == escape.value) && (entry.length == escape.length)) { |
340 | 736 | esc_idx++; |
341 | 789k | } else { |
342 | 789k | *huff_bits += ixheaace_write_bits(pstr_bit_buf, sym_bits, num_sbits); |
343 | 789k | } |
344 | 789k | } |
345 | 68.9k | if (esc_idx > 0) { |
346 | 581 | *huff_bits += ixheaace_mps_212_calc_pcm_bits(2 * esc_idx, (2 * lav + 1)); |
347 | 581 | ixheaace_mps_212_apply_pcm_coding(pstr_bit_buf, esc_data[0], esc_data[1], 0, 2 * esc_idx, |
348 | 581 | (2 * lav + 1)); |
349 | 581 | } |
350 | | |
351 | 68.9k | return IA_NO_ERROR; |
352 | 68.9k | } |
353 | | |
354 | 50.2k | static WORD8 ixheaace_mps_212_get_next_lav_step(const WORD32 lav, const WORD32 data_type) { |
355 | 50.2k | WORD8 lav_step = 0; |
356 | 50.2k | if (data_type == IXHEAACE_MPS_SAC_DATA_TYPE_ICC) { |
357 | 25.4k | lav_step = (lav > 7) ? -1 : lav_step_icc[lav]; |
358 | 25.4k | } else if (data_type == IXHEAACE_MPS_SAC_DATA_TYPE_CLD) { |
359 | 24.8k | lav_step = (lav > 9) ? -1 : lav_step_cld[lav]; |
360 | 24.8k | } else { |
361 | 0 | lav_step = 0; |
362 | 0 | } |
363 | | |
364 | 50.2k | return lav_step; |
365 | 50.2k | } |
366 | | |
367 | | static IA_ERRORCODE ixheaace_mps_212_calc_huff_bits( |
368 | | WORD16 *in_data_1, WORD16 *in_data_2, const WORD32 data_type, const WORD32 diff_type_1, |
369 | | const WORD32 diff_type_2, const WORD16 num_bands, WORD16 *const lav_idx, WORD16 *bit_count, |
370 | 50.2k | WORD16 *const huff_dim, WORD16 *const huff_pair_type) { |
371 | 50.2k | IA_ERRORCODE error; |
372 | 50.2k | WORD16 band = 0; |
373 | 50.2k | WORD16 bit_count_1_d = 0; |
374 | 50.2k | WORD16 bit_count_2_d_freq = 0; |
375 | 50.2k | WORD16 bit_count_min = 0; |
376 | 50.2k | WORD16 num_band_short_data_1 = 0; |
377 | 50.2k | WORD16 num_band_short_data_2 = 0; |
378 | 50.2k | UWORD16 huff_bits; |
379 | 50.2k | WORD16 tab_idx_2_d[2][2] = {{0}}; |
380 | 50.2k | WORD16 tab_idx_1_d[2] = {0}; |
381 | 50.2k | WORD16 df_rest_flag[2] = {0}; |
382 | 50.2k | WORD16 part_0_flag[2] = {0}; |
383 | 50.2k | WORD16 lav_fp[2] = {0}; |
384 | 50.2k | WORD16 pair_vec[IXHEAACE_MPS_SAC_MAX_FREQ_BANDS][2] = {{0}}; |
385 | 50.2k | WORD16 *part_0_data_1[2] = {NULL}; |
386 | 50.2k | WORD16 *part_0_data_2[2] = {NULL}; |
387 | 50.2k | WORD16 *in_short_data_1 = NULL; |
388 | 50.2k | WORD16 *in_short_data_2 = NULL; |
389 | | |
390 | 50.2k | bit_count_1_d = 1; |
391 | 50.2k | bit_count_2_d_freq = 1; |
392 | 50.2k | num_band_short_data_1 = num_bands; |
393 | 50.2k | num_band_short_data_2 = num_bands; |
394 | | |
395 | 50.2k | tab_idx_1_d[0] = (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_FREQ) ? 0 : 1; |
396 | 50.2k | tab_idx_1_d[1] = (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_FREQ) ? 0 : 1; |
397 | | |
398 | 50.2k | part_0_flag[0] = (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_FREQ); |
399 | 50.2k | part_0_flag[1] = (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_FREQ); |
400 | | |
401 | 50.2k | if (in_data_1 != NULL) { |
402 | 50.2k | in_short_data_1 = in_data_1 + (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_TIME ? 2 : 0); |
403 | 50.2k | error = ixheaace_mps_212_huff_enc_1_d(NULL, data_type, tab_idx_1_d[0], in_short_data_1, |
404 | 50.2k | num_band_short_data_1, part_0_flag[0], &huff_bits); |
405 | 50.2k | if (error) { |
406 | 0 | return error; |
407 | 0 | } |
408 | 50.2k | bit_count_1_d += huff_bits; |
409 | 50.2k | } |
410 | 50.2k | if (in_data_2 != NULL) { |
411 | 0 | in_short_data_2 = in_data_2 + (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_TIME ? 2 : 0); |
412 | 0 | error = ixheaace_mps_212_huff_enc_1_d(NULL, data_type, tab_idx_1_d[1], in_short_data_2, |
413 | 0 | num_band_short_data_2, part_0_flag[1], &huff_bits); |
414 | 0 | if (error) { |
415 | 0 | return error; |
416 | 0 | } |
417 | 0 | bit_count_1_d += huff_bits; |
418 | 0 | } |
419 | | |
420 | 50.2k | bit_count_min = bit_count_1_d; |
421 | 50.2k | *huff_dim = IXHEAACE_MPS_SAC_HUFF_1D; |
422 | 50.2k | lav_idx[0] = lav_idx[1] = -1; |
423 | | |
424 | 50.2k | if (in_data_1 != NULL) { |
425 | 50.2k | if (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_FREQ) { |
426 | 35.4k | part_0_data_1[0] = &in_data_1[0]; |
427 | 35.4k | part_0_data_1[1] = NULL; |
428 | | |
429 | 35.4k | num_band_short_data_1 -= 1; |
430 | 35.4k | in_short_data_1 += 1; |
431 | 35.4k | } |
432 | | |
433 | 50.2k | df_rest_flag[0] = num_band_short_data_1 % 2; |
434 | | |
435 | 50.2k | if (df_rest_flag[0]) { |
436 | 32.2k | num_band_short_data_1 -= 1; |
437 | 32.2k | } |
438 | | |
439 | 622k | for (band = 0; band < num_band_short_data_1 - 1; band += 2) { |
440 | 572k | pair_vec[band][0] = in_short_data_1[band]; |
441 | 572k | pair_vec[band][1] = in_short_data_1[band + 1]; |
442 | | |
443 | 572k | lav_fp[0] = (WORD16)MAX(lav_fp[0], abs(pair_vec[band][0])); |
444 | 572k | lav_fp[0] = (WORD16)MAX(lav_fp[0], abs(pair_vec[band][1])); |
445 | 572k | } |
446 | | |
447 | 50.2k | tab_idx_2_d[0][0] = (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_TIME) ? 1 : 0; |
448 | 50.2k | tab_idx_2_d[0][1] = 0; |
449 | | |
450 | 50.2k | tab_idx_1_d[0] = (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_FREQ) ? 0 : 1; |
451 | | |
452 | 50.2k | lav_fp[0] = ixheaace_mps_212_get_next_lav_step(lav_fp[0], data_type); |
453 | | |
454 | 50.2k | if (lav_fp[0] != -1) { |
455 | 49.5k | bit_count_2_d_freq += lav_huff_len[lav_fp[0]]; |
456 | 49.5k | } |
457 | 50.2k | } |
458 | | |
459 | 50.2k | if (in_data_2 != NULL) { |
460 | 0 | if (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_FREQ) { |
461 | 0 | part_0_data_2[0] = NULL; |
462 | 0 | part_0_data_2[1] = &in_data_2[0]; |
463 | |
|
464 | 0 | num_band_short_data_2 -= 1; |
465 | 0 | in_short_data_2 += 1; |
466 | 0 | } |
467 | |
|
468 | 0 | df_rest_flag[1] = num_band_short_data_2 % 2; |
469 | |
|
470 | 0 | if (df_rest_flag[1]) { |
471 | 0 | num_band_short_data_2 -= 1; |
472 | 0 | } |
473 | |
|
474 | 0 | for (band = 0; band < num_band_short_data_2 - 1; band += 2) { |
475 | 0 | pair_vec[band + 1][0] = in_short_data_2[band]; |
476 | 0 | pair_vec[band + 1][1] = in_short_data_2[band + 1]; |
477 | |
|
478 | 0 | lav_fp[1] = (WORD16)MAX(lav_fp[1], abs(pair_vec[band + 1][0])); |
479 | 0 | lav_fp[1] = (WORD16)MAX(lav_fp[1], abs(pair_vec[band + 1][1])); |
480 | 0 | } |
481 | |
|
482 | 0 | tab_idx_2_d[1][0] = (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_TIME) ? 1 : 0; |
483 | 0 | tab_idx_2_d[1][1] = 0; |
484 | |
|
485 | 0 | tab_idx_1_d[1] = (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_FREQ) ? 0 : 1; |
486 | |
|
487 | 0 | lav_fp[1] = ixheaace_mps_212_get_next_lav_step(lav_fp[1], data_type); |
488 | |
|
489 | 0 | if (lav_fp[1] != -1) bit_count_2_d_freq += lav_huff_len[lav_fp[1]]; |
490 | 0 | } |
491 | | |
492 | 50.2k | if ((lav_fp[0] != -1) && (lav_fp[1] != -1)) { |
493 | 49.5k | if (in_data_1 != NULL) { |
494 | 49.5k | error = ixheaace_mps_212_huff_enc_2_d(NULL, data_type, tab_idx_2_d[0], lav_fp[0], pair_vec, |
495 | 49.5k | num_band_short_data_1, 2, part_0_data_1, &huff_bits); |
496 | 49.5k | if (error) { |
497 | 0 | return error; |
498 | 0 | } |
499 | | |
500 | 49.5k | bit_count_2_d_freq += huff_bits; |
501 | 49.5k | } |
502 | 49.5k | if (in_data_2 != NULL) { |
503 | 0 | error = |
504 | 0 | ixheaace_mps_212_huff_enc_2_d(NULL, data_type, tab_idx_2_d[1], lav_fp[1], pair_vec + 1, |
505 | 0 | num_band_short_data_2, 2, part_0_data_2, &huff_bits); |
506 | 0 | if (error) { |
507 | 0 | return error; |
508 | 0 | } |
509 | 0 | bit_count_2_d_freq += huff_bits; |
510 | 0 | } |
511 | 49.5k | if (in_data_1 != NULL) { |
512 | 49.5k | if (df_rest_flag[0]) { |
513 | 31.9k | error = ixheaace_mps_212_huff_enc_1_d(NULL, data_type, tab_idx_1_d[0], |
514 | 31.9k | in_short_data_1 + num_band_short_data_1, 1, 0, |
515 | 31.9k | &huff_bits); |
516 | 31.9k | if (error) { |
517 | 0 | return error; |
518 | 0 | } |
519 | 31.9k | bit_count_2_d_freq += huff_bits; |
520 | 31.9k | } |
521 | 49.5k | } |
522 | 49.5k | if (in_data_2 != NULL) { |
523 | 0 | if (df_rest_flag[1]) { |
524 | 0 | error = ixheaace_mps_212_huff_enc_1_d(NULL, data_type, tab_idx_1_d[1], |
525 | 0 | in_short_data_2 + num_band_short_data_2, 1, 0, |
526 | 0 | &huff_bits); |
527 | 0 | if (error) { |
528 | 0 | return error; |
529 | 0 | } |
530 | 0 | bit_count_2_d_freq += huff_bits; |
531 | 0 | } |
532 | 0 | } |
533 | | |
534 | 49.5k | if (bit_count_2_d_freq < bit_count_min) { |
535 | 25.8k | bit_count_min = bit_count_2_d_freq; |
536 | 25.8k | *huff_dim = IXHEAACE_MPS_SAC_HUFF_2D; |
537 | 25.8k | *huff_pair_type = IXHEAACE_MPS_SAC_FREQ_PAIR; |
538 | 25.8k | lav_idx[0] = lav_fp[0]; |
539 | 25.8k | lav_idx[1] = lav_fp[1]; |
540 | 25.8k | } |
541 | 49.5k | } |
542 | | |
543 | 50.2k | *bit_count = bit_count_min; |
544 | 50.2k | return IA_NO_ERROR; |
545 | 50.2k | } |
546 | | |
547 | | static IA_ERRORCODE ixheaace_mps_212_apply_huff_coding( |
548 | | ixheaace_bit_buf_handle pstr_bit_buf, WORD16 *const in_data_1, WORD16 *const in_data_2, |
549 | | const WORD32 data_type, const WORD32 diff_type_1, const WORD32 diff_type_2, |
550 | 33.6k | const WORD16 bands, const WORD16 *const lav_idx, WORD16 huff_dim, WORD16 huff_pair_type) { |
551 | 33.6k | IA_ERRORCODE error; |
552 | 33.6k | WORD16 band = 0; |
553 | 33.6k | WORD16 num_band_short_data_1 = bands; |
554 | 33.6k | WORD16 num_band_short_data_2 = bands; |
555 | 33.6k | UWORD16 huff_bits; |
556 | | |
557 | 33.6k | WORD16 df_rest_flag[2] = {0}; |
558 | 33.6k | WORD16 part_0_flag[2] = {0}; |
559 | 33.6k | WORD16 tab_idx_1_d[2] = {0}; |
560 | 33.6k | WORD16 tab_idx_2_d[2][2] = {{0}}; |
561 | 33.6k | WORD16 pair_vec[IXHEAACE_MPS_SAC_MAX_FREQ_BANDS][2] = {{0}}; |
562 | | |
563 | 33.6k | WORD16 *part_0_data_1[2] = {NULL}; |
564 | 33.6k | WORD16 *part_0_data_2[2] = {NULL}; |
565 | 33.6k | WORD16 *in_short_data_1 = NULL; |
566 | 33.6k | WORD16 *in_short_data_2 = NULL; |
567 | | |
568 | 33.6k | ixheaace_write_bits(pstr_bit_buf, huff_dim, 1); |
569 | 33.6k | if (huff_dim == IXHEAACE_MPS_SAC_HUFF_1D) { |
570 | 14.2k | part_0_flag[0] = (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_FREQ); |
571 | 14.2k | part_0_flag[1] = (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_FREQ); |
572 | | |
573 | 14.2k | tab_idx_1_d[0] = (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_FREQ) ? 0 : 1; |
574 | 14.2k | tab_idx_1_d[1] = (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_FREQ) ? 0 : 1; |
575 | | |
576 | 14.2k | if (in_data_1 != NULL) { |
577 | 14.2k | in_short_data_1 = in_data_1 + (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_TIME ? 2 : 0); |
578 | 14.2k | error = |
579 | 14.2k | ixheaace_mps_212_huff_enc_1_d(pstr_bit_buf, data_type, tab_idx_1_d[0], in_short_data_1, |
580 | 14.2k | num_band_short_data_1, part_0_flag[0], &huff_bits); |
581 | 14.2k | if (error) { |
582 | 0 | return error; |
583 | 0 | } |
584 | 14.2k | } |
585 | 14.2k | if (in_data_2 != NULL) { |
586 | 0 | in_short_data_2 = in_data_2 + (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_TIME ? 2 : 0); |
587 | 0 | error = |
588 | 0 | ixheaace_mps_212_huff_enc_1_d(pstr_bit_buf, data_type, tab_idx_1_d[1], in_short_data_2, |
589 | 0 | num_band_short_data_2, part_0_flag[1], &huff_bits); |
590 | 0 | if (error) { |
591 | 0 | return error; |
592 | 0 | } |
593 | 0 | } |
594 | 19.3k | } else { |
595 | 19.3k | if (huff_pair_type == IXHEAACE_MPS_SAC_FREQ_PAIR) { |
596 | 19.3k | if (in_data_1 != NULL) { |
597 | 19.3k | in_short_data_1 = in_data_1 + (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_TIME ? 2 : 0); |
598 | 19.3k | if (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_FREQ) { |
599 | 14.5k | part_0_data_1[0] = &in_data_1[0]; |
600 | 14.5k | part_0_data_1[1] = NULL; |
601 | | |
602 | 14.5k | num_band_short_data_1 -= 1; |
603 | 14.5k | in_short_data_1 += 1; |
604 | 14.5k | } |
605 | | |
606 | 19.3k | df_rest_flag[0] = num_band_short_data_1 % 2; |
607 | | |
608 | 19.3k | if (df_rest_flag[0]) num_band_short_data_1 -= 1; |
609 | | |
610 | 246k | for (band = 0; band < num_band_short_data_1 - 1; band += 2) { |
611 | 226k | pair_vec[band][0] = in_short_data_1[band]; |
612 | 226k | pair_vec[band][1] = in_short_data_1[band + 1]; |
613 | 226k | } |
614 | | |
615 | 19.3k | tab_idx_2_d[0][0] = (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_TIME) ? 1 : 0; |
616 | 19.3k | tab_idx_2_d[0][1] = 0; |
617 | | |
618 | 19.3k | tab_idx_1_d[0] = (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_FREQ) ? 0 : 1; |
619 | 19.3k | } |
620 | 19.3k | if (in_data_2 != NULL) { |
621 | 0 | in_short_data_2 = in_data_2 + (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_TIME ? 2 : 0); |
622 | 0 | if (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_FREQ) { |
623 | 0 | part_0_data_2[0] = NULL; |
624 | 0 | part_0_data_2[1] = &in_data_2[0]; |
625 | |
|
626 | 0 | num_band_short_data_2 -= 1; |
627 | 0 | in_short_data_2 += 1; |
628 | 0 | } |
629 | |
|
630 | 0 | df_rest_flag[1] = num_band_short_data_2 % 2; |
631 | |
|
632 | 0 | if (df_rest_flag[1]) num_band_short_data_2 -= 1; |
633 | |
|
634 | 0 | for (band = 0; band < num_band_short_data_2 - 1; band += 2) { |
635 | 0 | pair_vec[band + 1][0] = in_short_data_2[band]; |
636 | 0 | pair_vec[band + 1][1] = in_short_data_2[band + 1]; |
637 | 0 | } |
638 | |
|
639 | 0 | tab_idx_2_d[1][0] = (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_TIME) ? 1 : 0; |
640 | 0 | tab_idx_2_d[1][1] = 0; |
641 | |
|
642 | 0 | tab_idx_1_d[1] = (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_FREQ) ? 0 : 1; |
643 | 0 | } |
644 | 19.3k | if (in_data_1 != NULL) { |
645 | 19.3k | ixheaace_write_bits(pstr_bit_buf, lav_huff_val[lav_idx[0]], lav_huff_len[lav_idx[0]]); |
646 | 19.3k | error = ixheaace_mps_212_huff_enc_2_d(pstr_bit_buf, data_type, tab_idx_2_d[0], lav_idx[0], |
647 | 19.3k | pair_vec, num_band_short_data_1, 2, part_0_data_1, |
648 | 19.3k | &huff_bits); |
649 | 19.3k | if (error) { |
650 | 0 | return error; |
651 | 0 | } |
652 | 19.3k | if (df_rest_flag[0]) { |
653 | 13.5k | error = ixheaace_mps_212_huff_enc_1_d(pstr_bit_buf, data_type, tab_idx_1_d[0], |
654 | 13.5k | in_short_data_1 + num_band_short_data_1, 1, 0, |
655 | 13.5k | &huff_bits); |
656 | 13.5k | if (error) { |
657 | 0 | return error; |
658 | 0 | } |
659 | 13.5k | } |
660 | 19.3k | } |
661 | 19.3k | if (in_data_2 != NULL) { |
662 | 0 | ixheaace_write_bits(pstr_bit_buf, lav_huff_val[lav_idx[1]], lav_huff_len[lav_idx[1]]); |
663 | 0 | error = ixheaace_mps_212_huff_enc_2_d(pstr_bit_buf, data_type, tab_idx_2_d[1], lav_idx[1], |
664 | 0 | pair_vec + 1, num_band_short_data_2, 2, |
665 | 0 | part_0_data_2, &huff_bits); |
666 | 0 | if (error) { |
667 | 0 | return error; |
668 | 0 | } |
669 | 0 | if (df_rest_flag[1]) { |
670 | 0 | error = ixheaace_mps_212_huff_enc_1_d(pstr_bit_buf, data_type, tab_idx_1_d[1], |
671 | 0 | in_short_data_2 + num_band_short_data_2, 1, 0, |
672 | 0 | &huff_bits); |
673 | 0 | if (error) { |
674 | 0 | return error; |
675 | 0 | } |
676 | 0 | } |
677 | 0 | } |
678 | 19.3k | } else { |
679 | 0 | if (in_data_1 == NULL || in_data_2 == NULL || in_short_data_1 == NULL || |
680 | 0 | in_short_data_2 == NULL) { |
681 | 0 | return IA_EXHEAACE_EXE_FATAL_MPS_NULL_DATA_HANDLE; |
682 | 0 | } |
683 | | |
684 | 0 | if ((diff_type_1 == IXHEAACE_MPS_SAC_DIFF_FREQ) || |
685 | 0 | (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_FREQ)) { |
686 | 0 | part_0_data_1[0] = &in_data_1[0]; |
687 | 0 | part_0_data_1[1] = &in_data_2[0]; |
688 | |
|
689 | 0 | in_short_data_1 += 1; |
690 | 0 | in_short_data_2 += 1; |
691 | |
|
692 | 0 | num_band_short_data_1 -= 1; |
693 | 0 | } |
694 | |
|
695 | 0 | for (band = 0; band < num_band_short_data_1; band++) { |
696 | 0 | pair_vec[band][0] = in_short_data_1[band]; |
697 | 0 | pair_vec[band][1] = in_short_data_2[band]; |
698 | 0 | } |
699 | 0 | tab_idx_2_d[0][0] = ((diff_type_1 == IXHEAACE_MPS_SAC_DIFF_TIME) || |
700 | 0 | (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_TIME)) |
701 | 0 | ? 1 |
702 | 0 | : 0; |
703 | 0 | tab_idx_2_d[0][1] = 1; |
704 | 0 | ixheaace_write_bits(pstr_bit_buf, lav_huff_val[lav_idx[0]], lav_huff_len[lav_idx[0]]); |
705 | 0 | error = ixheaace_mps_212_huff_enc_2_d(pstr_bit_buf, data_type, tab_idx_2_d[0], lav_idx[0], |
706 | 0 | pair_vec, num_band_short_data_1, 1, part_0_data_1, |
707 | 0 | &huff_bits); |
708 | 0 | if (error) { |
709 | 0 | return error; |
710 | 0 | } |
711 | 0 | } |
712 | 19.3k | } |
713 | 33.6k | return IA_NO_ERROR; |
714 | 33.6k | } |
715 | | |
716 | | static WORD32 ixheaace_mps_515_split_lsb(WORD32 *in_data, WORD32 offset, WORD32 num_lsb, |
717 | | WORD32 num_val, WORD32 *out_data_lsb, |
718 | 150k | WORD32 *out_data_msb) { |
719 | 150k | WORD32 i = 0, val = 0, lsb = 0, msb = 0; |
720 | | |
721 | 150k | UWORD32 mask = (1 << num_lsb) - 1; |
722 | 150k | WORD32 no_lsb_bits = 0; |
723 | | |
724 | 2.81M | for (i = 0; i < num_val; i++) { |
725 | 2.66M | val = in_data[i] + offset; |
726 | | |
727 | 2.66M | lsb = val & mask; |
728 | 2.66M | msb = val >> num_lsb; |
729 | | |
730 | 2.66M | if (out_data_lsb != NULL) out_data_lsb[i] = lsb; |
731 | 2.66M | if (out_data_msb != NULL) out_data_msb[i] = msb; |
732 | | |
733 | 2.66M | no_lsb_bits += num_lsb; |
734 | 2.66M | } |
735 | | |
736 | 150k | return no_lsb_bits; |
737 | 150k | } |
738 | | |
739 | | static VOID ixheaace_mps_515_apply_lsb_coding(ixheaace_bit_buf_handle pstr_bit_buf, |
740 | | WORD32 *in_data_lsb, UWORD8 num_lsb, |
741 | 0 | WORD32 num_val) { |
742 | 0 | WORD32 i = 0; |
743 | |
|
744 | 0 | for (i = 0; i < num_val; i++) { |
745 | 0 | ixheaace_write_bits(pstr_bit_buf, in_data_lsb[i], num_lsb); |
746 | 0 | } |
747 | 0 | } |
748 | | |
749 | 150k | static VOID ixheaace_mps_515_calc_diff_freq(WORD32 *in_data, WORD32 *out_data, WORD32 num_val) { |
750 | 150k | WORD32 i = 0; |
751 | | |
752 | 150k | out_data[0] = in_data[0]; |
753 | | |
754 | 2.66M | for (i = 1; i < num_val; i++) { |
755 | 2.51M | out_data[i] = in_data[i] - in_data[i - 1]; |
756 | 2.51M | } |
757 | 150k | } |
758 | | |
759 | | static VOID ixheaace_mps_515_calc_diff_time(WORD32 *in_data, WORD32 *prev_data, WORD32 *out_data, |
760 | 0 | WORD32 direction, WORD32 num_val) { |
761 | 0 | WORD32 i = 0; |
762 | |
|
763 | 0 | out_data[-1] = (direction == IXHEAACE_MPS_SAC_DIRECTION_BACKWARDS) ? in_data[0] : prev_data[0]; |
764 | |
|
765 | 0 | for (i = 0; i < num_val; i++) { |
766 | 0 | out_data[i] = in_data[i] - prev_data[i]; |
767 | 0 | } |
768 | 0 | } |
769 | | |
770 | 150k | static WORD32 ixheaace_mps_515_calc_pcm_bits(WORD32 num_val, WORD32 n_levels) { |
771 | 150k | WORD32 num_complete_chunks = 0, rest_chunk_size = 0; |
772 | 150k | WORD32 max_grp_len = 0, bits_pcm = 0; |
773 | | |
774 | 150k | FLOAT32 num_ld_levels = 0.f; |
775 | | |
776 | 150k | switch (n_levels) { |
777 | 0 | case 3: |
778 | 0 | max_grp_len = 5; |
779 | 0 | break; |
780 | 0 | case 7: |
781 | 0 | max_grp_len = 6; |
782 | 0 | break; |
783 | 0 | case 11: |
784 | 0 | max_grp_len = 2; |
785 | 0 | break; |
786 | 0 | case 13: |
787 | 0 | max_grp_len = 4; |
788 | 0 | break; |
789 | 0 | case 19: |
790 | 0 | max_grp_len = 4; |
791 | 0 | break; |
792 | 0 | case 25: |
793 | 0 | max_grp_len = 3; |
794 | 0 | break; |
795 | 0 | case 51: |
796 | 0 | max_grp_len = 4; |
797 | 0 | break; |
798 | | |
799 | 0 | case 4: |
800 | 56.2k | case 8: |
801 | 56.2k | case 15: |
802 | 56.2k | case 26: |
803 | 150k | case 31: |
804 | 150k | max_grp_len = 1; |
805 | 150k | break; |
806 | 0 | default: |
807 | 0 | return IA_EXHEAACE_EXE_FATAL_MPS_INVALID_LEVELS; |
808 | 150k | } |
809 | | |
810 | 150k | num_ld_levels = (FLOAT32)(log((FLOAT32)n_levels) / log(2.f)); |
811 | | |
812 | 150k | num_complete_chunks = num_val / max_grp_len; |
813 | 150k | rest_chunk_size = num_val % max_grp_len; |
814 | | |
815 | 150k | bits_pcm = ((WORD32)ceil((FLOAT32)(max_grp_len)*num_ld_levels)) * num_complete_chunks; |
816 | 150k | bits_pcm += (WORD32)ceil((FLOAT32)(rest_chunk_size)*num_ld_levels); |
817 | | |
818 | 150k | return bits_pcm; |
819 | 150k | } |
820 | | |
821 | | static VOID ixheaace_mps_515_apply_pcm_coding(ixheaace_bit_buf_handle pstr_bit_buf, |
822 | | WORD32 *in_data_1, WORD32 *in_data_2, WORD32 offset, |
823 | 9.72k | WORD32 num_val, WORD32 n_levels) { |
824 | 9.72k | WORD32 i = 0, j = 0, idx = 0; |
825 | 9.72k | WORD32 max_grp_len = 1, grp_len = 0, next_val = 0, grp_val = 0; |
826 | | |
827 | 9.72k | FLOAT32 num_ld_levels = 0.f; |
828 | | |
829 | 9.72k | UWORD8 pcm_block_size[7] = {0}; |
830 | 9.72k | switch (n_levels) { |
831 | 0 | case 4: |
832 | 0 | num_ld_levels = 2.00000000f; |
833 | 0 | break; |
834 | 2.33k | case 8: |
835 | 2.33k | num_ld_levels = 3.00000000f; |
836 | 2.33k | break; |
837 | 0 | case 15: |
838 | 0 | num_ld_levels = 3.90689060f; |
839 | 0 | break; |
840 | 0 | case 26: |
841 | 0 | num_ld_levels = 4.70043972f; |
842 | 0 | break; |
843 | 7.39k | case 31: |
844 | 7.39k | num_ld_levels = 4.95419645f; |
845 | 7.39k | break; |
846 | 0 | case 51: |
847 | 0 | max_grp_len = 4; |
848 | 0 | num_ld_levels = 5.67242534f; |
849 | 0 | break; |
850 | 9.72k | } |
851 | | |
852 | 19.4k | for (i = 1; i <= max_grp_len; i++) { |
853 | 9.72k | pcm_block_size[i] = (UWORD8)ceil((FLOAT32)(i)*num_ld_levels); |
854 | 9.72k | } |
855 | | |
856 | 73.3k | for (i = 0; i < num_val; i += max_grp_len) { |
857 | 63.6k | grp_len = MIN(max_grp_len, num_val - i); |
858 | 63.6k | grp_val = 0; |
859 | 127k | for (j = 0; j < grp_len; j++) { |
860 | 63.6k | idx = i + j; |
861 | 63.6k | next_val = (in_data_2 == NULL) |
862 | 63.6k | ? in_data_1[idx] |
863 | 63.6k | : (in_data_1 == NULL) |
864 | 0 | ? in_data_2[idx] |
865 | 0 | : ((idx & 01) ? in_data_2[idx >> 1] : in_data_1[idx >> 1]); |
866 | 63.6k | grp_val = grp_val * n_levels + next_val + offset; |
867 | 63.6k | } |
868 | | |
869 | 63.6k | ixheaace_write_bits(pstr_bit_buf, grp_val, pcm_block_size[grp_len]); |
870 | 63.6k | } |
871 | 9.72k | } |
872 | | |
873 | | static WORD32 ixheaace_mps_515_count_huff_cld( |
874 | | const ixheaace_mps_sac_huff_cld_tab *pstr_huff_cld_tab_pt0, |
875 | | const ixheaace_mps_sac_huff_cld_tab *pstr_huff_cld_tab_diff, WORD32 *in_data, WORD32 num_val, |
876 | 187k | WORD32 p0_flag) { |
877 | 187k | WORD32 i = 0, id = 0; |
878 | 187k | WORD32 huff_bits = 0; |
879 | 187k | WORD32 offset = 0; |
880 | 187k | if (p0_flag) { |
881 | 187k | huff_bits += pstr_huff_cld_tab_pt0->length[in_data[0]]; |
882 | 187k | offset = 1; |
883 | 187k | } |
884 | | |
885 | 3.07M | for (i = offset; i < num_val; i++) { |
886 | 2.89M | id = in_data[i]; |
887 | | |
888 | 2.89M | if (id != 0) { |
889 | 269k | if (id < 0) { |
890 | 134k | id = -id; |
891 | 134k | } |
892 | 269k | huff_bits += 1; |
893 | 269k | } |
894 | | |
895 | 2.89M | huff_bits += pstr_huff_cld_tab_diff->length[id]; |
896 | 2.89M | } |
897 | | |
898 | 187k | return huff_bits; |
899 | 187k | } |
900 | | |
901 | | static WORD32 ixheaace_mps_515_count_huff_icc( |
902 | | const ixheaace_mps_sac_huff_icc_tab *pstr_huff_icc_tab_pt0, |
903 | | const ixheaace_mps_sac_huff_icc_tab *pstr_huff_icc_tab_diff, WORD32 *in_data, WORD32 num_val, |
904 | 112k | WORD32 p0_flag) { |
905 | 112k | WORD32 i = 0, id = 0; |
906 | 112k | WORD32 huff_bits = 0; |
907 | 112k | WORD32 offset = 0; |
908 | | |
909 | 112k | if (p0_flag) { |
910 | 112k | huff_bits += pstr_huff_icc_tab_pt0->length[in_data[0]]; |
911 | 112k | offset = 1; |
912 | 112k | } |
913 | | |
914 | 2.25M | for (i = offset; i < num_val; i++) { |
915 | 2.13M | id = in_data[i]; |
916 | | |
917 | 2.13M | if (id != 0) { |
918 | 150k | if (id < 0) { |
919 | 59.7k | id = -id; |
920 | 59.7k | } |
921 | 150k | huff_bits += 1; |
922 | 150k | } |
923 | | |
924 | 2.13M | huff_bits += pstr_huff_icc_tab_diff->length[id]; |
925 | 2.13M | } |
926 | | |
927 | 112k | return huff_bits; |
928 | 112k | } |
929 | | |
930 | | static WORD32 ixheaace_mps_515_count_huff_cpc( |
931 | | const ixheaace_mps_sac_huff_cpc_tab *pstr_huff_cpc_tab_pt0, |
932 | | const ixheaace_mps_sac_huff_cpc_tab *pstr_huff_cpc_tab_diff, WORD32 *in_data, WORD32 num_val, |
933 | 0 | WORD32 p0_flag) { |
934 | 0 | WORD32 i = 0, id = 0; |
935 | 0 | WORD32 huff_bits = 0; |
936 | 0 | WORD32 offset = 0; |
937 | |
|
938 | 0 | if (p0_flag) { |
939 | 0 | huff_bits += pstr_huff_cpc_tab_pt0->length[in_data[0]]; |
940 | 0 | offset = 1; |
941 | 0 | } |
942 | |
|
943 | 0 | for (i = offset; i < num_val; i++) { |
944 | 0 | id = in_data[i]; |
945 | |
|
946 | 0 | if (id != 0) { |
947 | 0 | if (id < 0) { |
948 | 0 | id = -id; |
949 | 0 | } |
950 | 0 | huff_bits += 1; |
951 | 0 | } |
952 | |
|
953 | 0 | huff_bits += pstr_huff_cpc_tab_diff->length[id]; |
954 | 0 | } |
955 | |
|
956 | 0 | return huff_bits; |
957 | 0 | } |
958 | | |
959 | | static VOID ixheaace_mps_515_huff_enc_cld( |
960 | | ixheaace_bit_buf_handle pstr_bit_buf, |
961 | | const ixheaace_mps_sac_huff_cld_tab *pstr_huff_cld_tab_pt0, |
962 | | const ixheaace_mps_sac_huff_cld_tab *pstr_huff_cld_tab_diff, WORD32 *in_data, WORD32 num_val, |
963 | 86.4k | WORD32 p0_flag) { |
964 | 86.4k | WORD32 i = 0, id = 0, id_sign = 0; |
965 | 86.4k | WORD32 offset = 0; |
966 | | |
967 | 86.4k | if (p0_flag) { |
968 | 86.4k | ixheaace_write_bits(pstr_bit_buf, pstr_huff_cld_tab_pt0->value[in_data[0]], |
969 | 86.4k | pstr_huff_cld_tab_pt0->length[in_data[0]]); |
970 | 86.4k | offset = 1; |
971 | 86.4k | } |
972 | | |
973 | 1.52M | for (i = offset; i < num_val; i++) { |
974 | 1.43M | id = in_data[i]; |
975 | | |
976 | 1.43M | if (id != 0) { |
977 | 132k | id_sign = 0; |
978 | 132k | if (id < 0) { |
979 | 66.2k | id = -id; |
980 | 66.2k | id_sign = 1; |
981 | 66.2k | } |
982 | 132k | } |
983 | | |
984 | 1.43M | ixheaace_write_bits(pstr_bit_buf, pstr_huff_cld_tab_diff->value[id], |
985 | 1.43M | pstr_huff_cld_tab_diff->length[id]); |
986 | | |
987 | 1.43M | if (id != 0) { |
988 | 132k | ixheaace_write_bits(pstr_bit_buf, id_sign, 1); |
989 | 132k | } |
990 | 1.43M | } |
991 | 86.4k | } |
992 | | |
993 | | static VOID ixheaace_mps_515_huff_enc_icc( |
994 | | ixheaace_bit_buf_handle pstr_bit_buf, |
995 | | const ixheaace_mps_sac_huff_icc_tab *pstr_huff_icc_tab_pt0, |
996 | | const ixheaace_mps_sac_huff_icc_tab *pstr_huff_icc_tab_diff, WORD32 *in_data, WORD32 num_val, |
997 | 53.9k | WORD32 p0_flag) { |
998 | 53.9k | WORD32 i = 0, id = 0, id_sign = 0; |
999 | 53.9k | WORD32 offset = 0; |
1000 | | |
1001 | 53.9k | if (p0_flag) { |
1002 | 53.9k | ixheaace_write_bits(pstr_bit_buf, pstr_huff_icc_tab_pt0->value[in_data[0]], |
1003 | 53.9k | pstr_huff_icc_tab_pt0->length[in_data[0]]); |
1004 | 53.9k | offset = 1; |
1005 | 53.9k | } |
1006 | | |
1007 | 1.07M | for (i = offset; i < num_val; i++) { |
1008 | 1.02M | id = in_data[i]; |
1009 | | |
1010 | 1.02M | if (id != 0) { |
1011 | 45.1k | id_sign = 0; |
1012 | 45.1k | if (id < 0) { |
1013 | 17.1k | id = -id; |
1014 | 17.1k | id_sign = 1; |
1015 | 17.1k | } |
1016 | 45.1k | } |
1017 | | |
1018 | 1.02M | ixheaace_write_bits(pstr_bit_buf, pstr_huff_icc_tab_diff->value[id], |
1019 | 1.02M | pstr_huff_icc_tab_diff->length[id]); |
1020 | | |
1021 | 1.02M | if (id != 0) { |
1022 | 45.1k | ixheaace_write_bits(pstr_bit_buf, id_sign, 1); |
1023 | 45.1k | } |
1024 | 1.02M | } |
1025 | 53.9k | } |
1026 | | |
1027 | | static VOID ixheaace_mps_515_huff_enc_cpc( |
1028 | | ixheaace_bit_buf_handle pstr_bit_buf, |
1029 | | const ixheaace_mps_sac_huff_cpc_tab *pstr_huff_cpc_tab_pt0, |
1030 | | const ixheaace_mps_sac_huff_cpc_tab *pstr_huff_cpc_tab_diff, WORD32 *in_data, WORD32 num_val, |
1031 | 0 | WORD32 p0_flag) { |
1032 | 0 | WORD32 i = 0, id = 0, id_sign = 0; |
1033 | 0 | WORD32 offset = 0; |
1034 | |
|
1035 | 0 | if (p0_flag) { |
1036 | 0 | ixheaace_write_bits(pstr_bit_buf, pstr_huff_cpc_tab_pt0->value[in_data[0]], |
1037 | 0 | pstr_huff_cpc_tab_pt0->length[in_data[0]]); |
1038 | 0 | offset = 1; |
1039 | 0 | } |
1040 | |
|
1041 | 0 | for (i = offset; i < num_val; i++) { |
1042 | 0 | id = in_data[i]; |
1043 | |
|
1044 | 0 | if (id != 0) { |
1045 | 0 | id_sign = 0; |
1046 | 0 | if (id < 0) { |
1047 | 0 | id = -id; |
1048 | 0 | id_sign = 1; |
1049 | 0 | } |
1050 | 0 | } |
1051 | |
|
1052 | 0 | ixheaace_write_bits(pstr_bit_buf, pstr_huff_cpc_tab_diff->value[id], |
1053 | 0 | pstr_huff_cpc_tab_diff->length[id]); |
1054 | |
|
1055 | 0 | if (id != 0) { |
1056 | 0 | ixheaace_write_bits(pstr_bit_buf, id_sign, 1); |
1057 | 0 | } |
1058 | 0 | } |
1059 | 0 | } |
1060 | | |
1061 | | static WORD32 ixheaace_mps_515_calc_huff_bits(WORD32 *in_data_1, WORD32 *in_data_2, |
1062 | | WORD32 data_type, WORD32 diff_type_1, |
1063 | 300k | WORD32 diff_type_2, WORD32 num_val) { |
1064 | 300k | WORD32 p0_flag[2]; |
1065 | | |
1066 | 300k | WORD32 offset_1 = (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_TIME) ? 1 : 0; |
1067 | 300k | WORD32 offset_2 = (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_TIME) ? 1 : 0; |
1068 | | |
1069 | 300k | WORD32 bit_count_huff = 0; |
1070 | | |
1071 | 300k | WORD32 num_val_1_int = 0; |
1072 | 300k | WORD32 num_val_2_int = 0; |
1073 | | |
1074 | 300k | WORD32 *in_data_1_int = in_data_1 + offset_1; |
1075 | 300k | WORD32 *in_data_2_int = in_data_2 + offset_2; |
1076 | | |
1077 | 300k | bit_count_huff = 1; |
1078 | | |
1079 | 300k | num_val_1_int = num_val; |
1080 | 300k | num_val_2_int = num_val; |
1081 | | |
1082 | 300k | p0_flag[0] = (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_FREQ); |
1083 | 300k | p0_flag[1] = (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_FREQ); |
1084 | | |
1085 | 300k | switch (data_type) { |
1086 | 187k | case IXHEAACE_MPS_SAC_DATA_TYPE_CLD: |
1087 | 187k | if (in_data_1 != NULL) |
1088 | 187k | bit_count_huff += |
1089 | 187k | ixheaace_mps_515_count_huff_cld(&ixheaace_mps_515_huff_cld_tab.huff_pt0, |
1090 | 187k | &ixheaace_mps_515_huff_cld_tab.huff_diff[diff_type_1], |
1091 | 187k | in_data_1_int, num_val_1_int, p0_flag[0]); |
1092 | 187k | if (in_data_2 != NULL) |
1093 | 0 | bit_count_huff += |
1094 | 0 | ixheaace_mps_515_count_huff_cld(&ixheaace_mps_515_huff_cld_tab.huff_pt0, |
1095 | 0 | &ixheaace_mps_515_huff_cld_tab.huff_diff[diff_type_2], |
1096 | 0 | in_data_2_int, num_val_2_int, p0_flag[1]); |
1097 | 187k | break; |
1098 | | |
1099 | 112k | case IXHEAACE_MPS_SAC_DATA_TYPE_ICC: |
1100 | 112k | if (in_data_1 != NULL) |
1101 | 112k | bit_count_huff += |
1102 | 112k | ixheaace_mps_515_count_huff_icc(&ixheaace_mps_515_huff_icc_tab.huff_pt0, |
1103 | 112k | &ixheaace_mps_515_huff_icc_tab.huff_diff[diff_type_1], |
1104 | 112k | in_data_1_int, num_val_1_int, p0_flag[0]); |
1105 | 112k | if (in_data_2 != NULL) |
1106 | 0 | bit_count_huff += |
1107 | 0 | ixheaace_mps_515_count_huff_icc(&ixheaace_mps_515_huff_icc_tab.huff_pt0, |
1108 | 0 | &ixheaace_mps_515_huff_icc_tab.huff_diff[diff_type_2], |
1109 | 0 | in_data_2_int, num_val_2_int, p0_flag[1]); |
1110 | 112k | break; |
1111 | | |
1112 | 0 | case IXHEAACE_MPS_SAC_DATA_TYPE_CPC: |
1113 | 0 | if (in_data_1 != NULL) |
1114 | 0 | bit_count_huff += |
1115 | 0 | ixheaace_mps_515_count_huff_cpc(&ixheaace_mps_515_huff_cpc_tab.huff_pt0, |
1116 | 0 | &ixheaace_mps_515_huff_cpc_tab.huff_diff[diff_type_1], |
1117 | 0 | in_data_1_int, num_val_1_int, p0_flag[0]); |
1118 | 0 | if (in_data_2 != NULL) |
1119 | 0 | bit_count_huff += |
1120 | 0 | ixheaace_mps_515_count_huff_cpc(&ixheaace_mps_515_huff_cpc_tab.huff_pt0, |
1121 | 0 | &ixheaace_mps_515_huff_cpc_tab.huff_diff[diff_type_2], |
1122 | 0 | in_data_2_int, num_val_2_int, p0_flag[1]); |
1123 | 0 | break; |
1124 | | |
1125 | 0 | default: |
1126 | 0 | break; |
1127 | 300k | } |
1128 | | |
1129 | 300k | return bit_count_huff; |
1130 | 300k | } |
1131 | | |
1132 | | static VOID ixheaace_mps_515_apply_huff_coding(ixheaace_bit_buf_handle pstr_bit_buf, |
1133 | | WORD32 *in_data_1, WORD32 *in_data_2, |
1134 | | WORD32 data_type, WORD32 diff_type_1, |
1135 | 140k | WORD32 diff_type_2, WORD32 num_val) { |
1136 | 140k | WORD32 p0_flag[2]; |
1137 | | |
1138 | 140k | WORD32 num_val_1_int = num_val; |
1139 | 140k | WORD32 num_val_2_int = num_val; |
1140 | | |
1141 | 140k | WORD32 *in_data_1_int = in_data_1; |
1142 | 140k | WORD32 *in_data_2_int = in_data_2; |
1143 | | |
1144 | 140k | if (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_TIME) in_data_1_int += 1; |
1145 | 140k | if (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_TIME) in_data_2_int += 1; |
1146 | | |
1147 | 140k | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_HUFF_1D, 1); |
1148 | | |
1149 | 140k | p0_flag[0] = (diff_type_1 == IXHEAACE_MPS_SAC_DIFF_FREQ); |
1150 | 140k | p0_flag[1] = (diff_type_2 == IXHEAACE_MPS_SAC_DIFF_FREQ); |
1151 | | |
1152 | 140k | switch (data_type) { |
1153 | 86.4k | case IXHEAACE_MPS_SAC_DATA_TYPE_CLD: |
1154 | 86.4k | if (in_data_1 != NULL) |
1155 | 86.4k | ixheaace_mps_515_huff_enc_cld(pstr_bit_buf, &ixheaace_mps_515_huff_cld_tab.huff_pt0, |
1156 | 86.4k | &ixheaace_mps_515_huff_cld_tab.huff_diff[diff_type_1], |
1157 | 86.4k | in_data_1_int, num_val_1_int, p0_flag[0]); |
1158 | 86.4k | if (in_data_2 != NULL) |
1159 | 0 | ixheaace_mps_515_huff_enc_cld(pstr_bit_buf, &ixheaace_mps_515_huff_cld_tab.huff_pt0, |
1160 | 0 | &ixheaace_mps_515_huff_cld_tab.huff_diff[diff_type_2], |
1161 | 0 | in_data_2_int, num_val_2_int, p0_flag[1]); |
1162 | 86.4k | break; |
1163 | | |
1164 | 53.9k | case IXHEAACE_MPS_SAC_DATA_TYPE_ICC: |
1165 | 53.9k | if (in_data_1 != NULL) |
1166 | 53.9k | ixheaace_mps_515_huff_enc_icc(pstr_bit_buf, &ixheaace_mps_515_huff_icc_tab.huff_pt0, |
1167 | 53.9k | &ixheaace_mps_515_huff_icc_tab.huff_diff[diff_type_1], |
1168 | 53.9k | in_data_1_int, num_val_1_int, p0_flag[0]); |
1169 | 53.9k | if (in_data_2 != NULL) |
1170 | 0 | ixheaace_mps_515_huff_enc_icc(pstr_bit_buf, &ixheaace_mps_515_huff_icc_tab.huff_pt0, |
1171 | 0 | &ixheaace_mps_515_huff_icc_tab.huff_diff[diff_type_2], |
1172 | 0 | in_data_2_int, num_val_2_int, p0_flag[1]); |
1173 | 53.9k | break; |
1174 | | |
1175 | 0 | case IXHEAACE_MPS_SAC_DATA_TYPE_CPC: |
1176 | 0 | if (in_data_1 != NULL) |
1177 | 0 | ixheaace_mps_515_huff_enc_cpc(pstr_bit_buf, &ixheaace_mps_515_huff_cpc_tab.huff_pt0, |
1178 | 0 | &ixheaace_mps_515_huff_cpc_tab.huff_diff[diff_type_1], |
1179 | 0 | in_data_1_int, num_val_1_int, p0_flag[0]); |
1180 | 0 | if (in_data_2 != NULL) |
1181 | 0 | ixheaace_mps_515_huff_enc_cpc(pstr_bit_buf, &ixheaace_mps_515_huff_cpc_tab.huff_pt0, |
1182 | 0 | &ixheaace_mps_515_huff_cpc_tab.huff_diff[diff_type_2], |
1183 | 0 | in_data_2_int, num_val_2_int, p0_flag[1]); |
1184 | 0 | break; |
1185 | | |
1186 | 0 | default: |
1187 | 0 | break; |
1188 | 140k | } |
1189 | 140k | } |
1190 | | |
1191 | | IA_ERRORCODE ixheaace_mps_212_ec_data_pair_enc( |
1192 | | ixheaace_bit_buf_handle pstr_bit_buf, WORD16 pp_in_data[][IXHEAACE_MPS_SAC_MAX_FREQ_BANDS], |
1193 | | WORD16 p_in_data[IXHEAACE_MPS_SAC_MAX_FREQ_BANDS], const WORD32 data_type, |
1194 | | const WORD32 set_idx, const WORD32 start_band, const WORD16 data_bands, |
1195 | 0 | const WORD32 coarse_flag, const WORD32 independency_flag) { |
1196 | 0 | IA_ERRORCODE error; |
1197 | 0 | WORD16 band = 0; |
1198 | 0 | WORD16 quant_levels = 0, quant_offset = 0, num_pcm_val = 0; |
1199 | 0 | WORD16 pcm_coding_flag = 0; |
1200 | 0 | WORD16 min_bits_all = 0; |
1201 | 0 | WORD16 min_found = 0; |
1202 | 0 | WORD16 huff_dim_df_df = 0; |
1203 | 0 | WORD16 huff_pair_type_df_df = 0; |
1204 | 0 | WORD16 huff_dim_df_dt = 0; |
1205 | 0 | WORD16 huff_pair_type_df_dt = 0; |
1206 | 0 | WORD16 huff_dim_dtbw_df = 0; |
1207 | 0 | WORD16 huff_pair_type_dtbw_df = 0; |
1208 | 0 | WORD16 huff_dim_dt_dt = 0; |
1209 | 0 | WORD16 huff_pair_type_dt_dt = 0; |
1210 | 0 | WORD16 num_pcm_bits = -1; |
1211 | 0 | WORD16 min_bits_df_df = -1; |
1212 | 0 | WORD16 min_bits_df_dt = -1; |
1213 | 0 | WORD16 min_bits_dtbw_df = -1; |
1214 | 0 | WORD16 min_bits_dt_dt = -1; |
1215 | 0 | WORD16 allow_diff_time_back_flag = !independency_flag || (set_idx > 0); |
1216 | |
|
1217 | 0 | WORD16 quant_data_msb[2][IXHEAACE_MPS_SAC_MAX_FREQ_BANDS] = {{0}}; |
1218 | 0 | WORD16 quant_data_hist_msb[IXHEAACE_MPS_SAC_MAX_FREQ_BANDS] = {0}; |
1219 | 0 | WORD16 data_diff_freq[2][IXHEAACE_MPS_SAC_MAX_FREQ_BANDS] = {{0}}; |
1220 | 0 | WORD16 data_diff_time[2][IXHEAACE_MPS_SAC_MAX_FREQ_BANDS + 2]; |
1221 | 0 | WORD16 lav_df_df[2] = {-1, -1}; |
1222 | 0 | WORD16 lav_df_dt[2] = {-1, -1}; |
1223 | 0 | WORD16 lav_dtbw_df[2] = {-1, -1}; |
1224 | 0 | WORD16 lav_dt_dt[2] = {-1, -1}; |
1225 | 0 | WORD16 *p_quant_data_msb[2] = {NULL, NULL}; |
1226 | 0 | WORD16 *p_quant_data_hist_msb = NULL; |
1227 | |
|
1228 | 0 | if (data_bands <= 0) { |
1229 | 0 | return IA_EXHEAACE_EXE_NONFATAL_MPS_INVALID_DATA_BANDS; |
1230 | 0 | } |
1231 | | |
1232 | 0 | if (data_type == IXHEAACE_MPS_SAC_DATA_TYPE_ICC) { |
1233 | 0 | if (coarse_flag) { |
1234 | 0 | quant_levels = 4; |
1235 | 0 | quant_offset = 0; |
1236 | 0 | } else { |
1237 | 0 | quant_levels = 8; |
1238 | 0 | quant_offset = 0; |
1239 | 0 | } |
1240 | 0 | } else if (data_type == IXHEAACE_MPS_SAC_DATA_TYPE_CLD) { |
1241 | 0 | if (coarse_flag) { |
1242 | 0 | quant_levels = 15; |
1243 | 0 | quant_offset = 7; |
1244 | 0 | } else { |
1245 | 0 | quant_levels = 31; |
1246 | 0 | quant_offset = 15; |
1247 | 0 | } |
1248 | 0 | } else { |
1249 | 0 | return IA_EXHEAACE_EXE_FATAL_MPS_INVALID_HUFF_DATA_TYPE; |
1250 | 0 | } |
1251 | 0 | if (quant_offset != 0) { |
1252 | 0 | for (band = 0; band < data_bands; band++) { |
1253 | 0 | quant_data_msb[0][band] = pp_in_data[set_idx][start_band + band] + quant_offset; |
1254 | 0 | quant_data_msb[1][band] = pp_in_data[set_idx + 1][start_band + band] + quant_offset; |
1255 | 0 | } |
1256 | 0 | p_quant_data_msb[0] = quant_data_msb[0]; |
1257 | 0 | p_quant_data_msb[1] = quant_data_msb[1]; |
1258 | 0 | } else { |
1259 | 0 | p_quant_data_msb[0] = pp_in_data[set_idx] + start_band; |
1260 | 0 | p_quant_data_msb[1] = pp_in_data[set_idx + 1] + start_band; |
1261 | 0 | } |
1262 | |
|
1263 | 0 | if (allow_diff_time_back_flag) { |
1264 | 0 | if (quant_offset != 0) { |
1265 | 0 | for (band = 0; band < data_bands; band++) { |
1266 | 0 | quant_data_hist_msb[band] = p_in_data[start_band + band] + quant_offset; |
1267 | 0 | } |
1268 | 0 | p_quant_data_hist_msb = quant_data_hist_msb; |
1269 | 0 | } else { |
1270 | 0 | p_quant_data_hist_msb = p_in_data + start_band; |
1271 | 0 | } |
1272 | 0 | } |
1273 | |
|
1274 | 0 | data_diff_freq[0][0] = p_quant_data_msb[0][0]; |
1275 | 0 | data_diff_freq[1][0] = p_quant_data_msb[1][0]; |
1276 | |
|
1277 | 0 | for (band = 1; band < data_bands; band++) { |
1278 | 0 | data_diff_freq[0][band] = p_quant_data_msb[0][band] - p_quant_data_msb[0][band - 1]; |
1279 | 0 | data_diff_freq[1][band] = p_quant_data_msb[1][band] - p_quant_data_msb[1][band - 1]; |
1280 | 0 | } |
1281 | |
|
1282 | 0 | if (allow_diff_time_back_flag) { |
1283 | 0 | data_diff_time[0][0] = p_quant_data_msb[0][0]; |
1284 | 0 | data_diff_time[0][1] = p_quant_data_hist_msb[0]; |
1285 | |
|
1286 | 0 | for (band = 0; band < data_bands; band++) { |
1287 | 0 | data_diff_time[0][band + 2] = p_quant_data_msb[0][band] - p_quant_data_hist_msb[band]; |
1288 | 0 | } |
1289 | 0 | } |
1290 | |
|
1291 | 0 | data_diff_time[1][0] = p_quant_data_msb[1][0]; |
1292 | 0 | data_diff_time[1][1] = p_quant_data_msb[0][0]; |
1293 | |
|
1294 | 0 | for (band = 0; band < data_bands; band++) { |
1295 | 0 | data_diff_time[1][band + 2] = p_quant_data_msb[1][band] - p_quant_data_msb[0][band]; |
1296 | 0 | } |
1297 | |
|
1298 | 0 | num_pcm_bits = ixheaace_mps_212_calc_pcm_bits((WORD16)(2 * data_bands), quant_levels); |
1299 | 0 | num_pcm_val = (WORD16)(2 * data_bands); |
1300 | |
|
1301 | 0 | min_bits_all = num_pcm_bits; |
1302 | |
|
1303 | 0 | error = ixheaace_mps_212_calc_huff_bits(data_diff_freq[0], data_diff_freq[1], data_type, |
1304 | 0 | IXHEAACE_MPS_SAC_DIFF_FREQ, IXHEAACE_MPS_SAC_DIFF_FREQ, |
1305 | 0 | data_bands, lav_df_df, &min_bits_df_df, &huff_dim_df_df, |
1306 | 0 | &huff_pair_type_df_df); |
1307 | 0 | if (error) { |
1308 | 0 | return error; |
1309 | 0 | } |
1310 | | |
1311 | 0 | min_bits_df_df += 2; |
1312 | |
|
1313 | 0 | if (min_bits_df_df < min_bits_all) { |
1314 | 0 | min_bits_all = min_bits_df_df; |
1315 | 0 | } |
1316 | 0 | error = ixheaace_mps_212_calc_huff_bits(data_diff_freq[0], data_diff_time[1], data_type, |
1317 | 0 | IXHEAACE_MPS_SAC_DIFF_FREQ, IXHEAACE_MPS_SAC_DIFF_TIME, |
1318 | 0 | data_bands, lav_df_dt, &min_bits_df_dt, &huff_dim_df_dt, |
1319 | 0 | &huff_pair_type_df_dt); |
1320 | 0 | if (error) { |
1321 | 0 | return error; |
1322 | 0 | } |
1323 | | |
1324 | 0 | min_bits_df_dt += 2; |
1325 | |
|
1326 | 0 | if (min_bits_df_dt < min_bits_all) { |
1327 | 0 | min_bits_all = min_bits_df_dt; |
1328 | 0 | } |
1329 | |
|
1330 | 0 | if (allow_diff_time_back_flag) { |
1331 | 0 | error = ixheaace_mps_212_calc_huff_bits( |
1332 | 0 | data_diff_time[0], data_diff_freq[1], data_type, IXHEAACE_MPS_SAC_DIFF_TIME, |
1333 | 0 | IXHEAACE_MPS_SAC_DIFF_FREQ, data_bands, lav_dtbw_df, &min_bits_dtbw_df, &huff_dim_dtbw_df, |
1334 | 0 | &huff_pair_type_dtbw_df); |
1335 | 0 | if (error) { |
1336 | 0 | return error; |
1337 | 0 | } |
1338 | | |
1339 | 0 | min_bits_dtbw_df += 2; |
1340 | |
|
1341 | 0 | if (min_bits_dtbw_df < min_bits_all) { |
1342 | 0 | min_bits_all = min_bits_dtbw_df; |
1343 | 0 | } |
1344 | |
|
1345 | 0 | error = ixheaace_mps_212_calc_huff_bits( |
1346 | 0 | data_diff_time[0], data_diff_time[1], data_type, IXHEAACE_MPS_SAC_DIFF_TIME, |
1347 | 0 | IXHEAACE_MPS_SAC_DIFF_TIME, data_bands, lav_dt_dt, &min_bits_dt_dt, &huff_dim_dt_dt, |
1348 | 0 | &huff_pair_type_dt_dt); |
1349 | 0 | if (error) { |
1350 | 0 | return error; |
1351 | 0 | } |
1352 | | |
1353 | 0 | min_bits_dt_dt += 2; |
1354 | |
|
1355 | 0 | if (min_bits_dt_dt < min_bits_all) { |
1356 | 0 | min_bits_all = min_bits_dt_dt; |
1357 | 0 | } |
1358 | 0 | } |
1359 | | |
1360 | 0 | pcm_coding_flag = (min_bits_all == num_pcm_bits); |
1361 | |
|
1362 | 0 | ixheaace_write_bits(pstr_bit_buf, pcm_coding_flag, 1); |
1363 | |
|
1364 | 0 | if (pcm_coding_flag) { |
1365 | 0 | ixheaace_mps_212_apply_pcm_coding(pstr_bit_buf, pp_in_data[set_idx] + start_band, |
1366 | 0 | pp_in_data[set_idx + 1] + start_band, quant_offset, |
1367 | 0 | num_pcm_val, quant_levels); |
1368 | 0 | } else { |
1369 | 0 | min_found = 0; |
1370 | |
|
1371 | 0 | if (min_bits_all == min_bits_df_df) { |
1372 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_FREQ, 1); |
1373 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_FREQ, 1); |
1374 | |
|
1375 | 0 | error = ixheaace_mps_212_apply_huff_coding( |
1376 | 0 | pstr_bit_buf, data_diff_freq[0], data_diff_freq[1], data_type, |
1377 | 0 | IXHEAACE_MPS_SAC_DIFF_FREQ, IXHEAACE_MPS_SAC_DIFF_FREQ, data_bands, lav_df_df, |
1378 | 0 | huff_dim_df_df, huff_pair_type_df_df); |
1379 | 0 | if (error) { |
1380 | 0 | return error; |
1381 | 0 | } |
1382 | 0 | min_found = 1; |
1383 | 0 | } |
1384 | | |
1385 | 0 | if (!min_found && (min_bits_all == min_bits_df_dt)) { |
1386 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_FREQ, 1); |
1387 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_TIME, 1); |
1388 | |
|
1389 | 0 | error = ixheaace_mps_212_apply_huff_coding( |
1390 | 0 | pstr_bit_buf, data_diff_freq[0], data_diff_time[1], data_type, |
1391 | 0 | IXHEAACE_MPS_SAC_DIFF_FREQ, IXHEAACE_MPS_SAC_DIFF_TIME, data_bands, lav_df_dt, |
1392 | 0 | huff_dim_df_dt, huff_pair_type_df_dt); |
1393 | 0 | if (error) { |
1394 | 0 | return error; |
1395 | 0 | } |
1396 | 0 | min_found = 1; |
1397 | 0 | } |
1398 | | |
1399 | 0 | if (allow_diff_time_back_flag) { |
1400 | 0 | if (!min_found && (min_bits_all == min_bits_dtbw_df)) { |
1401 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_TIME, 1); |
1402 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_FREQ, 1); |
1403 | |
|
1404 | 0 | error = ixheaace_mps_212_apply_huff_coding( |
1405 | 0 | pstr_bit_buf, data_diff_time[0], data_diff_freq[1], data_type, |
1406 | 0 | IXHEAACE_MPS_SAC_DIFF_TIME, IXHEAACE_MPS_SAC_DIFF_FREQ, data_bands, lav_dtbw_df, |
1407 | 0 | huff_dim_dtbw_df, huff_pair_type_dtbw_df); |
1408 | 0 | if (error) { |
1409 | 0 | return error; |
1410 | 0 | } |
1411 | 0 | min_found = 1; |
1412 | 0 | } |
1413 | 0 | if (!min_found && (min_bits_all == min_bits_dt_dt)) { |
1414 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_TIME, 1); |
1415 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_TIME, 1); |
1416 | |
|
1417 | 0 | error = ixheaace_mps_212_apply_huff_coding( |
1418 | 0 | pstr_bit_buf, data_diff_time[0], data_diff_time[1], data_type, |
1419 | 0 | IXHEAACE_MPS_SAC_DIFF_TIME, IXHEAACE_MPS_SAC_DIFF_TIME, data_bands, lav_dt_dt, |
1420 | 0 | huff_dim_dt_dt, huff_pair_type_dt_dt); |
1421 | 0 | if (error) { |
1422 | 0 | return error; |
1423 | 0 | } |
1424 | 0 | } |
1425 | 0 | } |
1426 | 0 | } |
1427 | | |
1428 | 0 | return IA_NO_ERROR; |
1429 | 0 | } |
1430 | | |
1431 | | IA_ERRORCODE ixheaace_mps_212_ec_data_single_enc( |
1432 | | ixheaace_bit_buf_handle pstr_bit_buf, WORD16 pp_in_data[][IXHEAACE_MPS_SAC_MAX_FREQ_BANDS], |
1433 | | WORD16 p_in_data[IXHEAACE_MPS_SAC_MAX_FREQ_BANDS], const WORD32 data_type, |
1434 | | const WORD32 set_idx, const WORD32 start_band, const WORD16 data_bands, |
1435 | 35.4k | const WORD32 coarse_flag, const WORD32 independency_flag) { |
1436 | 35.4k | IA_ERRORCODE error; |
1437 | 35.4k | WORD16 band = 0; |
1438 | 35.4k | WORD16 quant_levels = 0, quant_offset = 0, num_pcm_val = 0; |
1439 | 35.4k | WORD16 pcm_coding_flag = 0; |
1440 | 35.4k | WORD16 min_bits_all = 0; |
1441 | 35.4k | WORD16 min_found = 0; |
1442 | 35.4k | WORD16 huff_dim_df = 0; |
1443 | 35.4k | WORD16 huff_pair_type_df = 0; |
1444 | 35.4k | WORD16 huff_dim_dt = 0; |
1445 | 35.4k | WORD16 huff_pair_type_dt = 0; |
1446 | 35.4k | WORD16 allow_diff_time_back_flag = !independency_flag || (set_idx > 0); |
1447 | 35.4k | WORD16 num_pcm_bits = -1; |
1448 | 35.4k | WORD16 min_bits_df = -1; |
1449 | 35.4k | WORD16 min_bits_dt = -1; |
1450 | | |
1451 | 35.4k | WORD16 quant_data_msb[IXHEAACE_MPS_SAC_MAX_FREQ_BANDS] = {0}; |
1452 | 35.4k | WORD16 quant_data_hist_msb[IXHEAACE_MPS_SAC_MAX_FREQ_BANDS] = {0}; |
1453 | 35.4k | WORD16 data_diff_freq[IXHEAACE_MPS_SAC_MAX_FREQ_BANDS] = {0}; |
1454 | 35.4k | WORD16 data_diff_time[IXHEAACE_MPS_SAC_MAX_FREQ_BANDS + 2] = {0}; |
1455 | 35.4k | WORD16 *p_quant_data_msb; |
1456 | 35.4k | WORD16 *p_quant_data_hist_msb = NULL; |
1457 | 35.4k | WORD16 lav_df[2] = {-1, -1}; |
1458 | 35.4k | WORD16 lav_dt[2] = {-1, -1}; |
1459 | | |
1460 | 35.4k | if (data_type == IXHEAACE_MPS_SAC_DATA_TYPE_ICC) { |
1461 | 17.8k | if (coarse_flag) { |
1462 | 0 | quant_levels = 4; |
1463 | 0 | quant_offset = 0; |
1464 | 17.8k | } else { |
1465 | 17.8k | quant_levels = 8; |
1466 | 17.8k | quant_offset = 0; |
1467 | 17.8k | } |
1468 | 17.8k | } else if (data_type == IXHEAACE_MPS_SAC_DATA_TYPE_CLD) { |
1469 | 17.5k | if (coarse_flag) { |
1470 | 0 | quant_levels = 15; |
1471 | 0 | quant_offset = 7; |
1472 | 17.5k | } else { |
1473 | 17.5k | quant_levels = 31; |
1474 | 17.5k | quant_offset = 15; |
1475 | 17.5k | } |
1476 | 17.5k | } else { |
1477 | 0 | return IA_EXHEAACE_EXE_FATAL_MPS_INVALID_HUFF_DATA_TYPE; |
1478 | 0 | } |
1479 | 35.4k | if (quant_offset != 0) { |
1480 | 452k | for (band = 0; band < data_bands; band++) { |
1481 | 434k | quant_data_msb[band] = pp_in_data[set_idx][start_band + band] + quant_offset; |
1482 | 434k | } |
1483 | 17.5k | p_quant_data_msb = quant_data_msb; |
1484 | 17.8k | } else { |
1485 | 17.8k | p_quant_data_msb = pp_in_data[set_idx] + start_band; |
1486 | 17.8k | } |
1487 | | |
1488 | 35.4k | if (allow_diff_time_back_flag) { |
1489 | 14.8k | if (quant_offset != 0) { |
1490 | 174k | for (band = 0; band < data_bands; band++) { |
1491 | 167k | quant_data_hist_msb[band] = p_in_data[start_band + band] + quant_offset; |
1492 | 167k | } |
1493 | 7.28k | p_quant_data_hist_msb = quant_data_hist_msb; |
1494 | 7.57k | } else { |
1495 | 7.57k | p_quant_data_hist_msb = p_in_data + start_band; |
1496 | 7.57k | } |
1497 | 14.8k | } |
1498 | | |
1499 | 35.4k | data_diff_freq[0] = p_quant_data_msb[0]; |
1500 | | |
1501 | 873k | for (band = 1; band < data_bands; band++) { |
1502 | 838k | data_diff_freq[band] = p_quant_data_msb[band] - p_quant_data_msb[band - 1]; |
1503 | 838k | } |
1504 | | |
1505 | 35.4k | if (allow_diff_time_back_flag) { |
1506 | 14.8k | data_diff_time[0] = p_quant_data_msb[0]; |
1507 | 14.8k | data_diff_time[1] = p_quant_data_hist_msb[0]; |
1508 | | |
1509 | 354k | for (band = 0; band < data_bands; band++) { |
1510 | 339k | data_diff_time[band + 2] = p_quant_data_msb[band] - p_quant_data_hist_msb[band]; |
1511 | 339k | } |
1512 | 14.8k | } |
1513 | | |
1514 | 35.4k | num_pcm_bits = ixheaace_mps_212_calc_pcm_bits(data_bands, quant_levels); |
1515 | 35.4k | num_pcm_val = data_bands; |
1516 | 35.4k | min_bits_all = num_pcm_bits; |
1517 | | |
1518 | 35.4k | error = ixheaace_mps_212_calc_huff_bits( |
1519 | 35.4k | data_diff_freq, NULL, data_type, IXHEAACE_MPS_SAC_DIFF_FREQ, IXHEAACE_MPS_SAC_DIFF_FREQ, |
1520 | 35.4k | data_bands, lav_df, &min_bits_df, &huff_dim_df, &huff_pair_type_df); |
1521 | 35.4k | if (error) { |
1522 | 0 | return error; |
1523 | 0 | } |
1524 | | |
1525 | 35.4k | if (allow_diff_time_back_flag) min_bits_df += 1; |
1526 | | |
1527 | 35.4k | if (min_bits_df < min_bits_all) { |
1528 | 32.0k | min_bits_all = min_bits_df; |
1529 | 32.0k | } |
1530 | 35.4k | if (allow_diff_time_back_flag) { |
1531 | 14.8k | error = ixheaace_mps_212_calc_huff_bits( |
1532 | 14.8k | data_diff_time, NULL, data_type, IXHEAACE_MPS_SAC_DIFF_TIME, IXHEAACE_MPS_SAC_DIFF_TIME, |
1533 | 14.8k | data_bands, lav_dt, &min_bits_dt, &huff_dim_dt, &huff_pair_type_dt); |
1534 | 14.8k | if (error) { |
1535 | 0 | return error; |
1536 | 0 | } |
1537 | | |
1538 | 14.8k | min_bits_dt += 1; |
1539 | | |
1540 | 14.8k | if (min_bits_dt < min_bits_all) { |
1541 | 6.20k | min_bits_all = min_bits_dt; |
1542 | 6.20k | } |
1543 | 14.8k | } |
1544 | 35.4k | pcm_coding_flag = (min_bits_all == num_pcm_bits); |
1545 | | |
1546 | 35.4k | ixheaace_write_bits(pstr_bit_buf, pcm_coding_flag, 1); |
1547 | | |
1548 | 35.4k | if (pcm_coding_flag) { |
1549 | 1.77k | ixheaace_mps_212_apply_pcm_coding(pstr_bit_buf, pp_in_data[set_idx] + start_band, NULL, |
1550 | 1.77k | quant_offset, num_pcm_val, quant_levels); |
1551 | 33.6k | } else { |
1552 | 33.6k | min_found = 0; |
1553 | 33.6k | if (min_bits_all == min_bits_df) { |
1554 | 27.4k | if (allow_diff_time_back_flag) { |
1555 | 7.54k | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_FREQ, 1); |
1556 | 7.54k | } |
1557 | | |
1558 | 27.4k | error = ixheaace_mps_212_apply_huff_coding( |
1559 | 27.4k | pstr_bit_buf, data_diff_freq, NULL, data_type, IXHEAACE_MPS_SAC_DIFF_FREQ, |
1560 | 27.4k | IXHEAACE_MPS_SAC_DIFF_FREQ, data_bands, lav_df, huff_dim_df, huff_pair_type_df); |
1561 | 27.4k | if (error) { |
1562 | 0 | return error; |
1563 | 0 | } |
1564 | 27.4k | min_found = 1; |
1565 | 27.4k | } |
1566 | | |
1567 | 33.6k | if (allow_diff_time_back_flag) { |
1568 | 13.7k | if (!min_found && (min_bits_all == min_bits_dt)) { |
1569 | 6.20k | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_TIME, 1); |
1570 | | |
1571 | 6.20k | error = ixheaace_mps_212_apply_huff_coding( |
1572 | 6.20k | pstr_bit_buf, data_diff_time, NULL, data_type, IXHEAACE_MPS_SAC_DIFF_TIME, |
1573 | 6.20k | IXHEAACE_MPS_SAC_DIFF_TIME, data_bands, lav_dt, huff_dim_dt, huff_pair_type_dt); |
1574 | 6.20k | if (error) { |
1575 | 0 | return error; |
1576 | 0 | } |
1577 | 6.20k | } |
1578 | 13.7k | } |
1579 | 33.6k | } |
1580 | | |
1581 | 35.4k | return IA_NO_ERROR; |
1582 | 35.4k | } |
1583 | | |
1584 | | VOID ixheaace_mps_515_ec_data_pair_enc(ixheaace_bit_buf_handle pstr_bit_buf, |
1585 | | WORD32 pp_in_data[][MAXBANDS], |
1586 | | WORD32 p_in_data[MAXBANDS], WORD32 data_type, |
1587 | | WORD32 set_idx, WORD32 start_band, WORD32 data_bands, |
1588 | | WORD32 pair_flag, WORD32 coarse_flag, |
1589 | 150k | WORD32 independency_flag) { |
1590 | 150k | WORD32 dummy = 0; |
1591 | 150k | WORD32 quant_levels = 0, quant_offset = 0, num_pcm_val = 0; |
1592 | | |
1593 | 150k | WORD32 split_lsb_flag = 0; |
1594 | 150k | WORD32 pcm_coding_flag = 0; |
1595 | | |
1596 | 150k | WORD32 min_bits_df_df = -1; |
1597 | 150k | WORD32 min_bits_df_dt = -1; |
1598 | 150k | WORD32 min_bits_dtbw_df = -1; |
1599 | 150k | WORD32 min_bits_dtfw_df = -1; |
1600 | | |
1601 | 150k | WORD32 min_bits_dt_dt = -1; |
1602 | 150k | WORD32 allow_diff_time_back_flag = !independency_flag || (set_idx > 0); |
1603 | | |
1604 | 150k | WORD32 num_lsb_bits[2] = {0, 0}; |
1605 | 150k | WORD32 num_pcm_bits = 0; |
1606 | | |
1607 | 150k | WORD32 a_data_hist[MAXBANDS] = {0}; |
1608 | 150k | WORD32 aa_data_pair[2][MAXBANDS] = {{0}}; |
1609 | | |
1610 | 150k | WORD32 quant_data_lsb[2][MAXBANDS] = {{0}}; |
1611 | 150k | WORD32 quant_data_msb[2][MAXBANDS] = {{0}}; |
1612 | | |
1613 | 150k | WORD32 quant_data_hist_lsb[MAXBANDS] = {0}; |
1614 | 150k | WORD32 quant_data_hist_msb[MAXBANDS] = {0}; |
1615 | | |
1616 | 150k | WORD32 data_diff_freq[2][MAXBANDS] = {{0}}; |
1617 | 150k | WORD32 data_diff_time_bw[2][MAXBANDS + 1] = {{0}}; |
1618 | 150k | WORD32 data_diff_time_fw[MAXBANDS + 1] = {0}; |
1619 | | |
1620 | 150k | WORD32 *p_data_pcm[2] = {NULL}; |
1621 | 150k | WORD32 *p_data_diff_freq[2] = {NULL}; |
1622 | 150k | WORD32 *p_data_diff_time_bw[2] = {NULL}; |
1623 | 150k | WORD32 *p_data_diff_time_fw = NULL; |
1624 | | |
1625 | 150k | WORD32 min_bits_all = 0; |
1626 | 150k | WORD32 min_found = 0; |
1627 | | |
1628 | 150k | switch (data_type) { |
1629 | 93.8k | case IXHEAACE_MPS_SAC_DATA_TYPE_CLD: |
1630 | 93.8k | if (coarse_flag) { |
1631 | 0 | split_lsb_flag = 0; |
1632 | 0 | quant_levels = 15; |
1633 | 0 | quant_offset = 7; |
1634 | 93.8k | } else { |
1635 | 93.8k | split_lsb_flag = 0; |
1636 | 93.8k | quant_levels = 31; |
1637 | 93.8k | quant_offset = 15; |
1638 | 93.8k | } |
1639 | | |
1640 | 93.8k | break; |
1641 | | |
1642 | 56.2k | case IXHEAACE_MPS_SAC_DATA_TYPE_ICC: |
1643 | 56.2k | if (coarse_flag) { |
1644 | 0 | split_lsb_flag = 0; |
1645 | 0 | quant_levels = 4; |
1646 | 0 | quant_offset = 0; |
1647 | 56.2k | } else { |
1648 | 56.2k | split_lsb_flag = 0; |
1649 | 56.2k | quant_levels = 8; |
1650 | 56.2k | quant_offset = 0; |
1651 | 56.2k | } |
1652 | | |
1653 | 56.2k | break; |
1654 | | |
1655 | 0 | case IXHEAACE_MPS_SAC_DATA_TYPE_CPC: |
1656 | 0 | if (coarse_flag) { |
1657 | 0 | split_lsb_flag = 0; |
1658 | 0 | quant_levels = 26; |
1659 | 0 | quant_offset = 0; |
1660 | 0 | } else { |
1661 | 0 | split_lsb_flag = 1; |
1662 | 0 | quant_levels = 51; |
1663 | 0 | quant_offset = 0; |
1664 | 0 | } |
1665 | |
|
1666 | 0 | break; |
1667 | | |
1668 | 0 | default: |
1669 | 0 | return; |
1670 | 150k | } |
1671 | | |
1672 | 150k | memcpy(a_data_hist, p_in_data + start_band, sizeof(WORD32) * data_bands); |
1673 | | |
1674 | 150k | memcpy(aa_data_pair[0], pp_in_data[set_idx] + start_band, sizeof(int) * data_bands); |
1675 | 150k | p_data_pcm[0] = aa_data_pair[0]; |
1676 | 150k | if (pair_flag) { |
1677 | 0 | memcpy(aa_data_pair[1], pp_in_data[set_idx + 1] + start_band, sizeof(int) * data_bands); |
1678 | 0 | p_data_pcm[1] = aa_data_pair[1]; |
1679 | 0 | } |
1680 | | |
1681 | 150k | num_lsb_bits[0] = |
1682 | 150k | ixheaace_mps_515_split_lsb(aa_data_pair[0], quant_offset, split_lsb_flag ? 1 : 0, |
1683 | 150k | data_bands, quant_data_lsb[0], quant_data_msb[0]); |
1684 | | |
1685 | 150k | if (pair_flag) { |
1686 | 0 | num_lsb_bits[1] = |
1687 | 0 | ixheaace_mps_515_split_lsb(aa_data_pair[1], quant_offset, split_lsb_flag ? 1 : 0, |
1688 | 0 | data_bands, quant_data_lsb[1], quant_data_msb[1]); |
1689 | 0 | } |
1690 | 150k | (VOID) num_lsb_bits; |
1691 | 150k | if (allow_diff_time_back_flag) { |
1692 | 0 | dummy = ixheaace_mps_515_split_lsb(a_data_hist, quant_offset, split_lsb_flag ? 1 : 0, |
1693 | 0 | data_bands, quant_data_hist_lsb, quant_data_hist_msb); |
1694 | 0 | (VOID) dummy; |
1695 | 0 | } |
1696 | | |
1697 | 150k | ixheaace_mps_515_calc_diff_freq(quant_data_msb[0], data_diff_freq[0], data_bands); |
1698 | 150k | p_data_diff_freq[0] = data_diff_freq[0]; |
1699 | | |
1700 | 150k | if (pair_flag) { |
1701 | 0 | ixheaace_mps_515_calc_diff_freq(quant_data_msb[1], data_diff_freq[1], data_bands); |
1702 | 0 | p_data_diff_freq[1] = data_diff_freq[1]; |
1703 | 0 | } |
1704 | | |
1705 | 150k | if (allow_diff_time_back_flag) { |
1706 | 0 | ixheaace_mps_515_calc_diff_time(quant_data_msb[0], quant_data_hist_msb, |
1707 | 0 | data_diff_time_bw[0] + 1, |
1708 | 0 | IXHEAACE_MPS_SAC_DIRECTION_BACKWARDS, data_bands); |
1709 | 0 | p_data_diff_time_bw[0] = data_diff_time_bw[0]; |
1710 | 0 | } |
1711 | | |
1712 | 150k | if (pair_flag) { |
1713 | 0 | ixheaace_mps_515_calc_diff_time(quant_data_msb[1], quant_data_msb[0], |
1714 | 0 | data_diff_time_bw[1] + 1, |
1715 | 0 | IXHEAACE_MPS_SAC_DIRECTION_BACKWARDS, data_bands); |
1716 | 0 | p_data_diff_time_bw[1] = data_diff_time_bw[1]; |
1717 | |
|
1718 | 0 | ixheaace_mps_515_calc_diff_time(quant_data_msb[1], quant_data_msb[0], data_diff_time_fw + 1, |
1719 | 0 | IXHEAACE_MPS_SAC_DIRECTION_FORWARDS, data_bands); |
1720 | 0 | p_data_diff_time_fw = data_diff_time_fw; |
1721 | 0 | } |
1722 | | |
1723 | 150k | if (pair_flag) { |
1724 | 0 | num_pcm_bits = ixheaace_mps_515_calc_pcm_bits(2 * data_bands, quant_levels); |
1725 | 0 | num_pcm_val = 2 * data_bands; |
1726 | 150k | } else { |
1727 | 150k | num_pcm_bits = ixheaace_mps_515_calc_pcm_bits(data_bands, quant_levels); |
1728 | 150k | num_pcm_val = data_bands; |
1729 | 150k | } |
1730 | | |
1731 | 150k | min_bits_all = num_pcm_bits; |
1732 | | |
1733 | 150k | if ((p_data_diff_freq[0] != NULL) || (p_data_diff_freq[1] != NULL)) { |
1734 | 150k | min_bits_df_df = ixheaace_mps_515_calc_huff_bits(p_data_diff_freq[0], p_data_diff_freq[1], |
1735 | 150k | data_type, IXHEAACE_MPS_SAC_DIFF_FREQ, |
1736 | 150k | IXHEAACE_MPS_SAC_DIFF_FREQ, data_bands); |
1737 | | |
1738 | 150k | if (pair_flag || allow_diff_time_back_flag) min_bits_df_df += 1; |
1739 | 150k | if (pair_flag) min_bits_df_df += 1; |
1740 | | |
1741 | 150k | if (min_bits_df_df < min_bits_all) { |
1742 | 140k | min_bits_all = min_bits_df_df; |
1743 | 140k | } |
1744 | 150k | } |
1745 | | |
1746 | 150k | if ((p_data_diff_freq[0] != NULL) || (p_data_diff_time_bw[1] != NULL)) { |
1747 | 150k | min_bits_df_dt = ixheaace_mps_515_calc_huff_bits(p_data_diff_freq[0], p_data_diff_time_bw[1], |
1748 | 150k | data_type, IXHEAACE_MPS_SAC_DIFF_FREQ, |
1749 | 150k | IXHEAACE_MPS_SAC_DIFF_TIME, data_bands); |
1750 | | |
1751 | 150k | if (pair_flag || allow_diff_time_back_flag) min_bits_df_dt += 1; |
1752 | 150k | if (pair_flag) min_bits_df_dt += 1; |
1753 | | |
1754 | 150k | if (min_bits_df_dt < min_bits_all) { |
1755 | 0 | min_bits_all = min_bits_df_dt; |
1756 | 0 | } |
1757 | 150k | } |
1758 | | |
1759 | 150k | if ((p_data_diff_time_fw != NULL) || (p_data_diff_freq[1] != NULL)) { |
1760 | 0 | min_bits_dtfw_df = ixheaace_mps_515_calc_huff_bits(p_data_diff_time_fw, p_data_diff_freq[1], |
1761 | 0 | data_type, IXHEAACE_MPS_SAC_DIFF_TIME, |
1762 | 0 | IXHEAACE_MPS_SAC_DIFF_FREQ, data_bands); |
1763 | |
|
1764 | 0 | if (pair_flag || allow_diff_time_back_flag) min_bits_dtfw_df += 1; |
1765 | 0 | if (pair_flag && allow_diff_time_back_flag) min_bits_dtfw_df += 1; |
1766 | |
|
1767 | 0 | if (pair_flag && allow_diff_time_back_flag) { |
1768 | 0 | min_bits_dtfw_df += 1; |
1769 | 0 | } |
1770 | |
|
1771 | 0 | if (min_bits_dtfw_df < min_bits_all) { |
1772 | 0 | min_bits_all = min_bits_dtfw_df; |
1773 | 0 | } |
1774 | 0 | } |
1775 | | |
1776 | 150k | if (allow_diff_time_back_flag) { |
1777 | 0 | if ((p_data_diff_time_bw[0] != NULL) || (p_data_diff_freq[1] != NULL)) { |
1778 | 0 | min_bits_dtbw_df = ixheaace_mps_515_calc_huff_bits( |
1779 | 0 | p_data_diff_time_bw[0], p_data_diff_freq[1], data_type, IXHEAACE_MPS_SAC_DIFF_TIME, |
1780 | 0 | IXHEAACE_MPS_SAC_DIFF_FREQ, data_bands); |
1781 | |
|
1782 | 0 | min_bits_dtbw_df += 1; |
1783 | 0 | if (pair_flag) { |
1784 | 0 | min_bits_dtbw_df += 2; |
1785 | 0 | } |
1786 | |
|
1787 | 0 | if (min_bits_dtbw_df < min_bits_all) { |
1788 | 0 | min_bits_all = min_bits_dtbw_df; |
1789 | 0 | } |
1790 | 0 | } |
1791 | |
|
1792 | 0 | if ((p_data_diff_time_bw[0] != NULL) || (p_data_diff_time_bw[1] != NULL)) { |
1793 | 0 | min_bits_dt_dt = ixheaace_mps_515_calc_huff_bits( |
1794 | 0 | p_data_diff_time_bw[0], p_data_diff_time_bw[1], data_type, IXHEAACE_MPS_SAC_DIFF_TIME, |
1795 | 0 | IXHEAACE_MPS_SAC_DIFF_TIME, data_bands); |
1796 | |
|
1797 | 0 | min_bits_dt_dt += 1; |
1798 | 0 | if (pair_flag) min_bits_dt_dt += 1; |
1799 | |
|
1800 | 0 | if (min_bits_dt_dt < min_bits_all) { |
1801 | 0 | min_bits_all = min_bits_dt_dt; |
1802 | 0 | } |
1803 | 0 | } |
1804 | 0 | } |
1805 | | |
1806 | 150k | pcm_coding_flag = (min_bits_all == num_pcm_bits); |
1807 | | |
1808 | 150k | ixheaace_write_bits(pstr_bit_buf, pcm_coding_flag, 1); |
1809 | | |
1810 | 150k | if (pcm_coding_flag) { |
1811 | 9.72k | if (data_bands >= PBC_MIN_BANDS) { |
1812 | 2.45k | ixheaace_write_bits(pstr_bit_buf, 0, 1); |
1813 | 2.45k | } |
1814 | | |
1815 | 9.72k | ixheaace_mps_515_apply_pcm_coding(pstr_bit_buf, p_data_pcm[0], p_data_pcm[1], quant_offset, |
1816 | 9.72k | num_pcm_val, quant_levels); |
1817 | 140k | } else { |
1818 | 140k | min_found = 0; |
1819 | | |
1820 | 140k | if (min_bits_all == min_bits_df_df) { |
1821 | 140k | if (pair_flag || allow_diff_time_back_flag) { |
1822 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_FREQ, 1); |
1823 | 0 | } |
1824 | | |
1825 | 140k | if (pair_flag) { |
1826 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_FREQ, 1); |
1827 | 0 | } |
1828 | | |
1829 | 140k | ixheaace_mps_515_apply_huff_coding(pstr_bit_buf, p_data_diff_freq[0], p_data_diff_freq[1], |
1830 | 140k | data_type, IXHEAACE_MPS_SAC_DIFF_FREQ, |
1831 | 140k | IXHEAACE_MPS_SAC_DIFF_FREQ, data_bands); |
1832 | | |
1833 | 140k | min_found = 1; |
1834 | 140k | } |
1835 | | |
1836 | 140k | if (!min_found && (min_bits_all == min_bits_df_dt)) { |
1837 | 0 | if (pair_flag || allow_diff_time_back_flag) { |
1838 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_FREQ, 1); |
1839 | 0 | } |
1840 | |
|
1841 | 0 | if (pair_flag) { |
1842 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_TIME, 1); |
1843 | 0 | } |
1844 | |
|
1845 | 0 | ixheaace_mps_515_apply_huff_coding( |
1846 | 0 | pstr_bit_buf, p_data_diff_freq[0], p_data_diff_time_bw[1], data_type, |
1847 | 0 | IXHEAACE_MPS_SAC_DIFF_FREQ, IXHEAACE_MPS_SAC_DIFF_TIME, data_bands); |
1848 | |
|
1849 | 0 | min_found = 1; |
1850 | 0 | } |
1851 | | |
1852 | 140k | if (!min_found && (min_bits_all == min_bits_dtfw_df)) { |
1853 | 0 | if (pair_flag || allow_diff_time_back_flag) { |
1854 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_TIME, 1); |
1855 | 0 | } |
1856 | |
|
1857 | 0 | if (pair_flag && allow_diff_time_back_flag) { |
1858 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_FREQ, 1); |
1859 | 0 | } |
1860 | |
|
1861 | 0 | ixheaace_mps_515_apply_huff_coding(pstr_bit_buf, p_data_diff_time_fw, p_data_diff_freq[1], |
1862 | 0 | data_type, IXHEAACE_MPS_SAC_DIFF_TIME, |
1863 | 0 | IXHEAACE_MPS_SAC_DIFF_FREQ, data_bands); |
1864 | |
|
1865 | 0 | if (pair_flag && allow_diff_time_back_flag) { |
1866 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIRECTION_FORWARDS, 1); |
1867 | 0 | } |
1868 | |
|
1869 | 0 | min_found = 1; |
1870 | 0 | } |
1871 | | |
1872 | 140k | if (allow_diff_time_back_flag) { |
1873 | 0 | if (!min_found && (min_bits_all == min_bits_dtbw_df)) { |
1874 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_TIME, 1); |
1875 | |
|
1876 | 0 | if (pair_flag) { |
1877 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_FREQ, 1); |
1878 | 0 | } |
1879 | |
|
1880 | 0 | ixheaace_mps_515_apply_huff_coding( |
1881 | 0 | pstr_bit_buf, p_data_diff_time_bw[0], p_data_diff_freq[1], data_type, |
1882 | 0 | IXHEAACE_MPS_SAC_DIFF_TIME, IXHEAACE_MPS_SAC_DIFF_FREQ, data_bands); |
1883 | |
|
1884 | 0 | if (pair_flag) { |
1885 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIRECTION_BACKWARDS, 1); |
1886 | 0 | } |
1887 | |
|
1888 | 0 | min_found = 1; |
1889 | 0 | } |
1890 | |
|
1891 | 0 | if (!min_found && (min_bits_all == min_bits_dt_dt)) { |
1892 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_TIME, 1); |
1893 | |
|
1894 | 0 | if (pair_flag) { |
1895 | 0 | ixheaace_write_bits(pstr_bit_buf, IXHEAACE_MPS_SAC_DIFF_TIME, 1); |
1896 | 0 | } |
1897 | |
|
1898 | 0 | ixheaace_mps_515_apply_huff_coding( |
1899 | 0 | pstr_bit_buf, p_data_diff_time_bw[0], p_data_diff_time_bw[1], data_type, |
1900 | 0 | IXHEAACE_MPS_SAC_DIFF_TIME, IXHEAACE_MPS_SAC_DIFF_TIME, data_bands); |
1901 | 0 | } |
1902 | 0 | } |
1903 | | |
1904 | 140k | if (split_lsb_flag) { |
1905 | 0 | ixheaace_mps_515_apply_lsb_coding(pstr_bit_buf, quant_data_lsb[0], 1, data_bands); |
1906 | |
|
1907 | 0 | if (pair_flag) { |
1908 | 0 | ixheaace_mps_515_apply_lsb_coding(pstr_bit_buf, quant_data_lsb[1], 1, data_bands); |
1909 | 0 | } |
1910 | 0 | } |
1911 | 140k | } |
1912 | | |
1913 | 150k | return; |
1914 | 150k | } |