michael@0: /* Copyright (c) 2007-2008 CSIRO michael@0: Copyright (c) 2007-2009 Xiph.Org Foundation michael@0: Written by Jean-Marc Valin */ michael@0: /* michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions michael@0: are met: michael@0: michael@0: - Redistributions of source code must retain the above copyright michael@0: notice, this list of conditions and the following disclaimer. michael@0: michael@0: - Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER michael@0: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifndef RATE_H michael@0: #define RATE_H michael@0: michael@0: #define MAX_PSEUDO 40 michael@0: #define LOG_MAX_PSEUDO 6 michael@0: michael@0: #define MAX_PULSES 128 michael@0: michael@0: #define MAX_FINE_BITS 8 michael@0: michael@0: #define FINE_OFFSET 21 michael@0: #define QTHETA_OFFSET 4 michael@0: #define QTHETA_OFFSET_TWOPHASE 16 michael@0: michael@0: #include "cwrs.h" michael@0: #include "modes.h" michael@0: michael@0: void compute_pulse_cache(CELTMode *m, int LM); michael@0: michael@0: static OPUS_INLINE int get_pulses(int i) michael@0: { michael@0: return i<8 ? i : (8 + (i&7)) << ((i>>3)-1); michael@0: } michael@0: michael@0: static OPUS_INLINE int bits2pulses(const CELTMode *m, int band, int LM, int bits) michael@0: { michael@0: int i; michael@0: int lo, hi; michael@0: const unsigned char *cache; michael@0: michael@0: LM++; michael@0: cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band]; michael@0: michael@0: lo = 0; michael@0: hi = cache[0]; michael@0: bits--; michael@0: for (i=0;i>1; michael@0: /* OPT: Make sure this is implemented with a conditional move */ michael@0: if ((int)cache[mid] >= bits) michael@0: hi = mid; michael@0: else michael@0: lo = mid; michael@0: } michael@0: if (bits- (lo == 0 ? -1 : (int)cache[lo]) <= (int)cache[hi]-bits) michael@0: return lo; michael@0: else michael@0: return hi; michael@0: } michael@0: michael@0: static OPUS_INLINE int pulses2bits(const CELTMode *m, int band, int LM, int pulses) michael@0: { michael@0: const unsigned char *cache; michael@0: michael@0: LM++; michael@0: cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band]; michael@0: return pulses == 0 ? 0 : cache[pulses]+1; michael@0: } michael@0: michael@0: /** Compute the pulse allocation, i.e. how many pulses will go in each michael@0: * band. michael@0: @param m mode michael@0: @param offsets Requested increase or decrease in the number of bits for michael@0: each band michael@0: @param total Number of bands michael@0: @param pulses Number of pulses per band (returned) michael@0: @return Total number of bits allocated michael@0: */ michael@0: int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, const int *cap, int alloc_trim, int *intensity, int *dual_stero, michael@0: 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); michael@0: michael@0: #endif