Line data Source code
1 : /*====================================================================================
2 : EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
3 : ====================================================================================*/
4 :
5 : #include <stdint.h>
6 : #include "options.h" /* Compilation switches */
7 : #include "cnst.h" /* Common constants */
8 : #include "rom_com.h" /* Static table prototypes */
9 : #include "prot_fx.h" /* Function prototypes */
10 :
11 :
12 : /*-------------------------------------------------------------------*
13 : * Local prototypes
14 : *-------------------------------------------------------------------*/
15 : static Word16 rc_dec_read_fx( Decoder_State *st_fx, PVQ_DEC_HANDLE hPVQ );
16 : /*-------------------------------------------------------------------*
17 : * rc_dec_init()
18 : *
19 : * Initialize range coder
20 : *-------------------------------------------------------------------*/
21 :
22 28288 : void rc_dec_init_fx(
23 : Decoder_State *st_fx, /* i/o: Decoder State */
24 : PVQ_DEC_HANDLE hPVQ, /* i/o: PVQ decoder handle */
25 : Word16 tot_bits /* i : Total bit budget Q0 */
26 : )
27 : {
28 : Word16 i;
29 :
30 28288 : hPVQ->rc_low = L_deposit_l( 0 );
31 28288 : move32();
32 28288 : hPVQ->rc_range = 0xffffffff;
33 28288 : move32();
34 28288 : hPVQ->rc_num_bits = 0;
35 28288 : move16();
36 28288 : hPVQ->rc_offset = add( tot_bits, st_fx->next_bit_pos );
37 28288 : move16();
38 28288 : hPVQ->rc_end = hPVQ->rc_offset;
39 28288 : move16();
40 :
41 141440 : FOR( i = 0; i < 4; i++ )
42 : {
43 113152 : hPVQ->rc_low = UL_addNsD( UL_lshl( hPVQ->rc_low, 8 ), UL_deposit_l( rc_dec_read_fx( st_fx, hPVQ ) ) );
44 113152 : move32();
45 : }
46 28288 : }
47 :
48 : /*-------------------------------------------------------------------*
49 : * rc_decode()
50 : *
51 : * Decode symbol
52 : *-------------------------------------------------------------------*/
53 :
54 303769 : UWord32 rc_decode_fx( /* o : Decoded cumulative frequency */
55 : Word16 *BER_detect, /* o : Bit error detection flag */
56 : PVQ_DEC_HANDLE hPVQ, /* i/o: PVQ decoder handle */
57 : UWord32 tot /* i : Total cumulative frequency Q0 */
58 : )
59 : {
60 : UWord32 inv, lsb, val, UL_tmp1, UL_tmp2;
61 : Word16 exp_num, exp_den, exp;
62 : UWord16 sgn;
63 :
64 303769 : inv = UL_inverse( tot, &exp );
65 303769 : Mpy_32_32_uu( hPVQ->rc_range, inv, &( hPVQ->rc_help ), &lsb ); /*0+exp-32*/
66 303769 : hPVQ->rc_help = UL_lshr( hPVQ->rc_help, sub( exp, 32 ) );
67 303769 : move32();
68 :
69 303769 : exp_den = norm_ul( hPVQ->rc_help );
70 303769 : UL_tmp2 = UL_lshl( hPVQ->rc_help, exp_den );
71 303769 : exp_num = sub( norm_ul( hPVQ->rc_low ), 1 );
72 303769 : UL_tmp1 = UL_lshl( hPVQ->rc_low, exp_num );
73 303769 : exp = add( 32, sub( exp_num, exp_den ) );
74 :
75 303769 : val = UL_div( UL_tmp1, UL_tmp2 );
76 303769 : val = UL_lshr( val, exp );
77 :
78 303769 : UL_tmp1 = UL_Mpy_32_32( val, hPVQ->rc_help );
79 303769 : UL_tmp2 = UL_Mpy_32_32( UL_addNsD( val, 1 ), hPVQ->rc_help );
80 303769 : UL_tmp1 = UL_subNsD( hPVQ->rc_low, UL_tmp1 );
81 303769 : UL_tmp2 = UL_subNsD( hPVQ->rc_low, UL_tmp2 );
82 303769 : IF( LT_64( UL_tmp2, UL_tmp1 ) )
83 : {
84 41 : val = UL_addNsD( val, 1 );
85 : }
86 :
87 : /* safety check in case of bit errors */
88 303769 : UL_tmp1 = UL_subNs( tot, val, &sgn );
89 303769 : IF( sgn != 0 )
90 : {
91 0 : *BER_detect = 1;
92 0 : move16();
93 0 : return 0;
94 : }
95 :
96 303769 : return val;
97 : }
98 :
99 : /*-------------------------------------------------------------------*
100 : * rc_dec_update()
101 : *
102 : * Update range coder
103 : *-------------------------------------------------------------------*/
104 :
105 303769 : void rc_dec_update_fx(
106 : Decoder_State *st_fx, /* i/o: Decoder State */
107 : PVQ_DEC_HANDLE hPVQ, /* i/o: PVQ decoder handle */
108 : UWord32 cum_freq, /* i : Cumulative frequency */
109 : UWord32 sym_freq /* i : Symbol frequency */
110 : )
111 : {
112 303769 : hPVQ->rc_low = UL_subNsD( hPVQ->rc_low, UL_Mpy_32_32( cum_freq, hPVQ->rc_help ) ); /*0+0*/
113 303769 : hPVQ->rc_range = UL_Mpy_32_32( hPVQ->rc_help, sym_freq );
114 303769 : move32();
115 303769 : move32();
116 :
117 550098 : WHILE( LT_64( hPVQ->rc_range, 1 << 24 ) )
118 : {
119 246329 : L_sub( 0, 0 ); /* For comparision in while*/
120 246329 : hPVQ->rc_num_bits = add( hPVQ->rc_num_bits, 8 );
121 246329 : hPVQ->rc_low = UL_addNsD( UL_lshl( hPVQ->rc_low, 8 ), UL_deposit_l( rc_dec_read_fx( st_fx, hPVQ ) ) );
122 246329 : hPVQ->rc_range = UL_lshl( hPVQ->rc_range, 8 );
123 246329 : move16();
124 246329 : move32();
125 246329 : move32();
126 : }
127 303769 : }
128 :
129 : /*-------------------------------------------------------------------*
130 : * rc_dec_bits()
131 : *
132 : * Encode bits
133 : *-------------------------------------------------------------------*/
134 :
135 482833 : Word32 rc_dec_bits_fx( /* i : Decoded value */
136 : Decoder_State *st_fx, /* i/o: Decoder State */
137 : PVQ_DEC_HANDLE hPVQ, /* i/o: PVQ decoder handle */
138 : Word16 bits /* i : Number of bits */
139 : )
140 : {
141 : Word32 value;
142 :
143 482833 : hPVQ->rc_num_bits = add( hPVQ->rc_num_bits, bits );
144 482833 : move16();
145 :
146 482833 : IF( GT_16( bits, 16 ) )
147 : {
148 43695 : hPVQ->rc_offset = sub( hPVQ->rc_offset, sub( bits, 16 ) );
149 43695 : move16();
150 43695 : value = UL_lshl( UL_deposit_l( get_indice( st_fx, hPVQ->rc_offset, sub( bits, 16 ) ) ), 16 );
151 43695 : hPVQ->rc_offset = sub( hPVQ->rc_offset, 16 );
152 43695 : move16();
153 43695 : value = UL_or( value, UL_deposit_l( get_indice( st_fx, hPVQ->rc_offset, 16 ) ) );
154 : }
155 : ELSE
156 : {
157 439138 : hPVQ->rc_offset = sub( hPVQ->rc_offset, bits );
158 439138 : move16();
159 439138 : value = UL_deposit_l( get_indice( st_fx, hPVQ->rc_offset, bits ) );
160 : }
161 :
162 482833 : return value;
163 : }
164 :
165 : /*-------------------------------------------------------------------*
166 : * rc_dec_uniform()
167 : *
168 : * Encode with uniform distribution
169 : *-------------------------------------------------------------------*/
170 :
171 273641 : UWord32 rc_dec_uniform_fx( /* i : Decoded value */
172 : Decoder_State *st_fx, /* i/o: Decoder State */
173 : PVQ_DEC_HANDLE hPVQ, /* i/o: PVQ decoder handle */
174 : UWord32 tot /* i : Maximum value */
175 : )
176 : {
177 : UWord32 value;
178 : Word16 n;
179 273641 : n = sub( 32, norm_ul( UL_subNsD( tot, 1 ) ) );
180 :
181 273641 : IF( LE_16( n, 8 ) )
182 : {
183 64561 : value = rc_decode_fx( &st_fx->BER_detect, hPVQ, tot );
184 64561 : rc_dec_update_fx( st_fx, hPVQ, value, 1 );
185 : }
186 : ELSE
187 : {
188 209080 : n = sub( n, 8 );
189 209080 : value = rc_decode_fx( &st_fx->BER_detect, hPVQ, UL_addNsD( UL_lshr( tot, n ), 1 ) );
190 209080 : rc_dec_update_fx( st_fx, hPVQ, value, 1 );
191 209080 : value = UL_lshl( value, n );
192 209080 : value = UL_or( value, rc_dec_bits_fx( st_fx, hPVQ, n ) );
193 : }
194 :
195 273641 : return value;
196 : }
197 :
198 : /*-------------------------------------------------------------------*
199 : * rc_dec_finish()
200 : *
201 : * Finalize range decoder
202 : *-------------------------------------------------------------------*/
203 :
204 28288 : void rc_dec_finish_fx(
205 : Decoder_State *st_fx, /* i/o: Decoder state */
206 : PVQ_DEC_HANDLE hPVQ /* i/o: PVQ decoder handle */
207 : )
208 : {
209 28288 : st_fx->next_bit_pos = hPVQ->rc_end;
210 28288 : move16();
211 28288 : }
212 :
213 : /*-------------------------------------------------------------------*
214 : * rc_dec_read()
215 : *
216 : * Read a byte from bit stream
217 : *-------------------------------------------------------------------*/
218 :
219 :
220 359481 : static Word16 rc_dec_read_fx(
221 : Decoder_State *st_fx, /* i/o: Decoder state */
222 : PVQ_DEC_HANDLE hPVQ /* i/o: PVQ decoder handle */
223 : )
224 : {
225 : Word16 bits;
226 :
227 359481 : bits = sub( hPVQ->rc_end, st_fx->next_bit_pos );
228 :
229 : /* If the end of the buffer has been reached, pad the last byte with zeros */
230 359481 : IF( sub( bits, 8 ) < 0 )
231 : {
232 34333 : return shl( get_next_indice_fx( st_fx, bits ), sub( 8, bits ) );
233 : }
234 : ELSE
235 : {
236 325148 : return get_next_indice_fx( st_fx, 8 );
237 : }
238 : }
|