Jean-Marc Valin | 8b2ff0d | 2009-10-17 21:40:10 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2007-2008 CSIRO |
| 2 | Copyright (c) 2007-2009 Xiph.Org Foundation |
| 3 | Written by Jean-Marc Valin */ |
Jean-Marc Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 4 | /* |
| 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 Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 8 | |
Jean-Marc Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 9 | - Redistributions of source code must retain the above copyright |
| 10 | notice, this list of conditions and the following disclaimer. |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 11 | |
Jean-Marc Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 12 | - 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 Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 15 | |
Jean-Marc Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 16 | 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 Valin | 02fa913 | 2008-02-20 12:09:29 +1100 | [diff] [blame] | 29 | #ifdef HAVE_CONFIG_H |
| 30 | #include "config.h" |
| 31 | #endif |
| 32 | |
Jean-Marc Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 33 | #include <math.h> |
| 34 | #include "modes.h" |
| 35 | #include "cwrs.h" |
| 36 | #include "arch.h" |
Jean-Marc Valin | a663174 | 2008-01-16 17:16:04 +1100 | [diff] [blame] | 37 | #include "os_support.h" |
Jean-Marc Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 38 | |
| 39 | #include "entcode.h" |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 40 | #include "rate.h" |
Jean-Marc Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 41 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 42 | static 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 Valin | 5ad35bf | 2011-01-28 22:42:09 -0500 | [diff] [blame] | 50 | #ifdef CUSTOM_MODES |
Jean-Marc Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 51 | |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 52 | /*Determines if V(N,K) fits in a 32-bit unsigned integer. |
| 53 | N and K are themselves limited to 15 bits.*/ |
| 54 | static int fits_in32(int _n, int _k) |
Jean-Marc Valin | a663174 | 2008-01-16 17:16:04 +1100 | [diff] [blame] | 55 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 56 | static const opus_int16 maxN[15] = { |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 57 | 32767, 32767, 32767, 1476, 283, 109, 60, 40, |
| 58 | 29, 24, 20, 18, 16, 14, 13}; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 59 | static const opus_int16 maxK[15] = { |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 60 | 32767, 32767, 32767, 32767, 1172, 238, 95, 53, |
| 61 | 36, 27, 22, 18, 16, 15, 13}; |
| 62 | if (_n>=14) |
Jean-Marc Valin | a663174 | 2008-01-16 17:16:04 +1100 | [diff] [blame] | 63 | { |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 64 | if (_k>=14) |
| 65 | return 0; |
Jean-Marc Valin | cae30df | 2010-05-21 00:26:03 -0400 | [diff] [blame] | 66 | else |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 67 | return _n <= maxN[_k]; |
| 68 | } else { |
| 69 | return _k <= maxK[_n]; |
Jean-Marc Valin | a663174 | 2008-01-16 17:16:04 +1100 | [diff] [blame] | 70 | } |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void compute_pulse_cache(CELTMode *m, int LM) |
| 74 | { |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 75 | int C; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 76 | int i; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 77 | int j; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 78 | int curr=0; |
| 79 | int nbEntries=0; |
| 80 | int entryN[100], entryK[100], entryI[100]; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 81 | const opus_int16 *eBands = m->eBands; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 82 | PulseCache *cache = &m->cache; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 83 | opus_int16 *cindex; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 84 | unsigned char *bits; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 85 | unsigned char *cap; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 86 | |
Jean-Marc Valin | 382cad4 | 2011-08-30 01:39:40 -0400 | [diff] [blame] | 87 | cindex = opus_alloc(sizeof(cache->index[0])*m->nbEBands*(LM+2)); |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 88 | cache->index = cindex; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 89 | |
Jean-Marc Valin | 7fff572 | 2010-10-18 16:20:00 -0400 | [diff] [blame] | 90 | /* Scan for all unique band sizes */ |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 91 | for (i=0;i<=LM+1;i++) |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 92 | { |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 93 | for (j=0;j<m->nbEBands;j++) |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 94 | { |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 95 | int k; |
| 96 | int N = (eBands[j+1]-eBands[j])<<i>>1; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 97 | cindex[i*m->nbEBands+j] = -1; |
Jean-Marc Valin | 7fff572 | 2010-10-18 16:20:00 -0400 | [diff] [blame] | 98 | /* Find other bands that have the same size */ |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 99 | for (k=0;k<=i;k++) |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 100 | { |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 101 | int n; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 102 | for (n=0;n<m->nbEBands && (k!=i || n<j);n++) |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 103 | { |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 104 | if (N == (eBands[n+1]-eBands[n])<<k>>1) |
| 105 | { |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 106 | cindex[i*m->nbEBands+j] = cindex[k*m->nbEBands+n]; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 107 | break; |
| 108 | } |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 109 | } |
| 110 | } |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 111 | if (cache->index[i*m->nbEBands+j] == -1 && N!=0) |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 112 | { |
| 113 | int K; |
| 114 | entryN[nbEntries] = N; |
| 115 | K = 0; |
Jean-Marc Valin | 7fff572 | 2010-10-18 16:20:00 -0400 | [diff] [blame] | 116 | while (fits_in32(N,get_pulses(K+1)) && K<MAX_PSEUDO) |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 117 | K++; |
| 118 | entryK[nbEntries] = K; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 119 | cindex[i*m->nbEBands+j] = curr; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 120 | entryI[nbEntries] = curr; |
| 121 | |
| 122 | curr += K+1; |
| 123 | nbEntries++; |
| 124 | } |
| 125 | } |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 126 | } |
Jean-Marc Valin | 382cad4 | 2011-08-30 01:39:40 -0400 | [diff] [blame] | 127 | bits = opus_alloc(sizeof(unsigned char)*curr); |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 128 | cache->bits = bits; |
| 129 | cache->size = curr; |
Jean-Marc Valin | 7fff572 | 2010-10-18 16:20:00 -0400 | [diff] [blame] | 130 | /* Compute the cache for all unique sizes */ |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 131 | for (i=0;i<nbEntries;i++) |
| 132 | { |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 133 | unsigned char *ptr = bits+entryI[i]; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 134 | opus_int16 tmp[MAX_PULSES+1]; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 135 | 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. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 140 | |
| 141 | /* Compute the maximum rate for each band at which we'll reliably use as |
| 142 | many bits as we ask for. */ |
Jean-Marc Valin | 382cad4 | 2011-08-30 01:39:40 -0400 | [diff] [blame] | 143 | cache->caps = cap = opus_alloc(sizeof(cache->caps[0])*(LM+1)*2*m->nbEBands); |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 144 | for (i=0;i<=LM;i++) |
| 145 | { |
| 146 | for (C=1;C<=2;C++) |
| 147 | { |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 148 | for (j=0;j<m->nbEBands;j++) |
| 149 | { |
| 150 | int N0; |
| 151 | int max_bits; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 152 | N0 = m->eBands[j+1]-m->eBands[j]; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 153 | /* N=1 bands only have a sign bit and fine bits. */ |
| 154 | if (N0<<i == 1) |
Timothy B. Terriberry | b5d123a | 2011-01-30 22:18:12 -0800 | [diff] [blame] | 155 | max_bits = C*(1+MAX_FINE_BITS)<<BITRES; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 156 | else |
| 157 | { |
| 158 | const unsigned char *pcache; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 159 | opus_int32 num; |
| 160 | opus_int32 den; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 161 | int LM0; |
| 162 | int N; |
| 163 | int offset; |
| 164 | int ndof; |
| 165 | int qb; |
| 166 | int k; |
| 167 | LM0 = 0; |
Gregory Maxwell | 342d654 | 2011-09-30 17:57:27 -0400 | [diff] [blame] | 168 | /* 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. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 171 | { |
| 172 | N0>>=1; |
| 173 | LM0--; |
| 174 | } |
Timothy B. Terriberry | 4499263 | 2011-01-31 10:44:03 -0800 | [diff] [blame] | 175 | /* N0=1 bands can't be split down to N<2. */ |
| 176 | else if (N0 <= 1) |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 177 | { |
Timothy B. Terriberry | 4499263 | 2011-01-31 10:44:03 -0800 | [diff] [blame] | 178 | LM0=IMIN(i,1); |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 179 | 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 Maxwell | d6335ab | 2011-08-30 19:50:41 -0400 | [diff] [blame] | 192 | offset = ((m->logN[j]+((LM0+k)<<BITRES))>>1)-QTHETA_OFFSET; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 193 | /* The number of qtheta bits we'll allocate if the remainder |
Timothy B. Terriberry | b5d123a | 2011-01-30 22:18:12 -0800 | [diff] [blame] | 194 | 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 Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 197 | num=459*(opus_int32)((2*N-1)*offset+max_bits); |
| 198 | den=((opus_int32)(2*N-1)<<9)-459; |
Timothy B. Terriberry | b5d123a | 2011-01-30 22:18:12 -0800 | [diff] [blame] | 199 | qb = IMIN((num+(den>>1))/den, 57); |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 200 | celt_assert(qb >= 0); |
Timothy B. Terriberry | b5d123a | 2011-01-30 22:18:12 -0800 | [diff] [blame] | 201 | max_bits += qb; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 202 | N <<= 1; |
| 203 | } |
| 204 | /* Add in the cost of a stereo split, if necessary. */ |
| 205 | if (C==2) |
| 206 | { |
| 207 | max_bits <<= 1; |
Gregory Maxwell | d6335ab | 2011-08-30 19:50:41 -0400 | [diff] [blame] | 208 | offset = ((m->logN[j]+(i<<BITRES))>>1)-(N==2?QTHETA_OFFSET_TWOPHASE:QTHETA_OFFSET); |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 209 | ndof = 2*N-1-(N==2); |
Timothy B. Terriberry | b5d123a | 2011-01-30 22:18:12 -0800 | [diff] [blame] | 210 | /* The average measured cost for theta with the step PDF is |
| 211 | 0.95164 times qb, approximated here as 487/512. */ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 212 | num = (N==2?512:487)*(opus_int32)(max_bits+ndof*offset); |
| 213 | den = ((opus_int32)ndof<<9)-(N==2?512:487); |
Timothy B. Terriberry | b5d123a | 2011-01-30 22:18:12 -0800 | [diff] [blame] | 214 | qb = IMIN((num+(den>>1))/den, (N==2?64:61)); |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 215 | celt_assert(qb >= 0); |
Timothy B. Terriberry | b5d123a | 2011-01-30 22:18:12 -0800 | [diff] [blame] | 216 | max_bits += qb; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 217 | } |
| 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 Maxwell | d6335ab | 2011-08-30 19:50:41 -0400 | [diff] [blame] | 223 | offset = ((m->logN[j] + (i<<BITRES))>>1)-FINE_OFFSET; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 224 | /* 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 Maxwell | d6335ab | 2011-08-30 19:50:41 -0400 | [diff] [blame] | 230 | den = (ndof-1)<<BITRES; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 231 | qb = IMIN((num+(den>>1))/den, MAX_FINE_BITS); |
| 232 | celt_assert(qb >= 0); |
| 233 | max_bits += C*qb<<BITRES; |
| 234 | } |
Gregory Maxwell | d6335ab | 2011-08-30 19:50:41 -0400 | [diff] [blame] | 235 | max_bits = (4*max_bits/(C*((m->eBands[j+1]-m->eBands[j])<<i)))-64; |
Timothy B. Terriberry | ce6d090 | 2011-02-01 17:41:12 -0800 | [diff] [blame] | 236 | celt_assert(max_bits >= 0); |
| 237 | celt_assert(max_bits < 256); |
| 238 | *cap++ = (unsigned char)max_bits; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |
Jean-Marc Valin | a663174 | 2008-01-16 17:16:04 +1100 | [diff] [blame] | 242 | } |
| 243 | |
Timothy B. Terriberry | b5d123a | 2011-01-30 22:18:12 -0800 | [diff] [blame] | 244 | #endif /* CUSTOM_MODES */ |
Jean-Marc Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 245 | |
Jean-Marc Valin | ae319fe | 2010-09-13 09:46:54 -0400 | [diff] [blame] | 246 | #define ALLOC_STEPS 6 |
Jean-Marc Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 247 | |
Gregory Maxwell | 8e44767 | 2010-12-16 14:22:51 -0500 | [diff] [blame] | 248 | static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int skip_start, |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 249 | const int *bits1, const int *bits2, const int *thresh, const int *cap, opus_int32 total, opus_int32 *_balance, |
Timothy B. Terriberry | 948d27c | 2011-01-31 12:28:12 -0800 | [diff] [blame] | 250 | int skip_rsv, int *intensity, int intensity_rsv, int *dual_stereo, int dual_stereo_rsv, int *bits, |
Ralph Giles | 120800f | 2011-11-25 13:02:00 -0800 | [diff] [blame] | 251 | int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev) |
Jean-Marc Valin | c6b4390 | 2008-01-16 22:04:17 +1100 | [diff] [blame] | 252 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 253 | opus_int32 psum; |
Jean-Marc Valin | ebf72da | 2008-09-09 23:21:36 -0400 | [diff] [blame] | 254 | int lo, hi; |
Jean-Marc Valin | a6a53ab | 2010-08-31 16:46:40 -0400 | [diff] [blame] | 255 | int i, j; |
Jean-Marc Valin | aead79b | 2010-05-11 07:34:24 -0400 | [diff] [blame] | 256 | int logM; |
Gregory Maxwell | 9b98aaa | 2010-12-19 02:50:12 -0500 | [diff] [blame] | 257 | int stereo; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 258 | int codedBands=-1; |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 259 | int alloc_floor; |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 260 | opus_int32 left, percoeff; |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 261 | int done; |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 262 | int balance; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 263 | SAVE_STACK; |
Jean-Marc Valin | aead79b | 2010-05-11 07:34:24 -0400 | [diff] [blame] | 264 | |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 265 | alloc_floor = C<<BITRES; |
Gregory Maxwell | 9b98aaa | 2010-12-19 02:50:12 -0500 | [diff] [blame] | 266 | stereo = C>1; |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 267 | |
Jean-Marc Valin | 5c0c936 | 2010-08-31 10:11:11 -0400 | [diff] [blame] | 268 | logM = LM<<BITRES; |
Jean-Marc Valin | c6b4390 | 2008-01-16 22:04:17 +1100 | [diff] [blame] | 269 | lo = 0; |
Jean-Marc Valin | a6a53ab | 2010-08-31 16:46:40 -0400 | [diff] [blame] | 270 | hi = 1<<ALLOC_STEPS; |
| 271 | for (i=0;i<ALLOC_STEPS;i++) |
Jean-Marc Valin | c6b4390 | 2008-01-16 22:04:17 +1100 | [diff] [blame] | 272 | { |
| 273 | int mid = (lo+hi)>>1; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 274 | psum = 0; |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 275 | done = 0; |
Timothy B. Terriberry | 405e6a9 | 2010-12-15 20:46:09 -0800 | [diff] [blame] | 276 | for (j=end;j-->start;) |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 277 | { |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 278 | int tmp = bits1[j] + (mid*(opus_int32)bits2[j]>>ALLOC_STEPS); |
Timothy B. Terriberry | 405e6a9 | 2010-12-15 20:46:09 -0800 | [diff] [blame] | 279 | if (tmp >= thresh[j] || done) |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 280 | { |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 281 | done = 1; |
Timothy B. Terriberry | 405e6a9 | 2010-12-15 20:46:09 -0800 | [diff] [blame] | 282 | /* Don't allocate more than we can actually use */ |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 283 | psum += IMIN(tmp, cap[j]); |
Timothy B. Terriberry | 405e6a9 | 2010-12-15 20:46:09 -0800 | [diff] [blame] | 284 | } else { |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 285 | if (tmp >= alloc_floor) |
| 286 | psum += alloc_floor; |
| 287 | } |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 288 | } |
Timothy B. Terriberry | 4777f06 | 2010-12-15 06:56:00 -0800 | [diff] [blame] | 289 | if (psum > total) |
Jean-Marc Valin | c6b4390 | 2008-01-16 22:04:17 +1100 | [diff] [blame] | 290 | hi = mid; |
| 291 | else |
| 292 | lo = mid; |
| 293 | } |
Jean-Marc Valin | 825ead8 | 2008-09-09 00:15:40 -0400 | [diff] [blame] | 294 | psum = 0; |
Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 295 | /*printf ("interp bisection gave %d\n", lo);*/ |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 296 | done = 0; |
Timothy B. Terriberry | 405e6a9 | 2010-12-15 20:46:09 -0800 | [diff] [blame] | 297 | for (j=end;j-->start;) |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 298 | { |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 299 | int tmp = bits1[j] + (lo*bits2[j]>>ALLOC_STEPS); |
Timothy B. Terriberry | 405e6a9 | 2010-12-15 20:46:09 -0800 | [diff] [blame] | 300 | if (tmp < thresh[j] && !done) |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 301 | { |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 302 | if (tmp >= alloc_floor) |
| 303 | tmp = alloc_floor; |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 304 | else |
| 305 | tmp = 0; |
Timothy B. Terriberry | 405e6a9 | 2010-12-15 20:46:09 -0800 | [diff] [blame] | 306 | } else |
| 307 | done = 1; |
Jean-Marc Valin | 52dc66b | 2010-12-06 21:31:15 -0500 | [diff] [blame] | 308 | /* Don't allocate more than we can actually use */ |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 309 | tmp = IMIN(tmp, cap[j]); |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 310 | bits[j] = tmp; |
| 311 | psum += tmp; |
Jean-Marc Valin | 825ead8 | 2008-09-09 00:15:40 -0400 | [diff] [blame] | 312 | } |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 313 | |
Timothy B. Terriberry | 7627b9f | 2010-12-15 08:22:14 -0800 | [diff] [blame] | 314 | /* Decide which bands to skip, working backwards from the end. */ |
| 315 | for (codedBands=end;;codedBands--) |
Jean-Marc Valin | dfd6e71 | 2010-12-09 23:23:34 -0500 | [diff] [blame] | 316 | { |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 317 | int band_width; |
| 318 | int band_bits; |
| 319 | int rem; |
Timothy B. Terriberry | 7627b9f | 2010-12-15 08:22:14 -0800 | [diff] [blame] | 320 | j = codedBands-1; |
Timothy B. Terriberry | 76ea41e | 2010-12-16 14:39:58 -0800 | [diff] [blame] | 321 | /* 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 Maxwell | 8e44767 | 2010-12-16 14:22:51 -0500 | [diff] [blame] | 327 | if (j<=skip_start) |
Timothy B. Terriberry | 76ea41e | 2010-12-16 14:39:58 -0800 | [diff] [blame] | 328 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 329 | /* Give the bit we reserved to end skipping back. */ |
| 330 | total += skip_rsv; |
Timothy B. Terriberry | 7627b9f | 2010-12-15 08:22:14 -0800 | [diff] [blame] | 331 | break; |
Timothy B. Terriberry | 76ea41e | 2010-12-16 14:39:58 -0800 | [diff] [blame] | 332 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 333 | /*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. Terriberry | 4777f06 | 2010-12-15 06:56:00 -0800 | [diff] [blame] | 338 | rem = IMAX(left-(m->eBands[j]-m->eBands[start]),0); |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 339 | band_width = m->eBands[codedBands]-m->eBands[j]; |
Timothy B. Terriberry | 285bc37 | 2011-02-06 13:29:00 -0800 | [diff] [blame] | 340 | band_bits = (int)(bits[j] + percoeff*band_width + rem); |
Timothy B. Terriberry | 283a9b6 | 2010-12-15 05:35:54 -0800 | [diff] [blame] | 341 | /*Only code a skip decision if we're above the threshold for this band. |
| 342 | Otherwise it is force-skipped. |
Timothy B. Terriberry | 405e6a9 | 2010-12-15 20:46:09 -0800 | [diff] [blame] | 343 | 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 Valin | dfd6e71 | 2010-12-09 23:23:34 -0500 | [diff] [blame] | 345 | { |
Timothy B. Terriberry | b2f5900 | 2010-12-15 05:12:43 -0800 | [diff] [blame] | 346 | if (encode) |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 347 | { |
Timothy B. Terriberry | 7cbf168 | 2010-12-14 21:55:49 -0800 | [diff] [blame] | 348 | /*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 Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 351 | /*Choose a threshold with some hysteresis to keep bands from |
| 352 | fluctuating in and out.*/ |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 353 | #ifdef FUZZING |
| 354 | if ((rand()&0x1) == 0) |
| 355 | #else |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 356 | if (band_bits > ((j<prev?7:9)*band_width<<LM<<BITRES)>>4) |
Jean-Marc Valin | f334c82 | 2011-08-11 16:21:58 -0400 | [diff] [blame] | 357 | #endif |
Timothy B. Terriberry | b2f5900 | 2010-12-15 05:12:43 -0800 | [diff] [blame] | 358 | { |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 359 | ec_enc_bit_logp(ec, 1, 1); |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 360 | break; |
Timothy B. Terriberry | b2f5900 | 2010-12-15 05:12:43 -0800 | [diff] [blame] | 361 | } |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 362 | ec_enc_bit_logp(ec, 0, 1); |
| 363 | } else if (ec_dec_bit_logp(ec, 1)) { |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 364 | break; |
Timothy B. Terriberry | b2f5900 | 2010-12-15 05:12:43 -0800 | [diff] [blame] | 365 | } |
| 366 | /*We used a bit to skip this band.*/ |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 367 | psum += 1<<BITRES; |
| 368 | band_bits -= 1<<BITRES; |
Jean-Marc Valin | dfd6e71 | 2010-12-09 23:23:34 -0500 | [diff] [blame] | 369 | } |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 370 | /*Reclaim the bits originally allocated to this band.*/ |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 371 | psum -= bits[j]+intensity_rsv; |
| 372 | if (intensity_rsv > 0) |
| 373 | intensity_rsv = LOG2_FRAC_TABLE[j-start]; |
| 374 | psum += intensity_rsv; |
Timothy B. Terriberry | 7cbf168 | 2010-12-14 21:55:49 -0800 | [diff] [blame] | 375 | if (band_bits >= alloc_floor) |
Jean-Marc Valin | dcacb73 | 2010-12-14 13:39:30 -0500 | [diff] [blame] | 376 | { |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 377 | /*If we have enough for a fine energy bit per channel, use it.*/ |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 378 | psum += alloc_floor; |
Timothy B. Terriberry | 7cbf168 | 2010-12-14 21:55:49 -0800 | [diff] [blame] | 379 | bits[j] = alloc_floor; |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 380 | } else { |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 381 | /*Otherwise this band gets nothing at all.*/ |
Timothy B. Terriberry | 7cbf168 | 2010-12-14 21:55:49 -0800 | [diff] [blame] | 382 | bits[j] = 0; |
Jean-Marc Valin | dcacb73 | 2010-12-14 13:39:30 -0500 | [diff] [blame] | 383 | } |
Jean-Marc Valin | dfd6e71 | 2010-12-09 23:23:34 -0500 | [diff] [blame] | 384 | } |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 385 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 386 | celt_assert(codedBands > start); |
| 387 | /* Code the intensity and dual stereo parameters. */ |
| 388 | if (intensity_rsv > 0) |
| 389 | { |
| 390 | if (encode) |
Jean-Marc Valin | fb03111 | 2010-11-25 16:32:54 -0500 | [diff] [blame] | 391 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 392 | *intensity = IMIN(*intensity, codedBands); |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 393 | ec_enc_uint(ec, *intensity-start, codedBands+1-start); |
Jean-Marc Valin | fb03111 | 2010-11-25 16:32:54 -0500 | [diff] [blame] | 394 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 395 | else |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 396 | *intensity = start+ec_dec_uint(ec, codedBands+1-start); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 397 | } |
| 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. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 408 | ec_enc_bit_logp(ec, *dual_stereo, 1); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 409 | else |
Timothy B. Terriberry | a093f4d | 2011-02-03 14:22:15 -0800 | [diff] [blame] | 410 | *dual_stereo = ec_dec_bit_logp(ec, 1); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 411 | } |
| 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 Valin | 35a96ea | 2011-02-06 20:23:19 -0500 | [diff] [blame] | 420 | bits[j] += ((int)percoeff*(m->eBands[j+1]-m->eBands[j])); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 421 | for (j=start;j<codedBands;j++) |
| 422 | { |
Timothy B. Terriberry | 285bc37 | 2011-02-06 13:29:00 -0800 | [diff] [blame] | 423 | int tmp = (int)IMIN(left, m->eBands[j+1]-m->eBands[j]); |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 424 | bits[j] += tmp; |
| 425 | left -= tmp; |
Jean-Marc Valin | ebf72da | 2008-09-09 23:21:36 -0400 | [diff] [blame] | 426 | } |
Jean-Marc Valin | 54d84c0 | 2010-11-19 11:45:37 -0500 | [diff] [blame] | 427 | /*for (j=0;j<end;j++)printf("%d ", bits[j]);printf("\n");*/ |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 428 | |
| 429 | balance = 0; |
Timothy B. Terriberry | 7627b9f | 2010-12-15 08:22:14 -0800 | [diff] [blame] | 430 | for (j=start;j<codedBands;j++) |
Jean-Marc Valin | 9a6c496 | 2009-02-09 00:45:48 -0500 | [diff] [blame] | 431 | { |
Jean-Marc Valin | 3f9857b | 2010-08-13 21:20:37 -0400 | [diff] [blame] | 432 | int N0, N, den; |
Jean-Marc Valin | 9a6c496 | 2009-02-09 00:45:48 -0500 | [diff] [blame] | 433 | int offset; |
Jean-Marc Valin | 5c0c936 | 2010-08-31 10:11:11 -0400 | [diff] [blame] | 434 | int NClogN; |
Timothy B. Terriberry | 13bffd2 | 2011-01-30 00:12:31 -0800 | [diff] [blame] | 435 | int excess; |
Jean-Marc Valin | 5c0c936 | 2010-08-31 10:11:11 -0400 | [diff] [blame] | 436 | |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 437 | celt_assert(bits[j] >= 0); |
Jean-Marc Valin | 3603428 | 2010-08-06 14:42:43 -0400 | [diff] [blame] | 438 | N0 = m->eBands[j+1]-m->eBands[j]; |
Jean-Marc Valin | 5c0c936 | 2010-08-31 10:11:11 -0400 | [diff] [blame] | 439 | N=N0<<LM; |
Timothy B. Terriberry | 13bffd2 | 2011-01-30 00:12:31 -0800 | [diff] [blame] | 440 | bits[j] += balance; |
Jean-Marc Valin | 5c0c936 | 2010-08-31 10:11:11 -0400 | [diff] [blame] | 441 | |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 442 | if (N>1) |
Jean-Marc Valin | dcacb73 | 2010-12-14 13:39:30 -0500 | [diff] [blame] | 443 | { |
Timothy B. Terriberry | 13bffd2 | 2011-01-30 00:12:31 -0800 | [diff] [blame] | 444 | excess = IMAX(bits[j]-cap[j],0); |
| 445 | bits[j] -= excess; |
| 446 | |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 447 | /* Compensate for the extra DoF in stereo */ |
Jean-Marc Valin | b44e94e | 2011-02-02 21:23:21 -0500 | [diff] [blame] | 448 | den=(C*N+ ((C==2 && N>2 && !*dual_stereo && j<*intensity) ? 1 : 0)); |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 449 | |
Jean-Marc Valin | 17cab43 | 2011-01-28 20:56:56 -0500 | [diff] [blame] | 450 | NClogN = den*(m->logN[j] + logM); |
| 451 | |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 452 | /* Offset for the number of fine bits by log2(N)/2 + FINE_OFFSET |
| 453 | compared to their "fair share" of total/N */ |
Jean-Marc Valin | 17cab43 | 2011-01-28 20:56:56 -0500 | [diff] [blame] | 454 | offset = (NClogN>>1)-den*FINE_OFFSET; |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 455 | |
| 456 | /* N=2 is the only point that doesn't match the curve */ |
| 457 | if (N==2) |
Jean-Marc Valin | 17cab43 | 2011-01-28 20:56:56 -0500 | [diff] [blame] | 458 | offset += den<<BITRES>>2; |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 459 | |
| 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. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 470 | /* Make sure not to bust */ |
| 471 | if (C*ebits[j] > (bits[j]>>BITRES)) |
Gregory Maxwell | 9b98aaa | 2010-12-19 02:50:12 -0500 | [diff] [blame] | 472 | ebits[j] = bits[j] >> stereo >> BITRES; |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 473 | |
Jean-Marc Valin | a66b757 | 2011-01-10 13:21:04 -0500 | [diff] [blame] | 474 | /* 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. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 476 | |
Timothy B. Terriberry | 949f180 | 2010-12-30 08:38:48 -0800 | [diff] [blame] | 477 | /* 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. Terriberry | 13bffd2 | 2011-01-30 00:12:31 -0800 | [diff] [blame] | 481 | /* Remove the allocated fine bits; the rest are assigned to PVQ */ |
| 482 | bits[j] -= C*ebits[j]<<BITRES; |
| 483 | |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 484 | } else { |
| 485 | /* For N=1, all bits go to fine energy except for a single sign bit */ |
Timothy B. Terriberry | 13bffd2 | 2011-01-30 00:12:31 -0800 | [diff] [blame] | 486 | excess = IMAX(0,bits[j]-(C<<BITRES)); |
| 487 | bits[j] -= excess; |
| 488 | ebits[j] = 0; |
| 489 | fine_priority[j] = 1; |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 490 | } |
Jean-Marc Valin | 3f9857b | 2010-08-13 21:20:37 -0400 | [diff] [blame] | 491 | |
Timothy B. Terriberry | 13bffd2 | 2011-01-30 00:12:31 -0800 | [diff] [blame] | 492 | /* 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. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 496 | { |
Timothy B. Terriberry | 13bffd2 | 2011-01-30 00:12:31 -0800 | [diff] [blame] | 497 | int extra_fine; |
| 498 | int extra_bits; |
Gregory Maxwell | 75d2780 | 2011-08-30 14:02:41 -0400 | [diff] [blame] | 499 | extra_fine = IMIN(excess>>(stereo+BITRES),MAX_FINE_BITS-ebits[j]); |
Timothy B. Terriberry | 13bffd2 | 2011-01-30 00:12:31 -0800 | [diff] [blame] | 500 | 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. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 504 | } |
Timothy B. Terriberry | 13bffd2 | 2011-01-30 00:12:31 -0800 | [diff] [blame] | 505 | balance = excess; |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 506 | |
Jean-Marc Valin | 6cbfbc3 | 2010-12-14 11:53:39 -0500 | [diff] [blame] | 507 | celt_assert(bits[j] >= 0); |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 508 | celt_assert(ebits[j] >= 0); |
Jean-Marc Valin | 9a6c496 | 2009-02-09 00:45:48 -0500 | [diff] [blame] | 509 | } |
Timothy B. Terriberry | 948d27c | 2011-01-31 12:28:12 -0800 | [diff] [blame] | 510 | /* Save any remaining bits over the cap for the rebalancing in |
| 511 | quant_all_bands(). */ |
| 512 | *_balance = balance; |
Timothy B. Terriberry | 13bffd2 | 2011-01-30 00:12:31 -0800 | [diff] [blame] | 513 | |
Timothy B. Terriberry | 7627b9f | 2010-12-15 08:22:14 -0800 | [diff] [blame] | 514 | /* The skipped bands use all their bits for fine energy. */ |
| 515 | for (;j<end;j++) |
| 516 | { |
Gregory Maxwell | 9b98aaa | 2010-12-19 02:50:12 -0500 | [diff] [blame] | 517 | ebits[j] = bits[j] >> stereo >> BITRES; |
Timothy B. Terriberry | 7627b9f | 2010-12-15 08:22:14 -0800 | [diff] [blame] | 518 | celt_assert(C*ebits[j]<<BITRES == bits[j]); |
| 519 | bits[j] = 0; |
Jean-Marc Valin | df6620e | 2010-12-16 13:07:29 -0500 | [diff] [blame] | 520 | fine_priority[j] = ebits[j]<1; |
Timothy B. Terriberry | 7627b9f | 2010-12-15 08:22:14 -0800 | [diff] [blame] | 521 | } |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 522 | RESTORE_STACK; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 523 | return codedBands; |
Jean-Marc Valin | c6b4390 | 2008-01-16 22:04:17 +1100 | [diff] [blame] | 524 | } |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 525 | |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 526 | int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, const int *cap, int alloc_trim, int *intensity, int *dual_stereo, |
Ralph Giles | 120800f | 2011-11-25 13:02:00 -0800 | [diff] [blame] | 527 | 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 Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 528 | { |
Jean-Marc Valin | 54aab42 | 2008-12-06 07:52:48 -0500 | [diff] [blame] | 529 | int lo, hi, len, j; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 530 | int codedBands; |
Gregory Maxwell | 8e44767 | 2010-12-16 14:22:51 -0500 | [diff] [blame] | 531 | int skip_start; |
Timothy B. Terriberry | 76ea41e | 2010-12-16 14:39:58 -0800 | [diff] [blame] | 532 | int skip_rsv; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 533 | int intensity_rsv; |
| 534 | int dual_stereo_rsv; |
Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 535 | VARDECL(int, bits1); |
| 536 | VARDECL(int, bits2); |
Jean-Marc Valin | c40addc | 2010-10-22 14:57:07 -0400 | [diff] [blame] | 537 | VARDECL(int, thresh); |
| 538 | VARDECL(int, trim_offset); |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 539 | SAVE_STACK; |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame] | 540 | |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 541 | total = IMAX(total, 0); |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 542 | len = m->nbEBands; |
Gregory Maxwell | 8e44767 | 2010-12-16 14:22:51 -0500 | [diff] [blame] | 543 | skip_start = start; |
Timothy B. Terriberry | 76ea41e | 2010-12-16 14:39:58 -0800 | [diff] [blame] | 544 | /* 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. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 547 | /* 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 Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 561 | ALLOC(bits1, len, int); |
| 562 | ALLOC(bits2, len, int); |
Jean-Marc Valin | c40addc | 2010-10-22 14:57:07 -0400 | [diff] [blame] | 563 | ALLOC(thresh, len, int); |
| 564 | ALLOC(trim_offset, len, int); |
| 565 | |
| 566 | for (j=start;j<end;j++) |
Jean-Marc Valin | 3fed34a | 2010-12-17 14:17:27 -0500 | [diff] [blame] | 567 | { |
| 568 | /* Below this threshold, we're sure not to allocate any PVQ bits */ |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 569 | thresh[j] = IMAX((C)<<BITRES, (3*(m->eBands[j+1]-m->eBands[j])<<LM<<BITRES)>>4); |
Jean-Marc Valin | 3fed34a | 2010-12-17 14:17:27 -0500 | [diff] [blame] | 570 | /* Tilt of the allocation curve */ |
Jean-Marc Valin | 079b916 | 2011-02-14 13:54:59 -0500 | [diff] [blame] | 571 | trim_offset[j] = C*(m->eBands[j+1]-m->eBands[j])*(alloc_trim-5-LM)*(end-j-1) |
Jean-Marc Valin | 803ec8c | 2011-09-23 23:48:40 -0400 | [diff] [blame] | 572 | *(1<<(LM+BITRES))>>6; |
Jean-Marc Valin | 3fed34a | 2010-12-17 14:17:27 -0500 | [diff] [blame] | 573 | /* 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. Terriberry | 428a77d | 2010-12-16 16:50:16 -0800 | [diff] [blame] | 578 | lo = 1; |
Timothy B. Terriberry | ce6d090 | 2011-02-01 17:41:12 -0800 | [diff] [blame] | 579 | hi = m->nbAllocVectors - 1; |
Timothy B. Terriberry | 428a77d | 2010-12-16 16:50:16 -0800 | [diff] [blame] | 580 | do |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 581 | { |
Gregory Maxwell | 7c673cf | 2010-12-19 02:26:56 -0500 | [diff] [blame] | 582 | int done = 0; |
Jean-Marc Valin | 825ead8 | 2008-09-09 00:15:40 -0400 | [diff] [blame] | 583 | int psum = 0; |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 584 | int mid = (lo+hi) >> 1; |
Gregory Maxwell | 7c673cf | 2010-12-19 02:26:56 -0500 | [diff] [blame] | 585 | for (j=end;j-->start;) |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 586 | { |
Jean-Marc Valin | 86da2c8 | 2011-03-11 10:36:11 -0500 | [diff] [blame] | 587 | int bitsj; |
Jean-Marc Valin | bb8fa1f | 2010-06-03 00:33:42 -0400 | [diff] [blame] | 588 | int N = m->eBands[j+1]-m->eBands[j]; |
Jean-Marc Valin | 86da2c8 | 2011-03-11 10:36:11 -0500 | [diff] [blame] | 589 | 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 Maxwell | 7c673cf | 2010-12-19 02:26:56 -0500 | [diff] [blame] | 594 | { |
| 595 | done = 1; |
| 596 | /* Don't allocate more than we can actually use */ |
Jean-Marc Valin | 86da2c8 | 2011-03-11 10:36:11 -0500 | [diff] [blame] | 597 | psum += IMIN(bitsj, cap[j]); |
Gregory Maxwell | 7c673cf | 2010-12-19 02:26:56 -0500 | [diff] [blame] | 598 | } else { |
Jean-Marc Valin | 86da2c8 | 2011-03-11 10:36:11 -0500 | [diff] [blame] | 599 | if (bitsj >= C<<BITRES) |
Gregory Maxwell | 7c673cf | 2010-12-19 02:26:56 -0500 | [diff] [blame] | 600 | psum += C<<BITRES; |
| 601 | } |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 602 | } |
Timothy B. Terriberry | 4777f06 | 2010-12-15 06:56:00 -0800 | [diff] [blame] | 603 | if (psum > total) |
Timothy B. Terriberry | 428a77d | 2010-12-16 16:50:16 -0800 | [diff] [blame] | 604 | hi = mid - 1; |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 605 | else |
Timothy B. Terriberry | 428a77d | 2010-12-16 16:50:16 -0800 | [diff] [blame] | 606 | lo = mid + 1; |
Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 607 | /*printf ("lo = %d, hi = %d\n", lo, hi);*/ |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 608 | } |
Timothy B. Terriberry | 428a77d | 2010-12-16 16:50:16 -0800 | [diff] [blame] | 609 | while (lo <= hi); |
| 610 | hi = lo--; |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 611 | /*printf ("interp between %d and %d\n", lo, hi);*/ |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 612 | for (j=start;j<end;j++) |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 613 | { |
Jean-Marc Valin | 86da2c8 | 2011-03-11 10:36:11 -0500 | [diff] [blame] | 614 | int bits1j, bits2j; |
Jean-Marc Valin | bb8fa1f | 2010-06-03 00:33:42 -0400 | [diff] [blame] | 615 | int N = m->eBands[j+1]-m->eBands[j]; |
Jean-Marc Valin | 86da2c8 | 2011-03-11 10:36:11 -0500 | [diff] [blame] | 616 | bits1j = C*N*m->allocVectors[lo*len+j]<<LM>>2; |
| 617 | bits2j = hi>=m->nbAllocVectors ? |
Jean-Marc Valin | ec6588a | 2011-02-03 10:34:30 -0500 | [diff] [blame] | 618 | cap[j] : C*N*m->allocVectors[hi*len+j]<<LM>>2; |
Jean-Marc Valin | 86da2c8 | 2011-03-11 10:36:11 -0500 | [diff] [blame] | 619 | 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. Terriberry | 428a77d | 2010-12-16 16:50:16 -0800 | [diff] [blame] | 623 | if (lo > 0) |
Jean-Marc Valin | 86da2c8 | 2011-03-11 10:36:11 -0500 | [diff] [blame] | 624 | bits1j += offsets[j]; |
| 625 | bits2j += offsets[j]; |
Gregory Maxwell | 8e44767 | 2010-12-16 14:22:51 -0500 | [diff] [blame] | 626 | if (offsets[j]>0) |
| 627 | skip_start = j; |
Jean-Marc Valin | 86da2c8 | 2011-03-11 10:36:11 -0500 | [diff] [blame] | 628 | bits2j = IMAX(0,bits2j-bits1j); |
| 629 | bits1[j] = bits1j; |
| 630 | bits2[j] = bits2j; |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 631 | } |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 632 | codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thresh, cap, |
Timothy B. Terriberry | 948d27c | 2011-01-31 12:28:12 -0800 | [diff] [blame] | 633 | total, balance, skip_rsv, intensity, intensity_rsv, dual_stereo, dual_stereo_rsv, |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame] | 634 | pulses, ebits, fine_priority, C, LM, ec, encode, prev); |
Jean-Marc Valin | 825ead8 | 2008-09-09 00:15:40 -0400 | [diff] [blame] | 635 | RESTORE_STACK; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 636 | return codedBands; |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 637 | } |
Jean-Marc Valin | b86ed07 | 2008-01-15 16:33:21 +1100 | [diff] [blame] | 638 | |