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_rom_com.h"
37 : #include "ivas_rom_dec.h"
38 : #include <assert.h>
39 : #include "prot_fx.h"
40 : #include "wmc_auto.h"
41 : #include "ivas_prot_fx.h"
42 :
43 :
44 : /*---------------------------------------------------------------
45 : * arith_decode_elias_mod()
46 : *
47 : *
48 : * ---------------------------------------------------------------*/
49 :
50 7531 : static Word16 arith_decode_elias_mod(
51 : RangeUniDecState *rc_st_dec )
52 : {
53 : Word16 n, n_bits, bit;
54 :
55 7531 : n_bits = 0;
56 7531 : move16();
57 :
58 7531 : bit = rc_uni_dec_read_bit( rc_st_dec ); /* Q0 */
59 :
60 17806 : WHILE( bit == 0 )
61 : {
62 10275 : bit = rc_uni_dec_read_bit( rc_st_dec ); /* Q0 */
63 10275 : n_bits = add( n_bits, 1 ); /* Q0 */
64 10275 : IF( EQ_16( n_bits, 17 ) )
65 : {
66 : /* bitstream error encountered */
67 0 : rc_st_dec->bit_error_detected = 1; /* Q0 */
68 0 : move16();
69 0 : return 0;
70 : }
71 : }
72 :
73 7531 : IF( n_bits == 0 )
74 : {
75 : /* code for 0 is 10 and code for 1 is 11 */
76 2414 : n = rc_uni_dec_read_bit( rc_st_dec );
77 : }
78 : ELSE
79 : {
80 5117 : n = add( rc_uni_dec_read_bits( rc_st_dec, n_bits ), shl( 1, n_bits ) );
81 : }
82 :
83 7531 : return n;
84 : }
85 :
86 :
87 : /*---------------------------------------------------------------
88 : * arith_decode_prob_escape()
89 : *
90 : *
91 : * ---------------------------------------------------------------*/
92 :
93 543776 : static Word16 arith_decode_prob_escape(
94 : RangeUniDecState *rc_st_dec,
95 : const UWord16 cum_freq_table[], /* i : Cumulative frequency up to symbol Q0*/
96 : const UWord16 sym_freq_table[], /* i : Symbol frequency Q0*/
97 : const Word16 table_size /* Q0 */
98 : )
99 : {
100 : Word16 symbol;
101 :
102 543776 : symbol = rc_uni_dec_read_symbol_fastS_fx( rc_st_dec, cum_freq_table, sym_freq_table, table_size, ECSQ_PROB_BITS ); /* Q0 */
103 :
104 543776 : IF( EQ_16( symbol, sub( table_size, 1 ) ) ) /* escape symbol */
105 : {
106 : /* decode the additional value using a modified Elias integer code */
107 7531 : symbol = add( symbol, arith_decode_elias_mod( rc_st_dec ) ); /* Q0 */
108 : }
109 :
110 543776 : return symbol;
111 : }
112 :
113 :
114 : /*---------------------------------------------------------------
115 : * ECSQ_decode()
116 : *
117 : * decode into output a quantized integer-valued vector, which must be afterwards dequantized;
118 : * if global_gain_index == ECSQ_GLOBAL_GAIN_INDEX_ALL_ZERO, the entire vector is zero and the method should not be called;
119 : * the dequantized vector is obtained using the ECSQ_dequantize_vector method
120 : * ---------------------------------------------------------------*/
121 :
122 16373 : void ECSQ_decode(
123 : ECSQ_instance *ecsq_inst,
124 : const Word16 N, /* Q0 */
125 : Word16 *output /* Q0 */
126 : )
127 : {
128 : Word16 i, idx, segment, segment_count, seg_start, seg_stop;
129 : const UWord16 *tab_vals_cum_freq;
130 : const UWord16 *tab_vals_sym_freq;
131 : const UWord16 *tab_abs_lsbs_cum_freq;
132 : const UWord16 *tab_abs_lsbs_sym_freq;
133 : RangeUniDecState *rc_st_dec;
134 : Word16 param_zb; /* zero-based parameter index for coding */
135 : Word16 shift, lsbs, nonzero, left1, left0, sym, count0;
136 :
137 16373 : rc_st_dec = (RangeUniDecState *) ecsq_inst->ac_handle;
138 :
139 :
140 16373 : segment_count = shr( sub( add( N, ECSQ_SEGMENT_SIZE ), 1 ), 3 ); /* Q0 */
141 :
142 98238 : FOR( segment = 0; segment < segment_count; ++segment )
143 : {
144 81865 : seg_start = shl( segment, 3 ); /* Q0 */
145 81865 : seg_stop = sub( s_min( add( seg_start, ECSQ_SEGMENT_SIZE ), N ), 1 ); /* Q0 */
146 :
147 81865 : param_zb = rc_uni_dec_read_symbol_fastS_fx( 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 ); /* Q0 */
148 81865 : shift = s_max( 0, sub( param_zb, 3 ) ); /* first nonzero shift of 1 is used for param 3 */ /* Q0 */
149 :
150 81865 : IF( param_zb != 0 ) /* not the ECSQ_ALL_ZERO_PARAM param */
151 : {
152 67972 : tab_vals_cum_freq = cum_freq_ECSQ_tab_vals[param_zb - 1]; /* Q0 */
153 67972 : tab_vals_sym_freq = sym_freq_ECSQ_tab_vals[param_zb - 1]; /* Q0 */
154 67972 : idx = s_min( shift, 4 ); /* Q0 */
155 67972 : tab_abs_lsbs_cum_freq = cum_freq_ECSQ_tab_abs_lsbs[idx]; /* Q0 */
156 67972 : tab_abs_lsbs_sym_freq = sym_freq_ECSQ_tab_abs_lsbs[idx]; /* Q0 */
157 :
158 611748 : FOR( i = seg_start; i <= seg_stop; ++i )
159 : {
160 543776 : sym = arith_decode_prob_escape( rc_st_dec, tab_vals_cum_freq, tab_vals_sym_freq, ECSQ_TAB_VALS_SIZE ); /* Q0 */
161 :
162 543776 : IF( shift != 0 )
163 : {
164 57960 : test();
165 57960 : IF( ( sym > 0 ) || ( GT_16( shift, 4 ) ) )
166 : {
167 46980 : lsbs = rc_uni_dec_read_bits( rc_st_dec, shift ); /* Q0 */
168 : }
169 : ELSE /* (sym == 0) && (shift <= 4) */
170 : {
171 10980 : lsbs = rc_uni_dec_read_symbol_fastS_fx( rc_st_dec, tab_abs_lsbs_cum_freq, tab_abs_lsbs_sym_freq, shl( 1, shift ), ECSQ_PROB_BITS ); /* Q0 */
172 : }
173 57960 : sym = s_or( shl( sym, shift ), lsbs ); /* Q0 */
174 : }
175 :
176 543776 : IF( sym != 0 )
177 : {
178 : /* map the sign bit to +1 or -1 and then multiply */
179 405685 : IF( rc_uni_dec_read_bit( rc_st_dec ) )
180 : {
181 203339 : sym = negate( sym ); /* Q0 */
182 : }
183 : }
184 :
185 543776 : output[i] = sym; /* Q0 */
186 543776 : move16();
187 : }
188 : }
189 : ELSE
190 : {
191 :
192 13893 : nonzero = rc_uni_dec_read_bits( rc_st_dec, 2 ); /* Q0 */
193 :
194 13893 : left1 = nonzero; /* Q0 */
195 13893 : move16();
196 13893 : left0 = sub( add( sub( seg_stop, seg_start ), 1 ), nonzero ); /* Q0 */
197 :
198 125037 : FOR( i = seg_start; i <= seg_stop; ++i )
199 : {
200 111144 : IF( left1 == 0 )
201 : {
202 54622 : sym = 0; /* Q0 */
203 54622 : move16();
204 : }
205 56522 : ELSE IF( left0 == 0 )
206 : {
207 3013 : sym = 1; /* Q0 */
208 3013 : move16();
209 : }
210 : ELSE
211 : {
212 53509 : count0 = left0 * ECSQ_tab_inverse[left0 + left1]; /* left0 * round(ECSQ_PROB_TOTAL / (left0 + left1)) Q0*/
213 53509 : sym = rc_uni_dec_read_bit_prob_fast( rc_st_dec, count0, ECSQ_PROB_BITS ); /* Q0 */
214 : }
215 :
216 111144 : IF( sym != 0 )
217 : {
218 : /* map the sign bit to +1 or -1 and then multiply */
219 20619 : IF( rc_uni_dec_read_bit( rc_st_dec ) )
220 : {
221 10473 : sym = negate( sym ); /* Q0 */
222 : }
223 20619 : left1 = sub( left1, 1 ); /* Q0 */
224 : }
225 : ELSE
226 : {
227 90525 : left0 = sub( left0, 1 ); /* Q0 */
228 : }
229 :
230 111144 : output[i] = sym;
231 111144 : move16();
232 : }
233 : }
234 : }
235 :
236 16373 : return;
237 : }
|