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: |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame^] | 8 | |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 9 | - Redistributions of source code must retain the above copyright |
| 10 | notice, this list of conditions and the following disclaimer. |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame^] | 11 | |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 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. |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame^] | 15 | |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 16 | 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 Valin | 33ddd79 | 2008-01-14 17:39:01 +1100 | [diff] [blame] | 28 | |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 29 | #ifndef RATE_H |
| 30 | #define RATE_H |
| 31 | |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 32 | #define MAX_PSEUDO 40 |
| 33 | #define LOG_MAX_PSEUDO 6 |
| 34 | |
Jean-Marc Valin | 7fff572 | 2010-10-18 16:20:00 -0400 | [diff] [blame] | 35 | #define MAX_PULSES 128 |
Jean-Marc Valin | b76ee70 | 2008-03-10 15:42:35 +1100 | [diff] [blame] | 36 | |
Jean-Marc Valin | a66b757 | 2011-01-10 13:21:04 -0500 | [diff] [blame] | 37 | #define MAX_FINE_BITS 8 |
| 38 | |
Jean-Marc Valin | 32a1e6b | 2010-08-30 23:35:02 -0400 | [diff] [blame] | 39 | #define FINE_OFFSET 21 |
Jean-Marc Valin | f1c8fb1 | 2010-08-31 06:45:32 -0400 | [diff] [blame] | 40 | #define QTHETA_OFFSET 4 |
Timothy B. Terriberry | 411a84f | 2011-02-01 01:55:01 -0800 | [diff] [blame] | 41 | #define QTHETA_OFFSET_TWOPHASE 16 |
Jean-Marc Valin | 91f07dc | 2009-07-25 20:42:24 -0400 | [diff] [blame] | 42 | |
Jean-Marc Valin | 7b0cb4b | 2008-09-11 00:20:08 -0400 | [diff] [blame] | 43 | #define BITOVERFLOW 30000 |
| 44 | |
Jean-Marc Valin | 6f18d11 | 2009-05-20 22:50:46 -0400 | [diff] [blame] | 45 | #include "cwrs.h" |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 46 | #include "modes.h" |
| 47 | |
| 48 | void compute_pulse_cache(CELTMode *m, int LM); |
Jean-Marc Valin | 6f18d11 | 2009-05-20 22:50:46 -0400 | [diff] [blame] | 49 | |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 50 | static inline int get_pulses(int i) |
| 51 | { |
| 52 | return i<8 ? i : (8 + (i&7)) << ((i>>3)-1); |
| 53 | } |
| 54 | |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 55 | 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] | 56 | { |
| 57 | int i; |
| 58 | int lo, hi; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 59 | const unsigned char *cache; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 60 | |
| 61 | LM++; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 62 | cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band]; |
Jean-Marc Valin | 2fcb239 | 2010-05-24 09:02:35 -0400 | [diff] [blame] | 63 | |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 64 | lo = 0; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 65 | hi = cache[0]; |
Timothy B. Terriberry | 1cb32aa | 2011-01-04 09:07:40 -0800 | [diff] [blame] | 66 | bits--; |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 67 | for (i=0;i<LOG_MAX_PSEUDO;i++) |
| 68 | { |
Timothy B. Terriberry | 1cb32aa | 2011-01-04 09:07:40 -0800 | [diff] [blame] | 69 | int mid = (lo+hi+1)>>1; |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 70 | /* OPT: Make sure this is implemented with a conditional move */ |
Timothy B. Terriberry | 1cb32aa | 2011-01-04 09:07:40 -0800 | [diff] [blame] | 71 | if (cache[mid] >= bits) |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 72 | hi = mid; |
| 73 | else |
| 74 | lo = mid; |
| 75 | } |
Timothy B. Terriberry | 1cb32aa | 2011-01-04 09:07:40 -0800 | [diff] [blame] | 76 | if (bits- (lo == 0 ? -1 : cache[lo]) <= cache[hi]-bits) |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 77 | return lo; |
| 78 | else |
| 79 | return hi; |
Jean-Marc Valin | 7b0cb4b | 2008-09-11 00:20:08 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 82 | 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] | 83 | { |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 84 | const unsigned char *cache; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 85 | |
| 86 | LM++; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 87 | cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band]; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 88 | return pulses == 0 ? 0 : cache[pulses]+1; |
Jean-Marc Valin | 6f18d11 | 2009-05-20 22:50:46 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Jean-Marc Valin | 879fbfd | 2008-02-20 17:17:13 +1100 | [diff] [blame] | 91 | /** Computes a cache of the pulses->bits mapping in each band */ |
Jean-Marc Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 92 | opus_int16 **compute_alloc_cache(CELTMode *m, int M); |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 93 | |
Jean-Marc Valin | 879fbfd | 2008-02-20 17:17:13 +1100 | [diff] [blame] | 94 | /** Compute the pulse allocation, i.e. how many pulses will go in each |
Gregory Maxwell | 71d39ad | 2011-07-30 00:00:29 -0400 | [diff] [blame^] | 95 | * band. |
Jean-Marc Valin | 879fbfd | 2008-02-20 17:17:13 +1100 | [diff] [blame] | 96 | @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. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 103 | int 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 Valin | d77d6a5 | 2011-07-29 17:33:06 -0400 | [diff] [blame] | 104 | 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 Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 105 | |
| 106 | |
| 107 | #endif |