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 22599 : static int16_t arith_decode_elias_mod(
51 : RangeUniDecState *rc_st_dec )
52 : {
53 : int16_t n, n_bits, bit;
54 :
55 22599 : n_bits = 0;
56 :
57 22599 : bit = rc_uni_dec_read_bit( rc_st_dec );
58 :
59 53508 : while ( bit == 0 )
60 : {
61 30909 : bit = rc_uni_dec_read_bit( rc_st_dec );
62 30909 : ++n_bits;
63 30909 : 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 22599 : if ( n_bits == 0 )
72 : {
73 : /* code for 0 is 10 and code for 1 is 11 */
74 7233 : n = rc_uni_dec_read_bit( rc_st_dec );
75 : }
76 : else
77 : {
78 15366 : n = rc_uni_dec_read_bits( rc_st_dec, n_bits ) + ( 1 << n_bits );
79 : }
80 :
81 22599 : return n;
82 : }
83 :
84 :
85 : /*---------------------------------------------------------------
86 : * arith_decode_prob_escape()
87 : *
88 : *
89 : * ---------------------------------------------------------------*/
90 :
91 1621272 : 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 1621272 : symbol = rc_uni_dec_read_symbol_fastS( rc_st_dec, cum_freq_table, sym_freq_table, table_size, ECSQ_PROB_BITS );
100 :
101 1621272 : if ( symbol == table_size - 1 ) /* escape symbol */
102 : {
103 : /* decode the additional value using a modified Elias integer code */
104 22599 : symbol += arith_decode_elias_mod( rc_st_dec );
105 : }
106 :
107 1621272 : 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 48828 : 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 48828 : rc_st_dec = (RangeUniDecState *) ecsq_inst->ac_handle;
134 :
135 : #ifdef DEBUGGING
136 : assert( N > 0 );
137 : #endif
138 :
139 48828 : segment_count = ( N + ECSQ_SEGMENT_SIZE - 1 ) / ECSQ_SEGMENT_SIZE;
140 :
141 292968 : for ( segment = 0; segment < segment_count; ++segment )
142 : {
143 244140 : seg_start = segment * ECSQ_SEGMENT_SIZE;
144 244140 : seg_stop = min( seg_start + ECSQ_SEGMENT_SIZE, N ) - 1;
145 :
146 244140 : 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 244140 : shift = max( 0, param_zb - 3 ); /* first nonzero shift of 1 is used for param 3 */
148 :
149 244140 : if ( param_zb != 0 ) /* not the ECSQ_ALL_ZERO_PARAM param */
150 : {
151 202659 : tab_vals_cum_freq = cum_freq_ECSQ_tab_vals[param_zb - 1];
152 202659 : tab_vals_sym_freq = sym_freq_ECSQ_tab_vals[param_zb - 1];
153 202659 : idx = min( shift, 4 );
154 202659 : tab_abs_lsbs_cum_freq = cum_freq_ECSQ_tab_abs_lsbs[idx];
155 202659 : tab_abs_lsbs_sym_freq = sym_freq_ECSQ_tab_abs_lsbs[idx];
156 :
157 1823931 : for ( i = seg_start; i <= seg_stop; ++i )
158 : {
159 1621272 : sym = arith_decode_prob_escape( rc_st_dec, tab_vals_cum_freq, tab_vals_sym_freq, ECSQ_TAB_VALS_SIZE );
160 :
161 1621272 : if ( shift != 0 )
162 : {
163 172584 : if ( ( sym > 0 ) || ( shift > 4 ) )
164 : {
165 139680 : lsbs = rc_uni_dec_read_bits( rc_st_dec, shift );
166 : }
167 : else /* (sym == 0) && (shift <= 4) */
168 : {
169 32904 : 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 172584 : sym = ( sym << shift ) | lsbs;
172 : }
173 :
174 1621272 : if ( sym != 0 )
175 : {
176 1208715 : 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 1621272 : output[i] = sym;
180 : }
181 : }
182 : else
183 : {
184 : #ifdef DEBUGGING
185 : assert( ECSQ_NONZERO_MAX == 3 );
186 : #endif
187 :
188 41481 : nonzero = rc_uni_dec_read_bits( rc_st_dec, 2 );
189 :
190 41481 : left1 = nonzero;
191 41481 : left0 = ( seg_stop - seg_start + 1 ) - nonzero;
192 :
193 373329 : for ( i = seg_start; i <= seg_stop; ++i )
194 : {
195 331848 : if ( left1 == 0 )
196 : {
197 163860 : sym = 0;
198 : }
199 167988 : else if ( left0 == 0 )
200 : {
201 8994 : sym = 1;
202 : }
203 : else
204 : {
205 158994 : count0 = left0 * ECSQ_tab_inverse[left0 + left1]; /* left0 * round(ECSQ_PROB_TOTAL / (left0 + left1)) */
206 158994 : sym = rc_uni_dec_read_bit_prob_fast( rc_st_dec, count0, ECSQ_PROB_BITS );
207 : }
208 :
209 331848 : if ( sym != 0 )
210 : {
211 61290 : sym *= 1 - 2 * rc_uni_dec_read_bit( rc_st_dec ); /* map the sign bit to +1 or -1 and then multiply */
212 61290 : --left1;
213 : }
214 : else
215 : {
216 270558 : --left0;
217 : }
218 :
219 331848 : output[i] = sym;
220 : }
221 : }
222 : }
223 :
224 48828 : return;
225 : }
|