xref: /aosp_15_r20/external/libxaac/encoder/ixheaace_mps_bitstream.c (revision 15dc779a375ca8b5125643b829a8aa4b70d7f451)
1*15dc779aSAndroid Build Coastguard Worker /******************************************************************************
2*15dc779aSAndroid Build Coastguard Worker  *                                                                            *
3*15dc779aSAndroid Build Coastguard Worker  * Copyright (C) 2023 The Android Open Source Project
4*15dc779aSAndroid Build Coastguard Worker  *
5*15dc779aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
6*15dc779aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
7*15dc779aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at:
8*15dc779aSAndroid Build Coastguard Worker  *
9*15dc779aSAndroid Build Coastguard Worker  * http://www.apache.org/licenses/LICENSE-2.0
10*15dc779aSAndroid Build Coastguard Worker  *
11*15dc779aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
12*15dc779aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
13*15dc779aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*15dc779aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
15*15dc779aSAndroid Build Coastguard Worker  * limitations under the License.
16*15dc779aSAndroid Build Coastguard Worker  *
17*15dc779aSAndroid Build Coastguard Worker  *****************************************************************************
18*15dc779aSAndroid Build Coastguard Worker  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19*15dc779aSAndroid Build Coastguard Worker  */
20*15dc779aSAndroid Build Coastguard Worker 
21*15dc779aSAndroid Build Coastguard Worker #include <stdlib.h>
22*15dc779aSAndroid Build Coastguard Worker #include <math.h>
23*15dc779aSAndroid Build Coastguard Worker #include "ixheaac_type_def.h"
24*15dc779aSAndroid Build Coastguard Worker #include "impd_drc_common_enc.h"
25*15dc779aSAndroid Build Coastguard Worker #include "impd_drc_uni_drc.h"
26*15dc779aSAndroid Build Coastguard Worker #include "impd_drc_tables.h"
27*15dc779aSAndroid Build Coastguard Worker #include "impd_drc_api.h"
28*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_api.h"
29*15dc779aSAndroid Build Coastguard Worker #include "ixheaac_error_standards.h"
30*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_error_codes.h"
31*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_mps_common_fix.h"
32*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_mps_defines.h"
33*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_mps_common_define.h"
34*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_bitbuffer.h"
35*15dc779aSAndroid Build Coastguard Worker 
36*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_mps_nlc_enc.h"
37*15dc779aSAndroid Build Coastguard Worker #include "ixheaac_constants.h"
38*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_aac_constants.h"
39*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_common_rom.h"
40*15dc779aSAndroid Build Coastguard Worker 
41*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_mps_struct_def.h"
42*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_mps_sac_polyphase.h"
43*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_mps_sac_nlc_enc.h"
44*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_mps_sac_hybfilter.h"
45*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_mps_bitstream.h"
46*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_mps_spatial_bitstream.h"
47*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_mps_param_extract.h"
48*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_mps_tree.h"
49*15dc779aSAndroid Build Coastguard Worker #include "ixheaace_mps_rom.h"
50*15dc779aSAndroid Build Coastguard Worker 
ixheaace_mps_212_get_bs_freq_res_stride(const WORD32 index)51*15dc779aSAndroid Build Coastguard Worker static UWORD8 ixheaace_mps_212_get_bs_freq_res_stride(const WORD32 index) {
52*15dc779aSAndroid Build Coastguard Worker   WORD32 freq_res_stride_table_size = 0;
53*15dc779aSAndroid Build Coastguard Worker   const UWORD8 *ptr_freq_res_stride_table = NULL;
54*15dc779aSAndroid Build Coastguard Worker 
55*15dc779aSAndroid Build Coastguard Worker   ptr_freq_res_stride_table = freq_res_stride_table_212;
56*15dc779aSAndroid Build Coastguard Worker   freq_res_stride_table_size =
57*15dc779aSAndroid Build Coastguard Worker       sizeof(freq_res_stride_table_212) / sizeof(*freq_res_stride_table_212);
58*15dc779aSAndroid Build Coastguard Worker 
59*15dc779aSAndroid Build Coastguard Worker   return (((index >= 0) && (index < freq_res_stride_table_size))
60*15dc779aSAndroid Build Coastguard Worker               ? ptr_freq_res_stride_table[index]
61*15dc779aSAndroid Build Coastguard Worker               : 1);
62*15dc779aSAndroid Build Coastguard Worker }
63*15dc779aSAndroid Build Coastguard Worker 
ixheaace_mps_212_get_bs_freq_res_index(const WORD32 num_bands,WORD32 * const ptr_bs_freq_res_index,WORD32 aot)64*15dc779aSAndroid Build Coastguard Worker static IA_ERRORCODE ixheaace_mps_212_get_bs_freq_res_index(const WORD32 num_bands,
65*15dc779aSAndroid Build Coastguard Worker                                                            WORD32 *const ptr_bs_freq_res_index,
66*15dc779aSAndroid Build Coastguard Worker                                                            WORD32 aot) {
67*15dc779aSAndroid Build Coastguard Worker   IA_ERRORCODE error = IA_NO_ERROR;
68*15dc779aSAndroid Build Coastguard Worker   WORD32 idx;
69*15dc779aSAndroid Build Coastguard Worker   const UWORD8 *p_freq_res_bin_table;
70*15dc779aSAndroid Build Coastguard Worker   *ptr_bs_freq_res_index = -1;
71*15dc779aSAndroid Build Coastguard Worker 
72*15dc779aSAndroid Build Coastguard Worker   if (aot == AOT_AAC_ELD) {
73*15dc779aSAndroid Build Coastguard Worker     p_freq_res_bin_table = freq_res_bin_table_ld;
74*15dc779aSAndroid Build Coastguard Worker   } else {
75*15dc779aSAndroid Build Coastguard Worker     p_freq_res_bin_table = freq_res_bin_table_usac;
76*15dc779aSAndroid Build Coastguard Worker   }
77*15dc779aSAndroid Build Coastguard Worker   for (idx = 0; idx < MAX_FREQ_RES_INDEX; idx++) {
78*15dc779aSAndroid Build Coastguard Worker     if (num_bands == p_freq_res_bin_table[idx]) {
79*15dc779aSAndroid Build Coastguard Worker       *ptr_bs_freq_res_index = idx;
80*15dc779aSAndroid Build Coastguard Worker       break;
81*15dc779aSAndroid Build Coastguard Worker     }
82*15dc779aSAndroid Build Coastguard Worker   }
83*15dc779aSAndroid Build Coastguard Worker   if (*ptr_bs_freq_res_index < 0 || *ptr_bs_freq_res_index >= MAX_FREQ_RES_INDEX) {
84*15dc779aSAndroid Build Coastguard Worker     return IA_EXHEAACE_CONFIG_NONFATAL_MPS_INVALID_CONFIG;
85*15dc779aSAndroid Build Coastguard Worker   }
86*15dc779aSAndroid Build Coastguard Worker   return error;
87*15dc779aSAndroid Build Coastguard Worker }
88*15dc779aSAndroid Build Coastguard Worker 
ixheaace_mps_212_get_sampling_frequency_index(const UWORD32 bs_sampling_frequency,WORD32 * const ptr_bs_sampling_frequency_index)89*15dc779aSAndroid Build Coastguard Worker static VOID ixheaace_mps_212_get_sampling_frequency_index(
90*15dc779aSAndroid Build Coastguard Worker     const UWORD32 bs_sampling_frequency, WORD32 *const ptr_bs_sampling_frequency_index) {
91*15dc779aSAndroid Build Coastguard Worker   WORD32 idx;
92*15dc779aSAndroid Build Coastguard Worker   *ptr_bs_sampling_frequency_index = SAMPLING_FREQUENCY_INDEX_ESCAPE;
93*15dc779aSAndroid Build Coastguard Worker 
94*15dc779aSAndroid Build Coastguard Worker   for (idx = 0; idx < MAX_SAMPLING_FREQUENCY_INDEX; idx++) {
95*15dc779aSAndroid Build Coastguard Worker     if (bs_sampling_frequency == ia_sampl_freq_table[idx]) {
96*15dc779aSAndroid Build Coastguard Worker       *ptr_bs_sampling_frequency_index = idx;
97*15dc779aSAndroid Build Coastguard Worker       break;
98*15dc779aSAndroid Build Coastguard Worker     }
99*15dc779aSAndroid Build Coastguard Worker   }
100*15dc779aSAndroid Build Coastguard Worker }
101*15dc779aSAndroid Build Coastguard Worker 
ixheaace_mps_212_ec_data(ixheaace_bit_buf_handle pstr_bit_buf,WORD8 data[MAX_NUM_PARAMS][MAX_NUM_BINS],WORD8 old_data[MAX_NUM_BINS],UWORD8 quant_coarse_xxx_prev[MAX_NUM_PARAMS],ixheaace_mps_lossless_data * const pstr_lossless_data,const WORD32 data_type,const WORD32 param_idx,const WORD32 num_param_sets,const WORD32 independency_flag,const WORD32 start_band,const WORD32 stop_band,const WORD32 default_value)102*15dc779aSAndroid Build Coastguard Worker static IA_ERRORCODE ixheaace_mps_212_ec_data(
103*15dc779aSAndroid Build Coastguard Worker     ixheaace_bit_buf_handle pstr_bit_buf, WORD8 data[MAX_NUM_PARAMS][MAX_NUM_BINS],
104*15dc779aSAndroid Build Coastguard Worker     WORD8 old_data[MAX_NUM_BINS], UWORD8 quant_coarse_xxx_prev[MAX_NUM_PARAMS],
105*15dc779aSAndroid Build Coastguard Worker     ixheaace_mps_lossless_data *const pstr_lossless_data, const WORD32 data_type,
106*15dc779aSAndroid Build Coastguard Worker     const WORD32 param_idx, const WORD32 num_param_sets, const WORD32 independency_flag,
107*15dc779aSAndroid Build Coastguard Worker     const WORD32 start_band, const WORD32 stop_band, const WORD32 default_value) {
108*15dc779aSAndroid Build Coastguard Worker   IA_ERRORCODE error;
109*15dc779aSAndroid Build Coastguard Worker   WORD32 ps, pb, str_offset, pb_stride, i;
110*15dc779aSAndroid Build Coastguard Worker   WORD16 data_bands;
111*15dc779aSAndroid Build Coastguard Worker   WORD32 a_strides[MAX_NUM_BINS + 1] = {0};
112*15dc779aSAndroid Build Coastguard Worker   WORD16 cmp_idx_data[2][MAX_NUM_BINS] = {{0}};
113*15dc779aSAndroid Build Coastguard Worker   WORD16 cmp_old_data[MAX_NUM_BINS] = {0};
114*15dc779aSAndroid Build Coastguard Worker 
115*15dc779aSAndroid Build Coastguard Worker   if (num_param_sets > MAX_NUM_PARAMS) {
116*15dc779aSAndroid Build Coastguard Worker     return IA_EXHEAACE_EXE_FATAL_MPS_INVALID_NUM_PARAM_SETS;
117*15dc779aSAndroid Build Coastguard Worker   }
118*15dc779aSAndroid Build Coastguard Worker 
119*15dc779aSAndroid Build Coastguard Worker   if (independency_flag || (pstr_lossless_data->bs_quant_coarse_xxx[param_idx][0] !=
120*15dc779aSAndroid Build Coastguard Worker                             quant_coarse_xxx_prev[param_idx])) {
121*15dc779aSAndroid Build Coastguard Worker     pstr_lossless_data->bs_xxx_data_mode[param_idx][0] = IXHEAACE_MPS_DATA_MODE_FINECOARSE;
122*15dc779aSAndroid Build Coastguard Worker   } else {
123*15dc779aSAndroid Build Coastguard Worker     pstr_lossless_data->bs_xxx_data_mode[param_idx][0] = IXHEAACE_MPS_DATA_MODE_KEEP;
124*15dc779aSAndroid Build Coastguard Worker     for (i = start_band; i < stop_band; i++) {
125*15dc779aSAndroid Build Coastguard Worker       if (data[0][i] != old_data[i]) {
126*15dc779aSAndroid Build Coastguard Worker         pstr_lossless_data->bs_xxx_data_mode[param_idx][0] = IXHEAACE_MPS_DATA_MODE_FINECOARSE;
127*15dc779aSAndroid Build Coastguard Worker         break;
128*15dc779aSAndroid Build Coastguard Worker       }
129*15dc779aSAndroid Build Coastguard Worker     }
130*15dc779aSAndroid Build Coastguard Worker   }
131*15dc779aSAndroid Build Coastguard Worker 
132*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_lossless_data->bs_xxx_data_mode[param_idx][0], 2);
133*15dc779aSAndroid Build Coastguard Worker 
134*15dc779aSAndroid Build Coastguard Worker   for (ps = 1; ps < num_param_sets; ps++) {
135*15dc779aSAndroid Build Coastguard Worker     if (pstr_lossless_data->bs_quant_coarse_xxx[param_idx][ps] !=
136*15dc779aSAndroid Build Coastguard Worker         pstr_lossless_data->bs_quant_coarse_xxx[param_idx][ps - 1]) {
137*15dc779aSAndroid Build Coastguard Worker       pstr_lossless_data->bs_xxx_data_mode[param_idx][ps] = IXHEAACE_MPS_DATA_MODE_FINECOARSE;
138*15dc779aSAndroid Build Coastguard Worker     } else {
139*15dc779aSAndroid Build Coastguard Worker       pstr_lossless_data->bs_xxx_data_mode[param_idx][ps] = IXHEAACE_MPS_DATA_MODE_KEEP;
140*15dc779aSAndroid Build Coastguard Worker       for (i = start_band; i < stop_band; i++) {
141*15dc779aSAndroid Build Coastguard Worker         if (data[ps][i] != data[ps - 1][i]) {
142*15dc779aSAndroid Build Coastguard Worker           pstr_lossless_data->bs_xxx_data_mode[param_idx][ps] = IXHEAACE_MPS_DATA_MODE_FINECOARSE;
143*15dc779aSAndroid Build Coastguard Worker           break;
144*15dc779aSAndroid Build Coastguard Worker         }
145*15dc779aSAndroid Build Coastguard Worker       }
146*15dc779aSAndroid Build Coastguard Worker     }
147*15dc779aSAndroid Build Coastguard Worker 
148*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_lossless_data->bs_xxx_data_mode[param_idx][ps], 2);
149*15dc779aSAndroid Build Coastguard Worker   }
150*15dc779aSAndroid Build Coastguard Worker 
151*15dc779aSAndroid Build Coastguard Worker   for (ps = 0; ps < (num_param_sets - 1); ps++) {
152*15dc779aSAndroid Build Coastguard Worker     if (pstr_lossless_data->bs_xxx_data_mode[param_idx][ps] ==
153*15dc779aSAndroid Build Coastguard Worker         IXHEAACE_MPS_DATA_MODE_FINECOARSE) {
154*15dc779aSAndroid Build Coastguard Worker       if (pstr_lossless_data->bs_xxx_data_mode[param_idx][ps + 1] ==
155*15dc779aSAndroid Build Coastguard Worker           IXHEAACE_MPS_DATA_MODE_FINECOARSE) {
156*15dc779aSAndroid Build Coastguard Worker         if ((pstr_lossless_data->bs_quant_coarse_xxx[param_idx][ps + 1] ==
157*15dc779aSAndroid Build Coastguard Worker              pstr_lossless_data->bs_quant_coarse_xxx[param_idx][ps]) &&
158*15dc779aSAndroid Build Coastguard Worker             (pstr_lossless_data->bs_freq_res_stride_xxx[param_idx][ps + 1] ==
159*15dc779aSAndroid Build Coastguard Worker              pstr_lossless_data->bs_freq_res_stride_xxx[param_idx][ps])) {
160*15dc779aSAndroid Build Coastguard Worker           pstr_lossless_data->bs_data_pair[param_idx][ps] = 1;
161*15dc779aSAndroid Build Coastguard Worker           pstr_lossless_data->bs_data_pair[param_idx][ps + 1] = 1;
162*15dc779aSAndroid Build Coastguard Worker           ps++;
163*15dc779aSAndroid Build Coastguard Worker           continue;
164*15dc779aSAndroid Build Coastguard Worker         }
165*15dc779aSAndroid Build Coastguard Worker       }
166*15dc779aSAndroid Build Coastguard Worker 
167*15dc779aSAndroid Build Coastguard Worker       pstr_lossless_data->bs_data_pair[param_idx][ps] = 0;
168*15dc779aSAndroid Build Coastguard Worker 
169*15dc779aSAndroid Build Coastguard Worker       pstr_lossless_data->bs_data_pair[param_idx][ps + 1] = 0;
170*15dc779aSAndroid Build Coastguard Worker     } else {
171*15dc779aSAndroid Build Coastguard Worker       pstr_lossless_data->bs_data_pair[param_idx][ps] = 0;
172*15dc779aSAndroid Build Coastguard Worker       pstr_lossless_data->bs_data_pair[param_idx][ps + 1] = 0;
173*15dc779aSAndroid Build Coastguard Worker     }
174*15dc779aSAndroid Build Coastguard Worker   }
175*15dc779aSAndroid Build Coastguard Worker 
176*15dc779aSAndroid Build Coastguard Worker   for (ps = 0; ps < num_param_sets; ps++) {
177*15dc779aSAndroid Build Coastguard Worker     if (pstr_lossless_data->bs_xxx_data_mode[param_idx][ps] == IXHEAACE_MPS_DATA_MODE_DEFAULT) {
178*15dc779aSAndroid Build Coastguard Worker       for (i = start_band; i < stop_band; i++) {
179*15dc779aSAndroid Build Coastguard Worker         old_data[i] = (WORD8)default_value;
180*15dc779aSAndroid Build Coastguard Worker       }
181*15dc779aSAndroid Build Coastguard Worker       quant_coarse_xxx_prev[param_idx] = 0;
182*15dc779aSAndroid Build Coastguard Worker     }
183*15dc779aSAndroid Build Coastguard Worker 
184*15dc779aSAndroid Build Coastguard Worker     if (pstr_lossless_data->bs_xxx_data_mode[param_idx][ps] ==
185*15dc779aSAndroid Build Coastguard Worker         IXHEAACE_MPS_DATA_MODE_FINECOARSE) {
186*15dc779aSAndroid Build Coastguard Worker       ixheaace_write_bits(pstr_bit_buf, pstr_lossless_data->bs_data_pair[param_idx][ps], 1);
187*15dc779aSAndroid Build Coastguard Worker       ixheaace_write_bits(pstr_bit_buf, pstr_lossless_data->bs_quant_coarse_xxx[param_idx][ps],
188*15dc779aSAndroid Build Coastguard Worker                           1);
189*15dc779aSAndroid Build Coastguard Worker       ixheaace_write_bits(pstr_bit_buf, pstr_lossless_data->bs_freq_res_stride_xxx[param_idx][ps],
190*15dc779aSAndroid Build Coastguard Worker                           2);
191*15dc779aSAndroid Build Coastguard Worker 
192*15dc779aSAndroid Build Coastguard Worker       if (pstr_lossless_data->bs_quant_coarse_xxx[param_idx][ps] !=
193*15dc779aSAndroid Build Coastguard Worker           quant_coarse_xxx_prev[param_idx]) {
194*15dc779aSAndroid Build Coastguard Worker         if (quant_coarse_xxx_prev[param_idx]) {
195*15dc779aSAndroid Build Coastguard Worker           for (i = start_band; i < stop_band; i++) {
196*15dc779aSAndroid Build Coastguard Worker             old_data[i] *= 2;
197*15dc779aSAndroid Build Coastguard Worker           }
198*15dc779aSAndroid Build Coastguard Worker 
199*15dc779aSAndroid Build Coastguard Worker           if (data_type == IXHEAACE_MPS_SAC_DATA_TYPE_CLD) {
200*15dc779aSAndroid Build Coastguard Worker             for (i = start_band; i < stop_band; i++) {
201*15dc779aSAndroid Build Coastguard Worker               if (old_data[i] == -14) {
202*15dc779aSAndroid Build Coastguard Worker                 old_data[i] = -15;
203*15dc779aSAndroid Build Coastguard Worker               } else if (old_data[i] == 14) {
204*15dc779aSAndroid Build Coastguard Worker                 old_data[i] = 15;
205*15dc779aSAndroid Build Coastguard Worker               }
206*15dc779aSAndroid Build Coastguard Worker             }
207*15dc779aSAndroid Build Coastguard Worker           }
208*15dc779aSAndroid Build Coastguard Worker         } else {
209*15dc779aSAndroid Build Coastguard Worker           for (i = start_band; i < stop_band; i++) {
210*15dc779aSAndroid Build Coastguard Worker             old_data[i] /= 2;
211*15dc779aSAndroid Build Coastguard Worker           }
212*15dc779aSAndroid Build Coastguard Worker         }
213*15dc779aSAndroid Build Coastguard Worker       }
214*15dc779aSAndroid Build Coastguard Worker 
215*15dc779aSAndroid Build Coastguard Worker       pb_stride = ixheaace_mps_212_get_bs_freq_res_stride(
216*15dc779aSAndroid Build Coastguard Worker           pstr_lossless_data->bs_freq_res_stride_xxx[param_idx][ps]);
217*15dc779aSAndroid Build Coastguard Worker       data_bands = (WORD16)((stop_band - start_band - 1) / pb_stride + 1);
218*15dc779aSAndroid Build Coastguard Worker 
219*15dc779aSAndroid Build Coastguard Worker       a_strides[0] = start_band;
220*15dc779aSAndroid Build Coastguard Worker       for (pb = 1; pb <= data_bands; pb++) {
221*15dc779aSAndroid Build Coastguard Worker         a_strides[pb] = a_strides[pb - 1] + pb_stride;
222*15dc779aSAndroid Build Coastguard Worker       }
223*15dc779aSAndroid Build Coastguard Worker 
224*15dc779aSAndroid Build Coastguard Worker       str_offset = 0;
225*15dc779aSAndroid Build Coastguard Worker       while (a_strides[data_bands] > stop_band) {
226*15dc779aSAndroid Build Coastguard Worker         if (str_offset < data_bands) {
227*15dc779aSAndroid Build Coastguard Worker           str_offset++;
228*15dc779aSAndroid Build Coastguard Worker         }
229*15dc779aSAndroid Build Coastguard Worker         for (i = str_offset; i <= data_bands; i++) {
230*15dc779aSAndroid Build Coastguard Worker           a_strides[i]--;
231*15dc779aSAndroid Build Coastguard Worker         }
232*15dc779aSAndroid Build Coastguard Worker       }
233*15dc779aSAndroid Build Coastguard Worker 
234*15dc779aSAndroid Build Coastguard Worker       for (pb = 0; pb < data_bands; pb++) {
235*15dc779aSAndroid Build Coastguard Worker         cmp_old_data[start_band + pb] = old_data[a_strides[pb]];
236*15dc779aSAndroid Build Coastguard Worker         cmp_idx_data[0][start_band + pb] = data[ps][a_strides[pb]];
237*15dc779aSAndroid Build Coastguard Worker 
238*15dc779aSAndroid Build Coastguard Worker         if (pstr_lossless_data->bs_data_pair[param_idx][ps]) {
239*15dc779aSAndroid Build Coastguard Worker           cmp_idx_data[1][start_band + pb] = data[ps + 1][a_strides[pb]];
240*15dc779aSAndroid Build Coastguard Worker         }
241*15dc779aSAndroid Build Coastguard Worker       }
242*15dc779aSAndroid Build Coastguard Worker 
243*15dc779aSAndroid Build Coastguard Worker       if (pstr_lossless_data->bs_data_pair[param_idx][ps]) {
244*15dc779aSAndroid Build Coastguard Worker         error = ixheaace_mps_212_ec_data_pair_enc(
245*15dc779aSAndroid Build Coastguard Worker             pstr_bit_buf, cmp_idx_data, cmp_old_data, data_type, 0, start_band, data_bands,
246*15dc779aSAndroid Build Coastguard Worker             pstr_lossless_data->bs_quant_coarse_xxx[param_idx][ps],
247*15dc779aSAndroid Build Coastguard Worker             independency_flag && (ps == 0));
248*15dc779aSAndroid Build Coastguard Worker         if (error != IA_NO_ERROR) {
249*15dc779aSAndroid Build Coastguard Worker           return error;
250*15dc779aSAndroid Build Coastguard Worker         }
251*15dc779aSAndroid Build Coastguard Worker       } else {
252*15dc779aSAndroid Build Coastguard Worker         error = ixheaace_mps_212_ec_data_single_enc(
253*15dc779aSAndroid Build Coastguard Worker             pstr_bit_buf, cmp_idx_data, cmp_old_data, data_type, 0, start_band, data_bands,
254*15dc779aSAndroid Build Coastguard Worker             pstr_lossless_data->bs_quant_coarse_xxx[param_idx][ps],
255*15dc779aSAndroid Build Coastguard Worker             independency_flag && (ps == 0));
256*15dc779aSAndroid Build Coastguard Worker         if (error != IA_NO_ERROR) {
257*15dc779aSAndroid Build Coastguard Worker           return error;
258*15dc779aSAndroid Build Coastguard Worker         }
259*15dc779aSAndroid Build Coastguard Worker       }
260*15dc779aSAndroid Build Coastguard Worker       for (i = start_band; i < stop_band; i++) {
261*15dc779aSAndroid Build Coastguard Worker         if (pstr_lossless_data->bs_data_pair[param_idx][ps]) {
262*15dc779aSAndroid Build Coastguard Worker           old_data[i] = data[ps + 1][i];
263*15dc779aSAndroid Build Coastguard Worker         } else {
264*15dc779aSAndroid Build Coastguard Worker           old_data[i] = data[ps][i];
265*15dc779aSAndroid Build Coastguard Worker         }
266*15dc779aSAndroid Build Coastguard Worker       }
267*15dc779aSAndroid Build Coastguard Worker 
268*15dc779aSAndroid Build Coastguard Worker       quant_coarse_xxx_prev[param_idx] = pstr_lossless_data->bs_quant_coarse_xxx[param_idx][ps];
269*15dc779aSAndroid Build Coastguard Worker 
270*15dc779aSAndroid Build Coastguard Worker       if (pstr_lossless_data->bs_data_pair[param_idx][ps]) {
271*15dc779aSAndroid Build Coastguard Worker         ps++;
272*15dc779aSAndroid Build Coastguard Worker       }
273*15dc779aSAndroid Build Coastguard Worker     }
274*15dc779aSAndroid Build Coastguard Worker   }
275*15dc779aSAndroid Build Coastguard Worker   return IA_NO_ERROR;
276*15dc779aSAndroid Build Coastguard Worker }
277*15dc779aSAndroid Build Coastguard Worker 
ixheaace_mps_212_write_smg_data(ixheaace_bit_buf_handle pstr_bit_buf,const ixheaace_mps_smg_data * const pstr_smg_data,const WORD32 num_param_sets,const WORD32 data_bands)278*15dc779aSAndroid Build Coastguard Worker static VOID ixheaace_mps_212_write_smg_data(ixheaace_bit_buf_handle pstr_bit_buf,
279*15dc779aSAndroid Build Coastguard Worker                                             const ixheaace_mps_smg_data *const pstr_smg_data,
280*15dc779aSAndroid Build Coastguard Worker                                             const WORD32 num_param_sets,
281*15dc779aSAndroid Build Coastguard Worker                                             const WORD32 data_bands) {
282*15dc779aSAndroid Build Coastguard Worker   WORD32 param, band;
283*15dc779aSAndroid Build Coastguard Worker 
284*15dc779aSAndroid Build Coastguard Worker   for (param = 0; param < num_param_sets; param++) {
285*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_smg_data->bs_smooth_mode[param], 2);
286*15dc779aSAndroid Build Coastguard Worker     if (pstr_smg_data->bs_smooth_mode[param] >= 2) {
287*15dc779aSAndroid Build Coastguard Worker       ixheaace_write_bits(pstr_bit_buf, pstr_smg_data->bs_smooth_time[param], 2);
288*15dc779aSAndroid Build Coastguard Worker     }
289*15dc779aSAndroid Build Coastguard Worker     if (pstr_smg_data->bs_smooth_mode[param] == 3) {
290*15dc779aSAndroid Build Coastguard Worker       WORD32 stride =
291*15dc779aSAndroid Build Coastguard Worker           ixheaace_mps_212_get_bs_freq_res_stride(pstr_smg_data->bs_freq_res_stride[param]);
292*15dc779aSAndroid Build Coastguard Worker       ixheaace_write_bits(pstr_bit_buf, pstr_smg_data->bs_freq_res_stride[param], 2);
293*15dc779aSAndroid Build Coastguard Worker       for (band = 0; band < data_bands; band += stride) {
294*15dc779aSAndroid Build Coastguard Worker         ixheaace_write_bits(pstr_bit_buf, pstr_smg_data->bs_smg_data[param][band], 1);
295*15dc779aSAndroid Build Coastguard Worker       }
296*15dc779aSAndroid Build Coastguard Worker     }
297*15dc779aSAndroid Build Coastguard Worker   }
298*15dc779aSAndroid Build Coastguard Worker }
299*15dc779aSAndroid Build Coastguard Worker 
ixheaace_mps_212_write_ott_data(ixheaace_bit_buf_handle pstr_bit_buf,ixheaace_mps_prev_ott_data * const pstr_prev_ott_data,ixheaace_mps_ott_data * const pstr_ott_data,const ixheaace_mps_ott_config ott_config[IXHEAACE_MPS_MAX_NUM_BOXES],ixheaace_mps_lossless_data * const pstr_cld_lossless_data,ixheaace_mps_lossless_data * const pstr_icc_lossless_data,const WORD32 num_ott_boxes,const WORD32 num_bands,const WORD32 num_param_sets,const WORD32 bs_independency_flag)300*15dc779aSAndroid Build Coastguard Worker static IA_ERRORCODE ixheaace_mps_212_write_ott_data(
301*15dc779aSAndroid Build Coastguard Worker     ixheaace_bit_buf_handle pstr_bit_buf, ixheaace_mps_prev_ott_data *const pstr_prev_ott_data,
302*15dc779aSAndroid Build Coastguard Worker     ixheaace_mps_ott_data *const pstr_ott_data,
303*15dc779aSAndroid Build Coastguard Worker     const ixheaace_mps_ott_config ott_config[IXHEAACE_MPS_MAX_NUM_BOXES],
304*15dc779aSAndroid Build Coastguard Worker     ixheaace_mps_lossless_data *const pstr_cld_lossless_data,
305*15dc779aSAndroid Build Coastguard Worker     ixheaace_mps_lossless_data *const pstr_icc_lossless_data, const WORD32 num_ott_boxes,
306*15dc779aSAndroid Build Coastguard Worker     const WORD32 num_bands, const WORD32 num_param_sets, const WORD32 bs_independency_flag) {
307*15dc779aSAndroid Build Coastguard Worker   IA_ERRORCODE error = IA_NO_ERROR;
308*15dc779aSAndroid Build Coastguard Worker   WORD32 box;
309*15dc779aSAndroid Build Coastguard Worker   for (box = 0; box < num_ott_boxes; box++) {
310*15dc779aSAndroid Build Coastguard Worker     error = ixheaace_mps_212_ec_data(
311*15dc779aSAndroid Build Coastguard Worker         pstr_bit_buf, pstr_ott_data->cld[box], pstr_prev_ott_data->cld_old[box],
312*15dc779aSAndroid Build Coastguard Worker         pstr_prev_ott_data->quant_coarse_cld_prev[box], pstr_cld_lossless_data,
313*15dc779aSAndroid Build Coastguard Worker         IXHEAACE_MPS_SAC_DATA_TYPE_CLD, box, num_param_sets, bs_independency_flag, 0,
314*15dc779aSAndroid Build Coastguard Worker         ott_config[box].bs_ott_bands, 15);
315*15dc779aSAndroid Build Coastguard Worker     if (error != IA_NO_ERROR) {
316*15dc779aSAndroid Build Coastguard Worker       return error;
317*15dc779aSAndroid Build Coastguard Worker     }
318*15dc779aSAndroid Build Coastguard Worker   }
319*15dc779aSAndroid Build Coastguard Worker   for (box = 0; box < num_ott_boxes; box++) {
320*15dc779aSAndroid Build Coastguard Worker     error = ixheaace_mps_212_ec_data(pstr_bit_buf, pstr_ott_data->icc[box],
321*15dc779aSAndroid Build Coastguard Worker                                      pstr_prev_ott_data->icc_old[box],
322*15dc779aSAndroid Build Coastguard Worker                                      pstr_prev_ott_data->quant_coarse_icc_prev[box],
323*15dc779aSAndroid Build Coastguard Worker                                      pstr_icc_lossless_data, IXHEAACE_MPS_SAC_DATA_TYPE_ICC, box,
324*15dc779aSAndroid Build Coastguard Worker                                      num_param_sets, bs_independency_flag, 0, num_bands, 0);
325*15dc779aSAndroid Build Coastguard Worker     if (error != IA_NO_ERROR) {
326*15dc779aSAndroid Build Coastguard Worker       return error;
327*15dc779aSAndroid Build Coastguard Worker     }
328*15dc779aSAndroid Build Coastguard Worker   }
329*15dc779aSAndroid Build Coastguard Worker 
330*15dc779aSAndroid Build Coastguard Worker   return IA_NO_ERROR;
331*15dc779aSAndroid Build Coastguard Worker }
332*15dc779aSAndroid Build Coastguard Worker 
ixheaace_mps_212_write_framing_info(ixheaace_bit_buf_handle pstr_bit_buf,const ixheaace_mps_framing_info * const pstr_framing_info,const WORD32 frame_length)333*15dc779aSAndroid Build Coastguard Worker static VOID ixheaace_mps_212_write_framing_info(
334*15dc779aSAndroid Build Coastguard Worker     ixheaace_bit_buf_handle pstr_bit_buf,
335*15dc779aSAndroid Build Coastguard Worker     const ixheaace_mps_framing_info *const pstr_framing_info, const WORD32 frame_length) {
336*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_framing_info->bs_framing_type, 1);
337*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_framing_info->num_param_sets - 1, 1);
338*15dc779aSAndroid Build Coastguard Worker 
339*15dc779aSAndroid Build Coastguard Worker   if (pstr_framing_info->bs_framing_type) {
340*15dc779aSAndroid Build Coastguard Worker     WORD32 ps = 0;
341*15dc779aSAndroid Build Coastguard Worker     UWORD8 bits_param_slot = 0;
342*15dc779aSAndroid Build Coastguard Worker     WORD32 num_param_sets = pstr_framing_info->num_param_sets;
343*15dc779aSAndroid Build Coastguard Worker     while ((1 << bits_param_slot) < (frame_length + 1)) {
344*15dc779aSAndroid Build Coastguard Worker       bits_param_slot++;
345*15dc779aSAndroid Build Coastguard Worker     }
346*15dc779aSAndroid Build Coastguard Worker     if (bits_param_slot > 0) {
347*15dc779aSAndroid Build Coastguard Worker       for (ps = 0; ps < num_param_sets; ps++) {
348*15dc779aSAndroid Build Coastguard Worker         ixheaace_write_bits(pstr_bit_buf, pstr_framing_info->bs_param_slots[ps], bits_param_slot);
349*15dc779aSAndroid Build Coastguard Worker       }
350*15dc779aSAndroid Build Coastguard Worker     }
351*15dc779aSAndroid Build Coastguard Worker   }
352*15dc779aSAndroid Build Coastguard Worker }
353*15dc779aSAndroid Build Coastguard Worker 
ixheaace_mps_515_ec_data(ixheaace_bit_buf_handle pstr_bit_buf,WORD32 data[MAX_NUM_PARAMS][MAX_NUM_BINS],WORD32 old_data[MAX_NUM_BINS],ixheaace_mps_sac_lossless_data * pstr_lossless_data,WORD32 data_type,WORD32 param_idx,WORD32 num_param_sets,WORD32 independency_flag,WORD32 start_band,WORD32 stop_band)354*15dc779aSAndroid Build Coastguard Worker static IA_ERRORCODE ixheaace_mps_515_ec_data(ixheaace_bit_buf_handle pstr_bit_buf,
355*15dc779aSAndroid Build Coastguard Worker                                              WORD32 data[MAX_NUM_PARAMS][MAX_NUM_BINS],
356*15dc779aSAndroid Build Coastguard Worker                                              WORD32 old_data[MAX_NUM_BINS],
357*15dc779aSAndroid Build Coastguard Worker                                              ixheaace_mps_sac_lossless_data *pstr_lossless_data,
358*15dc779aSAndroid Build Coastguard Worker                                              WORD32 data_type, WORD32 param_idx,
359*15dc779aSAndroid Build Coastguard Worker                                              WORD32 num_param_sets, WORD32 independency_flag,
360*15dc779aSAndroid Build Coastguard Worker                                              WORD32 start_band, WORD32 stop_band) {
361*15dc779aSAndroid Build Coastguard Worker   WORD32 param_set, set_idx, pb_stride, data_bands, bin, data_sets;
362*15dc779aSAndroid Build Coastguard Worker   WORD32 param_set_index[MAX_NUM_PARAMS] = {0};
363*15dc779aSAndroid Build Coastguard Worker 
364*15dc779aSAndroid Build Coastguard Worker   for (param_set = 0; param_set < num_param_sets; param_set++) {
365*15dc779aSAndroid Build Coastguard Worker     pstr_lossless_data->bs_xxx_data_mode[param_idx][param_set] =
366*15dc779aSAndroid Build Coastguard Worker         IXHEAACE_MPS_DATA_MODE_FINECOARSE;
367*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_lossless_data->bs_xxx_data_mode[param_idx][param_set],
368*15dc779aSAndroid Build Coastguard Worker                         2);
369*15dc779aSAndroid Build Coastguard Worker   }
370*15dc779aSAndroid Build Coastguard Worker 
371*15dc779aSAndroid Build Coastguard Worker   for (param_set = 0, set_idx = 0; param_set < num_param_sets; param_set++) {
372*15dc779aSAndroid Build Coastguard Worker     param_set_index[set_idx] = param_set;
373*15dc779aSAndroid Build Coastguard Worker     if (param_set != num_param_sets - 1) {
374*15dc779aSAndroid Build Coastguard Worker       pstr_lossless_data->bs_data_pair[param_idx][set_idx] = 1;
375*15dc779aSAndroid Build Coastguard Worker       pstr_lossless_data->bs_data_pair[param_idx][set_idx + 1] = 1;
376*15dc779aSAndroid Build Coastguard Worker       param_set++;
377*15dc779aSAndroid Build Coastguard Worker       set_idx += 2;
378*15dc779aSAndroid Build Coastguard Worker     } else {
379*15dc779aSAndroid Build Coastguard Worker       pstr_lossless_data->bs_data_pair[param_idx][set_idx] = 0;
380*15dc779aSAndroid Build Coastguard Worker       set_idx++;
381*15dc779aSAndroid Build Coastguard Worker     }
382*15dc779aSAndroid Build Coastguard Worker   }
383*15dc779aSAndroid Build Coastguard Worker   data_sets = set_idx;
384*15dc779aSAndroid Build Coastguard Worker 
385*15dc779aSAndroid Build Coastguard Worker   for (set_idx = 0; set_idx < data_sets;) {
386*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_lossless_data->bs_data_pair[param_idx][set_idx], 1);
387*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_lossless_data->bs_quant_coarse_xxx[param_idx][set_idx],
388*15dc779aSAndroid Build Coastguard Worker                         1);
389*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf,
390*15dc779aSAndroid Build Coastguard Worker                         pstr_lossless_data->bs_freq_res_stride_xxx[param_idx][set_idx], 2);
391*15dc779aSAndroid Build Coastguard Worker 
392*15dc779aSAndroid Build Coastguard Worker     pb_stride =
393*15dc779aSAndroid Build Coastguard Worker         freq_res_stride_table[pstr_lossless_data->bs_freq_res_stride_xxx[param_idx][set_idx]];
394*15dc779aSAndroid Build Coastguard Worker     data_bands = (stop_band - start_band - 1) / pb_stride + 1;
395*15dc779aSAndroid Build Coastguard Worker 
396*15dc779aSAndroid Build Coastguard Worker     ixheaace_mps_515_ec_data_pair_enc(
397*15dc779aSAndroid Build Coastguard Worker         pstr_bit_buf, data, old_data, data_type, param_set_index[set_idx], start_band, data_bands,
398*15dc779aSAndroid Build Coastguard Worker         pstr_lossless_data->bs_data_pair[param_idx][set_idx],
399*15dc779aSAndroid Build Coastguard Worker         pstr_lossless_data->bs_quant_coarse_xxx[param_idx][set_idx], independency_flag);
400*15dc779aSAndroid Build Coastguard Worker 
401*15dc779aSAndroid Build Coastguard Worker     if (pstr_lossless_data->bs_data_pair[param_idx][set_idx]) {
402*15dc779aSAndroid Build Coastguard Worker       if (pstr_lossless_data->bs_freq_res_stride_xxx[param_idx][set_idx + 1] !=
403*15dc779aSAndroid Build Coastguard Worker           pstr_lossless_data->bs_freq_res_stride_xxx[param_idx][set_idx]) {
404*15dc779aSAndroid Build Coastguard Worker         return IA_EXHEAACE_EXE_FATAL_MPS_INVALID_RES_STRIDE;
405*15dc779aSAndroid Build Coastguard Worker       }
406*15dc779aSAndroid Build Coastguard Worker       if (pstr_lossless_data->bs_quant_coarse_xxx[param_idx][set_idx + 1] !=
407*15dc779aSAndroid Build Coastguard Worker           pstr_lossless_data->bs_quant_coarse_xxx[param_idx][set_idx]) {
408*15dc779aSAndroid Build Coastguard Worker         return IA_EXHEAACE_EXE_FATAL_MPS_INVALID_QUANT_COARSE;
409*15dc779aSAndroid Build Coastguard Worker       }
410*15dc779aSAndroid Build Coastguard Worker     }
411*15dc779aSAndroid Build Coastguard Worker 
412*15dc779aSAndroid Build Coastguard Worker     for (bin = 0; bin < MAX_NUM_BINS; bin++) {
413*15dc779aSAndroid Build Coastguard Worker       old_data[bin] = data[param_set_index[set_idx] +
414*15dc779aSAndroid Build Coastguard Worker                            pstr_lossless_data->bs_data_pair[param_idx][set_idx]][bin];
415*15dc779aSAndroid Build Coastguard Worker     }
416*15dc779aSAndroid Build Coastguard Worker 
417*15dc779aSAndroid Build Coastguard Worker     set_idx += pstr_lossless_data->bs_data_pair[param_idx][set_idx] + 1;
418*15dc779aSAndroid Build Coastguard Worker   }
419*15dc779aSAndroid Build Coastguard Worker   return IA_NO_ERROR;
420*15dc779aSAndroid Build Coastguard Worker }
421*15dc779aSAndroid Build Coastguard Worker 
ixheaace_mps_515_icc_quant(FLOAT32 val)422*15dc779aSAndroid Build Coastguard Worker WORD32 ixheaace_mps_515_icc_quant(FLOAT32 val) {
423*15dc779aSAndroid Build Coastguard Worker   FLOAT32 p_qsteps[7] = {0.9685f, 0.88909f, 0.72105f, 0.48428f, 0.18382f, -0.2945f, -0.7895f};
424*15dc779aSAndroid Build Coastguard Worker   WORD32 i;
425*15dc779aSAndroid Build Coastguard Worker 
426*15dc779aSAndroid Build Coastguard Worker   if (val >= p_qsteps[0]) {
427*15dc779aSAndroid Build Coastguard Worker     return 0;
428*15dc779aSAndroid Build Coastguard Worker   }
429*15dc779aSAndroid Build Coastguard Worker   for (i = 1; i < 6; i++) {
430*15dc779aSAndroid Build Coastguard Worker     if ((val >= p_qsteps[i]) && (val <= p_qsteps[i - 1])) {
431*15dc779aSAndroid Build Coastguard Worker       return i;
432*15dc779aSAndroid Build Coastguard Worker     }
433*15dc779aSAndroid Build Coastguard Worker   }
434*15dc779aSAndroid Build Coastguard Worker   return 7;
435*15dc779aSAndroid Build Coastguard Worker }
436*15dc779aSAndroid Build Coastguard Worker 
ixheaace_mps_515_cld_quant(FLOAT32 val)437*15dc779aSAndroid Build Coastguard Worker WORD32 ixheaace_mps_515_cld_quant(FLOAT32 val) {
438*15dc779aSAndroid Build Coastguard Worker   FLOAT32 p_qsteps[30] = {-47.5, -42.5, -37.5, -32.5, -27.5, -23.5, -20.5, -17.5, -14.5, -11.5,
439*15dc779aSAndroid Build Coastguard Worker                           -9.0,  -7.0,  -5.0,  -3.0,  -1.0,  1.0,   3.0,   5.0,   7.0,   9.0,
440*15dc779aSAndroid Build Coastguard Worker                           11.5,  14.5,  17.5,  20.5,  23.5,  27.5,  32.5,  37.5,  42.5,  47.5};
441*15dc779aSAndroid Build Coastguard Worker 
442*15dc779aSAndroid Build Coastguard Worker   WORD32 i;
443*15dc779aSAndroid Build Coastguard Worker 
444*15dc779aSAndroid Build Coastguard Worker   if (val < p_qsteps[0]) {
445*15dc779aSAndroid Build Coastguard Worker     return 0 - 15;
446*15dc779aSAndroid Build Coastguard Worker   }
447*15dc779aSAndroid Build Coastguard Worker   for (i = 1; i < 30; i++) {
448*15dc779aSAndroid Build Coastguard Worker     if ((val <= p_qsteps[i]) && (val >= p_qsteps[i - 1])) {
449*15dc779aSAndroid Build Coastguard Worker       return i - 15;
450*15dc779aSAndroid Build Coastguard Worker     }
451*15dc779aSAndroid Build Coastguard Worker   }
452*15dc779aSAndroid Build Coastguard Worker   return 30 - 15;
453*15dc779aSAndroid Build Coastguard Worker }
454*15dc779aSAndroid Build Coastguard Worker 
ixheaace_mps_515_ttt_box(WORD32 slots,FLOAT32 * ptr_real1,FLOAT32 * ptr_imag1,FLOAT32 * ptr_real2,FLOAT32 * ptr_imag2,FLOAT32 * ptr_real3,FLOAT32 * ptr_imag3,WORD32 * ptr_qclds1,WORD32 * ptr_qclds2)455*15dc779aSAndroid Build Coastguard Worker VOID ixheaace_mps_515_ttt_box(WORD32 slots, FLOAT32 *ptr_real1, FLOAT32 *ptr_imag1,
456*15dc779aSAndroid Build Coastguard Worker                               FLOAT32 *ptr_real2, FLOAT32 *ptr_imag2, FLOAT32 *ptr_real3,
457*15dc779aSAndroid Build Coastguard Worker                               FLOAT32 *ptr_imag3, WORD32 *ptr_qclds1, WORD32 *ptr_qclds2) {
458*15dc779aSAndroid Build Coastguard Worker   WORD32 i, j;
459*15dc779aSAndroid Build Coastguard Worker 
460*15dc779aSAndroid Build Coastguard Worker   FLOAT32 cld_s1[PARAMETER_BANDS] = {0};
461*15dc779aSAndroid Build Coastguard Worker   FLOAT32 cld_s2[PARAMETER_BANDS] = {0};
462*15dc779aSAndroid Build Coastguard Worker   FLOAT32 p_pow1[MAX_HYBRID_BANDS] = {0};
463*15dc779aSAndroid Build Coastguard Worker   FLOAT32 p_pow2[MAX_HYBRID_BANDS] = {0};
464*15dc779aSAndroid Build Coastguard Worker   FLOAT32 p_pow3[MAX_HYBRID_BANDS] = {0};
465*15dc779aSAndroid Build Coastguard Worker 
466*15dc779aSAndroid Build Coastguard Worker   FLOAT32 p_pow_par_band1[PARAMETER_BANDS] = {0};
467*15dc779aSAndroid Build Coastguard Worker   FLOAT32 p_pow_par_band2[PARAMETER_BANDS] = {0};
468*15dc779aSAndroid Build Coastguard Worker   FLOAT32 p_pow_par_band3[PARAMETER_BANDS] = {0};
469*15dc779aSAndroid Build Coastguard Worker 
470*15dc779aSAndroid Build Coastguard Worker   for (i = 0; i < slots; i++) {
471*15dc779aSAndroid Build Coastguard Worker     for (j = 0; j < MAX_HYBRID_BANDS; j++) {
472*15dc779aSAndroid Build Coastguard Worker       p_pow1[j] += ptr_real1[i * MAX_HYBRID_BANDS + j] * ptr_real1[i * MAX_HYBRID_BANDS + j] +
473*15dc779aSAndroid Build Coastguard Worker                    ptr_imag1[i * MAX_HYBRID_BANDS + j] * ptr_imag1[i * MAX_HYBRID_BANDS + j];
474*15dc779aSAndroid Build Coastguard Worker       p_pow2[j] += ptr_real2[i * MAX_HYBRID_BANDS + j] * ptr_real2[i * MAX_HYBRID_BANDS + j] +
475*15dc779aSAndroid Build Coastguard Worker                    ptr_imag2[i * MAX_HYBRID_BANDS + j] * ptr_imag2[i * MAX_HYBRID_BANDS + j];
476*15dc779aSAndroid Build Coastguard Worker       p_pow3[j] += ptr_real3[i * MAX_HYBRID_BANDS + j] * ptr_real3[i * MAX_HYBRID_BANDS + j] +
477*15dc779aSAndroid Build Coastguard Worker                    ptr_imag3[i * MAX_HYBRID_BANDS + j] * ptr_imag3[i * MAX_HYBRID_BANDS + j];
478*15dc779aSAndroid Build Coastguard Worker 
479*15dc779aSAndroid Build Coastguard Worker       ptr_real1[i * MAX_HYBRID_BANDS + j] =
480*15dc779aSAndroid Build Coastguard Worker           (ptr_real1[i * MAX_HYBRID_BANDS + j] + ptr_real3[i * MAX_HYBRID_BANDS + j] * 0.7071f);
481*15dc779aSAndroid Build Coastguard Worker       ptr_imag1[i * MAX_HYBRID_BANDS + j] =
482*15dc779aSAndroid Build Coastguard Worker           (ptr_imag1[i * MAX_HYBRID_BANDS + j] + ptr_imag3[i * MAX_HYBRID_BANDS + j] * 0.7071f);
483*15dc779aSAndroid Build Coastguard Worker 
484*15dc779aSAndroid Build Coastguard Worker       ptr_real2[i * MAX_HYBRID_BANDS + j] =
485*15dc779aSAndroid Build Coastguard Worker           (ptr_real2[i * MAX_HYBRID_BANDS + j] + ptr_real3[i * MAX_HYBRID_BANDS + j] * 0.7071f);
486*15dc779aSAndroid Build Coastguard Worker       ptr_imag2[i * MAX_HYBRID_BANDS + j] =
487*15dc779aSAndroid Build Coastguard Worker           (ptr_imag2[i * MAX_HYBRID_BANDS + j] + ptr_imag3[i * MAX_HYBRID_BANDS + j] * 0.7071f);
488*15dc779aSAndroid Build Coastguard Worker     }
489*15dc779aSAndroid Build Coastguard Worker   }
490*15dc779aSAndroid Build Coastguard Worker   for (i = 0; i < MAX_HYBRID_BANDS; i++) {
491*15dc779aSAndroid Build Coastguard Worker     p_pow_par_band1[kernels_20[i]] += p_pow1[i];
492*15dc779aSAndroid Build Coastguard Worker     p_pow_par_band2[kernels_20[i]] += p_pow2[i];
493*15dc779aSAndroid Build Coastguard Worker     p_pow_par_band3[kernels_20[i]] += p_pow3[i];
494*15dc779aSAndroid Build Coastguard Worker   }
495*15dc779aSAndroid Build Coastguard Worker   for (i = 0; i < PARAMETER_BANDS; i++) {
496*15dc779aSAndroid Build Coastguard Worker     if (p_pow_par_band3[i]) {
497*15dc779aSAndroid Build Coastguard Worker       cld_s1[i] = ((p_pow_par_band1[i] + p_pow_par_band2[i]) / (p_pow_par_band3[i]));
498*15dc779aSAndroid Build Coastguard Worker       cld_s1[i] = (FLOAT32)(10.0 * log(cld_s1[i] + 1e-10) / log(10.0));
499*15dc779aSAndroid Build Coastguard Worker     } else if ((p_pow_par_band1[i] + p_pow_par_band2[i]))
500*15dc779aSAndroid Build Coastguard Worker       cld_s1[i] = 50.0;
501*15dc779aSAndroid Build Coastguard Worker     else
502*15dc779aSAndroid Build Coastguard Worker       cld_s1[i] = -50;
503*15dc779aSAndroid Build Coastguard Worker     ptr_qclds1[i] = ixheaace_mps_515_cld_quant(cld_s1[i]);
504*15dc779aSAndroid Build Coastguard Worker 
505*15dc779aSAndroid Build Coastguard Worker     if (p_pow_par_band2[i]) {
506*15dc779aSAndroid Build Coastguard Worker       cld_s2[i] = (p_pow_par_band1[i] / (p_pow_par_band2[i]));
507*15dc779aSAndroid Build Coastguard Worker       cld_s2[i] = (FLOAT32)(10 * log(cld_s2[i] + 1e-10f) / log(10.0));
508*15dc779aSAndroid Build Coastguard Worker     } else if (p_pow_par_band1[i])
509*15dc779aSAndroid Build Coastguard Worker       cld_s2[i] = 50.0;
510*15dc779aSAndroid Build Coastguard Worker     else
511*15dc779aSAndroid Build Coastguard Worker       cld_s2[i] = -50;
512*15dc779aSAndroid Build Coastguard Worker     ptr_qclds2[i] = ixheaace_mps_515_cld_quant(cld_s2[i]);
513*15dc779aSAndroid Build Coastguard Worker   }
514*15dc779aSAndroid Build Coastguard Worker }
515*15dc779aSAndroid Build Coastguard Worker 
ixheaace_mps_515_ott_box(WORD32 slots,FLOAT32 * ptr_real1,FLOAT32 * ptr_imag1,FLOAT32 * ptr_real2,FLOAT32 * ptr_imag2,WORD32 * ptr_p_qclds,WORD32 * ptr_qiccs)516*15dc779aSAndroid Build Coastguard Worker VOID ixheaace_mps_515_ott_box(WORD32 slots, FLOAT32 *ptr_real1, FLOAT32 *ptr_imag1,
517*15dc779aSAndroid Build Coastguard Worker                               FLOAT32 *ptr_real2, FLOAT32 *ptr_imag2, WORD32 *ptr_p_qclds,
518*15dc779aSAndroid Build Coastguard Worker                               WORD32 *ptr_qiccs) {
519*15dc779aSAndroid Build Coastguard Worker   WORD32 i, j;
520*15dc779aSAndroid Build Coastguard Worker 
521*15dc779aSAndroid Build Coastguard Worker   FLOAT32 clds[PARAMETER_BANDS] = {0};
522*15dc779aSAndroid Build Coastguard Worker   FLOAT32 iccs[PARAMETER_BANDS] = {0};
523*15dc779aSAndroid Build Coastguard Worker   FLOAT32 p_pow1[MAX_HYBRID_BANDS] = {0};
524*15dc779aSAndroid Build Coastguard Worker   FLOAT32 p_pow2[MAX_HYBRID_BANDS] = {0};
525*15dc779aSAndroid Build Coastguard Worker   FLOAT32 p_xcor_real[MAX_HYBRID_BANDS] = {0};
526*15dc779aSAndroid Build Coastguard Worker 
527*15dc779aSAndroid Build Coastguard Worker   FLOAT32 p_pow_par_band1[PARAMETER_BANDS] = {0};
528*15dc779aSAndroid Build Coastguard Worker   FLOAT32 p_pow_par_band2[PARAMETER_BANDS] = {0};
529*15dc779aSAndroid Build Coastguard Worker   FLOAT32 p_xcor_par_band[PARAMETER_BANDS] = {0};
530*15dc779aSAndroid Build Coastguard Worker 
531*15dc779aSAndroid Build Coastguard Worker   for (i = 0; i < slots; i++) {
532*15dc779aSAndroid Build Coastguard Worker     for (j = 0; j < MAX_HYBRID_BANDS; j++) {
533*15dc779aSAndroid Build Coastguard Worker       p_pow1[j] += ptr_real1[i * MAX_HYBRID_BANDS + j] * ptr_real1[i * MAX_HYBRID_BANDS + j] +
534*15dc779aSAndroid Build Coastguard Worker                    ptr_imag1[i * MAX_HYBRID_BANDS + j] * ptr_imag1[i * MAX_HYBRID_BANDS + j];
535*15dc779aSAndroid Build Coastguard Worker       p_pow2[j] += ptr_real2[i * MAX_HYBRID_BANDS + j] * ptr_real2[i * MAX_HYBRID_BANDS + j] +
536*15dc779aSAndroid Build Coastguard Worker                    ptr_imag2[i * MAX_HYBRID_BANDS + j] * ptr_imag2[i * MAX_HYBRID_BANDS + j];
537*15dc779aSAndroid Build Coastguard Worker       p_xcor_real[j] +=
538*15dc779aSAndroid Build Coastguard Worker           ptr_real1[i * MAX_HYBRID_BANDS + j] * ptr_real2[i * MAX_HYBRID_BANDS + j] +
539*15dc779aSAndroid Build Coastguard Worker           ptr_imag1[i * MAX_HYBRID_BANDS + j] * ptr_imag2[i * MAX_HYBRID_BANDS + j];
540*15dc779aSAndroid Build Coastguard Worker 
541*15dc779aSAndroid Build Coastguard Worker       ptr_real1[i * MAX_HYBRID_BANDS + j] =
542*15dc779aSAndroid Build Coastguard Worker           (ptr_real1[i * MAX_HYBRID_BANDS + j] + ptr_real2[i * MAX_HYBRID_BANDS + j]);
543*15dc779aSAndroid Build Coastguard Worker       ptr_imag1[i * MAX_HYBRID_BANDS + j] =
544*15dc779aSAndroid Build Coastguard Worker           (ptr_imag1[i * MAX_HYBRID_BANDS + j] + ptr_imag2[i * MAX_HYBRID_BANDS + j]);
545*15dc779aSAndroid Build Coastguard Worker     }
546*15dc779aSAndroid Build Coastguard Worker   }
547*15dc779aSAndroid Build Coastguard Worker   for (i = 0; i < MAX_HYBRID_BANDS; i++) {
548*15dc779aSAndroid Build Coastguard Worker     p_pow_par_band1[kernels_20[i]] += p_pow1[i];
549*15dc779aSAndroid Build Coastguard Worker     p_pow_par_band2[kernels_20[i]] += p_pow2[i];
550*15dc779aSAndroid Build Coastguard Worker     p_xcor_par_band[kernels_20[i]] += p_xcor_real[i];
551*15dc779aSAndroid Build Coastguard Worker   }
552*15dc779aSAndroid Build Coastguard Worker   for (i = 0; i < PARAMETER_BANDS; i++) {
553*15dc779aSAndroid Build Coastguard Worker     if (p_pow_par_band2[i]) {
554*15dc779aSAndroid Build Coastguard Worker       clds[i] = (p_pow_par_band1[i] / (p_pow_par_band2[i]));
555*15dc779aSAndroid Build Coastguard Worker       clds[i] = (FLOAT32)(10 * log(clds[i] + 1e-10) / log(10.0));
556*15dc779aSAndroid Build Coastguard Worker     } else if (p_pow_par_band1[i])
557*15dc779aSAndroid Build Coastguard Worker       clds[i] = 50.0;
558*15dc779aSAndroid Build Coastguard Worker     else
559*15dc779aSAndroid Build Coastguard Worker       clds[i] = -50;  // 0.0;
560*15dc779aSAndroid Build Coastguard Worker     iccs[i] =
561*15dc779aSAndroid Build Coastguard Worker         p_xcor_par_band[i] / (FLOAT32)sqrt((p_pow_par_band1[i] * p_pow_par_band2[i] + 1e-10));
562*15dc779aSAndroid Build Coastguard Worker 
563*15dc779aSAndroid Build Coastguard Worker     ptr_p_qclds[i] = ixheaace_mps_515_cld_quant(clds[i]);
564*15dc779aSAndroid Build Coastguard Worker     ptr_qiccs[i] = ixheaace_mps_515_icc_quant(iccs[i]);
565*15dc779aSAndroid Build Coastguard Worker   }
566*15dc779aSAndroid Build Coastguard Worker }
567*15dc779aSAndroid Build Coastguard Worker 
ixheaace_mps_212_write_spatial_specific_config(ixheaace_mps_spatial_specific_config * const pstr_spatial_specific_config,UWORD8 * const ptr_output_buffer,const WORD32 output_buffer_size,WORD32 * const ptr_output_bits,WORD32 aot)568*15dc779aSAndroid Build Coastguard Worker IA_ERRORCODE ixheaace_mps_212_write_spatial_specific_config(
569*15dc779aSAndroid Build Coastguard Worker     ixheaace_mps_spatial_specific_config *const pstr_spatial_specific_config,
570*15dc779aSAndroid Build Coastguard Worker     UWORD8 *const ptr_output_buffer, const WORD32 output_buffer_size,
571*15dc779aSAndroid Build Coastguard Worker     WORD32 *const ptr_output_bits, WORD32 aot) {
572*15dc779aSAndroid Build Coastguard Worker   IA_ERRORCODE error = IA_NO_ERROR;
573*15dc779aSAndroid Build Coastguard Worker   WORD32 bs_sampling_frequency_index = 0;
574*15dc779aSAndroid Build Coastguard Worker   WORD32 bs_freq_res = 0;
575*15dc779aSAndroid Build Coastguard Worker   ixheaace_bit_buf bit_buf;
576*15dc779aSAndroid Build Coastguard Worker   ixheaace_bit_buf_handle pstr_bit_buf =
577*15dc779aSAndroid Build Coastguard Worker       ia_enhaacplus_enc_create_bitbuffer(&bit_buf, ptr_output_buffer, output_buffer_size);
578*15dc779aSAndroid Build Coastguard Worker 
579*15dc779aSAndroid Build Coastguard Worker   error = ixheaace_mps_212_get_bs_freq_res_index(pstr_spatial_specific_config->num_bands,
580*15dc779aSAndroid Build Coastguard Worker                                                  &bs_freq_res, aot);
581*15dc779aSAndroid Build Coastguard Worker   if (error) {
582*15dc779aSAndroid Build Coastguard Worker     return error;
583*15dc779aSAndroid Build Coastguard Worker   }
584*15dc779aSAndroid Build Coastguard Worker 
585*15dc779aSAndroid Build Coastguard Worker   if (aot == AOT_AAC_ELD) {
586*15dc779aSAndroid Build Coastguard Worker     ixheaace_mps_212_get_sampling_frequency_index(
587*15dc779aSAndroid Build Coastguard Worker         pstr_spatial_specific_config->bs_sampling_frequency, &bs_sampling_frequency_index);
588*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, bs_sampling_frequency_index, 4);
589*15dc779aSAndroid Build Coastguard Worker 
590*15dc779aSAndroid Build Coastguard Worker     if (bs_sampling_frequency_index == 15) {
591*15dc779aSAndroid Build Coastguard Worker       ixheaace_write_bits(pstr_bit_buf, pstr_spatial_specific_config->bs_sampling_frequency, 24);
592*15dc779aSAndroid Build Coastguard Worker     }
593*15dc779aSAndroid Build Coastguard Worker 
594*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_spatial_specific_config->bs_frame_length, 5);
595*15dc779aSAndroid Build Coastguard Worker 
596*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, bs_freq_res, 3);
597*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_spatial_specific_config->bs_tree_config, 4);
598*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_spatial_specific_config->bs_quant_mode, 2);
599*15dc779aSAndroid Build Coastguard Worker 
600*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, 0, 1);
601*15dc779aSAndroid Build Coastguard Worker 
602*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_spatial_specific_config->bs_fixed_gain_dmx, 3);
603*15dc779aSAndroid Build Coastguard Worker 
604*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, 0, 2);
605*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_spatial_specific_config->bs_decorr_config, 2);
606*15dc779aSAndroid Build Coastguard Worker 
607*15dc779aSAndroid Build Coastguard Worker     ixheaace_byte_align_buffer(pstr_bit_buf);
608*15dc779aSAndroid Build Coastguard Worker 
609*15dc779aSAndroid Build Coastguard Worker     if ((*ptr_output_bits = ia_enhaacplus_enc_get_bits_available(pstr_bit_buf)) >
610*15dc779aSAndroid Build Coastguard Worker         (output_buffer_size * 8)) {
611*15dc779aSAndroid Build Coastguard Worker       return IA_EXHEAACE_CONFIG_NONFATAL_MPS_INVALID_CONFIG;
612*15dc779aSAndroid Build Coastguard Worker     }
613*15dc779aSAndroid Build Coastguard Worker 
614*15dc779aSAndroid Build Coastguard Worker     ixheaace_byte_align_buffer(pstr_bit_buf);
615*15dc779aSAndroid Build Coastguard Worker   } else {
616*15dc779aSAndroid Build Coastguard Worker     ixheaace_mps_212_get_sampling_frequency_index(
617*15dc779aSAndroid Build Coastguard Worker         pstr_spatial_specific_config->bs_sampling_frequency, &bs_sampling_frequency_index);
618*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, bs_freq_res, 3);
619*15dc779aSAndroid Build Coastguard Worker 
620*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_spatial_specific_config->bs_fixed_gain_dmx, 3);
621*15dc779aSAndroid Build Coastguard Worker 
622*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, 0, 2);
623*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_spatial_specific_config->bs_decorr_config, 2);
624*15dc779aSAndroid Build Coastguard Worker 
625*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, 0, 1);
626*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, 0, 1);
627*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, 0, 1);
628*15dc779aSAndroid Build Coastguard Worker 
629*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, 0, 5);
630*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, 0, 1);
631*15dc779aSAndroid Build Coastguard Worker 
632*15dc779aSAndroid Build Coastguard Worker     *ptr_output_bits = ia_enhaacplus_enc_get_bits_available(pstr_bit_buf);
633*15dc779aSAndroid Build Coastguard Worker   }
634*15dc779aSAndroid Build Coastguard Worker   return error;
635*15dc779aSAndroid Build Coastguard Worker }
636*15dc779aSAndroid Build Coastguard Worker 
ixheaace_mps_212_write_spatial_frame(UWORD8 * const ptr_output_buffer,const WORD32 output_buffer_size,WORD32 * const ptr_output_bits,ixheaace_mps_pstr_bsf_instance pstr_bsf_instance,WORD32 aot)637*15dc779aSAndroid Build Coastguard Worker IA_ERRORCODE ixheaace_mps_212_write_spatial_frame(
638*15dc779aSAndroid Build Coastguard Worker     UWORD8 *const ptr_output_buffer, const WORD32 output_buffer_size,
639*15dc779aSAndroid Build Coastguard Worker     WORD32 *const ptr_output_bits, ixheaace_mps_pstr_bsf_instance pstr_bsf_instance, WORD32 aot) {
640*15dc779aSAndroid Build Coastguard Worker   IA_ERRORCODE error = IA_NO_ERROR;
641*15dc779aSAndroid Build Coastguard Worker   WORD32 i, j, num_param_sets, num_ott_boxes;
642*15dc779aSAndroid Build Coastguard Worker   ixheaace_mps_spatial_frame *pstr_spatial_frame = NULL;
643*15dc779aSAndroid Build Coastguard Worker   ixheaace_mps_spatial_specific_config *pstr_specific_config = NULL;
644*15dc779aSAndroid Build Coastguard Worker   ixheaace_bit_buf bit_buf;
645*15dc779aSAndroid Build Coastguard Worker   ixheaace_bit_buf_handle pstr_bit_buf =
646*15dc779aSAndroid Build Coastguard Worker       ia_enhaacplus_enc_create_bitbuffer(&bit_buf, ptr_output_buffer, output_buffer_size);
647*15dc779aSAndroid Build Coastguard Worker   pstr_spatial_frame = &pstr_bsf_instance->frame;
648*15dc779aSAndroid Build Coastguard Worker   pstr_specific_config = &pstr_bsf_instance->spatial_specific_config;
649*15dc779aSAndroid Build Coastguard Worker   num_ott_boxes = pstr_bsf_instance->spatial_specific_config.tree_description.num_ott_boxes;
650*15dc779aSAndroid Build Coastguard Worker   num_param_sets = pstr_spatial_frame->framing_info.num_param_sets;
651*15dc779aSAndroid Build Coastguard Worker 
652*15dc779aSAndroid Build Coastguard Worker   if (pstr_spatial_frame->b_use_bb_cues) {
653*15dc779aSAndroid Build Coastguard Worker     for (i = 0; i < IXHEAACE_MPS_MAX_NUM_BOXES; i++) {
654*15dc779aSAndroid Build Coastguard Worker       if (num_param_sets == 1) {
655*15dc779aSAndroid Build Coastguard Worker         pstr_spatial_frame->cld_lossless_data.bs_freq_res_stride_xxx[i][0] = 3;
656*15dc779aSAndroid Build Coastguard Worker         pstr_spatial_frame->icc_lossless_data.bs_freq_res_stride_xxx[i][0] = 3;
657*15dc779aSAndroid Build Coastguard Worker       } else {
658*15dc779aSAndroid Build Coastguard Worker         for (j = 1; j < MAX_NUM_PARAMS; j++) {
659*15dc779aSAndroid Build Coastguard Worker           pstr_spatial_frame->cld_lossless_data.bs_freq_res_stride_xxx[i][j] = 3;
660*15dc779aSAndroid Build Coastguard Worker           pstr_spatial_frame->icc_lossless_data.bs_freq_res_stride_xxx[i][j] = 3;
661*15dc779aSAndroid Build Coastguard Worker         }
662*15dc779aSAndroid Build Coastguard Worker       }
663*15dc779aSAndroid Build Coastguard Worker     }
664*15dc779aSAndroid Build Coastguard Worker   }
665*15dc779aSAndroid Build Coastguard Worker 
666*15dc779aSAndroid Build Coastguard Worker   if (aot == AOT_AAC_ELD) {
667*15dc779aSAndroid Build Coastguard Worker     ixheaace_mps_212_write_framing_info(
668*15dc779aSAndroid Build Coastguard Worker         pstr_bit_buf, &(pstr_spatial_frame->framing_info),
669*15dc779aSAndroid Build Coastguard Worker         pstr_bsf_instance->spatial_specific_config.bs_frame_length);
670*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_spatial_frame->bs_independency_flag, 1);
671*15dc779aSAndroid Build Coastguard Worker   } else if (aot == AOT_USAC) {
672*15dc779aSAndroid Build Coastguard Worker     if (!pstr_spatial_frame->bs_independency_flag) {
673*15dc779aSAndroid Build Coastguard Worker       ixheaace_write_bits(pstr_bit_buf, pstr_spatial_frame->bs_independency_flag, 1);
674*15dc779aSAndroid Build Coastguard Worker     }
675*15dc779aSAndroid Build Coastguard Worker   }
676*15dc779aSAndroid Build Coastguard Worker   error = ixheaace_mps_212_write_ott_data(
677*15dc779aSAndroid Build Coastguard Worker       pstr_bit_buf, &pstr_bsf_instance->prev_frame_data.prev_ott_data,
678*15dc779aSAndroid Build Coastguard Worker       &pstr_spatial_frame->ott_data, pstr_specific_config->ott_config,
679*15dc779aSAndroid Build Coastguard Worker       &pstr_spatial_frame->cld_lossless_data, &pstr_spatial_frame->icc_lossless_data,
680*15dc779aSAndroid Build Coastguard Worker       num_ott_boxes, pstr_specific_config->num_bands, num_param_sets,
681*15dc779aSAndroid Build Coastguard Worker       pstr_spatial_frame->bs_independency_flag);
682*15dc779aSAndroid Build Coastguard Worker   if (error != IA_NO_ERROR) {
683*15dc779aSAndroid Build Coastguard Worker     return error;
684*15dc779aSAndroid Build Coastguard Worker   }
685*15dc779aSAndroid Build Coastguard Worker   if (aot == AOT_AAC_ELD) {
686*15dc779aSAndroid Build Coastguard Worker     ixheaace_mps_212_write_smg_data(pstr_bit_buf, &pstr_spatial_frame->smg_data, num_param_sets,
687*15dc779aSAndroid Build Coastguard Worker                                     pstr_specific_config->num_bands);
688*15dc779aSAndroid Build Coastguard Worker   }
689*15dc779aSAndroid Build Coastguard Worker 
690*15dc779aSAndroid Build Coastguard Worker   *ptr_output_bits = ia_enhaacplus_enc_get_bits_available(pstr_bit_buf);
691*15dc779aSAndroid Build Coastguard Worker   if ((*ptr_output_bits) > (output_buffer_size * 8)) {
692*15dc779aSAndroid Build Coastguard Worker     return IA_EXHEAACE_CONFIG_NONFATAL_MPS_INVALID_CONFIG;
693*15dc779aSAndroid Build Coastguard Worker   }
694*15dc779aSAndroid Build Coastguard Worker   return error;
695*15dc779aSAndroid Build Coastguard Worker }
696*15dc779aSAndroid Build Coastguard Worker 
697*15dc779aSAndroid Build Coastguard Worker IA_ERRORCODE
ixheaace_mps_515_write_spatial_specific_config(ixheaace_bit_buf_handle pstr_bit_buf,ixheaace_mps_sac_bsf_instance * pstr_bsf_instance)698*15dc779aSAndroid Build Coastguard Worker ixheaace_mps_515_write_spatial_specific_config(ixheaace_bit_buf_handle pstr_bit_buf,
699*15dc779aSAndroid Build Coastguard Worker                                                ixheaace_mps_sac_bsf_instance *pstr_bsf_instance) {
700*15dc779aSAndroid Build Coastguard Worker   WORD32 idx, box, bin;
701*15dc779aSAndroid Build Coastguard Worker   WORD32 bs_sampling_frequency_index;
702*15dc779aSAndroid Build Coastguard Worker   ixheaace_mps_sac_spatial_frame *pstr_spatial_frame = &(pstr_bsf_instance->current_frame);
703*15dc779aSAndroid Build Coastguard Worker   ixheaace_mps_sac_specific_config *pstr_specific_config =
704*15dc779aSAndroid Build Coastguard Worker       &(pstr_bsf_instance->spatial_specific_config);
705*15dc779aSAndroid Build Coastguard Worker   ixheaace_mps_sac_tree_description *pstr_tree_description =
706*15dc779aSAndroid Build Coastguard Worker       &(pstr_specific_config->tree_description);
707*15dc779aSAndroid Build Coastguard Worker 
708*15dc779aSAndroid Build Coastguard Worker   pstr_tree_description->num_ott_boxes =
709*15dc779aSAndroid Build Coastguard Worker       tree_config_table[pstr_specific_config->bs_tree_config].num_ott_boxes;
710*15dc779aSAndroid Build Coastguard Worker   pstr_tree_description->num_ttt_boxes =
711*15dc779aSAndroid Build Coastguard Worker       tree_config_table[pstr_specific_config->bs_tree_config].num_ttt_boxes;
712*15dc779aSAndroid Build Coastguard Worker   pstr_tree_description->num_in_chan =
713*15dc779aSAndroid Build Coastguard Worker       tree_config_table[pstr_specific_config->bs_tree_config].num_in_chan;
714*15dc779aSAndroid Build Coastguard Worker   pstr_tree_description->num_out_chan =
715*15dc779aSAndroid Build Coastguard Worker       tree_config_table[pstr_specific_config->bs_tree_config].num_out_chan;
716*15dc779aSAndroid Build Coastguard Worker 
717*15dc779aSAndroid Build Coastguard Worker   if (pstr_specific_config->bs_temp_shape_config == 2) {
718*15dc779aSAndroid Build Coastguard Worker     return IA_EXHEAACE_EXE_FATAL_MPS_UNSUPPORTED_GUIDED_ENV_SHAPE;
719*15dc779aSAndroid Build Coastguard Worker   }
720*15dc779aSAndroid Build Coastguard Worker 
721*15dc779aSAndroid Build Coastguard Worker   if (pstr_specific_config->bs_3d_audio_mode > 0) {
722*15dc779aSAndroid Build Coastguard Worker     return IA_EXHEAACE_EXE_FATAL_MPS_3D_STEREO_MODE_NOT_SUPPORTED;
723*15dc779aSAndroid Build Coastguard Worker   }
724*15dc779aSAndroid Build Coastguard Worker 
725*15dc779aSAndroid Build Coastguard Worker   if (pstr_specific_config->bs_residual_coding == 1) {
726*15dc779aSAndroid Build Coastguard Worker     return IA_EXHEAACE_EXE_FATAL_MPS_UNSUPPORTED_RESIDUAL_CODING;
727*15dc779aSAndroid Build Coastguard Worker   }
728*15dc779aSAndroid Build Coastguard Worker   if (pstr_specific_config->bs_arbitrary_downmix == 2) {
729*15dc779aSAndroid Build Coastguard Worker     return IA_EXHEAACE_EXE_FATAL_MPS_UNSUPPORTED_ARBITARY_DOWNMIX_CODING;
730*15dc779aSAndroid Build Coastguard Worker   }
731*15dc779aSAndroid Build Coastguard Worker   if (pstr_specific_config->tree_description.arbitrary_tree) {
732*15dc779aSAndroid Build Coastguard Worker     return IA_EXHEAACE_EXE_FATAL_MPS_ARBITARY_TREE_NOT_SUPPORTED;
733*15dc779aSAndroid Build Coastguard Worker   }
734*15dc779aSAndroid Build Coastguard Worker 
735*15dc779aSAndroid Build Coastguard Worker   for (box = 0; box < MAX_NUM_BOXES; box++) {
736*15dc779aSAndroid Build Coastguard Worker     pstr_tree_description->ott_mode_lfe[box] =
737*15dc779aSAndroid Build Coastguard Worker         tree_config_table[pstr_specific_config->bs_tree_config].ott_mode_lfe[box];
738*15dc779aSAndroid Build Coastguard Worker   }
739*15dc779aSAndroid Build Coastguard Worker   pstr_bsf_instance->num_bins = freq_res_bin_table[pstr_specific_config->bs_freq_res];
740*15dc779aSAndroid Build Coastguard Worker 
741*15dc779aSAndroid Build Coastguard Worker   bs_sampling_frequency_index = 15;
742*15dc779aSAndroid Build Coastguard Worker   for (idx = 0; idx < 15; idx++) {
743*15dc779aSAndroid Build Coastguard Worker     if (pstr_specific_config->bs_sampling_frequency == ia_sampl_freq_table[idx]) {
744*15dc779aSAndroid Build Coastguard Worker       bs_sampling_frequency_index = idx;
745*15dc779aSAndroid Build Coastguard Worker       break;
746*15dc779aSAndroid Build Coastguard Worker     }
747*15dc779aSAndroid Build Coastguard Worker   }
748*15dc779aSAndroid Build Coastguard Worker 
749*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, bs_sampling_frequency_index, 4);
750*15dc779aSAndroid Build Coastguard Worker   if (bs_sampling_frequency_index == 15) {
751*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->bs_sampling_frequency, 24);
752*15dc779aSAndroid Build Coastguard Worker   }
753*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->bs_frame_length, 5);
754*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->bs_freq_res, 3);
755*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->bs_tree_config, 4);
756*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->bs_quant_mode, 2);
757*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->bs_one_icc, 1);
758*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->bs_arbitrary_downmix, 1);
759*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->bs_fixed_gain_sur, 3);
760*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->bs_fixed_gain_lfe, 3);
761*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->bs_fixed_gain_dmx, 3);
762*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->bs_matrix_mode, 1);
763*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->bs_temp_shape_config, 2);
764*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->bs_decorr_config, 2);
765*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->bs_3d_audio_mode, 1);
766*15dc779aSAndroid Build Coastguard Worker 
767*15dc779aSAndroid Build Coastguard Worker   for (box = 0; box < pstr_tree_description->num_ott_boxes; box++) {
768*15dc779aSAndroid Build Coastguard Worker     if (pstr_tree_description->ott_mode_lfe[box]) {
769*15dc779aSAndroid Build Coastguard Worker       ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->ott_config[box].bs_ott_bands, 5);
770*15dc779aSAndroid Build Coastguard Worker     }
771*15dc779aSAndroid Build Coastguard Worker   }
772*15dc779aSAndroid Build Coastguard Worker   for (box = 0; box < pstr_tree_description->num_ttt_boxes; box++) {
773*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->ttt_config[box].bs_ttt_dual_mode, 1);
774*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->ttt_config[box].bs_ttt_mode_low, 3);
775*15dc779aSAndroid Build Coastguard Worker     if (pstr_specific_config->ttt_config[box].bs_ttt_dual_mode == 1) {
776*15dc779aSAndroid Build Coastguard Worker       ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->ttt_config[box].bs_ttt_mode_high,
777*15dc779aSAndroid Build Coastguard Worker                           3);
778*15dc779aSAndroid Build Coastguard Worker       ixheaace_write_bits(pstr_bit_buf, pstr_specific_config->ttt_config[box].bs_ttt_bands_low,
779*15dc779aSAndroid Build Coastguard Worker                           5);
780*15dc779aSAndroid Build Coastguard Worker     }
781*15dc779aSAndroid Build Coastguard Worker   }
782*15dc779aSAndroid Build Coastguard Worker   ixheaace_byte_align_buffer(pstr_bit_buf);
783*15dc779aSAndroid Build Coastguard Worker 
784*15dc779aSAndroid Build Coastguard Worker   for (box = 0; box < MAX_NUM_BOXES; box++) {
785*15dc779aSAndroid Build Coastguard Worker     pstr_specific_config->ttt_config[box].bs_ttt_bands_low = pstr_bsf_instance->num_bins;
786*15dc779aSAndroid Build Coastguard Worker     for (bin = 0; bin < MAX_NUM_BINS; bin++) {
787*15dc779aSAndroid Build Coastguard Worker       pstr_spatial_frame->ott_data.cld_old[box][bin] = 0;
788*15dc779aSAndroid Build Coastguard Worker       pstr_spatial_frame->ott_data.icc_old[box][bin] = 0;
789*15dc779aSAndroid Build Coastguard Worker       pstr_spatial_frame->ttt_data.cpc_cld1_old[box][bin] = 0;
790*15dc779aSAndroid Build Coastguard Worker       pstr_spatial_frame->ttt_data.cpc_cld2_old[box][bin] = 0;
791*15dc779aSAndroid Build Coastguard Worker       pstr_spatial_frame->ttt_data.icc_old[box][bin] = 0;
792*15dc779aSAndroid Build Coastguard Worker     }
793*15dc779aSAndroid Build Coastguard Worker   }
794*15dc779aSAndroid Build Coastguard Worker 
795*15dc779aSAndroid Build Coastguard Worker   for (box = 0; box < pstr_tree_description->num_ott_boxes; box++) {
796*15dc779aSAndroid Build Coastguard Worker     if (!pstr_tree_description->ott_mode_lfe[box]) {
797*15dc779aSAndroid Build Coastguard Worker       pstr_specific_config->ott_config[box].bs_ott_bands = pstr_bsf_instance->num_bins;
798*15dc779aSAndroid Build Coastguard Worker     }
799*15dc779aSAndroid Build Coastguard Worker     if (!pstr_specific_config->ttt_config[box].bs_ttt_dual_mode) {
800*15dc779aSAndroid Build Coastguard Worker       pstr_specific_config->ttt_config[box].bs_ttt_bands_low = pstr_bsf_instance->num_bins;
801*15dc779aSAndroid Build Coastguard Worker     }
802*15dc779aSAndroid Build Coastguard Worker   }
803*15dc779aSAndroid Build Coastguard Worker 
804*15dc779aSAndroid Build Coastguard Worker   return IA_NO_ERROR;
805*15dc779aSAndroid Build Coastguard Worker }
806*15dc779aSAndroid Build Coastguard Worker 
807*15dc779aSAndroid Build Coastguard Worker IA_ERRORCODE
ixheaace_mps_515_write_spatial_frame(ixheaace_bit_buf_handle pstr_bit_buf,ixheaace_mps_sac_bsf_instance * pstr_bsf_instance)808*15dc779aSAndroid Build Coastguard Worker ixheaace_mps_515_write_spatial_frame(ixheaace_bit_buf_handle pstr_bit_buf,
809*15dc779aSAndroid Build Coastguard Worker                                      ixheaace_mps_sac_bsf_instance *pstr_bsf_instance) {
810*15dc779aSAndroid Build Coastguard Worker   IA_ERRORCODE error = IA_NO_ERROR;
811*15dc779aSAndroid Build Coastguard Worker   UWORD8 bits_param_slot;
812*15dc779aSAndroid Build Coastguard Worker   WORD32 param, box, ch, bin;
813*15dc779aSAndroid Build Coastguard Worker   WORD32 prev_bs_param_slot, num_temp_shape_chan;
814*15dc779aSAndroid Build Coastguard Worker   ixheaace_mps_sac_spatial_frame *pstr_spatial_frame = &(pstr_bsf_instance->current_frame);
815*15dc779aSAndroid Build Coastguard Worker   ixheaace_mps_sac_specific_config *pstr_specific_config =
816*15dc779aSAndroid Build Coastguard Worker       &(pstr_bsf_instance->spatial_specific_config);
817*15dc779aSAndroid Build Coastguard Worker   WORD32 bs_independency_flag = pstr_spatial_frame->bs_independency_flag;
818*15dc779aSAndroid Build Coastguard Worker   WORD32 num_param_sets = pstr_spatial_frame->framing_info.bs_num_param_sets;
819*15dc779aSAndroid Build Coastguard Worker   WORD32 num_ott_boxes =
820*15dc779aSAndroid Build Coastguard Worker       pstr_bsf_instance->spatial_specific_config.tree_description.num_ott_boxes;
821*15dc779aSAndroid Build Coastguard Worker   WORD32 num_ttt_boxes =
822*15dc779aSAndroid Build Coastguard Worker       pstr_bsf_instance->spatial_specific_config.tree_description.num_ttt_boxes;
823*15dc779aSAndroid Build Coastguard Worker   WORD32 *ptr_ott_mode_lfe =
824*15dc779aSAndroid Build Coastguard Worker       pstr_bsf_instance->spatial_specific_config.tree_description.ott_mode_lfe;
825*15dc779aSAndroid Build Coastguard Worker 
826*15dc779aSAndroid Build Coastguard Worker   if (pstr_specific_config->bs_arbitrary_downmix) {
827*15dc779aSAndroid Build Coastguard Worker     return IA_EXHEAACE_EXE_FATAL_MPS_UNSUPPORTED_ARBITARY_DOWNMIX_CODING;
828*15dc779aSAndroid Build Coastguard Worker   }
829*15dc779aSAndroid Build Coastguard Worker   if (pstr_specific_config->bs_residual_coding == 1) {
830*15dc779aSAndroid Build Coastguard Worker     return IA_EXHEAACE_EXE_FATAL_MPS_UNSUPPORTED_RESIDUAL_CODING;
831*15dc779aSAndroid Build Coastguard Worker   }
832*15dc779aSAndroid Build Coastguard Worker   if (pstr_specific_config->bs_arbitrary_downmix == 2) {
833*15dc779aSAndroid Build Coastguard Worker     return IA_EXHEAACE_EXE_FATAL_MPS_UNSUPPORTED_ARBITARY_DOWNMIX_CODING;
834*15dc779aSAndroid Build Coastguard Worker   }
835*15dc779aSAndroid Build Coastguard Worker   if (pstr_specific_config->tree_description.arbitrary_tree) {
836*15dc779aSAndroid Build Coastguard Worker     return IA_EXHEAACE_EXE_FATAL_MPS_ARBITARY_TREE_NOT_SUPPORTED;
837*15dc779aSAndroid Build Coastguard Worker   }
838*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_spatial_frame->framing_info.bs_framing_type, 1);
839*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, num_param_sets - 1, 1);
840*15dc779aSAndroid Build Coastguard Worker 
841*15dc779aSAndroid Build Coastguard Worker   if (pstr_spatial_frame->framing_info.bs_framing_type) {
842*15dc779aSAndroid Build Coastguard Worker     prev_bs_param_slot = -1;
843*15dc779aSAndroid Build Coastguard Worker 
844*15dc779aSAndroid Build Coastguard Worker     for (param = 0; param < num_param_sets; param++) {
845*15dc779aSAndroid Build Coastguard Worker       bits_param_slot = 0;
846*15dc779aSAndroid Build Coastguard Worker       while ((1 << bits_param_slot) < (pstr_specific_config->bs_frame_length + 1 -
847*15dc779aSAndroid Build Coastguard Worker                                        num_param_sets + param - prev_bs_param_slot)) {
848*15dc779aSAndroid Build Coastguard Worker         bits_param_slot++;
849*15dc779aSAndroid Build Coastguard Worker       }
850*15dc779aSAndroid Build Coastguard Worker 
851*15dc779aSAndroid Build Coastguard Worker       if (bits_param_slot > 0) {
852*15dc779aSAndroid Build Coastguard Worker         ixheaace_write_bits(
853*15dc779aSAndroid Build Coastguard Worker             pstr_bit_buf,
854*15dc779aSAndroid Build Coastguard Worker             pstr_spatial_frame->framing_info.bs_param_slots[param] - prev_bs_param_slot - 1,
855*15dc779aSAndroid Build Coastguard Worker             bits_param_slot);
856*15dc779aSAndroid Build Coastguard Worker       }
857*15dc779aSAndroid Build Coastguard Worker       prev_bs_param_slot = pstr_spatial_frame->framing_info.bs_param_slots[param];
858*15dc779aSAndroid Build Coastguard Worker     }
859*15dc779aSAndroid Build Coastguard Worker   }
860*15dc779aSAndroid Build Coastguard Worker 
861*15dc779aSAndroid Build Coastguard Worker   ixheaace_write_bits(pstr_bit_buf, pstr_spatial_frame->bs_independency_flag, 1);
862*15dc779aSAndroid Build Coastguard Worker 
863*15dc779aSAndroid Build Coastguard Worker   for (box = 0; box < num_ott_boxes; box++) {
864*15dc779aSAndroid Build Coastguard Worker     error = ixheaace_mps_515_ec_data(
865*15dc779aSAndroid Build Coastguard Worker         pstr_bit_buf, pstr_spatial_frame->ott_data.cld[box],
866*15dc779aSAndroid Build Coastguard Worker         pstr_spatial_frame->ott_data.cld_old[box], &(pstr_spatial_frame->cld_lossless_data),
867*15dc779aSAndroid Build Coastguard Worker         IXHEAACE_MPS_SAC_DATA_TYPE_CLD, box, num_param_sets, bs_independency_flag, 0,
868*15dc779aSAndroid Build Coastguard Worker         pstr_specific_config->ott_config[box].bs_ott_bands);
869*15dc779aSAndroid Build Coastguard Worker     if (error) {
870*15dc779aSAndroid Build Coastguard Worker       return error;
871*15dc779aSAndroid Build Coastguard Worker     }
872*15dc779aSAndroid Build Coastguard Worker   }
873*15dc779aSAndroid Build Coastguard Worker   if (!pstr_bsf_instance->spatial_specific_config.bs_one_icc) {
874*15dc779aSAndroid Build Coastguard Worker     for (box = 0; box < num_ott_boxes; box++) {
875*15dc779aSAndroid Build Coastguard Worker       if (!ptr_ott_mode_lfe[box]) {
876*15dc779aSAndroid Build Coastguard Worker         error = ixheaace_mps_515_ec_data(pstr_bit_buf, pstr_spatial_frame->ott_data.icc[box],
877*15dc779aSAndroid Build Coastguard Worker                                          pstr_spatial_frame->ott_data.icc_old[box],
878*15dc779aSAndroid Build Coastguard Worker                                          &(pstr_spatial_frame->cld_lossless_data),
879*15dc779aSAndroid Build Coastguard Worker                                          IXHEAACE_MPS_SAC_DATA_TYPE_ICC, box, num_param_sets,
880*15dc779aSAndroid Build Coastguard Worker                                          bs_independency_flag, 0, pstr_bsf_instance->num_bins);
881*15dc779aSAndroid Build Coastguard Worker         if (error) {
882*15dc779aSAndroid Build Coastguard Worker           return error;
883*15dc779aSAndroid Build Coastguard Worker         }
884*15dc779aSAndroid Build Coastguard Worker       }
885*15dc779aSAndroid Build Coastguard Worker     }
886*15dc779aSAndroid Build Coastguard Worker   } else {
887*15dc779aSAndroid Build Coastguard Worker     error = ixheaace_mps_515_ec_data(pstr_bit_buf, pstr_spatial_frame->ott_data.icc[0],
888*15dc779aSAndroid Build Coastguard Worker                                      pstr_spatial_frame->ott_data.icc_old[0],
889*15dc779aSAndroid Build Coastguard Worker                                      &(pstr_spatial_frame->cld_lossless_data),
890*15dc779aSAndroid Build Coastguard Worker                                      IXHEAACE_MPS_SAC_DATA_TYPE_ICC, 0, num_param_sets,
891*15dc779aSAndroid Build Coastguard Worker                                      bs_independency_flag, 0, pstr_bsf_instance->num_bins);
892*15dc779aSAndroid Build Coastguard Worker     if (error) {
893*15dc779aSAndroid Build Coastguard Worker       return error;
894*15dc779aSAndroid Build Coastguard Worker     }
895*15dc779aSAndroid Build Coastguard Worker   }
896*15dc779aSAndroid Build Coastguard Worker   for (box = 0; box < num_ttt_boxes; box++) {
897*15dc779aSAndroid Build Coastguard Worker     if (pstr_specific_config->ttt_config[box].bs_ttt_mode_low >= 2) {
898*15dc779aSAndroid Build Coastguard Worker       error = ixheaace_mps_515_ec_data(pstr_bit_buf, pstr_spatial_frame->ttt_data.cpc_cld1[box],
899*15dc779aSAndroid Build Coastguard Worker                                        pstr_spatial_frame->ttt_data.cpc_cld1_old[box],
900*15dc779aSAndroid Build Coastguard Worker                                        &(pstr_spatial_frame->cld_lossless_data),
901*15dc779aSAndroid Build Coastguard Worker                                        IXHEAACE_MPS_SAC_DATA_TYPE_CLD, box, num_param_sets,
902*15dc779aSAndroid Build Coastguard Worker                                        bs_independency_flag, 0,
903*15dc779aSAndroid Build Coastguard Worker                                        pstr_specific_config->ttt_config[box].bs_ttt_bands_low);
904*15dc779aSAndroid Build Coastguard Worker       if (error) {
905*15dc779aSAndroid Build Coastguard Worker         return error;
906*15dc779aSAndroid Build Coastguard Worker       }
907*15dc779aSAndroid Build Coastguard Worker       error = ixheaace_mps_515_ec_data(pstr_bit_buf, pstr_spatial_frame->ttt_data.cpc_cld2[box],
908*15dc779aSAndroid Build Coastguard Worker                                        pstr_spatial_frame->ttt_data.cpc_cld2_old[box],
909*15dc779aSAndroid Build Coastguard Worker                                        &(pstr_spatial_frame->cld_lossless_data),
910*15dc779aSAndroid Build Coastguard Worker                                        IXHEAACE_MPS_SAC_DATA_TYPE_CLD, box, num_param_sets,
911*15dc779aSAndroid Build Coastguard Worker                                        bs_independency_flag, 0,
912*15dc779aSAndroid Build Coastguard Worker                                        pstr_specific_config->ttt_config[box].bs_ttt_bands_low);
913*15dc779aSAndroid Build Coastguard Worker       if (error) {
914*15dc779aSAndroid Build Coastguard Worker         return error;
915*15dc779aSAndroid Build Coastguard Worker       }
916*15dc779aSAndroid Build Coastguard Worker     } else {
917*15dc779aSAndroid Build Coastguard Worker       error = ixheaace_mps_515_ec_data(pstr_bit_buf, pstr_spatial_frame->ttt_data.cpc_cld1[box],
918*15dc779aSAndroid Build Coastguard Worker                                        pstr_spatial_frame->ttt_data.cpc_cld1_old[box],
919*15dc779aSAndroid Build Coastguard Worker                                        &(pstr_spatial_frame->cpc_lossless_data),
920*15dc779aSAndroid Build Coastguard Worker                                        IXHEAACE_MPS_SAC_DATA_TYPE_CPC, box, num_param_sets,
921*15dc779aSAndroid Build Coastguard Worker                                        bs_independency_flag, 0,
922*15dc779aSAndroid Build Coastguard Worker                                        pstr_specific_config->ttt_config[box].bs_ttt_bands_low);
923*15dc779aSAndroid Build Coastguard Worker       if (error) {
924*15dc779aSAndroid Build Coastguard Worker         return error;
925*15dc779aSAndroid Build Coastguard Worker       }
926*15dc779aSAndroid Build Coastguard Worker       error = ixheaace_mps_515_ec_data(pstr_bit_buf, pstr_spatial_frame->ttt_data.cpc_cld2[box],
927*15dc779aSAndroid Build Coastguard Worker                                        pstr_spatial_frame->ttt_data.cpc_cld2_old[box],
928*15dc779aSAndroid Build Coastguard Worker                                        &(pstr_spatial_frame->cpc_lossless_data),
929*15dc779aSAndroid Build Coastguard Worker                                        IXHEAACE_MPS_SAC_DATA_TYPE_CPC, box, num_param_sets,
930*15dc779aSAndroid Build Coastguard Worker                                        bs_independency_flag, 0,
931*15dc779aSAndroid Build Coastguard Worker                                        pstr_specific_config->ttt_config[box].bs_ttt_bands_low);
932*15dc779aSAndroid Build Coastguard Worker       if (error) {
933*15dc779aSAndroid Build Coastguard Worker         return error;
934*15dc779aSAndroid Build Coastguard Worker       }
935*15dc779aSAndroid Build Coastguard Worker       error = ixheaace_mps_515_ec_data(
936*15dc779aSAndroid Build Coastguard Worker           pstr_bit_buf, pstr_spatial_frame->ttt_data.icc[box],
937*15dc779aSAndroid Build Coastguard Worker           pstr_spatial_frame->ttt_data.icc_old[box], &(pstr_spatial_frame->icc_lossless_data),
938*15dc779aSAndroid Build Coastguard Worker           IXHEAACE_MPS_SAC_DATA_TYPE_ICC, box, num_param_sets, bs_independency_flag, 0,
939*15dc779aSAndroid Build Coastguard Worker           pstr_specific_config->ttt_config[box].bs_ttt_bands_low);
940*15dc779aSAndroid Build Coastguard Worker       if (error) {
941*15dc779aSAndroid Build Coastguard Worker         return error;
942*15dc779aSAndroid Build Coastguard Worker       }
943*15dc779aSAndroid Build Coastguard Worker     }
944*15dc779aSAndroid Build Coastguard Worker 
945*15dc779aSAndroid Build Coastguard Worker     if (pstr_specific_config->ttt_config[box].bs_ttt_dual_mode) {
946*15dc779aSAndroid Build Coastguard Worker       if (pstr_specific_config->ttt_config[box].bs_ttt_mode_high >= 2) {
947*15dc779aSAndroid Build Coastguard Worker         error = ixheaace_mps_515_ec_data(
948*15dc779aSAndroid Build Coastguard Worker             pstr_bit_buf, pstr_spatial_frame->ttt_data.cpc_cld1[box],
949*15dc779aSAndroid Build Coastguard Worker             pstr_spatial_frame->ttt_data.cpc_cld1_old[box],
950*15dc779aSAndroid Build Coastguard Worker             &(pstr_spatial_frame->cld_lossless_data), IXHEAACE_MPS_SAC_DATA_TYPE_CLD, box,
951*15dc779aSAndroid Build Coastguard Worker             num_param_sets, bs_independency_flag,
952*15dc779aSAndroid Build Coastguard Worker             pstr_specific_config->ttt_config[box].bs_ttt_bands_low, pstr_bsf_instance->num_bins);
953*15dc779aSAndroid Build Coastguard Worker         if (error) {
954*15dc779aSAndroid Build Coastguard Worker           return error;
955*15dc779aSAndroid Build Coastguard Worker         }
956*15dc779aSAndroid Build Coastguard Worker         error = ixheaace_mps_515_ec_data(
957*15dc779aSAndroid Build Coastguard Worker             pstr_bit_buf, pstr_spatial_frame->ttt_data.cpc_cld2[box],
958*15dc779aSAndroid Build Coastguard Worker             pstr_spatial_frame->ttt_data.cpc_cld2_old[box],
959*15dc779aSAndroid Build Coastguard Worker             &(pstr_spatial_frame->cld_lossless_data), IXHEAACE_MPS_SAC_DATA_TYPE_CLD, box,
960*15dc779aSAndroid Build Coastguard Worker             num_param_sets, bs_independency_flag,
961*15dc779aSAndroid Build Coastguard Worker             pstr_specific_config->ttt_config[box].bs_ttt_bands_low, pstr_bsf_instance->num_bins);
962*15dc779aSAndroid Build Coastguard Worker         if (error) {
963*15dc779aSAndroid Build Coastguard Worker           return error;
964*15dc779aSAndroid Build Coastguard Worker         }
965*15dc779aSAndroid Build Coastguard Worker       } else {
966*15dc779aSAndroid Build Coastguard Worker         error = ixheaace_mps_515_ec_data(
967*15dc779aSAndroid Build Coastguard Worker             pstr_bit_buf, pstr_spatial_frame->ttt_data.cpc_cld1[box],
968*15dc779aSAndroid Build Coastguard Worker             pstr_spatial_frame->ttt_data.cpc_cld1_old[box],
969*15dc779aSAndroid Build Coastguard Worker             &(pstr_spatial_frame->cpc_lossless_data), IXHEAACE_MPS_SAC_DATA_TYPE_CPC, box,
970*15dc779aSAndroid Build Coastguard Worker             num_param_sets, bs_independency_flag,
971*15dc779aSAndroid Build Coastguard Worker             pstr_specific_config->ttt_config[box].bs_ttt_bands_low, pstr_bsf_instance->num_bins);
972*15dc779aSAndroid Build Coastguard Worker         if (error) {
973*15dc779aSAndroid Build Coastguard Worker           return error;
974*15dc779aSAndroid Build Coastguard Worker         }
975*15dc779aSAndroid Build Coastguard Worker         error = ixheaace_mps_515_ec_data(
976*15dc779aSAndroid Build Coastguard Worker             pstr_bit_buf, pstr_spatial_frame->ttt_data.cpc_cld2[box],
977*15dc779aSAndroid Build Coastguard Worker             pstr_spatial_frame->ttt_data.cpc_cld2_old[box],
978*15dc779aSAndroid Build Coastguard Worker             &(pstr_spatial_frame->cpc_lossless_data), IXHEAACE_MPS_SAC_DATA_TYPE_CPC, box,
979*15dc779aSAndroid Build Coastguard Worker             num_param_sets, bs_independency_flag,
980*15dc779aSAndroid Build Coastguard Worker             pstr_specific_config->ttt_config[box].bs_ttt_bands_low, pstr_bsf_instance->num_bins);
981*15dc779aSAndroid Build Coastguard Worker         if (error) {
982*15dc779aSAndroid Build Coastguard Worker           return error;
983*15dc779aSAndroid Build Coastguard Worker         }
984*15dc779aSAndroid Build Coastguard Worker         error = ixheaace_mps_515_ec_data(
985*15dc779aSAndroid Build Coastguard Worker             pstr_bit_buf, pstr_spatial_frame->ttt_data.icc[box],
986*15dc779aSAndroid Build Coastguard Worker             pstr_spatial_frame->ttt_data.icc_old[box], &(pstr_spatial_frame->icc_lossless_data),
987*15dc779aSAndroid Build Coastguard Worker             IXHEAACE_MPS_SAC_DATA_TYPE_ICC, box, num_param_sets, bs_independency_flag,
988*15dc779aSAndroid Build Coastguard Worker             pstr_specific_config->ttt_config[box].bs_ttt_bands_low, pstr_bsf_instance->num_bins);
989*15dc779aSAndroid Build Coastguard Worker         if (error) {
990*15dc779aSAndroid Build Coastguard Worker           return error;
991*15dc779aSAndroid Build Coastguard Worker         }
992*15dc779aSAndroid Build Coastguard Worker       }
993*15dc779aSAndroid Build Coastguard Worker     }
994*15dc779aSAndroid Build Coastguard Worker   }
995*15dc779aSAndroid Build Coastguard Worker   for (param = 0; param < num_param_sets; param++) {
996*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_spatial_frame->smg_data.bs_smooth_mode[param], 2);
997*15dc779aSAndroid Build Coastguard Worker 
998*15dc779aSAndroid Build Coastguard Worker     if (pstr_spatial_frame->smg_data.bs_smooth_mode[param] >= 2) {
999*15dc779aSAndroid Build Coastguard Worker       ixheaace_write_bits(pstr_bit_buf, pstr_spatial_frame->smg_data.bs_smooth_time[param], 2);
1000*15dc779aSAndroid Build Coastguard Worker     }
1001*15dc779aSAndroid Build Coastguard Worker     if (pstr_spatial_frame->smg_data.bs_smooth_mode[param] == 3) {
1002*15dc779aSAndroid Build Coastguard Worker       ixheaace_write_bits(pstr_bit_buf, pstr_spatial_frame->smg_data.bs_freq_res_stride[param],
1003*15dc779aSAndroid Build Coastguard Worker                           2);
1004*15dc779aSAndroid Build Coastguard Worker       for (bin = 0; bin < pstr_bsf_instance->num_bins;
1005*15dc779aSAndroid Build Coastguard Worker            bin += pstr_spatial_frame->smg_data.bs_freq_res_stride[param]) {
1006*15dc779aSAndroid Build Coastguard Worker         ixheaace_write_bits(pstr_bit_buf, pstr_spatial_frame->smg_data.bs_smg_data[param][bin],
1007*15dc779aSAndroid Build Coastguard Worker                             1);
1008*15dc779aSAndroid Build Coastguard Worker       }
1009*15dc779aSAndroid Build Coastguard Worker     }
1010*15dc779aSAndroid Build Coastguard Worker   }
1011*15dc779aSAndroid Build Coastguard Worker   if (pstr_specific_config->bs_temp_shape_config != 0) {
1012*15dc779aSAndroid Build Coastguard Worker     ixheaace_write_bits(pstr_bit_buf, pstr_spatial_frame->temp_shape_data.bs_temp_shape_enable,
1013*15dc779aSAndroid Build Coastguard Worker                         1);
1014*15dc779aSAndroid Build Coastguard Worker     if (pstr_spatial_frame->temp_shape_data.bs_temp_shape_enable) {
1015*15dc779aSAndroid Build Coastguard Worker       num_temp_shape_chan = temp_shape_chan_table[pstr_specific_config->bs_temp_shape_config - 1]
1016*15dc779aSAndroid Build Coastguard Worker                                                  [pstr_specific_config->bs_tree_config];
1017*15dc779aSAndroid Build Coastguard Worker       for (ch = 0; ch < num_temp_shape_chan; ch++) {
1018*15dc779aSAndroid Build Coastguard Worker         ixheaace_write_bits(pstr_bit_buf,
1019*15dc779aSAndroid Build Coastguard Worker                             pstr_spatial_frame->temp_shape_data.bs_temp_shape_enable_channel[ch],
1020*15dc779aSAndroid Build Coastguard Worker                             1);
1021*15dc779aSAndroid Build Coastguard Worker       }
1022*15dc779aSAndroid Build Coastguard Worker       if (pstr_specific_config->bs_temp_shape_config == 2) {
1023*15dc779aSAndroid Build Coastguard Worker         return IA_EXHEAACE_EXE_FATAL_MPS_UNSUPPORTED_GUIDED_ENV_SHAPE;
1024*15dc779aSAndroid Build Coastguard Worker       }
1025*15dc779aSAndroid Build Coastguard Worker     }
1026*15dc779aSAndroid Build Coastguard Worker   }
1027*15dc779aSAndroid Build Coastguard Worker 
1028*15dc779aSAndroid Build Coastguard Worker   ixheaace_byte_align_buffer(pstr_bit_buf);
1029*15dc779aSAndroid Build Coastguard Worker 
1030*15dc779aSAndroid Build Coastguard Worker   return IA_NO_ERROR;
1031*15dc779aSAndroid Build Coastguard Worker }