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 |
Jean-Marc Valin | cb05e7c | 2012-04-20 16:40:24 -0400 | [diff] [blame] | 19 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
| 20 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 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 | 6f18d11 | 2009-05-20 22:50:46 -0400 | [diff] [blame] | 43 | #include "cwrs.h" |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 44 | #include "modes.h" |
| 45 | |
| 46 | void compute_pulse_cache(CELTMode *m, int LM); |
Jean-Marc Valin | 6f18d11 | 2009-05-20 22:50:46 -0400 | [diff] [blame] | 47 | |
Gregory Maxwell | 7830cf1 | 2013-10-17 15:56:52 -0700 | [diff] [blame] | 48 | static OPUS_INLINE int get_pulses(int i) |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 49 | { |
| 50 | return i<8 ? i : (8 + (i&7)) << ((i>>3)-1); |
| 51 | } |
| 52 | |
Gregory Maxwell | 7830cf1 | 2013-10-17 15:56:52 -0700 | [diff] [blame] | 53 | static OPUS_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] | 54 | { |
| 55 | int i; |
| 56 | int lo, hi; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 57 | const unsigned char *cache; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 58 | |
| 59 | LM++; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 60 | cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band]; |
Jean-Marc Valin | 2fcb239 | 2010-05-24 09:02:35 -0400 | [diff] [blame] | 61 | |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 62 | lo = 0; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 63 | hi = cache[0]; |
Timothy B. Terriberry | 1cb32aa | 2011-01-04 09:07:40 -0800 | [diff] [blame] | 64 | bits--; |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 65 | for (i=0;i<LOG_MAX_PSEUDO;i++) |
| 66 | { |
Timothy B. Terriberry | 1cb32aa | 2011-01-04 09:07:40 -0800 | [diff] [blame] | 67 | int mid = (lo+hi+1)>>1; |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 68 | /* OPT: Make sure this is implemented with a conditional move */ |
Jean-Marc Valin | 905197d | 2012-03-08 14:09:09 -0500 | [diff] [blame] | 69 | if ((int)cache[mid] >= bits) |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 70 | hi = mid; |
| 71 | else |
| 72 | lo = mid; |
| 73 | } |
Jean-Marc Valin | 905197d | 2012-03-08 14:09:09 -0500 | [diff] [blame] | 74 | if (bits- (lo == 0 ? -1 : (int)cache[lo]) <= (int)cache[hi]-bits) |
Jean-Marc Valin | 164a229 | 2009-07-22 07:48:35 -0400 | [diff] [blame] | 75 | return lo; |
| 76 | else |
| 77 | return hi; |
Jean-Marc Valin | 7b0cb4b | 2008-09-11 00:20:08 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Gregory Maxwell | 7830cf1 | 2013-10-17 15:56:52 -0700 | [diff] [blame] | 80 | static OPUS_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] | 81 | { |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 82 | const unsigned char *cache; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 83 | |
| 84 | LM++; |
Jean-Marc Valin | 732ea38 | 2010-08-25 13:52:27 -0400 | [diff] [blame] | 85 | cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band]; |
Jean-Marc Valin | 3ad8db4 | 2010-08-25 13:11:09 -0400 | [diff] [blame] | 86 | return pulses == 0 ? 0 : cache[pulses]+1; |
Jean-Marc Valin | 6f18d11 | 2009-05-20 22:50:46 -0400 | [diff] [blame] | 87 | } |
| 88 | |
Jean-Marc Valin | 879fbfd | 2008-02-20 17:17:13 +1100 | [diff] [blame] | 89 | /** 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] | 90 | * band. |
Jean-Marc Valin | 879fbfd | 2008-02-20 17:17:13 +1100 | [diff] [blame] | 91 | @param m mode |
| 92 | @param offsets Requested increase or decrease in the number of bits for |
| 93 | each band |
| 94 | @param total Number of bands |
| 95 | @param pulses Number of pulses per band (returned) |
| 96 | @return Total number of bits allocated |
| 97 | */ |
Timothy B. Terriberry | c564307 | 2011-01-29 12:57:18 -0800 | [diff] [blame] | 98 | 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 | 5fb50ad | 2012-12-21 01:23:26 -0500 | [diff] [blame] | 99 | 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); |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 100 | |
Jean-Marc Valin | f51ca49 | 2008-01-17 10:58:38 +1100 | [diff] [blame] | 101 | #endif |