diff --git a/lib_com/ivas_cnst.h b/lib_com/ivas_cnst.h index bc5907a34ad007da5f208e43567e9583d38f1573..b61f041c17a43d25c93ce91877bf7973840eb3d6 100755 --- a/lib_com/ivas_cnst.h +++ b/lib_com/ivas_cnst.h @@ -1021,6 +1021,10 @@ typedef enum /* AGC constants */ #define AGC_BITS_PER_CH 3 #define AGC_EMAX 0 +#ifdef ARITH_HUFF_CODER_CHANGES +#define AGC_SIGNALLING_BITS 1 +#define IVAS_SPAR_ARITH_OVERSHOOT_BITS (16) +#endif /* Common SPAR metadata constants */ #define IVAS_ACTIVEW_DM_F_SCALE 0.5f @@ -1749,11 +1753,17 @@ typedef enum #define IVAS_16K_12BANDS_ACTIVE_BANDS 10 #define SPAR_DIRAC_SPLIT_START_BAND 8 +#define DIRAC_TO_SPAR_HBR_PRED_CHS (FOA_CHANNELS - 1) #define SPAR_DTX_BANDS 2 #define DIRAC_DTX_BANDS 2 #define SPAR_DIRAC_DTX_BANDS ( SPAR_DTX_BANDS + DIRAC_DTX_BANDS ) #define CLDFB_PAR_WEIGHT_START_BAND 7 +#ifdef ARITH_HUFF_CODER_CHANGES +#define QUANT_STRAT_0 0 +#define QUANT_STRAT_2 2 +#endif + /*----------------------------------------------------------------------------------* * Limiter constants diff --git a/lib_com/ivas_dirac_com.c b/lib_com/ivas_dirac_com.c index a5964edd6642dd7af34e6c2ee63154f2a43bcf48..7d11a0427d3557d62c805c4ff3636241b40859d0 100644 --- a/lib_com/ivas_dirac_com.c +++ b/lib_com/ivas_dirac_com.c @@ -329,6 +329,70 @@ void ivas_dirac_config_bands( return; } +#ifdef ARITH_HUFF_CODER_CHANGES +void ivas_get_dirac_sba_max_md_bits( + const int32_t sba_total_brate, + int16_t *bits_frame_nominal, + int16_t *metadata_max_bits, + int16_t *qmetadata_max_bit_req, + int16_t nbands ) +{ + if ( sba_total_brate <= IVAS_13k2 ) + { + *bits_frame_nominal = ACELP_9k60 / FRAMES_PER_SEC; + *metadata_max_bits = 70; + } + else if ( sba_total_brate <= IVAS_16k4 ) + { + *bits_frame_nominal = ACELP_13k20 / FRAMES_PER_SEC; + *metadata_max_bits = 80; + } + else if ( sba_total_brate <= IVAS_24k4 ) + { + *bits_frame_nominal = ACELP_16k40 / FRAMES_PER_SEC; + *metadata_max_bits = 103; + } + else if ( sba_total_brate <= IVAS_32k ) + { + *bits_frame_nominal = ACELP_32k / FRAMES_PER_SEC; + *metadata_max_bits = 214; + } + else if ( sba_total_brate <= IVAS_48k ) + { + *bits_frame_nominal = IVAS_48k / FRAMES_PER_SEC; + *metadata_max_bits = 240; + } + else if ( sba_total_brate <= IVAS_64k ) + { + *bits_frame_nominal = IVAS_64k / FRAMES_PER_SEC; + *metadata_max_bits = 200; + } + else if ( sba_total_brate <= IVAS_80k ) + { + *bits_frame_nominal = IVAS_80k / FRAMES_PER_SEC; + *metadata_max_bits = 200; + } + else if ( sba_total_brate <= IVAS_96k ) + { + *bits_frame_nominal = IVAS_96k / FRAMES_PER_SEC; + *metadata_max_bits = 200; + } + else if ( sba_total_brate <= IVAS_128k ) + { + *bits_frame_nominal = IVAS_128k / FRAMES_PER_SEC; + *metadata_max_bits = 250; + } + else + { + *bits_frame_nominal = (int16_t) ( sba_total_brate / FRAMES_PER_SEC ); + *metadata_max_bits = MAX16B; /* no limit */ + } + *metadata_max_bits = (int16_t) ceilf( (float) *metadata_max_bits * nbands / 5 ); + *qmetadata_max_bit_req = QMETADATA_MAXBIT_REQ_SBA >> 1; + + return; +} +#endif /*------------------------------------------------------------------------- * ivas_dirac_sba_config() @@ -427,7 +491,14 @@ ivas_error ivas_dirac_sba_config( } #endif } - +#ifdef ARITH_HUFF_CODER_CHANGES + ivas_get_dirac_sba_max_md_bits( + sba_total_brate, + &hQMetaData->bits_frame_nominal, + &hQMetaData->metadata_max_bits, + &hQMetaData->qmetadata_max_bit_req, + hQMetaData->q_direction[0].cfg.nbands ); +#else if ( sba_total_brate <= IVAS_13k2 ) { hQMetaData->bits_frame_nominal = ACELP_9k60 / FRAMES_PER_SEC; @@ -481,6 +552,7 @@ ivas_error ivas_dirac_sba_config( hQMetaData->metadata_max_bits = (int16_t) ceilf( (float) hQMetaData->metadata_max_bits * hQMetaData->q_direction[0].cfg.nbands / 5 ); hQMetaData->qmetadata_max_bit_req = QMETADATA_MAXBIT_REQ_SBA >> 1; +#endif return error; } diff --git a/lib_com/ivas_prot.h b/lib_com/ivas_prot.h index d3a1eda3cce85877350cdaa2ecbc5f0a4634c0b1..f4841e2526aff874dbea2ee52a06a14467f11364 100644 --- a/lib_com/ivas_prot.h +++ b/lib_com/ivas_prot.h @@ -114,6 +114,10 @@ ivas_error ivas_spar_md_enc_init ivas_error ivas_sba_enc_reconfigure( Encoder_Struct *st_ivas /* i/o: IVAS encoder structure */ ); +#ifdef ARITH_HUFF_CODER_CHANGES +int16_t ivas_sba_get_max_md_bits( + Encoder_Struct *st_ivas ); +#endif void destroy_sce_enc( SCE_ENC_HANDLE hSCE /* i/o: SCE encoder structure */ @@ -3599,6 +3603,15 @@ void ivas_dirac_config_bands( IVAS_FB_MIXER_HANDLE hFbMdft ); +#ifdef ARITH_HUFF_CODER_CHANGES +void ivas_get_dirac_sba_max_md_bits( + const int32_t sba_total_brate, + int16_t *bits_frame_nominal, + int16_t *metadata_max_bits, + int16_t *qmetadata_max_bit_req, + int16_t nbands ); +#endif + ivas_error ivas_dirac_sba_config( IVAS_QMETADATA_HANDLE hQMetaData, /* i/o: q_metadata handle */ int16_t *nchan_transport, /* o : number of transport channel needed for MASA format */ @@ -4448,7 +4461,14 @@ int16_t ivas_get_sba_num_TCs( void ivas_spar_set_bitrate_config( ivas_spar_md_com_cfg *pSpar_md_cfg, /* i/o: SPAR MD config. handle */ const int16_t table_idx, /* i : config. table index */ - const int16_t num_bands /* i : number of bands */ + const int16_t num_bands /* i : number of bands */ +#ifdef ARITH_HUFF_CODER_CHANGES + , + const int16_t dirac2spar_md_flag, + const int16_t enc_flag, + const int16_t pca_flag, + const int16_t agc_flag +#endif ); void ivas_spar_bitrate_dist( @@ -4964,7 +4984,16 @@ int16_t ivas_get_bits_to_encode( void ivas_huffman_encode( ivas_huffman_cfg_t *huff_cfg, int16_t in, int16_t *hcode, int16_t *hlen ); void ivas_spar_huff_coeffs_com_init( ivas_huff_coeffs_t *pHuff_coeffs, ivas_spar_md_com_cfg *pSpar_cfg, const int16_t table_idx, const int16_t enc_dec ); void ivas_spar_arith_coeffs_com_init( ivas_arith_coeffs_t *pArith_coeffs, ivas_spar_md_com_cfg *pSpar_cfg, const int16_t table_idx, const int16_t enc_dec ); -void ivas_arith_encode_cmplx_cell_array(ivas_arith_t *pArith_re, ivas_arith_t *pArith_re_diff, const int16_t *pDo_diff, const int16_t nB, int16_t *pSymbol_re, int16_t *pSymbol_old_re, ivas_cell_dim_t *pCell_dims, BSTR_ENC_HANDLE hMetaData, const int16_t any_diff); +#ifdef ARITH_HUFF_CODER_CHANGES +int16_t ivas_arith_encode_cmplx_cell_array( +#else +void ivas_arith_encode_cmplx_cell_array( +#endif +ivas_arith_t *pArith_re, ivas_arith_t *pArith_re_diff, const int16_t *pDo_diff, const int16_t nB, int16_t *pSymbol_re, int16_t *pSymbol_old_re, ivas_cell_dim_t *pCell_dims, BSTR_ENC_HANDLE hMetaData, const int16_t any_diff +#ifdef ARITH_HUFF_CODER_CHANGES + , int32_t wc_strat_arith +#endif +); ivas_error ivas_huffman_decode( ivas_huffman_cfg_t *huff_cfg, Decoder_State *st0, int16_t *dec_out ); void ivas_arith_decode_cmplx_cell_array( ivas_arith_t *pArith_re, ivas_arith_t *pArith_re_diff, Decoder_State *st0, ivas_cell_dim_t *pCell_dims, int16_t *pDo_diff, const int16_t nB, int16_t *pSymbol_re, int16_t *pSymbol_re_old ); diff --git a/lib_com/ivas_rom_com.c b/lib_com/ivas_rom_com.c index b083210e0796f78a2bac3fe4722ec20250858637..9f67c602f6aaf3d2994aee41aeefdc48a3941d04 100644 --- a/lib_com/ivas_rom_com.c +++ b/lib_com/ivas_rom_com.c @@ -935,7 +935,13 @@ const ivas_spar_br_table_t ivas_spar_br_table_consts[IVAS_SPAR_BR_TABLE_LEN] = { { 31, 11, 11, 1 },{ 1, 1, 1, 1 },{ 1, 1, 1, 1 } }, 1, 2, 0 }, { 256000, 0, SBA_HOA3_ORDER, FB, 24000, 4, WYXZ, 0, 0,{ { 76300, 73550, 112000 },{ 59350, 57200, 56000 },{ 42400, 40850, 48000 },{ 25450, 24500, 40000 } }, - { { 31, 11, 11, 1 },{ 1, 1, 1, 1 },{ 31, 1, 1, 1 } }, 1, 2, 0 }, + { { 31, 11, 11, 1 },{ 1, 1, 1, 1 }, +#ifdef ARITH_HUFF_CODER_CHANGES + { 1, 1, 1, 1 } +#else + { 31, 1, 1, 1 } +#endif + }, 1, 2, 0 }, { 384000, 0, SBA_FOA_ORDER, FB, 24000, 4, WYXZ, 0, 0,{ { 128000, 128000, 128000 },{ 100000, 100000, 128000 },{ 79850, 79850, 104000 },{ 66600, 66600, 104000 } }, // not yet optimized { { 31, 1, 1, 1 },{ 1, 1, 1, 1 },{ 1, 1, 1, 1 } }, 1, 2, 0 }, diff --git a/lib_com/ivas_rom_com.h b/lib_com/ivas_rom_com.h index f43bacbf3bcb91500bfb8bd65b080fe760ab05d1..a6437565c995239d1d71d7fcb6b76dcbfe0e1398 100644 --- a/lib_com/ivas_rom_com.h +++ b/lib_com/ivas_rom_com.h @@ -328,7 +328,6 @@ extern const float McMASA_LFEGain_vectors[64]; extern const float ism_azimuth_borders[4]; extern const float ism_elevation_borders[4]; - /*----------------------------------------------------------------------------------* * Param ISM ROM tables *----------------------------------------------------------------------------------*/ diff --git a/lib_com/ivas_spar_com.c b/lib_com/ivas_spar_com.c index fb0f2637653dc12f2f173922b39d4265fa441de1..c25a88e5cb464e3f5c094d187941af3a4f324db0 100644 --- a/lib_com/ivas_spar_com.c +++ b/lib_com/ivas_spar_com.c @@ -2130,12 +2130,27 @@ void ivas_spar_set_bitrate_config( ivas_spar_md_com_cfg *pSpar_md_cfg, /* i/o: SPAR MD config. handle */ const int16_t table_idx, /* i : config. table index */ const int16_t num_bands /* i : number of bands */ +#ifdef ARITH_HUFF_CODER_CHANGES + , + const int16_t dirac2spar_md_flag, + const int16_t enc_flag, + const int16_t pca_flag, + const int16_t agc_flag +#endif ) { int32_t ivas_total_brate; int16_t i, total_bits, max_bits, code, length; int16_t sba_order; int16_t md_coding_bits_header; +#ifdef ARITH_HUFF_CODER_CHANGES + int16_t agc_bits, pca_bits, num_PR_bits_dirac_bands; + int16_t bits_PR, bits_C, bits_P; + int16_t wc_coarse_strat; + int16_t n_input, n_dmx, n_dec; + int16_t quant_strat; + int16_t bands_bw; +#endif pSpar_md_cfg->nchan_transport = ivas_spar_br_table_consts[table_idx].nchan_transport; for ( i = 0; i < pSpar_md_cfg->nchan_transport; i++ ) @@ -2174,6 +2189,16 @@ void ivas_spar_set_bitrate_config( pSpar_md_cfg->tgt_bits_per_blk -= md_coding_bits_header; pSpar_md_cfg->max_bits_per_blk -= md_coding_bits_header; +#ifdef ARITH_HUFF_CODER_CHANGES + if ( ivas_total_brate < IVAS_24k4 ) + { + bands_bw = 2; + } + else + { + bands_bw = 1; + } +#endif pSpar_md_cfg->tgt_bits_per_blk = (int16_t) ceilf( ( 1.0f * pSpar_md_cfg->tgt_bits_per_blk * num_bands ) / IVAS_MAX_NUM_BANDS ); pSpar_md_cfg->max_bits_per_blk = (int16_t) ceilf( ( 1.0f * pSpar_md_cfg->max_bits_per_blk * num_bands ) / IVAS_MAX_NUM_BANDS ); @@ -2181,6 +2206,74 @@ void ivas_spar_set_bitrate_config( pSpar_md_cfg->tgt_bits_per_blk += md_coding_bits_header; pSpar_md_cfg->max_bits_per_blk += md_coding_bits_header; +#ifdef ARITH_HUFF_CODER_CHANGES + if ( enc_flag ) + { + /*calculate the actual worst case bits*/ + if ( ivas_total_brate >= BRATE_SPAR_Q_STRAT ) + { + quant_strat = QUANT_STRAT_0; + } + else + { + quant_strat = QUANT_STRAT_2; + } + + num_PR_bits_dirac_bands = ( dirac2spar_md_flag == 1 ) ? num_bands - SPAR_DIRAC_SPLIT_START_BAND : 0; + num_PR_bits_dirac_bands /= bands_bw; + num_PR_bits_dirac_bands = max( 0, num_PR_bits_dirac_bands ); + num_PR_bits_dirac_bands *= DIRAC_TO_SPAR_HBR_PRED_CHS; + + n_input = ivas_sba_get_nchan_metadata( sba_order, ivas_total_brate ); + n_dmx = ivas_spar_br_table_consts[table_idx].nchan_transport; + n_dec = n_input - n_dmx; + bits_PR = (int16_t) ceilf( log2f( ivas_spar_br_table_consts[table_idx].q_lvls[quant_strat][0] ) ); + num_PR_bits_dirac_bands *= bits_PR; + bits_PR = bits_PR * ( n_input - 1 ); + bits_C = (int16_t) ceilf( log2f( ivas_spar_br_table_consts[table_idx].q_lvls[quant_strat][1] ) ) * ( ( n_dmx - 1 ) * n_dec ); + bits_P = (int16_t) ceilf( log2f( ivas_spar_br_table_consts[table_idx].q_lvls[quant_strat][2] ) ) * ( n_dec ); + wc_coarse_strat = bits_PR + bits_C + bits_P; + wc_coarse_strat *= num_bands; + wc_coarse_strat /= bands_bw; + wc_coarse_strat -= num_PR_bits_dirac_bands; + wc_coarse_strat += md_coding_bits_header; + + if ( pSpar_md_cfg->max_bits_per_blk < wc_coarse_strat ) + { + assert( 0 ); + } + + if ( agc_flag ) + { + if ( pSpar_md_cfg->nchan_transport == 1 ) + { + agc_bits = AGC_BITS_PER_CH; + } + else + { + agc_bits = AGC_BITS_PER_CH * pSpar_md_cfg->nchan_transport + AGC_SIGNALLING_BITS; + } + } + else + { + agc_bits = AGC_SIGNALLING_BITS; + } + + if ( ivas_total_brate == PCA_BRATE && sba_order == SBA_FOA_ORDER ) + { + pca_bits = 1; + if ( pca_flag ) + { + pca_bits += IVAS_PCA_QBITS + IVAS_PCA_QBITS - 1; + } + } + else + { + pca_bits = 0; + } + pSpar_md_cfg->max_md_bits_spar = pSpar_md_cfg->max_bits_per_blk + agc_bits + pca_bits; + } +#endif return; } diff --git a/lib_com/ivas_stat_com.h b/lib_com/ivas_stat_com.h index 4c4a2299382ab75dd50eb93c05bab19e2daf6abb..56d9c21aa7d95ef44c682e1a99186f25a0321e5f 100644 --- a/lib_com/ivas_stat_com.h +++ b/lib_com/ivas_stat_com.h @@ -260,6 +260,9 @@ typedef struct ivas_spar_md_com_cfg int16_t agc_bits_ch_idx; int16_t planarCP; int16_t num_umx_chs; +#ifdef ARITH_HUFF_CODER_CHANGES + int16_t max_md_bits_spar; +#endif } ivas_spar_md_com_cfg; diff --git a/lib_com/options.h b/lib_com/options.h index ed7c70ab02b16951a5933e457c720e1c0bc34f2d..a24b45626f2d41cbb471ba89b96e97985bb826c7 100755 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -146,6 +146,7 @@ /*#define FIX_XXX_JBM_FIFO_BUFFER */ /* FhG: prevent wraparound of a length identifier in cause of large frames and many channels*/ #define HR_METADATA /* Nok: Contribution #45: encode directional MASA metadata with more bits at 384k and 512k */ #define FIX_439_OTR_PARAMS /* Philips: Issue 439: orientation tracking parameter aspects. */ +#define ARITH_HUFF_CODER_CHANGES /* DLB: Optimization of metadata memory for Huffman and arithmetic coders */ #define FIX_463_TD_RENDERER_DIRECTIVITY_RESET /* Eri: Remove unintentional reset of directivity pattern */ #define FIX_642 /* FhG: Fix for issue 642, buggy DoA-array access in DirAC head rotation*/ #define FIX_443_FD_CNG_INIT /* FhG: correct bitrate value for FD-CNG init */ diff --git a/lib_dec/ivas_spar_md_dec.c b/lib_dec/ivas_spar_md_dec.c index 5945023010770b783c78e70e8ffefe96fb6670e8..f9d34970c25e5177f2833d8bbf782e0fd1f5e4ad 100644 --- a/lib_dec/ivas_spar_md_dec.c +++ b/lib_dec/ivas_spar_md_dec.c @@ -467,8 +467,13 @@ ivas_error ivas_spar_md_dec_init( ivas_sba_get_spar_hoa_ch_ind( num_channels, hDecoderConfig->ivas_total_brate, hMdDec->HOA_md_ind ); hMdDec->spar_md.num_bands = ( hMdDec->spar_hoa_md_flag ) ? IVAS_MAX_NUM_BANDS : min( IVAS_MAX_NUM_BANDS, SPAR_DIRAC_SPLIT_START_BAND ); - - ivas_spar_set_bitrate_config( &hMdDec->spar_md_cfg, hMdDec->table_idx, hMdDec->spar_md.num_bands ); + ivas_spar_set_bitrate_config( &hMdDec->spar_md_cfg, hMdDec->table_idx, hMdDec->spar_md.num_bands +#ifdef ARITH_HUFF_CODER_CHANGES + , + hMdDec->spar_hoa_dirac2spar_md_flag, + 0, 0, 0 +#endif + ); nchan_transport = hMdDec->spar_md_cfg.nchan_transport; diff --git a/lib_enc/ivas_entropy_coder.c b/lib_enc/ivas_entropy_coder.c index a8f5219f2a3f692b44e6edfcfbfce3771d0a8315..830a05e41a3ce8c426c5f6def0012141c3d65904 100644 --- a/lib_enc/ivas_entropy_coder.c +++ b/lib_enc/ivas_entropy_coder.c @@ -128,12 +128,20 @@ static ivas_error ivas_get_dyn_freq_model( * * Arith encoding of an array of symbols *-----------------------------------------------------------------------------------------*/ - +#ifdef ARITH_HUFF_CODER_CHANGES +static int16_t ivas_arith_encode_array( +#else static void ivas_arith_encode_array( +#endif int16_t *pInput, ivas_arith_t *pArith, BSTR_ENC_HANDLE hMetaData, - const int16_t in_len ) + const int16_t in_len +#ifdef ARITH_HUFF_CODER_CHANGES + , + int32_t wc_strat_arith +#endif +) { int16_t model_index, i, ind; int16_t *pCum_freq = NULL; @@ -144,6 +152,12 @@ static void ivas_arith_encode_array( if ( pArith->dyn_model_bits > 0 ) { ivas_get_dyn_freq_model( pInput, in_len, &model_index, pArith, &pCum_freq ); +#ifdef ARITH_HUFF_CODER_CHANGES + if ( ( hMetaData->nb_bits_tot + pArith->dyn_model_bits ) > wc_strat_arith ) + { + return -1; + } +#endif push_next_indice( hMetaData, model_index, pArith->dyn_model_bits ); } else @@ -158,12 +172,27 @@ static void ivas_arith_encode_array( ind = pInput[i] - pArith->vals[0]; ivas_ari_encode_14bits_ext( hMetaData, &as, ind, (const uint16_t *) pCum_freq ); +#ifdef ARITH_HUFF_CODER_CHANGES + if ( hMetaData->nb_bits_tot > wc_strat_arith ) + { + return -1; + } +#endif } ivas_ari_done_encoding_14bits( hMetaData, &as ); +#ifdef ARITH_HUFF_CODER_CHANGES + if ( hMetaData->nb_bits_tot > wc_strat_arith ) + { + return -1; + } +#endif } - +#ifdef ARITH_HUFF_CODER_CHANGES + return 0; +#else return; +#endif } @@ -172,15 +201,26 @@ static void ivas_arith_encode_array( * * Differential arith encoding *-----------------------------------------------------------------------------------------*/ - +#ifdef ARITH_HUFF_CODER_CHANGES +static int16_t ivas_arithCoder_encode_array_diff( +#else static void ivas_arithCoder_encode_array_diff( +#endif ivas_arith_t *pArith_diff, int16_t *pIn_new, int16_t *pIn_old_scratch, const int16_t length, - BSTR_ENC_HANDLE hMetaData ) + BSTR_ENC_HANDLE hMetaData +#ifdef ARITH_HUFF_CODER_CHANGES + , + int32_t wc_strat_arith +#endif +) { int16_t n; +#ifdef ARITH_HUFF_CODER_CHANGES + int16_t arith_result; +#endif if ( length > 0 ) { @@ -191,10 +231,22 @@ static void ivas_arithCoder_encode_array_diff( ivas_wrap_arround( pIn_old_scratch, pArith_diff->vals[0], pArith_diff->vals[pArith_diff->range - 1], length ); +#ifdef ARITH_HUFF_CODER_CHANGES + arith_result = ivas_arith_encode_array( pIn_old_scratch, pArith_diff, hMetaData, length, + wc_strat_arith ); + if ( arith_result < 0 ) + { + return -1; + } +#else ivas_arith_encode_array( pIn_old_scratch, pArith_diff, hMetaData, length ); +#endif } - +#ifdef ARITH_HUFF_CODER_CHANGES + return 0; +#else return; +#endif } @@ -228,16 +280,27 @@ void ivas_huffman_encode( * * Arithmetic encode a cell array *-----------------------------------------------------------------------------------------*/ - +#ifdef ARITH_HUFF_CODER_CHANGES +static int16_t arith_encode_cell_array( +#else static void arith_encode_cell_array( +#endif ivas_cell_dim_t *pCell_dims, BSTR_ENC_HANDLE hMetaData, const int16_t nB, ivas_arith_t *pArith, - int16_t *pSymbol ) + int16_t *pSymbol +#ifdef ARITH_HUFF_CODER_CHANGES + , + int32_t wc_strat_arith +#endif +) { int16_t total_symbol_len = 0; int16_t i; +#ifdef ARITH_HUFF_CODER_CHANGES + int16_t arith_result; +#endif for ( i = 0; i < nB; i++ ) { @@ -250,11 +313,24 @@ static void arith_encode_cell_array( { if ( pArith->range > 1 ) { +#ifdef ARITH_HUFF_CODER_CHANGES + arith_result = ivas_arith_encode_array( pSymbol, pArith, hMetaData, total_symbol_len, + wc_strat_arith ); + if ( arith_result < 0 ) + { + return -1; + } +#else ivas_arith_encode_array( pSymbol, pArith, hMetaData, total_symbol_len ); +#endif } } +#ifdef ARITH_HUFF_CODER_CHANGES + return 0; +#else return; +#endif } @@ -263,16 +339,27 @@ static void arith_encode_cell_array( * * Arithmetic encode a cell array - differential *-----------------------------------------------------------------------------------------*/ - +#ifdef ARITH_HUFF_CODER_CHANGES +static int16_t arith_encode_cell_array_diff( +#else static void arith_encode_cell_array_diff( +#endif const ivas_cell_dim_t *pCell_dims, BSTR_ENC_HANDLE hMetaData, int16_t nB, ivas_arith_t *pArith_diff, int16_t *pSymbol_old, - int16_t *pSymbol ) + int16_t *pSymbol +#ifdef ARITH_HUFF_CODER_CHANGES + , + int32_t wc_strat_arith +#endif +) { int16_t i, total_symbol_len; +#ifdef ARITH_HUFF_CODER_CHANGES + int16_t arith_result; +#endif total_symbol_len = 0; for ( i = 0; i < nB; i++ ) @@ -286,11 +373,24 @@ static void arith_encode_cell_array_diff( { if ( pArith_diff->range > 1 ) { +#ifdef ARITH_HUFF_CODER_CHANGES + arith_result = ivas_arithCoder_encode_array_diff( pArith_diff, pSymbol, pSymbol_old, total_symbol_len, hMetaData, + wc_strat_arith ); + if ( arith_result < 0 ) + { + return -1; + } +#else ivas_arithCoder_encode_array_diff( pArith_diff, pSymbol, pSymbol_old, total_symbol_len, hMetaData ); +#endif } } +#ifdef ARITH_HUFF_CODER_CHANGES + return 0; +#else return; +#endif } @@ -299,8 +399,11 @@ static void arith_encode_cell_array_diff( * * Arithmetic encode a cell array *-----------------------------------------------------------------------------------------*/ - +#ifdef ARITH_HUFF_CODER_CHANGES +int16_t ivas_arith_encode_cmplx_cell_array( +#else void ivas_arith_encode_cmplx_cell_array( +#endif ivas_arith_t *pArith_re, ivas_arith_t *pArith_re_diff, const int16_t *pDo_diff, @@ -309,7 +412,12 @@ void ivas_arith_encode_cmplx_cell_array( int16_t *pSymbol_old_re, ivas_cell_dim_t *pCell_dims, BSTR_ENC_HANDLE hMetaData, - const int16_t any_diff ) + const int16_t any_diff +#ifdef ARITH_HUFF_CODER_CHANGES + , + int32_t wc_strat_arith +#endif +) { int16_t input_old[IVAS_MAX_INPUT_LEN]; int16_t input_new[IVAS_MAX_INPUT_LEN]; @@ -317,6 +425,9 @@ void ivas_arith_encode_cmplx_cell_array( ivas_cell_dim_t cell_dim[IVAS_MAX_NUM_BANDS], cell_dim_diff[IVAS_MAX_NUM_BANDS]; int16_t len, idx, i, j, idx1; int16_t total_len; +#ifdef ARITH_HUFF_CODER_CHANGES + int16_t arith_result; +#endif idx1 = 0; if ( any_diff == 1 ) @@ -382,14 +493,44 @@ void ivas_arith_encode_cmplx_cell_array( }*/ #endif +#ifdef ARITH_HUFF_CODER_CHANGES + arith_result = arith_encode_cell_array( cell_dim, hMetaData, nB, pArith_re, input, + wc_strat_arith ); + if ( arith_result < 0 ) + { + return -1; + } + + arith_result = arith_encode_cell_array_diff( cell_dim_diff, hMetaData, nB, pArith_re_diff, input_old, input_new, + wc_strat_arith ); + if ( arith_result < 0 ) + { + return -1; + } +#else arith_encode_cell_array( cell_dim, hMetaData, nB, pArith_re, input ); arith_encode_cell_array_diff( cell_dim_diff, hMetaData, nB, pArith_re_diff, input_old, input_new ); +#endif } else { +#ifdef ARITH_HUFF_CODER_CHANGES + arith_result = arith_encode_cell_array( pCell_dims, hMetaData, nB, pArith_re, pSymbol_re, + wc_strat_arith ); + if ( arith_result < 0 ) + { + return -1; + } +#else arith_encode_cell_array( pCell_dims, hMetaData, nB, pArith_re, pSymbol_re ); +#endif } + +#ifdef ARITH_HUFF_CODER_CHANGES + return 0; +#else return; +#endif } diff --git a/lib_enc/ivas_sba_enc.c b/lib_enc/ivas_sba_enc.c index 3a2dd72820778d2af4230f2d0426aa54406e63cf..39da325d620109c32b1e4116ac82a614bc817450 100644 --- a/lib_enc/ivas_sba_enc.c +++ b/lib_enc/ivas_sba_enc.c @@ -277,3 +277,26 @@ ivas_error ivas_sba_enc_reconfigure( return error; } + +#ifdef ARITH_HUFF_CODER_CHANGES +int16_t ivas_sba_get_max_md_bits( + Encoder_Struct *st_ivas ) +{ + int16_t max_md_bits; + int16_t max_bits; + if ( ivas_get_hodirac_flag( st_ivas->hEncoderConfig->ivas_total_brate, st_ivas->sba_analysis_order ) ) + { + max_bits = 2000; + } + else + { + max_bits = 500; + } + max_md_bits = min( st_ivas->hQMetaData->metadata_max_bits + 1, max_bits ); // TODO: remove 500 once max MD bits has been defined at all bitrates in DirAC + if ( st_ivas->sba_mode == SBA_MODE_SPAR ) + { + max_md_bits += st_ivas->hSpar->hMdEnc->spar_md_cfg.max_md_bits_spar; + } + return max_md_bits; +} +#endif diff --git a/lib_enc/ivas_spar_encoder.c b/lib_enc/ivas_spar_encoder.c index 39d1d6ead71169e342e9c841f70c8d142dc9fdca..ae28c8b8a35aae8875c7d53328245d5ea20dbbeb 100644 --- a/lib_enc/ivas_spar_encoder.c +++ b/lib_enc/ivas_spar_encoder.c @@ -438,8 +438,18 @@ static ivas_error ivas_spar_enc_process( float energySum, vecLen; int16_t nchan_fb_in; +#ifdef ARITH_HUFF_CODER_CHANGES + /*Commented for now*/ + /*int16_t start_nb_bits; + int16_t total_md_bits, total_sba_bits;*/ +#endif push_wmops( "ivas_spar_enc_process" ); +#ifdef ARITH_HUFF_CODER_CHANGES + /*Commented for now*/ + // start_nb_bits = hMetaData->nb_bits_tot; +#endif + /*-----------------------------------------------------------------------------------------* * Initialization *-----------------------------------------------------------------------------------------*/ @@ -646,7 +656,15 @@ static ivas_error ivas_spar_enc_process( } else { - ivas_spar_set_bitrate_config( &hSpar->hMdEnc->spar_md_cfg, table_idx, ( hSpar->hMdEnc->spar_hoa_md_flag ) ? IVAS_MAX_NUM_BANDS : SPAR_DIRAC_SPLIT_START_BAND ); + ivas_spar_set_bitrate_config( &hSpar->hMdEnc->spar_md_cfg, table_idx, + ( hSpar->hMdEnc->spar_hoa_md_flag ) ? IVAS_MAX_NUM_BANDS : SPAR_DIRAC_SPLIT_START_BAND +#ifdef ARITH_HUFF_CODER_CHANGES + , + hSpar->hMdEnc->spar_hoa_dirac2spar_md_flag, + 1, hEncoderConfig->Opt_PCA_ON, + hSpar->AGC_Enable +#endif + ); } } /*-----------------------------------------------------------------------------------------* @@ -1007,5 +1025,12 @@ static ivas_error ivas_spar_enc_process( pop_wmops(); +#ifdef ARITH_HUFF_CODER_CHANGES + /*Commented for now*/ + /*total_md_bits = hMetaData->nb_bits_tot - start_nb_bits; + total_sba_bits = ivas_sba_get_max_md_bits( st_ivas ); + assert( total_md_bits <= total_sba_bits );*/ +#endif + return error; } diff --git a/lib_enc/ivas_spar_md_enc.c b/lib_enc/ivas_spar_md_enc.c index f93c9e6ca5fc88b38627278762fe1e829f72e2a5..ca5728f81ed4bceea3897899683abc9ca1017b1a 100644 --- a/lib_enc/ivas_spar_md_enc.c +++ b/lib_enc/ivas_spar_md_enc.c @@ -42,7 +42,6 @@ #include #include "wmc_auto.h" - /*------------------------------------------------------------------------------------------* * PreProcessor *------------------------------------------------------------------------------------------*/ @@ -72,9 +71,16 @@ static void ivas_band_mixer( float *cov_re[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH], static void ivas_get_band_differential_index( ivas_band_coeffs_ind_t *pBand_idx, const int16_t q_levels[2], const int16_t one_sided, const int16_t nB, const int16_t complex_cov, const int16_t dim, const ivas_coeffs_type_t coeff_type ); +#ifdef ARITH_HUFF_CODER_CHANGES +static int16_t ivas_get_huffman_coded_bs( ivas_spar_md_enc_state_t *hMdEnc, BSTR_ENC_HANDLE hMetaData, const int16_t nB, const int16_t qsi, const int16_t planarCP, const int16_t bands_bw ); +#else static void ivas_get_huffman_coded_bs( ivas_spar_md_enc_state_t *hMdEnc, BSTR_ENC_HANDLE hMetaData, const int16_t nB, const int16_t qsi, const int16_t planarCP, const int16_t bands_bw ); - +#endif +#ifdef ARITH_HUFF_CODER_CHANGES +static int16_t ivas_get_arith_coded_bs( ivas_spar_md_enc_state_t *hMdEnc, BSTR_ENC_HANDLE hMetaData, const int16_t *pDo_diff, const int16_t bands_bw, const int16_t nB, const int16_t qsi, const int16_t planarCP, const int16_t strat, const int32_t ivas_total_brate ); +#else static void ivas_get_arith_coded_bs( ivas_spar_md_enc_state_t *hMdEnc, BSTR_ENC_HANDLE hMetaData, const int16_t *pDo_diff, const int16_t bands_bw, const int16_t nB, const int16_t qsi, const int16_t planarCP, const int16_t strat, const int32_t ivas_total_brate ); +#endif static ivas_error ivas_spar_set_enc_config( ivas_spar_md_enc_state_t *hMdEnc, int16_t *max_freq_per_chan, const int16_t nchan_transport, float *pFC, const int16_t nchan_inp ); @@ -111,7 +117,6 @@ ivas_error ivas_spar_md_enc_open( ivas_spar_md_enc_state_t *hMdEnc; ivas_error error; int16_t num_channels, i, j; - error = IVAS_ERR_OK; if ( ( hMdEnc = (ivas_spar_md_enc_state_t *) malloc( sizeof( ivas_spar_md_enc_state_t ) ) ) == NULL ) @@ -321,7 +326,20 @@ ivas_error ivas_spar_md_enc_init( ivas_sba_get_spar_hoa_ch_ind( num_channels, hEncoderConfig->ivas_total_brate, hMdEnc->HOA_md_ind ); table_idx = ivas_get_spar_table_idx( hEncoderConfig->ivas_total_brate, sba_order, SPAR_CONFIG_BW, NULL, NULL ); - ivas_spar_set_bitrate_config( &hMdEnc->spar_md_cfg, table_idx, ( hMdEnc->spar_hoa_md_flag ) ? IVAS_MAX_NUM_BANDS : SPAR_DIRAC_SPLIT_START_BAND ); + ivas_spar_set_bitrate_config( &hMdEnc->spar_md_cfg, table_idx, + ( hMdEnc->spar_hoa_md_flag ) ? IVAS_MAX_NUM_BANDS : SPAR_DIRAC_SPLIT_START_BAND +#ifdef ARITH_HUFF_CODER_CHANGES + , + hMdEnc->spar_hoa_dirac2spar_md_flag, + 1, hEncoderConfig->Opt_PCA_ON, +#ifndef DEBUG_AGC_ENCODER_CMD_OPTION + ivas_agc_enc_get_flag( ivas_spar_br_table_consts[table_idx].nchan_transport ) +#else + ivas_agc_enc_get_flag( hEncoderConfig->Opt_AGC_ON, ivas_spar_br_table_consts[table_idx].nchan_transport ) +#endif + +#endif + ); /* get FB coefficients */ for ( i = 0; i < IVAS_MAX_NUM_BANDS; i++ ) @@ -577,12 +595,23 @@ ivas_error ivas_spar_md_enc_process( int16_t last_ind_start; #endif BSTR_ENC_DATA hMetaData_tmp; +#ifdef ARITH_HUFF_CODER_CHANGES + Indice *ind_list_tmp; + int16_t md_indices_allocated; +#else Indice ind_list_tmp[MAX_BITS_METADATA]; // IVAS_fmToDo: size to be optimized +#endif #ifdef IND_LIST_DYN int16_t max_num_indices_tmp; #endif float Wscale[IVAS_MAX_NUM_BANDS]; +#ifdef ARITH_HUFF_CODER_CHANGES + /*extra 16 bits for arithmetic coder as overshoot check is after a symbol is written*/ + md_indices_allocated = hMdEnc->spar_md_cfg.max_bits_per_blk + IVAS_SPAR_ARITH_OVERSHOOT_BITS; + ind_list_tmp = (Indice *) malloc( sizeof( Indice ) * md_indices_allocated ); +#endif + num_quant_strats = hMdEnc->spar_md_cfg.num_quant_strats; num_ch = ivas_sba_get_nchan_metadata( sba_order, hEncoderConfig->ivas_total_brate ); @@ -630,6 +659,7 @@ ivas_error ivas_spar_md_enc_process( } hMetaData_tmp.ind_list = ind_list_tmp; + hMetaData_tmp.nb_bits_tot = 0; #ifdef IND_LIST_DYN max_num_indices_tmp = MAX_BITS_METADATA; hMetaData_tmp.ivas_max_num_indices = &max_num_indices_tmp; @@ -670,7 +700,7 @@ ivas_error ivas_spar_md_enc_process( { for ( b = SPAR_DIRAC_SPLIT_START_BAND; b < num_bands; b++ ) { - for ( i = 0; i < FOA_CHANNELS - 1; i++ ) + for ( i = 0; i < DIRAC_TO_SPAR_HBR_PRED_CHS; i++ ) { pred_coeffs_re_local[i][b] = hMdEnc->spar_md.band_coeffs[b].pred_re[i]; } @@ -847,7 +877,7 @@ ivas_error ivas_spar_md_enc_process( { for ( b = SPAR_DIRAC_SPLIT_START_BAND; b < num_bands; b++ ) { - for ( i = 0; i < FOA_CHANNELS - 1; i++ ) + for ( i = 0; i < DIRAC_TO_SPAR_HBR_PRED_CHS; i++ ) { /* Use the prediction coeffs computed based on DirAC MD to generate mixer matrix */ pred_coeffs_re[i][b] = pred_coeffs_re_local[i][b]; @@ -948,24 +978,35 @@ ivas_error ivas_spar_md_enc_process( strat = cs[i]; if ( strat != NO_STRAT ) { +#ifdef ARITH_HUFF_CODER_CHANGES + reset_indices_enc( &hMetaData_tmp, md_indices_allocated ); +#else reset_indices_enc( &hMetaData_tmp, MAX_BITS_METADATA ); - +#endif ivas_write_spar_md_bitstream( hMdEnc, num_bands, bands_bw, &hMetaData_tmp, hEncoderConfig->ivas_total_brate, strat, qsi, planarCP ); - - if ( hMetaData->nb_bits_tot == bit_pos_start || hMetaData_tmp.nb_bits_tot < ( hMetaData->nb_bits_tot - bit_pos_start ) ) +#ifdef ARITH_HUFF_CODER_CHANGES + /*write to main buffer if its a valid bitstream*/ + if ( hMetaData_tmp.nb_bits_tot > 0 ) { - write_metadata_buffer( &hMetaData_tmp, hMetaData, bit_pos_start, next_ind_start +#endif + if ( hMetaData->nb_bits_tot == bit_pos_start || hMetaData_tmp.nb_bits_tot < ( hMetaData->nb_bits_tot - bit_pos_start ) ) + { + write_metadata_buffer( &hMetaData_tmp, hMetaData, bit_pos_start, next_ind_start #ifndef IND_LIST_DYN - , - last_ind_start + , + last_ind_start #endif - ); - code_strat = strat; - } - if ( hMetaData->nb_bits_tot - bit_pos_start + ( ( ( hEncoderConfig->ivas_total_brate == IVAS_256k ) && ( sba_order == SBA_FOA_ORDER ) ) ? 1 : 0 ) <= hMdEnc->spar_md_cfg.tgt_bits_per_blk ) - { - packed_ok = 1; - break; + ); + code_strat = strat; + } + if ( hMetaData->nb_bits_tot - bit_pos_start + ( ( ( hEncoderConfig->ivas_total_brate == IVAS_256k ) && ( sba_order == SBA_FOA_ORDER ) ) ? 1 : 0 ) <= hMdEnc->spar_md_cfg.tgt_bits_per_blk ) + { + + { + packed_ok = 1; + break; + } + } } } } @@ -975,12 +1016,27 @@ ivas_error ivas_spar_md_enc_process( break; } - if ( hMetaData->nb_bits_tot - bit_pos_start + ( ( ( hEncoderConfig->ivas_total_brate == IVAS_256k ) && ( sba_order == SBA_FOA_ORDER ) ) ? 1 : 0 ) <= hMdEnc->spar_md_cfg.max_bits_per_blk ) +#ifdef ARITH_HUFF_CODER_CHANGES + /*only if valid bitstream was written to main buffer*/ + if ( hMetaData->nb_bits_tot > bit_pos_start ) +#endif { - break; + if ( hMetaData->nb_bits_tot - bit_pos_start + ( ( ( hEncoderConfig->ivas_total_brate == IVAS_256k ) && ( sba_order == SBA_FOA_ORDER ) ) ? 1 : 0 ) <= hMdEnc->spar_md_cfg.max_bits_per_blk ) + { +#ifdef ARITH_HUFF_CODER_CHANGES + packed_ok = 1; +#endif + break; + } } } +#ifdef ARITH_HUFF_CODER_CHANGES + if ( dtx_vad == 1 ) + { + assert( packed_ok == 1 ); + } +#endif if ( hEncoderConfig->ivas_total_brate >= IVAS_256k ) { assert( qsi == 0 ); @@ -1218,12 +1274,15 @@ ivas_error ivas_spar_md_enc_process( fclose( fp ); #endif - ivas_store_prior_coeffs( hMdEnc, num_bands, - code_strat, dtx_vad, qsi ); + ivas_store_prior_coeffs( hMdEnc, num_bands, code_strat, dtx_vad, qsi ); hMdEnc->spar_md.dtx_vad = dtx_vad; hMdEnc->spar_md.num_bands = num_bands; +#ifdef ARITH_HUFF_CODER_CHANGES + free( ind_list_tmp ); +#endif + return IVAS_ERR_OK; } @@ -1293,6 +1352,9 @@ static void ivas_write_spar_md_bitstream( { int16_t no_ec, i; int16_t do_diff[IVAS_MAX_NUM_BANDS]; +#ifdef ARITH_HUFF_CODER_CHANGES + int16_t entropy_coding_result; +#endif if ( strat == NO_STRAT ) { @@ -1397,16 +1459,29 @@ static void ivas_write_spar_md_bitstream( #endif if ( no_ec == 1 ) { - ivas_get_huffman_coded_bs( hMdEnc, hMetaData, nB, qsi, planarCP, - bands_bw ); +#ifdef ARITH_HUFF_CODER_CHANGES + entropy_coding_result = +#endif + ivas_get_huffman_coded_bs( hMdEnc, hMetaData, nB, qsi, planarCP, + bands_bw ); } else { - ivas_get_arith_coded_bs( hMdEnc, hMetaData, do_diff, bands_bw, nB, qsi, planarCP, - strat, - ivas_total_brate ); +#ifdef ARITH_HUFF_CODER_CHANGES + entropy_coding_result = +#endif + ivas_get_arith_coded_bs( hMdEnc, hMetaData, do_diff, bands_bw, nB, qsi, planarCP, + strat, + ivas_total_brate ); } +#ifdef ARITH_HUFF_CODER_CHANGES + if ( entropy_coding_result < 0 ) + { + hMetaData->nb_bits_tot = 0; + } +#endif + return; } @@ -1416,8 +1491,11 @@ static void ivas_write_spar_md_bitstream( * * Generate huffman coded bitstream *-----------------------------------------------------------------------------------------*/ - +#ifdef ARITH_HUFF_CODER_CHANGES +static int16_t ivas_get_huffman_coded_bs( +#else static void ivas_get_huffman_coded_bs( +#endif ivas_spar_md_enc_state_t *hMdEnc, BSTR_ENC_HANDLE hMetaData, const int16_t nB, @@ -1441,7 +1519,7 @@ static void ivas_get_huffman_coded_bs( { if ( i >= SPAR_DIRAC_SPLIT_START_BAND ) { - pred_offset = FOA_CHANNELS - 1; + pred_offset = DIRAC_TO_SPAR_HBR_PRED_CHS; } } @@ -1450,6 +1528,12 @@ static void ivas_get_huffman_coded_bs( for ( j = pred_offset; j < pred_coeff_dim; j++ ) { ivas_huffman_encode( &hMdEnc->huff_coeffs.pred_huff_re[qsi], hMdEnc->spar_md.band_coeffs_idx[i].pred_index_re[j], &code, &len ); +#ifdef ARITH_HUFF_CODER_CHANGES + if ( ( hMetaData->nb_bits_tot + len ) > hMdEnc->spar_md_cfg.max_bits_per_blk ) + { + return -1; + } +#endif push_next_indice( hMetaData, code, len ); } @@ -1458,6 +1542,12 @@ static void ivas_get_huffman_coded_bs( if ( keep_planar[(int16_t) floor( j / ( ndm - 1 ) )] ) { ivas_huffman_encode( &hMdEnc->huff_coeffs.drct_huff_re[qsi], hMdEnc->spar_md.band_coeffs_idx[i].drct_index_re[j], &code, &len ); +#ifdef ARITH_HUFF_CODER_CHANGES + if ( ( hMetaData->nb_bits_tot + len ) > hMdEnc->spar_md_cfg.max_bits_per_blk ) + { + return -1; + } +#endif push_next_indice( hMetaData, code, len ); } } @@ -1467,6 +1557,12 @@ static void ivas_get_huffman_coded_bs( if ( keep_planar[j] ) { ivas_huffman_encode( &hMdEnc->huff_coeffs.decd_huff_re[qsi], hMdEnc->spar_md.band_coeffs_idx[i].decd_index_re[j], &code, &len ); +#ifdef ARITH_HUFF_CODER_CHANGES + if ( ( hMetaData->nb_bits_tot + len ) > hMdEnc->spar_md_cfg.max_bits_per_blk ) + { + return -1; + } +#endif push_next_indice( hMetaData, code, len ); } } @@ -1476,24 +1572,46 @@ static void ivas_get_huffman_coded_bs( for ( j = pred_offset; j < pred_coeff_dim; j++ ) { ivas_huffman_encode( &hMdEnc->huff_coeffs.pred_huff_re[qsi], hMdEnc->spar_md.band_coeffs_idx[i].pred_index_re[j], &code, &len ); +#ifdef ARITH_HUFF_CODER_CHANGES + if ( ( hMetaData->nb_bits_tot + len ) > hMdEnc->spar_md_cfg.max_bits_per_blk ) + { + return -1; + } +#endif push_next_indice( hMetaData, code, len ); } for ( j = 0; j < ndec * ( ndm - 1 ); j++ ) { ivas_huffman_encode( &hMdEnc->huff_coeffs.drct_huff_re[qsi], hMdEnc->spar_md.band_coeffs_idx[i].drct_index_re[j], &code, &len ); +#ifdef ARITH_HUFF_CODER_CHANGES + if ( ( hMetaData->nb_bits_tot + len ) > hMdEnc->spar_md_cfg.max_bits_per_blk ) + { + return -1; + } +#endif push_next_indice( hMetaData, code, len ); } for ( j = 0; j < ndec; j++ ) { ivas_huffman_encode( &hMdEnc->huff_coeffs.decd_huff_re[qsi], hMdEnc->spar_md.band_coeffs_idx[i].decd_index_re[j], &code, &len ); +#ifdef ARITH_HUFF_CODER_CHANGES + if ( ( hMetaData->nb_bits_tot + len ) > hMdEnc->spar_md_cfg.max_bits_per_blk ) + { + return -1; + } +#endif push_next_indice( hMetaData, code, len ); } } } +#ifdef ARITH_HUFF_CODER_CHANGES + return 0; +#else return; +#endif } @@ -1502,8 +1620,11 @@ static void ivas_get_huffman_coded_bs( * * Generate arithmetic coded bitstream *-----------------------------------------------------------------------------------------*/ - +#ifdef ARITH_HUFF_CODER_CHANGES +static int16_t ivas_get_arith_coded_bs( +#else static void ivas_get_arith_coded_bs( +#endif ivas_spar_md_enc_state_t *hMdEnc, BSTR_ENC_HANDLE hMetaData, const int16_t *pDo_diff, @@ -1522,6 +1643,9 @@ static void ivas_get_arith_coded_bs( ivas_cell_dim_t decx_cell_dims[IVAS_MAX_NUM_BANDS]; int16_t symbol_arr_re[IVAS_MAX_INPUT_LEN]; int16_t symbol_arr_old_re[IVAS_MAX_INPUT_LEN]; +#ifdef ARITH_HUFF_CODER_CHANGES + int16_t arith_result; +#endif for ( i = 0; i < nB; i++ ) { @@ -1578,12 +1702,12 @@ static void ivas_get_arith_coded_bs( for ( j = 0; j < pred_cell_dims[i].dim1; j++ ) { hMdEnc->spar_md.band_coeffs_idx[i].pred_index_re[j] = - hMdEnc->spar_md.band_coeffs_idx[i].pred_index_re[j + ( FOA_CHANNELS - 1 )]; + hMdEnc->spar_md.band_coeffs_idx[i].pred_index_re[j + DIRAC_TO_SPAR_HBR_PRED_CHS]; if ( any_diff == 1 ) { hMdEnc->spar_md_prior.band_coeffs_idx_mapped[i].pred_index_re[j] = - hMdEnc->spar_md_prior.band_coeffs_idx_mapped[i].pred_index_re[j + ( FOA_CHANNELS - 1 )]; + hMdEnc->spar_md_prior.band_coeffs_idx_mapped[i].pred_index_re[j + DIRAC_TO_SPAR_HBR_PRED_CHS]; } } } @@ -1596,8 +1720,18 @@ static void ivas_get_arith_coded_bs( ivas_copy_band_coeffs_idx_to_arr( hMdEnc->spar_md_prior.band_coeffs_idx_mapped, nB, symbol_arr_old_re, pred_cell_dims, PRED_COEFF, planarCP ); } +#ifdef ARITH_HUFF_CODER_CHANGES + arith_result = ivas_arith_encode_cmplx_cell_array( &hMdEnc->arith_coeffs.pred_arith_re[qsi], &hMdEnc->arith_coeffs.pred_arith_re_diff[qsi], pDo_diff, nB, + symbol_arr_re, symbol_arr_old_re, pred_cell_dims, hMetaData, any_diff, + hMdEnc->spar_md_cfg.max_bits_per_blk ); + if ( arith_result < 0 ) + { + return -1; + } +#else ivas_arith_encode_cmplx_cell_array( &hMdEnc->arith_coeffs.pred_arith_re[qsi], &hMdEnc->arith_coeffs.pred_arith_re_diff[qsi], pDo_diff, nB, symbol_arr_re, symbol_arr_old_re, pred_cell_dims, hMetaData, any_diff ); +#endif if ( hMdEnc->spar_hoa_md_flag && hMdEnc->spar_hoa_dirac2spar_md_flag ) { @@ -1607,10 +1741,10 @@ static void ivas_get_arith_coded_bs( { for ( j = pred_cell_dims[i].dim1 - 1; j >= 0; j-- ) { - hMdEnc->spar_md.band_coeffs_idx[i].pred_index_re[j + ( FOA_CHANNELS - 1 )] = + hMdEnc->spar_md.band_coeffs_idx[i].pred_index_re[j + DIRAC_TO_SPAR_HBR_PRED_CHS] = hMdEnc->spar_md.band_coeffs_idx[i].pred_index_re[j]; } - for ( j = 0; j < FOA_CHANNELS - 1; j++ ) + for ( j = 0; j < DIRAC_TO_SPAR_HBR_PRED_CHS; j++ ) { hMdEnc->spar_md.band_coeffs_idx[i].pred_index_re[j] = 0; } @@ -1639,8 +1773,19 @@ static void ivas_get_arith_coded_bs( } } +#ifdef ARITH_HUFF_CODER_CHANGES + arith_result = ivas_arith_encode_cmplx_cell_array( &hMdEnc->arith_coeffs.drct_arith_re[qsi], &hMdEnc->arith_coeffs.drct_arith_re_diff[qsi], pDo_diff, nB, + symbol_arr_re, symbol_arr_old_re, drct_cell_dims, hMetaData, any_diff, + hMdEnc->spar_md_cfg.max_bits_per_blk ); + if ( arith_result < 0 ) + { + return -1; + } +#else ivas_arith_encode_cmplx_cell_array( &hMdEnc->arith_coeffs.drct_arith_re[qsi], &hMdEnc->arith_coeffs.drct_arith_re_diff[qsi], pDo_diff, nB, symbol_arr_re, symbol_arr_old_re, drct_cell_dims, hMetaData, any_diff ); +#endif + ivas_copy_band_coeffs_idx_to_arr( hMdEnc->spar_md.band_coeffs_idx, nB, symbol_arr_re, decd_cell_dims, DECD_COEFF, planarCP ); @@ -1656,9 +1801,19 @@ static void ivas_get_arith_coded_bs( decd_cell_dims[i].dim1 = decd_cell_dims[i].dim1 - IVAS_SPAR_HOA3_NP_CHS; } } - +#ifdef ARITH_HUFF_CODER_CHANGES + arith_result = ivas_arith_encode_cmplx_cell_array( &hMdEnc->arith_coeffs.decd_arith_re[qsi], &hMdEnc->arith_coeffs.decd_arith_re_diff[qsi], pDo_diff, nB, + symbol_arr_re, symbol_arr_old_re, decd_cell_dims, hMetaData, any_diff, + hMdEnc->spar_md_cfg.max_bits_per_blk ); + if ( arith_result < 0 ) + { + return -1; + } +#else ivas_arith_encode_cmplx_cell_array( &hMdEnc->arith_coeffs.decd_arith_re[qsi], &hMdEnc->arith_coeffs.decd_arith_re_diff[qsi], pDo_diff, nB, symbol_arr_re, symbol_arr_old_re, decd_cell_dims, hMetaData, any_diff ); +#endif + ivas_copy_band_coeffs_idx_to_arr( hMdEnc->spar_md.band_coeffs_idx, nB, symbol_arr_re, decx_cell_dims, DECX_COEFF, planarCP ); @@ -1667,7 +1822,11 @@ static void ivas_get_arith_coded_bs( ivas_copy_band_coeffs_idx_to_arr( hMdEnc->spar_md_prior.band_coeffs_idx_mapped, nB, symbol_arr_old_re, decx_cell_dims, DECX_COEFF, planarCP ); } +#ifdef ARITH_HUFF_CODER_CHANGES + return 0; +#else return; +#endif }