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 "options.h"
36 : #include <math.h>
37 : #include "ivas_cnst.h"
38 : #include "prot.h"
39 : #include "ivas_prot.h"
40 : #include "ivas_rom_com.h"
41 : #include "ivas_stat_enc.h"
42 : #include "wmc_auto.h"
43 : #include "prot.h"
44 : #include "basop_settings.h"
45 : #if defined( DEBUGGING ) && defined( DBG_BITSTREAM_ANALYSIS )
46 : #include <string.h>
47 : #endif
48 :
49 :
50 : /*-----------------------------------------------------------------------*
51 : * Local function prototypes
52 : *-----------------------------------------------------------------------*/
53 :
54 : #ifdef DEBUG_MODE_QMETADATA
55 : static float direction_distance( float elevation[DIRAC_MAX_NBANDS][MAX_PARAM_SPATIAL_SUBFRAMES], float azimuth[DIRAC_MAX_NBANDS][MAX_PARAM_SPATIAL_SUBFRAMES], IVAS_QDIRECTION *q_direction, const int16_t dim1, const int16_t dim2, float mat_dist[DIRAC_MAX_NBANDS][MAX_PARAM_SPATIAL_SUBFRAMES] );
56 :
57 : #endif
58 : static void ivas_qmetadata_quantize_diffuseness_nrg_ratios( IVAS_QMETADATA_HANDLE hQMetaData, int16_t *needed_bits, int16_t *nbits_diff, int16_t *dfRatioBits, const int16_t hodirac_flag );
59 :
60 : static int16_t ivas_qmetadata_entropy_encode_diffuseness( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, uint16_t *diffuseness_index_max_ec_frame );
61 :
62 : static void ivas_qmetadata_reorder_2dir_bands( IVAS_QMETADATA_HANDLE hQMetaData );
63 :
64 : static int16_t ivas_qmetadata_entropy_encode_df_ratio( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, int16_t *df_ratio_bits );
65 :
66 : static int16_t ivas_qmetadata_entropy_encode_dir( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, const uint16_t diffuseness_index_max_ec_frame, const int16_t nbands, const int16_t start_band, const int16_t direction_bits_raw, int16_t max_bits, const int16_t hrmasa_flag );
67 :
68 : static int16_t ivas_qmetadata_raw_encode_dir( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, const int16_t nbands, const int16_t start_band );
69 :
70 : int16_t ivas_qmetadata_encode_extended_gr_length( const uint16_t value, const uint16_t alphabet_size, const int16_t gr_param );
71 :
72 : static int16_t ivas_qmetadata_get_optimal_gr_param( uint16_t *unsigned_data, const int16_t count, const int16_t gr_param_count, int16_t *opt_gr_size );
73 :
74 : static int16_t ivas_qmetadata_encode_quasi_uniform_length( const uint16_t value, const uint16_t alphabet_size );
75 :
76 : static void ivas_qmetadata_encode_quasi_uniform( BSTR_ENC_HANDLE hMetaData, const uint16_t value, const uint16_t alphabet_size );
77 :
78 : static int16_t ivas_qmetadata_reorder_elevation_index( const int16_t elevation_index, const int16_t avg_elevation_index, const int16_t elevation_alphabet );
79 :
80 : static int16_t ivas_qmetadata_reorder_azimuth_index( const int16_t azimuth_index, const int16_t avg_azimuth_index, const int16_t azimuth_alphabet );
81 :
82 : static ivas_error requantize_direction_EC_3( int16_t *extra_bits, IVAS_QDIRECTION *q_direction, const int16_t coding_subbands, BSTR_ENC_HANDLE hMetaData, float elevation_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES], float azimuth_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES], int16_t *ind_order );
83 :
84 : static void calculate_two_distances( float *el, int16_t *bits, const int16_t total_bits, const int16_t len, float *p_d1, float *p_d2 );
85 :
86 : static void joint_encoding( IVAS_QDIRECTION *q_direction, const int16_t j, const int16_t next_j, const int16_t coding_subbands, int16_t *bits_dir0, const int16_t allowed_bits, BSTR_ENC_HANDLE hMetaData, int16_t *diff );
87 :
88 : static ivas_error write_ec_direction( int16_t *num_bits_written, BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, const int16_t j_idx, const int16_t len, const int16_t GR_ord_elevation, const int16_t GR_ord_azimuth, const int16_t use_context, const int16_t same );
89 :
90 : static int16_t write_fixed_rate_direction( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, const int16_t j_idx, const int16_t len );
91 :
92 : static int16_t ivas_qmetadata_quantize_coherence( IVAS_QMETADATA *hQMetaData, const int16_t idx_d, const int16_t all_coherence_zero, BSTR_ENC_HANDLE hMetaData, const int16_t write_flag, int16_t *indice_coherence, const int16_t hrmasa_flag );
93 :
94 : static void dct4_transform( uint8_t *v, float *dct_v );
95 :
96 : static float quantize_DCT_0_coh( const float x, const int16_t j, const float *coherence_cb, const float delta_var, const int16_t no_cb, IVAS_QDIRECTION *q_direction, uint16_t *idx_x, int16_t *p_no_cb, const int16_t hrmasa_flag );
97 :
98 : static int16_t encode_coherence_indexesDCT0( uint16_t *idx_dct, const int16_t len, int16_t *no_cb_vec, BSTR_ENC_HANDLE hMetaData, const int16_t indice_coherence, const int16_t nbits, const int16_t nbits1 );
99 :
100 : static int16_t encode_coherence_indexesDCT1( uint16_t *idx_dct, const int16_t len, BSTR_ENC_HANDLE hMetaData );
101 :
102 : static uint64_t create_combined_index( uint16_t *idx_dct, const int16_t len, const int16_t *no_cb_vec );
103 :
104 : static int16_t encode_surround_coherence( IVAS_QMETADATA *hQMetaData, BSTR_ENC_HANDLE hMetaData );
105 :
106 : static int16_t write_fixed_rate_direction( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, const int16_t j_idx, const int16_t len );
107 :
108 : static int16_t common_direction( IVAS_QDIRECTION *q_direction, const int16_t band_idx, const int16_t len, const int16_t bits_allowed, BSTR_ENC_HANDLE hMetaData, float *elevation_orig, float *azimuth_orig );
109 :
110 : static int16_t ivas_diffuseness_huff_ec_encode( BSTR_ENC_HANDLE hMetaData, const uint16_t idx );
111 :
112 : static void ivas_diffuseness_huff_ec_prepare( IVAS_QDIRECTION *q_direction, int16_t *best_av, uint16_t *avr_idx, int16_t *diffuseness_bits_huff );
113 :
114 : static int16_t coherence_coding_length( const uint16_t *idx_sur_coh_shift, const uint8_t idx_shift_len, const int16_t coding_subbands, const int16_t *no_cv, uint16_t *mr_idx, int16_t *no_cv_shift, int16_t *p_min_idx, int16_t *GR_ord, int16_t *nbits_fr, int16_t *nbits_fr1 );
115 :
116 : static int16_t write_2dir_info( BSTR_ENC_HANDLE hMetaData, uint8_t *twoDirBands, const int16_t n, const int16_t k );
117 :
118 : static void transform_azimuth_dir2( IVAS_QMETADATA_HANDLE hQMetaData, int16_t *dir2_bands );
119 :
120 : static int16_t calc_var_azi( const IVAS_QDIRECTION *q_direction, const int16_t diffuseness_index_max_ec_frame, const float avg_azimuth, float *avg_azimuth_out );
121 :
122 : static void ivas_qmetadata_quantize_diffuseness_nrg_ratios_hr_512( IVAS_QMETADATA_HANDLE hQMetaData, int16_t *needed_bits, const int16_t bits_dir_hr, BSTR_ENC_HANDLE hMetaData );
123 :
124 : static int16_t encode_surround_coherence_hr( IVAS_QMETADATA *hQMetaData, BSTR_ENC_HANDLE hMetaData );
125 :
126 : static int16_t ivas_qmetadata_quantize_coherence_hr_512( IVAS_QMETADATA *hQMetaData, const int16_t idx_d, const int16_t all_coherence_zero, BSTR_ENC_HANDLE hMetaData, const int16_t bits_coh );
127 :
128 : static int16_t write_stream_dct_coeffs_omasa( int16_t *q_idx, const int16_t len_stream, BSTR_ENC_HANDLE hMetaData, const int16_t first_line, const int16_t low_bitrate_mode );
129 :
130 : static int16_t find_optimal_GR_order( const int16_t *q_idx, const int16_t len, int16_t *GR );
131 :
132 : static int16_t find_optimal_GR_orders( const int16_t *q_idx, const int16_t len, const int16_t len_max_GR1, int16_t *GR1, int16_t *GR2, int16_t *i_min );
133 :
134 :
135 : /*-----------------------------------------------------------------------*
136 : * ivas_qmetadata_enc_encode()
137 : *
138 : * Main function for quantizing and coding Spatial Metadata
139 : *-----------------------------------------------------------------------*/
140 :
141 204703 : ivas_error ivas_qmetadata_enc_encode(
142 : BSTR_ENC_HANDLE hMetaData, /* i/o: metadata bitstream handle */
143 : IVAS_QMETADATA *hQMetaData, /* i/o: metadata handle */
144 : const int16_t hodirac_flag /* i : flag to indicate HO-DirAC mode */
145 : )
146 : {
147 : int16_t i, bit_pos_start, bit_pos_start_coh;
148 : int16_t next_ind_start;
149 : uint16_t diffuseness_index_max_ec_frame;
150 : uint16_t diffuseness_index_max_ec_frame_pre[QMETADATA_MAX_NO_DIRECTIONS];
151 : int16_t bits_dir_raw_pre[QMETADATA_MAX_NO_DIRECTIONS];
152 : int16_t bits_diff_sum;
153 : int16_t bits_diff[QMETADATA_MAX_NO_DIRECTIONS], bits_coherence[QMETADATA_MAX_NO_DIRECTIONS];
154 : int16_t bits_dir[QMETADATA_MAX_NO_DIRECTIONS], bits_dir_raw;
155 : int16_t extra_bits;
156 : IVAS_QDIRECTION *q_direction;
157 : int16_t nbands, nblocks, start_band;
158 : int16_t ndirections, d;
159 : float azimuth_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES], elevation_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
160 : int16_t all_coherence_zero;
161 : int16_t bit_pos_0, total_bits_1dir, bits_no_dirs_coh;
162 : int16_t bits_signaling[QMETADATA_MAX_NO_DIRECTIONS];
163 : int16_t indice_coherence;
164 : int16_t bits_dir_bands[MASA_MAXIMUM_CODING_SUBBANDS], raw_flag[MASA_MAXIMUM_CODING_SUBBANDS];
165 : int16_t diff_bits, bits_ec, next_ind_raw_flag;
166 : int16_t dfRatio_bits[MASA_MAXIMUM_CODING_SUBBANDS];
167 : int16_t bits_surround_coh, no_TF;
168 : int16_t dir2_bands[MASA_MAXIMUM_TWO_DIR_BANDS];
169 : int16_t ind_order[MASA_MAXIMUM_CODING_SUBBANDS];
170 : int16_t reduce_bits;
171 : ivas_error error;
172 :
173 204703 : error = IVAS_ERR_OK;
174 :
175 : #ifdef DEBUG_MODE_QMETADATA
176 : int16_t ec_flag = 0;
177 : int16_t tmp;
178 :
179 : static FILE *pF = NULL;
180 : static FILE *pF_azi = NULL;
181 : static FILE *pF_ele = NULL;
182 : static FILE *pF_ratio = NULL;
183 : static FILE *pF_spcoh = NULL;
184 : static FILE *pF_spcoh_orig = NULL;
185 : static FILE *pF_surcoh = NULL;
186 :
187 : if ( pF == NULL )
188 : pF = fopen( "./res/qmetadata_enc.txt", "w" );
189 : if ( pF_azi == NULL )
190 : pF_azi = fopen( "./res/qmetadata_azi_enc.txt", "w" );
191 : if ( pF_ele == NULL )
192 : pF_ele = fopen( "./res/qmetadata_ele_enc.txt", "w" );
193 : if ( pF_ratio == NULL )
194 : pF_ratio = fopen( "./res/qmetadata_ratio_enc.txt", "w" );
195 : if ( pF_spcoh == NULL )
196 : pF_spcoh = fopen( "./res/qmetadata_spcoh_enc.txt", "w" );
197 : if ( pF_spcoh_orig == NULL )
198 : pF_spcoh_orig = fopen( "./res/qmetadata_spcoh_orig.txt", "w" );
199 : if ( pF_surcoh == NULL )
200 : pF_surcoh = fopen( "./res/qmetadata_surcoh_enc.txt", "w" );
201 : #endif
202 :
203 :
204 : /* Save initial position in bitstream */
205 204703 : bit_pos_0 = hMetaData->nb_bits_tot;
206 204703 : bit_pos_start = bit_pos_0;
207 :
208 204703 : ndirections = hQMetaData->no_directions;
209 204703 : extra_bits = 0;
210 :
211 : /* Check if coherence should be encoded */
212 204703 : all_coherence_zero = 1;
213 204703 : bits_no_dirs_coh = 0;
214 :
215 204703 : if ( hQMetaData->coherence_flag )
216 : {
217 28076 : all_coherence_zero = hQMetaData->all_coherence_zero;
218 :
219 28076 : push_next_indice( hMetaData, all_coherence_zero, 1 ); /* signal coherence */
220 28076 : bits_no_dirs_coh += 1;
221 : }
222 :
223 204703 : if ( ndirections > 1 )
224 : {
225 : #ifdef DEBUGGING
226 : assert( ndirections == 2 );
227 : #endif
228 : /* Reorder 2dir bands for more efficient encoding. */
229 20820 : if ( !hodirac_flag )
230 : {
231 4580 : ivas_qmetadata_reorder_2dir_bands( hQMetaData );
232 : }
233 20820 : d = 0;
234 239012 : for ( i = hQMetaData->q_direction[1].cfg.start_band; i < hQMetaData->q_direction[1].cfg.nbands; i++ )
235 : {
236 218192 : if ( hQMetaData->twoDirBands[i] == 1 )
237 : {
238 191840 : mvr2r( hQMetaData->q_direction[1].band_data[i].azimuth, hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].cfg.nblocks );
239 191840 : mvr2r( hQMetaData->q_direction[1].band_data[i].elevation, hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].cfg.nblocks );
240 191840 : mvr2r( hQMetaData->q_direction[1].band_data[i].energy_ratio, hQMetaData->q_direction[1].band_data[d].energy_ratio, hQMetaData->q_direction[1].cfg.nblocks );
241 :
242 191840 : dir2_bands[d] = i;
243 :
244 191840 : if ( hQMetaData->coherence_flag )
245 : {
246 13200 : mvc2c( hQMetaData->q_direction[1].coherence_band_data[i].spread_coherence, hQMetaData->q_direction[1].coherence_band_data[d].spread_coherence, hQMetaData->q_direction[1].cfg.nblocks );
247 : }
248 191840 : d++;
249 : }
250 : }
251 :
252 20820 : bits_no_dirs_coh += write_2dir_info( hMetaData, hQMetaData->twoDirBands, hQMetaData->q_direction[0].cfg.nbands, hQMetaData->numTwoDirBands );
253 :
254 47172 : for ( i = d; i < hQMetaData->q_direction[0].cfg.nbands; i++ )
255 : {
256 26352 : set_f( hQMetaData->q_direction[1].band_data[i].energy_ratio, 0.0f, hQMetaData->q_direction[1].cfg.nblocks );
257 : }
258 : #ifdef DEBUGGING
259 : assert( d == hQMetaData->numTwoDirBands );
260 : #endif
261 :
262 20820 : hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands;
263 : }
264 :
265 : /*Quantization of the Diffuseness */
266 204703 : ivas_qmetadata_quantize_diffuseness_nrg_ratios( hQMetaData, bits_dir_raw_pre, bits_diff, dfRatio_bits, hodirac_flag );
267 :
268 204703 : bits_diff_sum = 0;
269 204703 : bits_diff[0] = ivas_qmetadata_entropy_encode_diffuseness( hMetaData, &( hQMetaData->q_direction[0] ), &diffuseness_index_max_ec_frame_pre[0] );
270 204703 : bits_diff_sum += bits_diff[0];
271 :
272 204703 : if ( ndirections == 2 )
273 : {
274 20820 : bits_diff[1] = ivas_qmetadata_entropy_encode_df_ratio( hMetaData, &( hQMetaData->q_direction[1] ), dfRatio_bits );
275 20820 : bits_diff_sum += bits_diff[1];
276 : }
277 :
278 : /* 2dir energy ratio encoding reuses index memory. Now that diffRatio and dFRatio have been encoded,
279 : * we retrieve index_dirRatio1Inv and index_dirRatio1Inv for further parameter encoding. This is
280 : * necessary only for bands that have two concurrent directions. */
281 204703 : if ( hQMetaData->no_directions == 2 )
282 : {
283 : int16_t j, k, dir2band, index_dirRatio1Inv, index_dirRatio2Inv;
284 :
285 20820 : dir2band = 0;
286 239012 : for ( j = hQMetaData->q_direction[0].cfg.start_band; j < hQMetaData->q_direction[0].cfg.nbands; ++j )
287 : {
288 218192 : if ( hQMetaData->twoDirBands[j] == 1 )
289 : {
290 191840 : index_dirRatio1Inv = masa_sq( 1.0f - hQMetaData->q_direction[0].band_data[j].energy_ratio[0], diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
291 191840 : index_dirRatio2Inv = masa_sq( 1.0f - hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[0], diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
292 :
293 950068 : for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ )
294 : {
295 758228 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index[k] = index_dirRatio1Inv;
296 : }
297 :
298 950068 : for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ )
299 : {
300 758228 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[k] = index_dirRatio2Inv;
301 : }
302 :
303 191840 : dir2band++;
304 : }
305 : }
306 : }
307 :
308 : /* Encode surround coherence */
309 204703 : if ( ndirections == 2 )
310 : {
311 20820 : no_TF = hQMetaData->q_direction[0].cfg.nbands * hQMetaData->q_direction[0].cfg.nblocks + hQMetaData->q_direction[1].cfg.nbands * hQMetaData->q_direction[1].cfg.nblocks;
312 20820 : if ( ( all_coherence_zero == 0 ) && ( hQMetaData->metadata_max_bits - bits_no_dirs_coh - 4.3f * no_TF - sum_s( bits_diff, ndirections ) >= MASA_MIN_BITS_SURR_COH ) )
313 : {
314 4580 : bits_surround_coh = encode_surround_coherence( hQMetaData, hMetaData );
315 : }
316 : else
317 : {
318 16240 : bits_surround_coh = 0;
319 194880 : for ( i = 0; i < hQMetaData->q_direction[0].cfg.nbands; i++ )
320 : {
321 178640 : if ( hQMetaData->surcoh_band_data != NULL )
322 : {
323 0 : set_c( (int8_t *) hQMetaData->surcoh_band_data[i].surround_coherence, 0, hQMetaData->q_direction[0].cfg.nblocks );
324 : }
325 : }
326 : }
327 20820 : bits_no_dirs_coh += bits_surround_coh;
328 20820 : total_bits_1dir = ( ( hQMetaData->metadata_max_bits - bits_no_dirs_coh ) * hQMetaData->q_direction[0].cfg.nbands * hQMetaData->q_direction[0].cfg.nblocks ) / no_TF;
329 : }
330 : else
331 : {
332 183883 : no_TF = hQMetaData->q_direction[0].cfg.nbands * hQMetaData->q_direction[0].cfg.nblocks;
333 183883 : if ( ( all_coherence_zero == 0 ) && ( hQMetaData->metadata_max_bits - bits_no_dirs_coh - 4.3f * no_TF - bits_diff[0] >= MASA_MIN_BITS_SURR_COH ) )
334 : {
335 22141 : bits_surround_coh = encode_surround_coherence( hQMetaData, hMetaData );
336 : }
337 : else
338 : {
339 161742 : bits_surround_coh = 0;
340 679612 : for ( i = 0; i < hQMetaData->q_direction[0].cfg.nbands; i++ )
341 : {
342 517870 : if ( hQMetaData->surcoh_band_data != NULL )
343 : {
344 14974 : set_c( (int8_t *) hQMetaData->surcoh_band_data[i].surround_coherence, 0, hQMetaData->q_direction[0].cfg.nblocks );
345 : }
346 : }
347 : }
348 183883 : total_bits_1dir = hQMetaData->metadata_max_bits - bits_no_dirs_coh - bits_surround_coh;
349 : }
350 :
351 : /* Loop over number of directions*/
352 430226 : for ( d = 0; d < ndirections; d++ )
353 : {
354 225523 : q_direction = &( hQMetaData->q_direction[d] );
355 :
356 225523 : if ( d == 1 )
357 : {
358 20820 : transform_azimuth_dir2( hQMetaData, dir2_bands );
359 : }
360 :
361 225523 : nbands = q_direction->cfg.nbands;
362 225523 : nblocks = q_direction->cfg.nblocks;
363 225523 : start_band = q_direction->cfg.start_band;
364 225523 : diffuseness_index_max_ec_frame = diffuseness_index_max_ec_frame_pre[0];
365 225523 : bits_dir_raw = bits_dir_raw_pre[d];
366 :
367 : /* This sets bit budget correctly for the second direction */
368 225523 : if ( d == 0 )
369 : {
370 204703 : bits_diff[d] = bits_diff_sum;
371 : }
372 : else
373 : {
374 20820 : bits_diff[d] = 0;
375 : }
376 :
377 : #ifdef DEBUG_MODE_QMETADATA
378 : {
379 : int16_t j, k;
380 : k = 0;
381 : fprintf( pF_spcoh_orig, "%d %d ", frame, k );
382 :
383 : for ( i = start_band; i < nbands; i++ )
384 : {
385 : for ( j = 0; j < nblocks; j++ )
386 : {
387 : if ( q_direction->coherence_band_data != NULL )
388 : {
389 : fprintf( pF_spcoh_orig, " %d ", q_direction->coherence_band_data[i].spread_coherence[j] );
390 : }
391 : }
392 : }
393 : fprintf( pF_spcoh_orig, "\n" );
394 : }
395 : #endif
396 :
397 225523 : bits_signaling[d] = 0;
398 :
399 : /*Coherence */
400 225523 : bits_coherence[d] = 0;
401 225523 : bit_pos_start_coh = hMetaData->nb_bits_tot;
402 :
403 225523 : if ( all_coherence_zero == 0 )
404 : {
405 31473 : bits_coherence[d] = ivas_qmetadata_quantize_coherence( hQMetaData, d, all_coherence_zero, hMetaData, 0, &indice_coherence, 0 );
406 : }
407 :
408 225523 : if ( q_direction->cfg.mc_ls_setup == MC_LS_SETUP_5_1 || q_direction->cfg.mc_ls_setup == MC_LS_SETUP_7_1 )
409 : {
410 10150 : q_direction->not_in_2D = 0;
411 : /* Quantize directions*/
412 10150 : quantize_direction_frame2D( q_direction, azimuth_orig, elevation_orig );
413 : }
414 : else
415 : {
416 : /* Quantize directions*/
417 215373 : quantize_direction_frame( q_direction, azimuth_orig, elevation_orig, 0 );
418 : }
419 :
420 : /* Signalling 2D*/
421 225523 : push_next_indice( hMetaData, ( q_direction->not_in_2D > 0 ), 1 ); /*2D flag*/
422 225523 : bits_signaling[d] = 1;
423 :
424 : /* Save state of metadata bitstream buffer after writing energy ratios, number of dirs and save space for coherence*/
425 225523 : bit_pos_start = hMetaData->nb_bits_tot;
426 225523 : next_ind_start = hMetaData->nb_ind_tot;
427 :
428 : /* Encode quantized directions with EC frame-wise*/
429 225523 : if ( total_bits_1dir + bits_surround_coh <= hQMetaData->qmetadata_max_bit_req )
430 : {
431 157842 : push_next_indice( hMetaData, 0, 1 ); /*Write 1 bit to signal EC frame-wise (EC1)*/
432 157842 : bits_signaling[d]++;
433 : }
434 :
435 225523 : next_ind_raw_flag = hMetaData->nb_ind_tot;
436 225523 : push_next_indice( hMetaData, 0, 1 ); /* Raw coding flag*/
437 :
438 225523 : bits_dir_bands[0] = ivas_qmetadata_raw_encode_dir( NULL, q_direction, q_direction->cfg.nbands, q_direction->cfg.start_band );
439 :
440 225523 : reduce_bits = hQMetaData->is_masa_ivas_format ? ( total_bits_1dir - ( bits_diff[d] + bits_coherence[d] + bits_signaling[d] ) - 1 ) : MASA_MAX_BITS;
441 225523 : bits_ec = ivas_qmetadata_entropy_encode_dir( hMetaData, q_direction, diffuseness_index_max_ec_frame, q_direction->cfg.nbands, q_direction->cfg.start_band, bits_dir_bands[0], reduce_bits, 0 );
442 :
443 225523 : if ( bits_ec < 0 )
444 : {
445 104069 : hMetaData->ind_list[next_ind_raw_flag].value = 1; /*rewrite flag*/
446 104069 : bits_ec = ivas_qmetadata_raw_encode_dir( hMetaData, q_direction, q_direction->cfg.nbands, q_direction->cfg.start_band );
447 : #ifdef DEBUGGING
448 : assert( bits_dir_bands[0] == bits_ec );
449 : #endif
450 : }
451 225523 : bits_dir[d] = bits_ec + 1;
452 : #ifdef DEBUG_MODE_QMETADATA
453 : tmp = bits_dir[d] - ( total_bits_1dir - ( bits_diff[d] + bits_coherence[d] + bits_signaling[d] ) );
454 : #endif
455 225523 : extra_bits = hQMetaData->metadata_max_bits - ( hMetaData->nb_bits_tot - bit_pos_0 );
456 : #ifdef DEBUGGING
457 : assert( bit_pos_start + bits_signaling[d] - 1 + bits_dir[d] == hMetaData->nb_bits_tot );
458 : #endif
459 : #ifdef DEBUG_MODE_QMETADATA
460 : ec_flag = 0;
461 : #endif
462 :
463 : /* Encode quantized directions with EC band-wise */
464 225523 : if ( ( total_bits_1dir + bits_surround_coh <= hQMetaData->qmetadata_max_bit_req ) && ( bits_dir[d] + bits_diff[d] + bits_coherence[d] + bits_signaling[d] > total_bits_1dir ) && q_direction->cfg.nblocks > 1 )
465 : {
466 7640 : restore_metadata_buffer( hMetaData, next_ind_start, bit_pos_start );
467 :
468 : /* Write signaling */
469 7640 : push_next_indice( hMetaData, 1, 1 ); /*Write 1 bit to signal no EC frame-wise (EC1)*/
470 7640 : push_next_indice( hMetaData, 0, 1 ); /*Write 1 bit to signal EC band-wise (EC2)*/
471 7640 : bits_signaling[d] = 3;
472 :
473 : /* Write raw flags */
474 7640 : next_ind_raw_flag = hMetaData->nb_ind_tot;
475 39705 : for ( i = start_band; i < nbands; i++ )
476 : {
477 32065 : push_next_indice( hMetaData, 0, 1 ); /* Raw coding flag*/
478 : }
479 :
480 7640 : bits_dir[d] = 0;
481 7640 : diff_bits = bits_diff[d] + bits_coherence[d] + bits_signaling[d] - total_bits_1dir;
482 39705 : for ( i = start_band; i < nbands; i++ )
483 : {
484 32065 : bits_dir_bands[i] = ivas_qmetadata_raw_encode_dir( NULL, q_direction, i + 1, i );
485 :
486 : /* Write ec bits */
487 32065 : bits_ec = ivas_qmetadata_entropy_encode_dir( hMetaData, q_direction, diffuseness_index_max_ec_frame, i + 1, i, bits_dir_bands[i], MASA_MAX_BITS, 0 );
488 :
489 32065 : if ( bits_ec >= 0 )
490 : {
491 17734 : bits_dir_bands[i] = bits_ec;
492 17734 : raw_flag[i] = 0;
493 : }
494 : else
495 : {
496 14331 : raw_flag[i] = 1;
497 : }
498 32065 : diff_bits += bits_dir_bands[i] + 1;
499 : }
500 :
501 7640 : small_requantize_direction_frame( q_direction, azimuth_orig, elevation_orig, raw_flag, bits_dir_bands, &diff_bits );
502 :
503 39705 : for ( i = start_band; i < nbands; i++ )
504 : {
505 32065 : if ( raw_flag[i] )
506 : {
507 : /* Rewrite raw flag value */
508 14331 : hMetaData->ind_list[next_ind_raw_flag + i - start_band].value = 1;
509 :
510 : /* Write ec bits */
511 14331 : bits_ec = ivas_qmetadata_raw_encode_dir( hMetaData, q_direction, i + 1, i );
512 : #ifdef DEBUGGING
513 : assert( bits_dir_bands[i] == bits_ec );
514 : #endif
515 : }
516 32065 : bits_dir[d] += bits_dir_bands[i] + 1;
517 : }
518 :
519 7640 : extra_bits = hQMetaData->metadata_max_bits - ( hMetaData->nb_bits_tot - bit_pos_0 );
520 :
521 : #ifdef DEBUGGING
522 : if ( ( diff_bits <= 0 ) && ( bits_dir[d] + bits_diff[d] + bits_coherence[d] + bits_signaling[d] > total_bits_1dir ) )
523 : {
524 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "not possible!!" );
525 : }
526 : #endif
527 :
528 : #ifdef DEBUGGING
529 : assert( bit_pos_start + bits_signaling[d] - 1 + bits_dir[d] == hMetaData->nb_bits_tot );
530 : #endif
531 : #ifdef DEBUG_MODE_QMETADATA
532 : ec_flag = 1;
533 : #endif
534 : }
535 :
536 : /* Requantized directions */
537 225523 : if ( ( total_bits_1dir + bits_surround_coh <= hQMetaData->qmetadata_max_bit_req ) && ( bits_dir[d] + bits_diff[d] + bits_coherence[d] + bits_signaling[d] > total_bits_1dir ) )
538 : {
539 :
540 : /*Bit budget exceeded, bit reduction strategy?*/
541 9076 : extra_bits = 0;
542 :
543 9076 : restore_metadata_buffer( hMetaData, next_ind_start, bit_pos_start );
544 :
545 9076 : push_next_indice( hMetaData, 1, 1 ); /*Write 1 bit to signal no EC frame-wise (EC1)*/
546 9076 : if ( nblocks > 1 )
547 : {
548 5693 : push_next_indice( hMetaData, 1, 1 ); /*Write 1 bit to signal requantization stage (EC3)*/
549 5693 : bits_signaling[d] = 3;
550 : }
551 : else
552 : {
553 3383 : bits_signaling[d] = 2;
554 : }
555 :
556 9076 : if ( hQMetaData->is_masa_ivas_format == 0 )
557 : {
558 3248 : reduce_bits = bits_dir_raw - ( total_bits_1dir - bits_diff[d] - bits_coherence[d] - bits_signaling[d] );
559 3248 : ind_order[0] = -1;
560 : }
561 : else
562 : {
563 5828 : ind_order[0] = 0;
564 5828 : reduce_bits = min( nbands * nblocks + MASA_BIT_REDUCT_PARAM, bits_dir_raw - ( total_bits_1dir - bits_diff[d] - bits_coherence[d] - bits_signaling[d] ) );
565 :
566 5828 : if ( reduce_bits > bits_dir_raw - nbands * nblocks )
567 : {
568 150 : reduce_bits = bits_dir_raw - nbands * nblocks;
569 : }
570 : }
571 :
572 9076 : only_reduce_bits_direction( &extra_bits, q_direction, reduce_bits, nbands, nblocks, ind_order );
573 9076 : bits_dir[d] = hMetaData->nb_bits_tot;
574 9076 : requantize_direction_EC_3( &extra_bits, q_direction, nbands, hMetaData, elevation_orig, azimuth_orig, ind_order );
575 9076 : bits_dir[d] = hMetaData->nb_bits_tot - bits_dir[d];
576 : #ifdef DEBUG_MODE_QMETADATA
577 : ec_flag = 2;
578 : #endif
579 : }
580 :
581 : /* finalize writing coherence */
582 225523 : if ( ( bits_coherence[d] > 0 ) && ( all_coherence_zero == 0 ) && ( nblocks > 1 ) )
583 : {
584 23237 : bit_pos_start = hMetaData->nb_bits_tot;
585 23237 : hMetaData->nb_bits_tot = bit_pos_start_coh;
586 23237 : ivas_qmetadata_quantize_coherence( hQMetaData, d, all_coherence_zero, hMetaData, 1, &indice_coherence, 0 );
587 23237 : hMetaData->nb_bits_tot = bit_pos_start;
588 : }
589 :
590 225523 : if ( d == 0 )
591 : {
592 204703 : total_bits_1dir = hQMetaData->metadata_max_bits - ( hMetaData->nb_bits_tot - bit_pos_0 );
593 : }
594 : #ifdef DEBUG_MODE_QMETADATA
595 : {
596 : int16_t j;
597 : float tmp_f, mat_dist[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
598 :
599 : fprintf( pF, "frame %d: diff %d coh %d surcoh %d ", frame, bits_diff[d], bits_coherence[d], bits_surround_coh );
600 : fprintf( pF, "dir %d %d,%d,%d %d %5.3f\n", ec_flag, hMetaData->nb_bits_tot - bit_pos_0, total_bits_1dir, bits_dir_raw, tmp, direction_distance( elevation_orig, azimuth_orig, q_direction, nbands, nblocks, mat_dist ) );
601 : fprintf( pF_azi, "frame %d/dir/ec %d/%d: ", frame, d, ec_flag );
602 : fprintf( pF_ele, "frame %d/dir/ec %d/%d: ", frame, d, ec_flag );
603 : fprintf( pF_ratio, "frame %d/dir %d: ", frame, d );
604 : /*fprintf( pF_spcoh, "frame %d/dir %d: ", frame, d ); */
605 : fprintf( pF_spcoh, " %d %d ", frame, d );
606 : if ( d == 0 )
607 : {
608 : fprintf( pF_surcoh, "frame %d/dir %d: ", frame, d );
609 : }
610 :
611 : /* direction_distance( elevation_orig, azimuth_orig, q_direction, nbands, nblocks, mat_dist );*/
612 : for ( i = start_band; i < nbands; i++ )
613 : {
614 : for ( j = 0; j < nblocks; j++ )
615 : {
616 : fprintf( pF_azi, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[i].azimuth[j] ) / 100.f );
617 : fprintf( pF_ele, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[i].elevation[j] ) / 100.f );
618 : fprintf( pF_ratio, " %1.3f ", q_direction->band_data[i].energy_ratio[j] );
619 : if ( q_direction->coherence_band_data != NULL )
620 : {
621 : fprintf( pF_spcoh, " %d ", q_direction->coherence_band_data[i].spread_coherence[j] );
622 : }
623 : if ( d == 0 && hQMetaData->surcoh_band_data != NULL )
624 : {
625 : fprintf( pF_surcoh, " %d ", hQMetaData->surcoh_band_data[i].surround_coherence[0] );
626 : }
627 : }
628 : }
629 : fprintf( pF_azi, "\n" );
630 : fprintf( pF_ele, "\n" );
631 : fprintf( pF_ratio, "\n" );
632 : fprintf( pF_spcoh, "\n" );
633 : if ( d == 0 )
634 : {
635 : fprintf( pF_surcoh, "\n" );
636 : }
637 :
638 : for ( i = 0; i < nblocks; i++ )
639 : {
640 : for ( j = 0; j < nbands; j++ )
641 : {
642 : dbgwrite( &( q_direction->band_data[j].azimuth[i] ), sizeof( float ), 1, 1, "./res/IVAS_QMETADATA_azi.bin" );
643 : dbgwrite( &( q_direction->band_data[j].elevation[i] ), sizeof( float ), 1, 1, "./res/IVAS_QMETADATA_ele.bin" );
644 : dbgwrite( &( mat_dist[j][i] ), sizeof( float ), 1, 1, "./res/IVAS_QMETADATA_dist.bin" );
645 : dbgwrite( &( q_direction->band_data[j].energy_ratio_index[i] ), sizeof( uint16_t ), 1, 1, "./res/IVAS_QMETADATA_diffuseness_index.bin" );
646 : tmp_f = 1.f - q_direction->band_data[j].energy_ratio[i];
647 : dbgwrite( &tmp_f, sizeof( float ), 1, 1, "./res/IVAS_QMETADATA_diffuseness.bin" );
648 : }
649 : }
650 :
651 : j = (int16_t) ( hMetaData->nb_bits_tot );
652 : dbgwrite( &j, sizeof( int16_t ), 1, 1, "./res/IVAS_QMETADATA_bits.bin" );
653 : }
654 : #endif
655 :
656 : /* Save quantized DOAs */
657 1309865 : for ( i = start_band; i < nbands; i++ )
658 : {
659 1084342 : mvr2r( q_direction->band_data[i].azimuth, q_direction->band_data[i].q_azimuth, nblocks );
660 1084342 : mvr2r( q_direction->band_data[i].elevation, q_direction->band_data[i].q_elevation, nblocks );
661 : }
662 :
663 : /* Copy original DOAs back to q_direction*/
664 1309865 : for ( i = start_band; i < nbands; i++ )
665 : {
666 1084342 : mvr2r( azimuth_orig[i], q_direction->band_data[i].azimuth, nblocks );
667 1084342 : mvr2r( elevation_orig[i], q_direction->band_data[i].elevation, nblocks );
668 : }
669 : }
670 :
671 204703 : return error;
672 : }
673 :
674 :
675 : /*-----------------------------------------------------------------------*
676 : * ivas_qmetadata_enc_encode_hr_384_512()
677 : *
678 : * Main function for quantizing and coding Spatial Metadata at HRs
679 : *-----------------------------------------------------------------------*/
680 :
681 2276 : ivas_error ivas_qmetadata_enc_encode_hr_384_512(
682 : BSTR_ENC_HANDLE hMetaData, /* i/o: metadata bitstream handle */
683 : IVAS_QMETADATA *hQMetaData, /* i/o: metadata handle */
684 : const int16_t bits_sph_idx,
685 : const int16_t bits_sp_coh )
686 : {
687 : int16_t i, j;
688 : int16_t bits_diff[QMETADATA_MAX_NO_DIRECTIONS];
689 : IVAS_QDIRECTION *q_direction;
690 : int16_t nbands, nblocks, start_band;
691 : int16_t ndirections, d;
692 : int16_t all_coherence_zero;
693 : #ifdef DEBUG_MODE_QMETADATA
694 : int16_t bits_no_dirs_coh;
695 : #endif
696 : int16_t bits_ec;
697 : float azimuth_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES], elevation_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
698 : ivas_error error;
699 :
700 2276 : error = IVAS_ERR_OK;
701 :
702 : #ifdef DEBUG_MODE_QMETADATA
703 :
704 : static FILE *pF = NULL;
705 : static FILE *pF_azi = NULL;
706 : static FILE *pF_ele = NULL;
707 : static FILE *pF_ratio = NULL;
708 : static FILE *pF_spcoh = NULL;
709 : static FILE *pF_spcoh_orig = NULL;
710 : static FILE *pF_surcoh = NULL;
711 :
712 : if ( pF == NULL )
713 : pF = fopen( "./res/qmetadata_enc.txt", "w" );
714 : if ( pF_azi == NULL )
715 : pF_azi = fopen( "./res/qmetadata_azi_enc.txt", "w" );
716 : if ( pF_ele == NULL )
717 : pF_ele = fopen( "./res/qmetadata_ele_enc.txt", "w" );
718 : if ( pF_ratio == NULL )
719 : pF_ratio = fopen( "./res/qmetadata_ratio_enc.txt", "w" );
720 : if ( pF_spcoh == NULL )
721 : pF_spcoh = fopen( "./res/qmetadata_spcoh_enc.txt", "w" );
722 : if ( pF_spcoh_orig == NULL )
723 : pF_spcoh_orig = fopen( "./res/qmetadata_spcoh_orig.txt", "w" );
724 : if ( pF_surcoh == NULL )
725 : pF_surcoh = fopen( "./res/qmetadata_surcoh_enc.txt", "w" );
726 : #endif
727 :
728 2276 : ndirections = hQMetaData->no_directions;
729 :
730 : /* Check if coherence should be encoded */
731 2276 : all_coherence_zero = 1;
732 : #ifdef DEBUG_MODE_QMETADATA
733 : bits_no_dirs_coh = 0;
734 : #endif
735 2276 : if ( hQMetaData->q_direction->cfg.inactiveBands > 0 )
736 : {
737 300 : push_next_indice( hMetaData, 1, 1 );
738 : /* write the number of inactive higher bands */
739 300 : ivas_qmetadata_encode_extended_gr( hMetaData, hQMetaData->q_direction->cfg.inactiveBands - 1, MASA_MAXIMUM_CODING_SUBBANDS, 1 );
740 : }
741 : else
742 : {
743 : /* no change */
744 1976 : push_next_indice( hMetaData, 0, 1 );
745 : }
746 2276 : if ( hQMetaData->coherence_flag )
747 : {
748 2276 : all_coherence_zero = hQMetaData->all_coherence_zero;
749 2276 : push_next_indice( hMetaData, all_coherence_zero, 1 ); /* signal coherence */
750 : #ifdef DEBUG_MODE_QMETADATA
751 : bits_no_dirs_coh += 1;
752 : #endif
753 : }
754 :
755 : /* encode 2 direction subbands position */
756 2276 : if ( ndirections == 2 && bits_sph_idx == 11 )
757 : {
758 : #ifdef DEBUG_MODE_QMETADATA
759 : bits_no_dirs_coh +=
760 : #endif
761 876 : write_2dir_info( hMetaData, hQMetaData->twoDirBands, hQMetaData->q_direction[0].cfg.nbands, hQMetaData->numTwoDirBands );
762 876 : d = 0;
763 21600 : for ( i = hQMetaData->q_direction[1].cfg.start_band; i < hQMetaData->q_direction[1].cfg.nbands; i++ )
764 : {
765 20724 : if ( hQMetaData->twoDirBands[i] == 1 )
766 : {
767 8334 : mvr2r( hQMetaData->q_direction[1].band_data[i].azimuth, hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].cfg.nblocks );
768 8334 : mvr2r( hQMetaData->q_direction[1].band_data[i].elevation, hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].cfg.nblocks );
769 8334 : mvr2r( hQMetaData->q_direction[1].band_data[i].energy_ratio, hQMetaData->q_direction[1].band_data[d].energy_ratio, hQMetaData->q_direction[1].cfg.nblocks );
770 :
771 8334 : if ( hQMetaData->coherence_flag )
772 : {
773 8334 : mvc2c( hQMetaData->q_direction[1].coherence_band_data[i].spread_coherence, hQMetaData->q_direction[1].coherence_band_data[d].spread_coherence, hQMetaData->q_direction[1].cfg.nblocks );
774 : }
775 8334 : d++;
776 : }
777 : }
778 13266 : for ( i = hQMetaData->numTwoDirBands; i < hQMetaData->q_direction[0].cfg.nbands; i++ )
779 : {
780 12390 : set_f( hQMetaData->q_direction[1].band_data[i].energy_ratio, 0.0f, hQMetaData->q_direction[1].cfg.nblocks );
781 : }
782 :
783 876 : hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands;
784 : }
785 :
786 : /*Quantization and encoding of the Diffuseness */
787 2276 : ivas_qmetadata_quantize_diffuseness_nrg_ratios_hr_512( hQMetaData, bits_diff, bits_sph_idx, hMetaData );
788 :
789 : /* Encode surround coherence */
790 2276 : if ( all_coherence_zero == 0 )
791 : {
792 2276 : encode_surround_coherence_hr( hQMetaData, hMetaData );
793 : }
794 : else
795 : {
796 0 : for ( i = 0; i < hQMetaData->q_direction[0].cfg.nbands; i++ )
797 : {
798 0 : if ( hQMetaData->surcoh_band_data != NULL )
799 : {
800 0 : set_c( (int8_t *) hQMetaData->surcoh_band_data[i].surround_coherence, 0, hQMetaData->q_direction[0].cfg.nblocks );
801 : }
802 : }
803 : }
804 :
805 : /* Loop over number of directions*/
806 5808 : for ( d = 0; d < ndirections; d++ )
807 : {
808 3532 : q_direction = &( hQMetaData->q_direction[d] );
809 :
810 3532 : nbands = q_direction->cfg.nbands;
811 3532 : nblocks = q_direction->cfg.nblocks;
812 3532 : start_band = q_direction->cfg.start_band;
813 :
814 : #ifdef DEBUG_MODE_QMETADATA
815 : {
816 : int16_t k;
817 : k = 0;
818 : fprintf( pF_spcoh_orig, "%d %d ", frame, k );
819 :
820 : for ( i = start_band; i < nbands; i++ )
821 : {
822 : for ( j = 0; j < nblocks; j++ )
823 : {
824 : if ( q_direction->coherence_band_data != NULL )
825 : {
826 : fprintf( pF_spcoh_orig, " %d ", q_direction->coherence_band_data[i].spread_coherence[j] );
827 : }
828 : }
829 : }
830 : fprintf( pF_spcoh_orig, "\n" );
831 : }
832 : #endif
833 :
834 3532 : q_direction->not_in_2D = 0;
835 :
836 : /*Coherence */
837 3532 : if ( all_coherence_zero == 0 )
838 : {
839 3532 : ivas_qmetadata_quantize_coherence_hr_512( hQMetaData, d, all_coherence_zero, hMetaData, bits_sp_coh );
840 : }
841 :
842 : /* write the spherical indexes */
843 3532 : bits_ec = hMetaData->nb_bits_tot;
844 3532 : if ( bits_sph_idx == 11 )
845 : {
846 : /* do the quantization */
847 2352 : quantize_direction_frame( q_direction, azimuth_orig, elevation_orig, 1 );
848 : }
849 :
850 75310 : for ( i = start_band; i < nbands; i++ )
851 : {
852 343140 : for ( j = 0; j < nblocks; j++ )
853 : {
854 271362 : push_next_indice( hMetaData, q_direction->band_data[i].spherical_index[j], bits_sph_idx );
855 : }
856 : }
857 3532 : bits_ec = hMetaData->nb_bits_tot - bits_ec;
858 :
859 : #ifdef DEBUG_MODE_QMETADATA
860 : {
861 : float tmp_f;
862 : fprintf( pF, "frame %d: diff %d ", frame, bits_diff[d] );
863 : fprintf( pF_azi, "frame %d/dir/ec %d: ", frame, d );
864 : fprintf( pF_ele, "frame %d/dir/ec %d: ", frame, d );
865 : fprintf( pF_ratio, "frame %d/dir %d: ", frame, d );
866 : /*fprintf( pF_spcoh, "frame %d/dir %d: ", frame, d ); */
867 : fprintf( pF_spcoh, " %d %d ", frame, d );
868 :
869 : if ( d == 0 )
870 : {
871 : fprintf( pF_surcoh, "frame %d/dir %d: ", frame, d );
872 : }
873 :
874 : /* direction_distance( elevation_orig, azimuth_orig, q_direction, nbands, nblocks, mat_dist );*/
875 : for ( i = start_band; i < nbands; i++ )
876 : {
877 : for ( j = 0; j < nblocks; j++ )
878 : {
879 : fprintf( pF_azi, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[i].azimuth[j] ) / 100.f );
880 : fprintf( pF_ele, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[i].elevation[j] ) / 100.f );
881 : fprintf( pF_ratio, " %1.3f ", q_direction->band_data[i].energy_ratio[j] );
882 : if ( q_direction->coherence_band_data != NULL )
883 : {
884 : fprintf( pF_spcoh, " %d ", q_direction->coherence_band_data[i].spread_coherence[j] );
885 : }
886 : if ( d == 0 && hQMetaData->surcoh_band_data != NULL )
887 : {
888 : fprintf( pF_surcoh, " %d ", hQMetaData->surcoh_band_data[i].surround_coherence[j] );
889 : }
890 : }
891 : }
892 : fprintf( pF, "\n" );
893 : fprintf( pF_azi, "\n" );
894 : fprintf( pF_ele, "\n" );
895 : fprintf( pF_ratio, "\n" );
896 : fprintf( pF_spcoh, "\n" );
897 : if ( d == 0 )
898 : {
899 : fprintf( pF_surcoh, "\n" );
900 : }
901 :
902 : for ( i = 0; i < nblocks; i++ )
903 : {
904 : for ( j = 0; j < nbands; j++ )
905 : {
906 : dbgwrite( &( q_direction->band_data[j].azimuth[i] ), sizeof( float ), 1, 1, "./res/IVAS_QMETADATA_azi.bin" );
907 : dbgwrite( &( q_direction->band_data[j].elevation[i] ), sizeof( float ), 1, 1, "./res/IVAS_QMETADATA_ele.bin" );
908 : dbgwrite( &( q_direction->band_data[j].energy_ratio_index[i] ), sizeof( uint16_t ), 1, 1, "./res/IVAS_QMETADATA_diffuseness_index.bin" );
909 : tmp_f = 1.f - q_direction->band_data[j].energy_ratio[i];
910 : dbgwrite( &tmp_f, sizeof( float ), 1, 1, "./res/IVAS_QMETADATA_diffuseness.bin" );
911 : }
912 : }
913 :
914 : j = (int16_t) ( hMetaData->nb_bits_tot );
915 : dbgwrite( &j, sizeof( int16_t ), 1, 1, "./res/IVAS_QMETADATA_bits.bin" );
916 : }
917 : #endif
918 :
919 : /* Save quantized DOAs */
920 3532 : if ( bits_sph_idx == 11 )
921 : {
922 45810 : for ( i = start_band; i < nbands; i++ )
923 : {
924 43458 : mvr2r( azimuth_orig[i], q_direction->band_data[i].azimuth, nblocks );
925 43458 : mvr2r( elevation_orig[i], q_direction->band_data[i].elevation, nblocks );
926 : }
927 : }
928 : else
929 : {
930 29500 : for ( i = start_band; i < nbands; i++ )
931 : {
932 28320 : mvr2r( q_direction->band_data[i].azimuth, q_direction->band_data[i].q_azimuth, nblocks );
933 28320 : mvr2r( q_direction->band_data[i].elevation, q_direction->band_data[i].q_elevation, nblocks );
934 : }
935 : }
936 : }
937 :
938 2276 : if ( hQMetaData->q_direction->cfg.inactiveBands > 0 )
939 : {
940 300 : hQMetaData->q_direction[0].cfg.nbands += hQMetaData->q_direction->cfg.inactiveBands;
941 300 : if ( ndirections > 1 )
942 : {
943 300 : hQMetaData->q_direction[1].cfg.nbands += hQMetaData->q_direction->cfg.inactiveBands;
944 : }
945 : }
946 :
947 2276 : return error;
948 : }
949 :
950 :
951 : /*-----------------------------------------------------------------------*
952 : * ivas_qmetadata_enc_sid_encode()
953 : *
954 : * Main function for coding SID for Spatial Metadata
955 : *-----------------------------------------------------------------------*/
956 :
957 : /*! r: number of bits written */
958 10855 : void ivas_qmetadata_enc_sid_encode(
959 : BSTR_ENC_HANDLE hMetaData, /* i/o: metadata bitstream handle */
960 : IVAS_QMETADATA *q_metadata, /* i/o: metadata handle */
961 : const int16_t masa_sid_descriptor, /* i : description of MASA SID coding structure */
962 : const int16_t nchan_transport, /* i : number of transport channels */
963 : const int16_t ivas_format /* i : IVAS format */
964 : )
965 : {
966 : int16_t b, m;
967 : int16_t bit_pos_start;
968 : IVAS_QDIRECTION *q_direction;
969 : int16_t nbands, nblocks, start_band;
970 : float avg_direction_vector[3];
971 : float direction_vector[3];
972 : float avg_azimuth[MASA_MAXIMUM_CODING_SUBBANDS];
973 : float avg_elevation[MASA_MAXIMUM_CODING_SUBBANDS];
974 : int16_t bits_dir, bits_diff, bits_delta;
975 : int16_t metadata_sid_bits; /* bits allocated to SID for metadata */
976 : int16_t sba_spar_bitlen;
977 :
978 10855 : if ( ivas_format == SBA_FORMAT )
979 : {
980 3827 : sba_spar_bitlen = ivas_sba_spar_sid_bitlen( nchan_transport );
981 3827 : metadata_sid_bits = (int16_t) ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - sba_spar_bitlen - SID_FORMAT_NBITS - SBA_ORDER_BITS - SBA_PLANAR_BITS - 1; /* -1 for inactive mode header bit*/
982 : }
983 : else
984 : {
985 7028 : metadata_sid_bits = ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS;
986 : }
987 :
988 : #ifdef DEBUG_MODE_QMETADATA
989 : static FILE *pF_azi = NULL;
990 : static FILE *pF_ele = NULL;
991 : static FILE *pF_ratio = NULL;
992 :
993 : if ( pF_azi == NULL )
994 : pF_azi = fopen( "./res/qmetadata_sid_azi_enc.txt", "w" );
995 : if ( pF_ele == NULL )
996 : pF_ele = fopen( "./res/qmetadata_sid_ele_enc.txt", "w" );
997 : if ( pF_ratio == NULL )
998 : pF_ratio = fopen( "./res/qmetadata_sid_ratio_enc.txt", "w" );
999 :
1000 : #endif
1001 :
1002 : /* Save initial position in bitstream */
1003 10855 : bit_pos_start = hMetaData->nb_bits_tot;
1004 :
1005 : /* write for MASA the number of transport channels used at coding and the CPE mode DFT/MDCT */
1006 10855 : if ( masa_sid_descriptor > -1 )
1007 : {
1008 4016 : push_next_indice( hMetaData, masa_sid_descriptor, 1 );
1009 : }
1010 :
1011 : /* Code for one direction: diffuseness and average DOA(s)*/
1012 10855 : q_direction = &( q_metadata->q_direction[0] );
1013 10855 : nbands = q_direction->cfg.nbands;
1014 10855 : nblocks = q_direction->cfg.nblocks;
1015 10855 : start_band = 0; /*Start always with band 0 for SID*/
1016 :
1017 : /* sanity checks*/
1018 10855 : assert( q_metadata->no_directions == 1 && "Qmetadata SID: only one direction supported!" );
1019 10855 : if ( ivas_format == SBA_FORMAT )
1020 : {
1021 3827 : assert( ( q_direction->cfg.nbands == DIRAC_DTX_BANDS ) && "Qmetadata SID: only 2 bands supported!" );
1022 : }
1023 : else
1024 : {
1025 7028 : assert( ( q_direction->cfg.nbands == 5 ) && "Qmetadata SID: only 5 bands supported!" );
1026 : }
1027 :
1028 10855 : if ( ivas_format != SBA_FORMAT )
1029 : {
1030 : /* Signalling 2D*/
1031 7028 : push_next_indice( hMetaData, ( q_direction->not_in_2D > 0 ), 1 ); /*2D flag*/
1032 : }
1033 : else
1034 : {
1035 3827 : q_direction->not_in_2D = 1; /* force for merged modes */
1036 : }
1037 :
1038 : /*Encode the quantized diffuseness in raw coding*/
1039 10855 : bits_dir = 0;
1040 10855 : bits_diff = 0;
1041 10855 : if ( ivas_format != SBA_FORMAT )
1042 : {
1043 42168 : for ( b = start_band; b < nbands; b++ )
1044 : {
1045 35140 : q_direction->band_data[b].energy_ratio_index[0] = max( q_direction->band_data[b].energy_ratio_index[0], 4 );
1046 35140 : bits_diff += ivas_qmetadata_encode_quasi_uniform_length( q_direction->band_data[b].energy_ratio_index[0] - 4, DIRAC_DIFFUSE_LEVELS - 4 );
1047 35140 : q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[q_direction->band_data[b].energy_ratio_index[0]];
1048 :
1049 35140 : if ( q_direction->not_in_2D == 0 )
1050 : {
1051 625 : q_direction->band_data[b].azimuth_m_alphabet[0] = 1 << ( min( 8, q_direction->band_data[b].bits_sph_idx[0] ) );
1052 625 : bits_dir += ivas_qmetadata_encode_quasi_uniform_length( q_direction->band_data[b].azimuth_m_alphabet[0] - 1, q_direction->band_data[b].azimuth_m_alphabet[0] );
1053 : }
1054 : else
1055 : {
1056 34515 : bits_dir += q_direction->band_data[b].bits_sph_idx[0];
1057 : }
1058 : }
1059 :
1060 : /* Reduce bit demand by increasing diffuseness*/
1061 7028 : bits_delta = metadata_sid_bits - ( hMetaData->nb_bits_tot - bit_pos_start ) - bits_diff - bits_dir;
1062 7028 : if ( bits_delta > 0 )
1063 : {
1064 32504 : while ( bits_delta > 0 )
1065 : {
1066 139242 : for ( b = start_band; b < nbands && ( bits_delta > 0 ); b++ )
1067 : {
1068 113766 : if ( q_direction->band_data[b].bits_sph_idx[0] < 11 )
1069 : {
1070 113764 : bits_delta -= 1;
1071 113764 : q_direction->band_data[b].bits_sph_idx[0]++;
1072 : }
1073 : }
1074 : }
1075 :
1076 7028 : if ( q_direction->not_in_2D == 0 )
1077 : {
1078 750 : for ( b = start_band; b < nbands; b++ )
1079 : {
1080 625 : q_direction->band_data[b].azimuth_m_alphabet[0] = 1 << ( min( 8, q_direction->band_data[b].bits_sph_idx[0] ) );
1081 : }
1082 : }
1083 : }
1084 : else
1085 : {
1086 0 : while ( bits_delta < 0 )
1087 : {
1088 0 : for ( b = nbands - 1; b >= start_band && ( bits_delta < 0 ); b-- )
1089 : {
1090 0 : if ( q_direction->band_data[b].bits_sph_idx[0] >= 4 )
1091 : {
1092 0 : bits_delta += 1;
1093 0 : q_direction->band_data[b].bits_sph_idx[0]--;
1094 0 : if ( q_direction->not_in_2D == 0 )
1095 : {
1096 0 : q_direction->band_data[b].azimuth_m_alphabet[0] = 1 << ( min( 8, q_direction->band_data[b].bits_sph_idx[0] ) );
1097 : }
1098 : }
1099 : }
1100 : }
1101 : }
1102 : }
1103 : else
1104 : {
1105 11481 : for ( b = start_band; b < nbands; b++ )
1106 : {
1107 7654 : q_direction->band_data[b].energy_ratio_index[0] = max( q_direction->band_data[b].energy_ratio_index[0], 4 );
1108 7654 : bits_diff += ivas_qmetadata_encode_quasi_uniform_length( q_direction->band_data[b].energy_ratio_index[0] - 4, DIRAC_DIFFUSE_LEVELS - 4 );
1109 7654 : q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[q_direction->band_data[b].energy_ratio_index[0]];
1110 :
1111 7654 : if ( q_direction->not_in_2D == 0 )
1112 : {
1113 0 : q_direction->band_data[b].azimuth_m_alphabet[0] = 1 << ( min( 5, q_direction->band_data[b].bits_sph_idx[0] ) );
1114 0 : bits_dir += ivas_qmetadata_encode_quasi_uniform_length( q_direction->band_data[b].azimuth_m_alphabet[0] - 1, q_direction->band_data[b].azimuth_m_alphabet[0] );
1115 : }
1116 : else
1117 : {
1118 7654 : bits_dir += q_direction->band_data[b].bits_sph_idx[0];
1119 : }
1120 : }
1121 :
1122 : /* Reduce bit demand by increasing diffuseness*/
1123 3827 : bits_delta = metadata_sid_bits - ( hMetaData->nb_bits_tot - bit_pos_start ) - bits_diff - bits_dir;
1124 :
1125 3856 : while ( bits_delta < 0 && ( q_direction->not_in_2D > 0 ) )
1126 : {
1127 78 : for ( b = nbands - 1; b >= start_band && ( bits_delta < 0 ); b-- )
1128 : {
1129 49 : if ( q_direction->band_data[b].energy_ratio_index[0] < ( DIRAC_DIFFUSE_LEVELS - 1 ) )
1130 : {
1131 49 : bits_delta += q_direction->band_data[b].bits_sph_idx[0];
1132 49 : q_direction->band_data[b].energy_ratio_index[0]++;
1133 49 : q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[q_direction->band_data[b].energy_ratio_index[0]];
1134 49 : bits_delta -= q_direction->band_data[b].bits_sph_idx[0];
1135 : }
1136 : }
1137 : }
1138 : }
1139 10855 : assert( ( bits_delta >= 0 ) && "Bit budget in Qmetadata SID is violated!!!" );
1140 :
1141 : /*Code diffuseness*/
1142 53649 : for ( b = start_band; b < nbands; b++ )
1143 : {
1144 42794 : ivas_qmetadata_encode_quasi_uniform( hMetaData, q_direction->band_data[b].energy_ratio_index[0] - 4, DIRAC_DIFFUSE_LEVELS - 4 );
1145 : }
1146 :
1147 : /* Compute and Quantize an average direction per band*/
1148 53649 : for ( b = start_band; b < nbands; b++ )
1149 : {
1150 42794 : set_zero( avg_direction_vector, 3 );
1151 136543 : for ( m = 0; m < nblocks; m++ )
1152 : {
1153 : /*compute the average direction */
1154 93749 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[m], q_direction->band_data[b].elevation[m], direction_vector );
1155 93749 : v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
1156 : }
1157 :
1158 42794 : ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &avg_azimuth[b], &avg_elevation[b] );
1159 :
1160 : /* Quantize the average direction */
1161 42794 : if ( q_direction->not_in_2D == 0 )
1162 : {
1163 625 : avg_elevation[b] = 0;
1164 625 : q_direction->band_data[b].spherical_index[0] = quantize_direction2D( avg_azimuth[b], q_direction->band_data[b].azimuth_m_alphabet[0], &avg_azimuth[b],
1165 625 : &q_direction->band_data[b].azimuth_index[0], q_direction->cfg.mc_ls_setup );
1166 : }
1167 : else
1168 : {
1169 42169 : q_direction->band_data[b].spherical_index[0] = quantize_direction( avg_elevation[b], avg_azimuth[b], q_direction->band_data[b].bits_sph_idx[0], &avg_elevation[b], &avg_azimuth[b],
1170 42169 : &q_direction->band_data[b].elevation_index[0], &q_direction->band_data[b].azimuth_index[0], q_direction->cfg.mc_ls_setup );
1171 : }
1172 :
1173 : /* Save quantized DOAs */
1174 42794 : q_direction->band_data[b].q_azimuth[0] = avg_azimuth[b];
1175 42794 : q_direction->band_data[b].q_elevation[0] = avg_elevation[b];
1176 :
1177 42794 : if ( q_direction->band_data[b].azimuth_index[0] == MASA_NO_INDEX )
1178 : {
1179 125 : q_direction->band_data[b].azimuth_index[0] = 0;
1180 : }
1181 : }
1182 :
1183 : /* quantize average elevation and azimuth angles */
1184 10855 : if ( q_direction->not_in_2D > 0 )
1185 : {
1186 52899 : for ( b = start_band; b < nbands; b++ )
1187 : {
1188 42169 : push_next_indice( hMetaData, q_direction->band_data[b].spherical_index[0], q_direction->band_data[b].bits_sph_idx[0] );
1189 : }
1190 : }
1191 : else
1192 : {
1193 750 : for ( b = start_band; b < nbands; b++ )
1194 : {
1195 625 : ivas_qmetadata_encode_quasi_uniform( hMetaData, q_direction->band_data[b].azimuth_index[0], q_direction->band_data[b].azimuth_m_alphabet[0] );
1196 : }
1197 : }
1198 :
1199 : #ifdef DEBUG_MODE_QMETADATA
1200 : {
1201 : fprintf( pF_azi, "frame %d: ", frame );
1202 : fprintf( pF_ele, "frame %d: ", frame );
1203 : fprintf( pF_ratio, "frame %d: ", frame );
1204 :
1205 :
1206 : /* Data is not used currently. Fix function when needed. */
1207 : /*direction_distance( elevation_orig, azimuth_orig, q_direction->elevation, q_direction->azimuth, nbands, nblocks, mat_dist );*/
1208 : for ( b = start_band; b < nbands; b++ )
1209 : {
1210 : fprintf( pF_azi, " %+5.2f ", (int16_t) ( 100.f * avg_azimuth[b] ) / 100.f );
1211 : fprintf( pF_ele, " %+5.2f ", (int16_t) ( 100.f * avg_elevation[b] ) / 100.f );
1212 : fprintf( pF_ratio, " %1.3f ", q_direction->band_data[b].energy_ratio[0] );
1213 : }
1214 : fprintf( pF_azi, "\n" );
1215 : fprintf( pF_ele, "\n" );
1216 : fprintf( pF_ratio, "\n" );
1217 : }
1218 : #endif
1219 :
1220 : /* fill bits*/
1221 10855 : assert( ( hMetaData->nb_bits_tot - bit_pos_start ) <= metadata_sid_bits && "Too many written bits!" );
1222 25738 : while ( ( hMetaData->nb_bits_tot - bit_pos_start ) < metadata_sid_bits )
1223 : {
1224 14883 : push_next_indice( hMetaData, 0, 1 ); /*fill bit*/
1225 : }
1226 :
1227 10855 : return;
1228 : }
1229 :
1230 :
1231 : /*-------------------------------------------------------------------------
1232 : * reset_metadata_spatial()
1233 : *
1234 : * Reset metadata in spatial formats
1235 : *------------------------------------------------------------------------*/
1236 :
1237 82109 : void reset_metadata_spatial(
1238 : const IVAS_FORMAT ivas_format, /* i : IVAS format */
1239 : BSTR_ENC_HANDLE hMetaData, /* i/o: metadata bitstream handle */
1240 : const int32_t element_brate, /* i : element bitrate */
1241 : int32_t *total_brate, /* o : total bitrate */
1242 : const int32_t core_brate, /* i : core bitrate */
1243 : const int16_t nb_bits_metadata /* i : number of meatdata bits */
1244 : )
1245 : {
1246 : int16_t i, next_ind_sid, last_ind_sid;
1247 : int16_t j;
1248 : int16_t metadata_sid_bits;
1249 :
1250 82109 : if ( core_brate == SID_2k40 || core_brate == FRAME_NO_DATA )
1251 : {
1252 4941 : if ( ( ivas_format == SBA_FORMAT || ivas_format == MASA_FORMAT ) && core_brate != FRAME_NO_DATA )
1253 : {
1254 689 : if ( ivas_format == SBA_FORMAT )
1255 : {
1256 : #ifdef DEBUGGING
1257 : assert( hMetaData->ind_list[0].nb_bits == 1 );
1258 : #endif
1259 484 : hMetaData->ind_list[0].value = 1;
1260 484 : metadata_sid_bits = (int16_t) ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS - SBA_PLANAR_BITS - SBA_ORDER_BITS;
1261 :
1262 484 : while ( hMetaData->nb_bits_tot < metadata_sid_bits )
1263 : {
1264 0 : push_next_indice( hMetaData, 0, 1 ); /*fill bit*/
1265 : }
1266 : }
1267 : else
1268 : {
1269 : /* Reset metadata and keep only SID metadata*/
1270 205 : last_ind_sid = hMetaData->nb_ind_tot;
1271 205 : next_ind_sid = hMetaData->nb_ind_tot;
1272 2534 : while ( hMetaData->nb_bits_tot > nb_bits_metadata )
1273 : {
1274 2329 : next_ind_sid--;
1275 2329 : hMetaData->nb_bits_tot -= hMetaData->ind_list[next_ind_sid].nb_bits;
1276 : }
1277 :
1278 205 : hMetaData->nb_bits_tot = 0;
1279 :
1280 4279 : for ( i = 0; i < next_ind_sid; i++ )
1281 : {
1282 4074 : hMetaData->ind_list[i].nb_bits = -1;
1283 : #if defined( DEBUGGING ) && defined( DBG_BITSTREAM_ANALYSIS )
1284 : sprintf( hMetaData->ind_list[i].function_name, "RESET in reset_metadata_spatial" );
1285 : #endif
1286 : }
1287 :
1288 2534 : for ( j = 0, i = next_ind_sid; i < last_ind_sid; i++, j++ )
1289 : {
1290 2329 : hMetaData->ind_list[j].value = hMetaData->ind_list[i].value;
1291 2329 : hMetaData->ind_list[j].nb_bits = hMetaData->ind_list[i].nb_bits;
1292 : #if defined( DEBUGGING ) && defined( DBG_BITSTREAM_ANALYSIS )
1293 : strncpy( hMetaData->ind_list[j].function_name, hMetaData->ind_list[i].function_name, 100 );
1294 : #endif
1295 2329 : hMetaData->nb_bits_tot += hMetaData->ind_list[j].nb_bits;
1296 2329 : hMetaData->ind_list[i].nb_bits = -1;
1297 : #ifdef DBG_BISTREAM_ANALYSIS
1298 : sprintf( hMetaData->ind_list[i].function_name, "RESET in reset_metadata_spatial" );
1299 : #endif
1300 : }
1301 :
1302 205 : hMetaData->nb_ind_tot = j;
1303 : #ifdef DEBUGGING
1304 : assert( ( hMetaData->nb_bits_tot == ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS ) && "Problem of SID metadata in SCE" );
1305 : #endif
1306 : }
1307 : }
1308 : else
1309 : {
1310 : /*Reset metadata*/
1311 4252 : reset_indices_enc( hMetaData, hMetaData->nb_ind_tot );
1312 : }
1313 :
1314 4941 : *total_brate = element_brate;
1315 : }
1316 77168 : else if ( ivas_format != SBA_FORMAT )
1317 : {
1318 : /* Reset SID metadata bits*/
1319 101903 : while ( hMetaData->nb_bits_tot > nb_bits_metadata )
1320 : {
1321 65156 : hMetaData->nb_ind_tot--;
1322 65156 : hMetaData->nb_bits_tot -= hMetaData->ind_list[hMetaData->nb_ind_tot].nb_bits;
1323 65156 : hMetaData->ind_list[hMetaData->nb_ind_tot].nb_bits = -1;
1324 : #if defined( DEBUGGING ) && defined( DBG_BITSTREAM_ANALYSIS )
1325 : sprintf( hMetaData->ind_list[hMetaData->nb_ind_tot].function_name, "RESET in reset_metadata_spatial" );
1326 : #endif
1327 : }
1328 : #ifdef DEBUGGING
1329 : assert( hMetaData->nb_bits_tot == nb_bits_metadata && "Problem in metadata for SCE" );
1330 : #endif
1331 : }
1332 :
1333 82109 : return;
1334 : }
1335 :
1336 :
1337 : /*-------------------------------------------------------------------------
1338 : * quantize_direction2D()
1339 : *
1340 : *
1341 : *------------------------------------------------------------------------*/
1342 :
1343 : /*! r: quantized spherical index */
1344 87841 : int16_t quantize_direction2D(
1345 : float phi, /* i : input azimuth value */
1346 : const int16_t no_cw, /* i : number of bits */
1347 : float *phi_q, /* o : quantized azimuth value */
1348 : uint16_t *index_phi, /* o : quantized azimuth index */
1349 : const MC_LS_SETUP mc_format /* i : channel format if in MC-mode */
1350 : )
1351 : {
1352 : int16_t idx_sph;
1353 : uint16_t id_phi;
1354 87841 : if ( no_cw < 2 )
1355 : {
1356 0 : *phi_q = 0;
1357 :
1358 0 : return 0;
1359 : }
1360 :
1361 87841 : if ( mc_format != MC_LS_SETUP_INVALID )
1362 : {
1363 86004 : id_phi = quantize_phi_chan_compand( phi + 180, phi_q, no_cw, 0, mc_format );
1364 : }
1365 : else
1366 : {
1367 1837 : id_phi = quantize_phi( phi + 180, 0, phi_q, no_cw );
1368 : }
1369 87841 : *phi_q -= 180;
1370 87841 : *index_phi = ivas_qmetadata_reorder_generic( id_phi - ( no_cw >> 1 ) );
1371 :
1372 87841 : idx_sph = id_phi;
1373 :
1374 87841 : return idx_sph;
1375 : }
1376 :
1377 :
1378 2276 : static void ivas_qmetadata_quantize_diffuseness_nrg_ratios_hr_512(
1379 : IVAS_QMETADATA_HANDLE hQMetaData,
1380 : int16_t *needed_bits,
1381 : const int16_t bits_dir_hr,
1382 : BSTR_ENC_HANDLE hMetaData )
1383 : {
1384 : int16_t j, k;
1385 : int16_t index;
1386 :
1387 2276 : needed_bits[0] = 0;
1388 2276 : needed_bits[1] = 0;
1389 :
1390 56600 : for ( j = hQMetaData->q_direction[0].cfg.start_band; j < hQMetaData->q_direction[0].cfg.nbands; ++j )
1391 : {
1392 261270 : for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ )
1393 : {
1394 206946 : index = masa_sq( 1.0f - hQMetaData->q_direction[0].band_data[j].energy_ratio[k], diffuseness_thresholds_hr, HR_MASA_ER_LEVELS );
1395 206946 : push_next_indice( hMetaData, index, MASA_BITS_ER_HR );
1396 206946 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index[k] = index;
1397 206946 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index_mod[k] = index;
1398 206946 : hQMetaData->q_direction[0].band_data[j].energy_ratio[k] = 1.0f - diffuseness_reconstructions_hr[index];
1399 206946 : needed_bits[0] += MASA_BITS_ER_HR;
1400 206946 : hQMetaData->q_direction[0].band_data[j].bits_sph_idx[k] = bits_dir_hr;
1401 : }
1402 : }
1403 :
1404 2276 : if ( hQMetaData->no_directions == 2 )
1405 : {
1406 : float ratioSum;
1407 1256 : if ( bits_dir_hr == 16 )
1408 : {
1409 9500 : for ( j = hQMetaData->q_direction[1].cfg.start_band; j < hQMetaData->q_direction[1].cfg.nbands; j++ )
1410 : {
1411 45600 : for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ )
1412 : {
1413 36480 : index = masa_sq( 1.0f - hQMetaData->q_direction[1].band_data[j].energy_ratio[k], diffuseness_thresholds_hr, HR_MASA_ER_LEVELS );
1414 36480 : push_next_indice( hMetaData, index, MASA_BITS_ER_HR );
1415 36480 : hQMetaData->q_direction[1].band_data[j].energy_ratio_index[k] = index;
1416 36480 : hQMetaData->q_direction[1].band_data[j].energy_ratio[k] = 1.0f - diffuseness_reconstructions_hr[index];
1417 :
1418 36480 : ratioSum = hQMetaData->q_direction[0].band_data[j].energy_ratio[k] + hQMetaData->q_direction[1].band_data[j].energy_ratio[k];
1419 36480 : if ( ratioSum > 1.0f )
1420 : {
1421 1294 : hQMetaData->q_direction[0].band_data[j].energy_ratio[k] /= ratioSum;
1422 1294 : hQMetaData->q_direction[1].band_data[j].energy_ratio[k] /= ratioSum;
1423 : }
1424 :
1425 36480 : needed_bits[1] += MASA_BITS_ER_HR;
1426 36480 : hQMetaData->q_direction[1].band_data[j].bits_sph_idx[k] = bits_dir_hr;
1427 : }
1428 : }
1429 : }
1430 : else
1431 : {
1432 : int16_t pos_2dir_band[MASA_MAXIMUM_CODING_SUBBANDS];
1433 876 : k = 0;
1434 21600 : for ( j = hQMetaData->q_direction[0].cfg.start_band; j < hQMetaData->q_direction[0].cfg.nbands; j++ )
1435 : {
1436 20724 : if ( hQMetaData->twoDirBands[j] == 1 )
1437 : {
1438 8334 : pos_2dir_band[k] = j;
1439 8334 : k++;
1440 : }
1441 : else
1442 : {
1443 12390 : pos_2dir_band[k] = 0;
1444 : }
1445 : }
1446 9210 : for ( j = hQMetaData->q_direction[1].cfg.start_band; j < hQMetaData->q_direction[1].cfg.nbands; j++ )
1447 : {
1448 36270 : for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ )
1449 : {
1450 27936 : index = masa_sq( 1.0f - hQMetaData->q_direction[1].band_data[j].energy_ratio[k], diffuseness_thresholds_hr, HR_MASA_ER_LEVELS );
1451 27936 : push_next_indice( hMetaData, index, MASA_BITS_ER_HR );
1452 27936 : hQMetaData->q_direction[1].band_data[j].energy_ratio_index[k] = index;
1453 27936 : hQMetaData->q_direction[1].band_data[j].energy_ratio[k] = 1.0f - diffuseness_reconstructions_hr[index];
1454 :
1455 27936 : ratioSum = hQMetaData->q_direction[0].band_data[pos_2dir_band[j]].energy_ratio[k] + hQMetaData->q_direction[1].band_data[j].energy_ratio[k];
1456 :
1457 27936 : if ( ratioSum > 1.0f )
1458 : {
1459 326 : hQMetaData->q_direction[0].band_data[pos_2dir_band[j]].energy_ratio[k] /= ratioSum;
1460 326 : hQMetaData->q_direction[1].band_data[j].energy_ratio[k] /= ratioSum;
1461 : }
1462 :
1463 27936 : needed_bits[1] += MASA_BITS_ER_HR;
1464 27936 : hQMetaData->q_direction[1].band_data[j].bits_sph_idx[k] = bits_dir_hr;
1465 : }
1466 : }
1467 : }
1468 : }
1469 :
1470 2276 : return;
1471 : }
1472 :
1473 :
1474 : /*-------------------------------------------------------------------------
1475 : * ivas_qmetadata_quantize_diffuseness_nrg_ratios()
1476 : *
1477 : * Quantize diffuseness
1478 : *------------------------------------------------------------------------*/
1479 :
1480 204703 : static void ivas_qmetadata_quantize_diffuseness_nrg_ratios(
1481 : IVAS_QMETADATA_HANDLE hQMetaData,
1482 : int16_t *needed_bits,
1483 : int16_t *nbits_diff,
1484 : int16_t *dfRatioBits,
1485 : const int16_t hodirac_flag )
1486 : {
1487 : int16_t j, k, dir2band;
1488 : int16_t index_dirRatio1Inv, index_dirRatio2Inv, index_dirRatio1Inv_mod, index_dirRatio2Inv_mod;
1489 : int16_t index_diff;
1490 :
1491 204703 : nbits_diff[0] = 0;
1492 204703 : nbits_diff[1] = 0;
1493 204703 : needed_bits[0] = 0;
1494 204703 : needed_bits[1] = 0;
1495 204703 : dir2band = 0;
1496 :
1497 1097205 : for ( j = hQMetaData->q_direction[0].cfg.start_band; j < hQMetaData->q_direction[0].cfg.nbands; ++j )
1498 : {
1499 892502 : if ( hQMetaData->no_directions == 2 && hQMetaData->twoDirBands[j] == 1 )
1500 191840 : {
1501 : float diffRatio, dfRatio, dfRatioQ, diffRatioQ, dirRatio1Q, dirRatio2Q;
1502 : float dirRatio1, dirRatio2, sumRatio;
1503 : int16_t dfRatio_index, dfRatio_qsteps, dfRatio_bits;
1504 :
1505 : /* With 2dir metadata, we quantize and transmit diffuse-to-total ratio (diffRatio) and
1506 : * distribution factor of direct-to-total ratios (dFRatio). This is more efficient and
1507 : * accurate than simple separate quantization of each direct-to-total ratio or their
1508 : * separate inverses. */
1509 191840 : if ( hodirac_flag )
1510 : {
1511 : /* already encoded as total and ratios in HO-DirAC */
1512 178640 : diffRatio = 1.f - hQMetaData->q_direction[0].band_data[j].energy_ratio[0];
1513 178640 : dfRatio = hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[0];
1514 : }
1515 : else
1516 : {
1517 13200 : dirRatio1 = hQMetaData->q_direction[0].band_data[j].energy_ratio[0];
1518 13200 : dirRatio2 = hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[0];
1519 13200 : sumRatio = dirRatio1 + dirRatio2;
1520 13200 : diffRatio = 1.0f - sumRatio;
1521 13200 : dfRatio = sumRatio < EPSILON ? 0.5f : dirRatio1 / sumRatio;
1522 : }
1523 :
1524 :
1525 191840 : index_diff = masa_sq( diffRatio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
1526 191840 : diffRatioQ = diffuseness_reconstructions[index_diff];
1527 :
1528 191840 : if ( hodirac_flag )
1529 : {
1530 178640 : dfRatio_bits = ivas_get_df_ratio_bits_hodirac( index_diff );
1531 : }
1532 : else
1533 : {
1534 13200 : dfRatio_bits = ivas_get_df_ratio_bits( index_diff );
1535 : }
1536 :
1537 191840 : dfRatioBits[dir2band] = dfRatio_bits;
1538 :
1539 191840 : dfRatio_qsteps = ( 1 << dfRatio_bits );
1540 191840 : if ( hodirac_flag )
1541 : {
1542 178640 : dfRatio_index = usquant( dfRatio, &dfRatioQ, 0.0f, 1.f / ( dfRatio_qsteps - 1 ), dfRatio_qsteps );
1543 178640 : dirRatio1Q = 1.f - diffRatioQ;
1544 178640 : dirRatio2Q = dfRatioQ;
1545 : }
1546 : else
1547 : {
1548 13200 : dfRatio_index = usquant( dfRatio, &dfRatioQ, 0.5f, 0.5f / ( dfRatio_qsteps - 1 ), dfRatio_qsteps );
1549 :
1550 : /* Direction quantization requires also separately quantized direct-to-total ratios. Thus, we calculate them. */
1551 13200 : dirRatio1Q = dfRatioQ * ( 1.0f - diffRatioQ );
1552 13200 : dirRatio2Q = ( 1.0f - diffRatioQ ) - dirRatio1Q;
1553 : }
1554 :
1555 191840 : index_dirRatio1Inv = masa_sq( 1.0f - dirRatio1Q, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
1556 :
1557 : /* Note: To save memory, we store temporarily index_diff and dfRatio_index into first and second direction
1558 : * energy ratio index variables until they have been encoded. index_dirRatio1Inv and index_dirRatio2Inv are
1559 : * then later retrieved for further use in encoding. */
1560 950068 : for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ )
1561 : {
1562 758228 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index[k] = index_diff;
1563 758228 : hQMetaData->q_direction[0].band_data[j].energy_ratio[k] = dirRatio1Q;
1564 : }
1565 191840 : nbits_diff[0] += MASA_BITS_ER;
1566 :
1567 191840 : if ( hodirac_flag )
1568 : {
1569 : float tmp;
1570 178640 : index_dirRatio2Inv = usquant( dirRatio2Q, &tmp, 0.0f, 1.f / ( DIRAC_DIFFUSE_LEVELS - 1 ), DIRAC_DIFFUSE_LEVELS );
1571 : }
1572 : else
1573 : {
1574 13200 : index_dirRatio2Inv = masa_sq( 1.0f - dirRatio2Q, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
1575 : }
1576 :
1577 950068 : for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ )
1578 : {
1579 758228 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[k] = dfRatio_index;
1580 758228 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[k] = dirRatio2Q;
1581 : }
1582 191840 : nbits_diff[1] += dfRatio_bits;
1583 :
1584 : /* Obtain compensated direct-to-total ratios for direction quantization. This compensates for the
1585 : * fact that with 2dir data, it is harder to achieve separate high direct-to-total ratio values
1586 : * which are assumed by the direction quantization system. In practice, this improves direction
1587 : * accuracy when it is perceptual meaningful. */
1588 191840 : masa_compensate_two_dir_energy_ratio_index( index_dirRatio1Inv, index_dirRatio2Inv, &index_dirRatio1Inv_mod, &index_dirRatio2Inv_mod, hodirac_flag );
1589 :
1590 950068 : for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ )
1591 : {
1592 758228 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index_mod[k] = index_dirRatio1Inv_mod;
1593 758228 : hQMetaData->q_direction[0].band_data[j].bits_sph_idx[k] = bits_direction_masa[index_dirRatio1Inv_mod];
1594 : }
1595 191840 : needed_bits[0] += hQMetaData->q_direction[0].cfg.nblocks * bits_direction_masa[index_dirRatio1Inv_mod];
1596 :
1597 950068 : for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ )
1598 : {
1599 758228 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index_mod[k] = index_dirRatio2Inv_mod;
1600 758228 : hQMetaData->q_direction[1].band_data[dir2band].bits_sph_idx[k] = bits_direction_masa[index_dirRatio2Inv_mod];
1601 : }
1602 191840 : needed_bits[1] += hQMetaData->q_direction[1].cfg.nblocks * bits_direction_masa[index_dirRatio2Inv_mod];
1603 :
1604 191840 : dir2band++;
1605 : }
1606 : else
1607 : {
1608 700662 : index_dirRatio1Inv = masa_sq( 1.0f - hQMetaData->q_direction[0].band_data[j].energy_ratio[0], diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
1609 :
1610 2942781 : for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ )
1611 : {
1612 2242119 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index[k] = index_dirRatio1Inv;
1613 2242119 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index_mod[k] = index_dirRatio1Inv;
1614 2242119 : hQMetaData->q_direction[0].band_data[j].energy_ratio[k] = 1.0f - diffuseness_reconstructions[index_dirRatio1Inv];
1615 2242119 : hQMetaData->q_direction[0].band_data[j].bits_sph_idx[k] = bits_direction_masa[index_dirRatio1Inv];
1616 : }
1617 :
1618 700662 : nbits_diff[0] += MASA_BITS_ER;
1619 :
1620 700662 : needed_bits[0] += hQMetaData->q_direction[0].cfg.nblocks * bits_direction_masa[index_dirRatio1Inv];
1621 : }
1622 : }
1623 :
1624 204703 : return;
1625 : }
1626 :
1627 :
1628 : /*-------------------------------------------------------------------------
1629 : * ivas_diffuseness_huff_ec_encode()
1630 : *
1631 : *
1632 : *------------------------------------------------------------------------*/
1633 :
1634 206029 : static int16_t ivas_diffuseness_huff_ec_encode(
1635 : BSTR_ENC_HANDLE hMetaData,
1636 : const uint16_t idx )
1637 : {
1638 : int16_t nbits;
1639 206029 : nbits = 0;
1640 206029 : if ( idx <= DIFF_EC_HUFF_GR0_LIMIT )
1641 : {
1642 205765 : if ( idx > 0 )
1643 : {
1644 107672 : push_next_indice( hMetaData, ( 1 << idx ) - 1, idx );
1645 107672 : nbits += idx;
1646 : }
1647 205765 : push_next_indice( hMetaData, 0, 1 );
1648 205765 : nbits += 1;
1649 : }
1650 : else
1651 : {
1652 264 : push_next_indice( hMetaData, 511, DIFF_EC_HUFF_GR0_LIMIT + 1 );
1653 264 : push_next_indice( hMetaData, idx - DIFF_EC_HUFF_GR0_LIMIT - 1, 2 );
1654 264 : nbits += DIFF_EC_HUFF_GR0_LIMIT + 3;
1655 : }
1656 206029 : return nbits;
1657 : }
1658 :
1659 :
1660 : /*-------------------------------------------------------------------------
1661 : * ivas_diffuseness_huff_ec_prepare()
1662 : *
1663 : *
1664 : *------------------------------------------------------------------------*/
1665 :
1666 25294 : static void ivas_diffuseness_huff_ec_prepare(
1667 : IVAS_QDIRECTION *q_direction,
1668 : int16_t *best_av,
1669 : uint16_t *avr_idx,
1670 : int16_t *diffuseness_bits_huff )
1671 : {
1672 : int16_t bits;
1673 : int16_t av_crt;
1674 : int16_t av;
1675 : int16_t sh_idx;
1676 : uint16_t ui_sh_idx[MASA_MAXIMUM_CODING_SUBBANDS];
1677 : int16_t b, start_band, nbands;
1678 :
1679 25294 : start_band = q_direction->cfg.start_band;
1680 25294 : nbands = q_direction->cfg.nbands;
1681 :
1682 25294 : *diffuseness_bits_huff = 0;
1683 25294 : av = 0;
1684 328342 : for ( b = start_band; b < nbands; b++ )
1685 : {
1686 303048 : av += q_direction->band_data[b].energy_ratio_index[0];
1687 : }
1688 25294 : av = (int16_t) ( 0.5f + av / (float) nbands );
1689 25294 : *best_av = av;
1690 :
1691 25294 : *diffuseness_bits_huff = MAX16B;
1692 101176 : for ( av_crt = av - 1; av_crt <= av + 1; av_crt++ )
1693 : {
1694 75882 : bits = 0;
1695 985026 : for ( b = start_band; b < nbands; b++ )
1696 : {
1697 909144 : sh_idx = q_direction->band_data[b].energy_ratio_index[0] - av_crt;
1698 909144 : ui_sh_idx[b] = ( sh_idx <= 0 ) ? ( -2 * sh_idx ) : sh_idx * 2 - 1;
1699 909144 : if ( ui_sh_idx[b] >= 2 * DIRAC_DIFFUSE_LEVELS - 3 )
1700 : {
1701 361 : bits = 100; /* to avoid difference larger than 6 in absolute value */
1702 : }
1703 :
1704 909144 : bits += ( ui_sh_idx[b] <= DIFF_EC_HUFF_GR0_LIMIT ) ? ( ui_sh_idx[b] + 1 ) : 11;
1705 : }
1706 :
1707 75882 : if ( bits < *diffuseness_bits_huff )
1708 : {
1709 45172 : *diffuseness_bits_huff = bits;
1710 45172 : mvs2s( (int16_t *) ui_sh_idx, (int16_t *) avr_idx, nbands );
1711 45172 : *best_av = av_crt;
1712 : }
1713 : }
1714 :
1715 25294 : *diffuseness_bits_huff += MASA_BITS_ER; /* for the average */
1716 :
1717 25294 : return;
1718 : }
1719 :
1720 : /*-------------------------------------------------------------------------
1721 : * ivas_qmetadata_entropy_encode_diffuseness()
1722 : *
1723 : * encode diffuseness
1724 : *------------------------------------------------------------------------*/
1725 :
1726 204703 : static int16_t ivas_qmetadata_entropy_encode_diffuseness(
1727 : BSTR_ENC_HANDLE hMetaData,
1728 : IVAS_QDIRECTION *q_direction,
1729 : uint16_t *diffuseness_index_max_ec_frame )
1730 : {
1731 : int16_t start_bit_pos;
1732 : int16_t diffuseness_bits_raw;
1733 : int16_t b;
1734 : int16_t min_diffuseness_m_index, max_diffuseness_m_index;
1735 : int16_t nbands;
1736 : int16_t start_band;
1737 :
1738 204703 : nbands = q_direction->cfg.nbands;
1739 204703 : start_band = q_direction->cfg.start_band;
1740 :
1741 204703 : start_bit_pos = hMetaData->nb_bits_tot;
1742 :
1743 204703 : if ( nbands == 1 )
1744 : {
1745 : /* If there is only one band, diffuseness should be coded directly as raw with no signaling. */
1746 4016 : push_next_indice( hMetaData, q_direction->band_data[0].energy_ratio_index[0], MASA_BITS_ER );
1747 4016 : *diffuseness_index_max_ec_frame = 5;
1748 4016 : return ( hMetaData->nb_bits_tot - start_bit_pos );
1749 : }
1750 :
1751 : /* compute the number of raw coding bits */
1752 200687 : diffuseness_bits_raw = 0;
1753 1089173 : for ( b = start_band; b < nbands; b++ )
1754 : {
1755 888486 : diffuseness_bits_raw += ivas_qmetadata_encode_quasi_uniform_length( q_direction->band_data[b].energy_ratio_index[0], DIRAC_DIFFUSE_LEVELS );
1756 : }
1757 :
1758 200687 : min_diffuseness_m_index = q_direction->band_data[start_band].energy_ratio_index[0];
1759 200687 : max_diffuseness_m_index = q_direction->band_data[start_band].energy_ratio_index[0];
1760 :
1761 1089173 : for ( b = start_band; b < nbands; b++ )
1762 : {
1763 888486 : if ( q_direction->band_data[b].energy_ratio_index[0] < min_diffuseness_m_index )
1764 : {
1765 104209 : min_diffuseness_m_index = q_direction->band_data[b].energy_ratio_index[0];
1766 : }
1767 :
1768 888486 : if ( q_direction->band_data[b].energy_ratio_index[0] > max_diffuseness_m_index )
1769 : {
1770 132176 : max_diffuseness_m_index = q_direction->band_data[b].energy_ratio_index[0];
1771 : }
1772 : }
1773 :
1774 200687 : if ( nbands < DIFF_EC_HUFF_BAND_LIMIT )
1775 : {
1776 : /* Use similarity coding approach or raw coding when there is a low number of bands. */
1777 : /* one bit is used to indicate whether diffuseness values are entropy coded or coded raw */
1778 175393 : if ( min_diffuseness_m_index == max_diffuseness_m_index ) /* all values are equal */
1779 : {
1780 52502 : push_next_indice( hMetaData, 0, 1 ); /* dif_use_raw_coding */
1781 52502 : push_next_indice( hMetaData, 1, 1 ); /* dif_have_unique_value */
1782 52502 : ivas_qmetadata_encode_quasi_uniform( hMetaData, min_diffuseness_m_index, DIRAC_DIFFUSE_LEVELS ); /* dif_unique_value */
1783 : }
1784 122891 : else if ( min_diffuseness_m_index + 1 == max_diffuseness_m_index ) /* only two consecutive values are present */
1785 : {
1786 39356 : push_next_indice( hMetaData, 0, 1 ); /* dif_use_raw_coding */
1787 39356 : push_next_indice( hMetaData, 0, 1 ); /* dif_have_unique_value */
1788 39356 : ivas_qmetadata_encode_quasi_uniform( hMetaData, min_diffuseness_m_index, DIRAC_DIFFUSE_LEVELS - 1 ); /* dif_min_value */
1789 :
1790 172761 : for ( b = start_band; b < nbands; b++ )
1791 : {
1792 133405 : push_next_indice( hMetaData, q_direction->band_data[b].energy_ratio_index[0] - min_diffuseness_m_index, 1 ); /* dif_bit_offset_values */
1793 : }
1794 : }
1795 : else /* raw coding */
1796 : {
1797 83535 : push_next_indice( hMetaData, 1, 1 ); /* dif_use_raw_coding */
1798 :
1799 380630 : for ( b = start_band; b < nbands; b++ )
1800 : {
1801 297095 : ivas_qmetadata_encode_quasi_uniform( hMetaData, q_direction->band_data[b].energy_ratio_index[0], DIRAC_DIFFUSE_LEVELS ); /* dif_values */
1802 : }
1803 : }
1804 : }
1805 : else
1806 : {
1807 : /* Use Huffman-coding approach or raw coding when there is a high number of bands. */
1808 : int16_t diffuseness_bits_huff;
1809 : int16_t best_av;
1810 : uint16_t avr_idx[MASA_MAXIMUM_CODING_SUBBANDS];
1811 :
1812 : /* First, obtain average indices and bit usage for Huffman-coding. */
1813 25294 : ivas_diffuseness_huff_ec_prepare( q_direction, &best_av, avr_idx, &diffuseness_bits_huff );
1814 :
1815 : /* If there is benefit, use Huffman-coding. Otherwise, use raw coding. */
1816 25294 : if ( diffuseness_bits_huff < diffuseness_bits_raw )
1817 : {
1818 : /* Signal Huffman EC */
1819 17324 : push_next_indice( hMetaData, 0, 1 );
1820 17324 : push_next_indice( hMetaData, best_av, MASA_BITS_ER );
1821 223353 : for ( b = start_band; b < nbands; b++ )
1822 : {
1823 206029 : ivas_diffuseness_huff_ec_encode( hMetaData, avr_idx[b] );
1824 : }
1825 : #ifdef DEBUGGING
1826 : assert( ( hMetaData->nb_bits_tot - start_bit_pos ) == diffuseness_bits_huff + 1 );
1827 : #endif
1828 : }
1829 : else
1830 : {
1831 : /* Signal raw */
1832 7970 : push_next_indice( hMetaData, 1, 1 );
1833 104989 : for ( b = start_band; b < nbands; b++ )
1834 : {
1835 97019 : ivas_qmetadata_encode_quasi_uniform( hMetaData, q_direction->band_data[b].energy_ratio_index[0], DIRAC_DIFFUSE_LEVELS ); /* dif_values */
1836 : }
1837 : }
1838 : }
1839 :
1840 200687 : *diffuseness_index_max_ec_frame = 5;
1841 : /* adaptively select the diffuseness_index_max_ec threshold */
1842 200687 : if ( min_diffuseness_m_index > 5 )
1843 : {
1844 76455 : *diffuseness_index_max_ec_frame = DIRAC_DIFFUSE_LEVELS - 1;
1845 : }
1846 :
1847 : #ifdef DEBUGGING
1848 : assert( ( hMetaData->nb_bits_tot - start_bit_pos ) <= 1 + diffuseness_bits_raw );
1849 : #endif
1850 :
1851 200687 : return ( hMetaData->nb_bits_tot - start_bit_pos );
1852 : }
1853 :
1854 :
1855 : /*-------------------------------------------------------------------------
1856 : * ivas_qmetadata_entropy_encode_df_ratio()
1857 : *
1858 : * encode dfRatio
1859 : *------------------------------------------------------------------------*/
1860 :
1861 20820 : static int16_t ivas_qmetadata_entropy_encode_df_ratio(
1862 : BSTR_ENC_HANDLE hMetaData,
1863 : IVAS_QDIRECTION *q_direction,
1864 : int16_t *df_ratio_bits )
1865 : {
1866 : int16_t start_bit_pos;
1867 : int16_t bits_raw;
1868 : int16_t b;
1869 : int16_t min_index, max_index;
1870 : int16_t nbands, start_band;
1871 : int16_t max_df_ratio_bits;
1872 20820 : int16_t ec_mode = 0;
1873 : int16_t max_alphabet_size;
1874 :
1875 20820 : nbands = q_direction->cfg.nbands;
1876 20820 : start_band = q_direction->cfg.start_band;
1877 :
1878 20820 : start_bit_pos = hMetaData->nb_bits_tot;
1879 :
1880 20820 : if ( nbands == 1 )
1881 : {
1882 : /* If there is only one band, ratio should be coded directly as raw with no signaling. */
1883 1732 : push_next_indice( hMetaData, q_direction->band_data[0].energy_ratio_index[0], df_ratio_bits[0] );
1884 :
1885 1732 : return ( hMetaData->nb_bits_tot - start_bit_pos );
1886 : }
1887 :
1888 : /* compute the number of raw coding bits */
1889 19088 : bits_raw = 0;
1890 19088 : max_df_ratio_bits = 0;
1891 209196 : for ( b = start_band; b < nbands; b++ )
1892 : {
1893 190108 : bits_raw += df_ratio_bits[b];
1894 190108 : max_df_ratio_bits = max( df_ratio_bits[b], max_df_ratio_bits );
1895 : }
1896 :
1897 19088 : min_index = q_direction->band_data[start_band].energy_ratio_index[0];
1898 19088 : max_index = q_direction->band_data[start_band].energy_ratio_index[0];
1899 209196 : for ( b = start_band; b < nbands; b++ )
1900 : {
1901 190108 : if ( q_direction->band_data[b].energy_ratio_index[0] < min_index )
1902 : {
1903 14352 : min_index = q_direction->band_data[b].energy_ratio_index[0];
1904 : }
1905 :
1906 190108 : if ( q_direction->band_data[b].energy_ratio_index[0] > max_index )
1907 : {
1908 18729 : max_index = q_direction->band_data[b].energy_ratio_index[0];
1909 : }
1910 : }
1911 :
1912 : /* Decide what modes are possible */
1913 19088 : if ( bits_raw >= max_df_ratio_bits + 2 + nbands )
1914 : {
1915 11396 : ec_mode = 2;
1916 : }
1917 7692 : else if ( bits_raw >= max_df_ratio_bits + 1 )
1918 : {
1919 7692 : ec_mode = 1;
1920 : }
1921 : else
1922 : {
1923 0 : ec_mode = 0;
1924 : }
1925 19088 : max_alphabet_size = 1 << max_df_ratio_bits;
1926 :
1927 19088 : if ( min_index == max_index && ec_mode > 0 ) /* all values are equal */
1928 : {
1929 399 : push_next_indice( hMetaData, 0, 1 ); /* Signal between EC and raw */
1930 399 : if ( ec_mode > 1 )
1931 : {
1932 : /* Only use bit for signaling if necessary */
1933 102 : push_next_indice( hMetaData, 0, 1 ); /* Signal between one value or bandwise diff mode */
1934 : }
1935 :
1936 399 : ivas_qmetadata_encode_quasi_uniform( hMetaData, min_index, max_alphabet_size );
1937 : }
1938 18689 : else if ( min_index + 1 == max_index && ec_mode > 1 ) /* only two consecutive values are present */
1939 : {
1940 1243 : push_next_indice( hMetaData, 0, 1 );
1941 1243 : push_next_indice( hMetaData, 1, 1 );
1942 1243 : ivas_qmetadata_encode_quasi_uniform( hMetaData, min_index, max_alphabet_size - 1 );
1943 :
1944 12142 : for ( b = start_band; b < nbands; b++ )
1945 : {
1946 10899 : push_next_indice( hMetaData, q_direction->band_data[b].energy_ratio_index[0] - min_index, 1 ); /* Band-wise offset values */
1947 : }
1948 : }
1949 : else /* raw coding */
1950 : {
1951 17446 : if ( ec_mode > 0 )
1952 : {
1953 17446 : push_next_indice( hMetaData, 1, 1 ); /* Only signal raw mode if not implicitly using it */
1954 : }
1955 :
1956 194783 : for ( b = start_band; b < nbands; b++ )
1957 : {
1958 177337 : ivas_qmetadata_encode_quasi_uniform( hMetaData, q_direction->band_data[b].energy_ratio_index[0], 1 << df_ratio_bits[b] ); /* dif_values */
1959 : }
1960 : }
1961 :
1962 19088 : return ( hMetaData->nb_bits_tot - start_bit_pos );
1963 : }
1964 :
1965 :
1966 : /*-------------------------------------------------------------------------
1967 : * restore_metadata_buffer()
1968 : *
1969 : * Restore metadata buffer
1970 : *------------------------------------------------------------------------*/
1971 :
1972 250251 : void restore_metadata_buffer(
1973 : BSTR_ENC_HANDLE hMetaData,
1974 : const int16_t next_ind_start,
1975 : const int16_t bit_pos_start )
1976 : {
1977 : int16_t i;
1978 :
1979 11298014 : for ( i = next_ind_start; i < hMetaData->nb_ind_tot; i++ )
1980 : {
1981 11047763 : hMetaData->ind_list[i].nb_bits = -1;
1982 : #if defined( DEBUGGING ) && defined( DBG_BITSTREAM_ANALYSIS )
1983 : sprintf( hMetaData->ind_list[i].function_name, "RESET in restore_metadata_buffer" );
1984 : #endif
1985 : }
1986 250251 : hMetaData->nb_bits_tot = bit_pos_start;
1987 250251 : hMetaData->nb_ind_tot = next_ind_start;
1988 :
1989 250251 : return;
1990 : }
1991 :
1992 :
1993 : /*-------------------------------------------------------------------------
1994 : * ivas_qmetadata_encode_quasi_uniform()
1995 : *
1996 : * encode value using a quasi-uniform code of b or b + 1 bits, where b = floor(log2(alphabet_size))
1997 : *------------------------------------------------------------------------*/
1998 :
1999 1836475 : static void ivas_qmetadata_encode_quasi_uniform(
2000 : BSTR_ENC_HANDLE hMetaData,
2001 : const uint16_t value,
2002 : const uint16_t alphabet_size )
2003 : {
2004 : int16_t bits;
2005 : uint16_t tresh;
2006 : #ifdef DEBUGGING
2007 : assert( ( alphabet_size >= 1 ) );
2008 : assert( value < alphabet_size );
2009 : #endif
2010 :
2011 1836475 : bits = 30 - norm_l( alphabet_size ); /* bits = floor(log2(alphabet_size)) */
2012 1836475 : tresh = ( 1U << ( bits + 1 ) ) - alphabet_size;
2013 :
2014 1836475 : if ( value < tresh )
2015 : {
2016 1546760 : push_next_indice( hMetaData, value, bits );
2017 : }
2018 : else /* value >= tresh */
2019 : {
2020 289715 : push_next_indice( hMetaData, value + tresh, bits + 1 );
2021 : }
2022 :
2023 1836475 : return;
2024 : }
2025 :
2026 :
2027 : /*-----------------------------------------------------------------------*
2028 : * GR encoder function definitions
2029 : *-----------------------------------------------------------------------*/
2030 : /*-------------------------------------------------------------------------
2031 : * GR_bits_new()
2032 : *
2033 : *
2034 : *------------------------------------------------------------------------*/
2035 :
2036 : /*! r: number of bits using Golomb Rice code */
2037 134001 : static int16_t GR_bits_new(
2038 : uint16_t *data, /* i : data to encode with GR */
2039 : int16_t *no_symb, /* i : number of symbols for each component*/
2040 : const int16_t no_data, /* i : number of input data */
2041 : const int16_t GR_order, /* i : GR order to be used */
2042 : const int16_t check_two_orders, /* i : check also coding with GR_order-1 */
2043 : int16_t *real_GR_ord /* o : the GR order that has been used */
2044 : )
2045 : {
2046 134001 : int16_t nbits = 0, i;
2047 134001 : int16_t nbits1 = 0;
2048 : int16_t nb;
2049 :
2050 1181087 : for ( i = 0; i < no_data; i++ )
2051 : {
2052 1047086 : nb = ivas_qmetadata_encode_extended_gr_length( data[i], no_symb[i], GR_order );
2053 1047086 : nbits += nb;
2054 : }
2055 :
2056 134001 : if ( check_two_orders == 1 )
2057 : {
2058 1009546 : for ( i = 0; i < no_data; i++ )
2059 : {
2060 898782 : nb = ivas_qmetadata_encode_extended_gr_length( data[i], no_symb[i], GR_order - 1 );
2061 898782 : nbits1 += nb;
2062 : }
2063 :
2064 110764 : if ( nbits1 < nbits )
2065 : {
2066 68724 : nbits = nbits1 + 1;
2067 68724 : *real_GR_ord = GR_order - 1;
2068 : }
2069 : else
2070 : {
2071 42040 : nbits += 1;
2072 42040 : *real_GR_ord = GR_order;
2073 : }
2074 : }
2075 : else
2076 : {
2077 23237 : *real_GR_ord = GR_order;
2078 : }
2079 :
2080 134001 : return nbits;
2081 : }
2082 :
2083 :
2084 : /*-------------------------------------------------------------------------
2085 : * GR_bits_azimuth_context()
2086 : *
2087 : * Encoding azimuth indexes with GR code using context
2088 : *------------------------------------------------------------------------*/
2089 :
2090 : /*! r: numer of bits used for coding */
2091 18276 : static int16_t GR_bits_azimuth_context(
2092 : uint16_t *data_in, /* i : data to be encoded */
2093 : int16_t *no_symb, /* i : number of symbols for each component */
2094 : const int16_t no_data_in, /* i : number of input data */
2095 : const int16_t GR_order, /* i : GR order (GR_order or GR_order-1 are used ) */
2096 : const uint16_t *bits_dir, /* i : bits for encoding the direction for each TF tile */
2097 : int16_t *real_GR_ord, /* o : which GR order has been used */
2098 : int16_t *p_use_context /* o : flag telling if context has been used or not */
2099 : )
2100 : {
2101 : int16_t i, nbits, nbits1, use_context;
2102 : uint16_t cdata[MAX_PARAM_SPATIAL_SUBFRAMES];
2103 : uint16_t data[MAX_PARAM_SPATIAL_SUBFRAMES];
2104 : int16_t min_val, max_val;
2105 : int16_t real_GR_ord1;
2106 : int16_t no_symb_local[MAX_PARAM_SPATIAL_SUBFRAMES];
2107 18276 : int16_t no_data = 0;
2108 :
2109 74550 : for ( i = 0; i < no_data_in; i++ )
2110 : {
2111 56274 : if ( data_in[i] < MASA_NO_INDEX )
2112 : {
2113 56254 : no_symb_local[no_data] = no_symb[i];
2114 56254 : data[no_data++] = data_in[i];
2115 : }
2116 : }
2117 :
2118 18276 : if ( no_data == 0 )
2119 : {
2120 12 : *p_use_context = -3; /* corresponding to nothing to be written */
2121 12 : return 0;
2122 : }
2123 :
2124 18264 : nbits = 0;
2125 18264 : use_context = 0;
2126 :
2127 74518 : for ( i = 0; i < no_data; i++ )
2128 : {
2129 56254 : if ( ( bits_dir[i] <= 1 ) )
2130 : {
2131 0 : nbits += bits_dir[i];
2132 0 : use_context = 1;
2133 : }
2134 : else
2135 : {
2136 56254 : *real_GR_ord = GR_order - ( bits_dir[i] == 2 );
2137 56254 : nbits += ivas_qmetadata_encode_extended_gr_length( data[i], no_symb_local[i], *real_GR_ord );
2138 : }
2139 : }
2140 :
2141 18264 : real_GR_ord1 = 0;
2142 18264 : if ( use_context == 0 )
2143 : {
2144 18264 : nbits = GR_bits_new( data, no_symb_local, no_data, GR_order, 1, real_GR_ord );
2145 18264 : nbits1 = nbits;
2146 :
2147 18264 : min_val = data[0];
2148 56254 : for ( i = 1; i < no_data; i++ )
2149 : {
2150 37990 : if ( data[i] < min_val )
2151 : {
2152 5306 : min_val = data[i];
2153 : }
2154 : }
2155 74518 : for ( i = 0; i < no_data; i++ )
2156 : {
2157 56254 : cdata[i] = data[i] - min_val;
2158 : }
2159 :
2160 18264 : maximum_s( no_symb_local, no_data, &max_val );
2161 18264 : nbits1 = GR_bits_new( cdata, no_symb_local, no_data, GR_order - 1, 1, &real_GR_ord1 ) + ivas_qmetadata_encode_extended_gr_length( min_val, max_val, MASA_GR_ORD_AZ );
2162 :
2163 18264 : if ( nbits1 < nbits )
2164 : {
2165 6556 : nbits = nbits1 + 1;
2166 6556 : use_context = -2;
2167 6556 : *real_GR_ord = real_GR_ord1;
2168 : }
2169 : else
2170 : {
2171 11708 : nbits = nbits + 1;
2172 11708 : use_context = -1;
2173 : }
2174 : }
2175 :
2176 18264 : *p_use_context = use_context;
2177 :
2178 18264 : return nbits;
2179 : }
2180 :
2181 :
2182 : /*-------------------------------------------------------------------------
2183 : * mean_removed_GR_new()
2184 : *
2185 : * Golomb Rice encoding with mean removing
2186 : *------------------------------------------------------------------------*/
2187 :
2188 : /*! r: number of bits used */
2189 36465 : static int16_t mean_removed_GR_new(
2190 : const uint16_t *idx, /* i : data to encode */
2191 : const int16_t max_no_symb,
2192 : const int16_t len, /* i : number of data */
2193 : const int16_t adapt_GR, /* i : flag for telling to use or nor two GR order values */
2194 : int16_t *GR_ord, /* i/o: GR order */
2195 : uint16_t *p_av, /* o : average index */
2196 : uint16_t *mr_idx /* o : mean removed indexes */
2197 : )
2198 : {
2199 : int16_t av, i, nbits;
2200 : int16_t sh_idx[MASA_MAXIMUM_CODING_SUBBANDS];
2201 : int16_t max_ns[MASA_MAXIMUM_CODING_SUBBANDS];
2202 :
2203 36465 : av = (int16_t) ( 0.5f + sum_s( (const int16_t *) idx, len ) / (float) len );
2204 36465 : *p_av = av;
2205 456131 : for ( i = 0; i < len; i++ )
2206 : {
2207 419666 : max_ns[i] = 2 * ( max_no_symb );
2208 419666 : sh_idx[i] = idx[i] - av;
2209 : }
2210 :
2211 456131 : for ( i = 0; i < len; i++ )
2212 : {
2213 419666 : if ( sh_idx[i] < 0 )
2214 : {
2215 51432 : sh_idx[i] = -2 * sh_idx[i];
2216 : }
2217 368234 : else if ( sh_idx[i] > 0 )
2218 : {
2219 49742 : sh_idx[i] = sh_idx[i] * 2 - 1;
2220 : }
2221 : else
2222 : {
2223 318492 : sh_idx[i] = 0;
2224 : }
2225 419666 : mr_idx[i] = (uint16_t) sh_idx[i];
2226 : }
2227 :
2228 36465 : nbits = GR_bits_new( mr_idx, max_ns, len, *GR_ord, adapt_GR, GR_ord );
2229 :
2230 36465 : return nbits;
2231 : }
2232 :
2233 :
2234 : /*-------------------------------------------------------------------------
2235 : * ivas_qmetadata_encode_quasi_uniform_length()
2236 : *
2237 : *------------------------------------------------------------------------*/
2238 :
2239 8947132 : static int16_t ivas_qmetadata_encode_quasi_uniform_length(
2240 : const uint16_t value,
2241 : const uint16_t alphabet_size )
2242 : {
2243 : int16_t bits;
2244 : uint16_t tresh;
2245 : #ifdef DEBUGGING
2246 : assert( ( alphabet_size >= 1 ) );
2247 : assert( value < alphabet_size );
2248 : #endif
2249 :
2250 8947132 : bits = 30 - norm_l( alphabet_size ); /* bits = floor(log2(alphabet_size)) */
2251 8947132 : tresh = ( 1U << ( bits + 1 ) ) - alphabet_size;
2252 :
2253 8947132 : if ( value >= tresh )
2254 : {
2255 2253086 : bits++;
2256 : }
2257 :
2258 8947132 : return bits;
2259 : }
2260 :
2261 :
2262 : /*-------------------------------------------------------------------------
2263 : * ivas_qmetadata_entropy_encode_dir()
2264 : *
2265 : * Main function for entropy coding of the directions
2266 : *------------------------------------------------------------------------*/
2267 :
2268 257588 : static int16_t ivas_qmetadata_entropy_encode_dir(
2269 : BSTR_ENC_HANDLE hMetaData,
2270 : IVAS_QDIRECTION *q_direction,
2271 : const uint16_t diffuseness_index_max_ec_frame,
2272 : const int16_t nbands,
2273 : const int16_t start_band,
2274 : const int16_t direction_bits_raw,
2275 : int16_t max_bits,
2276 : const int16_t hrmasa_flag )
2277 : {
2278 : uint16_t diff_idx_min;
2279 : int16_t i, j;
2280 : int16_t nblocks;
2281 :
2282 : float avg_direction_vector[3], direction_vector[3], avg_azimuth, avg_elevation;
2283 : int16_t avg_azimuth_alphabet, avg_elevation_alphabet;
2284 : uint16_t avg_azimuth_index, avg_elevation_index;
2285 : int16_t avg_elevation_index_projected;
2286 : int16_t avg_azimuth_index_projected;
2287 : uint16_t avg_elevation_index_initial, avg_elevation_offset;
2288 : uint16_t avg_azimuth_index_initial, avg_azimuth_offset;
2289 : int16_t elevation_bits_ec_best, azimuth_bits_ec_best;
2290 :
2291 257588 : int16_t gr_param_elevation_best = 0, avg_elevation_index_best = 0;
2292 : uint16_t dist_elevation_indexes_best[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2293 : int16_t gr_param_azimuth_best, avg_azimuth_index_best;
2294 : uint16_t dist_azimuth_indexes_best[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2295 :
2296 : uint16_t idx, dist_count;
2297 : int16_t direction_bits_ec;
2298 :
2299 : uint16_t dist_elevation_indexes[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2300 : uint16_t dist_elevation_alphabets[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2301 : uint16_t dist_azimuth_indexes[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2302 : uint16_t dist_azimuth_alphabets[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2303 257588 : int16_t all_zero_dist_elevation_indexes = 1, all_zero_dist_azimuth_indexes = 1;
2304 : int16_t gr_param_elevation, gr_size_elevation, egr_size_elevation, gr_param_azimuth, gr_size_azimuth;
2305 : int16_t egr_size_azimuth, elevation_bits_ec, azimuth_bits_ec;
2306 :
2307 : float abs_theta;
2308 : float theta_cb[MAX_NO_THETA];
2309 : int16_t sign_th, no_th;
2310 257588 : int16_t avg_azimuth_index_upd = 0, use_adapt_avg;
2311 257588 : int16_t make_gain = 0;
2312 257588 : int16_t bits_gained = 0;
2313 257588 : nblocks = q_direction->cfg.nblocks;
2314 :
2315 : /* estimate the number of bits for entropy coding of the direction values */
2316 257588 : direction_bits_ec = 0;
2317 257588 : diff_idx_min = DIRAC_DIFFUSE_LEVELS;
2318 257588 : idx = 0;
2319 257588 : dist_count = 0;
2320 257588 : set_zero( avg_direction_vector, 3 );
2321 :
2322 1373995 : for ( i = start_band; i < nbands; i++ )
2323 : {
2324 1116407 : if ( hrmasa_flag )
2325 : {
2326 0 : diff_idx_min = 0; // min( q_direction->band_data[i].energy_ratio_index_mod[0]>>1, diff_idx_min );
2327 : }
2328 : else
2329 : {
2330 1116407 : diff_idx_min = min( q_direction->band_data[i].energy_ratio_index_mod[0], diff_idx_min );
2331 : }
2332 :
2333 1116407 : if ( q_direction->band_data[i].energy_ratio_index_mod[0] > diffuseness_index_max_ec_frame )
2334 : {
2335 : /* estimate the raw part */
2336 287979 : if ( q_direction->not_in_2D > 0 )
2337 : {
2338 1102739 : for ( j = 0; j < nblocks; j++ )
2339 : {
2340 852139 : direction_bits_ec += q_direction->band_data[i].bits_sph_idx[j];
2341 : }
2342 : }
2343 : else
2344 : {
2345 148102 : for ( j = 0; j < nblocks; j++ )
2346 : {
2347 110723 : direction_bits_ec += ivas_qmetadata_encode_quasi_uniform_length( q_direction->band_data[i].azimuth_index[j], q_direction->band_data[i].azimuth_m_alphabet[j] );
2348 : }
2349 : }
2350 : }
2351 : else
2352 : {
2353 828428 : dist_count += nblocks;
2354 :
2355 3752401 : for ( j = 0; j < nblocks; j++ )
2356 : {
2357 : /*compute the average direction */
2358 2923973 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[i].azimuth[j], q_direction->band_data[i].elevation[j], direction_vector );
2359 2923973 : v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
2360 : }
2361 : }
2362 : }
2363 :
2364 : /* quantize average elevation and azimuth angles using the best angle spacing and equatorial precision */
2365 257588 : ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &avg_azimuth, &avg_elevation );
2366 :
2367 257588 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2368 : {
2369 15230 : avg_elevation_alphabet = no_theta_masa[bits_direction_masa[diff_idx_min] - 3];
2370 15230 : avg_azimuth_alphabet = no_phi_masa[bits_direction_masa[diff_idx_min] - 1][0]; /* average azimuth is quantized on the equatorial plane */
2371 : }
2372 : else
2373 : {
2374 242358 : avg_elevation_alphabet = no_theta_masa[bits_direction_masa[diff_idx_min] - 3] * 2 - 1;
2375 242358 : avg_azimuth_alphabet = no_phi_masa[bits_direction_masa[diff_idx_min] - 1][0]; /* average azimuth is quantized on the equatorial plane */
2376 : }
2377 :
2378 257588 : no_th = no_theta_masa[bits_direction_masa[diff_idx_min] - 3];
2379 :
2380 2366504 : for ( i = 0; i < no_th; i++ )
2381 : {
2382 2108916 : theta_cb[i] = i * delta_theta_masa[bits_direction_masa[diff_idx_min] - 3];
2383 : }
2384 :
2385 257588 : if ( theta_cb[i - 1] > 90 )
2386 : {
2387 144300 : theta_cb[i - 1] = 90;
2388 : }
2389 :
2390 257588 : if ( avg_elevation < 0 )
2391 : {
2392 104212 : abs_theta = -avg_elevation;
2393 104212 : sign_th = -1;
2394 : }
2395 : else
2396 : {
2397 153376 : abs_theta = avg_elevation;
2398 153376 : sign_th = 1;
2399 : }
2400 :
2401 257588 : avg_elevation_index = squant( abs_theta, &avg_elevation, theta_cb, no_th );
2402 :
2403 257588 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2404 : {
2405 15230 : assert( avg_elevation >= 0 );
2406 : }
2407 : else
2408 : {
2409 242358 : if ( sign_th < 0 )
2410 : {
2411 104212 : avg_elevation_index = ( avg_elevation_alphabet >> 1 ) - avg_elevation_index;
2412 : }
2413 : else
2414 : {
2415 138146 : avg_elevation_index += ( avg_elevation_alphabet >> 1 );
2416 : }
2417 242358 : avg_elevation *= sign_th;
2418 : }
2419 :
2420 257588 : avg_azimuth_index = (uint16_t) ( quantize_phi( avg_azimuth + 180, 0, &avg_azimuth, avg_azimuth_alphabet ) );
2421 :
2422 : /* Elevation only if not 2D */
2423 257588 : if ( q_direction->not_in_2D > 0 )
2424 : {
2425 219975 : avg_elevation_index_initial = avg_elevation_index;
2426 219975 : elevation_bits_ec_best = MAX16B;
2427 219975 : avg_elevation_index_best = -1; /* out of range value */
2428 219975 : gr_param_elevation_best = -1; /* out of range value */
2429 :
2430 879900 : for ( avg_elevation_offset = 0; avg_elevation_offset < q_direction->cfg.search_effort; avg_elevation_offset++ )
2431 : {
2432 659925 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2433 : {
2434 12978 : avg_elevation_index = avg_elevation_index_initial + avg_elevation_offset;
2435 : }
2436 : else
2437 : {
2438 646947 : avg_elevation_index = (uint16_t) ( avg_elevation_index_initial + ivas_qmetadata_dereorder_generic( avg_elevation_offset ) );
2439 : }
2440 659925 : avg_elevation_index = (uint16_t) ( ( avg_elevation_index + avg_elevation_alphabet ) % avg_elevation_alphabet );
2441 :
2442 659925 : all_zero_dist_elevation_indexes = 1;
2443 659925 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2444 : {
2445 12978 : elevation_bits_ec = ivas_qmetadata_encode_quasi_uniform_length( avg_elevation_index, avg_elevation_alphabet );
2446 : }
2447 : else
2448 : {
2449 646947 : elevation_bits_ec = ivas_qmetadata_encode_quasi_uniform_length( ivas_qmetadata_reorder_generic( avg_elevation_index - ( avg_elevation_alphabet >> 1 ) ), avg_elevation_alphabet );
2450 : }
2451 659925 : idx = 0;
2452 3520611 : for ( i = start_band; i < nbands; i++ )
2453 : {
2454 2860686 : if ( q_direction->band_data[i].energy_ratio_index_mod[0] <= diffuseness_index_max_ec_frame )
2455 : {
2456 9740811 : for ( j = 0; j < nblocks; j++ )
2457 : {
2458 : /* project the quantized average elevation to the same grid as the current sample */
2459 7631925 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2460 : {
2461 77481 : avg_elevation_index_projected = ivas_chan_project_elevation_index( avg_elevation_index, avg_elevation_alphabet, q_direction->band_data[i].elevation_m_alphabet[j] );
2462 : }
2463 : else
2464 : {
2465 7554444 : avg_elevation_index_projected = ivas_dirac_project_elevation_index( avg_elevation_index, avg_elevation_alphabet, q_direction->band_data[i].elevation_m_alphabet[j] );
2466 : }
2467 : #ifdef DEBUGGING
2468 : assert( ( 0 <= avg_elevation_index_projected ) && ( avg_elevation_index_projected < q_direction->band_data[i].elevation_m_alphabet[j] ) );
2469 : #endif
2470 :
2471 7631925 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2472 : {
2473 77481 : if ( q_direction->band_data[i].elevation_index[j] - avg_elevation_index_projected > 0 )
2474 : {
2475 3888 : dist_elevation_indexes[idx] = 2 * ( q_direction->band_data[i].elevation_index[j] - avg_elevation_index_projected ) - 1;
2476 : }
2477 : else
2478 : {
2479 73593 : dist_elevation_indexes[idx] = -2 * ( q_direction->band_data[i].elevation_index[j] - avg_elevation_index_projected );
2480 : }
2481 : }
2482 : else
2483 : {
2484 7554444 : dist_elevation_indexes[idx] = ivas_qmetadata_reorder_elevation_index( q_direction->band_data[i].elevation_index[j], avg_elevation_index_projected, q_direction->band_data[i].elevation_m_alphabet[j] );
2485 : }
2486 7631925 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2487 : {
2488 77481 : dist_elevation_alphabets[idx] = 2 * q_direction->band_data[i].elevation_m_alphabet[j] - 1;
2489 : }
2490 : else
2491 : {
2492 7554444 : dist_elevation_alphabets[idx] = q_direction->band_data[i].elevation_m_alphabet[j];
2493 : }
2494 :
2495 7631925 : if ( dist_elevation_indexes[idx] != 0 )
2496 : {
2497 4898345 : all_zero_dist_elevation_indexes = 0;
2498 : }
2499 7631925 : idx++;
2500 : }
2501 : }
2502 : }
2503 :
2504 659925 : if ( all_zero_dist_elevation_indexes )
2505 : {
2506 57885 : egr_size_elevation = 0;
2507 57885 : gr_param_elevation = 4;
2508 : }
2509 : else
2510 : {
2511 602040 : gr_param_elevation = ivas_qmetadata_get_optimal_gr_param( dist_elevation_indexes, idx, 4, &gr_size_elevation );
2512 602040 : egr_size_elevation = 0;
2513 8118758 : for ( i = 0; i < idx; i++ )
2514 : {
2515 7516718 : egr_size_elevation += ivas_qmetadata_encode_extended_gr_length( dist_elevation_indexes[i], dist_elevation_alphabets[i], gr_param_elevation );
2516 : }
2517 : #ifdef DEBUGGING
2518 : assert( egr_size_elevation <= gr_size_elevation );
2519 : #endif
2520 : }
2521 659925 : elevation_bits_ec += ivas_qmetadata_encode_quasi_uniform_length( gr_param_elevation, 4 + 1 ) + egr_size_elevation;
2522 :
2523 659925 : if ( elevation_bits_ec < elevation_bits_ec_best )
2524 : {
2525 295412 : elevation_bits_ec_best = elevation_bits_ec;
2526 295412 : avg_elevation_index_best = avg_elevation_index;
2527 295412 : gr_param_elevation_best = gr_param_elevation;
2528 3993150 : for ( idx = 0; idx < dist_count; idx++ )
2529 : {
2530 3697738 : dist_elevation_indexes_best[idx] = dist_elevation_indexes[idx];
2531 : }
2532 : }
2533 : }
2534 :
2535 219975 : direction_bits_ec += elevation_bits_ec_best;
2536 : }
2537 :
2538 : /*Azimuth*/
2539 257588 : use_adapt_avg = 0;
2540 257588 : if ( ( nbands - start_band >= 5 ) && ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( nblocks > 1 ) )
2541 : {
2542 3525 : use_adapt_avg = calc_var_azi( q_direction, diffuseness_index_max_ec_frame, avg_azimuth - 180, &avg_azimuth );
2543 3525 : avg_azimuth_index = (uint16_t) ( quantize_phi( avg_azimuth + 180, 0, &avg_azimuth, avg_azimuth_alphabet ) );
2544 : }
2545 257588 : avg_azimuth_index_initial = avg_azimuth_index; /* avg_azimuth_index;*/
2546 257588 : azimuth_bits_ec_best = MAX16B;
2547 257588 : avg_azimuth_index_best = -1; /* out of range value */
2548 257588 : gr_param_azimuth_best = -1; /* out of range value */
2549 :
2550 1030352 : for ( avg_azimuth_offset = 0; avg_azimuth_offset < q_direction->cfg.search_effort; avg_azimuth_offset++ )
2551 : {
2552 772764 : set_zero( avg_direction_vector, 3 );
2553 772764 : avg_azimuth_index = (uint16_t) ( avg_azimuth_index_initial + ivas_qmetadata_dereorder_generic( avg_azimuth_offset ) );
2554 772764 : avg_azimuth_index = (uint16_t) ( ( avg_azimuth_index + avg_azimuth_alphabet ) % avg_azimuth_alphabet );
2555 772764 : all_zero_dist_azimuth_indexes = 1;
2556 772764 : azimuth_bits_ec = ivas_qmetadata_encode_quasi_uniform_length( ivas_qmetadata_reorder_generic( avg_azimuth_index - ( avg_azimuth_alphabet >> 1 ) ), avg_azimuth_alphabet );
2557 :
2558 772764 : idx = 0;
2559 4121985 : for ( i = start_band; i < nbands; i++ )
2560 : {
2561 3349221 : if ( q_direction->band_data[i].energy_ratio_index_mod[0] <= diffuseness_index_max_ec_frame )
2562 : {
2563 11257203 : for ( j = 0; j < nblocks; j++ )
2564 : {
2565 :
2566 8771919 : if ( ( idx > MASA_LIMIT_IDX_AVG_AZI ) && ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( use_adapt_avg == 1 ) )
2567 : {
2568 52803 : avg_azimuth_index_projected = ivas_dirac_project_azimuth_index( avg_azimuth_index_upd, avg_azimuth_alphabet, q_direction->band_data[i].azimuth_m_alphabet[j] );
2569 : }
2570 : else
2571 : {
2572 8719116 : if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( use_adapt_avg == 1 ) )
2573 : {
2574 32721 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[i].azimuth[j], q_direction->band_data[i].elevation[j], direction_vector );
2575 32721 : if ( idx < 4 )
2576 : {
2577 27024 : v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
2578 : }
2579 : }
2580 : /* project the quantized average azimuth angle to the same grid as the current sample */
2581 8719116 : avg_azimuth_index_projected = ivas_dirac_project_azimuth_index( avg_azimuth_index, avg_azimuth_alphabet, q_direction->band_data[i].azimuth_m_alphabet[j] );
2582 : }
2583 : #ifdef DEBUGGING
2584 : assert( ( 0 <= avg_azimuth_index_projected ) && ( avg_azimuth_index_projected < q_direction->band_data[i].azimuth_m_alphabet[j] ) );
2585 : #endif
2586 8771919 : dist_azimuth_indexes[idx] = ivas_qmetadata_reorder_azimuth_index( ivas_qmetadata_dereorder_generic( q_direction->band_data[i].azimuth_index[j] ) + ( q_direction->band_data[i].azimuth_m_alphabet[j] >> 1 ), avg_azimuth_index_projected, q_direction->band_data[i].azimuth_m_alphabet[j] );
2587 8771919 : dist_azimuth_alphabets[idx] = q_direction->band_data[i].azimuth_m_alphabet[j];
2588 :
2589 8771919 : if ( dist_azimuth_indexes[idx] != 0 )
2590 : {
2591 6259136 : all_zero_dist_azimuth_indexes = 0;
2592 : }
2593 :
2594 8771919 : if ( ( idx >= MASA_LIMIT_IDX_AVG_AZI ) && ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( use_adapt_avg == 1 ) )
2595 : {
2596 58500 : if ( idx % nblocks == 0 )
2597 : {
2598 14625 : v_multc( avg_direction_vector, 0.5f, avg_direction_vector, 3 );
2599 : }
2600 :
2601 : /*compute the average direction per already coded subband */
2602 58500 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[i].azimuth[j], q_direction->band_data[i].elevation[j], direction_vector );
2603 58500 : v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
2604 58500 : ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &avg_azimuth, &avg_elevation );
2605 58500 : avg_azimuth_index_upd = quantize_phi( avg_azimuth + 180, 0, &avg_azimuth, avg_azimuth_alphabet );
2606 : }
2607 8771919 : idx++;
2608 : }
2609 : }
2610 : }
2611 :
2612 772764 : if ( all_zero_dist_azimuth_indexes )
2613 : {
2614 68496 : egr_size_azimuth = 0;
2615 68496 : gr_param_azimuth = 5;
2616 : }
2617 : else
2618 : {
2619 : /* estimate the ExtendedGR part for azimuth */
2620 704268 : gr_param_azimuth = ivas_qmetadata_get_optimal_gr_param( dist_azimuth_indexes, idx, 5, &gr_size_azimuth );
2621 704268 : egr_size_azimuth = 0;
2622 9343830 : for ( i = 0; i < idx; i++ )
2623 : {
2624 8639562 : egr_size_azimuth += ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes[i], dist_azimuth_alphabets[i], gr_param_azimuth );
2625 : }
2626 : #ifdef DEBUGGING
2627 : assert( egr_size_azimuth <= gr_size_azimuth );
2628 : #endif
2629 : }
2630 :
2631 772764 : azimuth_bits_ec += ivas_qmetadata_encode_quasi_uniform_length( gr_param_azimuth, 5 + 1 ) + egr_size_azimuth;
2632 :
2633 772764 : if ( azimuth_bits_ec < azimuth_bits_ec_best )
2634 : {
2635 367655 : azimuth_bits_ec_best = azimuth_bits_ec;
2636 367655 : avg_azimuth_index_best = avg_azimuth_index;
2637 367655 : gr_param_azimuth_best = gr_param_azimuth;
2638 :
2639 4787550 : for ( idx = 0; idx < dist_count; idx++ )
2640 : {
2641 4419895 : dist_azimuth_indexes_best[idx] = dist_azimuth_indexes[idx];
2642 : }
2643 : }
2644 : }
2645 :
2646 257588 : if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( dist_count > 4 ) && ( gr_param_azimuth_best != 5 ) && ( nblocks > 1 ) )
2647 : {
2648 2913 : azimuth_bits_ec_best += 1;
2649 : }
2650 :
2651 257588 : direction_bits_ec += azimuth_bits_ec_best;
2652 :
2653 : /*Decision raw or EC*/
2654 : /* one bit is used to indicate whether the direction values are entropy coded or coded raw */
2655 257588 : if ( direction_bits_ec < direction_bits_raw ) /* entropy coding is better */
2656 : {
2657 :
2658 : /* encode the raw part first */
2659 845269 : for ( i = start_band; i < nbands; i++ )
2660 : {
2661 706081 : if ( q_direction->band_data[i].energy_ratio_index_mod[0] > diffuseness_index_max_ec_frame )
2662 : {
2663 173043 : if ( q_direction->not_in_2D > 0 )
2664 : {
2665 727300 : for ( j = 0; j < nblocks; j++ )
2666 : {
2667 573692 : push_next_indice( hMetaData, q_direction->band_data[i].spherical_index[j], q_direction->band_data[i].bits_sph_idx[j] );
2668 : }
2669 : }
2670 : else
2671 : {
2672 89480 : for ( j = 0; j < nblocks; j++ )
2673 : {
2674 70045 : ivas_qmetadata_encode_quasi_uniform( hMetaData, q_direction->band_data[i].azimuth_index[j], q_direction->band_data[i].azimuth_m_alphabet[j] );
2675 : }
2676 : }
2677 : }
2678 : }
2679 :
2680 139188 : if ( nbands > 1 && direction_bits_ec - max_bits > 0 && direction_bits_ec - max_bits < nblocks * nbands )
2681 : {
2682 2163 : make_gain = 1;
2683 : }
2684 :
2685 139188 : if ( q_direction->not_in_2D > 0 )
2686 : {
2687 : /* encode the ExtendedGR part for elevation */
2688 117254 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2689 : {
2690 2564 : ivas_qmetadata_encode_quasi_uniform( hMetaData, avg_elevation_index_best, avg_elevation_alphabet );
2691 : }
2692 : else
2693 : {
2694 114690 : ivas_qmetadata_encode_quasi_uniform( hMetaData, ivas_qmetadata_reorder_generic( avg_elevation_index_best - ( avg_elevation_alphabet >> 1 ) ), avg_elevation_alphabet );
2695 : }
2696 :
2697 117254 : ivas_qmetadata_encode_quasi_uniform( hMetaData, gr_param_elevation_best, 4 + 1 );
2698 :
2699 117254 : if ( gr_param_elevation_best != 4 ) /* not all zero */
2700 : {
2701 1663306 : for ( idx = 0; idx < dist_count; idx++ )
2702 : {
2703 1568123 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_elevation_indexes_best[idx], dist_elevation_alphabets[idx], gr_param_elevation_best );
2704 : }
2705 : }
2706 : }
2707 :
2708 : /* encode the ExtendedGR part for azimuth */
2709 139188 : ivas_qmetadata_encode_quasi_uniform( hMetaData, ivas_qmetadata_reorder_generic( avg_azimuth_index_best - ( avg_azimuth_alphabet >> 1 ) ), avg_azimuth_alphabet );
2710 :
2711 139188 : ivas_qmetadata_encode_quasi_uniform( hMetaData, gr_param_azimuth_best, 5 + 1 );
2712 :
2713 139188 : if ( gr_param_azimuth_best != 5 ) /* not all zero */
2714 : {
2715 574531 : for ( idx = 0; idx < min( nblocks, dist_count ); idx++ )
2716 : {
2717 455339 : if ( make_gain == 1 && bits_gained < direction_bits_ec - max_bits && dist_azimuth_alphabets[idx] > 40 )
2718 : {
2719 1501 : if ( dist_azimuth_indexes_best[idx] > 1 )
2720 : {
2721 1002 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx] - 2, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2722 1002 : bits_gained += ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best ) -
2723 1002 : ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx] - 2, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2724 : }
2725 499 : else if ( dist_azimuth_indexes_best[idx] == 1 )
2726 : {
2727 226 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx] - 1, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2728 226 : bits_gained += ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best ) -
2729 226 : ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx] - 1, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2730 : }
2731 : else
2732 : {
2733 273 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2734 : }
2735 : }
2736 : else
2737 : {
2738 453838 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2739 : }
2740 : }
2741 :
2742 119192 : if ( dist_count > nblocks )
2743 : {
2744 92480 : if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( nblocks > 1 ) )
2745 : {
2746 2502 : push_next_indice( hMetaData, use_adapt_avg, 1 );
2747 : }
2748 1475256 : for ( idx = nblocks; idx < dist_count; idx++ )
2749 : {
2750 1382776 : if ( make_gain == 1 && bits_gained < direction_bits_ec - max_bits && dist_azimuth_alphabets[idx] > 40 )
2751 : {
2752 10054 : if ( dist_azimuth_indexes_best[idx] > 1 )
2753 : {
2754 7203 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx] - 2, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2755 7203 : bits_gained += ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best ) -
2756 7203 : ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx] - 2, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2757 : }
2758 2851 : else if ( dist_azimuth_indexes_best[idx] == 1 )
2759 : {
2760 1232 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx] - 1, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2761 1232 : bits_gained += ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best ) -
2762 1232 : ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx] - 1, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2763 : }
2764 : else
2765 : {
2766 1619 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2767 : }
2768 : }
2769 : else
2770 : {
2771 1372722 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2772 : }
2773 : }
2774 : }
2775 : }
2776 :
2777 139188 : direction_bits_ec -= bits_gained;
2778 : }
2779 : else
2780 : {
2781 118400 : direction_bits_ec = -1;
2782 : }
2783 :
2784 257588 : return direction_bits_ec;
2785 : }
2786 :
2787 :
2788 : /*-------------------------------------------------------------------------
2789 : * ivas_qmetadata_raw_encode_dir()
2790 : *
2791 : * Main function for raw coding of the directions (writing and bit estimation)
2792 : *------------------------------------------------------------------------*/
2793 :
2794 375988 : static int16_t ivas_qmetadata_raw_encode_dir(
2795 : BSTR_ENC_HANDLE hMetaData,
2796 : IVAS_QDIRECTION *q_direction,
2797 : const int16_t nbands,
2798 : const int16_t start_band )
2799 : {
2800 : int16_t i, j;
2801 : int16_t direction_bits_raw;
2802 375988 : int16_t start_bits = 0; /*To avoid compiler warning*/
2803 :
2804 375988 : direction_bits_raw = 0;
2805 375988 : if ( hMetaData != NULL )
2806 : {
2807 118400 : start_bits = hMetaData->nb_bits_tot;
2808 : }
2809 :
2810 375988 : if ( q_direction->not_in_2D > 0 )
2811 : {
2812 1629572 : for ( i = start_band; i < nbands; i++ )
2813 : {
2814 1306876 : if ( hMetaData != NULL )
2815 : {
2816 1506008 : for ( j = 0; j < q_direction->cfg.nblocks; j++ )
2817 : {
2818 1152694 : push_next_indice( hMetaData, q_direction->band_data[i].spherical_index[j], q_direction->band_data[i].bits_sph_idx[j] );
2819 : }
2820 : }
2821 : else
2822 : {
2823 953562 : direction_bits_raw += q_direction->cfg.nblocks * q_direction->band_data[i].bits_sph_idx[0];
2824 : }
2825 : }
2826 : }
2827 : else
2828 : {
2829 273149 : for ( i = start_band; i < nbands; i++ )
2830 : {
2831 851587 : for ( j = 0; j < q_direction->cfg.nblocks; j++ )
2832 : {
2833 631730 : if ( hMetaData != NULL )
2834 : {
2835 141009 : ivas_qmetadata_encode_quasi_uniform( hMetaData, q_direction->band_data[i].azimuth_index[j], q_direction->band_data[i].azimuth_m_alphabet[j] );
2836 : }
2837 : else
2838 : {
2839 490721 : direction_bits_raw += ivas_qmetadata_encode_quasi_uniform_length(
2840 490721 : q_direction->band_data[i].azimuth_index[j], q_direction->band_data[i].azimuth_m_alphabet[j] );
2841 : }
2842 : }
2843 : }
2844 : }
2845 :
2846 375988 : if ( hMetaData != NULL )
2847 : {
2848 118400 : direction_bits_raw = hMetaData->nb_bits_tot - start_bits;
2849 : }
2850 :
2851 375988 : return direction_bits_raw;
2852 : }
2853 :
2854 :
2855 : /*-------------------------------------------------------------------------
2856 : * ivas_qmetadata_get_optimal_gr_param()
2857 : *
2858 : *
2859 : *------------------------------------------------------------------------*/
2860 :
2861 1306308 : static int16_t ivas_qmetadata_get_optimal_gr_param(
2862 : uint16_t *unsigned_data,
2863 : const int16_t count,
2864 : const int16_t gr_param_count,
2865 : int16_t *opt_gr_size )
2866 : {
2867 : int16_t opt_bits, bits, idx;
2868 : int16_t opt_gr_param;
2869 : int16_t p;
2870 :
2871 1306308 : opt_bits = MAX16B;
2872 1306308 : opt_gr_param = -1;
2873 :
2874 7235808 : for ( p = 0; p < gr_param_count; p++ )
2875 : {
2876 5929500 : bits = count * ( 1 + p ); /* terminating zero bit and the lsb bits */
2877 79194182 : for ( idx = 0; idx < count; idx++ )
2878 : {
2879 73264682 : bits += unsigned_data[idx] >> p; /* leading one bits */
2880 : }
2881 :
2882 5929500 : if ( bits < opt_bits )
2883 : {
2884 2042736 : opt_gr_param = p;
2885 2042736 : opt_bits = bits;
2886 : }
2887 : }
2888 :
2889 1306308 : *opt_gr_size = opt_bits;
2890 :
2891 1306308 : return opt_gr_param;
2892 : }
2893 :
2894 :
2895 : /*-------------------------------------------------------------------------
2896 : * ivas_qmetadata_encode_extended_gr_length()
2897 : *
2898 : *
2899 : *------------------------------------------------------------------------*/
2900 :
2901 : int16_t
2902 20117021 : ivas_qmetadata_encode_extended_gr_length(
2903 : const uint16_t value,
2904 : const uint16_t alphabet_size,
2905 : const int16_t gr_param )
2906 : {
2907 : uint16_t msb_alphabet_size;
2908 : int16_t bits;
2909 : uint16_t msb, lsb;
2910 :
2911 : #ifdef DEBUGGING
2912 : assert( alphabet_size >= 1 );
2913 : assert( value < alphabet_size );
2914 : assert( ( gr_param >= 0 ) && ( gr_param <= 15 ) );
2915 : #endif
2916 :
2917 20117021 : msb_alphabet_size = ( alphabet_size + ( 1U << gr_param ) - 1 ) >> gr_param;
2918 :
2919 20117021 : if ( msb_alphabet_size <= 3 )
2920 : {
2921 : /* EncodeQuasiUniform is always equal or better than Limited GR with up to 3 msb values */
2922 4350438 : bits = ivas_qmetadata_encode_quasi_uniform_length( value, alphabet_size );
2923 : }
2924 : else
2925 : {
2926 15766583 : msb = value >> gr_param;
2927 :
2928 15766583 : bits = msb; /* leading one bits */
2929 15766583 : if ( msb < msb_alphabet_size - 1 )
2930 : {
2931 15568616 : bits += 1 + gr_param; /* terminating zero bit, if not the largest msb (Limited GR), and the lsb bits */
2932 : }
2933 : else
2934 : {
2935 197967 : lsb = value & ( ( 1U << gr_param ) - 1 );
2936 197967 : bits += ivas_qmetadata_encode_quasi_uniform_length( lsb, alphabet_size - ( ( msb_alphabet_size - 1 ) << gr_param ) );
2937 : }
2938 : }
2939 :
2940 20117021 : return bits;
2941 : }
2942 :
2943 :
2944 : /*-------------------------------------------------------------------------
2945 : * ivas_qmetadata_reorder_elevation_index()
2946 : *
2947 : *
2948 : *------------------------------------------------------------------------*/
2949 :
2950 10642755 : static int16_t ivas_qmetadata_reorder_elevation_index(
2951 : const int16_t elevation_index,
2952 : const int16_t avg_elevation_index,
2953 : const int16_t elevation_alphabet )
2954 : {
2955 : int16_t elevation_alphabet_half;
2956 : int16_t elevation_index_reordered;
2957 : #ifdef DEBUGGING
2958 : assert( ( elevation_alphabet & 0x01 ) == 1 ); /* elevation_alphabet has the form 2 * n_points + 1 */
2959 : #endif
2960 :
2961 10642755 : elevation_alphabet_half = elevation_alphabet >> 1;
2962 10642755 : elevation_index_reordered = elevation_index - avg_elevation_index;
2963 :
2964 : /* reduce the distance for the index elevation to the range [-elevation_alphabet_half, elevation_alphabet_half] */
2965 10642755 : if ( elevation_index_reordered < -elevation_alphabet_half )
2966 : {
2967 111914 : elevation_index_reordered += elevation_alphabet;
2968 : }
2969 10530841 : else if ( elevation_index_reordered > elevation_alphabet_half )
2970 : {
2971 193381 : elevation_index_reordered -= elevation_alphabet;
2972 : }
2973 :
2974 : /* fold reduced signed distance value for converting to unsigned */
2975 10642755 : elevation_index_reordered = ivas_qmetadata_reorder_generic( elevation_index_reordered );
2976 : #ifdef DEBUGGING
2977 : assert( ( 0 <= elevation_index_reordered ) && ( elevation_index_reordered < elevation_alphabet ) );
2978 : #endif
2979 :
2980 10642755 : return elevation_index_reordered;
2981 : }
2982 :
2983 :
2984 : /*-------------------------------------------------------------------------
2985 : * ivas_qmetadata_reorder_azimuth_index()
2986 : *
2987 : *
2988 : *------------------------------------------------------------------------*/
2989 :
2990 8771919 : static int16_t ivas_qmetadata_reorder_azimuth_index(
2991 : const int16_t azimuth_index,
2992 : const int16_t avg_azimuth_index,
2993 : const int16_t azimuth_alphabet )
2994 : {
2995 : int16_t azimuth_alphabet_half;
2996 : int16_t azimuth_index_reordered;
2997 :
2998 8771919 : azimuth_index_reordered = azimuth_index - avg_azimuth_index;
2999 :
3000 8771919 : if ( ( azimuth_alphabet != 1 ) && ( ( azimuth_alphabet & 0x01 ) == 1 ) )
3001 : {
3002 3088311 : return ( ivas_qmetadata_reorder_elevation_index( azimuth_index, avg_azimuth_index, azimuth_alphabet ) );
3003 : }
3004 5683608 : else if ( azimuth_alphabet != 1 )
3005 : {
3006 5678781 : azimuth_alphabet_half = azimuth_alphabet >> 1;
3007 : /* reduce the distance for the index azimuth to the range [-azimuth_alphabet_half, azimuth_alphabet_half - 1] */
3008 5678781 : if ( azimuth_index_reordered < -azimuth_alphabet_half )
3009 : {
3010 109054 : azimuth_index_reordered += azimuth_alphabet;
3011 : }
3012 5569727 : else if ( azimuth_index_reordered > azimuth_alphabet_half - 1 )
3013 : {
3014 408150 : azimuth_index_reordered -= azimuth_alphabet;
3015 : }
3016 : /* fold reduced signed distance value for converting to unsigned */
3017 5678781 : azimuth_index_reordered = ivas_qmetadata_reorder_generic( azimuth_index_reordered );
3018 : #ifdef DEBUGGING
3019 : assert( ( 0 <= azimuth_index_reordered ) && ( azimuth_index_reordered < azimuth_alphabet ) );
3020 : #endif
3021 : }
3022 : else
3023 : {
3024 : /* for North and South poles, a single azimuth direction exists */
3025 : #ifdef DEBUGGING
3026 : assert( ( azimuth_index == 0 ) || ( azimuth_index == MASA_NO_INDEX ) );
3027 : #endif
3028 4827 : azimuth_index_reordered = 0;
3029 :
3030 : #ifdef DEBUGGING
3031 : assert( avg_azimuth_index == 0 );
3032 : #endif
3033 : }
3034 :
3035 5683608 : return azimuth_index_reordered;
3036 : }
3037 :
3038 :
3039 : /*-------------------------------------------------------------------------
3040 : * ivas_qmetadata_encode_extended_gr()
3041 : *
3042 : *
3043 : *------------------------------------------------------------------------*/
3044 :
3045 4588299 : void ivas_qmetadata_encode_extended_gr(
3046 : BSTR_ENC_HANDLE hMetaData,
3047 : const uint16_t value,
3048 : const uint16_t alphabet_size,
3049 : const int16_t gr_param )
3050 : {
3051 : uint16_t msb_alphabet_size;
3052 : uint16_t msb, lsb, cnt;
3053 :
3054 : #ifdef DEBUGGING
3055 : assert( alphabet_size >= 1 );
3056 : assert( value < alphabet_size );
3057 : assert( ( gr_param >= 0 ) && ( gr_param <= 31 ) );
3058 : #endif
3059 :
3060 4588299 : msb_alphabet_size = ( alphabet_size + ( 1U << gr_param ) - 1 ) >> gr_param;
3061 :
3062 4588299 : if ( msb_alphabet_size <= 3 )
3063 : {
3064 : /* EncodeQuasiUniform is always equal or better than Limited GR with up to 3 msb values */
3065 389142 : ivas_qmetadata_encode_quasi_uniform( hMetaData, value, alphabet_size );
3066 : }
3067 : else
3068 : {
3069 4199157 : msb = value >> gr_param;
3070 4199157 : lsb = value & ( ( 1U << gr_param ) - 1 );
3071 :
3072 7272421 : for ( cnt = 0; cnt < msb; cnt++ )
3073 : {
3074 : /* leading one bits */
3075 3073264 : push_next_indice( hMetaData, 1, 1 );
3076 : }
3077 :
3078 4199157 : if ( msb < msb_alphabet_size - 1 )
3079 : {
3080 4184132 : push_next_indice( hMetaData, 0, 1 ); /* terminating zero bit, if not the largest msb (Limited GR) */
3081 4184132 : if ( gr_param > 0 )
3082 : {
3083 1348859 : push_next_indice( hMetaData, lsb, gr_param );
3084 : }
3085 : }
3086 : else
3087 : {
3088 15025 : ivas_qmetadata_encode_quasi_uniform( hMetaData, lsb, alphabet_size - ( ( msb_alphabet_size - 1 ) << gr_param ) );
3089 : }
3090 : }
3091 :
3092 4588299 : return;
3093 : }
3094 :
3095 :
3096 : /*-----------------------------------------------------------------------*
3097 : * Local functions (EC3, requantize directions)
3098 : *-----------------------------------------------------------------------*/
3099 :
3100 3382 : static int16_t truncGR0(
3101 : float *data,
3102 : float *data_hat,
3103 : uint16_t *data_idx,
3104 : const int16_t len,
3105 : const int16_t bits_allowed,
3106 : float *st,
3107 : float *ct )
3108 : {
3109 : int16_t i;
3110 : int16_t bits;
3111 3382 : const int16_t remap3b[8] = { 1, 6, 2, 4, 0, 5, 3, 7 };
3112 3382 : const int16_t remap2b[4] = { 1, 2, 0, 3 };
3113 : float diff[MAX_PARAM_SPATIAL_SUBFRAMES];
3114 : int16_t indx[MAX_PARAM_SPATIAL_SUBFRAMES];
3115 :
3116 3382 : bits = 0;
3117 3382 : set_f( data_hat, 0.0f, len );
3118 3382 : set_f( diff, 10000.0f, len );
3119 :
3120 3382 : if ( bits_allowed <= len + 1 )
3121 : {
3122 248 : bits = min( bits_allowed, len );
3123 248 : set_f( data_hat, 0.0f, len );
3124 : /*set_s(data_idx, 0, len); */
3125 1200 : for ( i = 0; i < bits; i++ )
3126 : {
3127 952 : if ( fabsf( data[i] ) <= 90 )
3128 : {
3129 867 : data_idx[i] = 0;
3130 867 : data_hat[i] = 0.0f;
3131 : }
3132 : else
3133 : {
3134 85 : data_idx[i] = 1;
3135 85 : data_hat[i] = -180.0f;
3136 : }
3137 : }
3138 :
3139 248 : return bits;
3140 : }
3141 :
3142 15460 : for ( i = 0; i < len; i++ )
3143 : {
3144 : #ifdef DEBUGGING
3145 : assert( data_idx[i] < MASA_NO_INDEX );
3146 : #endif
3147 12326 : data_idx[i] = quantize_phi( data[i] + 180, 0, &data_hat[i], 8 );
3148 12326 : data_hat[i] -= 180;
3149 12326 : data_idx[i] = remap3b[data_idx[i]];
3150 12326 : bits += ivas_qmetadata_encode_extended_gr_length( data_idx[i], 8, 0 );
3151 12326 : diff[i] = -st[i] - ct[i] * cosf( PI_OVER_180 * ( data[i] - data_hat[i] ) ); /*(data[i] - data_hat[i])*(data[i] - data_hat[i]);*/
3152 : }
3153 :
3154 3134 : i = 0;
3155 3134 : if ( bits > bits_allowed )
3156 : {
3157 2808 : sort_desc_ind( diff, len, indx );
3158 11756 : for ( i = len - 1; i >= 0; i-- )
3159 : {
3160 10092 : if ( data_idx[indx[i]] > 3 )
3161 : {
3162 5659 : bits -= ivas_qmetadata_encode_extended_gr_length( data_idx[indx[i]], 8, 0 );
3163 5659 : data_idx[indx[i]] = quantize_phi( data[indx[i]] + 180, 0, &data_hat[indx[i]], 4 );
3164 5659 : data_hat[indx[i]] -= 180;
3165 5659 : data_idx[indx[i]] = remap2b[data_idx[indx[i]]];
3166 5659 : bits += ivas_qmetadata_encode_extended_gr_length( data_idx[indx[i]], 8, 0 );
3167 5659 : diff[indx[i]] = -st[i] - ct[i] * cosf( PI_OVER_180 * ( data[indx[i]] - data_hat[indx[i]] ) );
3168 : }
3169 10092 : if ( bits <= bits_allowed )
3170 : {
3171 1144 : break;
3172 : }
3173 : }
3174 : }
3175 :
3176 3134 : if ( bits > bits_allowed )
3177 : {
3178 1664 : sort_desc_ind( diff, len, indx );
3179 3522 : for ( i = len - 1; i >= 0; i-- )
3180 : {
3181 :
3182 3491 : if ( data_idx[indx[i]] > 1 )
3183 : {
3184 3003 : bits -= ivas_qmetadata_encode_extended_gr_length( data_idx[indx[i]], 8, 0 );
3185 3003 : if ( fabsf( data[indx[i]] ) <= 90 )
3186 : {
3187 1886 : data_idx[indx[i]] = 0;
3188 1886 : data_hat[indx[i]] = 0;
3189 : }
3190 : else
3191 : {
3192 1117 : data_idx[indx[i]] = 1;
3193 1117 : data_hat[indx[i]] = -180;
3194 : }
3195 :
3196 3003 : bits += ivas_qmetadata_encode_extended_gr_length( data_idx[indx[i]], 8, 0 );
3197 3003 : diff[indx[i]] = -st[i] - ct[i] * cosf( PI_OVER_180 * ( data[indx[i]] - data_hat[indx[i]] ) );
3198 : }
3199 :
3200 3491 : if ( bits <= bits_allowed )
3201 : {
3202 1633 : break;
3203 : }
3204 : }
3205 : }
3206 :
3207 3134 : if ( bits > bits_allowed )
3208 : {
3209 :
3210 : #ifdef DEBUGGING
3211 : assert( bits_allowed > len );
3212 : for ( i = 0; i < len; i++ )
3213 : {
3214 : assert( data_idx[i] <= 1 );
3215 : }
3216 : #endif
3217 :
3218 31 : sort_desc_ind( diff, len, indx );
3219 32 : for ( i = len - 1; i >= 0; i-- )
3220 : {
3221 :
3222 32 : if ( data_idx[indx[i]] > 0 )
3223 : {
3224 31 : bits -= data_idx[indx[i]];
3225 31 : data_idx[indx[i]] = 0;
3226 31 : data_hat[indx[i]] = 0;
3227 : }
3228 32 : if ( bits <= bits_allowed )
3229 : {
3230 31 : break;
3231 : }
3232 : }
3233 : }
3234 :
3235 3134 : return bits;
3236 : }
3237 :
3238 :
3239 : /*-------------------------------------------------------------------*
3240 : * truncGR0_chan()
3241 : *
3242 : *
3243 : *-------------------------------------------------------------------*/
3244 :
3245 515 : static int16_t truncGR0_chan(
3246 : const float *data,
3247 : float *data_hat,
3248 : uint16_t *data_idx,
3249 : const int16_t len,
3250 : const int16_t bits_allowed,
3251 : float *st,
3252 : float *ct )
3253 : {
3254 : int16_t i, idx_crt;
3255 : int16_t bits;
3256 : float diff[MAX_PARAM_SPATIAL_SUBFRAMES], sort_diff[MAX_PARAM_SPATIAL_SUBFRAMES], min_diff, sum_diff;
3257 : int16_t indx[MAX_PARAM_SPATIAL_SUBFRAMES];
3258 :
3259 515 : bits = 0;
3260 515 : set_f( data_hat, 0.0f, len );
3261 515 : set_f( diff, 10000.0f, len );
3262 :
3263 515 : if ( bits_allowed <= len + 1 )
3264 : {
3265 0 : bits = min( bits_allowed, len );
3266 0 : set_f( data_hat, 0.0f, len );
3267 : /*set_s(data_idx, 0, len); */
3268 0 : for ( i = 0; i < bits; i++ )
3269 : {
3270 0 : if ( fabsf( data[i] ) <= 90 )
3271 : {
3272 0 : data_idx[i] = 0;
3273 0 : data_hat[i] = 0.0f;
3274 : }
3275 : else
3276 : {
3277 0 : data_idx[i] = 1;
3278 0 : data_hat[i] = -180.0f;
3279 : }
3280 : }
3281 0 : return bits;
3282 : }
3283 :
3284 2569 : for ( i = 0; i < len; i++ )
3285 : {
3286 : #ifdef DEBUGGING
3287 : assert( data_idx[i] < MASA_NO_INDEX );
3288 : #endif
3289 2054 : data_idx[i] = quantize_phi_chan_lbr( data[i], &data_hat[i], 9 );
3290 :
3291 2054 : bits += ivas_qmetadata_encode_extended_gr_length( data_idx[i], 9, 0 );
3292 2054 : diff[i] = -st[i] - ct[i] * cosf( ( data[i] - data_hat[i] ) * PI_OVER_180 );
3293 : }
3294 :
3295 734 : while ( bits > bits_allowed )
3296 : {
3297 219 : min_diff = 1000.0f;
3298 219 : idx_crt = -1;
3299 219 : mvr2r( diff, sort_diff, len );
3300 1092 : for ( i = 0; i < len; i++ )
3301 : {
3302 873 : if ( data_idx[i] > 0 )
3303 : {
3304 693 : sort_diff[i] = -st[i] - ct[i] * cosf( ( fabsf( data[i] ) - cb_azi_chan[( ( data_idx[i] + 1 ) >> 1 ) - 1] ) * PI_OVER_180 );
3305 693 : sum_diff = sum_f( sort_diff, len );
3306 :
3307 693 : if ( sum_diff < min_diff )
3308 : {
3309 376 : min_diff = sum_diff;
3310 376 : idx_crt = i;
3311 : }
3312 693 : sort_diff[i] = diff[i];
3313 : }
3314 : }
3315 :
3316 219 : if ( idx_crt > -1 )
3317 : {
3318 219 : bits -= ivas_qmetadata_encode_extended_gr_length( data_idx[idx_crt], 9, 0 );
3319 219 : data_idx[idx_crt] = quantize_phi_chan_lbr( data[idx_crt], &data_hat[idx_crt], data_idx[idx_crt] + 1 );
3320 219 : bits += ivas_qmetadata_encode_extended_gr_length( data_idx[idx_crt], 9, 0 );
3321 219 : diff[idx_crt] = -st[idx_crt] - ct[idx_crt] * cosf( ( data[idx_crt] - data_hat[idx_crt] ) * PI_OVER_180 );
3322 : }
3323 : else
3324 : {
3325 0 : break;
3326 : }
3327 : }
3328 :
3329 515 : if ( bits > bits_allowed )
3330 : {
3331 0 : mvr2r( diff, sort_diff, len );
3332 0 : sort_desc_ind( sort_diff, len, indx );
3333 :
3334 0 : for ( i = len - 1; i >= 0; i-- )
3335 : {
3336 0 : idx_crt = indx[i];
3337 0 : if ( data_idx[idx_crt] > 0 )
3338 : {
3339 0 : bits -= ivas_qmetadata_encode_extended_gr_length( data_idx[idx_crt], 9, 0 );
3340 0 : data_idx[idx_crt] = 0;
3341 0 : data_hat[idx_crt] = 0;
3342 0 : bits += 1;
3343 : }
3344 :
3345 0 : if ( bits <= bits_allowed )
3346 : {
3347 0 : break;
3348 : }
3349 : }
3350 : }
3351 :
3352 515 : return bits;
3353 : }
3354 :
3355 :
3356 : /*-------------------------------------------------------------------*
3357 : * common_direction()
3358 : *
3359 : *
3360 : *-------------------------------------------------------------------*/
3361 :
3362 3594 : static int16_t common_direction(
3363 : IVAS_QDIRECTION *q_direction,
3364 : const int16_t band_idx,
3365 : const int16_t len,
3366 : const int16_t bits_allowed,
3367 : BSTR_ENC_HANDLE hMetaData,
3368 : float *elevation_orig,
3369 : float *azimuth_orig )
3370 : {
3371 : int16_t nbits;
3372 : int16_t no_th, i, id_th, k;
3373 : float theta_cb[5];
3374 : float dist, best_dist;
3375 : float ct[MAX_PARAM_SPATIAL_SUBFRAMES], st[MAX_PARAM_SPATIAL_SUBFRAMES];
3376 :
3377 3594 : nbits = 0;
3378 :
3379 3594 : if ( bits_allowed == 0 )
3380 : {
3381 0 : for ( i = 0; i < len; i++ )
3382 : {
3383 0 : q_direction->band_data[band_idx].elevation[i] = 0;
3384 0 : q_direction->band_data[band_idx].azimuth[i] = 0;
3385 : }
3386 :
3387 0 : return 0;
3388 : }
3389 :
3390 3594 : if ( bits_allowed <= len + 1 )
3391 : {
3392 83 : set_f( q_direction->band_data[band_idx].elevation, 0.0f, len );
3393 83 : set_f( st, 0.0f, len );
3394 :
3395 415 : for ( i = 0; i < len; i++ )
3396 : {
3397 332 : ct[i] = cosf( elevation_orig[i] * PI_OVER_180 );
3398 : }
3399 :
3400 83 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3401 : {
3402 0 : nbits = truncGR0_chan( azimuth_orig, q_direction->band_data[band_idx].azimuth, q_direction->band_data[band_idx].azimuth_index, len, bits_allowed, st, ct );
3403 : }
3404 : else
3405 : {
3406 83 : nbits = truncGR0( azimuth_orig, q_direction->band_data[band_idx].azimuth, q_direction->band_data[band_idx].azimuth_index, len, bits_allowed, st, ct );
3407 : }
3408 :
3409 415 : for ( i = 0; i < nbits; i++ )
3410 : {
3411 332 : push_next_indice( hMetaData, q_direction->band_data[band_idx].azimuth_index[i], 1 );
3412 : }
3413 :
3414 83 : return nbits;
3415 : }
3416 :
3417 3511 : no_th = no_theta_masa[0] + 3; /* only 5 values for theta; the lat 2 are +/-90 */
3418 :
3419 3511 : theta_cb[0] = 0;
3420 3511 : theta_cb[1] = delta_theta_masa[2];
3421 3511 : theta_cb[2] = -theta_cb[1];
3422 3511 : theta_cb[3] = 90.0f;
3423 3511 : theta_cb[4] = -90.0f;
3424 3511 : best_dist = 900000.0f;
3425 3511 : id_th = 0;
3426 :
3427 21066 : for ( i = 0; i < no_th; i++ )
3428 : {
3429 17555 : dist = 0.0f;
3430 87775 : for ( k = 0; k < len; k++ )
3431 : {
3432 70220 : dist += ( elevation_orig[k] - theta_cb[i] ) * ( elevation_orig[k] - theta_cb[i] );
3433 : }
3434 17555 : if ( dist < best_dist )
3435 : {
3436 4645 : id_th = i;
3437 4645 : best_dist = dist;
3438 : }
3439 : }
3440 :
3441 3511 : set_f( q_direction->band_data[band_idx].elevation, theta_cb[id_th], len );
3442 :
3443 17555 : for ( i = 0; i < len; i++ )
3444 : {
3445 14044 : q_direction->band_data[band_idx].elevation_index[i] = id_th;
3446 : }
3447 :
3448 3511 : if ( id_th == 0 )
3449 : {
3450 2388 : push_next_indice( hMetaData, 0, 1 ); /* average theta index */
3451 2388 : set_f( st, 0.0f, len );
3452 :
3453 11940 : for ( i = 0; i < len; i++ )
3454 : {
3455 9552 : ct[i] = cosf( elevation_orig[i] * PI_OVER_180 );
3456 : }
3457 :
3458 2388 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3459 : {
3460 178 : nbits = truncGR0_chan( azimuth_orig, q_direction->band_data[band_idx].azimuth, q_direction->band_data[band_idx].azimuth_index, len, bits_allowed - 1, st, ct ) + 1;
3461 : }
3462 : else
3463 : {
3464 2210 : nbits = truncGR0( azimuth_orig, q_direction->band_data[band_idx].azimuth, q_direction->band_data[band_idx].azimuth_index, len, bits_allowed - 1, st, ct ) + 1;
3465 : }
3466 : }
3467 : else
3468 : {
3469 1123 : if ( id_th >= 3 )
3470 : {
3471 : /* theta is 90 or -90; only theta is sent */
3472 11 : push_next_indice( hMetaData, id_th + 11, 4 ); /* average theta index */
3473 11 : set_f( q_direction->band_data[band_idx].azimuth, 0.0f, len );
3474 55 : for ( i = 0; i < len; i++ )
3475 : {
3476 44 : q_direction->band_data[band_idx].azimuth_index[i] = 0;
3477 : }
3478 11 : nbits = 4;
3479 :
3480 11 : return nbits;
3481 : }
3482 :
3483 1112 : set_f( st, sinf( theta_cb[id_th] * PI_OVER_180 ), len );
3484 1112 : set_f( ct, cosf( theta_cb[id_th] * PI_OVER_180 ), len );
3485 :
3486 5560 : for ( i = 0; i < len; i++ )
3487 : {
3488 4448 : st[i] *= sinf( elevation_orig[i] * PI_OVER_180 );
3489 4448 : ct[i] *= cosf( elevation_orig[i] * PI_OVER_180 );
3490 4448 : q_direction->band_data[band_idx].azimuth_index[i] = 0;
3491 : }
3492 :
3493 1112 : if ( id_th == 1 )
3494 : {
3495 675 : push_next_indice( hMetaData, 2, 2 ); /* average theta index */
3496 : }
3497 : else
3498 : {
3499 437 : assert( id_th == 2 );
3500 437 : push_next_indice( hMetaData, 6, 3 ); /* average theta index */
3501 : }
3502 :
3503 1112 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3504 : {
3505 335 : nbits = truncGR0_chan( azimuth_orig, q_direction->band_data[band_idx].azimuth, q_direction->band_data[band_idx].azimuth_index, len, bits_allowed - ( id_th + 1 ), st, ct ) + ( id_th + 1 );
3506 : }
3507 : else
3508 : {
3509 777 : nbits = truncGR0( azimuth_orig, q_direction->band_data[band_idx].azimuth, q_direction->band_data[band_idx].azimuth_index, len, bits_allowed - ( id_th + 1 ), st, ct ) + ( id_th + 1 );
3510 : }
3511 : }
3512 :
3513 3500 : if ( bits_allowed - ( id_th + 1 ) <= len + 1 )
3514 : {
3515 :
3516 84 : for ( i = 0; i < min( len, bits_allowed - ( id_th + 1 ) ); i++ )
3517 : {
3518 67 : push_next_indice( hMetaData, q_direction->band_data[band_idx].azimuth_index[i], 1 );
3519 : }
3520 : }
3521 : else
3522 : {
3523 3483 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3524 : {
3525 2565 : for ( i = 0; i < len; i++ )
3526 : {
3527 2052 : ivas_qmetadata_encode_extended_gr( hMetaData, q_direction->band_data[band_idx].azimuth_index[i], 9, 0 );
3528 : }
3529 : }
3530 : else
3531 : {
3532 14850 : for ( i = 0; i < len; i++ )
3533 : {
3534 11880 : ivas_qmetadata_encode_extended_gr( hMetaData, q_direction->band_data[band_idx].azimuth_index[i], 8, 0 );
3535 : }
3536 : }
3537 : }
3538 :
3539 3500 : return nbits;
3540 : }
3541 :
3542 :
3543 : /*-------------------------------------------------------------------*
3544 : * encode_directions_subband()
3545 : *
3546 : *
3547 : *-------------------------------------------------------------------*/
3548 :
3549 40656 : static int16_t encode_directions_subband(
3550 : IVAS_QDIRECTION *q_direction,
3551 : int16_t coding_subbands,
3552 : BSTR_ENC_HANDLE hMetaData,
3553 : const int16_t j,
3554 : const int16_t next_j,
3555 : const int16_t no_subframes,
3556 : const int16_t last_subband,
3557 : int16_t *p_diff,
3558 : float *elevation_orig,
3559 : float *azimuth_orig )
3560 : {
3561 : int16_t allowed_bits, use_vq, max_nb_idx, k;
3562 : int16_t diff;
3563 : float d1, d2;
3564 : int16_t nbits;
3565 : int16_t *bits_dir0;
3566 :
3567 40656 : nbits = 0;
3568 40656 : diff = *p_diff;
3569 40656 : bits_dir0 = (int16_t *) q_direction->band_data[j].bits_sph_idx;
3570 40656 : allowed_bits = sum_s( bits_dir0, no_subframes );
3571 :
3572 40656 : if ( allowed_bits > 0 )
3573 : {
3574 40656 : use_vq = 0;
3575 40656 : max_nb_idx = 0;
3576 :
3577 151011 : for ( k = 0; k < no_subframes; k++ )
3578 : {
3579 110355 : if ( bits_dir0[k] > use_vq )
3580 : {
3581 49083 : use_vq = bits_dir0[k];
3582 49083 : max_nb_idx = k;
3583 : }
3584 : }
3585 :
3586 40656 : if ( no_subframes > 1 )
3587 : {
3588 23233 : if ( ( use_vq > 1 ) && ( use_vq <= LIMIT_USE_COMMON ) )
3589 : {
3590 3656 : bits_dir0[max_nb_idx] -= 1;
3591 3656 : allowed_bits -= 1;
3592 : }
3593 : #ifdef DEBUGGING
3594 : assert( bits_dir0[max_nb_idx] > 0 );
3595 : #endif
3596 : }
3597 40656 : if ( no_subframes > 1 )
3598 : {
3599 23233 : if ( use_vq <= LIMIT_USE_COMMON )
3600 : {
3601 : /* calculate the two distances */
3602 3712 : calculate_two_distances( q_direction->band_data[j].elevation, bits_dir0, allowed_bits, no_subframes, &d1, &d2 );
3603 3712 : if ( ( ( use_vq > 1 ) && ( d2 <= d1 ) ) || ( use_vq <= 1 ) )
3604 : {
3605 3594 : if ( use_vq > 1 )
3606 : {
3607 3538 : push_next_indice( hMetaData, 1, 1 ); /* signal VQ */
3608 : }
3609 :
3610 3594 : diff += common_direction( q_direction, j, no_subframes, allowed_bits, hMetaData, elevation_orig, azimuth_orig ) - allowed_bits;
3611 :
3612 3594 : if ( last_subband == 0 )
3613 : {
3614 2501 : update_bits_next_block( q_direction, &diff, next_j, coding_subbands, no_subframes );
3615 : }
3616 : }
3617 : else
3618 : {
3619 118 : push_next_indice( hMetaData, 0, 1 );
3620 :
3621 118 : if ( last_subband == 0 )
3622 : {
3623 61 : mvr2r( elevation_orig, q_direction->band_data[j].elevation, no_subframes );
3624 61 : mvr2r( azimuth_orig, q_direction->band_data[j].azimuth, no_subframes );
3625 61 : joint_encoding( q_direction, j, next_j, coding_subbands, bits_dir0, allowed_bits, hMetaData, &diff );
3626 : }
3627 : else
3628 : {
3629 285 : for ( k = 0; k < no_subframes; k++ )
3630 : {
3631 : /* requantize the direction */
3632 228 : q_direction->band_data[j].spherical_index[k] = quantize_direction( elevation_orig[k], azimuth_orig[k], q_direction->band_data[j].bits_sph_idx[k], &q_direction->band_data[j].elevation[k], &q_direction->band_data[j].azimuth[k],
3633 228 : &q_direction->band_data[j].elevation_index[k], &q_direction->band_data[j].azimuth_index[k], q_direction->cfg.mc_ls_setup );
3634 : }
3635 :
3636 57 : if ( allowed_bits > 0 )
3637 : {
3638 57 : nbits = write_fixed_rate_direction( hMetaData, q_direction, j, no_subframes );
3639 : }
3640 : }
3641 : }
3642 : }
3643 : else
3644 : {
3645 : /* there is only joint coding */
3646 19521 : mvr2r( elevation_orig, q_direction->band_data[j].elevation, no_subframes );
3647 19521 : mvr2r( azimuth_orig, q_direction->band_data[j].azimuth, no_subframes );
3648 :
3649 19521 : if ( last_subband == 0 )
3650 : {
3651 12605 : joint_encoding( q_direction, j, next_j, coding_subbands, bits_dir0, allowed_bits, hMetaData, &diff );
3652 : }
3653 : else
3654 : {
3655 34580 : for ( k = 0; k < no_subframes; k++ )
3656 : {
3657 : /* requantize the direction */
3658 27664 : q_direction->band_data[j].spherical_index[k] = quantize_direction( elevation_orig[k], azimuth_orig[k], q_direction->band_data[j].bits_sph_idx[k], &q_direction->band_data[j].elevation[k], &q_direction->band_data[j].azimuth[k],
3659 27664 : &q_direction->band_data[j].elevation_index[k], &q_direction->band_data[j].azimuth_index[k], q_direction->cfg.mc_ls_setup );
3660 : }
3661 :
3662 6916 : if ( allowed_bits > 0 )
3663 : {
3664 6916 : nbits = write_fixed_rate_direction( hMetaData, q_direction, j, no_subframes );
3665 : }
3666 : }
3667 : }
3668 : }
3669 : else
3670 : {
3671 : /* 1 subframe case */
3672 : /* there is only joint coding */
3673 17423 : mvr2r( elevation_orig, q_direction->band_data[j].elevation, no_subframes );
3674 17423 : mvr2r( azimuth_orig, q_direction->band_data[j].azimuth, no_subframes );
3675 :
3676 17423 : if ( last_subband == 0 )
3677 : {
3678 11834 : joint_encoding( q_direction, j, next_j, coding_subbands, bits_dir0, allowed_bits, hMetaData, &diff );
3679 : }
3680 : else
3681 : {
3682 11178 : for ( k = 0; k < no_subframes; k++ )
3683 : {
3684 : /* requantize the direction */
3685 5589 : q_direction->band_data[j].spherical_index[k] = quantize_direction( elevation_orig[k], azimuth_orig[k], q_direction->band_data[j].bits_sph_idx[k], &q_direction->band_data[j].elevation[k], &q_direction->band_data[j].azimuth[k],
3686 5589 : &q_direction->band_data[j].elevation_index[k], &q_direction->band_data[j].azimuth_index[k], q_direction->cfg.mc_ls_setup );
3687 : }
3688 :
3689 5589 : if ( allowed_bits > 0 )
3690 : {
3691 5589 : nbits = write_fixed_rate_direction( hMetaData, q_direction, j, no_subframes );
3692 : }
3693 : }
3694 : }
3695 : }
3696 : else
3697 : {
3698 0 : set_f( q_direction->band_data[j].elevation, 0.0f, no_subframes );
3699 0 : set_f( q_direction->band_data[j].azimuth, 0.0f, no_subframes );
3700 : }
3701 :
3702 40656 : *p_diff = diff;
3703 :
3704 40656 : return nbits;
3705 : }
3706 :
3707 :
3708 : /*-------------------------------------------------------------------*
3709 : * calc_var_azi()
3710 : *
3711 : *
3712 : *-------------------------------------------------------------------*/
3713 :
3714 3525 : static int16_t calc_var_azi(
3715 : const IVAS_QDIRECTION *q_direction,
3716 : const int16_t diffuseness_index_max_ec_frame,
3717 : const float avg_azimuth,
3718 : float *avg_azimuth_out )
3719 : {
3720 : float var_band, dif;
3721 : float avg_direction_vector_band[3], avg_azimuth_band[24], direction_vector[3];
3722 : float avg_elevation;
3723 : int16_t i, j, idx;
3724 :
3725 3525 : idx = 0;
3726 3525 : set_zero( avg_azimuth_band, 24 );
3727 :
3728 21150 : for ( i = 0; i < q_direction->cfg.nbands; i++ )
3729 : {
3730 17625 : set_zero( avg_direction_vector_band, 3 );
3731 17625 : if ( q_direction->band_data[i].energy_ratio_index_mod[0] <= diffuseness_index_max_ec_frame )
3732 : {
3733 63995 : for ( j = 0; j < q_direction->cfg.nblocks; j++ )
3734 : {
3735 : /*compute the average direction */
3736 51196 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[i].azimuth[j], q_direction->band_data[i].elevation[j], direction_vector );
3737 51196 : v_add( avg_direction_vector_band, direction_vector, avg_direction_vector_band, 3 );
3738 : }
3739 12799 : ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector_band, &avg_azimuth_band[idx], &avg_elevation );
3740 12799 : idx++;
3741 : }
3742 : }
3743 :
3744 3525 : var_band = 0.0f;
3745 :
3746 16324 : for ( i = 0; i < idx; i++ )
3747 : {
3748 12799 : dif = ( avg_azimuth_band[idx] - avg_azimuth );
3749 12799 : if ( dif < 0 )
3750 : {
3751 2879 : dif = -dif;
3752 : }
3753 12799 : if ( dif > 180 )
3754 : {
3755 0 : dif = 360 - dif;
3756 : }
3757 :
3758 12799 : var_band += dif * dif;
3759 : }
3760 :
3761 3525 : if ( idx > 0 )
3762 : {
3763 3525 : var_band = var_band / idx;
3764 : }
3765 :
3766 3525 : if ( var_band <= VAR_AZI_THRESH )
3767 : {
3768 1273 : *avg_azimuth_out = avg_azimuth;
3769 1273 : return 0;
3770 : }
3771 : else
3772 : {
3773 2252 : *avg_azimuth_out = avg_azimuth_band[0];
3774 2252 : return 1;
3775 : }
3776 : }
3777 :
3778 :
3779 : /*-------------------------------------------------------------------*
3780 : * requantize_direction_EC_3()
3781 : *
3782 : *
3783 : *-------------------------------------------------------------------*/
3784 :
3785 9076 : static ivas_error requantize_direction_EC_3(
3786 : int16_t *extra_bits,
3787 : IVAS_QDIRECTION *q_direction,
3788 : const int16_t coding_subbands,
3789 : BSTR_ENC_HANDLE hMetaData,
3790 : float elevation_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES],
3791 : float azimuth_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES],
3792 : int16_t *ind_order )
3793 : {
3794 : /* gradually increase the bits following the performance of the EC layer*/
3795 : int16_t j, k;
3796 : int16_t use_vq;
3797 : int16_t diff, allowed_bits, last_j;
3798 : int16_t no_subframes, start_band;
3799 : float st[MAX_PARAM_SPATIAL_SUBFRAMES], ct[MAX_PARAM_SPATIAL_SUBFRAMES];
3800 : int16_t *bits_dir0;
3801 : #ifdef DEBUGGING
3802 : int16_t nbits;
3803 :
3804 : nbits = 0;
3805 : #endif
3806 9076 : no_subframes = q_direction->cfg.nblocks;
3807 9076 : start_band = q_direction->cfg.start_band;
3808 :
3809 9076 : if ( q_direction->not_in_2D > MASA_LIMIT_2D )
3810 : {
3811 8620 : j = ind_order[coding_subbands - 1];
3812 8620 : bits_dir0 = (int16_t *) q_direction->band_data[j].bits_sph_idx;
3813 8620 : allowed_bits = sum_s( bits_dir0, no_subframes );
3814 8620 : last_j = j - ( allowed_bits == 0 );
3815 8620 : diff = 0;
3816 8620 : if ( coding_subbands == 1 )
3817 : {
3818 145 : last_j = start_band;
3819 : }
3820 35621 : for ( j = 0; j < last_j; j++ )
3821 : {
3822 27001 : k = ind_order[j];
3823 27001 : encode_directions_subband( q_direction, coding_subbands, hMetaData, k, ind_order[j + 1], no_subframes, 0, &diff, elevation_orig[k], azimuth_orig[k] );
3824 : }
3825 :
3826 : /* last subbands to be written in fixed rate */
3827 22275 : for ( j = last_j; j < coding_subbands; j++ )
3828 : {
3829 13655 : k = ind_order[j];
3830 13655 : encode_directions_subband( q_direction, coding_subbands, hMetaData, k, 0, no_subframes, 1, &diff, elevation_orig[k], azimuth_orig[k] );
3831 : }
3832 : }
3833 : else /* 2D */
3834 : {
3835 456 : diff = 0;
3836 : #ifdef DEBUGGING
3837 : nbits = 0;
3838 : #endif
3839 1952 : for ( j = start_band; j < coding_subbands; j++ )
3840 : {
3841 1496 : bits_dir0 = (int16_t *) q_direction->band_data[j].bits_sph_idx;
3842 1496 : allowed_bits = sum_s( bits_dir0, no_subframes );
3843 1496 : use_vq = 0;
3844 :
3845 6052 : for ( k = 0; k < no_subframes; k++ )
3846 : {
3847 4556 : if ( bits_dir0[k] > use_vq )
3848 : {
3849 1749 : use_vq = bits_dir0[k];
3850 : }
3851 : }
3852 :
3853 1496 : if ( ( use_vq <= 3 ) && ( allowed_bits <= 11 ) )
3854 : {
3855 314 : set_f( st, 0.0f, no_subframes );
3856 :
3857 1315 : for ( k = 0; k < no_subframes; k++ )
3858 : {
3859 1001 : ct[k] = cosf( elevation_orig[j][k] * PI_OVER_180 );
3860 : }
3861 :
3862 314 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3863 : {
3864 : #ifdef DEBUGGING
3865 : nbits += truncGR0_chan( azimuth_orig[j], q_direction->band_data[j].azimuth, q_direction->band_data[j].azimuth_index, no_subframes, allowed_bits, st, ct );
3866 : #else
3867 2 : truncGR0_chan( azimuth_orig[j], q_direction->band_data[j].azimuth, q_direction->band_data[j].azimuth_index, no_subframes, allowed_bits, st, ct );
3868 : #endif
3869 : }
3870 : else
3871 : {
3872 : #ifdef DEBUGGING
3873 : nbits += truncGR0( azimuth_orig[j], q_direction->band_data[j].azimuth, q_direction->band_data[j].azimuth_index, no_subframes, allowed_bits, st, ct );
3874 : #else
3875 312 : truncGR0( azimuth_orig[j], q_direction->band_data[j].azimuth, q_direction->band_data[j].azimuth_index, no_subframes, allowed_bits, st, ct );
3876 : #endif
3877 : }
3878 :
3879 314 : if ( allowed_bits <= no_subframes + 1 )
3880 : {
3881 701 : for ( k = 0; k < min( no_subframes, allowed_bits ); k++ )
3882 : {
3883 553 : push_next_indice( hMetaData, q_direction->band_data[j].azimuth_index[k], 1 );
3884 : }
3885 : }
3886 : else
3887 : {
3888 614 : for ( k = 0; k < no_subframes; k++ )
3889 : {
3890 448 : ivas_qmetadata_encode_extended_gr( hMetaData, q_direction->band_data[j].azimuth_index[k], q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ? 9 : 8, 0 );
3891 : }
3892 : }
3893 : }
3894 : else
3895 : {
3896 4737 : for ( k = 0; k < no_subframes; k++ )
3897 : {
3898 : /* requantize the direction */
3899 7110 : q_direction->band_data[j].spherical_index[k] = quantize_direction2D( azimuth_orig[j][k], 1 << q_direction->band_data[j].bits_sph_idx[k], &q_direction->band_data[j].azimuth[k],
3900 3555 : &q_direction->band_data[j].azimuth_index[k], q_direction->cfg.mc_ls_setup );
3901 3555 : q_direction->band_data[j].elevation_index[k] = 0;
3902 : }
3903 : #ifdef DEBUGGING
3904 : nbits += write_fixed_rate_direction( hMetaData, q_direction, j, no_subframes );
3905 : #else
3906 1182 : write_fixed_rate_direction( hMetaData, q_direction, j, no_subframes );
3907 : #endif
3908 : }
3909 : }
3910 : }
3911 : #ifdef DEBUGGING
3912 : if ( diff > 0 )
3913 : {
3914 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Not enough bits in requantize_direction_EC_3(); %d bits written \n", nbits );
3915 : }
3916 : #endif
3917 :
3918 9076 : *extra_bits = -diff;
3919 :
3920 9076 : return IVAS_ERR_OK;
3921 : }
3922 :
3923 :
3924 : /*-------------------------------------------------------------------*
3925 : * write_fixed_rate_direction()
3926 : *
3927 : * writing of the spherical indexes
3928 : *-------------------------------------------------------------------*/
3929 :
3930 : /*! r: number of bits written */
3931 30432 : static int16_t write_fixed_rate_direction(
3932 : BSTR_ENC_HANDLE hMetaData, /* i : MASA metadata structure */
3933 : IVAS_QDIRECTION *qdirection, /* i/o: quantized directional parameters */
3934 : const int16_t j_idx, /* i : index of subband for which the data is written */
3935 : const int16_t len /* i : number of data */
3936 : )
3937 : {
3938 : int16_t nbits, i;
3939 :
3940 30432 : nbits = 0;
3941 98850 : for ( i = 0; i < len; i++ )
3942 : {
3943 68418 : push_next_indice( hMetaData, qdirection->band_data[j_idx].spherical_index[i], qdirection->band_data[j_idx].bits_sph_idx[i] );
3944 68418 : nbits += qdirection->band_data[j_idx].bits_sph_idx[i];
3945 : }
3946 :
3947 30432 : return nbits;
3948 : }
3949 :
3950 :
3951 : /*-------------------------------------------------------------------*
3952 : * joint_encoding()
3953 : *
3954 : * joint encoding of elevation and azimuth
3955 : *-------------------------------------------------------------------*/
3956 :
3957 24500 : static void joint_encoding(
3958 : IVAS_QDIRECTION *q_direction, /* i/o: quantized directional parameters */
3959 : const int16_t j, /* i : subband index */
3960 : const int16_t next_j, /* i : next subband index */
3961 : const int16_t coding_subbands, /* i : total number of subband */
3962 : int16_t *bits_dir0, /* i/o: number of bits for each tile in each subband */
3963 : const int16_t allowed_bits, /* i : maximum number of bits available for the current subband */
3964 : BSTR_ENC_HANDLE hMetaData, /* i/o: metadata bitstream handle */
3965 : int16_t *diff /* o : bits to be given/taken to next subband */
3966 : )
3967 : {
3968 : int16_t k;
3969 : int16_t GR_ord_azimuth, use_context, GR_ord_elevation;
3970 : uint8_t method;
3971 : int16_t nbits;
3972 : int16_t same;
3973 : uint16_t data[MAX_PARAM_SPATIAL_SUBFRAMES];
3974 24500 : int16_t len_data = 0;
3975 : int16_t no_symb_ele[MAX_PARAM_SPATIAL_SUBFRAMES];
3976 :
3977 86998 : for ( k = 0; k < q_direction->cfg.nblocks; k++ )
3978 : {
3979 62498 : q_direction->band_data[j].bits_sph_idx[k] = bits_dir0[k];
3980 :
3981 : /* requantize the direction */
3982 124996 : q_direction->band_data[j].spherical_index[k] = quantize_direction( q_direction->band_data[j].elevation[k], q_direction->band_data[j].azimuth[k], q_direction->band_data[j].bits_sph_idx[k], &q_direction->band_data[j].elevation[k],
3983 62498 : &q_direction->band_data[j].azimuth[k], &q_direction->band_data[j].elevation_index[k], &q_direction->band_data[j].azimuth_index[k], q_direction->cfg.mc_ls_setup );
3984 :
3985 62498 : if ( bits_dir0[k] >= 3 )
3986 : {
3987 62232 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3988 : {
3989 3607 : q_direction->band_data[j].elevation_m_alphabet[k] = no_theta_masa[bits_dir0[k] - 3];
3990 3607 : q_direction->band_data[j].azimuth_m_alphabet[k] = no_phi_masa[bits_dir0[k] - 1][q_direction->band_data[j].elevation_index[k]];
3991 : }
3992 : else
3993 : {
3994 58625 : q_direction->band_data[j].elevation_m_alphabet[k] = no_theta_masa[bits_dir0[k] - 3] * 2 - 1;
3995 58625 : q_direction->band_data[j].azimuth_m_alphabet[k] = no_phi_masa[bits_dir0[k] - 1][( q_direction->band_data[j].elevation_index[k] + 1 ) >> 1];
3996 : }
3997 62232 : assert( q_direction->band_data[j].elevation_index[k] != MASA_NO_INDEX );
3998 : }
3999 266 : else if ( bits_dir0[k] > 0 )
4000 : {
4001 266 : q_direction->band_data[j].elevation_m_alphabet[k] = 1;
4002 266 : q_direction->band_data[j].azimuth_m_alphabet[k] = no_phi_masa[bits_dir0[k] - 1][0];
4003 : }
4004 : else
4005 : {
4006 0 : q_direction->band_data[j].elevation_m_alphabet[k] = 1;
4007 0 : q_direction->band_data[j].azimuth_m_alphabet[k] = 1;
4008 : }
4009 : }
4010 :
4011 86998 : for ( k = 0; k < q_direction->cfg.nblocks; k++ )
4012 : {
4013 62498 : if ( q_direction->band_data[j].bits_sph_idx[k] <= 2 )
4014 : {
4015 266 : q_direction->band_data[j].elevation_index[k] = MASA_NO_INDEX;
4016 : }
4017 : else
4018 : {
4019 62232 : no_symb_ele[len_data] = q_direction->band_data[j].elevation_m_alphabet[k];
4020 62232 : data[len_data++] = q_direction->band_data[j].elevation_index[k];
4021 : }
4022 : }
4023 :
4024 :
4025 : /* encode indexes for current subband and count the number of bits */
4026 :
4027 24500 : if ( q_direction->cfg.nblocks == 1 && q_direction->band_data[j].bits_sph_idx[0] <= MASA_MIN_BITS_TF + 1 )
4028 : {
4029 : /* encode with fixed rate only if only one subframe and very low number of bits */
4030 6224 : nbits = write_fixed_rate_direction( hMetaData, q_direction, j, q_direction->cfg.nblocks );
4031 : }
4032 : else
4033 : {
4034 :
4035 18276 : if ( len_data > 0 )
4036 : {
4037 18276 : nbits = GR_bits_new( data, no_symb_ele, len_data, MASA_GR_ORD_EL, 1, &GR_ord_elevation );
4038 : }
4039 : else
4040 : {
4041 0 : nbits = 0;
4042 0 : GR_ord_elevation = MASA_GR_ORD_EL;
4043 : }
4044 :
4045 18276 : same = 1;
4046 56274 : for ( k = 1; k < q_direction->cfg.nblocks; k++ )
4047 : {
4048 37998 : if ( q_direction->band_data[j].elevation_index[k] != q_direction->band_data[j].elevation_index[0] )
4049 : {
4050 9339 : same = 0;
4051 : }
4052 : }
4053 18276 : if ( same == 1 && q_direction->band_data[j].elevation_index[0] < 4 )
4054 : {
4055 12140 : nbits = 3;
4056 : }
4057 : else
4058 : {
4059 6136 : same = 0;
4060 6136 : nbits += 1;
4061 : }
4062 :
4063 36552 : nbits += GR_bits_azimuth_context( q_direction->band_data[j].azimuth_index,
4064 18276 : q_direction->band_data[j].azimuth_m_alphabet, q_direction->cfg.nblocks, MASA_GR_ORD_AZ,
4065 18276 : q_direction->band_data[j].bits_sph_idx, &GR_ord_azimuth, &use_context );
4066 :
4067 18276 : if ( allowed_bits == 0 )
4068 : {
4069 0 : nbits = 0;
4070 : }
4071 : else
4072 : {
4073 18276 : if ( nbits >= allowed_bits )
4074 : {
4075 10464 : nbits = allowed_bits + 1; /* fixed rate encoding */
4076 10464 : method = 1;
4077 10464 : push_next_indice( hMetaData, method, 1 );
4078 :
4079 : /* write current subband data */
4080 10464 : nbits = 1 + write_fixed_rate_direction( hMetaData, q_direction, j, q_direction->cfg.nblocks );
4081 : }
4082 : else
4083 : {
4084 7812 : nbits += 1; /* EC coding */
4085 7812 : method = 0;
4086 7812 : push_next_indice( hMetaData, method, 1 );
4087 :
4088 : /* write current subband data */
4089 7812 : write_ec_direction( &nbits, hMetaData, q_direction, j, q_direction->cfg.nblocks, GR_ord_elevation, GR_ord_azimuth, use_context, same );
4090 7812 : nbits++;
4091 : }
4092 : }
4093 : }
4094 24500 : *diff += nbits - allowed_bits;
4095 24500 : update_bits_next_block( q_direction, diff, next_j, coding_subbands, q_direction->cfg.nblocks );
4096 :
4097 24500 : return;
4098 : }
4099 :
4100 :
4101 : /*-------------------------------------------------------------------*
4102 : * calculate_two_distances()
4103 : *
4104 : * calculate estimated distortions if encoding with VQ or not
4105 : *-------------------------------------------------------------------*/
4106 :
4107 3712 : static void calculate_two_distances(
4108 : float *el, /* i : elevation values */
4109 : int16_t *bits, /* i : number of bits for each tile */
4110 : const int16_t total_bits, /* i : total number of bits for subband */
4111 : const int16_t len, /* i : number of tiles */
4112 : float *p_d1, /* o : first distortion */
4113 : float *p_d2 /* o : second distortion */
4114 : )
4115 : {
4116 : int16_t i;
4117 : float d1, d2, el_av;
4118 3712 : const float cos_delta_phi_cb[] = { 0.848f, 0.8988f, 0.9272f, 0.9563f, 0.9744f, 0.9816f, 0.9877f, 0.9925f };
4119 : float var_el;
4120 :
4121 3712 : d1 = 0.0f;
4122 3712 : d2 = 0.0f;
4123 :
4124 3712 : el_av = mean( el, len );
4125 3712 : if ( total_bits > 9 )
4126 : {
4127 17070 : for ( i = 0; i < len; i++ )
4128 : {
4129 13656 : if ( bits[i] > 2 )
4130 : {
4131 9494 : if ( fabsf( el[i] ) < fabsf( ( fabsf( el[i] ) - 45 ) ) )
4132 : {
4133 : /* el_hat = 0*/
4134 6198 : if ( bits[i] == 3 )
4135 : {
4136 6198 : d1 += 1 - 0.7f * cosf( el[i] * PI_OVER_180 );
4137 : }
4138 : else
4139 : {
4140 0 : d1 += 1 - 0.92f * cosf( el[i] * PI_OVER_180 );
4141 : }
4142 : }
4143 : else
4144 : {
4145 3296 : if ( bits[i] == 3 )
4146 : {
4147 3296 : d1 += 1 - sinf( el[i] * PI_OVER_180 ) * 0.7f * sign( el[i] );
4148 : }
4149 : else
4150 : {
4151 0 : d1 += 1 - 0.7f * 0.92f * cosf( el[i] * PI_OVER_180 ) - sinf( fabsf( el[i] * PI_OVER_180 ) ) * 0.7f;
4152 : }
4153 : }
4154 : }
4155 : else
4156 : {
4157 4162 : if ( bits[i] == 2 )
4158 : {
4159 4162 : d1 += 1 - cosf( el[i] * PI_OVER_180 ) * 0.7f;
4160 : }
4161 : else
4162 : {
4163 0 : d1 += 1;
4164 : }
4165 : }
4166 13656 : d2 += 1 - sinf( el_av * PI_OVER_180 ) * sinf( el[i] * PI_OVER_180 ) - cosf( el[i] * PI_OVER_180 ) * cosf( el_av * PI_OVER_180 ) * cos_delta_phi_cb[total_bits - 9];
4167 : }
4168 : }
4169 :
4170 3712 : var_el = var( el, len );
4171 3712 : if ( var_el > 1300.0f )
4172 : {
4173 118 : d2 = d1 + 0.1f;
4174 : }
4175 :
4176 3712 : *p_d1 = d1;
4177 3712 : *p_d2 = d2;
4178 :
4179 3712 : return;
4180 : }
4181 :
4182 :
4183 : /*-------------------------------------------------------------------*
4184 : * write_ec_direction()
4185 : *
4186 : * write metadata using entropy encoding
4187 : *-------------------------------------------------------------------*/
4188 :
4189 : /*! r: number of bits written */
4190 7812 : static ivas_error write_ec_direction(
4191 : int16_t *num_bits_written, /* o : Number of bits written */
4192 : BSTR_ENC_HANDLE hMetaData, /* i : MASA metadata structure */
4193 : IVAS_QDIRECTION *qdirection, /* i : quantized directional info */
4194 : const int16_t j_idx, /* i : index of subband to encode and write */
4195 : const int16_t len, /* i : number of tiles */
4196 : const int16_t GR_ord_elevation, /* i : GR order for elevation encoding */
4197 : const int16_t GR_ord_azimuth, /* i : GR order for azimuth encoding */
4198 : const int16_t use_context, /* i : flag for context usiage in azimuth encoding */
4199 : const int16_t same /* i : flag if elevation indexes are the same or not */
4200 : )
4201 : {
4202 : int16_t i, nbits, bits_crt, nr_NO_INDEX;
4203 : uint16_t data;
4204 : int16_t min_val, max_val;
4205 :
4206 7812 : nr_NO_INDEX = 0;
4207 :
4208 7812 : nbits = 0;
4209 :
4210 : /* write elevation */
4211 38928 : for ( i = 0; i < len; i++ )
4212 : {
4213 31116 : data = qdirection->band_data[j_idx].elevation_index[i];
4214 31116 : if ( ( data == MASA_NO_INDEX ) || ( qdirection->band_data[j_idx].bits_sph_idx[i] == 0 ) )
4215 : {
4216 3 : nr_NO_INDEX += 1;
4217 : }
4218 : }
4219 :
4220 7812 : if ( nr_NO_INDEX < len )
4221 : {
4222 7812 : if ( same == 1 )
4223 : {
4224 6069 : push_next_indice( hMetaData, 1, 1 );
4225 6069 : nbits += 1;
4226 6069 : push_next_indice( hMetaData, qdirection->band_data[j_idx].elevation_index[0], 2 );
4227 6069 : nbits += 2;
4228 : }
4229 : else
4230 : {
4231 1743 : push_next_indice( hMetaData, 0, 1 );
4232 1743 : nbits += 1;
4233 :
4234 1743 : push_next_indice( hMetaData, 1 - ( GR_ord_elevation == MASA_GR_ORD_EL ), 1 );
4235 1743 : nbits += 1;
4236 :
4237 8715 : for ( i = 0; i < len; i++ )
4238 : {
4239 6972 : data = qdirection->band_data[j_idx].elevation_index[i];
4240 6972 : if ( data < MASA_NO_INDEX )
4241 : {
4242 6969 : bits_crt = hMetaData->nb_bits_tot;
4243 6969 : ivas_qmetadata_encode_extended_gr( hMetaData, data, qdirection->band_data[j_idx].elevation_m_alphabet[i], GR_ord_elevation );
4244 6969 : nbits += hMetaData->nb_bits_tot - bits_crt;
4245 : }
4246 : }
4247 : }
4248 : }
4249 :
4250 : /* write azimuth */
4251 7812 : if ( use_context < 0 )
4252 : {
4253 7812 : if ( use_context == -1 )
4254 : {
4255 : /* regular GR coding */
4256 2544 : push_next_indice( hMetaData, 0, 1 );
4257 2544 : nbits += 1;
4258 2544 : push_next_indice( hMetaData, 1 - ( GR_ord_azimuth == MASA_GR_ORD_AZ ), 1 );
4259 2544 : nbits += 1;
4260 :
4261 12588 : for ( i = 0; i < len; i++ )
4262 : {
4263 10044 : data = qdirection->band_data[j_idx].azimuth_index[i];
4264 10044 : if ( data < MASA_NO_INDEX )
4265 : {
4266 10042 : bits_crt = hMetaData->nb_bits_tot;
4267 10042 : ivas_qmetadata_encode_extended_gr( hMetaData, data, qdirection->band_data[j_idx].azimuth_m_alphabet[i], GR_ord_azimuth );
4268 10042 : nbits += hMetaData->nb_bits_tot - bits_crt;
4269 : }
4270 : }
4271 : }
4272 5268 : else if ( use_context == -2 )
4273 : {
4274 : /* min removed GR coding */
4275 5268 : push_next_indice( hMetaData, 1, 1 );
4276 5268 : nbits += 1;
4277 5268 : push_next_indice( hMetaData, 1 - ( GR_ord_azimuth == MASA_GR_ORD_AZ - 1 ), 1 );
4278 5268 : nbits += 1;
4279 :
4280 : /* find min */
4281 5268 : min_val = MASA_NO_INDEX;
4282 26340 : for ( i = 0; i < len; i++ )
4283 : {
4284 21072 : if ( qdirection->band_data[j_idx].azimuth_index[i] < min_val )
4285 : {
4286 6759 : min_val = qdirection->band_data[j_idx].azimuth_index[i];
4287 : }
4288 : }
4289 :
4290 : #ifdef DEBUGGING
4291 : if ( min_val == MASA_NO_INDEX )
4292 : {
4293 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "No azimuth data to be coded in write_ec_direction()" );
4294 : }
4295 : #endif
4296 :
4297 : /* write min*/
4298 5268 : bits_crt = hMetaData->nb_bits_tot;
4299 5268 : maximum_s( qdirection->band_data[j_idx].azimuth_m_alphabet, len, &max_val );
4300 5268 : ivas_qmetadata_encode_extended_gr( hMetaData, min_val, max_val, MASA_GR_ORD_AZ );
4301 5268 : nbits += hMetaData->nb_bits_tot - bits_crt;
4302 :
4303 26340 : for ( i = 0; i < len; i++ )
4304 : {
4305 21072 : data = qdirection->band_data[j_idx].azimuth_index[i] - min_val;
4306 21072 : if ( data < MASA_NO_INDEX - min_val )
4307 : {
4308 21072 : bits_crt = hMetaData->nb_bits_tot;
4309 21072 : ivas_qmetadata_encode_extended_gr( hMetaData, data, qdirection->band_data[j_idx].azimuth_m_alphabet[i], GR_ord_azimuth );
4310 21072 : nbits += hMetaData->nb_bits_tot - bits_crt;
4311 : }
4312 : }
4313 : }
4314 : #ifdef DEBUGGING
4315 : else
4316 : {
4317 : if ( use_context != -3 )
4318 : {
4319 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong use_context value in write_ec_direction()" );
4320 : }
4321 : }
4322 : #endif
4323 : }
4324 : else
4325 : {
4326 0 : for ( i = 0; i < len; i++ )
4327 : {
4328 0 : data = qdirection->band_data[j_idx].azimuth_index[i];
4329 0 : if ( data < MASA_NO_INDEX )
4330 : {
4331 0 : switch ( qdirection->band_data[j_idx].bits_sph_idx[i] )
4332 : {
4333 0 : case 0:
4334 0 : break;
4335 0 : case 1:
4336 0 : nbits += 1;
4337 0 : push_next_indice( hMetaData, data, 1 );
4338 0 : break;
4339 0 : case 2:
4340 0 : bits_crt = hMetaData->nb_bits_tot;
4341 0 : ivas_qmetadata_encode_extended_gr( hMetaData, data, qdirection->band_data[j_idx].azimuth_m_alphabet[i], MASA_GR_ORD_AZ - 1 );
4342 0 : nbits += hMetaData->nb_bits_tot - bits_crt;
4343 0 : break;
4344 0 : default:
4345 0 : bits_crt = hMetaData->nb_bits_tot;
4346 0 : ivas_qmetadata_encode_extended_gr( hMetaData, data, qdirection->band_data[j_idx].azimuth_m_alphabet[i], MASA_GR_ORD_AZ );
4347 0 : nbits += hMetaData->nb_bits_tot - bits_crt;
4348 0 : break;
4349 : }
4350 0 : }
4351 : }
4352 : }
4353 :
4354 7812 : *num_bits_written = nbits;
4355 7812 : return IVAS_ERR_OK;
4356 : }
4357 :
4358 :
4359 : /*-----------------------------------------------------------------------*
4360 : * Local functions (coherence Q and coding)
4361 : *-----------------------------------------------------------------------*/
4362 :
4363 : /*! r: index */
4364 34360 : static uint64_t create_combined_index(
4365 : uint16_t *idx_dct, /* i : indexes to combine */
4366 : const int16_t len, /* i : number of indexes */
4367 : const int16_t *no_cb_vec /* i : how many codewords for each position */
4368 : )
4369 : {
4370 : int16_t i;
4371 : uint64_t idx, base;
4372 :
4373 34360 : base = 1;
4374 34360 : idx = 0;
4375 239367 : for ( i = 0; i < len; i++ )
4376 : {
4377 205007 : idx += base * idx_dct[i];
4378 205007 : base *= no_cb_vec[i];
4379 : }
4380 :
4381 34360 : return idx;
4382 : }
4383 :
4384 :
4385 : /*-----------------------------------------------------------------------*
4386 : * encoding DCT0 coeffs with joint index
4387 : *-----------------------------------------------------------------------*/
4388 :
4389 : /*! r: number of bits written */
4390 23237 : static int16_t encode_coherence_indexesDCT0(
4391 : uint16_t *idx_dct, /* i : indexes to be encoded */
4392 : const int16_t len, /* i : number of indexes */
4393 : int16_t *no_cb_vec, /* i : number of codewords for each position */
4394 : BSTR_ENC_HANDLE hMetaData,
4395 : const int16_t indice_coherence,
4396 : const int16_t nbits,
4397 : const int16_t nbits1 )
4398 : {
4399 : int16_t i;
4400 : uint64_t idx;
4401 : int16_t no_idx16;
4402 : int16_t k;
4403 : int16_t half_len, j;
4404 : uint64_t idx1;
4405 :
4406 : /* calculate bits for dct0 components with joint encoding */
4407 23237 : if ( nbits1 > 0 )
4408 : {
4409 174 : half_len = len / 2;
4410 174 : idx = create_combined_index( idx_dct, half_len, no_cb_vec );
4411 174 : idx1 = create_combined_index( &idx_dct[half_len], half_len, &no_cb_vec[half_len] );
4412 : }
4413 : else
4414 : {
4415 23063 : idx = create_combined_index( idx_dct, len, no_cb_vec );
4416 23063 : idx1 = 0;
4417 : }
4418 :
4419 23237 : if ( nbits % 16 == 0 )
4420 : {
4421 311 : no_idx16 = nbits / 16;
4422 : }
4423 : else
4424 : {
4425 22926 : no_idx16 = (int16_t) round_f( ( nbits / 16.0f + 0.5f ) );
4426 : }
4427 :
4428 23237 : k = nbits;
4429 23237 : i = 0;
4430 27347 : for ( i = 0; i < no_idx16 - 1; i++ )
4431 : {
4432 4110 : k -= 16;
4433 4110 : hMetaData->ind_list[indice_coherence + i].value = ( ( idx >> k ) & 65535 ); /* 16 bits */
4434 : }
4435 23237 : hMetaData->ind_list[indice_coherence + i].value = ( idx & ( ( 1 << k ) - 1 ) );
4436 :
4437 23237 : if ( nbits1 > 0 )
4438 : {
4439 174 : if ( nbits1 % 16 == 0 )
4440 : {
4441 0 : no_idx16 = nbits1 / 16;
4442 : }
4443 : else
4444 : {
4445 174 : no_idx16 = (int16_t) round_f( ( nbits1 / 16.0f + 0.5f ) );
4446 : }
4447 :
4448 174 : k = nbits1;
4449 :
4450 348 : for ( j = i + 1; j < no_idx16 + i; j++ )
4451 : {
4452 174 : k -= 16;
4453 174 : hMetaData->ind_list[indice_coherence + j].value = ( ( idx1 >> k ) & 65535 ); /* 16 bits */
4454 : }
4455 174 : hMetaData->ind_list[indice_coherence + j].value = ( idx1 & ( ( 1 << k ) - 1 ) );
4456 : }
4457 :
4458 23237 : return nbits + nbits1;
4459 : }
4460 :
4461 :
4462 : /*-------------------------------------------------------------------*
4463 : * coherence_coding_length()
4464 : *
4465 : *
4466 : *-------------------------------------------------------------------*/
4467 :
4468 42732 : static int16_t coherence_coding_length(
4469 : const uint16_t *idx_sur_coh_shift,
4470 : const uint8_t idx_shift_len,
4471 : const int16_t coding_subbands,
4472 : const int16_t *no_cv,
4473 : uint16_t *mr_idx,
4474 : int16_t *no_cv_shift,
4475 : int16_t *p_min_idx,
4476 : int16_t *GR_ord,
4477 : int16_t *nbits_fr,
4478 : int16_t *nbits_fr1 )
4479 : {
4480 : int16_t half_coding_subbands;
4481 : int16_t j;
4482 : int16_t nbits;
4483 : uint64_t no_cb;
4484 : int16_t min_idx;
4485 :
4486 42732 : half_coding_subbands = 0;
4487 :
4488 42732 : if ( sum_s( no_cv, coding_subbands ) > MASA_COH_LIMIT_2IDX )
4489 : {
4490 :
4491 2773 : no_cb = 1;
4492 2773 : half_coding_subbands = coding_subbands / 2;
4493 35382 : for ( j = 0; j < half_coding_subbands; j++ )
4494 : {
4495 32609 : no_cb *= no_cv[j];
4496 : }
4497 2773 : *nbits_fr = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
4498 2773 : no_cb = 1;
4499 35506 : for ( j = half_coding_subbands; j < coding_subbands; j++ )
4500 : {
4501 32733 : no_cb *= no_cv[j];
4502 : }
4503 2773 : *nbits_fr1 = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
4504 : }
4505 : else
4506 : {
4507 39959 : no_cb = 1;
4508 435223 : for ( j = 0; j < coding_subbands; j++ )
4509 : {
4510 395264 : no_cb *= no_cv[j];
4511 : }
4512 39959 : *nbits_fr = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
4513 39959 : *nbits_fr1 = 0;
4514 : }
4515 :
4516 :
4517 42732 : minimum_s( (const int16_t *) idx_sur_coh_shift, (int16_t) idx_shift_len, &min_idx );
4518 501523 : for ( j = 0; j < idx_shift_len; j++ )
4519 : {
4520 458791 : mr_idx[j] = idx_sur_coh_shift[j] - min_idx;
4521 458791 : no_cv_shift[j] -= min_idx;
4522 : }
4523 42732 : nbits = min_idx + 1 + GR_bits_new( mr_idx, no_cv_shift, idx_shift_len, *GR_ord, 1, GR_ord );
4524 42732 : *p_min_idx = min_idx;
4525 :
4526 42732 : return nbits;
4527 : }
4528 :
4529 :
4530 : /*-------------------------------------------------------------------*
4531 : * encode_spread_coherence_1sf()
4532 : *
4533 : * Encoding spread coherence for 1 subframe bands
4534 : *-------------------------------------------------------------------*/
4535 :
4536 : /*! r: number of bits written */
4537 8236 : static int16_t encode_spread_coherence_1sf(
4538 : IVAS_QMETADATA *q_metadata, /* i : quantized metadata */
4539 : const int16_t idx_d, /* i : current direction index */
4540 : BSTR_ENC_HANDLE hMasaMetaData, /* i/o: metadata bitstream handle */
4541 : const int16_t hrmasa_flag /* i : flag indicating high-rate MASA MD coding */
4542 : )
4543 : {
4544 : int16_t i, j, k;
4545 : int16_t idx_ER;
4546 : int16_t nbits, nbits_fr;
4547 : uint16_t idx_sp_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4548 : uint16_t mr_idx_sp_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4549 : int16_t GR_ord, bits_GR;
4550 : uint64_t idx, idx1;
4551 : int16_t no_idx16;
4552 : int16_t no_cv[MASA_MAXIMUM_CODING_SUBBANDS];
4553 : IVAS_QDIRECTION *q_direction;
4554 : int16_t half_coding_subbands, nbits_fr1, coding_subbands;
4555 : uint16_t idx_sp_coh_shift[MASA_MAXIMUM_CODING_SUBBANDS];
4556 : uint8_t idx_shift;
4557 8236 : int16_t max_val = 0, nbits_max;
4558 : int16_t extra_cv;
4559 : int16_t no_cv_shift[MASA_MAXIMUM_CODING_SUBBANDS], min_idx;
4560 :
4561 8236 : coding_subbands = q_metadata->q_direction[idx_d].cfg.nbands;
4562 8236 : q_direction = &( q_metadata->q_direction[idx_d] );
4563 8236 : nbits = 0;
4564 8236 : GR_ord = 1;
4565 8236 : idx_shift = 0;
4566 :
4567 : /* number of codevectors added dependent on number of subbands */
4568 8236 : extra_cv = coding_subbands / MASA_FACTOR_CV_COH;
4569 69984 : for ( j = 0; j < coding_subbands; j++ )
4570 : {
4571 61748 : if ( hrmasa_flag )
4572 : {
4573 0 : idx_ER = 7 - ( q_direction->band_data[j].energy_ratio_index_mod[0] >> 1 ) + extra_cv;
4574 : }
4575 : else
4576 : {
4577 61748 : idx_ER = 7 - q_direction->band_data[j].energy_ratio_index_mod[0] + extra_cv;
4578 : }
4579 :
4580 61748 : if ( idx_ER > 0 )
4581 : {
4582 47159 : idx_sp_coh[j] = (uint16_t) roundf( q_direction->coherence_band_data[j].spread_coherence[0] / ( 255.0f / (float) idx_ER ) );
4583 47159 : q_direction->coherence_band_data[j].spread_coherence[0] = (uint8_t) roundf( idx_sp_coh[j] * ( 255.0f / (float) idx_ER ) );
4584 : }
4585 : else
4586 : {
4587 14589 : idx_sp_coh[j] = 0;
4588 14589 : q_direction->coherence_band_data[j].spread_coherence[0] = 0;
4589 : }
4590 61748 : no_cv[j] = idx_ER + 1;
4591 :
4592 61748 : no_cv_shift[idx_shift] = no_cv[j];
4593 61748 : idx_sp_coh_shift[idx_shift++] = idx_sp_coh[j];
4594 : }
4595 :
4596 8236 : if ( sum_s( no_cv, coding_subbands ) == coding_subbands )
4597 : {
4598 879 : return 0;
4599 : }
4600 :
4601 7357 : nbits_max = 0;
4602 7357 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
4603 : {
4604 1962 : j = maximum_s( (int16_t *) idx_sp_coh, coding_subbands, &max_val );
4605 33582 : for ( j = 0; j < coding_subbands; j++ )
4606 : {
4607 31620 : if ( no_cv[j] > max_val + 1 )
4608 : {
4609 30448 : no_cv[j] = max_val + 1;
4610 : }
4611 : }
4612 1962 : nbits_max = MASA_MAX_NO_CV_SUR_COH - max_val + extra_cv;
4613 : }
4614 :
4615 7357 : nbits = coherence_coding_length( idx_sp_coh_shift, idx_shift, coding_subbands, no_cv,
4616 : mr_idx_sp_coh, no_cv_shift, &min_idx, &GR_ord, &nbits_fr, &nbits_fr1 );
4617 7357 : half_coding_subbands = 0;
4618 7357 : idx1 = 0;
4619 :
4620 7357 : if ( nbits_fr + nbits_fr1 + nbits_max < nbits )
4621 : {
4622 : /* write flag*/
4623 4875 : push_next_indice( hMasaMetaData, 0, 1 );
4624 :
4625 : /* create combined index */
4626 4875 : nbits = nbits_fr + nbits_fr1 + 1;
4627 :
4628 4875 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
4629 : {
4630 : /* write max value*/
4631 179 : bits_GR = hMasaMetaData->nb_bits_tot;
4632 179 : ivas_qmetadata_encode_extended_gr( hMasaMetaData, MASA_MAX_NO_CV_SUR_COH - max_val - 1 + extra_cv, MASA_MAX_NO_CV_SUR_COH + extra_cv, 0 );
4633 179 : nbits += hMasaMetaData->nb_bits_tot - bits_GR;
4634 : }
4635 :
4636 4875 : if ( nbits_fr1 > 0 )
4637 : {
4638 24 : half_coding_subbands = coding_subbands / 2;
4639 24 : idx = create_combined_index( idx_sp_coh, half_coding_subbands, no_cv );
4640 24 : idx1 = create_combined_index( &idx_sp_coh[half_coding_subbands], half_coding_subbands, &no_cv[half_coding_subbands] );
4641 : }
4642 : else
4643 : {
4644 4851 : idx = create_combined_index( idx_sp_coh, coding_subbands, no_cv );
4645 : }
4646 :
4647 4875 : if ( nbits_fr % 16 == 0 )
4648 : {
4649 88 : no_idx16 = nbits_fr / 16;
4650 : }
4651 : else
4652 : {
4653 4787 : no_idx16 = (int16_t) round_f( ( nbits_fr / 16.0f + 0.5f ) );
4654 : }
4655 :
4656 : /* write combined index */
4657 4875 : k = nbits_fr;
4658 4988 : for ( i = 0; i < no_idx16 - 1; i++ )
4659 : {
4660 113 : k -= 16;
4661 113 : push_next_indice( hMasaMetaData, ( ( idx >> k ) & 65535 ), 16 ); /* 16 bits */
4662 : }
4663 :
4664 4875 : push_next_indice( hMasaMetaData, ( idx & ( ( 1 << k ) - 1 ) ), k );
4665 :
4666 4875 : if ( nbits_fr1 > 0 )
4667 : {
4668 24 : if ( nbits_fr1 % 16 == 0 )
4669 : {
4670 0 : no_idx16 = nbits_fr1 / 16;
4671 : }
4672 : else
4673 : {
4674 24 : no_idx16 = (int16_t) round_f( ( nbits_fr1 / 16.0f + 0.5f ) );
4675 : }
4676 :
4677 24 : assert( no_idx16 <= 4 );
4678 :
4679 24 : k = nbits_fr1;
4680 56 : for ( i = 0; i < no_idx16 - 1; i++ )
4681 : {
4682 32 : k -= 16;
4683 32 : push_next_indice( hMasaMetaData, ( ( idx1 >> k ) & 65535 ), 16 ); /* 16 bits */
4684 : }
4685 24 : push_next_indice( hMasaMetaData, ( idx1 & ( ( 1 << k ) - 1 ) ), k );
4686 : }
4687 : }
4688 : else
4689 : {
4690 : /* write flag */
4691 2482 : nbits = 1;
4692 :
4693 : /* write flag*/
4694 2482 : push_next_indice( hMasaMetaData, 1, 1 );
4695 :
4696 : /* write GR_ord */
4697 2482 : push_next_indice( hMasaMetaData, GR_ord, 1 );
4698 2482 : nbits += 1;
4699 :
4700 : /* write the min */
4701 2482 : bits_GR = hMasaMetaData->nb_bits_tot;
4702 2482 : ivas_qmetadata_encode_extended_gr( hMasaMetaData, min_idx, MASA_MAX_NO_CV_SUR_COH + extra_cv, 0 );
4703 2482 : nbits += hMasaMetaData->nb_bits_tot - bits_GR;
4704 :
4705 : /* write GR data */
4706 34498 : for ( j = 0; j < idx_shift; j++ )
4707 : {
4708 32016 : bits_GR = hMasaMetaData->nb_bits_tot;
4709 32016 : ivas_qmetadata_encode_extended_gr( hMasaMetaData, mr_idx_sp_coh[j], no_cv_shift[j], GR_ord );
4710 32016 : nbits += hMasaMetaData->nb_bits_tot - bits_GR;
4711 : }
4712 : }
4713 :
4714 7357 : return nbits;
4715 : }
4716 :
4717 :
4718 : /*-------------------------------------------------------------------*
4719 : * encode_surround_coherence()
4720 : *
4721 : * encoding surround coherence
4722 : *-------------------------------------------------------------------*/
4723 :
4724 : /*! r: number of bits written */
4725 26721 : static int16_t encode_surround_coherence(
4726 : IVAS_QMETADATA *hQMetaData, /* i : quantized metadata */
4727 : BSTR_ENC_HANDLE hMetaData /* i/o: metadata bitstream handle */
4728 : )
4729 : {
4730 : int16_t i, j, k;
4731 : int16_t idx_ER, idx16;
4732 : int16_t nbits, nbits_fr;
4733 : uint16_t idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4734 : uint16_t mr_idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4735 : int16_t GR_ord, bits_GR;
4736 : uint64_t idx, idx1;
4737 : int16_t no_idx16;
4738 : int16_t no_cv[MASA_MAXIMUM_CODING_SUBBANDS];
4739 : float error_ratio_surr;
4740 : IVAS_QDIRECTION *q_direction;
4741 : int16_t half_coding_subbands, nbits_fr1, coding_subbands;
4742 : int16_t all_coherence_zero;
4743 : uint16_t idx_sur_coh_shift[MASA_MAXIMUM_CODING_SUBBANDS];
4744 : uint8_t idx_shift;
4745 26721 : int16_t max_val = 0, nbits_max;
4746 : int16_t no_cv_shift[MASA_MAXIMUM_CODING_SUBBANDS], min_idx;
4747 :
4748 26721 : coding_subbands = hQMetaData->q_direction[0].cfg.nbands;
4749 26721 : all_coherence_zero = hQMetaData->all_coherence_zero;
4750 26721 : q_direction = &( hQMetaData->q_direction[0] );
4751 26721 : nbits = 0;
4752 :
4753 26721 : if ( all_coherence_zero == 1 )
4754 : {
4755 0 : nbits = 0;
4756 : }
4757 : else
4758 : {
4759 26721 : GR_ord = 1;
4760 26721 : k = 0;
4761 26721 : idx_shift = 0;
4762 222713 : for ( j = 0; j < coding_subbands; j++ )
4763 : {
4764 195992 : if ( hQMetaData->no_directions == 2 )
4765 : {
4766 39552 : k += hQMetaData->twoDirBands[j];
4767 39552 : idx16 = max( k - 1, 0 );
4768 39552 : error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[0] - q_direction[1].band_data[idx16].energy_ratio[0] * hQMetaData->twoDirBands[j];
4769 : }
4770 : else
4771 : {
4772 156440 : error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[0];
4773 : }
4774 :
4775 195992 : if ( error_ratio_surr <= 0 )
4776 : {
4777 171 : error_ratio_surr = 0;
4778 171 : idx_sur_coh[j] = 0;
4779 171 : no_cv[j] = 1;
4780 171 : hQMetaData->surcoh_band_data[j].surround_coherence[0] = 0; /* sur_coherence_cb_masa[idx_cb_sur_coh_masa[DIRAC_DIFFUSE_LEVELS - 1] * MASA_MAX_NO_CV_SUR_COH]; */
4781 : }
4782 : else
4783 : {
4784 195821 : idx_ER = masa_sq( error_ratio_surr, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
4785 :
4786 391642 : idx_sur_coh[j] = squant_int( hQMetaData->surcoh_band_data[j].surround_coherence[0], &hQMetaData->surcoh_band_data[j].surround_coherence[0],
4787 195821 : &sur_coherence_cb_masa[idx_cb_sur_coh_masa[idx_ER] * MASA_MAX_NO_CV_SUR_COH], idx_cb_sur_coh_masa[idx_ER] + 2 );
4788 :
4789 195821 : no_cv[j] = idx_cb_sur_coh_masa[idx_ER] + 2;
4790 195821 : no_cv_shift[idx_shift] = no_cv[j];
4791 195821 : idx_sur_coh_shift[idx_shift++] = idx_sur_coh[j];
4792 : }
4793 : }
4794 :
4795 26721 : if ( sum_s( no_cv, coding_subbands ) == coding_subbands )
4796 : {
4797 0 : return 0;
4798 : }
4799 :
4800 26721 : nbits_max = 0;
4801 26721 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
4802 : {
4803 4855 : j = maximum_s( (int16_t *) idx_sur_coh, coding_subbands, &max_val );
4804 89707 : for ( j = 0; j < coding_subbands; j++ )
4805 : {
4806 84852 : if ( no_cv[j] > max_val + 1 )
4807 : {
4808 70827 : no_cv[j] = max_val + 1;
4809 : }
4810 : }
4811 4855 : nbits_max = MASA_MAX_NO_CV_SUR_COH - max_val; /* encoded with GR0 as max_no_vals - no_vals*/
4812 : }
4813 :
4814 26721 : nbits = coherence_coding_length( idx_sur_coh_shift, idx_shift, coding_subbands, no_cv,
4815 : mr_idx_sur_coh, no_cv_shift, &min_idx, &GR_ord, &nbits_fr, &nbits_fr1 );
4816 26721 : half_coding_subbands = coding_subbands / 2;
4817 26721 : idx1 = 0;
4818 :
4819 : /* should check how to encode the average - check distribution */
4820 26721 : if ( nbits_fr + nbits_fr1 + nbits_max < nbits )
4821 : {
4822 : /* write flag*/
4823 5765 : push_next_indice( hMetaData, 0, 1 );
4824 :
4825 : /* create combined index */
4826 5765 : nbits = nbits_fr + nbits_fr1 + 1;
4827 5765 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
4828 : {
4829 : /* write max value*/
4830 319 : bits_GR = hMetaData->nb_bits_tot;
4831 319 : ivas_qmetadata_encode_extended_gr( hMetaData, MASA_MAX_NO_CV_SUR_COH - max_val - 1, MASA_MAX_NO_CV_SUR_COH, 0 );
4832 319 : nbits += hMetaData->nb_bits_tot - bits_GR;
4833 : }
4834 :
4835 5765 : if ( nbits_fr1 > 0 )
4836 : {
4837 40 : idx = create_combined_index( idx_sur_coh, half_coding_subbands, no_cv );
4838 40 : idx1 = create_combined_index( &idx_sur_coh[half_coding_subbands], half_coding_subbands, &no_cv[half_coding_subbands] );
4839 : }
4840 : else
4841 : {
4842 5725 : idx = create_combined_index( idx_sur_coh, coding_subbands, no_cv );
4843 : }
4844 :
4845 5765 : if ( nbits_fr % 16 == 0 )
4846 : {
4847 205 : no_idx16 = nbits_fr / 16;
4848 : }
4849 : else
4850 : {
4851 5560 : no_idx16 = (int16_t) round_f( ( nbits_fr / 16.0f + 0.5f ) );
4852 : }
4853 :
4854 : /* write combined index */
4855 5765 : k = nbits_fr;
4856 5924 : for ( i = 0; i < no_idx16 - 1; i++ )
4857 : {
4858 159 : k -= 16;
4859 159 : push_next_indice( hMetaData, ( ( idx >> k ) & 65535 ), 16 ); /* 16 bits */
4860 : }
4861 :
4862 5765 : push_next_indice( hMetaData, ( idx & ( ( 1 << k ) - 1 ) ), k );
4863 :
4864 5765 : if ( nbits_fr1 > 0 )
4865 : {
4866 40 : if ( nbits_fr1 % 16 == 0 )
4867 : {
4868 0 : no_idx16 = nbits_fr1 / 16;
4869 : }
4870 : else
4871 : {
4872 40 : no_idx16 = (int16_t) round_f( ( nbits_fr1 / 16.0f + 0.5f ) );
4873 : }
4874 :
4875 40 : assert( no_idx16 <= 4 );
4876 :
4877 40 : k = nbits_fr1;
4878 106 : for ( i = 0; i < no_idx16 - 1; i++ )
4879 : {
4880 66 : k -= 16;
4881 66 : push_next_indice( hMetaData, ( ( idx1 >> k ) & 65535 ), 16 ); /* 16 bits */
4882 : }
4883 :
4884 40 : push_next_indice( hMetaData, ( idx1 & ( ( 1 << k ) - 1 ) ), k );
4885 : }
4886 : }
4887 : else
4888 : {
4889 : /* write flag */
4890 20956 : nbits = 1;
4891 :
4892 : /* write flag*/
4893 20956 : push_next_indice( hMetaData, 1, 1 );
4894 :
4895 : /* write GR_ord */
4896 20956 : push_next_indice( hMetaData, GR_ord, 1 );
4897 20956 : nbits += 1;
4898 :
4899 : /* write the min */
4900 20956 : bits_GR = hMetaData->nb_bits_tot;
4901 20956 : ivas_qmetadata_encode_extended_gr( hMetaData, min_idx, MASA_MAX_NO_CV_SUR_COH, 0 );
4902 20956 : nbits += hMetaData->nb_bits_tot - bits_GR;
4903 :
4904 : /* write GR data */
4905 189920 : for ( j = 0; j < idx_shift; j++ )
4906 : {
4907 168964 : bits_GR = hMetaData->nb_bits_tot;
4908 :
4909 168964 : ivas_qmetadata_encode_extended_gr( hMetaData, mr_idx_sur_coh[j], no_cv_shift[j], GR_ord );
4910 :
4911 168964 : nbits += hMetaData->nb_bits_tot - bits_GR;
4912 : }
4913 : }
4914 : }
4915 : #ifdef DEBUGGING
4916 : for ( i = 0; i < coding_subbands; i++ )
4917 : {
4918 : for ( j = 1; j < q_direction->cfg.nblocks; j++ )
4919 : {
4920 : hQMetaData->surcoh_band_data[i].surround_coherence[j] = hQMetaData->surcoh_band_data[i].surround_coherence[0];
4921 : }
4922 : }
4923 : #endif
4924 :
4925 26721 : return nbits;
4926 : }
4927 :
4928 :
4929 2276 : static int16_t encode_surround_coherence_hr(
4930 : IVAS_QMETADATA *hQMetaData, /* i : quantized metadata */
4931 : BSTR_ENC_HANDLE hMetaData /* i/o: metadata bitstream handle */
4932 : )
4933 : {
4934 : int16_t i, j, k, sf;
4935 : int16_t nbits, nbits_fr, nbits_sf;
4936 : uint16_t idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4937 : uint16_t mr_idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4938 : int16_t GR_ord, bits_GR;
4939 : uint64_t idx, idx1;
4940 : int16_t no_idx16;
4941 : int16_t no_cv[MASA_MAXIMUM_CODING_SUBBANDS];
4942 : float error_ratio_surr;
4943 : IVAS_QDIRECTION *q_direction;
4944 : int16_t half_coding_subbands, nbits_fr1, coding_subbands;
4945 : int16_t all_coherence_zero;
4946 : uint16_t idx_sur_coh_shift[MASA_MAXIMUM_CODING_SUBBANDS];
4947 : uint8_t idx_shift;
4948 2276 : int16_t max_val = 0, nbits_max;
4949 : int16_t no_cv_shift[MASA_MAXIMUM_CODING_SUBBANDS], min_idx;
4950 : int16_t idx16;
4951 : int32_t int_error_ratio_surr;
4952 :
4953 2276 : coding_subbands = hQMetaData->q_direction[0].cfg.nbands;
4954 2276 : all_coherence_zero = hQMetaData->all_coherence_zero;
4955 2276 : q_direction = &( hQMetaData->q_direction[0] );
4956 2276 : nbits = 0;
4957 :
4958 2276 : if ( all_coherence_zero == 1 )
4959 : {
4960 0 : nbits = 0;
4961 : }
4962 : else
4963 : {
4964 10930 : for ( sf = 0; sf < hQMetaData->q_direction[0].cfg.nblocks; sf++ )
4965 : {
4966 8654 : GR_ord = 1;
4967 8654 : k = 0;
4968 8654 : idx_shift = 0;
4969 215600 : for ( j = 0; j < coding_subbands; j++ )
4970 : {
4971 206946 : if ( hQMetaData->no_directions == 2 )
4972 : {
4973 109026 : k += hQMetaData->twoDirBands[j];
4974 109026 : idx16 = max( k - 1, 0 );
4975 109026 : error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[sf] - q_direction[1].band_data[idx16].energy_ratio[sf] * hQMetaData->twoDirBands[j];
4976 : }
4977 : else
4978 : {
4979 97920 : error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[sf];
4980 : }
4981 :
4982 206946 : int_error_ratio_surr = (int32_t) ( MASA_SUR_COH_PRECISION * error_ratio_surr );
4983 206946 : error_ratio_surr = (float) ( int_error_ratio_surr * MASA_SUR_COH_THRESHOLD );
4984 :
4985 206946 : if ( error_ratio_surr <= 0 )
4986 : {
4987 1644 : error_ratio_surr = 0;
4988 1644 : idx_sur_coh[j] = 0;
4989 1644 : no_cv[j] = 1;
4990 1644 : hQMetaData->surcoh_band_data[j].surround_coherence[sf] = 0; /* sur_coherence_cb_masa[idx_cb_sur_coh_masa[DIRAC_DIFFUSE_LEVELS - 1] * MASA_MAX_NO_CV_SUR_COH]; */
4991 : }
4992 : else
4993 : {
4994 410604 : idx_sur_coh[j] = squant_int( hQMetaData->surcoh_band_data[j].surround_coherence[sf], &hQMetaData->surcoh_band_data[j].surround_coherence[sf],
4995 205302 : &sur_coherence_cb_masa[idx_cb_sur_coh_masa[7] * MASA_MAX_NO_CV_SUR_COH], idx_cb_sur_coh_masa[7] + 2 );
4996 205302 : no_cv[j] = idx_cb_sur_coh_masa[7] + 2;
4997 205302 : no_cv_shift[idx_shift] = no_cv[j];
4998 205302 : idx_sur_coh_shift[idx_shift++] = idx_sur_coh[j];
4999 : }
5000 : }
5001 :
5002 8654 : if ( sum_s( no_cv, coding_subbands ) != coding_subbands )
5003 : {
5004 8654 : nbits_max = 0;
5005 8654 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
5006 : {
5007 8654 : j = maximum_s( (int16_t *) idx_sur_coh, coding_subbands, &max_val );
5008 215600 : for ( j = 0; j < coding_subbands; j++ )
5009 : {
5010 206946 : if ( no_cv[j] > max_val + 1 )
5011 : {
5012 205302 : no_cv[j] = max_val + 1;
5013 : }
5014 : }
5015 8654 : nbits_max = MASA_MAX_NO_CV_SUR_COH - max_val; /* encoded with GR0 as max_no_vals - no_vals*/
5016 : }
5017 8654 : if ( max_val == 0 )
5018 : {
5019 74 : for ( j = 0; j < coding_subbands; j++ )
5020 : {
5021 71 : hQMetaData->surcoh_band_data[j].surround_coherence[sf] = 0;
5022 : }
5023 : }
5024 8654 : nbits_sf = coherence_coding_length( idx_sur_coh_shift, idx_shift, coding_subbands, no_cv,
5025 : mr_idx_sur_coh, no_cv_shift, &min_idx, &GR_ord, &nbits_fr, &nbits_fr1 );
5026 8654 : half_coding_subbands = coding_subbands / 2;
5027 8654 : idx1 = 0;
5028 :
5029 : /* should check how to encode the average - check distribution */
5030 8654 : if ( nbits_fr + nbits_fr1 + nbits_max < nbits_sf )
5031 : {
5032 : /* write flag*/
5033 177 : push_next_indice( hMetaData, 0, 1 );
5034 :
5035 : /* create combined index */
5036 177 : nbits += nbits_fr + nbits_fr1 + 1;
5037 177 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
5038 : {
5039 : /* write max value*/
5040 177 : bits_GR = hMetaData->nb_bits_tot;
5041 177 : ivas_qmetadata_encode_extended_gr( hMetaData, MASA_MAX_NO_CV_SUR_COH - max_val - 1, MASA_MAX_NO_CV_SUR_COH, 0 );
5042 177 : nbits += hMetaData->nb_bits_tot - bits_GR;
5043 : }
5044 :
5045 177 : if ( nbits_fr1 > 0 )
5046 : {
5047 68 : idx = create_combined_index( idx_sur_coh, half_coding_subbands, no_cv );
5048 68 : idx1 = create_combined_index( &idx_sur_coh[half_coding_subbands], half_coding_subbands, &no_cv[half_coding_subbands] );
5049 : }
5050 : else
5051 : {
5052 109 : idx = create_combined_index( idx_sur_coh, coding_subbands, no_cv );
5053 : }
5054 :
5055 177 : if ( nbits_fr % 16 == 0 )
5056 : {
5057 23 : no_idx16 = nbits_fr / 16;
5058 : }
5059 : else
5060 : {
5061 154 : no_idx16 = (int16_t) round_f( ( nbits_fr / 16.0f + 0.5f ) );
5062 : }
5063 :
5064 : /* write combined index */
5065 177 : k = nbits_fr;
5066 376 : for ( i = 0; i < no_idx16 - 1; i++ )
5067 : {
5068 199 : k -= 16;
5069 199 : push_next_indice( hMetaData, ( ( idx >> k ) & 65535 ), 16 ); /* 16 bits */
5070 : }
5071 :
5072 177 : push_next_indice( hMetaData, ( idx & ( ( 1 << k ) - 1 ) ), k );
5073 :
5074 177 : if ( nbits_fr1 > 0 )
5075 : {
5076 68 : if ( nbits_fr1 % 16 == 0 )
5077 : {
5078 34 : no_idx16 = nbits_fr1 / 16;
5079 : }
5080 : else
5081 : {
5082 34 : no_idx16 = (int16_t) round_f( ( nbits_fr1 / 16.0f + 0.5f ) );
5083 : }
5084 :
5085 68 : assert( no_idx16 <= 4 );
5086 :
5087 68 : k = nbits_fr1;
5088 163 : for ( i = 0; i < no_idx16 - 1; i++ )
5089 : {
5090 95 : k -= 16;
5091 95 : push_next_indice( hMetaData, ( ( idx1 >> k ) & 65535 ), 16 ); /* 16 bits */
5092 : }
5093 :
5094 68 : push_next_indice( hMetaData, ( idx1 & ( ( 1 << k ) - 1 ) ), k );
5095 : }
5096 : }
5097 : else
5098 : {
5099 : /* write flag */
5100 8477 : nbits += 1;
5101 :
5102 : /* write flag*/
5103 8477 : push_next_indice( hMetaData, 1, 1 );
5104 :
5105 : /* write GR_ord */
5106 8477 : push_next_indice( hMetaData, GR_ord, 1 );
5107 8477 : nbits += 1;
5108 :
5109 : /* write the min */
5110 8477 : bits_GR = hMetaData->nb_bits_tot;
5111 8477 : ivas_qmetadata_encode_extended_gr( hMetaData, min_idx, MASA_MAX_NO_CV_SUR_COH, 0 );
5112 8477 : nbits += hMetaData->nb_bits_tot - bits_GR;
5113 :
5114 : /* write GR data */
5115 209609 : for ( j = 0; j < idx_shift; j++ )
5116 : {
5117 201132 : bits_GR = hMetaData->nb_bits_tot;
5118 :
5119 201132 : ivas_qmetadata_encode_extended_gr( hMetaData, mr_idx_sur_coh[j], no_cv_shift[j], GR_ord );
5120 :
5121 201132 : nbits += hMetaData->nb_bits_tot - bits_GR;
5122 : }
5123 : }
5124 : }
5125 : }
5126 : }
5127 :
5128 2276 : return nbits;
5129 : }
5130 :
5131 :
5132 : /*-------------------------------------------------------------------*
5133 : * quantize_DCT_0_coh()
5134 : *
5135 : * quanization of DCT component of order zero for transformed coherence vector
5136 : *-------------------------------------------------------------------*/
5137 :
5138 : /*! r: quantized value */
5139 148304 : static float quantize_DCT_0_coh(
5140 : const float x, /* i : input value */
5141 : const int16_t j, /* i : subband index */
5142 : const float *coherence_cb, /* i : coherence codebook */
5143 : const float delta_var, /* i : azimuth variance threshold */
5144 : const int16_t no_cb, /* i : maximum number of codewords */
5145 : IVAS_QDIRECTION *q_direction, /* i : quantized metadata */
5146 : uint16_t *idx_x, /* o : codewords index */
5147 : int16_t *p_no_cb, /* o : actual number of codewords dependent on energy ratio value */
5148 : const int16_t hrmasa_flag /* i : flag indicating high-rate MASA MD coding */
5149 : )
5150 : {
5151 : float var_azi, xhat;
5152 : int16_t idx_sub_cb, idx;
5153 : int16_t min_index;
5154 : /* quantize first DCT component */
5155 148304 : var_azi = var( q_direction->band_data[j].azimuth, q_direction->cfg.nblocks );
5156 :
5157 148304 : if ( hrmasa_flag )
5158 : {
5159 0 : minimum_s( (int16_t *) ( q_direction->band_data[j].energy_ratio_index ), q_direction->cfg.nblocks, &min_index );
5160 0 : min_index = min_index >> 1;
5161 : }
5162 : else
5163 : {
5164 148304 : min_index = q_direction->band_data[j].energy_ratio_index[0];
5165 : }
5166 :
5167 148304 : if ( var_azi < delta_var )
5168 : {
5169 36893 : idx_sub_cb = no_cb * min_index;
5170 : }
5171 : else
5172 : {
5173 111411 : idx_sub_cb = no_cb * ( min_index + DIRAC_DIFFUSE_LEVELS );
5174 : }
5175 :
5176 148304 : idx = squant( x, &xhat, &coherence_cb[idx_sub_cb], len_cb_dct0_masa[min_index] );
5177 :
5178 148304 : *p_no_cb = len_cb_dct0_masa[min_index];
5179 148304 : *idx_x = idx;
5180 :
5181 148304 : return xhat;
5182 : }
5183 :
5184 :
5185 : /*-------------------------------------------------------------------*
5186 : * encode_coherence_indexesDCT1()
5187 : *
5188 : * Encoding DCT1 coeffs with joint index or EC
5189 : *-------------------------------------------------------------------*/
5190 :
5191 : /*! r: number of bits written */
5192 23237 : static int16_t encode_coherence_indexesDCT1(
5193 : uint16_t *idx_dct, /* i : data to be encoded */
5194 : const int16_t len, /* i : number of data */
5195 : BSTR_ENC_HANDLE hMetaData /* i : metadata handle */
5196 : )
5197 : {
5198 : int16_t i, nbits, GR_ord;
5199 : uint16_t av;
5200 : uint16_t mr_idx_dct[MASA_MAXIMUM_CODING_SUBBANDS];
5201 :
5202 23237 : GR_ord = 0;
5203 23237 : nbits = 0;
5204 :
5205 23237 : nbits = mean_removed_GR_new( idx_dct, MASA_NO_CV_COH1, len, 0, &GR_ord, &av, mr_idx_dct );
5206 :
5207 171541 : for ( i = 0; i < len; i++ )
5208 : {
5209 148304 : ivas_qmetadata_encode_extended_gr( hMetaData, mr_idx_dct[i], 2 * MASA_NO_CV_COH1, GR_ord );
5210 : }
5211 :
5212 23237 : nbits += len_huf_masa[av];
5213 :
5214 23237 : push_next_indice( hMetaData, huff_code_av_masa[av], len_huf_masa[av] );
5215 :
5216 23237 : return nbits;
5217 : }
5218 :
5219 :
5220 : /*-------------------------------------------------------------------*
5221 : * dct4_transform()
5222 : *
5223 : * 4D implementation of DCT transform
5224 : *-------------------------------------------------------------------*/
5225 :
5226 296608 : static void dct4_transform(
5227 : uint8_t *v, /* i : input 4D vector */
5228 : float *dct_v /* o : output transformed vector */
5229 : )
5230 : {
5231 : float a, b, c, d;
5232 :
5233 296608 : a = ( v[0] + v[3] ) / 256.0f;
5234 296608 : b = ( v[1] + v[2] ) / 256.0f;
5235 296608 : c = ( v[0] - v[3] ) / 256.0f;
5236 296608 : d = ( v[1] - v[2] ) / 256.0f;
5237 :
5238 296608 : dct_v[0] = 0.5f * ( a + b );
5239 296608 : dct_v[1] = 0.653281482438188f * c + 0.270598050073099f * d;
5240 296608 : dct_v[2] = 0.5f * ( a - b );
5241 296608 : dct_v[3] = 0.270598050073099f * c - 0.653281482438188f * d;
5242 :
5243 296608 : return;
5244 : }
5245 :
5246 :
5247 : /*-------------------------------------------------------------------*
5248 : * ivas_qmetadata_quantize_coherence_hr_512()
5249 : *
5250 : *
5251 : *-------------------------------------------------------------------*/
5252 :
5253 3532 : static int16_t ivas_qmetadata_quantize_coherence_hr_512(
5254 : IVAS_QMETADATA *hQMetaData, /* i/o: quantized metadata */
5255 : const int16_t idx_d, /* i : current direction index */
5256 : const int16_t all_coherence_zero, /* i : all coherence is zero - flag */
5257 : BSTR_ENC_HANDLE hMetaData, /* i : metadata handle */
5258 : const int16_t bits_coh )
5259 : {
5260 : int16_t j, k;
5261 : int16_t nbands, nblocks;
5262 : int16_t nbits;
5263 : int16_t nbits1, nbits0, nbits_av;
5264 : uint16_t idx_coh[MASA_MAXIMUM_CODING_SUBBANDS];
5265 : IVAS_QDIRECTION *q_direction;
5266 : int16_t cbsize;
5267 : float delta, tmp;
5268 : int16_t min_idx, GR_param, GR_param_av;
5269 : uint16_t av, mr_idx[MASA_MAXIMUM_CODING_SUBBANDS];
5270 :
5271 3532 : q_direction = &( hQMetaData->q_direction[idx_d] );
5272 3532 : nbands = q_direction->cfg.nbands;
5273 3532 : nblocks = q_direction->cfg.nblocks;
5274 3532 : nbits = 0;
5275 :
5276 3532 : if ( all_coherence_zero == 1 )
5277 : {
5278 0 : return nbits;
5279 : }
5280 3532 : nbits = hMetaData->nb_bits_tot;
5281 :
5282 3532 : cbsize = 1 << bits_coh;
5283 3532 : delta = 256.0f / cbsize;
5284 :
5285 16760 : for ( k = 0; k < nblocks; k++ )
5286 : {
5287 13228 : min_idx = 0;
5288 284590 : for ( j = 0; j < nbands; j++ )
5289 : {
5290 271362 : idx_coh[j] = usquant( (float) ( q_direction->coherence_band_data[j].spread_coherence[k] ), &tmp, delta / 2.0f, delta, cbsize );
5291 271362 : q_direction->coherence_band_data[j].spread_coherence[k] = (uint8_t) ( idx_coh[j] * delta + delta / 2.0f );
5292 271362 : if ( idx_coh[j] < min_idx )
5293 : {
5294 0 : min_idx = idx_coh[j];
5295 : }
5296 : }
5297 :
5298 13228 : nbits0 = 0;
5299 13228 : nbits1 = 0;
5300 284590 : for ( j = 0; j < nbands; j++ )
5301 : {
5302 271362 : idx_coh[j] = idx_coh[j] - min_idx;
5303 271362 : nbits0 += ivas_qmetadata_encode_extended_gr_length( idx_coh[j], cbsize - min_idx, 0 );
5304 271362 : nbits1 += ivas_qmetadata_encode_extended_gr_length( idx_coh[j], cbsize - min_idx, 1 );
5305 : }
5306 13228 : if ( nbits0 < nbits1 )
5307 : {
5308 12746 : GR_param = 0;
5309 12746 : nbits1 = nbits0;
5310 : }
5311 : else
5312 : {
5313 482 : GR_param = 1;
5314 : }
5315 :
5316 13228 : GR_param_av = 1;
5317 13228 : nbits_av = mean_removed_GR_new( idx_coh, cbsize, nbands, 1, &GR_param_av, &av, mr_idx );
5318 :
5319 13228 : if ( nbits_av < nbits1 )
5320 : {
5321 141 : nbits1 = nbits_av;
5322 141 : GR_param = GR_param_av;
5323 :
5324 : /* use average removed */
5325 141 : push_next_indice( hMetaData, 1, 1 );
5326 :
5327 : /* write average */
5328 141 : push_next_indice( hMetaData, av, bits_coh );
5329 :
5330 : /* write GR param */
5331 141 : push_next_indice( hMetaData, GR_param, 1 );
5332 :
5333 1938 : for ( j = 0; j < nbands; j++ )
5334 : {
5335 1797 : ivas_qmetadata_encode_extended_gr( hMetaData, mr_idx[j], 2 * cbsize, GR_param );
5336 : }
5337 : }
5338 : else
5339 : {
5340 : /* use min removed */
5341 13087 : push_next_indice( hMetaData, 0, 1 );
5342 :
5343 : /* write min index */
5344 13087 : push_next_indice( hMetaData, min_idx, bits_coh );
5345 :
5346 : /* write GR param */
5347 13087 : push_next_indice( hMetaData, GR_param, 1 );
5348 :
5349 282652 : for ( j = 0; j < nbands; j++ )
5350 : {
5351 269565 : ivas_qmetadata_encode_extended_gr( hMetaData, idx_coh[j], cbsize - min_idx, GR_param );
5352 : }
5353 : }
5354 : }
5355 :
5356 3532 : nbits = hMetaData->nb_bits_tot - nbits;
5357 3532 : return nbits;
5358 : }
5359 :
5360 :
5361 : /*-------------------------------------------------------------------*
5362 : * ivas_qmetadata_quantize_coherence()
5363 : *
5364 : *
5365 : *-------------------------------------------------------------------*/
5366 :
5367 : /*! r: number of bits written */
5368 54710 : static int16_t ivas_qmetadata_quantize_coherence(
5369 : IVAS_QMETADATA *hQMetaData, /* i/o: quantized metadata */
5370 : const int16_t idx_d, /* i : current direction index */
5371 : const int16_t all_coherence_zero, /* i : all coherence is zero - flag */
5372 : BSTR_ENC_HANDLE hMetaData, /* i : metadata handle */
5373 : const int16_t write_flag, /* i : flag to actually write the data or not */
5374 : int16_t *indice_coherence,
5375 : const int16_t hrmasa_flag /* i : flag indicating high-rate MASA MD coding */
5376 : )
5377 : {
5378 : int16_t j, k;
5379 : float dct_coh[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
5380 : uint16_t idx_dct[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
5381 : int16_t coding_subbands;
5382 : int16_t nbits;
5383 : uint64_t no_cb;
5384 : int16_t MASA_grouping[MASA_MAXIMUM_CODING_SUBBANDS];
5385 : int16_t nbits1;
5386 : int16_t coding_subbands_0, d;
5387 : int16_t two_dir_band[MASA_MAXIMUM_CODING_SUBBANDS];
5388 : int16_t no_cb_vec[MASA_MAXIMUM_CODING_SUBBANDS];
5389 : IVAS_QDIRECTION *q_direction;
5390 : int16_t min_index;
5391 54710 : min_index = 0;
5392 54710 : q_direction = &( hQMetaData->q_direction[idx_d] );
5393 54710 : coding_subbands = q_direction->cfg.nbands;
5394 54710 : nbits = 0;
5395 :
5396 54710 : if ( all_coherence_zero == 1 )
5397 : {
5398 0 : return nbits;
5399 : }
5400 :
5401 54710 : if ( hQMetaData->q_direction[idx_d].cfg.nblocks == 1 )
5402 : {
5403 8236 : nbits = encode_spread_coherence_1sf( hQMetaData, idx_d, hMetaData, hrmasa_flag );
5404 :
5405 8236 : return nbits;
5406 : }
5407 : else
5408 : {
5409 46474 : k = 0;
5410 46474 : no_cb = 1;
5411 46474 : coding_subbands_0 = hQMetaData->q_direction[0].cfg.nbands;
5412 46474 : if ( coding_subbands_0 <= 5 )
5413 : {
5414 204672 : for ( j = 0; j < 5; j++ )
5415 : {
5416 170560 : MASA_grouping[j] = j;
5417 : }
5418 : }
5419 : else
5420 : {
5421 12362 : if ( coding_subbands_0 <= 8 )
5422 : {
5423 5092 : mvs2s( MASA_grouping_8_to_5, MASA_grouping, 8 );
5424 : }
5425 7270 : else if ( coding_subbands_0 <= 12 )
5426 : {
5427 3022 : mvs2s( MASA_grouping_12_to_5, MASA_grouping, 12 );
5428 : }
5429 4248 : else if ( coding_subbands_0 <= 18 )
5430 : {
5431 1880 : mvs2s( MASA_grouping_18_to_5, MASA_grouping, 18 );
5432 : }
5433 : else
5434 : {
5435 2368 : if ( coding_subbands_0 <= 24 )
5436 : {
5437 2368 : mvs2s( MASA_grouping_24_to_5, MASA_grouping, 24 );
5438 : }
5439 : }
5440 : }
5441 :
5442 46474 : if ( coding_subbands < coding_subbands_0 )
5443 : {
5444 7468 : d = 0;
5445 58684 : for ( j = 0; j < coding_subbands_0; j++ )
5446 : {
5447 51216 : if ( hQMetaData->twoDirBands[j] == 1 )
5448 : {
5449 20312 : two_dir_band[d++] = j;
5450 : }
5451 : }
5452 : }
5453 :
5454 343082 : for ( j = 0; j < coding_subbands; j++ )
5455 : {
5456 : /* DCT transform */
5457 296608 : dct4_transform( hQMetaData->q_direction[idx_d].coherence_band_data[j].spread_coherence, dct_coh[j] );
5458 :
5459 296608 : if ( hrmasa_flag )
5460 : {
5461 0 : minimum_s( (int16_t *) ( q_direction->band_data[j].energy_ratio_index ), q_direction->cfg.nblocks, &min_index );
5462 0 : no_cb_vec[j] = len_cb_dct0_masa[min_index >> 1];
5463 : }
5464 : else
5465 : {
5466 296608 : no_cb_vec[j] = len_cb_dct0_masa[q_direction->band_data[j].energy_ratio_index[0]];
5467 : }
5468 :
5469 296608 : if ( write_flag )
5470 : {
5471 : /* quantize first DCT parameter */
5472 148304 : dct_coh[j][0] = quantize_DCT_0_coh( dct_coh[j][0], j, coherence_cb0_masa, MASA_DELTA_AZI_DCT0, MASA_NO_CV_COH, q_direction, &idx_dct[k], &no_cb_vec[j], hrmasa_flag );
5473 : }
5474 :
5475 296608 : if ( coding_subbands < coding_subbands_0 )
5476 : {
5477 20312 : idx_dct[k + coding_subbands] = squant( dct_coh[j][1], &dct_coh[j][1], &coherence_cb1_masa[MASA_grouping[two_dir_band[j]] * MASA_NO_CV_COH1], MASA_NO_CV_COH1 );
5478 : }
5479 : else
5480 : {
5481 276296 : idx_dct[k + coding_subbands] = squant( dct_coh[j][1], &dct_coh[j][1], &coherence_cb1_masa[MASA_grouping[j] * MASA_NO_CV_COH1], MASA_NO_CV_COH1 );
5482 : }
5483 296608 : k++;
5484 :
5485 296608 : dct_coh[j][2] = 0.0f;
5486 296608 : dct_coh[j][3] = 0.0f;
5487 : }
5488 :
5489 46474 : nbits1 = 0;
5490 46474 : if ( sum_s( no_cb_vec, coding_subbands ) > MASA_COH_LIMIT_2IDX )
5491 : {
5492 : /* make two indxes */
5493 348 : no_cb = 1;
5494 : #ifdef DEBUGGING
5495 : assert( coding_subbands % 2 == 0 );
5496 : #endif
5497 :
5498 4524 : for ( j = 0; j < coding_subbands / 2; j++ )
5499 : {
5500 4176 : no_cb *= no_cb_vec[j];
5501 : }
5502 :
5503 348 : nbits = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
5504 348 : no_cb = 1;
5505 :
5506 4524 : for ( j = coding_subbands / 2; j < coding_subbands; j++ )
5507 : {
5508 4176 : no_cb *= no_cb_vec[j];
5509 : }
5510 348 : nbits1 = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
5511 : }
5512 : else
5513 : {
5514 46126 : no_cb = 1;
5515 :
5516 334382 : for ( j = 0; j < coding_subbands; j++ )
5517 : {
5518 288256 : no_cb *= no_cb_vec[j];
5519 : }
5520 :
5521 46126 : nbits = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
5522 : }
5523 :
5524 46474 : if ( write_flag )
5525 : {
5526 171541 : for ( j = 0; j < coding_subbands; j++ )
5527 : {
5528 : /* inverse DCT transform */
5529 148304 : invdct4_transform( dct_coh[j], q_direction->coherence_band_data[j].spread_coherence );
5530 : }
5531 :
5532 23237 : nbits = encode_coherence_indexesDCT0( idx_dct, coding_subbands, no_cb_vec, hMetaData, *indice_coherence, nbits, nbits1 );
5533 : }
5534 : else
5535 : {
5536 : /* write dummy data now and save the position */
5537 23237 : *indice_coherence = hMetaData->nb_ind_tot;
5538 23237 : k = nbits;
5539 50584 : while ( k > 0 )
5540 : {
5541 27347 : push_next_indice( hMetaData, 0, min( 16, k ) );
5542 27347 : k -= 16;
5543 : }
5544 :
5545 23237 : if ( nbits1 > 0 )
5546 : {
5547 174 : k = nbits1;
5548 522 : while ( k > 0 )
5549 : {
5550 348 : push_next_indice( hMetaData, 0, min( 16, k ) );
5551 348 : k -= 16;
5552 : }
5553 : }
5554 23237 : nbits += nbits1;
5555 23237 : set_s( no_cb_vec, MASA_NO_CV_COH1, coding_subbands );
5556 23237 : nbits += encode_coherence_indexesDCT1( &idx_dct[coding_subbands], coding_subbands, hMetaData );
5557 :
5558 23237 : return nbits;
5559 : }
5560 : }
5561 :
5562 23237 : return nbits;
5563 : }
5564 :
5565 :
5566 : /*-------------------------------------------------------------------*
5567 : * ivas_qmetadata_reorder_2dir_bands()
5568 : *
5569 : * Reorders metadata on 2dir bands such that direct-to-total ratio of first direction is
5570 : * always larger or equal to direct-to-total ratio of second direction.
5571 : *-------------------------------------------------------------------*/
5572 :
5573 4580 : static void ivas_qmetadata_reorder_2dir_bands(
5574 : IVAS_QMETADATA_HANDLE hQMetaData )
5575 : {
5576 : int16_t nbands;
5577 : int16_t nsubframes;
5578 : int16_t band, sf;
5579 :
5580 4580 : nbands = hQMetaData->q_direction[0].cfg.nbands;
5581 4580 : nsubframes = hQMetaData->q_direction[0].cfg.nblocks;
5582 :
5583 44132 : for ( band = 0; band < nbands; band++ )
5584 : {
5585 39552 : if ( hQMetaData->twoDirBands[band] == 1 )
5586 : {
5587 13200 : if ( hQMetaData->q_direction[0].band_data[band].energy_ratio[0] < hQMetaData->q_direction[1].band_data[band].energy_ratio[0] )
5588 : {
5589 5424 : uint16_t uint16_tmp = 0;
5590 5424 : float flt_tmp = 0;
5591 5424 : uint8_t uint8_tmp = 0;
5592 :
5593 23454 : for ( sf = 0; sf < nsubframes; sf++ )
5594 : {
5595 18030 : uint16_tmp = hQMetaData->q_direction[0].band_data[band].spherical_index[sf];
5596 18030 : hQMetaData->q_direction[0].band_data[band].spherical_index[sf] = hQMetaData->q_direction[1].band_data[band].spherical_index[sf];
5597 18030 : hQMetaData->q_direction[1].band_data[band].spherical_index[sf] = uint16_tmp;
5598 :
5599 18030 : flt_tmp = hQMetaData->q_direction[0].band_data[band].azimuth[sf];
5600 18030 : hQMetaData->q_direction[0].band_data[band].azimuth[sf] = hQMetaData->q_direction[1].band_data[band].azimuth[sf];
5601 18030 : hQMetaData->q_direction[1].band_data[band].azimuth[sf] = flt_tmp;
5602 :
5603 18030 : flt_tmp = hQMetaData->q_direction[0].band_data[band].elevation[sf];
5604 18030 : hQMetaData->q_direction[0].band_data[band].elevation[sf] = hQMetaData->q_direction[1].band_data[band].elevation[sf];
5605 18030 : hQMetaData->q_direction[1].band_data[band].elevation[sf] = flt_tmp;
5606 :
5607 18030 : uint8_tmp = hQMetaData->q_direction[0].band_data[band].distance[sf];
5608 18030 : hQMetaData->q_direction[0].band_data[band].distance[sf] = hQMetaData->q_direction[1].band_data[band].distance[sf];
5609 18030 : hQMetaData->q_direction[1].band_data[band].distance[sf] = uint8_tmp;
5610 :
5611 18030 : if ( hQMetaData->coherence_flag )
5612 : {
5613 18030 : uint8_tmp = hQMetaData->q_direction[0].coherence_band_data[band].spread_coherence[sf];
5614 18030 : hQMetaData->q_direction[0].coherence_band_data[band].spread_coherence[sf] = hQMetaData->q_direction[1].coherence_band_data[band].spread_coherence[sf];
5615 18030 : hQMetaData->q_direction[1].coherence_band_data[band].spread_coherence[sf] = uint8_tmp;
5616 : }
5617 : }
5618 5424 : if ( hQMetaData->coherence_flag )
5619 : {
5620 5424 : flt_tmp = hQMetaData->q_direction[0].band_data[band].energy_ratio[0];
5621 5424 : hQMetaData->q_direction[0].band_data[band].energy_ratio[0] = hQMetaData->q_direction[1].band_data[band].energy_ratio[0];
5622 5424 : hQMetaData->q_direction[1].band_data[band].energy_ratio[0] = flt_tmp;
5623 : }
5624 : }
5625 : }
5626 : }
5627 :
5628 4580 : return;
5629 : }
5630 :
5631 :
5632 : /*-------------------------------------------------------------------*
5633 : * write_2dir_info()
5634 : *
5635 : *
5636 : *-------------------------------------------------------------------*/
5637 :
5638 21696 : static int16_t write_2dir_info(
5639 : BSTR_ENC_HANDLE hMetaData,
5640 : uint8_t *twoDirBands,
5641 : const int16_t n,
5642 : const int16_t k )
5643 : {
5644 : int16_t nbits;
5645 : int16_t p[MASA_MAXIMUM_CODING_SUBBANDS];
5646 : uint16_t dif_p[MASA_MAXIMUM_CODING_SUBBANDS];
5647 : int16_t i, j;
5648 :
5649 21696 : j = 0;
5650 21696 : p[0] = 0;
5651 260612 : for ( i = 0; i < n; i++ )
5652 : {
5653 238916 : if ( twoDirBands[i] == 1 )
5654 : {
5655 200174 : p[j] = i;
5656 200174 : j++;
5657 : }
5658 : }
5659 :
5660 21696 : dif_p[0] = p[0];
5661 200174 : for ( i = 1; i < j; i++ )
5662 : {
5663 178478 : dif_p[i] = p[i] - p[i - 1] - 1;
5664 : }
5665 :
5666 : #ifdef DEBUGGING
5667 : assert( k == j );
5668 : #endif
5669 21696 : j = hMetaData->nb_bits_tot;
5670 221870 : for ( i = 0; i < k; i++ )
5671 : {
5672 200174 : ivas_qmetadata_encode_extended_gr( hMetaData, dif_p[i], 24, 0 );
5673 : }
5674 21696 : nbits = hMetaData->nb_bits_tot - j;
5675 :
5676 21696 : return nbits;
5677 : }
5678 :
5679 :
5680 : /*-------------------------------------------------------------------*
5681 : * transform_azimuth_dir2()
5682 : *
5683 : *
5684 : *-------------------------------------------------------------------*/
5685 :
5686 20820 : static void transform_azimuth_dir2(
5687 : IVAS_QMETADATA_HANDLE hQMetaData,
5688 : int16_t *dir2_bands )
5689 : {
5690 : int16_t i, b;
5691 :
5692 212660 : for ( i = hQMetaData->q_direction[1].cfg.start_band; i < hQMetaData->q_direction[1].cfg.nbands; i++ )
5693 : {
5694 191840 : if ( hQMetaData->q_direction[0].band_data[dir2_bands[i]].energy_ratio_index[0] < 7 )
5695 : {
5696 : /* transform azimuth */
5697 654624 : for ( b = 0; b < hQMetaData->q_direction[1].cfg.nblocks; b++ )
5698 : {
5699 521973 : hQMetaData->q_direction[1].band_data[i].azimuth[b] = hQMetaData->q_direction[1].band_data[i].azimuth[b] - hQMetaData->q_direction[0].band_data[dir2_bands[i]].azimuth[b] + 180;
5700 521973 : if ( hQMetaData->q_direction[1].band_data[i].azimuth[b] >= 180 )
5701 : {
5702 28879 : hQMetaData->q_direction[1].band_data[i].azimuth[b] -= 360;
5703 : }
5704 521973 : if ( hQMetaData->q_direction[1].band_data[i].azimuth[b] < -180 )
5705 : {
5706 0 : hQMetaData->q_direction[1].band_data[i].azimuth[b] += 360;
5707 : }
5708 521973 : if ( hQMetaData->q_direction[1].band_data[i].azimuth[b] >= 180 )
5709 : {
5710 0 : hQMetaData->q_direction[1].band_data[i].azimuth[b] -= 360;
5711 : }
5712 521973 : if ( hQMetaData->q_direction[1].band_data[i].azimuth[b] < -180 )
5713 : {
5714 0 : hQMetaData->q_direction[1].band_data[i].azimuth[b] += 360;
5715 : }
5716 : #ifdef DEBUGGING
5717 : assert( hQMetaData->q_direction[1].band_data[i].azimuth[b] < 180 && hQMetaData->q_direction[1].band_data[i].azimuth[b] >= -180 );
5718 : #endif
5719 : }
5720 : }
5721 : }
5722 :
5723 20820 : return;
5724 : }
5725 :
5726 : #ifdef DEBUG_MODE_QMETADATA
5727 : /*-------------------------------------------------------------------------
5728 : * DEBUG function direction_distance()
5729 : *
5730 : *------------------------------------------------------------------------*/
5731 :
5732 : static float direction_distance(
5733 : float elevation[DIRAC_MAX_NBANDS][MAX_PARAM_SPATIAL_SUBFRAMES],
5734 : float azimuth[DIRAC_MAX_NBANDS][MAX_PARAM_SPATIAL_SUBFRAMES],
5735 : IVAS_QDIRECTION *q_direction,
5736 : const int16_t dim1,
5737 : const int16_t dim2,
5738 : float mat_dist[DIRAC_MAX_NBANDS][MAX_PARAM_SPATIAL_SUBFRAMES] )
5739 : {
5740 : float d;
5741 : int16_t i, j;
5742 :
5743 : d = 0;
5744 : for ( i = 0; i < dim1; i++ )
5745 : {
5746 : for ( j = 0; j < dim2; j++ )
5747 : {
5748 : mat_dist[i][j] = sinf( elevation[i][j] * PI_OVER_180 ) * sinf( q_direction->band_data[i].elevation[j] * PI_OVER_180 ) +
5749 : cosf( elevation[i][j] * PI_OVER_180 ) * cosf( q_direction->band_data[i].elevation[j] * PI_OVER_180 ) * cosf( ( azimuth[i][j] - q_direction->band_data[i].azimuth[j] ) * PI_OVER_180 );
5750 :
5751 : d += fabsf( mat_dist[i][j] );
5752 : }
5753 : }
5754 :
5755 : return d / (float) ( dim1 * dim2 );
5756 : }
5757 : #endif
5758 :
5759 :
5760 4836 : static int16_t divide_GR_orders(
5761 : const int16_t *q_idx,
5762 : const int16_t GR1,
5763 : const int16_t GR2,
5764 : const int16_t len,
5765 : const int16_t len_max_GR1,
5766 : int16_t *i_min )
5767 : {
5768 : int16_t nb_GR_min;
5769 : int16_t i, j, nb_GR;
5770 4836 : nb_GR_min = 1000;
5771 4836 : *i_min = -1;
5772 72840 : for ( i = 0; i < min( len_max_GR1, len ); i++ )
5773 : {
5774 68004 : nb_GR = 0;
5775 :
5776 587088 : for ( j = 0; j <= i; j++ )
5777 : {
5778 519084 : nb_GR += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR1 );
5779 : }
5780 741204 : for ( j = i + 1; j < len; j++ )
5781 : {
5782 673200 : nb_GR += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR2 );
5783 : }
5784 :
5785 68004 : if ( nb_GR < nb_GR_min )
5786 : {
5787 16416 : nb_GR_min = nb_GR;
5788 16416 : *i_min = i + 1;
5789 : }
5790 : }
5791 :
5792 4836 : return nb_GR_min;
5793 : }
5794 :
5795 :
5796 250 : static int16_t find_optimal_GR_order(
5797 : const int16_t *q_idx,
5798 : const int16_t len,
5799 : int16_t *GR )
5800 : {
5801 : int16_t nb_GR_0, nb_GR_1;
5802 : int16_t i;
5803 : /* find optimum length of the part encoded with GR2 */
5804 250 : nb_GR_0 = 0;
5805 250 : nb_GR_1 = 0;
5806 1226 : for ( i = 0; i < len; i++ )
5807 : {
5808 976 : nb_GR_0 += ivas_qmetadata_encode_extended_gr_length( q_idx[i], 100, 0 );
5809 976 : nb_GR_1 += ivas_qmetadata_encode_extended_gr_length( q_idx[i], 100, 1 );
5810 : }
5811 :
5812 250 : if ( nb_GR_0 < nb_GR_1 )
5813 : {
5814 113 : *GR = 0;
5815 113 : return nb_GR_0;
5816 : }
5817 : else
5818 : {
5819 137 : *GR = 1;
5820 :
5821 137 : return nb_GR_1;
5822 : }
5823 : }
5824 :
5825 :
5826 1612 : static int16_t find_optimal_GR_orders(
5827 : const int16_t *q_idx,
5828 : const int16_t len,
5829 : const int16_t len_max_GR1,
5830 : int16_t *GR1,
5831 : int16_t *GR2,
5832 : int16_t *i_min )
5833 : {
5834 : int16_t nb_GR_20, nb_GR_21, nb_GR_10, nb_GR_min;
5835 : int16_t i_min_20, i_min_21, i_min_10;
5836 : /* find optimum length of the part encoded with GR2 */
5837 1612 : nb_GR_20 = divide_GR_orders( q_idx, 2, 0, len, len_max_GR1, &i_min_20 );
5838 1612 : nb_GR_21 = divide_GR_orders( q_idx, 2, 1, len, len_max_GR1, &i_min_21 );
5839 1612 : nb_GR_10 = divide_GR_orders( q_idx, 1, 0, len, len_max_GR1, &i_min_10 );
5840 :
5841 1612 : if ( nb_GR_20 < nb_GR_21 && nb_GR_20 < nb_GR_10 )
5842 : {
5843 1046 : *GR1 = 2;
5844 1046 : *GR2 = 0;
5845 1046 : nb_GR_min = nb_GR_20;
5846 1046 : *i_min = i_min_20;
5847 : }
5848 : else
5849 : {
5850 566 : if ( nb_GR_21 < nb_GR_20 && nb_GR_21 < nb_GR_10 )
5851 : {
5852 185 : *GR1 = 2;
5853 185 : *GR2 = 1;
5854 185 : nb_GR_min = nb_GR_21;
5855 185 : *i_min = i_min_21;
5856 : }
5857 : else
5858 : {
5859 381 : *GR1 = 1;
5860 381 : *GR2 = 0;
5861 381 : nb_GR_min = nb_GR_10;
5862 381 : *i_min = i_min_10;
5863 : }
5864 : }
5865 :
5866 1612 : return nb_GR_min;
5867 : }
5868 :
5869 :
5870 1862 : static int16_t write_stream_dct_coeffs_omasa(
5871 : int16_t *q_idx, /* i : array of indexes to be written */
5872 : const int16_t len_stream, /* i : array length */
5873 : BSTR_ENC_HANDLE hMetaData, /* i/o: metadata bitstream */
5874 : const int16_t first_line, /* i : is first line of the matrix? 1/0 */
5875 : const int16_t low_bitrate_mode /* i : is low bitrate mode? if yes, limit the number of bits written */
5876 : )
5877 : {
5878 1862 : int16_t nb_bits = 0, bits_pos;
5879 : uint16_t nb_GR_min;
5880 : int16_t i, j;
5881 : int16_t changed, update_needed;
5882 :
5883 : int16_t GR1, GR2, i_min;
5884 : int16_t max_bits;
5885 :
5886 1862 : bits_pos = hMetaData->nb_bits_tot;
5887 1862 : if ( low_bitrate_mode == 1 )
5888 : {
5889 250 : max_bits = 50;
5890 : }
5891 : else
5892 : {
5893 1612 : max_bits = 1000;
5894 : }
5895 :
5896 : /* write DCT 0 component */
5897 : /* write sign only if not the very first DCT coeff */
5898 1862 : if ( first_line == 0 )
5899 : {
5900 0 : if ( q_idx[0] > 0 )
5901 : {
5902 0 : push_next_indice( hMetaData, 1, 1 );
5903 0 : push_next_indice( hMetaData, q_idx[0], BITS_MASA2TOTTAL_DCT0 );
5904 : }
5905 : else
5906 : {
5907 0 : push_next_indice( hMetaData, 0, 1 );
5908 0 : push_next_indice( hMetaData, -q_idx[0], BITS_MASA2TOTTAL_DCT0 );
5909 : }
5910 0 : nb_bits += BITS_MASA2TOTTAL_DCT0 + 1;
5911 : }
5912 : else
5913 : {
5914 1862 : push_next_indice( hMetaData, q_idx[0], BITS_MASA2TOTTAL_DCT0 );
5915 1862 : nb_bits += BITS_MASA2TOTTAL_DCT0;
5916 : }
5917 :
5918 1862 : if ( q_idx[0] != 0 )
5919 : {
5920 1862 : i_min = 1;
5921 1862 : GR2 = 0;
5922 1862 : if ( len_stream >= 8 )
5923 : {
5924 1612 : nb_GR_min = find_optimal_GR_orders( &q_idx[1], len_stream - 1, 15, &GR1, &GR2, &i_min );
5925 : }
5926 : else
5927 : {
5928 250 : nb_GR_min = find_optimal_GR_order( &q_idx[1], len_stream - 1, &GR1 );
5929 : }
5930 :
5931 1862 : assert( nb_GR_min < 1000 );
5932 1862 : changed = 1;
5933 1862 : update_needed = 0;
5934 1862 : while ( len_stream >= 8 && nb_GR_min > max_bits && changed >= 1 )
5935 : {
5936 0 : update_needed = 1;
5937 0 : changed = 0;
5938 0 : for ( j = len_stream - 1; j > 6; j-- )
5939 : {
5940 0 : if ( q_idx[j] >= 2 )
5941 : {
5942 :
5943 0 : if ( j > i_min )
5944 : {
5945 0 : changed = 1;
5946 0 : nb_GR_min -= ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR2 );
5947 0 : q_idx[j] -= 2;
5948 0 : nb_GR_min += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR2 );
5949 : }
5950 : else
5951 : {
5952 0 : changed = 1;
5953 0 : nb_GR_min -= ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR1 );
5954 0 : q_idx[j] -= 2;
5955 0 : nb_GR_min += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, 0 );
5956 : }
5957 : }
5958 0 : else if ( q_idx[j] == 1 )
5959 : {
5960 0 : if ( j > i_min )
5961 : {
5962 0 : changed = 1;
5963 0 : nb_GR_min -= ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR2 );
5964 0 : q_idx[j] -= 1;
5965 :
5966 0 : nb_GR_min += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR2 );
5967 : }
5968 : else
5969 : {
5970 0 : changed = 1;
5971 0 : nb_GR_min -= ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR1 );
5972 0 : q_idx[j] -= 1;
5973 0 : nb_GR_min += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, 0 );
5974 : }
5975 : }
5976 0 : if ( nb_GR_min < max_bits )
5977 : {
5978 0 : break;
5979 : }
5980 : }
5981 : }
5982 :
5983 1862 : if ( update_needed == 1 )
5984 : {
5985 : /* re-calculate */
5986 : /* find optimum length of the part encoded with GR2 */
5987 0 : nb_GR_min = find_optimal_GR_orders( &q_idx[1], len_stream - 1, 15, &GR1, &GR2, &i_min );
5988 : }
5989 :
5990 1862 : if ( len_stream >= 8 )
5991 : {
5992 : /* write number of indexes encoded with GR2 on 4 bits */
5993 1612 : push_next_indice( hMetaData, i_min, 4 );
5994 1612 : nb_bits += 4;
5995 : /* write GR orders */
5996 1612 : push_next_indice( hMetaData, GR1 - 1, 1 );
5997 1612 : nb_bits += 1;
5998 1612 : if ( GR1 == 2 )
5999 : {
6000 1231 : push_next_indice( hMetaData, GR2, 1 );
6001 1231 : nb_bits += 1;
6002 : }
6003 :
6004 : /* write GR data */
6005 9249 : for ( i = 1; i <= i_min; i++ )
6006 : {
6007 7637 : ivas_qmetadata_encode_extended_gr( hMetaData, q_idx[i], 100, GR1 );
6008 : }
6009 :
6010 21579 : for ( i = i_min + 1; i < len_stream; i++ )
6011 : {
6012 19967 : ivas_qmetadata_encode_extended_gr( hMetaData, q_idx[i], 100, GR2 );
6013 : }
6014 : }
6015 : else
6016 : {
6017 : /* len_stream <= 8 */
6018 : /* write GR order */
6019 250 : push_next_indice( hMetaData, GR1, 1 );
6020 250 : nb_bits += 1;
6021 1226 : for ( i = 1; i < len_stream; i++ )
6022 : {
6023 976 : ivas_qmetadata_encode_extended_gr( hMetaData, q_idx[i], 100, GR1 );
6024 : }
6025 : }
6026 :
6027 1862 : nb_bits += nb_GR_min;
6028 :
6029 1862 : assert( nb_bits == ( hMetaData->nb_bits_tot - bits_pos ) );
6030 : }
6031 :
6032 1862 : return nb_bits;
6033 : }
6034 :
6035 :
6036 : /*-------------------------------------------------------------------------
6037 : * ivas_omasa_encode_masa_to_total()
6038 : *
6039 : *------------------------------------------------------------------------*/
6040 :
6041 1862 : void ivas_omasa_encode_masa_to_total(
6042 : float masa_to_total_energy_ratio[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS],
6043 : BSTR_ENC_HANDLE hMetaData,
6044 : const int16_t low_bitrate_mode,
6045 : const int16_t nbands,
6046 : const int16_t nblocks )
6047 : {
6048 : int16_t i, j, k;
6049 : float data[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6050 : float q_dct_data[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6051 1862 : float step = STEP_M2T;
6052 :
6053 : int16_t q_idx[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6054 : float dct_data_tmp[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6055 : float dct_data[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6056 :
6057 : int16_t bits_pos, nb_bits;
6058 : int16_t n_streams, len_stream;
6059 :
6060 : Word32 q_dct_data_fx[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS],
6061 : dct_data_tmp_fx[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6062 :
6063 : int64_t step_fx;
6064 1862 : step_fx = STEP_M2T_FX;
6065 : #ifdef DEBUG_MODE_QMETADATA
6066 : static FILE *pF = NULL;
6067 : static FILE *pF_ratio = NULL;
6068 :
6069 : if ( pF == NULL )
6070 : pF = fopen( "./res/qmetadata_ism_qidx__enc.txt", "w" );
6071 : if ( pF_ratio == NULL )
6072 : pF_ratio = fopen( "./res/qmetadata_masa2tot_enc.txt", "w" );
6073 :
6074 : #endif
6075 :
6076 1862 : bits_pos = hMetaData->nb_bits_tot;
6077 1862 : k = 0;
6078 13722 : for ( i = 0; i < nbands; i++ )
6079 : {
6080 42302 : for ( j = 0; j < nblocks; j++ )
6081 : {
6082 30442 : data[k] = masa_to_total_energy_ratio[j][i];
6083 30442 : k++;
6084 : }
6085 : }
6086 :
6087 : /* DCT2 transform */
6088 1862 : n_streams = 1;
6089 1862 : len_stream = nbands * nblocks;
6090 1862 : switch ( len_stream )
6091 : {
6092 24 : case 4:
6093 24 : matrix_product( dct4, nblocks, nblocks, 0, data, 1, nblocks, 1, dct_data );
6094 24 : n_streams = 1;
6095 24 : len_stream = 4;
6096 24 : break;
6097 226 : case 5:
6098 226 : matrix_product( dct5, nbands, nbands, 0, data, 1, nbands, 1, dct_data );
6099 226 : n_streams = 1;
6100 226 : len_stream = nbands;
6101 226 : break;
6102 0 : case 8:
6103 0 : matrix_product( dct8, nbands, nbands, 0, data, 1, nbands, 1, dct_data );
6104 0 : n_streams = 1;
6105 0 : len_stream = nbands;
6106 0 : break;
6107 378 : case 12:
6108 378 : matrix_product( dct12, nbands, nbands, 0, data, 1, nbands, 1, dct_data );
6109 378 : n_streams = 1;
6110 378 : len_stream = nbands;
6111 378 : break;
6112 1234 : case 20:
6113 1234 : matrix_product( dct5, nbands, nbands, 0, data, nblocks, nbands, 1, dct_data_tmp );
6114 1234 : matrix_product( dct_data_tmp, nbands, nblocks, 0, dct4, nblocks, nblocks, 1, dct_data );
6115 1234 : n_streams = 1;
6116 1234 : len_stream = nbands * nblocks;
6117 1234 : break;
6118 0 : case 32:
6119 0 : matrix_product( dct8, nbands, nbands, 0, data, nblocks, nbands, 1, dct_data_tmp );
6120 0 : matrix_product( dct_data_tmp, nbands, nblocks, 0, dct4, nblocks, nblocks, 1, dct_data );
6121 0 : n_streams = nblocks;
6122 0 : len_stream = nbands;
6123 0 : break;
6124 0 : default:
6125 0 : printf( "Incorrect number of coefficients for OMASA.\n" );
6126 0 : break;
6127 : }
6128 :
6129 3724 : for ( k = 0; k < n_streams; k++ )
6130 : {
6131 1862 : j = k * len_stream;
6132 : /* quantize with fixed common step */
6133 1862 : q_idx[j] = (int16_t) rintf( dct_data[j] / step );
6134 :
6135 1862 : if ( q_idx[j] > ( ( 1 << BITS_MASA2TOTTAL_DCT0 ) - 1 ) ) /* limit DCT0 to BITS_MASA2TOTTAL_DCT0 bit representation */
6136 : {
6137 0 : q_idx[j] = ( ( 1 << BITS_MASA2TOTTAL_DCT0 ) - 1 );
6138 : }
6139 :
6140 1862 : q_dct_data[j] = step * q_idx[j];
6141 :
6142 1862 : if ( q_idx[j] == 0 )
6143 : {
6144 0 : set_s( &q_idx[j], 0, len_stream );
6145 0 : set_zero( &q_dct_data[j], len_stream );
6146 : }
6147 : else
6148 : {
6149 30442 : for ( i = 1; i < len_stream; i++ )
6150 : {
6151 28580 : q_idx[j + i] = (int16_t) rintf( dct_data[j + i] / step );
6152 28580 : q_dct_data[j + i] = step * q_idx[j + i];
6153 28580 : if ( q_idx[j + i] <= 0 )
6154 : {
6155 19968 : q_idx[j + i] = -2 * q_idx[j + i];
6156 : }
6157 : else
6158 : {
6159 8612 : q_idx[j + i] = 2 * q_idx[j + i] - 1;
6160 : }
6161 : }
6162 : }
6163 : }
6164 :
6165 : /* write data */
6166 1862 : nb_bits = 0;
6167 3724 : for ( i = 0; i < n_streams; i++ )
6168 : {
6169 1862 : nb_bits += write_stream_dct_coeffs_omasa( &q_idx[i * len_stream], len_stream, hMetaData, ( i == 0 ), low_bitrate_mode );
6170 : }
6171 :
6172 : /* reconstruct masa2total */
6173 1862 : q_dct_data_fx[0] = (Word32) ( ( step_fx * q_idx[0] ) >> 6 ); // Q25
6174 30442 : for ( i = 1; i < len_stream; i++ )
6175 : {
6176 28580 : if ( ( q_idx[i] & 1 ) == 0 )
6177 : {
6178 19968 : q_dct_data_fx[i] = (Word32) ( ( step_fx * ( -q_idx[i] ) ) >> 7 ); // Q25
6179 : }
6180 : ELSE
6181 : {
6182 8612 : q_dct_data_fx[i] = (Word32) ( ( step_fx * ( q_idx[i] + 1 ) ) >> 7 ); // Q25
6183 : }
6184 : }
6185 1862 : SWITCH( len_stream )
6186 : {
6187 24 : case 4:
6188 24 : matrix_product_q30_fx( dct4_fx, nblocks, nblocks, 1, q_dct_data_fx, nblocks, 1, 0, dct_data_tmp_fx );
6189 24 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nblocks ); /*Q30*/
6190 24 : BREAK;
6191 226 : case 5:
6192 226 : matrix_product_q30_fx( dct5_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
6193 226 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
6194 226 : BREAK;
6195 0 : case 8:
6196 0 : matrix_product_q30_fx( dct8_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
6197 0 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
6198 0 : BREAK;
6199 378 : case 12:
6200 378 : matrix_product_q30_fx( dct12_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
6201 378 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
6202 378 : BREAK;
6203 1234 : case 20:
6204 1234 : matrix_product_fx( dct5_fx, nbands, nbands, 1, q_dct_data_fx, nbands, nblocks, 0, dct_data_tmp_fx );
6205 1234 : matrix_product_q30_fx( dct_data_tmp_fx, nbands, nblocks, 0, dct4_fx, nblocks, nblocks, 0, q_dct_data_fx ); /* reuse of variable*/
6206 1234 : BREAK;
6207 0 : case 32:
6208 0 : matrix_product_fx( dct8_fx, nbands, nbands, 1, q_dct_data_fx, nbands, nblocks, 0, dct_data_tmp_fx );
6209 0 : matrix_product_q30_fx( dct_data_tmp_fx, nbands, nblocks, 0, dct4_fx, nblocks, nblocks, 0, q_dct_data_fx );
6210 0 : BREAK;
6211 0 : default:
6212 0 : printf( "Incorrect number of coefficients for OMASA.\n" );
6213 0 : BREAK;
6214 : }
6215 :
6216 : /* this is to make sure the comparison to the threshold 0.98 will go the same way in */
6217 : /* fixed point and floating point without having to drag the fixed point values to the */
6218 : /* comparison place in the code; */
6219 : /* 1052266987 is 0.98 in Q30 it is not needed in the fixed point */
6220 32304 : for ( i = 0; i < nblocks * nbands; i++ )
6221 : {
6222 30442 : if ( q_dct_data_fx[i] >= 1052266987 && q_dct_data_fx[i] < 1052400000 )
6223 : {
6224 30 : q_dct_data_fx[i] = 1052400000;
6225 : }
6226 : }
6227 :
6228 1862 : k = 0;
6229 7498 : for ( i = 0; i < nblocks; i++ )
6230 : {
6231 36078 : for ( j = 0; j < nbands; j++ )
6232 : {
6233 30442 : masa_to_total_energy_ratio[i][j] = q_dct_data_fx[k] / (float) ( 1 << 30 );
6234 30442 : masa_to_total_energy_ratio[i][j] = max( 0.0f, masa_to_total_energy_ratio[i][j] );
6235 30442 : masa_to_total_energy_ratio[i][j] = min( 1.0f, masa_to_total_energy_ratio[i][j] );
6236 30442 : k++;
6237 : }
6238 : }
6239 :
6240 1862 : if ( nblocks == 1 )
6241 : {
6242 2416 : for ( i = 1; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
6243 : {
6244 18810 : for ( j = 0; j < nbands; j++ )
6245 : {
6246 16998 : masa_to_total_energy_ratio[i][j] = masa_to_total_energy_ratio[0][j];
6247 : }
6248 : }
6249 : }
6250 :
6251 1862 : if ( nbands == 1 )
6252 : {
6253 120 : for ( j = 1; j < 5; j++ )
6254 : {
6255 480 : for ( i = 0; i < nblocks; i++ )
6256 : {
6257 384 : masa_to_total_energy_ratio[i][j] = masa_to_total_energy_ratio[i][0];
6258 : }
6259 : }
6260 : }
6261 :
6262 1862 : assert( nb_bits == ( hMetaData->nb_bits_tot - bits_pos ) );
6263 :
6264 : #ifdef DEBUG_MODE_QMETADATA
6265 : {
6266 :
6267 : fprintf( pF, "frame %d: ", frame );
6268 : fprintf( pF_ratio, "frame %d: ", frame );
6269 :
6270 :
6271 : /* direction_distance( elevation_orig, azimuth_orig, q_direction, nbands, nblocks, mat_dist );*/
6272 : for ( i = 0; i < nbands; i++ )
6273 : {
6274 : for ( j = 0; j < 4; j++ )
6275 : {
6276 : fprintf( pF_ratio, " %5.2f ", hQMetaData->masa_to_total_energy_ratio[j][i] );
6277 : }
6278 : }
6279 : for ( i = 0; i < 20; i++ )
6280 : {
6281 : fprintf( pF, " %4d ", q_idx[i] );
6282 : }
6283 : fprintf( pF, "\n" );
6284 : fprintf( pF_ratio, "\n" );
6285 : }
6286 : #endif
6287 :
6288 1862 : return;
6289 : }
|