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_prot.h"
36 : #include "ivas_cnst.h"
37 : #include "ivas_rom_com.h"
38 : #include "ivas_rom_dec.h"
39 : #include <assert.h>
40 : #include "prot.h"
41 : #include "wmc_auto.h"
42 :
43 :
44 : /*---------------------------------------------------------------
45 : * arith_decode_elias_mod()
46 : *
47 : *
48 : * ---------------------------------------------------------------*/
49 :
50 93192 : static int16_t arith_decode_elias_mod(
51 : RangeUniDecState *rc_st_dec )
52 : {
53 : int16_t n, n_bits, bit;
54 :
55 93192 : n_bits = 0;
56 :
57 93192 : bit = rc_uni_dec_read_bit( rc_st_dec );
58 :
59 214061 : while ( bit == 0 )
60 : {
61 120869 : bit = rc_uni_dec_read_bit( rc_st_dec );
62 120869 : ++n_bits;
63 120869 : if ( n_bits == 17 )
64 : {
65 : /* bitstream error encountered */
66 0 : rc_st_dec->bit_error_detected = 1;
67 0 : return 0;
68 : }
69 : }
70 :
71 93192 : if ( n_bits == 0 )
72 : {
73 : /* code for 0 is 10 and code for 1 is 11 */
74 31879 : n = rc_uni_dec_read_bit( rc_st_dec );
75 : }
76 : else
77 : {
78 61313 : n = rc_uni_dec_read_bits( rc_st_dec, n_bits ) + ( 1 << n_bits );
79 : }
80 :
81 93192 : return n;
82 : }
83 :
84 :
85 : /*---------------------------------------------------------------
86 : * arith_decode_prob_escape()
87 : *
88 : *
89 : * ---------------------------------------------------------------*/
90 :
91 9049176 : static int16_t arith_decode_prob_escape(
92 : RangeUniDecState *rc_st_dec,
93 : const uint16_t cum_freq_table[], /* i : Cumulative frequency up to symbol */
94 : const uint16_t sym_freq_table[], /* i : Symbol frequency */
95 : const int16_t table_size )
96 : {
97 : int16_t symbol;
98 :
99 9049176 : symbol = rc_uni_dec_read_symbol_fastS( rc_st_dec, cum_freq_table, sym_freq_table, table_size, ECSQ_PROB_BITS );
100 :
101 9049176 : if ( symbol == table_size - 1 ) /* escape symbol */
102 : {
103 : /* decode the additional value using a modified Elias integer code */
104 93192 : symbol += arith_decode_elias_mod( rc_st_dec );
105 : }
106 :
107 9049176 : return symbol;
108 : }
109 :
110 :
111 : /*---------------------------------------------------------------
112 : * ECSQ_decode()
113 : *
114 : * decode into output a quantized integer-valued vector, which must be afterwards dequantized;
115 : * if global_gain_index == ECSQ_GLOBAL_GAIN_INDEX_ALL_ZERO, the entire vector is zero and the method should not be called;
116 : * the dequantized vector is obtained using the ECSQ_dequantize_vector method
117 : * ---------------------------------------------------------------*/
118 :
119 275696 : void ECSQ_decode(
120 : ECSQ_instance *ecsq_inst,
121 : const int16_t N,
122 : int16_t *output )
123 : {
124 : int16_t i, idx, segment, segment_count, seg_start, seg_stop;
125 : const uint16_t *tab_vals_cum_freq;
126 : const uint16_t *tab_vals_sym_freq;
127 : const uint16_t *tab_abs_lsbs_cum_freq;
128 : const uint16_t *tab_abs_lsbs_sym_freq;
129 : RangeUniDecState *rc_st_dec;
130 : int16_t param_zb; /* zero-based parameter index for coding */
131 : int16_t shift, lsbs, nonzero, left1, left0, sym, count0;
132 :
133 275696 : rc_st_dec = (RangeUniDecState *) ecsq_inst->ac_handle;
134 :
135 : #ifdef DEBUGGING
136 : assert( N > 0 );
137 : #endif
138 :
139 275696 : segment_count = ( N + ECSQ_SEGMENT_SIZE - 1 ) / ECSQ_SEGMENT_SIZE;
140 :
141 1654176 : for ( segment = 0; segment < segment_count; ++segment )
142 : {
143 1378480 : seg_start = segment * ECSQ_SEGMENT_SIZE;
144 1378480 : seg_stop = min( seg_start + ECSQ_SEGMENT_SIZE, N ) - 1;
145 :
146 1378480 : param_zb = rc_uni_dec_read_symbol_fastS( rc_st_dec, cum_freq_ECSQ_tab_param[ecsq_inst->config_index], sym_freq_ECSQ_tab_param[ecsq_inst->config_index], ECSQ_PARAM_COUNT, ECSQ_PROB_BITS );
147 1378480 : shift = max( 0, param_zb - 3 ); /* first nonzero shift of 1 is used for param 3 */
148 :
149 1378480 : if ( param_zb != 0 ) /* not the ECSQ_ALL_ZERO_PARAM param */
150 : {
151 1131147 : tab_vals_cum_freq = cum_freq_ECSQ_tab_vals[param_zb - 1];
152 1131147 : tab_vals_sym_freq = sym_freq_ECSQ_tab_vals[param_zb - 1];
153 1131147 : idx = min( shift, 4 );
154 1131147 : tab_abs_lsbs_cum_freq = cum_freq_ECSQ_tab_abs_lsbs[idx];
155 1131147 : tab_abs_lsbs_sym_freq = sym_freq_ECSQ_tab_abs_lsbs[idx];
156 :
157 10180323 : for ( i = seg_start; i <= seg_stop; ++i )
158 : {
159 9049176 : sym = arith_decode_prob_escape( rc_st_dec, tab_vals_cum_freq, tab_vals_sym_freq, ECSQ_TAB_VALS_SIZE );
160 :
161 9049176 : if ( shift != 0 )
162 : {
163 622096 : if ( ( sym > 0 ) || ( shift > 4 ) )
164 : {
165 491758 : lsbs = rc_uni_dec_read_bits( rc_st_dec, shift );
166 : }
167 : else /* (sym == 0) && (shift <= 4) */
168 : {
169 130338 : lsbs = rc_uni_dec_read_symbol_fastS( rc_st_dec, tab_abs_lsbs_cum_freq, tab_abs_lsbs_sym_freq, 1 << shift, ECSQ_PROB_BITS );
170 : }
171 622096 : sym = ( sym << shift ) | lsbs;
172 : }
173 :
174 9049176 : if ( sym != 0 )
175 : {
176 6664925 : sym *= 1 - 2 * rc_uni_dec_read_bit( rc_st_dec ); /* map the sign bit to +1 or -1 and then multiply */
177 : }
178 :
179 9049176 : output[i] = sym;
180 : }
181 : }
182 : else
183 : {
184 : #ifdef DEBUGGING
185 : assert( ECSQ_NONZERO_MAX == 3 );
186 : #endif
187 :
188 247333 : nonzero = rc_uni_dec_read_bits( rc_st_dec, 2 );
189 :
190 247333 : left1 = nonzero;
191 247333 : left0 = ( seg_stop - seg_start + 1 ) - nonzero;
192 :
193 2225997 : for ( i = seg_start; i <= seg_stop; ++i )
194 : {
195 1978664 : if ( left1 == 0 )
196 : {
197 1123620 : sym = 0;
198 : }
199 855044 : else if ( left0 == 0 )
200 : {
201 53655 : sym = 1;
202 : }
203 : else
204 : {
205 801389 : count0 = left0 * ECSQ_tab_inverse[left0 + left1]; /* left0 * round(ECSQ_PROB_TOTAL / (left0 + left1)) */
206 801389 : sym = rc_uni_dec_read_bit_prob_fast( rc_st_dec, count0, ECSQ_PROB_BITS );
207 : }
208 :
209 1978664 : if ( sym != 0 )
210 : {
211 307917 : sym *= 1 - 2 * rc_uni_dec_read_bit( rc_st_dec ); /* map the sign bit to +1 or -1 and then multiply */
212 307917 : --left1;
213 : }
214 : else
215 : {
216 1670747 : --left0;
217 : }
218 :
219 1978664 : output[i] = sym;
220 : }
221 : }
222 : }
223 :
224 275696 : return;
225 : }
|