Coverage Report

Created: 2024-06-21 06:45

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