Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : #include <assert.h>
34 : #include <stdint.h>
35 : #include <math.h>
36 : #include "options.h"
37 : #include "cnst.h"
38 : #include "ivas_cnst.h"
39 : #include "rom_com.h"
40 : #include "prot.h"
41 : #include "ivas_prot.h"
42 : #include "ivas_rom_com.h"
43 : #ifdef DEBUGGING
44 : #include "debug.h"
45 : #endif
46 : #include "wmc_auto.h"
47 :
48 :
49 : /*-------------------------------------------------------------------*
50 : * ivas_sba_getTCs()
51 : *
52 : * Get TCs from Ambisonics signal in ACN
53 : *-------------------------------------------------------------------*/
54 :
55 2349361 : void ivas_sba_getTCs(
56 : float *sba_data[], /* i : SBA signals */
57 : Encoder_Struct *st_ivas, /* i/o: Encoder struct */
58 : const int16_t input_frame /* i : frame length */
59 : )
60 : {
61 2349361 : if ( st_ivas->hEncoderConfig->sba_planar )
62 : {
63 119380 : ivas_sba_zero_vert_comp( sba_data, st_ivas->sba_analysis_order, st_ivas->hEncoderConfig->sba_planar, input_frame );
64 : }
65 :
66 2349361 : st_ivas->nchan_transport = ivas_get_sba_num_TCs( st_ivas->hEncoderConfig->ivas_total_brate, st_ivas->sba_analysis_order );
67 :
68 2349361 : if ( st_ivas->nchan_transport >= 3 )
69 : {
70 : /*convert WYZX downmix to WYXZ*/
71 1012595 : int16_t i = 0;
72 : float temp;
73 787924915 : for ( i = 0; i < input_frame; i++ )
74 : {
75 786912320 : temp = sba_data[2][i];
76 786912320 : sba_data[2][i] = sba_data[3][i];
77 786912320 : sba_data[3][i] = temp;
78 : }
79 : }
80 :
81 : #ifdef DEBUG_MODE_DIRAC
82 : for ( int16_t n = 0; n < st_ivas->nchan_transport; n++ )
83 : {
84 : int16_t tmp[L_FRAME48k];
85 : char file_name[50] = { 0 };
86 :
87 : sprintf( file_name, "./res/ivas_dirac_enc_%d.%d.pcm", n, (int16_t) ( input_frame * 0.05 ) );
88 : for ( int16_t i = 0; i < input_frame; i++ )
89 : {
90 : tmp[i] = (int16_t) ( sba_data[n][i] + 0.5f );
91 : }
92 : dbgwrite( tmp, sizeof( int16_t ), input_frame, 1, file_name );
93 : }
94 : #endif
95 :
96 2349361 : return;
97 : }
98 :
99 : /*-------------------------------------------------------------------*
100 : * ivas_sba_enc_reconfigure()
101 : *
102 : * Reconfigure IVAS SBA encoder
103 : *-------------------------------------------------------------------*/
104 :
105 2125508 : ivas_error ivas_sba_enc_reconfigure(
106 : Encoder_Struct *st_ivas /* i/o: IVAS encoder structure */
107 : )
108 : {
109 : int16_t n, nSCE_old, nCPE_old, nchan_transport_old;
110 : int32_t ivas_total_brate;
111 : ivas_error error;
112 : ENCODER_CONFIG_HANDLE hEncoderConfig;
113 :
114 2125508 : error = IVAS_ERR_OK;
115 2125508 : hEncoderConfig = st_ivas->hEncoderConfig;
116 2125508 : ivas_total_brate = hEncoderConfig->ivas_total_brate;
117 :
118 2125508 : if ( ivas_total_brate != hEncoderConfig->last_ivas_total_brate )
119 : {
120 11474 : DIRAC_ENC_HANDLE hDirAC = st_ivas->hDirAC;
121 : SPAR_ENC_HANDLE hSpar;
122 : int16_t analysis_order_old;
123 : int16_t spar_reconfig_flag;
124 : int16_t nbands_old;
125 : int16_t ndir_old;
126 :
127 11474 : spar_reconfig_flag = 0;
128 11474 : nchan_transport_old = st_ivas->nchan_transport;
129 11474 : nCPE_old = st_ivas->nCPE;
130 11474 : nSCE_old = st_ivas->nSCE;
131 11474 : st_ivas->sba_analysis_order = ivas_sba_get_analysis_order( ivas_total_brate, hEncoderConfig->sba_order );
132 11474 : analysis_order_old = ivas_sba_get_analysis_order( hEncoderConfig->last_ivas_total_brate, hEncoderConfig->sba_order );
133 11474 : nbands_old = st_ivas->hQMetaData->q_direction->cfg.nbands;
134 11474 : ndir_old = st_ivas->hQMetaData->no_directions;
135 :
136 11474 : if ( analysis_order_old != st_ivas->sba_analysis_order )
137 : {
138 : int16_t i, n_old;
139 : float **old_mem_hp20_in;
140 :
141 2467 : n_old = ( analysis_order_old + 1 ) * ( analysis_order_old + 1 );
142 2467 : n = ( st_ivas->sba_analysis_order + 1 ) * ( st_ivas->sba_analysis_order + 1 );
143 :
144 2467 : if ( n > n_old )
145 : {
146 : /* save old mem_hp_20 pointer */
147 1237 : old_mem_hp20_in = st_ivas->mem_hp20_in;
148 1237 : st_ivas->mem_hp20_in = NULL;
149 :
150 1237 : if ( ( st_ivas->mem_hp20_in = (float **) malloc( n * sizeof( float * ) ) ) == NULL )
151 : {
152 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
153 : }
154 :
155 6185 : for ( i = 0; i < n_old; i++ )
156 : {
157 4948 : st_ivas->mem_hp20_in[i] = old_mem_hp20_in[i];
158 4948 : old_mem_hp20_in[i] = NULL;
159 : }
160 : /* create additional hp20 memories */
161 14730 : for ( ; i < n; i++ )
162 : {
163 13493 : if ( ( st_ivas->mem_hp20_in[i] = (float *) malloc( L_HP20_MEM * sizeof( float ) ) ) == NULL )
164 : {
165 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
166 : }
167 :
168 13493 : set_f( st_ivas->mem_hp20_in[i], 0.0f, L_HP20_MEM );
169 : }
170 :
171 1237 : free( old_mem_hp20_in );
172 1237 : old_mem_hp20_in = NULL;
173 : }
174 1230 : else if ( n < n_old )
175 : {
176 : /* save old mem_hp_20 pointer */
177 1230 : old_mem_hp20_in = st_ivas->mem_hp20_in;
178 1230 : st_ivas->mem_hp20_in = NULL;
179 :
180 1230 : if ( ( st_ivas->mem_hp20_in = (float **) malloc( n * sizeof( float * ) ) ) == NULL )
181 : {
182 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for HP20 filter memory\n" ) );
183 : }
184 :
185 6150 : for ( i = 0; i < n; i++ )
186 : {
187 4920 : st_ivas->mem_hp20_in[i] = old_mem_hp20_in[i];
188 4920 : old_mem_hp20_in[i] = NULL;
189 : }
190 : /* remove superfluous hp20 memories */
191 14646 : for ( ; i < n_old; i++ )
192 : {
193 13416 : free( old_mem_hp20_in[i] );
194 13416 : old_mem_hp20_in[i] = NULL;
195 : }
196 :
197 1230 : free( old_mem_hp20_in );
198 1230 : old_mem_hp20_in = NULL;
199 : }
200 : }
201 :
202 11474 : ivas_spar_config( ivas_total_brate, min( st_ivas->sba_analysis_order, IVAS_MAX_SBA_ORDER ), &st_ivas->nchan_transport, &st_ivas->nSCE, &st_ivas->nCPE, &st_ivas->hSpar->core_nominal_brate, -1 );
203 :
204 11474 : hSpar = st_ivas->hSpar;
205 :
206 11474 : if ( st_ivas->nchan_transport == 1 )
207 : {
208 3853 : hEncoderConfig->element_mode_init = IVAS_SCE;
209 : }
210 : else
211 : {
212 7621 : hEncoderConfig->element_mode_init = IVAS_CPE_MDCT;
213 : }
214 :
215 11474 : if ( nchan_transport_old != st_ivas->nchan_transport || ( ivas_total_brate < IVAS_512k && hEncoderConfig->last_ivas_total_brate >= IVAS_512k ) || ( ivas_total_brate >= IVAS_512k && hEncoderConfig->last_ivas_total_brate < IVAS_512k ) )
216 : {
217 : /* FB mixer handle */
218 8712 : if ( hDirAC->hFbMixer != NULL )
219 : {
220 0 : ivas_FB_mixer_close( &( hDirAC->hFbMixer ), hEncoderConfig->input_Fs, 0 );
221 0 : hDirAC->hFbMixer = NULL;
222 : }
223 8712 : spar_reconfig_flag = 1;
224 8712 : ivas_spar_enc_close( &( st_ivas->hSpar ), hEncoderConfig->input_Fs, hEncoderConfig->nchan_inp, spar_reconfig_flag );
225 :
226 8712 : if ( ( error = ivas_spar_enc_open( st_ivas, spar_reconfig_flag ) ) != IVAS_ERR_OK )
227 : {
228 0 : return error;
229 : }
230 : }
231 :
232 11474 : st_ivas->hSpar->spar_reconfig_flag = spar_reconfig_flag;
233 :
234 11474 : if ( ( error = ivas_dirac_enc_reconfigure( st_ivas ) ) != IVAS_ERR_OK )
235 : {
236 0 : return error;
237 : }
238 :
239 11474 : if ( st_ivas->hQMetaData->q_direction->cfg.nbands != nbands_old || st_ivas->hQMetaData->no_directions != ndir_old )
240 : {
241 : int16_t dir, j, i;
242 5012 : IVAS_QDIRECTION *q_direction = st_ivas->hQMetaData->q_direction;
243 10887 : for ( dir = 0; dir < st_ivas->hQMetaData->no_directions; dir++ )
244 : {
245 35476 : for ( j = 0; j < q_direction[dir].cfg.nbands; j++ )
246 : {
247 148005 : for ( i = 0; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
248 : {
249 118404 : q_direction[dir].band_data[j].energy_ratio_index[i] = 0;
250 118404 : q_direction[dir].band_data[j].energy_ratio_index_mod[i] = 0;
251 : }
252 : }
253 : }
254 : }
255 11474 : hSpar->enc_param_start_band = hDirAC->hConfig->enc_param_start_band;
256 :
257 : /*-----------------------------------------------------------------*
258 : * Allocate, initialize, and configure SCE/CPE/MCT handles
259 : *-----------------------------------------------------------------*/
260 :
261 11474 : if ( ( error = ivas_corecoder_enc_reconfig( st_ivas, nSCE_old, nCPE_old, nchan_transport_old, ivas_total_brate / st_ivas->nchan_transport, ( ivas_total_brate / st_ivas->nchan_transport ) * CPE_CHANNELS, MC_MODE_NONE ) ) != IVAS_ERR_OK )
262 : {
263 0 : return error;
264 : }
265 : }
266 :
267 2125508 : return error;
268 : }
|