Line data Source code
1 : /****************************************************************************************************** 2 : 3 : (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB, 4 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD., 5 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange, 6 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other 7 : contributors to this repository. All Rights Reserved. 8 : 9 : This software is protected by copyright law and by international treaties. 10 : The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB, 11 : Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD., 12 : Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange, 13 : Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other 14 : contributors to this repository retain full ownership rights in their respective contributions in 15 : the software. This notice grants no license of any kind, including but not limited to patent 16 : license, nor is any license granted by implication, estoppel or otherwise. 17 : 18 : Contributors are required to enter into the IVAS codec Public Collaboration agreement before making 19 : contributions. 20 : 21 : This software is provided "AS IS", without any express or implied warranties. The software is in the 22 : development stage. It is intended exclusively for experts who have experience with such software and 23 : solely for the purpose of inspection. All implied warranties of non-infringement, merchantability 24 : and fitness for a particular purpose are hereby disclaimed and excluded. 25 : 26 : Any dispute, controversy or claim arising under or in relation to providing this software shall be 27 : submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in 28 : accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and 29 : the United Nations Convention on Contracts on the International Sales of Goods. 30 : 31 : *******************************************************************************************************/ 32 : 33 : #include <stdint.h> 34 : #include "options.h" 35 : #include "ivas_cnst.h" 36 : #include "ivas_prot.h" 37 : #include "prot.h" 38 : #include "wmc_auto.h" 39 : #include <assert.h> 40 : 41 : 42 : /*-------------------------------------------------------------------* 43 : * Local constants 44 : *-------------------------------------------------------------------*/ 45 : 46 : #define MIN_SAFETY_BITS_LFE 30 47 : #define MIN_SAFETY_BITS_MC 5 48 : 49 : 50 : /*-------------------------------------------------------------------* 51 : * splitAvailableBitsMCT() 52 : * 53 : * split available TCX bits among channels 54 : *-------------------------------------------------------------------*/ 55 : 56 397840 : void splitAvailableBitsMCT( 57 : void **sts, /* i/o: encoder/decoder state structure */ 58 : const int16_t total_bits, /* i : total number of available bits */ 59 : const int16_t split_ratio[MCT_MAX_CHANNELS], /* i : ratio for splitting the bits */ 60 : const int16_t enc_dec, /* i : encoder or decoder flag */ 61 : const int16_t nchan /* i : number of channels */ 62 : ) 63 : { 64 : int16_t i, k, nSubframes, diff, bits_split, max_chn, tmp; 65 : int16_t *bits_frame_channel; 66 : int16_t min_chan_bits[MCT_MAX_CHANNELS], min_bits_tot, remaining_bits; 67 : int16_t core[MCT_MAX_CHANNELS]; 68 : MCT_CHAN_MODE mct_chan_mode[MCT_MAX_CHANNELS]; 69 397840 : min_bits_tot = 0; 70 : 71 2302257 : for ( i = 0; i < nchan; i++ ) 72 : { 73 1904417 : if ( enc_dec == ENC ) 74 : { 75 481787 : mct_chan_mode[i] = ( (Encoder_State *) sts[i] )->mct_chan_mode; 76 481787 : core[i] = ( (Encoder_State *) sts[i] )->core; 77 : } 78 : else 79 : { 80 1422630 : mct_chan_mode[i] = ( (Decoder_State *) sts[i] )->mct_chan_mode; 81 1422630 : core[i] = ( (Decoder_State *) sts[i] )->core; 82 : } 83 : } 84 : 85 2302257 : for ( i = 0; i < nchan; i++ ) 86 : { 87 1904417 : if ( 88 1904417 : mct_chan_mode[i] != MCT_CHAN_MODE_IGNORE ) 89 : { 90 1870274 : min_chan_bits[i] = 0; 91 : 92 1870274 : nSubframes = ( core[i] == TCX_20_CORE ) ? 1 : NB_DIV; 93 : 94 3782532 : for ( k = 0; k < nSubframes; k++ ) 95 : { 96 1912258 : min_chan_bits[i] += SMDCT_MINIMUM_ARITH_BITS + MIN_SAFETY_BITS_MC; 97 : } 98 1870274 : min_bits_tot += min_chan_bits[i]; 99 : } 100 : } 101 : 102 397840 : remaining_bits = total_bits - min_bits_tot; 103 : 104 : /*initial value of bits already given*/ 105 397840 : bits_split = 0; 106 : 107 397840 : tmp = 0; 108 397840 : max_chn = 0; 109 2302257 : for ( i = 0; i < nchan; i++ ) 110 : { 111 1904417 : if ( enc_dec == ENC ) 112 : { 113 481787 : bits_frame_channel = &( (Encoder_State *) sts[i] )->bits_frame_channel; 114 : } 115 : else /* DEC */ 116 : { 117 1422630 : bits_frame_channel = &( (Decoder_State *) sts[i] )->bits_frame_channel; 118 : } 119 : 120 1904417 : if ( 121 1904417 : mct_chan_mode[i] != MCT_CHAN_MODE_IGNORE ) 122 : { 123 1870274 : assert( split_ratio[i] >= 1 && split_ratio[i] < BITRATE_MCT_RATIO_RANGE ); 124 1870274 : *bits_frame_channel = split_ratio[i] * remaining_bits / BITRATE_MCT_RATIO_RANGE + min_chan_bits[i]; 125 1870274 : bits_split += *bits_frame_channel; 126 : 127 : /*determine channel with most bits (energy)*/ 128 1870274 : if ( *bits_frame_channel > tmp ) 129 : { 130 528501 : tmp = *bits_frame_channel; 131 528501 : max_chn = i; 132 : } 133 : } 134 : } 135 : 136 : /*if bits distributed are more than available bits, substract the proportional amount of bits from all channels*/ 137 397840 : if ( bits_split != total_bits ) 138 : { 139 379273 : diff = bits_split - total_bits; 140 : 141 : /*re-count bits distributed to each channel*/ 142 379273 : bits_split = 0; 143 : 144 2208444 : for ( i = 0; i < nchan; i++ ) 145 : { 146 1829171 : if ( enc_dec == ENC ) 147 : { 148 462680 : bits_frame_channel = &( (Encoder_State *) sts[i] )->bits_frame_channel; 149 : } 150 : else /* DEC */ 151 : { 152 1366491 : bits_frame_channel = &( (Decoder_State *) sts[i] )->bits_frame_channel; 153 : } 154 : 155 1829171 : if ( 156 1829171 : mct_chan_mode[i] != MCT_CHAN_MODE_IGNORE ) 157 : { 158 1796857 : *bits_frame_channel -= diff * split_ratio[i] / BITRATE_MCT_RATIO_RANGE; 159 1796857 : *bits_frame_channel = max( min_chan_bits[i], *bits_frame_channel ); 160 1796857 : bits_split += *bits_frame_channel; 161 : } 162 : } 163 : } 164 : 165 : /*if there any bits left assign them to the channel with the maximum energy (or more bits)*/ 166 397840 : if ( total_bits != bits_split ) 167 : { 168 379223 : if ( enc_dec == ENC ) 169 : { 170 96203 : bits_frame_channel = &( (Encoder_State *) sts[max_chn] )->bits_frame_channel; 171 : } 172 : else /* DEC */ 173 : { 174 283020 : bits_frame_channel = &( (Decoder_State *) sts[max_chn] )->bits_frame_channel; 175 : } 176 : 177 379223 : *bits_frame_channel += ( total_bits - bits_split ); 178 : 179 : /*if all channels are silent assign bits to ch 0*/ 180 379223 : if ( enc_dec == ENC ) 181 : { 182 96203 : if ( ( (Encoder_State *) sts[max_chn] )->mct_chan_mode == MCT_CHAN_MODE_IGNORE ) 183 : { 184 145 : assert( bits_split == 0 ); 185 : 186 145 : ( (Encoder_State *) sts[max_chn] )->mct_chan_mode = MCT_CHAN_MODE_REGULAR; 187 145 : ( (Encoder_State *) sts[max_chn] )->side_bits_frame_channel = ( ( (Encoder_State *) sts[max_chn] )->core ) * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL ); 188 145 : *bits_frame_channel -= ( (Encoder_State *) sts[max_chn] )->side_bits_frame_channel; 189 : } 190 : } 191 : } 192 : 193 397840 : return; 194 : }