• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string.h>
22 #include <limits.h>
23 
24 #include "ixheaac_type_def.h"
25 #include "ixheaac_constants.h"
26 #include "ixheaac_error_standards.h"
27 #include "ixheaace_error_codes.h"
28 #include "ixheaace_aac_constants.h"
29 #include "ixheaac_basic_ops32.h"
30 #include "ixheaac_basic_ops16.h"
31 #include "ixheaac_basic_ops40.h"
32 #include "ixheaac_basic_ops.h"
33 #include "ixheaace_sbr_header.h"
34 #include "ixheaace_sbr_def.h"
35 #include "ixheaace_resampler.h"
36 #include "ixheaace_sbr_rom.h"
37 #include "ixheaace_common_rom.h"
38 #include "ixheaace_sbr_hbe.h"
39 #include "ixheaace_sbr_qmf_enc.h"
40 #include "ixheaace_sbr_tran_det.h"
41 #include "ixheaace_sbr_frame_info_gen.h"
42 #include "ixheaace_sbr_env_est.h"
43 #include "ixheaace_sbr_code_envelope.h"
44 #include "ixheaace_sbr_main.h"
45 #include "ixheaace_sbr_missing_harmonics_det.h"
46 #include "ixheaace_sbr_inv_filtering_estimation.h"
47 #include "ixheaace_sbr_noise_floor_est.h"
48 
49 #include "ixheaace_common_rom.h"
50 #include "ixheaace_sbr_ton_corr.h"
51 #include "iusace_esbr_pvc.h"
52 #include "iusace_esbr_inter_tes.h"
53 #include "ixheaace_sbr.h"
54 #include "ixheaace_bitbuffer.h"
55 #include "ixheaace_sbr_cmondata.h"
56 #include "ixheaace_sbr_write_bitstream.h"
57 #include "ixheaace_sbr_hybrid.h"
58 #include "ixheaace_sbr_ps_enc.h"
59 
60 IA_ERRORCODE
ixheaace_create_extract_sbr_envelope(WORD32 ch,ixheaace_pstr_sbr_extract_envelope pstr_sbr_ext_env,WORD32 start_index,WORD32 * ptr_common_buffer2,FLOAT32 * ptr_sbr_env_r_buf,FLOAT32 * ptr_sbr_env_i_buf,WORD32 is_ld_sbr,WORD32 frame_flag_480,ixheaace_sbr_codec_type sbr_codec)61 ixheaace_create_extract_sbr_envelope(WORD32 ch,
62                                      ixheaace_pstr_sbr_extract_envelope pstr_sbr_ext_env,
63                                      WORD32 start_index, WORD32 *ptr_common_buffer2,
64                                      FLOAT32 *ptr_sbr_env_r_buf, FLOAT32 *ptr_sbr_env_i_buf,
65                                      WORD32 is_ld_sbr, WORD32 frame_flag_480,
66                                      ixheaace_sbr_codec_type sbr_codec) {
67   WORD32 i;
68   WORD32 y_buffer_length, r_buffer_length;
69   WORD32 offset = 0;
70   WORD32 y_buffer_write_offset = 32;
71   WORD32 no_cols = 32;
72   WORD32 time_slots = 16;
73   WORD32 sbr_ratio_idx = pstr_sbr_ext_env->sbr_ratio_idx;
74   WORD32 qmf_time_slots = IXHEAACE_QMF_TIME_SLOTS;
75   FLOAT32 *ptr_buffer = NULL;
76   FLOAT32 *ptr_i_buffer = NULL;
77   memset(pstr_sbr_ext_env, 0, sizeof(ixheaace_str_sbr_extr_env));
78 
79   pstr_sbr_ext_env->pre_transient_info[0] = 0;
80   pstr_sbr_ext_env->pre_transient_info[1] = 0;
81 
82   if (sbr_codec == ELD_SBR) {
83     if (frame_flag_480) {
84       no_cols = 30;
85       time_slots = 15;
86     }
87 
88     y_buffer_write_offset = time_slots / 2;
89     pstr_sbr_ext_env->y_buffer_write_offset = y_buffer_write_offset;
90     pstr_sbr_ext_env->no_cols = no_cols;
91     pstr_sbr_ext_env->no_rows = 64;
92     pstr_sbr_ext_env->start_index = start_index;
93     pstr_sbr_ext_env->time_slots = time_slots;
94     pstr_sbr_ext_env->time_step = 1;
95 
96     y_buffer_length = y_buffer_write_offset + time_slots;
97     r_buffer_length = time_slots;
98   } else {
99     if ((sbr_codec == USAC_SBR) && (USAC_SBR_RATIO_INDEX_4_1 == sbr_ratio_idx)) {
100       qmf_time_slots = QMF_TIME_SLOTS_USAC_4_1;
101       y_buffer_write_offset = QMF_TIME_SLOTS_USAC_4_1;
102       no_cols = qmf_time_slots;
103     }
104 
105     pstr_sbr_ext_env->y_buffer_write_offset = y_buffer_write_offset;
106 
107     y_buffer_length = pstr_sbr_ext_env->y_buffer_write_offset + y_buffer_write_offset;
108 
109     r_buffer_length = y_buffer_write_offset;
110 
111     pstr_sbr_ext_env->pre_transient_info[0] = 0;
112     pstr_sbr_ext_env->pre_transient_info[1] = 0;
113 
114     pstr_sbr_ext_env->no_cols = no_cols;
115     pstr_sbr_ext_env->no_rows = 64;
116     pstr_sbr_ext_env->start_index = start_index;
117 
118     pstr_sbr_ext_env->time_slots = time_slots;
119     pstr_sbr_ext_env->time_step = no_cols / time_slots;
120 
121     if ((r_buffer_length != qmf_time_slots) || (y_buffer_length != 2 * qmf_time_slots)) {
122       return IA_EXHEAACE_INIT_FATAL_SBR_INVALID_BUFFER_LENGTH;
123     }
124 
125     y_buffer_length /= 2;
126 
127     if ((sbr_codec == USAC_SBR) && (USAC_SBR_RATIO_INDEX_4_1 == sbr_ratio_idx)) {
128       pstr_sbr_ext_env->y_buffer_write_offset /= 4;
129     } else {
130       pstr_sbr_ext_env->y_buffer_write_offset /= 2;
131     }
132   }
133 
134   pstr_sbr_ext_env->buffer_flag = 0;
135 
136   ptr_buffer =
137       (FLOAT32 *)&ptr_common_buffer2[ch * y_buffer_length * 64 + offset * IXHEAACE_QMF_CHANNELS];
138 
139   i = 0;
140   while (i < pstr_sbr_ext_env->y_buffer_write_offset) {
141     pstr_sbr_ext_env->ptr_y_buffer[i] = ptr_buffer + i * IXHEAACE_QMF_CHANNELS;
142     memset(pstr_sbr_ext_env->ptr_y_buffer[i], 0,
143            IXHEAACE_QMF_CHANNELS * sizeof(pstr_sbr_ext_env->ptr_y_buffer[0]));
144     i++;
145   }
146 
147   ptr_buffer =
148       (FLOAT32 *)&ptr_common_buffer2[ch * y_buffer_length * 64 - offset * IXHEAACE_QMF_CHANNELS];
149   for (i = pstr_sbr_ext_env->y_buffer_write_offset; i < y_buffer_length; i++) {
150     pstr_sbr_ext_env->ptr_y_buffer[i] = ptr_buffer + i * IXHEAACE_QMF_CHANNELS;
151     memset(pstr_sbr_ext_env->ptr_y_buffer[i], 0,
152            IXHEAACE_QMF_CHANNELS * sizeof(pstr_sbr_ext_env->ptr_y_buffer[0]));
153   }
154 
155   ptr_buffer = &ptr_sbr_env_r_buf[ch * qmf_time_slots * IXHEAACE_QMF_CHANNELS];
156   ptr_i_buffer = &ptr_sbr_env_i_buf[ch * qmf_time_slots * IXHEAACE_QMF_CHANNELS];
157   for (i = 0; i < r_buffer_length; i++) {
158     pstr_sbr_ext_env->ptr_r_buffer[i] = ptr_buffer + i * IXHEAACE_QMF_CHANNELS;
159     memset(pstr_sbr_ext_env->ptr_r_buffer[i], 0,
160            IXHEAACE_QMF_CHANNELS * sizeof(pstr_sbr_ext_env->ptr_r_buffer[0]));
161     pstr_sbr_ext_env->ptr_i_buffer[i] = ptr_i_buffer + i * IXHEAACE_QMF_CHANNELS;
162     memset(pstr_sbr_ext_env->ptr_i_buffer[i], 0,
163            IXHEAACE_QMF_CHANNELS * sizeof(pstr_sbr_ext_env->ptr_i_buffer[0]));
164   }
165 
166   memset(pstr_sbr_ext_env->envelope_compensation, 0,
167          sizeof(pstr_sbr_ext_env->envelope_compensation[0]) * MAXIMUM_FREQ_COEFFS);
168 
169   return IA_NO_ERROR;
170 }
171