1 /******************************************************************************
2 * *
3 * Copyright (C) 2023 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************
18 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20
21 #include <stddef.h>
22 #include "ixheaac_type_def.h"
23 #include "ixheaace_aac_constants.h"
24 #include "impd_drc_common_enc.h"
25 #include "impd_drc_uni_drc.h"
26 #include "impd_drc_tables.h"
27 #include "impd_drc_api.h"
28 #include "ixheaace_api.h"
29 #include "ixheaace_adjust_threshold_data.h"
30 #include "ixheaace_psy_const.h"
31 #include "ixheaace_tns.h"
32 #include "ixheaace_tns_params.h"
33 #include "ixheaace_rom.h"
34 #include "ixheaace_common_rom.h"
35 #include "ixheaace_bitbuffer.h"
36
37 #include "ixheaace_dynamic_bits.h"
38 #include "ixheaace_qc_data.h"
39
40 #include "ixheaace_channel_map.h"
41 #include "ixheaace_block_switch.h"
42
43 #include "ixheaace_psy_data.h"
44 #include "ixheaace_interface.h"
45 #include "ixheaace_write_bitstream.h"
46
ia_enhaacplus_enc_count_ms_mask_bits(WORD32 sfb_cnt,WORD32 sfb_per_group,WORD32 max_sfb_per_grp,ixheaace_tools_info * pstr_tools_info)47 static WORD32 ia_enhaacplus_enc_count_ms_mask_bits(WORD32 sfb_cnt, WORD32 sfb_per_group,
48 WORD32 max_sfb_per_grp,
49 ixheaace_tools_info *pstr_tools_info) {
50 WORD32 ms_bits = 0, sfb_off;
51
52 switch (pstr_tools_info->ms_digest) {
53 case MS_NONE:
54 case MS_ALL:
55 break;
56
57 case MS_SOME:
58
59 for (sfb_off = 0; sfb_off < sfb_cnt; sfb_off += sfb_per_group) {
60 ms_bits += max_sfb_per_grp;
61 }
62 break;
63 }
64
65 return ms_bits;
66 }
67
ia_enhaacplus_enc_tns_count(ixheaace_temporal_noise_shaping_params * pstr_tns_info,WORD32 block_type)68 static WORD32 ia_enhaacplus_enc_tns_count(ixheaace_temporal_noise_shaping_params *pstr_tns_info,
69 WORD32 block_type) {
70 WORD32 i, k;
71 WORD32 tns_present;
72 WORD32 num_windows;
73 WORD32 count;
74 WORD32 coef_bits;
75
76 count = 0;
77 num_windows = (block_type == 2 ? 8 : 1);
78 tns_present = 0;
79
80 for (i = 0; i < num_windows; i++) {
81 if (pstr_tns_info->tns_active[i] == 1) {
82 tns_present = 1;
83 }
84 }
85
86 if (tns_present == 1) {
87 for (i = 0; i < num_windows; i++) {
88 count += (block_type == SHORT_WINDOW ? 1 : 2);
89
90 if (pstr_tns_info->tns_active[i]) {
91 count += (block_type == SHORT_WINDOW ? 8 : 12);
92
93 if (pstr_tns_info->order[i]) {
94 count += 2; /*coef_compression */
95
96 if (pstr_tns_info->coef_res[i] == 4) {
97 coef_bits = 3;
98
99 for (k = 0; k < pstr_tns_info->order[i]; k++) {
100 if (pstr_tns_info->coef[i * TEMPORAL_NOISE_SHAPING_MAX_ORDER_SHORT + k] > 3 ||
101 pstr_tns_info->coef[i * TEMPORAL_NOISE_SHAPING_MAX_ORDER_SHORT + k] < -4) {
102 coef_bits = 4;
103 break;
104 }
105 }
106 } else {
107 coef_bits = 2;
108
109 for (k = 0; k < pstr_tns_info->order[i]; k++) {
110 if (pstr_tns_info->coef[i * TEMPORAL_NOISE_SHAPING_MAX_ORDER_SHORT + k] > 1 ||
111 pstr_tns_info->coef[i * TEMPORAL_NOISE_SHAPING_MAX_ORDER_SHORT + k] < -2) {
112 coef_bits = 3;
113 break;
114 }
115 }
116 }
117
118 for (k = 0; k < pstr_tns_info->order[i]; k++) {
119 count += coef_bits;
120 }
121 }
122 }
123 }
124 }
125
126 return count;
127 }
128
ia_enhaacplus_enc_count_tns_bits(ixheaace_temporal_noise_shaping_params * pstr_tns_info,WORD32 block_type)129 static WORD32 ia_enhaacplus_enc_count_tns_bits(
130 ixheaace_temporal_noise_shaping_params *pstr_tns_info, WORD32 block_type) {
131 return (ia_enhaacplus_enc_tns_count(pstr_tns_info, block_type));
132 }
133
ia_enhaacplus_enc_count_static_bitdemand(ixheaace_psy_out_channel ** psy_out_ch,ixheaace_psy_out_element * pstr_psy_out_element,WORD32 channels,WORD32 aot,WORD32 adts_flag,WORD32 stat_bits_flag,WORD32 flag_last_element)134 WORD32 ia_enhaacplus_enc_count_static_bitdemand(
135 ixheaace_psy_out_channel **psy_out_ch,
136 ixheaace_psy_out_element *pstr_psy_out_element, WORD32 channels, WORD32 aot, WORD32 adts_flag,
137 WORD32 stat_bits_flag, WORD32 flag_last_element) {
138 WORD32 static_bits = 0;
139 WORD32 ch;
140
141 switch (channels) {
142 case 1:
143
144 static_bits += SI_ID_BITS + SI_SCE_BITS + SI_ICS_BITS;
145
146 static_bits += ia_enhaacplus_enc_count_tns_bits(&(psy_out_ch[0]->tns_info),
147 psy_out_ch[0]->window_sequence);
148 switch (psy_out_ch[0]->window_sequence) {
149 case LONG_WINDOW:
150 case START_WINDOW:
151 case STOP_WINDOW:
152
153 static_bits += SI_ICS_INFO_BITS_LONG;
154 break;
155
156 case SHORT_WINDOW:
157
158 static_bits += SI_ICS_INFO_BITS_SHORT;
159 break;
160 }
161 break;
162
163 case 2:
164 static_bits += SI_ID_BITS + SI_CPE_BITS + 2 * SI_ICS_BITS;
165
166 static_bits += SI_CPE_MS_MASK_BITS;
167
168 static_bits += ia_enhaacplus_enc_count_ms_mask_bits(
169 psy_out_ch[0]->sfb_count, psy_out_ch[0]->sfb_per_group, psy_out_ch[0]->max_sfb_per_grp,
170 &pstr_psy_out_element->tools_info);
171
172 switch (psy_out_ch[0]->window_sequence) {
173 case LONG_WINDOW:
174 case START_WINDOW:
175 case STOP_WINDOW:
176
177 static_bits += SI_ICS_INFO_BITS_LONG;
178 break;
179
180 case SHORT_WINDOW:
181
182 static_bits += SI_ICS_INFO_BITS_SHORT;
183 break;
184 }
185
186 for (ch = 0; ch < 2; ch++) {
187 static_bits += ia_enhaacplus_enc_count_tns_bits(&(psy_out_ch[ch]->tns_info),
188 psy_out_ch[ch]->window_sequence);
189 }
190
191 break;
192 }
193
194 if (aot == AOT_AAC_LC || aot == AOT_SBR || aot == AOT_PS) {
195 if (!(adts_flag)) {
196 return static_bits + stat_bits_flag * 8;
197 }
198
199 if (adts_flag && (stat_bits_flag) && (flag_last_element)) {
200 return static_bits + 56;
201 } else {
202 return static_bits;
203 }
204 } else {
205 return static_bits; // Default Case
206 }
207 }
208