xref: /aosp_15_r20/external/libxaac/encoder/iusace_psy_mod.h (revision 15dc779a375ca8b5125643b829a8aa4b70d7f451)
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 #pragma once
22 #include <stdio.h>
23 
24 #define MAX_NUM_GROUPED_SFB (60)
25 #define MAX_BARK_VALUE (24.0f)
26 #define MASK_LOW_FAC (3.0f)
27 #define MASK_HIGH_FAC (1.5f)
28 #define MASK_LOW_SP_ENERGY_L (3.0f)
29 #define MASK_HIGH_SP_ENERGY_L (2.0f)
30 #define MASK_HIGH_SP_ENERGY_L_LBR (1.5f)
31 #define MASK_LOW_SP_ENERGY_S (2.0f)
32 #define MASK_HIGH_SP_ENERGY_S (1.5f)
33 #define C_RATIO (0.001258925f)
34 
35 #define MAXIMUM_SCALE_FACTOR_BAND_LONG 51
36 #define MAXIMUM_SCALE_FACTOR_BAND_SHORT 15
37 
38 #define MAX_GROUPED_SFB 51
39 #define MAX_GROUPED_SFB_TEMP 60
40 #define BLOCK_SWITCHING_OFFSET (1 * 1024 + 3 * 128 + 64 + 128)
41 #define MAX_CHANNEL_BITS 6144
42 #define MAX_SFB_SHORT 15
43 
44 #define TRANS_FAC 8
45 #ifndef FRAME_LEN_SHORT_128
46 #define FRAME_LEN_SHORT_128 (FRAME_LEN_LONG / TRANS_FAC)
47 #endif
48 
49 typedef struct {
50   WORD32 sfb_count;
51   WORD32 sfb_active;
52   WORD32 sfb_offset[MAXIMUM_SCALE_FACTOR_BAND_LONG + 1];
53   FLOAT32 sfb_thr_quiet[MAXIMUM_SCALE_FACTOR_BAND_LONG];
54   FLOAT32 max_allowed_inc_fac;
55   FLOAT32 min_remaining_thr_fac;
56   WORD32 low_pass_line;
57   FLOAT32 clip_energy;
58   FLOAT32 ratio;
59   FLOAT32 sfb_mask_low_fac[MAXIMUM_SCALE_FACTOR_BAND_LONG];
60   FLOAT32 sfb_mask_high_fac[MAXIMUM_SCALE_FACTOR_BAND_LONG];
61   FLOAT32 sfb_mask_low_fac_spr_ener[MAXIMUM_SCALE_FACTOR_BAND_LONG];
62   FLOAT32 sfb_mask_high_fac_spr_ener[MAXIMUM_SCALE_FACTOR_BAND_LONG];
63   FLOAT32 sfb_min_snr[MAXIMUM_SCALE_FACTOR_BAND_LONG];
64 } ia_psy_mod_long_config_struct;
65 
66 typedef struct {
67   WORD32 sfb_count;
68   WORD32 sfb_active;
69   WORD32 sfb_offset[MAXIMUM_SCALE_FACTOR_BAND_SHORT + 1];
70   FLOAT32 sfb_thr_quiet[MAXIMUM_SCALE_FACTOR_BAND_SHORT];
71   FLOAT32 max_allowed_inc_fac;
72   FLOAT32 min_remaining_thr_fac;
73   WORD32 low_pass_line;
74   FLOAT32 clip_energy;
75   FLOAT32 ratio;
76   FLOAT32 sfb_mask_low_fac[MAXIMUM_SCALE_FACTOR_BAND_SHORT];
77   FLOAT32 sfb_mask_high_fac[MAXIMUM_SCALE_FACTOR_BAND_SHORT];
78   FLOAT32 sfb_mask_low_fac_spr_ener[MAXIMUM_SCALE_FACTOR_BAND_SHORT];
79   FLOAT32 sfb_mask_high_fac_spr_ener[MAXIMUM_SCALE_FACTOR_BAND_SHORT];
80   FLOAT32 sfb_min_snr[MAXIMUM_SCALE_FACTOR_BAND_SHORT];
81 } ia_psy_mod_short_config_struct;
82 
83 typedef struct {
84   WORD32 sfb_count;
85   WORD32 max_sfb_per_grp;
86   WORD32 sfb_per_group;
87   WORD32 window_sequence;
88   WORD32 window_shape;
89   WORD32 sfb_offsets[100];
90   FLOAT32 *ptr_sfb_energy;
91   FLOAT32 *ptr_sfb_spread_energy;
92   FLOAT32 *ptr_sfb_thr;
93   FLOAT64 *ptr_spec_coeffs;
94   FLOAT32 sfb_sum_lr_energy;
95   FLOAT32 pe;
96   FLOAT32 sfb_min_snr[100];
97   WORD32 ms_used[100];
98 } ia_psy_mod_out_data_struct;
99 
100 typedef struct {
101   WORD32 window_sequence;
102   FLOAT32 sfb_thr_nm1[MAX_GROUPED_SFB_TEMP];
103   FLOAT32 *ptr_sfb_thr_long;
104   FLOAT32 sfb_thr_short[TRANS_FAC][MAXIMUM_SCALE_FACTOR_BAND_SHORT];
105   FLOAT32 *ptr_sfb_energy_long;
106   FLOAT32 ptr_sfb_energy_long_ms[MAX_GROUPED_SFB_TEMP];
107   FLOAT32 ptr_sfb_energy_short_ms[TRANS_FAC][MAXIMUM_SCALE_FACTOR_BAND_SHORT];
108   FLOAT32 sfb_energy_short[TRANS_FAC][MAXIMUM_SCALE_FACTOR_BAND_SHORT];
109   FLOAT32 *ptr_sfb_spreaded_energy_long;
110   FLOAT32 sfb_spreaded_energy_short[TRANS_FAC][MAXIMUM_SCALE_FACTOR_BAND_SHORT];
111 } ia_psy_mod_data_struct;
112 
113 typedef struct ia_psy_mod_struct {
114   ia_psy_mod_long_config_struct str_psy_long_config[MAX_TIME_CHANNELS];
115   ia_psy_mod_short_config_struct str_psy_short_config[MAX_TIME_CHANNELS];
116   ia_psy_mod_data_struct str_psy_data[MAX_TIME_CHANNELS];
117   ia_psy_mod_out_data_struct str_psy_out_data[MAX_TIME_CHANNELS];
118   FLOAT32 mdct_spec_coeff_buf[MAX_TIME_CHANNELS][1024];
119 } ia_psy_mod_struct;
120 
121 typedef struct ia_sfb_params_struct {
122   WORD32 num_sfb[MAX_TIME_CHANNELS];
123   WORD32 max_sfb[MAX_TIME_CHANNELS];
124   WORD32 max_sfb_ste;
125   WORD32 sfb_width_table[MAX_TIME_CHANNELS][MAX_SFB_LONG];
126   WORD32 grouped_sfb_offset[MAX_TIME_CHANNELS][MAX_SF_BANDS + 1];
127   WORD32 sfb_offset[MAX_TIME_CHANNELS][MAX_SF_BANDS + 1];
128   WORD32 num_window_groups[MAX_TIME_CHANNELS];
129   WORD32 window_group_length[MAX_TIME_CHANNELS][8];
130   WORD32 window_shape[MAX_TIME_CHANNELS];
131   WORD32 window_sequence[MAX_TIME_CHANNELS];
132   WORD32 common_win[MAX_TIME_CHANNELS];
133 
134 } ia_sfb_params_struct;
135 
136 VOID iusace_psy_mod_init(ia_psy_mod_struct *pstr_psy_mod, WORD32 sample_rate, WORD32 bit_rate,
137                          WORD32 band_width, WORD32 num_channels, WORD32 ch, WORD32 ele_id,
138                          WORD32 ccfl);
139 
140 VOID iusace_psy_mod_sb(ia_psy_mod_struct *pstr_psy_mod, ia_sfb_params_struct *pstr_sfb_prms,
141                        FLOAT64 *ptr_spec_in, ia_tns_info *pstr_tns_info[MAX_TIME_CHANNELS],
142                        WORD32 tns_select, WORD32 i_ch, WORD32 chn, WORD32 channel_type,
143                        FLOAT64 *scratch_tns_filter, WORD32 elem_idx, FLOAT64 *ptr_tns_scratch,
144                        WORD32 ccfl);
145 
146 VOID iusace_psy_mod_lb(ia_psy_mod_struct *pstr_psy_mod, ia_sfb_params_struct *pstr_sfb_prms,
147                        FLOAT64 *ptr_spec_in, ia_tns_info *pstr_tns_info[MAX_TIME_CHANNELS],
148                        WORD32 tns_select, WORD32 i_ch, WORD32 chn, WORD32 channel_type,
149                        FLOAT64 *scratch_tns_filter, WORD32 elem_idx, FLOAT64 *ptr_tns_scratch,
150                        WORD32 ccfl);
151