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 <stdlib.h>
22 #include <string.h>
23 #include <math.h>
24 #include "ixheaac_type_def.h"
25 #include "ixheaac_error_standards.h"
26 #include "ixheaace_error_codes.h"
27 #include "ixheaace_mps_common_fix.h"
28 #include "ixheaace_mps_defines.h"
29 #include "ixheaace_mps_common_define.h"
30 #include "ixheaace_mps_onset_detect.h"
31 #include "ixheaace_mps_vector_functions.h"
32 #include "ixheaac_constants.h"
33 #include "ixheaac_basic_ops32.h"
34 #include "ixheaac_basic_ops40.h"
35 #include "ixheaac_basic_ops.h"
36
ixheaace_mps_212_onset_detect_init(ixheaace_mps_pstr_onset_detect pstr_onset_detect,const ixheaace_mps_pstr_onset_detect_config pstr_onset_detect_config,const UWORD32 init_flag)37 IA_ERRORCODE ixheaace_mps_212_onset_detect_init(
38 ixheaace_mps_pstr_onset_detect pstr_onset_detect,
39 const ixheaace_mps_pstr_onset_detect_config pstr_onset_detect_config,
40 const UWORD32 init_flag) {
41 IA_ERRORCODE error = IA_NO_ERROR;
42 WORD32 slot;
43 if ((pstr_onset_detect_config->max_time_slots > pstr_onset_detect->max_time_slots) ||
44 (pstr_onset_detect_config->upper_bound_onset_detection <
45 pstr_onset_detect->lower_bound_onset_detection)) {
46 return IA_EXHEAACE_CONFIG_NONFATAL_MPS_INVALID_CONFIG;
47 }
48 pstr_onset_detect->avg_energy_dist_scale = 4;
49 pstr_onset_detect->min_trans_dist = 8;
50 pstr_onset_detect->avg_energy_dist = 16;
51 pstr_onset_detect->lower_bound_onset_detection =
52 pstr_onset_detect_config->lower_bound_onset_detection;
53 pstr_onset_detect->upper_bound_onset_detection =
54 pstr_onset_detect_config->upper_bound_onset_detection;
55 pstr_onset_detect->max_time_slots = pstr_onset_detect_config->max_time_slots;
56 if (init_flag) {
57 for (slot = 0; slot < pstr_onset_detect->avg_energy_dist + pstr_onset_detect->max_time_slots;
58 slot++) {
59 pstr_onset_detect->p_energy_hist[slot] = EPSILON;
60 }
61 }
62 return error;
63 }
64
65 IA_ERRORCODE
ixheaace_mps_212_onset_detect_update(ixheaace_mps_pstr_onset_detect pstr_onset_detect,const WORD32 time_slots)66 ixheaace_mps_212_onset_detect_update(ixheaace_mps_pstr_onset_detect pstr_onset_detect,
67 const WORD32 time_slots) {
68 IA_ERRORCODE error = IA_NO_ERROR;
69 WORD32 slot;
70 if (time_slots > pstr_onset_detect->max_time_slots) {
71 error = IA_EXHEAACE_CONFIG_NONFATAL_MPS_INVALID_CONFIG;
72 } else {
73 for (slot = 0; slot < pstr_onset_detect->avg_energy_dist; slot++) {
74 pstr_onset_detect->p_energy_hist[slot] =
75 pstr_onset_detect->p_energy_hist[slot + time_slots];
76 }
77
78 for (slot = 0; slot < time_slots; slot++) {
79 pstr_onset_detect->p_energy_hist[pstr_onset_detect->avg_energy_dist + slot] = EPSILON;
80 }
81 }
82 return error;
83 }
84
ixheaace_mps_212_onset_detect_apply(ixheaace_mps_pstr_onset_detect pstr_onset_detect,const WORD32 num_time_slots,ixheaace_cmplx_str pp_hybrid_data[MAX_ANA_TIME_SLOT][MAX_QMF_BANDS],const WORD32 prev_pos,WORD32 p_transient_pos[MAX_NUM_TRANS])85 IA_ERRORCODE ixheaace_mps_212_onset_detect_apply(
86 ixheaace_mps_pstr_onset_detect pstr_onset_detect, const WORD32 num_time_slots,
87 ixheaace_cmplx_str pp_hybrid_data[MAX_ANA_TIME_SLOT][MAX_QMF_BANDS], const WORD32 prev_pos,
88 WORD32 p_transient_pos[MAX_NUM_TRANS]) {
89 IA_ERRORCODE error = IA_NO_ERROR;
90 WORD32 slot, tran_cnt, curr_pos;
91 WORD32 trans;
92 WORD32 lower_bound_onset_detection = pstr_onset_detect->lower_bound_onset_detection;
93 WORD32 upper_bound_onset_detection = pstr_onset_detect->upper_bound_onset_detection;
94 WORD32 avg_energy = pstr_onset_detect->avg_energy_dist;
95 WORD32 curr_pos_prev;
96 FLOAT32 p1, p2;
97 FLOAT32 threshold_square = (FLOAT32)SPACE_ONSET_THRESHOLD_SQUARE_FLT;
98 FLOAT32 *ptr_energy = pstr_onset_detect->p_energy_hist;
99 FLOAT32 energy[16 + MAX_TIME_SLOTS];
100 memset(energy, 0, (16 + MAX_TIME_SLOTS) * sizeof(FLOAT32));
101 tran_cnt = 0;
102 p2 = 0.0f;
103 for (trans = 0; trans < MAX_NUM_TRANS; trans++) {
104 p_transient_pos[trans] = -1;
105 }
106
107 if (prev_pos <= 0) {
108 curr_pos = num_time_slots;
109 } else {
110 curr_pos = MAX(num_time_slots, prev_pos - num_time_slots + pstr_onset_detect->min_trans_dist);
111 }
112
113 for (slot = 0; slot < num_time_slots; slot++) {
114 ptr_energy[avg_energy + slot] = ixheaace_mps_212_sum_up_cplx_pow_2(
115 &pp_hybrid_data[slot][lower_bound_onset_detection + 1],
116 upper_bound_onset_detection - lower_bound_onset_detection - 1);
117 }
118
119 for (slot = 0; slot < (num_time_slots + avg_energy); slot++) {
120 energy[slot] = ptr_energy[slot];
121 }
122
123 curr_pos_prev = curr_pos;
124 for (; (curr_pos < (num_time_slots << 1)) && (tran_cnt < MAX_NUM_TRANS); curr_pos++) {
125 p1 = energy[curr_pos - num_time_slots + avg_energy] * threshold_square;
126
127 if (curr_pos_prev != (curr_pos - 1)) {
128 p2 = 0.0f;
129 for (slot = 0; slot < avg_energy; slot++) {
130 p2 += energy[curr_pos - num_time_slots + slot];
131 }
132 } else {
133 p2 += energy[curr_pos - num_time_slots + avg_energy - 1];
134 #ifdef _WIN32
135 #pragma warning(suppress : 6385)
136 #endif
137 p2 -= energy[curr_pos_prev - num_time_slots];
138 }
139 curr_pos_prev = curr_pos;
140
141 if (p1 > p2) {
142 p_transient_pos[tran_cnt++] = curr_pos;
143 curr_pos += pstr_onset_detect->min_trans_dist;
144 }
145 }
146
147 return error;
148 }
149