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