blob: 515f7687cec6f6f068c3fcdc6745f27527514e69 [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
Jean-Marc Valincb05e7c2012-04-20 16:40:24 -040019 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 Valinf51ca492008-01-17 10:58:38 +110021 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 Valin68a0e042015-12-31 14:43:49 -050035#define CELT_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 Valin6f18d112009-05-20 22:50:46 -040043#include "cwrs.h"
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040044#include "modes.h"
45
46void compute_pulse_cache(CELTMode *m, int LM);
Jean-Marc Valin6f18d112009-05-20 22:50:46 -040047
Gregory Maxwell7830cf12013-10-17 15:56:52 -070048static OPUS_INLINE int get_pulses(int i)
Jean-Marc Valin164a2292009-07-22 07:48:35 -040049{
50 return i<8 ? i : (8 + (i&7)) << ((i>>3)-1);
51}
52
Gregory Maxwell7830cf12013-10-17 15:56:52 -070053static OPUS_INLINE int bits2pulses(const CELTMode *m, int band, int LM, int bits)
Jean-Marc Valin7b0cb4b2008-09-11 00:20:08 -040054{
55 int i;
56 int lo, hi;
Jean-Marc Valin732ea382010-08-25 13:52:27 -040057 const unsigned char *cache;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040058
59 LM++;
Jean-Marc Valin732ea382010-08-25 13:52:27 -040060 cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band];
Jean-Marc Valin2fcb2392010-05-24 09:02:35 -040061
Jean-Marc Valin164a2292009-07-22 07:48:35 -040062 lo = 0;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040063 hi = cache[0];
Timothy B. Terriberry1cb32aa2011-01-04 09:07:40 -080064 bits--;
Jean-Marc Valin164a2292009-07-22 07:48:35 -040065 for (i=0;i<LOG_MAX_PSEUDO;i++)
66 {
Timothy B. Terriberry1cb32aa2011-01-04 09:07:40 -080067 int mid = (lo+hi+1)>>1;
Jean-Marc Valin164a2292009-07-22 07:48:35 -040068 /* OPT: Make sure this is implemented with a conditional move */
Jean-Marc Valin905197d2012-03-08 14:09:09 -050069 if ((int)cache[mid] >= bits)
Jean-Marc Valin164a2292009-07-22 07:48:35 -040070 hi = mid;
71 else
72 lo = mid;
73 }
Jean-Marc Valin905197d2012-03-08 14:09:09 -050074 if (bits- (lo == 0 ? -1 : (int)cache[lo]) <= (int)cache[hi]-bits)
Jean-Marc Valin164a2292009-07-22 07:48:35 -040075 return lo;
76 else
77 return hi;
Jean-Marc Valin7b0cb4b2008-09-11 00:20:08 -040078}
79
Gregory Maxwell7830cf12013-10-17 15:56:52 -070080static OPUS_INLINE int pulses2bits(const CELTMode *m, int band, int LM, int pulses)
Jean-Marc Valin6f18d112009-05-20 22:50:46 -040081{
Jean-Marc Valin732ea382010-08-25 13:52:27 -040082 const unsigned char *cache;
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040083
84 LM++;
Jean-Marc Valin732ea382010-08-25 13:52:27 -040085 cache = m->cache.bits + m->cache.index[LM*m->nbEBands+band];
Jean-Marc Valin3ad8db42010-08-25 13:11:09 -040086 return pulses == 0 ? 0 : cache[pulses]+1;
Jean-Marc Valin6f18d112009-05-20 22:50:46 -040087}
88
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +110089/** Compute the pulse allocation, i.e. how many pulses will go in each
Gregory Maxwell71d39ad2011-07-30 00:00:29 -040090 * band.
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +110091 @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. Terriberryc5643072011-01-29 12:57:18 -080098int 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 Valin5fb50ad2012-12-21 01:23:26 -050099 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 Valinf51ca492008-01-17 10:58:38 +1100100
Jean-Marc Valinf51ca492008-01-17 10:58:38 +1100101#endif