summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libmusepack/mpc_bits_reader.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libmusepack/mpc_bits_reader.h')
-rw-r--r--lib/rbcodec/codecs/libmusepack/mpc_bits_reader.h175
1 files changed, 175 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libmusepack/mpc_bits_reader.h b/lib/rbcodec/codecs/libmusepack/mpc_bits_reader.h
new file mode 100644
index 0000000000..1233720c74
--- /dev/null
+++ b/lib/rbcodec/codecs/libmusepack/mpc_bits_reader.h
@@ -0,0 +1,175 @@
1/*
2 Copyright (c) 2007-2009, The Musepack Development Team
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 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
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
16
17 * Neither the name of the The Musepack Development Team nor the
18 names of its contributors may be used to endorse or promote
19 products derived from this software without specific prior
20 written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*/
34
35#define MAX_ENUM 32
36
37MPC_API int mpc_bits_get_block(mpc_bits_reader * r, mpc_block * p_block);
38mpc_int32_t mpc_bits_golomb_dec(mpc_bits_reader * r, const mpc_uint_t k);
39MPC_API unsigned int mpc_bits_get_size(mpc_bits_reader * r, mpc_uint64_t * p_size);
40mpc_uint32_t mpc_bits_log_dec(mpc_bits_reader * r, mpc_uint_t max);
41
42extern const mpc_uint32_t Cnk [MAX_ENUM / 2][MAX_ENUM];
43extern const mpc_uint8_t Cnk_len [MAX_ENUM / 2][MAX_ENUM];
44extern const mpc_uint32_t Cnk_lost[MAX_ENUM / 2][MAX_ENUM];
45
46// can read up to 31 bits
47static mpc_inline mpc_uint32_t mpc_bits_read(mpc_bits_reader * r, const unsigned int nb_bits)
48{
49 mpc_uint32_t ret;
50
51 r->buff -= (int)(r->count - nb_bits) >> 3;
52 r->count = (r->count - nb_bits) & 0x07;
53
54 ret = (r->buff[0] | (r->buff[-1] << 8)) >> r->count;
55 if (nb_bits > (16 - r->count)) {
56 ret |= (mpc_uint32_t)((r->buff[-2] << 16) | (r->buff[-3] << 24)) >> r->count;
57 if (nb_bits > 24 && r->count != 0)
58 ret |= r->buff[-4] << (32 - r->count);
59 }
60
61 return ret & ((1 << nb_bits) - 1);
62}
63
64#if defined(CPU_COLDFIRE)
65/* rockbox: This is specific code to optimize demux performance on Coldfire
66 * CPUs. Coldfire CPUs are very sensible to RAM accesses. As the bitstream
67 * buffer does not fit into IRAM the read accesses to the uint8 buffer are very
68 * expensive in terms of CPU cycles.
69 * The following code uses two variables in IRAM. The variable last_code keeps
70 * the 4-byte value of buf[0]<<16 | buf[1]<<8 | buf[2]. As long as buf[0] will
71 * read from the same address the following code will avoid re-reading of the
72 * buffers. If buf[0] did advance to the next uint8-entry since the last call
73 * the following will only need to load 1 uint8-entry instead of 3.
74 */
75static mpc_inline mpc_uint16_t get_code_from_buffer(mpc_bits_reader *r)
76{
77 /* Buffer advanced by 1 entry since last call */
78 if (r->buff == r->buffered_addr + 1) {
79 r->buffered_code = (r->buffered_code<<8) | r->buff[2];
80 r->buffered_addr = r->buff;
81 }
82 /* Buffer must be fully re-read */
83 else if (r->buff != r->buffered_addr) {
84 r->buffered_code = (r->buff[0] << 16) | (r->buff[1] << 8) | r->buff[2];
85 r->buffered_addr = r->buff;
86 }
87
88 return (mpc_uint16_t)((r->buffered_code >> r->count) & 0xFFFF);
89}
90#else
91/* Use the decoder's default implementation. This is faster on non-Coldfire targets */
92#define get_code_from_buffer(r) (mpc_uint16_t)((((r->buff[0] << 16) | (r->buff[1] << 8) | r->buff[2]) >> r->count) & 0xFFFF);
93#endif
94
95// basic huffman decoding routine
96// works with maximum lengths up to 16
97static mpc_inline mpc_int32_t mpc_bits_huff_dec(mpc_bits_reader * r, const mpc_huffman *Table)
98{
99 const mpc_uint16_t code = get_code_from_buffer(r);
100
101 while (code < Table->Code) Table++;
102
103 r->buff -= (int)(r->count - Table->Length) >> 3;
104 r->count = (r->count - Table->Length) & 0x07;
105
106 return Table->Value;
107}
108
109static mpc_inline mpc_int32_t mpc_bits_can_dec(mpc_bits_reader * r, const mpc_can_data *can)
110{
111 const mpc_uint16_t code = get_code_from_buffer(r);
112 const mpc_huff_lut tmp = can->lut[code >> (16 - LUT_DEPTH)];
113 const mpc_huffman * Table;
114
115 if (tmp.Length != 0) {
116 r->buff -= (int)(r->count - tmp.Length) >> 3;
117 r->count = (r->count - tmp.Length) & 0x07;
118 return tmp.Value;
119 }
120
121 Table = can->table + (unsigned char)tmp.Value;
122 while (code < Table->Code) Table++;
123
124 r->buff -= (int)(r->count - Table->Length) >> 3;
125 r->count = (r->count - Table->Length) & 0x07;
126
127 return can->sym[(Table->Value - (code >> (16 - Table->Length))) & 0xFF] ;
128}
129
130// LUT-based huffman decoding routine
131// works with maximum lengths up to 16
132static mpc_inline mpc_int32_t mpc_bits_huff_lut(mpc_bits_reader * r, const mpc_lut_data *lut)
133{
134 const mpc_uint16_t code = get_code_from_buffer(r);
135 const mpc_huff_lut tmp = lut->lut[code >> (16 - LUT_DEPTH)];
136 const mpc_huffman * Table;
137
138 if (tmp.Length != 0) {
139 r->buff -= (int)(r->count - tmp.Length) >> 3;
140 r->count = (r->count - tmp.Length) & 0x07;
141 return tmp.Value;
142 }
143
144 Table = lut->table + (unsigned char)tmp.Value;
145 while (code < Table->Code) Table++;
146
147 r->buff -= (int)(r->count - Table->Length) >> 3;
148 r->count = (r->count - Table->Length) & 0x07;
149
150 return Table->Value;
151}
152
153static mpc_inline mpc_uint32_t mpc_bits_enum_dec(mpc_bits_reader * r, mpc_uint_t k, mpc_uint_t n)
154{
155 mpc_uint32_t bits = 0;
156 mpc_uint32_t code;
157 const mpc_uint32_t * C = Cnk[k-1];
158
159 code = mpc_bits_read(r, Cnk_len[k-1][n-1] - 1);
160
161 if (code >= Cnk_lost[k-1][n-1])
162 code = ((code << 1) | mpc_bits_read(r, 1)) - Cnk_lost[k-1][n-1];
163
164 do {
165 n--;
166 if (code >= C[n]) {
167 bits |= 1 << n;
168 code -= C[n];
169 C -= MAX_ENUM;
170 k--;
171 }
172 } while(k > 0);
173
174 return bits;
175}