Coverage Report

Created: 2025-07-18 06:38

/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
3.28k
    FLOAT32* channel_audio[], WORD32 impd_apply_gains) {
42
3.28k
  WORD32 c, b, g, i;
43
3.28k
  WORD32 offset = 0, signal_index = 0;
44
3.28k
  WORD32 gain_index_for_group[CHANNEL_GROUP_COUNT_MAX];
45
3.28k
  WORD32 signal_index_for_channel[MAX_CHANNEL_COUNT];
46
3.28k
  FLOAT32* lpcm_gains;
47
3.28k
  FLOAT32 sum;
48
3.28k
  FLOAT32 drc_gain_last = 0, gain_thr;
49
3.28k
  WORD32 i_end, i_start;
50
3.28k
  ia_drc_instructions_struct* str_drc_instruction_str =
51
3.28k
      &(pstr_drc_instruction_arr[drc_instructions_index]);
52
53
3.28k
  if (drc_instructions_index >= 0) {
54
3.28k
    str_drc_instruction_str =
55
3.28k
        &(pstr_drc_instruction_arr[drc_instructions_index]);
56
3.28k
    {
57
3.28k
      if (str_drc_instruction_str->drc_set_id > 0) {
58
3.28k
        if (ia_drc_params_struct->delay_mode == DELAY_MODE_LOW_DELAY) {
59
0
          offset = ia_drc_params_struct->drc_frame_size;
60
0
        }
61
3.28k
        gain_index_for_group[0] = 0;
62
6.57k
        for (g = 0; g < str_drc_instruction_str->num_drc_ch_groups - 1; g++) {
63
3.28k
          gain_index_for_group[g + 1] =
64
3.28k
              gain_index_for_group[g] +
65
3.28k
              str_drc_instruction_str->band_count_of_ch_group[g];
66
3.28k
        }
67
3.28k
        signal_index_for_channel[0] = 0;
68
5.64k
        for (c = 0; c < str_drc_instruction_str->audio_num_chan - 1; c++) {
69
2.35k
          if (str_drc_instruction_str->channel_group_of_ch[c] >= 0) {
70
2.35k
            signal_index_for_channel[c + 1] =
71
2.35k
                signal_index_for_channel[c] +
72
2.35k
                str_drc_instruction_str->band_count_of_ch_group
73
2.35k
                    [str_drc_instruction_str->channel_group_of_ch[c]];
74
2.35k
          } else {
75
0
            signal_index_for_channel[c + 1] = signal_index_for_channel[c] + 1;
76
0
          }
77
2.35k
        }
78
79
9.86k
        for (g = 0; g < str_drc_instruction_str->num_drc_ch_groups; g++) {
80
13.1k
          for (b = 0; b < str_drc_instruction_str->band_count_of_ch_group[g];
81
6.57k
               b++) {
82
6.57k
            if (str_drc_instruction_str->ch_group_parametric_drc_flag[g] == 0) {
83
6.57k
              lpcm_gains =
84
6.57k
                  pstr_gain_buf->buf_interpolation[gain_index_for_group[g] + b]
85
6.57k
                      .lpcm_gains +
86
6.57k
                  MAX_SIGNAL_DELAY - ia_drc_params_struct->gain_delay_samples -
87
6.57k
                  ia_drc_params_struct->audio_delay_samples + offset;
88
6.57k
            } 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
6.57k
            i_end = 0;
98
6.57k
            i_start = 0;
99
13.1k
            while (i_end < ia_drc_params_struct->drc_frame_size) {
100
6.57k
              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
6.57k
              } else {
107
6.57k
                i_end = ia_drc_params_struct->drc_frame_size;
108
6.57k
              }
109
110
17.8k
              for (c = 0; c < str_drc_instruction_str->audio_num_chan; c++)
111
112
11.2k
              {
113
11.2k
                if (g == str_drc_instruction_str->channel_group_of_ch[c]) {
114
5.64k
                  signal_index = signal_index_for_channel[c] + b;
115
116
5.64k
                  if (impd_apply_gains == 1) {
117
5.64k
                    impd_shape_filt_block_time_process(
118
5.64k
                        &shape_filter_block[g], &lpcm_gains[0], signal_index,
119
5.64k
                        &deinterleaved_audio[signal_index][0], i_start, i_end);
120
121
5.64k
                  } 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
5.64k
                }
127
11.2k
              }
128
6.57k
              if ((i_end < ia_drc_params_struct->drc_frame_size) &&
129
6.57k
                  (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
6.57k
              if ((i_end == i_start) &&
134
6.57k
                  (drc_gain_last == shape_filter_block[g].drc_gain_last))
135
0
                break;
136
6.57k
              i_start = i_end;
137
6.57k
            }
138
6.57k
          }
139
6.57k
        }
140
3.28k
      }
141
3.28k
    }
142
3.28k
  }
143
144
3.28k
  signal_index = 0;
145
146
3.28k
  if (str_drc_instruction_str->drc_set_id > 0) {
147
8.93k
    for (c = 0; c < str_drc_instruction_str->audio_num_chan; c++)
148
149
5.64k
    {
150
5.64k
      g = str_drc_instruction_str->channel_group_of_ch[c];
151
5.64k
      if (g >= 0) {
152
12.9M
        for (i = 0; i < ia_drc_params_struct->drc_frame_size; i++) {
153
12.9M
          sum = 0.0f;
154
25.8M
          for (b = 0; b < str_drc_instruction_str->band_count_of_ch_group[g];
155
12.9M
               b++) {
156
12.9M
            sum += deinterleaved_audio[signal_index + b][i];
157
12.9M
          }
158
159
12.9M
          channel_audio[c][i] = sum;
160
12.9M
        }
161
5.64k
        signal_index += str_drc_instruction_str->band_count_of_ch_group[g];
162
5.64k
      } 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
5.64k
    }
169
3.28k
  } 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
3.28k
  return;
180
3.28k
}
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
3.28k
                          const WORD32 passThru) {
190
3.28k
  WORD32 c, g, e, i, num_bands;
191
  // WORD32 err = 0;
192
3.28k
  FLOAT32* audio_in;
193
3.28k
  FLOAT32** audio_out;
194
3.28k
  ia_drc_filter_bank_struct* str_drc_filter_bank;
195
3.28k
  ia_drc_instructions_struct* str_drc_instruction_str;
196
3.28k
  WORD32 drc_frame_size = ia_drc_params_struct->drc_frame_size;
197
198
3.28k
  if (drc_instructions_index >= 0) {
199
3.28k
    str_drc_instruction_str =
200
3.28k
        &(pstr_drc_instruction_arr[drc_instructions_index]);
201
3.28k
  } else {
202
0
    return -1;
203
0
  }
204
205
3.28k
  e = 0;
206
207
8.93k
  for (c = 0; c < str_drc_instruction_str->audio_num_chan; c++)
208
209
5.64k
  {
210
5.64k
    str_drc_filter_bank = NULL;
211
212
5.64k
    audio_in = audio_io_buf[c];
213
214
5.64k
    audio_out = &(audio_band_buffer->non_interleaved_audio[e]);
215
5.64k
    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
5.64k
    } else {
247
5.64k
      num_bands = 1;
248
5.64k
    }
249
5.64k
    switch (num_bands) {
250
5.64k
      case 1:
251
12.9M
        for (i = 0; i < drc_frame_size; i++) {
252
12.9M
          audio_out[0][i] = audio_in[i];
253
12.9M
        }
254
5.64k
        e++;
255
5.64k
        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
5.64k
    }
276
5.64k
  }
277
278
3.28k
  return (0);
279
3.28k
}
280
281
VOID impd_store_audio_io_buffer_time(
282
36.9k
    FLOAT32* audio_in_out_buf[], ia_audio_in_out_buf* audio_io_buf_internal) {
283
36.9k
  WORD32 i, j;
284
285
36.9k
  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
36.9k
  } else {
294
36.9k
    audio_io_buf_internal->audio_io_buffer_delayed = audio_in_out_buf;
295
36.9k
    audio_io_buf_internal->audio_in_out_buf = audio_in_out_buf;
296
36.9k
  }
297
298
36.9k
  return;
299
36.9k
}
300
301
VOID impd_retrieve_audio_io_buffer_time(
302
13.0k
    FLOAT32* audio_in_out_buf[], ia_audio_in_out_buf* audio_io_buf_internal) {
303
13.0k
  WORD32 i, j;
304
305
13.0k
  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.0k
  return;
315
13.0k
}
316
317
VOID impd_advance_audio_io_buffer_time(
318
36.1k
    ia_audio_in_out_buf* audio_io_buf_internal) {
319
36.1k
  WORD32 i;
320
36.1k
  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
36.1k
  return;
331
36.1k
}