Line | Count | Source |
1 | | /* Copyright (c) 2007 CSIRO |
2 | | Copyright (c) 2007-2009 Xiph.Org Foundation |
3 | | Written by Jean-Marc Valin */ |
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 | | 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 COPYRIGHT OWNER |
20 | | OR 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 | | */ |
28 | | |
29 | | #ifdef HAVE_CONFIG_H |
30 | | #include "config.h" |
31 | | #endif |
32 | | |
33 | | #include "laplace.h" |
34 | | #include "mathops.h" |
35 | | |
36 | | /* The minimum probability of an energy delta (out of 32768). */ |
37 | 287M | #define LAPLACE_LOG_MINP (0) |
38 | 287M | #define LAPLACE_MINP (1<<LAPLACE_LOG_MINP) |
39 | | /* The minimum number of guaranteed representable energy deltas (in one |
40 | | direction). */ |
41 | 92.3M | #define LAPLACE_NMIN (16) |
42 | | |
43 | | /* When called, decay is positive and at most 11456. */ |
44 | | static unsigned ec_laplace_get_freq1(unsigned fs0, int decay) |
45 | 92.3M | { |
46 | 92.3M | unsigned ft; |
47 | 92.3M | ft = 32768 - LAPLACE_MINP*(2*LAPLACE_NMIN) - fs0; |
48 | 92.3M | return ft*(opus_int32)(16384-decay)>>15; |
49 | 92.3M | } |
50 | | |
51 | | void ec_laplace_encode(ec_enc *enc, int *value, unsigned fs, int decay) |
52 | 140M | { |
53 | 140M | unsigned fl; |
54 | 140M | int val = *value; |
55 | 140M | fl = 0; |
56 | 140M | if (val) |
57 | 91.7M | { |
58 | 91.7M | int s; |
59 | 91.7M | int i; |
60 | 91.7M | s = -(val<0); |
61 | 91.7M | val = (val+s)^s; |
62 | 91.7M | fl = fs; |
63 | 91.7M | fs = ec_laplace_get_freq1(fs, decay); |
64 | | /* Search the decaying part of the PDF.*/ |
65 | 189M | for (i=1; fs > 0 && i < val; i++) |
66 | 97.9M | { |
67 | 97.9M | fs *= 2; |
68 | 97.9M | fl += fs+2*LAPLACE_MINP; |
69 | 97.9M | fs = (fs*(opus_int32)decay)>>15; |
70 | 97.9M | } |
71 | | /* Everything beyond that has probability LAPLACE_MINP. */ |
72 | 91.7M | if (!fs) |
73 | 1.71M | { |
74 | 1.71M | int di; |
75 | 1.71M | int ndi_max; |
76 | 1.71M | ndi_max = (32768-fl+LAPLACE_MINP-1)>>LAPLACE_LOG_MINP; |
77 | 1.71M | ndi_max = (ndi_max-s)>>1; |
78 | 1.71M | di = IMIN(val - i, ndi_max - 1); |
79 | 1.71M | fl += (2*di+1+s)*LAPLACE_MINP; |
80 | 1.71M | fs = IMIN(LAPLACE_MINP, 32768-fl); |
81 | 1.71M | *value = (i+di+s)^s; |
82 | 1.71M | } |
83 | 90.0M | else |
84 | 90.0M | { |
85 | 90.0M | fs += LAPLACE_MINP; |
86 | 90.0M | fl += fs&~s; |
87 | 90.0M | } |
88 | 91.7M | celt_assert(fl+fs<=32768); |
89 | 91.7M | celt_assert(fs>0); |
90 | 91.7M | } |
91 | 140M | ec_encode_bin(enc, fl, fl+fs, 15); |
92 | 140M | } |
93 | | |
94 | | int ec_laplace_decode(ec_dec *dec, unsigned fs, int decay) |
95 | 1.16M | { |
96 | 1.16M | int val=0; |
97 | 1.16M | unsigned fl; |
98 | 1.16M | unsigned fm; |
99 | 1.16M | fm = ec_decode_bin(dec, 15); |
100 | 1.16M | fl = 0; |
101 | 1.16M | if (fm >= fs) |
102 | 545k | { |
103 | 545k | val++; |
104 | 545k | fl = fs; |
105 | 545k | fs = ec_laplace_get_freq1(fs, decay)+LAPLACE_MINP; |
106 | | /* Search the decaying part of the PDF.*/ |
107 | 823k | while(fs > LAPLACE_MINP && fm >= fl+2*fs) |
108 | 278k | { |
109 | 278k | fs *= 2; |
110 | 278k | fl += fs; |
111 | 278k | fs = ((fs-2*LAPLACE_MINP)*(opus_int32)decay)>>15; |
112 | 278k | fs += LAPLACE_MINP; |
113 | 278k | val++; |
114 | 278k | } |
115 | | /* Everything beyond that has probability LAPLACE_MINP. */ |
116 | 545k | if (fs <= LAPLACE_MINP) |
117 | 5.93k | { |
118 | 5.93k | int di; |
119 | 5.93k | di = (fm-fl)>>(LAPLACE_LOG_MINP+1); |
120 | 5.93k | val += di; |
121 | 5.93k | fl += 2*di*LAPLACE_MINP; |
122 | 5.93k | } |
123 | 545k | if (fm < fl+fs) |
124 | 273k | val = -val; |
125 | 272k | else |
126 | 272k | fl += fs; |
127 | 545k | } |
128 | 1.16M | celt_assert(fl<32768); |
129 | 1.16M | celt_assert(fs>0); |
130 | 1.16M | celt_assert(fl<=fm); |
131 | 1.16M | celt_assert(fm<IMIN(fl+fs,32768)); |
132 | 1.16M | ec_dec_update(dec, fl, IMIN(fl+fs,32768), 32768); |
133 | 1.16M | return val; |
134 | 1.16M | } |