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 "prot_fx.h"
37 : #include "ivas_prot_fx.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 191392 : void splitAvailableBitsMCT_fx(
57 : void **sts, /* i/o: encoder/decoder state structure */
58 : const Word16 total_bits, /* i : total number of available bits */
59 : const Word16 split_ratio[MCT_MAX_CHANNELS], /* i : ratio for splitting the bits Q0 */
60 : const Word16 enc_dec, /* i : encoder or decoder flag */
61 : const Word16 nchan /* i : number of channels */
62 : )
63 : {
64 : Word16 i, k, nSubframes, diff, bits_split, max_chn, tmp;
65 : Word16 *bits_frame_channel;
66 : Word16 min_chan_bits[MCT_MAX_CHANNELS], min_bits_tot, remaining_bits;
67 : Word16 core[MCT_MAX_CHANNELS];
68 : MCT_CHAN_MODE mct_chan_mode[MCT_MAX_CHANNELS];
69 191392 : min_bits_tot = 0;
70 191392 : move16();
71 :
72 1069429 : FOR( i = 0; i < nchan; i++ )
73 : {
74 878037 : IF( enc_dec == ENC )
75 : {
76 459697 : mct_chan_mode[i] = ( (Encoder_State *) sts[i] )->mct_chan_mode;
77 459697 : move32();
78 459697 : core[i] = ( (Encoder_State *) sts[i] )->core;
79 459697 : move16();
80 : }
81 : ELSE
82 : {
83 418340 : mct_chan_mode[i] = ( (Decoder_State *) sts[i] )->mct_chan_mode;
84 418340 : move32();
85 418340 : core[i] = ( (Decoder_State *) sts[i] )->core;
86 418340 : move16();
87 : }
88 : }
89 :
90 1069429 : FOR( i = 0; i < nchan; i++ )
91 : {
92 878037 : IF( NE_32( mct_chan_mode[i], MCT_CHAN_MODE_IGNORE ) )
93 : {
94 853904 : min_chan_bits[i] = 0;
95 853904 : move16();
96 853904 : IF( EQ_32( core[i], TCX_20_CORE ) )
97 : {
98 835447 : nSubframes = 1;
99 835447 : move16();
100 : }
101 : ELSE
102 : {
103 18457 : nSubframes = NB_DIV;
104 18457 : move16();
105 : }
106 1726265 : FOR( k = 0; k < nSubframes; k++ )
107 : {
108 872361 : min_chan_bits[i] = add( min_chan_bits[i], SMDCT_MINIMUM_ARITH_BITS + MIN_SAFETY_BITS_MC );
109 872361 : move16();
110 : }
111 853904 : min_bits_tot = add( min_bits_tot, min_chan_bits[i] );
112 : }
113 : }
114 :
115 191392 : remaining_bits = sub( total_bits, min_bits_tot );
116 :
117 : /*initial value of bits already given*/
118 191392 : bits_split = 0;
119 191392 : move16();
120 :
121 191392 : tmp = 0;
122 191392 : move16();
123 191392 : max_chn = 0;
124 191392 : move16();
125 1069429 : FOR( i = 0; i < nchan; i++ )
126 : {
127 878037 : IF( enc_dec == ENC )
128 : {
129 459697 : bits_frame_channel = &( (Encoder_State *) sts[i] )->bits_frame_channel;
130 : }
131 : ELSE /* DEC */
132 : {
133 418340 : bits_frame_channel = &( (Decoder_State *) sts[i] )->bits_frame_channel;
134 : }
135 878037 : move16();
136 878037 : IF( NE_32( mct_chan_mode[i], MCT_CHAN_MODE_IGNORE ) )
137 : {
138 853904 : assert( split_ratio[i] >= 1 && split_ratio[i] < BITRATE_MCT_RATIO_RANGE );
139 853904 : *bits_frame_channel = add( extract_l( L_shr( L_mult0( split_ratio[i], remaining_bits ), NBBITS_MCT_RATIO ) ), min_chan_bits[i] );
140 853904 : bits_split = add( bits_split, *bits_frame_channel );
141 :
142 : /*determine channel with most bits (energy)*/
143 853904 : if ( GT_16( *bits_frame_channel, tmp ) )
144 : {
145 248017 : tmp = *bits_frame_channel;
146 248017 : move16();
147 248017 : max_chn = i;
148 248017 : move16();
149 : }
150 : }
151 : }
152 :
153 : /*if bits distributed are more than available bits, substract the proportional amount of bits from all channels*/
154 191392 : IF( NE_16( bits_split, total_bits ) )
155 : {
156 181429 : diff = sub( bits_split, total_bits );
157 :
158 : /*re-count bits distributed to each channel*/
159 181429 : bits_split = 0;
160 181429 : move16();
161 :
162 1020912 : FOR( i = 0; i < nchan; i++ )
163 : {
164 839483 : IF( enc_dec == ENC )
165 : {
166 439491 : bits_frame_channel = &( (Encoder_State *) sts[i] )->bits_frame_channel;
167 : }
168 : ELSE /* DEC */
169 : {
170 399992 : bits_frame_channel = &( (Decoder_State *) sts[i] )->bits_frame_channel;
171 : }
172 839483 : move16();
173 839483 : IF( NE_32( mct_chan_mode[i], MCT_CHAN_MODE_IGNORE ) )
174 : {
175 : Word32 temp_res;
176 817331 : temp_res = L_mult0( diff, split_ratio[i] );
177 817331 : IF( temp_res < 0 )
178 : {
179 542939 : temp_res = L_negate( temp_res );
180 542939 : temp_res = L_shr( temp_res, NBBITS_MCT_RATIO );
181 542939 : temp_res = L_negate( temp_res );
182 : }
183 : ELSE
184 : {
185 274392 : temp_res = L_shr( temp_res, NBBITS_MCT_RATIO );
186 : }
187 817331 : *bits_frame_channel = sub( *bits_frame_channel, extract_l( temp_res ) );
188 817331 : move16();
189 817331 : *bits_frame_channel = s_max( min_chan_bits[i], *bits_frame_channel );
190 817331 : move16();
191 817331 : bits_split = add( bits_split, *bits_frame_channel );
192 : }
193 : }
194 : }
195 :
196 : /*if there any bits left assign them to the channel with the maximum energy (or more bits)*/
197 191392 : IF( NE_16( total_bits, bits_split ) )
198 : {
199 181419 : IF( enc_dec == ENC )
200 : {
201 94763 : bits_frame_channel = &( (Encoder_State *) sts[max_chn] )->bits_frame_channel;
202 : }
203 : ELSE /* DEC */
204 : {
205 86656 : bits_frame_channel = &( (Decoder_State *) sts[max_chn] )->bits_frame_channel;
206 : }
207 181419 : move16();
208 181419 : *bits_frame_channel = add( *bits_frame_channel, sub( total_bits, bits_split ) );
209 :
210 : /*if all channels are silent assign bits to ch 0*/
211 181419 : IF( enc_dec == ENC )
212 : {
213 94763 : IF( EQ_32( ( (Encoder_State *) sts[max_chn] )->mct_chan_mode, MCT_CHAN_MODE_IGNORE ) )
214 : {
215 147 : assert( bits_split == 0 );
216 :
217 147 : ( (Encoder_State *) sts[max_chn] )->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
218 147 : move32();
219 147 : ( (Encoder_State *) sts[max_chn] )->side_bits_frame_channel = imult1616( ( ( (Encoder_State *) sts[max_chn] )->core ), ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL ) );
220 147 : move16();
221 147 : *bits_frame_channel = sub( *bits_frame_channel, ( (Encoder_State *) sts[max_chn] )->side_bits_frame_channel );
222 147 : move16();
223 : }
224 : }
225 : }
226 :
227 191392 : return;
228 : }
|