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 <stdint.h>
34 : #include "options.h"
35 : #include <math.h>
36 : #include <assert.h>
37 : #include "prot.h"
38 : #include "ivas_prot.h"
39 : #include "ivas_prot_rend.h"
40 : #include "ivas_rom_rend.h"
41 : #ifdef DEBUGGING
42 : #include "debug.h"
43 : #endif
44 : #include "wmc_auto.h"
45 :
46 :
47 : #ifdef DEBUG_MODE_INFO_ALLRAD
48 : /*-------------------------------------------------------------------------*
49 : * debugMatrix_ivas()
50 : *
51 : * Write a column-major (IVAS-style) flattened matrix to
52 : * a 2D array in a CSV file
53 : *-------------------------------------------------------------------------*/
54 : static void debugMatrix_ivas( float *mtx, int16_t rows, int16_t cols, char *filename )
55 : {
56 : FILE *fid;
57 : int16_t i, j;
58 :
59 : fid = fopen( filename, "w" );
60 :
61 : if ( fid == NULL )
62 : {
63 : IVAS_ERROR( IVAS_ERR_FAILED_FILE_WRITE, "Error writing file %s\n", filename );
64 : return;
65 : }
66 :
67 : for ( i = 0; i < rows; i++ )
68 : {
69 : for ( j = 0; j < cols - 1; j++ )
70 : {
71 : fprintf( fid, "%.16f, ", mtx[i * cols + j] );
72 : }
73 : fprintf( fid, "%.16f\n", mtx[i * cols + j] );
74 : }
75 : fclose( fid );
76 : }
77 : #endif
78 :
79 : /*-----------------------------------------------------------------------*
80 : * Global function definitions
81 : *-----------------------------------------------------------------------*/
82 :
83 : /*-------------------------------------------------------------------------*
84 : * ivas_sba_get_hoa_dec_matrix()
85 : *
86 : * Computes the ALLRAD decoder matrix for HOA to loudspeakers
87 : *-------------------------------------------------------------------------*/
88 :
89 389 : ivas_error ivas_sba_get_hoa_dec_matrix(
90 : const IVAS_OUTPUT_SETUP hOutSetup, /* i : target output setup */
91 : float **hoa_dec_mtx, /* o : ALLRAD decoder matrix */
92 : const int16_t ambisonics_order /* i : Ambisonics order */
93 : )
94 : {
95 : int16_t i, j, k;
96 : int16_t num_harm, num_td, num_spk;
97 : const float *t_design_azi, *t_design_ele;
98 : float tmp_val;
99 : float G_td[MAX_OUTPUT_CHANNELS];
100 : float Y_td[SBA_NHARM_HOA3];
101 : float *p_dec_mtx;
102 : EFAP_HANDLE hEFAP;
103 : ivas_error error;
104 :
105 389 : error = IVAS_ERR_OK;
106 :
107 : /* Allocate memory */
108 389 : assert( *hoa_dec_mtx == NULL && "hoa_dec_mtx != NULL" );
109 389 : if ( ( *hoa_dec_mtx = (float *) malloc( SBA_NHARM_HOA3 * ( hOutSetup.nchan_out_woLFE ) * sizeof( float ) ) ) == NULL )
110 : {
111 0 : return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "ALLRAD: Cannot allocate memory!" ) );
112 : }
113 :
114 : /* Initialization by zeroing all SH coeff up to 3rd order (IVAS convention) */
115 389 : num_spk = hOutSetup.nchan_out_woLFE;
116 389 : p_dec_mtx = *hoa_dec_mtx;
117 :
118 2355 : for ( i = 0; i < num_spk; i++ )
119 : {
120 33422 : for ( j = 0; j < SBA_NHARM_HOA3; j++ )
121 : {
122 31456 : *( p_dec_mtx++ ) = 0.f;
123 : }
124 : }
125 :
126 389 : if ( hOutSetup.output_config == IVAS_AUDIO_CONFIG_MONO )
127 : {
128 185 : ( *hoa_dec_mtx )[0] = 1.f;
129 : }
130 204 : else if ( hOutSetup.output_config == IVAS_AUDIO_CONFIG_STEREO )
131 : {
132 16 : ( *hoa_dec_mtx )[0] = 0.5f;
133 16 : ( *hoa_dec_mtx )[1] = 0.5f;
134 16 : ( *hoa_dec_mtx )[SBA_NHARM_HOA3] = 0.5f;
135 16 : ( *hoa_dec_mtx )[SBA_NHARM_HOA3 + 1] = -0.5f;
136 : }
137 188 : else if ( hOutSetup.is_loudspeaker_setup )
138 : {
139 : /* init EFIP */
140 188 : if ( ( error = efap_init_data( &( hEFAP ), hOutSetup.ls_azimuth, hOutSetup.ls_elevation, num_spk, EFAP_MODE_EFIP ) ) != IVAS_ERR_OK )
141 : {
142 0 : return error;
143 : }
144 :
145 188 : num_harm = ivas_sba_get_nchan( ambisonics_order, 0 );
146 :
147 : /* Get t-design values */
148 188 : num_td = SBA_T_DESIGN_11_SIZE;
149 188 : t_design_azi = t_design_11_azimuth;
150 188 : t_design_ele = t_design_11_elevation;
151 :
152 : /* dec_mtx = ( 1 / num_td ) * (G_td * Y_td') * diag(norm_sn3d) */
153 13348 : for ( i = 0; i < num_td; i++ )
154 : {
155 : /* spherical harmonics response for t-design, corresponding to ambisonic order */
156 13160 : ivas_dirac_dec_get_response( (int16_t) t_design_azi[i], (int16_t) t_design_ele[i], Y_td, ambisonics_order );
157 161700 : for ( j = 0; j < num_harm; j++ )
158 : {
159 148540 : Y_td[j] *= norm_sn3d_hoa3[j];
160 : }
161 :
162 : /* t-design to real LS panning gains */
163 13160 : efap_determine_gains( hEFAP, G_td, t_design_azi[i], t_design_ele[i], EFAP_MODE_EFIP );
164 :
165 13160 : p_dec_mtx = *hoa_dec_mtx;
166 135590 : for ( j = 0; j < num_spk; j++ )
167 : {
168 1529990 : for ( k = 0; k < num_harm; k++ )
169 : {
170 1407560 : *( p_dec_mtx++ ) += G_td[j] * Y_td[k];
171 : }
172 122430 : p_dec_mtx += ( SBA_NHARM_HOA3 - num_harm );
173 : }
174 : }
175 :
176 188 : tmp_val = 1.f / num_td;
177 188 : p_dec_mtx = *hoa_dec_mtx;
178 1937 : for ( i = 0; i < num_spk; i++ )
179 : {
180 21857 : for ( j = 0; j < num_harm; j++ )
181 : {
182 20108 : *( p_dec_mtx++ ) *= tmp_val * norm_sn3d_hoa3[j];
183 : }
184 1749 : p_dec_mtx += ( SBA_NHARM_HOA3 - num_harm );
185 : }
186 :
187 : /* free EFAP handle */
188 188 : efap_free_data( &hEFAP );
189 : }
190 : else
191 : {
192 0 : assert( 0 && "ALLRAD: output not supported!!!" );
193 : }
194 :
195 : #ifdef DEBUG_MODE_INFO_ALLRAD
196 : {
197 : char filename[50];
198 : sprintf( filename, "./res/mtx_hoa%d_decoder_allradC.csv", ambisonics_order );
199 : debugMatrix_ivas( *hoa_dec_mtx, num_spk, SBA_NHARM_HOA3, filename );
200 : }
201 : #endif
202 :
203 389 : return error;
204 : }
|