Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (c) 2007-2008 CSIRO |
2 | | Copyright (c) 2007-2009 Xiph.Org Foundation |
3 | | Written by Jean-Marc Valin */ |
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 | | 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 COPYRIGHT OWNER |
20 | | OR 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 | | |
29 | | #ifdef HAVE_CONFIG_H |
30 | | #include "config.h" |
31 | | #endif |
32 | | |
33 | | #include <math.h> |
34 | | #include "modes.h" |
35 | | #include "cwrs.h" |
36 | | #include "arch.h" |
37 | | #include "os_support.h" |
38 | | |
39 | | #include "entcode.h" |
40 | | #include "rate.h" |
41 | | |
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 | | |
50 | | #ifdef CUSTOM_MODES |
51 | | |
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) |
55 | | { |
56 | | static const opus_int16 maxN[15] = { |
57 | | 32767, 32767, 32767, 1476, 283, 109, 60, 40, |
58 | | 29, 24, 20, 18, 16, 14, 13}; |
59 | | static const opus_int16 maxK[15] = { |
60 | | 32767, 32767, 32767, 32767, 1172, 238, 95, 53, |
61 | | 36, 27, 22, 18, 16, 15, 13}; |
62 | | if (_n>=14) |
63 | | { |
64 | | if (_k>=14) |
65 | | return 0; |
66 | | else |
67 | | return _n <= maxN[_k]; |
68 | | } else { |
69 | | return _k <= maxK[_n]; |
70 | | } |
71 | | } |
72 | | |
73 | | void compute_pulse_cache(CELTMode *m, int LM) |
74 | | { |
75 | | int C; |
76 | | int i; |
77 | | int j; |
78 | | int curr=0; |
79 | | int nbEntries=0; |
80 | | int entryN[100], entryK[100], entryI[100]; |
81 | | const opus_int16 *eBands = m->eBands; |
82 | | PulseCache *cache = &m->cache; |
83 | | opus_int16 *cindex; |
84 | | unsigned char *bits; |
85 | | unsigned char *cap; |
86 | | |
87 | | cindex = (opus_int16 *)opus_alloc(sizeof(cache->index[0])*m->nbEBands*(LM+2)); |
88 | | cache->index = cindex; |
89 | | |
90 | | /* Scan for all unique band sizes */ |
91 | | for (i=0;i<=LM+1;i++) |
92 | | { |
93 | | for (j=0;j<m->nbEBands;j++) |
94 | | { |
95 | | int k; |
96 | | int N = (eBands[j+1]-eBands[j])<<i>>1; |
97 | | cindex[i*m->nbEBands+j] = -1; |
98 | | /* Find other bands that have the same size */ |
99 | | for (k=0;k<=i;k++) |
100 | | { |
101 | | int n; |
102 | | for (n=0;n<m->nbEBands && (k!=i || n<j);n++) |
103 | | { |
104 | | if (N == (eBands[n+1]-eBands[n])<<k>>1) |
105 | | { |
106 | | cindex[i*m->nbEBands+j] = cindex[k*m->nbEBands+n]; |
107 | | break; |
108 | | } |
109 | | } |
110 | | } |
111 | | if (cache->index[i*m->nbEBands+j] == -1 && N!=0) |
112 | | { |
113 | | int K; |
114 | | entryN[nbEntries] = N; |
115 | | K = 0; |
116 | | while (fits_in32(N,get_pulses(K+1)) && K<MAX_PSEUDO) |
117 | | K++; |
118 | | entryK[nbEntries] = K; |
119 | | cindex[i*m->nbEBands+j] = curr; |
120 | | entryI[nbEntries] = curr; |
121 | | |
122 | | curr += K+1; |
123 | | nbEntries++; |
124 | | } |
125 | | } |
126 | | } |
127 | | bits = (unsigned char *)opus_alloc(sizeof(unsigned char)*curr); |
128 | | cache->bits = bits; |
129 | | cache->size = curr; |
130 | | /* Compute the cache for all unique sizes */ |
131 | | for (i=0;i<nbEntries;i++) |
132 | | { |
133 | | unsigned char *ptr = bits+entryI[i]; |
134 | | opus_int16 tmp[CELT_MAX_PULSES+1]; |
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 | | } |
140 | | |
141 | | /* Compute the maximum rate for each band at which we'll reliably use as |
142 | | many bits as we ask for. */ |
143 | | cache->caps = cap = (unsigned char *)opus_alloc(sizeof(cache->caps[0])*(LM+1)*2*m->nbEBands); |
144 | | for (i=0;i<=LM;i++) |
145 | | { |
146 | | for (C=1;C<=2;C++) |
147 | | { |
148 | | for (j=0;j<m->nbEBands;j++) |
149 | | { |
150 | | int N0; |
151 | | int max_bits; |
152 | | N0 = m->eBands[j+1]-m->eBands[j]; |
153 | | /* N=1 bands only have a sign bit and fine bits. */ |
154 | | if (N0<<i == 1) |
155 | | max_bits = C*(1+MAX_FINE_BITS)<<BITRES; |
156 | | else |
157 | | { |
158 | | const unsigned char *pcache; |
159 | | opus_int32 num; |
160 | | opus_int32 den; |
161 | | int LM0; |
162 | | int N; |
163 | | int offset; |
164 | | int ndof; |
165 | | int qb; |
166 | | int k; |
167 | | LM0 = 0; |
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) |
171 | | { |
172 | | N0>>=1; |
173 | | LM0--; |
174 | | } |
175 | | /* N0=1 bands can't be split down to N<2. */ |
176 | | else if (N0 <= 1) |
177 | | { |
178 | | LM0=IMIN(i,1); |
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 */ |
192 | | offset = ((m->logN[j]+((LM0+k)<<BITRES))>>1)-QTHETA_OFFSET; |
193 | | /* The number of qtheta bits we'll allocate if the remainder |
194 | | is to be max_bits. |
195 | | The average measured cost for theta is 0.89701 times qb, |
196 | | approximated here as 459/512. */ |
197 | | num=459*(opus_int32)((2*N-1)*offset+max_bits); |
198 | | den=((opus_int32)(2*N-1)<<9)-459; |
199 | | qb = IMIN((num+(den>>1))/den, 57); |
200 | | celt_assert(qb >= 0); |
201 | | max_bits += qb; |
202 | | N <<= 1; |
203 | | } |
204 | | /* Add in the cost of a stereo split, if necessary. */ |
205 | | if (C==2) |
206 | | { |
207 | | max_bits <<= 1; |
208 | | offset = ((m->logN[j]+(i<<BITRES))>>1)-(N==2?QTHETA_OFFSET_TWOPHASE:QTHETA_OFFSET); |
209 | | ndof = 2*N-1-(N==2); |
210 | | /* The average measured cost for theta with the step PDF is |
211 | | 0.95164 times qb, approximated here as 487/512. */ |
212 | | num = (N==2?512:487)*(opus_int32)(max_bits+ndof*offset); |
213 | | den = ((opus_int32)ndof<<9)-(N==2?512:487); |
214 | | qb = IMIN((num+(den>>1))/den, (N==2?64:61)); |
215 | | celt_assert(qb >= 0); |
216 | | max_bits += qb; |
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 */ |
223 | | offset = ((m->logN[j] + (i<<BITRES))>>1)-FINE_OFFSET; |
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; |
230 | | den = (ndof-1)<<BITRES; |
231 | | qb = IMIN((num+(den>>1))/den, MAX_FINE_BITS); |
232 | | celt_assert(qb >= 0); |
233 | | max_bits += C*qb<<BITRES; |
234 | | } |
235 | | max_bits = (4*max_bits/(C*((m->eBands[j+1]-m->eBands[j])<<i)))-64; |
236 | | celt_assert(max_bits >= 0); |
237 | | celt_assert(max_bits < 256); |
238 | | *cap++ = (unsigned char)max_bits; |
239 | | } |
240 | | } |
241 | | } |
242 | | } |
243 | | |
244 | | #endif /* CUSTOM_MODES */ |
245 | | |
246 | 0 | #define ALLOC_STEPS 6 |
247 | | |
248 | | static OPUS_INLINE int interp_bits2pulses(const CELTMode *m, int start, int end, int skip_start, |
249 | | const int *bits1, const int *bits2, const int *thresh, const int *cap, opus_int32 total, opus_int32 *_balance, |
250 | | int skip_rsv, int *intensity, int intensity_rsv, int *dual_stereo, int dual_stereo_rsv, int *bits, |
251 | | int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev, int signalBandwidth) |
252 | 0 | { |
253 | 0 | opus_int32 psum; |
254 | 0 | int lo, hi; |
255 | 0 | int i, j; |
256 | 0 | int logM; |
257 | 0 | int stereo; |
258 | 0 | int codedBands=-1; |
259 | 0 | int alloc_floor; |
260 | 0 | opus_int32 left, percoeff; |
261 | 0 | int done; |
262 | 0 | opus_int32 balance; |
263 | 0 | SAVE_STACK; |
264 | |
|
265 | 0 | alloc_floor = C<<BITRES; |
266 | 0 | stereo = C>1; |
267 | |
|
268 | 0 | logM = LM<<BITRES; |
269 | 0 | lo = 0; |
270 | 0 | hi = 1<<ALLOC_STEPS; |
271 | 0 | for (i=0;i<ALLOC_STEPS;i++) |
272 | 0 | { |
273 | 0 | int mid = (lo+hi)>>1; |
274 | 0 | psum = 0; |
275 | 0 | done = 0; |
276 | 0 | for (j=end;j-->start;) |
277 | 0 | { |
278 | 0 | int tmp = bits1[j] + (mid*(opus_int32)bits2[j]>>ALLOC_STEPS); |
279 | 0 | if (tmp >= thresh[j] || done) |
280 | 0 | { |
281 | 0 | done = 1; |
282 | | /* Don't allocate more than we can actually use */ |
283 | 0 | psum += IMIN(tmp, cap[j]); |
284 | 0 | } else { |
285 | 0 | if (tmp >= alloc_floor) |
286 | 0 | psum += alloc_floor; |
287 | 0 | } |
288 | 0 | } |
289 | 0 | if (psum > total) |
290 | 0 | hi = mid; |
291 | 0 | else |
292 | 0 | lo = mid; |
293 | 0 | } |
294 | 0 | psum = 0; |
295 | | /*printf ("interp bisection gave %d\n", lo);*/ |
296 | 0 | done = 0; |
297 | 0 | for (j=end;j-->start;) |
298 | 0 | { |
299 | 0 | int tmp = bits1[j] + ((opus_int32)lo*bits2[j]>>ALLOC_STEPS); |
300 | 0 | if (tmp < thresh[j] && !done) |
301 | 0 | { |
302 | 0 | if (tmp >= alloc_floor) |
303 | 0 | tmp = alloc_floor; |
304 | 0 | else |
305 | 0 | tmp = 0; |
306 | 0 | } else |
307 | 0 | done = 1; |
308 | | /* Don't allocate more than we can actually use */ |
309 | 0 | tmp = IMIN(tmp, cap[j]); |
310 | 0 | bits[j] = tmp; |
311 | 0 | psum += tmp; |
312 | 0 | } |
313 | | |
314 | | /* Decide which bands to skip, working backwards from the end. */ |
315 | 0 | for (codedBands=end;;codedBands--) |
316 | 0 | { |
317 | 0 | int band_width; |
318 | 0 | int band_bits; |
319 | 0 | int rem; |
320 | 0 | j = codedBands-1; |
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. */ |
327 | 0 | if (j<=skip_start) |
328 | 0 | { |
329 | | /* Give the bit we reserved to end skipping back. */ |
330 | 0 | total += skip_rsv; |
331 | 0 | break; |
332 | 0 | } |
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 | 0 | left = total-psum; |
336 | 0 | percoeff = celt_udiv(left, m->eBands[codedBands]-m->eBands[start]); |
337 | 0 | left -= (m->eBands[codedBands]-m->eBands[start])*percoeff; |
338 | 0 | rem = IMAX(left-(m->eBands[j]-m->eBands[start]),0); |
339 | 0 | band_width = m->eBands[codedBands]-m->eBands[j]; |
340 | 0 | band_bits = (int)(bits[j] + percoeff*band_width + rem); |
341 | | /*Only code a skip decision if we're above the threshold for this band. |
342 | | Otherwise it is force-skipped. |
343 | | This ensures that we have enough bits to code the skip flag.*/ |
344 | 0 | if (band_bits >= IMAX(thresh[j], alloc_floor+(1<<BITRES))) |
345 | 0 | { |
346 | 0 | if (encode) |
347 | 0 | { |
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.*/ |
351 | 0 | int depth_threshold; |
352 | | /*We choose a threshold with some hysteresis to keep bands from |
353 | | fluctuating in and out, but we try not to fold below a certain point. */ |
354 | 0 | if (codedBands > 17) |
355 | 0 | depth_threshold = j<prev ? 7 : 9; |
356 | 0 | else |
357 | 0 | depth_threshold = 0; |
358 | | #ifdef FUZZING |
359 | | (void)signalBandwidth; |
360 | | (void)depth_threshold; |
361 | | if ((rand()&0x1) == 0) |
362 | | #else |
363 | 0 | if (codedBands<=start+2 || (band_bits > (depth_threshold*band_width<<LM<<BITRES)>>4 && j<=signalBandwidth)) |
364 | 0 | #endif |
365 | 0 | { |
366 | 0 | ec_enc_bit_logp(ec, 1, 1); |
367 | 0 | break; |
368 | 0 | } |
369 | 0 | ec_enc_bit_logp(ec, 0, 1); |
370 | 0 | } else if (ec_dec_bit_logp(ec, 1)) { |
371 | 0 | break; |
372 | 0 | } |
373 | | /*We used a bit to skip this band.*/ |
374 | 0 | psum += 1<<BITRES; |
375 | 0 | band_bits -= 1<<BITRES; |
376 | 0 | } |
377 | | /*Reclaim the bits originally allocated to this band.*/ |
378 | 0 | psum -= bits[j]+intensity_rsv; |
379 | 0 | if (intensity_rsv > 0) |
380 | 0 | intensity_rsv = LOG2_FRAC_TABLE[j-start]; |
381 | 0 | psum += intensity_rsv; |
382 | 0 | if (band_bits >= alloc_floor) |
383 | 0 | { |
384 | | /*If we have enough for a fine energy bit per channel, use it.*/ |
385 | 0 | psum += alloc_floor; |
386 | 0 | bits[j] = alloc_floor; |
387 | 0 | } else { |
388 | | /*Otherwise this band gets nothing at all.*/ |
389 | 0 | bits[j] = 0; |
390 | 0 | } |
391 | 0 | } |
392 | |
|
393 | 0 | celt_assert(codedBands > start); |
394 | | /* Code the intensity and dual stereo parameters. */ |
395 | 0 | if (intensity_rsv > 0) |
396 | 0 | { |
397 | 0 | if (encode) |
398 | 0 | { |
399 | 0 | *intensity = IMIN(*intensity, codedBands); |
400 | 0 | ec_enc_uint(ec, *intensity-start, codedBands+1-start); |
401 | 0 | } |
402 | 0 | else |
403 | 0 | *intensity = start+ec_dec_uint(ec, codedBands+1-start); |
404 | 0 | } |
405 | 0 | else |
406 | 0 | *intensity = 0; |
407 | 0 | if (*intensity <= start) |
408 | 0 | { |
409 | 0 | total += dual_stereo_rsv; |
410 | 0 | dual_stereo_rsv = 0; |
411 | 0 | } |
412 | 0 | if (dual_stereo_rsv > 0) |
413 | 0 | { |
414 | 0 | if (encode) |
415 | 0 | ec_enc_bit_logp(ec, *dual_stereo, 1); |
416 | 0 | else |
417 | 0 | *dual_stereo = ec_dec_bit_logp(ec, 1); |
418 | 0 | } |
419 | 0 | else |
420 | 0 | *dual_stereo = 0; |
421 | | |
422 | | /* Allocate the remaining bits */ |
423 | 0 | left = total-psum; |
424 | 0 | percoeff = celt_udiv(left, m->eBands[codedBands]-m->eBands[start]); |
425 | 0 | left -= (m->eBands[codedBands]-m->eBands[start])*percoeff; |
426 | 0 | for (j=start;j<codedBands;j++) |
427 | 0 | bits[j] += ((int)percoeff*(m->eBands[j+1]-m->eBands[j])); |
428 | 0 | for (j=start;j<codedBands;j++) |
429 | 0 | { |
430 | 0 | int tmp = (int)IMIN(left, m->eBands[j+1]-m->eBands[j]); |
431 | 0 | bits[j] += tmp; |
432 | 0 | left -= tmp; |
433 | 0 | } |
434 | | /*for (j=0;j<end;j++)printf("%d ", bits[j]);printf("\n");*/ |
435 | |
|
436 | 0 | balance = 0; |
437 | 0 | for (j=start;j<codedBands;j++) |
438 | 0 | { |
439 | 0 | int N0, N, den; |
440 | 0 | int offset; |
441 | 0 | int NClogN; |
442 | 0 | opus_int32 excess, bit; |
443 | |
|
444 | 0 | celt_assert(bits[j] >= 0); |
445 | 0 | N0 = m->eBands[j+1]-m->eBands[j]; |
446 | 0 | N=N0<<LM; |
447 | 0 | bit = (opus_int32)bits[j]+balance; |
448 | |
|
449 | 0 | if (N>1) |
450 | 0 | { |
451 | 0 | excess = MAX32(bit-cap[j],0); |
452 | 0 | bits[j] = bit-excess; |
453 | | |
454 | | /* Compensate for the extra DoF in stereo */ |
455 | 0 | den=(C*N+ ((C==2 && N>2 && !*dual_stereo && j<*intensity) ? 1 : 0)); |
456 | |
|
457 | 0 | NClogN = den*(m->logN[j] + logM); |
458 | | |
459 | | /* Offset for the number of fine bits by log2(N)/2 + FINE_OFFSET |
460 | | compared to their "fair share" of total/N */ |
461 | 0 | offset = (NClogN>>1)-den*FINE_OFFSET; |
462 | | |
463 | | /* N=2 is the only point that doesn't match the curve */ |
464 | 0 | if (N==2) |
465 | 0 | offset += den<<BITRES>>2; |
466 | | |
467 | | /* Changing the offset for allocating the second and third |
468 | | fine energy bit */ |
469 | 0 | if (bits[j] + offset < den*2<<BITRES) |
470 | 0 | offset += NClogN>>2; |
471 | 0 | else if (bits[j] + offset < den*3<<BITRES) |
472 | 0 | offset += NClogN>>3; |
473 | | |
474 | | /* Divide with rounding */ |
475 | 0 | ebits[j] = IMAX(0, (bits[j] + offset + (den<<(BITRES-1)))); |
476 | 0 | ebits[j] = celt_udiv(ebits[j], den)>>BITRES; |
477 | | |
478 | | /* Make sure not to bust */ |
479 | 0 | if (C*ebits[j] > (bits[j]>>BITRES)) |
480 | 0 | ebits[j] = bits[j] >> stereo >> BITRES; |
481 | | |
482 | | /* More than that is useless because that's about as far as PVQ can go */ |
483 | 0 | ebits[j] = IMIN(ebits[j], MAX_FINE_BITS); |
484 | | |
485 | | /* If we rounded down or capped this band, make it a candidate for the |
486 | | final fine energy pass */ |
487 | 0 | fine_priority[j] = ebits[j]*(den<<BITRES) >= bits[j]+offset; |
488 | | |
489 | | /* Remove the allocated fine bits; the rest are assigned to PVQ */ |
490 | 0 | bits[j] -= C*ebits[j]<<BITRES; |
491 | |
|
492 | 0 | } else { |
493 | | /* For N=1, all bits go to fine energy except for a single sign bit */ |
494 | 0 | excess = MAX32(0,bit-(C<<BITRES)); |
495 | 0 | bits[j] = bit-excess; |
496 | 0 | ebits[j] = 0; |
497 | 0 | fine_priority[j] = 1; |
498 | 0 | } |
499 | | |
500 | | /* Fine energy can't take advantage of the re-balancing in |
501 | | quant_all_bands(). |
502 | | Instead, do the re-balancing here.*/ |
503 | 0 | if(excess > 0) |
504 | 0 | { |
505 | 0 | int extra_fine; |
506 | 0 | int extra_bits; |
507 | 0 | extra_fine = IMIN(excess>>(stereo+BITRES),MAX_FINE_BITS-ebits[j]); |
508 | 0 | ebits[j] += extra_fine; |
509 | 0 | extra_bits = extra_fine*C<<BITRES; |
510 | 0 | fine_priority[j] = extra_bits >= excess-balance; |
511 | 0 | excess -= extra_bits; |
512 | 0 | } |
513 | 0 | balance = excess; |
514 | |
|
515 | 0 | celt_assert(bits[j] >= 0); |
516 | 0 | celt_assert(ebits[j] >= 0); |
517 | 0 | } |
518 | | /* Save any remaining bits over the cap for the rebalancing in |
519 | | quant_all_bands(). */ |
520 | 0 | *_balance = balance; |
521 | | |
522 | | /* The skipped bands use all their bits for fine energy. */ |
523 | 0 | for (;j<end;j++) |
524 | 0 | { |
525 | 0 | ebits[j] = bits[j] >> stereo >> BITRES; |
526 | 0 | celt_assert(C*ebits[j]<<BITRES == bits[j]); |
527 | 0 | bits[j] = 0; |
528 | 0 | fine_priority[j] = ebits[j]<1; |
529 | 0 | } |
530 | 0 | RESTORE_STACK; |
531 | 0 | return codedBands; |
532 | 0 | } |
533 | | |
534 | | int clt_compute_allocation(const CELTMode *m, int start, int end, const int *offsets, const int *cap, int alloc_trim, int *intensity, int *dual_stereo, |
535 | | opus_int32 total, opus_int32 *balance, int *pulses, int *ebits, int *fine_priority, int C, int LM, ec_ctx *ec, int encode, int prev, int signalBandwidth) |
536 | 0 | { |
537 | 0 | int lo, hi, len, j; |
538 | 0 | int codedBands; |
539 | 0 | int skip_start; |
540 | 0 | int skip_rsv; |
541 | 0 | int intensity_rsv; |
542 | 0 | int dual_stereo_rsv; |
543 | 0 | VARDECL(int, bits1); |
544 | 0 | VARDECL(int, bits2); |
545 | 0 | VARDECL(int, thresh); |
546 | 0 | VARDECL(int, trim_offset); |
547 | 0 | SAVE_STACK; |
548 | |
|
549 | 0 | total = IMAX(total, 0); |
550 | 0 | len = m->nbEBands; |
551 | 0 | skip_start = start; |
552 | | /* Reserve a bit to signal the end of manually skipped bands. */ |
553 | 0 | skip_rsv = total >= 1<<BITRES ? 1<<BITRES : 0; |
554 | 0 | total -= skip_rsv; |
555 | | /* Reserve bits for the intensity and dual stereo parameters. */ |
556 | 0 | intensity_rsv = dual_stereo_rsv = 0; |
557 | 0 | if (C==2) |
558 | 0 | { |
559 | 0 | intensity_rsv = LOG2_FRAC_TABLE[end-start]; |
560 | 0 | if (intensity_rsv>total) |
561 | 0 | intensity_rsv = 0; |
562 | 0 | else |
563 | 0 | { |
564 | 0 | total -= intensity_rsv; |
565 | 0 | dual_stereo_rsv = total>=1<<BITRES ? 1<<BITRES : 0; |
566 | 0 | total -= dual_stereo_rsv; |
567 | 0 | } |
568 | 0 | } |
569 | 0 | ALLOC(bits1, len, int); |
570 | 0 | ALLOC(bits2, len, int); |
571 | 0 | ALLOC(thresh, len, int); |
572 | 0 | ALLOC(trim_offset, len, int); |
573 | |
|
574 | 0 | for (j=start;j<end;j++) |
575 | 0 | { |
576 | | /* Below this threshold, we're sure not to allocate any PVQ bits */ |
577 | 0 | thresh[j] = IMAX((C)<<BITRES, (3*(m->eBands[j+1]-m->eBands[j])<<LM<<BITRES)>>4); |
578 | | /* Tilt of the allocation curve */ |
579 | 0 | trim_offset[j] = C*(m->eBands[j+1]-m->eBands[j])*(alloc_trim-5-LM)*(end-j-1) |
580 | 0 | *(1<<(LM+BITRES))>>6; |
581 | | /* Giving less resolution to single-coefficient bands because they get |
582 | | more benefit from having one coarse value per coefficient*/ |
583 | 0 | if ((m->eBands[j+1]-m->eBands[j])<<LM==1) |
584 | 0 | trim_offset[j] -= C<<BITRES; |
585 | 0 | } |
586 | 0 | lo = 1; |
587 | 0 | hi = m->nbAllocVectors - 1; |
588 | 0 | do |
589 | 0 | { |
590 | 0 | int done = 0; |
591 | 0 | int psum = 0; |
592 | 0 | int mid = (lo+hi) >> 1; |
593 | 0 | for (j=end;j-->start;) |
594 | 0 | { |
595 | 0 | int bitsj; |
596 | 0 | int N = m->eBands[j+1]-m->eBands[j]; |
597 | 0 | bitsj = C*N*m->allocVectors[mid*len+j]<<LM>>2; |
598 | 0 | if (bitsj > 0) |
599 | 0 | bitsj = IMAX(0, bitsj + trim_offset[j]); |
600 | 0 | bitsj += offsets[j]; |
601 | 0 | if (bitsj >= thresh[j] || done) |
602 | 0 | { |
603 | 0 | done = 1; |
604 | | /* Don't allocate more than we can actually use */ |
605 | 0 | psum += IMIN(bitsj, cap[j]); |
606 | 0 | } else { |
607 | 0 | if (bitsj >= C<<BITRES) |
608 | 0 | psum += C<<BITRES; |
609 | 0 | } |
610 | 0 | } |
611 | 0 | if (psum > total) |
612 | 0 | hi = mid - 1; |
613 | 0 | else |
614 | 0 | lo = mid + 1; |
615 | | /*printf ("lo = %d, hi = %d\n", lo, hi);*/ |
616 | 0 | } |
617 | 0 | while (lo <= hi); |
618 | 0 | hi = lo--; |
619 | | /*printf ("interp between %d and %d\n", lo, hi);*/ |
620 | 0 | for (j=start;j<end;j++) |
621 | 0 | { |
622 | 0 | int bits1j, bits2j; |
623 | 0 | int N = m->eBands[j+1]-m->eBands[j]; |
624 | 0 | bits1j = C*N*m->allocVectors[lo*len+j]<<LM>>2; |
625 | 0 | bits2j = hi>=m->nbAllocVectors ? |
626 | 0 | cap[j] : C*N*m->allocVectors[hi*len+j]<<LM>>2; |
627 | 0 | if (bits1j > 0) |
628 | 0 | bits1j = IMAX(0, bits1j + trim_offset[j]); |
629 | 0 | if (bits2j > 0) |
630 | 0 | bits2j = IMAX(0, bits2j + trim_offset[j]); |
631 | 0 | if (lo > 0) |
632 | 0 | bits1j += offsets[j]; |
633 | 0 | bits2j += offsets[j]; |
634 | 0 | if (offsets[j]>0) |
635 | 0 | skip_start = j; |
636 | 0 | bits2j = IMAX(0,bits2j-bits1j); |
637 | 0 | bits1[j] = bits1j; |
638 | 0 | bits2[j] = bits2j; |
639 | 0 | } |
640 | 0 | codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thresh, cap, |
641 | 0 | total, balance, skip_rsv, intensity, intensity_rsv, dual_stereo, dual_stereo_rsv, |
642 | 0 | pulses, ebits, fine_priority, C, LM, ec, encode, prev, signalBandwidth); |
643 | 0 | RESTORE_STACK; |
644 | 0 | return codedBands; |
645 | 0 | } |
646 | | |