blob: 0e6391a9e39ecf552452af53a1057a572d7d91bd [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:
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*/
Jean-Marc Valin33ddd792008-01-14 17:39:01 +110032
Jean-Marc Valinf51ca492008-01-17 10:58:38 +110033#ifndef RATE_H
34#define RATE_H
35
Jean-Marc Valin164a2292009-07-22 07:48:35 -040036#define MAX_PSEUDO 40
37#define LOG_MAX_PSEUDO 6
38
Jean-Marc Valin7fff5722010-10-18 16:20:00 -040039#define MAX_PULSES 128
Jean-Marc Valinb76ee702008-03-10 15:42:35 +110040
Jean-Marc Valin6c37fb12010-06-02 21:25:19 -040041#define BITRES 3
Jean-Marc Valin32a1e6b2010-08-30 23:35:02 -040042#define FINE_OFFSET 21
Jean-Marc Valinf1c8fb12010-08-31 06:45:32 -040043#define QTHETA_OFFSET 4
Jean-Marc Valin88c59a52010-08-05 16:15:46 -040044#define QTHETA_OFFSET_STEREO 4
Jean-Marc Valin91f07dc2009-07-25 20:42:24 -040045
Jean-Marc Valin7b0cb4b2008-09-11 00:20:08 -040046#define BITOVERFLOW 30000
47
Jean-Marc Valin6f18d112009-05-20 22:50:46 -040048#include "cwrs.h"
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040049#include "modes.h"
50
51void compute_pulse_cache(CELTMode *m, int LM);
Jean-Marc Valin6f18d112009-05-20 22:50:46 -040052
Jean-Marc Valin164a2292009-07-22 07:48:35 -040053static inline int get_pulses(int i)
54{
55 return i<8 ? i : (8 + (i&7)) << ((i>>3)-1);
56}
57
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040058static inline int bits2pulses(const CELTMode *m, int band, int LM, int bits)
Jean-Marc Valin7b0cb4b2008-09-11 00:20:08 -040059{
60 int i;
61 int lo, hi;
Jean-Marc Valin732ea382010-08-25 13:52:27 -040062 const unsigned char *cache;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040063
64 LM++;
Jean-Marc Valin732ea382010-08-25 13:52:27 -040065 cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band];
Jean-Marc Valin2fcb2392010-05-24 09:02:35 -040066
Jean-Marc Valin164a2292009-07-22 07:48:35 -040067 lo = 0;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040068 hi = cache[0];
Jean-Marc Valin164a2292009-07-22 07:48:35 -040069 for (i=0;i<LOG_MAX_PSEUDO;i++)
70 {
71 int mid = (lo+hi)>>1;
72 /* OPT: Make sure this is implemented with a conditional move */
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040073 if (cache[mid]+1 >= bits)
Jean-Marc Valin164a2292009-07-22 07:48:35 -040074 hi = mid;
75 else
76 lo = mid;
77 }
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040078 if (bits- (lo == 0 ? 0 : cache[lo]+1) <= cache[hi]+1-bits)
Jean-Marc Valin164a2292009-07-22 07:48:35 -040079 return lo;
80 else
81 return hi;
Jean-Marc Valin7b0cb4b2008-09-11 00:20:08 -040082}
83
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040084static inline int pulses2bits(const CELTMode *m, int band, int LM, int pulses)
Jean-Marc Valin6f18d112009-05-20 22:50:46 -040085{
Jean-Marc Valin732ea382010-08-25 13:52:27 -040086 const unsigned char *cache;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040087
88 LM++;
Jean-Marc Valin732ea382010-08-25 13:52:27 -040089 cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band];
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040090 return pulses == 0 ? 0 : cache[pulses]+1;
Jean-Marc Valin6f18d112009-05-20 22:50:46 -040091}
92
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +110093/** Computes a cache of the pulses->bits mapping in each band */
Jean-Marc Valincae30df2010-05-21 00:26:03 -040094celt_int16 **compute_alloc_cache(CELTMode *m, int M);
Jean-Marc Valinf51ca492008-01-17 10:58:38 +110095
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +110096/** Compute the pulse allocation, i.e. how many pulses will go in each
97 * band.
98 @param m mode
99 @param offsets Requested increase or decrease in the number of bits for
100 each band
101 @param total Number of bands
102 @param pulses Number of pulses per band (returned)
103 @return Total number of bits allocated
104*/
Jean-Marc Valin5c803912010-12-16 14:11:48 -0500105int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, int alloc_trim,
Timothy B. Terriberryb2f59002010-12-15 05:12:43 -0800106 int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM, void *ec, int encode, int prev);
Jean-Marc Valinf51ca492008-01-17 10:58:38 +1100107
108
109#endif