1 /* Copyright (c) 2007 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
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.
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.
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.
40 /* The minimum probability of an energy delta (out of 32768). */
41 #define LAPLACE_LOG_MINP (0)
42 #define LAPLACE_MINP (1<<LAPLACE_LOG_MINP)
43 /* The minimum number of guaranteed representable energy deltas (in one
45 #define LAPLACE_NMIN (16)
47 int ec_laplace_get_start_freq(int decay)
49 celt_uint32 ft = 32768 - LAPLACE_MINP*(2*LAPLACE_NMIN+1);
50 int fs = (ft*(16384-decay))/(16384+decay);
51 return fs+LAPLACE_MINP;
54 static int ec_laplace_get_freq1(int fs0, int decay)
57 ft = 32768 - LAPLACE_MINP*(2*LAPLACE_NMIN) - fs0;
58 return ft*(16384-decay)>>15;
61 void ec_laplace_encode_start(ec_enc *enc, int *value, int decay, int fs)
73 fs = ec_laplace_get_freq1(fs, decay);
74 /* Search the decaying part of the PDF.*/
75 for (i=1; fs > 0 && i < val; i++)
78 fl += fs+2*LAPLACE_MINP;
79 fs = (fs*(celt_int32)decay)>>15;
81 /* Everything beyond that has probability LAPLACE_MINP. */
86 di_max = (32768-fl+LAPLACE_MINP-1)>>LAPLACE_LOG_MINP;
87 di_max = (di_max-s)>>1;
88 di = IMIN(val - i, di_max);
89 fl += (2*di+1+s)*LAPLACE_MINP;
90 fs = IMIN(LAPLACE_MINP, 32768-fl);
98 celt_assert(fl+fs<=32768);
101 ec_encode_bin(enc, fl, fl+fs, 15);
105 void ec_laplace_encode(ec_enc *enc, int *value, int decay)
107 int fs = ec_laplace_get_start_freq(decay);
108 ec_laplace_encode_start(enc, value, decay, fs);
112 int ec_laplace_decode_start(ec_dec *dec, int decay, int fs)
117 fm = ec_decode_bin(dec, 15);
123 fs = ec_laplace_get_freq1(fs, decay)+LAPLACE_MINP;
124 /* Search the decaying part of the PDF.*/
125 while(fs > LAPLACE_MINP && fm >= fl+2*fs)
129 fs = ((fs-2*LAPLACE_MINP)*(celt_int32)decay)>>15;
133 /* Everything beyond that has probability LAPLACE_MINP. */
134 if (fs <= LAPLACE_MINP)
137 di = (fm-fl)>>(LAPLACE_LOG_MINP+1);
139 fl += 2*di*LAPLACE_MINP;
146 celt_assert(fl<32768);
149 celt_assert(fm<IMIN(fl+fs,32768));
150 ec_dec_update(dec, fl, IMIN(fl+fs,32768), 32768);
154 int ec_laplace_decode(ec_dec *dec, int decay)
156 int fs = ec_laplace_get_start_freq(decay);
157 return ec_laplace_decode_start(dec, decay, fs);