bands.c 38.7 KB
Newer Older
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1
2
3
4
/* Copyright (c) 2007-2008 CSIRO
   Copyright (c) 2007-2009 Xiph.Org Foundation
   Copyright (c) 2008-2009 Gregory Maxwell 
   Written by Jean-Marc Valin and Gregory Maxwell */
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
   
   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
   
   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   
   - Neither the name of the Xiph.org Foundation nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.
   
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

34
35
36
37
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

38
39
#include <math.h>
#include "bands.h"
40
#include "modes.h"
41
#include "vq.h"
Jean-Marc Valin's avatar
Jean-Marc Valin committed
42
#include "cwrs.h"
43
#include "stack_alloc.h"
44
#include "os_support.h"
45
#include "mathops.h"
46
#include "rate.h"
47

48
celt_uint32 lcg_rand(celt_uint32 seed)
49
50
51
52
{
   return 1664525 * seed + 1013904223;
}

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* This is a cos() approximation designed to be bit-exact on any platform. Bit exactness
   with this approximation is important because it has an impact on the bit allocation */
static celt_int16 bitexact_cos(celt_int16 x)
{
   celt_int32 tmp;
   celt_int16 x2;
   tmp = (4096+((celt_int32)(x)*(x)))>>13;
   if (tmp > 32767)
      tmp = 32767;
   x2 = tmp;
   x2 = (32767-x2) + FRAC_MUL16(x2, (-7651 + FRAC_MUL16(x2, (8277 + FRAC_MUL16(-626, x2)))));
   if (x2 > 32766)
      x2 = 32766;
   return 1+x2;
}

69
70
71
72
73
74
75
76
77
78
79
80
static int bitexact_log2tan(int isin,int icos)
{
   int lc;
   int ls;
   lc=EC_ILOG(icos);
   ls=EC_ILOG(isin);
   icos<<=15-lc;
   isin<<=15-ls;
   return (ls-lc<<11)
         +FRAC_MUL16(isin, FRAC_MUL16(isin, -2597) + 7932)
         -FRAC_MUL16(icos, FRAC_MUL16(icos, -2597) + 7932);
}
81

82
#ifdef FIXED_POINT
83
/* Compute the amplitude (sqrt energy) in each of the bands */
84
void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bank, int end, int _C, int M)
85
{
86
   int i, c, N;
87
   const celt_int16 *eBands = m->eBands;
88
   const int C = CHANNELS(_C);
89
   N = M*m->shortMdctSize;
90
   c=0; do {
91
      for (i=0;i<end;i++)
92
93
      {
         int j;
94
95
         celt_word32 maxval=0;
         celt_word32 sum = 0;
96
         
97
         j=M*eBands[i]; do {
98
99
            maxval = MAX32(maxval, X[j+c*N]);
            maxval = MAX32(maxval, -X[j+c*N]);
100
         } while (++j<M*eBands[i+1]);
101
         
102
103
         if (maxval > 0)
         {
104
            int shift = celt_ilog2(maxval)-10;
105
            j=M*eBands[i]; do {
106
107
               sum = MAC16_16(sum, EXTRACT16(VSHR32(X[j+c*N],shift)),
                                   EXTRACT16(VSHR32(X[j+c*N],shift)));
108
            } while (++j<M*eBands[i+1]);
109
110
            /* We're adding one here to make damn sure we never end up with a pitch vector that's
               larger than unity norm */
111
            bank[i+c*m->nbEBands] = EPSILON+VSHR32(EXTEND32(celt_sqrt(sum)),-shift);
112
         } else {
113
            bank[i+c*m->nbEBands] = EPSILON;
114
         }
115
         /*printf ("%f ", bank[i+c*m->nbEBands]);*/
116
      }
117
   } while (++c<C);
118
119
120
121
   /*printf ("\n");*/
}

/* Normalise each band such that the energy is one. */
122
void normalise_bands(const CELTMode *m, const celt_sig * restrict freq, celt_norm * restrict X, const celt_ener *bank, int end, int _C, int M)
123
{
124
   int i, c, N;
125
   const celt_int16 *eBands = m->eBands;
126
   const int C = CHANNELS(_C);
127
   N = M*m->shortMdctSize;
128
   c=0; do {
Jean-Marc Valin's avatar
Jean-Marc Valin committed
129
      i=0; do {
130
         celt_word16 g;
131
         int j,shift;
132
         celt_word16 E;
133
134
         shift = celt_zlog2(bank[i+c*m->nbEBands])-13;
         E = VSHR32(bank[i+c*m->nbEBands], shift);
135
         g = EXTRACT16(celt_rcp(SHL32(E,3)));
136
         j=M*eBands[i]; do {
137
            X[j+c*N] = MULT16_16_Q15(VSHR32(freq[j+c*N],shift-1),g);
138
         } while (++j<M*eBands[i+1]);
139
      } while (++i<end);
140
   } while (++c<C);
141
142
}

143
#else /* FIXED_POINT */
144
/* Compute the amplitude (sqrt energy) in each of the bands */
145
void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bank, int end, int _C, int M)
146
{
147
   int i, c, N;
148
   const celt_int16 *eBands = m->eBands;
149
   const int C = CHANNELS(_C);
150
   N = M*m->shortMdctSize;
151
   c=0; do {
152
      for (i=0;i<end;i++)
153
154
      {
         int j;
155
         celt_word32 sum = 1e-27f;
156
         for (j=M*eBands[i];j<M*eBands[i+1];j++)
157
            sum += X[j+c*N]*X[j+c*N];
Jean-Marc Valin's avatar
Jean-Marc Valin committed
158
         bank[i+c*m->nbEBands] = celt_sqrt(sum);
159
         /*printf ("%f ", bank[i+c*m->nbEBands]);*/
160
      }
161
   } while (++c<C);
162
163
164
   /*printf ("\n");*/
}

165
/* Normalise each band such that the energy is one. */
166
void normalise_bands(const CELTMode *m, const celt_sig * restrict freq, celt_norm * restrict X, const celt_ener *bank, int end, int _C, int M)
167
{
168
   int i, c, N;
169
   const celt_int16 *eBands = m->eBands;
170
   const int C = CHANNELS(_C);
171
   N = M*m->shortMdctSize;
172
   c=0; do {
173
      for (i=0;i<end;i++)
174
175
      {
         int j;
176
         celt_word16 g = 1.f/(1e-27f+bank[i+c*m->nbEBands]);
177
         for (j=M*eBands[i];j<M*eBands[i+1];j++)
178
            X[j+c*N] = freq[j+c*N]*g;
179
      }
180
   } while (++c<C);
181
182
}

183
184
#endif /* FIXED_POINT */

185
/* De-normalise the energy to produce the synthesis from the unit-energy bands */
186
void denormalise_bands(const CELTMode *m, const celt_norm * restrict X, celt_sig * restrict freq, const celt_ener *bank, int end, int _C, int M)
187
{
188
   int i, c, N;
189
   const celt_int16 *eBands = m->eBands;
190
   const int C = CHANNELS(_C);
191
   N = M*m->shortMdctSize;
192
   celt_assert2(C<=2, "denormalise_bands() not implemented for >2 channels");
193
   c=0; do {
194
195
196
197
      celt_sig * restrict f;
      const celt_norm * restrict x;
      f = freq+c*N;
      x = X+c*N;
198
      for (i=0;i<end;i++)
199
      {
200
         int j, band_end;
201
         celt_word32 g = SHR32(bank[i+c*m->nbEBands],1);
202
         j=M*eBands[i];
203
         band_end = M*eBands[i+1];
204
205
206
         do {
            *f++ = SHL32(MULT16_32_Q15(*x, g),2);
            x++;
207
         } while (++j<band_end);
208
      }
209
      for (i=M*eBands[m->nbEBands];i<N;i++)
210
         *f++ = 0;
211
   } while (++c<C);
212
213
}

214
/* This prevents energy collapse for transients with multiple short MDCTs */
215
void anti_collapse(const CELTMode *m, celt_norm *_X, unsigned char *collapse_masks, int LM, int C, int CC, int size,
216
217
218
219
      int start, int end, celt_word16 *logE, celt_word16 *prev1logE,
      celt_word16 *prev2logE, int *pulses, celt_uint32 seed)
{
   int c, i, j, k;
220
   for (i=start;i<end;i++)
221
   {
222
223
224
225
226
227
228
229
      int N0;
      celt_word16 thresh, sqrt_1;
      int depth;
#ifdef FIXED_POINT
      int shift;
#endif

      N0 = m->eBands[i+1]-m->eBands[i];
230
231
      /* depth in 1/8 bits */
      depth = (1+pulses[i])/(m->eBands[i+1]-m->eBands[i]<<LM);
232
233

#ifdef FIXED_POINT
234
      thresh = MULT16_32_Q15(QCONST16(0.5f, 15), MIN32(32767,SHR32(celt_exp2(-SHL16(depth, 10-BITRES)),1) ));
235
236
237
238
239
240
241
242
      {
         celt_word32 t;
         t = N0<<LM;
         shift = celt_ilog2(t)>>1;
         t = SHL32(t, (7-shift)<<1);
         sqrt_1 = celt_rsqrt_norm(t);
      }
#else
Jean-Marc Valin's avatar
Jean-Marc Valin committed
243
      thresh = .5f*celt_exp2(-.125f*depth);
244
245
246
247
      sqrt_1 = celt_rsqrt(N0<<LM);
#endif

      c=0; do
248
249
      {
         celt_norm *X;
250
251
         celt_word16 prev1;
         celt_word16 prev2;
252
253
         celt_word16 Ediff;
         celt_word16 r;
254
255
256
257
258
259
260
261
         prev1 = prev1logE[c*m->nbEBands+i];
         prev2 = prev2logE[c*m->nbEBands+i];
         if (C<CC)
         {
            prev1 = MAX16(prev1,prev1logE[m->nbEBands+i]);
            prev2 = MAX16(prev2,prev2logE[m->nbEBands+i]);
         }
         Ediff = logE[c*m->nbEBands+i]-MIN16(prev1,prev2);
262
263
264
265
         Ediff = MAX16(0, Ediff);

#ifdef FIXED_POINT
         if (Ediff < 16384)
266
            r = 2*MIN16(16383,SHR32(celt_exp2(-Ediff),1));
267
268
         else
            r = 0;
269
         if (LM==3)
270
            r = MULT16_16_Q14(23170, MIN32(23169, r));
271
         r = SHR16(MIN16(thresh, r),1);
272
         r = SHR32(MULT16_16_Q15(sqrt_1, r),shift);
273
#else
274
275
         /* r needs to be multiplied by 2 or 2*sqrt(2) depending on LM because
            short blocks don't have the same energy as long */
276
         r = 2.f*celt_exp2(-Ediff);
277
         if (LM==3)
278
            r *= 1.41421356f;
279
         r = MIN16(thresh, r);
280
         r = r*sqrt_1;
281
282
283
284
285
#endif
         X = _X+c*size+(m->eBands[i]<<LM);
         for (k=0;k<1<<LM;k++)
         {
            /* Detect collapse */
286
            if (!(collapse_masks[i*C+c]&1<<k))
287
288
289
290
291
292
293
294
295
296
297
            {
               /* Fill with noise */
               for (j=0;j<N0;j++)
               {
                  seed = lcg_rand(seed);
                  X[(j<<LM)+k] = (seed&0x8000 ? r : -r);
               }
            }
         }
         /* We just added some energy, so we need to renormalise */
         renormalise_vector(X, N0<<LM, Q15ONE);
298
299
      } while (++c<C);
   }
300
301
302
303

}


304
static void intensity_stereo(const CELTMode *m, celt_norm *X, celt_norm *Y, const celt_ener *bank, int bandID, int N)
305
306
{
   int i = bandID;
307
   int j;
308
   celt_word16 a1, a2;
309
310
   celt_word16 left, right;
   celt_word16 norm;
311
#ifdef FIXED_POINT
312
   int shift = celt_zlog2(MAX32(bank[i], bank[i+m->nbEBands]))-13;
313
#endif
314
315
316
317
318
   left = VSHR32(bank[i],shift);
   right = VSHR32(bank[i+m->nbEBands],shift);
   norm = EPSILON + celt_sqrt(EPSILON+MULT16_16(left,left)+MULT16_16(right,right));
   a1 = DIV32_16(SHL32(EXTEND32(left),14),norm);
   a2 = DIV32_16(SHL32(EXTEND32(right),14),norm);
319
   for (j=0;j<N;j++)
320
   {
321
      celt_norm r, l;
322
323
324
      l = X[j];
      r = Y[j];
      X[j] = MULT16_16_Q14(a1,l) + MULT16_16_Q14(a2,r);
325
326
327
328
329
330
331
332
333
334
      /* Side is not encoded, no need to calculate */
   }
}

static void stereo_split(celt_norm *X, celt_norm *Y, int N)
{
   int j;
   for (j=0;j<N;j++)
   {
      celt_norm r, l;
335
336
      l = MULT16_16_Q15(QCONST16(.70710678f,15), X[j]);
      r = MULT16_16_Q15(QCONST16(.70710678f,15), Y[j]);
337
338
      X[j] = l+r;
      Y[j] = r-l;
339
340
341
   }
}

342
static void stereo_merge(celt_norm *X, celt_norm *Y, celt_word16 mid, int N)
343
344
{
   int j;
345
   celt_word32 xp=0, side=0;
346
   celt_word32 El, Er;
347
   celt_word16 mid2;
348
349
350
351
352
353
354
#ifdef FIXED_POINT
   int kl, kr;
#endif
   celt_word32 t, lgain, rgain;

   /* Compute the norm of X+Y and X-Y as |X|^2 + |Y|^2 +/- sum(xy) */
   for (j=0;j<N;j++)
355
   {
356
      xp = MAC16_16(xp, X[j], Y[j]);
357
358
      side = MAC16_16(side, Y[j], Y[j]);
   }
359
360
   /* Compensating for the mid normalization */
   xp = MULT16_32_Q15(mid, xp);
361
   /* mid and side are in Q15, not Q14 like X and Y */
362
363
364
   mid2 = SHR32(mid, 1);
   El = MULT16_16(mid2, mid2) + side - 2*xp;
   Er = MULT16_16(mid2, mid2) + side + 2*xp;
365
366
367
368
369
370
   if (Er < QCONST32(6e-4f, 28) || El < QCONST32(6e-4f, 28))
   {
      for (j=0;j<N;j++)
         Y[j] = X[j];
      return;
   }
371
372
373
374
375
376
377
378
379
380

#ifdef FIXED_POINT
   kl = celt_ilog2(El)>>1;
   kr = celt_ilog2(Er)>>1;
#endif
   t = VSHR32(El, (kl-7)<<1);
   lgain = celt_rsqrt_norm(t);
   t = VSHR32(Er, (kr-7)<<1);
   rgain = celt_rsqrt_norm(t);

381
382
383
384
385
386
387
#ifdef FIXED_POINT
   if (kl < 7)
      kl = 7;
   if (kr < 7)
      kr = 7;
#endif

388
389
390
   for (j=0;j<N;j++)
   {
      celt_norm r, l;
391
392
      /* Apply mid scaling (side is already scaled) */
      l = MULT16_16_Q15(mid, X[j]);
393
      r = Y[j];
394
395
      X[j] = EXTRACT16(PSHR32(MULT16_16(lgain, SUB16(l,r)), kl+1));
      Y[j] = EXTRACT16(PSHR32(MULT16_16(rgain, ADD16(l,r)), kr+1));
396
397
398
   }
}

Jean-Marc Valin's avatar
Jean-Marc Valin committed
399
/* Decide whether we should spread the pulses in the current frame */
Jean-Marc Valin's avatar
Jean-Marc Valin committed
400
401
402
int spreading_decision(const CELTMode *m, celt_norm *X, int *average,
      int last_decision, int *hf_average, int *tapset_decision, int update_hf,
      int end, int _C, int M)
403
{
404
   int i, c, N0;
405
   int sum = 0, nbBands=0;
406
   const int C = CHANNELS(_C);
407
   const celt_int16 * restrict eBands = m->eBands;
408
   int decision;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
409
   int hf_sum=0;
410
   
411
   N0 = M*m->shortMdctSize;
412

413
   if (M*(eBands[end]-eBands[end-1]) <= 8)
414
      return SPREAD_NONE;
415
   c=0; do {
416
      for (i=0;i<end;i++)
417
      {
418
419
420
         int j, N, tmp=0;
         int tcount[3] = {0};
         celt_norm * restrict x = X+M*eBands[i]+c*N0;
421
422
423
         N = M*(eBands[i+1]-eBands[i]);
         if (N<=8)
            continue;
424
425
         /* Compute rough CDF of |x[j]| */
         for (j=0;j<N;j++)
426
         {
427
428
429
430
431
432
433
434
435
            celt_word32 x2N; /* Q13 */

            x2N = MULT16_16(MULT16_16_Q15(x[j], x[j]), N);
            if (x2N < QCONST16(0.25f,13))
               tcount[0]++;
            if (x2N < QCONST16(0.0625f,13))
               tcount[1]++;
            if (x2N < QCONST16(0.015625f,13))
               tcount[2]++;
436
         }
437

Jean-Marc Valin's avatar
Jean-Marc Valin committed
438
439
440
         /* Only include four last bands (8 kHz and up) */
         if (i>m->nbEBands-4)
            hf_sum += 32*(tcount[1]+tcount[0])/N;
441
442
         tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
         sum += tmp*256;
443
         nbBands++;
444
      }
445
   } while (++c<C);
Jean-Marc Valin's avatar
Jean-Marc Valin committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464

   if (update_hf)
   {
      if (hf_sum)
         hf_sum /= C*(4-m->nbEBands+end);
      *hf_average = (*hf_average+hf_sum)>>1;
      hf_sum = *hf_average;
      if (*tapset_decision==2)
         hf_sum += 4;
      else if (*tapset_decision==0)
         hf_sum -= 4;
      if (hf_sum > 22)
         *tapset_decision=2;
      else if (hf_sum > 18)
         *tapset_decision=1;
      else
         *tapset_decision=0;
   }
   /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
465
   sum /= nbBands;
466
467
468
469
   /* Recursive averaging */
   sum = (sum+*average)>>1;
   *average = sum;
   /* Hysteresis */
470
   sum = (3*sum + (((3-last_decision)<<7) + 64) + 2)>>2;
471
   if (sum < 80)
472
   {
473
      decision = SPREAD_AGGRESSIVE;
474
475
   } else if (sum < 256)
   {
476
      decision = SPREAD_NORMAL;
477
   } else if (sum < 384)
478
   {
479
      decision = SPREAD_LIGHT;
480
   } else {
481
      decision = SPREAD_NONE;
482
   }
483
   return decision;
484
485
}

486
487
488
489
490
491
492
493
494
495
496
#ifdef MEASURE_NORM_MSE

float MSE[30] = {0};
int nbMSEBands = 0;
int MSECount[30] = {0};

void dump_norm_mse(void)
{
   int i;
   for (i=0;i<nbMSEBands;i++)
   {
497
      printf ("%g ", MSE[i]/MSECount[i]);
498
499
500
501
   }
   printf ("\n");
}

502
void measure_norm_mse(const CELTMode *m, float *X, float *X0, float *bandE, float *bandE0, int M, int N, int C)
503
504
505
506
507
508
509
510
511
512
513
{
   static int init = 0;
   int i;
   if (!init)
   {
      atexit(dump_norm_mse);
      init = 1;
   }
   for (i=0;i<m->nbEBands;i++)
   {
      int j;
514
515
      int c;
      float g;
516
      if (bandE0[i]<10 || (C==2 && bandE0[i+m->nbEBands]<1))
517
         continue;
518
      c=0; do {
519
520
521
         g = bandE[i+c*m->nbEBands]/(1e-15+bandE0[i+c*m->nbEBands]);
         for (j=M*m->eBands[i];j<M*m->eBands[i+1];j++)
            MSE[i] += (g*X[j+c*N]-X0[j+c*N])*(g*X[j+c*N]-X0[j+c*N]);
522
      } while (++c<C);
523
      MSECount[i]+=C;
524
525
526
527
528
529
   }
   nbMSEBands = m->nbEBands;
}

#endif

530
531
532
533
534
535
536
537
538
539
540
541
/* Indexing table for converting from natural Hadamard to ordery Hadamard
   This is essentially a bit-reversed Gray, on top of which we've added
   an inversion of the order because we want the DC at the end rather than
   the beginning. The lines are for N=2, 4, 8, 16 */
static const int ordery_table[] = {
       1,  0,
       3,  0,  2,  1,
       7,  0,  4,  3,  6,  1,  5,  2,
      15,  0,  8,  7, 12,  3, 11,  4, 14,  1,  9,  6, 13,  2, 10,  5,
};

static void deinterleave_hadamard(celt_norm *X, int N0, int stride, int hadamard)
542
543
544
545
546
547
548
{
   int i,j;
   VARDECL(celt_norm, tmp);
   int N;
   SAVE_STACK;
   N = N0*stride;
   ALLOC(tmp, N, celt_norm);
549
550
551
552
553
554
555
556
557
558
559
560
561
   if (hadamard)
   {
      const int *ordery = ordery_table+stride-2;
      for (i=0;i<stride;i++)
      {
         for (j=0;j<N0;j++)
            tmp[ordery[i]*N0+j] = X[j*stride+i];
      }
   } else {
      for (i=0;i<stride;i++)
         for (j=0;j<N0;j++)
            tmp[i*N0+j] = X[j*stride+i];
   }
562
563
564
565
566
   for (j=0;j<N;j++)
      X[j] = tmp[j];
   RESTORE_STACK;
}

567
static void interleave_hadamard(celt_norm *X, int N0, int stride, int hadamard)
568
569
570
571
572
573
574
{
   int i,j;
   VARDECL(celt_norm, tmp);
   int N;
   SAVE_STACK;
   N = N0*stride;
   ALLOC(tmp, N, celt_norm);
575
576
577
578
579
580
581
582
583
584
585
   if (hadamard)
   {
      const int *ordery = ordery_table+stride-2;
      for (i=0;i<stride;i++)
         for (j=0;j<N0;j++)
            tmp[j*stride+i] = X[ordery[i]*N0+j];
   } else {
      for (i=0;i<stride;i++)
         for (j=0;j<N0;j++)
            tmp[j*stride+i] = X[i*N0+j];
   }
586
587
588
589
590
   for (j=0;j<N;j++)
      X[j] = tmp[j];
   RESTORE_STACK;
}

591
void haar1(celt_norm *X, int N0, int stride)
592
593
594
595
596
597
{
   int i, j;
   N0 >>= 1;
   for (i=0;i<stride;i++)
      for (j=0;j<N0;j++)
      {
598
         celt_norm tmp1, tmp2;
599
600
         tmp1 = MULT16_16_Q15(QCONST16(.70710678f,15), X[stride*2*j+i]);
         tmp2 = MULT16_16_Q15(QCONST16(.70710678f,15), X[stride*(2*j+1)+i]);
601
602
         X[stride*2*j+i] = tmp1 + tmp2;
         X[stride*(2*j+1)+i] = tmp1 - tmp2;
603
604
605
      }
}

606
static int compute_qn(int N, int b, int offset, int pulse_cap, int stereo)
607
608
{
   static const celt_int16 exp2_table8[8] =
Jean-Marc Valin's avatar
Jean-Marc Valin committed
609
      {16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048};
610
611
612
613
   int qn, qb;
   int N2 = 2*N-1;
   if (stereo && N==2)
      N2--;
614
615
616
617
   /* The upper limit ensures that in a stereo split with itheta==16384, we'll
       always have enough bits left over to code at least one pulse in the
       side; otherwise it would collapse, since it doesn't get folded. */
   qb = IMIN(b-pulse_cap-(4<<BITRES), (b+N2*offset)/N2);
618

619
   qb = IMIN(8<<BITRES, qb);
620
621
622
623

   if (qb<(1<<BITRES>>1)) {
      qn = 1;
   } else {
624
625
      qn = exp2_table8[qb&0x7]>>(14-(qb>>BITRES));
      qn = (qn+1)>>1<<1;
626
   }
627
   celt_assert(qn <= 256);
628
629
630
   return qn;
}

631
632
633
634
/* This function is responsible for encoding and decoding a band for both
   the mono and stereo case. Even in the mono case, it can split the band
   in two and transmit the energy difference with the two half-bands. It
   can be called recursively so bands can end up being split in 8 parts. */
635
static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, celt_norm *Y,
636
      int N, int b, int spread, int B, int intensity, int tf_change, celt_norm *lowband, int resynth, void *ec,
637
      celt_int32 *remaining_bits, int LM, celt_norm *lowband_out, const celt_ener *bandE, int level,
638
      celt_uint32 *seed, celt_word16 gain, celt_norm *lowband_scratch, int fill)
639
{
640
   const unsigned char *cache;
641
642
   int q;
   int curr_bits;
643
   int stereo, split;
644
   int imid=0, iside=0;
645
   int N0=N;
646
   int N_B=N;
647
   int N_B0;
648
   int B0=B;
649
650
   int time_divide=0;
   int recombine=0;
651
   int inv = 0;
652
   celt_word16 mid=0, side=0;
653
   int longBlocks;
654
   unsigned cm=0;
655
656

   longBlocks = B0==1;
657

658
   N_B /= B;
659
   N_B0 = N_B;
660

661
   split = stereo = Y != NULL;
662

663
664
665
   /* Special case for one sample */
   if (N==1)
   {
666
667
      int c;
      celt_norm *x = X;
668
      c=0; do {
669
         int sign=0;
670
         if (*remaining_bits>=1<<BITRES)
671
         {
672
673
674
            if (encode)
            {
               sign = x[0]<0;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
675
               ec_enc_bits((ec_enc*)ec, sign, 1);
676
677
678
679
680
            } else {
               sign = ec_dec_bits((ec_dec*)ec, 1);
            }
            *remaining_bits -= 1<<BITRES;
            b-=1<<BITRES;
681
682
         }
         if (resynth)
683
684
            x[0] = sign ? -NORM_SCALING : NORM_SCALING;
         x = Y;
685
      } while (++c<1+stereo);
686
      if (lowband_out)
687
         lowband_out[0] = SHR16(X[0],4);
688
      return 1;
689
690
   }

691
   if (!stereo && level == 0)
692
   {
693
694
695
      int k;
      if (tf_change>0)
         recombine = tf_change;
696
      /* Band recombining to increase frequency resolution */
697
698
699
700
701
702
703
704
705

      if (lowband && (recombine || ((N_B&1) == 0 && tf_change<0) || B0>1))
      {
         int j;
         for (j=0;j<N;j++)
            lowband_scratch[j] = lowband[j];
         lowband = lowband_scratch;
      }

706
      for (k=0;k<recombine;k++)
707
      {
708
709
710
         static const unsigned char bit_interleave_table[16]={
           0,1,1,1,2,3,3,3,2,3,3,3,2,3,3,3
         };
711
         if (encode)
712
            haar1(X, N>>k, 1<<k);
713
         if (lowband)
714
            haar1(lowband, N>>k, 1<<k);
715
         fill = bit_interleave_table[fill&0xF]|bit_interleave_table[fill>>4]<<2;
716
      }
717
718
      B>>=recombine;
      N_B<<=recombine;
719

720
      /* Increasing the time resolution */
721
      while ((N_B&1) == 0 && tf_change<0)
722
      {
723
724
725
726
         if (encode)
            haar1(X, N_B, B);
         if (lowband)
            haar1(lowband, N_B, B);
727
         fill |= fill<<B;
728
729
730
731
         B <<= 1;
         N_B >>= 1;
         time_divide++;
         tf_change++;
732
      }
733
734
      B0=B;
      N_B0 = N_B;
735
736

      /* Reorganize the samples in time order instead of frequency order */
737
      if (B0>1)
738
739
      {
         if (encode)
740
            deinterleave_hadamard(X, N_B>>recombine, B0<<recombine, longBlocks);
741
         if (lowband)
742
            deinterleave_hadamard(lowband, N_B>>recombine, B0<<recombine, longBlocks);
743
      }
744
745
   }

Jean-Marc Valin's avatar
Jean-Marc Valin committed
746
   /* If we need 1.5 more bit than we can produce, split the band in two. */
747
   cache = m->cache.bits + m->cache.index[(LM+1)*m->nbEBands+i];
Jean-Marc Valin's avatar
Jean-Marc Valin committed
748
   if (!stereo && LM != -1 && b > cache[cache[0]]+12 && N>2)
749
   {
750
751
752
753
754
755
      if (LM>0 || (N&1)==0)
      {
         N >>= 1;
         Y = X+N;
         split = 1;
         LM -= 1;
756
         if (B==1)
757
            fill = fill&1|fill<<1;
758
         B = (B+1)>>1;
759
      }
760
761
762
   }

   if (split)
763
   {
764
      int qn;
765
      int itheta=0;
766
767
      int mbits, sbits, delta;
      int qalloc;
768
      int pulse_cap;
769
      int offset;
770
      int orig_fill;
771
      celt_int32 tell;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
772
773

      /* Decide on the resolution to give to the split parameter theta */
774
      pulse_cap = m->logN[i]+(LM<<BITRES);
775
      offset = (pulse_cap>>1) - (stereo&&N==2 ? QTHETA_OFFSET_TWOPHASE : QTHETA_OFFSET);
776
      qn = compute_qn(N, b, offset, pulse_cap, stereo);
777
      if (stereo && i>=intensity)
778
         qn = 1;
779
780
781
782
783
784
785
786
      if (encode)
      {
         /* theta is the atan() of the ratio between the (normalized)
            side and mid. With just that parameter, we can re-scale both
            mid and side because we know that 1) they have unit norm and
            2) they are orthogonal. */
         itheta = stereo_itheta(X, Y, stereo, N);
      }
787
      tell = encode ? ec_enc_tell(ec, BITRES) : ec_dec_tell(ec, BITRES);
Jean-Marc Valin's avatar
Jean-Marc Valin committed
788
      if (qn!=1)
789
      {
790
         if (encode)
791
            itheta = (itheta*qn+8192)>>14;
792
793

         /* Entropy coding of the angle. We use a uniform pdf for the
794
795
            time split, a step for stereo, and a triangular one for the rest. */
         if (stereo && N>2)
796
         {
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
            int p0 = 3;
            int x = itheta;
            int x0 = qn/2;
            int ft = p0*(x0+1) + x0;
            /* Use a probability of p0 up to itheta=8192 and then use 1 after */
            if (encode)
            {
               ec_encode((ec_enc*)ec,x<=x0?p0*x:(x-1-x0)+(x0+1)*p0,x<=x0?p0*(x+1):(x-x0)+(x0+1)*p0,ft);
            } else {
               int fs;
               fs=ec_decode(ec,ft);
               if (fs<(x0+1)*p0)
                  x=fs/p0;
               else
                  x=x0+1+(fs-(x0+1)*p0);
               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);
               itheta = x;
            }
         } else if (B0>1 || stereo) {
            /* Uniform pdf */
817
            if (encode)
Jean-Marc Valin's avatar
Jean-Marc Valin committed
818
               ec_enc_uint((ec_enc*)ec, itheta, qn+1);
819
            else
Jean-Marc Valin's avatar
Jean-Marc Valin committed
820
               itheta = ec_dec_uint((ec_dec*)ec, qn+1);
821
         } else {
822
            int fs=1, ft;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
823
            ft = ((qn>>1)+1)*((qn>>1)+1);
824
825
            if (encode)
            {
826
827
               int fl;

Jean-Marc Valin's avatar
Jean-Marc Valin committed
828
829
830
               fs = itheta <= (qn>>1) ? itheta + 1 : qn + 1 - itheta;
               fl = itheta <= (qn>>1) ? itheta*(itheta + 1)>>1 :
                ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
831

Jean-Marc Valin's avatar
Jean-Marc Valin committed
832
               ec_encode((ec_enc*)ec, fl, fl+fs, ft);
833
            } else {
834
               /* Triangular pdf */
835
               int fl=0;
836
               int fm;
837
               fm = ec_decode((ec_dec*)ec, ft);
838

Jean-Marc Valin's avatar
Jean-Marc Valin committed
839
               if (fm < ((qn>>1)*((qn>>1) + 1)>>1))
840
841
842
843
844
845
               {
                  itheta = (isqrt32(8*(celt_uint32)fm + 1) - 1)>>1;
                  fs = itheta + 1;
                  fl = itheta*(itheta + 1)>>1;
               }
               else
846
               {
Jean-Marc Valin's avatar
Jean-Marc Valin committed
847
                  itheta = (2*(qn + 1)
848
                   - isqrt32(8*(celt_uint32)(ft - fm - 1) + 1))>>1;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
849
850
                  fs = qn + 1 - itheta;
                  fl = ft - ((qn + 1 - itheta)*(qn + 2 - itheta)>>1);
851
               }
852

853
               ec_dec_update((ec_dec*)ec, fl, fl+fs, ft);
854
855
            }
         }
856
         itheta = (celt_int32)itheta*16384/qn;
857
         if (encode && stereo)
858
859
860
861
862
863
         {
            if (itheta==0)
               intensity_stereo(m, X, Y, bandE, i, N);
            else
               stereo_split(X, Y, N);
         }
864
865
866
867
868
869
870
871
872
873
874
875
         /* TODO: Renormalising X and Y *may* help fixed-point a bit at very high rate.
                  Let's do that at higher complexity */
      } else if (stereo) {
         if (encode)
         {
            inv = itheta > 8192;
            if (inv)
            {
               int j;
               for (j=0;j<N;j++)
                  Y[j] = -Y[j];
            }
876
            intensity_stereo(m, X, Y, bandE, i, N);
877
         }
878
         if (b>2<<