Coverage Report

Created: 2026-09-03 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
3.32k
                         WORD32 ccfl) {
63
3.32k
  WORD32 i;
64
65
8.98k
  for (i = 0; i < num_channels; i++) {
66
5.66k
    iusace_psy_long_config_init(bit_rate / num_channels, sample_rate, band_width,
67
5.66k
                                &(pstr_psy_mod->str_psy_long_config[ele_id]), ccfl);
68
69
5.66k
    iusace_psy_short_config_init(bit_rate / num_channels, sample_rate, band_width,
70
5.66k
                                 &(pstr_psy_mod->str_psy_short_config[ele_id]), ccfl);
71
72
5.66k
    pstr_psy_mod->str_psy_data[ch].ptr_sfb_thr_long =
73
5.66k
        (FLOAT32 *)pstr_psy_mod->str_psy_data[ch].sfb_thr_short;
74
5.66k
    pstr_psy_mod->str_psy_data[ch].ptr_sfb_energy_long =
75
5.66k
        (FLOAT32 *)pstr_psy_mod->str_psy_data[ch].sfb_energy_short;
76
5.66k
    pstr_psy_mod->str_psy_data[ch].ptr_sfb_spreaded_energy_long =
77
5.66k
        (FLOAT32 *)pstr_psy_mod->str_psy_data[ch].sfb_spreaded_energy_short;
78
79
5.66k
    memcpy(pstr_psy_mod->str_psy_data[ch].sfb_thr_nm1,
80
5.66k
           pstr_psy_mod->str_psy_long_config[ele_id].sfb_thr_quiet,
81
5.66k
           pstr_psy_mod->str_psy_long_config[ele_id].sfb_count * sizeof(FLOAT32));
82
83
5.66k
    ch++;
84
5.66k
  }
85
86
3.32k
  return;
87
3.32k
}
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
283k
                       WORD32 ccfl) {
94
283k
  ia_psy_mod_data_struct *pstr_psy_data = &(pstr_psy_mod->str_psy_data[i_ch]);
95
283k
  ia_psy_mod_long_config_struct *pstr_psy_config = &(pstr_psy_mod->str_psy_long_config[elem_idx]);
96
283k
  WORD32 window_sequence = pstr_sfb_prms->window_sequence[i_ch];
97
283k
  WORD32 num_sfb = pstr_sfb_prms->num_sfb[i_ch];
98
283k
  ia_tns_info *ptr_tns_info = pstr_tns_info[i_ch];
99
283k
  WORD32 sfb, line;
100
283k
  WORD32 i;
101
283k
  WORD32 frame_len_long = ccfl;
102
283k
  FLOAT32 energy_shift = 0.25f;
103
283k
  FLOAT32 clip_energy = pstr_psy_config->clip_energy * energy_shift;
104
283k
  (VOID) channel_type;
105
106
283k
  pstr_psy_data->window_sequence = window_sequence;
107
283k
  memset(&ptr_spec_in[pstr_psy_config->low_pass_line], 0,
108
283k
         (frame_len_long - pstr_psy_config->low_pass_line) * sizeof(FLOAT64));
109
110
283k
  iusace_calc_band_energy(ptr_spec_in, pstr_psy_config->sfb_offset, pstr_psy_config->sfb_active,
111
283k
                          pstr_psy_data->ptr_sfb_energy_long, pstr_psy_config->sfb_count);
112
113
283k
  if (tns_select != 0) {
114
141k
    ia_tns_info *ptr_tns_info_ch2 = pstr_tns_info[i_ch - chn];
115
141k
    ptr_tns_info->number_of_bands = num_sfb;
116
141k
    ptr_tns_info->block_type = window_sequence;
117
141k
    ptr_tns_info->spec = ptr_spec_in;
118
141k
    iusace_tns_encode(ptr_tns_info_ch2, ptr_tns_info, pstr_psy_data->ptr_sfb_energy_long, 0, chn,
119
141k
                      pstr_psy_config->low_pass_line, scratch_tns_filter, 0, ptr_tns_scratch);
120
141k
  }
121
122
12.7M
  for (i = 0; i < pstr_psy_config->sfb_count; i++) {
123
12.4M
    pstr_psy_data->ptr_sfb_thr_long[i] =
124
12.4M
        pstr_psy_data->ptr_sfb_energy_long[i] * pstr_psy_config->ratio;
125
12.4M
    pstr_psy_data->ptr_sfb_thr_long[i] = MIN(pstr_psy_data->ptr_sfb_thr_long[i], clip_energy);
126
12.4M
  }
127
128
283k
  if (tns_select != 0) {
129
141k
    if (ptr_tns_info->tns_data_present == 1) {
130
97.9k
      iusace_calc_band_energy(ptr_spec_in, pstr_psy_config->sfb_offset,
131
97.9k
                              pstr_psy_config->sfb_active, pstr_psy_data->ptr_sfb_energy_long,
132
97.9k
                              pstr_psy_config->sfb_count);
133
97.9k
    }
134
141k
  }
135
136
283k
  iusace_find_max_spreading(pstr_psy_config->sfb_count, pstr_psy_config->sfb_mask_low_fac,
137
283k
                            pstr_psy_config->sfb_mask_high_fac, pstr_psy_data->ptr_sfb_thr_long);
138
139
12.7M
  for (i = 0; i < pstr_psy_config->sfb_count; i++) {
140
12.4M
    pstr_psy_data->ptr_sfb_thr_long[i] = MAX(pstr_psy_data->ptr_sfb_thr_long[i],
141
12.4M
                                             (pstr_psy_config->sfb_thr_quiet[i] * energy_shift));
142
12.4M
  }
143
144
283k
  if (pstr_psy_data->window_sequence == LONG_STOP_SEQUENCE) {
145
1.84M
    for (i = 0; i < pstr_psy_config->sfb_count; i++) {
146
1.80M
      pstr_psy_data->sfb_thr_nm1[i] = 1.0e20f;
147
1.80M
    }
148
41.2k
  }
149
150
283k
  iusace_pre_echo_control(pstr_psy_data->sfb_thr_nm1, pstr_psy_config->sfb_count,
151
283k
                          pstr_psy_config->max_allowed_inc_fac,
152
283k
                          pstr_psy_config->min_remaining_thr_fac,
153
283k
                          pstr_psy_data->ptr_sfb_thr_long);
154
155
283k
  if (pstr_psy_data->window_sequence == LONG_START_SEQUENCE) {
156
2.07M
    for (i = 0; i < pstr_psy_config->sfb_count; i++) {
157
2.02M
      pstr_psy_data->sfb_thr_nm1[i] = 1.0e20f;
158
2.02M
    }
159
46.3k
  }
160
161
12.7M
  for (i = 0; i < pstr_psy_config->sfb_count; i++) {
162
12.4M
    pstr_psy_data->ptr_sfb_spreaded_energy_long[i] = pstr_psy_data->ptr_sfb_energy_long[i];
163
12.4M
  }
164
283k
  iusace_find_max_spreading(
165
283k
      pstr_psy_config->sfb_count, pstr_psy_config->sfb_mask_low_fac_spr_ener,
166
283k
      pstr_psy_config->sfb_mask_high_fac_spr_ener, pstr_psy_data->ptr_sfb_spreaded_energy_long);
167
168
1.53M
  for (sfb = pstr_psy_config->sfb_count - 1; sfb >= 0; sfb--) {
169
1.52M
    for (line = pstr_psy_config->sfb_offset[sfb + 1] - 1;
170
46.9M
         line >= pstr_psy_config->sfb_offset[sfb]; line--) {
171
45.7M
      if (ptr_spec_in[line] != 0) break;
172
45.7M
    }
173
1.52M
    if (line >= pstr_psy_config->sfb_offset[sfb]) break;
174
1.52M
  }
175
176
283k
  pstr_psy_mod->str_psy_out_data[i_ch].max_sfb_per_grp = sfb + 1;
177
178
283k
  return;
179
283k
}
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
139k
                       WORD32 ccfl) {
186
139k
  ia_psy_mod_data_struct *pstr_psy_data = &(pstr_psy_mod->str_psy_data[i_ch]);
187
139k
  ia_psy_mod_short_config_struct *pstr_psy_config =
188
139k
      &(pstr_psy_mod->str_psy_short_config[elem_idx]);
189
139k
  WORD32 max_sfb = 0, sfb, line;
190
139k
  WORD32 window_sequence = pstr_sfb_prms->window_sequence[i_ch];
191
139k
  WORD32 num_sfb = pstr_sfb_prms->num_sfb[i_ch];
192
139k
  ia_tns_info *ptr_tns_info = pstr_tns_info[i_ch];
193
139k
  WORD32 i, w;
194
139k
  WORD32 frame_len_short = (ccfl * FRAME_LEN_SHORT_128) / FRAME_LEN_LONG;
195
139k
  FLOAT32 energy_shift = 0.25f;
196
139k
  FLOAT32 clip_energy = pstr_psy_config->clip_energy * energy_shift;
197
139k
  (VOID) channel_type;
198
199
139k
  pstr_psy_data->window_sequence = window_sequence;
200
201
1.25M
  for (w = 0; w < MAX_SHORT_WINDOWS; w++) {
202
1.11M
    WORD32 w_offset = w * frame_len_short;
203
1.11M
    WORD32 offset;
204
1.11M
    FLOAT64 *pmdct_double = &ptr_spec_in[pstr_psy_config->low_pass_line + w_offset];
205
206
1.11M
    offset = frame_len_short - pstr_psy_config->low_pass_line;
207
208
1.11M
    memset(pmdct_double, 0, sizeof(FLOAT64) * offset);
209
210
1.11M
    iusace_calc_band_energy(ptr_spec_in + w_offset, pstr_psy_config->sfb_offset,
211
1.11M
                            pstr_psy_config->sfb_active, pstr_psy_data->sfb_energy_short[w],
212
1.11M
                            pstr_psy_config->sfb_count);
213
214
1.11M
    if (tns_select != 0) {
215
525k
      ia_tns_info *ptr_tns_info_ch2 = pstr_tns_info[i_ch - chn];
216
525k
      ptr_tns_info->number_of_bands = num_sfb;
217
525k
      ptr_tns_info->block_type = window_sequence;
218
525k
      ptr_tns_info->spec = ptr_spec_in + w_offset;
219
525k
      iusace_tns_encode(ptr_tns_info_ch2, ptr_tns_info, pstr_psy_data->sfb_energy_short[w], w,
220
525k
                        chn, pstr_psy_config->low_pass_line, scratch_tns_filter, 0,
221
525k
                        ptr_tns_scratch);
222
525k
    }
223
224
16.5M
    for (i = 0; i < pstr_psy_config->sfb_count; i++) {
225
15.4M
      pstr_psy_data->sfb_thr_short[w][i] =
226
15.4M
          pstr_psy_data->sfb_energy_short[w][i] * pstr_psy_config->ratio;
227
15.4M
      pstr_psy_data->sfb_thr_short[w][i] = MIN(pstr_psy_data->sfb_thr_short[w][i], clip_energy);
228
15.4M
    }
229
230
1.11M
    if (tns_select != 0) {
231
525k
      if (ptr_tns_info->tns_data_present == 1) {
232
172k
        iusace_calc_band_energy(ptr_spec_in + w_offset, pstr_psy_config->sfb_offset,
233
172k
                                pstr_psy_config->sfb_active, pstr_psy_data->sfb_energy_short[w],
234
172k
                                pstr_psy_config->sfb_count);
235
172k
      }
236
525k
    }
237
238
1.11M
    iusace_find_max_spreading(pstr_psy_config->sfb_count, pstr_psy_config->sfb_mask_low_fac,
239
1.11M
                              pstr_psy_config->sfb_mask_high_fac,
240
1.11M
                              pstr_psy_data->sfb_thr_short[w]);
241
242
16.5M
    for (i = 0; i < pstr_psy_config->sfb_count; i++) {
243
15.4M
      pstr_psy_data->sfb_thr_short[w][i] =
244
15.4M
          MAX(pstr_psy_data->sfb_thr_short[w][i], (pstr_psy_config->sfb_thr_quiet[i] * 0.25f));
245
15.4M
    }
246
247
1.11M
    iusace_pre_echo_control(pstr_psy_data->sfb_thr_nm1, pstr_psy_config->sfb_count,
248
1.11M
                            pstr_psy_config->max_allowed_inc_fac,
249
1.11M
                            pstr_psy_config->min_remaining_thr_fac,
250
1.11M
                            pstr_psy_data->sfb_thr_short[w]);
251
252
16.5M
    for (i = 0; i < pstr_psy_config->sfb_count; i++) {
253
15.4M
      pstr_psy_data->sfb_spreaded_energy_short[w][i] = pstr_psy_data->sfb_energy_short[w][i];
254
15.4M
    }
255
1.11M
    iusace_find_max_spreading(
256
1.11M
        pstr_psy_config->sfb_count, pstr_psy_config->sfb_mask_low_fac_spr_ener,
257
1.11M
        pstr_psy_config->sfb_mask_high_fac_spr_ener, pstr_psy_data->sfb_spreaded_energy_short[w]);
258
1.11M
  }
259
260
1.25M
  for (WORD32 wnd = 0; wnd < MAX_SHORT_WINDOWS; wnd++) {
261
4.14M
    for (sfb = pstr_psy_config->sfb_count - 1; sfb >= max_sfb; sfb--) {
262
3.96M
      for (line = pstr_psy_config->sfb_offset[sfb + 1] - 1;
263
37.9M
           line >= pstr_psy_config->sfb_offset[sfb]; line--) {
264
34.9M
        if (ptr_spec_in[wnd * frame_len_short + line] != 0.0) break;
265
34.9M
      }
266
3.96M
      if (line >= pstr_psy_config->sfb_offset[sfb]) break;
267
3.96M
    }
268
1.11M
    max_sfb = MAX(max_sfb, sfb);
269
1.11M
  }
270
139k
  max_sfb = max_sfb > 0 ? max_sfb : 0;
271
272
139k
  pstr_psy_mod->str_psy_out_data[i_ch].max_sfb_per_grp = max_sfb + 1;
273
274
139k
  return;
275
139k
}