blob: b39a9440245980ec4c97e4636e869a010cbe9f5f [file] [log] [blame]
Jean-Marc Valin8b2ff0d2009-10-17 21:40:10 -04001/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
Jean-Marc Valin33ddd792008-01-14 17:39:01 +11004/*
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
Gregory Maxwell71d39ad2011-07-30 00:00:29 -04008
Jean-Marc Valin33ddd792008-01-14 17:39:01 +11009 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
Gregory Maxwell71d39ad2011-07-30 00:00:29 -040011
Jean-Marc Valin33ddd792008-01-14 17:39:01 +110012 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
Gregory Maxwell71d39ad2011-07-30 00:00:29 -040015
Jean-Marc Valin33ddd792008-01-14 17:39:01 +110016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
Jean-Marc Valin02fa9132008-02-20 12:09:29 +110029#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
Jean-Marc Valin33ddd792008-01-14 17:39:01 +110033#include <math.h>
34#include "modes.h"
35#include "cwrs.h"
36#include "arch.h"
Jean-Marc Valina6631742008-01-16 17:16:04 +110037#include "os_support.h"
Jean-Marc Valin33ddd792008-01-14 17:39:01 +110038
39#include "entcode.h"
Jean-Marc Valinf51ca492008-01-17 10:58:38 +110040#include "rate.h"
Jean-Marc Valin33ddd792008-01-14 17:39:01 +110041
Timothy B. Terriberry76469c62011-01-07 09:18:34 -080042static const unsigned char LOG2_FRAC_TABLE[24]={
43 0,
44 8,13,
45 16,19,21,23,
46 24,26,27,28,29,30,31,32,
47 32,33,34,34,35,36,36,37,37
48};
49
Jean-Marc Valin5ad35bf2011-01-28 22:42:09 -050050#ifdef CUSTOM_MODES
Jean-Marc Valin33ddd792008-01-14 17:39:01 +110051
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040052/*Determines if V(N,K) fits in a 32-bit unsigned integer.
53 N and K are themselves limited to 15 bits.*/
54static int fits_in32(int _n, int _k)
Jean-Marc Valina6631742008-01-16 17:16:04 +110055{
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040056 static const opus_int16 maxN[15] = {
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040057 32767, 32767, 32767, 1476, 283, 109, 60, 40,
58 29, 24, 20, 18, 16, 14, 13};
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040059 static const opus_int16 maxK[15] = {
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040060 32767, 32767, 32767, 32767, 1172, 238, 95, 53,
61 36, 27, 22, 18, 16, 15, 13};
62 if (_n>=14)
Jean-Marc Valina6631742008-01-16 17:16:04 +110063 {
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040064 if (_k>=14)
65 return 0;
Jean-Marc Valincae30df2010-05-21 00:26:03 -040066 else
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040067 return _n <= maxN[_k];
68 } else {
69 return _k <= maxK[_n];
Jean-Marc Valina6631742008-01-16 17:16:04 +110070 }
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040071}
72
73void compute_pulse_cache(CELTMode *m, int LM)
74{
Timothy B. Terriberryc5643072011-01-29 12:57:18 -080075 int C;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040076 int i;
Timothy B. Terriberryc5643072011-01-29 12:57:18 -080077 int j;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040078 int curr=0;
79 int nbEntries=0;
80 int entryN[100], entryK[100], entryI[100];
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040081 const opus_int16 *eBands = m->eBands;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040082 PulseCache *cache = &m->cache;
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040083 opus_int16 *cindex;
Jean-Marc Valin732ea382010-08-25 13:52:27 -040084 unsigned char *bits;
Timothy B. Terriberryc5643072011-01-29 12:57:18 -080085 unsigned char *cap;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040086
Jean-Marc Valin382cad42011-08-30 01:39:40 -040087 cindex = opus_alloc(sizeof(cache->index[0])*m->nbEBands*(LM+2));
Jean-Marc Valin732ea382010-08-25 13:52:27 -040088 cache->index = cindex;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040089
Jean-Marc Valin7fff5722010-10-18 16:20:00 -040090 /* Scan for all unique band sizes */
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040091 for (i=0;i<=LM+1;i++)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -040092 {
Jean-Marc Valin732ea382010-08-25 13:52:27 -040093 for (j=0;j<m->nbEBands;j++)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -040094 {
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040095 int k;
96 int N = (eBands[j+1]-eBands[j])<<i>>1;
Jean-Marc Valin732ea382010-08-25 13:52:27 -040097 cindex[i*m->nbEBands+j] = -1;
Jean-Marc Valin7fff5722010-10-18 16:20:00 -040098 /* Find other bands that have the same size */
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040099 for (k=0;k<=i;k++)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400100 {
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -0400101 int n;
Jean-Marc Valin732ea382010-08-25 13:52:27 -0400102 for (n=0;n<m->nbEBands && (k!=i || n<j);n++)
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400103 {
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -0400104 if (N == (eBands[n+1]-eBands[n])<<k>>1)
105 {
Jean-Marc Valin732ea382010-08-25 13:52:27 -0400106 cindex[i*m->nbEBands+j] = cindex[k*m->nbEBands+n];
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -0400107 break;
108 }
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400109 }
110 }
Jean-Marc Valin732ea382010-08-25 13:52:27 -0400111 if (cache->index[i*m->nbEBands+j] == -1 && N!=0)
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -0400112 {
113 int K;
114 entryN[nbEntries] = N;
115 K = 0;
Jean-Marc Valin7fff5722010-10-18 16:20:00 -0400116 while (fits_in32(N,get_pulses(K+1)) && K<MAX_PSEUDO)
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -0400117 K++;
118 entryK[nbEntries] = K;
Jean-Marc Valin732ea382010-08-25 13:52:27 -0400119 cindex[i*m->nbEBands+j] = curr;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -0400120 entryI[nbEntries] = curr;
121
122 curr += K+1;
123 nbEntries++;
124 }
125 }
Gregory Maxwelldc67fa92009-06-04 17:17:35 -0400126 }
Jean-Marc Valin382cad42011-08-30 01:39:40 -0400127 bits = opus_alloc(sizeof(unsigned char)*curr);
Jean-Marc Valin732ea382010-08-25 13:52:27 -0400128 cache->bits = bits;
129 cache->size = curr;
Jean-Marc Valin7fff5722010-10-18 16:20:00 -0400130 /* Compute the cache for all unique sizes */
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -0400131 for (i=0;i<nbEntries;i++)
132 {
Jean-Marc Valin732ea382010-08-25 13:52:27 -0400133 unsigned char *ptr = bits+entryI[i];
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400134 opus_int16 tmp[MAX_PULSES+1];
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -0400135 get_required_bits(tmp, entryN[i], get_pulses(entryK[i]), BITRES);
136 for (j=1;j<=entryK[i];j++)
137 ptr[j] = tmp[get_pulses(j)]-1;
138 ptr[0] = entryK[i];
139 }
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800140
141 /* Compute the maximum rate for each band at which we'll reliably use as
142 many bits as we ask for. */
Jean-Marc Valin382cad42011-08-30 01:39:40 -0400143 cache->caps = cap = opus_alloc(sizeof(cache->caps[0])*(LM+1)*2*m->nbEBands);
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800144 for (i=0;i<=LM;i++)
145 {
146 for (C=1;C<=2;C++)
147 {
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800148 for (j=0;j<m->nbEBands;j++)
149 {
150 int N0;
151 int max_bits;
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800152 N0 = m->eBands[j+1]-m->eBands[j];
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800153 /* N=1 bands only have a sign bit and fine bits. */
154 if (N0<<i == 1)
Timothy B. Terriberryb5d123a2011-01-30 22:18:12 -0800155 max_bits = C*(1+MAX_FINE_BITS)<<BITRES;
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800156 else
157 {
158 const unsigned char *pcache;
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400159 opus_int32 num;
160 opus_int32 den;
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800161 int LM0;
162 int N;
163 int offset;
164 int ndof;
165 int qb;
166 int k;
167 LM0 = 0;
Gregory Maxwell342d6542011-09-30 17:57:27 -0400168 /* Even-sized bands bigger than N=2 can be split one more time.
169 As of commit 44203907 all bands >1 are even, including custom modes.*/
170 if (N0 > 2)
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800171 {
172 N0>>=1;
173 LM0--;
174 }
Timothy B. Terriberry44992632011-01-31 10:44:03 -0800175 /* N0=1 bands can't be split down to N<2. */
176 else if (N0 <= 1)
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800177 {
Timothy B. Terriberry44992632011-01-31 10:44:03 -0800178 LM0=IMIN(i,1);
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800179 N0<<=LM0;
180 }
181 /* Compute the cost for the lowest-level PVQ of a fully split
182 band. */
183 pcache = bits + cindex[(LM0+1)*m->nbEBands+j];
184 max_bits = pcache[pcache[0]]+1;
185 /* Add in the cost of coding regular splits. */
186 N = N0;
187 for(k=0;k<i-LM0;k++){
188 max_bits <<= 1;
189 /* Offset the number of qtheta bits by log2(N)/2
190 + QTHETA_OFFSET compared to their "fair share" of
191 total/N */
Gregory Maxwelld6335ab2011-08-30 19:50:41 -0400192 offset = ((m->logN[j]+((LM0+k)<<BITRES))>>1)-QTHETA_OFFSET;
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800193 /* The number of qtheta bits we'll allocate if the remainder
Timothy B. Terriberryb5d123a2011-01-30 22:18:12 -0800194 is to be max_bits.
195 The average measured cost for theta is 0.89701 times qb,
196 approximated here as 459/512. */
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400197 num=459*(opus_int32)((2*N-1)*offset+max_bits);
198 den=((opus_int32)(2*N-1)<<9)-459;
Timothy B. Terriberryb5d123a2011-01-30 22:18:12 -0800199 qb = IMIN((num+(den>>1))/den, 57);
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800200 celt_assert(qb >= 0);
Timothy B. Terriberryb5d123a2011-01-30 22:18:12 -0800201 max_bits += qb;
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800202 N <<= 1;
203 }
204 /* Add in the cost of a stereo split, if necessary. */
205 if (C==2)
206 {
207 max_bits <<= 1;
Gregory Maxwelld6335ab2011-08-30 19:50:41 -0400208 offset = ((m->logN[j]+(i<<BITRES))>>1)-(N==2?QTHETA_OFFSET_TWOPHASE:QTHETA_OFFSET);
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800209 ndof = 2*N-1-(N==2);
Timothy B. Terriberryb5d123a2011-01-30 22:18:12 -0800210 /* The average measured cost for theta with the step PDF is
211 0.95164 times qb, approximated here as 487/512. */
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400212 num = (N==2?512:487)*(opus_int32)(max_bits+ndof*offset);
213 den = ((opus_int32)ndof<<9)-(N==2?512:487);
Timothy B. Terriberryb5d123a2011-01-30 22:18:12 -0800214 qb = IMIN((num+(den>>1))/den, (N==2?64:61));
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800215 celt_assert(qb >= 0);
Timothy B. Terriberryb5d123a2011-01-30 22:18:12 -0800216 max_bits += qb;
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800217 }
218 /* Add the fine bits we'll use. */
219 /* Compensate for the extra DoF in stereo */
220 ndof = C*N + ((C==2 && N>2) ? 1 : 0);
221 /* Offset the number of fine bits by log2(N)/2 + FINE_OFFSET
222 compared to their "fair share" of total/N */
Gregory Maxwelld6335ab2011-08-30 19:50:41 -0400223 offset = ((m->logN[j] + (i<<BITRES))>>1)-FINE_OFFSET;
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800224 /* N=2 is the only point that doesn't match the curve */
225 if (N==2)
226 offset += 1<<BITRES>>2;
227 /* The number of fine bits we'll allocate if the remainder is
228 to be max_bits. */
229 num = max_bits+ndof*offset;
Gregory Maxwelld6335ab2011-08-30 19:50:41 -0400230 den = (ndof-1)<<BITRES;
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800231 qb = IMIN((num+(den>>1))/den, MAX_FINE_BITS);
232 celt_assert(qb >= 0);
233 max_bits += C*qb<<BITRES;
234 }
Gregory Maxwelld6335ab2011-08-30 19:50:41 -0400235 max_bits = (4*max_bits/(C*((m->eBands[j+1]-m->eBands[j])<<i)))-64;
Timothy B. Terriberryce6d0902011-02-01 17:41:12 -0800236 celt_assert(max_bits >= 0);
237 celt_assert(max_bits < 256);
238 *cap++ = (unsigned char)max_bits;
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800239 }
240 }
241 }
Jean-Marc Valina6631742008-01-16 17:16:04 +1100242}
243
Timothy B. Terriberryb5d123a2011-01-30 22:18:12 -0800244#endif /* CUSTOM_MODES */
Jean-Marc Valin33ddd792008-01-14 17:39:01 +1100245
Jean-Marc Valinae319fe2010-09-13 09:46:54 -0400246#define ALLOC_STEPS 6
Jean-Marc Valin33ddd792008-01-14 17:39:01 +1100247
Gregory Maxwell8e447672010-12-16 14:22:51 -0500248static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int skip_start,
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400249 const int *bits1, const int *bits2, const int *thresh, const int *cap, opus_int32 total, opus_int32 *_balance,
Timothy B. Terriberry948d27c2011-01-31 12:28:12 -0800250 int skip_rsv, int *intensity, int intensity_rsv, int *dual_stereo, int dual_stereo_rsv, int *bits,
Ralph Giles120800f2011-11-25 13:02:00 -0800251 int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev)
Jean-Marc Valinc6b43902008-01-16 22:04:17 +1100252{
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400253 opus_int32 psum;
Jean-Marc Valinebf72da2008-09-09 23:21:36 -0400254 int lo, hi;
Jean-Marc Valina6a53ab2010-08-31 16:46:40 -0400255 int i, j;
Jean-Marc Valinaead79b2010-05-11 07:34:24 -0400256 int logM;
Gregory Maxwell9b98aaa2010-12-19 02:50:12 -0500257 int stereo;
Jean-Marc Valinb801da52010-09-28 14:56:20 -0400258 int codedBands=-1;
Jean-Marc Valin9651ffd2010-12-14 16:41:03 -0500259 int alloc_floor;
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400260 opus_int32 left, percoeff;
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500261 int done;
Timothy B. Terriberry66c5ab42010-12-16 08:39:37 -0800262 int balance;
Jean-Marc Valin8600f692008-02-29 15:14:12 +1100263 SAVE_STACK;
Jean-Marc Valinaead79b2010-05-11 07:34:24 -0400264
Jean-Marc Valin9651ffd2010-12-14 16:41:03 -0500265 alloc_floor = C<<BITRES;
Gregory Maxwell9b98aaa2010-12-19 02:50:12 -0500266 stereo = C>1;
Jean-Marc Valin9651ffd2010-12-14 16:41:03 -0500267
Jean-Marc Valin5c0c9362010-08-31 10:11:11 -0400268 logM = LM<<BITRES;
Jean-Marc Valinc6b43902008-01-16 22:04:17 +1100269 lo = 0;
Jean-Marc Valina6a53ab2010-08-31 16:46:40 -0400270 hi = 1<<ALLOC_STEPS;
271 for (i=0;i<ALLOC_STEPS;i++)
Jean-Marc Valinc6b43902008-01-16 22:04:17 +1100272 {
273 int mid = (lo+hi)>>1;
Jean-Marc Valinb801da52010-09-28 14:56:20 -0400274 psum = 0;
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500275 done = 0;
Timothy B. Terriberry405e6a92010-12-15 20:46:09 -0800276 for (j=end;j-->start;)
Jean-Marc Valinb801da52010-09-28 14:56:20 -0400277 {
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400278 int tmp = bits1[j] + (mid*(opus_int32)bits2[j]>>ALLOC_STEPS);
Timothy B. Terriberry405e6a92010-12-15 20:46:09 -0800279 if (tmp >= thresh[j] || done)
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500280 {
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500281 done = 1;
Timothy B. Terriberry405e6a92010-12-15 20:46:09 -0800282 /* Don't allocate more than we can actually use */
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800283 psum += IMIN(tmp, cap[j]);
Timothy B. Terriberry405e6a92010-12-15 20:46:09 -0800284 } else {
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500285 if (tmp >= alloc_floor)
286 psum += alloc_floor;
287 }
Jean-Marc Valinb801da52010-09-28 14:56:20 -0400288 }
Timothy B. Terriberry4777f062010-12-15 06:56:00 -0800289 if (psum > total)
Jean-Marc Valinc6b43902008-01-16 22:04:17 +1100290 hi = mid;
291 else
292 lo = mid;
293 }
Jean-Marc Valin825ead82008-09-09 00:15:40 -0400294 psum = 0;
Jean-Marc Valina85657b2008-02-20 11:59:30 +1100295 /*printf ("interp bisection gave %d\n", lo);*/
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500296 done = 0;
Timothy B. Terriberry405e6a92010-12-15 20:46:09 -0800297 for (j=end;j-->start;)
Jean-Marc Valin6775de32008-08-02 08:14:42 -0400298 {
Jean-Marc Valinb801da52010-09-28 14:56:20 -0400299 int tmp = bits1[j] + (lo*bits2[j]>>ALLOC_STEPS);
Timothy B. Terriberry405e6a92010-12-15 20:46:09 -0800300 if (tmp < thresh[j] && !done)
Jean-Marc Valinb801da52010-09-28 14:56:20 -0400301 {
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500302 if (tmp >= alloc_floor)
303 tmp = alloc_floor;
Jean-Marc Valin9651ffd2010-12-14 16:41:03 -0500304 else
305 tmp = 0;
Timothy B. Terriberry405e6a92010-12-15 20:46:09 -0800306 } else
307 done = 1;
Jean-Marc Valin52dc66b2010-12-06 21:31:15 -0500308 /* Don't allocate more than we can actually use */
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800309 tmp = IMIN(tmp, cap[j]);
Jean-Marc Valin9651ffd2010-12-14 16:41:03 -0500310 bits[j] = tmp;
311 psum += tmp;
Jean-Marc Valin825ead82008-09-09 00:15:40 -0400312 }
Jean-Marc Valin9651ffd2010-12-14 16:41:03 -0500313
Timothy B. Terriberry7627b9f2010-12-15 08:22:14 -0800314 /* Decide which bands to skip, working backwards from the end. */
315 for (codedBands=end;;codedBands--)
Jean-Marc Valindfd6e712010-12-09 23:23:34 -0500316 {
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500317 int band_width;
318 int band_bits;
319 int rem;
Timothy B. Terriberry7627b9f2010-12-15 08:22:14 -0800320 j = codedBands-1;
Timothy B. Terriberry76ea41e2010-12-16 14:39:58 -0800321 /* Never skip the first band, nor a band that has been boosted by
322 dynalloc.
323 In the first case, we'd be coding a bit to signal we're going to waste
324 all the other bits.
325 In the second case, we'd be coding a bit to redistribute all the bits
326 we just signaled should be cocentrated in this band. */
Gregory Maxwell8e447672010-12-16 14:22:51 -0500327 if (j<=skip_start)
Timothy B. Terriberry76ea41e2010-12-16 14:39:58 -0800328 {
Timothy B. Terriberry76469c62011-01-07 09:18:34 -0800329 /* Give the bit we reserved to end skipping back. */
330 total += skip_rsv;
Timothy B. Terriberry7627b9f2010-12-15 08:22:14 -0800331 break;
Timothy B. Terriberry76ea41e2010-12-16 14:39:58 -0800332 }
Timothy B. Terriberry76469c62011-01-07 09:18:34 -0800333 /*Figure out how many left-over bits we would be adding to this band.
334 This can include bits we've stolen back from higher, skipped bands.*/
335 left = total-psum;
336 percoeff = left/(m->eBands[codedBands]-m->eBands[start]);
337 left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
Timothy B. Terriberry4777f062010-12-15 06:56:00 -0800338 rem = IMAX(left-(m->eBands[j]-m->eBands[start]),0);
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500339 band_width = m->eBands[codedBands]-m->eBands[j];
Timothy B. Terriberry285bc372011-02-06 13:29:00 -0800340 band_bits = (int)(bits[j] + percoeff*band_width + rem);
Timothy B. Terriberry283a9b62010-12-15 05:35:54 -0800341 /*Only code a skip decision if we're above the threshold for this band.
342 Otherwise it is force-skipped.
Timothy B. Terriberry405e6a92010-12-15 20:46:09 -0800343 This ensures that we have enough bits to code the skip flag.*/
344 if (band_bits >= IMAX(thresh[j], alloc_floor+(1<<BITRES)))
Jean-Marc Valindfd6e712010-12-09 23:23:34 -0500345 {
Timothy B. Terriberryb2f59002010-12-15 05:12:43 -0800346 if (encode)
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500347 {
Timothy B. Terriberry7cbf1682010-12-14 21:55:49 -0800348 /*This if() block is the only part of the allocation function that
349 is not a mandatory part of the bitstream: any bands we choose to
350 skip here must be explicitly signaled.*/
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500351 /*Choose a threshold with some hysteresis to keep bands from
352 fluctuating in and out.*/
Jean-Marc Valinf334c822011-08-11 16:21:58 -0400353#ifdef FUZZING
354 if ((rand()&0x1) == 0)
355#else
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500356 if (band_bits > ((j<prev?7:9)*band_width<<LM<<BITRES)>>4)
Jean-Marc Valinf334c822011-08-11 16:21:58 -0400357#endif
Timothy B. Terriberryb2f59002010-12-15 05:12:43 -0800358 {
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800359 ec_enc_bit_logp(ec, 1, 1);
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500360 break;
Timothy B. Terriberryb2f59002010-12-15 05:12:43 -0800361 }
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800362 ec_enc_bit_logp(ec, 0, 1);
363 } else if (ec_dec_bit_logp(ec, 1)) {
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500364 break;
Timothy B. Terriberryb2f59002010-12-15 05:12:43 -0800365 }
366 /*We used a bit to skip this band.*/
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500367 psum += 1<<BITRES;
368 band_bits -= 1<<BITRES;
Jean-Marc Valindfd6e712010-12-09 23:23:34 -0500369 }
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500370 /*Reclaim the bits originally allocated to this band.*/
Timothy B. Terriberry76469c62011-01-07 09:18:34 -0800371 psum -= bits[j]+intensity_rsv;
372 if (intensity_rsv > 0)
373 intensity_rsv = LOG2_FRAC_TABLE[j-start];
374 psum += intensity_rsv;
Timothy B. Terriberry7cbf1682010-12-14 21:55:49 -0800375 if (band_bits >= alloc_floor)
Jean-Marc Valindcacb732010-12-14 13:39:30 -0500376 {
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500377 /*If we have enough for a fine energy bit per channel, use it.*/
Jean-Marc Valin9651ffd2010-12-14 16:41:03 -0500378 psum += alloc_floor;
Timothy B. Terriberry7cbf1682010-12-14 21:55:49 -0800379 bits[j] = alloc_floor;
Jean-Marc Valin9651ffd2010-12-14 16:41:03 -0500380 } else {
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500381 /*Otherwise this band gets nothing at all.*/
Timothy B. Terriberry7cbf1682010-12-14 21:55:49 -0800382 bits[j] = 0;
Jean-Marc Valindcacb732010-12-14 13:39:30 -0500383 }
Jean-Marc Valindfd6e712010-12-09 23:23:34 -0500384 }
Jean-Marc Valinffe10572010-12-15 00:36:41 -0500385
Timothy B. Terriberry76469c62011-01-07 09:18:34 -0800386 celt_assert(codedBands > start);
387 /* Code the intensity and dual stereo parameters. */
388 if (intensity_rsv > 0)
389 {
390 if (encode)
Jean-Marc Valinfb031112010-11-25 16:32:54 -0500391 {
Timothy B. Terriberry76469c62011-01-07 09:18:34 -0800392 *intensity = IMIN(*intensity, codedBands);
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800393 ec_enc_uint(ec, *intensity-start, codedBands+1-start);
Jean-Marc Valinfb031112010-11-25 16:32:54 -0500394 }
Timothy B. Terriberry76469c62011-01-07 09:18:34 -0800395 else
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800396 *intensity = start+ec_dec_uint(ec, codedBands+1-start);
Timothy B. Terriberry76469c62011-01-07 09:18:34 -0800397 }
398 else
399 *intensity = 0;
400 if (*intensity <= start)
401 {
402 total += dual_stereo_rsv;
403 dual_stereo_rsv = 0;
404 }
405 if (dual_stereo_rsv > 0)
406 {
407 if (encode)
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800408 ec_enc_bit_logp(ec, *dual_stereo, 1);
Timothy B. Terriberry76469c62011-01-07 09:18:34 -0800409 else
Timothy B. Terriberrya093f4d2011-02-03 14:22:15 -0800410 *dual_stereo = ec_dec_bit_logp(ec, 1);
Timothy B. Terriberry76469c62011-01-07 09:18:34 -0800411 }
412 else
413 *dual_stereo = 0;
414
415 /* Allocate the remaining bits */
416 left = total-psum;
417 percoeff = left/(m->eBands[codedBands]-m->eBands[start]);
418 left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
419 for (j=start;j<codedBands;j++)
Jean-Marc Valin35a96ea2011-02-06 20:23:19 -0500420 bits[j] += ((int)percoeff*(m->eBands[j+1]-m->eBands[j]));
Timothy B. Terriberry76469c62011-01-07 09:18:34 -0800421 for (j=start;j<codedBands;j++)
422 {
Timothy B. Terriberry285bc372011-02-06 13:29:00 -0800423 int tmp = (int)IMIN(left, m->eBands[j+1]-m->eBands[j]);
Timothy B. Terriberry76469c62011-01-07 09:18:34 -0800424 bits[j] += tmp;
425 left -= tmp;
Jean-Marc Valinebf72da2008-09-09 23:21:36 -0400426 }
Jean-Marc Valin54d84c02010-11-19 11:45:37 -0500427 /*for (j=0;j<end;j++)printf("%d ", bits[j]);printf("\n");*/
Timothy B. Terriberry66c5ab42010-12-16 08:39:37 -0800428
429 balance = 0;
Timothy B. Terriberry7627b9f2010-12-15 08:22:14 -0800430 for (j=start;j<codedBands;j++)
Jean-Marc Valin9a6c4962009-02-09 00:45:48 -0500431 {
Jean-Marc Valin3f9857b2010-08-13 21:20:37 -0400432 int N0, N, den;
Jean-Marc Valin9a6c4962009-02-09 00:45:48 -0500433 int offset;
Jean-Marc Valin5c0c9362010-08-31 10:11:11 -0400434 int NClogN;
Timothy B. Terriberry13bffd22011-01-30 00:12:31 -0800435 int excess;
Jean-Marc Valin5c0c9362010-08-31 10:11:11 -0400436
Jean-Marc Valin9651ffd2010-12-14 16:41:03 -0500437 celt_assert(bits[j] >= 0);
Jean-Marc Valin36034282010-08-06 14:42:43 -0400438 N0 = m->eBands[j+1]-m->eBands[j];
Jean-Marc Valin5c0c9362010-08-31 10:11:11 -0400439 N=N0<<LM;
Timothy B. Terriberry13bffd22011-01-30 00:12:31 -0800440 bits[j] += balance;
Jean-Marc Valin5c0c9362010-08-31 10:11:11 -0400441
Timothy B. Terriberry66c5ab42010-12-16 08:39:37 -0800442 if (N>1)
Jean-Marc Valindcacb732010-12-14 13:39:30 -0500443 {
Timothy B. Terriberry13bffd22011-01-30 00:12:31 -0800444 excess = IMAX(bits[j]-cap[j],0);
445 bits[j] -= excess;
446
Timothy B. Terriberry66c5ab42010-12-16 08:39:37 -0800447 /* Compensate for the extra DoF in stereo */
Jean-Marc Valinb44e94e2011-02-02 21:23:21 -0500448 den=(C*N+ ((C==2 && N>2 && !*dual_stereo && j<*intensity) ? 1 : 0));
Timothy B. Terriberry66c5ab42010-12-16 08:39:37 -0800449
Jean-Marc Valin17cab432011-01-28 20:56:56 -0500450 NClogN = den*(m->logN[j] + logM);
451
Timothy B. Terriberry66c5ab42010-12-16 08:39:37 -0800452 /* Offset for the number of fine bits by log2(N)/2 + FINE_OFFSET
453 compared to their "fair share" of total/N */
Jean-Marc Valin17cab432011-01-28 20:56:56 -0500454 offset = (NClogN>>1)-den*FINE_OFFSET;
Timothy B. Terriberry66c5ab42010-12-16 08:39:37 -0800455
456 /* N=2 is the only point that doesn't match the curve */
457 if (N==2)
Jean-Marc Valin17cab432011-01-28 20:56:56 -0500458 offset += den<<BITRES>>2;
Timothy B. Terriberry66c5ab42010-12-16 08:39:37 -0800459
460 /* Changing the offset for allocating the second and third
461 fine energy bit */
462 if (bits[j] + offset < den*2<<BITRES)
463 offset += NClogN>>2;
464 else if (bits[j] + offset < den*3<<BITRES)
465 offset += NClogN>>3;
466
467 /* Divide with rounding */
468 ebits[j] = IMAX(0, (bits[j] + offset + (den<<(BITRES-1))) / (den<<BITRES));
469
Timothy B. Terriberry66c5ab42010-12-16 08:39:37 -0800470 /* Make sure not to bust */
471 if (C*ebits[j] > (bits[j]>>BITRES))
Gregory Maxwell9b98aaa2010-12-19 02:50:12 -0500472 ebits[j] = bits[j] >> stereo >> BITRES;
Timothy B. Terriberry66c5ab42010-12-16 08:39:37 -0800473
Jean-Marc Valina66b7572011-01-10 13:21:04 -0500474 /* More than that is useless because that's about as far as PVQ can go */
475 ebits[j] = IMIN(ebits[j], MAX_FINE_BITS);
Timothy B. Terriberry66c5ab42010-12-16 08:39:37 -0800476
Timothy B. Terriberry949f1802010-12-30 08:38:48 -0800477 /* If we rounded down or capped this band, make it a candidate for the
478 final fine energy pass */
479 fine_priority[j] = ebits[j]*(den<<BITRES) >= bits[j]+offset;
480
Timothy B. Terriberry13bffd22011-01-30 00:12:31 -0800481 /* Remove the allocated fine bits; the rest are assigned to PVQ */
482 bits[j] -= C*ebits[j]<<BITRES;
483
Timothy B. Terriberry66c5ab42010-12-16 08:39:37 -0800484 } else {
485 /* For N=1, all bits go to fine energy except for a single sign bit */
Timothy B. Terriberry13bffd22011-01-30 00:12:31 -0800486 excess = IMAX(0,bits[j]-(C<<BITRES));
487 bits[j] -= excess;
488 ebits[j] = 0;
489 fine_priority[j] = 1;
Timothy B. Terriberry66c5ab42010-12-16 08:39:37 -0800490 }
Jean-Marc Valin3f9857b2010-08-13 21:20:37 -0400491
Timothy B. Terriberry13bffd22011-01-30 00:12:31 -0800492 /* Fine energy can't take advantage of the re-balancing in
493 quant_all_bands().
494 Instead, do the re-balancing here.*/
495 if(excess > 0)
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800496 {
Timothy B. Terriberry13bffd22011-01-30 00:12:31 -0800497 int extra_fine;
498 int extra_bits;
Gregory Maxwell75d27802011-08-30 14:02:41 -0400499 extra_fine = IMIN(excess>>(stereo+BITRES),MAX_FINE_BITS-ebits[j]);
Timothy B. Terriberry13bffd22011-01-30 00:12:31 -0800500 ebits[j] += extra_fine;
501 extra_bits = extra_fine*C<<BITRES;
502 fine_priority[j] = extra_bits >= excess-balance;
503 excess -= extra_bits;
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800504 }
Timothy B. Terriberry13bffd22011-01-30 00:12:31 -0800505 balance = excess;
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800506
Jean-Marc Valin6cbfbc32010-12-14 11:53:39 -0500507 celt_assert(bits[j] >= 0);
Jean-Marc Valin9651ffd2010-12-14 16:41:03 -0500508 celt_assert(ebits[j] >= 0);
Jean-Marc Valin9a6c4962009-02-09 00:45:48 -0500509 }
Timothy B. Terriberry948d27c2011-01-31 12:28:12 -0800510 /* Save any remaining bits over the cap for the rebalancing in
511 quant_all_bands(). */
512 *_balance = balance;
Timothy B. Terriberry13bffd22011-01-30 00:12:31 -0800513
Timothy B. Terriberry7627b9f2010-12-15 08:22:14 -0800514 /* The skipped bands use all their bits for fine energy. */
515 for (;j<end;j++)
516 {
Gregory Maxwell9b98aaa2010-12-19 02:50:12 -0500517 ebits[j] = bits[j] >> stereo >> BITRES;
Timothy B. Terriberry7627b9f2010-12-15 08:22:14 -0800518 celt_assert(C*ebits[j]<<BITRES == bits[j]);
519 bits[j] = 0;
Jean-Marc Valindf6620e2010-12-16 13:07:29 -0500520 fine_priority[j] = ebits[j]<1;
Timothy B. Terriberry7627b9f2010-12-15 08:22:14 -0800521 }
Jean-Marc Valin8600f692008-02-29 15:14:12 +1100522 RESTORE_STACK;
Jean-Marc Valinb801da52010-09-28 14:56:20 -0400523 return codedBands;
Jean-Marc Valinc6b43902008-01-16 22:04:17 +1100524}
Jean-Marc Valinf51ca492008-01-17 10:58:38 +1100525
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800526int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, const int *cap, int alloc_trim, int *intensity, int *dual_stereo,
Ralph Giles120800f2011-11-25 13:02:00 -0800527 opus_int32 total, opus_int32 *balance, int *pulses, int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev)
Jean-Marc Valinf51ca492008-01-17 10:58:38 +1100528{
Jean-Marc Valin54aab422008-12-06 07:52:48 -0500529 int lo, hi, len, j;
Jean-Marc Valinb801da52010-09-28 14:56:20 -0400530 int codedBands;
Gregory Maxwell8e447672010-12-16 14:22:51 -0500531 int skip_start;
Timothy B. Terriberry76ea41e2010-12-16 14:39:58 -0800532 int skip_rsv;
Timothy B. Terriberry76469c62011-01-07 09:18:34 -0800533 int intensity_rsv;
534 int dual_stereo_rsv;
Jean-Marc Valin31b79d12008-03-12 17:17:23 +1100535 VARDECL(int, bits1);
536 VARDECL(int, bits2);
Jean-Marc Valinc40addc2010-10-22 14:57:07 -0400537 VARDECL(int, thresh);
538 VARDECL(int, trim_offset);
Jean-Marc Valin8600f692008-02-29 15:14:12 +1100539 SAVE_STACK;
Gregory Maxwell71d39ad2011-07-30 00:00:29 -0400540
Jean-Marc Valin9651ffd2010-12-14 16:41:03 -0500541 total = IMAX(total, 0);
Jean-Marc Valinf51ca492008-01-17 10:58:38 +1100542 len = m->nbEBands;
Gregory Maxwell8e447672010-12-16 14:22:51 -0500543 skip_start = start;
Timothy B. Terriberry76ea41e2010-12-16 14:39:58 -0800544 /* Reserve a bit to signal the end of manually skipped bands. */
545 skip_rsv = total >= 1<<BITRES ? 1<<BITRES : 0;
546 total -= skip_rsv;
Timothy B. Terriberry76469c62011-01-07 09:18:34 -0800547 /* Reserve bits for the intensity and dual stereo parameters. */
548 intensity_rsv = dual_stereo_rsv = 0;
549 if (C==2)
550 {
551 intensity_rsv = LOG2_FRAC_TABLE[end-start];
552 if (intensity_rsv>total)
553 intensity_rsv = 0;
554 else
555 {
556 total -= intensity_rsv;
557 dual_stereo_rsv = total>=1<<BITRES ? 1<<BITRES : 0;
558 total -= dual_stereo_rsv;
559 }
560 }
Jean-Marc Valin9a0bba12008-02-20 14:08:50 +1100561 ALLOC(bits1, len, int);
562 ALLOC(bits2, len, int);
Jean-Marc Valinc40addc2010-10-22 14:57:07 -0400563 ALLOC(thresh, len, int);
564 ALLOC(trim_offset, len, int);
565
566 for (j=start;j<end;j++)
Jean-Marc Valin3fed34a2010-12-17 14:17:27 -0500567 {
568 /* Below this threshold, we're sure not to allocate any PVQ bits */
Jean-Marc Valin9651ffd2010-12-14 16:41:03 -0500569 thresh[j] = IMAX((C)<<BITRES, (3*(m->eBands[j+1]-m->eBands[j])<<LM<<BITRES)>>4);
Jean-Marc Valin3fed34a2010-12-17 14:17:27 -0500570 /* Tilt of the allocation curve */
Jean-Marc Valin079b9162011-02-14 13:54:59 -0500571 trim_offset[j] = C*(m->eBands[j+1]-m->eBands[j])*(alloc_trim-5-LM)*(end-j-1)
Jean-Marc Valin803ec8c2011-09-23 23:48:40 -0400572 *(1<<(LM+BITRES))>>6;
Jean-Marc Valin3fed34a2010-12-17 14:17:27 -0500573 /* Giving less resolution to single-coefficient bands because they get
574 more benefit from having one coarse value per coefficient*/
575 if ((m->eBands[j+1]-m->eBands[j])<<LM==1)
576 trim_offset[j] -= C<<BITRES;
577 }
Timothy B. Terriberry428a77d2010-12-16 16:50:16 -0800578 lo = 1;
Timothy B. Terriberryce6d0902011-02-01 17:41:12 -0800579 hi = m->nbAllocVectors - 1;
Timothy B. Terriberry428a77d2010-12-16 16:50:16 -0800580 do
Jean-Marc Valinf51ca492008-01-17 10:58:38 +1100581 {
Gregory Maxwell7c673cf2010-12-19 02:26:56 -0500582 int done = 0;
Jean-Marc Valin825ead82008-09-09 00:15:40 -0400583 int psum = 0;
Jean-Marc Valinf51ca492008-01-17 10:58:38 +1100584 int mid = (lo+hi) >> 1;
Gregory Maxwell7c673cf2010-12-19 02:26:56 -0500585 for (j=end;j-->start;)
Jean-Marc Valinf51ca492008-01-17 10:58:38 +1100586 {
Jean-Marc Valin86da2c82011-03-11 10:36:11 -0500587 int bitsj;
Jean-Marc Valinbb8fa1f2010-06-03 00:33:42 -0400588 int N = m->eBands[j+1]-m->eBands[j];
Jean-Marc Valin86da2c82011-03-11 10:36:11 -0500589 bitsj = C*N*m->allocVectors[mid*len+j]<<LM>>2;
590 if (bitsj > 0)
591 bitsj = IMAX(0, bitsj + trim_offset[j]);
592 bitsj += offsets[j];
593 if (bitsj >= thresh[j] || done)
Gregory Maxwell7c673cf2010-12-19 02:26:56 -0500594 {
595 done = 1;
596 /* Don't allocate more than we can actually use */
Jean-Marc Valin86da2c82011-03-11 10:36:11 -0500597 psum += IMIN(bitsj, cap[j]);
Gregory Maxwell7c673cf2010-12-19 02:26:56 -0500598 } else {
Jean-Marc Valin86da2c82011-03-11 10:36:11 -0500599 if (bitsj >= C<<BITRES)
Gregory Maxwell7c673cf2010-12-19 02:26:56 -0500600 psum += C<<BITRES;
601 }
Jean-Marc Valinf51ca492008-01-17 10:58:38 +1100602 }
Timothy B. Terriberry4777f062010-12-15 06:56:00 -0800603 if (psum > total)
Timothy B. Terriberry428a77d2010-12-16 16:50:16 -0800604 hi = mid - 1;
Jean-Marc Valinf51ca492008-01-17 10:58:38 +1100605 else
Timothy B. Terriberry428a77d2010-12-16 16:50:16 -0800606 lo = mid + 1;
Jean-Marc Valina85657b2008-02-20 11:59:30 +1100607 /*printf ("lo = %d, hi = %d\n", lo, hi);*/
Jean-Marc Valinf51ca492008-01-17 10:58:38 +1100608 }
Timothy B. Terriberry428a77d2010-12-16 16:50:16 -0800609 while (lo <= hi);
610 hi = lo--;
Jean-Marc Valin6775de32008-08-02 08:14:42 -0400611 /*printf ("interp between %d and %d\n", lo, hi);*/
Jean-Marc Valin525d7cf2010-07-13 14:14:16 -0400612 for (j=start;j<end;j++)
Jean-Marc Valinf51ca492008-01-17 10:58:38 +1100613 {
Jean-Marc Valin86da2c82011-03-11 10:36:11 -0500614 int bits1j, bits2j;
Jean-Marc Valinbb8fa1f2010-06-03 00:33:42 -0400615 int N = m->eBands[j+1]-m->eBands[j];
Jean-Marc Valin86da2c82011-03-11 10:36:11 -0500616 bits1j = C*N*m->allocVectors[lo*len+j]<<LM>>2;
617 bits2j = hi>=m->nbAllocVectors ?
Jean-Marc Valinec6588a2011-02-03 10:34:30 -0500618 cap[j] : C*N*m->allocVectors[hi*len+j]<<LM>>2;
Jean-Marc Valin86da2c82011-03-11 10:36:11 -0500619 if (bits1j > 0)
620 bits1j = IMAX(0, bits1j + trim_offset[j]);
621 if (bits2j > 0)
622 bits2j = IMAX(0, bits2j + trim_offset[j]);
Timothy B. Terriberry428a77d2010-12-16 16:50:16 -0800623 if (lo > 0)
Jean-Marc Valin86da2c82011-03-11 10:36:11 -0500624 bits1j += offsets[j];
625 bits2j += offsets[j];
Gregory Maxwell8e447672010-12-16 14:22:51 -0500626 if (offsets[j]>0)
627 skip_start = j;
Jean-Marc Valin86da2c82011-03-11 10:36:11 -0500628 bits2j = IMAX(0,bits2j-bits1j);
629 bits1[j] = bits1j;
630 bits2[j] = bits2j;
Jean-Marc Valinf51ca492008-01-17 10:58:38 +1100631 }
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800632 codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thresh, cap,
Timothy B. Terriberry948d27c2011-01-31 12:28:12 -0800633 total, balance, skip_rsv, intensity, intensity_rsv, dual_stereo, dual_stereo_rsv,
Jean-Marc Valinc39bb8a2011-01-26 10:50:55 -0500634 pulses, ebits, fine_priority, C, LM, ec, encode, prev);
Jean-Marc Valin825ead82008-09-09 00:15:40 -0400635 RESTORE_STACK;
Jean-Marc Valinb801da52010-09-28 14:56:20 -0400636 return codedBands;
Jean-Marc Valinf51ca492008-01-17 10:58:38 +1100637}
Jean-Marc Valinb86ed072008-01-15 16:33:21 +1100638