xref: /aosp_15_r20/external/libxaac/encoder/drc_src/impd_drc_gain_enc.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 "impd_drc_tables.h"
23 
24 #define N_UNIDRC_GAIN_MAX (MAX_NODE_COUNT)
25 
26 #ifndef M_LOG2_10
27 #define M_LOG2_10 (3.32192809488736234787) /* log_2 10 */
28 #endif
29 
30 #ifndef M_LOG10_E
31 #define M_LOG10_E (0.4342944819) /* log_10 e */
32 #endif
33 
34 #define M_LN10_DIV_20 (0.115129254649702284201) /* (log_e 10  / 20) */
35 
36 #define EXP10(x) (exp2(M_LOG2_10 * x))
37 
38 #define MAX_NUM_CHANNELS (32)
39 #define STFT256_HOP_SIZE (256)
40 #define IMPD_DRCSPECTRAL_FLOOR (0.02818383f)
41 
42 #define IMPD_DRCMAX_NSEQ (40)
43 #define IMPD_DRCMAX_FRAMESIZE (4096)
44 #define IMPD_DRCCOMPAND_MAX_NB_POINTS (MAX_NUM_CHANNELS << 3)
45 #define IMPD_DRCCOMPAND_MAX_NUM_SEGMENTS ((IMPD_DRCCOMPAND_MAX_NB_POINTS + 4) << 1)
46 
47 #define MAX_TIME_DEVIATION_FACTOR (0.25f)
48 #define SLOPE_CHANGE_THR (3.0f)
49 #define SLOPE_QUANT_THR (8.0f)
50 
51 #define GAIN_QUANT_STEP_SIZE (0.125f)
52 #define GAIN_QUANT_STEP_SIZE_INV (8.0f)
53 
54 #define MAX_DRC_GAIN_DELTA_BEFORE_QUANT (1.0f + 0.5f * GAIN_QUANT_STEP_SIZE)
55 
56 #define SCALE_APPROXIMATE_DB \
57   (0.99657842f) /* factor for converting dB to approximate dB: log2(10)*6/20 */
58 
59 typedef struct ia_drc_compand_chan_param_struct {
60   FLOAT64 attack;
61   FLOAT64 decay;
62   FLOAT64 volume;
63 } ia_drc_compand_chan_param_struct;
64 
65 typedef struct ia_drc_compand_segment_struct {
66   FLOAT64 x, y;
67   FLOAT64 a, b;
68 } ia_drc_compand_segment_struct;
69 
70 typedef struct ia_drc_compand_struct {
71   UWORD8 is_valid;
72   UWORD32 ch_idx;
73   UWORD32 nb_points;
74   UWORD32 nb_segments;
75   ia_drc_compand_segment_struct str_segment[IMPD_DRCCOMPAND_MAX_NUM_SEGMENTS];
76   ia_drc_compand_chan_param_struct str_channel_param;
77   FLOAT64 in_min_lin;
78   FLOAT64 out_min_lin;
79   FLOAT64 width_db;
80   FLOAT64 gain_db;
81   FLOAT64 initial_volume;
82 } ia_drc_compand_struct;
83 
84 typedef struct ia_drc_stft_gain_calc_struct {
85   UWORD8 is_valid;
86   UWORD32 ch_idx;
87   FLOAT32 theshold;
88   FLOAT32 ratio;
89   FLOAT32 attack_ms;
90   FLOAT32 release_ms;
91   FLOAT32 alpha_a;
92   FLOAT32 alpha_r;
93   FLOAT32 yl_z1[STFT256_HOP_SIZE + 1];
94 
95   UWORD32 nb_points;
96   UWORD32 nb_segments;
97   ia_drc_compand_segment_struct str_segment[IMPD_DRCCOMPAND_MAX_NUM_SEGMENTS];
98   ia_drc_compand_chan_param_struct str_channel_param;
99   FLOAT32 in_min_db;
100   FLOAT32 out_min_db;
101   FLOAT32 width_db;
102   FLOAT32 gain_db;
103   FLOAT32 initial_volume;
104 } ia_drc_stft_gain_calc_struct;
105 
106 typedef struct {
107   WORD32 n_gain_values;
108 
109   FLOAT32 drc_gain_quant[IMPD_DRCMAX_FRAMESIZE];
110   WORD32 gain_code[IMPD_DRCMAX_FRAMESIZE];
111   WORD32 gain_code_length[IMPD_DRCMAX_FRAMESIZE];
112   FLOAT32 slope_quant[IMPD_DRCMAX_FRAMESIZE];
113   WORD32 slope_code_index[IMPD_DRCMAX_FRAMESIZE];
114   WORD32 ts_gain_quant[IMPD_DRCMAX_FRAMESIZE];
115   WORD32 time_delta_quant[IMPD_DRCMAX_FRAMESIZE];
116 
117   FLOAT32 drc_gain[IMPD_DRCMAX_FRAMESIZE];
118   FLOAT32 slope[IMPD_DRCMAX_FRAMESIZE];
119   WORD32 ts_gain[IMPD_DRCMAX_FRAMESIZE];
120 
121   FLOAT32 gain_prev_node;
122   FLOAT32 drc_gain_quant_prev;
123 
124 } ia_drc_group_struct;
125 
126 typedef struct {
127   WORD32 n_gain_values;
128 
129   FLOAT32 drc_gain_quant[IMPD_DRCMAX_FRAMESIZE];
130   WORD32 gain_code[IMPD_DRCMAX_FRAMESIZE];
131   WORD32 gain_code_length[IMPD_DRCMAX_FRAMESIZE];
132   FLOAT32 slope_quant[IMPD_DRCMAX_FRAMESIZE];
133   WORD32 slope_code_index[IMPD_DRCMAX_FRAMESIZE];
134   WORD32 ts_gain_quant[IMPD_DRCMAX_FRAMESIZE];
135   WORD32 time_delta_quant[IMPD_DRCMAX_FRAMESIZE];
136 
137   WORD32 time_delta_code[IMPD_DRCMAX_FRAMESIZE];
138   WORD32 time_delta_code_index[IMPD_DRCMAX_FRAMESIZE];
139   WORD32 time_delta_code_size[IMPD_DRCMAX_FRAMESIZE];
140   WORD32 slope_code[IMPD_DRCMAX_FRAMESIZE];
141   WORD32 slope_code_size[IMPD_DRCMAX_FRAMESIZE];
142 
143   FLOAT32 drc_gain_quant_prev;
144   FLOAT32 drc_gain_quant_next;
145   WORD32 time_quant_next;
146   WORD32 time_quant_prev;
147   WORD32 slope_code_index_next;
148   WORD32 slope_code_index_prev;
149 
150   WORD32 coding_mode;
151 } ia_drc_group_for_output_struct;
152 
153 typedef struct {
154   ia_drc_gain_set_params_struct str_gain_set_params;
155   ia_drc_group_struct str_drc_group;
156   ia_drc_group_for_output_struct str_drc_group_for_output;
157 } ia_drc_gain_seq_buf_struct;
158 
159 typedef struct {
160   WORD32 n_sequences;
161   WORD32 delta_tmin;
162   WORD32 delta_tmin_default;
163   WORD32 drc_frame_size;
164   WORD32 sample_rate;
165   WORD32 delay_mode;
166   WORD32 domain;
167   WORD32 base_ch_count;
168   ia_drc_uni_drc_config_struct str_uni_drc_config;
169   ia_drc_loudness_info_set_struct str_loudness_info_set;
170   FLOAT32 drc_gain_per_sample_with_prev_frame[IMPD_DRCMAX_NSEQ][3 * IMPD_DRCMAX_FRAMESIZE];
171   ia_drc_gain_seq_buf_struct str_drc_gain_seq_buf[IMPD_DRCMAX_NSEQ];
172   ia_drc_delta_time_code_table_entry_struct
173       str_delta_time_code_table[2 * IMPD_DRCMAX_FRAMESIZE + 1];
174   WORD32 delta_time_quant_table[IMPD_DRCMAX_FRAMESIZE];
175 
176   ia_drc_eq_set_struct str_eq_set;
177   ia_drc_filter_banks_struct str_filter_banks;
178   ia_drc_compand_struct str_drc_compand[MAX_DRC_COEFF_COUNT][GAIN_SET_COUNT_MAX];
179   ia_drc_stft_gain_calc_struct str_drc_stft_gain_handle[MAX_DRC_COEFF_COUNT][GAIN_SET_COUNT_MAX]
180                                                        [MAX_BAND_COUNT];
181   FLOAT32 stft_tmp_in_buf_time[MAX_NUM_CHANNELS][STFT256_HOP_SIZE];
182   FLOAT32 complex_fft_ptr[MAX_NUM_CHANNELS][IMPD_DRCMAX_FRAMESIZE << 1];
183 } ia_drc_gain_enc_struct;
184 
185 IA_ERRORCODE impd_drc_quantize_and_encode_drc_gain(
186     ia_drc_gain_enc_struct *pstr_gain_enc, const FLOAT32 *ptr_drc_gain_per_sample,
187     FLOAT32 *ptr_drc_gain_per_sample_with_prev_frame,
188     ia_drc_delta_time_code_table_entry_struct *pstr_delta_time_code_table,
189     ia_drc_gain_seq_buf_struct *pstr_drc_gain_seq_buf, VOID *pstr_scratch);
190 
191 IA_ERRORCODE impd_drc_td_drc_gain_calc_init(ia_drc_gain_enc_struct *pstr_drc_gain_enc,
192                                             WORD32 drc_coefficients_uni_drc_idx,
193                                             WORD32 gain_set_idx);
194 
195 IA_ERRORCODE impd_drc_stft_drc_gain_calc_init(ia_drc_gain_enc_struct *pstr_drc_gain_enc,
196                                               WORD32 drc_coefficients_uni_drc_idx,
197                                               WORD32 gain_set_idx, WORD32 band_idx);
198 
199 VOID impd_drc_td_drc_gain_calc_process(ia_drc_gain_enc_struct *pstr_drc_gain_enc,
200                                        WORD32 drc_coefficients_uni_drc_idx, WORD32 gain_set_idx,
201                                        WORD32 num_samples, FLOAT32 *in_buff, FLOAT32 *out_buff);
202 
203 VOID impd_drc_stft_drc_gain_calc_process(ia_drc_gain_enc_struct *pstr_drc_gain_enc,
204                                          WORD32 drc_coefficients_uni_drc_idx, WORD32 gain_set_idx,
205                                          WORD32 band_idx, WORD32 start_sub_band_index,
206                                          WORD32 stop_sub_band_index, UWORD32 num_frames,
207                                          FLOAT32 *in_buff, FLOAT32 *gain_values);
208 
209 VOID impd_drc_stft_drc_convert_to_fd(ia_drc_gain_enc_struct *pstr_drc_gain_enc,
210                                      FLOAT32 *ptr_input, UWORD32 ch_idx, UWORD32 frame_size,
211                                      FLOAT32 *ptr_output, VOID *pstr_scratch);
212