Coverage Report

Created: 2025-08-26 06:53

/src/libxaac/encoder/ixheaace_ms_stereo.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 "ixheaac_type_def.h"
22
#include "ixheaac_constants.h"
23
#include "ixheaace_aac_constants.h"
24
#include <stdlib.h>
25
26
#include "ixheaace_psy_const.h"
27
#include "ixheaace_tns.h"
28
#include "ixheaace_tns_params.h"
29
#include "ixheaace_rom.h"
30
#include "ixheaace_common_rom.h"
31
#include "ixheaace_bitbuffer.h"
32
33
#include "ixheaac_basic_ops32.h"
34
#include "ixheaac_basic_ops16.h"
35
#include "ixheaac_basic_ops40.h"
36
#include "ixheaac_basic_ops.h"
37
38
#include <math.h>
39
#include "ixheaace_block_switch.h"
40
#include "ixheaace_psy_utils_spreading.h"
41
#include "ixheaace_psy_utils.h"
42
#include "ixheaace_calc_ms_band_energy.h"
43
#include "ixheaace_psy_configuration.h"
44
#include "ixheaace_psy_data.h"
45
#include "ixheaace_ms_stereo.h"
46
#include "ixheaace_common_utils.h"
47
48
VOID iaace_ms_apply(ixheaace_psy_data **ptr_psy_data, FLOAT32 *ptr_spec_left,
49
                    FLOAT32 *ptr_spec_right, WORD32 *ptr_ms_select, WORD32 *ptr_ms_used,
50
                    const WORD32 sfb_count, const WORD32 sfb_per_group,
51
                    const WORD32 max_sfb_per_grp, const WORD32 *ptr_sfb_offset,
52
167k
                    FLOAT32 *ptr_weight_ms_lr_pe_ratio) {
53
167k
  FLOAT32 *ptr_sfb_enegry_left = ptr_psy_data[0]->sfb_energy.long_nrg;
54
167k
  FLOAT32 *ptr_sfb_energy_right = ptr_psy_data[1]->sfb_energy.long_nrg;
55
167k
  const FLOAT32 *ptr_sfb_energy_mid = ptr_psy_data[0]->sfb_energy_ms.long_nrg;
56
167k
  const FLOAT32 *ptr_sfb_energy_side = ptr_psy_data[1]->sfb_energy_ms.long_nrg;
57
167k
  FLOAT32 *ptr_sfb_thr_left = ptr_psy_data[0]->sfb_threshold.long_nrg;
58
167k
  FLOAT32 *ptr_sfb_thr_right = ptr_psy_data[1]->sfb_threshold.long_nrg;
59
167k
  FLOAT32 *ptr_sfb_spread_energy_left = ptr_psy_data[0]->sfb_sreaded_energy.long_nrg;
60
167k
  FLOAT32 *ptr_sfb_spread_energy_right = ptr_psy_data[1]->sfb_sreaded_energy.long_nrg;
61
167k
  WORD32 sfb, sfb_offsets, j;
62
167k
  WORD32 grp = 0;
63
167k
  WORD32 ms_counter = 0;
64
167k
  WORD32 lr_counter = 0;
65
167k
  FLOAT32 sum_ss_sr_pe_ratio = 0;
66
167k
  WORD32 cnt = 0;
67
167k
  FLOAT32 atan_val;
68
167k
  *ptr_ms_select = 0;
69
70
446k
  for (sfb = 0; sfb < sfb_count; sfb += sfb_per_group, grp++) {
71
5.30M
    for (sfb_offsets = 0; sfb_offsets < max_sfb_per_grp; sfb_offsets++) {
72
5.02M
      FLOAT32 left_right, mid_side, min_thr;
73
5.02M
      WORD32 use_ms;
74
5.02M
      ptr_ms_used[sfb + sfb_offsets] = 0;
75
76
5.02M
      min_thr = MIN(ptr_sfb_thr_left[sfb + sfb_offsets], ptr_sfb_thr_right[sfb + sfb_offsets]);
77
78
5.02M
      left_right =
79
5.02M
          (ptr_sfb_thr_left[sfb + sfb_offsets] /
80
5.02M
           MAX(ptr_sfb_enegry_left[sfb + sfb_offsets], ptr_sfb_thr_left[sfb + sfb_offsets])) *
81
5.02M
          (ptr_sfb_thr_right[sfb + sfb_offsets] /
82
5.02M
           MAX(ptr_sfb_energy_right[sfb + sfb_offsets], ptr_sfb_thr_right[sfb + sfb_offsets]));
83
84
5.02M
      mid_side = (min_thr / MAX(ptr_sfb_energy_mid[sfb + sfb_offsets], min_thr)) *
85
5.02M
                 (min_thr / MAX(ptr_sfb_energy_side[sfb + sfb_offsets], min_thr));
86
87
5.02M
      sum_ss_sr_pe_ratio += (left_right + 1.0e-9f) / (mid_side + 1.0e-9f);
88
5.02M
      cnt++;
89
5.02M
      use_ms = (mid_side >= left_right);
90
91
5.02M
      if (use_ms) {
92
4.84M
        ptr_ms_used[sfb + sfb_offsets] = 1;
93
94
71.9M
        for (j = ptr_sfb_offset[sfb + sfb_offsets]; j < ptr_sfb_offset[sfb + sfb_offsets + 1];
95
67.0M
             j++) {
96
67.0M
          FLOAT32 tmp = ptr_spec_left[j];
97
98
67.0M
          ptr_spec_left[j] = 0.5f * (ptr_spec_left[j] + ptr_spec_right[j]);
99
100
67.0M
          ptr_spec_right[j] = 0.5f * (tmp - ptr_spec_right[j]);
101
67.0M
        }
102
103
4.84M
        ptr_sfb_thr_left[sfb + sfb_offsets] = ptr_sfb_thr_right[sfb + sfb_offsets] = min_thr;
104
105
4.84M
        ptr_sfb_enegry_left[sfb + sfb_offsets] = ptr_sfb_energy_mid[sfb + sfb_offsets];
106
4.84M
        ptr_sfb_energy_right[sfb + sfb_offsets] = ptr_sfb_energy_side[sfb + sfb_offsets];
107
108
4.84M
        ptr_sfb_spread_energy_left[sfb + sfb_offsets] =
109
4.84M
            ptr_sfb_spread_energy_right[sfb + sfb_offsets] =
110
4.84M
                MIN(ptr_sfb_spread_energy_left[sfb + sfb_offsets],
111
4.84M
                    ptr_sfb_spread_energy_right[sfb + sfb_offsets]) *
112
4.84M
                0.5f;
113
114
4.84M
        ms_counter++;
115
4.84M
      } else {
116
177k
        ptr_ms_used[sfb + sfb_offsets] = 0;
117
177k
        lr_counter++;
118
177k
      }
119
5.02M
    }
120
279k
  }
121
122
167k
  if (ms_counter == 0) {
123
13.6k
    *ptr_ms_select = 0;
124
153k
  } else {
125
153k
    if (lr_counter != 0) {
126
18.2k
      *ptr_ms_select = 1;
127
135k
    } else {
128
135k
      *ptr_ms_select = 2;
129
135k
    }
130
153k
  }
131
132
167k
  cnt = MAX(1, cnt);
133
134
167k
  atan_val = iaace_atan_approx((FLOAT32)(0.37f * (sum_ss_sr_pe_ratio / cnt - 6.5f)));
135
136
167k
  *ptr_weight_ms_lr_pe_ratio = (FLOAT32)((0.28f * atan_val) + 1.25f);
137
167k
}