Coverage Report

Created: 2025-08-29 06:15

/src/libxaac/encoder/iusace_psy_mod.c
Line
Count
Source
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 <string.h>
22
#include "iusace_cnst.h"
23
#include "iusace_type_def.h"
24
#include "iusace_block_switch_const.h"
25
#include "iusace_block_switch_struct_def.h"
26
#include "iusace_bitbuffer.h"
27
#include "iusace_tns_usac.h"
28
#include "impd_drc_common_enc.h"
29
#include "impd_drc_uni_drc.h"
30
#include "impd_drc_api.h"
31
#include "impd_drc_uni_drc_eq.h"
32
#include "impd_drc_uni_drc_filter_bank.h"
33
#include "impd_drc_gain_enc.h"
34
#include "impd_drc_struct_def.h"
35
#include "iusace_cnst.h"
36
#include "iusace_tns_usac.h"
37
#include "iusace_psy_mod.h"
38
#include "iusace_psy_utils.h"
39
40
#include "ixheaac_error_standards.h"
41
#include "iusace_type_def.h"
42
#include "iusace_cnst.h"
43
44
#include "iusace_ms.h"
45
46
#include "iusace_psy_utils.h"
47
#include "ixheaace_adjust_threshold_data.h"
48
#include "iusace_fd_qc_util.h"
49
#include "ixheaace_memory_standards.h"
50
#include "iusace_config.h"
51
#include "iusace_arith_enc.h"
52
#include "iusace_fd_quant.h"
53
#include "iusace_signal_classifier.h"
54
#include "iusace_block_switch_const.h"
55
#include "ixheaace_sbr_header.h"
56
#include "ixheaace_config.h"
57
#include "ixheaace_asc_write.h"
58
#include "iusace_main.h"
59
60
VOID iusace_psy_mod_init(ia_psy_mod_struct *pstr_psy_mod, WORD32 sample_rate, WORD32 bit_rate,
61
                         WORD32 band_width, WORD32 num_channels, WORD32 ch, WORD32 ele_id,
62
4.89k
                         WORD32 ccfl) {
63
4.89k
  WORD32 i;
64
65
13.4k
  for (i = 0; i < num_channels; i++) {
66
8.54k
    iusace_psy_long_config_init(bit_rate / num_channels, sample_rate, band_width,
67
8.54k
                                &(pstr_psy_mod->str_psy_long_config[ele_id]), ccfl);
68
69
8.54k
    iusace_psy_short_config_init(bit_rate / num_channels, sample_rate, band_width,
70
8.54k
                                 &(pstr_psy_mod->str_psy_short_config[ele_id]), ccfl);
71
72
8.54k
    pstr_psy_mod->str_psy_data[ch].ptr_sfb_thr_long =
73
8.54k
        (FLOAT32 *)pstr_psy_mod->str_psy_data[ch].sfb_thr_short;
74
8.54k
    pstr_psy_mod->str_psy_data[ch].ptr_sfb_energy_long =
75
8.54k
        (FLOAT32 *)pstr_psy_mod->str_psy_data[ch].sfb_energy_short;
76
8.54k
    pstr_psy_mod->str_psy_data[ch].ptr_sfb_spreaded_energy_long =
77
8.54k
        (FLOAT32 *)pstr_psy_mod->str_psy_data[ch].sfb_spreaded_energy_short;
78
79
8.54k
    memcpy(pstr_psy_mod->str_psy_data[ch].sfb_thr_nm1,
80
8.54k
           pstr_psy_mod->str_psy_long_config[ele_id].sfb_thr_quiet,
81
8.54k
           pstr_psy_mod->str_psy_long_config[ele_id].sfb_count * sizeof(FLOAT32));
82
83
8.54k
    ch++;
84
8.54k
  }
85
86
4.89k
  return;
87
4.89k
}
88
89
VOID iusace_psy_mod_lb(ia_psy_mod_struct *pstr_psy_mod, ia_sfb_params_struct *pstr_sfb_prms,
90
                       FLOAT64 *ptr_spec_in, ia_tns_info *pstr_tns_info[MAX_TIME_CHANNELS],
91
                       WORD32 tns_select, WORD32 i_ch, WORD32 chn, WORD32 channel_type,
92
                       FLOAT64 *scratch_tns_filter, WORD32 elem_idx, FLOAT64 *ptr_tns_scratch,
93
328k
                       WORD32 ccfl) {
94
328k
  ia_psy_mod_data_struct *pstr_psy_data = &(pstr_psy_mod->str_psy_data[i_ch]);
95
328k
  ia_psy_mod_long_config_struct *pstr_psy_config = &(pstr_psy_mod->str_psy_long_config[elem_idx]);
96
328k
  WORD32 window_sequence = pstr_sfb_prms->window_sequence[i_ch];
97
328k
  WORD32 num_sfb = pstr_sfb_prms->num_sfb[i_ch];
98
328k
  ia_tns_info *ptr_tns_info = pstr_tns_info[i_ch];
99
328k
  WORD32 sfb, line;
100
328k
  WORD32 i;
101
328k
  WORD32 frame_len_long = ccfl;
102
328k
  FLOAT32 energy_shift = 0.25f;
103
328k
  FLOAT32 clip_energy = pstr_psy_config->clip_energy * energy_shift;
104
328k
  (VOID) channel_type;
105
106
328k
  pstr_psy_data->window_sequence = window_sequence;
107
328k
  memset(&ptr_spec_in[pstr_psy_config->low_pass_line], 0,
108
328k
         (frame_len_long - pstr_psy_config->low_pass_line) * sizeof(FLOAT64));
109
110
328k
  iusace_calc_band_energy(ptr_spec_in, pstr_psy_config->sfb_offset, pstr_psy_config->sfb_active,
111
328k
                          pstr_psy_data->ptr_sfb_energy_long, pstr_psy_config->sfb_count);
112
113
328k
  if (tns_select != 0) {
114
108k
    ia_tns_info *ptr_tns_info_ch2 = pstr_tns_info[i_ch - chn];
115
108k
    ptr_tns_info->number_of_bands = num_sfb;
116
108k
    ptr_tns_info->block_type = window_sequence;
117
108k
    ptr_tns_info->spec = ptr_spec_in;
118
108k
    iusace_tns_encode(ptr_tns_info_ch2, ptr_tns_info, pstr_psy_data->ptr_sfb_energy_long, 0, chn,
119
108k
                      pstr_psy_config->low_pass_line, scratch_tns_filter, 0, ptr_tns_scratch);
120
108k
  }
121
122
14.4M
  for (i = 0; i < pstr_psy_config->sfb_count; i++) {
123
14.1M
    pstr_psy_data->ptr_sfb_thr_long[i] =
124
14.1M
        pstr_psy_data->ptr_sfb_energy_long[i] * pstr_psy_config->ratio;
125
14.1M
    pstr_psy_data->ptr_sfb_thr_long[i] = MIN(pstr_psy_data->ptr_sfb_thr_long[i], clip_energy);
126
14.1M
  }
127
128
328k
  if (tns_select != 0) {
129
108k
    if (ptr_tns_info->tns_data_present == 1) {
130
60.1k
      iusace_calc_band_energy(ptr_spec_in, pstr_psy_config->sfb_offset,
131
60.1k
                              pstr_psy_config->sfb_active, pstr_psy_data->ptr_sfb_energy_long,
132
60.1k
                              pstr_psy_config->sfb_count);
133
60.1k
    }
134
108k
  }
135
136
328k
  iusace_find_max_spreading(pstr_psy_config->sfb_count, pstr_psy_config->sfb_mask_low_fac,
137
328k
                            pstr_psy_config->sfb_mask_high_fac, pstr_psy_data->ptr_sfb_thr_long);
138
139
14.4M
  for (i = 0; i < pstr_psy_config->sfb_count; i++) {
140
14.1M
    pstr_psy_data->ptr_sfb_thr_long[i] = MAX(pstr_psy_data->ptr_sfb_thr_long[i],
141
14.1M
                                             (pstr_psy_config->sfb_thr_quiet[i] * energy_shift));
142
14.1M
  }
143
144
328k
  if (pstr_psy_data->window_sequence == LONG_STOP_SEQUENCE) {
145
2.18M
    for (i = 0; i < pstr_psy_config->sfb_count; i++) {
146
2.13M
      pstr_psy_data->sfb_thr_nm1[i] = 1.0e20f;
147
2.13M
    }
148
50.6k
  }
149
150
328k
  iusace_pre_echo_control(pstr_psy_data->sfb_thr_nm1, pstr_psy_config->sfb_count,
151
328k
                          pstr_psy_config->max_allowed_inc_fac,
152
328k
                          pstr_psy_config->min_remaining_thr_fac,
153
328k
                          pstr_psy_data->ptr_sfb_thr_long);
154
155
328k
  if (pstr_psy_data->window_sequence == LONG_START_SEQUENCE) {
156
2.50M
    for (i = 0; i < pstr_psy_config->sfb_count; i++) {
157
2.44M
      pstr_psy_data->sfb_thr_nm1[i] = 1.0e20f;
158
2.44M
    }
159
57.7k
  }
160
161
14.4M
  for (i = 0; i < pstr_psy_config->sfb_count; i++) {
162
14.1M
    pstr_psy_data->ptr_sfb_spreaded_energy_long[i] = pstr_psy_data->ptr_sfb_energy_long[i];
163
14.1M
  }
164
328k
  iusace_find_max_spreading(
165
328k
      pstr_psy_config->sfb_count, pstr_psy_config->sfb_mask_low_fac_spr_ener,
166
328k
      pstr_psy_config->sfb_mask_high_fac_spr_ener, pstr_psy_data->ptr_sfb_spreaded_energy_long);
167
168
1.96M
  for (sfb = pstr_psy_config->sfb_count - 1; sfb >= 0; sfb--) {
169
1.94M
    for (line = pstr_psy_config->sfb_offset[sfb + 1] - 1;
170
64.8M
         line >= pstr_psy_config->sfb_offset[sfb]; line--) {
171
63.2M
      if (ptr_spec_in[line] != 0) break;
172
63.2M
    }
173
1.94M
    if (line >= pstr_psy_config->sfb_offset[sfb]) break;
174
1.94M
  }
175
176
328k
  pstr_psy_mod->str_psy_out_data[i_ch].max_sfb_per_grp = sfb + 1;
177
178
328k
  return;
179
328k
}
180
181
VOID iusace_psy_mod_sb(ia_psy_mod_struct *pstr_psy_mod, ia_sfb_params_struct *pstr_sfb_prms,
182
                       FLOAT64 *ptr_spec_in, ia_tns_info *pstr_tns_info[MAX_TIME_CHANNELS],
183
                       WORD32 tns_select, WORD32 i_ch, WORD32 chn, WORD32 channel_type,
184
                       FLOAT64 *scratch_tns_filter, WORD32 elem_idx, FLOAT64 *ptr_tns_scratch,
185
164k
                       WORD32 ccfl) {
186
164k
  ia_psy_mod_data_struct *pstr_psy_data = &(pstr_psy_mod->str_psy_data[i_ch]);
187
164k
  ia_psy_mod_short_config_struct *pstr_psy_config =
188
164k
      &(pstr_psy_mod->str_psy_short_config[elem_idx]);
189
164k
  WORD32 max_sfb = 0, sfb, line;
190
164k
  WORD32 window_sequence = pstr_sfb_prms->window_sequence[i_ch];
191
164k
  WORD32 num_sfb = pstr_sfb_prms->num_sfb[i_ch];
192
164k
  ia_tns_info *ptr_tns_info = pstr_tns_info[i_ch];
193
164k
  WORD32 i, w;
194
164k
  WORD32 frame_len_short = (ccfl * FRAME_LEN_SHORT_128) / FRAME_LEN_LONG;
195
164k
  FLOAT32 energy_shift = 0.25f;
196
164k
  FLOAT32 clip_energy = pstr_psy_config->clip_energy * energy_shift;
197
164k
  (VOID) channel_type;
198
199
164k
  pstr_psy_data->window_sequence = window_sequence;
200
201
1.47M
  for (w = 0; w < MAX_SHORT_WINDOWS; w++) {
202
1.31M
    WORD32 w_offset = w * frame_len_short;
203
1.31M
    WORD32 offset;
204
1.31M
    FLOAT64 *pmdct_double = &ptr_spec_in[pstr_psy_config->low_pass_line + w_offset];
205
206
1.31M
    offset = frame_len_short - pstr_psy_config->low_pass_line;
207
208
1.31M
    memset(pmdct_double, 0, sizeof(FLOAT64) * offset);
209
210
1.31M
    iusace_calc_band_energy(ptr_spec_in + w_offset, pstr_psy_config->sfb_offset,
211
1.31M
                            pstr_psy_config->sfb_active, pstr_psy_data->sfb_energy_short[w],
212
1.31M
                            pstr_psy_config->sfb_count);
213
214
1.31M
    if (tns_select != 0) {
215
364k
      ia_tns_info *ptr_tns_info_ch2 = pstr_tns_info[i_ch - chn];
216
364k
      ptr_tns_info->number_of_bands = num_sfb;
217
364k
      ptr_tns_info->block_type = window_sequence;
218
364k
      ptr_tns_info->spec = ptr_spec_in + w_offset;
219
364k
      iusace_tns_encode(ptr_tns_info_ch2, ptr_tns_info, pstr_psy_data->sfb_energy_short[w], w,
220
364k
                        chn, pstr_psy_config->low_pass_line, scratch_tns_filter, 0,
221
364k
                        ptr_tns_scratch);
222
364k
    }
223
224
19.1M
    for (i = 0; i < pstr_psy_config->sfb_count; i++) {
225
17.8M
      pstr_psy_data->sfb_thr_short[w][i] =
226
17.8M
          pstr_psy_data->sfb_energy_short[w][i] * pstr_psy_config->ratio;
227
17.8M
      pstr_psy_data->sfb_thr_short[w][i] = MIN(pstr_psy_data->sfb_thr_short[w][i], clip_energy);
228
17.8M
    }
229
230
1.31M
    if (tns_select != 0) {
231
364k
      if (ptr_tns_info->tns_data_present == 1) {
232
99.1k
        iusace_calc_band_energy(ptr_spec_in + w_offset, pstr_psy_config->sfb_offset,
233
99.1k
                                pstr_psy_config->sfb_active, pstr_psy_data->sfb_energy_short[w],
234
99.1k
                                pstr_psy_config->sfb_count);
235
99.1k
      }
236
364k
    }
237
238
1.31M
    iusace_find_max_spreading(pstr_psy_config->sfb_count, pstr_psy_config->sfb_mask_low_fac,
239
1.31M
                              pstr_psy_config->sfb_mask_high_fac,
240
1.31M
                              pstr_psy_data->sfb_thr_short[w]);
241
242
19.1M
    for (i = 0; i < pstr_psy_config->sfb_count; i++) {
243
17.8M
      pstr_psy_data->sfb_thr_short[w][i] =
244
17.8M
          MAX(pstr_psy_data->sfb_thr_short[w][i], (pstr_psy_config->sfb_thr_quiet[i] * 0.25f));
245
17.8M
    }
246
247
1.31M
    iusace_pre_echo_control(pstr_psy_data->sfb_thr_nm1, pstr_psy_config->sfb_count,
248
1.31M
                            pstr_psy_config->max_allowed_inc_fac,
249
1.31M
                            pstr_psy_config->min_remaining_thr_fac,
250
1.31M
                            pstr_psy_data->sfb_thr_short[w]);
251
252
19.1M
    for (i = 0; i < pstr_psy_config->sfb_count; i++) {
253
17.8M
      pstr_psy_data->sfb_spreaded_energy_short[w][i] = pstr_psy_data->sfb_energy_short[w][i];
254
17.8M
    }
255
1.31M
    iusace_find_max_spreading(
256
1.31M
        pstr_psy_config->sfb_count, pstr_psy_config->sfb_mask_low_fac_spr_ener,
257
1.31M
        pstr_psy_config->sfb_mask_high_fac_spr_ener, pstr_psy_data->sfb_spreaded_energy_short[w]);
258
1.31M
  }
259
260
1.47M
  for (WORD32 wnd = 0; wnd < MAX_SHORT_WINDOWS; wnd++) {
261
6.48M
    for (sfb = pstr_psy_config->sfb_count - 1; sfb >= max_sfb; sfb--) {
262
6.23M
      for (line = pstr_psy_config->sfb_offset[sfb + 1] - 1;
263
58.9M
           line >= pstr_psy_config->sfb_offset[sfb]; line--) {
264
53.8M
        if (ptr_spec_in[wnd * frame_len_short + line] != 0.0) break;
265
53.8M
      }
266
6.23M
      if (line >= pstr_psy_config->sfb_offset[sfb]) break;
267
6.23M
    }
268
1.31M
    max_sfb = MAX(max_sfb, sfb);
269
1.31M
  }
270
164k
  max_sfb = max_sfb > 0 ? max_sfb : 0;
271
272
164k
  pstr_psy_mod->str_psy_out_data[i_ch].max_sfb_per_grp = max_sfb + 1;
273
274
164k
  return;
275
164k
}