Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2026 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 585308 : 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 585308 : 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 585308 : bit_pos_0 = hMetaData->nb_bits_tot;
206 585308 : bit_pos_start = bit_pos_0;
207 :
208 585308 : ndirections = hQMetaData->no_directions;
209 585308 : extra_bits = 0;
210 :
211 : /* Check if coherence should be encoded */
212 585308 : all_coherence_zero = 1;
213 585308 : bits_no_dirs_coh = 0;
214 :
215 585308 : if ( hQMetaData->coherence_flag )
216 : {
217 128936 : all_coherence_zero = hQMetaData->all_coherence_zero;
218 :
219 128936 : push_next_indice( hMetaData, all_coherence_zero, 1 ); /* signal coherence */
220 128936 : bits_no_dirs_coh += 1;
221 : }
222 :
223 585308 : if ( ndirections > 1 )
224 : {
225 : #ifdef DEBUGGING
226 : assert( ndirections == 2 );
227 : #endif
228 : /* Reorder 2dir bands for more efficient encoding. */
229 85988 : if ( !hodirac_flag )
230 : {
231 39448 : ivas_qmetadata_reorder_2dir_bands( hQMetaData );
232 : }
233 85988 : d = 0;
234 945465 : for ( i = hQMetaData->q_direction[1].cfg.start_band; i < hQMetaData->q_direction[1].cfg.nbands; i++ )
235 : {
236 859477 : if ( hQMetaData->twoDirBands[i] == 1 )
237 : {
238 645475 : mvr2r( hQMetaData->q_direction[1].band_data[i].azimuth, hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].cfg.nblocks );
239 645475 : mvr2r( hQMetaData->q_direction[1].band_data[i].elevation, hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].cfg.nblocks );
240 645475 : 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 645475 : dir2_bands[d] = i;
243 :
244 645475 : if ( hQMetaData->coherence_flag )
245 : {
246 133535 : 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 645475 : d++;
249 : }
250 : }
251 :
252 85988 : bits_no_dirs_coh += write_2dir_info( hMetaData, hQMetaData->twoDirBands, hQMetaData->q_direction[0].cfg.nbands, hQMetaData->numTwoDirBands );
253 :
254 299990 : for ( i = d; i < hQMetaData->q_direction[0].cfg.nbands; i++ )
255 : {
256 214002 : 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 85988 : hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands;
263 : }
264 :
265 : /*Quantization of the Diffuseness */
266 585308 : ivas_qmetadata_quantize_diffuseness_nrg_ratios( hQMetaData, bits_dir_raw_pre, bits_diff, dfRatio_bits, hodirac_flag );
267 :
268 585308 : bits_diff_sum = 0;
269 585308 : bits_diff[0] = ivas_qmetadata_entropy_encode_diffuseness( hMetaData, &( hQMetaData->q_direction[0] ), &diffuseness_index_max_ec_frame_pre[0] );
270 585308 : bits_diff_sum += bits_diff[0];
271 :
272 585308 : if ( ndirections == 2 )
273 : {
274 85988 : bits_diff[1] = ivas_qmetadata_entropy_encode_df_ratio( hMetaData, &( hQMetaData->q_direction[1] ), dfRatio_bits );
275 85988 : 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 585308 : if ( hQMetaData->no_directions == 2 )
282 : {
283 : int16_t j, k, dir2band, index_dirRatio1Inv, index_dirRatio2Inv;
284 :
285 85988 : dir2band = 0;
286 945465 : for ( j = hQMetaData->q_direction[0].cfg.start_band; j < hQMetaData->q_direction[0].cfg.nbands; ++j )
287 : {
288 859477 : if ( hQMetaData->twoDirBands[j] == 1 )
289 : {
290 645475 : index_dirRatio1Inv = masa_sq( 1.0f - hQMetaData->q_direction[0].band_data[j].energy_ratio[0], diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
291 645475 : index_dirRatio2Inv = masa_sq( 1.0f - hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[0], diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
292 :
293 3144641 : for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ )
294 : {
295 2499166 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index[k] = index_dirRatio1Inv;
296 : }
297 :
298 3144641 : for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ )
299 : {
300 2499166 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[k] = index_dirRatio2Inv;
301 : }
302 :
303 645475 : dir2band++;
304 : }
305 : }
306 : }
307 :
308 : /* Encode surround coherence */
309 585308 : if ( ndirections == 2 )
310 : {
311 85988 : 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 85988 : 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 39366 : bits_surround_coh = encode_surround_coherence( hQMetaData, hMetaData );
315 : }
316 : else
317 : {
318 46622 : bits_surround_coh = 0;
319 559554 : for ( i = 0; i < hQMetaData->q_direction[0].cfg.nbands; i++ )
320 : {
321 512932 : if ( hQMetaData->surcoh_band_data != NULL )
322 : {
323 992 : set_c( (int8_t *) hQMetaData->surcoh_band_data[i].surround_coherence, 0, hQMetaData->q_direction[0].cfg.nblocks );
324 : }
325 : }
326 : }
327 85988 : bits_no_dirs_coh += bits_surround_coh;
328 85988 : 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 499320 : no_TF = hQMetaData->q_direction[0].cfg.nbands * hQMetaData->q_direction[0].cfg.nblocks;
333 499320 : 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 77535 : bits_surround_coh = encode_surround_coherence( hQMetaData, hMetaData );
336 : }
337 : else
338 : {
339 421785 : bits_surround_coh = 0;
340 1986494 : for ( i = 0; i < hQMetaData->q_direction[0].cfg.nbands; i++ )
341 : {
342 1564709 : if ( hQMetaData->surcoh_band_data != NULL )
343 : {
344 177338 : set_c( (int8_t *) hQMetaData->surcoh_band_data[i].surround_coherence, 0, hQMetaData->q_direction[0].cfg.nblocks );
345 : }
346 : }
347 : }
348 499320 : total_bits_1dir = hQMetaData->metadata_max_bits - bits_no_dirs_coh - bits_surround_coh;
349 : }
350 :
351 : /* Loop over number of directions*/
352 1256604 : for ( d = 0; d < ndirections; d++ )
353 : {
354 671296 : q_direction = &( hQMetaData->q_direction[d] );
355 :
356 671296 : if ( d == 1 )
357 : {
358 85988 : transform_azimuth_dir2( hQMetaData, dir2_bands );
359 : }
360 :
361 671296 : nbands = q_direction->cfg.nbands;
362 671296 : nblocks = q_direction->cfg.nblocks;
363 671296 : start_band = q_direction->cfg.start_band;
364 671296 : diffuseness_index_max_ec_frame = diffuseness_index_max_ec_frame_pre[0];
365 671296 : bits_dir_raw = bits_dir_raw_pre[d];
366 :
367 : /* This sets bit budget correctly for the second direction */
368 671296 : if ( d == 0 )
369 : {
370 585308 : bits_diff[d] = bits_diff_sum;
371 : }
372 : else
373 : {
374 85988 : 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 671296 : bits_signaling[d] = 0;
398 :
399 : /*Coherence */
400 671296 : bits_coherence[d] = 0;
401 671296 : bit_pos_start_coh = hMetaData->nb_bits_tot;
402 :
403 671296 : if ( all_coherence_zero == 0 )
404 : {
405 156668 : bits_coherence[d] = ivas_qmetadata_quantize_coherence( hQMetaData, d, all_coherence_zero, hMetaData, 0, &indice_coherence, 0 );
406 : }
407 :
408 671296 : 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 12770 : q_direction->not_in_2D = 0;
411 : /* Quantize directions*/
412 12770 : quantize_direction_frame2D( q_direction, azimuth_orig, elevation_orig );
413 : }
414 : else
415 : {
416 : /* Quantize directions*/
417 658526 : quantize_direction_frame( q_direction, azimuth_orig, elevation_orig, 0 );
418 : }
419 :
420 : /* Signalling 2D*/
421 671296 : push_next_indice( hMetaData, ( q_direction->not_in_2D > 0 ), 1 ); /*2D flag*/
422 671296 : 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 671296 : bit_pos_start = hMetaData->nb_bits_tot;
426 671296 : next_ind_start = hMetaData->nb_ind_tot;
427 :
428 : /* Encode quantized directions with EC frame-wise*/
429 671296 : if ( total_bits_1dir + bits_surround_coh <= hQMetaData->qmetadata_max_bit_req )
430 : {
431 465599 : push_next_indice( hMetaData, 0, 1 ); /*Write 1 bit to signal EC frame-wise (EC1)*/
432 465599 : bits_signaling[d]++;
433 : }
434 :
435 671296 : next_ind_raw_flag = hMetaData->nb_ind_tot;
436 671296 : push_next_indice( hMetaData, 0, 1 ); /* Raw coding flag*/
437 :
438 671296 : bits_dir_bands[0] = ivas_qmetadata_raw_encode_dir( NULL, q_direction, q_direction->cfg.nbands, q_direction->cfg.start_band );
439 :
440 671296 : reduce_bits = hQMetaData->is_masa_ivas_format ? ( total_bits_1dir - ( bits_diff[d] + bits_coherence[d] + bits_signaling[d] ) - 1 ) : MASA_MAX_BITS;
441 671296 : 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 671296 : if ( bits_ec < 0 )
444 : {
445 252854 : hMetaData->ind_list[next_ind_raw_flag].value = 1; /*rewrite flag*/
446 252854 : 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 671296 : 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 671296 : 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 671296 : 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 26726 : restore_metadata_buffer( hMetaData, next_ind_start, bit_pos_start );
467 :
468 : /* Write signaling */
469 26726 : push_next_indice( hMetaData, 1, 1 ); /*Write 1 bit to signal no EC frame-wise (EC1)*/
470 26726 : push_next_indice( hMetaData, 0, 1 ); /*Write 1 bit to signal EC band-wise (EC2)*/
471 26726 : bits_signaling[d] = 3;
472 :
473 : /* Write raw flags */
474 26726 : next_ind_raw_flag = hMetaData->nb_ind_tot;
475 194283 : for ( i = start_band; i < nbands; i++ )
476 : {
477 167557 : push_next_indice( hMetaData, 0, 1 ); /* Raw coding flag*/
478 : }
479 :
480 26726 : bits_dir[d] = 0;
481 26726 : diff_bits = bits_diff[d] + bits_coherence[d] + bits_signaling[d] - total_bits_1dir;
482 194283 : for ( i = start_band; i < nbands; i++ )
483 : {
484 167557 : bits_dir_bands[i] = ivas_qmetadata_raw_encode_dir( NULL, q_direction, i + 1, i );
485 :
486 : /* Write ec bits */
487 167557 : 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 167557 : if ( bits_ec >= 0 )
490 : {
491 107108 : bits_dir_bands[i] = bits_ec;
492 107108 : raw_flag[i] = 0;
493 : }
494 : else
495 : {
496 60449 : raw_flag[i] = 1;
497 : }
498 167557 : diff_bits += bits_dir_bands[i] + 1;
499 : }
500 :
501 26726 : small_requantize_direction_frame( q_direction, azimuth_orig, elevation_orig, raw_flag, bits_dir_bands, &diff_bits );
502 :
503 194283 : for ( i = start_band; i < nbands; i++ )
504 : {
505 167557 : if ( raw_flag[i] )
506 : {
507 : /* Rewrite raw flag value */
508 60449 : hMetaData->ind_list[next_ind_raw_flag + i - start_band].value = 1;
509 :
510 : /* Write ec bits */
511 60449 : 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 167557 : bits_dir[d] += bits_dir_bands[i] + 1;
517 : }
518 :
519 26726 : 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 671296 : 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 41595 : extra_bits = 0;
542 :
543 41595 : restore_metadata_buffer( hMetaData, next_ind_start, bit_pos_start );
544 :
545 41595 : push_next_indice( hMetaData, 1, 1 ); /*Write 1 bit to signal no EC frame-wise (EC1)*/
546 41595 : if ( nblocks > 1 )
547 : {
548 21621 : push_next_indice( hMetaData, 1, 1 ); /*Write 1 bit to signal requantization stage (EC3)*/
549 21621 : bits_signaling[d] = 3;
550 : }
551 : else
552 : {
553 19974 : bits_signaling[d] = 2;
554 : }
555 :
556 41595 : if ( hQMetaData->is_masa_ivas_format == 0 )
557 : {
558 6628 : reduce_bits = bits_dir_raw - ( total_bits_1dir - bits_diff[d] - bits_coherence[d] - bits_signaling[d] );
559 6628 : ind_order[0] = -1;
560 : }
561 : else
562 : {
563 34967 : ind_order[0] = 0;
564 34967 : 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 34967 : if ( reduce_bits > bits_dir_raw - nbands * nblocks )
567 : {
568 138 : reduce_bits = bits_dir_raw - nbands * nblocks;
569 : }
570 : }
571 :
572 41595 : only_reduce_bits_direction( &extra_bits, q_direction, reduce_bits, nbands, nblocks, ind_order );
573 41595 : bits_dir[d] = hMetaData->nb_bits_tot;
574 41595 : requantize_direction_EC_3( &extra_bits, q_direction, nbands, hMetaData, elevation_orig, azimuth_orig, ind_order );
575 41595 : 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 671296 : if ( ( bits_coherence[d] > 0 ) && ( all_coherence_zero == 0 ) && ( nblocks > 1 ) )
583 : {
584 127046 : bit_pos_start = hMetaData->nb_bits_tot;
585 127046 : hMetaData->nb_bits_tot = bit_pos_start_coh;
586 127046 : ivas_qmetadata_quantize_coherence( hQMetaData, d, all_coherence_zero, hMetaData, 1, &indice_coherence, 0 );
587 127046 : hMetaData->nb_bits_tot = bit_pos_start;
588 : }
589 :
590 671296 : if ( d == 0 )
591 : {
592 585308 : 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 4538544 : for ( i = start_band; i < nbands; i++ )
658 : {
659 3867248 : mvr2r( q_direction->band_data[i].azimuth, q_direction->band_data[i].q_azimuth, nblocks );
660 3867248 : 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 4538544 : for ( i = start_band; i < nbands; i++ )
665 : {
666 3867248 : mvr2r( azimuth_orig[i], q_direction->band_data[i].azimuth, nblocks );
667 3867248 : mvr2r( elevation_orig[i], q_direction->band_data[i].elevation, nblocks );
668 : }
669 : }
670 :
671 585308 : 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 8980 : 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 8980 : 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 8980 : ndirections = hQMetaData->no_directions;
729 :
730 : /* Check if coherence should be encoded */
731 8980 : all_coherence_zero = 1;
732 : #ifdef DEBUG_MODE_QMETADATA
733 : bits_no_dirs_coh = 0;
734 : #endif
735 8980 : if ( hQMetaData->q_direction->cfg.inactiveBands > 0 )
736 : {
737 1904 : push_next_indice( hMetaData, 1, 1 );
738 : /* write the number of inactive higher bands */
739 1904 : 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 7076 : push_next_indice( hMetaData, 0, 1 );
745 : }
746 8980 : if ( hQMetaData->coherence_flag )
747 : {
748 8980 : all_coherence_zero = hQMetaData->all_coherence_zero;
749 8980 : 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 8980 : if ( ndirections == 2 && bits_sph_idx == 11 )
757 : {
758 : #ifdef DEBUG_MODE_QMETADATA
759 : bits_no_dirs_coh +=
760 : #endif
761 1228 : write_2dir_info( hMetaData, hQMetaData->twoDirBands, hQMetaData->q_direction[0].cfg.nbands, hQMetaData->numTwoDirBands );
762 1228 : d = 0;
763 30362 : for ( i = hQMetaData->q_direction[1].cfg.start_band; i < hQMetaData->q_direction[1].cfg.nbands; i++ )
764 : {
765 29134 : if ( hQMetaData->twoDirBands[i] == 1 )
766 : {
767 12547 : mvr2r( hQMetaData->q_direction[1].band_data[i].azimuth, hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].cfg.nblocks );
768 12547 : mvr2r( hQMetaData->q_direction[1].band_data[i].elevation, hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].cfg.nblocks );
769 12547 : 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 12547 : if ( hQMetaData->coherence_flag )
772 : {
773 12547 : 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 12547 : d++;
776 : }
777 : }
778 17815 : for ( i = hQMetaData->numTwoDirBands; i < hQMetaData->q_direction[0].cfg.nbands; i++ )
779 : {
780 16587 : set_f( hQMetaData->q_direction[1].band_data[i].energy_ratio, 0.0f, hQMetaData->q_direction[1].cfg.nblocks );
781 : }
782 :
783 1228 : hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands;
784 : }
785 :
786 : /*Quantization and encoding of the Diffuseness */
787 8980 : ivas_qmetadata_quantize_diffuseness_nrg_ratios_hr_512( hQMetaData, bits_diff, bits_sph_idx, hMetaData );
788 :
789 : /* Encode surround coherence */
790 8980 : if ( all_coherence_zero == 0 )
791 : {
792 7576 : encode_surround_coherence_hr( hQMetaData, hMetaData );
793 : }
794 : else
795 : {
796 34675 : for ( i = 0; i < hQMetaData->q_direction[0].cfg.nbands; i++ )
797 : {
798 33271 : if ( hQMetaData->surcoh_band_data != NULL )
799 : {
800 33271 : 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 19810 : for ( d = 0; d < ndirections; d++ )
807 : {
808 10830 : q_direction = &( hQMetaData->q_direction[d] );
809 :
810 10830 : nbands = q_direction->cfg.nbands;
811 10830 : nblocks = q_direction->cfg.nblocks;
812 10830 : 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 10830 : q_direction->not_in_2D = 0;
835 :
836 : /*Coherence */
837 10830 : if ( all_coherence_zero == 0 )
838 : {
839 9421 : ivas_qmetadata_quantize_coherence_hr_512( hQMetaData, d, all_coherence_zero, hMetaData, bits_sp_coh );
840 : }
841 :
842 : /* write the spherical indexes */
843 10830 : bits_ec = hMetaData->nb_bits_tot;
844 10830 : if ( bits_sph_idx == 11 )
845 : {
846 : /* do the quantization */
847 6828 : quantize_direction_frame( q_direction, azimuth_orig, elevation_orig, 1 );
848 : }
849 :
850 248916 : for ( i = start_band; i < nbands; i++ )
851 : {
852 910815 : for ( j = 0; j < nblocks; j++ )
853 : {
854 672729 : push_next_indice( hMetaData, q_direction->band_data[i].spherical_index[j], bits_sph_idx );
855 : }
856 : }
857 10830 : 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 10830 : if ( bits_sph_idx == 11 )
921 : {
922 150660 : for ( i = start_band; i < nbands; i++ )
923 : {
924 143832 : mvr2r( azimuth_orig[i], q_direction->band_data[i].azimuth, nblocks );
925 143832 : mvr2r( elevation_orig[i], q_direction->band_data[i].elevation, nblocks );
926 : }
927 : }
928 : else
929 : {
930 98256 : for ( i = start_band; i < nbands; i++ )
931 : {
932 94254 : mvr2r( q_direction->band_data[i].azimuth, q_direction->band_data[i].q_azimuth, nblocks );
933 94254 : mvr2r( q_direction->band_data[i].elevation, q_direction->band_data[i].q_elevation, nblocks );
934 : }
935 : }
936 : }
937 :
938 8980 : if ( hQMetaData->q_direction->cfg.inactiveBands > 0 )
939 : {
940 1904 : hQMetaData->q_direction[0].cfg.nbands += hQMetaData->q_direction->cfg.inactiveBands;
941 1904 : if ( ndirections > 1 )
942 : {
943 302 : hQMetaData->q_direction[1].cfg.nbands += hQMetaData->q_direction->cfg.inactiveBands;
944 : }
945 : }
946 :
947 8980 : 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 15343 : 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 15343 : if ( ivas_format == SBA_FORMAT )
979 : {
980 3365 : sba_spar_bitlen = ivas_sba_spar_sid_bitlen( nchan_transport );
981 3365 : 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 11978 : 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 15343 : 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 15343 : if ( masa_sid_descriptor > -1 )
1007 : {
1008 6266 : push_next_indice( hMetaData, masa_sid_descriptor, 1 );
1009 : }
1010 :
1011 : /* Code for one direction: diffuseness and average DOA(s)*/
1012 15343 : q_direction = &( q_metadata->q_direction[0] );
1013 15343 : nbands = q_direction->cfg.nbands;
1014 15343 : nblocks = q_direction->cfg.nblocks;
1015 15343 : start_band = 0; /*Start always with band 0 for SID*/
1016 :
1017 : /* sanity checks*/
1018 15343 : assert( q_metadata->no_directions == 1 && "Qmetadata SID: only one direction supported!" );
1019 15343 : if ( ivas_format == SBA_FORMAT )
1020 : {
1021 3365 : assert( ( q_direction->cfg.nbands == DIRAC_DTX_BANDS ) && "Qmetadata SID: only 2 bands supported!" );
1022 : }
1023 : else
1024 : {
1025 11978 : assert( ( q_direction->cfg.nbands == 5 ) && "Qmetadata SID: only 5 bands supported!" );
1026 : }
1027 :
1028 15343 : if ( ivas_format != SBA_FORMAT )
1029 : {
1030 : /* Signalling 2D*/
1031 11978 : push_next_indice( hMetaData, ( q_direction->not_in_2D > 0 ), 1 ); /*2D flag*/
1032 : }
1033 : else
1034 : {
1035 3365 : q_direction->not_in_2D = 1; /* force for merged modes */
1036 : }
1037 :
1038 : /*Encode the quantized diffuseness in raw coding*/
1039 15343 : bits_dir = 0;
1040 15343 : bits_diff = 0;
1041 15343 : if ( ivas_format != SBA_FORMAT )
1042 : {
1043 71868 : for ( b = start_band; b < nbands; b++ )
1044 : {
1045 59890 : q_direction->band_data[b].energy_ratio_index[0] = max( q_direction->band_data[b].energy_ratio_index[0], 4 );
1046 59890 : bits_diff += ivas_qmetadata_encode_quasi_uniform_length( q_direction->band_data[b].energy_ratio_index[0] - 4, DIRAC_DIFFUSE_LEVELS - 4 );
1047 59890 : q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[q_direction->band_data[b].energy_ratio_index[0]];
1048 :
1049 59890 : if ( q_direction->not_in_2D == 0 )
1050 : {
1051 4475 : q_direction->band_data[b].azimuth_m_alphabet[0] = 1 << ( min( 8, q_direction->band_data[b].bits_sph_idx[0] ) );
1052 4475 : 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 55415 : bits_dir += q_direction->band_data[b].bits_sph_idx[0];
1057 : }
1058 : }
1059 :
1060 : /* Reduce bit demand by increasing diffuseness*/
1061 11978 : bits_delta = metadata_sid_bits - ( hMetaData->nb_bits_tot - bit_pos_start ) - bits_diff - bits_dir;
1062 11978 : if ( bits_delta > 0 )
1063 : {
1064 51198 : while ( bits_delta > 0 )
1065 : {
1066 212303 : for ( b = start_band; b < nbands && ( bits_delta > 0 ); b++ )
1067 : {
1068 173083 : if ( q_direction->band_data[b].bits_sph_idx[0] < 11 )
1069 : {
1070 173081 : bits_delta -= 1;
1071 173081 : q_direction->band_data[b].bits_sph_idx[0]++;
1072 : }
1073 : }
1074 : }
1075 :
1076 11978 : if ( q_direction->not_in_2D == 0 )
1077 : {
1078 5370 : for ( b = start_band; b < nbands; b++ )
1079 : {
1080 4475 : 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 10095 : for ( b = start_band; b < nbands; b++ )
1106 : {
1107 6730 : q_direction->band_data[b].energy_ratio_index[0] = max( q_direction->band_data[b].energy_ratio_index[0], 4 );
1108 6730 : bits_diff += ivas_qmetadata_encode_quasi_uniform_length( q_direction->band_data[b].energy_ratio_index[0] - 4, DIRAC_DIFFUSE_LEVELS - 4 );
1109 6730 : q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[q_direction->band_data[b].energy_ratio_index[0]];
1110 :
1111 6730 : 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 6730 : bits_dir += q_direction->band_data[b].bits_sph_idx[0];
1119 : }
1120 : }
1121 :
1122 : /* Reduce bit demand by increasing diffuseness*/
1123 3365 : bits_delta = metadata_sid_bits - ( hMetaData->nb_bits_tot - bit_pos_start ) - bits_diff - bits_dir;
1124 :
1125 3385 : while ( bits_delta < 0 && ( q_direction->not_in_2D > 0 ) )
1126 : {
1127 50 : for ( b = nbands - 1; b >= start_band && ( bits_delta < 0 ); b-- )
1128 : {
1129 30 : if ( q_direction->band_data[b].energy_ratio_index[0] < ( DIRAC_DIFFUSE_LEVELS - 1 ) )
1130 : {
1131 30 : bits_delta += q_direction->band_data[b].bits_sph_idx[0];
1132 30 : q_direction->band_data[b].energy_ratio_index[0]++;
1133 30 : q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[q_direction->band_data[b].energy_ratio_index[0]];
1134 30 : bits_delta -= q_direction->band_data[b].bits_sph_idx[0];
1135 : }
1136 : }
1137 : }
1138 : }
1139 15343 : assert( ( bits_delta >= 0 ) && "Bit budget in Qmetadata SID is violated!!!" );
1140 :
1141 : /*Code diffuseness*/
1142 81963 : for ( b = start_band; b < nbands; b++ )
1143 : {
1144 66620 : 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 81963 : for ( b = start_band; b < nbands; b++ )
1149 : {
1150 66620 : set_zero( avg_direction_vector, 3 );
1151 231757 : for ( m = 0; m < nblocks; m++ )
1152 : {
1153 : /*compute the average direction */
1154 165137 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[m], q_direction->band_data[b].elevation[m], direction_vector );
1155 165137 : v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
1156 : }
1157 :
1158 66620 : ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &avg_azimuth[b], &avg_elevation[b] );
1159 :
1160 : /* Quantize the average direction */
1161 66620 : if ( q_direction->not_in_2D == 0 )
1162 : {
1163 4475 : avg_elevation[b] = 0;
1164 4475 : 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 4475 : &q_direction->band_data[b].azimuth_index[0], q_direction->cfg.mc_ls_setup );
1166 : }
1167 : else
1168 : {
1169 62145 : 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 62145 : &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 66620 : q_direction->band_data[b].q_azimuth[0] = avg_azimuth[b];
1175 66620 : q_direction->band_data[b].q_elevation[0] = avg_elevation[b];
1176 :
1177 66620 : 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 15343 : if ( q_direction->not_in_2D > 0 )
1185 : {
1186 76593 : for ( b = start_band; b < nbands; b++ )
1187 : {
1188 62145 : 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 5370 : for ( b = start_band; b < nbands; b++ )
1194 : {
1195 4475 : 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 15343 : assert( ( hMetaData->nb_bits_tot - bit_pos_start ) <= metadata_sid_bits && "Too many written bits!" );
1222 30587 : while ( ( hMetaData->nb_bits_tot - bit_pos_start ) < metadata_sid_bits )
1223 : {
1224 15244 : push_next_indice( hMetaData, 0, 1 ); /*fill bit*/
1225 : }
1226 :
1227 15343 : return;
1228 : }
1229 :
1230 :
1231 : /*-------------------------------------------------------------------------
1232 : * reset_metadata_spatial()
1233 : *
1234 : * Reset metadata in spatial formats
1235 : *------------------------------------------------------------------------*/
1236 :
1237 173928 : 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 173928 : if ( core_brate == SID_2k40 || core_brate == FRAME_NO_DATA )
1251 : {
1252 4282 : if ( ( ivas_format == SBA_FORMAT || ivas_format == MASA_FORMAT ) && core_brate != FRAME_NO_DATA )
1253 : {
1254 616 : if ( ivas_format == SBA_FORMAT )
1255 : {
1256 : #ifdef DEBUGGING
1257 : assert( hMetaData->ind_list[0].nb_bits == 1 );
1258 : #endif
1259 411 : hMetaData->ind_list[0].value = 1;
1260 411 : metadata_sid_bits = (int16_t) ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS - SBA_PLANAR_BITS - SBA_ORDER_BITS;
1261 :
1262 411 : 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 3666 : reset_indices_enc( hMetaData, hMetaData->nb_ind_tot );
1312 : }
1313 :
1314 4282 : *total_brate = element_brate;
1315 : }
1316 169646 : else if ( ivas_format != SBA_FORMAT )
1317 : {
1318 : /* Reset SID metadata bits*/
1319 237926 : while ( hMetaData->nb_bits_tot > nb_bits_metadata )
1320 : {
1321 124135 : hMetaData->nb_ind_tot--;
1322 124135 : hMetaData->nb_bits_tot -= hMetaData->ind_list[hMetaData->nb_ind_tot].nb_bits;
1323 124135 : 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 173928 : return;
1334 : }
1335 :
1336 :
1337 : /*-------------------------------------------------------------------------
1338 : * quantize_direction2D()
1339 : *
1340 : *
1341 : *------------------------------------------------------------------------*/
1342 :
1343 : /*! r: quantized spherical index */
1344 128270 : 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 128270 : if ( no_cw < 2 )
1355 : {
1356 0 : *phi_q = 0;
1357 :
1358 0 : return 0;
1359 : }
1360 :
1361 128270 : if ( mc_format != MC_LS_SETUP_INVALID )
1362 : {
1363 106666 : id_phi = quantize_phi_chan_compand( phi + 180, phi_q, no_cw, 0, mc_format );
1364 : }
1365 : else
1366 : {
1367 21604 : id_phi = quantize_phi( phi + 180, 0, phi_q, no_cw );
1368 : }
1369 128270 : *phi_q -= 180;
1370 128270 : *index_phi = ivas_qmetadata_reorder_generic( id_phi - ( no_cw >> 1 ) );
1371 :
1372 128270 : idx_sph = id_phi;
1373 :
1374 128270 : return idx_sph;
1375 : }
1376 :
1377 :
1378 8980 : 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 8980 : needed_bits[0] = 0;
1388 8980 : needed_bits[1] = 0;
1389 :
1390 219591 : for ( j = hQMetaData->q_direction[0].cfg.start_band; j < hQMetaData->q_direction[0].cfg.nbands; ++j )
1391 : {
1392 812458 : for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ )
1393 : {
1394 601847 : index = masa_sq( 1.0f - hQMetaData->q_direction[0].band_data[j].energy_ratio[k], diffuseness_thresholds_hr, HR_MASA_ER_LEVELS );
1395 601847 : push_next_indice( hMetaData, index, MASA_BITS_ER_HR );
1396 601847 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index[k] = index;
1397 601847 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index_mod[k] = index;
1398 601847 : hQMetaData->q_direction[0].band_data[j].energy_ratio[k] = 1.0f - diffuseness_reconstructions_hr[index];
1399 601847 : needed_bits[0] += MASA_BITS_ER_HR;
1400 601847 : hQMetaData->q_direction[0].band_data[j].bits_sph_idx[k] = bits_dir_hr;
1401 : }
1402 : }
1403 :
1404 8980 : if ( hQMetaData->no_directions == 2 )
1405 : {
1406 : float ratioSum;
1407 1850 : if ( bits_dir_hr == 16 )
1408 : {
1409 15550 : for ( j = hQMetaData->q_direction[1].cfg.start_band; j < hQMetaData->q_direction[1].cfg.nbands; j++ )
1410 : {
1411 53688 : for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ )
1412 : {
1413 38760 : index = masa_sq( 1.0f - hQMetaData->q_direction[1].band_data[j].energy_ratio[k], diffuseness_thresholds_hr, HR_MASA_ER_LEVELS );
1414 38760 : push_next_indice( hMetaData, index, MASA_BITS_ER_HR );
1415 38760 : hQMetaData->q_direction[1].band_data[j].energy_ratio_index[k] = index;
1416 38760 : hQMetaData->q_direction[1].band_data[j].energy_ratio[k] = 1.0f - diffuseness_reconstructions_hr[index];
1417 :
1418 38760 : ratioSum = hQMetaData->q_direction[0].band_data[j].energy_ratio[k] + hQMetaData->q_direction[1].band_data[j].energy_ratio[k];
1419 38760 : if ( ratioSum > 1.0f )
1420 : {
1421 444 : hQMetaData->q_direction[0].band_data[j].energy_ratio[k] /= ratioSum;
1422 444 : hQMetaData->q_direction[1].band_data[j].energy_ratio[k] /= ratioSum;
1423 : }
1424 :
1425 38760 : needed_bits[1] += MASA_BITS_ER_HR;
1426 38760 : 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 1228 : k = 0;
1434 30362 : for ( j = hQMetaData->q_direction[0].cfg.start_band; j < hQMetaData->q_direction[0].cfg.nbands; j++ )
1435 : {
1436 29134 : if ( hQMetaData->twoDirBands[j] == 1 )
1437 : {
1438 12547 : pos_2dir_band[k] = j;
1439 12547 : k++;
1440 : }
1441 : else
1442 : {
1443 16587 : pos_2dir_band[k] = 0;
1444 : }
1445 : }
1446 13775 : for ( j = hQMetaData->q_direction[1].cfg.start_band; j < hQMetaData->q_direction[1].cfg.nbands; j++ )
1447 : {
1448 44669 : for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ )
1449 : {
1450 32122 : index = masa_sq( 1.0f - hQMetaData->q_direction[1].band_data[j].energy_ratio[k], diffuseness_thresholds_hr, HR_MASA_ER_LEVELS );
1451 32122 : push_next_indice( hMetaData, index, MASA_BITS_ER_HR );
1452 32122 : hQMetaData->q_direction[1].band_data[j].energy_ratio_index[k] = index;
1453 32122 : hQMetaData->q_direction[1].band_data[j].energy_ratio[k] = 1.0f - diffuseness_reconstructions_hr[index];
1454 :
1455 32122 : 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 32122 : if ( ratioSum > 1.0f )
1458 : {
1459 137 : hQMetaData->q_direction[0].band_data[pos_2dir_band[j]].energy_ratio[k] /= ratioSum;
1460 137 : hQMetaData->q_direction[1].band_data[j].energy_ratio[k] /= ratioSum;
1461 : }
1462 :
1463 32122 : needed_bits[1] += MASA_BITS_ER_HR;
1464 32122 : hQMetaData->q_direction[1].band_data[j].bits_sph_idx[k] = bits_dir_hr;
1465 : }
1466 : }
1467 : }
1468 : }
1469 :
1470 8980 : return;
1471 : }
1472 :
1473 :
1474 : /*-------------------------------------------------------------------------
1475 : * ivas_qmetadata_quantize_diffuseness_nrg_ratios()
1476 : *
1477 : * Quantize diffuseness
1478 : *------------------------------------------------------------------------*/
1479 :
1480 585308 : 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 585308 : nbits_diff[0] = 0;
1492 585308 : nbits_diff[1] = 0;
1493 585308 : needed_bits[0] = 0;
1494 585308 : needed_bits[1] = 0;
1495 585308 : dir2band = 0;
1496 :
1497 3807081 : for ( j = hQMetaData->q_direction[0].cfg.start_band; j < hQMetaData->q_direction[0].cfg.nbands; ++j )
1498 : {
1499 3221773 : if ( hQMetaData->no_directions == 2 && hQMetaData->twoDirBands[j] == 1 )
1500 645475 : {
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 645475 : if ( hodirac_flag )
1510 : {
1511 : /* already encoded as total and ratios in HO-DirAC */
1512 511940 : diffRatio = 1.f - hQMetaData->q_direction[0].band_data[j].energy_ratio[0];
1513 511940 : dfRatio = hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[0];
1514 : }
1515 : else
1516 : {
1517 133535 : dirRatio1 = hQMetaData->q_direction[0].band_data[j].energy_ratio[0];
1518 133535 : dirRatio2 = hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[0];
1519 133535 : sumRatio = dirRatio1 + dirRatio2;
1520 133535 : diffRatio = 1.0f - sumRatio;
1521 133535 : dfRatio = sumRatio < EPSILON ? 0.5f : dirRatio1 / sumRatio;
1522 : }
1523 :
1524 :
1525 645475 : index_diff = masa_sq( diffRatio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
1526 645475 : diffRatioQ = diffuseness_reconstructions[index_diff];
1527 :
1528 645475 : if ( hodirac_flag )
1529 : {
1530 511940 : dfRatio_bits = ivas_get_df_ratio_bits_hodirac( index_diff );
1531 : }
1532 : else
1533 : {
1534 133535 : dfRatio_bits = ivas_get_df_ratio_bits( index_diff );
1535 : }
1536 :
1537 645475 : dfRatioBits[dir2band] = dfRatio_bits;
1538 :
1539 645475 : dfRatio_qsteps = ( 1 << dfRatio_bits );
1540 645475 : if ( hodirac_flag )
1541 : {
1542 511940 : dfRatio_index = usquant( dfRatio, &dfRatioQ, 0.0f, 1.f / ( dfRatio_qsteps - 1 ), dfRatio_qsteps );
1543 511940 : dirRatio1Q = 1.f - diffRatioQ;
1544 511940 : dirRatio2Q = dfRatioQ;
1545 : }
1546 : else
1547 : {
1548 133535 : 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 133535 : dirRatio1Q = dfRatioQ * ( 1.0f - diffRatioQ );
1552 133535 : dirRatio2Q = ( 1.0f - diffRatioQ ) - dirRatio1Q;
1553 : }
1554 :
1555 645475 : 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 3144641 : for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ )
1561 : {
1562 2499166 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index[k] = index_diff;
1563 2499166 : hQMetaData->q_direction[0].band_data[j].energy_ratio[k] = dirRatio1Q;
1564 : }
1565 645475 : nbits_diff[0] += MASA_BITS_ER;
1566 :
1567 645475 : if ( hodirac_flag )
1568 : {
1569 : float tmp;
1570 511940 : index_dirRatio2Inv = usquant( dirRatio2Q, &tmp, 0.0f, 1.f / ( DIRAC_DIFFUSE_LEVELS - 1 ), DIRAC_DIFFUSE_LEVELS );
1571 : }
1572 : else
1573 : {
1574 133535 : index_dirRatio2Inv = masa_sq( 1.0f - dirRatio2Q, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
1575 : }
1576 :
1577 3144641 : for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ )
1578 : {
1579 2499166 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[k] = dfRatio_index;
1580 2499166 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[k] = dirRatio2Q;
1581 : }
1582 645475 : 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 645475 : masa_compensate_two_dir_energy_ratio_index( index_dirRatio1Inv, index_dirRatio2Inv, &index_dirRatio1Inv_mod, &index_dirRatio2Inv_mod, hodirac_flag );
1589 :
1590 3144641 : for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ )
1591 : {
1592 2499166 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index_mod[k] = index_dirRatio1Inv_mod;
1593 2499166 : hQMetaData->q_direction[0].band_data[j].bits_sph_idx[k] = bits_direction_masa[index_dirRatio1Inv_mod];
1594 : }
1595 645475 : needed_bits[0] += hQMetaData->q_direction[0].cfg.nblocks * bits_direction_masa[index_dirRatio1Inv_mod];
1596 :
1597 3144641 : for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ )
1598 : {
1599 2499166 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index_mod[k] = index_dirRatio2Inv_mod;
1600 2499166 : hQMetaData->q_direction[1].band_data[dir2band].bits_sph_idx[k] = bits_direction_masa[index_dirRatio2Inv_mod];
1601 : }
1602 645475 : needed_bits[1] += hQMetaData->q_direction[1].cfg.nblocks * bits_direction_masa[index_dirRatio2Inv_mod];
1603 :
1604 645475 : dir2band++;
1605 : }
1606 : else
1607 : {
1608 2576298 : index_dirRatio1Inv = masa_sq( 1.0f - hQMetaData->q_direction[0].band_data[j].energy_ratio[0], diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
1609 :
1610 9991050 : for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ )
1611 : {
1612 7414752 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index[k] = index_dirRatio1Inv;
1613 7414752 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index_mod[k] = index_dirRatio1Inv;
1614 7414752 : hQMetaData->q_direction[0].band_data[j].energy_ratio[k] = 1.0f - diffuseness_reconstructions[index_dirRatio1Inv];
1615 7414752 : hQMetaData->q_direction[0].band_data[j].bits_sph_idx[k] = bits_direction_masa[index_dirRatio1Inv];
1616 : }
1617 :
1618 2576298 : nbits_diff[0] += MASA_BITS_ER;
1619 :
1620 2576298 : needed_bits[0] += hQMetaData->q_direction[0].cfg.nblocks * bits_direction_masa[index_dirRatio1Inv];
1621 : }
1622 : }
1623 :
1624 585308 : return;
1625 : }
1626 :
1627 :
1628 : /*-------------------------------------------------------------------------
1629 : * ivas_diffuseness_huff_ec_encode()
1630 : *
1631 : *
1632 : *------------------------------------------------------------------------*/
1633 :
1634 1135394 : static int16_t ivas_diffuseness_huff_ec_encode(
1635 : BSTR_ENC_HANDLE hMetaData,
1636 : const uint16_t idx )
1637 : {
1638 : int16_t nbits;
1639 1135394 : nbits = 0;
1640 1135394 : if ( idx <= DIFF_EC_HUFF_GR0_LIMIT )
1641 : {
1642 1134851 : if ( idx > 0 )
1643 : {
1644 501088 : push_next_indice( hMetaData, ( 1 << idx ) - 1, idx );
1645 501088 : nbits += idx;
1646 : }
1647 1134851 : push_next_indice( hMetaData, 0, 1 );
1648 1134851 : nbits += 1;
1649 : }
1650 : else
1651 : {
1652 543 : push_next_indice( hMetaData, 511, DIFF_EC_HUFF_GR0_LIMIT + 1 );
1653 543 : push_next_indice( hMetaData, idx - DIFF_EC_HUFF_GR0_LIMIT - 1, 2 );
1654 543 : nbits += DIFF_EC_HUFF_GR0_LIMIT + 3;
1655 : }
1656 1135394 : return nbits;
1657 : }
1658 :
1659 :
1660 : /*-------------------------------------------------------------------------
1661 : * ivas_diffuseness_huff_ec_prepare()
1662 : *
1663 : *
1664 : *------------------------------------------------------------------------*/
1665 :
1666 119382 : 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 119382 : start_band = q_direction->cfg.start_band;
1680 119382 : nbands = q_direction->cfg.nbands;
1681 :
1682 119382 : *diffuseness_bits_huff = 0;
1683 119382 : av = 0;
1684 1704182 : for ( b = start_band; b < nbands; b++ )
1685 : {
1686 1584800 : av += q_direction->band_data[b].energy_ratio_index[0];
1687 : }
1688 119382 : av = (int16_t) ( 0.5f + av / (float) nbands );
1689 119382 : *best_av = av;
1690 :
1691 119382 : *diffuseness_bits_huff = MAX16B;
1692 477528 : for ( av_crt = av - 1; av_crt <= av + 1; av_crt++ )
1693 : {
1694 358146 : bits = 0;
1695 5112546 : for ( b = start_band; b < nbands; b++ )
1696 : {
1697 4754400 : sh_idx = q_direction->band_data[b].energy_ratio_index[0] - av_crt;
1698 4754400 : ui_sh_idx[b] = ( sh_idx <= 0 ) ? ( -2 * sh_idx ) : sh_idx * 2 - 1;
1699 4754400 : if ( ui_sh_idx[b] >= 2 * DIRAC_DIFFUSE_LEVELS - 3 )
1700 : {
1701 973 : bits = 100; /* to avoid difference larger than 6 in absolute value */
1702 : }
1703 :
1704 4754400 : bits += ( ui_sh_idx[b] <= DIFF_EC_HUFF_GR0_LIMIT ) ? ( ui_sh_idx[b] + 1 ) : 11;
1705 : }
1706 :
1707 358146 : if ( bits < *diffuseness_bits_huff )
1708 : {
1709 211078 : *diffuseness_bits_huff = bits;
1710 211078 : mvs2s( (int16_t *) ui_sh_idx, (int16_t *) avr_idx, nbands );
1711 211078 : *best_av = av_crt;
1712 : }
1713 : }
1714 :
1715 119382 : *diffuseness_bits_huff += MASA_BITS_ER; /* for the average */
1716 :
1717 119382 : return;
1718 : }
1719 :
1720 : /*-------------------------------------------------------------------------
1721 : * ivas_qmetadata_entropy_encode_diffuseness()
1722 : *
1723 : * encode diffuseness
1724 : *------------------------------------------------------------------------*/
1725 :
1726 585308 : 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 585308 : nbands = q_direction->cfg.nbands;
1739 585308 : start_band = q_direction->cfg.start_band;
1740 :
1741 585308 : start_bit_pos = hMetaData->nb_bits_tot;
1742 :
1743 585308 : if ( nbands == 1 )
1744 : {
1745 : /* If there is only one band, diffuseness should be coded directly as raw with no signaling. */
1746 15223 : push_next_indice( hMetaData, q_direction->band_data[0].energy_ratio_index[0], MASA_BITS_ER );
1747 15223 : *diffuseness_index_max_ec_frame = 5;
1748 15223 : return ( hMetaData->nb_bits_tot - start_bit_pos );
1749 : }
1750 :
1751 : /* compute the number of raw coding bits */
1752 570085 : diffuseness_bits_raw = 0;
1753 3776635 : for ( b = start_band; b < nbands; b++ )
1754 : {
1755 3206550 : diffuseness_bits_raw += ivas_qmetadata_encode_quasi_uniform_length( q_direction->band_data[b].energy_ratio_index[0], DIRAC_DIFFUSE_LEVELS );
1756 : }
1757 :
1758 570085 : min_diffuseness_m_index = q_direction->band_data[start_band].energy_ratio_index[0];
1759 570085 : max_diffuseness_m_index = q_direction->band_data[start_band].energy_ratio_index[0];
1760 :
1761 3776635 : for ( b = start_band; b < nbands; b++ )
1762 : {
1763 3206550 : if ( q_direction->band_data[b].energy_ratio_index[0] < min_diffuseness_m_index )
1764 : {
1765 376937 : min_diffuseness_m_index = q_direction->band_data[b].energy_ratio_index[0];
1766 : }
1767 :
1768 3206550 : if ( q_direction->band_data[b].energy_ratio_index[0] > max_diffuseness_m_index )
1769 : {
1770 417536 : max_diffuseness_m_index = q_direction->band_data[b].energy_ratio_index[0];
1771 : }
1772 : }
1773 :
1774 570085 : 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 450703 : if ( min_diffuseness_m_index == max_diffuseness_m_index ) /* all values are equal */
1779 : {
1780 110880 : push_next_indice( hMetaData, 0, 1 ); /* dif_use_raw_coding */
1781 110880 : push_next_indice( hMetaData, 1, 1 ); /* dif_have_unique_value */
1782 110880 : ivas_qmetadata_encode_quasi_uniform( hMetaData, min_diffuseness_m_index, DIRAC_DIFFUSE_LEVELS ); /* dif_unique_value */
1783 : }
1784 339823 : else if ( min_diffuseness_m_index + 1 == max_diffuseness_m_index ) /* only two consecutive values are present */
1785 : {
1786 77243 : push_next_indice( hMetaData, 0, 1 ); /* dif_use_raw_coding */
1787 77243 : push_next_indice( hMetaData, 0, 1 ); /* dif_have_unique_value */
1788 77243 : ivas_qmetadata_encode_quasi_uniform( hMetaData, min_diffuseness_m_index, DIRAC_DIFFUSE_LEVELS - 1 ); /* dif_min_value */
1789 :
1790 354430 : for ( b = start_band; b < nbands; b++ )
1791 : {
1792 277187 : 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 262580 : push_next_indice( hMetaData, 1, 1 ); /* dif_use_raw_coding */
1798 :
1799 1258821 : for ( b = start_band; b < nbands; b++ )
1800 : {
1801 996241 : 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 119382 : 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 119382 : if ( diffuseness_bits_huff < diffuseness_bits_raw )
1817 : {
1818 : /* Signal Huffman EC */
1819 85222 : push_next_indice( hMetaData, 0, 1 );
1820 85222 : push_next_indice( hMetaData, best_av, MASA_BITS_ER );
1821 1220616 : for ( b = start_band; b < nbands; b++ )
1822 : {
1823 1135394 : 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 34160 : push_next_indice( hMetaData, 1, 1 );
1833 483566 : for ( b = start_band; b < nbands; b++ )
1834 : {
1835 449406 : 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 570085 : *diffuseness_index_max_ec_frame = 5;
1841 : /* adaptively select the diffuseness_index_max_ec threshold */
1842 570085 : if ( min_diffuseness_m_index > 5 )
1843 : {
1844 166862 : *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 570085 : 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 85988 : 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 85988 : int16_t ec_mode = 0;
1873 : int16_t max_alphabet_size;
1874 :
1875 85988 : nbands = q_direction->cfg.nbands;
1876 85988 : start_band = q_direction->cfg.start_band;
1877 :
1878 85988 : start_bit_pos = hMetaData->nb_bits_tot;
1879 :
1880 85988 : if ( nbands == 1 )
1881 : {
1882 : /* If there is only one band, ratio should be coded directly as raw with no signaling. */
1883 12075 : push_next_indice( hMetaData, q_direction->band_data[0].energy_ratio_index[0], df_ratio_bits[0] );
1884 :
1885 12075 : return ( hMetaData->nb_bits_tot - start_bit_pos );
1886 : }
1887 :
1888 : /* compute the number of raw coding bits */
1889 73913 : bits_raw = 0;
1890 73913 : max_df_ratio_bits = 0;
1891 707313 : for ( b = start_band; b < nbands; b++ )
1892 : {
1893 633400 : bits_raw += df_ratio_bits[b];
1894 633400 : max_df_ratio_bits = max( df_ratio_bits[b], max_df_ratio_bits );
1895 : }
1896 :
1897 73913 : min_index = q_direction->band_data[start_band].energy_ratio_index[0];
1898 73913 : max_index = q_direction->band_data[start_band].energy_ratio_index[0];
1899 707313 : for ( b = start_band; b < nbands; b++ )
1900 : {
1901 633400 : if ( q_direction->band_data[b].energy_ratio_index[0] < min_index )
1902 : {
1903 56078 : min_index = q_direction->band_data[b].energy_ratio_index[0];
1904 : }
1905 :
1906 633400 : if ( q_direction->band_data[b].energy_ratio_index[0] > max_index )
1907 : {
1908 65439 : max_index = q_direction->band_data[b].energy_ratio_index[0];
1909 : }
1910 : }
1911 :
1912 : /* Decide what modes are possible */
1913 73913 : if ( bits_raw >= max_df_ratio_bits + 2 + nbands )
1914 : {
1915 44636 : ec_mode = 2;
1916 : }
1917 29277 : else if ( bits_raw >= max_df_ratio_bits + 1 )
1918 : {
1919 29277 : ec_mode = 1;
1920 : }
1921 : else
1922 : {
1923 0 : ec_mode = 0;
1924 : }
1925 73913 : max_alphabet_size = 1 << max_df_ratio_bits;
1926 :
1927 73913 : if ( min_index == max_index && ec_mode > 0 ) /* all values are equal */
1928 : {
1929 5306 : push_next_indice( hMetaData, 0, 1 ); /* Signal between EC and raw */
1930 5306 : if ( ec_mode > 1 )
1931 : {
1932 : /* Only use bit for signaling if necessary */
1933 1320 : push_next_indice( hMetaData, 0, 1 ); /* Signal between one value or bandwise diff mode */
1934 : }
1935 :
1936 5306 : ivas_qmetadata_encode_quasi_uniform( hMetaData, min_index, max_alphabet_size );
1937 : }
1938 68607 : else if ( min_index + 1 == max_index && ec_mode > 1 ) /* only two consecutive values are present */
1939 : {
1940 4468 : push_next_indice( hMetaData, 0, 1 );
1941 4468 : push_next_indice( hMetaData, 1, 1 );
1942 4468 : ivas_qmetadata_encode_quasi_uniform( hMetaData, min_index, max_alphabet_size - 1 );
1943 :
1944 36684 : for ( b = start_band; b < nbands; b++ )
1945 : {
1946 32216 : 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 64139 : if ( ec_mode > 0 )
1952 : {
1953 64139 : push_next_indice( hMetaData, 1, 1 ); /* Only signal raw mode if not implicitly using it */
1954 : }
1955 :
1956 640036 : for ( b = start_band; b < nbands; b++ )
1957 : {
1958 575897 : 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 73913 : return ( hMetaData->nb_bits_tot - start_bit_pos );
1963 : }
1964 :
1965 :
1966 : /*-------------------------------------------------------------------------
1967 : * restore_metadata_buffer()
1968 : *
1969 : * Restore metadata buffer
1970 : *------------------------------------------------------------------------*/
1971 :
1972 556614 : 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 23750294 : for ( i = next_ind_start; i < hMetaData->nb_ind_tot; i++ )
1980 : {
1981 23193680 : 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 556614 : hMetaData->nb_bits_tot = bit_pos_start;
1987 556614 : hMetaData->nb_ind_tot = next_ind_start;
1988 :
1989 556614 : 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 6258269 : 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 6258269 : bits = 30 - norm_l( alphabet_size ); /* bits = floor(log2(alphabet_size)) */
2012 6258269 : tresh = ( 1U << ( bits + 1 ) ) - alphabet_size;
2013 :
2014 6258269 : if ( value < tresh )
2015 : {
2016 5339021 : push_next_indice( hMetaData, value, bits );
2017 : }
2018 : else /* value >= tresh */
2019 : {
2020 919248 : push_next_indice( hMetaData, value + tresh, bits + 1 );
2021 : }
2022 :
2023 6258269 : 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 661304 : 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 661304 : int16_t nbits = 0, i;
2047 661304 : int16_t nbits1 = 0;
2048 : int16_t nb;
2049 :
2050 5325728 : for ( i = 0; i < no_data; i++ )
2051 : {
2052 4664424 : nb = ivas_qmetadata_encode_extended_gr_length( data[i], no_symb[i], GR_order );
2053 4664424 : nbits += nb;
2054 : }
2055 :
2056 661304 : if ( check_two_orders == 1 )
2057 : {
2058 4229508 : for ( i = 0; i < no_data; i++ )
2059 : {
2060 3695250 : nb = ivas_qmetadata_encode_extended_gr_length( data[i], no_symb[i], GR_order - 1 );
2061 3695250 : nbits1 += nb;
2062 : }
2063 :
2064 534258 : if ( nbits1 < nbits )
2065 : {
2066 312412 : nbits = nbits1 + 1;
2067 312412 : *real_GR_ord = GR_order - 1;
2068 : }
2069 : else
2070 : {
2071 221846 : nbits += 1;
2072 221846 : *real_GR_ord = GR_order;
2073 : }
2074 : }
2075 : else
2076 : {
2077 127046 : *real_GR_ord = GR_order;
2078 : }
2079 :
2080 661304 : 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 111991 : 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 111991 : int16_t no_data = 0;
2108 :
2109 111991 : *real_GR_ord = GR_order; /* Init to avoid sanitizer warnings */
2110 :
2111 459956 : for ( i = 0; i < no_data_in; i++ )
2112 : {
2113 347965 : if ( data_in[i] < MASA_NO_INDEX )
2114 : {
2115 347821 : no_symb_local[no_data] = no_symb[i];
2116 347821 : data[no_data++] = data_in[i];
2117 : }
2118 : }
2119 :
2120 111991 : if ( no_data == 0 )
2121 : {
2122 112 : *p_use_context = -3; /* corresponding to nothing to be written */
2123 112 : return 0;
2124 : }
2125 :
2126 111879 : nbits = 0;
2127 111879 : use_context = 0;
2128 :
2129 459700 : for ( i = 0; i < no_data; i++ )
2130 : {
2131 347821 : if ( ( bits_dir[i] <= 1 ) )
2132 : {
2133 0 : nbits += bits_dir[i];
2134 0 : use_context = 1;
2135 : }
2136 : else
2137 : {
2138 347821 : *real_GR_ord = GR_order - ( bits_dir[i] == 2 );
2139 347821 : nbits += ivas_qmetadata_encode_extended_gr_length( data[i], no_symb_local[i], *real_GR_ord );
2140 : }
2141 : }
2142 :
2143 111879 : real_GR_ord1 = 0;
2144 111879 : if ( use_context == 0 )
2145 : {
2146 111879 : nbits = GR_bits_new( data, no_symb_local, no_data, GR_order, 1, real_GR_ord );
2147 111879 : nbits1 = nbits;
2148 :
2149 111879 : min_val = data[0];
2150 347821 : for ( i = 1; i < no_data; i++ )
2151 : {
2152 235942 : if ( data[i] < min_val )
2153 : {
2154 41190 : min_val = data[i];
2155 : }
2156 : }
2157 459700 : for ( i = 0; i < no_data; i++ )
2158 : {
2159 347821 : cdata[i] = data[i] - min_val;
2160 : }
2161 :
2162 111879 : maximum_s( no_symb_local, no_data, &max_val );
2163 111879 : 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 );
2164 :
2165 111879 : if ( nbits1 < nbits )
2166 : {
2167 47990 : nbits = nbits1 + 1;
2168 47990 : use_context = -2;
2169 47990 : *real_GR_ord = real_GR_ord1;
2170 : }
2171 : else
2172 : {
2173 63889 : nbits = nbits + 1;
2174 63889 : use_context = -1;
2175 : }
2176 : }
2177 :
2178 111879 : *p_use_context = use_context;
2179 :
2180 111879 : return nbits;
2181 : }
2182 :
2183 :
2184 : /*-------------------------------------------------------------------------
2185 : * mean_removed_GR_new()
2186 : *
2187 : * Golomb Rice encoding with mean removing
2188 : *------------------------------------------------------------------------*/
2189 :
2190 : /*! r: number of bits used */
2191 156312 : static int16_t mean_removed_GR_new(
2192 : const uint16_t *idx, /* i : data to encode */
2193 : const int16_t max_no_symb,
2194 : const int16_t len, /* i : number of data */
2195 : const int16_t adapt_GR, /* i : flag for telling to use or nor two GR order values */
2196 : int16_t *GR_ord, /* i/o: GR order */
2197 : uint16_t *p_av, /* o : average index */
2198 : uint16_t *mr_idx /* o : mean removed indexes */
2199 : )
2200 : {
2201 : int16_t av, i, nbits;
2202 : int16_t sh_idx[MASA_MAXIMUM_CODING_SUBBANDS];
2203 : int16_t max_ns[MASA_MAXIMUM_CODING_SUBBANDS];
2204 :
2205 156312 : av = (int16_t) ( 0.5f + sum_s( (const int16_t *) idx, len ) / (float) len );
2206 156312 : *p_av = av;
2207 1763708 : for ( i = 0; i < len; i++ )
2208 : {
2209 1607396 : max_ns[i] = 2 * ( max_no_symb );
2210 1607396 : sh_idx[i] = idx[i] - av;
2211 : }
2212 :
2213 1763708 : for ( i = 0; i < len; i++ )
2214 : {
2215 1607396 : if ( sh_idx[i] < 0 )
2216 : {
2217 173409 : sh_idx[i] = -2 * sh_idx[i];
2218 : }
2219 1433987 : else if ( sh_idx[i] > 0 )
2220 : {
2221 209161 : sh_idx[i] = sh_idx[i] * 2 - 1;
2222 : }
2223 : else
2224 : {
2225 1224826 : sh_idx[i] = 0;
2226 : }
2227 1607396 : mr_idx[i] = (uint16_t) sh_idx[i];
2228 : }
2229 :
2230 156312 : nbits = GR_bits_new( mr_idx, max_ns, len, *GR_ord, adapt_GR, GR_ord );
2231 :
2232 156312 : return nbits;
2233 : }
2234 :
2235 :
2236 : /*-------------------------------------------------------------------------
2237 : * ivas_qmetadata_encode_quasi_uniform_length()
2238 : *
2239 : *------------------------------------------------------------------------*/
2240 :
2241 26569921 : static int16_t ivas_qmetadata_encode_quasi_uniform_length(
2242 : const uint16_t value,
2243 : const uint16_t alphabet_size )
2244 : {
2245 : int16_t bits;
2246 : uint16_t tresh;
2247 : #ifdef DEBUGGING
2248 : assert( ( alphabet_size >= 1 ) );
2249 : assert( value < alphabet_size );
2250 : #endif
2251 :
2252 26569921 : bits = 30 - norm_l( alphabet_size ); /* bits = floor(log2(alphabet_size)) */
2253 26569921 : tresh = ( 1U << ( bits + 1 ) ) - alphabet_size;
2254 :
2255 26569921 : if ( value >= tresh )
2256 : {
2257 5953876 : bits++;
2258 : }
2259 :
2260 26569921 : return bits;
2261 : }
2262 :
2263 :
2264 : /*-------------------------------------------------------------------------
2265 : * ivas_qmetadata_entropy_encode_dir()
2266 : *
2267 : * Main function for entropy coding of the directions
2268 : *------------------------------------------------------------------------*/
2269 :
2270 838853 : static int16_t ivas_qmetadata_entropy_encode_dir(
2271 : BSTR_ENC_HANDLE hMetaData,
2272 : IVAS_QDIRECTION *q_direction,
2273 : const uint16_t diffuseness_index_max_ec_frame,
2274 : const int16_t nbands,
2275 : const int16_t start_band,
2276 : const int16_t direction_bits_raw,
2277 : int16_t max_bits,
2278 : const int16_t hrmasa_flag )
2279 : {
2280 : uint16_t diff_idx_min;
2281 : int16_t i, j;
2282 : int16_t nblocks;
2283 :
2284 : float avg_direction_vector[3], direction_vector[3], avg_azimuth, avg_elevation;
2285 : int16_t avg_azimuth_alphabet, avg_elevation_alphabet;
2286 : uint16_t avg_azimuth_index, avg_elevation_index;
2287 : int16_t avg_elevation_index_projected;
2288 : int16_t avg_azimuth_index_projected;
2289 : uint16_t avg_elevation_index_initial, avg_elevation_offset;
2290 : uint16_t avg_azimuth_index_initial, avg_azimuth_offset;
2291 : int16_t elevation_bits_ec_best, azimuth_bits_ec_best;
2292 :
2293 838853 : int16_t gr_param_elevation_best = 0, avg_elevation_index_best = 0;
2294 : uint16_t dist_elevation_indexes_best[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2295 : int16_t gr_param_azimuth_best, avg_azimuth_index_best;
2296 : uint16_t dist_azimuth_indexes_best[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2297 :
2298 : uint16_t idx, dist_count;
2299 : int16_t direction_bits_ec;
2300 :
2301 : uint16_t dist_elevation_indexes[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2302 : uint16_t dist_elevation_alphabets[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2303 : uint16_t dist_azimuth_indexes[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2304 : uint16_t dist_azimuth_alphabets[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2305 838853 : int16_t all_zero_dist_elevation_indexes = 1, all_zero_dist_azimuth_indexes = 1;
2306 : int16_t gr_param_elevation, gr_size_elevation, egr_size_elevation, gr_param_azimuth, gr_size_azimuth;
2307 : int16_t egr_size_azimuth, elevation_bits_ec, azimuth_bits_ec;
2308 :
2309 : float abs_theta;
2310 : float theta_cb[MAX_NO_THETA];
2311 : int16_t sign_th, no_th;
2312 838853 : int16_t avg_azimuth_index_upd = 0, use_adapt_avg;
2313 838853 : int16_t make_gain = 0;
2314 838853 : int16_t bits_gained = 0;
2315 838853 : nblocks = q_direction->cfg.nblocks;
2316 :
2317 : /* estimate the number of bits for entropy coding of the direction values */
2318 838853 : direction_bits_ec = 0;
2319 838853 : diff_idx_min = DIRAC_DIFFUSE_LEVELS;
2320 838853 : idx = 0;
2321 838853 : dist_count = 0;
2322 838853 : set_zero( avg_direction_vector, 3 );
2323 :
2324 4873658 : for ( i = start_band; i < nbands; i++ )
2325 : {
2326 4034805 : if ( hrmasa_flag )
2327 : {
2328 0 : diff_idx_min = 0; // min( q_direction->band_data[i].energy_ratio_index_mod[0]>>1, diff_idx_min );
2329 : }
2330 : else
2331 : {
2332 4034805 : diff_idx_min = min( q_direction->band_data[i].energy_ratio_index_mod[0], diff_idx_min );
2333 : }
2334 :
2335 4034805 : if ( q_direction->band_data[i].energy_ratio_index_mod[0] > diffuseness_index_max_ec_frame )
2336 : {
2337 : /* estimate the raw part */
2338 1068203 : if ( q_direction->not_in_2D > 0 )
2339 : {
2340 3633822 : for ( j = 0; j < nblocks; j++ )
2341 : {
2342 2762025 : direction_bits_ec += q_direction->band_data[i].bits_sph_idx[j];
2343 : }
2344 : }
2345 : else
2346 : {
2347 763678 : for ( j = 0; j < nblocks; j++ )
2348 : {
2349 567272 : 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] );
2350 : }
2351 : }
2352 : }
2353 : else
2354 : {
2355 2966602 : dist_count += nblocks;
2356 :
2357 12720617 : for ( j = 0; j < nblocks; j++ )
2358 : {
2359 : /*compute the average direction */
2360 9754015 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[i].azimuth[j], q_direction->band_data[i].elevation[j], direction_vector );
2361 9754015 : v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
2362 : }
2363 : }
2364 : }
2365 :
2366 : /* quantize average elevation and azimuth angles using the best angle spacing and equatorial precision */
2367 838853 : ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &avg_azimuth, &avg_elevation );
2368 :
2369 838853 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2370 : {
2371 26535 : avg_elevation_alphabet = no_theta_masa[bits_direction_masa[diff_idx_min] - 3];
2372 26535 : avg_azimuth_alphabet = no_phi_masa[bits_direction_masa[diff_idx_min] - 1][0]; /* average azimuth is quantized on the equatorial plane */
2373 : }
2374 : else
2375 : {
2376 812318 : avg_elevation_alphabet = no_theta_masa[bits_direction_masa[diff_idx_min] - 3] * 2 - 1;
2377 812318 : avg_azimuth_alphabet = no_phi_masa[bits_direction_masa[diff_idx_min] - 1][0]; /* average azimuth is quantized on the equatorial plane */
2378 : }
2379 :
2380 838853 : no_th = no_theta_masa[bits_direction_masa[diff_idx_min] - 3];
2381 :
2382 8171194 : for ( i = 0; i < no_th; i++ )
2383 : {
2384 7332341 : theta_cb[i] = i * delta_theta_masa[bits_direction_masa[diff_idx_min] - 3];
2385 : }
2386 :
2387 838853 : if ( theta_cb[i - 1] > 90 )
2388 : {
2389 478179 : theta_cb[i - 1] = 90;
2390 : }
2391 :
2392 838853 : if ( avg_elevation < 0 )
2393 : {
2394 331625 : abs_theta = -avg_elevation;
2395 331625 : sign_th = -1;
2396 : }
2397 : else
2398 : {
2399 507228 : abs_theta = avg_elevation;
2400 507228 : sign_th = 1;
2401 : }
2402 :
2403 838853 : avg_elevation_index = squant( abs_theta, &avg_elevation, theta_cb, no_th );
2404 :
2405 838853 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2406 : {
2407 26535 : assert( avg_elevation >= 0 );
2408 : }
2409 : else
2410 : {
2411 812318 : if ( sign_th < 0 )
2412 : {
2413 331625 : avg_elevation_index = ( avg_elevation_alphabet >> 1 ) - avg_elevation_index;
2414 : }
2415 : else
2416 : {
2417 480693 : avg_elevation_index += ( avg_elevation_alphabet >> 1 );
2418 : }
2419 812318 : avg_elevation *= sign_th;
2420 : }
2421 :
2422 838853 : avg_azimuth_index = (uint16_t) ( quantize_phi( avg_azimuth + 180, 0, &avg_azimuth, avg_azimuth_alphabet ) );
2423 :
2424 : /* Elevation only if not 2D */
2425 838853 : if ( q_direction->not_in_2D > 0 )
2426 : {
2427 665754 : avg_elevation_index_initial = avg_elevation_index;
2428 665754 : elevation_bits_ec_best = MAX16B;
2429 665754 : avg_elevation_index_best = -1; /* out of range value */
2430 665754 : gr_param_elevation_best = -1; /* out of range value */
2431 :
2432 2663016 : for ( avg_elevation_offset = 0; avg_elevation_offset < q_direction->cfg.search_effort; avg_elevation_offset++ )
2433 : {
2434 1997262 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2435 : {
2436 37710 : avg_elevation_index = avg_elevation_index_initial + avg_elevation_offset;
2437 : }
2438 : else
2439 : {
2440 1959552 : avg_elevation_index = (uint16_t) ( avg_elevation_index_initial + ivas_qmetadata_dereorder_generic( avg_elevation_offset ) );
2441 : }
2442 1997262 : avg_elevation_index = (uint16_t) ( ( avg_elevation_index + avg_elevation_alphabet ) % avg_elevation_alphabet );
2443 :
2444 1997262 : all_zero_dist_elevation_indexes = 1;
2445 1997262 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2446 : {
2447 37710 : elevation_bits_ec = ivas_qmetadata_encode_quasi_uniform_length( avg_elevation_index, avg_elevation_alphabet );
2448 : }
2449 : else
2450 : {
2451 1959552 : elevation_bits_ec = ivas_qmetadata_encode_quasi_uniform_length( ivas_qmetadata_reorder_generic( avg_elevation_index - ( avg_elevation_alphabet >> 1 ) ), avg_elevation_alphabet );
2452 : }
2453 1997262 : idx = 0;
2454 11542377 : for ( i = start_band; i < nbands; i++ )
2455 : {
2456 9545115 : if ( q_direction->band_data[i].energy_ratio_index_mod[0] <= diffuseness_index_max_ec_frame )
2457 : {
2458 30382692 : for ( j = 0; j < nblocks; j++ )
2459 : {
2460 : /* project the quantized average elevation to the same grid as the current sample */
2461 23452968 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2462 : {
2463 228669 : avg_elevation_index_projected = ivas_chan_project_elevation_index( avg_elevation_index, avg_elevation_alphabet, q_direction->band_data[i].elevation_m_alphabet[j] );
2464 : }
2465 : else
2466 : {
2467 23224299 : avg_elevation_index_projected = ivas_dirac_project_elevation_index( avg_elevation_index, avg_elevation_alphabet, q_direction->band_data[i].elevation_m_alphabet[j] );
2468 : }
2469 : #ifdef DEBUGGING
2470 : assert( ( 0 <= avg_elevation_index_projected ) && ( avg_elevation_index_projected < q_direction->band_data[i].elevation_m_alphabet[j] ) );
2471 : #endif
2472 :
2473 23452968 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2474 : {
2475 228669 : if ( q_direction->band_data[i].elevation_index[j] - avg_elevation_index_projected > 0 )
2476 : {
2477 10100 : dist_elevation_indexes[idx] = 2 * ( q_direction->band_data[i].elevation_index[j] - avg_elevation_index_projected ) - 1;
2478 : }
2479 : else
2480 : {
2481 218569 : dist_elevation_indexes[idx] = -2 * ( q_direction->band_data[i].elevation_index[j] - avg_elevation_index_projected );
2482 : }
2483 : }
2484 : else
2485 : {
2486 23224299 : 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] );
2487 : }
2488 23452968 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2489 : {
2490 228669 : dist_elevation_alphabets[idx] = 2 * q_direction->band_data[i].elevation_m_alphabet[j] - 1;
2491 : }
2492 : else
2493 : {
2494 23224299 : dist_elevation_alphabets[idx] = q_direction->band_data[i].elevation_m_alphabet[j];
2495 : }
2496 :
2497 23452968 : if ( dist_elevation_indexes[idx] != 0 )
2498 : {
2499 14727826 : all_zero_dist_elevation_indexes = 0;
2500 : }
2501 23452968 : idx++;
2502 : }
2503 : }
2504 : }
2505 :
2506 1997262 : if ( all_zero_dist_elevation_indexes )
2507 : {
2508 242157 : egr_size_elevation = 0;
2509 242157 : gr_param_elevation = 4;
2510 : }
2511 : else
2512 : {
2513 1755105 : gr_param_elevation = ivas_qmetadata_get_optimal_gr_param( dist_elevation_indexes, idx, 4, &gr_size_elevation );
2514 1755105 : egr_size_elevation = 0;
2515 24787775 : for ( i = 0; i < idx; i++ )
2516 : {
2517 23032670 : egr_size_elevation += ivas_qmetadata_encode_extended_gr_length( dist_elevation_indexes[i], dist_elevation_alphabets[i], gr_param_elevation );
2518 : }
2519 : #ifdef DEBUGGING
2520 : assert( egr_size_elevation <= gr_size_elevation );
2521 : #endif
2522 : }
2523 1997262 : elevation_bits_ec += ivas_qmetadata_encode_quasi_uniform_length( gr_param_elevation, 4 + 1 ) + egr_size_elevation;
2524 :
2525 1997262 : if ( elevation_bits_ec < elevation_bits_ec_best )
2526 : {
2527 872715 : elevation_bits_ec_best = elevation_bits_ec;
2528 872715 : avg_elevation_index_best = avg_elevation_index;
2529 872715 : gr_param_elevation_best = gr_param_elevation;
2530 12261475 : for ( idx = 0; idx < dist_count; idx++ )
2531 : {
2532 11388760 : dist_elevation_indexes_best[idx] = dist_elevation_indexes[idx];
2533 : }
2534 : }
2535 : }
2536 :
2537 665754 : direction_bits_ec += elevation_bits_ec_best;
2538 : }
2539 :
2540 : /*Azimuth*/
2541 838853 : use_adapt_avg = 0;
2542 838853 : if ( ( nbands - start_band >= 5 ) && ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( nblocks > 1 ) )
2543 : {
2544 6115 : use_adapt_avg = calc_var_azi( q_direction, diffuseness_index_max_ec_frame, avg_azimuth - 180, &avg_azimuth );
2545 6115 : avg_azimuth_index = (uint16_t) ( quantize_phi( avg_azimuth + 180, 0, &avg_azimuth, avg_azimuth_alphabet ) );
2546 : }
2547 838853 : avg_azimuth_index_initial = avg_azimuth_index; /* avg_azimuth_index;*/
2548 838853 : azimuth_bits_ec_best = MAX16B;
2549 838853 : avg_azimuth_index_best = -1; /* out of range value */
2550 838853 : gr_param_azimuth_best = -1; /* out of range value */
2551 :
2552 3355412 : for ( avg_azimuth_offset = 0; avg_azimuth_offset < q_direction->cfg.search_effort; avg_azimuth_offset++ )
2553 : {
2554 2516559 : set_zero( avg_direction_vector, 3 );
2555 2516559 : avg_azimuth_index = (uint16_t) ( avg_azimuth_index_initial + ivas_qmetadata_dereorder_generic( avg_azimuth_offset ) );
2556 2516559 : avg_azimuth_index = (uint16_t) ( ( avg_azimuth_index + avg_azimuth_alphabet ) % avg_azimuth_alphabet );
2557 2516559 : all_zero_dist_azimuth_indexes = 1;
2558 2516559 : azimuth_bits_ec = ivas_qmetadata_encode_quasi_uniform_length( ivas_qmetadata_reorder_generic( avg_azimuth_index - ( avg_azimuth_alphabet >> 1 ) ), avg_azimuth_alphabet );
2559 :
2560 2516559 : idx = 0;
2561 14620974 : for ( i = start_band; i < nbands; i++ )
2562 : {
2563 12104415 : if ( q_direction->band_data[i].energy_ratio_index_mod[0] <= diffuseness_index_max_ec_frame )
2564 : {
2565 38161851 : for ( j = 0; j < nblocks; j++ )
2566 : {
2567 :
2568 29262045 : if ( ( idx > MASA_LIMIT_IDX_AVG_AZI ) && ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( use_adapt_avg == 1 ) )
2569 : {
2570 118083 : 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] );
2571 : }
2572 : else
2573 : {
2574 29143962 : if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( use_adapt_avg == 1 ) )
2575 : {
2576 66069 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[i].azimuth[j], q_direction->band_data[i].elevation[j], direction_vector );
2577 66069 : if ( idx < 4 )
2578 : {
2579 53988 : v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
2580 : }
2581 : }
2582 : /* project the quantized average azimuth angle to the same grid as the current sample */
2583 29143962 : avg_azimuth_index_projected = ivas_dirac_project_azimuth_index( avg_azimuth_index, avg_azimuth_alphabet, q_direction->band_data[i].azimuth_m_alphabet[j] );
2584 : }
2585 : #ifdef DEBUGGING
2586 : assert( ( 0 <= avg_azimuth_index_projected ) && ( avg_azimuth_index_projected < q_direction->band_data[i].azimuth_m_alphabet[j] ) );
2587 : #endif
2588 29262045 : 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] );
2589 29262045 : dist_azimuth_alphabets[idx] = q_direction->band_data[i].azimuth_m_alphabet[j];
2590 :
2591 29262045 : if ( dist_azimuth_indexes[idx] != 0 )
2592 : {
2593 21366308 : all_zero_dist_azimuth_indexes = 0;
2594 : }
2595 :
2596 29262045 : if ( ( idx >= MASA_LIMIT_IDX_AVG_AZI ) && ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( use_adapt_avg == 1 ) )
2597 : {
2598 130164 : if ( idx % nblocks == 0 )
2599 : {
2600 32541 : v_multc( avg_direction_vector, 0.5f, avg_direction_vector, 3 );
2601 : }
2602 :
2603 : /*compute the average direction per already coded subband */
2604 130164 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[i].azimuth[j], q_direction->band_data[i].elevation[j], direction_vector );
2605 130164 : v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
2606 130164 : ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &avg_azimuth, &avg_elevation );
2607 130164 : avg_azimuth_index_upd = quantize_phi( avg_azimuth + 180, 0, &avg_azimuth, avg_azimuth_alphabet );
2608 : }
2609 29262045 : idx++;
2610 : }
2611 : }
2612 : }
2613 :
2614 2516559 : if ( all_zero_dist_azimuth_indexes )
2615 : {
2616 291399 : egr_size_azimuth = 0;
2617 291399 : gr_param_azimuth = 5;
2618 : }
2619 : else
2620 : {
2621 : /* estimate the ExtendedGR part for azimuth */
2622 2225160 : gr_param_azimuth = ivas_qmetadata_get_optimal_gr_param( dist_azimuth_indexes, idx, 5, &gr_size_azimuth );
2623 2225160 : egr_size_azimuth = 0;
2624 30609339 : for ( i = 0; i < idx; i++ )
2625 : {
2626 28384179 : egr_size_azimuth += ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes[i], dist_azimuth_alphabets[i], gr_param_azimuth );
2627 : }
2628 : #ifdef DEBUGGING
2629 : assert( egr_size_azimuth <= gr_size_azimuth );
2630 : #endif
2631 : }
2632 :
2633 2516559 : azimuth_bits_ec += ivas_qmetadata_encode_quasi_uniform_length( gr_param_azimuth, 5 + 1 ) + egr_size_azimuth;
2634 :
2635 2516559 : if ( azimuth_bits_ec < azimuth_bits_ec_best )
2636 : {
2637 1168131 : azimuth_bits_ec_best = azimuth_bits_ec;
2638 1168131 : avg_azimuth_index_best = avg_azimuth_index;
2639 1168131 : gr_param_azimuth_best = gr_param_azimuth;
2640 :
2641 15815528 : for ( idx = 0; idx < dist_count; idx++ )
2642 : {
2643 14647397 : dist_azimuth_indexes_best[idx] = dist_azimuth_indexes[idx];
2644 : }
2645 : }
2646 : }
2647 :
2648 838853 : if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( dist_count > 4 ) && ( gr_param_azimuth_best != 5 ) && ( nblocks > 1 ) )
2649 : {
2650 5358 : azimuth_bits_ec_best += 1;
2651 : }
2652 :
2653 838853 : direction_bits_ec += azimuth_bits_ec_best;
2654 :
2655 : /*Decision raw or EC*/
2656 : /* one bit is used to indicate whether the direction values are entropy coded or coded raw */
2657 838853 : if ( direction_bits_ec < direction_bits_raw ) /* entropy coding is better */
2658 : {
2659 :
2660 : /* encode the raw part first */
2661 3358533 : for ( i = start_band; i < nbands; i++ )
2662 : {
2663 2832983 : if ( q_direction->band_data[i].energy_ratio_index_mod[0] > diffuseness_index_max_ec_frame )
2664 : {
2665 655589 : if ( q_direction->not_in_2D > 0 )
2666 : {
2667 2462947 : for ( j = 0; j < nblocks; j++ )
2668 : {
2669 1925249 : push_next_indice( hMetaData, q_direction->band_data[i].spherical_index[j], q_direction->band_data[i].bits_sph_idx[j] );
2670 : }
2671 : }
2672 : else
2673 : {
2674 508119 : for ( j = 0; j < nblocks; j++ )
2675 : {
2676 390228 : ivas_qmetadata_encode_quasi_uniform( hMetaData, q_direction->band_data[i].azimuth_index[j], q_direction->band_data[i].azimuth_m_alphabet[j] );
2677 : }
2678 : }
2679 : }
2680 : }
2681 :
2682 525550 : if ( nbands > 1 && direction_bits_ec - max_bits > 0 && direction_bits_ec - max_bits < nblocks * nbands )
2683 : {
2684 11220 : make_gain = 1;
2685 : }
2686 :
2687 525550 : if ( q_direction->not_in_2D > 0 )
2688 : {
2689 : /* encode the ExtendedGR part for elevation */
2690 410701 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2691 : {
2692 7446 : ivas_qmetadata_encode_quasi_uniform( hMetaData, avg_elevation_index_best, avg_elevation_alphabet );
2693 : }
2694 : else
2695 : {
2696 403255 : ivas_qmetadata_encode_quasi_uniform( hMetaData, ivas_qmetadata_reorder_generic( avg_elevation_index_best - ( avg_elevation_alphabet >> 1 ) ), avg_elevation_alphabet );
2697 : }
2698 :
2699 410701 : ivas_qmetadata_encode_quasi_uniform( hMetaData, gr_param_elevation_best, 4 + 1 );
2700 :
2701 410701 : if ( gr_param_elevation_best != 4 ) /* not all zero */
2702 : {
2703 5891098 : for ( idx = 0; idx < dist_count; idx++ )
2704 : {
2705 5568575 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_elevation_indexes_best[idx], dist_elevation_alphabets[idx], gr_param_elevation_best );
2706 : }
2707 : }
2708 : }
2709 :
2710 : /* encode the ExtendedGR part for azimuth */
2711 525550 : ivas_qmetadata_encode_quasi_uniform( hMetaData, ivas_qmetadata_reorder_generic( avg_azimuth_index_best - ( avg_azimuth_alphabet >> 1 ) ), avg_azimuth_alphabet );
2712 :
2713 525550 : ivas_qmetadata_encode_quasi_uniform( hMetaData, gr_param_azimuth_best, 5 + 1 );
2714 :
2715 525550 : if ( gr_param_azimuth_best != 5 ) /* not all zero */
2716 : {
2717 2088068 : for ( idx = 0; idx < min( nblocks, dist_count ); idx++ )
2718 : {
2719 1647163 : if ( make_gain == 1 && bits_gained < direction_bits_ec - max_bits && dist_azimuth_alphabets[idx] > 40 )
2720 : {
2721 9209 : if ( dist_azimuth_indexes_best[idx] > 1 )
2722 : {
2723 7033 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx] - 2, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2724 7033 : bits_gained += ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best ) -
2725 7033 : ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx] - 2, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2726 : }
2727 2176 : else if ( dist_azimuth_indexes_best[idx] == 1 )
2728 : {
2729 970 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx] - 1, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2730 970 : bits_gained += ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best ) -
2731 970 : ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx] - 1, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2732 : }
2733 : else
2734 : {
2735 1206 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2736 : }
2737 : }
2738 : else
2739 : {
2740 1637954 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2741 : }
2742 : }
2743 :
2744 440905 : if ( dist_count > nblocks )
2745 : {
2746 308722 : if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( nblocks > 1 ) )
2747 : {
2748 4267 : push_next_indice( hMetaData, use_adapt_avg, 1 );
2749 : }
2750 5434493 : for ( idx = nblocks; idx < dist_count; idx++ )
2751 : {
2752 5125771 : if ( make_gain == 1 && bits_gained < direction_bits_ec - max_bits && dist_azimuth_alphabets[idx] > 40 )
2753 : {
2754 63485 : if ( dist_azimuth_indexes_best[idx] > 1 )
2755 : {
2756 45133 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx] - 2, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2757 45133 : bits_gained += ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best ) -
2758 45133 : ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx] - 2, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2759 : }
2760 18352 : else if ( dist_azimuth_indexes_best[idx] == 1 )
2761 : {
2762 7027 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx] - 1, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2763 7027 : bits_gained += ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best ) -
2764 7027 : ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx] - 1, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2765 : }
2766 : else
2767 : {
2768 11325 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2769 : }
2770 : }
2771 : else
2772 : {
2773 5062286 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2774 : }
2775 : }
2776 : }
2777 : }
2778 :
2779 525550 : direction_bits_ec -= bits_gained;
2780 : }
2781 : else
2782 : {
2783 313303 : direction_bits_ec = -1;
2784 : }
2785 :
2786 838853 : return direction_bits_ec;
2787 : }
2788 :
2789 :
2790 : /*-------------------------------------------------------------------------
2791 : * ivas_qmetadata_raw_encode_dir()
2792 : *
2793 : * Main function for raw coding of the directions (writing and bit estimation)
2794 : *------------------------------------------------------------------------*/
2795 :
2796 1152156 : static int16_t ivas_qmetadata_raw_encode_dir(
2797 : BSTR_ENC_HANDLE hMetaData,
2798 : IVAS_QDIRECTION *q_direction,
2799 : const int16_t nbands,
2800 : const int16_t start_band )
2801 : {
2802 : int16_t i, j;
2803 : int16_t direction_bits_raw;
2804 1152156 : int16_t start_bits = 0; /*To avoid compiler warning*/
2805 :
2806 1152156 : direction_bits_raw = 0;
2807 1152156 : if ( hMetaData != NULL )
2808 : {
2809 313303 : start_bits = hMetaData->nb_bits_tot;
2810 : }
2811 :
2812 1152156 : if ( q_direction->not_in_2D > 0 )
2813 : {
2814 5103463 : for ( i = start_band; i < nbands; i++ )
2815 : {
2816 4182656 : if ( hMetaData != NULL )
2817 : {
2818 3704360 : for ( j = 0; j < q_direction->cfg.nblocks; j++ )
2819 : {
2820 2703409 : push_next_indice( hMetaData, q_direction->band_data[i].spherical_index[j], q_direction->band_data[i].bits_sph_idx[j] );
2821 : }
2822 : }
2823 : else
2824 : {
2825 3181705 : direction_bits_raw += q_direction->cfg.nblocks * q_direction->band_data[i].bits_sph_idx[0];
2826 : }
2827 : }
2828 : }
2829 : else
2830 : {
2831 1285320 : for ( i = start_band; i < nbands; i++ )
2832 : {
2833 4018552 : for ( j = 0; j < q_direction->cfg.nblocks; j++ )
2834 : {
2835 2964581 : if ( hMetaData != NULL )
2836 : {
2837 460950 : ivas_qmetadata_encode_quasi_uniform( hMetaData, q_direction->band_data[i].azimuth_index[j], q_direction->band_data[i].azimuth_m_alphabet[j] );
2838 : }
2839 : else
2840 : {
2841 2503631 : direction_bits_raw += ivas_qmetadata_encode_quasi_uniform_length(
2842 2503631 : q_direction->band_data[i].azimuth_index[j], q_direction->band_data[i].azimuth_m_alphabet[j] );
2843 : }
2844 : }
2845 : }
2846 : }
2847 :
2848 1152156 : if ( hMetaData != NULL )
2849 : {
2850 313303 : direction_bits_raw = hMetaData->nb_bits_tot - start_bits;
2851 : }
2852 :
2853 1152156 : return direction_bits_raw;
2854 : }
2855 :
2856 :
2857 : /*-------------------------------------------------------------------------
2858 : * ivas_qmetadata_get_optimal_gr_param()
2859 : *
2860 : *
2861 : *------------------------------------------------------------------------*/
2862 :
2863 3980265 : static int16_t ivas_qmetadata_get_optimal_gr_param(
2864 : uint16_t *unsigned_data,
2865 : const int16_t count,
2866 : const int16_t gr_param_count,
2867 : int16_t *opt_gr_size )
2868 : {
2869 : int16_t opt_bits, bits, idx;
2870 : int16_t opt_gr_param;
2871 : int16_t p;
2872 :
2873 3980265 : opt_bits = MAX16B;
2874 3980265 : opt_gr_param = -1;
2875 :
2876 22126485 : for ( p = 0; p < gr_param_count; p++ )
2877 : {
2878 18146220 : bits = count * ( 1 + p ); /* terminating zero bit and the lsb bits */
2879 252197795 : for ( idx = 0; idx < count; idx++ )
2880 : {
2881 234051575 : bits += unsigned_data[idx] >> p; /* leading one bits */
2882 : }
2883 :
2884 18146220 : if ( bits < opt_bits )
2885 : {
2886 6353430 : opt_gr_param = p;
2887 6353430 : opt_bits = bits;
2888 : }
2889 : }
2890 :
2891 3980265 : *opt_gr_size = opt_bits;
2892 :
2893 3980265 : return opt_gr_param;
2894 : }
2895 :
2896 :
2897 : /*-------------------------------------------------------------------------
2898 : * ivas_qmetadata_encode_extended_gr_length()
2899 : *
2900 : *
2901 : *------------------------------------------------------------------------*/
2902 :
2903 : int16_t
2904 74180880 : ivas_qmetadata_encode_extended_gr_length(
2905 : const uint16_t value,
2906 : const uint16_t alphabet_size,
2907 : const int16_t gr_param )
2908 : {
2909 : uint16_t msb_alphabet_size;
2910 : int16_t bits;
2911 : uint16_t msb, lsb;
2912 :
2913 : #ifdef DEBUGGING
2914 : assert( alphabet_size >= 1 );
2915 : assert( value < alphabet_size );
2916 : assert( ( gr_param >= 0 ) && ( gr_param <= 15 ) );
2917 : #endif
2918 :
2919 74180880 : msb_alphabet_size = ( alphabet_size + ( 1U << gr_param ) - 1 ) >> gr_param;
2920 :
2921 74180880 : if ( msb_alphabet_size <= 3 )
2922 : {
2923 : /* EncodeQuasiUniform is always equal or better than Limited GR with up to 3 msb values */
2924 10398064 : bits = ivas_qmetadata_encode_quasi_uniform_length( value, alphabet_size );
2925 : }
2926 : else
2927 : {
2928 63782816 : msb = value >> gr_param;
2929 :
2930 63782816 : bits = msb; /* leading one bits */
2931 63782816 : if ( msb < msb_alphabet_size - 1 )
2932 : {
2933 62987149 : bits += 1 + gr_param; /* terminating zero bit, if not the largest msb (Limited GR), and the lsb bits */
2934 : }
2935 : else
2936 : {
2937 795667 : lsb = value & ( ( 1U << gr_param ) - 1 );
2938 795667 : bits += ivas_qmetadata_encode_quasi_uniform_length( lsb, alphabet_size - ( ( msb_alphabet_size - 1 ) << gr_param ) );
2939 : }
2940 : }
2941 :
2942 74180880 : return bits;
2943 : }
2944 :
2945 :
2946 : /*-------------------------------------------------------------------------
2947 : * ivas_qmetadata_reorder_elevation_index()
2948 : *
2949 : *
2950 : *------------------------------------------------------------------------*/
2951 :
2952 33448185 : static int16_t ivas_qmetadata_reorder_elevation_index(
2953 : const int16_t elevation_index,
2954 : const int16_t avg_elevation_index,
2955 : const int16_t elevation_alphabet )
2956 : {
2957 : int16_t elevation_alphabet_half;
2958 : int16_t elevation_index_reordered;
2959 : #ifdef DEBUGGING
2960 : assert( ( elevation_alphabet & 0x01 ) == 1 ); /* elevation_alphabet has the form 2 * n_points + 1 */
2961 : #endif
2962 :
2963 33448185 : elevation_alphabet_half = elevation_alphabet >> 1;
2964 33448185 : elevation_index_reordered = elevation_index - avg_elevation_index;
2965 :
2966 : /* reduce the distance for the index elevation to the range [-elevation_alphabet_half, elevation_alphabet_half] */
2967 33448185 : if ( elevation_index_reordered < -elevation_alphabet_half )
2968 : {
2969 295807 : elevation_index_reordered += elevation_alphabet;
2970 : }
2971 33152378 : else if ( elevation_index_reordered > elevation_alphabet_half )
2972 : {
2973 344833 : elevation_index_reordered -= elevation_alphabet;
2974 : }
2975 :
2976 : /* fold reduced signed distance value for converting to unsigned */
2977 33448185 : elevation_index_reordered = ivas_qmetadata_reorder_generic( elevation_index_reordered );
2978 : #ifdef DEBUGGING
2979 : assert( ( 0 <= elevation_index_reordered ) && ( elevation_index_reordered < elevation_alphabet ) );
2980 : #endif
2981 :
2982 33448185 : return elevation_index_reordered;
2983 : }
2984 :
2985 :
2986 : /*-------------------------------------------------------------------------
2987 : * ivas_qmetadata_reorder_azimuth_index()
2988 : *
2989 : *
2990 : *------------------------------------------------------------------------*/
2991 :
2992 29262045 : static int16_t ivas_qmetadata_reorder_azimuth_index(
2993 : const int16_t azimuth_index,
2994 : const int16_t avg_azimuth_index,
2995 : const int16_t azimuth_alphabet )
2996 : {
2997 : int16_t azimuth_alphabet_half;
2998 : int16_t azimuth_index_reordered;
2999 :
3000 29262045 : azimuth_index_reordered = azimuth_index - avg_azimuth_index;
3001 :
3002 29262045 : if ( ( azimuth_alphabet != 1 ) && ( ( azimuth_alphabet & 0x01 ) == 1 ) )
3003 : {
3004 10223886 : return ( ivas_qmetadata_reorder_elevation_index( azimuth_index, avg_azimuth_index, azimuth_alphabet ) );
3005 : }
3006 19038159 : else if ( azimuth_alphabet != 1 )
3007 : {
3008 19029351 : azimuth_alphabet_half = azimuth_alphabet >> 1;
3009 : /* reduce the distance for the index azimuth to the range [-azimuth_alphabet_half, azimuth_alphabet_half - 1] */
3010 19029351 : if ( azimuth_index_reordered < -azimuth_alphabet_half )
3011 : {
3012 245800 : azimuth_index_reordered += azimuth_alphabet;
3013 : }
3014 18783551 : else if ( azimuth_index_reordered > azimuth_alphabet_half - 1 )
3015 : {
3016 924496 : azimuth_index_reordered -= azimuth_alphabet;
3017 : }
3018 : /* fold reduced signed distance value for converting to unsigned */
3019 19029351 : azimuth_index_reordered = ivas_qmetadata_reorder_generic( azimuth_index_reordered );
3020 : #ifdef DEBUGGING
3021 : assert( ( 0 <= azimuth_index_reordered ) && ( azimuth_index_reordered < azimuth_alphabet ) );
3022 : #endif
3023 : }
3024 : else
3025 : {
3026 : /* for North and South poles, a single azimuth direction exists */
3027 : #ifdef DEBUGGING
3028 : assert( ( azimuth_index == 0 ) || ( azimuth_index == MASA_NO_INDEX ) );
3029 : #endif
3030 8808 : azimuth_index_reordered = 0;
3031 :
3032 : #ifdef DEBUGGING
3033 : assert( avg_azimuth_index == 0 );
3034 : #endif
3035 : }
3036 :
3037 19038159 : return azimuth_index_reordered;
3038 : }
3039 :
3040 :
3041 : /*-------------------------------------------------------------------------
3042 : * ivas_qmetadata_encode_extended_gr()
3043 : *
3044 : *
3045 : *------------------------------------------------------------------------*/
3046 :
3047 17346337 : void ivas_qmetadata_encode_extended_gr(
3048 : BSTR_ENC_HANDLE hMetaData,
3049 : const uint16_t value,
3050 : const uint16_t alphabet_size,
3051 : const int16_t gr_param )
3052 : {
3053 : uint16_t msb_alphabet_size;
3054 : uint16_t msb, lsb, cnt;
3055 :
3056 : #ifdef DEBUGGING
3057 : assert( alphabet_size >= 1 );
3058 : assert( value < alphabet_size );
3059 : assert( ( gr_param >= 0 ) && ( gr_param <= 31 ) );
3060 : #endif
3061 :
3062 17346337 : msb_alphabet_size = ( alphabet_size + ( 1U << gr_param ) - 1 ) >> gr_param;
3063 :
3064 17346337 : if ( msb_alphabet_size <= 3 )
3065 : {
3066 : /* EncodeQuasiUniform is always equal or better than Limited GR with up to 3 msb values */
3067 1164893 : ivas_qmetadata_encode_quasi_uniform( hMetaData, value, alphabet_size );
3068 : }
3069 : else
3070 : {
3071 16181444 : msb = value >> gr_param;
3072 16181444 : lsb = value & ( ( 1U << gr_param ) - 1 );
3073 :
3074 28590290 : for ( cnt = 0; cnt < msb; cnt++ )
3075 : {
3076 : /* leading one bits */
3077 12408846 : push_next_indice( hMetaData, 1, 1 );
3078 : }
3079 :
3080 16181444 : if ( msb < msb_alphabet_size - 1 )
3081 : {
3082 16102284 : push_next_indice( hMetaData, 0, 1 ); /* terminating zero bit, if not the largest msb (Limited GR) */
3083 16102284 : if ( gr_param > 0 )
3084 : {
3085 5305219 : push_next_indice( hMetaData, lsb, gr_param );
3086 : }
3087 : }
3088 : else
3089 : {
3090 79160 : ivas_qmetadata_encode_quasi_uniform( hMetaData, lsb, alphabet_size - ( ( msb_alphabet_size - 1 ) << gr_param ) );
3091 : }
3092 : }
3093 :
3094 17346337 : return;
3095 : }
3096 :
3097 :
3098 : /*-----------------------------------------------------------------------*
3099 : * Local functions (EC3, requantize directions)
3100 : *-----------------------------------------------------------------------*/
3101 :
3102 20880 : static int16_t truncGR0(
3103 : float *data,
3104 : float *data_hat,
3105 : uint16_t *data_idx,
3106 : const int16_t len,
3107 : const int16_t bits_allowed,
3108 : float *st,
3109 : float *ct )
3110 : {
3111 : int16_t i;
3112 : int16_t bits;
3113 20880 : const int16_t remap3b[8] = { 1, 6, 2, 4, 0, 5, 3, 7 };
3114 20880 : const int16_t remap2b[4] = { 1, 2, 0, 3 };
3115 : float diff[MAX_PARAM_SPATIAL_SUBFRAMES];
3116 : int16_t indx[MAX_PARAM_SPATIAL_SUBFRAMES];
3117 :
3118 20880 : bits = 0;
3119 20880 : set_f( data_hat, 0.0f, len );
3120 20880 : set_f( diff, 10000.0f, len );
3121 :
3122 20880 : if ( bits_allowed <= len + 1 )
3123 : {
3124 1639 : bits = min( bits_allowed, len );
3125 1639 : set_f( data_hat, 0.0f, len );
3126 : /*set_s(data_idx, 0, len); */
3127 7666 : for ( i = 0; i < bits; i++ )
3128 : {
3129 6027 : if ( fabsf( data[i] ) <= 90 )
3130 : {
3131 5419 : data_idx[i] = 0;
3132 5419 : data_hat[i] = 0.0f;
3133 : }
3134 : else
3135 : {
3136 608 : data_idx[i] = 1;
3137 608 : data_hat[i] = -180.0f;
3138 : }
3139 : }
3140 :
3141 1639 : return bits;
3142 : }
3143 :
3144 95854 : for ( i = 0; i < len; i++ )
3145 : {
3146 : #ifdef DEBUGGING
3147 : assert( data_idx[i] < MASA_NO_INDEX );
3148 : #endif
3149 76613 : data_idx[i] = quantize_phi( data[i] + 180, 0, &data_hat[i], 8 );
3150 76613 : data_hat[i] -= 180;
3151 76613 : data_idx[i] = remap3b[data_idx[i]];
3152 76613 : bits += ivas_qmetadata_encode_extended_gr_length( data_idx[i], 8, 0 );
3153 76613 : 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]);*/
3154 : }
3155 :
3156 19241 : i = 0;
3157 19241 : if ( bits > bits_allowed )
3158 : {
3159 16539 : sort_desc_ind( diff, len, indx );
3160 68975 : for ( i = len - 1; i >= 0; i-- )
3161 : {
3162 59155 : if ( data_idx[indx[i]] > 3 )
3163 : {
3164 31839 : bits -= ivas_qmetadata_encode_extended_gr_length( data_idx[indx[i]], 8, 0 );
3165 31839 : data_idx[indx[i]] = quantize_phi( data[indx[i]] + 180, 0, &data_hat[indx[i]], 4 );
3166 31839 : data_hat[indx[i]] -= 180;
3167 31839 : data_idx[indx[i]] = remap2b[data_idx[indx[i]]];
3168 31839 : bits += ivas_qmetadata_encode_extended_gr_length( data_idx[indx[i]], 8, 0 );
3169 31839 : diff[indx[i]] = -st[i] - ct[i] * cosf( PI_OVER_180 * ( data[indx[i]] - data_hat[indx[i]] ) );
3170 : }
3171 59155 : if ( bits <= bits_allowed )
3172 : {
3173 6719 : break;
3174 : }
3175 : }
3176 : }
3177 :
3178 19241 : if ( bits > bits_allowed )
3179 : {
3180 9820 : sort_desc_ind( diff, len, indx );
3181 22882 : for ( i = len - 1; i >= 0; i-- )
3182 : {
3183 :
3184 22718 : if ( data_idx[indx[i]] > 1 )
3185 : {
3186 20196 : bits -= ivas_qmetadata_encode_extended_gr_length( data_idx[indx[i]], 8, 0 );
3187 20196 : if ( fabsf( data[indx[i]] ) <= 90 )
3188 : {
3189 13835 : data_idx[indx[i]] = 0;
3190 13835 : data_hat[indx[i]] = 0;
3191 : }
3192 : else
3193 : {
3194 6361 : data_idx[indx[i]] = 1;
3195 6361 : data_hat[indx[i]] = -180;
3196 : }
3197 :
3198 20196 : bits += ivas_qmetadata_encode_extended_gr_length( data_idx[indx[i]], 8, 0 );
3199 20196 : diff[indx[i]] = -st[i] - ct[i] * cosf( PI_OVER_180 * ( data[indx[i]] - data_hat[indx[i]] ) );
3200 : }
3201 :
3202 22718 : if ( bits <= bits_allowed )
3203 : {
3204 9656 : break;
3205 : }
3206 : }
3207 : }
3208 :
3209 19241 : if ( bits > bits_allowed )
3210 : {
3211 :
3212 : #ifdef DEBUGGING
3213 : assert( bits_allowed > len );
3214 : for ( i = 0; i < len; i++ )
3215 : {
3216 : assert( data_idx[i] <= 1 );
3217 : }
3218 : #endif
3219 :
3220 164 : sort_desc_ind( diff, len, indx );
3221 199 : for ( i = len - 1; i >= 0; i-- )
3222 : {
3223 :
3224 199 : if ( data_idx[indx[i]] > 0 )
3225 : {
3226 165 : bits -= data_idx[indx[i]];
3227 165 : data_idx[indx[i]] = 0;
3228 165 : data_hat[indx[i]] = 0;
3229 : }
3230 199 : if ( bits <= bits_allowed )
3231 : {
3232 164 : break;
3233 : }
3234 : }
3235 : }
3236 :
3237 19241 : return bits;
3238 : }
3239 :
3240 :
3241 : /*-------------------------------------------------------------------*
3242 : * truncGR0_chan()
3243 : *
3244 : *
3245 : *-------------------------------------------------------------------*/
3246 :
3247 1347 : static int16_t truncGR0_chan(
3248 : const float *data,
3249 : float *data_hat,
3250 : uint16_t *data_idx,
3251 : const int16_t len,
3252 : const int16_t bits_allowed,
3253 : float *st,
3254 : float *ct )
3255 : {
3256 : int16_t i, idx_crt;
3257 : int16_t bits;
3258 : float diff[MAX_PARAM_SPATIAL_SUBFRAMES], sort_diff[MAX_PARAM_SPATIAL_SUBFRAMES], min_diff, sum_diff;
3259 : int16_t indx[MAX_PARAM_SPATIAL_SUBFRAMES];
3260 :
3261 1347 : bits = 0;
3262 1347 : set_f( data_hat, 0.0f, len );
3263 1347 : set_f( diff, 10000.0f, len );
3264 :
3265 1347 : if ( bits_allowed <= len + 1 )
3266 : {
3267 4 : bits = min( bits_allowed, len );
3268 4 : set_f( data_hat, 0.0f, len );
3269 : /*set_s(data_idx, 0, len); */
3270 20 : for ( i = 0; i < bits; i++ )
3271 : {
3272 16 : if ( fabsf( data[i] ) <= 90 )
3273 : {
3274 13 : data_idx[i] = 0;
3275 13 : data_hat[i] = 0.0f;
3276 : }
3277 : else
3278 : {
3279 3 : data_idx[i] = 1;
3280 3 : data_hat[i] = -180.0f;
3281 : }
3282 : }
3283 4 : return bits;
3284 : }
3285 :
3286 6640 : for ( i = 0; i < len; i++ )
3287 : {
3288 : #ifdef DEBUGGING
3289 : assert( data_idx[i] < MASA_NO_INDEX );
3290 : #endif
3291 5297 : data_idx[i] = quantize_phi_chan_lbr( data[i], &data_hat[i], 9 );
3292 :
3293 5297 : bits += ivas_qmetadata_encode_extended_gr_length( data_idx[i], 9, 0 );
3294 5297 : diff[i] = -st[i] - ct[i] * cosf( ( data[i] - data_hat[i] ) * PI_OVER_180 );
3295 : }
3296 :
3297 1756 : while ( bits > bits_allowed )
3298 : {
3299 413 : min_diff = 1000.0f;
3300 413 : idx_crt = -1;
3301 413 : mvr2r( diff, sort_diff, len );
3302 2059 : for ( i = 0; i < len; i++ )
3303 : {
3304 1646 : if ( data_idx[i] > 0 )
3305 : {
3306 1306 : sort_diff[i] = -st[i] - ct[i] * cosf( ( fabsf( data[i] ) - cb_azi_chan[( ( data_idx[i] + 1 ) >> 1 ) - 1] ) * PI_OVER_180 );
3307 1306 : sum_diff = sum_f( sort_diff, len );
3308 :
3309 1306 : if ( sum_diff < min_diff )
3310 : {
3311 724 : min_diff = sum_diff;
3312 724 : idx_crt = i;
3313 : }
3314 1306 : sort_diff[i] = diff[i];
3315 : }
3316 : }
3317 :
3318 413 : if ( idx_crt > -1 )
3319 : {
3320 413 : bits -= ivas_qmetadata_encode_extended_gr_length( data_idx[idx_crt], 9, 0 );
3321 413 : data_idx[idx_crt] = quantize_phi_chan_lbr( data[idx_crt], &data_hat[idx_crt], data_idx[idx_crt] + 1 );
3322 413 : bits += ivas_qmetadata_encode_extended_gr_length( data_idx[idx_crt], 9, 0 );
3323 413 : diff[idx_crt] = -st[idx_crt] - ct[idx_crt] * cosf( ( data[idx_crt] - data_hat[idx_crt] ) * PI_OVER_180 );
3324 : }
3325 : else
3326 : {
3327 0 : break;
3328 : }
3329 : }
3330 :
3331 1343 : if ( bits > bits_allowed )
3332 : {
3333 0 : mvr2r( diff, sort_diff, len );
3334 0 : sort_desc_ind( sort_diff, len, indx );
3335 :
3336 0 : for ( i = len - 1; i >= 0; i-- )
3337 : {
3338 0 : idx_crt = indx[i];
3339 0 : if ( data_idx[idx_crt] > 0 )
3340 : {
3341 0 : bits -= ivas_qmetadata_encode_extended_gr_length( data_idx[idx_crt], 9, 0 );
3342 0 : data_idx[idx_crt] = 0;
3343 0 : data_hat[idx_crt] = 0;
3344 0 : bits += 1;
3345 : }
3346 :
3347 0 : if ( bits <= bits_allowed )
3348 : {
3349 0 : break;
3350 : }
3351 : }
3352 : }
3353 :
3354 1343 : return bits;
3355 : }
3356 :
3357 :
3358 : /*-------------------------------------------------------------------*
3359 : * common_direction()
3360 : *
3361 : *
3362 : *-------------------------------------------------------------------*/
3363 :
3364 19768 : static int16_t common_direction(
3365 : IVAS_QDIRECTION *q_direction,
3366 : const int16_t band_idx,
3367 : const int16_t len,
3368 : const int16_t bits_allowed,
3369 : BSTR_ENC_HANDLE hMetaData,
3370 : float *elevation_orig,
3371 : float *azimuth_orig )
3372 : {
3373 : int16_t nbits;
3374 : int16_t no_th, i, id_th, k;
3375 : float theta_cb[5];
3376 : float dist, best_dist;
3377 : float ct[MAX_PARAM_SPATIAL_SUBFRAMES], st[MAX_PARAM_SPATIAL_SUBFRAMES];
3378 :
3379 19768 : nbits = 0;
3380 :
3381 19768 : if ( bits_allowed == 0 )
3382 : {
3383 0 : for ( i = 0; i < len; i++ )
3384 : {
3385 0 : q_direction->band_data[band_idx].elevation[i] = 0;
3386 0 : q_direction->band_data[band_idx].azimuth[i] = 0;
3387 : }
3388 :
3389 0 : return 0;
3390 : }
3391 :
3392 19768 : if ( bits_allowed <= len + 1 )
3393 : {
3394 923 : set_f( q_direction->band_data[band_idx].elevation, 0.0f, len );
3395 923 : set_f( st, 0.0f, len );
3396 :
3397 4615 : for ( i = 0; i < len; i++ )
3398 : {
3399 3692 : ct[i] = cosf( elevation_orig[i] * PI_OVER_180 );
3400 : }
3401 :
3402 923 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3403 : {
3404 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 );
3405 : }
3406 : else
3407 : {
3408 923 : nbits = truncGR0( azimuth_orig, q_direction->band_data[band_idx].azimuth, q_direction->band_data[band_idx].azimuth_index, len, bits_allowed, st, ct );
3409 : }
3410 :
3411 4615 : for ( i = 0; i < nbits; i++ )
3412 : {
3413 3692 : push_next_indice( hMetaData, q_direction->band_data[band_idx].azimuth_index[i], 1 );
3414 : }
3415 :
3416 923 : return nbits;
3417 : }
3418 :
3419 18845 : no_th = no_theta_masa[0] + 3; /* only 5 values for theta; the lat 2 are +/-90 */
3420 :
3421 18845 : theta_cb[0] = 0;
3422 18845 : theta_cb[1] = delta_theta_masa[2];
3423 18845 : theta_cb[2] = -theta_cb[1];
3424 18845 : theta_cb[3] = 90.0f;
3425 18845 : theta_cb[4] = -90.0f;
3426 18845 : best_dist = 900000.0f;
3427 18845 : id_th = 0;
3428 :
3429 113070 : for ( i = 0; i < no_th; i++ )
3430 : {
3431 94225 : dist = 0.0f;
3432 471125 : for ( k = 0; k < len; k++ )
3433 : {
3434 376900 : dist += ( elevation_orig[k] - theta_cb[i] ) * ( elevation_orig[k] - theta_cb[i] );
3435 : }
3436 94225 : if ( dist < best_dist )
3437 : {
3438 22825 : id_th = i;
3439 22825 : best_dist = dist;
3440 : }
3441 : }
3442 :
3443 18845 : set_f( q_direction->band_data[band_idx].elevation, theta_cb[id_th], len );
3444 :
3445 94225 : for ( i = 0; i < len; i++ )
3446 : {
3447 75380 : q_direction->band_data[band_idx].elevation_index[i] = id_th;
3448 : }
3449 :
3450 18845 : if ( id_th == 0 )
3451 : {
3452 14890 : push_next_indice( hMetaData, 0, 1 ); /* average theta index */
3453 14890 : set_f( st, 0.0f, len );
3454 :
3455 74450 : for ( i = 0; i < len; i++ )
3456 : {
3457 59560 : ct[i] = cosf( elevation_orig[i] * PI_OVER_180 );
3458 : }
3459 :
3460 14890 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3461 : {
3462 408 : 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;
3463 : }
3464 : else
3465 : {
3466 14482 : 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;
3467 : }
3468 : }
3469 : else
3470 : {
3471 3955 : if ( id_th >= 3 )
3472 : {
3473 : /* theta is 90 or -90; only theta is sent */
3474 25 : push_next_indice( hMetaData, id_th + 11, 4 ); /* average theta index */
3475 25 : set_f( q_direction->band_data[band_idx].azimuth, 0.0f, len );
3476 125 : for ( i = 0; i < len; i++ )
3477 : {
3478 100 : q_direction->band_data[band_idx].azimuth_index[i] = 0;
3479 : }
3480 25 : nbits = 4;
3481 :
3482 25 : return nbits;
3483 : }
3484 :
3485 3930 : set_f( st, sinf( theta_cb[id_th] * PI_OVER_180 ), len );
3486 3930 : set_f( ct, cosf( theta_cb[id_th] * PI_OVER_180 ), len );
3487 :
3488 19650 : for ( i = 0; i < len; i++ )
3489 : {
3490 15720 : st[i] *= sinf( elevation_orig[i] * PI_OVER_180 );
3491 15720 : ct[i] *= cosf( elevation_orig[i] * PI_OVER_180 );
3492 15720 : q_direction->band_data[band_idx].azimuth_index[i] = 0;
3493 : }
3494 :
3495 3930 : if ( id_th == 1 )
3496 : {
3497 1573 : push_next_indice( hMetaData, 2, 2 ); /* average theta index */
3498 : }
3499 : else
3500 : {
3501 2357 : assert( id_th == 2 );
3502 2357 : push_next_indice( hMetaData, 6, 3 ); /* average theta index */
3503 : }
3504 :
3505 3930 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3506 : {
3507 914 : 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 );
3508 : }
3509 : else
3510 : {
3511 3016 : 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 );
3512 : }
3513 : }
3514 :
3515 18820 : if ( bits_allowed - ( id_th + 1 ) <= len + 1 )
3516 : {
3517 :
3518 1716 : for ( i = 0; i < min( len, bits_allowed - ( id_th + 1 ) ); i++ )
3519 : {
3520 1366 : push_next_indice( hMetaData, q_direction->band_data[band_idx].azimuth_index[i], 1 );
3521 : }
3522 : }
3523 : else
3524 : {
3525 18470 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3526 : {
3527 6590 : for ( i = 0; i < len; i++ )
3528 : {
3529 5272 : ivas_qmetadata_encode_extended_gr( hMetaData, q_direction->band_data[band_idx].azimuth_index[i], 9, 0 );
3530 : }
3531 : }
3532 : else
3533 : {
3534 85760 : for ( i = 0; i < len; i++ )
3535 : {
3536 68608 : ivas_qmetadata_encode_extended_gr( hMetaData, q_direction->band_data[band_idx].azimuth_index[i], 8, 0 );
3537 : }
3538 : }
3539 : }
3540 :
3541 18820 : return nbits;
3542 : }
3543 :
3544 :
3545 : /*-------------------------------------------------------------------*
3546 : * encode_directions_subband()
3547 : *
3548 : *
3549 : *-------------------------------------------------------------------*/
3550 :
3551 240654 : static int16_t encode_directions_subband(
3552 : IVAS_QDIRECTION *q_direction,
3553 : int16_t coding_subbands,
3554 : BSTR_ENC_HANDLE hMetaData,
3555 : const int16_t j,
3556 : const int16_t next_j,
3557 : const int16_t no_subframes,
3558 : const int16_t last_subband,
3559 : int16_t *p_diff,
3560 : float *elevation_orig,
3561 : float *azimuth_orig )
3562 : {
3563 : int16_t allowed_bits, use_vq, max_nb_idx, k;
3564 : int16_t diff;
3565 : float d1, d2;
3566 : int16_t nbits;
3567 : int16_t *bits_dir0;
3568 :
3569 240654 : nbits = 0;
3570 240654 : diff = *p_diff;
3571 240654 : bits_dir0 = (int16_t *) q_direction->band_data[j].bits_sph_idx;
3572 240654 : allowed_bits = sum_s( bits_dir0, no_subframes );
3573 :
3574 240654 : if ( allowed_bits > 0 )
3575 : {
3576 240654 : use_vq = 0;
3577 240654 : max_nb_idx = 0;
3578 :
3579 895806 : for ( k = 0; k < no_subframes; k++ )
3580 : {
3581 655152 : if ( bits_dir0[k] > use_vq )
3582 : {
3583 276701 : use_vq = bits_dir0[k];
3584 276701 : max_nb_idx = k;
3585 : }
3586 : }
3587 :
3588 240654 : if ( no_subframes > 1 )
3589 : {
3590 138166 : if ( ( use_vq > 1 ) && ( use_vq <= LIMIT_USE_COMMON ) )
3591 : {
3592 19387 : bits_dir0[max_nb_idx] -= 1;
3593 19387 : allowed_bits -= 1;
3594 : }
3595 : #ifdef DEBUGGING
3596 : assert( bits_dir0[max_nb_idx] > 0 );
3597 : #endif
3598 : }
3599 240654 : if ( no_subframes > 1 )
3600 : {
3601 138166 : if ( use_vq <= LIMIT_USE_COMMON )
3602 : {
3603 : /* calculate the two distances */
3604 19983 : calculate_two_distances( q_direction->band_data[j].elevation, bits_dir0, allowed_bits, no_subframes, &d1, &d2 );
3605 19983 : if ( ( ( use_vq > 1 ) && ( d2 <= d1 ) ) || ( use_vq <= 1 ) )
3606 : {
3607 19768 : if ( use_vq > 1 )
3608 : {
3609 19172 : push_next_indice( hMetaData, 1, 1 ); /* signal VQ */
3610 : }
3611 :
3612 19768 : diff += common_direction( q_direction, j, no_subframes, allowed_bits, hMetaData, elevation_orig, azimuth_orig ) - allowed_bits;
3613 :
3614 19768 : if ( last_subband == 0 )
3615 : {
3616 13770 : update_bits_next_block( q_direction, &diff, next_j, coding_subbands, no_subframes );
3617 : }
3618 : }
3619 : else
3620 : {
3621 215 : push_next_indice( hMetaData, 0, 1 );
3622 :
3623 215 : if ( last_subband == 0 )
3624 : {
3625 129 : mvr2r( elevation_orig, q_direction->band_data[j].elevation, no_subframes );
3626 129 : mvr2r( azimuth_orig, q_direction->band_data[j].azimuth, no_subframes );
3627 129 : joint_encoding( q_direction, j, next_j, coding_subbands, bits_dir0, allowed_bits, hMetaData, &diff );
3628 : }
3629 : else
3630 : {
3631 430 : for ( k = 0; k < no_subframes; k++ )
3632 : {
3633 : /* requantize the direction */
3634 344 : 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],
3635 344 : &q_direction->band_data[j].elevation_index[k], &q_direction->band_data[j].azimuth_index[k], q_direction->cfg.mc_ls_setup );
3636 : }
3637 :
3638 86 : if ( allowed_bits > 0 )
3639 : {
3640 86 : nbits = write_fixed_rate_direction( hMetaData, q_direction, j, no_subframes );
3641 : }
3642 : }
3643 : }
3644 : }
3645 : else
3646 : {
3647 : /* there is only joint coding */
3648 118183 : mvr2r( elevation_orig, q_direction->band_data[j].elevation, no_subframes );
3649 118183 : mvr2r( azimuth_orig, q_direction->band_data[j].azimuth, no_subframes );
3650 :
3651 118183 : if ( last_subband == 0 )
3652 : {
3653 78529 : joint_encoding( q_direction, j, next_j, coding_subbands, bits_dir0, allowed_bits, hMetaData, &diff );
3654 : }
3655 : else
3656 : {
3657 198270 : for ( k = 0; k < no_subframes; k++ )
3658 : {
3659 : /* requantize the direction */
3660 158616 : 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],
3661 158616 : &q_direction->band_data[j].elevation_index[k], &q_direction->band_data[j].azimuth_index[k], q_direction->cfg.mc_ls_setup );
3662 : }
3663 :
3664 39654 : if ( allowed_bits > 0 )
3665 : {
3666 39654 : nbits = write_fixed_rate_direction( hMetaData, q_direction, j, no_subframes );
3667 : }
3668 : }
3669 : }
3670 : }
3671 : else
3672 : {
3673 : /* 1 subframe case */
3674 : /* there is only joint coding */
3675 102488 : mvr2r( elevation_orig, q_direction->band_data[j].elevation, no_subframes );
3676 102488 : mvr2r( azimuth_orig, q_direction->band_data[j].azimuth, no_subframes );
3677 :
3678 102488 : if ( last_subband == 0 )
3679 : {
3680 68330 : joint_encoding( q_direction, j, next_j, coding_subbands, bits_dir0, allowed_bits, hMetaData, &diff );
3681 : }
3682 : else
3683 : {
3684 68316 : for ( k = 0; k < no_subframes; k++ )
3685 : {
3686 : /* requantize the direction */
3687 34158 : 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],
3688 34158 : &q_direction->band_data[j].elevation_index[k], &q_direction->band_data[j].azimuth_index[k], q_direction->cfg.mc_ls_setup );
3689 : }
3690 :
3691 34158 : if ( allowed_bits > 0 )
3692 : {
3693 34158 : nbits = write_fixed_rate_direction( hMetaData, q_direction, j, no_subframes );
3694 : }
3695 : }
3696 : }
3697 : }
3698 : else
3699 : {
3700 0 : set_f( q_direction->band_data[j].elevation, 0.0f, no_subframes );
3701 0 : set_f( q_direction->band_data[j].azimuth, 0.0f, no_subframes );
3702 : }
3703 :
3704 240654 : *p_diff = diff;
3705 :
3706 240654 : return nbits;
3707 : }
3708 :
3709 :
3710 : /*-------------------------------------------------------------------*
3711 : * calc_var_azi()
3712 : *
3713 : *
3714 : *-------------------------------------------------------------------*/
3715 :
3716 6115 : static int16_t calc_var_azi(
3717 : const IVAS_QDIRECTION *q_direction,
3718 : const int16_t diffuseness_index_max_ec_frame,
3719 : const float avg_azimuth,
3720 : float *avg_azimuth_out )
3721 : {
3722 : float var_band, dif;
3723 : float avg_direction_vector_band[3], avg_azimuth_band[24], direction_vector[3];
3724 : float avg_elevation;
3725 : int16_t i, j, idx;
3726 :
3727 6115 : idx = 0;
3728 6115 : set_zero( avg_azimuth_band, 24 );
3729 :
3730 36690 : for ( i = 0; i < q_direction->cfg.nbands; i++ )
3731 : {
3732 30575 : set_zero( avg_direction_vector_band, 3 );
3733 30575 : if ( q_direction->band_data[i].energy_ratio_index_mod[0] <= diffuseness_index_max_ec_frame )
3734 : {
3735 111090 : for ( j = 0; j < q_direction->cfg.nblocks; j++ )
3736 : {
3737 : /*compute the average direction */
3738 88872 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[i].azimuth[j], q_direction->band_data[i].elevation[j], direction_vector );
3739 88872 : v_add( avg_direction_vector_band, direction_vector, avg_direction_vector_band, 3 );
3740 : }
3741 22218 : ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector_band, &avg_azimuth_band[idx], &avg_elevation );
3742 22218 : idx++;
3743 : }
3744 : }
3745 :
3746 6115 : var_band = 0.0f;
3747 :
3748 28333 : for ( i = 0; i < idx; i++ )
3749 : {
3750 22218 : dif = ( avg_azimuth_band[idx] - avg_azimuth );
3751 22218 : if ( dif < 0 )
3752 : {
3753 7664 : dif = -dif;
3754 : }
3755 22218 : if ( dif > 180 )
3756 : {
3757 0 : dif = 360 - dif;
3758 : }
3759 :
3760 22218 : var_band += dif * dif;
3761 : }
3762 :
3763 6115 : if ( idx > 0 )
3764 : {
3765 6115 : var_band = var_band / idx;
3766 : }
3767 :
3768 6115 : if ( var_band <= VAR_AZI_THRESH )
3769 : {
3770 1616 : *avg_azimuth_out = avg_azimuth;
3771 1616 : return 0;
3772 : }
3773 : else
3774 : {
3775 4499 : *avg_azimuth_out = avg_azimuth_band[0];
3776 4499 : return 1;
3777 : }
3778 : }
3779 :
3780 :
3781 : /*-------------------------------------------------------------------*
3782 : * requantize_direction_EC_3()
3783 : *
3784 : *
3785 : *-------------------------------------------------------------------*/
3786 :
3787 41595 : static ivas_error requantize_direction_EC_3(
3788 : int16_t *extra_bits,
3789 : IVAS_QDIRECTION *q_direction,
3790 : const int16_t coding_subbands,
3791 : BSTR_ENC_HANDLE hMetaData,
3792 : float elevation_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES],
3793 : float azimuth_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES],
3794 : int16_t *ind_order )
3795 : {
3796 : /* gradually increase the bits following the performance of the EC layer*/
3797 : int16_t j, k;
3798 : int16_t use_vq;
3799 : int16_t diff, allowed_bits, last_j;
3800 : int16_t no_subframes, start_band;
3801 : float st[MAX_PARAM_SPATIAL_SUBFRAMES], ct[MAX_PARAM_SPATIAL_SUBFRAMES];
3802 : int16_t *bits_dir0;
3803 : #ifdef DEBUGGING
3804 : int16_t nbits;
3805 :
3806 : nbits = 0;
3807 : #endif
3808 41595 : no_subframes = q_direction->cfg.nblocks;
3809 41595 : start_band = q_direction->cfg.start_band;
3810 :
3811 41595 : if ( q_direction->not_in_2D > MASA_LIMIT_2D )
3812 : {
3813 39899 : j = ind_order[coding_subbands - 1];
3814 39899 : bits_dir0 = (int16_t *) q_direction->band_data[j].bits_sph_idx;
3815 39899 : allowed_bits = sum_s( bits_dir0, no_subframes );
3816 39899 : last_j = j - ( allowed_bits == 0 );
3817 39899 : diff = 0;
3818 39899 : if ( coding_subbands == 1 )
3819 : {
3820 425 : last_j = start_band;
3821 : }
3822 200657 : for ( j = 0; j < last_j; j++ )
3823 : {
3824 160758 : k = ind_order[j];
3825 160758 : encode_directions_subband( q_direction, coding_subbands, hMetaData, k, ind_order[j + 1], no_subframes, 0, &diff, elevation_orig[k], azimuth_orig[k] );
3826 : }
3827 :
3828 : /* last subbands to be written in fixed rate */
3829 119795 : for ( j = last_j; j < coding_subbands; j++ )
3830 : {
3831 79896 : k = ind_order[j];
3832 79896 : encode_directions_subband( q_direction, coding_subbands, hMetaData, k, 0, no_subframes, 1, &diff, elevation_orig[k], azimuth_orig[k] );
3833 : }
3834 : }
3835 : else /* 2D */
3836 : {
3837 1696 : diff = 0;
3838 : #ifdef DEBUGGING
3839 : nbits = 0;
3840 : #endif
3841 10073 : for ( j = start_band; j < coding_subbands; j++ )
3842 : {
3843 8377 : bits_dir0 = (int16_t *) q_direction->band_data[j].bits_sph_idx;
3844 8377 : allowed_bits = sum_s( bits_dir0, no_subframes );
3845 8377 : use_vq = 0;
3846 :
3847 38261 : for ( k = 0; k < no_subframes; k++ )
3848 : {
3849 29884 : if ( bits_dir0[k] > use_vq )
3850 : {
3851 9714 : use_vq = bits_dir0[k];
3852 : }
3853 : }
3854 :
3855 8377 : if ( ( use_vq <= 3 ) && ( allowed_bits <= 11 ) )
3856 : {
3857 2484 : set_f( st, 0.0f, no_subframes );
3858 :
3859 11499 : for ( k = 0; k < no_subframes; k++ )
3860 : {
3861 9015 : ct[k] = cosf( elevation_orig[j][k] * PI_OVER_180 );
3862 : }
3863 :
3864 2484 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3865 : {
3866 : #ifdef DEBUGGING
3867 : 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 );
3868 : #else
3869 25 : truncGR0_chan( azimuth_orig[j], q_direction->band_data[j].azimuth, q_direction->band_data[j].azimuth_index, no_subframes, allowed_bits, st, ct );
3870 : #endif
3871 : }
3872 : else
3873 : {
3874 : #ifdef DEBUGGING
3875 : nbits += truncGR0( azimuth_orig[j], q_direction->band_data[j].azimuth, q_direction->band_data[j].azimuth_index, no_subframes, allowed_bits, st, ct );
3876 : #else
3877 2459 : truncGR0( azimuth_orig[j], q_direction->band_data[j].azimuth, q_direction->band_data[j].azimuth_index, no_subframes, allowed_bits, st, ct );
3878 : #endif
3879 : }
3880 :
3881 2484 : if ( allowed_bits <= no_subframes + 1 )
3882 : {
3883 1355 : for ( k = 0; k < min( no_subframes, allowed_bits ); k++ )
3884 : {
3885 985 : push_next_indice( hMetaData, q_direction->band_data[j].azimuth_index[k], 1 );
3886 : }
3887 : }
3888 : else
3889 : {
3890 10144 : for ( k = 0; k < no_subframes; k++ )
3891 : {
3892 8030 : 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 );
3893 : }
3894 : }
3895 : }
3896 : else
3897 : {
3898 26762 : for ( k = 0; k < no_subframes; k++ )
3899 : {
3900 : /* requantize the direction */
3901 41738 : 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],
3902 20869 : &q_direction->band_data[j].azimuth_index[k], q_direction->cfg.mc_ls_setup );
3903 20869 : q_direction->band_data[j].elevation_index[k] = 0;
3904 : }
3905 : #ifdef DEBUGGING
3906 : nbits += write_fixed_rate_direction( hMetaData, q_direction, j, no_subframes );
3907 : #else
3908 5893 : write_fixed_rate_direction( hMetaData, q_direction, j, no_subframes );
3909 : #endif
3910 : }
3911 : }
3912 : }
3913 : #ifdef DEBUGGING
3914 : if ( diff > 0 )
3915 : {
3916 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Not enough bits in requantize_direction_EC_3(); %d bits written \n", nbits );
3917 : }
3918 : #endif
3919 :
3920 41595 : *extra_bits = -diff;
3921 :
3922 41595 : return IVAS_ERR_OK;
3923 : }
3924 :
3925 :
3926 : /*-------------------------------------------------------------------*
3927 : * write_fixed_rate_direction()
3928 : *
3929 : * writing of the spherical indexes
3930 : *-------------------------------------------------------------------*/
3931 :
3932 : /*! r: number of bits written */
3933 172124 : static int16_t write_fixed_rate_direction(
3934 : BSTR_ENC_HANDLE hMetaData, /* i : MASA metadata structure */
3935 : IVAS_QDIRECTION *qdirection, /* i/o: quantized directional parameters */
3936 : const int16_t j_idx, /* i : index of subband for which the data is written */
3937 : const int16_t len /* i : number of data */
3938 : )
3939 : {
3940 : int16_t nbits, i;
3941 :
3942 172124 : nbits = 0;
3943 551338 : for ( i = 0; i < len; i++ )
3944 : {
3945 379214 : push_next_indice( hMetaData, qdirection->band_data[j_idx].spherical_index[i], qdirection->band_data[j_idx].bits_sph_idx[i] );
3946 379214 : nbits += qdirection->band_data[j_idx].bits_sph_idx[i];
3947 : }
3948 :
3949 172124 : return nbits;
3950 : }
3951 :
3952 :
3953 : /*-------------------------------------------------------------------*
3954 : * joint_encoding()
3955 : *
3956 : * joint encoding of elevation and azimuth
3957 : *-------------------------------------------------------------------*/
3958 :
3959 146988 : static void joint_encoding(
3960 : IVAS_QDIRECTION *q_direction, /* i/o: quantized directional parameters */
3961 : const int16_t j, /* i : subband index */
3962 : const int16_t next_j, /* i : next subband index */
3963 : const int16_t coding_subbands, /* i : total number of subband */
3964 : int16_t *bits_dir0, /* i/o: number of bits for each tile in each subband */
3965 : const int16_t allowed_bits, /* i : maximum number of bits available for the current subband */
3966 : BSTR_ENC_HANDLE hMetaData, /* i/o: metadata bitstream handle */
3967 : int16_t *diff /* o : bits to be given/taken to next subband */
3968 : )
3969 : {
3970 : int16_t k;
3971 : int16_t GR_ord_azimuth, use_context, GR_ord_elevation;
3972 : uint8_t method;
3973 : int16_t nbits;
3974 : int16_t same;
3975 : uint16_t data[MAX_PARAM_SPATIAL_SUBFRAMES];
3976 146988 : int16_t len_data = 0;
3977 : int16_t no_symb_ele[MAX_PARAM_SPATIAL_SUBFRAMES];
3978 :
3979 529950 : for ( k = 0; k < q_direction->cfg.nblocks; k++ )
3980 : {
3981 382962 : q_direction->band_data[j].bits_sph_idx[k] = bits_dir0[k];
3982 :
3983 : /* requantize the direction */
3984 765924 : 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],
3985 382962 : &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 );
3986 :
3987 382962 : if ( bits_dir0[k] >= 3 )
3988 : {
3989 381886 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3990 : {
3991 11216 : q_direction->band_data[j].elevation_m_alphabet[k] = no_theta_masa[bits_dir0[k] - 3];
3992 11216 : q_direction->band_data[j].azimuth_m_alphabet[k] = no_phi_masa[bits_dir0[k] - 1][q_direction->band_data[j].elevation_index[k]];
3993 : }
3994 : else
3995 : {
3996 370670 : q_direction->band_data[j].elevation_m_alphabet[k] = no_theta_masa[bits_dir0[k] - 3] * 2 - 1;
3997 370670 : 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];
3998 : }
3999 381886 : assert( q_direction->band_data[j].elevation_index[k] != MASA_NO_INDEX );
4000 : }
4001 1076 : else if ( bits_dir0[k] > 0 )
4002 : {
4003 1076 : q_direction->band_data[j].elevation_m_alphabet[k] = 1;
4004 1076 : q_direction->band_data[j].azimuth_m_alphabet[k] = no_phi_masa[bits_dir0[k] - 1][0];
4005 : }
4006 : else
4007 : {
4008 0 : q_direction->band_data[j].elevation_m_alphabet[k] = 1;
4009 0 : q_direction->band_data[j].azimuth_m_alphabet[k] = 1;
4010 : }
4011 : }
4012 :
4013 529950 : for ( k = 0; k < q_direction->cfg.nblocks; k++ )
4014 : {
4015 382962 : if ( q_direction->band_data[j].bits_sph_idx[k] <= 2 )
4016 : {
4017 1076 : q_direction->band_data[j].elevation_index[k] = MASA_NO_INDEX;
4018 : }
4019 : else
4020 : {
4021 381886 : no_symb_ele[len_data] = q_direction->band_data[j].elevation_m_alphabet[k];
4022 381886 : data[len_data++] = q_direction->band_data[j].elevation_index[k];
4023 : }
4024 : }
4025 :
4026 :
4027 : /* encode indexes for current subband and count the number of bits */
4028 :
4029 146988 : if ( q_direction->cfg.nblocks == 1 && q_direction->band_data[j].bits_sph_idx[0] <= MASA_MIN_BITS_TF + 1 )
4030 : {
4031 : /* encode with fixed rate only if only one subframe and very low number of bits */
4032 34997 : nbits = write_fixed_rate_direction( hMetaData, q_direction, j, q_direction->cfg.nblocks );
4033 : }
4034 : else
4035 : {
4036 :
4037 111991 : if ( len_data > 0 )
4038 : {
4039 111991 : nbits = GR_bits_new( data, no_symb_ele, len_data, MASA_GR_ORD_EL, 1, &GR_ord_elevation );
4040 : }
4041 : else
4042 : {
4043 0 : nbits = 0;
4044 0 : GR_ord_elevation = MASA_GR_ORD_EL;
4045 : }
4046 :
4047 111991 : same = 1;
4048 347965 : for ( k = 1; k < q_direction->cfg.nblocks; k++ )
4049 : {
4050 235974 : if ( q_direction->band_data[j].elevation_index[k] != q_direction->band_data[j].elevation_index[0] )
4051 : {
4052 50647 : same = 0;
4053 : }
4054 : }
4055 111991 : if ( same == 1 && q_direction->band_data[j].elevation_index[0] < 4 )
4056 : {
4057 78215 : nbits = 3;
4058 : }
4059 : else
4060 : {
4061 33776 : same = 0;
4062 33776 : nbits += 1;
4063 : }
4064 :
4065 223982 : nbits += GR_bits_azimuth_context( q_direction->band_data[j].azimuth_index,
4066 111991 : q_direction->band_data[j].azimuth_m_alphabet, q_direction->cfg.nblocks, MASA_GR_ORD_AZ,
4067 111991 : q_direction->band_data[j].bits_sph_idx, &GR_ord_azimuth, &use_context );
4068 :
4069 111991 : if ( allowed_bits == 0 )
4070 : {
4071 0 : nbits = 0;
4072 : }
4073 : else
4074 : {
4075 111991 : if ( nbits >= allowed_bits )
4076 : {
4077 57336 : nbits = allowed_bits + 1; /* fixed rate encoding */
4078 57336 : method = 1;
4079 57336 : push_next_indice( hMetaData, method, 1 );
4080 :
4081 : /* write current subband data */
4082 57336 : nbits = 1 + write_fixed_rate_direction( hMetaData, q_direction, j, q_direction->cfg.nblocks );
4083 : }
4084 : else
4085 : {
4086 54655 : nbits += 1; /* EC coding */
4087 54655 : method = 0;
4088 54655 : push_next_indice( hMetaData, method, 1 );
4089 :
4090 : /* write current subband data */
4091 54655 : write_ec_direction( &nbits, hMetaData, q_direction, j, q_direction->cfg.nblocks, GR_ord_elevation, GR_ord_azimuth, use_context, same );
4092 54655 : nbits++;
4093 : }
4094 : }
4095 : }
4096 146988 : *diff += nbits - allowed_bits;
4097 146988 : update_bits_next_block( q_direction, diff, next_j, coding_subbands, q_direction->cfg.nblocks );
4098 :
4099 146988 : return;
4100 : }
4101 :
4102 :
4103 : /*-------------------------------------------------------------------*
4104 : * calculate_two_distances()
4105 : *
4106 : * calculate estimated distortions if encoding with VQ or not
4107 : *-------------------------------------------------------------------*/
4108 :
4109 19983 : static void calculate_two_distances(
4110 : float *el, /* i : elevation values */
4111 : int16_t *bits, /* i : number of bits for each tile */
4112 : const int16_t total_bits, /* i : total number of bits for subband */
4113 : const int16_t len, /* i : number of tiles */
4114 : float *p_d1, /* o : first distortion */
4115 : float *p_d2 /* o : second distortion */
4116 : )
4117 : {
4118 : int16_t i;
4119 : float d1, d2, el_av;
4120 19983 : const float cos_delta_phi_cb[] = { 0.848f, 0.8988f, 0.9272f, 0.9563f, 0.9744f, 0.9816f, 0.9877f, 0.9925f };
4121 : float var_el;
4122 :
4123 19983 : d1 = 0.0f;
4124 19983 : d2 = 0.0f;
4125 :
4126 19983 : el_av = mean( el, len );
4127 19983 : if ( total_bits > 9 )
4128 : {
4129 73875 : for ( i = 0; i < len; i++ )
4130 : {
4131 59100 : if ( bits[i] > 2 )
4132 : {
4133 40942 : if ( fabsf( el[i] ) < fabsf( ( fabsf( el[i] ) - 45 ) ) )
4134 : {
4135 : /* el_hat = 0*/
4136 31154 : if ( bits[i] == 3 )
4137 : {
4138 31154 : d1 += 1 - 0.7f * cosf( el[i] * PI_OVER_180 );
4139 : }
4140 : else
4141 : {
4142 0 : d1 += 1 - 0.92f * cosf( el[i] * PI_OVER_180 );
4143 : }
4144 : }
4145 : else
4146 : {
4147 9788 : if ( bits[i] == 3 )
4148 : {
4149 9788 : d1 += 1 - sinf( el[i] * PI_OVER_180 ) * 0.7f * sign( el[i] );
4150 : }
4151 : else
4152 : {
4153 0 : d1 += 1 - 0.7f * 0.92f * cosf( el[i] * PI_OVER_180 ) - sinf( fabsf( el[i] * PI_OVER_180 ) ) * 0.7f;
4154 : }
4155 : }
4156 : }
4157 : else
4158 : {
4159 18158 : if ( bits[i] == 2 )
4160 : {
4161 18158 : d1 += 1 - cosf( el[i] * PI_OVER_180 ) * 0.7f;
4162 : }
4163 : else
4164 : {
4165 0 : d1 += 1;
4166 : }
4167 : }
4168 59100 : 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];
4169 : }
4170 : }
4171 :
4172 19983 : var_el = var( el, len );
4173 19983 : if ( var_el > 1300.0f )
4174 : {
4175 215 : d2 = d1 + 0.1f;
4176 : }
4177 :
4178 19983 : *p_d1 = d1;
4179 19983 : *p_d2 = d2;
4180 :
4181 19983 : return;
4182 : }
4183 :
4184 :
4185 : /*-------------------------------------------------------------------*
4186 : * write_ec_direction()
4187 : *
4188 : * write metadata using entropy encoding
4189 : *-------------------------------------------------------------------*/
4190 :
4191 : /*! r: number of bits written */
4192 54655 : static ivas_error write_ec_direction(
4193 : int16_t *num_bits_written, /* o : Number of bits written */
4194 : BSTR_ENC_HANDLE hMetaData, /* i : MASA metadata structure */
4195 : IVAS_QDIRECTION *qdirection, /* i : quantized directional info */
4196 : const int16_t j_idx, /* i : index of subband to encode and write */
4197 : const int16_t len, /* i : number of tiles */
4198 : const int16_t GR_ord_elevation, /* i : GR order for elevation encoding */
4199 : const int16_t GR_ord_azimuth, /* i : GR order for azimuth encoding */
4200 : const int16_t use_context, /* i : flag for context usiage in azimuth encoding */
4201 : const int16_t same /* i : flag if elevation indexes are the same or not */
4202 : )
4203 : {
4204 : int16_t i, nbits, bits_crt, nr_NO_INDEX;
4205 : uint16_t data;
4206 : int16_t min_val, max_val;
4207 :
4208 54655 : nr_NO_INDEX = 0;
4209 :
4210 54655 : nbits = 0;
4211 :
4212 : /* write elevation */
4213 272390 : for ( i = 0; i < len; i++ )
4214 : {
4215 217735 : data = qdirection->band_data[j_idx].elevation_index[i];
4216 217735 : if ( ( data == MASA_NO_INDEX ) || ( qdirection->band_data[j_idx].bits_sph_idx[i] == 0 ) )
4217 : {
4218 3 : nr_NO_INDEX += 1;
4219 : }
4220 : }
4221 :
4222 54655 : if ( nr_NO_INDEX < len )
4223 : {
4224 54655 : if ( same == 1 )
4225 : {
4226 40430 : push_next_indice( hMetaData, 1, 1 );
4227 40430 : nbits += 1;
4228 40430 : push_next_indice( hMetaData, qdirection->band_data[j_idx].elevation_index[0], 2 );
4229 40430 : nbits += 2;
4230 : }
4231 : else
4232 : {
4233 14225 : push_next_indice( hMetaData, 0, 1 );
4234 14225 : nbits += 1;
4235 :
4236 14225 : push_next_indice( hMetaData, 1 - ( GR_ord_elevation == MASA_GR_ORD_EL ), 1 );
4237 14225 : nbits += 1;
4238 :
4239 71119 : for ( i = 0; i < len; i++ )
4240 : {
4241 56894 : data = qdirection->band_data[j_idx].elevation_index[i];
4242 56894 : if ( data < MASA_NO_INDEX )
4243 : {
4244 56891 : bits_crt = hMetaData->nb_bits_tot;
4245 56891 : ivas_qmetadata_encode_extended_gr( hMetaData, data, qdirection->band_data[j_idx].elevation_m_alphabet[i], GR_ord_elevation );
4246 56891 : nbits += hMetaData->nb_bits_tot - bits_crt;
4247 : }
4248 : }
4249 : }
4250 : }
4251 :
4252 : /* write azimuth */
4253 54655 : if ( use_context < 0 )
4254 : {
4255 54655 : if ( use_context == -1 )
4256 : {
4257 : /* regular GR coding */
4258 15336 : push_next_indice( hMetaData, 0, 1 );
4259 15336 : nbits += 1;
4260 15336 : push_next_indice( hMetaData, 1 - ( GR_ord_azimuth == MASA_GR_ORD_AZ ), 1 );
4261 15336 : nbits += 1;
4262 :
4263 75801 : for ( i = 0; i < len; i++ )
4264 : {
4265 60465 : data = qdirection->band_data[j_idx].azimuth_index[i];
4266 60465 : if ( data < MASA_NO_INDEX )
4267 : {
4268 60455 : bits_crt = hMetaData->nb_bits_tot;
4269 60455 : ivas_qmetadata_encode_extended_gr( hMetaData, data, qdirection->band_data[j_idx].azimuth_m_alphabet[i], GR_ord_azimuth );
4270 60455 : nbits += hMetaData->nb_bits_tot - bits_crt;
4271 : }
4272 : }
4273 : }
4274 39319 : else if ( use_context == -2 )
4275 : {
4276 : /* min removed GR coding */
4277 39317 : push_next_indice( hMetaData, 1, 1 );
4278 39317 : nbits += 1;
4279 39317 : push_next_indice( hMetaData, 1 - ( GR_ord_azimuth == MASA_GR_ORD_AZ - 1 ), 1 );
4280 39317 : nbits += 1;
4281 :
4282 : /* find min */
4283 39317 : min_val = MASA_NO_INDEX;
4284 196585 : for ( i = 0; i < len; i++ )
4285 : {
4286 157268 : if ( qdirection->band_data[j_idx].azimuth_index[i] < min_val )
4287 : {
4288 54979 : min_val = qdirection->band_data[j_idx].azimuth_index[i];
4289 : }
4290 : }
4291 :
4292 : #ifdef DEBUGGING
4293 : if ( min_val == MASA_NO_INDEX )
4294 : {
4295 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "No azimuth data to be coded in write_ec_direction()" );
4296 : }
4297 : #endif
4298 :
4299 : /* write min*/
4300 39317 : bits_crt = hMetaData->nb_bits_tot;
4301 39317 : maximum_s( qdirection->band_data[j_idx].azimuth_m_alphabet, len, &max_val );
4302 39317 : ivas_qmetadata_encode_extended_gr( hMetaData, min_val, max_val, MASA_GR_ORD_AZ );
4303 39317 : nbits += hMetaData->nb_bits_tot - bits_crt;
4304 :
4305 196585 : for ( i = 0; i < len; i++ )
4306 : {
4307 157268 : data = qdirection->band_data[j_idx].azimuth_index[i] - min_val;
4308 157268 : if ( data < MASA_NO_INDEX - min_val )
4309 : {
4310 157266 : bits_crt = hMetaData->nb_bits_tot;
4311 157266 : ivas_qmetadata_encode_extended_gr( hMetaData, data, qdirection->band_data[j_idx].azimuth_m_alphabet[i], GR_ord_azimuth );
4312 157266 : nbits += hMetaData->nb_bits_tot - bits_crt;
4313 : }
4314 : }
4315 : }
4316 : #ifdef DEBUGGING
4317 : else
4318 : {
4319 : if ( use_context != -3 )
4320 : {
4321 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong use_context value in write_ec_direction()" );
4322 : }
4323 : }
4324 : #endif
4325 : }
4326 : else
4327 : {
4328 0 : for ( i = 0; i < len; i++ )
4329 : {
4330 0 : data = qdirection->band_data[j_idx].azimuth_index[i];
4331 0 : if ( data < MASA_NO_INDEX )
4332 : {
4333 0 : switch ( qdirection->band_data[j_idx].bits_sph_idx[i] )
4334 : {
4335 0 : case 0:
4336 0 : break;
4337 0 : case 1:
4338 0 : nbits += 1;
4339 0 : push_next_indice( hMetaData, data, 1 );
4340 0 : break;
4341 0 : case 2:
4342 0 : bits_crt = hMetaData->nb_bits_tot;
4343 0 : ivas_qmetadata_encode_extended_gr( hMetaData, data, qdirection->band_data[j_idx].azimuth_m_alphabet[i], MASA_GR_ORD_AZ - 1 );
4344 0 : nbits += hMetaData->nb_bits_tot - bits_crt;
4345 0 : break;
4346 0 : default:
4347 0 : bits_crt = hMetaData->nb_bits_tot;
4348 0 : ivas_qmetadata_encode_extended_gr( hMetaData, data, qdirection->band_data[j_idx].azimuth_m_alphabet[i], MASA_GR_ORD_AZ );
4349 0 : nbits += hMetaData->nb_bits_tot - bits_crt;
4350 0 : break;
4351 : }
4352 0 : }
4353 : }
4354 : }
4355 :
4356 54655 : *num_bits_written = nbits;
4357 54655 : return IVAS_ERR_OK;
4358 : }
4359 :
4360 :
4361 : /*-----------------------------------------------------------------------*
4362 : * Local functions (coherence Q and coding)
4363 : *-----------------------------------------------------------------------*/
4364 :
4365 : /*! r: index */
4366 180016 : static uint64_t create_combined_index(
4367 : uint16_t *idx_dct, /* i : indexes to combine */
4368 : const int16_t len, /* i : number of indexes */
4369 : const int16_t *no_cb_vec /* i : how many codewords for each position */
4370 : )
4371 : {
4372 : int16_t i;
4373 : uint64_t idx, base;
4374 :
4375 180016 : base = 1;
4376 180016 : idx = 0;
4377 1633713 : for ( i = 0; i < len; i++ )
4378 : {
4379 1453697 : idx += base * idx_dct[i];
4380 1453697 : base *= no_cb_vec[i];
4381 : }
4382 :
4383 180016 : return idx;
4384 : }
4385 :
4386 :
4387 : /*-----------------------------------------------------------------------*
4388 : * encoding DCT0 coeffs with joint index
4389 : *-----------------------------------------------------------------------*/
4390 :
4391 : /*! r: number of bits written */
4392 127046 : static int16_t encode_coherence_indexesDCT0(
4393 : uint16_t *idx_dct, /* i : indexes to be encoded */
4394 : const int16_t len, /* i : number of indexes */
4395 : int16_t *no_cb_vec, /* i : number of codewords for each position */
4396 : BSTR_ENC_HANDLE hMetaData,
4397 : const int16_t indice_coherence,
4398 : const int16_t nbits,
4399 : const int16_t nbits1 )
4400 : {
4401 : int16_t i;
4402 : uint64_t idx;
4403 : int16_t no_idx16;
4404 : int16_t k;
4405 : int16_t half_len, j;
4406 : uint64_t idx1;
4407 :
4408 : /* calculate bits for dct0 components with joint encoding */
4409 127046 : if ( nbits1 > 0 )
4410 : {
4411 1390 : half_len = len / 2;
4412 1390 : idx = create_combined_index( idx_dct, half_len, no_cb_vec );
4413 1390 : idx1 = create_combined_index( &idx_dct[half_len], half_len, &no_cb_vec[half_len] );
4414 : }
4415 : else
4416 : {
4417 125656 : idx = create_combined_index( idx_dct, len, no_cb_vec );
4418 125656 : idx1 = 0;
4419 : }
4420 :
4421 127046 : if ( nbits % 16 == 0 )
4422 : {
4423 3565 : no_idx16 = nbits / 16;
4424 : }
4425 : else
4426 : {
4427 123481 : no_idx16 = (int16_t) round_f( ( nbits / 16.0f + 0.5f ) );
4428 : }
4429 :
4430 127046 : k = nbits;
4431 127046 : i = 0;
4432 167225 : for ( i = 0; i < no_idx16 - 1; i++ )
4433 : {
4434 40179 : k -= 16;
4435 40179 : hMetaData->ind_list[indice_coherence + i].value = ( ( idx >> k ) & 65535 ); /* 16 bits */
4436 : }
4437 127046 : hMetaData->ind_list[indice_coherence + i].value = ( idx & ( ( 1 << k ) - 1 ) );
4438 :
4439 127046 : if ( nbits1 > 0 )
4440 : {
4441 1390 : if ( nbits1 % 16 == 0 )
4442 : {
4443 0 : no_idx16 = nbits1 / 16;
4444 : }
4445 : else
4446 : {
4447 1390 : no_idx16 = (int16_t) round_f( ( nbits1 / 16.0f + 0.5f ) );
4448 : }
4449 :
4450 1390 : k = nbits1;
4451 :
4452 2780 : for ( j = i + 1; j < no_idx16 + i; j++ )
4453 : {
4454 1390 : k -= 16;
4455 1390 : hMetaData->ind_list[indice_coherence + j].value = ( ( idx1 >> k ) & 65535 ); /* 16 bits */
4456 : }
4457 1390 : hMetaData->ind_list[indice_coherence + j].value = ( idx1 & ( ( 1 << k ) - 1 ) );
4458 : }
4459 :
4460 127046 : return nbits + nbits1;
4461 : }
4462 :
4463 :
4464 : /*-------------------------------------------------------------------*
4465 : * coherence_coding_length()
4466 : *
4467 : *
4468 : *-------------------------------------------------------------------*/
4469 :
4470 169243 : static int16_t coherence_coding_length(
4471 : const uint16_t *idx_sur_coh_shift,
4472 : const uint8_t idx_shift_len,
4473 : const int16_t coding_subbands,
4474 : const int16_t *no_cv,
4475 : uint16_t *mr_idx,
4476 : int16_t *no_cv_shift,
4477 : int16_t *p_min_idx,
4478 : int16_t *GR_ord,
4479 : int16_t *nbits_fr,
4480 : int16_t *nbits_fr1 )
4481 : {
4482 : int16_t half_coding_subbands;
4483 : int16_t j;
4484 : int16_t nbits;
4485 : uint64_t no_cb;
4486 : int16_t min_idx;
4487 :
4488 169243 : half_coding_subbands = 0;
4489 :
4490 169243 : if ( sum_s( no_cv, coding_subbands ) > MASA_COH_LIMIT_2IDX )
4491 : {
4492 :
4493 27741 : no_cb = 1;
4494 27741 : half_coding_subbands = coding_subbands / 2;
4495 345498 : for ( j = 0; j < half_coding_subbands; j++ )
4496 : {
4497 317757 : no_cb *= no_cv[j];
4498 : }
4499 27741 : *nbits_fr = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
4500 27741 : no_cb = 1;
4501 347959 : for ( j = half_coding_subbands; j < coding_subbands; j++ )
4502 : {
4503 320218 : no_cb *= no_cv[j];
4504 : }
4505 27741 : *nbits_fr1 = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
4506 : }
4507 : else
4508 : {
4509 141502 : no_cb = 1;
4510 1519468 : for ( j = 0; j < coding_subbands; j++ )
4511 : {
4512 1377966 : no_cb *= no_cv[j];
4513 : }
4514 141502 : *nbits_fr = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
4515 141502 : *nbits_fr1 = 0;
4516 : }
4517 :
4518 :
4519 169243 : minimum_s( (const int16_t *) idx_sur_coh_shift, (int16_t) idx_shift_len, &min_idx );
4520 2183019 : for ( j = 0; j < idx_shift_len; j++ )
4521 : {
4522 2013776 : mr_idx[j] = idx_sur_coh_shift[j] - min_idx;
4523 2013776 : no_cv_shift[j] -= min_idx;
4524 : }
4525 169243 : nbits = min_idx + 1 + GR_bits_new( mr_idx, no_cv_shift, idx_shift_len, *GR_ord, 1, GR_ord );
4526 169243 : *p_min_idx = min_idx;
4527 :
4528 169243 : return nbits;
4529 : }
4530 :
4531 :
4532 : /*-------------------------------------------------------------------*
4533 : * encode_spread_coherence_1sf()
4534 : *
4535 : * Encoding spread coherence for 1 subframe bands
4536 : *-------------------------------------------------------------------*/
4537 :
4538 : /*! r: number of bits written */
4539 29622 : static int16_t encode_spread_coherence_1sf(
4540 : IVAS_QMETADATA *q_metadata, /* i : quantized metadata */
4541 : const int16_t idx_d, /* i : current direction index */
4542 : BSTR_ENC_HANDLE hMasaMetaData, /* i/o: metadata bitstream handle */
4543 : const int16_t hrmasa_flag /* i : flag indicating high-rate MASA MD coding */
4544 : )
4545 : {
4546 : int16_t i, j, k;
4547 : int16_t idx_ER;
4548 : int16_t nbits, nbits_fr;
4549 : uint16_t idx_sp_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4550 : uint16_t mr_idx_sp_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4551 : int16_t GR_ord, bits_GR;
4552 : uint64_t idx, idx1;
4553 : int16_t no_idx16;
4554 : int16_t no_cv[MASA_MAXIMUM_CODING_SUBBANDS];
4555 : IVAS_QDIRECTION *q_direction;
4556 : int16_t half_coding_subbands, nbits_fr1, coding_subbands;
4557 : uint16_t idx_sp_coh_shift[MASA_MAXIMUM_CODING_SUBBANDS];
4558 : uint8_t idx_shift;
4559 29622 : int16_t max_val = 0, nbits_max;
4560 : int16_t extra_cv;
4561 : int16_t no_cv_shift[MASA_MAXIMUM_CODING_SUBBANDS], min_idx;
4562 :
4563 29622 : coding_subbands = q_metadata->q_direction[idx_d].cfg.nbands;
4564 29622 : q_direction = &( q_metadata->q_direction[idx_d] );
4565 29622 : nbits = 0;
4566 29622 : GR_ord = 1;
4567 29622 : idx_shift = 0;
4568 :
4569 : /* number of codevectors added dependent on number of subbands */
4570 29622 : extra_cv = coding_subbands / MASA_FACTOR_CV_COH;
4571 339813 : for ( j = 0; j < coding_subbands; j++ )
4572 : {
4573 310191 : if ( hrmasa_flag )
4574 : {
4575 0 : idx_ER = 7 - ( q_direction->band_data[j].energy_ratio_index_mod[0] >> 1 ) + extra_cv;
4576 : }
4577 : else
4578 : {
4579 310191 : idx_ER = 7 - q_direction->band_data[j].energy_ratio_index_mod[0] + extra_cv;
4580 : }
4581 :
4582 310191 : if ( idx_ER > 0 )
4583 : {
4584 288563 : idx_sp_coh[j] = (uint16_t) roundf( q_direction->coherence_band_data[j].spread_coherence[0] / ( 255.0f / (float) idx_ER ) );
4585 288563 : q_direction->coherence_band_data[j].spread_coherence[0] = (uint8_t) roundf( idx_sp_coh[j] * ( 255.0f / (float) idx_ER ) );
4586 : }
4587 : else
4588 : {
4589 21628 : idx_sp_coh[j] = 0;
4590 21628 : q_direction->coherence_band_data[j].spread_coherence[0] = 0;
4591 : }
4592 310191 : no_cv[j] = idx_ER + 1;
4593 :
4594 310191 : no_cv_shift[idx_shift] = no_cv[j];
4595 310191 : idx_sp_coh_shift[idx_shift++] = idx_sp_coh[j];
4596 : }
4597 :
4598 29622 : if ( sum_s( no_cv, coding_subbands ) == coding_subbands )
4599 : {
4600 1548 : return 0;
4601 : }
4602 :
4603 28074 : nbits_max = 0;
4604 28074 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
4605 : {
4606 16383 : j = maximum_s( (int16_t *) idx_sp_coh, coding_subbands, &max_val );
4607 268737 : for ( j = 0; j < coding_subbands; j++ )
4608 : {
4609 252354 : if ( no_cv[j] > max_val + 1 )
4610 : {
4611 209894 : no_cv[j] = max_val + 1;
4612 : }
4613 : }
4614 16383 : nbits_max = MASA_MAX_NO_CV_SUR_COH - max_val + extra_cv;
4615 : }
4616 :
4617 28074 : nbits = coherence_coding_length( idx_sp_coh_shift, idx_shift, coding_subbands, no_cv,
4618 : mr_idx_sp_coh, no_cv_shift, &min_idx, &GR_ord, &nbits_fr, &nbits_fr1 );
4619 28074 : half_coding_subbands = 0;
4620 28074 : idx1 = 0;
4621 :
4622 28074 : if ( nbits_fr + nbits_fr1 + nbits_max < nbits )
4623 : {
4624 : /* write flag*/
4625 13760 : push_next_indice( hMasaMetaData, 0, 1 );
4626 :
4627 : /* create combined index */
4628 13760 : nbits = nbits_fr + nbits_fr1 + 1;
4629 :
4630 13760 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
4631 : {
4632 : /* write max value*/
4633 5100 : bits_GR = hMasaMetaData->nb_bits_tot;
4634 5100 : 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 );
4635 5100 : nbits += hMasaMetaData->nb_bits_tot - bits_GR;
4636 : }
4637 :
4638 13760 : if ( nbits_fr1 > 0 )
4639 : {
4640 302 : half_coding_subbands = coding_subbands / 2;
4641 302 : idx = create_combined_index( idx_sp_coh, half_coding_subbands, no_cv );
4642 302 : idx1 = create_combined_index( &idx_sp_coh[half_coding_subbands], half_coding_subbands, &no_cv[half_coding_subbands] );
4643 : }
4644 : else
4645 : {
4646 13458 : idx = create_combined_index( idx_sp_coh, coding_subbands, no_cv );
4647 : }
4648 :
4649 13760 : if ( nbits_fr % 16 == 0 )
4650 : {
4651 4427 : no_idx16 = nbits_fr / 16;
4652 : }
4653 : else
4654 : {
4655 9333 : no_idx16 = (int16_t) round_f( ( nbits_fr / 16.0f + 0.5f ) );
4656 : }
4657 :
4658 : /* write combined index */
4659 13760 : k = nbits_fr;
4660 14642 : for ( i = 0; i < no_idx16 - 1; i++ )
4661 : {
4662 882 : k -= 16;
4663 882 : push_next_indice( hMasaMetaData, ( ( idx >> k ) & 65535 ), 16 ); /* 16 bits */
4664 : }
4665 :
4666 13760 : push_next_indice( hMasaMetaData, ( idx & ( ( 1 << k ) - 1 ) ), k );
4667 :
4668 13760 : if ( nbits_fr1 > 0 )
4669 : {
4670 302 : if ( nbits_fr1 % 16 == 0 )
4671 : {
4672 16 : no_idx16 = nbits_fr1 / 16;
4673 : }
4674 : else
4675 : {
4676 286 : no_idx16 = (int16_t) round_f( ( nbits_fr1 / 16.0f + 0.5f ) );
4677 : }
4678 :
4679 302 : assert( no_idx16 <= 4 );
4680 :
4681 302 : k = nbits_fr1;
4682 723 : for ( i = 0; i < no_idx16 - 1; i++ )
4683 : {
4684 421 : k -= 16;
4685 421 : push_next_indice( hMasaMetaData, ( ( idx1 >> k ) & 65535 ), 16 ); /* 16 bits */
4686 : }
4687 302 : push_next_indice( hMasaMetaData, ( idx1 & ( ( 1 << k ) - 1 ) ), k );
4688 : }
4689 : }
4690 : else
4691 : {
4692 : /* write flag */
4693 14314 : nbits = 1;
4694 :
4695 : /* write flag*/
4696 14314 : push_next_indice( hMasaMetaData, 1, 1 );
4697 :
4698 : /* write GR_ord */
4699 14314 : push_next_indice( hMasaMetaData, GR_ord, 1 );
4700 14314 : nbits += 1;
4701 :
4702 : /* write the min */
4703 14314 : bits_GR = hMasaMetaData->nb_bits_tot;
4704 14314 : ivas_qmetadata_encode_extended_gr( hMasaMetaData, min_idx, MASA_MAX_NO_CV_SUR_COH + extra_cv, 0 );
4705 14314 : nbits += hMasaMetaData->nb_bits_tot - bits_GR;
4706 :
4707 : /* write GR data */
4708 200089 : for ( j = 0; j < idx_shift; j++ )
4709 : {
4710 185775 : bits_GR = hMasaMetaData->nb_bits_tot;
4711 185775 : ivas_qmetadata_encode_extended_gr( hMasaMetaData, mr_idx_sp_coh[j], no_cv_shift[j], GR_ord );
4712 185775 : nbits += hMasaMetaData->nb_bits_tot - bits_GR;
4713 : }
4714 : }
4715 :
4716 28074 : return nbits;
4717 : }
4718 :
4719 :
4720 : /*-------------------------------------------------------------------*
4721 : * encode_surround_coherence()
4722 : *
4723 : * encoding surround coherence
4724 : *-------------------------------------------------------------------*/
4725 :
4726 : /*! r: number of bits written */
4727 116901 : static int16_t encode_surround_coherence(
4728 : IVAS_QMETADATA *hQMetaData, /* i : quantized metadata */
4729 : BSTR_ENC_HANDLE hMetaData /* i/o: metadata bitstream handle */
4730 : )
4731 : {
4732 : int16_t i, j, k;
4733 : int16_t idx_ER, idx16;
4734 : int16_t nbits, nbits_fr;
4735 : uint16_t idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4736 : uint16_t mr_idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4737 : int16_t GR_ord, bits_GR;
4738 : uint64_t idx, idx1;
4739 : int16_t no_idx16;
4740 : int16_t no_cv[MASA_MAXIMUM_CODING_SUBBANDS];
4741 : float error_ratio_surr;
4742 : IVAS_QDIRECTION *q_direction;
4743 : int16_t half_coding_subbands, nbits_fr1, coding_subbands;
4744 : int16_t all_coherence_zero;
4745 : uint16_t idx_sur_coh_shift[MASA_MAXIMUM_CODING_SUBBANDS];
4746 : uint8_t idx_shift;
4747 116901 : int16_t max_val = 0, nbits_max;
4748 : int16_t no_cv_shift[MASA_MAXIMUM_CODING_SUBBANDS], min_idx;
4749 :
4750 116901 : coding_subbands = hQMetaData->q_direction[0].cfg.nbands;
4751 116901 : all_coherence_zero = hQMetaData->all_coherence_zero;
4752 116901 : q_direction = &( hQMetaData->q_direction[0] );
4753 116901 : nbits = 0;
4754 :
4755 116901 : if ( all_coherence_zero == 1 )
4756 : {
4757 0 : nbits = 0;
4758 : }
4759 : else
4760 : {
4761 116901 : GR_ord = 1;
4762 116901 : k = 0;
4763 116901 : idx_shift = 0;
4764 1261033 : for ( j = 0; j < coding_subbands; j++ )
4765 : {
4766 1144132 : if ( hQMetaData->no_directions == 2 )
4767 : {
4768 346545 : k += hQMetaData->twoDirBands[j];
4769 346545 : idx16 = max( k - 1, 0 );
4770 346545 : 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];
4771 : }
4772 : else
4773 : {
4774 797587 : error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[0];
4775 : }
4776 :
4777 1144132 : if ( error_ratio_surr <= 0 )
4778 : {
4779 1517 : error_ratio_surr = 0;
4780 1517 : idx_sur_coh[j] = 0;
4781 1517 : no_cv[j] = 1;
4782 1517 : 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]; */
4783 : }
4784 : else
4785 : {
4786 1142615 : idx_ER = masa_sq( error_ratio_surr, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
4787 :
4788 2285230 : idx_sur_coh[j] = squant_int( hQMetaData->surcoh_band_data[j].surround_coherence[0], &hQMetaData->surcoh_band_data[j].surround_coherence[0],
4789 1142615 : &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 );
4790 :
4791 1142615 : no_cv[j] = idx_cb_sur_coh_masa[idx_ER] + 2;
4792 1142615 : no_cv_shift[idx_shift] = no_cv[j];
4793 1142615 : idx_sur_coh_shift[idx_shift++] = idx_sur_coh[j];
4794 : }
4795 : }
4796 :
4797 116901 : if ( sum_s( no_cv, coding_subbands ) == coding_subbands )
4798 : {
4799 0 : return 0;
4800 : }
4801 :
4802 116901 : nbits_max = 0;
4803 116901 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
4804 : {
4805 41729 : j = maximum_s( (int16_t *) idx_sur_coh, coding_subbands, &max_val );
4806 777557 : for ( j = 0; j < coding_subbands; j++ )
4807 : {
4808 735828 : if ( no_cv[j] > max_val + 1 )
4809 : {
4810 544315 : no_cv[j] = max_val + 1;
4811 : }
4812 : }
4813 41729 : nbits_max = MASA_MAX_NO_CV_SUR_COH - max_val; /* encoded with GR0 as max_no_vals - no_vals*/
4814 : }
4815 :
4816 116901 : nbits = coherence_coding_length( idx_sur_coh_shift, idx_shift, coding_subbands, no_cv,
4817 : mr_idx_sur_coh, no_cv_shift, &min_idx, &GR_ord, &nbits_fr, &nbits_fr1 );
4818 116901 : half_coding_subbands = coding_subbands / 2;
4819 116901 : idx1 = 0;
4820 :
4821 : /* should check how to encode the average - check distribution */
4822 116901 : if ( nbits_fr + nbits_fr1 + nbits_max < nbits )
4823 : {
4824 : /* write flag*/
4825 21582 : push_next_indice( hMetaData, 0, 1 );
4826 :
4827 : /* create combined index */
4828 21582 : nbits = nbits_fr + nbits_fr1 + 1;
4829 21582 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
4830 : {
4831 : /* write max value*/
4832 8582 : bits_GR = hMetaData->nb_bits_tot;
4833 8582 : ivas_qmetadata_encode_extended_gr( hMetaData, MASA_MAX_NO_CV_SUR_COH - max_val - 1, MASA_MAX_NO_CV_SUR_COH, 0 );
4834 8582 : nbits += hMetaData->nb_bits_tot - bits_GR;
4835 : }
4836 :
4837 21582 : if ( nbits_fr1 > 0 )
4838 : {
4839 3938 : idx = create_combined_index( idx_sur_coh, half_coding_subbands, no_cv );
4840 3938 : idx1 = create_combined_index( &idx_sur_coh[half_coding_subbands], half_coding_subbands, &no_cv[half_coding_subbands] );
4841 : }
4842 : else
4843 : {
4844 17644 : idx = create_combined_index( idx_sur_coh, coding_subbands, no_cv );
4845 : }
4846 :
4847 21582 : if ( nbits_fr % 16 == 0 )
4848 : {
4849 2147 : no_idx16 = nbits_fr / 16;
4850 : }
4851 : else
4852 : {
4853 19435 : no_idx16 = (int16_t) round_f( ( nbits_fr / 16.0f + 0.5f ) );
4854 : }
4855 :
4856 : /* write combined index */
4857 21582 : k = nbits_fr;
4858 32433 : for ( i = 0; i < no_idx16 - 1; i++ )
4859 : {
4860 10851 : k -= 16;
4861 10851 : push_next_indice( hMetaData, ( ( idx >> k ) & 65535 ), 16 ); /* 16 bits */
4862 : }
4863 :
4864 21582 : push_next_indice( hMetaData, ( idx & ( ( 1 << k ) - 1 ) ), k );
4865 :
4866 21582 : if ( nbits_fr1 > 0 )
4867 : {
4868 3938 : if ( nbits_fr1 % 16 == 0 )
4869 : {
4870 283 : no_idx16 = nbits_fr1 / 16;
4871 : }
4872 : else
4873 : {
4874 3655 : no_idx16 = (int16_t) round_f( ( nbits_fr1 / 16.0f + 0.5f ) );
4875 : }
4876 :
4877 3938 : assert( no_idx16 <= 4 );
4878 :
4879 3938 : k = nbits_fr1;
4880 9123 : for ( i = 0; i < no_idx16 - 1; i++ )
4881 : {
4882 5185 : k -= 16;
4883 5185 : push_next_indice( hMetaData, ( ( idx1 >> k ) & 65535 ), 16 ); /* 16 bits */
4884 : }
4885 :
4886 3938 : push_next_indice( hMetaData, ( idx1 & ( ( 1 << k ) - 1 ) ), k );
4887 : }
4888 : }
4889 : else
4890 : {
4891 : /* write flag */
4892 95319 : nbits = 1;
4893 :
4894 : /* write flag*/
4895 95319 : push_next_indice( hMetaData, 1, 1 );
4896 :
4897 : /* write GR_ord */
4898 95319 : push_next_indice( hMetaData, GR_ord, 1 );
4899 95319 : nbits += 1;
4900 :
4901 : /* write the min */
4902 95319 : bits_GR = hMetaData->nb_bits_tot;
4903 95319 : ivas_qmetadata_encode_extended_gr( hMetaData, min_idx, MASA_MAX_NO_CV_SUR_COH, 0 );
4904 95319 : nbits += hMetaData->nb_bits_tot - bits_GR;
4905 :
4906 : /* write GR data */
4907 1026376 : for ( j = 0; j < idx_shift; j++ )
4908 : {
4909 931057 : bits_GR = hMetaData->nb_bits_tot;
4910 :
4911 931057 : ivas_qmetadata_encode_extended_gr( hMetaData, mr_idx_sur_coh[j], no_cv_shift[j], GR_ord );
4912 :
4913 931057 : nbits += hMetaData->nb_bits_tot - bits_GR;
4914 : }
4915 : }
4916 : }
4917 : #ifdef DEBUGGING
4918 : for ( i = 0; i < coding_subbands; i++ )
4919 : {
4920 : for ( j = 1; j < q_direction->cfg.nblocks; j++ )
4921 : {
4922 : hQMetaData->surcoh_band_data[i].surround_coherence[j] = hQMetaData->surcoh_band_data[i].surround_coherence[0];
4923 : }
4924 : }
4925 : #endif
4926 :
4927 116901 : return nbits;
4928 : }
4929 :
4930 :
4931 7576 : static int16_t encode_surround_coherence_hr(
4932 : IVAS_QMETADATA *hQMetaData, /* i : quantized metadata */
4933 : BSTR_ENC_HANDLE hMetaData /* i/o: metadata bitstream handle */
4934 : )
4935 : {
4936 : int16_t i, j, k, sf;
4937 : int16_t nbits, nbits_fr, nbits_sf;
4938 : uint16_t idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4939 : uint16_t mr_idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4940 : int16_t GR_ord, bits_GR;
4941 : uint64_t idx, idx1;
4942 : int16_t no_idx16;
4943 : int16_t no_cv[MASA_MAXIMUM_CODING_SUBBANDS];
4944 : float error_ratio_surr;
4945 : IVAS_QDIRECTION *q_direction;
4946 : int16_t half_coding_subbands, nbits_fr1, coding_subbands;
4947 : int16_t all_coherence_zero;
4948 : uint16_t idx_sur_coh_shift[MASA_MAXIMUM_CODING_SUBBANDS];
4949 : uint8_t idx_shift;
4950 7576 : int16_t max_val = 0, nbits_max;
4951 : int16_t no_cv_shift[MASA_MAXIMUM_CODING_SUBBANDS], min_idx;
4952 : int16_t idx16;
4953 : int32_t int_error_ratio_surr;
4954 :
4955 7576 : coding_subbands = hQMetaData->q_direction[0].cfg.nbands;
4956 7576 : all_coherence_zero = hQMetaData->all_coherence_zero;
4957 7576 : q_direction = &( hQMetaData->q_direction[0] );
4958 7576 : nbits = 0;
4959 :
4960 7576 : if ( all_coherence_zero == 1 )
4961 : {
4962 0 : nbits = 0;
4963 : }
4964 : else
4965 : {
4966 31844 : for ( sf = 0; sf < hQMetaData->q_direction[0].cfg.nblocks; sf++ )
4967 : {
4968 24268 : GR_ord = 1;
4969 24268 : k = 0;
4970 24268 : idx_shift = 0;
4971 591908 : for ( j = 0; j < coding_subbands; j++ )
4972 : {
4973 567640 : if ( hQMetaData->no_directions == 2 )
4974 : {
4975 119164 : k += hQMetaData->twoDirBands[j];
4976 119164 : idx16 = max( k - 1, 0 );
4977 119164 : 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];
4978 : }
4979 : else
4980 : {
4981 448476 : error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[sf];
4982 : }
4983 :
4984 567640 : int_error_ratio_surr = (int32_t) ( MASA_SUR_COH_PRECISION * error_ratio_surr );
4985 567640 : error_ratio_surr = (float) ( int_error_ratio_surr * MASA_SUR_COH_THRESHOLD );
4986 :
4987 567640 : if ( error_ratio_surr <= 0 )
4988 : {
4989 648 : error_ratio_surr = 0;
4990 648 : idx_sur_coh[j] = 0;
4991 648 : no_cv[j] = 1;
4992 648 : 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]; */
4993 : }
4994 : else
4995 : {
4996 1133984 : idx_sur_coh[j] = squant_int( hQMetaData->surcoh_band_data[j].surround_coherence[sf], &hQMetaData->surcoh_band_data[j].surround_coherence[sf],
4997 566992 : &sur_coherence_cb_masa[idx_cb_sur_coh_masa[7] * MASA_MAX_NO_CV_SUR_COH], idx_cb_sur_coh_masa[7] + 2 );
4998 566992 : no_cv[j] = idx_cb_sur_coh_masa[7] + 2;
4999 566992 : no_cv_shift[idx_shift] = no_cv[j];
5000 566992 : idx_sur_coh_shift[idx_shift++] = idx_sur_coh[j];
5001 : }
5002 : }
5003 :
5004 24268 : if ( sum_s( no_cv, coding_subbands ) != coding_subbands )
5005 : {
5006 24268 : nbits_max = 0;
5007 24268 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
5008 : {
5009 24221 : j = maximum_s( (int16_t *) idx_sur_coh, coding_subbands, &max_val );
5010 591626 : for ( j = 0; j < coding_subbands; j++ )
5011 : {
5012 567405 : if ( no_cv[j] > max_val + 1 )
5013 : {
5014 450564 : no_cv[j] = max_val + 1;
5015 : }
5016 : }
5017 24221 : nbits_max = MASA_MAX_NO_CV_SUR_COH - max_val; /* encoded with GR0 as max_no_vals - no_vals*/
5018 : }
5019 24268 : if ( max_val == 0 )
5020 : {
5021 1008 : for ( j = 0; j < coding_subbands; j++ )
5022 : {
5023 928 : hQMetaData->surcoh_band_data[j].surround_coherence[sf] = 0;
5024 : }
5025 : }
5026 24268 : nbits_sf = coherence_coding_length( idx_sur_coh_shift, idx_shift, coding_subbands, no_cv,
5027 : mr_idx_sur_coh, no_cv_shift, &min_idx, &GR_ord, &nbits_fr, &nbits_fr1 );
5028 24268 : half_coding_subbands = coding_subbands / 2;
5029 24268 : idx1 = 0;
5030 :
5031 : /* should check how to encode the average - check distribution */
5032 24268 : if ( nbits_fr + nbits_fr1 + nbits_max < nbits_sf )
5033 : {
5034 : /* write flag*/
5035 6564 : push_next_indice( hMetaData, 0, 1 );
5036 :
5037 : /* create combined index */
5038 6564 : nbits += nbits_fr + nbits_fr1 + 1;
5039 6564 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
5040 : {
5041 : /* write max value*/
5042 6564 : bits_GR = hMetaData->nb_bits_tot;
5043 6564 : ivas_qmetadata_encode_extended_gr( hMetaData, MASA_MAX_NO_CV_SUR_COH - max_val - 1, MASA_MAX_NO_CV_SUR_COH, 0 );
5044 6564 : nbits += hMetaData->nb_bits_tot - bits_GR;
5045 : }
5046 :
5047 6564 : if ( nbits_fr1 > 0 )
5048 : {
5049 5434 : idx = create_combined_index( idx_sur_coh, half_coding_subbands, no_cv );
5050 5434 : idx1 = create_combined_index( &idx_sur_coh[half_coding_subbands], half_coding_subbands, &no_cv[half_coding_subbands] );
5051 : }
5052 : else
5053 : {
5054 1130 : idx = create_combined_index( idx_sur_coh, coding_subbands, no_cv );
5055 : }
5056 :
5057 6564 : if ( nbits_fr % 16 == 0 )
5058 : {
5059 1233 : no_idx16 = nbits_fr / 16;
5060 : }
5061 : else
5062 : {
5063 5331 : no_idx16 = (int16_t) round_f( ( nbits_fr / 16.0f + 0.5f ) );
5064 : }
5065 :
5066 : /* write combined index */
5067 6564 : k = nbits_fr;
5068 17740 : for ( i = 0; i < no_idx16 - 1; i++ )
5069 : {
5070 11176 : k -= 16;
5071 11176 : push_next_indice( hMetaData, ( ( idx >> k ) & 65535 ), 16 ); /* 16 bits */
5072 : }
5073 :
5074 6564 : push_next_indice( hMetaData, ( idx & ( ( 1 << k ) - 1 ) ), k );
5075 :
5076 6564 : if ( nbits_fr1 > 0 )
5077 : {
5078 5434 : if ( nbits_fr1 % 16 == 0 )
5079 : {
5080 811 : no_idx16 = nbits_fr1 / 16;
5081 : }
5082 : else
5083 : {
5084 4623 : no_idx16 = (int16_t) round_f( ( nbits_fr1 / 16.0f + 0.5f ) );
5085 : }
5086 :
5087 5434 : assert( no_idx16 <= 4 );
5088 :
5089 5434 : k = nbits_fr1;
5090 14720 : for ( i = 0; i < no_idx16 - 1; i++ )
5091 : {
5092 9286 : k -= 16;
5093 9286 : push_next_indice( hMetaData, ( ( idx1 >> k ) & 65535 ), 16 ); /* 16 bits */
5094 : }
5095 :
5096 5434 : push_next_indice( hMetaData, ( idx1 & ( ( 1 << k ) - 1 ) ), k );
5097 : }
5098 : }
5099 : else
5100 : {
5101 : /* write flag */
5102 17704 : nbits += 1;
5103 :
5104 : /* write flag*/
5105 17704 : push_next_indice( hMetaData, 1, 1 );
5106 :
5107 : /* write GR_ord */
5108 17704 : push_next_indice( hMetaData, GR_ord, 1 );
5109 17704 : nbits += 1;
5110 :
5111 : /* write the min */
5112 17704 : bits_GR = hMetaData->nb_bits_tot;
5113 17704 : ivas_qmetadata_encode_extended_gr( hMetaData, min_idx, MASA_MAX_NO_CV_SUR_COH, 0 );
5114 17704 : nbits += hMetaData->nb_bits_tot - bits_GR;
5115 :
5116 : /* write GR data */
5117 429355 : for ( j = 0; j < idx_shift; j++ )
5118 : {
5119 411651 : bits_GR = hMetaData->nb_bits_tot;
5120 :
5121 411651 : ivas_qmetadata_encode_extended_gr( hMetaData, mr_idx_sur_coh[j], no_cv_shift[j], GR_ord );
5122 :
5123 411651 : nbits += hMetaData->nb_bits_tot - bits_GR;
5124 : }
5125 : }
5126 : }
5127 : }
5128 : }
5129 :
5130 7576 : return nbits;
5131 : }
5132 :
5133 :
5134 : /*-------------------------------------------------------------------*
5135 : * quantize_DCT_0_coh()
5136 : *
5137 : * quanization of DCT component of order zero for transformed coherence vector
5138 : *-------------------------------------------------------------------*/
5139 :
5140 : /*! r: quantized value */
5141 969174 : static float quantize_DCT_0_coh(
5142 : const float x, /* i : input value */
5143 : const int16_t j, /* i : subband index */
5144 : const float *coherence_cb, /* i : coherence codebook */
5145 : const float delta_var, /* i : azimuth variance threshold */
5146 : const int16_t no_cb, /* i : maximum number of codewords */
5147 : IVAS_QDIRECTION *q_direction, /* i : quantized metadata */
5148 : uint16_t *idx_x, /* o : codewords index */
5149 : int16_t *p_no_cb, /* o : actual number of codewords dependent on energy ratio value */
5150 : const int16_t hrmasa_flag /* i : flag indicating high-rate MASA MD coding */
5151 : )
5152 : {
5153 : float var_azi, xhat;
5154 : int16_t idx_sub_cb, idx;
5155 : int16_t min_index;
5156 : /* quantize first DCT component */
5157 969174 : var_azi = var( q_direction->band_data[j].azimuth, q_direction->cfg.nblocks );
5158 :
5159 969174 : if ( hrmasa_flag )
5160 : {
5161 0 : minimum_s( (int16_t *) ( q_direction->band_data[j].energy_ratio_index ), q_direction->cfg.nblocks, &min_index );
5162 0 : min_index = min_index >> 1;
5163 : }
5164 : else
5165 : {
5166 969174 : min_index = q_direction->band_data[j].energy_ratio_index[0];
5167 : }
5168 :
5169 969174 : if ( var_azi < delta_var )
5170 : {
5171 325309 : idx_sub_cb = no_cb * min_index;
5172 : }
5173 : else
5174 : {
5175 643865 : idx_sub_cb = no_cb * ( min_index + DIRAC_DIFFUSE_LEVELS );
5176 : }
5177 :
5178 969174 : idx = squant( x, &xhat, &coherence_cb[idx_sub_cb], len_cb_dct0_masa[min_index] );
5179 :
5180 969174 : *p_no_cb = len_cb_dct0_masa[min_index];
5181 969174 : *idx_x = idx;
5182 :
5183 969174 : return xhat;
5184 : }
5185 :
5186 :
5187 : /*-------------------------------------------------------------------*
5188 : * encode_coherence_indexesDCT1()
5189 : *
5190 : * Encoding DCT1 coeffs with joint index or EC
5191 : *-------------------------------------------------------------------*/
5192 :
5193 : /*! r: number of bits written */
5194 127046 : static int16_t encode_coherence_indexesDCT1(
5195 : uint16_t *idx_dct, /* i : data to be encoded */
5196 : const int16_t len, /* i : number of data */
5197 : BSTR_ENC_HANDLE hMetaData /* i : metadata handle */
5198 : )
5199 : {
5200 : int16_t i, nbits, GR_ord;
5201 : uint16_t av;
5202 : uint16_t mr_idx_dct[MASA_MAXIMUM_CODING_SUBBANDS];
5203 :
5204 127046 : GR_ord = 0;
5205 127046 : nbits = 0;
5206 :
5207 127046 : nbits = mean_removed_GR_new( idx_dct, MASA_NO_CV_COH1, len, 0, &GR_ord, &av, mr_idx_dct );
5208 :
5209 1096220 : for ( i = 0; i < len; i++ )
5210 : {
5211 969174 : ivas_qmetadata_encode_extended_gr( hMetaData, mr_idx_dct[i], 2 * MASA_NO_CV_COH1, GR_ord );
5212 : }
5213 :
5214 127046 : nbits += len_huf_masa[av];
5215 :
5216 127046 : push_next_indice( hMetaData, huff_code_av_masa[av], len_huf_masa[av] );
5217 :
5218 127046 : return nbits;
5219 : }
5220 :
5221 :
5222 : /*-------------------------------------------------------------------*
5223 : * dct4_transform()
5224 : *
5225 : * 4D implementation of DCT transform
5226 : *-------------------------------------------------------------------*/
5227 :
5228 1938348 : static void dct4_transform(
5229 : uint8_t *v, /* i : input 4D vector */
5230 : float *dct_v /* o : output transformed vector */
5231 : )
5232 : {
5233 : float a, b, c, d;
5234 :
5235 1938348 : a = ( v[0] + v[3] ) / 256.0f;
5236 1938348 : b = ( v[1] + v[2] ) / 256.0f;
5237 1938348 : c = ( v[0] - v[3] ) / 256.0f;
5238 1938348 : d = ( v[1] - v[2] ) / 256.0f;
5239 :
5240 1938348 : dct_v[0] = 0.5f * ( a + b );
5241 1938348 : dct_v[1] = 0.653281482438188f * c + 0.270598050073099f * d;
5242 1938348 : dct_v[2] = 0.5f * ( a - b );
5243 1938348 : dct_v[3] = 0.270598050073099f * c - 0.653281482438188f * d;
5244 :
5245 1938348 : return;
5246 : }
5247 :
5248 :
5249 : /*-------------------------------------------------------------------*
5250 : * ivas_qmetadata_quantize_coherence_hr_512()
5251 : *
5252 : *
5253 : *-------------------------------------------------------------------*/
5254 :
5255 9421 : static int16_t ivas_qmetadata_quantize_coherence_hr_512(
5256 : IVAS_QMETADATA *hQMetaData, /* i/o: quantized metadata */
5257 : const int16_t idx_d, /* i : current direction index */
5258 : const int16_t all_coherence_zero, /* i : all coherence is zero - flag */
5259 : BSTR_ENC_HANDLE hMetaData, /* i : metadata handle */
5260 : const int16_t bits_coh )
5261 : {
5262 : int16_t j, k;
5263 : int16_t nbands, nblocks;
5264 : int16_t nbits;
5265 : int16_t nbits1, nbits0, nbits_av;
5266 : uint16_t idx_coh[MASA_MAXIMUM_CODING_SUBBANDS];
5267 : IVAS_QDIRECTION *q_direction;
5268 : int16_t cbsize;
5269 : float delta, tmp;
5270 : int16_t min_idx, GR_param, GR_param_av;
5271 : uint16_t av, mr_idx[MASA_MAXIMUM_CODING_SUBBANDS];
5272 :
5273 9421 : q_direction = &( hQMetaData->q_direction[idx_d] );
5274 9421 : nbands = q_direction->cfg.nbands;
5275 9421 : nblocks = q_direction->cfg.nblocks;
5276 9421 : nbits = 0;
5277 :
5278 9421 : if ( all_coherence_zero == 1 )
5279 : {
5280 0 : return nbits;
5281 : }
5282 9421 : nbits = hMetaData->nb_bits_tot;
5283 :
5284 9421 : cbsize = 1 << bits_coh;
5285 9421 : delta = 256.0f / cbsize;
5286 :
5287 38687 : for ( k = 0; k < nblocks; k++ )
5288 : {
5289 29266 : min_idx = 0;
5290 667488 : for ( j = 0; j < nbands; j++ )
5291 : {
5292 638222 : idx_coh[j] = usquant( (float) ( q_direction->coherence_band_data[j].spread_coherence[k] ), &tmp, delta / 2.0f, delta, cbsize );
5293 638222 : q_direction->coherence_band_data[j].spread_coherence[k] = (uint8_t) ( idx_coh[j] * delta + delta / 2.0f );
5294 638222 : if ( idx_coh[j] < min_idx )
5295 : {
5296 0 : min_idx = idx_coh[j];
5297 : }
5298 : }
5299 :
5300 29266 : nbits0 = 0;
5301 29266 : nbits1 = 0;
5302 667488 : for ( j = 0; j < nbands; j++ )
5303 : {
5304 638222 : idx_coh[j] = idx_coh[j] - min_idx;
5305 638222 : nbits0 += ivas_qmetadata_encode_extended_gr_length( idx_coh[j], cbsize - min_idx, 0 );
5306 638222 : nbits1 += ivas_qmetadata_encode_extended_gr_length( idx_coh[j], cbsize - min_idx, 1 );
5307 : }
5308 29266 : if ( nbits0 < nbits1 )
5309 : {
5310 17356 : GR_param = 0;
5311 17356 : nbits1 = nbits0;
5312 : }
5313 : else
5314 : {
5315 11910 : GR_param = 1;
5316 : }
5317 :
5318 29266 : GR_param_av = 1;
5319 29266 : nbits_av = mean_removed_GR_new( idx_coh, cbsize, nbands, 1, &GR_param_av, &av, mr_idx );
5320 :
5321 29266 : if ( nbits_av < nbits1 )
5322 : {
5323 8975 : nbits1 = nbits_av;
5324 8975 : GR_param = GR_param_av;
5325 :
5326 : /* use average removed */
5327 8975 : push_next_indice( hMetaData, 1, 1 );
5328 :
5329 : /* write average */
5330 8975 : push_next_indice( hMetaData, av, bits_coh );
5331 :
5332 : /* write GR param */
5333 8975 : push_next_indice( hMetaData, GR_param, 1 );
5334 :
5335 205696 : for ( j = 0; j < nbands; j++ )
5336 : {
5337 196721 : ivas_qmetadata_encode_extended_gr( hMetaData, mr_idx[j], 2 * cbsize, GR_param );
5338 : }
5339 : }
5340 : else
5341 : {
5342 : /* use min removed */
5343 20291 : push_next_indice( hMetaData, 0, 1 );
5344 :
5345 : /* write min index */
5346 20291 : push_next_indice( hMetaData, min_idx, bits_coh );
5347 :
5348 : /* write GR param */
5349 20291 : push_next_indice( hMetaData, GR_param, 1 );
5350 :
5351 461792 : for ( j = 0; j < nbands; j++ )
5352 : {
5353 441501 : ivas_qmetadata_encode_extended_gr( hMetaData, idx_coh[j], cbsize - min_idx, GR_param );
5354 : }
5355 : }
5356 : }
5357 :
5358 9421 : nbits = hMetaData->nb_bits_tot - nbits;
5359 9421 : return nbits;
5360 : }
5361 :
5362 :
5363 : /*-------------------------------------------------------------------*
5364 : * ivas_qmetadata_quantize_coherence()
5365 : *
5366 : *
5367 : *-------------------------------------------------------------------*/
5368 :
5369 : /*! r: number of bits written */
5370 283714 : static int16_t ivas_qmetadata_quantize_coherence(
5371 : IVAS_QMETADATA *hQMetaData, /* i/o: quantized metadata */
5372 : const int16_t idx_d, /* i : current direction index */
5373 : const int16_t all_coherence_zero, /* i : all coherence is zero - flag */
5374 : BSTR_ENC_HANDLE hMetaData, /* i : metadata handle */
5375 : const int16_t write_flag, /* i : flag to actually write the data or not */
5376 : int16_t *indice_coherence,
5377 : const int16_t hrmasa_flag /* i : flag indicating high-rate MASA MD coding */
5378 : )
5379 : {
5380 : int16_t j, k;
5381 : float dct_coh[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
5382 : uint16_t idx_dct[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
5383 : int16_t coding_subbands;
5384 : int16_t nbits;
5385 : uint64_t no_cb;
5386 : int16_t MASA_grouping[MASA_MAXIMUM_CODING_SUBBANDS];
5387 : int16_t nbits1;
5388 : int16_t coding_subbands_0, d;
5389 : int16_t two_dir_band[MASA_MAXIMUM_CODING_SUBBANDS];
5390 : int16_t no_cb_vec[MASA_MAXIMUM_CODING_SUBBANDS];
5391 : IVAS_QDIRECTION *q_direction;
5392 : int16_t min_index;
5393 283714 : min_index = 0;
5394 283714 : q_direction = &( hQMetaData->q_direction[idx_d] );
5395 283714 : coding_subbands = q_direction->cfg.nbands;
5396 283714 : nbits = 0;
5397 :
5398 283714 : if ( all_coherence_zero == 1 )
5399 : {
5400 0 : return nbits;
5401 : }
5402 :
5403 283714 : if ( hQMetaData->q_direction[idx_d].cfg.nblocks == 1 )
5404 : {
5405 29622 : nbits = encode_spread_coherence_1sf( hQMetaData, idx_d, hMetaData, hrmasa_flag );
5406 :
5407 29622 : return nbits;
5408 : }
5409 : else
5410 : {
5411 254092 : k = 0;
5412 254092 : no_cb = 1;
5413 254092 : coding_subbands_0 = hQMetaData->q_direction[0].cfg.nbands;
5414 254092 : if ( coding_subbands_0 <= 5 )
5415 : {
5416 924480 : for ( j = 0; j < 5; j++ )
5417 : {
5418 770400 : MASA_grouping[j] = j;
5419 : }
5420 : }
5421 : else
5422 : {
5423 100012 : if ( coding_subbands_0 <= 8 )
5424 : {
5425 27300 : mvs2s( MASA_grouping_8_to_5, MASA_grouping, 8 );
5426 : }
5427 72712 : else if ( coding_subbands_0 <= 12 )
5428 : {
5429 34598 : mvs2s( MASA_grouping_12_to_5, MASA_grouping, 12 );
5430 : }
5431 38114 : else if ( coding_subbands_0 <= 18 )
5432 : {
5433 13072 : mvs2s( MASA_grouping_18_to_5, MASA_grouping, 18 );
5434 : }
5435 : else
5436 : {
5437 25042 : if ( coding_subbands_0 <= 24 )
5438 : {
5439 25042 : mvs2s( MASA_grouping_24_to_5, MASA_grouping, 24 );
5440 : }
5441 : }
5442 : }
5443 :
5444 254092 : if ( coding_subbands < coding_subbands_0 )
5445 : {
5446 65946 : d = 0;
5447 565140 : for ( j = 0; j < coding_subbands_0; j++ )
5448 : {
5449 499194 : if ( hQMetaData->twoDirBands[j] == 1 )
5450 : {
5451 211622 : two_dir_band[d++] = j;
5452 : }
5453 : }
5454 : }
5455 :
5456 2192440 : for ( j = 0; j < coding_subbands; j++ )
5457 : {
5458 : /* DCT transform */
5459 1938348 : dct4_transform( hQMetaData->q_direction[idx_d].coherence_band_data[j].spread_coherence, dct_coh[j] );
5460 :
5461 1938348 : if ( hrmasa_flag )
5462 : {
5463 0 : minimum_s( (int16_t *) ( q_direction->band_data[j].energy_ratio_index ), q_direction->cfg.nblocks, &min_index );
5464 0 : no_cb_vec[j] = len_cb_dct0_masa[min_index >> 1];
5465 : }
5466 : else
5467 : {
5468 1938348 : no_cb_vec[j] = len_cb_dct0_masa[q_direction->band_data[j].energy_ratio_index[0]];
5469 : }
5470 :
5471 1938348 : if ( write_flag )
5472 : {
5473 : /* quantize first DCT parameter */
5474 969174 : 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 );
5475 : }
5476 :
5477 1938348 : if ( coding_subbands < coding_subbands_0 )
5478 : {
5479 211622 : 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 );
5480 : }
5481 : else
5482 : {
5483 1726726 : 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 );
5484 : }
5485 1938348 : k++;
5486 :
5487 1938348 : dct_coh[j][2] = 0.0f;
5488 1938348 : dct_coh[j][3] = 0.0f;
5489 : }
5490 :
5491 254092 : nbits1 = 0;
5492 254092 : if ( sum_s( no_cb_vec, coding_subbands ) > MASA_COH_LIMIT_2IDX )
5493 : {
5494 : /* make two indxes */
5495 2780 : no_cb = 1;
5496 : #ifdef DEBUGGING
5497 : assert( coding_subbands % 2 == 0 );
5498 : #endif
5499 :
5500 36110 : for ( j = 0; j < coding_subbands / 2; j++ )
5501 : {
5502 33330 : no_cb *= no_cb_vec[j];
5503 : }
5504 :
5505 2780 : nbits = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
5506 2780 : no_cb = 1;
5507 :
5508 36110 : for ( j = coding_subbands / 2; j < coding_subbands; j++ )
5509 : {
5510 33330 : no_cb *= no_cb_vec[j];
5511 : }
5512 2780 : nbits1 = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
5513 : }
5514 : else
5515 : {
5516 251312 : no_cb = 1;
5517 :
5518 2123000 : for ( j = 0; j < coding_subbands; j++ )
5519 : {
5520 1871688 : no_cb *= no_cb_vec[j];
5521 : }
5522 :
5523 251312 : nbits = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
5524 : }
5525 :
5526 254092 : if ( write_flag )
5527 : {
5528 1096220 : for ( j = 0; j < coding_subbands; j++ )
5529 : {
5530 : /* inverse DCT transform */
5531 969174 : invdct4_transform( dct_coh[j], q_direction->coherence_band_data[j].spread_coherence );
5532 : }
5533 :
5534 127046 : nbits = encode_coherence_indexesDCT0( idx_dct, coding_subbands, no_cb_vec, hMetaData, *indice_coherence, nbits, nbits1 );
5535 : }
5536 : else
5537 : {
5538 : /* write dummy data now and save the position */
5539 127046 : *indice_coherence = hMetaData->nb_ind_tot;
5540 127046 : k = nbits;
5541 294271 : while ( k > 0 )
5542 : {
5543 167225 : push_next_indice( hMetaData, 0, min( 16, k ) );
5544 167225 : k -= 16;
5545 : }
5546 :
5547 127046 : if ( nbits1 > 0 )
5548 : {
5549 1390 : k = nbits1;
5550 4170 : while ( k > 0 )
5551 : {
5552 2780 : push_next_indice( hMetaData, 0, min( 16, k ) );
5553 2780 : k -= 16;
5554 : }
5555 : }
5556 127046 : nbits += nbits1;
5557 127046 : set_s( no_cb_vec, MASA_NO_CV_COH1, coding_subbands );
5558 127046 : nbits += encode_coherence_indexesDCT1( &idx_dct[coding_subbands], coding_subbands, hMetaData );
5559 :
5560 127046 : return nbits;
5561 : }
5562 : }
5563 :
5564 127046 : return nbits;
5565 : }
5566 :
5567 :
5568 : /*-------------------------------------------------------------------*
5569 : * ivas_qmetadata_reorder_2dir_bands()
5570 : *
5571 : * Reorders metadata on 2dir bands such that direct-to-total ratio of first direction is
5572 : * always larger or equal to direct-to-total ratio of second direction.
5573 : *-------------------------------------------------------------------*/
5574 :
5575 39448 : static void ivas_qmetadata_reorder_2dir_bands(
5576 : IVAS_QMETADATA_HANDLE hQMetaData )
5577 : {
5578 : int16_t nbands;
5579 : int16_t nsubframes;
5580 : int16_t band, sf;
5581 :
5582 39448 : nbands = hQMetaData->q_direction[0].cfg.nbands;
5583 39448 : nsubframes = hQMetaData->q_direction[0].cfg.nblocks;
5584 :
5585 386985 : for ( band = 0; band < nbands; band++ )
5586 : {
5587 347537 : if ( hQMetaData->twoDirBands[band] == 1 )
5588 : {
5589 133535 : if ( hQMetaData->q_direction[0].band_data[band].energy_ratio[0] < hQMetaData->q_direction[1].band_data[band].energy_ratio[0] )
5590 : {
5591 50603 : uint16_t uint16_tmp = 0;
5592 50603 : float flt_tmp = 0;
5593 50603 : uint8_t uint8_tmp = 0;
5594 :
5595 222226 : for ( sf = 0; sf < nsubframes; sf++ )
5596 : {
5597 171623 : uint16_tmp = hQMetaData->q_direction[0].band_data[band].spherical_index[sf];
5598 171623 : hQMetaData->q_direction[0].band_data[band].spherical_index[sf] = hQMetaData->q_direction[1].band_data[band].spherical_index[sf];
5599 171623 : hQMetaData->q_direction[1].band_data[band].spherical_index[sf] = uint16_tmp;
5600 :
5601 171623 : flt_tmp = hQMetaData->q_direction[0].band_data[band].azimuth[sf];
5602 171623 : hQMetaData->q_direction[0].band_data[band].azimuth[sf] = hQMetaData->q_direction[1].band_data[band].azimuth[sf];
5603 171623 : hQMetaData->q_direction[1].band_data[band].azimuth[sf] = flt_tmp;
5604 :
5605 171623 : flt_tmp = hQMetaData->q_direction[0].band_data[band].elevation[sf];
5606 171623 : hQMetaData->q_direction[0].band_data[band].elevation[sf] = hQMetaData->q_direction[1].band_data[band].elevation[sf];
5607 171623 : hQMetaData->q_direction[1].band_data[band].elevation[sf] = flt_tmp;
5608 :
5609 171623 : uint8_tmp = hQMetaData->q_direction[0].band_data[band].distance[sf];
5610 171623 : hQMetaData->q_direction[0].band_data[band].distance[sf] = hQMetaData->q_direction[1].band_data[band].distance[sf];
5611 171623 : hQMetaData->q_direction[1].band_data[band].distance[sf] = uint8_tmp;
5612 :
5613 171623 : if ( hQMetaData->coherence_flag )
5614 : {
5615 171623 : uint8_tmp = hQMetaData->q_direction[0].coherence_band_data[band].spread_coherence[sf];
5616 171623 : hQMetaData->q_direction[0].coherence_band_data[band].spread_coherence[sf] = hQMetaData->q_direction[1].coherence_band_data[band].spread_coherence[sf];
5617 171623 : hQMetaData->q_direction[1].coherence_band_data[band].spread_coherence[sf] = uint8_tmp;
5618 : }
5619 : }
5620 50603 : if ( hQMetaData->coherence_flag )
5621 : {
5622 50603 : flt_tmp = hQMetaData->q_direction[0].band_data[band].energy_ratio[0];
5623 50603 : hQMetaData->q_direction[0].band_data[band].energy_ratio[0] = hQMetaData->q_direction[1].band_data[band].energy_ratio[0];
5624 50603 : hQMetaData->q_direction[1].band_data[band].energy_ratio[0] = flt_tmp;
5625 : }
5626 : }
5627 : }
5628 : }
5629 :
5630 39448 : return;
5631 : }
5632 :
5633 :
5634 : /*-------------------------------------------------------------------*
5635 : * write_2dir_info()
5636 : *
5637 : *
5638 : *-------------------------------------------------------------------*/
5639 :
5640 87216 : static int16_t write_2dir_info(
5641 : BSTR_ENC_HANDLE hMetaData,
5642 : uint8_t *twoDirBands,
5643 : const int16_t n,
5644 : const int16_t k )
5645 : {
5646 : int16_t nbits;
5647 : int16_t p[MASA_MAXIMUM_CODING_SUBBANDS];
5648 : uint16_t dif_p[MASA_MAXIMUM_CODING_SUBBANDS];
5649 : int16_t i, j;
5650 :
5651 87216 : j = 0;
5652 87216 : p[0] = 0;
5653 975827 : for ( i = 0; i < n; i++ )
5654 : {
5655 888611 : if ( twoDirBands[i] == 1 )
5656 : {
5657 658022 : p[j] = i;
5658 658022 : j++;
5659 : }
5660 : }
5661 :
5662 87216 : dif_p[0] = p[0];
5663 658022 : for ( i = 1; i < j; i++ )
5664 : {
5665 570806 : dif_p[i] = p[i] - p[i - 1] - 1;
5666 : }
5667 :
5668 : #ifdef DEBUGGING
5669 : assert( k == j );
5670 : #endif
5671 87216 : j = hMetaData->nb_bits_tot;
5672 745238 : for ( i = 0; i < k; i++ )
5673 : {
5674 658022 : ivas_qmetadata_encode_extended_gr( hMetaData, dif_p[i], 24, 0 );
5675 : }
5676 87216 : nbits = hMetaData->nb_bits_tot - j;
5677 :
5678 87216 : return nbits;
5679 : }
5680 :
5681 :
5682 : /*-------------------------------------------------------------------*
5683 : * transform_azimuth_dir2()
5684 : *
5685 : *
5686 : *-------------------------------------------------------------------*/
5687 :
5688 85988 : static void transform_azimuth_dir2(
5689 : IVAS_QMETADATA_HANDLE hQMetaData,
5690 : int16_t *dir2_bands )
5691 : {
5692 : int16_t i, b;
5693 :
5694 731463 : for ( i = hQMetaData->q_direction[1].cfg.start_band; i < hQMetaData->q_direction[1].cfg.nbands; i++ )
5695 : {
5696 645475 : if ( hQMetaData->q_direction[0].band_data[dir2_bands[i]].energy_ratio_index[0] < 7 )
5697 : {
5698 : /* transform azimuth */
5699 2276898 : for ( b = 0; b < hQMetaData->q_direction[1].cfg.nblocks; b++ )
5700 : {
5701 1807134 : 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;
5702 1807134 : if ( hQMetaData->q_direction[1].band_data[i].azimuth[b] >= 180 )
5703 : {
5704 187752 : hQMetaData->q_direction[1].band_data[i].azimuth[b] -= 360;
5705 : }
5706 1807134 : if ( hQMetaData->q_direction[1].band_data[i].azimuth[b] < -180 )
5707 : {
5708 0 : hQMetaData->q_direction[1].band_data[i].azimuth[b] += 360;
5709 : }
5710 1807134 : if ( hQMetaData->q_direction[1].band_data[i].azimuth[b] >= 180 )
5711 : {
5712 0 : hQMetaData->q_direction[1].band_data[i].azimuth[b] -= 360;
5713 : }
5714 1807134 : if ( hQMetaData->q_direction[1].band_data[i].azimuth[b] < -180 )
5715 : {
5716 0 : hQMetaData->q_direction[1].band_data[i].azimuth[b] += 360;
5717 : }
5718 : #ifdef DEBUGGING
5719 : assert( hQMetaData->q_direction[1].band_data[i].azimuth[b] < 180 && hQMetaData->q_direction[1].band_data[i].azimuth[b] >= -180 );
5720 : #endif
5721 : }
5722 : }
5723 : }
5724 :
5725 85988 : return;
5726 : }
5727 :
5728 : #ifdef DEBUG_MODE_QMETADATA
5729 : /*-------------------------------------------------------------------------
5730 : * DEBUG function direction_distance()
5731 : *
5732 : *------------------------------------------------------------------------*/
5733 :
5734 : static float direction_distance(
5735 : float elevation[DIRAC_MAX_NBANDS][MAX_PARAM_SPATIAL_SUBFRAMES],
5736 : float azimuth[DIRAC_MAX_NBANDS][MAX_PARAM_SPATIAL_SUBFRAMES],
5737 : IVAS_QDIRECTION *q_direction,
5738 : const int16_t dim1,
5739 : const int16_t dim2,
5740 : float mat_dist[DIRAC_MAX_NBANDS][MAX_PARAM_SPATIAL_SUBFRAMES] )
5741 : {
5742 : float d;
5743 : int16_t i, j;
5744 :
5745 : d = 0;
5746 : for ( i = 0; i < dim1; i++ )
5747 : {
5748 : for ( j = 0; j < dim2; j++ )
5749 : {
5750 : mat_dist[i][j] = sinf( elevation[i][j] * PI_OVER_180 ) * sinf( q_direction->band_data[i].elevation[j] * PI_OVER_180 ) +
5751 : 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 );
5752 :
5753 : d += fabsf( mat_dist[i][j] );
5754 : }
5755 : }
5756 :
5757 : return d / (float) ( dim1 * dim2 );
5758 : }
5759 : #endif
5760 :
5761 :
5762 42831 : static int16_t divide_GR_orders(
5763 : const int16_t *q_idx,
5764 : const int16_t GR1,
5765 : const int16_t GR2,
5766 : const int16_t len,
5767 : const int16_t len_max_GR1,
5768 : int16_t *i_min )
5769 : {
5770 : int16_t nb_GR_min;
5771 : int16_t i, j, nb_GR;
5772 42831 : nb_GR_min = 1000;
5773 42831 : *i_min = -1;
5774 653124 : for ( i = 0; i < min( len_max_GR1, len ); i++ )
5775 : {
5776 610293 : nb_GR = 0;
5777 :
5778 5315691 : for ( j = 0; j <= i; j++ )
5779 : {
5780 4705398 : nb_GR += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR1 );
5781 : }
5782 6792678 : for ( j = i + 1; j < len; j++ )
5783 : {
5784 6182385 : nb_GR += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR2 );
5785 : }
5786 :
5787 610293 : if ( nb_GR < nb_GR_min )
5788 : {
5789 137390 : nb_GR_min = nb_GR;
5790 137390 : *i_min = i + 1;
5791 : }
5792 : }
5793 :
5794 42831 : return nb_GR_min;
5795 : }
5796 :
5797 :
5798 1981 : static int16_t find_optimal_GR_order(
5799 : const int16_t *q_idx,
5800 : const int16_t len,
5801 : int16_t *GR )
5802 : {
5803 : int16_t nb_GR_0, nb_GR_1;
5804 : int16_t i;
5805 : /* find optimum length of the part encoded with GR2 */
5806 1981 : nb_GR_0 = 0;
5807 1981 : nb_GR_1 = 0;
5808 9794 : for ( i = 0; i < len; i++ )
5809 : {
5810 7813 : nb_GR_0 += ivas_qmetadata_encode_extended_gr_length( q_idx[i], 100, 0 );
5811 7813 : nb_GR_1 += ivas_qmetadata_encode_extended_gr_length( q_idx[i], 100, 1 );
5812 : }
5813 :
5814 1981 : if ( nb_GR_0 < nb_GR_1 )
5815 : {
5816 1024 : *GR = 0;
5817 1024 : return nb_GR_0;
5818 : }
5819 : else
5820 : {
5821 957 : *GR = 1;
5822 :
5823 957 : return nb_GR_1;
5824 : }
5825 : }
5826 :
5827 :
5828 14277 : static int16_t find_optimal_GR_orders(
5829 : const int16_t *q_idx,
5830 : const int16_t len,
5831 : const int16_t len_max_GR1,
5832 : int16_t *GR1,
5833 : int16_t *GR2,
5834 : int16_t *i_min )
5835 : {
5836 : int16_t nb_GR_20, nb_GR_21, nb_GR_10, nb_GR_min;
5837 : int16_t i_min_20, i_min_21, i_min_10;
5838 : /* find optimum length of the part encoded with GR2 */
5839 14277 : nb_GR_20 = divide_GR_orders( q_idx, 2, 0, len, len_max_GR1, &i_min_20 );
5840 14277 : nb_GR_21 = divide_GR_orders( q_idx, 2, 1, len, len_max_GR1, &i_min_21 );
5841 14277 : nb_GR_10 = divide_GR_orders( q_idx, 1, 0, len, len_max_GR1, &i_min_10 );
5842 :
5843 14277 : if ( nb_GR_20 < nb_GR_21 && nb_GR_20 < nb_GR_10 )
5844 : {
5845 7560 : *GR1 = 2;
5846 7560 : *GR2 = 0;
5847 7560 : nb_GR_min = nb_GR_20;
5848 7560 : *i_min = i_min_20;
5849 : }
5850 : else
5851 : {
5852 6717 : if ( nb_GR_21 < nb_GR_20 && nb_GR_21 < nb_GR_10 )
5853 : {
5854 1454 : *GR1 = 2;
5855 1454 : *GR2 = 1;
5856 1454 : nb_GR_min = nb_GR_21;
5857 1454 : *i_min = i_min_21;
5858 : }
5859 : else
5860 : {
5861 5263 : *GR1 = 1;
5862 5263 : *GR2 = 0;
5863 5263 : nb_GR_min = nb_GR_10;
5864 5263 : *i_min = i_min_10;
5865 : }
5866 : }
5867 :
5868 14277 : return nb_GR_min;
5869 : }
5870 :
5871 :
5872 17504 : static int16_t write_stream_dct_coeffs_omasa(
5873 : int16_t *q_idx, /* i : array of indexes to be written */
5874 : const int16_t len_stream, /* i : array length */
5875 : BSTR_ENC_HANDLE hMetaData, /* i/o: metadata bitstream */
5876 : const int16_t first_line, /* i : is first line of the matrix? 1/0 */
5877 : const int16_t low_bitrate_mode /* i : is low bitrate mode? if yes, limit the number of bits written */
5878 : )
5879 : {
5880 17504 : int16_t nb_bits = 0, bits_pos;
5881 : uint16_t nb_GR_min;
5882 : int16_t i, j;
5883 : int16_t changed, update_needed;
5884 :
5885 : int16_t GR1, GR2, i_min;
5886 : int16_t max_bits;
5887 :
5888 17504 : bits_pos = hMetaData->nb_bits_tot;
5889 17504 : if ( low_bitrate_mode == 1 )
5890 : {
5891 1442 : max_bits = 50;
5892 : }
5893 : else
5894 : {
5895 16062 : max_bits = 1000;
5896 : }
5897 :
5898 : /* write DCT 0 component */
5899 : /* write sign only if not the very first DCT coeff */
5900 17504 : if ( first_line == 0 )
5901 : {
5902 0 : if ( q_idx[0] > 0 )
5903 : {
5904 0 : push_next_indice( hMetaData, 1, 1 );
5905 0 : push_next_indice( hMetaData, q_idx[0], BITS_MASA2TOTTAL_DCT0 );
5906 : }
5907 : else
5908 : {
5909 0 : push_next_indice( hMetaData, 0, 1 );
5910 0 : push_next_indice( hMetaData, -q_idx[0], BITS_MASA2TOTTAL_DCT0 );
5911 : }
5912 0 : nb_bits += BITS_MASA2TOTTAL_DCT0 + 1;
5913 : }
5914 : else
5915 : {
5916 17504 : push_next_indice( hMetaData, q_idx[0], BITS_MASA2TOTTAL_DCT0 );
5917 17504 : nb_bits += BITS_MASA2TOTTAL_DCT0;
5918 : }
5919 :
5920 17504 : if ( q_idx[0] != 0 )
5921 : {
5922 16258 : i_min = 1;
5923 16258 : GR2 = 0;
5924 16258 : if ( len_stream >= 8 )
5925 : {
5926 14277 : nb_GR_min = find_optimal_GR_orders( &q_idx[1], len_stream - 1, 15, &GR1, &GR2, &i_min );
5927 : }
5928 : else
5929 : {
5930 1981 : nb_GR_min = find_optimal_GR_order( &q_idx[1], len_stream - 1, &GR1 );
5931 : }
5932 :
5933 16258 : assert( nb_GR_min < 1000 );
5934 16258 : changed = 1;
5935 16258 : update_needed = 0;
5936 16258 : while ( len_stream >= 8 && nb_GR_min > max_bits && changed >= 1 )
5937 : {
5938 0 : update_needed = 1;
5939 0 : changed = 0;
5940 0 : for ( j = len_stream - 1; j > 6; j-- )
5941 : {
5942 0 : if ( q_idx[j] >= 2 )
5943 : {
5944 :
5945 0 : if ( j > i_min )
5946 : {
5947 0 : changed = 1;
5948 0 : nb_GR_min -= ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR2 );
5949 0 : q_idx[j] -= 2;
5950 0 : nb_GR_min += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR2 );
5951 : }
5952 : else
5953 : {
5954 0 : changed = 1;
5955 0 : nb_GR_min -= ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR1 );
5956 0 : q_idx[j] -= 2;
5957 0 : nb_GR_min += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, 0 );
5958 : }
5959 : }
5960 0 : else if ( q_idx[j] == 1 )
5961 : {
5962 0 : if ( j > i_min )
5963 : {
5964 0 : changed = 1;
5965 0 : nb_GR_min -= ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR2 );
5966 0 : q_idx[j] -= 1;
5967 :
5968 0 : nb_GR_min += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR2 );
5969 : }
5970 : else
5971 : {
5972 0 : changed = 1;
5973 0 : nb_GR_min -= ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR1 );
5974 0 : q_idx[j] -= 1;
5975 0 : nb_GR_min += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, 0 );
5976 : }
5977 : }
5978 0 : if ( nb_GR_min < max_bits )
5979 : {
5980 0 : break;
5981 : }
5982 : }
5983 : }
5984 :
5985 16258 : if ( update_needed == 1 )
5986 : {
5987 : /* re-calculate */
5988 : /* find optimum length of the part encoded with GR2 */
5989 0 : nb_GR_min = find_optimal_GR_orders( &q_idx[1], len_stream - 1, 15, &GR1, &GR2, &i_min );
5990 : }
5991 :
5992 16258 : if ( len_stream >= 8 )
5993 : {
5994 : /* write number of indexes encoded with GR2 on 4 bits */
5995 14277 : push_next_indice( hMetaData, i_min, 4 );
5996 14277 : nb_bits += 4;
5997 : /* write GR orders */
5998 14277 : push_next_indice( hMetaData, GR1 - 1, 1 );
5999 14277 : nb_bits += 1;
6000 14277 : if ( GR1 == 2 )
6001 : {
6002 9014 : push_next_indice( hMetaData, GR2, 1 );
6003 9014 : nb_bits += 1;
6004 : }
6005 :
6006 : /* write GR data */
6007 78428 : for ( i = 1; i <= i_min; i++ )
6008 : {
6009 64151 : ivas_qmetadata_encode_extended_gr( hMetaData, q_idx[i], 100, GR1 );
6010 : }
6011 :
6012 199941 : for ( i = i_min + 1; i < len_stream; i++ )
6013 : {
6014 185664 : ivas_qmetadata_encode_extended_gr( hMetaData, q_idx[i], 100, GR2 );
6015 : }
6016 : }
6017 : else
6018 : {
6019 : /* len_stream <= 8 */
6020 : /* write GR order */
6021 1981 : push_next_indice( hMetaData, GR1, 1 );
6022 1981 : nb_bits += 1;
6023 9794 : for ( i = 1; i < len_stream; i++ )
6024 : {
6025 7813 : ivas_qmetadata_encode_extended_gr( hMetaData, q_idx[i], 100, GR1 );
6026 : }
6027 : }
6028 :
6029 16258 : nb_bits += nb_GR_min;
6030 :
6031 16258 : assert( nb_bits == ( hMetaData->nb_bits_tot - bits_pos ) );
6032 : }
6033 :
6034 17504 : return nb_bits;
6035 : }
6036 :
6037 :
6038 : /*-------------------------------------------------------------------------
6039 : * ivas_omasa_encode_masa_to_total()
6040 : *
6041 : *------------------------------------------------------------------------*/
6042 :
6043 17504 : void ivas_omasa_encode_masa_to_total(
6044 : float masa_to_total_energy_ratio[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS],
6045 : BSTR_ENC_HANDLE hMetaData,
6046 : const int16_t low_bitrate_mode,
6047 : const int16_t nbands,
6048 : const int16_t nblocks )
6049 : {
6050 : int16_t i, j, k;
6051 : float data[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6052 : float q_dct_data[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6053 17504 : float step = STEP_M2T;
6054 :
6055 : int16_t q_idx[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6056 : float dct_data_tmp[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6057 : float dct_data[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6058 :
6059 : int16_t bits_pos, nb_bits;
6060 : int16_t n_streams, len_stream;
6061 :
6062 : Word32 q_dct_data_fx[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS],
6063 : dct_data_tmp_fx[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6064 :
6065 : int64_t step_fx;
6066 17504 : step_fx = STEP_M2T_FX;
6067 : #ifdef DEBUG_MODE_QMETADATA
6068 : static FILE *pF = NULL;
6069 : static FILE *pF_ratio = NULL;
6070 :
6071 : if ( pF == NULL )
6072 : pF = fopen( "./res/qmetadata_ism_qidx__enc.txt", "w" );
6073 : if ( pF_ratio == NULL )
6074 : pF_ratio = fopen( "./res/qmetadata_masa2tot_enc.txt", "w" );
6075 :
6076 : #endif
6077 :
6078 17504 : bits_pos = hMetaData->nb_bits_tot;
6079 17504 : k = 0;
6080 125170 : for ( i = 0; i < nbands; i++ )
6081 : {
6082 402388 : for ( j = 0; j < nblocks; j++ )
6083 : {
6084 294722 : data[k] = masa_to_total_energy_ratio[j][i];
6085 294722 : k++;
6086 : }
6087 : }
6088 :
6089 : /* DCT2 transform */
6090 17504 : n_streams = 1;
6091 17504 : len_stream = nbands * nblocks;
6092 17504 : switch ( len_stream )
6093 : {
6094 112 : case 4:
6095 112 : matrix_product( dct4, nblocks, nblocks, 0, data, 1, nblocks, 1, dct_data );
6096 112 : n_streams = 1;
6097 112 : len_stream = 4;
6098 112 : break;
6099 2002 : case 5:
6100 2002 : matrix_product( dct5, nbands, nbands, 0, data, 1, nbands, 1, dct_data );
6101 2002 : n_streams = 1;
6102 2002 : len_stream = nbands;
6103 2002 : break;
6104 0 : case 8:
6105 0 : matrix_product( dct8, nbands, nbands, 0, data, 1, nbands, 1, dct_data );
6106 0 : n_streams = 1;
6107 0 : len_stream = nbands;
6108 0 : break;
6109 2942 : case 12:
6110 2942 : matrix_product( dct12, nbands, nbands, 0, data, 1, nbands, 1, dct_data );
6111 2942 : n_streams = 1;
6112 2942 : len_stream = nbands;
6113 2942 : break;
6114 12448 : case 20:
6115 12448 : matrix_product( dct5, nbands, nbands, 0, data, nblocks, nbands, 1, dct_data_tmp );
6116 12448 : matrix_product( dct_data_tmp, nbands, nblocks, 0, dct4, nblocks, nblocks, 1, dct_data );
6117 12448 : n_streams = 1;
6118 12448 : len_stream = nbands * nblocks;
6119 12448 : break;
6120 0 : case 32:
6121 0 : matrix_product( dct8, nbands, nbands, 0, data, nblocks, nbands, 1, dct_data_tmp );
6122 0 : matrix_product( dct_data_tmp, nbands, nblocks, 0, dct4, nblocks, nblocks, 1, dct_data );
6123 0 : n_streams = nblocks;
6124 0 : len_stream = nbands;
6125 0 : break;
6126 0 : default:
6127 0 : printf( "Incorrect number of coefficients for OMASA.\n" );
6128 0 : break;
6129 : }
6130 :
6131 35008 : for ( k = 0; k < n_streams; k++ )
6132 : {
6133 17504 : j = k * len_stream;
6134 : /* quantize with fixed common step */
6135 17504 : q_idx[j] = (int16_t) rintf( dct_data[j] / step );
6136 :
6137 17504 : if ( q_idx[j] > ( ( 1 << BITS_MASA2TOTTAL_DCT0 ) - 1 ) ) /* limit DCT0 to BITS_MASA2TOTTAL_DCT0 bit representation */
6138 : {
6139 0 : q_idx[j] = ( ( 1 << BITS_MASA2TOTTAL_DCT0 ) - 1 );
6140 : }
6141 :
6142 17504 : q_dct_data[j] = step * q_idx[j];
6143 :
6144 17504 : if ( q_idx[j] == 0 )
6145 : {
6146 1246 : set_s( &q_idx[j], 0, len_stream );
6147 1246 : set_zero( &q_dct_data[j], len_stream );
6148 : }
6149 : else
6150 : {
6151 273886 : for ( i = 1; i < len_stream; i++ )
6152 : {
6153 257628 : q_idx[j + i] = (int16_t) rintf( dct_data[j + i] / step );
6154 257628 : q_dct_data[j + i] = step * q_idx[j + i];
6155 257628 : if ( q_idx[j + i] <= 0 )
6156 : {
6157 186768 : q_idx[j + i] = -2 * q_idx[j + i];
6158 : }
6159 : else
6160 : {
6161 70860 : q_idx[j + i] = 2 * q_idx[j + i] - 1;
6162 : }
6163 : }
6164 : }
6165 : }
6166 :
6167 : /* write data */
6168 17504 : nb_bits = 0;
6169 35008 : for ( i = 0; i < n_streams; i++ )
6170 : {
6171 17504 : nb_bits += write_stream_dct_coeffs_omasa( &q_idx[i * len_stream], len_stream, hMetaData, ( i == 0 ), low_bitrate_mode );
6172 : }
6173 :
6174 : /* reconstruct masa2total */
6175 17504 : q_dct_data_fx[0] = (Word32) ( ( step_fx * q_idx[0] ) >> 6 ); // Q25
6176 294722 : for ( i = 1; i < len_stream; i++ )
6177 : {
6178 277218 : if ( ( q_idx[i] & 1 ) == 0 )
6179 : {
6180 206358 : q_dct_data_fx[i] = (Word32) ( ( step_fx * ( -q_idx[i] ) ) >> 7 ); // Q25
6181 : }
6182 : ELSE
6183 : {
6184 70860 : q_dct_data_fx[i] = (Word32) ( ( step_fx * ( q_idx[i] + 1 ) ) >> 7 ); // Q25
6185 : }
6186 : }
6187 17504 : SWITCH( len_stream )
6188 : {
6189 112 : case 4:
6190 112 : matrix_product_q30_fx( dct4_fx, nblocks, nblocks, 1, q_dct_data_fx, nblocks, 1, 0, dct_data_tmp_fx );
6191 112 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nblocks ); /*Q30*/
6192 112 : BREAK;
6193 2002 : case 5:
6194 2002 : matrix_product_q30_fx( dct5_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
6195 2002 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
6196 2002 : BREAK;
6197 0 : case 8:
6198 0 : matrix_product_q30_fx( dct8_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
6199 0 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
6200 0 : BREAK;
6201 2942 : case 12:
6202 2942 : matrix_product_q30_fx( dct12_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
6203 2942 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
6204 2942 : BREAK;
6205 12448 : case 20:
6206 12448 : matrix_product_fx( dct5_fx, nbands, nbands, 1, q_dct_data_fx, nbands, nblocks, 0, dct_data_tmp_fx );
6207 12448 : matrix_product_q30_fx( dct_data_tmp_fx, nbands, nblocks, 0, dct4_fx, nblocks, nblocks, 0, q_dct_data_fx ); /* reuse of variable*/
6208 12448 : BREAK;
6209 0 : case 32:
6210 0 : matrix_product_fx( dct8_fx, nbands, nbands, 1, q_dct_data_fx, nbands, nblocks, 0, dct_data_tmp_fx );
6211 0 : matrix_product_q30_fx( dct_data_tmp_fx, nbands, nblocks, 0, dct4_fx, nblocks, nblocks, 0, q_dct_data_fx );
6212 0 : BREAK;
6213 0 : default:
6214 0 : printf( "Incorrect number of coefficients for OMASA.\n" );
6215 0 : BREAK;
6216 : }
6217 :
6218 : /* this is to make sure the comparison to the threshold 0.98 will go the same way in */
6219 : /* fixed point and floating point without having to drag the fixed point values to the */
6220 : /* comparison place in the code; */
6221 : /* 1052266987 is 0.98 in Q30 it is not needed in the fixed point */
6222 312226 : for ( i = 0; i < nblocks * nbands; i++ )
6223 : {
6224 294722 : if ( q_dct_data_fx[i] >= 1052266987 && q_dct_data_fx[i] < 1052400000 )
6225 : {
6226 252 : q_dct_data_fx[i] = 1052400000;
6227 : }
6228 : }
6229 :
6230 17504 : k = 0;
6231 72688 : for ( i = 0; i < nblocks; i++ )
6232 : {
6233 349906 : for ( j = 0; j < nbands; j++ )
6234 : {
6235 294722 : masa_to_total_energy_ratio[i][j] = q_dct_data_fx[k] / (float) ( 1 << 30 );
6236 294722 : masa_to_total_energy_ratio[i][j] = max( 0.0f, masa_to_total_energy_ratio[i][j] );
6237 294722 : masa_to_total_energy_ratio[i][j] = min( 1.0f, masa_to_total_energy_ratio[i][j] );
6238 294722 : k++;
6239 : }
6240 : }
6241 :
6242 17504 : if ( nblocks == 1 )
6243 : {
6244 19776 : for ( i = 1; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
6245 : {
6246 150774 : for ( j = 0; j < nbands; j++ )
6247 : {
6248 135942 : masa_to_total_energy_ratio[i][j] = masa_to_total_energy_ratio[0][j];
6249 : }
6250 : }
6251 : }
6252 :
6253 17504 : if ( nbands == 1 )
6254 : {
6255 560 : for ( j = 1; j < 5; j++ )
6256 : {
6257 2240 : for ( i = 0; i < nblocks; i++ )
6258 : {
6259 1792 : masa_to_total_energy_ratio[i][j] = masa_to_total_energy_ratio[i][0];
6260 : }
6261 : }
6262 : }
6263 :
6264 17504 : assert( nb_bits == ( hMetaData->nb_bits_tot - bits_pos ) );
6265 :
6266 : #ifdef DEBUG_MODE_QMETADATA
6267 : {
6268 :
6269 : fprintf( pF, "frame %d: ", frame );
6270 : fprintf( pF_ratio, "frame %d: ", frame );
6271 :
6272 :
6273 : /* direction_distance( elevation_orig, azimuth_orig, q_direction, nbands, nblocks, mat_dist );*/
6274 : for ( i = 0; i < nbands; i++ )
6275 : {
6276 : for ( j = 0; j < 4; j++ )
6277 : {
6278 : fprintf( pF_ratio, " %5.2f ", hQMetaData->masa_to_total_energy_ratio[j][i] );
6279 : }
6280 : }
6281 : for ( i = 0; i < 20; i++ )
6282 : {
6283 : fprintf( pF, " %4d ", q_idx[i] );
6284 : }
6285 : fprintf( pF, "\n" );
6286 : fprintf( pF_ratio, "\n" );
6287 : }
6288 : #endif
6289 :
6290 17504 : return;
6291 : }
|