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 8043233 : 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 8043233 : ec_flag = 0;
178 8043233 : start_index_0 = *index;
179 :
180 : /*Coherence flag decoding*/
181 8043233 : bits_no_dirs_coh = 0;
182 8043233 : all_coherence_zero = 1;
183 8043233 : if ( hQMetaData->coherence_flag )
184 : {
185 : /* read if coherence is zero */
186 1088493 : all_coherence_zero = bitstream[( *index )--];
187 1088493 : bits_no_dirs_coh += 1;
188 : }
189 :
190 8043233 : hQMetaData->all_coherence_zero = (uint8_t) all_coherence_zero;
191 :
192 8043233 : if ( hQMetaData->no_directions == 2 )
193 : {
194 : /* Read which bands have 2 directions */
195 603976 : hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands;
196 603976 : set_c( (int8_t *) hQMetaData->twoDirBands, 0, hQMetaData->q_direction[0].cfg.nbands );
197 603976 : d = *index;
198 603976 : dif_p[0] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
199 603976 : p[0] = dif_p[0];
200 603976 : hQMetaData->twoDirBands[p[0]] = 1;
201 5500728 : for ( b = 1; b < hQMetaData->numTwoDirBands; b++ )
202 : {
203 4896752 : dif_p[b] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
204 4896752 : p[b] = p[b - 1] + dif_p[b] + 1;
205 4896752 : hQMetaData->twoDirBands[p[b]] = 1;
206 : }
207 603976 : bits_no_dirs_coh += ( d - *index );
208 : }
209 :
210 8043233 : bits_diff_sum = 0;
211 8043233 : bits_diff_sum += ivas_qmetadata_entropy_decode_diffuseness( bitstream, index, &( hQMetaData->q_direction[0] ), &diffuseness_index_max_ec_frame_pre[0] );
212 :
213 8043233 : if ( hodirac_flag )
214 : {
215 453800 : if ( hQMetaData->no_directions == 2 )
216 : {
217 : /* Calculate bits for dfRatio */
218 453800 : dir2band = 0;
219 5445600 : for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
220 : {
221 4991800 : if ( hQMetaData->twoDirBands[b] == 1 )
222 : {
223 4991800 : dfRatio_bits[dir2band] = ivas_get_df_ratio_bits_hodirac( hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] );
224 4991800 : dir2band++;
225 : }
226 : }
227 :
228 453800 : bits_diff_sum += ivas_qmetadata_entropy_decode_df_ratio( bitstream, index, &( hQMetaData->q_direction[1] ), dfRatio_bits );
229 : }
230 : }
231 : else
232 : {
233 7589433 : if ( hQMetaData->no_directions == 2 )
234 : {
235 : /* Calculate bits for dfRatio */
236 150176 : dir2band = 0;
237 1721298 : for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
238 : {
239 1571122 : if ( hQMetaData->twoDirBands[b] == 1 )
240 : {
241 508928 : dfRatio_bits[dir2band] = ivas_get_df_ratio_bits( hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] );
242 508928 : dir2band++;
243 : }
244 : }
245 :
246 150176 : 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 8043233 : if ( hQMetaData->no_directions == 2 )
252 : {
253 603976 : dir2band = 0;
254 7166898 : for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
255 : {
256 6562922 : if ( hQMetaData->twoDirBands[b] == 1 )
257 : {
258 : float diffRatio, dfRatio, dir1ratio, dir2ratio;
259 : int16_t dfRatio_qsteps;
260 :
261 5500728 : diffRatio = diffuseness_reconstructions[hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0]];
262 5500728 : dfRatio_qsteps = 1 << dfRatio_bits[dir2band];
263 : /* already encoded as total and ratios in HO-DirAC */
264 5500728 : if ( hodirac_flag )
265 : {
266 4991800 : dfRatio = usdequant( hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0], 0.0f, 1.f / ( dfRatio_qsteps - 1 ) );
267 4991800 : dir1ratio = 1.f - diffRatio;
268 4991800 : dir2ratio = dfRatio;
269 : }
270 : else
271 : {
272 508928 : dfRatio = usdequant( hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0], 0.5f, 0.5f / ( dfRatio_qsteps - 1 ) );
273 :
274 508928 : dir1ratio = dfRatio * ( 1.0f - diffRatio );
275 508928 : 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 5500728 : hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] = masa_sq( 1.0f - dir1ratio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
280 5500728 : if ( hodirac_flag )
281 : {
282 : float tmp;
283 4991800 : 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 508928 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0] = masa_sq( 1.0f - dir2ratio, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
288 : }
289 :
290 26807649 : for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
291 : {
292 21306921 : hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = dir1ratio;
293 21306921 : 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 26807649 : for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
297 : {
298 21306921 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[m] = dir2ratio;
299 21306921 : 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 5500728 : dir2band++;
303 : }
304 : else
305 : {
306 : /* 1dir band */
307 1062194 : 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 2516639 : for ( m = 1; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
309 : {
310 1454445 : hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio[0];
311 1454445 : 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 36258385 : for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
320 : {
321 28819128 : 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 97000770 : for ( m = 1; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
323 : {
324 68181642 : hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio[0];
325 68181642 : 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 8043233 : bits_dir_raw_pre[0] = 0;
333 8043233 : bits_dir_raw_pre[1] = 0;
334 8043233 : dir2band = 0;
335 43425283 : for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
336 : {
337 35382050 : if ( hQMetaData->no_directions == 2 && hQMetaData->twoDirBands[b] == 1 )
338 5500728 : {
339 : int16_t index_dirRatio1Inv, index_dirRatio2Inv, index_dirRatio1Inv_mod, index_dirRatio2Inv_mod;
340 :
341 5500728 : index_dirRatio1Inv = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
342 5500728 : index_dirRatio2Inv = hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[0];
343 :
344 5500728 : masa_compensate_two_dir_energy_ratio_index( index_dirRatio1Inv, index_dirRatio2Inv, &index_dirRatio1Inv_mod, &index_dirRatio2Inv_mod, hodirac_flag );
345 :
346 26807649 : for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
347 : {
348 21306921 : hQMetaData->q_direction[0].band_data[b].energy_ratio_index_mod[m] = index_dirRatio1Inv_mod;
349 21306921 : hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m] = bits_direction_masa[index_dirRatio1Inv_mod];
350 21306921 : bits_dir_raw_pre[0] += hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m];
351 : }
352 :
353 26807649 : for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
354 : {
355 21306921 : hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index_mod[m] = index_dirRatio2Inv_mod;
356 21306921 : hQMetaData->q_direction[1].band_data[dir2band].bits_sph_idx[m] = bits_direction_masa[index_dirRatio2Inv_mod];
357 21306921 : bits_dir_raw_pre[1] += hQMetaData->q_direction[1].band_data[dir2band].bits_sph_idx[m];
358 : }
359 :
360 5500728 : dir2band++;
361 : }
362 : else
363 : {
364 129398731 : for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
365 : {
366 99517409 : hQMetaData->q_direction[0].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
367 99517409 : 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 99517409 : bits_dir_raw_pre[0] += hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m];
369 : }
370 : }
371 : }
372 :
373 8043233 : if ( hQMetaData->no_directions == 2 )
374 : {
375 603976 : 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 603976 : 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 143690 : bits_sur_coherence = read_surround_coherence( bitstream, index, hQMetaData );
379 : }
380 : else
381 : {
382 460286 : bits_sur_coherence = 0;
383 : /*Surround coherence*/
384 5540670 : for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
385 : {
386 5080384 : if ( hQMetaData->surcoh_band_data != NULL )
387 : {
388 88584 : set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
389 : }
390 : }
391 : }
392 603976 : bits_no_dirs_coh += bits_sur_coherence;
393 603976 : 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 7439257 : no_TF = hQMetaData->q_direction[0].cfg.nbands * hQMetaData->q_direction[0].cfg.nblocks;
398 7439257 : 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 864856 : bits_sur_coherence = read_surround_coherence( bitstream, index, hQMetaData );
401 : }
402 : else
403 : {
404 6574401 : bits_sur_coherence = 0;
405 : /*Surround coherence*/
406 27445655 : for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
407 : {
408 20871254 : if ( hQMetaData->surcoh_band_data != NULL )
409 : {
410 642327 : set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
411 : }
412 : }
413 : }
414 7439257 : bits_no_dirs_coh += bits_sur_coherence;
415 :
416 7439257 : total_bits_1dir = hQMetaData->metadata_max_bits - bits_no_dirs_coh;
417 : }
418 :
419 8043233 : bits_dir_target = 0;
420 8043233 : bits_dir_used = 0;
421 :
422 16690442 : for ( d = 0; d < hQMetaData->no_directions; d++ )
423 : {
424 8647209 : q_direction = &hQMetaData->q_direction[d];
425 8647209 : nbands = q_direction->cfg.nbands;
426 8647209 : nblocks = q_direction->cfg.nblocks;
427 8647209 : start_band = q_direction->cfg.start_band;
428 :
429 8647209 : diffuseness_index_max_ec_frame = diffuseness_index_max_ec_frame_pre[0];
430 8647209 : if ( d == 0 )
431 : {
432 8043233 : bits_diff = bits_diff_sum;
433 : }
434 : else
435 : {
436 603976 : bits_diff = 0;
437 : }
438 8647209 : bits_dir_raw = bits_dir_raw_pre[d];
439 :
440 : /* Read coherence, if any */
441 8647209 : bits_coherence = 0;
442 :
443 8647209 : if ( all_coherence_zero == 0 )
444 : {
445 1167637 : bits_coherence = read_coherence_data( bitstream, index, hQMetaData, d, 0 );
446 : }
447 : else
448 : {
449 : /*Surround coherence*/
450 38434589 : for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
451 : {
452 30955017 : if ( hQMetaData->surcoh_band_data != NULL )
453 : {
454 742490 : set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
455 : }
456 :
457 30955017 : if ( hQMetaData->q_direction[d].coherence_band_data != NULL )
458 : {
459 742490 : 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 8647209 : q_direction->not_in_2D = bitstream[( *index )--];
466 8647209 : signal_bits = 1;
467 :
468 : /* Read EC signaling */
469 8647209 : ec_flag = 0;
470 8647209 : if ( total_bits_1dir + bits_sur_coherence <= hQMetaData->qmetadata_max_bit_req )
471 : {
472 6172059 : ec_flag = bitstream[( *index )--];
473 6172059 : signal_bits++;
474 6172059 : if ( nblocks > 1 )
475 : {
476 4824105 : if ( ec_flag )
477 : {
478 281898 : ec_flag += bitstream[( *index )--];
479 281898 : signal_bits++;
480 : }
481 : }
482 : }
483 :
484 : /* Decode quantized directions frame-wise */
485 8647209 : if ( ec_flag == 0 ) /* EC 1*/
486 : {
487 8217995 : bits_dir = 0;
488 8217995 : raw_flag[0] = bitstream[( *index )--];
489 8217995 : bits_dir++;
490 :
491 8217995 : if ( raw_flag[0] == 0 )
492 : {
493 5364672 : 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 2853323 : bits_dir += ivas_qmetadata_raw_decode_dir( q_direction, bitstream, index, nbands, start_band, 0 );
498 : }
499 : }
500 : /* Decode quantized directions band-wise */
501 429214 : else if ( ec_flag == 1 && ( nblocks > 1 ) ) /* EC2 */
502 : {
503 81853 : bits_dir = 0;
504 425206 : for ( b = start_band; b < nbands; b++ )
505 : {
506 343353 : raw_flag[b] = bitstream[( *index )--];
507 343353 : bits_dir++;
508 : }
509 :
510 : /* Read EC bits*/
511 81853 : diff_bits = bits_diff + bits_coherence + signal_bits - total_bits_1dir;
512 :
513 425206 : for ( b = start_band; b < nbands; b++ )
514 : {
515 343353 : if ( raw_flag[b] == 0 )
516 : {
517 179075 : 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 164278 : diff_bits += q_direction->band_data[b].bits_sph_idx[0] * q_direction->cfg.nblocks;
522 : }
523 : }
524 81853 : diff_bits += bits_dir;
525 :
526 : /* Small requantization?*/
527 81853 : 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 344439 : for ( i = q_direction->cfg.start_band; i < q_direction->cfg.nbands; i++ )
534 : {
535 1408215 : for ( j = 0; j < q_direction->cfg.nblocks; j++ )
536 : {
537 1126572 : bits_temp[i][j] = q_direction->band_data[i].bits_sph_idx[j];
538 : }
539 : }
540 :
541 62796 : small_reduction_direction( q_direction, bits_temp, raw_flag, &diff_bits );
542 :
543 344439 : for ( i = q_direction->cfg.start_band; i < q_direction->cfg.nbands; i++ )
544 : {
545 1408215 : for ( j = 0; j < q_direction->cfg.nblocks; j++ )
546 : {
547 1126572 : 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 425206 : for ( b = start_band; b < nbands; b++ )
557 : {
558 343353 : if ( raw_flag[b] )
559 : {
560 164278 : 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 347361 : ec_flag = 2;
569 :
570 347361 : if ( hQMetaData->is_masa_ivas_format == 0 )
571 : {
572 118120 : reduce_bits = bits_dir_raw - ( total_bits_1dir - bits_diff - bits_coherence - signal_bits );
573 118120 : ind_order[0] = -1;
574 : }
575 : else
576 : {
577 229241 : ind_order[0] = 0;
578 229241 : reduce_bits = min( nbands * nblocks + MASA_BIT_REDUCT_PARAM, bits_dir_raw - ( total_bits_1dir - bits_diff - bits_coherence - signal_bits ) );
579 229241 : if ( reduce_bits > bits_dir_raw - nbands * nblocks )
580 : {
581 532 : reduce_bits = bits_dir_raw - nbands * nblocks;
582 : }
583 : }
584 347361 : only_reduce_bits_direction( &dummy, q_direction, reduce_bits, nbands, nblocks, ind_order );
585 :
586 : /* Read directions */
587 347361 : bits_dir = read_directions( q_direction, (uint8_t) nbands, (uint8_t) nblocks, bitstream, index, ind_order );
588 : }
589 :
590 8647209 : if ( bits_coherence > 0 )
591 : {
592 1158097 : if ( nblocks > 1 )
593 : {
594 899392 : decode_spread_coherence( hQMetaData, d, nblocks, 0 );
595 : }
596 : }
597 : else
598 : {
599 38423469 : for ( b = start_band; b < nbands; b++ )
600 : {
601 30934357 : if ( q_direction->coherence_band_data != NULL )
602 : {
603 721830 : set_c( (int8_t *) q_direction->coherence_band_data[b].spread_coherence, 0, nblocks );
604 : }
605 : }
606 : }
607 8647209 : if ( d == 0 )
608 : {
609 8043233 : total_bits_1dir = hQMetaData->metadata_max_bits - ( start_index_0 - *index );
610 : }
611 :
612 8647209 : bits_dir_target += bits_dir_raw;
613 8647209 : 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 8043233 : if ( hQMetaData->no_directions == 2 )
657 : {
658 603976 : d = hQMetaData->q_direction[1].cfg.nbands - 1;
659 603976 : nblocks = hQMetaData->q_direction[0].cfg.nblocks;
660 :
661 7166898 : for ( b = hQMetaData->q_direction[0].cfg.nbands - 1; b >= 0; b-- )
662 : {
663 6562922 : if ( hQMetaData->twoDirBands[b] == 1 )
664 : {
665 5500728 : mvr2r( hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].band_data[b].azimuth, nblocks );
666 5500728 : mvr2r( hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].band_data[b].elevation, nblocks );
667 5500728 : mvr2r( hQMetaData->q_direction[1].band_data[d].energy_ratio, hQMetaData->q_direction[1].band_data[b].energy_ratio, nblocks );
668 :
669 5500728 : if ( hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0] < 7 )
670 : {
671 21884694 : for ( m = 0; m < nblocks; m++ )
672 : {
673 17390988 : hQMetaData->q_direction[1].band_data[b].azimuth[m] += hQMetaData->q_direction[0].band_data[b].azimuth[m] - 180;
674 17390988 : 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 17390988 : if ( hQMetaData->q_direction[1].band_data[b].azimuth[m] < -180 )
679 : {
680 1980977 : hQMetaData->q_direction[1].band_data[b].azimuth[m] += 360;
681 : }
682 : }
683 : }
684 :
685 5500728 : if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
686 : {
687 508928 : mvc2c( hQMetaData->q_direction[1].coherence_band_data[d].spread_coherence, hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence, nblocks );
688 : }
689 5500728 : d--;
690 : }
691 : else
692 : {
693 1062194 : set_f( hQMetaData->q_direction[1].band_data[b].azimuth, 0.0f, nblocks );
694 1062194 : set_f( hQMetaData->q_direction[1].band_data[b].elevation, 0.0f, nblocks );
695 1062194 : set_f( hQMetaData->q_direction[1].band_data[b].energy_ratio, 0.0f, nblocks );
696 :
697 1062194 : if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
698 : {
699 1062194 : 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 603976 : if ( !hodirac_flag )
706 : {
707 1721298 : for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
708 : {
709 : float ratioSum;
710 :
711 1571122 : ratioSum = hQMetaData->q_direction[0].band_data[b].energy_ratio[0] + hQMetaData->q_direction[1].band_data[b].energy_ratio[0];
712 :
713 1571122 : 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 8043233 : hQMetaData->ec_flag = ec_flag;
724 :
725 8043233 : hQMetaData->dir_comp_ratio = (float) bits_dir_used / (float) bits_dir_target;
726 :
727 8043233 : if ( hQMetaData->dir_comp_ratio > 1.0f )
728 : {
729 2199680 : hQMetaData->dir_comp_ratio = 1.0f;
730 : }
731 :
732 8043233 : 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 116037 : 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 116037 : 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 116037 : start_index_0 = *index;
790 : /* read number of higher inactive/not encoded bands */
791 116037 : if ( bitstream[( *index )--] )
792 : {
793 45318 : codedBands = MASA_MAXIMUM_CODING_SUBBANDS - ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 1 ) - 1;
794 : }
795 : else
796 : {
797 70719 : codedBands = MASA_MAXIMUM_CODING_SUBBANDS;
798 : }
799 221878 : for ( b = codedBands; b < ncoding_bands_config; b++ )
800 : {
801 529205 : for ( m = 0; m < MAX_PARAM_SPATIAL_SUBFRAMES; m++ )
802 : {
803 423364 : hQMetaData->q_direction[0].band_data[b].azimuth[m] = 0.0f;
804 423364 : hQMetaData->q_direction[0].band_data[b].elevation[m] = 0.0f;
805 423364 : hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = 0.0f;
806 :
807 423364 : if ( hQMetaData->coherence_flag && hQMetaData->q_direction[0].coherence_band_data != NULL )
808 : {
809 423364 : hQMetaData->q_direction[0].coherence_band_data[b].spread_coherence[m] = 0u;
810 : }
811 :
812 423364 : if ( hQMetaData->no_directions == 2 )
813 : {
814 151816 : hQMetaData->q_direction[1].band_data[b].azimuth[m] = 0.0f;
815 151816 : hQMetaData->q_direction[1].band_data[b].elevation[m] = 0.0f;
816 151816 : hQMetaData->q_direction[1].band_data[b].energy_ratio[m] = 0.0f;
817 :
818 151816 : if ( hQMetaData->coherence_flag && hQMetaData->q_direction[1].coherence_band_data != NULL )
819 : {
820 151816 : hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence[m] = 0u;
821 : }
822 : }
823 :
824 423364 : if ( hQMetaData->coherence_flag && hQMetaData->surcoh_band_data != NULL )
825 : {
826 423364 : hQMetaData->surcoh_band_data[b].surround_coherence[m] = 0u;
827 : }
828 : }
829 :
830 105841 : if ( hQMetaData->no_directions == 2 )
831 : {
832 37954 : hQMetaData->twoDirBands[b] = 0;
833 : }
834 : }
835 116037 : sf_nbands0 = hQMetaData->q_direction[0].cfg.nbands;
836 :
837 116037 : 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 116037 : all_coherence_zero = 1;
844 116037 : if ( hQMetaData->coherence_flag )
845 : {
846 : /* read if coherence is zero */
847 116037 : all_coherence_zero = bitstream[( *index )--];
848 : #ifdef DEBUG_MODE_QMETADATA
849 : bits_no_dirs_coh += 1;
850 : #endif
851 : }
852 :
853 116037 : hQMetaData->all_coherence_zero = (uint8_t) all_coherence_zero;
854 :
855 116037 : if ( hQMetaData->no_directions == 2 )
856 : {
857 22234 : set_c( (int8_t *) hQMetaData->twoDirBands, 1, hQMetaData->q_direction[0].cfg.nbands );
858 : }
859 :
860 116037 : if ( bits_sph_idx == 11 && hQMetaData->no_directions == 2 )
861 : {
862 : /* Read which bands have 2 directions */
863 14147 : hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands;
864 14147 : sf_nbands1 = hQMetaData->q_direction[1].cfg.nbands;
865 14147 : if ( hQMetaData->q_direction[1].cfg.nbands > codedBands )
866 : {
867 1428 : hQMetaData->q_direction[1].cfg.nbands = codedBands;
868 : }
869 14147 : set_c( (int8_t *) hQMetaData->twoDirBands, 0, hQMetaData->q_direction[0].cfg.nbands );
870 14147 : d = *index;
871 14147 : dif_p[0] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
872 14147 : p[0] = dif_p[0];
873 14147 : hQMetaData->twoDirBands[p[0]] = 1;
874 133630 : for ( b = 1; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
875 : {
876 119483 : dif_p[b] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 );
877 119483 : p[b] = p[b - 1] + dif_p[b] + 1;
878 119483 : hQMetaData->twoDirBands[p[b]] = 1;
879 : }
880 : #ifdef DEBUG_MODE_QMETADATA
881 : bits_no_dirs_coh += ( d - *index );
882 : #endif
883 : }
884 :
885 116037 : if ( bits_sph_idx == 16 && hQMetaData->no_directions == 2 )
886 : {
887 8087 : sf_nbands1 = hQMetaData->q_direction[1].cfg.nbands;
888 8087 : if ( hQMetaData->q_direction[1].cfg.nbands > codedBands )
889 : {
890 539 : hQMetaData->q_direction[1].cfg.nbands = codedBands;
891 : }
892 : }
893 : #ifdef DEBUG_MODE_QMETADATA
894 : bits_diff_sum =
895 : #endif
896 116037 : ivas_qmetadata_entropy_decode_diffuseness_hr_512( bitstream, index, &( hQMetaData->q_direction[0] ) );
897 :
898 116037 : if ( hQMetaData->no_directions == 2 )
899 : {
900 : #ifdef DEBUG_MODE_QMETADATA
901 : bits_diff_sum +=
902 : #endif
903 22234 : ivas_qmetadata_entropy_decode_diffuseness_hr_512( bitstream, index, &( hQMetaData->q_direction[1] ) );
904 : }
905 :
906 :
907 2715065 : for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
908 : {
909 11695993 : for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
910 : {
911 9096965 : 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 116037 : if ( hQMetaData->no_directions == 2 )
915 : {
916 : float ratioSum;
917 22234 : if ( bits_sph_idx == 16 )
918 : {
919 194850 : for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
920 : {
921 757925 : for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
922 : {
923 571162 : 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 571162 : ratioSum = hQMetaData->q_direction[0].band_data[b].energy_ratio[m] + hQMetaData->q_direction[1].band_data[b].energy_ratio[m];
927 :
928 571162 : if ( ratioSum > 1.0f )
929 : {
930 112116 : hQMetaData->q_direction[0].band_data[b].energy_ratio[m] /= ratioSum;
931 112116 : 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 14147 : d = 0;
940 322648 : for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
941 : {
942 308501 : if ( hQMetaData->twoDirBands[b] == 1 )
943 : {
944 133630 : pos_2dir_band[d] = b;
945 133630 : d++;
946 : }
947 : else
948 : {
949 174871 : pos_2dir_band[d] = 0;
950 : }
951 : }
952 147777 : for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
953 : {
954 519950 : for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
955 : {
956 386320 : 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 386320 : 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 386320 : if ( ratioSum > 1.0f )
961 : {
962 30933 : hQMetaData->q_direction[0].band_data[pos_2dir_band[b]].energy_ratio[m] /= ratioSum;
963 30933 : hQMetaData->q_direction[1].band_data[b].energy_ratio[m] /= ratioSum;
964 : }
965 : }
966 : }
967 : }
968 : }
969 :
970 116037 : if ( hQMetaData->no_directions == 2 )
971 : {
972 342627 : for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ )
973 : {
974 1277875 : for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ )
975 : {
976 957482 : hQMetaData->q_direction[1].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[1].band_data[b].energy_ratio_index[m];
977 957482 : hQMetaData->q_direction[1].band_data[b].bits_sph_idx[m] = bits_sph_idx;
978 : }
979 : }
980 : }
981 :
982 2715065 : for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
983 : {
984 11695993 : for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
985 : {
986 9096965 : hQMetaData->q_direction[0].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0];
987 9096965 : hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m] = bits_sph_idx;
988 : }
989 : }
990 :
991 116037 : if ( all_coherence_zero == 0 )
992 : {
993 : #ifdef DEBUG_MODE_QMETADATA
994 : bits_sur_coherence =
995 : #endif
996 107685 : 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 184982 : for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
1005 : {
1006 176630 : if ( hQMetaData->surcoh_band_data != NULL )
1007 : {
1008 176630 : 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 254308 : for ( d = 0; d < hQMetaData->no_directions; d++ )
1017 : {
1018 138271 : q_direction = &hQMetaData->q_direction[d];
1019 138271 : nbands = q_direction->cfg.nbands;
1020 138271 : start_band = q_direction->cfg.start_band;
1021 :
1022 : /* Read coherence, if any */
1023 138271 : if ( all_coherence_zero == 0 )
1024 : {
1025 127691 : read_coherence_data_hr_512( bitstream, index, hQMetaData, d, bits_sp_coh );
1026 : }
1027 : else
1028 : {
1029 : /*Surround coherence*/
1030 239866 : for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
1031 : {
1032 229286 : if ( hQMetaData->surcoh_band_data != NULL )
1033 : {
1034 229286 : set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
1035 : }
1036 :
1037 229286 : if ( hQMetaData->q_direction[d].coherence_band_data != NULL )
1038 : {
1039 229286 : 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 138271 : 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 116037 : if ( hQMetaData->no_directions == 2 )
1077 : {
1078 : /* move 2 dir data to its correct subband */
1079 22234 : if ( bits_sph_idx == 11 )
1080 : {
1081 : int16_t nblocks;
1082 14147 : d = hQMetaData->q_direction[1].cfg.nbands - 1;
1083 14147 : nblocks = hQMetaData->q_direction[0].cfg.nblocks;
1084 :
1085 322648 : for ( b = hQMetaData->q_direction[0].cfg.nbands - 1; b >= 0; b-- )
1086 : {
1087 308501 : if ( hQMetaData->twoDirBands[b] == 1 )
1088 : {
1089 133630 : mvr2r( hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].band_data[b].azimuth, nblocks );
1090 133630 : mvr2r( hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].band_data[b].elevation, nblocks );
1091 133630 : mvr2r( hQMetaData->q_direction[1].band_data[d].energy_ratio, hQMetaData->q_direction[1].band_data[b].energy_ratio, nblocks );
1092 :
1093 :
1094 133630 : if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
1095 : {
1096 133630 : mvc2c( hQMetaData->q_direction[1].coherence_band_data[d].spread_coherence, hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence, nblocks );
1097 : }
1098 133630 : d--;
1099 : }
1100 : else
1101 : {
1102 174871 : set_f( hQMetaData->q_direction[1].band_data[b].azimuth, 0.0f, nblocks );
1103 174871 : set_f( hQMetaData->q_direction[1].band_data[b].elevation, 0.0f, nblocks );
1104 174871 : set_f( hQMetaData->q_direction[1].band_data[b].energy_ratio, 0.0f, nblocks );
1105 :
1106 174871 : if ( hQMetaData->q_direction[1].coherence_band_data != NULL )
1107 : {
1108 174871 : 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 517498 : for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ )
1116 : {
1117 2013345 : for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ )
1118 : {
1119 : float ratioSum;
1120 :
1121 1518081 : ratioSum = hQMetaData->q_direction[0].band_data[b].energy_ratio[m] + hQMetaData->q_direction[1].band_data[b].energy_ratio[m];
1122 :
1123 1518081 : 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 116037 : hQMetaData->ec_flag = 0;
1168 :
1169 116037 : hQMetaData->dir_comp_ratio = 1.0f;
1170 :
1171 116037 : if ( hQMetaData->dir_comp_ratio > 1.0f )
1172 : {
1173 0 : hQMetaData->dir_comp_ratio = 1.0f;
1174 : }
1175 :
1176 116037 : hQMetaData->q_direction[0].cfg.nbands = sf_nbands0;
1177 116037 : if ( hQMetaData->no_directions == 2 )
1178 : {
1179 22234 : hQMetaData->q_direction[1].cfg.nbands = sf_nbands1;
1180 : }
1181 :
1182 116037 : 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 13315 : 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 13315 : if ( ivas_format == SBA_FORMAT )
1231 : {
1232 11970 : sba_spar_bitlen = ivas_sba_spar_sid_bitlen( nchan_transport );
1233 11970 : 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 1345 : metadata_sid_bits = ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS;
1238 : }
1239 :
1240 13315 : start_index = *index;
1241 :
1242 : /* read MASA SID descriptor */
1243 13315 : if ( ivas_format == MASA_FORMAT && nchan_transport == 2 )
1244 : {
1245 489 : b = bitstream[( *index )--];
1246 489 : if ( b )
1247 : {
1248 70 : *element_mode = IVAS_CPE_MDCT;
1249 : }
1250 : else
1251 : {
1252 419 : *element_mode = IVAS_CPE_DFT;
1253 : }
1254 : }
1255 :
1256 : /* Fix configuration for SID */
1257 13315 : q_direction = &hQMetaData->q_direction[0]; /* only 1 direction */
1258 13315 : if ( ivas_format == SBA_FORMAT )
1259 : {
1260 11970 : nbands = DIRAC_DTX_BANDS; /* only 2 bands transmitted */
1261 : }
1262 : else
1263 : {
1264 1345 : nbands = 5; /* only 5 bands transmitted */
1265 : }
1266 :
1267 13315 : nblocks = q_direction->cfg.nblocks; /* only 1 block transmitted but up to 4 blocks re-generated */
1268 13315 : start_band = 0; /* start from band 0 */
1269 :
1270 : /* Read 2D signaling*/
1271 13315 : if ( ivas_format != SBA_FORMAT )
1272 : {
1273 1345 : q_direction->not_in_2D = bitstream[( *index )--];
1274 : }
1275 : else
1276 : {
1277 11970 : q_direction->not_in_2D = 1;
1278 : }
1279 :
1280 13315 : bits_dir = 0;
1281 13315 : if ( ivas_format != SBA_FORMAT )
1282 : {
1283 : /* Decode diffuseness*/
1284 8070 : for ( b = start_band; b < nbands; b++ )
1285 : {
1286 6725 : diffuseness_index[b] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS - 4 ) + 4;
1287 6725 : q_direction->band_data[b].energy_ratio_index[0] = diffuseness_index[b];
1288 6725 : q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[diffuseness_index[b]];
1289 6725 : bits_dir += q_direction->band_data[b].bits_sph_idx[0];
1290 6725 : 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 1345 : bits_delta = metadata_sid_bits - ( start_index - *index ) - bits_dir; /* bit_diff is already read */
1294 :
1295 1345 : if ( bits_delta > 0 )
1296 : {
1297 7266 : while ( bits_delta > 0 )
1298 : {
1299 32301 : for ( b = start_band; b < nbands && ( bits_delta > 0 ); b++ )
1300 : {
1301 26380 : if ( q_direction->band_data[b].bits_sph_idx[0] < 11 )
1302 : {
1303 26376 : bits_delta -= 1;
1304 26376 : q_direction->band_data[b].bits_sph_idx[0]++;
1305 : }
1306 : }
1307 : }
1308 :
1309 1345 : if ( q_direction->not_in_2D == 0 )
1310 : {
1311 990 : for ( b = start_band; b < nbands; b++ )
1312 : {
1313 825 : 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 35910 : for ( b = start_band; b < nbands; b++ )
1344 : {
1345 23940 : diffuseness_index[b] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS - 4 ) + 4;
1346 23940 : q_direction->band_data[b].energy_ratio_index[0] = diffuseness_index[b];
1347 23940 : q_direction->band_data[b].bits_sph_idx[0] = bits_direction_masa[diffuseness_index[b]];
1348 23940 : 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 43980 : for ( b = start_band; b < nbands; b++ )
1353 : {
1354 30665 : q_direction->band_data[b].energy_ratio[0] = 1.0f - diffuseness_reconstructions[diffuseness_index[b]];
1355 147901 : for ( i = 0; i < nblocks; i++ )
1356 : {
1357 117236 : q_direction->band_data[b].energy_ratio[i] = q_direction->band_data[b].energy_ratio[0];
1358 : }
1359 : }
1360 :
1361 : /* Decoder DOAs*/
1362 13315 : if ( q_direction->not_in_2D > 0 )
1363 : {
1364 42990 : for ( b = start_band; b < nbands; b++ )
1365 : {
1366 29840 : value = 0;
1367 161546 : for ( i = 0; i < q_direction->band_data[b].bits_sph_idx[0]; i++ )
1368 : {
1369 131706 : value = ( value << 1 ) + bitstream[( *index )--];
1370 : }
1371 144706 : for ( i = 0; i < nblocks; i++ )
1372 : {
1373 114866 : q_direction->band_data[b].spherical_index[i] = value;
1374 : }
1375 :
1376 29840 : 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 29840 : ivas_qmetadata_azimuth_elevation_to_direction_vector( avg_azimuth, avg_elevation, avg_direction_vector );
1379 29840 : 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 114866 : for ( m = 0; m < nblocks - 1; m++ )
1382 : {
1383 85026 : v_add( direction_vector, avg_direction_vector, direction_vector, 3 );
1384 85026 : 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 29840 : 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 990 : for ( b = start_band; b < nbands; b++ )
1393 : {
1394 825 : 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 825 : q_direction->band_data[b].azimuth_index[0] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, q_direction->band_data[b].azimuth_m_alphabet[0] );
1400 825 : 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 825 : 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 825 : avg_elevation = 0.f;
1404 :
1405 825 : ivas_qmetadata_azimuth_elevation_to_direction_vector( avg_azimuth, avg_elevation, avg_direction_vector );
1406 825 : 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 2370 : for ( m = 0; m < nblocks - 1; m++ )
1409 : {
1410 1545 : v_add( direction_vector, avg_direction_vector, direction_vector, 3 );
1411 1545 : 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 825 : 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 3195 : for ( i = 0; i < nblocks; i++ )
1417 : {
1418 2370 : 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 49943 : while ( start_index - *index < metadata_sid_bits )
1426 : {
1427 36628 : 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 13315 : return ( start_index - *index );
1452 : }
1453 :
1454 :
1455 : /*-----------------------------------------------------------------------*
1456 : * Local function definitions for diffuseness/energy ratios
1457 : *-----------------------------------------------------------------------*/
1458 :
1459 7786201 : 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 7786201 : val = 0;
1467 15681454 : while ( val <= DIFF_EC_HUFF_GR0_LIMIT )
1468 : {
1469 15674111 : if ( bitstream[( *index )--] == 1 )
1470 : {
1471 7895253 : val++;
1472 : }
1473 : else
1474 : {
1475 7778858 : break;
1476 : }
1477 : }
1478 :
1479 7786201 : if ( val == DIFF_EC_HUFF_GR0_LIMIT + 1 )
1480 : {
1481 7343 : val += 2 * bitstream[( *index )];
1482 7343 : ( *index )--;
1483 7343 : val += bitstream[( *index )];
1484 7343 : ( *index )--;
1485 : }
1486 :
1487 7786201 : if ( val % 2 == 0 )
1488 : {
1489 5309547 : return -( val / 2 ) + av;
1490 : }
1491 : else
1492 : {
1493 2476654 : return ( val + 1 ) / 2 + av;
1494 : }
1495 : }
1496 :
1497 :
1498 : /*-------------------------------------------------------------------*
1499 : * ivas_qmetadata_entropy_decode_diffuseness()
1500 : *
1501 : *
1502 : *-------------------------------------------------------------------*/
1503 :
1504 8043233 : 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 8043233 : index_start = *index;
1517 8043233 : nbands = q_direction->cfg.nbands;
1518 8043233 : start_band = q_direction->cfg.start_band;
1519 :
1520 : /* diffuseness decoding */
1521 : /* Handle one band as special case*/
1522 8043233 : if ( nbands == 1 )
1523 : {
1524 108234 : q_direction->band_data[0].energy_ratio_index[0] = 0;
1525 432936 : for ( b = 0; b < MASA_BITS_ER; b++ )
1526 : {
1527 324702 : q_direction->band_data[0].energy_ratio_index[0] = ( q_direction->band_data[0].energy_ratio_index[0] << 1 ) + bitstream[( *index )--];
1528 : }
1529 108234 : *diffuseness_index_max_ec_frame = 5;
1530 :
1531 108234 : return MASA_BITS_ER;
1532 : }
1533 :
1534 7934999 : 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 3566256 : if ( nbands < DIFF_EC_HUFF_BAND_LIMIT )
1538 : {
1539 2969320 : if ( bitstream[( *index )--] != 0 ) /* dif_have_unique_value */
1540 : {
1541 1477037 : dif_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS ); /* dif_unique_value */
1542 :
1543 6018266 : for ( b = start_band; b < nbands; b++ )
1544 : {
1545 4541229 : 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 1492283 : dif_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS - 1 ); /* dif_min_value */
1551 :
1552 6321948 : for ( b = start_band; b < nbands; b++ )
1553 : {
1554 4829665 : 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 596936 : av = 0;
1564 2387744 : for ( b = 0; b < MASA_BITS_ER; b++ )
1565 : {
1566 1790808 : av += bitstream[( *index )--] * ( 1 << ( MASA_BITS_ER - 1 - b ) );
1567 : }
1568 :
1569 596936 : dif_min = DIRAC_DIFFUSE_LEVELS;
1570 : /* read average removed data (average is added inside)*/
1571 8383137 : for ( b = start_band; b < nbands; b++ )
1572 : {
1573 7786201 : q_direction->band_data[b].energy_ratio_index[0] = ivas_diffuseness_huff_ec_decode( bitstream, index, av );
1574 7786201 : 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 4368743 : dif_min = DIRAC_DIFFUSE_LEVELS;
1581 :
1582 22485464 : for ( b = start_band; b < nbands; b++ )
1583 : {
1584 18116721 : q_direction->band_data[b].energy_ratio_index[0] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, DIRAC_DIFFUSE_LEVELS );
1585 18116721 : dif_min = min( dif_min, q_direction->band_data[b].energy_ratio_index[0] );
1586 : }
1587 : }
1588 :
1589 7934999 : *diffuseness_index_max_ec_frame = 5;
1590 : /* adaptively select the diffuseness_index_max_ec threshold */
1591 7934999 : if ( dif_min > 5 )
1592 : {
1593 1984378 : *diffuseness_index_max_ec_frame = DIRAC_DIFFUSE_LEVELS - 1;
1594 : }
1595 :
1596 7934999 : return ( index_start - *index );
1597 : }
1598 :
1599 :
1600 : /*-------------------------------------------------------------------*
1601 : * ivas_qmetadata_entropy_decode_diffuseness_hr_512()
1602 : *
1603 : *
1604 : *-------------------------------------------------------------------*/
1605 :
1606 138271 : 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 138271 : index_start = *index;
1618 138271 : nbands = q_direction->cfg.nbands;
1619 138271 : nblocks = q_direction->cfg.nblocks;
1620 138271 : start_band = q_direction->cfg.start_band;
1621 :
1622 : /* diffuseness decoding */
1623 : /* Handle one band as special case*/
1624 138271 : 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 3057692 : for ( b = start_band; b < nbands; b++ )
1636 : {
1637 12973868 : for ( k = 0; k < nblocks; k++ )
1638 : {
1639 10054447 : q_direction->band_data[b].energy_ratio_index[k] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, HR_MASA_ER_LEVELS );
1640 : }
1641 : }
1642 :
1643 138271 : return ( index_start - *index );
1644 : }
1645 :
1646 :
1647 : /*-------------------------------------------------------------------*
1648 : * ivas_qmetadata_entropy_decode_df_ratio()
1649 : *
1650 : *
1651 : *-------------------------------------------------------------------*/
1652 :
1653 603976 : 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 603976 : index_start = *index;
1671 603976 : nbands = q_direction->cfg.nbands;
1672 603976 : start_band = q_direction->cfg.start_band;
1673 :
1674 : /* Handle one band as special case*/
1675 603976 : if ( nbands == 1 )
1676 : {
1677 45882 : q_direction->band_data[0].energy_ratio_index[0] = 0;
1678 135036 : for ( b = 0; b < dfRatio_bits[0]; b++ )
1679 : {
1680 89154 : q_direction->band_data[0].energy_ratio_index[0] = ( q_direction->band_data[0].energy_ratio_index[0] << 1 ) + bitstream[( *index )--];
1681 : }
1682 45882 : return dfRatio_bits[0];
1683 : }
1684 :
1685 : /* Calculate raw coding bits and decide what modes are possible */
1686 558094 : bits_raw = 0;
1687 558094 : max_dfRatio_bits = 0;
1688 6012940 : for ( b = start_band; b < nbands; b++ )
1689 : {
1690 5454846 : bits_raw += dfRatio_bits[b];
1691 5454846 : max_dfRatio_bits = max( max_dfRatio_bits, dfRatio_bits[b] );
1692 : }
1693 :
1694 : /* Decide what modes are possible */
1695 558094 : if ( bits_raw >= max_dfRatio_bits + 2 + nbands )
1696 : {
1697 422001 : ec_mode = 2;
1698 : }
1699 136093 : else if ( bits_raw >= max_dfRatio_bits + 1 )
1700 : {
1701 136093 : ec_mode = 1;
1702 : }
1703 : else
1704 : {
1705 0 : ec_mode = 0;
1706 : }
1707 558094 : max_alphabet_size = 1 << max_dfRatio_bits;
1708 :
1709 558094 : dec_mode = 2; /* Default to raw decoding */
1710 558094 : if ( ec_mode == 1 )
1711 : {
1712 136093 : if ( bitstream[( *index )--] == 0 )
1713 : {
1714 23861 : dec_mode = 1; /* Switch to one value EC coding */
1715 : }
1716 : }
1717 422001 : else if ( ec_mode == 2 )
1718 : {
1719 422001 : if ( bitstream[( *index )--] == 0 )
1720 : {
1721 49297 : if ( bitstream[( *index )--] == 0 )
1722 : {
1723 11549 : dec_mode = 1; /* Switch to one value EC coding */
1724 : }
1725 : else
1726 : {
1727 37748 : dec_mode = 0; /* Use one-bit diff bandwise mode */
1728 : }
1729 : }
1730 : }
1731 :
1732 558094 : if ( dec_mode == 2 ) /* Raw decoding */
1733 : {
1734 5502373 : for ( b = start_band; b < nbands; b++ )
1735 : {
1736 5017437 : q_direction->band_data[b].energy_ratio_index[0] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, 1 << dfRatio_bits[b] );
1737 : }
1738 : }
1739 73158 : else if ( dec_mode == 1 ) /* One value decoding */
1740 : {
1741 35410 : ratio_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, max_alphabet_size ); /* dif_unique_value */
1742 :
1743 179700 : for ( b = start_band; b < nbands; b++ )
1744 : {
1745 144290 : q_direction->band_data[b].energy_ratio_index[0] = ratio_min;
1746 : }
1747 : }
1748 : else /* Bandwise 1-bit diff decoding */
1749 : {
1750 37748 : ratio_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, max_alphabet_size - 1 ); /* dif_min_value */
1751 :
1752 330867 : for ( b = start_band; b < nbands; b++ )
1753 : {
1754 293119 : q_direction->band_data[b].energy_ratio_index[0] = ratio_min + bitstream[( *index )--]; /* dif_bit_offset_values */
1755 : }
1756 : }
1757 :
1758 558094 : 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 5543747 : 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 5543747 : index_start = *index;
1800 5543747 : nblocks = q_direction->cfg.nblocks;
1801 :
1802 5543747 : diff_idx_min = DIRAC_DIFFUSE_LEVELS;
1803 :
1804 : /*Raw coding for high diffuseness*/
1805 34212956 : for ( b = start_band; b < nbands; b++ )
1806 : {
1807 28669209 : if ( hrmasa_flag )
1808 : {
1809 0 : diff_idx = 0;
1810 : }
1811 : else
1812 : {
1813 28669209 : diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
1814 : }
1815 :
1816 28669209 : diff_idx_min = min( diff_idx_min, diff_idx );
1817 28669209 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
1818 : {
1819 704341 : elev_alph[b] = no_theta_masa[bits_direction_masa[diff_idx] - 3];
1820 : }
1821 : else
1822 : {
1823 27964868 : elev_alph[b] = no_theta_masa[bits_direction_masa[diff_idx] - 3] * 2 - 1;
1824 : }
1825 :
1826 28669209 : if ( q_direction->band_data[b].energy_ratio_index_mod[0] > diffuseness_index_max_ec_frame )
1827 : {
1828 6944173 : bands_entropic[b] = 0;
1829 :
1830 6944173 : if ( q_direction->not_in_2D > 0 )
1831 : {
1832 6027939 : decode_fixed_rate( q_direction, bitstream, index, b, nblocks );
1833 : }
1834 : else
1835 : {
1836 : /* in 2D */
1837 4241510 : for ( m = 0; m < nblocks; m++ )
1838 : {
1839 3325276 : q_direction->band_data[b].elevation[m] = 0.f;
1840 3325276 : q_direction->band_data[b].elevation_index[m] = 0;
1841 :
1842 3325276 : azith_alph[b][m] = no_phi_masa[bits_direction_masa[diff_idx] - 1][0];
1843 3325276 : q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, azith_alph[b][m] );
1844 3325276 : 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 21725036 : bands_entropic[b] = 1;
1851 : }
1852 : }
1853 :
1854 : /*EC for the low diffuseness*/
1855 :
1856 : /*Elevation only if not 2D */
1857 5543747 : if ( q_direction->not_in_2D > 0 )
1858 : {
1859 4085968 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
1860 : {
1861 89788 : avg_elevation_alphabet = no_theta_masa[bits_direction_masa[diff_idx_min] - 3];
1862 89788 : avg_elevation_idx = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, avg_elevation_alphabet );
1863 : }
1864 : else
1865 : {
1866 3996180 : avg_elevation_alphabet = no_theta_masa[bits_direction_masa[diff_idx_min] - 3] * 2 - 1;
1867 3996180 : avg_elevation_idx = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, avg_elevation_alphabet );
1868 3996180 : avg_elevation_idx = ivas_qmetadata_dereorder_generic( avg_elevation_idx ) + ( avg_elevation_alphabet >> 1 );
1869 : }
1870 :
1871 4085968 : gr_param_elev = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, 4 + 1 );
1872 4085968 : if ( gr_param_elev == 4 ) /* all the elevation distances are zero */
1873 : {
1874 3815915 : for ( b = start_band; b < nbands; b++ )
1875 : {
1876 2942707 : if ( bands_entropic[b] )
1877 : {
1878 : int16_t tmp_index;
1879 1439679 : if ( hrmasa_flag )
1880 : {
1881 0 : diff_idx = 0;
1882 : }
1883 : else
1884 : {
1885 1439679 : diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
1886 : }
1887 :
1888 1439679 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
1889 : {
1890 60393 : avg_elevation_index_projected = ivas_chan_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
1891 : }
1892 : else
1893 : {
1894 1379286 : avg_elevation_index_projected = ivas_dirac_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
1895 :
1896 : /*reorder elevation indexing*/
1897 1379286 : tmp_index = avg_elevation_index_projected - ( elev_alph[b] >> 1 );
1898 1379286 : if ( tmp_index < 0 )
1899 : {
1900 322286 : tmp_index = tmp_index * -2;
1901 : }
1902 1057000 : else if ( tmp_index > 0 )
1903 : {
1904 304163 : tmp_index = tmp_index * 2 - 1;
1905 : }
1906 1379286 : avg_elevation_index_projected = tmp_index;
1907 : }
1908 :
1909 5890500 : for ( m = 0; m < nblocks; m++ )
1910 : {
1911 4450821 : q_direction->band_data[b].elevation_index[m] = avg_elevation_index_projected;
1912 :
1913 : /*deduce aplhabet for azimuth*/
1914 4450821 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
1915 : {
1916 180006 : 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 4270815 : 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 4450821 : 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 21687130 : for ( b = start_band; b < nbands; b++ )
1932 : {
1933 18474370 : if ( bands_entropic[b] )
1934 : {
1935 13949459 : if ( hrmasa_flag )
1936 : {
1937 0 : diff_idx = 0;
1938 : }
1939 : else
1940 : {
1941 13949459 : diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
1942 : }
1943 :
1944 66956644 : for ( m = 0; m < nblocks; m++ )
1945 : {
1946 : int16_t tmp_index;
1947 53007185 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
1948 : {
1949 783697 : avg_elevation_index_projected = ivas_chan_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
1950 783697 : tmp_index = ivas_qmetadata_DecodeExtendedGR( bitstream, index, 2 * elev_alph[b] - 1, gr_param_elev );
1951 783697 : if ( tmp_index % 2 )
1952 : {
1953 81482 : tmp_index = avg_elevation_index_projected + ( ( tmp_index + 1 ) >> 1 );
1954 : }
1955 : else
1956 : {
1957 702215 : tmp_index = avg_elevation_index_projected - ( tmp_index >> 1 );
1958 : }
1959 783697 : q_direction->band_data[b].elevation_index[m] = tmp_index;
1960 :
1961 : /*deduce aplhabet for azimuth*/
1962 783697 : 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 52223488 : avg_elevation_index_projected = ivas_dirac_project_elevation_index( avg_elevation_idx, avg_elevation_alphabet, elev_alph[b] );
1967 :
1968 52223488 : tmp_index = ivas_qmetadata_DecodeExtendedGR( bitstream, index, elev_alph[b], gr_param_elev );
1969 52223488 : tmp_index = ivas_qmetadata_ReorderElevationDecoded( tmp_index, avg_elevation_index_projected, elev_alph[b] );
1970 :
1971 : /*reorder elevation indexing*/
1972 52223488 : tmp_index = tmp_index - ( elev_alph[b] >> 1 );
1973 52223488 : if ( tmp_index < 0 )
1974 : {
1975 21060965 : tmp_index = tmp_index * -2;
1976 : }
1977 31162523 : else if ( tmp_index > 0 )
1978 : {
1979 11431515 : tmp_index = tmp_index * 2 - 1;
1980 : }
1981 52223488 : q_direction->band_data[b].elevation_index[m] = tmp_index;
1982 :
1983 : /*deduce aplhabet for azimuth*/
1984 52223488 : 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 53007185 : 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 8709911 : for ( b = start_band; b < nbands; b++ )
1997 : {
1998 7252132 : if ( bands_entropic[b] )
1999 : {
2000 6335898 : if ( hrmasa_flag )
2001 : {
2002 0 : diff_idx = 0;
2003 : }
2004 : else
2005 : {
2006 6335898 : diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
2007 : }
2008 :
2009 27544299 : for ( m = 0; m < nblocks; m++ )
2010 : {
2011 21208401 : q_direction->band_data[b].elevation_index[m] = 0;
2012 :
2013 : /*deduce alphabet for azimuth*/
2014 21208401 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2015 : {
2016 631059 : 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 20577342 : 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 21208401 : 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 5543747 : avg_azimuth_alphabet = no_phi_masa[bits_direction_masa[diff_idx_min] - 1][0]; /* average azimuth is quantized on the equatorial plane */
2032 5543747 : avg_azimuth_index = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, avg_azimuth_alphabet );
2033 5543747 : avg_azimuth_index = ivas_qmetadata_dereorder_generic( avg_azimuth_index );
2034 5543747 : avg_azimuth_index = avg_azimuth_index + ( avg_azimuth_alphabet >> 1 );
2035 :
2036 5543747 : gr_param_azith = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, 5 + 1 );
2037 5543747 : if ( gr_param_azith == 5 ) /* all the azimuth distances are zero */
2038 : {
2039 5071345 : for ( b = start_band; b < nbands; b++ )
2040 : {
2041 3994356 : if ( bands_entropic[b] )
2042 : {
2043 9100879 : for ( m = 0; m < nblocks; m++ )
2044 : {
2045 6467336 : 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 6467336 : if ( azith_alph[b][m] == 1 )
2048 : {
2049 11899 : q_direction->band_data[b].azimuth[m] = 0.f;
2050 : }
2051 : else
2052 : {
2053 6455437 : 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 4466758 : set_zero( avg_direction_vector, 3 );
2062 4466758 : use_adapt_avg = 0;
2063 4466758 : idx = 0;
2064 :
2065 29141611 : for ( b = start_band; b < nbands; b++ )
2066 : {
2067 24674853 : if ( bands_entropic[b] )
2068 : {
2069 91290564 : for ( m = 0; m < nblocks; m++ )
2070 : {
2071 72199071 : if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( idx == MASA_LIMIT_IDX_AVG_AZI ) && ( nblocks > 1 ) )
2072 : {
2073 69424 : use_adapt_avg = bitstream[*index];
2074 69424 : ( *index )--;
2075 : }
2076 72199071 : avg_azimuth_index_projected = ivas_dirac_project_azimuth_index( avg_azimuth_index, avg_azimuth_alphabet, azith_alph[b][m] );
2077 72199071 : q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, azith_alph[b][m], gr_param_azith );
2078 72199071 : 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 72199071 : if ( azith_alph[b][m] == 1 )
2081 : {
2082 100586 : q_direction->band_data[b].azimuth[m] = 0.f;
2083 : }
2084 : else
2085 : {
2086 72098485 : 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 72199071 : if ( ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) && ( nblocks > 1 ) )
2089 : {
2090 1261104 : if ( idx < MASA_LIMIT_IDX_AVG_AZI )
2091 : {
2092 398092 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[m], q_direction->band_data[b].elevation[m], direction_vector );
2093 398092 : v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
2094 : }
2095 : else
2096 : {
2097 863012 : if ( use_adapt_avg == 1 )
2098 : {
2099 621584 : if ( m == 0 )
2100 : {
2101 155396 : v_multc( avg_direction_vector, 0.5f, avg_direction_vector, 3 );
2102 : }
2103 : /*compute the average direction per already coded subband */
2104 621584 : ivas_qmetadata_azimuth_elevation_to_direction_vector( q_direction->band_data[b].azimuth[m], q_direction->band_data[b].elevation[m], direction_vector );
2105 621584 : v_add( avg_direction_vector, direction_vector, avg_direction_vector, 3 );
2106 621584 : ivas_qmetadata_direction_vector_to_azimuth_elevation( avg_direction_vector, &avg_azimuth, &avg_elevation );
2107 621584 : avg_azimuth_index = quantize_phi( avg_azimuth + 180, 0, &avg_azimuth, avg_azimuth_alphabet );
2108 : }
2109 : }
2110 1261104 : idx++;
2111 : }
2112 : }
2113 : }
2114 : }
2115 : }
2116 :
2117 5543747 : 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 138271 : 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 138271 : index_start = *index;
2142 138271 : nblocks = q_direction->cfg.nblocks;
2143 :
2144 3057692 : for ( b = start_band; b < nbands; b++ )
2145 : {
2146 12973868 : for ( m = 0; m < nblocks; m++ )
2147 : {
2148 10054447 : value = 0;
2149 141199074 : for ( i = 0; i < q_direction->band_data[b].bits_sph_idx[m]; i++ )
2150 : {
2151 131144627 : value = ( value << 1 ) + bitstream[( *index )--];
2152 : }
2153 10054447 : q_direction->band_data[b].spherical_index[m] = value;
2154 :
2155 10054447 : if ( q_direction->band_data[b].bits_sph_idx[m] == 16 )
2156 : {
2157 4109142 : 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 5945305 : 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 138271 : 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 3017601 : 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 3017601 : index_start = *index;
2191 3017601 : nblocks = q_direction->cfg.nblocks;
2192 :
2193 13666409 : for ( b = start_band; b < nbands; b++ )
2194 : {
2195 10648808 : if ( q_direction->not_in_2D > 0 )
2196 : {
2197 8791665 : decode_fixed_rate( q_direction, bitstream, index, b, nblocks );
2198 : }
2199 : else
2200 : {
2201 1857143 : if ( hrmasa_flag )
2202 : {
2203 0 : diff_idx = 0;
2204 : }
2205 : else
2206 : {
2207 1857143 : diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0];
2208 : }
2209 :
2210 6549991 : for ( m = 0; m < nblocks; m++ )
2211 : {
2212 4692848 : q_direction->band_data[b].elevation[m] = 0.f;
2213 4692848 : q_direction->band_data[b].elevation_index[m] = 0;
2214 4692848 : azith_alph = no_phi_masa[bits_direction_masa[diff_idx] - 1][0];
2215 4692848 : q_direction->band_data[b].azimuth_index[m] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, azith_alph );
2216 4692848 : 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 3017601 : 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 74015708 : 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 74015708 : bits = 30 - norm_l( alphabet_size ); /* bits = floor(log2(alphabet_size)) */
2246 74015708 : tresh = ( 1U << ( bits + 1 ) ) - alphabet_size;
2247 :
2248 74015708 : value = 0;
2249 292966409 : for ( i = 0; i < bits; i++ )
2250 : {
2251 218950701 : value = ( value << 1 ) + bitstream[( *index )--];
2252 : }
2253 :
2254 74015708 : if ( value >= tresh )
2255 : {
2256 10673984 : value = ( value << 1 ) - tresh + bitstream[( *index )--];
2257 : }
2258 :
2259 : #ifdef DEBUGGING
2260 : assert( value < alphabet_size );
2261 : #endif
2262 74015708 : 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 170495174 : 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 170495174 : msb_size = ( alph_size + ( 1 << gr_param ) - 1 ) >> gr_param; /* ceil division */
2285 170495174 : if ( msb_size <= 3 )
2286 : {
2287 9816464 : value = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, alph_size );
2288 : }
2289 : else
2290 : {
2291 160678710 : msb = 0;
2292 282710737 : while ( ( msb < msb_size - 1 ) && ( bitstream[*index] != 0 ) )
2293 : {
2294 122032027 : msb++;
2295 122032027 : ( *index )--;
2296 : }
2297 :
2298 160678710 : if ( msb == msb_size - 1 )
2299 : {
2300 659117 : 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 160019593 : ( *index )--;
2308 160019593 : lsb = 0;
2309 257596193 : for ( i = 0; i < gr_param; i++ )
2310 : {
2311 97576600 : lsb = ( lsb << 1 ) + bitstream[( *index )--];
2312 : }
2313 : }
2314 :
2315 160678710 : value = ( msb << gr_param ) + lsb;
2316 : }
2317 :
2318 : #ifdef DEBUGGING
2319 : assert( value < alph_size );
2320 : #endif
2321 170495174 : 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 124422559 : 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 124422559 : dist_reorder = ivas_qmetadata_dereorder_generic( elev_dist );
2342 124422559 : elev_index_reorder = elev_avg + dist_reorder;
2343 :
2344 124422559 : if ( elev_index_reorder < 0 )
2345 : {
2346 600528 : elev_index_reorder += elev_alph;
2347 : }
2348 123822031 : else if ( elev_index_reorder >= elev_alph )
2349 : {
2350 1140818 : elev_index_reorder -= elev_alph;
2351 : }
2352 :
2353 124422559 : return elev_index_reorder;
2354 : }
2355 :
2356 :
2357 : /*-----------------------------------------------------------------------*
2358 : * Local functions: requentizeEC3
2359 : *-----------------------------------------------------------------------*/
2360 :
2361 : /*! r: number of bits read */
2362 347361 : 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 347361 : bit_pos = *pbit_pos;
2379 :
2380 347361 : diff = 0;
2381 347361 : if ( q_direction->not_in_2D )
2382 : {
2383 333531 : if ( coding_subbands > 1 )
2384 : {
2385 330252 : j = ind_order[coding_subbands - 1];
2386 330252 : allowed_bits = 0;
2387 :
2388 1222404 : for ( k = 0; k < masa_subframes; k++ )
2389 : {
2390 892152 : allowed_bits += q_direction->band_data[j].bits_sph_idx[k];
2391 : }
2392 :
2393 330252 : last_j = j - ( allowed_bits == 0 );
2394 :
2395 1298967 : for ( j = 0; j < last_j; j++ )
2396 : {
2397 968715 : i = ind_order[j];
2398 968715 : bits_dir0 = (int16_t *) q_direction->band_data[i].bits_sph_idx;
2399 :
2400 968715 : nbits = 0;
2401 968715 : allowed_bits = sum_s( bits_dir0, q_direction->cfg.nblocks );
2402 968715 : use_vq = 0;
2403 968715 : max_nb_idx = 0;
2404 :
2405 3460548 : for ( k = 0; k < q_direction->cfg.nblocks; k++ )
2406 : {
2407 2491833 : if ( bits_dir0[k] > use_vq )
2408 : {
2409 1159743 : use_vq = bits_dir0[k];
2410 1159743 : max_nb_idx = k;
2411 : }
2412 : }
2413 :
2414 968715 : if ( q_direction->cfg.nblocks == 1 )
2415 : {
2416 461009 : byteBuffer = 0;
2417 : }
2418 : else
2419 : {
2420 507706 : byteBuffer = 0;
2421 507706 : if ( use_vq <= 1 )
2422 : {
2423 171 : byteBuffer = 1;
2424 : }
2425 507706 : if ( use_vq > 1 && use_vq <= LIMIT_USE_COMMON )
2426 : {
2427 103397 : bits_dir0[max_nb_idx] -= 1;
2428 103397 : allowed_bits -= 1;
2429 : /* read 1 bit to tell if joint of VQ coding */
2430 103397 : byteBuffer = bitstream[bit_pos--];
2431 : }
2432 : }
2433 :
2434 3460548 : for ( k = 0; k < masa_subframes; k++ )
2435 : {
2436 2491833 : q_direction->band_data[i].bits_sph_idx[k] = bits_dir0[k];
2437 2491833 : if ( bits_dir0[k] > 2 )
2438 : {
2439 2361040 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2440 : {
2441 494110 : q_direction->band_data[i].elevation_m_alphabet[k] = no_theta_masa[bits_dir0[k] - 3];
2442 : }
2443 : else
2444 : {
2445 1866930 : q_direction->band_data[i].elevation_m_alphabet[k] = no_theta_masa[bits_dir0[k] - 3] * 2 - 1;
2446 : }
2447 : }
2448 : else
2449 : {
2450 130793 : q_direction->band_data[i].elevation_m_alphabet[k] = 1;
2451 : }
2452 : }
2453 :
2454 968715 : if ( allowed_bits > 0 )
2455 : {
2456 968715 : if ( byteBuffer == 1 )
2457 : {
2458 101164 : nbits = read_common_direction( bitstream, q_direction, i, masa_subframes, allowed_bits, &bit_pos );
2459 : }
2460 : else
2461 : {
2462 867551 : 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 252218 : fixed_rate = 1;
2466 252218 : nbits = 0;
2467 : }
2468 : else
2469 : {
2470 : /* check if fixed_rate */
2471 615333 : fixed_rate = bitstream[bit_pos--];
2472 615333 : nbits = 1;
2473 : }
2474 :
2475 867551 : if ( fixed_rate == 1 )
2476 : {
2477 : /* decode_fixed_rate()*/
2478 650862 : nbits += decode_fixed_rate( q_direction, bitstream, &bit_pos, i, masa_subframes );
2479 : }
2480 : else
2481 : {
2482 : /* decode elevation */
2483 216689 : nbits += decode_elevation( q_direction, bitstream, &bit_pos, i, masa_subframes );
2484 :
2485 : /* decode azimuth */
2486 216689 : 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 968715 : diff += nbits - allowed_bits;
2495 :
2496 : /* update bits for next block */
2497 968715 : update_bits_next_block( q_direction, &diff, ind_order[j + 1], coding_subbands, masa_subframes );
2498 : }
2499 : }
2500 : else
2501 : {
2502 3279 : last_j = q_direction->cfg.start_band;
2503 : }
2504 :
2505 :
2506 872726 : for ( j = last_j; j < coding_subbands; j++ )
2507 : {
2508 539195 : i = ind_order[j];
2509 539195 : bits_dir0 = (int16_t *) q_direction->band_data[i].bits_sph_idx;
2510 :
2511 539195 : nbits = 0;
2512 539195 : allowed_bits = sum_s( bits_dir0, q_direction->cfg.nblocks );
2513 539195 : if ( allowed_bits > 0 && masa_subframes == 1 )
2514 : {
2515 256639 : nbits += decode_fixed_rate( q_direction, bitstream, &bit_pos, i, masa_subframes );
2516 : }
2517 : else
2518 : {
2519 282556 : if ( allowed_bits > 0 )
2520 : {
2521 282556 : use_vq = 0;
2522 282556 : max_nb_idx = 0;
2523 1412780 : for ( k = 0; k < masa_subframes; k++ )
2524 : {
2525 1130224 : if ( bits_dir0[k] > use_vq )
2526 : {
2527 412810 : use_vq = bits_dir0[k];
2528 412810 : max_nb_idx = k;
2529 : }
2530 : }
2531 :
2532 282556 : byteBuffer = 0;
2533 :
2534 282556 : if ( use_vq > 1 && use_vq <= LIMIT_USE_COMMON )
2535 : {
2536 38241 : bits_dir0[max_nb_idx] -= 1;
2537 38241 : allowed_bits -= 1;
2538 :
2539 : /* read 1 bit to tell if joint of VQ coding */
2540 38241 : byteBuffer = bitstream[bit_pos--];
2541 : }
2542 :
2543 282556 : if ( allowed_bits > 0 )
2544 : {
2545 282556 : if ( byteBuffer == 1 || use_vq <= 1 )
2546 : {
2547 37804 : nbits = read_common_direction( bitstream, q_direction, i, masa_subframes, allowed_bits, &bit_pos );
2548 : }
2549 : else
2550 : {
2551 : /* decode_fixed_rate()*/
2552 244752 : 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 70681 : for ( j = 0; j < coding_subbands; j++ )
2571 : {
2572 216638 : for ( k = 0; k < q_direction->cfg.nblocks; k++ )
2573 : {
2574 159787 : q_direction->band_data[j].elevation[k] = 0;
2575 159787 : q_direction->band_data[j].elevation_index[k] = 0;
2576 : }
2577 : }
2578 13830 : nbits = decode_azimuth2D( q_direction, bitstream, coding_subbands, &bit_pos, masa_subframes );
2579 : }
2580 347361 : nbits = *pbit_pos - bit_pos;
2581 347361 : *pbit_pos = bit_pos;
2582 :
2583 347361 : 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 216689 : 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 216689 : nbits = 0;
2608 216689 : bit_pos = *pbit_pos;
2609 216689 : byteBuffer = 0;
2610 :
2611 216689 : j_az = 0;
2612 : /* check number of valid indexes to decode */
2613 1061839 : for ( k = 0; k < masa_subframes; k++ )
2614 : {
2615 845150 : if ( q_direction->band_data[idx_subband].azimuth_index[k] < MASA_NO_INDEX )
2616 : {
2617 844295 : j_az++;
2618 : }
2619 : else
2620 : {
2621 855 : q_direction->band_data[idx_subband].azimuth[k] = 0.f; /*To be in sync with encoder values.*/
2622 : }
2623 : }
2624 :
2625 216689 : if ( j_az == 0 )
2626 : {
2627 80 : return nbits;
2628 : }
2629 :
2630 216609 : if ( byteBuffer == 0 )
2631 : {
2632 : /* use context */
2633 216609 : use_context = 0;
2634 1061439 : for ( k = 0; k < masa_subframes; k++ )
2635 : {
2636 844830 : if ( q_direction->band_data[idx_subband].bits_sph_idx[k] <= 1 )
2637 : {
2638 0 : use_context = 1;
2639 : }
2640 : }
2641 :
2642 216609 : 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 216609 : byteBuffer = bitstream[bit_pos--];
2673 216609 : if ( byteBuffer == 0 ) /* regular GR coding5 */
2674 : {
2675 : /* read GR_order */
2676 90231 : byteBuffer = bitstream[bit_pos--];
2677 90231 : nbits += 1;
2678 :
2679 429549 : for ( k = 0; k < masa_subframes; k++ )
2680 : {
2681 339318 : if ( q_direction->band_data[idx_subband].bits_sph_idx[k] > 0 )
2682 : {
2683 339318 : 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 338821 : 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 338821 : 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 497 : q_direction->band_data[idx_subband].azimuth[k] = 0;
2691 497 : 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 126378 : byteBuffer = bitstream[bit_pos--];
2706 : /* read min index value */
2707 126378 : maximum_s( q_direction->band_data[idx_subband].azimuth_m_alphabet, masa_subframes, &max_val );
2708 126378 : min_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, max_val, MASA_GR_ORD_AZ );
2709 :
2710 631890 : for ( k = 0; k < masa_subframes; k++ )
2711 : {
2712 505512 : if ( q_direction->band_data[idx_subband].bits_sph_idx[k] > 0 )
2713 : {
2714 505512 : 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 505474 : 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 505474 : q_direction->band_data[idx_subband].azimuth_index[k] += min_idx;
2718 505474 : 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 38 : q_direction->band_data[idx_subband].azimuth[k] = 0;
2723 38 : 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 216609 : nbits = *pbit_pos - bit_pos;
2737 :
2738 216609 : *pbit_pos = bit_pos;
2739 :
2740 216609 : 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 216689 : 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 216689 : nr_NO_INDEX = 0;
2766 216689 : nbits = 0;
2767 216689 : bit_pos = *pbit_pos;
2768 :
2769 1061839 : for ( k = 0; k < masa_subframes; k++ )
2770 : {
2771 845150 : q_direction->band_data[j].elevation_index[k] = 0;
2772 845150 : q_direction->band_data[j].elevation[k] = 0;
2773 :
2774 845150 : if ( q_direction->band_data[j].bits_sph_idx[k] > 0 )
2775 : {
2776 845150 : if ( q_direction->band_data[j].bits_sph_idx[k] <= 2 )
2777 : {
2778 17 : q_direction->band_data[j].elevation_index[k] = MASA_NO_INDEX;
2779 17 : nr_NO_INDEX += 1;
2780 17 : q_direction->band_data[j].elevation[k] = 0;
2781 17 : q_direction->band_data[j].elevation_m_alphabet[k] = 1;
2782 : }
2783 : else
2784 : {
2785 845133 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2786 : {
2787 244434 : 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 600699 : 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 216689 : if ( nr_NO_INDEX < masa_subframes )
2802 : {
2803 : {
2804 : /* read if same or not */
2805 216689 : byteBuffer = bitstream[bit_pos--];
2806 216689 : if ( byteBuffer == 1 ) /* same value */
2807 : {
2808 : /* read value */
2809 173100 : byteBuffer = bitstream[bit_pos--];
2810 173100 : byteBuffer = ( byteBuffer << 1 ) + bitstream[bit_pos--];
2811 173100 : same_idx = byteBuffer;
2812 :
2813 843900 : for ( k = 0; k < masa_subframes; k++ )
2814 : {
2815 670800 : if ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX )
2816 : {
2817 670800 : q_direction->band_data[j].elevation_index[k] = same_idx;
2818 670800 : 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 43589 : byteBuffer = bitstream[bit_pos--];
2826 43589 : GR_ord_elevation = MASA_GR_ORD_EL - byteBuffer;
2827 :
2828 217939 : for ( k = 0; k < masa_subframes; k++ )
2829 : {
2830 174350 : if ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX )
2831 : {
2832 174333 : 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 174333 : 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 1061839 : for ( k = 0; k < masa_subframes; k++ )
2841 : {
2842 845150 : if ( ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX ) &&
2843 845133 : ( no_phi_masa[q_direction->band_data[j].bits_sph_idx[k] - 1][q_direction->band_data[j].elevation_index[k]] <= 1 ) )
2844 : {
2845 855 : q_direction->band_data[j].azimuth_index[k] = MASA_NO_INDEX;
2846 855 : q_direction->band_data[j].azimuth_m_alphabet[k] = 1;
2847 : }
2848 : else
2849 : {
2850 844295 : q_direction->band_data[j].azimuth_index[k] = 0;
2851 844295 : if ( q_direction->band_data[j].elevation_index[k] < MASA_NO_INDEX )
2852 : {
2853 844278 : 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 17 : q_direction->band_data[j].azimuth_m_alphabet[k] = no_phi_masa[q_direction->band_data[j].bits_sph_idx[k] - 1][0];
2858 17 : q_direction->band_data[j].elevation_index[k] = 0;
2859 : }
2860 : }
2861 : }
2862 :
2863 216689 : nbits = *pbit_pos - bit_pos;
2864 216689 : *pbit_pos = bit_pos;
2865 :
2866 216689 : 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 15971857 : 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 15971857 : nbits = 0;
2889 :
2890 69857768 : for ( m = 0; m < nblocks; m++ )
2891 : {
2892 53885911 : value = 0;
2893 282553544 : for ( i = 0; i < q_direction->band_data[b].bits_sph_idx[m]; i++ )
2894 : {
2895 228667633 : value = ( value << 1 ) + bitstream[( *pbit_pos )--];
2896 : }
2897 :
2898 53885911 : q_direction->band_data[b].spherical_index[m] = value;
2899 53885911 : nbits += q_direction->band_data[b].bits_sph_idx[m];
2900 :
2901 53885911 : 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 15971857 : 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 13830 : 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 13830 : bit_pos = *pbit_pos;
2930 13830 : nbits = 0;
2931 70681 : for ( j = 0; j < coding_subbands; j++ )
2932 : {
2933 56851 : bits_dir0 = (int16_t *) q_direction->band_data[j].bits_sph_idx;
2934 56851 : allowed_bits = sum_s( bits_dir0, no_frames );
2935 :
2936 56851 : if ( allowed_bits > 0 )
2937 : {
2938 56851 : use_vq = 0;
2939 216638 : for ( k = 0; k < no_frames; k++ )
2940 : {
2941 159787 : q_direction->band_data[j].elevation[k] = 0;
2942 159787 : q_direction->band_data[j].elevation_index[k] = 0;
2943 :
2944 159787 : if ( bits_dir0[k] > use_vq )
2945 : {
2946 65722 : use_vq = bits_dir0[k];
2947 : }
2948 : }
2949 :
2950 56851 : if ( use_vq <= 3 && allowed_bits <= 11 )
2951 : {
2952 2543 : if ( allowed_bits <= no_frames + 1 )
2953 : {
2954 554 : set_f( q_direction->band_data[j].azimuth, 0.0f, no_frames );
2955 2677 : for ( k = 0; k < min( allowed_bits, no_frames ); k++ )
2956 : {
2957 2123 : q_direction->band_data[j].azimuth[k] = ( -180.f ) * bitstream[bit_pos--];
2958 2123 : nbits += 1;
2959 : }
2960 : }
2961 : else
2962 : {
2963 1989 : nbits += read_truncGR_azimuth( bitstream, q_direction, j, no_frames, &bit_pos );
2964 : }
2965 : }
2966 : else
2967 : {
2968 208474 : for ( k = 0; k < no_frames; k++ )
2969 : {
2970 154166 : Buffer = 0;
2971 896247 : for ( i = 0; i < bits_dir0[k]; i++ )
2972 : {
2973 742081 : Buffer = ( Buffer << 1 ) + bitstream[bit_pos--];
2974 : }
2975 :
2976 154166 : nbits += bits_dir0[k];
2977 :
2978 154166 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
2979 : {
2980 67016 : q_direction->band_data[j].azimuth[k] = 360.0f / (float) ( 1 << bits_dir0[k] ) * Buffer - 180;
2981 67016 : q_direction->band_data[j].azimuth[k] = companding_azimuth( q_direction->band_data[j].azimuth[k], q_direction->cfg.mc_ls_setup,
2982 67016 : ( q_direction->band_data[j].elevation[k] > MC_MASA_THR_ELEVATION ), -1 );
2983 : }
2984 : else
2985 : {
2986 87150 : 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 13830 : *pbit_pos = bit_pos;
2997 :
2998 13830 : return nbits;
2999 : }
3000 :
3001 :
3002 : /*-------------------------------------------------------------------*
3003 : * set_zero_direction()
3004 : *
3005 : *
3006 : *-------------------------------------------------------------------*/
3007 :
3008 138968 : 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 694840 : for ( k = 0; k < len; k++ )
3016 : {
3017 555872 : q_direction->band_data[idx_band].azimuth[k] = 0;
3018 555872 : q_direction->band_data[idx_band].azimuth_index[k] = 0;
3019 555872 : q_direction->band_data[idx_band].elevation[k] = 0;
3020 555872 : q_direction->band_data[idx_band].elevation_index[k] = 0;
3021 555872 : q_direction->band_data[idx_band].spherical_index[k] = 0;
3022 : }
3023 :
3024 138968 : return;
3025 : }
3026 :
3027 :
3028 : /*-------------------------------------------------------------------*
3029 : * read_truncGR_azimuth()
3030 : *
3031 : *
3032 : *-------------------------------------------------------------------*/
3033 :
3034 138945 : 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 138945 : allowed_bits = sum_s( (int16_t *) q_direction->band_data[j].bits_sph_idx, no_subframes );
3048 138945 : nbits = 0;
3049 138945 : 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 138945 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3068 : {
3069 36300 : no_symb = 9;
3070 : }
3071 : else
3072 : {
3073 102645 : no_symb = 8;
3074 : }
3075 138945 : nbits = 0;
3076 :
3077 138945 : nbits = *pbit_pos;
3078 :
3079 690267 : for ( i = 0; i < no_subframes; i++ )
3080 : {
3081 551322 : idx = ivas_qmetadata_DecodeExtendedGR( bitstream, pbit_pos, no_symb, 0 );
3082 551322 : q_direction->band_data[j].azimuth_index[i] = idx;
3083 551322 : if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID )
3084 : {
3085 143460 : q_direction->band_data[j].azimuth[i] = cb_azi_chan[( idx + 1 ) >> 1];
3086 143460 : if ( idx % 2 > 0 )
3087 : {
3088 45258 : q_direction->band_data[j].azimuth[i] = -q_direction->band_data[j].azimuth[i];
3089 : }
3090 : }
3091 : else
3092 : {
3093 407862 : q_direction->band_data[j].azimuth[i] = azimuth_cb[idx];
3094 : }
3095 : }
3096 :
3097 138945 : nbits -= *pbit_pos;
3098 :
3099 138945 : return nbits;
3100 : }
3101 :
3102 :
3103 : /*-------------------------------------------------------------------*
3104 : * read_common_direction()
3105 : *
3106 : *
3107 : *-------------------------------------------------------------------*/
3108 :
3109 : /*! r: number of bits read */
3110 138968 : 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 138968 : bit_pos = *pbit_pos;
3126 138968 : nbits = 0;
3127 :
3128 138968 : set_zero_direction( q_direction, j, no_subframes );
3129 138968 : if ( bits_total == 0 )
3130 : {
3131 0 : return nbits;
3132 : }
3133 :
3134 138968 : if ( bits_total <= no_subframes + 1 )
3135 : {
3136 2020 : for ( i = 0; i < min( no_subframes, bits_total ); i++ )
3137 : {
3138 1616 : byteBuffer = bitstream[bit_pos--];
3139 : /*qdirection->azimuth_index[j][i] = (uint16_t)byteBuffer; */
3140 1616 : q_direction->band_data[j].azimuth[i] = azimuth_cb[byteBuffer];
3141 1616 : nbits += 1;
3142 : }
3143 404 : *pbit_pos = bit_pos;
3144 :
3145 : /*nbits += read_truncGR_azimuth(bitstream, qdirection, j, no_subframes, pbit_pos); */
3146 404 : return nbits;
3147 : }
3148 :
3149 :
3150 138564 : byteBuffer = bitstream[bit_pos--];
3151 138564 : bits_el = 1;
3152 138564 : nbits += 1;
3153 : /* elevation is already set to 0*/
3154 138564 : if ( byteBuffer == 1 )
3155 : {
3156 66636 : byteBuffer = bitstream[bit_pos--];
3157 66636 : bits_el += 1;
3158 66636 : nbits += 1;
3159 66636 : if ( byteBuffer == 0 )
3160 : {
3161 177815 : for ( i = 0; i < no_subframes; i++ )
3162 : {
3163 142252 : q_direction->band_data[j].elevation[i] = delta_theta_masa[2];
3164 : }
3165 : }
3166 : else
3167 : {
3168 31073 : byteBuffer = bitstream[bit_pos--];
3169 31073 : bits_el += 1;
3170 31073 : nbits += 1;
3171 31073 : if ( byteBuffer == 0 )
3172 : {
3173 148585 : for ( i = 0; i < no_subframes; i++ )
3174 : {
3175 118868 : 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 1356 : byteBuffer = bitstream[bit_pos--];
3182 1356 : nbits += 1;
3183 1356 : if ( byteBuffer == 0 )
3184 : {
3185 1280 : set_f( q_direction->band_data[j].elevation, 90.0f, no_subframes );
3186 1280 : set_f( q_direction->band_data[j].azimuth, 0.0f, no_subframes );
3187 : }
3188 : else
3189 : {
3190 76 : set_f( q_direction->band_data[j].elevation, -90.0f, no_subframes );
3191 76 : set_f( q_direction->band_data[j].azimuth, 0.0f, no_subframes );
3192 : }
3193 1356 : *pbit_pos = bit_pos;
3194 :
3195 1356 : return nbits;
3196 : }
3197 : }
3198 : }
3199 :
3200 137208 : bits_el = sum_s( (int16_t *) q_direction->band_data[j].bits_sph_idx, no_subframes ) - bits_el;
3201 :
3202 137208 : if ( bits_el <= no_subframes + 1 )
3203 : {
3204 252 : nbits += min( no_subframes, bits_el );
3205 1255 : for ( i = 0; i < min( no_subframes, bits_el ); i++ )
3206 : {
3207 1003 : byteBuffer = bitstream[bit_pos--];
3208 : /*qdirection->azimuth_index[j][i] = (uint16_t) byteBuffer; */
3209 1003 : q_direction->band_data[j].azimuth[i] = azimuth_cb[byteBuffer];
3210 : }
3211 : }
3212 : else
3213 : {
3214 136956 : nbits += read_truncGR_azimuth( bitstream, q_direction, j, no_subframes, &bit_pos );
3215 : }
3216 :
3217 137208 : *pbit_pos = bit_pos;
3218 :
3219 : #ifdef DEBUGGING
3220 : /*assert(nbits == bits_total); */
3221 : #endif
3222 137208 : return nbits;
3223 : }
3224 :
3225 :
3226 : /*-----------------------------------------------------------------------*
3227 : * Local functions: coherence
3228 : *-----------------------------------------------------------------------*/
3229 :
3230 899392 : 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 899392 : coding_subbands_0 = hQMetaData->q_direction[0].cfg.nbands;
3247 899392 : coding_subbands = hQMetaData->q_direction[idx_d].cfg.nbands;
3248 899392 : if ( coding_subbands_0 <= 5 )
3249 : {
3250 3585294 : for ( j = 0; j < 5; j++ )
3251 : {
3252 2987745 : MASA_grouping[j] = j;
3253 : }
3254 : }
3255 : else
3256 : {
3257 301843 : if ( coding_subbands_0 <= 8 )
3258 : {
3259 88815 : mvs2s( MASA_grouping_8_to_5, MASA_grouping, 8 );
3260 : }
3261 213028 : else if ( coding_subbands_0 <= 12 )
3262 : {
3263 36225 : mvs2s( MASA_grouping_12_to_5, MASA_grouping, 12 );
3264 : }
3265 176803 : else if ( coding_subbands_0 <= 18 )
3266 : {
3267 83817 : mvs2s( MASA_grouping_18_to_5, MASA_grouping, 18 );
3268 : }
3269 : else
3270 : {
3271 92986 : if ( coding_subbands_0 <= 24 )
3272 : {
3273 92986 : mvs2s( MASA_grouping_24_to_5, MASA_grouping, 24 );
3274 : }
3275 : }
3276 : }
3277 :
3278 899392 : if ( coding_subbands < coding_subbands_0 )
3279 : {
3280 95621 : d = 0;
3281 845673 : for ( j = 0; j < coding_subbands_0; j++ )
3282 : {
3283 750052 : if ( hQMetaData->twoDirBands[j] == 1 )
3284 : {
3285 272566 : two_dir_band[d++] = j;
3286 : }
3287 : }
3288 : }
3289 : else
3290 : {
3291 803771 : set_s( two_dir_band, 0, coding_subbands );
3292 : }
3293 :
3294 899392 : q_direction = &hQMetaData->q_direction[idx_d];
3295 :
3296 8205241 : for ( i = 0; i < coding_subbands; i++ )
3297 : {
3298 7305849 : var_azi = var( q_direction->band_data[i].azimuth, no_frames );
3299 :
3300 7305849 : 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 7305849 : min_index = q_direction->band_data[i].energy_ratio_index[0];
3308 : }
3309 :
3310 7305849 : if ( var_azi < MASA_DELTA_AZI_DCT0 )
3311 : {
3312 2226674 : idx_sub_cb = MASA_NO_CV_COH * min_index;
3313 : }
3314 : else
3315 : {
3316 5079175 : idx_sub_cb = MASA_NO_CV_COH * ( min_index + DIRAC_DIFFUSE_LEVELS ); /* NO_CV_COH = 8 */
3317 : }
3318 :
3319 7305849 : dct_coh[i][0] = coherence_cb0_masa[idx_sub_cb + q_direction->coherence_band_data[i].spread_coherence_dct0_index];
3320 :
3321 7305849 : if ( coding_subbands < coding_subbands_0 )
3322 : {
3323 272566 : assert( idx_d == 1 );
3324 272566 : 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 7033283 : 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 21917547 : for ( j = 2; j < MAX_PARAM_SPATIAL_SUBFRAMES; j++ )
3332 : {
3333 14611698 : dct_coh[i][j] = 0;
3334 : }
3335 :
3336 7305849 : invdct4_transform( dct_coh[i], q_direction->coherence_band_data[i].spread_coherence );
3337 : }
3338 :
3339 899392 : return;
3340 : }
3341 :
3342 :
3343 : /*-------------------------------------------------------------------*
3344 : * read_huf()
3345 : *
3346 : * Read Hufman code
3347 : *-------------------------------------------------------------------*/
3348 :
3349 : /*! r: number of bits read */
3350 899392 : 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 899392 : int16_t done = 0, end_pos;
3361 : uint16_t ByteBuffer;
3362 : int16_t nbits, val;
3363 : uint16_t i;
3364 :
3365 899392 : end_pos = start_pos;
3366 899392 : nbits = 0;
3367 899392 : val = 0;
3368 4356370 : while ( ( done == 0 ) && ( nbits < max_len ) )
3369 : {
3370 3456978 : ByteBuffer = bitstream[end_pos--];
3371 3456978 : val = val * 2 + ( ByteBuffer & 1 );
3372 3456978 : nbits += 1;
3373 16322517 : for ( i = 0; i < len; i++ )
3374 : {
3375 13764931 : if ( val == huff_code[i] )
3376 : {
3377 899392 : *out = i;
3378 899392 : done = 1;
3379 899392 : 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 899392 : *num_bits_read = end_pos;
3391 :
3392 899392 : return IVAS_ERR_OK;
3393 : }
3394 :
3395 :
3396 : /*-------------------------------------------------------------------*
3397 : * read_GR_min_removed_data()
3398 : *
3399 : *
3400 : *-------------------------------------------------------------------*/
3401 :
3402 136383 : 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 136383 : bit_pos = *p_bit_pos;
3417 :
3418 : /* read GR order */
3419 136383 : byteBuffer = bitstream[bit_pos--];
3420 136383 : nbits = 1;
3421 :
3422 : /* read min index */
3423 136383 : bits_GR = bit_pos;
3424 136383 : min_index = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_symb, 0 );
3425 136383 : nbits += bits_GR - bit_pos;
3426 :
3427 : /* read GR data */
3428 1881173 : for ( j = 0; j < no_data; j++ )
3429 : {
3430 1744790 : bits_GR = bit_pos;
3431 1744790 : if ( no_cv_vec[j] > 1 )
3432 : {
3433 1735128 : decoded_idx[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_cv_vec[j] - min_index, ( byteBuffer & 1 ) );
3434 1735128 : nbits += bits_GR - bit_pos;
3435 : }
3436 : else
3437 : {
3438 9662 : decoded_idx[j] = 0;
3439 : }
3440 : }
3441 :
3442 1881173 : for ( j = 0; j < no_data; j++ )
3443 : {
3444 1744790 : if ( no_cv_vec[j] > 1 )
3445 : {
3446 1735128 : decoded_idx[j] += min_index;
3447 : }
3448 : }
3449 :
3450 : #ifdef DEBUGGING
3451 : assert( nbits == *p_bit_pos - bit_pos );
3452 : #endif
3453 136383 : *p_bit_pos = bit_pos;
3454 :
3455 136383 : return nbits;
3456 : }
3457 :
3458 :
3459 : /*-------------------------------------------------------------------*
3460 : * decode_fixed_rate_composed_index_coherence()
3461 : *
3462 : *
3463 : *-------------------------------------------------------------------*/
3464 :
3465 383962 : 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 383962 : bit_pos = *p_bit_pos;
3485 383962 : set_s( (int16_t *) temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
3486 :
3487 383962 : no_cb = 1;
3488 383962 : nbits = 0;
3489 383962 : if ( no_bands > MASA_LIMIT_NO_BANDS_SUR_COH )
3490 : {
3491 : /* read 8-max_val with GR0 */
3492 143632 : bits_GR = bit_pos;
3493 143632 : no_vals_local = no_symb - ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_symb, 0 );
3494 143632 : nbits += bits_GR - bit_pos;
3495 2827617 : for ( j = 0; j < no_bands; j++ )
3496 : {
3497 2683985 : if ( no_cv_vec[j] > no_vals_local )
3498 : {
3499 1940025 : no_cv_vec[j] = no_vals_local;
3500 : }
3501 : }
3502 : }
3503 :
3504 383962 : half_no_bands = no_bands / 2;
3505 383962 : if ( sum_s( no_cv_vec, no_bands ) > MASA_COH_LIMIT_2IDX )
3506 : {
3507 44866 : no_cb = 1;
3508 :
3509 539703 : for ( j = 0; j < half_no_bands; j++ )
3510 : {
3511 494837 : no_cb *= no_cv_vec[j];
3512 : }
3513 44866 : no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
3514 44866 : no_cb = 1;
3515 545246 : for ( j = half_no_bands; j < no_bands; j++ )
3516 : {
3517 500380 : no_cb *= no_cv_vec[j];
3518 : }
3519 44866 : no_bits_vec1 = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
3520 : }
3521 : else
3522 : {
3523 339096 : no_cb = 1;
3524 3116342 : for ( j = 0; j < no_bands; j++ )
3525 : {
3526 2777246 : no_cb *= no_cv_vec[j];
3527 : }
3528 339096 : no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
3529 339096 : no_bits_vec1 = 0;
3530 : }
3531 383962 : if ( no_bits_vec1 > 0 )
3532 : {
3533 44861 : idx_fr = 0;
3534 1391472 : for ( j = 0; j < no_bits_vec; j++ )
3535 : {
3536 1346611 : idx_fr = ( idx_fr << 1 ) + bitstream[bit_pos--];
3537 : }
3538 :
3539 44861 : nbits += no_bits_vec;
3540 :
3541 44861 : decode_combined_index( idx_fr, no_cv_vec, temp_index, half_no_bands );
3542 :
3543 44861 : idx_fr = 0;
3544 1409008 : for ( j = 0; j < no_bits_vec1; j++ )
3545 : {
3546 1364147 : idx_fr = ( idx_fr << 1 ) + bitstream[bit_pos--];
3547 : }
3548 44861 : nbits += no_bits_vec1;
3549 44861 : decode_combined_index( idx_fr, &no_cv_vec[half_no_bands], &temp_index[half_no_bands], half_no_bands );
3550 : }
3551 : else
3552 : {
3553 339101 : idx_fr = 0;
3554 3666400 : for ( j = 0; j < no_bits_vec; j++ )
3555 : {
3556 3327299 : idx_fr = ( idx_fr << 1 ) + bitstream[bit_pos--];
3557 : }
3558 339101 : nbits += no_bits_vec;
3559 339101 : decode_combined_index( idx_fr, no_cv_vec, temp_index, no_bands );
3560 : }
3561 :
3562 4156425 : for ( j = 0; j < no_bands; j++ )
3563 : {
3564 3772463 : decoded_index[j] = temp_index[j];
3565 : }
3566 : #ifdef DEBUGGING
3567 : assert( nbits == *p_bit_pos - bit_pos );
3568 : #endif
3569 383962 : nbits = *p_bit_pos - bit_pos;
3570 :
3571 383962 : *p_bit_pos = bit_pos;
3572 :
3573 383962 : 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 127691 : 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 127691 : nbands = hQMetaData->q_direction[idx_dir].cfg.nbands;
3599 127691 : nblocks = hQMetaData->q_direction[idx_dir].cfg.nblocks;
3600 :
3601 127691 : cb_size = 1 << nbits_coh;
3602 127691 : delta = 256.0f / cb_size;
3603 127691 : nbits = *p_bit_pos;
3604 577270 : for ( k = 0; k < nblocks; k++ )
3605 : {
3606 : /* read method */
3607 449579 : if ( bitstream[( *p_bit_pos )--] == 1 )
3608 : {
3609 : /* average removed */
3610 : /* read average index */
3611 51977 : min_index = 0;
3612 230559 : for ( i = 0; i < nbits_coh; i++ )
3613 : {
3614 178582 : min_index = ( min_index << 1 ) + bitstream[( *p_bit_pos )--];
3615 : }
3616 : /* read GR param */
3617 51977 : GR_param = bitstream[( *p_bit_pos )--];
3618 1168613 : for ( j = 0; j < nbands; j++ )
3619 : {
3620 1116636 : decoded_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, p_bit_pos, 2 * cb_size, GR_param );
3621 1116636 : if ( decoded_idx % 2 )
3622 : {
3623 305825 : decoded_idx = ( ( decoded_idx + 1 ) >> 1 ) + min_index;
3624 : }
3625 : else
3626 : {
3627 810811 : decoded_idx = -( decoded_idx >> 1 ) + min_index;
3628 : }
3629 1116636 : 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 397602 : min_index = 0;
3636 1738106 : for ( i = 0; i < nbits_coh; i++ )
3637 : {
3638 1340504 : min_index = ( min_index << 1 ) + bitstream[( *p_bit_pos )--];
3639 : }
3640 :
3641 : /* read GR param */
3642 397602 : GR_param = bitstream[( *p_bit_pos )--];
3643 8824131 : for ( j = 0; j < nbands; j++ )
3644 : {
3645 8426529 : decoded_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, p_bit_pos, cb_size - min_index, GR_param ) + min_index;
3646 8426529 : 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 127691 : nbits = nbits - *p_bit_pos;
3652 :
3653 127691 : return nbits;
3654 : }
3655 :
3656 :
3657 : /*------------------------------------------------------------------- *
3658 : * read_coherence_data()
3659 : *
3660 : * Read coherence data
3661 : *------------------------------------------------------------------- */
3662 :
3663 : /*! r: number of bits read */
3664 1167637 : 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 1167637 : coding_subbands = hQMetaData->q_direction[idx_dir].cfg.nbands;
3691 1167637 : extra_cv = (int16_t) ( coding_subbands / MASA_FACTOR_CV_COH );
3692 1167637 : q_direction = &( hQMetaData->q_direction[idx_dir] );
3693 1167637 : bit_pos = *p_bit_pos;
3694 1167637 : nbits = 0;
3695 :
3696 1167637 : if ( q_direction->cfg.nblocks == 1 )
3697 : {
3698 2951205 : for ( j = 0; j < coding_subbands; j++ )
3699 : {
3700 2682960 : 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 2682960 : idx_ER = 7 - q_direction->band_data[j].energy_ratio_index_mod[0] + extra_cv;
3707 : }
3708 2682960 : no_cv_vec[j] = idx_ER + 1;
3709 : }
3710 :
3711 268245 : if ( sum_s( no_cv_vec, coding_subbands ) == coding_subbands )
3712 : {
3713 49928 : for ( j = 0; j < coding_subbands; j++ )
3714 : {
3715 40388 : q_direction->coherence_band_data[j].spread_coherence[0] = 0;
3716 : }
3717 :
3718 9540 : return 0;
3719 : }
3720 258705 : byteBuffer = bitstream[bit_pos--];
3721 258705 : nbits += 1;
3722 :
3723 258705 : if ( byteBuffer & 1 )
3724 : {
3725 : /* decode GR min removed */
3726 136383 : 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 1881173 : for ( j = 0; j < coding_subbands; j++ )
3728 : {
3729 1744790 : if ( no_cv_vec[j] > 1 )
3730 : {
3731 1735128 : 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 9662 : 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 122322 : 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 1020104 : for ( j = 0; j < coding_subbands; j++ )
3745 : {
3746 897782 : if ( no_cv_vec[j] > 1 )
3747 : {
3748 352587 : 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 545195 : q_direction->coherence_band_data[j].spread_coherence[0] = 0;
3753 : }
3754 : }
3755 : }
3756 : }
3757 : else
3758 : {
3759 8205241 : for ( j = 0; j < coding_subbands; j++ )
3760 : {
3761 7305849 : 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 7305849 : no_cv_vec[j] = len_cb_dct0_masa[q_direction->band_data[j].energy_ratio_index[0]]; /* spread coherence DCT0*/
3769 : }
3770 : }
3771 :
3772 899392 : 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 18866 : no_cb = 1;
3777 : #ifdef DEBUGGING
3778 : assert( coding_subbands % 2 == 0 );
3779 : #endif
3780 234002 : for ( j = 0; j < coding_subbands / 2; j++ )
3781 : {
3782 215136 : no_cb *= no_cv_vec[j];
3783 : }
3784 :
3785 18866 : no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
3786 18866 : no_cb = 1;
3787 :
3788 234002 : for ( j = coding_subbands / 2; j < coding_subbands; j++ )
3789 : {
3790 215136 : no_cb *= no_cv_vec[j];
3791 : }
3792 :
3793 18866 : no_bits_vec1 = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
3794 18866 : dct0_index = 0;
3795 :
3796 518183 : for ( j = 0; j < no_bits_vec; j++ )
3797 : {
3798 499317 : dct0_index = ( dct0_index << 1 ) + bitstream[bit_pos--];
3799 : }
3800 :
3801 18866 : nbits += no_bits_vec;
3802 18866 : set_s( (int16_t *) spr_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
3803 :
3804 18866 : decode_combined_index( dct0_index, no_cv_vec, spr_coh_temp_index, coding_subbands / 2 );
3805 :
3806 18866 : dct0_index = 0;
3807 506278 : for ( j = 0; j < no_bits_vec1; j++ )
3808 : {
3809 487412 : dct0_index = ( dct0_index << 1 ) + bitstream[bit_pos--];
3810 : }
3811 :
3812 18866 : nbits += no_bits_vec1;
3813 :
3814 18866 : decode_combined_index( dct0_index, &no_cv_vec[coding_subbands / 2], &spr_coh_temp_index[coding_subbands / 2], coding_subbands / 2 );
3815 :
3816 449138 : for ( j = 0; j < coding_subbands; j++ )
3817 : {
3818 430272 : 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 880526 : no_cb = 1;
3827 :
3828 7756103 : for ( j = 0; j < coding_subbands; j++ )
3829 : {
3830 6875577 : no_cb *= no_cv_vec[j];
3831 : }
3832 :
3833 880526 : no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
3834 :
3835 : /* read joint index for DCT0 */
3836 880526 : no_bits_vec = (int16_t) ceilf( logf( (float) no_cb ) * INV_LOG_2 );
3837 880526 : dct0_index = 0;
3838 :
3839 13021920 : for ( j = 0; j < no_bits_vec; j++ )
3840 : {
3841 12141394 : dct0_index = ( dct0_index << 1 ) + bitstream[bit_pos--];
3842 : }
3843 :
3844 880526 : nbits += no_bits_vec;
3845 :
3846 880526 : set_s( (int16_t *) spr_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
3847 :
3848 880526 : decode_combined_index( dct0_index, no_cv_vec, spr_coh_temp_index, coding_subbands );
3849 :
3850 7756103 : for ( j = 0; j < coding_subbands; j++ )
3851 : {
3852 6875577 : 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 8205241 : for ( j = 0; j < coding_subbands; j++ )
3858 : {
3859 7305849 : bits_GR = bit_pos;
3860 7305849 : idx_dct1[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, 2 * MASA_NO_CV_COH1, 0 );
3861 7305849 : nbits += bits_GR - bit_pos;
3862 : }
3863 899392 : bits_GR = bit_pos; /* just to store the data */
3864 :
3865 : /* read average index */
3866 899392 : read_huf( &bit_pos, bitstream, &av_index, bit_pos, MASA_NO_CV_COH1, huff_code_av_masa, 10 ); /* 10 is MAX_LEN*/
3867 899392 : nbits += ( bits_GR - bit_pos );
3868 :
3869 : /* write indexes in metadata structure */
3870 8205241 : for ( j = 0; j < coding_subbands; j++ )
3871 : {
3872 7305849 : if ( idx_dct1[j] % 2 )
3873 : {
3874 317526 : q_direction->coherence_band_data[j].spread_coherence_dct1_index = ( idx_dct1[j] + 1 ) / 2 + av_index;
3875 : }
3876 : else
3877 : {
3878 6988323 : 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 1158097 : nbits = *p_bit_pos - bit_pos;
3886 :
3887 1158097 : *p_bit_pos = bit_pos;
3888 :
3889 1158097 : return nbits;
3890 : }
3891 :
3892 :
3893 : /*-------------------------------------------------------------------*
3894 : * read_surround_coherence()
3895 : *
3896 : *
3897 : *-------------------------------------------------------------------*/
3898 :
3899 1008546 : 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 1008546 : coding_subbands = hQMetaData->q_direction[0].cfg.nbands;
3918 1008546 : q_direction = hQMetaData->q_direction;
3919 :
3920 1008546 : bits_sur_coherence = 0;
3921 1008546 : bit_pos = *p_bit_pos;
3922 :
3923 1008546 : d = 0;
3924 10438958 : for ( j = 0; j < coding_subbands; j++ )
3925 : {
3926 9430412 : error_ratio_surr = 1.0f;
3927 :
3928 9430412 : if ( hQMetaData->no_directions == 2 )
3929 : {
3930 1482538 : d += hQMetaData->twoDirBands[j];
3931 1482538 : idx = max( d - 1, 0 );
3932 1482538 : 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 7947874 : error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[0];
3937 : }
3938 :
3939 9430412 : if ( error_ratio_surr <= 0 )
3940 : {
3941 376033 : error_ratio_surr = 0;
3942 376033 : no_cv_vec[j] = 1;
3943 376033 : idx_ER[j] = masa_sq( 0.0f, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
3944 : }
3945 : else
3946 : {
3947 9054379 : idx_ER[j] = masa_sq( error_ratio_surr, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS );
3948 9054379 : no_cv_vec[j] = idx_cb_sur_coh_masa[idx_ER[j]] + 2;
3949 : }
3950 : }
3951 :
3952 1008546 : if ( sum_s( no_cv_vec, coding_subbands ) == coding_subbands )
3953 : {
3954 : /* surround coherence is zero */
3955 160912 : for ( j = 0; j < coding_subbands; j++ )
3956 : {
3957 746515 : for ( k = 0; k < MAX_PARAM_SPATIAL_SUBFRAMES; k++ )
3958 : {
3959 597212 : if ( hQMetaData->surcoh_band_data != NULL )
3960 : {
3961 597212 : hQMetaData->surcoh_band_data[j].surround_coherence[k] = 0;
3962 : }
3963 : }
3964 : }
3965 :
3966 11609 : return bits_sur_coherence;
3967 : }
3968 :
3969 : /* read how the surround coherence is encoded */
3970 996937 : byteBuffer = bitstream[bit_pos--];
3971 996937 : bits_sur_coherence += 1;
3972 :
3973 996937 : if ( byteBuffer & 1 )
3974 : {
3975 : /* GR decoding */
3976 : /* read GR order */
3977 787118 : byteBuffer = bitstream[bit_pos--];
3978 787118 : bits_sur_coherence += 1;
3979 :
3980 : /* read min index */
3981 787118 : bits_GR = bit_pos;
3982 787118 : min_index = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, MASA_MAX_NO_CV_SUR_COH, 0 );
3983 787118 : bits_sur_coherence += bits_GR - bit_pos;
3984 :
3985 : /* read GR data */
3986 8394161 : for ( j = 0; j < coding_subbands; j++ )
3987 : {
3988 7607043 : bits_GR = bit_pos;
3989 : /* decoding for min removed */
3990 7607043 : if ( no_cv_vec[j] > 1 )
3991 : {
3992 7429349 : idx_sur_coh[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_cv_vec[j] - min_index, ( byteBuffer & 1 ) );
3993 7429349 : bits_sur_coherence += bits_GR - bit_pos;
3994 : }
3995 : else
3996 : {
3997 177694 : idx_sur_coh[j] = 0;
3998 : }
3999 : }
4000 :
4001 8394161 : for ( j = 0; j < coding_subbands; j++ )
4002 : {
4003 7607043 : if ( no_cv_vec[j] > 1 )
4004 : {
4005 7429349 : hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j] + min_index;
4006 : }
4007 : else
4008 : {
4009 177694 : hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j];
4010 : }
4011 :
4012 7607043 : 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 209819 : set_s( (int16_t *) sur_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
4020 :
4021 209819 : 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 1883885 : for ( j = 0; j < coding_subbands; j++ )
4024 : {
4025 1674066 : hQMetaData->surcoh_band_data[j].sur_coherence_index = sur_coh_temp_index[j];
4026 : }
4027 :
4028 : /* deindex surround coherence */
4029 1883885 : for ( j = 0; j < coding_subbands; j++ )
4030 : {
4031 1674066 : if ( no_cv_vec[j] > 1 )
4032 : {
4033 1385737 : 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 288329 : hQMetaData->surcoh_band_data[j].surround_coherence[0] = 0;
4038 : }
4039 : }
4040 : }
4041 :
4042 10278046 : for ( j = 0; j < coding_subbands; j++ )
4043 : {
4044 37124436 : for ( k = 1; k < MAX_PARAM_SPATIAL_SUBFRAMES; k++ )
4045 : {
4046 27843327 : 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 996937 : bits_sur_coherence = *p_bit_pos - bit_pos;
4052 996937 : *p_bit_pos = bit_pos;
4053 :
4054 996937 : return bits_sur_coherence;
4055 : }
4056 :
4057 :
4058 : /*-------------------------------------------------------------------*
4059 : * read_surround_coherence_hr()
4060 : *
4061 : *
4062 : *-------------------------------------------------------------------*/
4063 :
4064 107685 : 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 107685 : coding_subbands = hQMetaData->q_direction[0].cfg.nbands;
4085 107685 : q_direction = hQMetaData->q_direction;
4086 :
4087 107685 : bits_sur_coherence = 0;
4088 107685 : bit_pos = *p_bit_pos;
4089 :
4090 492333 : for ( sf = 0; sf < hQMetaData->q_direction[0].cfg.nblocks; sf++ )
4091 : {
4092 384648 : d = 0;
4093 9031785 : for ( j = 0; j < coding_subbands; j++ )
4094 : {
4095 8647137 : error_ratio_surr = 1.0f;
4096 8647137 : if ( hQMetaData->no_directions == 2 )
4097 : {
4098 1431198 : d += hQMetaData->twoDirBands[j];
4099 1431198 : idx = max( d - 1, 0 );
4100 1431198 : 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 7215939 : error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[sf];
4105 : }
4106 :
4107 8647137 : int_error_ratio_surr = (int32_t) ( MASA_SUR_COH_PRECISION * error_ratio_surr );
4108 8647137 : error_ratio_surr = (float) ( int_error_ratio_surr * MASA_SUR_COH_THRESHOLD );
4109 :
4110 8647137 : if ( error_ratio_surr <= 0 )
4111 : {
4112 793716 : error_ratio_surr = 0;
4113 793716 : no_cv_vec[j] = 1;
4114 793716 : idx_ER[j] = 0;
4115 : }
4116 : else
4117 : {
4118 7853421 : idx_ER[j] = 7; /* masa_sq( error_ratio_surr, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS ); */
4119 7853421 : no_cv_vec[j] = idx_cb_sur_coh_masa[idx_ER[j]] + 2;
4120 : }
4121 : }
4122 :
4123 384648 : if ( sum_s( no_cv_vec, coding_subbands ) == coding_subbands )
4124 : {
4125 : /* surround coherence is zero */
4126 77449 : for ( j = 0; j < coding_subbands; j++ )
4127 : {
4128 371555 : for ( k = 0; k < MAX_PARAM_SPATIAL_SUBFRAMES; k++ )
4129 : {
4130 297244 : if ( hQMetaData->surcoh_band_data != NULL )
4131 : {
4132 297244 : 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 381510 : byteBuffer = bitstream[bit_pos--];
4141 381510 : bits_sur_coherence += 1;
4142 :
4143 381510 : if ( byteBuffer & 1 )
4144 : {
4145 : /* GR decoding */
4146 : /* read GR order */
4147 329689 : byteBuffer = bitstream[bit_pos--];
4148 329689 : bits_sur_coherence += 1;
4149 :
4150 : /* read min index */
4151 329689 : bits_GR = bit_pos;
4152 329689 : min_index = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, MASA_MAX_NO_CV_SUR_COH, 0 );
4153 329689 : bits_sur_coherence += bits_GR - bit_pos;
4154 :
4155 : /* read GR data */
4156 7701900 : for ( j = 0; j < coding_subbands; j++ )
4157 : {
4158 7372211 : bits_GR = bit_pos;
4159 : /* decoding for min removed */
4160 7372211 : if ( no_cv_vec[j] > 1 )
4161 : {
4162 6819924 : idx_sur_coh[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_cv_vec[j] - min_index, ( byteBuffer & 1 ) );
4163 6819924 : bits_sur_coherence += bits_GR - bit_pos;
4164 : }
4165 : else
4166 : {
4167 552287 : idx_sur_coh[j] = 0;
4168 : }
4169 : }
4170 :
4171 7701900 : for ( j = 0; j < coding_subbands; j++ )
4172 : {
4173 7372211 : if ( no_cv_vec[j] > 1 )
4174 : {
4175 6819924 : hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j] + min_index;
4176 6819924 : 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 552287 : hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j];
4181 552287 : 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 51821 : set_s( (int16_t *) sur_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS );
4190 :
4191 51821 : 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 1252436 : for ( j = 0; j < coding_subbands; j++ )
4194 : {
4195 1200615 : hQMetaData->surcoh_band_data[j].sur_coherence_index = sur_coh_temp_index[j];
4196 : }
4197 :
4198 : /* deindex surround coherence */
4199 1252436 : for ( j = 0; j < coding_subbands; j++ )
4200 : {
4201 1200615 : if ( no_cv_vec[j] > 1 )
4202 : {
4203 774615 : 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 426000 : 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 107685 : bits_sur_coherence = *p_bit_pos - bit_pos;
4216 107685 : *p_bit_pos = bit_pos;
4217 :
4218 107685 : return bits_sur_coherence;
4219 : }
4220 :
4221 :
4222 : /*-------------------------------------------------------------------*
4223 : * decode_combined_index()
4224 : *
4225 : * Decode combined index into several indexes
4226 : *-------------------------------------------------------------------*/
4227 :
4228 1347081 : 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 1347081 : base[0] = 1;
4239 11072769 : for ( i = 1; i < len; i++ )
4240 : {
4241 9725688 : base[i] = base[i - 1] * no_cv_vec[i - 1];
4242 : }
4243 :
4244 11072769 : for ( i = len - 1; i > 0; i-- )
4245 : {
4246 9725688 : index[i] = (uint16_t) ( comb_index / base[i] );
4247 9725688 : comb_index -= index[i] * base[i];
4248 : }
4249 :
4250 1347081 : index[0] = (uint16_t) comb_index;
4251 :
4252 1347081 : return;
4253 : }
4254 :
4255 :
4256 114482 : 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 114482 : step_fx = STEP_M2T_FX;
4269 114482 : sign = 1;
4270 114482 : 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 114482 : set_s( q_idx, 0, len_stream );
4281 : /* read DCT 0 component */
4282 801374 : for ( i = 0; i < BITS_MASA2TOTTAL_DCT0; i++ )
4283 : {
4284 686892 : q_idx[0] = ( q_idx[0] << 1 ) + bit_stream[( *index )--];
4285 : }
4286 114482 : q_idx[0] *= sign;
4287 :
4288 114482 : if ( q_idx[0] != 0 )
4289 : {
4290 103144 : if ( len_stream >= 8 )
4291 : {
4292 : /* read index of last index encoded with GR2 */
4293 85670 : i_min = 0;
4294 85670 : j = 4;
4295 428350 : for ( i = 0; i < j; i++ )
4296 : {
4297 342680 : i_min = ( i_min << 1 ) + bit_stream[( *index )--];
4298 : }
4299 :
4300 : /* read GR orders */
4301 85670 : GR1 = bit_stream[( *index )--] + 1;
4302 85670 : if ( GR1 == 2 )
4303 : {
4304 52514 : GR2 = bit_stream[( *index )--];
4305 : }
4306 : else
4307 : {
4308 33156 : GR2 = 0;
4309 : }
4310 :
4311 : /* read GR data */
4312 397458 : for ( i = 1; i <= i_min; i++ )
4313 : {
4314 311788 : q_idx[i] = ivas_qmetadata_DecodeExtendedGR( bit_stream, index, 100, GR1 );
4315 : }
4316 1258828 : for ( i = i_min + 1; i < len_stream; i++ )
4317 : {
4318 1173158 : q_idx[i] = ivas_qmetadata_DecodeExtendedGR( bit_stream, index, 100, GR2 );
4319 : }
4320 : }
4321 : else
4322 : {
4323 : /* read GR order (only one) */
4324 17474 : GR1 = bit_stream[( *index )--];
4325 86157 : for ( i = 1; i < len_stream; i++ )
4326 : {
4327 68683 : q_idx[i] = ivas_qmetadata_DecodeExtendedGR( bit_stream, index, 100, GR1 );
4328 : }
4329 : }
4330 : }
4331 :
4332 : /* deindex */
4333 114482 : q_dct_data_fx[0] = (Word32) ( ( step_fx * q_idx[0] ) >> 6 ); /* Q25 */
4334 1827375 : for ( i = 1; i < len_stream; i++ )
4335 : {
4336 1712893 : if ( ( q_idx[i] & 1 ) == 0 )
4337 : {
4338 1289548 : q_dct_data_fx[i] = (Word32) ( ( step_fx * ( -q_idx[i] ) ) >> 7 ); /* Q25 */
4339 : }
4340 : ELSE
4341 : {
4342 423345 : q_dct_data_fx[i] = (Word32) ( ( step_fx * ( q_idx[i] + 1 ) ) >> 7 ); /* Q25 */
4343 : }
4344 : }
4345 114482 : return;
4346 : }
4347 :
4348 :
4349 : /*-------------------------------------------------------------------------
4350 : * ivas_omasa_decode_masa_to_total()
4351 : *
4352 : *------------------------------------------------------------------------*/
4353 :
4354 114482 : 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 114482 : n_streams = 1;
4369 114482 : len_stream = nbands * nblocks;
4370 114482 : if ( len_stream == 32 )
4371 : {
4372 0 : n_streams = 4;
4373 0 : len_stream = 8;
4374 : }
4375 :
4376 114482 : set_s( q_idx, 0, nbands * nblocks );
4377 228964 : for ( i = 0; i < n_streams; i++ )
4378 : {
4379 114482 : read_stream_dct_coeffs_omasa( &q_idx[i * len_stream],
4380 114482 : &q_dct_data_fx[i * len_stream],
4381 : len_stream, bit_stream, index, i == 0 );
4382 : }
4383 :
4384 : /* inverse DCT2 transform */
4385 114482 : SWITCH( len_stream )
4386 : {
4387 1220 : case 4:
4388 1220 : matrix_product_q30_fx( dct4_fx, nblocks, nblocks, 1, q_dct_data_fx, nblocks, 1, 0, dct_data_tmp_fx );
4389 1220 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nblocks ); /*Q30*/
4390 1220 : BREAK;
4391 17671 : case 5:
4392 17671 : matrix_product_q30_fx( dct5_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
4393 17671 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
4394 17671 : 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 22210 : case 12:
4400 22210 : matrix_product_q30_fx( dct12_fx, nbands, nbands, 1, q_dct_data_fx, nbands, 1, 0, dct_data_tmp_fx );
4401 22210 : mvl2l( dct_data_tmp_fx, q_dct_data_fx, nbands ); /*Q30*/
4402 22210 : BREAK;
4403 73381 : case 20:
4404 73381 : matrix_product_fx( dct5_fx, nbands, nbands, 1, q_dct_data_fx, nbands, nblocks, 0, dct_data_tmp_fx );
4405 73381 : matrix_product_q30_fx( dct_data_tmp_fx, nbands, nblocks, 0, dct4_fx, nblocks, nblocks, 0, q_dct_data_fx ); /* reuse of variable*/
4406 73381 : 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 1941857 : for ( i = 0; i < nblocks * nbands; i++ )
4421 : {
4422 1827375 : if ( q_dct_data_fx[i] >= 1052266987 && q_dct_data_fx[i] < 1052400000 )
4423 : {
4424 414 : q_dct_data_fx[i] = 1052400000;
4425 : }
4426 : }
4427 :
4428 114482 : k = 0;
4429 452767 : for ( i = 0; i < nblocks; i++ )
4430 : {
4431 2165660 : for ( j = 0; j < nbands; j++ )
4432 : {
4433 1827375 : masa_to_total_energy_ratio[i][j] = q_dct_data_fx[k] / (float) ( 1 << 30 );
4434 1827375 : masa_to_total_energy_ratio[i][j] = max( 0.0f, masa_to_total_energy_ratio[i][j] );
4435 1827375 : masa_to_total_energy_ratio[i][j] = min( 1.0f, masa_to_total_energy_ratio[i][j] );
4436 1827375 : k++;
4437 : }
4438 : }
4439 :
4440 114482 : if ( nblocks == 1 )
4441 : {
4442 159524 : for ( i = 1; i < MAX_PARAM_SPATIAL_SUBFRAMES; i++ )
4443 : {
4444 1184268 : for ( j = 0; j < nbands; j++ )
4445 : {
4446 1064625 : masa_to_total_energy_ratio[i][j] = masa_to_total_energy_ratio[0][j];
4447 : }
4448 : }
4449 : }
4450 :
4451 114482 : if ( nbands == 1 )
4452 : {
4453 6100 : for ( j = 1; j < 5; j++ )
4454 : {
4455 24400 : for ( i = 0; i < nblocks; i++ )
4456 : {
4457 19520 : masa_to_total_energy_ratio[i][j] = masa_to_total_energy_ratio[i][0];
4458 : }
4459 : }
4460 : }
4461 :
4462 114482 : return;
4463 : }
|