1 /******************************************************************************
2 * *
3 * Copyright (C) 2023 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************
18 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20
21 #include "ixheaac_type_def.h"
22
23 #include "ixheaace_sbr_header.h"
24 #include "ixheaace_sbr_def.h"
25 #include "ixheaace_resampler.h"
26 #include "ixheaace_sbr_rom.h"
27 #include "ixheaace_common_rom.h"
28 #include "ixheaace_bitbuffer.h"
29 #include "ixheaace_sbr_main.h"
30
31 #include "ixheaace_sbr_hybrid.h"
32 #include "ixheaace_sbr_ps_enc.h"
33 #include "ixheaace_sbr_ps_bitenc.h"
34 #include "ixheaace_env_bit.h"
35
iexheaax_append_bitstream(ixheaace_bit_buf_handle hdl_bitbuf_write,ixheaace_bit_buf_handle hdl_bitbuf_read,UWORD8 num_bits)36 static WORD32 iexheaax_append_bitstream(ixheaace_bit_buf_handle hdl_bitbuf_write,
37 ixheaace_bit_buf_handle hdl_bitbuf_read,
38 UWORD8 num_bits) {
39 WORD32 idx;
40 UWORD32 value;
41
42 if (num_bits > 16) {
43 WORD32 cnt;
44 UWORD8 rem;
45 cnt = num_bits >> 4;
46 rem = num_bits % 16;
47
48 for (idx = 0; idx < cnt; idx++) {
49 value = ixheaace_readbits(hdl_bitbuf_read, 16);
50 ixheaace_write_bits(hdl_bitbuf_write, value, 16);
51 }
52 if (rem) {
53 value = ixheaace_readbits(hdl_bitbuf_read, rem);
54 ixheaace_write_bits(hdl_bitbuf_write, value, rem);
55 }
56 } else {
57 value = ixheaace_readbits(hdl_bitbuf_read, num_bits);
58 ixheaace_write_bits(hdl_bitbuf_write, value, num_bits);
59 }
60
61 return num_bits;
62 }
63
64 WORD32
ixheaace_enc_write_ps_data(ixheaace_pstr_ps_enc pstr_ps_handle,WORD32 b_header_active,ixheaace_str_ps_tab * ps_tables)65 ixheaace_enc_write_ps_data(ixheaace_pstr_ps_enc pstr_ps_handle, WORD32 b_header_active,
66 ixheaace_str_ps_tab *ps_tables) {
67 WORD32 tmp_var, gr;
68 const WORD32 *aa_huff_book_iid_c;
69 const WORD16 *aa_huff_book_icc_c;
70 const WORD8 *aa_huff_book_iid_l;
71 const WORD8 *aa_huff_book_icc_l;
72 WORD32 *aa_delta_iid;
73 WORD32 *aa_delta_icc;
74
75 WORD8 index, last_index;
76 WORD32 no_bits_f = 0;
77 WORD32 no_bits_t = 0;
78
79 WORD32 aa_delta_iid_t[NUMBER_OF_IID_BINS] = {0};
80 WORD32 aa_delta_icc_t[NUMBER_OF_ICC_BINS] = {0};
81
82 WORD32 aa_delta_iid_f[NUMBER_OF_IID_BINS] = {0};
83 WORD32 aa_delta_icc_f[NUMBER_OF_ICC_BINS] = {0};
84
85 WORD32 ab_dt_flag_iid;
86 WORD32 ab_dt_flag_icc;
87
88 WORD32 b_send_header;
89
90 UWORD32 b_zero_iid = 1;
91 UWORD32 b_zero_icc = 1;
92 UWORD32 b_keep_params = 1;
93
94 ixheaace_bit_buf_handle bb = &pstr_ps_handle->ps_bit_buf;
95
96 tmp_var = ia_enhaacplus_enc_get_bits_available(bb);
97
98 /* bit buffer shall be empty */
99 if (tmp_var != 0) {
100 return -1;
101 }
102
103 if (b_header_active) {
104 b_keep_params = 0;
105 }
106
107 last_index = 0;
108
109 for (gr = 0; gr < pstr_ps_handle->iid_icc_bins; gr++) {
110 WORD IID_flag;
111 FLOAT32 pan_value = pstr_ps_handle->aaa_IID_data_buf[gr][SYSTEMLOOKAHEAD];
112 IID_flag = 0;
113 if ((pan_value >= -ps_tables->pan_class[0]) && (pan_value <= ps_tables->pan_class[0])) {
114 IID_flag = 1;
115 }
116
117 if (IID_flag) {
118 index = 0;
119 } else {
120 if (pan_value < 0) {
121 for (index = NO_IID_STEPS - 1; pan_value > -ps_tables->pan_class[index]; index--) {
122 }
123 index = -index - 1;
124 } else {
125 for (index = NO_IID_STEPS - 1; pan_value < ps_tables->pan_class[index]; index--) {
126 }
127 index++;
128 }
129
130 b_zero_iid = 0;
131 }
132
133 if (gr == 0) {
134 aa_delta_iid_f[gr] = index;
135 no_bits_t = 0;
136
137 no_bits_f = ps_tables->a_book_ps_iid_freq_length[index + CODE_BCK_LAV_IID];
138 } else {
139 aa_delta_iid_f[gr] = index - last_index;
140
141 no_bits_f += ps_tables->a_book_ps_iid_freq_length[aa_delta_iid_f[gr] + CODE_BCK_LAV_IID];
142 }
143
144 last_index = index;
145
146 aa_delta_iid_t[gr] = index - pstr_ps_handle->a_last_iid_index[gr];
147
148 pstr_ps_handle->a_last_iid_index[gr] = index;
149
150 no_bits_t += ps_tables->a_book_ps_iid_time_length[aa_delta_iid_t[gr] + CODE_BCK_LAV_IID];
151
152 if (aa_delta_iid_t[gr] != 0) {
153 b_keep_params = 0;
154 }
155 }
156
157 if (no_bits_t < no_bits_f && !b_header_active) {
158 ab_dt_flag_iid = 1;
159 aa_delta_iid = aa_delta_iid_t;
160 aa_huff_book_iid_c = ps_tables->a_book_ps_iid_time_code;
161 aa_huff_book_iid_l = ps_tables->a_book_ps_iid_time_length;
162 } else {
163 ab_dt_flag_iid = 0;
164 aa_delta_iid = aa_delta_iid_f;
165 aa_huff_book_iid_c = ps_tables->a_book_ps_iid_freq_code;
166 aa_huff_book_iid_l = ps_tables->a_book_ps_iid_freq_length;
167 }
168
169 last_index = 0;
170
171 for (gr = 0; gr < pstr_ps_handle->iid_icc_bins; gr++) {
172 WORD ICC_flag;
173 FLOAT32 sa_value = pstr_ps_handle->aaa_ICC_data_buf[gr][SYSTEMLOOKAHEAD];
174 ICC_flag = 0;
175 if (sa_value <= ps_tables->sa_class[0]) {
176 ICC_flag = 1;
177 }
178 if (ICC_flag) {
179 index = 0;
180 } else {
181 for (index = NO_ICC_STEPS - 2; sa_value < ps_tables->sa_class[index]; index--) {
182 }
183 index++;
184
185 b_zero_icc = 0;
186 }
187
188 if (gr == 0) {
189 aa_delta_icc_f[gr] = index;
190
191 no_bits_f = ps_tables->a_book_ps_icc_freq_length[index + CODE_BCK_LAV_ICC];
192
193 no_bits_t = 0;
194 } else {
195 aa_delta_icc_f[gr] = index - last_index;
196
197 no_bits_f += ps_tables->a_book_ps_icc_freq_length[aa_delta_icc_f[gr] + CODE_BCK_LAV_ICC];
198 }
199
200 last_index = index;
201
202 aa_delta_icc_t[gr] = index - pstr_ps_handle->a_last_icc_index[gr];
203
204 pstr_ps_handle->a_last_icc_index[gr] = index;
205
206 no_bits_t += ps_tables->a_book_ps_icc_time_length[aa_delta_icc_t[gr] + CODE_BCK_LAV_ICC];
207
208 if (aa_delta_icc_t[gr] != 0) {
209 b_keep_params = 0;
210 }
211 }
212
213 if (no_bits_t < no_bits_f && !b_header_active) {
214 ab_dt_flag_icc = 1;
215 aa_delta_icc = aa_delta_icc_t;
216 aa_huff_book_icc_c = ps_tables->a_book_ps_icc_time_code;
217 aa_huff_book_icc_l = ps_tables->a_book_ps_icc_time_length;
218 } else {
219 ab_dt_flag_icc = 0;
220 aa_delta_icc = aa_delta_icc_f;
221 aa_huff_book_icc_c = ps_tables->a_book_ps_icc_freq_code;
222 aa_huff_book_icc_l = ps_tables->a_book_ps_icc_freq_length;
223 }
224
225 {
226 static WORD32 initheader = 0;
227
228 if (!initheader || b_header_active) {
229 initheader = 1;
230 pstr_ps_handle->b_enable_header = 1;
231 } else {
232 pstr_ps_handle->b_enable_header = 0;
233 }
234 }
235
236 b_send_header = pstr_ps_handle->b_enable_header ||
237 pstr_ps_handle->b_prev_zero_iid != b_zero_iid ||
238 pstr_ps_handle->b_prev_zero_icc != b_zero_icc;
239
240 ixheaace_write_bits(bb, b_send_header, 1);
241
242 if (b_send_header) {
243 ixheaace_write_bits(bb, !b_zero_iid, 1);
244
245 if (!b_zero_iid) {
246 ixheaace_write_bits(bb, (pstr_ps_handle->b_hi_freq_res_iid_icc) ? 1 : 0, 3);
247 }
248
249 ixheaace_write_bits(bb, !b_zero_icc, 1);
250
251 if (!b_zero_icc) {
252 ixheaace_write_bits(bb, (pstr_ps_handle->b_hi_freq_res_iid_icc) ? 1 : 0, 3);
253 }
254
255 ixheaace_write_bits(bb, 0, 1);
256 }
257
258 ixheaace_write_bits(bb, 0, 1);
259
260 ixheaace_write_bits(bb, 1 - b_keep_params, 2);
261
262 if (!b_keep_params) {
263 if (!b_zero_iid) {
264 ixheaace_write_bits(bb, ab_dt_flag_iid, 1);
265
266 for (gr = 0; gr < pstr_ps_handle->iid_icc_bins; gr++) {
267 ixheaace_write_bits(bb, aa_huff_book_iid_c[aa_delta_iid[gr] + CODE_BCK_LAV_IID],
268 aa_huff_book_iid_l[aa_delta_iid[gr] + CODE_BCK_LAV_IID]);
269 }
270 }
271 }
272
273 if (!b_keep_params) {
274 if (!b_zero_icc) {
275 ixheaace_write_bits(bb, ab_dt_flag_icc, 1);
276
277 for (gr = 0; gr < pstr_ps_handle->iid_icc_bins; gr++) {
278 ixheaace_write_bits(bb, aa_huff_book_icc_c[aa_delta_icc[gr] + CODE_BCK_LAV_ICC],
279 aa_huff_book_icc_l[aa_delta_icc[gr] + CODE_BCK_LAV_ICC]);
280 }
281 }
282 }
283
284 pstr_ps_handle->b_prev_zero_iid = b_zero_iid;
285 pstr_ps_handle->b_prev_zero_icc = b_zero_icc;
286
287 return ia_enhaacplus_enc_get_bits_available(bb);
288 }
289
290 WORD32
ixheaace_append_ps_bitstream(ixheaace_pstr_ps_enc pstr_ps_handle,ixheaace_bit_buf_handle hdl_bs,WORD32 * sbr_hdr_bits)291 ixheaace_append_ps_bitstream(ixheaace_pstr_ps_enc pstr_ps_handle, ixheaace_bit_buf_handle hdl_bs,
292 WORD32 *sbr_hdr_bits) {
293 if (!pstr_ps_handle) {
294 return 0;
295 }
296 if (!hdl_bs) {
297 return ia_enhaacplus_enc_get_bits_available(&pstr_ps_handle->ps_bit_buf);
298 } else {
299 UWORD8 num_bits = (UWORD8)ia_enhaacplus_enc_get_bits_available(&pstr_ps_handle->ps_bit_buf);
300
301 ixheaace_write_bits(hdl_bs, EXTENSION_ID_PS_CODING, SI_SBR_EXTENSION_ID_BITS);
302 iexheaax_append_bitstream(hdl_bs, &pstr_ps_handle->ps_bit_buf, num_bits);
303
304 pstr_ps_handle->bit_buf_read_offset = (WORD32)(pstr_ps_handle->ps_bit_buf.ptr_read_next -
305 pstr_ps_handle->ps_bit_buf.ptr_bit_buf_base);
306 pstr_ps_handle->bit_buf_write_offset = (WORD32)(pstr_ps_handle->ps_bit_buf.ptr_write_next -
307 pstr_ps_handle->ps_bit_buf.ptr_bit_buf_base);
308
309 return ia_enhaacplus_enc_get_bits_available(hdl_bs) - (*sbr_hdr_bits) -
310 SI_FILL_EXTENTION_BITS;
311 }
312 }
313