Coverage Report

Created: 2024-06-21 06:45

/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
147k
                    FLOAT32 *ptr_weight_ms_lr_pe_ratio) {
53
147k
  FLOAT32 *ptr_sfb_enegry_left = ptr_psy_data[0]->sfb_energy.long_nrg;
54
147k
  FLOAT32 *ptr_sfb_energy_right = ptr_psy_data[1]->sfb_energy.long_nrg;
55
147k
  const FLOAT32 *ptr_sfb_energy_mid = ptr_psy_data[0]->sfb_energy_ms.long_nrg;
56
147k
  const FLOAT32 *ptr_sfb_energy_side = ptr_psy_data[1]->sfb_energy_ms.long_nrg;
57
147k
  FLOAT32 *ptr_sfb_thr_left = ptr_psy_data[0]->sfb_threshold.long_nrg;
58
147k
  FLOAT32 *ptr_sfb_thr_right = ptr_psy_data[1]->sfb_threshold.long_nrg;
59
147k
  FLOAT32 *ptr_sfb_spread_energy_left = ptr_psy_data[0]->sfb_sreaded_energy.long_nrg;
60
147k
  FLOAT32 *ptr_sfb_spread_energy_right = ptr_psy_data[1]->sfb_sreaded_energy.long_nrg;
61
147k
  WORD32 sfb, sfb_offsets, j;
62
147k
  WORD32 grp = 0;
63
147k
  WORD32 ms_counter = 0;
64
147k
  WORD32 lr_counter = 0;
65
147k
  FLOAT32 sum_ss_sr_pe_ratio = 0;
66
147k
  WORD32 cnt = 0;
67
147k
  FLOAT32 atan_val;
68
147k
  *ptr_ms_select = 0;
69
70
392k
  for (sfb = 0; sfb < sfb_count; sfb += sfb_per_group, grp++) {
71
4.73M
    for (sfb_offsets = 0; sfb_offsets < max_sfb_per_grp; sfb_offsets++) {
72
4.48M
      FLOAT32 left_right, mid_side, min_thr;
73
4.48M
      WORD32 use_ms;
74
4.48M
      ptr_ms_used[sfb + sfb_offsets] = 0;
75
76
4.48M
      min_thr = MIN(ptr_sfb_thr_left[sfb + sfb_offsets], ptr_sfb_thr_right[sfb + sfb_offsets]);
77
78
4.48M
      left_right =
79
4.48M
          (ptr_sfb_thr_left[sfb + sfb_offsets] /
80
4.48M
           MAX(ptr_sfb_enegry_left[sfb + sfb_offsets], ptr_sfb_thr_left[sfb + sfb_offsets])) *
81
4.48M
          (ptr_sfb_thr_right[sfb + sfb_offsets] /
82
4.48M
           MAX(ptr_sfb_energy_right[sfb + sfb_offsets], ptr_sfb_thr_right[sfb + sfb_offsets]));
83
84
4.48M
      mid_side = (min_thr / MAX(ptr_sfb_energy_mid[sfb + sfb_offsets], min_thr)) *
85
4.48M
                 (min_thr / MAX(ptr_sfb_energy_side[sfb + sfb_offsets], min_thr));
86
87
4.48M
      sum_ss_sr_pe_ratio += (left_right + 1.0e-9f) / (mid_side + 1.0e-9f);
88
4.48M
      cnt++;
89
4.48M
      use_ms = (mid_side >= left_right);
90
91
4.48M
      if (use_ms) {
92
4.30M
        ptr_ms_used[sfb + sfb_offsets] = 1;
93
94
66.7M
        for (j = ptr_sfb_offset[sfb + sfb_offsets]; j < ptr_sfb_offset[sfb + sfb_offsets + 1];
95
62.4M
             j++) {
96
62.4M
          FLOAT32 tmp = ptr_spec_left[j];
97
98
62.4M
          ptr_spec_left[j] = 0.5f * (ptr_spec_left[j] + ptr_spec_right[j]);
99
100
62.4M
          ptr_spec_right[j] = 0.5f * (tmp - ptr_spec_right[j]);
101
62.4M
        }
102
103
4.30M
        ptr_sfb_thr_left[sfb + sfb_offsets] = ptr_sfb_thr_right[sfb + sfb_offsets] = min_thr;
104
105
4.30M
        ptr_sfb_enegry_left[sfb + sfb_offsets] = ptr_sfb_energy_mid[sfb + sfb_offsets];
106
4.30M
        ptr_sfb_energy_right[sfb + sfb_offsets] = ptr_sfb_energy_side[sfb + sfb_offsets];
107
108
4.30M
        ptr_sfb_spread_energy_left[sfb + sfb_offsets] =
109
4.30M
            ptr_sfb_spread_energy_right[sfb + sfb_offsets] =
110
4.30M
                MIN(ptr_sfb_spread_energy_left[sfb + sfb_offsets],
111
4.30M
                    ptr_sfb_spread_energy_right[sfb + sfb_offsets]) *
112
4.30M
                0.5f;
113
114
4.30M
        ms_counter++;
115
4.30M
      } else {
116
176k
        ptr_ms_used[sfb + sfb_offsets] = 0;
117
176k
        lr_counter++;
118
176k
      }
119
4.48M
    }
120
245k
  }
121
122
147k
  if (ms_counter == 0) {
123
14.9k
    *ptr_ms_select = 0;
124
132k
  } else {
125
132k
    if (lr_counter != 0) {
126
16.4k
      *ptr_ms_select = 1;
127
115k
    } else {
128
115k
      *ptr_ms_select = 2;
129
115k
    }
130
132k
  }
131
132
147k
  cnt = MAX(1, cnt);
133
134
147k
  atan_val = iaace_atan_approx((FLOAT32)(0.37f * (sum_ss_sr_pe_ratio / cnt - 6.5f)));
135
136
147k
  *ptr_weight_ms_lr_pe_ratio = (FLOAT32)((0.28f * atan_val) + 1.25f);
137
147k
}