]> git.sesse.net Git - ffmpeg/blob - libavcodec/opus_pvq.c
opus_celt: rename structures to better names and reorganize them
[ffmpeg] / libavcodec / opus_pvq.c
1 /*
2  * Copyright (c) 2012 Andrew D'Addesio
3  * Copyright (c) 2013-2014 Mozilla Corporation
4  * Copyright (c) 2016 Rostislav Pehlivanov <atomnuker@gmail.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "opustab.h"
24 #include "opus_pvq.h"
25
26 #define CELT_PVQ_U(n, k) (ff_celt_pvq_u_row[FFMIN(n, k)][FFMAX(n, k)])
27 #define CELT_PVQ_V(n, k) (CELT_PVQ_U(n, k) + CELT_PVQ_U(n, (k) + 1))
28
29 static inline int16_t celt_cos(int16_t x)
30 {
31     x = (MUL16(x, x) + 4096) >> 13;
32     x = (32767-x) + ROUND_MUL16(x, (-7651 + ROUND_MUL16(x, (8277 + ROUND_MUL16(-626, x)))));
33     return 1+x;
34 }
35
36 static inline int celt_log2tan(int isin, int icos)
37 {
38     int lc, ls;
39     lc = opus_ilog(icos);
40     ls = opus_ilog(isin);
41     icos <<= 15 - lc;
42     isin <<= 15 - ls;
43     return (ls << 11) - (lc << 11) +
44            ROUND_MUL16(isin, ROUND_MUL16(isin, -2597) + 7932) -
45            ROUND_MUL16(icos, ROUND_MUL16(icos, -2597) + 7932);
46 }
47
48 static inline int celt_bits2pulses(const uint8_t *cache, int bits)
49 {
50     // TODO: Find the size of cache and make it into an array in the parameters list
51     int i, low = 0, high;
52
53     high = cache[0];
54     bits--;
55
56     for (i = 0; i < 6; i++) {
57         int center = (low + high + 1) >> 1;
58         if (cache[center] >= bits)
59             high = center;
60         else
61             low = center;
62     }
63
64     return (bits - (low == 0 ? -1 : cache[low]) <= cache[high] - bits) ? low : high;
65 }
66
67 static inline int celt_pulses2bits(const uint8_t *cache, int pulses)
68 {
69     // TODO: Find the size of cache and make it into an array in the parameters list
70    return (pulses == 0) ? 0 : cache[pulses] + 1;
71 }
72
73 static inline void celt_normalize_residual(const int * av_restrict iy, float * av_restrict X,
74                                            int N, float g)
75 {
76     int i;
77     for (i = 0; i < N; i++)
78         X[i] = g * iy[i];
79 }
80
81 static void celt_exp_rotation1(float *X, uint32_t len, uint32_t stride,
82                                float c, float s)
83 {
84     float *Xptr;
85     int i;
86
87     Xptr = X;
88     for (i = 0; i < len - stride; i++) {
89         float x1, x2;
90         x1           = Xptr[0];
91         x2           = Xptr[stride];
92         Xptr[stride] = c * x2 + s * x1;
93         *Xptr++      = c * x1 - s * x2;
94     }
95
96     Xptr = &X[len - 2 * stride - 1];
97     for (i = len - 2 * stride - 1; i >= 0; i--) {
98         float x1, x2;
99         x1           = Xptr[0];
100         x2           = Xptr[stride];
101         Xptr[stride] = c * x2 + s * x1;
102         *Xptr--      = c * x1 - s * x2;
103     }
104 }
105
106 static inline void celt_exp_rotation(float *X, uint32_t len,
107                                      uint32_t stride, uint32_t K,
108                                      enum CeltSpread spread)
109 {
110     uint32_t stride2 = 0;
111     float c, s;
112     float gain, theta;
113     int i;
114
115     if (2*K >= len || spread == CELT_SPREAD_NONE)
116         return;
117
118     gain = (float)len / (len + (20 - 5*spread) * K);
119     theta = M_PI * gain * gain / 4;
120
121     c = cosf(theta);
122     s = sinf(theta);
123
124     if (len >= stride << 3) {
125         stride2 = 1;
126         /* This is just a simple (equivalent) way of computing sqrt(len/stride) with rounding.
127         It's basically incrementing long as (stride2+0.5)^2 < len/stride. */
128         while ((stride2 * stride2 + stride2) * stride + (stride >> 2) < len)
129             stride2++;
130     }
131
132     /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for
133     extract_collapse_mask().*/
134     len /= stride;
135     for (i = 0; i < stride; i++) {
136         if (stride2)
137             celt_exp_rotation1(X + i * len, len, stride2, s, c);
138         celt_exp_rotation1(X + i * len, len, 1, c, s);
139     }
140 }
141
142 static inline uint32_t celt_extract_collapse_mask(const int *iy, uint32_t N, uint32_t B)
143 {
144     uint32_t collapse_mask;
145     int N0;
146     int i, j;
147
148     if (B <= 1)
149         return 1;
150
151     /*NOTE: As a minor optimization, we could be passing around log2(B), not B, for both this and for
152     exp_rotation().*/
153     N0 = N/B;
154     collapse_mask = 0;
155     for (i = 0; i < B; i++)
156         for (j = 0; j < N0; j++)
157             collapse_mask |= (iy[i*N0+j]!=0)<<i;
158     return collapse_mask;
159 }
160
161 static inline void celt_stereo_merge(float *X, float *Y, float mid, int N)
162 {
163     int i;
164     float xp = 0, side = 0;
165     float E[2];
166     float mid2;
167     float t, gain[2];
168
169     /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
170     for (i = 0; i < N; i++) {
171         xp   += X[i] * Y[i];
172         side += Y[i] * Y[i];
173     }
174
175     /* Compensating for the mid normalization */
176     xp *= mid;
177     mid2 = mid;
178     E[0] = mid2 * mid2 + side - 2 * xp;
179     E[1] = mid2 * mid2 + side + 2 * xp;
180     if (E[0] < 6e-4f || E[1] < 6e-4f) {
181         for (i = 0; i < N; i++)
182             Y[i] = X[i];
183         return;
184     }
185
186     t = E[0];
187     gain[0] = 1.0f / sqrtf(t);
188     t = E[1];
189     gain[1] = 1.0f / sqrtf(t);
190
191     for (i = 0; i < N; i++) {
192         float value[2];
193         /* Apply mid scaling (side is already scaled) */
194         value[0] = mid * X[i];
195         value[1] = Y[i];
196         X[i] = gain[0] * (value[0] - value[1]);
197         Y[i] = gain[1] * (value[0] + value[1]);
198     }
199 }
200
201 static void celt_interleave_hadamard(float *tmp, float *X, int N0,
202                                      int stride, int hadamard)
203 {
204     int i, j;
205     int N = N0*stride;
206
207     if (hadamard) {
208         const uint8_t *ordery = ff_celt_hadamard_ordery + stride - 2;
209         for (i = 0; i < stride; i++)
210             for (j = 0; j < N0; j++)
211                 tmp[j*stride+i] = X[ordery[i]*N0+j];
212     } else {
213         for (i = 0; i < stride; i++)
214             for (j = 0; j < N0; j++)
215                 tmp[j*stride+i] = X[i*N0+j];
216     }
217
218     for (i = 0; i < N; i++)
219         X[i] = tmp[i];
220 }
221
222 static void celt_deinterleave_hadamard(float *tmp, float *X, int N0,
223                                        int stride, int hadamard)
224 {
225     int i, j;
226     int N = N0*stride;
227
228     if (hadamard) {
229         const uint8_t *ordery = ff_celt_hadamard_ordery + stride - 2;
230         for (i = 0; i < stride; i++)
231             for (j = 0; j < N0; j++)
232                 tmp[ordery[i]*N0+j] = X[j*stride+i];
233     } else {
234         for (i = 0; i < stride; i++)
235             for (j = 0; j < N0; j++)
236                 tmp[i*N0+j] = X[j*stride+i];
237     }
238
239     for (i = 0; i < N; i++)
240         X[i] = tmp[i];
241 }
242
243 static void celt_haar1(float *X, int N0, int stride)
244 {
245     int i, j;
246     N0 >>= 1;
247     for (i = 0; i < stride; i++) {
248         for (j = 0; j < N0; j++) {
249             float x0 = X[stride * (2 * j + 0) + i];
250             float x1 = X[stride * (2 * j + 1) + i];
251             X[stride * (2 * j + 0) + i] = (x0 + x1) * M_SQRT1_2;
252             X[stride * (2 * j + 1) + i] = (x0 - x1) * M_SQRT1_2;
253         }
254     }
255 }
256
257 static inline int celt_compute_qn(int N, int b, int offset, int pulse_cap,
258                                   int dualstereo)
259 {
260     int qn, qb;
261     int N2 = 2 * N - 1;
262     if (dualstereo && N == 2)
263         N2--;
264
265     /* The upper limit ensures that in a stereo split with itheta==16384, we'll
266      * always have enough bits left over to code at least one pulse in the
267      * side; otherwise it would collapse, since it doesn't get folded. */
268     qb = FFMIN3(b - pulse_cap - (4 << 3), (b + N2 * offset) / N2, 8 << 3);
269     qn = (qb < (1 << 3 >> 1)) ? 1 : ((ff_celt_qn_exp2[qb & 0x7] >> (14 - (qb >> 3))) + 1) >> 1 << 1;
270     return qn;
271 }
272
273 // this code was adapted from libopus
274 static inline uint64_t celt_cwrsi(uint32_t N, uint32_t K, uint32_t i, int *y)
275 {
276     uint64_t norm = 0;
277     uint32_t p;
278     int s, val;
279     int k0;
280
281     while (N > 2) {
282         uint32_t q;
283
284         /*Lots of pulses case:*/
285         if (K >= N) {
286             const uint32_t *row = ff_celt_pvq_u_row[N];
287
288             /* Are the pulses in this dimension negative? */
289             p  = row[K + 1];
290             s  = -(i >= p);
291             i -= p & s;
292
293             /*Count how many pulses were placed in this dimension.*/
294             k0 = K;
295             q = row[N];
296             if (q > i) {
297                 K = N;
298                 do {
299                     p = ff_celt_pvq_u_row[--K][N];
300                 } while (p > i);
301             } else
302                 for (p = row[K]; p > i; p = row[K])
303                     K--;
304
305             i    -= p;
306             val   = (k0 - K + s) ^ s;
307             norm += val * val;
308             *y++  = val;
309         } else { /*Lots of dimensions case:*/
310             /*Are there any pulses in this dimension at all?*/
311             p = ff_celt_pvq_u_row[K    ][N];
312             q = ff_celt_pvq_u_row[K + 1][N];
313
314             if (p <= i && i < q) {
315                 i -= p;
316                 *y++ = 0;
317             } else {
318                 /*Are the pulses in this dimension negative?*/
319                 s  = -(i >= q);
320                 i -= q & s;
321
322                 /*Count how many pulses were placed in this dimension.*/
323                 k0 = K;
324                 do p = ff_celt_pvq_u_row[--K][N];
325                 while (p > i);
326
327                 i    -= p;
328                 val   = (k0 - K + s) ^ s;
329                 norm += val * val;
330                 *y++  = val;
331             }
332         }
333         N--;
334     }
335
336     /* N == 2 */
337     p  = 2 * K + 1;
338     s  = -(i >= p);
339     i -= p & s;
340     k0 = K;
341     K  = (i + 1) / 2;
342
343     if (K)
344         i -= 2 * K - 1;
345
346     val   = (k0 - K + s) ^ s;
347     norm += val * val;
348     *y++  = val;
349
350     /* N==1 */
351     s     = -i;
352     val   = (K + s) ^ s;
353     norm += val * val;
354     *y    = val;
355
356     return norm;
357 }
358
359 static inline float celt_decode_pulses(OpusRangeCoder *rc, int *y, uint32_t N, uint32_t K)
360 {
361     const uint32_t idx = ff_opus_rc_dec_uint(rc, CELT_PVQ_V(N, K));
362     return celt_cwrsi(N, K, idx, y);
363 }
364
365 /** Decode pulse vector and combine the result with the pitch vector to produce
366     the final normalised signal in the current band. */
367 static uint32_t celt_alg_unquant(OpusRangeCoder *rc, float *X, uint32_t N, uint32_t K,
368                                  enum CeltSpread spread, uint32_t blocks, float gain)
369 {
370     int y[176];
371
372     gain /= sqrtf(celt_decode_pulses(rc, y, N, K));
373     celt_normalize_residual(y, X, N, gain);
374     celt_exp_rotation(X, N, blocks, K, spread);
375     return celt_extract_collapse_mask(y, N, blocks);
376 }
377
378 uint32_t ff_celt_decode_band(CeltFrame *f, OpusRangeCoder *rc, const int band,
379                              float *X, float *Y, int N, int b, uint32_t blocks,
380                              float *lowband, int duration, float *lowband_out, int level,
381                              float gain, float *lowband_scratch, int fill)
382 {
383     const uint8_t *cache;
384     int dualstereo, split;
385     int imid = 0, iside = 0;
386     uint32_t N0 = N;
387     int N_B;
388     int N_B0;
389     int B0 = blocks;
390     int time_divide = 0;
391     int recombine = 0;
392     int inv = 0;
393     float mid = 0, side = 0;
394     int longblocks = (B0 == 1);
395     uint32_t cm = 0;
396
397     N_B0 = N_B = N / blocks;
398     split = dualstereo = (Y != NULL);
399
400     if (N == 1) {
401         /* special case for one sample */
402         int i;
403         float *x = X;
404         for (i = 0; i <= dualstereo; i++) {
405             int sign = 0;
406             if (f->remaining2 >= 1<<3) {
407                 sign           = ff_opus_rc_get_raw(rc, 1);
408                 f->remaining2 -= 1 << 3;
409                 b             -= 1 << 3;
410             }
411             x[0] = sign ? -1.0f : 1.0f;
412             x = Y;
413         }
414         if (lowband_out)
415             lowband_out[0] = X[0];
416         return 1;
417     }
418
419     if (!dualstereo && level == 0) {
420         int tf_change = f->tf_change[band];
421         int k;
422         if (tf_change > 0)
423             recombine = tf_change;
424         /* Band recombining to increase frequency resolution */
425
426         if (lowband &&
427             (recombine || ((N_B & 1) == 0 && tf_change < 0) || B0 > 1)) {
428             int j;
429             for (j = 0; j < N; j++)
430                 lowband_scratch[j] = lowband[j];
431             lowband = lowband_scratch;
432         }
433
434         for (k = 0; k < recombine; k++) {
435             if (lowband)
436                 celt_haar1(lowband, N >> k, 1 << k);
437             fill = ff_celt_bit_interleave[fill & 0xF] | ff_celt_bit_interleave[fill >> 4] << 2;
438         }
439         blocks >>= recombine;
440         N_B <<= recombine;
441
442         /* Increasing the time resolution */
443         while ((N_B & 1) == 0 && tf_change < 0) {
444             if (lowband)
445                 celt_haar1(lowband, N_B, blocks);
446             fill |= fill << blocks;
447             blocks <<= 1;
448             N_B >>= 1;
449             time_divide++;
450             tf_change++;
451         }
452         B0 = blocks;
453         N_B0 = N_B;
454
455         /* Reorganize the samples in time order instead of frequency order */
456         if (B0 > 1 && lowband)
457             celt_deinterleave_hadamard(f->scratch, lowband, N_B >> recombine,
458                                        B0 << recombine, longblocks);
459     }
460
461     /* If we need 1.5 more bit than we can produce, split the band in two. */
462     cache = ff_celt_cache_bits +
463             ff_celt_cache_index[(duration + 1) * CELT_MAX_BANDS + band];
464     if (!dualstereo && duration >= 0 && b > cache[cache[0]] + 12 && N > 2) {
465         N >>= 1;
466         Y = X + N;
467         split = 1;
468         duration -= 1;
469         if (blocks == 1)
470             fill = (fill & 1) | (fill << 1);
471         blocks = (blocks + 1) >> 1;
472     }
473
474     if (split) {
475         int qn;
476         int itheta = 0;
477         int mbits, sbits, delta;
478         int qalloc;
479         int pulse_cap;
480         int offset;
481         int orig_fill;
482         int tell;
483
484         /* Decide on the resolution to give to the split parameter theta */
485         pulse_cap = ff_celt_log_freq_range[band] + duration * 8;
486         offset = (pulse_cap >> 1) - (dualstereo && N == 2 ? CELT_QTHETA_OFFSET_TWOPHASE :
487                                                           CELT_QTHETA_OFFSET);
488         qn = (dualstereo && band >= f->intensity_stereo) ? 1 :
489              celt_compute_qn(N, b, offset, pulse_cap, dualstereo);
490         tell = opus_rc_tell_frac(rc);
491         if (qn != 1) {
492             /* Entropy coding of the angle. We use a uniform pdf for the
493             time split, a step for stereo, and a triangular one for the rest. */
494             if (dualstereo && N > 2)
495                 itheta = ff_opus_rc_dec_uint_step(rc, qn/2);
496             else if (dualstereo || B0 > 1)
497                 itheta = ff_opus_rc_dec_uint(rc, qn+1);
498             else
499                 itheta = ff_opus_rc_dec_uint_tri(rc, qn);
500             itheta = itheta * 16384 / qn;
501             /* NOTE: Renormalising X and Y *may* help fixed-point a bit at very high rate.
502             Let's do that at higher complexity */
503         } else if (dualstereo) {
504             inv = (b > 2 << 3 && f->remaining2 > 2 << 3) ? ff_opus_rc_dec_log(rc, 2) : 0;
505             itheta = 0;
506         }
507         qalloc = opus_rc_tell_frac(rc) - tell;
508         b -= qalloc;
509
510         orig_fill = fill;
511         if (itheta == 0) {
512             imid = 32767;
513             iside = 0;
514             fill = av_mod_uintp2(fill, blocks);
515             delta = -16384;
516         } else if (itheta == 16384) {
517             imid = 0;
518             iside = 32767;
519             fill &= ((1 << blocks) - 1) << blocks;
520             delta = 16384;
521         } else {
522             imid = celt_cos(itheta);
523             iside = celt_cos(16384-itheta);
524             /* This is the mid vs side allocation that minimizes squared error
525             in that band. */
526             delta = ROUND_MUL16((N - 1) << 7, celt_log2tan(iside, imid));
527         }
528
529         mid  = imid  / 32768.0f;
530         side = iside / 32768.0f;
531
532         /* This is a special case for N=2 that only works for stereo and takes
533         advantage of the fact that mid and side are orthogonal to encode
534         the side with just one bit. */
535         if (N == 2 && dualstereo) {
536             int c;
537             int sign = 0;
538             float tmp;
539             float *x2, *y2;
540             mbits = b;
541             /* Only need one bit for the side */
542             sbits = (itheta != 0 && itheta != 16384) ? 1 << 3 : 0;
543             mbits -= sbits;
544             c = (itheta > 8192);
545             f->remaining2 -= qalloc+sbits;
546
547             x2 = c ? Y : X;
548             y2 = c ? X : Y;
549             if (sbits)
550                 sign = ff_opus_rc_get_raw(rc, 1);
551             sign = 1 - 2 * sign;
552             /* We use orig_fill here because we want to fold the side, but if
553             itheta==16384, we'll have cleared the low bits of fill. */
554             cm = ff_celt_decode_band(f, rc, band, x2, NULL, N, mbits, blocks,
555                                      lowband, duration, lowband_out, level, gain,
556                                      lowband_scratch, orig_fill);
557             /* We don't split N=2 bands, so cm is either 1 or 0 (for a fold-collapse),
558             and there's no need to worry about mixing with the other channel. */
559             y2[0] = -sign * x2[1];
560             y2[1] =  sign * x2[0];
561             X[0] *= mid;
562             X[1] *= mid;
563             Y[0] *= side;
564             Y[1] *= side;
565             tmp = X[0];
566             X[0] = tmp - Y[0];
567             Y[0] = tmp + Y[0];
568             tmp = X[1];
569             X[1] = tmp - Y[1];
570             Y[1] = tmp + Y[1];
571         } else {
572             /* "Normal" split code */
573             float *next_lowband2     = NULL;
574             float *next_lowband_out1 = NULL;
575             int next_level = 0;
576             int rebalance;
577
578             /* Give more bits to low-energy MDCTs than they would
579              * otherwise deserve */
580             if (B0 > 1 && !dualstereo && (itheta & 0x3fff)) {
581                 if (itheta > 8192)
582                     /* Rough approximation for pre-echo masking */
583                     delta -= delta >> (4 - duration);
584                 else
585                     /* Corresponds to a forward-masking slope of
586                      * 1.5 dB per 10 ms */
587                     delta = FFMIN(0, delta + (N << 3 >> (5 - duration)));
588             }
589             mbits = av_clip((b - delta) / 2, 0, b);
590             sbits = b - mbits;
591             f->remaining2 -= qalloc;
592
593             if (lowband && !dualstereo)
594                 next_lowband2 = lowband + N; /* >32-bit split case */
595
596             /* Only stereo needs to pass on lowband_out.
597              * Otherwise, it's handled at the end */
598             if (dualstereo)
599                 next_lowband_out1 = lowband_out;
600             else
601                 next_level = level + 1;
602
603             rebalance = f->remaining2;
604             if (mbits >= sbits) {
605                 /* In stereo mode, we do not apply a scaling to the mid
606                  * because we need the normalized mid for folding later */
607                 cm = ff_celt_decode_band(f, rc, band, X, NULL, N, mbits, blocks,
608                                          lowband, duration, next_lowband_out1,
609                                          next_level, dualstereo ? 1.0f : (gain * mid),
610                                          lowband_scratch, fill);
611
612                 rebalance = mbits - (rebalance - f->remaining2);
613                 if (rebalance > 3 << 3 && itheta != 0)
614                     sbits += rebalance - (3 << 3);
615
616                 /* For a stereo split, the high bits of fill are always zero,
617                  * so no folding will be done to the side. */
618                 cm |= ff_celt_decode_band(f, rc, band, Y, NULL, N, sbits, blocks,
619                                           next_lowband2, duration, NULL,
620                                           next_level, gain * side, NULL,
621                                           fill >> blocks) << ((B0 >> 1) & (dualstereo - 1));
622             } else {
623                 /* For a stereo split, the high bits of fill are always zero,
624                  * so no folding will be done to the side. */
625                 cm = ff_celt_decode_band(f, rc, band, Y, NULL, N, sbits, blocks,
626                                          next_lowband2, duration, NULL,
627                                          next_level, gain * side, NULL,
628                                          fill >> blocks) << ((B0 >> 1) & (dualstereo - 1));
629
630                 rebalance = sbits - (rebalance - f->remaining2);
631                 if (rebalance > 3 << 3 && itheta != 16384)
632                     mbits += rebalance - (3 << 3);
633
634                 /* In stereo mode, we do not apply a scaling to the mid because
635                  * we need the normalized mid for folding later */
636                 cm |= ff_celt_decode_band(f, rc, band, X, NULL, N, mbits, blocks,
637                                           lowband, duration, next_lowband_out1,
638                                           next_level, dualstereo ? 1.0f : (gain * mid),
639                                           lowband_scratch, fill);
640             }
641         }
642     } else {
643         /* This is the basic no-split case */
644         uint32_t q         = celt_bits2pulses(cache, b);
645         uint32_t curr_bits = celt_pulses2bits(cache, q);
646         f->remaining2 -= curr_bits;
647
648         /* Ensures we can never bust the budget */
649         while (f->remaining2 < 0 && q > 0) {
650             f->remaining2 += curr_bits;
651             curr_bits      = celt_pulses2bits(cache, --q);
652             f->remaining2 -= curr_bits;
653         }
654
655         if (q != 0) {
656             /* Finally do the actual quantization */
657             cm = celt_alg_unquant(rc, X, N, (q < 8) ? q : (8 + (q & 7)) << ((q >> 3) - 1),
658                                   f->spread, blocks, gain);
659         } else {
660             /* If there's no pulse, fill the band anyway */
661             int j;
662             uint32_t cm_mask = (1 << blocks) - 1;
663             fill &= cm_mask;
664             if (!fill) {
665                 for (j = 0; j < N; j++)
666                     X[j] = 0.0f;
667             } else {
668                 if (!lowband) {
669                     /* Noise */
670                     for (j = 0; j < N; j++)
671                         X[j] = (((int32_t)celt_rng(f)) >> 20);
672                     cm = cm_mask;
673                 } else {
674                     /* Folded spectrum */
675                     for (j = 0; j < N; j++) {
676                         /* About 48 dB below the "normal" folding level */
677                         X[j] = lowband[j] + (((celt_rng(f)) & 0x8000) ? 1.0f / 256 : -1.0f / 256);
678                     }
679                     cm = fill;
680                 }
681                 celt_renormalize_vector(X, N, gain);
682             }
683         }
684     }
685
686     /* This code is used by the decoder and by the resynthesis-enabled encoder */
687     if (dualstereo) {
688         int j;
689         if (N != 2)
690             celt_stereo_merge(X, Y, mid, N);
691         if (inv) {
692             for (j = 0; j < N; j++)
693                 Y[j] *= -1;
694         }
695     } else if (level == 0) {
696         int k;
697
698         /* Undo the sample reorganization going from time order to frequency order */
699         if (B0 > 1)
700             celt_interleave_hadamard(f->scratch, X, N_B>>recombine,
701                                      B0<<recombine, longblocks);
702
703         /* Undo time-freq changes that we did earlier */
704         N_B = N_B0;
705         blocks = B0;
706         for (k = 0; k < time_divide; k++) {
707             blocks >>= 1;
708             N_B <<= 1;
709             cm |= cm >> blocks;
710             celt_haar1(X, N_B, blocks);
711         }
712
713         for (k = 0; k < recombine; k++) {
714             cm = ff_celt_bit_deinterleave[cm];
715             celt_haar1(X, N0>>k, 1<<k);
716         }
717         blocks <<= recombine;
718
719         /* Scale output for later folding */
720         if (lowband_out) {
721             int j;
722             float n = sqrtf(N0);
723             for (j = 0; j < N0; j++)
724                 lowband_out[j] = n * X[j];
725         }
726         cm = av_mod_uintp2(cm, blocks);
727     }
728     return cm;
729 }