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