Line data Source code
1 : /******************************************************************************************************
2 :
3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
7 : contributors to this repository. All Rights Reserved.
8 :
9 : This software is protected by copyright law and by international treaties.
10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
14 : contributors to this repository retain full ownership rights in their respective contributions in
15 : the software. This notice grants no license of any kind, including but not limited to patent
16 : license, nor is any license granted by implication, estoppel or otherwise.
17 :
18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
19 : contributions.
20 :
21 : This software is provided "AS IS", without any express or implied warranties. The software is in the
22 : development stage. It is intended exclusively for experts who have experience with such software and
23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
24 : and fitness for a particular purpose are hereby disclaimed and excluded.
25 :
26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be
27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
29 : the United Nations Convention on Contracts on the International Sales of Goods.
30 :
31 : *******************************************************************************************************/
32 :
33 : #include <assert.h>
34 : #include <stdint.h>
35 : #include "options.h"
36 : #include <math.h>
37 : #include "ivas_cnst.h"
38 : #include "prot.h"
39 : #include "ivas_prot.h"
40 : #include "ivas_rom_com.h"
41 : #include "ivas_stat_enc.h"
42 : #include "wmc_auto.h"
43 : #include "prot.h"
44 : #include "basop_settings.h"
45 : #if defined( DEBUGGING ) && defined( DBG_BITSTREAM_ANALYSIS )
46 : #include <string.h>
47 : #endif
48 :
49 :
50 : /*-----------------------------------------------------------------------*
51 : * Local function prototypes
52 : *-----------------------------------------------------------------------*/
53 :
54 : #ifdef DEBUG_MODE_QMETADATA
55 : static float direction_distance( float elevation[DIRAC_MAX_NBANDS][MAX_PARAM_SPATIAL_SUBFRAMES], float azimuth[DIRAC_MAX_NBANDS][MAX_PARAM_SPATIAL_SUBFRAMES], IVAS_QDIRECTION *q_direction, const int16_t dim1, const int16_t dim2, float mat_dist[DIRAC_MAX_NBANDS][MAX_PARAM_SPATIAL_SUBFRAMES] );
56 :
57 : #endif
58 : static void ivas_qmetadata_quantize_diffuseness_nrg_ratios( IVAS_QMETADATA_HANDLE hQMetaData, int16_t *needed_bits, int16_t *nbits_diff, int16_t *dfRatioBits, const int16_t hodirac_flag );
59 :
60 : static int16_t ivas_qmetadata_entropy_encode_diffuseness( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, uint16_t *diffuseness_index_max_ec_frame );
61 :
62 : static void ivas_qmetadata_reorder_2dir_bands( IVAS_QMETADATA_HANDLE hQMetaData );
63 :
64 : static int16_t ivas_qmetadata_entropy_encode_df_ratio( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, int16_t *df_ratio_bits );
65 :
66 : static int16_t ivas_qmetadata_entropy_encode_dir( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, const uint16_t diffuseness_index_max_ec_frame, const int16_t nbands, const int16_t start_band, const int16_t direction_bits_raw, int16_t max_bits, const int16_t hrmasa_flag );
67 :
68 : static int16_t ivas_qmetadata_raw_encode_dir( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, const int16_t nbands, const int16_t start_band );
69 :
70 : int16_t ivas_qmetadata_encode_extended_gr_length( const uint16_t value, const uint16_t alphabet_size, const int16_t gr_param );
71 :
72 : static int16_t ivas_qmetadata_get_optimal_gr_param( uint16_t *unsigned_data, const int16_t count, const int16_t gr_param_count, int16_t *opt_gr_size );
73 :
74 : static int16_t ivas_qmetadata_encode_quasi_uniform_length( const uint16_t value, const uint16_t alphabet_size );
75 :
76 : static void ivas_qmetadata_encode_quasi_uniform( BSTR_ENC_HANDLE hMetaData, const uint16_t value, const uint16_t alphabet_size );
77 :
78 : static int16_t ivas_qmetadata_reorder_elevation_index( const int16_t elevation_index, const int16_t avg_elevation_index, const int16_t elevation_alphabet );
79 :
80 : static int16_t ivas_qmetadata_reorder_azimuth_index( const int16_t azimuth_index, const int16_t avg_azimuth_index, const int16_t azimuth_alphabet );
81 :
82 : static ivas_error requantize_direction_EC_3( int16_t *extra_bits, IVAS_QDIRECTION *q_direction, const int16_t coding_subbands, BSTR_ENC_HANDLE hMetaData, float elevation_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES], float azimuth_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES], int16_t *ind_order );
83 :
84 : static void calculate_two_distances( float *el, int16_t *bits, const int16_t total_bits, const int16_t len, float *p_d1, float *p_d2 );
85 :
86 : static void joint_encoding( IVAS_QDIRECTION *q_direction, const int16_t j, const int16_t next_j, const int16_t coding_subbands, int16_t *bits_dir0, const int16_t allowed_bits, BSTR_ENC_HANDLE hMetaData, int16_t *diff );
87 :
88 : static ivas_error write_ec_direction( int16_t *num_bits_written, BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, const int16_t j_idx, const int16_t len, const int16_t GR_ord_elevation, const int16_t GR_ord_azimuth, const int16_t use_context, const int16_t same );
89 :
90 : static int16_t write_fixed_rate_direction( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, const int16_t j_idx, const int16_t len );
91 :
92 : static int16_t ivas_qmetadata_quantize_coherence( IVAS_QMETADATA *hQMetaData, const int16_t idx_d, const int16_t all_coherence_zero, BSTR_ENC_HANDLE hMetaData, const int16_t write_flag, int16_t *indice_coherence, const int16_t hrmasa_flag );
93 :
94 : static void dct4_transform( uint8_t *v, float *dct_v );
95 :
96 : static float quantize_DCT_0_coh( const float x, const int16_t j, const float *coherence_cb, const float delta_var, const int16_t no_cb, IVAS_QDIRECTION *q_direction, uint16_t *idx_x, int16_t *p_no_cb, const int16_t hrmasa_flag );
97 :
98 : static int16_t encode_coherence_indexesDCT0( uint16_t *idx_dct, const int16_t len, int16_t *no_cb_vec, BSTR_ENC_HANDLE hMetaData, const int16_t indice_coherence, const int16_t nbits, const int16_t nbits1 );
99 :
100 : static int16_t encode_coherence_indexesDCT1( uint16_t *idx_dct, const int16_t len, BSTR_ENC_HANDLE hMetaData );
101 :
102 : static uint64_t create_combined_index( uint16_t *idx_dct, const int16_t len, const int16_t *no_cb_vec );
103 :
104 : static int16_t encode_surround_coherence( IVAS_QMETADATA *hQMetaData, BSTR_ENC_HANDLE hMetaData );
105 :
106 : static int16_t write_fixed_rate_direction( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, const int16_t j_idx, const int16_t len );
107 :
108 : static int16_t common_direction( IVAS_QDIRECTION *q_direction, const int16_t band_idx, const int16_t len, const int16_t bits_allowed, BSTR_ENC_HANDLE hMetaData, float *elevation_orig, float *azimuth_orig );
109 :
110 : static int16_t ivas_diffuseness_huff_ec_encode( BSTR_ENC_HANDLE hMetaData, const uint16_t idx );
111 :
112 : static void ivas_diffuseness_huff_ec_prepare( IVAS_QDIRECTION *q_direction, int16_t *best_av, uint16_t *avr_idx, int16_t *diffuseness_bits_huff );
113 :
114 : static int16_t coherence_coding_length( const uint16_t *idx_sur_coh_shift, const uint8_t idx_shift_len, const int16_t coding_subbands, const int16_t *no_cv, uint16_t *mr_idx, int16_t *no_cv_shift, int16_t *p_min_idx, int16_t *GR_ord, int16_t *nbits_fr, int16_t *nbits_fr1 );
115 :
116 : static int16_t write_2dir_info( BSTR_ENC_HANDLE hMetaData, uint8_t *twoDirBands, const int16_t n, const int16_t k );
117 :
118 : static void transform_azimuth_dir2( IVAS_QMETADATA_HANDLE hQMetaData, int16_t *dir2_bands );
119 :
120 : static int16_t calc_var_azi( const IVAS_QDIRECTION *q_direction, const int16_t diffuseness_index_max_ec_frame, const float avg_azimuth, float *avg_azimuth_out );
121 :
122 : static void ivas_qmetadata_quantize_diffuseness_nrg_ratios_hr_512( IVAS_QMETADATA_HANDLE hQMetaData, int16_t *needed_bits, const int16_t bits_dir_hr, BSTR_ENC_HANDLE hMetaData );
123 :
124 : static int16_t encode_surround_coherence_hr( IVAS_QMETADATA *hQMetaData, BSTR_ENC_HANDLE hMetaData );
125 :
126 : static int16_t ivas_qmetadata_quantize_coherence_hr_512( IVAS_QMETADATA *hQMetaData, const int16_t idx_d, const int16_t all_coherence_zero, BSTR_ENC_HANDLE hMetaData, const int16_t bits_coh );
127 :
128 : static int16_t write_stream_dct_coeffs_omasa( int16_t *q_idx, const int16_t len_stream, BSTR_ENC_HANDLE hMetaData, const int16_t first_line, const int16_t low_bitrate_mode );
129 :
130 : static int16_t find_optimal_GR_order( const int16_t *q_idx, const int16_t len, int16_t *GR );
131 :
132 : static int16_t find_optimal_GR_orders( const int16_t *q_idx, const int16_t len, const int16_t len_max_GR1, int16_t *GR1, int16_t *GR2, int16_t *i_min );
133 :
134 :
135 : /*-----------------------------------------------------------------------*
136 : * ivas_qmetadata_enc_encode()
137 : *
138 : * Main function for quantizing and coding Spatial Metadata
139 : *-----------------------------------------------------------------------*/
140 :
141 3500022 : 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 3500022 : 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 3500022 : bit_pos_0 = hMetaData->nb_bits_tot;
206 3500022 : bit_pos_start = bit_pos_0;
207 :
208 3500022 : ndirections = hQMetaData->no_directions;
209 3500022 : extra_bits = 0;
210 :
211 : /* Check if coherence should be encoded */
212 3500022 : all_coherence_zero = 1;
213 3500022 : bits_no_dirs_coh = 0;
214 :
215 3500022 : if ( hQMetaData->coherence_flag )
216 : {
217 608747 : all_coherence_zero = hQMetaData->all_coherence_zero;
218 :
219 608747 : push_next_indice( hMetaData, all_coherence_zero, 1 ); /* signal coherence */
220 608747 : bits_no_dirs_coh += 1;
221 : }
222 :
223 3500022 : if ( ndirections > 1 )
224 : {
225 : #ifdef DEBUGGING
226 : assert( ndirections == 2 );
227 : #endif
228 : /* Reorder 2dir bands for more efficient encoding. */
229 294624 : if ( !hodirac_flag )
230 : {
231 142591 : ivas_qmetadata_reorder_2dir_bands( hQMetaData );
232 : }
233 294624 : d = 0;
234 3473799 : for ( i = hQMetaData->q_direction[1].cfg.start_band; i < hQMetaData->q_direction[1].cfg.nbands; i++ )
235 : {
236 3179175 : if ( hQMetaData->twoDirBands[i] == 1 )
237 : {
238 2159918 : mvr2r( hQMetaData->q_direction[1].band_data[i].azimuth, hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].cfg.nblocks );
239 2159918 : mvr2r( hQMetaData->q_direction[1].band_data[i].elevation, hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].cfg.nblocks );
240 2159918 : 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 2159918 : dir2_bands[d] = i;
243 :
244 2159918 : if ( hQMetaData->coherence_flag )
245 : {
246 487555 : 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 2159918 : d++;
249 : }
250 : }
251 :
252 294624 : bits_no_dirs_coh += write_2dir_info( hMetaData, hQMetaData->twoDirBands, hQMetaData->q_direction[0].cfg.nbands, hQMetaData->numTwoDirBands );
253 :
254 1313881 : for ( i = d; i < hQMetaData->q_direction[0].cfg.nbands; i++ )
255 : {
256 1019257 : 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 294624 : hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands;
263 : }
264 :
265 : /*Quantization of the Diffuseness */
266 3500022 : ivas_qmetadata_quantize_diffuseness_nrg_ratios( hQMetaData, bits_dir_raw_pre, bits_diff, dfRatio_bits, hodirac_flag );
267 :
268 3500022 : bits_diff_sum = 0;
269 3500022 : bits_diff[0] = ivas_qmetadata_entropy_encode_diffuseness( hMetaData, &( hQMetaData->q_direction[0] ), &diffuseness_index_max_ec_frame_pre[0] );
270 3500022 : bits_diff_sum += bits_diff[0];
271 :
272 3500022 : if ( ndirections == 2 )
273 : {
274 294624 : bits_diff[1] = ivas_qmetadata_entropy_encode_df_ratio( hMetaData, &( hQMetaData->q_direction[1] ), dfRatio_bits );
275 294624 : 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 3500022 : if ( hQMetaData->no_directions == 2 )
282 : {
283 : int16_t j, k, dir2band, index_dirRatio1Inv, index_dirRatio2Inv;
284 :
285 294624 : dir2band = 0;
286 3473799 : for ( j = hQMetaData->q_direction[0].cfg.start_band; j < hQMetaData->q_direction[0].cfg.nbands; ++j )
287 : {
288 3179175 : if ( hQMetaData->twoDirBands[j] == 1 )
289 : {
290 2159918 : index_dirRatio1Inv = masa_sq( 1.0f - hQMetaData->q_direction[0].band_data[j].energy_ratio[0], diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
291 2159918 : index_dirRatio2Inv = masa_sq( 1.0f - hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[0], diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
292 :
293 10112479 : for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ )
294 : {
295 7952561 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index[k] = index_dirRatio1Inv;
296 : }
297 :
298 10112479 : for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ )
299 : {
300 7952561 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[k] = index_dirRatio2Inv;
301 : }
302 :
303 2159918 : dir2band++;
304 : }
305 : }
306 : }
307 :
308 : /* Encode surround coherence */
309 3500022 : if ( ndirections == 2 )
310 : {
311 294624 : 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 294624 : 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 136009 : bits_surround_coh = encode_surround_coherence( hQMetaData, hMetaData );
315 : }
316 : else
317 : {
318 158615 : bits_surround_coh = 0;
319 1920934 : for ( i = 0; i < hQMetaData->q_direction[0].cfg.nbands; i++ )
320 : {
321 1762319 : if ( hQMetaData->surcoh_band_data != NULL )
322 : {
323 89956 : set_c( (int8_t *) hQMetaData->surcoh_band_data[i].surround_coherence, 0, hQMetaData->q_direction[0].cfg.nblocks );
324 : }
325 : }
326 : }
327 294624 : bits_no_dirs_coh += bits_surround_coh;
328 294624 : 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 3205398 : no_TF = hQMetaData->q_direction[0].cfg.nbands * hQMetaData->q_direction[0].cfg.nblocks;
333 3205398 : 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 413536 : bits_surround_coh = encode_surround_coherence( hQMetaData, hMetaData );
336 : }
337 : else
338 : {
339 2791862 : bits_surround_coh = 0;
340 12136133 : for ( i = 0; i < hQMetaData->q_direction[0].cfg.nbands; i++ )
341 : {
342 9344271 : if ( hQMetaData->surcoh_band_data != NULL )
343 : {
344 530767 : set_c( (int8_t *) hQMetaData->surcoh_band_data[i].surround_coherence, 0, hQMetaData->q_direction[0].cfg.nblocks );
345 : }
346 : }
347 : }
348 3205398 : total_bits_1dir = hQMetaData->metadata_max_bits - bits_no_dirs_coh - bits_surround_coh;
349 : }
350 :
351 : /* Loop over number of directions*/
352 7294668 : for ( d = 0; d < ndirections; d++ )
353 : {
354 3794646 : q_direction = &( hQMetaData->q_direction[d] );
355 :
356 3794646 : if ( d == 1 )
357 : {
358 294624 : transform_azimuth_dir2( hQMetaData, dir2_bands );
359 : }
360 :
361 3794646 : nbands = q_direction->cfg.nbands;
362 3794646 : nblocks = q_direction->cfg.nblocks;
363 3794646 : start_band = q_direction->cfg.start_band;
364 3794646 : diffuseness_index_max_ec_frame = diffuseness_index_max_ec_frame_pre[0];
365 3794646 : bits_dir_raw = bits_dir_raw_pre[d];
366 :
367 : /* This sets bit budget correctly for the second direction */
368 3794646 : if ( d == 0 )
369 : {
370 3500022 : bits_diff[d] = bits_diff_sum;
371 : }
372 : else
373 : {
374 294624 : 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 3794646 : bits_signaling[d] = 0;
398 :
399 : /*Coherence */
400 3794646 : bits_coherence[d] = 0;
401 3794646 : bit_pos_start_coh = hMetaData->nb_bits_tot;
402 :
403 3794646 : if ( all_coherence_zero == 0 )
404 : {
405 696920 : bits_coherence[d] = ivas_qmetadata_quantize_coherence( hQMetaData, d, all_coherence_zero, hMetaData, 0, &indice_coherence, 0 );
406 : }
407 :
408 3794646 : 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 98776 : q_direction->not_in_2D = 0;
411 : /* Quantize directions*/
412 98776 : quantize_direction_frame2D( q_direction, azimuth_orig, elevation_orig );
413 : }
414 : else
415 : {
416 : /* Quantize directions*/
417 3695870 : quantize_direction_frame( q_direction, azimuth_orig, elevation_orig, 0 );
418 : }
419 :
420 : /* Signalling 2D*/
421 3794646 : push_next_indice( hMetaData, ( q_direction->not_in_2D > 0 ), 1 ); /*2D flag*/
422 3794646 : 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 3794646 : bit_pos_start = hMetaData->nb_bits_tot;
426 3794646 : next_ind_start = hMetaData->nb_ind_tot;
427 :
428 : /* Encode quantized directions with EC frame-wise*/
429 3794646 : if ( total_bits_1dir + bits_surround_coh <= hQMetaData->qmetadata_max_bit_req )
430 : {
431 2926449 : push_next_indice( hMetaData, 0, 1 ); /*Write 1 bit to signal EC frame-wise (EC1)*/
432 2926449 : bits_signaling[d]++;
433 : }
434 :
435 3794646 : next_ind_raw_flag = hMetaData->nb_ind_tot;
436 3794646 : push_next_indice( hMetaData, 0, 1 ); /* Raw coding flag*/
437 :
438 3794646 : bits_dir_bands[0] = ivas_qmetadata_raw_encode_dir( NULL, q_direction, q_direction->cfg.nbands, q_direction->cfg.start_band );
439 :
440 3794646 : reduce_bits = hQMetaData->is_masa_ivas_format ? ( total_bits_1dir - ( bits_diff[d] + bits_coherence[d] + bits_signaling[d] ) - 1 ) : MASA_MAX_BITS;
441 3794646 : 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 3794646 : if ( bits_ec < 0 )
444 : {
445 1519582 : hMetaData->ind_list[next_ind_raw_flag].value = 1; /*rewrite flag*/
446 1519582 : 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 3794646 : 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 3794646 : 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 3794646 : 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 118518 : restore_metadata_buffer( hMetaData, next_ind_start, bit_pos_start );
467 :
468 : /* Write signaling */
469 118518 : push_next_indice( hMetaData, 1, 1 ); /*Write 1 bit to signal no EC frame-wise (EC1)*/
470 118518 : push_next_indice( hMetaData, 0, 1 ); /*Write 1 bit to signal EC band-wise (EC2)*/
471 118518 : bits_signaling[d] = 3;
472 :
473 : /* Write raw flags */
474 118518 : next_ind_raw_flag = hMetaData->nb_ind_tot;
475 662382 : for ( i = start_band; i < nbands; i++ )
476 : {
477 543864 : push_next_indice( hMetaData, 0, 1 ); /* Raw coding flag*/
478 : }
479 :
480 118518 : bits_dir[d] = 0;
481 118518 : diff_bits = bits_diff[d] + bits_coherence[d] + bits_signaling[d] - total_bits_1dir;
482 662382 : for ( i = start_band; i < nbands; i++ )
483 : {
484 543864 : bits_dir_bands[i] = ivas_qmetadata_raw_encode_dir( NULL, q_direction, i + 1, i );
485 :
486 : /* Write ec bits */
487 543864 : 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 543864 : if ( bits_ec >= 0 )
490 : {
491 268845 : bits_dir_bands[i] = bits_ec;
492 268845 : raw_flag[i] = 0;
493 : }
494 : else
495 : {
496 275019 : raw_flag[i] = 1;
497 : }
498 543864 : diff_bits += bits_dir_bands[i] + 1;
499 : }
500 :
501 118518 : small_requantize_direction_frame( q_direction, azimuth_orig, elevation_orig, raw_flag, bits_dir_bands, &diff_bits );
502 :
503 662382 : for ( i = start_band; i < nbands; i++ )
504 : {
505 543864 : if ( raw_flag[i] )
506 : {
507 : /* Rewrite raw flag value */
508 275019 : hMetaData->ind_list[next_ind_raw_flag + i - start_band].value = 1;
509 :
510 : /* Write ec bits */
511 275019 : 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 543864 : bits_dir[d] += bits_dir_bands[i] + 1;
517 : }
518 :
519 118518 : 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 3794646 : 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 145179 : extra_bits = 0;
542 :
543 145179 : restore_metadata_buffer( hMetaData, next_ind_start, bit_pos_start );
544 :
545 145179 : push_next_indice( hMetaData, 1, 1 ); /*Write 1 bit to signal no EC frame-wise (EC1)*/
546 145179 : if ( nblocks > 1 )
547 : {
548 83028 : push_next_indice( hMetaData, 1, 1 ); /*Write 1 bit to signal requantization stage (EC3)*/
549 83028 : bits_signaling[d] = 3;
550 : }
551 : else
552 : {
553 62151 : bits_signaling[d] = 2;
554 : }
555 :
556 145179 : if ( hQMetaData->is_masa_ivas_format == 0 )
557 : {
558 38590 : reduce_bits = bits_dir_raw - ( total_bits_1dir - bits_diff[d] - bits_coherence[d] - bits_signaling[d] );
559 38590 : ind_order[0] = -1;
560 : }
561 : else
562 : {
563 106589 : ind_order[0] = 0;
564 106589 : 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 106589 : if ( reduce_bits > bits_dir_raw - nbands * nblocks )
567 : {
568 241 : reduce_bits = bits_dir_raw - nbands * nblocks;
569 : }
570 : }
571 :
572 145179 : only_reduce_bits_direction( &extra_bits, q_direction, reduce_bits, nbands, nblocks, ind_order );
573 145179 : bits_dir[d] = hMetaData->nb_bits_tot;
574 145179 : requantize_direction_EC_3( &extra_bits, q_direction, nbands, hMetaData, elevation_orig, azimuth_orig, ind_order );
575 145179 : 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 3794646 : if ( ( bits_coherence[d] > 0 ) && ( all_coherence_zero == 0 ) && ( nblocks > 1 ) )
583 : {
584 466826 : bit_pos_start = hMetaData->nb_bits_tot;
585 466826 : hMetaData->nb_bits_tot = bit_pos_start_coh;
586 466826 : ivas_qmetadata_quantize_coherence( hQMetaData, d, all_coherence_zero, hMetaData, 1, &indice_coherence, 0 );
587 466826 : hMetaData->nb_bits_tot = bit_pos_start;
588 : }
589 :
590 3794646 : if ( d == 0 )
591 : {
592 3500022 : 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 22160794 : for ( i = start_band; i < nbands; i++ )
658 : {
659 18366148 : mvr2r( q_direction->band_data[i].azimuth, q_direction->band_data[i].q_azimuth, nblocks );
660 18366148 : 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 22160794 : for ( i = start_band; i < nbands; i++ )
665 : {
666 18366148 : mvr2r( azimuth_orig[i], q_direction->band_data[i].azimuth, nblocks );
667 18366148 : mvr2r( elevation_orig[i], q_direction->band_data[i].elevation, nblocks );
668 : }
669 : }
670 :
671 3500022 : 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 66396 : 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 66396 : 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 66396 : ndirections = hQMetaData->no_directions;
729 :
730 : /* Check if coherence should be encoded */
731 66396 : all_coherence_zero = 1;
732 : #ifdef DEBUG_MODE_QMETADATA
733 : bits_no_dirs_coh = 0;
734 : #endif
735 66396 : if ( hQMetaData->q_direction->cfg.inactiveBands > 0 )
736 : {
737 14670 : push_next_indice( hMetaData, 1, 1 );
738 : /* write the number of inactive higher bands */
739 14670 : 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 51726 : push_next_indice( hMetaData, 0, 1 );
745 : }
746 66396 : if ( hQMetaData->coherence_flag )
747 : {
748 66396 : all_coherence_zero = hQMetaData->all_coherence_zero;
749 66396 : 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 66396 : if ( ndirections == 2 && bits_sph_idx == 11 )
757 : {
758 : #ifdef DEBUG_MODE_QMETADATA
759 : bits_no_dirs_coh +=
760 : #endif
761 12660 : write_2dir_info( hMetaData, hQMetaData->twoDirBands, hQMetaData->q_direction[0].cfg.nbands, hQMetaData->numTwoDirBands );
762 12660 : d = 0;
763 284318 : for ( i = hQMetaData->q_direction[1].cfg.start_band; i < hQMetaData->q_direction[1].cfg.nbands; i++ )
764 : {
765 271658 : if ( hQMetaData->twoDirBands[i] == 1 )
766 : {
767 119249 : mvr2r( hQMetaData->q_direction[1].band_data[i].azimuth, hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].cfg.nblocks );
768 119249 : mvr2r( hQMetaData->q_direction[1].band_data[i].elevation, hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].cfg.nblocks );
769 119249 : 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 119249 : if ( hQMetaData->coherence_flag )
772 : {
773 119249 : 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 119249 : d++;
776 : }
777 : }
778 165069 : for ( i = hQMetaData->numTwoDirBands; i < hQMetaData->q_direction[0].cfg.nbands; i++ )
779 : {
780 152409 : set_f( hQMetaData->q_direction[1].band_data[i].energy_ratio, 0.0f, hQMetaData->q_direction[1].cfg.nblocks );
781 : }
782 :
783 12660 : hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands;
784 : }
785 :
786 : /*Quantization and encoding of the Diffuseness */
787 66396 : ivas_qmetadata_quantize_diffuseness_nrg_ratios_hr_512( hQMetaData, bits_diff, bits_sph_idx, hMetaData );
788 :
789 : /* Encode surround coherence */
790 66396 : if ( all_coherence_zero == 0 )
791 : {
792 57978 : encode_surround_coherence_hr( hQMetaData, hMetaData );
793 : }
794 : else
795 : {
796 186109 : for ( i = 0; i < hQMetaData->q_direction[0].cfg.nbands; i++ )
797 : {
798 177691 : if ( hQMetaData->surcoh_band_data != NULL )
799 : {
800 177691 : 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 152868 : for ( d = 0; d < ndirections; d++ )
807 : {
808 86472 : q_direction = &( hQMetaData->q_direction[d] );
809 :
810 86472 : nbands = q_direction->cfg.nbands;
811 86472 : nblocks = q_direction->cfg.nblocks;
812 86472 : 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 86472 : q_direction->not_in_2D = 0;
835 :
836 : /*Coherence */
837 86472 : if ( all_coherence_zero == 0 )
838 : {
839 75809 : ivas_qmetadata_quantize_coherence_hr_512( hQMetaData, d, all_coherence_zero, hMetaData, bits_sp_coh );
840 : }
841 :
842 : /* write the spherical indexes */
843 86472 : bits_ec = hMetaData->nb_bits_tot;
844 86472 : if ( bits_sph_idx == 11 )
845 : {
846 : /* do the quantization */
847 53146 : quantize_direction_frame( q_direction, azimuth_orig, elevation_orig, 1 );
848 : }
849 :
850 1857119 : for ( i = start_band; i < nbands; i++ )
851 : {
852 7239880 : for ( j = 0; j < nblocks; j++ )
853 : {
854 5469233 : push_next_indice( hMetaData, q_direction->band_data[i].spherical_index[j], bits_sph_idx );
855 : }
856 : }
857 86472 : 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 86472 : if ( bits_sph_idx == 11 )
921 : {
922 1061215 : for ( i = start_band; i < nbands; i++ )
923 : {
924 1008069 : mvr2r( azimuth_orig[i], q_direction->band_data[i].azimuth, nblocks );
925 1008069 : mvr2r( elevation_orig[i], q_direction->band_data[i].elevation, nblocks );
926 : }
927 : }
928 : else
929 : {
930 795904 : for ( i = start_band; i < nbands; i++ )
931 : {
932 762578 : mvr2r( q_direction->band_data[i].azimuth, q_direction->band_data[i].q_azimuth, nblocks );
933 762578 : mvr2r( q_direction->band_data[i].elevation, q_direction->band_data[i].q_elevation, nblocks );
934 : }
935 : }
936 : }
937 :
938 66396 : if ( hQMetaData->q_direction->cfg.inactiveBands > 0 )
939 : {
940 14670 : hQMetaData->q_direction[0].cfg.nbands += hQMetaData->q_direction->cfg.inactiveBands;
941 14670 : if ( ndirections > 1 )
942 : {
943 4846 : hQMetaData->q_direction[1].cfg.nbands += hQMetaData->q_direction->cfg.inactiveBands;
944 : }
945 : }
946 :
947 66396 : 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 77912 : 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 77912 : if ( ivas_format == SBA_FORMAT )
979 : {
980 14109 : sba_spar_bitlen = ivas_sba_spar_sid_bitlen( nchan_transport );
981 14109 : 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 63803 : 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 77912 : 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 77912 : if ( masa_sid_descriptor > -1 )
1007 : {
1008 40796 : push_next_indice( hMetaData, masa_sid_descriptor, 1 );
1009 : }
1010 :
1011 : /* Code for one direction: diffuseness and average DOA(s)*/
1012 77912 : q_direction = &( q_metadata->q_direction[0] );
1013 77912 : nbands = q_direction->cfg.nbands;
1014 77912 : nblocks = q_direction->cfg.nblocks;
1015 77912 : start_band = 0; /*Start always with band 0 for SID*/
1016 :
1017 : /* sanity checks*/
1018 77912 : assert( q_metadata->no_directions == 1 && "Qmetadata SID: only one direction supported!" );
1019 77912 : if ( ivas_format == SBA_FORMAT )
1020 : {
1021 14109 : assert( ( q_direction->cfg.nbands == DIRAC_DTX_BANDS ) && "Qmetadata SID: only 2 bands supported!" );
1022 : }
1023 : else
1024 : {
1025 63803 : assert( ( q_direction->cfg.nbands == 5 ) && "Qmetadata SID: only 5 bands supported!" );
1026 : }
1027 :
1028 77912 : if ( ivas_format != SBA_FORMAT )
1029 : {
1030 : /* Signalling 2D*/
1031 63803 : push_next_indice( hMetaData, ( q_direction->not_in_2D > 0 ), 1 ); /*2D flag*/
1032 : }
1033 : else
1034 : {
1035 14109 : q_direction->not_in_2D = 1; /* force for merged modes */
1036 : }
1037 :
1038 : /*Encode the quantized diffuseness in raw coding*/
1039 77912 : bits_dir = 0;
1040 77912 : bits_diff = 0;
1041 77912 : if ( ivas_format != SBA_FORMAT )
1042 : {
1043 382818 : for ( b = start_band; b < nbands; b++ )
1044 : {
1045 319015 : q_direction->band_data[b].energy_ratio_index[0] = max( q_direction->band_data[b].energy_ratio_index[0], 4 );
1046 319015 : bits_diff += ivas_qmetadata_encode_quasi_uniform_length( q_direction->band_data[b].energy_ratio_index[0] - 4, DIRAC_DIFFUSE_LEVELS - 4 );
1047 319015 : q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[q_direction->band_data[b].energy_ratio_index[0]];
1048 :
1049 319015 : if ( q_direction->not_in_2D == 0 )
1050 : {
1051 103345 : q_direction->band_data[b].azimuth_m_alphabet[0] = 1 << ( min( 8, q_direction->band_data[b].bits_sph_idx[0] ) );
1052 103345 : 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 215670 : bits_dir += q_direction->band_data[b].bits_sph_idx[0];
1057 : }
1058 : }
1059 :
1060 : /* Reduce bit demand by increasing diffuseness*/
1061 63803 : bits_delta = metadata_sid_bits - ( hMetaData->nb_bits_tot - bit_pos_start ) - bits_diff - bits_dir;
1062 63803 : if ( bits_delta > 0 )
1063 : {
1064 270659 : while ( bits_delta > 0 )
1065 : {
1066 1087221 : for ( b = start_band; b < nbands && ( bits_delta > 0 ); b++ )
1067 : {
1068 880365 : if ( q_direction->band_data[b].bits_sph_idx[0] < 11 )
1069 : {
1070 880184 : bits_delta -= 1;
1071 880184 : q_direction->band_data[b].bits_sph_idx[0]++;
1072 : }
1073 : }
1074 : }
1075 :
1076 63803 : if ( q_direction->not_in_2D == 0 )
1077 : {
1078 124014 : for ( b = start_band; b < nbands; b++ )
1079 : {
1080 103345 : 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 42327 : for ( b = start_band; b < nbands; b++ )
1106 : {
1107 28218 : q_direction->band_data[b].energy_ratio_index[0] = max( q_direction->band_data[b].energy_ratio_index[0], 4 );
1108 28218 : bits_diff += ivas_qmetadata_encode_quasi_uniform_length( q_direction->band_data[b].energy_ratio_index[0] - 4, DIRAC_DIFFUSE_LEVELS - 4 );
1109 28218 : q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[q_direction->band_data[b].energy_ratio_index[0]];
1110 :
1111 28218 : 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 28218 : bits_dir += q_direction->band_data[b].bits_sph_idx[0];
1119 : }
1120 : }
1121 :
1122 : /* Reduce bit demand by increasing diffuseness*/
1123 14109 : bits_delta = metadata_sid_bits - ( hMetaData->nb_bits_tot - bit_pos_start ) - bits_diff - bits_dir;
1124 :
1125 14476 : while ( bits_delta < 0 && ( q_direction->not_in_2D > 0 ) )
1126 : {
1127 969 : for ( b = nbands - 1; b >= start_band && ( bits_delta < 0 ); b-- )
1128 : {
1129 602 : if ( q_direction->band_data[b].energy_ratio_index[0] < ( DIRAC_DIFFUSE_LEVELS - 1 ) )
1130 : {
1131 590 : bits_delta += q_direction->band_data[b].bits_sph_idx[0];
1132 590 : q_direction->band_data[b].energy_ratio_index[0]++;
1133 590 : q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[q_direction->band_data[b].energy_ratio_index[0]];
1134 590 : bits_delta -= q_direction->band_data[b].bits_sph_idx[0];
1135 : }
1136 : }
1137 : }
1138 : }
1139 77912 : assert( ( bits_delta >= 0 ) && "Bit budget in Qmetadata SID is violated!!!" );
1140 :
1141 : /*Code diffuseness*/
1142 425145 : for ( b = start_band; b < nbands; b++ )
1143 : {
1144 347233 : 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 425145 : for ( b = start_band; b < nbands; b++ )
1149 : {
1150 347233 : set_zero( avg_direction_vector, 3 );
1151 1053674 : for ( m = 0; m < nblocks; m++ )
1152 : {
1153 : /*compute the average direction */
1154 706441 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[m], q_direction->band_data[b].elevation[m], direction_vector );
1155 706441 : v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
1156 : }
1157 :
1158 347233 : ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &avg_azimuth[b], &avg_elevation[b] );
1159 :
1160 : /* Quantize the average direction */
1161 347233 : if ( q_direction->not_in_2D == 0 )
1162 : {
1163 103345 : avg_elevation[b] = 0;
1164 103345 : 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 103345 : &q_direction->band_data[b].azimuth_index[0], q_direction->cfg.mc_ls_setup );
1166 : }
1167 : else
1168 : {
1169 243888 : 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 243888 : &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 347233 : q_direction->band_data[b].q_azimuth[0] = avg_azimuth[b];
1175 347233 : q_direction->band_data[b].q_elevation[0] = avg_elevation[b];
1176 :
1177 347233 : if ( q_direction->band_data[b].azimuth_index[0] == MASA_NO_INDEX )
1178 : {
1179 376 : q_direction->band_data[b].azimuth_index[0] = 0;
1180 : }
1181 : }
1182 :
1183 : /* quantize average elevation and azimuth angles */
1184 77912 : if ( q_direction->not_in_2D > 0 )
1185 : {
1186 301131 : for ( b = start_band; b < nbands; b++ )
1187 : {
1188 243888 : 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 124014 : for ( b = start_band; b < nbands; b++ )
1194 : {
1195 103345 : 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 77912 : assert( ( hMetaData->nb_bits_tot - bit_pos_start ) <= metadata_sid_bits && "Too many written bits!" );
1222 162713 : while ( ( hMetaData->nb_bits_tot - bit_pos_start ) < metadata_sid_bits )
1223 : {
1224 84801 : push_next_indice( hMetaData, 0, 1 ); /*fill bit*/
1225 : }
1226 :
1227 77912 : return;
1228 : }
1229 :
1230 :
1231 : /*-------------------------------------------------------------------------
1232 : * reset_metadata_spatial()
1233 : *
1234 : * Reset metadata in spatial formats
1235 : *------------------------------------------------------------------------*/
1236 :
1237 1546257 : 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 1546257 : if ( core_brate == SID_2k40 || core_brate == FRAME_NO_DATA )
1251 : {
1252 14926 : if ( ( ivas_format == SBA_FORMAT || ivas_format == MASA_FORMAT ) && core_brate != FRAME_NO_DATA )
1253 : {
1254 2369 : if ( ivas_format == SBA_FORMAT )
1255 : {
1256 : #ifdef DEBUGGING
1257 : assert( hMetaData->ind_list[0].nb_bits == 1 );
1258 : #endif
1259 1432 : hMetaData->ind_list[0].value = 1;
1260 1432 : metadata_sid_bits = (int16_t) ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS - SBA_PLANAR_BITS - SBA_ORDER_BITS;
1261 :
1262 1432 : 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 937 : last_ind_sid = hMetaData->nb_ind_tot;
1271 937 : next_ind_sid = hMetaData->nb_ind_tot;
1272 12086 : while ( hMetaData->nb_bits_tot > nb_bits_metadata )
1273 : {
1274 11149 : next_ind_sid--;
1275 11149 : hMetaData->nb_bits_tot -= hMetaData->ind_list[next_ind_sid].nb_bits;
1276 : }
1277 :
1278 937 : hMetaData->nb_bits_tot = 0;
1279 :
1280 22788 : for ( i = 0; i < next_ind_sid; i++ )
1281 : {
1282 21851 : 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 12086 : for ( j = 0, i = next_ind_sid; i < last_ind_sid; i++, j++ )
1289 : {
1290 11149 : hMetaData->ind_list[j].value = hMetaData->ind_list[i].value;
1291 11149 : 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 11149 : hMetaData->nb_bits_tot += hMetaData->ind_list[j].nb_bits;
1296 11149 : 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 937 : 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 12557 : reset_indices_enc( hMetaData, hMetaData->nb_ind_tot );
1312 : }
1313 :
1314 14926 : *total_brate = element_brate;
1315 : }
1316 1531331 : else if ( ivas_format != SBA_FORMAT )
1317 : {
1318 : /* Reset SID metadata bits*/
1319 1251273 : while ( hMetaData->nb_bits_tot > nb_bits_metadata )
1320 : {
1321 709511 : hMetaData->nb_ind_tot--;
1322 709511 : hMetaData->nb_bits_tot -= hMetaData->ind_list[hMetaData->nb_ind_tot].nb_bits;
1323 709511 : 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 1546257 : return;
1334 : }
1335 :
1336 :
1337 : /*-------------------------------------------------------------------------
1338 : * quantize_direction2D()
1339 : *
1340 : *
1341 : *------------------------------------------------------------------------*/
1342 :
1343 : /*! r: quantized spherical index */
1344 998206 : 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 998206 : if ( no_cw < 2 )
1355 : {
1356 0 : *phi_q = 0;
1357 :
1358 0 : return 0;
1359 : }
1360 :
1361 998206 : if ( mc_format != MC_LS_SETUP_INVALID )
1362 : {
1363 864247 : id_phi = quantize_phi_chan_compand( phi + 180, phi_q, no_cw, 0, mc_format );
1364 : }
1365 : else
1366 : {
1367 133959 : id_phi = quantize_phi( phi + 180, 0, phi_q, no_cw );
1368 : }
1369 998206 : *phi_q -= 180;
1370 998206 : *index_phi = ivas_qmetadata_reorder_generic( id_phi - ( no_cw >> 1 ) );
1371 :
1372 998206 : idx_sph = id_phi;
1373 :
1374 998206 : return idx_sph;
1375 : }
1376 :
1377 :
1378 66396 : 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 66396 : needed_bits[0] = 0;
1388 66396 : needed_bits[1] = 0;
1389 :
1390 1547344 : for ( j = hQMetaData->q_direction[0].cfg.start_band; j < hQMetaData->q_direction[0].cfg.nbands; ++j )
1391 : {
1392 6110126 : for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ )
1393 : {
1394 4629178 : index = masa_sq( 1.0f - hQMetaData->q_direction[0].band_data[j].energy_ratio[k], diffuseness_thresholds_hr, HR_MASA_ER_LEVELS );
1395 4629178 : push_next_indice( hMetaData, index, MASA_BITS_ER_HR );
1396 4629178 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index[k] = index;
1397 4629178 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index_mod[k] = index;
1398 4629178 : hQMetaData->q_direction[0].band_data[j].energy_ratio[k] = 1.0f - diffuseness_reconstructions_hr[index];
1399 4629178 : needed_bits[0] += MASA_BITS_ER_HR;
1400 4629178 : hQMetaData->q_direction[0].band_data[j].bits_sph_idx[k] = bits_dir_hr;
1401 : }
1402 : }
1403 :
1404 66396 : if ( hQMetaData->no_directions == 2 )
1405 : {
1406 : float ratioSum;
1407 20076 : if ( bits_dir_hr == 16 )
1408 : {
1409 177866 : for ( j = hQMetaData->q_direction[1].cfg.start_band; j < hQMetaData->q_direction[1].cfg.nbands; j++ )
1410 : {
1411 673840 : for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ )
1412 : {
1413 503390 : index = masa_sq( 1.0f - hQMetaData->q_direction[1].band_data[j].energy_ratio[k], diffuseness_thresholds_hr, HR_MASA_ER_LEVELS );
1414 503390 : push_next_indice( hMetaData, index, MASA_BITS_ER_HR );
1415 503390 : hQMetaData->q_direction[1].band_data[j].energy_ratio_index[k] = index;
1416 503390 : hQMetaData->q_direction[1].band_data[j].energy_ratio[k] = 1.0f - diffuseness_reconstructions_hr[index];
1417 :
1418 503390 : ratioSum = hQMetaData->q_direction[0].band_data[j].energy_ratio[k] + hQMetaData->q_direction[1].band_data[j].energy_ratio[k];
1419 503390 : if ( ratioSum > 1.0f )
1420 : {
1421 110890 : hQMetaData->q_direction[0].band_data[j].energy_ratio[k] /= ratioSum;
1422 110890 : hQMetaData->q_direction[1].band_data[j].energy_ratio[k] /= ratioSum;
1423 : }
1424 :
1425 503390 : needed_bits[1] += MASA_BITS_ER_HR;
1426 503390 : 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 12660 : k = 0;
1434 284318 : for ( j = hQMetaData->q_direction[0].cfg.start_band; j < hQMetaData->q_direction[0].cfg.nbands; j++ )
1435 : {
1436 271658 : if ( hQMetaData->twoDirBands[j] == 1 )
1437 : {
1438 119249 : pos_2dir_band[k] = j;
1439 119249 : k++;
1440 : }
1441 : else
1442 : {
1443 152409 : pos_2dir_band[k] = 0;
1444 : }
1445 : }
1446 131909 : for ( j = hQMetaData->q_direction[1].cfg.start_band; j < hQMetaData->q_direction[1].cfg.nbands; j++ )
1447 : {
1448 455914 : for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ )
1449 : {
1450 336665 : index = masa_sq( 1.0f - hQMetaData->q_direction[1].band_data[j].energy_ratio[k], diffuseness_thresholds_hr, HR_MASA_ER_LEVELS );
1451 336665 : push_next_indice( hMetaData, index, MASA_BITS_ER_HR );
1452 336665 : hQMetaData->q_direction[1].band_data[j].energy_ratio_index[k] = index;
1453 336665 : hQMetaData->q_direction[1].band_data[j].energy_ratio[k] = 1.0f - diffuseness_reconstructions_hr[index];
1454 :
1455 336665 : 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 336665 : if ( ratioSum > 1.0f )
1458 : {
1459 30299 : hQMetaData->q_direction[0].band_data[pos_2dir_band[j]].energy_ratio[k] /= ratioSum;
1460 30299 : hQMetaData->q_direction[1].band_data[j].energy_ratio[k] /= ratioSum;
1461 : }
1462 :
1463 336665 : needed_bits[1] += MASA_BITS_ER_HR;
1464 336665 : hQMetaData->q_direction[1].band_data[j].bits_sph_idx[k] = bits_dir_hr;
1465 : }
1466 : }
1467 : }
1468 : }
1469 :
1470 66396 : return;
1471 : }
1472 :
1473 :
1474 : /*-------------------------------------------------------------------------
1475 : * ivas_qmetadata_quantize_diffuseness_nrg_ratios()
1476 : *
1477 : * Quantize diffuseness
1478 : *------------------------------------------------------------------------*/
1479 :
1480 3500022 : 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 3500022 : nbits_diff[0] = 0;
1492 3500022 : nbits_diff[1] = 0;
1493 3500022 : needed_bits[0] = 0;
1494 3500022 : needed_bits[1] = 0;
1495 3500022 : dir2band = 0;
1496 :
1497 19706252 : for ( j = hQMetaData->q_direction[0].cfg.start_band; j < hQMetaData->q_direction[0].cfg.nbands; ++j )
1498 : {
1499 16206230 : if ( hQMetaData->no_directions == 2 && hQMetaData->twoDirBands[j] == 1 )
1500 2159918 : {
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 2159918 : if ( hodirac_flag )
1510 : {
1511 : /* already encoded as total and ratios in HO-DirAC */
1512 1672363 : diffRatio = 1.f - hQMetaData->q_direction[0].band_data[j].energy_ratio[0];
1513 1672363 : dfRatio = hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[0];
1514 : }
1515 : else
1516 : {
1517 487555 : dirRatio1 = hQMetaData->q_direction[0].band_data[j].energy_ratio[0];
1518 487555 : dirRatio2 = hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[0];
1519 487555 : sumRatio = dirRatio1 + dirRatio2;
1520 487555 : diffRatio = 1.0f - sumRatio;
1521 487555 : dfRatio = sumRatio < EPSILON ? 0.5f : dirRatio1 / sumRatio;
1522 : }
1523 :
1524 :
1525 2159918 : index_diff = masa_sq( diffRatio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
1526 2159918 : diffRatioQ = diffuseness_reconstructions[index_diff];
1527 :
1528 2159918 : if ( hodirac_flag )
1529 : {
1530 1672363 : dfRatio_bits = ivas_get_df_ratio_bits_hodirac( index_diff );
1531 : }
1532 : else
1533 : {
1534 487555 : dfRatio_bits = ivas_get_df_ratio_bits( index_diff );
1535 : }
1536 :
1537 2159918 : dfRatioBits[dir2band] = dfRatio_bits;
1538 :
1539 2159918 : dfRatio_qsteps = ( 1 << dfRatio_bits );
1540 2159918 : if ( hodirac_flag )
1541 : {
1542 1672363 : dfRatio_index = usquant( dfRatio, &dfRatioQ, 0.0f, 1.f / ( dfRatio_qsteps - 1 ), dfRatio_qsteps );
1543 1672363 : dirRatio1Q = 1.f - diffRatioQ;
1544 1672363 : dirRatio2Q = dfRatioQ;
1545 : }
1546 : else
1547 : {
1548 487555 : 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 487555 : dirRatio1Q = dfRatioQ * ( 1.0f - diffRatioQ );
1552 487555 : dirRatio2Q = ( 1.0f - diffRatioQ ) - dirRatio1Q;
1553 : }
1554 :
1555 2159918 : 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 10112479 : for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ )
1561 : {
1562 7952561 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index[k] = index_diff;
1563 7952561 : hQMetaData->q_direction[0].band_data[j].energy_ratio[k] = dirRatio1Q;
1564 : }
1565 2159918 : nbits_diff[0] += MASA_BITS_ER;
1566 :
1567 2159918 : if ( hodirac_flag )
1568 : {
1569 : float tmp;
1570 1672363 : index_dirRatio2Inv = usquant( dirRatio2Q, &tmp, 0.0f, 1.f / ( DIRAC_DIFFUSE_LEVELS - 1 ), DIRAC_DIFFUSE_LEVELS );
1571 : }
1572 : else
1573 : {
1574 487555 : index_dirRatio2Inv = masa_sq( 1.0f - dirRatio2Q, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
1575 : }
1576 :
1577 10112479 : for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ )
1578 : {
1579 7952561 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[k] = dfRatio_index;
1580 7952561 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[k] = dirRatio2Q;
1581 : }
1582 2159918 : 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 2159918 : masa_compensate_two_dir_energy_ratio_index( index_dirRatio1Inv, index_dirRatio2Inv, &index_dirRatio1Inv_mod, &index_dirRatio2Inv_mod, hodirac_flag );
1589 :
1590 10112479 : for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ )
1591 : {
1592 7952561 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index_mod[k] = index_dirRatio1Inv_mod;
1593 7952561 : hQMetaData->q_direction[0].band_data[j].bits_sph_idx[k] = bits_direction_masa[index_dirRatio1Inv_mod];
1594 : }
1595 2159918 : needed_bits[0] += hQMetaData->q_direction[0].cfg.nblocks * bits_direction_masa[index_dirRatio1Inv_mod];
1596 :
1597 10112479 : for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ )
1598 : {
1599 7952561 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index_mod[k] = index_dirRatio2Inv_mod;
1600 7952561 : hQMetaData->q_direction[1].band_data[dir2band].bits_sph_idx[k] = bits_direction_masa[index_dirRatio2Inv_mod];
1601 : }
1602 2159918 : needed_bits[1] += hQMetaData->q_direction[1].cfg.nblocks * bits_direction_masa[index_dirRatio2Inv_mod];
1603 :
1604 2159918 : dir2band++;
1605 : }
1606 : else
1607 : {
1608 14046312 : index_dirRatio1Inv = masa_sq( 1.0f - hQMetaData->q_direction[0].band_data[j].energy_ratio[0], diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
1609 :
1610 56180340 : for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ )
1611 : {
1612 42134028 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index[k] = index_dirRatio1Inv;
1613 42134028 : hQMetaData->q_direction[0].band_data[j].energy_ratio_index_mod[k] = index_dirRatio1Inv;
1614 42134028 : hQMetaData->q_direction[0].band_data[j].energy_ratio[k] = 1.0f - diffuseness_reconstructions[index_dirRatio1Inv];
1615 42134028 : hQMetaData->q_direction[0].band_data[j].bits_sph_idx[k] = bits_direction_masa[index_dirRatio1Inv];
1616 : }
1617 :
1618 14046312 : nbits_diff[0] += MASA_BITS_ER;
1619 :
1620 14046312 : needed_bits[0] += hQMetaData->q_direction[0].cfg.nblocks * bits_direction_masa[index_dirRatio1Inv];
1621 : }
1622 : }
1623 :
1624 3500022 : return;
1625 : }
1626 :
1627 :
1628 : /*-------------------------------------------------------------------------
1629 : * ivas_diffuseness_huff_ec_encode()
1630 : *
1631 : *
1632 : *------------------------------------------------------------------------*/
1633 :
1634 4050961 : static int16_t ivas_diffuseness_huff_ec_encode(
1635 : BSTR_ENC_HANDLE hMetaData,
1636 : const uint16_t idx )
1637 : {
1638 : int16_t nbits;
1639 4050961 : nbits = 0;
1640 4050961 : if ( idx <= DIFF_EC_HUFF_GR0_LIMIT )
1641 : {
1642 4046174 : if ( idx > 0 )
1643 : {
1644 1670184 : push_next_indice( hMetaData, ( 1 << idx ) - 1, idx );
1645 1670184 : nbits += idx;
1646 : }
1647 4046174 : push_next_indice( hMetaData, 0, 1 );
1648 4046174 : nbits += 1;
1649 : }
1650 : else
1651 : {
1652 4787 : push_next_indice( hMetaData, 511, DIFF_EC_HUFF_GR0_LIMIT + 1 );
1653 4787 : push_next_indice( hMetaData, idx - DIFF_EC_HUFF_GR0_LIMIT - 1, 2 );
1654 4787 : nbits += DIFF_EC_HUFF_GR0_LIMIT + 3;
1655 : }
1656 4050961 : return nbits;
1657 : }
1658 :
1659 :
1660 : /*-------------------------------------------------------------------------
1661 : * ivas_diffuseness_huff_ec_prepare()
1662 : *
1663 : *
1664 : *------------------------------------------------------------------------*/
1665 :
1666 457638 : 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 457638 : start_band = q_direction->cfg.start_band;
1680 457638 : nbands = q_direction->cfg.nbands;
1681 :
1682 457638 : *diffuseness_bits_huff = 0;
1683 457638 : av = 0;
1684 6528009 : for ( b = start_band; b < nbands; b++ )
1685 : {
1686 6070371 : av += q_direction->band_data[b].energy_ratio_index[0];
1687 : }
1688 457638 : av = (int16_t) ( 0.5f + av / (float) nbands );
1689 457638 : *best_av = av;
1690 :
1691 457638 : *diffuseness_bits_huff = MAX16B;
1692 1830552 : for ( av_crt = av - 1; av_crt <= av + 1; av_crt++ )
1693 : {
1694 1372914 : bits = 0;
1695 19584027 : for ( b = start_band; b < nbands; b++ )
1696 : {
1697 18211113 : sh_idx = q_direction->band_data[b].energy_ratio_index[0] - av_crt;
1698 18211113 : ui_sh_idx[b] = ( sh_idx <= 0 ) ? ( -2 * sh_idx ) : sh_idx * 2 - 1;
1699 18211113 : if ( ui_sh_idx[b] >= 2 * DIRAC_DIFFUSE_LEVELS - 3 )
1700 : {
1701 4966 : bits = 100; /* to avoid difference larger than 6 in absolute value */
1702 : }
1703 :
1704 18211113 : bits += ( ui_sh_idx[b] <= DIFF_EC_HUFF_GR0_LIMIT ) ? ( ui_sh_idx[b] + 1 ) : 11;
1705 : }
1706 :
1707 1372914 : if ( bits < *diffuseness_bits_huff )
1708 : {
1709 829061 : *diffuseness_bits_huff = bits;
1710 829061 : mvs2s( (int16_t *) ui_sh_idx, (int16_t *) avr_idx, nbands );
1711 829061 : *best_av = av_crt;
1712 : }
1713 : }
1714 :
1715 457638 : *diffuseness_bits_huff += MASA_BITS_ER; /* for the average */
1716 :
1717 457638 : return;
1718 : }
1719 :
1720 : /*-------------------------------------------------------------------------
1721 : * ivas_qmetadata_entropy_encode_diffuseness()
1722 : *
1723 : * encode diffuseness
1724 : *------------------------------------------------------------------------*/
1725 :
1726 3500022 : 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 3500022 : nbands = q_direction->cfg.nbands;
1739 3500022 : start_band = q_direction->cfg.start_band;
1740 :
1741 3500022 : start_bit_pos = hMetaData->nb_bits_tot;
1742 :
1743 3500022 : if ( nbands == 1 )
1744 : {
1745 : /* If there is only one band, diffuseness should be coded directly as raw with no signaling. */
1746 50688 : push_next_indice( hMetaData, q_direction->band_data[0].energy_ratio_index[0], MASA_BITS_ER );
1747 50688 : *diffuseness_index_max_ec_frame = 5;
1748 50688 : return ( hMetaData->nb_bits_tot - start_bit_pos );
1749 : }
1750 :
1751 : /* compute the number of raw coding bits */
1752 3449334 : diffuseness_bits_raw = 0;
1753 19604876 : for ( b = start_band; b < nbands; b++ )
1754 : {
1755 16155542 : diffuseness_bits_raw += ivas_qmetadata_encode_quasi_uniform_length( q_direction->band_data[b].energy_ratio_index[0], DIRAC_DIFFUSE_LEVELS );
1756 : }
1757 :
1758 3449334 : min_diffuseness_m_index = q_direction->band_data[start_band].energy_ratio_index[0];
1759 3449334 : max_diffuseness_m_index = q_direction->band_data[start_band].energy_ratio_index[0];
1760 :
1761 19604876 : for ( b = start_band; b < nbands; b++ )
1762 : {
1763 16155542 : if ( q_direction->band_data[b].energy_ratio_index[0] < min_diffuseness_m_index )
1764 : {
1765 1866941 : min_diffuseness_m_index = q_direction->band_data[b].energy_ratio_index[0];
1766 : }
1767 :
1768 16155542 : if ( q_direction->band_data[b].energy_ratio_index[0] > max_diffuseness_m_index )
1769 : {
1770 2137379 : max_diffuseness_m_index = q_direction->band_data[b].energy_ratio_index[0];
1771 : }
1772 : }
1773 :
1774 3449334 : 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 2991696 : if ( min_diffuseness_m_index == max_diffuseness_m_index ) /* all values are equal */
1779 : {
1780 819835 : push_next_indice( hMetaData, 0, 1 ); /* dif_use_raw_coding */
1781 819835 : push_next_indice( hMetaData, 1, 1 ); /* dif_have_unique_value */
1782 819835 : ivas_qmetadata_encode_quasi_uniform( hMetaData, min_diffuseness_m_index, DIRAC_DIFFUSE_LEVELS ); /* dif_unique_value */
1783 : }
1784 2171861 : else if ( min_diffuseness_m_index + 1 == max_diffuseness_m_index ) /* only two consecutive values are present */
1785 : {
1786 725655 : push_next_indice( hMetaData, 0, 1 ); /* dif_use_raw_coding */
1787 725655 : push_next_indice( hMetaData, 0, 1 ); /* dif_have_unique_value */
1788 725655 : ivas_qmetadata_encode_quasi_uniform( hMetaData, min_diffuseness_m_index, DIRAC_DIFFUSE_LEVELS - 1 ); /* dif_min_value */
1789 :
1790 3093612 : for ( b = start_band; b < nbands; b++ )
1791 : {
1792 2367957 : 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 1446206 : push_next_indice( hMetaData, 1, 1 ); /* dif_use_raw_coding */
1798 :
1799 6605013 : for ( b = start_band; b < nbands; b++ )
1800 : {
1801 5158807 : 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 457638 : 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 457638 : if ( diffuseness_bits_huff < diffuseness_bits_raw )
1817 : {
1818 : /* Signal Huffman EC */
1819 301796 : push_next_indice( hMetaData, 0, 1 );
1820 301796 : push_next_indice( hMetaData, best_av, MASA_BITS_ER );
1821 4352757 : for ( b = start_band; b < nbands; b++ )
1822 : {
1823 4050961 : 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 155842 : push_next_indice( hMetaData, 1, 1 );
1833 2175252 : for ( b = start_band; b < nbands; b++ )
1834 : {
1835 2019410 : 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 3449334 : *diffuseness_index_max_ec_frame = 5;
1841 : /* adaptively select the diffuseness_index_max_ec threshold */
1842 3449334 : if ( min_diffuseness_m_index > 5 )
1843 : {
1844 1169015 : *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 3449334 : 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 294624 : 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 294624 : int16_t ec_mode = 0;
1873 : int16_t max_alphabet_size;
1874 :
1875 294624 : nbands = q_direction->cfg.nbands;
1876 294624 : start_band = q_direction->cfg.start_band;
1877 :
1878 294624 : start_bit_pos = hMetaData->nb_bits_tot;
1879 :
1880 294624 : if ( nbands == 1 )
1881 : {
1882 : /* If there is only one band, ratio should be coded directly as raw with no signaling. */
1883 42795 : push_next_indice( hMetaData, q_direction->band_data[0].energy_ratio_index[0], df_ratio_bits[0] );
1884 :
1885 42795 : return ( hMetaData->nb_bits_tot - start_bit_pos );
1886 : }
1887 :
1888 : /* compute the number of raw coding bits */
1889 251829 : bits_raw = 0;
1890 251829 : max_df_ratio_bits = 0;
1891 2368952 : for ( b = start_band; b < nbands; b++ )
1892 : {
1893 2117123 : bits_raw += df_ratio_bits[b];
1894 2117123 : max_df_ratio_bits = max( df_ratio_bits[b], max_df_ratio_bits );
1895 : }
1896 :
1897 251829 : min_index = q_direction->band_data[start_band].energy_ratio_index[0];
1898 251829 : max_index = q_direction->band_data[start_band].energy_ratio_index[0];
1899 2368952 : for ( b = start_band; b < nbands; b++ )
1900 : {
1901 2117123 : if ( q_direction->band_data[b].energy_ratio_index[0] < min_index )
1902 : {
1903 166084 : min_index = q_direction->band_data[b].energy_ratio_index[0];
1904 : }
1905 :
1906 2117123 : if ( q_direction->band_data[b].energy_ratio_index[0] > max_index )
1907 : {
1908 219179 : max_index = q_direction->band_data[b].energy_ratio_index[0];
1909 : }
1910 : }
1911 :
1912 : /* Decide what modes are possible */
1913 251829 : if ( bits_raw >= max_df_ratio_bits + 2 + nbands )
1914 : {
1915 157322 : ec_mode = 2;
1916 : }
1917 94507 : else if ( bits_raw >= max_df_ratio_bits + 1 )
1918 : {
1919 94507 : ec_mode = 1;
1920 : }
1921 : else
1922 : {
1923 0 : ec_mode = 0;
1924 : }
1925 251829 : max_alphabet_size = 1 << max_df_ratio_bits;
1926 :
1927 251829 : if ( min_index == max_index && ec_mode > 0 ) /* all values are equal */
1928 : {
1929 34758 : push_next_indice( hMetaData, 0, 1 ); /* Signal between EC and raw */
1930 34758 : if ( ec_mode > 1 )
1931 : {
1932 : /* Only use bit for signaling if necessary */
1933 10932 : push_next_indice( hMetaData, 0, 1 ); /* Signal between one value or bandwise diff mode */
1934 : }
1935 :
1936 34758 : ivas_qmetadata_encode_quasi_uniform( hMetaData, min_index, max_alphabet_size );
1937 : }
1938 217071 : else if ( min_index + 1 == max_index && ec_mode > 1 ) /* only two consecutive values are present */
1939 : {
1940 29597 : push_next_indice( hMetaData, 0, 1 );
1941 29597 : push_next_indice( hMetaData, 1, 1 );
1942 29597 : ivas_qmetadata_encode_quasi_uniform( hMetaData, min_index, max_alphabet_size - 1 );
1943 :
1944 237555 : for ( b = start_band; b < nbands; b++ )
1945 : {
1946 207958 : 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 187474 : if ( ec_mode > 0 )
1952 : {
1953 187474 : push_next_indice( hMetaData, 1, 1 ); /* Only signal raw mode if not implicitly using it */
1954 : }
1955 :
1956 1960577 : for ( b = start_band; b < nbands; b++ )
1957 : {
1958 1773103 : 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 251829 : return ( hMetaData->nb_bits_tot - start_bit_pos );
1963 : }
1964 :
1965 :
1966 : /*-------------------------------------------------------------------------
1967 : * restore_metadata_buffer()
1968 : *
1969 : * Restore metadata buffer
1970 : *------------------------------------------------------------------------*/
1971 :
1972 4222844 : 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 211881853 : for ( i = next_ind_start; i < hMetaData->nb_ind_tot; i++ )
1980 : {
1981 207659009 : 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 4222844 : hMetaData->nb_bits_tot = bit_pos_start;
1987 4222844 : hMetaData->nb_ind_tot = next_ind_start;
1988 :
1989 4222844 : 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 28926068 : 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 28926068 : bits = 30 - norm_l( alphabet_size ); /* bits = floor(log2(alphabet_size)) */
2012 28926068 : tresh = ( 1U << ( bits + 1 ) ) - alphabet_size;
2013 :
2014 28926068 : if ( value < tresh )
2015 : {
2016 24063088 : push_next_indice( hMetaData, value, bits );
2017 : }
2018 : else /* value >= tresh */
2019 : {
2020 4862980 : push_next_indice( hMetaData, value + tresh, bits + 1 );
2021 : }
2022 :
2023 28926068 : 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 2525910 : 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 2525910 : int16_t nbits = 0, i;
2047 2525910 : int16_t nbits1 = 0;
2048 : int16_t nb;
2049 :
2050 23752171 : for ( i = 0; i < no_data; i++ )
2051 : {
2052 21226261 : nb = ivas_qmetadata_encode_extended_gr_length( data[i], no_symb[i], GR_order );
2053 21226261 : nbits += nb;
2054 : }
2055 :
2056 2525910 : if ( check_two_orders == 1 )
2057 : {
2058 20141212 : for ( i = 0; i < no_data; i++ )
2059 : {
2060 18082128 : nb = ivas_qmetadata_encode_extended_gr_length( data[i], no_symb[i], GR_order - 1 );
2061 18082128 : nbits1 += nb;
2062 : }
2063 :
2064 2059084 : if ( nbits1 < nbits )
2065 : {
2066 1235225 : nbits = nbits1 + 1;
2067 1235225 : *real_GR_ord = GR_order - 1;
2068 : }
2069 : else
2070 : {
2071 823859 : nbits += 1;
2072 823859 : *real_GR_ord = GR_order;
2073 : }
2074 : }
2075 : else
2076 : {
2077 466826 : *real_GR_ord = GR_order;
2078 : }
2079 :
2080 2525910 : 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 291210 : 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 291210 : int16_t no_data = 0;
2108 :
2109 1154868 : for ( i = 0; i < no_data_in; i++ )
2110 : {
2111 863658 : if ( data_in[i] < MASA_NO_INDEX )
2112 : {
2113 861767 : no_symb_local[no_data] = no_symb[i];
2114 861767 : data[no_data++] = data_in[i];
2115 : }
2116 : }
2117 :
2118 291210 : if ( no_data == 0 )
2119 : {
2120 193 : *p_use_context = -3; /* corresponding to nothing to be written */
2121 193 : return 0;
2122 : }
2123 :
2124 291017 : nbits = 0;
2125 291017 : use_context = 0;
2126 :
2127 1152784 : for ( i = 0; i < no_data; i++ )
2128 : {
2129 861767 : if ( ( bits_dir[i] <= 1 ) )
2130 : {
2131 4 : nbits += bits_dir[i];
2132 4 : use_context = 1;
2133 : }
2134 : else
2135 : {
2136 861763 : *real_GR_ord = GR_order - ( bits_dir[i] == 2 );
2137 861763 : nbits += ivas_qmetadata_encode_extended_gr_length( data[i], no_symb_local[i], *real_GR_ord );
2138 : }
2139 : }
2140 :
2141 291017 : real_GR_ord1 = 0;
2142 291017 : if ( use_context == 0 )
2143 : {
2144 291013 : nbits = GR_bits_new( data, no_symb_local, no_data, GR_order, 1, real_GR_ord );
2145 291013 : nbits1 = nbits;
2146 :
2147 291013 : min_val = data[0];
2148 861751 : for ( i = 1; i < no_data; i++ )
2149 : {
2150 570738 : if ( data[i] < min_val )
2151 : {
2152 75496 : min_val = data[i];
2153 : }
2154 : }
2155 1152764 : for ( i = 0; i < no_data; i++ )
2156 : {
2157 861751 : cdata[i] = data[i] - min_val;
2158 : }
2159 :
2160 291013 : maximum_s( no_symb_local, no_data, &max_val );
2161 291013 : nbits1 = GR_bits_new( cdata, no_symb_local, no_data, GR_order - 1, 1, &real_GR_ord1 ) + ivas_qmetadata_encode_extended_gr_length( min_val, max_val, MASA_GR_ORD_AZ );
2162 :
2163 291013 : if ( nbits1 < nbits )
2164 : {
2165 81419 : nbits = nbits1 + 1;
2166 81419 : use_context = -2;
2167 81419 : *real_GR_ord = real_GR_ord1;
2168 : }
2169 : else
2170 : {
2171 209594 : nbits = nbits + 1;
2172 209594 : use_context = -1;
2173 : }
2174 : }
2175 :
2176 291017 : *p_use_context = use_context;
2177 :
2178 291017 : return nbits;
2179 : }
2180 :
2181 :
2182 : /*-------------------------------------------------------------------------
2183 : * mean_removed_GR_new()
2184 : *
2185 : * Golomb Rice encoding with mean removing
2186 : *------------------------------------------------------------------------*/
2187 :
2188 : /*! r: number of bits used */
2189 709666 : static int16_t mean_removed_GR_new(
2190 : const uint16_t *idx, /* i : data to encode */
2191 : const int16_t max_no_symb,
2192 : const int16_t len, /* i : number of data */
2193 : const int16_t adapt_GR, /* i : flag for telling to use or nor two GR order values */
2194 : int16_t *GR_ord, /* i/o: GR order */
2195 : uint16_t *p_av, /* o : average index */
2196 : uint16_t *mr_idx /* o : mean removed indexes */
2197 : )
2198 : {
2199 : int16_t av, i, nbits;
2200 : int16_t sh_idx[MASA_MAXIMUM_CODING_SUBBANDS];
2201 : int16_t max_ns[MASA_MAXIMUM_CODING_SUBBANDS];
2202 :
2203 709666 : av = (int16_t) ( 0.5f + sum_s( (const int16_t *) idx, len ) / (float) len );
2204 709666 : *p_av = av;
2205 8808973 : for ( i = 0; i < len; i++ )
2206 : {
2207 8099307 : max_ns[i] = 2 * ( max_no_symb );
2208 8099307 : sh_idx[i] = idx[i] - av;
2209 : }
2210 :
2211 8808973 : for ( i = 0; i < len; i++ )
2212 : {
2213 8099307 : if ( sh_idx[i] < 0 )
2214 : {
2215 1505175 : sh_idx[i] = -2 * sh_idx[i];
2216 : }
2217 6594132 : else if ( sh_idx[i] > 0 )
2218 : {
2219 1306818 : sh_idx[i] = sh_idx[i] * 2 - 1;
2220 : }
2221 : else
2222 : {
2223 5287314 : sh_idx[i] = 0;
2224 : }
2225 8099307 : mr_idx[i] = (uint16_t) sh_idx[i];
2226 : }
2227 :
2228 709666 : nbits = GR_bits_new( mr_idx, max_ns, len, *GR_ord, adapt_GR, GR_ord );
2229 :
2230 709666 : return nbits;
2231 : }
2232 :
2233 :
2234 : /*-------------------------------------------------------------------------
2235 : * ivas_qmetadata_encode_quasi_uniform_length()
2236 : *
2237 : *------------------------------------------------------------------------*/
2238 :
2239 144459931 : static int16_t ivas_qmetadata_encode_quasi_uniform_length(
2240 : const uint16_t value,
2241 : const uint16_t alphabet_size )
2242 : {
2243 : int16_t bits;
2244 : uint16_t tresh;
2245 : #ifdef DEBUGGING
2246 : assert( ( alphabet_size >= 1 ) );
2247 : assert( value < alphabet_size );
2248 : #endif
2249 :
2250 144459931 : bits = 30 - norm_l( alphabet_size ); /* bits = floor(log2(alphabet_size)) */
2251 144459931 : tresh = ( 1U << ( bits + 1 ) ) - alphabet_size;
2252 :
2253 144459931 : if ( value >= tresh )
2254 : {
2255 35589648 : bits++;
2256 : }
2257 :
2258 144459931 : return bits;
2259 : }
2260 :
2261 :
2262 : /*-------------------------------------------------------------------------
2263 : * ivas_qmetadata_entropy_encode_dir()
2264 : *
2265 : * Main function for entropy coding of the directions
2266 : *------------------------------------------------------------------------*/
2267 :
2268 4338510 : static int16_t ivas_qmetadata_entropy_encode_dir(
2269 : BSTR_ENC_HANDLE hMetaData,
2270 : IVAS_QDIRECTION *q_direction,
2271 : const uint16_t diffuseness_index_max_ec_frame,
2272 : const int16_t nbands,
2273 : const int16_t start_band,
2274 : const int16_t direction_bits_raw,
2275 : int16_t max_bits,
2276 : const int16_t hrmasa_flag )
2277 : {
2278 : uint16_t diff_idx_min;
2279 : int16_t i, j;
2280 : int16_t nblocks;
2281 :
2282 : float avg_direction_vector[3], direction_vector[3], avg_azimuth, avg_elevation;
2283 : int16_t avg_azimuth_alphabet, avg_elevation_alphabet;
2284 : uint16_t avg_azimuth_index, avg_elevation_index;
2285 : int16_t avg_elevation_index_projected;
2286 : int16_t avg_azimuth_index_projected;
2287 : uint16_t avg_elevation_index_initial, avg_elevation_offset;
2288 : uint16_t avg_azimuth_index_initial, avg_azimuth_offset;
2289 : int16_t elevation_bits_ec_best, azimuth_bits_ec_best;
2290 :
2291 4338510 : int16_t gr_param_elevation_best = 0, avg_elevation_index_best = 0;
2292 : uint16_t dist_elevation_indexes_best[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2293 : int16_t gr_param_azimuth_best, avg_azimuth_index_best;
2294 : uint16_t dist_azimuth_indexes_best[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2295 :
2296 : uint16_t idx, dist_count;
2297 : int16_t direction_bits_ec;
2298 :
2299 : uint16_t dist_elevation_indexes[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2300 : uint16_t dist_elevation_alphabets[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2301 : uint16_t dist_azimuth_indexes[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2302 : uint16_t dist_azimuth_alphabets[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
2303 4338510 : int16_t all_zero_dist_elevation_indexes = 1, all_zero_dist_azimuth_indexes = 1;
2304 : int16_t gr_param_elevation, gr_size_elevation, egr_size_elevation, gr_param_azimuth, gr_size_azimuth;
2305 : int16_t egr_size_azimuth, elevation_bits_ec, azimuth_bits_ec;
2306 :
2307 : float abs_theta;
2308 : float theta_cb[MAX_NO_THETA];
2309 : int16_t sign_th, no_th;
2310 4338510 : int16_t avg_azimuth_index_upd = 0, use_adapt_avg;
2311 4338510 : int16_t make_gain = 0;
2312 4338510 : int16_t bits_gained = 0;
2313 4338510 : nblocks = q_direction->cfg.nblocks;
2314 :
2315 : /* estimate the number of bits for entropy coding of the direction values */
2316 4338510 : direction_bits_ec = 0;
2317 4338510 : diff_idx_min = DIRAC_DIFFUSE_LEVELS;
2318 4338510 : idx = 0;
2319 4338510 : dist_count = 0;
2320 4338510 : set_zero( avg_direction_vector, 3 );
2321 :
2322 23248522 : for ( i = start_band; i < nbands; i++ )
2323 : {
2324 18910012 : if ( hrmasa_flag )
2325 : {
2326 0 : diff_idx_min = 0; // min( q_direction->band_data[i].energy_ratio_index_mod[0]>>1, diff_idx_min );
2327 : }
2328 : else
2329 : {
2330 18910012 : diff_idx_min = min( q_direction->band_data[i].energy_ratio_index_mod[0], diff_idx_min );
2331 : }
2332 :
2333 18910012 : if ( q_direction->band_data[i].energy_ratio_index_mod[0] > diffuseness_index_max_ec_frame )
2334 : {
2335 : /* estimate the raw part */
2336 4883221 : if ( q_direction->not_in_2D > 0 )
2337 : {
2338 17601552 : for ( j = 0; j < nblocks; j++ )
2339 : {
2340 13377519 : direction_bits_ec += q_direction->band_data[i].bits_sph_idx[j];
2341 : }
2342 : }
2343 : else
2344 : {
2345 2348195 : for ( j = 0; j < nblocks; j++ )
2346 : {
2347 1689007 : direction_bits_ec += ivas_qmetadata_encode_quasi_uniform_length( q_direction->band_data[i].azimuth_index[j], q_direction->band_data[i].azimuth_m_alphabet[j] );
2348 : }
2349 : }
2350 : }
2351 : else
2352 : {
2353 14026791 : dist_count += nblocks;
2354 :
2355 59174871 : for ( j = 0; j < nblocks; j++ )
2356 : {
2357 : /*compute the average direction */
2358 45148080 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[i].azimuth[j], q_direction->band_data[i].elevation[j], direction_vector );
2359 45148080 : v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
2360 : }
2361 : }
2362 : }
2363 :
2364 : /* quantize average elevation and azimuth angles using the best angle spacing and equatorial precision */
2365 4338510 : ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &avg_azimuth, &avg_elevation );
2366 :
2367 4338510 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2368 : {
2369 344846 : avg_elevation_alphabet = no_theta_masa[bits_direction_masa[diff_idx_min] - 3];
2370 344846 : avg_azimuth_alphabet = no_phi_masa[bits_direction_masa[diff_idx_min] - 1][0]; /* average azimuth is quantized on the equatorial plane */
2371 : }
2372 : else
2373 : {
2374 3993664 : avg_elevation_alphabet = no_theta_masa[bits_direction_masa[diff_idx_min] - 3] * 2 - 1;
2375 3993664 : avg_azimuth_alphabet = no_phi_masa[bits_direction_masa[diff_idx_min] - 1][0]; /* average azimuth is quantized on the equatorial plane */
2376 : }
2377 :
2378 4338510 : no_th = no_theta_masa[bits_direction_masa[diff_idx_min] - 3];
2379 :
2380 43644158 : for ( i = 0; i < no_th; i++ )
2381 : {
2382 39305648 : theta_cb[i] = i * delta_theta_masa[bits_direction_masa[diff_idx_min] - 3];
2383 : }
2384 :
2385 4338510 : if ( theta_cb[i - 1] > 90 )
2386 : {
2387 2265438 : theta_cb[i - 1] = 90;
2388 : }
2389 :
2390 4338510 : if ( avg_elevation < 0 )
2391 : {
2392 1068380 : abs_theta = -avg_elevation;
2393 1068380 : sign_th = -1;
2394 : }
2395 : else
2396 : {
2397 3270130 : abs_theta = avg_elevation;
2398 3270130 : sign_th = 1;
2399 : }
2400 :
2401 4338510 : avg_elevation_index = squant( abs_theta, &avg_elevation, theta_cb, no_th );
2402 :
2403 4338510 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2404 : {
2405 344846 : assert( avg_elevation >= 0 );
2406 : }
2407 : else
2408 : {
2409 3993664 : if ( sign_th < 0 )
2410 : {
2411 1068380 : avg_elevation_index = ( avg_elevation_alphabet >> 1 ) - avg_elevation_index;
2412 : }
2413 : else
2414 : {
2415 2925284 : avg_elevation_index += ( avg_elevation_alphabet >> 1 );
2416 : }
2417 3993664 : avg_elevation *= sign_th;
2418 : }
2419 :
2420 4338510 : avg_azimuth_index = (uint16_t) ( quantize_phi( avg_azimuth + 180, 0, &avg_azimuth, avg_azimuth_alphabet ) );
2421 :
2422 : /* Elevation only if not 2D */
2423 4338510 : if ( q_direction->not_in_2D > 0 )
2424 : {
2425 3590103 : avg_elevation_index_initial = avg_elevation_index;
2426 3590103 : elevation_bits_ec_best = MAX16B;
2427 3590103 : avg_elevation_index_best = -1; /* out of range value */
2428 3590103 : gr_param_elevation_best = -1; /* out of range value */
2429 :
2430 14360412 : for ( avg_elevation_offset = 0; avg_elevation_offset < q_direction->cfg.search_effort; avg_elevation_offset++ )
2431 : {
2432 10770309 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2433 : {
2434 703881 : avg_elevation_index = avg_elevation_index_initial + avg_elevation_offset;
2435 : }
2436 : else
2437 : {
2438 10066428 : avg_elevation_index = (uint16_t) ( avg_elevation_index_initial + ivas_qmetadata_dereorder_generic( avg_elevation_offset ) );
2439 : }
2440 10770309 : avg_elevation_index = (uint16_t) ( ( avg_elevation_index + avg_elevation_alphabet ) % avg_elevation_alphabet );
2441 :
2442 10770309 : all_zero_dist_elevation_indexes = 1;
2443 10770309 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2444 : {
2445 703881 : elevation_bits_ec = ivas_qmetadata_encode_quasi_uniform_length( avg_elevation_index, avg_elevation_alphabet );
2446 : }
2447 : else
2448 : {
2449 10066428 : elevation_bits_ec = ivas_qmetadata_encode_quasi_uniform_length( ivas_qmetadata_reorder_generic( avg_elevation_index - ( avg_elevation_alphabet >> 1 ) ), avg_elevation_alphabet );
2450 : }
2451 10770309 : idx = 0;
2452 56082921 : for ( i = start_band; i < nbands; i++ )
2453 : {
2454 45312612 : if ( q_direction->band_data[i].energy_ratio_index_mod[0] <= diffuseness_index_max_ec_frame )
2455 : {
2456 143677506 : for ( j = 0; j < nblocks; j++ )
2457 : {
2458 : /* project the quantized average elevation to the same grid as the current sample */
2459 111036993 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2460 : {
2461 4025967 : avg_elevation_index_projected = ivas_chan_project_elevation_index( avg_elevation_index, avg_elevation_alphabet, q_direction->band_data[i].elevation_m_alphabet[j] );
2462 : }
2463 : else
2464 : {
2465 107011026 : avg_elevation_index_projected = ivas_dirac_project_elevation_index( avg_elevation_index, avg_elevation_alphabet, q_direction->band_data[i].elevation_m_alphabet[j] );
2466 : }
2467 : #ifdef DEBUGGING
2468 : assert( ( 0 <= avg_elevation_index_projected ) && ( avg_elevation_index_projected < q_direction->band_data[i].elevation_m_alphabet[j] ) );
2469 : #endif
2470 :
2471 111036993 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2472 : {
2473 4025967 : if ( q_direction->band_data[i].elevation_index[j] - avg_elevation_index_projected > 0 )
2474 : {
2475 395391 : dist_elevation_indexes[idx] = 2 * ( q_direction->band_data[i].elevation_index[j] - avg_elevation_index_projected ) - 1;
2476 : }
2477 : else
2478 : {
2479 3630576 : dist_elevation_indexes[idx] = -2 * ( q_direction->band_data[i].elevation_index[j] - avg_elevation_index_projected );
2480 : }
2481 : }
2482 : else
2483 : {
2484 107011026 : dist_elevation_indexes[idx] = ivas_qmetadata_reorder_elevation_index( q_direction->band_data[i].elevation_index[j], avg_elevation_index_projected, q_direction->band_data[i].elevation_m_alphabet[j] );
2485 : }
2486 111036993 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2487 : {
2488 4025967 : dist_elevation_alphabets[idx] = 2 * q_direction->band_data[i].elevation_m_alphabet[j] - 1;
2489 : }
2490 : else
2491 : {
2492 107011026 : dist_elevation_alphabets[idx] = q_direction->band_data[i].elevation_m_alphabet[j];
2493 : }
2494 :
2495 111036993 : if ( dist_elevation_indexes[idx] != 0 )
2496 : {
2497 71578870 : all_zero_dist_elevation_indexes = 0;
2498 : }
2499 111036993 : idx++;
2500 : }
2501 : }
2502 : }
2503 :
2504 10770309 : if ( all_zero_dist_elevation_indexes )
2505 : {
2506 1170175 : egr_size_elevation = 0;
2507 1170175 : gr_param_elevation = 4;
2508 : }
2509 : else
2510 : {
2511 9600134 : gr_param_elevation = ivas_qmetadata_get_optimal_gr_param( dist_elevation_indexes, idx, 4, &gr_size_elevation );
2512 9600134 : egr_size_elevation = 0;
2513 117776210 : for ( i = 0; i < idx; i++ )
2514 : {
2515 108176076 : egr_size_elevation += ivas_qmetadata_encode_extended_gr_length( dist_elevation_indexes[i], dist_elevation_alphabets[i], gr_param_elevation );
2516 : }
2517 : #ifdef DEBUGGING
2518 : assert( egr_size_elevation <= gr_size_elevation );
2519 : #endif
2520 : }
2521 10770309 : elevation_bits_ec += ivas_qmetadata_encode_quasi_uniform_length( gr_param_elevation, 4 + 1 ) + egr_size_elevation;
2522 :
2523 10770309 : if ( elevation_bits_ec < elevation_bits_ec_best )
2524 : {
2525 4608173 : elevation_bits_ec_best = elevation_bits_ec;
2526 4608173 : avg_elevation_index_best = avg_elevation_index;
2527 4608173 : gr_param_elevation_best = gr_param_elevation;
2528 55908977 : for ( idx = 0; idx < dist_count; idx++ )
2529 : {
2530 51300804 : dist_elevation_indexes_best[idx] = dist_elevation_indexes[idx];
2531 : }
2532 : }
2533 : }
2534 :
2535 3590103 : direction_bits_ec += elevation_bits_ec_best;
2536 : }
2537 :
2538 : /*Azimuth*/
2539 4338510 : use_adapt_avg = 0;
2540 4338510 : if ( ( nbands - start_band >= 5 ) && ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( nblocks > 1 ) )
2541 : {
2542 96662 : use_adapt_avg = calc_var_azi( q_direction, diffuseness_index_max_ec_frame, avg_azimuth - 180, &avg_azimuth );
2543 96662 : avg_azimuth_index = (uint16_t) ( quantize_phi( avg_azimuth + 180, 0, &avg_azimuth, avg_azimuth_alphabet ) );
2544 : }
2545 4338510 : avg_azimuth_index_initial = avg_azimuth_index; /* avg_azimuth_index;*/
2546 4338510 : azimuth_bits_ec_best = MAX16B;
2547 4338510 : avg_azimuth_index_best = -1; /* out of range value */
2548 4338510 : gr_param_azimuth_best = -1; /* out of range value */
2549 :
2550 17354040 : for ( avg_azimuth_offset = 0; avg_azimuth_offset < q_direction->cfg.search_effort; avg_azimuth_offset++ )
2551 : {
2552 13015530 : set_zero( avg_direction_vector, 3 );
2553 13015530 : avg_azimuth_index = (uint16_t) ( avg_azimuth_index_initial + ivas_qmetadata_dereorder_generic( avg_azimuth_offset ) );
2554 13015530 : avg_azimuth_index = (uint16_t) ( ( avg_azimuth_index + avg_azimuth_alphabet ) % avg_azimuth_alphabet );
2555 13015530 : all_zero_dist_azimuth_indexes = 1;
2556 13015530 : azimuth_bits_ec = ivas_qmetadata_encode_quasi_uniform_length( ivas_qmetadata_reorder_generic( avg_azimuth_index - ( avg_azimuth_alphabet >> 1 ) ), avg_azimuth_alphabet );
2557 :
2558 13015530 : idx = 0;
2559 69745566 : for ( i = start_band; i < nbands; i++ )
2560 : {
2561 56730036 : if ( q_direction->band_data[i].energy_ratio_index_mod[0] <= diffuseness_index_max_ec_frame )
2562 : {
2563 177524613 : for ( j = 0; j < nblocks; j++ )
2564 : {
2565 :
2566 135444240 : if ( ( idx > MASA_LIMIT_IDX_AVG_AZI ) && ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( use_adapt_avg == 1 ) )
2567 : {
2568 1681767 : avg_azimuth_index_projected = ivas_dirac_project_azimuth_index( avg_azimuth_index_upd, avg_azimuth_alphabet, q_direction->band_data[i].azimuth_m_alphabet[j] );
2569 : }
2570 : else
2571 : {
2572 133762473 : if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( use_adapt_avg == 1 ) )
2573 : {
2574 1066737 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[i].azimuth[j], q_direction->band_data[i].elevation[j], direction_vector );
2575 1066737 : if ( idx < 4 )
2576 : {
2577 889236 : v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
2578 : }
2579 : }
2580 : /* project the quantized average azimuth angle to the same grid as the current sample */
2581 133762473 : avg_azimuth_index_projected = ivas_dirac_project_azimuth_index( avg_azimuth_index, avg_azimuth_alphabet, q_direction->band_data[i].azimuth_m_alphabet[j] );
2582 : }
2583 : #ifdef DEBUGGING
2584 : assert( ( 0 <= avg_azimuth_index_projected ) && ( avg_azimuth_index_projected < q_direction->band_data[i].azimuth_m_alphabet[j] ) );
2585 : #endif
2586 135444240 : dist_azimuth_indexes[idx] = ivas_qmetadata_reorder_azimuth_index( ivas_qmetadata_dereorder_generic( q_direction->band_data[i].azimuth_index[j] ) + ( q_direction->band_data[i].azimuth_m_alphabet[j] >> 1 ), avg_azimuth_index_projected, q_direction->band_data[i].azimuth_m_alphabet[j] );
2587 135444240 : dist_azimuth_alphabets[idx] = q_direction->band_data[i].azimuth_m_alphabet[j];
2588 :
2589 135444240 : if ( dist_azimuth_indexes[idx] != 0 )
2590 : {
2591 94305844 : all_zero_dist_azimuth_indexes = 0;
2592 : }
2593 :
2594 135444240 : if ( ( idx >= MASA_LIMIT_IDX_AVG_AZI ) && ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( use_adapt_avg == 1 ) )
2595 : {
2596 1859268 : if ( idx % nblocks == 0 )
2597 : {
2598 464817 : v_multc( avg_direction_vector, 0.5f, avg_direction_vector, 3 );
2599 : }
2600 :
2601 : /*compute the average direction per already coded subband */
2602 1859268 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[i].azimuth[j], q_direction->band_data[i].elevation[j], direction_vector );
2603 1859268 : v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
2604 1859268 : ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &avg_azimuth, &avg_elevation );
2605 1859268 : avg_azimuth_index_upd = quantize_phi( avg_azimuth + 180, 0, &avg_azimuth, avg_azimuth_alphabet );
2606 : }
2607 135444240 : idx++;
2608 : }
2609 : }
2610 : }
2611 :
2612 13015530 : if ( all_zero_dist_azimuth_indexes )
2613 : {
2614 1458367 : egr_size_azimuth = 0;
2615 1458367 : gr_param_azimuth = 5;
2616 : }
2617 : else
2618 : {
2619 : /* estimate the ExtendedGR part for azimuth */
2620 11557163 : gr_param_azimuth = ivas_qmetadata_get_optimal_gr_param( dist_azimuth_indexes, idx, 5, &gr_size_azimuth );
2621 11557163 : egr_size_azimuth = 0;
2622 142534554 : for ( i = 0; i < idx; i++ )
2623 : {
2624 130977391 : egr_size_azimuth += ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes[i], dist_azimuth_alphabets[i], gr_param_azimuth );
2625 : }
2626 : #ifdef DEBUGGING
2627 : assert( egr_size_azimuth <= gr_size_azimuth );
2628 : #endif
2629 : }
2630 :
2631 13015530 : azimuth_bits_ec += ivas_qmetadata_encode_quasi_uniform_length( gr_param_azimuth, 5 + 1 ) + egr_size_azimuth;
2632 :
2633 13015530 : if ( azimuth_bits_ec < azimuth_bits_ec_best )
2634 : {
2635 6034098 : azimuth_bits_ec_best = azimuth_bits_ec;
2636 6034098 : avg_azimuth_index_best = avg_azimuth_index;
2637 6034098 : gr_param_azimuth_best = gr_param_azimuth;
2638 :
2639 72469111 : for ( idx = 0; idx < dist_count; idx++ )
2640 : {
2641 66435013 : dist_azimuth_indexes_best[idx] = dist_azimuth_indexes[idx];
2642 : }
2643 : }
2644 : }
2645 :
2646 4338510 : if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( dist_count > 4 ) && ( gr_param_azimuth_best != 5 ) && ( nblocks > 1 ) )
2647 : {
2648 76919 : azimuth_bits_ec_best += 1;
2649 : }
2650 :
2651 4338510 : direction_bits_ec += azimuth_bits_ec_best;
2652 :
2653 : /*Decision raw or EC*/
2654 : /* one bit is used to indicate whether the direction values are entropy coded or coded raw */
2655 4338510 : if ( direction_bits_ec < direction_bits_raw ) /* entropy coding is better */
2656 : {
2657 :
2658 : /* encode the raw part first */
2659 15120999 : for ( i = start_band; i < nbands; i++ )
2660 : {
2661 12577090 : if ( q_direction->band_data[i].energy_ratio_index_mod[0] > diffuseness_index_max_ec_frame )
2662 : {
2663 3113423 : if ( q_direction->not_in_2D > 0 )
2664 : {
2665 12319732 : for ( j = 0; j < nblocks; j++ )
2666 : {
2667 9547703 : push_next_indice( hMetaData, q_direction->band_data[i].spherical_index[j], q_direction->band_data[i].bits_sph_idx[j] );
2668 : }
2669 : }
2670 : else
2671 : {
2672 1413063 : for ( j = 0; j < nblocks; j++ )
2673 : {
2674 1071669 : ivas_qmetadata_encode_quasi_uniform( hMetaData, q_direction->band_data[i].azimuth_index[j], q_direction->band_data[i].azimuth_m_alphabet[j] );
2675 : }
2676 : }
2677 : }
2678 : }
2679 :
2680 2543909 : if ( nbands > 1 && direction_bits_ec - max_bits > 0 && direction_bits_ec - max_bits < nblocks * nbands )
2681 : {
2682 44416 : make_gain = 1;
2683 : }
2684 :
2685 2543909 : if ( q_direction->not_in_2D > 0 )
2686 : {
2687 : /* encode the ExtendedGR part for elevation */
2688 2024528 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2689 : {
2690 120985 : ivas_qmetadata_encode_quasi_uniform( hMetaData, avg_elevation_index_best, avg_elevation_alphabet );
2691 : }
2692 : else
2693 : {
2694 1903543 : ivas_qmetadata_encode_quasi_uniform( hMetaData, ivas_qmetadata_reorder_generic( avg_elevation_index_best - ( avg_elevation_alphabet >> 1 ) ), avg_elevation_alphabet );
2695 : }
2696 :
2697 2024528 : ivas_qmetadata_encode_quasi_uniform( hMetaData, gr_param_elevation_best, 4 + 1 );
2698 :
2699 2024528 : if ( gr_param_elevation_best != 4 ) /* not all zero */
2700 : {
2701 22713538 : for ( idx = 0; idx < dist_count; idx++ )
2702 : {
2703 21203366 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_elevation_indexes_best[idx], dist_elevation_alphabets[idx], gr_param_elevation_best );
2704 : }
2705 : }
2706 : }
2707 :
2708 : /* encode the ExtendedGR part for azimuth */
2709 2543909 : ivas_qmetadata_encode_quasi_uniform( hMetaData, ivas_qmetadata_reorder_generic( avg_azimuth_index_best - ( avg_azimuth_alphabet >> 1 ) ), avg_azimuth_alphabet );
2710 :
2711 2543909 : ivas_qmetadata_encode_quasi_uniform( hMetaData, gr_param_azimuth_best, 5 + 1 );
2712 :
2713 2543909 : if ( gr_param_azimuth_best != 5 ) /* not all zero */
2714 : {
2715 9208210 : for ( idx = 0; idx < min( nblocks, dist_count ); idx++ )
2716 : {
2717 7264079 : if ( make_gain == 1 && bits_gained < direction_bits_ec - max_bits && dist_azimuth_alphabets[idx] > 40 )
2718 : {
2719 36176 : if ( dist_azimuth_indexes_best[idx] > 1 )
2720 : {
2721 23657 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx] - 2, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2722 23657 : bits_gained += ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best ) -
2723 23657 : ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx] - 2, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2724 : }
2725 12519 : else if ( dist_azimuth_indexes_best[idx] == 1 )
2726 : {
2727 4924 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx] - 1, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2728 4924 : bits_gained += ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best ) -
2729 4924 : ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx] - 1, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2730 : }
2731 : else
2732 : {
2733 7595 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2734 : }
2735 : }
2736 : else
2737 : {
2738 7227903 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2739 : }
2740 : }
2741 :
2742 1944131 : if ( dist_count > nblocks )
2743 : {
2744 1547255 : if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( nblocks > 1 ) )
2745 : {
2746 60954 : push_next_indice( hMetaData, use_adapt_avg, 1 );
2747 : }
2748 20989899 : for ( idx = nblocks; idx < dist_count; idx++ )
2749 : {
2750 19442644 : if ( make_gain == 1 && bits_gained < direction_bits_ec - max_bits && dist_azimuth_alphabets[idx] > 40 )
2751 : {
2752 146008 : if ( dist_azimuth_indexes_best[idx] > 1 )
2753 : {
2754 100671 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx] - 2, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2755 100671 : bits_gained += ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best ) -
2756 100671 : ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx] - 2, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2757 : }
2758 45337 : else if ( dist_azimuth_indexes_best[idx] == 1 )
2759 : {
2760 18451 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx] - 1, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2761 18451 : bits_gained += ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best ) -
2762 18451 : ivas_qmetadata_encode_extended_gr_length( dist_azimuth_indexes_best[idx] - 1, dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2763 : }
2764 : else
2765 : {
2766 26886 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2767 : }
2768 : }
2769 : else
2770 : {
2771 19296636 : ivas_qmetadata_encode_extended_gr( hMetaData, dist_azimuth_indexes_best[idx], dist_azimuth_alphabets[idx], gr_param_azimuth_best );
2772 : }
2773 : }
2774 : }
2775 : }
2776 :
2777 2543909 : direction_bits_ec -= bits_gained;
2778 : }
2779 : else
2780 : {
2781 1794601 : direction_bits_ec = -1;
2782 : }
2783 :
2784 4338510 : return direction_bits_ec;
2785 : }
2786 :
2787 :
2788 : /*-------------------------------------------------------------------------
2789 : * ivas_qmetadata_raw_encode_dir()
2790 : *
2791 : * Main function for raw coding of the directions (writing and bit estimation)
2792 : *------------------------------------------------------------------------*/
2793 :
2794 6133111 : static int16_t ivas_qmetadata_raw_encode_dir(
2795 : BSTR_ENC_HANDLE hMetaData,
2796 : IVAS_QDIRECTION *q_direction,
2797 : const int16_t nbands,
2798 : const int16_t start_band )
2799 : {
2800 : int16_t i, j;
2801 : int16_t direction_bits_raw;
2802 6133111 : int16_t start_bits = 0; /*To avoid compiler warning*/
2803 :
2804 6133111 : direction_bits_raw = 0;
2805 6133111 : if ( hMetaData != NULL )
2806 : {
2807 1794601 : start_bits = hMetaData->nb_bits_tot;
2808 : }
2809 :
2810 6133111 : if ( q_direction->not_in_2D > 0 )
2811 : {
2812 25744509 : for ( i = start_band; i < nbands; i++ )
2813 : {
2814 20588831 : if ( hMetaData != NULL )
2815 : {
2816 22460355 : for ( j = 0; j < q_direction->cfg.nblocks; j++ )
2817 : {
2818 16975728 : push_next_indice( hMetaData, q_direction->band_data[i].spherical_index[j], q_direction->band_data[i].bits_sph_idx[j] );
2819 : }
2820 : }
2821 : else
2822 : {
2823 15104204 : direction_bits_raw += q_direction->cfg.nblocks * q_direction->band_data[i].bits_sph_idx[0];
2824 : }
2825 : }
2826 : }
2827 : else
2828 : {
2829 5631536 : for ( i = start_band; i < nbands; i++ )
2830 : {
2831 16272466 : for ( j = 0; j < q_direction->cfg.nblocks; j++ )
2832 : {
2833 11618363 : if ( hMetaData != NULL )
2834 : {
2835 1793607 : ivas_qmetadata_encode_quasi_uniform( hMetaData, q_direction->band_data[i].azimuth_index[j], q_direction->band_data[i].azimuth_m_alphabet[j] );
2836 : }
2837 : else
2838 : {
2839 9824756 : direction_bits_raw += ivas_qmetadata_encode_quasi_uniform_length(
2840 9824756 : q_direction->band_data[i].azimuth_index[j], q_direction->band_data[i].azimuth_m_alphabet[j] );
2841 : }
2842 : }
2843 : }
2844 : }
2845 :
2846 6133111 : if ( hMetaData != NULL )
2847 : {
2848 1794601 : direction_bits_raw = hMetaData->nb_bits_tot - start_bits;
2849 : }
2850 :
2851 6133111 : return direction_bits_raw;
2852 : }
2853 :
2854 :
2855 : /*-------------------------------------------------------------------------
2856 : * ivas_qmetadata_get_optimal_gr_param()
2857 : *
2858 : *
2859 : *------------------------------------------------------------------------*/
2860 :
2861 21157297 : static int16_t ivas_qmetadata_get_optimal_gr_param(
2862 : uint16_t *unsigned_data,
2863 : const int16_t count,
2864 : const int16_t gr_param_count,
2865 : int16_t *opt_gr_size )
2866 : {
2867 : int16_t opt_bits, bits, idx;
2868 : int16_t opt_gr_param;
2869 : int16_t p;
2870 :
2871 21157297 : opt_bits = MAX16B;
2872 21157297 : opt_gr_param = -1;
2873 :
2874 117343648 : for ( p = 0; p < gr_param_count; p++ )
2875 : {
2876 96186351 : bits = count * ( 1 + p ); /* terminating zero bit and the lsb bits */
2877 1183777610 : for ( idx = 0; idx < count; idx++ )
2878 : {
2879 1087591259 : bits += unsigned_data[idx] >> p; /* leading one bits */
2880 : }
2881 :
2882 96186351 : if ( bits < opt_bits )
2883 : {
2884 32002282 : opt_gr_param = p;
2885 32002282 : opt_bits = bits;
2886 : }
2887 : }
2888 :
2889 21157297 : *opt_gr_size = opt_bits;
2890 :
2891 21157297 : return opt_gr_param;
2892 : }
2893 :
2894 :
2895 : /*-------------------------------------------------------------------------
2896 : * ivas_qmetadata_encode_extended_gr_length()
2897 : *
2898 : *
2899 : *------------------------------------------------------------------------*/
2900 :
2901 : int16_t
2902 320984528 : ivas_qmetadata_encode_extended_gr_length(
2903 : const uint16_t value,
2904 : const uint16_t alphabet_size,
2905 : const int16_t gr_param )
2906 : {
2907 : uint16_t msb_alphabet_size;
2908 : int16_t bits;
2909 : uint16_t msb, lsb;
2910 :
2911 : #ifdef DEBUGGING
2912 : assert( alphabet_size >= 1 );
2913 : assert( value < alphabet_size );
2914 : assert( ( gr_param >= 0 ) && ( gr_param <= 15 ) );
2915 : #endif
2916 :
2917 320984528 : msb_alphabet_size = ( alphabet_size + ( 1U << gr_param ) - 1 ) >> gr_param;
2918 :
2919 320984528 : if ( msb_alphabet_size <= 3 )
2920 : {
2921 : /* EncodeQuasiUniform is always equal or better than Limited GR with up to 3 msb values */
2922 64863204 : bits = ivas_qmetadata_encode_quasi_uniform_length( value, alphabet_size );
2923 : }
2924 : else
2925 : {
2926 256121324 : msb = value >> gr_param;
2927 :
2928 256121324 : bits = msb; /* leading one bits */
2929 256121324 : if ( msb < msb_alphabet_size - 1 )
2930 : {
2931 252216158 : bits += 1 + gr_param; /* terminating zero bit, if not the largest msb (Limited GR), and the lsb bits */
2932 : }
2933 : else
2934 : {
2935 3905166 : lsb = value & ( ( 1U << gr_param ) - 1 );
2936 3905166 : bits += ivas_qmetadata_encode_quasi_uniform_length( lsb, alphabet_size - ( ( msb_alphabet_size - 1 ) << gr_param ) );
2937 : }
2938 : }
2939 :
2940 320984528 : return bits;
2941 : }
2942 :
2943 :
2944 : /*-------------------------------------------------------------------------
2945 : * ivas_qmetadata_reorder_elevation_index()
2946 : *
2947 : *
2948 : *------------------------------------------------------------------------*/
2949 :
2950 159439899 : static int16_t ivas_qmetadata_reorder_elevation_index(
2951 : const int16_t elevation_index,
2952 : const int16_t avg_elevation_index,
2953 : const int16_t elevation_alphabet )
2954 : {
2955 : int16_t elevation_alphabet_half;
2956 : int16_t elevation_index_reordered;
2957 : #ifdef DEBUGGING
2958 : assert( ( elevation_alphabet & 0x01 ) == 1 ); /* elevation_alphabet has the form 2 * n_points + 1 */
2959 : #endif
2960 :
2961 159439899 : elevation_alphabet_half = elevation_alphabet >> 1;
2962 159439899 : elevation_index_reordered = elevation_index - avg_elevation_index;
2963 :
2964 : /* reduce the distance for the index elevation to the range [-elevation_alphabet_half, elevation_alphabet_half] */
2965 159439899 : if ( elevation_index_reordered < -elevation_alphabet_half )
2966 : {
2967 1925941 : elevation_index_reordered += elevation_alphabet;
2968 : }
2969 157513958 : else if ( elevation_index_reordered > elevation_alphabet_half )
2970 : {
2971 2888089 : elevation_index_reordered -= elevation_alphabet;
2972 : }
2973 :
2974 : /* fold reduced signed distance value for converting to unsigned */
2975 159439899 : elevation_index_reordered = ivas_qmetadata_reorder_generic( elevation_index_reordered );
2976 : #ifdef DEBUGGING
2977 : assert( ( 0 <= elevation_index_reordered ) && ( elevation_index_reordered < elevation_alphabet ) );
2978 : #endif
2979 :
2980 159439899 : return elevation_index_reordered;
2981 : }
2982 :
2983 :
2984 : /*-------------------------------------------------------------------------
2985 : * ivas_qmetadata_reorder_azimuth_index()
2986 : *
2987 : *
2988 : *------------------------------------------------------------------------*/
2989 :
2990 135444240 : static int16_t ivas_qmetadata_reorder_azimuth_index(
2991 : const int16_t azimuth_index,
2992 : const int16_t avg_azimuth_index,
2993 : const int16_t azimuth_alphabet )
2994 : {
2995 : int16_t azimuth_alphabet_half;
2996 : int16_t azimuth_index_reordered;
2997 :
2998 135444240 : azimuth_index_reordered = azimuth_index - avg_azimuth_index;
2999 :
3000 135444240 : if ( ( azimuth_alphabet != 1 ) && ( ( azimuth_alphabet & 0x01 ) == 1 ) )
3001 : {
3002 52428873 : return ( ivas_qmetadata_reorder_elevation_index( azimuth_index, avg_azimuth_index, azimuth_alphabet ) );
3003 : }
3004 83015367 : else if ( azimuth_alphabet != 1 )
3005 : {
3006 82740309 : azimuth_alphabet_half = azimuth_alphabet >> 1;
3007 : /* reduce the distance for the index azimuth to the range [-azimuth_alphabet_half, azimuth_alphabet_half - 1] */
3008 82740309 : if ( azimuth_index_reordered < -azimuth_alphabet_half )
3009 : {
3010 1437787 : azimuth_index_reordered += azimuth_alphabet;
3011 : }
3012 81302522 : else if ( azimuth_index_reordered > azimuth_alphabet_half - 1 )
3013 : {
3014 5680892 : azimuth_index_reordered -= azimuth_alphabet;
3015 : }
3016 : /* fold reduced signed distance value for converting to unsigned */
3017 82740309 : azimuth_index_reordered = ivas_qmetadata_reorder_generic( azimuth_index_reordered );
3018 : #ifdef DEBUGGING
3019 : assert( ( 0 <= azimuth_index_reordered ) && ( azimuth_index_reordered < azimuth_alphabet ) );
3020 : #endif
3021 : }
3022 : else
3023 : {
3024 : /* for North and South poles, a single azimuth direction exists */
3025 : #ifdef DEBUGGING
3026 : assert( ( azimuth_index == 0 ) || ( azimuth_index == MASA_NO_INDEX ) );
3027 : #endif
3028 275058 : azimuth_index_reordered = 0;
3029 :
3030 : #ifdef DEBUGGING
3031 : assert( avg_azimuth_index == 0 );
3032 : #endif
3033 : }
3034 :
3035 83015367 : return azimuth_index_reordered;
3036 : }
3037 :
3038 :
3039 : /*-------------------------------------------------------------------------
3040 : * ivas_qmetadata_encode_extended_gr()
3041 : *
3042 : *
3043 : *------------------------------------------------------------------------*/
3044 :
3045 69461259 : void ivas_qmetadata_encode_extended_gr(
3046 : BSTR_ENC_HANDLE hMetaData,
3047 : const uint16_t value,
3048 : const uint16_t alphabet_size,
3049 : const int16_t gr_param )
3050 : {
3051 : uint16_t msb_alphabet_size;
3052 : uint16_t msb, lsb, cnt;
3053 :
3054 : #ifdef DEBUGGING
3055 : assert( alphabet_size >= 1 );
3056 : assert( value < alphabet_size );
3057 : assert( ( gr_param >= 0 ) && ( gr_param <= 31 ) );
3058 : #endif
3059 :
3060 69461259 : msb_alphabet_size = ( alphabet_size + ( 1U << gr_param ) - 1 ) >> gr_param;
3061 :
3062 69461259 : if ( msb_alphabet_size <= 3 )
3063 : {
3064 : /* EncodeQuasiUniform is always equal or better than Limited GR with up to 3 msb values */
3065 5477470 : ivas_qmetadata_encode_quasi_uniform( hMetaData, value, alphabet_size );
3066 : }
3067 : else
3068 : {
3069 63983789 : msb = value >> gr_param;
3070 63983789 : lsb = value & ( ( 1U << gr_param ) - 1 );
3071 :
3072 110131055 : for ( cnt = 0; cnt < msb; cnt++ )
3073 : {
3074 : /* leading one bits */
3075 46147266 : push_next_indice( hMetaData, 1, 1 );
3076 : }
3077 :
3078 63983789 : if ( msb < msb_alphabet_size - 1 )
3079 : {
3080 63549084 : push_next_indice( hMetaData, 0, 1 ); /* terminating zero bit, if not the largest msb (Limited GR) */
3081 63549084 : if ( gr_param > 0 )
3082 : {
3083 17478660 : push_next_indice( hMetaData, lsb, gr_param );
3084 : }
3085 : }
3086 : else
3087 : {
3088 434705 : ivas_qmetadata_encode_quasi_uniform( hMetaData, lsb, alphabet_size - ( ( msb_alphabet_size - 1 ) << gr_param ) );
3089 : }
3090 : }
3091 :
3092 69461259 : return;
3093 : }
3094 :
3095 :
3096 : /*-----------------------------------------------------------------------*
3097 : * Local functions (EC3, requantize directions)
3098 : *-----------------------------------------------------------------------*/
3099 :
3100 30288 : static int16_t truncGR0(
3101 : float *data,
3102 : float *data_hat,
3103 : uint16_t *data_idx,
3104 : const int16_t len,
3105 : const int16_t bits_allowed,
3106 : float *st,
3107 : float *ct )
3108 : {
3109 : int16_t i;
3110 : int16_t bits;
3111 30288 : const int16_t remap3b[8] = { 1, 6, 2, 4, 0, 5, 3, 7 };
3112 30288 : const int16_t remap2b[4] = { 1, 2, 0, 3 };
3113 : float diff[MAX_PARAM_SPATIAL_SUBFRAMES];
3114 : int16_t indx[MAX_PARAM_SPATIAL_SUBFRAMES];
3115 :
3116 30288 : bits = 0;
3117 30288 : set_f( data_hat, 0.0f, len );
3118 30288 : set_f( diff, 10000.0f, len );
3119 :
3120 30288 : if ( bits_allowed <= len + 1 )
3121 : {
3122 646 : bits = min( bits_allowed, len );
3123 646 : set_f( data_hat, 0.0f, len );
3124 : /*set_s(data_idx, 0, len); */
3125 3176 : for ( i = 0; i < bits; i++ )
3126 : {
3127 2530 : if ( fabsf( data[i] ) <= 90 )
3128 : {
3129 2180 : data_idx[i] = 0;
3130 2180 : data_hat[i] = 0.0f;
3131 : }
3132 : else
3133 : {
3134 350 : data_idx[i] = 1;
3135 350 : data_hat[i] = -180.0f;
3136 : }
3137 : }
3138 :
3139 646 : return bits;
3140 : }
3141 :
3142 146350 : for ( i = 0; i < len; i++ )
3143 : {
3144 : #ifdef DEBUGGING
3145 : assert( data_idx[i] < MASA_NO_INDEX );
3146 : #endif
3147 116708 : data_idx[i] = quantize_phi( data[i] + 180, 0, &data_hat[i], 8 );
3148 116708 : data_hat[i] -= 180;
3149 116708 : data_idx[i] = remap3b[data_idx[i]];
3150 116708 : bits += ivas_qmetadata_encode_extended_gr_length( data_idx[i], 8, 0 );
3151 116708 : diff[i] = -st[i] - ct[i] * cosf( PI_OVER_180 * ( data[i] - data_hat[i] ) ); /*(data[i] - data_hat[i])*(data[i] - data_hat[i]);*/
3152 : }
3153 :
3154 29642 : i = 0;
3155 29642 : if ( bits > bits_allowed )
3156 : {
3157 24606 : sort_desc_ind( diff, len, indx );
3158 99445 : for ( i = len - 1; i >= 0; i-- )
3159 : {
3160 86387 : if ( data_idx[indx[i]] > 3 )
3161 : {
3162 49934 : bits -= ivas_qmetadata_encode_extended_gr_length( data_idx[indx[i]], 8, 0 );
3163 49934 : data_idx[indx[i]] = quantize_phi( data[indx[i]] + 180, 0, &data_hat[indx[i]], 4 );
3164 49934 : data_hat[indx[i]] -= 180;
3165 49934 : data_idx[indx[i]] = remap2b[data_idx[indx[i]]];
3166 49934 : bits += ivas_qmetadata_encode_extended_gr_length( data_idx[indx[i]], 8, 0 );
3167 49934 : diff[indx[i]] = -st[i] - ct[i] * cosf( PI_OVER_180 * ( data[indx[i]] - data_hat[indx[i]] ) );
3168 : }
3169 86387 : if ( bits <= bits_allowed )
3170 : {
3171 11548 : break;
3172 : }
3173 : }
3174 : }
3175 :
3176 29642 : if ( bits > bits_allowed )
3177 : {
3178 13058 : sort_desc_ind( diff, len, indx );
3179 27155 : for ( i = len - 1; i >= 0; i-- )
3180 : {
3181 :
3182 27000 : if ( data_idx[indx[i]] > 1 )
3183 : {
3184 23149 : bits -= ivas_qmetadata_encode_extended_gr_length( data_idx[indx[i]], 8, 0 );
3185 23149 : if ( fabsf( data[indx[i]] ) <= 90 )
3186 : {
3187 14098 : data_idx[indx[i]] = 0;
3188 14098 : data_hat[indx[i]] = 0;
3189 : }
3190 : else
3191 : {
3192 9051 : data_idx[indx[i]] = 1;
3193 9051 : data_hat[indx[i]] = -180;
3194 : }
3195 :
3196 23149 : bits += ivas_qmetadata_encode_extended_gr_length( data_idx[indx[i]], 8, 0 );
3197 23149 : diff[indx[i]] = -st[i] - ct[i] * cosf( PI_OVER_180 * ( data[indx[i]] - data_hat[indx[i]] ) );
3198 : }
3199 :
3200 27000 : if ( bits <= bits_allowed )
3201 : {
3202 12903 : break;
3203 : }
3204 : }
3205 : }
3206 :
3207 29642 : if ( bits > bits_allowed )
3208 : {
3209 :
3210 : #ifdef DEBUGGING
3211 : assert( bits_allowed > len );
3212 : for ( i = 0; i < len; i++ )
3213 : {
3214 : assert( data_idx[i] <= 1 );
3215 : }
3216 : #endif
3217 :
3218 155 : sort_desc_ind( diff, len, indx );
3219 180 : for ( i = len - 1; i >= 0; i-- )
3220 : {
3221 :
3222 180 : if ( data_idx[indx[i]] > 0 )
3223 : {
3224 173 : bits -= data_idx[indx[i]];
3225 173 : data_idx[indx[i]] = 0;
3226 173 : data_hat[indx[i]] = 0;
3227 : }
3228 180 : if ( bits <= bits_allowed )
3229 : {
3230 155 : break;
3231 : }
3232 : }
3233 : }
3234 :
3235 29642 : return bits;
3236 : }
3237 :
3238 :
3239 : /*-------------------------------------------------------------------*
3240 : * truncGR0_chan()
3241 : *
3242 : *
3243 : *-------------------------------------------------------------------*/
3244 :
3245 23674 : static int16_t truncGR0_chan(
3246 : const float *data,
3247 : float *data_hat,
3248 : uint16_t *data_idx,
3249 : const int16_t len,
3250 : const int16_t bits_allowed,
3251 : float *st,
3252 : float *ct )
3253 : {
3254 : int16_t i, idx_crt;
3255 : int16_t bits;
3256 : float diff[MAX_PARAM_SPATIAL_SUBFRAMES], sort_diff[MAX_PARAM_SPATIAL_SUBFRAMES], min_diff, sum_diff;
3257 : int16_t indx[MAX_PARAM_SPATIAL_SUBFRAMES];
3258 :
3259 23674 : bits = 0;
3260 23674 : set_f( data_hat, 0.0f, len );
3261 23674 : set_f( diff, 10000.0f, len );
3262 :
3263 23674 : if ( bits_allowed <= len + 1 )
3264 : {
3265 94 : bits = min( bits_allowed, len );
3266 94 : set_f( data_hat, 0.0f, len );
3267 : /*set_s(data_idx, 0, len); */
3268 470 : for ( i = 0; i < bits; i++ )
3269 : {
3270 376 : if ( fabsf( data[i] ) <= 90 )
3271 : {
3272 315 : data_idx[i] = 0;
3273 315 : data_hat[i] = 0.0f;
3274 : }
3275 : else
3276 : {
3277 61 : data_idx[i] = 1;
3278 61 : data_hat[i] = -180.0f;
3279 : }
3280 : }
3281 94 : return bits;
3282 : }
3283 :
3284 117798 : for ( i = 0; i < len; i++ )
3285 : {
3286 : #ifdef DEBUGGING
3287 : assert( data_idx[i] < MASA_NO_INDEX );
3288 : #endif
3289 94218 : data_idx[i] = quantize_phi_chan_lbr( data[i], &data_hat[i], 9 );
3290 :
3291 94218 : bits += ivas_qmetadata_encode_extended_gr_length( data_idx[i], 9, 0 );
3292 94218 : diff[i] = -st[i] - ct[i] * cosf( ( data[i] - data_hat[i] ) * PI_OVER_180 );
3293 : }
3294 :
3295 59944 : while ( bits > bits_allowed )
3296 : {
3297 36364 : min_diff = 1000.0f;
3298 36364 : idx_crt = -1;
3299 36364 : mvr2r( diff, sort_diff, len );
3300 181787 : for ( i = 0; i < len; i++ )
3301 : {
3302 145423 : if ( data_idx[i] > 0 )
3303 : {
3304 111264 : sort_diff[i] = -st[i] - ct[i] * cosf( ( fabsf( data[i] ) - cb_azi_chan[( ( data_idx[i] + 1 ) >> 1 ) - 1] ) * PI_OVER_180 );
3305 111264 : sum_diff = sum_f( sort_diff, len );
3306 :
3307 111264 : if ( sum_diff < min_diff )
3308 : {
3309 65948 : min_diff = sum_diff;
3310 65948 : idx_crt = i;
3311 : }
3312 111264 : sort_diff[i] = diff[i];
3313 : }
3314 : }
3315 :
3316 36364 : if ( idx_crt > -1 )
3317 : {
3318 36364 : bits -= ivas_qmetadata_encode_extended_gr_length( data_idx[idx_crt], 9, 0 );
3319 36364 : data_idx[idx_crt] = quantize_phi_chan_lbr( data[idx_crt], &data_hat[idx_crt], data_idx[idx_crt] + 1 );
3320 36364 : bits += ivas_qmetadata_encode_extended_gr_length( data_idx[idx_crt], 9, 0 );
3321 36364 : diff[idx_crt] = -st[idx_crt] - ct[idx_crt] * cosf( ( data[idx_crt] - data_hat[idx_crt] ) * PI_OVER_180 );
3322 : }
3323 : else
3324 : {
3325 0 : break;
3326 : }
3327 : }
3328 :
3329 23580 : if ( bits > bits_allowed )
3330 : {
3331 0 : mvr2r( diff, sort_diff, len );
3332 0 : sort_desc_ind( sort_diff, len, indx );
3333 :
3334 0 : for ( i = len - 1; i >= 0; i-- )
3335 : {
3336 0 : idx_crt = indx[i];
3337 0 : if ( data_idx[idx_crt] > 0 )
3338 : {
3339 0 : bits -= ivas_qmetadata_encode_extended_gr_length( data_idx[idx_crt], 9, 0 );
3340 0 : data_idx[idx_crt] = 0;
3341 0 : data_hat[idx_crt] = 0;
3342 0 : bits += 1;
3343 : }
3344 :
3345 0 : if ( bits <= bits_allowed )
3346 : {
3347 0 : break;
3348 : }
3349 : }
3350 : }
3351 :
3352 23580 : return bits;
3353 : }
3354 :
3355 :
3356 : /*-------------------------------------------------------------------*
3357 : * common_direction()
3358 : *
3359 : *
3360 : *-------------------------------------------------------------------*/
3361 :
3362 54063 : static int16_t common_direction(
3363 : IVAS_QDIRECTION *q_direction,
3364 : const int16_t band_idx,
3365 : const int16_t len,
3366 : const int16_t bits_allowed,
3367 : BSTR_ENC_HANDLE hMetaData,
3368 : float *elevation_orig,
3369 : float *azimuth_orig )
3370 : {
3371 : int16_t nbits;
3372 : int16_t no_th, i, id_th, k;
3373 : float theta_cb[5];
3374 : float dist, best_dist;
3375 : float ct[MAX_PARAM_SPATIAL_SUBFRAMES], st[MAX_PARAM_SPATIAL_SUBFRAMES];
3376 :
3377 54063 : nbits = 0;
3378 :
3379 54063 : if ( bits_allowed == 0 )
3380 : {
3381 0 : for ( i = 0; i < len; i++ )
3382 : {
3383 0 : q_direction->band_data[band_idx].elevation[i] = 0;
3384 0 : q_direction->band_data[band_idx].azimuth[i] = 0;
3385 : }
3386 :
3387 0 : return 0;
3388 : }
3389 :
3390 54063 : if ( bits_allowed <= len + 1 )
3391 : {
3392 238 : set_f( q_direction->band_data[band_idx].elevation, 0.0f, len );
3393 238 : set_f( st, 0.0f, len );
3394 :
3395 1190 : for ( i = 0; i < len; i++ )
3396 : {
3397 952 : ct[i] = cosf( elevation_orig[i] * PI_OVER_180 );
3398 : }
3399 :
3400 238 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3401 : {
3402 0 : nbits = truncGR0_chan( azimuth_orig, q_direction->band_data[band_idx].azimuth, q_direction->band_data[band_idx].azimuth_index, len, bits_allowed, st, ct );
3403 : }
3404 : else
3405 : {
3406 238 : nbits = truncGR0( azimuth_orig, q_direction->band_data[band_idx].azimuth, q_direction->band_data[band_idx].azimuth_index, len, bits_allowed, st, ct );
3407 : }
3408 :
3409 1190 : for ( i = 0; i < nbits; i++ )
3410 : {
3411 952 : push_next_indice( hMetaData, q_direction->band_data[band_idx].azimuth_index[i], 1 );
3412 : }
3413 :
3414 238 : return nbits;
3415 : }
3416 :
3417 53825 : no_th = no_theta_masa[0] + 3; /* only 5 values for theta; the lat 2 are +/-90 */
3418 :
3419 53825 : theta_cb[0] = 0;
3420 53825 : theta_cb[1] = delta_theta_masa[2];
3421 53825 : theta_cb[2] = -theta_cb[1];
3422 53825 : theta_cb[3] = 90.0f;
3423 53825 : theta_cb[4] = -90.0f;
3424 53825 : best_dist = 900000.0f;
3425 53825 : id_th = 0;
3426 :
3427 322950 : for ( i = 0; i < no_th; i++ )
3428 : {
3429 269125 : dist = 0.0f;
3430 1345625 : for ( k = 0; k < len; k++ )
3431 : {
3432 1076500 : dist += ( elevation_orig[k] - theta_cb[i] ) * ( elevation_orig[k] - theta_cb[i] );
3433 : }
3434 269125 : if ( dist < best_dist )
3435 : {
3436 83291 : id_th = i;
3437 83291 : best_dist = dist;
3438 : }
3439 : }
3440 :
3441 53825 : set_f( q_direction->band_data[band_idx].elevation, theta_cb[id_th], len );
3442 :
3443 269125 : for ( i = 0; i < len; i++ )
3444 : {
3445 215300 : q_direction->band_data[band_idx].elevation_index[i] = id_th;
3446 : }
3447 :
3448 53825 : if ( id_th == 0 )
3449 : {
3450 25712 : push_next_indice( hMetaData, 0, 1 ); /* average theta index */
3451 25712 : set_f( st, 0.0f, len );
3452 :
3453 128560 : for ( i = 0; i < len; i++ )
3454 : {
3455 102848 : ct[i] = cosf( elevation_orig[i] * PI_OVER_180 );
3456 : }
3457 :
3458 25712 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3459 : {
3460 7842 : nbits = truncGR0_chan( azimuth_orig, q_direction->band_data[band_idx].azimuth, q_direction->band_data[band_idx].azimuth_index, len, bits_allowed - 1, st, ct ) + 1;
3461 : }
3462 : else
3463 : {
3464 17870 : nbits = truncGR0( azimuth_orig, q_direction->band_data[band_idx].azimuth, q_direction->band_data[band_idx].azimuth_index, len, bits_allowed - 1, st, ct ) + 1;
3465 : }
3466 : }
3467 : else
3468 : {
3469 28113 : if ( id_th >= 3 )
3470 : {
3471 : /* theta is 90 or -90; only theta is sent */
3472 1353 : push_next_indice( hMetaData, id_th + 11, 4 ); /* average theta index */
3473 1353 : set_f( q_direction->band_data[band_idx].azimuth, 0.0f, len );
3474 6765 : for ( i = 0; i < len; i++ )
3475 : {
3476 5412 : q_direction->band_data[band_idx].azimuth_index[i] = 0;
3477 : }
3478 1353 : nbits = 4;
3479 :
3480 1353 : return nbits;
3481 : }
3482 :
3483 26760 : set_f( st, sinf( theta_cb[id_th] * PI_OVER_180 ), len );
3484 26760 : set_f( ct, cosf( theta_cb[id_th] * PI_OVER_180 ), len );
3485 :
3486 133800 : for ( i = 0; i < len; i++ )
3487 : {
3488 107040 : st[i] *= sinf( elevation_orig[i] * PI_OVER_180 );
3489 107040 : ct[i] *= cosf( elevation_orig[i] * PI_OVER_180 );
3490 107040 : q_direction->band_data[band_idx].azimuth_index[i] = 0;
3491 : }
3492 :
3493 26760 : if ( id_th == 1 )
3494 : {
3495 22473 : push_next_indice( hMetaData, 2, 2 ); /* average theta index */
3496 : }
3497 : else
3498 : {
3499 4287 : assert( id_th == 2 );
3500 4287 : push_next_indice( hMetaData, 6, 3 ); /* average theta index */
3501 : }
3502 :
3503 26760 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3504 : {
3505 15746 : nbits = truncGR0_chan( azimuth_orig, q_direction->band_data[band_idx].azimuth, q_direction->band_data[band_idx].azimuth_index, len, bits_allowed - ( id_th + 1 ), st, ct ) + ( id_th + 1 );
3506 : }
3507 : else
3508 : {
3509 11014 : nbits = truncGR0( azimuth_orig, q_direction->band_data[band_idx].azimuth, q_direction->band_data[band_idx].azimuth_index, len, bits_allowed - ( id_th + 1 ), st, ct ) + ( id_th + 1 );
3510 : }
3511 : }
3512 :
3513 52472 : if ( bits_allowed - ( id_th + 1 ) <= len + 1 )
3514 : {
3515 :
3516 1117 : for ( i = 0; i < min( len, bits_allowed - ( id_th + 1 ) ); i++ )
3517 : {
3518 893 : push_next_indice( hMetaData, q_direction->band_data[band_idx].azimuth_index[i], 1 );
3519 : }
3520 : }
3521 : else
3522 : {
3523 52248 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3524 : {
3525 117470 : for ( i = 0; i < len; i++ )
3526 : {
3527 93976 : ivas_qmetadata_encode_extended_gr( hMetaData, q_direction->band_data[band_idx].azimuth_index[i], 9, 0 );
3528 : }
3529 : }
3530 : else
3531 : {
3532 143770 : for ( i = 0; i < len; i++ )
3533 : {
3534 115016 : ivas_qmetadata_encode_extended_gr( hMetaData, q_direction->band_data[band_idx].azimuth_index[i], 8, 0 );
3535 : }
3536 : }
3537 : }
3538 :
3539 52472 : return nbits;
3540 : }
3541 :
3542 :
3543 : /*-------------------------------------------------------------------*
3544 : * encode_directions_subband()
3545 : *
3546 : *
3547 : *-------------------------------------------------------------------*/
3548 :
3549 667816 : static int16_t encode_directions_subband(
3550 : IVAS_QDIRECTION *q_direction,
3551 : int16_t coding_subbands,
3552 : BSTR_ENC_HANDLE hMetaData,
3553 : const int16_t j,
3554 : const int16_t next_j,
3555 : const int16_t no_subframes,
3556 : const int16_t last_subband,
3557 : int16_t *p_diff,
3558 : float *elevation_orig,
3559 : float *azimuth_orig )
3560 : {
3561 : int16_t allowed_bits, use_vq, max_nb_idx, k;
3562 : int16_t diff;
3563 : float d1, d2;
3564 : int16_t nbits;
3565 : int16_t *bits_dir0;
3566 :
3567 667816 : nbits = 0;
3568 667816 : diff = *p_diff;
3569 667816 : bits_dir0 = (int16_t *) q_direction->band_data[j].bits_sph_idx;
3570 667816 : allowed_bits = sum_s( bits_dir0, no_subframes );
3571 :
3572 667816 : if ( allowed_bits > 0 )
3573 : {
3574 667816 : use_vq = 0;
3575 667816 : max_nb_idx = 0;
3576 :
3577 2391218 : for ( k = 0; k < no_subframes; k++ )
3578 : {
3579 1723402 : if ( bits_dir0[k] > use_vq )
3580 : {
3581 800030 : use_vq = bits_dir0[k];
3582 800030 : max_nb_idx = k;
3583 : }
3584 : }
3585 :
3586 667816 : if ( no_subframes > 1 )
3587 : {
3588 351862 : if ( ( use_vq > 1 ) && ( use_vq <= LIMIT_USE_COMMON ) )
3589 : {
3590 54965 : bits_dir0[max_nb_idx] -= 1;
3591 54965 : allowed_bits -= 1;
3592 : }
3593 : #ifdef DEBUGGING
3594 : assert( bits_dir0[max_nb_idx] > 0 );
3595 : #endif
3596 : }
3597 667816 : if ( no_subframes > 1 )
3598 : {
3599 351862 : if ( use_vq <= LIMIT_USE_COMMON )
3600 : {
3601 : /* calculate the two distances */
3602 55125 : calculate_two_distances( q_direction->band_data[j].elevation, bits_dir0, allowed_bits, no_subframes, &d1, &d2 );
3603 55125 : if ( ( ( use_vq > 1 ) && ( d2 <= d1 ) ) || ( use_vq <= 1 ) )
3604 : {
3605 54063 : if ( use_vq > 1 )
3606 : {
3607 53903 : push_next_indice( hMetaData, 1, 1 ); /* signal VQ */
3608 : }
3609 :
3610 54063 : diff += common_direction( q_direction, j, no_subframes, allowed_bits, hMetaData, elevation_orig, azimuth_orig ) - allowed_bits;
3611 :
3612 54063 : if ( last_subband == 0 )
3613 : {
3614 37931 : update_bits_next_block( q_direction, &diff, next_j, coding_subbands, no_subframes );
3615 : }
3616 : }
3617 : else
3618 : {
3619 1062 : push_next_indice( hMetaData, 0, 1 );
3620 :
3621 1062 : if ( last_subband == 0 )
3622 : {
3623 631 : mvr2r( elevation_orig, q_direction->band_data[j].elevation, no_subframes );
3624 631 : mvr2r( azimuth_orig, q_direction->band_data[j].azimuth, no_subframes );
3625 631 : joint_encoding( q_direction, j, next_j, coding_subbands, bits_dir0, allowed_bits, hMetaData, &diff );
3626 : }
3627 : else
3628 : {
3629 2155 : for ( k = 0; k < no_subframes; k++ )
3630 : {
3631 : /* requantize the direction */
3632 1724 : q_direction->band_data[j].spherical_index[k] = quantize_direction( elevation_orig[k], azimuth_orig[k], q_direction->band_data[j].bits_sph_idx[k], &q_direction->band_data[j].elevation[k], &q_direction->band_data[j].azimuth[k],
3633 1724 : &q_direction->band_data[j].elevation_index[k], &q_direction->band_data[j].azimuth_index[k], q_direction->cfg.mc_ls_setup );
3634 : }
3635 :
3636 431 : if ( allowed_bits > 0 )
3637 : {
3638 431 : nbits = write_fixed_rate_direction( hMetaData, q_direction, j, no_subframes );
3639 : }
3640 : }
3641 : }
3642 : }
3643 : else
3644 : {
3645 : /* there is only joint coding */
3646 296737 : mvr2r( elevation_orig, q_direction->band_data[j].elevation, no_subframes );
3647 296737 : mvr2r( azimuth_orig, q_direction->band_data[j].azimuth, no_subframes );
3648 :
3649 296737 : if ( last_subband == 0 )
3650 : {
3651 190185 : joint_encoding( q_direction, j, next_j, coding_subbands, bits_dir0, allowed_bits, hMetaData, &diff );
3652 : }
3653 : else
3654 : {
3655 532760 : for ( k = 0; k < no_subframes; k++ )
3656 : {
3657 : /* requantize the direction */
3658 426208 : q_direction->band_data[j].spherical_index[k] = quantize_direction( elevation_orig[k], azimuth_orig[k], q_direction->band_data[j].bits_sph_idx[k], &q_direction->band_data[j].elevation[k], &q_direction->band_data[j].azimuth[k],
3659 426208 : &q_direction->band_data[j].elevation_index[k], &q_direction->band_data[j].azimuth_index[k], q_direction->cfg.mc_ls_setup );
3660 : }
3661 :
3662 106552 : if ( allowed_bits > 0 )
3663 : {
3664 106552 : nbits = write_fixed_rate_direction( hMetaData, q_direction, j, no_subframes );
3665 : }
3666 : }
3667 : }
3668 : }
3669 : else
3670 : {
3671 : /* 1 subframe case */
3672 : /* there is only joint coding */
3673 315954 : mvr2r( elevation_orig, q_direction->band_data[j].elevation, no_subframes );
3674 315954 : mvr2r( azimuth_orig, q_direction->band_data[j].azimuth, no_subframes );
3675 :
3676 315954 : if ( last_subband == 0 )
3677 : {
3678 214000 : joint_encoding( q_direction, j, next_j, coding_subbands, bits_dir0, allowed_bits, hMetaData, &diff );
3679 : }
3680 : else
3681 : {
3682 203908 : for ( k = 0; k < no_subframes; k++ )
3683 : {
3684 : /* requantize the direction */
3685 101954 : q_direction->band_data[j].spherical_index[k] = quantize_direction( elevation_orig[k], azimuth_orig[k], q_direction->band_data[j].bits_sph_idx[k], &q_direction->band_data[j].elevation[k], &q_direction->band_data[j].azimuth[k],
3686 101954 : &q_direction->band_data[j].elevation_index[k], &q_direction->band_data[j].azimuth_index[k], q_direction->cfg.mc_ls_setup );
3687 : }
3688 :
3689 101954 : if ( allowed_bits > 0 )
3690 : {
3691 101954 : nbits = write_fixed_rate_direction( hMetaData, q_direction, j, no_subframes );
3692 : }
3693 : }
3694 : }
3695 : }
3696 : else
3697 : {
3698 0 : set_f( q_direction->band_data[j].elevation, 0.0f, no_subframes );
3699 0 : set_f( q_direction->band_data[j].azimuth, 0.0f, no_subframes );
3700 : }
3701 :
3702 667816 : *p_diff = diff;
3703 :
3704 667816 : return nbits;
3705 : }
3706 :
3707 :
3708 : /*-------------------------------------------------------------------*
3709 : * calc_var_azi()
3710 : *
3711 : *
3712 : *-------------------------------------------------------------------*/
3713 :
3714 96662 : static int16_t calc_var_azi(
3715 : const IVAS_QDIRECTION *q_direction,
3716 : const int16_t diffuseness_index_max_ec_frame,
3717 : const float avg_azimuth,
3718 : float *avg_azimuth_out )
3719 : {
3720 : float var_band, dif;
3721 : float avg_direction_vector_band[3], avg_azimuth_band[24], direction_vector[3];
3722 : float avg_elevation;
3723 : int16_t i, j, idx;
3724 :
3725 96662 : idx = 0;
3726 96662 : set_zero( avg_azimuth_band, 24 );
3727 :
3728 579972 : for ( i = 0; i < q_direction->cfg.nbands; i++ )
3729 : {
3730 483310 : set_zero( avg_direction_vector_band, 3 );
3731 483310 : if ( q_direction->band_data[i].energy_ratio_index_mod[0] <= diffuseness_index_max_ec_frame )
3732 : {
3733 1587970 : for ( j = 0; j < q_direction->cfg.nblocks; j++ )
3734 : {
3735 : /*compute the average direction */
3736 1270376 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[i].azimuth[j], q_direction->band_data[i].elevation[j], direction_vector );
3737 1270376 : v_add( avg_direction_vector_band, direction_vector, avg_direction_vector_band, 3 );
3738 : }
3739 317594 : ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector_band, &avg_azimuth_band[idx], &avg_elevation );
3740 317594 : idx++;
3741 : }
3742 : }
3743 :
3744 96662 : var_band = 0.0f;
3745 :
3746 414256 : for ( i = 0; i < idx; i++ )
3747 : {
3748 317594 : dif = ( avg_azimuth_band[idx] - avg_azimuth );
3749 317594 : if ( dif < 0 )
3750 : {
3751 73028 : dif = -dif;
3752 : }
3753 317594 : if ( dif > 180 )
3754 : {
3755 0 : dif = 360 - dif;
3756 : }
3757 :
3758 317594 : var_band += dif * dif;
3759 : }
3760 :
3761 96662 : if ( idx > 0 )
3762 : {
3763 96662 : var_band = var_band / idx;
3764 : }
3765 :
3766 96662 : if ( var_band <= VAR_AZI_THRESH )
3767 : {
3768 22559 : *avg_azimuth_out = avg_azimuth;
3769 22559 : return 0;
3770 : }
3771 : else
3772 : {
3773 74103 : *avg_azimuth_out = avg_azimuth_band[0];
3774 74103 : return 1;
3775 : }
3776 : }
3777 :
3778 :
3779 : /*-------------------------------------------------------------------*
3780 : * requantize_direction_EC_3()
3781 : *
3782 : *
3783 : *-------------------------------------------------------------------*/
3784 :
3785 145179 : static ivas_error requantize_direction_EC_3(
3786 : int16_t *extra_bits,
3787 : IVAS_QDIRECTION *q_direction,
3788 : const int16_t coding_subbands,
3789 : BSTR_ENC_HANDLE hMetaData,
3790 : float elevation_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES],
3791 : float azimuth_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES],
3792 : int16_t *ind_order )
3793 : {
3794 : /* gradually increase the bits following the performance of the EC layer*/
3795 : int16_t j, k;
3796 : int16_t use_vq;
3797 : int16_t diff, allowed_bits, last_j;
3798 : int16_t no_subframes, start_band;
3799 : float st[MAX_PARAM_SPATIAL_SUBFRAMES], ct[MAX_PARAM_SPATIAL_SUBFRAMES];
3800 : int16_t *bits_dir0;
3801 : #ifdef DEBUGGING
3802 : int16_t nbits;
3803 :
3804 : nbits = 0;
3805 : #endif
3806 145179 : no_subframes = q_direction->cfg.nblocks;
3807 145179 : start_band = q_direction->cfg.start_band;
3808 :
3809 145179 : if ( q_direction->not_in_2D > MASA_LIMIT_2D )
3810 : {
3811 138520 : j = ind_order[coding_subbands - 1];
3812 138520 : bits_dir0 = (int16_t *) q_direction->band_data[j].bits_sph_idx;
3813 138520 : allowed_bits = sum_s( bits_dir0, no_subframes );
3814 138520 : last_j = j - ( allowed_bits == 0 );
3815 138520 : diff = 0;
3816 138520 : if ( coding_subbands == 1 )
3817 : {
3818 2493 : last_j = start_band;
3819 : }
3820 581267 : for ( j = 0; j < last_j; j++ )
3821 : {
3822 442747 : k = ind_order[j];
3823 442747 : encode_directions_subband( q_direction, coding_subbands, hMetaData, k, ind_order[j + 1], no_subframes, 0, &diff, elevation_orig[k], azimuth_orig[k] );
3824 : }
3825 :
3826 : /* last subbands to be written in fixed rate */
3827 363589 : for ( j = last_j; j < coding_subbands; j++ )
3828 : {
3829 225069 : k = ind_order[j];
3830 225069 : encode_directions_subband( q_direction, coding_subbands, hMetaData, k, 0, no_subframes, 1, &diff, elevation_orig[k], azimuth_orig[k] );
3831 : }
3832 : }
3833 : else /* 2D */
3834 : {
3835 6659 : diff = 0;
3836 : #ifdef DEBUGGING
3837 : nbits = 0;
3838 : #endif
3839 38039 : for ( j = start_band; j < coding_subbands; j++ )
3840 : {
3841 31380 : bits_dir0 = (int16_t *) q_direction->band_data[j].bits_sph_idx;
3842 31380 : allowed_bits = sum_s( bits_dir0, no_subframes );
3843 31380 : use_vq = 0;
3844 :
3845 101082 : for ( k = 0; k < no_subframes; k++ )
3846 : {
3847 69702 : if ( bits_dir0[k] > use_vq )
3848 : {
3849 34218 : use_vq = bits_dir0[k];
3850 : }
3851 : }
3852 :
3853 31380 : if ( ( use_vq <= 3 ) && ( allowed_bits <= 11 ) )
3854 : {
3855 1252 : set_f( st, 0.0f, no_subframes );
3856 :
3857 4247 : for ( k = 0; k < no_subframes; k++ )
3858 : {
3859 2995 : ct[k] = cosf( elevation_orig[j][k] * PI_OVER_180 );
3860 : }
3861 :
3862 1252 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3863 : {
3864 : #ifdef DEBUGGING
3865 : nbits += truncGR0_chan( azimuth_orig[j], q_direction->band_data[j].azimuth, q_direction->band_data[j].azimuth_index, no_subframes, allowed_bits, st, ct );
3866 : #else
3867 86 : truncGR0_chan( azimuth_orig[j], q_direction->band_data[j].azimuth, q_direction->band_data[j].azimuth_index, no_subframes, allowed_bits, st, ct );
3868 : #endif
3869 : }
3870 : else
3871 : {
3872 : #ifdef DEBUGGING
3873 : nbits += truncGR0( azimuth_orig[j], q_direction->band_data[j].azimuth, q_direction->band_data[j].azimuth_index, no_subframes, allowed_bits, st, ct );
3874 : #else
3875 1166 : truncGR0( azimuth_orig[j], q_direction->band_data[j].azimuth, q_direction->band_data[j].azimuth_index, no_subframes, allowed_bits, st, ct );
3876 : #endif
3877 : }
3878 :
3879 1252 : if ( allowed_bits <= no_subframes + 1 )
3880 : {
3881 1339 : for ( k = 0; k < min( no_subframes, allowed_bits ); k++ )
3882 : {
3883 1061 : push_next_indice( hMetaData, q_direction->band_data[j].azimuth_index[k], 1 );
3884 : }
3885 : }
3886 : else
3887 : {
3888 2908 : for ( k = 0; k < no_subframes; k++ )
3889 : {
3890 1934 : ivas_qmetadata_encode_extended_gr( hMetaData, q_direction->band_data[j].azimuth_index[k], q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ? 9 : 8, 0 );
3891 : }
3892 : }
3893 : }
3894 : else
3895 : {
3896 96835 : for ( k = 0; k < no_subframes; k++ )
3897 : {
3898 : /* requantize the direction */
3899 133414 : q_direction->band_data[j].spherical_index[k] = quantize_direction2D( azimuth_orig[j][k], 1 << q_direction->band_data[j].bits_sph_idx[k], &q_direction->band_data[j].azimuth[k],
3900 66707 : &q_direction->band_data[j].azimuth_index[k], q_direction->cfg.mc_ls_setup );
3901 66707 : q_direction->band_data[j].elevation_index[k] = 0;
3902 : }
3903 : #ifdef DEBUGGING
3904 : nbits += write_fixed_rate_direction( hMetaData, q_direction, j, no_subframes );
3905 : #else
3906 30128 : write_fixed_rate_direction( hMetaData, q_direction, j, no_subframes );
3907 : #endif
3908 : }
3909 : }
3910 : }
3911 : #ifdef DEBUGGING
3912 : if ( diff > 0 )
3913 : {
3914 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Not enough bits in requantize_direction_EC_3(); %d bits written \n", nbits );
3915 : }
3916 : #endif
3917 :
3918 145179 : *extra_bits = -diff;
3919 :
3920 145179 : return IVAS_ERR_OK;
3921 : }
3922 :
3923 :
3924 : /*-------------------------------------------------------------------*
3925 : * write_fixed_rate_direction()
3926 : *
3927 : * writing of the spherical indexes
3928 : *-------------------------------------------------------------------*/
3929 :
3930 : /*! r: number of bits written */
3931 529239 : static int16_t write_fixed_rate_direction(
3932 : BSTR_ENC_HANDLE hMetaData, /* i : MASA metadata structure */
3933 : IVAS_QDIRECTION *qdirection, /* i/o: quantized directional parameters */
3934 : const int16_t j_idx, /* i : index of subband for which the data is written */
3935 : const int16_t len /* i : number of data */
3936 : )
3937 : {
3938 : int16_t nbits, i;
3939 :
3940 529239 : nbits = 0;
3941 1659030 : for ( i = 0; i < len; i++ )
3942 : {
3943 1129791 : push_next_indice( hMetaData, qdirection->band_data[j_idx].spherical_index[i], qdirection->band_data[j_idx].bits_sph_idx[i] );
3944 1129791 : nbits += qdirection->band_data[j_idx].bits_sph_idx[i];
3945 : }
3946 :
3947 529239 : return nbits;
3948 : }
3949 :
3950 :
3951 : /*-------------------------------------------------------------------*
3952 : * joint_encoding()
3953 : *
3954 : * joint encoding of elevation and azimuth
3955 : *-------------------------------------------------------------------*/
3956 :
3957 404816 : static void joint_encoding(
3958 : IVAS_QDIRECTION *q_direction, /* i/o: quantized directional parameters */
3959 : const int16_t j, /* i : subband index */
3960 : const int16_t next_j, /* i : next subband index */
3961 : const int16_t coding_subbands, /* i : total number of subband */
3962 : int16_t *bits_dir0, /* i/o: number of bits for each tile in each subband */
3963 : const int16_t allowed_bits, /* i : maximum number of bits available for the current subband */
3964 : BSTR_ENC_HANDLE hMetaData, /* i/o: metadata bitstream handle */
3965 : int16_t *diff /* o : bits to be given/taken to next subband */
3966 : )
3967 : {
3968 : int16_t k;
3969 : int16_t GR_ord_azimuth, use_context, GR_ord_elevation;
3970 : uint8_t method;
3971 : int16_t nbits;
3972 : int16_t same;
3973 : uint16_t data[MAX_PARAM_SPATIAL_SUBFRAMES];
3974 404816 : int16_t len_data = 0;
3975 : int16_t no_symb_ele[MAX_PARAM_SPATIAL_SUBFRAMES];
3976 :
3977 1382080 : for ( k = 0; k < q_direction->cfg.nblocks; k++ )
3978 : {
3979 977264 : q_direction->band_data[j].bits_sph_idx[k] = bits_dir0[k];
3980 :
3981 : /* requantize the direction */
3982 1954528 : q_direction->band_data[j].spherical_index[k] = quantize_direction( q_direction->band_data[j].elevation[k], q_direction->band_data[j].azimuth[k], q_direction->band_data[j].bits_sph_idx[k], &q_direction->band_data[j].elevation[k],
3983 977264 : &q_direction->band_data[j].azimuth[k], &q_direction->band_data[j].elevation_index[k], &q_direction->band_data[j].azimuth_index[k], q_direction->cfg.mc_ls_setup );
3984 :
3985 977264 : if ( bits_dir0[k] >= 3 )
3986 : {
3987 970061 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3988 : {
3989 268591 : q_direction->band_data[j].elevation_m_alphabet[k] = no_theta_masa[bits_dir0[k] - 3];
3990 268591 : q_direction->band_data[j].azimuth_m_alphabet[k] = no_phi_masa[bits_dir0[k] - 1][q_direction->band_data[j].elevation_index[k]];
3991 : }
3992 : else
3993 : {
3994 701470 : q_direction->band_data[j].elevation_m_alphabet[k] = no_theta_masa[bits_dir0[k] - 3] * 2 - 1;
3995 701470 : q_direction->band_data[j].azimuth_m_alphabet[k] = no_phi_masa[bits_dir0[k] - 1][( q_direction->band_data[j].elevation_index[k] + 1 ) >> 1];
3996 : }
3997 970061 : assert( q_direction->band_data[j].elevation_index[k] != MASA_NO_INDEX );
3998 : }
3999 7203 : else if ( bits_dir0[k] > 0 )
4000 : {
4001 7203 : q_direction->band_data[j].elevation_m_alphabet[k] = 1;
4002 7203 : q_direction->band_data[j].azimuth_m_alphabet[k] = no_phi_masa[bits_dir0[k] - 1][0];
4003 : }
4004 : else
4005 : {
4006 0 : q_direction->band_data[j].elevation_m_alphabet[k] = 1;
4007 0 : q_direction->band_data[j].azimuth_m_alphabet[k] = 1;
4008 : }
4009 : }
4010 :
4011 1382080 : for ( k = 0; k < q_direction->cfg.nblocks; k++ )
4012 : {
4013 977264 : if ( q_direction->band_data[j].bits_sph_idx[k] <= 2 )
4014 : {
4015 7203 : q_direction->band_data[j].elevation_index[k] = MASA_NO_INDEX;
4016 : }
4017 : else
4018 : {
4019 970061 : no_symb_ele[len_data] = q_direction->band_data[j].elevation_m_alphabet[k];
4020 970061 : data[len_data++] = q_direction->band_data[j].elevation_index[k];
4021 : }
4022 : }
4023 :
4024 :
4025 : /* encode indexes for current subband and count the number of bits */
4026 :
4027 404816 : if ( q_direction->cfg.nblocks == 1 && q_direction->band_data[j].bits_sph_idx[0] <= MASA_MIN_BITS_TF + 1 )
4028 : {
4029 : /* encode with fixed rate only if only one subframe and very low number of bits */
4030 113606 : nbits = write_fixed_rate_direction( hMetaData, q_direction, j, q_direction->cfg.nblocks );
4031 : }
4032 : else
4033 : {
4034 :
4035 291210 : if ( len_data > 0 )
4036 : {
4037 291206 : nbits = GR_bits_new( data, no_symb_ele, len_data, MASA_GR_ORD_EL, 1, &GR_ord_elevation );
4038 : }
4039 : else
4040 : {
4041 4 : nbits = 0;
4042 4 : GR_ord_elevation = MASA_GR_ORD_EL;
4043 : }
4044 :
4045 291210 : same = 1;
4046 863658 : for ( k = 1; k < q_direction->cfg.nblocks; k++ )
4047 : {
4048 572448 : if ( q_direction->band_data[j].elevation_index[k] != q_direction->band_data[j].elevation_index[0] )
4049 : {
4050 177381 : same = 0;
4051 : }
4052 : }
4053 291210 : if ( same == 1 && q_direction->band_data[j].elevation_index[0] < 4 )
4054 : {
4055 179119 : nbits = 3;
4056 : }
4057 : else
4058 : {
4059 112091 : same = 0;
4060 112091 : nbits += 1;
4061 : }
4062 :
4063 582420 : nbits += GR_bits_azimuth_context( q_direction->band_data[j].azimuth_index,
4064 291210 : q_direction->band_data[j].azimuth_m_alphabet, q_direction->cfg.nblocks, MASA_GR_ORD_AZ,
4065 291210 : q_direction->band_data[j].bits_sph_idx, &GR_ord_azimuth, &use_context );
4066 :
4067 291210 : if ( allowed_bits == 0 )
4068 : {
4069 0 : nbits = 0;
4070 : }
4071 : else
4072 : {
4073 291210 : if ( nbits >= allowed_bits )
4074 : {
4075 176568 : nbits = allowed_bits + 1; /* fixed rate encoding */
4076 176568 : method = 1;
4077 176568 : push_next_indice( hMetaData, method, 1 );
4078 :
4079 : /* write current subband data */
4080 176568 : nbits = 1 + write_fixed_rate_direction( hMetaData, q_direction, j, q_direction->cfg.nblocks );
4081 : }
4082 : else
4083 : {
4084 114642 : nbits += 1; /* EC coding */
4085 114642 : method = 0;
4086 114642 : push_next_indice( hMetaData, method, 1 );
4087 :
4088 : /* write current subband data */
4089 114642 : write_ec_direction( &nbits, hMetaData, q_direction, j, q_direction->cfg.nblocks, GR_ord_elevation, GR_ord_azimuth, use_context, same );
4090 114642 : nbits++;
4091 : }
4092 : }
4093 : }
4094 404816 : *diff += nbits - allowed_bits;
4095 404816 : update_bits_next_block( q_direction, diff, next_j, coding_subbands, q_direction->cfg.nblocks );
4096 :
4097 404816 : return;
4098 : }
4099 :
4100 :
4101 : /*-------------------------------------------------------------------*
4102 : * calculate_two_distances()
4103 : *
4104 : * calculate estimated distortions if encoding with VQ or not
4105 : *-------------------------------------------------------------------*/
4106 :
4107 55125 : static void calculate_two_distances(
4108 : float *el, /* i : elevation values */
4109 : int16_t *bits, /* i : number of bits for each tile */
4110 : const int16_t total_bits, /* i : total number of bits for subband */
4111 : const int16_t len, /* i : number of tiles */
4112 : float *p_d1, /* o : first distortion */
4113 : float *p_d2 /* o : second distortion */
4114 : )
4115 : {
4116 : int16_t i;
4117 : float d1, d2, el_av;
4118 55125 : const float cos_delta_phi_cb[] = { 0.848f, 0.8988f, 0.9272f, 0.9563f, 0.9744f, 0.9816f, 0.9877f, 0.9925f };
4119 : float var_el;
4120 :
4121 55125 : d1 = 0.0f;
4122 55125 : d2 = 0.0f;
4123 :
4124 55125 : el_av = mean( el, len );
4125 55125 : if ( total_bits > 9 )
4126 : {
4127 267490 : for ( i = 0; i < len; i++ )
4128 : {
4129 213992 : if ( bits[i] > 2 )
4130 : {
4131 151469 : if ( fabsf( el[i] ) < fabsf( ( fabsf( el[i] ) - 45 ) ) )
4132 : {
4133 : /* el_hat = 0*/
4134 77987 : if ( bits[i] == 3 )
4135 : {
4136 77987 : d1 += 1 - 0.7f * cosf( el[i] * PI_OVER_180 );
4137 : }
4138 : else
4139 : {
4140 0 : d1 += 1 - 0.92f * cosf( el[i] * PI_OVER_180 );
4141 : }
4142 : }
4143 : else
4144 : {
4145 73482 : if ( bits[i] == 3 )
4146 : {
4147 73482 : d1 += 1 - sinf( el[i] * PI_OVER_180 ) * 0.7f * sign( el[i] );
4148 : }
4149 : else
4150 : {
4151 0 : d1 += 1 - 0.7f * 0.92f * cosf( el[i] * PI_OVER_180 ) - sinf( fabsf( el[i] * PI_OVER_180 ) ) * 0.7f;
4152 : }
4153 : }
4154 : }
4155 : else
4156 : {
4157 62523 : if ( bits[i] == 2 )
4158 : {
4159 62523 : d1 += 1 - cosf( el[i] * PI_OVER_180 ) * 0.7f;
4160 : }
4161 : else
4162 : {
4163 0 : d1 += 1;
4164 : }
4165 : }
4166 213992 : d2 += 1 - sinf( el_av * PI_OVER_180 ) * sinf( el[i] * PI_OVER_180 ) - cosf( el[i] * PI_OVER_180 ) * cosf( el_av * PI_OVER_180 ) * cos_delta_phi_cb[total_bits - 9];
4167 : }
4168 : }
4169 :
4170 55125 : var_el = var( el, len );
4171 55125 : if ( var_el > 1300.0f )
4172 : {
4173 1064 : d2 = d1 + 0.1f;
4174 : }
4175 :
4176 55125 : *p_d1 = d1;
4177 55125 : *p_d2 = d2;
4178 :
4179 55125 : return;
4180 : }
4181 :
4182 :
4183 : /*-------------------------------------------------------------------*
4184 : * write_ec_direction()
4185 : *
4186 : * write metadata using entropy encoding
4187 : *-------------------------------------------------------------------*/
4188 :
4189 : /*! r: number of bits written */
4190 114642 : static ivas_error write_ec_direction(
4191 : int16_t *num_bits_written, /* o : Number of bits written */
4192 : BSTR_ENC_HANDLE hMetaData, /* i : MASA metadata structure */
4193 : IVAS_QDIRECTION *qdirection, /* i : quantized directional info */
4194 : const int16_t j_idx, /* i : index of subband to encode and write */
4195 : const int16_t len, /* i : number of tiles */
4196 : const int16_t GR_ord_elevation, /* i : GR order for elevation encoding */
4197 : const int16_t GR_ord_azimuth, /* i : GR order for azimuth encoding */
4198 : const int16_t use_context, /* i : flag for context usiage in azimuth encoding */
4199 : const int16_t same /* i : flag if elevation indexes are the same or not */
4200 : )
4201 : {
4202 : int16_t i, nbits, bits_crt, nr_NO_INDEX;
4203 : uint16_t data;
4204 : int16_t min_val, max_val;
4205 :
4206 114642 : nr_NO_INDEX = 0;
4207 :
4208 114642 : nbits = 0;
4209 :
4210 : /* write elevation */
4211 558708 : for ( i = 0; i < len; i++ )
4212 : {
4213 444066 : data = qdirection->band_data[j_idx].elevation_index[i];
4214 444066 : if ( ( data == MASA_NO_INDEX ) || ( qdirection->band_data[j_idx].bits_sph_idx[i] == 0 ) )
4215 : {
4216 11 : nr_NO_INDEX += 1;
4217 : }
4218 : }
4219 :
4220 114642 : if ( nr_NO_INDEX < len )
4221 : {
4222 114642 : if ( same == 1 )
4223 : {
4224 88548 : push_next_indice( hMetaData, 1, 1 );
4225 88548 : nbits += 1;
4226 88548 : push_next_indice( hMetaData, qdirection->band_data[j_idx].elevation_index[0], 2 );
4227 88548 : nbits += 2;
4228 : }
4229 : else
4230 : {
4231 26094 : push_next_indice( hMetaData, 0, 1 );
4232 26094 : nbits += 1;
4233 :
4234 26094 : push_next_indice( hMetaData, 1 - ( GR_ord_elevation == MASA_GR_ORD_EL ), 1 );
4235 26094 : nbits += 1;
4236 :
4237 130464 : for ( i = 0; i < len; i++ )
4238 : {
4239 104370 : data = qdirection->band_data[j_idx].elevation_index[i];
4240 104370 : if ( data < MASA_NO_INDEX )
4241 : {
4242 104359 : bits_crt = hMetaData->nb_bits_tot;
4243 104359 : ivas_qmetadata_encode_extended_gr( hMetaData, data, qdirection->band_data[j_idx].elevation_m_alphabet[i], GR_ord_elevation );
4244 104359 : nbits += hMetaData->nb_bits_tot - bits_crt;
4245 : }
4246 : }
4247 : }
4248 : }
4249 :
4250 : /* write azimuth */
4251 114642 : if ( use_context < 0 )
4252 : {
4253 114642 : if ( use_context == -1 )
4254 : {
4255 : /* regular GR coding */
4256 49866 : push_next_indice( hMetaData, 0, 1 );
4257 49866 : nbits += 1;
4258 49866 : push_next_indice( hMetaData, 1 - ( GR_ord_azimuth == MASA_GR_ORD_AZ ), 1 );
4259 49866 : nbits += 1;
4260 :
4261 234828 : for ( i = 0; i < len; i++ )
4262 : {
4263 184962 : data = qdirection->band_data[j_idx].azimuth_index[i];
4264 184962 : if ( data < MASA_NO_INDEX )
4265 : {
4266 184457 : bits_crt = hMetaData->nb_bits_tot;
4267 184457 : ivas_qmetadata_encode_extended_gr( hMetaData, data, qdirection->band_data[j_idx].azimuth_m_alphabet[i], GR_ord_azimuth );
4268 184457 : nbits += hMetaData->nb_bits_tot - bits_crt;
4269 : }
4270 : }
4271 : }
4272 64776 : else if ( use_context == -2 )
4273 : {
4274 : /* min removed GR coding */
4275 64695 : push_next_indice( hMetaData, 1, 1 );
4276 64695 : nbits += 1;
4277 64695 : push_next_indice( hMetaData, 1 - ( GR_ord_azimuth == MASA_GR_ORD_AZ - 1 ), 1 );
4278 64695 : nbits += 1;
4279 :
4280 : /* find min */
4281 64695 : min_val = MASA_NO_INDEX;
4282 323475 : for ( i = 0; i < len; i++ )
4283 : {
4284 258780 : if ( qdirection->band_data[j_idx].azimuth_index[i] < min_val )
4285 : {
4286 76568 : min_val = qdirection->band_data[j_idx].azimuth_index[i];
4287 : }
4288 : }
4289 :
4290 : #ifdef DEBUGGING
4291 : if ( min_val == MASA_NO_INDEX )
4292 : {
4293 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "No azimuth data to be coded in write_ec_direction()" );
4294 : }
4295 : #endif
4296 :
4297 : /* write min*/
4298 64695 : bits_crt = hMetaData->nb_bits_tot;
4299 64695 : maximum_s( qdirection->band_data[j_idx].azimuth_m_alphabet, len, &max_val );
4300 64695 : ivas_qmetadata_encode_extended_gr( hMetaData, min_val, max_val, MASA_GR_ORD_AZ );
4301 64695 : nbits += hMetaData->nb_bits_tot - bits_crt;
4302 :
4303 323475 : for ( i = 0; i < len; i++ )
4304 : {
4305 258780 : data = qdirection->band_data[j_idx].azimuth_index[i] - min_val;
4306 258780 : if ( data < MASA_NO_INDEX - min_val )
4307 : {
4308 258740 : bits_crt = hMetaData->nb_bits_tot;
4309 258740 : ivas_qmetadata_encode_extended_gr( hMetaData, data, qdirection->band_data[j_idx].azimuth_m_alphabet[i], GR_ord_azimuth );
4310 258740 : nbits += hMetaData->nb_bits_tot - bits_crt;
4311 : }
4312 : }
4313 : }
4314 : #ifdef DEBUGGING
4315 : else
4316 : {
4317 : if ( use_context != -3 )
4318 : {
4319 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong use_context value in write_ec_direction()" );
4320 : }
4321 : }
4322 : #endif
4323 : }
4324 : else
4325 : {
4326 0 : for ( i = 0; i < len; i++ )
4327 : {
4328 0 : data = qdirection->band_data[j_idx].azimuth_index[i];
4329 0 : if ( data < MASA_NO_INDEX )
4330 : {
4331 0 : switch ( qdirection->band_data[j_idx].bits_sph_idx[i] )
4332 : {
4333 0 : case 0:
4334 0 : break;
4335 0 : case 1:
4336 0 : nbits += 1;
4337 0 : push_next_indice( hMetaData, data, 1 );
4338 0 : break;
4339 0 : case 2:
4340 0 : bits_crt = hMetaData->nb_bits_tot;
4341 0 : ivas_qmetadata_encode_extended_gr( hMetaData, data, qdirection->band_data[j_idx].azimuth_m_alphabet[i], MASA_GR_ORD_AZ - 1 );
4342 0 : nbits += hMetaData->nb_bits_tot - bits_crt;
4343 0 : break;
4344 0 : default:
4345 0 : bits_crt = hMetaData->nb_bits_tot;
4346 0 : ivas_qmetadata_encode_extended_gr( hMetaData, data, qdirection->band_data[j_idx].azimuth_m_alphabet[i], MASA_GR_ORD_AZ );
4347 0 : nbits += hMetaData->nb_bits_tot - bits_crt;
4348 0 : break;
4349 : }
4350 0 : }
4351 : }
4352 : }
4353 :
4354 114642 : *num_bits_written = nbits;
4355 114642 : return IVAS_ERR_OK;
4356 : }
4357 :
4358 :
4359 : /*-----------------------------------------------------------------------*
4360 : * Local functions (coherence Q and coding)
4361 : *-----------------------------------------------------------------------*/
4362 :
4363 : /*! r: index */
4364 788970 : static uint64_t create_combined_index(
4365 : uint16_t *idx_dct, /* i : indexes to combine */
4366 : const int16_t len, /* i : number of indexes */
4367 : const int16_t *no_cb_vec /* i : how many codewords for each position */
4368 : )
4369 : {
4370 : int16_t i;
4371 : uint64_t idx, base;
4372 :
4373 788970 : base = 1;
4374 788970 : idx = 0;
4375 6810651 : for ( i = 0; i < len; i++ )
4376 : {
4377 6021681 : idx += base * idx_dct[i];
4378 6021681 : base *= no_cb_vec[i];
4379 : }
4380 :
4381 788970 : return idx;
4382 : }
4383 :
4384 :
4385 : /*-----------------------------------------------------------------------*
4386 : * encoding DCT0 coeffs with joint index
4387 : *-----------------------------------------------------------------------*/
4388 :
4389 : /*! r: number of bits written */
4390 466826 : static int16_t encode_coherence_indexesDCT0(
4391 : uint16_t *idx_dct, /* i : indexes to be encoded */
4392 : const int16_t len, /* i : number of indexes */
4393 : int16_t *no_cb_vec, /* i : number of codewords for each position */
4394 : BSTR_ENC_HANDLE hMetaData,
4395 : const int16_t indice_coherence,
4396 : const int16_t nbits,
4397 : const int16_t nbits1 )
4398 : {
4399 : int16_t i;
4400 : uint64_t idx;
4401 : int16_t no_idx16;
4402 : int16_t k;
4403 : int16_t half_len, j;
4404 : uint64_t idx1;
4405 :
4406 : /* calculate bits for dct0 components with joint encoding */
4407 466826 : if ( nbits1 > 0 )
4408 : {
4409 10111 : half_len = len / 2;
4410 10111 : idx = create_combined_index( idx_dct, half_len, no_cb_vec );
4411 10111 : idx1 = create_combined_index( &idx_dct[half_len], half_len, &no_cb_vec[half_len] );
4412 : }
4413 : else
4414 : {
4415 456715 : idx = create_combined_index( idx_dct, len, no_cb_vec );
4416 456715 : idx1 = 0;
4417 : }
4418 :
4419 466826 : if ( nbits % 16 == 0 )
4420 : {
4421 8832 : no_idx16 = nbits / 16;
4422 : }
4423 : else
4424 : {
4425 457994 : no_idx16 = (int16_t) round_f( ( nbits / 16.0f + 0.5f ) );
4426 : }
4427 :
4428 466826 : k = nbits;
4429 466826 : i = 0;
4430 575307 : for ( i = 0; i < no_idx16 - 1; i++ )
4431 : {
4432 108481 : k -= 16;
4433 108481 : hMetaData->ind_list[indice_coherence + i].value = ( ( idx >> k ) & 65535 ); /* 16 bits */
4434 : }
4435 466826 : hMetaData->ind_list[indice_coherence + i].value = ( idx & ( ( 1 << k ) - 1 ) );
4436 :
4437 466826 : if ( nbits1 > 0 )
4438 : {
4439 10111 : if ( nbits1 % 16 == 0 )
4440 : {
4441 268 : no_idx16 = nbits1 / 16;
4442 : }
4443 : else
4444 : {
4445 9843 : no_idx16 = (int16_t) round_f( ( nbits1 / 16.0f + 0.5f ) );
4446 : }
4447 :
4448 10111 : k = nbits1;
4449 :
4450 23070 : for ( j = i + 1; j < no_idx16 + i; j++ )
4451 : {
4452 12959 : k -= 16;
4453 12959 : hMetaData->ind_list[indice_coherence + j].value = ( ( idx1 >> k ) & 65535 ); /* 16 bits */
4454 : }
4455 10111 : hMetaData->ind_list[indice_coherence + j].value = ( idx1 & ( ( 1 << k ) - 1 ) );
4456 : }
4457 :
4458 466826 : return nbits + nbits1;
4459 : }
4460 :
4461 :
4462 : /*-------------------------------------------------------------------*
4463 : * coherence_coding_length()
4464 : *
4465 : *
4466 : *-------------------------------------------------------------------*/
4467 :
4468 943012 : static int16_t coherence_coding_length(
4469 : const uint16_t *idx_sur_coh_shift,
4470 : const uint8_t idx_shift_len,
4471 : const int16_t coding_subbands,
4472 : const int16_t *no_cv,
4473 : uint16_t *mr_idx,
4474 : int16_t *no_cv_shift,
4475 : int16_t *p_min_idx,
4476 : int16_t *GR_ord,
4477 : int16_t *nbits_fr,
4478 : int16_t *nbits_fr1 )
4479 : {
4480 : int16_t half_coding_subbands;
4481 : int16_t j;
4482 : int16_t nbits;
4483 : uint64_t no_cb;
4484 : int16_t min_idx;
4485 :
4486 943012 : half_coding_subbands = 0;
4487 :
4488 943012 : if ( sum_s( no_cv, coding_subbands ) > MASA_COH_LIMIT_2IDX )
4489 : {
4490 :
4491 100332 : no_cb = 1;
4492 100332 : half_coding_subbands = coding_subbands / 2;
4493 1210380 : for ( j = 0; j < half_coding_subbands; j++ )
4494 : {
4495 1110048 : no_cb *= no_cv[j];
4496 : }
4497 100332 : *nbits_fr = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
4498 100332 : no_cb = 1;
4499 1215810 : for ( j = half_coding_subbands; j < coding_subbands; j++ )
4500 : {
4501 1115478 : no_cb *= no_cv[j];
4502 : }
4503 100332 : *nbits_fr1 = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
4504 : }
4505 : else
4506 : {
4507 842680 : no_cb = 1;
4508 10107994 : for ( j = 0; j < coding_subbands; j++ )
4509 : {
4510 9265314 : no_cb *= no_cv[j];
4511 : }
4512 842680 : *nbits_fr = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
4513 842680 : *nbits_fr1 = 0;
4514 : }
4515 :
4516 :
4517 943012 : minimum_s( (const int16_t *) idx_sur_coh_shift, (int16_t) idx_shift_len, &min_idx );
4518 11485584 : for ( j = 0; j < idx_shift_len; j++ )
4519 : {
4520 10542572 : mr_idx[j] = idx_sur_coh_shift[j] - min_idx;
4521 10542572 : no_cv_shift[j] -= min_idx;
4522 : }
4523 943012 : nbits = min_idx + 1 + GR_bits_new( mr_idx, no_cv_shift, idx_shift_len, *GR_ord, 1, GR_ord );
4524 943012 : *p_min_idx = min_idx;
4525 :
4526 943012 : return nbits;
4527 : }
4528 :
4529 :
4530 : /*-------------------------------------------------------------------*
4531 : * encode_spread_coherence_1sf()
4532 : *
4533 : * Encoding spread coherence for 1 subframe bands
4534 : *-------------------------------------------------------------------*/
4535 :
4536 : /*! r: number of bits written */
4537 230094 : static int16_t encode_spread_coherence_1sf(
4538 : IVAS_QMETADATA *q_metadata, /* i : quantized metadata */
4539 : const int16_t idx_d, /* i : current direction index */
4540 : BSTR_ENC_HANDLE hMasaMetaData, /* i/o: metadata bitstream handle */
4541 : const int16_t hrmasa_flag /* i : flag indicating high-rate MASA MD coding */
4542 : )
4543 : {
4544 : int16_t i, j, k;
4545 : int16_t idx_ER;
4546 : int16_t nbits, nbits_fr;
4547 : uint16_t idx_sp_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4548 : uint16_t mr_idx_sp_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4549 : int16_t GR_ord, bits_GR;
4550 : uint64_t idx, idx1;
4551 : int16_t no_idx16;
4552 : int16_t no_cv[MASA_MAXIMUM_CODING_SUBBANDS];
4553 : IVAS_QDIRECTION *q_direction;
4554 : int16_t half_coding_subbands, nbits_fr1, coding_subbands;
4555 : uint16_t idx_sp_coh_shift[MASA_MAXIMUM_CODING_SUBBANDS];
4556 : uint8_t idx_shift;
4557 230094 : int16_t max_val = 0, nbits_max;
4558 : int16_t extra_cv;
4559 : int16_t no_cv_shift[MASA_MAXIMUM_CODING_SUBBANDS], min_idx;
4560 :
4561 230094 : coding_subbands = q_metadata->q_direction[idx_d].cfg.nbands;
4562 230094 : q_direction = &( q_metadata->q_direction[idx_d] );
4563 230094 : nbits = 0;
4564 230094 : GR_ord = 1;
4565 230094 : idx_shift = 0;
4566 :
4567 : /* number of codevectors added dependent on number of subbands */
4568 230094 : extra_cv = coding_subbands / MASA_FACTOR_CV_COH;
4569 2702050 : for ( j = 0; j < coding_subbands; j++ )
4570 : {
4571 2471956 : if ( hrmasa_flag )
4572 : {
4573 0 : idx_ER = 7 - ( q_direction->band_data[j].energy_ratio_index_mod[0] >> 1 ) + extra_cv;
4574 : }
4575 : else
4576 : {
4577 2471956 : idx_ER = 7 - q_direction->band_data[j].energy_ratio_index_mod[0] + extra_cv;
4578 : }
4579 :
4580 2471956 : if ( idx_ER > 0 )
4581 : {
4582 2329421 : idx_sp_coh[j] = (uint16_t) roundf( q_direction->coherence_band_data[j].spread_coherence[0] / ( 255.0f / (float) idx_ER ) );
4583 2329421 : q_direction->coherence_band_data[j].spread_coherence[0] = (uint8_t) roundf( idx_sp_coh[j] * ( 255.0f / (float) idx_ER ) );
4584 : }
4585 : else
4586 : {
4587 142535 : idx_sp_coh[j] = 0;
4588 142535 : q_direction->coherence_band_data[j].spread_coherence[0] = 0;
4589 : }
4590 2471956 : no_cv[j] = idx_ER + 1;
4591 :
4592 2471956 : no_cv_shift[idx_shift] = no_cv[j];
4593 2471956 : idx_sp_coh_shift[idx_shift++] = idx_sp_coh[j];
4594 : }
4595 :
4596 230094 : if ( sum_s( no_cv, coding_subbands ) == coding_subbands )
4597 : {
4598 7733 : return 0;
4599 : }
4600 :
4601 222361 : nbits_max = 0;
4602 222361 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
4603 : {
4604 135228 : j = maximum_s( (int16_t *) idx_sp_coh, coding_subbands, &max_val );
4605 2193678 : for ( j = 0; j < coding_subbands; j++ )
4606 : {
4607 2058450 : if ( no_cv[j] > max_val + 1 )
4608 : {
4609 1834315 : no_cv[j] = max_val + 1;
4610 : }
4611 : }
4612 135228 : nbits_max = MASA_MAX_NO_CV_SUR_COH - max_val + extra_cv;
4613 : }
4614 :
4615 222361 : nbits = coherence_coding_length( idx_sp_coh_shift, idx_shift, coding_subbands, no_cv,
4616 : mr_idx_sp_coh, no_cv_shift, &min_idx, &GR_ord, &nbits_fr, &nbits_fr1 );
4617 222361 : half_coding_subbands = 0;
4618 222361 : idx1 = 0;
4619 :
4620 222361 : if ( nbits_fr + nbits_fr1 + nbits_max < nbits )
4621 : {
4622 : /* write flag*/
4623 101768 : push_next_indice( hMasaMetaData, 0, 1 );
4624 :
4625 : /* create combined index */
4626 101768 : nbits = nbits_fr + nbits_fr1 + 1;
4627 :
4628 101768 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
4629 : {
4630 : /* write max value*/
4631 35713 : bits_GR = hMasaMetaData->nb_bits_tot;
4632 35713 : ivas_qmetadata_encode_extended_gr( hMasaMetaData, MASA_MAX_NO_CV_SUR_COH - max_val - 1 + extra_cv, MASA_MAX_NO_CV_SUR_COH + extra_cv, 0 );
4633 35713 : nbits += hMasaMetaData->nb_bits_tot - bits_GR;
4634 : }
4635 :
4636 101768 : if ( nbits_fr1 > 0 )
4637 : {
4638 927 : half_coding_subbands = coding_subbands / 2;
4639 927 : idx = create_combined_index( idx_sp_coh, half_coding_subbands, no_cv );
4640 927 : idx1 = create_combined_index( &idx_sp_coh[half_coding_subbands], half_coding_subbands, &no_cv[half_coding_subbands] );
4641 : }
4642 : else
4643 : {
4644 100841 : idx = create_combined_index( idx_sp_coh, coding_subbands, no_cv );
4645 : }
4646 :
4647 101768 : if ( nbits_fr % 16 == 0 )
4648 : {
4649 30621 : no_idx16 = nbits_fr / 16;
4650 : }
4651 : else
4652 : {
4653 71147 : no_idx16 = (int16_t) round_f( ( nbits_fr / 16.0f + 0.5f ) );
4654 : }
4655 :
4656 : /* write combined index */
4657 101768 : k = nbits_fr;
4658 108607 : for ( i = 0; i < no_idx16 - 1; i++ )
4659 : {
4660 6839 : k -= 16;
4661 6839 : push_next_indice( hMasaMetaData, ( ( idx >> k ) & 65535 ), 16 ); /* 16 bits */
4662 : }
4663 :
4664 101768 : push_next_indice( hMasaMetaData, ( idx & ( ( 1 << k ) - 1 ) ), k );
4665 :
4666 101768 : if ( nbits_fr1 > 0 )
4667 : {
4668 927 : if ( nbits_fr1 % 16 == 0 )
4669 : {
4670 76 : no_idx16 = nbits_fr1 / 16;
4671 : }
4672 : else
4673 : {
4674 851 : no_idx16 = (int16_t) round_f( ( nbits_fr1 / 16.0f + 0.5f ) );
4675 : }
4676 :
4677 927 : assert( no_idx16 <= 4 );
4678 :
4679 927 : k = nbits_fr1;
4680 1958 : for ( i = 0; i < no_idx16 - 1; i++ )
4681 : {
4682 1031 : k -= 16;
4683 1031 : push_next_indice( hMasaMetaData, ( ( idx1 >> k ) & 65535 ), 16 ); /* 16 bits */
4684 : }
4685 927 : push_next_indice( hMasaMetaData, ( idx1 & ( ( 1 << k ) - 1 ) ), k );
4686 : }
4687 : }
4688 : else
4689 : {
4690 : /* write flag */
4691 120593 : nbits = 1;
4692 :
4693 : /* write flag*/
4694 120593 : push_next_indice( hMasaMetaData, 1, 1 );
4695 :
4696 : /* write GR_ord */
4697 120593 : push_next_indice( hMasaMetaData, GR_ord, 1 );
4698 120593 : nbits += 1;
4699 :
4700 : /* write the min */
4701 120593 : bits_GR = hMasaMetaData->nb_bits_tot;
4702 120593 : ivas_qmetadata_encode_extended_gr( hMasaMetaData, min_idx, MASA_MAX_NO_CV_SUR_COH + extra_cv, 0 );
4703 120593 : nbits += hMasaMetaData->nb_bits_tot - bits_GR;
4704 :
4705 : /* write GR data */
4706 1762208 : for ( j = 0; j < idx_shift; j++ )
4707 : {
4708 1641615 : bits_GR = hMasaMetaData->nb_bits_tot;
4709 1641615 : ivas_qmetadata_encode_extended_gr( hMasaMetaData, mr_idx_sp_coh[j], no_cv_shift[j], GR_ord );
4710 1641615 : nbits += hMasaMetaData->nb_bits_tot - bits_GR;
4711 : }
4712 : }
4713 :
4714 222361 : return nbits;
4715 : }
4716 :
4717 :
4718 : /*-------------------------------------------------------------------*
4719 : * encode_surround_coherence()
4720 : *
4721 : * encoding surround coherence
4722 : *-------------------------------------------------------------------*/
4723 :
4724 : /*! r: number of bits written */
4725 549545 : static int16_t encode_surround_coherence(
4726 : IVAS_QMETADATA *hQMetaData, /* i : quantized metadata */
4727 : BSTR_ENC_HANDLE hMetaData /* i/o: metadata bitstream handle */
4728 : )
4729 : {
4730 : int16_t i, j, k;
4731 : int16_t idx_ER, idx16;
4732 : int16_t nbits, nbits_fr;
4733 : uint16_t idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4734 : uint16_t mr_idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4735 : int16_t GR_ord, bits_GR;
4736 : uint64_t idx, idx1;
4737 : int16_t no_idx16;
4738 : int16_t no_cv[MASA_MAXIMUM_CODING_SUBBANDS];
4739 : float error_ratio_surr;
4740 : IVAS_QDIRECTION *q_direction;
4741 : int16_t half_coding_subbands, nbits_fr1, coding_subbands;
4742 : int16_t all_coherence_zero;
4743 : uint16_t idx_sur_coh_shift[MASA_MAXIMUM_CODING_SUBBANDS];
4744 : uint8_t idx_shift;
4745 549545 : int16_t max_val = 0, nbits_max;
4746 : int16_t no_cv_shift[MASA_MAXIMUM_CODING_SUBBANDS], min_idx;
4747 :
4748 549545 : coding_subbands = hQMetaData->q_direction[0].cfg.nbands;
4749 549545 : all_coherence_zero = hQMetaData->all_coherence_zero;
4750 549545 : q_direction = &( hQMetaData->q_direction[0] );
4751 549545 : nbits = 0;
4752 :
4753 549545 : if ( all_coherence_zero == 1 )
4754 : {
4755 0 : nbits = 0;
4756 : }
4757 : else
4758 : {
4759 549545 : GR_ord = 1;
4760 549545 : k = 0;
4761 549545 : idx_shift = 0;
4762 5649185 : for ( j = 0; j < coding_subbands; j++ )
4763 : {
4764 5099640 : if ( hQMetaData->no_directions == 2 )
4765 : {
4766 1416856 : k += hQMetaData->twoDirBands[j];
4767 1416856 : idx16 = max( k - 1, 0 );
4768 1416856 : error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[0] - q_direction[1].band_data[idx16].energy_ratio[0] * hQMetaData->twoDirBands[j];
4769 : }
4770 : else
4771 : {
4772 3682784 : error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[0];
4773 : }
4774 :
4775 5099640 : if ( error_ratio_surr <= 0 )
4776 : {
4777 381455 : error_ratio_surr = 0;
4778 381455 : idx_sur_coh[j] = 0;
4779 381455 : no_cv[j] = 1;
4780 381455 : hQMetaData->surcoh_band_data[j].surround_coherence[0] = 0; /* sur_coherence_cb_masa[idx_cb_sur_coh_masa[DIRAC_DIFFUSE_LEVELS - 1] * MASA_MAX_NO_CV_SUR_COH]; */
4781 : }
4782 : else
4783 : {
4784 4718185 : idx_ER = masa_sq( error_ratio_surr, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
4785 :
4786 9436370 : idx_sur_coh[j] = squant_int( hQMetaData->surcoh_band_data[j].surround_coherence[0], &hQMetaData->surcoh_band_data[j].surround_coherence[0],
4787 4718185 : &sur_coherence_cb_masa[idx_cb_sur_coh_masa[idx_ER] * MASA_MAX_NO_CV_SUR_COH], idx_cb_sur_coh_masa[idx_ER] + 2 );
4788 :
4789 4718185 : no_cv[j] = idx_cb_sur_coh_masa[idx_ER] + 2;
4790 4718185 : no_cv_shift[idx_shift] = no_cv[j];
4791 4718185 : idx_sur_coh_shift[idx_shift++] = idx_sur_coh[j];
4792 : }
4793 : }
4794 :
4795 549545 : if ( sum_s( no_cv, coding_subbands ) == coding_subbands )
4796 : {
4797 11789 : return 0;
4798 : }
4799 :
4800 537756 : nbits_max = 0;
4801 537756 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
4802 : {
4803 191573 : j = maximum_s( (int16_t *) idx_sur_coh, coding_subbands, &max_val );
4804 3376349 : for ( j = 0; j < coding_subbands; j++ )
4805 : {
4806 3184776 : if ( no_cv[j] > max_val + 1 )
4807 : {
4808 2292540 : no_cv[j] = max_val + 1;
4809 : }
4810 : }
4811 191573 : nbits_max = MASA_MAX_NO_CV_SUR_COH - max_val; /* encoded with GR0 as max_no_vals - no_vals*/
4812 : }
4813 :
4814 537756 : nbits = coherence_coding_length( idx_sur_coh_shift, idx_shift, coding_subbands, no_cv,
4815 : mr_idx_sur_coh, no_cv_shift, &min_idx, &GR_ord, &nbits_fr, &nbits_fr1 );
4816 537756 : half_coding_subbands = coding_subbands / 2;
4817 537756 : idx1 = 0;
4818 :
4819 : /* should check how to encode the average - check distribution */
4820 537756 : if ( nbits_fr + nbits_fr1 + nbits_max < nbits )
4821 : {
4822 : /* write flag*/
4823 147578 : push_next_indice( hMetaData, 0, 1 );
4824 :
4825 : /* create combined index */
4826 147578 : nbits = nbits_fr + nbits_fr1 + 1;
4827 147578 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
4828 : {
4829 : /* write max value*/
4830 47277 : bits_GR = hMetaData->nb_bits_tot;
4831 47277 : ivas_qmetadata_encode_extended_gr( hMetaData, MASA_MAX_NO_CV_SUR_COH - max_val - 1, MASA_MAX_NO_CV_SUR_COH, 0 );
4832 47277 : nbits += hMetaData->nb_bits_tot - bits_GR;
4833 : }
4834 :
4835 147578 : if ( nbits_fr1 > 0 )
4836 : {
4837 12550 : idx = create_combined_index( idx_sur_coh, half_coding_subbands, no_cv );
4838 12550 : idx1 = create_combined_index( &idx_sur_coh[half_coding_subbands], half_coding_subbands, &no_cv[half_coding_subbands] );
4839 : }
4840 : else
4841 : {
4842 135028 : idx = create_combined_index( idx_sur_coh, coding_subbands, no_cv );
4843 : }
4844 :
4845 147578 : if ( nbits_fr % 16 == 0 )
4846 : {
4847 18992 : no_idx16 = nbits_fr / 16;
4848 : }
4849 : else
4850 : {
4851 128586 : no_idx16 = (int16_t) round_f( ( nbits_fr / 16.0f + 0.5f ) );
4852 : }
4853 :
4854 : /* write combined index */
4855 147578 : k = nbits_fr;
4856 190941 : for ( i = 0; i < no_idx16 - 1; i++ )
4857 : {
4858 43363 : k -= 16;
4859 43363 : push_next_indice( hMetaData, ( ( idx >> k ) & 65535 ), 16 ); /* 16 bits */
4860 : }
4861 :
4862 147578 : push_next_indice( hMetaData, ( idx & ( ( 1 << k ) - 1 ) ), k );
4863 :
4864 147578 : if ( nbits_fr1 > 0 )
4865 : {
4866 12550 : if ( nbits_fr1 % 16 == 0 )
4867 : {
4868 1097 : no_idx16 = nbits_fr1 / 16;
4869 : }
4870 : else
4871 : {
4872 11453 : no_idx16 = (int16_t) round_f( ( nbits_fr1 / 16.0f + 0.5f ) );
4873 : }
4874 :
4875 12550 : assert( no_idx16 <= 4 );
4876 :
4877 12550 : k = nbits_fr1;
4878 26665 : for ( i = 0; i < no_idx16 - 1; i++ )
4879 : {
4880 14115 : k -= 16;
4881 14115 : push_next_indice( hMetaData, ( ( idx1 >> k ) & 65535 ), 16 ); /* 16 bits */
4882 : }
4883 :
4884 12550 : push_next_indice( hMetaData, ( idx1 & ( ( 1 << k ) - 1 ) ), k );
4885 : }
4886 : }
4887 : else
4888 : {
4889 : /* write flag */
4890 390178 : nbits = 1;
4891 :
4892 : /* write flag*/
4893 390178 : push_next_indice( hMetaData, 1, 1 );
4894 :
4895 : /* write GR_ord */
4896 390178 : push_next_indice( hMetaData, GR_ord, 1 );
4897 390178 : nbits += 1;
4898 :
4899 : /* write the min */
4900 390178 : bits_GR = hMetaData->nb_bits_tot;
4901 390178 : ivas_qmetadata_encode_extended_gr( hMetaData, min_idx, MASA_MAX_NO_CV_SUR_COH, 0 );
4902 390178 : nbits += hMetaData->nb_bits_tot - bits_GR;
4903 :
4904 : /* write GR data */
4905 3936695 : for ( j = 0; j < idx_shift; j++ )
4906 : {
4907 3546517 : bits_GR = hMetaData->nb_bits_tot;
4908 :
4909 3546517 : ivas_qmetadata_encode_extended_gr( hMetaData, mr_idx_sur_coh[j], no_cv_shift[j], GR_ord );
4910 :
4911 3546517 : nbits += hMetaData->nb_bits_tot - bits_GR;
4912 : }
4913 : }
4914 : }
4915 : #ifdef DEBUGGING
4916 : for ( i = 0; i < coding_subbands; i++ )
4917 : {
4918 : for ( j = 1; j < q_direction->cfg.nblocks; j++ )
4919 : {
4920 : hQMetaData->surcoh_band_data[i].surround_coherence[j] = hQMetaData->surcoh_band_data[i].surround_coherence[0];
4921 : }
4922 : }
4923 : #endif
4924 :
4925 537756 : return nbits;
4926 : }
4927 :
4928 :
4929 57978 : static int16_t encode_surround_coherence_hr(
4930 : IVAS_QMETADATA *hQMetaData, /* i : quantized metadata */
4931 : BSTR_ENC_HANDLE hMetaData /* i/o: metadata bitstream handle */
4932 : )
4933 : {
4934 : int16_t i, j, k, sf;
4935 : int16_t nbits, nbits_fr, nbits_sf;
4936 : uint16_t idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4937 : uint16_t mr_idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4938 : int16_t GR_ord, bits_GR;
4939 : uint64_t idx, idx1;
4940 : int16_t no_idx16;
4941 : int16_t no_cv[MASA_MAXIMUM_CODING_SUBBANDS];
4942 : float error_ratio_surr;
4943 : IVAS_QDIRECTION *q_direction;
4944 : int16_t half_coding_subbands, nbits_fr1, coding_subbands;
4945 : int16_t all_coherence_zero;
4946 : uint16_t idx_sur_coh_shift[MASA_MAXIMUM_CODING_SUBBANDS];
4947 : uint8_t idx_shift;
4948 57978 : int16_t max_val = 0, nbits_max;
4949 : int16_t no_cv_shift[MASA_MAXIMUM_CODING_SUBBANDS], min_idx;
4950 : int16_t idx16;
4951 : int32_t int_error_ratio_surr;
4952 :
4953 57978 : coding_subbands = hQMetaData->q_direction[0].cfg.nbands;
4954 57978 : all_coherence_zero = hQMetaData->all_coherence_zero;
4955 57978 : q_direction = &( hQMetaData->q_direction[0] );
4956 57978 : nbits = 0;
4957 :
4958 57978 : if ( all_coherence_zero == 1 )
4959 : {
4960 0 : nbits = 0;
4961 : }
4962 : else
4963 : {
4964 244017 : for ( sf = 0; sf < hQMetaData->q_direction[0].cfg.nblocks; sf++ )
4965 : {
4966 186039 : GR_ord = 1;
4967 186039 : k = 0;
4968 186039 : idx_shift = 0;
4969 4363224 : for ( j = 0; j < coding_subbands; j++ )
4970 : {
4971 4177185 : if ( hQMetaData->no_directions == 2 )
4972 : {
4973 1230736 : k += hQMetaData->twoDirBands[j];
4974 1230736 : idx16 = max( k - 1, 0 );
4975 1230736 : error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[sf] - q_direction[1].band_data[idx16].energy_ratio[sf] * hQMetaData->twoDirBands[j];
4976 : }
4977 : else
4978 : {
4979 2946449 : error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[sf];
4980 : }
4981 :
4982 4177185 : int_error_ratio_surr = (int32_t) ( MASA_SUR_COH_PRECISION * error_ratio_surr );
4983 4177185 : error_ratio_surr = (float) ( int_error_ratio_surr * MASA_SUR_COH_THRESHOLD );
4984 :
4985 4177185 : if ( error_ratio_surr <= 0 )
4986 : {
4987 792849 : error_ratio_surr = 0;
4988 792849 : idx_sur_coh[j] = 0;
4989 792849 : no_cv[j] = 1;
4990 792849 : hQMetaData->surcoh_band_data[j].surround_coherence[sf] = 0; /* sur_coherence_cb_masa[idx_cb_sur_coh_masa[DIRAC_DIFFUSE_LEVELS - 1] * MASA_MAX_NO_CV_SUR_COH]; */
4991 : }
4992 : else
4993 : {
4994 6768672 : idx_sur_coh[j] = squant_int( hQMetaData->surcoh_band_data[j].surround_coherence[sf], &hQMetaData->surcoh_band_data[j].surround_coherence[sf],
4995 3384336 : &sur_coherence_cb_masa[idx_cb_sur_coh_masa[7] * MASA_MAX_NO_CV_SUR_COH], idx_cb_sur_coh_masa[7] + 2 );
4996 3384336 : no_cv[j] = idx_cb_sur_coh_masa[7] + 2;
4997 3384336 : no_cv_shift[idx_shift] = no_cv[j];
4998 3384336 : idx_sur_coh_shift[idx_shift++] = idx_sur_coh[j];
4999 : }
5000 : }
5001 :
5002 186039 : if ( sum_s( no_cv, coding_subbands ) != coding_subbands )
5003 : {
5004 182895 : nbits_max = 0;
5005 182895 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
5006 : {
5007 172991 : j = maximum_s( (int16_t *) idx_sur_coh, coding_subbands, &max_val );
5008 4226031 : for ( j = 0; j < coding_subbands; j++ )
5009 : {
5010 4053040 : if ( no_cv[j] > max_val + 1 )
5011 : {
5012 2970874 : no_cv[j] = max_val + 1;
5013 : }
5014 : }
5015 172991 : nbits_max = MASA_MAX_NO_CV_SUR_COH - max_val; /* encoded with GR0 as max_no_vals - no_vals*/
5016 : }
5017 182895 : if ( max_val == 0 )
5018 : {
5019 855578 : for ( j = 0; j < coding_subbands; j++ )
5020 : {
5021 813637 : hQMetaData->surcoh_band_data[j].surround_coherence[sf] = 0;
5022 : }
5023 : }
5024 182895 : nbits_sf = coherence_coding_length( idx_sur_coh_shift, idx_shift, coding_subbands, no_cv,
5025 : mr_idx_sur_coh, no_cv_shift, &min_idx, &GR_ord, &nbits_fr, &nbits_fr1 );
5026 182895 : half_coding_subbands = coding_subbands / 2;
5027 182895 : idx1 = 0;
5028 :
5029 : /* should check how to encode the average - check distribution */
5030 182895 : if ( nbits_fr + nbits_fr1 + nbits_max < nbits_sf )
5031 : {
5032 : /* write flag*/
5033 36326 : push_next_indice( hMetaData, 0, 1 );
5034 :
5035 : /* create combined index */
5036 36326 : nbits += nbits_fr + nbits_fr1 + 1;
5037 36326 : if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH )
5038 : {
5039 : /* write max value*/
5040 36239 : bits_GR = hMetaData->nb_bits_tot;
5041 36239 : ivas_qmetadata_encode_extended_gr( hMetaData, MASA_MAX_NO_CV_SUR_COH - max_val - 1, MASA_MAX_NO_CV_SUR_COH, 0 );
5042 36239 : nbits += hMetaData->nb_bits_tot - bits_GR;
5043 : }
5044 :
5045 36326 : if ( nbits_fr1 > 0 )
5046 : {
5047 12884 : idx = create_combined_index( idx_sur_coh, half_coding_subbands, no_cv );
5048 12884 : idx1 = create_combined_index( &idx_sur_coh[half_coding_subbands], half_coding_subbands, &no_cv[half_coding_subbands] );
5049 : }
5050 : else
5051 : {
5052 23442 : idx = create_combined_index( idx_sur_coh, coding_subbands, no_cv );
5053 : }
5054 :
5055 36326 : if ( nbits_fr % 16 == 0 )
5056 : {
5057 20268 : no_idx16 = nbits_fr / 16;
5058 : }
5059 : else
5060 : {
5061 16058 : no_idx16 = (int16_t) round_f( ( nbits_fr / 16.0f + 0.5f ) );
5062 : }
5063 :
5064 : /* write combined index */
5065 36326 : k = nbits_fr;
5066 66897 : for ( i = 0; i < no_idx16 - 1; i++ )
5067 : {
5068 30571 : k -= 16;
5069 30571 : push_next_indice( hMetaData, ( ( idx >> k ) & 65535 ), 16 ); /* 16 bits */
5070 : }
5071 :
5072 36326 : push_next_indice( hMetaData, ( idx & ( ( 1 << k ) - 1 ) ), k );
5073 :
5074 36326 : if ( nbits_fr1 > 0 )
5075 : {
5076 12884 : if ( nbits_fr1 % 16 == 0 )
5077 : {
5078 1773 : no_idx16 = nbits_fr1 / 16;
5079 : }
5080 : else
5081 : {
5082 11111 : no_idx16 = (int16_t) round_f( ( nbits_fr1 / 16.0f + 0.5f ) );
5083 : }
5084 :
5085 12884 : assert( no_idx16 <= 4 );
5086 :
5087 12884 : k = nbits_fr1;
5088 33799 : for ( i = 0; i < no_idx16 - 1; i++ )
5089 : {
5090 20915 : k -= 16;
5091 20915 : push_next_indice( hMetaData, ( ( idx1 >> k ) & 65535 ), 16 ); /* 16 bits */
5092 : }
5093 :
5094 12884 : push_next_indice( hMetaData, ( idx1 & ( ( 1 << k ) - 1 ) ), k );
5095 : }
5096 : }
5097 : else
5098 : {
5099 : /* write flag */
5100 146569 : nbits += 1;
5101 :
5102 : /* write flag*/
5103 146569 : push_next_indice( hMetaData, 1, 1 );
5104 :
5105 : /* write GR_ord */
5106 146569 : push_next_indice( hMetaData, GR_ord, 1 );
5107 146569 : nbits += 1;
5108 :
5109 : /* write the min */
5110 146569 : bits_GR = hMetaData->nb_bits_tot;
5111 146569 : ivas_qmetadata_encode_extended_gr( hMetaData, min_idx, MASA_MAX_NO_CV_SUR_COH, 0 );
5112 146569 : nbits += hMetaData->nb_bits_tot - bits_GR;
5113 :
5114 : /* write GR data */
5115 2839994 : for ( j = 0; j < idx_shift; j++ )
5116 : {
5117 2693425 : bits_GR = hMetaData->nb_bits_tot;
5118 :
5119 2693425 : ivas_qmetadata_encode_extended_gr( hMetaData, mr_idx_sur_coh[j], no_cv_shift[j], GR_ord );
5120 :
5121 2693425 : nbits += hMetaData->nb_bits_tot - bits_GR;
5122 : }
5123 : }
5124 : }
5125 : }
5126 : }
5127 :
5128 57978 : return nbits;
5129 : }
5130 :
5131 :
5132 : /*-------------------------------------------------------------------*
5133 : * quantize_DCT_0_coh()
5134 : *
5135 : * quanization of DCT component of order zero for transformed coherence vector
5136 : *-------------------------------------------------------------------*/
5137 :
5138 : /*! r: quantized value */
5139 3144133 : static float quantize_DCT_0_coh(
5140 : const float x, /* i : input value */
5141 : const int16_t j, /* i : subband index */
5142 : const float *coherence_cb, /* i : coherence codebook */
5143 : const float delta_var, /* i : azimuth variance threshold */
5144 : const int16_t no_cb, /* i : maximum number of codewords */
5145 : IVAS_QDIRECTION *q_direction, /* i : quantized metadata */
5146 : uint16_t *idx_x, /* o : codewords index */
5147 : int16_t *p_no_cb, /* o : actual number of codewords dependent on energy ratio value */
5148 : const int16_t hrmasa_flag /* i : flag indicating high-rate MASA MD coding */
5149 : )
5150 : {
5151 : float var_azi, xhat;
5152 : int16_t idx_sub_cb, idx;
5153 : int16_t min_index;
5154 : /* quantize first DCT component */
5155 3144133 : var_azi = var( q_direction->band_data[j].azimuth, q_direction->cfg.nblocks );
5156 :
5157 3144133 : if ( hrmasa_flag )
5158 : {
5159 0 : minimum_s( (int16_t *) ( q_direction->band_data[j].energy_ratio_index ), q_direction->cfg.nblocks, &min_index );
5160 0 : min_index = min_index >> 1;
5161 : }
5162 : else
5163 : {
5164 3144133 : min_index = q_direction->band_data[j].energy_ratio_index[0];
5165 : }
5166 :
5167 3144133 : if ( var_azi < delta_var )
5168 : {
5169 1288386 : idx_sub_cb = no_cb * min_index;
5170 : }
5171 : else
5172 : {
5173 1855747 : idx_sub_cb = no_cb * ( min_index + DIRAC_DIFFUSE_LEVELS );
5174 : }
5175 :
5176 3144133 : idx = squant( x, &xhat, &coherence_cb[idx_sub_cb], len_cb_dct0_masa[min_index] );
5177 :
5178 3144133 : *p_no_cb = len_cb_dct0_masa[min_index];
5179 3144133 : *idx_x = idx;
5180 :
5181 3144133 : return xhat;
5182 : }
5183 :
5184 :
5185 : /*-------------------------------------------------------------------*
5186 : * encode_coherence_indexesDCT1()
5187 : *
5188 : * Encoding DCT1 coeffs with joint index or EC
5189 : *-------------------------------------------------------------------*/
5190 :
5191 : /*! r: number of bits written */
5192 466826 : static int16_t encode_coherence_indexesDCT1(
5193 : uint16_t *idx_dct, /* i : data to be encoded */
5194 : const int16_t len, /* i : number of data */
5195 : BSTR_ENC_HANDLE hMetaData /* i : metadata handle */
5196 : )
5197 : {
5198 : int16_t i, nbits, GR_ord;
5199 : uint16_t av;
5200 : uint16_t mr_idx_dct[MASA_MAXIMUM_CODING_SUBBANDS];
5201 :
5202 466826 : GR_ord = 0;
5203 466826 : nbits = 0;
5204 :
5205 466826 : nbits = mean_removed_GR_new( idx_dct, MASA_NO_CV_COH1, len, 0, &GR_ord, &av, mr_idx_dct );
5206 :
5207 3610959 : for ( i = 0; i < len; i++ )
5208 : {
5209 3144133 : ivas_qmetadata_encode_extended_gr( hMetaData, mr_idx_dct[i], 2 * MASA_NO_CV_COH1, GR_ord );
5210 : }
5211 :
5212 466826 : nbits += len_huf_masa[av];
5213 :
5214 466826 : push_next_indice( hMetaData, huff_code_av_masa[av], len_huf_masa[av] );
5215 :
5216 466826 : return nbits;
5217 : }
5218 :
5219 :
5220 : /*-------------------------------------------------------------------*
5221 : * dct4_transform()
5222 : *
5223 : * 4D implementation of DCT transform
5224 : *-------------------------------------------------------------------*/
5225 :
5226 6288266 : static void dct4_transform(
5227 : uint8_t *v, /* i : input 4D vector */
5228 : float *dct_v /* o : output transformed vector */
5229 : )
5230 : {
5231 : float a, b, c, d;
5232 :
5233 6288266 : a = ( v[0] + v[3] ) / 256.0f;
5234 6288266 : b = ( v[1] + v[2] ) / 256.0f;
5235 6288266 : c = ( v[0] - v[3] ) / 256.0f;
5236 6288266 : d = ( v[1] - v[2] ) / 256.0f;
5237 :
5238 6288266 : dct_v[0] = 0.5f * ( a + b );
5239 6288266 : dct_v[1] = 0.653281482438188f * c + 0.270598050073099f * d;
5240 6288266 : dct_v[2] = 0.5f * ( a - b );
5241 6288266 : dct_v[3] = 0.270598050073099f * c - 0.653281482438188f * d;
5242 :
5243 6288266 : return;
5244 : }
5245 :
5246 :
5247 : /*-------------------------------------------------------------------*
5248 : * ivas_qmetadata_quantize_coherence_hr_512()
5249 : *
5250 : *
5251 : *-------------------------------------------------------------------*/
5252 :
5253 75809 : static int16_t ivas_qmetadata_quantize_coherence_hr_512(
5254 : IVAS_QMETADATA *hQMetaData, /* i/o: quantized metadata */
5255 : const int16_t idx_d, /* i : current direction index */
5256 : const int16_t all_coherence_zero, /* i : all coherence is zero - flag */
5257 : BSTR_ENC_HANDLE hMetaData, /* i : metadata handle */
5258 : const int16_t bits_coh )
5259 : {
5260 : int16_t j, k;
5261 : int16_t nbands, nblocks;
5262 : int16_t nbits;
5263 : int16_t nbits1, nbits0, nbits_av;
5264 : uint16_t idx_coh[MASA_MAXIMUM_CODING_SUBBANDS];
5265 : IVAS_QDIRECTION *q_direction;
5266 : int16_t cbsize;
5267 : float delta, tmp;
5268 : int16_t min_idx, GR_param, GR_param_av;
5269 : uint16_t av, mr_idx[MASA_MAXIMUM_CODING_SUBBANDS];
5270 :
5271 75809 : q_direction = &( hQMetaData->q_direction[idx_d] );
5272 75809 : nbands = q_direction->cfg.nbands;
5273 75809 : nblocks = q_direction->cfg.nblocks;
5274 75809 : nbits = 0;
5275 :
5276 75809 : if ( all_coherence_zero == 1 )
5277 : {
5278 0 : return nbits;
5279 : }
5280 75809 : nbits = hMetaData->nb_bits_tot;
5281 :
5282 75809 : cbsize = 1 << bits_coh;
5283 75809 : delta = 256.0f / cbsize;
5284 :
5285 318649 : for ( k = 0; k < nblocks; k++ )
5286 : {
5287 242840 : min_idx = 0;
5288 5198014 : for ( j = 0; j < nbands; j++ )
5289 : {
5290 4955174 : idx_coh[j] = usquant( (float) ( q_direction->coherence_band_data[j].spread_coherence[k] ), &tmp, delta / 2.0f, delta, cbsize );
5291 4955174 : q_direction->coherence_band_data[j].spread_coherence[k] = (uint8_t) ( idx_coh[j] * delta + delta / 2.0f );
5292 4955174 : if ( idx_coh[j] < min_idx )
5293 : {
5294 0 : min_idx = idx_coh[j];
5295 : }
5296 : }
5297 :
5298 242840 : nbits0 = 0;
5299 242840 : nbits1 = 0;
5300 5198014 : for ( j = 0; j < nbands; j++ )
5301 : {
5302 4955174 : idx_coh[j] = idx_coh[j] - min_idx;
5303 4955174 : nbits0 += ivas_qmetadata_encode_extended_gr_length( idx_coh[j], cbsize - min_idx, 0 );
5304 4955174 : nbits1 += ivas_qmetadata_encode_extended_gr_length( idx_coh[j], cbsize - min_idx, 1 );
5305 : }
5306 242840 : if ( nbits0 < nbits1 )
5307 : {
5308 164227 : GR_param = 0;
5309 164227 : nbits1 = nbits0;
5310 : }
5311 : else
5312 : {
5313 78613 : GR_param = 1;
5314 : }
5315 :
5316 242840 : GR_param_av = 1;
5317 242840 : nbits_av = mean_removed_GR_new( idx_coh, cbsize, nbands, 1, &GR_param_av, &av, mr_idx );
5318 :
5319 242840 : if ( nbits_av < nbits1 )
5320 : {
5321 42951 : nbits1 = nbits_av;
5322 42951 : GR_param = GR_param_av;
5323 :
5324 : /* use average removed */
5325 42951 : push_next_indice( hMetaData, 1, 1 );
5326 :
5327 : /* write average */
5328 42951 : push_next_indice( hMetaData, av, bits_coh );
5329 :
5330 : /* write GR param */
5331 42951 : push_next_indice( hMetaData, GR_param, 1 );
5332 :
5333 962956 : for ( j = 0; j < nbands; j++ )
5334 : {
5335 920005 : ivas_qmetadata_encode_extended_gr( hMetaData, mr_idx[j], 2 * cbsize, GR_param );
5336 : }
5337 : }
5338 : else
5339 : {
5340 : /* use min removed */
5341 199889 : push_next_indice( hMetaData, 0, 1 );
5342 :
5343 : /* write min index */
5344 199889 : push_next_indice( hMetaData, min_idx, bits_coh );
5345 :
5346 : /* write GR param */
5347 199889 : push_next_indice( hMetaData, GR_param, 1 );
5348 :
5349 4235058 : for ( j = 0; j < nbands; j++ )
5350 : {
5351 4035169 : ivas_qmetadata_encode_extended_gr( hMetaData, idx_coh[j], cbsize - min_idx, GR_param );
5352 : }
5353 : }
5354 : }
5355 :
5356 75809 : nbits = hMetaData->nb_bits_tot - nbits;
5357 75809 : return nbits;
5358 : }
5359 :
5360 :
5361 : /*-------------------------------------------------------------------*
5362 : * ivas_qmetadata_quantize_coherence()
5363 : *
5364 : *
5365 : *-------------------------------------------------------------------*/
5366 :
5367 : /*! r: number of bits written */
5368 1163746 : static int16_t ivas_qmetadata_quantize_coherence(
5369 : IVAS_QMETADATA *hQMetaData, /* i/o: quantized metadata */
5370 : const int16_t idx_d, /* i : current direction index */
5371 : const int16_t all_coherence_zero, /* i : all coherence is zero - flag */
5372 : BSTR_ENC_HANDLE hMetaData, /* i : metadata handle */
5373 : const int16_t write_flag, /* i : flag to actually write the data or not */
5374 : int16_t *indice_coherence,
5375 : const int16_t hrmasa_flag /* i : flag indicating high-rate MASA MD coding */
5376 : )
5377 : {
5378 : int16_t j, k;
5379 : float dct_coh[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
5380 : uint16_t idx_dct[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_MAXIMUM_CODING_SUBBANDS];
5381 : int16_t coding_subbands;
5382 : int16_t nbits;
5383 : uint64_t no_cb;
5384 : int16_t MASA_grouping[MASA_MAXIMUM_CODING_SUBBANDS];
5385 : int16_t nbits1;
5386 : int16_t coding_subbands_0, d;
5387 : int16_t two_dir_band[MASA_MAXIMUM_CODING_SUBBANDS];
5388 : int16_t no_cb_vec[MASA_MAXIMUM_CODING_SUBBANDS];
5389 : IVAS_QDIRECTION *q_direction;
5390 : int16_t min_index;
5391 1163746 : min_index = 0;
5392 1163746 : q_direction = &( hQMetaData->q_direction[idx_d] );
5393 1163746 : coding_subbands = q_direction->cfg.nbands;
5394 1163746 : nbits = 0;
5395 :
5396 1163746 : if ( all_coherence_zero == 1 )
5397 : {
5398 0 : return nbits;
5399 : }
5400 :
5401 1163746 : if ( hQMetaData->q_direction[idx_d].cfg.nblocks == 1 )
5402 : {
5403 230094 : nbits = encode_spread_coherence_1sf( hQMetaData, idx_d, hMetaData, hrmasa_flag );
5404 :
5405 230094 : return nbits;
5406 : }
5407 : else
5408 : {
5409 933652 : k = 0;
5410 933652 : no_cb = 1;
5411 933652 : coding_subbands_0 = hQMetaData->q_direction[0].cfg.nbands;
5412 933652 : if ( coding_subbands_0 <= 5 )
5413 : {
5414 4037508 : for ( j = 0; j < 5; j++ )
5415 : {
5416 3364590 : MASA_grouping[j] = j;
5417 : }
5418 : }
5419 : else
5420 : {
5421 260734 : if ( coding_subbands_0 <= 8 )
5422 : {
5423 73320 : mvs2s( MASA_grouping_8_to_5, MASA_grouping, 8 );
5424 : }
5425 187414 : else if ( coding_subbands_0 <= 12 )
5426 : {
5427 51758 : mvs2s( MASA_grouping_12_to_5, MASA_grouping, 12 );
5428 : }
5429 135656 : else if ( coding_subbands_0 <= 18 )
5430 : {
5431 86434 : mvs2s( MASA_grouping_18_to_5, MASA_grouping, 18 );
5432 : }
5433 : else
5434 : {
5435 49222 : if ( coding_subbands_0 <= 24 )
5436 : {
5437 49222 : mvs2s( MASA_grouping_24_to_5, MASA_grouping, 24 );
5438 : }
5439 : }
5440 : }
5441 :
5442 933652 : if ( coding_subbands < coding_subbands_0 )
5443 : {
5444 177876 : d = 0;
5445 1582604 : for ( j = 0; j < coding_subbands_0; j++ )
5446 : {
5447 1404728 : if ( hQMetaData->twoDirBands[j] == 1 )
5448 : {
5449 508152 : two_dir_band[d++] = j;
5450 : }
5451 : }
5452 : }
5453 :
5454 7221918 : for ( j = 0; j < coding_subbands; j++ )
5455 : {
5456 : /* DCT transform */
5457 6288266 : dct4_transform( hQMetaData->q_direction[idx_d].coherence_band_data[j].spread_coherence, dct_coh[j] );
5458 :
5459 6288266 : if ( hrmasa_flag )
5460 : {
5461 0 : minimum_s( (int16_t *) ( q_direction->band_data[j].energy_ratio_index ), q_direction->cfg.nblocks, &min_index );
5462 0 : no_cb_vec[j] = len_cb_dct0_masa[min_index >> 1];
5463 : }
5464 : else
5465 : {
5466 6288266 : no_cb_vec[j] = len_cb_dct0_masa[q_direction->band_data[j].energy_ratio_index[0]];
5467 : }
5468 :
5469 6288266 : if ( write_flag )
5470 : {
5471 : /* quantize first DCT parameter */
5472 3144133 : dct_coh[j][0] = quantize_DCT_0_coh( dct_coh[j][0], j, coherence_cb0_masa, MASA_DELTA_AZI_DCT0, MASA_NO_CV_COH, q_direction, &idx_dct[k], &no_cb_vec[j], hrmasa_flag );
5473 : }
5474 :
5475 6288266 : if ( coding_subbands < coding_subbands_0 )
5476 : {
5477 508152 : idx_dct[k + coding_subbands] = squant( dct_coh[j][1], &dct_coh[j][1], &coherence_cb1_masa[MASA_grouping[two_dir_band[j]] * MASA_NO_CV_COH1], MASA_NO_CV_COH1 );
5478 : }
5479 : else
5480 : {
5481 5780114 : idx_dct[k + coding_subbands] = squant( dct_coh[j][1], &dct_coh[j][1], &coherence_cb1_masa[MASA_grouping[j] * MASA_NO_CV_COH1], MASA_NO_CV_COH1 );
5482 : }
5483 6288266 : k++;
5484 :
5485 6288266 : dct_coh[j][2] = 0.0f;
5486 6288266 : dct_coh[j][3] = 0.0f;
5487 : }
5488 :
5489 933652 : nbits1 = 0;
5490 933652 : if ( sum_s( no_cb_vec, coding_subbands ) > MASA_COH_LIMIT_2IDX )
5491 : {
5492 : /* make two indxes */
5493 20222 : no_cb = 1;
5494 : #ifdef DEBUGGING
5495 : assert( coding_subbands % 2 == 0 );
5496 : #endif
5497 :
5498 239882 : for ( j = 0; j < coding_subbands / 2; j++ )
5499 : {
5500 219660 : no_cb *= no_cb_vec[j];
5501 : }
5502 :
5503 20222 : nbits = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
5504 20222 : no_cb = 1;
5505 :
5506 239882 : for ( j = coding_subbands / 2; j < coding_subbands; j++ )
5507 : {
5508 219660 : no_cb *= no_cb_vec[j];
5509 : }
5510 20222 : nbits1 = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
5511 : }
5512 : else
5513 : {
5514 913430 : no_cb = 1;
5515 :
5516 6762376 : for ( j = 0; j < coding_subbands; j++ )
5517 : {
5518 5848946 : no_cb *= no_cb_vec[j];
5519 : }
5520 :
5521 913430 : nbits = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
5522 : }
5523 :
5524 933652 : if ( write_flag )
5525 : {
5526 3610959 : for ( j = 0; j < coding_subbands; j++ )
5527 : {
5528 : /* inverse DCT transform */
5529 3144133 : invdct4_transform( dct_coh[j], q_direction->coherence_band_data[j].spread_coherence );
5530 : }
5531 :
5532 466826 : nbits = encode_coherence_indexesDCT0( idx_dct, coding_subbands, no_cb_vec, hMetaData, *indice_coherence, nbits, nbits1 );
5533 : }
5534 : else
5535 : {
5536 : /* write dummy data now and save the position */
5537 466826 : *indice_coherence = hMetaData->nb_ind_tot;
5538 466826 : k = nbits;
5539 1042133 : while ( k > 0 )
5540 : {
5541 575307 : push_next_indice( hMetaData, 0, min( 16, k ) );
5542 575307 : k -= 16;
5543 : }
5544 :
5545 466826 : if ( nbits1 > 0 )
5546 : {
5547 10111 : k = nbits1;
5548 33181 : while ( k > 0 )
5549 : {
5550 23070 : push_next_indice( hMetaData, 0, min( 16, k ) );
5551 23070 : k -= 16;
5552 : }
5553 : }
5554 466826 : nbits += nbits1;
5555 466826 : set_s( no_cb_vec, MASA_NO_CV_COH1, coding_subbands );
5556 466826 : nbits += encode_coherence_indexesDCT1( &idx_dct[coding_subbands], coding_subbands, hMetaData );
5557 :
5558 466826 : return nbits;
5559 : }
5560 : }
5561 :
5562 466826 : return nbits;
5563 : }
5564 :
5565 :
5566 : /*-------------------------------------------------------------------*
5567 : * ivas_qmetadata_reorder_2dir_bands()
5568 : *
5569 : * Reorders metadata on 2dir bands such that direct-to-total ratio of first direction is
5570 : * always larger or equal to direct-to-total ratio of second direction.
5571 : *-------------------------------------------------------------------*/
5572 :
5573 142591 : static void ivas_qmetadata_reorder_2dir_bands(
5574 : IVAS_QMETADATA_HANDLE hQMetaData )
5575 : {
5576 : int16_t nbands;
5577 : int16_t nsubframes;
5578 : int16_t band, sf;
5579 :
5580 142591 : nbands = hQMetaData->q_direction[0].cfg.nbands;
5581 142591 : nsubframes = hQMetaData->q_direction[0].cfg.nblocks;
5582 :
5583 1649403 : for ( band = 0; band < nbands; band++ )
5584 : {
5585 1506812 : if ( hQMetaData->twoDirBands[band] == 1 )
5586 : {
5587 487555 : if ( hQMetaData->q_direction[0].band_data[band].energy_ratio[0] < hQMetaData->q_direction[1].band_data[band].energy_ratio[0] )
5588 : {
5589 178566 : uint16_t uint16_tmp = 0;
5590 178566 : float flt_tmp = 0;
5591 178566 : uint8_t uint8_tmp = 0;
5592 :
5593 629700 : for ( sf = 0; sf < nsubframes; sf++ )
5594 : {
5595 451134 : uint16_tmp = hQMetaData->q_direction[0].band_data[band].spherical_index[sf];
5596 451134 : hQMetaData->q_direction[0].band_data[band].spherical_index[sf] = hQMetaData->q_direction[1].band_data[band].spherical_index[sf];
5597 451134 : hQMetaData->q_direction[1].band_data[band].spherical_index[sf] = uint16_tmp;
5598 :
5599 451134 : flt_tmp = hQMetaData->q_direction[0].band_data[band].azimuth[sf];
5600 451134 : hQMetaData->q_direction[0].band_data[band].azimuth[sf] = hQMetaData->q_direction[1].band_data[band].azimuth[sf];
5601 451134 : hQMetaData->q_direction[1].band_data[band].azimuth[sf] = flt_tmp;
5602 :
5603 451134 : flt_tmp = hQMetaData->q_direction[0].band_data[band].elevation[sf];
5604 451134 : hQMetaData->q_direction[0].band_data[band].elevation[sf] = hQMetaData->q_direction[1].band_data[band].elevation[sf];
5605 451134 : hQMetaData->q_direction[1].band_data[band].elevation[sf] = flt_tmp;
5606 :
5607 451134 : uint8_tmp = hQMetaData->q_direction[0].band_data[band].distance[sf];
5608 451134 : hQMetaData->q_direction[0].band_data[band].distance[sf] = hQMetaData->q_direction[1].band_data[band].distance[sf];
5609 451134 : hQMetaData->q_direction[1].band_data[band].distance[sf] = uint8_tmp;
5610 :
5611 451134 : if ( hQMetaData->coherence_flag )
5612 : {
5613 451134 : uint8_tmp = hQMetaData->q_direction[0].coherence_band_data[band].spread_coherence[sf];
5614 451134 : hQMetaData->q_direction[0].coherence_band_data[band].spread_coherence[sf] = hQMetaData->q_direction[1].coherence_band_data[band].spread_coherence[sf];
5615 451134 : hQMetaData->q_direction[1].coherence_band_data[band].spread_coherence[sf] = uint8_tmp;
5616 : }
5617 : }
5618 178566 : if ( hQMetaData->coherence_flag )
5619 : {
5620 178566 : flt_tmp = hQMetaData->q_direction[0].band_data[band].energy_ratio[0];
5621 178566 : hQMetaData->q_direction[0].band_data[band].energy_ratio[0] = hQMetaData->q_direction[1].band_data[band].energy_ratio[0];
5622 178566 : hQMetaData->q_direction[1].band_data[band].energy_ratio[0] = flt_tmp;
5623 : }
5624 : }
5625 : }
5626 : }
5627 :
5628 142591 : return;
5629 : }
5630 :
5631 :
5632 : /*-------------------------------------------------------------------*
5633 : * write_2dir_info()
5634 : *
5635 : *
5636 : *-------------------------------------------------------------------*/
5637 :
5638 307284 : static int16_t write_2dir_info(
5639 : BSTR_ENC_HANDLE hMetaData,
5640 : uint8_t *twoDirBands,
5641 : const int16_t n,
5642 : const int16_t k )
5643 : {
5644 : int16_t nbits;
5645 : int16_t p[MASA_MAXIMUM_CODING_SUBBANDS];
5646 : uint16_t dif_p[MASA_MAXIMUM_CODING_SUBBANDS];
5647 : int16_t i, j;
5648 :
5649 307284 : j = 0;
5650 307284 : p[0] = 0;
5651 3758117 : for ( i = 0; i < n; i++ )
5652 : {
5653 3450833 : if ( twoDirBands[i] == 1 )
5654 : {
5655 2279167 : p[j] = i;
5656 2279167 : j++;
5657 : }
5658 : }
5659 :
5660 307284 : dif_p[0] = p[0];
5661 2279167 : for ( i = 1; i < j; i++ )
5662 : {
5663 1971883 : dif_p[i] = p[i] - p[i - 1] - 1;
5664 : }
5665 :
5666 : #ifdef DEBUGGING
5667 : assert( k == j );
5668 : #endif
5669 307284 : j = hMetaData->nb_bits_tot;
5670 2586451 : for ( i = 0; i < k; i++ )
5671 : {
5672 2279167 : ivas_qmetadata_encode_extended_gr( hMetaData, dif_p[i], 24, 0 );
5673 : }
5674 307284 : nbits = hMetaData->nb_bits_tot - j;
5675 :
5676 307284 : return nbits;
5677 : }
5678 :
5679 :
5680 : /*-------------------------------------------------------------------*
5681 : * transform_azimuth_dir2()
5682 : *
5683 : *
5684 : *-------------------------------------------------------------------*/
5685 :
5686 294624 : static void transform_azimuth_dir2(
5687 : IVAS_QMETADATA_HANDLE hQMetaData,
5688 : int16_t *dir2_bands )
5689 : {
5690 : int16_t i, b;
5691 :
5692 2454542 : for ( i = hQMetaData->q_direction[1].cfg.start_band; i < hQMetaData->q_direction[1].cfg.nbands; i++ )
5693 : {
5694 2159918 : if ( hQMetaData->q_direction[0].band_data[dir2_bands[i]].energy_ratio_index[0] < 7 )
5695 : {
5696 : /* transform azimuth */
5697 7398745 : for ( b = 0; b < hQMetaData->q_direction[1].cfg.nblocks; b++ )
5698 : {
5699 5804429 : hQMetaData->q_direction[1].band_data[i].azimuth[b] = hQMetaData->q_direction[1].band_data[i].azimuth[b] - hQMetaData->q_direction[0].band_data[dir2_bands[i]].azimuth[b] + 180;
5700 5804429 : if ( hQMetaData->q_direction[1].band_data[i].azimuth[b] >= 180 )
5701 : {
5702 817295 : hQMetaData->q_direction[1].band_data[i].azimuth[b] -= 360;
5703 : }
5704 5804429 : if ( hQMetaData->q_direction[1].band_data[i].azimuth[b] < -180 )
5705 : {
5706 0 : hQMetaData->q_direction[1].band_data[i].azimuth[b] += 360;
5707 : }
5708 5804429 : if ( hQMetaData->q_direction[1].band_data[i].azimuth[b] >= 180 )
5709 : {
5710 0 : hQMetaData->q_direction[1].band_data[i].azimuth[b] -= 360;
5711 : }
5712 5804429 : if ( hQMetaData->q_direction[1].band_data[i].azimuth[b] < -180 )
5713 : {
5714 0 : hQMetaData->q_direction[1].band_data[i].azimuth[b] += 360;
5715 : }
5716 : #ifdef DEBUGGING
5717 : assert( hQMetaData->q_direction[1].band_data[i].azimuth[b] < 180 && hQMetaData->q_direction[1].band_data[i].azimuth[b] >= -180 );
5718 : #endif
5719 : }
5720 : }
5721 : }
5722 :
5723 294624 : return;
5724 : }
5725 :
5726 : #ifdef DEBUG_MODE_QMETADATA
5727 : /*-------------------------------------------------------------------------
5728 : * DEBUG function direction_distance()
5729 : *
5730 : *------------------------------------------------------------------------*/
5731 :
5732 : static float direction_distance(
5733 : float elevation[DIRAC_MAX_NBANDS][MAX_PARAM_SPATIAL_SUBFRAMES],
5734 : float azimuth[DIRAC_MAX_NBANDS][MAX_PARAM_SPATIAL_SUBFRAMES],
5735 : IVAS_QDIRECTION *q_direction,
5736 : const int16_t dim1,
5737 : const int16_t dim2,
5738 : float mat_dist[DIRAC_MAX_NBANDS][MAX_PARAM_SPATIAL_SUBFRAMES] )
5739 : {
5740 : float d;
5741 : int16_t i, j;
5742 :
5743 : d = 0;
5744 : for ( i = 0; i < dim1; i++ )
5745 : {
5746 : for ( j = 0; j < dim2; j++ )
5747 : {
5748 : mat_dist[i][j] = sinf( elevation[i][j] * PI_OVER_180 ) * sinf( q_direction->band_data[i].elevation[j] * PI_OVER_180 ) +
5749 : cosf( elevation[i][j] * PI_OVER_180 ) * cosf( q_direction->band_data[i].elevation[j] * PI_OVER_180 ) * cosf( ( azimuth[i][j] - q_direction->band_data[i].azimuth[j] ) * PI_OVER_180 );
5750 :
5751 : d += fabsf( mat_dist[i][j] );
5752 : }
5753 : }
5754 :
5755 : return d / (float) ( dim1 * dim2 );
5756 : }
5757 : #endif
5758 :
5759 :
5760 128379 : static int16_t divide_GR_orders(
5761 : const int16_t *q_idx,
5762 : const int16_t GR1,
5763 : const int16_t GR2,
5764 : const int16_t len,
5765 : const int16_t len_max_GR1,
5766 : int16_t *i_min )
5767 : {
5768 : int16_t nb_GR_min;
5769 : int16_t i, j, nb_GR;
5770 128379 : nb_GR_min = 1000;
5771 128379 : *i_min = -1;
5772 1845684 : for ( i = 0; i < min( len_max_GR1, len ); i++ )
5773 : {
5774 1717305 : nb_GR = 0;
5775 :
5776 14309655 : for ( j = 0; j <= i; j++ )
5777 : {
5778 12592350 : nb_GR += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR1 );
5779 : }
5780 17169390 : for ( j = i + 1; j < len; j++ )
5781 : {
5782 15452085 : nb_GR += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR2 );
5783 : }
5784 :
5785 1717305 : if ( nb_GR < nb_GR_min )
5786 : {
5787 291368 : nb_GR_min = nb_GR;
5788 291368 : *i_min = i + 1;
5789 : }
5790 : }
5791 :
5792 128379 : return nb_GR_min;
5793 : }
5794 :
5795 :
5796 9198 : static int16_t find_optimal_GR_order(
5797 : const int16_t *q_idx,
5798 : const int16_t len,
5799 : int16_t *GR )
5800 : {
5801 : int16_t nb_GR_0, nb_GR_1;
5802 : int16_t i;
5803 : /* find optimum length of the part encoded with GR2 */
5804 9198 : nb_GR_0 = 0;
5805 9198 : nb_GR_1 = 0;
5806 45646 : for ( i = 0; i < len; i++ )
5807 : {
5808 36448 : nb_GR_0 += ivas_qmetadata_encode_extended_gr_length( q_idx[i], 100, 0 );
5809 36448 : nb_GR_1 += ivas_qmetadata_encode_extended_gr_length( q_idx[i], 100, 1 );
5810 : }
5811 :
5812 9198 : if ( nb_GR_0 < nb_GR_1 )
5813 : {
5814 6612 : *GR = 0;
5815 6612 : return nb_GR_0;
5816 : }
5817 : else
5818 : {
5819 2586 : *GR = 1;
5820 :
5821 2586 : return nb_GR_1;
5822 : }
5823 : }
5824 :
5825 :
5826 42793 : static int16_t find_optimal_GR_orders(
5827 : const int16_t *q_idx,
5828 : const int16_t len,
5829 : const int16_t len_max_GR1,
5830 : int16_t *GR1,
5831 : int16_t *GR2,
5832 : int16_t *i_min )
5833 : {
5834 : int16_t nb_GR_20, nb_GR_21, nb_GR_10, nb_GR_min;
5835 : int16_t i_min_20, i_min_21, i_min_10;
5836 : /* find optimum length of the part encoded with GR2 */
5837 42793 : nb_GR_20 = divide_GR_orders( q_idx, 2, 0, len, len_max_GR1, &i_min_20 );
5838 42793 : nb_GR_21 = divide_GR_orders( q_idx, 2, 1, len, len_max_GR1, &i_min_21 );
5839 42793 : nb_GR_10 = divide_GR_orders( q_idx, 1, 0, len, len_max_GR1, &i_min_10 );
5840 :
5841 42793 : if ( nb_GR_20 < nb_GR_21 && nb_GR_20 < nb_GR_10 )
5842 : {
5843 13707 : *GR1 = 2;
5844 13707 : *GR2 = 0;
5845 13707 : nb_GR_min = nb_GR_20;
5846 13707 : *i_min = i_min_20;
5847 : }
5848 : else
5849 : {
5850 29086 : if ( nb_GR_21 < nb_GR_20 && nb_GR_21 < nb_GR_10 )
5851 : {
5852 2876 : *GR1 = 2;
5853 2876 : *GR2 = 1;
5854 2876 : nb_GR_min = nb_GR_21;
5855 2876 : *i_min = i_min_21;
5856 : }
5857 : else
5858 : {
5859 26210 : *GR1 = 1;
5860 26210 : *GR2 = 0;
5861 26210 : nb_GR_min = nb_GR_10;
5862 26210 : *i_min = i_min_10;
5863 : }
5864 : }
5865 :
5866 42793 : return nb_GR_min;
5867 : }
5868 :
5869 :
5870 63612 : static int16_t write_stream_dct_coeffs_omasa(
5871 : int16_t *q_idx, /* i : array of indexes to be written */
5872 : const int16_t len_stream, /* i : array length */
5873 : BSTR_ENC_HANDLE hMetaData, /* i/o: metadata bitstream */
5874 : const int16_t first_line, /* i : is first line of the matrix? 1/0 */
5875 : const int16_t low_bitrate_mode /* i : is low bitrate mode? if yes, limit the number of bits written */
5876 : )
5877 : {
5878 63612 : int16_t nb_bits = 0, bits_pos;
5879 : uint16_t nb_GR_min;
5880 : int16_t i, j;
5881 : int16_t changed, update_needed;
5882 :
5883 : int16_t GR1, GR2, i_min;
5884 : int16_t max_bits;
5885 :
5886 63612 : bits_pos = hMetaData->nb_bits_tot;
5887 63612 : if ( low_bitrate_mode == 1 )
5888 : {
5889 6234 : max_bits = 50;
5890 : }
5891 : else
5892 : {
5893 57378 : max_bits = 1000;
5894 : }
5895 :
5896 : /* write DCT 0 component */
5897 : /* write sign only if not the very first DCT coeff */
5898 63612 : if ( first_line == 0 )
5899 : {
5900 0 : if ( q_idx[0] > 0 )
5901 : {
5902 0 : push_next_indice( hMetaData, 1, 1 );
5903 0 : push_next_indice( hMetaData, q_idx[0], BITS_MASA2TOTTAL_DCT0 );
5904 : }
5905 : else
5906 : {
5907 0 : push_next_indice( hMetaData, 0, 1 );
5908 0 : push_next_indice( hMetaData, -q_idx[0], BITS_MASA2TOTTAL_DCT0 );
5909 : }
5910 0 : nb_bits += BITS_MASA2TOTTAL_DCT0 + 1;
5911 : }
5912 : else
5913 : {
5914 63612 : push_next_indice( hMetaData, q_idx[0], BITS_MASA2TOTTAL_DCT0 );
5915 63612 : nb_bits += BITS_MASA2TOTTAL_DCT0;
5916 : }
5917 :
5918 63612 : if ( q_idx[0] != 0 )
5919 : {
5920 51991 : i_min = 1;
5921 51991 : GR2 = 0;
5922 51991 : if ( len_stream >= 8 )
5923 : {
5924 42793 : nb_GR_min = find_optimal_GR_orders( &q_idx[1], len_stream - 1, 15, &GR1, &GR2, &i_min );
5925 : }
5926 : else
5927 : {
5928 9198 : nb_GR_min = find_optimal_GR_order( &q_idx[1], len_stream - 1, &GR1 );
5929 : }
5930 :
5931 51991 : assert( nb_GR_min < 1000 );
5932 51991 : changed = 1;
5933 51991 : update_needed = 0;
5934 51991 : while ( len_stream >= 8 && nb_GR_min > max_bits && changed >= 1 )
5935 : {
5936 0 : update_needed = 1;
5937 0 : changed = 0;
5938 0 : for ( j = len_stream - 1; j > 6; j-- )
5939 : {
5940 0 : if ( q_idx[j] >= 2 )
5941 : {
5942 :
5943 0 : if ( j > i_min )
5944 : {
5945 0 : changed = 1;
5946 0 : nb_GR_min -= ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR2 );
5947 0 : q_idx[j] -= 2;
5948 0 : nb_GR_min += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR2 );
5949 : }
5950 : else
5951 : {
5952 0 : changed = 1;
5953 0 : nb_GR_min -= ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR1 );
5954 0 : q_idx[j] -= 2;
5955 0 : nb_GR_min += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, 0 );
5956 : }
5957 : }
5958 0 : else if ( q_idx[j] == 1 )
5959 : {
5960 0 : if ( j > i_min )
5961 : {
5962 0 : changed = 1;
5963 0 : nb_GR_min -= ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR2 );
5964 0 : q_idx[j] -= 1;
5965 :
5966 0 : nb_GR_min += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR2 );
5967 : }
5968 : else
5969 : {
5970 0 : changed = 1;
5971 0 : nb_GR_min -= ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, GR1 );
5972 0 : q_idx[j] -= 1;
5973 0 : nb_GR_min += ivas_qmetadata_encode_extended_gr_length( q_idx[j], 100, 0 );
5974 : }
5975 : }
5976 0 : if ( nb_GR_min < max_bits )
5977 : {
5978 0 : break;
5979 : }
5980 : }
5981 : }
5982 :
5983 51991 : if ( update_needed == 1 )
5984 : {
5985 : /* re-calculate */
5986 : /* find optimum length of the part encoded with GR2 */
5987 0 : nb_GR_min = find_optimal_GR_orders( &q_idx[1], len_stream - 1, 15, &GR1, &GR2, &i_min );
5988 : }
5989 :
5990 51991 : if ( len_stream >= 8 )
5991 : {
5992 : /* write number of indexes encoded with GR2 on 4 bits */
5993 42793 : push_next_indice( hMetaData, i_min, 4 );
5994 42793 : nb_bits += 4;
5995 : /* write GR orders */
5996 42793 : push_next_indice( hMetaData, GR1 - 1, 1 );
5997 42793 : nb_bits += 1;
5998 42793 : if ( GR1 == 2 )
5999 : {
6000 16583 : push_next_indice( hMetaData, GR2, 1 );
6001 16583 : nb_bits += 1;
6002 : }
6003 :
6004 : /* write GR data */
6005 169879 : for ( i = 1; i <= i_min; i++ )
6006 : {
6007 127086 : ivas_qmetadata_encode_extended_gr( hMetaData, q_idx[i], 100, GR1 );
6008 : }
6009 :
6010 589854 : for ( i = i_min + 1; i < len_stream; i++ )
6011 : {
6012 547061 : ivas_qmetadata_encode_extended_gr( hMetaData, q_idx[i], 100, GR2 );
6013 : }
6014 : }
6015 : else
6016 : {
6017 : /* len_stream <= 8 */
6018 : /* write GR order */
6019 9198 : push_next_indice( hMetaData, GR1, 1 );
6020 9198 : nb_bits += 1;
6021 45646 : for ( i = 1; i < len_stream; i++ )
6022 : {
6023 36448 : ivas_qmetadata_encode_extended_gr( hMetaData, q_idx[i], 100, GR1 );
6024 : }
6025 : }
6026 :
6027 51991 : nb_bits += nb_GR_min;
6028 :
6029 51991 : assert( nb_bits == ( hMetaData->nb_bits_tot - bits_pos ) );
6030 : }
6031 :
6032 63612 : return nb_bits;
6033 : }
6034 :
6035 :
6036 : /*-------------------------------------------------------------------------
6037 : * ivas_omasa_encode_masa_to_total()
6038 : *
6039 : *------------------------------------------------------------------------*/
6040 :
6041 63612 : void ivas_omasa_encode_masa_to_total(
6042 : float masa_to_total_energy_ratio[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS],
6043 : BSTR_ENC_HANDLE hMetaData,
6044 : const int16_t low_bitrate_mode,
6045 : const int16_t nbands,
6046 : const int16_t nblocks )
6047 : {
6048 : int16_t i, j, k;
6049 : float data[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6050 : float q_dct_data[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6051 63612 : float step = STEP_M2T;
6052 :
6053 : int16_t q_idx[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6054 : float dct_data_tmp[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6055 : float dct_data[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6056 :
6057 : int16_t bits_pos, nb_bits;
6058 : int16_t n_streams, len_stream;
6059 :
6060 : Word32 q_dct_data_fx[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS],
6061 : dct_data_tmp_fx[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
6062 :
6063 : int64_t step_fx;
6064 63612 : step_fx = STEP_M2T_FX;
6065 : #ifdef DEBUG_MODE_QMETADATA
6066 : static FILE *pF = NULL;
6067 : static FILE *pF_ratio = NULL;
6068 :
6069 : if ( pF == NULL )
6070 : pF = fopen( "./res/qmetadata_ism_qidx__enc.txt", "w" );
6071 : if ( pF_ratio == NULL )
6072 : pF_ratio = fopen( "./res/qmetadata_masa2tot_enc.txt", "w" );
6073 :
6074 : #endif
6075 :
6076 63612 : bits_pos = hMetaData->nb_bits_tot;
6077 63612 : k = 0;
6078 532994 : for ( i = 0; i < nbands; i++ )
6079 : {
6080 1406227 : for ( j = 0; j < nblocks; j++ )
6081 : {
6082 936845 : data[k] = masa_to_total_energy_ratio[j][i];
6083 936845 : k++;
6084 : }
6085 : }
6086 :
6087 : /* DCT2 transform */
6088 63612 : n_streams = 1;
6089 63612 : len_stream = nbands * nblocks;
6090 63612 : switch ( len_stream )
6091 : {
6092 351 : case 4:
6093 351 : matrix_product( dct4, nblocks, nblocks, 0, data, 1, nblocks, 1, dct_data );
6094 351 : n_streams = 1;
6095 351 : len_stream = 4;
6096 351 : break;
6097 10349 : case 5:
6098 10349 : matrix_product( dct5, nbands, nbands, 0, data, 1, nbands, 1, dct_data );
6099 10349 : n_streams = 1;
6100 10349 : len_stream = nbands;
6101 10349 : break;
6102 0 : case 8:
6103 0 : matrix_product( dct8, nbands, nbands, 0, data, 1, nbands, 1, dct_data );
6104 0 : n_streams = 1;
6105 0 : len_stream = nbands;
6106 0 : break;
6107 21818 : case 12:
6108 21818 : matrix_product( dct12, nbands, nbands, 0, data, 1, nbands, 1, dct_data );
6109 21818 : n_streams = 1;
6110 21818 : len_stream = nbands;
6111 21818 : break;
6112 31094 : case 20:
6113 31094 : matrix_product( dct5, nbands, nbands, 0, data, nblocks, nbands, 1, dct_data_tmp );
6114 31094 : matrix_product( dct_data_tmp, nbands, nblocks, 0, dct4, nblocks, nblocks, 1, dct_data );
6115 31094 : n_streams = 1;
6116 31094 : len_stream = nbands * nblocks;
6117 31094 : break;
6118 0 : case 32:
6119 0 : matrix_product( dct8, nbands, nbands, 0, data, nblocks, nbands, 1, dct_data_tmp );
6120 0 : matrix_product( dct_data_tmp, nbands, nblocks, 0, dct4, nblocks, nblocks, 1, dct_data );
6121 0 : n_streams = nblocks;
6122 0 : len_stream = nbands;
6123 0 : break;
6124 0 : default:
6125 0 : printf( "Incorrect number of coefficients for OMASA.\n" );
6126 0 : break;
6127 : }
6128 :
6129 127224 : for ( k = 0; k < n_streams; k++ )
6130 : {
6131 63612 : j = k * len_stream;
6132 : /* quantize with fixed common step */
6133 63612 : q_idx[j] = (int16_t) rintf( dct_data[j] / step );
6134 :
6135 63612 : if ( q_idx[j] > ( ( 1 << BITS_MASA2TOTTAL_DCT0 ) - 1 ) ) /* limit DCT0 to BITS_MASA2TOTTAL_DCT0 bit representation */
6136 : {
6137 0 : q_idx[j] = ( ( 1 << BITS_MASA2TOTTAL_DCT0 ) - 1 );
6138 : }
6139 :
6140 63612 : q_dct_data[j] = step * q_idx[j];
6141 :
6142 63612 : if ( q_idx[j] == 0 )
6143 : {
6144 11621 : set_s( &q_idx[j], 0, len_stream );
6145 11621 : set_zero( &q_dct_data[j], len_stream );
6146 : }
6147 : else
6148 : {
6149 762586 : for ( i = 1; i < len_stream; i++ )
6150 : {
6151 710595 : q_idx[j + i] = (int16_t) rintf( dct_data[j + i] / step );
6152 710595 : q_dct_data[j + i] = step * q_idx[j + i];
6153 710595 : if ( q_idx[j + i] <= 0 )
6154 : {
6155 585724 : q_idx[j + i] = -2 * q_idx[j + i];
6156 : }
6157 : else
6158 : {
6159 124871 : q_idx[j + i] = 2 * q_idx[j + i] - 1;
6160 : }
6161 : }
6162 : }
6163 : }
6164 :
6165 : /* write data */
6166 63612 : nb_bits = 0;
6167 127224 : for ( i = 0; i < n_streams; i++ )
6168 : {
6169 63612 : nb_bits += write_stream_dct_coeffs_omasa( &q_idx[i * len_stream], len_stream, hMetaData, ( i == 0 ), low_bitrate_mode );
6170 : }
6171 :
6172 : /* reconstruct masa2total */
6173 63612 : q_dct_data_fx[0] = (Word32) ( ( step_fx * q_idx[0] ) >> 6 ); // Q25
6174 936845 : for ( i = 1; i < len_stream; i++ )
6175 : {
6176 873233 : if ( ( q_idx[i] & 1 ) == 0 )
6177 : {
6178 748362 : q_dct_data_fx[i] = (Word32) ( ( step_fx * ( -q_idx[i] ) ) >> 7 ); // Q25
6179 : }
6180 : ELSE
6181 : {
6182 124871 : q_dct_data_fx[i] = (Word32) ( ( step_fx * ( q_idx[i] + 1 ) ) >> 7 ); // Q25
6183 : }
6184 : }
6185 63612 : SWITCH( len_stream )
6186 : {
6187 351 : case 4:
6188 351 : matrix_product_q30_fx( dct4_fx, nblocks, nblocks, 1, q_dct_data_fx, nblocks, 1, 0, dct_data_tmp_fx );
6189 351 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nblocks ); /*Q30*/
6190 351 : BREAK;
6191 10349 : case 5:
6192 10349 : matrix_product_q30_fx( dct5_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
6193 10349 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
6194 10349 : BREAK;
6195 0 : case 8:
6196 0 : matrix_product_q30_fx( dct8_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
6197 0 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
6198 0 : BREAK;
6199 21818 : case 12:
6200 21818 : matrix_product_q30_fx( dct12_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
6201 21818 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
6202 21818 : BREAK;
6203 31094 : case 20:
6204 31094 : matrix_product_fx( dct5_fx, nbands, nbands, 1, q_dct_data_fx, nbands, nblocks, 0, dct_data_tmp_fx );
6205 31094 : matrix_product_q30_fx( dct_data_tmp_fx, nbands, nblocks, 0, dct4_fx, nblocks, nblocks, 0, q_dct_data_fx ); /* reuse of variable*/
6206 31094 : BREAK;
6207 0 : case 32:
6208 0 : matrix_product_fx( dct8_fx, nbands, nbands, 1, q_dct_data_fx, nbands, nblocks, 0, dct_data_tmp_fx );
6209 0 : matrix_product_q30_fx( dct_data_tmp_fx, nbands, nblocks, 0, dct4_fx, nblocks, nblocks, 0, q_dct_data_fx );
6210 0 : BREAK;
6211 0 : default:
6212 0 : printf( "Incorrect number of coefficients for OMASA.\n" );
6213 0 : BREAK;
6214 : }
6215 :
6216 : /* this is to make sure the comparison to the threshold 0.98 will go the same way in */
6217 : /* fixed point and floating point without having to drag the fixed point values to the */
6218 : /* comparison place in the code; */
6219 : /* 1052266987 is 0.98 in Q30 it is not needed in the fixed point */
6220 1000457 : for ( i = 0; i < nblocks * nbands; i++ )
6221 : {
6222 936845 : if ( q_dct_data_fx[i] >= 1052266987 && q_dct_data_fx[i] < 1052400000 )
6223 : {
6224 267 : q_dct_data_fx[i] = 1052400000;
6225 : }
6226 : }
6227 :
6228 63612 : k = 0;
6229 221559 : for ( i = 0; i < nblocks; i++ )
6230 : {
6231 1094792 : for ( j = 0; j < nbands; j++ )
6232 : {
6233 936845 : masa_to_total_energy_ratio[i][j] = q_dct_data_fx[k] / (float) ( 1 << 30 );
6234 936845 : masa_to_total_energy_ratio[i][j] = max( 0.0f, masa_to_total_energy_ratio[i][j] );
6235 936845 : masa_to_total_energy_ratio[i][j] = min( 1.0f, masa_to_total_energy_ratio[i][j] );
6236 936845 : k++;
6237 : }
6238 : }
6239 :
6240 63612 : if ( nblocks == 1 )
6241 : {
6242 128668 : for ( i = 1; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
6243 : {
6244 1037184 : for ( j = 0; j < nbands; j++ )
6245 : {
6246 940683 : masa_to_total_energy_ratio[i][j] = masa_to_total_energy_ratio[0][j];
6247 : }
6248 : }
6249 : }
6250 :
6251 63612 : if ( nbands == 1 )
6252 : {
6253 1755 : for ( j = 1; j < 5; j++ )
6254 : {
6255 7020 : for ( i = 0; i < nblocks; i++ )
6256 : {
6257 5616 : masa_to_total_energy_ratio[i][j] = masa_to_total_energy_ratio[i][0];
6258 : }
6259 : }
6260 : }
6261 :
6262 63612 : assert( nb_bits == ( hMetaData->nb_bits_tot - bits_pos ) );
6263 :
6264 : #ifdef DEBUG_MODE_QMETADATA
6265 : {
6266 :
6267 : fprintf( pF, "frame %d: ", frame );
6268 : fprintf( pF_ratio, "frame %d: ", frame );
6269 :
6270 :
6271 : /* direction_distance( elevation_orig, azimuth_orig, q_direction, nbands, nblocks, mat_dist );*/
6272 : for ( i = 0; i < nbands; i++ )
6273 : {
6274 : for ( j = 0; j < 4; j++ )
6275 : {
6276 : fprintf( pF_ratio, " %5.2f ", hQMetaData->masa_to_total_energy_ratio[j][i] );
6277 : }
6278 : }
6279 : for ( i = 0; i < 20; i++ )
6280 : {
6281 : fprintf( pF, " %4d ", q_idx[i] );
6282 : }
6283 : fprintf( pF, "\n" );
6284 : fprintf( pF_ratio, "\n" );
6285 : }
6286 : #endif
6287 :
6288 63612 : return;
6289 : }
|