Coverage Report

Created: 2025-08-03 06:57

/src/libxaac/decoder/drc_src/impd_drc_process.c
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2018 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
#include <stdio.h>
21
#include <stdlib.h>
22
#include <math.h>
23
#include <string.h>
24
25
#include "impd_type_def.h"
26
#include "impd_drc_extr_delta_coded_info.h"
27
#include "impd_drc_common.h"
28
#include "impd_drc_struct.h"
29
#include "impd_drc_filter_bank.h"
30
#include "impd_drc_multi_band.h"
31
#include "impd_drc_gain_dec.h"
32
#include "impd_drc_process_audio.h"
33
34
VOID impd_apply_gains_and_add(
35
    ia_drc_instructions_struct* pstr_drc_instruction_arr,
36
    const WORD32 drc_instructions_index,
37
    ia_drc_params_struct* ia_drc_params_struct,
38
    ia_gain_buffer_struct* pstr_gain_buf,
39
    shape_filter_block shape_filter_block[], FLOAT32* deinterleaved_audio[],
40
41
4.01k
    FLOAT32* channel_audio[], WORD32 impd_apply_gains) {
42
4.01k
  WORD32 c, b, g, i;
43
4.01k
  WORD32 offset = 0, signal_index = 0;
44
4.01k
  WORD32 gain_index_for_group[CHANNEL_GROUP_COUNT_MAX];
45
4.01k
  WORD32 signal_index_for_channel[MAX_CHANNEL_COUNT];
46
4.01k
  FLOAT32* lpcm_gains;
47
4.01k
  FLOAT32 sum;
48
4.01k
  FLOAT32 drc_gain_last = 0, gain_thr;
49
4.01k
  WORD32 i_end, i_start;
50
4.01k
  ia_drc_instructions_struct* str_drc_instruction_str =
51
4.01k
      &(pstr_drc_instruction_arr[drc_instructions_index]);
52
53
4.01k
  if (drc_instructions_index >= 0) {
54
4.01k
    str_drc_instruction_str =
55
4.01k
        &(pstr_drc_instruction_arr[drc_instructions_index]);
56
4.01k
    {
57
4.01k
      if (str_drc_instruction_str->drc_set_id > 0) {
58
4.01k
        if (ia_drc_params_struct->delay_mode == DELAY_MODE_LOW_DELAY) {
59
0
          offset = ia_drc_params_struct->drc_frame_size;
60
0
        }
61
4.01k
        gain_index_for_group[0] = 0;
62
8.03k
        for (g = 0; g < str_drc_instruction_str->num_drc_ch_groups - 1; g++) {
63
4.01k
          gain_index_for_group[g + 1] =
64
4.01k
              gain_index_for_group[g] +
65
4.01k
              str_drc_instruction_str->band_count_of_ch_group[g];
66
4.01k
        }
67
4.01k
        signal_index_for_channel[0] = 0;
68
6.32k
        for (c = 0; c < str_drc_instruction_str->audio_num_chan - 1; c++) {
69
2.31k
          if (str_drc_instruction_str->channel_group_of_ch[c] >= 0) {
70
2.31k
            signal_index_for_channel[c + 1] =
71
2.31k
                signal_index_for_channel[c] +
72
2.31k
                str_drc_instruction_str->band_count_of_ch_group
73
2.31k
                    [str_drc_instruction_str->channel_group_of_ch[c]];
74
2.31k
          } else {
75
0
            signal_index_for_channel[c + 1] = signal_index_for_channel[c] + 1;
76
0
          }
77
2.31k
        }
78
79
12.0k
        for (g = 0; g < str_drc_instruction_str->num_drc_ch_groups; g++) {
80
16.0k
          for (b = 0; b < str_drc_instruction_str->band_count_of_ch_group[g];
81
8.03k
               b++) {
82
8.03k
            if (str_drc_instruction_str->ch_group_parametric_drc_flag[g] == 0) {
83
8.03k
              lpcm_gains =
84
8.03k
                  pstr_gain_buf->buf_interpolation[gain_index_for_group[g] + b]
85
8.03k
                      .lpcm_gains +
86
8.03k
                  MAX_SIGNAL_DELAY - ia_drc_params_struct->gain_delay_samples -
87
8.03k
                  ia_drc_params_struct->audio_delay_samples + offset;
88
8.03k
            } else {
89
0
              lpcm_gains =
90
0
                  pstr_gain_buf->buf_interpolation[gain_index_for_group[g] + b]
91
0
                      .lpcm_gains +
92
0
                  MAX_SIGNAL_DELAY +
93
0
                  str_drc_instruction_str
94
0
                      ->parametric_drc_look_ahead_samples[g] -
95
0
                  ia_drc_params_struct->audio_delay_samples;
96
0
            }
97
8.03k
            i_end = 0;
98
8.03k
            i_start = 0;
99
16.0k
            while (i_end < ia_drc_params_struct->drc_frame_size) {
100
8.03k
              if (shape_filter_block[g].shape_flter_block_flag) {
101
0
                drc_gain_last = shape_filter_block[g].drc_gain_last;
102
0
                gain_thr = 0.0001f * drc_gain_last;
103
0
                while ((i_end < ia_drc_params_struct->drc_frame_size) &&
104
0
                       (fabs(lpcm_gains[i_end] - drc_gain_last) <= gain_thr))
105
0
                  i_end++;
106
8.03k
              } else {
107
8.03k
                i_end = ia_drc_params_struct->drc_frame_size;
108
8.03k
              }
109
110
20.6k
              for (c = 0; c < str_drc_instruction_str->audio_num_chan; c++)
111
112
12.6k
              {
113
12.6k
                if (g == str_drc_instruction_str->channel_group_of_ch[c]) {
114
6.32k
                  signal_index = signal_index_for_channel[c] + b;
115
116
6.32k
                  if (impd_apply_gains == 1) {
117
6.32k
                    impd_shape_filt_block_time_process(
118
6.32k
                        &shape_filter_block[g], &lpcm_gains[0], signal_index,
119
6.32k
                        &deinterleaved_audio[signal_index][0], i_start, i_end);
120
121
6.32k
                  } else {
122
0
                    for (i = i_start; i < i_end; i++) {
123
0
                      deinterleaved_audio[signal_index][i] = lpcm_gains[i];
124
0
                    }
125
0
                  }
126
6.32k
                }
127
12.6k
              }
128
8.03k
              if ((i_end < ia_drc_params_struct->drc_frame_size) &&
129
8.03k
                  (shape_filter_block[g].shape_flter_block_flag)) {
130
0
                impd_shape_filt_block_adapt(lpcm_gains[i_end],
131
0
                                            &shape_filter_block[g]);
132
0
              }
133
8.03k
              if ((i_end == i_start) &&
134
8.03k
                  (drc_gain_last == shape_filter_block[g].drc_gain_last))
135
0
                break;
136
8.03k
              i_start = i_end;
137
8.03k
            }
138
8.03k
          }
139
8.03k
        }
140
4.01k
      }
141
4.01k
    }
142
4.01k
  }
143
144
4.01k
  signal_index = 0;
145
146
4.01k
  if (str_drc_instruction_str->drc_set_id > 0) {
147
10.3k
    for (c = 0; c < str_drc_instruction_str->audio_num_chan; c++)
148
149
6.32k
    {
150
6.32k
      g = str_drc_instruction_str->channel_group_of_ch[c];
151
6.32k
      if (g >= 0) {
152
14.2M
        for (i = 0; i < ia_drc_params_struct->drc_frame_size; i++) {
153
14.2M
          sum = 0.0f;
154
28.5M
          for (b = 0; b < str_drc_instruction_str->band_count_of_ch_group[g];
155
14.2M
               b++) {
156
14.2M
            sum += deinterleaved_audio[signal_index + b][i];
157
14.2M
          }
158
159
14.2M
          channel_audio[c][i] = sum;
160
14.2M
        }
161
6.32k
        signal_index += str_drc_instruction_str->band_count_of_ch_group[g];
162
6.32k
      } else {
163
0
        for (i = 0; i < ia_drc_params_struct->drc_frame_size; i++) {
164
0
          channel_audio[c][i] = deinterleaved_audio[signal_index][i];
165
0
        }
166
0
        signal_index++;
167
0
      }
168
6.32k
    }
169
4.01k
  } else {
170
0
    for (c = 0; c < str_drc_instruction_str->audio_num_chan; c++)
171
172
0
    {
173
0
      for (i = 0; i < ia_drc_params_struct->drc_frame_size; i++) {
174
0
        channel_audio[c][i] = deinterleaved_audio[c][i];
175
0
      }
176
0
    }
177
0
  }
178
179
4.01k
  return;
180
4.01k
}
181
182
WORD32
183
impd_filter_banks_process(ia_drc_instructions_struct* pstr_drc_instruction_arr,
184
                          const WORD32 drc_instructions_index,
185
                          ia_drc_params_struct* ia_drc_params_struct,
186
                          FLOAT32* audio_io_buf[],
187
                          ia_audio_band_buffer_struct* audio_band_buffer,
188
                          ia_filter_banks_struct* ia_filter_banks_struct,
189
4.01k
                          const WORD32 passThru) {
190
4.01k
  WORD32 c, g, e, i, num_bands;
191
  // WORD32 err = 0;
192
4.01k
  FLOAT32* audio_in;
193
4.01k
  FLOAT32** audio_out;
194
4.01k
  ia_drc_filter_bank_struct* str_drc_filter_bank;
195
4.01k
  ia_drc_instructions_struct* str_drc_instruction_str;
196
4.01k
  WORD32 drc_frame_size = ia_drc_params_struct->drc_frame_size;
197
198
4.01k
  if (drc_instructions_index >= 0) {
199
4.01k
    str_drc_instruction_str =
200
4.01k
        &(pstr_drc_instruction_arr[drc_instructions_index]);
201
4.01k
  } else {
202
0
    return -1;
203
0
  }
204
205
4.01k
  e = 0;
206
207
10.3k
  for (c = 0; c < str_drc_instruction_str->audio_num_chan; c++)
208
209
6.32k
  {
210
6.32k
    str_drc_filter_bank = NULL;
211
212
6.32k
    audio_in = audio_io_buf[c];
213
214
6.32k
    audio_out = &(audio_band_buffer->non_interleaved_audio[e]);
215
6.32k
    if ((passThru == 0) && (drc_instructions_index >= 0)) {
216
0
      if (str_drc_instruction_str->drc_set_id < 0) {
217
0
        num_bands = 1;
218
0
      } else {
219
0
        g = str_drc_instruction_str->channel_group_of_ch[c];
220
0
        if (g == -1) {
221
0
          num_bands = 1;
222
          // if (ia_filter_banks_struct->str_drc_filter_bank != NULL)
223
          //{
224
0
          str_drc_filter_bank =
225
0
              &(ia_filter_banks_struct->str_drc_filter_bank
226
0
                    [str_drc_instruction_str->num_drc_ch_groups]);
227
          //}
228
0
        } else {
229
0
          num_bands = str_drc_instruction_str->band_count_of_ch_group[g];
230
          // if (ia_filter_banks_struct->str_drc_filter_bank != NULL)
231
          //{
232
0
          str_drc_filter_bank =
233
0
              &(ia_filter_banks_struct->str_drc_filter_bank[g]);
234
          //}
235
0
        }
236
        // if (ia_filter_banks_struct->str_drc_filter_bank != NULL)
237
        //{
238
        // if (&str_drc_filter_bank->str_all_pass_cascade != NULL)
239
        //{
240
0
        impd_all_pass_cascade_process(
241
0
            &str_drc_filter_bank->str_all_pass_cascade, c, drc_frame_size,
242
0
            audio_in);
243
        //}
244
        //}
245
0
      }
246
6.32k
    } else {
247
6.32k
      num_bands = 1;
248
6.32k
    }
249
6.32k
    switch (num_bands) {
250
6.32k
      case 1:
251
14.2M
        for (i = 0; i < drc_frame_size; i++) {
252
14.2M
          audio_out[0][i] = audio_in[i];
253
14.2M
        }
254
6.32k
        e++;
255
6.32k
        break;
256
0
      case 2:
257
0
        impd_two_band_filter_process(&str_drc_filter_bank->str_two_band_bank, c,
258
0
                                     drc_frame_size, audio_in, audio_out);
259
0
        e += 2;
260
0
        break;
261
0
      case 3:
262
0
        impd_three_band_filter_process(
263
0
            &str_drc_filter_bank->str_three_band_bank, c, drc_frame_size,
264
0
            audio_in, audio_out);
265
0
        e += 3;
266
0
        break;
267
0
      case 4:
268
0
        impd_four_band_filter_process(&str_drc_filter_bank->str_four_band_bank,
269
0
                                      c, drc_frame_size, audio_in, audio_out);
270
0
        e += 4;
271
0
        break;
272
0
      default:
273
0
        return (PARAM_ERROR);
274
0
        break;
275
6.32k
    }
276
6.32k
  }
277
278
4.01k
  return (0);
279
4.01k
}
280
281
VOID impd_store_audio_io_buffer_time(
282
41.5k
    FLOAT32* audio_in_out_buf[], ia_audio_in_out_buf* audio_io_buf_internal) {
283
41.5k
  WORD32 i, j;
284
285
41.5k
  if (audio_io_buf_internal->audio_delay_samples) {
286
0
    for (i = 0; i < audio_io_buf_internal->audio_num_chan; i++) {
287
0
      for (j = 0; j < audio_io_buf_internal->frame_size; j++) {
288
0
        audio_io_buf_internal->audio_io_buffer_delayed
289
0
            [i][audio_io_buf_internal->audio_delay_samples + j] =
290
0
            audio_in_out_buf[i][j];
291
0
      }
292
0
    }
293
41.5k
  } else {
294
41.5k
    audio_io_buf_internal->audio_io_buffer_delayed = audio_in_out_buf;
295
41.5k
    audio_io_buf_internal->audio_in_out_buf = audio_in_out_buf;
296
41.5k
  }
297
298
41.5k
  return;
299
41.5k
}
300
301
VOID impd_retrieve_audio_io_buffer_time(
302
13.9k
    FLOAT32* audio_in_out_buf[], ia_audio_in_out_buf* audio_io_buf_internal) {
303
13.9k
  WORD32 i, j;
304
305
13.9k
  if (audio_io_buf_internal->audio_delay_samples) {
306
0
    for (i = 0; i < audio_io_buf_internal->audio_num_chan; i++) {
307
0
      for (j = 0; j < audio_io_buf_internal->frame_size; j++) {
308
0
        audio_in_out_buf[i][j] =
309
0
            audio_io_buf_internal->audio_io_buffer_delayed[i][j];
310
0
      }
311
0
    }
312
0
  }
313
314
13.9k
  return;
315
13.9k
}
316
317
VOID impd_advance_audio_io_buffer_time(
318
39.8k
    ia_audio_in_out_buf* audio_io_buf_internal) {
319
39.8k
  WORD32 i;
320
39.8k
  if (audio_io_buf_internal->audio_delay_samples) {
321
0
    for (i = 0; i < audio_io_buf_internal->audio_num_chan; i++) {
322
0
      memmove(
323
0
          audio_io_buf_internal->audio_io_buffer_delayed[i],
324
0
          &audio_io_buf_internal
325
0
               ->audio_io_buffer_delayed[i][audio_io_buf_internal->frame_size],
326
0
          sizeof(FLOAT32) * audio_io_buf_internal->audio_delay_samples);
327
0
    }
328
0
  }
329
330
39.8k
  return;
331
39.8k
}