Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (c) 2007-2008 CSIRO |
2 | | Copyright (c) 2007-2009 Xiph.Org Foundation |
3 | | Copyright (c) 2008-2009 Gregory Maxwell |
4 | | Written by Jean-Marc Valin and Gregory Maxwell */ |
5 | | /* |
6 | | Redistribution and use in source and binary forms, with or without |
7 | | modification, are permitted provided that the following conditions |
8 | | are met: |
9 | | |
10 | | - Redistributions of source code must retain the above copyright |
11 | | notice, this list of conditions and the following disclaimer. |
12 | | |
13 | | - Redistributions in binary form must reproduce the above copyright |
14 | | notice, this list of conditions and the following disclaimer in the |
15 | | documentation and/or other materials provided with the distribution. |
16 | | |
17 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
21 | | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | */ |
29 | | |
30 | | #ifdef HAVE_CONFIG_H |
31 | | #include "config.h" |
32 | | #endif |
33 | | |
34 | | #include <math.h> |
35 | | #include "bands.h" |
36 | | #include "modes.h" |
37 | | #include "vq.h" |
38 | | #include "cwrs.h" |
39 | | #include "stack_alloc.h" |
40 | | #include "os_support.h" |
41 | | #include "mathops.h" |
42 | | #include "rate.h" |
43 | | #include "quant_bands.h" |
44 | | #include "pitch.h" |
45 | | |
46 | | #ifndef M_PI |
47 | | #define M_PI 3.141592653 |
48 | | #endif |
49 | | |
50 | | int hysteresis_decision(opus_val16 val, const opus_val16 *thresholds, const opus_val16 *hysteresis, int N, int prev) |
51 | 0 | { |
52 | 0 | int i; |
53 | 0 | for (i=0;i<N;i++) |
54 | 0 | { |
55 | 0 | if (val < thresholds[i]) |
56 | 0 | break; |
57 | 0 | } |
58 | 0 | if (i>prev && val < thresholds[prev]+hysteresis[prev]) |
59 | 0 | i=prev; |
60 | 0 | if (i<prev && val > thresholds[prev-1]-hysteresis[prev-1]) |
61 | 0 | i=prev; |
62 | 0 | return i; |
63 | 0 | } |
64 | | |
65 | | opus_uint32 celt_lcg_rand(opus_uint32 seed) |
66 | 0 | { |
67 | 0 | return 1664525 * seed + 1013904223; |
68 | 0 | } |
69 | | |
70 | | /* This is a cos() approximation designed to be bit-exact on any platform. Bit exactness |
71 | | with this approximation is important because it has an impact on the bit allocation */ |
72 | | opus_int16 bitexact_cos(opus_int16 x) |
73 | 0 | { |
74 | 0 | opus_int32 tmp; |
75 | 0 | opus_int16 x2; |
76 | 0 | tmp = (4096+((opus_int32)(x)*(x)))>>13; |
77 | 0 | celt_sig_assert(tmp<=32767); |
78 | 0 | x2 = tmp; |
79 | 0 | x2 = (32767-x2) + FRAC_MUL16(x2, (-7651 + FRAC_MUL16(x2, (8277 + FRAC_MUL16(-626, x2))))); |
80 | 0 | celt_sig_assert(x2<=32766); |
81 | 0 | return 1+x2; |
82 | 0 | } |
83 | | |
84 | | int bitexact_log2tan(int isin,int icos) |
85 | 0 | { |
86 | 0 | int lc; |
87 | 0 | int ls; |
88 | 0 | lc=EC_ILOG(icos); |
89 | 0 | ls=EC_ILOG(isin); |
90 | 0 | icos<<=15-lc; |
91 | 0 | isin<<=15-ls; |
92 | 0 | return (ls-lc)*(1<<11) |
93 | 0 | +FRAC_MUL16(isin, FRAC_MUL16(isin, -2597) + 7932) |
94 | 0 | -FRAC_MUL16(icos, FRAC_MUL16(icos, -2597) + 7932); |
95 | 0 | } |
96 | | |
97 | | #ifdef FIXED_POINT |
98 | | /* Compute the amplitude (sqrt energy) in each of the bands */ |
99 | | void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bandE, int end, int C, int LM, int arch) |
100 | | { |
101 | | int i, c, N; |
102 | | const opus_int16 *eBands = m->eBands; |
103 | | (void)arch; |
104 | | N = m->shortMdctSize<<LM; |
105 | | c=0; do { |
106 | | for (i=0;i<end;i++) |
107 | | { |
108 | | int j; |
109 | | opus_val32 maxval=0; |
110 | | opus_val32 sum = 0; |
111 | | |
112 | | maxval = celt_maxabs32(&X[c*N+(eBands[i]<<LM)], (eBands[i+1]-eBands[i])<<LM); |
113 | | if (maxval > 0) |
114 | | { |
115 | | int shift = IMAX(0, 30 - celt_ilog2(maxval+(maxval>>14)+1) - ((((m->logN[i]+7)>>BITRES)+LM+1)>>1)); |
116 | | j=eBands[i]<<LM; do { |
117 | | opus_val32 x = SHL32(X[j+c*N],shift); |
118 | | sum = ADD32(sum, MULT32_32_Q31(x, x)); |
119 | | } while (++j<eBands[i+1]<<LM); |
120 | | bandE[i+c*m->nbEBands] = MAX32(maxval, PSHR32(celt_sqrt32(SHR32(sum,1)), shift)); |
121 | | } else { |
122 | | bandE[i+c*m->nbEBands] = EPSILON; |
123 | | } |
124 | | } |
125 | | } while (++c<C); |
126 | | } |
127 | | |
128 | | /* Normalise each band such that the energy is one. */ |
129 | | void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, celt_norm * OPUS_RESTRICT X, const celt_ener *bandE, int end, int C, int M) |
130 | | { |
131 | | int i, c, N; |
132 | | const opus_int16 *eBands = m->eBands; |
133 | | N = M*m->shortMdctSize; |
134 | | c=0; do { |
135 | | i=0; do { |
136 | | int j,shift; |
137 | | opus_val32 E; |
138 | | opus_val32 g; |
139 | | E = bandE[i+c*m->nbEBands]; |
140 | | /* For very low energies, we need this to make sure not to prevent energy rounding from |
141 | | blowing up the normalized signal. */ |
142 | | if (E < 10) E += EPSILON; |
143 | | shift = 30-celt_zlog2(E); |
144 | | E = SHL32(E, shift); |
145 | | g = celt_rcp_norm32(E); |
146 | | j=M*eBands[i]; do { |
147 | | X[j+c*N] = PSHR32(MULT32_32_Q31(g, SHL32(freq[j+c*N], shift)), 30-NORM_SHIFT); |
148 | | } while (++j<M*eBands[i+1]); |
149 | | } while (++i<end); |
150 | | } while (++c<C); |
151 | | } |
152 | | |
153 | | #else /* FIXED_POINT */ |
154 | | /* Compute the amplitude (sqrt energy) in each of the bands */ |
155 | | void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bandE, int end, int C, int LM, int arch) |
156 | 0 | { |
157 | 0 | int i, c, N; |
158 | 0 | const opus_int16 *eBands = m->eBands; |
159 | 0 | N = m->shortMdctSize<<LM; |
160 | 0 | c=0; do { |
161 | 0 | for (i=0;i<end;i++) |
162 | 0 | { |
163 | 0 | opus_val32 sum; |
164 | 0 | sum = 1e-27f + celt_inner_prod(&X[c*N+(eBands[i]<<LM)], &X[c*N+(eBands[i]<<LM)], (eBands[i+1]-eBands[i])<<LM, arch); |
165 | 0 | bandE[i+c*m->nbEBands] = celt_sqrt(sum); |
166 | | /*printf ("%f ", bandE[i+c*m->nbEBands]);*/ |
167 | 0 | } |
168 | 0 | } while (++c<C); |
169 | | /*printf ("\n");*/ |
170 | 0 | } |
171 | | |
172 | | /* Normalise each band such that the energy is one. */ |
173 | | void normalise_bands(const CELTMode *m, const celt_sig * OPUS_RESTRICT freq, celt_norm * OPUS_RESTRICT X, const celt_ener *bandE, int end, int C, int M) |
174 | 0 | { |
175 | 0 | int i, c, N; |
176 | 0 | const opus_int16 *eBands = m->eBands; |
177 | 0 | N = M*m->shortMdctSize; |
178 | 0 | c=0; do { |
179 | 0 | for (i=0;i<end;i++) |
180 | 0 | { |
181 | 0 | int j; |
182 | 0 | opus_val16 g = 1.f/(1e-27f+bandE[i+c*m->nbEBands]); |
183 | 0 | for (j=M*eBands[i];j<M*eBands[i+1];j++) |
184 | 0 | X[j+c*N] = freq[j+c*N]*g; |
185 | 0 | } |
186 | 0 | } while (++c<C); |
187 | 0 | } |
188 | | |
189 | | #endif /* FIXED_POINT */ |
190 | | |
191 | | /* De-normalise the energy to produce the synthesis from the unit-energy bands */ |
192 | | void denormalise_bands(const CELTMode *m, const celt_norm * OPUS_RESTRICT X, |
193 | | celt_sig * OPUS_RESTRICT freq, const celt_glog *bandLogE, int start, |
194 | | int end, int M, int downsample, int silence) |
195 | 0 | { |
196 | 0 | int i, N; |
197 | 0 | int bound; |
198 | 0 | celt_sig * OPUS_RESTRICT f; |
199 | 0 | const celt_norm * OPUS_RESTRICT x; |
200 | 0 | const opus_int16 *eBands = m->eBands; |
201 | 0 | N = M*m->shortMdctSize; |
202 | 0 | bound = M*eBands[end]; |
203 | 0 | if (downsample!=1) |
204 | 0 | bound = IMIN(bound, N/downsample); |
205 | 0 | if (silence) |
206 | 0 | { |
207 | 0 | bound = 0; |
208 | 0 | start = end = 0; |
209 | 0 | } |
210 | 0 | f = freq; |
211 | 0 | x = X+M*eBands[start]; |
212 | 0 | if (start != 0) |
213 | 0 | { |
214 | 0 | for (i=0;i<M*eBands[start];i++) |
215 | 0 | *f++ = 0; |
216 | 0 | } else { |
217 | 0 | f += M*eBands[start]; |
218 | 0 | } |
219 | 0 | for (i=start;i<end;i++) |
220 | 0 | { |
221 | 0 | int j, band_end; |
222 | 0 | opus_val32 g; |
223 | 0 | celt_glog lg; |
224 | | #ifdef FIXED_POINT |
225 | | int shift; |
226 | | #endif |
227 | 0 | j=M*eBands[i]; |
228 | 0 | band_end = M*eBands[i+1]; |
229 | 0 | lg = ADD32(bandLogE[i], SHL32((opus_val32)eMeans[i],DB_SHIFT-4)); |
230 | 0 | #ifndef FIXED_POINT |
231 | 0 | g = celt_exp2_db(MIN32(32.f, lg)); |
232 | | #else |
233 | | /* Handle the integer part of the log energy */ |
234 | | shift = 17-(lg>>DB_SHIFT); |
235 | | if (shift>=31) |
236 | | { |
237 | | shift=0; |
238 | | g=0; |
239 | | } else { |
240 | | /* Handle the fractional part. */ |
241 | | g = SHL32(celt_exp2_db_frac((lg&((1<<DB_SHIFT)-1))), 2); |
242 | | } |
243 | | /* Handle extreme gains with negative shift. */ |
244 | | if (shift<0) |
245 | | { |
246 | | /* To avoid overflow, we're |
247 | | capping the gain here, which is equivalent to a cap of 18 on lg. |
248 | | This shouldn't trigger unless the bitstream is already corrupted. */ |
249 | | g = 2147483647; |
250 | | shift = 0; |
251 | | } |
252 | | #endif |
253 | 0 | do { |
254 | 0 | *f++ = PSHR32(MULT32_32_Q31(SHL32(*x, 30-NORM_SHIFT), g), shift); |
255 | 0 | x++; |
256 | 0 | } while (++j<band_end); |
257 | 0 | } |
258 | 0 | celt_assert(start <= end); |
259 | 0 | OPUS_CLEAR(&freq[bound], N-bound); |
260 | 0 | } |
261 | | |
262 | | /* This prevents energy collapse for transients with multiple short MDCTs */ |
263 | | void anti_collapse(const CELTMode *m, celt_norm *X_, unsigned char *collapse_masks, int LM, int C, int size, |
264 | | int start, int end, const celt_glog *logE, const celt_glog *prev1logE, |
265 | | const celt_glog *prev2logE, const int *pulses, opus_uint32 seed, int encode, int arch) |
266 | 0 | { |
267 | 0 | int c, i, j, k; |
268 | 0 | for (i=start;i<end;i++) |
269 | 0 | { |
270 | 0 | int N0; |
271 | 0 | opus_val16 thresh, sqrt_1; |
272 | 0 | int depth; |
273 | | #ifdef FIXED_POINT |
274 | | int shift; |
275 | | opus_val32 thresh32; |
276 | | #endif |
277 | |
|
278 | 0 | N0 = m->eBands[i+1]-m->eBands[i]; |
279 | | /* depth in 1/8 bits */ |
280 | 0 | celt_sig_assert(pulses[i]>=0); |
281 | 0 | depth = celt_udiv(1+pulses[i], (m->eBands[i+1]-m->eBands[i]))>>LM; |
282 | |
|
283 | | #ifdef FIXED_POINT |
284 | | thresh32 = SHR32(celt_exp2(-SHL16(depth, 10-BITRES)),1); |
285 | | thresh = MULT16_32_Q15(QCONST16(0.5f, 15), MIN32(32767,thresh32)); |
286 | | { |
287 | | opus_val32 t; |
288 | | t = N0<<LM; |
289 | | shift = celt_ilog2(t)>>1; |
290 | | t = SHL32(t, (7-shift)<<1); |
291 | | sqrt_1 = celt_rsqrt_norm(t); |
292 | | } |
293 | | #else |
294 | 0 | thresh = .5f*celt_exp2(-.125f*depth); |
295 | 0 | sqrt_1 = celt_rsqrt(N0<<LM); |
296 | 0 | #endif |
297 | |
|
298 | 0 | c=0; do |
299 | 0 | { |
300 | 0 | celt_norm *X; |
301 | 0 | celt_glog prev1; |
302 | 0 | celt_glog prev2; |
303 | 0 | opus_val32 Ediff; |
304 | 0 | celt_norm r; |
305 | 0 | int renormalize=0; |
306 | 0 | prev1 = prev1logE[c*m->nbEBands+i]; |
307 | 0 | prev2 = prev2logE[c*m->nbEBands+i]; |
308 | 0 | if (!encode && C==1) |
309 | 0 | { |
310 | 0 | prev1 = MAXG(prev1,prev1logE[m->nbEBands+i]); |
311 | 0 | prev2 = MAXG(prev2,prev2logE[m->nbEBands+i]); |
312 | 0 | } |
313 | 0 | Ediff = logE[c*m->nbEBands+i]-MING(prev1,prev2); |
314 | 0 | Ediff = MAX32(0, Ediff); |
315 | |
|
316 | | #ifdef FIXED_POINT |
317 | | if (Ediff < GCONST(16.f)) |
318 | | { |
319 | | opus_val32 r32 = SHR32(celt_exp2_db(-Ediff),1); |
320 | | r = 2*MIN16(16383,r32); |
321 | | } else { |
322 | | r = 0; |
323 | | } |
324 | | if (LM==3) |
325 | | r = MULT16_16_Q14(23170, MIN32(23169, r)); |
326 | | r = SHR16(MIN16(thresh, r),1); |
327 | | r = VSHR32(MULT16_16_Q15(sqrt_1, r),shift+14-NORM_SHIFT); |
328 | | #else |
329 | | /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because |
330 | | short blocks don't have the same energy as long */ |
331 | 0 | r = 2.f*celt_exp2_db(-Ediff); |
332 | 0 | if (LM==3) |
333 | 0 | r *= 1.41421356f; |
334 | 0 | r = MIN16(thresh, r); |
335 | 0 | r = r*sqrt_1; |
336 | 0 | #endif |
337 | 0 | X = X_+c*size+(m->eBands[i]<<LM); |
338 | 0 | for (k=0;k<1<<LM;k++) |
339 | 0 | { |
340 | | /* Detect collapse */ |
341 | 0 | if (!(collapse_masks[i*C+c]&1<<k)) |
342 | 0 | { |
343 | | /* Fill with noise */ |
344 | 0 | for (j=0;j<N0;j++) |
345 | 0 | { |
346 | 0 | seed = celt_lcg_rand(seed); |
347 | 0 | X[(j<<LM)+k] = (seed&0x8000 ? r : -r); |
348 | 0 | } |
349 | 0 | renormalize = 1; |
350 | 0 | } |
351 | 0 | } |
352 | | /* We just added some energy, so we need to renormalise */ |
353 | 0 | if (renormalize) |
354 | 0 | renormalise_vector(X, N0<<LM, Q31ONE, arch); |
355 | 0 | } while (++c<C); |
356 | 0 | } |
357 | 0 | } |
358 | | |
359 | | /* Compute the weights to use for optimizing normalized distortion across |
360 | | channels. We use the amplitude to weight square distortion, which means |
361 | | that we use the square root of the value we would have been using if we |
362 | | wanted to minimize the MSE in the non-normalized domain. This roughly |
363 | | corresponds to some quick-and-dirty perceptual experiments I ran to |
364 | | measure inter-aural masking (there doesn't seem to be any published data |
365 | | on the topic). */ |
366 | | static void compute_channel_weights(celt_ener Ex, celt_ener Ey, opus_val16 w[2]) |
367 | 0 | { |
368 | 0 | celt_ener minE; |
369 | | #ifdef FIXED_POINT |
370 | | int shift; |
371 | | #endif |
372 | 0 | minE = MIN32(Ex, Ey); |
373 | | /* Adjustment to make the weights a bit more conservative. */ |
374 | 0 | Ex = ADD32(Ex, minE/3); |
375 | 0 | Ey = ADD32(Ey, minE/3); |
376 | | #ifdef FIXED_POINT |
377 | | shift = celt_ilog2(EPSILON+MAX32(Ex, Ey))-14; |
378 | | #endif |
379 | 0 | w[0] = VSHR32(Ex, shift); |
380 | 0 | w[1] = VSHR32(Ey, shift); |
381 | 0 | } |
382 | | |
383 | | static void intensity_stereo(const CELTMode *m, celt_norm * OPUS_RESTRICT X, const celt_norm * OPUS_RESTRICT Y, const celt_ener *bandE, int bandID, int N) |
384 | 0 | { |
385 | 0 | int i = bandID; |
386 | 0 | int j; |
387 | 0 | opus_val16 a1, a2; |
388 | 0 | opus_val16 left, right; |
389 | 0 | opus_val16 norm; |
390 | | #ifdef FIXED_POINT |
391 | | int shift = celt_zlog2(MAX32(bandE[i], bandE[i+m->nbEBands]))-13; |
392 | | #endif |
393 | 0 | left = VSHR32(bandE[i],shift); |
394 | 0 | right = VSHR32(bandE[i+m->nbEBands],shift); |
395 | 0 | norm = EPSILON + celt_sqrt(EPSILON+MULT16_16(left,left)+MULT16_16(right,right)); |
396 | | #ifdef FIXED_POINT |
397 | | left = MIN32(left, norm-1); |
398 | | right = MIN32(right, norm-1); |
399 | | #endif |
400 | 0 | a1 = DIV32_16(SHL32(EXTEND32(left),15),norm); |
401 | 0 | a2 = DIV32_16(SHL32(EXTEND32(right),15),norm); |
402 | 0 | for (j=0;j<N;j++) |
403 | 0 | { |
404 | 0 | X[j] = ADD32(MULT16_32_Q15(a1, X[j]), MULT16_32_Q15(a2, Y[j])); |
405 | | /* Side is not encoded, no need to calculate */ |
406 | 0 | } |
407 | 0 | } |
408 | | |
409 | | static void stereo_split(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT Y, int N) |
410 | 0 | { |
411 | 0 | int j; |
412 | 0 | for (j=0;j<N;j++) |
413 | 0 | { |
414 | 0 | opus_val32 r, l; |
415 | 0 | l = MULT32_32_Q31(QCONST32(.70710678f,31), X[j]); |
416 | 0 | r = MULT32_32_Q31(QCONST32(.70710678f,31), Y[j]); |
417 | 0 | X[j] = ADD32(l, r); |
418 | 0 | Y[j] = SUB32(r, l); |
419 | 0 | } |
420 | 0 | } |
421 | | |
422 | | static void stereo_merge(celt_norm * OPUS_RESTRICT X, celt_norm * OPUS_RESTRICT Y, opus_val32 mid, int N, int arch) |
423 | 0 | { |
424 | 0 | int j; |
425 | 0 | opus_val32 xp=0, side=0; |
426 | 0 | opus_val32 El, Er; |
427 | | #ifdef FIXED_POINT |
428 | | int kl, kr; |
429 | | #endif |
430 | 0 | opus_val32 t, lgain, rgain; |
431 | | |
432 | | /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */ |
433 | 0 | xp = celt_inner_prod_norm_shift(Y, X, N, arch); |
434 | 0 | side = celt_inner_prod_norm_shift(Y, Y, N, arch); |
435 | | /* Compensating for the mid normalization */ |
436 | 0 | xp = MULT32_32_Q31(mid, xp); |
437 | | /* mid and side are in Q15, not Q14 like X and Y */ |
438 | 0 | El = SHR32(MULT32_32_Q31(mid, mid),3) + side - 2*xp; |
439 | 0 | Er = SHR32(MULT32_32_Q31(mid, mid),3) + side + 2*xp; |
440 | 0 | if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28)) |
441 | 0 | { |
442 | 0 | OPUS_COPY(Y, X, N); |
443 | 0 | return; |
444 | 0 | } |
445 | | |
446 | | #ifdef FIXED_POINT |
447 | | kl = celt_ilog2(El)>>1; |
448 | | kr = celt_ilog2(Er)>>1; |
449 | | #endif |
450 | 0 | t = VSHR32(El, (kl<<1)-29); |
451 | 0 | lgain = celt_rsqrt_norm32(t); |
452 | 0 | t = VSHR32(Er, (kr<<1)-29); |
453 | 0 | rgain = celt_rsqrt_norm32(t); |
454 | |
|
455 | | #ifdef FIXED_POINT |
456 | | if (kl < 7) |
457 | | kl = 7; |
458 | | if (kr < 7) |
459 | | kr = 7; |
460 | | #endif |
461 | |
|
462 | 0 | for (j=0;j<N;j++) |
463 | 0 | { |
464 | 0 | celt_norm r, l; |
465 | | /* Apply mid scaling (side is already scaled) */ |
466 | 0 | l = MULT32_32_Q31(mid, X[j]); |
467 | 0 | r = Y[j]; |
468 | 0 | X[j] = VSHR32(MULT32_32_Q31(lgain, SUB32(l,r)), kl-15); |
469 | 0 | Y[j] = VSHR32(MULT32_32_Q31(rgain, ADD32(l,r)), kr-15); |
470 | 0 | } |
471 | 0 | } |
472 | | |
473 | | /* Decide whether we should spread the pulses in the current frame */ |
474 | | int spreading_decision(const CELTMode *m, const celt_norm *X, int *average, |
475 | | int last_decision, int *hf_average, int *tapset_decision, int update_hf, |
476 | | int end, int C, int M, const int *spread_weight) |
477 | 0 | { |
478 | 0 | int i, c, N0; |
479 | 0 | int sum = 0, nbBands=0; |
480 | 0 | const opus_int16 * OPUS_RESTRICT eBands = m->eBands; |
481 | 0 | int decision; |
482 | 0 | int hf_sum=0; |
483 | |
|
484 | 0 | celt_assert(end>0); |
485 | |
|
486 | 0 | N0 = M*m->shortMdctSize; |
487 | |
|
488 | 0 | if (M*(eBands[end]-eBands[end-1]) <= 8) |
489 | 0 | return SPREAD_NONE; |
490 | 0 | c=0; do { |
491 | 0 | for (i=0;i<end;i++) |
492 | 0 | { |
493 | 0 | int j, N, tmp=0; |
494 | 0 | int tcount[3] = {0,0,0}; |
495 | 0 | const celt_norm * OPUS_RESTRICT x = X+M*eBands[i]+c*N0; |
496 | 0 | N = M*(eBands[i+1]-eBands[i]); |
497 | 0 | if (N<=8) |
498 | 0 | continue; |
499 | | /* Compute rough CDF of |x[j]| */ |
500 | 0 | for (j=0;j<N;j++) |
501 | 0 | { |
502 | 0 | opus_val32 x2N; /* Q13 */ |
503 | |
|
504 | 0 | x2N = MULT16_16(MULT16_16_Q15(SHR32(x[j], NORM_SHIFT-14), SHR32(x[j], NORM_SHIFT-14)), N); |
505 | 0 | if (x2N < QCONST16(0.25f,13)) |
506 | 0 | tcount[0]++; |
507 | 0 | if (x2N < QCONST16(0.0625f,13)) |
508 | 0 | tcount[1]++; |
509 | 0 | if (x2N < QCONST16(0.015625f,13)) |
510 | 0 | tcount[2]++; |
511 | 0 | } |
512 | | |
513 | | /* Only include four last bands (8 kHz and up) */ |
514 | 0 | if (i>m->nbEBands-4) |
515 | 0 | hf_sum += celt_udiv(32*(tcount[1]+tcount[0]), N); |
516 | 0 | tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N); |
517 | 0 | sum += tmp*spread_weight[i]; |
518 | 0 | nbBands+=spread_weight[i]; |
519 | 0 | } |
520 | 0 | } while (++c<C); |
521 | |
|
522 | 0 | if (update_hf) |
523 | 0 | { |
524 | 0 | if (hf_sum) |
525 | 0 | hf_sum = celt_udiv(hf_sum, C*(4-m->nbEBands+end)); |
526 | 0 | *hf_average = (*hf_average+hf_sum)>>1; |
527 | 0 | hf_sum = *hf_average; |
528 | 0 | if (*tapset_decision==2) |
529 | 0 | hf_sum += 4; |
530 | 0 | else if (*tapset_decision==0) |
531 | 0 | hf_sum -= 4; |
532 | 0 | if (hf_sum > 22) |
533 | 0 | *tapset_decision=2; |
534 | 0 | else if (hf_sum > 18) |
535 | 0 | *tapset_decision=1; |
536 | 0 | else |
537 | 0 | *tapset_decision=0; |
538 | 0 | } |
539 | | /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/ |
540 | 0 | celt_assert(nbBands>0); /* end has to be non-zero */ |
541 | 0 | celt_assert(sum>=0); |
542 | 0 | sum = celt_udiv((opus_int32)sum<<8, nbBands); |
543 | | /* Recursive averaging */ |
544 | 0 | sum = (sum+*average)>>1; |
545 | 0 | *average = sum; |
546 | | /* Hysteresis */ |
547 | 0 | sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2; |
548 | 0 | if (sum < 80) |
549 | 0 | { |
550 | 0 | decision = SPREAD_AGGRESSIVE; |
551 | 0 | } else if (sum < 256) |
552 | 0 | { |
553 | 0 | decision = SPREAD_NORMAL; |
554 | 0 | } else if (sum < 384) |
555 | 0 | { |
556 | 0 | decision = SPREAD_LIGHT; |
557 | 0 | } else { |
558 | 0 | decision = SPREAD_NONE; |
559 | 0 | } |
560 | | #ifdef FUZZING |
561 | | decision = rand()&0x3; |
562 | | *tapset_decision=rand()%3; |
563 | | #endif |
564 | 0 | return decision; |
565 | 0 | } |
566 | | |
567 | | /* Indexing table for converting from natural Hadamard to ordery Hadamard |
568 | | This is essentially a bit-reversed Gray, on top of which we've added |
569 | | an inversion of the order because we want the DC at the end rather than |
570 | | the beginning. The lines are for N=2, 4, 8, 16 */ |
571 | | static const int ordery_table[] = { |
572 | | 1, 0, |
573 | | 3, 0, 2, 1, |
574 | | 7, 0, 4, 3, 6, 1, 5, 2, |
575 | | 15, 0, 8, 7, 12, 3, 11, 4, 14, 1, 9, 6, 13, 2, 10, 5, |
576 | | }; |
577 | | |
578 | | static void deinterleave_hadamard(celt_norm *X, int N0, int stride, int hadamard) |
579 | 0 | { |
580 | 0 | int i,j; |
581 | 0 | VARDECL(celt_norm, tmp); |
582 | 0 | int N; |
583 | 0 | SAVE_STACK; |
584 | 0 | N = N0*stride; |
585 | 0 | ALLOC(tmp, N, celt_norm); |
586 | 0 | celt_assert(stride>0); |
587 | 0 | if (hadamard) |
588 | 0 | { |
589 | 0 | const int *ordery = ordery_table+stride-2; |
590 | 0 | for (i=0;i<stride;i++) |
591 | 0 | { |
592 | 0 | for (j=0;j<N0;j++) |
593 | 0 | tmp[ordery[i]*N0+j] = X[j*stride+i]; |
594 | 0 | } |
595 | 0 | } else { |
596 | 0 | for (i=0;i<stride;i++) |
597 | 0 | for (j=0;j<N0;j++) |
598 | 0 | tmp[i*N0+j] = X[j*stride+i]; |
599 | 0 | } |
600 | 0 | OPUS_COPY(X, tmp, N); |
601 | 0 | RESTORE_STACK; |
602 | 0 | } |
603 | | |
604 | | static void interleave_hadamard(celt_norm *X, int N0, int stride, int hadamard) |
605 | 0 | { |
606 | 0 | int i,j; |
607 | 0 | VARDECL(celt_norm, tmp); |
608 | 0 | int N; |
609 | 0 | SAVE_STACK; |
610 | 0 | N = N0*stride; |
611 | 0 | ALLOC(tmp, N, celt_norm); |
612 | 0 | if (hadamard) |
613 | 0 | { |
614 | 0 | const int *ordery = ordery_table+stride-2; |
615 | 0 | for (i=0;i<stride;i++) |
616 | 0 | for (j=0;j<N0;j++) |
617 | 0 | tmp[j*stride+i] = X[ordery[i]*N0+j]; |
618 | 0 | } else { |
619 | 0 | for (i=0;i<stride;i++) |
620 | 0 | for (j=0;j<N0;j++) |
621 | 0 | tmp[j*stride+i] = X[i*N0+j]; |
622 | 0 | } |
623 | 0 | OPUS_COPY(X, tmp, N); |
624 | 0 | RESTORE_STACK; |
625 | 0 | } |
626 | | |
627 | | void haar1(celt_norm *X, int N0, int stride) |
628 | 0 | { |
629 | 0 | int i, j; |
630 | 0 | N0 >>= 1; |
631 | 0 | for (i=0;i<stride;i++) |
632 | 0 | for (j=0;j<N0;j++) |
633 | 0 | { |
634 | 0 | opus_val32 tmp1, tmp2; |
635 | 0 | tmp1 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*2*j+i]); |
636 | 0 | tmp2 = MULT32_32_Q31(QCONST32(.70710678f,31), X[stride*(2*j+1)+i]); |
637 | 0 | X[stride*2*j+i] = ADD32(tmp1, tmp2); |
638 | 0 | X[stride*(2*j+1)+i] = SUB32(tmp1, tmp2); |
639 | 0 | } |
640 | 0 | } |
641 | | |
642 | | static int compute_qn(int N, int b, int offset, int pulse_cap, int stereo) |
643 | 0 | { |
644 | 0 | static const opus_int16 exp2_table8[8] = |
645 | 0 | {16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048}; |
646 | 0 | int qn, qb; |
647 | 0 | int N2 = 2*N-1; |
648 | 0 | if (stereo && N==2) |
649 | 0 | N2--; |
650 | | /* The upper limit ensures that in a stereo split with itheta==16384, we'll |
651 | | always have enough bits left over to code at least one pulse in the |
652 | | side; otherwise it would collapse, since it doesn't get folded. */ |
653 | 0 | qb = celt_sudiv(b+N2*offset, N2); |
654 | 0 | qb = IMIN(b-pulse_cap-(4<<BITRES), qb); |
655 | |
|
656 | 0 | qb = IMIN(8<<BITRES, qb); |
657 | |
|
658 | 0 | if (qb<(1<<BITRES>>1)) { |
659 | 0 | qn = 1; |
660 | 0 | } else { |
661 | 0 | qn = exp2_table8[qb&0x7]>>(14-(qb>>BITRES)); |
662 | 0 | qn = (qn+1)>>1<<1; |
663 | 0 | } |
664 | 0 | celt_assert(qn <= 256); |
665 | 0 | return qn; |
666 | 0 | } |
667 | | |
668 | | struct band_ctx { |
669 | | int encode; |
670 | | int resynth; |
671 | | const CELTMode *m; |
672 | | int i; |
673 | | int intensity; |
674 | | int spread; |
675 | | int tf_change; |
676 | | ec_ctx *ec; |
677 | | opus_int32 remaining_bits; |
678 | | const celt_ener *bandE; |
679 | | opus_uint32 seed; |
680 | | int arch; |
681 | | int theta_round; |
682 | | int disable_inv; |
683 | | int avoid_split_noise; |
684 | | #ifdef ENABLE_QEXT |
685 | | ec_ctx *ext_ec; |
686 | | int extra_bits; |
687 | | opus_int32 ext_total_bits; |
688 | | int extra_bands; |
689 | | #endif |
690 | | }; |
691 | | |
692 | | struct split_ctx { |
693 | | int inv; |
694 | | int imid; |
695 | | int iside; |
696 | | int delta; |
697 | | int itheta; |
698 | | #ifdef ENABLE_QEXT |
699 | | int itheta_q30; |
700 | | #endif |
701 | | int qalloc; |
702 | | }; |
703 | | |
704 | | static void compute_theta(struct band_ctx *ctx, struct split_ctx *sctx, |
705 | | celt_norm *X, celt_norm *Y, int N, int *b, int B, int B0, |
706 | | int LM, |
707 | | int stereo, int *fill ARG_QEXT(int *ext_b)) |
708 | 0 | { |
709 | 0 | int qn; |
710 | 0 | int itheta=0; |
711 | 0 | int itheta_q30=0; |
712 | 0 | int delta; |
713 | 0 | int imid, iside; |
714 | 0 | int qalloc; |
715 | 0 | int pulse_cap; |
716 | 0 | int offset; |
717 | 0 | opus_int32 tell; |
718 | 0 | int inv=0; |
719 | 0 | int encode; |
720 | 0 | const CELTMode *m; |
721 | 0 | int i; |
722 | 0 | int intensity; |
723 | 0 | ec_ctx *ec; |
724 | 0 | const celt_ener *bandE; |
725 | |
|
726 | 0 | encode = ctx->encode; |
727 | 0 | m = ctx->m; |
728 | 0 | i = ctx->i; |
729 | 0 | intensity = ctx->intensity; |
730 | 0 | ec = ctx->ec; |
731 | 0 | bandE = ctx->bandE; |
732 | | |
733 | | /* Decide on the resolution to give to the split parameter theta */ |
734 | 0 | pulse_cap = m->logN[i]+LM*(1<<BITRES); |
735 | 0 | offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET); |
736 | 0 | qn = compute_qn(N, *b, offset, pulse_cap, stereo); |
737 | 0 | if (stereo && i>=intensity) |
738 | 0 | qn = 1; |
739 | 0 | if (encode) |
740 | 0 | { |
741 | | /* theta is the atan() of the ratio between the (normalized) |
742 | | side and mid. With just that parameter, we can re-scale both |
743 | | mid and side because we know that 1) they have unit norm and |
744 | | 2) they are orthogonal. */ |
745 | 0 | itheta_q30 = stereo_itheta(X, Y, stereo, N, ctx->arch); |
746 | 0 | itheta = itheta_q30>>16; |
747 | 0 | } |
748 | 0 | tell = ec_tell_frac(ec); |
749 | 0 | if (qn!=1) |
750 | 0 | { |
751 | 0 | if (encode) |
752 | 0 | { |
753 | 0 | if (!stereo || ctx->theta_round == 0) |
754 | 0 | { |
755 | 0 | itheta = (itheta*(opus_int32)qn+8192)>>14; |
756 | 0 | if (!stereo && ctx->avoid_split_noise && itheta > 0 && itheta < qn) |
757 | 0 | { |
758 | | /* Check if the selected value of theta will cause the bit allocation |
759 | | to inject noise on one side. If so, make sure the energy of that side |
760 | | is zero. */ |
761 | 0 | int unquantized = celt_udiv((opus_int32)itheta*16384, qn); |
762 | 0 | imid = bitexact_cos((opus_int16)unquantized); |
763 | 0 | iside = bitexact_cos((opus_int16)(16384-unquantized)); |
764 | 0 | delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid)); |
765 | 0 | if (delta > *b) |
766 | 0 | itheta = qn; |
767 | 0 | else if (delta < -*b) |
768 | 0 | itheta = 0; |
769 | 0 | } |
770 | 0 | } else { |
771 | 0 | int down; |
772 | | /* Bias quantization towards itheta=0 and itheta=16384. */ |
773 | 0 | int bias = itheta > 8192 ? 32767/qn : -32767/qn; |
774 | 0 | down = IMIN(qn-1, IMAX(0, (itheta*(opus_int32)qn + bias)>>14)); |
775 | 0 | if (ctx->theta_round < 0) |
776 | 0 | itheta = down; |
777 | 0 | else |
778 | 0 | itheta = down+1; |
779 | 0 | } |
780 | 0 | } |
781 | | /* Entropy coding of the angle. We use a uniform pdf for the |
782 | | time split, a step for stereo, and a triangular one for the rest. */ |
783 | 0 | if (stereo && N>2) |
784 | 0 | { |
785 | 0 | int p0 = 3; |
786 | 0 | int x = itheta; |
787 | 0 | int x0 = qn/2; |
788 | 0 | int ft = p0*(x0+1) + x0; |
789 | | /* Use a probability of p0 up to itheta=8192 and then use 1 after */ |
790 | 0 | if (encode) |
791 | 0 | { |
792 | 0 | ec_encode(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft); |
793 | 0 | } else { |
794 | 0 | int fs; |
795 | 0 | fs=ec_decode(ec,ft); |
796 | 0 | if (fs<(x0+1)*p0) |
797 | 0 | x=fs/p0; |
798 | 0 | else |
799 | 0 | x=x0+1+(fs-(x0+1)*p0); |
800 | 0 | ec_dec_update(ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft); |
801 | 0 | itheta = x; |
802 | 0 | } |
803 | 0 | } else if (B0>1 || stereo) { |
804 | | /* Uniform pdf */ |
805 | 0 | if (encode) |
806 | 0 | ec_enc_uint(ec, itheta, qn+1); |
807 | 0 | else |
808 | 0 | itheta = ec_dec_uint(ec, qn+1); |
809 | 0 | } else { |
810 | 0 | int fs=1, ft; |
811 | 0 | ft = ((qn>>1)+1)*((qn>>1)+1); |
812 | 0 | if (encode) |
813 | 0 | { |
814 | 0 | int fl; |
815 | |
|
816 | 0 | fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta; |
817 | 0 | fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 : |
818 | 0 | ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1); |
819 | |
|
820 | 0 | ec_encode(ec, fl, fl+fs, ft); |
821 | 0 | } else { |
822 | | /* Triangular pdf */ |
823 | 0 | int fl=0; |
824 | 0 | int fm; |
825 | 0 | fm = ec_decode(ec, ft); |
826 | |
|
827 | 0 | if (fm < ((qn>>1)*((qn>>1) + 1)>>1)) |
828 | 0 | { |
829 | 0 | itheta = (isqrt32(8*(opus_uint32)fm + 1) - 1)>>1; |
830 | 0 | fs = itheta + 1; |
831 | 0 | fl = itheta*(itheta + 1)>>1; |
832 | 0 | } |
833 | 0 | else |
834 | 0 | { |
835 | 0 | itheta = (2*(qn + 1) |
836 | 0 | - isqrt32(8*(opus_uint32)(ft - fm - 1) + 1))>>1; |
837 | 0 | fs = qn + 1 - itheta; |
838 | 0 | fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1); |
839 | 0 | } |
840 | |
|
841 | 0 | ec_dec_update(ec, fl, fl+fs, ft); |
842 | 0 | } |
843 | 0 | } |
844 | 0 | celt_assert(itheta>=0); |
845 | 0 | itheta = celt_udiv((opus_int32)itheta*16384, qn); |
846 | | #ifdef ENABLE_QEXT |
847 | | *ext_b = IMIN(*ext_b, ctx->ext_total_bits - (opus_int32)ec_tell_frac(ctx->ext_ec)); |
848 | | if (*ext_b >= 2*N<<BITRES && ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec)-1 > 2<<BITRES) { |
849 | | int extra_bits; |
850 | | int ext_tell = ec_tell_frac(ctx->ext_ec); |
851 | | extra_bits = IMIN(12, IMAX(2, celt_sudiv(*ext_b, (2*N-1)<<BITRES))); |
852 | | if (encode) { |
853 | | itheta_q30 = itheta_q30 - (itheta<<16); |
854 | | itheta_q30 = (itheta_q30*(opus_int64)qn*((1<<extra_bits)-1)+(1<<29))>>30; |
855 | | itheta_q30 += (1<<(extra_bits-1))-1; |
856 | | itheta_q30 = IMAX(0, IMIN((1<<extra_bits)-2, itheta_q30)); |
857 | | ec_enc_uint(ctx->ext_ec, itheta_q30, (1<<extra_bits)-1); |
858 | | } else { |
859 | | itheta_q30 = ec_dec_uint(ctx->ext_ec, (1<<extra_bits)-1); |
860 | | } |
861 | | itheta_q30 -= (1<<(extra_bits-1))-1; |
862 | | itheta_q30 = (itheta<<16) + itheta_q30*(opus_int64)(1<<30)/(qn*((1<<extra_bits)-1)); |
863 | | /* Hard bounds on itheta (can only trigger on corrupted bitstreams). */ |
864 | | itheta_q30 = IMAX(0, IMIN(itheta_q30, 1073741824)); |
865 | | *ext_b -= ec_tell_frac(ctx->ext_ec) - ext_tell; |
866 | | } else { |
867 | | itheta_q30 = (opus_int32)itheta<<16; |
868 | | } |
869 | | #endif |
870 | 0 | if (encode && stereo) |
871 | 0 | { |
872 | 0 | if (itheta==0) |
873 | 0 | intensity_stereo(m, X, Y, bandE, i, N); |
874 | 0 | else |
875 | 0 | stereo_split(X, Y, N); |
876 | 0 | } |
877 | | /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate. |
878 | | Let's do that at higher complexity */ |
879 | 0 | } else if (stereo) { |
880 | 0 | if (encode) |
881 | 0 | { |
882 | 0 | inv = itheta > 8192 && !ctx->disable_inv; |
883 | 0 | if (inv) |
884 | 0 | { |
885 | 0 | int j; |
886 | 0 | for (j=0;j<N;j++) |
887 | 0 | Y[j] = -Y[j]; |
888 | 0 | } |
889 | 0 | intensity_stereo(m, X, Y, bandE, i, N); |
890 | 0 | } |
891 | 0 | if (*b>2<<BITRES && ctx->remaining_bits > 2<<BITRES) |
892 | 0 | { |
893 | 0 | if (encode) |
894 | 0 | ec_enc_bit_logp(ec, inv, 2); |
895 | 0 | else |
896 | 0 | inv = ec_dec_bit_logp(ec, 2); |
897 | 0 | } else |
898 | 0 | inv = 0; |
899 | | /* inv flag override to avoid problems with downmixing. */ |
900 | 0 | if (ctx->disable_inv) |
901 | 0 | inv = 0; |
902 | 0 | itheta = 0; |
903 | 0 | itheta_q30 = 0; |
904 | 0 | } |
905 | 0 | qalloc = ec_tell_frac(ec) - tell; |
906 | 0 | *b -= qalloc; |
907 | |
|
908 | 0 | if (itheta == 0) |
909 | 0 | { |
910 | 0 | imid = 32767; |
911 | 0 | iside = 0; |
912 | 0 | *fill &= (1<<B)-1; |
913 | 0 | delta = -16384; |
914 | 0 | } else if (itheta == 16384) |
915 | 0 | { |
916 | 0 | imid = 0; |
917 | 0 | iside = 32767; |
918 | 0 | *fill &= ((1<<B)-1)<<B; |
919 | 0 | delta = 16384; |
920 | 0 | } else { |
921 | 0 | imid = bitexact_cos((opus_int16)itheta); |
922 | 0 | iside = bitexact_cos((opus_int16)(16384-itheta)); |
923 | | /* This is the mid vs side allocation that minimizes squared error |
924 | | in that band. */ |
925 | 0 | delta = FRAC_MUL16((N-1)<<7,bitexact_log2tan(iside,imid)); |
926 | 0 | } |
927 | |
|
928 | 0 | sctx->inv = inv; |
929 | 0 | sctx->imid = imid; |
930 | 0 | sctx->iside = iside; |
931 | 0 | sctx->delta = delta; |
932 | 0 | sctx->itheta = itheta; |
933 | | #ifdef ENABLE_QEXT |
934 | | sctx->itheta_q30 = itheta_q30; |
935 | | #endif |
936 | 0 | sctx->qalloc = qalloc; |
937 | 0 | } |
938 | | static unsigned quant_band_n1(struct band_ctx *ctx, celt_norm *X, celt_norm *Y, |
939 | | celt_norm *lowband_out) |
940 | 0 | { |
941 | 0 | int c; |
942 | 0 | int stereo; |
943 | 0 | celt_norm *x = X; |
944 | 0 | int encode; |
945 | 0 | ec_ctx *ec; |
946 | |
|
947 | 0 | encode = ctx->encode; |
948 | 0 | ec = ctx->ec; |
949 | |
|
950 | 0 | stereo = Y != NULL; |
951 | 0 | c=0; do { |
952 | 0 | int sign=0; |
953 | 0 | if (ctx->remaining_bits>=1<<BITRES) |
954 | 0 | { |
955 | 0 | if (encode) |
956 | 0 | { |
957 | 0 | sign = x[0]<0; |
958 | 0 | ec_enc_bits(ec, sign, 1); |
959 | 0 | } else { |
960 | 0 | sign = ec_dec_bits(ec, 1); |
961 | 0 | } |
962 | 0 | ctx->remaining_bits -= 1<<BITRES; |
963 | 0 | } |
964 | 0 | if (ctx->resynth) |
965 | 0 | x[0] = sign ? -NORM_SCALING : NORM_SCALING; |
966 | 0 | x = Y; |
967 | 0 | } while (++c<1+stereo); |
968 | 0 | if (lowband_out) |
969 | 0 | lowband_out[0] = SHR32(X[0],4); |
970 | 0 | return 1; |
971 | 0 | } |
972 | | |
973 | | /* This function is responsible for encoding and decoding a mono partition. |
974 | | It can split the band in two and transmit the energy difference with |
975 | | the two half-bands. It can be called recursively so bands can end up being |
976 | | split in 8 parts. */ |
977 | | static unsigned quant_partition(struct band_ctx *ctx, celt_norm *X, |
978 | | int N, int b, int B, celt_norm *lowband, |
979 | | int LM, |
980 | | opus_val32 gain, int fill |
981 | | ARG_QEXT(int ext_b)) |
982 | 0 | { |
983 | 0 | const unsigned char *cache; |
984 | 0 | int q; |
985 | 0 | int curr_bits; |
986 | 0 | int imid=0, iside=0; |
987 | 0 | int B0=B; |
988 | 0 | opus_val32 mid=0, side=0; |
989 | 0 | unsigned cm=0; |
990 | 0 | celt_norm *Y=NULL; |
991 | 0 | int encode; |
992 | 0 | const CELTMode *m; |
993 | 0 | int i; |
994 | 0 | int spread; |
995 | 0 | ec_ctx *ec; |
996 | |
|
997 | 0 | encode = ctx->encode; |
998 | 0 | m = ctx->m; |
999 | 0 | i = ctx->i; |
1000 | 0 | spread = ctx->spread; |
1001 | 0 | ec = ctx->ec; |
1002 | | |
1003 | | /* If we need 1.5 more bit than we can produce, split the band in two. */ |
1004 | 0 | cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i]; |
1005 | 0 | if (LM != -1 && b > cache[cache[0]]+12 && N>2) |
1006 | 0 | { |
1007 | 0 | int mbits, sbits, delta; |
1008 | 0 | int itheta; |
1009 | 0 | int qalloc; |
1010 | 0 | struct split_ctx sctx; |
1011 | 0 | celt_norm *next_lowband2=NULL; |
1012 | 0 | opus_int32 rebalance; |
1013 | |
|
1014 | 0 | N >>= 1; |
1015 | 0 | Y = X+N; |
1016 | 0 | LM -= 1; |
1017 | 0 | if (B==1) |
1018 | 0 | fill = (fill&1)|(fill<<1); |
1019 | 0 | B = (B+1)>>1; |
1020 | |
|
1021 | 0 | compute_theta(ctx, &sctx, X, Y, N, &b, B, B0, LM, 0, &fill ARG_QEXT(&ext_b)); |
1022 | 0 | imid = sctx.imid; |
1023 | 0 | iside = sctx.iside; |
1024 | 0 | delta = sctx.delta; |
1025 | 0 | itheta = sctx.itheta; |
1026 | 0 | qalloc = sctx.qalloc; |
1027 | | #ifdef FIXED_POINT |
1028 | | # ifdef ENABLE_QEXT |
1029 | | (void)imid; |
1030 | | (void)iside; |
1031 | | mid = celt_cos_norm32(sctx.itheta_q30); |
1032 | | side = celt_cos_norm32((1<<30)-sctx.itheta_q30); |
1033 | | # else |
1034 | | mid = SHL32(EXTEND32(imid), 16); |
1035 | | side = SHL32(EXTEND32(iside), 16); |
1036 | | # endif |
1037 | | #else |
1038 | | # ifdef ENABLE_QEXT |
1039 | | (void)imid; |
1040 | | (void)iside; |
1041 | | mid = celt_cos_norm2(sctx.itheta_q30*(1.f/(1<<30))); |
1042 | | side = celt_cos_norm2(1.f-sctx.itheta_q30*(1.f/(1<<30))); |
1043 | | # else |
1044 | 0 | mid = (1.f/32768)*imid; |
1045 | 0 | side = (1.f/32768)*iside; |
1046 | 0 | # endif |
1047 | 0 | #endif |
1048 | | |
1049 | | /* Give more bits to low-energy MDCTs than they would otherwise deserve */ |
1050 | 0 | if (B0>1 && (itheta&0x3fff)) |
1051 | 0 | { |
1052 | 0 | if (itheta > 8192) |
1053 | | /* Rough approximation for pre-echo masking */ |
1054 | 0 | delta -= delta>>(4-LM); |
1055 | 0 | else |
1056 | | /* Corresponds to a forward-masking slope of 1.5 dB per 10 ms */ |
1057 | 0 | delta = IMIN(0, delta + (N<<BITRES>>(5-LM))); |
1058 | 0 | } |
1059 | 0 | mbits = IMAX(0, IMIN(b, (b-delta)/2)); |
1060 | 0 | sbits = b-mbits; |
1061 | 0 | ctx->remaining_bits -= qalloc; |
1062 | |
|
1063 | 0 | if (lowband) |
1064 | 0 | next_lowband2 = lowband+N; /* >32-bit split case */ |
1065 | |
|
1066 | 0 | rebalance = ctx->remaining_bits; |
1067 | 0 | if (mbits >= sbits) |
1068 | 0 | { |
1069 | 0 | cm = quant_partition(ctx, X, N, mbits, B, lowband, LM, |
1070 | 0 | MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2)); |
1071 | 0 | rebalance = mbits - (rebalance-ctx->remaining_bits); |
1072 | 0 | if (rebalance > 3<<BITRES && itheta!=0) |
1073 | 0 | sbits += rebalance - (3<<BITRES); |
1074 | 0 | cm |= quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM, |
1075 | 0 | MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1); |
1076 | 0 | } else { |
1077 | 0 | cm = quant_partition(ctx, Y, N, sbits, B, next_lowband2, LM, |
1078 | 0 | MULT32_32_Q31(gain,side), fill>>B ARG_QEXT(ext_b/2))<<(B0>>1); |
1079 | 0 | rebalance = sbits - (rebalance-ctx->remaining_bits); |
1080 | 0 | if (rebalance > 3<<BITRES && itheta!=16384) |
1081 | 0 | mbits += rebalance - (3<<BITRES); |
1082 | 0 | cm |= quant_partition(ctx, X, N, mbits, B, lowband, LM, |
1083 | 0 | MULT32_32_Q31(gain,mid), fill ARG_QEXT(ext_b/2)); |
1084 | 0 | } |
1085 | 0 | } else { |
1086 | | #ifdef ENABLE_QEXT |
1087 | | int extra_bits; |
1088 | | int ext_remaining_bits; |
1089 | | extra_bits = ext_b/(N-1)>>BITRES; |
1090 | | ext_remaining_bits = ctx->ext_total_bits-(opus_int32)ec_tell_frac(ctx->ext_ec); |
1091 | | if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) { |
1092 | | extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES; |
1093 | | extra_bits = IMAX(extra_bits-1, 0); |
1094 | | } |
1095 | | extra_bits = IMIN(12, extra_bits); |
1096 | | #endif |
1097 | | /* This is the basic no-split case */ |
1098 | 0 | q = bits2pulses(m, i, LM, b); |
1099 | 0 | curr_bits = pulses2bits(m, i, LM, q); |
1100 | 0 | ctx->remaining_bits -= curr_bits; |
1101 | | |
1102 | | /* Ensures we can never bust the budget */ |
1103 | 0 | while (ctx->remaining_bits < 0 && q > 0) |
1104 | 0 | { |
1105 | 0 | ctx->remaining_bits += curr_bits; |
1106 | 0 | q--; |
1107 | 0 | curr_bits = pulses2bits(m, i, LM, q); |
1108 | 0 | ctx->remaining_bits -= curr_bits; |
1109 | 0 | } |
1110 | |
|
1111 | 0 | if (q!=0) |
1112 | 0 | { |
1113 | 0 | int K = get_pulses(q); |
1114 | | |
1115 | | /* Finally do the actual quantization */ |
1116 | 0 | if (encode) |
1117 | 0 | { |
1118 | 0 | cm = alg_quant(X, N, K, spread, B, ec, gain, ctx->resynth |
1119 | 0 | ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits), |
1120 | 0 | ctx->arch); |
1121 | 0 | } else { |
1122 | 0 | cm = alg_unquant(X, N, K, spread, B, ec, gain |
1123 | 0 | ARG_QEXT(ctx->ext_ec) ARG_QEXT(extra_bits)); |
1124 | 0 | } |
1125 | | #ifdef ENABLE_QEXT |
1126 | | } else if (ext_b > 2*N<<BITRES) |
1127 | | { |
1128 | | extra_bits = ext_b/(N-1)>>BITRES; |
1129 | | ext_remaining_bits = ctx->ext_total_bits-ec_tell_frac(ctx->ext_ec); |
1130 | | if (ext_remaining_bits < ((extra_bits+1)*(N-1)+N)<<BITRES) { |
1131 | | extra_bits = (ext_remaining_bits-(N<<BITRES))/(N-1)>>BITRES; |
1132 | | extra_bits = IMAX(extra_bits-1, 0); |
1133 | | } |
1134 | | extra_bits = IMIN(14, extra_bits); |
1135 | | if (encode) cm = cubic_quant(X, N, extra_bits, B, ctx->ext_ec, gain, ctx->resynth); |
1136 | | else cm = cubic_unquant(X, N, extra_bits, B, ctx->ext_ec, gain); |
1137 | | #endif |
1138 | 0 | } else { |
1139 | | /* If there's no pulse, fill the band anyway */ |
1140 | 0 | int j; |
1141 | 0 | if (ctx->resynth) |
1142 | 0 | { |
1143 | 0 | unsigned cm_mask; |
1144 | | /* B can be as large as 16, so this shift might overflow an int on a |
1145 | | 16-bit platform; use a long to get defined behavior.*/ |
1146 | 0 | cm_mask = (unsigned)(1UL<<B)-1; |
1147 | 0 | fill &= cm_mask; |
1148 | 0 | if (!fill) |
1149 | 0 | { |
1150 | 0 | OPUS_CLEAR(X, N); |
1151 | 0 | } else { |
1152 | 0 | if (lowband == NULL) |
1153 | 0 | { |
1154 | | /* Noise */ |
1155 | 0 | for (j=0;j<N;j++) |
1156 | 0 | { |
1157 | 0 | ctx->seed = celt_lcg_rand(ctx->seed); |
1158 | 0 | X[j] = SHL32((celt_norm)((opus_int32)ctx->seed>>20), NORM_SHIFT-14); |
1159 | 0 | } |
1160 | 0 | cm = cm_mask; |
1161 | 0 | } else { |
1162 | | /* Folded spectrum */ |
1163 | 0 | for (j=0;j<N;j++) |
1164 | 0 | { |
1165 | 0 | opus_val16 tmp; |
1166 | 0 | ctx->seed = celt_lcg_rand(ctx->seed); |
1167 | | /* About 48 dB below the "normal" folding level */ |
1168 | 0 | tmp = QCONST16(1.0f/256, NORM_SHIFT-4); |
1169 | 0 | tmp = (ctx->seed)&0x8000 ? tmp : -tmp; |
1170 | 0 | X[j] = lowband[j]+tmp; |
1171 | 0 | } |
1172 | 0 | cm = fill; |
1173 | 0 | } |
1174 | 0 | renormalise_vector(X, N, gain, ctx->arch); |
1175 | 0 | } |
1176 | 0 | } |
1177 | 0 | } |
1178 | 0 | } |
1179 | |
|
1180 | 0 | return cm; |
1181 | 0 | } |
1182 | | |
1183 | | #ifdef ENABLE_QEXT |
1184 | | static unsigned cubic_quant_partition(struct band_ctx *ctx, celt_norm *X, int N, int b, int B, ec_ctx *ec, int LM, opus_val32 gain, int resynth, int encode) |
1185 | | { |
1186 | | celt_assert(LM>=0); |
1187 | | ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec); |
1188 | | b = IMIN(b, ctx->remaining_bits); |
1189 | | /* As long as we have at least two bits of depth, split all the way to LM=0 (not -1 like PVQ). */ |
1190 | | if (LM==0 || b<=2*N<<BITRES) { |
1191 | | int res, ret; |
1192 | | b = IMIN(b + ((N-1)<<BITRES)/2, ctx->remaining_bits); |
1193 | | /* Resolution left after taking into account coding the cube face. */ |
1194 | | res = (b-(1<<BITRES)-ctx->m->logN[ctx->i]-(LM<<BITRES)-1)/(N-1)>>BITRES; |
1195 | | res = IMIN(14, IMAX(0, res)); |
1196 | | if (encode) ret = cubic_quant(X, N, res, B, ec, gain, resynth); |
1197 | | else ret = cubic_unquant(X, N, res, B, ec, gain); |
1198 | | ctx->remaining_bits = ctx->ec->storage*8*8 - ec_tell_frac(ctx->ec); |
1199 | | return ret; |
1200 | | } else { |
1201 | | celt_norm *Y; |
1202 | | opus_int32 itheta_q30; |
1203 | | opus_val32 g1, g2; |
1204 | | opus_int32 theta_res; |
1205 | | opus_int32 qtheta; |
1206 | | int delta; |
1207 | | int b1, b2; |
1208 | | int cm; |
1209 | | int N0; |
1210 | | N0 = N; |
1211 | | N >>= 1; |
1212 | | Y = X+N; |
1213 | | LM -= 1; |
1214 | | B = (B+1)>>1; |
1215 | | theta_res = IMIN(16, (b>>BITRES)/(N0-1) + 1); |
1216 | | if (encode) { |
1217 | | itheta_q30 = stereo_itheta(X, Y, 0, N, ctx->arch); |
1218 | | qtheta = (itheta_q30+(1<<(29-theta_res)))>>(30-theta_res); |
1219 | | ec_enc_uint(ec, qtheta, (1<<theta_res)+1); |
1220 | | } else { |
1221 | | qtheta = ec_dec_uint(ec, (1<<theta_res)+1); |
1222 | | } |
1223 | | itheta_q30 = qtheta<<(30-theta_res); |
1224 | | b -= theta_res<<BITRES; |
1225 | | delta = (N0-1) * 23 * ((itheta_q30>>16)-8192) >> (17-BITRES); |
1226 | | |
1227 | | #ifdef FIXED_POINT |
1228 | | g1 = celt_cos_norm32(itheta_q30); |
1229 | | g2 = celt_cos_norm32((1<<30)-itheta_q30); |
1230 | | #else |
1231 | | g1 = celt_cos_norm2(itheta_q30*(1.f/(1<<30))); |
1232 | | g2 = celt_cos_norm2(1.f-itheta_q30*(1.f/(1<<30))); |
1233 | | #endif |
1234 | | if (itheta_q30 == 0) { |
1235 | | b1=b; |
1236 | | b2=0; |
1237 | | } else if (itheta_q30==1073741824) { |
1238 | | b1=0; |
1239 | | b2=b; |
1240 | | } else { |
1241 | | b1 = IMIN(b, IMAX(0, (b-delta)/2)); |
1242 | | b2 = b-b1; |
1243 | | } |
1244 | | cm = cubic_quant_partition(ctx, X, N, b1, B, ec, LM, MULT32_32_Q31(gain, g1), resynth, encode); |
1245 | | cm |= cubic_quant_partition(ctx, Y, N, b2, B, ec, LM, MULT32_32_Q31(gain, g2), resynth, encode); |
1246 | | return cm; |
1247 | | } |
1248 | | } |
1249 | | #endif |
1250 | | |
1251 | | /* This function is responsible for encoding and decoding a band for the mono case. */ |
1252 | | static unsigned quant_band(struct band_ctx *ctx, celt_norm *X, |
1253 | | int N, int b, int B, celt_norm *lowband, |
1254 | | int LM, celt_norm *lowband_out, |
1255 | | opus_val32 gain, celt_norm *lowband_scratch, int fill |
1256 | | ARG_QEXT(int ext_b)) |
1257 | 0 | { |
1258 | 0 | int N0=N; |
1259 | 0 | int N_B=N; |
1260 | 0 | int N_B0; |
1261 | 0 | int B0=B; |
1262 | 0 | int time_divide=0; |
1263 | 0 | int recombine=0; |
1264 | 0 | int longBlocks; |
1265 | 0 | unsigned cm=0; |
1266 | 0 | int k; |
1267 | 0 | int encode; |
1268 | 0 | int tf_change; |
1269 | |
|
1270 | 0 | encode = ctx->encode; |
1271 | 0 | tf_change = ctx->tf_change; |
1272 | |
|
1273 | 0 | longBlocks = B0==1; |
1274 | |
|
1275 | 0 | N_B = celt_udiv(N_B, B); |
1276 | | |
1277 | | /* Special case for one sample */ |
1278 | 0 | if (N==1) |
1279 | 0 | { |
1280 | 0 | return quant_band_n1(ctx, X, NULL, lowband_out); |
1281 | 0 | } |
1282 | | |
1283 | 0 | if (tf_change>0) |
1284 | 0 | recombine = tf_change; |
1285 | | /* Band recombining to increase frequency resolution */ |
1286 | |
|
1287 | 0 | if (lowband_scratch && lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1)) |
1288 | 0 | { |
1289 | 0 | OPUS_COPY(lowband_scratch, lowband, N); |
1290 | 0 | lowband = lowband_scratch; |
1291 | 0 | } |
1292 | |
|
1293 | 0 | for (k=0;k<recombine;k++) |
1294 | 0 | { |
1295 | 0 | static const unsigned char bit_interleave_table[16]={ |
1296 | 0 | 0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3 |
1297 | 0 | }; |
1298 | 0 | if (encode) |
1299 | 0 | haar1(X, N>>k, 1<<k); |
1300 | 0 | if (lowband) |
1301 | 0 | haar1(lowband, N>>k, 1<<k); |
1302 | 0 | fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2; |
1303 | 0 | } |
1304 | 0 | B>>=recombine; |
1305 | 0 | N_B<<=recombine; |
1306 | | |
1307 | | /* Increasing the time resolution */ |
1308 | 0 | while ((N_B&1) == 0 && tf_change<0) |
1309 | 0 | { |
1310 | 0 | if (encode) |
1311 | 0 | haar1(X, N_B, B); |
1312 | 0 | if (lowband) |
1313 | 0 | haar1(lowband, N_B, B); |
1314 | 0 | fill |= fill<<B; |
1315 | 0 | B <<= 1; |
1316 | 0 | N_B >>= 1; |
1317 | 0 | time_divide++; |
1318 | 0 | tf_change++; |
1319 | 0 | } |
1320 | 0 | B0=B; |
1321 | 0 | N_B0 = N_B; |
1322 | | |
1323 | | /* Reorganize the samples in time order instead of frequency order */ |
1324 | 0 | if (B0>1) |
1325 | 0 | { |
1326 | 0 | if (encode) |
1327 | 0 | deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks); |
1328 | 0 | if (lowband) |
1329 | 0 | deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks); |
1330 | 0 | } |
1331 | |
|
1332 | | #ifdef ENABLE_QEXT |
1333 | | if (ctx->extra_bands && b > (3*N<<BITRES)+(ctx->m->logN[ctx->i]+8+8*LM)) { |
1334 | | cm = cubic_quant_partition(ctx, X, N, b, B, ctx->ec, LM, gain, ctx->resynth, encode); |
1335 | | } else |
1336 | | #endif |
1337 | 0 | { |
1338 | 0 | cm = quant_partition(ctx, X, N, b, B, lowband, LM, gain, fill ARG_QEXT(ext_b)); |
1339 | 0 | } |
1340 | | |
1341 | | /* This code is used by the decoder and by the resynthesis-enabled encoder */ |
1342 | 0 | if (ctx->resynth) |
1343 | 0 | { |
1344 | | /* Undo the sample reorganization going from time order to frequency order */ |
1345 | 0 | if (B0>1) |
1346 | 0 | interleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks); |
1347 | | |
1348 | | /* Undo time-freq changes that we did earlier */ |
1349 | 0 | N_B = N_B0; |
1350 | 0 | B = B0; |
1351 | 0 | for (k=0;k<time_divide;k++) |
1352 | 0 | { |
1353 | 0 | B >>= 1; |
1354 | 0 | N_B <<= 1; |
1355 | 0 | cm |= cm>>B; |
1356 | 0 | haar1(X, N_B, B); |
1357 | 0 | } |
1358 | |
|
1359 | 0 | for (k=0;k<recombine;k++) |
1360 | 0 | { |
1361 | 0 | static const unsigned char bit_deinterleave_table[16]={ |
1362 | 0 | 0x00,0x03,0x0C,0x0F,0x30,0x33,0x3C,0x3F, |
1363 | 0 | 0xC0,0xC3,0xCC,0xCF,0xF0,0xF3,0xFC,0xFF |
1364 | 0 | }; |
1365 | 0 | cm = bit_deinterleave_table[cm]; |
1366 | 0 | haar1(X, N0>>k, 1<<k); |
1367 | 0 | } |
1368 | 0 | B<<=recombine; |
1369 | | |
1370 | | /* Scale output for later folding */ |
1371 | 0 | if (lowband_out) |
1372 | 0 | { |
1373 | 0 | int j; |
1374 | 0 | opus_val16 n; |
1375 | 0 | n = celt_sqrt(SHL32(EXTEND32(N0),22)); |
1376 | 0 | for (j=0;j<N0;j++) |
1377 | 0 | lowband_out[j] = MULT16_32_Q15(n,X[j]); |
1378 | 0 | } |
1379 | 0 | cm &= (1<<B)-1; |
1380 | 0 | } |
1381 | 0 | return cm; |
1382 | 0 | } |
1383 | | |
1384 | | #ifdef FIXED_POINT |
1385 | | #define MIN_STEREO_ENERGY 2 |
1386 | | #else |
1387 | 0 | #define MIN_STEREO_ENERGY 1e-10f |
1388 | | #endif |
1389 | | |
1390 | | /* This function is responsible for encoding and decoding a band for the stereo case. */ |
1391 | | static unsigned quant_band_stereo(struct band_ctx *ctx, celt_norm *X, celt_norm *Y, |
1392 | | int N, int b, int B, celt_norm *lowband, |
1393 | | int LM, celt_norm *lowband_out, |
1394 | | celt_norm *lowband_scratch, int fill |
1395 | | ARG_QEXT(int ext_b) ARG_QEXT(const int *cap)) |
1396 | 0 | { |
1397 | 0 | int imid=0, iside=0; |
1398 | 0 | int inv = 0; |
1399 | 0 | opus_val32 mid=0, side=0; |
1400 | 0 | unsigned cm=0; |
1401 | 0 | int mbits, sbits, delta; |
1402 | 0 | int itheta; |
1403 | 0 | int qalloc; |
1404 | 0 | struct split_ctx sctx; |
1405 | 0 | int orig_fill; |
1406 | 0 | int encode; |
1407 | 0 | ec_ctx *ec; |
1408 | |
|
1409 | 0 | encode = ctx->encode; |
1410 | 0 | ec = ctx->ec; |
1411 | | |
1412 | | /* Special case for one sample */ |
1413 | 0 | if (N==1) |
1414 | 0 | { |
1415 | 0 | return quant_band_n1(ctx, X, Y, lowband_out); |
1416 | 0 | } |
1417 | | |
1418 | 0 | orig_fill = fill; |
1419 | |
|
1420 | 0 | if (encode) { |
1421 | 0 | if (ctx->bandE[ctx->i] < MIN_STEREO_ENERGY || ctx->bandE[ctx->m->nbEBands+ctx->i] < MIN_STEREO_ENERGY) { |
1422 | 0 | if (ctx->bandE[ctx->i] > ctx->bandE[ctx->m->nbEBands+ctx->i]) OPUS_COPY(Y, X, N); |
1423 | 0 | else OPUS_COPY(X, Y, N); |
1424 | 0 | } |
1425 | 0 | } |
1426 | 0 | compute_theta(ctx, &sctx, X, Y, N, &b, B, B, LM, 1, &fill ARG_QEXT(&ext_b)); |
1427 | 0 | inv = sctx.inv; |
1428 | 0 | imid = sctx.imid; |
1429 | 0 | iside = sctx.iside; |
1430 | 0 | delta = sctx.delta; |
1431 | 0 | itheta = sctx.itheta; |
1432 | 0 | qalloc = sctx.qalloc; |
1433 | | #ifdef FIXED_POINT |
1434 | | # ifdef ENABLE_QEXT |
1435 | | (void)imid; |
1436 | | (void)iside; |
1437 | | mid = celt_cos_norm32(sctx.itheta_q30); |
1438 | | side = celt_cos_norm32((1<<30)-sctx.itheta_q30); |
1439 | | # else |
1440 | | mid = SHL32(EXTEND32(imid), 16); |
1441 | | side = SHL32(EXTEND32(iside), 16); |
1442 | | # endif |
1443 | | #else |
1444 | | # ifdef ENABLE_QEXT |
1445 | | (void)imid; |
1446 | | (void)iside; |
1447 | | mid = cos(.5*M_PI*sctx.itheta_q30*(1.f/(1<<30))); |
1448 | | side = sin(.5*M_PI*sctx.itheta_q30*(1.f/(1<<30))); |
1449 | | # else |
1450 | 0 | mid = (1.f/32768)*imid; |
1451 | 0 | side = (1.f/32768)*iside; |
1452 | 0 | # endif |
1453 | 0 | #endif |
1454 | | |
1455 | | /* This is a special case for N=2 that only works for stereo and takes |
1456 | | advantage of the fact that mid and side are orthogonal to encode |
1457 | | the side with just one bit. */ |
1458 | 0 | if (N==2) |
1459 | 0 | { |
1460 | 0 | int c; |
1461 | 0 | int sign=0; |
1462 | 0 | celt_norm *x2, *y2; |
1463 | 0 | mbits = b; |
1464 | 0 | sbits = 0; |
1465 | | /* Only need one bit for the side. */ |
1466 | 0 | if (itheta != 0 && itheta != 16384) |
1467 | 0 | sbits = 1<<BITRES; |
1468 | 0 | mbits -= sbits; |
1469 | 0 | c = itheta > 8192; |
1470 | 0 | ctx->remaining_bits -= qalloc+sbits; |
1471 | |
|
1472 | 0 | x2 = c ? Y : X; |
1473 | 0 | y2 = c ? X : Y; |
1474 | 0 | if (sbits) |
1475 | 0 | { |
1476 | 0 | if (encode) |
1477 | 0 | { |
1478 | | /* Here we only need to encode a sign for the side. */ |
1479 | | /* FIXME: Need to increase fixed-point precision? */ |
1480 | 0 | sign = MULT32_32_Q31(x2[0],y2[1]) - MULT32_32_Q31(x2[1],y2[0]) < 0; |
1481 | 0 | ec_enc_bits(ec, sign, 1); |
1482 | 0 | } else { |
1483 | 0 | sign = ec_dec_bits(ec, 1); |
1484 | 0 | } |
1485 | 0 | } |
1486 | 0 | sign = 1-2*sign; |
1487 | | /* We use orig_fill here because we want to fold the side, but if |
1488 | | itheta==16384, we'll have cleared the low bits of fill. */ |
1489 | 0 | cm = quant_band(ctx, x2, N, mbits, B, lowband, LM, lowband_out, Q31ONE, |
1490 | 0 | lowband_scratch, orig_fill ARG_QEXT(ext_b)); |
1491 | | /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse), |
1492 | | and there's no need to worry about mixing with the other channel. */ |
1493 | 0 | y2[0] = -sign*x2[1]; |
1494 | 0 | y2[1] = sign*x2[0]; |
1495 | 0 | if (ctx->resynth) |
1496 | 0 | { |
1497 | 0 | celt_norm tmp; |
1498 | 0 | X[0] = MULT32_32_Q31(mid, X[0]); |
1499 | 0 | X[1] = MULT32_32_Q31(mid, X[1]); |
1500 | 0 | Y[0] = MULT32_32_Q31(side, Y[0]); |
1501 | 0 | Y[1] = MULT32_32_Q31(side, Y[1]); |
1502 | 0 | tmp = X[0]; |
1503 | 0 | X[0] = SUB32(tmp,Y[0]); |
1504 | 0 | Y[0] = ADD32(tmp,Y[0]); |
1505 | 0 | tmp = X[1]; |
1506 | 0 | X[1] = SUB32(tmp,Y[1]); |
1507 | 0 | Y[1] = ADD32(tmp,Y[1]); |
1508 | 0 | } |
1509 | 0 | } else { |
1510 | | /* "Normal" split code */ |
1511 | 0 | opus_int32 rebalance; |
1512 | |
|
1513 | 0 | mbits = IMAX(0, IMIN(b, (b-delta)/2)); |
1514 | 0 | sbits = b-mbits; |
1515 | 0 | ctx->remaining_bits -= qalloc; |
1516 | |
|
1517 | 0 | rebalance = ctx->remaining_bits; |
1518 | 0 | if (mbits >= sbits) |
1519 | 0 | { |
1520 | | #ifdef ENABLE_QEXT |
1521 | | int qext_extra = 0; |
1522 | | /* Reallocate any mid bits that cannot be used to extra mid bits. */ |
1523 | | if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, mbits - cap[ctx->i]/2)); |
1524 | | #endif |
1525 | | /* In stereo mode, we do not apply a scaling to the mid because we need the normalized |
1526 | | mid for folding later. */ |
1527 | 0 | cm = quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE, |
1528 | 0 | lowband_scratch, fill ARG_QEXT(ext_b/2+qext_extra)); |
1529 | 0 | rebalance = mbits - (rebalance-ctx->remaining_bits); |
1530 | 0 | if (rebalance > 3<<BITRES && itheta!=0) |
1531 | 0 | sbits += rebalance - (3<<BITRES); |
1532 | | #ifdef ENABLE_QEXT |
1533 | | /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the mid. */ |
1534 | | if (ctx->extra_bands) sbits = IMIN(sbits, ctx->remaining_bits); |
1535 | | #endif |
1536 | | /* For a stereo split, the high bits of fill are always zero, so no |
1537 | | folding will be done to the side. */ |
1538 | 0 | cm |= quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2-qext_extra)); |
1539 | 0 | } else { |
1540 | | #ifdef ENABLE_QEXT |
1541 | | int qext_extra = 0; |
1542 | | /* Reallocate any side bits that cannot be used to extra side bits. */ |
1543 | | if (cap != NULL && ext_b != 0) qext_extra = IMAX(0, IMIN(ext_b/2, sbits - cap[ctx->i]/2)); |
1544 | | #endif |
1545 | | /* For a stereo split, the high bits of fill are always zero, so no |
1546 | | folding will be done to the side. */ |
1547 | 0 | cm = quant_band(ctx, Y, N, sbits, B, NULL, LM, NULL, side, NULL, fill>>B ARG_QEXT(ext_b/2+qext_extra)); |
1548 | 0 | rebalance = sbits - (rebalance-ctx->remaining_bits); |
1549 | 0 | if (rebalance > 3<<BITRES && itheta!=16384) |
1550 | 0 | mbits += rebalance - (3<<BITRES); |
1551 | | #ifdef ENABLE_QEXT |
1552 | | /* Guard against overflowing the EC with the angle if the cubic quant used too many bits for the side. */ |
1553 | | if (ctx->extra_bands) mbits = IMIN(mbits, ctx->remaining_bits); |
1554 | | #endif |
1555 | | /* In stereo mode, we do not apply a scaling to the mid because we need the normalized |
1556 | | mid for folding later. */ |
1557 | 0 | cm |= quant_band(ctx, X, N, mbits, B, lowband, LM, lowband_out, Q31ONE, |
1558 | 0 | lowband_scratch, fill ARG_QEXT(ext_b/2-qext_extra)); |
1559 | 0 | } |
1560 | 0 | } |
1561 | | |
1562 | | |
1563 | | /* This code is used by the decoder and by the resynthesis-enabled encoder */ |
1564 | 0 | if (ctx->resynth) |
1565 | 0 | { |
1566 | 0 | if (N!=2) |
1567 | 0 | stereo_merge(X, Y, mid, N, ctx->arch); |
1568 | 0 | if (inv) |
1569 | 0 | { |
1570 | 0 | int j; |
1571 | 0 | for (j=0;j<N;j++) |
1572 | 0 | Y[j] = -Y[j]; |
1573 | 0 | } |
1574 | 0 | } |
1575 | 0 | return cm; |
1576 | 0 | } |
1577 | | |
1578 | | #ifndef DISABLE_UPDATE_DRAFT |
1579 | | static void special_hybrid_folding(const CELTMode *m, celt_norm *norm, celt_norm *norm2, int start, int M, int dual_stereo) |
1580 | 0 | { |
1581 | 0 | int n1, n2; |
1582 | 0 | const opus_int16 * OPUS_RESTRICT eBands = m->eBands; |
1583 | 0 | n1 = M*(eBands[start+1]-eBands[start]); |
1584 | 0 | n2 = M*(eBands[start+2]-eBands[start+1]); |
1585 | | /* Duplicate enough of the first band folding data to be able to fold the second band. |
1586 | | Copies no data for CELT-only mode. */ |
1587 | 0 | OPUS_COPY(&norm[n1], &norm[2*n1 - n2], n2-n1); |
1588 | 0 | if (dual_stereo) |
1589 | 0 | OPUS_COPY(&norm2[n1], &norm2[2*n1 - n2], n2-n1); |
1590 | 0 | } |
1591 | | #endif |
1592 | | |
1593 | | void quant_all_bands(int encode, const CELTMode *m, int start, int end, |
1594 | | celt_norm *X_, celt_norm *Y_, unsigned char *collapse_masks, |
1595 | | const celt_ener *bandE, int *pulses, int shortBlocks, int spread, |
1596 | | int dual_stereo, int intensity, int *tf_res, opus_int32 total_bits, |
1597 | | opus_int32 balance, ec_ctx *ec, int LM, int codedBands, |
1598 | | opus_uint32 *seed, int complexity, int arch, int disable_inv |
1599 | | ARG_QEXT(ec_ctx *ext_ec) ARG_QEXT(int *extra_pulses) |
1600 | | ARG_QEXT(opus_int32 ext_total_bits) ARG_QEXT(const int *cap)) |
1601 | 0 | { |
1602 | 0 | int i; |
1603 | 0 | opus_int32 remaining_bits; |
1604 | 0 | const opus_int16 * OPUS_RESTRICT eBands = m->eBands; |
1605 | 0 | celt_norm * OPUS_RESTRICT norm, * OPUS_RESTRICT norm2; |
1606 | 0 | VARDECL(celt_norm, _norm); |
1607 | 0 | VARDECL(celt_norm, _lowband_scratch); |
1608 | 0 | VARDECL(celt_norm, X_save); |
1609 | 0 | VARDECL(celt_norm, Y_save); |
1610 | 0 | VARDECL(celt_norm, X_save2); |
1611 | 0 | VARDECL(celt_norm, Y_save2); |
1612 | 0 | VARDECL(celt_norm, norm_save2); |
1613 | 0 | int resynth_alloc; |
1614 | 0 | celt_norm *lowband_scratch; |
1615 | 0 | int B; |
1616 | 0 | int M; |
1617 | 0 | int lowband_offset; |
1618 | 0 | int update_lowband = 1; |
1619 | 0 | int C = Y_ != NULL ? 2 : 1; |
1620 | 0 | int norm_offset; |
1621 | 0 | int theta_rdo = encode && Y_!=NULL && !dual_stereo && complexity>=8; |
1622 | | #ifdef RESYNTH |
1623 | | int resynth = 1; |
1624 | | #else |
1625 | 0 | int resynth = !encode || theta_rdo; |
1626 | 0 | #endif |
1627 | 0 | struct band_ctx ctx; |
1628 | | #ifdef ENABLE_QEXT |
1629 | | int ext_b; |
1630 | | opus_int32 ext_balance=0; |
1631 | | opus_int32 ext_tell=0; |
1632 | | #endif |
1633 | 0 | SAVE_STACK; |
1634 | |
|
1635 | 0 | M = 1<<LM; |
1636 | 0 | B = shortBlocks ? M : 1; |
1637 | 0 | norm_offset = M*eBands[start]; |
1638 | | /* No need to allocate norm for the last band because we don't need an |
1639 | | output in that band. */ |
1640 | 0 | ALLOC(_norm, C*(M*eBands[m->nbEBands-1]-norm_offset), celt_norm); |
1641 | 0 | norm = _norm; |
1642 | 0 | norm2 = norm + M*eBands[m->nbEBands-1]-norm_offset; |
1643 | | |
1644 | | /* For decoding, we can use the last band as scratch space because we don't need that |
1645 | | scratch space for the last band and we don't care about the data there until we're |
1646 | | decoding the last band. */ |
1647 | 0 | if (encode && resynth) |
1648 | 0 | resynth_alloc = M*(eBands[m->nbEBands]-eBands[m->nbEBands-1]); |
1649 | 0 | else |
1650 | 0 | resynth_alloc = ALLOC_NONE; |
1651 | 0 | ALLOC(_lowband_scratch, resynth_alloc, celt_norm); |
1652 | 0 | if (encode && resynth) |
1653 | 0 | lowband_scratch = _lowband_scratch; |
1654 | 0 | else |
1655 | 0 | lowband_scratch = X_+M*eBands[m->effEBands-1]; |
1656 | 0 | ALLOC(X_save, resynth_alloc, celt_norm); |
1657 | 0 | ALLOC(Y_save, resynth_alloc, celt_norm); |
1658 | 0 | ALLOC(X_save2, resynth_alloc, celt_norm); |
1659 | 0 | ALLOC(Y_save2, resynth_alloc, celt_norm); |
1660 | 0 | ALLOC(norm_save2, resynth_alloc, celt_norm); |
1661 | |
|
1662 | 0 | lowband_offset = 0; |
1663 | 0 | ctx.bandE = bandE; |
1664 | 0 | ctx.ec = ec; |
1665 | 0 | ctx.encode = encode; |
1666 | 0 | ctx.intensity = intensity; |
1667 | 0 | ctx.m = m; |
1668 | 0 | ctx.seed = *seed; |
1669 | 0 | ctx.spread = spread; |
1670 | 0 | ctx.arch = arch; |
1671 | 0 | ctx.disable_inv = disable_inv; |
1672 | 0 | ctx.resynth = resynth; |
1673 | 0 | ctx.theta_round = 0; |
1674 | | #ifdef ENABLE_QEXT |
1675 | | ctx.ext_ec = ext_ec; |
1676 | | ctx.ext_total_bits = ext_total_bits; |
1677 | | ctx.extra_bands = end == NB_QEXT_BANDS || end == 2; |
1678 | | if (ctx.extra_bands) theta_rdo = 0; |
1679 | | #endif |
1680 | | /* Avoid injecting noise in the first band on transients. */ |
1681 | 0 | ctx.avoid_split_noise = B > 1; |
1682 | 0 | for (i=start;i<end;i++) |
1683 | 0 | { |
1684 | 0 | opus_int32 tell; |
1685 | 0 | int b; |
1686 | 0 | int N; |
1687 | 0 | opus_int32 curr_balance; |
1688 | 0 | int effective_lowband=-1; |
1689 | 0 | celt_norm * OPUS_RESTRICT X, * OPUS_RESTRICT Y; |
1690 | 0 | int tf_change=0; |
1691 | 0 | unsigned x_cm; |
1692 | 0 | unsigned y_cm; |
1693 | 0 | int last; |
1694 | |
|
1695 | 0 | ctx.i = i; |
1696 | 0 | last = (i==end-1); |
1697 | |
|
1698 | 0 | X = X_+M*eBands[i]; |
1699 | 0 | if (Y_!=NULL) |
1700 | 0 | Y = Y_+M*eBands[i]; |
1701 | 0 | else |
1702 | 0 | Y = NULL; |
1703 | 0 | N = M*eBands[i+1]-M*eBands[i]; |
1704 | 0 | celt_assert(N > 0); |
1705 | 0 | tell = ec_tell_frac(ec); |
1706 | | |
1707 | | /* Compute how many bits we want to allocate to this band */ |
1708 | 0 | if (i != start) |
1709 | 0 | balance -= tell; |
1710 | 0 | remaining_bits = total_bits-tell-1; |
1711 | 0 | ctx.remaining_bits = remaining_bits; |
1712 | | #ifdef ENABLE_QEXT |
1713 | | if (i != start) { |
1714 | | ext_balance += extra_pulses[i-1] + ext_tell; |
1715 | | } |
1716 | | ext_tell = ec_tell_frac(ext_ec); |
1717 | | ctx.extra_bits = extra_pulses[i]; |
1718 | | if (i != start) |
1719 | | ext_balance -= ext_tell; |
1720 | | if (i <= codedBands-1) |
1721 | | { |
1722 | | opus_int32 ext_curr_balance = celt_sudiv(ext_balance, IMIN(3, codedBands-i)); |
1723 | | ext_b = IMAX(0, IMIN(16383, IMIN(ext_total_bits-ext_tell,extra_pulses[i]+ext_curr_balance))); |
1724 | | } else { |
1725 | | ext_b = 0; |
1726 | | } |
1727 | | #endif |
1728 | 0 | if (i <= codedBands-1) |
1729 | 0 | { |
1730 | 0 | curr_balance = celt_sudiv(balance, IMIN(3, codedBands-i)); |
1731 | 0 | b = IMAX(0, IMIN(16383, IMIN(remaining_bits+1,pulses[i]+curr_balance))); |
1732 | 0 | } else { |
1733 | 0 | b = 0; |
1734 | 0 | } |
1735 | |
|
1736 | 0 | #ifndef DISABLE_UPDATE_DRAFT |
1737 | 0 | if (resynth && (M*eBands[i]-N >= M*eBands[start] || i==start+1) && (update_lowband || lowband_offset==0)) |
1738 | 0 | lowband_offset = i; |
1739 | 0 | if (i == start+1) |
1740 | 0 | special_hybrid_folding(m, norm, norm2, start, M, dual_stereo); |
1741 | | #else |
1742 | | if (resynth && M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowband_offset==0)) |
1743 | | lowband_offset = i; |
1744 | | #endif |
1745 | |
|
1746 | 0 | tf_change = tf_res[i]; |
1747 | 0 | ctx.tf_change = tf_change; |
1748 | 0 | if (i>=m->effEBands) |
1749 | 0 | { |
1750 | 0 | X=norm; |
1751 | 0 | if (Y_!=NULL) |
1752 | 0 | Y = norm; |
1753 | 0 | lowband_scratch = NULL; |
1754 | 0 | } |
1755 | 0 | if (last && !theta_rdo) |
1756 | 0 | lowband_scratch = NULL; |
1757 | | |
1758 | | /* Get a conservative estimate of the collapse_mask's for the bands we're |
1759 | | going to be folding from. */ |
1760 | 0 | if (lowband_offset != 0 && (spread!=SPREAD_AGGRESSIVE || B>1 || tf_change<0)) |
1761 | 0 | { |
1762 | 0 | int fold_start; |
1763 | 0 | int fold_end; |
1764 | 0 | int fold_i; |
1765 | | /* This ensures we never repeat spectral content within one band */ |
1766 | 0 | effective_lowband = IMAX(0, M*eBands[lowband_offset]-norm_offset-N); |
1767 | 0 | fold_start = lowband_offset; |
1768 | 0 | while(M*eBands[--fold_start] > effective_lowband+norm_offset); |
1769 | 0 | fold_end = lowband_offset-1; |
1770 | 0 | #ifndef DISABLE_UPDATE_DRAFT |
1771 | 0 | while(++fold_end < i && M*eBands[fold_end] < effective_lowband+norm_offset+N); |
1772 | | #else |
1773 | | while(M*eBands[++fold_end] < effective_lowband+norm_offset+N); |
1774 | | #endif |
1775 | 0 | x_cm = y_cm = 0; |
1776 | 0 | fold_i = fold_start; do { |
1777 | 0 | x_cm |= collapse_masks[fold_i*C+0]; |
1778 | 0 | y_cm |= collapse_masks[fold_i*C+C-1]; |
1779 | 0 | } while (++fold_i<fold_end); |
1780 | 0 | } |
1781 | | /* Otherwise, we'll be using the LCG to fold, so all blocks will (almost |
1782 | | always) be non-zero. */ |
1783 | 0 | else |
1784 | 0 | x_cm = y_cm = (1<<B)-1; |
1785 | |
|
1786 | 0 | if (dual_stereo && i==intensity) |
1787 | 0 | { |
1788 | 0 | int j; |
1789 | | |
1790 | | /* Switch off dual stereo to do intensity. */ |
1791 | 0 | dual_stereo = 0; |
1792 | 0 | if (resynth) |
1793 | 0 | for (j=0;j<M*eBands[i]-norm_offset;j++) |
1794 | 0 | norm[j] = HALF32(norm[j]+norm2[j]); |
1795 | 0 | } |
1796 | 0 | if (dual_stereo) |
1797 | 0 | { |
1798 | 0 | x_cm = quant_band(&ctx, X, N, b/2, B, |
1799 | 0 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, |
1800 | 0 | last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm ARG_QEXT(ext_b/2)); |
1801 | 0 | y_cm = quant_band(&ctx, Y, N, b/2, B, |
1802 | 0 | effective_lowband != -1 ? norm2+effective_lowband : NULL, LM, |
1803 | 0 | last?NULL:norm2+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, y_cm ARG_QEXT(ext_b/2)); |
1804 | 0 | } else { |
1805 | 0 | if (Y!=NULL) |
1806 | 0 | { |
1807 | 0 | if (theta_rdo && i < intensity) |
1808 | 0 | { |
1809 | 0 | ec_ctx ec_save, ec_save2; |
1810 | 0 | struct band_ctx ctx_save, ctx_save2; |
1811 | 0 | opus_val32 dist0, dist1; |
1812 | 0 | unsigned cm, cm2; |
1813 | 0 | int nstart_bytes, nend_bytes, save_bytes; |
1814 | 0 | unsigned char *bytes_buf; |
1815 | 0 | unsigned char bytes_save[1275]; |
1816 | | #ifdef ENABLE_QEXT |
1817 | | ec_ctx ext_ec_save, ext_ec_save2; |
1818 | | unsigned char *ext_bytes_buf; |
1819 | | int ext_nstart_bytes, ext_nend_bytes, ext_save_bytes; |
1820 | | unsigned char ext_bytes_save[QEXT_PACKET_SIZE_CAP]; |
1821 | | #endif |
1822 | 0 | opus_val16 w[2]; |
1823 | 0 | compute_channel_weights(bandE[i], bandE[i+m->nbEBands], w); |
1824 | | /* Make a copy. */ |
1825 | 0 | cm = x_cm|y_cm; |
1826 | 0 | ec_save = *ec; |
1827 | | #ifdef ENABLE_QEXT |
1828 | | ext_ec_save = *ext_ec; |
1829 | | #endif |
1830 | 0 | ctx_save = ctx; |
1831 | 0 | OPUS_COPY(X_save, X, N); |
1832 | 0 | OPUS_COPY(Y_save, Y, N); |
1833 | | /* Encode and round down. */ |
1834 | 0 | ctx.theta_round = -1; |
1835 | 0 | x_cm = quant_band_stereo(&ctx, X, Y, N, b, B, |
1836 | 0 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, |
1837 | 0 | last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap)); |
1838 | 0 | dist0 = MULT16_32_Q15(w[0], celt_inner_prod_norm_shift(X_save, X, N, arch)) + MULT16_32_Q15(w[1], celt_inner_prod_norm_shift(Y_save, Y, N, arch)); |
1839 | | |
1840 | | /* Save first result. */ |
1841 | 0 | cm2 = x_cm; |
1842 | 0 | ec_save2 = *ec; |
1843 | | #ifdef ENABLE_QEXT |
1844 | | ext_ec_save2 = *ext_ec; |
1845 | | #endif |
1846 | 0 | ctx_save2 = ctx; |
1847 | 0 | OPUS_COPY(X_save2, X, N); |
1848 | 0 | OPUS_COPY(Y_save2, Y, N); |
1849 | 0 | if (!last) |
1850 | 0 | OPUS_COPY(norm_save2, norm+M*eBands[i]-norm_offset, N); |
1851 | 0 | nstart_bytes = ec_save.offs; |
1852 | 0 | nend_bytes = ec_save.storage; |
1853 | 0 | bytes_buf = ec_save.buf+nstart_bytes; |
1854 | 0 | save_bytes = nend_bytes-nstart_bytes; |
1855 | 0 | OPUS_COPY(bytes_save, bytes_buf, save_bytes); |
1856 | | #ifdef ENABLE_QEXT |
1857 | | ext_nstart_bytes = ext_ec_save.offs; |
1858 | | ext_nend_bytes = ext_ec_save.storage; |
1859 | | ext_bytes_buf = ext_ec_save.buf!=NULL ? ext_ec_save.buf+ext_nstart_bytes : NULL; |
1860 | | ext_save_bytes = ext_nend_bytes-ext_nstart_bytes; |
1861 | | if (ext_save_bytes) OPUS_COPY(ext_bytes_save, ext_bytes_buf, ext_save_bytes); |
1862 | | #endif |
1863 | | /* Restore */ |
1864 | 0 | *ec = ec_save; |
1865 | | #ifdef ENABLE_QEXT |
1866 | | *ext_ec = ext_ec_save; |
1867 | | #endif |
1868 | 0 | ctx = ctx_save; |
1869 | 0 | OPUS_COPY(X, X_save, N); |
1870 | 0 | OPUS_COPY(Y, Y_save, N); |
1871 | 0 | #ifndef DISABLE_UPDATE_DRAFT |
1872 | 0 | if (i == start+1) |
1873 | 0 | special_hybrid_folding(m, norm, norm2, start, M, dual_stereo); |
1874 | 0 | #endif |
1875 | | /* Encode and round up. */ |
1876 | 0 | ctx.theta_round = 1; |
1877 | 0 | x_cm = quant_band_stereo(&ctx, X, Y, N, b, B, |
1878 | 0 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, |
1879 | 0 | last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, cm ARG_QEXT(ext_b) ARG_QEXT(cap)); |
1880 | 0 | dist1 = MULT16_32_Q15(w[0], celt_inner_prod_norm_shift(X_save, X, N, arch)) + MULT16_32_Q15(w[1], celt_inner_prod_norm_shift(Y_save, Y, N, arch)); |
1881 | 0 | if (dist0 >= dist1) { |
1882 | 0 | x_cm = cm2; |
1883 | 0 | *ec = ec_save2; |
1884 | | #ifdef ENABLE_QEXT |
1885 | | *ext_ec = ext_ec_save2; |
1886 | | #endif |
1887 | 0 | ctx = ctx_save2; |
1888 | 0 | OPUS_COPY(X, X_save2, N); |
1889 | 0 | OPUS_COPY(Y, Y_save2, N); |
1890 | 0 | if (!last) |
1891 | 0 | OPUS_COPY(norm+M*eBands[i]-norm_offset, norm_save2, N); |
1892 | 0 | OPUS_COPY(bytes_buf, bytes_save, save_bytes); |
1893 | | #ifdef ENABLE_QEXT |
1894 | | if (ext_save_bytes) OPUS_COPY(ext_bytes_buf, ext_bytes_save, ext_save_bytes); |
1895 | | #endif |
1896 | 0 | } |
1897 | 0 | } else { |
1898 | 0 | ctx.theta_round = 0; |
1899 | 0 | x_cm = quant_band_stereo(&ctx, X, Y, N, b, B, |
1900 | 0 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, |
1901 | 0 | last?NULL:norm+M*eBands[i]-norm_offset, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b) ARG_QEXT(cap)); |
1902 | 0 | } |
1903 | 0 | } else { |
1904 | 0 | x_cm = quant_band(&ctx, X, N, b, B, |
1905 | 0 | effective_lowband != -1 ? norm+effective_lowband : NULL, LM, |
1906 | 0 | last?NULL:norm+M*eBands[i]-norm_offset, Q31ONE, lowband_scratch, x_cm|y_cm ARG_QEXT(ext_b)); |
1907 | 0 | } |
1908 | 0 | y_cm = x_cm; |
1909 | 0 | } |
1910 | 0 | collapse_masks[i*C+0] = (unsigned char)x_cm; |
1911 | 0 | collapse_masks[i*C+C-1] = (unsigned char)y_cm; |
1912 | 0 | balance += pulses[i] + tell; |
1913 | | |
1914 | | /* Update the folding position only as long as we have 1 bit/sample depth. */ |
1915 | 0 | update_lowband = b>(N<<BITRES); |
1916 | | /* We only need to avoid noise on a split for the first band. After that, we |
1917 | | have folding. */ |
1918 | 0 | ctx.avoid_split_noise = 0; |
1919 | 0 | } |
1920 | 0 | *seed = ctx.seed; |
1921 | |
|
1922 | 0 | RESTORE_STACK; |
1923 | 0 | } |