blob: 41613f49a3626289fdf0a90ece15a9fbc0bed660 [file] [log] [blame]
Jean-Marc Valin8b2ff0d2009-10-17 21:40:10 -04001/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
Jean-Marc Valinf51ca492008-01-17 10:58:38 +11004/*
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 Maxwell71d39ad2011-07-30 00:00:29 -04008
Jean-Marc Valinf51ca492008-01-17 10:58:38 +11009 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
Gregory Maxwell71d39ad2011-07-30 00:00:29 -040011
Jean-Marc Valinf51ca492008-01-17 10:58:38 +110012 - 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 Maxwell71d39ad2011-07-30 00:00:29 -040015
Jean-Marc Valinf51ca492008-01-17 10:58:38 +110016 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*/
Jean-Marc Valin33ddd792008-01-14 17:39:01 +110028
Jean-Marc Valinf51ca492008-01-17 10:58:38 +110029#ifndef RATE_H
30#define RATE_H
31
Jean-Marc Valin164a2292009-07-22 07:48:35 -040032#define MAX_PSEUDO 40
33#define LOG_MAX_PSEUDO 6
34
Jean-Marc Valin7fff5722010-10-18 16:20:00 -040035#define MAX_PULSES 128
Jean-Marc Valinb76ee702008-03-10 15:42:35 +110036
Jean-Marc Valina66b7572011-01-10 13:21:04 -050037#define MAX_FINE_BITS 8
38
Jean-Marc Valin32a1e6b2010-08-30 23:35:02 -040039#define FINE_OFFSET 21
Jean-Marc Valinf1c8fb12010-08-31 06:45:32 -040040#define QTHETA_OFFSET 4
Timothy B. Terriberry411a84f2011-02-01 01:55:01 -080041#define QTHETA_OFFSET_TWOPHASE 16
Jean-Marc Valin91f07dc2009-07-25 20:42:24 -040042
Jean-Marc Valin7b0cb4b2008-09-11 00:20:08 -040043#define BITOVERFLOW 30000
44
Jean-Marc Valin6f18d112009-05-20 22:50:46 -040045#include "cwrs.h"
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040046#include "modes.h"
47
48void compute_pulse_cache(CELTMode *m, int LM);
Jean-Marc Valin6f18d112009-05-20 22:50:46 -040049
Jean-Marc Valin164a2292009-07-22 07:48:35 -040050static inline int get_pulses(int i)
51{
52 return i<8 ? i : (8 + (i&7)) << ((i>>3)-1);
53}
54
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040055static inline int bits2pulses(const CELTMode *m, int band, int LM, int bits)
Jean-Marc Valin7b0cb4b2008-09-11 00:20:08 -040056{
57 int i;
58 int lo, hi;
Jean-Marc Valin732ea382010-08-25 13:52:27 -040059 const unsigned char *cache;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040060
61 LM++;
Jean-Marc Valin732ea382010-08-25 13:52:27 -040062 cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band];
Jean-Marc Valin2fcb2392010-05-24 09:02:35 -040063
Jean-Marc Valin164a2292009-07-22 07:48:35 -040064 lo = 0;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040065 hi = cache[0];
Timothy B. Terriberry1cb32aa2011-01-04 09:07:40 -080066 bits--;
Jean-Marc Valin164a2292009-07-22 07:48:35 -040067 for (i=0;i<LOG_MAX_PSEUDO;i++)
68 {
Timothy B. Terriberry1cb32aa2011-01-04 09:07:40 -080069 int mid = (lo+hi+1)>>1;
Jean-Marc Valin164a2292009-07-22 07:48:35 -040070 /* OPT: Make sure this is implemented with a conditional move */
Timothy B. Terriberry1cb32aa2011-01-04 09:07:40 -080071 if (cache[mid] >= bits)
Jean-Marc Valin164a2292009-07-22 07:48:35 -040072 hi = mid;
73 else
74 lo = mid;
75 }
Timothy B. Terriberry1cb32aa2011-01-04 09:07:40 -080076 if (bits- (lo == 0 ? -1 : cache[lo]) <= cache[hi]-bits)
Jean-Marc Valin164a2292009-07-22 07:48:35 -040077 return lo;
78 else
79 return hi;
Jean-Marc Valin7b0cb4b2008-09-11 00:20:08 -040080}
81
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040082static inline int pulses2bits(const CELTMode *m, int band, int LM, int pulses)
Jean-Marc Valin6f18d112009-05-20 22:50:46 -040083{
Jean-Marc Valin732ea382010-08-25 13:52:27 -040084 const unsigned char *cache;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040085
86 LM++;
Jean-Marc Valin732ea382010-08-25 13:52:27 -040087 cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band];
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040088 return pulses == 0 ? 0 : cache[pulses]+1;
Jean-Marc Valin6f18d112009-05-20 22:50:46 -040089}
90
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +110091/** Computes a cache of the pulses->bits mapping in each band */
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040092opus_int16 **compute_alloc_cache(CELTMode *m, int M);
Jean-Marc Valinf51ca492008-01-17 10:58:38 +110093
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +110094/** Compute the pulse allocation, i.e. how many pulses will go in each
Gregory Maxwell71d39ad2011-07-30 00:00:29 -040095 * band.
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +110096 @param m mode
97 @param offsets Requested increase or decrease in the number of bits for
98 each band
99 @param total Number of bands
100 @param pulses Number of pulses per band (returned)
101 @return Total number of bits allocated
102*/
Timothy B. Terriberryc5643072011-01-29 12:57:18 -0800103int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, const int *cap, int alloc_trim, int *intensity, int *dual_stero,
Jean-Marc Valind77d6a52011-07-29 17:33:06 -0400104 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 Valinf51ca492008-01-17 10:58:38 +1100105
106
107#endif