diff --git a/lib_com/ivas_cnst.h b/lib_com/ivas_cnst.h index 0623cacb43df1f62dad36cfbca2c6bdcb5212316..781e35878ff777e1cc6ec3c1ed0a4a2f0346290b 100644 --- a/lib_com/ivas_cnst.h +++ b/lib_com/ivas_cnst.h @@ -1137,6 +1137,9 @@ enum #define MASA_DIRECTION_MAX_BITS 11 #define MASA_NO_INDEX 32767 #define MASA_BITS_ER 3 +#ifdef HR_METADATA +#define MASA_BITS_ER_HR 4 +#endif #define MASA_MIN_BITS_TF 4 #define MASA_LIMIT_2D 2 #define MASA_NO_CV_COH 8 @@ -1155,6 +1158,12 @@ enum #define MASA_COH_LIMIT_2IDX 144 /* limit for sum of values across components for having two joint indexes */ #define QMETADATA_MAX_NO_DIRECTIONS 2 #define MASA_MAX_BITS 1300 /* max. bit-budget for MASA metadata */ + +#ifdef HR_METADATA +#define MASA_MAX_BITS_HR 2000 /* max. bit-budget for MASA metadata in HR mode*/ +#define HR_MASA_ER_LEVELS 16 +#endif + #define LIMIT_ER_ELEVATION_ENC 4 #define LIMIT_ER_SIMPLE_ENC 6 #define LIMIT_USE_COMMON 3 @@ -1183,7 +1192,12 @@ enum #define MASA_STEREO_MIN_BITRATE IVAS_24k4 #define MASA_BIT_REDUCT_PARAM 10 +#ifdef HR_METADATA +#define MASA_MAXIMUM_TWO_DIR_BANDS 24 +#define NBITS_HR_COH 4 +#else #define MASA_MAXIMUM_TWO_DIR_BANDS 18 +#endif typedef enum { MASA_STEREO_NOT_DEFINED, diff --git a/lib_com/ivas_masa_com.c b/lib_com/ivas_masa_com.c index e576c725b21a3d8d9ebc5f6d338fe202ee7b1f30..02d9939dc49ef33e237293183fec208494287dd3 100644 --- a/lib_com/ivas_masa_com.c +++ b/lib_com/ivas_masa_com.c @@ -411,3 +411,180 @@ void masa_sample_rate_band_correction( return; } + +#ifdef HR_METADATA +/* !r: output index for direction */ +uint16_t index_theta_phi_16( + float *p_theta, /* i/o : input elevation to be indexed */ + float *p_phi, /* i/o : input azimuth to be indexed */ + SPHERICAL_GRID_DATA *gridData /* i : generated grid data */ +) +{ + float abs_theta; + int16_t sign_th, id_phi, id_th; + uint16_t idx_sph; + uint16_t cum_n; + float theta_hat, phi_hat; + float theta, phi; + + theta = *p_theta; + phi = *p_phi; + phi_hat = 0; + theta_hat = 0; + phi = phi + 180; + + if ( theta < 0 ) + { + abs_theta = -theta; + sign_th = -1; + } + else + { + abs_theta = theta; + sign_th = 1; + } + + id_th = quantize_theta( abs_theta, gridData->no_theta, &theta_hat ); + if ( gridData->no_theta > 1 ) + { + if ( gridData->no_phi[id_th] > 1 ) + { + id_phi = quantize_phi_masa( phi, ( id_th % 2 == 1 ), &phi_hat, gridData->no_phi[id_th] ); + } + else + { + id_phi = 0; + phi_hat = 180; + } + } + else + { + id_phi = quantize_phi_masa( phi, ( id_th % 2 == 1 ), &phi_hat, gridData->no_phi[id_th] ); + } + *p_theta = sign_th * theta_hat; + *p_phi = phi_hat - 180; + + /* Starting from Equator, alternating positive and negative */ + if ( id_th == 0 ) + { + idx_sph = id_phi; + } + else + { + if ( id_th == gridData->no_theta - 1 ) + { + idx_sph = 65534 + ( sign_th < 0 ); + } + else + { + theta = MASA_ANGLE_AT_EQUATOR * (float) ( id_th + 0.5f ); + if ( id_th == 1 ) + { + cum_n = 2 * (uint16_t) ceilf( MASA_NTOT2_FAC * ( sinf( theta ) - MASA_ASIN_OFFSET ) ); + } + else + { + cum_n = 2 * (uint16_t) roundf( MASA_NTOT2_FAC * ( sinf( theta ) - MASA_ASIN_OFFSET ) ); + } + + cum_n += gridData->no_phi[0]; + + if ( sign_th > 0 ) + { + cum_n -= 2 * gridData->no_phi[id_th]; + } + else + { + cum_n -= gridData->no_phi[id_th]; + } + idx_sph = cum_n + id_phi; + } + } + + + return idx_sph; +} + +int16_t quantize_theta( + float x, /* i : theta value to be quantized */ + int16_t no_cb, /* i : number of codewords */ + float *xhat /* o : quantized value */ +) +{ + int16_t imin; + float diff1, diff2; + + imin = (int16_t) ( x * MASA_INV_ANGLE_AT_EQUATOR_DEG + 0.5f ); + + if ( imin >= no_cb - 1 ) + { + imin = no_cb - 1; + diff1 = x - 90; + diff2 = x - MASA_ANGLE_AT_EQUATOR_DEG * ( imin - 1 ); + if ( fabsf( diff1 ) > fabsf( diff2 ) ) + { + imin--; + *xhat = imin * MASA_ANGLE_AT_EQUATOR_DEG; + } + else + { + *xhat = 90; + } + } + else + { + *xhat = imin * MASA_ANGLE_AT_EQUATOR_DEG; + } + + return imin; +} + + +/* !r: index azimuth */ +int16_t quantize_phi_masa( + float phi, /* i : azimuth value */ + int16_t flag_delta, /* i : flag indicating if the azimuth codebook is translated or not */ + float *phi_hat, /* o : quantized azimuth */ + const int16_t n /* i : azimuth codebook size */ +) +{ + int16_t id_phi; + float dd; + float delta_phi; + + delta_phi = 360.0f / (float) n; + + if ( n == 1 ) + { + *phi_hat = 0; + + return 0; + } + + if ( flag_delta == 1 ) + { + dd = delta_phi / 2.0f; + } + else + { + dd = 0; + } + + id_phi = (int16_t) ( ( phi - dd + delta_phi / 2.0f ) / (float) delta_phi ); + + if ( id_phi == n ) + { + id_phi = 0; + } + + if ( id_phi == -1 ) + { + id_phi = n - 1; + } + + *phi_hat = id_phi * delta_phi + dd; + + return id_phi; +} + +#endif diff --git a/lib_com/ivas_prot.h b/lib_com/ivas_prot.h index 890390309fbd5e9af11b6c6e28b35006682fefe2..16d475ab0977d8091903e95a837dcae8529e8d25 100644 --- a/lib_com/ivas_prot.h +++ b/lib_com/ivas_prot.h @@ -2932,6 +2932,52 @@ ivas_error ivas_qmetadata_enc_encode( IVAS_QMETADATA *hQMetaData /* i/o: q_metadata handle */ ); +#ifdef HR_METADATA + +ivas_error ivas_qmetadata_enc_encode_hr_384_512( + BSTR_ENC_HANDLE hMetaData, /* i/o: metadata bitstream handle */ + IVAS_QMETADATA *hQMetaData, /* i/o: metadata handle */ + int16_t bits_sph_idx, + int16_t bits_sp_coh +); +int16_t ivas_qmetadata_dec_decode_hr_384_512( + IVAS_QMETADATA_HANDLE hQMetaData, /* i/o: hQMetaData handle */ + uint16_t *bitstream, /* i : bitstream */ + int16_t *index, /* i/o: bitstream position */ + SPHERICAL_GRID_DATA *sph_grid16, /* i: spherical grid for deindexing */ + int16_t bits_sph_idx, + int16_t bits_sp_coh +); + +void deindex_sph_idx( + const uint16_t sphIndex, /* i : Spherical index */ + const SPHERICAL_GRID_DATA *gridData, /* i : Prepared spherical grid */ + float *theta, /* o : Elevation */ + float *phi /* o : Azimuth */ +); + + +uint16_t index_theta_phi_16( + float * p_theta, /* i/o : input elevation to be indexed */ + float * p_phi, /* i/o : input azimuth to be indexed */ + SPHERICAL_GRID_DATA *gridData /* i : generated grid data */ +); + + int16_t quantize_theta( + float x, /* i : theta value to be quantized */ + int16_t no_cb, /* i : number of codewords */ + float *xhat /* o : quantized value */ +); + +/* !r: index azimuth */ + int16_t quantize_phi_masa( + float phi, /* i : azimuth value */ + int16_t flag_delta, /* i : flag indicating if the azimuth codebook is translated or not */ + float *phi_hat, /* o : quantized azimuth */ + const int16_t n /* i : azimuth codebook size */ +); +#endif + void reset_metadata_spatial( const IVAS_FORMAT ivas_format, /* i : IVAS format */ BSTR_ENC_HANDLE hMetaData, /* i/o: Metadata bitstream handle */ @@ -2958,6 +3004,8 @@ int16_t ivas_qmetadata_dec_decode( int16_t *index /* i/o: bitstream position */ ); + + /*! r: number of bits read */ int16_t ivas_qmetadata_dec_sid_decode( IVAS_QMETADATA_HANDLE hQMetaData, /* i/o: q_metadata handle */ @@ -3032,6 +3080,10 @@ void quantize_direction_frame( IVAS_QDIRECTION *q_direction, /* i/o: quantized direction structure */ float azimuth_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES], float elevation_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES] +#ifdef HR_METADATA + , + int16_t is_hr +#endif ); /* !r: quantized spherical index */ diff --git a/lib_com/ivas_rom_com.c b/lib_com/ivas_rom_com.c index b220f19f9ecc9cde22ddc956675ab5fe67867662..4016fdfe17390bc0300a75f9076543178c3a0f92 100644 --- a/lib_com/ivas_rom_com.c +++ b/lib_com/ivas_rom_com.c @@ -2523,7 +2523,48 @@ const uint16_t ivas_param_mc_sym_freq_ild_delta_combined_48_16bits[2 * PARAM_MC_ /*----------------------------------------------------------------------------------* * MASA ROM tables *----------------------------------------------------------------------------------*/ - +#ifdef HR_METADATA + +const float diffuseness_reconstructions_hr[HR_MASA_ER_LEVELS] = +{ + 0.00f, + 0.0142822265625f, + 0.030029296875f, + 0.052001953125f, + 0.07708740234375f, + 0.10528564453125f, + 0.14483642578125f, + 0.19573974609375f, + 0.26568603515625f, + 0.35467529296875f, + 0.436279296875f, + 0.510498046875f, + 0.5943603515625f, + 0.6878662109375f, + 0.80096435546875f, + 0.93365478515625f +}; +const float diffuseness_thresholds_hr[HR_MASA_ER_LEVELS + 1] = +{ + 0.0f, + 0.009521484375f, + 0.01904296875f, + 0.0410156250f, + 0.06298828125f, + 0.0911865234375f, + 0.119384765625f, + 0.1702880859375f, + 0.22119140625f, + 0.3101806640625f, + 0.399169921875f, + 0.473388671875f, + 0.547607421875f, + 0.641113281250f, + 0.734619140625f, + 0.8673095703125f, + 2.0f /* out-of-range large value to make searching easier */ +}; +#endif const int16_t bits_direction_masa[DIRAC_DIFFUSE_LEVELS] = { 11, @@ -2536,6 +2577,7 @@ const int16_t bits_direction_masa[DIRAC_DIFFUSE_LEVELS] = 3 }; + const float coherence_cb0_masa[DIRAC_DIFFUSE_LEVELS * 2 * MASA_NO_CV_COH] = { /* this is the same */ @@ -2718,7 +2760,11 @@ const uint8_t masa_joined_nbands[IVAS_NUM_ACTIVE_BRATES] = const uint8_t masa_twodir_bands[IVAS_NUM_ACTIVE_BRATES] = { +#ifdef HR_METADATA + 0, 0, 0, 0, 0, 1, 1, 1, 3, 4, 6, 6, 9, 24 +#else 0, 0, 0, 0, 0, 1, 1, 1, 3, 4, 6, 6, 9, 12 +#endif }; const uint8_t masa_twodir_bands_joined[IVAS_NUM_ACTIVE_BRATES] = diff --git a/lib_com/ivas_rom_com.h b/lib_com/ivas_rom_com.h index e4bdc738a841cdc405896e235a2df314b103bf21..174435448a0d076088d715940899bcc7118949c9 100644 --- a/lib_com/ivas_rom_com.h +++ b/lib_com/ivas_rom_com.h @@ -289,6 +289,11 @@ extern const uint8_t masa_joined_nbands[]; extern const uint8_t masa_twodir_bands[]; extern const uint8_t masa_twodir_bands_joined[]; +#ifdef HR_METADATA +extern const float diffuseness_reconstructions_hr[HR_MASA_ER_LEVELS]; +extern const float diffuseness_thresholds_hr[HR_MASA_ER_LEVELS + 1]; +#endif + /* Multi-channel input and output setups */ extern const float ls_azimuth_CICP2[2]; extern const float ls_elevation_CICP2[2]; diff --git a/lib_com/ivas_stat_com.h b/lib_com/ivas_stat_com.h index 9f1fafff27b34e722d78c11288f974589010fe92..950e43a88efddccb73dcbee3b492183c89dc0cb3 100644 --- a/lib_com/ivas_stat_com.h +++ b/lib_com/ivas_stat_com.h @@ -423,6 +423,9 @@ typedef struct ivas_masa_directional_spatial_meta_struct float elevation[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS]; float energy_ratio[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS]; float spread_coherence[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS]; +#ifdef HR_METADATA + uint16_t spherical_index[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS]; +#endif } MASA_DIRECTIONAL_SPATIAL_META; @@ -498,7 +501,6 @@ typedef struct ivas_qdirection_band_coherence_data_struct uint8_t spread_coherence[MAX_PARAM_SPATIAL_SUBFRAMES]; uint16_t spread_coherence_dct0_index; uint16_t spread_coherence_dct1_index; - } IVAS_QDIRECTION_BAND_COHERENCE_DATA; typedef struct ivas_surround_coherence_band_data_struct diff --git a/lib_com/options.h b/lib_com/options.h index ac21bc2a368a8d923ee5951f93fbd5124afa0d77..cc884679fc690211305312fc577d3c593f019964 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -168,6 +168,7 @@ #define FIX_425_MASA_BRSW_RENDERER /* Nokia: Issue 425: renderer not reconfigure in MASA bitrate switching */ #define EUALER2QUAT_FIX /*Dlb :fix for issue 430 issue in euler2quat, sign of quat y is inverted*/ +#define HR_METADATA /* Nok: encode directional MASA metadata with more bits at 384k and 512k */ /* ################## End DEVELOPMENT switches ######################### */ /* clang-format on */ #endif diff --git a/lib_dec/ivas_masa_dec.c b/lib_dec/ivas_masa_dec.c index 32bfcb960d596e40bc20adf8432dcab43992c704..e13cf410467ad9ec182e2907fc641c64839cc32b 100644 --- a/lib_dec/ivas_masa_dec.c +++ b/lib_dec/ivas_masa_dec.c @@ -53,10 +53,11 @@ /*-----------------------------------------------------------------------* * Local function prototypes *-----------------------------------------------------------------------*/ - +#ifndef HR_METADATA static int16_t quantize_theta( float x, int16_t no_cb, float *xhat ); static uint16_t index_theta_phi_16( float theta, float phi, SPHERICAL_GRID_DATA *Sph_Grid16 ); static int16_t quantize_phi_masa( float phi, int16_t flag_delta, float *phi_hat, const int16_t n ); +#endif static void index_16bits( IVAS_QMETADATA_HANDLE hQMetaData, SPHERICAL_GRID_DATA *Sph_Grid16 ); static void create_masa_ext_out_meta( MASA_DECODER *hMasa, IVAS_QMETADATA_HANDLE hQMetaData, const int16_t nchan_transport ); @@ -191,9 +192,26 @@ ivas_error ivas_masa_decode( /* Remove already read bits from the bit budget */ hQMetaData->metadata_max_bits -= *nb_bits_read; +#ifdef HR_METADATA + if ( ivas_total_brate >= IVAS_384k ) + { + if ( ivas_total_brate >= IVAS_512k ) + { + *nb_bits_read += ivas_qmetadata_dec_decode_hr_384_512( hQMetaData, st->bit_stream, &st->next_bit_pos, hMasa->data.sph_grid16, 16, 4 ); + } + else + { + *nb_bits_read += ivas_qmetadata_dec_decode_hr_384_512( hQMetaData, st->bit_stream, &st->next_bit_pos, hMasa->data.sph_grid16, 11, 3 ); + } + } + else + { - *nb_bits_read += ivas_qmetadata_dec_decode( hQMetaData, st->bit_stream, &st->next_bit_pos ); - +#endif + *nb_bits_read += ivas_qmetadata_dec_decode( hQMetaData, st->bit_stream, &st->next_bit_pos ); +#ifdef HR_METADATA + } +#endif /* Get direction decoding quality. EC 1 and 2 are handled by the default value. */ if ( hQMetaData->ec_flag == 2 ) { @@ -460,6 +478,19 @@ static ivas_error ivas_masa_dec_config( ivas_masa_set_elements( st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->mc_mode, st_ivas->nchan_transport, st_ivas->hQMetaData, &st_ivas->element_mode_init, &st_ivas->nSCE, &st_ivas->nCPE ); ivas_masa_set_coding_config( &( hMasa->config ), hMasa->data.band_mapping, st_ivas->hDecoderConfig->ivas_total_brate, st_ivas->nchan_transport, ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCMASA ) ); + +#ifdef HR_METADATA + if ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->hDecoderConfig->ivas_total_brate == IVAS_512k ) + { + hMasa->config.mergeRatiosOverSubframes = 0; + /* initialize spherical grid */ + if ( hMasa->data.sph_grid16 == NULL ) + { + hMasa->data.sph_grid16 = (SPHERICAL_GRID_DATA *) malloc( sizeof( SPHERICAL_GRID_DATA ) ); + generate_gridEq( hMasa->data.sph_grid16 ); + } + } +#endif st_ivas->hQMetaData->metadata_max_bits = hMasa->config.max_metadata_bits; st_ivas->hQMetaData->bandMap = hMasa->data.band_mapping; st_ivas->hQMetaData->nchan_transport = st_ivas->nchan_transport; @@ -537,7 +568,7 @@ void ivas_masa_prerender( /*-------------------------------------------------------------------* * Local functions *-------------------------------------------------------------------*/ - +#ifndef HR_METADATA /* !r: index of quantized value */ static int16_t quantize_theta( float x, /* i : theta value to be quantized */ @@ -706,7 +737,7 @@ static uint16_t index_theta_phi_16( return idx_sph; } - +#endif static void index_16bits( IVAS_QMETADATA_HANDLE hQMetaData, @@ -721,8 +752,8 @@ static void index_16bits( { for ( block = 0; block < hQMetaData->q_direction[0].cfg.nblocks; block++ ) { - hQMetaData->q_direction[d].band_data[band].spherical_index[block] = index_theta_phi_16( hQMetaData->q_direction[d].band_data[band].elevation[block], - hQMetaData->q_direction[d].band_data[band].azimuth[block], Sph_Grid16 ); + hQMetaData->q_direction[d].band_data[band].spherical_index[block] = index_theta_phi_16( &( hQMetaData->q_direction[d].band_data[band].elevation[block] ), + &( hQMetaData->q_direction[d].band_data[band].azimuth[block] ), Sph_Grid16 ); } } } diff --git a/lib_dec/ivas_qmetadata_dec.c b/lib_dec/ivas_qmetadata_dec.c index 9c6597fdcb881530b248d221ae0934ea93b22882..47eb7917c0d17ea7ea1dd4333310a4a23ab8ddf7 100644 --- a/lib_dec/ivas_qmetadata_dec.c +++ b/lib_dec/ivas_qmetadata_dec.c @@ -49,9 +49,19 @@ static int16_t ivas_qmetadata_entropy_decode_diffuseness( uint16_t *bitstream, i static int16_t ivas_qmetadata_entropy_decode_df_ratio( uint16_t *bitstream, int16_t *index, IVAS_QDIRECTION *q_direction, int16_t *dfRatio_bits ); -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 ); +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 +#ifdef HR_METADATA + , + int16_t is_hr +#endif +); -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 ); +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 +#ifdef HR_METADATA + , + int16_t is_hr +#endif +); static uint16_t ivas_qmetadata_DecodeQuasiUniform( const uint16_t *bitstream, int16_t *index, const uint16_t alphabet_size ); @@ -77,11 +87,21 @@ static int16_t read_truncGR_azimuth( uint16_t *bitstream, IVAS_QDIRECTION *q_dir 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 ); -static int16_t read_coherence_data( uint16_t *bitstream, int16_t *p_bit_pos, IVAS_QMETADATA *hQMetaData, const int16_t idx_dir ); +static int16_t read_coherence_data( uint16_t *bitstream, int16_t *p_bit_pos, IVAS_QMETADATA *hQMetaData, const int16_t idx_dir +#ifdef HR_METADATA + , + int16_t is_hr +#endif +); static int16_t read_surround_coherence( uint16_t *bitstream, int16_t *p_bit_pos, IVAS_QMETADATA *hQMetaData ); -static void decode_spread_coherence( IVAS_QMETADATA_HANDLE hQMetaData, int16_t idx_d, const int16_t no_frames ); +static void decode_spread_coherence( IVAS_QMETADATA_HANDLE hQMetaData, int16_t idx_d, const int16_t no_frames +#ifdef HR_METADATA + , + int16_t is_hr +#endif +); static void decode_combined_index( uint64_t comb_index, const int16_t *no_cv_vec, uint16_t *index, const int16_t len ); @@ -91,6 +111,44 @@ static int16_t read_GR_min_removed_data( uint16_t *bitstream, int16_t *p_bit_pos 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 ); +#ifdef HR_METADATA + +static int16_t ivas_qmetadata_entropy_decode_diffuseness_hr( + uint16_t *bitstream, /* i : bitstream */ + int16_t *index, + IVAS_QDIRECTION *q_direction, + uint16_t *diffuseness_index_max_ec_frame ); + + +static int16_t ivas_qmetadata_entropy_decode_diffuseness_hr_512( + uint16_t *bitstream, /* i : bitstream */ + int16_t *index, + IVAS_QDIRECTION *q_direction ); + +static int16_t ivas_qmetadata_raw_decode_dir_512( + IVAS_QDIRECTION *q_direction, /* i/o: quantized direction structure */ + uint16_t *bitstream, /* i : bitstream */ + int16_t *index, + const int16_t nbands, + const int16_t start_band, + SPHERICAL_GRID_DATA *sph_grid16 /* i: spherical grid for deindexing */ +); +static int16_t read_surround_coherence_hr( + uint16_t *bitstream, /* i : bitstream */ + int16_t *p_bit_pos, /* i : position in the bitstream */ + IVAS_QMETADATA *hQMetaData /* i/o: quantized metadata structure */ +); + +/* !r: number of bits read */ +static int16_t read_coherence_data_hr_512( + uint16_t *bitstream, /* i : bitstream */ + int16_t *p_bit_pos, /* i : position in the bitstream */ + IVAS_QMETADATA *hQMetaData, /* i/o: quantized metadata structure */ + const int16_t idx_dir, /* i : direction index */ + int16_t nbits_coh ); + +#endif + /*-----------------------------------------------------------------------* * Global function definitions *-----------------------------------------------------------------------*/ @@ -101,6 +159,7 @@ static int16_t decode_fixed_rate_composed_index_coherence( uint16_t *bitstream, * Main function for decoding Spatial Metadata *-----------------------------------------------------------------------*/ + /*! r: number of bits read */ int16_t ivas_qmetadata_dec_decode( IVAS_QMETADATA_HANDLE hQMetaData, /* i/o: hQMetaData handle */ @@ -385,9 +444,15 @@ int16_t ivas_qmetadata_dec_decode( /* Read coherence, if any */ bits_coherence = 0; + if ( all_coherence_zero == 0 ) { - bits_coherence = read_coherence_data( bitstream, index, hQMetaData, d ); + bits_coherence = read_coherence_data( bitstream, index, hQMetaData, d +#ifdef HR_METADATA + , + 0 +#endif + ); } else { @@ -435,11 +500,21 @@ int16_t ivas_qmetadata_dec_decode( if ( raw_flag[0] == 0 ) { - bits_dir += ivas_qmetadata_entropy_decode_dir( q_direction, bitstream, index, diffuseness_index_max_ec_frame, nbands, start_band ); + bits_dir += ivas_qmetadata_entropy_decode_dir( q_direction, bitstream, index, diffuseness_index_max_ec_frame, nbands, start_band +#ifdef HR_METADATA + , + 0 +#endif + ); } else { - bits_dir += ivas_qmetadata_raw_decode_dir( q_direction, bitstream, index, nbands, start_band ); + bits_dir += ivas_qmetadata_raw_decode_dir( q_direction, bitstream, index, nbands, start_band +#ifdef HR_METADATA + , + 0 +#endif + ); } } /* Decode quantized directions band-wise */ @@ -459,7 +534,12 @@ int16_t ivas_qmetadata_dec_decode( { if ( raw_flag[b] == 0 ) { - bits_dir += ivas_qmetadata_entropy_decode_dir( q_direction, bitstream, index, diffuseness_index_max_ec_frame, b + 1, b ); + bits_dir += ivas_qmetadata_entropy_decode_dir( q_direction, bitstream, index, diffuseness_index_max_ec_frame, b + 1, b +#ifdef HR_METADATA + , + 0 +#endif + ); } else { @@ -503,7 +583,12 @@ int16_t ivas_qmetadata_dec_decode( { if ( raw_flag[b] ) { - bits_dir += ivas_qmetadata_raw_decode_dir( q_direction, bitstream, index, b + 1, b ); + bits_dir += ivas_qmetadata_raw_decode_dir( q_direction, bitstream, index, b + 1, b +#ifdef HR_METADATA + , + 0 +#endif + ); } } } @@ -537,7 +622,12 @@ int16_t ivas_qmetadata_dec_decode( { if ( nblocks > 1 ) { - decode_spread_coherence( hQMetaData, d, nblocks ); + decode_spread_coherence( hQMetaData, d, nblocks +#ifdef HR_METADATA + , + 0 +#endif + ); } } else @@ -670,10 +760,332 @@ int16_t ivas_qmetadata_dec_decode( hQMetaData->dir_comp_ratio = 1.0f; } + return ( start_index_0 - *index ); } +#ifdef HR_METADATA + +/*! r: number of bits read */ +int16_t ivas_qmetadata_dec_decode_hr_384_512( + IVAS_QMETADATA_HANDLE hQMetaData, /* i/o: hQMetaData handle */ + uint16_t *bitstream, /* i : bitstream */ + int16_t *index, /* i/o: bitstream position */ + SPHERICAL_GRID_DATA *sph_grid16, /* i: spherical grid for deindexing */ + int16_t bits_sph_idx, + int16_t bits_sp_coh ) +{ + int16_t d, b, m; + + int16_t bits_diff_sum; + int16_t nbands, start_band; + IVAS_QDIRECTION *q_direction; + int16_t start_index_0; + int16_t bits_no_dirs_coh, bits_sur_coherence; + uint16_t all_coherence_zero; + int16_t p[MASA_MAXIMUM_CODING_SUBBANDS], dif_p[MASA_MAXIMUM_CODING_SUBBANDS]; + +#ifdef DEBUG_MODE_QMETADATA + static FILE *pF = NULL; + static FILE *pF_azi = NULL; + static FILE *pF_ele = NULL; + static FILE *pF_ratio = NULL; + static FILE *pF_spcoh = NULL; + static FILE *pF_surcoh = NULL; + + if ( pF == NULL ) + pF = fopen( "./res/qmetadata_dec.txt", "w" ); + if ( pF_azi == NULL ) + pF_azi = fopen( "./res/qmetadata_azi_dec.txt", "w" ); + if ( pF_ele == NULL ) + pF_ele = fopen( "./res/qmetadata_ele_dec.txt", "w" ); + if ( pF_ratio == NULL ) + pF_ratio = fopen( "./res/qmetadata_ratio_dec.txt", "w" ); + if ( pF_spcoh == NULL ) + pF_spcoh = fopen( "./res/qmetadata_spcoh_dec.txt", "w" ); + if ( pF_surcoh == NULL ) + pF_surcoh = fopen( "./res/qmetadata_surcoh_dec.txt", "w" ); +#endif + + start_index_0 = *index; + + /*Coherence flag decoding*/ + bits_no_dirs_coh = 0; + all_coherence_zero = 1; + if ( hQMetaData->coherence_flag ) + { + /* read if coherence is zero */ + all_coherence_zero = bitstream[( *index )--]; + bits_no_dirs_coh += 1; + } + + hQMetaData->all_coherence_zero = (uint8_t) all_coherence_zero; + + if ( hQMetaData->no_directions == 2 ) + { + set_c( (int8_t *) hQMetaData->twoDirBands, 1, hQMetaData->q_direction[0].cfg.nbands ); + } + + if ( bits_sph_idx == 11 && hQMetaData->no_directions == 2 ) + { + /* Read which bands have 2 directions */ + hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands; + set_c( (int8_t *) hQMetaData->twoDirBands, 0, hQMetaData->q_direction[0].cfg.nbands ); + d = *index; + dif_p[0] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 ); + p[0] = dif_p[0]; + hQMetaData->twoDirBands[p[0]] = 1; + for ( b = 1; b < hQMetaData->numTwoDirBands; b++ ) + { + dif_p[b] = ivas_qmetadata_DecodeExtendedGR( bitstream, index, MASA_MAXIMUM_CODING_SUBBANDS, 0 ); + p[b] = p[b - 1] + dif_p[b] + 1; + hQMetaData->twoDirBands[p[b]] = 1; + } + bits_no_dirs_coh += ( d - *index ); + } + + bits_diff_sum = ivas_qmetadata_entropy_decode_diffuseness_hr_512( bitstream, index, &( hQMetaData->q_direction[0] ) ); + + if ( hQMetaData->no_directions == 2 ) + { + bits_diff_sum += ivas_qmetadata_entropy_decode_diffuseness_hr_512( bitstream, index, &( hQMetaData->q_direction[1] ) ); + } + + + for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ ) + { + for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ ) + { + 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]]; + } + } + if ( hQMetaData->no_directions == 2 ) + { + for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ ) + { + for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ ) + { + 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]]; + if ( hQMetaData->q_direction[1].band_data[b].energy_ratio[m] > 1.0f - hQMetaData->q_direction[0].band_data[b].energy_ratio[m] ) + { + hQMetaData->q_direction[1].band_data[b].energy_ratio[m] = 1.0f - hQMetaData->q_direction[0].band_data[b].energy_ratio[m]; + } + } + } + } + + if ( hQMetaData->no_directions == 2 ) + { + for ( b = hQMetaData->q_direction[1].cfg.start_band; b < hQMetaData->q_direction[1].cfg.nbands; b++ ) + { + for ( m = 0; m < hQMetaData->q_direction[1].cfg.nblocks; m++ ) + { + hQMetaData->q_direction[1].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[1].band_data[b].energy_ratio_index[m]; + hQMetaData->q_direction[1].band_data[b].bits_sph_idx[m] = bits_sph_idx; + } + } + } + + for ( b = hQMetaData->q_direction[0].cfg.start_band; b < hQMetaData->q_direction[0].cfg.nbands; b++ ) + { + for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ ) + { + hQMetaData->q_direction[0].band_data[b].energy_ratio_index_mod[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio_index[0]; + hQMetaData->q_direction[0].band_data[b].bits_sph_idx[m] = bits_sph_idx; + } + } + + + if ( all_coherence_zero == 0 ) + { + bits_sur_coherence = read_surround_coherence_hr( bitstream, index, hQMetaData ); + } + else + { + bits_sur_coherence = 0; + /*Surround coherence*/ + for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ ) + { + if ( hQMetaData->surcoh_band_data != NULL ) + { + set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES ); + } + } + } + bits_no_dirs_coh += bits_sur_coherence; + + + for ( d = 0; d < hQMetaData->no_directions; d++ ) + { + q_direction = &hQMetaData->q_direction[d]; + nbands = q_direction->cfg.nbands; + start_band = q_direction->cfg.start_band; + + + /* Read coherence, if any */ + if ( all_coherence_zero == 0 ) + { + read_coherence_data_hr_512( bitstream, index, hQMetaData, d, bits_sp_coh ); + } + else + { + /*Surround coherence*/ + for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ ) + { + if ( hQMetaData->surcoh_band_data != NULL ) + { + set_c( (int8_t *) hQMetaData->surcoh_band_data[b].surround_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES ); + } + + if ( hQMetaData->q_direction[d].coherence_band_data != NULL ) + { + set_c( (int8_t *) hQMetaData->q_direction[d].coherence_band_data[b].spread_coherence, 0, MAX_PARAM_SPATIAL_SUBFRAMES ); + } + } + } + + + /* Decode quantized directions frame-wise */ + ivas_qmetadata_raw_decode_dir_512( q_direction, bitstream, index, nbands, start_band, sph_grid16 ); + + +#ifdef DEBUG_MODE_QMETADATA + + fprintf( pF, "frame %d: diff %d surcoh %d ", frame, bits_diff_sum, bits_sur_coherence ); + fprintf( pF, "dir %d\n", start_index_0 - *index ); + fprintf( pF_azi, "frame %d/dir/ec %d: ", frame, d ); + fprintf( pF_ele, "frame %d/dir/ec %d: ", frame, d ); + fprintf( pF_spcoh, "frame %d/dir %d: ", frame, d ); + fprintf( pF_ratio, "frame %d/dir %d: ", frame, d ); + + + for ( b = start_band; b < nbands; b++ ) + { + for ( m = 0; m < q_direction->cfg.nblocks; m++ ) + { + + fprintf( pF_ratio, " %1.3f ", q_direction->band_data[b].energy_ratio[m] ); + fprintf( pF_azi, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[b].azimuth[m] ) / 100.f ); + fprintf( pF_ele, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[b].elevation[m] ) / 100.f ); + if ( q_direction->coherence_band_data != NULL ) + { + fprintf( pF_spcoh, " %d ", q_direction->coherence_band_data[b].spread_coherence[m] ); + } + } + } + fprintf( pF_ratio, "\n" ); + fprintf( pF_azi, "\n" ); + fprintf( pF_ele, "\n" ); + fprintf( pF_spcoh, "\n" ); +#endif + } + + if ( hQMetaData->no_directions == 2 ) + { + /* move 2 dir data to its correct subband */ + if ( bits_sph_idx == 11 ) + { + + int16_t nblocks; + d = hQMetaData->q_direction[1].cfg.nbands - 1; + nblocks = hQMetaData->q_direction[0].cfg.nblocks; + + for ( b = hQMetaData->q_direction[0].cfg.nbands - 1; b >= 0; b-- ) + { + if ( hQMetaData->twoDirBands[b] == 1 ) + { + mvr2r( hQMetaData->q_direction[1].band_data[d].azimuth, hQMetaData->q_direction[1].band_data[b].azimuth, nblocks ); + mvr2r( hQMetaData->q_direction[1].band_data[d].elevation, hQMetaData->q_direction[1].band_data[b].elevation, nblocks ); + mvr2r( hQMetaData->q_direction[1].band_data[d].energy_ratio, hQMetaData->q_direction[1].band_data[b].energy_ratio, nblocks ); + + + if ( hQMetaData->q_direction[1].coherence_band_data != NULL ) + { + mvc2c( hQMetaData->q_direction[1].coherence_band_data[d].spread_coherence, hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence, nblocks ); + } + d--; + } + else + { + set_f( hQMetaData->q_direction[1].band_data[b].azimuth, 0.0f, nblocks ); + set_f( hQMetaData->q_direction[1].band_data[b].elevation, 0.0f, nblocks ); + set_f( hQMetaData->q_direction[1].band_data[b].energy_ratio, 0.0f, nblocks ); + + if ( hQMetaData->q_direction[1].coherence_band_data != NULL ) + { + set_c( (int8_t *) hQMetaData->q_direction[1].coherence_band_data[b].spread_coherence, 0, nblocks ); + } + } + } + } + /* Scale energy ratios that sum to over one */ + for ( b = 0; b < hQMetaData->q_direction[0].cfg.nbands; b++ ) + { + float ratioSum; + for ( m = 0; m < hQMetaData->q_direction[0].cfg.nblocks; m++ ) + { + ratioSum = hQMetaData->q_direction[0].band_data[b].energy_ratio[m] + hQMetaData->q_direction[1].band_data[b].energy_ratio[m]; + + if ( ratioSum > 1.0f ) + { + hQMetaData->q_direction[0].band_data[b].energy_ratio[m] = hQMetaData->q_direction[0].band_data[b].energy_ratio[m] / ratioSum; + hQMetaData->q_direction[1].band_data[b].energy_ratio[m] = hQMetaData->q_direction[1].band_data[b].energy_ratio[m] / ratioSum; + } + } + } + } + + +#ifdef DEBUG_MODE_QMETADATA + for ( d = 0; d < hQMetaData->no_directions; d++ ) + { + q_direction = &hQMetaData->q_direction[d]; + nbands = q_direction->cfg.nbands; + start_band = q_direction->cfg.start_band; + + + if ( d == 0 ) + { + fprintf( pF_surcoh, "frame %d/dir %d: ", frame, d ); + } + for ( b = start_band; b < nbands; b++ ) + { + for ( m = 0; m < q_direction->cfg.nblocks; m++ ) + { + + + if ( d == 0 && hQMetaData->surcoh_band_data != NULL ) + { + fprintf( pF_surcoh, " %d ", hQMetaData->surcoh_band_data[b].surround_coherence[m] ); + } + } + } + + + if ( d == 0 ) + { + fprintf( pF_surcoh, "\n" ); + } + } +#endif + /* Store status information for renderer use */ + hQMetaData->ec_flag = 0; + + hQMetaData->dir_comp_ratio = 1.0f; + + if ( hQMetaData->dir_comp_ratio > 1.0f ) + { + hQMetaData->dir_comp_ratio = 1.0f; + } + + + return ( start_index_0 - *index ); +} + +#endif + + /*-----------------------------------------------------------------------* * ivas_qmetadata_dec_sid_decode() * @@ -1036,6 +1448,123 @@ static int16_t ivas_qmetadata_entropy_decode_diffuseness( } +#ifdef HR_METADATA +static int16_t ivas_qmetadata_entropy_decode_diffuseness_hr( + uint16_t *bitstream, /* i : bitstream */ + int16_t *index, + IVAS_QDIRECTION *q_direction, + uint16_t *diffuseness_index_max_ec_frame ) +{ + int16_t b; + uint16_t dif_min; + int16_t index_start; + int16_t nbands; + int16_t start_band; + + index_start = *index; + nbands = q_direction->cfg.nbands; + start_band = q_direction->cfg.start_band; + + /* diffuseness decoding */ + /* Handle one band as special case*/ + if ( nbands == 1 ) + { + q_direction->band_data[0].energy_ratio_index[0] = 0; + for ( b = 0; b < MASA_BITS_ER_HR; b++ ) + { + q_direction->band_data[0].energy_ratio_index[0] = ( q_direction->band_data[0].energy_ratio_index[0] << 1 ) + bitstream[( *index )--]; + } + *diffuseness_index_max_ec_frame = 5; + + return MASA_BITS_ER_HR; + } + + if ( bitstream[( *index )--] == 0 ) /* dif_use_raw_coding */ + { + /* Decode with similarity strategy with low band count. On higher band counts, decode with Huffman-coding strategy. */ + + if ( bitstream[( *index )--] != 0 ) /* dif_have_unique_value */ + { + dif_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, HR_MASA_ER_LEVELS ); /* dif_unique_value */ + + for ( b = start_band; b < nbands; b++ ) + { + q_direction->band_data[b].energy_ratio_index[0] = dif_min; + } + } + else /* all diffuseness values are dif_min_value or dif_min_value + 1 */ + { + dif_min = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, HR_MASA_ER_LEVELS - 1 ); /* dif_min_value */ + + for ( b = start_band; b < nbands; b++ ) + { + q_direction->band_data[b].energy_ratio_index[0] = dif_min + bitstream[( *index )--]; /* dif_bit_offset_values */ + } + } + } + else /* different values for diffuseness */ + { + dif_min = HR_MASA_ER_LEVELS; + + for ( b = start_band; b < nbands; b++ ) + { + q_direction->band_data[b].energy_ratio_index[0] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, HR_MASA_ER_LEVELS ); + dif_min = min( dif_min, q_direction->band_data[b].energy_ratio_index[0] ); + } + } + + *diffuseness_index_max_ec_frame = 10; + /* adaptively select the diffuseness_index_max_ec threshold */ + if ( dif_min > 10 ) + { + *diffuseness_index_max_ec_frame = HR_MASA_ER_LEVELS - 1; + } + + return ( index_start - *index ); +} + +static int16_t ivas_qmetadata_entropy_decode_diffuseness_hr_512( + uint16_t *bitstream, /* i : bitstream */ + int16_t *index, + IVAS_QDIRECTION *q_direction ) +{ + int16_t b, k; + + int16_t index_start; + int16_t nbands, nblocks; + int16_t start_band; + + index_start = *index; + nbands = q_direction->cfg.nbands; + nblocks = q_direction->cfg.nblocks; + start_band = q_direction->cfg.start_band; + + /* diffuseness decoding */ + /* Handle one band as special case*/ + if ( nbands == 1 ) + { + q_direction->band_data[0].energy_ratio_index[0] = 0; + for ( b = 0; b < MASA_BITS_ER_HR; b++ ) + { + q_direction->band_data[0].energy_ratio_index[0] = ( q_direction->band_data[0].energy_ratio_index[0] << 1 ) + bitstream[( *index )--]; + } + + return MASA_BITS_ER_HR; + } + + + for ( b = start_band; b < nbands; b++ ) + { + for ( k = 0; k < nblocks; k++ ) + { + q_direction->band_data[b].energy_ratio_index[k] = ivas_qmetadata_DecodeQuasiUniform( bitstream, index, HR_MASA_ER_LEVELS ); + } + } + + return ( index_start - *index ); +} +#endif + static int16_t ivas_qmetadata_entropy_decode_df_ratio( uint16_t *bitstream, int16_t *index, @@ -1161,7 +1690,12 @@ static int16_t ivas_qmetadata_entropy_decode_dir( int16_t *index, const uint16_t diffuseness_index_max_ec_frame, const int16_t nbands, - const int16_t start_band ) + const int16_t start_band +#ifdef HR_METADATA + , + int16_t is_hr +#endif +) { int16_t b, m; int16_t diff_idx; @@ -1188,7 +1722,18 @@ static int16_t ivas_qmetadata_entropy_decode_dir( /*Raw coding for high diffuseness*/ for ( b = start_band; b < nbands; b++ ) { - diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0]; +#ifdef HR_METADATA + if ( is_hr ) + { + diff_idx = 0; + } + else + { +#endif + diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0]; +#ifdef HR_METADATA + } +#endif diff_idx_min = min( diff_idx_min, diff_idx ); if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) { @@ -1198,8 +1743,11 @@ static int16_t ivas_qmetadata_entropy_decode_dir( { elev_alph[b] = no_theta_masa[bits_direction_masa[diff_idx] - 3] * 2 - 1; } - +#ifdef HR_METADATA + if ( q_direction->band_data[b].energy_ratio_index_mod[0] > diffuseness_index_max_ec_frame ) +#else if ( diff_idx > diffuseness_index_max_ec_frame ) +#endif { bands_entropic[b] = 0; @@ -1252,7 +1800,18 @@ static int16_t ivas_qmetadata_entropy_decode_dir( if ( bands_entropic[b] ) { int16_t tmp_index; - diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0]; +#ifdef HR_METADATA + if ( is_hr ) + { + diff_idx = 0; + } + else + { +#endif + diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0]; +#ifdef HR_METADATA + } +#endif if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) { @@ -1300,7 +1859,19 @@ static int16_t ivas_qmetadata_entropy_decode_dir( { if ( bands_entropic[b] ) { - diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0]; +#ifdef HR_METADATA + if ( is_hr ) + { + diff_idx = 0; + } + else + { + +#endif + diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0]; +#ifdef HR_METADATA + } +#endif for ( m = 0; m < nblocks; m++ ) { int16_t tmp_index; @@ -1359,7 +1930,19 @@ static int16_t ivas_qmetadata_entropy_decode_dir( { if ( bands_entropic[b] ) { - diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0]; +#ifdef HR_METADATA + if ( is_hr ) + { + diff_idx = 0; + } + else + { + +#endif + diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0]; +#ifdef HR_METADATA + } +#endif for ( m = 0; m < nblocks; m++ ) { q_direction->band_data[b].elevation_index[m] = 0; @@ -1477,7 +2060,58 @@ static int16_t ivas_qmetadata_entropy_decode_dir( return ( index_start - *index ); } +#ifdef HR_METADATA +/*------------------------------------------------------------------------- + * ivas_qmetadata_raw_decode_dir() + * + * Main function for raw decoding of the directions + *------------------------------------------------------------------------*/ + +static int16_t ivas_qmetadata_raw_decode_dir_512( + IVAS_QDIRECTION *q_direction, /* i/o: quantized direction structure */ + uint16_t *bitstream, /* i : bitstream */ + int16_t *index, + const int16_t nbands, + const int16_t start_band, + SPHERICAL_GRID_DATA *sph_grid16 /* i: spherical grid for deindexing */ +) +{ + int16_t b, m, i; + int16_t nblocks; + int16_t index_start; + uint16_t value; + + index_start = *index; + nblocks = q_direction->cfg.nblocks; + + for ( b = start_band; b < nbands; b++ ) + { + for ( m = 0; m < nblocks; m++ ) + { + value = 0; + for ( i = 0; i < q_direction->band_data[b].bits_sph_idx[m]; i++ ) + { + value = ( value << 1 ) + bitstream[( *index )--]; + } + q_direction->band_data[b].spherical_index[m] = value; + if ( q_direction->band_data[b].bits_sph_idx[m] == 16 ) + { + deindex_sph_idx( value, sph_grid16, &( q_direction->band_data[b].elevation[m] ), &( q_direction->band_data[b].azimuth[m] ) ); + } + else + { + 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 ); + } + } + } + + return ( index_start - *index ); +} + +#endif /*------------------------------------------------------------------------- * ivas_qmetadata_raw_decode_dir() * @@ -1489,7 +2123,12 @@ static int16_t ivas_qmetadata_raw_decode_dir( uint16_t *bitstream, /* i : bitstream */ int16_t *index, const int16_t nbands, - const int16_t start_band ) + const int16_t start_band +#ifdef HR_METADATA + , + int16_t is_hr +#endif +) { int16_t b, m, azith_alph; int16_t diff_idx; @@ -1507,7 +2146,18 @@ static int16_t ivas_qmetadata_raw_decode_dir( } else { - diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0]; +#ifdef HR_METADATA + if ( is_hr ) + { + diff_idx = 0; + } + else + { +#endif + diff_idx = q_direction->band_data[b].energy_ratio_index_mod[0]; +#ifdef HR_METADATA + } +#endif for ( m = 0; m < nblocks; m++ ) { @@ -2548,6 +3198,10 @@ static void decode_spread_coherence( IVAS_QMETADATA_HANDLE hQMetaData, /* i/o: quantized metadata structure */ int16_t idx_d, /* i : direction index */ const int16_t no_frames /* i : number of time subframes */ +#ifdef HR_METADATA + , + int16_t is_hr +#endif ) { int16_t i, j; @@ -2557,7 +3211,9 @@ static void decode_spread_coherence( int16_t MASA_grouping[MASA_MAXIMUM_CODING_SUBBANDS]; IVAS_QDIRECTION *q_direction; int16_t coding_subbands, coding_subbands_0, d, two_dir_band[MASA_MAXIMUM_CODING_SUBBANDS]; - +#ifdef HR_METADATA + int16_t min_index; +#endif coding_subbands_0 = hQMetaData->q_direction[0].cfg.nbands; coding_subbands = hQMetaData->q_direction[idx_d].cfg.nbands; if ( coding_subbands_0 <= 5 ) @@ -2612,7 +3268,26 @@ static void decode_spread_coherence( for ( i = 0; i < coding_subbands; i++ ) { var_azi = var( q_direction->band_data[i].azimuth, no_frames ); +#ifdef HR_METADATA + if ( is_hr ) + { + minimum_s( (int16_t *) ( q_direction->band_data[i].energy_ratio_index ), q_direction->cfg.nblocks, &min_index ); + min_index = min_index >> 1; + } + else + { + min_index = q_direction->band_data[i].energy_ratio_index[0]; + } + if ( var_azi < MASA_DELTA_AZI_DCT0 ) + { + idx_sub_cb = MASA_NO_CV_COH * min_index; + } + else + { + idx_sub_cb = MASA_NO_CV_COH * ( min_index + DIRAC_DIFFUSE_LEVELS ); /* NO_CV_COH = 8 */ + } +#else if ( var_azi < MASA_DELTA_AZI_DCT0 ) { idx_sub_cb = MASA_NO_CV_COH * q_direction->band_data[i].energy_ratio_index[0]; @@ -2621,7 +3296,7 @@ static void decode_spread_coherence( { idx_sub_cb = MASA_NO_CV_COH * ( q_direction->band_data[i].energy_ratio_index[0] + DIRAC_DIFFUSE_LEVELS ); /* NO_CV_COH = 8 */ } - +#endif dct_coh[i][0] = coherence_cb0_masa[idx_sub_cb + q_direction->coherence_band_data[i].spread_coherence_dct0_index]; if ( coding_subbands < coding_subbands_0 ) { @@ -2879,7 +3554,79 @@ static int16_t decode_fixed_rate_composed_index_coherence( return nbits; } +#ifdef HR_METADATA +/* !r: number of bits read */ +static int16_t read_coherence_data_hr_512( + uint16_t *bitstream, /* i : bitstream */ + int16_t *p_bit_pos, /* i : position in the bitstream */ + IVAS_QMETADATA *hQMetaData, /* i/o: quantized metadata structure */ + const int16_t idx_dir, /* i : direction index */ + int16_t nbits_coh ) +{ + int16_t j, k, i; + int16_t nbands, nblocks; + int16_t min_index, GR_param; + int16_t cb_size, nbits; + int16_t decoded_idx; + float delta; + + nbands = hQMetaData->q_direction[idx_dir].cfg.nbands; + nblocks = hQMetaData->q_direction[idx_dir].cfg.nblocks; + + + cb_size = 1 << nbits_coh; + delta = 256.0f / cb_size; + nbits = *p_bit_pos; + for ( k = 0; k < nblocks; k++ ) + { + /* read method */ + if ( bitstream[( *p_bit_pos )--] == 1 ) + { + /* average removed */ + /* read average index */ + min_index = 0; + for ( i = 0; i < nbits_coh; i++ ) + { + min_index = ( min_index << 1 ) + bitstream[( *p_bit_pos )--]; + } + /* read GR param */ + GR_param = bitstream[( *p_bit_pos )--]; + for ( j = 0; j < nbands; j++ ) + { + decoded_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, p_bit_pos, 2 * cb_size, GR_param ); + if ( decoded_idx % 2 ) + { + decoded_idx = ( ( decoded_idx + 1 ) >> 1 ) + min_index; + } + else + { + decoded_idx = -( decoded_idx >> 1 ) + min_index; + } + hQMetaData->q_direction[idx_dir].coherence_band_data[j].spread_coherence[k] = (uint8_t) ( decoded_idx * delta + delta / 2.0f ); + } + } + else + { + /* read min_index */ + min_index = 0; + for ( i = 0; i < nbits_coh; i++ ) + { + min_index = ( min_index << 1 ) + bitstream[( *p_bit_pos )--]; + } + /* read GR param */ + GR_param = bitstream[( *p_bit_pos )--]; + for ( j = 0; j < nbands; j++ ) + { + decoded_idx = ivas_qmetadata_DecodeExtendedGR( bitstream, p_bit_pos, cb_size - min_index, GR_param ) + min_index; + hQMetaData->q_direction[idx_dir].coherence_band_data[j].spread_coherence[k] = (uint8_t) ( decoded_idx * delta + delta / 2.0f ); + } + } + } + nbits = nbits - *p_bit_pos; + return nbits; +} +#endif /*------------------------------------------------------------------ - * * read_coherence_data() * @@ -2892,6 +3639,10 @@ static int16_t read_coherence_data( int16_t *p_bit_pos, /* i : position in the bitstream */ IVAS_QMETADATA *hQMetaData, /* i/o: quantized metadata structure */ const int16_t idx_dir /* i : direction index */ +#ifdef HR_METADATA + , + int16_t is_hr +#endif ) { int16_t j; @@ -2909,6 +3660,9 @@ static int16_t read_coherence_data( int16_t decoded_idx[MASA_MAXIMUM_CODING_SUBBANDS]; uint16_t byteBuffer; int16_t idx_ER; +#ifdef HR_METADATA + int16_t min_index; +#endif coding_subbands = hQMetaData->q_direction[idx_dir].cfg.nbands; q_direction = &( hQMetaData->q_direction[idx_dir] ); @@ -2919,7 +3673,19 @@ static int16_t read_coherence_data( { for ( j = 0; j < coding_subbands; j++ ) { - idx_ER = 7 - q_direction->band_data[j].energy_ratio_index_mod[0] + coding_subbands / MASA_FACTOR_CV_COH; +#ifdef HR_METADATA + if ( is_hr ) + { + idx_ER = 7 - ( q_direction->band_data[j].energy_ratio_index_mod[0] >> 1 ) + coding_subbands / MASA_FACTOR_CV_COH; + } + else + { + +#endif + idx_ER = 7 - q_direction->band_data[j].energy_ratio_index_mod[0] + coding_subbands / MASA_FACTOR_CV_COH; +#ifdef HR_METADATA + } +#endif no_cv_vec[j] = idx_ER + 1; } @@ -2943,7 +3709,11 @@ static int16_t read_coherence_data( { if ( no_cv_vec[j] > 1 ) { +#ifdef HR_METADATA + 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] >> ( is_hr ) ) + coding_subbands / MASA_FACTOR_CV_COH ) ) ); +#else 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] + coding_subbands / MASA_FACTOR_CV_COH ) ) ); +#endif } else { @@ -2960,7 +3730,11 @@ static int16_t read_coherence_data( { if ( no_cv_vec[j] > 1 ) { +#ifdef HR_METADATA + 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] >> ( is_hr ) ) + coding_subbands / MASA_FACTOR_CV_COH ) ) ); +#else 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] + coding_subbands / MASA_FACTOR_CV_COH ) ) ); +#endif } else { @@ -2973,7 +3747,19 @@ static int16_t read_coherence_data( { for ( j = 0; j < coding_subbands; j++ ) { - no_cv_vec[j] = len_cb_dct0_masa[q_direction->band_data[j].energy_ratio_index[0]]; /* spread coherence DCT0*/ +#ifdef HR_METADATA + if ( is_hr ) + { + minimum_s( (int16_t *) ( q_direction->band_data[j].energy_ratio_index ), q_direction->cfg.nblocks, &min_index ); + no_cv_vec[j] = len_cb_dct0_masa[min_index >> 1]; /* spread coherence DCT0*/ + } + else + { +#endif + no_cv_vec[j] = len_cb_dct0_masa[q_direction->band_data[j].energy_ratio_index[0]]; /* spread coherence DCT0*/ +#ifdef HR_METADATA + } +#endif } if ( sum_s( no_cv_vec, coding_subbands ) > MASA_COH_LIMIT_2IDX ) @@ -3096,13 +3882,14 @@ static int16_t read_coherence_data( return nbits; } + /*-------------------------------------------------------------------* * read_surround_coherence() * * *-------------------------------------------------------------------*/ -int16_t read_surround_coherence( +static int16_t read_surround_coherence( uint16_t *bitstream, /* i : bitstream */ int16_t *p_bit_pos, /* i : position in the bitstream */ IVAS_QMETADATA *hQMetaData /* i/o: quantized metadata structure */ @@ -3256,7 +4043,162 @@ int16_t read_surround_coherence( return bits_sur_coherence; } +#ifdef HR_METADATA +static int16_t read_surround_coherence_hr( + uint16_t *bitstream, /* i : bitstream */ + int16_t *p_bit_pos, /* i : position in the bitstream */ + IVAS_QMETADATA *hQMetaData /* i/o: quantized metadata structure */ +) +{ + int16_t coding_subbands; + int16_t no_cv_vec[MASA_MAXIMUM_CODING_SUBBANDS]; + int16_t bit_pos; + float error_ratio_surr; + int16_t idx_ER[MASA_MAXIMUM_CODING_SUBBANDS]; + int16_t bits_sur_coherence, bits_GR; + int16_t j, k, sf; + uint16_t byteBuffer; + uint16_t idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS]; + IVAS_QDIRECTION *q_direction; + int16_t min_index; + int16_t d, idx; + coding_subbands = hQMetaData->q_direction[0].cfg.nbands; + q_direction = hQMetaData->q_direction; + + bits_sur_coherence = 0; + bit_pos = *p_bit_pos; + + for ( sf = 0; sf < hQMetaData->q_direction[0].cfg.nblocks; sf++ ) + { + d = 0; + for ( j = 0; j < coding_subbands; j++ ) + { + error_ratio_surr = 1.0f; + if ( hQMetaData->no_directions == 2 ) + { + d += hQMetaData->twoDirBands[j]; + idx = max( d - 1, 0 ); + 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]; + } + else + { + error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[sf]; + } + + + if ( error_ratio_surr <= 0 ) + { + error_ratio_surr = 0; + no_cv_vec[j] = 1; + idx_ER[j] = 0; + } + else + { + idx_ER[j] = 7; // masa_sq( error_ratio_surr, diffuseness_thresholds, DIRAC_DIFFUSE_LEVELS ); + no_cv_vec[j] = idx_cb_sur_coh_masa[idx_ER[j]] + 2; + } + } + + if ( sum_s( no_cv_vec, coding_subbands ) == coding_subbands ) + { + /* surround coherence is zero */ + for ( j = 0; j < coding_subbands; j++ ) + { + for ( k = 0; k < MAX_PARAM_SPATIAL_SUBFRAMES; k++ ) + { + if ( hQMetaData->surcoh_band_data != NULL ) + { + hQMetaData->surcoh_band_data[j].surround_coherence[k] = 0; + } + } + } + } + else + { + + /* read how the surround coherence is encoded */ + byteBuffer = bitstream[bit_pos--]; + bits_sur_coherence += 1; + + if ( byteBuffer & 1 ) + { + /* GR decoding */ + /* read GR order */ + byteBuffer = bitstream[bit_pos--]; + bits_sur_coherence += 1; + + /* read min index */ + bits_GR = bit_pos; + min_index = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, MASA_MAX_NO_CV_SUR_COH, 0 ); + bits_sur_coherence += bits_GR - bit_pos; + + /* read GR data */ + for ( j = 0; j < coding_subbands; j++ ) + { + bits_GR = bit_pos; + /* decoding for min removed */ + if ( no_cv_vec[j] > 1 ) + { + idx_sur_coh[j] = ivas_qmetadata_DecodeExtendedGR( bitstream, &bit_pos, no_cv_vec[j] - min_index, ( byteBuffer & 1 ) ); + bits_sur_coherence += bits_GR - bit_pos; + } + else + { + idx_sur_coh[j] = 0; + } + } + + for ( j = 0; j < coding_subbands; j++ ) + { + if ( no_cv_vec[j] > 1 ) + { + hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j] + min_index; + 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]; + } + else + { + hQMetaData->surcoh_band_data[j].sur_coherence_index = idx_sur_coh[j]; + hQMetaData->surcoh_band_data[j].surround_coherence[sf] = 0; + } + } + } + else + { + /* fixed rate */ + uint16_t sur_coh_temp_index[MASA_MAXIMUM_CODING_SUBBANDS]; + set_s( (int16_t *) sur_coh_temp_index, 0, MASA_MAXIMUM_CODING_SUBBANDS ); + + decode_fixed_rate_composed_index_coherence( bitstream, &bit_pos, coding_subbands, no_cv_vec, sur_coh_temp_index, MASA_MAX_NO_CV_SUR_COH ); + + for ( j = 0; j < coding_subbands; j++ ) + { + hQMetaData->surcoh_band_data[j].sur_coherence_index = sur_coh_temp_index[j]; + } + + /* deindex surround coherence */ + for ( j = 0; j < coding_subbands; j++ ) + { + if ( no_cv_vec[j] > 1 ) + { + 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]; + } + else + { + hQMetaData->surcoh_band_data[j].surround_coherence[sf] = 0; + } + } + } + } + } + + /* Replace return value with the actual read bits. bits_sur_coherence might show wrong count at this point. */ + bits_sur_coherence = *p_bit_pos - bit_pos; + *p_bit_pos = bit_pos; + + return bits_sur_coherence; +} +#endif /*-------------------------------------------------------------------* * decode_combined_index() diff --git a/lib_enc/ivas_masa_enc.c b/lib_enc/ivas_masa_enc.c index 2a21a667e941be7447a36cd81869bf394896a49e..5b5363efb3eda26af319e9559d085bfcc877d7b5 100644 --- a/lib_enc/ivas_masa_enc.c +++ b/lib_enc/ivas_masa_enc.c @@ -61,7 +61,12 @@ static int16_t encode_lfe_to_total_energy_ratio( MASA_ENCODER_HANDLE hMasa, BSTR static void reduce_metadata_further( MASA_ENCODER_HANDLE hMasa, IVAS_QMETADATA_HANDLE hqmetadata, const IVAS_FORMAT ivas_format ); -static void average_masa_metadata( MASA_METADATA_FRAME *masaMetadata, float energy[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS] ); +static void average_masa_metadata( MASA_METADATA_FRAME *masaMetadata, float energy[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS] +#ifdef HR_METADATA + , + SPHERICAL_GRID_DATA *sphGrid +#endif +); static void copy_masa_metadata_subframe( const MASA_METADATA_HANDLE hMetaFrom, const uint8_t sfFrom, MASA_METADATA_HANDLE hMetaTo, const uint8_t sfTo ); @@ -245,10 +250,18 @@ ivas_error ivas_masa_encode( mvr2r( hMasa->masaMetadata.directional_meta[i].azimuth[j], h_orig_metadata[i].azimuth[j], MASA_FREQUENCY_BANDS ); mvr2r( hMasa->masaMetadata.directional_meta[i].elevation[j], h_orig_metadata[i].elevation[j], MASA_FREQUENCY_BANDS ); mvr2r( hMasa->masaMetadata.directional_meta[i].energy_ratio[j], h_orig_metadata[i].energy_ratio[j], MASA_FREQUENCY_BANDS ); +#ifdef HR_METADATA + mvs2s( (int16_t *) ( hMasa->masaMetadata.directional_meta[i].spherical_index[j] ), (int16_t *) ( h_orig_metadata[i].spherical_index[j] ), MASA_FREQUENCY_BANDS ); +#endif } } } - +#ifdef HR_METADATA + if ( ivas_format == MASA_FORMAT && ivas_total_brate >= IVAS_384k ) + { + hMasa->config.mergeRatiosOverSubframes = 0; + } +#endif /* Combine frequency bands and sub-frames */ combine_freqbands_and_subframes( hMasa ); } @@ -312,7 +325,25 @@ ivas_error ivas_masa_encode( } /* Encode metadata */ - ivas_qmetadata_enc_encode( hMetaData, hQMetaData ); +#ifdef HR_METADATA + if ( ivas_total_brate >= IVAS_384k ) + { + if ( ivas_total_brate >= IVAS_512k ) + { + ivas_qmetadata_enc_encode_hr_384_512( hMetaData, hQMetaData, 16, 4 ); + } + else + { + ivas_qmetadata_enc_encode_hr_384_512( hMetaData, hQMetaData, 11, 3 ); + } + } + else + { +#endif + ivas_qmetadata_enc_encode( hMetaData, hQMetaData ); +#ifdef HR_METADATA + } +#endif *nb_bits_metadata = hMetaData->nb_bits_tot; @@ -470,6 +501,9 @@ ivas_error ivas_masa_enc_config( uint8_t isActualTwoDir; /* Flag to tell that when there are two directions present in metadata, they both contain meaningful information. */ int32_t ivas_total_brate; ivas_error error; +#ifdef HR_METADATA + SPHERICAL_GRID_DATA *sphGrid; +#endif error = IVAS_ERR_OK; @@ -492,8 +526,27 @@ ivas_error ivas_masa_enc_config( if ( hMasa->data.sync_state.frame_mode == MASA_FRAME_1SF && hMasa->data.sync_state.prev_offset != 0 ) { +#ifdef HR_METADATA + if ( ( sphGrid = (SPHERICAL_GRID_DATA *) malloc( sizeof( SPHERICAL_GRID_DATA ) ) ) == NULL ) + { + return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MASA spherical grid\n" ) ); + } + if ( ivas_total_brate == IVAS_512k ) + { + generate_gridEq( sphGrid ); + } + else + { + sphGrid->no_theta = 0; + } +#endif /* average over sub-frames */ - average_masa_metadata( &( hMasa->masaMetadata ), hMasa->data.energy ); + average_masa_metadata( &( hMasa->masaMetadata ), hMasa->data.energy +#ifdef HR_METADATA + , + sphGrid +#endif + ); } /* Inspect metadata for parameter changes that affect coding. */ @@ -1023,6 +1076,9 @@ static void move_metadata_to_qmetadata( hQMeta->q_direction[dir].band_data[band].azimuth[sf] = hMeta->directional_meta[dir].azimuth[sf][band]; hQMeta->q_direction[dir].band_data[band].elevation[sf] = hMeta->directional_meta[dir].elevation[sf][band]; hQMeta->q_direction[dir].band_data[band].energy_ratio[sf] = hMeta->directional_meta[dir].energy_ratio[sf][band]; +#ifdef HR_METADATA + hQMeta->q_direction[dir].band_data[band].spherical_index[sf] = hMeta->directional_meta[dir].spherical_index[sf][band]; +#endif if ( hQMeta->q_direction[dir].coherence_band_data != NULL ) { hQMeta->q_direction[dir].coherence_band_data[band].spread_coherence[sf] = (uint8_t) roundf( hMeta->directional_meta[dir].spread_coherence[sf][band] * UINT8_MAX ); @@ -1710,7 +1766,12 @@ void ivas_masa_enc_reconfigure( static void average_masa_metadata( MASA_METADATA_FRAME *hMeta, - float energy[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS] ) + float energy[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS] +#ifdef HR_METADATA + , + SPHERICAL_GRID_DATA *Sph_Grid16 +#endif +) { int16_t i, j, k; float azi_rad, ele_rad; @@ -1757,7 +1818,13 @@ static void average_masa_metadata( j = 0; hMeta->directional_meta[i].azimuth[j][k] = atan2f( y_sum, x_sum ) / EVS_PI * 180.0f; hMeta->directional_meta[i].elevation[j][k] = atan2f( z_sum, sqrtf( x_sum * x_sum + y_sum * y_sum ) ) / EVS_PI * 180.0f; - +#ifdef HR_METADATA + if ( Sph_Grid16->no_theta > 0 ) + { + hMeta->directional_meta[i].spherical_index[j][k] = index_theta_phi_16( &( hMeta->directional_meta[i].elevation[j][k] ), + &( hMeta->directional_meta[i].azimuth[j][k] ), Sph_Grid16 ); + } +#endif vec_len = sqrtf( x_sum * x_sum + y_sum * y_sum + z_sum * z_sum ); hMeta->directional_meta[i].energy_ratio[j][k] = vec_len / ( energy_sum + EPSILON ); diff --git a/lib_enc/ivas_qmetadata_enc.c b/lib_enc/ivas_qmetadata_enc.c index ac4f83a65794d7bbcdc2104e7b858d5bc8319ff7..3a947584e9f869707194d55857b7d1d2d3de40a0 100644 --- a/lib_enc/ivas_qmetadata_enc.c +++ b/lib_enc/ivas_qmetadata_enc.c @@ -60,7 +60,12 @@ static void ivas_qmetadata_reorder_2dir_bands( IVAS_QMETADATA_HANDLE hQMetaData static int16_t ivas_qmetadata_entropy_encode_df_ratio( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, int16_t *df_ratio_bits ); -static int16_t ivas_qmetadata_entropy_encode_dir( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, const uint16_t diffuseness_index_max_ec_frame, const int16_t nbands, const int16_t start_band, const int16_t direction_bits_raw, int16_t max_bits ); +static int16_t ivas_qmetadata_entropy_encode_dir( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, const uint16_t diffuseness_index_max_ec_frame, const int16_t nbands, const int16_t start_band, const int16_t direction_bits_raw, int16_t max_bits +#ifdef HR_METADATA + , + int16_t is_hr +#endif +); static int16_t ivas_qmetadata_raw_encode_dir( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, const int16_t nbands, const int16_t start_band ); @@ -88,11 +93,21 @@ static ivas_error write_ec_direction( int16_t *num_bits_written, BSTR_ENC_HANDLE static int16_t write_fixed_rate_direction( BSTR_ENC_HANDLE hMetaData, IVAS_QDIRECTION *q_direction, const int16_t j_idx, const int16_t len ); -static int16_t ivas_qmetadata_quantize_coherence( IVAS_QMETADATA *hQMetaData, const int16_t idx_d, const int16_t all_coherence_zero, BSTR_ENC_HANDLE hMetaData, const int16_t write_flag, int16_t *indice_coherence ); +static int16_t ivas_qmetadata_quantize_coherence( IVAS_QMETADATA *hQMetaData, const int16_t idx_d, const int16_t all_coherence_zero, BSTR_ENC_HANDLE hMetaData, const int16_t write_flag, int16_t *indice_coherence +#ifdef HR_METADATA + , + int16_t is_hr +#endif +); static void dct4_transform( uint8_t *v, float *dct_v ); -static float quantize_DCT_0_coh( const float x, const int16_t j, const float *coherence_cb, const float delta_var, const int16_t no_cb, IVAS_QDIRECTION *q_direction, uint16_t *idx_x, int16_t *p_no_cb ); +static float quantize_DCT_0_coh( const float x, const int16_t j, const float *coherence_cb, const float delta_var, const int16_t no_cb, IVAS_QDIRECTION *q_direction, uint16_t *idx_x, int16_t *p_no_cb +#ifdef HR_METADATA + , + int16_t is_hr +#endif +); static int16_t encode_coherence_indexesDCT0( uint16_t *idx_dct, const int16_t len, int16_t *no_cb_vec, BSTR_ENC_HANDLE hMetaData, int16_t indice_coherence, const int16_t nbits, const int16_t nbits1 ); @@ -117,6 +132,40 @@ static int16_t write_2dir_info( BSTR_ENC_HANDLE hMetaData, uint8_t *twoDirBands, static void transform_azimuth_dir2( IVAS_QMETADATA_HANDLE hQMetaData, int16_t *dir2_bands ); static int16_t calc_var_azi( const IVAS_QDIRECTION *q_direction, const int16_t diffuseness_index_max_ec_frame, const float avg_azimuth, float *avg_azimuth_out ); +#ifdef HR_METADATA +static void ivas_qmetadata_quantize_diffuseness_nrg_ratios_hr( + IVAS_QMETADATA_HANDLE hQMetaData, + int16_t *needed_bits, + int16_t *nbits_diff, + int16_t *dfRatioBits, + int16_t bits_dir_hr ); +static int16_t ivas_qmetadata_entropy_encode_diffuseness_hr( + BSTR_ENC_HANDLE hMetaData, + IVAS_QDIRECTION *q_direction, + uint16_t *diffuseness_index_max_ec_frame ); + +static void ivas_qmetadata_quantize_diffuseness_nrg_ratios_hr_512( + IVAS_QMETADATA_HANDLE hQMetaData, + int16_t *needed_bits, + int16_t bits_dir_hr, + BSTR_ENC_HANDLE hMetaData ); + +static int16_t encode_surround_coherence_hr( + IVAS_QMETADATA *hQMetaData, /* i : quantized metadata */ + BSTR_ENC_HANDLE hMetaData /* i/o: metadata bitstream handle */ +); + +static void ivas_qmetadata_reorder_2dir_bands_hr( + IVAS_QMETADATA_HANDLE hQMetaData ); + +static int16_t ivas_qmetadata_quantize_coherence_hr_512( + IVAS_QMETADATA *hQMetaData, /* i/o: quantized metadata */ + const int16_t idx_d, /* i : current direction index */ + const int16_t all_coherence_zero, /* i : all coherence is zero - flag */ + BSTR_ENC_HANDLE hMetaData, /* i : metadata handle */ + int16_t bits_coh ); + +#endif /*-----------------------------------------------------------------------* * ivas_qmetadata_enc_encode() @@ -185,6 +234,7 @@ ivas_error ivas_qmetadata_enc_encode( pF_surcoh = fopen( "./res/qmetadata_surcoh_enc.txt", "w" ); #endif + /* Save initial position in bitstream */ bit_pos_0 = hMetaData->nb_bits_tot; bit_pos_start = bit_pos_0; @@ -380,9 +430,15 @@ ivas_error ivas_qmetadata_enc_encode( /*Coherence */ bits_coherence[d] = 0; bit_pos_start_coh = hMetaData->nb_bits_tot; + if ( all_coherence_zero == 0 ) { - bits_coherence[d] = ivas_qmetadata_quantize_coherence( hQMetaData, d, all_coherence_zero, hMetaData, 0, &indice_coherence ); + bits_coherence[d] = ivas_qmetadata_quantize_coherence( hQMetaData, d, all_coherence_zero, hMetaData, 0, &indice_coherence +#ifdef HR_METADATA + , + 0 +#endif + ); } if ( q_direction->cfg.mc_ls_setup == MC_LS_SETUP_5_1 || q_direction->cfg.mc_ls_setup == MC_LS_SETUP_7_1 ) @@ -394,7 +450,12 @@ ivas_error ivas_qmetadata_enc_encode( else { /* Quantize directions*/ - quantize_direction_frame( q_direction, azimuth_orig, elevation_orig ); + quantize_direction_frame( q_direction, azimuth_orig, elevation_orig +#ifdef HR_METADATA + , + 0 +#endif + ); } /* Signalling 2D*/ @@ -420,7 +481,12 @@ ivas_error ivas_qmetadata_enc_encode( reduce_bits = hQMetaData->is_masa_ivas_format ? ( total_bits_1dir - ( bits_diff[d] + bits_coherence[d] + bits_signaling[d] ) - 1 ) : MASA_MAX_BITS; bits_ec = ivas_qmetadata_entropy_encode_dir( hMetaData, q_direction, diffuseness_index_max_ec_frame, - q_direction->cfg.nbands, q_direction->cfg.start_band, bits_dir_bands[0], reduce_bits ); + q_direction->cfg.nbands, q_direction->cfg.start_band, bits_dir_bands[0], reduce_bits +#ifdef HR_METADATA + , + 0 +#endif + ); if ( bits_ec < 0 ) { @@ -466,7 +532,12 @@ ivas_error ivas_qmetadata_enc_encode( bits_dir_bands[i] = ivas_qmetadata_raw_encode_dir( NULL, q_direction, i + 1, i ); /* Write ec bits */ - bits_ec = ivas_qmetadata_entropy_encode_dir( hMetaData, q_direction, diffuseness_index_max_ec_frame, i + 1, i, bits_dir_bands[i], MASA_MAX_BITS ); + bits_ec = ivas_qmetadata_entropy_encode_dir( hMetaData, q_direction, diffuseness_index_max_ec_frame, i + 1, i, bits_dir_bands[i], MASA_MAX_BITS +#ifdef HR_METADATA + , + 0 +#endif + ); if ( bits_ec >= 0 ) { @@ -565,7 +636,12 @@ ivas_error ivas_qmetadata_enc_encode( { bit_pos_start = hMetaData->nb_bits_tot; hMetaData->nb_bits_tot = bit_pos_start_coh; - ivas_qmetadata_quantize_coherence( hQMetaData, d, all_coherence_zero, hMetaData, 1, &indice_coherence ); + ivas_qmetadata_quantize_coherence( hQMetaData, d, all_coherence_zero, hMetaData, 1, &indice_coherence +#ifdef HR_METADATA + , + 0 +#endif + ); hMetaData->nb_bits_tot = bit_pos_start; } @@ -653,6 +729,238 @@ ivas_error ivas_qmetadata_enc_encode( return error; } +#ifdef HR_METADATA + +ivas_error ivas_qmetadata_enc_encode_hr_384_512( + BSTR_ENC_HANDLE hMetaData, /* i/o: metadata bitstream handle */ + IVAS_QMETADATA *hQMetaData, /* i/o: metadata handle */ + int16_t bits_sph_idx, + int16_t bits_sp_coh ) +{ + int16_t i, j; + + int16_t bits_diff[QMETADATA_MAX_NO_DIRECTIONS]; + IVAS_QDIRECTION *q_direction; + int16_t nbands, nblocks, start_band; + int16_t ndirections, d; + int16_t all_coherence_zero; + int16_t bits_no_dirs_coh; + int16_t bits_ec; + float azimuth_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES], elevation_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES]; + + ivas_error error; + + error = IVAS_ERR_OK; + +#ifdef DEBUG_MODE_QMETADATA + + static FILE *pF = NULL; + static FILE *pF_azi = NULL; + static FILE *pF_ele = NULL; + static FILE *pF_ratio = NULL; + static FILE *pF_spcoh = NULL; + static FILE *pF_spcoh_orig = NULL; + static FILE *pF_surcoh = NULL; + + if ( pF == NULL ) + pF = fopen( "./res/qmetadata_enc.txt", "w" ); + if ( pF_azi == NULL ) + pF_azi = fopen( "./res/qmetadata_azi_enc.txt", "w" ); + if ( pF_ele == NULL ) + pF_ele = fopen( "./res/qmetadata_ele_enc.txt", "w" ); + if ( pF_ratio == NULL ) + pF_ratio = fopen( "./res/qmetadata_ratio_enc.txt", "w" ); + if ( pF_spcoh == NULL ) + pF_spcoh = fopen( "./res/qmetadata_spcoh_enc.txt", "w" ); + if ( pF_spcoh_orig == NULL ) + pF_spcoh_orig = fopen( "./res/qmetadata_spcoh_orig.txt", "w" ); + if ( pF_surcoh == NULL ) + pF_surcoh = fopen( "./res/qmetadata_surcoh_enc.txt", "w" ); +#endif + + ndirections = hQMetaData->no_directions; + + + /* Check if coherence should be encoded */ + all_coherence_zero = 1; + bits_no_dirs_coh = 0; + + if ( hQMetaData->coherence_flag ) + { + all_coherence_zero = hQMetaData->all_coherence_zero; + push_next_indice( hMetaData, all_coherence_zero, 1 ); /* signal coherence */ + bits_no_dirs_coh += 1; + } + /* encode 2 direction subbands position */ + if ( ndirections == 2 && bits_sph_idx == 11 ) + { + bits_no_dirs_coh += write_2dir_info( hMetaData, hQMetaData->twoDirBands, hQMetaData->q_direction[0].cfg.nbands, hQMetaData->numTwoDirBands ); + + for ( i = hQMetaData->numTwoDirBands; i < hQMetaData->q_direction[0].cfg.nbands; i++ ) + { + set_f( hQMetaData->q_direction[1].band_data[i].energy_ratio, 0.0f, hQMetaData->q_direction[1].cfg.nblocks ); + } + + hQMetaData->q_direction[1].cfg.nbands = hQMetaData->numTwoDirBands; + } + /*Quantization and encoding of the Diffuseness */ + + ivas_qmetadata_quantize_diffuseness_nrg_ratios_hr_512( hQMetaData, bits_diff, bits_sph_idx, hMetaData ); + + /* Encode surround coherence */ + if ( all_coherence_zero == 0 ) + { + encode_surround_coherence_hr( hQMetaData, hMetaData ); + } + else + { + + for ( i = 0; i < hQMetaData->q_direction[0].cfg.nbands; i++ ) + { + if ( hQMetaData->surcoh_band_data != NULL ) + { + set_c( (int8_t *) hQMetaData->surcoh_band_data[i].surround_coherence, 0, hQMetaData->q_direction[0].cfg.nblocks ); + } + } + } + + + /* Loop over number of directions*/ + for ( d = 0; d < ndirections; d++ ) + { + q_direction = &( hQMetaData->q_direction[d] ); + + nbands = q_direction->cfg.nbands; + nblocks = q_direction->cfg.nblocks; + start_band = q_direction->cfg.start_band; + + +#ifdef DEBUG_MODE_QMETADATA + { + int16_t k; + k = 0; + fprintf( pF_spcoh_orig, "%d %d ", frame, k ); + + for ( i = start_band; i < nbands; i++ ) + { + for ( j = 0; j < nblocks; j++ ) + { + if ( q_direction->coherence_band_data != NULL ) + { + fprintf( pF_spcoh_orig, " %d ", q_direction->coherence_band_data[i].spread_coherence[j] ); + } + } + } + fprintf( pF_spcoh_orig, "\n" ); + } +#endif + + /*Coherence */ + + if ( all_coherence_zero == 0 ) + { + ivas_qmetadata_quantize_coherence_hr_512( hQMetaData, d, all_coherence_zero, hMetaData, bits_sp_coh ); + } + + /* write the spherical indexes */ + bits_ec = hMetaData->nb_bits_tot; + if ( bits_sph_idx == 11 ) + { + /* do the quantization */ + quantize_direction_frame( q_direction, azimuth_orig, elevation_orig, 1 ); + } + for ( i = start_band; i < nbands; i++ ) + { + for ( j = 0; j < nblocks; j++ ) + { + push_next_indice( hMetaData, q_direction->band_data[i].spherical_index[j], bits_sph_idx ); + } + } + bits_ec = hMetaData->nb_bits_tot - bits_ec; + +#ifdef DEBUG_MODE_QMETADATA + { + float tmp_f; + fprintf( pF, "frame %d: diff %d ", frame, bits_diff[d] ); + fprintf( pF_azi, "frame %d/dir/ec %d: ", frame, d ); + fprintf( pF_ele, "frame %d/dir/ec %d: ", frame, d ); + fprintf( pF_ratio, "frame %d/dir %d: ", frame, d ); + /*fprintf( pF_spcoh, "frame %d/dir %d: ", frame, d ); */ + fprintf( pF_spcoh, " %d %d ", frame, d ); + + if ( d == 0 ) + { + fprintf( pF_surcoh, "frame %d/dir %d: ", frame, d ); + } + + /* direction_distance( elevation_orig, azimuth_orig, q_direction, nbands, nblocks, mat_dist );*/ + for ( i = start_band; i < nbands; i++ ) + { + for ( j = 0; j < nblocks; j++ ) + { + fprintf( pF_azi, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[i].azimuth[j] ) / 100.f ); + fprintf( pF_ele, " %+5.2f ", (int16_t) ( 100.f * q_direction->band_data[i].elevation[j] ) / 100.f ); + fprintf( pF_ratio, " %1.3f ", q_direction->band_data[i].energy_ratio[j] ); + if ( q_direction->coherence_band_data != NULL ) + { + fprintf( pF_spcoh, " %d ", q_direction->coherence_band_data[i].spread_coherence[j] ); + } + if ( d == 0 && hQMetaData->surcoh_band_data != NULL ) + { + fprintf( pF_surcoh, " %d ", hQMetaData->surcoh_band_data[i].surround_coherence[j] ); + } + } + } + fprintf( pF, "\n" ); + fprintf( pF_azi, "\n" ); + fprintf( pF_ele, "\n" ); + fprintf( pF_ratio, "\n" ); + fprintf( pF_spcoh, "\n" ); + if ( d == 0 ) + { + fprintf( pF_surcoh, "\n" ); + } + + for ( i = 0; i < nblocks; i++ ) + { + for ( j = 0; j < nbands; j++ ) + { + dbgwrite( &( q_direction->band_data[j].azimuth[i] ), sizeof( float ), 1, 1, "./res/IVAS_QMETADATA_azi.bin" ); + dbgwrite( &( q_direction->band_data[j].elevation[i] ), sizeof( float ), 1, 1, "./res/IVAS_QMETADATA_ele.bin" ); + dbgwrite( &( q_direction->band_data[j].energy_ratio_index[i] ), sizeof( uint16_t ), 1, 1, "./res/IVAS_QMETADATA_diffuseness_index.bin" ); + tmp_f = 1.f - q_direction->band_data[j].energy_ratio[i]; + dbgwrite( &tmp_f, sizeof( float ), 1, 1, "./res/IVAS_QMETADATA_diffuseness.bin" ); + } + } + + j = (int16_t) ( hMetaData->nb_bits_tot ); + dbgwrite( &j, sizeof( int16_t ), 1, 1, "./res/IVAS_QMETADATA_bits.bin" ); + } +#endif + + /* Save quantized DOAs */ + if ( bits_sph_idx == 11 ) + { + for ( i = start_band; i < nbands; i++ ) + { + mvr2r( azimuth_orig[i], q_direction->band_data[i].azimuth, nblocks ); + mvr2r( elevation_orig[i], q_direction->band_data[i].elevation, nblocks ); + } + } + else + { + for ( i = start_band; i < nbands; i++ ) + { + mvr2r( q_direction->band_data[i].azimuth, q_direction->band_data[i].q_azimuth, nblocks ); + mvr2r( q_direction->band_data[i].elevation, q_direction->band_data[i].q_elevation, nblocks ); + } + } + } + + return error; +} +#endif + /*-----------------------------------------------------------------------* * ivas_qmetadata_enc_sid_encode() @@ -1036,6 +1344,303 @@ int16_t quantize_direction2D( return idx_sph; } +#ifdef HR_METADATA +/*------------------------------------------------------------------------- + * ivas_qmetadata_quantize_diffuseness_nrg_ratios() + * + * Quantize diffuseness + *------------------------------------------------------------------------*/ + +static void ivas_qmetadata_quantize_diffuseness_nrg_ratios_hr( + IVAS_QMETADATA_HANDLE hQMetaData, + int16_t *needed_bits, + int16_t *nbits_diff, + int16_t *dfRatioBits, + int16_t bits_dir_hr ) +{ + int16_t j, k, dir2band; + int16_t index_dirRatio1Inv, index_dirRatio2Inv, index_dirRatio1Inv_mod, index_dirRatio2Inv_mod; + int16_t index_diff; + + nbits_diff[0] = 0; + nbits_diff[1] = 0; + needed_bits[0] = 0; + needed_bits[1] = 0; + dir2band = 0; + + for ( j = hQMetaData->q_direction[0].cfg.start_band; j < hQMetaData->q_direction[0].cfg.nbands; ++j ) + { + if ( hQMetaData->no_directions == 2 && hQMetaData->twoDirBands[j] == 1 ) + { + float dirRatio1, dirRatio2, diffRatio, sumRatio, dfRatio, dfRatioQ, diffRatioQ, dirRatio1Q, dirRatio2Q; + int16_t dfRatio_index, dfRatio_qsteps, dfRatio_bits; + + /* With 2dir metadata, we quantize and transmit diffuse-to-total ratio (diffRatio) and + * distribution factor of direct-to-total ratios (dFRatio). This is more efficient and + * accurate than simple separate quantization of each direct-to-total ratio or their + * separate inverses. */ + dirRatio1 = hQMetaData->q_direction[0].band_data[j].energy_ratio[0]; + dirRatio2 = hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[0]; + sumRatio = dirRatio1 + dirRatio2; + diffRatio = 1.0f - sumRatio; + dfRatio = sumRatio < EPSILON ? 0.5f : dirRatio1 / sumRatio; + + index_diff = masa_sq( diffRatio, diffuseness_thresholds_hr, HR_MASA_ER_LEVELS ); + diffRatioQ = diffuseness_reconstructions_hr[index_diff]; + + dfRatio_bits = ivas_get_df_ratio_bits( index_diff >> 1 ); + + dfRatioBits[dir2band] = dfRatio_bits; + + dfRatio_qsteps = ( 1 << dfRatio_bits ); + dfRatio_index = usquant( dfRatio, &dfRatioQ, 0.5f, 0.5f / ( dfRatio_qsteps - 1 ), dfRatio_qsteps ); + + /* Direction quantization requires also separately quantized direct-to-total ratios. Thus, we calculate them. */ + dirRatio1Q = dfRatioQ * ( 1.0f - diffRatioQ ); + dirRatio2Q = ( 1.0f - diffRatioQ ) - dirRatio1Q; + + index_dirRatio1Inv = masa_sq( 1.0f - dirRatio1Q, diffuseness_thresholds_hr, HR_MASA_ER_LEVELS ); + + /* Note: To save memory, we store temporarily index_diff and dfRatio_index into first and second direction + * energy ratio index variables until they have been encoded. index_dirRatio1Inv and index_dirRatio2Inv are + * then later retrieved for further use in encoding. */ + for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ ) + { + hQMetaData->q_direction[0].band_data[j].energy_ratio_index[k] = index_diff; + hQMetaData->q_direction[0].band_data[j].energy_ratio[k] = dirRatio1Q; + } + nbits_diff[0] += MASA_BITS_ER_HR; + + index_dirRatio2Inv = masa_sq( 1.0f - dirRatio2Q, diffuseness_thresholds_hr, HR_MASA_ER_LEVELS ); + for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ ) + { + hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index[k] = dfRatio_index; + hQMetaData->q_direction[1].band_data[dir2band].energy_ratio[k] = dirRatio2Q; + } + nbits_diff[1] += dfRatio_bits; + + /* Obtain compensated direct-to-total ratios for direction quantization. This compensates for the + * fact that with 2dir data, it is harder to achieve separate high direct-to-total ratio values + * which are assumed by the direction quantization system. In practice, this improves direction + * accuracy when it is perceptual meaningful. */ + masa_compensate_two_dir_energy_ratio_index( index_dirRatio1Inv, index_dirRatio2Inv, &index_dirRatio1Inv_mod, &index_dirRatio2Inv_mod ); + + for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ ) + { + hQMetaData->q_direction[0].band_data[j].energy_ratio_index_mod[k] = index_dirRatio1Inv_mod; + + if ( bits_dir_hr > 0 ) + { + hQMetaData->q_direction[0].band_data[j].bits_sph_idx[k] = bits_dir_hr; + } + else + { + + hQMetaData->q_direction[0].band_data[j].bits_sph_idx[k] = bits_direction_masa[index_dirRatio1Inv_mod]; + } + } + needed_bits[0] += hQMetaData->q_direction[0].cfg.nblocks * bits_direction_masa[index_dirRatio1Inv_mod]; + + for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ ) + { + hQMetaData->q_direction[1].band_data[dir2band].energy_ratio_index_mod[k] = index_dirRatio2Inv_mod; + + if ( bits_dir_hr > 0 ) + { + hQMetaData->q_direction[1].band_data[dir2band].bits_sph_idx[k] = bits_dir_hr; + } + else + { + + hQMetaData->q_direction[1].band_data[dir2band].bits_sph_idx[k] = bits_direction_masa[index_dirRatio2Inv_mod]; + } + } + needed_bits[1] += hQMetaData->q_direction[1].cfg.nblocks * bits_direction_masa[index_dirRatio2Inv_mod]; + + dir2band++; + } + else + { + index_dirRatio1Inv = masa_sq( 1.0f - hQMetaData->q_direction[0].band_data[j].energy_ratio[0], diffuseness_thresholds_hr, HR_MASA_ER_LEVELS ); + + for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ ) + { + hQMetaData->q_direction[0].band_data[j].energy_ratio_index[k] = index_dirRatio1Inv; + hQMetaData->q_direction[0].band_data[j].energy_ratio_index_mod[k] = index_dirRatio1Inv; + hQMetaData->q_direction[0].band_data[j].energy_ratio[k] = 1.0f - diffuseness_reconstructions_hr[index_dirRatio1Inv]; + + if ( bits_dir_hr > 0 ) + { + hQMetaData->q_direction[0].band_data[j].bits_sph_idx[k] = bits_dir_hr; + } + else + { + + hQMetaData->q_direction[0].band_data[j].bits_sph_idx[k] = bits_direction_masa[index_dirRatio1Inv]; + } + } + + nbits_diff[0] += MASA_BITS_ER_HR; + + needed_bits[0] += hQMetaData->q_direction[0].cfg.nblocks * bits_dir_hr; + } + } + + return; +} + + +static void ivas_qmetadata_quantize_diffuseness_nrg_ratios_hr_512( + IVAS_QMETADATA_HANDLE hQMetaData, + int16_t *needed_bits, + int16_t bits_dir_hr, + BSTR_ENC_HANDLE hMetaData ) +{ + int16_t j, k; + int16_t index; + + + needed_bits[0] = 0; + needed_bits[1] = 0; + + + for ( j = hQMetaData->q_direction[0].cfg.start_band; j < hQMetaData->q_direction[0].cfg.nbands; ++j ) + { + for ( k = 0; k < hQMetaData->q_direction[0].cfg.nblocks; k++ ) + { + index = masa_sq( 1.0f - hQMetaData->q_direction[0].band_data[j].energy_ratio[k], diffuseness_thresholds_hr, HR_MASA_ER_LEVELS ); + push_next_indice( hMetaData, index, MASA_BITS_ER_HR ); + hQMetaData->q_direction[0].band_data[j].energy_ratio_index[k] = index; + hQMetaData->q_direction[0].band_data[j].energy_ratio_index_mod[k] = index; + hQMetaData->q_direction[0].band_data[j].energy_ratio[k] = 1.0f - diffuseness_reconstructions_hr[index]; + needed_bits[0] += MASA_BITS_ER_HR; + hQMetaData->q_direction[0].band_data[j].bits_sph_idx[k] = bits_dir_hr; + } + } + if ( hQMetaData->no_directions == 2 ) + { + for ( j = hQMetaData->q_direction[1].cfg.start_band; j < hQMetaData->q_direction[1].cfg.nbands; ++j ) + { + for ( k = 0; k < hQMetaData->q_direction[1].cfg.nblocks; k++ ) + { + index = masa_sq( 1.0f - hQMetaData->q_direction[1].band_data[j].energy_ratio[k], diffuseness_thresholds_hr, HR_MASA_ER_LEVELS ); + push_next_indice( hMetaData, index, MASA_BITS_ER_HR ); + hQMetaData->q_direction[1].band_data[j].energy_ratio_index[k] = index; + hQMetaData->q_direction[1].band_data[j].energy_ratio[k] = 1.0f - diffuseness_reconstructions_hr[index]; + if ( hQMetaData->q_direction[1].band_data[j].energy_ratio[k] > 1.0f - hQMetaData->q_direction[0].band_data[j].energy_ratio[k] ) + { + hQMetaData->q_direction[1].band_data[j].energy_ratio[k] = 1.0f - hQMetaData->q_direction[0].band_data[j].energy_ratio[k]; + } + needed_bits[1] += MASA_BITS_ER_HR; + hQMetaData->q_direction[1].band_data[j].bits_sph_idx[k] = bits_dir_hr; + } + } + } + + return; +} + + +/*------------------------------------------------------------------------- + * ivas_qmetadata_entropy_encode_diffuseness() + * + * encode diffuseness + *------------------------------------------------------------------------*/ + +static int16_t ivas_qmetadata_entropy_encode_diffuseness_hr( + BSTR_ENC_HANDLE hMetaData, + IVAS_QDIRECTION *q_direction, + uint16_t *diffuseness_index_max_ec_frame ) +{ + int16_t start_bit_pos; + int16_t diffuseness_bits_raw; + int16_t b; + int16_t min_diffuseness_m_index, max_diffuseness_m_index; + int16_t nbands; + int16_t start_band; + + nbands = q_direction->cfg.nbands; + start_band = q_direction->cfg.start_band; + + start_bit_pos = hMetaData->nb_bits_tot; + + if ( nbands == 1 ) + { + /* If there is only one band, diffuseness should be coded directly as raw with no signaling. */ + push_next_indice( hMetaData, q_direction->band_data[0].energy_ratio_index[0], MASA_BITS_ER_HR ); + *diffuseness_index_max_ec_frame = 10; + return ( hMetaData->nb_bits_tot - start_bit_pos ); + } + + /* compute the number of raw coding bits */ + diffuseness_bits_raw = 0; + for ( b = start_band; b < nbands; b++ ) + { + diffuseness_bits_raw += ivas_qmetadata_encode_quasi_uniform_length( q_direction->band_data[b].energy_ratio_index[0], HR_MASA_ER_LEVELS ); + } + + min_diffuseness_m_index = q_direction->band_data[start_band].energy_ratio_index[0]; + max_diffuseness_m_index = q_direction->band_data[start_band].energy_ratio_index[0]; + + for ( b = start_band; b < nbands; b++ ) + { + if ( q_direction->band_data[b].energy_ratio_index[0] < min_diffuseness_m_index ) + { + min_diffuseness_m_index = q_direction->band_data[b].energy_ratio_index[0]; + } + + if ( q_direction->band_data[b].energy_ratio_index[0] > max_diffuseness_m_index ) + { + max_diffuseness_m_index = q_direction->band_data[b].energy_ratio_index[0]; + } + } + + + /* Use similarity coding approach or raw coding when there is a low number of bands. */ + /* one bit is used to indicate whether diffuseness values are entropy coded or coded raw */ + if ( min_diffuseness_m_index == max_diffuseness_m_index ) /* all values are equal */ + { + push_next_indice( hMetaData, 0, 1 ); /* dif_use_raw_coding */ + push_next_indice( hMetaData, 1, 1 ); /* dif_have_unique_value */ + ivas_qmetadata_encode_quasi_uniform( hMetaData, min_diffuseness_m_index, HR_MASA_ER_LEVELS ); /* dif_unique_value */ + } + else if ( min_diffuseness_m_index + 1 == max_diffuseness_m_index ) /* only two consecutive values are present */ + { + push_next_indice( hMetaData, 0, 1 ); /* dif_use_raw_coding */ + push_next_indice( hMetaData, 0, 1 ); /* dif_have_unique_value */ + ivas_qmetadata_encode_quasi_uniform( hMetaData, min_diffuseness_m_index, HR_MASA_ER_LEVELS - 1 ); /* dif_min_value */ + + for ( b = start_band; b < nbands; b++ ) + { + push_next_indice( hMetaData, q_direction->band_data[b].energy_ratio_index[0] - min_diffuseness_m_index, 1 ); /* dif_bit_offset_values */ + } + } + else /* raw coding */ + { + push_next_indice( hMetaData, 1, 1 ); /* dif_use_raw_coding */ + + for ( b = start_band; b < nbands; b++ ) + { + ivas_qmetadata_encode_quasi_uniform( hMetaData, q_direction->band_data[b].energy_ratio_index[0], HR_MASA_ER_LEVELS ); /* dif_values */ + } + } + + + *diffuseness_index_max_ec_frame = 10; + /* adaptively select the diffuseness_index_max_ec threshold */ + if ( min_diffuseness_m_index > 10 ) + { + *diffuseness_index_max_ec_frame = HR_MASA_ER_LEVELS - 1; + } + +#ifdef DEBUGGING + assert( ( hMetaData->nb_bits_tot - start_bit_pos ) <= 1 + diffuseness_bits_raw ); +#endif + + return ( hMetaData->nb_bits_tot - start_bit_pos ); +} + +#endif /*------------------------------------------------------------------------- * ivas_qmetadata_quantize_diffuseness_nrg_ratios() @@ -1145,6 +1750,7 @@ static void ivas_qmetadata_quantize_diffuseness_nrg_ratios( } nbits_diff[0] += MASA_BITS_ER; + needed_bits[0] += hQMetaData->q_direction[0].cfg.nblocks * bits_direction_masa[index_dirRatio1Inv]; } } @@ -1799,7 +2405,12 @@ static int16_t ivas_qmetadata_entropy_encode_dir( const int16_t nbands, const int16_t start_band, const int16_t direction_bits_raw, - int16_t max_bits ) + int16_t max_bits +#ifdef HR_METADATA + , + int16_t is_hr +#endif +) { uint16_t diff_idx_min; int16_t i, j; @@ -1845,7 +2456,18 @@ static int16_t ivas_qmetadata_entropy_encode_dir( set_zero( avg_direction_vector, 3 ); for ( i = start_band; i < nbands; i++ ) { - diff_idx_min = min( q_direction->band_data[i].energy_ratio_index_mod[0], diff_idx_min ); +#ifdef HR_METADATA + if ( is_hr ) + { + diff_idx_min = 0; // min( q_direction->band_data[i].energy_ratio_index_mod[0]>>1, diff_idx_min ); + } + else + { +#endif + diff_idx_min = min( q_direction->band_data[i].energy_ratio_index_mod[0], diff_idx_min ); +#ifdef HR_METADATA + } +#endif if ( q_direction->band_data[i].energy_ratio_index_mod[0] > diffuseness_index_max_ec_frame ) { @@ -1895,6 +2517,7 @@ static int16_t ivas_qmetadata_entropy_encode_dir( no_th = no_theta_masa[bits_direction_masa[diff_idx_min] - 3]; + for ( i = 0; i < no_th; i++ ) { theta_cb[i] = i * delta_theta_masa[bits_direction_masa[diff_idx_min] - 3]; @@ -4054,6 +4677,10 @@ static int16_t encode_spread_coherence_1sf( IVAS_QMETADATA *q_metadata, /* i : quantized metadata */ const int16_t idx_d, /* i : current direction index */ BSTR_ENC_HANDLE hMasaMetaData /* i/o: metadata bitstream handle */ +#ifdef HR_METADATA + , + int16_t is_hr +#endif ) { int16_t i, j, k; @@ -4079,11 +4706,23 @@ static int16_t encode_spread_coherence_1sf( GR_ord = 1; idx_shift = 0; + /* number of codevectors added dependent on number of subbands */ extra_cv = coding_subbands / MASA_FACTOR_CV_COH; for ( j = 0; j < coding_subbands; j++ ) { - idx_ER = 7 - q_direction->band_data[j].energy_ratio_index_mod[0] + extra_cv; +#ifdef HR_METADATA + if ( is_hr ) + { + idx_ER = 7 - ( q_direction->band_data[j].energy_ratio_index_mod[0] >> 1 ) + extra_cv; + } + else + { +#endif + idx_ER = 7 - q_direction->band_data[j].energy_ratio_index_mod[0] + extra_cv; +#ifdef HR_METADATA + } +#endif if ( idx_ER > 0 ) { idx_sp_coh[j] = (uint16_t) roundf( q_direction->coherence_band_data[j].spread_coherence[0] / ( 255.0f / (float) idx_ER ) ); @@ -4433,6 +5072,210 @@ static int16_t encode_surround_coherence( } +#ifdef HR_METADATA +static int16_t encode_surround_coherence_hr( + IVAS_QMETADATA *hQMetaData, /* i : quantized metadata */ + BSTR_ENC_HANDLE hMetaData /* i/o: metadata bitstream handle */ +) +{ + int16_t i, j, k, sf; + int16_t nbits, nbits_fr, nbits_sf; + uint16_t idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS]; + uint16_t mr_idx_sur_coh[MASA_MAXIMUM_CODING_SUBBANDS]; + int16_t GR_ord, bits_GR; + uint64_t idx, idx1; + int16_t no_idx16; + int16_t no_cv[MASA_MAXIMUM_CODING_SUBBANDS]; + float error_ratio_surr; + IVAS_QDIRECTION *q_direction; + int16_t half_coding_subbands, nbits_fr1, coding_subbands; + int16_t all_coherence_zero; + uint16_t idx_sur_coh_shift[MASA_MAXIMUM_CODING_SUBBANDS]; + uint8_t idx_shift; + int16_t max_val = 0, nbits_max; + int16_t no_cv_shift[MASA_MAXIMUM_CODING_SUBBANDS], min_idx; + int16_t idx16; + + coding_subbands = hQMetaData->q_direction[0].cfg.nbands; + all_coherence_zero = hQMetaData->all_coherence_zero; + q_direction = &( hQMetaData->q_direction[0] ); + nbits = 0; + + if ( all_coherence_zero == 1 ) + { + nbits = 0; + } + else + { + for ( sf = 0; sf < hQMetaData->q_direction[0].cfg.nblocks; sf++ ) + { + GR_ord = 1; + k = 0; + idx_shift = 0; + for ( j = 0; j < coding_subbands; j++ ) + { + if ( hQMetaData->no_directions == 2 ) + { + k += hQMetaData->twoDirBands[j]; + idx16 = max( k - 1, 0 ); + error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[sf] - q_direction[1].band_data[idx16].energy_ratio[sf] * hQMetaData->twoDirBands[j]; + } + else + { + error_ratio_surr = 1.0f - q_direction[0].band_data[j].energy_ratio[sf]; + } + + + if ( error_ratio_surr <= 0 ) + { + error_ratio_surr = 0; + idx_sur_coh[j] = 0; + no_cv[j] = 1; + hQMetaData->surcoh_band_data[j].surround_coherence[sf] = 0; /* sur_coherence_cb_masa[idx_cb_sur_coh_masa[DIRAC_DIFFUSE_LEVELS - 1] * MASA_MAX_NO_CV_SUR_COH]; */ + } + else + { + idx_sur_coh[j] = squant_int( hQMetaData->surcoh_band_data[j].surround_coherence[sf], &hQMetaData->surcoh_band_data[j].surround_coherence[sf], + &sur_coherence_cb_masa[idx_cb_sur_coh_masa[7] * MASA_MAX_NO_CV_SUR_COH], idx_cb_sur_coh_masa[7] + 2 ); + no_cv[j] = idx_cb_sur_coh_masa[7] + 2; + no_cv_shift[idx_shift] = no_cv[j]; + idx_sur_coh_shift[idx_shift++] = idx_sur_coh[j]; + } + } + + if ( sum_s( no_cv, coding_subbands ) != coding_subbands ) + { + nbits_max = 0; + if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH ) + { + j = maximum_s( (int16_t *) idx_sur_coh, coding_subbands, &max_val ); + for ( j = 0; j < coding_subbands; j++ ) + { + if ( no_cv[j] > max_val + 1 ) + { + no_cv[j] = max_val + 1; + } + } + nbits_max = MASA_MAX_NO_CV_SUR_COH - max_val; /* encoded with GR0 as max_no_vals - no_vals*/ + } + if ( max_val == 0 ) + { + for ( j = 0; j < coding_subbands; j++ ) + { + hQMetaData->surcoh_band_data[j].surround_coherence[sf] = 0; + } + } + nbits_sf = coherence_coding_length( idx_sur_coh_shift, idx_shift, coding_subbands, no_cv, + mr_idx_sur_coh, no_cv_shift, &min_idx, &GR_ord, &nbits_fr, &nbits_fr1 ); + half_coding_subbands = coding_subbands / 2; + idx1 = 0; + + /* should check how to encode the average - check distribution */ + if ( nbits_fr + nbits_fr1 + nbits_max < nbits_sf ) + { + /* write flag*/ + push_next_indice( hMetaData, 0, 1 ); + + /* create combined index */ + nbits += nbits_fr + nbits_fr1 + 1; + if ( coding_subbands > MASA_LIMIT_NO_BANDS_SUR_COH ) + { + /* write max value*/ + bits_GR = hMetaData->nb_bits_tot; + ivas_qmetadata_encode_extended_gr( hMetaData, MASA_MAX_NO_CV_SUR_COH - max_val - 1, MASA_MAX_NO_CV_SUR_COH, 0 ); + nbits += hMetaData->nb_bits_tot - bits_GR; + } + + if ( nbits_fr1 > 0 ) + { + idx = create_combined_index( idx_sur_coh, half_coding_subbands, no_cv ); + idx1 = create_combined_index( &idx_sur_coh[half_coding_subbands], half_coding_subbands, &no_cv[half_coding_subbands] ); + } + else + { + idx = create_combined_index( idx_sur_coh, coding_subbands, no_cv ); + } + + if ( nbits_fr % 16 == 0 ) + { + no_idx16 = nbits_fr / 16; + } + else + { + no_idx16 = (int16_t) round_f( ( nbits_fr / 16.0f + 0.5f ) ); + } + + /* write combined index */ + k = nbits_fr; + for ( i = 0; i < no_idx16 - 1; i++ ) + { + k -= 16; + push_next_indice( hMetaData, ( ( idx >> k ) & 65535 ), 16 ); /* 16 bits */ + } + + push_next_indice( hMetaData, ( idx & ( ( 1 << k ) - 1 ) ), k ); + + if ( nbits_fr1 > 0 ) + { + if ( nbits_fr1 % 16 == 0 ) + { + no_idx16 = nbits_fr1 / 16; + } + else + { + no_idx16 = (int16_t) round_f( ( nbits_fr1 / 16.0f + 0.5f ) ); + } + + assert( no_idx16 <= 4 ); + + k = nbits_fr1; + for ( i = 0; i < no_idx16 - 1; i++ ) + { + k -= 16; + push_next_indice( hMetaData, ( ( idx1 >> k ) & 65535 ), 16 ); /* 16 bits */ + } + + push_next_indice( hMetaData, ( idx1 & ( ( 1 << k ) - 1 ) ), k ); + } + } + else + { + /* write flag */ + nbits += 1; + + /* write flag*/ + push_next_indice( hMetaData, 1, 1 ); + + /* write GR_ord */ + push_next_indice( hMetaData, GR_ord, 1 ); + nbits += 1; + + /* write the min */ + bits_GR = hMetaData->nb_bits_tot; + ivas_qmetadata_encode_extended_gr( hMetaData, min_idx, MASA_MAX_NO_CV_SUR_COH, 0 ); + nbits += hMetaData->nb_bits_tot - bits_GR; + + /* write GR data */ + for ( j = 0; j < idx_shift; j++ ) + { + bits_GR = hMetaData->nb_bits_tot; + + ivas_qmetadata_encode_extended_gr( hMetaData, mr_idx_sur_coh[j], no_cv_shift[j], GR_ord ); + + nbits += hMetaData->nb_bits_tot - bits_GR; + } + } + } + } + } + + + return nbits; +} + +#endif + + /*-------------------------------------------------------------------* * quantize_DCT_0_coh() * @@ -4449,14 +5292,42 @@ static float quantize_DCT_0_coh( IVAS_QDIRECTION *q_direction, /* i : quantized metadata */ uint16_t *idx_x, /* o : codewords index */ int16_t *p_no_cb /* o : actual number of codewords dependent on energy ratio value */ +#ifdef HR_METADATA + , + int16_t is_hr +#endif ) { float var_azi, xhat; int16_t idx_sub_cb, idx; - +#ifdef HR_METADATA + int16_t min_index; +#endif /* quantize first DCT component */ var_azi = var( q_direction->band_data[j].azimuth, q_direction->cfg.nblocks ); +#ifdef HR_METADATA + if ( is_hr ) + { + minimum_s( (int16_t *) ( q_direction->band_data[j].energy_ratio_index ), q_direction->cfg.nblocks, &min_index ); + min_index = min_index >> 1; + } + else + { + min_index = q_direction->band_data[j].energy_ratio_index[0]; + } + if ( var_azi < delta_var ) + { + idx_sub_cb = no_cb * min_index; + } + else + { + idx_sub_cb = no_cb * ( min_index + DIRAC_DIFFUSE_LEVELS ); + } + idx = squant( x, &xhat, &coherence_cb[idx_sub_cb], len_cb_dct0_masa[min_index] ); + + *p_no_cb = len_cb_dct0_masa[min_index]; +#else if ( var_azi < delta_var ) { idx_sub_cb = no_cb * q_direction->band_data[j].energy_ratio_index[0]; @@ -4469,7 +5340,7 @@ static float quantize_DCT_0_coh( idx = squant( x, &xhat, &coherence_cb[idx_sub_cb], len_cb_dct0_masa[q_direction->band_data[j].energy_ratio_index[0]] ); *p_no_cb = len_cb_dct0_masa[q_direction->band_data[j].energy_ratio_index[0]]; - +#endif *idx_x = idx; return xhat; @@ -4537,6 +5408,107 @@ static void dct4_transform( return; } +#ifdef HR_METADATA +static int16_t ivas_qmetadata_quantize_coherence_hr_512( + IVAS_QMETADATA *hQMetaData, /* i/o: quantized metadata */ + const int16_t idx_d, /* i : current direction index */ + const int16_t all_coherence_zero, /* i : all coherence is zero - flag */ + BSTR_ENC_HANDLE hMetaData, /* i : metadata handle */ + int16_t bits_coh ) +{ + int16_t j, k; + int16_t nbands, nblocks; + int16_t nbits; + int16_t nbits1, nbits0, nbits_av; + uint16_t idx_coh[MASA_MAXIMUM_CODING_SUBBANDS]; + IVAS_QDIRECTION *q_direction; + int16_t cbsize; + float delta, tmp; + int16_t min_idx, GR_param, GR_param_av; + uint16_t av, mr_idx[MASA_MAXIMUM_CODING_SUBBANDS]; + + q_direction = &( hQMetaData->q_direction[idx_d] ); + nbands = q_direction->cfg.nbands; + nblocks = q_direction->cfg.nblocks; + nbits = 0; + + if ( all_coherence_zero == 1 ) + { + return nbits; + } + nbits = hMetaData->nb_bits_tot; + + cbsize = 1 << bits_coh; + delta = 256.0f / cbsize; + + for ( k = 0; k < nblocks; k++ ) + { + min_idx = 0; + for ( j = 0; j < nbands; j++ ) + { + idx_coh[j] = usquant( (float) ( q_direction->coherence_band_data[j].spread_coherence[k] ), &tmp, delta / 2.0f, delta, cbsize ); + q_direction->coherence_band_data[j].spread_coherence[k] = (uint8_t) ( idx_coh[j] * delta + delta / 2.0f ); + if ( idx_coh[j] < min_idx ) + { + min_idx = idx_coh[j]; + } + } + + + nbits0 = 0; + nbits1 = 0; + for ( j = 0; j < nbands; j++ ) + { + idx_coh[j] = idx_coh[j] - min_idx; + nbits0 += ivas_qmetadata_encode_extended_gr_length( idx_coh[j], cbsize - min_idx, 0 ); + nbits1 += ivas_qmetadata_encode_extended_gr_length( idx_coh[j], cbsize - min_idx, 1 ); + } + if ( nbits0 < nbits1 ) + { + GR_param = 0; + nbits1 = nbits0; + } + else + { + GR_param = 1; + } + GR_param_av = 1; + nbits_av = mean_removed_GR_new( idx_coh, cbsize, nbands, 1, &GR_param_av, &av, mr_idx ); + + if ( nbits_av < nbits1 ) + { + nbits1 = nbits_av; + GR_param = GR_param_av; + /* use average removed */ + push_next_indice( hMetaData, 1, 1 ); + /* write average */ + push_next_indice( hMetaData, av, bits_coh ); + /* write GR param */ + push_next_indice( hMetaData, GR_param, 1 ); + for ( j = 0; j < nbands; j++ ) + { + ivas_qmetadata_encode_extended_gr( hMetaData, mr_idx[j], 2 * cbsize, GR_param ); + } + } + else + { + /* use min removed */ + push_next_indice( hMetaData, 0, 1 ); + /* write min index */ + push_next_indice( hMetaData, min_idx, bits_coh ); + /* write GR param */ + push_next_indice( hMetaData, GR_param, 1 ); + for ( j = 0; j < nbands; j++ ) + { + ivas_qmetadata_encode_extended_gr( hMetaData, idx_coh[j], cbsize - min_idx, GR_param ); + } + } + } + + nbits = hMetaData->nb_bits_tot - nbits; + return nbits; +} +#endif /*-------------------------------------------------------------------* * ivas_qmetadata_quantize_coherence() @@ -4551,7 +5523,12 @@ static int16_t ivas_qmetadata_quantize_coherence( const int16_t all_coherence_zero, /* i : all coherence is zero - flag */ BSTR_ENC_HANDLE hMetaData, /* i : metadata handle */ const int16_t write_flag, /* i : flag to actually write the data or not */ - int16_t *indice_coherence ) + int16_t *indice_coherence +#ifdef HR_METADATA + , + int16_t is_hr +#endif +) { int16_t j, k; float dct_coh[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES]; @@ -4565,7 +5542,10 @@ static int16_t ivas_qmetadata_quantize_coherence( int16_t two_dir_band[MASA_MAXIMUM_CODING_SUBBANDS]; int16_t no_cb_vec[MASA_MAXIMUM_CODING_SUBBANDS]; IVAS_QDIRECTION *q_direction; - +#ifdef HR_METADATA + int16_t min_index; + min_index = 0; +#endif q_direction = &( hQMetaData->q_direction[idx_d] ); coding_subbands = q_direction->cfg.nbands; nbits = 0; @@ -4577,7 +5557,12 @@ static int16_t ivas_qmetadata_quantize_coherence( if ( hQMetaData->q_direction[idx_d].cfg.nblocks == 1 ) { - nbits = encode_spread_coherence_1sf( hQMetaData, idx_d, hMetaData ); + nbits = encode_spread_coherence_1sf( hQMetaData, idx_d, hMetaData +#ifdef HR_METADATA + , + is_hr +#endif + ); return nbits; } @@ -4632,13 +5617,31 @@ static int16_t ivas_qmetadata_quantize_coherence( { /* DCT transform */ dct4_transform( hQMetaData->q_direction[idx_d].coherence_band_data[j].spread_coherence, dct_coh[j] ); - no_cb_vec[j] = len_cb_dct0_masa[q_direction->band_data[j].energy_ratio_index[0]]; +#ifdef HR_METADATA + if ( is_hr ) + { + minimum_s( (int16_t *) ( q_direction->band_data[j].energy_ratio_index ), q_direction->cfg.nblocks, &min_index ); + no_cb_vec[j] = len_cb_dct0_masa[min_index >> 1]; + } + else + { +#endif + no_cb_vec[j] = len_cb_dct0_masa[q_direction->band_data[j].energy_ratio_index[0]]; +#ifdef HR_METADATA + } +#endif if ( write_flag ) { /* quantize first DCT parameter */ - dct_coh[j][0] = quantize_DCT_0_coh( dct_coh[j][0], j, coherence_cb0_masa, MASA_DELTA_AZI_DCT0, MASA_NO_CV_COH, q_direction, &idx_dct[k], &no_cb_vec[j] ); + dct_coh[j][0] = quantize_DCT_0_coh( dct_coh[j][0], j, coherence_cb0_masa, MASA_DELTA_AZI_DCT0, MASA_NO_CV_COH, q_direction, &idx_dct[k], &no_cb_vec[j] +#ifdef HR_METADATA + , + is_hr +#endif + ); } + if ( coding_subbands < coding_subbands_0 ) { idx_dct[k + coding_subbands] = squant( dct_coh[j][1], &dct_coh[j][1], &coherence_cb1_masa[MASA_grouping[two_dir_band[j]] * MASA_NO_CV_COH1], MASA_NO_CV_COH1 ); @@ -4790,6 +5793,61 @@ static void ivas_qmetadata_reorder_2dir_bands( return; } +#ifdef HR_METADATA +static void ivas_qmetadata_reorder_2dir_bands_hr( + IVAS_QMETADATA_HANDLE hQMetaData ) +{ + int16_t nbands; + int16_t nsubframes; + int16_t band, sf; + + nbands = hQMetaData->q_direction[0].cfg.nbands; + nsubframes = hQMetaData->q_direction[0].cfg.nblocks; + + for ( band = 0; band < nbands; band++ ) + { + if ( hQMetaData->twoDirBands[band] == 1 ) + { + + for ( sf = 0; sf < nsubframes; sf++ ) + { + if ( hQMetaData->q_direction[0].band_data[band].energy_ratio[sf] < hQMetaData->q_direction[1].band_data[band].energy_ratio[sf] ) + { + uint16_t uint16_tmp = 0; + float flt_tmp = 0; + uint8_t uint8_tmp = 0; + uint16_tmp = hQMetaData->q_direction[0].band_data[band].spherical_index[sf]; + hQMetaData->q_direction[0].band_data[band].spherical_index[sf] = hQMetaData->q_direction[1].band_data[band].spherical_index[sf]; + hQMetaData->q_direction[1].band_data[band].spherical_index[sf] = uint16_tmp; + + flt_tmp = hQMetaData->q_direction[0].band_data[band].azimuth[sf]; + hQMetaData->q_direction[0].band_data[band].azimuth[sf] = hQMetaData->q_direction[1].band_data[band].azimuth[sf]; + hQMetaData->q_direction[1].band_data[band].azimuth[sf] = flt_tmp; + + flt_tmp = hQMetaData->q_direction[0].band_data[band].elevation[sf]; + hQMetaData->q_direction[0].band_data[band].elevation[sf] = hQMetaData->q_direction[1].band_data[band].elevation[sf]; + hQMetaData->q_direction[1].band_data[band].elevation[sf] = flt_tmp; + + uint8_tmp = hQMetaData->q_direction[0].band_data[band].distance[sf]; + hQMetaData->q_direction[0].band_data[band].distance[sf] = hQMetaData->q_direction[1].band_data[band].distance[sf]; + hQMetaData->q_direction[1].band_data[band].distance[sf] = uint8_tmp; + + uint8_tmp = hQMetaData->q_direction[0].coherence_band_data[band].spread_coherence[sf]; + hQMetaData->q_direction[0].coherence_band_data[band].spread_coherence[sf] = hQMetaData->q_direction[1].coherence_band_data[band].spread_coherence[sf]; + hQMetaData->q_direction[1].coherence_band_data[band].spread_coherence[sf] = uint8_tmp; + + + flt_tmp = hQMetaData->q_direction[0].band_data[band].energy_ratio[sf]; + hQMetaData->q_direction[0].band_data[band].energy_ratio[sf] = hQMetaData->q_direction[1].band_data[band].energy_ratio[sf]; + hQMetaData->q_direction[1].band_data[band].energy_ratio[sf] = flt_tmp; + } + } + } + } + + return; +} +#endif /*-------------------------------------------------------------------* * write_2dir_info() @@ -4808,6 +5866,7 @@ static int16_t write_2dir_info( uint16_t dif_p[MASA_MAXIMUM_CODING_SUBBANDS]; int16_t i, j; j = 0; + p[0] = 0; for ( i = 0; i < n; i++ ) { if ( twoDirBands[i] == 1 ) diff --git a/lib_enc/ivas_qspherical_enc.c b/lib_enc/ivas_qspherical_enc.c index 80a9d0064f2dcfb23dbeb91ed3719dd74e0a526c..3d2df98217deca34987173f49e7226e4ebe21427 100644 --- a/lib_enc/ivas_qspherical_enc.c +++ b/lib_enc/ivas_qspherical_enc.c @@ -59,7 +59,12 @@ static float direction_distance_cp( float theta, float theta_hat, float theta_ha void quantize_direction_frame( IVAS_QDIRECTION *q_direction, /* i/o: quantized direction structure */ float azimuth_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES], - float elevation_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES] ) + float elevation_orig[MASA_MAXIMUM_CODING_SUBBANDS][MAX_PARAM_SPATIAL_SUBFRAMES] +#ifdef HR_METADATA + , + int16_t is_hr +#endif +) { int16_t i, j; int16_t idx; @@ -92,17 +97,36 @@ void quantize_direction_frame( q_direction->not_in_2D += q_direction->band_data[i].elevation_index[j]; - if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) +#ifdef HR_METADATA + if ( is_hr ) { - q_direction->band_data[i].elevation_m_alphabet[j] = no_theta_masa[bits_direction_masa[idx] - 3]; - q_direction->band_data[i].azimuth_m_alphabet[j] = no_phi_masa[bits_direction_masa[idx] - 1][q_direction->band_data[i].elevation_index[j]]; + if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) + { + q_direction->band_data[i].elevation_m_alphabet[j] = no_theta_masa[bits_direction_masa[0] - 3]; + q_direction->band_data[i].azimuth_m_alphabet[j] = no_phi_masa[bits_direction_masa[0] - 1][q_direction->band_data[i].elevation_index[j]]; + } + else + { + q_direction->band_data[i].elevation_m_alphabet[j] = no_theta_masa[bits_direction_masa[0] - 3] * 2 - 1; + q_direction->band_data[i].azimuth_m_alphabet[j] = no_phi_masa[bits_direction_masa[0] - 1][( q_direction->band_data[i].elevation_index[j] + 1 ) >> 1]; + } } else { - q_direction->band_data[i].elevation_m_alphabet[j] = no_theta_masa[bits_direction_masa[idx] - 3] * 2 - 1; - q_direction->band_data[i].azimuth_m_alphabet[j] = no_phi_masa[bits_direction_masa[idx] - 1][( q_direction->band_data[i].elevation_index[j] + 1 ) >> 1]; +#endif + if ( q_direction->cfg.mc_ls_setup != MC_LS_SETUP_INVALID ) + { + q_direction->band_data[i].elevation_m_alphabet[j] = no_theta_masa[bits_direction_masa[idx] - 3]; + q_direction->band_data[i].azimuth_m_alphabet[j] = no_phi_masa[bits_direction_masa[idx] - 1][q_direction->band_data[i].elevation_index[j]]; + } + else + { + q_direction->band_data[i].elevation_m_alphabet[j] = no_theta_masa[bits_direction_masa[idx] - 3] * 2 - 1; + q_direction->band_data[i].azimuth_m_alphabet[j] = no_phi_masa[bits_direction_masa[idx] - 1][( q_direction->band_data[i].elevation_index[j] + 1 ) >> 1]; + } +#ifdef HR_METADATA } - +#endif if ( q_direction->band_data[i].azimuth_index[j] == MASA_NO_INDEX ) { q_direction->band_data[i].azimuth_index[j] = 0; diff --git a/lib_util/masa_file_reader.c b/lib_util/masa_file_reader.c index 19f12182f3ab11246cbe74ff5ddf2eec45a0ca89..95da7818e703a19eb0c03ff85aba45d69c8d4b06 100644 --- a/lib_util/masa_file_reader.c +++ b/lib_util/masa_file_reader.c @@ -100,13 +100,16 @@ IVAS_MASA_METADATA_HANDLE MasaFileReader_getMetadataHandle( * * deindex the MASA metadata from the input metadata file *------------------------------------------------------------------------*/ - -static void deindex_sph_idx( - const uint16_t sphIndex, /* i : Spherical index */ - const SPHERICAL_GRID_DATA *gridData, /* i : Prepared spherical grid */ - float *theta, /* o : Elevation */ - float *phi /* o : Azimuth */ -) +#ifndef HR_METADATA +static +#endif + void + deindex_sph_idx( + const uint16_t sphIndex, /* i : Spherical index */ + const SPHERICAL_GRID_DATA *gridData, /* i : Prepared spherical grid */ + float *theta, /* o : Elevation */ + float *phi /* o : Azimuth */ + ) { float ba_crt, del_crt, div_crt, a4_crt; float estim; @@ -342,6 +345,9 @@ ivas_error MasaFileReader_readNextFrame( for ( b = 0; b < MASA_FREQUENCY_BANDS; b++ ) { deindex_sph_idx( readIndex[b], &self->sph_grid16, &( hMeta->directional_meta[i].elevation[j][b] ), &( hMeta->directional_meta[i].azimuth[j][b] ) ); +#ifdef HR_METADATA + hMeta->directional_meta[i].spherical_index[j][b] = readIndex[b]; +#endif } /* Direct-to-total ratio */