1 /* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
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.
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.
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.
37 #define LOG_MAX_PSEUDO 6
39 #define MAX_PULSES 128
40 #define LOG_MAX_PULSES 7
43 #define FINE_OFFSET 50
44 #define QTHETA_OFFSET 40
46 #define BITOVERFLOW 30000
50 static inline int get_pulses(int i)
52 return i<8 ? i : (8 + (i&7)) << ((i>>3)-1);
55 static inline int bits2pulses(const CELTMode *m, const celt_int16 *cache, int N, int bits)
62 for (i=0;i<LOG_MAX_PSEUDO;i++)
65 /* OPT: Make sure this is implemented with a conditional move */
66 if (cache[mid] >= bits)
71 if (bits-cache[lo] <= cache[hi]-bits)
79 #if 0 /* Disabled until we can make that useful */
80 /* Use of more than MAX_PULSES is disabled until we are able to cwrs that decently */
81 if (bits > cache[MAX_PULSES-1] && N<=4)
85 while (16 + log2_frac(2*(pulses+1)*(pulses+1) + 1, 4) <= bits && pulses < 32767)
94 int pulses = (lo+hi)>>1;
95 if (log2_frac(((UMUL16_16(pulses,pulses)>>1)+1)>>1, 4) > bits)
105 int pulses = (lo+hi)>>1;
106 if (log2_frac((UMUL32(UMUL16_16(pulses,pulses)+2,pulses))/3<<3, 4) > bits)
116 /* Instead of using the "bisection condition" we use a fixed number of
117 iterations because it should be faster */
118 /*while (hi-lo != 1)*/
119 for (i=0;i<LOG_MAX_PULSES;i++)
121 int mid = (lo+hi)>>1;
122 /* OPT: Make sure this is implemented with a conditional move */
123 if (cache[mid] >= bits)
128 if (bits-cache[lo] <= cache[hi]-bits)
135 static inline int pulses2bits(const celt_int16 *cache, int N, int pulses)
137 #if 0 /* Use of more than MAX_PULSES is disabled until we are able to cwrs that decently */
144 bits = log2_frac(((UMUL16_16(pulses,pulses)>>1)+1)>>1, 4);
147 bits = log2_frac((UMUL32(UMUL16_16(pulses,pulses)+2,pulses))/3<<3, 4);
150 /*printf ("%d <- %d\n", bits, pulses);*/
154 return cache[pulses];
157 /** Computes a cache of the pulses->bits mapping in each band */
158 celt_int16 **compute_alloc_cache(CELTMode *m, int C, int M);
160 /** Compute the pulse allocation, i.e. how many pulses will go in each
163 @param offsets Requested increase or decrease in the number of bits for
165 @param total Number of bands
166 @param pulses Number of pulses per band (returned)
167 @return Total number of bits allocated
169 void compute_allocation(const CELTMode *m, int start, int *offsets, int total, int *pulses, int *ebits, int *fine_priority, int _C, int M);