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 "ivas_prot.h"
39 : #include "ivas_rom_com.h"
40 : #include "ivas_rom_dec.h"
41 : #include "wmc_auto.h"
42 : #include "prot.h"
43 : #include "basop_settings.h"
44 :
45 :
46 : /*-----------------------------------------------------------------------*
47 : * Local function prototypes
48 : *-----------------------------------------------------------------------*/
49 :
50 : static int16_t ivas_qmetadata_entropy_decode_diffuseness( uint16_t *bitstream, int16_t *index, IVAS_QDIRECTION *q_direction, uint16_t *diffuseness_index_max_ec_frame );
51 :
52 : static int16_t ivas_qmetadata_entropy_decode_df_ratio( uint16_t *bitstream, int16_t *index, IVAS_QDIRECTION *q_direction, int16_t *dfRatio_bits );
53 :
54 : static int16_t ivas_qmetadata_entropy_decode_dir( IVAS_QDIRECTION *q_direction, uint16_t *bitstream, int16_t *index, const uint16_t diffuseness_index_max_ec_frame, const int16_t nbands, const int16_t start_band, const int16_t hrmasa_flag );
55 :
56 : static int16_t ivas_qmetadata_raw_decode_dir( IVAS_QDIRECTION *q_direction, uint16_t *bitstream, int16_t *index, const int16_t nbands, const int16_t start_band, const int16_t hrmasa_flag );
57 :
58 : static uint16_t ivas_qmetadata_DecodeQuasiUniform( const uint16_t *bitstream, int16_t *index, const uint16_t alphabet_size );
59 :
60 : static int16_t ivas_qmetadata_ReorderElevationDecoded( const int16_t elev_dist, const int16_t elev_avg, const int16_t elev_alph );
61 :
62 : static int16_t read_directions( IVAS_QDIRECTION *q_direction, const uint8_t coding_subbands, const uint8_t masa_subframes, uint16_t *bitstream, int16_t *pbit_pos, int16_t *ind_order );
63 :
64 : static int16_t read_common_direction( uint16_t *bitstream, IVAS_QDIRECTION *q_direction, const int16_t j, const int16_t no_subframes, const int16_t bits_total, int16_t *pbit_pos );
65 :
66 : static int16_t decode_fixed_rate( IVAS_QDIRECTION *q_direction, const uint16_t *bitstream, int16_t *pbit_pos, const int16_t b, const int16_t nblocks );
67 :
68 : static int16_t decode_azimuth( IVAS_QDIRECTION *q_direction, uint16_t *bitstream, int16_t *pbit_pos, const int16_t idx_subband, const int16_t masa_subframes );
69 :
70 : static int16_t decode_elevation( IVAS_QDIRECTION *q_direction, uint16_t *bitstream, int16_t *pbit_pos, const int16_t j, const int16_t masa_subframes );
71 :
72 : static int16_t decode_azimuth2D( IVAS_QDIRECTION *q_direction, uint16_t *bitstream, const int16_t coding_subbands, int16_t *pbit_pos, const int16_t no_frames );
73 :
74 : static void set_zero_direction( IVAS_QDIRECTION *q_direction, const int16_t idx_band, const int16_t len );
75 :
76 : static int16_t read_truncGR_azimuth( uint16_t *bitstream, IVAS_QDIRECTION *q_direction, const int16_t j, const int16_t no_subframes, int16_t *pbit_pos );
77 :
78 : static ivas_error read_huf( int16_t *num_bits_read, const uint16_t *bitstream, uint16_t *out, const int16_t start_pos, const int16_t len, const int16_t *huff_code, const int16_t max_len );
79 :
80 : static int16_t read_coherence_data( uint16_t *bitstream, int16_t *p_bit_pos, IVAS_QMETADATA *hQMetaData, const int16_t idx_dir, const int16_t hrmasa_flag );
81 :
82 : static int16_t read_surround_coherence( uint16_t *bitstream, int16_t *p_bit_pos, IVAS_QMETADATA *hQMetaData );
83 :
84 : static void decode_spread_coherence( IVAS_QMETADATA_HANDLE hQMetaData, int16_t idx_d, const int16_t no_frames, const int16_t hrmasa_flag );
85 :
86 : static void decode_combined_index( uint64_t comb_index, const int16_t *no_cv_vec, uint16_t *index, const int16_t len );
87 :
88 : static int16_t ivas_diffuseness_huff_ec_decode( const uint16_t *bitstream, int16_t *index, int16_t av );
89 :
90 : static int16_t read_GR_min_removed_data( uint16_t *bitstream, int16_t *p_bit_pos, const int16_t *no_cv_vec, const int16_t no_data, int16_t *decoded_idx, const int16_t no_symb );
91 :
92 : static int16_t decode_fixed_rate_composed_index_coherence( uint16_t *bitstream, int16_t *p_bit_pos, const int16_t no_bands, int16_t *no_cv_vec, uint16_t *decoded_index, const int16_t no_symb );
93 :
94 : static int16_t ivas_qmetadata_entropy_decode_diffuseness_hr_512( uint16_t *bitstream, int16_t *index, IVAS_QDIRECTION *q_direction );
95 :
96 : static int16_t ivas_qmetadata_raw_decode_dir_512( IVAS_QDIRECTION *q_direction, uint16_t *bitstream, int16_t *index, const int16_t nbands, const int16_t start_band, const SPHERICAL_GRID_DATA *sph_grid16 );
97 :
98 : static int16_t read_surround_coherence_hr( uint16_t *bitstream, int16_t *p_bit_pos, IVAS_QMETADATA *hQMetaData );
99 :
100 : static int16_t read_coherence_data_hr_512( uint16_t *bitstream, int16_t *p_bit_pos, IVAS_QMETADATA *hQMetaData, const int16_t idx_dir, const int16_t nbits_coh );
101 :
102 : static void read_stream_dct_coeffs_omasa( int16_t *q_idx,
103 : Word32 *q_dct_data_fx,
104 : const int16_t len_stream,
105 : uint16_t *bit_stream,
106 : int16_t *index,
107 : const int16_t first_line );
108 :
109 :
110 : /*-----------------------------------------------------------------------*
111 : * Global function definitions
112 : *-----------------------------------------------------------------------*/
113 :
114 : /*-----------------------------------------------------------------------*
115 : * ivas_qmetadata_dec_decode()
116 : *
117 : * Main function for decoding Spatial Metadata
118 : *-----------------------------------------------------------------------*/
119 :
120 : /*! r: number of bits read */
121 8685786 : int16_t ivas_qmetadata_dec_decode(
122 : IVAS_QMETADATA_HANDLE hQMetaData, /* i/o: hQMetaData handle */
123 : uint16_t *bitstream, /* i : bitstream */
124 : int16_t *index, /* i/o: bitstream position */
125 : const int16_t hodirac_flag /* i : flag to indicate HO-DirAC mode */
126 : )
127 : {
128 : int16_t d, b, m;
129 : uint16_t diffuseness_index_max_ec_frame;
130 : uint16_t diffuseness_index_max_ec_frame_pre[QMETADATA_MAX_NO_DIRECTIONS];
131 : int16_t bits_dir_raw_pre[QMETADATA_MAX_NO_DIRECTIONS];
132 : int16_t dir2band;
133 : int16_t bits_diff_sum;
134 : int16_t bits_diff, bits_coherence;
135 : int16_t bits_dir_raw;
136 : int16_t bits_dir;
137 : int16_t nbands, nblocks, start_band;
138 : IVAS_QDIRECTION *q_direction;
139 : int16_t start_index_0;
140 : int16_t total_bits_1dir;
141 : int16_t signal_bits;
142 : int16_t bits_no_dirs_coh, bits_sur_coherence;
143 : uint16_t all_coherence_zero;
144 : int16_t ec_flag;
145 : int16_t raw_flag[MASA_MAXIMUM_CODING_SUBBANDS];
146 : int16_t diff_bits;
147 : int16_t dfRatio_bits[MASA_MAXIMUM_CODING_SUBBANDS];
148 : int16_t no_TF;
149 : int16_t p[MASA_MAXIMUM_CODING_SUBBANDS], dif_p[MASA_MAXIMUM_CODING_SUBBANDS];
150 : int16_t bits_dir_target;
151 : int16_t bits_dir_used;
152 : int16_t reduce_bits;
153 : int16_t ind_order[MASA_MAXIMUM_CODING_SUBBANDS];
154 :
155 : #ifdef DEBUG_MODE_QMETADATA
156 : static FILE *pF = NULL;
157 : static FILE *pF_azi = NULL;
158 : static FILE *pF_ele = NULL;
159 : static FILE *pF_ratio = NULL;
160 : static FILE *pF_spcoh = NULL;
161 : static FILE *pF_surcoh = NULL;
162 :
163 : if ( pF == NULL )
164 : pF = fopen( "./res/qmetadata_dec.txt", "w" );
165 : if ( pF_azi == NULL )
166 : pF_azi = fopen( "./res/qmetadata_azi_dec.txt", "w" );
167 : if ( pF_ele == NULL )
168 : pF_ele = fopen( "./res/qmetadata_ele_dec.txt", "w" );
169 : if ( pF_ratio == NULL )
170 : pF_ratio = fopen( "./res/qmetadata_ratio_dec.txt", "w" );
171 : if ( pF_spcoh == NULL )
172 : pF_spcoh = fopen( "./res/qmetadata_spcoh_dec.txt", "w" );
173 : if ( pF_surcoh == NULL )
174 : pF_surcoh = fopen( "./res/qmetadata_surcoh_dec.txt", "w" );
175 : #endif
176 :
177 8685786 : ec_flag = 0;
178 8685786 : start_index_0 = *index;
179 :
180 : /*Coherence flag decoding*/
181 8685786 : bits_no_dirs_coh = 0;
182 8685786 : all_coherence_zero = 1;
183 8685786 : if ( hQMetaData->coherence_flag )
184 : {
185 : /* read if coherence is zero */
186 1655613 : all_coherence_zero = bitstream[( *index )--];
187 1655613 : bits_no_dirs_coh += 1;
188 : }
189 :
190 8685786 : hQMetaData->all_coherence_zero = (uint8_t) all_coherence_zero;
191 :
192 8685786 : if ( hQMetaData->no_directions == 2 )
193 : {
194 : /* Read which bands have 2 directions */
195 785370 : hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands;
196 785370 : set_c( (int8_t *) hQMetaData->twoDirBands, 0, hQMetaData->q_direction[0].cfg.nbands );
197 785370 : d = *index;
198 785370 : dif_p[0] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
199 785370 : p[0] = dif_p[0];
200 785370 : hQMetaData->twoDirBands[p[0]] = 1;
201 5594769 : for ( b = 1; b < hQMetaData->numTwoDirBands; b++ )
202 : {
203 4809399 : dif_p[b] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
204 4809399 : p[b] = p[b - 1] + dif_p[b] + 1;
205 4809399 : hQMetaData->twoDirBands[p[b]] = 1;
206 : }
207 785370 : bits_no_dirs_coh += ( d - *index );
208 : }
209 :
210 8685786 : bits_diff_sum = 0;
211 8685786 : bits_diff_sum += ivas_qmetadata_entropy_decode_diffuseness( bitstream, index, &( hQMetaData->q_direction[0] ), &diffuseness_index_max_ec_frame_pre[0] );
212 :
213 8685786 : if ( hodirac_flag )
214 : {
215 382767 : if ( hQMetaData->no_directions == 2 )
216 : {
217 : /* Calculate bits for dfRatio */
218 382767 : dir2band = 0;
219 4593204 : for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
220 : {
221 4210437 : if ( hQMetaData->twoDirBands[b] == 1 )
222 : {
223 4210437 : dfRatio_bits[dir2band] = ivas_get_df_ratio_bits_hodirac( hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] );
224 4210437 : dir2band++;
225 : }
226 : }
227 :
228 382767 : bits_diff_sum += ivas_qmetadata_entropy_decode_df_ratio( bitstream, index, &( hQMetaData->q_direction[1] ), dfRatio_bits );
229 : }
230 : }
231 : else
232 : {
233 8303019 : if ( hQMetaData->no_directions == 2 )
234 : {
235 : /* Calculate bits for dfRatio */
236 402603 : dir2band = 0;
237 4682385 : for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
238 : {
239 4279782 : if ( hQMetaData->twoDirBands[b] == 1 )
240 : {
241 1384332 : dfRatio_bits[dir2band] = ivas_get_df_ratio_bits( hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] );
242 1384332 : dir2band++;
243 : }
244 : }
245 :
246 402603 : bits_diff_sum += ivas_qmetadata_entropy_decode_df_ratio( bitstream, index, &( hQMetaData->q_direction[1] ), dfRatio_bits );
247 : }
248 : }
249 :
250 : /* Calculate direct-to-total energy ratios for both directions from diffuse-to-total ratio and distribution factor of direct-to-total ratios */
251 8685786 : if ( hQMetaData->no_directions == 2 )
252 : {
253 785370 : dir2band = 0;
254 9275589 : for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
255 : {
256 8490219 : if ( hQMetaData->twoDirBands[b] == 1 )
257 : {
258 : float diffRatio, dfRatio, dir1ratio, dir2ratio;
259 : int16_t dfRatio_qsteps;
260 :
261 5594769 : diffRatio = diffuseness_reconstructions[hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0]];
262 5594769 : dfRatio_qsteps = 1 << dfRatio_bits[dir2band];
263 : /* already encoded as total and ratios in HO-DirAC */
264 5594769 : if ( hodirac_flag )
265 : {
266 4210437 : dfRatio = usdequant( hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0], 0.0f, 1.f / ( dfRatio_qsteps - 1 ) );
267 4210437 : dir1ratio = 1.f - diffRatio;
268 4210437 : dir2ratio = dfRatio;
269 : }
270 : else
271 : {
272 1384332 : dfRatio = usdequant( hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0], 0.5f, 0.5f / ( dfRatio_qsteps - 1 ) );
273 :
274 1384332 : dir1ratio = dfRatio * ( 1.0f - diffRatio );
275 1384332 : dir2ratio = ( 1.0f - diffRatio ) - dir1ratio;
276 : }
277 :
278 : /* Requantize the 1 - dirRatio separately for each direction to obtain inverted dirRatio index. These are used in further decoding. */
279 5594769 : hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] = masa_sq( 1.0f - dir1ratio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
280 5594769 : if ( hodirac_flag )
281 : {
282 : float tmp;
283 4210437 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0] = usquant( dir2ratio, &tmp, 0.0f, 1.f / ( DIRAC_DIFFUSE_LEVELS - 1 ), DIRAC_DIFFUSE_LEVELS );
284 : }
285 : else
286 : {
287 1384332 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0] = masa_sq( 1.0f - dir2ratio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
288 : }
289 :
290 25987743 : for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
291 : {
292 20392974 : hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = dir1ratio;
293 20392974 : hQMetaData->q_direction[0].band_data[b].energy_ratio_index[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
294 : }
295 :
296 25987743 : for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
297 : {
298 20392974 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[m] = dir2ratio;
299 20392974 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[m] = hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0];
300 : }
301 :
302 5594769 : dir2band++;
303 : }
304 : else
305 : {
306 : /* 1dir band */
307 2895450 : hQMetaData->q_direction[0].band_data[b].energy_ratio[0] = 1.0f - diffuseness_reconstructions[hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0]];
308 6747810 : for ( m = 1; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
309 : {
310 3852360 : hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio[0];
311 3852360 : hQMetaData->q_direction[0].band_data[b].energy_ratio_index[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
312 : }
313 : }
314 : }
315 : }
316 : else
317 : {
318 : /* With 1dir band, the decoded index is directly diffuseness and we can decode to direct-to-total ratio with 1 - diff. */
319 41128389 : for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
320 : {
321 33227973 : hQMetaData->q_direction[0].band_data[b].energy_ratio[0] = 1.0f - diffuseness_reconstructions[hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0]];
322 100038222 : for ( m = 1; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
323 : {
324 66810249 : hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio[0];
325 66810249 : hQMetaData->q_direction[0].band_data[b].energy_ratio_index[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
326 : }
327 : }
328 : }
329 :
330 : /* To decode directions correctly for 2dir bands, we need to obtain the compensated direct-to-total ratios and their
331 : * corresponding inverted indices. */
332 8685786 : bits_dir_raw_pre[0] = 0;
333 8685786 : bits_dir_raw_pre[1] = 0;
334 8685786 : dir2band = 0;
335 50403978 : for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
336 : {
337 41718192 : if ( hQMetaData->no_directions == 2 && hQMetaData->twoDirBands[b] == 1 )
338 5594769 : {
339 : int16_t index_dirRatio1Inv, index_dirRatio2Inv, index_dirRatio1Inv_mod, index_dirRatio2Inv_mod;
340 :
341 5594769 : index_dirRatio1Inv = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
342 5594769 : index_dirRatio2Inv = hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0];
343 :
344 5594769 : masa_compensate_two_dir_energy_ratio_index( index_dirRatio1Inv, index_dirRatio2Inv, &index_dirRatio1Inv_mod, &index_dirRatio2Inv_mod, hodirac_flag );
345 :
346 25987743 : for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
347 : {
348 20392974 : hQMetaData->q_direction[0].band_data[b].energy_ratio_index_mod[m] = index_dirRatio1Inv_mod;
349 20392974 : hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m] = bits_direction_masa[index_dirRatio1Inv_mod];
350 20392974 : bits_dir_raw_pre[0] += hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m];
351 : }
352 :
353 25987743 : for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
354 : {
355 20392974 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index_mod[m] = index_dirRatio2Inv_mod;
356 20392974 : hQMetaData->q_direction[1].band_data[dir2band].bits_sph_idx[m] = bits_direction_masa[index_dirRatio2Inv_mod];
357 20392974 : bits_dir_raw_pre[1] += hQMetaData->q_direction[1].band_data[dir2band].bits_sph_idx[m];
358 : }
359 :
360 5594769 : dir2band++;
361 : }
362 : else
363 : {
364 142909455 : for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
365 : {
366 106786032 : hQMetaData->q_direction[0].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
367 106786032 : hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m] = bits_direction_masa[hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0]];
368 106786032 : bits_dir_raw_pre[0] += hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m];
369 : }
370 : }
371 : }
372 :
373 8685786 : if ( hQMetaData->no_directions == 2 )
374 : {
375 785370 : 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;
376 785370 : if ( ( all_coherence_zero == 0 ) && ( hQMetaData->metadata_max_bits - bits_no_dirs_coh - 4.3f * no_TF - bits_diff_sum >= MASA_MIN_BITS_SURR_COH ) )
377 : {
378 383145 : bits_sur_coherence = read_surround_coherence( bitstream, index, hQMetaData );
379 : }
380 : else
381 : {
382 402225 : bits_sur_coherence = 0;
383 : /*Surround coherence*/
384 4878414 : for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
385 : {
386 4476189 : if ( hQMetaData->surcoh_band_data != NULL )
387 : {
388 265752 : set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
389 : }
390 : }
391 : }
392 785370 : bits_no_dirs_coh += bits_sur_coherence;
393 785370 : 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;
394 : }
395 : else
396 : {
397 7900416 : no_TF = hQMetaData->q_direction[0].cfg.nbands * hQMetaData->q_direction[0].cfg.nblocks;
398 7900416 : if ( ( all_coherence_zero == 0 ) && ( hQMetaData->metadata_max_bits - bits_no_dirs_coh - 4.3f * no_TF - bits_diff_sum >= MASA_MIN_BITS_SURR_COH ) )
399 : {
400 1104921 : bits_sur_coherence = read_surround_coherence( bitstream, index, hQMetaData );
401 : }
402 : else
403 : {
404 6795495 : bits_sur_coherence = 0;
405 : /*Surround coherence*/
406 30084450 : for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
407 : {
408 23288955 : if ( hQMetaData->surcoh_band_data != NULL )
409 : {
410 1502550 : set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
411 : }
412 : }
413 : }
414 7900416 : bits_no_dirs_coh += bits_sur_coherence;
415 :
416 7900416 : total_bits_1dir = hQMetaData->metadata_max_bits - bits_no_dirs_coh;
417 : }
418 :
419 8685786 : bits_dir_target = 0;
420 8685786 : bits_dir_used = 0;
421 :
422 18156942 : for ( d = 0; d < hQMetaData->no_directions; d++ )
423 : {
424 9471156 : q_direction = &hQMetaData->q_direction[d];
425 9471156 : nbands = q_direction->cfg.nbands;
426 9471156 : nblocks = q_direction->cfg.nblocks;
427 9471156 : start_band = q_direction->cfg.start_band;
428 :
429 9471156 : diffuseness_index_max_ec_frame = diffuseness_index_max_ec_frame_pre[0];
430 9471156 : if ( d == 0 )
431 : {
432 8685786 : bits_diff = bits_diff_sum;
433 : }
434 : else
435 : {
436 785370 : bits_diff = 0;
437 : }
438 9471156 : bits_dir_raw = bits_dir_raw_pre[d];
439 :
440 : /* Read coherence, if any */
441 9471156 : bits_coherence = 0;
442 :
443 9471156 : if ( all_coherence_zero == 0 )
444 : {
445 1903677 : bits_coherence = read_coherence_data( bitstream, index, hQMetaData, d, 0 );
446 : }
447 : else
448 : {
449 : /*Surround coherence*/
450 39646482 : for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
451 : {
452 32079003 : if ( hQMetaData->surcoh_band_data != NULL )
453 : {
454 1871724 : set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
455 : }
456 :
457 32079003 : if ( hQMetaData->q_direction[d].coherence_band_data != NULL )
458 : {
459 1871724 : set_c( (int8_t *) hQMetaData->q_direction[d].coherence_band_data[b].spread_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
460 : }
461 : }
462 : }
463 :
464 : /* Read 2D signaling*/
465 9471156 : q_direction->not_in_2D = bitstream[( *index )--];
466 9471156 : signal_bits = 1;
467 :
468 : /* Read EC signaling */
469 9471156 : ec_flag = 0;
470 9471156 : if ( total_bits_1dir + bits_sur_coherence <= hQMetaData->qmetadata_max_bit_req )
471 : {
472 7241622 : ec_flag = bitstream[( *index )--];
473 7241622 : signal_bits++;
474 7241622 : if ( nblocks > 1 )
475 : {
476 5275323 : if ( ec_flag )
477 : {
478 308019 : ec_flag += bitstream[( *index )--];
479 308019 : signal_bits++;
480 : }
481 : }
482 : }
483 :
484 : /* Decode quantized directions frame-wise */
485 9471156 : if ( ec_flag == 0 ) /* EC 1*/
486 : {
487 9001050 : bits_dir = 0;
488 9001050 : raw_flag[0] = bitstream[( *index )--];
489 9001050 : bits_dir++;
490 :
491 9001050 : if ( raw_flag[0] == 0 )
492 : {
493 5450703 : bits_dir += ivas_qmetadata_entropy_decode_dir( q_direction, bitstream, index, diffuseness_index_max_ec_frame, nbands, start_band, 0 );
494 : }
495 : else
496 : {
497 3550347 : bits_dir += ivas_qmetadata_raw_decode_dir( q_direction, bitstream, index, nbands, start_band, 0 );
498 : }
499 : }
500 : /* Decode quantized directions band-wise */
501 470106 : else if ( ec_flag == 1 && ( nblocks > 1 ) ) /* EC2 */
502 : {
503 91779 : bits_dir = 0;
504 572442 : for ( b = start_band; b < nbands; b++ )
505 : {
506 480663 : raw_flag[b] = bitstream[( *index )--];
507 480663 : bits_dir++;
508 : }
509 :
510 : /* Read EC bits*/
511 91779 : diff_bits = bits_diff + bits_coherence + signal_bits - total_bits_1dir;
512 :
513 572442 : for ( b = start_band; b < nbands; b++ )
514 : {
515 480663 : if ( raw_flag[b] == 0 )
516 : {
517 210417 : bits_dir += ivas_qmetadata_entropy_decode_dir( q_direction, bitstream, index, diffuseness_index_max_ec_frame, b + 1, b, 0 );
518 : }
519 : else
520 : {
521 270246 : diff_bits += q_direction->band_data[b].bits_sph_idx[0] * q_direction->cfg.nblocks;
522 : }
523 : }
524 91779 : diff_bits += bits_dir;
525 :
526 : /* Small requantization?*/
527 91779 : if ( q_direction->not_in_2D > 0 )
528 : {
529 : /* This is not an ideal solution but mirrors better encoder */
530 : int16_t i, j;
531 : uint16_t bits_temp[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
532 :
533 538122 : for ( i = q_direction->cfg.start_band; i < q_direction->cfg.nbands; i++ )
534 : {
535 2260095 : for ( j = 0; j < q_direction->cfg.nblocks; j++ )
536 : {
537 1808076 : bits_temp[i][j] = q_direction->band_data[i].bits_sph_idx[j];
538 : }
539 : }
540 :
541 86103 : small_reduction_direction( q_direction, bits_temp, raw_flag, &diff_bits );
542 :
543 538122 : for ( i = q_direction->cfg.start_band; i < q_direction->cfg.nbands; i++ )
544 : {
545 2260095 : for ( j = 0; j < q_direction->cfg.nblocks; j++ )
546 : {
547 1808076 : q_direction->band_data[i].bits_sph_idx[j] = bits_temp[i][j];
548 : }
549 : }
550 : }
551 :
552 : #ifdef DEBUGGING
553 : assert( ( diff_bits <= 0 ) || ( q_direction->not_in_2D == 0 ) );
554 : #endif
555 : /* Read raw-coded bits*/
556 572442 : for ( b = start_band; b < nbands; b++ )
557 : {
558 480663 : if ( raw_flag[b] )
559 : {
560 270246 : bits_dir += ivas_qmetadata_raw_decode_dir( q_direction, bitstream, index, b + 1, b, 0 );
561 : }
562 : }
563 : }
564 : /* Decode requantized directions */
565 : else /* EC3 */
566 : {
567 : int16_t dummy;
568 378327 : ec_flag = 2;
569 :
570 378327 : if ( hQMetaData->is_masa_ivas_format == 0 )
571 : {
572 95112 : reduce_bits = bits_dir_raw - ( total_bits_1dir - bits_diff - bits_coherence - signal_bits );
573 95112 : ind_order[0] = -1;
574 : }
575 : else
576 : {
577 283215 : ind_order[0] = 0;
578 283215 : reduce_bits = min( nbands * nblocks + MASA_BIT_REDUCT_PARAM, bits_dir_raw - ( total_bits_1dir - bits_diff - bits_coherence - signal_bits ) );
579 283215 : if ( reduce_bits > bits_dir_raw - nbands * nblocks )
580 : {
581 273 : reduce_bits = bits_dir_raw - nbands * nblocks;
582 : }
583 : }
584 378327 : only_reduce_bits_direction( &dummy, q_direction, reduce_bits, nbands, nblocks, ind_order );
585 :
586 : /* Read directions */
587 378327 : bits_dir = read_directions( q_direction, (uint8_t) nbands, (uint8_t) nblocks, bitstream, index, ind_order );
588 : }
589 :
590 9471156 : if ( bits_coherence > 0 )
591 : {
592 1883826 : if ( nblocks > 1 )
593 : {
594 1256460 : decode_spread_coherence( hQMetaData, d, nblocks, 0 );
595 : }
596 : }
597 : else
598 : {
599 39563499 : for ( b = start_band; b < nbands; b++ )
600 : {
601 31976169 : if ( q_direction->coherence_band_data != NULL )
602 : {
603 1768890 : set_c( (int8_t *) q_direction->coherence_band_data[b].spread_coherence, 0, nblocks );
604 : }
605 : }
606 : }
607 9471156 : if ( d == 0 )
608 : {
609 8685786 : total_bits_1dir = hQMetaData->metadata_max_bits - ( start_index_0 - *index );
610 : }
611 :
612 9471156 : bits_dir_target += bits_dir_raw;
613 9471156 : bits_dir_used += bits_dir;
614 :
615 : #ifdef DEBUG_MODE_QMETADATA
616 : fprintf( pF, "frame %d: diff %d coh %d surcoh %d ", frame, bits_diff, bits_coherence, bits_sur_coherence );
617 : fprintf( pF, "dir %d %d,%d,%d\n", ec_flag, start_index_0 - *index, total_bits_1dir, bits_dir_raw );
618 :
619 : fprintf( pF_azi, "frame %d/dir/ec %d/%d: ", frame, d, ec_flag );
620 : fprintf( pF_ele, "frame %d/dir/ec %d/%d: ", frame, d, ec_flag );
621 : fprintf( pF_ratio, "frame %d/dir %d: ", frame, d );
622 : fprintf( pF_spcoh, "frame %d/dir %d: ", frame, d );
623 : if ( d == 0 )
624 : {
625 : fprintf( pF_surcoh, "frame %d/dir %d: ", frame, d );
626 : }
627 : for ( b = start_band; b < nbands; b++ )
628 : {
629 : for ( m = 0; m < q_direction->cfg.nblocks; m++ )
630 : {
631 : fprintf( pF_azi, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[b].azimuth[m] ) / 100.f );
632 : fprintf( pF_ele, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[b].elevation[m] ) / 100.f );
633 : fprintf( pF_ratio, " %1.3f ", q_direction->band_data[b].energy_ratio[m] );
634 : if ( q_direction->coherence_band_data != NULL )
635 : {
636 : fprintf( pF_spcoh, " %d ", q_direction->coherence_band_data[b].spread_coherence[m] );
637 : }
638 : if ( d == 0 && hQMetaData->surcoh_band_data != NULL )
639 : {
640 : fprintf( pF_surcoh, " %d ", hQMetaData->surcoh_band_data[b].surround_coherence[m] );
641 : }
642 : }
643 : }
644 : fprintf( pF_azi, "\n" );
645 : fprintf( pF_ele, "\n" );
646 : fprintf( pF_ratio, "\n" );
647 : fprintf( pF_spcoh, "\n" );
648 : if ( d == 0 )
649 : {
650 : fprintf( pF_surcoh, "\n" );
651 : }
652 : #endif
653 : }
654 :
655 : /* move 2 dir data to its correct subband */
656 8685786 : if ( hQMetaData->no_directions == 2 )
657 : {
658 785370 : d = hQMetaData->q_direction[1].cfg.nbands - 1;
659 785370 : nblocks = hQMetaData->q_direction[0].cfg.nblocks;
660 :
661 9275589 : for ( b = hQMetaData->q_direction[0].cfg.nbands - 1; b >= 0; b-- )
662 : {
663 8490219 : if ( hQMetaData->twoDirBands[b] == 1 )
664 : {
665 5594769 : mvr2r( hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].band_data[b].azimuth, nblocks );
666 5594769 : mvr2r( hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].band_data[b].elevation, nblocks );
667 5594769 : mvr2r( hQMetaData->q_direction[1].band_data[d].energy_ratio, hQMetaData->q_direction[1].band_data[b].energy_ratio, nblocks );
668 :
669 5594769 : if ( hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] < 7 )
670 : {
671 18857613 : for ( m = 0; m < nblocks; m++ )
672 : {
673 14755029 : hQMetaData->q_direction[1].band_data[b].azimuth[m] += hQMetaData->q_direction[0].band_data[b].azimuth[m] - 180;
674 14755029 : if ( hQMetaData->q_direction[1].band_data[b].azimuth[m] >= 180 )
675 : {
676 0 : hQMetaData->q_direction[1].band_data[b].azimuth[m] -= 360;
677 : }
678 14755029 : if ( hQMetaData->q_direction[1].band_data[b].azimuth[m] < -180 )
679 : {
680 3185829 : hQMetaData->q_direction[1].band_data[b].azimuth[m] += 360;
681 : }
682 : }
683 : }
684 :
685 5594769 : if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
686 : {
687 1384332 : mvc2c( hQMetaData->q_direction[1].coherence_band_data[d].spread_coherence, hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence, nblocks );
688 : }
689 5594769 : d--;
690 : }
691 : else
692 : {
693 2895450 : set_f( hQMetaData->q_direction[1].band_data[b].azimuth, 0.0f, nblocks );
694 2895450 : set_f( hQMetaData->q_direction[1].band_data[b].elevation, 0.0f, nblocks );
695 2895450 : set_f( hQMetaData->q_direction[1].band_data[b].energy_ratio, 0.0f, nblocks );
696 :
697 2895450 : if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
698 : {
699 2895450 : set_c( (int8_t *) hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence, 0, nblocks );
700 : }
701 : }
702 : }
703 :
704 : /* Scale energy ratios that sum to over one */
705 785370 : if ( !hodirac_flag )
706 : {
707 4682385 : for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
708 : {
709 : float ratioSum;
710 :
711 4279782 : ratioSum = hQMetaData->q_direction[0].band_data[b].energy_ratio[0] + hQMetaData->q_direction[1].band_data[b].energy_ratio[0];
712 :
713 4279782 : if ( ratioSum > 1.0f )
714 : {
715 0 : set_f( hQMetaData->q_direction[0].band_data[b].energy_ratio, hQMetaData->q_direction[0].band_data[b].energy_ratio[0] / ratioSum, nblocks );
716 0 : set_f( hQMetaData->q_direction[1].band_data[b].energy_ratio, hQMetaData->q_direction[1].band_data[b].energy_ratio[0] / ratioSum, nblocks );
717 : }
718 : }
719 : }
720 : }
721 :
722 : /* Store status information for renderer use */
723 8685786 : hQMetaData->ec_flag = ec_flag;
724 :
725 8685786 : hQMetaData->dir_comp_ratio = (float) bits_dir_used / (float) bits_dir_target;
726 :
727 8685786 : if ( hQMetaData->dir_comp_ratio > 1.0f )
728 : {
729 2794296 : hQMetaData->dir_comp_ratio = 1.0f;
730 : }
731 :
732 8685786 : return ( start_index_0 - *index );
733 : }
734 :
735 :
736 : /*-----------------------------------------------------------------------*
737 : * ivas_qmetadata_dec_decode_hr_384_512()
738 : *
739 : * Main function for decoding Spatial Metadata at HRs
740 : *-----------------------------------------------------------------------*/
741 :
742 : /*! r: number of bits read */
743 184644 : int16_t ivas_qmetadata_dec_decode_hr_384_512(
744 : IVAS_QMETADATA_HANDLE hQMetaData, /* i/o: hQMetaData handle */
745 : uint16_t *bitstream, /* i : bitstream */
746 : int16_t *index, /* i/o: bitstream position */
747 : const SPHERICAL_GRID_DATA *sph_grid16, /* i : spherical grid for deindexing */
748 : const int16_t bits_sph_idx,
749 : const int16_t bits_sp_coh,
750 : const uint8_t ncoding_bands_config )
751 : {
752 : int16_t d, b, m;
753 : #ifdef DEBUG_MODE_QMETADATA
754 : int16_t bits_diff_sum;
755 : #endif
756 : int16_t nbands, start_band;
757 : IVAS_QDIRECTION *q_direction;
758 : int16_t start_index_0;
759 : #ifdef DEBUG_MODE_QMETADATA
760 : int16_t bits_no_dirs_coh, bits_sur_coherence;
761 : #endif
762 : uint16_t all_coherence_zero;
763 : int16_t p[MASA_MAXIMUM_CODING_SUBBANDS], dif_p[MASA_MAXIMUM_CODING_SUBBANDS];
764 : int16_t codedBands, sf_nbands0, sf_nbands1;
765 184644 : sf_nbands1 = 1;
766 :
767 : #ifdef DEBUG_MODE_QMETADATA
768 : static FILE *pF = NULL;
769 : static FILE *pF_azi = NULL;
770 : static FILE *pF_ele = NULL;
771 : static FILE *pF_ratio = NULL;
772 : static FILE *pF_spcoh = NULL;
773 : static FILE *pF_surcoh = NULL;
774 :
775 : if ( pF == NULL )
776 : pF = fopen( "./res/qmetadata_dec.txt", "w" );
777 : if ( pF_azi == NULL )
778 : pF_azi = fopen( "./res/qmetadata_azi_dec.txt", "w" );
779 : if ( pF_ele == NULL )
780 : pF_ele = fopen( "./res/qmetadata_ele_dec.txt", "w" );
781 : if ( pF_ratio == NULL )
782 : pF_ratio = fopen( "./res/qmetadata_ratio_dec.txt", "w" );
783 : if ( pF_spcoh == NULL )
784 : pF_spcoh = fopen( "./res/qmetadata_spcoh_dec.txt", "w" );
785 : if ( pF_surcoh == NULL )
786 : pF_surcoh = fopen( "./res/qmetadata_surcoh_dec.txt", "w" );
787 : #endif
788 :
789 184644 : start_index_0 = *index;
790 : /* read number of higher inactive/not encoded bands */
791 184644 : if ( bitstream[( *index )--] )
792 : {
793 38445 : codedBands = MASA_MAXIMUM_CODING_SUBBANDS - ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 1 ) - 1;
794 : }
795 : else
796 : {
797 146199 : codedBands = MASA_MAXIMUM_CODING_SUBBANDS;
798 : }
799 499602 : for ( b = codedBands; b < ncoding_bands_config; b++ )
800 : {
801 1574790 : for ( m = 0; m < MAX_PARAM_SPATIAL_SUBFRAMES; m++ )
802 : {
803 1259832 : hQMetaData->q_direction[0].band_data[b].azimuth[m] = 0.0f;
804 1259832 : hQMetaData->q_direction[0].band_data[b].elevation[m] = 0.0f;
805 1259832 : hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = 0.0f;
806 :
807 1259832 : if ( hQMetaData->coherence_flag && hQMetaData->q_direction[0].coherence_band_data != NULL )
808 : {
809 1259832 : hQMetaData->q_direction[0].coherence_band_data[b].spread_coherence[m] = 0u;
810 : }
811 :
812 1259832 : if ( hQMetaData->no_directions == 2 )
813 : {
814 445188 : hQMetaData->q_direction[1].band_data[b].azimuth[m] = 0.0f;
815 445188 : hQMetaData->q_direction[1].band_data[b].elevation[m] = 0.0f;
816 445188 : hQMetaData->q_direction[1].band_data[b].energy_ratio[m] = 0.0f;
817 :
818 445188 : if ( hQMetaData->coherence_flag && hQMetaData->q_direction[1].coherence_band_data != NULL )
819 : {
820 445188 : hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence[m] = 0u;
821 : }
822 : }
823 :
824 1259832 : if ( hQMetaData->coherence_flag && hQMetaData->surcoh_band_data != NULL )
825 : {
826 1259832 : hQMetaData->surcoh_band_data[b].surround_coherence[m] = 0u;
827 : }
828 : }
829 :
830 314958 : if ( hQMetaData->no_directions == 2 )
831 : {
832 111297 : hQMetaData->twoDirBands[b] = 0;
833 : }
834 : }
835 184644 : sf_nbands0 = hQMetaData->q_direction[0].cfg.nbands;
836 :
837 184644 : hQMetaData->q_direction[0].cfg.nbands = codedBands;
838 :
839 : /*Coherence flag decoding*/
840 : #ifdef DEBUG_MODE_QMETADATA
841 : bits_no_dirs_coh = 0;
842 : #endif
843 184644 : all_coherence_zero = 1;
844 184644 : if ( hQMetaData->coherence_flag )
845 : {
846 : /* read if coherence is zero */
847 184644 : all_coherence_zero = bitstream[( *index )--];
848 : #ifdef DEBUG_MODE_QMETADATA
849 : bits_no_dirs_coh += 1;
850 : #endif
851 : }
852 :
853 184644 : hQMetaData->all_coherence_zero = (uint8_t) all_coherence_zero;
854 :
855 184644 : if ( hQMetaData->no_directions == 2 )
856 : {
857 55551 : set_c( (int8_t *) hQMetaData->twoDirBands, 1, hQMetaData->q_direction[0].cfg.nbands );
858 : }
859 :
860 184644 : if ( bits_sph_idx == 11 && hQMetaData->no_directions == 2 )
861 : {
862 : /* Read which bands have 2 directions */
863 34710 : hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands;
864 34710 : sf_nbands1 = hQMetaData->q_direction[1].cfg.nbands;
865 34710 : if ( hQMetaData->q_direction[1].cfg.nbands > codedBands )
866 : {
867 4284 : hQMetaData->q_direction[1].cfg.nbands = codedBands;
868 : }
869 34710 : set_c( (int8_t *) hQMetaData->twoDirBands, 0, hQMetaData->q_direction[0].cfg.nbands );
870 34710 : d = *index;
871 34710 : dif_p[0] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
872 34710 : p[0] = dif_p[0];
873 34710 : hQMetaData->twoDirBands[p[0]] = 1;
874 327504 : for ( b = 1; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
875 : {
876 292794 : dif_p[b] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
877 292794 : p[b] = p[b - 1] + dif_p[b] + 1;
878 292794 : hQMetaData->twoDirBands[p[b]] = 1;
879 : }
880 : #ifdef DEBUG_MODE_QMETADATA
881 : bits_no_dirs_coh += ( d - *index );
882 : #endif
883 : }
884 :
885 184644 : if ( bits_sph_idx == 16 && hQMetaData->no_directions == 2 )
886 : {
887 20841 : sf_nbands1 = hQMetaData->q_direction[1].cfg.nbands;
888 20841 : if ( hQMetaData->q_direction[1].cfg.nbands > codedBands )
889 : {
890 1617 : hQMetaData->q_direction[1].cfg.nbands = codedBands;
891 : }
892 : }
893 : #ifdef DEBUG_MODE_QMETADATA
894 : bits_diff_sum =
895 : #endif
896 184644 : ivas_qmetadata_entropy_decode_diffuseness_hr_512( bitstream, index, &( hQMetaData->q_direction[0] ) );
897 :
898 184644 : if ( hQMetaData->no_directions == 2 )
899 : {
900 : #ifdef DEBUG_MODE_QMETADATA
901 : bits_diff_sum +=
902 : #endif
903 55551 : ivas_qmetadata_entropy_decode_diffuseness_hr_512( bitstream, index, &( hQMetaData->q_direction[1] ) );
904 : }
905 :
906 :
907 4298445 : for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
908 : {
909 16759125 : for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
910 : {
911 12645324 : hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = 1.0f - diffuseness_reconstructions_hr[hQMetaData->q_direction[0].band_data[b].energy_ratio_index[m]];
912 : }
913 : }
914 184644 : if ( hQMetaData->no_directions == 2 )
915 : {
916 : float ratioSum;
917 55551 : if ( bits_sph_idx == 16 )
918 : {
919 499050 : for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
920 : {
921 1863375 : for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
922 : {
923 1385166 : hQMetaData->q_direction[1].band_data[b].energy_ratio[m] = 1.0f - diffuseness_reconstructions_hr[hQMetaData->q_direction[1].band_data[b].energy_ratio_index[m]];
924 :
925 : /* Scale energy ratios that sum to over one */
926 1385166 : ratioSum = hQMetaData->q_direction[0].band_data[b].energy_ratio[m] + hQMetaData->q_direction[1].band_data[b].energy_ratio[m];
927 :
928 1385166 : if ( ratioSum > 1.0f )
929 : {
930 324702 : hQMetaData->q_direction[0].band_data[b].energy_ratio[m] /= ratioSum;
931 324702 : hQMetaData->q_direction[1].band_data[b].energy_ratio[m] /= ratioSum;
932 : }
933 : }
934 : }
935 : }
936 : else
937 : {
938 : int16_t pos_2dir_band[MASA_MAXIMUM_CODING_SUBBANDS];
939 34710 : d = 0;
940 777234 : for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
941 : {
942 742524 : if ( hQMetaData->twoDirBands[b] == 1 )
943 : {
944 327504 : pos_2dir_band[d] = b;
945 327504 : d++;
946 : }
947 : else
948 : {
949 415020 : pos_2dir_band[d] = 0;
950 : }
951 : }
952 362214 : for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
953 : {
954 1238604 : for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
955 : {
956 911100 : hQMetaData->q_direction[1].band_data[b].energy_ratio[m] = 1.0f - diffuseness_reconstructions_hr[hQMetaData->q_direction[1].band_data[b].energy_ratio_index[m]];
957 :
958 911100 : ratioSum = hQMetaData->q_direction[0].band_data[pos_2dir_band[b]].energy_ratio[m] + hQMetaData->q_direction[1].band_data[b].energy_ratio[m];
959 :
960 911100 : if ( ratioSum > 1.0f )
961 : {
962 89883 : hQMetaData->q_direction[0].band_data[pos_2dir_band[b]].energy_ratio[m] /= ratioSum;
963 89883 : hQMetaData->q_direction[1].band_data[b].energy_ratio[m] /= ratioSum;
964 : }
965 : }
966 : }
967 : }
968 : }
969 :
970 184644 : if ( hQMetaData->no_directions == 2 )
971 : {
972 861264 : for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
973 : {
974 3101979 : for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
975 : {
976 2296266 : hQMetaData->q_direction[1].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[1].band_data[b].energy_ratio_index[m];
977 2296266 : hQMetaData->q_direction[1].band_data[b].bits_sph_idx[m] = bits_sph_idx;
978 : }
979 : }
980 : }
981 :
982 4298445 : for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
983 : {
984 16759125 : for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
985 : {
986 12645324 : hQMetaData->q_direction[0].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
987 12645324 : hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m] = bits_sph_idx;
988 : }
989 : }
990 :
991 184644 : if ( all_coherence_zero == 0 )
992 : {
993 : #ifdef DEBUG_MODE_QMETADATA
994 : bits_sur_coherence =
995 : #endif
996 159606 : read_surround_coherence_hr( bitstream, index, hQMetaData );
997 : }
998 : else
999 : {
1000 : #ifdef DEBUG_MODE_QMETADATA
1001 : bits_sur_coherence = 0;
1002 : #endif
1003 : /*Surround coherence*/
1004 554496 : for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
1005 : {
1006 529458 : if ( hQMetaData->surcoh_band_data != NULL )
1007 : {
1008 529458 : set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
1009 : }
1010 : }
1011 : }
1012 : #ifdef DEBUG_MODE_QMETADATA
1013 : bits_no_dirs_coh += bits_sur_coherence;
1014 : #endif
1015 :
1016 424839 : for ( d = 0; d < hQMetaData->no_directions; d++ )
1017 : {
1018 240195 : q_direction = &hQMetaData->q_direction[d];
1019 240195 : nbands = q_direction->cfg.nbands;
1020 240195 : start_band = q_direction->cfg.start_band;
1021 :
1022 : /* Read coherence, if any */
1023 240195 : if ( all_coherence_zero == 0 )
1024 : {
1025 208473 : read_coherence_data_hr_512( bitstream, index, hQMetaData, d, bits_sp_coh );
1026 : }
1027 : else
1028 : {
1029 : /*Surround coherence*/
1030 719148 : for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
1031 : {
1032 687426 : if ( hQMetaData->surcoh_band_data != NULL )
1033 : {
1034 687426 : set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
1035 : }
1036 :
1037 687426 : if ( hQMetaData->q_direction[d].coherence_band_data != NULL )
1038 : {
1039 687426 : set_c( (int8_t *) hQMetaData->q_direction[d].coherence_band_data[b].spread_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
1040 : }
1041 : }
1042 : }
1043 :
1044 : /* Decode quantized directions frame-wise */
1045 240195 : ivas_qmetadata_raw_decode_dir_512( q_direction, bitstream, index, nbands, start_band, sph_grid16 );
1046 :
1047 : #ifdef DEBUG_MODE_QMETADATA
1048 : fprintf( pF, "frame %d: diff %d surcoh %d ", frame, bits_diff_sum, bits_sur_coherence );
1049 : fprintf( pF, "dir %d\n", start_index_0 - *index );
1050 : fprintf( pF_azi, "frame %d/dir/ec %d: ", frame, d );
1051 : fprintf( pF_ele, "frame %d/dir/ec %d: ", frame, d );
1052 : fprintf( pF_spcoh, "frame %d/dir %d: ", frame, d );
1053 : fprintf( pF_ratio, "frame %d/dir %d: ", frame, d );
1054 :
1055 : for ( b = start_band; b < nbands; b++ )
1056 : {
1057 : for ( m = 0; m < q_direction->cfg.nblocks; m++ )
1058 : {
1059 :
1060 : fprintf( pF_ratio, " %1.3f ", q_direction->band_data[b].energy_ratio[m] );
1061 : fprintf( pF_azi, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[b].azimuth[m] ) / 100.f );
1062 : fprintf( pF_ele, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[b].elevation[m] ) / 100.f );
1063 : if ( q_direction->coherence_band_data != NULL )
1064 : {
1065 : fprintf( pF_spcoh, " %d ", q_direction->coherence_band_data[b].spread_coherence[m] );
1066 : }
1067 : }
1068 : }
1069 : fprintf( pF_ratio, "\n" );
1070 : fprintf( pF_azi, "\n" );
1071 : fprintf( pF_ele, "\n" );
1072 : fprintf( pF_spcoh, "\n" );
1073 : #endif
1074 : }
1075 :
1076 184644 : if ( hQMetaData->no_directions == 2 )
1077 : {
1078 : /* move 2 dir data to its correct subband */
1079 55551 : if ( bits_sph_idx == 11 )
1080 : {
1081 : int16_t nblocks;
1082 34710 : d = hQMetaData->q_direction[1].cfg.nbands - 1;
1083 34710 : nblocks = hQMetaData->q_direction[0].cfg.nblocks;
1084 :
1085 777234 : for ( b = hQMetaData->q_direction[0].cfg.nbands - 1; b >= 0; b-- )
1086 : {
1087 742524 : if ( hQMetaData->twoDirBands[b] == 1 )
1088 : {
1089 327504 : mvr2r( hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].band_data[b].azimuth, nblocks );
1090 327504 : mvr2r( hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].band_data[b].elevation, nblocks );
1091 327504 : mvr2r( hQMetaData->q_direction[1].band_data[d].energy_ratio, hQMetaData->q_direction[1].band_data[b].energy_ratio, nblocks );
1092 :
1093 :
1094 327504 : if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
1095 : {
1096 327504 : mvc2c( hQMetaData->q_direction[1].coherence_band_data[d].spread_coherence, hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence, nblocks );
1097 : }
1098 327504 : d--;
1099 : }
1100 : else
1101 : {
1102 415020 : set_f( hQMetaData->q_direction[1].band_data[b].azimuth, 0.0f, nblocks );
1103 415020 : set_f( hQMetaData->q_direction[1].band_data[b].elevation, 0.0f, nblocks );
1104 415020 : set_f( hQMetaData->q_direction[1].band_data[b].energy_ratio, 0.0f, nblocks );
1105 :
1106 415020 : if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
1107 : {
1108 415020 : set_c( (int8_t *) hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence, 0, nblocks );
1109 : }
1110 : }
1111 : }
1112 : }
1113 :
1114 : /* Scale energy ratios that sum to over one */
1115 1276284 : for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
1116 : {
1117 4802301 : for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
1118 : {
1119 : float ratioSum;
1120 :
1121 3581568 : ratioSum = hQMetaData->q_direction[0].band_data[b].energy_ratio[m] + hQMetaData->q_direction[1].band_data[b].energy_ratio[m];
1122 :
1123 3581568 : if ( ratioSum > 1.0f )
1124 : {
1125 : float ratioSumInv;
1126 0 : ratioSumInv = 1.0f / ratioSum;
1127 0 : hQMetaData->q_direction[0].band_data[b].energy_ratio[m] *= ratioSumInv;
1128 0 : hQMetaData->q_direction[1].band_data[b].energy_ratio[m] *= ratioSumInv;
1129 : }
1130 : }
1131 : }
1132 : }
1133 :
1134 : #ifdef DEBUG_MODE_QMETADATA
1135 : for ( d = 0; d < hQMetaData->no_directions; d++ )
1136 : {
1137 : q_direction = &hQMetaData->q_direction[d];
1138 : nbands = q_direction->cfg.nbands;
1139 : start_band = q_direction->cfg.start_band;
1140 :
1141 :
1142 : if ( d == 0 )
1143 : {
1144 : fprintf( pF_surcoh, "frame %d/dir %d: ", frame, d );
1145 : }
1146 : for ( b = start_band; b < nbands; b++ )
1147 : {
1148 : for ( m = 0; m < q_direction->cfg.nblocks; m++ )
1149 : {
1150 :
1151 :
1152 : if ( d == 0 && hQMetaData->surcoh_band_data != NULL )
1153 : {
1154 : fprintf( pF_surcoh, " %d ", hQMetaData->surcoh_band_data[b].surround_coherence[m] );
1155 : }
1156 : }
1157 : }
1158 :
1159 :
1160 : if ( d == 0 )
1161 : {
1162 : fprintf( pF_surcoh, "\n" );
1163 : }
1164 : }
1165 : #endif
1166 : /* Store status information for renderer use */
1167 184644 : hQMetaData->ec_flag = 0;
1168 :
1169 184644 : hQMetaData->dir_comp_ratio = 1.0f;
1170 :
1171 184644 : if ( hQMetaData->dir_comp_ratio > 1.0f )
1172 : {
1173 0 : hQMetaData->dir_comp_ratio = 1.0f;
1174 : }
1175 :
1176 184644 : hQMetaData->q_direction[0].cfg.nbands = sf_nbands0;
1177 184644 : if ( hQMetaData->no_directions == 2 )
1178 : {
1179 55551 : hQMetaData->q_direction[1].cfg.nbands = sf_nbands1;
1180 : }
1181 :
1182 184644 : return ( start_index_0 - *index );
1183 : }
1184 :
1185 :
1186 : /*-----------------------------------------------------------------------*
1187 : * ivas_qmetadata_dec_sid_decode()
1188 : *
1189 : * Main function for decoding SID for Spatial Metadata
1190 : *-----------------------------------------------------------------------*/
1191 :
1192 : /*! r: number of bits written */
1193 15036 : int16_t ivas_qmetadata_dec_sid_decode(
1194 : IVAS_QMETADATA_HANDLE hQMetaData, /* i/o: q_metadata handle */
1195 : uint16_t *bitstream, /* i : bitstream */
1196 : int16_t *index, /* i/o: bitstream position */
1197 : const int16_t nchan_transport, /* i : number of transport channels */
1198 : int16_t *element_mode, /* o : element mode */
1199 : const int16_t ivas_format /* i : IVAS format */
1200 : )
1201 : {
1202 : int16_t b, m, i;
1203 : uint16_t value;
1204 : uint16_t diffuseness_index[DIRAC_MAX_NBANDS];
1205 : int16_t nbands, nblocks, start_band;
1206 : IVAS_QDIRECTION *q_direction;
1207 : int16_t start_index;
1208 : float avg_elevation, avg_azimuth;
1209 : float avg_direction_vector[3];
1210 : float direction_vector[3];
1211 : int16_t metadata_sid_bits; /* bits allocated to SID for metadata */
1212 : int16_t bits_delta, bits_dir;
1213 : int16_t sba_spar_bitlen;
1214 : #ifdef DEBUG_MODE_QMETADATA
1215 : static FILE *pF = NULL;
1216 : static FILE *pF_azi = NULL;
1217 : static FILE *pF_ele = NULL;
1218 : static FILE *pF_ratio = NULL;
1219 :
1220 : if ( pF == NULL )
1221 : pF = fopen( "./res/qmetadata_sid_dec.txt", "w" );
1222 : if ( pF_azi == NULL )
1223 : pF_azi = fopen( "./res/qmetadata_sid_azi_dec.txt", "w" );
1224 : if ( pF_ele == NULL )
1225 : pF_ele = fopen( "./res/qmetadata_sid_ele_dec.txt", "w" );
1226 : if ( pF_ratio == NULL )
1227 : pF_ratio = fopen( "./res/qmetadata_sid_ratio_dec.txt", "w" );
1228 : #endif
1229 :
1230 15036 : if ( ivas_format == SBA_FORMAT )
1231 : {
1232 12846 : sba_spar_bitlen = ivas_sba_spar_sid_bitlen( nchan_transport );
1233 12846 : 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*/
1234 : }
1235 : else
1236 : {
1237 2190 : metadata_sid_bits = ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS;
1238 : }
1239 :
1240 15036 : start_index = *index;
1241 :
1242 : /* read MASA SID descriptor */
1243 15036 : if ( ivas_format == MASA_FORMAT && nchan_transport == 2 )
1244 : {
1245 801 : b = bitstream[( *index )--];
1246 801 : if ( b )
1247 : {
1248 210 : *element_mode = IVAS_CPE_MDCT;
1249 : }
1250 : else
1251 : {
1252 591 : *element_mode = IVAS_CPE_DFT;
1253 : }
1254 : }
1255 :
1256 : /* Fix configuration for SID */
1257 15036 : q_direction = &hQMetaData->q_direction[0]; /* only 1 direction */
1258 15036 : if ( ivas_format == SBA_FORMAT )
1259 : {
1260 12846 : nbands = DIRAC_DTX_BANDS; /* only 2 bands transmitted */
1261 : }
1262 : else
1263 : {
1264 2190 : nbands = 5; /* only 5 bands transmitted */
1265 : }
1266 :
1267 15036 : nblocks = q_direction->cfg.nblocks; /* only 1 block transmitted but up to 4 blocks re-generated */
1268 15036 : start_band = 0; /* start from band 0 */
1269 :
1270 : /* Read 2D signaling*/
1271 15036 : if ( ivas_format != SBA_FORMAT )
1272 : {
1273 2190 : q_direction->not_in_2D = bitstream[( *index )--];
1274 : }
1275 : else
1276 : {
1277 12846 : q_direction->not_in_2D = 1;
1278 : }
1279 :
1280 15036 : bits_dir = 0;
1281 15036 : if ( ivas_format != SBA_FORMAT )
1282 : {
1283 : /* Decode diffuseness*/
1284 13140 : for ( b = start_band; b < nbands; b++ )
1285 : {
1286 10950 : diffuseness_index[b] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS - 4 ) + 4;
1287 10950 : q_direction->band_data[b].energy_ratio_index[0] = diffuseness_index[b];
1288 10950 : q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[diffuseness_index[b]];
1289 10950 : bits_dir += q_direction->band_data[b].bits_sph_idx[0];
1290 10950 : q_direction->band_data[b].azimuth_m_alphabet[0] = no_phi_masa[q_direction->band_data[b].bits_sph_idx[0] - 1][0];
1291 : }
1292 :
1293 2190 : bits_delta = metadata_sid_bits - ( start_index - *index ) - bits_dir; /* bit_diff is already read */
1294 :
1295 2190 : if ( bits_delta > 0 )
1296 : {
1297 12996 : while ( bits_delta > 0 )
1298 : {
1299 59049 : for ( b = start_band; b < nbands && ( bits_delta > 0 ); b++ )
1300 : {
1301 48243 : if ( q_direction->band_data[b].bits_sph_idx[0] < 11 )
1302 : {
1303 48231 : bits_delta -= 1;
1304 48231 : q_direction->band_data[b].bits_sph_idx[0]++;
1305 : }
1306 : }
1307 : }
1308 :
1309 2190 : if ( q_direction->not_in_2D == 0 )
1310 : {
1311 2970 : for ( b = start_band; b < nbands; b++ )
1312 : {
1313 2475 : q_direction->band_data[b].azimuth_m_alphabet[0] = 1 << ( min( 8, q_direction->band_data[b].bits_sph_idx[0] ) );
1314 : }
1315 : }
1316 : }
1317 : else
1318 : {
1319 0 : while ( bits_delta < 0 )
1320 : {
1321 0 : for ( b = nbands - 1; b >= start_band && ( bits_delta < 0 ); b-- )
1322 : {
1323 0 : if ( q_direction->band_data[b].bits_sph_idx[0] >= 4 )
1324 : {
1325 0 : bits_delta += 1;
1326 0 : q_direction->band_data[b].bits_sph_idx[0]--;
1327 : }
1328 : }
1329 :
1330 0 : if ( q_direction->not_in_2D == 0 )
1331 : {
1332 0 : for ( b = start_band; b < nbands; b++ )
1333 : {
1334 0 : q_direction->band_data[b].azimuth_m_alphabet[0] = 1 << ( min( 8, q_direction->band_data[b].bits_sph_idx[0] ) );
1335 : }
1336 : }
1337 : }
1338 : }
1339 : }
1340 : else
1341 : {
1342 : /* Decode diffuseness*/
1343 38538 : for ( b = start_band; b < nbands; b++ )
1344 : {
1345 25692 : diffuseness_index[b] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS - 4 ) + 4;
1346 25692 : q_direction->band_data[b].energy_ratio_index[0] = diffuseness_index[b];
1347 25692 : q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[diffuseness_index[b]];
1348 25692 : q_direction->band_data[b].azimuth_m_alphabet[0] = no_phi_masa[q_direction->band_data[b].bits_sph_idx[0] - 1][0];
1349 : }
1350 : }
1351 :
1352 51678 : for ( b = start_band; b < nbands; b++ )
1353 : {
1354 36642 : q_direction->band_data[b].energy_ratio[0] = 1.0f - diffuseness_reconstructions[diffuseness_index[b]];
1355 168810 : for ( i = 0; i < nblocks; i++ )
1356 : {
1357 132168 : q_direction->band_data[b].energy_ratio[i] = q_direction->band_data[b].energy_ratio[0];
1358 : }
1359 : }
1360 :
1361 : /* Decoder DOAs*/
1362 15036 : if ( q_direction->not_in_2D > 0 )
1363 : {
1364 48708 : for ( b = start_band; b < nbands; b++ )
1365 : {
1366 34167 : value = 0;
1367 195612 : for ( i = 0; i < q_direction->band_data[b].bits_sph_idx[0]; i++ )
1368 : {
1369 161445 : value = ( value << 1 ) + bitstream[( *index )--];
1370 : }
1371 159225 : for ( i = 0; i < nblocks; i++ )
1372 : {
1373 125058 : q_direction->band_data[b].spherical_index[i] = value;
1374 : }
1375 :
1376 34167 : deindex_spherical_component( q_direction->band_data[b].spherical_index[0], &avg_azimuth, &avg_elevation, &q_direction->band_data[b].azimuth_index[0], &q_direction->band_data[b].elevation_index[0], q_direction->band_data[b].bits_sph_idx[0], q_direction->cfg.mc_ls_setup );
1377 :
1378 34167 : ivas_qmetadata_azimuth_elevation_to_direction_vector( avg_azimuth, avg_elevation, avg_direction_vector );
1379 34167 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[nblocks - 1], q_direction->band_data[b].elevation[nblocks - 1], direction_vector );
1380 :
1381 125058 : for ( m = 0; m < nblocks - 1; m++ )
1382 : {
1383 90891 : v_add( direction_vector, avg_direction_vector, direction_vector, 3 );
1384 90891 : ivas_qmetadata_direction_vector_to_azimuth_elevation( direction_vector, &q_direction->band_data[b].azimuth[m], &q_direction->band_data[b].elevation[m] );
1385 : }
1386 :
1387 34167 : ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &q_direction->band_data[b].azimuth[nblocks - 1], &q_direction->band_data[b].elevation[nblocks - 1] );
1388 : }
1389 : }
1390 : else
1391 : {
1392 2970 : for ( b = start_band; b < nbands; b++ )
1393 : {
1394 2475 : if ( ivas_format == SBA_FORMAT )
1395 : {
1396 0 : q_direction->band_data[b].azimuth_m_alphabet[0] = 1 << ( min( 5, q_direction->band_data[b].bits_sph_idx[0] ) );
1397 : }
1398 :
1399 2475 : q_direction->band_data[b].azimuth_index[0] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, q_direction->band_data[b].azimuth_m_alphabet[0] );
1400 2475 : q_direction->band_data[b].azimuth_index[0] = ivas_qmetadata_dereorder_generic( q_direction->band_data[b].azimuth_index[0] ) + ( q_direction->band_data[b].azimuth_m_alphabet[0] >> 1 );
1401 2475 : avg_azimuth = 360.0f / (float) ( q_direction->band_data[b].azimuth_m_alphabet[0] ) * q_direction->band_data[b].azimuth_index[0] - 180;
1402 :
1403 2475 : avg_elevation = 0.f;
1404 :
1405 2475 : ivas_qmetadata_azimuth_elevation_to_direction_vector( avg_azimuth, avg_elevation, avg_direction_vector );
1406 2475 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[nblocks - 1], q_direction->band_data[b].elevation[nblocks - 1], direction_vector );
1407 :
1408 7110 : for ( m = 0; m < nblocks - 1; m++ )
1409 : {
1410 4635 : v_add( direction_vector, avg_direction_vector, direction_vector, 3 );
1411 4635 : ivas_qmetadata_direction_vector_to_azimuth_elevation( direction_vector, &q_direction->band_data[b].azimuth[m], &q_direction->band_data[b].elevation[m] );
1412 : }
1413 :
1414 2475 : ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &q_direction->band_data[b].azimuth[nblocks - 1], &q_direction->band_data[b].elevation[nblocks - 1] );
1415 :
1416 9585 : for ( i = 0; i < nblocks; i++ )
1417 : {
1418 7110 : q_direction->band_data[b].spherical_index[i] = q_direction->band_data[b].azimuth_index[0];
1419 : }
1420 : }
1421 : }
1422 :
1423 :
1424 : /*Read filling bits*/
1425 53115 : while ( start_index - *index < metadata_sid_bits )
1426 : {
1427 38079 : b = bitstream[( *index )--];
1428 : }
1429 :
1430 : #ifdef DEBUG_MODE_QMETADATA
1431 : fprintf( pF, "frame %d: all %d ", frame, start_index - *index );
1432 :
1433 : fprintf( pF_azi, "frame %d SID: ", frame );
1434 : fprintf( pF_ele, "frame %d SID: ", frame );
1435 : fprintf( pF_ratio, "frame %d SID: ", frame );
1436 :
1437 : for ( b = start_band; b < nbands; b++ )
1438 : {
1439 : for ( m = 0; m < nblocks; m++ )
1440 : {
1441 : fprintf( pF_azi, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[b].azimuth[m] ) / 100.f );
1442 : fprintf( pF_ele, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[b].elevation[m] ) / 100.f );
1443 : fprintf( pF_ratio, " %1.3f ", q_direction->band_data[b].energy_ratio[m] );
1444 : }
1445 : }
1446 : fprintf( pF_azi, "\n" );
1447 : fprintf( pF_ele, "\n" );
1448 : fprintf( pF_ratio, "\n" );
1449 :
1450 : #endif
1451 15036 : return ( start_index - *index );
1452 : }
1453 :
1454 :
1455 : /*-----------------------------------------------------------------------*
1456 : * Local function definitions for diffuseness/energy ratios
1457 : *-----------------------------------------------------------------------*/
1458 :
1459 10979820 : static int16_t ivas_diffuseness_huff_ec_decode(
1460 : const uint16_t *bitstream,
1461 : int16_t *index,
1462 : int16_t av )
1463 : {
1464 : int16_t val;
1465 :
1466 10979820 : val = 0;
1467 19796631 : while ( val <= DIFF_EC_HUFF_GR0_LIMIT )
1468 : {
1469 19783530 : if ( bitstream[( *index )--] == 1 )
1470 : {
1471 8816811 : val++;
1472 : }
1473 : else
1474 : {
1475 10966719 : break;
1476 : }
1477 : }
1478 :
1479 10979820 : if ( val == DIFF_EC_HUFF_GR0_LIMIT + 1 )
1480 : {
1481 13101 : val += 2 * bitstream[( *index )];
1482 13101 : ( *index )--;
1483 13101 : val += bitstream[( *index )];
1484 13101 : ( *index )--;
1485 : }
1486 :
1487 10979820 : if ( val % 2 == 0 )
1488 : {
1489 8090604 : return -( val / 2 ) + av;
1490 : }
1491 : else
1492 : {
1493 2889216 : return ( val + 1 ) / 2 + av;
1494 : }
1495 : }
1496 :
1497 :
1498 : /*-------------------------------------------------------------------*
1499 : * ivas_qmetadata_entropy_decode_diffuseness()
1500 : *
1501 : *
1502 : *-------------------------------------------------------------------*/
1503 :
1504 8685786 : static int16_t ivas_qmetadata_entropy_decode_diffuseness(
1505 : uint16_t *bitstream, /* i : bitstream */
1506 : int16_t *index,
1507 : IVAS_QDIRECTION *q_direction,
1508 : uint16_t *diffuseness_index_max_ec_frame )
1509 : {
1510 : int16_t b;
1511 : uint16_t dif_min;
1512 : int16_t index_start;
1513 : int16_t nbands;
1514 : int16_t start_band;
1515 :
1516 8685786 : index_start = *index;
1517 8685786 : nbands = q_direction->cfg.nbands;
1518 8685786 : start_band = q_direction->cfg.start_band;
1519 :
1520 : /* diffuseness decoding */
1521 : /* Handle one band as special case*/
1522 8685786 : if ( nbands == 1 )
1523 : {
1524 132171 : q_direction->band_data[0].energy_ratio_index[0] = 0;
1525 528684 : for ( b = 0; b < MASA_BITS_ER; b++ )
1526 : {
1527 396513 : q_direction->band_data[0].energy_ratio_index[0] = ( q_direction->band_data[0].energy_ratio_index[0] << 1 ) + bitstream[( *index )--];
1528 : }
1529 132171 : *diffuseness_index_max_ec_frame = 5;
1530 :
1531 132171 : return MASA_BITS_ER;
1532 : }
1533 :
1534 8553615 : if ( bitstream[( *index )--] == 0 ) /* dif_use_raw_coding */
1535 : {
1536 : /* Decode with similarity strategy with low band count. On higher band counts, decode with Huffman-coding strategy. */
1537 4622097 : if ( nbands < DIFF_EC_HUFF_BAND_LIMIT )
1538 : {
1539 3810003 : if ( bitstream[( *index )--] != 0 ) /* dif_have_unique_value */
1540 : {
1541 2001054 : dif_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS ); /* dif_unique_value */
1542 :
1543 8395395 : for ( b = start_band; b < nbands; b++ )
1544 : {
1545 6394341 : q_direction->band_data[b].energy_ratio_index[0] = dif_min;
1546 : }
1547 : }
1548 : else /* all diffuseness values are dif_min_value or dif_min_value + 1 */
1549 : {
1550 1808949 : dif_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS - 1 ); /* dif_min_value */
1551 :
1552 7790847 : for ( b = start_band; b < nbands; b++ )
1553 : {
1554 5981898 : q_direction->band_data[b].energy_ratio_index[0] = dif_min + bitstream[( *index )--]; /* dif_bit_offset_values */
1555 : }
1556 : }
1557 : }
1558 : else
1559 : {
1560 : int16_t av;
1561 :
1562 : /* read average on 3 bits*/
1563 812094 : av = 0;
1564 3248376 : for ( b = 0; b < MASA_BITS_ER; b++ )
1565 : {
1566 2436282 : av += bitstream[( *index )--] * ( 1 << ( MASA_BITS_ER - 1 - b ) );
1567 : }
1568 :
1569 812094 : dif_min = DIRAC_DIFFUSE_LEVELS;
1570 : /* read average removed data (average is added inside)*/
1571 11791914 : for ( b = start_band; b < nbands; b++ )
1572 : {
1573 10979820 : q_direction->band_data[b].energy_ratio_index[0] = ivas_diffuseness_huff_ec_decode( bitstream, index, av );
1574 10979820 : dif_min = min( dif_min, q_direction->band_data[b].energy_ratio_index[0] );
1575 : }
1576 : }
1577 : }
1578 : else /* different values for diffuseness */
1579 : {
1580 3931518 : dif_min = DIRAC_DIFFUSE_LEVELS;
1581 :
1582 22161480 : for ( b = start_band; b < nbands; b++ )
1583 : {
1584 18229962 : q_direction->band_data[b].energy_ratio_index[0] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS );
1585 18229962 : dif_min = min( dif_min, q_direction->band_data[b].energy_ratio_index[0] );
1586 : }
1587 : }
1588 :
1589 8553615 : *diffuseness_index_max_ec_frame = 5;
1590 : /* adaptively select the diffuseness_index_max_ec threshold */
1591 8553615 : if ( dif_min > 5 )
1592 : {
1593 2907624 : *diffuseness_index_max_ec_frame = DIRAC_DIFFUSE_LEVELS - 1;
1594 : }
1595 :
1596 8553615 : return ( index_start - *index );
1597 : }
1598 :
1599 :
1600 : /*-------------------------------------------------------------------*
1601 : * ivas_qmetadata_entropy_decode_diffuseness_hr_512()
1602 : *
1603 : *
1604 : *-------------------------------------------------------------------*/
1605 :
1606 240195 : static int16_t ivas_qmetadata_entropy_decode_diffuseness_hr_512(
1607 : uint16_t *bitstream, /* i : bitstream */
1608 : int16_t *index,
1609 : IVAS_QDIRECTION *q_direction )
1610 : {
1611 : int16_t b, k;
1612 :
1613 : int16_t index_start;
1614 : int16_t nbands, nblocks;
1615 : int16_t start_band;
1616 :
1617 240195 : index_start = *index;
1618 240195 : nbands = q_direction->cfg.nbands;
1619 240195 : nblocks = q_direction->cfg.nblocks;
1620 240195 : start_band = q_direction->cfg.start_band;
1621 :
1622 : /* diffuseness decoding */
1623 : /* Handle one band as special case*/
1624 240195 : if ( nbands == 1 )
1625 : {
1626 0 : q_direction->band_data[0].energy_ratio_index[0] = 0;
1627 0 : for ( b = 0; b < MASA_BITS_ER_HR; b++ )
1628 : {
1629 0 : q_direction->band_data[0].energy_ratio_index[0] = ( q_direction->band_data[0].energy_ratio_index[0] << 1 ) + bitstream[( *index )--];
1630 : }
1631 :
1632 0 : return MASA_BITS_ER_HR;
1633 : }
1634 :
1635 5159709 : for ( b = start_band; b < nbands; b++ )
1636 : {
1637 19861104 : for ( k = 0; k < nblocks; k++ )
1638 : {
1639 14941590 : q_direction->band_data[b].energy_ratio_index[k] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, HR_MASA_ER_LEVELS );
1640 : }
1641 : }
1642 :
1643 240195 : return ( index_start - *index );
1644 : }
1645 :
1646 :
1647 : /*-------------------------------------------------------------------*
1648 : * ivas_qmetadata_entropy_decode_df_ratio()
1649 : *
1650 : *
1651 : *-------------------------------------------------------------------*/
1652 :
1653 785370 : static int16_t ivas_qmetadata_entropy_decode_df_ratio(
1654 : uint16_t *bitstream,
1655 : int16_t *index,
1656 : IVAS_QDIRECTION *q_direction,
1657 : int16_t *dfRatio_bits )
1658 : {
1659 : int16_t b;
1660 : int16_t bits_raw;
1661 : int16_t max_dfRatio_bits;
1662 : uint16_t ratio_min;
1663 : int16_t index_start;
1664 : int16_t nbands;
1665 : int16_t start_band;
1666 : int16_t ec_mode;
1667 : int16_t dec_mode;
1668 : int16_t max_alphabet_size;
1669 :
1670 785370 : index_start = *index;
1671 785370 : nbands = q_direction->cfg.nbands;
1672 785370 : start_band = q_direction->cfg.start_band;
1673 :
1674 : /* Handle one band as special case*/
1675 785370 : if ( nbands == 1 )
1676 : {
1677 120357 : q_direction->band_data[0].energy_ratio_index[0] = 0;
1678 351480 : for ( b = 0; b < dfRatio_bits[0]; b++ )
1679 : {
1680 231123 : q_direction->band_data[0].energy_ratio_index[0] = ( q_direction->band_data[0].energy_ratio_index[0] << 1 ) + bitstream[( *index )--];
1681 : }
1682 120357 : return dfRatio_bits[0];
1683 : }
1684 :
1685 : /* Calculate raw coding bits and decide what modes are possible */
1686 665013 : bits_raw = 0;
1687 665013 : max_dfRatio_bits = 0;
1688 6139425 : for ( b = start_band; b < nbands; b++ )
1689 : {
1690 5474412 : bits_raw += dfRatio_bits[b];
1691 5474412 : max_dfRatio_bits = max( max_dfRatio_bits, dfRatio_bits[b] );
1692 : }
1693 :
1694 : /* Decide what modes are possible */
1695 665013 : if ( bits_raw >= max_dfRatio_bits + 2 + nbands )
1696 : {
1697 410172 : ec_mode = 2;
1698 : }
1699 254841 : else if ( bits_raw >= max_dfRatio_bits + 1 )
1700 : {
1701 254841 : ec_mode = 1;
1702 : }
1703 : else
1704 : {
1705 0 : ec_mode = 0;
1706 : }
1707 665013 : max_alphabet_size = 1 << max_dfRatio_bits;
1708 :
1709 665013 : dec_mode = 2; /* Default to raw decoding */
1710 665013 : if ( ec_mode == 1 )
1711 : {
1712 254841 : if ( bitstream[( *index )--] == 0 )
1713 : {
1714 68469 : dec_mode = 1; /* Switch to one value EC coding */
1715 : }
1716 : }
1717 410172 : else if ( ec_mode == 2 )
1718 : {
1719 410172 : if ( bitstream[( *index )--] == 0 )
1720 : {
1721 114624 : if ( bitstream[( *index )--] == 0 )
1722 : {
1723 31782 : dec_mode = 1; /* Switch to one value EC coding */
1724 : }
1725 : else
1726 : {
1727 82842 : dec_mode = 0; /* Use one-bit diff bandwise mode */
1728 : }
1729 : }
1730 : }
1731 :
1732 665013 : if ( dec_mode == 2 ) /* Raw decoding */
1733 : {
1734 4987329 : for ( b = start_band; b < nbands; b++ )
1735 : {
1736 4505409 : q_direction->band_data[b].energy_ratio_index[0] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, 1 << dfRatio_bits[b] );
1737 : }
1738 : }
1739 183093 : else if ( dec_mode == 1 ) /* One value decoding */
1740 : {
1741 100251 : ratio_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, max_alphabet_size ); /* dif_unique_value */
1742 :
1743 492585 : for ( b = start_band; b < nbands; b++ )
1744 : {
1745 392334 : q_direction->band_data[b].energy_ratio_index[0] = ratio_min;
1746 : }
1747 : }
1748 : else /* Bandwise 1-bit diff decoding */
1749 : {
1750 82842 : ratio_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, max_alphabet_size - 1 ); /* dif_min_value */
1751 :
1752 659511 : for ( b = start_band; b < nbands; b++ )
1753 : {
1754 576669 : q_direction->band_data[b].energy_ratio_index[0] = ratio_min + bitstream[( *index )--]; /* dif_bit_offset_values */
1755 : }
1756 : }
1757 :
1758 665013 : return ( index_start - *index );
1759 : }
1760 :
1761 :
1762 : /*-----------------------------------------------------------------------*
1763 : * Local functions (EC1)
1764 : *-----------------------------------------------------------------------*/
1765 :
1766 : /*-------------------------------------------------------------------------
1767 : * ivas_qmetadata_entropy_decode_dir()
1768 : *
1769 : * Main function for entropy decoding of the directions
1770 : *------------------------------------------------------------------------*/
1771 :
1772 5661120 : static int16_t ivas_qmetadata_entropy_decode_dir(
1773 : IVAS_QDIRECTION *q_direction, /* i/o: quantized direction structure */
1774 : uint16_t *bitstream, /* i : bitstream */
1775 : int16_t *index,
1776 : const uint16_t diffuseness_index_max_ec_frame,
1777 : const int16_t nbands,
1778 : const int16_t start_band,
1779 : const int16_t hrmasa_flag /* i : flag indicating high-rate MASA MD coding*/
1780 : )
1781 : {
1782 : int16_t b, m;
1783 : int16_t diff_idx;
1784 : int16_t diff_idx_min;
1785 : int16_t nblocks;
1786 : int16_t index_start;
1787 :
1788 : uint16_t gr_param_elev, gr_param_azith;
1789 : int16_t bands_entropic[MASA_MAXIMUM_CODING_SUBBANDS];
1790 : int16_t elev_alph[MASA_MAXIMUM_CODING_SUBBANDS];
1791 : int16_t azith_alph[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
1792 :
1793 : int16_t avg_elevation_alphabet, avg_azimuth_alphabet;
1794 : int16_t avg_elevation_idx, avg_azimuth_index;
1795 : int16_t avg_elevation_index_projected, avg_azimuth_index_projected;
1796 : float direction_vector[3], avg_direction_vector[3], avg_azimuth, avg_elevation;
1797 : int16_t use_adapt_avg, idx;
1798 :
1799 5661120 : index_start = *index;
1800 5661120 : nblocks = q_direction->cfg.nblocks;
1801 :
1802 5661120 : diff_idx_min = DIRAC_DIFFUSE_LEVELS;
1803 :
1804 : /*Raw coding for high diffuseness*/
1805 36249240 : for ( b = start_band; b < nbands; b++ )
1806 : {
1807 30588120 : if ( hrmasa_flag )
1808 : {
1809 0 : diff_idx = 0;
1810 : }
1811 : else
1812 : {
1813 30588120 : diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
1814 : }
1815 :
1816 30588120 : diff_idx_min = min( diff_idx_min, diff_idx );
1817 30588120 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
1818 : {
1819 1134039 : elev_alph[b] = no_theta_masa[bits_direction_masa[diff_idx] - 3];
1820 : }
1821 : else
1822 : {
1823 29454081 : elev_alph[b] = no_theta_masa[bits_direction_masa[diff_idx] - 3] * 2 - 1;
1824 : }
1825 :
1826 30588120 : if ( q_direction->band_data[b].energy_ratio_index_mod[0] > diffuseness_index_max_ec_frame )
1827 : {
1828 7692153 : bands_entropic[b] = 0;
1829 :
1830 7692153 : if ( q_direction->not_in_2D > 0 )
1831 : {
1832 6815295 : decode_fixed_rate( q_direction, bitstream, index, b, nblocks );
1833 : }
1834 : else
1835 : {
1836 : /* in 2D */
1837 3552591 : for ( m = 0; m < nblocks; m++ )
1838 : {
1839 2675733 : q_direction->band_data[b].elevation[m] = 0.f;
1840 2675733 : q_direction->band_data[b].elevation_index[m] = 0;
1841 :
1842 2675733 : azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][0];
1843 2675733 : q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, azith_alph[b][m] );
1844 2675733 : q_direction->band_data[b].azimuth[m] = deindex_azimuth( q_direction->band_data[b].azimuth_index[m], q_direction->band_data[b].bits_sph_idx[m], 0, 1, q_direction->cfg.mc_ls_setup );
1845 : }
1846 : }
1847 : }
1848 : else
1849 : {
1850 22895967 : bands_entropic[b] = 1;
1851 : }
1852 : }
1853 :
1854 : /*EC for the low diffuseness*/
1855 :
1856 : /*Elevation only if not 2D */
1857 5661120 : if ( q_direction->not_in_2D > 0 )
1858 : {
1859 4362783 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
1860 : {
1861 123267 : avg_elevation_alphabet = no_theta_masa[bits_direction_masa[diff_idx_min] - 3];
1862 123267 : avg_elevation_idx = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, avg_elevation_alphabet );
1863 : }
1864 : else
1865 : {
1866 4239516 : avg_elevation_alphabet = no_theta_masa[bits_direction_masa[diff_idx_min] - 3] * 2 - 1;
1867 4239516 : avg_elevation_idx = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, avg_elevation_alphabet );
1868 4239516 : avg_elevation_idx = ivas_qmetadata_dereorder_generic( avg_elevation_idx ) + ( avg_elevation_alphabet >> 1 );
1869 : }
1870 :
1871 4362783 : gr_param_elev = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, 4 + 1 );
1872 4362783 : if ( gr_param_elev == 4 ) /* all the elevation distances are zero */
1873 : {
1874 5184372 : for ( b = start_band; b < nbands; b++ )
1875 : {
1876 4120101 : if ( bands_entropic[b] )
1877 : {
1878 : int16_t tmp_index;
1879 2182026 : if ( hrmasa_flag )
1880 : {
1881 0 : diff_idx = 0;
1882 : }
1883 : else
1884 : {
1885 2182026 : diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
1886 : }
1887 :
1888 2182026 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
1889 : {
1890 57378 : avg_elevation_index_projected = ivas_chan_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
1891 : }
1892 : else
1893 : {
1894 2124648 : avg_elevation_index_projected = ivas_dirac_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
1895 :
1896 : /*reorder elevation indexing*/
1897 2124648 : tmp_index = avg_elevation_index_projected - ( elev_alph[b] >> 1 );
1898 2124648 : if ( tmp_index < 0 )
1899 : {
1900 479466 : tmp_index = tmp_index * -2;
1901 : }
1902 1645182 : else if ( tmp_index > 0 )
1903 : {
1904 604503 : tmp_index = tmp_index * 2 - 1;
1905 : }
1906 2124648 : avg_elevation_index_projected = tmp_index;
1907 : }
1908 :
1909 8098053 : for ( m = 0; m < nblocks; m++ )
1910 : {
1911 5916027 : q_direction->band_data[b].elevation_index[m] = avg_elevation_index_projected;
1912 :
1913 : /*deduce aplhabet for azimuth*/
1914 5916027 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
1915 : {
1916 200262 : azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][q_direction->band_data[b].elevation_index[m]];
1917 : }
1918 : else
1919 : {
1920 5715765 : azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][( q_direction->band_data[b].elevation_index[m] + 1 ) >> 1];
1921 : }
1922 :
1923 : /*decode elevation*/
1924 5916027 : q_direction->band_data[b].elevation[m] = deindex_elevation( &q_direction->band_data[b].elevation_index[m], q_direction->band_data[b].bits_sph_idx[m], q_direction->cfg.mc_ls_setup );
1925 : }
1926 : }
1927 : }
1928 : }
1929 : else
1930 : {
1931 22004778 : for ( b = start_band; b < nbands; b++ )
1932 : {
1933 18706266 : if ( bands_entropic[b] )
1934 : {
1935 13829046 : if ( hrmasa_flag )
1936 : {
1937 0 : diff_idx = 0;
1938 : }
1939 : else
1940 : {
1941 13829046 : diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
1942 : }
1943 :
1944 63068745 : for ( m = 0; m < nblocks; m++ )
1945 : {
1946 : int16_t tmp_index;
1947 49239699 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
1948 : {
1949 1237245 : avg_elevation_index_projected = ivas_chan_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
1950 1237245 : tmp_index = ivas_qmetadata_DecodeExtendedGR( bitstream, index, 2 * elev_alph[b] - 1, gr_param_elev );
1951 1237245 : if ( tmp_index % 2 )
1952 : {
1953 125124 : tmp_index = avg_elevation_index_projected + ( ( tmp_index + 1 ) >> 1 );
1954 : }
1955 : else
1956 : {
1957 1112121 : tmp_index = avg_elevation_index_projected - ( tmp_index >> 1 );
1958 : }
1959 1237245 : q_direction->band_data[b].elevation_index[m] = tmp_index;
1960 :
1961 : /*deduce aplhabet for azimuth*/
1962 1237245 : azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][q_direction->band_data[b].elevation_index[m]];
1963 : }
1964 : else
1965 : {
1966 48002454 : avg_elevation_index_projected = ivas_dirac_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
1967 :
1968 48002454 : tmp_index = ivas_qmetadata_DecodeExtendedGR( bitstream, index, elev_alph[b], gr_param_elev );
1969 48002454 : tmp_index = ivas_qmetadata_ReorderElevationDecoded( tmp_index, avg_elevation_index_projected, elev_alph[b] );
1970 :
1971 : /*reorder elevation indexing*/
1972 48002454 : tmp_index = tmp_index - ( elev_alph[b] >> 1 );
1973 48002454 : if ( tmp_index < 0 )
1974 : {
1975 10845090 : tmp_index = tmp_index * -2;
1976 : }
1977 37157364 : else if ( tmp_index > 0 )
1978 : {
1979 15216381 : tmp_index = tmp_index * 2 - 1;
1980 : }
1981 48002454 : q_direction->band_data[b].elevation_index[m] = tmp_index;
1982 :
1983 : /*deduce aplhabet for azimuth*/
1984 48002454 : azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][( q_direction->band_data[b].elevation_index[m] + 1 ) >> 1];
1985 : }
1986 :
1987 : /*decode elevation*/
1988 49239699 : q_direction->band_data[b].elevation[m] = deindex_elevation( &q_direction->band_data[b].elevation_index[m], q_direction->band_data[b].bits_sph_idx[m], q_direction->cfg.mc_ls_setup );
1989 : }
1990 : }
1991 : }
1992 : }
1993 : }
1994 : else
1995 : {
1996 9060090 : for ( b = start_band; b < nbands; b++ )
1997 : {
1998 7761753 : if ( bands_entropic[b] )
1999 : {
2000 6884895 : if ( hrmasa_flag )
2001 : {
2002 0 : diff_idx = 0;
2003 : }
2004 : else
2005 : {
2006 6884895 : diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
2007 : }
2008 :
2009 24549819 : for ( m = 0; m < nblocks; m++ )
2010 : {
2011 17664924 : q_direction->band_data[b].elevation_index[m] = 0;
2012 :
2013 : /*deduce alphabet for azimuth*/
2014 17664924 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2015 : {
2016 1103706 : azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][q_direction->band_data[b].elevation_index[m]];
2017 : }
2018 : else
2019 : {
2020 16561218 : azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][( q_direction->band_data[b].elevation_index[m] + 1 ) >> 1];
2021 : }
2022 :
2023 : /*decode elevation*/
2024 17664924 : q_direction->band_data[b].elevation[m] = deindex_elevation( &q_direction->band_data[b].elevation_index[m], q_direction->band_data[b].bits_sph_idx[m], q_direction->cfg.mc_ls_setup );
2025 : }
2026 : }
2027 : }
2028 : }
2029 :
2030 : /*Azimuth*/
2031 5661120 : avg_azimuth_alphabet = no_phi_masa[bits_direction_masa[diff_idx_min] - 1][0]; /* average azimuth is quantized on the equatorial plane */
2032 5661120 : avg_azimuth_index = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, avg_azimuth_alphabet );
2033 5661120 : avg_azimuth_index = ivas_qmetadata_dereorder_generic( avg_azimuth_index );
2034 5661120 : avg_azimuth_index = avg_azimuth_index + ( avg_azimuth_alphabet >> 1 );
2035 :
2036 5661120 : gr_param_azith = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, 5 + 1 );
2037 5661120 : if ( gr_param_azith == 5 ) /* all the azimuth distances are zero */
2038 : {
2039 8313426 : for ( b = start_band; b < nbands; b++ )
2040 : {
2041 6864837 : if ( bands_entropic[b] )
2042 : {
2043 15903558 : for ( m = 0; m < nblocks; m++ )
2044 : {
2045 10692630 : q_direction->band_data[b].azimuth_index[m] = ivas_dirac_project_azimuth_index( avg_azimuth_index, avg_azimuth_alphabet, azith_alph[b][m] );
2046 :
2047 10692630 : if ( azith_alph[b][m] == 1 )
2048 : {
2049 17658 : q_direction->band_data[b].azimuth[m] = 0.f;
2050 : }
2051 : else
2052 : {
2053 10674972 : q_direction->band_data[b].azimuth[m] = deindex_azimuth( q_direction->band_data[b].azimuth_index[m], q_direction->band_data[b].bits_sph_idx[m], q_direction->band_data[b].elevation_index[m], 0, q_direction->cfg.mc_ls_setup );
2054 : }
2055 : }
2056 : }
2057 : }
2058 : }
2059 : else
2060 : {
2061 4212531 : set_zero( avg_direction_vector, 3 );
2062 4212531 : use_adapt_avg = 0;
2063 4212531 : idx = 0;
2064 :
2065 27935814 : for ( b = start_band; b < nbands; b++ )
2066 : {
2067 23723283 : if ( bands_entropic[b] )
2068 : {
2069 79813059 : for ( m = 0; m < nblocks; m++ )
2070 : {
2071 62128020 : if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( idx == MASA_LIMIT_IDX_AVG_AZI ) && ( nblocks > 1 ) )
2072 : {
2073 115185 : use_adapt_avg = bitstream[*index];
2074 115185 : ( *index )--;
2075 : }
2076 62128020 : avg_azimuth_index_projected = ivas_dirac_project_azimuth_index( avg_azimuth_index, avg_azimuth_alphabet, azith_alph[b][m] );
2077 62128020 : q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, azith_alph[b][m], gr_param_azith );
2078 62128020 : q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_ReorderElevationDecoded( q_direction->band_data[b].azimuth_index[m], avg_azimuth_index_projected, azith_alph[b][m] );
2079 :
2080 62128020 : if ( azith_alph[b][m] == 1 )
2081 : {
2082 145398 : q_direction->band_data[b].azimuth[m] = 0.f;
2083 : }
2084 : else
2085 : {
2086 61982622 : q_direction->band_data[b].azimuth[m] = deindex_azimuth( q_direction->band_data[b].azimuth_index[m], q_direction->band_data[b].bits_sph_idx[m], q_direction->band_data[b].elevation_index[m], 0, q_direction->cfg.mc_ls_setup );
2087 : }
2088 62128020 : if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( nblocks > 1 ) )
2089 : {
2090 2032704 : if ( idx < MASA_LIMIT_IDX_AVG_AZI )
2091 : {
2092 623184 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[m], q_direction->band_data[b].elevation[m], direction_vector );
2093 623184 : v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
2094 : }
2095 : else
2096 : {
2097 1409520 : if ( use_adapt_avg == 1 )
2098 : {
2099 897840 : if ( m == 0 )
2100 : {
2101 224460 : v_multc( avg_direction_vector, 0.5f, avg_direction_vector, 3 );
2102 : }
2103 : /*compute the average direction per already coded subband */
2104 897840 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[m], q_direction->band_data[b].elevation[m], direction_vector );
2105 897840 : v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
2106 897840 : ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &avg_azimuth, &avg_elevation );
2107 897840 : avg_azimuth_index = quantize_phi( avg_azimuth + 180, 0, &avg_azimuth, avg_azimuth_alphabet );
2108 : }
2109 : }
2110 2032704 : idx++;
2111 : }
2112 : }
2113 : }
2114 : }
2115 : }
2116 :
2117 5661120 : return ( index_start - *index );
2118 : }
2119 :
2120 :
2121 : /*-------------------------------------------------------------------------
2122 : * ivas_qmetadata_raw_decode_dir()
2123 : *
2124 : * Main function for raw decoding of the directions
2125 : *------------------------------------------------------------------------*/
2126 :
2127 240195 : static int16_t ivas_qmetadata_raw_decode_dir_512(
2128 : IVAS_QDIRECTION *q_direction, /* i/o: quantized direction structure */
2129 : uint16_t *bitstream, /* i : bitstream */
2130 : int16_t *index,
2131 : const int16_t nbands,
2132 : const int16_t start_band,
2133 : const SPHERICAL_GRID_DATA *sph_grid16 /* i : spherical grid for deindexing */
2134 : )
2135 : {
2136 : int16_t b, m, i;
2137 : int16_t nblocks;
2138 : int16_t index_start;
2139 : uint16_t value;
2140 :
2141 240195 : index_start = *index;
2142 240195 : nblocks = q_direction->cfg.nblocks;
2143 :
2144 5159709 : for ( b = start_band; b < nbands; b++ )
2145 : {
2146 19861104 : for ( m = 0; m < nblocks; m++ )
2147 : {
2148 14941590 : value = 0;
2149 211954770 : for ( i = 0; i < q_direction->band_data[b].bits_sph_idx[m]; i++ )
2150 : {
2151 197013180 : value = ( value << 1 ) + bitstream[( *index )--];
2152 : }
2153 14941590 : q_direction->band_data[b].spherical_index[m] = value;
2154 :
2155 14941590 : if ( q_direction->band_data[b].bits_sph_idx[m] == 16 )
2156 : {
2157 6531138 : deindex_sph_idx( value, sph_grid16, &( q_direction->band_data[b].elevation[m] ), &( q_direction->band_data[b].azimuth[m] ) );
2158 : }
2159 : else
2160 : {
2161 8410452 : deindex_spherical_component( q_direction->band_data[b].spherical_index[m], &q_direction->band_data[b].azimuth[m], &q_direction->band_data[b].elevation[m], &q_direction->band_data[b].azimuth_index[m], &q_direction->band_data[b].elevation_index[m], q_direction->band_data[b].bits_sph_idx[m], q_direction->cfg.mc_ls_setup );
2162 : }
2163 : }
2164 : }
2165 :
2166 240195 : return ( index_start - *index );
2167 : }
2168 :
2169 :
2170 : /*-------------------------------------------------------------------------
2171 : * ivas_qmetadata_raw_decode_dir()
2172 : *
2173 : * Main function for raw decoding of the directions
2174 : *------------------------------------------------------------------------*/
2175 :
2176 3820593 : static int16_t ivas_qmetadata_raw_decode_dir(
2177 : IVAS_QDIRECTION *q_direction, /* i/o: quantized direction structure */
2178 : uint16_t *bitstream, /* i : bitstream */
2179 : int16_t *index,
2180 : const int16_t nbands,
2181 : const int16_t start_band,
2182 : const int16_t hrmasa_flag /* i : flag indicating high-rate MASA MD coding*/
2183 : )
2184 : {
2185 : int16_t b, m, azith_alph;
2186 : int16_t diff_idx;
2187 : int16_t nblocks;
2188 : int16_t index_start;
2189 :
2190 3820593 : index_start = *index;
2191 3820593 : nblocks = q_direction->cfg.nblocks;
2192 :
2193 18703920 : for ( b = start_band; b < nbands; b++ )
2194 : {
2195 14883327 : if ( q_direction->not_in_2D > 0 )
2196 : {
2197 12843450 : decode_fixed_rate( q_direction, bitstream, index, b, nblocks );
2198 : }
2199 : else
2200 : {
2201 2039877 : if ( hrmasa_flag )
2202 : {
2203 0 : diff_idx = 0;
2204 : }
2205 : else
2206 : {
2207 2039877 : diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
2208 : }
2209 :
2210 6255630 : for ( m = 0; m < nblocks; m++ )
2211 : {
2212 4215753 : q_direction->band_data[b].elevation[m] = 0.f;
2213 4215753 : q_direction->band_data[b].elevation_index[m] = 0;
2214 4215753 : azith_alph = no_phi_masa[bits_direction_masa[diff_idx] - 1][0];
2215 4215753 : q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, azith_alph );
2216 4215753 : q_direction->band_data[b].azimuth[m] = deindex_azimuth( q_direction->band_data[b].azimuth_index[m], q_direction->band_data[b].bits_sph_idx[m], 0, 1, q_direction->cfg.mc_ls_setup );
2217 : }
2218 : }
2219 : }
2220 :
2221 3820593 : return ( index_start - *index );
2222 : }
2223 :
2224 :
2225 : /*-------------------------------------------------------------------------
2226 : * ivas_qmetadata_DecodeQuasiUniform()
2227 : *
2228 : * Read the bitstream following the encoding scheme of EncodeQuasiUniform
2229 : *------------------------------------------------------------------------*/
2230 :
2231 : /*! r: Value read from the bitstream */
2232 83905314 : static uint16_t ivas_qmetadata_DecodeQuasiUniform(
2233 : const uint16_t *bitstream, /* i : pointer to the bitstream to read */
2234 : int16_t *index, /* i : position in the bitstream to start reading (gets updated with reading) */
2235 : const uint16_t alphabet_size /* i : size of the alphabet, used to calculate the number of bits needed */
2236 : )
2237 : {
2238 : int16_t i, bits;
2239 : uint16_t tresh, value;
2240 :
2241 : #ifdef DEBUGGING
2242 : assert( ( alphabet_size >= 1 ) );
2243 : #endif
2244 :
2245 83905314 : bits = 30 - norm_l( alphabet_size ); /* bits = floor(log2(alphabet_size)) */
2246 83905314 : tresh = ( 1U << ( bits + 1 ) ) - alphabet_size;
2247 :
2248 83905314 : value = 0;
2249 322273887 : for ( i = 0; i < bits; i++ )
2250 : {
2251 238368573 : value = ( value << 1 ) + bitstream[( *index )--];
2252 : }
2253 :
2254 83905314 : if ( value >= tresh )
2255 : {
2256 11326485 : value = ( value << 1 ) - tresh + bitstream[( *index )--];
2257 : }
2258 :
2259 : #ifdef DEBUGGING
2260 : assert( value < alphabet_size );
2261 : #endif
2262 83905314 : return value;
2263 : }
2264 :
2265 :
2266 : /*-------------------------------------------------------------------------
2267 : * ivas_qmetadata_DecodeExtendedGR()
2268 : *
2269 : * Reads the bitstream and decodes the value using the ExtendedGR algorithm
2270 : *------------------------------------------------------------------------*/
2271 :
2272 : /*! r: Value decoded from the bitstream */
2273 169021407 : int16_t ivas_qmetadata_DecodeExtendedGR(
2274 : uint16_t *bitstream, /* i : pointer to the bitstream to read */
2275 : int16_t *index, /* i/o: position in the bitstream to start reading (gets updated with reading) */
2276 : const int16_t alph_size, /* i : size of the alphabet, used to calculate the number of bits needed */
2277 : const int16_t gr_param /* i : GR parameter that indicates the limit for the most significant bits (msb) */
2278 : )
2279 : {
2280 : int16_t i, msb_size;
2281 : uint16_t value;
2282 : int16_t msb, lsb;
2283 :
2284 169021407 : msb_size = ( alph_size + ( 1 << gr_param ) - 1 ) >> gr_param; /* ceil division */
2285 169021407 : if ( msb_size <= 3 )
2286 : {
2287 14062266 : value = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, alph_size );
2288 : }
2289 : else
2290 : {
2291 154959141 : msb = 0;
2292 265335333 : while ( ( msb < msb_size - 1 ) && ( bitstream[*index] != 0 ) )
2293 : {
2294 110376192 : msb++;
2295 110376192 : ( *index )--;
2296 : }
2297 :
2298 154959141 : if ( msb == msb_size - 1 )
2299 : {
2300 1194582 : lsb = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, alph_size - ( ( msb_size - 1 ) << gr_param ) );
2301 : }
2302 : else
2303 : {
2304 : #ifdef DEBUGGING
2305 : assert( bitstream[*index] == 0 );
2306 : #endif
2307 153764559 : ( *index )--;
2308 153764559 : lsb = 0;
2309 221940015 : for ( i = 0; i < gr_param; i++ )
2310 : {
2311 68175456 : lsb = ( lsb << 1 ) + bitstream[( *index )--];
2312 : }
2313 : }
2314 :
2315 154959141 : value = ( msb << gr_param ) + lsb;
2316 : }
2317 :
2318 : #ifdef DEBUGGING
2319 : assert( value < alph_size );
2320 : #endif
2321 169021407 : return value;
2322 : }
2323 :
2324 :
2325 : /*-------------------------------------------------------------------------
2326 : * ivas_qmetadata_ReorderElevationDecoded()
2327 : *
2328 : * Calculates the correct elevation index from the decoded data
2329 : *------------------------------------------------------------------------*/
2330 :
2331 : /*! r: Elevation index as it will be read by the dequantizer */
2332 110130474 : static int16_t ivas_qmetadata_ReorderElevationDecoded(
2333 : const int16_t elev_dist, /* i : Distance to the average extracted from the bitstream */
2334 : const int16_t elev_avg, /* i : Average value over time-blocks extracted from the bitstream */
2335 : const int16_t elev_alph /* i : elevation alphabet */
2336 : )
2337 : {
2338 : int16_t dist_reorder;
2339 : int16_t elev_index_reorder;
2340 :
2341 110130474 : dist_reorder = ivas_qmetadata_dereorder_generic( elev_dist );
2342 110130474 : elev_index_reorder = elev_avg + dist_reorder;
2343 :
2344 110130474 : if ( elev_index_reorder < 0 )
2345 : {
2346 933477 : elev_index_reorder += elev_alph;
2347 : }
2348 109196997 : else if ( elev_index_reorder >= elev_alph )
2349 : {
2350 968553 : elev_index_reorder -= elev_alph;
2351 : }
2352 :
2353 110130474 : return elev_index_reorder;
2354 : }
2355 :
2356 :
2357 : /*-----------------------------------------------------------------------*
2358 : * Local functions: requentizeEC3
2359 : *-----------------------------------------------------------------------*/
2360 :
2361 : /*! r: number of bits read */
2362 378327 : static int16_t read_directions(
2363 : IVAS_QDIRECTION *q_direction, /* i/o: quantized direction structure */
2364 : const uint8_t coding_subbands, /* i : number of directions */
2365 : const uint8_t masa_subframes, /* i : number of tiles */
2366 : uint16_t *bitstream, /* i : bitstream to be read */
2367 : int16_t *pbit_pos,
2368 : int16_t *ind_order )
2369 : {
2370 : int16_t j, k, allowed_bits, last_j, nbits, fixed_rate;
2371 : int16_t i;
2372 : int16_t diff;
2373 : uint16_t byteBuffer;
2374 : int16_t use_vq, max_nb_idx;
2375 : int16_t bit_pos;
2376 : int16_t *bits_dir0;
2377 :
2378 378327 : bit_pos = *pbit_pos;
2379 :
2380 378327 : diff = 0;
2381 378327 : if ( q_direction->not_in_2D )
2382 : {
2383 360732 : if ( coding_subbands > 1 )
2384 : {
2385 353874 : j = ind_order[coding_subbands - 1];
2386 353874 : allowed_bits = 0;
2387 :
2388 1313934 : for ( k = 0; k < masa_subframes; k++ )
2389 : {
2390 960060 : allowed_bits += q_direction->band_data[j].bits_sph_idx[k];
2391 : }
2392 :
2393 353874 : last_j = j - ( allowed_bits == 0 );
2394 :
2395 1521732 : for ( j = 0; j < last_j; j++ )
2396 : {
2397 1167858 : i = ind_order[j];
2398 1167858 : bits_dir0 = (int16_t *) q_direction->band_data[i].bits_sph_idx;
2399 :
2400 1167858 : nbits = 0;
2401 1167858 : allowed_bits = sum_s( bits_dir0, q_direction->cfg.nblocks );
2402 1167858 : use_vq = 0;
2403 1167858 : max_nb_idx = 0;
2404 :
2405 4139478 : for ( k = 0; k < q_direction->cfg.nblocks; k++ )
2406 : {
2407 2971620 : if ( bits_dir0[k] > use_vq )
2408 : {
2409 1394793 : use_vq = bits_dir0[k];
2410 1394793 : max_nb_idx = k;
2411 : }
2412 : }
2413 :
2414 1167858 : if ( q_direction->cfg.nblocks == 1 )
2415 : {
2416 566604 : byteBuffer = 0;
2417 : }
2418 : else
2419 : {
2420 601254 : byteBuffer = 0;
2421 601254 : if ( use_vq <= 1 )
2422 : {
2423 252 : byteBuffer = 1;
2424 : }
2425 601254 : if ( use_vq > 1 && use_vq <= LIMIT_USE_COMMON )
2426 : {
2427 98871 : bits_dir0[max_nb_idx] -= 1;
2428 98871 : allowed_bits -= 1;
2429 : /* read 1 bit to tell if joint of VQ coding */
2430 98871 : byteBuffer = bitstream[bit_pos--];
2431 : }
2432 : }
2433 :
2434 4139478 : for ( k = 0; k < masa_subframes; k++ )
2435 : {
2436 2971620 : q_direction->band_data[i].bits_sph_idx[k] = bits_dir0[k];
2437 2971620 : if ( bits_dir0[k] > 2 )
2438 : {
2439 2833266 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2440 : {
2441 893595 : q_direction->band_data[i].elevation_m_alphabet[k] = no_theta_masa[bits_dir0[k] - 3];
2442 : }
2443 : else
2444 : {
2445 1939671 : q_direction->band_data[i].elevation_m_alphabet[k] = no_theta_masa[bits_dir0[k] - 3] * 2 - 1;
2446 : }
2447 : }
2448 : else
2449 : {
2450 138354 : q_direction->band_data[i].elevation_m_alphabet[k] = 1;
2451 : }
2452 : }
2453 :
2454 1167858 : if ( allowed_bits > 0 )
2455 : {
2456 1167858 : if ( byteBuffer == 1 )
2457 : {
2458 97620 : nbits = read_common_direction( bitstream, q_direction, i, masa_subframes, allowed_bits, &bit_pos );
2459 : }
2460 : else
2461 : {
2462 1070238 : if ( q_direction->cfg.nblocks == 1 && q_direction->band_data[i].bits_sph_idx[0] <= MASA_MIN_BITS_TF + 1 )
2463 : {
2464 : /* there is fixed rate only, no need to read */
2465 301833 : fixed_rate = 1;
2466 301833 : nbits = 0;
2467 : }
2468 : else
2469 : {
2470 : /* check if fixed_rate */
2471 768405 : fixed_rate = bitstream[bit_pos--];
2472 768405 : nbits = 1;
2473 : }
2474 :
2475 1070238 : if ( fixed_rate == 1 )
2476 : {
2477 : /* decode_fixed_rate()*/
2478 765072 : nbits += decode_fixed_rate( q_direction, bitstream, &bit_pos, i, masa_subframes );
2479 : }
2480 : else
2481 : {
2482 : /* decode elevation */
2483 305166 : nbits += decode_elevation( q_direction, bitstream, &bit_pos, i, masa_subframes );
2484 :
2485 : /* decode azimuth */
2486 305166 : nbits += decode_azimuth( q_direction, bitstream, &bit_pos, i, masa_subframes );
2487 : }
2488 : }
2489 : }
2490 : else
2491 : {
2492 0 : set_zero_direction( q_direction, i, masa_subframes );
2493 : }
2494 1167858 : diff += nbits - allowed_bits;
2495 :
2496 : /* update bits for next block */
2497 1167858 : update_bits_next_block( q_direction, &diff, ind_order[j + 1], coding_subbands, masa_subframes );
2498 : }
2499 : }
2500 : else
2501 : {
2502 6858 : last_j = q_direction->cfg.start_band;
2503 : }
2504 :
2505 :
2506 948750 : for ( j = last_j; j < coding_subbands; j++ )
2507 : {
2508 588018 : i = ind_order[j];
2509 588018 : bits_dir0 = (int16_t *) q_direction->band_data[i].bits_sph_idx;
2510 :
2511 588018 : nbits = 0;
2512 588018 : allowed_bits = sum_s( bits_dir0, q_direction->cfg.nblocks );
2513 588018 : if ( allowed_bits > 0 && masa_subframes == 1 )
2514 : {
2515 265425 : nbits += decode_fixed_rate( q_direction, bitstream, &bit_pos, i, masa_subframes );
2516 : }
2517 : else
2518 : {
2519 322593 : if ( allowed_bits > 0 )
2520 : {
2521 322593 : use_vq = 0;
2522 322593 : max_nb_idx = 0;
2523 1612965 : for ( k = 0; k < masa_subframes; k++ )
2524 : {
2525 1290372 : if ( bits_dir0[k] > use_vq )
2526 : {
2527 440832 : use_vq = bits_dir0[k];
2528 440832 : max_nb_idx = k;
2529 : }
2530 : }
2531 :
2532 322593 : byteBuffer = 0;
2533 :
2534 322593 : if ( use_vq > 1 && use_vq <= LIMIT_USE_COMMON )
2535 : {
2536 43131 : bits_dir0[max_nb_idx] -= 1;
2537 43131 : allowed_bits -= 1;
2538 :
2539 : /* read 1 bit to tell if joint of VQ coding */
2540 43131 : byteBuffer = bitstream[bit_pos--];
2541 : }
2542 :
2543 322593 : if ( allowed_bits > 0 )
2544 : {
2545 322593 : if ( byteBuffer == 1 || use_vq <= 1 )
2546 : {
2547 42108 : nbits = read_common_direction( bitstream, q_direction, i, masa_subframes, allowed_bits, &bit_pos );
2548 : }
2549 : else
2550 : {
2551 : /* decode_fixed_rate()*/
2552 280485 : nbits += decode_fixed_rate( q_direction, bitstream, &bit_pos, i, masa_subframes );
2553 : }
2554 : }
2555 : else
2556 : {
2557 0 : set_zero_direction( q_direction, i, masa_subframes );
2558 : }
2559 : }
2560 : else
2561 : {
2562 0 : set_zero_direction( q_direction, i, masa_subframes );
2563 : }
2564 : }
2565 : }
2566 : }
2567 : else
2568 : {
2569 : /* 2D */
2570 103233 : for ( j = 0; j < coding_subbands; j++ )
2571 : {
2572 269673 : for ( k = 0; k < q_direction->cfg.nblocks; k++ )
2573 : {
2574 184035 : q_direction->band_data[j].elevation[k] = 0;
2575 184035 : q_direction->band_data[j].elevation_index[k] = 0;
2576 : }
2577 : }
2578 17595 : nbits = decode_azimuth2D( q_direction, bitstream, coding_subbands, &bit_pos, masa_subframes );
2579 : }
2580 378327 : nbits = *pbit_pos - bit_pos;
2581 378327 : *pbit_pos = bit_pos;
2582 :
2583 378327 : return nbits;
2584 : }
2585 :
2586 :
2587 : /*-------------------------------------------------------------------*
2588 : * decode_azimuth()
2589 : *
2590 : * read and decode the azimuth indexes for one subband
2591 : *-------------------------------------------------------------------*/
2592 :
2593 : /*! r: number of bits read */
2594 305166 : static int16_t decode_azimuth(
2595 : IVAS_QDIRECTION *q_direction, /* i/o: quantized metadata structure */
2596 : uint16_t *bitstream, /* i : bitstream to be read */
2597 : int16_t *pbit_pos, /* i/o: current position in bitstream */
2598 : const int16_t idx_subband, /* i : subband index */
2599 : const int16_t masa_subframes /* i : number of tiles */
2600 : )
2601 : {
2602 : int16_t bit_pos, nbits, k;
2603 : uint16_t use_context, byteBuffer;
2604 : uint16_t min_idx;
2605 : int16_t j_az, max_val;
2606 :
2607 305166 : nbits = 0;
2608 305166 : bit_pos = *pbit_pos;
2609 305166 : byteBuffer = 0;
2610 :
2611 305166 : j_az = 0;
2612 : /* check number of valid indexes to decode */
2613 1484799 : for ( k = 0; k < masa_subframes; k++ )
2614 : {
2615 1179633 : if ( q_direction->band_data[idx_subband].azimuth_index[k] < MASA_NO_INDEX )
2616 : {
2617 1177086 : j_az++;
2618 : }
2619 : else
2620 : {
2621 2547 : q_direction->band_data[idx_subband].azimuth[k] = 0.f; /*To be in sync with encoder values.*/
2622 : }
2623 : }
2624 :
2625 305166 : if ( j_az == 0 )
2626 : {
2627 240 : return nbits;
2628 : }
2629 :
2630 304926 : if ( byteBuffer == 0 )
2631 : {
2632 : /* use context */
2633 304926 : use_context = 0;
2634 1483599 : for ( k = 0; k < masa_subframes; k++ )
2635 : {
2636 1178673 : if ( q_direction->band_data[idx_subband].bits_sph_idx[k] <= 1 )
2637 : {
2638 0 : use_context = 1;
2639 : }
2640 : }
2641 :
2642 304926 : if ( use_context == 1 )
2643 : {
2644 0 : for ( k = 0; k < masa_subframes; k++ )
2645 : {
2646 0 : if ( q_direction->band_data[idx_subband].bits_sph_idx[k] == 0 )
2647 : {
2648 0 : q_direction->band_data[idx_subband].azimuth_index[k] = 0;
2649 0 : q_direction->band_data[idx_subband].azimuth[k] = 0;
2650 : }
2651 : else
2652 : {
2653 0 : if ( q_direction->band_data[idx_subband].bits_sph_idx[k] == 1 )
2654 : {
2655 0 : byteBuffer = bitstream[bit_pos--];
2656 0 : q_direction->band_data[idx_subband].azimuth_index[k] = byteBuffer;
2657 :
2658 0 : q_direction->band_data[idx_subband].azimuth[k] = q_direction->band_data[idx_subband].azimuth_index[k] * ( -180.f );
2659 : }
2660 : else
2661 : {
2662 0 : q_direction->band_data[idx_subband].azimuth_index[k] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, q_direction->band_data[idx_subband].azimuth_m_alphabet[k], MASA_GR_ORD_AZ - ( q_direction->band_data[idx_subband].bits_sph_idx[k] == 2 ) );
2663 :
2664 0 : q_direction->band_data[idx_subband].azimuth[k] = deindex_azimuth( q_direction->band_data[idx_subband].azimuth_index[k], q_direction->band_data[idx_subband].bits_sph_idx[k], q_direction->band_data[idx_subband].elevation_index[k], 1, q_direction->cfg.mc_ls_setup );
2665 : }
2666 : }
2667 : }
2668 : }
2669 : else
2670 : {
2671 : /* read bit to check if min removed encoding */
2672 304926 : byteBuffer = bitstream[bit_pos--];
2673 304926 : if ( byteBuffer == 0 ) /* regular GR coding5 */
2674 : {
2675 : /* read GR_order */
2676 135402 : byteBuffer = bitstream[bit_pos--];
2677 135402 : nbits += 1;
2678 :
2679 635979 : for ( k = 0; k < masa_subframes; k++ )
2680 : {
2681 500577 : if ( q_direction->band_data[idx_subband].bits_sph_idx[k] > 0 )
2682 : {
2683 500577 : if ( no_phi_masa[q_direction->band_data[idx_subband].bits_sph_idx[k] - 1][q_direction->band_data[idx_subband].elevation_index[k]] > 1 )
2684 : {
2685 499104 : q_direction->band_data[idx_subband].azimuth_index[k] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, q_direction->band_data[idx_subband].azimuth_m_alphabet[k], MASA_GR_ORD_AZ - byteBuffer );
2686 499104 : q_direction->band_data[idx_subband].azimuth[k] = deindex_azimuth( q_direction->band_data[idx_subband].azimuth_index[k], q_direction->band_data[idx_subband].bits_sph_idx[k], q_direction->band_data[idx_subband].elevation_index[k], 1, q_direction->cfg.mc_ls_setup );
2687 : }
2688 : else
2689 : {
2690 1473 : q_direction->band_data[idx_subband].azimuth[k] = 0;
2691 1473 : q_direction->band_data[idx_subband].azimuth_index[k] = 0;
2692 : }
2693 : }
2694 : else
2695 : {
2696 0 : q_direction->band_data[idx_subband].azimuth[k] = 0;
2697 0 : q_direction->band_data[idx_subband].azimuth_index[k] = 0;
2698 : }
2699 : }
2700 : }
2701 : else
2702 : {
2703 : /* min removed GR coding */
2704 : /* read GR_order */
2705 169524 : byteBuffer = bitstream[bit_pos--];
2706 : /* read min index value */
2707 169524 : maximum_s( q_direction->band_data[idx_subband].azimuth_m_alphabet, masa_subframes, &max_val );
2708 169524 : min_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, max_val, MASA_GR_ORD_AZ );
2709 :
2710 847620 : for ( k = 0; k < masa_subframes; k++ )
2711 : {
2712 678096 : if ( q_direction->band_data[idx_subband].bits_sph_idx[k] > 0 )
2713 : {
2714 678096 : if ( no_phi_masa[q_direction->band_data[idx_subband].bits_sph_idx[k] - 1][q_direction->band_data[idx_subband].elevation_index[k]] > 1 )
2715 : {
2716 677982 : q_direction->band_data[idx_subband].azimuth_index[k] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, q_direction->band_data[idx_subband].azimuth_m_alphabet[k], MASA_GR_ORD_AZ - 1 - byteBuffer );
2717 677982 : q_direction->band_data[idx_subband].azimuth_index[k] += min_idx;
2718 677982 : q_direction->band_data[idx_subband].azimuth[k] = deindex_azimuth( q_direction->band_data[idx_subband].azimuth_index[k], q_direction->band_data[idx_subband].bits_sph_idx[k], q_direction->band_data[idx_subband].elevation_index[k], 1, q_direction->cfg.mc_ls_setup );
2719 : }
2720 : else
2721 : {
2722 114 : q_direction->band_data[idx_subband].azimuth[k] = 0;
2723 114 : q_direction->band_data[idx_subband].azimuth_index[k] = 0;
2724 : }
2725 : }
2726 : else
2727 : {
2728 0 : q_direction->band_data[idx_subband].azimuth[k] = 0;
2729 0 : q_direction->band_data[idx_subband].azimuth_index[k] = 0;
2730 : }
2731 : }
2732 : }
2733 : }
2734 : }
2735 :
2736 304926 : nbits = *pbit_pos - bit_pos;
2737 :
2738 304926 : *pbit_pos = bit_pos;
2739 :
2740 304926 : return nbits;
2741 : }
2742 :
2743 :
2744 : /*-------------------------------------------------------------------*
2745 : * decode_elevation()
2746 : *
2747 : * Reads the bitstream and decode the elevation index
2748 : *-------------------------------------------------------------------*/
2749 :
2750 : /*! r: number of bits read */
2751 305166 : static int16_t decode_elevation(
2752 : IVAS_QDIRECTION *q_direction, /* i/o: quantized metadata structure */
2753 : uint16_t *bitstream, /* i : input bitstream */
2754 : int16_t *pbit_pos, /* i/o: current position to be read in bitstream*/
2755 : const int16_t j, /* i : subband index */
2756 : const int16_t masa_subframes /* i : number of tiles */
2757 : )
2758 : {
2759 : int16_t nr_NO_INDEX, nbits;
2760 : int16_t bit_pos;
2761 : uint16_t byteBuffer;
2762 : int16_t k, GR_ord_elevation;
2763 : uint16_t same_idx;
2764 :
2765 305166 : nr_NO_INDEX = 0;
2766 305166 : nbits = 0;
2767 305166 : bit_pos = *pbit_pos;
2768 :
2769 1484799 : for ( k = 0; k < masa_subframes; k++ )
2770 : {
2771 1179633 : q_direction->band_data[j].elevation_index[k] = 0;
2772 1179633 : q_direction->band_data[j].elevation[k] = 0;
2773 :
2774 1179633 : if ( q_direction->band_data[j].bits_sph_idx[k] > 0 )
2775 : {
2776 1179633 : if ( q_direction->band_data[j].bits_sph_idx[k] <= 2 )
2777 : {
2778 24 : q_direction->band_data[j].elevation_index[k] = MASA_NO_INDEX;
2779 24 : nr_NO_INDEX += 1;
2780 24 : q_direction->band_data[j].elevation[k] = 0;
2781 24 : q_direction->band_data[j].elevation_m_alphabet[k] = 1;
2782 : }
2783 : else
2784 : {
2785 1179609 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2786 : {
2787 419325 : q_direction->band_data[j].elevation_m_alphabet[k] = no_theta_masa[q_direction->band_data[j].bits_sph_idx[k] - 3];
2788 : }
2789 : else
2790 : {
2791 760284 : q_direction->band_data[j].elevation_m_alphabet[k] = no_theta_masa[q_direction->band_data[j].bits_sph_idx[k] - 3] * 2 - 1;
2792 : }
2793 : }
2794 : }
2795 : else
2796 : {
2797 0 : nr_NO_INDEX++;
2798 : }
2799 : }
2800 :
2801 305166 : if ( nr_NO_INDEX < masa_subframes )
2802 : {
2803 : {
2804 : /* read if same or not */
2805 305166 : byteBuffer = bitstream[bit_pos--];
2806 305166 : if ( byteBuffer == 1 ) /* same value */
2807 : {
2808 : /* read value */
2809 234984 : byteBuffer = bitstream[bit_pos--];
2810 234984 : byteBuffer = ( byteBuffer << 1 ) + bitstream[bit_pos--];
2811 234984 : same_idx = byteBuffer;
2812 :
2813 1133907 : for ( k = 0; k < masa_subframes; k++ )
2814 : {
2815 898923 : if ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX )
2816 : {
2817 898923 : q_direction->band_data[j].elevation_index[k] = same_idx;
2818 898923 : q_direction->band_data[j].elevation[k] = deindex_elevation( &q_direction->band_data[j].elevation_index[k], q_direction->band_data[j].bits_sph_idx[k], q_direction->cfg.mc_ls_setup );
2819 : }
2820 : }
2821 : }
2822 : else
2823 : {
2824 : /* not same; decode mean removed GR */
2825 70182 : byteBuffer = bitstream[bit_pos--];
2826 70182 : GR_ord_elevation = MASA_GR_ORD_EL - byteBuffer;
2827 :
2828 350892 : for ( k = 0; k < masa_subframes; k++ )
2829 : {
2830 280710 : if ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX )
2831 : {
2832 280686 : q_direction->band_data[j].elevation_index[k] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, q_direction->band_data[j].elevation_m_alphabet[k], GR_ord_elevation );
2833 280686 : q_direction->band_data[j].elevation[k] = deindex_elevation( &q_direction->band_data[j].elevation_index[k], q_direction->band_data[j].bits_sph_idx[k], q_direction->cfg.mc_ls_setup );
2834 : }
2835 : }
2836 : }
2837 : }
2838 : }
2839 :
2840 1484799 : for ( k = 0; k < masa_subframes; k++ )
2841 : {
2842 1179633 : if ( ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX ) &&
2843 1179609 : ( no_phi_masa[q_direction->band_data[j].bits_sph_idx[k] - 1][q_direction->band_data[j].elevation_index[k]] <= 1 ) )
2844 : {
2845 2547 : q_direction->band_data[j].azimuth_index[k] = MASA_NO_INDEX;
2846 2547 : q_direction->band_data[j].azimuth_m_alphabet[k] = 1;
2847 : }
2848 : else
2849 : {
2850 1177086 : q_direction->band_data[j].azimuth_index[k] = 0;
2851 1177086 : if ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX )
2852 : {
2853 1177062 : q_direction->band_data[j].azimuth_m_alphabet[k] = no_phi_masa[q_direction->band_data[j].bits_sph_idx[k] - 1][q_direction->band_data[j].elevation_index[k]];
2854 : }
2855 : else
2856 : {
2857 24 : q_direction->band_data[j].azimuth_m_alphabet[k] = no_phi_masa[q_direction->band_data[j].bits_sph_idx[k] - 1][0];
2858 24 : q_direction->band_data[j].elevation_index[k] = 0;
2859 : }
2860 : }
2861 : }
2862 :
2863 305166 : nbits = *pbit_pos - bit_pos;
2864 305166 : *pbit_pos = bit_pos;
2865 :
2866 305166 : return nbits;
2867 : }
2868 :
2869 :
2870 : /*----------------------------------------------------------------
2871 : * decode_fixed_rate()
2872 : *
2873 : * decoding in fixed rate case, i.e. when using the spherical indexes
2874 : *-----------------------------------------------------------------*/
2875 :
2876 : /*! r: number of bits read */
2877 20969727 : static int16_t decode_fixed_rate(
2878 : IVAS_QDIRECTION *q_direction, /* i/o: quantized metadata */
2879 : const uint16_t *bitstream, /* i : bitstream to be read */
2880 : int16_t *pbit_pos, /* i/o: position in bitstream */
2881 : const int16_t b, /* i : subband index */
2882 : const int16_t nblocks /* i : number of tiles in subband */
2883 : )
2884 : {
2885 : int16_t nbits, m, i;
2886 : uint16_t value;
2887 :
2888 20969727 : nbits = 0;
2889 :
2890 86906991 : for ( m = 0; m < nblocks; m++ )
2891 : {
2892 65937264 : value = 0;
2893 340834044 : for ( i = 0; i < q_direction->band_data[b].bits_sph_idx[m]; i++ )
2894 : {
2895 274896780 : value = ( value << 1 ) + bitstream[( *pbit_pos )--];
2896 : }
2897 :
2898 65937264 : q_direction->band_data[b].spherical_index[m] = value;
2899 65937264 : nbits += q_direction->band_data[b].bits_sph_idx[m];
2900 :
2901 65937264 : deindex_spherical_component( q_direction->band_data[b].spherical_index[m], &q_direction->band_data[b].azimuth[m], &q_direction->band_data[b].elevation[m], &q_direction->band_data[b].azimuth_index[m], &q_direction->band_data[b].elevation_index[m], q_direction->band_data[b].bits_sph_idx[m], q_direction->cfg.mc_ls_setup );
2902 : }
2903 :
2904 20969727 : return nbits;
2905 : }
2906 :
2907 :
2908 : /*-------------------------------------------------------------------*
2909 : * decode_azimuth2D()
2910 : *
2911 : * Azimuth bitstream reading and decoding in 2D case
2912 : *-------------------------------------------------------------------*/
2913 :
2914 : /*! r: number of bits read */
2915 17595 : static int16_t decode_azimuth2D(
2916 : IVAS_QDIRECTION *q_direction, /* i/o: quantized metadata structure */
2917 : uint16_t *bitstream, /* i : bitstream to be read */
2918 : const int16_t coding_subbands, /* i : number of subbands */
2919 : int16_t *pbit_pos,
2920 : const int16_t no_frames )
2921 : {
2922 : int16_t i, j, k;
2923 : int16_t allowed_bits, nbits;
2924 : int16_t use_vq;
2925 : uint16_t Buffer;
2926 : int16_t bit_pos;
2927 : int16_t *bits_dir0;
2928 :
2929 17595 : bit_pos = *pbit_pos;
2930 17595 : nbits = 0;
2931 103233 : for ( j = 0; j < coding_subbands; j++ )
2932 : {
2933 85638 : bits_dir0 = (int16_t *) q_direction->band_data[j].bits_sph_idx;
2934 85638 : allowed_bits = sum_s( bits_dir0, no_frames );
2935 :
2936 85638 : if ( allowed_bits > 0 )
2937 : {
2938 85638 : use_vq = 0;
2939 269673 : for ( k = 0; k < no_frames; k++ )
2940 : {
2941 184035 : q_direction->band_data[j].elevation[k] = 0;
2942 184035 : q_direction->band_data[j].elevation_index[k] = 0;
2943 :
2944 184035 : if ( bits_dir0[k] > use_vq )
2945 : {
2946 92712 : use_vq = bits_dir0[k];
2947 : }
2948 : }
2949 :
2950 85638 : if ( use_vq <= 3 && allowed_bits <= 11 )
2951 : {
2952 2688 : if ( allowed_bits <= no_frames + 1 )
2953 : {
2954 384 : set_f( q_direction->band_data[j].azimuth, 0.0f, no_frames );
2955 1884 : for ( k = 0; k < min( allowed_bits, no_frames ); k++ )
2956 : {
2957 1500 : q_direction->band_data[j].azimuth[k] = ( -180.f ) * bitstream[bit_pos--];
2958 1500 : nbits += 1;
2959 : }
2960 : }
2961 : else
2962 : {
2963 2304 : nbits += read_truncGR_azimuth( bitstream, q_direction, j, no_frames, &bit_pos );
2964 : }
2965 : }
2966 : else
2967 : {
2968 261192 : for ( k = 0; k < no_frames; k++ )
2969 : {
2970 178242 : Buffer = 0;
2971 1145211 : for ( i = 0; i < bits_dir0[k]; i++ )
2972 : {
2973 966969 : Buffer = ( Buffer << 1 ) + bitstream[bit_pos--];
2974 : }
2975 :
2976 178242 : nbits += bits_dir0[k];
2977 :
2978 178242 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2979 : {
2980 97632 : q_direction->band_data[j].azimuth[k] = 360.0f / (float) ( 1 << bits_dir0[k] ) * Buffer - 180;
2981 97632 : q_direction->band_data[j].azimuth[k] = companding_azimuth( q_direction->band_data[j].azimuth[k], q_direction->cfg.mc_ls_setup,
2982 97632 : ( q_direction->band_data[j].elevation[k] > MC_MASA_THR_ELEVATION ), -1 );
2983 : }
2984 : else
2985 : {
2986 80610 : q_direction->band_data[j].azimuth[k] = 360.0f / (float) ( 1 << bits_dir0[k] ) * Buffer - 180;
2987 : }
2988 : }
2989 : }
2990 : }
2991 : else
2992 : {
2993 0 : set_zero_direction( q_direction, j, no_frames );
2994 : }
2995 : }
2996 17595 : *pbit_pos = bit_pos;
2997 :
2998 17595 : return nbits;
2999 : }
3000 :
3001 :
3002 : /*-------------------------------------------------------------------*
3003 : * set_zero_direction()
3004 : *
3005 : *
3006 : *-------------------------------------------------------------------*/
3007 :
3008 139728 : static void set_zero_direction(
3009 : IVAS_QDIRECTION *q_direction,
3010 : const int16_t idx_band,
3011 : const int16_t len )
3012 : {
3013 : int16_t k;
3014 :
3015 698640 : for ( k = 0; k < len; k++ )
3016 : {
3017 558912 : q_direction->band_data[idx_band].azimuth[k] = 0;
3018 558912 : q_direction->band_data[idx_band].azimuth_index[k] = 0;
3019 558912 : q_direction->band_data[idx_band].elevation[k] = 0;
3020 558912 : q_direction->band_data[idx_band].elevation_index[k] = 0;
3021 558912 : q_direction->band_data[idx_band].spherical_index[k] = 0;
3022 : }
3023 :
3024 139728 : return;
3025 : }
3026 :
3027 :
3028 : /*-------------------------------------------------------------------*
3029 : * read_truncGR_azimuth()
3030 : *
3031 : *
3032 : *-------------------------------------------------------------------*/
3033 :
3034 136986 : static int16_t read_truncGR_azimuth(
3035 : uint16_t *bitstream, /* i : bitstream to be read */
3036 : IVAS_QDIRECTION *q_direction, /* i/o: quantized metadata structure */
3037 : const int16_t j, /* i : subband index */
3038 : const int16_t no_subframes, /* i : number of tiles */
3039 : int16_t *pbit_pos /* i/o: position in bitstream */
3040 : )
3041 : {
3042 : int16_t i;
3043 : int16_t nbits;
3044 : uint16_t idx;
3045 : int16_t no_symb, allowed_bits;
3046 :
3047 136986 : allowed_bits = sum_s( (int16_t *) q_direction->band_data[j].bits_sph_idx, no_subframes );
3048 136986 : nbits = 0;
3049 136986 : if ( allowed_bits <= no_subframes + 1 )
3050 : {
3051 0 : for ( i = 0; i < min( allowed_bits, no_subframes ); i++ )
3052 : {
3053 0 : if ( bitstream[( *pbit_pos )--] == 0 )
3054 : {
3055 0 : q_direction->band_data[j].azimuth[i] = 0.0f;
3056 : }
3057 : else
3058 : {
3059 0 : q_direction->band_data[j].azimuth[i] = -180;
3060 : }
3061 0 : nbits++;
3062 : }
3063 :
3064 0 : return nbits;
3065 : }
3066 :
3067 136986 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3068 : {
3069 66210 : no_symb = 9;
3070 : }
3071 : else
3072 : {
3073 70776 : no_symb = 8;
3074 : }
3075 136986 : nbits = 0;
3076 :
3077 136986 : nbits = *pbit_pos;
3078 :
3079 680007 : for ( i = 0; i < no_subframes; i++ )
3080 : {
3081 543021 : idx = ivas_qmetadata_DecodeExtendedGR( bitstream, pbit_pos, no_symb, 0 );
3082 543021 : q_direction->band_data[j].azimuth_index[i] = idx;
3083 543021 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3084 : {
3085 264732 : q_direction->band_data[j].azimuth[i] = cb_azi_chan[( idx + 1 ) >> 1];
3086 264732 : if ( idx % 2 > 0 )
3087 : {
3088 63663 : q_direction->band_data[j].azimuth[i] = -q_direction->band_data[j].azimuth[i];
3089 : }
3090 : }
3091 : else
3092 : {
3093 278289 : q_direction->band_data[j].azimuth[i] = azimuth_cb[idx];
3094 : }
3095 : }
3096 :
3097 136986 : nbits -= *pbit_pos;
3098 :
3099 136986 : return nbits;
3100 : }
3101 :
3102 :
3103 : /*-------------------------------------------------------------------*
3104 : * read_common_direction()
3105 : *
3106 : *
3107 : *-------------------------------------------------------------------*/
3108 :
3109 : /*! r: number of bits read */
3110 139728 : static int16_t read_common_direction(
3111 : uint16_t *bitstream, /* i : bitstream to be read */
3112 : IVAS_QDIRECTION *q_direction, /* i/o: quantized direction structure */
3113 : const int16_t j, /* i : subband index */
3114 : const int16_t no_subframes, /* i : number of tiles */
3115 : const int16_t bits_total, /* i : number of bits for subband directional data */
3116 : int16_t *pbit_pos /* i/o: position in bitstream */
3117 : )
3118 : {
3119 : int16_t nbits;
3120 : int16_t bit_pos;
3121 : int16_t i;
3122 : uint16_t byteBuffer;
3123 : int16_t bits_el;
3124 :
3125 139728 : bit_pos = *pbit_pos;
3126 139728 : nbits = 0;
3127 :
3128 139728 : set_zero_direction( q_direction, j, no_subframes );
3129 139728 : if ( bits_total == 0 )
3130 : {
3131 0 : return nbits;
3132 : }
3133 :
3134 139728 : if ( bits_total <= no_subframes + 1 )
3135 : {
3136 2325 : for ( i = 0; i < min( no_subframes, bits_total ); i++ )
3137 : {
3138 1860 : byteBuffer = bitstream[bit_pos--];
3139 : /*qdirection->azimuth_index[j][i] = (uint16_t)byteBuffer; */
3140 1860 : q_direction->band_data[j].azimuth[i] = azimuth_cb[byteBuffer];
3141 1860 : nbits += 1;
3142 : }
3143 465 : *pbit_pos = bit_pos;
3144 :
3145 : /*nbits += read_truncGR_azimuth(bitstream, qdirection, j, no_subframes, pbit_pos); */
3146 465 : return nbits;
3147 : }
3148 :
3149 :
3150 139263 : byteBuffer = bitstream[bit_pos--];
3151 139263 : bits_el = 1;
3152 139263 : nbits += 1;
3153 : /* elevation is already set to 0*/
3154 139263 : if ( byteBuffer == 1 )
3155 : {
3156 75405 : byteBuffer = bitstream[bit_pos--];
3157 75405 : bits_el += 1;
3158 75405 : nbits += 1;
3159 75405 : if ( byteBuffer == 0 )
3160 : {
3161 314595 : for ( i = 0; i < no_subframes; i++ )
3162 : {
3163 251676 : q_direction->band_data[j].elevation[i] = delta_theta_masa[2];
3164 : }
3165 : }
3166 : else
3167 : {
3168 12486 : byteBuffer = bitstream[bit_pos--];
3169 12486 : bits_el += 1;
3170 12486 : nbits += 1;
3171 12486 : if ( byteBuffer == 0 )
3172 : {
3173 42540 : for ( i = 0; i < no_subframes; i++ )
3174 : {
3175 34032 : q_direction->band_data[j].elevation[i] = -delta_theta_masa[2];
3176 : }
3177 : }
3178 : else
3179 : {
3180 : /* theta is +/- 90; no azimuth is read */
3181 3978 : byteBuffer = bitstream[bit_pos--];
3182 3978 : nbits += 1;
3183 3978 : if ( byteBuffer == 0 )
3184 : {
3185 3768 : set_f( q_direction->band_data[j].elevation, 90.0f, no_subframes );
3186 3768 : set_f( q_direction->band_data[j].azimuth, 0.0f, no_subframes );
3187 : }
3188 : else
3189 : {
3190 210 : set_f( q_direction->band_data[j].elevation, -90.0f, no_subframes );
3191 210 : set_f( q_direction->band_data[j].azimuth, 0.0f, no_subframes );
3192 : }
3193 3978 : *pbit_pos = bit_pos;
3194 :
3195 3978 : return nbits;
3196 : }
3197 : }
3198 : }
3199 :
3200 135285 : bits_el = sum_s( (int16_t *) q_direction->band_data[j].bits_sph_idx, no_subframes ) - bits_el;
3201 :
3202 135285 : if ( bits_el <= no_subframes + 1 )
3203 : {
3204 603 : nbits += min( no_subframes, bits_el );
3205 3009 : for ( i = 0; i < min( no_subframes, bits_el ); i++ )
3206 : {
3207 2406 : byteBuffer = bitstream[bit_pos--];
3208 : /*qdirection->azimuth_index[j][i] = (uint16_t) byteBuffer; */
3209 2406 : q_direction->band_data[j].azimuth[i] = azimuth_cb[byteBuffer];
3210 : }
3211 : }
3212 : else
3213 : {
3214 134682 : nbits += read_truncGR_azimuth( bitstream, q_direction, j, no_subframes, &bit_pos );
3215 : }
3216 :
3217 135285 : *pbit_pos = bit_pos;
3218 :
3219 : #ifdef DEBUGGING
3220 : /*assert(nbits == bits_total); */
3221 : #endif
3222 135285 : return nbits;
3223 : }
3224 :
3225 :
3226 : /*-----------------------------------------------------------------------*
3227 : * Local functions: coherence
3228 : *-----------------------------------------------------------------------*/
3229 :
3230 1256460 : static void decode_spread_coherence(
3231 : IVAS_QMETADATA_HANDLE hQMetaData, /* i/o: quantized metadata structure */
3232 : int16_t idx_d, /* i : direction index */
3233 : const int16_t no_frames, /* i : number of time subframes */
3234 : const int16_t hrmasa_flag /* i : flag indicating high-rate MASA MD coding */
3235 : )
3236 : {
3237 : int16_t i, j;
3238 : float var_azi;
3239 : int16_t idx_sub_cb;
3240 : float dct_coh[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES];
3241 : int16_t MASA_grouping[MASA_MAXIMUM_CODING_SUBBANDS];
3242 : IVAS_QDIRECTION *q_direction;
3243 : int16_t coding_subbands, coding_subbands_0, d, two_dir_band[MASA_MAXIMUM_CODING_SUBBANDS];
3244 : int16_t min_index;
3245 :
3246 1256460 : coding_subbands_0 = hQMetaData->q_direction[0].cfg.nbands;
3247 1256460 : coding_subbands = hQMetaData->q_direction[idx_d].cfg.nbands;
3248 1256460 : if ( coding_subbands_0 <= 5 )
3249 : {
3250 5460768 : for ( j = 0; j < 5; j++ )
3251 : {
3252 4550640 : MASA_grouping[j] = j;
3253 : }
3254 : }
3255 : else
3256 : {
3257 346332 : if ( coding_subbands_0 <= 8 )
3258 : {
3259 94779 : mvs2s( MASA_grouping_8_to_5, MASA_grouping, 8 );
3260 : }
3261 251553 : else if ( coding_subbands_0 <= 12 )
3262 : {
3263 70380 : mvs2s( MASA_grouping_12_to_5, MASA_grouping, 12 );
3264 : }
3265 181173 : else if ( coding_subbands_0 <= 18 )
3266 : {
3267 119151 : mvs2s( MASA_grouping_18_to_5, MASA_grouping, 18 );
3268 : }
3269 : else
3270 : {
3271 62022 : if ( coding_subbands_0 <= 24 )
3272 : {
3273 62022 : mvs2s( MASA_grouping_24_to_5, MASA_grouping, 24 );
3274 : }
3275 : }
3276 : }
3277 :
3278 1256460 : if ( coding_subbands < coding_subbands_0 )
3279 : {
3280 248145 : d = 0;
3281 2219481 : for ( j = 0; j < coding_subbands_0; j++ )
3282 : {
3283 1971336 : if ( hQMetaData->twoDirBands[j] == 1 )
3284 : {
3285 709203 : two_dir_band[d++] = j;
3286 : }
3287 : }
3288 : }
3289 : else
3290 : {
3291 1008315 : set_s( two_dir_band, 0, coding_subbands );
3292 : }
3293 :
3294 1256460 : q_direction = &hQMetaData->q_direction[idx_d];
3295 :
3296 9615717 : for ( i = 0; i < coding_subbands; i++ )
3297 : {
3298 8359257 : var_azi = var( q_direction->band_data[i].azimuth, no_frames );
3299 :
3300 8359257 : if ( hrmasa_flag )
3301 : {
3302 0 : minimum_s( (int16_t *) ( q_direction->band_data[i].energy_ratio_index ), q_direction->cfg.nblocks, &min_index );
3303 0 : min_index = min_index >> 1;
3304 : }
3305 : else
3306 : {
3307 8359257 : min_index = q_direction->band_data[i].energy_ratio_index[0];
3308 : }
3309 :
3310 8359257 : if ( var_azi < MASA_DELTA_AZI_DCT0 )
3311 : {
3312 3582186 : idx_sub_cb = MASA_NO_CV_COH * min_index;
3313 : }
3314 : else
3315 : {
3316 4777071 : idx_sub_cb = MASA_NO_CV_COH * ( min_index + DIRAC_DIFFUSE_LEVELS ); /* NO_CV_COH = 8 */
3317 : }
3318 :
3319 8359257 : dct_coh[i][0] = coherence_cb0_masa[idx_sub_cb + q_direction->coherence_band_data[i].spread_coherence_dct0_index];
3320 :
3321 8359257 : if ( coding_subbands < coding_subbands_0 )
3322 : {
3323 709203 : assert( idx_d == 1 );
3324 709203 : dct_coh[i][1] = coherence_cb1_masa[MASA_grouping[two_dir_band[i]] * MASA_NO_CV_COH1 + q_direction->coherence_band_data[i].spread_coherence_dct1_index];
3325 : }
3326 : else
3327 : {
3328 7650054 : dct_coh[i][1] = coherence_cb1_masa[MASA_grouping[i] * MASA_NO_CV_COH1 + q_direction->coherence_band_data[i].spread_coherence_dct1_index];
3329 : }
3330 :
3331 25077771 : for ( j = 2; j < MAX_PARAM_SPATIAL_SUBFRAMES; j++ )
3332 : {
3333 16718514 : dct_coh[i][j] = 0;
3334 : }
3335 :
3336 8359257 : invdct4_transform( dct_coh[i], q_direction->coherence_band_data[i].spread_coherence );
3337 : }
3338 :
3339 1256460 : return;
3340 : }
3341 :
3342 :
3343 : /*-------------------------------------------------------------------*
3344 : * read_huf()
3345 : *
3346 : * Read Hufman code
3347 : *-------------------------------------------------------------------*/
3348 :
3349 : /*! r: number of bits read */
3350 1256460 : static ivas_error read_huf(
3351 : int16_t *num_bits_read,
3352 : const uint16_t *bitstream, /* i : bitstream to be read */
3353 : uint16_t *out, /* o : decoded value */
3354 : const int16_t start_pos, /* i : starting position for reading */
3355 : const int16_t len, /* i : number of codewords */
3356 : const int16_t *huff_code, /* i : Huffman table */
3357 : const int16_t max_len /* i : maximum codeword length */
3358 : )
3359 : {
3360 1256460 : int16_t done = 0, end_pos;
3361 : uint16_t ByteBuffer;
3362 : int16_t nbits, val;
3363 : uint16_t i;
3364 :
3365 1256460 : end_pos = start_pos;
3366 1256460 : nbits = 0;
3367 1256460 : val = 0;
3368 5958771 : while ( ( done == 0 ) && ( nbits < max_len ) )
3369 : {
3370 4702311 : ByteBuffer = bitstream[end_pos--];
3371 4702311 : val = val * 2 + ( ByteBuffer & 1 );
3372 4702311 : nbits += 1;
3373 22111899 : for ( i = 0; i < len; i++ )
3374 : {
3375 18666048 : if ( val == huff_code[i] )
3376 : {
3377 1256460 : *out = i;
3378 1256460 : done = 1;
3379 1256460 : break;
3380 : }
3381 : }
3382 : }
3383 :
3384 : #ifdef DEBUGGING
3385 : if ( nbits == max_len )
3386 : {
3387 : return IVAS_ERROR( IVAS_ERR_INTERNAL_FATAL, "Wrong Huffman codeword for average index in coherence encoding." );
3388 : }
3389 : #endif
3390 1256460 : *num_bits_read = end_pos;
3391 :
3392 1256460 : return IVAS_ERR_OK;
3393 : }
3394 :
3395 :
3396 : /*-------------------------------------------------------------------*
3397 : * read_GR_min_removed_data()
3398 : *
3399 : *
3400 : *-------------------------------------------------------------------*/
3401 :
3402 345528 : static int16_t read_GR_min_removed_data(
3403 : uint16_t *bitstream, /* i : bitstream */
3404 : int16_t *p_bit_pos, /* i : position in the bitstream */
3405 : const int16_t *no_cv_vec,
3406 : const int16_t no_data,
3407 : int16_t *decoded_idx,
3408 : const int16_t no_symb )
3409 : {
3410 : int16_t j;
3411 : int16_t bit_pos;
3412 : int16_t nbits, bits_GR;
3413 : uint16_t byteBuffer;
3414 : int16_t min_index;
3415 :
3416 345528 : bit_pos = *p_bit_pos;
3417 :
3418 : /* read GR order */
3419 345528 : byteBuffer = bitstream[bit_pos--];
3420 345528 : nbits = 1;
3421 :
3422 : /* read min index */
3423 345528 : bits_GR = bit_pos;
3424 345528 : min_index = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_symb, 0 );
3425 345528 : nbits += bits_GR - bit_pos;
3426 :
3427 : /* read GR data */
3428 5069091 : for ( j = 0; j < no_data; j++ )
3429 : {
3430 4723563 : bits_GR = bit_pos;
3431 4723563 : if ( no_cv_vec[j] > 1 )
3432 : {
3433 4707633 : decoded_idx[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_cv_vec[j] - min_index, ( byteBuffer & 1 ) );
3434 4707633 : nbits += bits_GR - bit_pos;
3435 : }
3436 : else
3437 : {
3438 15930 : decoded_idx[j] = 0;
3439 : }
3440 : }
3441 :
3442 5069091 : for ( j = 0; j < no_data; j++ )
3443 : {
3444 4723563 : if ( no_cv_vec[j] > 1 )
3445 : {
3446 4707633 : decoded_idx[j] += min_index;
3447 : }
3448 : }
3449 :
3450 : #ifdef DEBUGGING
3451 : assert( nbits == *p_bit_pos - bit_pos );
3452 : #endif
3453 345528 : *p_bit_pos = bit_pos;
3454 :
3455 345528 : return nbits;
3456 : }
3457 :
3458 :
3459 : /*-------------------------------------------------------------------*
3460 : * decode_fixed_rate_composed_index_coherence()
3461 : *
3462 : *
3463 : *-------------------------------------------------------------------*/
3464 :
3465 798879 : static int16_t decode_fixed_rate_composed_index_coherence(
3466 : uint16_t *bitstream, /* i : bitstream */
3467 : int16_t *p_bit_pos, /* i : position in the bitstream */
3468 : const int16_t no_bands,
3469 : int16_t *no_cv_vec,
3470 : uint16_t *decoded_index,
3471 : const int16_t no_symb )
3472 : {
3473 : /* fixed rate */
3474 : uint64_t no_cb;
3475 : uint16_t temp_index[MASA_MAXIMUM_CODING_SUBBANDS];
3476 : uint64_t idx_fr;
3477 : int16_t no_bits_vec1, half_no_bands;
3478 : int16_t bit_pos;
3479 : int16_t nbits, bits_GR;
3480 : int16_t j;
3481 : int16_t no_vals_local;
3482 : int16_t no_bits_vec;
3483 :
3484 798879 : bit_pos = *p_bit_pos;
3485 798879 : set_s( (int16_t *) temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
3486 :
3487 798879 : no_cb = 1;
3488 798879 : nbits = 0;
3489 798879 : if ( no_bands > MASA_LIMIT_NO_BANDS_SUR_COH )
3490 : {
3491 : /* read 8-max_val with GR0 */
3492 346983 : bits_GR = bit_pos;
3493 346983 : no_vals_local = no_symb - ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_symb, 0 );
3494 346983 : nbits += bits_GR - bit_pos;
3495 6580518 : for ( j = 0; j < no_bands; j++ )
3496 : {
3497 6233535 : if ( no_cv_vec[j] > no_vals_local )
3498 : {
3499 4485969 : no_cv_vec[j] = no_vals_local;
3500 : }
3501 : }
3502 : }
3503 :
3504 798879 : half_no_bands = no_bands / 2;
3505 798879 : if ( sum_s( no_cv_vec, no_bands ) > MASA_COH_LIMIT_2IDX )
3506 : {
3507 75993 : no_cb = 1;
3508 :
3509 911682 : for ( j = 0; j < half_no_bands; j++ )
3510 : {
3511 835689 : no_cb *= no_cv_vec[j];
3512 : }
3513 75993 : no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
3514 75993 : no_cb = 1;
3515 914217 : for ( j = half_no_bands; j < no_bands; j++ )
3516 : {
3517 838224 : no_cb *= no_cv_vec[j];
3518 : }
3519 75993 : no_bits_vec1 = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
3520 : }
3521 : else
3522 : {
3523 722886 : no_cb = 1;
3524 7280877 : for ( j = 0; j < no_bands; j++ )
3525 : {
3526 6557991 : no_cb *= no_cv_vec[j];
3527 : }
3528 722886 : no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
3529 722886 : no_bits_vec1 = 0;
3530 : }
3531 798879 : if ( no_bits_vec1 > 0 )
3532 : {
3533 75978 : idx_fr = 0;
3534 2361381 : for ( j = 0; j < no_bits_vec; j++ )
3535 : {
3536 2285403 : idx_fr = ( idx_fr << 1 ) + bitstream[bit_pos--];
3537 : }
3538 :
3539 75978 : nbits += no_bits_vec;
3540 :
3541 75978 : decode_combined_index( idx_fr, no_cv_vec, temp_index, half_no_bands );
3542 :
3543 75978 : idx_fr = 0;
3544 2320746 : for ( j = 0; j < no_bits_vec1; j++ )
3545 : {
3546 2244768 : idx_fr = ( idx_fr << 1 ) + bitstream[bit_pos--];
3547 : }
3548 75978 : nbits += no_bits_vec1;
3549 75978 : decode_combined_index( idx_fr, &no_cv_vec[half_no_bands], &temp_index[half_no_bands], half_no_bands );
3550 : }
3551 : else
3552 : {
3553 722901 : idx_fr = 0;
3554 7396614 : for ( j = 0; j < no_bits_vec; j++ )
3555 : {
3556 6673713 : idx_fr = ( idx_fr << 1 ) + bitstream[bit_pos--];
3557 : }
3558 722901 : nbits += no_bits_vec;
3559 722901 : decode_combined_index( idx_fr, no_cv_vec, temp_index, no_bands );
3560 : }
3561 :
3562 9030783 : for ( j = 0; j < no_bands; j++ )
3563 : {
3564 8231904 : decoded_index[j] = temp_index[j];
3565 : }
3566 : #ifdef DEBUGGING
3567 : assert( nbits == *p_bit_pos - bit_pos );
3568 : #endif
3569 798879 : nbits = *p_bit_pos - bit_pos;
3570 :
3571 798879 : *p_bit_pos = bit_pos;
3572 :
3573 798879 : return nbits;
3574 : }
3575 :
3576 :
3577 : /*-------------------------------------------------------------------*
3578 : * read_coherence_data_hr_512()
3579 : *
3580 : * Read coherence data at HR
3581 : *-------------------------------------------------------------------*/
3582 :
3583 : /*! r: number of bits read */
3584 208473 : static int16_t read_coherence_data_hr_512(
3585 : uint16_t *bitstream, /* i : bitstream */
3586 : int16_t *p_bit_pos, /* i : position in the bitstream */
3587 : IVAS_QMETADATA *hQMetaData, /* i/o: quantized metadata structure */
3588 : const int16_t idx_dir, /* i : direction index */
3589 : const int16_t nbits_coh )
3590 : {
3591 : int16_t j, k, i;
3592 : int16_t nbands, nblocks;
3593 : int16_t min_index, GR_param;
3594 : int16_t cb_size, nbits;
3595 : int16_t decoded_idx;
3596 : float delta;
3597 :
3598 208473 : nbands = hQMetaData->q_direction[idx_dir].cfg.nbands;
3599 208473 : nblocks = hQMetaData->q_direction[idx_dir].cfg.nblocks;
3600 :
3601 208473 : cb_size = 1 << nbits_coh;
3602 208473 : delta = 256.0f / cb_size;
3603 208473 : nbits = *p_bit_pos;
3604 866424 : for ( k = 0; k < nblocks; k++ )
3605 : {
3606 : /* read method */
3607 657951 : if ( bitstream[( *p_bit_pos )--] == 1 )
3608 : {
3609 : /* average removed */
3610 : /* read average index */
3611 126588 : min_index = 0;
3612 562203 : for ( i = 0; i < nbits_coh; i++ )
3613 : {
3614 435615 : min_index = ( min_index << 1 ) + bitstream[( *p_bit_pos )--];
3615 : }
3616 : /* read GR param */
3617 126588 : GR_param = bitstream[( *p_bit_pos )--];
3618 2841543 : for ( j = 0; j < nbands; j++ )
3619 : {
3620 2714955 : decoded_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, p_bit_pos, 2 * cb_size, GR_param );
3621 2714955 : if ( decoded_idx % 2 )
3622 : {
3623 682701 : decoded_idx = ( ( decoded_idx + 1 ) >> 1 ) + min_index;
3624 : }
3625 : else
3626 : {
3627 2032254 : decoded_idx = -( decoded_idx >> 1 ) + min_index;
3628 : }
3629 2714955 : hQMetaData->q_direction[idx_dir].coherence_band_data[j].spread_coherence[k] = (uint8_t) ( decoded_idx * delta + delta / 2.0f );
3630 : }
3631 : }
3632 : else
3633 : {
3634 : /* read min_index */
3635 531363 : min_index = 0;
3636 2324256 : for ( i = 0; i < nbits_coh; i++ )
3637 : {
3638 1792893 : min_index = ( min_index << 1 ) + bitstream[( *p_bit_pos )--];
3639 : }
3640 :
3641 : /* read GR param */
3642 531363 : GR_param = bitstream[( *p_bit_pos )--];
3643 11225880 : for ( j = 0; j < nbands; j++ )
3644 : {
3645 10694517 : decoded_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, p_bit_pos, cb_size - min_index, GR_param ) + min_index;
3646 10694517 : hQMetaData->q_direction[idx_dir].coherence_band_data[j].spread_coherence[k] = (uint8_t) ( decoded_idx * delta + delta / 2.0f );
3647 : }
3648 : }
3649 : }
3650 :
3651 208473 : nbits = nbits - *p_bit_pos;
3652 :
3653 208473 : return nbits;
3654 : }
3655 :
3656 :
3657 : /*------------------------------------------------------------------- *
3658 : * read_coherence_data()
3659 : *
3660 : * Read coherence data
3661 : *------------------------------------------------------------------- */
3662 :
3663 : /*! r: number of bits read */
3664 1903677 : static int16_t read_coherence_data(
3665 : uint16_t *bitstream, /* i : bitstream */
3666 : int16_t *p_bit_pos, /* i : position in the bitstream */
3667 : IVAS_QMETADATA *hQMetaData, /* i/o: quantized metadata structure */
3668 : const int16_t idx_dir, /* i : direction index */
3669 : const int16_t hrmasa_flag /* i : flag indicating high-rate MASA MD coding*/
3670 : )
3671 : {
3672 : int16_t j;
3673 : int16_t no_cv_vec[MASA_MAXIMUM_CODING_SUBBANDS];
3674 : uint64_t no_cb;
3675 : int16_t no_bits_vec, nbits;
3676 : int16_t bits_GR;
3677 : uint16_t idx_dct1[MASA_MAXIMUM_CODING_SUBBANDS];
3678 : uint16_t av_index;
3679 : int16_t no_bits_vec1;
3680 : int16_t bit_pos;
3681 : IVAS_QDIRECTION *q_direction;
3682 : int16_t coding_subbands;
3683 : uint64_t dct0_index;
3684 : int16_t decoded_idx[MASA_MAXIMUM_CODING_SUBBANDS];
3685 : uint16_t byteBuffer;
3686 : int16_t idx_ER;
3687 : int16_t min_index;
3688 : int16_t extra_cv;
3689 :
3690 1903677 : coding_subbands = hQMetaData->q_direction[idx_dir].cfg.nbands;
3691 1903677 : extra_cv = (int16_t) ( coding_subbands / MASA_FACTOR_CV_COH );
3692 1903677 : q_direction = &( hQMetaData->q_direction[idx_dir] );
3693 1903677 : bit_pos = *p_bit_pos;
3694 1903677 : nbits = 0;
3695 :
3696 1903677 : if ( q_direction->cfg.nblocks == 1 )
3697 : {
3698 7705062 : for ( j = 0; j < coding_subbands; j++ )
3699 : {
3700 7057845 : if ( hrmasa_flag )
3701 : {
3702 0 : idx_ER = 7 - ( q_direction->band_data[j].energy_ratio_index_mod[0] >> 1 ) + extra_cv;
3703 : }
3704 : else
3705 : {
3706 7057845 : idx_ER = 7 - q_direction->band_data[j].energy_ratio_index_mod[0] + extra_cv;
3707 : }
3708 7057845 : no_cv_vec[j] = idx_ER + 1;
3709 : }
3710 :
3711 647217 : if ( sum_s( no_cv_vec, coding_subbands ) == coding_subbands )
3712 : {
3713 100161 : for ( j = 0; j < coding_subbands; j++ )
3714 : {
3715 80310 : q_direction->coherence_band_data[j].spread_coherence[0] = 0;
3716 : }
3717 :
3718 19851 : return 0;
3719 : }
3720 627366 : byteBuffer = bitstream[bit_pos--];
3721 627366 : nbits += 1;
3722 :
3723 627366 : if ( byteBuffer & 1 )
3724 : {
3725 : /* decode GR min removed */
3726 345528 : nbits += read_GR_min_removed_data( bitstream, &bit_pos, no_cv_vec, coding_subbands, decoded_idx, MASA_MAX_NO_CV_SUR_COH + extra_cv );
3727 5069091 : for ( j = 0; j < coding_subbands; j++ )
3728 : {
3729 4723563 : if ( no_cv_vec[j] > 1 )
3730 : {
3731 4707633 : q_direction->coherence_band_data[j].spread_coherence[0] = (uint8_t) roundf( decoded_idx[j] * ( 255.0f / (float) ( 7 - ( q_direction->band_data[j].energy_ratio_index_mod[0] >> ( hrmasa_flag ) ) + coding_subbands / MASA_FACTOR_CV_COH ) ) );
3732 : }
3733 : else
3734 : {
3735 15930 : q_direction->coherence_band_data[j].spread_coherence[0] = 0;
3736 : }
3737 : }
3738 : }
3739 : else
3740 : {
3741 : uint16_t decoded_index[MASA_MAXIMUM_CODING_SUBBANDS];
3742 : /* decode joint index */
3743 281838 : nbits += decode_fixed_rate_composed_index_coherence( bitstream, &bit_pos, coding_subbands, no_cv_vec, decoded_index, MASA_NO_CV_COH + coding_subbands / MASA_FACTOR_CV_COH );
3744 2535810 : for ( j = 0; j < coding_subbands; j++ )
3745 : {
3746 2253972 : if ( no_cv_vec[j] > 1 )
3747 : {
3748 758502 : q_direction->coherence_band_data[j].spread_coherence[0] = (uint8_t) roundf( decoded_index[j] * ( 255.0f / (float) ( 7 - ( q_direction->band_data[j].energy_ratio_index_mod[0] >> ( hrmasa_flag ) ) + coding_subbands / MASA_FACTOR_CV_COH ) ) );
3749 : }
3750 : else
3751 : {
3752 1495470 : q_direction->coherence_band_data[j].spread_coherence[0] = 0;
3753 : }
3754 : }
3755 : }
3756 : }
3757 : else
3758 : {
3759 9615717 : for ( j = 0; j < coding_subbands; j++ )
3760 : {
3761 8359257 : if ( hrmasa_flag )
3762 : {
3763 0 : minimum_s( (int16_t *) ( q_direction->band_data[j].energy_ratio_index ), q_direction->cfg.nblocks, &min_index );
3764 0 : no_cv_vec[j] = len_cb_dct0_masa[min_index >> 1]; /* spread coherence DCT0*/
3765 : }
3766 : else
3767 : {
3768 8359257 : no_cv_vec[j] = len_cb_dct0_masa[q_direction->band_data[j].energy_ratio_index[0]]; /* spread coherence DCT0*/
3769 : }
3770 : }
3771 :
3772 1256460 : if ( sum_s( no_cv_vec, coding_subbands ) > MASA_COH_LIMIT_2IDX )
3773 : {
3774 : uint16_t spr_coh_temp_index[MASA_MAXIMUM_CODING_SUBBANDS];
3775 :
3776 28629 : no_cb = 1;
3777 : #ifdef DEBUGGING
3778 : assert( coding_subbands % 2 == 0 );
3779 : #endif
3780 338409 : for ( j = 0; j < coding_subbands / 2; j++ )
3781 : {
3782 309780 : no_cb *= no_cv_vec[j];
3783 : }
3784 :
3785 28629 : no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
3786 28629 : no_cb = 1;
3787 :
3788 338409 : for ( j = coding_subbands / 2; j < coding_subbands; j++ )
3789 : {
3790 309780 : no_cb *= no_cv_vec[j];
3791 : }
3792 :
3793 28629 : no_bits_vec1 = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
3794 28629 : dct0_index = 0;
3795 :
3796 806655 : for ( j = 0; j < no_bits_vec; j++ )
3797 : {
3798 778026 : dct0_index = ( dct0_index << 1 ) + bitstream[bit_pos--];
3799 : }
3800 :
3801 28629 : nbits += no_bits_vec;
3802 28629 : set_s( (int16_t *) spr_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
3803 :
3804 28629 : decode_combined_index( dct0_index, no_cv_vec, spr_coh_temp_index, coding_subbands / 2 );
3805 :
3806 28629 : dct0_index = 0;
3807 834540 : for ( j = 0; j < no_bits_vec1; j++ )
3808 : {
3809 805911 : dct0_index = ( dct0_index << 1 ) + bitstream[bit_pos--];
3810 : }
3811 :
3812 28629 : nbits += no_bits_vec1;
3813 :
3814 28629 : decode_combined_index( dct0_index, &no_cv_vec[coding_subbands / 2], &spr_coh_temp_index[coding_subbands / 2], coding_subbands / 2 );
3815 :
3816 648189 : for ( j = 0; j < coding_subbands; j++ )
3817 : {
3818 619560 : q_direction->coherence_band_data[j].spread_coherence_dct0_index = spr_coh_temp_index[j];
3819 : }
3820 : }
3821 : else
3822 : {
3823 : /* spread coherence */
3824 : uint16_t spr_coh_temp_index[MASA_MAXIMUM_CODING_SUBBANDS];
3825 :
3826 1227831 : no_cb = 1;
3827 :
3828 8967528 : for ( j = 0; j < coding_subbands; j++ )
3829 : {
3830 7739697 : no_cb *= no_cv_vec[j];
3831 : }
3832 :
3833 1227831 : no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
3834 :
3835 : /* read joint index for DCT0 */
3836 1227831 : no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
3837 1227831 : dct0_index = 0;
3838 :
3839 14621676 : for ( j = 0; j < no_bits_vec; j++ )
3840 : {
3841 13393845 : dct0_index = ( dct0_index << 1 ) + bitstream[bit_pos--];
3842 : }
3843 :
3844 1227831 : nbits += no_bits_vec;
3845 :
3846 1227831 : set_s( (int16_t *) spr_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
3847 :
3848 1227831 : decode_combined_index( dct0_index, no_cv_vec, spr_coh_temp_index, coding_subbands );
3849 :
3850 8967528 : for ( j = 0; j < coding_subbands; j++ )
3851 : {
3852 7739697 : q_direction->coherence_band_data[j].spread_coherence_dct0_index = spr_coh_temp_index[j];
3853 : }
3854 : }
3855 :
3856 : /* read GR data for DCT1 */
3857 9615717 : for ( j = 0; j < coding_subbands; j++ )
3858 : {
3859 8359257 : bits_GR = bit_pos;
3860 8359257 : idx_dct1[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, 2 * MASA_NO_CV_COH1, 0 );
3861 8359257 : nbits += bits_GR - bit_pos;
3862 : }
3863 1256460 : bits_GR = bit_pos; /* just to store the data */
3864 :
3865 : /* read average index */
3866 1256460 : read_huf( &bit_pos, bitstream, &av_index, bit_pos, MASA_NO_CV_COH1, huff_code_av_masa, 10 ); /* 10 is MAX_LEN*/
3867 1256460 : nbits += ( bits_GR - bit_pos );
3868 :
3869 : /* write indexes in metadata structure */
3870 9615717 : for ( j = 0; j < coding_subbands; j++ )
3871 : {
3872 8359257 : if ( idx_dct1[j] % 2 )
3873 : {
3874 617316 : q_direction->coherence_band_data[j].spread_coherence_dct1_index = ( idx_dct1[j] + 1 ) / 2 + av_index;
3875 : }
3876 : else
3877 : {
3878 7741941 : q_direction->coherence_band_data[j].spread_coherence_dct1_index = -idx_dct1[j] / 2 + av_index;
3879 : }
3880 : }
3881 : }
3882 : #ifdef DEBUGGING
3883 : assert( nbits == *p_bit_pos - bit_pos );
3884 : #endif
3885 1883826 : nbits = *p_bit_pos - bit_pos;
3886 :
3887 1883826 : *p_bit_pos = bit_pos;
3888 :
3889 1883826 : return nbits;
3890 : }
3891 :
3892 :
3893 : /*-------------------------------------------------------------------*
3894 : * read_surround_coherence()
3895 : *
3896 : *
3897 : *-------------------------------------------------------------------*/
3898 :
3899 1488066 : static int16_t read_surround_coherence(
3900 : uint16_t *bitstream, /* i : bitstream */
3901 : int16_t *p_bit_pos, /* i : position in the bitstream */
3902 : IVAS_QMETADATA *hQMetaData /* i/o: quantized metadata structure */
3903 : )
3904 : {
3905 : int16_t coding_subbands;
3906 : int16_t no_cv_vec[MASA_MAXIMUM_CODING_SUBBANDS];
3907 : int16_t bit_pos;
3908 : float error_ratio_surr;
3909 : int16_t idx_ER[MASA_MAXIMUM_CODING_SUBBANDS];
3910 : int16_t bits_sur_coherence, bits_GR;
3911 : int16_t j, d, k, idx;
3912 : uint16_t byteBuffer;
3913 : uint16_t idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS];
3914 : IVAS_QDIRECTION *q_direction;
3915 : int16_t min_index;
3916 :
3917 1488066 : coding_subbands = hQMetaData->q_direction[0].cfg.nbands;
3918 1488066 : q_direction = hQMetaData->q_direction;
3919 :
3920 1488066 : bits_sur_coherence = 0;
3921 1488066 : bit_pos = *p_bit_pos;
3922 :
3923 1488066 : d = 0;
3924 15441114 : for ( j = 0; j < coding_subbands; j++ )
3925 : {
3926 13953048 : error_ratio_surr = 1.0f;
3927 :
3928 13953048 : if ( hQMetaData->no_directions == 2 )
3929 : {
3930 4014030 : d += hQMetaData->twoDirBands[j];
3931 4014030 : idx = max( d - 1, 0 );
3932 4014030 : error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[0] - q_direction[1].band_data[idx].energy_ratio[0] * hQMetaData->twoDirBands[j];
3933 : }
3934 : else
3935 : {
3936 9939018 : error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[0];
3937 : }
3938 :
3939 13953048 : if ( error_ratio_surr <= 0 )
3940 : {
3941 1126341 : error_ratio_surr = 0;
3942 1126341 : no_cv_vec[j] = 1;
3943 1126341 : idx_ER[j] = masa_sq( 0.0f, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
3944 : }
3945 : else
3946 : {
3947 12826707 : idx_ER[j] = masa_sq( error_ratio_surr, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
3948 12826707 : no_cv_vec[j] = idx_cb_sur_coh_masa[idx_ER[j]] + 2;
3949 : }
3950 : }
3951 :
3952 1488066 : if ( sum_s( no_cv_vec, coding_subbands ) == coding_subbands )
3953 : {
3954 : /* surround coherence is zero */
3955 482736 : for ( j = 0; j < coding_subbands; j++ )
3956 : {
3957 2239545 : for ( k = 0; k < MAX_PARAM_SPATIAL_SUBFRAMES; k++ )
3958 : {
3959 1791636 : if ( hQMetaData->surcoh_band_data != NULL )
3960 : {
3961 1791636 : hQMetaData->surcoh_band_data[j].surround_coherence[k] = 0;
3962 : }
3963 : }
3964 : }
3965 :
3966 34827 : return bits_sur_coherence;
3967 : }
3968 :
3969 : /* read how the surround coherence is encoded */
3970 1453239 : byteBuffer = bitstream[bit_pos--];
3971 1453239 : bits_sur_coherence += 1;
3972 :
3973 1453239 : if ( byteBuffer & 1 )
3974 : {
3975 : /* GR decoding */
3976 : /* read GR order */
3977 1042254 : byteBuffer = bitstream[bit_pos--];
3978 1042254 : bits_sur_coherence += 1;
3979 :
3980 : /* read min index */
3981 1042254 : bits_GR = bit_pos;
3982 1042254 : min_index = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, MASA_MAX_NO_CV_SUR_COH, 0 );
3983 1042254 : bits_sur_coherence += bits_GR - bit_pos;
3984 :
3985 : /* read GR data */
3986 11079708 : for ( j = 0; j < coding_subbands; j++ )
3987 : {
3988 10037454 : bits_GR = bit_pos;
3989 : /* decoding for min removed */
3990 10037454 : if ( no_cv_vec[j] > 1 )
3991 : {
3992 9505938 : idx_sur_coh[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_cv_vec[j] - min_index, ( byteBuffer & 1 ) );
3993 9505938 : bits_sur_coherence += bits_GR - bit_pos;
3994 : }
3995 : else
3996 : {
3997 531516 : idx_sur_coh[j] = 0;
3998 : }
3999 : }
4000 :
4001 11079708 : for ( j = 0; j < coding_subbands; j++ )
4002 : {
4003 10037454 : if ( no_cv_vec[j] > 1 )
4004 : {
4005 9505938 : hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j] + min_index;
4006 : }
4007 : else
4008 : {
4009 531516 : hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j];
4010 : }
4011 :
4012 10037454 : hQMetaData->surcoh_band_data[j].surround_coherence[0] = sur_coherence_cb_masa[idx_cb_sur_coh_masa[idx_ER[j]] * MASA_MAX_NO_CV_SUR_COH + hQMetaData->surcoh_band_data[j].sur_coherence_index];
4013 : }
4014 : }
4015 : else
4016 : {
4017 : /* fixed rate */
4018 : uint16_t sur_coh_temp_index[MASA_MAXIMUM_CODING_SUBBANDS];
4019 410985 : set_s( (int16_t *) sur_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
4020 :
4021 410985 : decode_fixed_rate_composed_index_coherence( bitstream, &bit_pos, coding_subbands, no_cv_vec, sur_coh_temp_index, MASA_MAX_NO_CV_SUR_COH );
4022 :
4023 3878670 : for ( j = 0; j < coding_subbands; j++ )
4024 : {
4025 3467685 : hQMetaData->surcoh_band_data[j].sur_coherence_index = sur_coh_temp_index[j];
4026 : }
4027 :
4028 : /* deindex surround coherence */
4029 3878670 : for ( j = 0; j < coding_subbands; j++ )
4030 : {
4031 3467685 : if ( no_cv_vec[j] > 1 )
4032 : {
4033 2663550 : hQMetaData->surcoh_band_data[j].surround_coherence[0] = sur_coherence_cb_masa[idx_cb_sur_coh_masa[idx_ER[j]] * MASA_MAX_NO_CV_SUR_COH + hQMetaData->surcoh_band_data[j].sur_coherence_index];
4034 : }
4035 : else
4036 : {
4037 804135 : hQMetaData->surcoh_band_data[j].surround_coherence[0] = 0;
4038 : }
4039 : }
4040 : }
4041 :
4042 14958378 : for ( j = 0; j < coding_subbands; j++ )
4043 : {
4044 54020556 : for ( k = 1; k < MAX_PARAM_SPATIAL_SUBFRAMES; k++ )
4045 : {
4046 40515417 : hQMetaData->surcoh_band_data[j].surround_coherence[k] = hQMetaData->surcoh_band_data[j].surround_coherence[0];
4047 : }
4048 : }
4049 :
4050 : /* Replace return value with the actual read bits. bits_sur_coherence might show wrong count at this point. */
4051 1453239 : bits_sur_coherence = *p_bit_pos - bit_pos;
4052 1453239 : *p_bit_pos = bit_pos;
4053 :
4054 1453239 : return bits_sur_coherence;
4055 : }
4056 :
4057 :
4058 : /*-------------------------------------------------------------------*
4059 : * read_surround_coherence_hr()
4060 : *
4061 : *
4062 : *-------------------------------------------------------------------*/
4063 :
4064 159606 : static int16_t read_surround_coherence_hr(
4065 : uint16_t *bitstream, /* i : bitstream */
4066 : int16_t *p_bit_pos, /* i : position in the bitstream */
4067 : IVAS_QMETADATA *hQMetaData /* i/o: quantized metadata structure */
4068 : )
4069 : {
4070 : int16_t coding_subbands;
4071 : int16_t no_cv_vec[MASA_MAXIMUM_CODING_SUBBANDS];
4072 : int16_t bit_pos;
4073 : float error_ratio_surr;
4074 : int16_t idx_ER[MASA_MAXIMUM_CODING_SUBBANDS];
4075 : int16_t bits_sur_coherence, bits_GR;
4076 : int16_t j, k, sf;
4077 : uint16_t byteBuffer;
4078 : uint16_t idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS];
4079 : IVAS_QDIRECTION *q_direction;
4080 : int16_t min_index;
4081 : int16_t d, idx;
4082 : int32_t int_error_ratio_surr;
4083 :
4084 159606 : coding_subbands = hQMetaData->q_direction[0].cfg.nbands;
4085 159606 : q_direction = hQMetaData->q_direction;
4086 :
4087 159606 : bits_sur_coherence = 0;
4088 159606 : bit_pos = *p_bit_pos;
4089 :
4090 663561 : for ( sf = 0; sf < hQMetaData->q_direction[0].cfg.nblocks; sf++ )
4091 : {
4092 503955 : d = 0;
4093 11801523 : for ( j = 0; j < coding_subbands; j++ )
4094 : {
4095 11297568 : error_ratio_surr = 1.0f;
4096 11297568 : if ( hQMetaData->no_directions == 2 )
4097 : {
4098 3320919 : d += hQMetaData->twoDirBands[j];
4099 3320919 : idx = max( d - 1, 0 );
4100 3320919 : error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[sf] - q_direction[1].band_data[idx].energy_ratio[sf] * hQMetaData->twoDirBands[j];
4101 : }
4102 : else
4103 : {
4104 7976649 : error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[sf];
4105 : }
4106 :
4107 11297568 : int_error_ratio_surr = (int32_t) ( MASA_SUR_COH_PRECISION * error_ratio_surr );
4108 11297568 : error_ratio_surr = (float) ( int_error_ratio_surr * MASA_SUR_COH_THRESHOLD );
4109 :
4110 11297568 : if ( error_ratio_surr <= 0 )
4111 : {
4112 2363010 : error_ratio_surr = 0;
4113 2363010 : no_cv_vec[j] = 1;
4114 2363010 : idx_ER[j] = 0;
4115 : }
4116 : else
4117 : {
4118 8934558 : idx_ER[j] = 7; /* masa_sq( error_ratio_surr, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS ); */
4119 8934558 : no_cv_vec[j] = idx_cb_sur_coh_masa[idx_ER[j]] + 2;
4120 : }
4121 : }
4122 :
4123 503955 : if ( sum_s( no_cv_vec, coding_subbands ) == coding_subbands )
4124 : {
4125 : /* surround coherence is zero */
4126 232347 : for ( j = 0; j < coding_subbands; j++ )
4127 : {
4128 1114665 : for ( k = 0; k < MAX_PARAM_SPATIAL_SUBFRAMES; k++ )
4129 : {
4130 891732 : if ( hQMetaData->surcoh_band_data != NULL )
4131 : {
4132 891732 : hQMetaData->surcoh_band_data[j].surround_coherence[k] = 0;
4133 : }
4134 : }
4135 : }
4136 : }
4137 : else
4138 : {
4139 : /* read how the surround coherence is encoded */
4140 494541 : byteBuffer = bitstream[bit_pos--];
4141 494541 : bits_sur_coherence += 1;
4142 :
4143 494541 : if ( byteBuffer & 1 )
4144 : {
4145 : /* GR decoding */
4146 : /* read GR order */
4147 388485 : byteBuffer = bitstream[bit_pos--];
4148 388485 : bits_sur_coherence += 1;
4149 :
4150 : /* read min index */
4151 388485 : bits_GR = bit_pos;
4152 388485 : min_index = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, MASA_MAX_NO_CV_SUR_COH, 0 );
4153 388485 : bits_sur_coherence += bits_GR - bit_pos;
4154 :
4155 : /* read GR data */
4156 8952873 : for ( j = 0; j < coding_subbands; j++ )
4157 : {
4158 8564388 : bits_GR = bit_pos;
4159 : /* decoding for min removed */
4160 8564388 : if ( no_cv_vec[j] > 1 )
4161 : {
4162 6925449 : idx_sur_coh[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_cv_vec[j] - min_index, ( byteBuffer & 1 ) );
4163 6925449 : bits_sur_coherence += bits_GR - bit_pos;
4164 : }
4165 : else
4166 : {
4167 1638939 : idx_sur_coh[j] = 0;
4168 : }
4169 : }
4170 :
4171 8952873 : for ( j = 0; j < coding_subbands; j++ )
4172 : {
4173 8564388 : if ( no_cv_vec[j] > 1 )
4174 : {
4175 6925449 : hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j] + min_index;
4176 6925449 : hQMetaData->surcoh_band_data[j].surround_coherence[sf] = sur_coherence_cb_masa[idx_cb_sur_coh_masa[idx_ER[j]] * MASA_MAX_NO_CV_SUR_COH + hQMetaData->surcoh_band_data[j].sur_coherence_index];
4177 : }
4178 : else
4179 : {
4180 1638939 : hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j];
4181 1638939 : hQMetaData->surcoh_band_data[j].surround_coherence[sf] = 0;
4182 : }
4183 : }
4184 : }
4185 : else
4186 : {
4187 : /* fixed rate */
4188 : uint16_t sur_coh_temp_index[MASA_MAXIMUM_CODING_SUBBANDS];
4189 106056 : set_s( (int16_t *) sur_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
4190 :
4191 106056 : decode_fixed_rate_composed_index_coherence( bitstream, &bit_pos, coding_subbands, no_cv_vec, sur_coh_temp_index, MASA_MAX_NO_CV_SUR_COH );
4192 :
4193 2616303 : for ( j = 0; j < coding_subbands; j++ )
4194 : {
4195 2510247 : hQMetaData->surcoh_band_data[j].sur_coherence_index = sur_coh_temp_index[j];
4196 : }
4197 :
4198 : /* deindex surround coherence */
4199 2616303 : for ( j = 0; j < coding_subbands; j++ )
4200 : {
4201 2510247 : if ( no_cv_vec[j] > 1 )
4202 : {
4203 1267272 : hQMetaData->surcoh_band_data[j].surround_coherence[sf] = sur_coherence_cb_masa[idx_cb_sur_coh_masa[idx_ER[j]] * MASA_MAX_NO_CV_SUR_COH + hQMetaData->surcoh_band_data[j].sur_coherence_index];
4204 : }
4205 : else
4206 : {
4207 1242975 : hQMetaData->surcoh_band_data[j].surround_coherence[sf] = 0;
4208 : }
4209 : }
4210 : }
4211 : }
4212 : }
4213 :
4214 : /* Replace return value with the actual read bits. bits_sur_coherence might show wrong count at this point. */
4215 159606 : bits_sur_coherence = *p_bit_pos - bit_pos;
4216 159606 : *p_bit_pos = bit_pos;
4217 :
4218 159606 : return bits_sur_coherence;
4219 : }
4220 :
4221 :
4222 : /*-------------------------------------------------------------------*
4223 : * decode_combined_index()
4224 : *
4225 : * Decode combined index into several indexes
4226 : *-------------------------------------------------------------------*/
4227 :
4228 2159946 : static void decode_combined_index(
4229 : uint64_t comb_index, /* i : index to be decoded */
4230 : const int16_t *no_cv_vec, /* i : number of codewords for each element*/
4231 : uint16_t *index, /* o : decoded indexes */
4232 : const int16_t len /* i : number of elements */
4233 : )
4234 : {
4235 : int16_t i;
4236 : uint64_t base[MASA_MAXIMUM_CODING_SUBBANDS + 1];
4237 :
4238 2159946 : base[0] = 1;
4239 16588626 : for ( i = 1; i < len; i++ )
4240 : {
4241 14428680 : base[i] = base[i - 1] * no_cv_vec[i - 1];
4242 : }
4243 :
4244 16588626 : for ( i = len - 1; i > 0; i-- )
4245 : {
4246 14428680 : index[i] = (uint16_t) ( comb_index / base[i] );
4247 14428680 : comb_index -= index[i] * base[i];
4248 : }
4249 :
4250 2159946 : index[0] = (uint16_t) comb_index;
4251 :
4252 2159946 : return;
4253 : }
4254 :
4255 :
4256 174984 : static void read_stream_dct_coeffs_omasa(
4257 : int16_t *q_idx,
4258 : Word32 *q_dct_data_fx,
4259 : const int16_t len_stream,
4260 : uint16_t *bit_stream,
4261 : int16_t *index,
4262 : const int16_t first_line )
4263 : {
4264 : int16_t sign;
4265 : int16_t i, j, i_min;
4266 : int16_t GR1, GR2;
4267 : int64_t step_fx;
4268 174984 : step_fx = STEP_M2T_FX;
4269 174984 : sign = 1;
4270 174984 : if ( first_line == 0 )
4271 : {
4272 : /* read sign */
4273 0 : sign = bit_stream[( *index )--];
4274 0 : if ( sign == 0 )
4275 : {
4276 0 : sign = -1;
4277 : }
4278 : }
4279 :
4280 174984 : set_s( q_idx, 0, len_stream );
4281 : /* read DCT 0 component */
4282 1224888 : for ( i = 0; i < BITS_MASA2TOTTAL_DCT0; i++ )
4283 : {
4284 1049904 : q_idx[0] = ( q_idx[0] << 1 ) + bit_stream[( *index )--];
4285 : }
4286 174984 : q_idx[0] *= sign;
4287 :
4288 174984 : if ( q_idx[0] != 0 )
4289 : {
4290 140970 : if ( len_stream >= 8 )
4291 : {
4292 : /* read index of last index encoded with GR2 */
4293 116520 : i_min = 0;
4294 116520 : j = 4;
4295 582600 : for ( i = 0; i < j; i++ )
4296 : {
4297 466080 : i_min = ( i_min << 1 ) + bit_stream[( *index )--];
4298 : }
4299 :
4300 : /* read GR orders */
4301 116520 : GR1 = bit_stream[( *index )--] + 1;
4302 116520 : if ( GR1 == 2 )
4303 : {
4304 40863 : GR2 = bit_stream[( *index )--];
4305 : }
4306 : else
4307 : {
4308 75657 : GR2 = 0;
4309 : }
4310 :
4311 : /* read GR data */
4312 446166 : for ( i = 1; i <= i_min; i++ )
4313 : {
4314 329646 : q_idx[i] = ivas_qmetadata_DecodeExtendedGR( bit_stream, index, 100, GR1 );
4315 : }
4316 1598322 : for ( i = i_min + 1; i < len_stream; i++ )
4317 : {
4318 1481802 : q_idx[i] = ivas_qmetadata_DecodeExtendedGR( bit_stream, index, 100, GR2 );
4319 : }
4320 : }
4321 : else
4322 : {
4323 : /* read GR order (only one) */
4324 24450 : GR1 = bit_stream[( *index )--];
4325 121449 : for ( i = 1; i < len_stream; i++ )
4326 : {
4327 96999 : q_idx[i] = ivas_qmetadata_DecodeExtendedGR( bit_stream, index, 100, GR1 );
4328 : }
4329 : }
4330 : }
4331 :
4332 : /* deindex */
4333 174984 : q_dct_data_fx[0] = (Word32) ( ( step_fx * q_idx[0] ) >> 6 ); /* Q25 */
4334 2561223 : for ( i = 1; i < len_stream; i++ )
4335 : {
4336 2386239 : if ( ( q_idx[i] & 1 ) == 0 )
4337 : {
4338 2079708 : q_dct_data_fx[i] = (Word32) ( ( step_fx * ( -q_idx[i] ) ) >> 7 ); /* Q25 */
4339 : }
4340 : ELSE
4341 : {
4342 306531 : q_dct_data_fx[i] = (Word32) ( ( step_fx * ( q_idx[i] + 1 ) ) >> 7 ); /* Q25 */
4343 : }
4344 : }
4345 174984 : return;
4346 : }
4347 :
4348 :
4349 : /*-------------------------------------------------------------------------
4350 : * ivas_omasa_decode_masa_to_total()
4351 : *
4352 : *------------------------------------------------------------------------*/
4353 :
4354 174984 : void ivas_omasa_decode_masa_to_total(
4355 : uint16_t *bit_stream,
4356 : int16_t *index,
4357 : float masa_to_total_energy_ratio[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS],
4358 : const int16_t nbands,
4359 : const int16_t nblocks )
4360 : {
4361 : int16_t i, j, k;
4362 : int16_t q_idx[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
4363 : Word32 q_dct_data_fx[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS],
4364 : dct_data_tmp_fx[MAX_PARAM_SPATIAL_SUBFRAMES * MASA_FREQUENCY_BANDS];
4365 : int16_t n_streams, len_stream;
4366 :
4367 : /* Setup coding parameters */
4368 174984 : n_streams = 1;
4369 174984 : len_stream = nbands * nblocks;
4370 174984 : if ( len_stream == 32 )
4371 : {
4372 0 : n_streams = 4;
4373 0 : len_stream = 8;
4374 : }
4375 :
4376 174984 : set_s( q_idx, 0, nbands * nblocks );
4377 349968 : for ( i = 0; i < n_streams; i++ )
4378 : {
4379 174984 : read_stream_dct_coeffs_omasa( &q_idx[i * len_stream],
4380 174984 : &q_dct_data_fx[i * len_stream],
4381 : len_stream, bit_stream, index, i == 0 );
4382 : }
4383 :
4384 : /* inverse DCT2 transform */
4385 174984 : SWITCH( len_stream )
4386 : {
4387 822 : case 4:
4388 822 : matrix_product_q30_fx( dct4_fx, nblocks, nblocks, 1, q_dct_data_fx, nblocks, 1, 0, dct_data_tmp_fx );
4389 822 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nblocks ); /*Q30*/
4390 822 : BREAK;
4391 27879 : case 5:
4392 27879 : matrix_product_q30_fx( dct5_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
4393 27879 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
4394 27879 : BREAK;
4395 0 : case 8:
4396 0 : matrix_product_q30_fx( dct8_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
4397 0 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
4398 0 : BREAK;
4399 63390 : case 12:
4400 63390 : matrix_product_q30_fx( dct12_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
4401 63390 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
4402 63390 : BREAK;
4403 82893 : case 20:
4404 82893 : matrix_product_fx( dct5_fx, nbands, nbands, 1, q_dct_data_fx, nbands, nblocks, 0, dct_data_tmp_fx );
4405 82893 : matrix_product_q30_fx( dct_data_tmp_fx, nbands, nblocks, 0, dct4_fx, nblocks, nblocks, 0, q_dct_data_fx ); /* reuse of variable*/
4406 82893 : BREAK;
4407 0 : case 32:
4408 0 : matrix_product_fx( dct8_fx, nbands, nbands, 1, q_dct_data_fx, nbands, nblocks, 0, dct_data_tmp_fx );
4409 0 : matrix_product_q30_fx( dct_data_tmp_fx, nbands, nblocks, 0, dct4_fx, nblocks, nblocks, 0, q_dct_data_fx );
4410 0 : BREAK;
4411 0 : default:
4412 0 : printf( "Incorrect number of coefficients for OMASA.\n" );
4413 0 : BREAK;
4414 : }
4415 :
4416 : /* this is to make sure the comparison to the threshold 0.98 will go the same way in */
4417 : /* fixed point and floating point without having to drag the fixed point values to the */
4418 : /* comparison place in the code; 1052266987 is 0.98 in Q30 it is not needed in the fixed point*/
4419 :
4420 2736207 : for ( i = 0; i < nblocks * nbands; i++ )
4421 : {
4422 2561223 : if ( q_dct_data_fx[i] >= 1052266987 && q_dct_data_fx[i] < 1052400000 )
4423 : {
4424 663 : q_dct_data_fx[i] = 1052400000;
4425 : }
4426 : }
4427 :
4428 174984 : k = 0;
4429 601113 : for ( i = 0; i < nblocks; i++ )
4430 : {
4431 2987352 : for ( j = 0; j < nbands; j++ )
4432 : {
4433 2561223 : masa_to_total_energy_ratio[i][j] = q_dct_data_fx[k] / (float) ( 1 << 30 );
4434 2561223 : masa_to_total_energy_ratio[i][j] = max( 0.0f, masa_to_total_energy_ratio[i][j] );
4435 2561223 : masa_to_total_energy_ratio[i][j] = min( 1.0f, masa_to_total_energy_ratio[i][j] );
4436 2561223 : k++;
4437 : }
4438 : }
4439 :
4440 174984 : if ( nblocks == 1 )
4441 : {
4442 365076 : for ( i = 1; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
4443 : {
4444 2974032 : for ( j = 0; j < nbands; j++ )
4445 : {
4446 2700225 : masa_to_total_energy_ratio[i][j] = masa_to_total_energy_ratio[0][j];
4447 : }
4448 : }
4449 : }
4450 :
4451 174984 : if ( nbands == 1 )
4452 : {
4453 4110 : for ( j = 1; j < 5; j++ )
4454 : {
4455 16440 : for ( i = 0; i < nblocks; i++ )
4456 : {
4457 13152 : masa_to_total_energy_ratio[i][j] = masa_to_total_energy_ratio[i][0];
4458 : }
4459 : }
4460 : }
4461 :
4462 174984 : return;
4463 : }
|