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 "options.h"
34 : #include <stdlib.h>
35 : #include "ivas_cnst.h"
36 : #include "ivas_prot.h"
37 : #include "prot.h"
38 : #include "ivas_prot_rend.h"
39 : #include "ivas_rom_com.h"
40 : #ifdef DEBUGGING
41 : #include "debug.h"
42 : #endif
43 : #include "wmc_auto.h"
44 :
45 :
46 : /*-------------------------------------------------------------------*
47 : * ivas_osba_data_open()
48 : *
49 : * Allocate and initialize SBA_ISM rendering handle
50 : *-------------------------------------------------------------------*/
51 :
52 14921 : ivas_error ivas_osba_data_open(
53 : Decoder_Struct *st_ivas /* i/o: IVAS decoder handle */
54 : )
55 : {
56 : SBA_ISM_DATA_HANDLE hSbaIsmData;
57 : int16_t i;
58 :
59 14921 : if ( ( hSbaIsmData = (SBA_ISM_DATA_HANDLE) malloc( sizeof( SBA_ISM_DATA ) ) ) == NULL )
60 : {
61 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for OSBA data\n" ) );
62 : }
63 :
64 14921 : hSbaIsmData->delayBuffer_nchan = st_ivas->nchan_ism;
65 14921 : hSbaIsmData->delayBuffer_size = (int16_t) ( ( st_ivas->hDecoderConfig->output_Fs / 50 ) / MAX_PARAM_SPATIAL_SUBFRAMES );
66 :
67 14921 : if ( ( hSbaIsmData->delayBuffer = (float **) malloc( hSbaIsmData->delayBuffer_nchan * sizeof( float * ) ) ) == NULL )
68 : {
69 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for OSBA delay buffer \n" ) );
70 : }
71 :
72 51848 : for ( i = 0; i < hSbaIsmData->delayBuffer_nchan; i++ )
73 : {
74 36927 : if ( ( hSbaIsmData->delayBuffer[i] = (float *) malloc( hSbaIsmData->delayBuffer_size * sizeof( float ) ) ) == NULL )
75 : {
76 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Cannot allocate memory for OSBA delay buffer \n" ) );
77 : }
78 36927 : set_zero( hSbaIsmData->delayBuffer[i], hSbaIsmData->delayBuffer_size );
79 : }
80 :
81 14921 : st_ivas->hSbaIsmData = hSbaIsmData;
82 :
83 14921 : return IVAS_ERR_OK;
84 : }
85 :
86 :
87 : /*-------------------------------------------------------------------*
88 : * ivas_osba_data_close()
89 : *
90 : * Deallocate SBA_ISM rendering handle
91 : *-------------------------------------------------------------------*/
92 :
93 88901 : void ivas_osba_data_close(
94 : SBA_ISM_DATA_HANDLE *hSbaIsmData /* i/o: OSBA rendering handle */
95 : )
96 : {
97 : int16_t i;
98 :
99 88901 : if ( hSbaIsmData == NULL || *hSbaIsmData == NULL )
100 : {
101 73980 : return;
102 : }
103 :
104 14921 : if ( ( *hSbaIsmData )->delayBuffer != NULL )
105 : {
106 51848 : for ( i = 0; i < ( *hSbaIsmData )->delayBuffer_nchan; i++ )
107 : {
108 36927 : free( ( *hSbaIsmData )->delayBuffer[i] );
109 : }
110 14921 : free( ( *hSbaIsmData )->delayBuffer );
111 14921 : ( *hSbaIsmData )->delayBuffer = NULL;
112 : }
113 :
114 14921 : free( *hSbaIsmData );
115 14921 : *hSbaIsmData = NULL;
116 :
117 14921 : return;
118 : }
119 :
120 :
121 : /*--------------------------------------------------------------------------*
122 : * ivas_osba_dirac_td_binaural_jbm()
123 : *
124 : * Binaural rendering in JBM OSBA format
125 : *--------------------------------------------------------------------------*/
126 :
127 245329 : ivas_error ivas_osba_dirac_td_binaural_jbm(
128 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
129 : const uint16_t nSamplesAsked, /* i : number of CLDFB slots requested */
130 : uint16_t *nSamplesRendered, /* o : number of CLDFB slots rendered */
131 : uint16_t *nSamplesAvailable, /* o : number of CLDFB slots still to render */
132 : float *output_f[] /* o : rendered time signal */
133 : )
134 : {
135 : int16_t n;
136 : ivas_error error;
137 : float output_separated_objects[BINAURAL_CHANNELS][L_FRAME48k];
138 : float *p_sepobj[BINAURAL_CHANNELS];
139 : int16_t channel_offset;
140 : float *re, *im;
141 :
142 735987 : for ( n = 0; n < BINAURAL_CHANNELS; n++ )
143 : {
144 490658 : p_sepobj[n] = &output_separated_objects[n][0];
145 : }
146 :
147 245329 : channel_offset = st_ivas->nchan_ism;
148 :
149 245329 : if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAsked, nSamplesRendered, nSamplesAvailable, &output_f[channel_offset] ) ) != IVAS_ERR_OK )
150 : {
151 0 : return error;
152 : }
153 :
154 245329 : ivas_combined_orientation_set_to_start_index( st_ivas->hCombinedOrientationData );
155 :
156 245329 : if ( st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_CODED || st_ivas->hDecoderConfig->output_config == IVAS_AUDIO_CONFIG_BINAURAL_SPLIT_PCM )
157 49396 : {
158 : int16_t slot_idx, num_cldfb_bands, nchan_transport_orig;
159 : int16_t cldfb_slots;
160 : float Cldfb_RealBuffer[CLDFB_NO_CHANNELS_MAX];
161 : float Cldfb_ImagBuffer[CLDFB_NO_CHANNELS_MAX];
162 :
163 49396 : num_cldfb_bands = st_ivas->hSplitBinRend->splitrend.hCldfbHandles->cldfbAna[0]->no_channels;
164 49396 : nchan_transport_orig = st_ivas->nchan_transport;
165 49396 : st_ivas->nchan_transport = st_ivas->nchan_ism;
166 :
167 49396 : if ( ( error = ivas_td_binaural_renderer_sf_splitBinaural( st_ivas, output_f, *nSamplesRendered ) ) != IVAS_ERR_OK )
168 : {
169 0 : return error;
170 : }
171 :
172 49396 : st_ivas->nchan_transport = nchan_transport_orig;
173 49396 : cldfb_slots = *nSamplesRendered / num_cldfb_bands;
174 :
175 454140 : for ( n = 0; n < st_ivas->hSplitBinRend->splitrend.multiBinPoseData.num_poses * BINAURAL_CHANNELS; ++n )
176 : {
177 6243088 : for ( slot_idx = 0; slot_idx < cldfb_slots; slot_idx++ )
178 : {
179 5838344 : cldfbAnalysis_ts( &( output_f[n][num_cldfb_bands * slot_idx] ), Cldfb_RealBuffer, Cldfb_ImagBuffer, num_cldfb_bands, st_ivas->hSplitBinRend->splitrend.hCldfbHandles->cldfbAna[n] );
180 :
181 5838344 : ivas_CLDFB_RINGBUF_GetByIdx( st_ivas->hSplitBinRend->hMultiBinCldfbData[n], &re, &im, slot_idx - cldfb_slots );
182 :
183 5838344 : v_add( re, Cldfb_RealBuffer, re, num_cldfb_bands );
184 5838344 : v_add( im, Cldfb_ImagBuffer, im, num_cldfb_bands );
185 : }
186 : }
187 : }
188 : else
189 : {
190 195933 : if ( ( error = ivas_td_binaural_renderer_sf( st_ivas, p_sepobj, *nSamplesRendered ) ) != IVAS_ERR_OK )
191 : {
192 0 : return error;
193 : }
194 :
195 587799 : for ( n = 0; n < BINAURAL_CHANNELS; n++ )
196 : {
197 : int16_t i;
198 219026586 : for ( i = 0; i < nSamplesAsked; i++ )
199 : {
200 218634720 : output_f[n][i] = output_f[channel_offset + n][i] + p_sepobj[n][i];
201 : }
202 : }
203 : }
204 :
205 245329 : return IVAS_ERR_OK;
206 : }
207 :
208 :
209 : /*-------------------------------------------------------------------------*
210 : * ivas_osba_ism_metadata_dec()
211 : *
212 : * ISM metadata decoding in OSBA format.
213 : *-------------------------------------------------------------------------*/
214 :
215 764386 : ivas_error ivas_osba_ism_metadata_dec(
216 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder structure */
217 : const int32_t ism_total_brate, /* i : ISM total bitrate */
218 : int16_t *nchan_ism, /* o : number of ISM separated channels */
219 : int16_t nb_bits_metadata[] /* o : number of ISM metadata bits */
220 : )
221 : {
222 : ivas_error error;
223 : int16_t nchan_transport_ism;
224 :
225 : /* set ISM parameters */
226 764386 : nchan_transport_ism = st_ivas->nchan_ism;
227 764386 : *nchan_ism = st_ivas->nchan_ism;
228 :
229 : /* decode ISM metadata */
230 764386 : if ( ( error = ivas_ism_metadata_dec( ism_total_brate, *nchan_ism, &nchan_transport_ism, st_ivas->hIsmMetaData, NULL, st_ivas->bfi,
231 764386 : nb_bits_metadata, st_ivas->ism_mode, st_ivas->hISMDTX, NULL, &st_ivas->ism_extmeta_active, &st_ivas->ism_extmeta_cnt, st_ivas->hCPE[0]->hCoreCoder[0] ) ) != IVAS_ERR_OK )
232 : {
233 0 : return error;
234 : }
235 :
236 764386 : return IVAS_ERR_OK;
237 : }
238 :
239 : /*-------------------------------------------------------------------------*
240 : * ivas_osba_render_sf()
241 : *
242 : * Object + SBA rendering process.
243 : *-------------------------------------------------------------------------*/
244 :
245 456244 : ivas_error ivas_osba_render_sf(
246 : Decoder_Struct *st_ivas, /* i/o: IVAS decoder handle */
247 : const uint16_t nSamplesAsked, /* i : number of CLDFB slots requested */
248 : uint16_t *nSamplesRendered, /* o : number of CLDFB slots rendered */
249 : uint16_t *nSamplesAvailableNext, /* o : number of CLDFB slots still to render */
250 : float *p_output[] /* o : rendered time signal */
251 : )
252 : {
253 : int16_t n;
254 : float output_sba[MAX_OUTPUT_CHANNELS][L_FRAME48k];
255 : float *p_output_sba[MAX_OUTPUT_CHANNELS];
256 : ivas_error error;
257 :
258 7756148 : for ( n = 0; n < MAX_OUTPUT_CHANNELS; n++ )
259 : {
260 7299904 : p_output_sba[n] = output_sba[n];
261 : }
262 :
263 456244 : if ( ( error = ivas_sba_dec_render( st_ivas, nSamplesAsked, nSamplesRendered, nSamplesAvailableNext, p_output_sba ) ) != IVAS_ERR_OK )
264 : {
265 0 : return error;
266 : }
267 :
268 456244 : if ( st_ivas->renderer_type != RENDERER_BINAURAL_FASTCONV_ROOM )
269 : {
270 365370 : ivas_ism_render_sf( st_ivas, st_ivas->renderer_type, p_output, *nSamplesRendered );
271 : }
272 :
273 3981846 : for ( n = 0; n < st_ivas->hDecoderConfig->nchan_out; n++ )
274 : {
275 3525602 : if ( st_ivas->renderer_type != RENDERER_BINAURAL_FASTCONV_ROOM )
276 : {
277 3343854 : v_add( p_output[n], p_output_sba[n], p_output[n], *nSamplesRendered );
278 : }
279 : else
280 : {
281 181748 : mvr2r( p_output_sba[n], p_output[n], *nSamplesRendered );
282 : }
283 : }
284 :
285 456244 : return IVAS_ERR_OK;
286 : }
287 :
288 :
289 : /*-------------------------------------------------------------------------*
290 : * ivas_osba_stereo_add_channels()
291 : *
292 : *
293 : *-------------------------------------------------------------------------*/
294 :
295 51614 : void ivas_osba_stereo_add_channels(
296 : float *tc_f[], /* i : transport channels */
297 : float *output_f[], /* i/o: output channels */
298 : const float gain, /* i : gain bed value */
299 : const int16_t nchan_out, /* i : number of output channels */
300 : const int16_t nchan_ism, /* i : number of ISM channels */
301 : const int16_t n_samples_to_render /* i : output frame length per channel */
302 : )
303 : {
304 : int16_t n;
305 :
306 51614 : if ( gain != 1.0f && gain >= 0.0f )
307 0 : {
308 : int16_t i;
309 0 : for ( n = 0; n < nchan_out; n++ )
310 : {
311 0 : for ( i = 0; i < n_samples_to_render; i++ )
312 : {
313 0 : output_f[n][i] += tc_f[n + nchan_ism][i] * gain;
314 : }
315 : }
316 : }
317 : else
318 : {
319 154842 : for ( n = 0; n < nchan_out; n++ )
320 : {
321 103228 : v_add( output_f[n], tc_f[n + nchan_ism], output_f[n], n_samples_to_render );
322 : }
323 : }
324 :
325 :
326 51614 : return;
327 : }
|