Jean-Marc Valin | 8b2ff0d | 2009-10-17 21:40:10 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2007-2008 CSIRO |
| 2 | Copyright (c) 2007-2009 Xiph.Org Foundation |
| 3 | Written by Jean-Marc Valin */ |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 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 | - 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 Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 32 | |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 33 | #ifndef RATE_H |
| 34 | #define RATE_H |
| 35 | |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 36 | #define MAX_PSEUDO 40 |
| 37 | #define LOG_MAX_PSEUDO 6 |
| 38 | |
Jean-Marc Valin | 7fff572 | 2010-10-18 16:20:00 -0400 | [diff] [blame] | 39 | #define MAX_PULSES 128 |
Jean-Marc Valin | b76ee70 | 2008-03-10 15:42:35 +1100 | [diff] [blame] | 40 | |
Jean-Marc Valin | 6c37fb1 | 2010-06-02 21:25:19 -0400 | [diff] [blame] | 41 | #define BITRES 3 |
Jean-Marc Valin | 32a1e6b | 2010-08-30 23:35:02 -0400 | [diff] [blame] | 42 | #define FINE_OFFSET 21 |
Jean-Marc Valin | f1c8fb1 | 2010-08-31 06:45:32 -0400 | [diff] [blame] | 43 | #define QTHETA_OFFSET 4 |
Jean-Marc Valin | 88c59a5 | 2010-08-05 16:15:46 -0400 | [diff] [blame] | 44 | #define QTHETA_OFFSET_STEREO 4 |
Jean-Marc Valin | 91f07dc | 2009-07-25 20:42:24 -0400 | [diff] [blame] | 45 | |
Jean-Marc Valin | 7b0cb4b | 2008-09-11 00:20:08 -0400 | [diff] [blame] | 46 | #define BITOVERFLOW 30000 |
| 47 | |
Jean-Marc Valin | 6f18d11 | 2009-05-20 22:50:46 -0400 | [diff] [blame] | 48 | #include "cwrs.h" |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 49 | #include "modes.h" |
| 50 | |
| 51 | void compute_pulse_cache(CELTMode *m, int LM); |
Jean-Marc Valin | 6f18d11 | 2009-05-20 22:50:46 -0400 | [diff] [blame] | 52 | |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 53 | static inline int get_pulses(int i) |
| 54 | { |
| 55 | return i<8 ? i : (8 + (i&7)) << ((i>>3)-1); |
| 56 | } |
| 57 | |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 58 | static inline int bits2pulses(const CELTMode *m, int band, int LM, int bits) |
Jean-Marc Valin | 7b0cb4b | 2008-09-11 00:20:08 -0400 | [diff] [blame] | 59 | { |
| 60 | int i; |
| 61 | int lo, hi; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 62 | const unsigned char *cache; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 63 | |
| 64 | LM++; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 65 | cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band]; |
Jean-Marc Valin | 2fcb239 | 2010-05-24 09:02:35 -0400 | [diff] [blame] | 66 | |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 67 | lo = 0; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 68 | hi = cache[0]; |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 69 | 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 Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 73 | if (cache[mid]+1 >= bits) |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 74 | hi = mid; |
| 75 | else |
| 76 | lo = mid; |
| 77 | } |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 78 | if (bits- (lo == 0 ? 0 : cache[lo]+1) <= cache[hi]+1-bits) |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 79 | return lo; |
| 80 | else |
| 81 | return hi; |
Jean-Marc Valin | 7b0cb4b | 2008-09-11 00:20:08 -0400 | [diff] [blame] | 82 | } |
| 83 | |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 84 | static inline int pulses2bits(const CELTMode *m, int band, int LM, int pulses) |
Jean-Marc Valin | 6f18d11 | 2009-05-20 22:50:46 -0400 | [diff] [blame] | 85 | { |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 86 | const unsigned char *cache; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 87 | |
| 88 | LM++; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 89 | cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band]; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 90 | return pulses == 0 ? 0 : cache[pulses]+1; |
Jean-Marc Valin | 6f18d11 | 2009-05-20 22:50:46 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Jean-Marc Valin | 879fbfd | 2008-02-20 17:17:13 +1100 | [diff] [blame] | 93 | /** Computes a cache of the pulses->bits mapping in each band */ |
Jean-Marc Valin | cae30df | 2010-05-21 00:26:03 -0400 | [diff] [blame] | 94 | celt_int16 **compute_alloc_cache(CELTMode *m, int M); |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 95 | |
Jean-Marc Valin | 879fbfd | 2008-02-20 17:17:13 +1100 | [diff] [blame] | 96 | /** 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 Valin | 5c80391 | 2010-12-16 14:11:48 -0500 | [diff] [blame] | 105 | int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, int alloc_trim, |
Timothy B. Terriberry | b2f5900 | 2010-12-15 05:12:43 -0800 | [diff] [blame] | 106 | int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM, void *ec, int encode, int prev); |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 107 | |
| 108 | |
| 109 | #endif |