xref: /aosp_15_r20/external/libxaac/encoder/iusace_fd_qc_adjthr.h (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 #pragma once
22*15dc779aSAndroid Build Coastguard Worker #define RED_EXP_VAL 0.25f
23*15dc779aSAndroid Build Coastguard Worker #define INV_RED_EXP_VAL (1.0f / RED_EXP_VAL)
24*15dc779aSAndroid Build Coastguard Worker #define MIN_SNR_LIMIT 0.8f
25*15dc779aSAndroid Build Coastguard Worker 
26*15dc779aSAndroid Build Coastguard Worker #define MAX_SCF_DELTA 60
27*15dc779aSAndroid Build Coastguard Worker 
28*15dc779aSAndroid Build Coastguard Worker #define LOG2_1 1.442695041f
29*15dc779aSAndroid Build Coastguard Worker #define C1_SF -69.33295f /* -16/3*log(MAX_QUANT+0.5-logCon)/log(2) */
30*15dc779aSAndroid Build Coastguard Worker #define C2_SF 5.77078f   /* 4/log(2) */
31*15dc779aSAndroid Build Coastguard Worker 
32*15dc779aSAndroid Build Coastguard Worker #define PE_C1 3.0f       /* log(8.0)/log(2) */
33*15dc779aSAndroid Build Coastguard Worker #define PE_C2 1.3219281f /* log(2.5)/log(2) */
34*15dc779aSAndroid Build Coastguard Worker #define PE_C3 0.5593573f /* 1-C2/C1 */
35*15dc779aSAndroid Build Coastguard Worker 
36*15dc779aSAndroid Build Coastguard Worker #define CLIP_SAVE_LO_TO_HI_LONG (CLIP_SAVE_HI_LONG - CLIP_SAVE_LO_LONG)
37*15dc779aSAndroid Build Coastguard Worker #define CLIP_SAVE_LO_TO_HI_SHORT (CLIP_SAVE_HI_SHORT - CLIP_SAVE_LO_SHORT)
38*15dc779aSAndroid Build Coastguard Worker #define CLIP_SPEND_LO_TO_HI_LONG (CLIP_SPEND_HI_LONG - CLIP_SPEND_LO_LONG)
39*15dc779aSAndroid Build Coastguard Worker #define CLIP_SPEND_LO_TO_HI_SHORT (CLIP_SPEND_HI_SHORT - CLIP_SPEND_LO_SHORT)
40*15dc779aSAndroid Build Coastguard Worker #define MIN_TO_MAX_SAVE_BITS_LONG (MAX_BITS_SAVE_LONG - MIN_BITS_SAVE_LONG)
41*15dc779aSAndroid Build Coastguard Worker #define MIN_TO_MAX_SAVE_BITS_SHORT (MAX_BITS_SAVE_SHORT - MIN_BITS_SAVE_SHORT)
42*15dc779aSAndroid Build Coastguard Worker #define MIN_TO_MAX_SPEND_BITS_LONG (MAX_BITS_SPEND_LONG - MIN_BITS_SPEND_LONG)
43*15dc779aSAndroid Build Coastguard Worker #define MIN_TO_MAX_SPEND_BITS_SHORT (MAX_BITS_SPEND_SHORT - MIN_BITS_SPEND_SHORT)
44*15dc779aSAndroid Build Coastguard Worker #define BITS_SAVE_RATIO_LONG (MIN_TO_MAX_SAVE_BITS_LONG / CLIP_SAVE_LO_TO_HI_LONG)
45*15dc779aSAndroid Build Coastguard Worker #define BITS_SAVE_RATIO_SHORT (MIN_TO_MAX_SAVE_BITS_SHORT / CLIP_SAVE_LO_TO_HI_SHORT)
46*15dc779aSAndroid Build Coastguard Worker #define BITS_SPEND_RATIO_LONG (MIN_TO_MAX_SPEND_BITS_LONG / CLIP_SPEND_LO_TO_HI_LONG)
47*15dc779aSAndroid Build Coastguard Worker #define BITS_SPEND_RATIO_SHORT (MIN_TO_MAX_SPEND_BITS_SHORT / CLIP_SPEND_LO_TO_HI_SHORT)
48*15dc779aSAndroid Build Coastguard Worker 
49*15dc779aSAndroid Build Coastguard Worker #define MAX_GAIN_INDEX (128)
50*15dc779aSAndroid Build Coastguard Worker #define MIN_GAIN_INDEX (-128)
51*15dc779aSAndroid Build Coastguard Worker #define SCF_COUNT_LIMIT_ONE (1)
52*15dc779aSAndroid Build Coastguard Worker #define SCF_COUNT_LIMIT_THREE (3)
53*15dc779aSAndroid Build Coastguard Worker 
54*15dc779aSAndroid Build Coastguard Worker typedef struct {
55*15dc779aSAndroid Build Coastguard Worker   FLOAT32 *sfb_ld_energy;
56*15dc779aSAndroid Build Coastguard Worker   FLOAT32 *sfb_lines;
57*15dc779aSAndroid Build Coastguard Worker   FLOAT32 sfb_pe[MAX_GROUPED_SFB_TEMP];
58*15dc779aSAndroid Build Coastguard Worker   FLOAT32 sfb_const_part[MAX_GROUPED_SFB_TEMP];
59*15dc779aSAndroid Build Coastguard Worker   FLOAT32 num_sfb_active_lines[MAX_GROUPED_SFB_TEMP];
60*15dc779aSAndroid Build Coastguard Worker   FLOAT32 pe;
61*15dc779aSAndroid Build Coastguard Worker   FLOAT32 const_part;
62*15dc779aSAndroid Build Coastguard Worker   FLOAT32 num_active_lines;
63*15dc779aSAndroid Build Coastguard Worker } ia_qc_pe_chan_data_struct;
64*15dc779aSAndroid Build Coastguard Worker 
65*15dc779aSAndroid Build Coastguard Worker typedef struct {
66*15dc779aSAndroid Build Coastguard Worker   ia_qc_pe_chan_data_struct pe_ch_data[30];
67*15dc779aSAndroid Build Coastguard Worker   FLOAT32 pe;
68*15dc779aSAndroid Build Coastguard Worker   FLOAT32 const_part;
69*15dc779aSAndroid Build Coastguard Worker   FLOAT32 num_active_lines;
70*15dc779aSAndroid Build Coastguard Worker   FLOAT32 offset;
71*15dc779aSAndroid Build Coastguard Worker } ia_qc_pe_data_struct;
72*15dc779aSAndroid Build Coastguard Worker 
73*15dc779aSAndroid Build Coastguard Worker enum ia_avoid_hole_state { NO_AH = 0, AH_INACTIVE = 1, AH_ACTIVE = 2 };
74*15dc779aSAndroid Build Coastguard Worker 
75*15dc779aSAndroid Build Coastguard Worker typedef enum {
76*15dc779aSAndroid Build Coastguard Worker   SI_ID_BITS = (3),
77*15dc779aSAndroid Build Coastguard Worker   SI_FILL_COUNT_BITS = (4),
78*15dc779aSAndroid Build Coastguard Worker   SI_FILL_ESC_COUNT_BITS = (8),
79*15dc779aSAndroid Build Coastguard Worker   SI_FILL_EXTENTION_BITS = (4),
80*15dc779aSAndroid Build Coastguard Worker   SI_FILL_NIBBLE_BITS = (4),
81*15dc779aSAndroid Build Coastguard Worker   SI_SCE_BITS = (4),
82*15dc779aSAndroid Build Coastguard Worker   SI_CPE_BITS = (5),
83*15dc779aSAndroid Build Coastguard Worker   SI_CPE_MS_MASK_BITS = (2),
84*15dc779aSAndroid Build Coastguard Worker   SI_ICS_INFO_BITS_LONG = (1 + 2 + 6),
85*15dc779aSAndroid Build Coastguard Worker   SI_ICS_INFO_BITS_SHORT = (1 + 2 + 4 + 7),
86*15dc779aSAndroid Build Coastguard Worker   SI_ICS_BITS = (8 + 1 + 1 + 1),
87*15dc779aSAndroid Build Coastguard Worker } SI_BITS;
88*15dc779aSAndroid Build Coastguard Worker 
89*15dc779aSAndroid Build Coastguard Worker FLOAT32 iusace_bits_to_pe(const FLOAT32 bits);
90*15dc779aSAndroid Build Coastguard Worker 
91*15dc779aSAndroid Build Coastguard Worker VOID iusace_adj_thr_init(ia_adj_thr_elem_struct *pstr_adj_thr_state, const FLOAT32 mean_pe,
92*15dc779aSAndroid Build Coastguard Worker                          WORD32 ch_bitrate);
93*15dc779aSAndroid Build Coastguard Worker 
94*15dc779aSAndroid Build Coastguard Worker IA_ERRORCODE iusace_adj_thr(ia_adj_thr_elem_struct *pstr_adj_thr_elem,
95*15dc779aSAndroid Build Coastguard Worker                             ia_psy_mod_out_data_struct *pstr_psy_out, FLOAT32 *ch_bit_dist,
96*15dc779aSAndroid Build Coastguard Worker                             ia_qc_out_data_struct *pstr_qc_out, const WORD32 avg_bits,
97*15dc779aSAndroid Build Coastguard Worker                             const WORD32 bitres_bits, const WORD32 max_bitres_bits,
98*15dc779aSAndroid Build Coastguard Worker                             const WORD32 side_info_bits, FLOAT32 *max_bit_fac,
99*15dc779aSAndroid Build Coastguard Worker                             WORD32 num_channels, WORD32 chn, iusace_scratch_mem *pstr_scratch);
100*15dc779aSAndroid Build Coastguard Worker 
101*15dc779aSAndroid Build Coastguard Worker VOID iusace_calc_form_fac_per_chan(ia_psy_mod_out_data_struct *pstr_psy_out_chan,
102*15dc779aSAndroid Build Coastguard Worker                                    iusace_scratch_mem *pstr_scratch, WORD32 i_ch);
103*15dc779aSAndroid Build Coastguard Worker 
104*15dc779aSAndroid Build Coastguard Worker VOID iusace_estimate_scfs_chan(ia_psy_mod_out_data_struct *pstr_psy_out,
105*15dc779aSAndroid Build Coastguard Worker                                ia_qc_out_chan_struct *str_qc_out_chan, WORD32 num_channels,
106*15dc779aSAndroid Build Coastguard Worker                                WORD32 chn, iusace_scratch_mem *pstr_scratch);
107*15dc779aSAndroid Build Coastguard Worker 
108*15dc779aSAndroid Build Coastguard Worker VOID iusace_quantize_lines(const WORD32 gain, const WORD32 num_lines, FLOAT32 *ptr_exp_spectrum,
109*15dc779aSAndroid Build Coastguard Worker                            WORD16 *ptr_quant_spectrum, FLOAT32 *ptr_mdct_spec);
110