/src/libxaac/encoder/ixheaace_mps_onset_detect.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 <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 | | |
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 | 2.82k | const UWORD32 init_flag) { |
41 | 2.82k | IA_ERRORCODE error = IA_NO_ERROR; |
42 | 2.82k | WORD32 slot; |
43 | 2.82k | if ((pstr_onset_detect_config->max_time_slots > pstr_onset_detect->max_time_slots) || |
44 | 2.82k | (pstr_onset_detect_config->upper_bound_onset_detection < |
45 | 2.82k | pstr_onset_detect->lower_bound_onset_detection)) { |
46 | 0 | return IA_EXHEAACE_CONFIG_NONFATAL_MPS_INVALID_CONFIG; |
47 | 0 | } |
48 | 2.82k | pstr_onset_detect->avg_energy_dist_scale = 4; |
49 | 2.82k | pstr_onset_detect->min_trans_dist = 8; |
50 | 2.82k | pstr_onset_detect->avg_energy_dist = 16; |
51 | 2.82k | pstr_onset_detect->lower_bound_onset_detection = |
52 | 2.82k | pstr_onset_detect_config->lower_bound_onset_detection; |
53 | 2.82k | pstr_onset_detect->upper_bound_onset_detection = |
54 | 2.82k | pstr_onset_detect_config->upper_bound_onset_detection; |
55 | 2.82k | pstr_onset_detect->max_time_slots = pstr_onset_detect_config->max_time_slots; |
56 | 2.82k | if (init_flag) { |
57 | 141k | for (slot = 0; slot < pstr_onset_detect->avg_energy_dist + pstr_onset_detect->max_time_slots; |
58 | 139k | slot++) { |
59 | 139k | pstr_onset_detect->p_energy_hist[slot] = EPSILON; |
60 | 139k | } |
61 | 2.82k | } |
62 | 2.82k | return error; |
63 | 2.82k | } |
64 | | |
65 | | IA_ERRORCODE |
66 | | ixheaace_mps_212_onset_detect_update(ixheaace_mps_pstr_onset_detect pstr_onset_detect, |
67 | 196k | const WORD32 time_slots) { |
68 | 196k | IA_ERRORCODE error = IA_NO_ERROR; |
69 | 196k | WORD32 slot; |
70 | 196k | if (time_slots > pstr_onset_detect->max_time_slots) { |
71 | 0 | error = IA_EXHEAACE_CONFIG_NONFATAL_MPS_INVALID_CONFIG; |
72 | 196k | } else { |
73 | 3.33M | for (slot = 0; slot < pstr_onset_detect->avg_energy_dist; slot++) { |
74 | 3.13M | pstr_onset_detect->p_energy_hist[slot] = |
75 | 3.13M | pstr_onset_detect->p_energy_hist[slot + time_slots]; |
76 | 3.13M | } |
77 | | |
78 | 7.04M | for (slot = 0; slot < time_slots; slot++) { |
79 | 6.84M | pstr_onset_detect->p_energy_hist[pstr_onset_detect->avg_energy_dist + slot] = EPSILON; |
80 | 6.84M | } |
81 | 196k | } |
82 | 196k | return error; |
83 | 196k | } |
84 | | |
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 | 196k | WORD32 p_transient_pos[MAX_NUM_TRANS]) { |
89 | 196k | IA_ERRORCODE error = IA_NO_ERROR; |
90 | 196k | WORD32 slot, tran_cnt, curr_pos; |
91 | 196k | WORD32 trans; |
92 | 196k | WORD32 lower_bound_onset_detection = pstr_onset_detect->lower_bound_onset_detection; |
93 | 196k | WORD32 upper_bound_onset_detection = pstr_onset_detect->upper_bound_onset_detection; |
94 | 196k | WORD32 avg_energy = pstr_onset_detect->avg_energy_dist; |
95 | 196k | WORD32 curr_pos_prev; |
96 | 196k | FLOAT32 p1, p2; |
97 | 196k | FLOAT32 threshold_square = (FLOAT32)SPACE_ONSET_THRESHOLD_SQUARE_FLT; |
98 | 196k | FLOAT32 *ptr_energy = pstr_onset_detect->p_energy_hist; |
99 | 196k | FLOAT32 energy[16 + MAX_TIME_SLOTS]; |
100 | 196k | memset(energy, 0, (16 + MAX_TIME_SLOTS) * sizeof(FLOAT32)); |
101 | 196k | tran_cnt = 0; |
102 | 196k | p2 = 0.0f; |
103 | 784k | for (trans = 0; trans < MAX_NUM_TRANS; trans++) { |
104 | 588k | p_transient_pos[trans] = -1; |
105 | 588k | } |
106 | | |
107 | 196k | if (prev_pos <= 0) { |
108 | 196k | curr_pos = num_time_slots; |
109 | 196k | } else { |
110 | 0 | curr_pos = MAX(num_time_slots, prev_pos - num_time_slots + pstr_onset_detect->min_trans_dist); |
111 | 0 | } |
112 | | |
113 | 7.04M | for (slot = 0; slot < num_time_slots; slot++) { |
114 | 6.84M | ptr_energy[avg_energy + slot] = ixheaace_mps_212_sum_up_cplx_pow_2( |
115 | 6.84M | &pp_hybrid_data[slot][lower_bound_onset_detection + 1], |
116 | 6.84M | upper_bound_onset_detection - lower_bound_onset_detection - 1); |
117 | 6.84M | } |
118 | | |
119 | 10.1M | for (slot = 0; slot < (num_time_slots + avg_energy); slot++) { |
120 | 9.98M | energy[slot] = ptr_energy[slot]; |
121 | 9.98M | } |
122 | | |
123 | 196k | curr_pos_prev = curr_pos; |
124 | 6.58M | for (; (curr_pos < (num_time_slots << 1)) && (tran_cnt < MAX_NUM_TRANS); curr_pos++) { |
125 | 6.38M | p1 = energy[curr_pos - num_time_slots + avg_energy] * threshold_square; |
126 | | |
127 | 6.38M | if (curr_pos_prev != (curr_pos - 1)) { |
128 | 249k | p2 = 0.0f; |
129 | 4.24M | for (slot = 0; slot < avg_energy; slot++) { |
130 | 3.99M | p2 += energy[curr_pos - num_time_slots + slot]; |
131 | 3.99M | } |
132 | 6.13M | } else { |
133 | 6.13M | p2 += energy[curr_pos - num_time_slots + avg_energy - 1]; |
134 | | #ifdef _WIN32 |
135 | | #pragma warning(suppress : 6385) |
136 | | #endif |
137 | 6.13M | p2 -= energy[curr_pos_prev - num_time_slots]; |
138 | 6.13M | } |
139 | 6.38M | curr_pos_prev = curr_pos; |
140 | | |
141 | 6.38M | if (p1 > p2) { |
142 | 58.8k | p_transient_pos[tran_cnt++] = curr_pos; |
143 | 58.8k | curr_pos += pstr_onset_detect->min_trans_dist; |
144 | 58.8k | } |
145 | 6.38M | } |
146 | | |
147 | 196k | return error; |
148 | 196k | } |