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: |
| 8 | |
| 9 | - Redistributions of source code must retain the above copyright |
| 10 | notice, this list of conditions and the following disclaimer. |
| 11 | |
| 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. |
| 15 | |
| 16 | - Neither the name of the Xiph.org Foundation nor the names of its |
| 17 | contributors may be used to endorse or promote products derived from |
| 18 | this software without specific prior written permission. |
| 19 | |
| 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 24 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
Jean-Marc Valin | 02fa913 | 2008-02-20 12:09:29 +1100 | [diff] [blame] | 33 | #ifdef HAVE_CONFIG_H |
| 34 | #include "config.h" |
| 35 | #endif |
| 36 | |
Jean-Marc Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 37 | #include <math.h> |
| 38 | #include "modes.h" |
| 39 | #include "cwrs.h" |
| 40 | #include "arch.h" |
Jean-Marc Valin | a663174 | 2008-01-16 17:16:04 +1100 | [diff] [blame] | 41 | #include "os_support.h" |
Jean-Marc Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 42 | |
| 43 | #include "entcode.h" |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 44 | #include "rate.h" |
Jean-Marc Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 45 | |
Jean-Marc Valin | a663174 | 2008-01-16 17:16:04 +1100 | [diff] [blame] | 46 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 47 | static const unsigned char LOG2_FRAC_TABLE[24]={ |
| 48 | 0, |
| 49 | 8,13, |
| 50 | 16,19,21,23, |
| 51 | 24,26,27,28,29,30,31,32, |
| 52 | 32,33,34,34,35,36,36,37,37 |
| 53 | }; |
| 54 | |
Jean-Marc Valin | b76ee70 | 2008-03-10 15:42:35 +1100 | [diff] [blame] | 55 | #ifndef STATIC_MODES |
Jean-Marc Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 56 | |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 57 | /*Determines if V(N,K) fits in a 32-bit unsigned integer. |
| 58 | N and K are themselves limited to 15 bits.*/ |
| 59 | static int fits_in32(int _n, int _k) |
Jean-Marc Valin | a663174 | 2008-01-16 17:16:04 +1100 | [diff] [blame] | 60 | { |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 61 | static const celt_int16 maxN[15] = { |
| 62 | 32767, 32767, 32767, 1476, 283, 109, 60, 40, |
| 63 | 29, 24, 20, 18, 16, 14, 13}; |
| 64 | static const celt_int16 maxK[15] = { |
| 65 | 32767, 32767, 32767, 32767, 1172, 238, 95, 53, |
| 66 | 36, 27, 22, 18, 16, 15, 13}; |
| 67 | if (_n>=14) |
Jean-Marc Valin | a663174 | 2008-01-16 17:16:04 +1100 | [diff] [blame] | 68 | { |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 69 | if (_k>=14) |
| 70 | return 0; |
Jean-Marc Valin | cae30df | 2010-05-21 00:26:03 -0400 | [diff] [blame] | 71 | else |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 72 | return _n <= maxN[_k]; |
| 73 | } else { |
| 74 | return _k <= maxK[_n]; |
Jean-Marc Valin | a663174 | 2008-01-16 17:16:04 +1100 | [diff] [blame] | 75 | } |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void compute_pulse_cache(CELTMode *m, int LM) |
| 79 | { |
| 80 | int i; |
| 81 | int curr=0; |
| 82 | int nbEntries=0; |
| 83 | int entryN[100], entryK[100], entryI[100]; |
| 84 | const celt_int16 *eBands = m->eBands; |
| 85 | PulseCache *cache = &m->cache; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 86 | celt_int16 *cindex; |
| 87 | unsigned char *bits; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 88 | |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 89 | cindex = celt_alloc(sizeof(cache->index[0])*m->nbEBands*(LM+2)); |
| 90 | cache->index = cindex; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 91 | |
Jean-Marc Valin | 7fff572 | 2010-10-18 16:20:00 -0400 | [diff] [blame] | 92 | /* Scan for all unique band sizes */ |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 93 | for (i=0;i<=LM+1;i++) |
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 j; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 96 | for (j=0;j<m->nbEBands;j++) |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 97 | { |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 98 | int k; |
| 99 | int N = (eBands[j+1]-eBands[j])<<i>>1; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 100 | cindex[i*m->nbEBands+j] = -1; |
Jean-Marc Valin | 7fff572 | 2010-10-18 16:20:00 -0400 | [diff] [blame] | 101 | /* Find other bands that have the same size */ |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 102 | for (k=0;k<=i;k++) |
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 | int n; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 105 | for (n=0;n<m->nbEBands && (k!=i || n<j);n++) |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 106 | { |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 107 | if (N == (eBands[n+1]-eBands[n])<<k>>1) |
| 108 | { |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 109 | cindex[i*m->nbEBands+j] = cindex[k*m->nbEBands+n]; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 110 | break; |
| 111 | } |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 112 | } |
| 113 | } |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 114 | if (cache->index[i*m->nbEBands+j] == -1 && N!=0) |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 115 | { |
| 116 | int K; |
| 117 | entryN[nbEntries] = N; |
| 118 | K = 0; |
Jean-Marc Valin | 7fff572 | 2010-10-18 16:20:00 -0400 | [diff] [blame] | 119 | while (fits_in32(N,get_pulses(K+1)) && K<MAX_PSEUDO) |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 120 | K++; |
| 121 | entryK[nbEntries] = K; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 122 | cindex[i*m->nbEBands+j] = curr; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 123 | entryI[nbEntries] = curr; |
| 124 | |
| 125 | curr += K+1; |
| 126 | nbEntries++; |
| 127 | } |
| 128 | } |
Gregory Maxwell | dc67fa9 | 2009-06-04 17:17:35 -0400 | [diff] [blame] | 129 | } |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 130 | bits = celt_alloc(sizeof(unsigned char)*curr); |
| 131 | cache->bits = bits; |
| 132 | cache->size = curr; |
Jean-Marc Valin | 7fff572 | 2010-10-18 16:20:00 -0400 | [diff] [blame] | 133 | /* Compute the cache for all unique sizes */ |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 134 | for (i=0;i<nbEntries;i++) |
| 135 | { |
| 136 | int j; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 137 | unsigned char *ptr = bits+entryI[i]; |
Jean-Marc Valin | 7fff572 | 2010-10-18 16:20:00 -0400 | [diff] [blame] | 138 | celt_int16 tmp[MAX_PULSES+1]; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 139 | get_required_bits(tmp, entryN[i], get_pulses(entryK[i]), BITRES); |
| 140 | for (j=1;j<=entryK[i];j++) |
| 141 | ptr[j] = tmp[get_pulses(j)]-1; |
| 142 | ptr[0] = entryK[i]; |
| 143 | } |
Jean-Marc Valin | a663174 | 2008-01-16 17:16:04 +1100 | [diff] [blame] | 144 | } |
| 145 | |
Jean-Marc Valin | b76ee70 | 2008-03-10 15:42:35 +1100 | [diff] [blame] | 146 | #endif /* !STATIC_MODES */ |
Jean-Marc Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 147 | |
Jean-Marc Valin | 7b0cb4b | 2008-09-11 00:20:08 -0400 | [diff] [blame] | 148 | |
Jean-Marc Valin | ae319fe | 2010-09-13 09:46:54 -0400 | [diff] [blame] | 149 | #define ALLOC_STEPS 6 |
Jean-Marc Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 150 | |
Gregory Maxwell | 8e44767 | 2010-12-16 14:22:51 -0500 | [diff] [blame] | 151 | static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int skip_start, |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 152 | const int *bits1, const int *bits2, const int *thresh, int total, int skip_rsv, |
| 153 | int *intensity, int intensity_rsv, int *dual_stereo, int dual_stereo_rsv, int *bits, |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame^] | 154 | int *ebits, int *fine_priority, int _C, int LM, void *ec, int encode, int prev) |
Jean-Marc Valin | c6b4390 | 2008-01-16 22:04:17 +1100 | [diff] [blame] | 155 | { |
Jean-Marc Valin | 9a6c496 | 2009-02-09 00:45:48 -0500 | [diff] [blame] | 156 | int psum; |
Jean-Marc Valin | ebf72da | 2008-09-09 23:21:36 -0400 | [diff] [blame] | 157 | int lo, hi; |
Jean-Marc Valin | a6a53ab | 2010-08-31 16:46:40 -0400 | [diff] [blame] | 158 | int i, j; |
Jean-Marc Valin | aead79b | 2010-05-11 07:34:24 -0400 | [diff] [blame] | 159 | int logM; |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 160 | const int C = CHANNELS(_C); |
Gregory Maxwell | 9b98aaa | 2010-12-19 02:50:12 -0500 | [diff] [blame] | 161 | int stereo; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 162 | int codedBands=-1; |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 163 | int alloc_floor; |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 164 | int left, percoeff; |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 165 | int done; |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 166 | int balance; |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 167 | SAVE_STACK; |
Jean-Marc Valin | aead79b | 2010-05-11 07:34:24 -0400 | [diff] [blame] | 168 | |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 169 | alloc_floor = C<<BITRES; |
Gregory Maxwell | 9b98aaa | 2010-12-19 02:50:12 -0500 | [diff] [blame] | 170 | stereo = C>1; |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 171 | |
Jean-Marc Valin | 5c0c936 | 2010-08-31 10:11:11 -0400 | [diff] [blame] | 172 | logM = LM<<BITRES; |
Jean-Marc Valin | c6b4390 | 2008-01-16 22:04:17 +1100 | [diff] [blame] | 173 | lo = 0; |
Jean-Marc Valin | a6a53ab | 2010-08-31 16:46:40 -0400 | [diff] [blame] | 174 | hi = 1<<ALLOC_STEPS; |
| 175 | for (i=0;i<ALLOC_STEPS;i++) |
Jean-Marc Valin | c6b4390 | 2008-01-16 22:04:17 +1100 | [diff] [blame] | 176 | { |
| 177 | int mid = (lo+hi)>>1; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 178 | psum = 0; |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 179 | done = 0; |
Timothy B. Terriberry | 405e6a9 | 2010-12-15 20:46:09 -0800 | [diff] [blame] | 180 | for (j=end;j-->start;) |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 181 | { |
| 182 | int tmp = bits1[j] + (mid*bits2[j]>>ALLOC_STEPS); |
Timothy B. Terriberry | 405e6a9 | 2010-12-15 20:46:09 -0800 | [diff] [blame] | 183 | if (tmp >= thresh[j] || done) |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 184 | { |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 185 | done = 1; |
Timothy B. Terriberry | 405e6a9 | 2010-12-15 20:46:09 -0800 | [diff] [blame] | 186 | /* Don't allocate more than we can actually use */ |
| 187 | psum += IMIN(tmp, 64*C<<BITRES<<LM); |
| 188 | } else { |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 189 | if (tmp >= alloc_floor) |
| 190 | psum += alloc_floor; |
| 191 | } |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 192 | } |
Timothy B. Terriberry | 4777f06 | 2010-12-15 06:56:00 -0800 | [diff] [blame] | 193 | if (psum > total) |
Jean-Marc Valin | c6b4390 | 2008-01-16 22:04:17 +1100 | [diff] [blame] | 194 | hi = mid; |
| 195 | else |
| 196 | lo = mid; |
| 197 | } |
Jean-Marc Valin | 825ead8 | 2008-09-09 00:15:40 -0400 | [diff] [blame] | 198 | psum = 0; |
Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 199 | /*printf ("interp bisection gave %d\n", lo);*/ |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 200 | done = 0; |
Timothy B. Terriberry | 405e6a9 | 2010-12-15 20:46:09 -0800 | [diff] [blame] | 201 | for (j=end;j-->start;) |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 202 | { |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 203 | int tmp = bits1[j] + (lo*bits2[j]>>ALLOC_STEPS); |
Timothy B. Terriberry | 405e6a9 | 2010-12-15 20:46:09 -0800 | [diff] [blame] | 204 | if (tmp < thresh[j] && !done) |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 205 | { |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 206 | if (tmp >= alloc_floor) |
| 207 | tmp = alloc_floor; |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 208 | else |
| 209 | tmp = 0; |
Timothy B. Terriberry | 405e6a9 | 2010-12-15 20:46:09 -0800 | [diff] [blame] | 210 | } else |
| 211 | done = 1; |
Jean-Marc Valin | 52dc66b | 2010-12-06 21:31:15 -0500 | [diff] [blame] | 212 | /* Don't allocate more than we can actually use */ |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 213 | tmp = IMIN(tmp, 64*C<<BITRES<<LM); |
| 214 | bits[j] = tmp; |
| 215 | psum += tmp; |
Jean-Marc Valin | 825ead8 | 2008-09-09 00:15:40 -0400 | [diff] [blame] | 216 | } |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 217 | |
Timothy B. Terriberry | 7627b9f | 2010-12-15 08:22:14 -0800 | [diff] [blame] | 218 | /* Decide which bands to skip, working backwards from the end. */ |
| 219 | for (codedBands=end;;codedBands--) |
Jean-Marc Valin | dfd6e71 | 2010-12-09 23:23:34 -0500 | [diff] [blame] | 220 | { |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 221 | int band_width; |
| 222 | int band_bits; |
| 223 | int rem; |
Timothy B. Terriberry | 7627b9f | 2010-12-15 08:22:14 -0800 | [diff] [blame] | 224 | j = codedBands-1; |
Timothy B. Terriberry | 76ea41e | 2010-12-16 14:39:58 -0800 | [diff] [blame] | 225 | /* Never skip the first band, nor a band that has been boosted by |
| 226 | dynalloc. |
| 227 | In the first case, we'd be coding a bit to signal we're going to waste |
| 228 | all the other bits. |
| 229 | In the second case, we'd be coding a bit to redistribute all the bits |
| 230 | we just signaled should be cocentrated in this band. */ |
Gregory Maxwell | 8e44767 | 2010-12-16 14:22:51 -0500 | [diff] [blame] | 231 | if (j<=skip_start) |
Timothy B. Terriberry | 76ea41e | 2010-12-16 14:39:58 -0800 | [diff] [blame] | 232 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 233 | /* Give the bit we reserved to end skipping back. */ |
| 234 | total += skip_rsv; |
Timothy B. Terriberry | 7627b9f | 2010-12-15 08:22:14 -0800 | [diff] [blame] | 235 | break; |
Timothy B. Terriberry | 76ea41e | 2010-12-16 14:39:58 -0800 | [diff] [blame] | 236 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 237 | /*Figure out how many left-over bits we would be adding to this band. |
| 238 | This can include bits we've stolen back from higher, skipped bands.*/ |
| 239 | left = total-psum; |
| 240 | percoeff = left/(m->eBands[codedBands]-m->eBands[start]); |
| 241 | left -= (m->eBands[codedBands]-m->eBands[start])*percoeff; |
Timothy B. Terriberry | 4777f06 | 2010-12-15 06:56:00 -0800 | [diff] [blame] | 242 | rem = IMAX(left-(m->eBands[j]-m->eBands[start]),0); |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 243 | band_width = m->eBands[codedBands]-m->eBands[j]; |
| 244 | band_bits = bits[j] + percoeff*band_width + rem; |
Timothy B. Terriberry | 283a9b6 | 2010-12-15 05:35:54 -0800 | [diff] [blame] | 245 | /*Only code a skip decision if we're above the threshold for this band. |
| 246 | Otherwise it is force-skipped. |
Timothy B. Terriberry | 405e6a9 | 2010-12-15 20:46:09 -0800 | [diff] [blame] | 247 | This ensures that we have enough bits to code the skip flag.*/ |
| 248 | if (band_bits >= IMAX(thresh[j], alloc_floor+(1<<BITRES))) |
Jean-Marc Valin | dfd6e71 | 2010-12-09 23:23:34 -0500 | [diff] [blame] | 249 | { |
Timothy B. Terriberry | b2f5900 | 2010-12-15 05:12:43 -0800 | [diff] [blame] | 250 | if (encode) |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 251 | { |
Timothy B. Terriberry | 7cbf168 | 2010-12-14 21:55:49 -0800 | [diff] [blame] | 252 | /*This if() block is the only part of the allocation function that |
| 253 | is not a mandatory part of the bitstream: any bands we choose to |
| 254 | skip here must be explicitly signaled.*/ |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 255 | /*Choose a threshold with some hysteresis to keep bands from |
| 256 | fluctuating in and out.*/ |
| 257 | if (band_bits > ((j<prev?7:9)*band_width<<LM<<BITRES)>>4) |
Timothy B. Terriberry | b2f5900 | 2010-12-15 05:12:43 -0800 | [diff] [blame] | 258 | { |
Timothy B. Terriberry | e86fb26 | 2010-12-17 14:50:19 -0800 | [diff] [blame] | 259 | ec_enc_bit_logp((ec_enc *)ec, 1, 1); |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 260 | break; |
Timothy B. Terriberry | b2f5900 | 2010-12-15 05:12:43 -0800 | [diff] [blame] | 261 | } |
Timothy B. Terriberry | e86fb26 | 2010-12-17 14:50:19 -0800 | [diff] [blame] | 262 | ec_enc_bit_logp((ec_enc *)ec, 0, 1); |
| 263 | } else if (ec_dec_bit_logp((ec_dec *)ec, 1)) { |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 264 | break; |
Timothy B. Terriberry | b2f5900 | 2010-12-15 05:12:43 -0800 | [diff] [blame] | 265 | } |
| 266 | /*We used a bit to skip this band.*/ |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 267 | psum += 1<<BITRES; |
| 268 | band_bits -= 1<<BITRES; |
Jean-Marc Valin | dfd6e71 | 2010-12-09 23:23:34 -0500 | [diff] [blame] | 269 | } |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 270 | /*Reclaim the bits originally allocated to this band.*/ |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 271 | psum -= bits[j]+intensity_rsv; |
| 272 | if (intensity_rsv > 0) |
| 273 | intensity_rsv = LOG2_FRAC_TABLE[j-start]; |
| 274 | psum += intensity_rsv; |
Timothy B. Terriberry | 7cbf168 | 2010-12-14 21:55:49 -0800 | [diff] [blame] | 275 | if (band_bits >= alloc_floor) |
Jean-Marc Valin | dcacb73 | 2010-12-14 13:39:30 -0500 | [diff] [blame] | 276 | { |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 277 | /*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] | 278 | psum += alloc_floor; |
Timothy B. Terriberry | 7cbf168 | 2010-12-14 21:55:49 -0800 | [diff] [blame] | 279 | bits[j] = alloc_floor; |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 280 | } else { |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 281 | /*Otherwise this band gets nothing at all.*/ |
Timothy B. Terriberry | 7cbf168 | 2010-12-14 21:55:49 -0800 | [diff] [blame] | 282 | bits[j] = 0; |
Jean-Marc Valin | dcacb73 | 2010-12-14 13:39:30 -0500 | [diff] [blame] | 283 | } |
Jean-Marc Valin | dfd6e71 | 2010-12-09 23:23:34 -0500 | [diff] [blame] | 284 | } |
Jean-Marc Valin | ffe1057 | 2010-12-15 00:36:41 -0500 | [diff] [blame] | 285 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 286 | celt_assert(codedBands > start); |
| 287 | /* Code the intensity and dual stereo parameters. */ |
| 288 | if (intensity_rsv > 0) |
| 289 | { |
| 290 | if (encode) |
Jean-Marc Valin | fb03111 | 2010-11-25 16:32:54 -0500 | [diff] [blame] | 291 | { |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 292 | *intensity = IMIN(*intensity, codedBands); |
| 293 | ec_enc_uint((ec_enc *)ec, *intensity-start, codedBands+1-start); |
Jean-Marc Valin | fb03111 | 2010-11-25 16:32:54 -0500 | [diff] [blame] | 294 | } |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 295 | else |
| 296 | *intensity = start+ec_dec_uint((ec_dec *)ec, codedBands+1-start); |
| 297 | } |
| 298 | else |
| 299 | *intensity = 0; |
| 300 | if (*intensity <= start) |
| 301 | { |
| 302 | total += dual_stereo_rsv; |
| 303 | dual_stereo_rsv = 0; |
| 304 | } |
| 305 | if (dual_stereo_rsv > 0) |
| 306 | { |
| 307 | if (encode) |
| 308 | ec_enc_bit_logp((ec_enc *)ec, *dual_stereo, 1); |
| 309 | else |
| 310 | *dual_stereo = ec_dec_bit_logp((ec_dec *)ec, 1); |
| 311 | } |
| 312 | else |
| 313 | *dual_stereo = 0; |
| 314 | |
| 315 | /* Allocate the remaining bits */ |
| 316 | left = total-psum; |
| 317 | percoeff = left/(m->eBands[codedBands]-m->eBands[start]); |
| 318 | left -= (m->eBands[codedBands]-m->eBands[start])*percoeff; |
| 319 | for (j=start;j<codedBands;j++) |
| 320 | bits[j] += percoeff*(m->eBands[j+1]-m->eBands[j]); |
| 321 | for (j=start;j<codedBands;j++) |
| 322 | { |
| 323 | int tmp = IMIN(left, m->eBands[j+1]-m->eBands[j]); |
| 324 | bits[j] += tmp; |
| 325 | left -= tmp; |
Jean-Marc Valin | ebf72da | 2008-09-09 23:21:36 -0400 | [diff] [blame] | 326 | } |
Jean-Marc Valin | 54d84c0 | 2010-11-19 11:45:37 -0500 | [diff] [blame] | 327 | /*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] | 328 | |
| 329 | balance = 0; |
Timothy B. Terriberry | 7627b9f | 2010-12-15 08:22:14 -0800 | [diff] [blame] | 330 | for (j=start;j<codedBands;j++) |
Jean-Marc Valin | 9a6c496 | 2009-02-09 00:45:48 -0500 | [diff] [blame] | 331 | { |
Jean-Marc Valin | 3f9857b | 2010-08-13 21:20:37 -0400 | [diff] [blame] | 332 | int N0, N, den; |
Jean-Marc Valin | 9a6c496 | 2009-02-09 00:45:48 -0500 | [diff] [blame] | 333 | int offset; |
Jean-Marc Valin | 5c0c936 | 2010-08-31 10:11:11 -0400 | [diff] [blame] | 334 | int NClogN; |
| 335 | |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 336 | celt_assert(bits[j] >= 0); |
Jean-Marc Valin | 3603428 | 2010-08-06 14:42:43 -0400 | [diff] [blame] | 337 | N0 = m->eBands[j+1]-m->eBands[j]; |
Jean-Marc Valin | 5c0c936 | 2010-08-31 10:11:11 -0400 | [diff] [blame] | 338 | N=N0<<LM; |
Jean-Marc Valin | 5c0c936 | 2010-08-31 10:11:11 -0400 | [diff] [blame] | 339 | |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 340 | if (N>1) |
Jean-Marc Valin | dcacb73 | 2010-12-14 13:39:30 -0500 | [diff] [blame] | 341 | { |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 342 | NClogN = N*C*(m->logN[j] + logM); |
Jean-Marc Valin | 9a6c496 | 2009-02-09 00:45:48 -0500 | [diff] [blame] | 343 | |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 344 | /* Compensate for the extra DoF in stereo */ |
| 345 | den=(C*N+ ((C==2 && N>2) ? 1 : 0)); |
| 346 | |
| 347 | /* Offset for the number of fine bits by log2(N)/2 + FINE_OFFSET |
| 348 | compared to their "fair share" of total/N */ |
| 349 | offset = (NClogN>>1)-N*C*FINE_OFFSET; |
| 350 | |
| 351 | /* N=2 is the only point that doesn't match the curve */ |
| 352 | if (N==2) |
| 353 | offset += N*C<<BITRES>>2; |
| 354 | |
| 355 | /* Changing the offset for allocating the second and third |
| 356 | fine energy bit */ |
| 357 | if (bits[j] + offset < den*2<<BITRES) |
| 358 | offset += NClogN>>2; |
| 359 | else if (bits[j] + offset < den*3<<BITRES) |
| 360 | offset += NClogN>>3; |
| 361 | |
| 362 | /* Divide with rounding */ |
| 363 | ebits[j] = IMAX(0, (bits[j] + offset + (den<<(BITRES-1))) / (den<<BITRES)); |
| 364 | |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 365 | /* Make sure not to bust */ |
| 366 | if (C*ebits[j] > (bits[j]>>BITRES)) |
Gregory Maxwell | 9b98aaa | 2010-12-19 02:50:12 -0500 | [diff] [blame] | 367 | ebits[j] = bits[j] >> stereo >> BITRES; |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 368 | |
Jean-Marc Valin | a66b757 | 2011-01-10 13:21:04 -0500 | [diff] [blame] | 369 | /* More than that is useless because that's about as far as PVQ can go */ |
| 370 | ebits[j] = IMIN(ebits[j], MAX_FINE_BITS); |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 371 | |
Timothy B. Terriberry | 949f180 | 2010-12-30 08:38:48 -0800 | [diff] [blame] | 372 | /* If we rounded down or capped this band, make it a candidate for the |
| 373 | final fine energy pass */ |
| 374 | fine_priority[j] = ebits[j]*(den<<BITRES) >= bits[j]+offset; |
| 375 | |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 376 | } else { |
| 377 | /* For N=1, all bits go to fine energy except for a single sign bit */ |
Jean-Marc Valin | a66b757 | 2011-01-10 13:21:04 -0500 | [diff] [blame] | 378 | ebits[j] = IMIN(IMAX(0,(bits[j] >> stereo >> BITRES)-1),MAX_FINE_BITS); |
Timothy B. Terriberry | 66c5ab4 | 2010-12-16 08:39:37 -0800 | [diff] [blame] | 379 | fine_priority[j] = (ebits[j]+1)*C<<BITRES >= (bits[j]-balance); |
| 380 | /* N=1 bands can't take advantage of the re-balancing in |
| 381 | quant_all_bands() because they don't have shape, only fine energy. |
| 382 | Instead, do the re-balancing here.*/ |
| 383 | balance = IMAX(0,bits[j] - ((ebits[j]+1)*C<<BITRES)); |
| 384 | if (j+1<codedBands) |
| 385 | { |
| 386 | bits[j] -= balance; |
| 387 | bits[j+1] += balance; |
| 388 | } |
| 389 | } |
Jean-Marc Valin | 3f9857b | 2010-08-13 21:20:37 -0400 | [diff] [blame] | 390 | |
Jean-Marc Valin | 5c0c936 | 2010-08-31 10:11:11 -0400 | [diff] [blame] | 391 | /* The other bits are assigned to PVQ */ |
Jean-Marc Valin | 9a6c496 | 2009-02-09 00:45:48 -0500 | [diff] [blame] | 392 | bits[j] -= C*ebits[j]<<BITRES; |
Jean-Marc Valin | 6cbfbc3 | 2010-12-14 11:53:39 -0500 | [diff] [blame] | 393 | celt_assert(bits[j] >= 0); |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 394 | celt_assert(ebits[j] >= 0); |
Jean-Marc Valin | 9a6c496 | 2009-02-09 00:45:48 -0500 | [diff] [blame] | 395 | } |
Timothy B. Terriberry | 7627b9f | 2010-12-15 08:22:14 -0800 | [diff] [blame] | 396 | /* The skipped bands use all their bits for fine energy. */ |
| 397 | for (;j<end;j++) |
| 398 | { |
Gregory Maxwell | 9b98aaa | 2010-12-19 02:50:12 -0500 | [diff] [blame] | 399 | ebits[j] = bits[j] >> stereo >> BITRES; |
Timothy B. Terriberry | 7627b9f | 2010-12-15 08:22:14 -0800 | [diff] [blame] | 400 | celt_assert(C*ebits[j]<<BITRES == bits[j]); |
| 401 | bits[j] = 0; |
Jean-Marc Valin | df6620e | 2010-12-16 13:07:29 -0500 | [diff] [blame] | 402 | fine_priority[j] = ebits[j]<1; |
Timothy B. Terriberry | 7627b9f | 2010-12-15 08:22:14 -0800 | [diff] [blame] | 403 | } |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 404 | RESTORE_STACK; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 405 | return codedBands; |
Jean-Marc Valin | c6b4390 | 2008-01-16 22:04:17 +1100 | [diff] [blame] | 406 | } |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 407 | |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 408 | int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, int alloc_trim, int *intensity, int *dual_stereo, |
Timothy B. Terriberry | b2f5900 | 2010-12-15 05:12:43 -0800 | [diff] [blame] | 409 | int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM, void *ec, int encode, int prev) |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 410 | { |
Jean-Marc Valin | 54aab42 | 2008-12-06 07:52:48 -0500 | [diff] [blame] | 411 | int lo, hi, len, j; |
Jean-Marc Valin | bf2398b | 2009-10-15 07:28:19 -0400 | [diff] [blame] | 412 | const int C = CHANNELS(_C); |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 413 | int codedBands; |
Gregory Maxwell | 8e44767 | 2010-12-16 14:22:51 -0500 | [diff] [blame] | 414 | int skip_start; |
Timothy B. Terriberry | 76ea41e | 2010-12-16 14:39:58 -0800 | [diff] [blame] | 415 | int skip_rsv; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 416 | int intensity_rsv; |
| 417 | int dual_stereo_rsv; |
Jean-Marc Valin | 31b79d1 | 2008-03-12 17:17:23 +1100 | [diff] [blame] | 418 | VARDECL(int, bits1); |
| 419 | VARDECL(int, bits2); |
Jean-Marc Valin | c40addc | 2010-10-22 14:57:07 -0400 | [diff] [blame] | 420 | VARDECL(int, thresh); |
| 421 | VARDECL(int, trim_offset); |
Jean-Marc Valin | 8600f69 | 2008-02-29 15:14:12 +1100 | [diff] [blame] | 422 | SAVE_STACK; |
Jean-Marc Valin | 0bb05bc | 2008-02-20 13:43:40 +1100 | [diff] [blame] | 423 | |
Jean-Marc Valin | 9651ffd | 2010-12-14 16:41:03 -0500 | [diff] [blame] | 424 | total = IMAX(total, 0); |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 425 | len = m->nbEBands; |
Gregory Maxwell | 8e44767 | 2010-12-16 14:22:51 -0500 | [diff] [blame] | 426 | skip_start = start; |
Timothy B. Terriberry | 76ea41e | 2010-12-16 14:39:58 -0800 | [diff] [blame] | 427 | /* Reserve a bit to signal the end of manually skipped bands. */ |
| 428 | skip_rsv = total >= 1<<BITRES ? 1<<BITRES : 0; |
| 429 | total -= skip_rsv; |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 430 | /* Reserve bits for the intensity and dual stereo parameters. */ |
| 431 | intensity_rsv = dual_stereo_rsv = 0; |
| 432 | if (C==2) |
| 433 | { |
| 434 | intensity_rsv = LOG2_FRAC_TABLE[end-start]; |
| 435 | if (intensity_rsv>total) |
| 436 | intensity_rsv = 0; |
| 437 | else |
| 438 | { |
| 439 | total -= intensity_rsv; |
| 440 | dual_stereo_rsv = total>=1<<BITRES ? 1<<BITRES : 0; |
| 441 | total -= dual_stereo_rsv; |
| 442 | } |
| 443 | } |
Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 444 | ALLOC(bits1, len, int); |
| 445 | ALLOC(bits2, len, int); |
Jean-Marc Valin | c40addc | 2010-10-22 14:57:07 -0400 | [diff] [blame] | 446 | ALLOC(thresh, len, int); |
| 447 | ALLOC(trim_offset, len, int); |
| 448 | |
| 449 | for (j=start;j<end;j++) |
Jean-Marc Valin | 3fed34a | 2010-12-17 14:17:27 -0500 | [diff] [blame] | 450 | { |
| 451 | /* 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] | 452 | 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] | 453 | /* Tilt of the allocation curve */ |
Jean-Marc Valin | bad42a7 | 2010-12-01 14:54:10 -0500 | [diff] [blame] | 454 | trim_offset[j] = C*(m->eBands[j+1]-m->eBands[j])*(alloc_trim-5-LM)*(m->nbEBands-j-1) |
Jean-Marc Valin | 35095c6 | 2010-11-04 13:24:44 -0400 | [diff] [blame] | 455 | <<(LM+BITRES)>>6; |
Jean-Marc Valin | 3fed34a | 2010-12-17 14:17:27 -0500 | [diff] [blame] | 456 | /* Giving less resolution to single-coefficient bands because they get |
| 457 | more benefit from having one coarse value per coefficient*/ |
| 458 | if ((m->eBands[j+1]-m->eBands[j])<<LM==1) |
| 459 | trim_offset[j] -= C<<BITRES; |
| 460 | } |
Timothy B. Terriberry | 428a77d | 2010-12-16 16:50:16 -0800 | [diff] [blame] | 461 | lo = 1; |
| 462 | hi = m->nbAllocVectors - 2; |
| 463 | do |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 464 | { |
Gregory Maxwell | 7c673cf | 2010-12-19 02:26:56 -0500 | [diff] [blame] | 465 | int done = 0; |
Jean-Marc Valin | 825ead8 | 2008-09-09 00:15:40 -0400 | [diff] [blame] | 466 | int psum = 0; |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 467 | int mid = (lo+hi) >> 1; |
Gregory Maxwell | 7c673cf | 2010-12-19 02:26:56 -0500 | [diff] [blame] | 468 | for (j=end;j-->start;) |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 469 | { |
Jean-Marc Valin | bb8fa1f | 2010-06-03 00:33:42 -0400 | [diff] [blame] | 470 | int N = m->eBands[j+1]-m->eBands[j]; |
Jean-Marc Valin | c40addc | 2010-10-22 14:57:07 -0400 | [diff] [blame] | 471 | bits1[j] = C*N*m->allocVectors[mid*len+j]<<LM>>2; |
| 472 | if (bits1[j] > 0) |
Timothy B. Terriberry | 428a77d | 2010-12-16 16:50:16 -0800 | [diff] [blame] | 473 | bits1[j] = IMAX(0, bits1[j] + trim_offset[j]); |
Jean-Marc Valin | c40addc | 2010-10-22 14:57:07 -0400 | [diff] [blame] | 474 | bits1[j] += offsets[j]; |
Gregory Maxwell | 7c673cf | 2010-12-19 02:26:56 -0500 | [diff] [blame] | 475 | if (bits1[j] >= thresh[j] || done) |
| 476 | { |
| 477 | done = 1; |
| 478 | /* Don't allocate more than we can actually use */ |
Timothy B. Terriberry | 428a77d | 2010-12-16 16:50:16 -0800 | [diff] [blame] | 479 | psum += IMIN(bits1[j], 64*C<<BITRES<<LM); |
Gregory Maxwell | 7c673cf | 2010-12-19 02:26:56 -0500 | [diff] [blame] | 480 | } else { |
| 481 | if (bits1[j] >= C<<BITRES) |
| 482 | psum += C<<BITRES; |
| 483 | } |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 484 | } |
Timothy B. Terriberry | 4777f06 | 2010-12-15 06:56:00 -0800 | [diff] [blame] | 485 | if (psum > total) |
Timothy B. Terriberry | 428a77d | 2010-12-16 16:50:16 -0800 | [diff] [blame] | 486 | hi = mid - 1; |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 487 | else |
Timothy B. Terriberry | 428a77d | 2010-12-16 16:50:16 -0800 | [diff] [blame] | 488 | lo = mid + 1; |
Jean-Marc Valin | a85657b | 2008-02-20 11:59:30 +1100 | [diff] [blame] | 489 | /*printf ("lo = %d, hi = %d\n", lo, hi);*/ |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 490 | } |
Timothy B. Terriberry | 428a77d | 2010-12-16 16:50:16 -0800 | [diff] [blame] | 491 | while (lo <= hi); |
| 492 | hi = lo--; |
Jean-Marc Valin | 6775de3 | 2008-08-02 08:14:42 -0400 | [diff] [blame] | 493 | /*printf ("interp between %d and %d\n", lo, hi);*/ |
Jean-Marc Valin | 525d7cf | 2010-07-13 14:14:16 -0400 | [diff] [blame] | 494 | for (j=start;j<end;j++) |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 495 | { |
Jean-Marc Valin | bb8fa1f | 2010-06-03 00:33:42 -0400 | [diff] [blame] | 496 | int N = m->eBands[j+1]-m->eBands[j]; |
Timothy B. Terriberry | 428a77d | 2010-12-16 16:50:16 -0800 | [diff] [blame] | 497 | bits1[j] = C*N*m->allocVectors[lo*len+j]<<LM>>2; |
| 498 | bits2[j] = C*N*m->allocVectors[hi*len+j]<<LM>>2; |
Jean-Marc Valin | c40addc | 2010-10-22 14:57:07 -0400 | [diff] [blame] | 499 | if (bits1[j] > 0) |
Timothy B. Terriberry | 428a77d | 2010-12-16 16:50:16 -0800 | [diff] [blame] | 500 | bits1[j] = IMAX(0, bits1[j] + trim_offset[j]); |
| 501 | if (bits2[j] > 0) |
| 502 | bits2[j] = IMAX(0, bits2[j] + trim_offset[j]); |
| 503 | if (lo > 0) |
| 504 | bits1[j] += offsets[j]; |
| 505 | bits2[j] += offsets[j]; |
Gregory Maxwell | 8e44767 | 2010-12-16 14:22:51 -0500 | [diff] [blame] | 506 | if (offsets[j]>0) |
| 507 | skip_start = j; |
Timothy B. Terriberry | 428a77d | 2010-12-16 16:50:16 -0800 | [diff] [blame] | 508 | bits2[j] -= bits1[j]; |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 509 | } |
Gregory Maxwell | 8e44767 | 2010-12-16 14:22:51 -0500 | [diff] [blame] | 510 | codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thresh, |
Timothy B. Terriberry | 76469c6 | 2011-01-07 09:18:34 -0800 | [diff] [blame] | 511 | total, skip_rsv, intensity, intensity_rsv, dual_stereo, dual_stereo_rsv, |
Jean-Marc Valin | c39bb8a | 2011-01-26 10:50:55 -0500 | [diff] [blame^] | 512 | pulses, ebits, fine_priority, C, LM, ec, encode, prev); |
Jean-Marc Valin | 825ead8 | 2008-09-09 00:15:40 -0400 | [diff] [blame] | 513 | RESTORE_STACK; |
Jean-Marc Valin | b801da5 | 2010-09-28 14:56:20 -0400 | [diff] [blame] | 514 | return codedBands; |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 515 | } |
Jean-Marc Valin | b86ed07 | 2008-01-15 16:33:21 +1100 | [diff] [blame] | 516 | |