Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cwrs.c « libcelt - gitlab.com/quite/celt.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3cdfdfe5b71ca3e6c1926e44ad207fe895da1904 (plain)
1
2
3
4
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/* (C) 2007-2008 Timothy B. Terriberry
   (C) 2008 Jean-Marc Valin */
/*
   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.
*/

/* Functions for encoding and decoding pulse vectors.
   These are based on the function
     U(n,m) = U(n-1,m) + U(n,m-1) + U(n-1,m-1),
     U(n,1) = U(1,m) = 2,
    which counts the number of ways of placing m pulses in n dimensions, where
     at least one pulse lies in dimension 0.
   For more details, see: http://people.xiph.org/~tterribe/notes/cwrs.html
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "os_support.h"
#include <stdlib.h>
#include <string.h>
#include "cwrs.h"
#include "mathops.h"

#if 0
int log2_frac(ec_uint32 val, int frac)
{
   int i;
   /* EC_ILOG() actually returns log2()+1, go figure */
   int L = EC_ILOG(val)-1;
   /*printf ("in: %d %d ", val, L);*/
   if (L>14)
      val >>= L-14;
   else if (L<14)
      val <<= 14-L;
   L <<= frac;
   /*printf ("%d\n", val);*/
   for (i=0;i<frac;i++)
{
      val = (val*val) >> 15;
      /*printf ("%d\n", val);*/
      if (val > 16384)
         L |= (1<<(frac-i-1));
      else   
         val <<= 1;
}
   return L;
}
#endif

int log2_frac64(ec_uint64 val, int frac)
{
   int i;
   /* EC_ILOG64() actually returns log2()+1, go figure */
   int L = EC_ILOG64(val)-1;
   /*printf ("in: %d %d ", val, L);*/
   if (L>14)
      val >>= L-14;
   else if (L<14)
      val <<= 14-L;
   L <<= frac;
   /*printf ("%d\n", val);*/
   for (i=0;i<frac;i++)
   {
      val = (val*val) >> 15;
      /*printf ("%d\n", val);*/
      if (val > 16384)
         L |= (1<<(frac-i-1));
      else   
         val <<= 1;
   }
   return L;
}

int fits_in32(int _n, int _m)
{
   static const celt_int16_t maxN[15] = {
      255, 255, 255, 255, 255, 109,  60,  40,
       29,  24,  20,  18,  16,  14,  13};
   static const celt_int16_t maxM[15] = {
      255, 255, 255, 255, 255, 238,  95,  53,
       36,  27,  22,  18,  16,  15,  13};
   if (_n>=14)
   {
      if (_m>=14)
         return 0;
      else
         return _n <= maxN[_m];
   } else {
      return _m <= maxM[_n];
   }   
}
int fits_in64(int _n, int _m)
{
   static const celt_int16_t maxN[28] = {
      255, 255, 255, 255, 255, 255, 255, 255,
      255, 255, 178, 129, 100,  81,  68,  58,
       51,  46,  42,  38,  36,  33,  31,  30,
       28, 27, 26, 25};
   static const celt_int16_t maxM[28] = {
      255, 255, 255, 255, 255, 255, 255, 255, 
      255, 255, 245, 166, 122,  94,  77,  64, 
       56,  49,  44,  40,  37,  34,  32,  30,
       29,  27,  26,  25};
   if (_n>=27)
   {
      if (_m>=27)
         return 0;
      else
         return _n <= maxN[_m];
   } else {
      return _m <= maxM[_n];
   }
}

/*Computes the next row/column of any recurrence that obeys the relation
   u[i][j]=u[i-1][j]+u[i][j-1]+u[i-1][j-1].
  _ui0 is the base case for the new row/column.*/
static inline void unext32(celt_uint32_t *_ui,int _len,celt_uint32_t _ui0){
  celt_uint32_t ui1;
  int           j;
  /* doing a do-while would overrun the array if we had less than 2 samples */
  j=1; do {
    ui1=_ui[j]+_ui[j-1]+_ui0;
    _ui[j-1]=_ui0;
    _ui0=ui1;
  } while (++j<_len);
  _ui[j-1]=_ui0;
}

static inline void unext64(celt_uint64_t *_ui,int _len,celt_uint64_t _ui0){
  celt_uint64_t ui1;
  int           j;
  /* doing a do-while would overrun the array if we had less than 2 samples */
  j=1; do {
    ui1=_ui[j]+_ui[j-1]+_ui0;
    _ui[j-1]=_ui0;
    _ui0=ui1;
  } while (++j<_len);
  _ui[j-1]=_ui0;
}

/*Computes the previous row/column of any recurrence that obeys the relation
   u[i-1][j]=u[i][j]-u[i][j-1]-u[i-1][j-1].
  _ui0 is the base case for the new row/column.*/
static inline void uprev32(celt_uint32_t *_ui,int _n,celt_uint32_t _ui0){
  celt_uint32_t ui1;
  int           j;
  /* doing a do-while would overrun the array if we had less than 2 samples */
  j=1; do {
    ui1=_ui[j]-_ui[j-1]-_ui0;
    _ui[j-1]=_ui0;
    _ui0=ui1;
  } while (++j<_n);
  _ui[j-1]=_ui0;
}

static inline void uprev64(celt_uint64_t *_ui,int _n,celt_uint64_t _ui0){
  celt_uint64_t ui1;
  int           j;
  /* doing a do-while would overrun the array if we had less than 2 samples */
  j=1; do {
    ui1=_ui[j]-_ui[j-1]-_ui0;
    _ui[j-1]=_ui0;
    _ui0=ui1;
  } while (++j<_n);
  _ui[j-1]=_ui0;
}

/*Returns the number of ways of choosing _m elements from a set of size _n with
   replacement when a sign bit is needed for each unique element.
  On input, _u should be initialized to column (_m-1) of U(n,m).
  On exit, _u will be initialized to column _m of U(n,m).*/
celt_uint32_t ncwrs_unext32(int _n,celt_uint32_t *_ui){
  celt_uint32_t ret;
  celt_uint32_t ui0;
  celt_uint32_t ui1;
  int           j;
  ret=ui0=2;
  celt_assert(_n>=2);
  j=1; do {
    ui1=_ui[j]+_ui[j-1]+ui0;
    _ui[j-1]=ui0;
    ui0=ui1;
    ret+=ui0;
  } while (++j<_n);
  _ui[j-1]=ui0;
  return ret;
}

celt_uint64_t ncwrs_unext64(int _n,celt_uint64_t *_ui){
  celt_uint64_t ret;
  celt_uint64_t ui0;
  celt_uint64_t ui1;
  int           j;
  ret=ui0=2;
  celt_assert(_n>=2);
  j=1; do {
    ui1=_ui[j]+_ui[j-1]+ui0;
    _ui[j-1]=ui0;
    ui0=ui1;
    ret+=ui0;
  } while (++j<_n);
  _ui[j-1]=ui0;
  return ret;
}

/*Returns the number of ways of choosing _m elements from a set of size _n with
   replacement when a sign bit is needed for each unique element.
  On exit, _u will be initialized to column _m of U(n,m).*/
celt_uint32_t ncwrs_u32(int _n,int _m,celt_uint32_t *_u){
  int k;
  CELT_MEMSET(_u,0,_n);
  if(_m<=0)return 1;
  if(_n<=0)return 0;
  for(k=1;k<_m;k++)unext32(_u,_n,2);
  return ncwrs_unext32(_n,_u);
}

celt_uint64_t ncwrs_u64(int _n,int _m,celt_uint64_t *_u){
  int k;
  CELT_MEMSET(_u,0,_n);
  if(_m<=0)return 1;
  if(_n<=0)return 0;
  for(k=1;k<_m;k++)unext64(_u,_n,2);
  return ncwrs_unext64(_n,_u);
}

/*Returns the _i'th combination of _m elements chosen from a set of size _n
   with associated sign bits.
  _x: Returns the combination with elements sorted in ascending order.
  _s: Returns the associated sign bits.
  _u: Temporary storage already initialized to column _m of U(n,m).
      Its contents will be overwritten.*/
void cwrsi32(int _n,int _m,celt_uint32_t _i,int *_x,int *_s,celt_uint32_t *_u){
  int j;
  int k;
  for(k=j=0;k<_m;k++){
    celt_uint32_t p;
    celt_uint32_t t;
    p=_u[_n-j-1];
    if(k>0){
      t=p>>1;
      if(t<=_i||_s[k-1])_i+=t;
    }
    while(p<=_i){
      _i-=p;
      j++;
      p=_u[_n-j-1];
    }
    t=p>>1;
    _s[k]=_i>=t;
    _x[k]=j;
    if(_s[k])_i-=t;
    uprev32(_u,_n-j,2);
  }
}

void cwrsi64(int _n,int _m,celt_uint64_t _i,int *_x,int *_s,celt_uint64_t *_u){
  int j;
  int k;
  for(k=j=0;k<_m;k++){
    celt_uint64_t p;
    celt_uint64_t t;
    p=_u[_n-j-1];
    if(k>0){
      t=p>>1;
      if(t<=_i||_s[k-1])_i+=t;
    }
    while(p<=_i){
      _i-=p;
      j++;
      p=_u[_n-j-1];
    }
    t=p>>1;
    _s[k]=_i>=t;
    _x[k]=j;
    if(_s[k])_i-=t;
    uprev64(_u,_n-j,2);
  }
}

/*Returns the index of the given combination of _m elements chosen from a set
   of size _n with associated sign bits.
  _x: The combination with elements sorted in ascending order.
  _s: The associated sign bits.
  _u: Temporary storage already initialized to column _m of U(n,m).
      Its contents will be overwritten.*/
celt_uint32_t icwrs32(int _n,int _m,const int *_x,const int *_s,
 celt_uint32_t *_u){
  celt_uint32_t i;
  int           j;
  int           k;
  i=0;
  for(k=j=0;k<_m;k++){
    celt_uint32_t p;
    p=_u[_n-j-1];
    if(k>0)p>>=1;
    while(j<_x[k]){
      i+=p;
      j++;
      p=_u[_n-j-1];
    }
    if((k==0||_x[k]!=_x[k-1])&&_s[k])i+=p>>1;
    uprev32(_u,_n-j,2);
  }
  return i;
}

celt_uint64_t icwrs64(int _n,int _m,const int *_x,const int *_s,
 celt_uint64_t *_u){
  celt_uint64_t i;
  int           j;
  int           k;
  i=0;
  for(k=j=0;k<_m;k++){
    celt_uint64_t p;
    p=_u[_n-j-1];
    if(k>0)p>>=1;
    while(j<_x[k]){
      i+=p;
      j++;
      p=_u[_n-j-1];
    }
    if((k==0||_x[k]!=_x[k-1])&&_s[k])i+=p>>1;
    uprev64(_u,_n-j,2);
  }
  return i;
}

/*Converts a combination _x of _m unit pulses with associated sign bits _s into
   a pulse vector _y of length _n.
  _y: Returns the vector of pulses.
  _x: The combination with elements sorted in ascending order. _x[_m] = -1
  _s: The associated sign bits.*/
void comb2pulse(int _n,int _m,int * restrict _y,const int *_x,const int *_s){
  int k;
  const int signs[2]={1,-1};
  CELT_MEMSET(_y, 0, _n);
  k=0; do {
    _y[_x[k]]+=signs[_s[k]];
  } while (++k<_m);
}

/*Converts a pulse vector vector _y of length _n into a combination of _m unit
   pulses with associated sign bits _s.
  _x: Returns the combination with elements sorted in ascending order.
  _s: Returns the associated sign bits.
  _y: The vector of pulses, whose sum of absolute values must be _m.*/
void pulse2comb(int _n,int _m,int *_x,int *_s,const int *_y){
  int j;
  int k;
  for(k=j=0;j<_n;j++){
    if(_y[j]){
      int n;
      int s;
      n=abs(_y[j]);
      s=_y[j]<0;
      do {
        _x[k]=j;
        _s[k]=s;
        k++;
      } while (--n>0);
    }
  }
}

static inline void encode_comb32(int _n,int _m,const int *_x,const int *_s,
 ec_enc *_enc){
  VARDECL(celt_uint32_t,u);
  celt_uint32_t nc;
  celt_uint32_t i;
  SAVE_STACK;
  ALLOC(u,_n,celt_uint32_t);
  nc=ncwrs_u32(_n,_m,u);
  i=icwrs32(_n,_m,_x,_s,u);
  ec_enc_uint(_enc,i,nc);
  RESTORE_STACK;
}

static inline void encode_comb64(int _n,int _m,const int *_x,const int *_s,
 ec_enc *_enc){
  VARDECL(celt_uint64_t,u);
  celt_uint64_t nc;
  celt_uint64_t i;
  SAVE_STACK;
  ALLOC(u,_n,celt_uint64_t);
  nc=ncwrs_u64(_n,_m,u);
  i=icwrs64(_n,_m,_x,_s,u);
  ec_enc_uint64(_enc,i,nc);
  RESTORE_STACK;
}

int get_required_bits(int N, int K, int frac)
{
   int nbits = 0;
   if(fits_in64(N,K))
   {
      VARDECL(celt_uint64_t,u);
      SAVE_STACK;
      ALLOC(u,N,celt_uint64_t);
      nbits = log2_frac64(ncwrs_u64(N,K,u), frac);
      RESTORE_STACK;
   } else {
      nbits = log2_frac64(N, frac);
      nbits += get_required_bits(N/2+1, (K+1)/2, frac);
      nbits += get_required_bits(N/2+1, K/2, frac);
   }
   return nbits;
}


void encode_pulses(int *_y, int N, int K, ec_enc *enc)
{
   VARDECL(int, comb);
   VARDECL(int, signs);
   SAVE_STACK;

   ALLOC(comb, K, int);
   ALLOC(signs, K, int);

   pulse2comb(N, K, comb, signs, _y);
   if (N==1)
   {
      ec_enc_bits(enc, _y[0]<0, 1);
   } else if(fits_in32(N,K))
   {
      encode_comb32(N, K, comb, signs, enc);
   } else if(fits_in64(N,K)) {
      encode_comb64(N, K, comb, signs, enc);
   } else {
     int count=0;
     int split=0;
     int tmp;
     while (split<N)
     {
       count += abs(_y[split]);
       if (count >= (K+1)/2)
         break;
       split++;
     }
     count -= (K+1)/2;
     ec_enc_uint(enc,split,N);
     if (_y[split]<0)
       count = -count;
     tmp = _y[split];
     _y[split] -= count;
     encode_pulses(_y, split+1, (K+1)/2, enc);
     _y[split] = count;
     if (K/2 != 0)
       encode_pulses(_y+split, N-split, K/2, enc);
     _y[split] = tmp;
   }
   RESTORE_STACK;
}

static inline void decode_comb32(int _n,int _m,int *_x,int *_s,ec_dec *_dec){
  VARDECL(celt_uint32_t,u);
  SAVE_STACK;
  ALLOC(u,_n,celt_uint32_t);
  cwrsi32(_n,_m,ec_dec_uint(_dec,ncwrs_u32(_n,_m,u)),_x,_s,u);
  RESTORE_STACK;
}

static inline void decode_comb64(int _n,int _m,int *_x,int *_s,ec_dec *_dec){
  VARDECL(celt_uint64_t,u);
  SAVE_STACK;
  ALLOC(u,_n,celt_uint64_t);
  cwrsi64(_n,_m,ec_dec_uint64(_dec,ncwrs_u64(_n,_m,u)),_x,_s,u);
  RESTORE_STACK;
}

void decode_pulses(int *_y, int N, int K, ec_dec *dec)
{
   VARDECL(int, comb);
   VARDECL(int, signs);
   SAVE_STACK;

   ALLOC(comb, K, int);
   ALLOC(signs, K, int);
   /* Simple heuristic to figure out whether it fits in 32 bits */
   if (N==1)
   {
      int s = ec_dec_bits(dec, 1);
      if (s==0)
         _y[0] = K;
      else
         _y[0] = -K;
   } else if(fits_in32(N,K))
   {
      decode_comb32(N, K, comb, signs, dec);
      comb2pulse(N, K, _y, comb, signs);
   } else if(fits_in64(N,K)) {
      decode_comb64(N, K, comb, signs, dec);
      comb2pulse(N, K, _y, comb, signs);
   } else {
     int count;
     int split = ec_dec_uint(dec,N);
     decode_pulses(_y, split+1, (K+1)/2, dec);
     count = _y[split];
     if (K/2 != 0)
     {
        decode_pulses(_y+split, N-split, K/2, dec);
     } else {
       int i;
       for (i=0;i<N-split;i++)
          _y[split+i] = 0;
     }
     _y[split] += count;
   }
   RESTORE_STACK;
}