rate.c 5.93 KB
Newer Older
1
/* (C) 2007-2009 Jean-Marc Valin, CSIRO
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
*/
/*
   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.
*/

32
33
34
35
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

36
37
38
39
#include <math.h>
#include "modes.h"
#include "cwrs.h"
#include "arch.h"
Jean-Marc Valin's avatar
Jean-Marc Valin committed
40
#include "os_support.h"
41
42

#include "entcode.h"
43
#include "rate.h"
44

Jean-Marc Valin's avatar
Jean-Marc Valin committed
45

46
#ifndef STATIC_MODES
47

48
celt_int16_t **compute_alloc_cache(CELTMode *m, int C)
Jean-Marc Valin's avatar
Jean-Marc Valin committed
49
{
50
   int i, prevN;
51
   int error = 0;
52
53
   celt_int16_t **bits;
   const celt_int16_t *eBands = m->eBands;
54

55
   bits = celt_alloc(m->nbEBands*sizeof(celt_int16_t*));
56
57
58
   if (bits==NULL)
     return NULL;
        
Jean-Marc Valin's avatar
Jean-Marc Valin committed
59
   prevN = -1;
60
   for (i=0;i<m->nbEBands;i++)
Jean-Marc Valin's avatar
Jean-Marc Valin committed
61
   {
62
      int N = C*(eBands[i+1]-eBands[i]);
63
      if (N == prevN && eBands[i] < m->pitchEnd)
Jean-Marc Valin's avatar
Jean-Marc Valin committed
64
      {
65
         bits[i] = bits[i-1];
Jean-Marc Valin's avatar
Jean-Marc Valin committed
66
      } else {
67
         bits[i] = celt_alloc(MAX_PSEUDO*sizeof(celt_int16_t));
68
         if (bits[i]!=NULL) {
69
70
71
72
73
            int j;
            celt_int16_t tmp[MAX_PULSES];
            get_required_bits(tmp, N, MAX_PULSES, BITRES);
            for (j=0;j<MAX_PSEUDO;j++)
               bits[i][j] = tmp[get_pulses(j)];
74
75
76
         } else {
            error=1;
         }
Jean-Marc Valin's avatar
Jean-Marc Valin committed
77
78
79
         prevN = N;
      }
   }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
   if (error)
   {
      const celt_int16_t *prevPtr = NULL;
      if (bits!=NULL)
      {
         for (i=0;i<m->nbEBands;i++)
         {
            if (bits[i] != prevPtr)
            {
               prevPtr = bits[i];
               celt_free((int*)bits[i]);
            }
         }
      free(bits);
      bits=NULL;
      }   
   }
97
   return bits;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
98
99
}

100
#endif /* !STATIC_MODES */
101

102

103

104
static void interp_bits2pulses(const CELTMode *m, int *bits1, int *bits2, int total, int *bits, int *ebits, int *fine_priority, int len)
105
{
106
   int psum;
107
   int lo, hi;
108
   int j;
109
   const int C = CHANNELS(m);
110
   SAVE_STACK;
111
112
113
114
115
   lo = 0;
   hi = 1<<BITRES;
   while (hi-lo != 1)
   {
      int mid = (lo+hi)>>1;
116
      psum = 0;
117
      for (j=0;j<len;j++)
118
         psum += ((1<<BITRES)-mid)*bits1[j] + mid*bits2[j];
119
      if (psum > (total<<BITRES))
120
121
122
         hi = mid;
      else
         lo = mid;
123
   }
124
   psum = 0;
125
   /*printf ("interp bisection gave %d\n", lo);*/
126
127
   for (j=0;j<len;j++)
   {
128
129
      bits[j] = ((1<<BITRES)-lo)*bits1[j] + lo*bits2[j];
      psum += bits[j];
130
   }
131
132
133
   /* Allocate the remaining bits */
   {
      int left, perband;
134
      left = (total<<BITRES)-psum;
135
136
137
138
139
140
141
      perband = left/len;
      for (j=0;j<len;j++)
         bits[j] += perband;
      left = left-len*perband;
      for (j=0;j<left;j++)
         bits[j]++;
   }
142
143
   for (j=0;j<len;j++)
   {
144
      int N, d;
145
      int offset;
146
147

      N=m->eBands[j+1]-m->eBands[j]; 
Jean-Marc Valin's avatar
Jean-Marc Valin committed
148
149
      /* Compensate for the extra DoF in stereo */
      d=(C*N+ ((C==2 && N>2) ? 1 : 0))<<BITRES; 
150
      offset = FINE_OFFSET - log2_frac(N, BITRES);
151
      /* Offset for the number of fine bits compared to their "fair share" of total/N */
152
      offset = bits[j]-offset*N*C;
Jean-Marc Valin's avatar
Jean-Marc Valin committed
153
154
155
      /* Compensate for the prediction gain in stereo */
      if (C==2)
         offset -= 1<<BITRES;
156
157
158
159
      if (offset < 0)
         offset = 0;
      ebits[j] = (2*offset+d)/(2*d);
      fine_priority[j] = ebits[j]*d >= offset;
160

161
162
163
164
165
166
167
168
169
170
171
      /* Make sure not to bust */
      if (C*ebits[j] > (bits[j]>>BITRES))
         ebits[j] = bits[j]/C >> BITRES;

      if (ebits[j]>7)
         ebits[j]=7;
      /* The bits used for fine allocation can't be used for pulses */
      bits[j] -= C*ebits[j]<<BITRES;
      if (bits[j] < 0)
         bits[j] = 0;
   }
172
   RESTORE_STACK;
173
174
}

175
void compute_allocation(const CELTMode *m, int *offsets, int total, int *pulses, int *ebits, int *fine_priority)
176
{
177
   int lo, hi, len, j;
178
179
   VARDECL(int, bits1);
   VARDECL(int, bits2);
180
   SAVE_STACK;
181
   
182
   len = m->nbEBands;
183
184
   ALLOC(bits1, len, int);
   ALLOC(bits2, len, int);
185

186
187
188
   lo = 0;
   hi = m->nbAllocVectors - 1;
   while (hi-lo != 1)
189
   {
190
      int psum = 0;
191
192
193
      int mid = (lo+hi) >> 1;
      for (j=0;j<len;j++)
      {
194
         bits1[j] = (m->allocVectors[mid*len+j] + offsets[j])<<BITRES;
195
196
         if (bits1[j] < 0)
            bits1[j] = 0;
197
         psum += bits1[j];
198
         /*printf ("%d ", bits[j]);*/
199
      }
200
      /*printf ("\n");*/
201
      if (psum > (total<<BITRES))
202
203
204
         hi = mid;
      else
         lo = mid;
205
      /*printf ("lo = %d, hi = %d\n", lo, hi);*/
206
   }
207
   /*printf ("interp between %d and %d\n", lo, hi);*/
208
   for (j=0;j<len;j++)
209
   {
210
211
212
213
214
215
      bits1[j] = m->allocVectors[lo*len+j] + offsets[j];
      bits2[j] = m->allocVectors[hi*len+j] + offsets[j];
      if (bits1[j] < 0)
         bits1[j] = 0;
      if (bits2[j] < 0)
         bits2[j] = 0;
216
   }
217
   interp_bits2pulses(m, bits1, bits2, total, pulses, ebits, fine_priority, len);
218
   RESTORE_STACK;
219
220
}